re PR c++/34055 (ICE with invalid specialization of variadic template)
[platform/upstream/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a system header.  */
75   BOOL_BITFIELD in_system_header : 1;
76   /* True if this token is from a context where it is implicitly extern "C" */
77   BOOL_BITFIELD implicit_extern_c : 1;
78   /* True for a CPP_NAME token that is not a keyword (i.e., for which
79      KEYWORD is RID_MAX) iff this name was looked up and found to be
80      ambiguous.  An error has already been reported.  */
81   BOOL_BITFIELD ambiguous_p : 1;
82   /* The 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_array:
1075           found = true;
1076           break;
1077
1078         case cdk_error:
1079           return true;
1080
1081         default:
1082           declarator = declarator->declarator;
1083           break;
1084         }
1085     }
1086
1087   return !found;
1088 }
1089
1090 cp_parameter_declarator *no_parameters;
1091
1092 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1093    DECLARATOR and DEFAULT_ARGUMENT.  */
1094
1095 cp_parameter_declarator *
1096 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1097                            cp_declarator *declarator,
1098                            tree default_argument)
1099 {
1100   cp_parameter_declarator *parameter;
1101
1102   parameter = ((cp_parameter_declarator *)
1103                alloc_declarator (sizeof (cp_parameter_declarator)));
1104   parameter->next = NULL;
1105   if (decl_specifiers)
1106     parameter->decl_specifiers = *decl_specifiers;
1107   else
1108     clear_decl_specs (&parameter->decl_specifiers);
1109   parameter->declarator = declarator;
1110   parameter->default_argument = default_argument;
1111   parameter->ellipsis_p = false;
1112
1113   return parameter;
1114 }
1115
1116 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1117
1118 static bool
1119 function_declarator_p (const cp_declarator *declarator)
1120 {
1121   while (declarator)
1122     {
1123       if (declarator->kind == cdk_function
1124           && declarator->declarator->kind == cdk_id)
1125         return true;
1126       if (declarator->kind == cdk_id
1127           || declarator->kind == cdk_error)
1128         return false;
1129       declarator = declarator->declarator;
1130     }
1131   return false;
1132 }
1133  
1134 /* The parser.  */
1135
1136 /* Overview
1137    --------
1138
1139    A cp_parser parses the token stream as specified by the C++
1140    grammar.  Its job is purely parsing, not semantic analysis.  For
1141    example, the parser breaks the token stream into declarators,
1142    expressions, statements, and other similar syntactic constructs.
1143    It does not check that the types of the expressions on either side
1144    of an assignment-statement are compatible, or that a function is
1145    not declared with a parameter of type `void'.
1146
1147    The parser invokes routines elsewhere in the compiler to perform
1148    semantic analysis and to build up the abstract syntax tree for the
1149    code processed.
1150
1151    The parser (and the template instantiation code, which is, in a
1152    way, a close relative of parsing) are the only parts of the
1153    compiler that should be calling push_scope and pop_scope, or
1154    related functions.  The parser (and template instantiation code)
1155    keeps track of what scope is presently active; everything else
1156    should simply honor that.  (The code that generates static
1157    initializers may also need to set the scope, in order to check
1158    access control correctly when emitting the initializers.)
1159
1160    Methodology
1161    -----------
1162
1163    The parser is of the standard recursive-descent variety.  Upcoming
1164    tokens in the token stream are examined in order to determine which
1165    production to use when parsing a non-terminal.  Some C++ constructs
1166    require arbitrary look ahead to disambiguate.  For example, it is
1167    impossible, in the general case, to tell whether a statement is an
1168    expression or declaration without scanning the entire statement.
1169    Therefore, the parser is capable of "parsing tentatively."  When the
1170    parser is not sure what construct comes next, it enters this mode.
1171    Then, while we attempt to parse the construct, the parser queues up
1172    error messages, rather than issuing them immediately, and saves the
1173    tokens it consumes.  If the construct is parsed successfully, the
1174    parser "commits", i.e., it issues any queued error messages and
1175    the tokens that were being preserved are permanently discarded.
1176    If, however, the construct is not parsed successfully, the parser
1177    rolls back its state completely so that it can resume parsing using
1178    a different alternative.
1179
1180    Future Improvements
1181    -------------------
1182
1183    The performance of the parser could probably be improved substantially.
1184    We could often eliminate the need to parse tentatively by looking ahead
1185    a little bit.  In some places, this approach might not entirely eliminate
1186    the need to parse tentatively, but it might still speed up the average
1187    case.  */
1188
1189 /* Flags that are passed to some parsing functions.  These values can
1190    be bitwise-ored together.  */
1191
1192 typedef enum cp_parser_flags
1193 {
1194   /* No flags.  */
1195   CP_PARSER_FLAGS_NONE = 0x0,
1196   /* The construct is optional.  If it is not present, then no error
1197      should be issued.  */
1198   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1199   /* When parsing a type-specifier, do not allow user-defined types.  */
1200   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1201 } cp_parser_flags;
1202
1203 /* The different kinds of declarators we want to parse.  */
1204
1205 typedef enum cp_parser_declarator_kind
1206 {
1207   /* We want an abstract declarator.  */
1208   CP_PARSER_DECLARATOR_ABSTRACT,
1209   /* We want a named declarator.  */
1210   CP_PARSER_DECLARATOR_NAMED,
1211   /* We don't mind, but the name must be an unqualified-id.  */
1212   CP_PARSER_DECLARATOR_EITHER
1213 } cp_parser_declarator_kind;
1214
1215 /* The precedence values used to parse binary expressions.  The minimum value
1216    of PREC must be 1, because zero is reserved to quickly discriminate
1217    binary operators from other tokens.  */
1218
1219 enum cp_parser_prec
1220 {
1221   PREC_NOT_OPERATOR,
1222   PREC_LOGICAL_OR_EXPRESSION,
1223   PREC_LOGICAL_AND_EXPRESSION,
1224   PREC_INCLUSIVE_OR_EXPRESSION,
1225   PREC_EXCLUSIVE_OR_EXPRESSION,
1226   PREC_AND_EXPRESSION,
1227   PREC_EQUALITY_EXPRESSION,
1228   PREC_RELATIONAL_EXPRESSION,
1229   PREC_SHIFT_EXPRESSION,
1230   PREC_ADDITIVE_EXPRESSION,
1231   PREC_MULTIPLICATIVE_EXPRESSION,
1232   PREC_PM_EXPRESSION,
1233   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1234 };
1235
1236 /* A mapping from a token type to a corresponding tree node type, with a
1237    precedence value.  */
1238
1239 typedef struct cp_parser_binary_operations_map_node
1240 {
1241   /* The token type.  */
1242   enum cpp_ttype token_type;
1243   /* The corresponding tree code.  */
1244   enum tree_code tree_type;
1245   /* The precedence of this operator.  */
1246   enum cp_parser_prec prec;
1247 } cp_parser_binary_operations_map_node;
1248
1249 /* The status of a tentative parse.  */
1250
1251 typedef enum cp_parser_status_kind
1252 {
1253   /* No errors have occurred.  */
1254   CP_PARSER_STATUS_KIND_NO_ERROR,
1255   /* An error has occurred.  */
1256   CP_PARSER_STATUS_KIND_ERROR,
1257   /* We are committed to this tentative parse, whether or not an error
1258      has occurred.  */
1259   CP_PARSER_STATUS_KIND_COMMITTED
1260 } cp_parser_status_kind;
1261
1262 typedef struct cp_parser_expression_stack_entry
1263 {
1264   /* Left hand side of the binary operation we are currently
1265      parsing.  */
1266   tree lhs;
1267   /* Original tree code for left hand side, if it was a binary
1268      expression itself (used for -Wparentheses).  */
1269   enum tree_code lhs_type;
1270   /* Tree code for the binary operation we are parsing.  */
1271   enum tree_code tree_type;
1272   /* Precedence of the binary operation we are parsing.  */
1273   int prec;
1274 } cp_parser_expression_stack_entry;
1275
1276 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1277    entries because precedence levels on the stack are monotonically
1278    increasing.  */
1279 typedef struct cp_parser_expression_stack_entry
1280   cp_parser_expression_stack[NUM_PREC_VALUES];
1281
1282 /* Context that is saved and restored when parsing tentatively.  */
1283 typedef struct cp_parser_context GTY (())
1284 {
1285   /* If this is a tentative parsing context, the status of the
1286      tentative parse.  */
1287   enum cp_parser_status_kind status;
1288   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1289      that are looked up in this context must be looked up both in the
1290      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1291      the context of the containing expression.  */
1292   tree object_type;
1293
1294   /* The next parsing context in the stack.  */
1295   struct cp_parser_context *next;
1296 } cp_parser_context;
1297
1298 /* Prototypes.  */
1299
1300 /* Constructors and destructors.  */
1301
1302 static cp_parser_context *cp_parser_context_new
1303   (cp_parser_context *);
1304
1305 /* Class variables.  */
1306
1307 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1308
1309 /* The operator-precedence table used by cp_parser_binary_expression.
1310    Transformed into an associative array (binops_by_token) by
1311    cp_parser_new.  */
1312
1313 static const cp_parser_binary_operations_map_node binops[] = {
1314   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1315   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1316
1317   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1318   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320
1321   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1322   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323
1324   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1325   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326
1327   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1328   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1330   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331
1332   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1333   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1334
1335   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1336
1337   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1338
1339   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1340
1341   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1342
1343   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1344 };
1345
1346 /* The same as binops, but initialized by cp_parser_new so that
1347    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1348    for speed.  */
1349 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1350
1351 /* Constructors and destructors.  */
1352
1353 /* Construct a new context.  The context below this one on the stack
1354    is given by NEXT.  */
1355
1356 static cp_parser_context *
1357 cp_parser_context_new (cp_parser_context* next)
1358 {
1359   cp_parser_context *context;
1360
1361   /* Allocate the storage.  */
1362   if (cp_parser_context_free_list != NULL)
1363     {
1364       /* Pull the first entry from the free list.  */
1365       context = cp_parser_context_free_list;
1366       cp_parser_context_free_list = context->next;
1367       memset (context, 0, sizeof (*context));
1368     }
1369   else
1370     context = GGC_CNEW (cp_parser_context);
1371
1372   /* No errors have occurred yet in this context.  */
1373   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1374   /* If this is not the bottomost context, copy information that we
1375      need from the previous context.  */
1376   if (next)
1377     {
1378       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1379          expression, then we are parsing one in this context, too.  */
1380       context->object_type = next->object_type;
1381       /* Thread the stack.  */
1382       context->next = next;
1383     }
1384
1385   return context;
1386 }
1387
1388 /* The cp_parser structure represents the C++ parser.  */
1389
1390 typedef struct cp_parser GTY(())
1391 {
1392   /* The lexer from which we are obtaining tokens.  */
1393   cp_lexer *lexer;
1394
1395   /* The scope in which names should be looked up.  If NULL_TREE, then
1396      we look up names in the scope that is currently open in the
1397      source program.  If non-NULL, this is either a TYPE or
1398      NAMESPACE_DECL for the scope in which we should look.  It can
1399      also be ERROR_MARK, when we've parsed a bogus scope.
1400
1401      This value is not cleared automatically after a name is looked
1402      up, so we must be careful to clear it before starting a new look
1403      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1404      will look up `Z' in the scope of `X', rather than the current
1405      scope.)  Unfortunately, it is difficult to tell when name lookup
1406      is complete, because we sometimes peek at a token, look it up,
1407      and then decide not to consume it.   */
1408   tree scope;
1409
1410   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1411      last lookup took place.  OBJECT_SCOPE is used if an expression
1412      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1413      respectively.  QUALIFYING_SCOPE is used for an expression of the
1414      form "X::Y"; it refers to X.  */
1415   tree object_scope;
1416   tree qualifying_scope;
1417
1418   /* A stack of parsing contexts.  All but the bottom entry on the
1419      stack will be tentative contexts.
1420
1421      We parse tentatively in order to determine which construct is in
1422      use in some situations.  For example, in order to determine
1423      whether a statement is an expression-statement or a
1424      declaration-statement we parse it tentatively as a
1425      declaration-statement.  If that fails, we then reparse the same
1426      token stream as an expression-statement.  */
1427   cp_parser_context *context;
1428
1429   /* True if we are parsing GNU C++.  If this flag is not set, then
1430      GNU extensions are not recognized.  */
1431   bool allow_gnu_extensions_p;
1432
1433   /* TRUE if the `>' token should be interpreted as the greater-than
1434      operator.  FALSE if it is the end of a template-id or
1435      template-parameter-list. In C++0x mode, this flag also applies to
1436      `>>' tokens, which are viewed as two consecutive `>' tokens when
1437      this flag is FALSE.  */
1438   bool greater_than_is_operator_p;
1439
1440   /* TRUE if default arguments are allowed within a parameter list
1441      that starts at this point. FALSE if only a gnu extension makes
1442      them permissible.  */
1443   bool default_arg_ok_p;
1444
1445   /* TRUE if we are parsing an integral constant-expression.  See
1446      [expr.const] for a precise definition.  */
1447   bool integral_constant_expression_p;
1448
1449   /* TRUE if we are parsing an integral constant-expression -- but a
1450      non-constant expression should be permitted as well.  This flag
1451      is used when parsing an array bound so that GNU variable-length
1452      arrays are tolerated.  */
1453   bool allow_non_integral_constant_expression_p;
1454
1455   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1456      been seen that makes the expression non-constant.  */
1457   bool non_integral_constant_expression_p;
1458
1459   /* TRUE if local variable names and `this' are forbidden in the
1460      current context.  */
1461   bool local_variables_forbidden_p;
1462
1463   /* TRUE if the declaration we are parsing is part of a
1464      linkage-specification of the form `extern string-literal
1465      declaration'.  */
1466   bool in_unbraced_linkage_specification_p;
1467
1468   /* TRUE if we are presently parsing a declarator, after the
1469      direct-declarator.  */
1470   bool in_declarator_p;
1471
1472   /* TRUE if we are presently parsing a template-argument-list.  */
1473   bool in_template_argument_list_p;
1474
1475   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1476      to IN_OMP_BLOCK if parsing OpenMP structured block and
1477      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1478      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1479      iteration-statement, OpenMP block or loop within that switch.  */
1480 #define IN_SWITCH_STMT          1
1481 #define IN_ITERATION_STMT       2
1482 #define IN_OMP_BLOCK            4
1483 #define IN_OMP_FOR              8
1484 #define IN_IF_STMT             16
1485   unsigned char in_statement;
1486
1487   /* TRUE if we are presently parsing the body of a switch statement.
1488      Note that this doesn't quite overlap with in_statement above.
1489      The difference relates to giving the right sets of error messages:
1490      "case not in switch" vs "break statement used with OpenMP...".  */
1491   bool in_switch_statement_p;
1492
1493   /* TRUE if we are parsing a type-id in an expression context.  In
1494      such a situation, both "type (expr)" and "type (type)" are valid
1495      alternatives.  */
1496   bool in_type_id_in_expr_p;
1497
1498   /* TRUE if we are currently in a header file where declarations are
1499      implicitly extern "C".  */
1500   bool implicit_extern_c;
1501
1502   /* TRUE if strings in expressions should be translated to the execution
1503      character set.  */
1504   bool translate_strings_p;
1505
1506   /* TRUE if we are presently parsing the body of a function, but not
1507      a local class.  */
1508   bool in_function_body;
1509
1510   /* If non-NULL, then we are parsing a construct where new type
1511      definitions are not permitted.  The string stored here will be
1512      issued as an error message if a type is defined.  */
1513   const char *type_definition_forbidden_message;
1514
1515   /* A list of lists. The outer list is a stack, used for member
1516      functions of local classes. At each level there are two sub-list,
1517      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1518      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1519      TREE_VALUE's. The functions are chained in reverse declaration
1520      order.
1521
1522      The TREE_PURPOSE sublist contains those functions with default
1523      arguments that need post processing, and the TREE_VALUE sublist
1524      contains those functions with definitions that need post
1525      processing.
1526
1527      These lists can only be processed once the outermost class being
1528      defined is complete.  */
1529   tree unparsed_functions_queues;
1530
1531   /* The number of classes whose definitions are currently in
1532      progress.  */
1533   unsigned num_classes_being_defined;
1534
1535   /* The number of template parameter lists that apply directly to the
1536      current declaration.  */
1537   unsigned num_template_parameter_lists;
1538 } cp_parser;
1539
1540 /* Prototypes.  */
1541
1542 /* Constructors and destructors.  */
1543
1544 static cp_parser *cp_parser_new
1545   (void);
1546
1547 /* Routines to parse various constructs.
1548
1549    Those that return `tree' will return the error_mark_node (rather
1550    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1551    Sometimes, they will return an ordinary node if error-recovery was
1552    attempted, even though a parse error occurred.  So, to check
1553    whether or not a parse error occurred, you should always use
1554    cp_parser_error_occurred.  If the construct is optional (indicated
1555    either by an `_opt' in the name of the function that does the
1556    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1557    the construct is not present.  */
1558
1559 /* Lexical conventions [gram.lex]  */
1560
1561 static tree cp_parser_identifier
1562   (cp_parser *);
1563 static tree cp_parser_string_literal
1564   (cp_parser *, bool, bool);
1565
1566 /* Basic concepts [gram.basic]  */
1567
1568 static bool cp_parser_translation_unit
1569   (cp_parser *);
1570
1571 /* Expressions [gram.expr]  */
1572
1573 static tree cp_parser_primary_expression
1574   (cp_parser *, bool, bool, bool, cp_id_kind *);
1575 static tree cp_parser_id_expression
1576   (cp_parser *, bool, bool, bool *, bool, bool);
1577 static tree cp_parser_unqualified_id
1578   (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier_opt
1580   (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier
1582   (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_class_or_namespace_name
1584   (cp_parser *, bool, bool, bool, bool, bool);
1585 static tree cp_parser_postfix_expression
1586   (cp_parser *, bool, bool, bool);
1587 static tree cp_parser_postfix_open_square_expression
1588   (cp_parser *, tree, bool);
1589 static tree cp_parser_postfix_dot_deref_expression
1590   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1591 static tree cp_parser_parenthesized_expression_list
1592   (cp_parser *, bool, bool, bool, bool *);
1593 static void cp_parser_pseudo_destructor_name
1594   (cp_parser *, tree *, tree *);
1595 static tree cp_parser_unary_expression
1596   (cp_parser *, bool, bool);
1597 static enum tree_code cp_parser_unary_operator
1598   (cp_token *);
1599 static tree cp_parser_new_expression
1600   (cp_parser *);
1601 static tree cp_parser_new_placement
1602   (cp_parser *);
1603 static tree cp_parser_new_type_id
1604   (cp_parser *, tree *);
1605 static cp_declarator *cp_parser_new_declarator_opt
1606   (cp_parser *);
1607 static cp_declarator *cp_parser_direct_new_declarator
1608   (cp_parser *);
1609 static tree cp_parser_new_initializer
1610   (cp_parser *);
1611 static tree cp_parser_delete_expression
1612   (cp_parser *);
1613 static tree cp_parser_cast_expression
1614   (cp_parser *, bool, bool);
1615 static tree cp_parser_binary_expression
1616   (cp_parser *, bool);
1617 static tree cp_parser_question_colon_clause
1618   (cp_parser *, tree);
1619 static tree cp_parser_assignment_expression
1620   (cp_parser *, bool);
1621 static enum tree_code cp_parser_assignment_operator_opt
1622   (cp_parser *);
1623 static tree cp_parser_expression
1624   (cp_parser *, bool);
1625 static tree cp_parser_constant_expression
1626   (cp_parser *, bool, bool *);
1627 static tree cp_parser_builtin_offsetof
1628   (cp_parser *);
1629
1630 /* Statements [gram.stmt.stmt]  */
1631
1632 static void cp_parser_statement
1633   (cp_parser *, tree, bool, bool *);
1634 static void cp_parser_label_for_labeled_statement
1635   (cp_parser *);
1636 static tree cp_parser_expression_statement
1637   (cp_parser *, tree);
1638 static tree cp_parser_compound_statement
1639   (cp_parser *, tree, bool);
1640 static void cp_parser_statement_seq_opt
1641   (cp_parser *, tree);
1642 static tree cp_parser_selection_statement
1643   (cp_parser *, bool *);
1644 static tree cp_parser_condition
1645   (cp_parser *);
1646 static tree cp_parser_iteration_statement
1647   (cp_parser *);
1648 static void cp_parser_for_init_statement
1649   (cp_parser *);
1650 static tree cp_parser_jump_statement
1651   (cp_parser *);
1652 static void cp_parser_declaration_statement
1653   (cp_parser *);
1654
1655 static tree cp_parser_implicitly_scoped_statement
1656   (cp_parser *, bool *);
1657 static void cp_parser_already_scoped_statement
1658   (cp_parser *);
1659
1660 /* Declarations [gram.dcl.dcl] */
1661
1662 static void cp_parser_declaration_seq_opt
1663   (cp_parser *);
1664 static void cp_parser_declaration
1665   (cp_parser *);
1666 static void cp_parser_block_declaration
1667   (cp_parser *, bool);
1668 static void cp_parser_simple_declaration
1669   (cp_parser *, bool);
1670 static void cp_parser_decl_specifier_seq
1671   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1672 static tree cp_parser_storage_class_specifier_opt
1673   (cp_parser *);
1674 static tree cp_parser_function_specifier_opt
1675   (cp_parser *, cp_decl_specifier_seq *);
1676 static tree cp_parser_type_specifier
1677   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1678    int *, bool *);
1679 static tree cp_parser_simple_type_specifier
1680   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1681 static tree cp_parser_type_name
1682   (cp_parser *);
1683 static tree cp_parser_elaborated_type_specifier
1684   (cp_parser *, bool, bool);
1685 static tree cp_parser_enum_specifier
1686   (cp_parser *);
1687 static void cp_parser_enumerator_list
1688   (cp_parser *, tree);
1689 static void cp_parser_enumerator_definition
1690   (cp_parser *, tree);
1691 static tree cp_parser_namespace_name
1692   (cp_parser *);
1693 static void cp_parser_namespace_definition
1694   (cp_parser *);
1695 static void cp_parser_namespace_body
1696   (cp_parser *);
1697 static tree cp_parser_qualified_namespace_specifier
1698   (cp_parser *);
1699 static void cp_parser_namespace_alias_definition
1700   (cp_parser *);
1701 static bool cp_parser_using_declaration
1702   (cp_parser *, bool);
1703 static void cp_parser_using_directive
1704   (cp_parser *);
1705 static void cp_parser_asm_definition
1706   (cp_parser *);
1707 static void cp_parser_linkage_specification
1708   (cp_parser *);
1709 static void cp_parser_static_assert
1710   (cp_parser *, bool);
1711 static tree cp_parser_decltype
1712   (cp_parser *);
1713
1714 /* Declarators [gram.dcl.decl] */
1715
1716 static tree cp_parser_init_declarator
1717   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1718 static cp_declarator *cp_parser_declarator
1719   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1720 static cp_declarator *cp_parser_direct_declarator
1721   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1722 static enum tree_code cp_parser_ptr_operator
1723   (cp_parser *, tree *, cp_cv_quals *);
1724 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1725   (cp_parser *);
1726 static tree cp_parser_declarator_id
1727   (cp_parser *, bool);
1728 static tree cp_parser_type_id
1729   (cp_parser *);
1730 static void cp_parser_type_specifier_seq
1731   (cp_parser *, bool, cp_decl_specifier_seq *);
1732 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1733   (cp_parser *);
1734 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1735   (cp_parser *, bool *);
1736 static cp_parameter_declarator *cp_parser_parameter_declaration
1737   (cp_parser *, bool, bool *);
1738 static tree cp_parser_default_argument 
1739   (cp_parser *, bool);
1740 static void cp_parser_function_body
1741   (cp_parser *);
1742 static tree cp_parser_initializer
1743   (cp_parser *, bool *, bool *);
1744 static tree cp_parser_initializer_clause
1745   (cp_parser *, bool *);
1746 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1747   (cp_parser *, bool *);
1748
1749 static bool cp_parser_ctor_initializer_opt_and_function_body
1750   (cp_parser *);
1751
1752 /* Classes [gram.class] */
1753
1754 static tree cp_parser_class_name
1755   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1756 static tree cp_parser_class_specifier
1757   (cp_parser *);
1758 static tree cp_parser_class_head
1759   (cp_parser *, bool *, tree *, tree *);
1760 static enum tag_types cp_parser_class_key
1761   (cp_parser *);
1762 static void cp_parser_member_specification_opt
1763   (cp_parser *);
1764 static void cp_parser_member_declaration
1765   (cp_parser *);
1766 static tree cp_parser_pure_specifier
1767   (cp_parser *);
1768 static tree cp_parser_constant_initializer
1769   (cp_parser *);
1770
1771 /* Derived classes [gram.class.derived] */
1772
1773 static tree cp_parser_base_clause
1774   (cp_parser *);
1775 static tree cp_parser_base_specifier
1776   (cp_parser *);
1777
1778 /* Special member functions [gram.special] */
1779
1780 static tree cp_parser_conversion_function_id
1781   (cp_parser *);
1782 static tree cp_parser_conversion_type_id
1783   (cp_parser *);
1784 static cp_declarator *cp_parser_conversion_declarator_opt
1785   (cp_parser *);
1786 static bool cp_parser_ctor_initializer_opt
1787   (cp_parser *);
1788 static void cp_parser_mem_initializer_list
1789   (cp_parser *);
1790 static tree cp_parser_mem_initializer
1791   (cp_parser *);
1792 static tree cp_parser_mem_initializer_id
1793   (cp_parser *);
1794
1795 /* Overloading [gram.over] */
1796
1797 static tree cp_parser_operator_function_id
1798   (cp_parser *);
1799 static tree cp_parser_operator
1800   (cp_parser *);
1801
1802 /* Templates [gram.temp] */
1803
1804 static void cp_parser_template_declaration
1805   (cp_parser *, bool);
1806 static tree cp_parser_template_parameter_list
1807   (cp_parser *);
1808 static tree cp_parser_template_parameter
1809   (cp_parser *, bool *, bool *);
1810 static tree cp_parser_type_parameter
1811   (cp_parser *, bool *);
1812 static tree cp_parser_template_id
1813   (cp_parser *, bool, bool, bool);
1814 static tree cp_parser_template_name
1815   (cp_parser *, bool, bool, bool, bool *);
1816 static tree cp_parser_template_argument_list
1817   (cp_parser *);
1818 static tree cp_parser_template_argument
1819   (cp_parser *);
1820 static void cp_parser_explicit_instantiation
1821   (cp_parser *);
1822 static void cp_parser_explicit_specialization
1823   (cp_parser *);
1824
1825 /* Exception handling [gram.exception] */
1826
1827 static tree cp_parser_try_block
1828   (cp_parser *);
1829 static bool cp_parser_function_try_block
1830   (cp_parser *);
1831 static void cp_parser_handler_seq
1832   (cp_parser *);
1833 static void cp_parser_handler
1834   (cp_parser *);
1835 static tree cp_parser_exception_declaration
1836   (cp_parser *);
1837 static tree cp_parser_throw_expression
1838   (cp_parser *);
1839 static tree cp_parser_exception_specification_opt
1840   (cp_parser *);
1841 static tree cp_parser_type_id_list
1842   (cp_parser *);
1843
1844 /* GNU Extensions */
1845
1846 static tree cp_parser_asm_specification_opt
1847   (cp_parser *);
1848 static tree cp_parser_asm_operand_list
1849   (cp_parser *);
1850 static tree cp_parser_asm_clobber_list
1851   (cp_parser *);
1852 static tree cp_parser_attributes_opt
1853   (cp_parser *);
1854 static tree cp_parser_attribute_list
1855   (cp_parser *);
1856 static bool cp_parser_extension_opt
1857   (cp_parser *, int *);
1858 static void cp_parser_label_declaration
1859   (cp_parser *);
1860
1861 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1862 static bool cp_parser_pragma
1863   (cp_parser *, enum pragma_context);
1864
1865 /* Objective-C++ Productions */
1866
1867 static tree cp_parser_objc_message_receiver
1868   (cp_parser *);
1869 static tree cp_parser_objc_message_args
1870   (cp_parser *);
1871 static tree cp_parser_objc_message_expression
1872   (cp_parser *);
1873 static tree cp_parser_objc_encode_expression
1874   (cp_parser *);
1875 static tree cp_parser_objc_defs_expression
1876   (cp_parser *);
1877 static tree cp_parser_objc_protocol_expression
1878   (cp_parser *);
1879 static tree cp_parser_objc_selector_expression
1880   (cp_parser *);
1881 static tree cp_parser_objc_expression
1882   (cp_parser *);
1883 static bool cp_parser_objc_selector_p
1884   (enum cpp_ttype);
1885 static tree cp_parser_objc_selector
1886   (cp_parser *);
1887 static tree cp_parser_objc_protocol_refs_opt
1888   (cp_parser *);
1889 static void cp_parser_objc_declaration
1890   (cp_parser *);
1891 static tree cp_parser_objc_statement
1892   (cp_parser *);
1893
1894 /* Utility Routines */
1895
1896 static tree cp_parser_lookup_name
1897   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1898 static tree cp_parser_lookup_name_simple
1899   (cp_parser *, tree);
1900 static tree cp_parser_maybe_treat_template_as_class
1901   (tree, bool);
1902 static bool cp_parser_check_declarator_template_parameters
1903   (cp_parser *, cp_declarator *);
1904 static bool cp_parser_check_template_parameters
1905   (cp_parser *, unsigned);
1906 static tree cp_parser_simple_cast_expression
1907   (cp_parser *);
1908 static tree cp_parser_global_scope_opt
1909   (cp_parser *, bool);
1910 static bool cp_parser_constructor_declarator_p
1911   (cp_parser *, bool);
1912 static tree cp_parser_function_definition_from_specifiers_and_declarator
1913   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1914 static tree cp_parser_function_definition_after_declarator
1915   (cp_parser *, bool);
1916 static void cp_parser_template_declaration_after_export
1917   (cp_parser *, bool);
1918 static void cp_parser_perform_template_parameter_access_checks
1919   (VEC (deferred_access_check,gc)*);
1920 static tree cp_parser_single_declaration
1921   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1922 static tree cp_parser_functional_cast
1923   (cp_parser *, tree);
1924 static tree cp_parser_save_member_function_body
1925   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1926 static tree cp_parser_enclosed_template_argument_list
1927   (cp_parser *);
1928 static void cp_parser_save_default_args
1929   (cp_parser *, tree);
1930 static void cp_parser_late_parsing_for_member
1931   (cp_parser *, tree);
1932 static void cp_parser_late_parsing_default_args
1933   (cp_parser *, tree);
1934 static tree cp_parser_sizeof_operand
1935   (cp_parser *, enum rid);
1936 static tree cp_parser_trait_expr
1937   (cp_parser *, enum rid);
1938 static bool cp_parser_declares_only_class_p
1939   (cp_parser *);
1940 static void cp_parser_set_storage_class
1941   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1942 static void cp_parser_set_decl_spec_type
1943   (cp_decl_specifier_seq *, tree, bool);
1944 static bool cp_parser_friend_p
1945   (const cp_decl_specifier_seq *);
1946 static cp_token *cp_parser_require
1947   (cp_parser *, enum cpp_ttype, const char *);
1948 static cp_token *cp_parser_require_keyword
1949   (cp_parser *, enum rid, const char *);
1950 static bool cp_parser_token_starts_function_definition_p
1951   (cp_token *);
1952 static bool cp_parser_next_token_starts_class_definition_p
1953   (cp_parser *);
1954 static bool cp_parser_next_token_ends_template_argument_p
1955   (cp_parser *);
1956 static bool cp_parser_nth_token_starts_template_argument_list_p
1957   (cp_parser *, size_t);
1958 static enum tag_types cp_parser_token_is_class_key
1959   (cp_token *);
1960 static void cp_parser_check_class_key
1961   (enum tag_types, tree type);
1962 static void cp_parser_check_access_in_redeclaration
1963   (tree type);
1964 static bool cp_parser_optional_template_keyword
1965   (cp_parser *);
1966 static void cp_parser_pre_parsed_nested_name_specifier
1967   (cp_parser *);
1968 static void cp_parser_cache_group
1969   (cp_parser *, enum cpp_ttype, unsigned);
1970 static void cp_parser_parse_tentatively
1971   (cp_parser *);
1972 static void cp_parser_commit_to_tentative_parse
1973   (cp_parser *);
1974 static void cp_parser_abort_tentative_parse
1975   (cp_parser *);
1976 static bool cp_parser_parse_definitely
1977   (cp_parser *);
1978 static inline bool cp_parser_parsing_tentatively
1979   (cp_parser *);
1980 static bool cp_parser_uncommitted_to_tentative_parse_p
1981   (cp_parser *);
1982 static void cp_parser_error
1983   (cp_parser *, const char *);
1984 static void cp_parser_name_lookup_error
1985   (cp_parser *, tree, tree, const char *);
1986 static bool cp_parser_simulate_error
1987   (cp_parser *);
1988 static bool cp_parser_check_type_definition
1989   (cp_parser *);
1990 static void cp_parser_check_for_definition_in_return_type
1991   (cp_declarator *, tree);
1992 static void cp_parser_check_for_invalid_template_id
1993   (cp_parser *, tree);
1994 static bool cp_parser_non_integral_constant_expression
1995   (cp_parser *, const char *);
1996 static void cp_parser_diagnose_invalid_type_name
1997   (cp_parser *, tree, tree);
1998 static bool cp_parser_parse_and_diagnose_invalid_type_name
1999   (cp_parser *);
2000 static int cp_parser_skip_to_closing_parenthesis
2001   (cp_parser *, bool, bool, bool);
2002 static void cp_parser_skip_to_end_of_statement
2003   (cp_parser *);
2004 static void cp_parser_consume_semicolon_at_end_of_statement
2005   (cp_parser *);
2006 static void cp_parser_skip_to_end_of_block_or_statement
2007   (cp_parser *);
2008 static bool cp_parser_skip_to_closing_brace
2009   (cp_parser *);
2010 static void cp_parser_skip_to_end_of_template_parameter_list
2011   (cp_parser *);
2012 static void cp_parser_skip_to_pragma_eol
2013   (cp_parser*, cp_token *);
2014 static bool cp_parser_error_occurred
2015   (cp_parser *);
2016 static bool cp_parser_allow_gnu_extensions_p
2017   (cp_parser *);
2018 static bool cp_parser_is_string_literal
2019   (cp_token *);
2020 static bool cp_parser_is_keyword
2021   (cp_token *, enum rid);
2022 static tree cp_parser_make_typename_type
2023   (cp_parser *, tree, tree);
2024 static cp_declarator * cp_parser_make_indirect_declarator
2025   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2026
2027 /* Returns nonzero if we are parsing tentatively.  */
2028
2029 static inline bool
2030 cp_parser_parsing_tentatively (cp_parser* parser)
2031 {
2032   return parser->context->next != NULL;
2033 }
2034
2035 /* Returns nonzero if TOKEN is a string literal.  */
2036
2037 static bool
2038 cp_parser_is_string_literal (cp_token* token)
2039 {
2040   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2041 }
2042
2043 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2044
2045 static bool
2046 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2047 {
2048   return token->keyword == keyword;
2049 }
2050
2051 /* If not parsing tentatively, issue a diagnostic of the form
2052       FILE:LINE: MESSAGE before TOKEN
2053    where TOKEN is the next token in the input stream.  MESSAGE
2054    (specified by the caller) is usually of the form "expected
2055    OTHER-TOKEN".  */
2056
2057 static void
2058 cp_parser_error (cp_parser* parser, const char* message)
2059 {
2060   if (!cp_parser_simulate_error (parser))
2061     {
2062       cp_token *token = cp_lexer_peek_token (parser->lexer);
2063       /* This diagnostic makes more sense if it is tagged to the line
2064          of the token we just peeked at.  */
2065       cp_lexer_set_source_position_from_token (token);
2066
2067       if (token->type == CPP_PRAGMA)
2068         {
2069           error ("%<#pragma%> is not allowed here");
2070           cp_parser_skip_to_pragma_eol (parser, token);
2071           return;
2072         }
2073
2074       c_parse_error (message,
2075                      /* Because c_parser_error does not understand
2076                         CPP_KEYWORD, keywords are treated like
2077                         identifiers.  */
2078                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2079                      token->u.value);
2080     }
2081 }
2082
2083 /* Issue an error about name-lookup failing.  NAME is the
2084    IDENTIFIER_NODE DECL is the result of
2085    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2086    the thing that we hoped to find.  */
2087
2088 static void
2089 cp_parser_name_lookup_error (cp_parser* parser,
2090                              tree name,
2091                              tree decl,
2092                              const char* desired)
2093 {
2094   /* If name lookup completely failed, tell the user that NAME was not
2095      declared.  */
2096   if (decl == error_mark_node)
2097     {
2098       if (parser->scope && parser->scope != global_namespace)
2099         error ("%<%E::%E%> has not been declared",
2100                parser->scope, name);
2101       else if (parser->scope == global_namespace)
2102         error ("%<::%E%> has not been declared", name);
2103       else if (parser->object_scope
2104                && !CLASS_TYPE_P (parser->object_scope))
2105         error ("request for member %qE in non-class type %qT",
2106                name, parser->object_scope);
2107       else if (parser->object_scope)
2108         error ("%<%T::%E%> has not been declared",
2109                parser->object_scope, name);
2110       else
2111         error ("%qE has not been declared", name);
2112     }
2113   else if (parser->scope && parser->scope != global_namespace)
2114     error ("%<%E::%E%> %s", parser->scope, name, desired);
2115   else if (parser->scope == global_namespace)
2116     error ("%<::%E%> %s", name, desired);
2117   else
2118     error ("%qE %s", name, desired);
2119 }
2120
2121 /* If we are parsing tentatively, remember that an error has occurred
2122    during this tentative parse.  Returns true if the error was
2123    simulated; false if a message should be issued by the caller.  */
2124
2125 static bool
2126 cp_parser_simulate_error (cp_parser* parser)
2127 {
2128   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2129     {
2130       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2131       return true;
2132     }
2133   return false;
2134 }
2135
2136 /* Check for repeated decl-specifiers.  */
2137
2138 static void
2139 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2140 {
2141   cp_decl_spec ds;
2142
2143   for (ds = ds_first; ds != ds_last; ++ds)
2144     {
2145       unsigned count = decl_specs->specs[(int)ds];
2146       if (count < 2)
2147         continue;
2148       /* The "long" specifier is a special case because of "long long".  */
2149       if (ds == ds_long)
2150         {
2151           if (count > 2)
2152             error ("%<long long long%> is too long for GCC");
2153           else if (pedantic && !in_system_header && warn_long_long
2154                    && cxx_dialect == cxx98)
2155             pedwarn ("ISO C++ 1998 does not support %<long long%>");
2156         }
2157       else if (count > 1)
2158         {
2159           static const char *const decl_spec_names[] = {
2160             "signed",
2161             "unsigned",
2162             "short",
2163             "long",
2164             "const",
2165             "volatile",
2166             "restrict",
2167             "inline",
2168             "virtual",
2169             "explicit",
2170             "friend",
2171             "typedef",
2172             "__complex",
2173             "__thread"
2174           };
2175           error ("duplicate %qs", decl_spec_names[(int)ds]);
2176         }
2177     }
2178 }
2179
2180 /* This function is called when a type is defined.  If type
2181    definitions are forbidden at this point, an error message is
2182    issued.  */
2183
2184 static bool
2185 cp_parser_check_type_definition (cp_parser* parser)
2186 {
2187   /* If types are forbidden here, issue a message.  */
2188   if (parser->type_definition_forbidden_message)
2189     {
2190       /* Use `%s' to print the string in case there are any escape
2191          characters in the message.  */
2192       error ("%s", parser->type_definition_forbidden_message);
2193       return false;
2194     }
2195   return true;
2196 }
2197
2198 /* This function is called when the DECLARATOR is processed.  The TYPE
2199    was a type defined in the decl-specifiers.  If it is invalid to
2200    define a type in the decl-specifiers for DECLARATOR, an error is
2201    issued.  */
2202
2203 static void
2204 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2205                                                tree type)
2206 {
2207   /* [dcl.fct] forbids type definitions in return types.
2208      Unfortunately, it's not easy to know whether or not we are
2209      processing a return type until after the fact.  */
2210   while (declarator
2211          && (declarator->kind == cdk_pointer
2212              || declarator->kind == cdk_reference
2213              || declarator->kind == cdk_ptrmem))
2214     declarator = declarator->declarator;
2215   if (declarator
2216       && declarator->kind == cdk_function)
2217     {
2218       error ("new types may not be defined in a return type");
2219       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2220               type);
2221     }
2222 }
2223
2224 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2225    "<" in any valid C++ program.  If the next token is indeed "<",
2226    issue a message warning the user about what appears to be an
2227    invalid attempt to form a template-id.  */
2228
2229 static void
2230 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2231                                          tree type)
2232 {
2233   cp_token_position start = 0;
2234
2235   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2236     {
2237       if (TYPE_P (type))
2238         error ("%qT is not a template", type);
2239       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2240         error ("%qE is not a template", type);
2241       else
2242         error ("invalid template-id");
2243       /* Remember the location of the invalid "<".  */
2244       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2245         start = cp_lexer_token_position (parser->lexer, true);
2246       /* Consume the "<".  */
2247       cp_lexer_consume_token (parser->lexer);
2248       /* Parse the template arguments.  */
2249       cp_parser_enclosed_template_argument_list (parser);
2250       /* Permanently remove the invalid template arguments so that
2251          this error message is not issued again.  */
2252       if (start)
2253         cp_lexer_purge_tokens_after (parser->lexer, start);
2254     }
2255 }
2256
2257 /* If parsing an integral constant-expression, issue an error message
2258    about the fact that THING appeared and return true.  Otherwise,
2259    return false.  In either case, set
2260    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2261
2262 static bool
2263 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2264                                             const char *thing)
2265 {
2266   parser->non_integral_constant_expression_p = true;
2267   if (parser->integral_constant_expression_p)
2268     {
2269       if (!parser->allow_non_integral_constant_expression_p)
2270         {
2271           error ("%s cannot appear in a constant-expression", thing);
2272           return true;
2273         }
2274     }
2275   return false;
2276 }
2277
2278 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2279    qualifying scope (or NULL, if none) for ID.  This function commits
2280    to the current active tentative parse, if any.  (Otherwise, the
2281    problematic construct might be encountered again later, resulting
2282    in duplicate error messages.)  */
2283
2284 static void
2285 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2286 {
2287   tree decl, old_scope;
2288   /* Try to lookup the identifier.  */
2289   old_scope = parser->scope;
2290   parser->scope = scope;
2291   decl = cp_parser_lookup_name_simple (parser, id);
2292   parser->scope = old_scope;
2293   /* If the lookup found a template-name, it means that the user forgot
2294   to specify an argument list. Emit a useful error message.  */
2295   if (TREE_CODE (decl) == TEMPLATE_DECL)
2296     error ("invalid use of template-name %qE without an argument list", decl);
2297   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2298     error ("invalid use of destructor %qD as a type", id);
2299   else if (TREE_CODE (decl) == TYPE_DECL)
2300     /* Something like 'unsigned A a;'  */
2301     error ("invalid combination of multiple type-specifiers");
2302   else if (!parser->scope)
2303     {
2304       /* Issue an error message.  */
2305       error ("%qE does not name a type", id);
2306       /* If we're in a template class, it's possible that the user was
2307          referring to a type from a base class.  For example:
2308
2309            template <typename T> struct A { typedef T X; };
2310            template <typename T> struct B : public A<T> { X x; };
2311
2312          The user should have said "typename A<T>::X".  */
2313       if (processing_template_decl && current_class_type
2314           && TYPE_BINFO (current_class_type))
2315         {
2316           tree b;
2317
2318           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2319                b;
2320                b = TREE_CHAIN (b))
2321             {
2322               tree base_type = BINFO_TYPE (b);
2323               if (CLASS_TYPE_P (base_type)
2324                   && dependent_type_p (base_type))
2325                 {
2326                   tree field;
2327                   /* Go from a particular instantiation of the
2328                      template (which will have an empty TYPE_FIELDs),
2329                      to the main version.  */
2330                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2331                   for (field = TYPE_FIELDS (base_type);
2332                        field;
2333                        field = TREE_CHAIN (field))
2334                     if (TREE_CODE (field) == TYPE_DECL
2335                         && DECL_NAME (field) == id)
2336                       {
2337                         inform ("(perhaps %<typename %T::%E%> was intended)",
2338                                 BINFO_TYPE (b), id);
2339                         break;
2340                       }
2341                   if (field)
2342                     break;
2343                 }
2344             }
2345         }
2346     }
2347   /* Here we diagnose qualified-ids where the scope is actually correct,
2348      but the identifier does not resolve to a valid type name.  */
2349   else if (parser->scope != error_mark_node)
2350     {
2351       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2352         error ("%qE in namespace %qE does not name a type",
2353                id, parser->scope);
2354       else if (TYPE_P (parser->scope))
2355         error ("%qE in class %qT does not name a type", id, parser->scope);
2356       else
2357         gcc_unreachable ();
2358     }
2359   cp_parser_commit_to_tentative_parse (parser);
2360 }
2361
2362 /* Check for a common situation where a type-name should be present,
2363    but is not, and issue a sensible error message.  Returns true if an
2364    invalid type-name was detected.
2365
2366    The situation handled by this function are variable declarations of the
2367    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2368    Usually, `ID' should name a type, but if we got here it means that it
2369    does not. We try to emit the best possible error message depending on
2370    how exactly the id-expression looks like.  */
2371
2372 static bool
2373 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2374 {
2375   tree id;
2376
2377   cp_parser_parse_tentatively (parser);
2378   id = cp_parser_id_expression (parser,
2379                                 /*template_keyword_p=*/false,
2380                                 /*check_dependency_p=*/true,
2381                                 /*template_p=*/NULL,
2382                                 /*declarator_p=*/true,
2383                                 /*optional_p=*/false);
2384   /* After the id-expression, there should be a plain identifier,
2385      otherwise this is not a simple variable declaration. Also, if
2386      the scope is dependent, we cannot do much.  */
2387   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2388       || (parser->scope && TYPE_P (parser->scope)
2389           && dependent_type_p (parser->scope))
2390       || TREE_CODE (id) == TYPE_DECL)
2391     {
2392       cp_parser_abort_tentative_parse (parser);
2393       return false;
2394     }
2395   if (!cp_parser_parse_definitely (parser))
2396     return false;
2397
2398   /* Emit a diagnostic for the invalid type.  */
2399   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2400   /* Skip to the end of the declaration; there's no point in
2401      trying to process it.  */
2402   cp_parser_skip_to_end_of_block_or_statement (parser);
2403   return true;
2404 }
2405
2406 /* Consume tokens up to, and including, the next non-nested closing `)'.
2407    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2408    are doing error recovery. Returns -1 if OR_COMMA is true and we
2409    found an unnested comma.  */
2410
2411 static int
2412 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2413                                        bool recovering,
2414                                        bool or_comma,
2415                                        bool consume_paren)
2416 {
2417   unsigned paren_depth = 0;
2418   unsigned brace_depth = 0;
2419
2420   if (recovering && !or_comma
2421       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2422     return 0;
2423
2424   while (true)
2425     {
2426       cp_token * token = cp_lexer_peek_token (parser->lexer);
2427
2428       switch (token->type)
2429         {
2430         case CPP_EOF:
2431         case CPP_PRAGMA_EOL:
2432           /* If we've run out of tokens, then there is no closing `)'.  */
2433           return 0;
2434
2435         case CPP_SEMICOLON:
2436           /* This matches the processing in skip_to_end_of_statement.  */
2437           if (!brace_depth)
2438             return 0;
2439           break;
2440
2441         case CPP_OPEN_BRACE:
2442           ++brace_depth;
2443           break;
2444         case CPP_CLOSE_BRACE:
2445           if (!brace_depth--)
2446             return 0;
2447           break;
2448
2449         case CPP_COMMA:
2450           if (recovering && or_comma && !brace_depth && !paren_depth)
2451             return -1;
2452           break;
2453
2454         case CPP_OPEN_PAREN:
2455           if (!brace_depth)
2456             ++paren_depth;
2457           break;
2458
2459         case CPP_CLOSE_PAREN:
2460           if (!brace_depth && !paren_depth--)
2461             {
2462               if (consume_paren)
2463                 cp_lexer_consume_token (parser->lexer);
2464               return 1;
2465             }
2466           break;
2467
2468         default:
2469           break;
2470         }
2471
2472       /* Consume the token.  */
2473       cp_lexer_consume_token (parser->lexer);
2474     }
2475 }
2476
2477 /* Consume tokens until we reach the end of the current statement.
2478    Normally, that will be just before consuming a `;'.  However, if a
2479    non-nested `}' comes first, then we stop before consuming that.  */
2480
2481 static void
2482 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2483 {
2484   unsigned nesting_depth = 0;
2485
2486   while (true)
2487     {
2488       cp_token *token = cp_lexer_peek_token (parser->lexer);
2489
2490       switch (token->type)
2491         {
2492         case CPP_EOF:
2493         case CPP_PRAGMA_EOL:
2494           /* If we've run out of tokens, stop.  */
2495           return;
2496
2497         case CPP_SEMICOLON:
2498           /* If the next token is a `;', we have reached the end of the
2499              statement.  */
2500           if (!nesting_depth)
2501             return;
2502           break;
2503
2504         case CPP_CLOSE_BRACE:
2505           /* If this is a non-nested '}', stop before consuming it.
2506              That way, when confronted with something like:
2507
2508                { 3 + }
2509
2510              we stop before consuming the closing '}', even though we
2511              have not yet reached a `;'.  */
2512           if (nesting_depth == 0)
2513             return;
2514
2515           /* If it is the closing '}' for a block that we have
2516              scanned, stop -- but only after consuming the token.
2517              That way given:
2518
2519                 void f g () { ... }
2520                 typedef int I;
2521
2522              we will stop after the body of the erroneously declared
2523              function, but before consuming the following `typedef'
2524              declaration.  */
2525           if (--nesting_depth == 0)
2526             {
2527               cp_lexer_consume_token (parser->lexer);
2528               return;
2529             }
2530
2531         case CPP_OPEN_BRACE:
2532           ++nesting_depth;
2533           break;
2534
2535         default:
2536           break;
2537         }
2538
2539       /* Consume the token.  */
2540       cp_lexer_consume_token (parser->lexer);
2541     }
2542 }
2543
2544 /* This function is called at the end of a statement or declaration.
2545    If the next token is a semicolon, it is consumed; otherwise, error
2546    recovery is attempted.  */
2547
2548 static void
2549 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2550 {
2551   /* Look for the trailing `;'.  */
2552   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2553     {
2554       /* If there is additional (erroneous) input, skip to the end of
2555          the statement.  */
2556       cp_parser_skip_to_end_of_statement (parser);
2557       /* If the next token is now a `;', consume it.  */
2558       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2559         cp_lexer_consume_token (parser->lexer);
2560     }
2561 }
2562
2563 /* Skip tokens until we have consumed an entire block, or until we
2564    have consumed a non-nested `;'.  */
2565
2566 static void
2567 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2568 {
2569   int nesting_depth = 0;
2570
2571   while (nesting_depth >= 0)
2572     {
2573       cp_token *token = cp_lexer_peek_token (parser->lexer);
2574
2575       switch (token->type)
2576         {
2577         case CPP_EOF:
2578         case CPP_PRAGMA_EOL:
2579           /* If we've run out of tokens, stop.  */
2580           return;
2581
2582         case CPP_SEMICOLON:
2583           /* Stop if this is an unnested ';'. */
2584           if (!nesting_depth)
2585             nesting_depth = -1;
2586           break;
2587
2588         case CPP_CLOSE_BRACE:
2589           /* Stop if this is an unnested '}', or closes the outermost
2590              nesting level.  */
2591           nesting_depth--;
2592           if (!nesting_depth)
2593             nesting_depth = -1;
2594           break;
2595
2596         case CPP_OPEN_BRACE:
2597           /* Nest. */
2598           nesting_depth++;
2599           break;
2600
2601         default:
2602           break;
2603         }
2604
2605       /* Consume the token.  */
2606       cp_lexer_consume_token (parser->lexer);
2607     }
2608 }
2609
2610 /* Skip tokens until a non-nested closing curly brace is the next
2611    token, or there are no more tokens. Return true in the first case,
2612    false otherwise.  */
2613
2614 static bool
2615 cp_parser_skip_to_closing_brace (cp_parser *parser)
2616 {
2617   unsigned nesting_depth = 0;
2618
2619   while (true)
2620     {
2621       cp_token *token = cp_lexer_peek_token (parser->lexer);
2622
2623       switch (token->type)
2624         {
2625         case CPP_EOF:
2626         case CPP_PRAGMA_EOL:
2627           /* If we've run out of tokens, stop.  */
2628           return false;
2629
2630         case CPP_CLOSE_BRACE:
2631           /* If the next token is a non-nested `}', then we have reached
2632              the end of the current block.  */
2633           if (nesting_depth-- == 0)
2634             return true;
2635           break;
2636
2637         case CPP_OPEN_BRACE:
2638           /* If it the next token is a `{', then we are entering a new
2639              block.  Consume the entire block.  */
2640           ++nesting_depth;
2641           break;
2642
2643         default:
2644           break;
2645         }
2646
2647       /* Consume the token.  */
2648       cp_lexer_consume_token (parser->lexer);
2649     }
2650 }
2651
2652 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2653    parameter is the PRAGMA token, allowing us to purge the entire pragma
2654    sequence.  */
2655
2656 static void
2657 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2658 {
2659   cp_token *token;
2660
2661   parser->lexer->in_pragma = false;
2662
2663   do
2664     token = cp_lexer_consume_token (parser->lexer);
2665   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2666
2667   /* Ensure that the pragma is not parsed again.  */
2668   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2669 }
2670
2671 /* Require pragma end of line, resyncing with it as necessary.  The
2672    arguments are as for cp_parser_skip_to_pragma_eol.  */
2673
2674 static void
2675 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2676 {
2677   parser->lexer->in_pragma = false;
2678   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2679     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2680 }
2681
2682 /* This is a simple wrapper around make_typename_type. When the id is
2683    an unresolved identifier node, we can provide a superior diagnostic
2684    using cp_parser_diagnose_invalid_type_name.  */
2685
2686 static tree
2687 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2688 {
2689   tree result;
2690   if (TREE_CODE (id) == IDENTIFIER_NODE)
2691     {
2692       result = make_typename_type (scope, id, typename_type,
2693                                    /*complain=*/tf_none);
2694       if (result == error_mark_node)
2695         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2696       return result;
2697     }
2698   return make_typename_type (scope, id, typename_type, tf_error);
2699 }
2700
2701 /* This is a wrapper around the
2702    make_{pointer,ptrmem,reference}_declarator functions that decides
2703    which one to call based on the CODE and CLASS_TYPE arguments. The
2704    CODE argument should be one of the values returned by
2705    cp_parser_ptr_operator. */
2706 static cp_declarator *
2707 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2708                                     cp_cv_quals cv_qualifiers,
2709                                     cp_declarator *target)
2710 {
2711   if (code == ERROR_MARK)
2712     return cp_error_declarator;
2713
2714   if (code == INDIRECT_REF)
2715     if (class_type == NULL_TREE)
2716       return make_pointer_declarator (cv_qualifiers, target);
2717     else
2718       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2719   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2720     return make_reference_declarator (cv_qualifiers, target, false);
2721   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2722     return make_reference_declarator (cv_qualifiers, target, true);
2723   gcc_unreachable ();
2724 }
2725
2726 /* Create a new C++ parser.  */
2727
2728 static cp_parser *
2729 cp_parser_new (void)
2730 {
2731   cp_parser *parser;
2732   cp_lexer *lexer;
2733   unsigned i;
2734
2735   /* cp_lexer_new_main is called before calling ggc_alloc because
2736      cp_lexer_new_main might load a PCH file.  */
2737   lexer = cp_lexer_new_main ();
2738
2739   /* Initialize the binops_by_token so that we can get the tree
2740      directly from the token.  */
2741   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2742     binops_by_token[binops[i].token_type] = binops[i];
2743
2744   parser = GGC_CNEW (cp_parser);
2745   parser->lexer = lexer;
2746   parser->context = cp_parser_context_new (NULL);
2747
2748   /* For now, we always accept GNU extensions.  */
2749   parser->allow_gnu_extensions_p = 1;
2750
2751   /* The `>' token is a greater-than operator, not the end of a
2752      template-id.  */
2753   parser->greater_than_is_operator_p = true;
2754
2755   parser->default_arg_ok_p = true;
2756
2757   /* We are not parsing a constant-expression.  */
2758   parser->integral_constant_expression_p = false;
2759   parser->allow_non_integral_constant_expression_p = false;
2760   parser->non_integral_constant_expression_p = false;
2761
2762   /* Local variable names are not forbidden.  */
2763   parser->local_variables_forbidden_p = false;
2764
2765   /* We are not processing an `extern "C"' declaration.  */
2766   parser->in_unbraced_linkage_specification_p = false;
2767
2768   /* We are not processing a declarator.  */
2769   parser->in_declarator_p = false;
2770
2771   /* We are not processing a template-argument-list.  */
2772   parser->in_template_argument_list_p = false;
2773
2774   /* We are not in an iteration statement.  */
2775   parser->in_statement = 0;
2776
2777   /* We are not in a switch statement.  */
2778   parser->in_switch_statement_p = false;
2779
2780   /* We are not parsing a type-id inside an expression.  */
2781   parser->in_type_id_in_expr_p = false;
2782
2783   /* Declarations aren't implicitly extern "C".  */
2784   parser->implicit_extern_c = false;
2785
2786   /* String literals should be translated to the execution character set.  */
2787   parser->translate_strings_p = true;
2788
2789   /* We are not parsing a function body.  */
2790   parser->in_function_body = false;
2791
2792   /* The unparsed function queue is empty.  */
2793   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2794
2795   /* There are no classes being defined.  */
2796   parser->num_classes_being_defined = 0;
2797
2798   /* No template parameters apply.  */
2799   parser->num_template_parameter_lists = 0;
2800
2801   return parser;
2802 }
2803
2804 /* Create a cp_lexer structure which will emit the tokens in CACHE
2805    and push it onto the parser's lexer stack.  This is used for delayed
2806    parsing of in-class method bodies and default arguments, and should
2807    not be confused with tentative parsing.  */
2808 static void
2809 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2810 {
2811   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2812   lexer->next = parser->lexer;
2813   parser->lexer = lexer;
2814
2815   /* Move the current source position to that of the first token in the
2816      new lexer.  */
2817   cp_lexer_set_source_position_from_token (lexer->next_token);
2818 }
2819
2820 /* Pop the top lexer off the parser stack.  This is never used for the
2821    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2822 static void
2823 cp_parser_pop_lexer (cp_parser *parser)
2824 {
2825   cp_lexer *lexer = parser->lexer;
2826   parser->lexer = lexer->next;
2827   cp_lexer_destroy (lexer);
2828
2829   /* Put the current source position back where it was before this
2830      lexer was pushed.  */
2831   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2832 }
2833
2834 /* Lexical conventions [gram.lex]  */
2835
2836 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2837    identifier.  */
2838
2839 static tree
2840 cp_parser_identifier (cp_parser* parser)
2841 {
2842   cp_token *token;
2843
2844   /* Look for the identifier.  */
2845   token = cp_parser_require (parser, CPP_NAME, "identifier");
2846   /* Return the value.  */
2847   return token ? token->u.value : error_mark_node;
2848 }
2849
2850 /* Parse a sequence of adjacent string constants.  Returns a
2851    TREE_STRING representing the combined, nul-terminated string
2852    constant.  If TRANSLATE is true, translate the string to the
2853    execution character set.  If WIDE_OK is true, a wide string is
2854    invalid here.
2855
2856    C++98 [lex.string] says that if a narrow string literal token is
2857    adjacent to a wide string literal token, the behavior is undefined.
2858    However, C99 6.4.5p4 says that this results in a wide string literal.
2859    We follow C99 here, for consistency with the C front end.
2860
2861    This code is largely lifted from lex_string() in c-lex.c.
2862
2863    FUTURE: ObjC++ will need to handle @-strings here.  */
2864 static tree
2865 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2866 {
2867   tree value;
2868   bool wide = false;
2869   size_t count;
2870   struct obstack str_ob;
2871   cpp_string str, istr, *strs;
2872   cp_token *tok;
2873
2874   tok = cp_lexer_peek_token (parser->lexer);
2875   if (!cp_parser_is_string_literal (tok))
2876     {
2877       cp_parser_error (parser, "expected string-literal");
2878       return error_mark_node;
2879     }
2880
2881   /* Try to avoid the overhead of creating and destroying an obstack
2882      for the common case of just one string.  */
2883   if (!cp_parser_is_string_literal
2884       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2885     {
2886       cp_lexer_consume_token (parser->lexer);
2887
2888       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2889       str.len = TREE_STRING_LENGTH (tok->u.value);
2890       count = 1;
2891       if (tok->type == CPP_WSTRING)
2892         wide = true;
2893
2894       strs = &str;
2895     }
2896   else
2897     {
2898       gcc_obstack_init (&str_ob);
2899       count = 0;
2900
2901       do
2902         {
2903           cp_lexer_consume_token (parser->lexer);
2904           count++;
2905           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2906           str.len = TREE_STRING_LENGTH (tok->u.value);
2907           if (tok->type == CPP_WSTRING)
2908             wide = true;
2909
2910           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2911
2912           tok = cp_lexer_peek_token (parser->lexer);
2913         }
2914       while (cp_parser_is_string_literal (tok));
2915
2916       strs = (cpp_string *) obstack_finish (&str_ob);
2917     }
2918
2919   if (wide && !wide_ok)
2920     {
2921       cp_parser_error (parser, "a wide string is invalid in this context");
2922       wide = false;
2923     }
2924
2925   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2926       (parse_in, strs, count, &istr, wide))
2927     {
2928       value = build_string (istr.len, (const char *)istr.text);
2929       free (CONST_CAST (unsigned char *, istr.text));
2930
2931       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2932       value = fix_string_type (value);
2933     }
2934   else
2935     /* cpp_interpret_string has issued an error.  */
2936     value = error_mark_node;
2937
2938   if (count > 1)
2939     obstack_free (&str_ob, 0);
2940
2941   return value;
2942 }
2943
2944
2945 /* Basic concepts [gram.basic]  */
2946
2947 /* Parse a translation-unit.
2948
2949    translation-unit:
2950      declaration-seq [opt]
2951
2952    Returns TRUE if all went well.  */
2953
2954 static bool
2955 cp_parser_translation_unit (cp_parser* parser)
2956 {
2957   /* The address of the first non-permanent object on the declarator
2958      obstack.  */
2959   static void *declarator_obstack_base;
2960
2961   bool success;
2962
2963   /* Create the declarator obstack, if necessary.  */
2964   if (!cp_error_declarator)
2965     {
2966       gcc_obstack_init (&declarator_obstack);
2967       /* Create the error declarator.  */
2968       cp_error_declarator = make_declarator (cdk_error);
2969       /* Create the empty parameter list.  */
2970       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2971       /* Remember where the base of the declarator obstack lies.  */
2972       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2973     }
2974
2975   cp_parser_declaration_seq_opt (parser);
2976
2977   /* If there are no tokens left then all went well.  */
2978   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2979     {
2980       /* Get rid of the token array; we don't need it any more.  */
2981       cp_lexer_destroy (parser->lexer);
2982       parser->lexer = NULL;
2983
2984       /* This file might have been a context that's implicitly extern
2985          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2986       if (parser->implicit_extern_c)
2987         {
2988           pop_lang_context ();
2989           parser->implicit_extern_c = false;
2990         }
2991
2992       /* Finish up.  */
2993       finish_translation_unit ();
2994
2995       success = true;
2996     }
2997   else
2998     {
2999       cp_parser_error (parser, "expected declaration");
3000       success = false;
3001     }
3002
3003   /* Make sure the declarator obstack was fully cleaned up.  */
3004   gcc_assert (obstack_next_free (&declarator_obstack)
3005               == declarator_obstack_base);
3006
3007   /* All went well.  */
3008   return success;
3009 }
3010
3011 /* Expressions [gram.expr] */
3012
3013 /* Parse a primary-expression.
3014
3015    primary-expression:
3016      literal
3017      this
3018      ( expression )
3019      id-expression
3020
3021    GNU Extensions:
3022
3023    primary-expression:
3024      ( compound-statement )
3025      __builtin_va_arg ( assignment-expression , type-id )
3026      __builtin_offsetof ( type-id , offsetof-expression )
3027
3028    C++ Extensions:
3029      __has_nothrow_assign ( type-id )   
3030      __has_nothrow_constructor ( type-id )
3031      __has_nothrow_copy ( type-id )
3032      __has_trivial_assign ( type-id )   
3033      __has_trivial_constructor ( type-id )
3034      __has_trivial_copy ( type-id )
3035      __has_trivial_destructor ( type-id )
3036      __has_virtual_destructor ( type-id )     
3037      __is_abstract ( type-id )
3038      __is_base_of ( type-id , type-id )
3039      __is_class ( type-id )
3040      __is_convertible_to ( type-id , type-id )     
3041      __is_empty ( type-id )
3042      __is_enum ( type-id )
3043      __is_pod ( type-id )
3044      __is_polymorphic ( type-id )
3045      __is_union ( type-id )
3046
3047    Objective-C++ Extension:
3048
3049    primary-expression:
3050      objc-expression
3051
3052    literal:
3053      __null
3054
3055    ADDRESS_P is true iff this expression was immediately preceded by
3056    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3057    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3058    true iff this expression is a template argument.
3059
3060    Returns a representation of the expression.  Upon return, *IDK
3061    indicates what kind of id-expression (if any) was present.  */
3062
3063 static tree
3064 cp_parser_primary_expression (cp_parser *parser,
3065                               bool address_p,
3066                               bool cast_p,
3067                               bool template_arg_p,
3068                               cp_id_kind *idk)
3069 {
3070   cp_token *token;
3071
3072   /* Assume the primary expression is not an id-expression.  */
3073   *idk = CP_ID_KIND_NONE;
3074
3075   /* Peek at the next token.  */
3076   token = cp_lexer_peek_token (parser->lexer);
3077   switch (token->type)
3078     {
3079       /* literal:
3080            integer-literal
3081            character-literal
3082            floating-literal
3083            string-literal
3084            boolean-literal  */
3085     case CPP_CHAR:
3086     case CPP_WCHAR:
3087     case CPP_NUMBER:
3088       token = cp_lexer_consume_token (parser->lexer);
3089       /* Floating-point literals are only allowed in an integral
3090          constant expression if they are cast to an integral or
3091          enumeration type.  */
3092       if (TREE_CODE (token->u.value) == REAL_CST
3093           && parser->integral_constant_expression_p
3094           && pedantic)
3095         {
3096           /* CAST_P will be set even in invalid code like "int(2.7 +
3097              ...)".   Therefore, we have to check that the next token
3098              is sure to end the cast.  */
3099           if (cast_p)
3100             {
3101               cp_token *next_token;
3102
3103               next_token = cp_lexer_peek_token (parser->lexer);
3104               if (/* The comma at the end of an
3105                      enumerator-definition.  */
3106                   next_token->type != CPP_COMMA
3107                   /* The curly brace at the end of an enum-specifier.  */
3108                   && next_token->type != CPP_CLOSE_BRACE
3109                   /* The end of a statement.  */
3110                   && next_token->type != CPP_SEMICOLON
3111                   /* The end of the cast-expression.  */
3112                   && next_token->type != CPP_CLOSE_PAREN
3113                   /* The end of an array bound.  */
3114                   && next_token->type != CPP_CLOSE_SQUARE
3115                   /* The closing ">" in a template-argument-list.  */
3116                   && (next_token->type != CPP_GREATER
3117                       || parser->greater_than_is_operator_p)
3118                   /* C++0x only: A ">>" treated like two ">" tokens,
3119                      in a template-argument-list.  */
3120                   && (next_token->type != CPP_RSHIFT
3121                       || (cxx_dialect == cxx98)
3122                       || parser->greater_than_is_operator_p))
3123                 cast_p = false;
3124             }
3125
3126           /* If we are within a cast, then the constraint that the
3127              cast is to an integral or enumeration type will be
3128              checked at that point.  If we are not within a cast, then
3129              this code is invalid.  */
3130           if (!cast_p)
3131             cp_parser_non_integral_constant_expression
3132               (parser, "floating-point literal");
3133         }
3134       return token->u.value;
3135
3136     case CPP_STRING:
3137     case CPP_WSTRING:
3138       /* ??? Should wide strings be allowed when parser->translate_strings_p
3139          is false (i.e. in attributes)?  If not, we can kill the third
3140          argument to cp_parser_string_literal.  */
3141       return cp_parser_string_literal (parser,
3142                                        parser->translate_strings_p,
3143                                        true);
3144
3145     case CPP_OPEN_PAREN:
3146       {
3147         tree expr;
3148         bool saved_greater_than_is_operator_p;
3149
3150         /* Consume the `('.  */
3151         cp_lexer_consume_token (parser->lexer);
3152         /* Within a parenthesized expression, a `>' token is always
3153            the greater-than operator.  */
3154         saved_greater_than_is_operator_p
3155           = parser->greater_than_is_operator_p;
3156         parser->greater_than_is_operator_p = true;
3157         /* If we see `( { ' then we are looking at the beginning of
3158            a GNU statement-expression.  */
3159         if (cp_parser_allow_gnu_extensions_p (parser)
3160             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3161           {
3162             /* Statement-expressions are not allowed by the standard.  */
3163             if (pedantic)
3164               pedwarn ("ISO C++ forbids braced-groups within expressions");
3165
3166             /* And they're not allowed outside of a function-body; you
3167                cannot, for example, write:
3168
3169                  int i = ({ int j = 3; j + 1; });
3170
3171                at class or namespace scope.  */
3172             if (!parser->in_function_body
3173                 || parser->in_template_argument_list_p)
3174               {
3175                 error ("statement-expressions are not allowed outside "
3176                        "functions nor in template-argument lists");
3177                 cp_parser_skip_to_end_of_block_or_statement (parser);
3178                 expr = error_mark_node;
3179               }
3180             else
3181               {
3182                 /* Start the statement-expression.  */
3183                 expr = begin_stmt_expr ();
3184                 /* Parse the compound-statement.  */
3185                 cp_parser_compound_statement (parser, expr, false);
3186                 /* Finish up.  */
3187                 expr = finish_stmt_expr (expr, false);
3188               }
3189           }
3190         else
3191           {
3192             /* Parse the parenthesized expression.  */
3193             expr = cp_parser_expression (parser, cast_p);
3194             /* Let the front end know that this expression was
3195                enclosed in parentheses. This matters in case, for
3196                example, the expression is of the form `A::B', since
3197                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3198                not.  */
3199             finish_parenthesized_expr (expr);
3200           }
3201         /* The `>' token might be the end of a template-id or
3202            template-parameter-list now.  */
3203         parser->greater_than_is_operator_p
3204           = saved_greater_than_is_operator_p;
3205         /* Consume the `)'.  */
3206         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3207           cp_parser_skip_to_end_of_statement (parser);
3208
3209         return expr;
3210       }
3211
3212     case CPP_KEYWORD:
3213       switch (token->keyword)
3214         {
3215           /* These two are the boolean literals.  */
3216         case RID_TRUE:
3217           cp_lexer_consume_token (parser->lexer);
3218           return boolean_true_node;
3219         case RID_FALSE:
3220           cp_lexer_consume_token (parser->lexer);
3221           return boolean_false_node;
3222
3223           /* The `__null' literal.  */
3224         case RID_NULL:
3225           cp_lexer_consume_token (parser->lexer);
3226           return null_node;
3227
3228           /* Recognize the `this' keyword.  */
3229         case RID_THIS:
3230           cp_lexer_consume_token (parser->lexer);
3231           if (parser->local_variables_forbidden_p)
3232             {
3233               error ("%<this%> may not be used in this context");
3234               return error_mark_node;
3235             }
3236           /* Pointers cannot appear in constant-expressions.  */
3237           if (cp_parser_non_integral_constant_expression (parser,
3238                                                           "`this'"))
3239             return error_mark_node;
3240           return finish_this_expr ();
3241
3242           /* The `operator' keyword can be the beginning of an
3243              id-expression.  */
3244         case RID_OPERATOR:
3245           goto id_expression;
3246
3247         case RID_FUNCTION_NAME:
3248         case RID_PRETTY_FUNCTION_NAME:
3249         case RID_C99_FUNCTION_NAME:
3250           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3251              __func__ are the names of variables -- but they are
3252              treated specially.  Therefore, they are handled here,
3253              rather than relying on the generic id-expression logic
3254              below.  Grammatically, these names are id-expressions.
3255
3256              Consume the token.  */
3257           token = cp_lexer_consume_token (parser->lexer);
3258           /* Look up the name.  */
3259           return finish_fname (token->u.value);
3260
3261         case RID_VA_ARG:
3262           {
3263             tree expression;
3264             tree type;
3265
3266             /* The `__builtin_va_arg' construct is used to handle
3267                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3268             cp_lexer_consume_token (parser->lexer);
3269             /* Look for the opening `('.  */
3270             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3271             /* Now, parse the assignment-expression.  */
3272             expression = cp_parser_assignment_expression (parser,
3273                                                           /*cast_p=*/false);
3274             /* Look for the `,'.  */
3275             cp_parser_require (parser, CPP_COMMA, "`,'");
3276             /* Parse the type-id.  */
3277             type = cp_parser_type_id (parser);
3278             /* Look for the closing `)'.  */
3279             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3280             /* Using `va_arg' in a constant-expression is not
3281                allowed.  */
3282             if (cp_parser_non_integral_constant_expression (parser,
3283                                                             "`va_arg'"))
3284               return error_mark_node;
3285             return build_x_va_arg (expression, type);
3286           }
3287
3288         case RID_OFFSETOF:
3289           return cp_parser_builtin_offsetof (parser);
3290
3291         case RID_HAS_NOTHROW_ASSIGN:
3292         case RID_HAS_NOTHROW_CONSTRUCTOR:
3293         case RID_HAS_NOTHROW_COPY:        
3294         case RID_HAS_TRIVIAL_ASSIGN:
3295         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3296         case RID_HAS_TRIVIAL_COPY:        
3297         case RID_HAS_TRIVIAL_DESTRUCTOR:
3298         case RID_HAS_VIRTUAL_DESTRUCTOR:
3299         case RID_IS_ABSTRACT:
3300         case RID_IS_BASE_OF:
3301         case RID_IS_CLASS:
3302         case RID_IS_CONVERTIBLE_TO:
3303         case RID_IS_EMPTY:
3304         case RID_IS_ENUM:
3305         case RID_IS_POD:
3306         case RID_IS_POLYMORPHIC:
3307         case RID_IS_UNION:
3308           return cp_parser_trait_expr (parser, token->keyword);
3309
3310         /* Objective-C++ expressions.  */
3311         case RID_AT_ENCODE:
3312         case RID_AT_PROTOCOL:
3313         case RID_AT_SELECTOR:
3314           return cp_parser_objc_expression (parser);
3315
3316         default:
3317           cp_parser_error (parser, "expected primary-expression");
3318           return error_mark_node;
3319         }
3320
3321       /* An id-expression can start with either an identifier, a
3322          `::' as the beginning of a qualified-id, or the "operator"
3323          keyword.  */
3324     case CPP_NAME:
3325     case CPP_SCOPE:
3326     case CPP_TEMPLATE_ID:
3327     case CPP_NESTED_NAME_SPECIFIER:
3328       {
3329         tree id_expression;
3330         tree decl;
3331         const char *error_msg;
3332         bool template_p;
3333         bool done;
3334
3335       id_expression:
3336         /* Parse the id-expression.  */
3337         id_expression
3338           = cp_parser_id_expression (parser,
3339                                      /*template_keyword_p=*/false,
3340                                      /*check_dependency_p=*/true,
3341                                      &template_p,
3342                                      /*declarator_p=*/false,
3343                                      /*optional_p=*/false);
3344         if (id_expression == error_mark_node)
3345           return error_mark_node;
3346         token = cp_lexer_peek_token (parser->lexer);
3347         done = (token->type != CPP_OPEN_SQUARE
3348                 && token->type != CPP_OPEN_PAREN
3349                 && token->type != CPP_DOT
3350                 && token->type != CPP_DEREF
3351                 && token->type != CPP_PLUS_PLUS
3352                 && token->type != CPP_MINUS_MINUS);
3353         /* If we have a template-id, then no further lookup is
3354            required.  If the template-id was for a template-class, we
3355            will sometimes have a TYPE_DECL at this point.  */
3356         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3357                  || TREE_CODE (id_expression) == TYPE_DECL)
3358           decl = id_expression;
3359         /* Look up the name.  */
3360         else
3361           {
3362             tree ambiguous_decls;
3363
3364             decl = cp_parser_lookup_name (parser, id_expression,
3365                                           none_type,
3366                                           template_p,
3367                                           /*is_namespace=*/false,
3368                                           /*check_dependency=*/true,
3369                                           &ambiguous_decls);
3370             /* If the lookup was ambiguous, an error will already have
3371                been issued.  */
3372             if (ambiguous_decls)
3373               return error_mark_node;
3374
3375             /* In Objective-C++, an instance variable (ivar) may be preferred
3376                to whatever cp_parser_lookup_name() found.  */
3377             decl = objc_lookup_ivar (decl, id_expression);
3378
3379             /* If name lookup gives us a SCOPE_REF, then the
3380                qualifying scope was dependent.  */
3381             if (TREE_CODE (decl) == SCOPE_REF)
3382               {
3383                 /* At this point, we do not know if DECL is a valid
3384                    integral constant expression.  We assume that it is
3385                    in fact such an expression, so that code like:
3386
3387                       template <int N> struct A {
3388                         int a[B<N>::i];
3389                       };
3390                      
3391                    is accepted.  At template-instantiation time, we
3392                    will check that B<N>::i is actually a constant.  */
3393                 return decl;
3394               }
3395             /* Check to see if DECL is a local variable in a context
3396                where that is forbidden.  */
3397             if (parser->local_variables_forbidden_p
3398                 && local_variable_p (decl))
3399               {
3400                 /* It might be that we only found DECL because we are
3401                    trying to be generous with pre-ISO scoping rules.
3402                    For example, consider:
3403
3404                      int i;
3405                      void g() {
3406                        for (int i = 0; i < 10; ++i) {}
3407                        extern void f(int j = i);
3408                      }
3409
3410                    Here, name look up will originally find the out
3411                    of scope `i'.  We need to issue a warning message,
3412                    but then use the global `i'.  */
3413                 decl = check_for_out_of_scope_variable (decl);
3414                 if (local_variable_p (decl))
3415                   {
3416                     error ("local variable %qD may not appear in this context",
3417                            decl);
3418                     return error_mark_node;
3419                   }
3420               }
3421           }
3422
3423         decl = (finish_id_expression
3424                 (id_expression, decl, parser->scope,
3425                  idk,
3426                  parser->integral_constant_expression_p,
3427                  parser->allow_non_integral_constant_expression_p,
3428                  &parser->non_integral_constant_expression_p,
3429                  template_p, done, address_p,
3430                  template_arg_p,
3431                  &error_msg));
3432         if (error_msg)
3433           cp_parser_error (parser, error_msg);
3434         return decl;
3435       }
3436
3437       /* Anything else is an error.  */
3438     default:
3439       /* ...unless we have an Objective-C++ message or string literal,
3440          that is.  */
3441       if (c_dialect_objc ()
3442           && (token->type == CPP_OPEN_SQUARE
3443               || token->type == CPP_OBJC_STRING))
3444         return cp_parser_objc_expression (parser);
3445
3446       cp_parser_error (parser, "expected primary-expression");
3447       return error_mark_node;
3448     }
3449 }
3450
3451 /* Parse an id-expression.
3452
3453    id-expression:
3454      unqualified-id
3455      qualified-id
3456
3457    qualified-id:
3458      :: [opt] nested-name-specifier template [opt] unqualified-id
3459      :: identifier
3460      :: operator-function-id
3461      :: template-id
3462
3463    Return a representation of the unqualified portion of the
3464    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3465    a `::' or nested-name-specifier.
3466
3467    Often, if the id-expression was a qualified-id, the caller will
3468    want to make a SCOPE_REF to represent the qualified-id.  This
3469    function does not do this in order to avoid wastefully creating
3470    SCOPE_REFs when they are not required.
3471
3472    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3473    `template' keyword.
3474
3475    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3476    uninstantiated templates.
3477
3478    If *TEMPLATE_P is non-NULL, it is set to true iff the
3479    `template' keyword is used to explicitly indicate that the entity
3480    named is a template.
3481
3482    If DECLARATOR_P is true, the id-expression is appearing as part of
3483    a declarator, rather than as part of an expression.  */
3484
3485 static tree
3486 cp_parser_id_expression (cp_parser *parser,
3487                          bool template_keyword_p,
3488                          bool check_dependency_p,
3489                          bool *template_p,
3490                          bool declarator_p,
3491                          bool optional_p)
3492 {
3493   bool global_scope_p;
3494   bool nested_name_specifier_p;
3495
3496   /* Assume the `template' keyword was not used.  */
3497   if (template_p)
3498     *template_p = template_keyword_p;
3499
3500   /* Look for the optional `::' operator.  */
3501   global_scope_p
3502     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3503        != NULL_TREE);
3504   /* Look for the optional nested-name-specifier.  */
3505   nested_name_specifier_p
3506     = (cp_parser_nested_name_specifier_opt (parser,
3507                                             /*typename_keyword_p=*/false,
3508                                             check_dependency_p,
3509                                             /*type_p=*/false,
3510                                             declarator_p)
3511        != NULL_TREE);
3512   /* If there is a nested-name-specifier, then we are looking at
3513      the first qualified-id production.  */
3514   if (nested_name_specifier_p)
3515     {
3516       tree saved_scope;
3517       tree saved_object_scope;
3518       tree saved_qualifying_scope;
3519       tree unqualified_id;
3520       bool is_template;
3521
3522       /* See if the next token is the `template' keyword.  */
3523       if (!template_p)
3524         template_p = &is_template;
3525       *template_p = cp_parser_optional_template_keyword (parser);
3526       /* Name lookup we do during the processing of the
3527          unqualified-id might obliterate SCOPE.  */
3528       saved_scope = parser->scope;
3529       saved_object_scope = parser->object_scope;
3530       saved_qualifying_scope = parser->qualifying_scope;
3531       /* Process the final unqualified-id.  */
3532       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3533                                                  check_dependency_p,
3534                                                  declarator_p,
3535                                                  /*optional_p=*/false);
3536       /* Restore the SAVED_SCOPE for our caller.  */
3537       parser->scope = saved_scope;
3538       parser->object_scope = saved_object_scope;
3539       parser->qualifying_scope = saved_qualifying_scope;
3540
3541       return unqualified_id;
3542     }
3543   /* Otherwise, if we are in global scope, then we are looking at one
3544      of the other qualified-id productions.  */
3545   else if (global_scope_p)
3546     {
3547       cp_token *token;
3548       tree id;
3549
3550       /* Peek at the next token.  */
3551       token = cp_lexer_peek_token (parser->lexer);
3552
3553       /* If it's an identifier, and the next token is not a "<", then
3554          we can avoid the template-id case.  This is an optimization
3555          for this common case.  */
3556       if (token->type == CPP_NAME
3557           && !cp_parser_nth_token_starts_template_argument_list_p
3558                (parser, 2))
3559         return cp_parser_identifier (parser);
3560
3561       cp_parser_parse_tentatively (parser);
3562       /* Try a template-id.  */
3563       id = cp_parser_template_id (parser,
3564                                   /*template_keyword_p=*/false,
3565                                   /*check_dependency_p=*/true,
3566                                   declarator_p);
3567       /* If that worked, we're done.  */
3568       if (cp_parser_parse_definitely (parser))
3569         return id;
3570
3571       /* Peek at the next token.  (Changes in the token buffer may
3572          have invalidated the pointer obtained above.)  */
3573       token = cp_lexer_peek_token (parser->lexer);
3574
3575       switch (token->type)
3576         {
3577         case CPP_NAME:
3578           return cp_parser_identifier (parser);
3579
3580         case CPP_KEYWORD:
3581           if (token->keyword == RID_OPERATOR)
3582             return cp_parser_operator_function_id (parser);
3583           /* Fall through.  */
3584
3585         default:
3586           cp_parser_error (parser, "expected id-expression");
3587           return error_mark_node;
3588         }
3589     }
3590   else
3591     return cp_parser_unqualified_id (parser, template_keyword_p,
3592                                      /*check_dependency_p=*/true,
3593                                      declarator_p,
3594                                      optional_p);
3595 }
3596
3597 /* Parse an unqualified-id.
3598
3599    unqualified-id:
3600      identifier
3601      operator-function-id
3602      conversion-function-id
3603      ~ class-name
3604      template-id
3605
3606    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3607    keyword, in a construct like `A::template ...'.
3608
3609    Returns a representation of unqualified-id.  For the `identifier'
3610    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3611    production a BIT_NOT_EXPR is returned; the operand of the
3612    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3613    other productions, see the documentation accompanying the
3614    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3615    names are looked up in uninstantiated templates.  If DECLARATOR_P
3616    is true, the unqualified-id is appearing as part of a declarator,
3617    rather than as part of an expression.  */
3618
3619 static tree
3620 cp_parser_unqualified_id (cp_parser* parser,
3621                           bool template_keyword_p,
3622                           bool check_dependency_p,
3623                           bool declarator_p,
3624                           bool optional_p)
3625 {
3626   cp_token *token;
3627
3628   /* Peek at the next token.  */
3629   token = cp_lexer_peek_token (parser->lexer);
3630
3631   switch (token->type)
3632     {
3633     case CPP_NAME:
3634       {
3635         tree id;
3636
3637         /* We don't know yet whether or not this will be a
3638            template-id.  */
3639         cp_parser_parse_tentatively (parser);
3640         /* Try a template-id.  */
3641         id = cp_parser_template_id (parser, template_keyword_p,
3642                                     check_dependency_p,
3643                                     declarator_p);
3644         /* If it worked, we're done.  */
3645         if (cp_parser_parse_definitely (parser))
3646           return id;
3647         /* Otherwise, it's an ordinary identifier.  */
3648         return cp_parser_identifier (parser);
3649       }
3650
3651     case CPP_TEMPLATE_ID:
3652       return cp_parser_template_id (parser, template_keyword_p,
3653                                     check_dependency_p,
3654                                     declarator_p);
3655
3656     case CPP_COMPL:
3657       {
3658         tree type_decl;
3659         tree qualifying_scope;
3660         tree object_scope;
3661         tree scope;
3662         bool done;
3663
3664         /* Consume the `~' token.  */
3665         cp_lexer_consume_token (parser->lexer);
3666         /* Parse the class-name.  The standard, as written, seems to
3667            say that:
3668
3669              template <typename T> struct S { ~S (); };
3670              template <typename T> S<T>::~S() {}
3671
3672            is invalid, since `~' must be followed by a class-name, but
3673            `S<T>' is dependent, and so not known to be a class.
3674            That's not right; we need to look in uninstantiated
3675            templates.  A further complication arises from:
3676
3677              template <typename T> void f(T t) {
3678                t.T::~T();
3679              }
3680
3681            Here, it is not possible to look up `T' in the scope of `T'
3682            itself.  We must look in both the current scope, and the
3683            scope of the containing complete expression.
3684
3685            Yet another issue is:
3686
3687              struct S {
3688                int S;
3689                ~S();
3690              };
3691
3692              S::~S() {}
3693
3694            The standard does not seem to say that the `S' in `~S'
3695            should refer to the type `S' and not the data member
3696            `S::S'.  */
3697
3698         /* DR 244 says that we look up the name after the "~" in the
3699            same scope as we looked up the qualifying name.  That idea
3700            isn't fully worked out; it's more complicated than that.  */
3701         scope = parser->scope;
3702         object_scope = parser->object_scope;
3703         qualifying_scope = parser->qualifying_scope;
3704
3705         /* Check for invalid scopes.  */
3706         if (scope == error_mark_node)
3707           {
3708             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3709               cp_lexer_consume_token (parser->lexer);
3710             return error_mark_node;
3711           }
3712         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3713           {
3714             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3715               error ("scope %qT before %<~%> is not a class-name", scope);
3716             cp_parser_simulate_error (parser);
3717             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3718               cp_lexer_consume_token (parser->lexer);
3719             return error_mark_node;
3720           }
3721         gcc_assert (!scope || TYPE_P (scope));
3722
3723         /* If the name is of the form "X::~X" it's OK.  */
3724         token = cp_lexer_peek_token (parser->lexer);
3725         if (scope
3726             && token->type == CPP_NAME
3727             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3728                 == CPP_OPEN_PAREN)
3729             && constructor_name_p (token->u.value, scope))
3730           {
3731             cp_lexer_consume_token (parser->lexer);
3732             return build_nt (BIT_NOT_EXPR, scope);
3733           }
3734
3735         /* If there was an explicit qualification (S::~T), first look
3736            in the scope given by the qualification (i.e., S).  */
3737         done = false;
3738         type_decl = NULL_TREE;
3739         if (scope)
3740           {
3741             cp_parser_parse_tentatively (parser);
3742             type_decl = cp_parser_class_name (parser,
3743                                               /*typename_keyword_p=*/false,
3744                                               /*template_keyword_p=*/false,
3745                                               none_type,
3746                                               /*check_dependency=*/false,
3747                                               /*class_head_p=*/false,
3748                                               declarator_p);
3749             if (cp_parser_parse_definitely (parser))
3750               done = true;
3751           }
3752         /* In "N::S::~S", look in "N" as well.  */
3753         if (!done && scope && qualifying_scope)
3754           {
3755             cp_parser_parse_tentatively (parser);
3756             parser->scope = qualifying_scope;
3757             parser->object_scope = NULL_TREE;
3758             parser->qualifying_scope = NULL_TREE;
3759             type_decl
3760               = cp_parser_class_name (parser,
3761                                       /*typename_keyword_p=*/false,
3762                                       /*template_keyword_p=*/false,
3763                                       none_type,
3764                                       /*check_dependency=*/false,
3765                                       /*class_head_p=*/false,
3766                                       declarator_p);
3767             if (cp_parser_parse_definitely (parser))
3768               done = true;
3769           }
3770         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3771         else if (!done && object_scope)
3772           {
3773             cp_parser_parse_tentatively (parser);
3774             parser->scope = object_scope;
3775             parser->object_scope = NULL_TREE;
3776             parser->qualifying_scope = NULL_TREE;
3777             type_decl
3778               = cp_parser_class_name (parser,
3779                                       /*typename_keyword_p=*/false,
3780                                       /*template_keyword_p=*/false,
3781                                       none_type,
3782                                       /*check_dependency=*/false,
3783                                       /*class_head_p=*/false,
3784                                       declarator_p);
3785             if (cp_parser_parse_definitely (parser))
3786               done = true;
3787           }
3788         /* Look in the surrounding context.  */
3789         if (!done)
3790           {
3791             parser->scope = NULL_TREE;
3792             parser->object_scope = NULL_TREE;
3793             parser->qualifying_scope = NULL_TREE;
3794             type_decl
3795               = cp_parser_class_name (parser,
3796                                       /*typename_keyword_p=*/false,
3797                                       /*template_keyword_p=*/false,
3798                                       none_type,
3799                                       /*check_dependency=*/false,
3800                                       /*class_head_p=*/false,
3801                                       declarator_p);
3802           }
3803         /* If an error occurred, assume that the name of the
3804            destructor is the same as the name of the qualifying
3805            class.  That allows us to keep parsing after running
3806            into ill-formed destructor names.  */
3807         if (type_decl == error_mark_node && scope)
3808           return build_nt (BIT_NOT_EXPR, scope);
3809         else if (type_decl == error_mark_node)
3810           return error_mark_node;
3811
3812         /* Check that destructor name and scope match.  */
3813         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3814           {
3815             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3816               error ("declaration of %<~%T%> as member of %qT",
3817                      type_decl, scope);
3818             cp_parser_simulate_error (parser);
3819             return error_mark_node;
3820           }
3821
3822         /* [class.dtor]
3823
3824            A typedef-name that names a class shall not be used as the
3825            identifier in the declarator for a destructor declaration.  */
3826         if (declarator_p
3827             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3828             && !DECL_SELF_REFERENCE_P (type_decl)
3829             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3830           error ("typedef-name %qD used as destructor declarator",
3831                  type_decl);
3832
3833         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3834       }
3835
3836     case CPP_KEYWORD:
3837       if (token->keyword == RID_OPERATOR)
3838         {
3839           tree id;
3840
3841           /* This could be a template-id, so we try that first.  */
3842           cp_parser_parse_tentatively (parser);
3843           /* Try a template-id.  */
3844           id = cp_parser_template_id (parser, template_keyword_p,
3845                                       /*check_dependency_p=*/true,
3846                                       declarator_p);
3847           /* If that worked, we're done.  */
3848           if (cp_parser_parse_definitely (parser))
3849             return id;
3850           /* We still don't know whether we're looking at an
3851              operator-function-id or a conversion-function-id.  */
3852           cp_parser_parse_tentatively (parser);
3853           /* Try an operator-function-id.  */
3854           id = cp_parser_operator_function_id (parser);
3855           /* If that didn't work, try a conversion-function-id.  */
3856           if (!cp_parser_parse_definitely (parser))
3857             id = cp_parser_conversion_function_id (parser);
3858
3859           return id;
3860         }
3861       /* Fall through.  */
3862
3863     default:
3864       if (optional_p)
3865         return NULL_TREE;
3866       cp_parser_error (parser, "expected unqualified-id");
3867       return error_mark_node;
3868     }
3869 }
3870
3871 /* Parse an (optional) nested-name-specifier.
3872
3873    nested-name-specifier:
3874      class-or-namespace-name :: nested-name-specifier [opt]
3875      class-or-namespace-name :: template nested-name-specifier [opt]
3876
3877    PARSER->SCOPE should be set appropriately before this function is
3878    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3879    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3880    in name lookups.
3881
3882    Sets PARSER->SCOPE to the class (TYPE) or namespace
3883    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3884    it unchanged if there is no nested-name-specifier.  Returns the new
3885    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3886
3887    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3888    part of a declaration and/or decl-specifier.  */
3889
3890 static tree
3891 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3892                                      bool typename_keyword_p,
3893                                      bool check_dependency_p,
3894                                      bool type_p,
3895                                      bool is_declaration)
3896 {
3897   bool success = false;
3898   cp_token_position start = 0;
3899   cp_token *token;
3900
3901   /* Remember where the nested-name-specifier starts.  */
3902   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3903     {
3904       start = cp_lexer_token_position (parser->lexer, false);
3905       push_deferring_access_checks (dk_deferred);
3906     }
3907
3908   while (true)
3909     {
3910       tree new_scope;
3911       tree old_scope;
3912       tree saved_qualifying_scope;
3913       bool template_keyword_p;
3914
3915       /* Spot cases that cannot be the beginning of a
3916          nested-name-specifier.  */
3917       token = cp_lexer_peek_token (parser->lexer);
3918
3919       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3920          the already parsed nested-name-specifier.  */
3921       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3922         {
3923           /* Grab the nested-name-specifier and continue the loop.  */
3924           cp_parser_pre_parsed_nested_name_specifier (parser);
3925           /* If we originally encountered this nested-name-specifier
3926              with IS_DECLARATION set to false, we will not have
3927              resolved TYPENAME_TYPEs, so we must do so here.  */
3928           if (is_declaration
3929               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3930             {
3931               new_scope = resolve_typename_type (parser->scope,
3932                                                  /*only_current_p=*/false);
3933               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3934                 parser->scope = new_scope;
3935             }
3936           success = true;
3937           continue;
3938         }
3939
3940       /* Spot cases that cannot be the beginning of a
3941          nested-name-specifier.  On the second and subsequent times
3942          through the loop, we look for the `template' keyword.  */
3943       if (success && token->keyword == RID_TEMPLATE)
3944         ;
3945       /* A template-id can start a nested-name-specifier.  */
3946       else if (token->type == CPP_TEMPLATE_ID)
3947         ;
3948       else
3949         {
3950           /* If the next token is not an identifier, then it is
3951              definitely not a class-or-namespace-name.  */
3952           if (token->type != CPP_NAME)
3953             break;
3954           /* If the following token is neither a `<' (to begin a
3955              template-id), nor a `::', then we are not looking at a
3956              nested-name-specifier.  */
3957           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3958           if (token->type != CPP_SCOPE
3959               && !cp_parser_nth_token_starts_template_argument_list_p
3960                   (parser, 2))
3961             break;
3962         }
3963
3964       /* The nested-name-specifier is optional, so we parse
3965          tentatively.  */
3966       cp_parser_parse_tentatively (parser);
3967
3968       /* Look for the optional `template' keyword, if this isn't the
3969          first time through the loop.  */
3970       if (success)
3971         template_keyword_p = cp_parser_optional_template_keyword (parser);
3972       else
3973         template_keyword_p = false;
3974
3975       /* Save the old scope since the name lookup we are about to do
3976          might destroy it.  */
3977       old_scope = parser->scope;
3978       saved_qualifying_scope = parser->qualifying_scope;
3979       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3980          look up names in "X<T>::I" in order to determine that "Y" is
3981          a template.  So, if we have a typename at this point, we make
3982          an effort to look through it.  */
3983       if (is_declaration
3984           && !typename_keyword_p
3985           && parser->scope
3986           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3987         parser->scope = resolve_typename_type (parser->scope,
3988                                                /*only_current_p=*/false);
3989       /* Parse the qualifying entity.  */
3990       new_scope
3991         = cp_parser_class_or_namespace_name (parser,
3992                                              typename_keyword_p,
3993                                              template_keyword_p,
3994                                              check_dependency_p,
3995                                              type_p,
3996                                              is_declaration);
3997       /* Look for the `::' token.  */
3998       cp_parser_require (parser, CPP_SCOPE, "`::'");
3999
4000       /* If we found what we wanted, we keep going; otherwise, we're
4001          done.  */
4002       if (!cp_parser_parse_definitely (parser))
4003         {
4004           bool error_p = false;
4005
4006           /* Restore the OLD_SCOPE since it was valid before the
4007              failed attempt at finding the last
4008              class-or-namespace-name.  */
4009           parser->scope = old_scope;
4010           parser->qualifying_scope = saved_qualifying_scope;
4011           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4012             break;
4013           /* If the next token is an identifier, and the one after
4014              that is a `::', then any valid interpretation would have
4015              found a class-or-namespace-name.  */
4016           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4017                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4018                      == CPP_SCOPE)
4019                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4020                      != CPP_COMPL))
4021             {
4022               token = cp_lexer_consume_token (parser->lexer);
4023               if (!error_p)
4024                 {
4025                   if (!token->ambiguous_p)
4026                     {
4027                       tree decl;
4028                       tree ambiguous_decls;
4029
4030                       decl = cp_parser_lookup_name (parser, token->u.value,
4031                                                     none_type,
4032                                                     /*is_template=*/false,
4033                                                     /*is_namespace=*/false,
4034                                                     /*check_dependency=*/true,
4035                                                     &ambiguous_decls);
4036                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4037                         error ("%qD used without template parameters", decl);
4038                       else if (ambiguous_decls)
4039                         {
4040                           error ("reference to %qD is ambiguous",
4041                                  token->u.value);
4042                           print_candidates (ambiguous_decls);
4043                           decl = error_mark_node;
4044                         }
4045                       else
4046                         cp_parser_name_lookup_error
4047                           (parser, token->u.value, decl,
4048                            "is not a class or namespace");
4049                     }
4050                   parser->scope = error_mark_node;
4051                   error_p = true;
4052                   /* Treat this as a successful nested-name-specifier
4053                      due to:
4054
4055                      [basic.lookup.qual]
4056
4057                      If the name found is not a class-name (clause
4058                      _class_) or namespace-name (_namespace.def_), the
4059                      program is ill-formed.  */
4060                   success = true;
4061                 }
4062               cp_lexer_consume_token (parser->lexer);
4063             }
4064           break;
4065         }
4066       /* We've found one valid nested-name-specifier.  */
4067       success = true;
4068       /* Name lookup always gives us a DECL.  */
4069       if (TREE_CODE (new_scope) == TYPE_DECL)
4070         new_scope = TREE_TYPE (new_scope);
4071       /* Uses of "template" must be followed by actual templates.  */
4072       if (template_keyword_p
4073           && !(CLASS_TYPE_P (new_scope)
4074                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4075                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4076                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4077           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4078                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4079                    == TEMPLATE_ID_EXPR)))
4080         pedwarn (TYPE_P (new_scope)
4081                  ? "%qT is not a template"
4082                  : "%qD is not a template",
4083                  new_scope);
4084       /* If it is a class scope, try to complete it; we are about to
4085          be looking up names inside the class.  */
4086       if (TYPE_P (new_scope)
4087           /* Since checking types for dependency can be expensive,
4088              avoid doing it if the type is already complete.  */
4089           && !COMPLETE_TYPE_P (new_scope)
4090           /* Do not try to complete dependent types.  */
4091           && !dependent_type_p (new_scope))
4092         {
4093           new_scope = complete_type (new_scope);
4094           /* If it is a typedef to current class, use the current
4095              class instead, as the typedef won't have any names inside
4096              it yet.  */
4097           if (!COMPLETE_TYPE_P (new_scope)
4098               && currently_open_class (new_scope))
4099             new_scope = TYPE_MAIN_VARIANT (new_scope);
4100         }
4101       /* Make sure we look in the right scope the next time through
4102          the loop.  */
4103       parser->scope = new_scope;
4104     }
4105
4106   /* If parsing tentatively, replace the sequence of tokens that makes
4107      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4108      token.  That way, should we re-parse the token stream, we will
4109      not have to repeat the effort required to do the parse, nor will
4110      we issue duplicate error messages.  */
4111   if (success && start)
4112     {
4113       cp_token *token;
4114
4115       token = cp_lexer_token_at (parser->lexer, start);
4116       /* Reset the contents of the START token.  */
4117       token->type = CPP_NESTED_NAME_SPECIFIER;
4118       /* Retrieve any deferred checks.  Do not pop this access checks yet
4119          so the memory will not be reclaimed during token replacing below.  */
4120       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4121       token->u.tree_check_value->value = parser->scope;
4122       token->u.tree_check_value->checks = get_deferred_access_checks ();
4123       token->u.tree_check_value->qualifying_scope =
4124         parser->qualifying_scope;
4125       token->keyword = RID_MAX;
4126
4127       /* Purge all subsequent tokens.  */
4128       cp_lexer_purge_tokens_after (parser->lexer, start);
4129     }
4130
4131   if (start)
4132     pop_to_parent_deferring_access_checks ();
4133
4134   return success ? parser->scope : NULL_TREE;
4135 }
4136
4137 /* Parse a nested-name-specifier.  See
4138    cp_parser_nested_name_specifier_opt for details.  This function
4139    behaves identically, except that it will an issue an error if no
4140    nested-name-specifier is present.  */
4141
4142 static tree
4143 cp_parser_nested_name_specifier (cp_parser *parser,
4144                                  bool typename_keyword_p,
4145                                  bool check_dependency_p,
4146                                  bool type_p,
4147                                  bool is_declaration)
4148 {
4149   tree scope;
4150
4151   /* Look for the nested-name-specifier.  */
4152   scope = cp_parser_nested_name_specifier_opt (parser,
4153                                                typename_keyword_p,
4154                                                check_dependency_p,
4155                                                type_p,
4156                                                is_declaration);
4157   /* If it was not present, issue an error message.  */
4158   if (!scope)
4159     {
4160       cp_parser_error (parser, "expected nested-name-specifier");
4161       parser->scope = NULL_TREE;
4162     }
4163
4164   return scope;
4165 }
4166
4167 /* Parse a class-or-namespace-name.
4168
4169    class-or-namespace-name:
4170      class-name
4171      namespace-name
4172
4173    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4174    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4175    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4176    TYPE_P is TRUE iff the next name should be taken as a class-name,
4177    even the same name is declared to be another entity in the same
4178    scope.
4179
4180    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4181    specified by the class-or-namespace-name.  If neither is found the
4182    ERROR_MARK_NODE is returned.  */
4183
4184 static tree
4185 cp_parser_class_or_namespace_name (cp_parser *parser,
4186                                    bool typename_keyword_p,
4187                                    bool template_keyword_p,
4188                                    bool check_dependency_p,
4189                                    bool type_p,
4190                                    bool is_declaration)
4191 {
4192   tree saved_scope;
4193   tree saved_qualifying_scope;
4194   tree saved_object_scope;
4195   tree scope;
4196   bool only_class_p;
4197
4198   /* Before we try to parse the class-name, we must save away the
4199      current PARSER->SCOPE since cp_parser_class_name will destroy
4200      it.  */
4201   saved_scope = parser->scope;
4202   saved_qualifying_scope = parser->qualifying_scope;
4203   saved_object_scope = parser->object_scope;
4204   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4205      there is no need to look for a namespace-name.  */
4206   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4207   if (!only_class_p)
4208     cp_parser_parse_tentatively (parser);
4209   scope = cp_parser_class_name (parser,
4210                                 typename_keyword_p,
4211                                 template_keyword_p,
4212                                 type_p ? class_type : none_type,
4213                                 check_dependency_p,
4214                                 /*class_head_p=*/false,
4215                                 is_declaration);
4216   /* If that didn't work, try for a namespace-name.  */
4217   if (!only_class_p && !cp_parser_parse_definitely (parser))
4218     {
4219       /* Restore the saved scope.  */
4220       parser->scope = saved_scope;
4221       parser->qualifying_scope = saved_qualifying_scope;
4222       parser->object_scope = saved_object_scope;
4223       /* If we are not looking at an identifier followed by the scope
4224          resolution operator, then this is not part of a
4225          nested-name-specifier.  (Note that this function is only used
4226          to parse the components of a nested-name-specifier.)  */
4227       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4228           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4229         return error_mark_node;
4230       scope = cp_parser_namespace_name (parser);
4231     }
4232
4233   return scope;
4234 }
4235
4236 /* Parse a postfix-expression.
4237
4238    postfix-expression:
4239      primary-expression
4240      postfix-expression [ expression ]
4241      postfix-expression ( expression-list [opt] )
4242      simple-type-specifier ( expression-list [opt] )
4243      typename :: [opt] nested-name-specifier identifier
4244        ( expression-list [opt] )
4245      typename :: [opt] nested-name-specifier template [opt] template-id
4246        ( expression-list [opt] )
4247      postfix-expression . template [opt] id-expression
4248      postfix-expression -> template [opt] id-expression
4249      postfix-expression . pseudo-destructor-name
4250      postfix-expression -> pseudo-destructor-name
4251      postfix-expression ++
4252      postfix-expression --
4253      dynamic_cast < type-id > ( expression )
4254      static_cast < type-id > ( expression )
4255      reinterpret_cast < type-id > ( expression )
4256      const_cast < type-id > ( expression )
4257      typeid ( expression )
4258      typeid ( type-id )
4259
4260    GNU Extension:
4261
4262    postfix-expression:
4263      ( type-id ) { initializer-list , [opt] }
4264
4265    This extension is a GNU version of the C99 compound-literal
4266    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4267    but they are essentially the same concept.)
4268
4269    If ADDRESS_P is true, the postfix expression is the operand of the
4270    `&' operator.  CAST_P is true if this expression is the target of a
4271    cast.
4272
4273    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4274    class member access expressions [expr.ref].
4275
4276    Returns a representation of the expression.  */
4277
4278 static tree
4279 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4280                               bool member_access_only_p)
4281 {
4282   cp_token *token;
4283   enum rid keyword;
4284   cp_id_kind idk = CP_ID_KIND_NONE;
4285   tree postfix_expression = NULL_TREE;
4286   bool is_member_access = false;
4287
4288   /* Peek at the next token.  */
4289   token = cp_lexer_peek_token (parser->lexer);
4290   /* Some of the productions are determined by keywords.  */
4291   keyword = token->keyword;
4292   switch (keyword)
4293     {
4294     case RID_DYNCAST:
4295     case RID_STATCAST:
4296     case RID_REINTCAST:
4297     case RID_CONSTCAST:
4298       {
4299         tree type;
4300         tree expression;
4301         const char *saved_message;
4302
4303         /* All of these can be handled in the same way from the point
4304            of view of parsing.  Begin by consuming the token
4305            identifying the cast.  */
4306         cp_lexer_consume_token (parser->lexer);
4307
4308         /* New types cannot be defined in the cast.  */
4309         saved_message = parser->type_definition_forbidden_message;
4310         parser->type_definition_forbidden_message
4311           = "types may not be defined in casts";
4312
4313         /* Look for the opening `<'.  */
4314         cp_parser_require (parser, CPP_LESS, "`<'");
4315         /* Parse the type to which we are casting.  */
4316         type = cp_parser_type_id (parser);
4317         /* Look for the closing `>'.  */
4318         cp_parser_require (parser, CPP_GREATER, "`>'");
4319         /* Restore the old message.  */
4320         parser->type_definition_forbidden_message = saved_message;
4321
4322         /* And the expression which is being cast.  */
4323         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4324         expression = cp_parser_expression (parser, /*cast_p=*/true);
4325         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4326
4327         /* Only type conversions to integral or enumeration types
4328            can be used in constant-expressions.  */
4329         if (!cast_valid_in_integral_constant_expression_p (type)
4330             && (cp_parser_non_integral_constant_expression
4331                 (parser,
4332                  "a cast to a type other than an integral or "
4333                  "enumeration type")))
4334           return error_mark_node;
4335
4336         switch (keyword)
4337           {
4338           case RID_DYNCAST:
4339             postfix_expression
4340               = build_dynamic_cast (type, expression);
4341             break;
4342           case RID_STATCAST:
4343             postfix_expression
4344               = build_static_cast (type, expression);
4345             break;
4346           case RID_REINTCAST:
4347             postfix_expression
4348               = build_reinterpret_cast (type, expression);
4349             break;
4350           case RID_CONSTCAST:
4351             postfix_expression
4352               = build_const_cast (type, expression);
4353             break;
4354           default:
4355             gcc_unreachable ();
4356           }
4357       }
4358       break;
4359
4360     case RID_TYPEID:
4361       {
4362         tree type;
4363         const char *saved_message;
4364         bool saved_in_type_id_in_expr_p;
4365
4366         /* Consume the `typeid' token.  */
4367         cp_lexer_consume_token (parser->lexer);
4368         /* Look for the `(' token.  */
4369         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4370         /* Types cannot be defined in a `typeid' expression.  */
4371         saved_message = parser->type_definition_forbidden_message;
4372         parser->type_definition_forbidden_message
4373           = "types may not be defined in a `typeid\' expression";
4374         /* We can't be sure yet whether we're looking at a type-id or an
4375            expression.  */
4376         cp_parser_parse_tentatively (parser);
4377         /* Try a type-id first.  */
4378         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4379         parser->in_type_id_in_expr_p = true;
4380         type = cp_parser_type_id (parser);
4381         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4382         /* Look for the `)' token.  Otherwise, we can't be sure that
4383            we're not looking at an expression: consider `typeid (int
4384            (3))', for example.  */
4385         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4386         /* If all went well, simply lookup the type-id.  */
4387         if (cp_parser_parse_definitely (parser))
4388           postfix_expression = get_typeid (type);
4389         /* Otherwise, fall back to the expression variant.  */
4390         else
4391           {
4392             tree expression;
4393
4394             /* Look for an expression.  */
4395             expression = cp_parser_expression (parser, /*cast_p=*/false);
4396             /* Compute its typeid.  */
4397             postfix_expression = build_typeid (expression);
4398             /* Look for the `)' token.  */
4399             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4400           }
4401         /* Restore the saved message.  */
4402         parser->type_definition_forbidden_message = saved_message;
4403         /* `typeid' may not appear in an integral constant expression.  */
4404         if (cp_parser_non_integral_constant_expression(parser,
4405                                                        "`typeid' operator"))
4406           return error_mark_node;
4407       }
4408       break;
4409
4410     case RID_TYPENAME:
4411       {
4412         tree type;
4413         /* The syntax permitted here is the same permitted for an
4414            elaborated-type-specifier.  */
4415         type = cp_parser_elaborated_type_specifier (parser,
4416                                                     /*is_friend=*/false,
4417                                                     /*is_declaration=*/false);
4418         postfix_expression = cp_parser_functional_cast (parser, type);
4419       }
4420       break;
4421
4422     default:
4423       {
4424         tree type;
4425
4426         /* If the next thing is a simple-type-specifier, we may be
4427            looking at a functional cast.  We could also be looking at
4428            an id-expression.  So, we try the functional cast, and if
4429            that doesn't work we fall back to the primary-expression.  */
4430         cp_parser_parse_tentatively (parser);
4431         /* Look for the simple-type-specifier.  */
4432         type = cp_parser_simple_type_specifier (parser,
4433                                                 /*decl_specs=*/NULL,
4434                                                 CP_PARSER_FLAGS_NONE);
4435         /* Parse the cast itself.  */
4436         if (!cp_parser_error_occurred (parser))
4437           postfix_expression
4438             = cp_parser_functional_cast (parser, type);
4439         /* If that worked, we're done.  */
4440         if (cp_parser_parse_definitely (parser))
4441           break;
4442
4443         /* If the functional-cast didn't work out, try a
4444            compound-literal.  */
4445         if (cp_parser_allow_gnu_extensions_p (parser)
4446             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4447           {
4448             VEC(constructor_elt,gc) *initializer_list = NULL;
4449             bool saved_in_type_id_in_expr_p;
4450
4451             cp_parser_parse_tentatively (parser);
4452             /* Consume the `('.  */
4453             cp_lexer_consume_token (parser->lexer);
4454             /* Parse the type.  */
4455             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4456             parser->in_type_id_in_expr_p = true;
4457             type = cp_parser_type_id (parser);
4458             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4459             /* Look for the `)'.  */
4460             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4461             /* Look for the `{'.  */
4462             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4463             /* If things aren't going well, there's no need to
4464                keep going.  */
4465             if (!cp_parser_error_occurred (parser))
4466               {
4467                 bool non_constant_p;
4468                 /* Parse the initializer-list.  */
4469                 initializer_list
4470                   = cp_parser_initializer_list (parser, &non_constant_p);
4471                 /* Allow a trailing `,'.  */
4472                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4473                   cp_lexer_consume_token (parser->lexer);
4474                 /* Look for the final `}'.  */
4475                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4476               }
4477             /* If that worked, we're definitely looking at a
4478                compound-literal expression.  */
4479             if (cp_parser_parse_definitely (parser))
4480               {
4481                 /* Warn the user that a compound literal is not
4482                    allowed in standard C++.  */
4483                 if (pedantic)
4484                   pedwarn ("ISO C++ forbids compound-literals");
4485                 /* For simplicity, we disallow compound literals in
4486                    constant-expressions.  We could
4487                    allow compound literals of integer type, whose
4488                    initializer was a constant, in constant
4489                    expressions.  Permitting that usage, as a further
4490                    extension, would not change the meaning of any
4491                    currently accepted programs.  (Of course, as
4492                    compound literals are not part of ISO C++, the
4493                    standard has nothing to say.)  */
4494                 if (cp_parser_non_integral_constant_expression 
4495                     (parser, "non-constant compound literals"))
4496                   {
4497                     postfix_expression = error_mark_node;
4498                     break;
4499                   }
4500                 /* Form the representation of the compound-literal.  */
4501                 postfix_expression
4502                   = finish_compound_literal (type, initializer_list);
4503                 break;
4504               }
4505           }
4506
4507         /* It must be a primary-expression.  */
4508         postfix_expression
4509           = cp_parser_primary_expression (parser, address_p, cast_p,
4510                                           /*template_arg_p=*/false,
4511                                           &idk);
4512       }
4513       break;
4514     }
4515
4516   /* Keep looping until the postfix-expression is complete.  */
4517   while (true)
4518     {
4519       if (idk == CP_ID_KIND_UNQUALIFIED
4520           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4521           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4522         /* It is not a Koenig lookup function call.  */
4523         postfix_expression
4524           = unqualified_name_lookup_error (postfix_expression);
4525
4526       /* Peek at the next token.  */
4527       token = cp_lexer_peek_token (parser->lexer);
4528
4529       switch (token->type)
4530         {
4531         case CPP_OPEN_SQUARE:
4532           postfix_expression
4533             = cp_parser_postfix_open_square_expression (parser,
4534                                                         postfix_expression,
4535                                                         false);
4536           idk = CP_ID_KIND_NONE;
4537           is_member_access = false;
4538           break;
4539
4540         case CPP_OPEN_PAREN:
4541           /* postfix-expression ( expression-list [opt] ) */
4542           {
4543             bool koenig_p;
4544             bool is_builtin_constant_p;
4545             bool saved_integral_constant_expression_p = false;
4546             bool saved_non_integral_constant_expression_p = false;
4547             tree args;
4548
4549             is_member_access = false;
4550
4551             is_builtin_constant_p
4552               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4553             if (is_builtin_constant_p)
4554               {
4555                 /* The whole point of __builtin_constant_p is to allow
4556                    non-constant expressions to appear as arguments.  */
4557                 saved_integral_constant_expression_p
4558                   = parser->integral_constant_expression_p;
4559                 saved_non_integral_constant_expression_p
4560                   = parser->non_integral_constant_expression_p;
4561                 parser->integral_constant_expression_p = false;
4562               }
4563             args = (cp_parser_parenthesized_expression_list
4564                     (parser, /*is_attribute_list=*/false,
4565                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4566                      /*non_constant_p=*/NULL));
4567             if (is_builtin_constant_p)
4568               {
4569                 parser->integral_constant_expression_p
4570                   = saved_integral_constant_expression_p;
4571                 parser->non_integral_constant_expression_p
4572                   = saved_non_integral_constant_expression_p;
4573               }
4574
4575             if (args == error_mark_node)
4576               {
4577                 postfix_expression = error_mark_node;
4578                 break;
4579               }
4580
4581             /* Function calls are not permitted in
4582                constant-expressions.  */
4583             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4584                 && cp_parser_non_integral_constant_expression (parser,
4585                                                                "a function call"))
4586               {
4587                 postfix_expression = error_mark_node;
4588                 break;
4589               }
4590
4591             koenig_p = false;
4592             if (idk == CP_ID_KIND_UNQUALIFIED)
4593               {
4594                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4595                   {
4596                     if (args)
4597                       {
4598                         koenig_p = true;
4599                         postfix_expression
4600                           = perform_koenig_lookup (postfix_expression, args);
4601                       }
4602                     else
4603                       postfix_expression
4604                         = unqualified_fn_lookup_error (postfix_expression);
4605                   }
4606                 /* We do not perform argument-dependent lookup if
4607                    normal lookup finds a non-function, in accordance
4608                    with the expected resolution of DR 218.  */
4609                 else if (args && is_overloaded_fn (postfix_expression))
4610                   {
4611                     tree fn = get_first_fn (postfix_expression);
4612
4613                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4614                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4615
4616                     /* Only do argument dependent lookup if regular
4617                        lookup does not find a set of member functions.
4618                        [basic.lookup.koenig]/2a  */
4619                     if (!DECL_FUNCTION_MEMBER_P (fn))
4620                       {
4621                         koenig_p = true;
4622                         postfix_expression
4623                           = perform_koenig_lookup (postfix_expression, args);
4624                       }
4625                   }
4626               }
4627
4628             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4629               {
4630                 tree instance = TREE_OPERAND (postfix_expression, 0);
4631                 tree fn = TREE_OPERAND (postfix_expression, 1);
4632
4633                 if (processing_template_decl
4634                     && (type_dependent_expression_p (instance)
4635                         || (!BASELINK_P (fn)
4636                             && TREE_CODE (fn) != FIELD_DECL)
4637                         || type_dependent_expression_p (fn)
4638                         || any_type_dependent_arguments_p (args)))
4639                   {
4640                     postfix_expression
4641                       = build_nt_call_list (postfix_expression, args);
4642                     break;
4643                   }
4644
4645                 if (BASELINK_P (fn))
4646                   postfix_expression
4647                     = (build_new_method_call
4648                        (instance, fn, args, NULL_TREE,
4649                         (idk == CP_ID_KIND_QUALIFIED
4650                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4651                         /*fn_p=*/NULL));
4652                 else
4653                   postfix_expression
4654                     = finish_call_expr (postfix_expression, args,
4655                                         /*disallow_virtual=*/false,
4656                                         /*koenig_p=*/false);
4657               }
4658             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4659                      || TREE_CODE (postfix_expression) == MEMBER_REF
4660                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4661               postfix_expression = (build_offset_ref_call_from_tree
4662                                     (postfix_expression, args));
4663             else if (idk == CP_ID_KIND_QUALIFIED)
4664               /* A call to a static class member, or a namespace-scope
4665                  function.  */
4666               postfix_expression
4667                 = finish_call_expr (postfix_expression, args,
4668                                     /*disallow_virtual=*/true,
4669                                     koenig_p);
4670             else
4671               /* All other function calls.  */
4672               postfix_expression
4673                 = finish_call_expr (postfix_expression, args,
4674                                     /*disallow_virtual=*/false,
4675                                     koenig_p);
4676
4677             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4678             idk = CP_ID_KIND_NONE;
4679           }
4680           break;
4681
4682         case CPP_DOT:
4683         case CPP_DEREF:
4684           /* postfix-expression . template [opt] id-expression
4685              postfix-expression . pseudo-destructor-name
4686              postfix-expression -> template [opt] id-expression
4687              postfix-expression -> pseudo-destructor-name */
4688
4689           /* Consume the `.' or `->' operator.  */
4690           cp_lexer_consume_token (parser->lexer);
4691
4692           postfix_expression
4693             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4694                                                       postfix_expression,
4695                                                       false, &idk);
4696
4697           is_member_access = true;
4698           break;
4699
4700         case CPP_PLUS_PLUS:
4701           /* postfix-expression ++  */
4702           /* Consume the `++' token.  */
4703           cp_lexer_consume_token (parser->lexer);
4704           /* Generate a representation for the complete expression.  */
4705           postfix_expression
4706             = finish_increment_expr (postfix_expression,
4707                                      POSTINCREMENT_EXPR);
4708           /* Increments may not appear in constant-expressions.  */
4709           if (cp_parser_non_integral_constant_expression (parser,
4710                                                           "an increment"))
4711             postfix_expression = error_mark_node;
4712           idk = CP_ID_KIND_NONE;
4713           is_member_access = false;
4714           break;
4715
4716         case CPP_MINUS_MINUS:
4717           /* postfix-expression -- */
4718           /* Consume the `--' token.  */
4719           cp_lexer_consume_token (parser->lexer);
4720           /* Generate a representation for the complete expression.  */
4721           postfix_expression
4722             = finish_increment_expr (postfix_expression,
4723                                      POSTDECREMENT_EXPR);
4724           /* Decrements may not appear in constant-expressions.  */
4725           if (cp_parser_non_integral_constant_expression (parser,
4726                                                           "a decrement"))
4727             postfix_expression = error_mark_node;
4728           idk = CP_ID_KIND_NONE;
4729           is_member_access = false;
4730           break;
4731
4732         default:
4733           if (member_access_only_p)
4734             return is_member_access? postfix_expression : error_mark_node;
4735           else
4736             return postfix_expression;
4737         }
4738     }
4739
4740   /* We should never get here.  */
4741   gcc_unreachable ();
4742   return error_mark_node;
4743 }
4744
4745 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4746    by cp_parser_builtin_offsetof.  We're looking for
4747
4748      postfix-expression [ expression ]
4749
4750    FOR_OFFSETOF is set if we're being called in that context, which
4751    changes how we deal with integer constant expressions.  */
4752
4753 static tree
4754 cp_parser_postfix_open_square_expression (cp_parser *parser,
4755                                           tree postfix_expression,
4756                                           bool for_offsetof)
4757 {
4758   tree index;
4759
4760   /* Consume the `[' token.  */
4761   cp_lexer_consume_token (parser->lexer);
4762
4763   /* Parse the index expression.  */
4764   /* ??? For offsetof, there is a question of what to allow here.  If
4765      offsetof is not being used in an integral constant expression context,
4766      then we *could* get the right answer by computing the value at runtime.
4767      If we are in an integral constant expression context, then we might
4768      could accept any constant expression; hard to say without analysis.
4769      Rather than open the barn door too wide right away, allow only integer
4770      constant expressions here.  */
4771   if (for_offsetof)
4772     index = cp_parser_constant_expression (parser, false, NULL);
4773   else
4774     index = cp_parser_expression (parser, /*cast_p=*/false);
4775
4776   /* Look for the closing `]'.  */
4777   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4778
4779   /* Build the ARRAY_REF.  */
4780   postfix_expression = grok_array_decl (postfix_expression, index);
4781
4782   /* When not doing offsetof, array references are not permitted in
4783      constant-expressions.  */
4784   if (!for_offsetof
4785       && (cp_parser_non_integral_constant_expression
4786           (parser, "an array reference")))
4787     postfix_expression = error_mark_node;
4788
4789   return postfix_expression;
4790 }
4791
4792 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4793    by cp_parser_builtin_offsetof.  We're looking for
4794
4795      postfix-expression . template [opt] id-expression
4796      postfix-expression . pseudo-destructor-name
4797      postfix-expression -> template [opt] id-expression
4798      postfix-expression -> pseudo-destructor-name
4799
4800    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4801    limits what of the above we'll actually accept, but nevermind.
4802    TOKEN_TYPE is the "." or "->" token, which will already have been
4803    removed from the stream.  */
4804
4805 static tree
4806 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4807                                         enum cpp_ttype token_type,
4808                                         tree postfix_expression,
4809                                         bool for_offsetof, cp_id_kind *idk)
4810 {
4811   tree name;
4812   bool dependent_p;
4813   bool pseudo_destructor_p;
4814   tree scope = NULL_TREE;
4815
4816   /* If this is a `->' operator, dereference the pointer.  */
4817   if (token_type == CPP_DEREF)
4818     postfix_expression = build_x_arrow (postfix_expression);
4819   /* Check to see whether or not the expression is type-dependent.  */
4820   dependent_p = type_dependent_expression_p (postfix_expression);
4821   /* The identifier following the `->' or `.' is not qualified.  */
4822   parser->scope = NULL_TREE;
4823   parser->qualifying_scope = NULL_TREE;
4824   parser->object_scope = NULL_TREE;
4825   *idk = CP_ID_KIND_NONE;
4826   /* Enter the scope corresponding to the type of the object
4827      given by the POSTFIX_EXPRESSION.  */
4828   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4829     {
4830       scope = TREE_TYPE (postfix_expression);
4831       /* According to the standard, no expression should ever have
4832          reference type.  Unfortunately, we do not currently match
4833          the standard in this respect in that our internal representation
4834          of an expression may have reference type even when the standard
4835          says it does not.  Therefore, we have to manually obtain the
4836          underlying type here.  */
4837       scope = non_reference (scope);
4838       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4839       if (scope == unknown_type_node)
4840         {
4841           error ("%qE does not have class type", postfix_expression);
4842           scope = NULL_TREE;
4843         }
4844       else
4845         scope = complete_type_or_else (scope, NULL_TREE);
4846       /* Let the name lookup machinery know that we are processing a
4847          class member access expression.  */
4848       parser->context->object_type = scope;
4849       /* If something went wrong, we want to be able to discern that case,
4850          as opposed to the case where there was no SCOPE due to the type
4851          of expression being dependent.  */
4852       if (!scope)
4853         scope = error_mark_node;
4854       /* If the SCOPE was erroneous, make the various semantic analysis
4855          functions exit quickly -- and without issuing additional error
4856          messages.  */
4857       if (scope == error_mark_node)
4858         postfix_expression = error_mark_node;
4859     }
4860
4861   /* Assume this expression is not a pseudo-destructor access.  */
4862   pseudo_destructor_p = false;
4863
4864   /* If the SCOPE is a scalar type, then, if this is a valid program,
4865      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4866      is type dependent, it can be pseudo-destructor-name or something else.
4867      Try to parse it as pseudo-destructor-name first.  */
4868   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4869     {
4870       tree s;
4871       tree type;
4872
4873       cp_parser_parse_tentatively (parser);
4874       /* Parse the pseudo-destructor-name.  */
4875       s = NULL_TREE;
4876       cp_parser_pseudo_destructor_name (parser, &s, &type);
4877       if (dependent_p
4878           && (cp_parser_error_occurred (parser)
4879               || TREE_CODE (type) != TYPE_DECL
4880               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4881         cp_parser_abort_tentative_parse (parser);
4882       else if (cp_parser_parse_definitely (parser))
4883         {
4884           pseudo_destructor_p = true;
4885           postfix_expression
4886             = finish_pseudo_destructor_expr (postfix_expression,
4887                                              s, TREE_TYPE (type));
4888         }
4889     }
4890
4891   if (!pseudo_destructor_p)
4892     {
4893       /* If the SCOPE is not a scalar type, we are looking at an
4894          ordinary class member access expression, rather than a
4895          pseudo-destructor-name.  */
4896       bool template_p;
4897       /* Parse the id-expression.  */
4898       name = (cp_parser_id_expression
4899               (parser,
4900                cp_parser_optional_template_keyword (parser),
4901                /*check_dependency_p=*/true,
4902                &template_p,
4903                /*declarator_p=*/false,
4904                /*optional_p=*/false));
4905       /* In general, build a SCOPE_REF if the member name is qualified.
4906          However, if the name was not dependent and has already been
4907          resolved; there is no need to build the SCOPE_REF.  For example;
4908
4909              struct X { void f(); };
4910              template <typename T> void f(T* t) { t->X::f(); }
4911
4912          Even though "t" is dependent, "X::f" is not and has been resolved
4913          to a BASELINK; there is no need to include scope information.  */
4914
4915       /* But we do need to remember that there was an explicit scope for
4916          virtual function calls.  */
4917       if (parser->scope)
4918         *idk = CP_ID_KIND_QUALIFIED;
4919
4920       /* If the name is a template-id that names a type, we will get a
4921          TYPE_DECL here.  That is invalid code.  */
4922       if (TREE_CODE (name) == TYPE_DECL)
4923         {
4924           error ("invalid use of %qD", name);
4925           postfix_expression = error_mark_node;
4926         }
4927       else
4928         {
4929           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4930             {
4931               name = build_qualified_name (/*type=*/NULL_TREE,
4932                                            parser->scope,
4933                                            name,
4934                                            template_p);
4935               parser->scope = NULL_TREE;
4936               parser->qualifying_scope = NULL_TREE;
4937               parser->object_scope = NULL_TREE;
4938             }
4939           if (scope && name && BASELINK_P (name))
4940             adjust_result_of_qualified_name_lookup
4941               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4942           postfix_expression
4943             = finish_class_member_access_expr (postfix_expression, name,
4944                                                template_p);
4945         }
4946     }
4947
4948   /* We no longer need to look up names in the scope of the object on
4949      the left-hand side of the `.' or `->' operator.  */
4950   parser->context->object_type = NULL_TREE;
4951
4952   /* Outside of offsetof, these operators may not appear in
4953      constant-expressions.  */
4954   if (!for_offsetof
4955       && (cp_parser_non_integral_constant_expression
4956           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4957     postfix_expression = error_mark_node;
4958
4959   return postfix_expression;
4960 }
4961
4962 /* Parse a parenthesized expression-list.
4963
4964    expression-list:
4965      assignment-expression
4966      expression-list, assignment-expression
4967
4968    attribute-list:
4969      expression-list
4970      identifier
4971      identifier, expression-list
4972
4973    CAST_P is true if this expression is the target of a cast.
4974
4975    ALLOW_EXPANSION_P is true if this expression allows expansion of an
4976    argument pack.
4977
4978    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4979    representation of an assignment-expression.  Note that a TREE_LIST
4980    is returned even if there is only a single expression in the list.
4981    error_mark_node is returned if the ( and or ) are
4982    missing. NULL_TREE is returned on no expressions. The parentheses
4983    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4984    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4985    indicates whether or not all of the expressions in the list were
4986    constant.  */
4987
4988 static tree
4989 cp_parser_parenthesized_expression_list (cp_parser* parser,
4990                                          bool is_attribute_list,
4991                                          bool cast_p,
4992                                          bool allow_expansion_p,
4993                                          bool *non_constant_p)
4994 {
4995   tree expression_list = NULL_TREE;
4996   bool fold_expr_p = is_attribute_list;
4997   tree identifier = NULL_TREE;
4998   bool saved_greater_than_is_operator_p;
4999
5000   /* Assume all the expressions will be constant.  */
5001   if (non_constant_p)
5002     *non_constant_p = false;
5003
5004   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5005     return error_mark_node;
5006
5007   /* Within a parenthesized expression, a `>' token is always
5008      the greater-than operator.  */
5009   saved_greater_than_is_operator_p
5010     = parser->greater_than_is_operator_p;
5011   parser->greater_than_is_operator_p = true;
5012
5013   /* Consume expressions until there are no more.  */
5014   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5015     while (true)
5016       {
5017         tree expr;
5018
5019         /* At the beginning of attribute lists, check to see if the
5020            next token is an identifier.  */
5021         if (is_attribute_list
5022             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5023           {
5024             cp_token *token;
5025
5026             /* Consume the identifier.  */
5027             token = cp_lexer_consume_token (parser->lexer);
5028             /* Save the identifier.  */
5029             identifier = token->u.value;
5030           }
5031         else
5032           {
5033             /* Parse the next assignment-expression.  */
5034             if (non_constant_p)
5035               {
5036                 bool expr_non_constant_p;
5037                 expr = (cp_parser_constant_expression
5038                         (parser, /*allow_non_constant_p=*/true,
5039                          &expr_non_constant_p));
5040                 if (expr_non_constant_p)
5041                   *non_constant_p = true;
5042               }
5043             else
5044               expr = cp_parser_assignment_expression (parser, cast_p);
5045
5046             if (fold_expr_p)
5047               expr = fold_non_dependent_expr (expr);
5048
5049             /* If we have an ellipsis, then this is an expression
5050                expansion.  */
5051             if (allow_expansion_p
5052                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5053               {
5054                 /* Consume the `...'.  */
5055                 cp_lexer_consume_token (parser->lexer);
5056
5057                 /* Build the argument pack.  */
5058                 expr = make_pack_expansion (expr);
5059               }
5060
5061              /* Add it to the list.  We add error_mark_node
5062                 expressions to the list, so that we can still tell if
5063                 the correct form for a parenthesized expression-list
5064                 is found. That gives better errors.  */
5065             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5066
5067             if (expr == error_mark_node)
5068               goto skip_comma;
5069           }
5070
5071         /* After the first item, attribute lists look the same as
5072            expression lists.  */
5073         is_attribute_list = false;
5074
5075       get_comma:;
5076         /* If the next token isn't a `,', then we are done.  */
5077         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5078           break;
5079
5080         /* Otherwise, consume the `,' and keep going.  */
5081         cp_lexer_consume_token (parser->lexer);
5082       }
5083
5084   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5085     {
5086       int ending;
5087
5088     skip_comma:;
5089       /* We try and resync to an unnested comma, as that will give the
5090          user better diagnostics.  */
5091       ending = cp_parser_skip_to_closing_parenthesis (parser,
5092                                                       /*recovering=*/true,
5093                                                       /*or_comma=*/true,
5094                                                       /*consume_paren=*/true);
5095       if (ending < 0)
5096         goto get_comma;
5097       if (!ending)
5098         {
5099           parser->greater_than_is_operator_p
5100             = saved_greater_than_is_operator_p;
5101           return error_mark_node;
5102         }
5103     }
5104
5105   parser->greater_than_is_operator_p
5106     = saved_greater_than_is_operator_p;
5107
5108   /* We built up the list in reverse order so we must reverse it now.  */
5109   expression_list = nreverse (expression_list);
5110   if (identifier)
5111     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5112
5113   return expression_list;
5114 }
5115
5116 /* Parse a pseudo-destructor-name.
5117
5118    pseudo-destructor-name:
5119      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5120      :: [opt] nested-name-specifier template template-id :: ~ type-name
5121      :: [opt] nested-name-specifier [opt] ~ type-name
5122
5123    If either of the first two productions is used, sets *SCOPE to the
5124    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5125    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5126    or ERROR_MARK_NODE if the parse fails.  */
5127
5128 static void
5129 cp_parser_pseudo_destructor_name (cp_parser* parser,
5130                                   tree* scope,
5131                                   tree* type)
5132 {
5133   bool nested_name_specifier_p;
5134
5135   /* Assume that things will not work out.  */
5136   *type = error_mark_node;
5137
5138   /* Look for the optional `::' operator.  */
5139   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5140   /* Look for the optional nested-name-specifier.  */
5141   nested_name_specifier_p
5142     = (cp_parser_nested_name_specifier_opt (parser,
5143                                             /*typename_keyword_p=*/false,
5144                                             /*check_dependency_p=*/true,
5145                                             /*type_p=*/false,
5146                                             /*is_declaration=*/true)
5147        != NULL_TREE);
5148   /* Now, if we saw a nested-name-specifier, we might be doing the
5149      second production.  */
5150   if (nested_name_specifier_p
5151       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5152     {
5153       /* Consume the `template' keyword.  */
5154       cp_lexer_consume_token (parser->lexer);
5155       /* Parse the template-id.  */
5156       cp_parser_template_id (parser,
5157                              /*template_keyword_p=*/true,
5158                              /*check_dependency_p=*/false,
5159                              /*is_declaration=*/true);
5160       /* Look for the `::' token.  */
5161       cp_parser_require (parser, CPP_SCOPE, "`::'");
5162     }
5163   /* If the next token is not a `~', then there might be some
5164      additional qualification.  */
5165   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5166     {
5167       /* Look for the type-name.  */
5168       *scope = TREE_TYPE (cp_parser_type_name (parser));
5169
5170       if (*scope == error_mark_node)
5171         return;
5172
5173       /* If we don't have ::~, then something has gone wrong.  Since
5174          the only caller of this function is looking for something
5175          after `.' or `->' after a scalar type, most likely the
5176          program is trying to get a member of a non-aggregate
5177          type.  */
5178       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5179           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5180         {
5181           cp_parser_error (parser, "request for member of non-aggregate type");
5182           return;
5183         }
5184
5185       /* Look for the `::' token.  */
5186       cp_parser_require (parser, CPP_SCOPE, "`::'");
5187     }
5188   else
5189     *scope = NULL_TREE;
5190
5191   /* Look for the `~'.  */
5192   cp_parser_require (parser, CPP_COMPL, "`~'");
5193   /* Look for the type-name again.  We are not responsible for
5194      checking that it matches the first type-name.  */
5195   *type = cp_parser_type_name (parser);
5196 }
5197
5198 /* Parse a unary-expression.
5199
5200    unary-expression:
5201      postfix-expression
5202      ++ cast-expression
5203      -- cast-expression
5204      unary-operator cast-expression
5205      sizeof unary-expression
5206      sizeof ( type-id )
5207      new-expression
5208      delete-expression
5209
5210    GNU Extensions:
5211
5212    unary-expression:
5213      __extension__ cast-expression
5214      __alignof__ unary-expression
5215      __alignof__ ( type-id )
5216      __real__ cast-expression
5217      __imag__ cast-expression
5218      && identifier
5219
5220    ADDRESS_P is true iff the unary-expression is appearing as the
5221    operand of the `&' operator.   CAST_P is true if this expression is
5222    the target of a cast.
5223
5224    Returns a representation of the expression.  */
5225
5226 static tree
5227 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5228 {
5229   cp_token *token;
5230   enum tree_code unary_operator;
5231
5232   /* Peek at the next token.  */
5233   token = cp_lexer_peek_token (parser->lexer);
5234   /* Some keywords give away the kind of expression.  */
5235   if (token->type == CPP_KEYWORD)
5236     {
5237       enum rid keyword = token->keyword;
5238
5239       switch (keyword)
5240         {
5241         case RID_ALIGNOF:
5242         case RID_SIZEOF:
5243           {
5244             tree operand;
5245             enum tree_code op;
5246
5247             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5248             /* Consume the token.  */
5249             cp_lexer_consume_token (parser->lexer);
5250             /* Parse the operand.  */
5251             operand = cp_parser_sizeof_operand (parser, keyword);
5252
5253             if (TYPE_P (operand))
5254               return cxx_sizeof_or_alignof_type (operand, op, true);
5255             else
5256               return cxx_sizeof_or_alignof_expr (operand, op);
5257           }
5258
5259         case RID_NEW:
5260           return cp_parser_new_expression (parser);
5261
5262         case RID_DELETE:
5263           return cp_parser_delete_expression (parser);
5264
5265         case RID_EXTENSION:
5266           {
5267             /* The saved value of the PEDANTIC flag.  */
5268             int saved_pedantic;
5269             tree expr;
5270
5271             /* Save away the PEDANTIC flag.  */
5272             cp_parser_extension_opt (parser, &saved_pedantic);
5273             /* Parse the cast-expression.  */
5274             expr = cp_parser_simple_cast_expression (parser);
5275             /* Restore the PEDANTIC flag.  */
5276             pedantic = saved_pedantic;
5277
5278             return expr;
5279           }
5280
5281         case RID_REALPART:
5282         case RID_IMAGPART:
5283           {
5284             tree expression;
5285
5286             /* Consume the `__real__' or `__imag__' token.  */
5287             cp_lexer_consume_token (parser->lexer);
5288             /* Parse the cast-expression.  */
5289             expression = cp_parser_simple_cast_expression (parser);
5290             /* Create the complete representation.  */
5291             return build_x_unary_op ((keyword == RID_REALPART
5292                                       ? REALPART_EXPR : IMAGPART_EXPR),
5293                                      expression);
5294           }
5295           break;
5296
5297         default:
5298           break;
5299         }
5300     }
5301
5302   /* Look for the `:: new' and `:: delete', which also signal the
5303      beginning of a new-expression, or delete-expression,
5304      respectively.  If the next token is `::', then it might be one of
5305      these.  */
5306   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5307     {
5308       enum rid keyword;
5309
5310       /* See if the token after the `::' is one of the keywords in
5311          which we're interested.  */
5312       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5313       /* If it's `new', we have a new-expression.  */
5314       if (keyword == RID_NEW)
5315         return cp_parser_new_expression (parser);
5316       /* Similarly, for `delete'.  */
5317       else if (keyword == RID_DELETE)
5318         return cp_parser_delete_expression (parser);
5319     }
5320
5321   /* Look for a unary operator.  */
5322   unary_operator = cp_parser_unary_operator (token);
5323   /* The `++' and `--' operators can be handled similarly, even though
5324      they are not technically unary-operators in the grammar.  */
5325   if (unary_operator == ERROR_MARK)
5326     {
5327       if (token->type == CPP_PLUS_PLUS)
5328         unary_operator = PREINCREMENT_EXPR;
5329       else if (token->type == CPP_MINUS_MINUS)
5330         unary_operator = PREDECREMENT_EXPR;
5331       /* Handle the GNU address-of-label extension.  */
5332       else if (cp_parser_allow_gnu_extensions_p (parser)
5333                && token->type == CPP_AND_AND)
5334         {
5335           tree identifier;
5336           tree expression;
5337
5338           /* Consume the '&&' token.  */
5339           cp_lexer_consume_token (parser->lexer);
5340           /* Look for the identifier.  */
5341           identifier = cp_parser_identifier (parser);
5342           /* Create an expression representing the address.  */
5343           expression = finish_label_address_expr (identifier);
5344           if (cp_parser_non_integral_constant_expression (parser,
5345                                                 "the address of a label"))
5346             expression = error_mark_node;
5347           return expression;
5348         }
5349     }
5350   if (unary_operator != ERROR_MARK)
5351     {
5352       tree cast_expression;
5353       tree expression = error_mark_node;
5354       const char *non_constant_p = NULL;
5355
5356       /* Consume the operator token.  */
5357       token = cp_lexer_consume_token (parser->lexer);
5358       /* Parse the cast-expression.  */
5359       cast_expression
5360         = cp_parser_cast_expression (parser,
5361                                      unary_operator == ADDR_EXPR,
5362                                      /*cast_p=*/false);
5363       /* Now, build an appropriate representation.  */
5364       switch (unary_operator)
5365         {
5366         case INDIRECT_REF:
5367           non_constant_p = "`*'";
5368           expression = build_x_indirect_ref (cast_expression, "unary *");
5369           break;
5370
5371         case ADDR_EXPR:
5372           non_constant_p = "`&'";
5373           /* Fall through.  */
5374         case BIT_NOT_EXPR:
5375           expression = build_x_unary_op (unary_operator, cast_expression);
5376           break;
5377
5378         case PREINCREMENT_EXPR:
5379         case PREDECREMENT_EXPR:
5380           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5381                             ? "`++'" : "`--'");
5382           /* Fall through.  */
5383         case UNARY_PLUS_EXPR:
5384         case NEGATE_EXPR:
5385         case TRUTH_NOT_EXPR:
5386           expression = finish_unary_op_expr (unary_operator, cast_expression);
5387           break;
5388
5389         default:
5390           gcc_unreachable ();
5391         }
5392
5393       if (non_constant_p
5394           && cp_parser_non_integral_constant_expression (parser,
5395                                                          non_constant_p))
5396         expression = error_mark_node;
5397
5398       return expression;
5399     }
5400
5401   return cp_parser_postfix_expression (parser, address_p, cast_p,
5402                                        /*member_access_only_p=*/false);
5403 }
5404
5405 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5406    unary-operator, the corresponding tree code is returned.  */
5407
5408 static enum tree_code
5409 cp_parser_unary_operator (cp_token* token)
5410 {
5411   switch (token->type)
5412     {
5413     case CPP_MULT:
5414       return INDIRECT_REF;
5415
5416     case CPP_AND:
5417       return ADDR_EXPR;
5418
5419     case CPP_PLUS:
5420       return UNARY_PLUS_EXPR;
5421
5422     case CPP_MINUS:
5423       return NEGATE_EXPR;
5424
5425     case CPP_NOT:
5426       return TRUTH_NOT_EXPR;
5427
5428     case CPP_COMPL:
5429       return BIT_NOT_EXPR;
5430
5431     default:
5432       return ERROR_MARK;
5433     }
5434 }
5435
5436 /* Parse a new-expression.
5437
5438    new-expression:
5439      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5440      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5441
5442    Returns a representation of the expression.  */
5443
5444 static tree
5445 cp_parser_new_expression (cp_parser* parser)
5446 {
5447   bool global_scope_p;
5448   tree placement;
5449   tree type;
5450   tree initializer;
5451   tree nelts;
5452
5453   /* Look for the optional `::' operator.  */
5454   global_scope_p
5455     = (cp_parser_global_scope_opt (parser,
5456                                    /*current_scope_valid_p=*/false)
5457        != NULL_TREE);
5458   /* Look for the `new' operator.  */
5459   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5460   /* There's no easy way to tell a new-placement from the
5461      `( type-id )' construct.  */
5462   cp_parser_parse_tentatively (parser);
5463   /* Look for a new-placement.  */
5464   placement = cp_parser_new_placement (parser);
5465   /* If that didn't work out, there's no new-placement.  */
5466   if (!cp_parser_parse_definitely (parser))
5467     placement = NULL_TREE;
5468
5469   /* If the next token is a `(', then we have a parenthesized
5470      type-id.  */
5471   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5472     {
5473       /* Consume the `('.  */
5474       cp_lexer_consume_token (parser->lexer);
5475       /* Parse the type-id.  */
5476       type = cp_parser_type_id (parser);
5477       /* Look for the closing `)'.  */
5478       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5479       /* There should not be a direct-new-declarator in this production,
5480          but GCC used to allowed this, so we check and emit a sensible error
5481          message for this case.  */
5482       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5483         {
5484           error ("array bound forbidden after parenthesized type-id");
5485           inform ("try removing the parentheses around the type-id");
5486           cp_parser_direct_new_declarator (parser);
5487         }
5488       nelts = NULL_TREE;
5489     }
5490   /* Otherwise, there must be a new-type-id.  */
5491   else
5492     type = cp_parser_new_type_id (parser, &nelts);
5493
5494   /* If the next token is a `(', then we have a new-initializer.  */
5495   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5496     initializer = cp_parser_new_initializer (parser);
5497   else
5498     initializer = NULL_TREE;
5499
5500   /* A new-expression may not appear in an integral constant
5501      expression.  */
5502   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5503     return error_mark_node;
5504
5505   /* Create a representation of the new-expression.  */
5506   return build_new (placement, type, nelts, initializer, global_scope_p);
5507 }
5508
5509 /* Parse a new-placement.
5510
5511    new-placement:
5512      ( expression-list )
5513
5514    Returns the same representation as for an expression-list.  */
5515
5516 static tree
5517 cp_parser_new_placement (cp_parser* parser)
5518 {
5519   tree expression_list;
5520
5521   /* Parse the expression-list.  */
5522   expression_list = (cp_parser_parenthesized_expression_list
5523                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5524                       /*non_constant_p=*/NULL));
5525
5526   return expression_list;
5527 }
5528
5529 /* Parse a new-type-id.
5530
5531    new-type-id:
5532      type-specifier-seq new-declarator [opt]
5533
5534    Returns the TYPE allocated.  If the new-type-id indicates an array
5535    type, *NELTS is set to the number of elements in the last array
5536    bound; the TYPE will not include the last array bound.  */
5537
5538 static tree
5539 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5540 {
5541   cp_decl_specifier_seq type_specifier_seq;
5542   cp_declarator *new_declarator;
5543   cp_declarator *declarator;
5544   cp_declarator *outer_declarator;
5545   const char *saved_message;
5546   tree type;
5547
5548   /* The type-specifier sequence must not contain type definitions.
5549      (It cannot contain declarations of new types either, but if they
5550      are not definitions we will catch that because they are not
5551      complete.)  */
5552   saved_message = parser->type_definition_forbidden_message;
5553   parser->type_definition_forbidden_message
5554     = "types may not be defined in a new-type-id";
5555   /* Parse the type-specifier-seq.  */
5556   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5557                                 &type_specifier_seq);
5558   /* Restore the old message.  */
5559   parser->type_definition_forbidden_message = saved_message;
5560   /* Parse the new-declarator.  */
5561   new_declarator = cp_parser_new_declarator_opt (parser);
5562
5563   /* Determine the number of elements in the last array dimension, if
5564      any.  */
5565   *nelts = NULL_TREE;
5566   /* Skip down to the last array dimension.  */
5567   declarator = new_declarator;
5568   outer_declarator = NULL;
5569   while (declarator && (declarator->kind == cdk_pointer
5570                         || declarator->kind == cdk_ptrmem))
5571     {
5572       outer_declarator = declarator;
5573       declarator = declarator->declarator;
5574     }
5575   while (declarator
5576          && declarator->kind == cdk_array
5577          && declarator->declarator
5578          && declarator->declarator->kind == cdk_array)
5579     {
5580       outer_declarator = declarator;
5581       declarator = declarator->declarator;
5582     }
5583
5584   if (declarator && declarator->kind == cdk_array)
5585     {
5586       *nelts = declarator->u.array.bounds;
5587       if (*nelts == error_mark_node)
5588         *nelts = integer_one_node;
5589
5590       if (outer_declarator)
5591         outer_declarator->declarator = declarator->declarator;
5592       else
5593         new_declarator = NULL;
5594     }
5595
5596   type = groktypename (&type_specifier_seq, new_declarator);
5597   return type;
5598 }
5599
5600 /* Parse an (optional) new-declarator.
5601
5602    new-declarator:
5603      ptr-operator new-declarator [opt]
5604      direct-new-declarator
5605
5606    Returns the declarator.  */
5607
5608 static cp_declarator *
5609 cp_parser_new_declarator_opt (cp_parser* parser)
5610 {
5611   enum tree_code code;
5612   tree type;
5613   cp_cv_quals cv_quals;
5614
5615   /* We don't know if there's a ptr-operator next, or not.  */
5616   cp_parser_parse_tentatively (parser);
5617   /* Look for a ptr-operator.  */
5618   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5619   /* If that worked, look for more new-declarators.  */
5620   if (cp_parser_parse_definitely (parser))
5621     {
5622       cp_declarator *declarator;
5623
5624       /* Parse another optional declarator.  */
5625       declarator = cp_parser_new_declarator_opt (parser);
5626
5627       return cp_parser_make_indirect_declarator
5628         (code, type, cv_quals, declarator);
5629     }
5630
5631   /* If the next token is a `[', there is a direct-new-declarator.  */
5632   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5633     return cp_parser_direct_new_declarator (parser);
5634
5635   return NULL;
5636 }
5637
5638 /* Parse a direct-new-declarator.
5639
5640    direct-new-declarator:
5641      [ expression ]
5642      direct-new-declarator [constant-expression]
5643
5644    */
5645
5646 static cp_declarator *
5647 cp_parser_direct_new_declarator (cp_parser* parser)
5648 {
5649   cp_declarator *declarator = NULL;
5650
5651   while (true)
5652     {
5653       tree expression;
5654
5655       /* Look for the opening `['.  */
5656       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5657       /* The first expression is not required to be constant.  */
5658       if (!declarator)
5659         {
5660           expression = cp_parser_expression (parser, /*cast_p=*/false);
5661           /* The standard requires that the expression have integral
5662              type.  DR 74 adds enumeration types.  We believe that the
5663              real intent is that these expressions be handled like the
5664              expression in a `switch' condition, which also allows
5665              classes with a single conversion to integral or
5666              enumeration type.  */
5667           if (!processing_template_decl)
5668             {
5669               expression
5670                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5671                                               expression,
5672                                               /*complain=*/true);
5673               if (!expression)
5674                 {
5675                   error ("expression in new-declarator must have integral "
5676                          "or enumeration type");
5677                   expression = error_mark_node;
5678                 }
5679             }
5680         }
5681       /* But all the other expressions must be.  */
5682       else
5683         expression
5684           = cp_parser_constant_expression (parser,
5685                                            /*allow_non_constant=*/false,
5686                                            NULL);
5687       /* Look for the closing `]'.  */
5688       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5689
5690       /* Add this bound to the declarator.  */
5691       declarator = make_array_declarator (declarator, expression);
5692
5693       /* If the next token is not a `[', then there are no more
5694          bounds.  */
5695       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5696         break;
5697     }
5698
5699   return declarator;
5700 }
5701
5702 /* Parse a new-initializer.
5703
5704    new-initializer:
5705      ( expression-list [opt] )
5706
5707    Returns a representation of the expression-list.  If there is no
5708    expression-list, VOID_ZERO_NODE is returned.  */
5709
5710 static tree
5711 cp_parser_new_initializer (cp_parser* parser)
5712 {
5713   tree expression_list;
5714
5715   expression_list = (cp_parser_parenthesized_expression_list
5716                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5717                       /*non_constant_p=*/NULL));
5718   if (!expression_list)
5719     expression_list = void_zero_node;
5720
5721   return expression_list;
5722 }
5723
5724 /* Parse a delete-expression.
5725
5726    delete-expression:
5727      :: [opt] delete cast-expression
5728      :: [opt] delete [ ] cast-expression
5729
5730    Returns a representation of the expression.  */
5731
5732 static tree
5733 cp_parser_delete_expression (cp_parser* parser)
5734 {
5735   bool global_scope_p;
5736   bool array_p;
5737   tree expression;
5738
5739   /* Look for the optional `::' operator.  */
5740   global_scope_p
5741     = (cp_parser_global_scope_opt (parser,
5742                                    /*current_scope_valid_p=*/false)
5743        != NULL_TREE);
5744   /* Look for the `delete' keyword.  */
5745   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5746   /* See if the array syntax is in use.  */
5747   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5748     {
5749       /* Consume the `[' token.  */
5750       cp_lexer_consume_token (parser->lexer);
5751       /* Look for the `]' token.  */
5752       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5753       /* Remember that this is the `[]' construct.  */
5754       array_p = true;
5755     }
5756   else
5757     array_p = false;
5758
5759   /* Parse the cast-expression.  */
5760   expression = cp_parser_simple_cast_expression (parser);
5761
5762   /* A delete-expression may not appear in an integral constant
5763      expression.  */
5764   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5765     return error_mark_node;
5766
5767   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5768 }
5769
5770 /* Parse a cast-expression.
5771
5772    cast-expression:
5773      unary-expression
5774      ( type-id ) cast-expression
5775
5776    ADDRESS_P is true iff the unary-expression is appearing as the
5777    operand of the `&' operator.   CAST_P is true if this expression is
5778    the target of a cast.
5779
5780    Returns a representation of the expression.  */
5781
5782 static tree
5783 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5784 {
5785   /* If it's a `(', then we might be looking at a cast.  */
5786   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5787     {
5788       tree type = NULL_TREE;
5789       tree expr = NULL_TREE;
5790       bool compound_literal_p;
5791       const char *saved_message;
5792
5793       /* There's no way to know yet whether or not this is a cast.
5794          For example, `(int (3))' is a unary-expression, while `(int)
5795          3' is a cast.  So, we resort to parsing tentatively.  */
5796       cp_parser_parse_tentatively (parser);
5797       /* Types may not be defined in a cast.  */
5798       saved_message = parser->type_definition_forbidden_message;
5799       parser->type_definition_forbidden_message
5800         = "types may not be defined in casts";
5801       /* Consume the `('.  */
5802       cp_lexer_consume_token (parser->lexer);
5803       /* A very tricky bit is that `(struct S) { 3 }' is a
5804          compound-literal (which we permit in C++ as an extension).
5805          But, that construct is not a cast-expression -- it is a
5806          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5807          is legal; if the compound-literal were a cast-expression,
5808          you'd need an extra set of parentheses.)  But, if we parse
5809          the type-id, and it happens to be a class-specifier, then we
5810          will commit to the parse at that point, because we cannot
5811          undo the action that is done when creating a new class.  So,
5812          then we cannot back up and do a postfix-expression.
5813
5814          Therefore, we scan ahead to the closing `)', and check to see
5815          if the token after the `)' is a `{'.  If so, we are not
5816          looking at a cast-expression.
5817
5818          Save tokens so that we can put them back.  */
5819       cp_lexer_save_tokens (parser->lexer);
5820       /* Skip tokens until the next token is a closing parenthesis.
5821          If we find the closing `)', and the next token is a `{', then
5822          we are looking at a compound-literal.  */
5823       compound_literal_p
5824         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5825                                                   /*consume_paren=*/true)
5826            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5827       /* Roll back the tokens we skipped.  */
5828       cp_lexer_rollback_tokens (parser->lexer);
5829       /* If we were looking at a compound-literal, simulate an error
5830          so that the call to cp_parser_parse_definitely below will
5831          fail.  */
5832       if (compound_literal_p)
5833         cp_parser_simulate_error (parser);
5834       else
5835         {
5836           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5837           parser->in_type_id_in_expr_p = true;
5838           /* Look for the type-id.  */
5839           type = cp_parser_type_id (parser);
5840           /* Look for the closing `)'.  */
5841           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5842           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5843         }
5844
5845       /* Restore the saved message.  */
5846       parser->type_definition_forbidden_message = saved_message;
5847
5848       /* If ok so far, parse the dependent expression. We cannot be
5849          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5850          ctor of T, but looks like a cast to function returning T
5851          without a dependent expression.  */
5852       if (!cp_parser_error_occurred (parser))
5853         expr = cp_parser_cast_expression (parser,
5854                                           /*address_p=*/false,
5855                                           /*cast_p=*/true);
5856
5857       if (cp_parser_parse_definitely (parser))
5858         {
5859           /* Warn about old-style casts, if so requested.  */
5860           if (warn_old_style_cast
5861               && !in_system_header
5862               && !VOID_TYPE_P (type)
5863               && current_lang_name != lang_name_c)
5864             warning (OPT_Wold_style_cast, "use of old-style cast");
5865
5866           /* Only type conversions to integral or enumeration types
5867              can be used in constant-expressions.  */
5868           if (!cast_valid_in_integral_constant_expression_p (type)
5869               && (cp_parser_non_integral_constant_expression
5870                   (parser,
5871                    "a cast to a type other than an integral or "
5872                    "enumeration type")))
5873             return error_mark_node;
5874
5875           /* Perform the cast.  */
5876           expr = build_c_cast (type, expr);
5877           return expr;
5878         }
5879     }
5880
5881   /* If we get here, then it's not a cast, so it must be a
5882      unary-expression.  */
5883   return cp_parser_unary_expression (parser, address_p, cast_p);
5884 }
5885
5886 /* Parse a binary expression of the general form:
5887
5888    pm-expression:
5889      cast-expression
5890      pm-expression .* cast-expression
5891      pm-expression ->* cast-expression
5892
5893    multiplicative-expression:
5894      pm-expression
5895      multiplicative-expression * pm-expression
5896      multiplicative-expression / pm-expression
5897      multiplicative-expression % pm-expression
5898
5899    additive-expression:
5900      multiplicative-expression
5901      additive-expression + multiplicative-expression
5902      additive-expression - multiplicative-expression
5903
5904    shift-expression:
5905      additive-expression
5906      shift-expression << additive-expression
5907      shift-expression >> additive-expression
5908
5909    relational-expression:
5910      shift-expression
5911      relational-expression < shift-expression
5912      relational-expression > shift-expression
5913      relational-expression <= shift-expression
5914      relational-expression >= shift-expression
5915
5916   GNU Extension:
5917
5918    relational-expression:
5919      relational-expression <? shift-expression
5920      relational-expression >? shift-expression
5921
5922    equality-expression:
5923      relational-expression
5924      equality-expression == relational-expression
5925      equality-expression != relational-expression
5926
5927    and-expression:
5928      equality-expression
5929      and-expression & equality-expression
5930
5931    exclusive-or-expression:
5932      and-expression
5933      exclusive-or-expression ^ and-expression
5934
5935    inclusive-or-expression:
5936      exclusive-or-expression
5937      inclusive-or-expression | exclusive-or-expression
5938
5939    logical-and-expression:
5940      inclusive-or-expression
5941      logical-and-expression && inclusive-or-expression
5942
5943    logical-or-expression:
5944      logical-and-expression
5945      logical-or-expression || logical-and-expression
5946
5947    All these are implemented with a single function like:
5948
5949    binary-expression:
5950      simple-cast-expression
5951      binary-expression <token> binary-expression
5952
5953    CAST_P is true if this expression is the target of a cast.
5954
5955    The binops_by_token map is used to get the tree codes for each <token> type.
5956    binary-expressions are associated according to a precedence table.  */
5957
5958 #define TOKEN_PRECEDENCE(token)                              \
5959 (((token->type == CPP_GREATER                                \
5960    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5961   && !parser->greater_than_is_operator_p)                    \
5962  ? PREC_NOT_OPERATOR                                         \
5963  : binops_by_token[token->type].prec)
5964
5965 static tree
5966 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5967 {
5968   cp_parser_expression_stack stack;
5969   cp_parser_expression_stack_entry *sp = &stack[0];
5970   tree lhs, rhs;
5971   cp_token *token;
5972   enum tree_code tree_type, lhs_type, rhs_type;
5973   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5974   bool overloaded_p;
5975
5976   /* Parse the first expression.  */
5977   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5978   lhs_type = ERROR_MARK;
5979
5980   for (;;)
5981     {
5982       /* Get an operator token.  */
5983       token = cp_lexer_peek_token (parser->lexer);
5984
5985       if (warn_cxx0x_compat
5986           && token->type == CPP_RSHIFT
5987           && !parser->greater_than_is_operator_p)
5988         {
5989           warning (OPT_Wc__0x_compat, 
5990                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
5991                    &token->location);
5992           warning (OPT_Wc__0x_compat, 
5993                    "suggest parentheses around %<>>%> expression");
5994         }
5995
5996       new_prec = TOKEN_PRECEDENCE (token);
5997
5998       /* Popping an entry off the stack means we completed a subexpression:
5999          - either we found a token which is not an operator (`>' where it is not
6000            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6001            will happen repeatedly;
6002          - or, we found an operator which has lower priority.  This is the case
6003            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6004            parsing `3 * 4'.  */
6005       if (new_prec <= prec)
6006         {
6007           if (sp == stack)
6008             break;
6009           else
6010             goto pop;
6011         }
6012
6013      get_rhs:
6014       tree_type = binops_by_token[token->type].tree_type;
6015
6016       /* We used the operator token.  */
6017       cp_lexer_consume_token (parser->lexer);
6018
6019       /* Extract another operand.  It may be the RHS of this expression
6020          or the LHS of a new, higher priority expression.  */
6021       rhs = cp_parser_simple_cast_expression (parser);
6022       rhs_type = ERROR_MARK;
6023
6024       /* Get another operator token.  Look up its precedence to avoid
6025          building a useless (immediately popped) stack entry for common
6026          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6027       token = cp_lexer_peek_token (parser->lexer);
6028       lookahead_prec = TOKEN_PRECEDENCE (token);
6029       if (lookahead_prec > new_prec)
6030         {
6031           /* ... and prepare to parse the RHS of the new, higher priority
6032              expression.  Since precedence levels on the stack are
6033              monotonically increasing, we do not have to care about
6034              stack overflows.  */
6035           sp->prec = prec;
6036           sp->tree_type = tree_type;
6037           sp->lhs = lhs;
6038           sp->lhs_type = lhs_type;
6039           sp++;
6040           lhs = rhs;
6041           lhs_type = rhs_type;
6042           prec = new_prec;
6043           new_prec = lookahead_prec;
6044           goto get_rhs;
6045
6046          pop:
6047           /* If the stack is not empty, we have parsed into LHS the right side
6048              (`4' in the example above) of an expression we had suspended.
6049              We can use the information on the stack to recover the LHS (`3')
6050              from the stack together with the tree code (`MULT_EXPR'), and
6051              the precedence of the higher level subexpression
6052              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6053              which will be used to actually build the additive expression.  */
6054           --sp;
6055           prec = sp->prec;
6056           tree_type = sp->tree_type;
6057           rhs = lhs;
6058           rhs_type = lhs_type;
6059           lhs = sp->lhs;
6060           lhs_type = sp->lhs_type;
6061         }
6062
6063       overloaded_p = false;
6064       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6065                                &overloaded_p);
6066       lhs_type = tree_type;
6067
6068       /* If the binary operator required the use of an overloaded operator,
6069          then this expression cannot be an integral constant-expression.
6070          An overloaded operator can be used even if both operands are
6071          otherwise permissible in an integral constant-expression if at
6072          least one of the operands is of enumeration type.  */
6073
6074       if (overloaded_p
6075           && (cp_parser_non_integral_constant_expression
6076               (parser, "calls to overloaded operators")))
6077         return error_mark_node;
6078     }
6079
6080   return lhs;
6081 }
6082
6083
6084 /* Parse the `? expression : assignment-expression' part of a
6085    conditional-expression.  The LOGICAL_OR_EXPR is the
6086    logical-or-expression that started the conditional-expression.
6087    Returns a representation of the entire conditional-expression.
6088
6089    This routine is used by cp_parser_assignment_expression.
6090
6091      ? expression : assignment-expression
6092
6093    GNU Extensions:
6094
6095      ? : assignment-expression */
6096
6097 static tree
6098 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6099 {
6100   tree expr;
6101   tree assignment_expr;
6102
6103   /* Consume the `?' token.  */
6104   cp_lexer_consume_token (parser->lexer);
6105   if (cp_parser_allow_gnu_extensions_p (parser)
6106       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6107     /* Implicit true clause.  */
6108     expr = NULL_TREE;
6109   else
6110     /* Parse the expression.  */
6111     expr = cp_parser_expression (parser, /*cast_p=*/false);
6112
6113   /* The next token should be a `:'.  */
6114   cp_parser_require (parser, CPP_COLON, "`:'");
6115   /* Parse the assignment-expression.  */
6116   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6117
6118   /* Build the conditional-expression.  */
6119   return build_x_conditional_expr (logical_or_expr,
6120                                    expr,
6121                                    assignment_expr);
6122 }
6123
6124 /* Parse an assignment-expression.
6125
6126    assignment-expression:
6127      conditional-expression
6128      logical-or-expression assignment-operator assignment_expression
6129      throw-expression
6130
6131    CAST_P is true if this expression is the target of a cast.
6132
6133    Returns a representation for the expression.  */
6134
6135 static tree
6136 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6137 {
6138   tree expr;
6139
6140   /* If the next token is the `throw' keyword, then we're looking at
6141      a throw-expression.  */
6142   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6143     expr = cp_parser_throw_expression (parser);
6144   /* Otherwise, it must be that we are looking at a
6145      logical-or-expression.  */
6146   else
6147     {
6148       /* Parse the binary expressions (logical-or-expression).  */
6149       expr = cp_parser_binary_expression (parser, cast_p);
6150       /* If the next token is a `?' then we're actually looking at a
6151          conditional-expression.  */
6152       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6153         return cp_parser_question_colon_clause (parser, expr);
6154       else
6155         {
6156           enum tree_code assignment_operator;
6157
6158           /* If it's an assignment-operator, we're using the second
6159              production.  */
6160           assignment_operator
6161             = cp_parser_assignment_operator_opt (parser);
6162           if (assignment_operator != ERROR_MARK)
6163             {
6164               tree rhs;
6165
6166               /* Parse the right-hand side of the assignment.  */
6167               rhs = cp_parser_assignment_expression (parser, cast_p);
6168               /* An assignment may not appear in a
6169                  constant-expression.  */
6170               if (cp_parser_non_integral_constant_expression (parser,
6171                                                               "an assignment"))
6172                 return error_mark_node;
6173               /* Build the assignment expression.  */
6174               expr = build_x_modify_expr (expr,
6175                                           assignment_operator,
6176                                           rhs);
6177             }
6178         }
6179     }
6180
6181   return expr;
6182 }
6183
6184 /* Parse an (optional) assignment-operator.
6185
6186    assignment-operator: one of
6187      = *= /= %= += -= >>= <<= &= ^= |=
6188
6189    GNU Extension:
6190
6191    assignment-operator: one of
6192      <?= >?=
6193
6194    If the next token is an assignment operator, the corresponding tree
6195    code is returned, and the token is consumed.  For example, for
6196    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6197    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6198    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6199    operator, ERROR_MARK is returned.  */
6200
6201 static enum tree_code
6202 cp_parser_assignment_operator_opt (cp_parser* parser)
6203 {
6204   enum tree_code op;
6205   cp_token *token;
6206
6207   /* Peek at the next toen.  */
6208   token = cp_lexer_peek_token (parser->lexer);
6209
6210   switch (token->type)
6211     {
6212     case CPP_EQ:
6213       op = NOP_EXPR;
6214       break;
6215
6216     case CPP_MULT_EQ:
6217       op = MULT_EXPR;
6218       break;
6219
6220     case CPP_DIV_EQ:
6221       op = TRUNC_DIV_EXPR;
6222       break;
6223
6224     case CPP_MOD_EQ:
6225       op = TRUNC_MOD_EXPR;
6226       break;
6227
6228     case CPP_PLUS_EQ:
6229       op = PLUS_EXPR;
6230       break;
6231
6232     case CPP_MINUS_EQ:
6233       op = MINUS_EXPR;
6234       break;
6235
6236     case CPP_RSHIFT_EQ:
6237       op = RSHIFT_EXPR;
6238       break;
6239
6240     case CPP_LSHIFT_EQ:
6241       op = LSHIFT_EXPR;
6242       break;
6243
6244     case CPP_AND_EQ:
6245       op = BIT_AND_EXPR;
6246       break;
6247
6248     case CPP_XOR_EQ:
6249       op = BIT_XOR_EXPR;
6250       break;
6251
6252     case CPP_OR_EQ:
6253       op = BIT_IOR_EXPR;
6254       break;
6255
6256     default:
6257       /* Nothing else is an assignment operator.  */
6258       op = ERROR_MARK;
6259     }
6260
6261   /* If it was an assignment operator, consume it.  */
6262   if (op != ERROR_MARK)
6263     cp_lexer_consume_token (parser->lexer);
6264
6265   return op;
6266 }
6267
6268 /* Parse an expression.
6269
6270    expression:
6271      assignment-expression
6272      expression , assignment-expression
6273
6274    CAST_P is true if this expression is the target of a cast.
6275
6276    Returns a representation of the expression.  */
6277
6278 static tree
6279 cp_parser_expression (cp_parser* parser, bool cast_p)
6280 {
6281   tree expression = NULL_TREE;
6282
6283   while (true)
6284     {
6285       tree assignment_expression;
6286
6287       /* Parse the next assignment-expression.  */
6288       assignment_expression
6289         = cp_parser_assignment_expression (parser, cast_p);
6290       /* If this is the first assignment-expression, we can just
6291          save it away.  */
6292       if (!expression)
6293         expression = assignment_expression;
6294       else
6295         expression = build_x_compound_expr (expression,
6296                                             assignment_expression);
6297       /* If the next token is not a comma, then we are done with the
6298          expression.  */
6299       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6300         break;
6301       /* Consume the `,'.  */
6302       cp_lexer_consume_token (parser->lexer);
6303       /* A comma operator cannot appear in a constant-expression.  */
6304       if (cp_parser_non_integral_constant_expression (parser,
6305                                                       "a comma operator"))
6306         expression = error_mark_node;
6307     }
6308
6309   return expression;
6310 }
6311
6312 /* Parse a constant-expression.
6313
6314    constant-expression:
6315      conditional-expression
6316
6317   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6318   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6319   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6320   is false, NON_CONSTANT_P should be NULL.  */
6321
6322 static tree
6323 cp_parser_constant_expression (cp_parser* parser,
6324                                bool allow_non_constant_p,
6325                                bool *non_constant_p)
6326 {
6327   bool saved_integral_constant_expression_p;
6328   bool saved_allow_non_integral_constant_expression_p;
6329   bool saved_non_integral_constant_expression_p;
6330   tree expression;
6331
6332   /* It might seem that we could simply parse the
6333      conditional-expression, and then check to see if it were
6334      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6335      one that the compiler can figure out is constant, possibly after
6336      doing some simplifications or optimizations.  The standard has a
6337      precise definition of constant-expression, and we must honor
6338      that, even though it is somewhat more restrictive.
6339
6340      For example:
6341
6342        int i[(2, 3)];
6343
6344      is not a legal declaration, because `(2, 3)' is not a
6345      constant-expression.  The `,' operator is forbidden in a
6346      constant-expression.  However, GCC's constant-folding machinery
6347      will fold this operation to an INTEGER_CST for `3'.  */
6348
6349   /* Save the old settings.  */
6350   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6351   saved_allow_non_integral_constant_expression_p
6352     = parser->allow_non_integral_constant_expression_p;
6353   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6354   /* We are now parsing a constant-expression.  */
6355   parser->integral_constant_expression_p = true;
6356   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6357   parser->non_integral_constant_expression_p = false;
6358   /* Although the grammar says "conditional-expression", we parse an
6359      "assignment-expression", which also permits "throw-expression"
6360      and the use of assignment operators.  In the case that
6361      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6362      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6363      actually essential that we look for an assignment-expression.
6364      For example, cp_parser_initializer_clauses uses this function to
6365      determine whether a particular assignment-expression is in fact
6366      constant.  */
6367   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6368   /* Restore the old settings.  */
6369   parser->integral_constant_expression_p
6370     = saved_integral_constant_expression_p;
6371   parser->allow_non_integral_constant_expression_p
6372     = saved_allow_non_integral_constant_expression_p;
6373   if (allow_non_constant_p)
6374     *non_constant_p = parser->non_integral_constant_expression_p;
6375   else if (parser->non_integral_constant_expression_p)
6376     expression = error_mark_node;
6377   parser->non_integral_constant_expression_p
6378     = saved_non_integral_constant_expression_p;
6379
6380   return expression;
6381 }
6382
6383 /* Parse __builtin_offsetof.
6384
6385    offsetof-expression:
6386      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6387
6388    offsetof-member-designator:
6389      id-expression
6390      | offsetof-member-designator "." id-expression
6391      | offsetof-member-designator "[" expression "]"  */
6392
6393 static tree
6394 cp_parser_builtin_offsetof (cp_parser *parser)
6395 {
6396   int save_ice_p, save_non_ice_p;
6397   tree type, expr;
6398   cp_id_kind dummy;
6399
6400   /* We're about to accept non-integral-constant things, but will
6401      definitely yield an integral constant expression.  Save and
6402      restore these values around our local parsing.  */
6403   save_ice_p = parser->integral_constant_expression_p;
6404   save_non_ice_p = parser->non_integral_constant_expression_p;
6405
6406   /* Consume the "__builtin_offsetof" token.  */
6407   cp_lexer_consume_token (parser->lexer);
6408   /* Consume the opening `('.  */
6409   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6410   /* Parse the type-id.  */
6411   type = cp_parser_type_id (parser);
6412   /* Look for the `,'.  */
6413   cp_parser_require (parser, CPP_COMMA, "`,'");
6414
6415   /* Build the (type *)null that begins the traditional offsetof macro.  */
6416   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6417
6418   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6419   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6420                                                  true, &dummy);
6421   while (true)
6422     {
6423       cp_token *token = cp_lexer_peek_token (parser->lexer);
6424       switch (token->type)
6425         {
6426         case CPP_OPEN_SQUARE:
6427           /* offsetof-member-designator "[" expression "]" */
6428           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6429           break;
6430
6431         case CPP_DOT:
6432           /* offsetof-member-designator "." identifier */
6433           cp_lexer_consume_token (parser->lexer);
6434           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6435                                                          true, &dummy);
6436           break;
6437
6438         case CPP_CLOSE_PAREN:
6439           /* Consume the ")" token.  */
6440           cp_lexer_consume_token (parser->lexer);
6441           goto success;
6442
6443         default:
6444           /* Error.  We know the following require will fail, but
6445              that gives the proper error message.  */
6446           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6447           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6448           expr = error_mark_node;
6449           goto failure;
6450         }
6451     }
6452
6453  success:
6454   /* If we're processing a template, we can't finish the semantics yet.
6455      Otherwise we can fold the entire expression now.  */
6456   if (processing_template_decl)
6457     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6458   else
6459     expr = finish_offsetof (expr);
6460
6461  failure:
6462   parser->integral_constant_expression_p = save_ice_p;
6463   parser->non_integral_constant_expression_p = save_non_ice_p;
6464
6465   return expr;
6466 }
6467
6468 /* Parse a trait expression.  */
6469
6470 static tree
6471 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6472 {
6473   cp_trait_kind kind;
6474   tree type1, type2 = NULL_TREE;
6475   bool binary = false;
6476   cp_decl_specifier_seq decl_specs;
6477
6478   switch (keyword)
6479     {
6480     case RID_HAS_NOTHROW_ASSIGN:
6481       kind = CPTK_HAS_NOTHROW_ASSIGN;
6482       break;
6483     case RID_HAS_NOTHROW_CONSTRUCTOR:
6484       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6485       break;
6486     case RID_HAS_NOTHROW_COPY:
6487       kind = CPTK_HAS_NOTHROW_COPY;
6488       break;
6489     case RID_HAS_TRIVIAL_ASSIGN:
6490       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6491       break;
6492     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6493       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6494       break;
6495     case RID_HAS_TRIVIAL_COPY:
6496       kind = CPTK_HAS_TRIVIAL_COPY;
6497       break;
6498     case RID_HAS_TRIVIAL_DESTRUCTOR:
6499       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6500       break;
6501     case RID_HAS_VIRTUAL_DESTRUCTOR:
6502       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6503       break;
6504     case RID_IS_ABSTRACT:
6505       kind = CPTK_IS_ABSTRACT;
6506       break;
6507     case RID_IS_BASE_OF:
6508       kind = CPTK_IS_BASE_OF;
6509       binary = true;
6510       break;
6511     case RID_IS_CLASS:
6512       kind = CPTK_IS_CLASS;
6513       break;
6514     case RID_IS_CONVERTIBLE_TO:
6515       kind = CPTK_IS_CONVERTIBLE_TO;
6516       binary = true;
6517       break;
6518     case RID_IS_EMPTY:
6519       kind = CPTK_IS_EMPTY;
6520       break;
6521     case RID_IS_ENUM:
6522       kind = CPTK_IS_ENUM;
6523       break;
6524     case RID_IS_POD:
6525       kind = CPTK_IS_POD;
6526       break;
6527     case RID_IS_POLYMORPHIC:
6528       kind = CPTK_IS_POLYMORPHIC;
6529       break;
6530     case RID_IS_UNION:
6531       kind = CPTK_IS_UNION;
6532       break;
6533     default:
6534       gcc_unreachable ();
6535     }
6536
6537   /* Consume the token.  */
6538   cp_lexer_consume_token (parser->lexer);
6539
6540   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6541
6542   type1 = cp_parser_type_id (parser);
6543
6544   if (type1 == error_mark_node)
6545     return error_mark_node;
6546
6547   /* Build a trivial decl-specifier-seq.  */
6548   clear_decl_specs (&decl_specs);
6549   decl_specs.type = type1;
6550
6551   /* Call grokdeclarator to figure out what type this is.  */
6552   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6553                           /*initialized=*/0, /*attrlist=*/NULL);
6554
6555   if (binary)
6556     {
6557       cp_parser_require (parser, CPP_COMMA, "`,'");
6558  
6559       type2 = cp_parser_type_id (parser);
6560
6561       if (type2 == error_mark_node)
6562         return error_mark_node;
6563
6564       /* Build a trivial decl-specifier-seq.  */
6565       clear_decl_specs (&decl_specs);
6566       decl_specs.type = type2;
6567
6568       /* Call grokdeclarator to figure out what type this is.  */
6569       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6570                               /*initialized=*/0, /*attrlist=*/NULL);
6571     }
6572
6573   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6574
6575   /* Complete the trait expression, which may mean either processing
6576      the trait expr now or saving it for template instantiation.  */
6577   return finish_trait_expr (kind, type1, type2);
6578 }
6579
6580 /* Statements [gram.stmt.stmt]  */
6581
6582 /* Parse a statement.
6583
6584    statement:
6585      labeled-statement
6586      expression-statement
6587      compound-statement
6588      selection-statement
6589      iteration-statement
6590      jump-statement
6591      declaration-statement
6592      try-block
6593
6594   IN_COMPOUND is true when the statement is nested inside a
6595   cp_parser_compound_statement; this matters for certain pragmas.
6596
6597   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6598   is a (possibly labeled) if statement which is not enclosed in braces
6599   and has an else clause.  This is used to implement -Wparentheses.  */
6600
6601 static void
6602 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6603                      bool in_compound, bool *if_p)
6604 {
6605   tree statement;
6606   cp_token *token;
6607   location_t statement_location;
6608
6609  restart:
6610   if (if_p != NULL)
6611     *if_p = false;
6612   /* There is no statement yet.  */
6613   statement = NULL_TREE;
6614   /* Peek at the next token.  */
6615   token = cp_lexer_peek_token (parser->lexer);
6616   /* Remember the location of the first token in the statement.  */
6617   statement_location = token->location;
6618   /* If this is a keyword, then that will often determine what kind of
6619      statement we have.  */
6620   if (token->type == CPP_KEYWORD)
6621     {
6622       enum rid keyword = token->keyword;
6623
6624       switch (keyword)
6625         {
6626         case RID_CASE:
6627         case RID_DEFAULT:
6628           /* Looks like a labeled-statement with a case label.
6629              Parse the label, and then use tail recursion to parse
6630              the statement.  */
6631           cp_parser_label_for_labeled_statement (parser);
6632           goto restart;
6633
6634         case RID_IF:
6635         case RID_SWITCH:
6636           statement = cp_parser_selection_statement (parser, if_p);
6637           break;
6638
6639         case RID_WHILE:
6640         case RID_DO:
6641         case RID_FOR:
6642           statement = cp_parser_iteration_statement (parser);
6643           break;
6644
6645         case RID_BREAK:
6646         case RID_CONTINUE:
6647         case RID_RETURN:
6648         case RID_GOTO:
6649           statement = cp_parser_jump_statement (parser);
6650           break;
6651
6652           /* Objective-C++ exception-handling constructs.  */
6653         case RID_AT_TRY:
6654         case RID_AT_CATCH:
6655         case RID_AT_FINALLY:
6656         case RID_AT_SYNCHRONIZED:
6657         case RID_AT_THROW:
6658           statement = cp_parser_objc_statement (parser);
6659           break;
6660
6661         case RID_TRY:
6662           statement = cp_parser_try_block (parser);
6663           break;
6664
6665         case RID_NAMESPACE:
6666           /* This must be a namespace alias definition.  */
6667           cp_parser_declaration_statement (parser);
6668           return;
6669           
6670         default:
6671           /* It might be a keyword like `int' that can start a
6672              declaration-statement.  */
6673           break;
6674         }
6675     }
6676   else if (token->type == CPP_NAME)
6677     {
6678       /* If the next token is a `:', then we are looking at a
6679          labeled-statement.  */
6680       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6681       if (token->type == CPP_COLON)
6682         {
6683           /* Looks like a labeled-statement with an ordinary label.
6684              Parse the label, and then use tail recursion to parse
6685              the statement.  */
6686           cp_parser_label_for_labeled_statement (parser);
6687           goto restart;
6688         }
6689     }
6690   /* Anything that starts with a `{' must be a compound-statement.  */
6691   else if (token->type == CPP_OPEN_BRACE)
6692     statement = cp_parser_compound_statement (parser, NULL, false);
6693   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6694      a statement all its own.  */
6695   else if (token->type == CPP_PRAGMA)
6696     {
6697       /* Only certain OpenMP pragmas are attached to statements, and thus
6698          are considered statements themselves.  All others are not.  In
6699          the context of a compound, accept the pragma as a "statement" and
6700          return so that we can check for a close brace.  Otherwise we
6701          require a real statement and must go back and read one.  */
6702       if (in_compound)
6703         cp_parser_pragma (parser, pragma_compound);
6704       else if (!cp_parser_pragma (parser, pragma_stmt))
6705         goto restart;
6706       return;
6707     }
6708   else if (token->type == CPP_EOF)
6709     {
6710       cp_parser_error (parser, "expected statement");
6711       return;
6712     }
6713
6714   /* Everything else must be a declaration-statement or an
6715      expression-statement.  Try for the declaration-statement
6716      first, unless we are looking at a `;', in which case we know that
6717      we have an expression-statement.  */
6718   if (!statement)
6719     {
6720       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6721         {
6722           cp_parser_parse_tentatively (parser);
6723           /* Try to parse the declaration-statement.  */
6724           cp_parser_declaration_statement (parser);
6725           /* If that worked, we're done.  */
6726           if (cp_parser_parse_definitely (parser))
6727             return;
6728         }
6729       /* Look for an expression-statement instead.  */
6730       statement = cp_parser_expression_statement (parser, in_statement_expr);
6731     }
6732
6733   /* Set the line number for the statement.  */
6734   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6735     SET_EXPR_LOCATION (statement, statement_location);
6736 }
6737
6738 /* Parse the label for a labeled-statement, i.e.
6739
6740    identifier :
6741    case constant-expression :
6742    default :
6743
6744    GNU Extension:
6745    case constant-expression ... constant-expression : statement
6746
6747    When a label is parsed without errors, the label is added to the
6748    parse tree by the finish_* functions, so this function doesn't
6749    have to return the label.  */
6750
6751 static void
6752 cp_parser_label_for_labeled_statement (cp_parser* parser)
6753 {
6754   cp_token *token;
6755
6756   /* The next token should be an identifier.  */
6757   token = cp_lexer_peek_token (parser->lexer);
6758   if (token->type != CPP_NAME
6759       && token->type != CPP_KEYWORD)
6760     {
6761       cp_parser_error (parser, "expected labeled-statement");
6762       return;
6763     }
6764
6765   switch (token->keyword)
6766     {
6767     case RID_CASE:
6768       {
6769         tree expr, expr_hi;
6770         cp_token *ellipsis;
6771
6772         /* Consume the `case' token.  */
6773         cp_lexer_consume_token (parser->lexer);
6774         /* Parse the constant-expression.  */
6775         expr = cp_parser_constant_expression (parser,
6776                                               /*allow_non_constant_p=*/false,
6777                                               NULL);
6778
6779         ellipsis = cp_lexer_peek_token (parser->lexer);
6780         if (ellipsis->type == CPP_ELLIPSIS)
6781           {
6782             /* Consume the `...' token.  */
6783             cp_lexer_consume_token (parser->lexer);
6784             expr_hi =
6785               cp_parser_constant_expression (parser,
6786                                              /*allow_non_constant_p=*/false,
6787                                              NULL);
6788             /* We don't need to emit warnings here, as the common code
6789                will do this for us.  */
6790           }
6791         else
6792           expr_hi = NULL_TREE;
6793
6794         if (parser->in_switch_statement_p)
6795           finish_case_label (expr, expr_hi);
6796         else
6797           error ("case label %qE not within a switch statement", expr);
6798       }
6799       break;
6800
6801     case RID_DEFAULT:
6802       /* Consume the `default' token.  */
6803       cp_lexer_consume_token (parser->lexer);
6804
6805       if (parser->in_switch_statement_p)
6806         finish_case_label (NULL_TREE, NULL_TREE);
6807       else
6808         error ("case label not within a switch statement");
6809       break;
6810
6811     default:
6812       /* Anything else must be an ordinary label.  */
6813       finish_label_stmt (cp_parser_identifier (parser));
6814       break;
6815     }
6816
6817   /* Require the `:' token.  */
6818   cp_parser_require (parser, CPP_COLON, "`:'");
6819 }
6820
6821 /* Parse an expression-statement.
6822
6823    expression-statement:
6824      expression [opt] ;
6825
6826    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6827    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6828    indicates whether this expression-statement is part of an
6829    expression statement.  */
6830
6831 static tree
6832 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6833 {
6834   tree statement = NULL_TREE;
6835
6836   /* If the next token is a ';', then there is no expression
6837      statement.  */
6838   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6839     statement = cp_parser_expression (parser, /*cast_p=*/false);
6840
6841   /* Consume the final `;'.  */
6842   cp_parser_consume_semicolon_at_end_of_statement (parser);
6843
6844   if (in_statement_expr
6845       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6846     /* This is the final expression statement of a statement
6847        expression.  */
6848     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6849   else if (statement)
6850     statement = finish_expr_stmt (statement);
6851   else
6852     finish_stmt ();
6853
6854   return statement;
6855 }
6856
6857 /* Parse a compound-statement.
6858
6859    compound-statement:
6860      { statement-seq [opt] }
6861
6862    GNU extension:
6863
6864    compound-statement:
6865      { label-declaration-seq [opt] statement-seq [opt] }
6866
6867    label-declaration-seq:
6868      label-declaration
6869      label-declaration-seq label-declaration
6870
6871    Returns a tree representing the statement.  */
6872
6873 static tree
6874 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6875                               bool in_try)
6876 {
6877   tree compound_stmt;
6878
6879   /* Consume the `{'.  */
6880   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6881     return error_mark_node;
6882   /* Begin the compound-statement.  */
6883   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6884   /* If the next keyword is `__label__' we have a label declaration.  */
6885   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6886     cp_parser_label_declaration (parser);
6887   /* Parse an (optional) statement-seq.  */
6888   cp_parser_statement_seq_opt (parser, in_statement_expr);
6889   /* Finish the compound-statement.  */
6890   finish_compound_stmt (compound_stmt);
6891   /* Consume the `}'.  */
6892   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6893
6894   return compound_stmt;
6895 }
6896
6897 /* Parse an (optional) statement-seq.
6898
6899    statement-seq:
6900      statement
6901      statement-seq [opt] statement  */
6902
6903 static void
6904 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6905 {
6906   /* Scan statements until there aren't any more.  */
6907   while (true)
6908     {
6909       cp_token *token = cp_lexer_peek_token (parser->lexer);
6910
6911       /* If we're looking at a `}', then we've run out of statements.  */
6912       if (token->type == CPP_CLOSE_BRACE
6913           || token->type == CPP_EOF
6914           || token->type == CPP_PRAGMA_EOL)
6915         break;
6916       
6917       /* If we are in a compound statement and find 'else' then
6918          something went wrong.  */
6919       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6920         {
6921           if (parser->in_statement & IN_IF_STMT) 
6922             break;
6923           else
6924             {
6925               token = cp_lexer_consume_token (parser->lexer);
6926               error ("%<else%> without a previous %<if%>");
6927             }
6928         }
6929
6930       /* Parse the statement.  */
6931       cp_parser_statement (parser, in_statement_expr, true, NULL);
6932     }
6933 }
6934
6935 /* Parse a selection-statement.
6936
6937    selection-statement:
6938      if ( condition ) statement
6939      if ( condition ) statement else statement
6940      switch ( condition ) statement
6941
6942    Returns the new IF_STMT or SWITCH_STMT.
6943
6944    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6945    is a (possibly labeled) if statement which is not enclosed in
6946    braces and has an else clause.  This is used to implement
6947    -Wparentheses.  */
6948
6949 static tree
6950 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6951 {
6952   cp_token *token;
6953   enum rid keyword;
6954
6955   if (if_p != NULL)
6956     *if_p = false;
6957
6958   /* Peek at the next token.  */
6959   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6960
6961   /* See what kind of keyword it is.  */
6962   keyword = token->keyword;
6963   switch (keyword)
6964     {
6965     case RID_IF:
6966     case RID_SWITCH:
6967       {
6968         tree statement;
6969         tree condition;
6970
6971         /* Look for the `('.  */
6972         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6973           {
6974             cp_parser_skip_to_end_of_statement (parser);
6975             return error_mark_node;
6976           }
6977
6978         /* Begin the selection-statement.  */
6979         if (keyword == RID_IF)
6980           statement = begin_if_stmt ();
6981         else
6982           statement = begin_switch_stmt ();
6983
6984         /* Parse the condition.  */
6985         condition = cp_parser_condition (parser);
6986         /* Look for the `)'.  */
6987         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6988           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6989                                                  /*consume_paren=*/true);
6990
6991         if (keyword == RID_IF)
6992           {
6993             bool nested_if;
6994             unsigned char in_statement;
6995
6996             /* Add the condition.  */
6997             finish_if_stmt_cond (condition, statement);
6998
6999             /* Parse the then-clause.  */
7000             in_statement = parser->in_statement;
7001             parser->in_statement |= IN_IF_STMT;
7002             cp_parser_implicitly_scoped_statement (parser, &nested_if);
7003             parser->in_statement = in_statement;
7004
7005             finish_then_clause (statement);
7006
7007             /* If the next token is `else', parse the else-clause.  */
7008             if (cp_lexer_next_token_is_keyword (parser->lexer,
7009                                                 RID_ELSE))
7010               {
7011                 /* Consume the `else' keyword.  */
7012                 cp_lexer_consume_token (parser->lexer);
7013                 begin_else_clause (statement);
7014                 /* Parse the else-clause.  */
7015                 cp_parser_implicitly_scoped_statement (parser, NULL);
7016                 finish_else_clause (statement);
7017
7018                 /* If we are currently parsing a then-clause, then
7019                    IF_P will not be NULL.  We set it to true to
7020                    indicate that this if statement has an else clause.
7021                    This may trigger the Wparentheses warning below
7022                    when we get back up to the parent if statement.  */
7023                 if (if_p != NULL)
7024                   *if_p = true;
7025               }
7026             else
7027               {
7028                 /* This if statement does not have an else clause.  If
7029                    NESTED_IF is true, then the then-clause is an if
7030                    statement which does have an else clause.  We warn
7031                    about the potential ambiguity.  */
7032                 if (nested_if)
7033                   warning (OPT_Wparentheses,
7034                            ("%Hsuggest explicit braces "
7035                             "to avoid ambiguous %<else%>"),
7036                            EXPR_LOCUS (statement));
7037               }
7038
7039             /* Now we're all done with the if-statement.  */
7040             finish_if_stmt (statement);
7041           }
7042         else
7043           {
7044             bool in_switch_statement_p;
7045             unsigned char in_statement;
7046
7047             /* Add the condition.  */
7048             finish_switch_cond (condition, statement);
7049
7050             /* Parse the body of the switch-statement.  */
7051             in_switch_statement_p = parser->in_switch_statement_p;
7052             in_statement = parser->in_statement;
7053             parser->in_switch_statement_p = true;
7054             parser->in_statement |= IN_SWITCH_STMT;
7055             cp_parser_implicitly_scoped_statement (parser, NULL);
7056             parser->in_switch_statement_p = in_switch_statement_p;
7057             parser->in_statement = in_statement;
7058
7059             /* Now we're all done with the switch-statement.  */
7060             finish_switch_stmt (statement);
7061           }
7062
7063         return statement;
7064       }
7065       break;
7066
7067     default:
7068       cp_parser_error (parser, "expected selection-statement");
7069       return error_mark_node;
7070     }
7071 }
7072
7073 /* Parse a condition.
7074
7075    condition:
7076      expression
7077      type-specifier-seq declarator = assignment-expression
7078
7079    GNU Extension:
7080
7081    condition:
7082      type-specifier-seq declarator asm-specification [opt]
7083        attributes [opt] = assignment-expression
7084
7085    Returns the expression that should be tested.  */
7086
7087 static tree
7088 cp_parser_condition (cp_parser* parser)
7089 {
7090   cp_decl_specifier_seq type_specifiers;
7091   const char *saved_message;
7092
7093   /* Try the declaration first.  */
7094   cp_parser_parse_tentatively (parser);
7095   /* New types are not allowed in the type-specifier-seq for a
7096      condition.  */
7097   saved_message = parser->type_definition_forbidden_message;
7098   parser->type_definition_forbidden_message
7099     = "types may not be defined in conditions";
7100   /* Parse the type-specifier-seq.  */
7101   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7102                                 &type_specifiers);
7103   /* Restore the saved message.  */
7104   parser->type_definition_forbidden_message = saved_message;
7105   /* If all is well, we might be looking at a declaration.  */
7106   if (!cp_parser_error_occurred (parser))
7107     {
7108       tree decl;
7109       tree asm_specification;
7110       tree attributes;
7111       cp_declarator *declarator;
7112       tree initializer = NULL_TREE;
7113
7114       /* Parse the declarator.  */
7115       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7116                                          /*ctor_dtor_or_conv_p=*/NULL,
7117                                          /*parenthesized_p=*/NULL,
7118                                          /*member_p=*/false);
7119       /* Parse the attributes.  */
7120       attributes = cp_parser_attributes_opt (parser);
7121       /* Parse the asm-specification.  */
7122       asm_specification = cp_parser_asm_specification_opt (parser);
7123       /* If the next token is not an `=', then we might still be
7124          looking at an expression.  For example:
7125
7126            if (A(a).x)
7127
7128          looks like a decl-specifier-seq and a declarator -- but then
7129          there is no `=', so this is an expression.  */
7130       cp_parser_require (parser, CPP_EQ, "`='");
7131       /* If we did see an `=', then we are looking at a declaration
7132          for sure.  */
7133       if (cp_parser_parse_definitely (parser))
7134         {
7135           tree pushed_scope;
7136           bool non_constant_p;
7137
7138           /* Create the declaration.  */
7139           decl = start_decl (declarator, &type_specifiers,
7140                              /*initialized_p=*/true,
7141                              attributes, /*prefix_attributes=*/NULL_TREE,
7142                              &pushed_scope);
7143           /* Parse the assignment-expression.  */
7144           initializer
7145             = cp_parser_constant_expression (parser,
7146                                              /*allow_non_constant_p=*/true,
7147                                              &non_constant_p);
7148           if (!non_constant_p)
7149             initializer = fold_non_dependent_expr (initializer);
7150
7151           /* Process the initializer.  */
7152           cp_finish_decl (decl,
7153                           initializer, !non_constant_p,
7154                           asm_specification,
7155                           LOOKUP_ONLYCONVERTING);
7156
7157           if (pushed_scope)
7158             pop_scope (pushed_scope);
7159
7160           return convert_from_reference (decl);
7161         }
7162     }
7163   /* If we didn't even get past the declarator successfully, we are
7164      definitely not looking at a declaration.  */
7165   else
7166     cp_parser_abort_tentative_parse (parser);
7167
7168   /* Otherwise, we are looking at an expression.  */
7169   return cp_parser_expression (parser, /*cast_p=*/false);
7170 }
7171
7172 /* We check for a ) immediately followed by ; with no whitespacing
7173    between.  This is used to issue a warning for:
7174
7175      while (...);
7176
7177    and:
7178
7179      for (...);
7180
7181    as the semicolon is probably extraneous.
7182
7183    On parse errors, the next token might not be a ), so do nothing in
7184    that case. */
7185
7186 static void
7187 check_empty_body (cp_parser* parser, const char* type)
7188 {
7189   cp_token *token;
7190   cp_token *close_paren;
7191   expanded_location close_loc;
7192   expanded_location semi_loc;
7193   
7194   close_paren = cp_lexer_peek_token (parser->lexer);
7195   if (close_paren->type != CPP_CLOSE_PAREN)
7196     return;
7197
7198   close_loc = expand_location (close_paren->location);
7199   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7200
7201   if (token->type != CPP_SEMICOLON
7202       || (token->flags & PREV_WHITE))
7203     return;
7204
7205   semi_loc =  expand_location (token->location);
7206   if (close_loc.line == semi_loc.line
7207 #ifdef USE_MAPPED_LOCATION
7208       && close_loc.column+1 == semi_loc.column
7209 #endif
7210       )
7211     warning (OPT_Wempty_body,
7212              "suggest a space before %<;%> or explicit braces around empty "
7213              "body in %<%s%> statement",
7214              type);
7215 }
7216
7217 /* Parse an iteration-statement.
7218
7219    iteration-statement:
7220      while ( condition ) statement
7221      do statement while ( expression ) ;
7222      for ( for-init-statement condition [opt] ; expression [opt] )
7223        statement
7224
7225    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7226
7227 static tree
7228 cp_parser_iteration_statement (cp_parser* parser)
7229 {
7230   cp_token *token;
7231   enum rid keyword;
7232   tree statement;
7233   unsigned char in_statement;
7234
7235   /* Peek at the next token.  */
7236   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7237   if (!token)
7238     return error_mark_node;
7239
7240   /* Remember whether or not we are already within an iteration
7241      statement.  */
7242   in_statement = parser->in_statement;
7243
7244   /* See what kind of keyword it is.  */
7245   keyword = token->keyword;
7246   switch (keyword)
7247     {
7248     case RID_WHILE:
7249       {
7250         tree condition;
7251
7252         /* Begin the while-statement.  */
7253         statement = begin_while_stmt ();
7254         /* Look for the `('.  */
7255         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7256         /* Parse the condition.  */
7257         condition = cp_parser_condition (parser);
7258         finish_while_stmt_cond (condition, statement);
7259         check_empty_body (parser, "while");
7260         /* Look for the `)'.  */
7261         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7262         /* Parse the dependent statement.  */
7263         parser->in_statement = IN_ITERATION_STMT;
7264         cp_parser_already_scoped_statement (parser);
7265         parser->in_statement = in_statement;
7266         /* We're done with the while-statement.  */
7267         finish_while_stmt (statement);
7268       }
7269       break;
7270
7271     case RID_DO:
7272       {
7273         tree expression;
7274
7275         /* Begin the do-statement.  */
7276         statement = begin_do_stmt ();
7277         /* Parse the body of the do-statement.  */
7278         parser->in_statement = IN_ITERATION_STMT;
7279         cp_parser_implicitly_scoped_statement (parser, NULL);
7280         parser->in_statement = in_statement;
7281         finish_do_body (statement);
7282         /* Look for the `while' keyword.  */
7283         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7284         /* Look for the `('.  */
7285         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7286         /* Parse the expression.  */
7287         expression = cp_parser_expression (parser, /*cast_p=*/false);
7288         /* We're done with the do-statement.  */
7289         finish_do_stmt (expression, statement);
7290         /* Look for the `)'.  */
7291         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7292         /* Look for the `;'.  */
7293         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7294       }
7295       break;
7296
7297     case RID_FOR:
7298       {
7299         tree condition = NULL_TREE;
7300         tree expression = NULL_TREE;
7301
7302         /* Begin the for-statement.  */
7303         statement = begin_for_stmt ();
7304         /* Look for the `('.  */
7305         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7306         /* Parse the initialization.  */
7307         cp_parser_for_init_statement (parser);
7308         finish_for_init_stmt (statement);
7309
7310         /* If there's a condition, process it.  */
7311         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7312           condition = cp_parser_condition (parser);
7313         finish_for_cond (condition, statement);
7314         /* Look for the `;'.  */
7315         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7316
7317         /* If there's an expression, process it.  */
7318         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7319           expression = cp_parser_expression (parser, /*cast_p=*/false);
7320         finish_for_expr (expression, statement);
7321         check_empty_body (parser, "for");
7322         /* Look for the `)'.  */
7323         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7324
7325         /* Parse the body of the for-statement.  */
7326         parser->in_statement = IN_ITERATION_STMT;
7327         cp_parser_already_scoped_statement (parser);
7328         parser->in_statement = in_statement;
7329
7330         /* We're done with the for-statement.  */
7331         finish_for_stmt (statement);
7332       }
7333       break;
7334
7335     default:
7336       cp_parser_error (parser, "expected iteration-statement");
7337       statement = error_mark_node;
7338       break;
7339     }
7340
7341   return statement;
7342 }
7343
7344 /* Parse a for-init-statement.
7345
7346    for-init-statement:
7347      expression-statement
7348      simple-declaration  */
7349
7350 static void
7351 cp_parser_for_init_statement (cp_parser* parser)
7352 {
7353   /* If the next token is a `;', then we have an empty
7354      expression-statement.  Grammatically, this is also a
7355      simple-declaration, but an invalid one, because it does not
7356      declare anything.  Therefore, if we did not handle this case
7357      specially, we would issue an error message about an invalid
7358      declaration.  */
7359   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7360     {
7361       /* We're going to speculatively look for a declaration, falling back
7362          to an expression, if necessary.  */
7363       cp_parser_parse_tentatively (parser);
7364       /* Parse the declaration.  */
7365       cp_parser_simple_declaration (parser,
7366                                     /*function_definition_allowed_p=*/false);
7367       /* If the tentative parse failed, then we shall need to look for an
7368          expression-statement.  */
7369       if (cp_parser_parse_definitely (parser))
7370         return;
7371     }
7372
7373   cp_parser_expression_statement (parser, false);
7374 }
7375
7376 /* Parse a jump-statement.
7377
7378    jump-statement:
7379      break ;
7380      continue ;
7381      return expression [opt] ;
7382      goto identifier ;
7383
7384    GNU extension:
7385
7386    jump-statement:
7387      goto * expression ;
7388
7389    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7390
7391 static tree
7392 cp_parser_jump_statement (cp_parser* parser)
7393 {
7394   tree statement = error_mark_node;
7395   cp_token *token;
7396   enum rid keyword;
7397   unsigned char in_statement;
7398
7399   /* Peek at the next token.  */
7400   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7401   if (!token)
7402     return error_mark_node;
7403
7404   /* See what kind of keyword it is.  */
7405   keyword = token->keyword;
7406   switch (keyword)
7407     {
7408     case RID_BREAK:
7409       in_statement = parser->in_statement & ~IN_IF_STMT;      
7410       switch (in_statement)
7411         {
7412         case 0:
7413           error ("break statement not within loop or switch");
7414           break;
7415         default:
7416           gcc_assert ((in_statement & IN_SWITCH_STMT)
7417                       || in_statement == IN_ITERATION_STMT);
7418           statement = finish_break_stmt ();
7419           break;
7420         case IN_OMP_BLOCK:
7421           error ("invalid exit from OpenMP structured block");
7422           break;
7423         case IN_OMP_FOR:
7424           error ("break statement used with OpenMP for loop");
7425           break;
7426         }
7427       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7428       break;
7429
7430     case RID_CONTINUE:
7431       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7432         {
7433         case 0:
7434           error ("continue statement not within a loop");
7435           break;
7436         case IN_ITERATION_STMT:
7437         case IN_OMP_FOR:
7438           statement = finish_continue_stmt ();
7439           break;
7440         case IN_OMP_BLOCK:
7441           error ("invalid exit from OpenMP structured block");
7442           break;
7443         default:
7444           gcc_unreachable ();
7445         }
7446       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7447       break;
7448
7449     case RID_RETURN:
7450       {
7451         tree expr;
7452
7453         /* If the next token is a `;', then there is no
7454            expression.  */
7455         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7456           expr = cp_parser_expression (parser, /*cast_p=*/false);
7457         else
7458           expr = NULL_TREE;
7459         /* Build the return-statement.  */
7460         statement = finish_return_stmt (expr);
7461         /* Look for the final `;'.  */
7462         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7463       }
7464       break;
7465
7466     case RID_GOTO:
7467       /* Create the goto-statement.  */
7468       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7469         {
7470           /* Issue a warning about this use of a GNU extension.  */
7471           if (pedantic)
7472             pedwarn ("ISO C++ forbids computed gotos");
7473           /* Consume the '*' token.  */
7474           cp_lexer_consume_token (parser->lexer);
7475           /* Parse the dependent expression.  */
7476           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7477         }
7478       else
7479         finish_goto_stmt (cp_parser_identifier (parser));
7480       /* Look for the final `;'.  */
7481       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7482       break;
7483
7484     default:
7485       cp_parser_error (parser, "expected jump-statement");
7486       break;
7487     }
7488
7489   return statement;
7490 }
7491
7492 /* Parse a declaration-statement.
7493
7494    declaration-statement:
7495      block-declaration  */
7496
7497 static void
7498 cp_parser_declaration_statement (cp_parser* parser)
7499 {
7500   void *p;
7501
7502   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7503   p = obstack_alloc (&declarator_obstack, 0);
7504
7505  /* Parse the block-declaration.  */
7506   cp_parser_block_declaration (parser, /*statement_p=*/true);
7507
7508   /* Free any declarators allocated.  */
7509   obstack_free (&declarator_obstack, p);
7510
7511   /* Finish off the statement.  */
7512   finish_stmt ();
7513 }
7514
7515 /* Some dependent statements (like `if (cond) statement'), are
7516    implicitly in their own scope.  In other words, if the statement is
7517    a single statement (as opposed to a compound-statement), it is
7518    none-the-less treated as if it were enclosed in braces.  Any
7519    declarations appearing in the dependent statement are out of scope
7520    after control passes that point.  This function parses a statement,
7521    but ensures that is in its own scope, even if it is not a
7522    compound-statement.
7523
7524    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7525    is a (possibly labeled) if statement which is not enclosed in
7526    braces and has an else clause.  This is used to implement
7527    -Wparentheses.
7528
7529    Returns the new statement.  */
7530
7531 static tree
7532 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7533 {
7534   tree statement;
7535
7536   if (if_p != NULL)
7537     *if_p = false;
7538
7539   /* Mark if () ; with a special NOP_EXPR.  */
7540   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7541     {
7542       cp_lexer_consume_token (parser->lexer);
7543       statement = add_stmt (build_empty_stmt ());
7544     }
7545   /* if a compound is opened, we simply parse the statement directly.  */
7546   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7547     statement = cp_parser_compound_statement (parser, NULL, false);
7548   /* If the token is not a `{', then we must take special action.  */
7549   else
7550     {
7551       /* Create a compound-statement.  */
7552       statement = begin_compound_stmt (0);
7553       /* Parse the dependent-statement.  */
7554       cp_parser_statement (parser, NULL_TREE, false, if_p);
7555       /* Finish the dummy compound-statement.  */
7556       finish_compound_stmt (statement);
7557     }
7558
7559   /* Return the statement.  */
7560   return statement;
7561 }
7562
7563 /* For some dependent statements (like `while (cond) statement'), we
7564    have already created a scope.  Therefore, even if the dependent
7565    statement is a compound-statement, we do not want to create another
7566    scope.  */
7567
7568 static void
7569 cp_parser_already_scoped_statement (cp_parser* parser)
7570 {
7571   /* If the token is a `{', then we must take special action.  */
7572   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7573     cp_parser_statement (parser, NULL_TREE, false, NULL);
7574   else
7575     {
7576       /* Avoid calling cp_parser_compound_statement, so that we
7577          don't create a new scope.  Do everything else by hand.  */
7578       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7579       cp_parser_statement_seq_opt (parser, NULL_TREE);
7580       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7581     }
7582 }
7583
7584 /* Declarations [gram.dcl.dcl] */
7585
7586 /* Parse an optional declaration-sequence.
7587
7588    declaration-seq:
7589      declaration
7590      declaration-seq declaration  */
7591
7592 static void
7593 cp_parser_declaration_seq_opt (cp_parser* parser)
7594 {
7595   while (true)
7596     {
7597       cp_token *token;
7598
7599       token = cp_lexer_peek_token (parser->lexer);
7600
7601       if (token->type == CPP_CLOSE_BRACE
7602           || token->type == CPP_EOF
7603           || token->type == CPP_PRAGMA_EOL)
7604         break;
7605
7606       if (token->type == CPP_SEMICOLON)
7607         {
7608           /* A declaration consisting of a single semicolon is
7609              invalid.  Allow it unless we're being pedantic.  */
7610           cp_lexer_consume_token (parser->lexer);
7611           if (pedantic && !in_system_header)
7612             pedwarn ("extra %<;%>");
7613           continue;
7614         }
7615
7616       /* If we're entering or exiting a region that's implicitly
7617          extern "C", modify the lang context appropriately.  */
7618       if (!parser->implicit_extern_c && token->implicit_extern_c)
7619         {
7620           push_lang_context (lang_name_c);
7621           parser->implicit_extern_c = true;
7622         }
7623       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7624         {
7625           pop_lang_context ();
7626           parser->implicit_extern_c = false;
7627         }
7628
7629       if (token->type == CPP_PRAGMA)
7630         {
7631           /* A top-level declaration can consist solely of a #pragma.
7632              A nested declaration cannot, so this is done here and not
7633              in cp_parser_declaration.  (A #pragma at block scope is
7634              handled in cp_parser_statement.)  */
7635           cp_parser_pragma (parser, pragma_external);
7636           continue;
7637         }
7638
7639       /* Parse the declaration itself.  */
7640       cp_parser_declaration (parser);
7641     }
7642 }
7643
7644 /* Parse a declaration.
7645
7646    declaration:
7647      block-declaration
7648      function-definition
7649      template-declaration
7650      explicit-instantiation
7651      explicit-specialization
7652      linkage-specification
7653      namespace-definition
7654
7655    GNU extension:
7656
7657    declaration:
7658       __extension__ declaration */
7659
7660 static void
7661 cp_parser_declaration (cp_parser* parser)
7662 {
7663   cp_token token1;
7664   cp_token token2;
7665   int saved_pedantic;
7666   void *p;
7667
7668   /* Check for the `__extension__' keyword.  */
7669   if (cp_parser_extension_opt (parser, &saved_pedantic))
7670     {
7671       /* Parse the qualified declaration.  */
7672       cp_parser_declaration (parser);
7673       /* Restore the PEDANTIC flag.  */
7674       pedantic = saved_pedantic;
7675
7676       return;
7677     }
7678
7679   /* Try to figure out what kind of declaration is present.  */
7680   token1 = *cp_lexer_peek_token (parser->lexer);
7681
7682   if (token1.type != CPP_EOF)
7683     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7684   else
7685     {
7686       token2.type = CPP_EOF;
7687       token2.keyword = RID_MAX;
7688     }
7689
7690   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7691   p = obstack_alloc (&declarator_obstack, 0);
7692
7693   /* If the next token is `extern' and the following token is a string
7694      literal, then we have a linkage specification.  */
7695   if (token1.keyword == RID_EXTERN
7696       && cp_parser_is_string_literal (&token2))
7697     cp_parser_linkage_specification (parser);
7698   /* If the next token is `template', then we have either a template
7699      declaration, an explicit instantiation, or an explicit
7700      specialization.  */
7701   else if (token1.keyword == RID_TEMPLATE)
7702     {
7703       /* `template <>' indicates a template specialization.  */
7704       if (token2.type == CPP_LESS
7705           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7706         cp_parser_explicit_specialization (parser);
7707       /* `template <' indicates a template declaration.  */
7708       else if (token2.type == CPP_LESS)
7709         cp_parser_template_declaration (parser, /*member_p=*/false);
7710       /* Anything else must be an explicit instantiation.  */
7711       else
7712         cp_parser_explicit_instantiation (parser);
7713     }
7714   /* If the next token is `export', then we have a template
7715      declaration.  */
7716   else if (token1.keyword == RID_EXPORT)
7717     cp_parser_template_declaration (parser, /*member_p=*/false);
7718   /* If the next token is `extern', 'static' or 'inline' and the one
7719      after that is `template', we have a GNU extended explicit
7720      instantiation directive.  */
7721   else if (cp_parser_allow_gnu_extensions_p (parser)
7722            && (token1.keyword == RID_EXTERN
7723                || token1.keyword == RID_STATIC
7724                || token1.keyword == RID_INLINE)
7725            && token2.keyword == RID_TEMPLATE)
7726     cp_parser_explicit_instantiation (parser);
7727   /* If the next token is `namespace', check for a named or unnamed
7728      namespace definition.  */
7729   else if (token1.keyword == RID_NAMESPACE
7730            && (/* A named namespace definition.  */
7731                (token2.type == CPP_NAME
7732                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7733                     != CPP_EQ))
7734                /* An unnamed namespace definition.  */
7735                || token2.type == CPP_OPEN_BRACE
7736                || token2.keyword == RID_ATTRIBUTE))
7737     cp_parser_namespace_definition (parser);
7738   /* Objective-C++ declaration/definition.  */
7739   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7740     cp_parser_objc_declaration (parser);
7741   /* We must have either a block declaration or a function
7742      definition.  */
7743   else
7744     /* Try to parse a block-declaration, or a function-definition.  */
7745     cp_parser_block_declaration (parser, /*statement_p=*/false);
7746
7747   /* Free any declarators allocated.  */
7748   obstack_free (&declarator_obstack, p);
7749 }
7750
7751 /* Parse a block-declaration.
7752
7753    block-declaration:
7754      simple-declaration
7755      asm-definition
7756      namespace-alias-definition
7757      using-declaration
7758      using-directive
7759
7760    GNU Extension:
7761
7762    block-declaration:
7763      __extension__ block-declaration
7764
7765    C++0x Extension:
7766
7767    block-declaration:
7768      static_assert-declaration
7769
7770    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7771    part of a declaration-statement.  */
7772
7773 static void
7774 cp_parser_block_declaration (cp_parser *parser,
7775                              bool      statement_p)
7776 {
7777   cp_token *token1;
7778   int saved_pedantic;
7779
7780   /* Check for the `__extension__' keyword.  */
7781   if (cp_parser_extension_opt (parser, &saved_pedantic))
7782     {
7783       /* Parse the qualified declaration.  */
7784       cp_parser_block_declaration (parser, statement_p);
7785       /* Restore the PEDANTIC flag.  */
7786       pedantic = saved_pedantic;
7787
7788       return;
7789     }
7790
7791   /* Peek at the next token to figure out which kind of declaration is
7792      present.  */
7793   token1 = cp_lexer_peek_token (parser->lexer);
7794
7795   /* If the next keyword is `asm', we have an asm-definition.  */
7796   if (token1->keyword == RID_ASM)
7797     {
7798       if (statement_p)
7799         cp_parser_commit_to_tentative_parse (parser);
7800       cp_parser_asm_definition (parser);
7801     }
7802   /* If the next keyword is `namespace', we have a
7803      namespace-alias-definition.  */
7804   else if (token1->keyword == RID_NAMESPACE)
7805     cp_parser_namespace_alias_definition (parser);
7806   /* If the next keyword is `using', we have either a
7807      using-declaration or a using-directive.  */
7808   else if (token1->keyword == RID_USING)
7809     {
7810       cp_token *token2;
7811
7812       if (statement_p)
7813         cp_parser_commit_to_tentative_parse (parser);
7814       /* If the token after `using' is `namespace', then we have a
7815          using-directive.  */
7816       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7817       if (token2->keyword == RID_NAMESPACE)
7818         cp_parser_using_directive (parser);
7819       /* Otherwise, it's a using-declaration.  */
7820       else
7821         cp_parser_using_declaration (parser,
7822                                      /*access_declaration_p=*/false);
7823     }
7824   /* If the next keyword is `__label__' we have a misplaced label
7825      declaration.  */
7826   else if (token1->keyword == RID_LABEL)
7827     {
7828       cp_lexer_consume_token (parser->lexer);
7829       error ("%<__label__%> not at the beginning of a block");
7830       cp_parser_skip_to_end_of_statement (parser);
7831       /* If the next token is now a `;', consume it.  */
7832       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7833         cp_lexer_consume_token (parser->lexer);
7834     }
7835   /* If the next token is `static_assert' we have a static assertion.  */
7836   else if (token1->keyword == RID_STATIC_ASSERT)
7837     cp_parser_static_assert (parser, /*member_p=*/false);
7838   /* Anything else must be a simple-declaration.  */
7839   else
7840     cp_parser_simple_declaration (parser, !statement_p);
7841 }
7842
7843 /* Parse a simple-declaration.
7844
7845    simple-declaration:
7846      decl-specifier-seq [opt] init-declarator-list [opt] ;
7847
7848    init-declarator-list:
7849      init-declarator
7850      init-declarator-list , init-declarator
7851
7852    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7853    function-definition as a simple-declaration.  */
7854
7855 static void
7856 cp_parser_simple_declaration (cp_parser* parser,
7857                               bool function_definition_allowed_p)
7858 {
7859   cp_decl_specifier_seq decl_specifiers;
7860   int declares_class_or_enum;
7861   bool saw_declarator;
7862
7863   /* Defer access checks until we know what is being declared; the
7864      checks for names appearing in the decl-specifier-seq should be
7865      done as if we were in the scope of the thing being declared.  */
7866   push_deferring_access_checks (dk_deferred);
7867
7868   /* Parse the decl-specifier-seq.  We have to keep track of whether
7869      or not the decl-specifier-seq declares a named class or
7870      enumeration type, since that is the only case in which the
7871      init-declarator-list is allowed to be empty.
7872
7873      [dcl.dcl]
7874
7875      In a simple-declaration, the optional init-declarator-list can be
7876      omitted only when declaring a class or enumeration, that is when
7877      the decl-specifier-seq contains either a class-specifier, an
7878      elaborated-type-specifier, or an enum-specifier.  */
7879   cp_parser_decl_specifier_seq (parser,
7880                                 CP_PARSER_FLAGS_OPTIONAL,
7881                                 &decl_specifiers,
7882                                 &declares_class_or_enum);
7883   /* We no longer need to defer access checks.  */
7884   stop_deferring_access_checks ();
7885
7886   /* In a block scope, a valid declaration must always have a
7887      decl-specifier-seq.  By not trying to parse declarators, we can
7888      resolve the declaration/expression ambiguity more quickly.  */
7889   if (!function_definition_allowed_p
7890       && !decl_specifiers.any_specifiers_p)
7891     {
7892       cp_parser_error (parser, "expected declaration");
7893       goto done;
7894     }
7895
7896   /* If the next two tokens are both identifiers, the code is
7897      erroneous. The usual cause of this situation is code like:
7898
7899        T t;
7900
7901      where "T" should name a type -- but does not.  */
7902   if (!decl_specifiers.type
7903       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7904     {
7905       /* If parsing tentatively, we should commit; we really are
7906          looking at a declaration.  */
7907       cp_parser_commit_to_tentative_parse (parser);
7908       /* Give up.  */
7909       goto done;
7910     }
7911
7912   /* If we have seen at least one decl-specifier, and the next token
7913      is not a parenthesis, then we must be looking at a declaration.
7914      (After "int (" we might be looking at a functional cast.)  */
7915   if (decl_specifiers.any_specifiers_p
7916       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7917     cp_parser_commit_to_tentative_parse (parser);
7918
7919   /* Keep going until we hit the `;' at the end of the simple
7920      declaration.  */
7921   saw_declarator = false;
7922   while (cp_lexer_next_token_is_not (parser->lexer,
7923                                      CPP_SEMICOLON))
7924     {
7925       cp_token *token;
7926       bool function_definition_p;
7927       tree decl;
7928
7929       if (saw_declarator)
7930         {
7931           /* If we are processing next declarator, coma is expected */
7932           token = cp_lexer_peek_token (parser->lexer);
7933           gcc_assert (token->type == CPP_COMMA);
7934           cp_lexer_consume_token (parser->lexer);
7935         }
7936       else
7937         saw_declarator = true;
7938
7939       /* Parse the init-declarator.  */
7940       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7941                                         /*checks=*/NULL,
7942                                         function_definition_allowed_p,
7943                                         /*member_p=*/false,
7944                                         declares_class_or_enum,
7945                                         &function_definition_p);
7946       /* If an error occurred while parsing tentatively, exit quickly.
7947          (That usually happens when in the body of a function; each
7948          statement is treated as a declaration-statement until proven
7949          otherwise.)  */
7950       if (cp_parser_error_occurred (parser))
7951         goto done;
7952       /* Handle function definitions specially.  */
7953       if (function_definition_p)
7954         {
7955           /* If the next token is a `,', then we are probably
7956              processing something like:
7957
7958                void f() {}, *p;
7959
7960              which is erroneous.  */
7961           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7962             error ("mixing declarations and function-definitions is forbidden");
7963           /* Otherwise, we're done with the list of declarators.  */
7964           else
7965             {
7966               pop_deferring_access_checks ();
7967               return;
7968             }
7969         }
7970       /* The next token should be either a `,' or a `;'.  */
7971       token = cp_lexer_peek_token (parser->lexer);
7972       /* If it's a `,', there are more declarators to come.  */
7973       if (token->type == CPP_COMMA)
7974         /* will be consumed next time around */;
7975       /* If it's a `;', we are done.  */
7976       else if (token->type == CPP_SEMICOLON)
7977         break;
7978       /* Anything else is an error.  */
7979       else
7980         {
7981           /* If we have already issued an error message we don't need
7982              to issue another one.  */
7983           if (decl != error_mark_node
7984               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7985             cp_parser_error (parser, "expected %<,%> or %<;%>");
7986           /* Skip tokens until we reach the end of the statement.  */
7987           cp_parser_skip_to_end_of_statement (parser);
7988           /* If the next token is now a `;', consume it.  */
7989           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7990             cp_lexer_consume_token (parser->lexer);
7991           goto done;
7992         }
7993       /* After the first time around, a function-definition is not
7994          allowed -- even if it was OK at first.  For example:
7995
7996            int i, f() {}
7997
7998          is not valid.  */
7999       function_definition_allowed_p = false;
8000     }
8001
8002   /* Issue an error message if no declarators are present, and the
8003      decl-specifier-seq does not itself declare a class or
8004      enumeration.  */
8005   if (!saw_declarator)
8006     {
8007       if (cp_parser_declares_only_class_p (parser))
8008         shadow_tag (&decl_specifiers);
8009       /* Perform any deferred access checks.  */
8010       perform_deferred_access_checks ();
8011     }
8012
8013   /* Consume the `;'.  */
8014   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8015
8016  done:
8017   pop_deferring_access_checks ();
8018 }
8019
8020 /* Parse a decl-specifier-seq.
8021
8022    decl-specifier-seq:
8023      decl-specifier-seq [opt] decl-specifier
8024
8025    decl-specifier:
8026      storage-class-specifier
8027      type-specifier
8028      function-specifier
8029      friend
8030      typedef
8031
8032    GNU Extension:
8033
8034    decl-specifier:
8035      attributes
8036
8037    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8038
8039    The parser flags FLAGS is used to control type-specifier parsing.
8040
8041    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8042    flags:
8043
8044      1: one of the decl-specifiers is an elaborated-type-specifier
8045         (i.e., a type declaration)
8046      2: one of the decl-specifiers is an enum-specifier or a
8047         class-specifier (i.e., a type definition)
8048
8049    */
8050
8051 static void
8052 cp_parser_decl_specifier_seq (cp_parser* parser,
8053                               cp_parser_flags flags,
8054                               cp_decl_specifier_seq *decl_specs,
8055                               int* declares_class_or_enum)
8056 {
8057   bool constructor_possible_p = !parser->in_declarator_p;
8058
8059   /* Clear DECL_SPECS.  */
8060   clear_decl_specs (decl_specs);
8061
8062   /* Assume no class or enumeration type is declared.  */
8063   *declares_class_or_enum = 0;
8064
8065   /* Keep reading specifiers until there are no more to read.  */
8066   while (true)
8067     {
8068       bool constructor_p;
8069       bool found_decl_spec;
8070       cp_token *token;
8071
8072       /* Peek at the next token.  */
8073       token = cp_lexer_peek_token (parser->lexer);
8074       /* Handle attributes.  */
8075       if (token->keyword == RID_ATTRIBUTE)
8076         {
8077           /* Parse the attributes.  */
8078           decl_specs->attributes
8079             = chainon (decl_specs->attributes,
8080                        cp_parser_attributes_opt (parser));
8081           continue;
8082         }
8083       /* Assume we will find a decl-specifier keyword.  */
8084       found_decl_spec = true;
8085       /* If the next token is an appropriate keyword, we can simply
8086          add it to the list.  */
8087       switch (token->keyword)
8088         {
8089           /* decl-specifier:
8090                friend  */
8091         case RID_FRIEND:
8092           if (!at_class_scope_p ())
8093             {
8094               error ("%<friend%> used outside of class");
8095               cp_lexer_purge_token (parser->lexer);
8096             }
8097           else
8098             {
8099               ++decl_specs->specs[(int) ds_friend];
8100               /* Consume the token.  */
8101               cp_lexer_consume_token (parser->lexer);
8102             }
8103           break;
8104
8105           /* function-specifier:
8106                inline
8107                virtual
8108                explicit  */
8109         case RID_INLINE:
8110         case RID_VIRTUAL:
8111         case RID_EXPLICIT:
8112           cp_parser_function_specifier_opt (parser, decl_specs);
8113           break;
8114
8115           /* decl-specifier:
8116                typedef  */
8117         case RID_TYPEDEF:
8118           ++decl_specs->specs[(int) ds_typedef];
8119           /* Consume the token.  */
8120           cp_lexer_consume_token (parser->lexer);
8121           /* A constructor declarator cannot appear in a typedef.  */
8122           constructor_possible_p = false;
8123           /* The "typedef" keyword can only occur in a declaration; we
8124              may as well commit at this point.  */
8125           cp_parser_commit_to_tentative_parse (parser);
8126
8127           if (decl_specs->storage_class != sc_none)
8128             decl_specs->conflicting_specifiers_p = true;
8129           break;
8130
8131           /* storage-class-specifier:
8132                auto
8133                register
8134                static
8135                extern
8136                mutable
8137
8138              GNU Extension:
8139                thread  */
8140         case RID_AUTO:
8141         case RID_REGISTER:
8142         case RID_STATIC:
8143         case RID_EXTERN:
8144         case RID_MUTABLE:
8145           /* Consume the token.  */
8146           cp_lexer_consume_token (parser->lexer);
8147           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8148           break;
8149         case RID_THREAD:
8150           /* Consume the token.  */
8151           cp_lexer_consume_token (parser->lexer);
8152           ++decl_specs->specs[(int) ds_thread];
8153           break;
8154
8155         default:
8156           /* We did not yet find a decl-specifier yet.  */
8157           found_decl_spec = false;
8158           break;
8159         }
8160
8161       /* Constructors are a special case.  The `S' in `S()' is not a
8162          decl-specifier; it is the beginning of the declarator.  */
8163       constructor_p
8164         = (!found_decl_spec
8165            && constructor_possible_p
8166            && (cp_parser_constructor_declarator_p
8167                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8168
8169       /* If we don't have a DECL_SPEC yet, then we must be looking at
8170          a type-specifier.  */
8171       if (!found_decl_spec && !constructor_p)
8172         {
8173           int decl_spec_declares_class_or_enum;
8174           bool is_cv_qualifier;
8175           tree type_spec;
8176
8177           type_spec
8178             = cp_parser_type_specifier (parser, flags,
8179                                         decl_specs,
8180                                         /*is_declaration=*/true,
8181                                         &decl_spec_declares_class_or_enum,
8182                                         &is_cv_qualifier);
8183
8184           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8185
8186           /* If this type-specifier referenced a user-defined type
8187              (a typedef, class-name, etc.), then we can't allow any
8188              more such type-specifiers henceforth.
8189
8190              [dcl.spec]
8191
8192              The longest sequence of decl-specifiers that could
8193              possibly be a type name is taken as the
8194              decl-specifier-seq of a declaration.  The sequence shall
8195              be self-consistent as described below.
8196
8197              [dcl.type]
8198
8199              As a general rule, at most one type-specifier is allowed
8200              in the complete decl-specifier-seq of a declaration.  The
8201              only exceptions are the following:
8202
8203              -- const or volatile can be combined with any other
8204                 type-specifier.
8205
8206              -- signed or unsigned can be combined with char, long,
8207                 short, or int.
8208
8209              -- ..
8210
8211              Example:
8212
8213                typedef char* Pc;
8214                void g (const int Pc);
8215
8216              Here, Pc is *not* part of the decl-specifier seq; it's
8217              the declarator.  Therefore, once we see a type-specifier
8218              (other than a cv-qualifier), we forbid any additional
8219              user-defined types.  We *do* still allow things like `int
8220              int' to be considered a decl-specifier-seq, and issue the
8221              error message later.  */
8222           if (type_spec && !is_cv_qualifier)
8223             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8224           /* A constructor declarator cannot follow a type-specifier.  */
8225           if (type_spec)
8226             {
8227               constructor_possible_p = false;
8228               found_decl_spec = true;
8229             }
8230         }
8231
8232       /* If we still do not have a DECL_SPEC, then there are no more
8233          decl-specifiers.  */
8234       if (!found_decl_spec)
8235         break;
8236
8237       decl_specs->any_specifiers_p = true;
8238       /* After we see one decl-specifier, further decl-specifiers are
8239          always optional.  */
8240       flags |= CP_PARSER_FLAGS_OPTIONAL;
8241     }
8242
8243   cp_parser_check_decl_spec (decl_specs);
8244
8245   /* Don't allow a friend specifier with a class definition.  */
8246   if (decl_specs->specs[(int) ds_friend] != 0
8247       && (*declares_class_or_enum & 2))
8248     error ("class definition may not be declared a friend");
8249 }
8250
8251 /* Parse an (optional) storage-class-specifier.
8252
8253    storage-class-specifier:
8254      auto
8255      register
8256      static
8257      extern
8258      mutable
8259
8260    GNU Extension:
8261
8262    storage-class-specifier:
8263      thread
8264
8265    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8266
8267 static tree
8268 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8269 {
8270   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8271     {
8272     case RID_AUTO:
8273     case RID_REGISTER:
8274     case RID_STATIC:
8275     case RID_EXTERN:
8276     case RID_MUTABLE:
8277     case RID_THREAD:
8278       /* Consume the token.  */
8279       return cp_lexer_consume_token (parser->lexer)->u.value;
8280
8281     default:
8282       return NULL_TREE;
8283     }
8284 }
8285
8286 /* Parse an (optional) function-specifier.
8287
8288    function-specifier:
8289      inline
8290      virtual
8291      explicit
8292
8293    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8294    Updates DECL_SPECS, if it is non-NULL.  */
8295
8296 static tree
8297 cp_parser_function_specifier_opt (cp_parser* parser,
8298                                   cp_decl_specifier_seq *decl_specs)
8299 {
8300   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8301     {
8302     case RID_INLINE:
8303       if (decl_specs)
8304         ++decl_specs->specs[(int) ds_inline];
8305       break;
8306
8307     case RID_VIRTUAL:
8308       /* 14.5.2.3 [temp.mem]
8309
8310          A member function template shall not be virtual.  */
8311       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8312         error ("templates may not be %<virtual%>");
8313       else if (decl_specs)
8314         ++decl_specs->specs[(int) ds_virtual];
8315       break;
8316
8317     case RID_EXPLICIT:
8318       if (decl_specs)
8319         ++decl_specs->specs[(int) ds_explicit];
8320       break;
8321
8322     default:
8323       return NULL_TREE;
8324     }
8325
8326   /* Consume the token.  */
8327   return cp_lexer_consume_token (parser->lexer)->u.value;
8328 }
8329
8330 /* Parse a linkage-specification.
8331
8332    linkage-specification:
8333      extern string-literal { declaration-seq [opt] }
8334      extern string-literal declaration  */
8335
8336 static void
8337 cp_parser_linkage_specification (cp_parser* parser)
8338 {
8339   tree linkage;
8340
8341   /* Look for the `extern' keyword.  */
8342   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8343
8344   /* Look for the string-literal.  */
8345   linkage = cp_parser_string_literal (parser, false, false);
8346
8347   /* Transform the literal into an identifier.  If the literal is a
8348      wide-character string, or contains embedded NULs, then we can't
8349      handle it as the user wants.  */
8350   if (strlen (TREE_STRING_POINTER (linkage))
8351       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8352     {
8353       cp_parser_error (parser, "invalid linkage-specification");
8354       /* Assume C++ linkage.  */
8355       linkage = lang_name_cplusplus;
8356     }
8357   else
8358     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8359
8360   /* We're now using the new linkage.  */
8361   push_lang_context (linkage);
8362
8363   /* If the next token is a `{', then we're using the first
8364      production.  */
8365   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8366     {
8367       /* Consume the `{' token.  */
8368       cp_lexer_consume_token (parser->lexer);
8369       /* Parse the declarations.  */
8370       cp_parser_declaration_seq_opt (parser);
8371       /* Look for the closing `}'.  */
8372       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8373     }
8374   /* Otherwise, there's just one declaration.  */
8375   else
8376     {
8377       bool saved_in_unbraced_linkage_specification_p;
8378
8379       saved_in_unbraced_linkage_specification_p
8380         = parser->in_unbraced_linkage_specification_p;
8381       parser->in_unbraced_linkage_specification_p = true;
8382       cp_parser_declaration (parser);
8383       parser->in_unbraced_linkage_specification_p
8384         = saved_in_unbraced_linkage_specification_p;
8385     }
8386
8387   /* We're done with the linkage-specification.  */
8388   pop_lang_context ();
8389 }
8390
8391 /* Parse a static_assert-declaration.
8392
8393    static_assert-declaration:
8394      static_assert ( constant-expression , string-literal ) ; 
8395
8396    If MEMBER_P, this static_assert is a class member.  */
8397
8398 static void 
8399 cp_parser_static_assert(cp_parser *parser, bool member_p)
8400 {
8401   tree condition;
8402   tree message;
8403   cp_token *token;
8404   location_t saved_loc;
8405
8406   /* Peek at the `static_assert' token so we can keep track of exactly
8407      where the static assertion started.  */
8408   token = cp_lexer_peek_token (parser->lexer);
8409   saved_loc = token->location;
8410
8411   /* Look for the `static_assert' keyword.  */
8412   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8413                                   "`static_assert'"))
8414     return;
8415
8416   /*  We know we are in a static assertion; commit to any tentative
8417       parse.  */
8418   if (cp_parser_parsing_tentatively (parser))
8419     cp_parser_commit_to_tentative_parse (parser);
8420
8421   /* Parse the `(' starting the static assertion condition.  */
8422   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8423
8424   /* Parse the constant-expression.  */
8425   condition = 
8426     cp_parser_constant_expression (parser,
8427                                    /*allow_non_constant_p=*/false,
8428                                    /*non_constant_p=*/NULL);
8429
8430   /* Parse the separating `,'.  */
8431   cp_parser_require (parser, CPP_COMMA, "`,'");
8432
8433   /* Parse the string-literal message.  */
8434   message = cp_parser_string_literal (parser, 
8435                                       /*translate=*/false,
8436                                       /*wide_ok=*/true);
8437
8438   /* A `)' completes the static assertion.  */
8439   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8440     cp_parser_skip_to_closing_parenthesis (parser, 
8441                                            /*recovering=*/true, 
8442                                            /*or_comma=*/false,
8443                                            /*consume_paren=*/true);
8444
8445   /* A semicolon terminates the declaration.  */
8446   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8447
8448   /* Complete the static assertion, which may mean either processing 
8449      the static assert now or saving it for template instantiation.  */
8450   finish_static_assert (condition, message, saved_loc, member_p);
8451 }
8452
8453 /* Parse a `decltype' type. Returns the type. 
8454
8455    simple-type-specifier:
8456      decltype ( expression )  */
8457
8458 static tree
8459 cp_parser_decltype (cp_parser *parser)
8460 {
8461   tree expr;
8462   bool id_expression_or_member_access_p = false;
8463   const char *saved_message;
8464   bool saved_integral_constant_expression_p;
8465   bool saved_non_integral_constant_expression_p;
8466
8467   /* Look for the `decltype' token.  */
8468   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8469     return error_mark_node;
8470
8471   /* Types cannot be defined in a `decltype' expression.  Save away the
8472      old message.  */
8473   saved_message = parser->type_definition_forbidden_message;
8474
8475   /* And create the new one.  */
8476   parser->type_definition_forbidden_message
8477     = "types may not be defined in `decltype' expressions";
8478
8479   /* The restrictions on constant-expressions do not apply inside
8480      decltype expressions.  */
8481   saved_integral_constant_expression_p
8482     = parser->integral_constant_expression_p;
8483   saved_non_integral_constant_expression_p
8484     = parser->non_integral_constant_expression_p;
8485   parser->integral_constant_expression_p = false;
8486
8487   /* Do not actually evaluate the expression.  */
8488   ++skip_evaluation;
8489
8490   /* Parse the opening `('.  */
8491   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
8492     return error_mark_node;
8493   
8494   /* First, try parsing an id-expression.  */
8495   cp_parser_parse_tentatively (parser);
8496   expr = cp_parser_id_expression (parser,
8497                                   /*template_keyword_p=*/false,
8498                                   /*check_dependency_p=*/true,
8499                                   /*template_p=*/NULL,
8500                                   /*declarator_p=*/false,
8501                                   /*optional_p=*/false);
8502
8503   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8504     {
8505       bool non_integral_constant_expression_p = false;
8506       tree id_expression = expr;
8507       cp_id_kind idk;
8508       const char *error_msg;
8509
8510       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8511         /* Lookup the name we got back from the id-expression.  */
8512         expr = cp_parser_lookup_name (parser, expr,
8513                                       none_type,
8514                                       /*is_template=*/false,
8515                                       /*is_namespace=*/false,
8516                                       /*check_dependency=*/true,
8517                                       /*ambiguous_decls=*/NULL);
8518
8519       if (expr
8520           && expr != error_mark_node
8521           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8522           && TREE_CODE (expr) != TYPE_DECL
8523           && (TREE_CODE (expr) != BIT_NOT_EXPR
8524               || !TYPE_P (TREE_OPERAND (expr, 0)))
8525           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8526         {
8527           /* Complete lookup of the id-expression.  */
8528           expr = (finish_id_expression
8529                   (id_expression, expr, parser->scope, &idk,
8530                    /*integral_constant_expression_p=*/false,
8531                    /*allow_non_integral_constant_expression_p=*/true,
8532                    &non_integral_constant_expression_p,
8533                    /*template_p=*/false,
8534                    /*done=*/true,
8535                    /*address_p=*/false,
8536                    /*template_arg_p=*/false,
8537                    &error_msg));
8538
8539           if (expr == error_mark_node)
8540             /* We found an id-expression, but it was something that we
8541                should not have found. This is an error, not something
8542                we can recover from, so note that we found an
8543                id-expression and we'll recover as gracefully as
8544                possible.  */
8545             id_expression_or_member_access_p = true;
8546         }
8547
8548       if (expr 
8549           && expr != error_mark_node
8550           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8551         /* We have an id-expression.  */
8552         id_expression_or_member_access_p = true;
8553     }
8554
8555   if (!id_expression_or_member_access_p)
8556     {
8557       /* Abort the id-expression parse.  */
8558       cp_parser_abort_tentative_parse (parser);
8559
8560       /* Parsing tentatively, again.  */
8561       cp_parser_parse_tentatively (parser);
8562
8563       /* Parse a class member access.  */
8564       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8565                                            /*cast_p=*/false,
8566                                            /*member_access_only_p=*/true);
8567
8568       if (expr 
8569           && expr != error_mark_node
8570           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8571         /* We have an id-expression.  */
8572         id_expression_or_member_access_p = true;
8573     }
8574
8575   if (id_expression_or_member_access_p)
8576     /* We have parsed the complete id-expression or member access.  */
8577     cp_parser_parse_definitely (parser);
8578   else
8579     {
8580       /* Abort our attempt to parse an id-expression or member access
8581          expression.  */
8582       cp_parser_abort_tentative_parse (parser);
8583
8584       /* Parse a full expression.  */
8585       expr = cp_parser_expression (parser, /*cast_p=*/false);
8586     }
8587
8588   /* Go back to evaluating expressions.  */
8589   --skip_evaluation;
8590
8591   /* Restore the old message and the integral constant expression
8592      flags.  */
8593   parser->type_definition_forbidden_message = saved_message;
8594   parser->integral_constant_expression_p
8595     = saved_integral_constant_expression_p;
8596   parser->non_integral_constant_expression_p
8597     = saved_non_integral_constant_expression_p;
8598
8599   if (expr == error_mark_node)
8600     {
8601       /* Skip everything up to the closing `)'.  */
8602       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8603                                              /*consume_paren=*/true);
8604       return error_mark_node;
8605     }
8606   
8607   /* Parse to the closing `)'.  */
8608   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8609     {
8610       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8611                                              /*consume_paren=*/true);
8612       return error_mark_node;
8613     }
8614
8615   return finish_decltype_type (expr, id_expression_or_member_access_p);
8616 }
8617
8618 /* Special member functions [gram.special] */
8619
8620 /* Parse a conversion-function-id.
8621
8622    conversion-function-id:
8623      operator conversion-type-id
8624
8625    Returns an IDENTIFIER_NODE representing the operator.  */
8626
8627 static tree
8628 cp_parser_conversion_function_id (cp_parser* parser)
8629 {
8630   tree type;
8631   tree saved_scope;
8632   tree saved_qualifying_scope;
8633   tree saved_object_scope;
8634   tree pushed_scope = NULL_TREE;
8635
8636   /* Look for the `operator' token.  */
8637   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8638     return error_mark_node;
8639   /* When we parse the conversion-type-id, the current scope will be
8640      reset.  However, we need that information in able to look up the
8641      conversion function later, so we save it here.  */
8642   saved_scope = parser->scope;
8643   saved_qualifying_scope = parser->qualifying_scope;
8644   saved_object_scope = parser->object_scope;
8645   /* We must enter the scope of the class so that the names of
8646      entities declared within the class are available in the
8647      conversion-type-id.  For example, consider:
8648
8649        struct S {
8650          typedef int I;
8651          operator I();
8652        };
8653
8654        S::operator I() { ... }
8655
8656      In order to see that `I' is a type-name in the definition, we
8657      must be in the scope of `S'.  */
8658   if (saved_scope)
8659     pushed_scope = push_scope (saved_scope);
8660   /* Parse the conversion-type-id.  */
8661   type = cp_parser_conversion_type_id (parser);
8662   /* Leave the scope of the class, if any.  */
8663   if (pushed_scope)
8664     pop_scope (pushed_scope);
8665   /* Restore the saved scope.  */
8666   parser->scope = saved_scope;
8667   parser->qualifying_scope = saved_qualifying_scope;
8668   parser->object_scope = saved_object_scope;
8669   /* If the TYPE is invalid, indicate failure.  */
8670   if (type == error_mark_node)
8671     return error_mark_node;
8672   return mangle_conv_op_name_for_type (type);
8673 }
8674
8675 /* Parse a conversion-type-id:
8676
8677    conversion-type-id:
8678      type-specifier-seq conversion-declarator [opt]
8679
8680    Returns the TYPE specified.  */
8681
8682 static tree
8683 cp_parser_conversion_type_id (cp_parser* parser)
8684 {
8685   tree attributes;
8686   cp_decl_specifier_seq type_specifiers;
8687   cp_declarator *declarator;
8688   tree type_specified;
8689
8690   /* Parse the attributes.  */
8691   attributes = cp_parser_attributes_opt (parser);
8692   /* Parse the type-specifiers.  */
8693   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8694                                 &type_specifiers);
8695   /* If that didn't work, stop.  */
8696   if (type_specifiers.type == error_mark_node)
8697     return error_mark_node;
8698   /* Parse the conversion-declarator.  */
8699   declarator = cp_parser_conversion_declarator_opt (parser);
8700
8701   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8702                                     /*initialized=*/0, &attributes);
8703   if (attributes)
8704     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8705   return type_specified;
8706 }
8707
8708 /* Parse an (optional) conversion-declarator.
8709
8710    conversion-declarator:
8711      ptr-operator conversion-declarator [opt]
8712
8713    */
8714
8715 static cp_declarator *
8716 cp_parser_conversion_declarator_opt (cp_parser* parser)
8717 {
8718   enum tree_code code;
8719   tree class_type;
8720   cp_cv_quals cv_quals;
8721
8722   /* We don't know if there's a ptr-operator next, or not.  */
8723   cp_parser_parse_tentatively (parser);
8724   /* Try the ptr-operator.  */
8725   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8726   /* If it worked, look for more conversion-declarators.  */
8727   if (cp_parser_parse_definitely (parser))
8728     {
8729       cp_declarator *declarator;
8730
8731       /* Parse another optional declarator.  */
8732       declarator = cp_parser_conversion_declarator_opt (parser);
8733
8734       return cp_parser_make_indirect_declarator
8735         (code, class_type, cv_quals, declarator);
8736    }
8737
8738   return NULL;
8739 }
8740
8741 /* Parse an (optional) ctor-initializer.
8742
8743    ctor-initializer:
8744      : mem-initializer-list
8745
8746    Returns TRUE iff the ctor-initializer was actually present.  */
8747
8748 static bool
8749 cp_parser_ctor_initializer_opt (cp_parser* parser)
8750 {
8751   /* If the next token is not a `:', then there is no
8752      ctor-initializer.  */
8753   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8754     {
8755       /* Do default initialization of any bases and members.  */
8756       if (DECL_CONSTRUCTOR_P (current_function_decl))
8757         finish_mem_initializers (NULL_TREE);
8758
8759       return false;
8760     }
8761
8762   /* Consume the `:' token.  */
8763   cp_lexer_consume_token (parser->lexer);
8764   /* And the mem-initializer-list.  */
8765   cp_parser_mem_initializer_list (parser);
8766
8767   return true;
8768 }
8769
8770 /* Parse a mem-initializer-list.
8771
8772    mem-initializer-list:
8773      mem-initializer ... [opt]
8774      mem-initializer ... [opt] , mem-initializer-list  */
8775
8776 static void
8777 cp_parser_mem_initializer_list (cp_parser* parser)
8778 {
8779   tree mem_initializer_list = NULL_TREE;
8780
8781   /* Let the semantic analysis code know that we are starting the
8782      mem-initializer-list.  */
8783   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8784     error ("only constructors take base initializers");
8785
8786   /* Loop through the list.  */
8787   while (true)
8788     {
8789       tree mem_initializer;
8790
8791       /* Parse the mem-initializer.  */
8792       mem_initializer = cp_parser_mem_initializer (parser);
8793       /* If the next token is a `...', we're expanding member initializers. */
8794       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8795         {
8796           /* Consume the `...'. */
8797           cp_lexer_consume_token (parser->lexer);
8798
8799           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8800              can be expanded but members cannot. */
8801           if (mem_initializer != error_mark_node
8802               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8803             {
8804               error ("cannot expand initializer for member %<%D%>", 
8805                      TREE_PURPOSE (mem_initializer));
8806               mem_initializer = error_mark_node;
8807             }
8808
8809           /* Construct the pack expansion type. */
8810           if (mem_initializer != error_mark_node)
8811             mem_initializer = make_pack_expansion (mem_initializer);
8812         }
8813       /* Add it to the list, unless it was erroneous.  */
8814       if (mem_initializer != error_mark_node)
8815         {
8816           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8817           mem_initializer_list = mem_initializer;
8818         }
8819       /* If the next token is not a `,', we're done.  */
8820       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8821         break;
8822       /* Consume the `,' token.  */
8823       cp_lexer_consume_token (parser->lexer);
8824     }
8825
8826   /* Perform semantic analysis.  */
8827   if (DECL_CONSTRUCTOR_P (current_function_decl))
8828     finish_mem_initializers (mem_initializer_list);
8829 }
8830
8831 /* Parse a mem-initializer.
8832
8833    mem-initializer:
8834      mem-initializer-id ( expression-list [opt] )
8835
8836    GNU extension:
8837
8838    mem-initializer:
8839      ( expression-list [opt] )
8840
8841    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8842    class) or FIELD_DECL (for a non-static data member) to initialize;
8843    the TREE_VALUE is the expression-list.  An empty initialization
8844    list is represented by void_list_node.  */
8845
8846 static tree
8847 cp_parser_mem_initializer (cp_parser* parser)
8848 {
8849   tree mem_initializer_id;
8850   tree expression_list;
8851   tree member;
8852
8853   /* Find out what is being initialized.  */
8854   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8855     {
8856       pedwarn ("anachronistic old-style base class initializer");
8857       mem_initializer_id = NULL_TREE;
8858     }
8859   else
8860     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8861   member = expand_member_init (mem_initializer_id);
8862   if (member && !DECL_P (member))
8863     in_base_initializer = 1;
8864
8865   expression_list
8866     = cp_parser_parenthesized_expression_list (parser, false,
8867                                                /*cast_p=*/false,
8868                                                /*allow_expansion_p=*/true,
8869                                                /*non_constant_p=*/NULL);
8870   if (expression_list == error_mark_node)
8871     return error_mark_node;
8872   if (!expression_list)
8873     expression_list = void_type_node;
8874
8875   in_base_initializer = 0;
8876
8877   return member ? build_tree_list (member, expression_list) : error_mark_node;
8878 }
8879
8880 /* Parse a mem-initializer-id.
8881
8882    mem-initializer-id:
8883      :: [opt] nested-name-specifier [opt] class-name
8884      identifier
8885
8886    Returns a TYPE indicating the class to be initializer for the first
8887    production.  Returns an IDENTIFIER_NODE indicating the data member
8888    to be initialized for the second production.  */
8889
8890 static tree
8891 cp_parser_mem_initializer_id (cp_parser* parser)
8892 {
8893   bool global_scope_p;
8894   bool nested_name_specifier_p;
8895   bool template_p = false;
8896   tree id;
8897
8898   /* `typename' is not allowed in this context ([temp.res]).  */
8899   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8900     {
8901       error ("keyword %<typename%> not allowed in this context (a qualified "
8902              "member initializer is implicitly a type)");
8903       cp_lexer_consume_token (parser->lexer);
8904     }
8905   /* Look for the optional `::' operator.  */
8906   global_scope_p
8907     = (cp_parser_global_scope_opt (parser,
8908                                    /*current_scope_valid_p=*/false)
8909        != NULL_TREE);
8910   /* Look for the optional nested-name-specifier.  The simplest way to
8911      implement:
8912
8913        [temp.res]
8914
8915        The keyword `typename' is not permitted in a base-specifier or
8916        mem-initializer; in these contexts a qualified name that
8917        depends on a template-parameter is implicitly assumed to be a
8918        type name.
8919
8920      is to assume that we have seen the `typename' keyword at this
8921      point.  */
8922   nested_name_specifier_p
8923     = (cp_parser_nested_name_specifier_opt (parser,
8924                                             /*typename_keyword_p=*/true,
8925                                             /*check_dependency_p=*/true,
8926                                             /*type_p=*/true,
8927                                             /*is_declaration=*/true)
8928        != NULL_TREE);
8929   if (nested_name_specifier_p)
8930     template_p = cp_parser_optional_template_keyword (parser);
8931   /* If there is a `::' operator or a nested-name-specifier, then we
8932      are definitely looking for a class-name.  */
8933   if (global_scope_p || nested_name_specifier_p)
8934     return cp_parser_class_name (parser,
8935                                  /*typename_keyword_p=*/true,
8936                                  /*template_keyword_p=*/template_p,
8937                                  none_type,
8938                                  /*check_dependency_p=*/true,
8939                                  /*class_head_p=*/false,
8940                                  /*is_declaration=*/true);
8941   /* Otherwise, we could also be looking for an ordinary identifier.  */
8942   cp_parser_parse_tentatively (parser);
8943   /* Try a class-name.  */
8944   id = cp_parser_class_name (parser,
8945                              /*typename_keyword_p=*/true,
8946                              /*template_keyword_p=*/false,
8947                              none_type,
8948                              /*check_dependency_p=*/true,
8949                              /*class_head_p=*/false,
8950                              /*is_declaration=*/true);
8951   /* If we found one, we're done.  */
8952   if (cp_parser_parse_definitely (parser))
8953     return id;
8954   /* Otherwise, look for an ordinary identifier.  */
8955   return cp_parser_identifier (parser);
8956 }
8957
8958 /* Overloading [gram.over] */
8959
8960 /* Parse an operator-function-id.
8961
8962    operator-function-id:
8963      operator operator
8964
8965    Returns an IDENTIFIER_NODE for the operator which is a
8966    human-readable spelling of the identifier, e.g., `operator +'.  */
8967
8968 static tree
8969 cp_parser_operator_function_id (cp_parser* parser)
8970 {
8971   /* Look for the `operator' keyword.  */
8972   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8973     return error_mark_node;
8974   /* And then the name of the operator itself.  */
8975   return cp_parser_operator (parser);
8976 }
8977
8978 /* Parse an operator.
8979
8980    operator:
8981      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8982      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8983      || ++ -- , ->* -> () []
8984
8985    GNU Extensions:
8986
8987    operator:
8988      <? >? <?= >?=
8989
8990    Returns an IDENTIFIER_NODE for the operator which is a
8991    human-readable spelling of the identifier, e.g., `operator +'.  */
8992
8993 static tree
8994 cp_parser_operator (cp_parser* parser)
8995 {
8996   tree id = NULL_TREE;
8997   cp_token *token;
8998
8999   /* Peek at the next token.  */
9000   token = cp_lexer_peek_token (parser->lexer);
9001   /* Figure out which operator we have.  */
9002   switch (token->type)
9003     {
9004     case CPP_KEYWORD:
9005       {
9006         enum tree_code op;
9007
9008         /* The keyword should be either `new' or `delete'.  */
9009         if (token->keyword == RID_NEW)
9010           op = NEW_EXPR;
9011         else if (token->keyword == RID_DELETE)
9012           op = DELETE_EXPR;
9013         else
9014           break;
9015
9016         /* Consume the `new' or `delete' token.  */
9017         cp_lexer_consume_token (parser->lexer);
9018
9019         /* Peek at the next token.  */
9020         token = cp_lexer_peek_token (parser->lexer);
9021         /* If it's a `[' token then this is the array variant of the
9022            operator.  */
9023         if (token->type == CPP_OPEN_SQUARE)
9024           {
9025             /* Consume the `[' token.  */
9026             cp_lexer_consume_token (parser->lexer);
9027             /* Look for the `]' token.  */
9028             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9029             id = ansi_opname (op == NEW_EXPR
9030                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9031           }
9032         /* Otherwise, we have the non-array variant.  */
9033         else
9034           id = ansi_opname (op);
9035
9036         return id;
9037       }
9038
9039     case CPP_PLUS:
9040       id = ansi_opname (PLUS_EXPR);
9041       break;
9042
9043     case CPP_MINUS:
9044       id = ansi_opname (MINUS_EXPR);
9045       break;
9046
9047     case CPP_MULT:
9048       id = ansi_opname (MULT_EXPR);
9049       break;
9050
9051     case CPP_DIV:
9052       id = ansi_opname (TRUNC_DIV_EXPR);
9053       break;
9054
9055     case CPP_MOD:
9056       id = ansi_opname (TRUNC_MOD_EXPR);
9057       break;
9058
9059     case CPP_XOR:
9060       id = ansi_opname (BIT_XOR_EXPR);
9061       break;
9062
9063     case CPP_AND:
9064       id = ansi_opname (BIT_AND_EXPR);
9065       break;
9066
9067     case CPP_OR:
9068       id = ansi_opname (BIT_IOR_EXPR);
9069       break;
9070
9071     case CPP_COMPL:
9072       id = ansi_opname (BIT_NOT_EXPR);
9073       break;
9074
9075     case CPP_NOT:
9076       id = ansi_opname (TRUTH_NOT_EXPR);
9077       break;
9078
9079     case CPP_EQ:
9080       id = ansi_assopname (NOP_EXPR);
9081       break;
9082
9083     case CPP_LESS:
9084       id = ansi_opname (LT_EXPR);
9085       break;
9086
9087     case CPP_GREATER:
9088       id = ansi_opname (GT_EXPR);
9089       break;
9090
9091     case CPP_PLUS_EQ:
9092       id = ansi_assopname (PLUS_EXPR);
9093       break;
9094
9095     case CPP_MINUS_EQ:
9096       id = ansi_assopname (MINUS_EXPR);
9097       break;
9098
9099     case CPP_MULT_EQ:
9100       id = ansi_assopname (MULT_EXPR);
9101       break;
9102
9103     case CPP_DIV_EQ:
9104       id = ansi_assopname (TRUNC_DIV_EXPR);
9105       break;
9106
9107     case CPP_MOD_EQ:
9108       id = ansi_assopname (TRUNC_MOD_EXPR);
9109       break;
9110
9111     case CPP_XOR_EQ:
9112       id = ansi_assopname (BIT_XOR_EXPR);
9113       break;
9114
9115     case CPP_AND_EQ:
9116       id = ansi_assopname (BIT_AND_EXPR);
9117       break;
9118
9119     case CPP_OR_EQ:
9120       id = ansi_assopname (BIT_IOR_EXPR);
9121       break;
9122
9123     case CPP_LSHIFT:
9124       id = ansi_opname (LSHIFT_EXPR);
9125       break;
9126
9127     case CPP_RSHIFT:
9128       id = ansi_opname (RSHIFT_EXPR);
9129       break;
9130
9131     case CPP_LSHIFT_EQ:
9132       id = ansi_assopname (LSHIFT_EXPR);
9133       break;
9134
9135     case CPP_RSHIFT_EQ:
9136       id = ansi_assopname (RSHIFT_EXPR);
9137       break;
9138
9139     case CPP_EQ_EQ:
9140       id = ansi_opname (EQ_EXPR);
9141       break;
9142
9143     case CPP_NOT_EQ:
9144       id = ansi_opname (NE_EXPR);
9145       break;
9146
9147     case CPP_LESS_EQ:
9148       id = ansi_opname (LE_EXPR);
9149       break;
9150
9151     case CPP_GREATER_EQ:
9152       id = ansi_opname (GE_EXPR);
9153       break;
9154
9155     case CPP_AND_AND:
9156       id = ansi_opname (TRUTH_ANDIF_EXPR);
9157       break;
9158
9159     case CPP_OR_OR:
9160       id = ansi_opname (TRUTH_ORIF_EXPR);
9161       break;
9162
9163     case CPP_PLUS_PLUS:
9164       id = ansi_opname (POSTINCREMENT_EXPR);
9165       break;
9166
9167     case CPP_MINUS_MINUS:
9168       id = ansi_opname (PREDECREMENT_EXPR);
9169       break;
9170
9171     case CPP_COMMA:
9172       id = ansi_opname (COMPOUND_EXPR);
9173       break;
9174
9175     case CPP_DEREF_STAR:
9176       id = ansi_opname (MEMBER_REF);
9177       break;
9178
9179     case CPP_DEREF:
9180       id = ansi_opname (COMPONENT_REF);
9181       break;
9182
9183     case CPP_OPEN_PAREN:
9184       /* Consume the `('.  */
9185       cp_lexer_consume_token (parser->lexer);
9186       /* Look for the matching `)'.  */
9187       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9188       return ansi_opname (CALL_EXPR);
9189
9190     case CPP_OPEN_SQUARE:
9191       /* Consume the `['.  */
9192       cp_lexer_consume_token (parser->lexer);
9193       /* Look for the matching `]'.  */
9194       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9195       return ansi_opname (ARRAY_REF);
9196
9197     default:
9198       /* Anything else is an error.  */
9199       break;
9200     }
9201
9202   /* If we have selected an identifier, we need to consume the
9203      operator token.  */
9204   if (id)
9205     cp_lexer_consume_token (parser->lexer);
9206   /* Otherwise, no valid operator name was present.  */
9207   else
9208     {
9209       cp_parser_error (parser, "expected operator");
9210       id = error_mark_node;
9211     }
9212
9213   return id;
9214 }
9215
9216 /* Parse a template-declaration.
9217
9218    template-declaration:
9219      export [opt] template < template-parameter-list > declaration
9220
9221    If MEMBER_P is TRUE, this template-declaration occurs within a
9222    class-specifier.
9223
9224    The grammar rule given by the standard isn't correct.  What
9225    is really meant is:
9226
9227    template-declaration:
9228      export [opt] template-parameter-list-seq
9229        decl-specifier-seq [opt] init-declarator [opt] ;
9230      export [opt] template-parameter-list-seq
9231        function-definition
9232
9233    template-parameter-list-seq:
9234      template-parameter-list-seq [opt]
9235      template < template-parameter-list >  */
9236
9237 static void
9238 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9239 {
9240   /* Check for `export'.  */
9241   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9242     {
9243       /* Consume the `export' token.  */
9244       cp_lexer_consume_token (parser->lexer);
9245       /* Warn that we do not support `export'.  */
9246       warning (0, "keyword %<export%> not implemented, and will be ignored");
9247     }
9248
9249   cp_parser_template_declaration_after_export (parser, member_p);
9250 }
9251
9252 /* Parse a template-parameter-list.
9253
9254    template-parameter-list:
9255      template-parameter
9256      template-parameter-list , template-parameter
9257
9258    Returns a TREE_LIST.  Each node represents a template parameter.
9259    The nodes are connected via their TREE_CHAINs.  */
9260
9261 static tree
9262 cp_parser_template_parameter_list (cp_parser* parser)
9263 {
9264   tree parameter_list = NULL_TREE;
9265
9266   begin_template_parm_list ();
9267   while (true)
9268     {
9269       tree parameter;
9270       cp_token *token;
9271       bool is_non_type;
9272       bool is_parameter_pack;
9273
9274       /* Parse the template-parameter.  */
9275       parameter = cp_parser_template_parameter (parser, 
9276                                                 &is_non_type,
9277                                                 &is_parameter_pack);
9278       /* Add it to the list.  */
9279       if (parameter != error_mark_node)
9280         parameter_list = process_template_parm (parameter_list,
9281                                                 parameter,
9282                                                 is_non_type,
9283                                                 is_parameter_pack);
9284       else
9285        {
9286          tree err_parm = build_tree_list (parameter, parameter);
9287          TREE_VALUE (err_parm) = error_mark_node;
9288          parameter_list = chainon (parameter_list, err_parm);
9289        }
9290
9291       /* Peek at the next token.  */
9292       token = cp_lexer_peek_token (parser->lexer);
9293       /* If it's not a `,', we're done.  */
9294       if (token->type != CPP_COMMA)
9295         break;
9296       /* Otherwise, consume the `,' token.  */
9297       cp_lexer_consume_token (parser->lexer);
9298     }
9299
9300   return end_template_parm_list (parameter_list);
9301 }
9302
9303 /* Parse a template-parameter.
9304
9305    template-parameter:
9306      type-parameter
9307      parameter-declaration
9308
9309    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9310    the parameter.  The TREE_PURPOSE is the default value, if any.
9311    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9312    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9313    set to true iff this parameter is a parameter pack. */
9314
9315 static tree
9316 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9317                               bool *is_parameter_pack)
9318 {
9319   cp_token *token;
9320   cp_parameter_declarator *parameter_declarator;
9321   cp_declarator *id_declarator;
9322   tree parm;
9323
9324   /* Assume it is a type parameter or a template parameter.  */
9325   *is_non_type = false;
9326   /* Assume it not a parameter pack. */
9327   *is_parameter_pack = false;
9328   /* Peek at the next token.  */
9329   token = cp_lexer_peek_token (parser->lexer);
9330   /* If it is `class' or `template', we have a type-parameter.  */
9331   if (token->keyword == RID_TEMPLATE)
9332     return cp_parser_type_parameter (parser, is_parameter_pack);
9333   /* If it is `class' or `typename' we do not know yet whether it is a
9334      type parameter or a non-type parameter.  Consider:
9335
9336        template <typename T, typename T::X X> ...
9337
9338      or:
9339
9340        template <class C, class D*> ...
9341
9342      Here, the first parameter is a type parameter, and the second is
9343      a non-type parameter.  We can tell by looking at the token after
9344      the identifier -- if it is a `,', `=', or `>' then we have a type
9345      parameter.  */
9346   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9347     {
9348       /* Peek at the token after `class' or `typename'.  */
9349       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9350       /* If it's an ellipsis, we have a template type parameter
9351          pack. */
9352       if (token->type == CPP_ELLIPSIS)
9353         return cp_parser_type_parameter (parser, is_parameter_pack);
9354       /* If it's an identifier, skip it.  */
9355       if (token->type == CPP_NAME)
9356         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9357       /* Now, see if the token looks like the end of a template
9358          parameter.  */
9359       if (token->type == CPP_COMMA
9360           || token->type == CPP_EQ
9361           || token->type == CPP_GREATER)
9362         return cp_parser_type_parameter (parser, is_parameter_pack);
9363     }
9364
9365   /* Otherwise, it is a non-type parameter.
9366
9367      [temp.param]
9368
9369      When parsing a default template-argument for a non-type
9370      template-parameter, the first non-nested `>' is taken as the end
9371      of the template parameter-list rather than a greater-than
9372      operator.  */
9373   *is_non_type = true;
9374   parameter_declarator
9375      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9376                                         /*parenthesized_p=*/NULL);
9377
9378   /* If the parameter declaration is marked as a parameter pack, set
9379      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9380      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9381      grokdeclarator. */
9382   if (parameter_declarator
9383       && parameter_declarator->declarator
9384       && parameter_declarator->declarator->parameter_pack_p)
9385     {
9386       *is_parameter_pack = true;
9387       parameter_declarator->declarator->parameter_pack_p = false;
9388     }
9389
9390   /* If the next token is an ellipsis, and we don't already have it
9391      marked as a parameter pack, then we have a parameter pack (that
9392      has no declarator).  */
9393   if (!*is_parameter_pack
9394       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9395       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9396     {
9397       /* Consume the `...'.  */
9398       cp_lexer_consume_token (parser->lexer);
9399       maybe_warn_variadic_templates ();
9400       
9401       *is_parameter_pack = true;
9402
9403       /* Parameter packs cannot have default arguments.  However, a
9404          user may try to do so, so we'll parse them and give an
9405          appropriate diagnostic here.  */
9406       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9407         {
9408           /* Consume the `='.  */
9409           cp_lexer_consume_token (parser->lexer);
9410
9411           /* Find the name of the parameter pack.  */     
9412           id_declarator = parameter_declarator->declarator;
9413           while (id_declarator && id_declarator->kind != cdk_id)
9414             id_declarator = id_declarator->declarator;
9415           
9416           if (id_declarator && id_declarator->kind == cdk_id)
9417             error ("template parameter pack %qD cannot have a default argument",
9418                    id_declarator->u.id.unqualified_name);
9419           else
9420             error ("template parameter pack cannot have a default argument");
9421
9422           /* Parse the default argument, but throw away the result.  */
9423           cp_parser_default_argument (parser, /*template_parm_p=*/true);
9424         }
9425     }
9426
9427   parm = grokdeclarator (parameter_declarator->declarator,
9428                          &parameter_declarator->decl_specifiers,
9429                          PARM, /*initialized=*/0,
9430                          /*attrlist=*/NULL);
9431   if (parm == error_mark_node)
9432     return error_mark_node;
9433
9434   return build_tree_list (parameter_declarator->default_argument, parm);
9435 }
9436
9437 /* Parse a type-parameter.
9438
9439    type-parameter:
9440      class identifier [opt]
9441      class identifier [opt] = type-id
9442      typename identifier [opt]
9443      typename identifier [opt] = type-id
9444      template < template-parameter-list > class identifier [opt]
9445      template < template-parameter-list > class identifier [opt]
9446        = id-expression
9447
9448    GNU Extension (variadic templates):
9449
9450    type-parameter:
9451      class ... identifier [opt]
9452      typename ... identifier [opt]
9453
9454    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9455    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9456    the declaration of the parameter.
9457
9458    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9459
9460 static tree
9461 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9462 {
9463   cp_token *token;
9464   tree parameter;
9465
9466   /* Look for a keyword to tell us what kind of parameter this is.  */
9467   token = cp_parser_require (parser, CPP_KEYWORD,
9468                              "`class', `typename', or `template'");
9469   if (!token)
9470     return error_mark_node;
9471
9472   switch (token->keyword)
9473     {
9474     case RID_CLASS:
9475     case RID_TYPENAME:
9476       {
9477         tree identifier;
9478         tree default_argument;
9479
9480         /* If the next token is an ellipsis, we have a template
9481            argument pack. */
9482         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9483           {
9484             /* Consume the `...' token. */
9485             cp_lexer_consume_token (parser->lexer);
9486             maybe_warn_variadic_templates ();
9487
9488             *is_parameter_pack = true;
9489           }
9490
9491         /* If the next token is an identifier, then it names the
9492            parameter.  */
9493         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9494           identifier = cp_parser_identifier (parser);
9495         else
9496           identifier = NULL_TREE;
9497
9498         /* Create the parameter.  */
9499         parameter = finish_template_type_parm (class_type_node, identifier);
9500
9501         /* If the next token is an `=', we have a default argument.  */
9502         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9503           {
9504             /* Consume the `=' token.  */
9505             cp_lexer_consume_token (parser->lexer);
9506             /* Parse the default-argument.  */
9507             push_deferring_access_checks (dk_no_deferred);
9508             default_argument = cp_parser_type_id (parser);
9509
9510             /* Template parameter packs cannot have default
9511                arguments. */
9512             if (*is_parameter_pack)
9513               {
9514                 if (identifier)
9515                   error ("template parameter pack %qD cannot have a default argument", 
9516                          identifier);
9517                 else
9518                   error ("template parameter packs cannot have default arguments");
9519                 default_argument = NULL_TREE;
9520               }
9521             pop_deferring_access_checks ();
9522           }
9523         else
9524           default_argument = NULL_TREE;
9525
9526         /* Create the combined representation of the parameter and the
9527            default argument.  */
9528         parameter = build_tree_list (default_argument, parameter);
9529       }
9530       break;
9531
9532     case RID_TEMPLATE:
9533       {
9534         tree parameter_list;
9535         tree identifier;
9536         tree default_argument;
9537
9538         /* Look for the `<'.  */
9539         cp_parser_require (parser, CPP_LESS, "`<'");
9540         /* Parse the template-parameter-list.  */
9541         parameter_list = cp_parser_template_parameter_list (parser);
9542         /* Look for the `>'.  */
9543         cp_parser_require (parser, CPP_GREATER, "`>'");
9544         /* Look for the `class' keyword.  */
9545         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9546         /* If the next token is an ellipsis, we have a template
9547            argument pack. */
9548         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9549           {
9550             /* Consume the `...' token. */
9551             cp_lexer_consume_token (parser->lexer);
9552             maybe_warn_variadic_templates ();
9553
9554             *is_parameter_pack = true;
9555           }
9556         /* If the next token is an `=', then there is a
9557            default-argument.  If the next token is a `>', we are at
9558            the end of the parameter-list.  If the next token is a `,',
9559            then we are at the end of this parameter.  */
9560         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9561             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9562             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9563           {
9564             identifier = cp_parser_identifier (parser);
9565             /* Treat invalid names as if the parameter were nameless.  */
9566             if (identifier == error_mark_node)
9567               identifier = NULL_TREE;
9568           }
9569         else
9570           identifier = NULL_TREE;
9571
9572         /* Create the template parameter.  */
9573         parameter = finish_template_template_parm (class_type_node,
9574                                                    identifier);
9575
9576         /* If the next token is an `=', then there is a
9577            default-argument.  */
9578         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9579           {
9580             bool is_template;
9581
9582             /* Consume the `='.  */
9583             cp_lexer_consume_token (parser->lexer);
9584             /* Parse the id-expression.  */
9585             push_deferring_access_checks (dk_no_deferred);
9586             default_argument
9587               = cp_parser_id_expression (parser,
9588                                          /*template_keyword_p=*/false,
9589                                          /*check_dependency_p=*/true,
9590                                          /*template_p=*/&is_template,
9591                                          /*declarator_p=*/false,
9592                                          /*optional_p=*/false);
9593             if (TREE_CODE (default_argument) == TYPE_DECL)
9594               /* If the id-expression was a template-id that refers to
9595                  a template-class, we already have the declaration here,
9596                  so no further lookup is needed.  */
9597                  ;
9598             else
9599               /* Look up the name.  */
9600               default_argument
9601                 = cp_parser_lookup_name (parser, default_argument,
9602                                          none_type,
9603                                          /*is_template=*/is_template,
9604                                          /*is_namespace=*/false,
9605                                          /*check_dependency=*/true,
9606                                          /*ambiguous_decls=*/NULL);
9607             /* See if the default argument is valid.  */
9608             default_argument
9609               = check_template_template_default_arg (default_argument);
9610
9611             /* Template parameter packs cannot have default
9612                arguments. */
9613             if (*is_parameter_pack)
9614               {
9615                 if (identifier)
9616                   error ("template parameter pack %qD cannot have a default argument", 
9617                          identifier);
9618                 else
9619                   error ("template parameter packs cannot have default arguments");
9620                 default_argument = NULL_TREE;
9621               }
9622             pop_deferring_access_checks ();
9623           }
9624         else
9625           default_argument = NULL_TREE;
9626
9627         /* Create the combined representation of the parameter and the
9628            default argument.  */
9629         parameter = build_tree_list (default_argument, parameter);
9630       }
9631       break;
9632
9633     default:
9634       gcc_unreachable ();
9635       break;
9636     }
9637
9638   return parameter;
9639 }
9640
9641 /* Parse a template-id.
9642
9643    template-id:
9644      template-name < template-argument-list [opt] >
9645
9646    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9647    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9648    returned.  Otherwise, if the template-name names a function, or set
9649    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9650    names a class, returns a TYPE_DECL for the specialization.
9651
9652    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9653    uninstantiated templates.  */
9654
9655 static tree
9656 cp_parser_template_id (cp_parser *parser,
9657                        bool template_keyword_p,
9658                        bool check_dependency_p,
9659                        bool is_declaration)
9660 {
9661   int i;
9662   tree template;
9663   tree arguments;
9664   tree template_id;
9665   cp_token_position start_of_id = 0;
9666   deferred_access_check *chk;
9667   VEC (deferred_access_check,gc) *access_check;
9668   cp_token *next_token, *next_token_2;
9669   bool is_identifier;
9670
9671   /* If the next token corresponds to a template-id, there is no need
9672      to reparse it.  */
9673   next_token = cp_lexer_peek_token (parser->lexer);
9674   if (next_token->type == CPP_TEMPLATE_ID)
9675     {
9676       struct tree_check *check_value;
9677
9678       /* Get the stored value.  */
9679       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9680       /* Perform any access checks that were deferred.  */
9681       access_check = check_value->checks;
9682       if (access_check)
9683         {
9684           for (i = 0 ;
9685                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9686                ++i)
9687             {
9688               perform_or_defer_access_check (chk->binfo,
9689                                              chk->decl,
9690                                              chk->diag_decl);
9691             }
9692         }
9693       /* Return the stored value.  */
9694       return check_value->value;
9695     }
9696
9697   /* Avoid performing name lookup if there is no possibility of
9698      finding a template-id.  */
9699   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9700       || (next_token->type == CPP_NAME
9701           && !cp_parser_nth_token_starts_template_argument_list_p
9702                (parser, 2)))
9703     {
9704       cp_parser_error (parser, "expected template-id");
9705       return error_mark_node;
9706     }
9707
9708   /* Remember where the template-id starts.  */
9709   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9710     start_of_id = cp_lexer_token_position (parser->lexer, false);
9711
9712   push_deferring_access_checks (dk_deferred);
9713
9714   /* Parse the template-name.  */
9715   is_identifier = false;
9716   template = cp_parser_template_name (parser, template_keyword_p,
9717                                       check_dependency_p,
9718                                       is_declaration,
9719                                       &is_identifier);
9720   if (template == error_mark_node || is_identifier)
9721     {
9722       pop_deferring_access_checks ();
9723       return template;
9724     }
9725
9726   /* If we find the sequence `[:' after a template-name, it's probably
9727      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9728      parse correctly the argument list.  */
9729   next_token = cp_lexer_peek_token (parser->lexer);
9730   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9731   if (next_token->type == CPP_OPEN_SQUARE
9732       && next_token->flags & DIGRAPH
9733       && next_token_2->type == CPP_COLON
9734       && !(next_token_2->flags & PREV_WHITE))
9735     {
9736       cp_parser_parse_tentatively (parser);
9737       /* Change `:' into `::'.  */
9738       next_token_2->type = CPP_SCOPE;
9739       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9740          CPP_LESS.  */
9741       cp_lexer_consume_token (parser->lexer);
9742       /* Parse the arguments.  */
9743       arguments = cp_parser_enclosed_template_argument_list (parser);
9744       if (!cp_parser_parse_definitely (parser))
9745         {
9746           /* If we couldn't parse an argument list, then we revert our changes
9747              and return simply an error. Maybe this is not a template-id
9748              after all.  */
9749           next_token_2->type = CPP_COLON;
9750           cp_parser_error (parser, "expected %<<%>");
9751           pop_deferring_access_checks ();
9752           return error_mark_node;
9753         }
9754       /* Otherwise, emit an error about the invalid digraph, but continue
9755          parsing because we got our argument list.  */
9756       pedwarn ("%<<::%> cannot begin a template-argument list");
9757       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9758               "between %<<%> and %<::%>");
9759       if (!flag_permissive)
9760         {
9761           static bool hint;
9762           if (!hint)
9763             {
9764               inform ("(if you use -fpermissive G++ will accept your code)");
9765               hint = true;
9766             }
9767         }
9768     }
9769   else
9770     {
9771       /* Look for the `<' that starts the template-argument-list.  */
9772       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9773         {
9774           pop_deferring_access_checks ();
9775           return error_mark_node;
9776         }
9777       /* Parse the arguments.  */
9778       arguments = cp_parser_enclosed_template_argument_list (parser);
9779     }
9780
9781   /* Build a representation of the specialization.  */
9782   if (TREE_CODE (template) == IDENTIFIER_NODE)
9783     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9784   else if (DECL_CLASS_TEMPLATE_P (template)
9785            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9786     {
9787       bool entering_scope;
9788       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9789          template (rather than some instantiation thereof) only if
9790          is not nested within some other construct.  For example, in
9791          "template <typename T> void f(T) { A<T>::", A<T> is just an
9792          instantiation of A.  */
9793       entering_scope = (template_parm_scope_p ()
9794                         && cp_lexer_next_token_is (parser->lexer,
9795                                                    CPP_SCOPE));
9796       template_id
9797         = finish_template_type (template, arguments, entering_scope);
9798     }
9799   else
9800     {
9801       /* If it's not a class-template or a template-template, it should be
9802          a function-template.  */
9803       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9804                    || TREE_CODE (template) == OVERLOAD
9805                    || BASELINK_P (template)));
9806
9807       template_id = lookup_template_function (template, arguments);
9808     }
9809
9810   /* If parsing tentatively, replace the sequence of tokens that makes
9811      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9812      should we re-parse the token stream, we will not have to repeat
9813      the effort required to do the parse, nor will we issue duplicate
9814      error messages about problems during instantiation of the
9815      template.  */
9816   if (start_of_id)
9817     {
9818       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9819
9820       /* Reset the contents of the START_OF_ID token.  */
9821       token->type = CPP_TEMPLATE_ID;
9822       /* Retrieve any deferred checks.  Do not pop this access checks yet
9823          so the memory will not be reclaimed during token replacing below.  */
9824       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9825       token->u.tree_check_value->value = template_id;
9826       token->u.tree_check_value->checks = get_deferred_access_checks ();
9827       token->keyword = RID_MAX;
9828
9829       /* Purge all subsequent tokens.  */
9830       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9831
9832       /* ??? Can we actually assume that, if template_id ==
9833          error_mark_node, we will have issued a diagnostic to the
9834          user, as opposed to simply marking the tentative parse as
9835          failed?  */
9836       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9837         error ("parse error in template argument list");
9838     }
9839
9840   pop_deferring_access_checks ();
9841   return template_id;
9842 }
9843
9844 /* Parse a template-name.
9845
9846    template-name:
9847      identifier
9848
9849    The standard should actually say:
9850
9851    template-name:
9852      identifier
9853      operator-function-id
9854
9855    A defect report has been filed about this issue.
9856
9857    A conversion-function-id cannot be a template name because they cannot
9858    be part of a template-id. In fact, looking at this code:
9859
9860    a.operator K<int>()
9861
9862    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9863    It is impossible to call a templated conversion-function-id with an
9864    explicit argument list, since the only allowed template parameter is
9865    the type to which it is converting.
9866
9867    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9868    `template' keyword, in a construction like:
9869
9870      T::template f<3>()
9871
9872    In that case `f' is taken to be a template-name, even though there
9873    is no way of knowing for sure.
9874
9875    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9876    name refers to a set of overloaded functions, at least one of which
9877    is a template, or an IDENTIFIER_NODE with the name of the template,
9878    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9879    names are looked up inside uninstantiated templates.  */
9880
9881 static tree
9882 cp_parser_template_name (cp_parser* parser,
9883                          bool template_keyword_p,
9884                          bool check_dependency_p,
9885                          bool is_declaration,
9886                          bool *is_identifier)
9887 {
9888   tree identifier;
9889   tree decl;
9890   tree fns;
9891
9892   /* If the next token is `operator', then we have either an
9893      operator-function-id or a conversion-function-id.  */
9894   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9895     {
9896       /* We don't know whether we're looking at an
9897          operator-function-id or a conversion-function-id.  */
9898       cp_parser_parse_tentatively (parser);
9899       /* Try an operator-function-id.  */
9900       identifier = cp_parser_operator_function_id (parser);
9901       /* If that didn't work, try a conversion-function-id.  */
9902       if (!cp_parser_parse_definitely (parser))
9903         {
9904           cp_parser_error (parser, "expected template-name");
9905           return error_mark_node;
9906         }
9907     }
9908   /* Look for the identifier.  */
9909   else
9910     identifier = cp_parser_identifier (parser);
9911
9912   /* If we didn't find an identifier, we don't have a template-id.  */
9913   if (identifier == error_mark_node)
9914     return error_mark_node;
9915
9916   /* If the name immediately followed the `template' keyword, then it
9917      is a template-name.  However, if the next token is not `<', then
9918      we do not treat it as a template-name, since it is not being used
9919      as part of a template-id.  This enables us to handle constructs
9920      like:
9921
9922        template <typename T> struct S { S(); };
9923        template <typename T> S<T>::S();
9924
9925      correctly.  We would treat `S' as a template -- if it were `S<T>'
9926      -- but we do not if there is no `<'.  */
9927
9928   if (processing_template_decl
9929       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9930     {
9931       /* In a declaration, in a dependent context, we pretend that the
9932          "template" keyword was present in order to improve error
9933          recovery.  For example, given:
9934
9935            template <typename T> void f(T::X<int>);
9936
9937          we want to treat "X<int>" as a template-id.  */
9938       if (is_declaration
9939           && !template_keyword_p
9940           && parser->scope && TYPE_P (parser->scope)
9941           && check_dependency_p
9942           && dependent_type_p (parser->scope)
9943           /* Do not do this for dtors (or ctors), since they never
9944              need the template keyword before their name.  */
9945           && !constructor_name_p (identifier, parser->scope))
9946         {
9947           cp_token_position start = 0;
9948
9949           /* Explain what went wrong.  */
9950           error ("non-template %qD used as template", identifier);
9951           inform ("use %<%T::template %D%> to indicate that it is a template",
9952                   parser->scope, identifier);
9953           /* If parsing tentatively, find the location of the "<" token.  */
9954           if (cp_parser_simulate_error (parser))
9955             start = cp_lexer_token_position (parser->lexer, true);
9956           /* Parse the template arguments so that we can issue error
9957              messages about them.  */
9958           cp_lexer_consume_token (parser->lexer);
9959           cp_parser_enclosed_template_argument_list (parser);
9960           /* Skip tokens until we find a good place from which to
9961              continue parsing.  */
9962           cp_parser_skip_to_closing_parenthesis (parser,
9963                                                  /*recovering=*/true,
9964                                                  /*or_comma=*/true,
9965                                                  /*consume_paren=*/false);
9966           /* If parsing tentatively, permanently remove the
9967              template argument list.  That will prevent duplicate
9968              error messages from being issued about the missing
9969              "template" keyword.  */
9970           if (start)
9971             cp_lexer_purge_tokens_after (parser->lexer, start);
9972           if (is_identifier)
9973             *is_identifier = true;
9974           return identifier;
9975         }
9976
9977       /* If the "template" keyword is present, then there is generally
9978          no point in doing name-lookup, so we just return IDENTIFIER.
9979          But, if the qualifying scope is non-dependent then we can
9980          (and must) do name-lookup normally.  */
9981       if (template_keyword_p
9982           && (!parser->scope
9983               || (TYPE_P (parser->scope)
9984                   && dependent_type_p (parser->scope))))
9985         return identifier;
9986     }
9987
9988   /* Look up the name.  */
9989   decl = cp_parser_lookup_name (parser, identifier,
9990                                 none_type,
9991                                 /*is_template=*/false,
9992                                 /*is_namespace=*/false,
9993                                 check_dependency_p,
9994                                 /*ambiguous_decls=*/NULL);
9995   decl = maybe_get_template_decl_from_type_decl (decl);
9996
9997   /* If DECL is a template, then the name was a template-name.  */
9998   if (TREE_CODE (decl) == TEMPLATE_DECL)
9999     ;
10000   else
10001     {
10002       tree fn = NULL_TREE;
10003
10004       /* The standard does not explicitly indicate whether a name that
10005          names a set of overloaded declarations, some of which are
10006          templates, is a template-name.  However, such a name should
10007          be a template-name; otherwise, there is no way to form a
10008          template-id for the overloaded templates.  */
10009       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10010       if (TREE_CODE (fns) == OVERLOAD)
10011         for (fn = fns; fn; fn = OVL_NEXT (fn))
10012           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10013             break;
10014
10015       if (!fn)
10016         {
10017           /* The name does not name a template.  */
10018           cp_parser_error (parser, "expected template-name");
10019           return error_mark_node;
10020         }
10021     }
10022
10023   /* If DECL is dependent, and refers to a function, then just return
10024      its name; we will look it up again during template instantiation.  */
10025   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10026     {
10027       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10028       if (TYPE_P (scope) && dependent_type_p (scope))
10029         return identifier;
10030     }
10031
10032   return decl;
10033 }
10034
10035 /* Parse a template-argument-list.
10036
10037    template-argument-list:
10038      template-argument ... [opt]
10039      template-argument-list , template-argument ... [opt]
10040
10041    Returns a TREE_VEC containing the arguments.  */
10042
10043 static tree
10044 cp_parser_template_argument_list (cp_parser* parser)
10045 {
10046   tree fixed_args[10];
10047   unsigned n_args = 0;
10048   unsigned alloced = 10;
10049   tree *arg_ary = fixed_args;
10050   tree vec;
10051   bool saved_in_template_argument_list_p;
10052   bool saved_ice_p;
10053   bool saved_non_ice_p;
10054
10055   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10056   parser->in_template_argument_list_p = true;
10057   /* Even if the template-id appears in an integral
10058      constant-expression, the contents of the argument list do
10059      not.  */
10060   saved_ice_p = parser->integral_constant_expression_p;
10061   parser->integral_constant_expression_p = false;
10062   saved_non_ice_p = parser->non_integral_constant_expression_p;
10063   parser->non_integral_constant_expression_p = false;
10064   /* Parse the arguments.  */
10065   do
10066     {
10067       tree argument;
10068
10069       if (n_args)
10070         /* Consume the comma.  */
10071         cp_lexer_consume_token (parser->lexer);
10072
10073       /* Parse the template-argument.  */
10074       argument = cp_parser_template_argument (parser);
10075
10076       /* If the next token is an ellipsis, we're expanding a template
10077          argument pack. */
10078       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10079         {
10080           /* Consume the `...' token. */
10081           cp_lexer_consume_token (parser->lexer);
10082
10083           /* Make the argument into a TYPE_PACK_EXPANSION or
10084              EXPR_PACK_EXPANSION. */
10085           argument = make_pack_expansion (argument);
10086         }
10087
10088       if (n_args == alloced)
10089         {
10090           alloced *= 2;
10091
10092           if (arg_ary == fixed_args)
10093             {
10094               arg_ary = XNEWVEC (tree, alloced);
10095               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10096             }
10097           else
10098             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10099         }
10100       arg_ary[n_args++] = argument;
10101     }
10102   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10103
10104   vec = make_tree_vec (n_args);
10105
10106   while (n_args--)
10107     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10108
10109   if (arg_ary != fixed_args)
10110     free (arg_ary);
10111   parser->non_integral_constant_expression_p = saved_non_ice_p;
10112   parser->integral_constant_expression_p = saved_ice_p;
10113   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10114   return vec;
10115 }
10116
10117 /* Parse a template-argument.
10118
10119    template-argument:
10120      assignment-expression
10121      type-id
10122      id-expression
10123
10124    The representation is that of an assignment-expression, type-id, or
10125    id-expression -- except that the qualified id-expression is
10126    evaluated, so that the value returned is either a DECL or an
10127    OVERLOAD.
10128
10129    Although the standard says "assignment-expression", it forbids
10130    throw-expressions or assignments in the template argument.
10131    Therefore, we use "conditional-expression" instead.  */
10132
10133 static tree
10134 cp_parser_template_argument (cp_parser* parser)
10135 {
10136   tree argument;
10137   bool template_p;
10138   bool address_p;
10139   bool maybe_type_id = false;
10140   cp_token *token;
10141   cp_id_kind idk;
10142
10143   /* There's really no way to know what we're looking at, so we just
10144      try each alternative in order.
10145
10146        [temp.arg]
10147
10148        In a template-argument, an ambiguity between a type-id and an
10149        expression is resolved to a type-id, regardless of the form of
10150        the corresponding template-parameter.
10151
10152      Therefore, we try a type-id first.  */
10153   cp_parser_parse_tentatively (parser);
10154   argument = cp_parser_type_id (parser);
10155   /* If there was no error parsing the type-id but the next token is a '>>',
10156      we probably found a typo for '> >'. But there are type-id which are
10157      also valid expressions. For instance:
10158
10159      struct X { int operator >> (int); };
10160      template <int V> struct Foo {};
10161      Foo<X () >> 5> r;
10162
10163      Here 'X()' is a valid type-id of a function type, but the user just
10164      wanted to write the expression "X() >> 5". Thus, we remember that we
10165      found a valid type-id, but we still try to parse the argument as an
10166      expression to see what happens.  */
10167   if (!cp_parser_error_occurred (parser)
10168       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10169     {
10170       maybe_type_id = true;
10171       cp_parser_abort_tentative_parse (parser);
10172     }
10173   else
10174     {
10175       /* If the next token isn't a `,' or a `>', then this argument wasn't
10176       really finished. This means that the argument is not a valid
10177       type-id.  */
10178       if (!cp_parser_next_token_ends_template_argument_p (parser))
10179         cp_parser_error (parser, "expected template-argument");
10180       /* If that worked, we're done.  */
10181       if (cp_parser_parse_definitely (parser))
10182         return argument;
10183     }
10184   /* We're still not sure what the argument will be.  */
10185   cp_parser_parse_tentatively (parser);
10186   /* Try a template.  */
10187   argument = cp_parser_id_expression (parser,
10188                                       /*template_keyword_p=*/false,
10189                                       /*check_dependency_p=*/true,
10190                                       &template_p,
10191                                       /*declarator_p=*/false,
10192                                       /*optional_p=*/false);
10193   /* If the next token isn't a `,' or a `>', then this argument wasn't
10194      really finished.  */
10195   if (!cp_parser_next_token_ends_template_argument_p (parser))
10196     cp_parser_error (parser, "expected template-argument");
10197   if (!cp_parser_error_occurred (parser))
10198     {
10199       /* Figure out what is being referred to.  If the id-expression
10200          was for a class template specialization, then we will have a
10201          TYPE_DECL at this point.  There is no need to do name lookup
10202          at this point in that case.  */
10203       if (TREE_CODE (argument) != TYPE_DECL)
10204         argument = cp_parser_lookup_name (parser, argument,
10205                                           none_type,
10206                                           /*is_template=*/template_p,
10207                                           /*is_namespace=*/false,
10208                                           /*check_dependency=*/true,
10209                                           /*ambiguous_decls=*/NULL);
10210       if (TREE_CODE (argument) != TEMPLATE_DECL
10211           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10212         cp_parser_error (parser, "expected template-name");
10213     }
10214   if (cp_parser_parse_definitely (parser))
10215     return argument;
10216   /* It must be a non-type argument.  There permitted cases are given
10217      in [temp.arg.nontype]:
10218
10219      -- an integral constant-expression of integral or enumeration
10220         type; or
10221
10222      -- the name of a non-type template-parameter; or
10223
10224      -- the name of an object or function with external linkage...
10225
10226      -- the address of an object or function with external linkage...
10227
10228      -- a pointer to member...  */
10229   /* Look for a non-type template parameter.  */
10230   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10231     {
10232       cp_parser_parse_tentatively (parser);
10233       argument = cp_parser_primary_expression (parser,
10234                                                /*adress_p=*/false,
10235                                                /*cast_p=*/false,
10236                                                /*template_arg_p=*/true,
10237                                                &idk);
10238       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10239           || !cp_parser_next_token_ends_template_argument_p (parser))
10240         cp_parser_simulate_error (parser);
10241       if (cp_parser_parse_definitely (parser))
10242         return argument;
10243     }
10244
10245   /* If the next token is "&", the argument must be the address of an
10246      object or function with external linkage.  */
10247   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10248   if (address_p)
10249     cp_lexer_consume_token (parser->lexer);
10250   /* See if we might have an id-expression.  */
10251   token = cp_lexer_peek_token (parser->lexer);
10252   if (token->type == CPP_NAME
10253       || token->keyword == RID_OPERATOR
10254       || token->type == CPP_SCOPE
10255       || token->type == CPP_TEMPLATE_ID
10256       || token->type == CPP_NESTED_NAME_SPECIFIER)
10257     {
10258       cp_parser_parse_tentatively (parser);
10259       argument = cp_parser_primary_expression (parser,
10260                                                address_p,
10261                                                /*cast_p=*/false,
10262                                                /*template_arg_p=*/true,
10263                                                &idk);
10264       if (cp_parser_error_occurred (parser)
10265           || !cp_parser_next_token_ends_template_argument_p (parser))
10266         cp_parser_abort_tentative_parse (parser);
10267       else
10268         {
10269           if (TREE_CODE (argument) == INDIRECT_REF)
10270             {
10271               gcc_assert (REFERENCE_REF_P (argument));
10272               argument = TREE_OPERAND (argument, 0);
10273             }
10274
10275           if (TREE_CODE (argument) == VAR_DECL)
10276             {
10277               /* A variable without external linkage might still be a
10278                  valid constant-expression, so no error is issued here
10279                  if the external-linkage check fails.  */
10280               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10281                 cp_parser_simulate_error (parser);
10282             }
10283           else if (is_overloaded_fn (argument))
10284             /* All overloaded functions are allowed; if the external
10285                linkage test does not pass, an error will be issued
10286                later.  */
10287             ;
10288           else if (address_p
10289                    && (TREE_CODE (argument) == OFFSET_REF
10290                        || TREE_CODE (argument) == SCOPE_REF))
10291             /* A pointer-to-member.  */
10292             ;
10293           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10294             ;
10295           else
10296             cp_parser_simulate_error (parser);
10297
10298           if (cp_parser_parse_definitely (parser))
10299             {
10300               if (address_p)
10301                 argument = build_x_unary_op (ADDR_EXPR, argument);
10302               return argument;
10303             }
10304         }
10305     }
10306   /* If the argument started with "&", there are no other valid
10307      alternatives at this point.  */
10308   if (address_p)
10309     {
10310       cp_parser_error (parser, "invalid non-type template argument");
10311       return error_mark_node;
10312     }
10313
10314   /* If the argument wasn't successfully parsed as a type-id followed
10315      by '>>', the argument can only be a constant expression now.
10316      Otherwise, we try parsing the constant-expression tentatively,
10317      because the argument could really be a type-id.  */
10318   if (maybe_type_id)
10319     cp_parser_parse_tentatively (parser);
10320   argument = cp_parser_constant_expression (parser,
10321                                             /*allow_non_constant_p=*/false,
10322                                             /*non_constant_p=*/NULL);
10323   argument = fold_non_dependent_expr (argument);
10324   if (!maybe_type_id)
10325     return argument;
10326   if (!cp_parser_next_token_ends_template_argument_p (parser))
10327     cp_parser_error (parser, "expected template-argument");
10328   if (cp_parser_parse_definitely (parser))
10329     return argument;
10330   /* We did our best to parse the argument as a non type-id, but that
10331      was the only alternative that matched (albeit with a '>' after
10332      it). We can assume it's just a typo from the user, and a
10333      diagnostic will then be issued.  */
10334   return cp_parser_type_id (parser);
10335 }
10336
10337 /* Parse an explicit-instantiation.
10338
10339    explicit-instantiation:
10340      template declaration
10341
10342    Although the standard says `declaration', what it really means is:
10343
10344    explicit-instantiation:
10345      template decl-specifier-seq [opt] declarator [opt] ;
10346
10347    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10348    supposed to be allowed.  A defect report has been filed about this
10349    issue.
10350
10351    GNU Extension:
10352
10353    explicit-instantiation:
10354      storage-class-specifier template
10355        decl-specifier-seq [opt] declarator [opt] ;
10356      function-specifier template
10357        decl-specifier-seq [opt] declarator [opt] ;  */
10358
10359 static void
10360 cp_parser_explicit_instantiation (cp_parser* parser)
10361 {
10362   int declares_class_or_enum;
10363   cp_decl_specifier_seq decl_specifiers;
10364   tree extension_specifier = NULL_TREE;
10365
10366   /* Look for an (optional) storage-class-specifier or
10367      function-specifier.  */
10368   if (cp_parser_allow_gnu_extensions_p (parser))
10369     {
10370       extension_specifier
10371         = cp_parser_storage_class_specifier_opt (parser);
10372       if (!extension_specifier)
10373         extension_specifier
10374           = cp_parser_function_specifier_opt (parser,
10375                                               /*decl_specs=*/NULL);
10376     }
10377
10378   /* Look for the `template' keyword.  */
10379   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10380   /* Let the front end know that we are processing an explicit
10381      instantiation.  */
10382   begin_explicit_instantiation ();
10383   /* [temp.explicit] says that we are supposed to ignore access
10384      control while processing explicit instantiation directives.  */
10385   push_deferring_access_checks (dk_no_check);
10386   /* Parse a decl-specifier-seq.  */
10387   cp_parser_decl_specifier_seq (parser,
10388                                 CP_PARSER_FLAGS_OPTIONAL,
10389                                 &decl_specifiers,
10390                                 &declares_class_or_enum);
10391   /* If there was exactly one decl-specifier, and it declared a class,
10392      and there's no declarator, then we have an explicit type
10393      instantiation.  */
10394   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10395     {
10396       tree type;
10397
10398       type = check_tag_decl (&decl_specifiers);
10399       /* Turn access control back on for names used during
10400          template instantiation.  */
10401       pop_deferring_access_checks ();
10402       if (type)
10403         do_type_instantiation (type, extension_specifier,
10404                                /*complain=*/tf_error);
10405     }
10406   else
10407     {
10408       cp_declarator *declarator;
10409       tree decl;
10410
10411       /* Parse the declarator.  */
10412       declarator
10413         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10414                                 /*ctor_dtor_or_conv_p=*/NULL,
10415                                 /*parenthesized_p=*/NULL,
10416                                 /*member_p=*/false);
10417       if (declares_class_or_enum & 2)
10418         cp_parser_check_for_definition_in_return_type (declarator,
10419                                                        decl_specifiers.type);
10420       if (declarator != cp_error_declarator)
10421         {
10422           decl = grokdeclarator (declarator, &decl_specifiers,
10423                                  NORMAL, 0, &decl_specifiers.attributes);
10424           /* Turn access control back on for names used during
10425              template instantiation.  */
10426           pop_deferring_access_checks ();
10427           /* Do the explicit instantiation.  */
10428           do_decl_instantiation (decl, extension_specifier);
10429         }
10430       else
10431         {
10432           pop_deferring_access_checks ();
10433           /* Skip the body of the explicit instantiation.  */
10434           cp_parser_skip_to_end_of_statement (parser);
10435         }
10436     }
10437   /* We're done with the instantiation.  */
10438   end_explicit_instantiation ();
10439
10440   cp_parser_consume_semicolon_at_end_of_statement (parser);
10441 }
10442
10443 /* Parse an explicit-specialization.
10444
10445    explicit-specialization:
10446      template < > declaration
10447
10448    Although the standard says `declaration', what it really means is:
10449
10450    explicit-specialization:
10451      template <> decl-specifier [opt] init-declarator [opt] ;
10452      template <> function-definition
10453      template <> explicit-specialization
10454      template <> template-declaration  */
10455
10456 static void
10457 cp_parser_explicit_specialization (cp_parser* parser)
10458 {
10459   bool need_lang_pop;
10460   /* Look for the `template' keyword.  */
10461   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10462   /* Look for the `<'.  */
10463   cp_parser_require (parser, CPP_LESS, "`<'");
10464   /* Look for the `>'.  */
10465   cp_parser_require (parser, CPP_GREATER, "`>'");
10466   /* We have processed another parameter list.  */
10467   ++parser->num_template_parameter_lists;
10468   /* [temp]
10469
10470      A template ... explicit specialization ... shall not have C
10471      linkage.  */
10472   if (current_lang_name == lang_name_c)
10473     {
10474       error ("template specialization with C linkage");
10475       /* Give it C++ linkage to avoid confusing other parts of the
10476          front end.  */
10477       push_lang_context (lang_name_cplusplus);
10478       need_lang_pop = true;
10479     }
10480   else
10481     need_lang_pop = false;
10482   /* Let the front end know that we are beginning a specialization.  */
10483   if (!begin_specialization ())
10484     {
10485       end_specialization ();
10486       cp_parser_skip_to_end_of_block_or_statement (parser);
10487       return;
10488     }
10489
10490   /* If the next keyword is `template', we need to figure out whether
10491      or not we're looking a template-declaration.  */
10492   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10493     {
10494       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10495           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10496         cp_parser_template_declaration_after_export (parser,
10497                                                      /*member_p=*/false);
10498       else
10499         cp_parser_explicit_specialization (parser);
10500     }
10501   else
10502     /* Parse the dependent declaration.  */
10503     cp_parser_single_declaration (parser,
10504                                   /*checks=*/NULL,
10505                                   /*member_p=*/false,
10506                                   /*explicit_specialization_p=*/true,
10507                                   /*friend_p=*/NULL);
10508   /* We're done with the specialization.  */
10509   end_specialization ();
10510   /* For the erroneous case of a template with C linkage, we pushed an
10511      implicit C++ linkage scope; exit that scope now.  */
10512   if (need_lang_pop)
10513     pop_lang_context ();
10514   /* We're done with this parameter list.  */
10515   --parser->num_template_parameter_lists;
10516 }
10517
10518 /* Parse a type-specifier.
10519
10520    type-specifier:
10521      simple-type-specifier
10522      class-specifier
10523      enum-specifier
10524      elaborated-type-specifier
10525      cv-qualifier
10526
10527    GNU Extension:
10528
10529    type-specifier:
10530      __complex__
10531
10532    Returns a representation of the type-specifier.  For a
10533    class-specifier, enum-specifier, or elaborated-type-specifier, a
10534    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10535
10536    The parser flags FLAGS is used to control type-specifier parsing.
10537
10538    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10539    in a decl-specifier-seq.
10540
10541    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10542    class-specifier, enum-specifier, or elaborated-type-specifier, then
10543    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10544    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10545    zero.
10546
10547    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10548    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10549    is set to FALSE.  */
10550
10551 static tree
10552 cp_parser_type_specifier (cp_parser* parser,
10553                           cp_parser_flags flags,
10554                           cp_decl_specifier_seq *decl_specs,
10555                           bool is_declaration,
10556                           int* declares_class_or_enum,
10557                           bool* is_cv_qualifier)
10558 {
10559   tree type_spec = NULL_TREE;
10560   cp_token *token;
10561   enum rid keyword;
10562   cp_decl_spec ds = ds_last;
10563
10564   /* Assume this type-specifier does not declare a new type.  */
10565   if (declares_class_or_enum)
10566     *declares_class_or_enum = 0;
10567   /* And that it does not specify a cv-qualifier.  */
10568   if (is_cv_qualifier)
10569     *is_cv_qualifier = false;
10570   /* Peek at the next token.  */
10571   token = cp_lexer_peek_token (parser->lexer);
10572
10573   /* If we're looking at a keyword, we can use that to guide the
10574      production we choose.  */
10575   keyword = token->keyword;
10576   switch (keyword)
10577     {
10578     case RID_ENUM:
10579       /* Look for the enum-specifier.  */
10580       type_spec = cp_parser_enum_specifier (parser);
10581       /* If that worked, we're done.  */
10582       if (type_spec)
10583         {
10584           if (declares_class_or_enum)
10585             *declares_class_or_enum = 2;
10586           if (decl_specs)
10587             cp_parser_set_decl_spec_type (decl_specs,
10588                                           type_spec,
10589                                           /*user_defined_p=*/true);
10590           return type_spec;
10591         }
10592       else
10593         goto elaborated_type_specifier;
10594
10595       /* Any of these indicate either a class-specifier, or an
10596          elaborated-type-specifier.  */
10597     case RID_CLASS:
10598     case RID_STRUCT:
10599     case RID_UNION:
10600       /* Parse tentatively so that we can back up if we don't find a
10601          class-specifier.  */
10602       cp_parser_parse_tentatively (parser);
10603       /* Look for the class-specifier.  */
10604       type_spec = cp_parser_class_specifier (parser);
10605       /* If that worked, we're done.  */
10606       if (cp_parser_parse_definitely (parser))
10607         {
10608           if (declares_class_or_enum)
10609             *declares_class_or_enum = 2;
10610           if (decl_specs)
10611             cp_parser_set_decl_spec_type (decl_specs,
10612                                           type_spec,
10613                                           /*user_defined_p=*/true);
10614           return type_spec;
10615         }
10616
10617       /* Fall through.  */
10618     elaborated_type_specifier:
10619       /* We're declaring (not defining) a class or enum.  */
10620       if (declares_class_or_enum)
10621         *declares_class_or_enum = 1;
10622
10623       /* Fall through.  */
10624     case RID_TYPENAME:
10625       /* Look for an elaborated-type-specifier.  */
10626       type_spec
10627         = (cp_parser_elaborated_type_specifier
10628            (parser,
10629             decl_specs && decl_specs->specs[(int) ds_friend],
10630             is_declaration));
10631       if (decl_specs)
10632         cp_parser_set_decl_spec_type (decl_specs,
10633                                       type_spec,
10634                                       /*user_defined_p=*/true);
10635       return type_spec;
10636
10637     case RID_CONST:
10638       ds = ds_const;
10639       if (is_cv_qualifier)
10640         *is_cv_qualifier = true;
10641       break;
10642
10643     case RID_VOLATILE:
10644       ds = ds_volatile;
10645       if (is_cv_qualifier)
10646         *is_cv_qualifier = true;
10647       break;
10648
10649     case RID_RESTRICT:
10650       ds = ds_restrict;
10651       if (is_cv_qualifier)
10652         *is_cv_qualifier = true;
10653       break;
10654
10655     case RID_COMPLEX:
10656       /* The `__complex__' keyword is a GNU extension.  */
10657       ds = ds_complex;
10658       break;
10659
10660     default:
10661       break;
10662     }
10663
10664   /* Handle simple keywords.  */
10665   if (ds != ds_last)
10666     {
10667       if (decl_specs)
10668         {
10669           ++decl_specs->specs[(int)ds];
10670           decl_specs->any_specifiers_p = true;
10671         }
10672       return cp_lexer_consume_token (parser->lexer)->u.value;
10673     }
10674
10675   /* If we do not already have a type-specifier, assume we are looking
10676      at a simple-type-specifier.  */
10677   type_spec = cp_parser_simple_type_specifier (parser,
10678                                                decl_specs,
10679                                                flags);
10680
10681   /* If we didn't find a type-specifier, and a type-specifier was not
10682      optional in this context, issue an error message.  */
10683   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10684     {
10685       cp_parser_error (parser, "expected type specifier");
10686       return error_mark_node;
10687     }
10688
10689   return type_spec;
10690 }
10691
10692 /* Parse a simple-type-specifier.
10693
10694    simple-type-specifier:
10695      :: [opt] nested-name-specifier [opt] type-name
10696      :: [opt] nested-name-specifier template template-id
10697      char
10698      wchar_t
10699      bool
10700      short
10701      int
10702      long
10703      signed
10704      unsigned
10705      float
10706      double
10707      void
10708
10709    C++0x Extension:
10710
10711    simple-type-specifier:
10712      decltype ( expression )   
10713
10714    GNU Extension:
10715
10716    simple-type-specifier:
10717      __typeof__ unary-expression
10718      __typeof__ ( type-id )
10719
10720    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10721    appropriately updated.  */
10722
10723 static tree
10724 cp_parser_simple_type_specifier (cp_parser* parser,
10725                                  cp_decl_specifier_seq *decl_specs,
10726                                  cp_parser_flags flags)
10727 {
10728   tree type = NULL_TREE;
10729   cp_token *token;
10730
10731   /* Peek at the next token.  */
10732   token = cp_lexer_peek_token (parser->lexer);
10733
10734   /* If we're looking at a keyword, things are easy.  */
10735   switch (token->keyword)
10736     {
10737     case RID_CHAR:
10738       if (decl_specs)
10739         decl_specs->explicit_char_p = true;
10740       type = char_type_node;
10741       break;
10742     case RID_WCHAR:
10743       type = wchar_type_node;
10744       break;
10745     case RID_BOOL:
10746       type = boolean_type_node;
10747       break;
10748     case RID_SHORT:
10749       if (decl_specs)
10750         ++decl_specs->specs[(int) ds_short];
10751       type = short_integer_type_node;
10752       break;
10753     case RID_INT:
10754       if (decl_specs)
10755         decl_specs->explicit_int_p = true;
10756       type = integer_type_node;
10757       break;
10758     case RID_LONG:
10759       if (decl_specs)
10760         ++decl_specs->specs[(int) ds_long];
10761       type = long_integer_type_node;
10762       break;
10763     case RID_SIGNED:
10764       if (decl_specs)
10765         ++decl_specs->specs[(int) ds_signed];
10766       type = integer_type_node;
10767       break;
10768     case RID_UNSIGNED:
10769       if (decl_specs)
10770         ++decl_specs->specs[(int) ds_unsigned];
10771       type = unsigned_type_node;
10772       break;
10773     case RID_FLOAT:
10774       type = float_type_node;
10775       break;
10776     case RID_DOUBLE:
10777       type = double_type_node;
10778       break;
10779     case RID_VOID:
10780       type = void_type_node;
10781       break;
10782
10783     case RID_DECLTYPE:
10784       /* Parse the `decltype' type.  */
10785       type = cp_parser_decltype (parser);
10786
10787       if (decl_specs)
10788         cp_parser_set_decl_spec_type (decl_specs, type,
10789                                       /*user_defined_p=*/true);
10790
10791       return type;
10792
10793     case RID_TYPEOF:
10794       /* Consume the `typeof' token.  */
10795       cp_lexer_consume_token (parser->lexer);
10796       /* Parse the operand to `typeof'.  */
10797       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10798       /* If it is not already a TYPE, take its type.  */
10799       if (!TYPE_P (type))
10800         type = finish_typeof (type);
10801
10802       if (decl_specs)
10803         cp_parser_set_decl_spec_type (decl_specs, type,
10804                                       /*user_defined_p=*/true);
10805
10806       return type;
10807
10808     default:
10809       break;
10810     }
10811
10812   /* If the type-specifier was for a built-in type, we're done.  */
10813   if (type)
10814     {
10815       tree id;
10816
10817       /* Record the type.  */
10818       if (decl_specs
10819           && (token->keyword != RID_SIGNED
10820               && token->keyword != RID_UNSIGNED
10821               && token->keyword != RID_SHORT
10822               && token->keyword != RID_LONG))
10823         cp_parser_set_decl_spec_type (decl_specs,
10824                                       type,
10825                                       /*user_defined=*/false);
10826       if (decl_specs)
10827         decl_specs->any_specifiers_p = true;
10828
10829       /* Consume the token.  */
10830       id = cp_lexer_consume_token (parser->lexer)->u.value;
10831
10832       /* There is no valid C++ program where a non-template type is
10833          followed by a "<".  That usually indicates that the user thought
10834          that the type was a template.  */
10835       cp_parser_check_for_invalid_template_id (parser, type);
10836
10837       return TYPE_NAME (type);
10838     }
10839
10840   /* The type-specifier must be a user-defined type.  */
10841   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10842     {
10843       bool qualified_p;
10844       bool global_p;
10845
10846       /* Don't gobble tokens or issue error messages if this is an
10847          optional type-specifier.  */
10848       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10849         cp_parser_parse_tentatively (parser);
10850
10851       /* Look for the optional `::' operator.  */
10852       global_p
10853         = (cp_parser_global_scope_opt (parser,
10854                                        /*current_scope_valid_p=*/false)
10855            != NULL_TREE);
10856       /* Look for the nested-name specifier.  */
10857       qualified_p
10858         = (cp_parser_nested_name_specifier_opt (parser,
10859                                                 /*typename_keyword_p=*/false,
10860                                                 /*check_dependency_p=*/true,
10861                                                 /*type_p=*/false,
10862                                                 /*is_declaration=*/false)
10863            != NULL_TREE);
10864       /* If we have seen a nested-name-specifier, and the next token
10865          is `template', then we are using the template-id production.  */
10866       if (parser->scope
10867           && cp_parser_optional_template_keyword (parser))
10868         {
10869           /* Look for the template-id.  */
10870           type = cp_parser_template_id (parser,
10871                                         /*template_keyword_p=*/true,
10872                                         /*check_dependency_p=*/true,
10873                                         /*is_declaration=*/false);
10874           /* If the template-id did not name a type, we are out of
10875              luck.  */
10876           if (TREE_CODE (type) != TYPE_DECL)
10877             {
10878               cp_parser_error (parser, "expected template-id for type");
10879               type = NULL_TREE;
10880             }
10881         }
10882       /* Otherwise, look for a type-name.  */
10883       else
10884         type = cp_parser_type_name (parser);
10885       /* Keep track of all name-lookups performed in class scopes.  */
10886       if (type
10887           && !global_p
10888           && !qualified_p
10889           && TREE_CODE (type) == TYPE_DECL
10890           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10891         maybe_note_name_used_in_class (DECL_NAME (type), type);
10892       /* If it didn't work out, we don't have a TYPE.  */
10893       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10894           && !cp_parser_parse_definitely (parser))
10895         type = NULL_TREE;
10896       if (type && decl_specs)
10897         cp_parser_set_decl_spec_type (decl_specs, type,
10898                                       /*user_defined=*/true);
10899     }
10900
10901   /* If we didn't get a type-name, issue an error message.  */
10902   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10903     {
10904       cp_parser_error (parser, "expected type-name");
10905       return error_mark_node;
10906     }
10907
10908   /* There is no valid C++ program where a non-template type is
10909      followed by a "<".  That usually indicates that the user thought
10910      that the type was a template.  */
10911   if (type && type != error_mark_node)
10912     {
10913       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10914          If it is, then the '<'...'>' enclose protocol names rather than
10915          template arguments, and so everything is fine.  */
10916       if (c_dialect_objc ()
10917           && (objc_is_id (type) || objc_is_class_name (type)))
10918         {
10919           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10920           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10921
10922           /* Clobber the "unqualified" type previously entered into
10923              DECL_SPECS with the new, improved protocol-qualified version.  */
10924           if (decl_specs)
10925             decl_specs->type = qual_type;
10926
10927           return qual_type;
10928         }
10929
10930       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10931     }
10932
10933   return type;
10934 }
10935
10936 /* Parse a type-name.
10937
10938    type-name:
10939      class-name
10940      enum-name
10941      typedef-name
10942
10943    enum-name:
10944      identifier
10945
10946    typedef-name:
10947      identifier
10948
10949    Returns a TYPE_DECL for the type.  */
10950
10951 static tree
10952 cp_parser_type_name (cp_parser* parser)
10953 {
10954   tree type_decl;
10955   tree identifier;
10956
10957   /* We can't know yet whether it is a class-name or not.  */
10958   cp_parser_parse_tentatively (parser);
10959   /* Try a class-name.  */
10960   type_decl = cp_parser_class_name (parser,
10961                                     /*typename_keyword_p=*/false,
10962                                     /*template_keyword_p=*/false,
10963                                     none_type,
10964                                     /*check_dependency_p=*/true,
10965                                     /*class_head_p=*/false,
10966                                     /*is_declaration=*/false);
10967   /* If it's not a class-name, keep looking.  */
10968   if (!cp_parser_parse_definitely (parser))
10969     {
10970       /* It must be a typedef-name or an enum-name.  */
10971       identifier = cp_parser_identifier (parser);
10972       if (identifier == error_mark_node)
10973         return error_mark_node;
10974
10975       /* Look up the type-name.  */
10976       type_decl = cp_parser_lookup_name_simple (parser, identifier);
10977
10978       if (TREE_CODE (type_decl) != TYPE_DECL
10979           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10980         {
10981           /* See if this is an Objective-C type.  */
10982           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10983           tree type = objc_get_protocol_qualified_type (identifier, protos);
10984           if (type)
10985             type_decl = TYPE_NAME (type);
10986         }
10987
10988       /* Issue an error if we did not find a type-name.  */
10989       if (TREE_CODE (type_decl) != TYPE_DECL)
10990         {
10991           if (!cp_parser_simulate_error (parser))
10992             cp_parser_name_lookup_error (parser, identifier, type_decl,
10993                                          "is not a type");
10994           type_decl = error_mark_node;
10995         }
10996       /* Remember that the name was used in the definition of the
10997          current class so that we can check later to see if the
10998          meaning would have been different after the class was
10999          entirely defined.  */
11000       else if (type_decl != error_mark_node
11001                && !parser->scope)
11002         maybe_note_name_used_in_class (identifier, type_decl);
11003     }
11004
11005   return type_decl;
11006 }
11007
11008
11009 /* Parse an elaborated-type-specifier.  Note that the grammar given
11010    here incorporates the resolution to DR68.
11011
11012    elaborated-type-specifier:
11013      class-key :: [opt] nested-name-specifier [opt] identifier
11014      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11015      enum :: [opt] nested-name-specifier [opt] identifier
11016      typename :: [opt] nested-name-specifier identifier
11017      typename :: [opt] nested-name-specifier template [opt]
11018        template-id
11019
11020    GNU extension:
11021
11022    elaborated-type-specifier:
11023      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11024      class-key attributes :: [opt] nested-name-specifier [opt]
11025                template [opt] template-id
11026      enum attributes :: [opt] nested-name-specifier [opt] identifier
11027
11028    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11029    declared `friend'.  If IS_DECLARATION is TRUE, then this
11030    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11031    something is being declared.
11032
11033    Returns the TYPE specified.  */
11034
11035 static tree
11036 cp_parser_elaborated_type_specifier (cp_parser* parser,
11037                                      bool is_friend,
11038                                      bool is_declaration)
11039 {
11040   enum tag_types tag_type;
11041   tree identifier;
11042   tree type = NULL_TREE;
11043   tree attributes = NULL_TREE;
11044
11045   /* See if we're looking at the `enum' keyword.  */
11046   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11047     {
11048       /* Consume the `enum' token.  */
11049       cp_lexer_consume_token (parser->lexer);
11050       /* Remember that it's an enumeration type.  */
11051       tag_type = enum_type;
11052       /* Parse the attributes.  */
11053       attributes = cp_parser_attributes_opt (parser);
11054     }
11055   /* Or, it might be `typename'.  */
11056   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11057                                            RID_TYPENAME))
11058     {
11059       /* Consume the `typename' token.  */
11060       cp_lexer_consume_token (parser->lexer);
11061       /* Remember that it's a `typename' type.  */
11062       tag_type = typename_type;
11063       /* The `typename' keyword is only allowed in templates.  */
11064       if (!processing_template_decl)
11065         pedwarn ("using %<typename%> outside of template");
11066     }
11067   /* Otherwise it must be a class-key.  */
11068   else
11069     {
11070       tag_type = cp_parser_class_key (parser);
11071       if (tag_type == none_type)
11072         return error_mark_node;
11073       /* Parse the attributes.  */
11074       attributes = cp_parser_attributes_opt (parser);
11075     }
11076
11077   /* Look for the `::' operator.  */
11078   cp_parser_global_scope_opt (parser,
11079                               /*current_scope_valid_p=*/false);
11080   /* Look for the nested-name-specifier.  */
11081   if (tag_type == typename_type)
11082     {
11083       if (!cp_parser_nested_name_specifier (parser,
11084                                            /*typename_keyword_p=*/true,
11085                                            /*check_dependency_p=*/true,
11086                                            /*type_p=*/true,
11087                                             is_declaration))
11088         return error_mark_node;
11089     }
11090   else
11091     /* Even though `typename' is not present, the proposed resolution
11092        to Core Issue 180 says that in `class A<T>::B', `B' should be
11093        considered a type-name, even if `A<T>' is dependent.  */
11094     cp_parser_nested_name_specifier_opt (parser,
11095                                          /*typename_keyword_p=*/true,
11096                                          /*check_dependency_p=*/true,
11097                                          /*type_p=*/true,
11098                                          is_declaration);
11099  /* For everything but enumeration types, consider a template-id.
11100     For an enumeration type, consider only a plain identifier.  */
11101   if (tag_type != enum_type)
11102     {
11103       bool template_p = false;
11104       tree decl;
11105
11106       /* Allow the `template' keyword.  */
11107       template_p = cp_parser_optional_template_keyword (parser);
11108       /* If we didn't see `template', we don't know if there's a
11109          template-id or not.  */
11110       if (!template_p)
11111         cp_parser_parse_tentatively (parser);
11112       /* Parse the template-id.  */
11113       decl = cp_parser_template_id (parser, template_p,
11114                                     /*check_dependency_p=*/true,
11115                                     is_declaration);
11116       /* If we didn't find a template-id, look for an ordinary
11117          identifier.  */
11118       if (!template_p && !cp_parser_parse_definitely (parser))
11119         ;
11120       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11121          in effect, then we must assume that, upon instantiation, the
11122          template will correspond to a class.  */
11123       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11124                && tag_type == typename_type)
11125         type = make_typename_type (parser->scope, decl,
11126                                    typename_type,
11127                                    /*complain=*/tf_error);
11128       else
11129         type = TREE_TYPE (decl);
11130     }
11131
11132   if (!type)
11133     {
11134       identifier = cp_parser_identifier (parser);
11135
11136       if (identifier == error_mark_node)
11137         {
11138           parser->scope = NULL_TREE;
11139           return error_mark_node;
11140         }
11141
11142       /* For a `typename', we needn't call xref_tag.  */
11143       if (tag_type == typename_type
11144           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11145         return cp_parser_make_typename_type (parser, parser->scope,
11146                                              identifier);
11147       /* Look up a qualified name in the usual way.  */
11148       if (parser->scope)
11149         {
11150           tree decl;
11151           tree ambiguous_decls;
11152
11153           decl = cp_parser_lookup_name (parser, identifier,
11154                                         tag_type,
11155                                         /*is_template=*/false,
11156                                         /*is_namespace=*/false,
11157                                         /*check_dependency=*/true,
11158                                         &ambiguous_decls);
11159
11160           /* If the lookup was ambiguous, an error will already have been
11161              issued.  */
11162           if (ambiguous_decls)
11163             return error_mark_node;
11164
11165           /* If we are parsing friend declaration, DECL may be a
11166              TEMPLATE_DECL tree node here.  However, we need to check
11167              whether this TEMPLATE_DECL results in valid code.  Consider
11168              the following example:
11169
11170                namespace N {
11171                  template <class T> class C {};
11172                }
11173                class X {
11174                  template <class T> friend class N::C; // #1, valid code
11175                };
11176                template <class T> class Y {
11177                  friend class N::C;                    // #2, invalid code
11178                };
11179
11180              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11181              name lookup of `N::C'.  We see that friend declaration must
11182              be template for the code to be valid.  Note that
11183              processing_template_decl does not work here since it is
11184              always 1 for the above two cases.  */
11185
11186           decl = (cp_parser_maybe_treat_template_as_class
11187                   (decl, /*tag_name_p=*/is_friend
11188                          && parser->num_template_parameter_lists));
11189
11190           if (TREE_CODE (decl) != TYPE_DECL)
11191             {
11192               cp_parser_diagnose_invalid_type_name (parser,
11193                                                     parser->scope,
11194                                                     identifier);
11195               return error_mark_node;
11196             }
11197
11198           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11199             {
11200               bool allow_template = (parser->num_template_parameter_lists
11201                                       || DECL_SELF_REFERENCE_P (decl));
11202               type = check_elaborated_type_specifier (tag_type, decl, 
11203                                                       allow_template);
11204
11205               if (type == error_mark_node)
11206                 return error_mark_node;
11207             }
11208
11209           /* Forward declarations of nested types, such as
11210
11211                class C1::C2;
11212                class C1::C2::C3;
11213
11214              are invalid unless all components preceding the final '::'
11215              are complete.  If all enclosing types are complete, these
11216              declarations become merely pointless.
11217
11218              Invalid forward declarations of nested types are errors
11219              caught elsewhere in parsing.  Those that are pointless arrive
11220              here.  */
11221
11222           if (cp_parser_declares_only_class_p (parser)
11223               && !is_friend && !processing_explicit_instantiation)
11224             warning (0, "declaration %qD does not declare anything", decl);
11225
11226           type = TREE_TYPE (decl);
11227         }
11228       else
11229         {
11230           /* An elaborated-type-specifier sometimes introduces a new type and
11231              sometimes names an existing type.  Normally, the rule is that it
11232              introduces a new type only if there is not an existing type of
11233              the same name already in scope.  For example, given:
11234
11235                struct S {};
11236                void f() { struct S s; }
11237
11238              the `struct S' in the body of `f' is the same `struct S' as in
11239              the global scope; the existing definition is used.  However, if
11240              there were no global declaration, this would introduce a new
11241              local class named `S'.
11242
11243              An exception to this rule applies to the following code:
11244
11245                namespace N { struct S; }
11246
11247              Here, the elaborated-type-specifier names a new type
11248              unconditionally; even if there is already an `S' in the
11249              containing scope this declaration names a new type.
11250              This exception only applies if the elaborated-type-specifier
11251              forms the complete declaration:
11252
11253                [class.name]
11254
11255                A declaration consisting solely of `class-key identifier ;' is
11256                either a redeclaration of the name in the current scope or a
11257                forward declaration of the identifier as a class name.  It
11258                introduces the name into the current scope.
11259
11260              We are in this situation precisely when the next token is a `;'.
11261
11262              An exception to the exception is that a `friend' declaration does
11263              *not* name a new type; i.e., given:
11264
11265                struct S { friend struct T; };
11266
11267              `T' is not a new type in the scope of `S'.
11268
11269              Also, `new struct S' or `sizeof (struct S)' never results in the
11270              definition of a new type; a new type can only be declared in a
11271              declaration context.  */
11272
11273           tag_scope ts;
11274           bool template_p;
11275
11276           if (is_friend)
11277             /* Friends have special name lookup rules.  */
11278             ts = ts_within_enclosing_non_class;
11279           else if (is_declaration
11280                    && cp_lexer_next_token_is (parser->lexer,
11281                                               CPP_SEMICOLON))
11282             /* This is a `class-key identifier ;' */
11283             ts = ts_current;
11284           else
11285             ts = ts_global;
11286
11287           template_p =
11288             (parser->num_template_parameter_lists
11289              && (cp_parser_next_token_starts_class_definition_p (parser)
11290                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11291           /* An unqualified name was used to reference this type, so
11292              there were no qualifying templates.  */
11293           if (!cp_parser_check_template_parameters (parser,
11294                                                     /*num_templates=*/0))
11295             return error_mark_node;
11296           type = xref_tag (tag_type, identifier, ts, template_p);
11297         }
11298     }
11299
11300   if (type == error_mark_node)
11301     return error_mark_node;
11302
11303   /* Allow attributes on forward declarations of classes.  */
11304   if (attributes)
11305     {
11306       if (TREE_CODE (type) == TYPENAME_TYPE)
11307         warning (OPT_Wattributes,
11308                  "attributes ignored on uninstantiated type");
11309       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11310                && ! processing_explicit_instantiation)
11311         warning (OPT_Wattributes,
11312                  "attributes ignored on template instantiation");
11313       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11314         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11315       else
11316         warning (OPT_Wattributes,
11317                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11318     }
11319
11320   if (tag_type != enum_type)
11321     cp_parser_check_class_key (tag_type, type);
11322
11323   /* A "<" cannot follow an elaborated type specifier.  If that
11324      happens, the user was probably trying to form a template-id.  */
11325   cp_parser_check_for_invalid_template_id (parser, type);
11326
11327   return type;
11328 }
11329
11330 /* Parse an enum-specifier.
11331
11332    enum-specifier:
11333      enum identifier [opt] { enumerator-list [opt] }
11334
11335    GNU Extensions:
11336      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11337        attributes[opt]
11338
11339    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11340    if the token stream isn't an enum-specifier after all.  */
11341
11342 static tree
11343 cp_parser_enum_specifier (cp_parser* parser)
11344 {
11345   tree identifier;
11346   tree type;
11347   tree attributes;
11348
11349   /* Parse tentatively so that we can back up if we don't find a
11350      enum-specifier.  */
11351   cp_parser_parse_tentatively (parser);
11352
11353   /* Caller guarantees that the current token is 'enum', an identifier
11354      possibly follows, and the token after that is an opening brace.
11355      If we don't have an identifier, fabricate an anonymous name for
11356      the enumeration being defined.  */
11357   cp_lexer_consume_token (parser->lexer);
11358
11359   attributes = cp_parser_attributes_opt (parser);
11360
11361   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11362     identifier = cp_parser_identifier (parser);
11363   else
11364     identifier = make_anon_name ();
11365
11366   /* Look for the `{' but don't consume it yet.  */
11367   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11368     cp_parser_simulate_error (parser);
11369
11370   if (!cp_parser_parse_definitely (parser))
11371     return NULL_TREE;
11372
11373   /* Issue an error message if type-definitions are forbidden here.  */
11374   if (!cp_parser_check_type_definition (parser))
11375     type = error_mark_node;
11376   else
11377     /* Create the new type.  We do this before consuming the opening
11378        brace so the enum will be recorded as being on the line of its
11379        tag (or the 'enum' keyword, if there is no tag).  */
11380     type = start_enum (identifier);
11381   
11382   /* Consume the opening brace.  */
11383   cp_lexer_consume_token (parser->lexer);
11384
11385   if (type == error_mark_node)
11386     {
11387       cp_parser_skip_to_end_of_block_or_statement (parser);
11388       return error_mark_node;
11389     }
11390
11391   /* If the next token is not '}', then there are some enumerators.  */
11392   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11393     cp_parser_enumerator_list (parser, type);
11394
11395   /* Consume the final '}'.  */
11396   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11397
11398   /* Look for trailing attributes to apply to this enumeration, and
11399      apply them if appropriate.  */
11400   if (cp_parser_allow_gnu_extensions_p (parser))
11401     {
11402       tree trailing_attr = cp_parser_attributes_opt (parser);
11403       cplus_decl_attributes (&type,
11404                              trailing_attr,
11405                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11406     }
11407
11408   /* Finish up the enumeration.  */
11409   finish_enum (type);
11410
11411   return type;
11412 }
11413
11414 /* Parse an enumerator-list.  The enumerators all have the indicated
11415    TYPE.
11416
11417    enumerator-list:
11418      enumerator-definition
11419      enumerator-list , enumerator-definition  */
11420
11421 static void
11422 cp_parser_enumerator_list (cp_parser* parser, tree type)
11423 {
11424   while (true)
11425     {
11426       /* Parse an enumerator-definition.  */
11427       cp_parser_enumerator_definition (parser, type);
11428
11429       /* If the next token is not a ',', we've reached the end of
11430          the list.  */
11431       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11432         break;
11433       /* Otherwise, consume the `,' and keep going.  */
11434       cp_lexer_consume_token (parser->lexer);
11435       /* If the next token is a `}', there is a trailing comma.  */
11436       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11437         {
11438           if (pedantic && !in_system_header)
11439             pedwarn ("comma at end of enumerator list");
11440           break;
11441         }
11442     }
11443 }
11444
11445 /* Parse an enumerator-definition.  The enumerator has the indicated
11446    TYPE.
11447
11448    enumerator-definition:
11449      enumerator
11450      enumerator = constant-expression
11451
11452    enumerator:
11453      identifier  */
11454
11455 static void
11456 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11457 {
11458   tree identifier;
11459   tree value;
11460
11461   /* Look for the identifier.  */
11462   identifier = cp_parser_identifier (parser);
11463   if (identifier == error_mark_node)
11464     return;
11465
11466   /* If the next token is an '=', then there is an explicit value.  */
11467   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11468     {
11469       /* Consume the `=' token.  */
11470       cp_lexer_consume_token (parser->lexer);
11471       /* Parse the value.  */
11472       value = cp_parser_constant_expression (parser,
11473                                              /*allow_non_constant_p=*/false,
11474                                              NULL);
11475     }
11476   else
11477     value = NULL_TREE;
11478
11479   /* Create the enumerator.  */
11480   build_enumerator (identifier, value, type);
11481 }
11482
11483 /* Parse a namespace-name.
11484
11485    namespace-name:
11486      original-namespace-name
11487      namespace-alias
11488
11489    Returns the NAMESPACE_DECL for the namespace.  */
11490
11491 static tree
11492 cp_parser_namespace_name (cp_parser* parser)
11493 {
11494   tree identifier;
11495   tree namespace_decl;
11496
11497   /* Get the name of the namespace.  */
11498   identifier = cp_parser_identifier (parser);
11499   if (identifier == error_mark_node)
11500     return error_mark_node;
11501
11502   /* Look up the identifier in the currently active scope.  Look only
11503      for namespaces, due to:
11504
11505        [basic.lookup.udir]
11506
11507        When looking up a namespace-name in a using-directive or alias
11508        definition, only namespace names are considered.
11509
11510      And:
11511
11512        [basic.lookup.qual]
11513
11514        During the lookup of a name preceding the :: scope resolution
11515        operator, object, function, and enumerator names are ignored.
11516
11517      (Note that cp_parser_class_or_namespace_name only calls this
11518      function if the token after the name is the scope resolution
11519      operator.)  */
11520   namespace_decl = cp_parser_lookup_name (parser, identifier,
11521                                           none_type,
11522                                           /*is_template=*/false,
11523                                           /*is_namespace=*/true,
11524                                           /*check_dependency=*/true,
11525                                           /*ambiguous_decls=*/NULL);
11526   /* If it's not a namespace, issue an error.  */
11527   if (namespace_decl == error_mark_node
11528       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11529     {
11530       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11531         error ("%qD is not a namespace-name", identifier);
11532       cp_parser_error (parser, "expected namespace-name");
11533       namespace_decl = error_mark_node;
11534     }
11535
11536   return namespace_decl;
11537 }
11538
11539 /* Parse a namespace-definition.
11540
11541    namespace-definition:
11542      named-namespace-definition
11543      unnamed-namespace-definition
11544
11545    named-namespace-definition:
11546      original-namespace-definition
11547      extension-namespace-definition
11548
11549    original-namespace-definition:
11550      namespace identifier { namespace-body }
11551
11552    extension-namespace-definition:
11553      namespace original-namespace-name { namespace-body }
11554
11555    unnamed-namespace-definition:
11556      namespace { namespace-body } */
11557
11558 static void
11559 cp_parser_namespace_definition (cp_parser* parser)
11560 {
11561   tree identifier, attribs;
11562   bool has_visibility;
11563
11564   /* Look for the `namespace' keyword.  */
11565   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11566
11567   /* Get the name of the namespace.  We do not attempt to distinguish
11568      between an original-namespace-definition and an
11569      extension-namespace-definition at this point.  The semantic
11570      analysis routines are responsible for that.  */
11571   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11572     identifier = cp_parser_identifier (parser);
11573   else
11574     identifier = NULL_TREE;
11575
11576   /* Parse any specified attributes.  */
11577   attribs = cp_parser_attributes_opt (parser);
11578
11579   /* Look for the `{' to start the namespace.  */
11580   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11581   /* Start the namespace.  */
11582   push_namespace (identifier);
11583
11584   has_visibility = handle_namespace_attrs (current_namespace, attribs);
11585
11586   /* Parse the body of the namespace.  */
11587   cp_parser_namespace_body (parser);
11588
11589 #ifdef HANDLE_PRAGMA_VISIBILITY
11590   if (has_visibility)
11591     pop_visibility ();
11592 #endif
11593
11594   /* Finish the namespace.  */
11595   pop_namespace ();
11596   /* Look for the final `}'.  */
11597   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11598 }
11599
11600 /* Parse a namespace-body.
11601
11602    namespace-body:
11603      declaration-seq [opt]  */
11604
11605 static void
11606 cp_parser_namespace_body (cp_parser* parser)
11607 {
11608   cp_parser_declaration_seq_opt (parser);
11609 }
11610
11611 /* Parse a namespace-alias-definition.
11612
11613    namespace-alias-definition:
11614      namespace identifier = qualified-namespace-specifier ;  */
11615
11616 static void
11617 cp_parser_namespace_alias_definition (cp_parser* parser)
11618 {
11619   tree identifier;
11620   tree namespace_specifier;
11621
11622   /* Look for the `namespace' keyword.  */
11623   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11624   /* Look for the identifier.  */
11625   identifier = cp_parser_identifier (parser);
11626   if (identifier == error_mark_node)
11627     return;
11628   /* Look for the `=' token.  */
11629   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11630       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11631     {
11632       error ("%<namespace%> definition is not allowed here");
11633       /* Skip the definition.  */
11634       cp_lexer_consume_token (parser->lexer);
11635       if (cp_parser_skip_to_closing_brace (parser))
11636         cp_lexer_consume_token (parser->lexer);
11637       return;
11638     }
11639   cp_parser_require (parser, CPP_EQ, "`='");
11640   /* Look for the qualified-namespace-specifier.  */
11641   namespace_specifier
11642     = cp_parser_qualified_namespace_specifier (parser);
11643   /* Look for the `;' token.  */
11644   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11645
11646   /* Register the alias in the symbol table.  */
11647   do_namespace_alias (identifier, namespace_specifier);
11648 }
11649
11650 /* Parse a qualified-namespace-specifier.
11651
11652    qualified-namespace-specifier:
11653      :: [opt] nested-name-specifier [opt] namespace-name
11654
11655    Returns a NAMESPACE_DECL corresponding to the specified
11656    namespace.  */
11657
11658 static tree
11659 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11660 {
11661   /* Look for the optional `::'.  */
11662   cp_parser_global_scope_opt (parser,
11663                               /*current_scope_valid_p=*/false);
11664
11665   /* Look for the optional nested-name-specifier.  */
11666   cp_parser_nested_name_specifier_opt (parser,
11667                                        /*typename_keyword_p=*/false,
11668                                        /*check_dependency_p=*/true,
11669                                        /*type_p=*/false,
11670                                        /*is_declaration=*/true);
11671
11672   return cp_parser_namespace_name (parser);
11673 }
11674
11675 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11676    access declaration.
11677
11678    using-declaration:
11679      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11680      using :: unqualified-id ;  
11681
11682    access-declaration:
11683      qualified-id ;  
11684
11685    */
11686
11687 static bool
11688 cp_parser_using_declaration (cp_parser* parser, 
11689                              bool access_declaration_p)
11690 {
11691   cp_token *token;
11692   bool typename_p = false;
11693   bool global_scope_p;
11694   tree decl;
11695   tree identifier;
11696   tree qscope;
11697
11698   if (access_declaration_p)
11699     cp_parser_parse_tentatively (parser);
11700   else
11701     {
11702       /* Look for the `using' keyword.  */
11703       cp_parser_require_keyword (parser, RID_USING, "`using'");
11704       
11705       /* Peek at the next token.  */
11706       token = cp_lexer_peek_token (parser->lexer);
11707       /* See if it's `typename'.  */
11708       if (token->keyword == RID_TYPENAME)
11709         {
11710           /* Remember that we've seen it.  */
11711           typename_p = true;
11712           /* Consume the `typename' token.  */
11713           cp_lexer_consume_token (parser->lexer);
11714         }
11715     }
11716
11717   /* Look for the optional global scope qualification.  */
11718   global_scope_p
11719     = (cp_parser_global_scope_opt (parser,
11720                                    /*current_scope_valid_p=*/false)
11721        != NULL_TREE);
11722
11723   /* If we saw `typename', or didn't see `::', then there must be a
11724      nested-name-specifier present.  */
11725   if (typename_p || !global_scope_p)
11726     qscope = cp_parser_nested_name_specifier (parser, typename_p,
11727                                               /*check_dependency_p=*/true,
11728                                               /*type_p=*/false,
11729                                               /*is_declaration=*/true);
11730   /* Otherwise, we could be in either of the two productions.  In that
11731      case, treat the nested-name-specifier as optional.  */
11732   else
11733     qscope = cp_parser_nested_name_specifier_opt (parser,
11734                                                   /*typename_keyword_p=*/false,
11735                                                   /*check_dependency_p=*/true,
11736                                                   /*type_p=*/false,
11737                                                   /*is_declaration=*/true);
11738   if (!qscope)
11739     qscope = global_namespace;
11740
11741   if (access_declaration_p && cp_parser_error_occurred (parser))
11742     /* Something has already gone wrong; there's no need to parse
11743        further.  Since an error has occurred, the return value of
11744        cp_parser_parse_definitely will be false, as required.  */
11745     return cp_parser_parse_definitely (parser);
11746
11747   /* Parse the unqualified-id.  */
11748   identifier = cp_parser_unqualified_id (parser,
11749                                          /*template_keyword_p=*/false,
11750                                          /*check_dependency_p=*/true,
11751                                          /*declarator_p=*/true,
11752                                          /*optional_p=*/false);
11753
11754   if (access_declaration_p)
11755     {
11756       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11757         cp_parser_simulate_error (parser);
11758       if (!cp_parser_parse_definitely (parser))
11759         return false;
11760     }
11761
11762   /* The function we call to handle a using-declaration is different
11763      depending on what scope we are in.  */
11764   if (qscope == error_mark_node || identifier == error_mark_node)
11765     ;
11766   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11767            && TREE_CODE (identifier) != BIT_NOT_EXPR)
11768     /* [namespace.udecl]
11769
11770        A using declaration shall not name a template-id.  */
11771     error ("a template-id may not appear in a using-declaration");
11772   else
11773     {
11774       if (at_class_scope_p ())
11775         {
11776           /* Create the USING_DECL.  */
11777           decl = do_class_using_decl (parser->scope, identifier);
11778
11779           if (check_for_bare_parameter_packs (decl))
11780             return false;
11781           else
11782             /* Add it to the list of members in this class.  */
11783             finish_member_declaration (decl);
11784         }
11785       else
11786         {
11787           decl = cp_parser_lookup_name_simple (parser, identifier);
11788           if (decl == error_mark_node)
11789             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11790           else if (check_for_bare_parameter_packs (decl))
11791             return false;
11792           else if (!at_namespace_scope_p ())
11793             do_local_using_decl (decl, qscope, identifier);
11794           else
11795             do_toplevel_using_decl (decl, qscope, identifier);
11796         }
11797     }
11798
11799   /* Look for the final `;'.  */
11800   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11801   
11802   return true;
11803 }
11804
11805 /* Parse a using-directive.
11806
11807    using-directive:
11808      using namespace :: [opt] nested-name-specifier [opt]
11809        namespace-name ;  */
11810
11811 static void
11812 cp_parser_using_directive (cp_parser* parser)
11813 {
11814   tree namespace_decl;
11815   tree attribs;
11816
11817   /* Look for the `using' keyword.  */
11818   cp_parser_require_keyword (parser, RID_USING, "`using'");
11819   /* And the `namespace' keyword.  */
11820   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11821   /* Look for the optional `::' operator.  */
11822   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11823   /* And the optional nested-name-specifier.  */
11824   cp_parser_nested_name_specifier_opt (parser,
11825                                        /*typename_keyword_p=*/false,
11826                                        /*check_dependency_p=*/true,
11827                                        /*type_p=*/false,
11828                                        /*is_declaration=*/true);
11829   /* Get the namespace being used.  */
11830   namespace_decl = cp_parser_namespace_name (parser);
11831   /* And any specified attributes.  */
11832   attribs = cp_parser_attributes_opt (parser);
11833   /* Update the symbol table.  */
11834   parse_using_directive (namespace_decl, attribs);
11835   /* Look for the final `;'.  */
11836   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11837 }
11838
11839 /* Parse an asm-definition.
11840
11841    asm-definition:
11842      asm ( string-literal ) ;
11843
11844    GNU Extension:
11845
11846    asm-definition:
11847      asm volatile [opt] ( string-literal ) ;
11848      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11849      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11850                           : asm-operand-list [opt] ) ;
11851      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11852                           : asm-operand-list [opt]
11853                           : asm-operand-list [opt] ) ;  */
11854
11855 static void
11856 cp_parser_asm_definition (cp_parser* parser)
11857 {
11858   tree string;
11859   tree outputs = NULL_TREE;
11860   tree inputs = NULL_TREE;
11861   tree clobbers = NULL_TREE;
11862   tree asm_stmt;
11863   bool volatile_p = false;
11864   bool extended_p = false;
11865   bool invalid_inputs_p = false;
11866   bool invalid_outputs_p = false;
11867
11868   /* Look for the `asm' keyword.  */
11869   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11870   /* See if the next token is `volatile'.  */
11871   if (cp_parser_allow_gnu_extensions_p (parser)
11872       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11873     {
11874       /* Remember that we saw the `volatile' keyword.  */
11875       volatile_p = true;
11876       /* Consume the token.  */
11877       cp_lexer_consume_token (parser->lexer);
11878     }
11879   /* Look for the opening `('.  */
11880   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11881     return;
11882   /* Look for the string.  */
11883   string = cp_parser_string_literal (parser, false, false);
11884   if (string == error_mark_node)
11885     {
11886       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11887                                              /*consume_paren=*/true);
11888       return;
11889     }
11890
11891   /* If we're allowing GNU extensions, check for the extended assembly
11892      syntax.  Unfortunately, the `:' tokens need not be separated by
11893      a space in C, and so, for compatibility, we tolerate that here
11894      too.  Doing that means that we have to treat the `::' operator as
11895      two `:' tokens.  */
11896   if (cp_parser_allow_gnu_extensions_p (parser)
11897       && parser->in_function_body
11898       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11899           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11900     {
11901       bool inputs_p = false;
11902       bool clobbers_p = false;
11903
11904       /* The extended syntax was used.  */
11905       extended_p = true;
11906
11907       /* Look for outputs.  */
11908       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11909         {
11910           /* Consume the `:'.  */
11911           cp_lexer_consume_token (parser->lexer);
11912           /* Parse the output-operands.  */
11913           if (cp_lexer_next_token_is_not (parser->lexer,
11914                                           CPP_COLON)
11915               && cp_lexer_next_token_is_not (parser->lexer,
11916                                              CPP_SCOPE)
11917               && cp_lexer_next_token_is_not (parser->lexer,
11918                                              CPP_CLOSE_PAREN))
11919             outputs = cp_parser_asm_operand_list (parser);
11920
11921             if (outputs == error_mark_node)
11922               invalid_outputs_p = true;
11923         }
11924       /* If the next token is `::', there are no outputs, and the
11925          next token is the beginning of the inputs.  */
11926       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11927         /* The inputs are coming next.  */
11928         inputs_p = true;
11929
11930       /* Look for inputs.  */
11931       if (inputs_p
11932           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11933         {
11934           /* Consume the `:' or `::'.  */
11935           cp_lexer_consume_token (parser->lexer);
11936           /* Parse the output-operands.  */
11937           if (cp_lexer_next_token_is_not (parser->lexer,
11938                                           CPP_COLON)
11939               && cp_lexer_next_token_is_not (parser->lexer,
11940                                              CPP_CLOSE_PAREN))
11941             inputs = cp_parser_asm_operand_list (parser);
11942
11943             if (inputs == error_mark_node)
11944               invalid_inputs_p = true;
11945         }
11946       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11947         /* The clobbers are coming next.  */
11948         clobbers_p = true;
11949
11950       /* Look for clobbers.  */
11951       if (clobbers_p
11952           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11953         {
11954           /* Consume the `:' or `::'.  */
11955           cp_lexer_consume_token (parser->lexer);
11956           /* Parse the clobbers.  */
11957           if (cp_lexer_next_token_is_not (parser->lexer,
11958                                           CPP_CLOSE_PAREN))
11959             clobbers = cp_parser_asm_clobber_list (parser);
11960         }
11961     }
11962   /* Look for the closing `)'.  */
11963   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11964     cp_parser_skip_to_closing_parenthesis (parser, true, false,
11965                                            /*consume_paren=*/true);
11966   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11967
11968   if (!invalid_inputs_p && !invalid_outputs_p)
11969     {
11970       /* Create the ASM_EXPR.  */
11971       if (parser->in_function_body)
11972         {
11973           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11974                                       inputs, clobbers);
11975           /* If the extended syntax was not used, mark the ASM_EXPR.  */
11976           if (!extended_p)
11977             {
11978               tree temp = asm_stmt;
11979               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11980                 temp = TREE_OPERAND (temp, 0);
11981
11982               ASM_INPUT_P (temp) = 1;
11983             }
11984         }
11985       else
11986         cgraph_add_asm_node (string);
11987     }
11988 }
11989
11990 /* Declarators [gram.dcl.decl] */
11991
11992 /* Parse an init-declarator.
11993
11994    init-declarator:
11995      declarator initializer [opt]
11996
11997    GNU Extension:
11998
11999    init-declarator:
12000      declarator asm-specification [opt] attributes [opt] initializer [opt]
12001
12002    function-definition:
12003      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12004        function-body
12005      decl-specifier-seq [opt] declarator function-try-block
12006
12007    GNU Extension:
12008
12009    function-definition:
12010      __extension__ function-definition
12011
12012    The DECL_SPECIFIERS apply to this declarator.  Returns a
12013    representation of the entity declared.  If MEMBER_P is TRUE, then
12014    this declarator appears in a class scope.  The new DECL created by
12015    this declarator is returned.
12016
12017    The CHECKS are access checks that should be performed once we know
12018    what entity is being declared (and, therefore, what classes have
12019    befriended it).
12020
12021    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12022    for a function-definition here as well.  If the declarator is a
12023    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12024    be TRUE upon return.  By that point, the function-definition will
12025    have been completely parsed.
12026
12027    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12028    is FALSE.  */
12029
12030 static tree
12031 cp_parser_init_declarator (cp_parser* parser,
12032                            cp_decl_specifier_seq *decl_specifiers,
12033                            VEC (deferred_access_check,gc)* checks,
12034                            bool function_definition_allowed_p,
12035                            bool member_p,
12036                            int declares_class_or_enum,
12037                            bool* function_definition_p)
12038 {
12039   cp_token *token;
12040   cp_declarator *declarator;
12041   tree prefix_attributes;
12042   tree attributes;
12043   tree asm_specification;
12044   tree initializer;
12045   tree decl = NULL_TREE;
12046   tree scope;
12047   bool is_initialized;
12048   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12049      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12050      "(...)".  */
12051   enum cpp_ttype initialization_kind;
12052   bool is_parenthesized_init = false;
12053   bool is_non_constant_init;
12054   int ctor_dtor_or_conv_p;
12055   bool friend_p;
12056   tree pushed_scope = NULL;
12057
12058   /* Gather the attributes that were provided with the
12059      decl-specifiers.  */
12060   prefix_attributes = decl_specifiers->attributes;
12061
12062   /* Assume that this is not the declarator for a function
12063      definition.  */
12064   if (function_definition_p)
12065     *function_definition_p = false;
12066
12067   /* Defer access checks while parsing the declarator; we cannot know
12068      what names are accessible until we know what is being
12069      declared.  */
12070   resume_deferring_access_checks ();
12071
12072   /* Parse the declarator.  */
12073   declarator
12074     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12075                             &ctor_dtor_or_conv_p,
12076                             /*parenthesized_p=*/NULL,
12077                             /*member_p=*/false);
12078   /* Gather up the deferred checks.  */
12079   stop_deferring_access_checks ();
12080
12081   /* If the DECLARATOR was erroneous, there's no need to go
12082      further.  */
12083   if (declarator == cp_error_declarator)
12084     return error_mark_node;
12085
12086   /* Check that the number of template-parameter-lists is OK.  */
12087   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12088     return error_mark_node;
12089
12090   if (declares_class_or_enum & 2)
12091     cp_parser_check_for_definition_in_return_type (declarator,
12092                                                    decl_specifiers->type);
12093
12094   /* Figure out what scope the entity declared by the DECLARATOR is
12095      located in.  `grokdeclarator' sometimes changes the scope, so
12096      we compute it now.  */
12097   scope = get_scope_of_declarator (declarator);
12098
12099   /* If we're allowing GNU extensions, look for an asm-specification
12100      and attributes.  */
12101   if (cp_parser_allow_gnu_extensions_p (parser))
12102     {
12103       /* Look for an asm-specification.  */
12104       asm_specification = cp_parser_asm_specification_opt (parser);
12105       /* And attributes.  */
12106       attributes = cp_parser_attributes_opt (parser);
12107     }
12108   else
12109     {
12110       asm_specification = NULL_TREE;
12111       attributes = NULL_TREE;
12112     }
12113
12114   /* Peek at the next token.  */
12115   token = cp_lexer_peek_token (parser->lexer);
12116   /* Check to see if the token indicates the start of a
12117      function-definition.  */
12118   if (cp_parser_token_starts_function_definition_p (token))
12119     {
12120       if (!function_definition_allowed_p)
12121         {
12122           /* If a function-definition should not appear here, issue an
12123              error message.  */
12124           cp_parser_error (parser,
12125                            "a function-definition is not allowed here");
12126           return error_mark_node;
12127         }
12128       else
12129         {
12130           /* Neither attributes nor an asm-specification are allowed
12131              on a function-definition.  */
12132           if (asm_specification)
12133             error ("an asm-specification is not allowed on a function-definition");
12134           if (attributes)
12135             error ("attributes are not allowed on a function-definition");
12136           /* This is a function-definition.  */
12137           *function_definition_p = true;
12138
12139           /* Parse the function definition.  */
12140           if (member_p)
12141             decl = cp_parser_save_member_function_body (parser,
12142                                                         decl_specifiers,
12143                                                         declarator,
12144                                                         prefix_attributes);
12145           else
12146             decl
12147               = (cp_parser_function_definition_from_specifiers_and_declarator
12148                  (parser, decl_specifiers, prefix_attributes, declarator));
12149
12150           return decl;
12151         }
12152     }
12153
12154   /* [dcl.dcl]
12155
12156      Only in function declarations for constructors, destructors, and
12157      type conversions can the decl-specifier-seq be omitted.
12158
12159      We explicitly postpone this check past the point where we handle
12160      function-definitions because we tolerate function-definitions
12161      that are missing their return types in some modes.  */
12162   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12163     {
12164       cp_parser_error (parser,
12165                        "expected constructor, destructor, or type conversion");
12166       return error_mark_node;
12167     }
12168
12169   /* An `=' or an `(' indicates an initializer.  */
12170   if (token->type == CPP_EQ
12171       || token->type == CPP_OPEN_PAREN)
12172     {
12173       is_initialized = true;
12174       initialization_kind = token->type;
12175     }
12176   else
12177     {
12178       /* If the init-declarator isn't initialized and isn't followed by a
12179          `,' or `;', it's not a valid init-declarator.  */
12180       if (token->type != CPP_COMMA
12181           && token->type != CPP_SEMICOLON)
12182         {
12183           cp_parser_error (parser, "expected initializer");
12184           return error_mark_node;
12185         }
12186       is_initialized = false;
12187       initialization_kind = CPP_EOF;
12188     }
12189
12190   /* Because start_decl has side-effects, we should only call it if we
12191      know we're going ahead.  By this point, we know that we cannot
12192      possibly be looking at any other construct.  */
12193   cp_parser_commit_to_tentative_parse (parser);
12194
12195   /* If the decl specifiers were bad, issue an error now that we're
12196      sure this was intended to be a declarator.  Then continue
12197      declaring the variable(s), as int, to try to cut down on further
12198      errors.  */
12199   if (decl_specifiers->any_specifiers_p
12200       && decl_specifiers->type == error_mark_node)
12201     {
12202       cp_parser_error (parser, "invalid type in declaration");
12203       decl_specifiers->type = integer_type_node;
12204     }
12205
12206   /* Check to see whether or not this declaration is a friend.  */
12207   friend_p = cp_parser_friend_p (decl_specifiers);
12208
12209   /* Enter the newly declared entry in the symbol table.  If we're
12210      processing a declaration in a class-specifier, we wait until
12211      after processing the initializer.  */
12212   if (!member_p)
12213     {
12214       if (parser->in_unbraced_linkage_specification_p)
12215         decl_specifiers->storage_class = sc_extern;
12216       decl = start_decl (declarator, decl_specifiers,
12217                          is_initialized, attributes, prefix_attributes,
12218                          &pushed_scope);
12219     }
12220   else if (scope)
12221     /* Enter the SCOPE.  That way unqualified names appearing in the
12222        initializer will be looked up in SCOPE.  */
12223     pushed_scope = push_scope (scope);
12224
12225   /* Perform deferred access control checks, now that we know in which
12226      SCOPE the declared entity resides.  */
12227   if (!member_p && decl)
12228     {
12229       tree saved_current_function_decl = NULL_TREE;
12230
12231       /* If the entity being declared is a function, pretend that we
12232          are in its scope.  If it is a `friend', it may have access to
12233          things that would not otherwise be accessible.  */
12234       if (TREE_CODE (decl) == FUNCTION_DECL)
12235         {
12236           saved_current_function_decl = current_function_decl;
12237           current_function_decl = decl;
12238         }
12239
12240       /* Perform access checks for template parameters.  */
12241       cp_parser_perform_template_parameter_access_checks (checks);
12242
12243       /* Perform the access control checks for the declarator and the
12244          the decl-specifiers.  */
12245       perform_deferred_access_checks ();
12246
12247       /* Restore the saved value.  */
12248       if (TREE_CODE (decl) == FUNCTION_DECL)
12249         current_function_decl = saved_current_function_decl;
12250     }
12251
12252   /* Parse the initializer.  */
12253   initializer = NULL_TREE;
12254   is_parenthesized_init = false;
12255   is_non_constant_init = true;
12256   if (is_initialized)
12257     {
12258       if (function_declarator_p (declarator))
12259         {
12260            if (initialization_kind == CPP_EQ)
12261              initializer = cp_parser_pure_specifier (parser);
12262            else
12263              {
12264                /* If the declaration was erroneous, we don't really
12265                   know what the user intended, so just silently
12266                   consume the initializer.  */
12267                if (decl != error_mark_node)
12268                  error ("initializer provided for function");
12269                cp_parser_skip_to_closing_parenthesis (parser,
12270                                                       /*recovering=*/true,
12271                                                       /*or_comma=*/false,
12272                                                       /*consume_paren=*/true);
12273              }
12274         }
12275       else
12276         initializer = cp_parser_initializer (parser,
12277                                              &is_parenthesized_init,
12278                                              &is_non_constant_init);
12279     }
12280
12281   /* The old parser allows attributes to appear after a parenthesized
12282      initializer.  Mark Mitchell proposed removing this functionality
12283      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12284      attributes -- but ignores them.  */
12285   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12286     if (cp_parser_attributes_opt (parser))
12287       warning (OPT_Wattributes,
12288                "attributes after parenthesized initializer ignored");
12289
12290   /* For an in-class declaration, use `grokfield' to create the
12291      declaration.  */
12292   if (member_p)
12293     {
12294       if (pushed_scope)
12295         {
12296           pop_scope (pushed_scope);
12297           pushed_scope = false;
12298         }
12299       decl = grokfield (declarator, decl_specifiers,
12300                         initializer, !is_non_constant_init,
12301                         /*asmspec=*/NULL_TREE,
12302                         prefix_attributes);
12303       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12304         cp_parser_save_default_args (parser, decl);
12305     }
12306
12307   /* Finish processing the declaration.  But, skip friend
12308      declarations.  */
12309   if (!friend_p && decl && decl != error_mark_node)
12310     {
12311       cp_finish_decl (decl,
12312                       initializer, !is_non_constant_init,
12313                       asm_specification,
12314                       /* If the initializer is in parentheses, then this is
12315                          a direct-initialization, which means that an
12316                          `explicit' constructor is OK.  Otherwise, an
12317                          `explicit' constructor cannot be used.  */
12318                       ((is_parenthesized_init || !is_initialized)
12319                      ? 0 : LOOKUP_ONLYCONVERTING));
12320     }
12321   else if ((cxx_dialect != cxx98) && friend_p
12322            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12323     /* Core issue #226 (C++0x only): A default template-argument
12324        shall not be specified in a friend class template
12325        declaration. */
12326     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12327                              /*is_partial=*/0, /*is_friend_decl=*/1);
12328
12329   if (!friend_p && pushed_scope)
12330     pop_scope (pushed_scope);
12331
12332   return decl;
12333 }
12334
12335 /* Parse a declarator.
12336
12337    declarator:
12338      direct-declarator
12339      ptr-operator declarator
12340
12341    abstract-declarator:
12342      ptr-operator abstract-declarator [opt]
12343      direct-abstract-declarator
12344
12345    GNU Extensions:
12346
12347    declarator:
12348      attributes [opt] direct-declarator
12349      attributes [opt] ptr-operator declarator
12350
12351    abstract-declarator:
12352      attributes [opt] ptr-operator abstract-declarator [opt]
12353      attributes [opt] direct-abstract-declarator
12354
12355    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12356    detect constructor, destructor or conversion operators. It is set
12357    to -1 if the declarator is a name, and +1 if it is a
12358    function. Otherwise it is set to zero. Usually you just want to
12359    test for >0, but internally the negative value is used.
12360
12361    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12362    a decl-specifier-seq unless it declares a constructor, destructor,
12363    or conversion.  It might seem that we could check this condition in
12364    semantic analysis, rather than parsing, but that makes it difficult
12365    to handle something like `f()'.  We want to notice that there are
12366    no decl-specifiers, and therefore realize that this is an
12367    expression, not a declaration.)
12368
12369    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12370    the declarator is a direct-declarator of the form "(...)".
12371
12372    MEMBER_P is true iff this declarator is a member-declarator.  */
12373
12374 static cp_declarator *
12375 cp_parser_declarator (cp_parser* parser,
12376                       cp_parser_declarator_kind dcl_kind,
12377                       int* ctor_dtor_or_conv_p,
12378                       bool* parenthesized_p,
12379                       bool member_p)
12380 {
12381   cp_token *token;
12382   cp_declarator *declarator;
12383   enum tree_code code;
12384   cp_cv_quals cv_quals;
12385   tree class_type;
12386   tree attributes = NULL_TREE;
12387
12388   /* Assume this is not a constructor, destructor, or type-conversion
12389      operator.  */
12390   if (ctor_dtor_or_conv_p)
12391     *ctor_dtor_or_conv_p = 0;
12392
12393   if (cp_parser_allow_gnu_extensions_p (parser))
12394     attributes = cp_parser_attributes_opt (parser);
12395
12396   /* Peek at the next token.  */
12397   token = cp_lexer_peek_token (parser->lexer);
12398
12399   /* Check for the ptr-operator production.  */
12400   cp_parser_parse_tentatively (parser);
12401   /* Parse the ptr-operator.  */
12402   code = cp_parser_ptr_operator (parser,
12403                                  &class_type,
12404                                  &cv_quals);
12405   /* If that worked, then we have a ptr-operator.  */
12406   if (cp_parser_parse_definitely (parser))
12407     {
12408       /* If a ptr-operator was found, then this declarator was not
12409          parenthesized.  */
12410       if (parenthesized_p)
12411         *parenthesized_p = true;
12412       /* The dependent declarator is optional if we are parsing an
12413          abstract-declarator.  */
12414       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12415         cp_parser_parse_tentatively (parser);
12416
12417       /* Parse the dependent declarator.  */
12418       declarator = cp_parser_declarator (parser, dcl_kind,
12419                                          /*ctor_dtor_or_conv_p=*/NULL,
12420                                          /*parenthesized_p=*/NULL,
12421                                          /*member_p=*/false);
12422
12423       /* If we are parsing an abstract-declarator, we must handle the
12424          case where the dependent declarator is absent.  */
12425       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12426           && !cp_parser_parse_definitely (parser))
12427         declarator = NULL;
12428
12429       declarator = cp_parser_make_indirect_declarator
12430         (code, class_type, cv_quals, declarator);
12431     }
12432   /* Everything else is a direct-declarator.  */
12433   else
12434     {
12435       if (parenthesized_p)
12436         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12437                                                    CPP_OPEN_PAREN);
12438       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12439                                                 ctor_dtor_or_conv_p,
12440                                                 member_p);
12441     }
12442
12443   if (attributes && declarator && declarator != cp_error_declarator)
12444     declarator->attributes = attributes;
12445
12446   return declarator;
12447 }
12448
12449 /* Parse a direct-declarator or direct-abstract-declarator.
12450
12451    direct-declarator:
12452      declarator-id
12453      direct-declarator ( parameter-declaration-clause )
12454        cv-qualifier-seq [opt]
12455        exception-specification [opt]
12456      direct-declarator [ constant-expression [opt] ]
12457      ( declarator )
12458
12459    direct-abstract-declarator:
12460      direct-abstract-declarator [opt]
12461        ( parameter-declaration-clause )
12462        cv-qualifier-seq [opt]
12463        exception-specification [opt]
12464      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12465      ( abstract-declarator )
12466
12467    Returns a representation of the declarator.  DCL_KIND is
12468    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12469    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12470    we are parsing a direct-declarator.  It is
12471    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12472    of ambiguity we prefer an abstract declarator, as per
12473    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12474    cp_parser_declarator.  */
12475
12476 static cp_declarator *
12477 cp_parser_direct_declarator (cp_parser* parser,
12478                              cp_parser_declarator_kind dcl_kind,
12479                              int* ctor_dtor_or_conv_p,
12480                              bool member_p)
12481 {
12482   cp_token *token;
12483   cp_declarator *declarator = NULL;
12484   tree scope = NULL_TREE;
12485   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12486   bool saved_in_declarator_p = parser->in_declarator_p;
12487   bool first = true;
12488   tree pushed_scope = NULL_TREE;
12489
12490   while (true)
12491     {
12492       /* Peek at the next token.  */
12493       token = cp_lexer_peek_token (parser->lexer);
12494       if (token->type == CPP_OPEN_PAREN)
12495         {
12496           /* This is either a parameter-declaration-clause, or a
12497              parenthesized declarator. When we know we are parsing a
12498              named declarator, it must be a parenthesized declarator
12499              if FIRST is true. For instance, `(int)' is a
12500              parameter-declaration-clause, with an omitted
12501              direct-abstract-declarator. But `((*))', is a
12502              parenthesized abstract declarator. Finally, when T is a
12503              template parameter `(T)' is a
12504              parameter-declaration-clause, and not a parenthesized
12505              named declarator.
12506
12507              We first try and parse a parameter-declaration-clause,
12508              and then try a nested declarator (if FIRST is true).
12509
12510              It is not an error for it not to be a
12511              parameter-declaration-clause, even when FIRST is
12512              false. Consider,
12513
12514                int i (int);
12515                int i (3);
12516
12517              The first is the declaration of a function while the
12518              second is a the definition of a variable, including its
12519              initializer.
12520
12521              Having seen only the parenthesis, we cannot know which of
12522              these two alternatives should be selected.  Even more
12523              complex are examples like:
12524
12525                int i (int (a));
12526                int i (int (3));
12527
12528              The former is a function-declaration; the latter is a
12529              variable initialization.
12530
12531              Thus again, we try a parameter-declaration-clause, and if
12532              that fails, we back out and return.  */
12533
12534           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12535             {
12536               cp_parameter_declarator *params;
12537               unsigned saved_num_template_parameter_lists;
12538
12539               /* In a member-declarator, the only valid interpretation
12540                  of a parenthesis is the start of a
12541                  parameter-declaration-clause.  (It is invalid to
12542                  initialize a static data member with a parenthesized
12543                  initializer; only the "=" form of initialization is
12544                  permitted.)  */
12545               if (!member_p)
12546                 cp_parser_parse_tentatively (parser);
12547
12548               /* Consume the `('.  */
12549               cp_lexer_consume_token (parser->lexer);
12550               if (first)
12551                 {
12552                   /* If this is going to be an abstract declarator, we're
12553                      in a declarator and we can't have default args.  */
12554                   parser->default_arg_ok_p = false;
12555                   parser->in_declarator_p = true;
12556                 }
12557
12558               /* Inside the function parameter list, surrounding
12559                  template-parameter-lists do not apply.  */
12560               saved_num_template_parameter_lists
12561                 = parser->num_template_parameter_lists;
12562               parser->num_template_parameter_lists = 0;
12563
12564               /* Parse the parameter-declaration-clause.  */
12565               params = cp_parser_parameter_declaration_clause (parser);
12566
12567               parser->num_template_parameter_lists
12568                 = saved_num_template_parameter_lists;
12569
12570               /* If all went well, parse the cv-qualifier-seq and the
12571                  exception-specification.  */
12572               if (member_p || cp_parser_parse_definitely (parser))
12573                 {
12574                   cp_cv_quals cv_quals;
12575                   tree exception_specification;
12576
12577                   if (ctor_dtor_or_conv_p)
12578                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12579                   first = false;
12580                   /* Consume the `)'.  */
12581                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12582
12583                   /* Parse the cv-qualifier-seq.  */
12584                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12585                   /* And the exception-specification.  */
12586                   exception_specification
12587                     = cp_parser_exception_specification_opt (parser);
12588
12589                   /* Create the function-declarator.  */
12590                   declarator = make_call_declarator (declarator,
12591                                                      params,
12592                                                      cv_quals,
12593                                                      exception_specification);
12594                   /* Any subsequent parameter lists are to do with
12595                      return type, so are not those of the declared
12596                      function.  */
12597                   parser->default_arg_ok_p = false;
12598
12599                   /* Repeat the main loop.  */
12600                   continue;
12601                 }
12602             }
12603
12604           /* If this is the first, we can try a parenthesized
12605              declarator.  */
12606           if (first)
12607             {
12608               bool saved_in_type_id_in_expr_p;
12609
12610               parser->default_arg_ok_p = saved_default_arg_ok_p;
12611               parser->in_declarator_p = saved_in_declarator_p;
12612
12613               /* Consume the `('.  */
12614               cp_lexer_consume_token (parser->lexer);
12615               /* Parse the nested declarator.  */
12616               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12617               parser->in_type_id_in_expr_p = true;
12618               declarator
12619                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12620                                         /*parenthesized_p=*/NULL,
12621                                         member_p);
12622               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12623               first = false;
12624               /* Expect a `)'.  */
12625               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12626                 declarator = cp_error_declarator;
12627               if (declarator == cp_error_declarator)
12628                 break;
12629
12630               goto handle_declarator;
12631             }
12632           /* Otherwise, we must be done.  */
12633           else
12634             break;
12635         }
12636       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12637                && token->type == CPP_OPEN_SQUARE)
12638         {
12639           /* Parse an array-declarator.  */
12640           tree bounds;
12641
12642           if (ctor_dtor_or_conv_p)
12643             *ctor_dtor_or_conv_p = 0;
12644
12645           first = false;
12646           parser->default_arg_ok_p = false;
12647           parser->in_declarator_p = true;
12648           /* Consume the `['.  */
12649           cp_lexer_consume_token (parser->lexer);
12650           /* Peek at the next token.  */
12651           token = cp_lexer_peek_token (parser->lexer);
12652           /* If the next token is `]', then there is no
12653              constant-expression.  */
12654           if (token->type != CPP_CLOSE_SQUARE)
12655             {
12656               bool non_constant_p;
12657
12658               bounds
12659                 = cp_parser_constant_expression (parser,
12660                                                  /*allow_non_constant=*/true,
12661                                                  &non_constant_p);
12662               if (!non_constant_p)
12663                 bounds = fold_non_dependent_expr (bounds);
12664               /* Normally, the array bound must be an integral constant
12665                  expression.  However, as an extension, we allow VLAs
12666                  in function scopes.  */
12667               else if (!parser->in_function_body)
12668                 {
12669                   error ("array bound is not an integer constant");
12670                   bounds = error_mark_node;
12671                 }
12672             }
12673           else
12674             bounds = NULL_TREE;
12675           /* Look for the closing `]'.  */
12676           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12677             {
12678               declarator = cp_error_declarator;
12679               break;
12680             }
12681
12682           declarator = make_array_declarator (declarator, bounds);
12683         }
12684       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12685         {
12686           tree qualifying_scope;
12687           tree unqualified_name;
12688           special_function_kind sfk;
12689           bool abstract_ok;
12690           bool pack_expansion_p = false;
12691
12692           /* Parse a declarator-id */
12693           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12694           if (abstract_ok)
12695             {
12696               cp_parser_parse_tentatively (parser);
12697
12698               /* If we see an ellipsis, we should be looking at a
12699                  parameter pack. */
12700               if (token->type == CPP_ELLIPSIS)
12701                 {
12702                   /* Consume the `...' */
12703                   cp_lexer_consume_token (parser->lexer);
12704
12705                   pack_expansion_p = true;
12706                 }
12707             }
12708
12709           unqualified_name
12710             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12711           qualifying_scope = parser->scope;
12712           if (abstract_ok)
12713             {
12714               bool okay = false;
12715
12716               if (!unqualified_name && pack_expansion_p)
12717                 {
12718                   /* Check whether an error occurred. */
12719                   okay = !cp_parser_error_occurred (parser);
12720
12721                   /* We already consumed the ellipsis to mark a
12722                      parameter pack, but we have no way to report it,
12723                      so abort the tentative parse. We will be exiting
12724                      immediately anyway. */
12725                   cp_parser_abort_tentative_parse (parser);
12726                 }
12727               else
12728                 okay = cp_parser_parse_definitely (parser);
12729
12730               if (!okay)
12731                 unqualified_name = error_mark_node;
12732               else if (unqualified_name
12733                        && (qualifying_scope
12734                            || (TREE_CODE (unqualified_name)
12735                                != IDENTIFIER_NODE)))
12736                 {
12737                   cp_parser_error (parser, "expected unqualified-id");
12738                   unqualified_name = error_mark_node;
12739                 }
12740             }
12741
12742           if (!unqualified_name)
12743             return NULL;
12744           if (unqualified_name == error_mark_node)
12745             {
12746               declarator = cp_error_declarator;
12747               pack_expansion_p = false;
12748               declarator->parameter_pack_p = false;
12749               break;
12750             }
12751
12752           if (qualifying_scope && at_namespace_scope_p ()
12753               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12754             {
12755               /* In the declaration of a member of a template class
12756                  outside of the class itself, the SCOPE will sometimes
12757                  be a TYPENAME_TYPE.  For example, given:
12758
12759                  template <typename T>
12760                  int S<T>::R::i = 3;
12761
12762                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
12763                  this context, we must resolve S<T>::R to an ordinary
12764                  type, rather than a typename type.
12765
12766                  The reason we normally avoid resolving TYPENAME_TYPEs
12767                  is that a specialization of `S' might render
12768                  `S<T>::R' not a type.  However, if `S' is
12769                  specialized, then this `i' will not be used, so there
12770                  is no harm in resolving the types here.  */
12771               tree type;
12772
12773               /* Resolve the TYPENAME_TYPE.  */
12774               type = resolve_typename_type (qualifying_scope,
12775                                             /*only_current_p=*/false);
12776               /* If that failed, the declarator is invalid.  */
12777               if (TREE_CODE (type) == TYPENAME_TYPE)
12778                 error ("%<%T::%E%> is not a type",
12779                        TYPE_CONTEXT (qualifying_scope),
12780                        TYPE_IDENTIFIER (qualifying_scope));
12781               qualifying_scope = type;
12782             }
12783
12784           sfk = sfk_none;
12785
12786           if (unqualified_name)
12787             {
12788               tree class_type;
12789
12790               if (qualifying_scope
12791                   && CLASS_TYPE_P (qualifying_scope))
12792                 class_type = qualifying_scope;
12793               else
12794                 class_type = current_class_type;
12795
12796               if (TREE_CODE (unqualified_name) == TYPE_DECL)
12797                 {
12798                   tree name_type = TREE_TYPE (unqualified_name);
12799                   if (class_type && same_type_p (name_type, class_type))
12800                     {
12801                       if (qualifying_scope
12802                           && CLASSTYPE_USE_TEMPLATE (name_type))
12803                         {
12804                           error ("invalid use of constructor as a template");
12805                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12806                                   "name the constructor in a qualified name",
12807                                   class_type,
12808                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12809                                   class_type, name_type);
12810                           declarator = cp_error_declarator;
12811                           break;
12812                         }
12813                       else
12814                         unqualified_name = constructor_name (class_type);
12815                     }
12816                   else
12817                     {
12818                       /* We do not attempt to print the declarator
12819                          here because we do not have enough
12820                          information about its original syntactic
12821                          form.  */
12822                       cp_parser_error (parser, "invalid declarator");
12823                       declarator = cp_error_declarator;
12824                       break;
12825                     }
12826                 }
12827
12828               if (class_type)
12829                 {
12830                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12831                     sfk = sfk_destructor;
12832                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12833                     sfk = sfk_conversion;
12834                   else if (/* There's no way to declare a constructor
12835                               for an anonymous type, even if the type
12836                               got a name for linkage purposes.  */
12837                            !TYPE_WAS_ANONYMOUS (class_type)
12838                            && constructor_name_p (unqualified_name,
12839                                                   class_type))
12840                     {
12841                       unqualified_name = constructor_name (class_type);
12842                       sfk = sfk_constructor;
12843                     }
12844
12845                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
12846                     *ctor_dtor_or_conv_p = -1;
12847                 }
12848             }
12849           declarator = make_id_declarator (qualifying_scope,
12850                                            unqualified_name,
12851                                            sfk);
12852           declarator->id_loc = token->location;
12853           declarator->parameter_pack_p = pack_expansion_p;
12854
12855           if (pack_expansion_p)
12856             maybe_warn_variadic_templates ();
12857
12858         handle_declarator:;
12859           scope = get_scope_of_declarator (declarator);
12860           if (scope)
12861             /* Any names that appear after the declarator-id for a
12862                member are looked up in the containing scope.  */
12863             pushed_scope = push_scope (scope);
12864           parser->in_declarator_p = true;
12865           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12866               || (declarator && declarator->kind == cdk_id))
12867             /* Default args are only allowed on function
12868                declarations.  */
12869             parser->default_arg_ok_p = saved_default_arg_ok_p;
12870           else
12871             parser->default_arg_ok_p = false;
12872
12873           first = false;
12874         }
12875       /* We're done.  */
12876       else
12877         break;
12878     }
12879
12880   /* For an abstract declarator, we might wind up with nothing at this
12881      point.  That's an error; the declarator is not optional.  */
12882   if (!declarator)
12883     cp_parser_error (parser, "expected declarator");
12884
12885   /* If we entered a scope, we must exit it now.  */
12886   if (pushed_scope)
12887     pop_scope (pushed_scope);
12888
12889   parser->default_arg_ok_p = saved_default_arg_ok_p;
12890   parser->in_declarator_p = saved_in_declarator_p;
12891
12892   return declarator;
12893 }
12894
12895 /* Parse a ptr-operator.
12896
12897    ptr-operator:
12898      * cv-qualifier-seq [opt]
12899      &
12900      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12901
12902    GNU Extension:
12903
12904    ptr-operator:
12905      & cv-qualifier-seq [opt]
12906
12907    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12908    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12909    an rvalue reference. In the case of a pointer-to-member, *TYPE is
12910    filled in with the TYPE containing the member.  *CV_QUALS is
12911    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12912    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
12913    Note that the tree codes returned by this function have nothing
12914    to do with the types of trees that will be eventually be created
12915    to represent the pointer or reference type being parsed. They are
12916    just constants with suggestive names. */
12917 static enum tree_code
12918 cp_parser_ptr_operator (cp_parser* parser,
12919                         tree* type,
12920                         cp_cv_quals *cv_quals)
12921 {
12922   enum tree_code code = ERROR_MARK;
12923   cp_token *token;
12924
12925   /* Assume that it's not a pointer-to-member.  */
12926   *type = NULL_TREE;
12927   /* And that there are no cv-qualifiers.  */
12928   *cv_quals = TYPE_UNQUALIFIED;
12929
12930   /* Peek at the next token.  */
12931   token = cp_lexer_peek_token (parser->lexer);
12932
12933   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
12934   if (token->type == CPP_MULT)
12935     code = INDIRECT_REF;
12936   else if (token->type == CPP_AND)
12937     code = ADDR_EXPR;
12938   else if ((cxx_dialect != cxx98) &&
12939            token->type == CPP_AND_AND) /* C++0x only */
12940     code = NON_LVALUE_EXPR;
12941
12942   if (code != ERROR_MARK)
12943     {
12944       /* Consume the `*', `&' or `&&'.  */
12945       cp_lexer_consume_token (parser->lexer);
12946
12947       /* A `*' can be followed by a cv-qualifier-seq, and so can a
12948          `&', if we are allowing GNU extensions.  (The only qualifier
12949          that can legally appear after `&' is `restrict', but that is
12950          enforced during semantic analysis.  */
12951       if (code == INDIRECT_REF
12952           || cp_parser_allow_gnu_extensions_p (parser))
12953         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12954     }
12955   else
12956     {
12957       /* Try the pointer-to-member case.  */
12958       cp_parser_parse_tentatively (parser);
12959       /* Look for the optional `::' operator.  */
12960       cp_parser_global_scope_opt (parser,
12961                                   /*current_scope_valid_p=*/false);
12962       /* Look for the nested-name specifier.  */
12963       cp_parser_nested_name_specifier (parser,
12964                                        /*typename_keyword_p=*/false,
12965                                        /*check_dependency_p=*/true,
12966                                        /*type_p=*/false,
12967                                        /*is_declaration=*/false);
12968       /* If we found it, and the next token is a `*', then we are
12969          indeed looking at a pointer-to-member operator.  */
12970       if (!cp_parser_error_occurred (parser)
12971           && cp_parser_require (parser, CPP_MULT, "`*'"))
12972         {
12973           /* Indicate that the `*' operator was used.  */
12974           code = INDIRECT_REF;
12975
12976           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12977             error ("%qD is a namespace", parser->scope);
12978           else
12979             {
12980               /* The type of which the member is a member is given by the
12981                  current SCOPE.  */
12982               *type = parser->scope;
12983               /* The next name will not be qualified.  */
12984               parser->scope = NULL_TREE;
12985               parser->qualifying_scope = NULL_TREE;
12986               parser->object_scope = NULL_TREE;
12987               /* Look for the optional cv-qualifier-seq.  */
12988               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12989             }
12990         }
12991       /* If that didn't work we don't have a ptr-operator.  */
12992       if (!cp_parser_parse_definitely (parser))
12993         cp_parser_error (parser, "expected ptr-operator");
12994     }
12995
12996   return code;
12997 }
12998
12999 /* Parse an (optional) cv-qualifier-seq.
13000
13001    cv-qualifier-seq:
13002      cv-qualifier cv-qualifier-seq [opt]
13003
13004    cv-qualifier:
13005      const
13006      volatile
13007
13008    GNU Extension:
13009
13010    cv-qualifier:
13011      __restrict__
13012
13013    Returns a bitmask representing the cv-qualifiers.  */
13014
13015 static cp_cv_quals
13016 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13017 {
13018   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13019
13020   while (true)
13021     {
13022       cp_token *token;
13023       cp_cv_quals cv_qualifier;
13024
13025       /* Peek at the next token.  */
13026       token = cp_lexer_peek_token (parser->lexer);
13027       /* See if it's a cv-qualifier.  */
13028       switch (token->keyword)
13029         {
13030         case RID_CONST:
13031           cv_qualifier = TYPE_QUAL_CONST;
13032           break;
13033
13034         case RID_VOLATILE:
13035           cv_qualifier = TYPE_QUAL_VOLATILE;
13036           break;
13037
13038         case RID_RESTRICT:
13039           cv_qualifier = TYPE_QUAL_RESTRICT;
13040           break;
13041
13042         default:
13043           cv_qualifier = TYPE_UNQUALIFIED;
13044           break;
13045         }
13046
13047       if (!cv_qualifier)
13048         break;
13049
13050       if (cv_quals & cv_qualifier)
13051         {
13052           error ("duplicate cv-qualifier");
13053           cp_lexer_purge_token (parser->lexer);
13054         }
13055       else
13056         {
13057           cp_lexer_consume_token (parser->lexer);
13058           cv_quals |= cv_qualifier;
13059         }
13060     }
13061
13062   return cv_quals;
13063 }
13064
13065 /* Parse a declarator-id.
13066
13067    declarator-id:
13068      id-expression
13069      :: [opt] nested-name-specifier [opt] type-name
13070
13071    In the `id-expression' case, the value returned is as for
13072    cp_parser_id_expression if the id-expression was an unqualified-id.
13073    If the id-expression was a qualified-id, then a SCOPE_REF is
13074    returned.  The first operand is the scope (either a NAMESPACE_DECL
13075    or TREE_TYPE), but the second is still just a representation of an
13076    unqualified-id.  */
13077
13078 static tree
13079 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13080 {
13081   tree id;
13082   /* The expression must be an id-expression.  Assume that qualified
13083      names are the names of types so that:
13084
13085        template <class T>
13086        int S<T>::R::i = 3;
13087
13088      will work; we must treat `S<T>::R' as the name of a type.
13089      Similarly, assume that qualified names are templates, where
13090      required, so that:
13091
13092        template <class T>
13093        int S<T>::R<T>::i = 3;
13094
13095      will work, too.  */
13096   id = cp_parser_id_expression (parser,
13097                                 /*template_keyword_p=*/false,
13098                                 /*check_dependency_p=*/false,
13099                                 /*template_p=*/NULL,
13100                                 /*declarator_p=*/true,
13101                                 optional_p);
13102   if (id && BASELINK_P (id))
13103     id = BASELINK_FUNCTIONS (id);
13104   return id;
13105 }
13106
13107 /* Parse a type-id.
13108
13109    type-id:
13110      type-specifier-seq abstract-declarator [opt]
13111
13112    Returns the TYPE specified.  */
13113
13114 static tree
13115 cp_parser_type_id (cp_parser* parser)
13116 {
13117   cp_decl_specifier_seq type_specifier_seq;
13118   cp_declarator *abstract_declarator;
13119
13120   /* Parse the type-specifier-seq.  */
13121   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13122                                 &type_specifier_seq);
13123   if (type_specifier_seq.type == error_mark_node)
13124     return error_mark_node;
13125
13126   /* There might or might not be an abstract declarator.  */
13127   cp_parser_parse_tentatively (parser);
13128   /* Look for the declarator.  */
13129   abstract_declarator
13130     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13131                             /*parenthesized_p=*/NULL,
13132                             /*member_p=*/false);
13133   /* Check to see if there really was a declarator.  */
13134   if (!cp_parser_parse_definitely (parser))
13135     abstract_declarator = NULL;
13136
13137   return groktypename (&type_specifier_seq, abstract_declarator);
13138 }
13139
13140 /* Parse a type-specifier-seq.
13141
13142    type-specifier-seq:
13143      type-specifier type-specifier-seq [opt]
13144
13145    GNU extension:
13146
13147    type-specifier-seq:
13148      attributes type-specifier-seq [opt]
13149
13150    If IS_CONDITION is true, we are at the start of a "condition",
13151    e.g., we've just seen "if (".
13152
13153    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13154
13155 static void
13156 cp_parser_type_specifier_seq (cp_parser* parser,
13157                               bool is_condition,
13158                               cp_decl_specifier_seq *type_specifier_seq)
13159 {
13160   bool seen_type_specifier = false;
13161   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13162
13163   /* Clear the TYPE_SPECIFIER_SEQ.  */
13164   clear_decl_specs (type_specifier_seq);
13165
13166   /* Parse the type-specifiers and attributes.  */
13167   while (true)
13168     {
13169       tree type_specifier;
13170       bool is_cv_qualifier;
13171
13172       /* Check for attributes first.  */
13173       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13174         {
13175           type_specifier_seq->attributes =
13176             chainon (type_specifier_seq->attributes,
13177                      cp_parser_attributes_opt (parser));
13178           continue;
13179         }
13180
13181       /* Look for the type-specifier.  */
13182       type_specifier = cp_parser_type_specifier (parser,
13183                                                  flags,
13184                                                  type_specifier_seq,
13185                                                  /*is_declaration=*/false,
13186                                                  NULL,
13187                                                  &is_cv_qualifier);
13188       if (!type_specifier)
13189         {
13190           /* If the first type-specifier could not be found, this is not a
13191              type-specifier-seq at all.  */
13192           if (!seen_type_specifier)
13193             {
13194               cp_parser_error (parser, "expected type-specifier");
13195               type_specifier_seq->type = error_mark_node;
13196               return;
13197             }
13198           /* If subsequent type-specifiers could not be found, the
13199              type-specifier-seq is complete.  */
13200           break;
13201         }
13202
13203       seen_type_specifier = true;
13204       /* The standard says that a condition can be:
13205
13206             type-specifier-seq declarator = assignment-expression
13207
13208          However, given:
13209
13210            struct S {};
13211            if (int S = ...)
13212
13213          we should treat the "S" as a declarator, not as a
13214          type-specifier.  The standard doesn't say that explicitly for
13215          type-specifier-seq, but it does say that for
13216          decl-specifier-seq in an ordinary declaration.  Perhaps it
13217          would be clearer just to allow a decl-specifier-seq here, and
13218          then add a semantic restriction that if any decl-specifiers
13219          that are not type-specifiers appear, the program is invalid.  */
13220       if (is_condition && !is_cv_qualifier)
13221         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13222     }
13223
13224   cp_parser_check_decl_spec (type_specifier_seq);
13225 }
13226
13227 /* Parse a parameter-declaration-clause.
13228
13229    parameter-declaration-clause:
13230      parameter-declaration-list [opt] ... [opt]
13231      parameter-declaration-list , ...
13232
13233    Returns a representation for the parameter declarations.  A return
13234    value of NULL indicates a parameter-declaration-clause consisting
13235    only of an ellipsis.  */
13236
13237 static cp_parameter_declarator *
13238 cp_parser_parameter_declaration_clause (cp_parser* parser)
13239 {
13240   cp_parameter_declarator *parameters;
13241   cp_token *token;
13242   bool ellipsis_p;
13243   bool is_error;
13244
13245   /* Peek at the next token.  */
13246   token = cp_lexer_peek_token (parser->lexer);
13247   /* Check for trivial parameter-declaration-clauses.  */
13248   if (token->type == CPP_ELLIPSIS)
13249     {
13250       /* Consume the `...' token.  */
13251       cp_lexer_consume_token (parser->lexer);
13252       return NULL;
13253     }
13254   else if (token->type == CPP_CLOSE_PAREN)
13255     /* There are no parameters.  */
13256     {
13257 #ifndef NO_IMPLICIT_EXTERN_C
13258       if (in_system_header && current_class_type == NULL
13259           && current_lang_name == lang_name_c)
13260         return NULL;
13261       else
13262 #endif
13263         return no_parameters;
13264     }
13265   /* Check for `(void)', too, which is a special case.  */
13266   else if (token->keyword == RID_VOID
13267            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13268                == CPP_CLOSE_PAREN))
13269     {
13270       /* Consume the `void' token.  */
13271       cp_lexer_consume_token (parser->lexer);
13272       /* There are no parameters.  */
13273       return no_parameters;
13274     }
13275
13276   /* Parse the parameter-declaration-list.  */
13277   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13278   /* If a parse error occurred while parsing the
13279      parameter-declaration-list, then the entire
13280      parameter-declaration-clause is erroneous.  */
13281   if (is_error)
13282     return NULL;
13283
13284   /* Peek at the next token.  */
13285   token = cp_lexer_peek_token (parser->lexer);
13286   /* If it's a `,', the clause should terminate with an ellipsis.  */
13287   if (token->type == CPP_COMMA)
13288     {
13289       /* Consume the `,'.  */
13290       cp_lexer_consume_token (parser->lexer);
13291       /* Expect an ellipsis.  */
13292       ellipsis_p
13293         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13294     }
13295   /* It might also be `...' if the optional trailing `,' was
13296      omitted.  */
13297   else if (token->type == CPP_ELLIPSIS)
13298     {
13299       /* Consume the `...' token.  */
13300       cp_lexer_consume_token (parser->lexer);
13301       /* And remember that we saw it.  */
13302       ellipsis_p = true;
13303     }
13304   else
13305     ellipsis_p = false;
13306
13307   /* Finish the parameter list.  */
13308   if (parameters && ellipsis_p)
13309     parameters->ellipsis_p = true;
13310
13311   return parameters;
13312 }
13313
13314 /* Parse a parameter-declaration-list.
13315
13316    parameter-declaration-list:
13317      parameter-declaration
13318      parameter-declaration-list , parameter-declaration
13319
13320    Returns a representation of the parameter-declaration-list, as for
13321    cp_parser_parameter_declaration_clause.  However, the
13322    `void_list_node' is never appended to the list.  Upon return,
13323    *IS_ERROR will be true iff an error occurred.  */
13324
13325 static cp_parameter_declarator *
13326 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13327 {
13328   cp_parameter_declarator *parameters = NULL;
13329   cp_parameter_declarator **tail = &parameters;
13330   bool saved_in_unbraced_linkage_specification_p;
13331
13332   /* Assume all will go well.  */
13333   *is_error = false;
13334   /* The special considerations that apply to a function within an
13335      unbraced linkage specifications do not apply to the parameters
13336      to the function.  */
13337   saved_in_unbraced_linkage_specification_p 
13338     = parser->in_unbraced_linkage_specification_p;
13339   parser->in_unbraced_linkage_specification_p = false;
13340
13341   /* Look for more parameters.  */
13342   while (true)
13343     {
13344       cp_parameter_declarator *parameter;
13345       bool parenthesized_p;
13346       /* Parse the parameter.  */
13347       parameter
13348         = cp_parser_parameter_declaration (parser,
13349                                            /*template_parm_p=*/false,
13350                                            &parenthesized_p);
13351
13352       /* If a parse error occurred parsing the parameter declaration,
13353          then the entire parameter-declaration-list is erroneous.  */
13354       if (!parameter)
13355         {
13356           *is_error = true;
13357           parameters = NULL;
13358           break;
13359         }
13360       /* Add the new parameter to the list.  */
13361       *tail = parameter;
13362       tail = &parameter->next;
13363
13364       /* Peek at the next token.  */
13365       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13366           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13367           /* These are for Objective-C++ */
13368           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13369           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13370         /* The parameter-declaration-list is complete.  */
13371         break;
13372       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13373         {
13374           cp_token *token;
13375
13376           /* Peek at the next token.  */
13377           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13378           /* If it's an ellipsis, then the list is complete.  */
13379           if (token->type == CPP_ELLIPSIS)
13380             break;
13381           /* Otherwise, there must be more parameters.  Consume the
13382              `,'.  */
13383           cp_lexer_consume_token (parser->lexer);
13384           /* When parsing something like:
13385
13386                 int i(float f, double d)
13387
13388              we can tell after seeing the declaration for "f" that we
13389              are not looking at an initialization of a variable "i",
13390              but rather at the declaration of a function "i".
13391
13392              Due to the fact that the parsing of template arguments
13393              (as specified to a template-id) requires backtracking we
13394              cannot use this technique when inside a template argument
13395              list.  */
13396           if (!parser->in_template_argument_list_p
13397               && !parser->in_type_id_in_expr_p
13398               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13399               /* However, a parameter-declaration of the form
13400                  "foat(f)" (which is a valid declaration of a
13401                  parameter "f") can also be interpreted as an
13402                  expression (the conversion of "f" to "float").  */
13403               && !parenthesized_p)
13404             cp_parser_commit_to_tentative_parse (parser);
13405         }
13406       else
13407         {
13408           cp_parser_error (parser, "expected %<,%> or %<...%>");
13409           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13410             cp_parser_skip_to_closing_parenthesis (parser,
13411                                                    /*recovering=*/true,
13412                                                    /*or_comma=*/false,
13413                                                    /*consume_paren=*/false);
13414           break;
13415         }
13416     }
13417
13418   parser->in_unbraced_linkage_specification_p
13419     = saved_in_unbraced_linkage_specification_p;
13420
13421   return parameters;
13422 }
13423
13424 /* Parse a parameter declaration.
13425
13426    parameter-declaration:
13427      decl-specifier-seq ... [opt] declarator
13428      decl-specifier-seq declarator = assignment-expression
13429      decl-specifier-seq ... [opt] abstract-declarator [opt]
13430      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13431
13432    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13433    declares a template parameter.  (In that case, a non-nested `>'
13434    token encountered during the parsing of the assignment-expression
13435    is not interpreted as a greater-than operator.)
13436
13437    Returns a representation of the parameter, or NULL if an error
13438    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13439    true iff the declarator is of the form "(p)".  */
13440
13441 static cp_parameter_declarator *
13442 cp_parser_parameter_declaration (cp_parser *parser,
13443                                  bool template_parm_p,
13444                                  bool *parenthesized_p)
13445 {
13446   int declares_class_or_enum;
13447   bool greater_than_is_operator_p;
13448   cp_decl_specifier_seq decl_specifiers;
13449   cp_declarator *declarator;
13450   tree default_argument;
13451   cp_token *token;
13452   const char *saved_message;
13453
13454   /* In a template parameter, `>' is not an operator.
13455
13456      [temp.param]
13457
13458      When parsing a default template-argument for a non-type
13459      template-parameter, the first non-nested `>' is taken as the end
13460      of the template parameter-list rather than a greater-than
13461      operator.  */
13462   greater_than_is_operator_p = !template_parm_p;
13463
13464   /* Type definitions may not appear in parameter types.  */
13465   saved_message = parser->type_definition_forbidden_message;
13466   parser->type_definition_forbidden_message
13467     = "types may not be defined in parameter types";
13468
13469   /* Parse the declaration-specifiers.  */
13470   cp_parser_decl_specifier_seq (parser,
13471                                 CP_PARSER_FLAGS_NONE,
13472                                 &decl_specifiers,
13473                                 &declares_class_or_enum);
13474   /* If an error occurred, there's no reason to attempt to parse the
13475      rest of the declaration.  */
13476   if (cp_parser_error_occurred (parser))
13477     {
13478       parser->type_definition_forbidden_message = saved_message;
13479       return NULL;
13480     }
13481
13482   /* Peek at the next token.  */
13483   token = cp_lexer_peek_token (parser->lexer);
13484
13485   /* If the next token is a `)', `,', `=', `>', or `...', then there
13486      is no declarator. However, when variadic templates are enabled,
13487      there may be a declarator following `...'.  */
13488   if (token->type == CPP_CLOSE_PAREN
13489       || token->type == CPP_COMMA
13490       || token->type == CPP_EQ
13491       || token->type == CPP_GREATER)
13492     {
13493       declarator = NULL;
13494       if (parenthesized_p)
13495         *parenthesized_p = false;
13496     }
13497   /* Otherwise, there should be a declarator.  */
13498   else
13499     {
13500       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13501       parser->default_arg_ok_p = false;
13502
13503       /* After seeing a decl-specifier-seq, if the next token is not a
13504          "(", there is no possibility that the code is a valid
13505          expression.  Therefore, if parsing tentatively, we commit at
13506          this point.  */
13507       if (!parser->in_template_argument_list_p
13508           /* In an expression context, having seen:
13509
13510                (int((char ...
13511
13512              we cannot be sure whether we are looking at a
13513              function-type (taking a "char" as a parameter) or a cast
13514              of some object of type "char" to "int".  */
13515           && !parser->in_type_id_in_expr_p
13516           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13517           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13518         cp_parser_commit_to_tentative_parse (parser);
13519       /* Parse the declarator.  */
13520       declarator = cp_parser_declarator (parser,
13521                                          CP_PARSER_DECLARATOR_EITHER,
13522                                          /*ctor_dtor_or_conv_p=*/NULL,
13523                                          parenthesized_p,
13524                                          /*member_p=*/false);
13525       parser->default_arg_ok_p = saved_default_arg_ok_p;
13526       /* After the declarator, allow more attributes.  */
13527       decl_specifiers.attributes
13528         = chainon (decl_specifiers.attributes,
13529                    cp_parser_attributes_opt (parser));
13530     }
13531
13532   /* If the next token is an ellipsis, and we have not seen a
13533      declarator name, and the type of the declarator contains parameter
13534      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13535      a parameter pack expansion expression. Otherwise, leave the
13536      ellipsis for a C-style variadic function. */
13537   token = cp_lexer_peek_token (parser->lexer);
13538   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13539     {
13540       tree type = decl_specifiers.type;
13541
13542       if (type && DECL_P (type))
13543         type = TREE_TYPE (type);
13544
13545       if (type
13546           && TREE_CODE (type) != TYPE_PACK_EXPANSION
13547           && declarator_can_be_parameter_pack (declarator)
13548           && (!declarator || !declarator->parameter_pack_p)
13549           && uses_parameter_packs (type))
13550         {
13551           /* Consume the `...'. */
13552           cp_lexer_consume_token (parser->lexer);
13553           maybe_warn_variadic_templates ();
13554           
13555           /* Build a pack expansion type */
13556           if (declarator)
13557             declarator->parameter_pack_p = true;
13558           else
13559             decl_specifiers.type = make_pack_expansion (type);
13560         }
13561     }
13562
13563   /* The restriction on defining new types applies only to the type
13564      of the parameter, not to the default argument.  */
13565   parser->type_definition_forbidden_message = saved_message;
13566
13567   /* If the next token is `=', then process a default argument.  */
13568   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13569     {
13570       /* Consume the `='.  */
13571       cp_lexer_consume_token (parser->lexer);
13572
13573       /* If we are defining a class, then the tokens that make up the
13574          default argument must be saved and processed later.  */
13575       if (!template_parm_p && at_class_scope_p ()
13576           && TYPE_BEING_DEFINED (current_class_type))
13577         {
13578           unsigned depth = 0;
13579           cp_token *first_token;
13580           cp_token *token;
13581
13582           /* Add tokens until we have processed the entire default
13583              argument.  We add the range [first_token, token).  */
13584           first_token = cp_lexer_peek_token (parser->lexer);
13585           while (true)
13586             {
13587               bool done = false;
13588
13589               /* Peek at the next token.  */
13590               token = cp_lexer_peek_token (parser->lexer);
13591               /* What we do depends on what token we have.  */
13592               switch (token->type)
13593                 {
13594                   /* In valid code, a default argument must be
13595                      immediately followed by a `,' `)', or `...'.  */
13596                 case CPP_COMMA:
13597                 case CPP_CLOSE_PAREN:
13598                 case CPP_ELLIPSIS:
13599                   /* If we run into a non-nested `;', `}', or `]',
13600                      then the code is invalid -- but the default
13601                      argument is certainly over.  */
13602                 case CPP_SEMICOLON:
13603                 case CPP_CLOSE_BRACE:
13604                 case CPP_CLOSE_SQUARE:
13605                   if (depth == 0)
13606                     done = true;
13607                   /* Update DEPTH, if necessary.  */
13608                   else if (token->type == CPP_CLOSE_PAREN
13609                            || token->type == CPP_CLOSE_BRACE
13610                            || token->type == CPP_CLOSE_SQUARE)
13611                     --depth;
13612                   break;
13613
13614                 case CPP_OPEN_PAREN:
13615                 case CPP_OPEN_SQUARE:
13616                 case CPP_OPEN_BRACE:
13617                   ++depth;
13618                   break;
13619
13620                 case CPP_RSHIFT:
13621                   if (cxx_dialect == cxx98)
13622                     break;
13623                   /* Fall through for C++0x, which treats the `>>'
13624                      operator like two `>' tokens in certain
13625                      cases.  */
13626
13627                 case CPP_GREATER:
13628                   /* If we see a non-nested `>', and `>' is not an
13629                      operator, then it marks the end of the default
13630                      argument.  */
13631                   if (!depth && !greater_than_is_operator_p)
13632                     done = true;
13633                   break;
13634
13635                   /* If we run out of tokens, issue an error message.  */
13636                 case CPP_EOF:
13637                 case CPP_PRAGMA_EOL:
13638                   error ("file ends in default argument");
13639                   done = true;
13640                   break;
13641
13642                 case CPP_NAME:
13643                 case CPP_SCOPE:
13644                   /* In these cases, we should look for template-ids.
13645                      For example, if the default argument is
13646                      `X<int, double>()', we need to do name lookup to
13647                      figure out whether or not `X' is a template; if
13648                      so, the `,' does not end the default argument.
13649
13650                      That is not yet done.  */
13651                   break;
13652
13653                 default:
13654                   break;
13655                 }
13656
13657               /* If we've reached the end, stop.  */
13658               if (done)
13659                 break;
13660
13661               /* Add the token to the token block.  */
13662               token = cp_lexer_consume_token (parser->lexer);
13663             }
13664
13665           /* Create a DEFAULT_ARG to represent the unparsed default
13666              argument.  */
13667           default_argument = make_node (DEFAULT_ARG);
13668           DEFARG_TOKENS (default_argument)
13669             = cp_token_cache_new (first_token, token);
13670           DEFARG_INSTANTIATIONS (default_argument) = NULL;
13671         }
13672       /* Outside of a class definition, we can just parse the
13673          assignment-expression.  */
13674       else
13675         default_argument 
13676           = cp_parser_default_argument (parser, template_parm_p);
13677
13678       if (!parser->default_arg_ok_p)
13679         {
13680           if (!flag_pedantic_errors)
13681             warning (0, "deprecated use of default argument for parameter of non-function");
13682           else
13683             {
13684               error ("default arguments are only permitted for function parameters");
13685               default_argument = NULL_TREE;
13686             }
13687         }
13688       else if ((declarator && declarator->parameter_pack_p)
13689                || (decl_specifiers.type
13690                    && PACK_EXPANSION_P (decl_specifiers.type)))
13691         {
13692           const char* kind = template_parm_p? "template " : "";
13693           
13694           /* Find the name of the parameter pack.  */     
13695           cp_declarator *id_declarator = declarator;
13696           while (id_declarator && id_declarator->kind != cdk_id)
13697             id_declarator = id_declarator->declarator;
13698           
13699           if (id_declarator && id_declarator->kind == cdk_id)
13700             error ("%sparameter pack %qD cannot have a default argument",
13701                    kind, id_declarator->u.id.unqualified_name);
13702           else
13703             error ("%sparameter pack cannot have a default argument",
13704                    kind);
13705           
13706           default_argument = NULL_TREE;
13707         }
13708     }
13709   else
13710     default_argument = NULL_TREE;
13711
13712   return make_parameter_declarator (&decl_specifiers,
13713                                     declarator,
13714                                     default_argument);
13715 }
13716
13717 /* Parse a default argument and return it.
13718
13719    TEMPLATE_PARM_P is true if this is a default argument for a
13720    non-type template parameter.  */
13721 static tree
13722 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
13723 {
13724   tree default_argument = NULL_TREE;
13725   bool saved_greater_than_is_operator_p;
13726   bool saved_local_variables_forbidden_p;
13727
13728   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13729      set correctly.  */
13730   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
13731   parser->greater_than_is_operator_p = !template_parm_p;
13732   /* Local variable names (and the `this' keyword) may not
13733      appear in a default argument.  */
13734   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13735   parser->local_variables_forbidden_p = true;
13736   /* The default argument expression may cause implicitly
13737      defined member functions to be synthesized, which will
13738      result in garbage collection.  We must treat this
13739      situation as if we were within the body of function so as
13740      to avoid collecting live data on the stack.  */
13741   ++function_depth;
13742   /* Parse the assignment-expression.  */
13743   if (template_parm_p)
13744     push_deferring_access_checks (dk_no_deferred);
13745   default_argument
13746     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13747   if (template_parm_p)
13748     pop_deferring_access_checks ();
13749   /* Restore saved state.  */
13750   --function_depth;
13751   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
13752   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13753
13754   return default_argument;
13755 }
13756
13757 /* Parse a function-body.
13758
13759    function-body:
13760      compound_statement  */
13761
13762 static void
13763 cp_parser_function_body (cp_parser *parser)
13764 {
13765   cp_parser_compound_statement (parser, NULL, false);
13766 }
13767
13768 /* Parse a ctor-initializer-opt followed by a function-body.  Return
13769    true if a ctor-initializer was present.  */
13770
13771 static bool
13772 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13773 {
13774   tree body;
13775   bool ctor_initializer_p;
13776
13777   /* Begin the function body.  */
13778   body = begin_function_body ();
13779   /* Parse the optional ctor-initializer.  */
13780   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13781   /* Parse the function-body.  */
13782   cp_parser_function_body (parser);
13783   /* Finish the function body.  */
13784   finish_function_body (body);
13785
13786   return ctor_initializer_p;
13787 }
13788
13789 /* Parse an initializer.
13790
13791    initializer:
13792      = initializer-clause
13793      ( expression-list )
13794
13795    Returns an expression representing the initializer.  If no
13796    initializer is present, NULL_TREE is returned.
13797
13798    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13799    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
13800    set to FALSE if there is no initializer present.  If there is an
13801    initializer, and it is not a constant-expression, *NON_CONSTANT_P
13802    is set to true; otherwise it is set to false.  */
13803
13804 static tree
13805 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13806                        bool* non_constant_p)
13807 {
13808   cp_token *token;
13809   tree init;
13810
13811   /* Peek at the next token.  */
13812   token = cp_lexer_peek_token (parser->lexer);
13813
13814   /* Let our caller know whether or not this initializer was
13815      parenthesized.  */
13816   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13817   /* Assume that the initializer is constant.  */
13818   *non_constant_p = false;
13819
13820   if (token->type == CPP_EQ)
13821     {
13822       /* Consume the `='.  */
13823       cp_lexer_consume_token (parser->lexer);
13824       /* Parse the initializer-clause.  */
13825       init = cp_parser_initializer_clause (parser, non_constant_p);
13826     }
13827   else if (token->type == CPP_OPEN_PAREN)
13828     init = cp_parser_parenthesized_expression_list (parser, false,
13829                                                     /*cast_p=*/false,
13830                                                     /*allow_expansion_p=*/true,
13831                                                     non_constant_p);
13832   else
13833     {
13834       /* Anything else is an error.  */
13835       cp_parser_error (parser, "expected initializer");
13836       init = error_mark_node;
13837     }
13838
13839   return init;
13840 }
13841
13842 /* Parse an initializer-clause.
13843
13844    initializer-clause:
13845      assignment-expression
13846      { initializer-list , [opt] }
13847      { }
13848
13849    Returns an expression representing the initializer.
13850
13851    If the `assignment-expression' production is used the value
13852    returned is simply a representation for the expression.
13853
13854    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
13855    the elements of the initializer-list (or NULL, if the last
13856    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
13857    NULL_TREE.  There is no way to detect whether or not the optional
13858    trailing `,' was provided.  NON_CONSTANT_P is as for
13859    cp_parser_initializer.  */
13860
13861 static tree
13862 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13863 {
13864   tree initializer;
13865
13866   /* Assume the expression is constant.  */
13867   *non_constant_p = false;
13868
13869   /* If it is not a `{', then we are looking at an
13870      assignment-expression.  */
13871   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13872     {
13873       initializer
13874         = cp_parser_constant_expression (parser,
13875                                         /*allow_non_constant_p=*/true,
13876                                         non_constant_p);
13877       if (!*non_constant_p)
13878         initializer = fold_non_dependent_expr (initializer);
13879     }
13880   else
13881     {
13882       /* Consume the `{' token.  */
13883       cp_lexer_consume_token (parser->lexer);
13884       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
13885       initializer = make_node (CONSTRUCTOR);
13886       /* If it's not a `}', then there is a non-trivial initializer.  */
13887       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13888         {
13889           /* Parse the initializer list.  */
13890           CONSTRUCTOR_ELTS (initializer)
13891             = cp_parser_initializer_list (parser, non_constant_p);
13892           /* A trailing `,' token is allowed.  */
13893           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13894             cp_lexer_consume_token (parser->lexer);
13895         }
13896       /* Now, there should be a trailing `}'.  */
13897       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13898     }
13899
13900   return initializer;
13901 }
13902
13903 /* Parse an initializer-list.
13904
13905    initializer-list:
13906      initializer-clause ... [opt]
13907      initializer-list , initializer-clause ... [opt]
13908
13909    GNU Extension:
13910
13911    initializer-list:
13912      identifier : initializer-clause
13913      initializer-list, identifier : initializer-clause
13914
13915    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
13916    for the initializer.  If the INDEX of the elt is non-NULL, it is the
13917    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
13918    as for cp_parser_initializer.  */
13919
13920 static VEC(constructor_elt,gc) *
13921 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13922 {
13923   VEC(constructor_elt,gc) *v = NULL;
13924
13925   /* Assume all of the expressions are constant.  */
13926   *non_constant_p = false;
13927
13928   /* Parse the rest of the list.  */
13929   while (true)
13930     {
13931       cp_token *token;
13932       tree identifier;
13933       tree initializer;
13934       bool clause_non_constant_p;
13935
13936       /* If the next token is an identifier and the following one is a
13937          colon, we are looking at the GNU designated-initializer
13938          syntax.  */
13939       if (cp_parser_allow_gnu_extensions_p (parser)
13940           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13941           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13942         {
13943           /* Warn the user that they are using an extension.  */
13944           if (pedantic)
13945             pedwarn ("ISO C++ does not allow designated initializers");
13946           /* Consume the identifier.  */
13947           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13948           /* Consume the `:'.  */
13949           cp_lexer_consume_token (parser->lexer);
13950         }
13951       else
13952         identifier = NULL_TREE;
13953
13954       /* Parse the initializer.  */
13955       initializer = cp_parser_initializer_clause (parser,
13956                                                   &clause_non_constant_p);
13957       /* If any clause is non-constant, so is the entire initializer.  */
13958       if (clause_non_constant_p)
13959         *non_constant_p = true;
13960
13961       /* If we have an ellipsis, this is an initializer pack
13962          expansion.  */
13963       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13964         {
13965           /* Consume the `...'.  */
13966           cp_lexer_consume_token (parser->lexer);
13967
13968           /* Turn the initializer into an initializer expansion.  */
13969           initializer = make_pack_expansion (initializer);
13970         }
13971
13972       /* Add it to the vector.  */
13973       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13974
13975       /* If the next token is not a comma, we have reached the end of
13976          the list.  */
13977       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13978         break;
13979
13980       /* Peek at the next token.  */
13981       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13982       /* If the next token is a `}', then we're still done.  An
13983          initializer-clause can have a trailing `,' after the
13984          initializer-list and before the closing `}'.  */
13985       if (token->type == CPP_CLOSE_BRACE)
13986         break;
13987
13988       /* Consume the `,' token.  */
13989       cp_lexer_consume_token (parser->lexer);
13990     }
13991
13992   return v;
13993 }
13994
13995 /* Classes [gram.class] */
13996
13997 /* Parse a class-name.
13998
13999    class-name:
14000      identifier
14001      template-id
14002
14003    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14004    to indicate that names looked up in dependent types should be
14005    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14006    keyword has been used to indicate that the name that appears next
14007    is a template.  TAG_TYPE indicates the explicit tag given before
14008    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14009    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14010    is the class being defined in a class-head.
14011
14012    Returns the TYPE_DECL representing the class.  */
14013
14014 static tree
14015 cp_parser_class_name (cp_parser *parser,
14016                       bool typename_keyword_p,
14017                       bool template_keyword_p,
14018                       enum tag_types tag_type,
14019                       bool check_dependency_p,
14020                       bool class_head_p,
14021                       bool is_declaration)
14022 {
14023   tree decl;
14024   tree scope;
14025   bool typename_p;
14026   cp_token *token;
14027
14028   /* All class-names start with an identifier.  */
14029   token = cp_lexer_peek_token (parser->lexer);
14030   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14031     {
14032       cp_parser_error (parser, "expected class-name");
14033       return error_mark_node;
14034     }
14035
14036   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14037      to a template-id, so we save it here.  */
14038   scope = parser->scope;
14039   if (scope == error_mark_node)
14040     return error_mark_node;
14041
14042   /* Any name names a type if we're following the `typename' keyword
14043      in a qualified name where the enclosing scope is type-dependent.  */
14044   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14045                 && dependent_type_p (scope));
14046   /* Handle the common case (an identifier, but not a template-id)
14047      efficiently.  */
14048   if (token->type == CPP_NAME
14049       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14050     {
14051       cp_token *identifier_token;
14052       tree identifier;
14053       bool ambiguous_p;
14054
14055       /* Look for the identifier.  */
14056       identifier_token = cp_lexer_peek_token (parser->lexer);
14057       ambiguous_p = identifier_token->ambiguous_p;
14058       identifier = cp_parser_identifier (parser);
14059       /* If the next token isn't an identifier, we are certainly not
14060          looking at a class-name.  */
14061       if (identifier == error_mark_node)
14062         decl = error_mark_node;
14063       /* If we know this is a type-name, there's no need to look it
14064          up.  */
14065       else if (typename_p)
14066         decl = identifier;
14067       else
14068         {
14069           tree ambiguous_decls;
14070           /* If we already know that this lookup is ambiguous, then
14071              we've already issued an error message; there's no reason
14072              to check again.  */
14073           if (ambiguous_p)
14074             {
14075               cp_parser_simulate_error (parser);
14076               return error_mark_node;
14077             }
14078           /* If the next token is a `::', then the name must be a type
14079              name.
14080
14081              [basic.lookup.qual]
14082
14083              During the lookup for a name preceding the :: scope
14084              resolution operator, object, function, and enumerator
14085              names are ignored.  */
14086           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14087             tag_type = typename_type;
14088           /* Look up the name.  */
14089           decl = cp_parser_lookup_name (parser, identifier,
14090                                         tag_type,
14091                                         /*is_template=*/false,
14092                                         /*is_namespace=*/false,
14093                                         check_dependency_p,
14094                                         &ambiguous_decls);
14095           if (ambiguous_decls)
14096             {
14097               error ("reference to %qD is ambiguous", identifier);
14098               print_candidates (ambiguous_decls);
14099               if (cp_parser_parsing_tentatively (parser))
14100                 {
14101                   identifier_token->ambiguous_p = true;
14102                   cp_parser_simulate_error (parser);
14103                 }
14104               return error_mark_node;
14105             }
14106         }
14107     }
14108   else
14109     {
14110       /* Try a template-id.  */
14111       decl = cp_parser_template_id (parser, template_keyword_p,
14112                                     check_dependency_p,
14113                                     is_declaration);
14114       if (decl == error_mark_node)
14115         return error_mark_node;
14116     }
14117
14118   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14119
14120   /* If this is a typename, create a TYPENAME_TYPE.  */
14121   if (typename_p && decl != error_mark_node)
14122     {
14123       decl = make_typename_type (scope, decl, typename_type,
14124                                  /*complain=*/tf_error);
14125       if (decl != error_mark_node)
14126         decl = TYPE_NAME (decl);
14127     }
14128
14129   /* Check to see that it is really the name of a class.  */
14130   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14131       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14132       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14133     /* Situations like this:
14134
14135          template <typename T> struct A {
14136            typename T::template X<int>::I i;
14137          };
14138
14139        are problematic.  Is `T::template X<int>' a class-name?  The
14140        standard does not seem to be definitive, but there is no other
14141        valid interpretation of the following `::'.  Therefore, those
14142        names are considered class-names.  */
14143     {
14144       decl = make_typename_type (scope, decl, tag_type, tf_error);
14145       if (decl != error_mark_node)
14146         decl = TYPE_NAME (decl);
14147     }
14148   else if (TREE_CODE (decl) != TYPE_DECL
14149            || TREE_TYPE (decl) == error_mark_node
14150            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14151     decl = error_mark_node;
14152
14153   if (decl == error_mark_node)
14154     cp_parser_error (parser, "expected class-name");
14155
14156   return decl;
14157 }
14158
14159 /* Parse a class-specifier.
14160
14161    class-specifier:
14162      class-head { member-specification [opt] }
14163
14164    Returns the TREE_TYPE representing the class.  */
14165
14166 static tree
14167 cp_parser_class_specifier (cp_parser* parser)
14168 {
14169   cp_token *token;
14170   tree type;
14171   tree attributes = NULL_TREE;
14172   int has_trailing_semicolon;
14173   bool nested_name_specifier_p;
14174   unsigned saved_num_template_parameter_lists;
14175   bool saved_in_function_body;
14176   tree old_scope = NULL_TREE;
14177   tree scope = NULL_TREE;
14178   tree bases;
14179
14180   push_deferring_access_checks (dk_no_deferred);
14181
14182   /* Parse the class-head.  */
14183   type = cp_parser_class_head (parser,
14184                                &nested_name_specifier_p,
14185                                &attributes,
14186                                &bases);
14187   /* If the class-head was a semantic disaster, skip the entire body
14188      of the class.  */
14189   if (!type)
14190     {
14191       cp_parser_skip_to_end_of_block_or_statement (parser);
14192       pop_deferring_access_checks ();
14193       return error_mark_node;
14194     }
14195
14196   /* Look for the `{'.  */
14197   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14198     {
14199       pop_deferring_access_checks ();
14200       return error_mark_node;
14201     }
14202
14203   /* Process the base classes. If they're invalid, skip the 
14204      entire class body.  */
14205   if (!xref_basetypes (type, bases))
14206     {
14207       /* Consuming the closing brace yields better error messages
14208          later on.  */
14209       if (cp_parser_skip_to_closing_brace (parser))
14210         cp_lexer_consume_token (parser->lexer);
14211       pop_deferring_access_checks ();
14212       return error_mark_node;
14213     }
14214
14215   /* Issue an error message if type-definitions are forbidden here.  */
14216   cp_parser_check_type_definition (parser);
14217   /* Remember that we are defining one more class.  */
14218   ++parser->num_classes_being_defined;
14219   /* Inside the class, surrounding template-parameter-lists do not
14220      apply.  */
14221   saved_num_template_parameter_lists
14222     = parser->num_template_parameter_lists;
14223   parser->num_template_parameter_lists = 0;
14224   /* We are not in a function body.  */
14225   saved_in_function_body = parser->in_function_body;
14226   parser->in_function_body = false;
14227
14228   /* Start the class.  */
14229   if (nested_name_specifier_p)
14230     {
14231       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14232       old_scope = push_inner_scope (scope);
14233     }
14234   type = begin_class_definition (type, attributes);
14235
14236   if (type == error_mark_node)
14237     /* If the type is erroneous, skip the entire body of the class.  */
14238     cp_parser_skip_to_closing_brace (parser);
14239   else
14240     /* Parse the member-specification.  */
14241     cp_parser_member_specification_opt (parser);
14242
14243   /* Look for the trailing `}'.  */
14244   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14245   /* We get better error messages by noticing a common problem: a
14246      missing trailing `;'.  */
14247   token = cp_lexer_peek_token (parser->lexer);
14248   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14249   /* Look for trailing attributes to apply to this class.  */
14250   if (cp_parser_allow_gnu_extensions_p (parser))
14251     attributes = cp_parser_attributes_opt (parser);
14252   if (type != error_mark_node)
14253     type = finish_struct (type, attributes);
14254   if (nested_name_specifier_p)
14255     pop_inner_scope (old_scope, scope);
14256   /* If this class is not itself within the scope of another class,
14257      then we need to parse the bodies of all of the queued function
14258      definitions.  Note that the queued functions defined in a class
14259      are not always processed immediately following the
14260      class-specifier for that class.  Consider:
14261
14262        struct A {
14263          struct B { void f() { sizeof (A); } };
14264        };
14265
14266      If `f' were processed before the processing of `A' were
14267      completed, there would be no way to compute the size of `A'.
14268      Note that the nesting we are interested in here is lexical --
14269      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14270      for:
14271
14272        struct A { struct B; };
14273        struct A::B { void f() { } };
14274
14275      there is no need to delay the parsing of `A::B::f'.  */
14276   if (--parser->num_classes_being_defined == 0)
14277     {
14278       tree queue_entry;
14279       tree fn;
14280       tree class_type = NULL_TREE;
14281       tree pushed_scope = NULL_TREE;
14282
14283       /* In a first pass, parse default arguments to the functions.
14284          Then, in a second pass, parse the bodies of the functions.
14285          This two-phased approach handles cases like:
14286
14287             struct S {
14288               void f() { g(); }
14289               void g(int i = 3);
14290             };
14291
14292          */
14293       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14294              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14295            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14296            TREE_PURPOSE (parser->unparsed_functions_queues)
14297              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14298         {
14299           fn = TREE_VALUE (queue_entry);
14300           /* If there are default arguments that have not yet been processed,
14301              take care of them now.  */
14302           if (class_type != TREE_PURPOSE (queue_entry))
14303             {
14304               if (pushed_scope)
14305                 pop_scope (pushed_scope);
14306               class_type = TREE_PURPOSE (queue_entry);
14307               pushed_scope = push_scope (class_type);
14308             }
14309           /* Make sure that any template parameters are in scope.  */
14310           maybe_begin_member_template_processing (fn);
14311           /* Parse the default argument expressions.  */
14312           cp_parser_late_parsing_default_args (parser, fn);
14313           /* Remove any template parameters from the symbol table.  */
14314           maybe_end_member_template_processing ();
14315         }
14316       if (pushed_scope)
14317         pop_scope (pushed_scope);
14318       /* Now parse the body of the functions.  */
14319       for (TREE_VALUE (parser->unparsed_functions_queues)
14320              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14321            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14322            TREE_VALUE (parser->unparsed_functions_queues)
14323              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14324         {
14325           /* Figure out which function we need to process.  */
14326           fn = TREE_VALUE (queue_entry);
14327           /* Parse the function.  */
14328           cp_parser_late_parsing_for_member (parser, fn);
14329         }
14330     }
14331
14332   /* Put back any saved access checks.  */
14333   pop_deferring_access_checks ();
14334
14335   /* Restore saved state.  */
14336   parser->in_function_body = saved_in_function_body;
14337   parser->num_template_parameter_lists
14338     = saved_num_template_parameter_lists;
14339
14340   return type;
14341 }
14342
14343 /* Parse a class-head.
14344
14345    class-head:
14346      class-key identifier [opt] base-clause [opt]
14347      class-key nested-name-specifier identifier base-clause [opt]
14348      class-key nested-name-specifier [opt] template-id
14349        base-clause [opt]
14350
14351    GNU Extensions:
14352      class-key attributes identifier [opt] base-clause [opt]
14353      class-key attributes nested-name-specifier identifier base-clause [opt]
14354      class-key attributes nested-name-specifier [opt] template-id
14355        base-clause [opt]
14356
14357    Upon return BASES is initialized to the list of base classes (or
14358    NULL, if there are none) in the same form returned by
14359    cp_parser_base_clause.
14360
14361    Returns the TYPE of the indicated class.  Sets
14362    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14363    involving a nested-name-specifier was used, and FALSE otherwise.
14364
14365    Returns error_mark_node if this is not a class-head.
14366
14367    Returns NULL_TREE if the class-head is syntactically valid, but
14368    semantically invalid in a way that means we should skip the entire
14369    body of the class.  */
14370
14371 static tree
14372 cp_parser_class_head (cp_parser* parser,
14373                       bool* nested_name_specifier_p,
14374                       tree *attributes_p,
14375                       tree *bases)
14376 {
14377   tree nested_name_specifier;
14378   enum tag_types class_key;
14379   tree id = NULL_TREE;
14380   tree type = NULL_TREE;
14381   tree attributes;
14382   bool template_id_p = false;
14383   bool qualified_p = false;
14384   bool invalid_nested_name_p = false;
14385   bool invalid_explicit_specialization_p = false;
14386   tree pushed_scope = NULL_TREE;
14387   unsigned num_templates;
14388
14389   /* Assume no nested-name-specifier will be present.  */
14390   *nested_name_specifier_p = false;
14391   /* Assume no template parameter lists will be used in defining the
14392      type.  */
14393   num_templates = 0;
14394
14395   *bases = NULL_TREE;
14396
14397   /* Look for the class-key.  */
14398   class_key = cp_parser_class_key (parser);
14399   if (class_key == none_type)
14400     return error_mark_node;
14401
14402   /* Parse the attributes.  */
14403   attributes = cp_parser_attributes_opt (parser);
14404
14405   /* If the next token is `::', that is invalid -- but sometimes
14406      people do try to write:
14407
14408        struct ::S {};
14409
14410      Handle this gracefully by accepting the extra qualifier, and then
14411      issuing an error about it later if this really is a
14412      class-head.  If it turns out just to be an elaborated type
14413      specifier, remain silent.  */
14414   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14415     qualified_p = true;
14416
14417   push_deferring_access_checks (dk_no_check);
14418
14419   /* Determine the name of the class.  Begin by looking for an
14420      optional nested-name-specifier.  */
14421   nested_name_specifier
14422     = cp_parser_nested_name_specifier_opt (parser,
14423                                            /*typename_keyword_p=*/false,
14424                                            /*check_dependency_p=*/false,
14425                                            /*type_p=*/false,
14426                                            /*is_declaration=*/false);
14427   /* If there was a nested-name-specifier, then there *must* be an
14428      identifier.  */
14429   if (nested_name_specifier)
14430     {
14431       /* Although the grammar says `identifier', it really means
14432          `class-name' or `template-name'.  You are only allowed to
14433          define a class that has already been declared with this
14434          syntax.
14435
14436          The proposed resolution for Core Issue 180 says that wherever
14437          you see `class T::X' you should treat `X' as a type-name.
14438
14439          It is OK to define an inaccessible class; for example:
14440
14441            class A { class B; };
14442            class A::B {};
14443
14444          We do not know if we will see a class-name, or a
14445          template-name.  We look for a class-name first, in case the
14446          class-name is a template-id; if we looked for the
14447          template-name first we would stop after the template-name.  */
14448       cp_parser_parse_tentatively (parser);
14449       type = cp_parser_class_name (parser,
14450                                    /*typename_keyword_p=*/false,
14451                                    /*template_keyword_p=*/false,
14452                                    class_type,
14453                                    /*check_dependency_p=*/false,
14454                                    /*class_head_p=*/true,
14455                                    /*is_declaration=*/false);
14456       /* If that didn't work, ignore the nested-name-specifier.  */
14457       if (!cp_parser_parse_definitely (parser))
14458         {
14459           invalid_nested_name_p = true;
14460           id = cp_parser_identifier (parser);
14461           if (id == error_mark_node)
14462             id = NULL_TREE;
14463         }
14464       /* If we could not find a corresponding TYPE, treat this
14465          declaration like an unqualified declaration.  */
14466       if (type == error_mark_node)
14467         nested_name_specifier = NULL_TREE;
14468       /* Otherwise, count the number of templates used in TYPE and its
14469          containing scopes.  */
14470       else
14471         {
14472           tree scope;
14473
14474           for (scope = TREE_TYPE (type);
14475                scope && TREE_CODE (scope) != NAMESPACE_DECL;
14476                scope = (TYPE_P (scope)
14477                         ? TYPE_CONTEXT (scope)
14478                         : DECL_CONTEXT (scope)))
14479             if (TYPE_P (scope)
14480                 && CLASS_TYPE_P (scope)
14481                 && CLASSTYPE_TEMPLATE_INFO (scope)
14482                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14483                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14484               ++num_templates;
14485         }
14486     }
14487   /* Otherwise, the identifier is optional.  */
14488   else
14489     {
14490       /* We don't know whether what comes next is a template-id,
14491          an identifier, or nothing at all.  */
14492       cp_parser_parse_tentatively (parser);
14493       /* Check for a template-id.  */
14494       id = cp_parser_template_id (parser,
14495                                   /*template_keyword_p=*/false,
14496                                   /*check_dependency_p=*/true,
14497                                   /*is_declaration=*/true);
14498       /* If that didn't work, it could still be an identifier.  */
14499       if (!cp_parser_parse_definitely (parser))
14500         {
14501           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14502             id = cp_parser_identifier (parser);
14503           else
14504             id = NULL_TREE;
14505         }
14506       else
14507         {
14508           template_id_p = true;
14509           ++num_templates;
14510         }
14511     }
14512
14513   pop_deferring_access_checks ();
14514
14515   if (id)
14516     cp_parser_check_for_invalid_template_id (parser, id);
14517
14518   /* If it's not a `:' or a `{' then we can't really be looking at a
14519      class-head, since a class-head only appears as part of a
14520      class-specifier.  We have to detect this situation before calling
14521      xref_tag, since that has irreversible side-effects.  */
14522   if (!cp_parser_next_token_starts_class_definition_p (parser))
14523     {
14524       cp_parser_error (parser, "expected %<{%> or %<:%>");
14525       return error_mark_node;
14526     }
14527
14528   /* At this point, we're going ahead with the class-specifier, even
14529      if some other problem occurs.  */
14530   cp_parser_commit_to_tentative_parse (parser);
14531   /* Issue the error about the overly-qualified name now.  */
14532   if (qualified_p)
14533     cp_parser_error (parser,
14534                      "global qualification of class name is invalid");
14535   else if (invalid_nested_name_p)
14536     cp_parser_error (parser,
14537                      "qualified name does not name a class");
14538   else if (nested_name_specifier)
14539     {
14540       tree scope;
14541
14542       /* Reject typedef-names in class heads.  */
14543       if (!DECL_IMPLICIT_TYPEDEF_P (type))
14544         {
14545           error ("invalid class name in declaration of %qD", type);
14546           type = NULL_TREE;
14547           goto done;
14548         }
14549
14550       /* Figure out in what scope the declaration is being placed.  */
14551       scope = current_scope ();
14552       /* If that scope does not contain the scope in which the
14553          class was originally declared, the program is invalid.  */
14554       if (scope && !is_ancestor (scope, nested_name_specifier))
14555         {
14556           if (at_namespace_scope_p ())
14557             error ("declaration of %qD in namespace %qD which does not "
14558                    "enclose %qD", type, scope, nested_name_specifier);
14559           else
14560             error ("declaration of %qD in %qD which does not enclose %qD",
14561                    type, scope, nested_name_specifier);
14562           type = NULL_TREE;
14563           goto done;
14564         }
14565       /* [dcl.meaning]
14566
14567          A declarator-id shall not be qualified exception of the
14568          definition of a ... nested class outside of its class
14569          ... [or] a the definition or explicit instantiation of a
14570          class member of a namespace outside of its namespace.  */
14571       if (scope == nested_name_specifier)
14572         {
14573           pedwarn ("extra qualification ignored");
14574           nested_name_specifier = NULL_TREE;
14575           num_templates = 0;
14576         }
14577     }
14578   /* An explicit-specialization must be preceded by "template <>".  If
14579      it is not, try to recover gracefully.  */
14580   if (at_namespace_scope_p ()
14581       && parser->num_template_parameter_lists == 0
14582       && template_id_p)
14583     {
14584       error ("an explicit specialization must be preceded by %<template <>%>");
14585       invalid_explicit_specialization_p = true;
14586       /* Take the same action that would have been taken by
14587          cp_parser_explicit_specialization.  */
14588       ++parser->num_template_parameter_lists;
14589       begin_specialization ();
14590     }
14591   /* There must be no "return" statements between this point and the
14592      end of this function; set "type "to the correct return value and
14593      use "goto done;" to return.  */
14594   /* Make sure that the right number of template parameters were
14595      present.  */
14596   if (!cp_parser_check_template_parameters (parser, num_templates))
14597     {
14598       /* If something went wrong, there is no point in even trying to
14599          process the class-definition.  */
14600       type = NULL_TREE;
14601       goto done;
14602     }
14603
14604   /* Look up the type.  */
14605   if (template_id_p)
14606     {
14607       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14608           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14609               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14610         {
14611           error ("function template %qD redeclared as a class template", id);
14612           type = error_mark_node;
14613         }
14614       else
14615         {
14616           type = TREE_TYPE (id);
14617           type = maybe_process_partial_specialization (type);
14618         }
14619       if (nested_name_specifier)
14620         pushed_scope = push_scope (nested_name_specifier);
14621     }
14622   else if (nested_name_specifier)
14623     {
14624       tree class_type;
14625
14626       /* Given:
14627
14628             template <typename T> struct S { struct T };
14629             template <typename T> struct S<T>::T { };
14630
14631          we will get a TYPENAME_TYPE when processing the definition of
14632          `S::T'.  We need to resolve it to the actual type before we
14633          try to define it.  */
14634       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14635         {
14636           class_type = resolve_typename_type (TREE_TYPE (type),
14637                                               /*only_current_p=*/false);
14638           if (TREE_CODE (class_type) != TYPENAME_TYPE)
14639             type = TYPE_NAME (class_type);
14640           else
14641             {
14642               cp_parser_error (parser, "could not resolve typename type");
14643               type = error_mark_node;
14644             }
14645         }
14646
14647       maybe_process_partial_specialization (TREE_TYPE (type));
14648       class_type = current_class_type;
14649       /* Enter the scope indicated by the nested-name-specifier.  */
14650       pushed_scope = push_scope (nested_name_specifier);
14651       /* Get the canonical version of this type.  */
14652       type = TYPE_MAIN_DECL (TREE_TYPE (type));
14653       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14654           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14655         {
14656           type = push_template_decl (type);
14657           if (type == error_mark_node)
14658             {
14659               type = NULL_TREE;
14660               goto done;
14661             }
14662         }
14663
14664       type = TREE_TYPE (type);
14665       *nested_name_specifier_p = true;
14666     }
14667   else      /* The name is not a nested name.  */
14668     {
14669       /* If the class was unnamed, create a dummy name.  */
14670       if (!id)
14671         id = make_anon_name ();
14672       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14673                        parser->num_template_parameter_lists);
14674     }
14675
14676   /* Indicate whether this class was declared as a `class' or as a
14677      `struct'.  */
14678   if (TREE_CODE (type) == RECORD_TYPE)
14679     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14680   cp_parser_check_class_key (class_key, type);
14681
14682   /* If this type was already complete, and we see another definition,
14683      that's an error.  */
14684   if (type != error_mark_node && COMPLETE_TYPE_P (type))
14685     {
14686       error ("redefinition of %q#T", type);
14687       error ("previous definition of %q+#T", type);
14688       type = NULL_TREE;
14689       goto done;
14690     }
14691   else if (type == error_mark_node)
14692     type = NULL_TREE;
14693
14694   /* We will have entered the scope containing the class; the names of
14695      base classes should be looked up in that context.  For example:
14696
14697        struct A { struct B {}; struct C; };
14698        struct A::C : B {};
14699
14700      is valid.  */
14701
14702   /* Get the list of base-classes, if there is one.  */
14703   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14704     *bases = cp_parser_base_clause (parser);
14705
14706  done:
14707   /* Leave the scope given by the nested-name-specifier.  We will
14708      enter the class scope itself while processing the members.  */
14709   if (pushed_scope)
14710     pop_scope (pushed_scope);
14711
14712   if (invalid_explicit_specialization_p)
14713     {
14714       end_specialization ();
14715       --parser->num_template_parameter_lists;
14716     }
14717   *attributes_p = attributes;
14718   return type;
14719 }
14720
14721 /* Parse a class-key.
14722
14723    class-key:
14724      class
14725      struct
14726      union
14727
14728    Returns the kind of class-key specified, or none_type to indicate
14729    error.  */
14730
14731 static enum tag_types
14732 cp_parser_class_key (cp_parser* parser)
14733 {
14734   cp_token *token;
14735   enum tag_types tag_type;
14736
14737   /* Look for the class-key.  */
14738   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14739   if (!token)
14740     return none_type;
14741
14742   /* Check to see if the TOKEN is a class-key.  */
14743   tag_type = cp_parser_token_is_class_key (token);
14744   if (!tag_type)
14745     cp_parser_error (parser, "expected class-key");
14746   return tag_type;
14747 }
14748
14749 /* Parse an (optional) member-specification.
14750
14751    member-specification:
14752      member-declaration member-specification [opt]
14753      access-specifier : member-specification [opt]  */
14754
14755 static void
14756 cp_parser_member_specification_opt (cp_parser* parser)
14757 {
14758   while (true)
14759     {
14760       cp_token *token;
14761       enum rid keyword;
14762
14763       /* Peek at the next token.  */
14764       token = cp_lexer_peek_token (parser->lexer);
14765       /* If it's a `}', or EOF then we've seen all the members.  */
14766       if (token->type == CPP_CLOSE_BRACE
14767           || token->type == CPP_EOF
14768           || token->type == CPP_PRAGMA_EOL)
14769         break;
14770
14771       /* See if this token is a keyword.  */
14772       keyword = token->keyword;
14773       switch (keyword)
14774         {
14775         case RID_PUBLIC:
14776         case RID_PROTECTED:
14777         case RID_PRIVATE:
14778           /* Consume the access-specifier.  */
14779           cp_lexer_consume_token (parser->lexer);
14780           /* Remember which access-specifier is active.  */
14781           current_access_specifier = token->u.value;
14782           /* Look for the `:'.  */
14783           cp_parser_require (parser, CPP_COLON, "`:'");
14784           break;
14785
14786         default:
14787           /* Accept #pragmas at class scope.  */
14788           if (token->type == CPP_PRAGMA)
14789             {
14790               cp_parser_pragma (parser, pragma_external);
14791               break;
14792             }
14793
14794           /* Otherwise, the next construction must be a
14795              member-declaration.  */
14796           cp_parser_member_declaration (parser);
14797         }
14798     }
14799 }
14800
14801 /* Parse a member-declaration.
14802
14803    member-declaration:
14804      decl-specifier-seq [opt] member-declarator-list [opt] ;
14805      function-definition ; [opt]
14806      :: [opt] nested-name-specifier template [opt] unqualified-id ;
14807      using-declaration
14808      template-declaration
14809
14810    member-declarator-list:
14811      member-declarator
14812      member-declarator-list , member-declarator
14813
14814    member-declarator:
14815      declarator pure-specifier [opt]
14816      declarator constant-initializer [opt]
14817      identifier [opt] : constant-expression
14818
14819    GNU Extensions:
14820
14821    member-declaration:
14822      __extension__ member-declaration
14823
14824    member-declarator:
14825      declarator attributes [opt] pure-specifier [opt]
14826      declarator attributes [opt] constant-initializer [opt]
14827      identifier [opt] attributes [opt] : constant-expression  
14828
14829    C++0x Extensions:
14830
14831    member-declaration:
14832      static_assert-declaration  */
14833
14834 static void
14835 cp_parser_member_declaration (cp_parser* parser)
14836 {
14837   cp_decl_specifier_seq decl_specifiers;
14838   tree prefix_attributes;
14839   tree decl;
14840   int declares_class_or_enum;
14841   bool friend_p;
14842   cp_token *token;
14843   int saved_pedantic;
14844
14845   /* Check for the `__extension__' keyword.  */
14846   if (cp_parser_extension_opt (parser, &saved_pedantic))
14847     {
14848       /* Recurse.  */
14849       cp_parser_member_declaration (parser);
14850       /* Restore the old value of the PEDANTIC flag.  */
14851       pedantic = saved_pedantic;
14852
14853       return;
14854     }
14855
14856   /* Check for a template-declaration.  */
14857   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14858     {
14859       /* An explicit specialization here is an error condition, and we
14860          expect the specialization handler to detect and report this.  */
14861       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14862           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14863         cp_parser_explicit_specialization (parser);
14864       else
14865         cp_parser_template_declaration (parser, /*member_p=*/true);
14866
14867       return;
14868     }
14869
14870   /* Check for a using-declaration.  */
14871   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14872     {
14873       /* Parse the using-declaration.  */
14874       cp_parser_using_declaration (parser,
14875                                    /*access_declaration_p=*/false);
14876       return;
14877     }
14878
14879   /* Check for @defs.  */
14880   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14881     {
14882       tree ivar, member;
14883       tree ivar_chains = cp_parser_objc_defs_expression (parser);
14884       ivar = ivar_chains;
14885       while (ivar)
14886         {
14887           member = ivar;
14888           ivar = TREE_CHAIN (member);
14889           TREE_CHAIN (member) = NULL_TREE;
14890           finish_member_declaration (member);
14891         }
14892       return;
14893     }
14894
14895   /* If the next token is `static_assert' we have a static assertion.  */
14896   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14897     {
14898       cp_parser_static_assert (parser, /*member_p=*/true);
14899       return;
14900     }
14901
14902   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14903     return;
14904
14905   /* Parse the decl-specifier-seq.  */
14906   cp_parser_decl_specifier_seq (parser,
14907                                 CP_PARSER_FLAGS_OPTIONAL,
14908                                 &decl_specifiers,
14909                                 &declares_class_or_enum);
14910   prefix_attributes = decl_specifiers.attributes;
14911   decl_specifiers.attributes = NULL_TREE;
14912   /* Check for an invalid type-name.  */
14913   if (!decl_specifiers.type
14914       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14915     return;
14916   /* If there is no declarator, then the decl-specifier-seq should
14917      specify a type.  */
14918   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14919     {
14920       /* If there was no decl-specifier-seq, and the next token is a
14921          `;', then we have something like:
14922
14923            struct S { ; };
14924
14925          [class.mem]
14926
14927          Each member-declaration shall declare at least one member
14928          name of the class.  */
14929       if (!decl_specifiers.any_specifiers_p)
14930         {
14931           cp_token *token = cp_lexer_peek_token (parser->lexer);
14932           if (pedantic && !token->in_system_header)
14933             pedwarn ("%Hextra %<;%>", &token->location);
14934         }
14935       else
14936         {
14937           tree type;
14938
14939           /* See if this declaration is a friend.  */
14940           friend_p = cp_parser_friend_p (&decl_specifiers);
14941           /* If there were decl-specifiers, check to see if there was
14942              a class-declaration.  */
14943           type = check_tag_decl (&decl_specifiers);
14944           /* Nested classes have already been added to the class, but
14945              a `friend' needs to be explicitly registered.  */
14946           if (friend_p)
14947             {
14948               /* If the `friend' keyword was present, the friend must
14949                  be introduced with a class-key.  */
14950                if (!declares_class_or_enum)
14951                  error ("a class-key must be used when declaring a friend");
14952                /* In this case:
14953
14954                     template <typename T> struct A {
14955                       friend struct A<T>::B;
14956                     };
14957
14958                   A<T>::B will be represented by a TYPENAME_TYPE, and
14959                   therefore not recognized by check_tag_decl.  */
14960                if (!type
14961                    && decl_specifiers.type
14962                    && TYPE_P (decl_specifiers.type))
14963                  type = decl_specifiers.type;
14964                if (!type || !TYPE_P (type))
14965                  error ("friend declaration does not name a class or "
14966                         "function");
14967                else
14968                  make_friend_class (current_class_type, type,
14969                                     /*complain=*/true);
14970             }
14971           /* If there is no TYPE, an error message will already have
14972              been issued.  */
14973           else if (!type || type == error_mark_node)
14974             ;
14975           /* An anonymous aggregate has to be handled specially; such
14976              a declaration really declares a data member (with a
14977              particular type), as opposed to a nested class.  */
14978           else if (ANON_AGGR_TYPE_P (type))
14979             {
14980               /* Remove constructors and such from TYPE, now that we
14981                  know it is an anonymous aggregate.  */
14982               fixup_anonymous_aggr (type);
14983               /* And make the corresponding data member.  */
14984               decl = build_decl (FIELD_DECL, NULL_TREE, type);
14985               /* Add it to the class.  */
14986               finish_member_declaration (decl);
14987             }
14988           else
14989             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14990         }
14991     }
14992   else
14993     {
14994       /* See if these declarations will be friends.  */
14995       friend_p = cp_parser_friend_p (&decl_specifiers);
14996
14997       /* Keep going until we hit the `;' at the end of the
14998          declaration.  */
14999       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15000         {
15001           tree attributes = NULL_TREE;
15002           tree first_attribute;
15003
15004           /* Peek at the next token.  */
15005           token = cp_lexer_peek_token (parser->lexer);
15006
15007           /* Check for a bitfield declaration.  */
15008           if (token->type == CPP_COLON
15009               || (token->type == CPP_NAME
15010                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15011                   == CPP_COLON))
15012             {
15013               tree identifier;
15014               tree width;
15015
15016               /* Get the name of the bitfield.  Note that we cannot just
15017                  check TOKEN here because it may have been invalidated by
15018                  the call to cp_lexer_peek_nth_token above.  */
15019               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15020                 identifier = cp_parser_identifier (parser);
15021               else
15022                 identifier = NULL_TREE;
15023
15024               /* Consume the `:' token.  */
15025               cp_lexer_consume_token (parser->lexer);
15026               /* Get the width of the bitfield.  */
15027               width
15028                 = cp_parser_constant_expression (parser,
15029                                                  /*allow_non_constant=*/false,
15030                                                  NULL);
15031
15032               /* Look for attributes that apply to the bitfield.  */
15033               attributes = cp_parser_attributes_opt (parser);
15034               /* Remember which attributes are prefix attributes and
15035                  which are not.  */
15036               first_attribute = attributes;
15037               /* Combine the attributes.  */
15038               attributes = chainon (prefix_attributes, attributes);
15039
15040               /* Create the bitfield declaration.  */
15041               decl = grokbitfield (identifier
15042                                    ? make_id_declarator (NULL_TREE,
15043                                                          identifier,
15044                                                          sfk_none)
15045                                    : NULL,
15046                                    &decl_specifiers,
15047                                    width);
15048               /* Apply the attributes.  */
15049               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
15050             }
15051           else
15052             {
15053               cp_declarator *declarator;
15054               tree initializer;
15055               tree asm_specification;
15056               int ctor_dtor_or_conv_p;
15057
15058               /* Parse the declarator.  */
15059               declarator
15060                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15061                                         &ctor_dtor_or_conv_p,
15062                                         /*parenthesized_p=*/NULL,
15063                                         /*member_p=*/true);
15064
15065               /* If something went wrong parsing the declarator, make sure
15066                  that we at least consume some tokens.  */
15067               if (declarator == cp_error_declarator)
15068                 {
15069                   /* Skip to the end of the statement.  */
15070                   cp_parser_skip_to_end_of_statement (parser);
15071                   /* If the next token is not a semicolon, that is
15072                      probably because we just skipped over the body of
15073                      a function.  So, we consume a semicolon if
15074                      present, but do not issue an error message if it
15075                      is not present.  */
15076                   if (cp_lexer_next_token_is (parser->lexer,
15077                                               CPP_SEMICOLON))
15078                     cp_lexer_consume_token (parser->lexer);
15079                   return;
15080                 }
15081
15082               if (declares_class_or_enum & 2)
15083                 cp_parser_check_for_definition_in_return_type
15084                   (declarator, decl_specifiers.type);
15085
15086               /* Look for an asm-specification.  */
15087               asm_specification = cp_parser_asm_specification_opt (parser);
15088               /* Look for attributes that apply to the declaration.  */
15089               attributes = cp_parser_attributes_opt (parser);
15090               /* Remember which attributes are prefix attributes and
15091                  which are not.  */
15092               first_attribute = attributes;
15093               /* Combine the attributes.  */
15094               attributes = chainon (prefix_attributes, attributes);
15095
15096               /* If it's an `=', then we have a constant-initializer or a
15097                  pure-specifier.  It is not correct to parse the
15098                  initializer before registering the member declaration
15099                  since the member declaration should be in scope while
15100                  its initializer is processed.  However, the rest of the
15101                  front end does not yet provide an interface that allows
15102                  us to handle this correctly.  */
15103               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15104                 {
15105                   /* In [class.mem]:
15106
15107                      A pure-specifier shall be used only in the declaration of
15108                      a virtual function.
15109
15110                      A member-declarator can contain a constant-initializer
15111                      only if it declares a static member of integral or
15112                      enumeration type.
15113
15114                      Therefore, if the DECLARATOR is for a function, we look
15115                      for a pure-specifier; otherwise, we look for a
15116                      constant-initializer.  When we call `grokfield', it will
15117                      perform more stringent semantics checks.  */
15118                   if (function_declarator_p (declarator))
15119                     initializer = cp_parser_pure_specifier (parser);
15120                   else
15121                     /* Parse the initializer.  */
15122                     initializer = cp_parser_constant_initializer (parser);
15123                 }
15124               /* Otherwise, there is no initializer.  */
15125               else
15126                 initializer = NULL_TREE;
15127
15128               /* See if we are probably looking at a function
15129                  definition.  We are certainly not looking at a
15130                  member-declarator.  Calling `grokfield' has
15131                  side-effects, so we must not do it unless we are sure
15132                  that we are looking at a member-declarator.  */
15133               if (cp_parser_token_starts_function_definition_p
15134                   (cp_lexer_peek_token (parser->lexer)))
15135                 {
15136                   /* The grammar does not allow a pure-specifier to be
15137                      used when a member function is defined.  (It is
15138                      possible that this fact is an oversight in the
15139                      standard, since a pure function may be defined
15140                      outside of the class-specifier.  */
15141                   if (initializer)
15142                     error ("pure-specifier on function-definition");
15143                   decl = cp_parser_save_member_function_body (parser,
15144                                                               &decl_specifiers,
15145                                                               declarator,
15146                                                               attributes);
15147                   /* If the member was not a friend, declare it here.  */
15148                   if (!friend_p)
15149                     finish_member_declaration (decl);
15150                   /* Peek at the next token.  */
15151                   token = cp_lexer_peek_token (parser->lexer);
15152                   /* If the next token is a semicolon, consume it.  */
15153                   if (token->type == CPP_SEMICOLON)
15154                     cp_lexer_consume_token (parser->lexer);
15155                   return;
15156                 }
15157               else
15158                 /* Create the declaration.  */
15159                 decl = grokfield (declarator, &decl_specifiers,
15160                                   initializer, /*init_const_expr_p=*/true,
15161                                   asm_specification,
15162                                   attributes);
15163             }
15164
15165           /* Reset PREFIX_ATTRIBUTES.  */
15166           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15167             attributes = TREE_CHAIN (attributes);
15168           if (attributes)
15169             TREE_CHAIN (attributes) = NULL_TREE;
15170
15171           /* If there is any qualification still in effect, clear it
15172              now; we will be starting fresh with the next declarator.  */
15173           parser->scope = NULL_TREE;
15174           parser->qualifying_scope = NULL_TREE;
15175           parser->object_scope = NULL_TREE;
15176           /* If it's a `,', then there are more declarators.  */
15177           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15178             cp_lexer_consume_token (parser->lexer);
15179           /* If the next token isn't a `;', then we have a parse error.  */
15180           else if (cp_lexer_next_token_is_not (parser->lexer,
15181                                                CPP_SEMICOLON))
15182             {
15183               cp_parser_error (parser, "expected %<;%>");
15184               /* Skip tokens until we find a `;'.  */
15185               cp_parser_skip_to_end_of_statement (parser);
15186
15187               break;
15188             }
15189
15190           if (decl)
15191             {
15192               /* Add DECL to the list of members.  */
15193               if (!friend_p)
15194                 finish_member_declaration (decl);
15195
15196               if (TREE_CODE (decl) == FUNCTION_DECL)
15197                 cp_parser_save_default_args (parser, decl);
15198             }
15199         }
15200     }
15201
15202   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15203 }
15204
15205 /* Parse a pure-specifier.
15206
15207    pure-specifier:
15208      = 0
15209
15210    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15211    Otherwise, ERROR_MARK_NODE is returned.  */
15212
15213 static tree
15214 cp_parser_pure_specifier (cp_parser* parser)
15215 {
15216   cp_token *token;
15217
15218   /* Look for the `=' token.  */
15219   if (!cp_parser_require (parser, CPP_EQ, "`='"))
15220     return error_mark_node;
15221   /* Look for the `0' token.  */
15222   token = cp_lexer_consume_token (parser->lexer);
15223   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15224   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15225     {
15226       cp_parser_error (parser,
15227                        "invalid pure specifier (only `= 0' is allowed)");
15228       cp_parser_skip_to_end_of_statement (parser);
15229       return error_mark_node;
15230     }
15231   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15232     {
15233       error ("templates may not be %<virtual%>");
15234       return error_mark_node;
15235     }
15236
15237   return integer_zero_node;
15238 }
15239
15240 /* Parse a constant-initializer.
15241
15242    constant-initializer:
15243      = constant-expression
15244
15245    Returns a representation of the constant-expression.  */
15246
15247 static tree
15248 cp_parser_constant_initializer (cp_parser* parser)
15249 {
15250   /* Look for the `=' token.  */
15251   if (!cp_parser_require (parser, CPP_EQ, "`='"))
15252     return error_mark_node;
15253
15254   /* It is invalid to write:
15255
15256        struct S { static const int i = { 7 }; };
15257
15258      */
15259   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15260     {
15261       cp_parser_error (parser,
15262                        "a brace-enclosed initializer is not allowed here");
15263       /* Consume the opening brace.  */
15264       cp_lexer_consume_token (parser->lexer);
15265       /* Skip the initializer.  */
15266       cp_parser_skip_to_closing_brace (parser);
15267       /* Look for the trailing `}'.  */
15268       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15269
15270       return error_mark_node;
15271     }
15272
15273   return cp_parser_constant_expression (parser,
15274                                         /*allow_non_constant=*/false,
15275                                         NULL);
15276 }
15277
15278 /* Derived classes [gram.class.derived] */
15279
15280 /* Parse a base-clause.
15281
15282    base-clause:
15283      : base-specifier-list
15284
15285    base-specifier-list:
15286      base-specifier ... [opt]
15287      base-specifier-list , base-specifier ... [opt]
15288
15289    Returns a TREE_LIST representing the base-classes, in the order in
15290    which they were declared.  The representation of each node is as
15291    described by cp_parser_base_specifier.
15292
15293    In the case that no bases are specified, this function will return
15294    NULL_TREE, not ERROR_MARK_NODE.  */
15295
15296 static tree
15297 cp_parser_base_clause (cp_parser* parser)
15298 {
15299   tree bases = NULL_TREE;
15300
15301   /* Look for the `:' that begins the list.  */
15302   cp_parser_require (parser, CPP_COLON, "`:'");
15303
15304   /* Scan the base-specifier-list.  */
15305   while (true)
15306     {
15307       cp_token *token;
15308       tree base;
15309       bool pack_expansion_p = false;
15310
15311       /* Look for the base-specifier.  */
15312       base = cp_parser_base_specifier (parser);
15313       /* Look for the (optional) ellipsis. */
15314       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15315         {
15316           /* Consume the `...'. */
15317           cp_lexer_consume_token (parser->lexer);
15318
15319           pack_expansion_p = true;
15320         }
15321
15322       /* Add BASE to the front of the list.  */
15323       if (base != error_mark_node)
15324         {
15325           if (pack_expansion_p)
15326             /* Make this a pack expansion type. */
15327             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15328           
15329
15330           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15331             {
15332               TREE_CHAIN (base) = bases;
15333               bases = base;
15334             }
15335         }
15336       /* Peek at the next token.  */
15337       token = cp_lexer_peek_token (parser->lexer);
15338       /* If it's not a comma, then the list is complete.  */
15339       if (token->type != CPP_COMMA)
15340         break;
15341       /* Consume the `,'.  */
15342       cp_lexer_consume_token (parser->lexer);
15343     }
15344
15345   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15346      base class had a qualified name.  However, the next name that
15347      appears is certainly not qualified.  */
15348   parser->scope = NULL_TREE;
15349   parser->qualifying_scope = NULL_TREE;
15350   parser->object_scope = NULL_TREE;
15351
15352   return nreverse (bases);
15353 }
15354
15355 /* Parse a base-specifier.
15356
15357    base-specifier:
15358      :: [opt] nested-name-specifier [opt] class-name
15359      virtual access-specifier [opt] :: [opt] nested-name-specifier
15360        [opt] class-name
15361      access-specifier virtual [opt] :: [opt] nested-name-specifier
15362        [opt] class-name
15363
15364    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15365    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15366    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15367    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15368
15369 static tree
15370 cp_parser_base_specifier (cp_parser* parser)
15371 {
15372   cp_token *token;
15373   bool done = false;
15374   bool virtual_p = false;
15375   bool duplicate_virtual_error_issued_p = false;
15376   bool duplicate_access_error_issued_p = false;
15377   bool class_scope_p, template_p;
15378   tree access = access_default_node;
15379   tree type;
15380
15381   /* Process the optional `virtual' and `access-specifier'.  */
15382   while (!done)
15383     {
15384       /* Peek at the next token.  */
15385       token = cp_lexer_peek_token (parser->lexer);
15386       /* Process `virtual'.  */
15387       switch (token->keyword)
15388         {
15389         case RID_VIRTUAL:
15390           /* If `virtual' appears more than once, issue an error.  */
15391           if (virtual_p && !duplicate_virtual_error_issued_p)
15392             {
15393               cp_parser_error (parser,
15394                                "%<virtual%> specified more than once in base-specified");
15395               duplicate_virtual_error_issued_p = true;
15396             }
15397
15398           virtual_p = true;
15399
15400           /* Consume the `virtual' token.  */
15401           cp_lexer_consume_token (parser->lexer);
15402
15403           break;
15404
15405         case RID_PUBLIC:
15406         case RID_PROTECTED:
15407         case RID_PRIVATE:
15408           /* If more than one access specifier appears, issue an
15409              error.  */
15410           if (access != access_default_node
15411               && !duplicate_access_error_issued_p)
15412             {
15413               cp_parser_error (parser,
15414                                "more than one access specifier in base-specified");
15415               duplicate_access_error_issued_p = true;
15416             }
15417
15418           access = ridpointers[(int) token->keyword];
15419
15420           /* Consume the access-specifier.  */
15421           cp_lexer_consume_token (parser->lexer);
15422
15423           break;
15424
15425         default:
15426           done = true;
15427           break;
15428         }
15429     }
15430   /* It is not uncommon to see programs mechanically, erroneously, use
15431      the 'typename' keyword to denote (dependent) qualified types
15432      as base classes.  */
15433   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15434     {
15435       if (!processing_template_decl)
15436         error ("keyword %<typename%> not allowed outside of templates");
15437       else
15438         error ("keyword %<typename%> not allowed in this context "
15439                "(the base class is implicitly a type)");
15440       cp_lexer_consume_token (parser->lexer);
15441     }
15442
15443   /* Look for the optional `::' operator.  */
15444   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15445   /* Look for the nested-name-specifier.  The simplest way to
15446      implement:
15447
15448        [temp.res]
15449
15450        The keyword `typename' is not permitted in a base-specifier or
15451        mem-initializer; in these contexts a qualified name that
15452        depends on a template-parameter is implicitly assumed to be a
15453        type name.
15454
15455      is to pretend that we have seen the `typename' keyword at this
15456      point.  */
15457   cp_parser_nested_name_specifier_opt (parser,
15458                                        /*typename_keyword_p=*/true,
15459                                        /*check_dependency_p=*/true,
15460                                        typename_type,
15461                                        /*is_declaration=*/true);
15462   /* If the base class is given by a qualified name, assume that names
15463      we see are type names or templates, as appropriate.  */
15464   class_scope_p = (parser->scope && TYPE_P (parser->scope));
15465   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15466
15467   /* Finally, look for the class-name.  */
15468   type = cp_parser_class_name (parser,
15469                                class_scope_p,
15470                                template_p,
15471                                typename_type,
15472                                /*check_dependency_p=*/true,
15473                                /*class_head_p=*/false,
15474                                /*is_declaration=*/true);
15475
15476   if (type == error_mark_node)
15477     return error_mark_node;
15478
15479   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15480 }
15481
15482 /* Exception handling [gram.exception] */
15483
15484 /* Parse an (optional) exception-specification.
15485
15486    exception-specification:
15487      throw ( type-id-list [opt] )
15488
15489    Returns a TREE_LIST representing the exception-specification.  The
15490    TREE_VALUE of each node is a type.  */
15491
15492 static tree
15493 cp_parser_exception_specification_opt (cp_parser* parser)
15494 {
15495   cp_token *token;
15496   tree type_id_list;
15497
15498   /* Peek at the next token.  */
15499   token = cp_lexer_peek_token (parser->lexer);
15500   /* If it's not `throw', then there's no exception-specification.  */
15501   if (!cp_parser_is_keyword (token, RID_THROW))
15502     return NULL_TREE;
15503
15504   /* Consume the `throw'.  */
15505   cp_lexer_consume_token (parser->lexer);
15506
15507   /* Look for the `('.  */
15508   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15509
15510   /* Peek at the next token.  */
15511   token = cp_lexer_peek_token (parser->lexer);
15512   /* If it's not a `)', then there is a type-id-list.  */
15513   if (token->type != CPP_CLOSE_PAREN)
15514     {
15515       const char *saved_message;
15516
15517       /* Types may not be defined in an exception-specification.  */
15518       saved_message = parser->type_definition_forbidden_message;
15519       parser->type_definition_forbidden_message
15520         = "types may not be defined in an exception-specification";
15521       /* Parse the type-id-list.  */
15522       type_id_list = cp_parser_type_id_list (parser);
15523       /* Restore the saved message.  */
15524       parser->type_definition_forbidden_message = saved_message;
15525     }
15526   else
15527     type_id_list = empty_except_spec;
15528
15529   /* Look for the `)'.  */
15530   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15531
15532   return type_id_list;
15533 }
15534
15535 /* Parse an (optional) type-id-list.
15536
15537    type-id-list:
15538      type-id ... [opt]
15539      type-id-list , type-id ... [opt]
15540
15541    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
15542    in the order that the types were presented.  */
15543
15544 static tree
15545 cp_parser_type_id_list (cp_parser* parser)
15546 {
15547   tree types = NULL_TREE;
15548
15549   while (true)
15550     {
15551       cp_token *token;
15552       tree type;
15553
15554       /* Get the next type-id.  */
15555       type = cp_parser_type_id (parser);
15556       /* Parse the optional ellipsis. */
15557       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15558         {
15559           /* Consume the `...'. */
15560           cp_lexer_consume_token (parser->lexer);
15561
15562           /* Turn the type into a pack expansion expression. */
15563           type = make_pack_expansion (type);
15564         }
15565       /* Add it to the list.  */
15566       types = add_exception_specifier (types, type, /*complain=*/1);
15567       /* Peek at the next token.  */
15568       token = cp_lexer_peek_token (parser->lexer);
15569       /* If it is not a `,', we are done.  */
15570       if (token->type != CPP_COMMA)
15571         break;
15572       /* Consume the `,'.  */
15573       cp_lexer_consume_token (parser->lexer);
15574     }
15575
15576   return nreverse (types);
15577 }
15578
15579 /* Parse a try-block.
15580
15581    try-block:
15582      try compound-statement handler-seq  */
15583
15584 static tree
15585 cp_parser_try_block (cp_parser* parser)
15586 {
15587   tree try_block;
15588
15589   cp_parser_require_keyword (parser, RID_TRY, "`try'");
15590   try_block = begin_try_block ();
15591   cp_parser_compound_statement (parser, NULL, true);
15592   finish_try_block (try_block);
15593   cp_parser_handler_seq (parser);
15594   finish_handler_sequence (try_block);
15595
15596   return try_block;
15597 }
15598
15599 /* Parse a function-try-block.
15600
15601    function-try-block:
15602      try ctor-initializer [opt] function-body handler-seq  */
15603
15604 static bool
15605 cp_parser_function_try_block (cp_parser* parser)
15606 {
15607   tree compound_stmt;
15608   tree try_block;
15609   bool ctor_initializer_p;
15610
15611   /* Look for the `try' keyword.  */
15612   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15613     return false;
15614   /* Let the rest of the front end know where we are.  */
15615   try_block = begin_function_try_block (&compound_stmt);
15616   /* Parse the function-body.  */
15617   ctor_initializer_p
15618     = cp_parser_ctor_initializer_opt_and_function_body (parser);
15619   /* We're done with the `try' part.  */
15620   finish_function_try_block (try_block);
15621   /* Parse the handlers.  */
15622   cp_parser_handler_seq (parser);
15623   /* We're done with the handlers.  */
15624   finish_function_handler_sequence (try_block, compound_stmt);
15625
15626   return ctor_initializer_p;
15627 }
15628
15629 /* Parse a handler-seq.
15630
15631    handler-seq:
15632      handler handler-seq [opt]  */
15633
15634 static void
15635 cp_parser_handler_seq (cp_parser* parser)
15636 {
15637   while (true)
15638     {
15639       cp_token *token;
15640
15641       /* Parse the handler.  */
15642       cp_parser_handler (parser);
15643       /* Peek at the next token.  */
15644       token = cp_lexer_peek_token (parser->lexer);
15645       /* If it's not `catch' then there are no more handlers.  */
15646       if (!cp_parser_is_keyword (token, RID_CATCH))
15647         break;
15648     }
15649 }
15650
15651 /* Parse a handler.
15652
15653    handler:
15654      catch ( exception-declaration ) compound-statement  */
15655
15656 static void
15657 cp_parser_handler (cp_parser* parser)
15658 {
15659   tree handler;
15660   tree declaration;
15661
15662   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15663   handler = begin_handler ();
15664   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15665   declaration = cp_parser_exception_declaration (parser);
15666   finish_handler_parms (declaration, handler);
15667   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15668   cp_parser_compound_statement (parser, NULL, false);
15669   finish_handler (handler);
15670 }
15671
15672 /* Parse an exception-declaration.
15673
15674    exception-declaration:
15675      type-specifier-seq declarator
15676      type-specifier-seq abstract-declarator
15677      type-specifier-seq
15678      ...
15679
15680    Returns a VAR_DECL for the declaration, or NULL_TREE if the
15681    ellipsis variant is used.  */
15682
15683 static tree
15684 cp_parser_exception_declaration (cp_parser* parser)
15685 {
15686   cp_decl_specifier_seq type_specifiers;
15687   cp_declarator *declarator;
15688   const char *saved_message;
15689
15690   /* If it's an ellipsis, it's easy to handle.  */
15691   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15692     {
15693       /* Consume the `...' token.  */
15694       cp_lexer_consume_token (parser->lexer);
15695       return NULL_TREE;
15696     }
15697
15698   /* Types may not be defined in exception-declarations.  */
15699   saved_message = parser->type_definition_forbidden_message;
15700   parser->type_definition_forbidden_message
15701     = "types may not be defined in exception-declarations";
15702
15703   /* Parse the type-specifier-seq.  */
15704   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15705                                 &type_specifiers);
15706   /* If it's a `)', then there is no declarator.  */
15707   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15708     declarator = NULL;
15709   else
15710     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15711                                        /*ctor_dtor_or_conv_p=*/NULL,
15712                                        /*parenthesized_p=*/NULL,
15713                                        /*member_p=*/false);
15714
15715   /* Restore the saved message.  */
15716   parser->type_definition_forbidden_message = saved_message;
15717
15718   if (!type_specifiers.any_specifiers_p)
15719     return error_mark_node;
15720
15721   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15722 }
15723
15724 /* Parse a throw-expression.
15725
15726    throw-expression:
15727      throw assignment-expression [opt]
15728
15729    Returns a THROW_EXPR representing the throw-expression.  */
15730
15731 static tree
15732 cp_parser_throw_expression (cp_parser* parser)
15733 {
15734   tree expression;
15735   cp_token* token;
15736
15737   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15738   token = cp_lexer_peek_token (parser->lexer);
15739   /* Figure out whether or not there is an assignment-expression
15740      following the "throw" keyword.  */
15741   if (token->type == CPP_COMMA
15742       || token->type == CPP_SEMICOLON
15743       || token->type == CPP_CLOSE_PAREN
15744       || token->type == CPP_CLOSE_SQUARE
15745       || token->type == CPP_CLOSE_BRACE
15746       || token->type == CPP_COLON)
15747     expression = NULL_TREE;
15748   else
15749     expression = cp_parser_assignment_expression (parser,
15750                                                   /*cast_p=*/false);
15751
15752   return build_throw (expression);
15753 }
15754
15755 /* GNU Extensions */
15756
15757 /* Parse an (optional) asm-specification.
15758
15759    asm-specification:
15760      asm ( string-literal )
15761
15762    If the asm-specification is present, returns a STRING_CST
15763    corresponding to the string-literal.  Otherwise, returns
15764    NULL_TREE.  */
15765
15766 static tree
15767 cp_parser_asm_specification_opt (cp_parser* parser)
15768 {
15769   cp_token *token;
15770   tree asm_specification;
15771
15772   /* Peek at the next token.  */
15773   token = cp_lexer_peek_token (parser->lexer);
15774   /* If the next token isn't the `asm' keyword, then there's no
15775      asm-specification.  */
15776   if (!cp_parser_is_keyword (token, RID_ASM))
15777     return NULL_TREE;
15778
15779   /* Consume the `asm' token.  */
15780   cp_lexer_consume_token (parser->lexer);
15781   /* Look for the `('.  */
15782   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15783
15784   /* Look for the string-literal.  */
15785   asm_specification = cp_parser_string_literal (parser, false, false);
15786
15787   /* Look for the `)'.  */
15788   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15789
15790   return asm_specification;
15791 }
15792
15793 /* Parse an asm-operand-list.
15794
15795    asm-operand-list:
15796      asm-operand
15797      asm-operand-list , asm-operand
15798
15799    asm-operand:
15800      string-literal ( expression )
15801      [ string-literal ] string-literal ( expression )
15802
15803    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
15804    each node is the expression.  The TREE_PURPOSE is itself a
15805    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15806    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15807    is a STRING_CST for the string literal before the parenthesis. Returns
15808    ERROR_MARK_NODE if any of the operands are invalid.  */
15809
15810 static tree
15811 cp_parser_asm_operand_list (cp_parser* parser)
15812 {
15813   tree asm_operands = NULL_TREE;
15814   bool invalid_operands = false;
15815
15816   while (true)
15817     {
15818       tree string_literal;
15819       tree expression;
15820       tree name;
15821
15822       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15823         {
15824           /* Consume the `[' token.  */
15825           cp_lexer_consume_token (parser->lexer);
15826           /* Read the operand name.  */
15827           name = cp_parser_identifier (parser);
15828           if (name != error_mark_node)
15829             name = build_string (IDENTIFIER_LENGTH (name),
15830                                  IDENTIFIER_POINTER (name));
15831           /* Look for the closing `]'.  */
15832           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15833         }
15834       else
15835         name = NULL_TREE;
15836       /* Look for the string-literal.  */
15837       string_literal = cp_parser_string_literal (parser, false, false);
15838
15839       /* Look for the `('.  */
15840       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15841       /* Parse the expression.  */
15842       expression = cp_parser_expression (parser, /*cast_p=*/false);
15843       /* Look for the `)'.  */
15844       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15845
15846       if (name == error_mark_node 
15847           || string_literal == error_mark_node 
15848           || expression == error_mark_node)
15849         invalid_operands = true;
15850
15851       /* Add this operand to the list.  */
15852       asm_operands = tree_cons (build_tree_list (name, string_literal),
15853                                 expression,
15854                                 asm_operands);
15855       /* If the next token is not a `,', there are no more
15856          operands.  */
15857       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15858         break;
15859       /* Consume the `,'.  */
15860       cp_lexer_consume_token (parser->lexer);
15861     }
15862
15863   return invalid_operands ? error_mark_node : nreverse (asm_operands);
15864 }
15865
15866 /* Parse an asm-clobber-list.
15867
15868    asm-clobber-list:
15869      string-literal
15870      asm-clobber-list , string-literal
15871
15872    Returns a TREE_LIST, indicating the clobbers in the order that they
15873    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
15874
15875 static tree
15876 cp_parser_asm_clobber_list (cp_parser* parser)
15877 {
15878   tree clobbers = NULL_TREE;
15879
15880   while (true)
15881     {
15882       tree string_literal;
15883
15884       /* Look for the string literal.  */
15885       string_literal = cp_parser_string_literal (parser, false, false);
15886       /* Add it to the list.  */
15887       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15888       /* If the next token is not a `,', then the list is
15889          complete.  */
15890       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15891         break;
15892       /* Consume the `,' token.  */
15893       cp_lexer_consume_token (parser->lexer);
15894     }
15895
15896   return clobbers;
15897 }
15898
15899 /* Parse an (optional) series of attributes.
15900
15901    attributes:
15902      attributes attribute
15903
15904    attribute:
15905      __attribute__ (( attribute-list [opt] ))
15906
15907    The return value is as for cp_parser_attribute_list.  */
15908
15909 static tree
15910 cp_parser_attributes_opt (cp_parser* parser)
15911 {
15912   tree attributes = NULL_TREE;
15913
15914   while (true)
15915     {
15916       cp_token *token;
15917       tree attribute_list;
15918
15919       /* Peek at the next token.  */
15920       token = cp_lexer_peek_token (parser->lexer);
15921       /* If it's not `__attribute__', then we're done.  */
15922       if (token->keyword != RID_ATTRIBUTE)
15923         break;
15924
15925       /* Consume the `__attribute__' keyword.  */
15926       cp_lexer_consume_token (parser->lexer);
15927       /* Look for the two `(' tokens.  */
15928       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15929       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15930
15931       /* Peek at the next token.  */
15932       token = cp_lexer_peek_token (parser->lexer);
15933       if (token->type != CPP_CLOSE_PAREN)
15934         /* Parse the attribute-list.  */
15935         attribute_list = cp_parser_attribute_list (parser);
15936       else
15937         /* If the next token is a `)', then there is no attribute
15938            list.  */
15939         attribute_list = NULL;
15940
15941       /* Look for the two `)' tokens.  */
15942       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15943       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15944
15945       /* Add these new attributes to the list.  */
15946       attributes = chainon (attributes, attribute_list);
15947     }
15948
15949   return attributes;
15950 }
15951
15952 /* Parse an attribute-list.
15953
15954    attribute-list:
15955      attribute
15956      attribute-list , attribute
15957
15958    attribute:
15959      identifier
15960      identifier ( identifier )
15961      identifier ( identifier , expression-list )
15962      identifier ( expression-list )
15963
15964    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
15965    to an attribute.  The TREE_PURPOSE of each node is the identifier
15966    indicating which attribute is in use.  The TREE_VALUE represents
15967    the arguments, if any.  */
15968
15969 static tree
15970 cp_parser_attribute_list (cp_parser* parser)
15971 {
15972   tree attribute_list = NULL_TREE;
15973   bool save_translate_strings_p = parser->translate_strings_p;
15974
15975   parser->translate_strings_p = false;
15976   while (true)
15977     {
15978       cp_token *token;
15979       tree identifier;
15980       tree attribute;
15981
15982       /* Look for the identifier.  We also allow keywords here; for
15983          example `__attribute__ ((const))' is legal.  */
15984       token = cp_lexer_peek_token (parser->lexer);
15985       if (token->type == CPP_NAME
15986           || token->type == CPP_KEYWORD)
15987         {
15988           tree arguments = NULL_TREE;
15989
15990           /* Consume the token.  */
15991           token = cp_lexer_consume_token (parser->lexer);
15992
15993           /* Save away the identifier that indicates which attribute
15994              this is.  */
15995           identifier = token->u.value;
15996           attribute = build_tree_list (identifier, NULL_TREE);
15997
15998           /* Peek at the next token.  */
15999           token = cp_lexer_peek_token (parser->lexer);
16000           /* If it's an `(', then parse the attribute arguments.  */
16001           if (token->type == CPP_OPEN_PAREN)
16002             {
16003               arguments = cp_parser_parenthesized_expression_list
16004                           (parser, true, /*cast_p=*/false,
16005                            /*allow_expansion_p=*/false,
16006                            /*non_constant_p=*/NULL);
16007               /* Save the arguments away.  */
16008               TREE_VALUE (attribute) = arguments;
16009             }
16010
16011           if (arguments != error_mark_node)
16012             {
16013               /* Add this attribute to the list.  */
16014               TREE_CHAIN (attribute) = attribute_list;
16015               attribute_list = attribute;
16016             }
16017
16018           token = cp_lexer_peek_token (parser->lexer);
16019         }
16020       /* Now, look for more attributes.  If the next token isn't a
16021          `,', we're done.  */
16022       if (token->type != CPP_COMMA)
16023         break;
16024
16025       /* Consume the comma and keep going.  */
16026       cp_lexer_consume_token (parser->lexer);
16027     }
16028   parser->translate_strings_p = save_translate_strings_p;
16029
16030   /* We built up the list in reverse order.  */
16031   return nreverse (attribute_list);
16032 }
16033
16034 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16035    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16036    current value of the PEDANTIC flag, regardless of whether or not
16037    the `__extension__' keyword is present.  The caller is responsible
16038    for restoring the value of the PEDANTIC flag.  */
16039
16040 static bool
16041 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16042 {
16043   /* Save the old value of the PEDANTIC flag.  */
16044   *saved_pedantic = pedantic;
16045
16046   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16047     {
16048       /* Consume the `__extension__' token.  */
16049       cp_lexer_consume_token (parser->lexer);
16050       /* We're not being pedantic while the `__extension__' keyword is
16051          in effect.  */
16052       pedantic = 0;
16053
16054       return true;
16055     }
16056
16057   return false;
16058 }
16059
16060 /* Parse a label declaration.
16061
16062    label-declaration:
16063      __label__ label-declarator-seq ;
16064
16065    label-declarator-seq:
16066      identifier , label-declarator-seq
16067      identifier  */
16068
16069 static void
16070 cp_parser_label_declaration (cp_parser* parser)
16071 {
16072   /* Look for the `__label__' keyword.  */
16073   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
16074
16075   while (true)
16076     {
16077       tree identifier;
16078
16079       /* Look for an identifier.  */
16080       identifier = cp_parser_identifier (parser);
16081       /* If we failed, stop.  */
16082       if (identifier == error_mark_node)
16083         break;
16084       /* Declare it as a label.  */
16085       finish_label_decl (identifier);
16086       /* If the next token is a `;', stop.  */
16087       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16088         break;
16089       /* Look for the `,' separating the label declarations.  */
16090       cp_parser_require (parser, CPP_COMMA, "`,'");
16091     }
16092
16093   /* Look for the final `;'.  */
16094   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
16095 }
16096
16097 /* Support Functions */
16098
16099 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16100    NAME should have one of the representations used for an
16101    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16102    is returned.  If PARSER->SCOPE is a dependent type, then a
16103    SCOPE_REF is returned.
16104
16105    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16106    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16107    was formed.  Abstractly, such entities should not be passed to this
16108    function, because they do not need to be looked up, but it is
16109    simpler to check for this special case here, rather than at the
16110    call-sites.
16111
16112    In cases not explicitly covered above, this function returns a
16113    DECL, OVERLOAD, or baselink representing the result of the lookup.
16114    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16115    is returned.
16116
16117    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16118    (e.g., "struct") that was used.  In that case bindings that do not
16119    refer to types are ignored.
16120
16121    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16122    ignored.
16123
16124    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16125    are ignored.
16126
16127    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16128    types.
16129
16130    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16131    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16132    NULL_TREE otherwise.  */
16133
16134 static tree
16135 cp_parser_lookup_name (cp_parser *parser, tree name,
16136                        enum tag_types tag_type,
16137                        bool is_template,
16138                        bool is_namespace,
16139                        bool check_dependency,
16140                        tree *ambiguous_decls)
16141 {
16142   int flags = 0;
16143   tree decl;
16144   tree object_type = parser->context->object_type;
16145
16146   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16147     flags |= LOOKUP_COMPLAIN;
16148
16149   /* Assume that the lookup will be unambiguous.  */
16150   if (ambiguous_decls)
16151     *ambiguous_decls = NULL_TREE;
16152
16153   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16154      no longer valid.  Note that if we are parsing tentatively, and
16155      the parse fails, OBJECT_TYPE will be automatically restored.  */
16156   parser->context->object_type = NULL_TREE;
16157
16158   if (name == error_mark_node)
16159     return error_mark_node;
16160
16161   /* A template-id has already been resolved; there is no lookup to
16162      do.  */
16163   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16164     return name;
16165   if (BASELINK_P (name))
16166     {
16167       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16168                   == TEMPLATE_ID_EXPR);
16169       return name;
16170     }
16171
16172   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16173      it should already have been checked to make sure that the name
16174      used matches the type being destroyed.  */
16175   if (TREE_CODE (name) == BIT_NOT_EXPR)
16176     {
16177       tree type;
16178
16179       /* Figure out to which type this destructor applies.  */
16180       if (parser->scope)
16181         type = parser->scope;
16182       else if (object_type)
16183         type = object_type;
16184       else
16185         type = current_class_type;
16186       /* If that's not a class type, there is no destructor.  */
16187       if (!type || !CLASS_TYPE_P (type))
16188         return error_mark_node;
16189       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16190         lazily_declare_fn (sfk_destructor, type);
16191       if (!CLASSTYPE_DESTRUCTORS (type))
16192           return error_mark_node;
16193       /* If it was a class type, return the destructor.  */
16194       return CLASSTYPE_DESTRUCTORS (type);
16195     }
16196
16197   /* By this point, the NAME should be an ordinary identifier.  If
16198      the id-expression was a qualified name, the qualifying scope is
16199      stored in PARSER->SCOPE at this point.  */
16200   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16201
16202   /* Perform the lookup.  */
16203   if (parser->scope)
16204     {
16205       bool dependent_p;
16206
16207       if (parser->scope == error_mark_node)
16208         return error_mark_node;
16209
16210       /* If the SCOPE is dependent, the lookup must be deferred until
16211          the template is instantiated -- unless we are explicitly
16212          looking up names in uninstantiated templates.  Even then, we
16213          cannot look up the name if the scope is not a class type; it
16214          might, for example, be a template type parameter.  */
16215       dependent_p = (TYPE_P (parser->scope)
16216                      && !(parser->in_declarator_p
16217                           && currently_open_class (parser->scope))
16218                      && dependent_type_p (parser->scope));
16219       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16220            && dependent_p)
16221         {
16222           if (tag_type)
16223             {
16224               tree type;
16225
16226               /* The resolution to Core Issue 180 says that `struct
16227                  A::B' should be considered a type-name, even if `A'
16228                  is dependent.  */
16229               type = make_typename_type (parser->scope, name, tag_type,
16230                                          /*complain=*/tf_error);
16231               decl = TYPE_NAME (type);
16232             }
16233           else if (is_template
16234                    && (cp_parser_next_token_ends_template_argument_p (parser)
16235                        || cp_lexer_next_token_is (parser->lexer,
16236                                                   CPP_CLOSE_PAREN)))
16237             decl = make_unbound_class_template (parser->scope,
16238                                                 name, NULL_TREE,
16239                                                 /*complain=*/tf_error);
16240           else
16241             decl = build_qualified_name (/*type=*/NULL_TREE,
16242                                          parser->scope, name,
16243                                          is_template);
16244         }
16245       else
16246         {
16247           tree pushed_scope = NULL_TREE;
16248
16249           /* If PARSER->SCOPE is a dependent type, then it must be a
16250              class type, and we must not be checking dependencies;
16251              otherwise, we would have processed this lookup above.  So
16252              that PARSER->SCOPE is not considered a dependent base by
16253              lookup_member, we must enter the scope here.  */
16254           if (dependent_p)
16255             pushed_scope = push_scope (parser->scope);
16256           /* If the PARSER->SCOPE is a template specialization, it
16257              may be instantiated during name lookup.  In that case,
16258              errors may be issued.  Even if we rollback the current
16259              tentative parse, those errors are valid.  */
16260           decl = lookup_qualified_name (parser->scope, name,
16261                                         tag_type != none_type,
16262                                         /*complain=*/true);
16263           if (pushed_scope)
16264             pop_scope (pushed_scope);
16265         }
16266       parser->qualifying_scope = parser->scope;
16267       parser->object_scope = NULL_TREE;
16268     }
16269   else if (object_type)
16270     {
16271       tree object_decl = NULL_TREE;
16272       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16273          OBJECT_TYPE is not a class.  */
16274       if (CLASS_TYPE_P (object_type))
16275         /* If the OBJECT_TYPE is a template specialization, it may
16276            be instantiated during name lookup.  In that case, errors
16277            may be issued.  Even if we rollback the current tentative
16278            parse, those errors are valid.  */
16279         object_decl = lookup_member (object_type,
16280                                      name,
16281                                      /*protect=*/0,
16282                                      tag_type != none_type);
16283       /* Look it up in the enclosing context, too.  */
16284       decl = lookup_name_real (name, tag_type != none_type,
16285                                /*nonclass=*/0,
16286                                /*block_p=*/true, is_namespace, flags);
16287       parser->object_scope = object_type;
16288       parser->qualifying_scope = NULL_TREE;
16289       if (object_decl)
16290         decl = object_decl;
16291     }
16292   else
16293     {
16294       decl = lookup_name_real (name, tag_type != none_type,
16295                                /*nonclass=*/0,
16296                                /*block_p=*/true, is_namespace, flags);
16297       parser->qualifying_scope = NULL_TREE;
16298       parser->object_scope = NULL_TREE;
16299     }
16300
16301   /* If the lookup failed, let our caller know.  */
16302   if (!decl || decl == error_mark_node)
16303     return error_mark_node;
16304
16305   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16306   if (TREE_CODE (decl) == TREE_LIST)
16307     {
16308       if (ambiguous_decls)
16309         *ambiguous_decls = decl;
16310       /* The error message we have to print is too complicated for
16311          cp_parser_error, so we incorporate its actions directly.  */
16312       if (!cp_parser_simulate_error (parser))
16313         {
16314           error ("reference to %qD is ambiguous", name);
16315           print_candidates (decl);
16316         }
16317       return error_mark_node;
16318     }
16319
16320   gcc_assert (DECL_P (decl)
16321               || TREE_CODE (decl) == OVERLOAD
16322               || TREE_CODE (decl) == SCOPE_REF
16323               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16324               || BASELINK_P (decl));
16325
16326   /* If we have resolved the name of a member declaration, check to
16327      see if the declaration is accessible.  When the name resolves to
16328      set of overloaded functions, accessibility is checked when
16329      overload resolution is done.
16330
16331      During an explicit instantiation, access is not checked at all,
16332      as per [temp.explicit].  */
16333   if (DECL_P (decl))
16334     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16335
16336   return decl;
16337 }
16338
16339 /* Like cp_parser_lookup_name, but for use in the typical case where
16340    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16341    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16342
16343 static tree
16344 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16345 {
16346   return cp_parser_lookup_name (parser, name,
16347                                 none_type,
16348                                 /*is_template=*/false,
16349                                 /*is_namespace=*/false,
16350                                 /*check_dependency=*/true,
16351                                 /*ambiguous_decls=*/NULL);
16352 }
16353
16354 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16355    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16356    true, the DECL indicates the class being defined in a class-head,
16357    or declared in an elaborated-type-specifier.
16358
16359    Otherwise, return DECL.  */
16360
16361 static tree
16362 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16363 {
16364   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16365      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16366
16367        struct A {
16368          template <typename T> struct B;
16369        };
16370
16371        template <typename T> struct A::B {};
16372
16373      Similarly, in an elaborated-type-specifier:
16374
16375        namespace N { struct X{}; }
16376
16377        struct A {
16378          template <typename T> friend struct N::X;
16379        };
16380
16381      However, if the DECL refers to a class type, and we are in
16382      the scope of the class, then the name lookup automatically
16383      finds the TYPE_DECL created by build_self_reference rather
16384      than a TEMPLATE_DECL.  For example, in:
16385
16386        template <class T> struct S {
16387          S s;
16388        };
16389
16390      there is no need to handle such case.  */
16391
16392   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16393     return DECL_TEMPLATE_RESULT (decl);
16394
16395   return decl;
16396 }
16397
16398 /* If too many, or too few, template-parameter lists apply to the
16399    declarator, issue an error message.  Returns TRUE if all went well,
16400    and FALSE otherwise.  */
16401
16402 static bool
16403 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16404                                                 cp_declarator *declarator)
16405 {
16406   unsigned num_templates;
16407
16408   /* We haven't seen any classes that involve template parameters yet.  */
16409   num_templates = 0;
16410
16411   switch (declarator->kind)
16412     {
16413     case cdk_id:
16414       if (declarator->u.id.qualifying_scope)
16415         {
16416           tree scope;
16417           tree member;
16418
16419           scope = declarator->u.id.qualifying_scope;
16420           member = declarator->u.id.unqualified_name;
16421
16422           while (scope && CLASS_TYPE_P (scope))
16423             {
16424               /* You're supposed to have one `template <...>'
16425                  for every template class, but you don't need one
16426                  for a full specialization.  For example:
16427
16428                  template <class T> struct S{};
16429                  template <> struct S<int> { void f(); };
16430                  void S<int>::f () {}
16431
16432                  is correct; there shouldn't be a `template <>' for
16433                  the definition of `S<int>::f'.  */
16434               if (!CLASSTYPE_TEMPLATE_INFO (scope))
16435                 /* If SCOPE does not have template information of any
16436                    kind, then it is not a template, nor is it nested
16437                    within a template.  */
16438                 break;
16439               if (explicit_class_specialization_p (scope))
16440                 break;
16441               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16442                 ++num_templates;
16443
16444               scope = TYPE_CONTEXT (scope);
16445             }
16446         }
16447       else if (TREE_CODE (declarator->u.id.unqualified_name)
16448                == TEMPLATE_ID_EXPR)
16449         /* If the DECLARATOR has the form `X<y>' then it uses one
16450            additional level of template parameters.  */
16451         ++num_templates;
16452
16453       return cp_parser_check_template_parameters (parser,
16454                                                   num_templates);
16455
16456     case cdk_function:
16457     case cdk_array:
16458     case cdk_pointer:
16459     case cdk_reference:
16460     case cdk_ptrmem:
16461       return (cp_parser_check_declarator_template_parameters
16462               (parser, declarator->declarator));
16463
16464     case cdk_error:
16465       return true;
16466
16467     default:
16468       gcc_unreachable ();
16469     }
16470   return false;
16471 }
16472
16473 /* NUM_TEMPLATES were used in the current declaration.  If that is
16474    invalid, return FALSE and issue an error messages.  Otherwise,
16475    return TRUE.  */
16476
16477 static bool
16478 cp_parser_check_template_parameters (cp_parser* parser,
16479                                      unsigned num_templates)
16480 {
16481   /* If there are more template classes than parameter lists, we have
16482      something like:
16483
16484        template <class T> void S<T>::R<T>::f ();  */
16485   if (parser->num_template_parameter_lists < num_templates)
16486     {
16487       error ("too few template-parameter-lists");
16488       return false;
16489     }
16490   /* If there are the same number of template classes and parameter
16491      lists, that's OK.  */
16492   if (parser->num_template_parameter_lists == num_templates)
16493     return true;
16494   /* If there are more, but only one more, then we are referring to a
16495      member template.  That's OK too.  */
16496   if (parser->num_template_parameter_lists == num_templates + 1)
16497       return true;
16498   /* Otherwise, there are too many template parameter lists.  We have
16499      something like:
16500
16501      template <class T> template <class U> void S::f();  */
16502   error ("too many template-parameter-lists");
16503   return false;
16504 }
16505
16506 /* Parse an optional `::' token indicating that the following name is
16507    from the global namespace.  If so, PARSER->SCOPE is set to the
16508    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16509    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16510    Returns the new value of PARSER->SCOPE, if the `::' token is
16511    present, and NULL_TREE otherwise.  */
16512
16513 static tree
16514 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16515 {
16516   cp_token *token;
16517
16518   /* Peek at the next token.  */
16519   token = cp_lexer_peek_token (parser->lexer);
16520   /* If we're looking at a `::' token then we're starting from the
16521      global namespace, not our current location.  */
16522   if (token->type == CPP_SCOPE)
16523     {
16524       /* Consume the `::' token.  */
16525       cp_lexer_consume_token (parser->lexer);
16526       /* Set the SCOPE so that we know where to start the lookup.  */
16527       parser->scope = global_namespace;
16528       parser->qualifying_scope = global_namespace;
16529       parser->object_scope = NULL_TREE;
16530
16531       return parser->scope;
16532     }
16533   else if (!current_scope_valid_p)
16534     {
16535       parser->scope = NULL_TREE;
16536       parser->qualifying_scope = NULL_TREE;
16537       parser->object_scope = NULL_TREE;
16538     }
16539
16540   return NULL_TREE;
16541 }
16542
16543 /* Returns TRUE if the upcoming token sequence is the start of a
16544    constructor declarator.  If FRIEND_P is true, the declarator is
16545    preceded by the `friend' specifier.  */
16546
16547 static bool
16548 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16549 {
16550   bool constructor_p;
16551   tree type_decl = NULL_TREE;
16552   bool nested_name_p;
16553   cp_token *next_token;
16554
16555   /* The common case is that this is not a constructor declarator, so
16556      try to avoid doing lots of work if at all possible.  It's not
16557      valid declare a constructor at function scope.  */
16558   if (parser->in_function_body)
16559     return false;
16560   /* And only certain tokens can begin a constructor declarator.  */
16561   next_token = cp_lexer_peek_token (parser->lexer);
16562   if (next_token->type != CPP_NAME
16563       && next_token->type != CPP_SCOPE
16564       && next_token->type != CPP_NESTED_NAME_SPECIFIER
16565       && next_token->type != CPP_TEMPLATE_ID)
16566     return false;
16567
16568   /* Parse tentatively; we are going to roll back all of the tokens
16569      consumed here.  */
16570   cp_parser_parse_tentatively (parser);
16571   /* Assume that we are looking at a constructor declarator.  */
16572   constructor_p = true;
16573
16574   /* Look for the optional `::' operator.  */
16575   cp_parser_global_scope_opt (parser,
16576                               /*current_scope_valid_p=*/false);
16577   /* Look for the nested-name-specifier.  */
16578   nested_name_p
16579     = (cp_parser_nested_name_specifier_opt (parser,
16580                                             /*typename_keyword_p=*/false,
16581                                             /*check_dependency_p=*/false,
16582                                             /*type_p=*/false,
16583                                             /*is_declaration=*/false)
16584        != NULL_TREE);
16585   /* Outside of a class-specifier, there must be a
16586      nested-name-specifier.  */
16587   if (!nested_name_p &&
16588       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16589        || friend_p))
16590     constructor_p = false;
16591   /* If we still think that this might be a constructor-declarator,
16592      look for a class-name.  */
16593   if (constructor_p)
16594     {
16595       /* If we have:
16596
16597            template <typename T> struct S { S(); };
16598            template <typename T> S<T>::S ();
16599
16600          we must recognize that the nested `S' names a class.
16601          Similarly, for:
16602
16603            template <typename T> S<T>::S<T> ();
16604
16605          we must recognize that the nested `S' names a template.  */
16606       type_decl = cp_parser_class_name (parser,
16607                                         /*typename_keyword_p=*/false,
16608                                         /*template_keyword_p=*/false,
16609                                         none_type,
16610                                         /*check_dependency_p=*/false,
16611                                         /*class_head_p=*/false,
16612                                         /*is_declaration=*/false);
16613       /* If there was no class-name, then this is not a constructor.  */
16614       constructor_p = !cp_parser_error_occurred (parser);
16615     }
16616
16617   /* If we're still considering a constructor, we have to see a `(',
16618      to begin the parameter-declaration-clause, followed by either a
16619      `)', an `...', or a decl-specifier.  We need to check for a
16620      type-specifier to avoid being fooled into thinking that:
16621
16622        S::S (f) (int);
16623
16624      is a constructor.  (It is actually a function named `f' that
16625      takes one parameter (of type `int') and returns a value of type
16626      `S::S'.  */
16627   if (constructor_p
16628       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16629     {
16630       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16631           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16632           /* A parameter declaration begins with a decl-specifier,
16633              which is either the "attribute" keyword, a storage class
16634              specifier, or (usually) a type-specifier.  */
16635           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16636         {
16637           tree type;
16638           tree pushed_scope = NULL_TREE;
16639           unsigned saved_num_template_parameter_lists;
16640
16641           /* Names appearing in the type-specifier should be looked up
16642              in the scope of the class.  */
16643           if (current_class_type)
16644             type = NULL_TREE;
16645           else
16646             {
16647               type = TREE_TYPE (type_decl);
16648               if (TREE_CODE (type) == TYPENAME_TYPE)
16649                 {
16650                   type = resolve_typename_type (type,
16651                                                 /*only_current_p=*/false);
16652                   if (TREE_CODE (type) == TYPENAME_TYPE)
16653                     {
16654                       cp_parser_abort_tentative_parse (parser);
16655                       return false;
16656                     }
16657                 }
16658               pushed_scope = push_scope (type);
16659             }
16660
16661           /* Inside the constructor parameter list, surrounding
16662              template-parameter-lists do not apply.  */
16663           saved_num_template_parameter_lists
16664             = parser->num_template_parameter_lists;
16665           parser->num_template_parameter_lists = 0;
16666
16667           /* Look for the type-specifier.  */
16668           cp_parser_type_specifier (parser,
16669                                     CP_PARSER_FLAGS_NONE,
16670                                     /*decl_specs=*/NULL,
16671                                     /*is_declarator=*/true,
16672                                     /*declares_class_or_enum=*/NULL,
16673                                     /*is_cv_qualifier=*/NULL);
16674
16675           parser->num_template_parameter_lists
16676             = saved_num_template_parameter_lists;
16677
16678           /* Leave the scope of the class.  */
16679           if (pushed_scope)
16680             pop_scope (pushed_scope);
16681
16682           constructor_p = !cp_parser_error_occurred (parser);
16683         }
16684     }
16685   else
16686     constructor_p = false;
16687   /* We did not really want to consume any tokens.  */
16688   cp_parser_abort_tentative_parse (parser);
16689
16690   return constructor_p;
16691 }
16692
16693 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16694    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
16695    they must be performed once we are in the scope of the function.
16696
16697    Returns the function defined.  */
16698
16699 static tree
16700 cp_parser_function_definition_from_specifiers_and_declarator
16701   (cp_parser* parser,
16702    cp_decl_specifier_seq *decl_specifiers,
16703    tree attributes,
16704    const cp_declarator *declarator)
16705 {
16706   tree fn;
16707   bool success_p;
16708
16709   /* Begin the function-definition.  */
16710   success_p = start_function (decl_specifiers, declarator, attributes);
16711
16712   /* The things we're about to see are not directly qualified by any
16713      template headers we've seen thus far.  */
16714   reset_specialization ();
16715
16716   /* If there were names looked up in the decl-specifier-seq that we
16717      did not check, check them now.  We must wait until we are in the
16718      scope of the function to perform the checks, since the function
16719      might be a friend.  */
16720   perform_deferred_access_checks ();
16721
16722   if (!success_p)
16723     {
16724       /* Skip the entire function.  */
16725       cp_parser_skip_to_end_of_block_or_statement (parser);
16726       fn = error_mark_node;
16727     }
16728   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16729     {
16730       /* Seen already, skip it.  An error message has already been output.  */
16731       cp_parser_skip_to_end_of_block_or_statement (parser);
16732       fn = current_function_decl;
16733       current_function_decl = NULL_TREE;
16734       /* If this is a function from a class, pop the nested class.  */
16735       if (current_class_name)
16736         pop_nested_class ();
16737     }
16738   else
16739     fn = cp_parser_function_definition_after_declarator (parser,
16740                                                          /*inline_p=*/false);
16741
16742   return fn;
16743 }
16744
16745 /* Parse the part of a function-definition that follows the
16746    declarator.  INLINE_P is TRUE iff this function is an inline
16747    function defined with a class-specifier.
16748
16749    Returns the function defined.  */
16750
16751 static tree
16752 cp_parser_function_definition_after_declarator (cp_parser* parser,
16753                                                 bool inline_p)
16754 {
16755   tree fn;
16756   bool ctor_initializer_p = false;
16757   bool saved_in_unbraced_linkage_specification_p;
16758   bool saved_in_function_body;
16759   unsigned saved_num_template_parameter_lists;
16760
16761   saved_in_function_body = parser->in_function_body;
16762   parser->in_function_body = true;
16763   /* If the next token is `return', then the code may be trying to
16764      make use of the "named return value" extension that G++ used to
16765      support.  */
16766   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16767     {
16768       /* Consume the `return' keyword.  */
16769       cp_lexer_consume_token (parser->lexer);
16770       /* Look for the identifier that indicates what value is to be
16771          returned.  */
16772       cp_parser_identifier (parser);
16773       /* Issue an error message.  */
16774       error ("named return values are no longer supported");
16775       /* Skip tokens until we reach the start of the function body.  */
16776       while (true)
16777         {
16778           cp_token *token = cp_lexer_peek_token (parser->lexer);
16779           if (token->type == CPP_OPEN_BRACE
16780               || token->type == CPP_EOF
16781               || token->type == CPP_PRAGMA_EOL)
16782             break;
16783           cp_lexer_consume_token (parser->lexer);
16784         }
16785     }
16786   /* The `extern' in `extern "C" void f () { ... }' does not apply to
16787      anything declared inside `f'.  */
16788   saved_in_unbraced_linkage_specification_p
16789     = parser->in_unbraced_linkage_specification_p;
16790   parser->in_unbraced_linkage_specification_p = false;
16791   /* Inside the function, surrounding template-parameter-lists do not
16792      apply.  */
16793   saved_num_template_parameter_lists
16794     = parser->num_template_parameter_lists;
16795   parser->num_template_parameter_lists = 0;
16796   /* If the next token is `try', then we are looking at a
16797      function-try-block.  */
16798   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16799     ctor_initializer_p = cp_parser_function_try_block (parser);
16800   /* A function-try-block includes the function-body, so we only do
16801      this next part if we're not processing a function-try-block.  */
16802   else
16803     ctor_initializer_p
16804       = cp_parser_ctor_initializer_opt_and_function_body (parser);
16805
16806   /* Finish the function.  */
16807   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16808                         (inline_p ? 2 : 0));
16809   /* Generate code for it, if necessary.  */
16810   expand_or_defer_fn (fn);
16811   /* Restore the saved values.  */
16812   parser->in_unbraced_linkage_specification_p
16813     = saved_in_unbraced_linkage_specification_p;
16814   parser->num_template_parameter_lists
16815     = saved_num_template_parameter_lists;
16816   parser->in_function_body = saved_in_function_body;
16817
16818   return fn;
16819 }
16820
16821 /* Parse a template-declaration, assuming that the `export' (and
16822    `extern') keywords, if present, has already been scanned.  MEMBER_P
16823    is as for cp_parser_template_declaration.  */
16824
16825 static void
16826 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16827 {
16828   tree decl = NULL_TREE;
16829   VEC (deferred_access_check,gc) *checks;
16830   tree parameter_list;
16831   bool friend_p = false;
16832   bool need_lang_pop;
16833
16834   /* Look for the `template' keyword.  */
16835   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16836     return;
16837
16838   /* And the `<'.  */
16839   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16840     return;
16841   if (at_class_scope_p () && current_function_decl)
16842     {
16843       /* 14.5.2.2 [temp.mem]
16844
16845          A local class shall not have member templates.  */
16846       error ("invalid declaration of member template in local class");
16847       cp_parser_skip_to_end_of_block_or_statement (parser);
16848       return;
16849     }
16850   /* [temp]
16851
16852      A template ... shall not have C linkage.  */
16853   if (current_lang_name == lang_name_c)
16854     {
16855       error ("template with C linkage");
16856       /* Give it C++ linkage to avoid confusing other parts of the
16857          front end.  */
16858       push_lang_context (lang_name_cplusplus);
16859       need_lang_pop = true;
16860     }
16861   else
16862     need_lang_pop = false;
16863
16864   /* We cannot perform access checks on the template parameter
16865      declarations until we know what is being declared, just as we
16866      cannot check the decl-specifier list.  */
16867   push_deferring_access_checks (dk_deferred);
16868
16869   /* If the next token is `>', then we have an invalid
16870      specialization.  Rather than complain about an invalid template
16871      parameter, issue an error message here.  */
16872   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16873     {
16874       cp_parser_error (parser, "invalid explicit specialization");
16875       begin_specialization ();
16876       parameter_list = NULL_TREE;
16877     }
16878   else
16879     /* Parse the template parameters.  */
16880     parameter_list = cp_parser_template_parameter_list (parser);
16881
16882   /* Get the deferred access checks from the parameter list.  These
16883      will be checked once we know what is being declared, as for a
16884      member template the checks must be performed in the scope of the
16885      class containing the member.  */
16886   checks = get_deferred_access_checks ();
16887
16888   /* Look for the `>'.  */
16889   cp_parser_skip_to_end_of_template_parameter_list (parser);
16890   /* We just processed one more parameter list.  */
16891   ++parser->num_template_parameter_lists;
16892   /* If the next token is `template', there are more template
16893      parameters.  */
16894   if (cp_lexer_next_token_is_keyword (parser->lexer,
16895                                       RID_TEMPLATE))
16896     cp_parser_template_declaration_after_export (parser, member_p);
16897   else
16898     {
16899       /* There are no access checks when parsing a template, as we do not
16900          know if a specialization will be a friend.  */
16901       push_deferring_access_checks (dk_no_check);
16902       decl = cp_parser_single_declaration (parser,
16903                                            checks,
16904                                            member_p,
16905                                            /*explicit_specialization_p=*/false,
16906                                            &friend_p);
16907       pop_deferring_access_checks ();
16908
16909       /* If this is a member template declaration, let the front
16910          end know.  */
16911       if (member_p && !friend_p && decl)
16912         {
16913           if (TREE_CODE (decl) == TYPE_DECL)
16914             cp_parser_check_access_in_redeclaration (decl);
16915
16916           decl = finish_member_template_decl (decl);
16917         }
16918       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16919         make_friend_class (current_class_type, TREE_TYPE (decl),
16920                            /*complain=*/true);
16921     }
16922   /* We are done with the current parameter list.  */
16923   --parser->num_template_parameter_lists;
16924
16925   pop_deferring_access_checks ();
16926
16927   /* Finish up.  */
16928   finish_template_decl (parameter_list);
16929
16930   /* Register member declarations.  */
16931   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16932     finish_member_declaration (decl);
16933   /* For the erroneous case of a template with C linkage, we pushed an
16934      implicit C++ linkage scope; exit that scope now.  */
16935   if (need_lang_pop)
16936     pop_lang_context ();
16937   /* If DECL is a function template, we must return to parse it later.
16938      (Even though there is no definition, there might be default
16939      arguments that need handling.)  */
16940   if (member_p && decl
16941       && (TREE_CODE (decl) == FUNCTION_DECL
16942           || DECL_FUNCTION_TEMPLATE_P (decl)))
16943     TREE_VALUE (parser->unparsed_functions_queues)
16944       = tree_cons (NULL_TREE, decl,
16945                    TREE_VALUE (parser->unparsed_functions_queues));
16946 }
16947
16948 /* Perform the deferred access checks from a template-parameter-list.
16949    CHECKS is a TREE_LIST of access checks, as returned by
16950    get_deferred_access_checks.  */
16951
16952 static void
16953 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16954 {
16955   ++processing_template_parmlist;
16956   perform_access_checks (checks);
16957   --processing_template_parmlist;
16958 }
16959
16960 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16961    `function-definition' sequence.  MEMBER_P is true, this declaration
16962    appears in a class scope.
16963
16964    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
16965    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
16966
16967 static tree
16968 cp_parser_single_declaration (cp_parser* parser,
16969                               VEC (deferred_access_check,gc)* checks,
16970                               bool member_p,
16971                               bool explicit_specialization_p,
16972                               bool* friend_p)
16973 {
16974   int declares_class_or_enum;
16975   tree decl = NULL_TREE;
16976   cp_decl_specifier_seq decl_specifiers;
16977   bool function_definition_p = false;
16978
16979   /* This function is only used when processing a template
16980      declaration.  */
16981   gcc_assert (innermost_scope_kind () == sk_template_parms
16982               || innermost_scope_kind () == sk_template_spec);
16983
16984   /* Defer access checks until we know what is being declared.  */
16985   push_deferring_access_checks (dk_deferred);
16986
16987   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16988      alternative.  */
16989   cp_parser_decl_specifier_seq (parser,
16990                                 CP_PARSER_FLAGS_OPTIONAL,
16991                                 &decl_specifiers,
16992                                 &declares_class_or_enum);
16993   if (friend_p)
16994     *friend_p = cp_parser_friend_p (&decl_specifiers);
16995
16996   /* There are no template typedefs.  */
16997   if (decl_specifiers.specs[(int) ds_typedef])
16998     {
16999       error ("template declaration of %qs", "typedef");
17000       decl = error_mark_node;
17001     }
17002
17003   /* Gather up the access checks that occurred the
17004      decl-specifier-seq.  */
17005   stop_deferring_access_checks ();
17006
17007   /* Check for the declaration of a template class.  */
17008   if (declares_class_or_enum)
17009     {
17010       if (cp_parser_declares_only_class_p (parser))
17011         {
17012           decl = shadow_tag (&decl_specifiers);
17013
17014           /* In this case:
17015
17016                struct C {
17017                  friend template <typename T> struct A<T>::B;
17018                };
17019
17020              A<T>::B will be represented by a TYPENAME_TYPE, and
17021              therefore not recognized by shadow_tag.  */
17022           if (friend_p && *friend_p
17023               && !decl
17024               && decl_specifiers.type
17025               && TYPE_P (decl_specifiers.type))
17026             decl = decl_specifiers.type;
17027
17028           if (decl && decl != error_mark_node)
17029             decl = TYPE_NAME (decl);
17030           else
17031             decl = error_mark_node;
17032
17033           /* Perform access checks for template parameters.  */
17034           cp_parser_perform_template_parameter_access_checks (checks);
17035         }
17036     }
17037   /* If it's not a template class, try for a template function.  If
17038      the next token is a `;', then this declaration does not declare
17039      anything.  But, if there were errors in the decl-specifiers, then
17040      the error might well have come from an attempted class-specifier.
17041      In that case, there's no need to warn about a missing declarator.  */
17042   if (!decl
17043       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17044           || decl_specifiers.type != error_mark_node))
17045     {
17046       decl = cp_parser_init_declarator (parser,
17047                                         &decl_specifiers,
17048                                         checks,
17049                                         /*function_definition_allowed_p=*/true,
17050                                         member_p,
17051                                         declares_class_or_enum,
17052                                         &function_definition_p);
17053
17054     /* 7.1.1-1 [dcl.stc]
17055
17056        A storage-class-specifier shall not be specified in an explicit
17057        specialization...  */
17058     if (decl
17059         && explicit_specialization_p
17060         && decl_specifiers.storage_class != sc_none)
17061       {
17062         error ("explicit template specialization cannot have a storage class");
17063         decl = error_mark_node;
17064       }
17065     }
17066
17067   pop_deferring_access_checks ();
17068
17069   /* Clear any current qualification; whatever comes next is the start
17070      of something new.  */
17071   parser->scope = NULL_TREE;
17072   parser->qualifying_scope = NULL_TREE;
17073   parser->object_scope = NULL_TREE;
17074   /* Look for a trailing `;' after the declaration.  */
17075   if (!function_definition_p
17076       && (decl == error_mark_node
17077           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
17078     cp_parser_skip_to_end_of_block_or_statement (parser);
17079
17080   return decl;
17081 }
17082
17083 /* Parse a cast-expression that is not the operand of a unary "&".  */
17084
17085 static tree
17086 cp_parser_simple_cast_expression (cp_parser *parser)
17087 {
17088   return cp_parser_cast_expression (parser, /*address_p=*/false,
17089                                     /*cast_p=*/false);
17090 }
17091
17092 /* Parse a functional cast to TYPE.  Returns an expression
17093    representing the cast.  */
17094
17095 static tree
17096 cp_parser_functional_cast (cp_parser* parser, tree type)
17097 {
17098   tree expression_list;
17099   tree cast;
17100
17101   expression_list
17102     = cp_parser_parenthesized_expression_list (parser, false,
17103                                                /*cast_p=*/true,
17104                                                /*allow_expansion_p=*/true,
17105                                                /*non_constant_p=*/NULL);
17106
17107   cast = build_functional_cast (type, expression_list);
17108   /* [expr.const]/1: In an integral constant expression "only type
17109      conversions to integral or enumeration type can be used".  */
17110   if (TREE_CODE (type) == TYPE_DECL)
17111     type = TREE_TYPE (type);
17112   if (cast != error_mark_node
17113       && !cast_valid_in_integral_constant_expression_p (type)
17114       && (cp_parser_non_integral_constant_expression
17115           (parser, "a call to a constructor")))
17116     return error_mark_node;
17117   return cast;
17118 }
17119
17120 /* Save the tokens that make up the body of a member function defined
17121    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17122    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17123    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17124    for the member function.  */
17125
17126 static tree
17127 cp_parser_save_member_function_body (cp_parser* parser,
17128                                      cp_decl_specifier_seq *decl_specifiers,
17129                                      cp_declarator *declarator,
17130                                      tree attributes)
17131 {
17132   cp_token *first;
17133   cp_token *last;
17134   tree fn;
17135
17136   /* Create the function-declaration.  */
17137   fn = start_method (decl_specifiers, declarator, attributes);
17138   /* If something went badly wrong, bail out now.  */
17139   if (fn == error_mark_node)
17140     {
17141       /* If there's a function-body, skip it.  */
17142       if (cp_parser_token_starts_function_definition_p
17143           (cp_lexer_peek_token (parser->lexer)))
17144         cp_parser_skip_to_end_of_block_or_statement (parser);
17145       return error_mark_node;
17146     }
17147
17148   /* Remember it, if there default args to post process.  */
17149   cp_parser_save_default_args (parser, fn);
17150
17151   /* Save away the tokens that make up the body of the
17152      function.  */
17153   first = parser->lexer->next_token;
17154   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17155   /* Handle function try blocks.  */
17156   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17157     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17158   last = parser->lexer->next_token;
17159
17160   /* Save away the inline definition; we will process it when the
17161      class is complete.  */
17162   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17163   DECL_PENDING_INLINE_P (fn) = 1;
17164
17165   /* We need to know that this was defined in the class, so that
17166      friend templates are handled correctly.  */
17167   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17168
17169   /* We're done with the inline definition.  */
17170   finish_method (fn);
17171
17172   /* Add FN to the queue of functions to be parsed later.  */
17173   TREE_VALUE (parser->unparsed_functions_queues)
17174     = tree_cons (NULL_TREE, fn,
17175                  TREE_VALUE (parser->unparsed_functions_queues));
17176
17177   return fn;
17178 }
17179
17180 /* Parse a template-argument-list, as well as the trailing ">" (but
17181    not the opening ">").  See cp_parser_template_argument_list for the
17182    return value.  */
17183
17184 static tree
17185 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17186 {
17187   tree arguments;
17188   tree saved_scope;
17189   tree saved_qualifying_scope;
17190   tree saved_object_scope;
17191   bool saved_greater_than_is_operator_p;
17192   bool saved_skip_evaluation;
17193
17194   /* [temp.names]
17195
17196      When parsing a template-id, the first non-nested `>' is taken as
17197      the end of the template-argument-list rather than a greater-than
17198      operator.  */
17199   saved_greater_than_is_operator_p
17200     = parser->greater_than_is_operator_p;
17201   parser->greater_than_is_operator_p = false;
17202   /* Parsing the argument list may modify SCOPE, so we save it
17203      here.  */
17204   saved_scope = parser->scope;
17205   saved_qualifying_scope = parser->qualifying_scope;
17206   saved_object_scope = parser->object_scope;
17207   /* We need to evaluate the template arguments, even though this
17208      template-id may be nested within a "sizeof".  */
17209   saved_skip_evaluation = skip_evaluation;
17210   skip_evaluation = false;
17211   /* Parse the template-argument-list itself.  */
17212   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17213       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17214     arguments = NULL_TREE;
17215   else
17216     arguments = cp_parser_template_argument_list (parser);
17217   /* Look for the `>' that ends the template-argument-list. If we find
17218      a '>>' instead, it's probably just a typo.  */
17219   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17220     {
17221       if (cxx_dialect != cxx98)
17222         {
17223           /* In C++0x, a `>>' in a template argument list or cast
17224              expression is considered to be two separate `>'
17225              tokens. So, change the current token to a `>', but don't
17226              consume it: it will be consumed later when the outer
17227              template argument list (or cast expression) is parsed.
17228              Note that this replacement of `>' for `>>' is necessary
17229              even if we are parsing tentatively: in the tentative
17230              case, after calling
17231              cp_parser_enclosed_template_argument_list we will always
17232              throw away all of the template arguments and the first
17233              closing `>', either because the template argument list
17234              was erroneous or because we are replacing those tokens
17235              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17236              not have been thrown away) is needed either to close an
17237              outer template argument list or to complete a new-style
17238              cast.  */
17239           cp_token *token = cp_lexer_peek_token (parser->lexer);
17240           token->type = CPP_GREATER;
17241         }
17242       else if (!saved_greater_than_is_operator_p)
17243         {
17244           /* If we're in a nested template argument list, the '>>' has
17245             to be a typo for '> >'. We emit the error message, but we
17246             continue parsing and we push a '>' as next token, so that
17247             the argument list will be parsed correctly.  Note that the
17248             global source location is still on the token before the
17249             '>>', so we need to say explicitly where we want it.  */
17250           cp_token *token = cp_lexer_peek_token (parser->lexer);
17251           error ("%H%<>>%> should be %<> >%> "
17252                  "within a nested template argument list",
17253                  &token->location);
17254
17255           token->type = CPP_GREATER;
17256         }
17257       else
17258         {
17259           /* If this is not a nested template argument list, the '>>'
17260             is a typo for '>'. Emit an error message and continue.
17261             Same deal about the token location, but here we can get it
17262             right by consuming the '>>' before issuing the diagnostic.  */
17263           cp_lexer_consume_token (parser->lexer);
17264           error ("spurious %<>>%>, use %<>%> to terminate "
17265                  "a template argument list");
17266         }
17267     }
17268   else
17269     cp_parser_skip_to_end_of_template_parameter_list (parser);
17270   /* The `>' token might be a greater-than operator again now.  */
17271   parser->greater_than_is_operator_p
17272     = saved_greater_than_is_operator_p;
17273   /* Restore the SAVED_SCOPE.  */
17274   parser->scope = saved_scope;
17275   parser->qualifying_scope = saved_qualifying_scope;
17276   parser->object_scope = saved_object_scope;
17277   skip_evaluation = saved_skip_evaluation;
17278
17279   return arguments;
17280 }
17281
17282 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17283    arguments, or the body of the function have not yet been parsed,
17284    parse them now.  */
17285
17286 static void
17287 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17288 {
17289   /* If this member is a template, get the underlying
17290      FUNCTION_DECL.  */
17291   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17292     member_function = DECL_TEMPLATE_RESULT (member_function);
17293
17294   /* There should not be any class definitions in progress at this
17295      point; the bodies of members are only parsed outside of all class
17296      definitions.  */
17297   gcc_assert (parser->num_classes_being_defined == 0);
17298   /* While we're parsing the member functions we might encounter more
17299      classes.  We want to handle them right away, but we don't want
17300      them getting mixed up with functions that are currently in the
17301      queue.  */
17302   parser->unparsed_functions_queues
17303     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17304
17305   /* Make sure that any template parameters are in scope.  */
17306   maybe_begin_member_template_processing (member_function);
17307
17308   /* If the body of the function has not yet been parsed, parse it
17309      now.  */
17310   if (DECL_PENDING_INLINE_P (member_function))
17311     {
17312       tree function_scope;
17313       cp_token_cache *tokens;
17314
17315       /* The function is no longer pending; we are processing it.  */
17316       tokens = DECL_PENDING_INLINE_INFO (member_function);
17317       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17318       DECL_PENDING_INLINE_P (member_function) = 0;
17319
17320       /* If this is a local class, enter the scope of the containing
17321          function.  */
17322       function_scope = current_function_decl;
17323       if (function_scope)
17324         push_function_context_to (function_scope);
17325
17326
17327       /* Push the body of the function onto the lexer stack.  */
17328       cp_parser_push_lexer_for_tokens (parser, tokens);
17329
17330       /* Let the front end know that we going to be defining this
17331          function.  */
17332       start_preparsed_function (member_function, NULL_TREE,
17333                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17334
17335       /* Don't do access checking if it is a templated function.  */
17336       if (processing_template_decl)
17337         push_deferring_access_checks (dk_no_check);
17338
17339       /* Now, parse the body of the function.  */
17340       cp_parser_function_definition_after_declarator (parser,
17341                                                       /*inline_p=*/true);
17342
17343       if (processing_template_decl)
17344         pop_deferring_access_checks ();
17345
17346       /* Leave the scope of the containing function.  */
17347       if (function_scope)
17348         pop_function_context_from (function_scope);
17349       cp_parser_pop_lexer (parser);
17350     }
17351
17352   /* Remove any template parameters from the symbol table.  */
17353   maybe_end_member_template_processing ();
17354
17355   /* Restore the queue.  */
17356   parser->unparsed_functions_queues
17357     = TREE_CHAIN (parser->unparsed_functions_queues);
17358 }
17359
17360 /* If DECL contains any default args, remember it on the unparsed
17361    functions queue.  */
17362
17363 static void
17364 cp_parser_save_default_args (cp_parser* parser, tree decl)
17365 {
17366   tree probe;
17367
17368   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17369        probe;
17370        probe = TREE_CHAIN (probe))
17371     if (TREE_PURPOSE (probe))
17372       {
17373         TREE_PURPOSE (parser->unparsed_functions_queues)
17374           = tree_cons (current_class_type, decl,
17375                        TREE_PURPOSE (parser->unparsed_functions_queues));
17376         break;
17377       }
17378 }
17379
17380 /* FN is a FUNCTION_DECL which may contains a parameter with an
17381    unparsed DEFAULT_ARG.  Parse the default args now.  This function
17382    assumes that the current scope is the scope in which the default
17383    argument should be processed.  */
17384
17385 static void
17386 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17387 {
17388   bool saved_local_variables_forbidden_p;
17389   tree parm;
17390
17391   /* While we're parsing the default args, we might (due to the
17392      statement expression extension) encounter more classes.  We want
17393      to handle them right away, but we don't want them getting mixed
17394      up with default args that are currently in the queue.  */
17395   parser->unparsed_functions_queues
17396     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17397
17398   /* Local variable names (and the `this' keyword) may not appear
17399      in a default argument.  */
17400   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17401   parser->local_variables_forbidden_p = true;
17402
17403   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17404        parm;
17405        parm = TREE_CHAIN (parm))
17406     {
17407       cp_token_cache *tokens;
17408       tree default_arg = TREE_PURPOSE (parm);
17409       tree parsed_arg;
17410       VEC(tree,gc) *insts;
17411       tree copy;
17412       unsigned ix;
17413
17414       if (!default_arg)
17415         continue;
17416
17417       if (TREE_CODE (default_arg) != DEFAULT_ARG)
17418         /* This can happen for a friend declaration for a function
17419            already declared with default arguments.  */
17420         continue;
17421
17422        /* Push the saved tokens for the default argument onto the parser's
17423           lexer stack.  */
17424       tokens = DEFARG_TOKENS (default_arg);
17425       cp_parser_push_lexer_for_tokens (parser, tokens);
17426
17427       /* Parse the assignment-expression.  */
17428       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17429
17430       if (!processing_template_decl)
17431         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17432
17433       TREE_PURPOSE (parm) = parsed_arg;
17434
17435       /* Update any instantiations we've already created.  */
17436       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17437            VEC_iterate (tree, insts, ix, copy); ix++)
17438         TREE_PURPOSE (copy) = parsed_arg;
17439
17440       /* If the token stream has not been completely used up, then
17441          there was extra junk after the end of the default
17442          argument.  */
17443       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17444         cp_parser_error (parser, "expected %<,%>");
17445
17446       /* Revert to the main lexer.  */
17447       cp_parser_pop_lexer (parser);
17448     }
17449
17450   /* Make sure no default arg is missing.  */
17451   check_default_args (fn);
17452
17453   /* Restore the state of local_variables_forbidden_p.  */
17454   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17455
17456   /* Restore the queue.  */
17457   parser->unparsed_functions_queues
17458     = TREE_CHAIN (parser->unparsed_functions_queues);
17459 }
17460
17461 /* Parse the operand of `sizeof' (or a similar operator).  Returns
17462    either a TYPE or an expression, depending on the form of the
17463    input.  The KEYWORD indicates which kind of expression we have
17464    encountered.  */
17465
17466 static tree
17467 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17468 {
17469   static const char *format;
17470   tree expr = NULL_TREE;
17471   const char *saved_message;
17472   char *tmp;
17473   bool saved_integral_constant_expression_p;
17474   bool saved_non_integral_constant_expression_p;
17475   bool pack_expansion_p = false;
17476
17477   /* Initialize FORMAT the first time we get here.  */
17478   if (!format)
17479     format = "types may not be defined in '%s' expressions";
17480
17481   /* Types cannot be defined in a `sizeof' expression.  Save away the
17482      old message.  */
17483   saved_message = parser->type_definition_forbidden_message;
17484   /* And create the new one.  */
17485   parser->type_definition_forbidden_message = tmp
17486     = XNEWVEC (char, strlen (format)
17487                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17488                + 1 /* `\0' */);
17489   sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17490
17491   /* The restrictions on constant-expressions do not apply inside
17492      sizeof expressions.  */
17493   saved_integral_constant_expression_p
17494     = parser->integral_constant_expression_p;
17495   saved_non_integral_constant_expression_p
17496     = parser->non_integral_constant_expression_p;
17497   parser->integral_constant_expression_p = false;
17498
17499   /* If it's a `...', then we are computing the length of a parameter
17500      pack.  */
17501   if (keyword == RID_SIZEOF
17502       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17503     {
17504       /* Consume the `...'.  */
17505       cp_lexer_consume_token (parser->lexer);
17506       maybe_warn_variadic_templates ();
17507
17508       /* Note that this is an expansion.  */
17509       pack_expansion_p = true;
17510     }
17511
17512   /* Do not actually evaluate the expression.  */
17513   ++skip_evaluation;
17514   /* If it's a `(', then we might be looking at the type-id
17515      construction.  */
17516   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17517     {
17518       tree type;
17519       bool saved_in_type_id_in_expr_p;
17520
17521       /* We can't be sure yet whether we're looking at a type-id or an
17522          expression.  */
17523       cp_parser_parse_tentatively (parser);
17524       /* Consume the `('.  */
17525       cp_lexer_consume_token (parser->lexer);
17526       /* Parse the type-id.  */
17527       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17528       parser->in_type_id_in_expr_p = true;
17529       type = cp_parser_type_id (parser);
17530       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17531       /* Now, look for the trailing `)'.  */
17532       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17533       /* If all went well, then we're done.  */
17534       if (cp_parser_parse_definitely (parser))
17535         {
17536           cp_decl_specifier_seq decl_specs;
17537
17538           /* Build a trivial decl-specifier-seq.  */
17539           clear_decl_specs (&decl_specs);
17540           decl_specs.type = type;
17541
17542           /* Call grokdeclarator to figure out what type this is.  */
17543           expr = grokdeclarator (NULL,
17544                                  &decl_specs,
17545                                  TYPENAME,
17546                                  /*initialized=*/0,
17547                                  /*attrlist=*/NULL);
17548         }
17549     }
17550
17551   /* If the type-id production did not work out, then we must be
17552      looking at the unary-expression production.  */
17553   if (!expr)
17554     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17555                                        /*cast_p=*/false);
17556
17557   if (pack_expansion_p)
17558     /* Build a pack expansion. */
17559     expr = make_pack_expansion (expr);
17560
17561   /* Go back to evaluating expressions.  */
17562   --skip_evaluation;
17563
17564   /* Free the message we created.  */
17565   free (tmp);
17566   /* And restore the old one.  */
17567   parser->type_definition_forbidden_message = saved_message;
17568   parser->integral_constant_expression_p
17569     = saved_integral_constant_expression_p;
17570   parser->non_integral_constant_expression_p
17571     = saved_non_integral_constant_expression_p;
17572
17573   return expr;
17574 }
17575
17576 /* If the current declaration has no declarator, return true.  */
17577
17578 static bool
17579 cp_parser_declares_only_class_p (cp_parser *parser)
17580 {
17581   /* If the next token is a `;' or a `,' then there is no
17582      declarator.  */
17583   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17584           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17585 }
17586
17587 /* Update the DECL_SPECS to reflect the storage class indicated by
17588    KEYWORD.  */
17589
17590 static void
17591 cp_parser_set_storage_class (cp_parser *parser,
17592                              cp_decl_specifier_seq *decl_specs,
17593                              enum rid keyword)
17594 {
17595   cp_storage_class storage_class;
17596
17597   if (parser->in_unbraced_linkage_specification_p)
17598     {
17599       error ("invalid use of %qD in linkage specification",
17600              ridpointers[keyword]);
17601       return;
17602     }
17603   else if (decl_specs->storage_class != sc_none)
17604     {
17605       decl_specs->conflicting_specifiers_p = true;
17606       return;
17607     }
17608
17609   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17610       && decl_specs->specs[(int) ds_thread])
17611     {
17612       error ("%<__thread%> before %qD", ridpointers[keyword]);
17613       decl_specs->specs[(int) ds_thread] = 0;
17614     }
17615
17616   switch (keyword)
17617     {
17618     case RID_AUTO:
17619       storage_class = sc_auto;
17620       break;
17621     case RID_REGISTER:
17622       storage_class = sc_register;
17623       break;
17624     case RID_STATIC:
17625       storage_class = sc_static;
17626       break;
17627     case RID_EXTERN:
17628       storage_class = sc_extern;
17629       break;
17630     case RID_MUTABLE:
17631       storage_class = sc_mutable;
17632       break;
17633     default:
17634       gcc_unreachable ();
17635     }
17636   decl_specs->storage_class = storage_class;
17637
17638   /* A storage class specifier cannot be applied alongside a typedef 
17639      specifier. If there is a typedef specifier present then set 
17640      conflicting_specifiers_p which will trigger an error later
17641      on in grokdeclarator. */
17642   if (decl_specs->specs[(int)ds_typedef])
17643     decl_specs->conflicting_specifiers_p = true;
17644 }
17645
17646 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
17647    is true, the type is a user-defined type; otherwise it is a
17648    built-in type specified by a keyword.  */
17649
17650 static void
17651 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17652                               tree type_spec,
17653                               bool user_defined_p)
17654 {
17655   decl_specs->any_specifiers_p = true;
17656
17657   /* If the user tries to redeclare bool or wchar_t (with, for
17658      example, in "typedef int wchar_t;") we remember that this is what
17659      happened.  In system headers, we ignore these declarations so
17660      that G++ can work with system headers that are not C++-safe.  */
17661   if (decl_specs->specs[(int) ds_typedef]
17662       && !user_defined_p
17663       && (type_spec == boolean_type_node
17664           || type_spec == wchar_type_node)
17665       && (decl_specs->type
17666           || decl_specs->specs[(int) ds_long]
17667           || decl_specs->specs[(int) ds_short]
17668           || decl_specs->specs[(int) ds_unsigned]
17669           || decl_specs->specs[(int) ds_signed]))
17670     {
17671       decl_specs->redefined_builtin_type = type_spec;
17672       if (!decl_specs->type)
17673         {
17674           decl_specs->type = type_spec;
17675           decl_specs->user_defined_type_p = false;
17676         }
17677     }
17678   else if (decl_specs->type)
17679     decl_specs->multiple_types_p = true;
17680   else
17681     {
17682       decl_specs->type = type_spec;
17683       decl_specs->user_defined_type_p = user_defined_p;
17684       decl_specs->redefined_builtin_type = NULL_TREE;
17685     }
17686 }
17687
17688 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17689    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
17690
17691 static bool
17692 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17693 {
17694   return decl_specifiers->specs[(int) ds_friend] != 0;
17695 }
17696
17697 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
17698    issue an error message indicating that TOKEN_DESC was expected.
17699
17700    Returns the token consumed, if the token had the appropriate type.
17701    Otherwise, returns NULL.  */
17702
17703 static cp_token *
17704 cp_parser_require (cp_parser* parser,
17705                    enum cpp_ttype type,
17706                    const char* token_desc)
17707 {
17708   if (cp_lexer_next_token_is (parser->lexer, type))
17709     return cp_lexer_consume_token (parser->lexer);
17710   else
17711     {
17712       /* Output the MESSAGE -- unless we're parsing tentatively.  */
17713       if (!cp_parser_simulate_error (parser))
17714         {
17715           char *message = concat ("expected ", token_desc, NULL);
17716           cp_parser_error (parser, message);
17717           free (message);
17718         }
17719       return NULL;
17720     }
17721 }
17722
17723 /* An error message is produced if the next token is not '>'.
17724    All further tokens are skipped until the desired token is
17725    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
17726
17727 static void
17728 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17729 {
17730   /* Current level of '< ... >'.  */
17731   unsigned level = 0;
17732   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
17733   unsigned nesting_depth = 0;
17734
17735   /* Are we ready, yet?  If not, issue error message.  */
17736   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17737     return;
17738
17739   /* Skip tokens until the desired token is found.  */
17740   while (true)
17741     {
17742       /* Peek at the next token.  */
17743       switch (cp_lexer_peek_token (parser->lexer)->type)
17744         {
17745         case CPP_LESS:
17746           if (!nesting_depth)
17747             ++level;
17748           break;
17749
17750         case CPP_RSHIFT:
17751           if (cxx_dialect == cxx98)
17752             /* C++0x views the `>>' operator as two `>' tokens, but
17753                C++98 does not. */
17754             break;
17755           else if (!nesting_depth && level-- == 0)
17756             {
17757               /* We've hit a `>>' where the first `>' closes the
17758                  template argument list, and the second `>' is
17759                  spurious.  Just consume the `>>' and stop; we've
17760                  already produced at least one error.  */
17761               cp_lexer_consume_token (parser->lexer);
17762               return;
17763             }
17764           /* Fall through for C++0x, so we handle the second `>' in
17765              the `>>'.  */
17766
17767         case CPP_GREATER:
17768           if (!nesting_depth && level-- == 0)
17769             {
17770               /* We've reached the token we want, consume it and stop.  */
17771               cp_lexer_consume_token (parser->lexer);
17772               return;
17773             }
17774           break;
17775
17776         case CPP_OPEN_PAREN:
17777         case CPP_OPEN_SQUARE:
17778           ++nesting_depth;
17779           break;
17780
17781         case CPP_CLOSE_PAREN:
17782         case CPP_CLOSE_SQUARE:
17783           if (nesting_depth-- == 0)
17784             return;
17785           break;
17786
17787         case CPP_EOF:
17788         case CPP_PRAGMA_EOL:
17789         case CPP_SEMICOLON:
17790         case CPP_OPEN_BRACE:
17791         case CPP_CLOSE_BRACE:
17792           /* The '>' was probably forgotten, don't look further.  */
17793           return;
17794
17795         default:
17796           break;
17797         }
17798
17799       /* Consume this token.  */
17800       cp_lexer_consume_token (parser->lexer);
17801     }
17802 }
17803
17804 /* If the next token is the indicated keyword, consume it.  Otherwise,
17805    issue an error message indicating that TOKEN_DESC was expected.
17806
17807    Returns the token consumed, if the token had the appropriate type.
17808    Otherwise, returns NULL.  */
17809
17810 static cp_token *
17811 cp_parser_require_keyword (cp_parser* parser,
17812                            enum rid keyword,
17813                            const char* token_desc)
17814 {
17815   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17816
17817   if (token && token->keyword != keyword)
17818     {
17819       dyn_string_t error_msg;
17820
17821       /* Format the error message.  */
17822       error_msg = dyn_string_new (0);
17823       dyn_string_append_cstr (error_msg, "expected ");
17824       dyn_string_append_cstr (error_msg, token_desc);
17825       cp_parser_error (parser, error_msg->s);
17826       dyn_string_delete (error_msg);
17827       return NULL;
17828     }
17829
17830   return token;
17831 }
17832
17833 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17834    function-definition.  */
17835
17836 static bool
17837 cp_parser_token_starts_function_definition_p (cp_token* token)
17838 {
17839   return (/* An ordinary function-body begins with an `{'.  */
17840           token->type == CPP_OPEN_BRACE
17841           /* A ctor-initializer begins with a `:'.  */
17842           || token->type == CPP_COLON
17843           /* A function-try-block begins with `try'.  */
17844           || token->keyword == RID_TRY
17845           /* The named return value extension begins with `return'.  */
17846           || token->keyword == RID_RETURN);
17847 }
17848
17849 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17850    definition.  */
17851
17852 static bool
17853 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17854 {
17855   cp_token *token;
17856
17857   token = cp_lexer_peek_token (parser->lexer);
17858   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17859 }
17860
17861 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17862    C++0x) ending a template-argument.  */
17863
17864 static bool
17865 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17866 {
17867   cp_token *token;
17868
17869   token = cp_lexer_peek_token (parser->lexer);
17870   return (token->type == CPP_COMMA 
17871           || token->type == CPP_GREATER
17872           || token->type == CPP_ELLIPSIS
17873           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17874 }
17875
17876 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17877    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
17878
17879 static bool
17880 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17881                                                      size_t n)
17882 {
17883   cp_token *token;
17884
17885   token = cp_lexer_peek_nth_token (parser->lexer, n);
17886   if (token->type == CPP_LESS)
17887     return true;
17888   /* Check for the sequence `<::' in the original code. It would be lexed as
17889      `[:', where `[' is a digraph, and there is no whitespace before
17890      `:'.  */
17891   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17892     {
17893       cp_token *token2;
17894       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17895       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17896         return true;
17897     }
17898   return false;
17899 }
17900
17901 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17902    or none_type otherwise.  */
17903
17904 static enum tag_types
17905 cp_parser_token_is_class_key (cp_token* token)
17906 {
17907   switch (token->keyword)
17908     {
17909     case RID_CLASS:
17910       return class_type;
17911     case RID_STRUCT:
17912       return record_type;
17913     case RID_UNION:
17914       return union_type;
17915
17916     default:
17917       return none_type;
17918     }
17919 }
17920
17921 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
17922
17923 static void
17924 cp_parser_check_class_key (enum tag_types class_key, tree type)
17925 {
17926   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17927     pedwarn ("%qs tag used in naming %q#T",
17928             class_key == union_type ? "union"
17929              : class_key == record_type ? "struct" : "class",
17930              type);
17931 }
17932
17933 /* Issue an error message if DECL is redeclared with different
17934    access than its original declaration [class.access.spec/3].
17935    This applies to nested classes and nested class templates.
17936    [class.mem/1].  */
17937
17938 static void
17939 cp_parser_check_access_in_redeclaration (tree decl)
17940 {
17941   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
17942     return;
17943
17944   if ((TREE_PRIVATE (decl)
17945        != (current_access_specifier == access_private_node))
17946       || (TREE_PROTECTED (decl)
17947           != (current_access_specifier == access_protected_node)))
17948     error ("%qD redeclared with different access", decl);
17949 }
17950
17951 /* Look for the `template' keyword, as a syntactic disambiguator.
17952    Return TRUE iff it is present, in which case it will be
17953    consumed.  */
17954
17955 static bool
17956 cp_parser_optional_template_keyword (cp_parser *parser)
17957 {
17958   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17959     {
17960       /* The `template' keyword can only be used within templates;
17961          outside templates the parser can always figure out what is a
17962          template and what is not.  */
17963       if (!processing_template_decl)
17964         {
17965           error ("%<template%> (as a disambiguator) is only allowed "
17966                  "within templates");
17967           /* If this part of the token stream is rescanned, the same
17968              error message would be generated.  So, we purge the token
17969              from the stream.  */
17970           cp_lexer_purge_token (parser->lexer);
17971           return false;
17972         }
17973       else
17974         {
17975           /* Consume the `template' keyword.  */
17976           cp_lexer_consume_token (parser->lexer);
17977           return true;
17978         }
17979     }
17980
17981   return false;
17982 }
17983
17984 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
17985    set PARSER->SCOPE, and perform other related actions.  */
17986
17987 static void
17988 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17989 {
17990   int i;
17991   struct tree_check *check_value;
17992   deferred_access_check *chk;
17993   VEC (deferred_access_check,gc) *checks;
17994
17995   /* Get the stored value.  */
17996   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17997   /* Perform any access checks that were deferred.  */
17998   checks = check_value->checks;
17999   if (checks)
18000     {
18001       for (i = 0 ;
18002            VEC_iterate (deferred_access_check, checks, i, chk) ;
18003            ++i)
18004         {
18005           perform_or_defer_access_check (chk->binfo,
18006                                          chk->decl,
18007                                          chk->diag_decl);
18008         }
18009     }
18010   /* Set the scope from the stored value.  */
18011   parser->scope = check_value->value;
18012   parser->qualifying_scope = check_value->qualifying_scope;
18013   parser->object_scope = NULL_TREE;
18014 }
18015
18016 /* Consume tokens up through a non-nested END token.  */
18017
18018 static void
18019 cp_parser_cache_group (cp_parser *parser,
18020                        enum cpp_ttype end,
18021                        unsigned depth)
18022 {
18023   while (true)
18024     {
18025       cp_token *token;
18026
18027       /* Abort a parenthesized expression if we encounter a brace.  */
18028       if ((end == CPP_CLOSE_PAREN || depth == 0)
18029           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18030         return;
18031       /* If we've reached the end of the file, stop.  */
18032       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
18033           || (end != CPP_PRAGMA_EOL
18034               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
18035         return;
18036       /* Consume the next token.  */
18037       token = cp_lexer_consume_token (parser->lexer);
18038       /* See if it starts a new group.  */
18039       if (token->type == CPP_OPEN_BRACE)
18040         {
18041           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18042           if (depth == 0)
18043             return;
18044         }
18045       else if (token->type == CPP_OPEN_PAREN)
18046         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18047       else if (token->type == CPP_PRAGMA)
18048         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18049       else if (token->type == end)
18050         return;
18051     }
18052 }
18053
18054 /* Begin parsing tentatively.  We always save tokens while parsing
18055    tentatively so that if the tentative parsing fails we can restore the
18056    tokens.  */
18057
18058 static void
18059 cp_parser_parse_tentatively (cp_parser* parser)
18060 {
18061   /* Enter a new parsing context.  */
18062   parser->context = cp_parser_context_new (parser->context);
18063   /* Begin saving tokens.  */
18064   cp_lexer_save_tokens (parser->lexer);
18065   /* In order to avoid repetitive access control error messages,
18066      access checks are queued up until we are no longer parsing
18067      tentatively.  */
18068   push_deferring_access_checks (dk_deferred);
18069 }
18070
18071 /* Commit to the currently active tentative parse.  */
18072
18073 static void
18074 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18075 {
18076   cp_parser_context *context;
18077   cp_lexer *lexer;
18078
18079   /* Mark all of the levels as committed.  */
18080   lexer = parser->lexer;
18081   for (context = parser->context; context->next; context = context->next)
18082     {
18083       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18084         break;
18085       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18086       while (!cp_lexer_saving_tokens (lexer))
18087         lexer = lexer->next;
18088       cp_lexer_commit_tokens (lexer);
18089     }
18090 }
18091
18092 /* Abort the currently active tentative parse.  All consumed tokens
18093    will be rolled back, and no diagnostics will be issued.  */
18094
18095 static void
18096 cp_parser_abort_tentative_parse (cp_parser* parser)
18097 {
18098   cp_parser_simulate_error (parser);
18099   /* Now, pretend that we want to see if the construct was
18100      successfully parsed.  */
18101   cp_parser_parse_definitely (parser);
18102 }
18103
18104 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18105    token stream.  Otherwise, commit to the tokens we have consumed.
18106    Returns true if no error occurred; false otherwise.  */
18107
18108 static bool
18109 cp_parser_parse_definitely (cp_parser* parser)
18110 {
18111   bool error_occurred;
18112   cp_parser_context *context;
18113
18114   /* Remember whether or not an error occurred, since we are about to
18115      destroy that information.  */
18116   error_occurred = cp_parser_error_occurred (parser);
18117   /* Remove the topmost context from the stack.  */
18118   context = parser->context;
18119   parser->context = context->next;
18120   /* If no parse errors occurred, commit to the tentative parse.  */
18121   if (!error_occurred)
18122     {
18123       /* Commit to the tokens read tentatively, unless that was
18124          already done.  */
18125       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18126         cp_lexer_commit_tokens (parser->lexer);
18127
18128       pop_to_parent_deferring_access_checks ();
18129     }
18130   /* Otherwise, if errors occurred, roll back our state so that things
18131      are just as they were before we began the tentative parse.  */
18132   else
18133     {
18134       cp_lexer_rollback_tokens (parser->lexer);
18135       pop_deferring_access_checks ();
18136     }
18137   /* Add the context to the front of the free list.  */
18138   context->next = cp_parser_context_free_list;
18139   cp_parser_context_free_list = context;
18140
18141   return !error_occurred;
18142 }
18143
18144 /* Returns true if we are parsing tentatively and are not committed to
18145    this tentative parse.  */
18146
18147 static bool
18148 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18149 {
18150   return (cp_parser_parsing_tentatively (parser)
18151           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18152 }
18153
18154 /* Returns nonzero iff an error has occurred during the most recent
18155    tentative parse.  */
18156
18157 static bool
18158 cp_parser_error_occurred (cp_parser* parser)
18159 {
18160   return (cp_parser_parsing_tentatively (parser)
18161           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18162 }
18163
18164 /* Returns nonzero if GNU extensions are allowed.  */
18165
18166 static bool
18167 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18168 {
18169   return parser->allow_gnu_extensions_p;
18170 }
18171 \f
18172 /* Objective-C++ Productions */
18173
18174
18175 /* Parse an Objective-C expression, which feeds into a primary-expression
18176    above.
18177
18178    objc-expression:
18179      objc-message-expression
18180      objc-string-literal
18181      objc-encode-expression
18182      objc-protocol-expression
18183      objc-selector-expression
18184
18185   Returns a tree representation of the expression.  */
18186
18187 static tree
18188 cp_parser_objc_expression (cp_parser* parser)
18189 {
18190   /* Try to figure out what kind of declaration is present.  */
18191   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18192
18193   switch (kwd->type)
18194     {
18195     case CPP_OPEN_SQUARE:
18196       return cp_parser_objc_message_expression (parser);
18197
18198     case CPP_OBJC_STRING:
18199       kwd = cp_lexer_consume_token (parser->lexer);
18200       return objc_build_string_object (kwd->u.value);
18201
18202     case CPP_KEYWORD:
18203       switch (kwd->keyword)
18204         {
18205         case RID_AT_ENCODE:
18206           return cp_parser_objc_encode_expression (parser);
18207
18208         case RID_AT_PROTOCOL:
18209           return cp_parser_objc_protocol_expression (parser);
18210
18211         case RID_AT_SELECTOR:
18212           return cp_parser_objc_selector_expression (parser);
18213
18214         default:
18215           break;
18216         }
18217     default:
18218       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18219       cp_parser_skip_to_end_of_block_or_statement (parser);
18220     }
18221
18222   return error_mark_node;
18223 }
18224
18225 /* Parse an Objective-C message expression.
18226
18227    objc-message-expression:
18228      [ objc-message-receiver objc-message-args ]
18229
18230    Returns a representation of an Objective-C message.  */
18231
18232 static tree
18233 cp_parser_objc_message_expression (cp_parser* parser)
18234 {
18235   tree receiver, messageargs;
18236
18237   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18238   receiver = cp_parser_objc_message_receiver (parser);
18239   messageargs = cp_parser_objc_message_args (parser);
18240   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18241
18242   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18243 }
18244
18245 /* Parse an objc-message-receiver.
18246
18247    objc-message-receiver:
18248      expression
18249      simple-type-specifier
18250
18251   Returns a representation of the type or expression.  */
18252
18253 static tree
18254 cp_parser_objc_message_receiver (cp_parser* parser)
18255 {
18256   tree rcv;
18257
18258   /* An Objective-C message receiver may be either (1) a type
18259      or (2) an expression.  */
18260   cp_parser_parse_tentatively (parser);
18261   rcv = cp_parser_expression (parser, false);
18262
18263   if (cp_parser_parse_definitely (parser))
18264     return rcv;
18265
18266   rcv = cp_parser_simple_type_specifier (parser,
18267                                          /*decl_specs=*/NULL,
18268                                          CP_PARSER_FLAGS_NONE);
18269
18270   return objc_get_class_reference (rcv);
18271 }
18272
18273 /* Parse the arguments and selectors comprising an Objective-C message.
18274
18275    objc-message-args:
18276      objc-selector
18277      objc-selector-args
18278      objc-selector-args , objc-comma-args
18279
18280    objc-selector-args:
18281      objc-selector [opt] : assignment-expression
18282      objc-selector-args objc-selector [opt] : assignment-expression
18283
18284    objc-comma-args:
18285      assignment-expression
18286      objc-comma-args , assignment-expression
18287
18288    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18289    selector arguments and TREE_VALUE containing a list of comma
18290    arguments.  */
18291
18292 static tree
18293 cp_parser_objc_message_args (cp_parser* parser)
18294 {
18295   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18296   bool maybe_unary_selector_p = true;
18297   cp_token *token = cp_lexer_peek_token (parser->lexer);
18298
18299   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18300     {
18301       tree selector = NULL_TREE, arg;
18302
18303       if (token->type != CPP_COLON)
18304         selector = cp_parser_objc_selector (parser);
18305
18306       /* Detect if we have a unary selector.  */
18307       if (maybe_unary_selector_p
18308           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18309         return build_tree_list (selector, NULL_TREE);
18310
18311       maybe_unary_selector_p = false;
18312       cp_parser_require (parser, CPP_COLON, "`:'");
18313       arg = cp_parser_assignment_expression (parser, false);
18314
18315       sel_args
18316         = chainon (sel_args,
18317                    build_tree_list (selector, arg));
18318
18319       token = cp_lexer_peek_token (parser->lexer);
18320     }
18321
18322   /* Handle non-selector arguments, if any. */
18323   while (token->type == CPP_COMMA)
18324     {
18325       tree arg;
18326
18327       cp_lexer_consume_token (parser->lexer);
18328       arg = cp_parser_assignment_expression (parser, false);
18329
18330       addl_args
18331         = chainon (addl_args,
18332                    build_tree_list (NULL_TREE, arg));
18333
18334       token = cp_lexer_peek_token (parser->lexer);
18335     }
18336
18337   return build_tree_list (sel_args, addl_args);
18338 }
18339
18340 /* Parse an Objective-C encode expression.
18341
18342    objc-encode-expression:
18343      @encode objc-typename
18344
18345    Returns an encoded representation of the type argument.  */
18346
18347 static tree
18348 cp_parser_objc_encode_expression (cp_parser* parser)
18349 {
18350   tree type;
18351
18352   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
18353   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18354   type = complete_type (cp_parser_type_id (parser));
18355   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18356
18357   if (!type)
18358     {
18359       error ("%<@encode%> must specify a type as an argument");
18360       return error_mark_node;
18361     }
18362
18363   return objc_build_encode_expr (type);
18364 }
18365
18366 /* Parse an Objective-C @defs expression.  */
18367
18368 static tree
18369 cp_parser_objc_defs_expression (cp_parser *parser)
18370 {
18371   tree name;
18372
18373   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
18374   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18375   name = cp_parser_identifier (parser);
18376   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18377
18378   return objc_get_class_ivars (name);
18379 }
18380
18381 /* Parse an Objective-C protocol expression.
18382
18383   objc-protocol-expression:
18384     @protocol ( identifier )
18385
18386   Returns a representation of the protocol expression.  */
18387
18388 static tree
18389 cp_parser_objc_protocol_expression (cp_parser* parser)
18390 {
18391   tree proto;
18392
18393   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18394   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18395   proto = cp_parser_identifier (parser);
18396   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18397
18398   return objc_build_protocol_expr (proto);
18399 }
18400
18401 /* Parse an Objective-C selector expression.
18402
18403    objc-selector-expression:
18404      @selector ( objc-method-signature )
18405
18406    objc-method-signature:
18407      objc-selector
18408      objc-selector-seq
18409
18410    objc-selector-seq:
18411      objc-selector :
18412      objc-selector-seq objc-selector :
18413
18414   Returns a representation of the method selector.  */
18415
18416 static tree
18417 cp_parser_objc_selector_expression (cp_parser* parser)
18418 {
18419   tree sel_seq = NULL_TREE;
18420   bool maybe_unary_selector_p = true;
18421   cp_token *token;
18422
18423   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
18424   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18425   token = cp_lexer_peek_token (parser->lexer);
18426
18427   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18428          || token->type == CPP_SCOPE)
18429     {
18430       tree selector = NULL_TREE;
18431
18432       if (token->type != CPP_COLON
18433           || token->type == CPP_SCOPE)
18434         selector = cp_parser_objc_selector (parser);
18435
18436       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18437           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18438         {
18439           /* Detect if we have a unary selector.  */
18440           if (maybe_unary_selector_p)
18441             {
18442               sel_seq = selector;
18443               goto finish_selector;
18444             }
18445           else
18446             {
18447               cp_parser_error (parser, "expected %<:%>");
18448             }
18449         }
18450       maybe_unary_selector_p = false;
18451       token = cp_lexer_consume_token (parser->lexer);
18452
18453       if (token->type == CPP_SCOPE)
18454         {
18455           sel_seq
18456             = chainon (sel_seq,
18457                        build_tree_list (selector, NULL_TREE));
18458           sel_seq
18459             = chainon (sel_seq,
18460                        build_tree_list (NULL_TREE, NULL_TREE));
18461         }
18462       else
18463         sel_seq
18464           = chainon (sel_seq,
18465                      build_tree_list (selector, NULL_TREE));
18466
18467       token = cp_lexer_peek_token (parser->lexer);
18468     }
18469
18470  finish_selector:
18471   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18472
18473   return objc_build_selector_expr (sel_seq);
18474 }
18475
18476 /* Parse a list of identifiers.
18477
18478    objc-identifier-list:
18479      identifier
18480      objc-identifier-list , identifier
18481
18482    Returns a TREE_LIST of identifier nodes.  */
18483
18484 static tree
18485 cp_parser_objc_identifier_list (cp_parser* parser)
18486 {
18487   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18488   cp_token *sep = cp_lexer_peek_token (parser->lexer);
18489
18490   while (sep->type == CPP_COMMA)
18491     {
18492       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18493       list = chainon (list,
18494                       build_tree_list (NULL_TREE,
18495                                        cp_parser_identifier (parser)));
18496       sep = cp_lexer_peek_token (parser->lexer);
18497     }
18498
18499   return list;
18500 }
18501
18502 /* Parse an Objective-C alias declaration.
18503
18504    objc-alias-declaration:
18505      @compatibility_alias identifier identifier ;
18506
18507    This function registers the alias mapping with the Objective-C front end.
18508    It returns nothing.  */
18509
18510 static void
18511 cp_parser_objc_alias_declaration (cp_parser* parser)
18512 {
18513   tree alias, orig;
18514
18515   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
18516   alias = cp_parser_identifier (parser);
18517   orig = cp_parser_identifier (parser);
18518   objc_declare_alias (alias, orig);
18519   cp_parser_consume_semicolon_at_end_of_statement (parser);
18520 }
18521
18522 /* Parse an Objective-C class forward-declaration.
18523
18524    objc-class-declaration:
18525      @class objc-identifier-list ;
18526
18527    The function registers the forward declarations with the Objective-C
18528    front end.  It returns nothing.  */
18529
18530 static void
18531 cp_parser_objc_class_declaration (cp_parser* parser)
18532 {
18533   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
18534   objc_declare_class (cp_parser_objc_identifier_list (parser));
18535   cp_parser_consume_semicolon_at_end_of_statement (parser);
18536 }
18537
18538 /* Parse a list of Objective-C protocol references.
18539
18540    objc-protocol-refs-opt:
18541      objc-protocol-refs [opt]
18542
18543    objc-protocol-refs:
18544      < objc-identifier-list >
18545
18546    Returns a TREE_LIST of identifiers, if any.  */
18547
18548 static tree
18549 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18550 {
18551   tree protorefs = NULL_TREE;
18552
18553   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18554     {
18555       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
18556       protorefs = cp_parser_objc_identifier_list (parser);
18557       cp_parser_require (parser, CPP_GREATER, "`>'");
18558     }
18559
18560   return protorefs;
18561 }
18562
18563 /* Parse a Objective-C visibility specification.  */
18564
18565 static void
18566 cp_parser_objc_visibility_spec (cp_parser* parser)
18567 {
18568   cp_token *vis = cp_lexer_peek_token (parser->lexer);
18569
18570   switch (vis->keyword)
18571     {
18572     case RID_AT_PRIVATE:
18573       objc_set_visibility (2);
18574       break;
18575     case RID_AT_PROTECTED:
18576       objc_set_visibility (0);
18577       break;
18578     case RID_AT_PUBLIC:
18579       objc_set_visibility (1);
18580       break;
18581     default:
18582       return;
18583     }
18584
18585   /* Eat '@private'/'@protected'/'@public'.  */
18586   cp_lexer_consume_token (parser->lexer);
18587 }
18588
18589 /* Parse an Objective-C method type.  */
18590
18591 static void
18592 cp_parser_objc_method_type (cp_parser* parser)
18593 {
18594   objc_set_method_type
18595    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18596     ? PLUS_EXPR
18597     : MINUS_EXPR);
18598 }
18599
18600 /* Parse an Objective-C protocol qualifier.  */
18601
18602 static tree
18603 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18604 {
18605   tree quals = NULL_TREE, node;
18606   cp_token *token = cp_lexer_peek_token (parser->lexer);
18607
18608   node = token->u.value;
18609
18610   while (node && TREE_CODE (node) == IDENTIFIER_NODE
18611          && (node == ridpointers [(int) RID_IN]
18612              || node == ridpointers [(int) RID_OUT]
18613              || node == ridpointers [(int) RID_INOUT]
18614              || node == ridpointers [(int) RID_BYCOPY]
18615              || node == ridpointers [(int) RID_BYREF]
18616              || node == ridpointers [(int) RID_ONEWAY]))
18617     {
18618       quals = tree_cons (NULL_TREE, node, quals);
18619       cp_lexer_consume_token (parser->lexer);
18620       token = cp_lexer_peek_token (parser->lexer);
18621       node = token->u.value;
18622     }
18623
18624   return quals;
18625 }
18626
18627 /* Parse an Objective-C typename.  */
18628
18629 static tree
18630 cp_parser_objc_typename (cp_parser* parser)
18631 {
18632   tree typename = NULL_TREE;
18633
18634   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18635     {
18636       tree proto_quals, cp_type = NULL_TREE;
18637
18638       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18639       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18640
18641       /* An ObjC type name may consist of just protocol qualifiers, in which
18642          case the type shall default to 'id'.  */
18643       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18644         cp_type = cp_parser_type_id (parser);
18645
18646       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18647       typename = build_tree_list (proto_quals, cp_type);
18648     }
18649
18650   return typename;
18651 }
18652
18653 /* Check to see if TYPE refers to an Objective-C selector name.  */
18654
18655 static bool
18656 cp_parser_objc_selector_p (enum cpp_ttype type)
18657 {
18658   return (type == CPP_NAME || type == CPP_KEYWORD
18659           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18660           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18661           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18662           || type == CPP_XOR || type == CPP_XOR_EQ);
18663 }
18664
18665 /* Parse an Objective-C selector.  */
18666
18667 static tree
18668 cp_parser_objc_selector (cp_parser* parser)
18669 {
18670   cp_token *token = cp_lexer_consume_token (parser->lexer);
18671
18672   if (!cp_parser_objc_selector_p (token->type))
18673     {
18674       error ("invalid Objective-C++ selector name");
18675       return error_mark_node;
18676     }
18677
18678   /* C++ operator names are allowed to appear in ObjC selectors.  */
18679   switch (token->type)
18680     {
18681     case CPP_AND_AND: return get_identifier ("and");
18682     case CPP_AND_EQ: return get_identifier ("and_eq");
18683     case CPP_AND: return get_identifier ("bitand");
18684     case CPP_OR: return get_identifier ("bitor");
18685     case CPP_COMPL: return get_identifier ("compl");
18686     case CPP_NOT: return get_identifier ("not");
18687     case CPP_NOT_EQ: return get_identifier ("not_eq");
18688     case CPP_OR_OR: return get_identifier ("or");
18689     case CPP_OR_EQ: return get_identifier ("or_eq");
18690     case CPP_XOR: return get_identifier ("xor");
18691     case CPP_XOR_EQ: return get_identifier ("xor_eq");
18692     default: return token->u.value;
18693     }
18694 }
18695
18696 /* Parse an Objective-C params list.  */
18697
18698 static tree
18699 cp_parser_objc_method_keyword_params (cp_parser* parser)
18700 {
18701   tree params = NULL_TREE;
18702   bool maybe_unary_selector_p = true;
18703   cp_token *token = cp_lexer_peek_token (parser->lexer);
18704
18705   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18706     {
18707       tree selector = NULL_TREE, typename, identifier;
18708
18709       if (token->type != CPP_COLON)
18710         selector = cp_parser_objc_selector (parser);
18711
18712       /* Detect if we have a unary selector.  */
18713       if (maybe_unary_selector_p
18714           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18715         return selector;
18716
18717       maybe_unary_selector_p = false;
18718       cp_parser_require (parser, CPP_COLON, "`:'");
18719       typename = cp_parser_objc_typename (parser);
18720       identifier = cp_parser_identifier (parser);
18721
18722       params
18723         = chainon (params,
18724                    objc_build_keyword_decl (selector,
18725                                             typename,
18726                                             identifier));
18727
18728       token = cp_lexer_peek_token (parser->lexer);
18729     }
18730
18731   return params;
18732 }
18733
18734 /* Parse the non-keyword Objective-C params.  */
18735
18736 static tree
18737 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18738 {
18739   tree params = make_node (TREE_LIST);
18740   cp_token *token = cp_lexer_peek_token (parser->lexer);
18741   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
18742
18743   while (token->type == CPP_COMMA)
18744     {
18745       cp_parameter_declarator *parmdecl;
18746       tree parm;
18747
18748       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18749       token = cp_lexer_peek_token (parser->lexer);
18750
18751       if (token->type == CPP_ELLIPSIS)
18752         {
18753           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
18754           *ellipsisp = true;
18755           break;
18756         }
18757
18758       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18759       parm = grokdeclarator (parmdecl->declarator,
18760                              &parmdecl->decl_specifiers,
18761                              PARM, /*initialized=*/0,
18762                              /*attrlist=*/NULL);
18763
18764       chainon (params, build_tree_list (NULL_TREE, parm));
18765       token = cp_lexer_peek_token (parser->lexer);
18766     }
18767
18768   return params;
18769 }
18770
18771 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
18772
18773 static void
18774 cp_parser_objc_interstitial_code (cp_parser* parser)
18775 {
18776   cp_token *token = cp_lexer_peek_token (parser->lexer);
18777
18778   /* If the next token is `extern' and the following token is a string
18779      literal, then we have a linkage specification.  */
18780   if (token->keyword == RID_EXTERN
18781       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18782     cp_parser_linkage_specification (parser);
18783   /* Handle #pragma, if any.  */
18784   else if (token->type == CPP_PRAGMA)
18785     cp_parser_pragma (parser, pragma_external);
18786   /* Allow stray semicolons.  */
18787   else if (token->type == CPP_SEMICOLON)
18788     cp_lexer_consume_token (parser->lexer);
18789   /* Finally, try to parse a block-declaration, or a function-definition.  */
18790   else
18791     cp_parser_block_declaration (parser, /*statement_p=*/false);
18792 }
18793
18794 /* Parse a method signature.  */
18795
18796 static tree
18797 cp_parser_objc_method_signature (cp_parser* parser)
18798 {
18799   tree rettype, kwdparms, optparms;
18800   bool ellipsis = false;
18801
18802   cp_parser_objc_method_type (parser);
18803   rettype = cp_parser_objc_typename (parser);
18804   kwdparms = cp_parser_objc_method_keyword_params (parser);
18805   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18806
18807   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18808 }
18809
18810 /* Pars an Objective-C method prototype list.  */
18811
18812 static void
18813 cp_parser_objc_method_prototype_list (cp_parser* parser)
18814 {
18815   cp_token *token = cp_lexer_peek_token (parser->lexer);
18816
18817   while (token->keyword != RID_AT_END)
18818     {
18819       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18820         {
18821           objc_add_method_declaration
18822            (cp_parser_objc_method_signature (parser));
18823           cp_parser_consume_semicolon_at_end_of_statement (parser);
18824         }
18825       else
18826         /* Allow for interspersed non-ObjC++ code.  */
18827         cp_parser_objc_interstitial_code (parser);
18828
18829       token = cp_lexer_peek_token (parser->lexer);
18830     }
18831
18832   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18833   objc_finish_interface ();
18834 }
18835
18836 /* Parse an Objective-C method definition list.  */
18837
18838 static void
18839 cp_parser_objc_method_definition_list (cp_parser* parser)
18840 {
18841   cp_token *token = cp_lexer_peek_token (parser->lexer);
18842
18843   while (token->keyword != RID_AT_END)
18844     {
18845       tree meth;
18846
18847       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18848         {
18849           push_deferring_access_checks (dk_deferred);
18850           objc_start_method_definition
18851            (cp_parser_objc_method_signature (parser));
18852
18853           /* For historical reasons, we accept an optional semicolon.  */
18854           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18855             cp_lexer_consume_token (parser->lexer);
18856
18857           perform_deferred_access_checks ();
18858           stop_deferring_access_checks ();
18859           meth = cp_parser_function_definition_after_declarator (parser,
18860                                                                  false);
18861           pop_deferring_access_checks ();
18862           objc_finish_method_definition (meth);
18863         }
18864       else
18865         /* Allow for interspersed non-ObjC++ code.  */
18866         cp_parser_objc_interstitial_code (parser);
18867
18868       token = cp_lexer_peek_token (parser->lexer);
18869     }
18870
18871   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18872   objc_finish_implementation ();
18873 }
18874
18875 /* Parse Objective-C ivars.  */
18876
18877 static void
18878 cp_parser_objc_class_ivars (cp_parser* parser)
18879 {
18880   cp_token *token = cp_lexer_peek_token (parser->lexer);
18881
18882   if (token->type != CPP_OPEN_BRACE)
18883     return;     /* No ivars specified.  */
18884
18885   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
18886   token = cp_lexer_peek_token (parser->lexer);
18887
18888   while (token->type != CPP_CLOSE_BRACE)
18889     {
18890       cp_decl_specifier_seq declspecs;
18891       int decl_class_or_enum_p;
18892       tree prefix_attributes;
18893
18894       cp_parser_objc_visibility_spec (parser);
18895
18896       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18897         break;
18898
18899       cp_parser_decl_specifier_seq (parser,
18900                                     CP_PARSER_FLAGS_OPTIONAL,
18901                                     &declspecs,
18902                                     &decl_class_or_enum_p);
18903       prefix_attributes = declspecs.attributes;
18904       declspecs.attributes = NULL_TREE;
18905
18906       /* Keep going until we hit the `;' at the end of the
18907          declaration.  */
18908       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18909         {
18910           tree width = NULL_TREE, attributes, first_attribute, decl;
18911           cp_declarator *declarator = NULL;
18912           int ctor_dtor_or_conv_p;
18913
18914           /* Check for a (possibly unnamed) bitfield declaration.  */
18915           token = cp_lexer_peek_token (parser->lexer);
18916           if (token->type == CPP_COLON)
18917             goto eat_colon;
18918
18919           if (token->type == CPP_NAME
18920               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18921                   == CPP_COLON))
18922             {
18923               /* Get the name of the bitfield.  */
18924               declarator = make_id_declarator (NULL_TREE,
18925                                                cp_parser_identifier (parser),
18926                                                sfk_none);
18927
18928              eat_colon:
18929               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18930               /* Get the width of the bitfield.  */
18931               width
18932                 = cp_parser_constant_expression (parser,
18933                                                  /*allow_non_constant=*/false,
18934                                                  NULL);
18935             }
18936           else
18937             {
18938               /* Parse the declarator.  */
18939               declarator
18940                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18941                                         &ctor_dtor_or_conv_p,
18942                                         /*parenthesized_p=*/NULL,
18943                                         /*member_p=*/false);
18944             }
18945
18946           /* Look for attributes that apply to the ivar.  */
18947           attributes = cp_parser_attributes_opt (parser);
18948           /* Remember which attributes are prefix attributes and
18949              which are not.  */
18950           first_attribute = attributes;
18951           /* Combine the attributes.  */
18952           attributes = chainon (prefix_attributes, attributes);
18953
18954           if (width)
18955             {
18956               /* Create the bitfield declaration.  */
18957               decl = grokbitfield (declarator, &declspecs, width);
18958               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18959             }
18960           else
18961             decl = grokfield (declarator, &declspecs,
18962                               NULL_TREE, /*init_const_expr_p=*/false,
18963                               NULL_TREE, attributes);
18964
18965           /* Add the instance variable.  */
18966           objc_add_instance_variable (decl);
18967
18968           /* Reset PREFIX_ATTRIBUTES.  */
18969           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18970             attributes = TREE_CHAIN (attributes);
18971           if (attributes)
18972             TREE_CHAIN (attributes) = NULL_TREE;
18973
18974           token = cp_lexer_peek_token (parser->lexer);
18975
18976           if (token->type == CPP_COMMA)
18977             {
18978               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18979               continue;
18980             }
18981           break;
18982         }
18983
18984       cp_parser_consume_semicolon_at_end_of_statement (parser);
18985       token = cp_lexer_peek_token (parser->lexer);
18986     }
18987
18988   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
18989   /* For historical reasons, we accept an optional semicolon.  */
18990   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18991     cp_lexer_consume_token (parser->lexer);
18992 }
18993
18994 /* Parse an Objective-C protocol declaration.  */
18995
18996 static void
18997 cp_parser_objc_protocol_declaration (cp_parser* parser)
18998 {
18999   tree proto, protorefs;
19000   cp_token *tok;
19001
19002   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19003   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19004     {
19005       error ("identifier expected after %<@protocol%>");
19006       goto finish;
19007     }
19008
19009   /* See if we have a forward declaration or a definition.  */
19010   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19011
19012   /* Try a forward declaration first.  */
19013   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19014     {
19015       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19016      finish:
19017       cp_parser_consume_semicolon_at_end_of_statement (parser);
19018     }
19019
19020   /* Ok, we got a full-fledged definition (or at least should).  */
19021   else
19022     {
19023       proto = cp_parser_identifier (parser);
19024       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19025       objc_start_protocol (proto, protorefs);
19026       cp_parser_objc_method_prototype_list (parser);
19027     }
19028 }
19029
19030 /* Parse an Objective-C superclass or category.  */
19031
19032 static void
19033 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19034                                                           tree *categ)
19035 {
19036   cp_token *next = cp_lexer_peek_token (parser->lexer);
19037
19038   *super = *categ = NULL_TREE;
19039   if (next->type == CPP_COLON)
19040     {
19041       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19042       *super = cp_parser_identifier (parser);
19043     }
19044   else if (next->type == CPP_OPEN_PAREN)
19045     {
19046       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19047       *categ = cp_parser_identifier (parser);
19048       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19049     }
19050 }
19051
19052 /* Parse an Objective-C class interface.  */
19053
19054 static void
19055 cp_parser_objc_class_interface (cp_parser* parser)
19056 {
19057   tree name, super, categ, protos;
19058
19059   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19060   name = cp_parser_identifier (parser);
19061   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19062   protos = cp_parser_objc_protocol_refs_opt (parser);
19063
19064   /* We have either a class or a category on our hands.  */
19065   if (categ)
19066     objc_start_category_interface (name, categ, protos);
19067   else
19068     {
19069       objc_start_class_interface (name, super, protos);
19070       /* Handle instance variable declarations, if any.  */
19071       cp_parser_objc_class_ivars (parser);
19072       objc_continue_interface ();
19073     }
19074
19075   cp_parser_objc_method_prototype_list (parser);
19076 }
19077
19078 /* Parse an Objective-C class implementation.  */
19079
19080 static void
19081 cp_parser_objc_class_implementation (cp_parser* parser)
19082 {
19083   tree name, super, categ;
19084
19085   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19086   name = cp_parser_identifier (parser);
19087   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19088
19089   /* We have either a class or a category on our hands.  */
19090   if (categ)
19091     objc_start_category_implementation (name, categ);
19092   else
19093     {
19094       objc_start_class_implementation (name, super);
19095       /* Handle instance variable declarations, if any.  */
19096       cp_parser_objc_class_ivars (parser);
19097       objc_continue_implementation ();
19098     }
19099
19100   cp_parser_objc_method_definition_list (parser);
19101 }
19102
19103 /* Consume the @end token and finish off the implementation.  */
19104
19105 static void
19106 cp_parser_objc_end_implementation (cp_parser* parser)
19107 {
19108   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19109   objc_finish_implementation ();
19110 }
19111
19112 /* Parse an Objective-C declaration.  */
19113
19114 static void
19115 cp_parser_objc_declaration (cp_parser* parser)
19116 {
19117   /* Try to figure out what kind of declaration is present.  */
19118   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19119
19120   switch (kwd->keyword)
19121     {
19122     case RID_AT_ALIAS:
19123       cp_parser_objc_alias_declaration (parser);
19124       break;
19125     case RID_AT_CLASS:
19126       cp_parser_objc_class_declaration (parser);
19127       break;
19128     case RID_AT_PROTOCOL:
19129       cp_parser_objc_protocol_declaration (parser);
19130       break;
19131     case RID_AT_INTERFACE:
19132       cp_parser_objc_class_interface (parser);
19133       break;
19134     case RID_AT_IMPLEMENTATION:
19135       cp_parser_objc_class_implementation (parser);
19136       break;
19137     case RID_AT_END:
19138       cp_parser_objc_end_implementation (parser);
19139       break;
19140     default:
19141       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19142       cp_parser_skip_to_end_of_block_or_statement (parser);
19143     }
19144 }
19145
19146 /* Parse an Objective-C try-catch-finally statement.
19147
19148    objc-try-catch-finally-stmt:
19149      @try compound-statement objc-catch-clause-seq [opt]
19150        objc-finally-clause [opt]
19151
19152    objc-catch-clause-seq:
19153      objc-catch-clause objc-catch-clause-seq [opt]
19154
19155    objc-catch-clause:
19156      @catch ( exception-declaration ) compound-statement
19157
19158    objc-finally-clause
19159      @finally compound-statement
19160
19161    Returns NULL_TREE.  */
19162
19163 static tree
19164 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19165   location_t location;
19166   tree stmt;
19167
19168   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19169   location = cp_lexer_peek_token (parser->lexer)->location;
19170   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19171      node, lest it get absorbed into the surrounding block.  */
19172   stmt = push_stmt_list ();
19173   cp_parser_compound_statement (parser, NULL, false);
19174   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19175
19176   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19177     {
19178       cp_parameter_declarator *parmdecl;
19179       tree parm;
19180
19181       cp_lexer_consume_token (parser->lexer);
19182       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19183       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19184       parm = grokdeclarator (parmdecl->declarator,
19185                              &parmdecl->decl_specifiers,
19186                              PARM, /*initialized=*/0,
19187                              /*attrlist=*/NULL);
19188       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19189       objc_begin_catch_clause (parm);
19190       cp_parser_compound_statement (parser, NULL, false);
19191       objc_finish_catch_clause ();
19192     }
19193
19194   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19195     {
19196       cp_lexer_consume_token (parser->lexer);
19197       location = cp_lexer_peek_token (parser->lexer)->location;
19198       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19199          node, lest it get absorbed into the surrounding block.  */
19200       stmt = push_stmt_list ();
19201       cp_parser_compound_statement (parser, NULL, false);
19202       objc_build_finally_clause (location, pop_stmt_list (stmt));
19203     }
19204
19205   return objc_finish_try_stmt ();
19206 }
19207
19208 /* Parse an Objective-C synchronized statement.
19209
19210    objc-synchronized-stmt:
19211      @synchronized ( expression ) compound-statement
19212
19213    Returns NULL_TREE.  */
19214
19215 static tree
19216 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19217   location_t location;
19218   tree lock, stmt;
19219
19220   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19221
19222   location = cp_lexer_peek_token (parser->lexer)->location;
19223   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19224   lock = cp_parser_expression (parser, false);
19225   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19226
19227   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19228      node, lest it get absorbed into the surrounding block.  */
19229   stmt = push_stmt_list ();
19230   cp_parser_compound_statement (parser, NULL, false);
19231
19232   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19233 }
19234
19235 /* Parse an Objective-C throw statement.
19236
19237    objc-throw-stmt:
19238      @throw assignment-expression [opt] ;
19239
19240    Returns a constructed '@throw' statement.  */
19241
19242 static tree
19243 cp_parser_objc_throw_statement (cp_parser *parser) {
19244   tree expr = NULL_TREE;
19245
19246   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19247
19248   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19249     expr = cp_parser_assignment_expression (parser, false);
19250
19251   cp_parser_consume_semicolon_at_end_of_statement (parser);
19252
19253   return objc_build_throw_stmt (expr);
19254 }
19255
19256 /* Parse an Objective-C statement.  */
19257
19258 static tree
19259 cp_parser_objc_statement (cp_parser * parser) {
19260   /* Try to figure out what kind of declaration is present.  */
19261   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19262
19263   switch (kwd->keyword)
19264     {
19265     case RID_AT_TRY:
19266       return cp_parser_objc_try_catch_finally_statement (parser);
19267     case RID_AT_SYNCHRONIZED:
19268       return cp_parser_objc_synchronized_statement (parser);
19269     case RID_AT_THROW:
19270       return cp_parser_objc_throw_statement (parser);
19271     default:
19272       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19273       cp_parser_skip_to_end_of_block_or_statement (parser);
19274     }
19275
19276   return error_mark_node;
19277 }
19278 \f
19279 /* OpenMP 2.5 parsing routines.  */
19280
19281 /* Returns name of the next clause.
19282    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19283    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19284    returned and the token is consumed.  */
19285
19286 static pragma_omp_clause
19287 cp_parser_omp_clause_name (cp_parser *parser)
19288 {
19289   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19290
19291   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19292     result = PRAGMA_OMP_CLAUSE_IF;
19293   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19294     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19295   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19296     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19297   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19298     {
19299       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19300       const char *p = IDENTIFIER_POINTER (id);
19301
19302       switch (p[0])
19303         {
19304         case 'c':
19305           if (!strcmp ("copyin", p))
19306             result = PRAGMA_OMP_CLAUSE_COPYIN;
19307           else if (!strcmp ("copyprivate", p))
19308             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19309           break;
19310         case 'f':
19311           if (!strcmp ("firstprivate", p))
19312             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19313           break;
19314         case 'l':
19315           if (!strcmp ("lastprivate", p))
19316             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19317           break;
19318         case 'n':
19319           if (!strcmp ("nowait", p))
19320             result = PRAGMA_OMP_CLAUSE_NOWAIT;
19321           else if (!strcmp ("num_threads", p))
19322             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19323           break;
19324         case 'o':
19325           if (!strcmp ("ordered", p))
19326             result = PRAGMA_OMP_CLAUSE_ORDERED;
19327           break;
19328         case 'r':
19329           if (!strcmp ("reduction", p))
19330             result = PRAGMA_OMP_CLAUSE_REDUCTION;
19331           break;
19332         case 's':
19333           if (!strcmp ("schedule", p))
19334             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19335           else if (!strcmp ("shared", p))
19336             result = PRAGMA_OMP_CLAUSE_SHARED;
19337           break;
19338         }
19339     }
19340
19341   if (result != PRAGMA_OMP_CLAUSE_NONE)
19342     cp_lexer_consume_token (parser->lexer);
19343
19344   return result;
19345 }
19346
19347 /* Validate that a clause of the given type does not already exist.  */
19348
19349 static void
19350 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19351 {
19352   tree c;
19353
19354   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19355     if (OMP_CLAUSE_CODE (c) == code)
19356       {
19357         error ("too many %qs clauses", name);
19358         break;
19359       }
19360 }
19361
19362 /* OpenMP 2.5:
19363    variable-list:
19364      identifier
19365      variable-list , identifier
19366
19367    In addition, we match a closing parenthesis.  An opening parenthesis
19368    will have been consumed by the caller.
19369
19370    If KIND is nonzero, create the appropriate node and install the decl
19371    in OMP_CLAUSE_DECL and add the node to the head of the list.
19372
19373    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19374    return the list created.  */
19375
19376 static tree
19377 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19378                                 tree list)
19379 {
19380   while (1)
19381     {
19382       tree name, decl;
19383
19384       name = cp_parser_id_expression (parser, /*template_p=*/false,
19385                                       /*check_dependency_p=*/true,
19386                                       /*template_p=*/NULL,
19387                                       /*declarator_p=*/false,
19388                                       /*optional_p=*/false);
19389       if (name == error_mark_node)
19390         goto skip_comma;
19391
19392       decl = cp_parser_lookup_name_simple (parser, name);
19393       if (decl == error_mark_node)
19394         cp_parser_name_lookup_error (parser, name, decl, NULL);
19395       else if (kind != 0)
19396         {
19397           tree u = build_omp_clause (kind);
19398           OMP_CLAUSE_DECL (u) = decl;
19399           OMP_CLAUSE_CHAIN (u) = list;
19400           list = u;
19401         }
19402       else
19403         list = tree_cons (decl, NULL_TREE, list);
19404
19405     get_comma:
19406       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19407         break;
19408       cp_lexer_consume_token (parser->lexer);
19409     }
19410
19411   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19412     {
19413       int ending;
19414
19415       /* Try to resync to an unnested comma.  Copied from
19416          cp_parser_parenthesized_expression_list.  */
19417     skip_comma:
19418       ending = cp_parser_skip_to_closing_parenthesis (parser,
19419                                                       /*recovering=*/true,
19420                                                       /*or_comma=*/true,
19421                                                       /*consume_paren=*/true);
19422       if (ending < 0)
19423         goto get_comma;
19424     }
19425
19426   return list;
19427 }
19428
19429 /* Similarly, but expect leading and trailing parenthesis.  This is a very
19430    common case for omp clauses.  */
19431
19432 static tree
19433 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19434 {
19435   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19436     return cp_parser_omp_var_list_no_open (parser, kind, list);
19437   return list;
19438 }
19439
19440 /* OpenMP 2.5:
19441    default ( shared | none ) */
19442
19443 static tree
19444 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19445 {
19446   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19447   tree c;
19448
19449   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19450     return list;
19451   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19452     {
19453       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19454       const char *p = IDENTIFIER_POINTER (id);
19455
19456       switch (p[0])
19457         {
19458         case 'n':
19459           if (strcmp ("none", p) != 0)
19460             goto invalid_kind;
19461           kind = OMP_CLAUSE_DEFAULT_NONE;
19462           break;
19463
19464         case 's':
19465           if (strcmp ("shared", p) != 0)
19466             goto invalid_kind;
19467           kind = OMP_CLAUSE_DEFAULT_SHARED;
19468           break;
19469
19470         default:
19471           goto invalid_kind;
19472         }
19473
19474       cp_lexer_consume_token (parser->lexer);
19475     }
19476   else
19477     {
19478     invalid_kind:
19479       cp_parser_error (parser, "expected %<none%> or %<shared%>");
19480     }
19481
19482   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19483     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19484                                            /*or_comma=*/false,
19485                                            /*consume_paren=*/true);
19486
19487   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19488     return list;
19489
19490   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19491   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19492   OMP_CLAUSE_CHAIN (c) = list;
19493   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19494
19495   return c;
19496 }
19497
19498 /* OpenMP 2.5:
19499    if ( expression ) */
19500
19501 static tree
19502 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19503 {
19504   tree t, c;
19505
19506   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19507     return list;
19508
19509   t = cp_parser_condition (parser);
19510
19511   if (t == error_mark_node
19512       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19513     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19514                                            /*or_comma=*/false,
19515                                            /*consume_paren=*/true);
19516
19517   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19518
19519   c = build_omp_clause (OMP_CLAUSE_IF);
19520   OMP_CLAUSE_IF_EXPR (c) = t;
19521   OMP_CLAUSE_CHAIN (c) = list;
19522
19523   return c;
19524 }
19525
19526 /* OpenMP 2.5:
19527    nowait */
19528
19529 static tree
19530 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19531 {
19532   tree c;
19533
19534   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19535
19536   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19537   OMP_CLAUSE_CHAIN (c) = list;
19538   return c;
19539 }
19540
19541 /* OpenMP 2.5:
19542    num_threads ( expression ) */
19543
19544 static tree
19545 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19546 {
19547   tree t, c;
19548
19549   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19550     return list;
19551
19552   t = cp_parser_expression (parser, false);
19553
19554   if (t == error_mark_node
19555       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19556     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19557                                            /*or_comma=*/false,
19558                                            /*consume_paren=*/true);
19559
19560   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19561
19562   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19563   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19564   OMP_CLAUSE_CHAIN (c) = list;
19565
19566   return c;
19567 }
19568
19569 /* OpenMP 2.5:
19570    ordered */
19571
19572 static tree
19573 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19574 {
19575   tree c;
19576
19577   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19578
19579   c = build_omp_clause (OMP_CLAUSE_ORDERED);
19580   OMP_CLAUSE_CHAIN (c) = list;
19581   return c;
19582 }
19583
19584 /* OpenMP 2.5:
19585    reduction ( reduction-operator : variable-list )
19586
19587    reduction-operator:
19588      One of: + * - & ^ | && || */
19589
19590 static tree
19591 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19592 {
19593   enum tree_code code;
19594   tree nlist, c;
19595
19596   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19597     return list;
19598
19599   switch (cp_lexer_peek_token (parser->lexer)->type)
19600     {
19601     case CPP_PLUS:
19602       code = PLUS_EXPR;
19603       break;
19604     case CPP_MULT:
19605       code = MULT_EXPR;
19606       break;
19607     case CPP_MINUS:
19608       code = MINUS_EXPR;
19609       break;
19610     case CPP_AND:
19611       code = BIT_AND_EXPR;
19612       break;
19613     case CPP_XOR:
19614       code = BIT_XOR_EXPR;
19615       break;
19616     case CPP_OR:
19617       code = BIT_IOR_EXPR;
19618       break;
19619     case CPP_AND_AND:
19620       code = TRUTH_ANDIF_EXPR;
19621       break;
19622     case CPP_OR_OR:
19623       code = TRUTH_ORIF_EXPR;
19624       break;
19625     default:
19626       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19627     resync_fail:
19628       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19629                                              /*or_comma=*/false,
19630                                              /*consume_paren=*/true);
19631       return list;
19632     }
19633   cp_lexer_consume_token (parser->lexer);
19634
19635   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19636     goto resync_fail;
19637
19638   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19639   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19640     OMP_CLAUSE_REDUCTION_CODE (c) = code;
19641
19642   return nlist;
19643 }
19644
19645 /* OpenMP 2.5:
19646    schedule ( schedule-kind )
19647    schedule ( schedule-kind , expression )
19648
19649    schedule-kind:
19650      static | dynamic | guided | runtime  */
19651
19652 static tree
19653 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19654 {
19655   tree c, t;
19656
19657   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19658     return list;
19659
19660   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19661
19662   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19663     {
19664       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19665       const char *p = IDENTIFIER_POINTER (id);
19666
19667       switch (p[0])
19668         {
19669         case 'd':
19670           if (strcmp ("dynamic", p) != 0)
19671             goto invalid_kind;
19672           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19673           break;
19674
19675         case 'g':
19676           if (strcmp ("guided", p) != 0)
19677             goto invalid_kind;
19678           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19679           break;
19680
19681         case 'r':
19682           if (strcmp ("runtime", p) != 0)
19683             goto invalid_kind;
19684           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19685           break;
19686
19687         default:
19688           goto invalid_kind;
19689         }
19690     }
19691   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19692     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19693   else
19694     goto invalid_kind;
19695   cp_lexer_consume_token (parser->lexer);
19696
19697   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19698     {
19699       cp_lexer_consume_token (parser->lexer);
19700
19701       t = cp_parser_assignment_expression (parser, false);
19702
19703       if (t == error_mark_node)
19704         goto resync_fail;
19705       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19706         error ("schedule %<runtime%> does not take "
19707                "a %<chunk_size%> parameter");
19708       else
19709         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19710
19711       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19712         goto resync_fail;
19713     }
19714   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19715     goto resync_fail;
19716
19717   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19718   OMP_CLAUSE_CHAIN (c) = list;
19719   return c;
19720
19721  invalid_kind:
19722   cp_parser_error (parser, "invalid schedule kind");
19723  resync_fail:
19724   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19725                                          /*or_comma=*/false,
19726                                          /*consume_paren=*/true);
19727   return list;
19728 }
19729
19730 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
19731    is a bitmask in MASK.  Return the list of clauses found; the result
19732    of clause default goes in *pdefault.  */
19733
19734 static tree
19735 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19736                            const char *where, cp_token *pragma_tok)
19737 {
19738   tree clauses = NULL;
19739   bool first = true;
19740
19741   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19742     {
19743       pragma_omp_clause c_kind;
19744       const char *c_name;
19745       tree prev = clauses;
19746
19747       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19748         cp_lexer_consume_token (parser->lexer);
19749
19750       c_kind = cp_parser_omp_clause_name (parser);
19751       first = false;
19752
19753       switch (c_kind)
19754         {
19755         case PRAGMA_OMP_CLAUSE_COPYIN:
19756           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19757           c_name = "copyin";
19758           break;
19759         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19760           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19761                                             clauses);
19762           c_name = "copyprivate";
19763           break;
19764         case PRAGMA_OMP_CLAUSE_DEFAULT:
19765           clauses = cp_parser_omp_clause_default (parser, clauses);
19766           c_name = "default";
19767           break;
19768         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19769           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19770                                             clauses);
19771           c_name = "firstprivate";
19772           break;
19773         case PRAGMA_OMP_CLAUSE_IF:
19774           clauses = cp_parser_omp_clause_if (parser, clauses);
19775           c_name = "if";
19776           break;
19777         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19778           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19779                                             clauses);
19780           c_name = "lastprivate";
19781           break;
19782         case PRAGMA_OMP_CLAUSE_NOWAIT:
19783           clauses = cp_parser_omp_clause_nowait (parser, clauses);
19784           c_name = "nowait";
19785           break;
19786         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19787           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19788           c_name = "num_threads";
19789           break;
19790         case PRAGMA_OMP_CLAUSE_ORDERED:
19791           clauses = cp_parser_omp_clause_ordered (parser, clauses);
19792           c_name = "ordered";
19793           break;
19794         case PRAGMA_OMP_CLAUSE_PRIVATE:
19795           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19796                                             clauses);
19797           c_name = "private";
19798           break;
19799         case PRAGMA_OMP_CLAUSE_REDUCTION:
19800           clauses = cp_parser_omp_clause_reduction (parser, clauses);
19801           c_name = "reduction";
19802           break;
19803         case PRAGMA_OMP_CLAUSE_SCHEDULE:
19804           clauses = cp_parser_omp_clause_schedule (parser, clauses);
19805           c_name = "schedule";
19806           break;
19807         case PRAGMA_OMP_CLAUSE_SHARED:
19808           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19809                                             clauses);
19810           c_name = "shared";
19811           break;
19812         default:
19813           cp_parser_error (parser, "expected %<#pragma omp%> clause");
19814           goto saw_error;
19815         }
19816
19817       if (((mask >> c_kind) & 1) == 0)
19818         {
19819           /* Remove the invalid clause(s) from the list to avoid
19820              confusing the rest of the compiler.  */
19821           clauses = prev;
19822           error ("%qs is not valid for %qs", c_name, where);
19823         }
19824     }
19825  saw_error:
19826   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19827   return finish_omp_clauses (clauses);
19828 }
19829
19830 /* OpenMP 2.5:
19831    structured-block:
19832      statement
19833
19834    In practice, we're also interested in adding the statement to an
19835    outer node.  So it is convenient if we work around the fact that
19836    cp_parser_statement calls add_stmt.  */
19837
19838 static unsigned
19839 cp_parser_begin_omp_structured_block (cp_parser *parser)
19840 {
19841   unsigned save = parser->in_statement;
19842
19843   /* Only move the values to IN_OMP_BLOCK if they weren't false.
19844      This preserves the "not within loop or switch" style error messages
19845      for nonsense cases like
19846         void foo() {
19847         #pragma omp single
19848           break;
19849         }
19850   */
19851   if (parser->in_statement)
19852     parser->in_statement = IN_OMP_BLOCK;
19853
19854   return save;
19855 }
19856
19857 static void
19858 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19859 {
19860   parser->in_statement = save;
19861 }
19862
19863 static tree
19864 cp_parser_omp_structured_block (cp_parser *parser)
19865 {
19866   tree stmt = begin_omp_structured_block ();
19867   unsigned int save = cp_parser_begin_omp_structured_block (parser);
19868
19869   cp_parser_statement (parser, NULL_TREE, false, NULL);
19870
19871   cp_parser_end_omp_structured_block (parser, save);
19872   return finish_omp_structured_block (stmt);
19873 }
19874
19875 /* OpenMP 2.5:
19876    # pragma omp atomic new-line
19877      expression-stmt
19878
19879    expression-stmt:
19880      x binop= expr | x++ | ++x | x-- | --x
19881    binop:
19882      +, *, -, /, &, ^, |, <<, >>
19883
19884   where x is an lvalue expression with scalar type.  */
19885
19886 static void
19887 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19888 {
19889   tree lhs, rhs;
19890   enum tree_code code;
19891
19892   cp_parser_require_pragma_eol (parser, pragma_tok);
19893
19894   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19895                                     /*cast_p=*/false);
19896   switch (TREE_CODE (lhs))
19897     {
19898     case ERROR_MARK:
19899       goto saw_error;
19900
19901     case PREINCREMENT_EXPR:
19902     case POSTINCREMENT_EXPR:
19903       lhs = TREE_OPERAND (lhs, 0);
19904       code = PLUS_EXPR;
19905       rhs = integer_one_node;
19906       break;
19907
19908     case PREDECREMENT_EXPR:
19909     case POSTDECREMENT_EXPR:
19910       lhs = TREE_OPERAND (lhs, 0);
19911       code = MINUS_EXPR;
19912       rhs = integer_one_node;
19913       break;
19914
19915     default:
19916       switch (cp_lexer_peek_token (parser->lexer)->type)
19917         {
19918         case CPP_MULT_EQ:
19919           code = MULT_EXPR;
19920           break;
19921         case CPP_DIV_EQ:
19922           code = TRUNC_DIV_EXPR;
19923           break;
19924         case CPP_PLUS_EQ:
19925           code = PLUS_EXPR;
19926           break;
19927         case CPP_MINUS_EQ:
19928           code = MINUS_EXPR;
19929           break;
19930         case CPP_LSHIFT_EQ:
19931           code = LSHIFT_EXPR;
19932           break;
19933         case CPP_RSHIFT_EQ:
19934           code = RSHIFT_EXPR;
19935           break;
19936         case CPP_AND_EQ:
19937           code = BIT_AND_EXPR;
19938           break;
19939         case CPP_OR_EQ:
19940           code = BIT_IOR_EXPR;
19941           break;
19942         case CPP_XOR_EQ:
19943           code = BIT_XOR_EXPR;
19944           break;
19945         default:
19946           cp_parser_error (parser,
19947                            "invalid operator for %<#pragma omp atomic%>");
19948           goto saw_error;
19949         }
19950       cp_lexer_consume_token (parser->lexer);
19951
19952       rhs = cp_parser_expression (parser, false);
19953       if (rhs == error_mark_node)
19954         goto saw_error;
19955       break;
19956     }
19957   finish_omp_atomic (code, lhs, rhs);
19958   cp_parser_consume_semicolon_at_end_of_statement (parser);
19959   return;
19960
19961  saw_error:
19962   cp_parser_skip_to_end_of_block_or_statement (parser);
19963 }
19964
19965
19966 /* OpenMP 2.5:
19967    # pragma omp barrier new-line  */
19968
19969 static void
19970 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19971 {
19972   cp_parser_require_pragma_eol (parser, pragma_tok);
19973   finish_omp_barrier ();
19974 }
19975
19976 /* OpenMP 2.5:
19977    # pragma omp critical [(name)] new-line
19978      structured-block  */
19979
19980 static tree
19981 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19982 {
19983   tree stmt, name = NULL;
19984
19985   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19986     {
19987       cp_lexer_consume_token (parser->lexer);
19988
19989       name = cp_parser_identifier (parser);
19990
19991       if (name == error_mark_node
19992           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19993         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19994                                                /*or_comma=*/false,
19995                                                /*consume_paren=*/true);
19996       if (name == error_mark_node)
19997         name = NULL;
19998     }
19999   cp_parser_require_pragma_eol (parser, pragma_tok);
20000
20001   stmt = cp_parser_omp_structured_block (parser);
20002   return c_finish_omp_critical (stmt, name);
20003 }
20004
20005 /* OpenMP 2.5:
20006    # pragma omp flush flush-vars[opt] new-line
20007
20008    flush-vars:
20009      ( variable-list ) */
20010
20011 static void
20012 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20013 {
20014   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20015     (void) cp_parser_omp_var_list (parser, 0, NULL);
20016   cp_parser_require_pragma_eol (parser, pragma_tok);
20017
20018   finish_omp_flush ();
20019 }
20020
20021 /* Parse the restricted form of the for statment allowed by OpenMP.  */
20022
20023 static tree
20024 cp_parser_omp_for_loop (cp_parser *parser)
20025 {
20026   tree init, cond, incr, body, decl, pre_body;
20027   location_t loc;
20028
20029   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20030     {
20031       cp_parser_error (parser, "for statement expected");
20032       return NULL;
20033     }
20034   loc = cp_lexer_consume_token (parser->lexer)->location;
20035   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20036     return NULL;
20037
20038   init = decl = NULL;
20039   pre_body = push_stmt_list ();
20040   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20041     {
20042       cp_decl_specifier_seq type_specifiers;
20043
20044       /* First, try to parse as an initialized declaration.  See
20045          cp_parser_condition, from whence the bulk of this is copied.  */
20046
20047       cp_parser_parse_tentatively (parser);
20048       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20049                                     &type_specifiers);
20050       if (!cp_parser_error_occurred (parser))
20051         {
20052           tree asm_specification, attributes;
20053           cp_declarator *declarator;
20054
20055           declarator = cp_parser_declarator (parser,
20056                                              CP_PARSER_DECLARATOR_NAMED,
20057                                              /*ctor_dtor_or_conv_p=*/NULL,
20058                                              /*parenthesized_p=*/NULL,
20059                                              /*member_p=*/false);
20060           attributes = cp_parser_attributes_opt (parser);
20061           asm_specification = cp_parser_asm_specification_opt (parser);
20062
20063           cp_parser_require (parser, CPP_EQ, "`='");
20064           if (cp_parser_parse_definitely (parser))
20065             {
20066               tree pushed_scope;
20067
20068               decl = start_decl (declarator, &type_specifiers,
20069                                  /*initialized_p=*/false, attributes,
20070                                  /*prefix_attributes=*/NULL_TREE,
20071                                  &pushed_scope);
20072
20073               init = cp_parser_assignment_expression (parser, false);
20074
20075               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
20076                               asm_specification, LOOKUP_ONLYCONVERTING);
20077
20078               if (pushed_scope)
20079                 pop_scope (pushed_scope);
20080             }
20081         }
20082       else
20083         cp_parser_abort_tentative_parse (parser);
20084
20085       /* If parsing as an initialized declaration failed, try again as
20086          a simple expression.  */
20087       if (decl == NULL)
20088         init = cp_parser_expression (parser, false);
20089     }
20090   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20091   pre_body = pop_stmt_list (pre_body);
20092
20093   cond = NULL;
20094   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20095     cond = cp_parser_condition (parser);
20096   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20097
20098   incr = NULL;
20099   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20100     incr = cp_parser_expression (parser, false);
20101
20102   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20103     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20104                                            /*or_comma=*/false,
20105                                            /*consume_paren=*/true);
20106
20107   /* Note that we saved the original contents of this flag when we entered
20108      the structured block, and so we don't need to re-save it here.  */
20109   parser->in_statement = IN_OMP_FOR;
20110
20111   /* Note that the grammar doesn't call for a structured block here,
20112      though the loop as a whole is a structured block.  */
20113   body = push_stmt_list ();
20114   cp_parser_statement (parser, NULL_TREE, false, NULL);
20115   body = pop_stmt_list (body);
20116
20117   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
20118 }
20119
20120 /* OpenMP 2.5:
20121    #pragma omp for for-clause[optseq] new-line
20122      for-loop  */
20123
20124 #define OMP_FOR_CLAUSE_MASK                             \
20125         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20126         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20127         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
20128         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20129         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
20130         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
20131         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20132
20133 static tree
20134 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20135 {
20136   tree clauses, sb, ret;
20137   unsigned int save;
20138
20139   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20140                                        "#pragma omp for", pragma_tok);
20141
20142   sb = begin_omp_structured_block ();
20143   save = cp_parser_begin_omp_structured_block (parser);
20144
20145   ret = cp_parser_omp_for_loop (parser);
20146   if (ret)
20147     OMP_FOR_CLAUSES (ret) = clauses;
20148
20149   cp_parser_end_omp_structured_block (parser, save);
20150   add_stmt (finish_omp_structured_block (sb));
20151
20152   return ret;
20153 }
20154
20155 /* OpenMP 2.5:
20156    # pragma omp master new-line
20157      structured-block  */
20158
20159 static tree
20160 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20161 {
20162   cp_parser_require_pragma_eol (parser, pragma_tok);
20163   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20164 }
20165
20166 /* OpenMP 2.5:
20167    # pragma omp ordered new-line
20168      structured-block  */
20169
20170 static tree
20171 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20172 {
20173   cp_parser_require_pragma_eol (parser, pragma_tok);
20174   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20175 }
20176
20177 /* OpenMP 2.5:
20178
20179    section-scope:
20180      { section-sequence }
20181
20182    section-sequence:
20183      section-directive[opt] structured-block
20184      section-sequence section-directive structured-block  */
20185
20186 static tree
20187 cp_parser_omp_sections_scope (cp_parser *parser)
20188 {
20189   tree stmt, substmt;
20190   bool error_suppress = false;
20191   cp_token *tok;
20192
20193   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20194     return NULL_TREE;
20195
20196   stmt = push_stmt_list ();
20197
20198   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20199     {
20200       unsigned save;
20201
20202       substmt = begin_omp_structured_block ();
20203       save = cp_parser_begin_omp_structured_block (parser);
20204
20205       while (1)
20206         {
20207           cp_parser_statement (parser, NULL_TREE, false, NULL);
20208
20209           tok = cp_lexer_peek_token (parser->lexer);
20210           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20211             break;
20212           if (tok->type == CPP_CLOSE_BRACE)
20213             break;
20214           if (tok->type == CPP_EOF)
20215             break;
20216         }
20217
20218       cp_parser_end_omp_structured_block (parser, save);
20219       substmt = finish_omp_structured_block (substmt);
20220       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20221       add_stmt (substmt);
20222     }
20223
20224   while (1)
20225     {
20226       tok = cp_lexer_peek_token (parser->lexer);
20227       if (tok->type == CPP_CLOSE_BRACE)
20228         break;
20229       if (tok->type == CPP_EOF)
20230         break;
20231
20232       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20233         {
20234           cp_lexer_consume_token (parser->lexer);
20235           cp_parser_require_pragma_eol (parser, tok);
20236           error_suppress = false;
20237         }
20238       else if (!error_suppress)
20239         {
20240           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20241           error_suppress = true;
20242         }
20243
20244       substmt = cp_parser_omp_structured_block (parser);
20245       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20246       add_stmt (substmt);
20247     }
20248   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20249
20250   substmt = pop_stmt_list (stmt);
20251
20252   stmt = make_node (OMP_SECTIONS);
20253   TREE_TYPE (stmt) = void_type_node;
20254   OMP_SECTIONS_BODY (stmt) = substmt;
20255
20256   add_stmt (stmt);
20257   return stmt;
20258 }
20259
20260 /* OpenMP 2.5:
20261    # pragma omp sections sections-clause[optseq] newline
20262      sections-scope  */
20263
20264 #define OMP_SECTIONS_CLAUSE_MASK                        \
20265         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20266         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20267         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
20268         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20269         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20270
20271 static tree
20272 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20273 {
20274   tree clauses, ret;
20275
20276   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20277                                        "#pragma omp sections", pragma_tok);
20278
20279   ret = cp_parser_omp_sections_scope (parser);
20280   if (ret)
20281     OMP_SECTIONS_CLAUSES (ret) = clauses;
20282
20283   return ret;
20284 }
20285
20286 /* OpenMP 2.5:
20287    # pragma parallel parallel-clause new-line
20288    # pragma parallel for parallel-for-clause new-line
20289    # pragma parallel sections parallel-sections-clause new-line  */
20290
20291 #define OMP_PARALLEL_CLAUSE_MASK                        \
20292         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
20293         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20294         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20295         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
20296         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
20297         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
20298         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20299         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20300
20301 static tree
20302 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20303 {
20304   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20305   const char *p_name = "#pragma omp parallel";
20306   tree stmt, clauses, par_clause, ws_clause, block;
20307   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20308   unsigned int save;
20309
20310   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20311     {
20312       cp_lexer_consume_token (parser->lexer);
20313       p_kind = PRAGMA_OMP_PARALLEL_FOR;
20314       p_name = "#pragma omp parallel for";
20315       mask |= OMP_FOR_CLAUSE_MASK;
20316       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20317     }
20318   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20319     {
20320       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20321       const char *p = IDENTIFIER_POINTER (id);
20322       if (strcmp (p, "sections") == 0)
20323         {
20324           cp_lexer_consume_token (parser->lexer);
20325           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20326           p_name = "#pragma omp parallel sections";
20327           mask |= OMP_SECTIONS_CLAUSE_MASK;
20328           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20329         }
20330     }
20331
20332   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20333   block = begin_omp_parallel ();
20334   save = cp_parser_begin_omp_structured_block (parser);
20335
20336   switch (p_kind)
20337     {
20338     case PRAGMA_OMP_PARALLEL:
20339       cp_parser_statement (parser, NULL_TREE, false, NULL);
20340       par_clause = clauses;
20341       break;
20342
20343     case PRAGMA_OMP_PARALLEL_FOR:
20344       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20345       stmt = cp_parser_omp_for_loop (parser);
20346       if (stmt)
20347         OMP_FOR_CLAUSES (stmt) = ws_clause;
20348       break;
20349
20350     case PRAGMA_OMP_PARALLEL_SECTIONS:
20351       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20352       stmt = cp_parser_omp_sections_scope (parser);
20353       if (stmt)
20354         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20355       break;
20356
20357     default:
20358       gcc_unreachable ();
20359     }
20360
20361   cp_parser_end_omp_structured_block (parser, save);
20362   stmt = finish_omp_parallel (par_clause, block);
20363   if (p_kind != PRAGMA_OMP_PARALLEL)
20364     OMP_PARALLEL_COMBINED (stmt) = 1;
20365   return stmt;
20366 }
20367
20368 /* OpenMP 2.5:
20369    # pragma omp single single-clause[optseq] new-line
20370      structured-block  */
20371
20372 #define OMP_SINGLE_CLAUSE_MASK                          \
20373         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20374         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20375         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
20376         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20377
20378 static tree
20379 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20380 {
20381   tree stmt = make_node (OMP_SINGLE);
20382   TREE_TYPE (stmt) = void_type_node;
20383
20384   OMP_SINGLE_CLAUSES (stmt)
20385     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20386                                  "#pragma omp single", pragma_tok);
20387   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20388
20389   return add_stmt (stmt);
20390 }
20391
20392 /* OpenMP 2.5:
20393    # pragma omp threadprivate (variable-list) */
20394
20395 static void
20396 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20397 {
20398   tree vars;
20399
20400   vars = cp_parser_omp_var_list (parser, 0, NULL);
20401   cp_parser_require_pragma_eol (parser, pragma_tok);
20402
20403   finish_omp_threadprivate (vars);
20404 }
20405
20406 /* Main entry point to OpenMP statement pragmas.  */
20407
20408 static void
20409 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20410 {
20411   tree stmt;
20412
20413   switch (pragma_tok->pragma_kind)
20414     {
20415     case PRAGMA_OMP_ATOMIC:
20416       cp_parser_omp_atomic (parser, pragma_tok);
20417       return;
20418     case PRAGMA_OMP_CRITICAL:
20419       stmt = cp_parser_omp_critical (parser, pragma_tok);
20420       break;
20421     case PRAGMA_OMP_FOR:
20422       stmt = cp_parser_omp_for (parser, pragma_tok);
20423       break;
20424     case PRAGMA_OMP_MASTER:
20425       stmt = cp_parser_omp_master (parser, pragma_tok);
20426       break;
20427     case PRAGMA_OMP_ORDERED:
20428       stmt = cp_parser_omp_ordered (parser, pragma_tok);
20429       break;
20430     case PRAGMA_OMP_PARALLEL:
20431       stmt = cp_parser_omp_parallel (parser, pragma_tok);
20432       break;
20433     case PRAGMA_OMP_SECTIONS:
20434       stmt = cp_parser_omp_sections (parser, pragma_tok);
20435       break;
20436     case PRAGMA_OMP_SINGLE:
20437       stmt = cp_parser_omp_single (parser, pragma_tok);
20438       break;
20439     default:
20440       gcc_unreachable ();
20441     }
20442
20443   if (stmt)
20444     SET_EXPR_LOCATION (stmt, pragma_tok->location);
20445 }
20446 \f
20447 /* The parser.  */
20448
20449 static GTY (()) cp_parser *the_parser;
20450
20451 \f
20452 /* Special handling for the first token or line in the file.  The first
20453    thing in the file might be #pragma GCC pch_preprocess, which loads a
20454    PCH file, which is a GC collection point.  So we need to handle this
20455    first pragma without benefit of an existing lexer structure.
20456
20457    Always returns one token to the caller in *FIRST_TOKEN.  This is
20458    either the true first token of the file, or the first token after
20459    the initial pragma.  */
20460
20461 static void
20462 cp_parser_initial_pragma (cp_token *first_token)
20463 {
20464   tree name = NULL;
20465
20466   cp_lexer_get_preprocessor_token (NULL, first_token);
20467   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20468     return;
20469
20470   cp_lexer_get_preprocessor_token (NULL, first_token);
20471   if (first_token->type == CPP_STRING)
20472     {
20473       name = first_token->u.value;
20474
20475       cp_lexer_get_preprocessor_token (NULL, first_token);
20476       if (first_token->type != CPP_PRAGMA_EOL)
20477         error ("junk at end of %<#pragma GCC pch_preprocess%>");
20478     }
20479   else
20480     error ("expected string literal");
20481
20482   /* Skip to the end of the pragma.  */
20483   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20484     cp_lexer_get_preprocessor_token (NULL, first_token);
20485
20486   /* Now actually load the PCH file.  */
20487   if (name)
20488     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20489
20490   /* Read one more token to return to our caller.  We have to do this
20491      after reading the PCH file in, since its pointers have to be
20492      live.  */
20493   cp_lexer_get_preprocessor_token (NULL, first_token);
20494 }
20495
20496 /* Normal parsing of a pragma token.  Here we can (and must) use the
20497    regular lexer.  */
20498
20499 static bool
20500 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20501 {
20502   cp_token *pragma_tok;
20503   unsigned int id;
20504
20505   pragma_tok = cp_lexer_consume_token (parser->lexer);
20506   gcc_assert (pragma_tok->type == CPP_PRAGMA);
20507   parser->lexer->in_pragma = true;
20508
20509   id = pragma_tok->pragma_kind;
20510   switch (id)
20511     {
20512     case PRAGMA_GCC_PCH_PREPROCESS:
20513       error ("%<#pragma GCC pch_preprocess%> must be first");
20514       break;
20515
20516     case PRAGMA_OMP_BARRIER:
20517       switch (context)
20518         {
20519         case pragma_compound:
20520           cp_parser_omp_barrier (parser, pragma_tok);
20521           return false;
20522         case pragma_stmt:
20523           error ("%<#pragma omp barrier%> may only be "
20524                  "used in compound statements");
20525           break;
20526         default:
20527           goto bad_stmt;
20528         }
20529       break;
20530
20531     case PRAGMA_OMP_FLUSH:
20532       switch (context)
20533         {
20534         case pragma_compound:
20535           cp_parser_omp_flush (parser, pragma_tok);
20536           return false;
20537         case pragma_stmt:
20538           error ("%<#pragma omp flush%> may only be "
20539                  "used in compound statements");
20540           break;
20541         default:
20542           goto bad_stmt;
20543         }
20544       break;
20545
20546     case PRAGMA_OMP_THREADPRIVATE:
20547       cp_parser_omp_threadprivate (parser, pragma_tok);
20548       return false;
20549
20550     case PRAGMA_OMP_ATOMIC:
20551     case PRAGMA_OMP_CRITICAL:
20552     case PRAGMA_OMP_FOR:
20553     case PRAGMA_OMP_MASTER:
20554     case PRAGMA_OMP_ORDERED:
20555     case PRAGMA_OMP_PARALLEL:
20556     case PRAGMA_OMP_SECTIONS:
20557     case PRAGMA_OMP_SINGLE:
20558       if (context == pragma_external)
20559         goto bad_stmt;
20560       cp_parser_omp_construct (parser, pragma_tok);
20561       return true;
20562
20563     case PRAGMA_OMP_SECTION:
20564       error ("%<#pragma omp section%> may only be used in "
20565              "%<#pragma omp sections%> construct");
20566       break;
20567
20568     default:
20569       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20570       c_invoke_pragma_handler (id);
20571       break;
20572
20573     bad_stmt:
20574       cp_parser_error (parser, "expected declaration specifiers");
20575       break;
20576     }
20577
20578   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20579   return false;
20580 }
20581
20582 /* The interface the pragma parsers have to the lexer.  */
20583
20584 enum cpp_ttype
20585 pragma_lex (tree *value)
20586 {
20587   cp_token *tok;
20588   enum cpp_ttype ret;
20589
20590   tok = cp_lexer_peek_token (the_parser->lexer);
20591
20592   ret = tok->type;
20593   *value = tok->u.value;
20594
20595   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20596     ret = CPP_EOF;
20597   else if (ret == CPP_STRING)
20598     *value = cp_parser_string_literal (the_parser, false, false);
20599   else
20600     {
20601       cp_lexer_consume_token (the_parser->lexer);
20602       if (ret == CPP_KEYWORD)
20603         ret = CPP_NAME;
20604     }
20605
20606   return ret;
20607 }
20608
20609 \f
20610 /* External interface.  */
20611
20612 /* Parse one entire translation unit.  */
20613
20614 void
20615 c_parse_file (void)
20616 {
20617   bool error_occurred;
20618   static bool already_called = false;
20619
20620   if (already_called)
20621     {
20622       sorry ("inter-module optimizations not implemented for C++");
20623       return;
20624     }
20625   already_called = true;
20626
20627   the_parser = cp_parser_new ();
20628   push_deferring_access_checks (flag_access_control
20629                                 ? dk_no_deferred : dk_no_check);
20630   error_occurred = cp_parser_translation_unit (the_parser);
20631   the_parser = NULL;
20632 }
20633
20634 #include "gt-cp-parser.h"