OSDN Git Service

* parser.c (cp_parser_asm_specification_opt): Print CPP_CLOSE_PAREN
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a system header.  */
75   BOOL_BITFIELD in_system_header : 1;
76   /* True if this token is from a context where it is implicitly extern "C" */
77   BOOL_BITFIELD implicit_extern_c : 1;
78   /* True for a CPP_NAME token that is not a keyword (i.e., for which
79      KEYWORD is RID_MAX) iff this name was looked up and found to be
80      ambiguous.  An error has already been reported.  */
81   BOOL_BITFIELD ambiguous_p : 1;
82   /* The value associated with this token, if any.  */
83   union cp_token_value {
84     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
85     struct tree_check* GTY((tag ("1"))) tree_check_value;
86     /* Use for all other tokens.  */
87     tree GTY((tag ("0"))) value;
88   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89   /* The location at which this token was found.  */
90   location_t location;
91 } cp_token;
92
93 /* We use a stack of token pointer for saving token sets.  */
94 typedef struct cp_token *cp_token_position;
95 DEF_VEC_P (cp_token_position);
96 DEF_VEC_ALLOC_P (cp_token_position,heap);
97
98 static cp_token eof_token =
99 {
100   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, false, 0, { NULL },
101   0
102 };
103
104 /* The cp_lexer structure represents the C++ lexer.  It is responsible
105    for managing the token stream from the preprocessor and supplying
106    it to the parser.  Tokens are never added to the cp_lexer after
107    it is created.  */
108
109 typedef struct cp_lexer GTY (())
110 {
111   /* The memory allocated for the buffer.  NULL if this lexer does not
112      own the token buffer.  */
113   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
114   /* If the lexer owns the buffer, this is the number of tokens in the
115      buffer.  */
116   size_t buffer_length;
117
118   /* A pointer just past the last available token.  The tokens
119      in this lexer are [buffer, last_token).  */
120   cp_token_position GTY ((skip)) last_token;
121
122   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
123      no more available tokens.  */
124   cp_token_position GTY ((skip)) next_token;
125
126   /* A stack indicating positions at which cp_lexer_save_tokens was
127      called.  The top entry is the most recent position at which we
128      began saving tokens.  If the stack is non-empty, we are saving
129      tokens.  */
130   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
131
132   /* The next lexer in a linked list of lexers.  */
133   struct cp_lexer *next;
134
135   /* True if we should output debugging information.  */
136   bool debugging_p;
137
138   /* True if we're in the context of parsing a pragma, and should not
139      increment past the end-of-line marker.  */
140   bool in_pragma;
141 } cp_lexer;
142
143 /* cp_token_cache is a range of tokens.  There is no need to represent
144    allocate heap memory for it, since tokens are never removed from the
145    lexer's array.  There is also no need for the GC to walk through
146    a cp_token_cache, since everything in here is referenced through
147    a lexer.  */
148
149 typedef struct cp_token_cache GTY(())
150 {
151   /* The beginning of the token range.  */
152   cp_token * GTY((skip)) first;
153
154   /* Points immediately after the last token in the range.  */
155   cp_token * GTY ((skip)) last;
156 } cp_token_cache;
157
158 /* Prototypes.  */
159
160 static cp_lexer *cp_lexer_new_main
161   (void);
162 static cp_lexer *cp_lexer_new_from_tokens
163   (cp_token_cache *tokens);
164 static void cp_lexer_destroy
165   (cp_lexer *);
166 static int cp_lexer_saving_tokens
167   (const cp_lexer *);
168 static cp_token_position cp_lexer_token_position
169   (cp_lexer *, bool);
170 static cp_token *cp_lexer_token_at
171   (cp_lexer *, cp_token_position);
172 static void cp_lexer_get_preprocessor_token
173   (cp_lexer *, cp_token *);
174 static inline cp_token *cp_lexer_peek_token
175   (cp_lexer *);
176 static cp_token *cp_lexer_peek_nth_token
177   (cp_lexer *, size_t);
178 static inline bool cp_lexer_next_token_is
179   (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_not
181   (cp_lexer *, enum cpp_ttype);
182 static bool cp_lexer_next_token_is_keyword
183   (cp_lexer *, enum rid);
184 static cp_token *cp_lexer_consume_token
185   (cp_lexer *);
186 static void cp_lexer_purge_token
187   (cp_lexer *);
188 static void cp_lexer_purge_tokens_after
189   (cp_lexer *, cp_token_position);
190 static void cp_lexer_save_tokens
191   (cp_lexer *);
192 static void cp_lexer_commit_tokens
193   (cp_lexer *);
194 static void cp_lexer_rollback_tokens
195   (cp_lexer *);
196 #ifdef ENABLE_CHECKING
197 static void cp_lexer_print_token
198   (FILE *, cp_token *);
199 static inline bool cp_lexer_debugging_p
200   (cp_lexer *);
201 static void cp_lexer_start_debugging
202   (cp_lexer *) ATTRIBUTE_UNUSED;
203 static void cp_lexer_stop_debugging
204   (cp_lexer *) ATTRIBUTE_UNUSED;
205 #else
206 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
207    about passing NULL to functions that require non-NULL arguments
208    (fputs, fprintf).  It will never be used, so all we need is a value
209    of the right type that's guaranteed not to be NULL.  */
210 #define cp_lexer_debug_stream stdout
211 #define cp_lexer_print_token(str, tok) (void) 0
212 #define cp_lexer_debugging_p(lexer) 0
213 #endif /* ENABLE_CHECKING */
214
215 static cp_token_cache *cp_token_cache_new
216   (cp_token *, cp_token *);
217
218 static void cp_parser_initial_pragma
219   (cp_token *);
220
221 /* Manifest constants.  */
222 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
223 #define CP_SAVED_TOKEN_STACK 5
224
225 /* A token type for keywords, as opposed to ordinary identifiers.  */
226 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
227
228 /* A token type for template-ids.  If a template-id is processed while
229    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
230    the value of the CPP_TEMPLATE_ID is whatever was returned by
231    cp_parser_template_id.  */
232 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
233
234 /* A token type for nested-name-specifiers.  If a
235    nested-name-specifier is processed while parsing tentatively, it is
236    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
237    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
238    cp_parser_nested_name_specifier_opt.  */
239 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
240
241 /* A token type for tokens that are not tokens at all; these are used
242    to represent slots in the array where there used to be a token
243    that has now been deleted.  */
244 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
245
246 /* The number of token types, including C++-specific ones.  */
247 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
248
249 /* Variables.  */
250
251 #ifdef ENABLE_CHECKING
252 /* The stream to which debugging output should be written.  */
253 static FILE *cp_lexer_debug_stream;
254 #endif /* ENABLE_CHECKING */
255
256 /* Create a new main C++ lexer, the lexer that gets tokens from the
257    preprocessor.  */
258
259 static cp_lexer *
260 cp_lexer_new_main (void)
261 {
262   cp_token first_token;
263   cp_lexer *lexer;
264   cp_token *pos;
265   size_t alloc;
266   size_t space;
267   cp_token *buffer;
268
269   /* It's possible that parsing the first pragma will load a PCH file,
270      which is a GC collection point.  So we have to do that before
271      allocating any memory.  */
272   cp_parser_initial_pragma (&first_token);
273
274   c_common_no_more_pch ();
275
276   /* Allocate the memory.  */
277   lexer = GGC_CNEW (cp_lexer);
278
279 #ifdef ENABLE_CHECKING
280   /* Initially we are not debugging.  */
281   lexer->debugging_p = false;
282 #endif /* ENABLE_CHECKING */
283   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
284                                    CP_SAVED_TOKEN_STACK);
285
286   /* Create the buffer.  */
287   alloc = CP_LEXER_BUFFER_SIZE;
288   buffer = GGC_NEWVEC (cp_token, alloc);
289
290   /* Put the first token in the buffer.  */
291   space = alloc;
292   pos = buffer;
293   *pos = first_token;
294
295   /* Get the remaining tokens from the preprocessor.  */
296   while (pos->type != CPP_EOF)
297     {
298       pos++;
299       if (!--space)
300         {
301           space = alloc;
302           alloc *= 2;
303           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
304           pos = buffer + space;
305         }
306       cp_lexer_get_preprocessor_token (lexer, pos);
307     }
308   lexer->buffer = buffer;
309   lexer->buffer_length = alloc - space;
310   lexer->last_token = pos;
311   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
312
313   /* Subsequent preprocessor diagnostics should use compiler
314      diagnostic functions to get the compiler source location.  */
315   cpp_get_options (parse_in)->client_diagnostic = true;
316   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
317
318   gcc_assert (lexer->next_token->type != CPP_PURGED);
319   return lexer;
320 }
321
322 /* Create a new lexer whose token stream is primed with the tokens in
323    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
324
325 static cp_lexer *
326 cp_lexer_new_from_tokens (cp_token_cache *cache)
327 {
328   cp_token *first = cache->first;
329   cp_token *last = cache->last;
330   cp_lexer *lexer = GGC_CNEW (cp_lexer);
331
332   /* We do not own the buffer.  */
333   lexer->buffer = NULL;
334   lexer->buffer_length = 0;
335   lexer->next_token = first == last ? &eof_token : first;
336   lexer->last_token = last;
337
338   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
339                                    CP_SAVED_TOKEN_STACK);
340
341 #ifdef ENABLE_CHECKING
342   /* Initially we are not debugging.  */
343   lexer->debugging_p = false;
344 #endif
345
346   gcc_assert (lexer->next_token->type != CPP_PURGED);
347   return lexer;
348 }
349
350 /* Frees all resources associated with LEXER.  */
351
352 static void
353 cp_lexer_destroy (cp_lexer *lexer)
354 {
355   if (lexer->buffer)
356     ggc_free (lexer->buffer);
357   VEC_free (cp_token_position, heap, lexer->saved_tokens);
358   ggc_free (lexer);
359 }
360
361 /* Returns nonzero if debugging information should be output.  */
362
363 #ifdef ENABLE_CHECKING
364
365 static inline bool
366 cp_lexer_debugging_p (cp_lexer *lexer)
367 {
368   return lexer->debugging_p;
369 }
370
371 #endif /* ENABLE_CHECKING */
372
373 static inline cp_token_position
374 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
375 {
376   gcc_assert (!previous_p || lexer->next_token != &eof_token);
377
378   return lexer->next_token - previous_p;
379 }
380
381 static inline cp_token *
382 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
383 {
384   return pos;
385 }
386
387 /* nonzero if we are presently saving tokens.  */
388
389 static inline int
390 cp_lexer_saving_tokens (const cp_lexer* lexer)
391 {
392   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 }
394
395 /* Store the next token from the preprocessor in *TOKEN.  Return true
396    if we reach EOF.  If LEXER is NULL, assume we are handling an
397    initial #pragma pch_preprocess, and thus want the lexer to return
398    processed strings.  */
399
400 static void
401 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
402 {
403   static int is_extern_c = 0;
404
405    /* Get a new token from the preprocessor.  */
406   token->type
407     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
408                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
409   token->keyword = RID_MAX;
410   token->pragma_kind = PRAGMA_NONE;
411   token->in_system_header = in_system_header;
412
413   /* On some systems, some header files are surrounded by an
414      implicit extern "C" block.  Set a flag in the token if it
415      comes from such a header.  */
416   is_extern_c += pending_lang_change;
417   pending_lang_change = 0;
418   token->implicit_extern_c = is_extern_c > 0;
419
420   /* Check to see if this token is a keyword.  */
421   if (token->type == CPP_NAME)
422     {
423       if (C_IS_RESERVED_WORD (token->u.value))
424         {
425           /* Mark this token as a keyword.  */
426           token->type = CPP_KEYWORD;
427           /* Record which keyword.  */
428           token->keyword = C_RID_CODE (token->u.value);
429           /* Update the value.  Some keywords are mapped to particular
430              entities, rather than simply having the value of the
431              corresponding IDENTIFIER_NODE.  For example, `__const' is
432              mapped to `const'.  */
433           token->u.value = ridpointers[token->keyword];
434         }
435       else
436         {
437           if (warn_cxx0x_compat
438               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
439               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
440             {
441               /* Warn about the C++0x keyword (but still treat it as
442                  an identifier).  */
443               warning (OPT_Wc__0x_compat, 
444                        "identifier %<%s%> will become a keyword in C++0x",
445                        IDENTIFIER_POINTER (token->u.value));
446
447               /* Clear out the C_RID_CODE so we don't warn about this
448                  particular identifier-turned-keyword again.  */
449               C_RID_CODE (token->u.value) = RID_MAX;
450             }
451
452           token->ambiguous_p = false;
453           token->keyword = RID_MAX;
454         }
455     }
456   /* Handle Objective-C++ keywords.  */
457   else if (token->type == CPP_AT_NAME)
458     {
459       token->type = CPP_KEYWORD;
460       switch (C_RID_CODE (token->u.value))
461         {
462         /* Map 'class' to '@class', 'private' to '@private', etc.  */
463         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
464         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
465         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
466         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
467         case RID_THROW: token->keyword = RID_AT_THROW; break;
468         case RID_TRY: token->keyword = RID_AT_TRY; break;
469         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
470         default: token->keyword = C_RID_CODE (token->u.value);
471         }
472     }
473   else if (token->type == CPP_PRAGMA)
474     {
475       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
476       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
477       token->u.value = NULL_TREE;
478     }
479 }
480
481 /* Update the globals input_location and in_system_header and the
482    input file stack from TOKEN.  */
483 static inline void
484 cp_lexer_set_source_position_from_token (cp_token *token)
485 {
486   if (token->type != CPP_EOF)
487     {
488       input_location = token->location;
489       in_system_header = token->in_system_header;
490     }
491 }
492
493 /* Return a pointer to the next token in the token stream, but do not
494    consume it.  */
495
496 static inline cp_token *
497 cp_lexer_peek_token (cp_lexer *lexer)
498 {
499   if (cp_lexer_debugging_p (lexer))
500     {
501       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
502       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
503       putc ('\n', cp_lexer_debug_stream);
504     }
505   return lexer->next_token;
506 }
507
508 /* Return true if the next token has the indicated TYPE.  */
509
510 static inline bool
511 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
512 {
513   return cp_lexer_peek_token (lexer)->type == type;
514 }
515
516 /* Return true if the next token does not have the indicated TYPE.  */
517
518 static inline bool
519 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
520 {
521   return !cp_lexer_next_token_is (lexer, type);
522 }
523
524 /* Return true if the next token is the indicated KEYWORD.  */
525
526 static inline bool
527 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
528 {
529   return cp_lexer_peek_token (lexer)->keyword == keyword;
530 }
531
532 /* Return true if the next token is a keyword for a decl-specifier.  */
533
534 static bool
535 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
536 {
537   cp_token *token;
538
539   token = cp_lexer_peek_token (lexer);
540   switch (token->keyword) 
541     {
542       /* auto specifier: storage-class-specifier in C++,
543          simple-type-specifier in C++0x.  */
544     case RID_AUTO:
545       /* Storage classes.  */
546     case RID_REGISTER:
547     case RID_STATIC:
548     case RID_EXTERN:
549     case RID_MUTABLE:
550     case RID_THREAD:
551       /* Elaborated type specifiers.  */
552     case RID_ENUM:
553     case RID_CLASS:
554     case RID_STRUCT:
555     case RID_UNION:
556     case RID_TYPENAME:
557       /* Simple type specifiers.  */
558     case RID_CHAR:
559     case RID_WCHAR:
560     case RID_BOOL:
561     case RID_SHORT:
562     case RID_INT:
563     case RID_LONG:
564     case RID_SIGNED:
565     case RID_UNSIGNED:
566     case RID_FLOAT:
567     case RID_DOUBLE:
568     case RID_VOID:
569       /* GNU extensions.  */ 
570     case RID_ATTRIBUTE:
571     case RID_TYPEOF:
572       /* C++0x extensions.  */
573     case RID_DECLTYPE:
574       return true;
575
576     default:
577       return false;
578     }
579 }
580
581 /* Return a pointer to the Nth token in the token stream.  If N is 1,
582    then this is precisely equivalent to cp_lexer_peek_token (except
583    that it is not inline).  One would like to disallow that case, but
584    there is one case (cp_parser_nth_token_starts_template_id) where
585    the caller passes a variable for N and it might be 1.  */
586
587 static cp_token *
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
589 {
590   cp_token *token;
591
592   /* N is 1-based, not zero-based.  */
593   gcc_assert (n > 0);
594
595   if (cp_lexer_debugging_p (lexer))
596     fprintf (cp_lexer_debug_stream,
597              "cp_lexer: peeking ahead %ld at token: ", (long)n);
598
599   --n;
600   token = lexer->next_token;
601   gcc_assert (!n || token != &eof_token);
602   while (n != 0)
603     {
604       ++token;
605       if (token == lexer->last_token)
606         {
607           token = &eof_token;
608           break;
609         }
610
611       if (token->type != CPP_PURGED)
612         --n;
613     }
614
615   if (cp_lexer_debugging_p (lexer))
616     {
617       cp_lexer_print_token (cp_lexer_debug_stream, token);
618       putc ('\n', cp_lexer_debug_stream);
619     }
620
621   return token;
622 }
623
624 /* Return the next token, and advance the lexer's next_token pointer
625    to point to the next non-purged token.  */
626
627 static cp_token *
628 cp_lexer_consume_token (cp_lexer* lexer)
629 {
630   cp_token *token = lexer->next_token;
631
632   gcc_assert (token != &eof_token);
633   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
634
635   do
636     {
637       lexer->next_token++;
638       if (lexer->next_token == lexer->last_token)
639         {
640           lexer->next_token = &eof_token;
641           break;
642         }
643
644     }
645   while (lexer->next_token->type == CPP_PURGED);
646
647   cp_lexer_set_source_position_from_token (token);
648
649   /* Provide debugging output.  */
650   if (cp_lexer_debugging_p (lexer))
651     {
652       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653       cp_lexer_print_token (cp_lexer_debug_stream, token);
654       putc ('\n', cp_lexer_debug_stream);
655     }
656
657   return token;
658 }
659
660 /* Permanently remove the next token from the token stream, and
661    advance the next_token pointer to refer to the next non-purged
662    token.  */
663
664 static void
665 cp_lexer_purge_token (cp_lexer *lexer)
666 {
667   cp_token *tok = lexer->next_token;
668
669   gcc_assert (tok != &eof_token);
670   tok->type = CPP_PURGED;
671   tok->location = UNKNOWN_LOCATION;
672   tok->u.value = NULL_TREE;
673   tok->keyword = RID_MAX;
674
675   do
676     {
677       tok++;
678       if (tok == lexer->last_token)
679         {
680           tok = &eof_token;
681           break;
682         }
683     }
684   while (tok->type == CPP_PURGED);
685   lexer->next_token = tok;
686 }
687
688 /* Permanently remove all tokens after TOK, up to, but not
689    including, the token that will be returned next by
690    cp_lexer_peek_token.  */
691
692 static void
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
694 {
695   cp_token *peek = lexer->next_token;
696
697   if (peek == &eof_token)
698     peek = lexer->last_token;
699
700   gcc_assert (tok < peek);
701
702   for ( tok += 1; tok != peek; tok += 1)
703     {
704       tok->type = CPP_PURGED;
705       tok->location = UNKNOWN_LOCATION;
706       tok->u.value = NULL_TREE;
707       tok->keyword = RID_MAX;
708     }
709 }
710
711 /* Begin saving tokens.  All tokens consumed after this point will be
712    preserved.  */
713
714 static void
715 cp_lexer_save_tokens (cp_lexer* lexer)
716 {
717   /* Provide debugging output.  */
718   if (cp_lexer_debugging_p (lexer))
719     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
720
721   VEC_safe_push (cp_token_position, heap,
722                  lexer->saved_tokens, lexer->next_token);
723 }
724
725 /* Commit to the portion of the token stream most recently saved.  */
726
727 static void
728 cp_lexer_commit_tokens (cp_lexer* lexer)
729 {
730   /* Provide debugging output.  */
731   if (cp_lexer_debugging_p (lexer))
732     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
733
734   VEC_pop (cp_token_position, lexer->saved_tokens);
735 }
736
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738    to the token stream.  Stop saving tokens.  */
739
740 static void
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
742 {
743   /* Provide debugging output.  */
744   if (cp_lexer_debugging_p (lexer))
745     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
746
747   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
748 }
749
750 /* Print a representation of the TOKEN on the STREAM.  */
751
752 #ifdef ENABLE_CHECKING
753
754 static void
755 cp_lexer_print_token (FILE * stream, cp_token *token)
756 {
757   /* We don't use cpp_type2name here because the parser defines
758      a few tokens of its own.  */
759   static const char *const token_names[] = {
760     /* cpplib-defined token types */
761 #define OP(e, s) #e,
762 #define TK(e, s) #e,
763     TTYPE_TABLE
764 #undef OP
765 #undef TK
766     /* C++ parser token types - see "Manifest constants", above.  */
767     "KEYWORD",
768     "TEMPLATE_ID",
769     "NESTED_NAME_SPECIFIER",
770     "PURGED"
771   };
772
773   /* If we have a name for the token, print it out.  Otherwise, we
774      simply give the numeric code.  */
775   gcc_assert (token->type < ARRAY_SIZE(token_names));
776   fputs (token_names[token->type], stream);
777
778   /* For some tokens, print the associated data.  */
779   switch (token->type)
780     {
781     case CPP_KEYWORD:
782       /* Some keywords have a value that is not an IDENTIFIER_NODE.
783          For example, `struct' is mapped to an INTEGER_CST.  */
784       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
785         break;
786       /* else fall through */
787     case CPP_NAME:
788       fputs (IDENTIFIER_POINTER (token->u.value), stream);
789       break;
790
791     case CPP_STRING:
792     case CPP_WSTRING:
793       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
794       break;
795
796     default:
797       break;
798     }
799 }
800
801 /* Start emitting debugging information.  */
802
803 static void
804 cp_lexer_start_debugging (cp_lexer* lexer)
805 {
806   lexer->debugging_p = true;
807 }
808
809 /* Stop emitting debugging information.  */
810
811 static void
812 cp_lexer_stop_debugging (cp_lexer* lexer)
813 {
814   lexer->debugging_p = false;
815 }
816
817 #endif /* ENABLE_CHECKING */
818
819 /* Create a new cp_token_cache, representing a range of tokens.  */
820
821 static cp_token_cache *
822 cp_token_cache_new (cp_token *first, cp_token *last)
823 {
824   cp_token_cache *cache = GGC_NEW (cp_token_cache);
825   cache->first = first;
826   cache->last = last;
827   return cache;
828 }
829
830 \f
831 /* Decl-specifiers.  */
832
833 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
834
835 static void
836 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
837 {
838   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
839 }
840
841 /* Declarators.  */
842
843 /* Nothing other than the parser should be creating declarators;
844    declarators are a semi-syntactic representation of C++ entities.
845    Other parts of the front end that need to create entities (like
846    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
847
848 static cp_declarator *make_call_declarator
849   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
850 static cp_declarator *make_array_declarator
851   (cp_declarator *, tree);
852 static cp_declarator *make_pointer_declarator
853   (cp_cv_quals, cp_declarator *);
854 static cp_declarator *make_reference_declarator
855   (cp_cv_quals, cp_declarator *, bool);
856 static cp_parameter_declarator *make_parameter_declarator
857   (cp_decl_specifier_seq *, cp_declarator *, tree);
858 static cp_declarator *make_ptrmem_declarator
859   (cp_cv_quals, tree, cp_declarator *);
860
861 /* An erroneous declarator.  */
862 static cp_declarator *cp_error_declarator;
863
864 /* The obstack on which declarators and related data structures are
865    allocated.  */
866 static struct obstack declarator_obstack;
867
868 /* Alloc BYTES from the declarator memory pool.  */
869
870 static inline void *
871 alloc_declarator (size_t bytes)
872 {
873   return obstack_alloc (&declarator_obstack, bytes);
874 }
875
876 /* Allocate a declarator of the indicated KIND.  Clear fields that are
877    common to all declarators.  */
878
879 static cp_declarator *
880 make_declarator (cp_declarator_kind kind)
881 {
882   cp_declarator *declarator;
883
884   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
885   declarator->kind = kind;
886   declarator->attributes = NULL_TREE;
887   declarator->declarator = NULL;
888   declarator->parameter_pack_p = false;
889
890   return declarator;
891 }
892
893 /* Make a declarator for a generalized identifier.  If
894    QUALIFYING_SCOPE is non-NULL, the identifier is
895    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
896    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
897    is, if any.   */
898
899 static cp_declarator *
900 make_id_declarator (tree qualifying_scope, tree unqualified_name,
901                     special_function_kind sfk)
902 {
903   cp_declarator *declarator;
904
905   /* It is valid to write:
906
907        class C { void f(); };
908        typedef C D;
909        void D::f();
910
911      The standard is not clear about whether `typedef const C D' is
912      legal; as of 2002-09-15 the committee is considering that
913      question.  EDG 3.0 allows that syntax.  Therefore, we do as
914      well.  */
915   if (qualifying_scope && TYPE_P (qualifying_scope))
916     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
917
918   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
919               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
920               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
921
922   declarator = make_declarator (cdk_id);
923   declarator->u.id.qualifying_scope = qualifying_scope;
924   declarator->u.id.unqualified_name = unqualified_name;
925   declarator->u.id.sfk = sfk;
926   
927   return declarator;
928 }
929
930 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
931    of modifiers such as const or volatile to apply to the pointer
932    type, represented as identifiers.  */
933
934 cp_declarator *
935 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
936 {
937   cp_declarator *declarator;
938
939   declarator = make_declarator (cdk_pointer);
940   declarator->declarator = target;
941   declarator->u.pointer.qualifiers = cv_qualifiers;
942   declarator->u.pointer.class_type = NULL_TREE;
943   if (target)
944     {
945       declarator->parameter_pack_p = target->parameter_pack_p;
946       target->parameter_pack_p = false;
947     }
948   else
949     declarator->parameter_pack_p = false;
950
951   return declarator;
952 }
953
954 /* Like make_pointer_declarator -- but for references.  */
955
956 cp_declarator *
957 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
958                            bool rvalue_ref)
959 {
960   cp_declarator *declarator;
961
962   declarator = make_declarator (cdk_reference);
963   declarator->declarator = target;
964   declarator->u.reference.qualifiers = cv_qualifiers;
965   declarator->u.reference.rvalue_ref = rvalue_ref;
966   if (target)
967     {
968       declarator->parameter_pack_p = target->parameter_pack_p;
969       target->parameter_pack_p = false;
970     }
971   else
972     declarator->parameter_pack_p = false;
973
974   return declarator;
975 }
976
977 /* Like make_pointer_declarator -- but for a pointer to a non-static
978    member of CLASS_TYPE.  */
979
980 cp_declarator *
981 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
982                         cp_declarator *pointee)
983 {
984   cp_declarator *declarator;
985
986   declarator = make_declarator (cdk_ptrmem);
987   declarator->declarator = pointee;
988   declarator->u.pointer.qualifiers = cv_qualifiers;
989   declarator->u.pointer.class_type = class_type;
990
991   if (pointee)
992     {
993       declarator->parameter_pack_p = pointee->parameter_pack_p;
994       pointee->parameter_pack_p = false;
995     }
996   else
997     declarator->parameter_pack_p = false;
998
999   return declarator;
1000 }
1001
1002 /* Make a declarator for the function given by TARGET, with the
1003    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1004    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1005    indicates what exceptions can be thrown.  */
1006
1007 cp_declarator *
1008 make_call_declarator (cp_declarator *target,
1009                       cp_parameter_declarator *parms,
1010                       cp_cv_quals cv_qualifiers,
1011                       tree exception_specification)
1012 {
1013   cp_declarator *declarator;
1014
1015   declarator = make_declarator (cdk_function);
1016   declarator->declarator = target;
1017   declarator->u.function.parameters = parms;
1018   declarator->u.function.qualifiers = cv_qualifiers;
1019   declarator->u.function.exception_specification = exception_specification;
1020   if (target)
1021     {
1022       declarator->parameter_pack_p = target->parameter_pack_p;
1023       target->parameter_pack_p = false;
1024     }
1025   else
1026     declarator->parameter_pack_p = false;
1027
1028   return declarator;
1029 }
1030
1031 /* Make a declarator for an array of BOUNDS elements, each of which is
1032    defined by ELEMENT.  */
1033
1034 cp_declarator *
1035 make_array_declarator (cp_declarator *element, tree bounds)
1036 {
1037   cp_declarator *declarator;
1038
1039   declarator = make_declarator (cdk_array);
1040   declarator->declarator = element;
1041   declarator->u.array.bounds = bounds;
1042   if (element)
1043     {
1044       declarator->parameter_pack_p = element->parameter_pack_p;
1045       element->parameter_pack_p = false;
1046     }
1047   else
1048     declarator->parameter_pack_p = false;
1049
1050   return declarator;
1051 }
1052
1053 /* Determine whether the declarator we've seen so far can be a
1054    parameter pack, when followed by an ellipsis.  */
1055 static bool 
1056 declarator_can_be_parameter_pack (cp_declarator *declarator)
1057 {
1058   /* Search for a declarator name, or any other declarator that goes
1059      after the point where the ellipsis could appear in a parameter
1060      pack. If we find any of these, then this declarator can not be
1061      made into a parameter pack.  */
1062   bool found = false;
1063   while (declarator && !found)
1064     {
1065       switch ((int)declarator->kind)
1066         {
1067         case cdk_id:
1068         case cdk_array:
1069           found = true;
1070           break;
1071
1072         case cdk_error:
1073           return true;
1074
1075         default:
1076           declarator = declarator->declarator;
1077           break;
1078         }
1079     }
1080
1081   return !found;
1082 }
1083
1084 cp_parameter_declarator *no_parameters;
1085
1086 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1087    DECLARATOR and DEFAULT_ARGUMENT.  */
1088
1089 cp_parameter_declarator *
1090 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1091                            cp_declarator *declarator,
1092                            tree default_argument)
1093 {
1094   cp_parameter_declarator *parameter;
1095
1096   parameter = ((cp_parameter_declarator *)
1097                alloc_declarator (sizeof (cp_parameter_declarator)));
1098   parameter->next = NULL;
1099   if (decl_specifiers)
1100     parameter->decl_specifiers = *decl_specifiers;
1101   else
1102     clear_decl_specs (&parameter->decl_specifiers);
1103   parameter->declarator = declarator;
1104   parameter->default_argument = default_argument;
1105   parameter->ellipsis_p = false;
1106
1107   return parameter;
1108 }
1109
1110 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1111
1112 static bool
1113 function_declarator_p (const cp_declarator *declarator)
1114 {
1115   while (declarator)
1116     {
1117       if (declarator->kind == cdk_function
1118           && declarator->declarator->kind == cdk_id)
1119         return true;
1120       if (declarator->kind == cdk_id
1121           || declarator->kind == cdk_error)
1122         return false;
1123       declarator = declarator->declarator;
1124     }
1125   return false;
1126 }
1127  
1128 /* The parser.  */
1129
1130 /* Overview
1131    --------
1132
1133    A cp_parser parses the token stream as specified by the C++
1134    grammar.  Its job is purely parsing, not semantic analysis.  For
1135    example, the parser breaks the token stream into declarators,
1136    expressions, statements, and other similar syntactic constructs.
1137    It does not check that the types of the expressions on either side
1138    of an assignment-statement are compatible, or that a function is
1139    not declared with a parameter of type `void'.
1140
1141    The parser invokes routines elsewhere in the compiler to perform
1142    semantic analysis and to build up the abstract syntax tree for the
1143    code processed.
1144
1145    The parser (and the template instantiation code, which is, in a
1146    way, a close relative of parsing) are the only parts of the
1147    compiler that should be calling push_scope and pop_scope, or
1148    related functions.  The parser (and template instantiation code)
1149    keeps track of what scope is presently active; everything else
1150    should simply honor that.  (The code that generates static
1151    initializers may also need to set the scope, in order to check
1152    access control correctly when emitting the initializers.)
1153
1154    Methodology
1155    -----------
1156
1157    The parser is of the standard recursive-descent variety.  Upcoming
1158    tokens in the token stream are examined in order to determine which
1159    production to use when parsing a non-terminal.  Some C++ constructs
1160    require arbitrary look ahead to disambiguate.  For example, it is
1161    impossible, in the general case, to tell whether a statement is an
1162    expression or declaration without scanning the entire statement.
1163    Therefore, the parser is capable of "parsing tentatively."  When the
1164    parser is not sure what construct comes next, it enters this mode.
1165    Then, while we attempt to parse the construct, the parser queues up
1166    error messages, rather than issuing them immediately, and saves the
1167    tokens it consumes.  If the construct is parsed successfully, the
1168    parser "commits", i.e., it issues any queued error messages and
1169    the tokens that were being preserved are permanently discarded.
1170    If, however, the construct is not parsed successfully, the parser
1171    rolls back its state completely so that it can resume parsing using
1172    a different alternative.
1173
1174    Future Improvements
1175    -------------------
1176
1177    The performance of the parser could probably be improved substantially.
1178    We could often eliminate the need to parse tentatively by looking ahead
1179    a little bit.  In some places, this approach might not entirely eliminate
1180    the need to parse tentatively, but it might still speed up the average
1181    case.  */
1182
1183 /* Flags that are passed to some parsing functions.  These values can
1184    be bitwise-ored together.  */
1185
1186 typedef enum cp_parser_flags
1187 {
1188   /* No flags.  */
1189   CP_PARSER_FLAGS_NONE = 0x0,
1190   /* The construct is optional.  If it is not present, then no error
1191      should be issued.  */
1192   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1193   /* When parsing a type-specifier, do not allow user-defined types.  */
1194   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1195 } cp_parser_flags;
1196
1197 /* The different kinds of declarators we want to parse.  */
1198
1199 typedef enum cp_parser_declarator_kind
1200 {
1201   /* We want an abstract declarator.  */
1202   CP_PARSER_DECLARATOR_ABSTRACT,
1203   /* We want a named declarator.  */
1204   CP_PARSER_DECLARATOR_NAMED,
1205   /* We don't mind, but the name must be an unqualified-id.  */
1206   CP_PARSER_DECLARATOR_EITHER
1207 } cp_parser_declarator_kind;
1208
1209 /* The precedence values used to parse binary expressions.  The minimum value
1210    of PREC must be 1, because zero is reserved to quickly discriminate
1211    binary operators from other tokens.  */
1212
1213 enum cp_parser_prec
1214 {
1215   PREC_NOT_OPERATOR,
1216   PREC_LOGICAL_OR_EXPRESSION,
1217   PREC_LOGICAL_AND_EXPRESSION,
1218   PREC_INCLUSIVE_OR_EXPRESSION,
1219   PREC_EXCLUSIVE_OR_EXPRESSION,
1220   PREC_AND_EXPRESSION,
1221   PREC_EQUALITY_EXPRESSION,
1222   PREC_RELATIONAL_EXPRESSION,
1223   PREC_SHIFT_EXPRESSION,
1224   PREC_ADDITIVE_EXPRESSION,
1225   PREC_MULTIPLICATIVE_EXPRESSION,
1226   PREC_PM_EXPRESSION,
1227   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1228 };
1229
1230 /* A mapping from a token type to a corresponding tree node type, with a
1231    precedence value.  */
1232
1233 typedef struct cp_parser_binary_operations_map_node
1234 {
1235   /* The token type.  */
1236   enum cpp_ttype token_type;
1237   /* The corresponding tree code.  */
1238   enum tree_code tree_type;
1239   /* The precedence of this operator.  */
1240   enum cp_parser_prec prec;
1241 } cp_parser_binary_operations_map_node;
1242
1243 /* The status of a tentative parse.  */
1244
1245 typedef enum cp_parser_status_kind
1246 {
1247   /* No errors have occurred.  */
1248   CP_PARSER_STATUS_KIND_NO_ERROR,
1249   /* An error has occurred.  */
1250   CP_PARSER_STATUS_KIND_ERROR,
1251   /* We are committed to this tentative parse, whether or not an error
1252      has occurred.  */
1253   CP_PARSER_STATUS_KIND_COMMITTED
1254 } cp_parser_status_kind;
1255
1256 typedef struct cp_parser_expression_stack_entry
1257 {
1258   /* Left hand side of the binary operation we are currently
1259      parsing.  */
1260   tree lhs;
1261   /* Original tree code for left hand side, if it was a binary
1262      expression itself (used for -Wparentheses).  */
1263   enum tree_code lhs_type;
1264   /* Tree code for the binary operation we are parsing.  */
1265   enum tree_code tree_type;
1266   /* Precedence of the binary operation we are parsing.  */
1267   int prec;
1268 } cp_parser_expression_stack_entry;
1269
1270 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1271    entries because precedence levels on the stack are monotonically
1272    increasing.  */
1273 typedef struct cp_parser_expression_stack_entry
1274   cp_parser_expression_stack[NUM_PREC_VALUES];
1275
1276 /* Context that is saved and restored when parsing tentatively.  */
1277 typedef struct cp_parser_context GTY (())
1278 {
1279   /* If this is a tentative parsing context, the status of the
1280      tentative parse.  */
1281   enum cp_parser_status_kind status;
1282   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1283      that are looked up in this context must be looked up both in the
1284      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1285      the context of the containing expression.  */
1286   tree object_type;
1287
1288   /* The next parsing context in the stack.  */
1289   struct cp_parser_context *next;
1290 } cp_parser_context;
1291
1292 /* Prototypes.  */
1293
1294 /* Constructors and destructors.  */
1295
1296 static cp_parser_context *cp_parser_context_new
1297   (cp_parser_context *);
1298
1299 /* Class variables.  */
1300
1301 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1302
1303 /* The operator-precedence table used by cp_parser_binary_expression.
1304    Transformed into an associative array (binops_by_token) by
1305    cp_parser_new.  */
1306
1307 static const cp_parser_binary_operations_map_node binops[] = {
1308   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1309   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1310
1311   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1312   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1313   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1314
1315   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1316   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1317
1318   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1319   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1320
1321   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1322   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1323   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1324   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1325
1326   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1327   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1328
1329   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1330
1331   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1332
1333   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1334
1335   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1336
1337   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1338 };
1339
1340 /* The same as binops, but initialized by cp_parser_new so that
1341    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1342    for speed.  */
1343 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1344
1345 /* Constructors and destructors.  */
1346
1347 /* Construct a new context.  The context below this one on the stack
1348    is given by NEXT.  */
1349
1350 static cp_parser_context *
1351 cp_parser_context_new (cp_parser_context* next)
1352 {
1353   cp_parser_context *context;
1354
1355   /* Allocate the storage.  */
1356   if (cp_parser_context_free_list != NULL)
1357     {
1358       /* Pull the first entry from the free list.  */
1359       context = cp_parser_context_free_list;
1360       cp_parser_context_free_list = context->next;
1361       memset (context, 0, sizeof (*context));
1362     }
1363   else
1364     context = GGC_CNEW (cp_parser_context);
1365
1366   /* No errors have occurred yet in this context.  */
1367   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1368   /* If this is not the bottomost context, copy information that we
1369      need from the previous context.  */
1370   if (next)
1371     {
1372       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1373          expression, then we are parsing one in this context, too.  */
1374       context->object_type = next->object_type;
1375       /* Thread the stack.  */
1376       context->next = next;
1377     }
1378
1379   return context;
1380 }
1381
1382 /* The cp_parser structure represents the C++ parser.  */
1383
1384 typedef struct cp_parser GTY(())
1385 {
1386   /* The lexer from which we are obtaining tokens.  */
1387   cp_lexer *lexer;
1388
1389   /* The scope in which names should be looked up.  If NULL_TREE, then
1390      we look up names in the scope that is currently open in the
1391      source program.  If non-NULL, this is either a TYPE or
1392      NAMESPACE_DECL for the scope in which we should look.  It can
1393      also be ERROR_MARK, when we've parsed a bogus scope.
1394
1395      This value is not cleared automatically after a name is looked
1396      up, so we must be careful to clear it before starting a new look
1397      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1398      will look up `Z' in the scope of `X', rather than the current
1399      scope.)  Unfortunately, it is difficult to tell when name lookup
1400      is complete, because we sometimes peek at a token, look it up,
1401      and then decide not to consume it.   */
1402   tree scope;
1403
1404   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1405      last lookup took place.  OBJECT_SCOPE is used if an expression
1406      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1407      respectively.  QUALIFYING_SCOPE is used for an expression of the
1408      form "X::Y"; it refers to X.  */
1409   tree object_scope;
1410   tree qualifying_scope;
1411
1412   /* A stack of parsing contexts.  All but the bottom entry on the
1413      stack will be tentative contexts.
1414
1415      We parse tentatively in order to determine which construct is in
1416      use in some situations.  For example, in order to determine
1417      whether a statement is an expression-statement or a
1418      declaration-statement we parse it tentatively as a
1419      declaration-statement.  If that fails, we then reparse the same
1420      token stream as an expression-statement.  */
1421   cp_parser_context *context;
1422
1423   /* True if we are parsing GNU C++.  If this flag is not set, then
1424      GNU extensions are not recognized.  */
1425   bool allow_gnu_extensions_p;
1426
1427   /* TRUE if the `>' token should be interpreted as the greater-than
1428      operator.  FALSE if it is the end of a template-id or
1429      template-parameter-list. In C++0x mode, this flag also applies to
1430      `>>' tokens, which are viewed as two consecutive `>' tokens when
1431      this flag is FALSE.  */
1432   bool greater_than_is_operator_p;
1433
1434   /* TRUE if default arguments are allowed within a parameter list
1435      that starts at this point. FALSE if only a gnu extension makes
1436      them permissible.  */
1437   bool default_arg_ok_p;
1438
1439   /* TRUE if we are parsing an integral constant-expression.  See
1440      [expr.const] for a precise definition.  */
1441   bool integral_constant_expression_p;
1442
1443   /* TRUE if we are parsing an integral constant-expression -- but a
1444      non-constant expression should be permitted as well.  This flag
1445      is used when parsing an array bound so that GNU variable-length
1446      arrays are tolerated.  */
1447   bool allow_non_integral_constant_expression_p;
1448
1449   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1450      been seen that makes the expression non-constant.  */
1451   bool non_integral_constant_expression_p;
1452
1453   /* TRUE if local variable names and `this' are forbidden in the
1454      current context.  */
1455   bool local_variables_forbidden_p;
1456
1457   /* TRUE if the declaration we are parsing is part of a
1458      linkage-specification of the form `extern string-literal
1459      declaration'.  */
1460   bool in_unbraced_linkage_specification_p;
1461
1462   /* TRUE if we are presently parsing a declarator, after the
1463      direct-declarator.  */
1464   bool in_declarator_p;
1465
1466   /* TRUE if we are presently parsing a template-argument-list.  */
1467   bool in_template_argument_list_p;
1468
1469   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1470      to IN_OMP_BLOCK if parsing OpenMP structured block and
1471      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1472      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1473      iteration-statement, OpenMP block or loop within that switch.  */
1474 #define IN_SWITCH_STMT          1
1475 #define IN_ITERATION_STMT       2
1476 #define IN_OMP_BLOCK            4
1477 #define IN_OMP_FOR              8
1478 #define IN_IF_STMT             16
1479   unsigned char in_statement;
1480
1481   /* TRUE if we are presently parsing the body of a switch statement.
1482      Note that this doesn't quite overlap with in_statement above.
1483      The difference relates to giving the right sets of error messages:
1484      "case not in switch" vs "break statement used with OpenMP...".  */
1485   bool in_switch_statement_p;
1486
1487   /* TRUE if we are parsing a type-id in an expression context.  In
1488      such a situation, both "type (expr)" and "type (type)" are valid
1489      alternatives.  */
1490   bool in_type_id_in_expr_p;
1491
1492   /* TRUE if we are currently in a header file where declarations are
1493      implicitly extern "C".  */
1494   bool implicit_extern_c;
1495
1496   /* TRUE if strings in expressions should be translated to the execution
1497      character set.  */
1498   bool translate_strings_p;
1499
1500   /* TRUE if we are presently parsing the body of a function, but not
1501      a local class.  */
1502   bool in_function_body;
1503
1504   /* If non-NULL, then we are parsing a construct where new type
1505      definitions are not permitted.  The string stored here will be
1506      issued as an error message if a type is defined.  */
1507   const char *type_definition_forbidden_message;
1508
1509   /* A list of lists. The outer list is a stack, used for member
1510      functions of local classes. At each level there are two sub-list,
1511      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1512      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1513      TREE_VALUE's. The functions are chained in reverse declaration
1514      order.
1515
1516      The TREE_PURPOSE sublist contains those functions with default
1517      arguments that need post processing, and the TREE_VALUE sublist
1518      contains those functions with definitions that need post
1519      processing.
1520
1521      These lists can only be processed once the outermost class being
1522      defined is complete.  */
1523   tree unparsed_functions_queues;
1524
1525   /* The number of classes whose definitions are currently in
1526      progress.  */
1527   unsigned num_classes_being_defined;
1528
1529   /* The number of template parameter lists that apply directly to the
1530      current declaration.  */
1531   unsigned num_template_parameter_lists;
1532 } cp_parser;
1533
1534 /* Prototypes.  */
1535
1536 /* Constructors and destructors.  */
1537
1538 static cp_parser *cp_parser_new
1539   (void);
1540
1541 /* Routines to parse various constructs.
1542
1543    Those that return `tree' will return the error_mark_node (rather
1544    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1545    Sometimes, they will return an ordinary node if error-recovery was
1546    attempted, even though a parse error occurred.  So, to check
1547    whether or not a parse error occurred, you should always use
1548    cp_parser_error_occurred.  If the construct is optional (indicated
1549    either by an `_opt' in the name of the function that does the
1550    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1551    the construct is not present.  */
1552
1553 /* Lexical conventions [gram.lex]  */
1554
1555 static tree cp_parser_identifier
1556   (cp_parser *);
1557 static tree cp_parser_string_literal
1558   (cp_parser *, bool, bool);
1559
1560 /* Basic concepts [gram.basic]  */
1561
1562 static bool cp_parser_translation_unit
1563   (cp_parser *);
1564
1565 /* Expressions [gram.expr]  */
1566
1567 static tree cp_parser_primary_expression
1568   (cp_parser *, bool, bool, bool, cp_id_kind *);
1569 static tree cp_parser_id_expression
1570   (cp_parser *, bool, bool, bool *, bool, bool);
1571 static tree cp_parser_unqualified_id
1572   (cp_parser *, bool, bool, bool, bool);
1573 static tree cp_parser_nested_name_specifier_opt
1574   (cp_parser *, bool, bool, bool, bool);
1575 static tree cp_parser_nested_name_specifier
1576   (cp_parser *, bool, bool, bool, bool);
1577 static tree cp_parser_class_or_namespace_name
1578   (cp_parser *, bool, bool, bool, bool, bool);
1579 static tree cp_parser_postfix_expression
1580   (cp_parser *, bool, bool, bool);
1581 static tree cp_parser_postfix_open_square_expression
1582   (cp_parser *, tree, bool);
1583 static tree cp_parser_postfix_dot_deref_expression
1584   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1585 static tree cp_parser_parenthesized_expression_list
1586   (cp_parser *, bool, bool, bool, bool *);
1587 static void cp_parser_pseudo_destructor_name
1588   (cp_parser *, tree *, tree *);
1589 static tree cp_parser_unary_expression
1590   (cp_parser *, bool, bool);
1591 static enum tree_code cp_parser_unary_operator
1592   (cp_token *);
1593 static tree cp_parser_new_expression
1594   (cp_parser *);
1595 static tree cp_parser_new_placement
1596   (cp_parser *);
1597 static tree cp_parser_new_type_id
1598   (cp_parser *, tree *);
1599 static cp_declarator *cp_parser_new_declarator_opt
1600   (cp_parser *);
1601 static cp_declarator *cp_parser_direct_new_declarator
1602   (cp_parser *);
1603 static tree cp_parser_new_initializer
1604   (cp_parser *);
1605 static tree cp_parser_delete_expression
1606   (cp_parser *);
1607 static tree cp_parser_cast_expression
1608   (cp_parser *, bool, bool);
1609 static tree cp_parser_binary_expression
1610   (cp_parser *, bool);
1611 static tree cp_parser_question_colon_clause
1612   (cp_parser *, tree);
1613 static tree cp_parser_assignment_expression
1614   (cp_parser *, bool);
1615 static enum tree_code cp_parser_assignment_operator_opt
1616   (cp_parser *);
1617 static tree cp_parser_expression
1618   (cp_parser *, bool);
1619 static tree cp_parser_constant_expression
1620   (cp_parser *, bool, bool *);
1621 static tree cp_parser_builtin_offsetof
1622   (cp_parser *);
1623
1624 /* Statements [gram.stmt.stmt]  */
1625
1626 static void cp_parser_statement
1627   (cp_parser *, tree, bool, bool *);
1628 static void cp_parser_label_for_labeled_statement
1629   (cp_parser *);
1630 static tree cp_parser_expression_statement
1631   (cp_parser *, tree);
1632 static tree cp_parser_compound_statement
1633   (cp_parser *, tree, bool);
1634 static void cp_parser_statement_seq_opt
1635   (cp_parser *, tree);
1636 static tree cp_parser_selection_statement
1637   (cp_parser *, bool *);
1638 static tree cp_parser_condition
1639   (cp_parser *);
1640 static tree cp_parser_iteration_statement
1641   (cp_parser *);
1642 static void cp_parser_for_init_statement
1643   (cp_parser *);
1644 static tree cp_parser_jump_statement
1645   (cp_parser *);
1646 static void cp_parser_declaration_statement
1647   (cp_parser *);
1648
1649 static tree cp_parser_implicitly_scoped_statement
1650   (cp_parser *, bool *);
1651 static void cp_parser_already_scoped_statement
1652   (cp_parser *);
1653
1654 /* Declarations [gram.dcl.dcl] */
1655
1656 static void cp_parser_declaration_seq_opt
1657   (cp_parser *);
1658 static void cp_parser_declaration
1659   (cp_parser *);
1660 static void cp_parser_block_declaration
1661   (cp_parser *, bool);
1662 static void cp_parser_simple_declaration
1663   (cp_parser *, bool);
1664 static void cp_parser_decl_specifier_seq
1665   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1666 static tree cp_parser_storage_class_specifier_opt
1667   (cp_parser *);
1668 static tree cp_parser_function_specifier_opt
1669   (cp_parser *, cp_decl_specifier_seq *);
1670 static tree cp_parser_type_specifier
1671   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1672    int *, bool *);
1673 static tree cp_parser_simple_type_specifier
1674   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1675 static tree cp_parser_type_name
1676   (cp_parser *);
1677 static tree cp_parser_nonclass_name 
1678   (cp_parser* parser);
1679 static tree cp_parser_elaborated_type_specifier
1680   (cp_parser *, bool, bool);
1681 static tree cp_parser_enum_specifier
1682   (cp_parser *);
1683 static void cp_parser_enumerator_list
1684   (cp_parser *, tree);
1685 static void cp_parser_enumerator_definition
1686   (cp_parser *, tree);
1687 static tree cp_parser_namespace_name
1688   (cp_parser *);
1689 static void cp_parser_namespace_definition
1690   (cp_parser *);
1691 static void cp_parser_namespace_body
1692   (cp_parser *);
1693 static tree cp_parser_qualified_namespace_specifier
1694   (cp_parser *);
1695 static void cp_parser_namespace_alias_definition
1696   (cp_parser *);
1697 static bool cp_parser_using_declaration
1698   (cp_parser *, bool);
1699 static void cp_parser_using_directive
1700   (cp_parser *);
1701 static void cp_parser_asm_definition
1702   (cp_parser *);
1703 static void cp_parser_linkage_specification
1704   (cp_parser *);
1705 static void cp_parser_static_assert
1706   (cp_parser *, bool);
1707 static tree cp_parser_decltype
1708   (cp_parser *);
1709
1710 /* Declarators [gram.dcl.decl] */
1711
1712 static tree cp_parser_init_declarator
1713   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1714 static cp_declarator *cp_parser_declarator
1715   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1716 static cp_declarator *cp_parser_direct_declarator
1717   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1718 static enum tree_code cp_parser_ptr_operator
1719   (cp_parser *, tree *, cp_cv_quals *);
1720 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1721   (cp_parser *);
1722 static tree cp_parser_declarator_id
1723   (cp_parser *, bool);
1724 static tree cp_parser_type_id
1725   (cp_parser *);
1726 static void cp_parser_type_specifier_seq
1727   (cp_parser *, bool, cp_decl_specifier_seq *);
1728 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1729   (cp_parser *);
1730 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1731   (cp_parser *, bool *);
1732 static cp_parameter_declarator *cp_parser_parameter_declaration
1733   (cp_parser *, bool, bool *);
1734 static tree cp_parser_default_argument 
1735   (cp_parser *, bool);
1736 static void cp_parser_function_body
1737   (cp_parser *);
1738 static tree cp_parser_initializer
1739   (cp_parser *, bool *, bool *);
1740 static tree cp_parser_initializer_clause
1741   (cp_parser *, bool *);
1742 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1743   (cp_parser *, bool *);
1744
1745 static bool cp_parser_ctor_initializer_opt_and_function_body
1746   (cp_parser *);
1747
1748 /* Classes [gram.class] */
1749
1750 static tree cp_parser_class_name
1751   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1752 static tree cp_parser_class_specifier
1753   (cp_parser *);
1754 static tree cp_parser_class_head
1755   (cp_parser *, bool *, tree *, tree *);
1756 static enum tag_types cp_parser_class_key
1757   (cp_parser *);
1758 static void cp_parser_member_specification_opt
1759   (cp_parser *);
1760 static void cp_parser_member_declaration
1761   (cp_parser *);
1762 static tree cp_parser_pure_specifier
1763   (cp_parser *);
1764 static tree cp_parser_constant_initializer
1765   (cp_parser *);
1766
1767 /* Derived classes [gram.class.derived] */
1768
1769 static tree cp_parser_base_clause
1770   (cp_parser *);
1771 static tree cp_parser_base_specifier
1772   (cp_parser *);
1773
1774 /* Special member functions [gram.special] */
1775
1776 static tree cp_parser_conversion_function_id
1777   (cp_parser *);
1778 static tree cp_parser_conversion_type_id
1779   (cp_parser *);
1780 static cp_declarator *cp_parser_conversion_declarator_opt
1781   (cp_parser *);
1782 static bool cp_parser_ctor_initializer_opt
1783   (cp_parser *);
1784 static void cp_parser_mem_initializer_list
1785   (cp_parser *);
1786 static tree cp_parser_mem_initializer
1787   (cp_parser *);
1788 static tree cp_parser_mem_initializer_id
1789   (cp_parser *);
1790
1791 /* Overloading [gram.over] */
1792
1793 static tree cp_parser_operator_function_id
1794   (cp_parser *);
1795 static tree cp_parser_operator
1796   (cp_parser *);
1797
1798 /* Templates [gram.temp] */
1799
1800 static void cp_parser_template_declaration
1801   (cp_parser *, bool);
1802 static tree cp_parser_template_parameter_list
1803   (cp_parser *);
1804 static tree cp_parser_template_parameter
1805   (cp_parser *, bool *, bool *);
1806 static tree cp_parser_type_parameter
1807   (cp_parser *, bool *);
1808 static tree cp_parser_template_id
1809   (cp_parser *, bool, bool, bool);
1810 static tree cp_parser_template_name
1811   (cp_parser *, bool, bool, bool, bool *);
1812 static tree cp_parser_template_argument_list
1813   (cp_parser *);
1814 static tree cp_parser_template_argument
1815   (cp_parser *);
1816 static void cp_parser_explicit_instantiation
1817   (cp_parser *);
1818 static void cp_parser_explicit_specialization
1819   (cp_parser *);
1820
1821 /* Exception handling [gram.exception] */
1822
1823 static tree cp_parser_try_block
1824   (cp_parser *);
1825 static bool cp_parser_function_try_block
1826   (cp_parser *);
1827 static void cp_parser_handler_seq
1828   (cp_parser *);
1829 static void cp_parser_handler
1830   (cp_parser *);
1831 static tree cp_parser_exception_declaration
1832   (cp_parser *);
1833 static tree cp_parser_throw_expression
1834   (cp_parser *);
1835 static tree cp_parser_exception_specification_opt
1836   (cp_parser *);
1837 static tree cp_parser_type_id_list
1838   (cp_parser *);
1839
1840 /* GNU Extensions */
1841
1842 static tree cp_parser_asm_specification_opt
1843   (cp_parser *);
1844 static tree cp_parser_asm_operand_list
1845   (cp_parser *);
1846 static tree cp_parser_asm_clobber_list
1847   (cp_parser *);
1848 static tree cp_parser_attributes_opt
1849   (cp_parser *);
1850 static tree cp_parser_attribute_list
1851   (cp_parser *);
1852 static bool cp_parser_extension_opt
1853   (cp_parser *, int *);
1854 static void cp_parser_label_declaration
1855   (cp_parser *);
1856
1857 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1858 static bool cp_parser_pragma
1859   (cp_parser *, enum pragma_context);
1860
1861 /* Objective-C++ Productions */
1862
1863 static tree cp_parser_objc_message_receiver
1864   (cp_parser *);
1865 static tree cp_parser_objc_message_args
1866   (cp_parser *);
1867 static tree cp_parser_objc_message_expression
1868   (cp_parser *);
1869 static tree cp_parser_objc_encode_expression
1870   (cp_parser *);
1871 static tree cp_parser_objc_defs_expression
1872   (cp_parser *);
1873 static tree cp_parser_objc_protocol_expression
1874   (cp_parser *);
1875 static tree cp_parser_objc_selector_expression
1876   (cp_parser *);
1877 static tree cp_parser_objc_expression
1878   (cp_parser *);
1879 static bool cp_parser_objc_selector_p
1880   (enum cpp_ttype);
1881 static tree cp_parser_objc_selector
1882   (cp_parser *);
1883 static tree cp_parser_objc_protocol_refs_opt
1884   (cp_parser *);
1885 static void cp_parser_objc_declaration
1886   (cp_parser *);
1887 static tree cp_parser_objc_statement
1888   (cp_parser *);
1889
1890 /* Utility Routines */
1891
1892 static tree cp_parser_lookup_name
1893   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1894 static tree cp_parser_lookup_name_simple
1895   (cp_parser *, tree);
1896 static tree cp_parser_maybe_treat_template_as_class
1897   (tree, bool);
1898 static bool cp_parser_check_declarator_template_parameters
1899   (cp_parser *, cp_declarator *);
1900 static bool cp_parser_check_template_parameters
1901   (cp_parser *, unsigned);
1902 static tree cp_parser_simple_cast_expression
1903   (cp_parser *);
1904 static tree cp_parser_global_scope_opt
1905   (cp_parser *, bool);
1906 static bool cp_parser_constructor_declarator_p
1907   (cp_parser *, bool);
1908 static tree cp_parser_function_definition_from_specifiers_and_declarator
1909   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1910 static tree cp_parser_function_definition_after_declarator
1911   (cp_parser *, bool);
1912 static void cp_parser_template_declaration_after_export
1913   (cp_parser *, bool);
1914 static void cp_parser_perform_template_parameter_access_checks
1915   (VEC (deferred_access_check,gc)*);
1916 static tree cp_parser_single_declaration
1917   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1918 static tree cp_parser_functional_cast
1919   (cp_parser *, tree);
1920 static tree cp_parser_save_member_function_body
1921   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1922 static tree cp_parser_enclosed_template_argument_list
1923   (cp_parser *);
1924 static void cp_parser_save_default_args
1925   (cp_parser *, tree);
1926 static void cp_parser_late_parsing_for_member
1927   (cp_parser *, tree);
1928 static void cp_parser_late_parsing_default_args
1929   (cp_parser *, tree);
1930 static tree cp_parser_sizeof_operand
1931   (cp_parser *, enum rid);
1932 static tree cp_parser_trait_expr
1933   (cp_parser *, enum rid);
1934 static bool cp_parser_declares_only_class_p
1935   (cp_parser *);
1936 static void cp_parser_set_storage_class
1937   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1938 static void cp_parser_set_decl_spec_type
1939   (cp_decl_specifier_seq *, tree, bool);
1940 static bool cp_parser_friend_p
1941   (const cp_decl_specifier_seq *);
1942 static cp_token *cp_parser_require
1943   (cp_parser *, enum cpp_ttype, const char *);
1944 static cp_token *cp_parser_require_keyword
1945   (cp_parser *, enum rid, const char *);
1946 static bool cp_parser_token_starts_function_definition_p
1947   (cp_token *);
1948 static bool cp_parser_next_token_starts_class_definition_p
1949   (cp_parser *);
1950 static bool cp_parser_next_token_ends_template_argument_p
1951   (cp_parser *);
1952 static bool cp_parser_nth_token_starts_template_argument_list_p
1953   (cp_parser *, size_t);
1954 static enum tag_types cp_parser_token_is_class_key
1955   (cp_token *);
1956 static void cp_parser_check_class_key
1957   (enum tag_types, tree type);
1958 static void cp_parser_check_access_in_redeclaration
1959   (tree type);
1960 static bool cp_parser_optional_template_keyword
1961   (cp_parser *);
1962 static void cp_parser_pre_parsed_nested_name_specifier
1963   (cp_parser *);
1964 static void cp_parser_cache_group
1965   (cp_parser *, enum cpp_ttype, unsigned);
1966 static void cp_parser_parse_tentatively
1967   (cp_parser *);
1968 static void cp_parser_commit_to_tentative_parse
1969   (cp_parser *);
1970 static void cp_parser_abort_tentative_parse
1971   (cp_parser *);
1972 static bool cp_parser_parse_definitely
1973   (cp_parser *);
1974 static inline bool cp_parser_parsing_tentatively
1975   (cp_parser *);
1976 static bool cp_parser_uncommitted_to_tentative_parse_p
1977   (cp_parser *);
1978 static void cp_parser_error
1979   (cp_parser *, const char *);
1980 static void cp_parser_name_lookup_error
1981   (cp_parser *, tree, tree, const char *);
1982 static bool cp_parser_simulate_error
1983   (cp_parser *);
1984 static bool cp_parser_check_type_definition
1985   (cp_parser *);
1986 static void cp_parser_check_for_definition_in_return_type
1987   (cp_declarator *, tree);
1988 static void cp_parser_check_for_invalid_template_id
1989   (cp_parser *, tree);
1990 static bool cp_parser_non_integral_constant_expression
1991   (cp_parser *, const char *);
1992 static void cp_parser_diagnose_invalid_type_name
1993   (cp_parser *, tree, tree);
1994 static bool cp_parser_parse_and_diagnose_invalid_type_name
1995   (cp_parser *);
1996 static int cp_parser_skip_to_closing_parenthesis
1997   (cp_parser *, bool, bool, bool);
1998 static void cp_parser_skip_to_end_of_statement
1999   (cp_parser *);
2000 static void cp_parser_consume_semicolon_at_end_of_statement
2001   (cp_parser *);
2002 static void cp_parser_skip_to_end_of_block_or_statement
2003   (cp_parser *);
2004 static bool cp_parser_skip_to_closing_brace
2005   (cp_parser *);
2006 static void cp_parser_skip_to_end_of_template_parameter_list
2007   (cp_parser *);
2008 static void cp_parser_skip_to_pragma_eol
2009   (cp_parser*, cp_token *);
2010 static bool cp_parser_error_occurred
2011   (cp_parser *);
2012 static bool cp_parser_allow_gnu_extensions_p
2013   (cp_parser *);
2014 static bool cp_parser_is_string_literal
2015   (cp_token *);
2016 static bool cp_parser_is_keyword
2017   (cp_token *, enum rid);
2018 static tree cp_parser_make_typename_type
2019   (cp_parser *, tree, tree);
2020 static cp_declarator * cp_parser_make_indirect_declarator
2021   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2022
2023 /* Returns nonzero if we are parsing tentatively.  */
2024
2025 static inline bool
2026 cp_parser_parsing_tentatively (cp_parser* parser)
2027 {
2028   return parser->context->next != NULL;
2029 }
2030
2031 /* Returns nonzero if TOKEN is a string literal.  */
2032
2033 static bool
2034 cp_parser_is_string_literal (cp_token* token)
2035 {
2036   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2037 }
2038
2039 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2040
2041 static bool
2042 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2043 {
2044   return token->keyword == keyword;
2045 }
2046
2047 /* If not parsing tentatively, issue a diagnostic of the form
2048       FILE:LINE: MESSAGE before TOKEN
2049    where TOKEN is the next token in the input stream.  MESSAGE
2050    (specified by the caller) is usually of the form "expected
2051    OTHER-TOKEN".  */
2052
2053 static void
2054 cp_parser_error (cp_parser* parser, const char* message)
2055 {
2056   if (!cp_parser_simulate_error (parser))
2057     {
2058       cp_token *token = cp_lexer_peek_token (parser->lexer);
2059       /* This diagnostic makes more sense if it is tagged to the line
2060          of the token we just peeked at.  */
2061       cp_lexer_set_source_position_from_token (token);
2062
2063       if (token->type == CPP_PRAGMA)
2064         {
2065           error ("%<#pragma%> is not allowed here");
2066           cp_parser_skip_to_pragma_eol (parser, token);
2067           return;
2068         }
2069
2070       c_parse_error (message,
2071                      /* Because c_parser_error does not understand
2072                         CPP_KEYWORD, keywords are treated like
2073                         identifiers.  */
2074                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2075                      token->u.value);
2076     }
2077 }
2078
2079 /* Issue an error about name-lookup failing.  NAME is the
2080    IDENTIFIER_NODE DECL is the result of
2081    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2082    the thing that we hoped to find.  */
2083
2084 static void
2085 cp_parser_name_lookup_error (cp_parser* parser,
2086                              tree name,
2087                              tree decl,
2088                              const char* desired)
2089 {
2090   /* If name lookup completely failed, tell the user that NAME was not
2091      declared.  */
2092   if (decl == error_mark_node)
2093     {
2094       if (parser->scope && parser->scope != global_namespace)
2095         error ("%<%E::%E%> has not been declared",
2096                parser->scope, name);
2097       else if (parser->scope == global_namespace)
2098         error ("%<::%E%> has not been declared", name);
2099       else if (parser->object_scope
2100                && !CLASS_TYPE_P (parser->object_scope))
2101         error ("request for member %qE in non-class type %qT",
2102                name, parser->object_scope);
2103       else if (parser->object_scope)
2104         error ("%<%T::%E%> has not been declared",
2105                parser->object_scope, name);
2106       else
2107         error ("%qE has not been declared", name);
2108     }
2109   else if (parser->scope && parser->scope != global_namespace)
2110     error ("%<%E::%E%> %s", parser->scope, name, desired);
2111   else if (parser->scope == global_namespace)
2112     error ("%<::%E%> %s", name, desired);
2113   else
2114     error ("%qE %s", name, desired);
2115 }
2116
2117 /* If we are parsing tentatively, remember that an error has occurred
2118    during this tentative parse.  Returns true if the error was
2119    simulated; false if a message should be issued by the caller.  */
2120
2121 static bool
2122 cp_parser_simulate_error (cp_parser* parser)
2123 {
2124   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2125     {
2126       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2127       return true;
2128     }
2129   return false;
2130 }
2131
2132 /* Check for repeated decl-specifiers.  */
2133
2134 static void
2135 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2136 {
2137   cp_decl_spec ds;
2138
2139   for (ds = ds_first; ds != ds_last; ++ds)
2140     {
2141       unsigned count = decl_specs->specs[(int)ds];
2142       if (count < 2)
2143         continue;
2144       /* The "long" specifier is a special case because of "long long".  */
2145       if (ds == ds_long)
2146         {
2147           if (count > 2)
2148             error ("%<long long long%> is too long for GCC");
2149           else if (pedantic && !in_system_header && warn_long_long
2150                    && cxx_dialect == cxx98)
2151             pedwarn ("ISO C++ 1998 does not support %<long long%>");
2152         }
2153       else if (count > 1)
2154         {
2155           static const char *const decl_spec_names[] = {
2156             "signed",
2157             "unsigned",
2158             "short",
2159             "long",
2160             "const",
2161             "volatile",
2162             "restrict",
2163             "inline",
2164             "virtual",
2165             "explicit",
2166             "friend",
2167             "typedef",
2168             "__complex",
2169             "__thread"
2170           };
2171           error ("duplicate %qs", decl_spec_names[(int)ds]);
2172         }
2173     }
2174 }
2175
2176 /* This function is called when a type is defined.  If type
2177    definitions are forbidden at this point, an error message is
2178    issued.  */
2179
2180 static bool
2181 cp_parser_check_type_definition (cp_parser* parser)
2182 {
2183   /* If types are forbidden here, issue a message.  */
2184   if (parser->type_definition_forbidden_message)
2185     {
2186       /* Use `%s' to print the string in case there are any escape
2187          characters in the message.  */
2188       error ("%s", parser->type_definition_forbidden_message);
2189       return false;
2190     }
2191   return true;
2192 }
2193
2194 /* This function is called when the DECLARATOR is processed.  The TYPE
2195    was a type defined in the decl-specifiers.  If it is invalid to
2196    define a type in the decl-specifiers for DECLARATOR, an error is
2197    issued.  */
2198
2199 static void
2200 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2201                                                tree type)
2202 {
2203   /* [dcl.fct] forbids type definitions in return types.
2204      Unfortunately, it's not easy to know whether or not we are
2205      processing a return type until after the fact.  */
2206   while (declarator
2207          && (declarator->kind == cdk_pointer
2208              || declarator->kind == cdk_reference
2209              || declarator->kind == cdk_ptrmem))
2210     declarator = declarator->declarator;
2211   if (declarator
2212       && declarator->kind == cdk_function)
2213     {
2214       error ("new types may not be defined in a return type");
2215       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2216               type);
2217     }
2218 }
2219
2220 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2221    "<" in any valid C++ program.  If the next token is indeed "<",
2222    issue a message warning the user about what appears to be an
2223    invalid attempt to form a template-id.  */
2224
2225 static void
2226 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2227                                          tree type)
2228 {
2229   cp_token_position start = 0;
2230
2231   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2232     {
2233       if (TYPE_P (type))
2234         error ("%qT is not a template", type);
2235       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2236         error ("%qE is not a template", type);
2237       else
2238         error ("invalid template-id");
2239       /* Remember the location of the invalid "<".  */
2240       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2241         start = cp_lexer_token_position (parser->lexer, true);
2242       /* Consume the "<".  */
2243       cp_lexer_consume_token (parser->lexer);
2244       /* Parse the template arguments.  */
2245       cp_parser_enclosed_template_argument_list (parser);
2246       /* Permanently remove the invalid template arguments so that
2247          this error message is not issued again.  */
2248       if (start)
2249         cp_lexer_purge_tokens_after (parser->lexer, start);
2250     }
2251 }
2252
2253 /* If parsing an integral constant-expression, issue an error message
2254    about the fact that THING appeared and return true.  Otherwise,
2255    return false.  In either case, set
2256    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2257
2258 static bool
2259 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2260                                             const char *thing)
2261 {
2262   parser->non_integral_constant_expression_p = true;
2263   if (parser->integral_constant_expression_p)
2264     {
2265       if (!parser->allow_non_integral_constant_expression_p)
2266         {
2267           error ("%s cannot appear in a constant-expression", thing);
2268           return true;
2269         }
2270     }
2271   return false;
2272 }
2273
2274 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2275    qualifying scope (or NULL, if none) for ID.  This function commits
2276    to the current active tentative parse, if any.  (Otherwise, the
2277    problematic construct might be encountered again later, resulting
2278    in duplicate error messages.)  */
2279
2280 static void
2281 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2282 {
2283   tree decl, old_scope;
2284   /* Try to lookup the identifier.  */
2285   old_scope = parser->scope;
2286   parser->scope = scope;
2287   decl = cp_parser_lookup_name_simple (parser, id);
2288   parser->scope = old_scope;
2289   /* If the lookup found a template-name, it means that the user forgot
2290   to specify an argument list. Emit a useful error message.  */
2291   if (TREE_CODE (decl) == TEMPLATE_DECL)
2292     error ("invalid use of template-name %qE without an argument list", decl);
2293   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2294     error ("invalid use of destructor %qD as a type", id);
2295   else if (TREE_CODE (decl) == TYPE_DECL)
2296     /* Something like 'unsigned A a;'  */
2297     error ("invalid combination of multiple type-specifiers");
2298   else if (!parser->scope)
2299     {
2300       /* Issue an error message.  */
2301       error ("%qE does not name a type", id);
2302       /* If we're in a template class, it's possible that the user was
2303          referring to a type from a base class.  For example:
2304
2305            template <typename T> struct A { typedef T X; };
2306            template <typename T> struct B : public A<T> { X x; };
2307
2308          The user should have said "typename A<T>::X".  */
2309       if (processing_template_decl && current_class_type
2310           && TYPE_BINFO (current_class_type))
2311         {
2312           tree b;
2313
2314           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2315                b;
2316                b = TREE_CHAIN (b))
2317             {
2318               tree base_type = BINFO_TYPE (b);
2319               if (CLASS_TYPE_P (base_type)
2320                   && dependent_type_p (base_type))
2321                 {
2322                   tree field;
2323                   /* Go from a particular instantiation of the
2324                      template (which will have an empty TYPE_FIELDs),
2325                      to the main version.  */
2326                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2327                   for (field = TYPE_FIELDS (base_type);
2328                        field;
2329                        field = TREE_CHAIN (field))
2330                     if (TREE_CODE (field) == TYPE_DECL
2331                         && DECL_NAME (field) == id)
2332                       {
2333                         inform ("(perhaps %<typename %T::%E%> was intended)",
2334                                 BINFO_TYPE (b), id);
2335                         break;
2336                       }
2337                   if (field)
2338                     break;
2339                 }
2340             }
2341         }
2342     }
2343   /* Here we diagnose qualified-ids where the scope is actually correct,
2344      but the identifier does not resolve to a valid type name.  */
2345   else if (parser->scope != error_mark_node)
2346     {
2347       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2348         error ("%qE in namespace %qE does not name a type",
2349                id, parser->scope);
2350       else if (TYPE_P (parser->scope))
2351         error ("%qE in class %qT does not name a type", id, parser->scope);
2352       else
2353         gcc_unreachable ();
2354     }
2355   cp_parser_commit_to_tentative_parse (parser);
2356 }
2357
2358 /* Check for a common situation where a type-name should be present,
2359    but is not, and issue a sensible error message.  Returns true if an
2360    invalid type-name was detected.
2361
2362    The situation handled by this function are variable declarations of the
2363    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2364    Usually, `ID' should name a type, but if we got here it means that it
2365    does not. We try to emit the best possible error message depending on
2366    how exactly the id-expression looks like.  */
2367
2368 static bool
2369 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2370 {
2371   tree id;
2372
2373   cp_parser_parse_tentatively (parser);
2374   id = cp_parser_id_expression (parser,
2375                                 /*template_keyword_p=*/false,
2376                                 /*check_dependency_p=*/true,
2377                                 /*template_p=*/NULL,
2378                                 /*declarator_p=*/true,
2379                                 /*optional_p=*/false);
2380   /* After the id-expression, there should be a plain identifier,
2381      otherwise this is not a simple variable declaration. Also, if
2382      the scope is dependent, we cannot do much.  */
2383   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2384       || (parser->scope && TYPE_P (parser->scope)
2385           && dependent_type_p (parser->scope))
2386       || TREE_CODE (id) == TYPE_DECL)
2387     {
2388       cp_parser_abort_tentative_parse (parser);
2389       return false;
2390     }
2391   if (!cp_parser_parse_definitely (parser))
2392     return false;
2393
2394   /* Emit a diagnostic for the invalid type.  */
2395   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2396   /* Skip to the end of the declaration; there's no point in
2397      trying to process it.  */
2398   cp_parser_skip_to_end_of_block_or_statement (parser);
2399   return true;
2400 }
2401
2402 /* Consume tokens up to, and including, the next non-nested closing `)'.
2403    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2404    are doing error recovery. Returns -1 if OR_COMMA is true and we
2405    found an unnested comma.  */
2406
2407 static int
2408 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2409                                        bool recovering,
2410                                        bool or_comma,
2411                                        bool consume_paren)
2412 {
2413   unsigned paren_depth = 0;
2414   unsigned brace_depth = 0;
2415
2416   if (recovering && !or_comma
2417       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2418     return 0;
2419
2420   while (true)
2421     {
2422       cp_token * token = cp_lexer_peek_token (parser->lexer);
2423
2424       switch (token->type)
2425         {
2426         case CPP_EOF:
2427         case CPP_PRAGMA_EOL:
2428           /* If we've run out of tokens, then there is no closing `)'.  */
2429           return 0;
2430
2431         case CPP_SEMICOLON:
2432           /* This matches the processing in skip_to_end_of_statement.  */
2433           if (!brace_depth)
2434             return 0;
2435           break;
2436
2437         case CPP_OPEN_BRACE:
2438           ++brace_depth;
2439           break;
2440         case CPP_CLOSE_BRACE:
2441           if (!brace_depth--)
2442             return 0;
2443           break;
2444
2445         case CPP_COMMA:
2446           if (recovering && or_comma && !brace_depth && !paren_depth)
2447             return -1;
2448           break;
2449
2450         case CPP_OPEN_PAREN:
2451           if (!brace_depth)
2452             ++paren_depth;
2453           break;
2454
2455         case CPP_CLOSE_PAREN:
2456           if (!brace_depth && !paren_depth--)
2457             {
2458               if (consume_paren)
2459                 cp_lexer_consume_token (parser->lexer);
2460               return 1;
2461             }
2462           break;
2463
2464         default:
2465           break;
2466         }
2467
2468       /* Consume the token.  */
2469       cp_lexer_consume_token (parser->lexer);
2470     }
2471 }
2472
2473 /* Consume tokens until we reach the end of the current statement.
2474    Normally, that will be just before consuming a `;'.  However, if a
2475    non-nested `}' comes first, then we stop before consuming that.  */
2476
2477 static void
2478 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2479 {
2480   unsigned nesting_depth = 0;
2481
2482   while (true)
2483     {
2484       cp_token *token = cp_lexer_peek_token (parser->lexer);
2485
2486       switch (token->type)
2487         {
2488         case CPP_EOF:
2489         case CPP_PRAGMA_EOL:
2490           /* If we've run out of tokens, stop.  */
2491           return;
2492
2493         case CPP_SEMICOLON:
2494           /* If the next token is a `;', we have reached the end of the
2495              statement.  */
2496           if (!nesting_depth)
2497             return;
2498           break;
2499
2500         case CPP_CLOSE_BRACE:
2501           /* If this is a non-nested '}', stop before consuming it.
2502              That way, when confronted with something like:
2503
2504                { 3 + }
2505
2506              we stop before consuming the closing '}', even though we
2507              have not yet reached a `;'.  */
2508           if (nesting_depth == 0)
2509             return;
2510
2511           /* If it is the closing '}' for a block that we have
2512              scanned, stop -- but only after consuming the token.
2513              That way given:
2514
2515                 void f g () { ... }
2516                 typedef int I;
2517
2518              we will stop after the body of the erroneously declared
2519              function, but before consuming the following `typedef'
2520              declaration.  */
2521           if (--nesting_depth == 0)
2522             {
2523               cp_lexer_consume_token (parser->lexer);
2524               return;
2525             }
2526
2527         case CPP_OPEN_BRACE:
2528           ++nesting_depth;
2529           break;
2530
2531         default:
2532           break;
2533         }
2534
2535       /* Consume the token.  */
2536       cp_lexer_consume_token (parser->lexer);
2537     }
2538 }
2539
2540 /* This function is called at the end of a statement or declaration.
2541    If the next token is a semicolon, it is consumed; otherwise, error
2542    recovery is attempted.  */
2543
2544 static void
2545 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2546 {
2547   /* Look for the trailing `;'.  */
2548   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2549     {
2550       /* If there is additional (erroneous) input, skip to the end of
2551          the statement.  */
2552       cp_parser_skip_to_end_of_statement (parser);
2553       /* If the next token is now a `;', consume it.  */
2554       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2555         cp_lexer_consume_token (parser->lexer);
2556     }
2557 }
2558
2559 /* Skip tokens until we have consumed an entire block, or until we
2560    have consumed a non-nested `;'.  */
2561
2562 static void
2563 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2564 {
2565   int nesting_depth = 0;
2566
2567   while (nesting_depth >= 0)
2568     {
2569       cp_token *token = cp_lexer_peek_token (parser->lexer);
2570
2571       switch (token->type)
2572         {
2573         case CPP_EOF:
2574         case CPP_PRAGMA_EOL:
2575           /* If we've run out of tokens, stop.  */
2576           return;
2577
2578         case CPP_SEMICOLON:
2579           /* Stop if this is an unnested ';'. */
2580           if (!nesting_depth)
2581             nesting_depth = -1;
2582           break;
2583
2584         case CPP_CLOSE_BRACE:
2585           /* Stop if this is an unnested '}', or closes the outermost
2586              nesting level.  */
2587           nesting_depth--;
2588           if (!nesting_depth)
2589             nesting_depth = -1;
2590           break;
2591
2592         case CPP_OPEN_BRACE:
2593           /* Nest. */
2594           nesting_depth++;
2595           break;
2596
2597         default:
2598           break;
2599         }
2600
2601       /* Consume the token.  */
2602       cp_lexer_consume_token (parser->lexer);
2603     }
2604 }
2605
2606 /* Skip tokens until a non-nested closing curly brace is the next
2607    token, or there are no more tokens. Return true in the first case,
2608    false otherwise.  */
2609
2610 static bool
2611 cp_parser_skip_to_closing_brace (cp_parser *parser)
2612 {
2613   unsigned nesting_depth = 0;
2614
2615   while (true)
2616     {
2617       cp_token *token = cp_lexer_peek_token (parser->lexer);
2618
2619       switch (token->type)
2620         {
2621         case CPP_EOF:
2622         case CPP_PRAGMA_EOL:
2623           /* If we've run out of tokens, stop.  */
2624           return false;
2625
2626         case CPP_CLOSE_BRACE:
2627           /* If the next token is a non-nested `}', then we have reached
2628              the end of the current block.  */
2629           if (nesting_depth-- == 0)
2630             return true;
2631           break;
2632
2633         case CPP_OPEN_BRACE:
2634           /* If it the next token is a `{', then we are entering a new
2635              block.  Consume the entire block.  */
2636           ++nesting_depth;
2637           break;
2638
2639         default:
2640           break;
2641         }
2642
2643       /* Consume the token.  */
2644       cp_lexer_consume_token (parser->lexer);
2645     }
2646 }
2647
2648 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2649    parameter is the PRAGMA token, allowing us to purge the entire pragma
2650    sequence.  */
2651
2652 static void
2653 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2654 {
2655   cp_token *token;
2656
2657   parser->lexer->in_pragma = false;
2658
2659   do
2660     token = cp_lexer_consume_token (parser->lexer);
2661   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2662
2663   /* Ensure that the pragma is not parsed again.  */
2664   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2665 }
2666
2667 /* Require pragma end of line, resyncing with it as necessary.  The
2668    arguments are as for cp_parser_skip_to_pragma_eol.  */
2669
2670 static void
2671 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2672 {
2673   parser->lexer->in_pragma = false;
2674   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2675     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2676 }
2677
2678 /* This is a simple wrapper around make_typename_type. When the id is
2679    an unresolved identifier node, we can provide a superior diagnostic
2680    using cp_parser_diagnose_invalid_type_name.  */
2681
2682 static tree
2683 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2684 {
2685   tree result;
2686   if (TREE_CODE (id) == IDENTIFIER_NODE)
2687     {
2688       result = make_typename_type (scope, id, typename_type,
2689                                    /*complain=*/tf_none);
2690       if (result == error_mark_node)
2691         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2692       return result;
2693     }
2694   return make_typename_type (scope, id, typename_type, tf_error);
2695 }
2696
2697 /* This is a wrapper around the
2698    make_{pointer,ptrmem,reference}_declarator functions that decides
2699    which one to call based on the CODE and CLASS_TYPE arguments. The
2700    CODE argument should be one of the values returned by
2701    cp_parser_ptr_operator. */
2702 static cp_declarator *
2703 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2704                                     cp_cv_quals cv_qualifiers,
2705                                     cp_declarator *target)
2706 {
2707   if (code == ERROR_MARK)
2708     return cp_error_declarator;
2709
2710   if (code == INDIRECT_REF)
2711     if (class_type == NULL_TREE)
2712       return make_pointer_declarator (cv_qualifiers, target);
2713     else
2714       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2715   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2716     return make_reference_declarator (cv_qualifiers, target, false);
2717   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2718     return make_reference_declarator (cv_qualifiers, target, true);
2719   gcc_unreachable ();
2720 }
2721
2722 /* Create a new C++ parser.  */
2723
2724 static cp_parser *
2725 cp_parser_new (void)
2726 {
2727   cp_parser *parser;
2728   cp_lexer *lexer;
2729   unsigned i;
2730
2731   /* cp_lexer_new_main is called before calling ggc_alloc because
2732      cp_lexer_new_main might load a PCH file.  */
2733   lexer = cp_lexer_new_main ();
2734
2735   /* Initialize the binops_by_token so that we can get the tree
2736      directly from the token.  */
2737   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2738     binops_by_token[binops[i].token_type] = binops[i];
2739
2740   parser = GGC_CNEW (cp_parser);
2741   parser->lexer = lexer;
2742   parser->context = cp_parser_context_new (NULL);
2743
2744   /* For now, we always accept GNU extensions.  */
2745   parser->allow_gnu_extensions_p = 1;
2746
2747   /* The `>' token is a greater-than operator, not the end of a
2748      template-id.  */
2749   parser->greater_than_is_operator_p = true;
2750
2751   parser->default_arg_ok_p = true;
2752
2753   /* We are not parsing a constant-expression.  */
2754   parser->integral_constant_expression_p = false;
2755   parser->allow_non_integral_constant_expression_p = false;
2756   parser->non_integral_constant_expression_p = false;
2757
2758   /* Local variable names are not forbidden.  */
2759   parser->local_variables_forbidden_p = false;
2760
2761   /* We are not processing an `extern "C"' declaration.  */
2762   parser->in_unbraced_linkage_specification_p = false;
2763
2764   /* We are not processing a declarator.  */
2765   parser->in_declarator_p = false;
2766
2767   /* We are not processing a template-argument-list.  */
2768   parser->in_template_argument_list_p = false;
2769
2770   /* We are not in an iteration statement.  */
2771   parser->in_statement = 0;
2772
2773   /* We are not in a switch statement.  */
2774   parser->in_switch_statement_p = false;
2775
2776   /* We are not parsing a type-id inside an expression.  */
2777   parser->in_type_id_in_expr_p = false;
2778
2779   /* Declarations aren't implicitly extern "C".  */
2780   parser->implicit_extern_c = false;
2781
2782   /* String literals should be translated to the execution character set.  */
2783   parser->translate_strings_p = true;
2784
2785   /* We are not parsing a function body.  */
2786   parser->in_function_body = false;
2787
2788   /* The unparsed function queue is empty.  */
2789   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2790
2791   /* There are no classes being defined.  */
2792   parser->num_classes_being_defined = 0;
2793
2794   /* No template parameters apply.  */
2795   parser->num_template_parameter_lists = 0;
2796
2797   return parser;
2798 }
2799
2800 /* Create a cp_lexer structure which will emit the tokens in CACHE
2801    and push it onto the parser's lexer stack.  This is used for delayed
2802    parsing of in-class method bodies and default arguments, and should
2803    not be confused with tentative parsing.  */
2804 static void
2805 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2806 {
2807   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2808   lexer->next = parser->lexer;
2809   parser->lexer = lexer;
2810
2811   /* Move the current source position to that of the first token in the
2812      new lexer.  */
2813   cp_lexer_set_source_position_from_token (lexer->next_token);
2814 }
2815
2816 /* Pop the top lexer off the parser stack.  This is never used for the
2817    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2818 static void
2819 cp_parser_pop_lexer (cp_parser *parser)
2820 {
2821   cp_lexer *lexer = parser->lexer;
2822   parser->lexer = lexer->next;
2823   cp_lexer_destroy (lexer);
2824
2825   /* Put the current source position back where it was before this
2826      lexer was pushed.  */
2827   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2828 }
2829
2830 /* Lexical conventions [gram.lex]  */
2831
2832 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2833    identifier.  */
2834
2835 static tree
2836 cp_parser_identifier (cp_parser* parser)
2837 {
2838   cp_token *token;
2839
2840   /* Look for the identifier.  */
2841   token = cp_parser_require (parser, CPP_NAME, "identifier");
2842   /* Return the value.  */
2843   return token ? token->u.value : error_mark_node;
2844 }
2845
2846 /* Parse a sequence of adjacent string constants.  Returns a
2847    TREE_STRING representing the combined, nul-terminated string
2848    constant.  If TRANSLATE is true, translate the string to the
2849    execution character set.  If WIDE_OK is true, a wide string is
2850    invalid here.
2851
2852    C++98 [lex.string] says that if a narrow string literal token is
2853    adjacent to a wide string literal token, the behavior is undefined.
2854    However, C99 6.4.5p4 says that this results in a wide string literal.
2855    We follow C99 here, for consistency with the C front end.
2856
2857    This code is largely lifted from lex_string() in c-lex.c.
2858
2859    FUTURE: ObjC++ will need to handle @-strings here.  */
2860 static tree
2861 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2862 {
2863   tree value;
2864   bool wide = false;
2865   size_t count;
2866   struct obstack str_ob;
2867   cpp_string str, istr, *strs;
2868   cp_token *tok;
2869
2870   tok = cp_lexer_peek_token (parser->lexer);
2871   if (!cp_parser_is_string_literal (tok))
2872     {
2873       cp_parser_error (parser, "expected string-literal");
2874       return error_mark_node;
2875     }
2876
2877   /* Try to avoid the overhead of creating and destroying an obstack
2878      for the common case of just one string.  */
2879   if (!cp_parser_is_string_literal
2880       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2881     {
2882       cp_lexer_consume_token (parser->lexer);
2883
2884       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2885       str.len = TREE_STRING_LENGTH (tok->u.value);
2886       count = 1;
2887       if (tok->type == CPP_WSTRING)
2888         wide = true;
2889
2890       strs = &str;
2891     }
2892   else
2893     {
2894       gcc_obstack_init (&str_ob);
2895       count = 0;
2896
2897       do
2898         {
2899           cp_lexer_consume_token (parser->lexer);
2900           count++;
2901           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2902           str.len = TREE_STRING_LENGTH (tok->u.value);
2903           if (tok->type == CPP_WSTRING)
2904             wide = true;
2905
2906           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2907
2908           tok = cp_lexer_peek_token (parser->lexer);
2909         }
2910       while (cp_parser_is_string_literal (tok));
2911
2912       strs = (cpp_string *) obstack_finish (&str_ob);
2913     }
2914
2915   if (wide && !wide_ok)
2916     {
2917       cp_parser_error (parser, "a wide string is invalid in this context");
2918       wide = false;
2919     }
2920
2921   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2922       (parse_in, strs, count, &istr, wide))
2923     {
2924       value = build_string (istr.len, (const char *)istr.text);
2925       free (CONST_CAST (unsigned char *, istr.text));
2926
2927       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2928       value = fix_string_type (value);
2929     }
2930   else
2931     /* cpp_interpret_string has issued an error.  */
2932     value = error_mark_node;
2933
2934   if (count > 1)
2935     obstack_free (&str_ob, 0);
2936
2937   return value;
2938 }
2939
2940
2941 /* Basic concepts [gram.basic]  */
2942
2943 /* Parse a translation-unit.
2944
2945    translation-unit:
2946      declaration-seq [opt]
2947
2948    Returns TRUE if all went well.  */
2949
2950 static bool
2951 cp_parser_translation_unit (cp_parser* parser)
2952 {
2953   /* The address of the first non-permanent object on the declarator
2954      obstack.  */
2955   static void *declarator_obstack_base;
2956
2957   bool success;
2958
2959   /* Create the declarator obstack, if necessary.  */
2960   if (!cp_error_declarator)
2961     {
2962       gcc_obstack_init (&declarator_obstack);
2963       /* Create the error declarator.  */
2964       cp_error_declarator = make_declarator (cdk_error);
2965       /* Create the empty parameter list.  */
2966       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2967       /* Remember where the base of the declarator obstack lies.  */
2968       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2969     }
2970
2971   cp_parser_declaration_seq_opt (parser);
2972
2973   /* If there are no tokens left then all went well.  */
2974   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2975     {
2976       /* Get rid of the token array; we don't need it any more.  */
2977       cp_lexer_destroy (parser->lexer);
2978       parser->lexer = NULL;
2979
2980       /* This file might have been a context that's implicitly extern
2981          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2982       if (parser->implicit_extern_c)
2983         {
2984           pop_lang_context ();
2985           parser->implicit_extern_c = false;
2986         }
2987
2988       /* Finish up.  */
2989       finish_translation_unit ();
2990
2991       success = true;
2992     }
2993   else
2994     {
2995       cp_parser_error (parser, "expected declaration");
2996       success = false;
2997     }
2998
2999   /* Make sure the declarator obstack was fully cleaned up.  */
3000   gcc_assert (obstack_next_free (&declarator_obstack)
3001               == declarator_obstack_base);
3002
3003   /* All went well.  */
3004   return success;
3005 }
3006
3007 /* Expressions [gram.expr] */
3008
3009 /* Parse a primary-expression.
3010
3011    primary-expression:
3012      literal
3013      this
3014      ( expression )
3015      id-expression
3016
3017    GNU Extensions:
3018
3019    primary-expression:
3020      ( compound-statement )
3021      __builtin_va_arg ( assignment-expression , type-id )
3022      __builtin_offsetof ( type-id , offsetof-expression )
3023
3024    C++ Extensions:
3025      __has_nothrow_assign ( type-id )   
3026      __has_nothrow_constructor ( type-id )
3027      __has_nothrow_copy ( type-id )
3028      __has_trivial_assign ( type-id )   
3029      __has_trivial_constructor ( type-id )
3030      __has_trivial_copy ( type-id )
3031      __has_trivial_destructor ( type-id )
3032      __has_virtual_destructor ( type-id )     
3033      __is_abstract ( type-id )
3034      __is_base_of ( type-id , type-id )
3035      __is_class ( type-id )
3036      __is_convertible_to ( type-id , type-id )     
3037      __is_empty ( type-id )
3038      __is_enum ( type-id )
3039      __is_pod ( type-id )
3040      __is_polymorphic ( type-id )
3041      __is_union ( type-id )
3042
3043    Objective-C++ Extension:
3044
3045    primary-expression:
3046      objc-expression
3047
3048    literal:
3049      __null
3050
3051    ADDRESS_P is true iff this expression was immediately preceded by
3052    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3053    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3054    true iff this expression is a template argument.
3055
3056    Returns a representation of the expression.  Upon return, *IDK
3057    indicates what kind of id-expression (if any) was present.  */
3058
3059 static tree
3060 cp_parser_primary_expression (cp_parser *parser,
3061                               bool address_p,
3062                               bool cast_p,
3063                               bool template_arg_p,
3064                               cp_id_kind *idk)
3065 {
3066   cp_token *token;
3067
3068   /* Assume the primary expression is not an id-expression.  */
3069   *idk = CP_ID_KIND_NONE;
3070
3071   /* Peek at the next token.  */
3072   token = cp_lexer_peek_token (parser->lexer);
3073   switch (token->type)
3074     {
3075       /* literal:
3076            integer-literal
3077            character-literal
3078            floating-literal
3079            string-literal
3080            boolean-literal  */
3081     case CPP_CHAR:
3082     case CPP_WCHAR:
3083     case CPP_NUMBER:
3084       token = cp_lexer_consume_token (parser->lexer);
3085       /* Floating-point literals are only allowed in an integral
3086          constant expression if they are cast to an integral or
3087          enumeration type.  */
3088       if (TREE_CODE (token->u.value) == REAL_CST
3089           && parser->integral_constant_expression_p
3090           && pedantic)
3091         {
3092           /* CAST_P will be set even in invalid code like "int(2.7 +
3093              ...)".   Therefore, we have to check that the next token
3094              is sure to end the cast.  */
3095           if (cast_p)
3096             {
3097               cp_token *next_token;
3098
3099               next_token = cp_lexer_peek_token (parser->lexer);
3100               if (/* The comma at the end of an
3101                      enumerator-definition.  */
3102                   next_token->type != CPP_COMMA
3103                   /* The curly brace at the end of an enum-specifier.  */
3104                   && next_token->type != CPP_CLOSE_BRACE
3105                   /* The end of a statement.  */
3106                   && next_token->type != CPP_SEMICOLON
3107                   /* The end of the cast-expression.  */
3108                   && next_token->type != CPP_CLOSE_PAREN
3109                   /* The end of an array bound.  */
3110                   && next_token->type != CPP_CLOSE_SQUARE
3111                   /* The closing ">" in a template-argument-list.  */
3112                   && (next_token->type != CPP_GREATER
3113                       || parser->greater_than_is_operator_p)
3114                   /* C++0x only: A ">>" treated like two ">" tokens,
3115                      in a template-argument-list.  */
3116                   && (next_token->type != CPP_RSHIFT
3117                       || (cxx_dialect == cxx98)
3118                       || parser->greater_than_is_operator_p))
3119                 cast_p = false;
3120             }
3121
3122           /* If we are within a cast, then the constraint that the
3123              cast is to an integral or enumeration type will be
3124              checked at that point.  If we are not within a cast, then
3125              this code is invalid.  */
3126           if (!cast_p)
3127             cp_parser_non_integral_constant_expression
3128               (parser, "floating-point literal");
3129         }
3130       return token->u.value;
3131
3132     case CPP_STRING:
3133     case CPP_WSTRING:
3134       /* ??? Should wide strings be allowed when parser->translate_strings_p
3135          is false (i.e. in attributes)?  If not, we can kill the third
3136          argument to cp_parser_string_literal.  */
3137       return cp_parser_string_literal (parser,
3138                                        parser->translate_strings_p,
3139                                        true);
3140
3141     case CPP_OPEN_PAREN:
3142       {
3143         tree expr;
3144         bool saved_greater_than_is_operator_p;
3145
3146         /* Consume the `('.  */
3147         cp_lexer_consume_token (parser->lexer);
3148         /* Within a parenthesized expression, a `>' token is always
3149            the greater-than operator.  */
3150         saved_greater_than_is_operator_p
3151           = parser->greater_than_is_operator_p;
3152         parser->greater_than_is_operator_p = true;
3153         /* If we see `( { ' then we are looking at the beginning of
3154            a GNU statement-expression.  */
3155         if (cp_parser_allow_gnu_extensions_p (parser)
3156             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3157           {
3158             /* Statement-expressions are not allowed by the standard.  */
3159             if (pedantic)
3160               pedwarn ("ISO C++ forbids braced-groups within expressions");
3161
3162             /* And they're not allowed outside of a function-body; you
3163                cannot, for example, write:
3164
3165                  int i = ({ int j = 3; j + 1; });
3166
3167                at class or namespace scope.  */
3168             if (!parser->in_function_body
3169                 || parser->in_template_argument_list_p)
3170               {
3171                 error ("statement-expressions are not allowed outside "
3172                        "functions nor in template-argument lists");
3173                 cp_parser_skip_to_end_of_block_or_statement (parser);
3174                 expr = error_mark_node;
3175               }
3176             else
3177               {
3178                 /* Start the statement-expression.  */
3179                 expr = begin_stmt_expr ();
3180                 /* Parse the compound-statement.  */
3181                 cp_parser_compound_statement (parser, expr, false);
3182                 /* Finish up.  */
3183                 expr = finish_stmt_expr (expr, false);
3184               }
3185           }
3186         else
3187           {
3188             /* Parse the parenthesized expression.  */
3189             expr = cp_parser_expression (parser, cast_p);
3190             /* Let the front end know that this expression was
3191                enclosed in parentheses. This matters in case, for
3192                example, the expression is of the form `A::B', since
3193                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3194                not.  */
3195             finish_parenthesized_expr (expr);
3196           }
3197         /* The `>' token might be the end of a template-id or
3198            template-parameter-list now.  */
3199         parser->greater_than_is_operator_p
3200           = saved_greater_than_is_operator_p;
3201         /* Consume the `)'.  */
3202         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3203           cp_parser_skip_to_end_of_statement (parser);
3204
3205         return expr;
3206       }
3207
3208     case CPP_KEYWORD:
3209       switch (token->keyword)
3210         {
3211           /* These two are the boolean literals.  */
3212         case RID_TRUE:
3213           cp_lexer_consume_token (parser->lexer);
3214           return boolean_true_node;
3215         case RID_FALSE:
3216           cp_lexer_consume_token (parser->lexer);
3217           return boolean_false_node;
3218
3219           /* The `__null' literal.  */
3220         case RID_NULL:
3221           cp_lexer_consume_token (parser->lexer);
3222           return null_node;
3223
3224           /* Recognize the `this' keyword.  */
3225         case RID_THIS:
3226           cp_lexer_consume_token (parser->lexer);
3227           if (parser->local_variables_forbidden_p)
3228             {
3229               error ("%<this%> may not be used in this context");
3230               return error_mark_node;
3231             }
3232           /* Pointers cannot appear in constant-expressions.  */
3233           if (cp_parser_non_integral_constant_expression (parser,
3234                                                           "`this'"))
3235             return error_mark_node;
3236           return finish_this_expr ();
3237
3238           /* The `operator' keyword can be the beginning of an
3239              id-expression.  */
3240         case RID_OPERATOR:
3241           goto id_expression;
3242
3243         case RID_FUNCTION_NAME:
3244         case RID_PRETTY_FUNCTION_NAME:
3245         case RID_C99_FUNCTION_NAME:
3246           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3247              __func__ are the names of variables -- but they are
3248              treated specially.  Therefore, they are handled here,
3249              rather than relying on the generic id-expression logic
3250              below.  Grammatically, these names are id-expressions.
3251
3252              Consume the token.  */
3253           token = cp_lexer_consume_token (parser->lexer);
3254           /* Look up the name.  */
3255           return finish_fname (token->u.value);
3256
3257         case RID_VA_ARG:
3258           {
3259             tree expression;
3260             tree type;
3261
3262             /* The `__builtin_va_arg' construct is used to handle
3263                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3264             cp_lexer_consume_token (parser->lexer);
3265             /* Look for the opening `('.  */
3266             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3267             /* Now, parse the assignment-expression.  */
3268             expression = cp_parser_assignment_expression (parser,
3269                                                           /*cast_p=*/false);
3270             /* Look for the `,'.  */
3271             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3272             /* Parse the type-id.  */
3273             type = cp_parser_type_id (parser);
3274             /* Look for the closing `)'.  */
3275             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3276             /* Using `va_arg' in a constant-expression is not
3277                allowed.  */
3278             if (cp_parser_non_integral_constant_expression (parser,
3279                                                             "`va_arg'"))
3280               return error_mark_node;
3281             return build_x_va_arg (expression, type);
3282           }
3283
3284         case RID_OFFSETOF:
3285           return cp_parser_builtin_offsetof (parser);
3286
3287         case RID_HAS_NOTHROW_ASSIGN:
3288         case RID_HAS_NOTHROW_CONSTRUCTOR:
3289         case RID_HAS_NOTHROW_COPY:        
3290         case RID_HAS_TRIVIAL_ASSIGN:
3291         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3292         case RID_HAS_TRIVIAL_COPY:        
3293         case RID_HAS_TRIVIAL_DESTRUCTOR:
3294         case RID_HAS_VIRTUAL_DESTRUCTOR:
3295         case RID_IS_ABSTRACT:
3296         case RID_IS_BASE_OF:
3297         case RID_IS_CLASS:
3298         case RID_IS_CONVERTIBLE_TO:
3299         case RID_IS_EMPTY:
3300         case RID_IS_ENUM:
3301         case RID_IS_POD:
3302         case RID_IS_POLYMORPHIC:
3303         case RID_IS_UNION:
3304           return cp_parser_trait_expr (parser, token->keyword);
3305
3306         /* Objective-C++ expressions.  */
3307         case RID_AT_ENCODE:
3308         case RID_AT_PROTOCOL:
3309         case RID_AT_SELECTOR:
3310           return cp_parser_objc_expression (parser);
3311
3312         default:
3313           cp_parser_error (parser, "expected primary-expression");
3314           return error_mark_node;
3315         }
3316
3317       /* An id-expression can start with either an identifier, a
3318          `::' as the beginning of a qualified-id, or the "operator"
3319          keyword.  */
3320     case CPP_NAME:
3321     case CPP_SCOPE:
3322     case CPP_TEMPLATE_ID:
3323     case CPP_NESTED_NAME_SPECIFIER:
3324       {
3325         tree id_expression;
3326         tree decl;
3327         const char *error_msg;
3328         bool template_p;
3329         bool done;
3330
3331       id_expression:
3332         /* Parse the id-expression.  */
3333         id_expression
3334           = cp_parser_id_expression (parser,
3335                                      /*template_keyword_p=*/false,
3336                                      /*check_dependency_p=*/true,
3337                                      &template_p,
3338                                      /*declarator_p=*/false,
3339                                      /*optional_p=*/false);
3340         if (id_expression == error_mark_node)
3341           return error_mark_node;
3342         token = cp_lexer_peek_token (parser->lexer);
3343         done = (token->type != CPP_OPEN_SQUARE
3344                 && token->type != CPP_OPEN_PAREN
3345                 && token->type != CPP_DOT
3346                 && token->type != CPP_DEREF
3347                 && token->type != CPP_PLUS_PLUS
3348                 && token->type != CPP_MINUS_MINUS);
3349         /* If we have a template-id, then no further lookup is
3350            required.  If the template-id was for a template-class, we
3351            will sometimes have a TYPE_DECL at this point.  */
3352         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3353                  || TREE_CODE (id_expression) == TYPE_DECL)
3354           decl = id_expression;
3355         /* Look up the name.  */
3356         else
3357           {
3358             tree ambiguous_decls;
3359
3360             decl = cp_parser_lookup_name (parser, id_expression,
3361                                           none_type,
3362                                           template_p,
3363                                           /*is_namespace=*/false,
3364                                           /*check_dependency=*/true,
3365                                           &ambiguous_decls);
3366             /* If the lookup was ambiguous, an error will already have
3367                been issued.  */
3368             if (ambiguous_decls)
3369               return error_mark_node;
3370
3371             /* In Objective-C++, an instance variable (ivar) may be preferred
3372                to whatever cp_parser_lookup_name() found.  */
3373             decl = objc_lookup_ivar (decl, id_expression);
3374
3375             /* If name lookup gives us a SCOPE_REF, then the
3376                qualifying scope was dependent.  */
3377             if (TREE_CODE (decl) == SCOPE_REF)
3378               {
3379                 /* At this point, we do not know if DECL is a valid
3380                    integral constant expression.  We assume that it is
3381                    in fact such an expression, so that code like:
3382
3383                       template <int N> struct A {
3384                         int a[B<N>::i];
3385                       };
3386                      
3387                    is accepted.  At template-instantiation time, we
3388                    will check that B<N>::i is actually a constant.  */
3389                 return decl;
3390               }
3391             /* Check to see if DECL is a local variable in a context
3392                where that is forbidden.  */
3393             if (parser->local_variables_forbidden_p
3394                 && local_variable_p (decl))
3395               {
3396                 /* It might be that we only found DECL because we are
3397                    trying to be generous with pre-ISO scoping rules.
3398                    For example, consider:
3399
3400                      int i;
3401                      void g() {
3402                        for (int i = 0; i < 10; ++i) {}
3403                        extern void f(int j = i);
3404                      }
3405
3406                    Here, name look up will originally find the out
3407                    of scope `i'.  We need to issue a warning message,
3408                    but then use the global `i'.  */
3409                 decl = check_for_out_of_scope_variable (decl);
3410                 if (local_variable_p (decl))
3411                   {
3412                     error ("local variable %qD may not appear in this context",
3413                            decl);
3414                     return error_mark_node;
3415                   }
3416               }
3417           }
3418
3419         decl = (finish_id_expression
3420                 (id_expression, decl, parser->scope,
3421                  idk,
3422                  parser->integral_constant_expression_p,
3423                  parser->allow_non_integral_constant_expression_p,
3424                  &parser->non_integral_constant_expression_p,
3425                  template_p, done, address_p,
3426                  template_arg_p,
3427                  &error_msg));
3428         if (error_msg)
3429           cp_parser_error (parser, error_msg);
3430         return decl;
3431       }
3432
3433       /* Anything else is an error.  */
3434     default:
3435       /* ...unless we have an Objective-C++ message or string literal,
3436          that is.  */
3437       if (c_dialect_objc ()
3438           && (token->type == CPP_OPEN_SQUARE
3439               || token->type == CPP_OBJC_STRING))
3440         return cp_parser_objc_expression (parser);
3441
3442       cp_parser_error (parser, "expected primary-expression");
3443       return error_mark_node;
3444     }
3445 }
3446
3447 /* Parse an id-expression.
3448
3449    id-expression:
3450      unqualified-id
3451      qualified-id
3452
3453    qualified-id:
3454      :: [opt] nested-name-specifier template [opt] unqualified-id
3455      :: identifier
3456      :: operator-function-id
3457      :: template-id
3458
3459    Return a representation of the unqualified portion of the
3460    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3461    a `::' or nested-name-specifier.
3462
3463    Often, if the id-expression was a qualified-id, the caller will
3464    want to make a SCOPE_REF to represent the qualified-id.  This
3465    function does not do this in order to avoid wastefully creating
3466    SCOPE_REFs when they are not required.
3467
3468    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3469    `template' keyword.
3470
3471    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3472    uninstantiated templates.
3473
3474    If *TEMPLATE_P is non-NULL, it is set to true iff the
3475    `template' keyword is used to explicitly indicate that the entity
3476    named is a template.
3477
3478    If DECLARATOR_P is true, the id-expression is appearing as part of
3479    a declarator, rather than as part of an expression.  */
3480
3481 static tree
3482 cp_parser_id_expression (cp_parser *parser,
3483                          bool template_keyword_p,
3484                          bool check_dependency_p,
3485                          bool *template_p,
3486                          bool declarator_p,
3487                          bool optional_p)
3488 {
3489   bool global_scope_p;
3490   bool nested_name_specifier_p;
3491
3492   /* Assume the `template' keyword was not used.  */
3493   if (template_p)
3494     *template_p = template_keyword_p;
3495
3496   /* Look for the optional `::' operator.  */
3497   global_scope_p
3498     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3499        != NULL_TREE);
3500   /* Look for the optional nested-name-specifier.  */
3501   nested_name_specifier_p
3502     = (cp_parser_nested_name_specifier_opt (parser,
3503                                             /*typename_keyword_p=*/false,
3504                                             check_dependency_p,
3505                                             /*type_p=*/false,
3506                                             declarator_p)
3507        != NULL_TREE);
3508   /* If there is a nested-name-specifier, then we are looking at
3509      the first qualified-id production.  */
3510   if (nested_name_specifier_p)
3511     {
3512       tree saved_scope;
3513       tree saved_object_scope;
3514       tree saved_qualifying_scope;
3515       tree unqualified_id;
3516       bool is_template;
3517
3518       /* See if the next token is the `template' keyword.  */
3519       if (!template_p)
3520         template_p = &is_template;
3521       *template_p = cp_parser_optional_template_keyword (parser);
3522       /* Name lookup we do during the processing of the
3523          unqualified-id might obliterate SCOPE.  */
3524       saved_scope = parser->scope;
3525       saved_object_scope = parser->object_scope;
3526       saved_qualifying_scope = parser->qualifying_scope;
3527       /* Process the final unqualified-id.  */
3528       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3529                                                  check_dependency_p,
3530                                                  declarator_p,
3531                                                  /*optional_p=*/false);
3532       /* Restore the SAVED_SCOPE for our caller.  */
3533       parser->scope = saved_scope;
3534       parser->object_scope = saved_object_scope;
3535       parser->qualifying_scope = saved_qualifying_scope;
3536
3537       return unqualified_id;
3538     }
3539   /* Otherwise, if we are in global scope, then we are looking at one
3540      of the other qualified-id productions.  */
3541   else if (global_scope_p)
3542     {
3543       cp_token *token;
3544       tree id;
3545
3546       /* Peek at the next token.  */
3547       token = cp_lexer_peek_token (parser->lexer);
3548
3549       /* If it's an identifier, and the next token is not a "<", then
3550          we can avoid the template-id case.  This is an optimization
3551          for this common case.  */
3552       if (token->type == CPP_NAME
3553           && !cp_parser_nth_token_starts_template_argument_list_p
3554                (parser, 2))
3555         return cp_parser_identifier (parser);
3556
3557       cp_parser_parse_tentatively (parser);
3558       /* Try a template-id.  */
3559       id = cp_parser_template_id (parser,
3560                                   /*template_keyword_p=*/false,
3561                                   /*check_dependency_p=*/true,
3562                                   declarator_p);
3563       /* If that worked, we're done.  */
3564       if (cp_parser_parse_definitely (parser))
3565         return id;
3566
3567       /* Peek at the next token.  (Changes in the token buffer may
3568          have invalidated the pointer obtained above.)  */
3569       token = cp_lexer_peek_token (parser->lexer);
3570
3571       switch (token->type)
3572         {
3573         case CPP_NAME:
3574           return cp_parser_identifier (parser);
3575
3576         case CPP_KEYWORD:
3577           if (token->keyword == RID_OPERATOR)
3578             return cp_parser_operator_function_id (parser);
3579           /* Fall through.  */
3580
3581         default:
3582           cp_parser_error (parser, "expected id-expression");
3583           return error_mark_node;
3584         }
3585     }
3586   else
3587     return cp_parser_unqualified_id (parser, template_keyword_p,
3588                                      /*check_dependency_p=*/true,
3589                                      declarator_p,
3590                                      optional_p);
3591 }
3592
3593 /* Parse an unqualified-id.
3594
3595    unqualified-id:
3596      identifier
3597      operator-function-id
3598      conversion-function-id
3599      ~ class-name
3600      template-id
3601
3602    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3603    keyword, in a construct like `A::template ...'.
3604
3605    Returns a representation of unqualified-id.  For the `identifier'
3606    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3607    production a BIT_NOT_EXPR is returned; the operand of the
3608    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3609    other productions, see the documentation accompanying the
3610    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3611    names are looked up in uninstantiated templates.  If DECLARATOR_P
3612    is true, the unqualified-id is appearing as part of a declarator,
3613    rather than as part of an expression.  */
3614
3615 static tree
3616 cp_parser_unqualified_id (cp_parser* parser,
3617                           bool template_keyword_p,
3618                           bool check_dependency_p,
3619                           bool declarator_p,
3620                           bool optional_p)
3621 {
3622   cp_token *token;
3623
3624   /* Peek at the next token.  */
3625   token = cp_lexer_peek_token (parser->lexer);
3626
3627   switch (token->type)
3628     {
3629     case CPP_NAME:
3630       {
3631         tree id;
3632
3633         /* We don't know yet whether or not this will be a
3634            template-id.  */
3635         cp_parser_parse_tentatively (parser);
3636         /* Try a template-id.  */
3637         id = cp_parser_template_id (parser, template_keyword_p,
3638                                     check_dependency_p,
3639                                     declarator_p);
3640         /* If it worked, we're done.  */
3641         if (cp_parser_parse_definitely (parser))
3642           return id;
3643         /* Otherwise, it's an ordinary identifier.  */
3644         return cp_parser_identifier (parser);
3645       }
3646
3647     case CPP_TEMPLATE_ID:
3648       return cp_parser_template_id (parser, template_keyword_p,
3649                                     check_dependency_p,
3650                                     declarator_p);
3651
3652     case CPP_COMPL:
3653       {
3654         tree type_decl;
3655         tree qualifying_scope;
3656         tree object_scope;
3657         tree scope;
3658         bool done;
3659
3660         /* Consume the `~' token.  */
3661         cp_lexer_consume_token (parser->lexer);
3662         /* Parse the class-name.  The standard, as written, seems to
3663            say that:
3664
3665              template <typename T> struct S { ~S (); };
3666              template <typename T> S<T>::~S() {}
3667
3668            is invalid, since `~' must be followed by a class-name, but
3669            `S<T>' is dependent, and so not known to be a class.
3670            That's not right; we need to look in uninstantiated
3671            templates.  A further complication arises from:
3672
3673              template <typename T> void f(T t) {
3674                t.T::~T();
3675              }
3676
3677            Here, it is not possible to look up `T' in the scope of `T'
3678            itself.  We must look in both the current scope, and the
3679            scope of the containing complete expression.
3680
3681            Yet another issue is:
3682
3683              struct S {
3684                int S;
3685                ~S();
3686              };
3687
3688              S::~S() {}
3689
3690            The standard does not seem to say that the `S' in `~S'
3691            should refer to the type `S' and not the data member
3692            `S::S'.  */
3693
3694         /* DR 244 says that we look up the name after the "~" in the
3695            same scope as we looked up the qualifying name.  That idea
3696            isn't fully worked out; it's more complicated than that.  */
3697         scope = parser->scope;
3698         object_scope = parser->object_scope;
3699         qualifying_scope = parser->qualifying_scope;
3700
3701         /* Check for invalid scopes.  */
3702         if (scope == error_mark_node)
3703           {
3704             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3705               cp_lexer_consume_token (parser->lexer);
3706             return error_mark_node;
3707           }
3708         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3709           {
3710             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3711               error ("scope %qT before %<~%> is not a class-name", scope);
3712             cp_parser_simulate_error (parser);
3713             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3714               cp_lexer_consume_token (parser->lexer);
3715             return error_mark_node;
3716           }
3717         gcc_assert (!scope || TYPE_P (scope));
3718
3719         /* If the name is of the form "X::~X" it's OK.  */
3720         token = cp_lexer_peek_token (parser->lexer);
3721         if (scope
3722             && token->type == CPP_NAME
3723             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3724                 == CPP_OPEN_PAREN)
3725             && constructor_name_p (token->u.value, scope))
3726           {
3727             cp_lexer_consume_token (parser->lexer);
3728             return build_nt (BIT_NOT_EXPR, scope);
3729           }
3730
3731         /* If there was an explicit qualification (S::~T), first look
3732            in the scope given by the qualification (i.e., S).  */
3733         done = false;
3734         type_decl = NULL_TREE;
3735         if (scope)
3736           {
3737             cp_parser_parse_tentatively (parser);
3738             type_decl = cp_parser_class_name (parser,
3739                                               /*typename_keyword_p=*/false,
3740                                               /*template_keyword_p=*/false,
3741                                               none_type,
3742                                               /*check_dependency=*/false,
3743                                               /*class_head_p=*/false,
3744                                               declarator_p);
3745             if (cp_parser_parse_definitely (parser))
3746               done = true;
3747           }
3748         /* In "N::S::~S", look in "N" as well.  */
3749         if (!done && scope && qualifying_scope)
3750           {
3751             cp_parser_parse_tentatively (parser);
3752             parser->scope = qualifying_scope;
3753             parser->object_scope = NULL_TREE;
3754             parser->qualifying_scope = NULL_TREE;
3755             type_decl
3756               = cp_parser_class_name (parser,
3757                                       /*typename_keyword_p=*/false,
3758                                       /*template_keyword_p=*/false,
3759                                       none_type,
3760                                       /*check_dependency=*/false,
3761                                       /*class_head_p=*/false,
3762                                       declarator_p);
3763             if (cp_parser_parse_definitely (parser))
3764               done = true;
3765           }
3766         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3767         else if (!done && object_scope)
3768           {
3769             cp_parser_parse_tentatively (parser);
3770             parser->scope = object_scope;
3771             parser->object_scope = NULL_TREE;
3772             parser->qualifying_scope = NULL_TREE;
3773             type_decl
3774               = cp_parser_class_name (parser,
3775                                       /*typename_keyword_p=*/false,
3776                                       /*template_keyword_p=*/false,
3777                                       none_type,
3778                                       /*check_dependency=*/false,
3779                                       /*class_head_p=*/false,
3780                                       declarator_p);
3781             if (cp_parser_parse_definitely (parser))
3782               done = true;
3783           }
3784         /* Look in the surrounding context.  */
3785         if (!done)
3786           {
3787             parser->scope = NULL_TREE;
3788             parser->object_scope = NULL_TREE;
3789             parser->qualifying_scope = NULL_TREE;
3790             type_decl
3791               = cp_parser_class_name (parser,
3792                                       /*typename_keyword_p=*/false,
3793                                       /*template_keyword_p=*/false,
3794                                       none_type,
3795                                       /*check_dependency=*/false,
3796                                       /*class_head_p=*/false,
3797                                       declarator_p);
3798           }
3799         /* If an error occurred, assume that the name of the
3800            destructor is the same as the name of the qualifying
3801            class.  That allows us to keep parsing after running
3802            into ill-formed destructor names.  */
3803         if (type_decl == error_mark_node && scope)
3804           return build_nt (BIT_NOT_EXPR, scope);
3805         else if (type_decl == error_mark_node)
3806           return error_mark_node;
3807
3808         /* Check that destructor name and scope match.  */
3809         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3810           {
3811             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3812               error ("declaration of %<~%T%> as member of %qT",
3813                      type_decl, scope);
3814             cp_parser_simulate_error (parser);
3815             return error_mark_node;
3816           }
3817
3818         /* [class.dtor]
3819
3820            A typedef-name that names a class shall not be used as the
3821            identifier in the declarator for a destructor declaration.  */
3822         if (declarator_p
3823             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3824             && !DECL_SELF_REFERENCE_P (type_decl)
3825             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3826           error ("typedef-name %qD used as destructor declarator",
3827                  type_decl);
3828
3829         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3830       }
3831
3832     case CPP_KEYWORD:
3833       if (token->keyword == RID_OPERATOR)
3834         {
3835           tree id;
3836
3837           /* This could be a template-id, so we try that first.  */
3838           cp_parser_parse_tentatively (parser);
3839           /* Try a template-id.  */
3840           id = cp_parser_template_id (parser, template_keyword_p,
3841                                       /*check_dependency_p=*/true,
3842                                       declarator_p);
3843           /* If that worked, we're done.  */
3844           if (cp_parser_parse_definitely (parser))
3845             return id;
3846           /* We still don't know whether we're looking at an
3847              operator-function-id or a conversion-function-id.  */
3848           cp_parser_parse_tentatively (parser);
3849           /* Try an operator-function-id.  */
3850           id = cp_parser_operator_function_id (parser);
3851           /* If that didn't work, try a conversion-function-id.  */
3852           if (!cp_parser_parse_definitely (parser))
3853             id = cp_parser_conversion_function_id (parser);
3854
3855           return id;
3856         }
3857       /* Fall through.  */
3858
3859     default:
3860       if (optional_p)
3861         return NULL_TREE;
3862       cp_parser_error (parser, "expected unqualified-id");
3863       return error_mark_node;
3864     }
3865 }
3866
3867 /* Parse an (optional) nested-name-specifier.
3868
3869    nested-name-specifier:
3870      class-or-namespace-name :: nested-name-specifier [opt]
3871      class-or-namespace-name :: template nested-name-specifier [opt]
3872
3873    PARSER->SCOPE should be set appropriately before this function is
3874    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3875    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3876    in name lookups.
3877
3878    Sets PARSER->SCOPE to the class (TYPE) or namespace
3879    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3880    it unchanged if there is no nested-name-specifier.  Returns the new
3881    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3882
3883    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3884    part of a declaration and/or decl-specifier.  */
3885
3886 static tree
3887 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3888                                      bool typename_keyword_p,
3889                                      bool check_dependency_p,
3890                                      bool type_p,
3891                                      bool is_declaration)
3892 {
3893   bool success = false;
3894   cp_token_position start = 0;
3895   cp_token *token;
3896
3897   /* Remember where the nested-name-specifier starts.  */
3898   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3899     {
3900       start = cp_lexer_token_position (parser->lexer, false);
3901       push_deferring_access_checks (dk_deferred);
3902     }
3903
3904   while (true)
3905     {
3906       tree new_scope;
3907       tree old_scope;
3908       tree saved_qualifying_scope;
3909       bool template_keyword_p;
3910
3911       /* Spot cases that cannot be the beginning of a
3912          nested-name-specifier.  */
3913       token = cp_lexer_peek_token (parser->lexer);
3914
3915       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3916          the already parsed nested-name-specifier.  */
3917       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3918         {
3919           /* Grab the nested-name-specifier and continue the loop.  */
3920           cp_parser_pre_parsed_nested_name_specifier (parser);
3921           /* If we originally encountered this nested-name-specifier
3922              with IS_DECLARATION set to false, we will not have
3923              resolved TYPENAME_TYPEs, so we must do so here.  */
3924           if (is_declaration
3925               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3926             {
3927               new_scope = resolve_typename_type (parser->scope,
3928                                                  /*only_current_p=*/false);
3929               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3930                 parser->scope = new_scope;
3931             }
3932           success = true;
3933           continue;
3934         }
3935
3936       /* Spot cases that cannot be the beginning of a
3937          nested-name-specifier.  On the second and subsequent times
3938          through the loop, we look for the `template' keyword.  */
3939       if (success && token->keyword == RID_TEMPLATE)
3940         ;
3941       /* A template-id can start a nested-name-specifier.  */
3942       else if (token->type == CPP_TEMPLATE_ID)
3943         ;
3944       else
3945         {
3946           /* If the next token is not an identifier, then it is
3947              definitely not a class-or-namespace-name.  */
3948           if (token->type != CPP_NAME)
3949             break;
3950           /* If the following token is neither a `<' (to begin a
3951              template-id), nor a `::', then we are not looking at a
3952              nested-name-specifier.  */
3953           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3954           if (token->type != CPP_SCOPE
3955               && !cp_parser_nth_token_starts_template_argument_list_p
3956                   (parser, 2))
3957             break;
3958         }
3959
3960       /* The nested-name-specifier is optional, so we parse
3961          tentatively.  */
3962       cp_parser_parse_tentatively (parser);
3963
3964       /* Look for the optional `template' keyword, if this isn't the
3965          first time through the loop.  */
3966       if (success)
3967         template_keyword_p = cp_parser_optional_template_keyword (parser);
3968       else
3969         template_keyword_p = false;
3970
3971       /* Save the old scope since the name lookup we are about to do
3972          might destroy it.  */
3973       old_scope = parser->scope;
3974       saved_qualifying_scope = parser->qualifying_scope;
3975       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3976          look up names in "X<T>::I" in order to determine that "Y" is
3977          a template.  So, if we have a typename at this point, we make
3978          an effort to look through it.  */
3979       if (is_declaration
3980           && !typename_keyword_p
3981           && parser->scope
3982           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3983         parser->scope = resolve_typename_type (parser->scope,
3984                                                /*only_current_p=*/false);
3985       /* Parse the qualifying entity.  */
3986       new_scope
3987         = cp_parser_class_or_namespace_name (parser,
3988                                              typename_keyword_p,
3989                                              template_keyword_p,
3990                                              check_dependency_p,
3991                                              type_p,
3992                                              is_declaration);
3993       /* Look for the `::' token.  */
3994       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
3995
3996       /* If we found what we wanted, we keep going; otherwise, we're
3997          done.  */
3998       if (!cp_parser_parse_definitely (parser))
3999         {
4000           bool error_p = false;
4001
4002           /* Restore the OLD_SCOPE since it was valid before the
4003              failed attempt at finding the last
4004              class-or-namespace-name.  */
4005           parser->scope = old_scope;
4006           parser->qualifying_scope = saved_qualifying_scope;
4007           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4008             break;
4009           /* If the next token is an identifier, and the one after
4010              that is a `::', then any valid interpretation would have
4011              found a class-or-namespace-name.  */
4012           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4013                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4014                      == CPP_SCOPE)
4015                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4016                      != CPP_COMPL))
4017             {
4018               token = cp_lexer_consume_token (parser->lexer);
4019               if (!error_p)
4020                 {
4021                   if (!token->ambiguous_p)
4022                     {
4023                       tree decl;
4024                       tree ambiguous_decls;
4025
4026                       decl = cp_parser_lookup_name (parser, token->u.value,
4027                                                     none_type,
4028                                                     /*is_template=*/false,
4029                                                     /*is_namespace=*/false,
4030                                                     /*check_dependency=*/true,
4031                                                     &ambiguous_decls);
4032                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4033                         error ("%qD used without template parameters", decl);
4034                       else if (ambiguous_decls)
4035                         {
4036                           error ("reference to %qD is ambiguous",
4037                                  token->u.value);
4038                           print_candidates (ambiguous_decls);
4039                           decl = error_mark_node;
4040                         }
4041                       else
4042                         cp_parser_name_lookup_error
4043                           (parser, token->u.value, decl,
4044                            "is not a class or namespace");
4045                     }
4046                   parser->scope = error_mark_node;
4047                   error_p = true;
4048                   /* Treat this as a successful nested-name-specifier
4049                      due to:
4050
4051                      [basic.lookup.qual]
4052
4053                      If the name found is not a class-name (clause
4054                      _class_) or namespace-name (_namespace.def_), the
4055                      program is ill-formed.  */
4056                   success = true;
4057                 }
4058               cp_lexer_consume_token (parser->lexer);
4059             }
4060           break;
4061         }
4062       /* We've found one valid nested-name-specifier.  */
4063       success = true;
4064       /* Name lookup always gives us a DECL.  */
4065       if (TREE_CODE (new_scope) == TYPE_DECL)
4066         new_scope = TREE_TYPE (new_scope);
4067       /* Uses of "template" must be followed by actual templates.  */
4068       if (template_keyword_p
4069           && !(CLASS_TYPE_P (new_scope)
4070                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4071                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4072                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4073           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4074                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4075                    == TEMPLATE_ID_EXPR)))
4076         pedwarn (TYPE_P (new_scope)
4077                  ? "%qT is not a template"
4078                  : "%qD is not a template",
4079                  new_scope);
4080       /* If it is a class scope, try to complete it; we are about to
4081          be looking up names inside the class.  */
4082       if (TYPE_P (new_scope)
4083           /* Since checking types for dependency can be expensive,
4084              avoid doing it if the type is already complete.  */
4085           && !COMPLETE_TYPE_P (new_scope)
4086           /* Do not try to complete dependent types.  */
4087           && !dependent_type_p (new_scope))
4088         {
4089           new_scope = complete_type (new_scope);
4090           /* If it is a typedef to current class, use the current
4091              class instead, as the typedef won't have any names inside
4092              it yet.  */
4093           if (!COMPLETE_TYPE_P (new_scope)
4094               && currently_open_class (new_scope))
4095             new_scope = TYPE_MAIN_VARIANT (new_scope);
4096         }
4097       /* Make sure we look in the right scope the next time through
4098          the loop.  */
4099       parser->scope = new_scope;
4100     }
4101
4102   /* If parsing tentatively, replace the sequence of tokens that makes
4103      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4104      token.  That way, should we re-parse the token stream, we will
4105      not have to repeat the effort required to do the parse, nor will
4106      we issue duplicate error messages.  */
4107   if (success && start)
4108     {
4109       cp_token *token;
4110
4111       token = cp_lexer_token_at (parser->lexer, start);
4112       /* Reset the contents of the START token.  */
4113       token->type = CPP_NESTED_NAME_SPECIFIER;
4114       /* Retrieve any deferred checks.  Do not pop this access checks yet
4115          so the memory will not be reclaimed during token replacing below.  */
4116       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4117       token->u.tree_check_value->value = parser->scope;
4118       token->u.tree_check_value->checks = get_deferred_access_checks ();
4119       token->u.tree_check_value->qualifying_scope =
4120         parser->qualifying_scope;
4121       token->keyword = RID_MAX;
4122
4123       /* Purge all subsequent tokens.  */
4124       cp_lexer_purge_tokens_after (parser->lexer, start);
4125     }
4126
4127   if (start)
4128     pop_to_parent_deferring_access_checks ();
4129
4130   return success ? parser->scope : NULL_TREE;
4131 }
4132
4133 /* Parse a nested-name-specifier.  See
4134    cp_parser_nested_name_specifier_opt for details.  This function
4135    behaves identically, except that it will an issue an error if no
4136    nested-name-specifier is present.  */
4137
4138 static tree
4139 cp_parser_nested_name_specifier (cp_parser *parser,
4140                                  bool typename_keyword_p,
4141                                  bool check_dependency_p,
4142                                  bool type_p,
4143                                  bool is_declaration)
4144 {
4145   tree scope;
4146
4147   /* Look for the nested-name-specifier.  */
4148   scope = cp_parser_nested_name_specifier_opt (parser,
4149                                                typename_keyword_p,
4150                                                check_dependency_p,
4151                                                type_p,
4152                                                is_declaration);
4153   /* If it was not present, issue an error message.  */
4154   if (!scope)
4155     {
4156       cp_parser_error (parser, "expected nested-name-specifier");
4157       parser->scope = NULL_TREE;
4158     }
4159
4160   return scope;
4161 }
4162
4163 /* Parse a class-or-namespace-name.
4164
4165    class-or-namespace-name:
4166      class-name
4167      namespace-name
4168
4169    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4170    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4171    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4172    TYPE_P is TRUE iff the next name should be taken as a class-name,
4173    even the same name is declared to be another entity in the same
4174    scope.
4175
4176    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4177    specified by the class-or-namespace-name.  If neither is found the
4178    ERROR_MARK_NODE is returned.  */
4179
4180 static tree
4181 cp_parser_class_or_namespace_name (cp_parser *parser,
4182                                    bool typename_keyword_p,
4183                                    bool template_keyword_p,
4184                                    bool check_dependency_p,
4185                                    bool type_p,
4186                                    bool is_declaration)
4187 {
4188   tree saved_scope;
4189   tree saved_qualifying_scope;
4190   tree saved_object_scope;
4191   tree scope;
4192   bool only_class_p;
4193
4194   /* Before we try to parse the class-name, we must save away the
4195      current PARSER->SCOPE since cp_parser_class_name will destroy
4196      it.  */
4197   saved_scope = parser->scope;
4198   saved_qualifying_scope = parser->qualifying_scope;
4199   saved_object_scope = parser->object_scope;
4200   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4201      there is no need to look for a namespace-name.  */
4202   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4203   if (!only_class_p)
4204     cp_parser_parse_tentatively (parser);
4205   scope = cp_parser_class_name (parser,
4206                                 typename_keyword_p,
4207                                 template_keyword_p,
4208                                 type_p ? class_type : none_type,
4209                                 check_dependency_p,
4210                                 /*class_head_p=*/false,
4211                                 is_declaration);
4212   /* If that didn't work, try for a namespace-name.  */
4213   if (!only_class_p && !cp_parser_parse_definitely (parser))
4214     {
4215       /* Restore the saved scope.  */
4216       parser->scope = saved_scope;
4217       parser->qualifying_scope = saved_qualifying_scope;
4218       parser->object_scope = saved_object_scope;
4219       /* If we are not looking at an identifier followed by the scope
4220          resolution operator, then this is not part of a
4221          nested-name-specifier.  (Note that this function is only used
4222          to parse the components of a nested-name-specifier.)  */
4223       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4224           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4225         return error_mark_node;
4226       scope = cp_parser_namespace_name (parser);
4227     }
4228
4229   return scope;
4230 }
4231
4232 /* Parse a postfix-expression.
4233
4234    postfix-expression:
4235      primary-expression
4236      postfix-expression [ expression ]
4237      postfix-expression ( expression-list [opt] )
4238      simple-type-specifier ( expression-list [opt] )
4239      typename :: [opt] nested-name-specifier identifier
4240        ( expression-list [opt] )
4241      typename :: [opt] nested-name-specifier template [opt] template-id
4242        ( expression-list [opt] )
4243      postfix-expression . template [opt] id-expression
4244      postfix-expression -> template [opt] id-expression
4245      postfix-expression . pseudo-destructor-name
4246      postfix-expression -> pseudo-destructor-name
4247      postfix-expression ++
4248      postfix-expression --
4249      dynamic_cast < type-id > ( expression )
4250      static_cast < type-id > ( expression )
4251      reinterpret_cast < type-id > ( expression )
4252      const_cast < type-id > ( expression )
4253      typeid ( expression )
4254      typeid ( type-id )
4255
4256    GNU Extension:
4257
4258    postfix-expression:
4259      ( type-id ) { initializer-list , [opt] }
4260
4261    This extension is a GNU version of the C99 compound-literal
4262    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4263    but they are essentially the same concept.)
4264
4265    If ADDRESS_P is true, the postfix expression is the operand of the
4266    `&' operator.  CAST_P is true if this expression is the target of a
4267    cast.
4268
4269    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4270    class member access expressions [expr.ref].
4271
4272    Returns a representation of the expression.  */
4273
4274 static tree
4275 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4276                               bool member_access_only_p)
4277 {
4278   cp_token *token;
4279   enum rid keyword;
4280   cp_id_kind idk = CP_ID_KIND_NONE;
4281   tree postfix_expression = NULL_TREE;
4282   bool is_member_access = false;
4283
4284   /* Peek at the next token.  */
4285   token = cp_lexer_peek_token (parser->lexer);
4286   /* Some of the productions are determined by keywords.  */
4287   keyword = token->keyword;
4288   switch (keyword)
4289     {
4290     case RID_DYNCAST:
4291     case RID_STATCAST:
4292     case RID_REINTCAST:
4293     case RID_CONSTCAST:
4294       {
4295         tree type;
4296         tree expression;
4297         const char *saved_message;
4298
4299         /* All of these can be handled in the same way from the point
4300            of view of parsing.  Begin by consuming the token
4301            identifying the cast.  */
4302         cp_lexer_consume_token (parser->lexer);
4303
4304         /* New types cannot be defined in the cast.  */
4305         saved_message = parser->type_definition_forbidden_message;
4306         parser->type_definition_forbidden_message
4307           = "types may not be defined in casts";
4308
4309         /* Look for the opening `<'.  */
4310         cp_parser_require (parser, CPP_LESS, "%<<%>");
4311         /* Parse the type to which we are casting.  */
4312         type = cp_parser_type_id (parser);
4313         /* Look for the closing `>'.  */
4314         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4315         /* Restore the old message.  */
4316         parser->type_definition_forbidden_message = saved_message;
4317
4318         /* And the expression which is being cast.  */
4319         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4320         expression = cp_parser_expression (parser, /*cast_p=*/true);
4321         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4322
4323         /* Only type conversions to integral or enumeration types
4324            can be used in constant-expressions.  */
4325         if (!cast_valid_in_integral_constant_expression_p (type)
4326             && (cp_parser_non_integral_constant_expression
4327                 (parser,
4328                  "a cast to a type other than an integral or "
4329                  "enumeration type")))
4330           return error_mark_node;
4331
4332         switch (keyword)
4333           {
4334           case RID_DYNCAST:
4335             postfix_expression
4336               = build_dynamic_cast (type, expression, tf_warning_or_error);
4337             break;
4338           case RID_STATCAST:
4339             postfix_expression
4340               = build_static_cast (type, expression, tf_warning_or_error);
4341             break;
4342           case RID_REINTCAST:
4343             postfix_expression
4344               = build_reinterpret_cast (type, expression, 
4345                                         tf_warning_or_error);
4346             break;
4347           case RID_CONSTCAST:
4348             postfix_expression
4349               = build_const_cast (type, expression, tf_warning_or_error);
4350             break;
4351           default:
4352             gcc_unreachable ();
4353           }
4354       }
4355       break;
4356
4357     case RID_TYPEID:
4358       {
4359         tree type;
4360         const char *saved_message;
4361         bool saved_in_type_id_in_expr_p;
4362
4363         /* Consume the `typeid' token.  */
4364         cp_lexer_consume_token (parser->lexer);
4365         /* Look for the `(' token.  */
4366         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4367         /* Types cannot be defined in a `typeid' expression.  */
4368         saved_message = parser->type_definition_forbidden_message;
4369         parser->type_definition_forbidden_message
4370           = "types may not be defined in a `typeid\' expression";
4371         /* We can't be sure yet whether we're looking at a type-id or an
4372            expression.  */
4373         cp_parser_parse_tentatively (parser);
4374         /* Try a type-id first.  */
4375         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4376         parser->in_type_id_in_expr_p = true;
4377         type = cp_parser_type_id (parser);
4378         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4379         /* Look for the `)' token.  Otherwise, we can't be sure that
4380            we're not looking at an expression: consider `typeid (int
4381            (3))', for example.  */
4382         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4383         /* If all went well, simply lookup the type-id.  */
4384         if (cp_parser_parse_definitely (parser))
4385           postfix_expression = get_typeid (type);
4386         /* Otherwise, fall back to the expression variant.  */
4387         else
4388           {
4389             tree expression;
4390
4391             /* Look for an expression.  */
4392             expression = cp_parser_expression (parser, /*cast_p=*/false);
4393             /* Compute its typeid.  */
4394             postfix_expression = build_typeid (expression);
4395             /* Look for the `)' token.  */
4396             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4397           }
4398         /* Restore the saved message.  */
4399         parser->type_definition_forbidden_message = saved_message;
4400         /* `typeid' may not appear in an integral constant expression.  */
4401         if (cp_parser_non_integral_constant_expression(parser,
4402                                                        "`typeid' operator"))
4403           return error_mark_node;
4404       }
4405       break;
4406
4407     case RID_TYPENAME:
4408       {
4409         tree type;
4410         /* The syntax permitted here is the same permitted for an
4411            elaborated-type-specifier.  */
4412         type = cp_parser_elaborated_type_specifier (parser,
4413                                                     /*is_friend=*/false,
4414                                                     /*is_declaration=*/false);
4415         postfix_expression = cp_parser_functional_cast (parser, type);
4416       }
4417       break;
4418
4419     default:
4420       {
4421         tree type;
4422
4423         /* If the next thing is a simple-type-specifier, we may be
4424            looking at a functional cast.  We could also be looking at
4425            an id-expression.  So, we try the functional cast, and if
4426            that doesn't work we fall back to the primary-expression.  */
4427         cp_parser_parse_tentatively (parser);
4428         /* Look for the simple-type-specifier.  */
4429         type = cp_parser_simple_type_specifier (parser,
4430                                                 /*decl_specs=*/NULL,
4431                                                 CP_PARSER_FLAGS_NONE);
4432         /* Parse the cast itself.  */
4433         if (!cp_parser_error_occurred (parser))
4434           postfix_expression
4435             = cp_parser_functional_cast (parser, type);
4436         /* If that worked, we're done.  */
4437         if (cp_parser_parse_definitely (parser))
4438           break;
4439
4440         /* If the functional-cast didn't work out, try a
4441            compound-literal.  */
4442         if (cp_parser_allow_gnu_extensions_p (parser)
4443             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4444           {
4445             VEC(constructor_elt,gc) *initializer_list = NULL;
4446             bool saved_in_type_id_in_expr_p;
4447
4448             cp_parser_parse_tentatively (parser);
4449             /* Consume the `('.  */
4450             cp_lexer_consume_token (parser->lexer);
4451             /* Parse the type.  */
4452             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4453             parser->in_type_id_in_expr_p = true;
4454             type = cp_parser_type_id (parser);
4455             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4456             /* Look for the `)'.  */
4457             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4458             /* Look for the `{'.  */
4459             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4460             /* If things aren't going well, there's no need to
4461                keep going.  */
4462             if (!cp_parser_error_occurred (parser))
4463               {
4464                 bool non_constant_p;
4465                 /* Parse the initializer-list.  */
4466                 initializer_list
4467                   = cp_parser_initializer_list (parser, &non_constant_p);
4468                 /* Allow a trailing `,'.  */
4469                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4470                   cp_lexer_consume_token (parser->lexer);
4471                 /* Look for the final `}'.  */
4472                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4473               }
4474             /* If that worked, we're definitely looking at a
4475                compound-literal expression.  */
4476             if (cp_parser_parse_definitely (parser))
4477               {
4478                 /* Warn the user that a compound literal is not
4479                    allowed in standard C++.  */
4480                 if (pedantic)
4481                   pedwarn ("ISO C++ forbids compound-literals");
4482                 /* For simplicity, we disallow compound literals in
4483                    constant-expressions.  We could
4484                    allow compound literals of integer type, whose
4485                    initializer was a constant, in constant
4486                    expressions.  Permitting that usage, as a further
4487                    extension, would not change the meaning of any
4488                    currently accepted programs.  (Of course, as
4489                    compound literals are not part of ISO C++, the
4490                    standard has nothing to say.)  */
4491                 if (cp_parser_non_integral_constant_expression 
4492                     (parser, "non-constant compound literals"))
4493                   {
4494                     postfix_expression = error_mark_node;
4495                     break;
4496                   }
4497                 /* Form the representation of the compound-literal.  */
4498                 postfix_expression
4499                   = finish_compound_literal (type, initializer_list);
4500                 break;
4501               }
4502           }
4503
4504         /* It must be a primary-expression.  */
4505         postfix_expression
4506           = cp_parser_primary_expression (parser, address_p, cast_p,
4507                                           /*template_arg_p=*/false,
4508                                           &idk);
4509       }
4510       break;
4511     }
4512
4513   /* Keep looping until the postfix-expression is complete.  */
4514   while (true)
4515     {
4516       if (idk == CP_ID_KIND_UNQUALIFIED
4517           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4518           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4519         /* It is not a Koenig lookup function call.  */
4520         postfix_expression
4521           = unqualified_name_lookup_error (postfix_expression);
4522
4523       /* Peek at the next token.  */
4524       token = cp_lexer_peek_token (parser->lexer);
4525
4526       switch (token->type)
4527         {
4528         case CPP_OPEN_SQUARE:
4529           postfix_expression
4530             = cp_parser_postfix_open_square_expression (parser,
4531                                                         postfix_expression,
4532                                                         false);
4533           idk = CP_ID_KIND_NONE;
4534           is_member_access = false;
4535           break;
4536
4537         case CPP_OPEN_PAREN:
4538           /* postfix-expression ( expression-list [opt] ) */
4539           {
4540             bool koenig_p;
4541             bool is_builtin_constant_p;
4542             bool saved_integral_constant_expression_p = false;
4543             bool saved_non_integral_constant_expression_p = false;
4544             tree args;
4545
4546             is_member_access = false;
4547
4548             is_builtin_constant_p
4549               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4550             if (is_builtin_constant_p)
4551               {
4552                 /* The whole point of __builtin_constant_p is to allow
4553                    non-constant expressions to appear as arguments.  */
4554                 saved_integral_constant_expression_p
4555                   = parser->integral_constant_expression_p;
4556                 saved_non_integral_constant_expression_p
4557                   = parser->non_integral_constant_expression_p;
4558                 parser->integral_constant_expression_p = false;
4559               }
4560             args = (cp_parser_parenthesized_expression_list
4561                     (parser, /*is_attribute_list=*/false,
4562                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4563                      /*non_constant_p=*/NULL));
4564             if (is_builtin_constant_p)
4565               {
4566                 parser->integral_constant_expression_p
4567                   = saved_integral_constant_expression_p;
4568                 parser->non_integral_constant_expression_p
4569                   = saved_non_integral_constant_expression_p;
4570               }
4571
4572             if (args == error_mark_node)
4573               {
4574                 postfix_expression = error_mark_node;
4575                 break;
4576               }
4577
4578             /* Function calls are not permitted in
4579                constant-expressions.  */
4580             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4581                 && cp_parser_non_integral_constant_expression (parser,
4582                                                                "a function call"))
4583               {
4584                 postfix_expression = error_mark_node;
4585                 break;
4586               }
4587
4588             koenig_p = false;
4589             if (idk == CP_ID_KIND_UNQUALIFIED)
4590               {
4591                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4592                   {
4593                     if (args)
4594                       {
4595                         koenig_p = true;
4596                         postfix_expression
4597                           = perform_koenig_lookup (postfix_expression, args);
4598                       }
4599                     else
4600                       postfix_expression
4601                         = unqualified_fn_lookup_error (postfix_expression);
4602                   }
4603                 /* We do not perform argument-dependent lookup if
4604                    normal lookup finds a non-function, in accordance
4605                    with the expected resolution of DR 218.  */
4606                 else if (args && is_overloaded_fn (postfix_expression))
4607                   {
4608                     tree fn = get_first_fn (postfix_expression);
4609
4610                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4611                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4612
4613                     /* Only do argument dependent lookup if regular
4614                        lookup does not find a set of member functions.
4615                        [basic.lookup.koenig]/2a  */
4616                     if (!DECL_FUNCTION_MEMBER_P (fn))
4617                       {
4618                         koenig_p = true;
4619                         postfix_expression
4620                           = perform_koenig_lookup (postfix_expression, args);
4621                       }
4622                   }
4623               }
4624
4625             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4626               {
4627                 tree instance = TREE_OPERAND (postfix_expression, 0);
4628                 tree fn = TREE_OPERAND (postfix_expression, 1);
4629
4630                 if (processing_template_decl
4631                     && (type_dependent_expression_p (instance)
4632                         || (!BASELINK_P (fn)
4633                             && TREE_CODE (fn) != FIELD_DECL)
4634                         || type_dependent_expression_p (fn)
4635                         || any_type_dependent_arguments_p (args)))
4636                   {
4637                     postfix_expression
4638                       = build_nt_call_list (postfix_expression, args);
4639                     break;
4640                   }
4641
4642                 if (BASELINK_P (fn))
4643                   postfix_expression
4644                     = (build_new_method_call
4645                        (instance, fn, args, NULL_TREE,
4646                         (idk == CP_ID_KIND_QUALIFIED
4647                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4648                         /*fn_p=*/NULL,
4649                         tf_warning_or_error));
4650                 else
4651                   postfix_expression
4652                     = finish_call_expr (postfix_expression, args,
4653                                         /*disallow_virtual=*/false,
4654                                         /*koenig_p=*/false,
4655                                         tf_warning_or_error);
4656               }
4657             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4658                      || TREE_CODE (postfix_expression) == MEMBER_REF
4659                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4660               postfix_expression = (build_offset_ref_call_from_tree
4661                                     (postfix_expression, args));
4662             else if (idk == CP_ID_KIND_QUALIFIED)
4663               /* A call to a static class member, or a namespace-scope
4664                  function.  */
4665               postfix_expression
4666                 = finish_call_expr (postfix_expression, args,
4667                                     /*disallow_virtual=*/true,
4668                                     koenig_p,
4669                                     tf_warning_or_error);
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                                     tf_warning_or_error);
4677
4678             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4679             idk = CP_ID_KIND_NONE;
4680           }
4681           break;
4682
4683         case CPP_DOT:
4684         case CPP_DEREF:
4685           /* postfix-expression . template [opt] id-expression
4686              postfix-expression . pseudo-destructor-name
4687              postfix-expression -> template [opt] id-expression
4688              postfix-expression -> pseudo-destructor-name */
4689
4690           /* Consume the `.' or `->' operator.  */
4691           cp_lexer_consume_token (parser->lexer);
4692
4693           postfix_expression
4694             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4695                                                       postfix_expression,
4696                                                       false, &idk);
4697
4698           is_member_access = true;
4699           break;
4700
4701         case CPP_PLUS_PLUS:
4702           /* postfix-expression ++  */
4703           /* Consume the `++' token.  */
4704           cp_lexer_consume_token (parser->lexer);
4705           /* Generate a representation for the complete expression.  */
4706           postfix_expression
4707             = finish_increment_expr (postfix_expression,
4708                                      POSTINCREMENT_EXPR);
4709           /* Increments may not appear in constant-expressions.  */
4710           if (cp_parser_non_integral_constant_expression (parser,
4711                                                           "an increment"))
4712             postfix_expression = error_mark_node;
4713           idk = CP_ID_KIND_NONE;
4714           is_member_access = false;
4715           break;
4716
4717         case CPP_MINUS_MINUS:
4718           /* postfix-expression -- */
4719           /* Consume the `--' token.  */
4720           cp_lexer_consume_token (parser->lexer);
4721           /* Generate a representation for the complete expression.  */
4722           postfix_expression
4723             = finish_increment_expr (postfix_expression,
4724                                      POSTDECREMENT_EXPR);
4725           /* Decrements may not appear in constant-expressions.  */
4726           if (cp_parser_non_integral_constant_expression (parser,
4727                                                           "a decrement"))
4728             postfix_expression = error_mark_node;
4729           idk = CP_ID_KIND_NONE;
4730           is_member_access = false;
4731           break;
4732
4733         default:
4734           if (member_access_only_p)
4735             return is_member_access? postfix_expression : error_mark_node;
4736           else
4737             return postfix_expression;
4738         }
4739     }
4740
4741   /* We should never get here.  */
4742   gcc_unreachable ();
4743   return error_mark_node;
4744 }
4745
4746 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4747    by cp_parser_builtin_offsetof.  We're looking for
4748
4749      postfix-expression [ expression ]
4750
4751    FOR_OFFSETOF is set if we're being called in that context, which
4752    changes how we deal with integer constant expressions.  */
4753
4754 static tree
4755 cp_parser_postfix_open_square_expression (cp_parser *parser,
4756                                           tree postfix_expression,
4757                                           bool for_offsetof)
4758 {
4759   tree index;
4760
4761   /* Consume the `[' token.  */
4762   cp_lexer_consume_token (parser->lexer);
4763
4764   /* Parse the index expression.  */
4765   /* ??? For offsetof, there is a question of what to allow here.  If
4766      offsetof is not being used in an integral constant expression context,
4767      then we *could* get the right answer by computing the value at runtime.
4768      If we are in an integral constant expression context, then we might
4769      could accept any constant expression; hard to say without analysis.
4770      Rather than open the barn door too wide right away, allow only integer
4771      constant expressions here.  */
4772   if (for_offsetof)
4773     index = cp_parser_constant_expression (parser, false, NULL);
4774   else
4775     index = cp_parser_expression (parser, /*cast_p=*/false);
4776
4777   /* Look for the closing `]'.  */
4778   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4779
4780   /* Build the ARRAY_REF.  */
4781   postfix_expression = grok_array_decl (postfix_expression, index);
4782
4783   /* When not doing offsetof, array references are not permitted in
4784      constant-expressions.  */
4785   if (!for_offsetof
4786       && (cp_parser_non_integral_constant_expression
4787           (parser, "an array reference")))
4788     postfix_expression = error_mark_node;
4789
4790   return postfix_expression;
4791 }
4792
4793 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4794    by cp_parser_builtin_offsetof.  We're looking for
4795
4796      postfix-expression . template [opt] id-expression
4797      postfix-expression . pseudo-destructor-name
4798      postfix-expression -> template [opt] id-expression
4799      postfix-expression -> pseudo-destructor-name
4800
4801    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4802    limits what of the above we'll actually accept, but nevermind.
4803    TOKEN_TYPE is the "." or "->" token, which will already have been
4804    removed from the stream.  */
4805
4806 static tree
4807 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4808                                         enum cpp_ttype token_type,
4809                                         tree postfix_expression,
4810                                         bool for_offsetof, cp_id_kind *idk)
4811 {
4812   tree name;
4813   bool dependent_p;
4814   bool pseudo_destructor_p;
4815   tree scope = NULL_TREE;
4816
4817   /* If this is a `->' operator, dereference the pointer.  */
4818   if (token_type == CPP_DEREF)
4819     postfix_expression = build_x_arrow (postfix_expression);
4820   /* Check to see whether or not the expression is type-dependent.  */
4821   dependent_p = type_dependent_expression_p (postfix_expression);
4822   /* The identifier following the `->' or `.' is not qualified.  */
4823   parser->scope = NULL_TREE;
4824   parser->qualifying_scope = NULL_TREE;
4825   parser->object_scope = NULL_TREE;
4826   *idk = CP_ID_KIND_NONE;
4827   /* Enter the scope corresponding to the type of the object
4828      given by the POSTFIX_EXPRESSION.  */
4829   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4830     {
4831       scope = TREE_TYPE (postfix_expression);
4832       /* According to the standard, no expression should ever have
4833          reference type.  Unfortunately, we do not currently match
4834          the standard in this respect in that our internal representation
4835          of an expression may have reference type even when the standard
4836          says it does not.  Therefore, we have to manually obtain the
4837          underlying type here.  */
4838       scope = non_reference (scope);
4839       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4840       if (scope == unknown_type_node)
4841         {
4842           error ("%qE does not have class type", postfix_expression);
4843           scope = NULL_TREE;
4844         }
4845       else
4846         scope = complete_type_or_else (scope, NULL_TREE);
4847       /* Let the name lookup machinery know that we are processing a
4848          class member access expression.  */
4849       parser->context->object_type = scope;
4850       /* If something went wrong, we want to be able to discern that case,
4851          as opposed to the case where there was no SCOPE due to the type
4852          of expression being dependent.  */
4853       if (!scope)
4854         scope = error_mark_node;
4855       /* If the SCOPE was erroneous, make the various semantic analysis
4856          functions exit quickly -- and without issuing additional error
4857          messages.  */
4858       if (scope == error_mark_node)
4859         postfix_expression = error_mark_node;
4860     }
4861
4862   /* Assume this expression is not a pseudo-destructor access.  */
4863   pseudo_destructor_p = false;
4864
4865   /* If the SCOPE is a scalar type, then, if this is a valid program,
4866      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4867      is type dependent, it can be pseudo-destructor-name or something else.
4868      Try to parse it as pseudo-destructor-name first.  */
4869   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4870     {
4871       tree s;
4872       tree type;
4873
4874       cp_parser_parse_tentatively (parser);
4875       /* Parse the pseudo-destructor-name.  */
4876       s = NULL_TREE;
4877       cp_parser_pseudo_destructor_name (parser, &s, &type);
4878       if (dependent_p
4879           && (cp_parser_error_occurred (parser)
4880               || TREE_CODE (type) != TYPE_DECL
4881               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4882         cp_parser_abort_tentative_parse (parser);
4883       else if (cp_parser_parse_definitely (parser))
4884         {
4885           pseudo_destructor_p = true;
4886           postfix_expression
4887             = finish_pseudo_destructor_expr (postfix_expression,
4888                                              s, TREE_TYPE (type));
4889         }
4890     }
4891
4892   if (!pseudo_destructor_p)
4893     {
4894       /* If the SCOPE is not a scalar type, we are looking at an
4895          ordinary class member access expression, rather than a
4896          pseudo-destructor-name.  */
4897       bool template_p;
4898       /* Parse the id-expression.  */
4899       name = (cp_parser_id_expression
4900               (parser,
4901                cp_parser_optional_template_keyword (parser),
4902                /*check_dependency_p=*/true,
4903                &template_p,
4904                /*declarator_p=*/false,
4905                /*optional_p=*/false));
4906       /* In general, build a SCOPE_REF if the member name is qualified.
4907          However, if the name was not dependent and has already been
4908          resolved; there is no need to build the SCOPE_REF.  For example;
4909
4910              struct X { void f(); };
4911              template <typename T> void f(T* t) { t->X::f(); }
4912
4913          Even though "t" is dependent, "X::f" is not and has been resolved
4914          to a BASELINK; there is no need to include scope information.  */
4915
4916       /* But we do need to remember that there was an explicit scope for
4917          virtual function calls.  */
4918       if (parser->scope)
4919         *idk = CP_ID_KIND_QUALIFIED;
4920
4921       /* If the name is a template-id that names a type, we will get a
4922          TYPE_DECL here.  That is invalid code.  */
4923       if (TREE_CODE (name) == TYPE_DECL)
4924         {
4925           error ("invalid use of %qD", name);
4926           postfix_expression = error_mark_node;
4927         }
4928       else
4929         {
4930           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4931             {
4932               name = build_qualified_name (/*type=*/NULL_TREE,
4933                                            parser->scope,
4934                                            name,
4935                                            template_p);
4936               parser->scope = NULL_TREE;
4937               parser->qualifying_scope = NULL_TREE;
4938               parser->object_scope = NULL_TREE;
4939             }
4940           if (scope && name && BASELINK_P (name))
4941             adjust_result_of_qualified_name_lookup
4942               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4943           postfix_expression
4944             = finish_class_member_access_expr (postfix_expression, name,
4945                                                template_p, 
4946                                                tf_warning_or_error);
4947         }
4948     }
4949
4950   /* We no longer need to look up names in the scope of the object on
4951      the left-hand side of the `.' or `->' operator.  */
4952   parser->context->object_type = NULL_TREE;
4953
4954   /* Outside of offsetof, these operators may not appear in
4955      constant-expressions.  */
4956   if (!for_offsetof
4957       && (cp_parser_non_integral_constant_expression
4958           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4959     postfix_expression = error_mark_node;
4960
4961   return postfix_expression;
4962 }
4963
4964 /* Parse a parenthesized expression-list.
4965
4966    expression-list:
4967      assignment-expression
4968      expression-list, assignment-expression
4969
4970    attribute-list:
4971      expression-list
4972      identifier
4973      identifier, expression-list
4974
4975    CAST_P is true if this expression is the target of a cast.
4976
4977    ALLOW_EXPANSION_P is true if this expression allows expansion of an
4978    argument pack.
4979
4980    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4981    representation of an assignment-expression.  Note that a TREE_LIST
4982    is returned even if there is only a single expression in the list.
4983    error_mark_node is returned if the ( and or ) are
4984    missing. NULL_TREE is returned on no expressions. The parentheses
4985    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4986    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4987    indicates whether or not all of the expressions in the list were
4988    constant.  */
4989
4990 static tree
4991 cp_parser_parenthesized_expression_list (cp_parser* parser,
4992                                          bool is_attribute_list,
4993                                          bool cast_p,
4994                                          bool allow_expansion_p,
4995                                          bool *non_constant_p)
4996 {
4997   tree expression_list = NULL_TREE;
4998   bool fold_expr_p = is_attribute_list;
4999   tree identifier = NULL_TREE;
5000   bool saved_greater_than_is_operator_p;
5001
5002   /* Assume all the expressions will be constant.  */
5003   if (non_constant_p)
5004     *non_constant_p = false;
5005
5006   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5007     return error_mark_node;
5008
5009   /* Within a parenthesized expression, a `>' token is always
5010      the greater-than operator.  */
5011   saved_greater_than_is_operator_p
5012     = parser->greater_than_is_operator_p;
5013   parser->greater_than_is_operator_p = true;
5014
5015   /* Consume expressions until there are no more.  */
5016   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5017     while (true)
5018       {
5019         tree expr;
5020
5021         /* At the beginning of attribute lists, check to see if the
5022            next token is an identifier.  */
5023         if (is_attribute_list
5024             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5025           {
5026             cp_token *token;
5027
5028             /* Consume the identifier.  */
5029             token = cp_lexer_consume_token (parser->lexer);
5030             /* Save the identifier.  */
5031             identifier = token->u.value;
5032           }
5033         else
5034           {
5035             /* Parse the next assignment-expression.  */
5036             if (non_constant_p)
5037               {
5038                 bool expr_non_constant_p;
5039                 expr = (cp_parser_constant_expression
5040                         (parser, /*allow_non_constant_p=*/true,
5041                          &expr_non_constant_p));
5042                 if (expr_non_constant_p)
5043                   *non_constant_p = true;
5044               }
5045             else
5046               expr = cp_parser_assignment_expression (parser, cast_p);
5047
5048             if (fold_expr_p)
5049               expr = fold_non_dependent_expr (expr);
5050
5051             /* If we have an ellipsis, then this is an expression
5052                expansion.  */
5053             if (allow_expansion_p
5054                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5055               {
5056                 /* Consume the `...'.  */
5057                 cp_lexer_consume_token (parser->lexer);
5058
5059                 /* Build the argument pack.  */
5060                 expr = make_pack_expansion (expr);
5061               }
5062
5063              /* Add it to the list.  We add error_mark_node
5064                 expressions to the list, so that we can still tell if
5065                 the correct form for a parenthesized expression-list
5066                 is found. That gives better errors.  */
5067             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5068
5069             if (expr == error_mark_node)
5070               goto skip_comma;
5071           }
5072
5073         /* After the first item, attribute lists look the same as
5074            expression lists.  */
5075         is_attribute_list = false;
5076
5077       get_comma:;
5078         /* If the next token isn't a `,', then we are done.  */
5079         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5080           break;
5081
5082         /* Otherwise, consume the `,' and keep going.  */
5083         cp_lexer_consume_token (parser->lexer);
5084       }
5085
5086   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5087     {
5088       int ending;
5089
5090     skip_comma:;
5091       /* We try and resync to an unnested comma, as that will give the
5092          user better diagnostics.  */
5093       ending = cp_parser_skip_to_closing_parenthesis (parser,
5094                                                       /*recovering=*/true,
5095                                                       /*or_comma=*/true,
5096                                                       /*consume_paren=*/true);
5097       if (ending < 0)
5098         goto get_comma;
5099       if (!ending)
5100         {
5101           parser->greater_than_is_operator_p
5102             = saved_greater_than_is_operator_p;
5103           return error_mark_node;
5104         }
5105     }
5106
5107   parser->greater_than_is_operator_p
5108     = saved_greater_than_is_operator_p;
5109
5110   /* We built up the list in reverse order so we must reverse it now.  */
5111   expression_list = nreverse (expression_list);
5112   if (identifier)
5113     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5114
5115   return expression_list;
5116 }
5117
5118 /* Parse a pseudo-destructor-name.
5119
5120    pseudo-destructor-name:
5121      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5122      :: [opt] nested-name-specifier template template-id :: ~ type-name
5123      :: [opt] nested-name-specifier [opt] ~ type-name
5124
5125    If either of the first two productions is used, sets *SCOPE to the
5126    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5127    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5128    or ERROR_MARK_NODE if the parse fails.  */
5129
5130 static void
5131 cp_parser_pseudo_destructor_name (cp_parser* parser,
5132                                   tree* scope,
5133                                   tree* type)
5134 {
5135   bool nested_name_specifier_p;
5136
5137   /* Assume that things will not work out.  */
5138   *type = error_mark_node;
5139
5140   /* Look for the optional `::' operator.  */
5141   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5142   /* Look for the optional nested-name-specifier.  */
5143   nested_name_specifier_p
5144     = (cp_parser_nested_name_specifier_opt (parser,
5145                                             /*typename_keyword_p=*/false,
5146                                             /*check_dependency_p=*/true,
5147                                             /*type_p=*/false,
5148                                             /*is_declaration=*/true)
5149        != NULL_TREE);
5150   /* Now, if we saw a nested-name-specifier, we might be doing the
5151      second production.  */
5152   if (nested_name_specifier_p
5153       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5154     {
5155       /* Consume the `template' keyword.  */
5156       cp_lexer_consume_token (parser->lexer);
5157       /* Parse the template-id.  */
5158       cp_parser_template_id (parser,
5159                              /*template_keyword_p=*/true,
5160                              /*check_dependency_p=*/false,
5161                              /*is_declaration=*/true);
5162       /* Look for the `::' token.  */
5163       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5164     }
5165   /* If the next token is not a `~', then there might be some
5166      additional qualification.  */
5167   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5168     {
5169       /* At this point, we're looking for "type-name :: ~".  The type-name
5170          must not be a class-name, since this is a pseudo-destructor.  So,
5171          it must be either an enum-name, or a typedef-name -- both of which
5172          are just identifiers.  So, we peek ahead to check that the "::"
5173          and "~" tokens are present; if they are not, then we can avoid
5174          calling type_name.  */
5175       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5176           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5177           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5178         {
5179           cp_parser_error (parser, "non-scalar type");
5180           return;
5181         }
5182
5183       /* Look for the type-name.  */
5184       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5185       if (*scope == error_mark_node)
5186         return;
5187
5188       /* Look for the `::' token.  */
5189       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5190     }
5191   else
5192     *scope = NULL_TREE;
5193
5194   /* Look for the `~'.  */
5195   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5196   /* Look for the type-name again.  We are not responsible for
5197      checking that it matches the first type-name.  */
5198   *type = cp_parser_nonclass_name (parser);
5199 }
5200
5201 /* Parse a unary-expression.
5202
5203    unary-expression:
5204      postfix-expression
5205      ++ cast-expression
5206      -- cast-expression
5207      unary-operator cast-expression
5208      sizeof unary-expression
5209      sizeof ( type-id )
5210      new-expression
5211      delete-expression
5212
5213    GNU Extensions:
5214
5215    unary-expression:
5216      __extension__ cast-expression
5217      __alignof__ unary-expression
5218      __alignof__ ( type-id )
5219      __real__ cast-expression
5220      __imag__ cast-expression
5221      && identifier
5222
5223    ADDRESS_P is true iff the unary-expression is appearing as the
5224    operand of the `&' operator.   CAST_P is true if this expression is
5225    the target of a cast.
5226
5227    Returns a representation of the expression.  */
5228
5229 static tree
5230 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5231 {
5232   cp_token *token;
5233   enum tree_code unary_operator;
5234
5235   /* Peek at the next token.  */
5236   token = cp_lexer_peek_token (parser->lexer);
5237   /* Some keywords give away the kind of expression.  */
5238   if (token->type == CPP_KEYWORD)
5239     {
5240       enum rid keyword = token->keyword;
5241
5242       switch (keyword)
5243         {
5244         case RID_ALIGNOF:
5245         case RID_SIZEOF:
5246           {
5247             tree operand;
5248             enum tree_code op;
5249
5250             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5251             /* Consume the token.  */
5252             cp_lexer_consume_token (parser->lexer);
5253             /* Parse the operand.  */
5254             operand = cp_parser_sizeof_operand (parser, keyword);
5255
5256             if (TYPE_P (operand))
5257               return cxx_sizeof_or_alignof_type (operand, op, true);
5258             else
5259               return cxx_sizeof_or_alignof_expr (operand, op, true);
5260           }
5261
5262         case RID_NEW:
5263           return cp_parser_new_expression (parser);
5264
5265         case RID_DELETE:
5266           return cp_parser_delete_expression (parser);
5267
5268         case RID_EXTENSION:
5269           {
5270             /* The saved value of the PEDANTIC flag.  */
5271             int saved_pedantic;
5272             tree expr;
5273
5274             /* Save away the PEDANTIC flag.  */
5275             cp_parser_extension_opt (parser, &saved_pedantic);
5276             /* Parse the cast-expression.  */
5277             expr = cp_parser_simple_cast_expression (parser);
5278             /* Restore the PEDANTIC flag.  */
5279             pedantic = saved_pedantic;
5280
5281             return expr;
5282           }
5283
5284         case RID_REALPART:
5285         case RID_IMAGPART:
5286           {
5287             tree expression;
5288
5289             /* Consume the `__real__' or `__imag__' token.  */
5290             cp_lexer_consume_token (parser->lexer);
5291             /* Parse the cast-expression.  */
5292             expression = cp_parser_simple_cast_expression (parser);
5293             /* Create the complete representation.  */
5294             return build_x_unary_op ((keyword == RID_REALPART
5295                                       ? REALPART_EXPR : IMAGPART_EXPR),
5296                                      expression,
5297                                      tf_warning_or_error);
5298           }
5299           break;
5300
5301         default:
5302           break;
5303         }
5304     }
5305
5306   /* Look for the `:: new' and `:: delete', which also signal the
5307      beginning of a new-expression, or delete-expression,
5308      respectively.  If the next token is `::', then it might be one of
5309      these.  */
5310   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5311     {
5312       enum rid keyword;
5313
5314       /* See if the token after the `::' is one of the keywords in
5315          which we're interested.  */
5316       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5317       /* If it's `new', we have a new-expression.  */
5318       if (keyword == RID_NEW)
5319         return cp_parser_new_expression (parser);
5320       /* Similarly, for `delete'.  */
5321       else if (keyword == RID_DELETE)
5322         return cp_parser_delete_expression (parser);
5323     }
5324
5325   /* Look for a unary operator.  */
5326   unary_operator = cp_parser_unary_operator (token);
5327   /* The `++' and `--' operators can be handled similarly, even though
5328      they are not technically unary-operators in the grammar.  */
5329   if (unary_operator == ERROR_MARK)
5330     {
5331       if (token->type == CPP_PLUS_PLUS)
5332         unary_operator = PREINCREMENT_EXPR;
5333       else if (token->type == CPP_MINUS_MINUS)
5334         unary_operator = PREDECREMENT_EXPR;
5335       /* Handle the GNU address-of-label extension.  */
5336       else if (cp_parser_allow_gnu_extensions_p (parser)
5337                && token->type == CPP_AND_AND)
5338         {
5339           tree identifier;
5340           tree expression;
5341
5342           /* Consume the '&&' token.  */
5343           cp_lexer_consume_token (parser->lexer);
5344           /* Look for the identifier.  */
5345           identifier = cp_parser_identifier (parser);
5346           /* Create an expression representing the address.  */
5347           expression = finish_label_address_expr (identifier);
5348           if (cp_parser_non_integral_constant_expression (parser,
5349                                                 "the address of a label"))
5350             expression = error_mark_node;
5351           return expression;
5352         }
5353     }
5354   if (unary_operator != ERROR_MARK)
5355     {
5356       tree cast_expression;
5357       tree expression = error_mark_node;
5358       const char *non_constant_p = NULL;
5359
5360       /* Consume the operator token.  */
5361       token = cp_lexer_consume_token (parser->lexer);
5362       /* Parse the cast-expression.  */
5363       cast_expression
5364         = cp_parser_cast_expression (parser,
5365                                      unary_operator == ADDR_EXPR,
5366                                      /*cast_p=*/false);
5367       /* Now, build an appropriate representation.  */
5368       switch (unary_operator)
5369         {
5370         case INDIRECT_REF:
5371           non_constant_p = "`*'";
5372           expression = build_x_indirect_ref (cast_expression, "unary *",
5373                                              tf_warning_or_error);
5374           break;
5375
5376         case ADDR_EXPR:
5377           non_constant_p = "`&'";
5378           /* Fall through.  */
5379         case BIT_NOT_EXPR:
5380           expression = build_x_unary_op (unary_operator, cast_expression,
5381                                          tf_warning_or_error);
5382           break;
5383
5384         case PREINCREMENT_EXPR:
5385         case PREDECREMENT_EXPR:
5386           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5387                             ? "`++'" : "`--'");
5388           /* Fall through.  */
5389         case UNARY_PLUS_EXPR:
5390         case NEGATE_EXPR:
5391         case TRUTH_NOT_EXPR:
5392           expression = finish_unary_op_expr (unary_operator, cast_expression);
5393           break;
5394
5395         default:
5396           gcc_unreachable ();
5397         }
5398
5399       if (non_constant_p
5400           && cp_parser_non_integral_constant_expression (parser,
5401                                                          non_constant_p))
5402         expression = error_mark_node;
5403
5404       return expression;
5405     }
5406
5407   return cp_parser_postfix_expression (parser, address_p, cast_p,
5408                                        /*member_access_only_p=*/false);
5409 }
5410
5411 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5412    unary-operator, the corresponding tree code is returned.  */
5413
5414 static enum tree_code
5415 cp_parser_unary_operator (cp_token* token)
5416 {
5417   switch (token->type)
5418     {
5419     case CPP_MULT:
5420       return INDIRECT_REF;
5421
5422     case CPP_AND:
5423       return ADDR_EXPR;
5424
5425     case CPP_PLUS:
5426       return UNARY_PLUS_EXPR;
5427
5428     case CPP_MINUS:
5429       return NEGATE_EXPR;
5430
5431     case CPP_NOT:
5432       return TRUTH_NOT_EXPR;
5433
5434     case CPP_COMPL:
5435       return BIT_NOT_EXPR;
5436
5437     default:
5438       return ERROR_MARK;
5439     }
5440 }
5441
5442 /* Parse a new-expression.
5443
5444    new-expression:
5445      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5446      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5447
5448    Returns a representation of the expression.  */
5449
5450 static tree
5451 cp_parser_new_expression (cp_parser* parser)
5452 {
5453   bool global_scope_p;
5454   tree placement;
5455   tree type;
5456   tree initializer;
5457   tree nelts;
5458
5459   /* Look for the optional `::' operator.  */
5460   global_scope_p
5461     = (cp_parser_global_scope_opt (parser,
5462                                    /*current_scope_valid_p=*/false)
5463        != NULL_TREE);
5464   /* Look for the `new' operator.  */
5465   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5466   /* There's no easy way to tell a new-placement from the
5467      `( type-id )' construct.  */
5468   cp_parser_parse_tentatively (parser);
5469   /* Look for a new-placement.  */
5470   placement = cp_parser_new_placement (parser);
5471   /* If that didn't work out, there's no new-placement.  */
5472   if (!cp_parser_parse_definitely (parser))
5473     placement = NULL_TREE;
5474
5475   /* If the next token is a `(', then we have a parenthesized
5476      type-id.  */
5477   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5478     {
5479       /* Consume the `('.  */
5480       cp_lexer_consume_token (parser->lexer);
5481       /* Parse the type-id.  */
5482       type = cp_parser_type_id (parser);
5483       /* Look for the closing `)'.  */
5484       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5485       /* There should not be a direct-new-declarator in this production,
5486          but GCC used to allowed this, so we check and emit a sensible error
5487          message for this case.  */
5488       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5489         {
5490           error ("array bound forbidden after parenthesized type-id");
5491           inform ("try removing the parentheses around the type-id");
5492           cp_parser_direct_new_declarator (parser);
5493         }
5494       nelts = NULL_TREE;
5495     }
5496   /* Otherwise, there must be a new-type-id.  */
5497   else
5498     type = cp_parser_new_type_id (parser, &nelts);
5499
5500   /* If the next token is a `(', then we have a new-initializer.  */
5501   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5502     initializer = cp_parser_new_initializer (parser);
5503   else
5504     initializer = NULL_TREE;
5505
5506   /* A new-expression may not appear in an integral constant
5507      expression.  */
5508   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5509     return error_mark_node;
5510
5511   /* Create a representation of the new-expression.  */
5512   return build_new (placement, type, nelts, initializer, global_scope_p,
5513                     tf_warning_or_error);
5514 }
5515
5516 /* Parse a new-placement.
5517
5518    new-placement:
5519      ( expression-list )
5520
5521    Returns the same representation as for an expression-list.  */
5522
5523 static tree
5524 cp_parser_new_placement (cp_parser* parser)
5525 {
5526   tree expression_list;
5527
5528   /* Parse the expression-list.  */
5529   expression_list = (cp_parser_parenthesized_expression_list
5530                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5531                       /*non_constant_p=*/NULL));
5532
5533   return expression_list;
5534 }
5535
5536 /* Parse a new-type-id.
5537
5538    new-type-id:
5539      type-specifier-seq new-declarator [opt]
5540
5541    Returns the TYPE allocated.  If the new-type-id indicates an array
5542    type, *NELTS is set to the number of elements in the last array
5543    bound; the TYPE will not include the last array bound.  */
5544
5545 static tree
5546 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5547 {
5548   cp_decl_specifier_seq type_specifier_seq;
5549   cp_declarator *new_declarator;
5550   cp_declarator *declarator;
5551   cp_declarator *outer_declarator;
5552   const char *saved_message;
5553   tree type;
5554
5555   /* The type-specifier sequence must not contain type definitions.
5556      (It cannot contain declarations of new types either, but if they
5557      are not definitions we will catch that because they are not
5558      complete.)  */
5559   saved_message = parser->type_definition_forbidden_message;
5560   parser->type_definition_forbidden_message
5561     = "types may not be defined in a new-type-id";
5562   /* Parse the type-specifier-seq.  */
5563   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5564                                 &type_specifier_seq);
5565   /* Restore the old message.  */
5566   parser->type_definition_forbidden_message = saved_message;
5567   /* Parse the new-declarator.  */
5568   new_declarator = cp_parser_new_declarator_opt (parser);
5569
5570   /* Determine the number of elements in the last array dimension, if
5571      any.  */
5572   *nelts = NULL_TREE;
5573   /* Skip down to the last array dimension.  */
5574   declarator = new_declarator;
5575   outer_declarator = NULL;
5576   while (declarator && (declarator->kind == cdk_pointer
5577                         || declarator->kind == cdk_ptrmem))
5578     {
5579       outer_declarator = declarator;
5580       declarator = declarator->declarator;
5581     }
5582   while (declarator
5583          && declarator->kind == cdk_array
5584          && declarator->declarator
5585          && declarator->declarator->kind == cdk_array)
5586     {
5587       outer_declarator = declarator;
5588       declarator = declarator->declarator;
5589     }
5590
5591   if (declarator && declarator->kind == cdk_array)
5592     {
5593       *nelts = declarator->u.array.bounds;
5594       if (*nelts == error_mark_node)
5595         *nelts = integer_one_node;
5596
5597       if (outer_declarator)
5598         outer_declarator->declarator = declarator->declarator;
5599       else
5600         new_declarator = NULL;
5601     }
5602
5603   type = groktypename (&type_specifier_seq, new_declarator);
5604   return type;
5605 }
5606
5607 /* Parse an (optional) new-declarator.
5608
5609    new-declarator:
5610      ptr-operator new-declarator [opt]
5611      direct-new-declarator
5612
5613    Returns the declarator.  */
5614
5615 static cp_declarator *
5616 cp_parser_new_declarator_opt (cp_parser* parser)
5617 {
5618   enum tree_code code;
5619   tree type;
5620   cp_cv_quals cv_quals;
5621
5622   /* We don't know if there's a ptr-operator next, or not.  */
5623   cp_parser_parse_tentatively (parser);
5624   /* Look for a ptr-operator.  */
5625   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5626   /* If that worked, look for more new-declarators.  */
5627   if (cp_parser_parse_definitely (parser))
5628     {
5629       cp_declarator *declarator;
5630
5631       /* Parse another optional declarator.  */
5632       declarator = cp_parser_new_declarator_opt (parser);
5633
5634       return cp_parser_make_indirect_declarator
5635         (code, type, cv_quals, declarator);
5636     }
5637
5638   /* If the next token is a `[', there is a direct-new-declarator.  */
5639   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5640     return cp_parser_direct_new_declarator (parser);
5641
5642   return NULL;
5643 }
5644
5645 /* Parse a direct-new-declarator.
5646
5647    direct-new-declarator:
5648      [ expression ]
5649      direct-new-declarator [constant-expression]
5650
5651    */
5652
5653 static cp_declarator *
5654 cp_parser_direct_new_declarator (cp_parser* parser)
5655 {
5656   cp_declarator *declarator = NULL;
5657
5658   while (true)
5659     {
5660       tree expression;
5661
5662       /* Look for the opening `['.  */
5663       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5664       /* The first expression is not required to be constant.  */
5665       if (!declarator)
5666         {
5667           expression = cp_parser_expression (parser, /*cast_p=*/false);
5668           /* The standard requires that the expression have integral
5669              type.  DR 74 adds enumeration types.  We believe that the
5670              real intent is that these expressions be handled like the
5671              expression in a `switch' condition, which also allows
5672              classes with a single conversion to integral or
5673              enumeration type.  */
5674           if (!processing_template_decl)
5675             {
5676               expression
5677                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5678                                               expression,
5679                                               /*complain=*/true);
5680               if (!expression)
5681                 {
5682                   error ("expression in new-declarator must have integral "
5683                          "or enumeration type");
5684                   expression = error_mark_node;
5685                 }
5686             }
5687         }
5688       /* But all the other expressions must be.  */
5689       else
5690         expression
5691           = cp_parser_constant_expression (parser,
5692                                            /*allow_non_constant=*/false,
5693                                            NULL);
5694       /* Look for the closing `]'.  */
5695       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5696
5697       /* Add this bound to the declarator.  */
5698       declarator = make_array_declarator (declarator, expression);
5699
5700       /* If the next token is not a `[', then there are no more
5701          bounds.  */
5702       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5703         break;
5704     }
5705
5706   return declarator;
5707 }
5708
5709 /* Parse a new-initializer.
5710
5711    new-initializer:
5712      ( expression-list [opt] )
5713
5714    Returns a representation of the expression-list.  If there is no
5715    expression-list, VOID_ZERO_NODE is returned.  */
5716
5717 static tree
5718 cp_parser_new_initializer (cp_parser* parser)
5719 {
5720   tree expression_list;
5721
5722   expression_list = (cp_parser_parenthesized_expression_list
5723                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5724                       /*non_constant_p=*/NULL));
5725   if (!expression_list)
5726     expression_list = void_zero_node;
5727
5728   return expression_list;
5729 }
5730
5731 /* Parse a delete-expression.
5732
5733    delete-expression:
5734      :: [opt] delete cast-expression
5735      :: [opt] delete [ ] cast-expression
5736
5737    Returns a representation of the expression.  */
5738
5739 static tree
5740 cp_parser_delete_expression (cp_parser* parser)
5741 {
5742   bool global_scope_p;
5743   bool array_p;
5744   tree expression;
5745
5746   /* Look for the optional `::' operator.  */
5747   global_scope_p
5748     = (cp_parser_global_scope_opt (parser,
5749                                    /*current_scope_valid_p=*/false)
5750        != NULL_TREE);
5751   /* Look for the `delete' keyword.  */
5752   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5753   /* See if the array syntax is in use.  */
5754   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5755     {
5756       /* Consume the `[' token.  */
5757       cp_lexer_consume_token (parser->lexer);
5758       /* Look for the `]' token.  */
5759       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5760       /* Remember that this is the `[]' construct.  */
5761       array_p = true;
5762     }
5763   else
5764     array_p = false;
5765
5766   /* Parse the cast-expression.  */
5767   expression = cp_parser_simple_cast_expression (parser);
5768
5769   /* A delete-expression may not appear in an integral constant
5770      expression.  */
5771   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5772     return error_mark_node;
5773
5774   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5775 }
5776
5777 /* Parse a cast-expression.
5778
5779    cast-expression:
5780      unary-expression
5781      ( type-id ) cast-expression
5782
5783    ADDRESS_P is true iff the unary-expression is appearing as the
5784    operand of the `&' operator.   CAST_P is true if this expression is
5785    the target of a cast.
5786
5787    Returns a representation of the expression.  */
5788
5789 static tree
5790 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5791 {
5792   /* If it's a `(', then we might be looking at a cast.  */
5793   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5794     {
5795       tree type = NULL_TREE;
5796       tree expr = NULL_TREE;
5797       bool compound_literal_p;
5798       const char *saved_message;
5799
5800       /* There's no way to know yet whether or not this is a cast.
5801          For example, `(int (3))' is a unary-expression, while `(int)
5802          3' is a cast.  So, we resort to parsing tentatively.  */
5803       cp_parser_parse_tentatively (parser);
5804       /* Types may not be defined in a cast.  */
5805       saved_message = parser->type_definition_forbidden_message;
5806       parser->type_definition_forbidden_message
5807         = "types may not be defined in casts";
5808       /* Consume the `('.  */
5809       cp_lexer_consume_token (parser->lexer);
5810       /* A very tricky bit is that `(struct S) { 3 }' is a
5811          compound-literal (which we permit in C++ as an extension).
5812          But, that construct is not a cast-expression -- it is a
5813          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5814          is legal; if the compound-literal were a cast-expression,
5815          you'd need an extra set of parentheses.)  But, if we parse
5816          the type-id, and it happens to be a class-specifier, then we
5817          will commit to the parse at that point, because we cannot
5818          undo the action that is done when creating a new class.  So,
5819          then we cannot back up and do a postfix-expression.
5820
5821          Therefore, we scan ahead to the closing `)', and check to see
5822          if the token after the `)' is a `{'.  If so, we are not
5823          looking at a cast-expression.
5824
5825          Save tokens so that we can put them back.  */
5826       cp_lexer_save_tokens (parser->lexer);
5827       /* Skip tokens until the next token is a closing parenthesis.
5828          If we find the closing `)', and the next token is a `{', then
5829          we are looking at a compound-literal.  */
5830       compound_literal_p
5831         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5832                                                   /*consume_paren=*/true)
5833            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5834       /* Roll back the tokens we skipped.  */
5835       cp_lexer_rollback_tokens (parser->lexer);
5836       /* If we were looking at a compound-literal, simulate an error
5837          so that the call to cp_parser_parse_definitely below will
5838          fail.  */
5839       if (compound_literal_p)
5840         cp_parser_simulate_error (parser);
5841       else
5842         {
5843           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5844           parser->in_type_id_in_expr_p = true;
5845           /* Look for the type-id.  */
5846           type = cp_parser_type_id (parser);
5847           /* Look for the closing `)'.  */
5848           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5849           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5850         }
5851
5852       /* Restore the saved message.  */
5853       parser->type_definition_forbidden_message = saved_message;
5854
5855       /* If ok so far, parse the dependent expression. We cannot be
5856          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5857          ctor of T, but looks like a cast to function returning T
5858          without a dependent expression.  */
5859       if (!cp_parser_error_occurred (parser))
5860         expr = cp_parser_cast_expression (parser,
5861                                           /*address_p=*/false,
5862                                           /*cast_p=*/true);
5863
5864       if (cp_parser_parse_definitely (parser))
5865         {
5866           /* Warn about old-style casts, if so requested.  */
5867           if (warn_old_style_cast
5868               && !in_system_header
5869               && !VOID_TYPE_P (type)
5870               && current_lang_name != lang_name_c)
5871             warning (OPT_Wold_style_cast, "use of old-style cast");
5872
5873           /* Only type conversions to integral or enumeration types
5874              can be used in constant-expressions.  */
5875           if (!cast_valid_in_integral_constant_expression_p (type)
5876               && (cp_parser_non_integral_constant_expression
5877                   (parser,
5878                    "a cast to a type other than an integral or "
5879                    "enumeration type")))
5880             return error_mark_node;
5881
5882           /* Perform the cast.  */
5883           expr = build_c_cast (type, expr);
5884           return expr;
5885         }
5886     }
5887
5888   /* If we get here, then it's not a cast, so it must be a
5889      unary-expression.  */
5890   return cp_parser_unary_expression (parser, address_p, cast_p);
5891 }
5892
5893 /* Parse a binary expression of the general form:
5894
5895    pm-expression:
5896      cast-expression
5897      pm-expression .* cast-expression
5898      pm-expression ->* cast-expression
5899
5900    multiplicative-expression:
5901      pm-expression
5902      multiplicative-expression * pm-expression
5903      multiplicative-expression / pm-expression
5904      multiplicative-expression % pm-expression
5905
5906    additive-expression:
5907      multiplicative-expression
5908      additive-expression + multiplicative-expression
5909      additive-expression - multiplicative-expression
5910
5911    shift-expression:
5912      additive-expression
5913      shift-expression << additive-expression
5914      shift-expression >> additive-expression
5915
5916    relational-expression:
5917      shift-expression
5918      relational-expression < shift-expression
5919      relational-expression > shift-expression
5920      relational-expression <= shift-expression
5921      relational-expression >= shift-expression
5922
5923   GNU Extension:
5924
5925    relational-expression:
5926      relational-expression <? shift-expression
5927      relational-expression >? shift-expression
5928
5929    equality-expression:
5930      relational-expression
5931      equality-expression == relational-expression
5932      equality-expression != relational-expression
5933
5934    and-expression:
5935      equality-expression
5936      and-expression & equality-expression
5937
5938    exclusive-or-expression:
5939      and-expression
5940      exclusive-or-expression ^ and-expression
5941
5942    inclusive-or-expression:
5943      exclusive-or-expression
5944      inclusive-or-expression | exclusive-or-expression
5945
5946    logical-and-expression:
5947      inclusive-or-expression
5948      logical-and-expression && inclusive-or-expression
5949
5950    logical-or-expression:
5951      logical-and-expression
5952      logical-or-expression || logical-and-expression
5953
5954    All these are implemented with a single function like:
5955
5956    binary-expression:
5957      simple-cast-expression
5958      binary-expression <token> binary-expression
5959
5960    CAST_P is true if this expression is the target of a cast.
5961
5962    The binops_by_token map is used to get the tree codes for each <token> type.
5963    binary-expressions are associated according to a precedence table.  */
5964
5965 #define TOKEN_PRECEDENCE(token)                              \
5966 (((token->type == CPP_GREATER                                \
5967    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5968   && !parser->greater_than_is_operator_p)                    \
5969  ? PREC_NOT_OPERATOR                                         \
5970  : binops_by_token[token->type].prec)
5971
5972 static tree
5973 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5974 {
5975   cp_parser_expression_stack stack;
5976   cp_parser_expression_stack_entry *sp = &stack[0];
5977   tree lhs, rhs;
5978   cp_token *token;
5979   enum tree_code tree_type, lhs_type, rhs_type;
5980   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5981   bool overloaded_p;
5982
5983   /* Parse the first expression.  */
5984   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5985   lhs_type = ERROR_MARK;
5986
5987   for (;;)
5988     {
5989       /* Get an operator token.  */
5990       token = cp_lexer_peek_token (parser->lexer);
5991
5992       if (warn_cxx0x_compat
5993           && token->type == CPP_RSHIFT
5994           && !parser->greater_than_is_operator_p)
5995         {
5996           warning (OPT_Wc__0x_compat, 
5997                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
5998                    &token->location);
5999           warning (OPT_Wc__0x_compat, 
6000                    "suggest parentheses around %<>>%> expression");
6001         }
6002
6003       new_prec = TOKEN_PRECEDENCE (token);
6004
6005       /* Popping an entry off the stack means we completed a subexpression:
6006          - either we found a token which is not an operator (`>' where it is not
6007            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6008            will happen repeatedly;
6009          - or, we found an operator which has lower priority.  This is the case
6010            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6011            parsing `3 * 4'.  */
6012       if (new_prec <= prec)
6013         {
6014           if (sp == stack)
6015             break;
6016           else
6017             goto pop;
6018         }
6019
6020      get_rhs:
6021       tree_type = binops_by_token[token->type].tree_type;
6022
6023       /* We used the operator token.  */
6024       cp_lexer_consume_token (parser->lexer);
6025
6026       /* Extract another operand.  It may be the RHS of this expression
6027          or the LHS of a new, higher priority expression.  */
6028       rhs = cp_parser_simple_cast_expression (parser);
6029       rhs_type = ERROR_MARK;
6030
6031       /* Get another operator token.  Look up its precedence to avoid
6032          building a useless (immediately popped) stack entry for common
6033          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6034       token = cp_lexer_peek_token (parser->lexer);
6035       lookahead_prec = TOKEN_PRECEDENCE (token);
6036       if (lookahead_prec > new_prec)
6037         {
6038           /* ... and prepare to parse the RHS of the new, higher priority
6039              expression.  Since precedence levels on the stack are
6040              monotonically increasing, we do not have to care about
6041              stack overflows.  */
6042           sp->prec = prec;
6043           sp->tree_type = tree_type;
6044           sp->lhs = lhs;
6045           sp->lhs_type = lhs_type;
6046           sp++;
6047           lhs = rhs;
6048           lhs_type = rhs_type;
6049           prec = new_prec;
6050           new_prec = lookahead_prec;
6051           goto get_rhs;
6052
6053          pop:
6054           /* If the stack is not empty, we have parsed into LHS the right side
6055              (`4' in the example above) of an expression we had suspended.
6056              We can use the information on the stack to recover the LHS (`3')
6057              from the stack together with the tree code (`MULT_EXPR'), and
6058              the precedence of the higher level subexpression
6059              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6060              which will be used to actually build the additive expression.  */
6061           --sp;
6062           prec = sp->prec;
6063           tree_type = sp->tree_type;
6064           rhs = lhs;
6065           rhs_type = lhs_type;
6066           lhs = sp->lhs;
6067           lhs_type = sp->lhs_type;
6068         }
6069
6070       overloaded_p = false;
6071       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6072                                &overloaded_p, tf_warning_or_error);
6073       lhs_type = tree_type;
6074
6075       /* If the binary operator required the use of an overloaded operator,
6076          then this expression cannot be an integral constant-expression.
6077          An overloaded operator can be used even if both operands are
6078          otherwise permissible in an integral constant-expression if at
6079          least one of the operands is of enumeration type.  */
6080
6081       if (overloaded_p
6082           && (cp_parser_non_integral_constant_expression
6083               (parser, "calls to overloaded operators")))
6084         return error_mark_node;
6085     }
6086
6087   return lhs;
6088 }
6089
6090
6091 /* Parse the `? expression : assignment-expression' part of a
6092    conditional-expression.  The LOGICAL_OR_EXPR is the
6093    logical-or-expression that started the conditional-expression.
6094    Returns a representation of the entire conditional-expression.
6095
6096    This routine is used by cp_parser_assignment_expression.
6097
6098      ? expression : assignment-expression
6099
6100    GNU Extensions:
6101
6102      ? : assignment-expression */
6103
6104 static tree
6105 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6106 {
6107   tree expr;
6108   tree assignment_expr;
6109
6110   /* Consume the `?' token.  */
6111   cp_lexer_consume_token (parser->lexer);
6112   if (cp_parser_allow_gnu_extensions_p (parser)
6113       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6114     /* Implicit true clause.  */
6115     expr = NULL_TREE;
6116   else
6117     /* Parse the expression.  */
6118     expr = cp_parser_expression (parser, /*cast_p=*/false);
6119
6120   /* The next token should be a `:'.  */
6121   cp_parser_require (parser, CPP_COLON, "%<:%>");
6122   /* Parse the assignment-expression.  */
6123   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6124
6125   /* Build the conditional-expression.  */
6126   return build_x_conditional_expr (logical_or_expr,
6127                                    expr,
6128                                    assignment_expr,
6129                                    tf_warning_or_error);
6130 }
6131
6132 /* Parse an assignment-expression.
6133
6134    assignment-expression:
6135      conditional-expression
6136      logical-or-expression assignment-operator assignment_expression
6137      throw-expression
6138
6139    CAST_P is true if this expression is the target of a cast.
6140
6141    Returns a representation for the expression.  */
6142
6143 static tree
6144 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6145 {
6146   tree expr;
6147
6148   /* If the next token is the `throw' keyword, then we're looking at
6149      a throw-expression.  */
6150   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6151     expr = cp_parser_throw_expression (parser);
6152   /* Otherwise, it must be that we are looking at a
6153      logical-or-expression.  */
6154   else
6155     {
6156       /* Parse the binary expressions (logical-or-expression).  */
6157       expr = cp_parser_binary_expression (parser, cast_p);
6158       /* If the next token is a `?' then we're actually looking at a
6159          conditional-expression.  */
6160       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6161         return cp_parser_question_colon_clause (parser, expr);
6162       else
6163         {
6164           enum tree_code assignment_operator;
6165
6166           /* If it's an assignment-operator, we're using the second
6167              production.  */
6168           assignment_operator
6169             = cp_parser_assignment_operator_opt (parser);
6170           if (assignment_operator != ERROR_MARK)
6171             {
6172               tree rhs;
6173
6174               /* Parse the right-hand side of the assignment.  */
6175               rhs = cp_parser_assignment_expression (parser, cast_p);
6176               /* An assignment may not appear in a
6177                  constant-expression.  */
6178               if (cp_parser_non_integral_constant_expression (parser,
6179                                                               "an assignment"))
6180                 return error_mark_node;
6181               /* Build the assignment expression.  */
6182               expr = build_x_modify_expr (expr,
6183                                           assignment_operator,
6184                                           rhs,
6185                                           tf_warning_or_error);
6186             }
6187         }
6188     }
6189
6190   return expr;
6191 }
6192
6193 /* Parse an (optional) assignment-operator.
6194
6195    assignment-operator: one of
6196      = *= /= %= += -= >>= <<= &= ^= |=
6197
6198    GNU Extension:
6199
6200    assignment-operator: one of
6201      <?= >?=
6202
6203    If the next token is an assignment operator, the corresponding tree
6204    code is returned, and the token is consumed.  For example, for
6205    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6206    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6207    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6208    operator, ERROR_MARK is returned.  */
6209
6210 static enum tree_code
6211 cp_parser_assignment_operator_opt (cp_parser* parser)
6212 {
6213   enum tree_code op;
6214   cp_token *token;
6215
6216   /* Peek at the next toen.  */
6217   token = cp_lexer_peek_token (parser->lexer);
6218
6219   switch (token->type)
6220     {
6221     case CPP_EQ:
6222       op = NOP_EXPR;
6223       break;
6224
6225     case CPP_MULT_EQ:
6226       op = MULT_EXPR;
6227       break;
6228
6229     case CPP_DIV_EQ:
6230       op = TRUNC_DIV_EXPR;
6231       break;
6232
6233     case CPP_MOD_EQ:
6234       op = TRUNC_MOD_EXPR;
6235       break;
6236
6237     case CPP_PLUS_EQ:
6238       op = PLUS_EXPR;
6239       break;
6240
6241     case CPP_MINUS_EQ:
6242       op = MINUS_EXPR;
6243       break;
6244
6245     case CPP_RSHIFT_EQ:
6246       op = RSHIFT_EXPR;
6247       break;
6248
6249     case CPP_LSHIFT_EQ:
6250       op = LSHIFT_EXPR;
6251       break;
6252
6253     case CPP_AND_EQ:
6254       op = BIT_AND_EXPR;
6255       break;
6256
6257     case CPP_XOR_EQ:
6258       op = BIT_XOR_EXPR;
6259       break;
6260
6261     case CPP_OR_EQ:
6262       op = BIT_IOR_EXPR;
6263       break;
6264
6265     default:
6266       /* Nothing else is an assignment operator.  */
6267       op = ERROR_MARK;
6268     }
6269
6270   /* If it was an assignment operator, consume it.  */
6271   if (op != ERROR_MARK)
6272     cp_lexer_consume_token (parser->lexer);
6273
6274   return op;
6275 }
6276
6277 /* Parse an expression.
6278
6279    expression:
6280      assignment-expression
6281      expression , assignment-expression
6282
6283    CAST_P is true if this expression is the target of a cast.
6284
6285    Returns a representation of the expression.  */
6286
6287 static tree
6288 cp_parser_expression (cp_parser* parser, bool cast_p)
6289 {
6290   tree expression = NULL_TREE;
6291
6292   while (true)
6293     {
6294       tree assignment_expression;
6295
6296       /* Parse the next assignment-expression.  */
6297       assignment_expression
6298         = cp_parser_assignment_expression (parser, cast_p);
6299       /* If this is the first assignment-expression, we can just
6300          save it away.  */
6301       if (!expression)
6302         expression = assignment_expression;
6303       else
6304         expression = build_x_compound_expr (expression,
6305                                             assignment_expression,
6306                                             tf_warning_or_error);
6307       /* If the next token is not a comma, then we are done with the
6308          expression.  */
6309       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6310         break;
6311       /* Consume the `,'.  */
6312       cp_lexer_consume_token (parser->lexer);
6313       /* A comma operator cannot appear in a constant-expression.  */
6314       if (cp_parser_non_integral_constant_expression (parser,
6315                                                       "a comma operator"))
6316         expression = error_mark_node;
6317     }
6318
6319   return expression;
6320 }
6321
6322 /* Parse a constant-expression.
6323
6324    constant-expression:
6325      conditional-expression
6326
6327   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6328   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6329   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6330   is false, NON_CONSTANT_P should be NULL.  */
6331
6332 static tree
6333 cp_parser_constant_expression (cp_parser* parser,
6334                                bool allow_non_constant_p,
6335                                bool *non_constant_p)
6336 {
6337   bool saved_integral_constant_expression_p;
6338   bool saved_allow_non_integral_constant_expression_p;
6339   bool saved_non_integral_constant_expression_p;
6340   tree expression;
6341
6342   /* It might seem that we could simply parse the
6343      conditional-expression, and then check to see if it were
6344      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6345      one that the compiler can figure out is constant, possibly after
6346      doing some simplifications or optimizations.  The standard has a
6347      precise definition of constant-expression, and we must honor
6348      that, even though it is somewhat more restrictive.
6349
6350      For example:
6351
6352        int i[(2, 3)];
6353
6354      is not a legal declaration, because `(2, 3)' is not a
6355      constant-expression.  The `,' operator is forbidden in a
6356      constant-expression.  However, GCC's constant-folding machinery
6357      will fold this operation to an INTEGER_CST for `3'.  */
6358
6359   /* Save the old settings.  */
6360   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6361   saved_allow_non_integral_constant_expression_p
6362     = parser->allow_non_integral_constant_expression_p;
6363   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6364   /* We are now parsing a constant-expression.  */
6365   parser->integral_constant_expression_p = true;
6366   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6367   parser->non_integral_constant_expression_p = false;
6368   /* Although the grammar says "conditional-expression", we parse an
6369      "assignment-expression", which also permits "throw-expression"
6370      and the use of assignment operators.  In the case that
6371      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6372      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6373      actually essential that we look for an assignment-expression.
6374      For example, cp_parser_initializer_clauses uses this function to
6375      determine whether a particular assignment-expression is in fact
6376      constant.  */
6377   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6378   /* Restore the old settings.  */
6379   parser->integral_constant_expression_p
6380     = saved_integral_constant_expression_p;
6381   parser->allow_non_integral_constant_expression_p
6382     = saved_allow_non_integral_constant_expression_p;
6383   if (allow_non_constant_p)
6384     *non_constant_p = parser->non_integral_constant_expression_p;
6385   else if (parser->non_integral_constant_expression_p)
6386     expression = error_mark_node;
6387   parser->non_integral_constant_expression_p
6388     = saved_non_integral_constant_expression_p;
6389
6390   return expression;
6391 }
6392
6393 /* Parse __builtin_offsetof.
6394
6395    offsetof-expression:
6396      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6397
6398    offsetof-member-designator:
6399      id-expression
6400      | offsetof-member-designator "." id-expression
6401      | offsetof-member-designator "[" expression "]"  */
6402
6403 static tree
6404 cp_parser_builtin_offsetof (cp_parser *parser)
6405 {
6406   int save_ice_p, save_non_ice_p;
6407   tree type, expr;
6408   cp_id_kind dummy;
6409
6410   /* We're about to accept non-integral-constant things, but will
6411      definitely yield an integral constant expression.  Save and
6412      restore these values around our local parsing.  */
6413   save_ice_p = parser->integral_constant_expression_p;
6414   save_non_ice_p = parser->non_integral_constant_expression_p;
6415
6416   /* Consume the "__builtin_offsetof" token.  */
6417   cp_lexer_consume_token (parser->lexer);
6418   /* Consume the opening `('.  */
6419   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6420   /* Parse the type-id.  */
6421   type = cp_parser_type_id (parser);
6422   /* Look for the `,'.  */
6423   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6424
6425   /* Build the (type *)null that begins the traditional offsetof macro.  */
6426   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6427                             tf_warning_or_error);
6428
6429   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6430   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6431                                                  true, &dummy);
6432   while (true)
6433     {
6434       cp_token *token = cp_lexer_peek_token (parser->lexer);
6435       switch (token->type)
6436         {
6437         case CPP_OPEN_SQUARE:
6438           /* offsetof-member-designator "[" expression "]" */
6439           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6440           break;
6441
6442         case CPP_DOT:
6443           /* offsetof-member-designator "." identifier */
6444           cp_lexer_consume_token (parser->lexer);
6445           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6446                                                          true, &dummy);
6447           break;
6448
6449         case CPP_CLOSE_PAREN:
6450           /* Consume the ")" token.  */
6451           cp_lexer_consume_token (parser->lexer);
6452           goto success;
6453
6454         default:
6455           /* Error.  We know the following require will fail, but
6456              that gives the proper error message.  */
6457           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6458           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6459           expr = error_mark_node;
6460           goto failure;
6461         }
6462     }
6463
6464  success:
6465   /* If we're processing a template, we can't finish the semantics yet.
6466      Otherwise we can fold the entire expression now.  */
6467   if (processing_template_decl)
6468     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6469   else
6470     expr = finish_offsetof (expr);
6471
6472  failure:
6473   parser->integral_constant_expression_p = save_ice_p;
6474   parser->non_integral_constant_expression_p = save_non_ice_p;
6475
6476   return expr;
6477 }
6478
6479 /* Parse a trait expression.  */
6480
6481 static tree
6482 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6483 {
6484   cp_trait_kind kind;
6485   tree type1, type2 = NULL_TREE;
6486   bool binary = false;
6487   cp_decl_specifier_seq decl_specs;
6488
6489   switch (keyword)
6490     {
6491     case RID_HAS_NOTHROW_ASSIGN:
6492       kind = CPTK_HAS_NOTHROW_ASSIGN;
6493       break;
6494     case RID_HAS_NOTHROW_CONSTRUCTOR:
6495       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6496       break;
6497     case RID_HAS_NOTHROW_COPY:
6498       kind = CPTK_HAS_NOTHROW_COPY;
6499       break;
6500     case RID_HAS_TRIVIAL_ASSIGN:
6501       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6502       break;
6503     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6504       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6505       break;
6506     case RID_HAS_TRIVIAL_COPY:
6507       kind = CPTK_HAS_TRIVIAL_COPY;
6508       break;
6509     case RID_HAS_TRIVIAL_DESTRUCTOR:
6510       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6511       break;
6512     case RID_HAS_VIRTUAL_DESTRUCTOR:
6513       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6514       break;
6515     case RID_IS_ABSTRACT:
6516       kind = CPTK_IS_ABSTRACT;
6517       break;
6518     case RID_IS_BASE_OF:
6519       kind = CPTK_IS_BASE_OF;
6520       binary = true;
6521       break;
6522     case RID_IS_CLASS:
6523       kind = CPTK_IS_CLASS;
6524       break;
6525     case RID_IS_CONVERTIBLE_TO:
6526       kind = CPTK_IS_CONVERTIBLE_TO;
6527       binary = true;
6528       break;
6529     case RID_IS_EMPTY:
6530       kind = CPTK_IS_EMPTY;
6531       break;
6532     case RID_IS_ENUM:
6533       kind = CPTK_IS_ENUM;
6534       break;
6535     case RID_IS_POD:
6536       kind = CPTK_IS_POD;
6537       break;
6538     case RID_IS_POLYMORPHIC:
6539       kind = CPTK_IS_POLYMORPHIC;
6540       break;
6541     case RID_IS_UNION:
6542       kind = CPTK_IS_UNION;
6543       break;
6544     default:
6545       gcc_unreachable ();
6546     }
6547
6548   /* Consume the token.  */
6549   cp_lexer_consume_token (parser->lexer);
6550
6551   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6552
6553   type1 = cp_parser_type_id (parser);
6554
6555   if (type1 == error_mark_node)
6556     return error_mark_node;
6557
6558   /* Build a trivial decl-specifier-seq.  */
6559   clear_decl_specs (&decl_specs);
6560   decl_specs.type = type1;
6561
6562   /* Call grokdeclarator to figure out what type this is.  */
6563   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6564                           /*initialized=*/0, /*attrlist=*/NULL);
6565
6566   if (binary)
6567     {
6568       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6569  
6570       type2 = cp_parser_type_id (parser);
6571
6572       if (type2 == error_mark_node)
6573         return error_mark_node;
6574
6575       /* Build a trivial decl-specifier-seq.  */
6576       clear_decl_specs (&decl_specs);
6577       decl_specs.type = type2;
6578
6579       /* Call grokdeclarator to figure out what type this is.  */
6580       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6581                               /*initialized=*/0, /*attrlist=*/NULL);
6582     }
6583
6584   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6585
6586   /* Complete the trait expression, which may mean either processing
6587      the trait expr now or saving it for template instantiation.  */
6588   return finish_trait_expr (kind, type1, type2);
6589 }
6590
6591 /* Statements [gram.stmt.stmt]  */
6592
6593 /* Parse a statement.
6594
6595    statement:
6596      labeled-statement
6597      expression-statement
6598      compound-statement
6599      selection-statement
6600      iteration-statement
6601      jump-statement
6602      declaration-statement
6603      try-block
6604
6605   IN_COMPOUND is true when the statement is nested inside a
6606   cp_parser_compound_statement; this matters for certain pragmas.
6607
6608   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6609   is a (possibly labeled) if statement which is not enclosed in braces
6610   and has an else clause.  This is used to implement -Wparentheses.  */
6611
6612 static void
6613 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6614                      bool in_compound, bool *if_p)
6615 {
6616   tree statement;
6617   cp_token *token;
6618   location_t statement_location;
6619
6620  restart:
6621   if (if_p != NULL)
6622     *if_p = false;
6623   /* There is no statement yet.  */
6624   statement = NULL_TREE;
6625   /* Peek at the next token.  */
6626   token = cp_lexer_peek_token (parser->lexer);
6627   /* Remember the location of the first token in the statement.  */
6628   statement_location = token->location;
6629   /* If this is a keyword, then that will often determine what kind of
6630      statement we have.  */
6631   if (token->type == CPP_KEYWORD)
6632     {
6633       enum rid keyword = token->keyword;
6634
6635       switch (keyword)
6636         {
6637         case RID_CASE:
6638         case RID_DEFAULT:
6639           /* Looks like a labeled-statement with a case label.
6640              Parse the label, and then use tail recursion to parse
6641              the statement.  */
6642           cp_parser_label_for_labeled_statement (parser);
6643           goto restart;
6644
6645         case RID_IF:
6646         case RID_SWITCH:
6647           statement = cp_parser_selection_statement (parser, if_p);
6648           break;
6649
6650         case RID_WHILE:
6651         case RID_DO:
6652         case RID_FOR:
6653           statement = cp_parser_iteration_statement (parser);
6654           break;
6655
6656         case RID_BREAK:
6657         case RID_CONTINUE:
6658         case RID_RETURN:
6659         case RID_GOTO:
6660           statement = cp_parser_jump_statement (parser);
6661           break;
6662
6663           /* Objective-C++ exception-handling constructs.  */
6664         case RID_AT_TRY:
6665         case RID_AT_CATCH:
6666         case RID_AT_FINALLY:
6667         case RID_AT_SYNCHRONIZED:
6668         case RID_AT_THROW:
6669           statement = cp_parser_objc_statement (parser);
6670           break;
6671
6672         case RID_TRY:
6673           statement = cp_parser_try_block (parser);
6674           break;
6675
6676         case RID_NAMESPACE:
6677           /* This must be a namespace alias definition.  */
6678           cp_parser_declaration_statement (parser);
6679           return;
6680           
6681         default:
6682           /* It might be a keyword like `int' that can start a
6683              declaration-statement.  */
6684           break;
6685         }
6686     }
6687   else if (token->type == CPP_NAME)
6688     {
6689       /* If the next token is a `:', then we are looking at a
6690          labeled-statement.  */
6691       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6692       if (token->type == CPP_COLON)
6693         {
6694           /* Looks like a labeled-statement with an ordinary label.
6695              Parse the label, and then use tail recursion to parse
6696              the statement.  */
6697           cp_parser_label_for_labeled_statement (parser);
6698           goto restart;
6699         }
6700     }
6701   /* Anything that starts with a `{' must be a compound-statement.  */
6702   else if (token->type == CPP_OPEN_BRACE)
6703     statement = cp_parser_compound_statement (parser, NULL, false);
6704   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6705      a statement all its own.  */
6706   else if (token->type == CPP_PRAGMA)
6707     {
6708       /* Only certain OpenMP pragmas are attached to statements, and thus
6709          are considered statements themselves.  All others are not.  In
6710          the context of a compound, accept the pragma as a "statement" and
6711          return so that we can check for a close brace.  Otherwise we
6712          require a real statement and must go back and read one.  */
6713       if (in_compound)
6714         cp_parser_pragma (parser, pragma_compound);
6715       else if (!cp_parser_pragma (parser, pragma_stmt))
6716         goto restart;
6717       return;
6718     }
6719   else if (token->type == CPP_EOF)
6720     {
6721       cp_parser_error (parser, "expected statement");
6722       return;
6723     }
6724
6725   /* Everything else must be a declaration-statement or an
6726      expression-statement.  Try for the declaration-statement
6727      first, unless we are looking at a `;', in which case we know that
6728      we have an expression-statement.  */
6729   if (!statement)
6730     {
6731       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6732         {
6733           cp_parser_parse_tentatively (parser);
6734           /* Try to parse the declaration-statement.  */
6735           cp_parser_declaration_statement (parser);
6736           /* If that worked, we're done.  */
6737           if (cp_parser_parse_definitely (parser))
6738             return;
6739         }
6740       /* Look for an expression-statement instead.  */
6741       statement = cp_parser_expression_statement (parser, in_statement_expr);
6742     }
6743
6744   /* Set the line number for the statement.  */
6745   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6746     SET_EXPR_LOCATION (statement, statement_location);
6747 }
6748
6749 /* Parse the label for a labeled-statement, i.e.
6750
6751    identifier :
6752    case constant-expression :
6753    default :
6754
6755    GNU Extension:
6756    case constant-expression ... constant-expression : statement
6757
6758    When a label is parsed without errors, the label is added to the
6759    parse tree by the finish_* functions, so this function doesn't
6760    have to return the label.  */
6761
6762 static void
6763 cp_parser_label_for_labeled_statement (cp_parser* parser)
6764 {
6765   cp_token *token;
6766
6767   /* The next token should be an identifier.  */
6768   token = cp_lexer_peek_token (parser->lexer);
6769   if (token->type != CPP_NAME
6770       && token->type != CPP_KEYWORD)
6771     {
6772       cp_parser_error (parser, "expected labeled-statement");
6773       return;
6774     }
6775
6776   switch (token->keyword)
6777     {
6778     case RID_CASE:
6779       {
6780         tree expr, expr_hi;
6781         cp_token *ellipsis;
6782
6783         /* Consume the `case' token.  */
6784         cp_lexer_consume_token (parser->lexer);
6785         /* Parse the constant-expression.  */
6786         expr = cp_parser_constant_expression (parser,
6787                                               /*allow_non_constant_p=*/false,
6788                                               NULL);
6789
6790         ellipsis = cp_lexer_peek_token (parser->lexer);
6791         if (ellipsis->type == CPP_ELLIPSIS)
6792           {
6793             /* Consume the `...' token.  */
6794             cp_lexer_consume_token (parser->lexer);
6795             expr_hi =
6796               cp_parser_constant_expression (parser,
6797                                              /*allow_non_constant_p=*/false,
6798                                              NULL);
6799             /* We don't need to emit warnings here, as the common code
6800                will do this for us.  */
6801           }
6802         else
6803           expr_hi = NULL_TREE;
6804
6805         if (parser->in_switch_statement_p)
6806           finish_case_label (expr, expr_hi);
6807         else
6808           error ("case label %qE not within a switch statement", expr);
6809       }
6810       break;
6811
6812     case RID_DEFAULT:
6813       /* Consume the `default' token.  */
6814       cp_lexer_consume_token (parser->lexer);
6815
6816       if (parser->in_switch_statement_p)
6817         finish_case_label (NULL_TREE, NULL_TREE);
6818       else
6819         error ("case label not within a switch statement");
6820       break;
6821
6822     default:
6823       /* Anything else must be an ordinary label.  */
6824       finish_label_stmt (cp_parser_identifier (parser));
6825       break;
6826     }
6827
6828   /* Require the `:' token.  */
6829   cp_parser_require (parser, CPP_COLON, "%<:%>");
6830 }
6831
6832 /* Parse an expression-statement.
6833
6834    expression-statement:
6835      expression [opt] ;
6836
6837    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6838    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6839    indicates whether this expression-statement is part of an
6840    expression statement.  */
6841
6842 static tree
6843 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6844 {
6845   tree statement = NULL_TREE;
6846
6847   /* If the next token is a ';', then there is no expression
6848      statement.  */
6849   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6850     statement = cp_parser_expression (parser, /*cast_p=*/false);
6851
6852   /* Consume the final `;'.  */
6853   cp_parser_consume_semicolon_at_end_of_statement (parser);
6854
6855   if (in_statement_expr
6856       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6857     /* This is the final expression statement of a statement
6858        expression.  */
6859     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6860   else if (statement)
6861     statement = finish_expr_stmt (statement);
6862   else
6863     finish_stmt ();
6864
6865   return statement;
6866 }
6867
6868 /* Parse a compound-statement.
6869
6870    compound-statement:
6871      { statement-seq [opt] }
6872
6873    GNU extension:
6874
6875    compound-statement:
6876      { label-declaration-seq [opt] statement-seq [opt] }
6877
6878    label-declaration-seq:
6879      label-declaration
6880      label-declaration-seq label-declaration
6881
6882    Returns a tree representing the statement.  */
6883
6884 static tree
6885 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6886                               bool in_try)
6887 {
6888   tree compound_stmt;
6889
6890   /* Consume the `{'.  */
6891   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
6892     return error_mark_node;
6893   /* Begin the compound-statement.  */
6894   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6895   /* If the next keyword is `__label__' we have a label declaration.  */
6896   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6897     cp_parser_label_declaration (parser);
6898   /* Parse an (optional) statement-seq.  */
6899   cp_parser_statement_seq_opt (parser, in_statement_expr);
6900   /* Finish the compound-statement.  */
6901   finish_compound_stmt (compound_stmt);
6902   /* Consume the `}'.  */
6903   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
6904
6905   return compound_stmt;
6906 }
6907
6908 /* Parse an (optional) statement-seq.
6909
6910    statement-seq:
6911      statement
6912      statement-seq [opt] statement  */
6913
6914 static void
6915 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6916 {
6917   /* Scan statements until there aren't any more.  */
6918   while (true)
6919     {
6920       cp_token *token = cp_lexer_peek_token (parser->lexer);
6921
6922       /* If we're looking at a `}', then we've run out of statements.  */
6923       if (token->type == CPP_CLOSE_BRACE
6924           || token->type == CPP_EOF
6925           || token->type == CPP_PRAGMA_EOL)
6926         break;
6927       
6928       /* If we are in a compound statement and find 'else' then
6929          something went wrong.  */
6930       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6931         {
6932           if (parser->in_statement & IN_IF_STMT) 
6933             break;
6934           else
6935             {
6936               token = cp_lexer_consume_token (parser->lexer);
6937               error ("%<else%> without a previous %<if%>");
6938             }
6939         }
6940
6941       /* Parse the statement.  */
6942       cp_parser_statement (parser, in_statement_expr, true, NULL);
6943     }
6944 }
6945
6946 /* Parse a selection-statement.
6947
6948    selection-statement:
6949      if ( condition ) statement
6950      if ( condition ) statement else statement
6951      switch ( condition ) statement
6952
6953    Returns the new IF_STMT or SWITCH_STMT.
6954
6955    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6956    is a (possibly labeled) if statement which is not enclosed in
6957    braces and has an else clause.  This is used to implement
6958    -Wparentheses.  */
6959
6960 static tree
6961 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6962 {
6963   cp_token *token;
6964   enum rid keyword;
6965
6966   if (if_p != NULL)
6967     *if_p = false;
6968
6969   /* Peek at the next token.  */
6970   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6971
6972   /* See what kind of keyword it is.  */
6973   keyword = token->keyword;
6974   switch (keyword)
6975     {
6976     case RID_IF:
6977     case RID_SWITCH:
6978       {
6979         tree statement;
6980         tree condition;
6981
6982         /* Look for the `('.  */
6983         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
6984           {
6985             cp_parser_skip_to_end_of_statement (parser);
6986             return error_mark_node;
6987           }
6988
6989         /* Begin the selection-statement.  */
6990         if (keyword == RID_IF)
6991           statement = begin_if_stmt ();
6992         else
6993           statement = begin_switch_stmt ();
6994
6995         /* Parse the condition.  */
6996         condition = cp_parser_condition (parser);
6997         /* Look for the `)'.  */
6998         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
6999           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7000                                                  /*consume_paren=*/true);
7001
7002         if (keyword == RID_IF)
7003           {
7004             bool nested_if;
7005             unsigned char in_statement;
7006
7007             /* Add the condition.  */
7008             finish_if_stmt_cond (condition, statement);
7009
7010             /* Parse the then-clause.  */
7011             in_statement = parser->in_statement;
7012             parser->in_statement |= IN_IF_STMT;
7013             cp_parser_implicitly_scoped_statement (parser, &nested_if);
7014             parser->in_statement = in_statement;
7015
7016             finish_then_clause (statement);
7017
7018             /* If the next token is `else', parse the else-clause.  */
7019             if (cp_lexer_next_token_is_keyword (parser->lexer,
7020                                                 RID_ELSE))
7021               {
7022                 /* Consume the `else' keyword.  */
7023                 cp_lexer_consume_token (parser->lexer);
7024                 begin_else_clause (statement);
7025                 /* Parse the else-clause.  */
7026                 cp_parser_implicitly_scoped_statement (parser, NULL);
7027                 finish_else_clause (statement);
7028
7029                 /* If we are currently parsing a then-clause, then
7030                    IF_P will not be NULL.  We set it to true to
7031                    indicate that this if statement has an else clause.
7032                    This may trigger the Wparentheses warning below
7033                    when we get back up to the parent if statement.  */
7034                 if (if_p != NULL)
7035                   *if_p = true;
7036               }
7037             else
7038               {
7039                 /* This if statement does not have an else clause.  If
7040                    NESTED_IF is true, then the then-clause is an if
7041                    statement which does have an else clause.  We warn
7042                    about the potential ambiguity.  */
7043                 if (nested_if)
7044                   warning (OPT_Wparentheses,
7045                            ("%Hsuggest explicit braces "
7046                             "to avoid ambiguous %<else%>"),
7047                            EXPR_LOCUS (statement));
7048               }
7049
7050             /* Now we're all done with the if-statement.  */
7051             finish_if_stmt (statement);
7052           }
7053         else
7054           {
7055             bool in_switch_statement_p;
7056             unsigned char in_statement;
7057
7058             /* Add the condition.  */
7059             finish_switch_cond (condition, statement);
7060
7061             /* Parse the body of the switch-statement.  */
7062             in_switch_statement_p = parser->in_switch_statement_p;
7063             in_statement = parser->in_statement;
7064             parser->in_switch_statement_p = true;
7065             parser->in_statement |= IN_SWITCH_STMT;
7066             cp_parser_implicitly_scoped_statement (parser, NULL);
7067             parser->in_switch_statement_p = in_switch_statement_p;
7068             parser->in_statement = in_statement;
7069
7070             /* Now we're all done with the switch-statement.  */
7071             finish_switch_stmt (statement);
7072           }
7073
7074         return statement;
7075       }
7076       break;
7077
7078     default:
7079       cp_parser_error (parser, "expected selection-statement");
7080       return error_mark_node;
7081     }
7082 }
7083
7084 /* Parse a condition.
7085
7086    condition:
7087      expression
7088      type-specifier-seq declarator = assignment-expression
7089
7090    GNU Extension:
7091
7092    condition:
7093      type-specifier-seq declarator asm-specification [opt]
7094        attributes [opt] = assignment-expression
7095
7096    Returns the expression that should be tested.  */
7097
7098 static tree
7099 cp_parser_condition (cp_parser* parser)
7100 {
7101   cp_decl_specifier_seq type_specifiers;
7102   const char *saved_message;
7103
7104   /* Try the declaration first.  */
7105   cp_parser_parse_tentatively (parser);
7106   /* New types are not allowed in the type-specifier-seq for a
7107      condition.  */
7108   saved_message = parser->type_definition_forbidden_message;
7109   parser->type_definition_forbidden_message
7110     = "types may not be defined in conditions";
7111   /* Parse the type-specifier-seq.  */
7112   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7113                                 &type_specifiers);
7114   /* Restore the saved message.  */
7115   parser->type_definition_forbidden_message = saved_message;
7116   /* If all is well, we might be looking at a declaration.  */
7117   if (!cp_parser_error_occurred (parser))
7118     {
7119       tree decl;
7120       tree asm_specification;
7121       tree attributes;
7122       cp_declarator *declarator;
7123       tree initializer = NULL_TREE;
7124
7125       /* Parse the declarator.  */
7126       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7127                                          /*ctor_dtor_or_conv_p=*/NULL,
7128                                          /*parenthesized_p=*/NULL,
7129                                          /*member_p=*/false);
7130       /* Parse the attributes.  */
7131       attributes = cp_parser_attributes_opt (parser);
7132       /* Parse the asm-specification.  */
7133       asm_specification = cp_parser_asm_specification_opt (parser);
7134       /* If the next token is not an `=', then we might still be
7135          looking at an expression.  For example:
7136
7137            if (A(a).x)
7138
7139          looks like a decl-specifier-seq and a declarator -- but then
7140          there is no `=', so this is an expression.  */
7141       cp_parser_require (parser, CPP_EQ, "%<=%>");
7142       /* If we did see an `=', then we are looking at a declaration
7143          for sure.  */
7144       if (cp_parser_parse_definitely (parser))
7145         {
7146           tree pushed_scope;
7147           bool non_constant_p;
7148
7149           /* Create the declaration.  */
7150           decl = start_decl (declarator, &type_specifiers,
7151                              /*initialized_p=*/true,
7152                              attributes, /*prefix_attributes=*/NULL_TREE,
7153                              &pushed_scope);
7154           /* Parse the assignment-expression.  */
7155           initializer
7156             = cp_parser_constant_expression (parser,
7157                                              /*allow_non_constant_p=*/true,
7158                                              &non_constant_p);
7159           if (!non_constant_p)
7160             initializer = fold_non_dependent_expr (initializer);
7161
7162           /* Process the initializer.  */
7163           cp_finish_decl (decl,
7164                           initializer, !non_constant_p,
7165                           asm_specification,
7166                           LOOKUP_ONLYCONVERTING);
7167
7168           if (pushed_scope)
7169             pop_scope (pushed_scope);
7170
7171           return convert_from_reference (decl);
7172         }
7173     }
7174   /* If we didn't even get past the declarator successfully, we are
7175      definitely not looking at a declaration.  */
7176   else
7177     cp_parser_abort_tentative_parse (parser);
7178
7179   /* Otherwise, we are looking at an expression.  */
7180   return cp_parser_expression (parser, /*cast_p=*/false);
7181 }
7182
7183 /* We check for a ) immediately followed by ; with no whitespacing
7184    between.  This is used to issue a warning for:
7185
7186      while (...);
7187
7188    and:
7189
7190      for (...);
7191
7192    as the semicolon is probably extraneous.
7193
7194    On parse errors, the next token might not be a ), so do nothing in
7195    that case. */
7196
7197 static void
7198 check_empty_body (cp_parser* parser, const char* type)
7199 {
7200   cp_token *token;
7201   cp_token *close_paren;
7202   expanded_location close_loc;
7203   expanded_location semi_loc;
7204   
7205   close_paren = cp_lexer_peek_token (parser->lexer);
7206   if (close_paren->type != CPP_CLOSE_PAREN)
7207     return;
7208
7209   close_loc = expand_location (close_paren->location);
7210   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7211
7212   if (token->type != CPP_SEMICOLON
7213       || (token->flags & PREV_WHITE))
7214     return;
7215
7216   semi_loc =  expand_location (token->location);
7217   if (close_loc.line == semi_loc.line
7218       && close_loc.column+1 == semi_loc.column)
7219     warning (OPT_Wempty_body,
7220              "suggest a space before %<;%> or explicit braces around empty "
7221              "body in %<%s%> statement",
7222              type);
7223 }
7224
7225 /* Parse an iteration-statement.
7226
7227    iteration-statement:
7228      while ( condition ) statement
7229      do statement while ( expression ) ;
7230      for ( for-init-statement condition [opt] ; expression [opt] )
7231        statement
7232
7233    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7234
7235 static tree
7236 cp_parser_iteration_statement (cp_parser* parser)
7237 {
7238   cp_token *token;
7239   enum rid keyword;
7240   tree statement;
7241   unsigned char in_statement;
7242
7243   /* Peek at the next token.  */
7244   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7245   if (!token)
7246     return error_mark_node;
7247
7248   /* Remember whether or not we are already within an iteration
7249      statement.  */
7250   in_statement = parser->in_statement;
7251
7252   /* See what kind of keyword it is.  */
7253   keyword = token->keyword;
7254   switch (keyword)
7255     {
7256     case RID_WHILE:
7257       {
7258         tree condition;
7259
7260         /* Begin the while-statement.  */
7261         statement = begin_while_stmt ();
7262         /* Look for the `('.  */
7263         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7264         /* Parse the condition.  */
7265         condition = cp_parser_condition (parser);
7266         finish_while_stmt_cond (condition, statement);
7267         check_empty_body (parser, "while");
7268         /* Look for the `)'.  */
7269         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7270         /* Parse the dependent statement.  */
7271         parser->in_statement = IN_ITERATION_STMT;
7272         cp_parser_already_scoped_statement (parser);
7273         parser->in_statement = in_statement;
7274         /* We're done with the while-statement.  */
7275         finish_while_stmt (statement);
7276       }
7277       break;
7278
7279     case RID_DO:
7280       {
7281         tree expression;
7282
7283         /* Begin the do-statement.  */
7284         statement = begin_do_stmt ();
7285         /* Parse the body of the do-statement.  */
7286         parser->in_statement = IN_ITERATION_STMT;
7287         cp_parser_implicitly_scoped_statement (parser, NULL);
7288         parser->in_statement = in_statement;
7289         finish_do_body (statement);
7290         /* Look for the `while' keyword.  */
7291         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7292         /* Look for the `('.  */
7293         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7294         /* Parse the expression.  */
7295         expression = cp_parser_expression (parser, /*cast_p=*/false);
7296         /* We're done with the do-statement.  */
7297         finish_do_stmt (expression, statement);
7298         /* Look for the `)'.  */
7299         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7300         /* Look for the `;'.  */
7301         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7302       }
7303       break;
7304
7305     case RID_FOR:
7306       {
7307         tree condition = NULL_TREE;
7308         tree expression = NULL_TREE;
7309
7310         /* Begin the for-statement.  */
7311         statement = begin_for_stmt ();
7312         /* Look for the `('.  */
7313         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7314         /* Parse the initialization.  */
7315         cp_parser_for_init_statement (parser);
7316         finish_for_init_stmt (statement);
7317
7318         /* If there's a condition, process it.  */
7319         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7320           condition = cp_parser_condition (parser);
7321         finish_for_cond (condition, statement);
7322         /* Look for the `;'.  */
7323         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7324
7325         /* If there's an expression, process it.  */
7326         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7327           expression = cp_parser_expression (parser, /*cast_p=*/false);
7328         finish_for_expr (expression, statement);
7329         check_empty_body (parser, "for");
7330         /* Look for the `)'.  */
7331         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7332
7333         /* Parse the body of the for-statement.  */
7334         parser->in_statement = IN_ITERATION_STMT;
7335         cp_parser_already_scoped_statement (parser);
7336         parser->in_statement = in_statement;
7337
7338         /* We're done with the for-statement.  */
7339         finish_for_stmt (statement);
7340       }
7341       break;
7342
7343     default:
7344       cp_parser_error (parser, "expected iteration-statement");
7345       statement = error_mark_node;
7346       break;
7347     }
7348
7349   return statement;
7350 }
7351
7352 /* Parse a for-init-statement.
7353
7354    for-init-statement:
7355      expression-statement
7356      simple-declaration  */
7357
7358 static void
7359 cp_parser_for_init_statement (cp_parser* parser)
7360 {
7361   /* If the next token is a `;', then we have an empty
7362      expression-statement.  Grammatically, this is also a
7363      simple-declaration, but an invalid one, because it does not
7364      declare anything.  Therefore, if we did not handle this case
7365      specially, we would issue an error message about an invalid
7366      declaration.  */
7367   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7368     {
7369       /* We're going to speculatively look for a declaration, falling back
7370          to an expression, if necessary.  */
7371       cp_parser_parse_tentatively (parser);
7372       /* Parse the declaration.  */
7373       cp_parser_simple_declaration (parser,
7374                                     /*function_definition_allowed_p=*/false);
7375       /* If the tentative parse failed, then we shall need to look for an
7376          expression-statement.  */
7377       if (cp_parser_parse_definitely (parser))
7378         return;
7379     }
7380
7381   cp_parser_expression_statement (parser, false);
7382 }
7383
7384 /* Parse a jump-statement.
7385
7386    jump-statement:
7387      break ;
7388      continue ;
7389      return expression [opt] ;
7390      goto identifier ;
7391
7392    GNU extension:
7393
7394    jump-statement:
7395      goto * expression ;
7396
7397    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7398
7399 static tree
7400 cp_parser_jump_statement (cp_parser* parser)
7401 {
7402   tree statement = error_mark_node;
7403   cp_token *token;
7404   enum rid keyword;
7405   unsigned char in_statement;
7406
7407   /* Peek at the next token.  */
7408   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7409   if (!token)
7410     return error_mark_node;
7411
7412   /* See what kind of keyword it is.  */
7413   keyword = token->keyword;
7414   switch (keyword)
7415     {
7416     case RID_BREAK:
7417       in_statement = parser->in_statement & ~IN_IF_STMT;      
7418       switch (in_statement)
7419         {
7420         case 0:
7421           error ("break statement not within loop or switch");
7422           break;
7423         default:
7424           gcc_assert ((in_statement & IN_SWITCH_STMT)
7425                       || in_statement == IN_ITERATION_STMT);
7426           statement = finish_break_stmt ();
7427           break;
7428         case IN_OMP_BLOCK:
7429           error ("invalid exit from OpenMP structured block");
7430           break;
7431         case IN_OMP_FOR:
7432           error ("break statement used with OpenMP for loop");
7433           break;
7434         }
7435       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7436       break;
7437
7438     case RID_CONTINUE:
7439       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7440         {
7441         case 0:
7442           error ("continue statement not within a loop");
7443           break;
7444         case IN_ITERATION_STMT:
7445         case IN_OMP_FOR:
7446           statement = finish_continue_stmt ();
7447           break;
7448         case IN_OMP_BLOCK:
7449           error ("invalid exit from OpenMP structured block");
7450           break;
7451         default:
7452           gcc_unreachable ();
7453         }
7454       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7455       break;
7456
7457     case RID_RETURN:
7458       {
7459         tree expr;
7460
7461         /* If the next token is a `;', then there is no
7462            expression.  */
7463         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7464           expr = cp_parser_expression (parser, /*cast_p=*/false);
7465         else
7466           expr = NULL_TREE;
7467         /* Build the return-statement.  */
7468         statement = finish_return_stmt (expr);
7469         /* Look for the final `;'.  */
7470         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7471       }
7472       break;
7473
7474     case RID_GOTO:
7475       /* Create the goto-statement.  */
7476       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7477         {
7478           /* Issue a warning about this use of a GNU extension.  */
7479           if (pedantic)
7480             pedwarn ("ISO C++ forbids computed gotos");
7481           /* Consume the '*' token.  */
7482           cp_lexer_consume_token (parser->lexer);
7483           /* Parse the dependent expression.  */
7484           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7485         }
7486       else
7487         finish_goto_stmt (cp_parser_identifier (parser));
7488       /* Look for the final `;'.  */
7489       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7490       break;
7491
7492     default:
7493       cp_parser_error (parser, "expected jump-statement");
7494       break;
7495     }
7496
7497   return statement;
7498 }
7499
7500 /* Parse a declaration-statement.
7501
7502    declaration-statement:
7503      block-declaration  */
7504
7505 static void
7506 cp_parser_declaration_statement (cp_parser* parser)
7507 {
7508   void *p;
7509
7510   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7511   p = obstack_alloc (&declarator_obstack, 0);
7512
7513  /* Parse the block-declaration.  */
7514   cp_parser_block_declaration (parser, /*statement_p=*/true);
7515
7516   /* Free any declarators allocated.  */
7517   obstack_free (&declarator_obstack, p);
7518
7519   /* Finish off the statement.  */
7520   finish_stmt ();
7521 }
7522
7523 /* Some dependent statements (like `if (cond) statement'), are
7524    implicitly in their own scope.  In other words, if the statement is
7525    a single statement (as opposed to a compound-statement), it is
7526    none-the-less treated as if it were enclosed in braces.  Any
7527    declarations appearing in the dependent statement are out of scope
7528    after control passes that point.  This function parses a statement,
7529    but ensures that is in its own scope, even if it is not a
7530    compound-statement.
7531
7532    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7533    is a (possibly labeled) if statement which is not enclosed in
7534    braces and has an else clause.  This is used to implement
7535    -Wparentheses.
7536
7537    Returns the new statement.  */
7538
7539 static tree
7540 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7541 {
7542   tree statement;
7543
7544   if (if_p != NULL)
7545     *if_p = false;
7546
7547   /* Mark if () ; with a special NOP_EXPR.  */
7548   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7549     {
7550       cp_lexer_consume_token (parser->lexer);
7551       statement = add_stmt (build_empty_stmt ());
7552     }
7553   /* if a compound is opened, we simply parse the statement directly.  */
7554   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7555     statement = cp_parser_compound_statement (parser, NULL, false);
7556   /* If the token is not a `{', then we must take special action.  */
7557   else
7558     {
7559       /* Create a compound-statement.  */
7560       statement = begin_compound_stmt (0);
7561       /* Parse the dependent-statement.  */
7562       cp_parser_statement (parser, NULL_TREE, false, if_p);
7563       /* Finish the dummy compound-statement.  */
7564       finish_compound_stmt (statement);
7565     }
7566
7567   /* Return the statement.  */
7568   return statement;
7569 }
7570
7571 /* For some dependent statements (like `while (cond) statement'), we
7572    have already created a scope.  Therefore, even if the dependent
7573    statement is a compound-statement, we do not want to create another
7574    scope.  */
7575
7576 static void
7577 cp_parser_already_scoped_statement (cp_parser* parser)
7578 {
7579   /* If the token is a `{', then we must take special action.  */
7580   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7581     cp_parser_statement (parser, NULL_TREE, false, NULL);
7582   else
7583     {
7584       /* Avoid calling cp_parser_compound_statement, so that we
7585          don't create a new scope.  Do everything else by hand.  */
7586       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7587       cp_parser_statement_seq_opt (parser, NULL_TREE);
7588       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7589     }
7590 }
7591
7592 /* Declarations [gram.dcl.dcl] */
7593
7594 /* Parse an optional declaration-sequence.
7595
7596    declaration-seq:
7597      declaration
7598      declaration-seq declaration  */
7599
7600 static void
7601 cp_parser_declaration_seq_opt (cp_parser* parser)
7602 {
7603   while (true)
7604     {
7605       cp_token *token;
7606
7607       token = cp_lexer_peek_token (parser->lexer);
7608
7609       if (token->type == CPP_CLOSE_BRACE
7610           || token->type == CPP_EOF
7611           || token->type == CPP_PRAGMA_EOL)
7612         break;
7613
7614       if (token->type == CPP_SEMICOLON)
7615         {
7616           /* A declaration consisting of a single semicolon is
7617              invalid.  Allow it unless we're being pedantic.  */
7618           cp_lexer_consume_token (parser->lexer);
7619           if (pedantic && !in_system_header)
7620             pedwarn ("extra %<;%>");
7621           continue;
7622         }
7623
7624       /* If we're entering or exiting a region that's implicitly
7625          extern "C", modify the lang context appropriately.  */
7626       if (!parser->implicit_extern_c && token->implicit_extern_c)
7627         {
7628           push_lang_context (lang_name_c);
7629           parser->implicit_extern_c = true;
7630         }
7631       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7632         {
7633           pop_lang_context ();
7634           parser->implicit_extern_c = false;
7635         }
7636
7637       if (token->type == CPP_PRAGMA)
7638         {
7639           /* A top-level declaration can consist solely of a #pragma.
7640              A nested declaration cannot, so this is done here and not
7641              in cp_parser_declaration.  (A #pragma at block scope is
7642              handled in cp_parser_statement.)  */
7643           cp_parser_pragma (parser, pragma_external);
7644           continue;
7645         }
7646
7647       /* Parse the declaration itself.  */
7648       cp_parser_declaration (parser);
7649     }
7650 }
7651
7652 /* Parse a declaration.
7653
7654    declaration:
7655      block-declaration
7656      function-definition
7657      template-declaration
7658      explicit-instantiation
7659      explicit-specialization
7660      linkage-specification
7661      namespace-definition
7662
7663    GNU extension:
7664
7665    declaration:
7666       __extension__ declaration */
7667
7668 static void
7669 cp_parser_declaration (cp_parser* parser)
7670 {
7671   cp_token token1;
7672   cp_token token2;
7673   int saved_pedantic;
7674   void *p;
7675
7676   /* Check for the `__extension__' keyword.  */
7677   if (cp_parser_extension_opt (parser, &saved_pedantic))
7678     {
7679       /* Parse the qualified declaration.  */
7680       cp_parser_declaration (parser);
7681       /* Restore the PEDANTIC flag.  */
7682       pedantic = saved_pedantic;
7683
7684       return;
7685     }
7686
7687   /* Try to figure out what kind of declaration is present.  */
7688   token1 = *cp_lexer_peek_token (parser->lexer);
7689
7690   if (token1.type != CPP_EOF)
7691     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7692   else
7693     {
7694       token2.type = CPP_EOF;
7695       token2.keyword = RID_MAX;
7696     }
7697
7698   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7699   p = obstack_alloc (&declarator_obstack, 0);
7700
7701   /* If the next token is `extern' and the following token is a string
7702      literal, then we have a linkage specification.  */
7703   if (token1.keyword == RID_EXTERN
7704       && cp_parser_is_string_literal (&token2))
7705     cp_parser_linkage_specification (parser);
7706   /* If the next token is `template', then we have either a template
7707      declaration, an explicit instantiation, or an explicit
7708      specialization.  */
7709   else if (token1.keyword == RID_TEMPLATE)
7710     {
7711       /* `template <>' indicates a template specialization.  */
7712       if (token2.type == CPP_LESS
7713           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7714         cp_parser_explicit_specialization (parser);
7715       /* `template <' indicates a template declaration.  */
7716       else if (token2.type == CPP_LESS)
7717         cp_parser_template_declaration (parser, /*member_p=*/false);
7718       /* Anything else must be an explicit instantiation.  */
7719       else
7720         cp_parser_explicit_instantiation (parser);
7721     }
7722   /* If the next token is `export', then we have a template
7723      declaration.  */
7724   else if (token1.keyword == RID_EXPORT)
7725     cp_parser_template_declaration (parser, /*member_p=*/false);
7726   /* If the next token is `extern', 'static' or 'inline' and the one
7727      after that is `template', we have a GNU extended explicit
7728      instantiation directive.  */
7729   else if (cp_parser_allow_gnu_extensions_p (parser)
7730            && (token1.keyword == RID_EXTERN
7731                || token1.keyword == RID_STATIC
7732                || token1.keyword == RID_INLINE)
7733            && token2.keyword == RID_TEMPLATE)
7734     cp_parser_explicit_instantiation (parser);
7735   /* If the next token is `namespace', check for a named or unnamed
7736      namespace definition.  */
7737   else if (token1.keyword == RID_NAMESPACE
7738            && (/* A named namespace definition.  */
7739                (token2.type == CPP_NAME
7740                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7741                     != CPP_EQ))
7742                /* An unnamed namespace definition.  */
7743                || token2.type == CPP_OPEN_BRACE
7744                || token2.keyword == RID_ATTRIBUTE))
7745     cp_parser_namespace_definition (parser);
7746   /* An inline (associated) namespace definition.  */
7747   else if (token1.keyword == RID_INLINE
7748            && token2.keyword == RID_NAMESPACE)
7749     cp_parser_namespace_definition (parser);
7750   /* Objective-C++ declaration/definition.  */
7751   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7752     cp_parser_objc_declaration (parser);
7753   /* We must have either a block declaration or a function
7754      definition.  */
7755   else
7756     /* Try to parse a block-declaration, or a function-definition.  */
7757     cp_parser_block_declaration (parser, /*statement_p=*/false);
7758
7759   /* Free any declarators allocated.  */
7760   obstack_free (&declarator_obstack, p);
7761 }
7762
7763 /* Parse a block-declaration.
7764
7765    block-declaration:
7766      simple-declaration
7767      asm-definition
7768      namespace-alias-definition
7769      using-declaration
7770      using-directive
7771
7772    GNU Extension:
7773
7774    block-declaration:
7775      __extension__ block-declaration
7776
7777    C++0x Extension:
7778
7779    block-declaration:
7780      static_assert-declaration
7781
7782    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7783    part of a declaration-statement.  */
7784
7785 static void
7786 cp_parser_block_declaration (cp_parser *parser,
7787                              bool      statement_p)
7788 {
7789   cp_token *token1;
7790   int saved_pedantic;
7791
7792   /* Check for the `__extension__' keyword.  */
7793   if (cp_parser_extension_opt (parser, &saved_pedantic))
7794     {
7795       /* Parse the qualified declaration.  */
7796       cp_parser_block_declaration (parser, statement_p);
7797       /* Restore the PEDANTIC flag.  */
7798       pedantic = saved_pedantic;
7799
7800       return;
7801     }
7802
7803   /* Peek at the next token to figure out which kind of declaration is
7804      present.  */
7805   token1 = cp_lexer_peek_token (parser->lexer);
7806
7807   /* If the next keyword is `asm', we have an asm-definition.  */
7808   if (token1->keyword == RID_ASM)
7809     {
7810       if (statement_p)
7811         cp_parser_commit_to_tentative_parse (parser);
7812       cp_parser_asm_definition (parser);
7813     }
7814   /* If the next keyword is `namespace', we have a
7815      namespace-alias-definition.  */
7816   else if (token1->keyword == RID_NAMESPACE)
7817     cp_parser_namespace_alias_definition (parser);
7818   /* If the next keyword is `using', we have either a
7819      using-declaration or a using-directive.  */
7820   else if (token1->keyword == RID_USING)
7821     {
7822       cp_token *token2;
7823
7824       if (statement_p)
7825         cp_parser_commit_to_tentative_parse (parser);
7826       /* If the token after `using' is `namespace', then we have a
7827          using-directive.  */
7828       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7829       if (token2->keyword == RID_NAMESPACE)
7830         cp_parser_using_directive (parser);
7831       /* Otherwise, it's a using-declaration.  */
7832       else
7833         cp_parser_using_declaration (parser,
7834                                      /*access_declaration_p=*/false);
7835     }
7836   /* If the next keyword is `__label__' we have a misplaced label
7837      declaration.  */
7838   else if (token1->keyword == RID_LABEL)
7839     {
7840       cp_lexer_consume_token (parser->lexer);
7841       error ("%<__label__%> not at the beginning of a block");
7842       cp_parser_skip_to_end_of_statement (parser);
7843       /* If the next token is now a `;', consume it.  */
7844       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7845         cp_lexer_consume_token (parser->lexer);
7846     }
7847   /* If the next token is `static_assert' we have a static assertion.  */
7848   else if (token1->keyword == RID_STATIC_ASSERT)
7849     cp_parser_static_assert (parser, /*member_p=*/false);
7850   /* Anything else must be a simple-declaration.  */
7851   else
7852     cp_parser_simple_declaration (parser, !statement_p);
7853 }
7854
7855 /* Parse a simple-declaration.
7856
7857    simple-declaration:
7858      decl-specifier-seq [opt] init-declarator-list [opt] ;
7859
7860    init-declarator-list:
7861      init-declarator
7862      init-declarator-list , init-declarator
7863
7864    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7865    function-definition as a simple-declaration.  */
7866
7867 static void
7868 cp_parser_simple_declaration (cp_parser* parser,
7869                               bool function_definition_allowed_p)
7870 {
7871   cp_decl_specifier_seq decl_specifiers;
7872   int declares_class_or_enum;
7873   bool saw_declarator;
7874
7875   /* Defer access checks until we know what is being declared; the
7876      checks for names appearing in the decl-specifier-seq should be
7877      done as if we were in the scope of the thing being declared.  */
7878   push_deferring_access_checks (dk_deferred);
7879
7880   /* Parse the decl-specifier-seq.  We have to keep track of whether
7881      or not the decl-specifier-seq declares a named class or
7882      enumeration type, since that is the only case in which the
7883      init-declarator-list is allowed to be empty.
7884
7885      [dcl.dcl]
7886
7887      In a simple-declaration, the optional init-declarator-list can be
7888      omitted only when declaring a class or enumeration, that is when
7889      the decl-specifier-seq contains either a class-specifier, an
7890      elaborated-type-specifier, or an enum-specifier.  */
7891   cp_parser_decl_specifier_seq (parser,
7892                                 CP_PARSER_FLAGS_OPTIONAL,
7893                                 &decl_specifiers,
7894                                 &declares_class_or_enum);
7895   /* We no longer need to defer access checks.  */
7896   stop_deferring_access_checks ();
7897
7898   /* In a block scope, a valid declaration must always have a
7899      decl-specifier-seq.  By not trying to parse declarators, we can
7900      resolve the declaration/expression ambiguity more quickly.  */
7901   if (!function_definition_allowed_p
7902       && !decl_specifiers.any_specifiers_p)
7903     {
7904       cp_parser_error (parser, "expected declaration");
7905       goto done;
7906     }
7907
7908   /* If the next two tokens are both identifiers, the code is
7909      erroneous. The usual cause of this situation is code like:
7910
7911        T t;
7912
7913      where "T" should name a type -- but does not.  */
7914   if (!decl_specifiers.type
7915       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7916     {
7917       /* If parsing tentatively, we should commit; we really are
7918          looking at a declaration.  */
7919       cp_parser_commit_to_tentative_parse (parser);
7920       /* Give up.  */
7921       goto done;
7922     }
7923
7924   /* If we have seen at least one decl-specifier, and the next token
7925      is not a parenthesis, then we must be looking at a declaration.
7926      (After "int (" we might be looking at a functional cast.)  */
7927   if (decl_specifiers.any_specifiers_p
7928       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7929     cp_parser_commit_to_tentative_parse (parser);
7930
7931   /* Keep going until we hit the `;' at the end of the simple
7932      declaration.  */
7933   saw_declarator = false;
7934   while (cp_lexer_next_token_is_not (parser->lexer,
7935                                      CPP_SEMICOLON))
7936     {
7937       cp_token *token;
7938       bool function_definition_p;
7939       tree decl;
7940
7941       if (saw_declarator)
7942         {
7943           /* If we are processing next declarator, coma is expected */
7944           token = cp_lexer_peek_token (parser->lexer);
7945           gcc_assert (token->type == CPP_COMMA);
7946           cp_lexer_consume_token (parser->lexer);
7947         }
7948       else
7949         saw_declarator = true;
7950
7951       /* Parse the init-declarator.  */
7952       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7953                                         /*checks=*/NULL,
7954                                         function_definition_allowed_p,
7955                                         /*member_p=*/false,
7956                                         declares_class_or_enum,
7957                                         &function_definition_p);
7958       /* If an error occurred while parsing tentatively, exit quickly.
7959          (That usually happens when in the body of a function; each
7960          statement is treated as a declaration-statement until proven
7961          otherwise.)  */
7962       if (cp_parser_error_occurred (parser))
7963         goto done;
7964       /* Handle function definitions specially.  */
7965       if (function_definition_p)
7966         {
7967           /* If the next token is a `,', then we are probably
7968              processing something like:
7969
7970                void f() {}, *p;
7971
7972              which is erroneous.  */
7973           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7974             error ("mixing declarations and function-definitions is forbidden");
7975           /* Otherwise, we're done with the list of declarators.  */
7976           else
7977             {
7978               pop_deferring_access_checks ();
7979               return;
7980             }
7981         }
7982       /* The next token should be either a `,' or a `;'.  */
7983       token = cp_lexer_peek_token (parser->lexer);
7984       /* If it's a `,', there are more declarators to come.  */
7985       if (token->type == CPP_COMMA)
7986         /* will be consumed next time around */;
7987       /* If it's a `;', we are done.  */
7988       else if (token->type == CPP_SEMICOLON)
7989         break;
7990       /* Anything else is an error.  */
7991       else
7992         {
7993           /* If we have already issued an error message we don't need
7994              to issue another one.  */
7995           if (decl != error_mark_node
7996               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7997             cp_parser_error (parser, "expected %<,%> or %<;%>");
7998           /* Skip tokens until we reach the end of the statement.  */
7999           cp_parser_skip_to_end_of_statement (parser);
8000           /* If the next token is now a `;', consume it.  */
8001           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8002             cp_lexer_consume_token (parser->lexer);
8003           goto done;
8004         }
8005       /* After the first time around, a function-definition is not
8006          allowed -- even if it was OK at first.  For example:
8007
8008            int i, f() {}
8009
8010          is not valid.  */
8011       function_definition_allowed_p = false;
8012     }
8013
8014   /* Issue an error message if no declarators are present, and the
8015      decl-specifier-seq does not itself declare a class or
8016      enumeration.  */
8017   if (!saw_declarator)
8018     {
8019       if (cp_parser_declares_only_class_p (parser))
8020         shadow_tag (&decl_specifiers);
8021       /* Perform any deferred access checks.  */
8022       perform_deferred_access_checks ();
8023     }
8024
8025   /* Consume the `;'.  */
8026   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8027
8028  done:
8029   pop_deferring_access_checks ();
8030 }
8031
8032 /* Parse a decl-specifier-seq.
8033
8034    decl-specifier-seq:
8035      decl-specifier-seq [opt] decl-specifier
8036
8037    decl-specifier:
8038      storage-class-specifier
8039      type-specifier
8040      function-specifier
8041      friend
8042      typedef
8043
8044    GNU Extension:
8045
8046    decl-specifier:
8047      attributes
8048
8049    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8050
8051    The parser flags FLAGS is used to control type-specifier parsing.
8052
8053    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8054    flags:
8055
8056      1: one of the decl-specifiers is an elaborated-type-specifier
8057         (i.e., a type declaration)
8058      2: one of the decl-specifiers is an enum-specifier or a
8059         class-specifier (i.e., a type definition)
8060
8061    */
8062
8063 static void
8064 cp_parser_decl_specifier_seq (cp_parser* parser,
8065                               cp_parser_flags flags,
8066                               cp_decl_specifier_seq *decl_specs,
8067                               int* declares_class_or_enum)
8068 {
8069   bool constructor_possible_p = !parser->in_declarator_p;
8070
8071   /* Clear DECL_SPECS.  */
8072   clear_decl_specs (decl_specs);
8073
8074   /* Assume no class or enumeration type is declared.  */
8075   *declares_class_or_enum = 0;
8076
8077   /* Keep reading specifiers until there are no more to read.  */
8078   while (true)
8079     {
8080       bool constructor_p;
8081       bool found_decl_spec;
8082       cp_token *token;
8083
8084       /* Peek at the next token.  */
8085       token = cp_lexer_peek_token (parser->lexer);
8086       /* Handle attributes.  */
8087       if (token->keyword == RID_ATTRIBUTE)
8088         {
8089           /* Parse the attributes.  */
8090           decl_specs->attributes
8091             = chainon (decl_specs->attributes,
8092                        cp_parser_attributes_opt (parser));
8093           continue;
8094         }
8095       /* Assume we will find a decl-specifier keyword.  */
8096       found_decl_spec = true;
8097       /* If the next token is an appropriate keyword, we can simply
8098          add it to the list.  */
8099       switch (token->keyword)
8100         {
8101           /* decl-specifier:
8102                friend  */
8103         case RID_FRIEND:
8104           if (!at_class_scope_p ())
8105             {
8106               error ("%H%<friend%> used outside of class", &token->location);
8107               cp_lexer_purge_token (parser->lexer);
8108             }
8109           else
8110             {
8111               ++decl_specs->specs[(int) ds_friend];
8112               /* Consume the token.  */
8113               cp_lexer_consume_token (parser->lexer);
8114             }
8115           break;
8116
8117           /* function-specifier:
8118                inline
8119                virtual
8120                explicit  */
8121         case RID_INLINE:
8122         case RID_VIRTUAL:
8123         case RID_EXPLICIT:
8124           cp_parser_function_specifier_opt (parser, decl_specs);
8125           break;
8126
8127           /* decl-specifier:
8128                typedef  */
8129         case RID_TYPEDEF:
8130           ++decl_specs->specs[(int) ds_typedef];
8131           /* Consume the token.  */
8132           cp_lexer_consume_token (parser->lexer);
8133           /* A constructor declarator cannot appear in a typedef.  */
8134           constructor_possible_p = false;
8135           /* The "typedef" keyword can only occur in a declaration; we
8136              may as well commit at this point.  */
8137           cp_parser_commit_to_tentative_parse (parser);
8138
8139           if (decl_specs->storage_class != sc_none)
8140             decl_specs->conflicting_specifiers_p = true;
8141           break;
8142
8143           /* storage-class-specifier:
8144                auto
8145                register
8146                static
8147                extern
8148                mutable
8149
8150              GNU Extension:
8151                thread  */
8152         case RID_AUTO:
8153           /* Consume the token.  */
8154           cp_lexer_consume_token (parser->lexer);
8155
8156           if (cxx_dialect == cxx98) 
8157             {
8158               /* Complain about `auto' as a storage specifier, if
8159                  we're complaining about C++0x compatibility.  */
8160               warning 
8161                 (OPT_Wc__0x_compat, 
8162                  "%<auto%> will change meaning in C++0x; please remove it");
8163
8164               /* Set the storage class anyway.  */
8165               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO);
8166             }
8167           else 
8168             /* We do not yet support the use of `auto' as a
8169                type-specifier.  */
8170             error ("C++0x %<auto%> specifier not supported");
8171           break;
8172
8173         case RID_REGISTER:
8174         case RID_STATIC:
8175         case RID_EXTERN:
8176         case RID_MUTABLE:
8177           /* Consume the token.  */
8178           cp_lexer_consume_token (parser->lexer);
8179           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8180           break;
8181         case RID_THREAD:
8182           /* Consume the token.  */
8183           cp_lexer_consume_token (parser->lexer);
8184           ++decl_specs->specs[(int) ds_thread];
8185           break;
8186
8187         default:
8188           /* We did not yet find a decl-specifier yet.  */
8189           found_decl_spec = false;
8190           break;
8191         }
8192
8193       /* Constructors are a special case.  The `S' in `S()' is not a
8194          decl-specifier; it is the beginning of the declarator.  */
8195       constructor_p
8196         = (!found_decl_spec
8197            && constructor_possible_p
8198            && (cp_parser_constructor_declarator_p
8199                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8200
8201       /* If we don't have a DECL_SPEC yet, then we must be looking at
8202          a type-specifier.  */
8203       if (!found_decl_spec && !constructor_p)
8204         {
8205           int decl_spec_declares_class_or_enum;
8206           bool is_cv_qualifier;
8207           tree type_spec;
8208
8209           type_spec
8210             = cp_parser_type_specifier (parser, flags,
8211                                         decl_specs,
8212                                         /*is_declaration=*/true,
8213                                         &decl_spec_declares_class_or_enum,
8214                                         &is_cv_qualifier);
8215
8216           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8217
8218           /* If this type-specifier referenced a user-defined type
8219              (a typedef, class-name, etc.), then we can't allow any
8220              more such type-specifiers henceforth.
8221
8222              [dcl.spec]
8223
8224              The longest sequence of decl-specifiers that could
8225              possibly be a type name is taken as the
8226              decl-specifier-seq of a declaration.  The sequence shall
8227              be self-consistent as described below.
8228
8229              [dcl.type]
8230
8231              As a general rule, at most one type-specifier is allowed
8232              in the complete decl-specifier-seq of a declaration.  The
8233              only exceptions are the following:
8234
8235              -- const or volatile can be combined with any other
8236                 type-specifier.
8237
8238              -- signed or unsigned can be combined with char, long,
8239                 short, or int.
8240
8241              -- ..
8242
8243              Example:
8244
8245                typedef char* Pc;
8246                void g (const int Pc);
8247
8248              Here, Pc is *not* part of the decl-specifier seq; it's
8249              the declarator.  Therefore, once we see a type-specifier
8250              (other than a cv-qualifier), we forbid any additional
8251              user-defined types.  We *do* still allow things like `int
8252              int' to be considered a decl-specifier-seq, and issue the
8253              error message later.  */
8254           if (type_spec && !is_cv_qualifier)
8255             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8256           /* A constructor declarator cannot follow a type-specifier.  */
8257           if (type_spec)
8258             {
8259               constructor_possible_p = false;
8260               found_decl_spec = true;
8261             }
8262         }
8263
8264       /* If we still do not have a DECL_SPEC, then there are no more
8265          decl-specifiers.  */
8266       if (!found_decl_spec)
8267         break;
8268
8269       decl_specs->any_specifiers_p = true;
8270       /* After we see one decl-specifier, further decl-specifiers are
8271          always optional.  */
8272       flags |= CP_PARSER_FLAGS_OPTIONAL;
8273     }
8274
8275   cp_parser_check_decl_spec (decl_specs);
8276
8277   /* Don't allow a friend specifier with a class definition.  */
8278   if (decl_specs->specs[(int) ds_friend] != 0
8279       && (*declares_class_or_enum & 2))
8280     error ("class definition may not be declared a friend");
8281 }
8282
8283 /* Parse an (optional) storage-class-specifier.
8284
8285    storage-class-specifier:
8286      auto
8287      register
8288      static
8289      extern
8290      mutable
8291
8292    GNU Extension:
8293
8294    storage-class-specifier:
8295      thread
8296
8297    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8298
8299 static tree
8300 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8301 {
8302   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8303     {
8304     case RID_AUTO:
8305       if (cxx_dialect != cxx98)
8306         return NULL_TREE;
8307       /* Fall through for C++98.  */
8308
8309     case RID_REGISTER:
8310     case RID_STATIC:
8311     case RID_EXTERN:
8312     case RID_MUTABLE:
8313     case RID_THREAD:
8314       /* Consume the token.  */
8315       return cp_lexer_consume_token (parser->lexer)->u.value;
8316
8317     default:
8318       return NULL_TREE;
8319     }
8320 }
8321
8322 /* Parse an (optional) function-specifier.
8323
8324    function-specifier:
8325      inline
8326      virtual
8327      explicit
8328
8329    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8330    Updates DECL_SPECS, if it is non-NULL.  */
8331
8332 static tree
8333 cp_parser_function_specifier_opt (cp_parser* parser,
8334                                   cp_decl_specifier_seq *decl_specs)
8335 {
8336   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8337     {
8338     case RID_INLINE:
8339       if (decl_specs)
8340         ++decl_specs->specs[(int) ds_inline];
8341       break;
8342
8343     case RID_VIRTUAL:
8344       /* 14.5.2.3 [temp.mem]
8345
8346          A member function template shall not be virtual.  */
8347       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8348         error ("templates may not be %<virtual%>");
8349       else if (decl_specs)
8350         ++decl_specs->specs[(int) ds_virtual];
8351       break;
8352
8353     case RID_EXPLICIT:
8354       if (decl_specs)
8355         ++decl_specs->specs[(int) ds_explicit];
8356       break;
8357
8358     default:
8359       return NULL_TREE;
8360     }
8361
8362   /* Consume the token.  */
8363   return cp_lexer_consume_token (parser->lexer)->u.value;
8364 }
8365
8366 /* Parse a linkage-specification.
8367
8368    linkage-specification:
8369      extern string-literal { declaration-seq [opt] }
8370      extern string-literal declaration  */
8371
8372 static void
8373 cp_parser_linkage_specification (cp_parser* parser)
8374 {
8375   tree linkage;
8376
8377   /* Look for the `extern' keyword.  */
8378   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8379
8380   /* Look for the string-literal.  */
8381   linkage = cp_parser_string_literal (parser, false, false);
8382
8383   /* Transform the literal into an identifier.  If the literal is a
8384      wide-character string, or contains embedded NULs, then we can't
8385      handle it as the user wants.  */
8386   if (strlen (TREE_STRING_POINTER (linkage))
8387       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8388     {
8389       cp_parser_error (parser, "invalid linkage-specification");
8390       /* Assume C++ linkage.  */
8391       linkage = lang_name_cplusplus;
8392     }
8393   else
8394     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8395
8396   /* We're now using the new linkage.  */
8397   push_lang_context (linkage);
8398
8399   /* If the next token is a `{', then we're using the first
8400      production.  */
8401   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8402     {
8403       /* Consume the `{' token.  */
8404       cp_lexer_consume_token (parser->lexer);
8405       /* Parse the declarations.  */
8406       cp_parser_declaration_seq_opt (parser);
8407       /* Look for the closing `}'.  */
8408       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8409     }
8410   /* Otherwise, there's just one declaration.  */
8411   else
8412     {
8413       bool saved_in_unbraced_linkage_specification_p;
8414
8415       saved_in_unbraced_linkage_specification_p
8416         = parser->in_unbraced_linkage_specification_p;
8417       parser->in_unbraced_linkage_specification_p = true;
8418       cp_parser_declaration (parser);
8419       parser->in_unbraced_linkage_specification_p
8420         = saved_in_unbraced_linkage_specification_p;
8421     }
8422
8423   /* We're done with the linkage-specification.  */
8424   pop_lang_context ();
8425 }
8426
8427 /* Parse a static_assert-declaration.
8428
8429    static_assert-declaration:
8430      static_assert ( constant-expression , string-literal ) ; 
8431
8432    If MEMBER_P, this static_assert is a class member.  */
8433
8434 static void 
8435 cp_parser_static_assert(cp_parser *parser, bool member_p)
8436 {
8437   tree condition;
8438   tree message;
8439   cp_token *token;
8440   location_t saved_loc;
8441
8442   /* Peek at the `static_assert' token so we can keep track of exactly
8443      where the static assertion started.  */
8444   token = cp_lexer_peek_token (parser->lexer);
8445   saved_loc = token->location;
8446
8447   /* Look for the `static_assert' keyword.  */
8448   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8449                                   "%<static_assert%>"))
8450     return;
8451
8452   /*  We know we are in a static assertion; commit to any tentative
8453       parse.  */
8454   if (cp_parser_parsing_tentatively (parser))
8455     cp_parser_commit_to_tentative_parse (parser);
8456
8457   /* Parse the `(' starting the static assertion condition.  */
8458   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8459
8460   /* Parse the constant-expression.  */
8461   condition = 
8462     cp_parser_constant_expression (parser,
8463                                    /*allow_non_constant_p=*/false,
8464                                    /*non_constant_p=*/NULL);
8465
8466   /* Parse the separating `,'.  */
8467   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8468
8469   /* Parse the string-literal message.  */
8470   message = cp_parser_string_literal (parser, 
8471                                       /*translate=*/false,
8472                                       /*wide_ok=*/true);
8473
8474   /* A `)' completes the static assertion.  */
8475   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8476     cp_parser_skip_to_closing_parenthesis (parser, 
8477                                            /*recovering=*/true, 
8478                                            /*or_comma=*/false,
8479                                            /*consume_paren=*/true);
8480
8481   /* A semicolon terminates the declaration.  */
8482   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8483
8484   /* Complete the static assertion, which may mean either processing 
8485      the static assert now or saving it for template instantiation.  */
8486   finish_static_assert (condition, message, saved_loc, member_p);
8487 }
8488
8489 /* Parse a `decltype' type. Returns the type. 
8490
8491    simple-type-specifier:
8492      decltype ( expression )  */
8493
8494 static tree
8495 cp_parser_decltype (cp_parser *parser)
8496 {
8497   tree expr;
8498   bool id_expression_or_member_access_p = false;
8499   const char *saved_message;
8500   bool saved_integral_constant_expression_p;
8501   bool saved_non_integral_constant_expression_p;
8502
8503   /* Look for the `decltype' token.  */
8504   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8505     return error_mark_node;
8506
8507   /* Types cannot be defined in a `decltype' expression.  Save away the
8508      old message.  */
8509   saved_message = parser->type_definition_forbidden_message;
8510
8511   /* And create the new one.  */
8512   parser->type_definition_forbidden_message
8513     = "types may not be defined in `decltype' expressions";
8514
8515   /* The restrictions on constant-expressions do not apply inside
8516      decltype expressions.  */
8517   saved_integral_constant_expression_p
8518     = parser->integral_constant_expression_p;
8519   saved_non_integral_constant_expression_p
8520     = parser->non_integral_constant_expression_p;
8521   parser->integral_constant_expression_p = false;
8522
8523   /* Do not actually evaluate the expression.  */
8524   ++skip_evaluation;
8525
8526   /* Parse the opening `('.  */
8527   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8528     return error_mark_node;
8529   
8530   /* First, try parsing an id-expression.  */
8531   cp_parser_parse_tentatively (parser);
8532   expr = cp_parser_id_expression (parser,
8533                                   /*template_keyword_p=*/false,
8534                                   /*check_dependency_p=*/true,
8535                                   /*template_p=*/NULL,
8536                                   /*declarator_p=*/false,
8537                                   /*optional_p=*/false);
8538
8539   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8540     {
8541       bool non_integral_constant_expression_p = false;
8542       tree id_expression = expr;
8543       cp_id_kind idk;
8544       const char *error_msg;
8545
8546       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8547         /* Lookup the name we got back from the id-expression.  */
8548         expr = cp_parser_lookup_name (parser, expr,
8549                                       none_type,
8550                                       /*is_template=*/false,
8551                                       /*is_namespace=*/false,
8552                                       /*check_dependency=*/true,
8553                                       /*ambiguous_decls=*/NULL);
8554
8555       if (expr
8556           && expr != error_mark_node
8557           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8558           && TREE_CODE (expr) != TYPE_DECL
8559           && (TREE_CODE (expr) != BIT_NOT_EXPR
8560               || !TYPE_P (TREE_OPERAND (expr, 0)))
8561           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8562         {
8563           /* Complete lookup of the id-expression.  */
8564           expr = (finish_id_expression
8565                   (id_expression, expr, parser->scope, &idk,
8566                    /*integral_constant_expression_p=*/false,
8567                    /*allow_non_integral_constant_expression_p=*/true,
8568                    &non_integral_constant_expression_p,
8569                    /*template_p=*/false,
8570                    /*done=*/true,
8571                    /*address_p=*/false,
8572                    /*template_arg_p=*/false,
8573                    &error_msg));
8574
8575           if (expr == error_mark_node)
8576             /* We found an id-expression, but it was something that we
8577                should not have found. This is an error, not something
8578                we can recover from, so note that we found an
8579                id-expression and we'll recover as gracefully as
8580                possible.  */
8581             id_expression_or_member_access_p = true;
8582         }
8583
8584       if (expr 
8585           && expr != error_mark_node
8586           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8587         /* We have an id-expression.  */
8588         id_expression_or_member_access_p = true;
8589     }
8590
8591   if (!id_expression_or_member_access_p)
8592     {
8593       /* Abort the id-expression parse.  */
8594       cp_parser_abort_tentative_parse (parser);
8595
8596       /* Parsing tentatively, again.  */
8597       cp_parser_parse_tentatively (parser);
8598
8599       /* Parse a class member access.  */
8600       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8601                                            /*cast_p=*/false,
8602                                            /*member_access_only_p=*/true);
8603
8604       if (expr 
8605           && expr != error_mark_node
8606           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8607         /* We have an id-expression.  */
8608         id_expression_or_member_access_p = true;
8609     }
8610
8611   if (id_expression_or_member_access_p)
8612     /* We have parsed the complete id-expression or member access.  */
8613     cp_parser_parse_definitely (parser);
8614   else
8615     {
8616       /* Abort our attempt to parse an id-expression or member access
8617          expression.  */
8618       cp_parser_abort_tentative_parse (parser);
8619
8620       /* Parse a full expression.  */
8621       expr = cp_parser_expression (parser, /*cast_p=*/false);
8622     }
8623
8624   /* Go back to evaluating expressions.  */
8625   --skip_evaluation;
8626
8627   /* Restore the old message and the integral constant expression
8628      flags.  */
8629   parser->type_definition_forbidden_message = saved_message;
8630   parser->integral_constant_expression_p
8631     = saved_integral_constant_expression_p;
8632   parser->non_integral_constant_expression_p
8633     = saved_non_integral_constant_expression_p;
8634
8635   if (expr == error_mark_node)
8636     {
8637       /* Skip everything up to the closing `)'.  */
8638       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8639                                              /*consume_paren=*/true);
8640       return error_mark_node;
8641     }
8642   
8643   /* Parse to the closing `)'.  */
8644   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8645     {
8646       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8647                                              /*consume_paren=*/true);
8648       return error_mark_node;
8649     }
8650
8651   return finish_decltype_type (expr, id_expression_or_member_access_p);
8652 }
8653
8654 /* Special member functions [gram.special] */
8655
8656 /* Parse a conversion-function-id.
8657
8658    conversion-function-id:
8659      operator conversion-type-id
8660
8661    Returns an IDENTIFIER_NODE representing the operator.  */
8662
8663 static tree
8664 cp_parser_conversion_function_id (cp_parser* parser)
8665 {
8666   tree type;
8667   tree saved_scope;
8668   tree saved_qualifying_scope;
8669   tree saved_object_scope;
8670   tree pushed_scope = NULL_TREE;
8671
8672   /* Look for the `operator' token.  */
8673   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8674     return error_mark_node;
8675   /* When we parse the conversion-type-id, the current scope will be
8676      reset.  However, we need that information in able to look up the
8677      conversion function later, so we save it here.  */
8678   saved_scope = parser->scope;
8679   saved_qualifying_scope = parser->qualifying_scope;
8680   saved_object_scope = parser->object_scope;
8681   /* We must enter the scope of the class so that the names of
8682      entities declared within the class are available in the
8683      conversion-type-id.  For example, consider:
8684
8685        struct S {
8686          typedef int I;
8687          operator I();
8688        };
8689
8690        S::operator I() { ... }
8691
8692      In order to see that `I' is a type-name in the definition, we
8693      must be in the scope of `S'.  */
8694   if (saved_scope)
8695     pushed_scope = push_scope (saved_scope);
8696   /* Parse the conversion-type-id.  */
8697   type = cp_parser_conversion_type_id (parser);
8698   /* Leave the scope of the class, if any.  */
8699   if (pushed_scope)
8700     pop_scope (pushed_scope);
8701   /* Restore the saved scope.  */
8702   parser->scope = saved_scope;
8703   parser->qualifying_scope = saved_qualifying_scope;
8704   parser->object_scope = saved_object_scope;
8705   /* If the TYPE is invalid, indicate failure.  */
8706   if (type == error_mark_node)
8707     return error_mark_node;
8708   return mangle_conv_op_name_for_type (type);
8709 }
8710
8711 /* Parse a conversion-type-id:
8712
8713    conversion-type-id:
8714      type-specifier-seq conversion-declarator [opt]
8715
8716    Returns the TYPE specified.  */
8717
8718 static tree
8719 cp_parser_conversion_type_id (cp_parser* parser)
8720 {
8721   tree attributes;
8722   cp_decl_specifier_seq type_specifiers;
8723   cp_declarator *declarator;
8724   tree type_specified;
8725
8726   /* Parse the attributes.  */
8727   attributes = cp_parser_attributes_opt (parser);
8728   /* Parse the type-specifiers.  */
8729   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8730                                 &type_specifiers);
8731   /* If that didn't work, stop.  */
8732   if (type_specifiers.type == error_mark_node)
8733     return error_mark_node;
8734   /* Parse the conversion-declarator.  */
8735   declarator = cp_parser_conversion_declarator_opt (parser);
8736
8737   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8738                                     /*initialized=*/0, &attributes);
8739   if (attributes)
8740     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8741   return type_specified;
8742 }
8743
8744 /* Parse an (optional) conversion-declarator.
8745
8746    conversion-declarator:
8747      ptr-operator conversion-declarator [opt]
8748
8749    */
8750
8751 static cp_declarator *
8752 cp_parser_conversion_declarator_opt (cp_parser* parser)
8753 {
8754   enum tree_code code;
8755   tree class_type;
8756   cp_cv_quals cv_quals;
8757
8758   /* We don't know if there's a ptr-operator next, or not.  */
8759   cp_parser_parse_tentatively (parser);
8760   /* Try the ptr-operator.  */
8761   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8762   /* If it worked, look for more conversion-declarators.  */
8763   if (cp_parser_parse_definitely (parser))
8764     {
8765       cp_declarator *declarator;
8766
8767       /* Parse another optional declarator.  */
8768       declarator = cp_parser_conversion_declarator_opt (parser);
8769
8770       return cp_parser_make_indirect_declarator
8771         (code, class_type, cv_quals, declarator);
8772    }
8773
8774   return NULL;
8775 }
8776
8777 /* Parse an (optional) ctor-initializer.
8778
8779    ctor-initializer:
8780      : mem-initializer-list
8781
8782    Returns TRUE iff the ctor-initializer was actually present.  */
8783
8784 static bool
8785 cp_parser_ctor_initializer_opt (cp_parser* parser)
8786 {
8787   /* If the next token is not a `:', then there is no
8788      ctor-initializer.  */
8789   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8790     {
8791       /* Do default initialization of any bases and members.  */
8792       if (DECL_CONSTRUCTOR_P (current_function_decl))
8793         finish_mem_initializers (NULL_TREE);
8794
8795       return false;
8796     }
8797
8798   /* Consume the `:' token.  */
8799   cp_lexer_consume_token (parser->lexer);
8800   /* And the mem-initializer-list.  */
8801   cp_parser_mem_initializer_list (parser);
8802
8803   return true;
8804 }
8805
8806 /* Parse a mem-initializer-list.
8807
8808    mem-initializer-list:
8809      mem-initializer ... [opt]
8810      mem-initializer ... [opt] , mem-initializer-list  */
8811
8812 static void
8813 cp_parser_mem_initializer_list (cp_parser* parser)
8814 {
8815   tree mem_initializer_list = NULL_TREE;
8816
8817   /* Let the semantic analysis code know that we are starting the
8818      mem-initializer-list.  */
8819   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8820     error ("only constructors take base initializers");
8821
8822   /* Loop through the list.  */
8823   while (true)
8824     {
8825       tree mem_initializer;
8826
8827       /* Parse the mem-initializer.  */
8828       mem_initializer = cp_parser_mem_initializer (parser);
8829       /* If the next token is a `...', we're expanding member initializers. */
8830       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8831         {
8832           /* Consume the `...'. */
8833           cp_lexer_consume_token (parser->lexer);
8834
8835           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8836              can be expanded but members cannot. */
8837           if (mem_initializer != error_mark_node
8838               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8839             {
8840               error ("cannot expand initializer for member %<%D%>", 
8841                      TREE_PURPOSE (mem_initializer));
8842               mem_initializer = error_mark_node;
8843             }
8844
8845           /* Construct the pack expansion type. */
8846           if (mem_initializer != error_mark_node)
8847             mem_initializer = make_pack_expansion (mem_initializer);
8848         }
8849       /* Add it to the list, unless it was erroneous.  */
8850       if (mem_initializer != error_mark_node)
8851         {
8852           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8853           mem_initializer_list = mem_initializer;
8854         }
8855       /* If the next token is not a `,', we're done.  */
8856       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8857         break;
8858       /* Consume the `,' token.  */
8859       cp_lexer_consume_token (parser->lexer);
8860     }
8861
8862   /* Perform semantic analysis.  */
8863   if (DECL_CONSTRUCTOR_P (current_function_decl))
8864     finish_mem_initializers (mem_initializer_list);
8865 }
8866
8867 /* Parse a mem-initializer.
8868
8869    mem-initializer:
8870      mem-initializer-id ( expression-list [opt] )
8871
8872    GNU extension:
8873
8874    mem-initializer:
8875      ( expression-list [opt] )
8876
8877    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8878    class) or FIELD_DECL (for a non-static data member) to initialize;
8879    the TREE_VALUE is the expression-list.  An empty initialization
8880    list is represented by void_list_node.  */
8881
8882 static tree
8883 cp_parser_mem_initializer (cp_parser* parser)
8884 {
8885   tree mem_initializer_id;
8886   tree expression_list;
8887   tree member;
8888
8889   /* Find out what is being initialized.  */
8890   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8891     {
8892       pedwarn ("anachronistic old-style base class initializer");
8893       mem_initializer_id = NULL_TREE;
8894     }
8895   else
8896     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8897   member = expand_member_init (mem_initializer_id);
8898   if (member && !DECL_P (member))
8899     in_base_initializer = 1;
8900
8901   expression_list
8902     = cp_parser_parenthesized_expression_list (parser, false,
8903                                                /*cast_p=*/false,
8904                                                /*allow_expansion_p=*/true,
8905                                                /*non_constant_p=*/NULL);
8906   if (expression_list == error_mark_node)
8907     return error_mark_node;
8908   if (!expression_list)
8909     expression_list = void_type_node;
8910
8911   in_base_initializer = 0;
8912
8913   return member ? build_tree_list (member, expression_list) : error_mark_node;
8914 }
8915
8916 /* Parse a mem-initializer-id.
8917
8918    mem-initializer-id:
8919      :: [opt] nested-name-specifier [opt] class-name
8920      identifier
8921
8922    Returns a TYPE indicating the class to be initializer for the first
8923    production.  Returns an IDENTIFIER_NODE indicating the data member
8924    to be initialized for the second production.  */
8925
8926 static tree
8927 cp_parser_mem_initializer_id (cp_parser* parser)
8928 {
8929   bool global_scope_p;
8930   bool nested_name_specifier_p;
8931   bool template_p = false;
8932   tree id;
8933
8934   /* `typename' is not allowed in this context ([temp.res]).  */
8935   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8936     {
8937       error ("keyword %<typename%> not allowed in this context (a qualified "
8938              "member initializer is implicitly a type)");
8939       cp_lexer_consume_token (parser->lexer);
8940     }
8941   /* Look for the optional `::' operator.  */
8942   global_scope_p
8943     = (cp_parser_global_scope_opt (parser,
8944                                    /*current_scope_valid_p=*/false)
8945        != NULL_TREE);
8946   /* Look for the optional nested-name-specifier.  The simplest way to
8947      implement:
8948
8949        [temp.res]
8950
8951        The keyword `typename' is not permitted in a base-specifier or
8952        mem-initializer; in these contexts a qualified name that
8953        depends on a template-parameter is implicitly assumed to be a
8954        type name.
8955
8956      is to assume that we have seen the `typename' keyword at this
8957      point.  */
8958   nested_name_specifier_p
8959     = (cp_parser_nested_name_specifier_opt (parser,
8960                                             /*typename_keyword_p=*/true,
8961                                             /*check_dependency_p=*/true,
8962                                             /*type_p=*/true,
8963                                             /*is_declaration=*/true)
8964        != NULL_TREE);
8965   if (nested_name_specifier_p)
8966     template_p = cp_parser_optional_template_keyword (parser);
8967   /* If there is a `::' operator or a nested-name-specifier, then we
8968      are definitely looking for a class-name.  */
8969   if (global_scope_p || nested_name_specifier_p)
8970     return cp_parser_class_name (parser,
8971                                  /*typename_keyword_p=*/true,
8972                                  /*template_keyword_p=*/template_p,
8973                                  none_type,
8974                                  /*check_dependency_p=*/true,
8975                                  /*class_head_p=*/false,
8976                                  /*is_declaration=*/true);
8977   /* Otherwise, we could also be looking for an ordinary identifier.  */
8978   cp_parser_parse_tentatively (parser);
8979   /* Try a class-name.  */
8980   id = cp_parser_class_name (parser,
8981                              /*typename_keyword_p=*/true,
8982                              /*template_keyword_p=*/false,
8983                              none_type,
8984                              /*check_dependency_p=*/true,
8985                              /*class_head_p=*/false,
8986                              /*is_declaration=*/true);
8987   /* If we found one, we're done.  */
8988   if (cp_parser_parse_definitely (parser))
8989     return id;
8990   /* Otherwise, look for an ordinary identifier.  */
8991   return cp_parser_identifier (parser);
8992 }
8993
8994 /* Overloading [gram.over] */
8995
8996 /* Parse an operator-function-id.
8997
8998    operator-function-id:
8999      operator operator
9000
9001    Returns an IDENTIFIER_NODE for the operator which is a
9002    human-readable spelling of the identifier, e.g., `operator +'.  */
9003
9004 static tree
9005 cp_parser_operator_function_id (cp_parser* parser)
9006 {
9007   /* Look for the `operator' keyword.  */
9008   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9009     return error_mark_node;
9010   /* And then the name of the operator itself.  */
9011   return cp_parser_operator (parser);
9012 }
9013
9014 /* Parse an operator.
9015
9016    operator:
9017      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9018      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9019      || ++ -- , ->* -> () []
9020
9021    GNU Extensions:
9022
9023    operator:
9024      <? >? <?= >?=
9025
9026    Returns an IDENTIFIER_NODE for the operator which is a
9027    human-readable spelling of the identifier, e.g., `operator +'.  */
9028
9029 static tree
9030 cp_parser_operator (cp_parser* parser)
9031 {
9032   tree id = NULL_TREE;
9033   cp_token *token;
9034
9035   /* Peek at the next token.  */
9036   token = cp_lexer_peek_token (parser->lexer);
9037   /* Figure out which operator we have.  */
9038   switch (token->type)
9039     {
9040     case CPP_KEYWORD:
9041       {
9042         enum tree_code op;
9043
9044         /* The keyword should be either `new' or `delete'.  */
9045         if (token->keyword == RID_NEW)
9046           op = NEW_EXPR;
9047         else if (token->keyword == RID_DELETE)
9048           op = DELETE_EXPR;
9049         else
9050           break;
9051
9052         /* Consume the `new' or `delete' token.  */
9053         cp_lexer_consume_token (parser->lexer);
9054
9055         /* Peek at the next token.  */
9056         token = cp_lexer_peek_token (parser->lexer);
9057         /* If it's a `[' token then this is the array variant of the
9058            operator.  */
9059         if (token->type == CPP_OPEN_SQUARE)
9060           {
9061             /* Consume the `[' token.  */
9062             cp_lexer_consume_token (parser->lexer);
9063             /* Look for the `]' token.  */
9064             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9065             id = ansi_opname (op == NEW_EXPR
9066                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9067           }
9068         /* Otherwise, we have the non-array variant.  */
9069         else
9070           id = ansi_opname (op);
9071
9072         return id;
9073       }
9074
9075     case CPP_PLUS:
9076       id = ansi_opname (PLUS_EXPR);
9077       break;
9078
9079     case CPP_MINUS:
9080       id = ansi_opname (MINUS_EXPR);
9081       break;
9082
9083     case CPP_MULT:
9084       id = ansi_opname (MULT_EXPR);
9085       break;
9086
9087     case CPP_DIV:
9088       id = ansi_opname (TRUNC_DIV_EXPR);
9089       break;
9090
9091     case CPP_MOD:
9092       id = ansi_opname (TRUNC_MOD_EXPR);
9093       break;
9094
9095     case CPP_XOR:
9096       id = ansi_opname (BIT_XOR_EXPR);
9097       break;
9098
9099     case CPP_AND:
9100       id = ansi_opname (BIT_AND_EXPR);
9101       break;
9102
9103     case CPP_OR:
9104       id = ansi_opname (BIT_IOR_EXPR);
9105       break;
9106
9107     case CPP_COMPL:
9108       id = ansi_opname (BIT_NOT_EXPR);
9109       break;
9110
9111     case CPP_NOT:
9112       id = ansi_opname (TRUTH_NOT_EXPR);
9113       break;
9114
9115     case CPP_EQ:
9116       id = ansi_assopname (NOP_EXPR);
9117       break;
9118
9119     case CPP_LESS:
9120       id = ansi_opname (LT_EXPR);
9121       break;
9122
9123     case CPP_GREATER:
9124       id = ansi_opname (GT_EXPR);
9125       break;
9126
9127     case CPP_PLUS_EQ:
9128       id = ansi_assopname (PLUS_EXPR);
9129       break;
9130
9131     case CPP_MINUS_EQ:
9132       id = ansi_assopname (MINUS_EXPR);
9133       break;
9134
9135     case CPP_MULT_EQ:
9136       id = ansi_assopname (MULT_EXPR);
9137       break;
9138
9139     case CPP_DIV_EQ:
9140       id = ansi_assopname (TRUNC_DIV_EXPR);
9141       break;
9142
9143     case CPP_MOD_EQ:
9144       id = ansi_assopname (TRUNC_MOD_EXPR);
9145       break;
9146
9147     case CPP_XOR_EQ:
9148       id = ansi_assopname (BIT_XOR_EXPR);
9149       break;
9150
9151     case CPP_AND_EQ:
9152       id = ansi_assopname (BIT_AND_EXPR);
9153       break;
9154
9155     case CPP_OR_EQ:
9156       id = ansi_assopname (BIT_IOR_EXPR);
9157       break;
9158
9159     case CPP_LSHIFT:
9160       id = ansi_opname (LSHIFT_EXPR);
9161       break;
9162
9163     case CPP_RSHIFT:
9164       id = ansi_opname (RSHIFT_EXPR);
9165       break;
9166
9167     case CPP_LSHIFT_EQ:
9168       id = ansi_assopname (LSHIFT_EXPR);
9169       break;
9170
9171     case CPP_RSHIFT_EQ:
9172       id = ansi_assopname (RSHIFT_EXPR);
9173       break;
9174
9175     case CPP_EQ_EQ:
9176       id = ansi_opname (EQ_EXPR);
9177       break;
9178
9179     case CPP_NOT_EQ:
9180       id = ansi_opname (NE_EXPR);
9181       break;
9182
9183     case CPP_LESS_EQ:
9184       id = ansi_opname (LE_EXPR);
9185       break;
9186
9187     case CPP_GREATER_EQ:
9188       id = ansi_opname (GE_EXPR);
9189       break;
9190
9191     case CPP_AND_AND:
9192       id = ansi_opname (TRUTH_ANDIF_EXPR);
9193       break;
9194
9195     case CPP_OR_OR:
9196       id = ansi_opname (TRUTH_ORIF_EXPR);
9197       break;
9198
9199     case CPP_PLUS_PLUS:
9200       id = ansi_opname (POSTINCREMENT_EXPR);
9201       break;
9202
9203     case CPP_MINUS_MINUS:
9204       id = ansi_opname (PREDECREMENT_EXPR);
9205       break;
9206
9207     case CPP_COMMA:
9208       id = ansi_opname (COMPOUND_EXPR);
9209       break;
9210
9211     case CPP_DEREF_STAR:
9212       id = ansi_opname (MEMBER_REF);
9213       break;
9214
9215     case CPP_DEREF:
9216       id = ansi_opname (COMPONENT_REF);
9217       break;
9218
9219     case CPP_OPEN_PAREN:
9220       /* Consume the `('.  */
9221       cp_lexer_consume_token (parser->lexer);
9222       /* Look for the matching `)'.  */
9223       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9224       return ansi_opname (CALL_EXPR);
9225
9226     case CPP_OPEN_SQUARE:
9227       /* Consume the `['.  */
9228       cp_lexer_consume_token (parser->lexer);
9229       /* Look for the matching `]'.  */
9230       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9231       return ansi_opname (ARRAY_REF);
9232
9233     default:
9234       /* Anything else is an error.  */
9235       break;
9236     }
9237
9238   /* If we have selected an identifier, we need to consume the
9239      operator token.  */
9240   if (id)
9241     cp_lexer_consume_token (parser->lexer);
9242   /* Otherwise, no valid operator name was present.  */
9243   else
9244     {
9245       cp_parser_error (parser, "expected operator");
9246       id = error_mark_node;
9247     }
9248
9249   return id;
9250 }
9251
9252 /* Parse a template-declaration.
9253
9254    template-declaration:
9255      export [opt] template < template-parameter-list > declaration
9256
9257    If MEMBER_P is TRUE, this template-declaration occurs within a
9258    class-specifier.
9259
9260    The grammar rule given by the standard isn't correct.  What
9261    is really meant is:
9262
9263    template-declaration:
9264      export [opt] template-parameter-list-seq
9265        decl-specifier-seq [opt] init-declarator [opt] ;
9266      export [opt] template-parameter-list-seq
9267        function-definition
9268
9269    template-parameter-list-seq:
9270      template-parameter-list-seq [opt]
9271      template < template-parameter-list >  */
9272
9273 static void
9274 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9275 {
9276   /* Check for `export'.  */
9277   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9278     {
9279       /* Consume the `export' token.  */
9280       cp_lexer_consume_token (parser->lexer);
9281       /* Warn that we do not support `export'.  */
9282       warning (0, "keyword %<export%> not implemented, and will be ignored");
9283     }
9284
9285   cp_parser_template_declaration_after_export (parser, member_p);
9286 }
9287
9288 /* Parse a template-parameter-list.
9289
9290    template-parameter-list:
9291      template-parameter
9292      template-parameter-list , template-parameter
9293
9294    Returns a TREE_LIST.  Each node represents a template parameter.
9295    The nodes are connected via their TREE_CHAINs.  */
9296
9297 static tree
9298 cp_parser_template_parameter_list (cp_parser* parser)
9299 {
9300   tree parameter_list = NULL_TREE;
9301
9302   begin_template_parm_list ();
9303   while (true)
9304     {
9305       tree parameter;
9306       bool is_non_type;
9307       bool is_parameter_pack;
9308
9309       /* Parse the template-parameter.  */
9310       parameter = cp_parser_template_parameter (parser, 
9311                                                 &is_non_type,
9312                                                 &is_parameter_pack);
9313       /* Add it to the list.  */
9314       if (parameter != error_mark_node)
9315         parameter_list = process_template_parm (parameter_list,
9316                                                 parameter,
9317                                                 is_non_type,
9318                                                 is_parameter_pack);
9319       else
9320        {
9321          tree err_parm = build_tree_list (parameter, parameter);
9322          TREE_VALUE (err_parm) = error_mark_node;
9323          parameter_list = chainon (parameter_list, err_parm);
9324        }
9325
9326       /* If the next token is not a `,', we're done.  */
9327       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9328         break;
9329       /* Otherwise, consume the `,' token.  */
9330       cp_lexer_consume_token (parser->lexer);
9331     }
9332
9333   return end_template_parm_list (parameter_list);
9334 }
9335
9336 /* Parse a template-parameter.
9337
9338    template-parameter:
9339      type-parameter
9340      parameter-declaration
9341
9342    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9343    the parameter.  The TREE_PURPOSE is the default value, if any.
9344    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9345    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9346    set to true iff this parameter is a parameter pack. */
9347
9348 static tree
9349 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9350                               bool *is_parameter_pack)
9351 {
9352   cp_token *token;
9353   cp_parameter_declarator *parameter_declarator;
9354   cp_declarator *id_declarator;
9355   tree parm;
9356
9357   /* Assume it is a type parameter or a template parameter.  */
9358   *is_non_type = false;
9359   /* Assume it not a parameter pack. */
9360   *is_parameter_pack = false;
9361   /* Peek at the next token.  */
9362   token = cp_lexer_peek_token (parser->lexer);
9363   /* If it is `class' or `template', we have a type-parameter.  */
9364   if (token->keyword == RID_TEMPLATE)
9365     return cp_parser_type_parameter (parser, is_parameter_pack);
9366   /* If it is `class' or `typename' we do not know yet whether it is a
9367      type parameter or a non-type parameter.  Consider:
9368
9369        template <typename T, typename T::X X> ...
9370
9371      or:
9372
9373        template <class C, class D*> ...
9374
9375      Here, the first parameter is a type parameter, and the second is
9376      a non-type parameter.  We can tell by looking at the token after
9377      the identifier -- if it is a `,', `=', or `>' then we have a type
9378      parameter.  */
9379   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9380     {
9381       /* Peek at the token after `class' or `typename'.  */
9382       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9383       /* If it's an ellipsis, we have a template type parameter
9384          pack. */
9385       if (token->type == CPP_ELLIPSIS)
9386         return cp_parser_type_parameter (parser, is_parameter_pack);
9387       /* If it's an identifier, skip it.  */
9388       if (token->type == CPP_NAME)
9389         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9390       /* Now, see if the token looks like the end of a template
9391          parameter.  */
9392       if (token->type == CPP_COMMA
9393           || token->type == CPP_EQ
9394           || token->type == CPP_GREATER)
9395         return cp_parser_type_parameter (parser, is_parameter_pack);
9396     }
9397
9398   /* Otherwise, it is a non-type parameter.
9399
9400      [temp.param]
9401
9402      When parsing a default template-argument for a non-type
9403      template-parameter, the first non-nested `>' is taken as the end
9404      of the template parameter-list rather than a greater-than
9405      operator.  */
9406   *is_non_type = true;
9407   parameter_declarator
9408      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9409                                         /*parenthesized_p=*/NULL);
9410
9411   /* If the parameter declaration is marked as a parameter pack, set
9412      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9413      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9414      grokdeclarator. */
9415   if (parameter_declarator
9416       && parameter_declarator->declarator
9417       && parameter_declarator->declarator->parameter_pack_p)
9418     {
9419       *is_parameter_pack = true;
9420       parameter_declarator->declarator->parameter_pack_p = false;
9421     }
9422
9423   /* If the next token is an ellipsis, and we don't already have it
9424      marked as a parameter pack, then we have a parameter pack (that
9425      has no declarator).  */
9426   if (!*is_parameter_pack
9427       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9428       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9429     {
9430       /* Consume the `...'.  */
9431       cp_lexer_consume_token (parser->lexer);
9432       maybe_warn_variadic_templates ();
9433       
9434       *is_parameter_pack = true;
9435     }
9436   /* We might end up with a pack expansion as the type of the non-type
9437      template parameter, in which case this is a non-type template
9438      parameter pack.  */
9439   else if (parameter_declarator
9440            && parameter_declarator->decl_specifiers.type
9441            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9442     {
9443       *is_parameter_pack = true;
9444       parameter_declarator->decl_specifiers.type = 
9445         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9446     }
9447
9448   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9449     {
9450       /* Parameter packs cannot have default arguments.  However, a
9451          user may try to do so, so we'll parse them and give an
9452          appropriate diagnostic here.  */
9453
9454       /* Consume the `='.  */
9455       cp_lexer_consume_token (parser->lexer);
9456       
9457       /* Find the name of the parameter pack.  */     
9458       id_declarator = parameter_declarator->declarator;
9459       while (id_declarator && id_declarator->kind != cdk_id)
9460         id_declarator = id_declarator->declarator;
9461       
9462       if (id_declarator && id_declarator->kind == cdk_id)
9463         error ("template parameter pack %qD cannot have a default argument",
9464                id_declarator->u.id.unqualified_name);
9465       else
9466         error ("template parameter pack cannot have a default argument");
9467       
9468       /* Parse the default argument, but throw away the result.  */
9469       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9470     }
9471
9472   parm = grokdeclarator (parameter_declarator->declarator,
9473                          &parameter_declarator->decl_specifiers,
9474                          PARM, /*initialized=*/0,
9475                          /*attrlist=*/NULL);
9476   if (parm == error_mark_node)
9477     return error_mark_node;
9478
9479   return build_tree_list (parameter_declarator->default_argument, parm);
9480 }
9481
9482 /* Parse a type-parameter.
9483
9484    type-parameter:
9485      class identifier [opt]
9486      class identifier [opt] = type-id
9487      typename identifier [opt]
9488      typename identifier [opt] = type-id
9489      template < template-parameter-list > class identifier [opt]
9490      template < template-parameter-list > class identifier [opt]
9491        = id-expression
9492
9493    GNU Extension (variadic templates):
9494
9495    type-parameter:
9496      class ... identifier [opt]
9497      typename ... identifier [opt]
9498
9499    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9500    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9501    the declaration of the parameter.
9502
9503    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9504
9505 static tree
9506 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9507 {
9508   cp_token *token;
9509   tree parameter;
9510
9511   /* Look for a keyword to tell us what kind of parameter this is.  */
9512   token = cp_parser_require (parser, CPP_KEYWORD,
9513                              "%<class%>, %<typename%>, or %<template%>");
9514   if (!token)
9515     return error_mark_node;
9516
9517   switch (token->keyword)
9518     {
9519     case RID_CLASS:
9520     case RID_TYPENAME:
9521       {
9522         tree identifier;
9523         tree default_argument;
9524
9525         /* If the next token is an ellipsis, we have a template
9526            argument pack. */
9527         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9528           {
9529             /* Consume the `...' token. */
9530             cp_lexer_consume_token (parser->lexer);
9531             maybe_warn_variadic_templates ();
9532
9533             *is_parameter_pack = true;
9534           }
9535
9536         /* If the next token is an identifier, then it names the
9537            parameter.  */
9538         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9539           identifier = cp_parser_identifier (parser);
9540         else
9541           identifier = NULL_TREE;
9542
9543         /* Create the parameter.  */
9544         parameter = finish_template_type_parm (class_type_node, identifier);
9545
9546         /* If the next token is an `=', we have a default argument.  */
9547         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9548           {
9549             /* Consume the `=' token.  */
9550             cp_lexer_consume_token (parser->lexer);
9551             /* Parse the default-argument.  */
9552             push_deferring_access_checks (dk_no_deferred);
9553             default_argument = cp_parser_type_id (parser);
9554
9555             /* Template parameter packs cannot have default
9556                arguments. */
9557             if (*is_parameter_pack)
9558               {
9559                 if (identifier)
9560                   error ("template parameter pack %qD cannot have a default argument", 
9561                          identifier);
9562                 else
9563                   error ("template parameter packs cannot have default arguments");
9564                 default_argument = NULL_TREE;
9565               }
9566             pop_deferring_access_checks ();
9567           }
9568         else
9569           default_argument = NULL_TREE;
9570
9571         /* Create the combined representation of the parameter and the
9572            default argument.  */
9573         parameter = build_tree_list (default_argument, parameter);
9574       }
9575       break;
9576
9577     case RID_TEMPLATE:
9578       {
9579         tree parameter_list;
9580         tree identifier;
9581         tree default_argument;
9582
9583         /* Look for the `<'.  */
9584         cp_parser_require (parser, CPP_LESS, "%<<%>");
9585         /* Parse the template-parameter-list.  */
9586         parameter_list = cp_parser_template_parameter_list (parser);
9587         /* Look for the `>'.  */
9588         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9589         /* Look for the `class' keyword.  */
9590         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9591         /* If the next token is an ellipsis, we have a template
9592            argument pack. */
9593         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9594           {
9595             /* Consume the `...' token. */
9596             cp_lexer_consume_token (parser->lexer);
9597             maybe_warn_variadic_templates ();
9598
9599             *is_parameter_pack = true;
9600           }
9601         /* If the next token is an `=', then there is a
9602            default-argument.  If the next token is a `>', we are at
9603            the end of the parameter-list.  If the next token is a `,',
9604            then we are at the end of this parameter.  */
9605         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9606             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9607             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9608           {
9609             identifier = cp_parser_identifier (parser);
9610             /* Treat invalid names as if the parameter were nameless.  */
9611             if (identifier == error_mark_node)
9612               identifier = NULL_TREE;
9613           }
9614         else
9615           identifier = NULL_TREE;
9616
9617         /* Create the template parameter.  */
9618         parameter = finish_template_template_parm (class_type_node,
9619                                                    identifier);
9620
9621         /* If the next token is an `=', then there is a
9622            default-argument.  */
9623         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9624           {
9625             bool is_template;
9626
9627             /* Consume the `='.  */
9628             cp_lexer_consume_token (parser->lexer);
9629             /* Parse the id-expression.  */
9630             push_deferring_access_checks (dk_no_deferred);
9631             default_argument
9632               = cp_parser_id_expression (parser,
9633                                          /*template_keyword_p=*/false,
9634                                          /*check_dependency_p=*/true,
9635                                          /*template_p=*/&is_template,
9636                                          /*declarator_p=*/false,
9637                                          /*optional_p=*/false);
9638             if (TREE_CODE (default_argument) == TYPE_DECL)
9639               /* If the id-expression was a template-id that refers to
9640                  a template-class, we already have the declaration here,
9641                  so no further lookup is needed.  */
9642                  ;
9643             else
9644               /* Look up the name.  */
9645               default_argument
9646                 = cp_parser_lookup_name (parser, default_argument,
9647                                          none_type,
9648                                          /*is_template=*/is_template,
9649                                          /*is_namespace=*/false,
9650                                          /*check_dependency=*/true,
9651                                          /*ambiguous_decls=*/NULL);
9652             /* See if the default argument is valid.  */
9653             default_argument
9654               = check_template_template_default_arg (default_argument);
9655
9656             /* Template parameter packs cannot have default
9657                arguments. */
9658             if (*is_parameter_pack)
9659               {
9660                 if (identifier)
9661                   error ("template parameter pack %qD cannot have a default argument", 
9662                          identifier);
9663                 else
9664                   error ("template parameter packs cannot have default arguments");
9665                 default_argument = NULL_TREE;
9666               }
9667             pop_deferring_access_checks ();
9668           }
9669         else
9670           default_argument = NULL_TREE;
9671
9672         /* Create the combined representation of the parameter and the
9673            default argument.  */
9674         parameter = build_tree_list (default_argument, parameter);
9675       }
9676       break;
9677
9678     default:
9679       gcc_unreachable ();
9680       break;
9681     }
9682
9683   return parameter;
9684 }
9685
9686 /* Parse a template-id.
9687
9688    template-id:
9689      template-name < template-argument-list [opt] >
9690
9691    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9692    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9693    returned.  Otherwise, if the template-name names a function, or set
9694    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9695    names a class, returns a TYPE_DECL for the specialization.
9696
9697    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9698    uninstantiated templates.  */
9699
9700 static tree
9701 cp_parser_template_id (cp_parser *parser,
9702                        bool template_keyword_p,
9703                        bool check_dependency_p,
9704                        bool is_declaration)
9705 {
9706   int i;
9707   tree template;
9708   tree arguments;
9709   tree template_id;
9710   cp_token_position start_of_id = 0;
9711   deferred_access_check *chk;
9712   VEC (deferred_access_check,gc) *access_check;
9713   cp_token *next_token, *next_token_2;
9714   bool is_identifier;
9715
9716   /* If the next token corresponds to a template-id, there is no need
9717      to reparse it.  */
9718   next_token = cp_lexer_peek_token (parser->lexer);
9719   if (next_token->type == CPP_TEMPLATE_ID)
9720     {
9721       struct tree_check *check_value;
9722
9723       /* Get the stored value.  */
9724       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9725       /* Perform any access checks that were deferred.  */
9726       access_check = check_value->checks;
9727       if (access_check)
9728         {
9729           for (i = 0 ;
9730                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9731                ++i)
9732             {
9733               perform_or_defer_access_check (chk->binfo,
9734                                              chk->decl,
9735                                              chk->diag_decl);
9736             }
9737         }
9738       /* Return the stored value.  */
9739       return check_value->value;
9740     }
9741
9742   /* Avoid performing name lookup if there is no possibility of
9743      finding a template-id.  */
9744   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9745       || (next_token->type == CPP_NAME
9746           && !cp_parser_nth_token_starts_template_argument_list_p
9747                (parser, 2)))
9748     {
9749       cp_parser_error (parser, "expected template-id");
9750       return error_mark_node;
9751     }
9752
9753   /* Remember where the template-id starts.  */
9754   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9755     start_of_id = cp_lexer_token_position (parser->lexer, false);
9756
9757   push_deferring_access_checks (dk_deferred);
9758
9759   /* Parse the template-name.  */
9760   is_identifier = false;
9761   template = cp_parser_template_name (parser, template_keyword_p,
9762                                       check_dependency_p,
9763                                       is_declaration,
9764                                       &is_identifier);
9765   if (template == error_mark_node || is_identifier)
9766     {
9767       pop_deferring_access_checks ();
9768       return template;
9769     }
9770
9771   /* If we find the sequence `[:' after a template-name, it's probably
9772      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9773      parse correctly the argument list.  */
9774   next_token = cp_lexer_peek_token (parser->lexer);
9775   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9776   if (next_token->type == CPP_OPEN_SQUARE
9777       && next_token->flags & DIGRAPH
9778       && next_token_2->type == CPP_COLON
9779       && !(next_token_2->flags & PREV_WHITE))
9780     {
9781       cp_parser_parse_tentatively (parser);
9782       /* Change `:' into `::'.  */
9783       next_token_2->type = CPP_SCOPE;
9784       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9785          CPP_LESS.  */
9786       cp_lexer_consume_token (parser->lexer);
9787       /* Parse the arguments.  */
9788       arguments = cp_parser_enclosed_template_argument_list (parser);
9789       if (!cp_parser_parse_definitely (parser))
9790         {
9791           /* If we couldn't parse an argument list, then we revert our changes
9792              and return simply an error. Maybe this is not a template-id
9793              after all.  */
9794           next_token_2->type = CPP_COLON;
9795           cp_parser_error (parser, "expected %<<%>");
9796           pop_deferring_access_checks ();
9797           return error_mark_node;
9798         }
9799       /* Otherwise, emit an error about the invalid digraph, but continue
9800          parsing because we got our argument list.  */
9801       permerror ("%<<::%> cannot begin a template-argument list");
9802       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9803               "between %<<%> and %<::%>");
9804       if (!flag_permissive)
9805         {
9806           static bool hint;
9807           if (!hint)
9808             {
9809               inform ("(if you use -fpermissive G++ will accept your code)");
9810               hint = true;
9811             }
9812         }
9813     }
9814   else
9815     {
9816       /* Look for the `<' that starts the template-argument-list.  */
9817       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
9818         {
9819           pop_deferring_access_checks ();
9820           return error_mark_node;
9821         }
9822       /* Parse the arguments.  */
9823       arguments = cp_parser_enclosed_template_argument_list (parser);
9824     }
9825
9826   /* Build a representation of the specialization.  */
9827   if (TREE_CODE (template) == IDENTIFIER_NODE)
9828     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9829   else if (DECL_CLASS_TEMPLATE_P (template)
9830            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9831     {
9832       bool entering_scope;
9833       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9834          template (rather than some instantiation thereof) only if
9835          is not nested within some other construct.  For example, in
9836          "template <typename T> void f(T) { A<T>::", A<T> is just an
9837          instantiation of A.  */
9838       entering_scope = (template_parm_scope_p ()
9839                         && cp_lexer_next_token_is (parser->lexer,
9840                                                    CPP_SCOPE));
9841       template_id
9842         = finish_template_type (template, arguments, entering_scope);
9843     }
9844   else
9845     {
9846       /* If it's not a class-template or a template-template, it should be
9847          a function-template.  */
9848       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9849                    || TREE_CODE (template) == OVERLOAD
9850                    || BASELINK_P (template)));
9851
9852       template_id = lookup_template_function (template, arguments);
9853     }
9854
9855   /* If parsing tentatively, replace the sequence of tokens that makes
9856      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9857      should we re-parse the token stream, we will not have to repeat
9858      the effort required to do the parse, nor will we issue duplicate
9859      error messages about problems during instantiation of the
9860      template.  */
9861   if (start_of_id)
9862     {
9863       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9864
9865       /* Reset the contents of the START_OF_ID token.  */
9866       token->type = CPP_TEMPLATE_ID;
9867       /* Retrieve any deferred checks.  Do not pop this access checks yet
9868          so the memory will not be reclaimed during token replacing below.  */
9869       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9870       token->u.tree_check_value->value = template_id;
9871       token->u.tree_check_value->checks = get_deferred_access_checks ();
9872       token->keyword = RID_MAX;
9873
9874       /* Purge all subsequent tokens.  */
9875       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9876
9877       /* ??? Can we actually assume that, if template_id ==
9878          error_mark_node, we will have issued a diagnostic to the
9879          user, as opposed to simply marking the tentative parse as
9880          failed?  */
9881       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9882         error ("parse error in template argument list");
9883     }
9884
9885   pop_deferring_access_checks ();
9886   return template_id;
9887 }
9888
9889 /* Parse a template-name.
9890
9891    template-name:
9892      identifier
9893
9894    The standard should actually say:
9895
9896    template-name:
9897      identifier
9898      operator-function-id
9899
9900    A defect report has been filed about this issue.
9901
9902    A conversion-function-id cannot be a template name because they cannot
9903    be part of a template-id. In fact, looking at this code:
9904
9905    a.operator K<int>()
9906
9907    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9908    It is impossible to call a templated conversion-function-id with an
9909    explicit argument list, since the only allowed template parameter is
9910    the type to which it is converting.
9911
9912    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9913    `template' keyword, in a construction like:
9914
9915      T::template f<3>()
9916
9917    In that case `f' is taken to be a template-name, even though there
9918    is no way of knowing for sure.
9919
9920    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9921    name refers to a set of overloaded functions, at least one of which
9922    is a template, or an IDENTIFIER_NODE with the name of the template,
9923    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9924    names are looked up inside uninstantiated templates.  */
9925
9926 static tree
9927 cp_parser_template_name (cp_parser* parser,
9928                          bool template_keyword_p,
9929                          bool check_dependency_p,
9930                          bool is_declaration,
9931                          bool *is_identifier)
9932 {
9933   tree identifier;
9934   tree decl;
9935   tree fns;
9936
9937   /* If the next token is `operator', then we have either an
9938      operator-function-id or a conversion-function-id.  */
9939   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9940     {
9941       /* We don't know whether we're looking at an
9942          operator-function-id or a conversion-function-id.  */
9943       cp_parser_parse_tentatively (parser);
9944       /* Try an operator-function-id.  */
9945       identifier = cp_parser_operator_function_id (parser);
9946       /* If that didn't work, try a conversion-function-id.  */
9947       if (!cp_parser_parse_definitely (parser))
9948         {
9949           cp_parser_error (parser, "expected template-name");
9950           return error_mark_node;
9951         }
9952     }
9953   /* Look for the identifier.  */
9954   else
9955     identifier = cp_parser_identifier (parser);
9956
9957   /* If we didn't find an identifier, we don't have a template-id.  */
9958   if (identifier == error_mark_node)
9959     return error_mark_node;
9960
9961   /* If the name immediately followed the `template' keyword, then it
9962      is a template-name.  However, if the next token is not `<', then
9963      we do not treat it as a template-name, since it is not being used
9964      as part of a template-id.  This enables us to handle constructs
9965      like:
9966
9967        template <typename T> struct S { S(); };
9968        template <typename T> S<T>::S();
9969
9970      correctly.  We would treat `S' as a template -- if it were `S<T>'
9971      -- but we do not if there is no `<'.  */
9972
9973   if (processing_template_decl
9974       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9975     {
9976       /* In a declaration, in a dependent context, we pretend that the
9977          "template" keyword was present in order to improve error
9978          recovery.  For example, given:
9979
9980            template <typename T> void f(T::X<int>);
9981
9982          we want to treat "X<int>" as a template-id.  */
9983       if (is_declaration
9984           && !template_keyword_p
9985           && parser->scope && TYPE_P (parser->scope)
9986           && check_dependency_p
9987           && dependent_type_p (parser->scope)
9988           /* Do not do this for dtors (or ctors), since they never
9989              need the template keyword before their name.  */
9990           && !constructor_name_p (identifier, parser->scope))
9991         {
9992           cp_token_position start = 0;
9993
9994           /* Explain what went wrong.  */
9995           error ("non-template %qD used as template", identifier);
9996           inform ("use %<%T::template %D%> to indicate that it is a template",
9997                   parser->scope, identifier);
9998           /* If parsing tentatively, find the location of the "<" token.  */
9999           if (cp_parser_simulate_error (parser))
10000             start = cp_lexer_token_position (parser->lexer, true);
10001           /* Parse the template arguments so that we can issue error
10002              messages about them.  */
10003           cp_lexer_consume_token (parser->lexer);
10004           cp_parser_enclosed_template_argument_list (parser);
10005           /* Skip tokens until we find a good place from which to
10006              continue parsing.  */
10007           cp_parser_skip_to_closing_parenthesis (parser,
10008                                                  /*recovering=*/true,
10009                                                  /*or_comma=*/true,
10010                                                  /*consume_paren=*/false);
10011           /* If parsing tentatively, permanently remove the
10012              template argument list.  That will prevent duplicate
10013              error messages from being issued about the missing
10014              "template" keyword.  */
10015           if (start)
10016             cp_lexer_purge_tokens_after (parser->lexer, start);
10017           if (is_identifier)
10018             *is_identifier = true;
10019           return identifier;
10020         }
10021
10022       /* If the "template" keyword is present, then there is generally
10023          no point in doing name-lookup, so we just return IDENTIFIER.
10024          But, if the qualifying scope is non-dependent then we can
10025          (and must) do name-lookup normally.  */
10026       if (template_keyword_p
10027           && (!parser->scope
10028               || (TYPE_P (parser->scope)
10029                   && dependent_type_p (parser->scope))))
10030         return identifier;
10031     }
10032
10033   /* Look up the name.  */
10034   decl = cp_parser_lookup_name (parser, identifier,
10035                                 none_type,
10036                                 /*is_template=*/false,
10037                                 /*is_namespace=*/false,
10038                                 check_dependency_p,
10039                                 /*ambiguous_decls=*/NULL);
10040   decl = maybe_get_template_decl_from_type_decl (decl);
10041
10042   /* If DECL is a template, then the name was a template-name.  */
10043   if (TREE_CODE (decl) == TEMPLATE_DECL)
10044     ;
10045   else
10046     {
10047       tree fn = NULL_TREE;
10048
10049       /* The standard does not explicitly indicate whether a name that
10050          names a set of overloaded declarations, some of which are
10051          templates, is a template-name.  However, such a name should
10052          be a template-name; otherwise, there is no way to form a
10053          template-id for the overloaded templates.  */
10054       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10055       if (TREE_CODE (fns) == OVERLOAD)
10056         for (fn = fns; fn; fn = OVL_NEXT (fn))
10057           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10058             break;
10059
10060       if (!fn)
10061         {
10062           /* The name does not name a template.  */
10063           cp_parser_error (parser, "expected template-name");
10064           return error_mark_node;
10065         }
10066     }
10067
10068   /* If DECL is dependent, and refers to a function, then just return
10069      its name; we will look it up again during template instantiation.  */
10070   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10071     {
10072       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10073       if (TYPE_P (scope) && dependent_type_p (scope))
10074         return identifier;
10075     }
10076
10077   return decl;
10078 }
10079
10080 /* Parse a template-argument-list.
10081
10082    template-argument-list:
10083      template-argument ... [opt]
10084      template-argument-list , template-argument ... [opt]
10085
10086    Returns a TREE_VEC containing the arguments.  */
10087
10088 static tree
10089 cp_parser_template_argument_list (cp_parser* parser)
10090 {
10091   tree fixed_args[10];
10092   unsigned n_args = 0;
10093   unsigned alloced = 10;
10094   tree *arg_ary = fixed_args;
10095   tree vec;
10096   bool saved_in_template_argument_list_p;
10097   bool saved_ice_p;
10098   bool saved_non_ice_p;
10099
10100   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10101   parser->in_template_argument_list_p = true;
10102   /* Even if the template-id appears in an integral
10103      constant-expression, the contents of the argument list do
10104      not.  */
10105   saved_ice_p = parser->integral_constant_expression_p;
10106   parser->integral_constant_expression_p = false;
10107   saved_non_ice_p = parser->non_integral_constant_expression_p;
10108   parser->non_integral_constant_expression_p = false;
10109   /* Parse the arguments.  */
10110   do
10111     {
10112       tree argument;
10113
10114       if (n_args)
10115         /* Consume the comma.  */
10116         cp_lexer_consume_token (parser->lexer);
10117
10118       /* Parse the template-argument.  */
10119       argument = cp_parser_template_argument (parser);
10120
10121       /* If the next token is an ellipsis, we're expanding a template
10122          argument pack. */
10123       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10124         {
10125           /* Consume the `...' token. */
10126           cp_lexer_consume_token (parser->lexer);
10127
10128           /* Make the argument into a TYPE_PACK_EXPANSION or
10129              EXPR_PACK_EXPANSION. */
10130           argument = make_pack_expansion (argument);
10131         }
10132
10133       if (n_args == alloced)
10134         {
10135           alloced *= 2;
10136
10137           if (arg_ary == fixed_args)
10138             {
10139               arg_ary = XNEWVEC (tree, alloced);
10140               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10141             }
10142           else
10143             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10144         }
10145       arg_ary[n_args++] = argument;
10146     }
10147   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10148
10149   vec = make_tree_vec (n_args);
10150
10151   while (n_args--)
10152     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10153
10154   if (arg_ary != fixed_args)
10155     free (arg_ary);
10156   parser->non_integral_constant_expression_p = saved_non_ice_p;
10157   parser->integral_constant_expression_p = saved_ice_p;
10158   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10159   return vec;
10160 }
10161
10162 /* Parse a template-argument.
10163
10164    template-argument:
10165      assignment-expression
10166      type-id
10167      id-expression
10168
10169    The representation is that of an assignment-expression, type-id, or
10170    id-expression -- except that the qualified id-expression is
10171    evaluated, so that the value returned is either a DECL or an
10172    OVERLOAD.
10173
10174    Although the standard says "assignment-expression", it forbids
10175    throw-expressions or assignments in the template argument.
10176    Therefore, we use "conditional-expression" instead.  */
10177
10178 static tree
10179 cp_parser_template_argument (cp_parser* parser)
10180 {
10181   tree argument;
10182   bool template_p;
10183   bool address_p;
10184   bool maybe_type_id = false;
10185   cp_token *token;
10186   cp_id_kind idk;
10187
10188   /* There's really no way to know what we're looking at, so we just
10189      try each alternative in order.
10190
10191        [temp.arg]
10192
10193        In a template-argument, an ambiguity between a type-id and an
10194        expression is resolved to a type-id, regardless of the form of
10195        the corresponding template-parameter.
10196
10197      Therefore, we try a type-id first.  */
10198   cp_parser_parse_tentatively (parser);
10199   argument = cp_parser_type_id (parser);
10200   /* If there was no error parsing the type-id but the next token is a '>>',
10201      we probably found a typo for '> >'. But there are type-id which are
10202      also valid expressions. For instance:
10203
10204      struct X { int operator >> (int); };
10205      template <int V> struct Foo {};
10206      Foo<X () >> 5> r;
10207
10208      Here 'X()' is a valid type-id of a function type, but the user just
10209      wanted to write the expression "X() >> 5". Thus, we remember that we
10210      found a valid type-id, but we still try to parse the argument as an
10211      expression to see what happens.  */
10212   if (!cp_parser_error_occurred (parser)
10213       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10214     {
10215       maybe_type_id = true;
10216       cp_parser_abort_tentative_parse (parser);
10217     }
10218   else
10219     {
10220       /* If the next token isn't a `,' or a `>', then this argument wasn't
10221       really finished. This means that the argument is not a valid
10222       type-id.  */
10223       if (!cp_parser_next_token_ends_template_argument_p (parser))
10224         cp_parser_error (parser, "expected template-argument");
10225       /* If that worked, we're done.  */
10226       if (cp_parser_parse_definitely (parser))
10227         return argument;
10228     }
10229   /* We're still not sure what the argument will be.  */
10230   cp_parser_parse_tentatively (parser);
10231   /* Try a template.  */
10232   argument = cp_parser_id_expression (parser,
10233                                       /*template_keyword_p=*/false,
10234                                       /*check_dependency_p=*/true,
10235                                       &template_p,
10236                                       /*declarator_p=*/false,
10237                                       /*optional_p=*/false);
10238   /* If the next token isn't a `,' or a `>', then this argument wasn't
10239      really finished.  */
10240   if (!cp_parser_next_token_ends_template_argument_p (parser))
10241     cp_parser_error (parser, "expected template-argument");
10242   if (!cp_parser_error_occurred (parser))
10243     {
10244       /* Figure out what is being referred to.  If the id-expression
10245          was for a class template specialization, then we will have a
10246          TYPE_DECL at this point.  There is no need to do name lookup
10247          at this point in that case.  */
10248       if (TREE_CODE (argument) != TYPE_DECL)
10249         argument = cp_parser_lookup_name (parser, argument,
10250                                           none_type,
10251                                           /*is_template=*/template_p,
10252                                           /*is_namespace=*/false,
10253                                           /*check_dependency=*/true,
10254                                           /*ambiguous_decls=*/NULL);
10255       if (TREE_CODE (argument) != TEMPLATE_DECL
10256           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10257         cp_parser_error (parser, "expected template-name");
10258     }
10259   if (cp_parser_parse_definitely (parser))
10260     return argument;
10261   /* It must be a non-type argument.  There permitted cases are given
10262      in [temp.arg.nontype]:
10263
10264      -- an integral constant-expression of integral or enumeration
10265         type; or
10266
10267      -- the name of a non-type template-parameter; or
10268
10269      -- the name of an object or function with external linkage...
10270
10271      -- the address of an object or function with external linkage...
10272
10273      -- a pointer to member...  */
10274   /* Look for a non-type template parameter.  */
10275   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10276     {
10277       cp_parser_parse_tentatively (parser);
10278       argument = cp_parser_primary_expression (parser,
10279                                                /*adress_p=*/false,
10280                                                /*cast_p=*/false,
10281                                                /*template_arg_p=*/true,
10282                                                &idk);
10283       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10284           || !cp_parser_next_token_ends_template_argument_p (parser))
10285         cp_parser_simulate_error (parser);
10286       if (cp_parser_parse_definitely (parser))
10287         return argument;
10288     }
10289
10290   /* If the next token is "&", the argument must be the address of an
10291      object or function with external linkage.  */
10292   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10293   if (address_p)
10294     cp_lexer_consume_token (parser->lexer);
10295   /* See if we might have an id-expression.  */
10296   token = cp_lexer_peek_token (parser->lexer);
10297   if (token->type == CPP_NAME
10298       || token->keyword == RID_OPERATOR
10299       || token->type == CPP_SCOPE
10300       || token->type == CPP_TEMPLATE_ID
10301       || token->type == CPP_NESTED_NAME_SPECIFIER)
10302     {
10303       cp_parser_parse_tentatively (parser);
10304       argument = cp_parser_primary_expression (parser,
10305                                                address_p,
10306                                                /*cast_p=*/false,
10307                                                /*template_arg_p=*/true,
10308                                                &idk);
10309       if (cp_parser_error_occurred (parser)
10310           || !cp_parser_next_token_ends_template_argument_p (parser))
10311         cp_parser_abort_tentative_parse (parser);
10312       else
10313         {
10314           if (TREE_CODE (argument) == INDIRECT_REF)
10315             {
10316               gcc_assert (REFERENCE_REF_P (argument));
10317               argument = TREE_OPERAND (argument, 0);
10318             }
10319
10320           if (TREE_CODE (argument) == VAR_DECL)
10321             {
10322               /* A variable without external linkage might still be a
10323                  valid constant-expression, so no error is issued here
10324                  if the external-linkage check fails.  */
10325               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10326                 cp_parser_simulate_error (parser);
10327             }
10328           else if (is_overloaded_fn (argument))
10329             /* All overloaded functions are allowed; if the external
10330                linkage test does not pass, an error will be issued
10331                later.  */
10332             ;
10333           else if (address_p
10334                    && (TREE_CODE (argument) == OFFSET_REF
10335                        || TREE_CODE (argument) == SCOPE_REF))
10336             /* A pointer-to-member.  */
10337             ;
10338           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10339             ;
10340           else
10341             cp_parser_simulate_error (parser);
10342
10343           if (cp_parser_parse_definitely (parser))
10344             {
10345               if (address_p)
10346                 argument = build_x_unary_op (ADDR_EXPR, argument,
10347                                              tf_warning_or_error);
10348               return argument;
10349             }
10350         }
10351     }
10352   /* If the argument started with "&", there are no other valid
10353      alternatives at this point.  */
10354   if (address_p)
10355     {
10356       cp_parser_error (parser, "invalid non-type template argument");
10357       return error_mark_node;
10358     }
10359
10360   /* If the argument wasn't successfully parsed as a type-id followed
10361      by '>>', the argument can only be a constant expression now.
10362      Otherwise, we try parsing the constant-expression tentatively,
10363      because the argument could really be a type-id.  */
10364   if (maybe_type_id)
10365     cp_parser_parse_tentatively (parser);
10366   argument = cp_parser_constant_expression (parser,
10367                                             /*allow_non_constant_p=*/false,
10368                                             /*non_constant_p=*/NULL);
10369   argument = fold_non_dependent_expr (argument);
10370   if (!maybe_type_id)
10371     return argument;
10372   if (!cp_parser_next_token_ends_template_argument_p (parser))
10373     cp_parser_error (parser, "expected template-argument");
10374   if (cp_parser_parse_definitely (parser))
10375     return argument;
10376   /* We did our best to parse the argument as a non type-id, but that
10377      was the only alternative that matched (albeit with a '>' after
10378      it). We can assume it's just a typo from the user, and a
10379      diagnostic will then be issued.  */
10380   return cp_parser_type_id (parser);
10381 }
10382
10383 /* Parse an explicit-instantiation.
10384
10385    explicit-instantiation:
10386      template declaration
10387
10388    Although the standard says `declaration', what it really means is:
10389
10390    explicit-instantiation:
10391      template decl-specifier-seq [opt] declarator [opt] ;
10392
10393    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10394    supposed to be allowed.  A defect report has been filed about this
10395    issue.
10396
10397    GNU Extension:
10398
10399    explicit-instantiation:
10400      storage-class-specifier template
10401        decl-specifier-seq [opt] declarator [opt] ;
10402      function-specifier template
10403        decl-specifier-seq [opt] declarator [opt] ;  */
10404
10405 static void
10406 cp_parser_explicit_instantiation (cp_parser* parser)
10407 {
10408   int declares_class_or_enum;
10409   cp_decl_specifier_seq decl_specifiers;
10410   tree extension_specifier = NULL_TREE;
10411
10412   /* Look for an (optional) storage-class-specifier or
10413      function-specifier.  */
10414   if (cp_parser_allow_gnu_extensions_p (parser))
10415     {
10416       extension_specifier
10417         = cp_parser_storage_class_specifier_opt (parser);
10418       if (!extension_specifier)
10419         extension_specifier
10420           = cp_parser_function_specifier_opt (parser,
10421                                               /*decl_specs=*/NULL);
10422     }
10423
10424   /* Look for the `template' keyword.  */
10425   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10426   /* Let the front end know that we are processing an explicit
10427      instantiation.  */
10428   begin_explicit_instantiation ();
10429   /* [temp.explicit] says that we are supposed to ignore access
10430      control while processing explicit instantiation directives.  */
10431   push_deferring_access_checks (dk_no_check);
10432   /* Parse a decl-specifier-seq.  */
10433   cp_parser_decl_specifier_seq (parser,
10434                                 CP_PARSER_FLAGS_OPTIONAL,
10435                                 &decl_specifiers,
10436                                 &declares_class_or_enum);
10437   /* If there was exactly one decl-specifier, and it declared a class,
10438      and there's no declarator, then we have an explicit type
10439      instantiation.  */
10440   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10441     {
10442       tree type;
10443
10444       type = check_tag_decl (&decl_specifiers);
10445       /* Turn access control back on for names used during
10446          template instantiation.  */
10447       pop_deferring_access_checks ();
10448       if (type)
10449         do_type_instantiation (type, extension_specifier,
10450                                /*complain=*/tf_error);
10451     }
10452   else
10453     {
10454       cp_declarator *declarator;
10455       tree decl;
10456
10457       /* Parse the declarator.  */
10458       declarator
10459         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10460                                 /*ctor_dtor_or_conv_p=*/NULL,
10461                                 /*parenthesized_p=*/NULL,
10462                                 /*member_p=*/false);
10463       if (declares_class_or_enum & 2)
10464         cp_parser_check_for_definition_in_return_type (declarator,
10465                                                        decl_specifiers.type);
10466       if (declarator != cp_error_declarator)
10467         {
10468           decl = grokdeclarator (declarator, &decl_specifiers,
10469                                  NORMAL, 0, &decl_specifiers.attributes);
10470           /* Turn access control back on for names used during
10471              template instantiation.  */
10472           pop_deferring_access_checks ();
10473           /* Do the explicit instantiation.  */
10474           do_decl_instantiation (decl, extension_specifier);
10475         }
10476       else
10477         {
10478           pop_deferring_access_checks ();
10479           /* Skip the body of the explicit instantiation.  */
10480           cp_parser_skip_to_end_of_statement (parser);
10481         }
10482     }
10483   /* We're done with the instantiation.  */
10484   end_explicit_instantiation ();
10485
10486   cp_parser_consume_semicolon_at_end_of_statement (parser);
10487 }
10488
10489 /* Parse an explicit-specialization.
10490
10491    explicit-specialization:
10492      template < > declaration
10493
10494    Although the standard says `declaration', what it really means is:
10495
10496    explicit-specialization:
10497      template <> decl-specifier [opt] init-declarator [opt] ;
10498      template <> function-definition
10499      template <> explicit-specialization
10500      template <> template-declaration  */
10501
10502 static void
10503 cp_parser_explicit_specialization (cp_parser* parser)
10504 {
10505   bool need_lang_pop;
10506   /* Look for the `template' keyword.  */
10507   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10508   /* Look for the `<'.  */
10509   cp_parser_require (parser, CPP_LESS, "%<<%>");
10510   /* Look for the `>'.  */
10511   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10512   /* We have processed another parameter list.  */
10513   ++parser->num_template_parameter_lists;
10514   /* [temp]
10515
10516      A template ... explicit specialization ... shall not have C
10517      linkage.  */
10518   if (current_lang_name == lang_name_c)
10519     {
10520       error ("template specialization with C linkage");
10521       /* Give it C++ linkage to avoid confusing other parts of the
10522          front end.  */
10523       push_lang_context (lang_name_cplusplus);
10524       need_lang_pop = true;
10525     }
10526   else
10527     need_lang_pop = false;
10528   /* Let the front end know that we are beginning a specialization.  */
10529   if (!begin_specialization ())
10530     {
10531       end_specialization ();
10532       cp_parser_skip_to_end_of_block_or_statement (parser);
10533       return;
10534     }
10535
10536   /* If the next keyword is `template', we need to figure out whether
10537      or not we're looking a template-declaration.  */
10538   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10539     {
10540       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10541           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10542         cp_parser_template_declaration_after_export (parser,
10543                                                      /*member_p=*/false);
10544       else
10545         cp_parser_explicit_specialization (parser);
10546     }
10547   else
10548     /* Parse the dependent declaration.  */
10549     cp_parser_single_declaration (parser,
10550                                   /*checks=*/NULL,
10551                                   /*member_p=*/false,
10552                                   /*explicit_specialization_p=*/true,
10553                                   /*friend_p=*/NULL);
10554   /* We're done with the specialization.  */
10555   end_specialization ();
10556   /* For the erroneous case of a template with C linkage, we pushed an
10557      implicit C++ linkage scope; exit that scope now.  */
10558   if (need_lang_pop)
10559     pop_lang_context ();
10560   /* We're done with this parameter list.  */
10561   --parser->num_template_parameter_lists;
10562 }
10563
10564 /* Parse a type-specifier.
10565
10566    type-specifier:
10567      simple-type-specifier
10568      class-specifier
10569      enum-specifier
10570      elaborated-type-specifier
10571      cv-qualifier
10572
10573    GNU Extension:
10574
10575    type-specifier:
10576      __complex__
10577
10578    Returns a representation of the type-specifier.  For a
10579    class-specifier, enum-specifier, or elaborated-type-specifier, a
10580    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10581
10582    The parser flags FLAGS is used to control type-specifier parsing.
10583
10584    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10585    in a decl-specifier-seq.
10586
10587    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10588    class-specifier, enum-specifier, or elaborated-type-specifier, then
10589    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10590    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10591    zero.
10592
10593    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10594    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10595    is set to FALSE.  */
10596
10597 static tree
10598 cp_parser_type_specifier (cp_parser* parser,
10599                           cp_parser_flags flags,
10600                           cp_decl_specifier_seq *decl_specs,
10601                           bool is_declaration,
10602                           int* declares_class_or_enum,
10603                           bool* is_cv_qualifier)
10604 {
10605   tree type_spec = NULL_TREE;
10606   cp_token *token;
10607   enum rid keyword;
10608   cp_decl_spec ds = ds_last;
10609
10610   /* Assume this type-specifier does not declare a new type.  */
10611   if (declares_class_or_enum)
10612     *declares_class_or_enum = 0;
10613   /* And that it does not specify a cv-qualifier.  */
10614   if (is_cv_qualifier)
10615     *is_cv_qualifier = false;
10616   /* Peek at the next token.  */
10617   token = cp_lexer_peek_token (parser->lexer);
10618
10619   /* If we're looking at a keyword, we can use that to guide the
10620      production we choose.  */
10621   keyword = token->keyword;
10622   switch (keyword)
10623     {
10624     case RID_ENUM:
10625       /* Look for the enum-specifier.  */
10626       type_spec = cp_parser_enum_specifier (parser);
10627       /* If that worked, we're done.  */
10628       if (type_spec)
10629         {
10630           if (declares_class_or_enum)
10631             *declares_class_or_enum = 2;
10632           if (decl_specs)
10633             cp_parser_set_decl_spec_type (decl_specs,
10634                                           type_spec,
10635                                           /*user_defined_p=*/true);
10636           return type_spec;
10637         }
10638       else
10639         goto elaborated_type_specifier;
10640
10641       /* Any of these indicate either a class-specifier, or an
10642          elaborated-type-specifier.  */
10643     case RID_CLASS:
10644     case RID_STRUCT:
10645     case RID_UNION:
10646       /* Parse tentatively so that we can back up if we don't find a
10647          class-specifier.  */
10648       cp_parser_parse_tentatively (parser);
10649       /* Look for the class-specifier.  */
10650       type_spec = cp_parser_class_specifier (parser);
10651       /* If that worked, we're done.  */
10652       if (cp_parser_parse_definitely (parser))
10653         {
10654           if (declares_class_or_enum)
10655             *declares_class_or_enum = 2;
10656           if (decl_specs)
10657             cp_parser_set_decl_spec_type (decl_specs,
10658                                           type_spec,
10659                                           /*user_defined_p=*/true);
10660           return type_spec;
10661         }
10662
10663       /* Fall through.  */
10664     elaborated_type_specifier:
10665       /* We're declaring (not defining) a class or enum.  */
10666       if (declares_class_or_enum)
10667         *declares_class_or_enum = 1;
10668
10669       /* Fall through.  */
10670     case RID_TYPENAME:
10671       /* Look for an elaborated-type-specifier.  */
10672       type_spec
10673         = (cp_parser_elaborated_type_specifier
10674            (parser,
10675             decl_specs && decl_specs->specs[(int) ds_friend],
10676             is_declaration));
10677       if (decl_specs)
10678         cp_parser_set_decl_spec_type (decl_specs,
10679                                       type_spec,
10680                                       /*user_defined_p=*/true);
10681       return type_spec;
10682
10683     case RID_CONST:
10684       ds = ds_const;
10685       if (is_cv_qualifier)
10686         *is_cv_qualifier = true;
10687       break;
10688
10689     case RID_VOLATILE:
10690       ds = ds_volatile;
10691       if (is_cv_qualifier)
10692         *is_cv_qualifier = true;
10693       break;
10694
10695     case RID_RESTRICT:
10696       ds = ds_restrict;
10697       if (is_cv_qualifier)
10698         *is_cv_qualifier = true;
10699       break;
10700
10701     case RID_COMPLEX:
10702       /* The `__complex__' keyword is a GNU extension.  */
10703       ds = ds_complex;
10704       break;
10705
10706     default:
10707       break;
10708     }
10709
10710   /* Handle simple keywords.  */
10711   if (ds != ds_last)
10712     {
10713       if (decl_specs)
10714         {
10715           ++decl_specs->specs[(int)ds];
10716           decl_specs->any_specifiers_p = true;
10717         }
10718       return cp_lexer_consume_token (parser->lexer)->u.value;
10719     }
10720
10721   /* If we do not already have a type-specifier, assume we are looking
10722      at a simple-type-specifier.  */
10723   type_spec = cp_parser_simple_type_specifier (parser,
10724                                                decl_specs,
10725                                                flags);
10726
10727   /* If we didn't find a type-specifier, and a type-specifier was not
10728      optional in this context, issue an error message.  */
10729   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10730     {
10731       cp_parser_error (parser, "expected type specifier");
10732       return error_mark_node;
10733     }
10734
10735   return type_spec;
10736 }
10737
10738 /* Parse a simple-type-specifier.
10739
10740    simple-type-specifier:
10741      :: [opt] nested-name-specifier [opt] type-name
10742      :: [opt] nested-name-specifier template template-id
10743      char
10744      wchar_t
10745      bool
10746      short
10747      int
10748      long
10749      signed
10750      unsigned
10751      float
10752      double
10753      void
10754
10755    C++0x Extension:
10756
10757    simple-type-specifier:
10758      auto
10759      decltype ( expression )   
10760
10761    GNU Extension:
10762
10763    simple-type-specifier:
10764      __typeof__ unary-expression
10765      __typeof__ ( type-id )
10766
10767    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10768    appropriately updated.  */
10769
10770 static tree
10771 cp_parser_simple_type_specifier (cp_parser* parser,
10772                                  cp_decl_specifier_seq *decl_specs,
10773                                  cp_parser_flags flags)
10774 {
10775   tree type = NULL_TREE;
10776   cp_token *token;
10777
10778   /* Peek at the next token.  */
10779   token = cp_lexer_peek_token (parser->lexer);
10780
10781   /* If we're looking at a keyword, things are easy.  */
10782   switch (token->keyword)
10783     {
10784     case RID_CHAR:
10785       if (decl_specs)
10786         decl_specs->explicit_char_p = true;
10787       type = char_type_node;
10788       break;
10789     case RID_WCHAR:
10790       type = wchar_type_node;
10791       break;
10792     case RID_BOOL:
10793       type = boolean_type_node;
10794       break;
10795     case RID_SHORT:
10796       if (decl_specs)
10797         ++decl_specs->specs[(int) ds_short];
10798       type = short_integer_type_node;
10799       break;
10800     case RID_INT:
10801       if (decl_specs)
10802         decl_specs->explicit_int_p = true;
10803       type = integer_type_node;
10804       break;
10805     case RID_LONG:
10806       if (decl_specs)
10807         ++decl_specs->specs[(int) ds_long];
10808       type = long_integer_type_node;
10809       break;
10810     case RID_SIGNED:
10811       if (decl_specs)
10812         ++decl_specs->specs[(int) ds_signed];
10813       type = integer_type_node;
10814       break;
10815     case RID_UNSIGNED:
10816       if (decl_specs)
10817         ++decl_specs->specs[(int) ds_unsigned];
10818       type = unsigned_type_node;
10819       break;
10820     case RID_FLOAT:
10821       type = float_type_node;
10822       break;
10823     case RID_DOUBLE:
10824       type = double_type_node;
10825       break;
10826     case RID_VOID:
10827       type = void_type_node;
10828       break;
10829       
10830     case RID_AUTO:
10831       if (cxx_dialect != cxx98)
10832         {
10833           /* Consume the token.  */
10834           cp_lexer_consume_token (parser->lexer);
10835           /* We do not yet support the use of `auto' as a
10836              type-specifier.  */
10837           error ("C++0x %<auto%> specifier not supported");
10838         }
10839       break;
10840
10841     case RID_DECLTYPE:
10842       /* Parse the `decltype' type.  */
10843       type = cp_parser_decltype (parser);
10844
10845       if (decl_specs)
10846         cp_parser_set_decl_spec_type (decl_specs, type,
10847                                       /*user_defined_p=*/true);
10848
10849       return type;
10850
10851     case RID_TYPEOF:
10852       /* Consume the `typeof' token.  */
10853       cp_lexer_consume_token (parser->lexer);
10854       /* Parse the operand to `typeof'.  */
10855       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10856       /* If it is not already a TYPE, take its type.  */
10857       if (!TYPE_P (type))
10858         type = finish_typeof (type);
10859
10860       if (decl_specs)
10861         cp_parser_set_decl_spec_type (decl_specs, type,
10862                                       /*user_defined_p=*/true);
10863
10864       return type;
10865
10866     default:
10867       break;
10868     }
10869
10870   /* If the type-specifier was for a built-in type, we're done.  */
10871   if (type)
10872     {
10873       tree id;
10874
10875       /* Record the type.  */
10876       if (decl_specs
10877           && (token->keyword != RID_SIGNED
10878               && token->keyword != RID_UNSIGNED
10879               && token->keyword != RID_SHORT
10880               && token->keyword != RID_LONG))
10881         cp_parser_set_decl_spec_type (decl_specs,
10882                                       type,
10883                                       /*user_defined=*/false);
10884       if (decl_specs)
10885         decl_specs->any_specifiers_p = true;
10886
10887       /* Consume the token.  */
10888       id = cp_lexer_consume_token (parser->lexer)->u.value;
10889
10890       /* There is no valid C++ program where a non-template type is
10891          followed by a "<".  That usually indicates that the user thought
10892          that the type was a template.  */
10893       cp_parser_check_for_invalid_template_id (parser, type);
10894
10895       return TYPE_NAME (type);
10896     }
10897
10898   /* The type-specifier must be a user-defined type.  */
10899   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10900     {
10901       bool qualified_p;
10902       bool global_p;
10903
10904       /* Don't gobble tokens or issue error messages if this is an
10905          optional type-specifier.  */
10906       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10907         cp_parser_parse_tentatively (parser);
10908
10909       /* Look for the optional `::' operator.  */
10910       global_p
10911         = (cp_parser_global_scope_opt (parser,
10912                                        /*current_scope_valid_p=*/false)
10913            != NULL_TREE);
10914       /* Look for the nested-name specifier.  */
10915       qualified_p
10916         = (cp_parser_nested_name_specifier_opt (parser,
10917                                                 /*typename_keyword_p=*/false,
10918                                                 /*check_dependency_p=*/true,
10919                                                 /*type_p=*/false,
10920                                                 /*is_declaration=*/false)
10921            != NULL_TREE);
10922       /* If we have seen a nested-name-specifier, and the next token
10923          is `template', then we are using the template-id production.  */
10924       if (parser->scope
10925           && cp_parser_optional_template_keyword (parser))
10926         {
10927           /* Look for the template-id.  */
10928           type = cp_parser_template_id (parser,
10929                                         /*template_keyword_p=*/true,
10930                                         /*check_dependency_p=*/true,
10931                                         /*is_declaration=*/false);
10932           /* If the template-id did not name a type, we are out of
10933              luck.  */
10934           if (TREE_CODE (type) != TYPE_DECL)
10935             {
10936               cp_parser_error (parser, "expected template-id for type");
10937               type = NULL_TREE;
10938             }
10939         }
10940       /* Otherwise, look for a type-name.  */
10941       else
10942         type = cp_parser_type_name (parser);
10943       /* Keep track of all name-lookups performed in class scopes.  */
10944       if (type
10945           && !global_p
10946           && !qualified_p
10947           && TREE_CODE (type) == TYPE_DECL
10948           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10949         maybe_note_name_used_in_class (DECL_NAME (type), type);
10950       /* If it didn't work out, we don't have a TYPE.  */
10951       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10952           && !cp_parser_parse_definitely (parser))
10953         type = NULL_TREE;
10954       if (type && decl_specs)
10955         cp_parser_set_decl_spec_type (decl_specs, type,
10956                                       /*user_defined=*/true);
10957     }
10958
10959   /* If we didn't get a type-name, issue an error message.  */
10960   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10961     {
10962       cp_parser_error (parser, "expected type-name");
10963       return error_mark_node;
10964     }
10965
10966   /* There is no valid C++ program where a non-template type is
10967      followed by a "<".  That usually indicates that the user thought
10968      that the type was a template.  */
10969   if (type && type != error_mark_node)
10970     {
10971       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10972          If it is, then the '<'...'>' enclose protocol names rather than
10973          template arguments, and so everything is fine.  */
10974       if (c_dialect_objc ()
10975           && (objc_is_id (type) || objc_is_class_name (type)))
10976         {
10977           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10978           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10979
10980           /* Clobber the "unqualified" type previously entered into
10981              DECL_SPECS with the new, improved protocol-qualified version.  */
10982           if (decl_specs)
10983             decl_specs->type = qual_type;
10984
10985           return qual_type;
10986         }
10987
10988       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10989     }
10990
10991   return type;
10992 }
10993
10994 /* Parse a type-name.
10995
10996    type-name:
10997      class-name
10998      enum-name
10999      typedef-name
11000
11001    enum-name:
11002      identifier
11003
11004    typedef-name:
11005      identifier
11006
11007    Returns a TYPE_DECL for the type.  */
11008
11009 static tree
11010 cp_parser_type_name (cp_parser* parser)
11011 {
11012   tree type_decl;
11013
11014   /* We can't know yet whether it is a class-name or not.  */
11015   cp_parser_parse_tentatively (parser);
11016   /* Try a class-name.  */
11017   type_decl = cp_parser_class_name (parser,
11018                                     /*typename_keyword_p=*/false,
11019                                     /*template_keyword_p=*/false,
11020                                     none_type,
11021                                     /*check_dependency_p=*/true,
11022                                     /*class_head_p=*/false,
11023                                     /*is_declaration=*/false);
11024   /* If it's not a class-name, keep looking.  */
11025   if (!cp_parser_parse_definitely (parser))
11026     {
11027       /* It must be a typedef-name or an enum-name.  */
11028       return cp_parser_nonclass_name (parser);
11029     }
11030
11031   return type_decl;
11032 }
11033
11034 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11035
11036    enum-name:
11037      identifier
11038
11039    typedef-name:
11040      identifier
11041
11042    Returns a TYPE_DECL for the type.  */
11043
11044 static tree
11045 cp_parser_nonclass_name (cp_parser* parser)
11046 {
11047   tree type_decl;
11048   tree identifier;
11049
11050   identifier = cp_parser_identifier (parser);
11051   if (identifier == error_mark_node)
11052     return error_mark_node;
11053
11054   /* Look up the type-name.  */
11055   type_decl = cp_parser_lookup_name_simple (parser, identifier);
11056
11057   if (TREE_CODE (type_decl) != TYPE_DECL
11058       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11059     {
11060       /* See if this is an Objective-C type.  */
11061       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11062       tree type = objc_get_protocol_qualified_type (identifier, protos);
11063       if (type)
11064         type_decl = TYPE_NAME (type);
11065     }
11066   
11067   /* Issue an error if we did not find a type-name.  */
11068   if (TREE_CODE (type_decl) != TYPE_DECL)
11069     {
11070       if (!cp_parser_simulate_error (parser))
11071         cp_parser_name_lookup_error (parser, identifier, type_decl,
11072                                      "is not a type");
11073       return error_mark_node;
11074     }
11075   /* Remember that the name was used in the definition of the
11076      current class so that we can check later to see if the
11077      meaning would have been different after the class was
11078      entirely defined.  */
11079   else if (type_decl != error_mark_node
11080            && !parser->scope)
11081     maybe_note_name_used_in_class (identifier, type_decl);
11082   
11083   return type_decl;
11084 }
11085
11086 /* Parse an elaborated-type-specifier.  Note that the grammar given
11087    here incorporates the resolution to DR68.
11088
11089    elaborated-type-specifier:
11090      class-key :: [opt] nested-name-specifier [opt] identifier
11091      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11092      enum :: [opt] nested-name-specifier [opt] identifier
11093      typename :: [opt] nested-name-specifier identifier
11094      typename :: [opt] nested-name-specifier template [opt]
11095        template-id
11096
11097    GNU extension:
11098
11099    elaborated-type-specifier:
11100      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11101      class-key attributes :: [opt] nested-name-specifier [opt]
11102                template [opt] template-id
11103      enum attributes :: [opt] nested-name-specifier [opt] identifier
11104
11105    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11106    declared `friend'.  If IS_DECLARATION is TRUE, then this
11107    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11108    something is being declared.
11109
11110    Returns the TYPE specified.  */
11111
11112 static tree
11113 cp_parser_elaborated_type_specifier (cp_parser* parser,
11114                                      bool is_friend,
11115                                      bool is_declaration)
11116 {
11117   enum tag_types tag_type;
11118   tree identifier;
11119   tree type = NULL_TREE;
11120   tree attributes = NULL_TREE;
11121
11122   /* See if we're looking at the `enum' keyword.  */
11123   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11124     {
11125       /* Consume the `enum' token.  */
11126       cp_lexer_consume_token (parser->lexer);
11127       /* Remember that it's an enumeration type.  */
11128       tag_type = enum_type;
11129       /* Parse the attributes.  */
11130       attributes = cp_parser_attributes_opt (parser);
11131     }
11132   /* Or, it might be `typename'.  */
11133   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11134                                            RID_TYPENAME))
11135     {
11136       /* Consume the `typename' token.  */
11137       cp_lexer_consume_token (parser->lexer);
11138       /* Remember that it's a `typename' type.  */
11139       tag_type = typename_type;
11140       /* The `typename' keyword is only allowed in templates.  */
11141       if (!processing_template_decl)
11142         pedwarn ("using %<typename%> outside of template");
11143     }
11144   /* Otherwise it must be a class-key.  */
11145   else
11146     {
11147       tag_type = cp_parser_class_key (parser);
11148       if (tag_type == none_type)
11149         return error_mark_node;
11150       /* Parse the attributes.  */
11151       attributes = cp_parser_attributes_opt (parser);
11152     }
11153
11154   /* Look for the `::' operator.  */
11155   cp_parser_global_scope_opt (parser,
11156                               /*current_scope_valid_p=*/false);
11157   /* Look for the nested-name-specifier.  */
11158   if (tag_type == typename_type)
11159     {
11160       if (!cp_parser_nested_name_specifier (parser,
11161                                            /*typename_keyword_p=*/true,
11162                                            /*check_dependency_p=*/true,
11163                                            /*type_p=*/true,
11164                                             is_declaration))
11165         return error_mark_node;
11166     }
11167   else
11168     /* Even though `typename' is not present, the proposed resolution
11169        to Core Issue 180 says that in `class A<T>::B', `B' should be
11170        considered a type-name, even if `A<T>' is dependent.  */
11171     cp_parser_nested_name_specifier_opt (parser,
11172                                          /*typename_keyword_p=*/true,
11173                                          /*check_dependency_p=*/true,
11174                                          /*type_p=*/true,
11175                                          is_declaration);
11176  /* For everything but enumeration types, consider a template-id.
11177     For an enumeration type, consider only a plain identifier.  */
11178   if (tag_type != enum_type)
11179     {
11180       bool template_p = false;
11181       tree decl;
11182
11183       /* Allow the `template' keyword.  */
11184       template_p = cp_parser_optional_template_keyword (parser);
11185       /* If we didn't see `template', we don't know if there's a
11186          template-id or not.  */
11187       if (!template_p)
11188         cp_parser_parse_tentatively (parser);
11189       /* Parse the template-id.  */
11190       decl = cp_parser_template_id (parser, template_p,
11191                                     /*check_dependency_p=*/true,
11192                                     is_declaration);
11193       /* If we didn't find a template-id, look for an ordinary
11194          identifier.  */
11195       if (!template_p && !cp_parser_parse_definitely (parser))
11196         ;
11197       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11198          in effect, then we must assume that, upon instantiation, the
11199          template will correspond to a class.  */
11200       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11201                && tag_type == typename_type)
11202         type = make_typename_type (parser->scope, decl,
11203                                    typename_type,
11204                                    /*complain=*/tf_error);
11205       else
11206         type = TREE_TYPE (decl);
11207     }
11208
11209   if (!type)
11210     {
11211       identifier = cp_parser_identifier (parser);
11212
11213       if (identifier == error_mark_node)
11214         {
11215           parser->scope = NULL_TREE;
11216           return error_mark_node;
11217         }
11218
11219       /* For a `typename', we needn't call xref_tag.  */
11220       if (tag_type == typename_type
11221           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11222         return cp_parser_make_typename_type (parser, parser->scope,
11223                                              identifier);
11224       /* Look up a qualified name in the usual way.  */
11225       if (parser->scope)
11226         {
11227           tree decl;
11228           tree ambiguous_decls;
11229
11230           decl = cp_parser_lookup_name (parser, identifier,
11231                                         tag_type,
11232                                         /*is_template=*/false,
11233                                         /*is_namespace=*/false,
11234                                         /*check_dependency=*/true,
11235                                         &ambiguous_decls);
11236
11237           /* If the lookup was ambiguous, an error will already have been
11238              issued.  */
11239           if (ambiguous_decls)
11240             return error_mark_node;
11241
11242           /* If we are parsing friend declaration, DECL may be a
11243              TEMPLATE_DECL tree node here.  However, we need to check
11244              whether this TEMPLATE_DECL results in valid code.  Consider
11245              the following example:
11246
11247                namespace N {
11248                  template <class T> class C {};
11249                }
11250                class X {
11251                  template <class T> friend class N::C; // #1, valid code
11252                };
11253                template <class T> class Y {
11254                  friend class N::C;                    // #2, invalid code
11255                };
11256
11257              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11258              name lookup of `N::C'.  We see that friend declaration must
11259              be template for the code to be valid.  Note that
11260              processing_template_decl does not work here since it is
11261              always 1 for the above two cases.  */
11262
11263           decl = (cp_parser_maybe_treat_template_as_class
11264                   (decl, /*tag_name_p=*/is_friend
11265                          && parser->num_template_parameter_lists));
11266
11267           if (TREE_CODE (decl) != TYPE_DECL)
11268             {
11269               cp_parser_diagnose_invalid_type_name (parser,
11270                                                     parser->scope,
11271                                                     identifier);
11272               return error_mark_node;
11273             }
11274
11275           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11276             {
11277               bool allow_template = (parser->num_template_parameter_lists
11278                                       || DECL_SELF_REFERENCE_P (decl));
11279               type = check_elaborated_type_specifier (tag_type, decl, 
11280                                                       allow_template);
11281
11282               if (type == error_mark_node)
11283                 return error_mark_node;
11284             }
11285
11286           /* Forward declarations of nested types, such as
11287
11288                class C1::C2;
11289                class C1::C2::C3;
11290
11291              are invalid unless all components preceding the final '::'
11292              are complete.  If all enclosing types are complete, these
11293              declarations become merely pointless.
11294
11295              Invalid forward declarations of nested types are errors
11296              caught elsewhere in parsing.  Those that are pointless arrive
11297              here.  */
11298
11299           if (cp_parser_declares_only_class_p (parser)
11300               && !is_friend && !processing_explicit_instantiation)
11301             warning (0, "declaration %qD does not declare anything", decl);
11302
11303           type = TREE_TYPE (decl);
11304         }
11305       else
11306         {
11307           /* An elaborated-type-specifier sometimes introduces a new type and
11308              sometimes names an existing type.  Normally, the rule is that it
11309              introduces a new type only if there is not an existing type of
11310              the same name already in scope.  For example, given:
11311
11312                struct S {};
11313                void f() { struct S s; }
11314
11315              the `struct S' in the body of `f' is the same `struct S' as in
11316              the global scope; the existing definition is used.  However, if
11317              there were no global declaration, this would introduce a new
11318              local class named `S'.
11319
11320              An exception to this rule applies to the following code:
11321
11322                namespace N { struct S; }
11323
11324              Here, the elaborated-type-specifier names a new type
11325              unconditionally; even if there is already an `S' in the
11326              containing scope this declaration names a new type.
11327              This exception only applies if the elaborated-type-specifier
11328              forms the complete declaration:
11329
11330                [class.name]
11331
11332                A declaration consisting solely of `class-key identifier ;' is
11333                either a redeclaration of the name in the current scope or a
11334                forward declaration of the identifier as a class name.  It
11335                introduces the name into the current scope.
11336
11337              We are in this situation precisely when the next token is a `;'.
11338
11339              An exception to the exception is that a `friend' declaration does
11340              *not* name a new type; i.e., given:
11341
11342                struct S { friend struct T; };
11343
11344              `T' is not a new type in the scope of `S'.
11345
11346              Also, `new struct S' or `sizeof (struct S)' never results in the
11347              definition of a new type; a new type can only be declared in a
11348              declaration context.  */
11349
11350           tag_scope ts;
11351           bool template_p;
11352
11353           if (is_friend)
11354             /* Friends have special name lookup rules.  */
11355             ts = ts_within_enclosing_non_class;
11356           else if (is_declaration
11357                    && cp_lexer_next_token_is (parser->lexer,
11358                                               CPP_SEMICOLON))
11359             /* This is a `class-key identifier ;' */
11360             ts = ts_current;
11361           else
11362             ts = ts_global;
11363
11364           template_p =
11365             (parser->num_template_parameter_lists
11366              && (cp_parser_next_token_starts_class_definition_p (parser)
11367                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11368           /* An unqualified name was used to reference this type, so
11369              there were no qualifying templates.  */
11370           if (!cp_parser_check_template_parameters (parser,
11371                                                     /*num_templates=*/0))
11372             return error_mark_node;
11373           type = xref_tag (tag_type, identifier, ts, template_p);
11374         }
11375     }
11376
11377   if (type == error_mark_node)
11378     return error_mark_node;
11379
11380   /* Allow attributes on forward declarations of classes.  */
11381   if (attributes)
11382     {
11383       if (TREE_CODE (type) == TYPENAME_TYPE)
11384         warning (OPT_Wattributes,
11385                  "attributes ignored on uninstantiated type");
11386       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11387                && ! processing_explicit_instantiation)
11388         warning (OPT_Wattributes,
11389                  "attributes ignored on template instantiation");
11390       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11391         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11392       else
11393         warning (OPT_Wattributes,
11394                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11395     }
11396
11397   if (tag_type != enum_type)
11398     cp_parser_check_class_key (tag_type, type);
11399
11400   /* A "<" cannot follow an elaborated type specifier.  If that
11401      happens, the user was probably trying to form a template-id.  */
11402   cp_parser_check_for_invalid_template_id (parser, type);
11403
11404   return type;
11405 }
11406
11407 /* Parse an enum-specifier.
11408
11409    enum-specifier:
11410      enum identifier [opt] { enumerator-list [opt] }
11411
11412    GNU Extensions:
11413      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11414        attributes[opt]
11415
11416    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11417    if the token stream isn't an enum-specifier after all.  */
11418
11419 static tree
11420 cp_parser_enum_specifier (cp_parser* parser)
11421 {
11422   tree identifier;
11423   tree type;
11424   tree attributes;
11425
11426   /* Parse tentatively so that we can back up if we don't find a
11427      enum-specifier.  */
11428   cp_parser_parse_tentatively (parser);
11429
11430   /* Caller guarantees that the current token is 'enum', an identifier
11431      possibly follows, and the token after that is an opening brace.
11432      If we don't have an identifier, fabricate an anonymous name for
11433      the enumeration being defined.  */
11434   cp_lexer_consume_token (parser->lexer);
11435
11436   attributes = cp_parser_attributes_opt (parser);
11437
11438   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11439     identifier = cp_parser_identifier (parser);
11440   else
11441     identifier = make_anon_name ();
11442
11443   /* Look for the `{' but don't consume it yet.  */
11444   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11445     cp_parser_simulate_error (parser);
11446
11447   if (!cp_parser_parse_definitely (parser))
11448     return NULL_TREE;
11449
11450   /* Issue an error message if type-definitions are forbidden here.  */
11451   if (!cp_parser_check_type_definition (parser))
11452     type = error_mark_node;
11453   else
11454     /* Create the new type.  We do this before consuming the opening
11455        brace so the enum will be recorded as being on the line of its
11456        tag (or the 'enum' keyword, if there is no tag).  */
11457     type = start_enum (identifier);
11458   
11459   /* Consume the opening brace.  */
11460   cp_lexer_consume_token (parser->lexer);
11461
11462   if (type == error_mark_node)
11463     {
11464       cp_parser_skip_to_end_of_block_or_statement (parser);
11465       return error_mark_node;
11466     }
11467
11468   /* If the next token is not '}', then there are some enumerators.  */
11469   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11470     cp_parser_enumerator_list (parser, type);
11471
11472   /* Consume the final '}'.  */
11473   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11474
11475   /* Look for trailing attributes to apply to this enumeration, and
11476      apply them if appropriate.  */
11477   if (cp_parser_allow_gnu_extensions_p (parser))
11478     {
11479       tree trailing_attr = cp_parser_attributes_opt (parser);
11480       cplus_decl_attributes (&type,
11481                              trailing_attr,
11482                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11483     }
11484
11485   /* Finish up the enumeration.  */
11486   finish_enum (type);
11487
11488   return type;
11489 }
11490
11491 /* Parse an enumerator-list.  The enumerators all have the indicated
11492    TYPE.
11493
11494    enumerator-list:
11495      enumerator-definition
11496      enumerator-list , enumerator-definition  */
11497
11498 static void
11499 cp_parser_enumerator_list (cp_parser* parser, tree type)
11500 {
11501   while (true)
11502     {
11503       /* Parse an enumerator-definition.  */
11504       cp_parser_enumerator_definition (parser, type);
11505
11506       /* If the next token is not a ',', we've reached the end of
11507          the list.  */
11508       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11509         break;
11510       /* Otherwise, consume the `,' and keep going.  */
11511       cp_lexer_consume_token (parser->lexer);
11512       /* If the next token is a `}', there is a trailing comma.  */
11513       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11514         {
11515           if (pedantic && !in_system_header)
11516             pedwarn ("comma at end of enumerator list");
11517           break;
11518         }
11519     }
11520 }
11521
11522 /* Parse an enumerator-definition.  The enumerator has the indicated
11523    TYPE.
11524
11525    enumerator-definition:
11526      enumerator
11527      enumerator = constant-expression
11528
11529    enumerator:
11530      identifier  */
11531
11532 static void
11533 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11534 {
11535   tree identifier;
11536   tree value;
11537
11538   /* Look for the identifier.  */
11539   identifier = cp_parser_identifier (parser);
11540   if (identifier == error_mark_node)
11541     return;
11542
11543   /* If the next token is an '=', then there is an explicit value.  */
11544   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11545     {
11546       /* Consume the `=' token.  */
11547       cp_lexer_consume_token (parser->lexer);
11548       /* Parse the value.  */
11549       value = cp_parser_constant_expression (parser,
11550                                              /*allow_non_constant_p=*/false,
11551                                              NULL);
11552     }
11553   else
11554     value = NULL_TREE;
11555
11556   /* Create the enumerator.  */
11557   build_enumerator (identifier, value, type);
11558 }
11559
11560 /* Parse a namespace-name.
11561
11562    namespace-name:
11563      original-namespace-name
11564      namespace-alias
11565
11566    Returns the NAMESPACE_DECL for the namespace.  */
11567
11568 static tree
11569 cp_parser_namespace_name (cp_parser* parser)
11570 {
11571   tree identifier;
11572   tree namespace_decl;
11573
11574   /* Get the name of the namespace.  */
11575   identifier = cp_parser_identifier (parser);
11576   if (identifier == error_mark_node)
11577     return error_mark_node;
11578
11579   /* Look up the identifier in the currently active scope.  Look only
11580      for namespaces, due to:
11581
11582        [basic.lookup.udir]
11583
11584        When looking up a namespace-name in a using-directive or alias
11585        definition, only namespace names are considered.
11586
11587      And:
11588
11589        [basic.lookup.qual]
11590
11591        During the lookup of a name preceding the :: scope resolution
11592        operator, object, function, and enumerator names are ignored.
11593
11594      (Note that cp_parser_class_or_namespace_name only calls this
11595      function if the token after the name is the scope resolution
11596      operator.)  */
11597   namespace_decl = cp_parser_lookup_name (parser, identifier,
11598                                           none_type,
11599                                           /*is_template=*/false,
11600                                           /*is_namespace=*/true,
11601                                           /*check_dependency=*/true,
11602                                           /*ambiguous_decls=*/NULL);
11603   /* If it's not a namespace, issue an error.  */
11604   if (namespace_decl == error_mark_node
11605       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11606     {
11607       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11608         error ("%qD is not a namespace-name", identifier);
11609       cp_parser_error (parser, "expected namespace-name");
11610       namespace_decl = error_mark_node;
11611     }
11612
11613   return namespace_decl;
11614 }
11615
11616 /* Parse a namespace-definition.
11617
11618    namespace-definition:
11619      named-namespace-definition
11620      unnamed-namespace-definition
11621
11622    named-namespace-definition:
11623      original-namespace-definition
11624      extension-namespace-definition
11625
11626    original-namespace-definition:
11627      namespace identifier { namespace-body }
11628
11629    extension-namespace-definition:
11630      namespace original-namespace-name { namespace-body }
11631
11632    unnamed-namespace-definition:
11633      namespace { namespace-body } */
11634
11635 static void
11636 cp_parser_namespace_definition (cp_parser* parser)
11637 {
11638   tree identifier, attribs;
11639   bool has_visibility;
11640   bool is_inline;
11641
11642   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11643     {
11644       is_inline = true;
11645       cp_lexer_consume_token (parser->lexer);
11646     }
11647   else
11648     is_inline = false;
11649
11650   /* Look for the `namespace' keyword.  */
11651   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11652
11653   /* Get the name of the namespace.  We do not attempt to distinguish
11654      between an original-namespace-definition and an
11655      extension-namespace-definition at this point.  The semantic
11656      analysis routines are responsible for that.  */
11657   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11658     identifier = cp_parser_identifier (parser);
11659   else
11660     identifier = NULL_TREE;
11661
11662   /* Parse any specified attributes.  */
11663   attribs = cp_parser_attributes_opt (parser);
11664
11665   /* Look for the `{' to start the namespace.  */
11666   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11667   /* Start the namespace.  */
11668   push_namespace (identifier);
11669
11670   /* "inline namespace" is equivalent to a stub namespace definition
11671      followed by a strong using directive.  */
11672   if (is_inline)
11673     {
11674       tree namespace = current_namespace;
11675       /* Set up namespace association.  */
11676       DECL_NAMESPACE_ASSOCIATIONS (namespace)
11677         = tree_cons (CP_DECL_CONTEXT (namespace), NULL_TREE,
11678                      DECL_NAMESPACE_ASSOCIATIONS (namespace));
11679       /* Import the contents of the inline namespace.  */
11680       pop_namespace ();
11681       do_using_directive (namespace);
11682       push_namespace (identifier);
11683     }
11684
11685   has_visibility = handle_namespace_attrs (current_namespace, attribs);
11686
11687   /* Parse the body of the namespace.  */
11688   cp_parser_namespace_body (parser);
11689
11690 #ifdef HANDLE_PRAGMA_VISIBILITY
11691   if (has_visibility)
11692     pop_visibility ();
11693 #endif
11694
11695   /* Finish the namespace.  */
11696   pop_namespace ();
11697   /* Look for the final `}'.  */
11698   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11699 }
11700
11701 /* Parse a namespace-body.
11702
11703    namespace-body:
11704      declaration-seq [opt]  */
11705
11706 static void
11707 cp_parser_namespace_body (cp_parser* parser)
11708 {
11709   cp_parser_declaration_seq_opt (parser);
11710 }
11711
11712 /* Parse a namespace-alias-definition.
11713
11714    namespace-alias-definition:
11715      namespace identifier = qualified-namespace-specifier ;  */
11716
11717 static void
11718 cp_parser_namespace_alias_definition (cp_parser* parser)
11719 {
11720   tree identifier;
11721   tree namespace_specifier;
11722
11723   /* Look for the `namespace' keyword.  */
11724   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11725   /* Look for the identifier.  */
11726   identifier = cp_parser_identifier (parser);
11727   if (identifier == error_mark_node)
11728     return;
11729   /* Look for the `=' token.  */
11730   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11731       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11732     {
11733       error ("%<namespace%> definition is not allowed here");
11734       /* Skip the definition.  */
11735       cp_lexer_consume_token (parser->lexer);
11736       if (cp_parser_skip_to_closing_brace (parser))
11737         cp_lexer_consume_token (parser->lexer);
11738       return;
11739     }
11740   cp_parser_require (parser, CPP_EQ, "%<=%>");
11741   /* Look for the qualified-namespace-specifier.  */
11742   namespace_specifier
11743     = cp_parser_qualified_namespace_specifier (parser);
11744   /* Look for the `;' token.  */
11745   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11746
11747   /* Register the alias in the symbol table.  */
11748   do_namespace_alias (identifier, namespace_specifier);
11749 }
11750
11751 /* Parse a qualified-namespace-specifier.
11752
11753    qualified-namespace-specifier:
11754      :: [opt] nested-name-specifier [opt] namespace-name
11755
11756    Returns a NAMESPACE_DECL corresponding to the specified
11757    namespace.  */
11758
11759 static tree
11760 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11761 {
11762   /* Look for the optional `::'.  */
11763   cp_parser_global_scope_opt (parser,
11764                               /*current_scope_valid_p=*/false);
11765
11766   /* Look for the optional nested-name-specifier.  */
11767   cp_parser_nested_name_specifier_opt (parser,
11768                                        /*typename_keyword_p=*/false,
11769                                        /*check_dependency_p=*/true,
11770                                        /*type_p=*/false,
11771                                        /*is_declaration=*/true);
11772
11773   return cp_parser_namespace_name (parser);
11774 }
11775
11776 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11777    access declaration.
11778
11779    using-declaration:
11780      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11781      using :: unqualified-id ;  
11782
11783    access-declaration:
11784      qualified-id ;  
11785
11786    */
11787
11788 static bool
11789 cp_parser_using_declaration (cp_parser* parser, 
11790                              bool access_declaration_p)
11791 {
11792   cp_token *token;
11793   bool typename_p = false;
11794   bool global_scope_p;
11795   tree decl;
11796   tree identifier;
11797   tree qscope;
11798
11799   if (access_declaration_p)
11800     cp_parser_parse_tentatively (parser);
11801   else
11802     {
11803       /* Look for the `using' keyword.  */
11804       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
11805       
11806       /* Peek at the next token.  */
11807       token = cp_lexer_peek_token (parser->lexer);
11808       /* See if it's `typename'.  */
11809       if (token->keyword == RID_TYPENAME)
11810         {
11811           /* Remember that we've seen it.  */
11812           typename_p = true;
11813           /* Consume the `typename' token.  */
11814           cp_lexer_consume_token (parser->lexer);
11815         }
11816     }
11817
11818   /* Look for the optional global scope qualification.  */
11819   global_scope_p
11820     = (cp_parser_global_scope_opt (parser,
11821                                    /*current_scope_valid_p=*/false)
11822        != NULL_TREE);
11823
11824   /* If we saw `typename', or didn't see `::', then there must be a
11825      nested-name-specifier present.  */
11826   if (typename_p || !global_scope_p)
11827     qscope = cp_parser_nested_name_specifier (parser, typename_p,
11828                                               /*check_dependency_p=*/true,
11829                                               /*type_p=*/false,
11830                                               /*is_declaration=*/true);
11831   /* Otherwise, we could be in either of the two productions.  In that
11832      case, treat the nested-name-specifier as optional.  */
11833   else
11834     qscope = cp_parser_nested_name_specifier_opt (parser,
11835                                                   /*typename_keyword_p=*/false,
11836                                                   /*check_dependency_p=*/true,
11837                                                   /*type_p=*/false,
11838                                                   /*is_declaration=*/true);
11839   if (!qscope)
11840     qscope = global_namespace;
11841
11842   if (access_declaration_p && cp_parser_error_occurred (parser))
11843     /* Something has already gone wrong; there's no need to parse
11844        further.  Since an error has occurred, the return value of
11845        cp_parser_parse_definitely will be false, as required.  */
11846     return cp_parser_parse_definitely (parser);
11847
11848   /* Parse the unqualified-id.  */
11849   identifier = cp_parser_unqualified_id (parser,
11850                                          /*template_keyword_p=*/false,
11851                                          /*check_dependency_p=*/true,
11852                                          /*declarator_p=*/true,
11853                                          /*optional_p=*/false);
11854
11855   if (access_declaration_p)
11856     {
11857       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11858         cp_parser_simulate_error (parser);
11859       if (!cp_parser_parse_definitely (parser))
11860         return false;
11861     }
11862
11863   /* The function we call to handle a using-declaration is different
11864      depending on what scope we are in.  */
11865   if (qscope == error_mark_node || identifier == error_mark_node)
11866     ;
11867   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11868            && TREE_CODE (identifier) != BIT_NOT_EXPR)
11869     /* [namespace.udecl]
11870
11871        A using declaration shall not name a template-id.  */
11872     error ("a template-id may not appear in a using-declaration");
11873   else
11874     {
11875       if (at_class_scope_p ())
11876         {
11877           /* Create the USING_DECL.  */
11878           decl = do_class_using_decl (parser->scope, identifier);
11879
11880           if (check_for_bare_parameter_packs (decl))
11881             return false;
11882           else
11883             /* Add it to the list of members in this class.  */
11884             finish_member_declaration (decl);
11885         }
11886       else
11887         {
11888           decl = cp_parser_lookup_name_simple (parser, identifier);
11889           if (decl == error_mark_node)
11890             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11891           else if (check_for_bare_parameter_packs (decl))
11892             return false;
11893           else if (!at_namespace_scope_p ())
11894             do_local_using_decl (decl, qscope, identifier);
11895           else
11896             do_toplevel_using_decl (decl, qscope, identifier);
11897         }
11898     }
11899
11900   /* Look for the final `;'.  */
11901   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11902   
11903   return true;
11904 }
11905
11906 /* Parse a using-directive.
11907
11908    using-directive:
11909      using namespace :: [opt] nested-name-specifier [opt]
11910        namespace-name ;  */
11911
11912 static void
11913 cp_parser_using_directive (cp_parser* parser)
11914 {
11915   tree namespace_decl;
11916   tree attribs;
11917
11918   /* Look for the `using' keyword.  */
11919   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
11920   /* And the `namespace' keyword.  */
11921   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11922   /* Look for the optional `::' operator.  */
11923   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11924   /* And the optional nested-name-specifier.  */
11925   cp_parser_nested_name_specifier_opt (parser,
11926                                        /*typename_keyword_p=*/false,
11927                                        /*check_dependency_p=*/true,
11928                                        /*type_p=*/false,
11929                                        /*is_declaration=*/true);
11930   /* Get the namespace being used.  */
11931   namespace_decl = cp_parser_namespace_name (parser);
11932   /* And any specified attributes.  */
11933   attribs = cp_parser_attributes_opt (parser);
11934   /* Update the symbol table.  */
11935   parse_using_directive (namespace_decl, attribs);
11936   /* Look for the final `;'.  */
11937   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11938 }
11939
11940 /* Parse an asm-definition.
11941
11942    asm-definition:
11943      asm ( string-literal ) ;
11944
11945    GNU Extension:
11946
11947    asm-definition:
11948      asm volatile [opt] ( string-literal ) ;
11949      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11950      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11951                           : asm-operand-list [opt] ) ;
11952      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11953                           : asm-operand-list [opt]
11954                           : asm-operand-list [opt] ) ;  */
11955
11956 static void
11957 cp_parser_asm_definition (cp_parser* parser)
11958 {
11959   tree string;
11960   tree outputs = NULL_TREE;
11961   tree inputs = NULL_TREE;
11962   tree clobbers = NULL_TREE;
11963   tree asm_stmt;
11964   bool volatile_p = false;
11965   bool extended_p = false;
11966   bool invalid_inputs_p = false;
11967   bool invalid_outputs_p = false;
11968
11969   /* Look for the `asm' keyword.  */
11970   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
11971   /* See if the next token is `volatile'.  */
11972   if (cp_parser_allow_gnu_extensions_p (parser)
11973       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11974     {
11975       /* Remember that we saw the `volatile' keyword.  */
11976       volatile_p = true;
11977       /* Consume the token.  */
11978       cp_lexer_consume_token (parser->lexer);
11979     }
11980   /* Look for the opening `('.  */
11981   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
11982     return;
11983   /* Look for the string.  */
11984   string = cp_parser_string_literal (parser, false, false);
11985   if (string == error_mark_node)
11986     {
11987       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11988                                              /*consume_paren=*/true);
11989       return;
11990     }
11991
11992   /* If we're allowing GNU extensions, check for the extended assembly
11993      syntax.  Unfortunately, the `:' tokens need not be separated by
11994      a space in C, and so, for compatibility, we tolerate that here
11995      too.  Doing that means that we have to treat the `::' operator as
11996      two `:' tokens.  */
11997   if (cp_parser_allow_gnu_extensions_p (parser)
11998       && parser->in_function_body
11999       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12000           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12001     {
12002       bool inputs_p = false;
12003       bool clobbers_p = false;
12004
12005       /* The extended syntax was used.  */
12006       extended_p = true;
12007
12008       /* Look for outputs.  */
12009       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12010         {
12011           /* Consume the `:'.  */
12012           cp_lexer_consume_token (parser->lexer);
12013           /* Parse the output-operands.  */
12014           if (cp_lexer_next_token_is_not (parser->lexer,
12015                                           CPP_COLON)
12016               && cp_lexer_next_token_is_not (parser->lexer,
12017                                              CPP_SCOPE)
12018               && cp_lexer_next_token_is_not (parser->lexer,
12019                                              CPP_CLOSE_PAREN))
12020             outputs = cp_parser_asm_operand_list (parser);
12021
12022             if (outputs == error_mark_node)
12023               invalid_outputs_p = true;
12024         }
12025       /* If the next token is `::', there are no outputs, and the
12026          next token is the beginning of the inputs.  */
12027       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12028         /* The inputs are coming next.  */
12029         inputs_p = true;
12030
12031       /* Look for inputs.  */
12032       if (inputs_p
12033           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12034         {
12035           /* Consume the `:' or `::'.  */
12036           cp_lexer_consume_token (parser->lexer);
12037           /* Parse the output-operands.  */
12038           if (cp_lexer_next_token_is_not (parser->lexer,
12039                                           CPP_COLON)
12040               && cp_lexer_next_token_is_not (parser->lexer,
12041                                              CPP_CLOSE_PAREN))
12042             inputs = cp_parser_asm_operand_list (parser);
12043
12044             if (inputs == error_mark_node)
12045               invalid_inputs_p = true;
12046         }
12047       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12048         /* The clobbers are coming next.  */
12049         clobbers_p = true;
12050
12051       /* Look for clobbers.  */
12052       if (clobbers_p
12053           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12054         {
12055           /* Consume the `:' or `::'.  */
12056           cp_lexer_consume_token (parser->lexer);
12057           /* Parse the clobbers.  */
12058           if (cp_lexer_next_token_is_not (parser->lexer,
12059                                           CPP_CLOSE_PAREN))
12060             clobbers = cp_parser_asm_clobber_list (parser);
12061         }
12062     }
12063   /* Look for the closing `)'.  */
12064   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12065     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12066                                            /*consume_paren=*/true);
12067   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12068
12069   if (!invalid_inputs_p && !invalid_outputs_p)
12070     {
12071       /* Create the ASM_EXPR.  */
12072       if (parser->in_function_body)
12073         {
12074           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12075                                       inputs, clobbers);
12076           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12077           if (!extended_p)
12078             {
12079               tree temp = asm_stmt;
12080               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12081                 temp = TREE_OPERAND (temp, 0);
12082
12083               ASM_INPUT_P (temp) = 1;
12084             }
12085         }
12086       else
12087         cgraph_add_asm_node (string);
12088     }
12089 }
12090
12091 /* Declarators [gram.dcl.decl] */
12092
12093 /* Parse an init-declarator.
12094
12095    init-declarator:
12096      declarator initializer [opt]
12097
12098    GNU Extension:
12099
12100    init-declarator:
12101      declarator asm-specification [opt] attributes [opt] initializer [opt]
12102
12103    function-definition:
12104      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12105        function-body
12106      decl-specifier-seq [opt] declarator function-try-block
12107
12108    GNU Extension:
12109
12110    function-definition:
12111      __extension__ function-definition
12112
12113    The DECL_SPECIFIERS apply to this declarator.  Returns a
12114    representation of the entity declared.  If MEMBER_P is TRUE, then
12115    this declarator appears in a class scope.  The new DECL created by
12116    this declarator is returned.
12117
12118    The CHECKS are access checks that should be performed once we know
12119    what entity is being declared (and, therefore, what classes have
12120    befriended it).
12121
12122    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12123    for a function-definition here as well.  If the declarator is a
12124    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12125    be TRUE upon return.  By that point, the function-definition will
12126    have been completely parsed.
12127
12128    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12129    is FALSE.  */
12130
12131 static tree
12132 cp_parser_init_declarator (cp_parser* parser,
12133                            cp_decl_specifier_seq *decl_specifiers,
12134                            VEC (deferred_access_check,gc)* checks,
12135                            bool function_definition_allowed_p,
12136                            bool member_p,
12137                            int declares_class_or_enum,
12138                            bool* function_definition_p)
12139 {
12140   cp_token *token;
12141   cp_declarator *declarator;
12142   tree prefix_attributes;
12143   tree attributes;
12144   tree asm_specification;
12145   tree initializer;
12146   tree decl = NULL_TREE;
12147   tree scope;
12148   bool is_initialized;
12149   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12150      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12151      "(...)".  */
12152   enum cpp_ttype initialization_kind;
12153   bool is_parenthesized_init = false;
12154   bool is_non_constant_init;
12155   int ctor_dtor_or_conv_p;
12156   bool friend_p;
12157   tree pushed_scope = NULL;
12158
12159   /* Gather the attributes that were provided with the
12160      decl-specifiers.  */
12161   prefix_attributes = decl_specifiers->attributes;
12162
12163   /* Assume that this is not the declarator for a function
12164      definition.  */
12165   if (function_definition_p)
12166     *function_definition_p = false;
12167
12168   /* Defer access checks while parsing the declarator; we cannot know
12169      what names are accessible until we know what is being
12170      declared.  */
12171   resume_deferring_access_checks ();
12172
12173   /* Parse the declarator.  */
12174   declarator
12175     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12176                             &ctor_dtor_or_conv_p,
12177                             /*parenthesized_p=*/NULL,
12178                             /*member_p=*/false);
12179   /* Gather up the deferred checks.  */
12180   stop_deferring_access_checks ();
12181
12182   /* If the DECLARATOR was erroneous, there's no need to go
12183      further.  */
12184   if (declarator == cp_error_declarator)
12185     return error_mark_node;
12186
12187   /* Check that the number of template-parameter-lists is OK.  */
12188   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12189     return error_mark_node;
12190
12191   if (declares_class_or_enum & 2)
12192     cp_parser_check_for_definition_in_return_type (declarator,
12193                                                    decl_specifiers->type);
12194
12195   /* Figure out what scope the entity declared by the DECLARATOR is
12196      located in.  `grokdeclarator' sometimes changes the scope, so
12197      we compute it now.  */
12198   scope = get_scope_of_declarator (declarator);
12199
12200   /* If we're allowing GNU extensions, look for an asm-specification
12201      and attributes.  */
12202   if (cp_parser_allow_gnu_extensions_p (parser))
12203     {
12204       /* Look for an asm-specification.  */
12205       asm_specification = cp_parser_asm_specification_opt (parser);
12206       /* And attributes.  */
12207       attributes = cp_parser_attributes_opt (parser);
12208     }
12209   else
12210     {
12211       asm_specification = NULL_TREE;
12212       attributes = NULL_TREE;
12213     }
12214
12215   /* Peek at the next token.  */
12216   token = cp_lexer_peek_token (parser->lexer);
12217   /* Check to see if the token indicates the start of a
12218      function-definition.  */
12219   if (cp_parser_token_starts_function_definition_p (token))
12220     {
12221       if (!function_definition_allowed_p)
12222         {
12223           /* If a function-definition should not appear here, issue an
12224              error message.  */
12225           cp_parser_error (parser,
12226                            "a function-definition is not allowed here");
12227           return error_mark_node;
12228         }
12229       else
12230         {
12231           /* Neither attributes nor an asm-specification are allowed
12232              on a function-definition.  */
12233           if (asm_specification)
12234             error ("an asm-specification is not allowed on a function-definition");
12235           if (attributes)
12236             error ("attributes are not allowed on a function-definition");
12237           /* This is a function-definition.  */
12238           *function_definition_p = true;
12239
12240           /* Parse the function definition.  */
12241           if (member_p)
12242             decl = cp_parser_save_member_function_body (parser,
12243                                                         decl_specifiers,
12244                                                         declarator,
12245                                                         prefix_attributes);
12246           else
12247             decl
12248               = (cp_parser_function_definition_from_specifiers_and_declarator
12249                  (parser, decl_specifiers, prefix_attributes, declarator));
12250
12251           return decl;
12252         }
12253     }
12254
12255   /* [dcl.dcl]
12256
12257      Only in function declarations for constructors, destructors, and
12258      type conversions can the decl-specifier-seq be omitted.
12259
12260      We explicitly postpone this check past the point where we handle
12261      function-definitions because we tolerate function-definitions
12262      that are missing their return types in some modes.  */
12263   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12264     {
12265       cp_parser_error (parser,
12266                        "expected constructor, destructor, or type conversion");
12267       return error_mark_node;
12268     }
12269
12270   /* An `=' or an `(' indicates an initializer.  */
12271   if (token->type == CPP_EQ
12272       || token->type == CPP_OPEN_PAREN)
12273     {
12274       is_initialized = true;
12275       initialization_kind = token->type;
12276     }
12277   else
12278     {
12279       /* If the init-declarator isn't initialized and isn't followed by a
12280          `,' or `;', it's not a valid init-declarator.  */
12281       if (token->type != CPP_COMMA
12282           && token->type != CPP_SEMICOLON)
12283         {
12284           cp_parser_error (parser, "expected initializer");
12285           return error_mark_node;
12286         }
12287       is_initialized = false;
12288       initialization_kind = CPP_EOF;
12289     }
12290
12291   /* Because start_decl has side-effects, we should only call it if we
12292      know we're going ahead.  By this point, we know that we cannot
12293      possibly be looking at any other construct.  */
12294   cp_parser_commit_to_tentative_parse (parser);
12295
12296   /* If the decl specifiers were bad, issue an error now that we're
12297      sure this was intended to be a declarator.  Then continue
12298      declaring the variable(s), as int, to try to cut down on further
12299      errors.  */
12300   if (decl_specifiers->any_specifiers_p
12301       && decl_specifiers->type == error_mark_node)
12302     {
12303       cp_parser_error (parser, "invalid type in declaration");
12304       decl_specifiers->type = integer_type_node;
12305     }
12306
12307   /* Check to see whether or not this declaration is a friend.  */
12308   friend_p = cp_parser_friend_p (decl_specifiers);
12309
12310   /* Enter the newly declared entry in the symbol table.  If we're
12311      processing a declaration in a class-specifier, we wait until
12312      after processing the initializer.  */
12313   if (!member_p)
12314     {
12315       if (parser->in_unbraced_linkage_specification_p)
12316         decl_specifiers->storage_class = sc_extern;
12317       decl = start_decl (declarator, decl_specifiers,
12318                          is_initialized, attributes, prefix_attributes,
12319                          &pushed_scope);
12320     }
12321   else if (scope)
12322     /* Enter the SCOPE.  That way unqualified names appearing in the
12323        initializer will be looked up in SCOPE.  */
12324     pushed_scope = push_scope (scope);
12325
12326   /* Perform deferred access control checks, now that we know in which
12327      SCOPE the declared entity resides.  */
12328   if (!member_p && decl)
12329     {
12330       tree saved_current_function_decl = NULL_TREE;
12331
12332       /* If the entity being declared is a function, pretend that we
12333          are in its scope.  If it is a `friend', it may have access to
12334          things that would not otherwise be accessible.  */
12335       if (TREE_CODE (decl) == FUNCTION_DECL)
12336         {
12337           saved_current_function_decl = current_function_decl;
12338           current_function_decl = decl;
12339         }
12340
12341       /* Perform access checks for template parameters.  */
12342       cp_parser_perform_template_parameter_access_checks (checks);
12343
12344       /* Perform the access control checks for the declarator and the
12345          the decl-specifiers.  */
12346       perform_deferred_access_checks ();
12347
12348       /* Restore the saved value.  */
12349       if (TREE_CODE (decl) == FUNCTION_DECL)
12350         current_function_decl = saved_current_function_decl;
12351     }
12352
12353   /* Parse the initializer.  */
12354   initializer = NULL_TREE;
12355   is_parenthesized_init = false;
12356   is_non_constant_init = true;
12357   if (is_initialized)
12358     {
12359       if (function_declarator_p (declarator))
12360         {
12361            if (initialization_kind == CPP_EQ)
12362              initializer = cp_parser_pure_specifier (parser);
12363            else
12364              {
12365                /* If the declaration was erroneous, we don't really
12366                   know what the user intended, so just silently
12367                   consume the initializer.  */
12368                if (decl != error_mark_node)
12369                  error ("initializer provided for function");
12370                cp_parser_skip_to_closing_parenthesis (parser,
12371                                                       /*recovering=*/true,
12372                                                       /*or_comma=*/false,
12373                                                       /*consume_paren=*/true);
12374              }
12375         }
12376       else
12377         initializer = cp_parser_initializer (parser,
12378                                              &is_parenthesized_init,
12379                                              &is_non_constant_init);
12380     }
12381
12382   /* The old parser allows attributes to appear after a parenthesized
12383      initializer.  Mark Mitchell proposed removing this functionality
12384      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12385      attributes -- but ignores them.  */
12386   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12387     if (cp_parser_attributes_opt (parser))
12388       warning (OPT_Wattributes,
12389                "attributes after parenthesized initializer ignored");
12390
12391   /* For an in-class declaration, use `grokfield' to create the
12392      declaration.  */
12393   if (member_p)
12394     {
12395       if (pushed_scope)
12396         {
12397           pop_scope (pushed_scope);
12398           pushed_scope = false;
12399         }
12400       decl = grokfield (declarator, decl_specifiers,
12401                         initializer, !is_non_constant_init,
12402                         /*asmspec=*/NULL_TREE,
12403                         prefix_attributes);
12404       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12405         cp_parser_save_default_args (parser, decl);
12406     }
12407
12408   /* Finish processing the declaration.  But, skip friend
12409      declarations.  */
12410   if (!friend_p && decl && decl != error_mark_node)
12411     {
12412       cp_finish_decl (decl,
12413                       initializer, !is_non_constant_init,
12414                       asm_specification,
12415                       /* If the initializer is in parentheses, then this is
12416                          a direct-initialization, which means that an
12417                          `explicit' constructor is OK.  Otherwise, an
12418                          `explicit' constructor cannot be used.  */
12419                       ((is_parenthesized_init || !is_initialized)
12420                      ? 0 : LOOKUP_ONLYCONVERTING));
12421     }
12422   else if ((cxx_dialect != cxx98) && friend_p
12423            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12424     /* Core issue #226 (C++0x only): A default template-argument
12425        shall not be specified in a friend class template
12426        declaration. */
12427     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12428                              /*is_partial=*/0, /*is_friend_decl=*/1);
12429
12430   if (!friend_p && pushed_scope)
12431     pop_scope (pushed_scope);
12432
12433   return decl;
12434 }
12435
12436 /* Parse a declarator.
12437
12438    declarator:
12439      direct-declarator
12440      ptr-operator declarator
12441
12442    abstract-declarator:
12443      ptr-operator abstract-declarator [opt]
12444      direct-abstract-declarator
12445
12446    GNU Extensions:
12447
12448    declarator:
12449      attributes [opt] direct-declarator
12450      attributes [opt] ptr-operator declarator
12451
12452    abstract-declarator:
12453      attributes [opt] ptr-operator abstract-declarator [opt]
12454      attributes [opt] direct-abstract-declarator
12455
12456    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12457    detect constructor, destructor or conversion operators. It is set
12458    to -1 if the declarator is a name, and +1 if it is a
12459    function. Otherwise it is set to zero. Usually you just want to
12460    test for >0, but internally the negative value is used.
12461
12462    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12463    a decl-specifier-seq unless it declares a constructor, destructor,
12464    or conversion.  It might seem that we could check this condition in
12465    semantic analysis, rather than parsing, but that makes it difficult
12466    to handle something like `f()'.  We want to notice that there are
12467    no decl-specifiers, and therefore realize that this is an
12468    expression, not a declaration.)
12469
12470    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12471    the declarator is a direct-declarator of the form "(...)".
12472
12473    MEMBER_P is true iff this declarator is a member-declarator.  */
12474
12475 static cp_declarator *
12476 cp_parser_declarator (cp_parser* parser,
12477                       cp_parser_declarator_kind dcl_kind,
12478                       int* ctor_dtor_or_conv_p,
12479                       bool* parenthesized_p,
12480                       bool member_p)
12481 {
12482   cp_token *token;
12483   cp_declarator *declarator;
12484   enum tree_code code;
12485   cp_cv_quals cv_quals;
12486   tree class_type;
12487   tree attributes = NULL_TREE;
12488
12489   /* Assume this is not a constructor, destructor, or type-conversion
12490      operator.  */
12491   if (ctor_dtor_or_conv_p)
12492     *ctor_dtor_or_conv_p = 0;
12493
12494   if (cp_parser_allow_gnu_extensions_p (parser))
12495     attributes = cp_parser_attributes_opt (parser);
12496
12497   /* Peek at the next token.  */
12498   token = cp_lexer_peek_token (parser->lexer);
12499
12500   /* Check for the ptr-operator production.  */
12501   cp_parser_parse_tentatively (parser);
12502   /* Parse the ptr-operator.  */
12503   code = cp_parser_ptr_operator (parser,
12504                                  &class_type,
12505                                  &cv_quals);
12506   /* If that worked, then we have a ptr-operator.  */
12507   if (cp_parser_parse_definitely (parser))
12508     {
12509       /* If a ptr-operator was found, then this declarator was not
12510          parenthesized.  */
12511       if (parenthesized_p)
12512         *parenthesized_p = true;
12513       /* The dependent declarator is optional if we are parsing an
12514          abstract-declarator.  */
12515       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12516         cp_parser_parse_tentatively (parser);
12517
12518       /* Parse the dependent declarator.  */
12519       declarator = cp_parser_declarator (parser, dcl_kind,
12520                                          /*ctor_dtor_or_conv_p=*/NULL,
12521                                          /*parenthesized_p=*/NULL,
12522                                          /*member_p=*/false);
12523
12524       /* If we are parsing an abstract-declarator, we must handle the
12525          case where the dependent declarator is absent.  */
12526       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12527           && !cp_parser_parse_definitely (parser))
12528         declarator = NULL;
12529
12530       declarator = cp_parser_make_indirect_declarator
12531         (code, class_type, cv_quals, declarator);
12532     }
12533   /* Everything else is a direct-declarator.  */
12534   else
12535     {
12536       if (parenthesized_p)
12537         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12538                                                    CPP_OPEN_PAREN);
12539       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12540                                                 ctor_dtor_or_conv_p,
12541                                                 member_p);
12542     }
12543
12544   if (attributes && declarator && declarator != cp_error_declarator)
12545     declarator->attributes = attributes;
12546
12547   return declarator;
12548 }
12549
12550 /* Parse a direct-declarator or direct-abstract-declarator.
12551
12552    direct-declarator:
12553      declarator-id
12554      direct-declarator ( parameter-declaration-clause )
12555        cv-qualifier-seq [opt]
12556        exception-specification [opt]
12557      direct-declarator [ constant-expression [opt] ]
12558      ( declarator )
12559
12560    direct-abstract-declarator:
12561      direct-abstract-declarator [opt]
12562        ( parameter-declaration-clause )
12563        cv-qualifier-seq [opt]
12564        exception-specification [opt]
12565      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12566      ( abstract-declarator )
12567
12568    Returns a representation of the declarator.  DCL_KIND is
12569    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12570    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12571    we are parsing a direct-declarator.  It is
12572    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12573    of ambiguity we prefer an abstract declarator, as per
12574    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12575    cp_parser_declarator.  */
12576
12577 static cp_declarator *
12578 cp_parser_direct_declarator (cp_parser* parser,
12579                              cp_parser_declarator_kind dcl_kind,
12580                              int* ctor_dtor_or_conv_p,
12581                              bool member_p)
12582 {
12583   cp_token *token;
12584   cp_declarator *declarator = NULL;
12585   tree scope = NULL_TREE;
12586   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12587   bool saved_in_declarator_p = parser->in_declarator_p;
12588   bool first = true;
12589   tree pushed_scope = NULL_TREE;
12590
12591   while (true)
12592     {
12593       /* Peek at the next token.  */
12594       token = cp_lexer_peek_token (parser->lexer);
12595       if (token->type == CPP_OPEN_PAREN)
12596         {
12597           /* This is either a parameter-declaration-clause, or a
12598              parenthesized declarator. When we know we are parsing a
12599              named declarator, it must be a parenthesized declarator
12600              if FIRST is true. For instance, `(int)' is a
12601              parameter-declaration-clause, with an omitted
12602              direct-abstract-declarator. But `((*))', is a
12603              parenthesized abstract declarator. Finally, when T is a
12604              template parameter `(T)' is a
12605              parameter-declaration-clause, and not a parenthesized
12606              named declarator.
12607
12608              We first try and parse a parameter-declaration-clause,
12609              and then try a nested declarator (if FIRST is true).
12610
12611              It is not an error for it not to be a
12612              parameter-declaration-clause, even when FIRST is
12613              false. Consider,
12614
12615                int i (int);
12616                int i (3);
12617
12618              The first is the declaration of a function while the
12619              second is a the definition of a variable, including its
12620              initializer.
12621
12622              Having seen only the parenthesis, we cannot know which of
12623              these two alternatives should be selected.  Even more
12624              complex are examples like:
12625
12626                int i (int (a));
12627                int i (int (3));
12628
12629              The former is a function-declaration; the latter is a
12630              variable initialization.
12631
12632              Thus again, we try a parameter-declaration-clause, and if
12633              that fails, we back out and return.  */
12634
12635           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12636             {
12637               cp_parameter_declarator *params;
12638               unsigned saved_num_template_parameter_lists;
12639
12640               /* In a member-declarator, the only valid interpretation
12641                  of a parenthesis is the start of a
12642                  parameter-declaration-clause.  (It is invalid to
12643                  initialize a static data member with a parenthesized
12644                  initializer; only the "=" form of initialization is
12645                  permitted.)  */
12646               if (!member_p)
12647                 cp_parser_parse_tentatively (parser);
12648
12649               /* Consume the `('.  */
12650               cp_lexer_consume_token (parser->lexer);
12651               if (first)
12652                 {
12653                   /* If this is going to be an abstract declarator, we're
12654                      in a declarator and we can't have default args.  */
12655                   parser->default_arg_ok_p = false;
12656                   parser->in_declarator_p = true;
12657                 }
12658
12659               /* Inside the function parameter list, surrounding
12660                  template-parameter-lists do not apply.  */
12661               saved_num_template_parameter_lists
12662                 = parser->num_template_parameter_lists;
12663               parser->num_template_parameter_lists = 0;
12664
12665               /* Parse the parameter-declaration-clause.  */
12666               params = cp_parser_parameter_declaration_clause (parser);
12667
12668               parser->num_template_parameter_lists
12669                 = saved_num_template_parameter_lists;
12670
12671               /* If all went well, parse the cv-qualifier-seq and the
12672                  exception-specification.  */
12673               if (member_p || cp_parser_parse_definitely (parser))
12674                 {
12675                   cp_cv_quals cv_quals;
12676                   tree exception_specification;
12677
12678                   if (ctor_dtor_or_conv_p)
12679                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12680                   first = false;
12681                   /* Consume the `)'.  */
12682                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
12683
12684                   /* Parse the cv-qualifier-seq.  */
12685                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12686                   /* And the exception-specification.  */
12687                   exception_specification
12688                     = cp_parser_exception_specification_opt (parser);
12689
12690                   /* Create the function-declarator.  */
12691                   declarator = make_call_declarator (declarator,
12692                                                      params,
12693                                                      cv_quals,
12694                                                      exception_specification);
12695                   /* Any subsequent parameter lists are to do with
12696                      return type, so are not those of the declared
12697                      function.  */
12698                   parser->default_arg_ok_p = false;
12699
12700                   /* Repeat the main loop.  */
12701                   continue;
12702                 }
12703             }
12704
12705           /* If this is the first, we can try a parenthesized
12706              declarator.  */
12707           if (first)
12708             {
12709               bool saved_in_type_id_in_expr_p;
12710
12711               parser->default_arg_ok_p = saved_default_arg_ok_p;
12712               parser->in_declarator_p = saved_in_declarator_p;
12713
12714               /* Consume the `('.  */
12715               cp_lexer_consume_token (parser->lexer);
12716               /* Parse the nested declarator.  */
12717               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12718               parser->in_type_id_in_expr_p = true;
12719               declarator
12720                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12721                                         /*parenthesized_p=*/NULL,
12722                                         member_p);
12723               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12724               first = false;
12725               /* Expect a `)'.  */
12726               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12727                 declarator = cp_error_declarator;
12728               if (declarator == cp_error_declarator)
12729                 break;
12730
12731               goto handle_declarator;
12732             }
12733           /* Otherwise, we must be done.  */
12734           else
12735             break;
12736         }
12737       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12738                && token->type == CPP_OPEN_SQUARE)
12739         {
12740           /* Parse an array-declarator.  */
12741           tree bounds;
12742
12743           if (ctor_dtor_or_conv_p)
12744             *ctor_dtor_or_conv_p = 0;
12745
12746           first = false;
12747           parser->default_arg_ok_p = false;
12748           parser->in_declarator_p = true;
12749           /* Consume the `['.  */
12750           cp_lexer_consume_token (parser->lexer);
12751           /* Peek at the next token.  */
12752           token = cp_lexer_peek_token (parser->lexer);
12753           /* If the next token is `]', then there is no
12754              constant-expression.  */
12755           if (token->type != CPP_CLOSE_SQUARE)
12756             {
12757               bool non_constant_p;
12758
12759               bounds
12760                 = cp_parser_constant_expression (parser,
12761                                                  /*allow_non_constant=*/true,
12762                                                  &non_constant_p);
12763               if (!non_constant_p)
12764                 bounds = fold_non_dependent_expr (bounds);
12765               /* Normally, the array bound must be an integral constant
12766                  expression.  However, as an extension, we allow VLAs
12767                  in function scopes.  */
12768               else if (!parser->in_function_body)
12769                 {
12770                   error ("array bound is not an integer constant");
12771                   bounds = error_mark_node;
12772                 }
12773             }
12774           else
12775             bounds = NULL_TREE;
12776           /* Look for the closing `]'.  */
12777           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
12778             {
12779               declarator = cp_error_declarator;
12780               break;
12781             }
12782
12783           declarator = make_array_declarator (declarator, bounds);
12784         }
12785       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12786         {
12787           tree qualifying_scope;
12788           tree unqualified_name;
12789           special_function_kind sfk;
12790           bool abstract_ok;
12791           bool pack_expansion_p = false;
12792
12793           /* Parse a declarator-id */
12794           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12795           if (abstract_ok)
12796             {
12797               cp_parser_parse_tentatively (parser);
12798
12799               /* If we see an ellipsis, we should be looking at a
12800                  parameter pack. */
12801               if (token->type == CPP_ELLIPSIS)
12802                 {
12803                   /* Consume the `...' */
12804                   cp_lexer_consume_token (parser->lexer);
12805
12806                   pack_expansion_p = true;
12807                 }
12808             }
12809
12810           unqualified_name
12811             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12812           qualifying_scope = parser->scope;
12813           if (abstract_ok)
12814             {
12815               bool okay = false;
12816
12817               if (!unqualified_name && pack_expansion_p)
12818                 {
12819                   /* Check whether an error occurred. */
12820                   okay = !cp_parser_error_occurred (parser);
12821
12822                   /* We already consumed the ellipsis to mark a
12823                      parameter pack, but we have no way to report it,
12824                      so abort the tentative parse. We will be exiting
12825                      immediately anyway. */
12826                   cp_parser_abort_tentative_parse (parser);
12827                 }
12828               else
12829                 okay = cp_parser_parse_definitely (parser);
12830
12831               if (!okay)
12832                 unqualified_name = error_mark_node;
12833               else if (unqualified_name
12834                        && (qualifying_scope
12835                            || (TREE_CODE (unqualified_name)
12836                                != IDENTIFIER_NODE)))
12837                 {
12838                   cp_parser_error (parser, "expected unqualified-id");
12839                   unqualified_name = error_mark_node;
12840                 }
12841             }
12842
12843           if (!unqualified_name)
12844             return NULL;
12845           if (unqualified_name == error_mark_node)
12846             {
12847               declarator = cp_error_declarator;
12848               pack_expansion_p = false;
12849               declarator->parameter_pack_p = false;
12850               break;
12851             }
12852
12853           if (qualifying_scope && at_namespace_scope_p ()
12854               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12855             {
12856               /* In the declaration of a member of a template class
12857                  outside of the class itself, the SCOPE will sometimes
12858                  be a TYPENAME_TYPE.  For example, given:
12859
12860                  template <typename T>
12861                  int S<T>::R::i = 3;
12862
12863                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
12864                  this context, we must resolve S<T>::R to an ordinary
12865                  type, rather than a typename type.
12866
12867                  The reason we normally avoid resolving TYPENAME_TYPEs
12868                  is that a specialization of `S' might render
12869                  `S<T>::R' not a type.  However, if `S' is
12870                  specialized, then this `i' will not be used, so there
12871                  is no harm in resolving the types here.  */
12872               tree type;
12873
12874               /* Resolve the TYPENAME_TYPE.  */
12875               type = resolve_typename_type (qualifying_scope,
12876                                             /*only_current_p=*/false);
12877               /* If that failed, the declarator is invalid.  */
12878               if (TREE_CODE (type) == TYPENAME_TYPE)
12879                 error ("%<%T::%E%> is not a type",
12880                        TYPE_CONTEXT (qualifying_scope),
12881                        TYPE_IDENTIFIER (qualifying_scope));
12882               qualifying_scope = type;
12883             }
12884
12885           sfk = sfk_none;
12886
12887           if (unqualified_name)
12888             {
12889               tree class_type;
12890
12891               if (qualifying_scope
12892                   && CLASS_TYPE_P (qualifying_scope))
12893                 class_type = qualifying_scope;
12894               else
12895                 class_type = current_class_type;
12896
12897               if (TREE_CODE (unqualified_name) == TYPE_DECL)
12898                 {
12899                   tree name_type = TREE_TYPE (unqualified_name);
12900                   if (class_type && same_type_p (name_type, class_type))
12901                     {
12902                       if (qualifying_scope
12903                           && CLASSTYPE_USE_TEMPLATE (name_type))
12904                         {
12905                           error ("invalid use of constructor as a template");
12906                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12907                                   "name the constructor in a qualified name",
12908                                   class_type,
12909                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12910                                   class_type, name_type);
12911                           declarator = cp_error_declarator;
12912                           break;
12913                         }
12914                       else
12915                         unqualified_name = constructor_name (class_type);
12916                     }
12917                   else
12918                     {
12919                       /* We do not attempt to print the declarator
12920                          here because we do not have enough
12921                          information about its original syntactic
12922                          form.  */
12923                       cp_parser_error (parser, "invalid declarator");
12924                       declarator = cp_error_declarator;
12925                       break;
12926                     }
12927                 }
12928
12929               if (class_type)
12930                 {
12931                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12932                     sfk = sfk_destructor;
12933                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12934                     sfk = sfk_conversion;
12935                   else if (/* There's no way to declare a constructor
12936                               for an anonymous type, even if the type
12937                               got a name for linkage purposes.  */
12938                            !TYPE_WAS_ANONYMOUS (class_type)
12939                            && constructor_name_p (unqualified_name,
12940                                                   class_type))
12941                     {
12942                       unqualified_name = constructor_name (class_type);
12943                       sfk = sfk_constructor;
12944                     }
12945
12946                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
12947                     *ctor_dtor_or_conv_p = -1;
12948                 }
12949             }
12950           declarator = make_id_declarator (qualifying_scope,
12951                                            unqualified_name,
12952                                            sfk);
12953           declarator->id_loc = token->location;
12954           declarator->parameter_pack_p = pack_expansion_p;
12955
12956           if (pack_expansion_p)
12957             maybe_warn_variadic_templates ();
12958
12959         handle_declarator:;
12960           scope = get_scope_of_declarator (declarator);
12961           if (scope)
12962             /* Any names that appear after the declarator-id for a
12963                member are looked up in the containing scope.  */
12964             pushed_scope = push_scope (scope);
12965           parser->in_declarator_p = true;
12966           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12967               || (declarator && declarator->kind == cdk_id))
12968             /* Default args are only allowed on function
12969                declarations.  */
12970             parser->default_arg_ok_p = saved_default_arg_ok_p;
12971           else
12972             parser->default_arg_ok_p = false;
12973
12974           first = false;
12975         }
12976       /* We're done.  */
12977       else
12978         break;
12979     }
12980
12981   /* For an abstract declarator, we might wind up with nothing at this
12982      point.  That's an error; the declarator is not optional.  */
12983   if (!declarator)
12984     cp_parser_error (parser, "expected declarator");
12985
12986   /* If we entered a scope, we must exit it now.  */
12987   if (pushed_scope)
12988     pop_scope (pushed_scope);
12989
12990   parser->default_arg_ok_p = saved_default_arg_ok_p;
12991   parser->in_declarator_p = saved_in_declarator_p;
12992
12993   return declarator;
12994 }
12995
12996 /* Parse a ptr-operator.
12997
12998    ptr-operator:
12999      * cv-qualifier-seq [opt]
13000      &
13001      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13002
13003    GNU Extension:
13004
13005    ptr-operator:
13006      & cv-qualifier-seq [opt]
13007
13008    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13009    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13010    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13011    filled in with the TYPE containing the member.  *CV_QUALS is
13012    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13013    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13014    Note that the tree codes returned by this function have nothing
13015    to do with the types of trees that will be eventually be created
13016    to represent the pointer or reference type being parsed. They are
13017    just constants with suggestive names. */
13018 static enum tree_code
13019 cp_parser_ptr_operator (cp_parser* parser,
13020                         tree* type,
13021                         cp_cv_quals *cv_quals)
13022 {
13023   enum tree_code code = ERROR_MARK;
13024   cp_token *token;
13025
13026   /* Assume that it's not a pointer-to-member.  */
13027   *type = NULL_TREE;
13028   /* And that there are no cv-qualifiers.  */
13029   *cv_quals = TYPE_UNQUALIFIED;
13030
13031   /* Peek at the next token.  */
13032   token = cp_lexer_peek_token (parser->lexer);
13033
13034   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13035   if (token->type == CPP_MULT)
13036     code = INDIRECT_REF;
13037   else if (token->type == CPP_AND)
13038     code = ADDR_EXPR;
13039   else if ((cxx_dialect != cxx98) &&
13040            token->type == CPP_AND_AND) /* C++0x only */
13041     code = NON_LVALUE_EXPR;
13042
13043   if (code != ERROR_MARK)
13044     {
13045       /* Consume the `*', `&' or `&&'.  */
13046       cp_lexer_consume_token (parser->lexer);
13047
13048       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13049          `&', if we are allowing GNU extensions.  (The only qualifier
13050          that can legally appear after `&' is `restrict', but that is
13051          enforced during semantic analysis.  */
13052       if (code == INDIRECT_REF
13053           || cp_parser_allow_gnu_extensions_p (parser))
13054         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13055     }
13056   else
13057     {
13058       /* Try the pointer-to-member case.  */
13059       cp_parser_parse_tentatively (parser);
13060       /* Look for the optional `::' operator.  */
13061       cp_parser_global_scope_opt (parser,
13062                                   /*current_scope_valid_p=*/false);
13063       /* Look for the nested-name specifier.  */
13064       cp_parser_nested_name_specifier (parser,
13065                                        /*typename_keyword_p=*/false,
13066                                        /*check_dependency_p=*/true,
13067                                        /*type_p=*/false,
13068                                        /*is_declaration=*/false);
13069       /* If we found it, and the next token is a `*', then we are
13070          indeed looking at a pointer-to-member operator.  */
13071       if (!cp_parser_error_occurred (parser)
13072           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13073         {
13074           /* Indicate that the `*' operator was used.  */
13075           code = INDIRECT_REF;
13076
13077           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13078             error ("%qD is a namespace", parser->scope);
13079           else
13080             {
13081               /* The type of which the member is a member is given by the
13082                  current SCOPE.  */
13083               *type = parser->scope;
13084               /* The next name will not be qualified.  */
13085               parser->scope = NULL_TREE;
13086               parser->qualifying_scope = NULL_TREE;
13087               parser->object_scope = NULL_TREE;
13088               /* Look for the optional cv-qualifier-seq.  */
13089               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13090             }
13091         }
13092       /* If that didn't work we don't have a ptr-operator.  */
13093       if (!cp_parser_parse_definitely (parser))
13094         cp_parser_error (parser, "expected ptr-operator");
13095     }
13096
13097   return code;
13098 }
13099
13100 /* Parse an (optional) cv-qualifier-seq.
13101
13102    cv-qualifier-seq:
13103      cv-qualifier cv-qualifier-seq [opt]
13104
13105    cv-qualifier:
13106      const
13107      volatile
13108
13109    GNU Extension:
13110
13111    cv-qualifier:
13112      __restrict__
13113
13114    Returns a bitmask representing the cv-qualifiers.  */
13115
13116 static cp_cv_quals
13117 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13118 {
13119   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13120
13121   while (true)
13122     {
13123       cp_token *token;
13124       cp_cv_quals cv_qualifier;
13125
13126       /* Peek at the next token.  */
13127       token = cp_lexer_peek_token (parser->lexer);
13128       /* See if it's a cv-qualifier.  */
13129       switch (token->keyword)
13130         {
13131         case RID_CONST:
13132           cv_qualifier = TYPE_QUAL_CONST;
13133           break;
13134
13135         case RID_VOLATILE:
13136           cv_qualifier = TYPE_QUAL_VOLATILE;
13137           break;
13138
13139         case RID_RESTRICT:
13140           cv_qualifier = TYPE_QUAL_RESTRICT;
13141           break;
13142
13143         default:
13144           cv_qualifier = TYPE_UNQUALIFIED;
13145           break;
13146         }
13147
13148       if (!cv_qualifier)
13149         break;
13150
13151       if (cv_quals & cv_qualifier)
13152         {
13153           error ("duplicate cv-qualifier");
13154           cp_lexer_purge_token (parser->lexer);
13155         }
13156       else
13157         {
13158           cp_lexer_consume_token (parser->lexer);
13159           cv_quals |= cv_qualifier;
13160         }
13161     }
13162
13163   return cv_quals;
13164 }
13165
13166 /* Parse a declarator-id.
13167
13168    declarator-id:
13169      id-expression
13170      :: [opt] nested-name-specifier [opt] type-name
13171
13172    In the `id-expression' case, the value returned is as for
13173    cp_parser_id_expression if the id-expression was an unqualified-id.
13174    If the id-expression was a qualified-id, then a SCOPE_REF is
13175    returned.  The first operand is the scope (either a NAMESPACE_DECL
13176    or TREE_TYPE), but the second is still just a representation of an
13177    unqualified-id.  */
13178
13179 static tree
13180 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13181 {
13182   tree id;
13183   /* The expression must be an id-expression.  Assume that qualified
13184      names are the names of types so that:
13185
13186        template <class T>
13187        int S<T>::R::i = 3;
13188
13189      will work; we must treat `S<T>::R' as the name of a type.
13190      Similarly, assume that qualified names are templates, where
13191      required, so that:
13192
13193        template <class T>
13194        int S<T>::R<T>::i = 3;
13195
13196      will work, too.  */
13197   id = cp_parser_id_expression (parser,
13198                                 /*template_keyword_p=*/false,
13199                                 /*check_dependency_p=*/false,
13200                                 /*template_p=*/NULL,
13201                                 /*declarator_p=*/true,
13202                                 optional_p);
13203   if (id && BASELINK_P (id))
13204     id = BASELINK_FUNCTIONS (id);
13205   return id;
13206 }
13207
13208 /* Parse a type-id.
13209
13210    type-id:
13211      type-specifier-seq abstract-declarator [opt]
13212
13213    Returns the TYPE specified.  */
13214
13215 static tree
13216 cp_parser_type_id (cp_parser* parser)
13217 {
13218   cp_decl_specifier_seq type_specifier_seq;
13219   cp_declarator *abstract_declarator;
13220
13221   /* Parse the type-specifier-seq.  */
13222   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13223                                 &type_specifier_seq);
13224   if (type_specifier_seq.type == error_mark_node)
13225     return error_mark_node;
13226
13227   /* There might or might not be an abstract declarator.  */
13228   cp_parser_parse_tentatively (parser);
13229   /* Look for the declarator.  */
13230   abstract_declarator
13231     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13232                             /*parenthesized_p=*/NULL,
13233                             /*member_p=*/false);
13234   /* Check to see if there really was a declarator.  */
13235   if (!cp_parser_parse_definitely (parser))
13236     abstract_declarator = NULL;
13237
13238   return groktypename (&type_specifier_seq, abstract_declarator);
13239 }
13240
13241 /* Parse a type-specifier-seq.
13242
13243    type-specifier-seq:
13244      type-specifier type-specifier-seq [opt]
13245
13246    GNU extension:
13247
13248    type-specifier-seq:
13249      attributes type-specifier-seq [opt]
13250
13251    If IS_CONDITION is true, we are at the start of a "condition",
13252    e.g., we've just seen "if (".
13253
13254    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13255
13256 static void
13257 cp_parser_type_specifier_seq (cp_parser* parser,
13258                               bool is_condition,
13259                               cp_decl_specifier_seq *type_specifier_seq)
13260 {
13261   bool seen_type_specifier = false;
13262   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13263
13264   /* Clear the TYPE_SPECIFIER_SEQ.  */
13265   clear_decl_specs (type_specifier_seq);
13266
13267   /* Parse the type-specifiers and attributes.  */
13268   while (true)
13269     {
13270       tree type_specifier;
13271       bool is_cv_qualifier;
13272
13273       /* Check for attributes first.  */
13274       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13275         {
13276           type_specifier_seq->attributes =
13277             chainon (type_specifier_seq->attributes,
13278                      cp_parser_attributes_opt (parser));
13279           continue;
13280         }
13281
13282       /* Look for the type-specifier.  */
13283       type_specifier = cp_parser_type_specifier (parser,
13284                                                  flags,
13285                                                  type_specifier_seq,
13286                                                  /*is_declaration=*/false,
13287                                                  NULL,
13288                                                  &is_cv_qualifier);
13289       if (!type_specifier)
13290         {
13291           /* If the first type-specifier could not be found, this is not a
13292              type-specifier-seq at all.  */
13293           if (!seen_type_specifier)
13294             {
13295               cp_parser_error (parser, "expected type-specifier");
13296               type_specifier_seq->type = error_mark_node;
13297               return;
13298             }
13299           /* If subsequent type-specifiers could not be found, the
13300              type-specifier-seq is complete.  */
13301           break;
13302         }
13303
13304       seen_type_specifier = true;
13305       /* The standard says that a condition can be:
13306
13307             type-specifier-seq declarator = assignment-expression
13308
13309          However, given:
13310
13311            struct S {};
13312            if (int S = ...)
13313
13314          we should treat the "S" as a declarator, not as a
13315          type-specifier.  The standard doesn't say that explicitly for
13316          type-specifier-seq, but it does say that for
13317          decl-specifier-seq in an ordinary declaration.  Perhaps it
13318          would be clearer just to allow a decl-specifier-seq here, and
13319          then add a semantic restriction that if any decl-specifiers
13320          that are not type-specifiers appear, the program is invalid.  */
13321       if (is_condition && !is_cv_qualifier)
13322         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13323     }
13324
13325   cp_parser_check_decl_spec (type_specifier_seq);
13326 }
13327
13328 /* Parse a parameter-declaration-clause.
13329
13330    parameter-declaration-clause:
13331      parameter-declaration-list [opt] ... [opt]
13332      parameter-declaration-list , ...
13333
13334    Returns a representation for the parameter declarations.  A return
13335    value of NULL indicates a parameter-declaration-clause consisting
13336    only of an ellipsis.  */
13337
13338 static cp_parameter_declarator *
13339 cp_parser_parameter_declaration_clause (cp_parser* parser)
13340 {
13341   cp_parameter_declarator *parameters;
13342   cp_token *token;
13343   bool ellipsis_p;
13344   bool is_error;
13345
13346   /* Peek at the next token.  */
13347   token = cp_lexer_peek_token (parser->lexer);
13348   /* Check for trivial parameter-declaration-clauses.  */
13349   if (token->type == CPP_ELLIPSIS)
13350     {
13351       /* Consume the `...' token.  */
13352       cp_lexer_consume_token (parser->lexer);
13353       return NULL;
13354     }
13355   else if (token->type == CPP_CLOSE_PAREN)
13356     /* There are no parameters.  */
13357     {
13358 #ifndef NO_IMPLICIT_EXTERN_C
13359       if (in_system_header && current_class_type == NULL
13360           && current_lang_name == lang_name_c)
13361         return NULL;
13362       else
13363 #endif
13364         return no_parameters;
13365     }
13366   /* Check for `(void)', too, which is a special case.  */
13367   else if (token->keyword == RID_VOID
13368            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13369                == CPP_CLOSE_PAREN))
13370     {
13371       /* Consume the `void' token.  */
13372       cp_lexer_consume_token (parser->lexer);
13373       /* There are no parameters.  */
13374       return no_parameters;
13375     }
13376
13377   /* Parse the parameter-declaration-list.  */
13378   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13379   /* If a parse error occurred while parsing the
13380      parameter-declaration-list, then the entire
13381      parameter-declaration-clause is erroneous.  */
13382   if (is_error)
13383     return NULL;
13384
13385   /* Peek at the next token.  */
13386   token = cp_lexer_peek_token (parser->lexer);
13387   /* If it's a `,', the clause should terminate with an ellipsis.  */
13388   if (token->type == CPP_COMMA)
13389     {
13390       /* Consume the `,'.  */
13391       cp_lexer_consume_token (parser->lexer);
13392       /* Expect an ellipsis.  */
13393       ellipsis_p
13394         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13395     }
13396   /* It might also be `...' if the optional trailing `,' was
13397      omitted.  */
13398   else if (token->type == CPP_ELLIPSIS)
13399     {
13400       /* Consume the `...' token.  */
13401       cp_lexer_consume_token (parser->lexer);
13402       /* And remember that we saw it.  */
13403       ellipsis_p = true;
13404     }
13405   else
13406     ellipsis_p = false;
13407
13408   /* Finish the parameter list.  */
13409   if (parameters && ellipsis_p)
13410     parameters->ellipsis_p = true;
13411
13412   return parameters;
13413 }
13414
13415 /* Parse a parameter-declaration-list.
13416
13417    parameter-declaration-list:
13418      parameter-declaration
13419      parameter-declaration-list , parameter-declaration
13420
13421    Returns a representation of the parameter-declaration-list, as for
13422    cp_parser_parameter_declaration_clause.  However, the
13423    `void_list_node' is never appended to the list.  Upon return,
13424    *IS_ERROR will be true iff an error occurred.  */
13425
13426 static cp_parameter_declarator *
13427 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13428 {
13429   cp_parameter_declarator *parameters = NULL;
13430   cp_parameter_declarator **tail = &parameters;
13431   bool saved_in_unbraced_linkage_specification_p;
13432
13433   /* Assume all will go well.  */
13434   *is_error = false;
13435   /* The special considerations that apply to a function within an
13436      unbraced linkage specifications do not apply to the parameters
13437      to the function.  */
13438   saved_in_unbraced_linkage_specification_p 
13439     = parser->in_unbraced_linkage_specification_p;
13440   parser->in_unbraced_linkage_specification_p = false;
13441
13442   /* Look for more parameters.  */
13443   while (true)
13444     {
13445       cp_parameter_declarator *parameter;
13446       bool parenthesized_p;
13447       /* Parse the parameter.  */
13448       parameter
13449         = cp_parser_parameter_declaration (parser,
13450                                            /*template_parm_p=*/false,
13451                                            &parenthesized_p);
13452
13453       /* If a parse error occurred parsing the parameter declaration,
13454          then the entire parameter-declaration-list is erroneous.  */
13455       if (!parameter)
13456         {
13457           *is_error = true;
13458           parameters = NULL;
13459           break;
13460         }
13461       /* Add the new parameter to the list.  */
13462       *tail = parameter;
13463       tail = &parameter->next;
13464
13465       /* Peek at the next token.  */
13466       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13467           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13468           /* These are for Objective-C++ */
13469           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13470           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13471         /* The parameter-declaration-list is complete.  */
13472         break;
13473       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13474         {
13475           cp_token *token;
13476
13477           /* Peek at the next token.  */
13478           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13479           /* If it's an ellipsis, then the list is complete.  */
13480           if (token->type == CPP_ELLIPSIS)
13481             break;
13482           /* Otherwise, there must be more parameters.  Consume the
13483              `,'.  */
13484           cp_lexer_consume_token (parser->lexer);
13485           /* When parsing something like:
13486
13487                 int i(float f, double d)
13488
13489              we can tell after seeing the declaration for "f" that we
13490              are not looking at an initialization of a variable "i",
13491              but rather at the declaration of a function "i".
13492
13493              Due to the fact that the parsing of template arguments
13494              (as specified to a template-id) requires backtracking we
13495              cannot use this technique when inside a template argument
13496              list.  */
13497           if (!parser->in_template_argument_list_p
13498               && !parser->in_type_id_in_expr_p
13499               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13500               /* However, a parameter-declaration of the form
13501                  "foat(f)" (which is a valid declaration of a
13502                  parameter "f") can also be interpreted as an
13503                  expression (the conversion of "f" to "float").  */
13504               && !parenthesized_p)
13505             cp_parser_commit_to_tentative_parse (parser);
13506         }
13507       else
13508         {
13509           cp_parser_error (parser, "expected %<,%> or %<...%>");
13510           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13511             cp_parser_skip_to_closing_parenthesis (parser,
13512                                                    /*recovering=*/true,
13513                                                    /*or_comma=*/false,
13514                                                    /*consume_paren=*/false);
13515           break;
13516         }
13517     }
13518
13519   parser->in_unbraced_linkage_specification_p
13520     = saved_in_unbraced_linkage_specification_p;
13521
13522   return parameters;
13523 }
13524
13525 /* Parse a parameter declaration.
13526
13527    parameter-declaration:
13528      decl-specifier-seq ... [opt] declarator
13529      decl-specifier-seq declarator = assignment-expression
13530      decl-specifier-seq ... [opt] abstract-declarator [opt]
13531      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13532
13533    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13534    declares a template parameter.  (In that case, a non-nested `>'
13535    token encountered during the parsing of the assignment-expression
13536    is not interpreted as a greater-than operator.)
13537
13538    Returns a representation of the parameter, or NULL if an error
13539    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13540    true iff the declarator is of the form "(p)".  */
13541
13542 static cp_parameter_declarator *
13543 cp_parser_parameter_declaration (cp_parser *parser,
13544                                  bool template_parm_p,
13545                                  bool *parenthesized_p)
13546 {
13547   int declares_class_or_enum;
13548   bool greater_than_is_operator_p;
13549   cp_decl_specifier_seq decl_specifiers;
13550   cp_declarator *declarator;
13551   tree default_argument;
13552   cp_token *token;
13553   const char *saved_message;
13554
13555   /* In a template parameter, `>' is not an operator.
13556
13557      [temp.param]
13558
13559      When parsing a default template-argument for a non-type
13560      template-parameter, the first non-nested `>' is taken as the end
13561      of the template parameter-list rather than a greater-than
13562      operator.  */
13563   greater_than_is_operator_p = !template_parm_p;
13564
13565   /* Type definitions may not appear in parameter types.  */
13566   saved_message = parser->type_definition_forbidden_message;
13567   parser->type_definition_forbidden_message
13568     = "types may not be defined in parameter types";
13569
13570   /* Parse the declaration-specifiers.  */
13571   cp_parser_decl_specifier_seq (parser,
13572                                 CP_PARSER_FLAGS_NONE,
13573                                 &decl_specifiers,
13574                                 &declares_class_or_enum);
13575   /* If an error occurred, there's no reason to attempt to parse the
13576      rest of the declaration.  */
13577   if (cp_parser_error_occurred (parser))
13578     {
13579       parser->type_definition_forbidden_message = saved_message;
13580       return NULL;
13581     }
13582
13583   /* Peek at the next token.  */
13584   token = cp_lexer_peek_token (parser->lexer);
13585
13586   /* If the next token is a `)', `,', `=', `>', or `...', then there
13587      is no declarator. However, when variadic templates are enabled,
13588      there may be a declarator following `...'.  */
13589   if (token->type == CPP_CLOSE_PAREN
13590       || token->type == CPP_COMMA
13591       || token->type == CPP_EQ
13592       || token->type == CPP_GREATER)
13593     {
13594       declarator = NULL;
13595       if (parenthesized_p)
13596         *parenthesized_p = false;
13597     }
13598   /* Otherwise, there should be a declarator.  */
13599   else
13600     {
13601       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13602       parser->default_arg_ok_p = false;
13603
13604       /* After seeing a decl-specifier-seq, if the next token is not a
13605          "(", there is no possibility that the code is a valid
13606          expression.  Therefore, if parsing tentatively, we commit at
13607          this point.  */
13608       if (!parser->in_template_argument_list_p
13609           /* In an expression context, having seen:
13610
13611                (int((char ...
13612
13613              we cannot be sure whether we are looking at a
13614              function-type (taking a "char" as a parameter) or a cast
13615              of some object of type "char" to "int".  */
13616           && !parser->in_type_id_in_expr_p
13617           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13618           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13619         cp_parser_commit_to_tentative_parse (parser);
13620       /* Parse the declarator.  */
13621       declarator = cp_parser_declarator (parser,
13622                                          CP_PARSER_DECLARATOR_EITHER,
13623                                          /*ctor_dtor_or_conv_p=*/NULL,
13624                                          parenthesized_p,
13625                                          /*member_p=*/false);
13626       parser->default_arg_ok_p = saved_default_arg_ok_p;
13627       /* After the declarator, allow more attributes.  */
13628       decl_specifiers.attributes
13629         = chainon (decl_specifiers.attributes,
13630                    cp_parser_attributes_opt (parser));
13631     }
13632
13633   /* If the next token is an ellipsis, and we have not seen a
13634      declarator name, and the type of the declarator contains parameter
13635      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13636      a parameter pack expansion expression. Otherwise, leave the
13637      ellipsis for a C-style variadic function. */
13638   token = cp_lexer_peek_token (parser->lexer);
13639   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13640     {
13641       tree type = decl_specifiers.type;
13642
13643       if (type && DECL_P (type))
13644         type = TREE_TYPE (type);
13645
13646       if (type
13647           && TREE_CODE (type) != TYPE_PACK_EXPANSION
13648           && declarator_can_be_parameter_pack (declarator)
13649           && (!declarator || !declarator->parameter_pack_p)
13650           && uses_parameter_packs (type))
13651         {
13652           /* Consume the `...'. */
13653           cp_lexer_consume_token (parser->lexer);
13654           maybe_warn_variadic_templates ();
13655           
13656           /* Build a pack expansion type */
13657           if (declarator)
13658             declarator->parameter_pack_p = true;
13659           else
13660             decl_specifiers.type = make_pack_expansion (type);
13661         }
13662     }
13663
13664   /* The restriction on defining new types applies only to the type
13665      of the parameter, not to the default argument.  */
13666   parser->type_definition_forbidden_message = saved_message;
13667
13668   /* If the next token is `=', then process a default argument.  */
13669   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13670     {
13671       /* Consume the `='.  */
13672       cp_lexer_consume_token (parser->lexer);
13673
13674       /* If we are defining a class, then the tokens that make up the
13675          default argument must be saved and processed later.  */
13676       if (!template_parm_p && at_class_scope_p ()
13677           && TYPE_BEING_DEFINED (current_class_type))
13678         {
13679           unsigned depth = 0;
13680           cp_token *first_token;
13681           cp_token *token;
13682
13683           /* Add tokens until we have processed the entire default
13684              argument.  We add the range [first_token, token).  */
13685           first_token = cp_lexer_peek_token (parser->lexer);
13686           while (true)
13687             {
13688               bool done = false;
13689
13690               /* Peek at the next token.  */
13691               token = cp_lexer_peek_token (parser->lexer);
13692               /* What we do depends on what token we have.  */
13693               switch (token->type)
13694                 {
13695                   /* In valid code, a default argument must be
13696                      immediately followed by a `,' `)', or `...'.  */
13697                 case CPP_COMMA:
13698                 case CPP_CLOSE_PAREN:
13699                 case CPP_ELLIPSIS:
13700                   /* If we run into a non-nested `;', `}', or `]',
13701                      then the code is invalid -- but the default
13702                      argument is certainly over.  */
13703                 case CPP_SEMICOLON:
13704                 case CPP_CLOSE_BRACE:
13705                 case CPP_CLOSE_SQUARE:
13706                   if (depth == 0)
13707                     done = true;
13708                   /* Update DEPTH, if necessary.  */
13709                   else if (token->type == CPP_CLOSE_PAREN
13710                            || token->type == CPP_CLOSE_BRACE
13711                            || token->type == CPP_CLOSE_SQUARE)
13712                     --depth;
13713                   break;
13714
13715                 case CPP_OPEN_PAREN:
13716                 case CPP_OPEN_SQUARE:
13717                 case CPP_OPEN_BRACE:
13718                   ++depth;
13719                   break;
13720
13721                 case CPP_RSHIFT:
13722                   if (cxx_dialect == cxx98)
13723                     break;
13724                   /* Fall through for C++0x, which treats the `>>'
13725                      operator like two `>' tokens in certain
13726                      cases.  */
13727
13728                 case CPP_GREATER:
13729                   /* If we see a non-nested `>', and `>' is not an
13730                      operator, then it marks the end of the default
13731                      argument.  */
13732                   if (!depth && !greater_than_is_operator_p)
13733                     done = true;
13734                   break;
13735
13736                   /* If we run out of tokens, issue an error message.  */
13737                 case CPP_EOF:
13738                 case CPP_PRAGMA_EOL:
13739                   error ("file ends in default argument");
13740                   done = true;
13741                   break;
13742
13743                 case CPP_NAME:
13744                 case CPP_SCOPE:
13745                   /* In these cases, we should look for template-ids.
13746                      For example, if the default argument is
13747                      `X<int, double>()', we need to do name lookup to
13748                      figure out whether or not `X' is a template; if
13749                      so, the `,' does not end the default argument.
13750
13751                      That is not yet done.  */
13752                   break;
13753
13754                 default:
13755                   break;
13756                 }
13757
13758               /* If we've reached the end, stop.  */
13759               if (done)
13760                 break;
13761
13762               /* Add the token to the token block.  */
13763               token = cp_lexer_consume_token (parser->lexer);
13764             }
13765
13766           /* Create a DEFAULT_ARG to represent the unparsed default
13767              argument.  */
13768           default_argument = make_node (DEFAULT_ARG);
13769           DEFARG_TOKENS (default_argument)
13770             = cp_token_cache_new (first_token, token);
13771           DEFARG_INSTANTIATIONS (default_argument) = NULL;
13772         }
13773       /* Outside of a class definition, we can just parse the
13774          assignment-expression.  */
13775       else
13776         default_argument 
13777           = cp_parser_default_argument (parser, template_parm_p);
13778
13779       if (!parser->default_arg_ok_p)
13780         {
13781           if (!flag_pedantic_errors)
13782             warning (0, "deprecated use of default argument for parameter of non-function");
13783           else
13784             {
13785               error ("default arguments are only permitted for function parameters");
13786               default_argument = NULL_TREE;
13787             }
13788         }
13789       else if ((declarator && declarator->parameter_pack_p)
13790                || (decl_specifiers.type
13791                    && PACK_EXPANSION_P (decl_specifiers.type)))
13792         {
13793           const char* kind = template_parm_p? "template " : "";
13794           
13795           /* Find the name of the parameter pack.  */     
13796           cp_declarator *id_declarator = declarator;
13797           while (id_declarator && id_declarator->kind != cdk_id)
13798             id_declarator = id_declarator->declarator;
13799           
13800           if (id_declarator && id_declarator->kind == cdk_id)
13801             error ("%sparameter pack %qD cannot have a default argument",
13802                    kind, id_declarator->u.id.unqualified_name);
13803           else
13804             error ("%sparameter pack cannot have a default argument",
13805                    kind);
13806           
13807           default_argument = NULL_TREE;
13808         }
13809     }
13810   else
13811     default_argument = NULL_TREE;
13812
13813   return make_parameter_declarator (&decl_specifiers,
13814                                     declarator,
13815                                     default_argument);
13816 }
13817
13818 /* Parse a default argument and return it.
13819
13820    TEMPLATE_PARM_P is true if this is a default argument for a
13821    non-type template parameter.  */
13822 static tree
13823 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
13824 {
13825   tree default_argument = NULL_TREE;
13826   bool saved_greater_than_is_operator_p;
13827   bool saved_local_variables_forbidden_p;
13828
13829   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13830      set correctly.  */
13831   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
13832   parser->greater_than_is_operator_p = !template_parm_p;
13833   /* Local variable names (and the `this' keyword) may not
13834      appear in a default argument.  */
13835   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13836   parser->local_variables_forbidden_p = true;
13837   /* The default argument expression may cause implicitly
13838      defined member functions to be synthesized, which will
13839      result in garbage collection.  We must treat this
13840      situation as if we were within the body of function so as
13841      to avoid collecting live data on the stack.  */
13842   ++function_depth;
13843   /* Parse the assignment-expression.  */
13844   if (template_parm_p)
13845     push_deferring_access_checks (dk_no_deferred);
13846   default_argument
13847     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13848   if (template_parm_p)
13849     pop_deferring_access_checks ();
13850   /* Restore saved state.  */
13851   --function_depth;
13852   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
13853   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13854
13855   return default_argument;
13856 }
13857
13858 /* Parse a function-body.
13859
13860    function-body:
13861      compound_statement  */
13862
13863 static void
13864 cp_parser_function_body (cp_parser *parser)
13865 {
13866   cp_parser_compound_statement (parser, NULL, false);
13867 }
13868
13869 /* Parse a ctor-initializer-opt followed by a function-body.  Return
13870    true if a ctor-initializer was present.  */
13871
13872 static bool
13873 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13874 {
13875   tree body;
13876   bool ctor_initializer_p;
13877
13878   /* Begin the function body.  */
13879   body = begin_function_body ();
13880   /* Parse the optional ctor-initializer.  */
13881   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13882   /* Parse the function-body.  */
13883   cp_parser_function_body (parser);
13884   /* Finish the function body.  */
13885   finish_function_body (body);
13886
13887   return ctor_initializer_p;
13888 }
13889
13890 /* Parse an initializer.
13891
13892    initializer:
13893      = initializer-clause
13894      ( expression-list )
13895
13896    Returns an expression representing the initializer.  If no
13897    initializer is present, NULL_TREE is returned.
13898
13899    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13900    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
13901    set to FALSE if there is no initializer present.  If there is an
13902    initializer, and it is not a constant-expression, *NON_CONSTANT_P
13903    is set to true; otherwise it is set to false.  */
13904
13905 static tree
13906 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13907                        bool* non_constant_p)
13908 {
13909   cp_token *token;
13910   tree init;
13911
13912   /* Peek at the next token.  */
13913   token = cp_lexer_peek_token (parser->lexer);
13914
13915   /* Let our caller know whether or not this initializer was
13916      parenthesized.  */
13917   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13918   /* Assume that the initializer is constant.  */
13919   *non_constant_p = false;
13920
13921   if (token->type == CPP_EQ)
13922     {
13923       /* Consume the `='.  */
13924       cp_lexer_consume_token (parser->lexer);
13925       /* Parse the initializer-clause.  */
13926       init = cp_parser_initializer_clause (parser, non_constant_p);
13927     }
13928   else if (token->type == CPP_OPEN_PAREN)
13929     init = cp_parser_parenthesized_expression_list (parser, false,
13930                                                     /*cast_p=*/false,
13931                                                     /*allow_expansion_p=*/true,
13932                                                     non_constant_p);
13933   else
13934     {
13935       /* Anything else is an error.  */
13936       cp_parser_error (parser, "expected initializer");
13937       init = error_mark_node;
13938     }
13939
13940   return init;
13941 }
13942
13943 /* Parse an initializer-clause.
13944
13945    initializer-clause:
13946      assignment-expression
13947      { initializer-list , [opt] }
13948      { }
13949
13950    Returns an expression representing the initializer.
13951
13952    If the `assignment-expression' production is used the value
13953    returned is simply a representation for the expression.
13954
13955    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
13956    the elements of the initializer-list (or NULL, if the last
13957    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
13958    NULL_TREE.  There is no way to detect whether or not the optional
13959    trailing `,' was provided.  NON_CONSTANT_P is as for
13960    cp_parser_initializer.  */
13961
13962 static tree
13963 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13964 {
13965   tree initializer;
13966
13967   /* Assume the expression is constant.  */
13968   *non_constant_p = false;
13969
13970   /* If it is not a `{', then we are looking at an
13971      assignment-expression.  */
13972   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13973     {
13974       initializer
13975         = cp_parser_constant_expression (parser,
13976                                         /*allow_non_constant_p=*/true,
13977                                         non_constant_p);
13978       if (!*non_constant_p)
13979         initializer = fold_non_dependent_expr (initializer);
13980     }
13981   else
13982     {
13983       /* Consume the `{' token.  */
13984       cp_lexer_consume_token (parser->lexer);
13985       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
13986       initializer = make_node (CONSTRUCTOR);
13987       /* If it's not a `}', then there is a non-trivial initializer.  */
13988       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13989         {
13990           /* Parse the initializer list.  */
13991           CONSTRUCTOR_ELTS (initializer)
13992             = cp_parser_initializer_list (parser, non_constant_p);
13993           /* A trailing `,' token is allowed.  */
13994           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13995             cp_lexer_consume_token (parser->lexer);
13996         }
13997       /* Now, there should be a trailing `}'.  */
13998       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
13999     }
14000
14001   return initializer;
14002 }
14003
14004 /* Parse an initializer-list.
14005
14006    initializer-list:
14007      initializer-clause ... [opt]
14008      initializer-list , initializer-clause ... [opt]
14009
14010    GNU Extension:
14011
14012    initializer-list:
14013      identifier : initializer-clause
14014      initializer-list, identifier : initializer-clause
14015
14016    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14017    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14018    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14019    as for cp_parser_initializer.  */
14020
14021 static VEC(constructor_elt,gc) *
14022 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14023 {
14024   VEC(constructor_elt,gc) *v = NULL;
14025
14026   /* Assume all of the expressions are constant.  */
14027   *non_constant_p = false;
14028
14029   /* Parse the rest of the list.  */
14030   while (true)
14031     {
14032       cp_token *token;
14033       tree identifier;
14034       tree initializer;
14035       bool clause_non_constant_p;
14036
14037       /* If the next token is an identifier and the following one is a
14038          colon, we are looking at the GNU designated-initializer
14039          syntax.  */
14040       if (cp_parser_allow_gnu_extensions_p (parser)
14041           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14042           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14043         {
14044           /* Warn the user that they are using an extension.  */
14045           if (pedantic)
14046             pedwarn ("ISO C++ does not allow designated initializers");
14047           /* Consume the identifier.  */
14048           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14049           /* Consume the `:'.  */
14050           cp_lexer_consume_token (parser->lexer);
14051         }
14052       else
14053         identifier = NULL_TREE;
14054
14055       /* Parse the initializer.  */
14056       initializer = cp_parser_initializer_clause (parser,
14057                                                   &clause_non_constant_p);
14058       /* If any clause is non-constant, so is the entire initializer.  */
14059       if (clause_non_constant_p)
14060         *non_constant_p = true;
14061
14062       /* If we have an ellipsis, this is an initializer pack
14063          expansion.  */
14064       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14065         {
14066           /* Consume the `...'.  */
14067           cp_lexer_consume_token (parser->lexer);
14068
14069           /* Turn the initializer into an initializer expansion.  */
14070           initializer = make_pack_expansion (initializer);
14071         }
14072
14073       /* Add it to the vector.  */
14074       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14075
14076       /* If the next token is not a comma, we have reached the end of
14077          the list.  */
14078       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14079         break;
14080
14081       /* Peek at the next token.  */
14082       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14083       /* If the next token is a `}', then we're still done.  An
14084          initializer-clause can have a trailing `,' after the
14085          initializer-list and before the closing `}'.  */
14086       if (token->type == CPP_CLOSE_BRACE)
14087         break;
14088
14089       /* Consume the `,' token.  */
14090       cp_lexer_consume_token (parser->lexer);
14091     }
14092
14093   return v;
14094 }
14095
14096 /* Classes [gram.class] */
14097
14098 /* Parse a class-name.
14099
14100    class-name:
14101      identifier
14102      template-id
14103
14104    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14105    to indicate that names looked up in dependent types should be
14106    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14107    keyword has been used to indicate that the name that appears next
14108    is a template.  TAG_TYPE indicates the explicit tag given before
14109    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14110    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14111    is the class being defined in a class-head.
14112
14113    Returns the TYPE_DECL representing the class.  */
14114
14115 static tree
14116 cp_parser_class_name (cp_parser *parser,
14117                       bool typename_keyword_p,
14118                       bool template_keyword_p,
14119                       enum tag_types tag_type,
14120                       bool check_dependency_p,
14121                       bool class_head_p,
14122                       bool is_declaration)
14123 {
14124   tree decl;
14125   tree scope;
14126   bool typename_p;
14127   cp_token *token;
14128
14129   /* All class-names start with an identifier.  */
14130   token = cp_lexer_peek_token (parser->lexer);
14131   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14132     {
14133       cp_parser_error (parser, "expected class-name");
14134       return error_mark_node;
14135     }
14136
14137   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14138      to a template-id, so we save it here.  */
14139   scope = parser->scope;
14140   if (scope == error_mark_node)
14141     return error_mark_node;
14142
14143   /* Any name names a type if we're following the `typename' keyword
14144      in a qualified name where the enclosing scope is type-dependent.  */
14145   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14146                 && dependent_type_p (scope));
14147   /* Handle the common case (an identifier, but not a template-id)
14148      efficiently.  */
14149   if (token->type == CPP_NAME
14150       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14151     {
14152       cp_token *identifier_token;
14153       tree identifier;
14154       bool ambiguous_p;
14155
14156       /* Look for the identifier.  */
14157       identifier_token = cp_lexer_peek_token (parser->lexer);
14158       ambiguous_p = identifier_token->ambiguous_p;
14159       identifier = cp_parser_identifier (parser);
14160       /* If the next token isn't an identifier, we are certainly not
14161          looking at a class-name.  */
14162       if (identifier == error_mark_node)
14163         decl = error_mark_node;
14164       /* If we know this is a type-name, there's no need to look it
14165          up.  */
14166       else if (typename_p)
14167         decl = identifier;
14168       else
14169         {
14170           tree ambiguous_decls;
14171           /* If we already know that this lookup is ambiguous, then
14172              we've already issued an error message; there's no reason
14173              to check again.  */
14174           if (ambiguous_p)
14175             {
14176               cp_parser_simulate_error (parser);
14177               return error_mark_node;
14178             }
14179           /* If the next token is a `::', then the name must be a type
14180              name.
14181
14182              [basic.lookup.qual]
14183
14184              During the lookup for a name preceding the :: scope
14185              resolution operator, object, function, and enumerator
14186              names are ignored.  */
14187           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14188             tag_type = typename_type;
14189           /* Look up the name.  */
14190           decl = cp_parser_lookup_name (parser, identifier,
14191                                         tag_type,
14192                                         /*is_template=*/false,
14193                                         /*is_namespace=*/false,
14194                                         check_dependency_p,
14195                                         &ambiguous_decls);
14196           if (ambiguous_decls)
14197             {
14198               error ("reference to %qD is ambiguous", identifier);
14199               print_candidates (ambiguous_decls);
14200               if (cp_parser_parsing_tentatively (parser))
14201                 {
14202                   identifier_token->ambiguous_p = true;
14203                   cp_parser_simulate_error (parser);
14204                 }
14205               return error_mark_node;
14206             }
14207         }
14208     }
14209   else
14210     {
14211       /* Try a template-id.  */
14212       decl = cp_parser_template_id (parser, template_keyword_p,
14213                                     check_dependency_p,
14214                                     is_declaration);
14215       if (decl == error_mark_node)
14216         return error_mark_node;
14217     }
14218
14219   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14220
14221   /* If this is a typename, create a TYPENAME_TYPE.  */
14222   if (typename_p && decl != error_mark_node)
14223     {
14224       decl = make_typename_type (scope, decl, typename_type,
14225                                  /*complain=*/tf_error);
14226       if (decl != error_mark_node)
14227         decl = TYPE_NAME (decl);
14228     }
14229
14230   /* Check to see that it is really the name of a class.  */
14231   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14232       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14233       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14234     /* Situations like this:
14235
14236          template <typename T> struct A {
14237            typename T::template X<int>::I i;
14238          };
14239
14240        are problematic.  Is `T::template X<int>' a class-name?  The
14241        standard does not seem to be definitive, but there is no other
14242        valid interpretation of the following `::'.  Therefore, those
14243        names are considered class-names.  */
14244     {
14245       decl = make_typename_type (scope, decl, tag_type, tf_error);
14246       if (decl != error_mark_node)
14247         decl = TYPE_NAME (decl);
14248     }
14249   else if (TREE_CODE (decl) != TYPE_DECL
14250            || TREE_TYPE (decl) == error_mark_node
14251            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14252     decl = error_mark_node;
14253
14254   if (decl == error_mark_node)
14255     cp_parser_error (parser, "expected class-name");
14256
14257   return decl;
14258 }
14259
14260 /* Parse a class-specifier.
14261
14262    class-specifier:
14263      class-head { member-specification [opt] }
14264
14265    Returns the TREE_TYPE representing the class.  */
14266
14267 static tree
14268 cp_parser_class_specifier (cp_parser* parser)
14269 {
14270   cp_token *token;
14271   tree type;
14272   tree attributes = NULL_TREE;
14273   int has_trailing_semicolon;
14274   bool nested_name_specifier_p;
14275   unsigned saved_num_template_parameter_lists;
14276   bool saved_in_function_body;
14277   tree old_scope = NULL_TREE;
14278   tree scope = NULL_TREE;
14279   tree bases;
14280
14281   push_deferring_access_checks (dk_no_deferred);
14282
14283   /* Parse the class-head.  */
14284   type = cp_parser_class_head (parser,
14285                                &nested_name_specifier_p,
14286                                &attributes,
14287                                &bases);
14288   /* If the class-head was a semantic disaster, skip the entire body
14289      of the class.  */
14290   if (!type)
14291     {
14292       cp_parser_skip_to_end_of_block_or_statement (parser);
14293       pop_deferring_access_checks ();
14294       return error_mark_node;
14295     }
14296
14297   /* Look for the `{'.  */
14298   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14299     {
14300       pop_deferring_access_checks ();
14301       return error_mark_node;
14302     }
14303
14304   /* Process the base classes. If they're invalid, skip the 
14305      entire class body.  */
14306   if (!xref_basetypes (type, bases))
14307     {
14308       /* Consuming the closing brace yields better error messages
14309          later on.  */
14310       if (cp_parser_skip_to_closing_brace (parser))
14311         cp_lexer_consume_token (parser->lexer);
14312       pop_deferring_access_checks ();
14313       return error_mark_node;
14314     }
14315
14316   /* Issue an error message if type-definitions are forbidden here.  */
14317   cp_parser_check_type_definition (parser);
14318   /* Remember that we are defining one more class.  */
14319   ++parser->num_classes_being_defined;
14320   /* Inside the class, surrounding template-parameter-lists do not
14321      apply.  */
14322   saved_num_template_parameter_lists
14323     = parser->num_template_parameter_lists;
14324   parser->num_template_parameter_lists = 0;
14325   /* We are not in a function body.  */
14326   saved_in_function_body = parser->in_function_body;
14327   parser->in_function_body = false;
14328
14329   /* Start the class.  */
14330   if (nested_name_specifier_p)
14331     {
14332       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14333       old_scope = push_inner_scope (scope);
14334     }
14335   type = begin_class_definition (type, attributes);
14336
14337   if (type == error_mark_node)
14338     /* If the type is erroneous, skip the entire body of the class.  */
14339     cp_parser_skip_to_closing_brace (parser);
14340   else
14341     /* Parse the member-specification.  */
14342     cp_parser_member_specification_opt (parser);
14343
14344   /* Look for the trailing `}'.  */
14345   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14346   /* We get better error messages by noticing a common problem: a
14347      missing trailing `;'.  */
14348   token = cp_lexer_peek_token (parser->lexer);
14349   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14350   /* Look for trailing attributes to apply to this class.  */
14351   if (cp_parser_allow_gnu_extensions_p (parser))
14352     attributes = cp_parser_attributes_opt (parser);
14353   if (type != error_mark_node)
14354     type = finish_struct (type, attributes);
14355   if (nested_name_specifier_p)
14356     pop_inner_scope (old_scope, scope);
14357   /* If this class is not itself within the scope of another class,
14358      then we need to parse the bodies of all of the queued function
14359      definitions.  Note that the queued functions defined in a class
14360      are not always processed immediately following the
14361      class-specifier for that class.  Consider:
14362
14363        struct A {
14364          struct B { void f() { sizeof (A); } };
14365        };
14366
14367      If `f' were processed before the processing of `A' were
14368      completed, there would be no way to compute the size of `A'.
14369      Note that the nesting we are interested in here is lexical --
14370      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14371      for:
14372
14373        struct A { struct B; };
14374        struct A::B { void f() { } };
14375
14376      there is no need to delay the parsing of `A::B::f'.  */
14377   if (--parser->num_classes_being_defined == 0)
14378     {
14379       tree queue_entry;
14380       tree fn;
14381       tree class_type = NULL_TREE;
14382       tree pushed_scope = NULL_TREE;
14383
14384       /* In a first pass, parse default arguments to the functions.
14385          Then, in a second pass, parse the bodies of the functions.
14386          This two-phased approach handles cases like:
14387
14388             struct S {
14389               void f() { g(); }
14390               void g(int i = 3);
14391             };
14392
14393          */
14394       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14395              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14396            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14397            TREE_PURPOSE (parser->unparsed_functions_queues)
14398              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14399         {
14400           fn = TREE_VALUE (queue_entry);
14401           /* If there are default arguments that have not yet been processed,
14402              take care of them now.  */
14403           if (class_type != TREE_PURPOSE (queue_entry))
14404             {
14405               if (pushed_scope)
14406                 pop_scope (pushed_scope);
14407               class_type = TREE_PURPOSE (queue_entry);
14408               pushed_scope = push_scope (class_type);
14409             }
14410           /* Make sure that any template parameters are in scope.  */
14411           maybe_begin_member_template_processing (fn);
14412           /* Parse the default argument expressions.  */
14413           cp_parser_late_parsing_default_args (parser, fn);
14414           /* Remove any template parameters from the symbol table.  */
14415           maybe_end_member_template_processing ();
14416         }
14417       if (pushed_scope)
14418         pop_scope (pushed_scope);
14419       /* Now parse the body of the functions.  */
14420       for (TREE_VALUE (parser->unparsed_functions_queues)
14421              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14422            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14423            TREE_VALUE (parser->unparsed_functions_queues)
14424              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14425         {
14426           /* Figure out which function we need to process.  */
14427           fn = TREE_VALUE (queue_entry);
14428           /* Parse the function.  */
14429           cp_parser_late_parsing_for_member (parser, fn);
14430         }
14431     }
14432
14433   /* Put back any saved access checks.  */
14434   pop_deferring_access_checks ();
14435
14436   /* Restore saved state.  */
14437   parser->in_function_body = saved_in_function_body;
14438   parser->num_template_parameter_lists
14439     = saved_num_template_parameter_lists;
14440
14441   return type;
14442 }
14443
14444 /* Parse a class-head.
14445
14446    class-head:
14447      class-key identifier [opt] base-clause [opt]
14448      class-key nested-name-specifier identifier base-clause [opt]
14449      class-key nested-name-specifier [opt] template-id
14450        base-clause [opt]
14451
14452    GNU Extensions:
14453      class-key attributes identifier [opt] base-clause [opt]
14454      class-key attributes nested-name-specifier identifier base-clause [opt]
14455      class-key attributes nested-name-specifier [opt] template-id
14456        base-clause [opt]
14457
14458    Upon return BASES is initialized to the list of base classes (or
14459    NULL, if there are none) in the same form returned by
14460    cp_parser_base_clause.
14461
14462    Returns the TYPE of the indicated class.  Sets
14463    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14464    involving a nested-name-specifier was used, and FALSE otherwise.
14465
14466    Returns error_mark_node if this is not a class-head.
14467
14468    Returns NULL_TREE if the class-head is syntactically valid, but
14469    semantically invalid in a way that means we should skip the entire
14470    body of the class.  */
14471
14472 static tree
14473 cp_parser_class_head (cp_parser* parser,
14474                       bool* nested_name_specifier_p,
14475                       tree *attributes_p,
14476                       tree *bases)
14477 {
14478   tree nested_name_specifier;
14479   enum tag_types class_key;
14480   tree id = NULL_TREE;
14481   tree type = NULL_TREE;
14482   tree attributes;
14483   bool template_id_p = false;
14484   bool qualified_p = false;
14485   bool invalid_nested_name_p = false;
14486   bool invalid_explicit_specialization_p = false;
14487   tree pushed_scope = NULL_TREE;
14488   unsigned num_templates;
14489
14490   /* Assume no nested-name-specifier will be present.  */
14491   *nested_name_specifier_p = false;
14492   /* Assume no template parameter lists will be used in defining the
14493      type.  */
14494   num_templates = 0;
14495
14496   *bases = NULL_TREE;
14497
14498   /* Look for the class-key.  */
14499   class_key = cp_parser_class_key (parser);
14500   if (class_key == none_type)
14501     return error_mark_node;
14502
14503   /* Parse the attributes.  */
14504   attributes = cp_parser_attributes_opt (parser);
14505
14506   /* If the next token is `::', that is invalid -- but sometimes
14507      people do try to write:
14508
14509        struct ::S {};
14510
14511      Handle this gracefully by accepting the extra qualifier, and then
14512      issuing an error about it later if this really is a
14513      class-head.  If it turns out just to be an elaborated type
14514      specifier, remain silent.  */
14515   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14516     qualified_p = true;
14517
14518   push_deferring_access_checks (dk_no_check);
14519
14520   /* Determine the name of the class.  Begin by looking for an
14521      optional nested-name-specifier.  */
14522   nested_name_specifier
14523     = cp_parser_nested_name_specifier_opt (parser,
14524                                            /*typename_keyword_p=*/false,
14525                                            /*check_dependency_p=*/false,
14526                                            /*type_p=*/false,
14527                                            /*is_declaration=*/false);
14528   /* If there was a nested-name-specifier, then there *must* be an
14529      identifier.  */
14530   if (nested_name_specifier)
14531     {
14532       /* Although the grammar says `identifier', it really means
14533          `class-name' or `template-name'.  You are only allowed to
14534          define a class that has already been declared with this
14535          syntax.
14536
14537          The proposed resolution for Core Issue 180 says that wherever
14538          you see `class T::X' you should treat `X' as a type-name.
14539
14540          It is OK to define an inaccessible class; for example:
14541
14542            class A { class B; };
14543            class A::B {};
14544
14545          We do not know if we will see a class-name, or a
14546          template-name.  We look for a class-name first, in case the
14547          class-name is a template-id; if we looked for the
14548          template-name first we would stop after the template-name.  */
14549       cp_parser_parse_tentatively (parser);
14550       type = cp_parser_class_name (parser,
14551                                    /*typename_keyword_p=*/false,
14552                                    /*template_keyword_p=*/false,
14553                                    class_type,
14554                                    /*check_dependency_p=*/false,
14555                                    /*class_head_p=*/true,
14556                                    /*is_declaration=*/false);
14557       /* If that didn't work, ignore the nested-name-specifier.  */
14558       if (!cp_parser_parse_definitely (parser))
14559         {
14560           invalid_nested_name_p = true;
14561           id = cp_parser_identifier (parser);
14562           if (id == error_mark_node)
14563             id = NULL_TREE;
14564         }
14565       /* If we could not find a corresponding TYPE, treat this
14566          declaration like an unqualified declaration.  */
14567       if (type == error_mark_node)
14568         nested_name_specifier = NULL_TREE;
14569       /* Otherwise, count the number of templates used in TYPE and its
14570          containing scopes.  */
14571       else
14572         {
14573           tree scope;
14574
14575           for (scope = TREE_TYPE (type);
14576                scope && TREE_CODE (scope) != NAMESPACE_DECL;
14577                scope = (TYPE_P (scope)
14578                         ? TYPE_CONTEXT (scope)
14579                         : DECL_CONTEXT (scope)))
14580             if (TYPE_P (scope)
14581                 && CLASS_TYPE_P (scope)
14582                 && CLASSTYPE_TEMPLATE_INFO (scope)
14583                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14584                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14585               ++num_templates;
14586         }
14587     }
14588   /* Otherwise, the identifier is optional.  */
14589   else
14590     {
14591       /* We don't know whether what comes next is a template-id,
14592          an identifier, or nothing at all.  */
14593       cp_parser_parse_tentatively (parser);
14594       /* Check for a template-id.  */
14595       id = cp_parser_template_id (parser,
14596                                   /*template_keyword_p=*/false,
14597                                   /*check_dependency_p=*/true,
14598                                   /*is_declaration=*/true);
14599       /* If that didn't work, it could still be an identifier.  */
14600       if (!cp_parser_parse_definitely (parser))
14601         {
14602           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14603             id = cp_parser_identifier (parser);
14604           else
14605             id = NULL_TREE;
14606         }
14607       else
14608         {
14609           template_id_p = true;
14610           ++num_templates;
14611         }
14612     }
14613
14614   pop_deferring_access_checks ();
14615
14616   if (id)
14617     cp_parser_check_for_invalid_template_id (parser, id);
14618
14619   /* If it's not a `:' or a `{' then we can't really be looking at a
14620      class-head, since a class-head only appears as part of a
14621      class-specifier.  We have to detect this situation before calling
14622      xref_tag, since that has irreversible side-effects.  */
14623   if (!cp_parser_next_token_starts_class_definition_p (parser))
14624     {
14625       cp_parser_error (parser, "expected %<{%> or %<:%>");
14626       return error_mark_node;
14627     }
14628
14629   /* At this point, we're going ahead with the class-specifier, even
14630      if some other problem occurs.  */
14631   cp_parser_commit_to_tentative_parse (parser);
14632   /* Issue the error about the overly-qualified name now.  */
14633   if (qualified_p)
14634     cp_parser_error (parser,
14635                      "global qualification of class name is invalid");
14636   else if (invalid_nested_name_p)
14637     cp_parser_error (parser,
14638                      "qualified name does not name a class");
14639   else if (nested_name_specifier)
14640     {
14641       tree scope;
14642
14643       /* Reject typedef-names in class heads.  */
14644       if (!DECL_IMPLICIT_TYPEDEF_P (type))
14645         {
14646           error ("invalid class name in declaration of %qD", type);
14647           type = NULL_TREE;
14648           goto done;
14649         }
14650
14651       /* Figure out in what scope the declaration is being placed.  */
14652       scope = current_scope ();
14653       /* If that scope does not contain the scope in which the
14654          class was originally declared, the program is invalid.  */
14655       if (scope && !is_ancestor (scope, nested_name_specifier))
14656         {
14657           if (at_namespace_scope_p ())
14658             error ("declaration of %qD in namespace %qD which does not "
14659                    "enclose %qD", type, scope, nested_name_specifier);
14660           else
14661             error ("declaration of %qD in %qD which does not enclose %qD",
14662                    type, scope, nested_name_specifier);
14663           type = NULL_TREE;
14664           goto done;
14665         }
14666       /* [dcl.meaning]
14667
14668          A declarator-id shall not be qualified exception of the
14669          definition of a ... nested class outside of its class
14670          ... [or] a the definition or explicit instantiation of a
14671          class member of a namespace outside of its namespace.  */
14672       if (scope == nested_name_specifier)
14673         {
14674           pedwarn ("extra qualification ignored");
14675           nested_name_specifier = NULL_TREE;
14676           num_templates = 0;
14677         }
14678     }
14679   /* An explicit-specialization must be preceded by "template <>".  If
14680      it is not, try to recover gracefully.  */
14681   if (at_namespace_scope_p ()
14682       && parser->num_template_parameter_lists == 0
14683       && template_id_p)
14684     {
14685       error ("an explicit specialization must be preceded by %<template <>%>");
14686       invalid_explicit_specialization_p = true;
14687       /* Take the same action that would have been taken by
14688          cp_parser_explicit_specialization.  */
14689       ++parser->num_template_parameter_lists;
14690       begin_specialization ();
14691     }
14692   /* There must be no "return" statements between this point and the
14693      end of this function; set "type "to the correct return value and
14694      use "goto done;" to return.  */
14695   /* Make sure that the right number of template parameters were
14696      present.  */
14697   if (!cp_parser_check_template_parameters (parser, num_templates))
14698     {
14699       /* If something went wrong, there is no point in even trying to
14700          process the class-definition.  */
14701       type = NULL_TREE;
14702       goto done;
14703     }
14704
14705   /* Look up the type.  */
14706   if (template_id_p)
14707     {
14708       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14709           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14710               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14711         {
14712           error ("function template %qD redeclared as a class template", id);
14713           type = error_mark_node;
14714         }
14715       else
14716         {
14717           type = TREE_TYPE (id);
14718           type = maybe_process_partial_specialization (type);
14719         }
14720       if (nested_name_specifier)
14721         pushed_scope = push_scope (nested_name_specifier);
14722     }
14723   else if (nested_name_specifier)
14724     {
14725       tree class_type;
14726
14727       /* Given:
14728
14729             template <typename T> struct S { struct T };
14730             template <typename T> struct S<T>::T { };
14731
14732          we will get a TYPENAME_TYPE when processing the definition of
14733          `S::T'.  We need to resolve it to the actual type before we
14734          try to define it.  */
14735       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14736         {
14737           class_type = resolve_typename_type (TREE_TYPE (type),
14738                                               /*only_current_p=*/false);
14739           if (TREE_CODE (class_type) != TYPENAME_TYPE)
14740             type = TYPE_NAME (class_type);
14741           else
14742             {
14743               cp_parser_error (parser, "could not resolve typename type");
14744               type = error_mark_node;
14745             }
14746         }
14747
14748       maybe_process_partial_specialization (TREE_TYPE (type));
14749       class_type = current_class_type;
14750       /* Enter the scope indicated by the nested-name-specifier.  */
14751       pushed_scope = push_scope (nested_name_specifier);
14752       /* Get the canonical version of this type.  */
14753       type = TYPE_MAIN_DECL (TREE_TYPE (type));
14754       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14755           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14756         {
14757           type = push_template_decl (type);
14758           if (type == error_mark_node)
14759             {
14760               type = NULL_TREE;
14761               goto done;
14762             }
14763         }
14764
14765       type = TREE_TYPE (type);
14766       *nested_name_specifier_p = true;
14767     }
14768   else      /* The name is not a nested name.  */
14769     {
14770       /* If the class was unnamed, create a dummy name.  */
14771       if (!id)
14772         id = make_anon_name ();
14773       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14774                        parser->num_template_parameter_lists);
14775     }
14776
14777   /* Indicate whether this class was declared as a `class' or as a
14778      `struct'.  */
14779   if (TREE_CODE (type) == RECORD_TYPE)
14780     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14781   cp_parser_check_class_key (class_key, type);
14782
14783   /* If this type was already complete, and we see another definition,
14784      that's an error.  */
14785   if (type != error_mark_node && COMPLETE_TYPE_P (type))
14786     {
14787       error ("redefinition of %q#T", type);
14788       error ("previous definition of %q+#T", type);
14789       type = NULL_TREE;
14790       goto done;
14791     }
14792   else if (type == error_mark_node)
14793     type = NULL_TREE;
14794
14795   /* We will have entered the scope containing the class; the names of
14796      base classes should be looked up in that context.  For example:
14797
14798        struct A { struct B {}; struct C; };
14799        struct A::C : B {};
14800
14801      is valid.  */
14802
14803   /* Get the list of base-classes, if there is one.  */
14804   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14805     *bases = cp_parser_base_clause (parser);
14806
14807  done:
14808   /* Leave the scope given by the nested-name-specifier.  We will
14809      enter the class scope itself while processing the members.  */
14810   if (pushed_scope)
14811     pop_scope (pushed_scope);
14812
14813   if (invalid_explicit_specialization_p)
14814     {
14815       end_specialization ();
14816       --parser->num_template_parameter_lists;
14817     }
14818   *attributes_p = attributes;
14819   return type;
14820 }
14821
14822 /* Parse a class-key.
14823
14824    class-key:
14825      class
14826      struct
14827      union
14828
14829    Returns the kind of class-key specified, or none_type to indicate
14830    error.  */
14831
14832 static enum tag_types
14833 cp_parser_class_key (cp_parser* parser)
14834 {
14835   cp_token *token;
14836   enum tag_types tag_type;
14837
14838   /* Look for the class-key.  */
14839   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14840   if (!token)
14841     return none_type;
14842
14843   /* Check to see if the TOKEN is a class-key.  */
14844   tag_type = cp_parser_token_is_class_key (token);
14845   if (!tag_type)
14846     cp_parser_error (parser, "expected class-key");
14847   return tag_type;
14848 }
14849
14850 /* Parse an (optional) member-specification.
14851
14852    member-specification:
14853      member-declaration member-specification [opt]
14854      access-specifier : member-specification [opt]  */
14855
14856 static void
14857 cp_parser_member_specification_opt (cp_parser* parser)
14858 {
14859   while (true)
14860     {
14861       cp_token *token;
14862       enum rid keyword;
14863
14864       /* Peek at the next token.  */
14865       token = cp_lexer_peek_token (parser->lexer);
14866       /* If it's a `}', or EOF then we've seen all the members.  */
14867       if (token->type == CPP_CLOSE_BRACE
14868           || token->type == CPP_EOF
14869           || token->type == CPP_PRAGMA_EOL)
14870         break;
14871
14872       /* See if this token is a keyword.  */
14873       keyword = token->keyword;
14874       switch (keyword)
14875         {
14876         case RID_PUBLIC:
14877         case RID_PROTECTED:
14878         case RID_PRIVATE:
14879           /* Consume the access-specifier.  */
14880           cp_lexer_consume_token (parser->lexer);
14881           /* Remember which access-specifier is active.  */
14882           current_access_specifier = token->u.value;
14883           /* Look for the `:'.  */
14884           cp_parser_require (parser, CPP_COLON, "%<:%>");
14885           break;
14886
14887         default:
14888           /* Accept #pragmas at class scope.  */
14889           if (token->type == CPP_PRAGMA)
14890             {
14891               cp_parser_pragma (parser, pragma_external);
14892               break;
14893             }
14894
14895           /* Otherwise, the next construction must be a
14896              member-declaration.  */
14897           cp_parser_member_declaration (parser);
14898         }
14899     }
14900 }
14901
14902 /* Parse a member-declaration.
14903
14904    member-declaration:
14905      decl-specifier-seq [opt] member-declarator-list [opt] ;
14906      function-definition ; [opt]
14907      :: [opt] nested-name-specifier template [opt] unqualified-id ;
14908      using-declaration
14909      template-declaration
14910
14911    member-declarator-list:
14912      member-declarator
14913      member-declarator-list , member-declarator
14914
14915    member-declarator:
14916      declarator pure-specifier [opt]
14917      declarator constant-initializer [opt]
14918      identifier [opt] : constant-expression
14919
14920    GNU Extensions:
14921
14922    member-declaration:
14923      __extension__ member-declaration
14924
14925    member-declarator:
14926      declarator attributes [opt] pure-specifier [opt]
14927      declarator attributes [opt] constant-initializer [opt]
14928      identifier [opt] attributes [opt] : constant-expression  
14929
14930    C++0x Extensions:
14931
14932    member-declaration:
14933      static_assert-declaration  */
14934
14935 static void
14936 cp_parser_member_declaration (cp_parser* parser)
14937 {
14938   cp_decl_specifier_seq decl_specifiers;
14939   tree prefix_attributes;
14940   tree decl;
14941   int declares_class_or_enum;
14942   bool friend_p;
14943   cp_token *token;
14944   int saved_pedantic;
14945
14946   /* Check for the `__extension__' keyword.  */
14947   if (cp_parser_extension_opt (parser, &saved_pedantic))
14948     {
14949       /* Recurse.  */
14950       cp_parser_member_declaration (parser);
14951       /* Restore the old value of the PEDANTIC flag.  */
14952       pedantic = saved_pedantic;
14953
14954       return;
14955     }
14956
14957   /* Check for a template-declaration.  */
14958   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14959     {
14960       /* An explicit specialization here is an error condition, and we
14961          expect the specialization handler to detect and report this.  */
14962       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14963           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14964         cp_parser_explicit_specialization (parser);
14965       else
14966         cp_parser_template_declaration (parser, /*member_p=*/true);
14967
14968       return;
14969     }
14970
14971   /* Check for a using-declaration.  */
14972   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14973     {
14974       /* Parse the using-declaration.  */
14975       cp_parser_using_declaration (parser,
14976                                    /*access_declaration_p=*/false);
14977       return;
14978     }
14979
14980   /* Check for @defs.  */
14981   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14982     {
14983       tree ivar, member;
14984       tree ivar_chains = cp_parser_objc_defs_expression (parser);
14985       ivar = ivar_chains;
14986       while (ivar)
14987         {
14988           member = ivar;
14989           ivar = TREE_CHAIN (member);
14990           TREE_CHAIN (member) = NULL_TREE;
14991           finish_member_declaration (member);
14992         }
14993       return;
14994     }
14995
14996   /* If the next token is `static_assert' we have a static assertion.  */
14997   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14998     {
14999       cp_parser_static_assert (parser, /*member_p=*/true);
15000       return;
15001     }
15002
15003   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15004     return;
15005
15006   /* Parse the decl-specifier-seq.  */
15007   cp_parser_decl_specifier_seq (parser,
15008                                 CP_PARSER_FLAGS_OPTIONAL,
15009                                 &decl_specifiers,
15010                                 &declares_class_or_enum);
15011   prefix_attributes = decl_specifiers.attributes;
15012   decl_specifiers.attributes = NULL_TREE;
15013   /* Check for an invalid type-name.  */
15014   if (!decl_specifiers.type
15015       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15016     return;
15017   /* If there is no declarator, then the decl-specifier-seq should
15018      specify a type.  */
15019   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15020     {
15021       /* If there was no decl-specifier-seq, and the next token is a
15022          `;', then we have something like:
15023
15024            struct S { ; };
15025
15026          [class.mem]
15027
15028          Each member-declaration shall declare at least one member
15029          name of the class.  */
15030       if (!decl_specifiers.any_specifiers_p)
15031         {
15032           cp_token *token = cp_lexer_peek_token (parser->lexer);
15033           if (pedantic && !token->in_system_header)
15034             pedwarn ("%Hextra %<;%>", &token->location);
15035         }
15036       else
15037         {
15038           tree type;
15039
15040           /* See if this declaration is a friend.  */
15041           friend_p = cp_parser_friend_p (&decl_specifiers);
15042           /* If there were decl-specifiers, check to see if there was
15043              a class-declaration.  */
15044           type = check_tag_decl (&decl_specifiers);
15045           /* Nested classes have already been added to the class, but
15046              a `friend' needs to be explicitly registered.  */
15047           if (friend_p)
15048             {
15049               /* If the `friend' keyword was present, the friend must
15050                  be introduced with a class-key.  */
15051                if (!declares_class_or_enum)
15052                  error ("a class-key must be used when declaring a friend");
15053                /* In this case:
15054
15055                     template <typename T> struct A {
15056                       friend struct A<T>::B;
15057                     };
15058
15059                   A<T>::B will be represented by a TYPENAME_TYPE, and
15060                   therefore not recognized by check_tag_decl.  */
15061                if (!type
15062                    && decl_specifiers.type
15063                    && TYPE_P (decl_specifiers.type))
15064                  type = decl_specifiers.type;
15065                if (!type || !TYPE_P (type))
15066                  error ("friend declaration does not name a class or "
15067                         "function");
15068                else
15069                  make_friend_class (current_class_type, type,
15070                                     /*complain=*/true);
15071             }
15072           /* If there is no TYPE, an error message will already have
15073              been issued.  */
15074           else if (!type || type == error_mark_node)
15075             ;
15076           /* An anonymous aggregate has to be handled specially; such
15077              a declaration really declares a data member (with a
15078              particular type), as opposed to a nested class.  */
15079           else if (ANON_AGGR_TYPE_P (type))
15080             {
15081               /* Remove constructors and such from TYPE, now that we
15082                  know it is an anonymous aggregate.  */
15083               fixup_anonymous_aggr (type);
15084               /* And make the corresponding data member.  */
15085               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15086               /* Add it to the class.  */
15087               finish_member_declaration (decl);
15088             }
15089           else
15090             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
15091         }
15092     }
15093   else
15094     {
15095       /* See if these declarations will be friends.  */
15096       friend_p = cp_parser_friend_p (&decl_specifiers);
15097
15098       /* Keep going until we hit the `;' at the end of the
15099          declaration.  */
15100       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15101         {
15102           tree attributes = NULL_TREE;
15103           tree first_attribute;
15104
15105           /* Peek at the next token.  */
15106           token = cp_lexer_peek_token (parser->lexer);
15107
15108           /* Check for a bitfield declaration.  */
15109           if (token->type == CPP_COLON
15110               || (token->type == CPP_NAME
15111                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15112                   == CPP_COLON))
15113             {
15114               tree identifier;
15115               tree width;
15116
15117               /* Get the name of the bitfield.  Note that we cannot just
15118                  check TOKEN here because it may have been invalidated by
15119                  the call to cp_lexer_peek_nth_token above.  */
15120               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15121                 identifier = cp_parser_identifier (parser);
15122               else
15123                 identifier = NULL_TREE;
15124
15125               /* Consume the `:' token.  */
15126               cp_lexer_consume_token (parser->lexer);
15127               /* Get the width of the bitfield.  */
15128               width
15129                 = cp_parser_constant_expression (parser,
15130                                                  /*allow_non_constant=*/false,
15131                                                  NULL);
15132
15133               /* Look for attributes that apply to the bitfield.  */
15134               attributes = cp_parser_attributes_opt (parser);
15135               /* Remember which attributes are prefix attributes and
15136                  which are not.  */
15137               first_attribute = attributes;
15138               /* Combine the attributes.  */
15139               attributes = chainon (prefix_attributes, attributes);
15140
15141               /* Create the bitfield declaration.  */
15142               decl = grokbitfield (identifier
15143                                    ? make_id_declarator (NULL_TREE,
15144                                                          identifier,
15145                                                          sfk_none)
15146                                    : NULL,
15147                                    &decl_specifiers,
15148                                    width);
15149               /* Apply the attributes.  */
15150               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
15151             }
15152           else
15153             {
15154               cp_declarator *declarator;
15155               tree initializer;
15156               tree asm_specification;
15157               int ctor_dtor_or_conv_p;
15158
15159               /* Parse the declarator.  */
15160               declarator
15161                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15162                                         &ctor_dtor_or_conv_p,
15163                                         /*parenthesized_p=*/NULL,
15164                                         /*member_p=*/true);
15165
15166               /* If something went wrong parsing the declarator, make sure
15167                  that we at least consume some tokens.  */
15168               if (declarator == cp_error_declarator)
15169                 {
15170                   /* Skip to the end of the statement.  */
15171                   cp_parser_skip_to_end_of_statement (parser);
15172                   /* If the next token is not a semicolon, that is
15173                      probably because we just skipped over the body of
15174                      a function.  So, we consume a semicolon if
15175                      present, but do not issue an error message if it
15176                      is not present.  */
15177                   if (cp_lexer_next_token_is (parser->lexer,
15178                                               CPP_SEMICOLON))
15179                     cp_lexer_consume_token (parser->lexer);
15180                   return;
15181                 }
15182
15183               if (declares_class_or_enum & 2)
15184                 cp_parser_check_for_definition_in_return_type
15185                   (declarator, decl_specifiers.type);
15186
15187               /* Look for an asm-specification.  */
15188               asm_specification = cp_parser_asm_specification_opt (parser);
15189               /* Look for attributes that apply to the declaration.  */
15190               attributes = cp_parser_attributes_opt (parser);
15191               /* Remember which attributes are prefix attributes and
15192                  which are not.  */
15193               first_attribute = attributes;
15194               /* Combine the attributes.  */
15195               attributes = chainon (prefix_attributes, attributes);
15196
15197               /* If it's an `=', then we have a constant-initializer or a
15198                  pure-specifier.  It is not correct to parse the
15199                  initializer before registering the member declaration
15200                  since the member declaration should be in scope while
15201                  its initializer is processed.  However, the rest of the
15202                  front end does not yet provide an interface that allows
15203                  us to handle this correctly.  */
15204               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15205                 {
15206                   /* In [class.mem]:
15207
15208                      A pure-specifier shall be used only in the declaration of
15209                      a virtual function.
15210
15211                      A member-declarator can contain a constant-initializer
15212                      only if it declares a static member of integral or
15213                      enumeration type.
15214
15215                      Therefore, if the DECLARATOR is for a function, we look
15216                      for a pure-specifier; otherwise, we look for a
15217                      constant-initializer.  When we call `grokfield', it will
15218                      perform more stringent semantics checks.  */
15219                   if (function_declarator_p (declarator))
15220                     initializer = cp_parser_pure_specifier (parser);
15221                   else
15222                     /* Parse the initializer.  */
15223                     initializer = cp_parser_constant_initializer (parser);
15224                 }
15225               /* Otherwise, there is no initializer.  */
15226               else
15227                 initializer = NULL_TREE;
15228
15229               /* See if we are probably looking at a function
15230                  definition.  We are certainly not looking at a
15231                  member-declarator.  Calling `grokfield' has
15232                  side-effects, so we must not do it unless we are sure
15233                  that we are looking at a member-declarator.  */
15234               if (cp_parser_token_starts_function_definition_p
15235                   (cp_lexer_peek_token (parser->lexer)))
15236                 {
15237                   /* The grammar does not allow a pure-specifier to be
15238                      used when a member function is defined.  (It is
15239                      possible that this fact is an oversight in the
15240                      standard, since a pure function may be defined
15241                      outside of the class-specifier.  */
15242                   if (initializer)
15243                     error ("pure-specifier on function-definition");
15244                   decl = cp_parser_save_member_function_body (parser,
15245                                                               &decl_specifiers,
15246                                                               declarator,
15247                                                               attributes);
15248                   /* If the member was not a friend, declare it here.  */
15249                   if (!friend_p)
15250                     finish_member_declaration (decl);
15251                   /* Peek at the next token.  */
15252                   token = cp_lexer_peek_token (parser->lexer);
15253                   /* If the next token is a semicolon, consume it.  */
15254                   if (token->type == CPP_SEMICOLON)
15255                     cp_lexer_consume_token (parser->lexer);
15256                   return;
15257                 }
15258               else
15259                 /* Create the declaration.  */
15260                 decl = grokfield (declarator, &decl_specifiers,
15261                                   initializer, /*init_const_expr_p=*/true,
15262                                   asm_specification,
15263                                   attributes);
15264             }
15265
15266           /* Reset PREFIX_ATTRIBUTES.  */
15267           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15268             attributes = TREE_CHAIN (attributes);
15269           if (attributes)
15270             TREE_CHAIN (attributes) = NULL_TREE;
15271
15272           /* If there is any qualification still in effect, clear it
15273              now; we will be starting fresh with the next declarator.  */
15274           parser->scope = NULL_TREE;
15275           parser->qualifying_scope = NULL_TREE;
15276           parser->object_scope = NULL_TREE;
15277           /* If it's a `,', then there are more declarators.  */
15278           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15279             cp_lexer_consume_token (parser->lexer);
15280           /* If the next token isn't a `;', then we have a parse error.  */
15281           else if (cp_lexer_next_token_is_not (parser->lexer,
15282                                                CPP_SEMICOLON))
15283             {
15284               cp_parser_error (parser, "expected %<;%>");
15285               /* Skip tokens until we find a `;'.  */
15286               cp_parser_skip_to_end_of_statement (parser);
15287
15288               break;
15289             }
15290
15291           if (decl)
15292             {
15293               /* Add DECL to the list of members.  */
15294               if (!friend_p)
15295                 finish_member_declaration (decl);
15296
15297               if (TREE_CODE (decl) == FUNCTION_DECL)
15298                 cp_parser_save_default_args (parser, decl);
15299             }
15300         }
15301     }
15302
15303   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15304 }
15305
15306 /* Parse a pure-specifier.
15307
15308    pure-specifier:
15309      = 0
15310
15311    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15312    Otherwise, ERROR_MARK_NODE is returned.  */
15313
15314 static tree
15315 cp_parser_pure_specifier (cp_parser* parser)
15316 {
15317   cp_token *token;
15318
15319   /* Look for the `=' token.  */
15320   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15321     return error_mark_node;
15322   /* Look for the `0' token.  */
15323   token = cp_lexer_consume_token (parser->lexer);
15324   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15325   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15326     {
15327       cp_parser_error (parser,
15328                        "invalid pure specifier (only %<= 0%> is allowed)");
15329       cp_parser_skip_to_end_of_statement (parser);
15330       return error_mark_node;
15331     }
15332   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15333     {
15334       error ("templates may not be %<virtual%>");
15335       return error_mark_node;
15336     }
15337
15338   return integer_zero_node;
15339 }
15340
15341 /* Parse a constant-initializer.
15342
15343    constant-initializer:
15344      = constant-expression
15345
15346    Returns a representation of the constant-expression.  */
15347
15348 static tree
15349 cp_parser_constant_initializer (cp_parser* parser)
15350 {
15351   /* Look for the `=' token.  */
15352   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15353     return error_mark_node;
15354
15355   /* It is invalid to write:
15356
15357        struct S { static const int i = { 7 }; };
15358
15359      */
15360   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15361     {
15362       cp_parser_error (parser,
15363                        "a brace-enclosed initializer is not allowed here");
15364       /* Consume the opening brace.  */
15365       cp_lexer_consume_token (parser->lexer);
15366       /* Skip the initializer.  */
15367       cp_parser_skip_to_closing_brace (parser);
15368       /* Look for the trailing `}'.  */
15369       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15370
15371       return error_mark_node;
15372     }
15373
15374   return cp_parser_constant_expression (parser,
15375                                         /*allow_non_constant=*/false,
15376                                         NULL);
15377 }
15378
15379 /* Derived classes [gram.class.derived] */
15380
15381 /* Parse a base-clause.
15382
15383    base-clause:
15384      : base-specifier-list
15385
15386    base-specifier-list:
15387      base-specifier ... [opt]
15388      base-specifier-list , base-specifier ... [opt]
15389
15390    Returns a TREE_LIST representing the base-classes, in the order in
15391    which they were declared.  The representation of each node is as
15392    described by cp_parser_base_specifier.
15393
15394    In the case that no bases are specified, this function will return
15395    NULL_TREE, not ERROR_MARK_NODE.  */
15396
15397 static tree
15398 cp_parser_base_clause (cp_parser* parser)
15399 {
15400   tree bases = NULL_TREE;
15401
15402   /* Look for the `:' that begins the list.  */
15403   cp_parser_require (parser, CPP_COLON, "%<:%>");
15404
15405   /* Scan the base-specifier-list.  */
15406   while (true)
15407     {
15408       cp_token *token;
15409       tree base;
15410       bool pack_expansion_p = false;
15411
15412       /* Look for the base-specifier.  */
15413       base = cp_parser_base_specifier (parser);
15414       /* Look for the (optional) ellipsis. */
15415       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15416         {
15417           /* Consume the `...'. */
15418           cp_lexer_consume_token (parser->lexer);
15419
15420           pack_expansion_p = true;
15421         }
15422
15423       /* Add BASE to the front of the list.  */
15424       if (base != error_mark_node)
15425         {
15426           if (pack_expansion_p)
15427             /* Make this a pack expansion type. */
15428             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15429           
15430
15431           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15432             {
15433               TREE_CHAIN (base) = bases;
15434               bases = base;
15435             }
15436         }
15437       /* Peek at the next token.  */
15438       token = cp_lexer_peek_token (parser->lexer);
15439       /* If it's not a comma, then the list is complete.  */
15440       if (token->type != CPP_COMMA)
15441         break;
15442       /* Consume the `,'.  */
15443       cp_lexer_consume_token (parser->lexer);
15444     }
15445
15446   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15447      base class had a qualified name.  However, the next name that
15448      appears is certainly not qualified.  */
15449   parser->scope = NULL_TREE;
15450   parser->qualifying_scope = NULL_TREE;
15451   parser->object_scope = NULL_TREE;
15452
15453   return nreverse (bases);
15454 }
15455
15456 /* Parse a base-specifier.
15457
15458    base-specifier:
15459      :: [opt] nested-name-specifier [opt] class-name
15460      virtual access-specifier [opt] :: [opt] nested-name-specifier
15461        [opt] class-name
15462      access-specifier virtual [opt] :: [opt] nested-name-specifier
15463        [opt] class-name
15464
15465    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15466    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15467    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15468    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15469
15470 static tree
15471 cp_parser_base_specifier (cp_parser* parser)
15472 {
15473   cp_token *token;
15474   bool done = false;
15475   bool virtual_p = false;
15476   bool duplicate_virtual_error_issued_p = false;
15477   bool duplicate_access_error_issued_p = false;
15478   bool class_scope_p, template_p;
15479   tree access = access_default_node;
15480   tree type;
15481
15482   /* Process the optional `virtual' and `access-specifier'.  */
15483   while (!done)
15484     {
15485       /* Peek at the next token.  */
15486       token = cp_lexer_peek_token (parser->lexer);
15487       /* Process `virtual'.  */
15488       switch (token->keyword)
15489         {
15490         case RID_VIRTUAL:
15491           /* If `virtual' appears more than once, issue an error.  */
15492           if (virtual_p && !duplicate_virtual_error_issued_p)
15493             {
15494               cp_parser_error (parser,
15495                                "%<virtual%> specified more than once in base-specified");
15496               duplicate_virtual_error_issued_p = true;
15497             }
15498
15499           virtual_p = true;
15500
15501           /* Consume the `virtual' token.  */
15502           cp_lexer_consume_token (parser->lexer);
15503
15504           break;
15505
15506         case RID_PUBLIC:
15507         case RID_PROTECTED:
15508         case RID_PRIVATE:
15509           /* If more than one access specifier appears, issue an
15510              error.  */
15511           if (access != access_default_node
15512               && !duplicate_access_error_issued_p)
15513             {
15514               cp_parser_error (parser,
15515                                "more than one access specifier in base-specified");
15516               duplicate_access_error_issued_p = true;
15517             }
15518
15519           access = ridpointers[(int) token->keyword];
15520
15521           /* Consume the access-specifier.  */
15522           cp_lexer_consume_token (parser->lexer);
15523
15524           break;
15525
15526         default:
15527           done = true;
15528           break;
15529         }
15530     }
15531   /* It is not uncommon to see programs mechanically, erroneously, use
15532      the 'typename' keyword to denote (dependent) qualified types
15533      as base classes.  */
15534   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15535     {
15536       if (!processing_template_decl)
15537         error ("keyword %<typename%> not allowed outside of templates");
15538       else
15539         error ("keyword %<typename%> not allowed in this context "
15540                "(the base class is implicitly a type)");
15541       cp_lexer_consume_token (parser->lexer);
15542     }
15543
15544   /* Look for the optional `::' operator.  */
15545   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15546   /* Look for the nested-name-specifier.  The simplest way to
15547      implement:
15548
15549        [temp.res]
15550
15551        The keyword `typename' is not permitted in a base-specifier or
15552        mem-initializer; in these contexts a qualified name that
15553        depends on a template-parameter is implicitly assumed to be a
15554        type name.
15555
15556      is to pretend that we have seen the `typename' keyword at this
15557      point.  */
15558   cp_parser_nested_name_specifier_opt (parser,
15559                                        /*typename_keyword_p=*/true,
15560                                        /*check_dependency_p=*/true,
15561                                        typename_type,
15562                                        /*is_declaration=*/true);
15563   /* If the base class is given by a qualified name, assume that names
15564      we see are type names or templates, as appropriate.  */
15565   class_scope_p = (parser->scope && TYPE_P (parser->scope));
15566   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15567
15568   /* Finally, look for the class-name.  */
15569   type = cp_parser_class_name (parser,
15570                                class_scope_p,
15571                                template_p,
15572                                typename_type,
15573                                /*check_dependency_p=*/true,
15574                                /*class_head_p=*/false,
15575                                /*is_declaration=*/true);
15576
15577   if (type == error_mark_node)
15578     return error_mark_node;
15579
15580   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15581 }
15582
15583 /* Exception handling [gram.exception] */
15584
15585 /* Parse an (optional) exception-specification.
15586
15587    exception-specification:
15588      throw ( type-id-list [opt] )
15589
15590    Returns a TREE_LIST representing the exception-specification.  The
15591    TREE_VALUE of each node is a type.  */
15592
15593 static tree
15594 cp_parser_exception_specification_opt (cp_parser* parser)
15595 {
15596   cp_token *token;
15597   tree type_id_list;
15598
15599   /* Peek at the next token.  */
15600   token = cp_lexer_peek_token (parser->lexer);
15601   /* If it's not `throw', then there's no exception-specification.  */
15602   if (!cp_parser_is_keyword (token, RID_THROW))
15603     return NULL_TREE;
15604
15605   /* Consume the `throw'.  */
15606   cp_lexer_consume_token (parser->lexer);
15607
15608   /* Look for the `('.  */
15609   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15610
15611   /* Peek at the next token.  */
15612   token = cp_lexer_peek_token (parser->lexer);
15613   /* If it's not a `)', then there is a type-id-list.  */
15614   if (token->type != CPP_CLOSE_PAREN)
15615     {
15616       const char *saved_message;
15617
15618       /* Types may not be defined in an exception-specification.  */
15619       saved_message = parser->type_definition_forbidden_message;
15620       parser->type_definition_forbidden_message
15621         = "types may not be defined in an exception-specification";
15622       /* Parse the type-id-list.  */
15623       type_id_list = cp_parser_type_id_list (parser);
15624       /* Restore the saved message.  */
15625       parser->type_definition_forbidden_message = saved_message;
15626     }
15627   else
15628     type_id_list = empty_except_spec;
15629
15630   /* Look for the `)'.  */
15631   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15632
15633   return type_id_list;
15634 }
15635
15636 /* Parse an (optional) type-id-list.
15637
15638    type-id-list:
15639      type-id ... [opt]
15640      type-id-list , type-id ... [opt]
15641
15642    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
15643    in the order that the types were presented.  */
15644
15645 static tree
15646 cp_parser_type_id_list (cp_parser* parser)
15647 {
15648   tree types = NULL_TREE;
15649
15650   while (true)
15651     {
15652       cp_token *token;
15653       tree type;
15654
15655       /* Get the next type-id.  */
15656       type = cp_parser_type_id (parser);
15657       /* Parse the optional ellipsis. */
15658       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15659         {
15660           /* Consume the `...'. */
15661           cp_lexer_consume_token (parser->lexer);
15662
15663           /* Turn the type into a pack expansion expression. */
15664           type = make_pack_expansion (type);
15665         }
15666       /* Add it to the list.  */
15667       types = add_exception_specifier (types, type, /*complain=*/1);
15668       /* Peek at the next token.  */
15669       token = cp_lexer_peek_token (parser->lexer);
15670       /* If it is not a `,', we are done.  */
15671       if (token->type != CPP_COMMA)
15672         break;
15673       /* Consume the `,'.  */
15674       cp_lexer_consume_token (parser->lexer);
15675     }
15676
15677   return nreverse (types);
15678 }
15679
15680 /* Parse a try-block.
15681
15682    try-block:
15683      try compound-statement handler-seq  */
15684
15685 static tree
15686 cp_parser_try_block (cp_parser* parser)
15687 {
15688   tree try_block;
15689
15690   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
15691   try_block = begin_try_block ();
15692   cp_parser_compound_statement (parser, NULL, true);
15693   finish_try_block (try_block);
15694   cp_parser_handler_seq (parser);
15695   finish_handler_sequence (try_block);
15696
15697   return try_block;
15698 }
15699
15700 /* Parse a function-try-block.
15701
15702    function-try-block:
15703      try ctor-initializer [opt] function-body handler-seq  */
15704
15705 static bool
15706 cp_parser_function_try_block (cp_parser* parser)
15707 {
15708   tree compound_stmt;
15709   tree try_block;
15710   bool ctor_initializer_p;
15711
15712   /* Look for the `try' keyword.  */
15713   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
15714     return false;
15715   /* Let the rest of the front end know where we are.  */
15716   try_block = begin_function_try_block (&compound_stmt);
15717   /* Parse the function-body.  */
15718   ctor_initializer_p
15719     = cp_parser_ctor_initializer_opt_and_function_body (parser);
15720   /* We're done with the `try' part.  */
15721   finish_function_try_block (try_block);
15722   /* Parse the handlers.  */
15723   cp_parser_handler_seq (parser);
15724   /* We're done with the handlers.  */
15725   finish_function_handler_sequence (try_block, compound_stmt);
15726
15727   return ctor_initializer_p;
15728 }
15729
15730 /* Parse a handler-seq.
15731
15732    handler-seq:
15733      handler handler-seq [opt]  */
15734
15735 static void
15736 cp_parser_handler_seq (cp_parser* parser)
15737 {
15738   while (true)
15739     {
15740       cp_token *token;
15741
15742       /* Parse the handler.  */
15743       cp_parser_handler (parser);
15744       /* Peek at the next token.  */
15745       token = cp_lexer_peek_token (parser->lexer);
15746       /* If it's not `catch' then there are no more handlers.  */
15747       if (!cp_parser_is_keyword (token, RID_CATCH))
15748         break;
15749     }
15750 }
15751
15752 /* Parse a handler.
15753
15754    handler:
15755      catch ( exception-declaration ) compound-statement  */
15756
15757 static void
15758 cp_parser_handler (cp_parser* parser)
15759 {
15760   tree handler;
15761   tree declaration;
15762
15763   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
15764   handler = begin_handler ();
15765   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15766   declaration = cp_parser_exception_declaration (parser);
15767   finish_handler_parms (declaration, handler);
15768   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15769   cp_parser_compound_statement (parser, NULL, false);
15770   finish_handler (handler);
15771 }
15772
15773 /* Parse an exception-declaration.
15774
15775    exception-declaration:
15776      type-specifier-seq declarator
15777      type-specifier-seq abstract-declarator
15778      type-specifier-seq
15779      ...
15780
15781    Returns a VAR_DECL for the declaration, or NULL_TREE if the
15782    ellipsis variant is used.  */
15783
15784 static tree
15785 cp_parser_exception_declaration (cp_parser* parser)
15786 {
15787   cp_decl_specifier_seq type_specifiers;
15788   cp_declarator *declarator;
15789   const char *saved_message;
15790
15791   /* If it's an ellipsis, it's easy to handle.  */
15792   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15793     {
15794       /* Consume the `...' token.  */
15795       cp_lexer_consume_token (parser->lexer);
15796       return NULL_TREE;
15797     }
15798
15799   /* Types may not be defined in exception-declarations.  */
15800   saved_message = parser->type_definition_forbidden_message;
15801   parser->type_definition_forbidden_message
15802     = "types may not be defined in exception-declarations";
15803
15804   /* Parse the type-specifier-seq.  */
15805   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15806                                 &type_specifiers);
15807   /* If it's a `)', then there is no declarator.  */
15808   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15809     declarator = NULL;
15810   else
15811     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15812                                        /*ctor_dtor_or_conv_p=*/NULL,
15813                                        /*parenthesized_p=*/NULL,
15814                                        /*member_p=*/false);
15815
15816   /* Restore the saved message.  */
15817   parser->type_definition_forbidden_message = saved_message;
15818
15819   if (!type_specifiers.any_specifiers_p)
15820     return error_mark_node;
15821
15822   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15823 }
15824
15825 /* Parse a throw-expression.
15826
15827    throw-expression:
15828      throw assignment-expression [opt]
15829
15830    Returns a THROW_EXPR representing the throw-expression.  */
15831
15832 static tree
15833 cp_parser_throw_expression (cp_parser* parser)
15834 {
15835   tree expression;
15836   cp_token* token;
15837
15838   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
15839   token = cp_lexer_peek_token (parser->lexer);
15840   /* Figure out whether or not there is an assignment-expression
15841      following the "throw" keyword.  */
15842   if (token->type == CPP_COMMA
15843       || token->type == CPP_SEMICOLON
15844       || token->type == CPP_CLOSE_PAREN
15845       || token->type == CPP_CLOSE_SQUARE
15846       || token->type == CPP_CLOSE_BRACE
15847       || token->type == CPP_COLON)
15848     expression = NULL_TREE;
15849   else
15850     expression = cp_parser_assignment_expression (parser,
15851                                                   /*cast_p=*/false);
15852
15853   return build_throw (expression);
15854 }
15855
15856 /* GNU Extensions */
15857
15858 /* Parse an (optional) asm-specification.
15859
15860    asm-specification:
15861      asm ( string-literal )
15862
15863    If the asm-specification is present, returns a STRING_CST
15864    corresponding to the string-literal.  Otherwise, returns
15865    NULL_TREE.  */
15866
15867 static tree
15868 cp_parser_asm_specification_opt (cp_parser* parser)
15869 {
15870   cp_token *token;
15871   tree asm_specification;
15872
15873   /* Peek at the next token.  */
15874   token = cp_lexer_peek_token (parser->lexer);
15875   /* If the next token isn't the `asm' keyword, then there's no
15876      asm-specification.  */
15877   if (!cp_parser_is_keyword (token, RID_ASM))
15878     return NULL_TREE;
15879
15880   /* Consume the `asm' token.  */
15881   cp_lexer_consume_token (parser->lexer);
15882   /* Look for the `('.  */
15883   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15884
15885   /* Look for the string-literal.  */
15886   asm_specification = cp_parser_string_literal (parser, false, false);
15887
15888   /* Look for the `)'.  */
15889   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15890
15891   return asm_specification;
15892 }
15893
15894 /* Parse an asm-operand-list.
15895
15896    asm-operand-list:
15897      asm-operand
15898      asm-operand-list , asm-operand
15899
15900    asm-operand:
15901      string-literal ( expression )
15902      [ string-literal ] string-literal ( expression )
15903
15904    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
15905    each node is the expression.  The TREE_PURPOSE is itself a
15906    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15907    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15908    is a STRING_CST for the string literal before the parenthesis. Returns
15909    ERROR_MARK_NODE if any of the operands are invalid.  */
15910
15911 static tree
15912 cp_parser_asm_operand_list (cp_parser* parser)
15913 {
15914   tree asm_operands = NULL_TREE;
15915   bool invalid_operands = false;
15916
15917   while (true)
15918     {
15919       tree string_literal;
15920       tree expression;
15921       tree name;
15922
15923       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15924         {
15925           /* Consume the `[' token.  */
15926           cp_lexer_consume_token (parser->lexer);
15927           /* Read the operand name.  */
15928           name = cp_parser_identifier (parser);
15929           if (name != error_mark_node)
15930             name = build_string (IDENTIFIER_LENGTH (name),
15931                                  IDENTIFIER_POINTER (name));
15932           /* Look for the closing `]'.  */
15933           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
15934         }
15935       else
15936         name = NULL_TREE;
15937       /* Look for the string-literal.  */
15938       string_literal = cp_parser_string_literal (parser, false, false);
15939
15940       /* Look for the `('.  */
15941       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15942       /* Parse the expression.  */
15943       expression = cp_parser_expression (parser, /*cast_p=*/false);
15944       /* Look for the `)'.  */
15945       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15946
15947       if (name == error_mark_node 
15948           || string_literal == error_mark_node 
15949           || expression == error_mark_node)
15950         invalid_operands = true;
15951
15952       /* Add this operand to the list.  */
15953       asm_operands = tree_cons (build_tree_list (name, string_literal),
15954                                 expression,
15955                                 asm_operands);
15956       /* If the next token is not a `,', there are no more
15957          operands.  */
15958       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15959         break;
15960       /* Consume the `,'.  */
15961       cp_lexer_consume_token (parser->lexer);
15962     }
15963
15964   return invalid_operands ? error_mark_node : nreverse (asm_operands);
15965 }
15966
15967 /* Parse an asm-clobber-list.
15968
15969    asm-clobber-list:
15970      string-literal
15971      asm-clobber-list , string-literal
15972
15973    Returns a TREE_LIST, indicating the clobbers in the order that they
15974    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
15975
15976 static tree
15977 cp_parser_asm_clobber_list (cp_parser* parser)
15978 {
15979   tree clobbers = NULL_TREE;
15980
15981   while (true)
15982     {
15983       tree string_literal;
15984
15985       /* Look for the string literal.  */
15986       string_literal = cp_parser_string_literal (parser, false, false);
15987       /* Add it to the list.  */
15988       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15989       /* If the next token is not a `,', then the list is
15990          complete.  */
15991       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15992         break;
15993       /* Consume the `,' token.  */
15994       cp_lexer_consume_token (parser->lexer);
15995     }
15996
15997   return clobbers;
15998 }
15999
16000 /* Parse an (optional) series of attributes.
16001
16002    attributes:
16003      attributes attribute
16004
16005    attribute:
16006      __attribute__ (( attribute-list [opt] ))
16007
16008    The return value is as for cp_parser_attribute_list.  */
16009
16010 static tree
16011 cp_parser_attributes_opt (cp_parser* parser)
16012 {
16013   tree attributes = NULL_TREE;
16014
16015   while (true)
16016     {
16017       cp_token *token;
16018       tree attribute_list;
16019
16020       /* Peek at the next token.  */
16021       token = cp_lexer_peek_token (parser->lexer);
16022       /* If it's not `__attribute__', then we're done.  */
16023       if (token->keyword != RID_ATTRIBUTE)
16024         break;
16025
16026       /* Consume the `__attribute__' keyword.  */
16027       cp_lexer_consume_token (parser->lexer);
16028       /* Look for the two `(' tokens.  */
16029       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16030       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16031
16032       /* Peek at the next token.  */
16033       token = cp_lexer_peek_token (parser->lexer);
16034       if (token->type != CPP_CLOSE_PAREN)
16035         /* Parse the attribute-list.  */
16036         attribute_list = cp_parser_attribute_list (parser);
16037       else
16038         /* If the next token is a `)', then there is no attribute
16039            list.  */
16040         attribute_list = NULL;
16041
16042       /* Look for the two `)' tokens.  */
16043       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16044       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16045
16046       /* Add these new attributes to the list.  */
16047       attributes = chainon (attributes, attribute_list);
16048     }
16049
16050   return attributes;
16051 }
16052
16053 /* Parse an attribute-list.
16054
16055    attribute-list:
16056      attribute
16057      attribute-list , attribute
16058
16059    attribute:
16060      identifier
16061      identifier ( identifier )
16062      identifier ( identifier , expression-list )
16063      identifier ( expression-list )
16064
16065    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16066    to an attribute.  The TREE_PURPOSE of each node is the identifier
16067    indicating which attribute is in use.  The TREE_VALUE represents
16068    the arguments, if any.  */
16069
16070 static tree
16071 cp_parser_attribute_list (cp_parser* parser)
16072 {
16073   tree attribute_list = NULL_TREE;
16074   bool save_translate_strings_p = parser->translate_strings_p;
16075
16076   parser->translate_strings_p = false;
16077   while (true)
16078     {
16079       cp_token *token;
16080       tree identifier;
16081       tree attribute;
16082
16083       /* Look for the identifier.  We also allow keywords here; for
16084          example `__attribute__ ((const))' is legal.  */
16085       token = cp_lexer_peek_token (parser->lexer);
16086       if (token->type == CPP_NAME
16087           || token->type == CPP_KEYWORD)
16088         {
16089           tree arguments = NULL_TREE;
16090
16091           /* Consume the token.  */
16092           token = cp_lexer_consume_token (parser->lexer);
16093
16094           /* Save away the identifier that indicates which attribute
16095              this is.  */
16096           identifier = token->u.value;
16097           attribute = build_tree_list (identifier, NULL_TREE);
16098
16099           /* Peek at the next token.  */
16100           token = cp_lexer_peek_token (parser->lexer);
16101           /* If it's an `(', then parse the attribute arguments.  */
16102           if (token->type == CPP_OPEN_PAREN)
16103             {
16104               arguments = cp_parser_parenthesized_expression_list
16105                           (parser, true, /*cast_p=*/false,
16106                            /*allow_expansion_p=*/false,
16107                            /*non_constant_p=*/NULL);
16108               /* Save the arguments away.  */
16109               TREE_VALUE (attribute) = arguments;
16110             }
16111
16112           if (arguments != error_mark_node)
16113             {
16114               /* Add this attribute to the list.  */
16115               TREE_CHAIN (attribute) = attribute_list;
16116               attribute_list = attribute;
16117             }
16118
16119           token = cp_lexer_peek_token (parser->lexer);
16120         }
16121       /* Now, look for more attributes.  If the next token isn't a
16122          `,', we're done.  */
16123       if (token->type != CPP_COMMA)
16124         break;
16125
16126       /* Consume the comma and keep going.  */
16127       cp_lexer_consume_token (parser->lexer);
16128     }
16129   parser->translate_strings_p = save_translate_strings_p;
16130
16131   /* We built up the list in reverse order.  */
16132   return nreverse (attribute_list);
16133 }
16134
16135 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16136    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16137    current value of the PEDANTIC flag, regardless of whether or not
16138    the `__extension__' keyword is present.  The caller is responsible
16139    for restoring the value of the PEDANTIC flag.  */
16140
16141 static bool
16142 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16143 {
16144   /* Save the old value of the PEDANTIC flag.  */
16145   *saved_pedantic = pedantic;
16146
16147   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16148     {
16149       /* Consume the `__extension__' token.  */
16150       cp_lexer_consume_token (parser->lexer);
16151       /* We're not being pedantic while the `__extension__' keyword is
16152          in effect.  */
16153       pedantic = 0;
16154
16155       return true;
16156     }
16157
16158   return false;
16159 }
16160
16161 /* Parse a label declaration.
16162
16163    label-declaration:
16164      __label__ label-declarator-seq ;
16165
16166    label-declarator-seq:
16167      identifier , label-declarator-seq
16168      identifier  */
16169
16170 static void
16171 cp_parser_label_declaration (cp_parser* parser)
16172 {
16173   /* Look for the `__label__' keyword.  */
16174   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16175
16176   while (true)
16177     {
16178       tree identifier;
16179
16180       /* Look for an identifier.  */
16181       identifier = cp_parser_identifier (parser);
16182       /* If we failed, stop.  */
16183       if (identifier == error_mark_node)
16184         break;
16185       /* Declare it as a label.  */
16186       finish_label_decl (identifier);
16187       /* If the next token is a `;', stop.  */
16188       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16189         break;
16190       /* Look for the `,' separating the label declarations.  */
16191       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16192     }
16193
16194   /* Look for the final `;'.  */
16195   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16196 }
16197
16198 /* Support Functions */
16199
16200 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16201    NAME should have one of the representations used for an
16202    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16203    is returned.  If PARSER->SCOPE is a dependent type, then a
16204    SCOPE_REF is returned.
16205
16206    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16207    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16208    was formed.  Abstractly, such entities should not be passed to this
16209    function, because they do not need to be looked up, but it is
16210    simpler to check for this special case here, rather than at the
16211    call-sites.
16212
16213    In cases not explicitly covered above, this function returns a
16214    DECL, OVERLOAD, or baselink representing the result of the lookup.
16215    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16216    is returned.
16217
16218    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16219    (e.g., "struct") that was used.  In that case bindings that do not
16220    refer to types are ignored.
16221
16222    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16223    ignored.
16224
16225    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16226    are ignored.
16227
16228    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16229    types.
16230
16231    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16232    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16233    NULL_TREE otherwise.  */
16234
16235 static tree
16236 cp_parser_lookup_name (cp_parser *parser, tree name,
16237                        enum tag_types tag_type,
16238                        bool is_template,
16239                        bool is_namespace,
16240                        bool check_dependency,
16241                        tree *ambiguous_decls)
16242 {
16243   int flags = 0;
16244   tree decl;
16245   tree object_type = parser->context->object_type;
16246
16247   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16248     flags |= LOOKUP_COMPLAIN;
16249
16250   /* Assume that the lookup will be unambiguous.  */
16251   if (ambiguous_decls)
16252     *ambiguous_decls = NULL_TREE;
16253
16254   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16255      no longer valid.  Note that if we are parsing tentatively, and
16256      the parse fails, OBJECT_TYPE will be automatically restored.  */
16257   parser->context->object_type = NULL_TREE;
16258
16259   if (name == error_mark_node)
16260     return error_mark_node;
16261
16262   /* A template-id has already been resolved; there is no lookup to
16263      do.  */
16264   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16265     return name;
16266   if (BASELINK_P (name))
16267     {
16268       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16269                   == TEMPLATE_ID_EXPR);
16270       return name;
16271     }
16272
16273   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16274      it should already have been checked to make sure that the name
16275      used matches the type being destroyed.  */
16276   if (TREE_CODE (name) == BIT_NOT_EXPR)
16277     {
16278       tree type;
16279
16280       /* Figure out to which type this destructor applies.  */
16281       if (parser->scope)
16282         type = parser->scope;
16283       else if (object_type)
16284         type = object_type;
16285       else
16286         type = current_class_type;
16287       /* If that's not a class type, there is no destructor.  */
16288       if (!type || !CLASS_TYPE_P (type))
16289         return error_mark_node;
16290       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16291         lazily_declare_fn (sfk_destructor, type);
16292       if (!CLASSTYPE_DESTRUCTORS (type))
16293           return error_mark_node;
16294       /* If it was a class type, return the destructor.  */
16295       return CLASSTYPE_DESTRUCTORS (type);
16296     }
16297
16298   /* By this point, the NAME should be an ordinary identifier.  If
16299      the id-expression was a qualified name, the qualifying scope is
16300      stored in PARSER->SCOPE at this point.  */
16301   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16302
16303   /* Perform the lookup.  */
16304   if (parser->scope)
16305     {
16306       bool dependent_p;
16307
16308       if (parser->scope == error_mark_node)
16309         return error_mark_node;
16310
16311       /* If the SCOPE is dependent, the lookup must be deferred until
16312          the template is instantiated -- unless we are explicitly
16313          looking up names in uninstantiated templates.  Even then, we
16314          cannot look up the name if the scope is not a class type; it
16315          might, for example, be a template type parameter.  */
16316       dependent_p = (TYPE_P (parser->scope)
16317                      && !(parser->in_declarator_p
16318                           && currently_open_class (parser->scope))
16319                      && dependent_type_p (parser->scope));
16320       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16321            && dependent_p)
16322         {
16323           if (tag_type)
16324             {
16325               tree type;
16326
16327               /* The resolution to Core Issue 180 says that `struct
16328                  A::B' should be considered a type-name, even if `A'
16329                  is dependent.  */
16330               type = make_typename_type (parser->scope, name, tag_type,
16331                                          /*complain=*/tf_error);
16332               decl = TYPE_NAME (type);
16333             }
16334           else if (is_template
16335                    && (cp_parser_next_token_ends_template_argument_p (parser)
16336                        || cp_lexer_next_token_is (parser->lexer,
16337                                                   CPP_CLOSE_PAREN)))
16338             decl = make_unbound_class_template (parser->scope,
16339                                                 name, NULL_TREE,
16340                                                 /*complain=*/tf_error);
16341           else
16342             decl = build_qualified_name (/*type=*/NULL_TREE,
16343                                          parser->scope, name,
16344                                          is_template);
16345         }
16346       else
16347         {
16348           tree pushed_scope = NULL_TREE;
16349
16350           /* If PARSER->SCOPE is a dependent type, then it must be a
16351              class type, and we must not be checking dependencies;
16352              otherwise, we would have processed this lookup above.  So
16353              that PARSER->SCOPE is not considered a dependent base by
16354              lookup_member, we must enter the scope here.  */
16355           if (dependent_p)
16356             pushed_scope = push_scope (parser->scope);
16357           /* If the PARSER->SCOPE is a template specialization, it
16358              may be instantiated during name lookup.  In that case,
16359              errors may be issued.  Even if we rollback the current
16360              tentative parse, those errors are valid.  */
16361           decl = lookup_qualified_name (parser->scope, name,
16362                                         tag_type != none_type,
16363                                         /*complain=*/true);
16364           if (pushed_scope)
16365             pop_scope (pushed_scope);
16366         }
16367       parser->qualifying_scope = parser->scope;
16368       parser->object_scope = NULL_TREE;
16369     }
16370   else if (object_type)
16371     {
16372       tree object_decl = NULL_TREE;
16373       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16374          OBJECT_TYPE is not a class.  */
16375       if (CLASS_TYPE_P (object_type))
16376         /* If the OBJECT_TYPE is a template specialization, it may
16377            be instantiated during name lookup.  In that case, errors
16378            may be issued.  Even if we rollback the current tentative
16379            parse, those errors are valid.  */
16380         object_decl = lookup_member (object_type,
16381                                      name,
16382                                      /*protect=*/0,
16383                                      tag_type != none_type);
16384       /* Look it up in the enclosing context, too.  */
16385       decl = lookup_name_real (name, tag_type != none_type,
16386                                /*nonclass=*/0,
16387                                /*block_p=*/true, is_namespace, flags);
16388       parser->object_scope = object_type;
16389       parser->qualifying_scope = NULL_TREE;
16390       if (object_decl)
16391         decl = object_decl;
16392     }
16393   else
16394     {
16395       decl = lookup_name_real (name, tag_type != none_type,
16396                                /*nonclass=*/0,
16397                                /*block_p=*/true, is_namespace, flags);
16398       parser->qualifying_scope = NULL_TREE;
16399       parser->object_scope = NULL_TREE;
16400     }
16401
16402   /* If the lookup failed, let our caller know.  */
16403   if (!decl || decl == error_mark_node)
16404     return error_mark_node;
16405
16406   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16407   if (TREE_CODE (decl) == TREE_LIST)
16408     {
16409       if (ambiguous_decls)
16410         *ambiguous_decls = decl;
16411       /* The error message we have to print is too complicated for
16412          cp_parser_error, so we incorporate its actions directly.  */
16413       if (!cp_parser_simulate_error (parser))
16414         {
16415           error ("reference to %qD is ambiguous", name);
16416           print_candidates (decl);
16417         }
16418       return error_mark_node;
16419     }
16420
16421   gcc_assert (DECL_P (decl)
16422               || TREE_CODE (decl) == OVERLOAD
16423               || TREE_CODE (decl) == SCOPE_REF
16424               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16425               || BASELINK_P (decl));
16426
16427   /* If we have resolved the name of a member declaration, check to
16428      see if the declaration is accessible.  When the name resolves to
16429      set of overloaded functions, accessibility is checked when
16430      overload resolution is done.
16431
16432      During an explicit instantiation, access is not checked at all,
16433      as per [temp.explicit].  */
16434   if (DECL_P (decl))
16435     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16436
16437   return decl;
16438 }
16439
16440 /* Like cp_parser_lookup_name, but for use in the typical case where
16441    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16442    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16443
16444 static tree
16445 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16446 {
16447   return cp_parser_lookup_name (parser, name,
16448                                 none_type,
16449                                 /*is_template=*/false,
16450                                 /*is_namespace=*/false,
16451                                 /*check_dependency=*/true,
16452                                 /*ambiguous_decls=*/NULL);
16453 }
16454
16455 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16456    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16457    true, the DECL indicates the class being defined in a class-head,
16458    or declared in an elaborated-type-specifier.
16459
16460    Otherwise, return DECL.  */
16461
16462 static tree
16463 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16464 {
16465   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16466      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16467
16468        struct A {
16469          template <typename T> struct B;
16470        };
16471
16472        template <typename T> struct A::B {};
16473
16474      Similarly, in an elaborated-type-specifier:
16475
16476        namespace N { struct X{}; }
16477
16478        struct A {
16479          template <typename T> friend struct N::X;
16480        };
16481
16482      However, if the DECL refers to a class type, and we are in
16483      the scope of the class, then the name lookup automatically
16484      finds the TYPE_DECL created by build_self_reference rather
16485      than a TEMPLATE_DECL.  For example, in:
16486
16487        template <class T> struct S {
16488          S s;
16489        };
16490
16491      there is no need to handle such case.  */
16492
16493   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16494     return DECL_TEMPLATE_RESULT (decl);
16495
16496   return decl;
16497 }
16498
16499 /* If too many, or too few, template-parameter lists apply to the
16500    declarator, issue an error message.  Returns TRUE if all went well,
16501    and FALSE otherwise.  */
16502
16503 static bool
16504 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16505                                                 cp_declarator *declarator)
16506 {
16507   unsigned num_templates;
16508
16509   /* We haven't seen any classes that involve template parameters yet.  */
16510   num_templates = 0;
16511
16512   switch (declarator->kind)
16513     {
16514     case cdk_id:
16515       if (declarator->u.id.qualifying_scope)
16516         {
16517           tree scope;
16518           tree member;
16519
16520           scope = declarator->u.id.qualifying_scope;
16521           member = declarator->u.id.unqualified_name;
16522
16523           while (scope && CLASS_TYPE_P (scope))
16524             {
16525               /* You're supposed to have one `template <...>'
16526                  for every template class, but you don't need one
16527                  for a full specialization.  For example:
16528
16529                  template <class T> struct S{};
16530                  template <> struct S<int> { void f(); };
16531                  void S<int>::f () {}
16532
16533                  is correct; there shouldn't be a `template <>' for
16534                  the definition of `S<int>::f'.  */
16535               if (!CLASSTYPE_TEMPLATE_INFO (scope))
16536                 /* If SCOPE does not have template information of any
16537                    kind, then it is not a template, nor is it nested
16538                    within a template.  */
16539                 break;
16540               if (explicit_class_specialization_p (scope))
16541                 break;
16542               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16543                 ++num_templates;
16544
16545               scope = TYPE_CONTEXT (scope);
16546             }
16547         }
16548       else if (TREE_CODE (declarator->u.id.unqualified_name)
16549                == TEMPLATE_ID_EXPR)
16550         /* If the DECLARATOR has the form `X<y>' then it uses one
16551            additional level of template parameters.  */
16552         ++num_templates;
16553
16554       return cp_parser_check_template_parameters (parser,
16555                                                   num_templates);
16556
16557     case cdk_function:
16558     case cdk_array:
16559     case cdk_pointer:
16560     case cdk_reference:
16561     case cdk_ptrmem:
16562       return (cp_parser_check_declarator_template_parameters
16563               (parser, declarator->declarator));
16564
16565     case cdk_error:
16566       return true;
16567
16568     default:
16569       gcc_unreachable ();
16570     }
16571   return false;
16572 }
16573
16574 /* NUM_TEMPLATES were used in the current declaration.  If that is
16575    invalid, return FALSE and issue an error messages.  Otherwise,
16576    return TRUE.  */
16577
16578 static bool
16579 cp_parser_check_template_parameters (cp_parser* parser,
16580                                      unsigned num_templates)
16581 {
16582   /* If there are more template classes than parameter lists, we have
16583      something like:
16584
16585        template <class T> void S<T>::R<T>::f ();  */
16586   if (parser->num_template_parameter_lists < num_templates)
16587     {
16588       error ("too few template-parameter-lists");
16589       return false;
16590     }
16591   /* If there are the same number of template classes and parameter
16592      lists, that's OK.  */
16593   if (parser->num_template_parameter_lists == num_templates)
16594     return true;
16595   /* If there are more, but only one more, then we are referring to a
16596      member template.  That's OK too.  */
16597   if (parser->num_template_parameter_lists == num_templates + 1)
16598       return true;
16599   /* Otherwise, there are too many template parameter lists.  We have
16600      something like:
16601
16602      template <class T> template <class U> void S::f();  */
16603   error ("too many template-parameter-lists");
16604   return false;
16605 }
16606
16607 /* Parse an optional `::' token indicating that the following name is
16608    from the global namespace.  If so, PARSER->SCOPE is set to the
16609    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16610    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16611    Returns the new value of PARSER->SCOPE, if the `::' token is
16612    present, and NULL_TREE otherwise.  */
16613
16614 static tree
16615 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16616 {
16617   cp_token *token;
16618
16619   /* Peek at the next token.  */
16620   token = cp_lexer_peek_token (parser->lexer);
16621   /* If we're looking at a `::' token then we're starting from the
16622      global namespace, not our current location.  */
16623   if (token->type == CPP_SCOPE)
16624     {
16625       /* Consume the `::' token.  */
16626       cp_lexer_consume_token (parser->lexer);
16627       /* Set the SCOPE so that we know where to start the lookup.  */
16628       parser->scope = global_namespace;
16629       parser->qualifying_scope = global_namespace;
16630       parser->object_scope = NULL_TREE;
16631
16632       return parser->scope;
16633     }
16634   else if (!current_scope_valid_p)
16635     {
16636       parser->scope = NULL_TREE;
16637       parser->qualifying_scope = NULL_TREE;
16638       parser->object_scope = NULL_TREE;
16639     }
16640
16641   return NULL_TREE;
16642 }
16643
16644 /* Returns TRUE if the upcoming token sequence is the start of a
16645    constructor declarator.  If FRIEND_P is true, the declarator is
16646    preceded by the `friend' specifier.  */
16647
16648 static bool
16649 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16650 {
16651   bool constructor_p;
16652   tree type_decl = NULL_TREE;
16653   bool nested_name_p;
16654   cp_token *next_token;
16655
16656   /* The common case is that this is not a constructor declarator, so
16657      try to avoid doing lots of work if at all possible.  It's not
16658      valid declare a constructor at function scope.  */
16659   if (parser->in_function_body)
16660     return false;
16661   /* And only certain tokens can begin a constructor declarator.  */
16662   next_token = cp_lexer_peek_token (parser->lexer);
16663   if (next_token->type != CPP_NAME
16664       && next_token->type != CPP_SCOPE
16665       && next_token->type != CPP_NESTED_NAME_SPECIFIER
16666       && next_token->type != CPP_TEMPLATE_ID)
16667     return false;
16668
16669   /* Parse tentatively; we are going to roll back all of the tokens
16670      consumed here.  */
16671   cp_parser_parse_tentatively (parser);
16672   /* Assume that we are looking at a constructor declarator.  */
16673   constructor_p = true;
16674
16675   /* Look for the optional `::' operator.  */
16676   cp_parser_global_scope_opt (parser,
16677                               /*current_scope_valid_p=*/false);
16678   /* Look for the nested-name-specifier.  */
16679   nested_name_p
16680     = (cp_parser_nested_name_specifier_opt (parser,
16681                                             /*typename_keyword_p=*/false,
16682                                             /*check_dependency_p=*/false,
16683                                             /*type_p=*/false,
16684                                             /*is_declaration=*/false)
16685        != NULL_TREE);
16686   /* Outside of a class-specifier, there must be a
16687      nested-name-specifier.  */
16688   if (!nested_name_p &&
16689       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16690        || friend_p))
16691     constructor_p = false;
16692   /* If we still think that this might be a constructor-declarator,
16693      look for a class-name.  */
16694   if (constructor_p)
16695     {
16696       /* If we have:
16697
16698            template <typename T> struct S { S(); };
16699            template <typename T> S<T>::S ();
16700
16701          we must recognize that the nested `S' names a class.
16702          Similarly, for:
16703
16704            template <typename T> S<T>::S<T> ();
16705
16706          we must recognize that the nested `S' names a template.  */
16707       type_decl = cp_parser_class_name (parser,
16708                                         /*typename_keyword_p=*/false,
16709                                         /*template_keyword_p=*/false,
16710                                         none_type,
16711                                         /*check_dependency_p=*/false,
16712                                         /*class_head_p=*/false,
16713                                         /*is_declaration=*/false);
16714       /* If there was no class-name, then this is not a constructor.  */
16715       constructor_p = !cp_parser_error_occurred (parser);
16716     }
16717
16718   /* If we're still considering a constructor, we have to see a `(',
16719      to begin the parameter-declaration-clause, followed by either a
16720      `)', an `...', or a decl-specifier.  We need to check for a
16721      type-specifier to avoid being fooled into thinking that:
16722
16723        S::S (f) (int);
16724
16725      is a constructor.  (It is actually a function named `f' that
16726      takes one parameter (of type `int') and returns a value of type
16727      `S::S'.  */
16728   if (constructor_p
16729       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
16730     {
16731       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16732           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16733           /* A parameter declaration begins with a decl-specifier,
16734              which is either the "attribute" keyword, a storage class
16735              specifier, or (usually) a type-specifier.  */
16736           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16737         {
16738           tree type;
16739           tree pushed_scope = NULL_TREE;
16740           unsigned saved_num_template_parameter_lists;
16741
16742           /* Names appearing in the type-specifier should be looked up
16743              in the scope of the class.  */
16744           if (current_class_type)
16745             type = NULL_TREE;
16746           else
16747             {
16748               type = TREE_TYPE (type_decl);
16749               if (TREE_CODE (type) == TYPENAME_TYPE)
16750                 {
16751                   type = resolve_typename_type (type,
16752                                                 /*only_current_p=*/false);
16753                   if (TREE_CODE (type) == TYPENAME_TYPE)
16754                     {
16755                       cp_parser_abort_tentative_parse (parser);
16756                       return false;
16757                     }
16758                 }
16759               pushed_scope = push_scope (type);
16760             }
16761
16762           /* Inside the constructor parameter list, surrounding
16763              template-parameter-lists do not apply.  */
16764           saved_num_template_parameter_lists
16765             = parser->num_template_parameter_lists;
16766           parser->num_template_parameter_lists = 0;
16767
16768           /* Look for the type-specifier.  */
16769           cp_parser_type_specifier (parser,
16770                                     CP_PARSER_FLAGS_NONE,
16771                                     /*decl_specs=*/NULL,
16772                                     /*is_declarator=*/true,
16773                                     /*declares_class_or_enum=*/NULL,
16774                                     /*is_cv_qualifier=*/NULL);
16775
16776           parser->num_template_parameter_lists
16777             = saved_num_template_parameter_lists;
16778
16779           /* Leave the scope of the class.  */
16780           if (pushed_scope)
16781             pop_scope (pushed_scope);
16782
16783           constructor_p = !cp_parser_error_occurred (parser);
16784         }
16785     }
16786   else
16787     constructor_p = false;
16788   /* We did not really want to consume any tokens.  */
16789   cp_parser_abort_tentative_parse (parser);
16790
16791   return constructor_p;
16792 }
16793
16794 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16795    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
16796    they must be performed once we are in the scope of the function.
16797
16798    Returns the function defined.  */
16799
16800 static tree
16801 cp_parser_function_definition_from_specifiers_and_declarator
16802   (cp_parser* parser,
16803    cp_decl_specifier_seq *decl_specifiers,
16804    tree attributes,
16805    const cp_declarator *declarator)
16806 {
16807   tree fn;
16808   bool success_p;
16809
16810   /* Begin the function-definition.  */
16811   success_p = start_function (decl_specifiers, declarator, attributes);
16812
16813   /* The things we're about to see are not directly qualified by any
16814      template headers we've seen thus far.  */
16815   reset_specialization ();
16816
16817   /* If there were names looked up in the decl-specifier-seq that we
16818      did not check, check them now.  We must wait until we are in the
16819      scope of the function to perform the checks, since the function
16820      might be a friend.  */
16821   perform_deferred_access_checks ();
16822
16823   if (!success_p)
16824     {
16825       /* Skip the entire function.  */
16826       cp_parser_skip_to_end_of_block_or_statement (parser);
16827       fn = error_mark_node;
16828     }
16829   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16830     {
16831       /* Seen already, skip it.  An error message has already been output.  */
16832       cp_parser_skip_to_end_of_block_or_statement (parser);
16833       fn = current_function_decl;
16834       current_function_decl = NULL_TREE;
16835       /* If this is a function from a class, pop the nested class.  */
16836       if (current_class_name)
16837         pop_nested_class ();
16838     }
16839   else
16840     fn = cp_parser_function_definition_after_declarator (parser,
16841                                                          /*inline_p=*/false);
16842
16843   return fn;
16844 }
16845
16846 /* Parse the part of a function-definition that follows the
16847    declarator.  INLINE_P is TRUE iff this function is an inline
16848    function defined with a class-specifier.
16849
16850    Returns the function defined.  */
16851
16852 static tree
16853 cp_parser_function_definition_after_declarator (cp_parser* parser,
16854                                                 bool inline_p)
16855 {
16856   tree fn;
16857   bool ctor_initializer_p = false;
16858   bool saved_in_unbraced_linkage_specification_p;
16859   bool saved_in_function_body;
16860   unsigned saved_num_template_parameter_lists;
16861
16862   saved_in_function_body = parser->in_function_body;
16863   parser->in_function_body = true;
16864   /* If the next token is `return', then the code may be trying to
16865      make use of the "named return value" extension that G++ used to
16866      support.  */
16867   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16868     {
16869       /* Consume the `return' keyword.  */
16870       cp_lexer_consume_token (parser->lexer);
16871       /* Look for the identifier that indicates what value is to be
16872          returned.  */
16873       cp_parser_identifier (parser);
16874       /* Issue an error message.  */
16875       error ("named return values are no longer supported");
16876       /* Skip tokens until we reach the start of the function body.  */
16877       while (true)
16878         {
16879           cp_token *token = cp_lexer_peek_token (parser->lexer);
16880           if (token->type == CPP_OPEN_BRACE
16881               || token->type == CPP_EOF
16882               || token->type == CPP_PRAGMA_EOL)
16883             break;
16884           cp_lexer_consume_token (parser->lexer);
16885         }
16886     }
16887   /* The `extern' in `extern "C" void f () { ... }' does not apply to
16888      anything declared inside `f'.  */
16889   saved_in_unbraced_linkage_specification_p
16890     = parser->in_unbraced_linkage_specification_p;
16891   parser->in_unbraced_linkage_specification_p = false;
16892   /* Inside the function, surrounding template-parameter-lists do not
16893      apply.  */
16894   saved_num_template_parameter_lists
16895     = parser->num_template_parameter_lists;
16896   parser->num_template_parameter_lists = 0;
16897   /* If the next token is `try', then we are looking at a
16898      function-try-block.  */
16899   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16900     ctor_initializer_p = cp_parser_function_try_block (parser);
16901   /* A function-try-block includes the function-body, so we only do
16902      this next part if we're not processing a function-try-block.  */
16903   else
16904     ctor_initializer_p
16905       = cp_parser_ctor_initializer_opt_and_function_body (parser);
16906
16907   /* Finish the function.  */
16908   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16909                         (inline_p ? 2 : 0));
16910   /* Generate code for it, if necessary.  */
16911   expand_or_defer_fn (fn);
16912   /* Restore the saved values.  */
16913   parser->in_unbraced_linkage_specification_p
16914     = saved_in_unbraced_linkage_specification_p;
16915   parser->num_template_parameter_lists
16916     = saved_num_template_parameter_lists;
16917   parser->in_function_body = saved_in_function_body;
16918
16919   return fn;
16920 }
16921
16922 /* Parse a template-declaration, assuming that the `export' (and
16923    `extern') keywords, if present, has already been scanned.  MEMBER_P
16924    is as for cp_parser_template_declaration.  */
16925
16926 static void
16927 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16928 {
16929   tree decl = NULL_TREE;
16930   VEC (deferred_access_check,gc) *checks;
16931   tree parameter_list;
16932   bool friend_p = false;
16933   bool need_lang_pop;
16934
16935   /* Look for the `template' keyword.  */
16936   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
16937     return;
16938
16939   /* And the `<'.  */
16940   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
16941     return;
16942   if (at_class_scope_p () && current_function_decl)
16943     {
16944       /* 14.5.2.2 [temp.mem]
16945
16946          A local class shall not have member templates.  */
16947       error ("invalid declaration of member template in local class");
16948       cp_parser_skip_to_end_of_block_or_statement (parser);
16949       return;
16950     }
16951   /* [temp]
16952
16953      A template ... shall not have C linkage.  */
16954   if (current_lang_name == lang_name_c)
16955     {
16956       error ("template with C linkage");
16957       /* Give it C++ linkage to avoid confusing other parts of the
16958          front end.  */
16959       push_lang_context (lang_name_cplusplus);
16960       need_lang_pop = true;
16961     }
16962   else
16963     need_lang_pop = false;
16964
16965   /* We cannot perform access checks on the template parameter
16966      declarations until we know what is being declared, just as we
16967      cannot check the decl-specifier list.  */
16968   push_deferring_access_checks (dk_deferred);
16969
16970   /* If the next token is `>', then we have an invalid
16971      specialization.  Rather than complain about an invalid template
16972      parameter, issue an error message here.  */
16973   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16974     {
16975       cp_parser_error (parser, "invalid explicit specialization");
16976       begin_specialization ();
16977       parameter_list = NULL_TREE;
16978     }
16979   else
16980     /* Parse the template parameters.  */
16981     parameter_list = cp_parser_template_parameter_list (parser);
16982
16983   /* Get the deferred access checks from the parameter list.  These
16984      will be checked once we know what is being declared, as for a
16985      member template the checks must be performed in the scope of the
16986      class containing the member.  */
16987   checks = get_deferred_access_checks ();
16988
16989   /* Look for the `>'.  */
16990   cp_parser_skip_to_end_of_template_parameter_list (parser);
16991   /* We just processed one more parameter list.  */
16992   ++parser->num_template_parameter_lists;
16993   /* If the next token is `template', there are more template
16994      parameters.  */
16995   if (cp_lexer_next_token_is_keyword (parser->lexer,
16996                                       RID_TEMPLATE))
16997     cp_parser_template_declaration_after_export (parser, member_p);
16998   else
16999     {
17000       /* There are no access checks when parsing a template, as we do not
17001          know if a specialization will be a friend.  */
17002       push_deferring_access_checks (dk_no_check);
17003       decl = cp_parser_single_declaration (parser,
17004                                            checks,
17005                                            member_p,
17006                                            /*explicit_specialization_p=*/false,
17007                                            &friend_p);
17008       pop_deferring_access_checks ();
17009
17010       /* If this is a member template declaration, let the front
17011          end know.  */
17012       if (member_p && !friend_p && decl)
17013         {
17014           if (TREE_CODE (decl) == TYPE_DECL)
17015             cp_parser_check_access_in_redeclaration (decl);
17016
17017           decl = finish_member_template_decl (decl);
17018         }
17019       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17020         make_friend_class (current_class_type, TREE_TYPE (decl),
17021                            /*complain=*/true);
17022     }
17023   /* We are done with the current parameter list.  */
17024   --parser->num_template_parameter_lists;
17025
17026   pop_deferring_access_checks ();
17027
17028   /* Finish up.  */
17029   finish_template_decl (parameter_list);
17030
17031   /* Register member declarations.  */
17032   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17033     finish_member_declaration (decl);
17034   /* For the erroneous case of a template with C linkage, we pushed an
17035      implicit C++ linkage scope; exit that scope now.  */
17036   if (need_lang_pop)
17037     pop_lang_context ();
17038   /* If DECL is a function template, we must return to parse it later.
17039      (Even though there is no definition, there might be default
17040      arguments that need handling.)  */
17041   if (member_p && decl
17042       && (TREE_CODE (decl) == FUNCTION_DECL
17043           || DECL_FUNCTION_TEMPLATE_P (decl)))
17044     TREE_VALUE (parser->unparsed_functions_queues)
17045       = tree_cons (NULL_TREE, decl,
17046                    TREE_VALUE (parser->unparsed_functions_queues));
17047 }
17048
17049 /* Perform the deferred access checks from a template-parameter-list.
17050    CHECKS is a TREE_LIST of access checks, as returned by
17051    get_deferred_access_checks.  */
17052
17053 static void
17054 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17055 {
17056   ++processing_template_parmlist;
17057   perform_access_checks (checks);
17058   --processing_template_parmlist;
17059 }
17060
17061 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17062    `function-definition' sequence.  MEMBER_P is true, this declaration
17063    appears in a class scope.
17064
17065    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17066    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17067
17068 static tree
17069 cp_parser_single_declaration (cp_parser* parser,
17070                               VEC (deferred_access_check,gc)* checks,
17071                               bool member_p,
17072                               bool explicit_specialization_p,
17073                               bool* friend_p)
17074 {
17075   int declares_class_or_enum;
17076   tree decl = NULL_TREE;
17077   cp_decl_specifier_seq decl_specifiers;
17078   bool function_definition_p = false;
17079
17080   /* This function is only used when processing a template
17081      declaration.  */
17082   gcc_assert (innermost_scope_kind () == sk_template_parms
17083               || innermost_scope_kind () == sk_template_spec);
17084
17085   /* Defer access checks until we know what is being declared.  */
17086   push_deferring_access_checks (dk_deferred);
17087
17088   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17089      alternative.  */
17090   cp_parser_decl_specifier_seq (parser,
17091                                 CP_PARSER_FLAGS_OPTIONAL,
17092                                 &decl_specifiers,
17093                                 &declares_class_or_enum);
17094   if (friend_p)
17095     *friend_p = cp_parser_friend_p (&decl_specifiers);
17096
17097   /* There are no template typedefs.  */
17098   if (decl_specifiers.specs[(int) ds_typedef])
17099     {
17100       error ("template declaration of %qs", "typedef");
17101       decl = error_mark_node;
17102     }
17103
17104   /* Gather up the access checks that occurred the
17105      decl-specifier-seq.  */
17106   stop_deferring_access_checks ();
17107
17108   /* Check for the declaration of a template class.  */
17109   if (declares_class_or_enum)
17110     {
17111       if (cp_parser_declares_only_class_p (parser))
17112         {
17113           decl = shadow_tag (&decl_specifiers);
17114
17115           /* In this case:
17116
17117                struct C {
17118                  friend template <typename T> struct A<T>::B;
17119                };
17120
17121              A<T>::B will be represented by a TYPENAME_TYPE, and
17122              therefore not recognized by shadow_tag.  */
17123           if (friend_p && *friend_p
17124               && !decl
17125               && decl_specifiers.type
17126               && TYPE_P (decl_specifiers.type))
17127             decl = decl_specifiers.type;
17128
17129           if (decl && decl != error_mark_node)
17130             decl = TYPE_NAME (decl);
17131           else
17132             decl = error_mark_node;
17133
17134           /* Perform access checks for template parameters.  */
17135           cp_parser_perform_template_parameter_access_checks (checks);
17136         }
17137     }
17138   /* If it's not a template class, try for a template function.  If
17139      the next token is a `;', then this declaration does not declare
17140      anything.  But, if there were errors in the decl-specifiers, then
17141      the error might well have come from an attempted class-specifier.
17142      In that case, there's no need to warn about a missing declarator.  */
17143   if (!decl
17144       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17145           || decl_specifiers.type != error_mark_node))
17146     {
17147       decl = cp_parser_init_declarator (parser,
17148                                         &decl_specifiers,
17149                                         checks,
17150                                         /*function_definition_allowed_p=*/true,
17151                                         member_p,
17152                                         declares_class_or_enum,
17153                                         &function_definition_p);
17154
17155     /* 7.1.1-1 [dcl.stc]
17156
17157        A storage-class-specifier shall not be specified in an explicit
17158        specialization...  */
17159     if (decl
17160         && explicit_specialization_p
17161         && decl_specifiers.storage_class != sc_none)
17162       {
17163         error ("explicit template specialization cannot have a storage class");
17164         decl = error_mark_node;
17165       }
17166     }
17167
17168   pop_deferring_access_checks ();
17169
17170   /* Clear any current qualification; whatever comes next is the start
17171      of something new.  */
17172   parser->scope = NULL_TREE;
17173   parser->qualifying_scope = NULL_TREE;
17174   parser->object_scope = NULL_TREE;
17175   /* Look for a trailing `;' after the declaration.  */
17176   if (!function_definition_p
17177       && (decl == error_mark_node
17178           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17179     cp_parser_skip_to_end_of_block_or_statement (parser);
17180
17181   return decl;
17182 }
17183
17184 /* Parse a cast-expression that is not the operand of a unary "&".  */
17185
17186 static tree
17187 cp_parser_simple_cast_expression (cp_parser *parser)
17188 {
17189   return cp_parser_cast_expression (parser, /*address_p=*/false,
17190                                     /*cast_p=*/false);
17191 }
17192
17193 /* Parse a functional cast to TYPE.  Returns an expression
17194    representing the cast.  */
17195
17196 static tree
17197 cp_parser_functional_cast (cp_parser* parser, tree type)
17198 {
17199   tree expression_list;
17200   tree cast;
17201
17202   expression_list
17203     = cp_parser_parenthesized_expression_list (parser, false,
17204                                                /*cast_p=*/true,
17205                                                /*allow_expansion_p=*/true,
17206                                                /*non_constant_p=*/NULL);
17207
17208   cast = build_functional_cast (type, expression_list,
17209                                 tf_warning_or_error);
17210   /* [expr.const]/1: In an integral constant expression "only type
17211      conversions to integral or enumeration type can be used".  */
17212   if (TREE_CODE (type) == TYPE_DECL)
17213     type = TREE_TYPE (type);
17214   if (cast != error_mark_node
17215       && !cast_valid_in_integral_constant_expression_p (type)
17216       && (cp_parser_non_integral_constant_expression
17217           (parser, "a call to a constructor")))
17218     return error_mark_node;
17219   return cast;
17220 }
17221
17222 /* Save the tokens that make up the body of a member function defined
17223    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17224    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17225    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17226    for the member function.  */
17227
17228 static tree
17229 cp_parser_save_member_function_body (cp_parser* parser,
17230                                      cp_decl_specifier_seq *decl_specifiers,
17231                                      cp_declarator *declarator,
17232                                      tree attributes)
17233 {
17234   cp_token *first;
17235   cp_token *last;
17236   tree fn;
17237
17238   /* Create the function-declaration.  */
17239   fn = start_method (decl_specifiers, declarator, attributes);
17240   /* If something went badly wrong, bail out now.  */
17241   if (fn == error_mark_node)
17242     {
17243       /* If there's a function-body, skip it.  */
17244       if (cp_parser_token_starts_function_definition_p
17245           (cp_lexer_peek_token (parser->lexer)))
17246         cp_parser_skip_to_end_of_block_or_statement (parser);
17247       return error_mark_node;
17248     }
17249
17250   /* Remember it, if there default args to post process.  */
17251   cp_parser_save_default_args (parser, fn);
17252
17253   /* Save away the tokens that make up the body of the
17254      function.  */
17255   first = parser->lexer->next_token;
17256   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17257   /* Handle function try blocks.  */
17258   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17259     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17260   last = parser->lexer->next_token;
17261
17262   /* Save away the inline definition; we will process it when the
17263      class is complete.  */
17264   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17265   DECL_PENDING_INLINE_P (fn) = 1;
17266
17267   /* We need to know that this was defined in the class, so that
17268      friend templates are handled correctly.  */
17269   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17270
17271   /* We're done with the inline definition.  */
17272   finish_method (fn);
17273
17274   /* Add FN to the queue of functions to be parsed later.  */
17275   TREE_VALUE (parser->unparsed_functions_queues)
17276     = tree_cons (NULL_TREE, fn,
17277                  TREE_VALUE (parser->unparsed_functions_queues));
17278
17279   return fn;
17280 }
17281
17282 /* Parse a template-argument-list, as well as the trailing ">" (but
17283    not the opening ">").  See cp_parser_template_argument_list for the
17284    return value.  */
17285
17286 static tree
17287 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17288 {
17289   tree arguments;
17290   tree saved_scope;
17291   tree saved_qualifying_scope;
17292   tree saved_object_scope;
17293   bool saved_greater_than_is_operator_p;
17294   bool saved_skip_evaluation;
17295
17296   /* [temp.names]
17297
17298      When parsing a template-id, the first non-nested `>' is taken as
17299      the end of the template-argument-list rather than a greater-than
17300      operator.  */
17301   saved_greater_than_is_operator_p
17302     = parser->greater_than_is_operator_p;
17303   parser->greater_than_is_operator_p = false;
17304   /* Parsing the argument list may modify SCOPE, so we save it
17305      here.  */
17306   saved_scope = parser->scope;
17307   saved_qualifying_scope = parser->qualifying_scope;
17308   saved_object_scope = parser->object_scope;
17309   /* We need to evaluate the template arguments, even though this
17310      template-id may be nested within a "sizeof".  */
17311   saved_skip_evaluation = skip_evaluation;
17312   skip_evaluation = false;
17313   /* Parse the template-argument-list itself.  */
17314   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17315       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17316     arguments = NULL_TREE;
17317   else
17318     arguments = cp_parser_template_argument_list (parser);
17319   /* Look for the `>' that ends the template-argument-list. If we find
17320      a '>>' instead, it's probably just a typo.  */
17321   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17322     {
17323       if (cxx_dialect != cxx98)
17324         {
17325           /* In C++0x, a `>>' in a template argument list or cast
17326              expression is considered to be two separate `>'
17327              tokens. So, change the current token to a `>', but don't
17328              consume it: it will be consumed later when the outer
17329              template argument list (or cast expression) is parsed.
17330              Note that this replacement of `>' for `>>' is necessary
17331              even if we are parsing tentatively: in the tentative
17332              case, after calling
17333              cp_parser_enclosed_template_argument_list we will always
17334              throw away all of the template arguments and the first
17335              closing `>', either because the template argument list
17336              was erroneous or because we are replacing those tokens
17337              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17338              not have been thrown away) is needed either to close an
17339              outer template argument list or to complete a new-style
17340              cast.  */
17341           cp_token *token = cp_lexer_peek_token (parser->lexer);
17342           token->type = CPP_GREATER;
17343         }
17344       else if (!saved_greater_than_is_operator_p)
17345         {
17346           /* If we're in a nested template argument list, the '>>' has
17347             to be a typo for '> >'. We emit the error message, but we
17348             continue parsing and we push a '>' as next token, so that
17349             the argument list will be parsed correctly.  Note that the
17350             global source location is still on the token before the
17351             '>>', so we need to say explicitly where we want it.  */
17352           cp_token *token = cp_lexer_peek_token (parser->lexer);
17353           error ("%H%<>>%> should be %<> >%> "
17354                  "within a nested template argument list",
17355                  &token->location);
17356
17357           token->type = CPP_GREATER;
17358         }
17359       else
17360         {
17361           /* If this is not a nested template argument list, the '>>'
17362             is a typo for '>'. Emit an error message and continue.
17363             Same deal about the token location, but here we can get it
17364             right by consuming the '>>' before issuing the diagnostic.  */
17365           cp_lexer_consume_token (parser->lexer);
17366           error ("spurious %<>>%>, use %<>%> to terminate "
17367                  "a template argument list");
17368         }
17369     }
17370   else
17371     cp_parser_skip_to_end_of_template_parameter_list (parser);
17372   /* The `>' token might be a greater-than operator again now.  */
17373   parser->greater_than_is_operator_p
17374     = saved_greater_than_is_operator_p;
17375   /* Restore the SAVED_SCOPE.  */
17376   parser->scope = saved_scope;
17377   parser->qualifying_scope = saved_qualifying_scope;
17378   parser->object_scope = saved_object_scope;
17379   skip_evaluation = saved_skip_evaluation;
17380
17381   return arguments;
17382 }
17383
17384 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17385    arguments, or the body of the function have not yet been parsed,
17386    parse them now.  */
17387
17388 static void
17389 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17390 {
17391   /* If this member is a template, get the underlying
17392      FUNCTION_DECL.  */
17393   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17394     member_function = DECL_TEMPLATE_RESULT (member_function);
17395
17396   /* There should not be any class definitions in progress at this
17397      point; the bodies of members are only parsed outside of all class
17398      definitions.  */
17399   gcc_assert (parser->num_classes_being_defined == 0);
17400   /* While we're parsing the member functions we might encounter more
17401      classes.  We want to handle them right away, but we don't want
17402      them getting mixed up with functions that are currently in the
17403      queue.  */
17404   parser->unparsed_functions_queues
17405     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17406
17407   /* Make sure that any template parameters are in scope.  */
17408   maybe_begin_member_template_processing (member_function);
17409
17410   /* If the body of the function has not yet been parsed, parse it
17411      now.  */
17412   if (DECL_PENDING_INLINE_P (member_function))
17413     {
17414       tree function_scope;
17415       cp_token_cache *tokens;
17416
17417       /* The function is no longer pending; we are processing it.  */
17418       tokens = DECL_PENDING_INLINE_INFO (member_function);
17419       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17420       DECL_PENDING_INLINE_P (member_function) = 0;
17421
17422       /* If this is a local class, enter the scope of the containing
17423          function.  */
17424       function_scope = current_function_decl;
17425       if (function_scope)
17426         push_function_context ();
17427
17428       /* Push the body of the function onto the lexer stack.  */
17429       cp_parser_push_lexer_for_tokens (parser, tokens);
17430
17431       /* Let the front end know that we going to be defining this
17432          function.  */
17433       start_preparsed_function (member_function, NULL_TREE,
17434                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17435
17436       /* Don't do access checking if it is a templated function.  */
17437       if (processing_template_decl)
17438         push_deferring_access_checks (dk_no_check);
17439
17440       /* Now, parse the body of the function.  */
17441       cp_parser_function_definition_after_declarator (parser,
17442                                                       /*inline_p=*/true);
17443
17444       if (processing_template_decl)
17445         pop_deferring_access_checks ();
17446
17447       /* Leave the scope of the containing function.  */
17448       if (function_scope)
17449         pop_function_context ();
17450       cp_parser_pop_lexer (parser);
17451     }
17452
17453   /* Remove any template parameters from the symbol table.  */
17454   maybe_end_member_template_processing ();
17455
17456   /* Restore the queue.  */
17457   parser->unparsed_functions_queues
17458     = TREE_CHAIN (parser->unparsed_functions_queues);
17459 }
17460
17461 /* If DECL contains any default args, remember it on the unparsed
17462    functions queue.  */
17463
17464 static void
17465 cp_parser_save_default_args (cp_parser* parser, tree decl)
17466 {
17467   tree probe;
17468
17469   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17470        probe;
17471        probe = TREE_CHAIN (probe))
17472     if (TREE_PURPOSE (probe))
17473       {
17474         TREE_PURPOSE (parser->unparsed_functions_queues)
17475           = tree_cons (current_class_type, decl,
17476                        TREE_PURPOSE (parser->unparsed_functions_queues));
17477         break;
17478       }
17479 }
17480
17481 /* FN is a FUNCTION_DECL which may contains a parameter with an
17482    unparsed DEFAULT_ARG.  Parse the default args now.  This function
17483    assumes that the current scope is the scope in which the default
17484    argument should be processed.  */
17485
17486 static void
17487 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17488 {
17489   bool saved_local_variables_forbidden_p;
17490   tree parm;
17491
17492   /* While we're parsing the default args, we might (due to the
17493      statement expression extension) encounter more classes.  We want
17494      to handle them right away, but we don't want them getting mixed
17495      up with default args that are currently in the queue.  */
17496   parser->unparsed_functions_queues
17497     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17498
17499   /* Local variable names (and the `this' keyword) may not appear
17500      in a default argument.  */
17501   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17502   parser->local_variables_forbidden_p = true;
17503
17504   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17505        parm;
17506        parm = TREE_CHAIN (parm))
17507     {
17508       cp_token_cache *tokens;
17509       tree default_arg = TREE_PURPOSE (parm);
17510       tree parsed_arg;
17511       VEC(tree,gc) *insts;
17512       tree copy;
17513       unsigned ix;
17514
17515       if (!default_arg)
17516         continue;
17517
17518       if (TREE_CODE (default_arg) != DEFAULT_ARG)
17519         /* This can happen for a friend declaration for a function
17520            already declared with default arguments.  */
17521         continue;
17522
17523        /* Push the saved tokens for the default argument onto the parser's
17524           lexer stack.  */
17525       tokens = DEFARG_TOKENS (default_arg);
17526       cp_parser_push_lexer_for_tokens (parser, tokens);
17527
17528       /* Parse the assignment-expression.  */
17529       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17530
17531       if (!processing_template_decl)
17532         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17533
17534       TREE_PURPOSE (parm) = parsed_arg;
17535
17536       /* Update any instantiations we've already created.  */
17537       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17538            VEC_iterate (tree, insts, ix, copy); ix++)
17539         TREE_PURPOSE (copy) = parsed_arg;
17540
17541       /* If the token stream has not been completely used up, then
17542          there was extra junk after the end of the default
17543          argument.  */
17544       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17545         cp_parser_error (parser, "expected %<,%>");
17546
17547       /* Revert to the main lexer.  */
17548       cp_parser_pop_lexer (parser);
17549     }
17550
17551   /* Make sure no default arg is missing.  */
17552   check_default_args (fn);
17553
17554   /* Restore the state of local_variables_forbidden_p.  */
17555   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17556
17557   /* Restore the queue.  */
17558   parser->unparsed_functions_queues
17559     = TREE_CHAIN (parser->unparsed_functions_queues);
17560 }
17561
17562 /* Parse the operand of `sizeof' (or a similar operator).  Returns
17563    either a TYPE or an expression, depending on the form of the
17564    input.  The KEYWORD indicates which kind of expression we have
17565    encountered.  */
17566
17567 static tree
17568 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17569 {
17570   static const char *format;
17571   tree expr = NULL_TREE;
17572   const char *saved_message;
17573   char *tmp;
17574   bool saved_integral_constant_expression_p;
17575   bool saved_non_integral_constant_expression_p;
17576   bool pack_expansion_p = false;
17577
17578   /* Initialize FORMAT the first time we get here.  */
17579   if (!format)
17580     format = "types may not be defined in '%s' expressions";
17581
17582   /* Types cannot be defined in a `sizeof' expression.  Save away the
17583      old message.  */
17584   saved_message = parser->type_definition_forbidden_message;
17585   /* And create the new one.  */
17586   parser->type_definition_forbidden_message = tmp
17587     = XNEWVEC (char, strlen (format)
17588                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17589                + 1 /* `\0' */);
17590   sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17591
17592   /* The restrictions on constant-expressions do not apply inside
17593      sizeof expressions.  */
17594   saved_integral_constant_expression_p
17595     = parser->integral_constant_expression_p;
17596   saved_non_integral_constant_expression_p
17597     = parser->non_integral_constant_expression_p;
17598   parser->integral_constant_expression_p = false;
17599
17600   /* If it's a `...', then we are computing the length of a parameter
17601      pack.  */
17602   if (keyword == RID_SIZEOF
17603       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17604     {
17605       /* Consume the `...'.  */
17606       cp_lexer_consume_token (parser->lexer);
17607       maybe_warn_variadic_templates ();
17608
17609       /* Note that this is an expansion.  */
17610       pack_expansion_p = true;
17611     }
17612
17613   /* Do not actually evaluate the expression.  */
17614   ++skip_evaluation;
17615   /* If it's a `(', then we might be looking at the type-id
17616      construction.  */
17617   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17618     {
17619       tree type;
17620       bool saved_in_type_id_in_expr_p;
17621
17622       /* We can't be sure yet whether we're looking at a type-id or an
17623          expression.  */
17624       cp_parser_parse_tentatively (parser);
17625       /* Consume the `('.  */
17626       cp_lexer_consume_token (parser->lexer);
17627       /* Parse the type-id.  */
17628       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17629       parser->in_type_id_in_expr_p = true;
17630       type = cp_parser_type_id (parser);
17631       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17632       /* Now, look for the trailing `)'.  */
17633       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17634       /* If all went well, then we're done.  */
17635       if (cp_parser_parse_definitely (parser))
17636         {
17637           cp_decl_specifier_seq decl_specs;
17638
17639           /* Build a trivial decl-specifier-seq.  */
17640           clear_decl_specs (&decl_specs);
17641           decl_specs.type = type;
17642
17643           /* Call grokdeclarator to figure out what type this is.  */
17644           expr = grokdeclarator (NULL,
17645                                  &decl_specs,
17646                                  TYPENAME,
17647                                  /*initialized=*/0,
17648                                  /*attrlist=*/NULL);
17649         }
17650     }
17651
17652   /* If the type-id production did not work out, then we must be
17653      looking at the unary-expression production.  */
17654   if (!expr)
17655     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17656                                        /*cast_p=*/false);
17657
17658   if (pack_expansion_p)
17659     /* Build a pack expansion. */
17660     expr = make_pack_expansion (expr);
17661
17662   /* Go back to evaluating expressions.  */
17663   --skip_evaluation;
17664
17665   /* Free the message we created.  */
17666   free (tmp);
17667   /* And restore the old one.  */
17668   parser->type_definition_forbidden_message = saved_message;
17669   parser->integral_constant_expression_p
17670     = saved_integral_constant_expression_p;
17671   parser->non_integral_constant_expression_p
17672     = saved_non_integral_constant_expression_p;
17673
17674   return expr;
17675 }
17676
17677 /* If the current declaration has no declarator, return true.  */
17678
17679 static bool
17680 cp_parser_declares_only_class_p (cp_parser *parser)
17681 {
17682   /* If the next token is a `;' or a `,' then there is no
17683      declarator.  */
17684   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17685           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17686 }
17687
17688 /* Update the DECL_SPECS to reflect the storage class indicated by
17689    KEYWORD.  */
17690
17691 static void
17692 cp_parser_set_storage_class (cp_parser *parser,
17693                              cp_decl_specifier_seq *decl_specs,
17694                              enum rid keyword)
17695 {
17696   cp_storage_class storage_class;
17697
17698   if (parser->in_unbraced_linkage_specification_p)
17699     {
17700       error ("invalid use of %qD in linkage specification",
17701              ridpointers[keyword]);
17702       return;
17703     }
17704   else if (decl_specs->storage_class != sc_none)
17705     {
17706       decl_specs->conflicting_specifiers_p = true;
17707       return;
17708     }
17709
17710   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17711       && decl_specs->specs[(int) ds_thread])
17712     {
17713       error ("%<__thread%> before %qD", ridpointers[keyword]);
17714       decl_specs->specs[(int) ds_thread] = 0;
17715     }
17716
17717   switch (keyword)
17718     {
17719     case RID_AUTO:
17720       storage_class = sc_auto;
17721       break;
17722     case RID_REGISTER:
17723       storage_class = sc_register;
17724       break;
17725     case RID_STATIC:
17726       storage_class = sc_static;
17727       break;
17728     case RID_EXTERN:
17729       storage_class = sc_extern;
17730       break;
17731     case RID_MUTABLE:
17732       storage_class = sc_mutable;
17733       break;
17734     default:
17735       gcc_unreachable ();
17736     }
17737   decl_specs->storage_class = storage_class;
17738
17739   /* A storage class specifier cannot be applied alongside a typedef 
17740      specifier. If there is a typedef specifier present then set 
17741      conflicting_specifiers_p which will trigger an error later
17742      on in grokdeclarator. */
17743   if (decl_specs->specs[(int)ds_typedef])
17744     decl_specs->conflicting_specifiers_p = true;
17745 }
17746
17747 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
17748    is true, the type is a user-defined type; otherwise it is a
17749    built-in type specified by a keyword.  */
17750
17751 static void
17752 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17753                               tree type_spec,
17754                               bool user_defined_p)
17755 {
17756   decl_specs->any_specifiers_p = true;
17757
17758   /* If the user tries to redeclare bool or wchar_t (with, for
17759      example, in "typedef int wchar_t;") we remember that this is what
17760      happened.  In system headers, we ignore these declarations so
17761      that G++ can work with system headers that are not C++-safe.  */
17762   if (decl_specs->specs[(int) ds_typedef]
17763       && !user_defined_p
17764       && (type_spec == boolean_type_node
17765           || type_spec == wchar_type_node)
17766       && (decl_specs->type
17767           || decl_specs->specs[(int) ds_long]
17768           || decl_specs->specs[(int) ds_short]
17769           || decl_specs->specs[(int) ds_unsigned]
17770           || decl_specs->specs[(int) ds_signed]))
17771     {
17772       decl_specs->redefined_builtin_type = type_spec;
17773       if (!decl_specs->type)
17774         {
17775           decl_specs->type = type_spec;
17776           decl_specs->user_defined_type_p = false;
17777         }
17778     }
17779   else if (decl_specs->type)
17780     decl_specs->multiple_types_p = true;
17781   else
17782     {
17783       decl_specs->type = type_spec;
17784       decl_specs->user_defined_type_p = user_defined_p;
17785       decl_specs->redefined_builtin_type = NULL_TREE;
17786     }
17787 }
17788
17789 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17790    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
17791
17792 static bool
17793 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17794 {
17795   return decl_specifiers->specs[(int) ds_friend] != 0;
17796 }
17797
17798 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
17799    issue an error message indicating that TOKEN_DESC was expected.
17800
17801    Returns the token consumed, if the token had the appropriate type.
17802    Otherwise, returns NULL.  */
17803
17804 static cp_token *
17805 cp_parser_require (cp_parser* parser,
17806                    enum cpp_ttype type,
17807                    const char* token_desc)
17808 {
17809   if (cp_lexer_next_token_is (parser->lexer, type))
17810     return cp_lexer_consume_token (parser->lexer);
17811   else
17812     {
17813       /* Output the MESSAGE -- unless we're parsing tentatively.  */
17814       if (!cp_parser_simulate_error (parser))
17815         {
17816           char *message = concat ("expected ", token_desc, NULL);
17817           cp_parser_error (parser, message);
17818           free (message);
17819         }
17820       return NULL;
17821     }
17822 }
17823
17824 /* An error message is produced if the next token is not '>'.
17825    All further tokens are skipped until the desired token is
17826    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
17827
17828 static void
17829 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17830 {
17831   /* Current level of '< ... >'.  */
17832   unsigned level = 0;
17833   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
17834   unsigned nesting_depth = 0;
17835
17836   /* Are we ready, yet?  If not, issue error message.  */
17837   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17838     return;
17839
17840   /* Skip tokens until the desired token is found.  */
17841   while (true)
17842     {
17843       /* Peek at the next token.  */
17844       switch (cp_lexer_peek_token (parser->lexer)->type)
17845         {
17846         case CPP_LESS:
17847           if (!nesting_depth)
17848             ++level;
17849           break;
17850
17851         case CPP_RSHIFT:
17852           if (cxx_dialect == cxx98)
17853             /* C++0x views the `>>' operator as two `>' tokens, but
17854                C++98 does not. */
17855             break;
17856           else if (!nesting_depth && level-- == 0)
17857             {
17858               /* We've hit a `>>' where the first `>' closes the
17859                  template argument list, and the second `>' is
17860                  spurious.  Just consume the `>>' and stop; we've
17861                  already produced at least one error.  */
17862               cp_lexer_consume_token (parser->lexer);
17863               return;
17864             }
17865           /* Fall through for C++0x, so we handle the second `>' in
17866              the `>>'.  */
17867
17868         case CPP_GREATER:
17869           if (!nesting_depth && level-- == 0)
17870             {
17871               /* We've reached the token we want, consume it and stop.  */
17872               cp_lexer_consume_token (parser->lexer);
17873               return;
17874             }
17875           break;
17876
17877         case CPP_OPEN_PAREN:
17878         case CPP_OPEN_SQUARE:
17879           ++nesting_depth;
17880           break;
17881
17882         case CPP_CLOSE_PAREN:
17883         case CPP_CLOSE_SQUARE:
17884           if (nesting_depth-- == 0)
17885             return;
17886           break;
17887
17888         case CPP_EOF:
17889         case CPP_PRAGMA_EOL:
17890         case CPP_SEMICOLON:
17891         case CPP_OPEN_BRACE:
17892         case CPP_CLOSE_BRACE:
17893           /* The '>' was probably forgotten, don't look further.  */
17894           return;
17895
17896         default:
17897           break;
17898         }
17899
17900       /* Consume this token.  */
17901       cp_lexer_consume_token (parser->lexer);
17902     }
17903 }
17904
17905 /* If the next token is the indicated keyword, consume it.  Otherwise,
17906    issue an error message indicating that TOKEN_DESC was expected.
17907
17908    Returns the token consumed, if the token had the appropriate type.
17909    Otherwise, returns NULL.  */
17910
17911 static cp_token *
17912 cp_parser_require_keyword (cp_parser* parser,
17913                            enum rid keyword,
17914                            const char* token_desc)
17915 {
17916   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17917
17918   if (token && token->keyword != keyword)
17919     {
17920       dyn_string_t error_msg;
17921
17922       /* Format the error message.  */
17923       error_msg = dyn_string_new (0);
17924       dyn_string_append_cstr (error_msg, "expected ");
17925       dyn_string_append_cstr (error_msg, token_desc);
17926       cp_parser_error (parser, error_msg->s);
17927       dyn_string_delete (error_msg);
17928       return NULL;
17929     }
17930
17931   return token;
17932 }
17933
17934 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17935    function-definition.  */
17936
17937 static bool
17938 cp_parser_token_starts_function_definition_p (cp_token* token)
17939 {
17940   return (/* An ordinary function-body begins with an `{'.  */
17941           token->type == CPP_OPEN_BRACE
17942           /* A ctor-initializer begins with a `:'.  */
17943           || token->type == CPP_COLON
17944           /* A function-try-block begins with `try'.  */
17945           || token->keyword == RID_TRY
17946           /* The named return value extension begins with `return'.  */
17947           || token->keyword == RID_RETURN);
17948 }
17949
17950 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17951    definition.  */
17952
17953 static bool
17954 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17955 {
17956   cp_token *token;
17957
17958   token = cp_lexer_peek_token (parser->lexer);
17959   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17960 }
17961
17962 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17963    C++0x) ending a template-argument.  */
17964
17965 static bool
17966 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17967 {
17968   cp_token *token;
17969
17970   token = cp_lexer_peek_token (parser->lexer);
17971   return (token->type == CPP_COMMA 
17972           || token->type == CPP_GREATER
17973           || token->type == CPP_ELLIPSIS
17974           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17975 }
17976
17977 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17978    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
17979
17980 static bool
17981 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17982                                                      size_t n)
17983 {
17984   cp_token *token;
17985
17986   token = cp_lexer_peek_nth_token (parser->lexer, n);
17987   if (token->type == CPP_LESS)
17988     return true;
17989   /* Check for the sequence `<::' in the original code. It would be lexed as
17990      `[:', where `[' is a digraph, and there is no whitespace before
17991      `:'.  */
17992   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17993     {
17994       cp_token *token2;
17995       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17996       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17997         return true;
17998     }
17999   return false;
18000 }
18001
18002 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18003    or none_type otherwise.  */
18004
18005 static enum tag_types
18006 cp_parser_token_is_class_key (cp_token* token)
18007 {
18008   switch (token->keyword)
18009     {
18010     case RID_CLASS:
18011       return class_type;
18012     case RID_STRUCT:
18013       return record_type;
18014     case RID_UNION:
18015       return union_type;
18016
18017     default:
18018       return none_type;
18019     }
18020 }
18021
18022 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18023
18024 static void
18025 cp_parser_check_class_key (enum tag_types class_key, tree type)
18026 {
18027   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18028     pedwarn ("%qs tag used in naming %q#T",
18029             class_key == union_type ? "union"
18030              : class_key == record_type ? "struct" : "class",
18031              type);
18032 }
18033
18034 /* Issue an error message if DECL is redeclared with different
18035    access than its original declaration [class.access.spec/3].
18036    This applies to nested classes and nested class templates.
18037    [class.mem/1].  */
18038
18039 static void
18040 cp_parser_check_access_in_redeclaration (tree decl)
18041 {
18042   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18043     return;
18044
18045   if ((TREE_PRIVATE (decl)
18046        != (current_access_specifier == access_private_node))
18047       || (TREE_PROTECTED (decl)
18048           != (current_access_specifier == access_protected_node)))
18049     error ("%qD redeclared with different access", decl);
18050 }
18051
18052 /* Look for the `template' keyword, as a syntactic disambiguator.
18053    Return TRUE iff it is present, in which case it will be
18054    consumed.  */
18055
18056 static bool
18057 cp_parser_optional_template_keyword (cp_parser *parser)
18058 {
18059   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18060     {
18061       /* The `template' keyword can only be used within templates;
18062          outside templates the parser can always figure out what is a
18063          template and what is not.  */
18064       if (!processing_template_decl)
18065         {
18066           error ("%<template%> (as a disambiguator) is only allowed "
18067                  "within templates");
18068           /* If this part of the token stream is rescanned, the same
18069              error message would be generated.  So, we purge the token
18070              from the stream.  */
18071           cp_lexer_purge_token (parser->lexer);
18072           return false;
18073         }
18074       else
18075         {
18076           /* Consume the `template' keyword.  */
18077           cp_lexer_consume_token (parser->lexer);
18078           return true;
18079         }
18080     }
18081
18082   return false;
18083 }
18084
18085 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18086    set PARSER->SCOPE, and perform other related actions.  */
18087
18088 static void
18089 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18090 {
18091   int i;
18092   struct tree_check *check_value;
18093   deferred_access_check *chk;
18094   VEC (deferred_access_check,gc) *checks;
18095
18096   /* Get the stored value.  */
18097   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18098   /* Perform any access checks that were deferred.  */
18099   checks = check_value->checks;
18100   if (checks)
18101     {
18102       for (i = 0 ;
18103            VEC_iterate (deferred_access_check, checks, i, chk) ;
18104            ++i)
18105         {
18106           perform_or_defer_access_check (chk->binfo,
18107                                          chk->decl,
18108                                          chk->diag_decl);
18109         }
18110     }
18111   /* Set the scope from the stored value.  */
18112   parser->scope = check_value->value;
18113   parser->qualifying_scope = check_value->qualifying_scope;
18114   parser->object_scope = NULL_TREE;
18115 }
18116
18117 /* Consume tokens up through a non-nested END token.  */
18118
18119 static void
18120 cp_parser_cache_group (cp_parser *parser,
18121                        enum cpp_ttype end,
18122                        unsigned depth)
18123 {
18124   while (true)
18125     {
18126       cp_token *token;
18127
18128       /* Abort a parenthesized expression if we encounter a brace.  */
18129       if ((end == CPP_CLOSE_PAREN || depth == 0)
18130           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18131         return;
18132       /* If we've reached the end of the file, stop.  */
18133       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
18134           || (end != CPP_PRAGMA_EOL
18135               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
18136         return;
18137       /* Consume the next token.  */
18138       token = cp_lexer_consume_token (parser->lexer);
18139       /* See if it starts a new group.  */
18140       if (token->type == CPP_OPEN_BRACE)
18141         {
18142           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18143           if (depth == 0)
18144             return;
18145         }
18146       else if (token->type == CPP_OPEN_PAREN)
18147         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18148       else if (token->type == CPP_PRAGMA)
18149         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18150       else if (token->type == end)
18151         return;
18152     }
18153 }
18154
18155 /* Begin parsing tentatively.  We always save tokens while parsing
18156    tentatively so that if the tentative parsing fails we can restore the
18157    tokens.  */
18158
18159 static void
18160 cp_parser_parse_tentatively (cp_parser* parser)
18161 {
18162   /* Enter a new parsing context.  */
18163   parser->context = cp_parser_context_new (parser->context);
18164   /* Begin saving tokens.  */
18165   cp_lexer_save_tokens (parser->lexer);
18166   /* In order to avoid repetitive access control error messages,
18167      access checks are queued up until we are no longer parsing
18168      tentatively.  */
18169   push_deferring_access_checks (dk_deferred);
18170 }
18171
18172 /* Commit to the currently active tentative parse.  */
18173
18174 static void
18175 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18176 {
18177   cp_parser_context *context;
18178   cp_lexer *lexer;
18179
18180   /* Mark all of the levels as committed.  */
18181   lexer = parser->lexer;
18182   for (context = parser->context; context->next; context = context->next)
18183     {
18184       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18185         break;
18186       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18187       while (!cp_lexer_saving_tokens (lexer))
18188         lexer = lexer->next;
18189       cp_lexer_commit_tokens (lexer);
18190     }
18191 }
18192
18193 /* Abort the currently active tentative parse.  All consumed tokens
18194    will be rolled back, and no diagnostics will be issued.  */
18195
18196 static void
18197 cp_parser_abort_tentative_parse (cp_parser* parser)
18198 {
18199   cp_parser_simulate_error (parser);
18200   /* Now, pretend that we want to see if the construct was
18201      successfully parsed.  */
18202   cp_parser_parse_definitely (parser);
18203 }
18204
18205 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18206    token stream.  Otherwise, commit to the tokens we have consumed.
18207    Returns true if no error occurred; false otherwise.  */
18208
18209 static bool
18210 cp_parser_parse_definitely (cp_parser* parser)
18211 {
18212   bool error_occurred;
18213   cp_parser_context *context;
18214
18215   /* Remember whether or not an error occurred, since we are about to
18216      destroy that information.  */
18217   error_occurred = cp_parser_error_occurred (parser);
18218   /* Remove the topmost context from the stack.  */
18219   context = parser->context;
18220   parser->context = context->next;
18221   /* If no parse errors occurred, commit to the tentative parse.  */
18222   if (!error_occurred)
18223     {
18224       /* Commit to the tokens read tentatively, unless that was
18225          already done.  */
18226       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18227         cp_lexer_commit_tokens (parser->lexer);
18228
18229       pop_to_parent_deferring_access_checks ();
18230     }
18231   /* Otherwise, if errors occurred, roll back our state so that things
18232      are just as they were before we began the tentative parse.  */
18233   else
18234     {
18235       cp_lexer_rollback_tokens (parser->lexer);
18236       pop_deferring_access_checks ();
18237     }
18238   /* Add the context to the front of the free list.  */
18239   context->next = cp_parser_context_free_list;
18240   cp_parser_context_free_list = context;
18241
18242   return !error_occurred;
18243 }
18244
18245 /* Returns true if we are parsing tentatively and are not committed to
18246    this tentative parse.  */
18247
18248 static bool
18249 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18250 {
18251   return (cp_parser_parsing_tentatively (parser)
18252           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18253 }
18254
18255 /* Returns nonzero iff an error has occurred during the most recent
18256    tentative parse.  */
18257
18258 static bool
18259 cp_parser_error_occurred (cp_parser* parser)
18260 {
18261   return (cp_parser_parsing_tentatively (parser)
18262           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18263 }
18264
18265 /* Returns nonzero if GNU extensions are allowed.  */
18266
18267 static bool
18268 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18269 {
18270   return parser->allow_gnu_extensions_p;
18271 }
18272 \f
18273 /* Objective-C++ Productions */
18274
18275
18276 /* Parse an Objective-C expression, which feeds into a primary-expression
18277    above.
18278
18279    objc-expression:
18280      objc-message-expression
18281      objc-string-literal
18282      objc-encode-expression
18283      objc-protocol-expression
18284      objc-selector-expression
18285
18286   Returns a tree representation of the expression.  */
18287
18288 static tree
18289 cp_parser_objc_expression (cp_parser* parser)
18290 {
18291   /* Try to figure out what kind of declaration is present.  */
18292   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18293
18294   switch (kwd->type)
18295     {
18296     case CPP_OPEN_SQUARE:
18297       return cp_parser_objc_message_expression (parser);
18298
18299     case CPP_OBJC_STRING:
18300       kwd = cp_lexer_consume_token (parser->lexer);
18301       return objc_build_string_object (kwd->u.value);
18302
18303     case CPP_KEYWORD:
18304       switch (kwd->keyword)
18305         {
18306         case RID_AT_ENCODE:
18307           return cp_parser_objc_encode_expression (parser);
18308
18309         case RID_AT_PROTOCOL:
18310           return cp_parser_objc_protocol_expression (parser);
18311
18312         case RID_AT_SELECTOR:
18313           return cp_parser_objc_selector_expression (parser);
18314
18315         default:
18316           break;
18317         }
18318     default:
18319       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18320       cp_parser_skip_to_end_of_block_or_statement (parser);
18321     }
18322
18323   return error_mark_node;
18324 }
18325
18326 /* Parse an Objective-C message expression.
18327
18328    objc-message-expression:
18329      [ objc-message-receiver objc-message-args ]
18330
18331    Returns a representation of an Objective-C message.  */
18332
18333 static tree
18334 cp_parser_objc_message_expression (cp_parser* parser)
18335 {
18336   tree receiver, messageargs;
18337
18338   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18339   receiver = cp_parser_objc_message_receiver (parser);
18340   messageargs = cp_parser_objc_message_args (parser);
18341   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18342
18343   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18344 }
18345
18346 /* Parse an objc-message-receiver.
18347
18348    objc-message-receiver:
18349      expression
18350      simple-type-specifier
18351
18352   Returns a representation of the type or expression.  */
18353
18354 static tree
18355 cp_parser_objc_message_receiver (cp_parser* parser)
18356 {
18357   tree rcv;
18358
18359   /* An Objective-C message receiver may be either (1) a type
18360      or (2) an expression.  */
18361   cp_parser_parse_tentatively (parser);
18362   rcv = cp_parser_expression (parser, false);
18363
18364   if (cp_parser_parse_definitely (parser))
18365     return rcv;
18366
18367   rcv = cp_parser_simple_type_specifier (parser,
18368                                          /*decl_specs=*/NULL,
18369                                          CP_PARSER_FLAGS_NONE);
18370
18371   return objc_get_class_reference (rcv);
18372 }
18373
18374 /* Parse the arguments and selectors comprising an Objective-C message.
18375
18376    objc-message-args:
18377      objc-selector
18378      objc-selector-args
18379      objc-selector-args , objc-comma-args
18380
18381    objc-selector-args:
18382      objc-selector [opt] : assignment-expression
18383      objc-selector-args objc-selector [opt] : assignment-expression
18384
18385    objc-comma-args:
18386      assignment-expression
18387      objc-comma-args , assignment-expression
18388
18389    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18390    selector arguments and TREE_VALUE containing a list of comma
18391    arguments.  */
18392
18393 static tree
18394 cp_parser_objc_message_args (cp_parser* parser)
18395 {
18396   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18397   bool maybe_unary_selector_p = true;
18398   cp_token *token = cp_lexer_peek_token (parser->lexer);
18399
18400   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18401     {
18402       tree selector = NULL_TREE, arg;
18403
18404       if (token->type != CPP_COLON)
18405         selector = cp_parser_objc_selector (parser);
18406
18407       /* Detect if we have a unary selector.  */
18408       if (maybe_unary_selector_p
18409           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18410         return build_tree_list (selector, NULL_TREE);
18411
18412       maybe_unary_selector_p = false;
18413       cp_parser_require (parser, CPP_COLON, "%<:%>");
18414       arg = cp_parser_assignment_expression (parser, false);
18415
18416       sel_args
18417         = chainon (sel_args,
18418                    build_tree_list (selector, arg));
18419
18420       token = cp_lexer_peek_token (parser->lexer);
18421     }
18422
18423   /* Handle non-selector arguments, if any. */
18424   while (token->type == CPP_COMMA)
18425     {
18426       tree arg;
18427
18428       cp_lexer_consume_token (parser->lexer);
18429       arg = cp_parser_assignment_expression (parser, false);
18430
18431       addl_args
18432         = chainon (addl_args,
18433                    build_tree_list (NULL_TREE, arg));
18434
18435       token = cp_lexer_peek_token (parser->lexer);
18436     }
18437
18438   return build_tree_list (sel_args, addl_args);
18439 }
18440
18441 /* Parse an Objective-C encode expression.
18442
18443    objc-encode-expression:
18444      @encode objc-typename
18445
18446    Returns an encoded representation of the type argument.  */
18447
18448 static tree
18449 cp_parser_objc_encode_expression (cp_parser* parser)
18450 {
18451   tree type;
18452
18453   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
18454   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18455   type = complete_type (cp_parser_type_id (parser));
18456   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18457
18458   if (!type)
18459     {
18460       error ("%<@encode%> must specify a type as an argument");
18461       return error_mark_node;
18462     }
18463
18464   return objc_build_encode_expr (type);
18465 }
18466
18467 /* Parse an Objective-C @defs expression.  */
18468
18469 static tree
18470 cp_parser_objc_defs_expression (cp_parser *parser)
18471 {
18472   tree name;
18473
18474   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
18475   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18476   name = cp_parser_identifier (parser);
18477   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18478
18479   return objc_get_class_ivars (name);
18480 }
18481
18482 /* Parse an Objective-C protocol expression.
18483
18484   objc-protocol-expression:
18485     @protocol ( identifier )
18486
18487   Returns a representation of the protocol expression.  */
18488
18489 static tree
18490 cp_parser_objc_protocol_expression (cp_parser* parser)
18491 {
18492   tree proto;
18493
18494   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18495   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18496   proto = cp_parser_identifier (parser);
18497   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18498
18499   return objc_build_protocol_expr (proto);
18500 }
18501
18502 /* Parse an Objective-C selector expression.
18503
18504    objc-selector-expression:
18505      @selector ( objc-method-signature )
18506
18507    objc-method-signature:
18508      objc-selector
18509      objc-selector-seq
18510
18511    objc-selector-seq:
18512      objc-selector :
18513      objc-selector-seq objc-selector :
18514
18515   Returns a representation of the method selector.  */
18516
18517 static tree
18518 cp_parser_objc_selector_expression (cp_parser* parser)
18519 {
18520   tree sel_seq = NULL_TREE;
18521   bool maybe_unary_selector_p = true;
18522   cp_token *token;
18523
18524   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
18525   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18526   token = cp_lexer_peek_token (parser->lexer);
18527
18528   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18529          || token->type == CPP_SCOPE)
18530     {
18531       tree selector = NULL_TREE;
18532
18533       if (token->type != CPP_COLON
18534           || token->type == CPP_SCOPE)
18535         selector = cp_parser_objc_selector (parser);
18536
18537       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18538           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18539         {
18540           /* Detect if we have a unary selector.  */
18541           if (maybe_unary_selector_p)
18542             {
18543               sel_seq = selector;
18544               goto finish_selector;
18545             }
18546           else
18547             {
18548               cp_parser_error (parser, "expected %<:%>");
18549             }
18550         }
18551       maybe_unary_selector_p = false;
18552       token = cp_lexer_consume_token (parser->lexer);
18553
18554       if (token->type == CPP_SCOPE)
18555         {
18556           sel_seq
18557             = chainon (sel_seq,
18558                        build_tree_list (selector, NULL_TREE));
18559           sel_seq
18560             = chainon (sel_seq,
18561                        build_tree_list (NULL_TREE, NULL_TREE));
18562         }
18563       else
18564         sel_seq
18565           = chainon (sel_seq,
18566                      build_tree_list (selector, NULL_TREE));
18567
18568       token = cp_lexer_peek_token (parser->lexer);
18569     }
18570
18571  finish_selector:
18572   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18573
18574   return objc_build_selector_expr (sel_seq);
18575 }
18576
18577 /* Parse a list of identifiers.
18578
18579    objc-identifier-list:
18580      identifier
18581      objc-identifier-list , identifier
18582
18583    Returns a TREE_LIST of identifier nodes.  */
18584
18585 static tree
18586 cp_parser_objc_identifier_list (cp_parser* parser)
18587 {
18588   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18589   cp_token *sep = cp_lexer_peek_token (parser->lexer);
18590
18591   while (sep->type == CPP_COMMA)
18592     {
18593       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18594       list = chainon (list,
18595                       build_tree_list (NULL_TREE,
18596                                        cp_parser_identifier (parser)));
18597       sep = cp_lexer_peek_token (parser->lexer);
18598     }
18599
18600   return list;
18601 }
18602
18603 /* Parse an Objective-C alias declaration.
18604
18605    objc-alias-declaration:
18606      @compatibility_alias identifier identifier ;
18607
18608    This function registers the alias mapping with the Objective-C front end.
18609    It returns nothing.  */
18610
18611 static void
18612 cp_parser_objc_alias_declaration (cp_parser* parser)
18613 {
18614   tree alias, orig;
18615
18616   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
18617   alias = cp_parser_identifier (parser);
18618   orig = cp_parser_identifier (parser);
18619   objc_declare_alias (alias, orig);
18620   cp_parser_consume_semicolon_at_end_of_statement (parser);
18621 }
18622
18623 /* Parse an Objective-C class forward-declaration.
18624
18625    objc-class-declaration:
18626      @class objc-identifier-list ;
18627
18628    The function registers the forward declarations with the Objective-C
18629    front end.  It returns nothing.  */
18630
18631 static void
18632 cp_parser_objc_class_declaration (cp_parser* parser)
18633 {
18634   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
18635   objc_declare_class (cp_parser_objc_identifier_list (parser));
18636   cp_parser_consume_semicolon_at_end_of_statement (parser);
18637 }
18638
18639 /* Parse a list of Objective-C protocol references.
18640
18641    objc-protocol-refs-opt:
18642      objc-protocol-refs [opt]
18643
18644    objc-protocol-refs:
18645      < objc-identifier-list >
18646
18647    Returns a TREE_LIST of identifiers, if any.  */
18648
18649 static tree
18650 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18651 {
18652   tree protorefs = NULL_TREE;
18653
18654   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18655     {
18656       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
18657       protorefs = cp_parser_objc_identifier_list (parser);
18658       cp_parser_require (parser, CPP_GREATER, "%<>%>");
18659     }
18660
18661   return protorefs;
18662 }
18663
18664 /* Parse a Objective-C visibility specification.  */
18665
18666 static void
18667 cp_parser_objc_visibility_spec (cp_parser* parser)
18668 {
18669   cp_token *vis = cp_lexer_peek_token (parser->lexer);
18670
18671   switch (vis->keyword)
18672     {
18673     case RID_AT_PRIVATE:
18674       objc_set_visibility (2);
18675       break;
18676     case RID_AT_PROTECTED:
18677       objc_set_visibility (0);
18678       break;
18679     case RID_AT_PUBLIC:
18680       objc_set_visibility (1);
18681       break;
18682     default:
18683       return;
18684     }
18685
18686   /* Eat '@private'/'@protected'/'@public'.  */
18687   cp_lexer_consume_token (parser->lexer);
18688 }
18689
18690 /* Parse an Objective-C method type.  */
18691
18692 static void
18693 cp_parser_objc_method_type (cp_parser* parser)
18694 {
18695   objc_set_method_type
18696    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18697     ? PLUS_EXPR
18698     : MINUS_EXPR);
18699 }
18700
18701 /* Parse an Objective-C protocol qualifier.  */
18702
18703 static tree
18704 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18705 {
18706   tree quals = NULL_TREE, node;
18707   cp_token *token = cp_lexer_peek_token (parser->lexer);
18708
18709   node = token->u.value;
18710
18711   while (node && TREE_CODE (node) == IDENTIFIER_NODE
18712          && (node == ridpointers [(int) RID_IN]
18713              || node == ridpointers [(int) RID_OUT]
18714              || node == ridpointers [(int) RID_INOUT]
18715              || node == ridpointers [(int) RID_BYCOPY]
18716              || node == ridpointers [(int) RID_BYREF]
18717              || node == ridpointers [(int) RID_ONEWAY]))
18718     {
18719       quals = tree_cons (NULL_TREE, node, quals);
18720       cp_lexer_consume_token (parser->lexer);
18721       token = cp_lexer_peek_token (parser->lexer);
18722       node = token->u.value;
18723     }
18724
18725   return quals;
18726 }
18727
18728 /* Parse an Objective-C typename.  */
18729
18730 static tree
18731 cp_parser_objc_typename (cp_parser* parser)
18732 {
18733   tree typename = NULL_TREE;
18734
18735   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18736     {
18737       tree proto_quals, cp_type = NULL_TREE;
18738
18739       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18740       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18741
18742       /* An ObjC type name may consist of just protocol qualifiers, in which
18743          case the type shall default to 'id'.  */
18744       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18745         cp_type = cp_parser_type_id (parser);
18746
18747       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18748       typename = build_tree_list (proto_quals, cp_type);
18749     }
18750
18751   return typename;
18752 }
18753
18754 /* Check to see if TYPE refers to an Objective-C selector name.  */
18755
18756 static bool
18757 cp_parser_objc_selector_p (enum cpp_ttype type)
18758 {
18759   return (type == CPP_NAME || type == CPP_KEYWORD
18760           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18761           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18762           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18763           || type == CPP_XOR || type == CPP_XOR_EQ);
18764 }
18765
18766 /* Parse an Objective-C selector.  */
18767
18768 static tree
18769 cp_parser_objc_selector (cp_parser* parser)
18770 {
18771   cp_token *token = cp_lexer_consume_token (parser->lexer);
18772
18773   if (!cp_parser_objc_selector_p (token->type))
18774     {
18775       error ("invalid Objective-C++ selector name");
18776       return error_mark_node;
18777     }
18778
18779   /* C++ operator names are allowed to appear in ObjC selectors.  */
18780   switch (token->type)
18781     {
18782     case CPP_AND_AND: return get_identifier ("and");
18783     case CPP_AND_EQ: return get_identifier ("and_eq");
18784     case CPP_AND: return get_identifier ("bitand");
18785     case CPP_OR: return get_identifier ("bitor");
18786     case CPP_COMPL: return get_identifier ("compl");
18787     case CPP_NOT: return get_identifier ("not");
18788     case CPP_NOT_EQ: return get_identifier ("not_eq");
18789     case CPP_OR_OR: return get_identifier ("or");
18790     case CPP_OR_EQ: return get_identifier ("or_eq");
18791     case CPP_XOR: return get_identifier ("xor");
18792     case CPP_XOR_EQ: return get_identifier ("xor_eq");
18793     default: return token->u.value;
18794     }
18795 }
18796
18797 /* Parse an Objective-C params list.  */
18798
18799 static tree
18800 cp_parser_objc_method_keyword_params (cp_parser* parser)
18801 {
18802   tree params = NULL_TREE;
18803   bool maybe_unary_selector_p = true;
18804   cp_token *token = cp_lexer_peek_token (parser->lexer);
18805
18806   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18807     {
18808       tree selector = NULL_TREE, typename, identifier;
18809
18810       if (token->type != CPP_COLON)
18811         selector = cp_parser_objc_selector (parser);
18812
18813       /* Detect if we have a unary selector.  */
18814       if (maybe_unary_selector_p
18815           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18816         return selector;
18817
18818       maybe_unary_selector_p = false;
18819       cp_parser_require (parser, CPP_COLON, "%<:%>");
18820       typename = cp_parser_objc_typename (parser);
18821       identifier = cp_parser_identifier (parser);
18822
18823       params
18824         = chainon (params,
18825                    objc_build_keyword_decl (selector,
18826                                             typename,
18827                                             identifier));
18828
18829       token = cp_lexer_peek_token (parser->lexer);
18830     }
18831
18832   return params;
18833 }
18834
18835 /* Parse the non-keyword Objective-C params.  */
18836
18837 static tree
18838 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18839 {
18840   tree params = make_node (TREE_LIST);
18841   cp_token *token = cp_lexer_peek_token (parser->lexer);
18842   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
18843
18844   while (token->type == CPP_COMMA)
18845     {
18846       cp_parameter_declarator *parmdecl;
18847       tree parm;
18848
18849       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18850       token = cp_lexer_peek_token (parser->lexer);
18851
18852       if (token->type == CPP_ELLIPSIS)
18853         {
18854           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
18855           *ellipsisp = true;
18856           break;
18857         }
18858
18859       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18860       parm = grokdeclarator (parmdecl->declarator,
18861                              &parmdecl->decl_specifiers,
18862                              PARM, /*initialized=*/0,
18863                              /*attrlist=*/NULL);
18864
18865       chainon (params, build_tree_list (NULL_TREE, parm));
18866       token = cp_lexer_peek_token (parser->lexer);
18867     }
18868
18869   return params;
18870 }
18871
18872 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
18873
18874 static void
18875 cp_parser_objc_interstitial_code (cp_parser* parser)
18876 {
18877   cp_token *token = cp_lexer_peek_token (parser->lexer);
18878
18879   /* If the next token is `extern' and the following token is a string
18880      literal, then we have a linkage specification.  */
18881   if (token->keyword == RID_EXTERN
18882       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18883     cp_parser_linkage_specification (parser);
18884   /* Handle #pragma, if any.  */
18885   else if (token->type == CPP_PRAGMA)
18886     cp_parser_pragma (parser, pragma_external);
18887   /* Allow stray semicolons.  */
18888   else if (token->type == CPP_SEMICOLON)
18889     cp_lexer_consume_token (parser->lexer);
18890   /* Finally, try to parse a block-declaration, or a function-definition.  */
18891   else
18892     cp_parser_block_declaration (parser, /*statement_p=*/false);
18893 }
18894
18895 /* Parse a method signature.  */
18896
18897 static tree
18898 cp_parser_objc_method_signature (cp_parser* parser)
18899 {
18900   tree rettype, kwdparms, optparms;
18901   bool ellipsis = false;
18902
18903   cp_parser_objc_method_type (parser);
18904   rettype = cp_parser_objc_typename (parser);
18905   kwdparms = cp_parser_objc_method_keyword_params (parser);
18906   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18907
18908   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18909 }
18910
18911 /* Pars an Objective-C method prototype list.  */
18912
18913 static void
18914 cp_parser_objc_method_prototype_list (cp_parser* parser)
18915 {
18916   cp_token *token = cp_lexer_peek_token (parser->lexer);
18917
18918   while (token->keyword != RID_AT_END)
18919     {
18920       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18921         {
18922           objc_add_method_declaration
18923            (cp_parser_objc_method_signature (parser));
18924           cp_parser_consume_semicolon_at_end_of_statement (parser);
18925         }
18926       else
18927         /* Allow for interspersed non-ObjC++ code.  */
18928         cp_parser_objc_interstitial_code (parser);
18929
18930       token = cp_lexer_peek_token (parser->lexer);
18931     }
18932
18933   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18934   objc_finish_interface ();
18935 }
18936
18937 /* Parse an Objective-C method definition list.  */
18938
18939 static void
18940 cp_parser_objc_method_definition_list (cp_parser* parser)
18941 {
18942   cp_token *token = cp_lexer_peek_token (parser->lexer);
18943
18944   while (token->keyword != RID_AT_END)
18945     {
18946       tree meth;
18947
18948       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18949         {
18950           push_deferring_access_checks (dk_deferred);
18951           objc_start_method_definition
18952            (cp_parser_objc_method_signature (parser));
18953
18954           /* For historical reasons, we accept an optional semicolon.  */
18955           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18956             cp_lexer_consume_token (parser->lexer);
18957
18958           perform_deferred_access_checks ();
18959           stop_deferring_access_checks ();
18960           meth = cp_parser_function_definition_after_declarator (parser,
18961                                                                  false);
18962           pop_deferring_access_checks ();
18963           objc_finish_method_definition (meth);
18964         }
18965       else
18966         /* Allow for interspersed non-ObjC++ code.  */
18967         cp_parser_objc_interstitial_code (parser);
18968
18969       token = cp_lexer_peek_token (parser->lexer);
18970     }
18971
18972   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18973   objc_finish_implementation ();
18974 }
18975
18976 /* Parse Objective-C ivars.  */
18977
18978 static void
18979 cp_parser_objc_class_ivars (cp_parser* parser)
18980 {
18981   cp_token *token = cp_lexer_peek_token (parser->lexer);
18982
18983   if (token->type != CPP_OPEN_BRACE)
18984     return;     /* No ivars specified.  */
18985
18986   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
18987   token = cp_lexer_peek_token (parser->lexer);
18988
18989   while (token->type != CPP_CLOSE_BRACE)
18990     {
18991       cp_decl_specifier_seq declspecs;
18992       int decl_class_or_enum_p;
18993       tree prefix_attributes;
18994
18995       cp_parser_objc_visibility_spec (parser);
18996
18997       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18998         break;
18999
19000       cp_parser_decl_specifier_seq (parser,
19001                                     CP_PARSER_FLAGS_OPTIONAL,
19002                                     &declspecs,
19003                                     &decl_class_or_enum_p);
19004       prefix_attributes = declspecs.attributes;
19005       declspecs.attributes = NULL_TREE;
19006
19007       /* Keep going until we hit the `;' at the end of the
19008          declaration.  */
19009       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19010         {
19011           tree width = NULL_TREE, attributes, first_attribute, decl;
19012           cp_declarator *declarator = NULL;
19013           int ctor_dtor_or_conv_p;
19014
19015           /* Check for a (possibly unnamed) bitfield declaration.  */
19016           token = cp_lexer_peek_token (parser->lexer);
19017           if (token->type == CPP_COLON)
19018             goto eat_colon;
19019
19020           if (token->type == CPP_NAME
19021               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19022                   == CPP_COLON))
19023             {
19024               /* Get the name of the bitfield.  */
19025               declarator = make_id_declarator (NULL_TREE,
19026                                                cp_parser_identifier (parser),
19027                                                sfk_none);
19028
19029              eat_colon:
19030               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19031               /* Get the width of the bitfield.  */
19032               width
19033                 = cp_parser_constant_expression (parser,
19034                                                  /*allow_non_constant=*/false,
19035                                                  NULL);
19036             }
19037           else
19038             {
19039               /* Parse the declarator.  */
19040               declarator
19041                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19042                                         &ctor_dtor_or_conv_p,
19043                                         /*parenthesized_p=*/NULL,
19044                                         /*member_p=*/false);
19045             }
19046
19047           /* Look for attributes that apply to the ivar.  */
19048           attributes = cp_parser_attributes_opt (parser);
19049           /* Remember which attributes are prefix attributes and
19050              which are not.  */
19051           first_attribute = attributes;
19052           /* Combine the attributes.  */
19053           attributes = chainon (prefix_attributes, attributes);
19054
19055           if (width)
19056             {
19057               /* Create the bitfield declaration.  */
19058               decl = grokbitfield (declarator, &declspecs, width);
19059               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
19060             }
19061           else
19062             decl = grokfield (declarator, &declspecs,
19063                               NULL_TREE, /*init_const_expr_p=*/false,
19064                               NULL_TREE, attributes);
19065
19066           /* Add the instance variable.  */
19067           objc_add_instance_variable (decl);
19068
19069           /* Reset PREFIX_ATTRIBUTES.  */
19070           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19071             attributes = TREE_CHAIN (attributes);
19072           if (attributes)
19073             TREE_CHAIN (attributes) = NULL_TREE;
19074
19075           token = cp_lexer_peek_token (parser->lexer);
19076
19077           if (token->type == CPP_COMMA)
19078             {
19079               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19080               continue;
19081             }
19082           break;
19083         }
19084
19085       cp_parser_consume_semicolon_at_end_of_statement (parser);
19086       token = cp_lexer_peek_token (parser->lexer);
19087     }
19088
19089   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19090   /* For historical reasons, we accept an optional semicolon.  */
19091   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19092     cp_lexer_consume_token (parser->lexer);
19093 }
19094
19095 /* Parse an Objective-C protocol declaration.  */
19096
19097 static void
19098 cp_parser_objc_protocol_declaration (cp_parser* parser)
19099 {
19100   tree proto, protorefs;
19101   cp_token *tok;
19102
19103   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19104   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19105     {
19106       error ("identifier expected after %<@protocol%>");
19107       goto finish;
19108     }
19109
19110   /* See if we have a forward declaration or a definition.  */
19111   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19112
19113   /* Try a forward declaration first.  */
19114   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19115     {
19116       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19117      finish:
19118       cp_parser_consume_semicolon_at_end_of_statement (parser);
19119     }
19120
19121   /* Ok, we got a full-fledged definition (or at least should).  */
19122   else
19123     {
19124       proto = cp_parser_identifier (parser);
19125       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19126       objc_start_protocol (proto, protorefs);
19127       cp_parser_objc_method_prototype_list (parser);
19128     }
19129 }
19130
19131 /* Parse an Objective-C superclass or category.  */
19132
19133 static void
19134 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19135                                                           tree *categ)
19136 {
19137   cp_token *next = cp_lexer_peek_token (parser->lexer);
19138
19139   *super = *categ = NULL_TREE;
19140   if (next->type == CPP_COLON)
19141     {
19142       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19143       *super = cp_parser_identifier (parser);
19144     }
19145   else if (next->type == CPP_OPEN_PAREN)
19146     {
19147       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19148       *categ = cp_parser_identifier (parser);
19149       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19150     }
19151 }
19152
19153 /* Parse an Objective-C class interface.  */
19154
19155 static void
19156 cp_parser_objc_class_interface (cp_parser* parser)
19157 {
19158   tree name, super, categ, protos;
19159
19160   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19161   name = cp_parser_identifier (parser);
19162   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19163   protos = cp_parser_objc_protocol_refs_opt (parser);
19164
19165   /* We have either a class or a category on our hands.  */
19166   if (categ)
19167     objc_start_category_interface (name, categ, protos);
19168   else
19169     {
19170       objc_start_class_interface (name, super, protos);
19171       /* Handle instance variable declarations, if any.  */
19172       cp_parser_objc_class_ivars (parser);
19173       objc_continue_interface ();
19174     }
19175
19176   cp_parser_objc_method_prototype_list (parser);
19177 }
19178
19179 /* Parse an Objective-C class implementation.  */
19180
19181 static void
19182 cp_parser_objc_class_implementation (cp_parser* parser)
19183 {
19184   tree name, super, categ;
19185
19186   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19187   name = cp_parser_identifier (parser);
19188   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19189
19190   /* We have either a class or a category on our hands.  */
19191   if (categ)
19192     objc_start_category_implementation (name, categ);
19193   else
19194     {
19195       objc_start_class_implementation (name, super);
19196       /* Handle instance variable declarations, if any.  */
19197       cp_parser_objc_class_ivars (parser);
19198       objc_continue_implementation ();
19199     }
19200
19201   cp_parser_objc_method_definition_list (parser);
19202 }
19203
19204 /* Consume the @end token and finish off the implementation.  */
19205
19206 static void
19207 cp_parser_objc_end_implementation (cp_parser* parser)
19208 {
19209   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19210   objc_finish_implementation ();
19211 }
19212
19213 /* Parse an Objective-C declaration.  */
19214
19215 static void
19216 cp_parser_objc_declaration (cp_parser* parser)
19217 {
19218   /* Try to figure out what kind of declaration is present.  */
19219   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19220
19221   switch (kwd->keyword)
19222     {
19223     case RID_AT_ALIAS:
19224       cp_parser_objc_alias_declaration (parser);
19225       break;
19226     case RID_AT_CLASS:
19227       cp_parser_objc_class_declaration (parser);
19228       break;
19229     case RID_AT_PROTOCOL:
19230       cp_parser_objc_protocol_declaration (parser);
19231       break;
19232     case RID_AT_INTERFACE:
19233       cp_parser_objc_class_interface (parser);
19234       break;
19235     case RID_AT_IMPLEMENTATION:
19236       cp_parser_objc_class_implementation (parser);
19237       break;
19238     case RID_AT_END:
19239       cp_parser_objc_end_implementation (parser);
19240       break;
19241     default:
19242       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19243       cp_parser_skip_to_end_of_block_or_statement (parser);
19244     }
19245 }
19246
19247 /* Parse an Objective-C try-catch-finally statement.
19248
19249    objc-try-catch-finally-stmt:
19250      @try compound-statement objc-catch-clause-seq [opt]
19251        objc-finally-clause [opt]
19252
19253    objc-catch-clause-seq:
19254      objc-catch-clause objc-catch-clause-seq [opt]
19255
19256    objc-catch-clause:
19257      @catch ( exception-declaration ) compound-statement
19258
19259    objc-finally-clause
19260      @finally compound-statement
19261
19262    Returns NULL_TREE.  */
19263
19264 static tree
19265 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19266   location_t location;
19267   tree stmt;
19268
19269   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19270   location = cp_lexer_peek_token (parser->lexer)->location;
19271   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19272      node, lest it get absorbed into the surrounding block.  */
19273   stmt = push_stmt_list ();
19274   cp_parser_compound_statement (parser, NULL, false);
19275   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19276
19277   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19278     {
19279       cp_parameter_declarator *parmdecl;
19280       tree parm;
19281
19282       cp_lexer_consume_token (parser->lexer);
19283       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19284       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19285       parm = grokdeclarator (parmdecl->declarator,
19286                              &parmdecl->decl_specifiers,
19287                              PARM, /*initialized=*/0,
19288                              /*attrlist=*/NULL);
19289       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19290       objc_begin_catch_clause (parm);
19291       cp_parser_compound_statement (parser, NULL, false);
19292       objc_finish_catch_clause ();
19293     }
19294
19295   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19296     {
19297       cp_lexer_consume_token (parser->lexer);
19298       location = cp_lexer_peek_token (parser->lexer)->location;
19299       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19300          node, lest it get absorbed into the surrounding block.  */
19301       stmt = push_stmt_list ();
19302       cp_parser_compound_statement (parser, NULL, false);
19303       objc_build_finally_clause (location, pop_stmt_list (stmt));
19304     }
19305
19306   return objc_finish_try_stmt ();
19307 }
19308
19309 /* Parse an Objective-C synchronized statement.
19310
19311    objc-synchronized-stmt:
19312      @synchronized ( expression ) compound-statement
19313
19314    Returns NULL_TREE.  */
19315
19316 static tree
19317 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19318   location_t location;
19319   tree lock, stmt;
19320
19321   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19322
19323   location = cp_lexer_peek_token (parser->lexer)->location;
19324   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19325   lock = cp_parser_expression (parser, false);
19326   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19327
19328   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19329      node, lest it get absorbed into the surrounding block.  */
19330   stmt = push_stmt_list ();
19331   cp_parser_compound_statement (parser, NULL, false);
19332
19333   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19334 }
19335
19336 /* Parse an Objective-C throw statement.
19337
19338    objc-throw-stmt:
19339      @throw assignment-expression [opt] ;
19340
19341    Returns a constructed '@throw' statement.  */
19342
19343 static tree
19344 cp_parser_objc_throw_statement (cp_parser *parser) {
19345   tree expr = NULL_TREE;
19346
19347   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19348
19349   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19350     expr = cp_parser_assignment_expression (parser, false);
19351
19352   cp_parser_consume_semicolon_at_end_of_statement (parser);
19353
19354   return objc_build_throw_stmt (expr);
19355 }
19356
19357 /* Parse an Objective-C statement.  */
19358
19359 static tree
19360 cp_parser_objc_statement (cp_parser * parser) {
19361   /* Try to figure out what kind of declaration is present.  */
19362   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19363
19364   switch (kwd->keyword)
19365     {
19366     case RID_AT_TRY:
19367       return cp_parser_objc_try_catch_finally_statement (parser);
19368     case RID_AT_SYNCHRONIZED:
19369       return cp_parser_objc_synchronized_statement (parser);
19370     case RID_AT_THROW:
19371       return cp_parser_objc_throw_statement (parser);
19372     default:
19373       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19374       cp_parser_skip_to_end_of_block_or_statement (parser);
19375     }
19376
19377   return error_mark_node;
19378 }
19379 \f
19380 /* OpenMP 2.5 parsing routines.  */
19381
19382 /* Returns name of the next clause.
19383    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19384    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19385    returned and the token is consumed.  */
19386
19387 static pragma_omp_clause
19388 cp_parser_omp_clause_name (cp_parser *parser)
19389 {
19390   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19391
19392   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19393     result = PRAGMA_OMP_CLAUSE_IF;
19394   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19395     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19396   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19397     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19398   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19399     {
19400       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19401       const char *p = IDENTIFIER_POINTER (id);
19402
19403       switch (p[0])
19404         {
19405         case 'c':
19406           if (!strcmp ("copyin", p))
19407             result = PRAGMA_OMP_CLAUSE_COPYIN;
19408           else if (!strcmp ("copyprivate", p))
19409             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19410           break;
19411         case 'f':
19412           if (!strcmp ("firstprivate", p))
19413             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19414           break;
19415         case 'l':
19416           if (!strcmp ("lastprivate", p))
19417             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19418           break;
19419         case 'n':
19420           if (!strcmp ("nowait", p))
19421             result = PRAGMA_OMP_CLAUSE_NOWAIT;
19422           else if (!strcmp ("num_threads", p))
19423             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19424           break;
19425         case 'o':
19426           if (!strcmp ("ordered", p))
19427             result = PRAGMA_OMP_CLAUSE_ORDERED;
19428           break;
19429         case 'r':
19430           if (!strcmp ("reduction", p))
19431             result = PRAGMA_OMP_CLAUSE_REDUCTION;
19432           break;
19433         case 's':
19434           if (!strcmp ("schedule", p))
19435             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19436           else if (!strcmp ("shared", p))
19437             result = PRAGMA_OMP_CLAUSE_SHARED;
19438           break;
19439         }
19440     }
19441
19442   if (result != PRAGMA_OMP_CLAUSE_NONE)
19443     cp_lexer_consume_token (parser->lexer);
19444
19445   return result;
19446 }
19447
19448 /* Validate that a clause of the given type does not already exist.  */
19449
19450 static void
19451 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19452 {
19453   tree c;
19454
19455   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19456     if (OMP_CLAUSE_CODE (c) == code)
19457       {
19458         error ("too many %qs clauses", name);
19459         break;
19460       }
19461 }
19462
19463 /* OpenMP 2.5:
19464    variable-list:
19465      identifier
19466      variable-list , identifier
19467
19468    In addition, we match a closing parenthesis.  An opening parenthesis
19469    will have been consumed by the caller.
19470
19471    If KIND is nonzero, create the appropriate node and install the decl
19472    in OMP_CLAUSE_DECL and add the node to the head of the list.
19473
19474    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19475    return the list created.  */
19476
19477 static tree
19478 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19479                                 tree list)
19480 {
19481   while (1)
19482     {
19483       tree name, decl;
19484
19485       name = cp_parser_id_expression (parser, /*template_p=*/false,
19486                                       /*check_dependency_p=*/true,
19487                                       /*template_p=*/NULL,
19488                                       /*declarator_p=*/false,
19489                                       /*optional_p=*/false);
19490       if (name == error_mark_node)
19491         goto skip_comma;
19492
19493       decl = cp_parser_lookup_name_simple (parser, name);
19494       if (decl == error_mark_node)
19495         cp_parser_name_lookup_error (parser, name, decl, NULL);
19496       else if (kind != 0)
19497         {
19498           tree u = build_omp_clause (kind);
19499           OMP_CLAUSE_DECL (u) = decl;
19500           OMP_CLAUSE_CHAIN (u) = list;
19501           list = u;
19502         }
19503       else
19504         list = tree_cons (decl, NULL_TREE, list);
19505
19506     get_comma:
19507       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19508         break;
19509       cp_lexer_consume_token (parser->lexer);
19510     }
19511
19512   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19513     {
19514       int ending;
19515
19516       /* Try to resync to an unnested comma.  Copied from
19517          cp_parser_parenthesized_expression_list.  */
19518     skip_comma:
19519       ending = cp_parser_skip_to_closing_parenthesis (parser,
19520                                                       /*recovering=*/true,
19521                                                       /*or_comma=*/true,
19522                                                       /*consume_paren=*/true);
19523       if (ending < 0)
19524         goto get_comma;
19525     }
19526
19527   return list;
19528 }
19529
19530 /* Similarly, but expect leading and trailing parenthesis.  This is a very
19531    common case for omp clauses.  */
19532
19533 static tree
19534 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19535 {
19536   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19537     return cp_parser_omp_var_list_no_open (parser, kind, list);
19538   return list;
19539 }
19540
19541 /* OpenMP 2.5:
19542    default ( shared | none ) */
19543
19544 static tree
19545 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19546 {
19547   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19548   tree c;
19549
19550   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19551     return list;
19552   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19553     {
19554       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19555       const char *p = IDENTIFIER_POINTER (id);
19556
19557       switch (p[0])
19558         {
19559         case 'n':
19560           if (strcmp ("none", p) != 0)
19561             goto invalid_kind;
19562           kind = OMP_CLAUSE_DEFAULT_NONE;
19563           break;
19564
19565         case 's':
19566           if (strcmp ("shared", p) != 0)
19567             goto invalid_kind;
19568           kind = OMP_CLAUSE_DEFAULT_SHARED;
19569           break;
19570
19571         default:
19572           goto invalid_kind;
19573         }
19574
19575       cp_lexer_consume_token (parser->lexer);
19576     }
19577   else
19578     {
19579     invalid_kind:
19580       cp_parser_error (parser, "expected %<none%> or %<shared%>");
19581     }
19582
19583   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19584     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19585                                            /*or_comma=*/false,
19586                                            /*consume_paren=*/true);
19587
19588   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19589     return list;
19590
19591   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19592   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19593   OMP_CLAUSE_CHAIN (c) = list;
19594   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19595
19596   return c;
19597 }
19598
19599 /* OpenMP 2.5:
19600    if ( expression ) */
19601
19602 static tree
19603 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19604 {
19605   tree t, c;
19606
19607   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19608     return list;
19609
19610   t = cp_parser_condition (parser);
19611
19612   if (t == error_mark_node
19613       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19614     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19615                                            /*or_comma=*/false,
19616                                            /*consume_paren=*/true);
19617
19618   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19619
19620   c = build_omp_clause (OMP_CLAUSE_IF);
19621   OMP_CLAUSE_IF_EXPR (c) = t;
19622   OMP_CLAUSE_CHAIN (c) = list;
19623
19624   return c;
19625 }
19626
19627 /* OpenMP 2.5:
19628    nowait */
19629
19630 static tree
19631 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19632 {
19633   tree c;
19634
19635   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19636
19637   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19638   OMP_CLAUSE_CHAIN (c) = list;
19639   return c;
19640 }
19641
19642 /* OpenMP 2.5:
19643    num_threads ( expression ) */
19644
19645 static tree
19646 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19647 {
19648   tree t, c;
19649
19650   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19651     return list;
19652
19653   t = cp_parser_expression (parser, false);
19654
19655   if (t == error_mark_node
19656       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19657     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19658                                            /*or_comma=*/false,
19659                                            /*consume_paren=*/true);
19660
19661   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19662
19663   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19664   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19665   OMP_CLAUSE_CHAIN (c) = list;
19666
19667   return c;
19668 }
19669
19670 /* OpenMP 2.5:
19671    ordered */
19672
19673 static tree
19674 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19675 {
19676   tree c;
19677
19678   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19679
19680   c = build_omp_clause (OMP_CLAUSE_ORDERED);
19681   OMP_CLAUSE_CHAIN (c) = list;
19682   return c;
19683 }
19684
19685 /* OpenMP 2.5:
19686    reduction ( reduction-operator : variable-list )
19687
19688    reduction-operator:
19689      One of: + * - & ^ | && || */
19690
19691 static tree
19692 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19693 {
19694   enum tree_code code;
19695   tree nlist, c;
19696
19697   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19698     return list;
19699
19700   switch (cp_lexer_peek_token (parser->lexer)->type)
19701     {
19702     case CPP_PLUS:
19703       code = PLUS_EXPR;
19704       break;
19705     case CPP_MULT:
19706       code = MULT_EXPR;
19707       break;
19708     case CPP_MINUS:
19709       code = MINUS_EXPR;
19710       break;
19711     case CPP_AND:
19712       code = BIT_AND_EXPR;
19713       break;
19714     case CPP_XOR:
19715       code = BIT_XOR_EXPR;
19716       break;
19717     case CPP_OR:
19718       code = BIT_IOR_EXPR;
19719       break;
19720     case CPP_AND_AND:
19721       code = TRUTH_ANDIF_EXPR;
19722       break;
19723     case CPP_OR_OR:
19724       code = TRUTH_ORIF_EXPR;
19725       break;
19726     default:
19727       cp_parser_error (parser, "%<+%>, %<*%>, %<-%>, %<&%>, %<^%>, %<|%>, "
19728                                "%<&&%>, or %<||%>");
19729     resync_fail:
19730       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19731                                              /*or_comma=*/false,
19732                                              /*consume_paren=*/true);
19733       return list;
19734     }
19735   cp_lexer_consume_token (parser->lexer);
19736
19737   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
19738     goto resync_fail;
19739
19740   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19741   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19742     OMP_CLAUSE_REDUCTION_CODE (c) = code;
19743
19744   return nlist;
19745 }
19746
19747 /* OpenMP 2.5:
19748    schedule ( schedule-kind )
19749    schedule ( schedule-kind , expression )
19750
19751    schedule-kind:
19752      static | dynamic | guided | runtime  */
19753
19754 static tree
19755 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19756 {
19757   tree c, t;
19758
19759   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19760     return list;
19761
19762   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19763
19764   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19765     {
19766       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19767       const char *p = IDENTIFIER_POINTER (id);
19768
19769       switch (p[0])
19770         {
19771         case 'd':
19772           if (strcmp ("dynamic", p) != 0)
19773             goto invalid_kind;
19774           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19775           break;
19776
19777         case 'g':
19778           if (strcmp ("guided", p) != 0)
19779             goto invalid_kind;
19780           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19781           break;
19782
19783         case 'r':
19784           if (strcmp ("runtime", p) != 0)
19785             goto invalid_kind;
19786           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19787           break;
19788
19789         default:
19790           goto invalid_kind;
19791         }
19792     }
19793   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19794     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19795   else
19796     goto invalid_kind;
19797   cp_lexer_consume_token (parser->lexer);
19798
19799   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19800     {
19801       cp_lexer_consume_token (parser->lexer);
19802
19803       t = cp_parser_assignment_expression (parser, false);
19804
19805       if (t == error_mark_node)
19806         goto resync_fail;
19807       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19808         error ("schedule %<runtime%> does not take "
19809                "a %<chunk_size%> parameter");
19810       else
19811         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19812
19813       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19814         goto resync_fail;
19815     }
19816   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
19817     goto resync_fail;
19818
19819   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19820   OMP_CLAUSE_CHAIN (c) = list;
19821   return c;
19822
19823  invalid_kind:
19824   cp_parser_error (parser, "invalid schedule kind");
19825  resync_fail:
19826   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19827                                          /*or_comma=*/false,
19828                                          /*consume_paren=*/true);
19829   return list;
19830 }
19831
19832 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
19833    is a bitmask in MASK.  Return the list of clauses found; the result
19834    of clause default goes in *pdefault.  */
19835
19836 static tree
19837 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19838                            const char *where, cp_token *pragma_tok)
19839 {
19840   tree clauses = NULL;
19841   bool first = true;
19842
19843   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19844     {
19845       pragma_omp_clause c_kind;
19846       const char *c_name;
19847       tree prev = clauses;
19848
19849       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19850         cp_lexer_consume_token (parser->lexer);
19851
19852       c_kind = cp_parser_omp_clause_name (parser);
19853       first = false;
19854
19855       switch (c_kind)
19856         {
19857         case PRAGMA_OMP_CLAUSE_COPYIN:
19858           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19859           c_name = "copyin";
19860           break;
19861         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19862           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19863                                             clauses);
19864           c_name = "copyprivate";
19865           break;
19866         case PRAGMA_OMP_CLAUSE_DEFAULT:
19867           clauses = cp_parser_omp_clause_default (parser, clauses);
19868           c_name = "default";
19869           break;
19870         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19871           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19872                                             clauses);
19873           c_name = "firstprivate";
19874           break;
19875         case PRAGMA_OMP_CLAUSE_IF:
19876           clauses = cp_parser_omp_clause_if (parser, clauses);
19877           c_name = "if";
19878           break;
19879         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19880           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19881                                             clauses);
19882           c_name = "lastprivate";
19883           break;
19884         case PRAGMA_OMP_CLAUSE_NOWAIT:
19885           clauses = cp_parser_omp_clause_nowait (parser, clauses);
19886           c_name = "nowait";
19887           break;
19888         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19889           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19890           c_name = "num_threads";
19891           break;
19892         case PRAGMA_OMP_CLAUSE_ORDERED:
19893           clauses = cp_parser_omp_clause_ordered (parser, clauses);
19894           c_name = "ordered";
19895           break;
19896         case PRAGMA_OMP_CLAUSE_PRIVATE:
19897           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19898                                             clauses);
19899           c_name = "private";
19900           break;
19901         case PRAGMA_OMP_CLAUSE_REDUCTION:
19902           clauses = cp_parser_omp_clause_reduction (parser, clauses);
19903           c_name = "reduction";
19904           break;
19905         case PRAGMA_OMP_CLAUSE_SCHEDULE:
19906           clauses = cp_parser_omp_clause_schedule (parser, clauses);
19907           c_name = "schedule";
19908           break;
19909         case PRAGMA_OMP_CLAUSE_SHARED:
19910           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19911                                             clauses);
19912           c_name = "shared";
19913           break;
19914         default:
19915           cp_parser_error (parser, "expected %<#pragma omp%> clause");
19916           goto saw_error;
19917         }
19918
19919       if (((mask >> c_kind) & 1) == 0)
19920         {
19921           /* Remove the invalid clause(s) from the list to avoid
19922              confusing the rest of the compiler.  */
19923           clauses = prev;
19924           error ("%qs is not valid for %qs", c_name, where);
19925         }
19926     }
19927  saw_error:
19928   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19929   return finish_omp_clauses (clauses);
19930 }
19931
19932 /* OpenMP 2.5:
19933    structured-block:
19934      statement
19935
19936    In practice, we're also interested in adding the statement to an
19937    outer node.  So it is convenient if we work around the fact that
19938    cp_parser_statement calls add_stmt.  */
19939
19940 static unsigned
19941 cp_parser_begin_omp_structured_block (cp_parser *parser)
19942 {
19943   unsigned save = parser->in_statement;
19944
19945   /* Only move the values to IN_OMP_BLOCK if they weren't false.
19946      This preserves the "not within loop or switch" style error messages
19947      for nonsense cases like
19948         void foo() {
19949         #pragma omp single
19950           break;
19951         }
19952   */
19953   if (parser->in_statement)
19954     parser->in_statement = IN_OMP_BLOCK;
19955
19956   return save;
19957 }
19958
19959 static void
19960 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19961 {
19962   parser->in_statement = save;
19963 }
19964
19965 static tree
19966 cp_parser_omp_structured_block (cp_parser *parser)
19967 {
19968   tree stmt = begin_omp_structured_block ();
19969   unsigned int save = cp_parser_begin_omp_structured_block (parser);
19970
19971   cp_parser_statement (parser, NULL_TREE, false, NULL);
19972
19973   cp_parser_end_omp_structured_block (parser, save);
19974   return finish_omp_structured_block (stmt);
19975 }
19976
19977 /* OpenMP 2.5:
19978    # pragma omp atomic new-line
19979      expression-stmt
19980
19981    expression-stmt:
19982      x binop= expr | x++ | ++x | x-- | --x
19983    binop:
19984      +, *, -, /, &, ^, |, <<, >>
19985
19986   where x is an lvalue expression with scalar type.  */
19987
19988 static void
19989 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19990 {
19991   tree lhs, rhs;
19992   enum tree_code code;
19993
19994   cp_parser_require_pragma_eol (parser, pragma_tok);
19995
19996   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19997                                     /*cast_p=*/false);
19998   switch (TREE_CODE (lhs))
19999     {
20000     case ERROR_MARK:
20001       goto saw_error;
20002
20003     case PREINCREMENT_EXPR:
20004     case POSTINCREMENT_EXPR:
20005       lhs = TREE_OPERAND (lhs, 0);
20006       code = PLUS_EXPR;
20007       rhs = integer_one_node;
20008       break;
20009
20010     case PREDECREMENT_EXPR:
20011     case POSTDECREMENT_EXPR:
20012       lhs = TREE_OPERAND (lhs, 0);
20013       code = MINUS_EXPR;
20014       rhs = integer_one_node;
20015       break;
20016
20017     default:
20018       switch (cp_lexer_peek_token (parser->lexer)->type)
20019         {
20020         case CPP_MULT_EQ:
20021           code = MULT_EXPR;
20022           break;
20023         case CPP_DIV_EQ:
20024           code = TRUNC_DIV_EXPR;
20025           break;
20026         case CPP_PLUS_EQ:
20027           code = PLUS_EXPR;
20028           break;
20029         case CPP_MINUS_EQ:
20030           code = MINUS_EXPR;
20031           break;
20032         case CPP_LSHIFT_EQ:
20033           code = LSHIFT_EXPR;
20034           break;
20035         case CPP_RSHIFT_EQ:
20036           code = RSHIFT_EXPR;
20037           break;
20038         case CPP_AND_EQ:
20039           code = BIT_AND_EXPR;
20040           break;
20041         case CPP_OR_EQ:
20042           code = BIT_IOR_EXPR;
20043           break;
20044         case CPP_XOR_EQ:
20045           code = BIT_XOR_EXPR;
20046           break;
20047         default:
20048           cp_parser_error (parser,
20049                            "invalid operator for %<#pragma omp atomic%>");
20050           goto saw_error;
20051         }
20052       cp_lexer_consume_token (parser->lexer);
20053
20054       rhs = cp_parser_expression (parser, false);
20055       if (rhs == error_mark_node)
20056         goto saw_error;
20057       break;
20058     }
20059   finish_omp_atomic (code, lhs, rhs);
20060   cp_parser_consume_semicolon_at_end_of_statement (parser);
20061   return;
20062
20063  saw_error:
20064   cp_parser_skip_to_end_of_block_or_statement (parser);
20065 }
20066
20067
20068 /* OpenMP 2.5:
20069    # pragma omp barrier new-line  */
20070
20071 static void
20072 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20073 {
20074   cp_parser_require_pragma_eol (parser, pragma_tok);
20075   finish_omp_barrier ();
20076 }
20077
20078 /* OpenMP 2.5:
20079    # pragma omp critical [(name)] new-line
20080      structured-block  */
20081
20082 static tree
20083 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20084 {
20085   tree stmt, name = NULL;
20086
20087   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20088     {
20089       cp_lexer_consume_token (parser->lexer);
20090
20091       name = cp_parser_identifier (parser);
20092
20093       if (name == error_mark_node
20094           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20095         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20096                                                /*or_comma=*/false,
20097                                                /*consume_paren=*/true);
20098       if (name == error_mark_node)
20099         name = NULL;
20100     }
20101   cp_parser_require_pragma_eol (parser, pragma_tok);
20102
20103   stmt = cp_parser_omp_structured_block (parser);
20104   return c_finish_omp_critical (stmt, name);
20105 }
20106
20107 /* OpenMP 2.5:
20108    # pragma omp flush flush-vars[opt] new-line
20109
20110    flush-vars:
20111      ( variable-list ) */
20112
20113 static void
20114 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20115 {
20116   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20117     (void) cp_parser_omp_var_list (parser, 0, NULL);
20118   cp_parser_require_pragma_eol (parser, pragma_tok);
20119
20120   finish_omp_flush ();
20121 }
20122
20123 /* Parse the restricted form of the for statment allowed by OpenMP.  */
20124
20125 static tree
20126 cp_parser_omp_for_loop (cp_parser *parser)
20127 {
20128   tree init, cond, incr, body, decl, pre_body;
20129   location_t loc;
20130
20131   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20132     {
20133       cp_parser_error (parser, "for statement expected");
20134       return NULL;
20135     }
20136   loc = cp_lexer_consume_token (parser->lexer)->location;
20137   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20138     return NULL;
20139
20140   init = decl = NULL;
20141   pre_body = push_stmt_list ();
20142   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20143     {
20144       cp_decl_specifier_seq type_specifiers;
20145
20146       /* First, try to parse as an initialized declaration.  See
20147          cp_parser_condition, from whence the bulk of this is copied.  */
20148
20149       cp_parser_parse_tentatively (parser);
20150       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20151                                     &type_specifiers);
20152       if (!cp_parser_error_occurred (parser))
20153         {
20154           tree asm_specification, attributes;
20155           cp_declarator *declarator;
20156
20157           declarator = cp_parser_declarator (parser,
20158                                              CP_PARSER_DECLARATOR_NAMED,
20159                                              /*ctor_dtor_or_conv_p=*/NULL,
20160                                              /*parenthesized_p=*/NULL,
20161                                              /*member_p=*/false);
20162           attributes = cp_parser_attributes_opt (parser);
20163           asm_specification = cp_parser_asm_specification_opt (parser);
20164
20165           cp_parser_require (parser, CPP_EQ, "%<=%>");
20166           if (cp_parser_parse_definitely (parser))
20167             {
20168               tree pushed_scope;
20169
20170               decl = start_decl (declarator, &type_specifiers,
20171                                  /*initialized_p=*/false, attributes,
20172                                  /*prefix_attributes=*/NULL_TREE,
20173                                  &pushed_scope);
20174
20175               init = cp_parser_assignment_expression (parser, false);
20176
20177               if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20178                 init = error_mark_node;
20179               else
20180                 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
20181                                 asm_specification, LOOKUP_ONLYCONVERTING);
20182
20183               if (pushed_scope)
20184                 pop_scope (pushed_scope);
20185             }
20186         }
20187       else
20188         cp_parser_abort_tentative_parse (parser);
20189
20190       /* If parsing as an initialized declaration failed, try again as
20191          a simple expression.  */
20192       if (decl == NULL)
20193         init = cp_parser_expression (parser, false);
20194     }
20195   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
20196   pre_body = pop_stmt_list (pre_body);
20197
20198   cond = NULL;
20199   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20200     cond = cp_parser_condition (parser);
20201   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
20202
20203   incr = NULL;
20204   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20205     incr = cp_parser_expression (parser, false);
20206
20207   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20208     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20209                                            /*or_comma=*/false,
20210                                            /*consume_paren=*/true);
20211
20212   /* Note that we saved the original contents of this flag when we entered
20213      the structured block, and so we don't need to re-save it here.  */
20214   parser->in_statement = IN_OMP_FOR;
20215
20216   /* Note that the grammar doesn't call for a structured block here,
20217      though the loop as a whole is a structured block.  */
20218   body = push_stmt_list ();
20219   cp_parser_statement (parser, NULL_TREE, false, NULL);
20220   body = pop_stmt_list (body);
20221
20222   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
20223 }
20224
20225 /* OpenMP 2.5:
20226    #pragma omp for for-clause[optseq] new-line
20227      for-loop  */
20228
20229 #define OMP_FOR_CLAUSE_MASK                             \
20230         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20231         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20232         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
20233         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20234         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
20235         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
20236         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20237
20238 static tree
20239 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20240 {
20241   tree clauses, sb, ret;
20242   unsigned int save;
20243
20244   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20245                                        "#pragma omp for", pragma_tok);
20246
20247   sb = begin_omp_structured_block ();
20248   save = cp_parser_begin_omp_structured_block (parser);
20249
20250   ret = cp_parser_omp_for_loop (parser);
20251   if (ret)
20252     OMP_FOR_CLAUSES (ret) = clauses;
20253
20254   cp_parser_end_omp_structured_block (parser, save);
20255   add_stmt (finish_omp_structured_block (sb));
20256
20257   return ret;
20258 }
20259
20260 /* OpenMP 2.5:
20261    # pragma omp master new-line
20262      structured-block  */
20263
20264 static tree
20265 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20266 {
20267   cp_parser_require_pragma_eol (parser, pragma_tok);
20268   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20269 }
20270
20271 /* OpenMP 2.5:
20272    # pragma omp ordered new-line
20273      structured-block  */
20274
20275 static tree
20276 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20277 {
20278   cp_parser_require_pragma_eol (parser, pragma_tok);
20279   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20280 }
20281
20282 /* OpenMP 2.5:
20283
20284    section-scope:
20285      { section-sequence }
20286
20287    section-sequence:
20288      section-directive[opt] structured-block
20289      section-sequence section-directive structured-block  */
20290
20291 static tree
20292 cp_parser_omp_sections_scope (cp_parser *parser)
20293 {
20294   tree stmt, substmt;
20295   bool error_suppress = false;
20296   cp_token *tok;
20297
20298   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
20299     return NULL_TREE;
20300
20301   stmt = push_stmt_list ();
20302
20303   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20304     {
20305       unsigned save;
20306
20307       substmt = begin_omp_structured_block ();
20308       save = cp_parser_begin_omp_structured_block (parser);
20309
20310       while (1)
20311         {
20312           cp_parser_statement (parser, NULL_TREE, false, NULL);
20313
20314           tok = cp_lexer_peek_token (parser->lexer);
20315           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20316             break;
20317           if (tok->type == CPP_CLOSE_BRACE)
20318             break;
20319           if (tok->type == CPP_EOF)
20320             break;
20321         }
20322
20323       cp_parser_end_omp_structured_block (parser, save);
20324       substmt = finish_omp_structured_block (substmt);
20325       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20326       add_stmt (substmt);
20327     }
20328
20329   while (1)
20330     {
20331       tok = cp_lexer_peek_token (parser->lexer);
20332       if (tok->type == CPP_CLOSE_BRACE)
20333         break;
20334       if (tok->type == CPP_EOF)
20335         break;
20336
20337       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20338         {
20339           cp_lexer_consume_token (parser->lexer);
20340           cp_parser_require_pragma_eol (parser, tok);
20341           error_suppress = false;
20342         }
20343       else if (!error_suppress)
20344         {
20345           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20346           error_suppress = true;
20347         }
20348
20349       substmt = cp_parser_omp_structured_block (parser);
20350       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20351       add_stmt (substmt);
20352     }
20353   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
20354
20355   substmt = pop_stmt_list (stmt);
20356
20357   stmt = make_node (OMP_SECTIONS);
20358   TREE_TYPE (stmt) = void_type_node;
20359   OMP_SECTIONS_BODY (stmt) = substmt;
20360
20361   add_stmt (stmt);
20362   return stmt;
20363 }
20364
20365 /* OpenMP 2.5:
20366    # pragma omp sections sections-clause[optseq] newline
20367      sections-scope  */
20368
20369 #define OMP_SECTIONS_CLAUSE_MASK                        \
20370         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20371         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20372         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
20373         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20374         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20375
20376 static tree
20377 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20378 {
20379   tree clauses, ret;
20380
20381   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20382                                        "#pragma omp sections", pragma_tok);
20383
20384   ret = cp_parser_omp_sections_scope (parser);
20385   if (ret)
20386     OMP_SECTIONS_CLAUSES (ret) = clauses;
20387
20388   return ret;
20389 }
20390
20391 /* OpenMP 2.5:
20392    # pragma parallel parallel-clause new-line
20393    # pragma parallel for parallel-for-clause new-line
20394    # pragma parallel sections parallel-sections-clause new-line  */
20395
20396 #define OMP_PARALLEL_CLAUSE_MASK                        \
20397         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
20398         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20399         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20400         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
20401         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
20402         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
20403         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20404         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20405
20406 static tree
20407 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20408 {
20409   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20410   const char *p_name = "#pragma omp parallel";
20411   tree stmt, clauses, par_clause, ws_clause, block;
20412   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20413   unsigned int save;
20414
20415   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20416     {
20417       cp_lexer_consume_token (parser->lexer);
20418       p_kind = PRAGMA_OMP_PARALLEL_FOR;
20419       p_name = "#pragma omp parallel for";
20420       mask |= OMP_FOR_CLAUSE_MASK;
20421       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20422     }
20423   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20424     {
20425       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20426       const char *p = IDENTIFIER_POINTER (id);
20427       if (strcmp (p, "sections") == 0)
20428         {
20429           cp_lexer_consume_token (parser->lexer);
20430           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20431           p_name = "#pragma omp parallel sections";
20432           mask |= OMP_SECTIONS_CLAUSE_MASK;
20433           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20434         }
20435     }
20436
20437   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20438   block = begin_omp_parallel ();
20439   save = cp_parser_begin_omp_structured_block (parser);
20440
20441   switch (p_kind)
20442     {
20443     case PRAGMA_OMP_PARALLEL:
20444       cp_parser_statement (parser, NULL_TREE, false, NULL);
20445       par_clause = clauses;
20446       break;
20447
20448     case PRAGMA_OMP_PARALLEL_FOR:
20449       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20450       stmt = cp_parser_omp_for_loop (parser);
20451       if (stmt)
20452         OMP_FOR_CLAUSES (stmt) = ws_clause;
20453       break;
20454
20455     case PRAGMA_OMP_PARALLEL_SECTIONS:
20456       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20457       stmt = cp_parser_omp_sections_scope (parser);
20458       if (stmt)
20459         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20460       break;
20461
20462     default:
20463       gcc_unreachable ();
20464     }
20465
20466   cp_parser_end_omp_structured_block (parser, save);
20467   stmt = finish_omp_parallel (par_clause, block);
20468   if (p_kind != PRAGMA_OMP_PARALLEL)
20469     OMP_PARALLEL_COMBINED (stmt) = 1;
20470   return stmt;
20471 }
20472
20473 /* OpenMP 2.5:
20474    # pragma omp single single-clause[optseq] new-line
20475      structured-block  */
20476
20477 #define OMP_SINGLE_CLAUSE_MASK                          \
20478         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20479         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20480         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
20481         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20482
20483 static tree
20484 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20485 {
20486   tree stmt = make_node (OMP_SINGLE);
20487   TREE_TYPE (stmt) = void_type_node;
20488
20489   OMP_SINGLE_CLAUSES (stmt)
20490     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20491                                  "#pragma omp single", pragma_tok);
20492   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20493
20494   return add_stmt (stmt);
20495 }
20496
20497 /* OpenMP 2.5:
20498    # pragma omp threadprivate (variable-list) */
20499
20500 static void
20501 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20502 {
20503   tree vars;
20504
20505   vars = cp_parser_omp_var_list (parser, 0, NULL);
20506   cp_parser_require_pragma_eol (parser, pragma_tok);
20507
20508   finish_omp_threadprivate (vars);
20509 }
20510
20511 /* Main entry point to OpenMP statement pragmas.  */
20512
20513 static void
20514 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20515 {
20516   tree stmt;
20517
20518   switch (pragma_tok->pragma_kind)
20519     {
20520     case PRAGMA_OMP_ATOMIC:
20521       cp_parser_omp_atomic (parser, pragma_tok);
20522       return;
20523     case PRAGMA_OMP_CRITICAL:
20524       stmt = cp_parser_omp_critical (parser, pragma_tok);
20525       break;
20526     case PRAGMA_OMP_FOR:
20527       stmt = cp_parser_omp_for (parser, pragma_tok);
20528       break;
20529     case PRAGMA_OMP_MASTER:
20530       stmt = cp_parser_omp_master (parser, pragma_tok);
20531       break;
20532     case PRAGMA_OMP_ORDERED:
20533       stmt = cp_parser_omp_ordered (parser, pragma_tok);
20534       break;
20535     case PRAGMA_OMP_PARALLEL:
20536       stmt = cp_parser_omp_parallel (parser, pragma_tok);
20537       break;
20538     case PRAGMA_OMP_SECTIONS:
20539       stmt = cp_parser_omp_sections (parser, pragma_tok);
20540       break;
20541     case PRAGMA_OMP_SINGLE:
20542       stmt = cp_parser_omp_single (parser, pragma_tok);
20543       break;
20544     default:
20545       gcc_unreachable ();
20546     }
20547
20548   if (stmt)
20549     SET_EXPR_LOCATION (stmt, pragma_tok->location);
20550 }
20551 \f
20552 /* The parser.  */
20553
20554 static GTY (()) cp_parser *the_parser;
20555
20556 \f
20557 /* Special handling for the first token or line in the file.  The first
20558    thing in the file might be #pragma GCC pch_preprocess, which loads a
20559    PCH file, which is a GC collection point.  So we need to handle this
20560    first pragma without benefit of an existing lexer structure.
20561
20562    Always returns one token to the caller in *FIRST_TOKEN.  This is
20563    either the true first token of the file, or the first token after
20564    the initial pragma.  */
20565
20566 static void
20567 cp_parser_initial_pragma (cp_token *first_token)
20568 {
20569   tree name = NULL;
20570
20571   cp_lexer_get_preprocessor_token (NULL, first_token);
20572   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20573     return;
20574
20575   cp_lexer_get_preprocessor_token (NULL, first_token);
20576   if (first_token->type == CPP_STRING)
20577     {
20578       name = first_token->u.value;
20579
20580       cp_lexer_get_preprocessor_token (NULL, first_token);
20581       if (first_token->type != CPP_PRAGMA_EOL)
20582         error ("junk at end of %<#pragma GCC pch_preprocess%>");
20583     }
20584   else
20585     error ("expected string literal");
20586
20587   /* Skip to the end of the pragma.  */
20588   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20589     cp_lexer_get_preprocessor_token (NULL, first_token);
20590
20591   /* Now actually load the PCH file.  */
20592   if (name)
20593     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20594
20595   /* Read one more token to return to our caller.  We have to do this
20596      after reading the PCH file in, since its pointers have to be
20597      live.  */
20598   cp_lexer_get_preprocessor_token (NULL, first_token);
20599 }
20600
20601 /* Normal parsing of a pragma token.  Here we can (and must) use the
20602    regular lexer.  */
20603
20604 static bool
20605 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20606 {
20607   cp_token *pragma_tok;
20608   unsigned int id;
20609
20610   pragma_tok = cp_lexer_consume_token (parser->lexer);
20611   gcc_assert (pragma_tok->type == CPP_PRAGMA);
20612   parser->lexer->in_pragma = true;
20613
20614   id = pragma_tok->pragma_kind;
20615   switch (id)
20616     {
20617     case PRAGMA_GCC_PCH_PREPROCESS:
20618       error ("%<#pragma GCC pch_preprocess%> must be first");
20619       break;
20620
20621     case PRAGMA_OMP_BARRIER:
20622       switch (context)
20623         {
20624         case pragma_compound:
20625           cp_parser_omp_barrier (parser, pragma_tok);
20626           return false;
20627         case pragma_stmt:
20628           error ("%<#pragma omp barrier%> may only be "
20629                  "used in compound statements");
20630           break;
20631         default:
20632           goto bad_stmt;
20633         }
20634       break;
20635
20636     case PRAGMA_OMP_FLUSH:
20637       switch (context)
20638         {
20639         case pragma_compound:
20640           cp_parser_omp_flush (parser, pragma_tok);
20641           return false;
20642         case pragma_stmt:
20643           error ("%<#pragma omp flush%> may only be "
20644                  "used in compound statements");
20645           break;
20646         default:
20647           goto bad_stmt;
20648         }
20649       break;
20650
20651     case PRAGMA_OMP_THREADPRIVATE:
20652       cp_parser_omp_threadprivate (parser, pragma_tok);
20653       return false;
20654
20655     case PRAGMA_OMP_ATOMIC:
20656     case PRAGMA_OMP_CRITICAL:
20657     case PRAGMA_OMP_FOR:
20658     case PRAGMA_OMP_MASTER:
20659     case PRAGMA_OMP_ORDERED:
20660     case PRAGMA_OMP_PARALLEL:
20661     case PRAGMA_OMP_SECTIONS:
20662     case PRAGMA_OMP_SINGLE:
20663       if (context == pragma_external)
20664         goto bad_stmt;
20665       cp_parser_omp_construct (parser, pragma_tok);
20666       return true;
20667
20668     case PRAGMA_OMP_SECTION:
20669       error ("%<#pragma omp section%> may only be used in "
20670              "%<#pragma omp sections%> construct");
20671       break;
20672
20673     default:
20674       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20675       c_invoke_pragma_handler (id);
20676       break;
20677
20678     bad_stmt:
20679       cp_parser_error (parser, "expected declaration specifiers");
20680       break;
20681     }
20682
20683   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20684   return false;
20685 }
20686
20687 /* The interface the pragma parsers have to the lexer.  */
20688
20689 enum cpp_ttype
20690 pragma_lex (tree *value)
20691 {
20692   cp_token *tok;
20693   enum cpp_ttype ret;
20694
20695   tok = cp_lexer_peek_token (the_parser->lexer);
20696
20697   ret = tok->type;
20698   *value = tok->u.value;
20699
20700   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20701     ret = CPP_EOF;
20702   else if (ret == CPP_STRING)
20703     *value = cp_parser_string_literal (the_parser, false, false);
20704   else
20705     {
20706       cp_lexer_consume_token (the_parser->lexer);
20707       if (ret == CPP_KEYWORD)
20708         ret = CPP_NAME;
20709     }
20710
20711   return ret;
20712 }
20713
20714 \f
20715 /* External interface.  */
20716
20717 /* Parse one entire translation unit.  */
20718
20719 void
20720 c_parse_file (void)
20721 {
20722   bool error_occurred;
20723   static bool already_called = false;
20724
20725   if (already_called)
20726     {
20727       sorry ("inter-module optimizations not implemented for C++");
20728       return;
20729     }
20730   already_called = true;
20731
20732   the_parser = cp_parser_new ();
20733   push_deferring_access_checks (flag_access_control
20734                                 ? dk_no_deferred : dk_no_check);
20735   error_occurred = cp_parser_translation_unit (the_parser);
20736   the_parser = NULL;
20737 }
20738
20739 #include "gt-cp-parser.h"