OSDN Git Service

2006-12-16 Simon Martin <simartin@users.sourceforge.net>
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005  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 2, 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 COPYING.  If not, write to the Free
20    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41
42 \f
43 /* The lexer.  */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46    and c-lex.c) and the C++ parser.  */
47
48 /* A token's value and its associated deferred access checks and
49    qualifying scope.  */
50
51 struct tree_check GTY(())
52 {
53   /* The value associated with the token.  */
54   tree value;
55   /* The checks that have been associated with value.  */
56   VEC (deferred_access_check, gc)* checks;
57   /* The token's qualifying scope (used when it is a
58      CPP_NESTED_NAME_SPECIFIER).  */
59   tree qualifying_scope;
60 };
61
62 /* A C++ token.  */
63
64 typedef struct cp_token GTY (())
65 {
66   /* The kind of token.  */
67   ENUM_BITFIELD (cpp_ttype) type : 8;
68   /* If this token is a keyword, this value indicates which keyword.
69      Otherwise, this value is RID_MAX.  */
70   ENUM_BITFIELD (rid) keyword : 8;
71   /* Token flags.  */
72   unsigned char flags;
73   /* Identifier for the pragma.  */
74   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
75   /* True if this token is from a system header.  */
76   BOOL_BITFIELD in_system_header : 1;
77   /* True if this token is from a context where it is implicitly extern "C" */
78   BOOL_BITFIELD implicit_extern_c : 1;
79   /* True for a CPP_NAME token that is not a keyword (i.e., for which
80      KEYWORD is RID_MAX) iff this name was looked up and found to be
81      ambiguous.  An error has already been reported.  */
82   BOOL_BITFIELD ambiguous_p : 1;
83   /* The input file stack index at which this token was found.  */
84   unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
85   /* The value associated with this token, if any.  */
86   union cp_token_value {
87     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
88     struct tree_check* GTY((tag ("1"))) tree_check_value;
89     /* Use for all other tokens.  */
90     tree GTY((tag ("0"))) value;
91   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
92   /* The location at which this token was found.  */
93   location_t location;
94 } cp_token;
95
96 /* We use a stack of token pointer for saving token sets.  */
97 typedef struct cp_token *cp_token_position;
98 DEF_VEC_P (cp_token_position);
99 DEF_VEC_ALLOC_P (cp_token_position,heap);
100
101 static const cp_token eof_token =
102 {
103   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
104 #if USE_MAPPED_LOCATION
105   0
106 #else
107   {0, 0}
108 #endif
109 };
110
111 /* The cp_lexer structure represents the C++ lexer.  It is responsible
112    for managing the token stream from the preprocessor and supplying
113    it to the parser.  Tokens are never added to the cp_lexer after
114    it is created.  */
115
116 typedef struct cp_lexer GTY (())
117 {
118   /* The memory allocated for the buffer.  NULL if this lexer does not
119      own the token buffer.  */
120   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
121   /* If the lexer owns the buffer, this is the number of tokens in the
122      buffer.  */
123   size_t buffer_length;
124
125   /* A pointer just past the last available token.  The tokens
126      in this lexer are [buffer, last_token).  */
127   cp_token_position GTY ((skip)) last_token;
128
129   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
130      no more available tokens.  */
131   cp_token_position GTY ((skip)) next_token;
132
133   /* A stack indicating positions at which cp_lexer_save_tokens was
134      called.  The top entry is the most recent position at which we
135      began saving tokens.  If the stack is non-empty, we are saving
136      tokens.  */
137   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
138
139   /* The next lexer in a linked list of lexers.  */
140   struct cp_lexer *next;
141
142   /* True if we should output debugging information.  */
143   bool debugging_p;
144
145   /* True if we're in the context of parsing a pragma, and should not
146      increment past the end-of-line marker.  */
147   bool in_pragma;
148 } cp_lexer;
149
150 /* cp_token_cache is a range of tokens.  There is no need to represent
151    allocate heap memory for it, since tokens are never removed from the
152    lexer's array.  There is also no need for the GC to walk through
153    a cp_token_cache, since everything in here is referenced through
154    a lexer.  */
155
156 typedef struct cp_token_cache GTY(())
157 {
158   /* The beginning of the token range.  */
159   cp_token * GTY((skip)) first;
160
161   /* Points immediately after the last token in the range.  */
162   cp_token * GTY ((skip)) last;
163 } cp_token_cache;
164
165 /* Prototypes.  */
166
167 static cp_lexer *cp_lexer_new_main
168   (void);
169 static cp_lexer *cp_lexer_new_from_tokens
170   (cp_token_cache *tokens);
171 static void cp_lexer_destroy
172   (cp_lexer *);
173 static int cp_lexer_saving_tokens
174   (const cp_lexer *);
175 static cp_token_position cp_lexer_token_position
176   (cp_lexer *, bool);
177 static cp_token *cp_lexer_token_at
178   (cp_lexer *, cp_token_position);
179 static void cp_lexer_get_preprocessor_token
180   (cp_lexer *, cp_token *);
181 static inline cp_token *cp_lexer_peek_token
182   (cp_lexer *);
183 static cp_token *cp_lexer_peek_nth_token
184   (cp_lexer *, size_t);
185 static inline bool cp_lexer_next_token_is
186   (cp_lexer *, enum cpp_ttype);
187 static bool cp_lexer_next_token_is_not
188   (cp_lexer *, enum cpp_ttype);
189 static bool cp_lexer_next_token_is_keyword
190   (cp_lexer *, enum rid);
191 static cp_token *cp_lexer_consume_token
192   (cp_lexer *);
193 static void cp_lexer_purge_token
194   (cp_lexer *);
195 static void cp_lexer_purge_tokens_after
196   (cp_lexer *, cp_token_position);
197 static void cp_lexer_save_tokens
198   (cp_lexer *);
199 static void cp_lexer_commit_tokens
200   (cp_lexer *);
201 static void cp_lexer_rollback_tokens
202   (cp_lexer *);
203 #ifdef ENABLE_CHECKING
204 static void cp_lexer_print_token
205   (FILE *, cp_token *);
206 static inline bool cp_lexer_debugging_p
207   (cp_lexer *);
208 static void cp_lexer_start_debugging
209   (cp_lexer *) ATTRIBUTE_UNUSED;
210 static void cp_lexer_stop_debugging
211   (cp_lexer *) ATTRIBUTE_UNUSED;
212 #else
213 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
214    about passing NULL to functions that require non-NULL arguments
215    (fputs, fprintf).  It will never be used, so all we need is a value
216    of the right type that's guaranteed not to be NULL.  */
217 #define cp_lexer_debug_stream stdout
218 #define cp_lexer_print_token(str, tok) (void) 0
219 #define cp_lexer_debugging_p(lexer) 0
220 #endif /* ENABLE_CHECKING */
221
222 static cp_token_cache *cp_token_cache_new
223   (cp_token *, cp_token *);
224
225 static void cp_parser_initial_pragma
226   (cp_token *);
227
228 /* Manifest constants.  */
229 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
230 #define CP_SAVED_TOKEN_STACK 5
231
232 /* A token type for keywords, as opposed to ordinary identifiers.  */
233 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
234
235 /* A token type for template-ids.  If a template-id is processed while
236    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
237    the value of the CPP_TEMPLATE_ID is whatever was returned by
238    cp_parser_template_id.  */
239 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
240
241 /* A token type for nested-name-specifiers.  If a
242    nested-name-specifier is processed while parsing tentatively, it is
243    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
244    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
245    cp_parser_nested_name_specifier_opt.  */
246 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
247
248 /* A token type for tokens that are not tokens at all; these are used
249    to represent slots in the array where there used to be a token
250    that has now been deleted.  */
251 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
252
253 /* The number of token types, including C++-specific ones.  */
254 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
255
256 /* Variables.  */
257
258 #ifdef ENABLE_CHECKING
259 /* The stream to which debugging output should be written.  */
260 static FILE *cp_lexer_debug_stream;
261 #endif /* ENABLE_CHECKING */
262
263 /* Create a new main C++ lexer, the lexer that gets tokens from the
264    preprocessor.  */
265
266 static cp_lexer *
267 cp_lexer_new_main (void)
268 {
269   cp_token first_token;
270   cp_lexer *lexer;
271   cp_token *pos;
272   size_t alloc;
273   size_t space;
274   cp_token *buffer;
275
276   /* It's possible that parsing the first pragma will load a PCH file,
277      which is a GC collection point.  So we have to do that before
278      allocating any memory.  */
279   cp_parser_initial_pragma (&first_token);
280
281   /* Tell c_lex_with_flags not to merge string constants.  */
282   c_lex_return_raw_strings = true;
283
284   c_common_no_more_pch ();
285
286   /* Allocate the memory.  */
287   lexer = GGC_CNEW (cp_lexer);
288
289 #ifdef ENABLE_CHECKING
290   /* Initially we are not debugging.  */
291   lexer->debugging_p = false;
292 #endif /* ENABLE_CHECKING */
293   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
294                                    CP_SAVED_TOKEN_STACK);
295
296   /* Create the buffer.  */
297   alloc = CP_LEXER_BUFFER_SIZE;
298   buffer = GGC_NEWVEC (cp_token, alloc);
299
300   /* Put the first token in the buffer.  */
301   space = alloc;
302   pos = buffer;
303   *pos = first_token;
304
305   /* Get the remaining tokens from the preprocessor.  */
306   while (pos->type != CPP_EOF)
307     {
308       pos++;
309       if (!--space)
310         {
311           space = alloc;
312           alloc *= 2;
313           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
314           pos = buffer + space;
315         }
316       cp_lexer_get_preprocessor_token (lexer, pos);
317     }
318   lexer->buffer = buffer;
319   lexer->buffer_length = alloc - space;
320   lexer->last_token = pos;
321   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
322
323   /* Subsequent preprocessor diagnostics should use compiler
324      diagnostic functions to get the compiler source location.  */
325   cpp_get_options (parse_in)->client_diagnostic = true;
326   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
327
328   gcc_assert (lexer->next_token->type != CPP_PURGED);
329   return lexer;
330 }
331
332 /* Create a new lexer whose token stream is primed with the tokens in
333    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
334
335 static cp_lexer *
336 cp_lexer_new_from_tokens (cp_token_cache *cache)
337 {
338   cp_token *first = cache->first;
339   cp_token *last = cache->last;
340   cp_lexer *lexer = GGC_CNEW (cp_lexer);
341
342   /* We do not own the buffer.  */
343   lexer->buffer = NULL;
344   lexer->buffer_length = 0;
345   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
346   lexer->last_token = last;
347
348   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
349                                    CP_SAVED_TOKEN_STACK);
350
351 #ifdef ENABLE_CHECKING
352   /* Initially we are not debugging.  */
353   lexer->debugging_p = false;
354 #endif
355
356   gcc_assert (lexer->next_token->type != CPP_PURGED);
357   return lexer;
358 }
359
360 /* Frees all resources associated with LEXER.  */
361
362 static void
363 cp_lexer_destroy (cp_lexer *lexer)
364 {
365   if (lexer->buffer)
366     ggc_free (lexer->buffer);
367   VEC_free (cp_token_position, heap, lexer->saved_tokens);
368   ggc_free (lexer);
369 }
370
371 /* Returns nonzero if debugging information should be output.  */
372
373 #ifdef ENABLE_CHECKING
374
375 static inline bool
376 cp_lexer_debugging_p (cp_lexer *lexer)
377 {
378   return lexer->debugging_p;
379 }
380
381 #endif /* ENABLE_CHECKING */
382
383 static inline cp_token_position
384 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
385 {
386   gcc_assert (!previous_p || lexer->next_token != &eof_token);
387
388   return lexer->next_token - previous_p;
389 }
390
391 static inline cp_token *
392 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
393 {
394   return pos;
395 }
396
397 /* nonzero if we are presently saving tokens.  */
398
399 static inline int
400 cp_lexer_saving_tokens (const cp_lexer* lexer)
401 {
402   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
403 }
404
405 /* Store the next token from the preprocessor in *TOKEN.  Return true
406    if we reach EOF.  */
407
408 static void
409 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
410                                  cp_token *token)
411 {
412   static int is_extern_c = 0;
413
414    /* Get a new token from the preprocessor.  */
415   token->type
416     = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
417   token->input_file_stack_index = input_file_stack_tick;
418   token->keyword = RID_MAX;
419   token->pragma_kind = PRAGMA_NONE;
420   token->in_system_header = in_system_header;
421
422   /* On some systems, some header files are surrounded by an
423      implicit extern "C" block.  Set a flag in the token if it
424      comes from such a header.  */
425   is_extern_c += pending_lang_change;
426   pending_lang_change = 0;
427   token->implicit_extern_c = is_extern_c > 0;
428
429   /* Check to see if this token is a keyword.  */
430   if (token->type == CPP_NAME)
431     {
432       if (C_IS_RESERVED_WORD (token->u.value))
433         {
434           /* Mark this token as a keyword.  */
435           token->type = CPP_KEYWORD;
436           /* Record which keyword.  */
437           token->keyword = C_RID_CODE (token->u.value);
438           /* Update the value.  Some keywords are mapped to particular
439              entities, rather than simply having the value of the
440              corresponding IDENTIFIER_NODE.  For example, `__const' is
441              mapped to `const'.  */
442           token->u.value = ridpointers[token->keyword];
443         }
444       else
445         {
446           token->ambiguous_p = false;
447           token->keyword = RID_MAX;
448         }
449     }
450   /* Handle Objective-C++ keywords.  */
451   else if (token->type == CPP_AT_NAME)
452     {
453       token->type = CPP_KEYWORD;
454       switch (C_RID_CODE (token->u.value))
455         {
456         /* Map 'class' to '@class', 'private' to '@private', etc.  */
457         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
458         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
459         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
460         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
461         case RID_THROW: token->keyword = RID_AT_THROW; break;
462         case RID_TRY: token->keyword = RID_AT_TRY; break;
463         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
464         default: token->keyword = C_RID_CODE (token->u.value);
465         }
466     }
467   else if (token->type == CPP_PRAGMA)
468     {
469       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
470       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
471       token->u.value = NULL_TREE;
472     }
473 }
474
475 /* Update the globals input_location and in_system_header and the
476    input file stack from TOKEN.  */
477 static inline void
478 cp_lexer_set_source_position_from_token (cp_token *token)
479 {
480   if (token->type != CPP_EOF)
481     {
482       input_location = token->location;
483       in_system_header = token->in_system_header;
484       restore_input_file_stack (token->input_file_stack_index);
485     }
486 }
487
488 /* Return a pointer to the next token in the token stream, but do not
489    consume it.  */
490
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
493 {
494   if (cp_lexer_debugging_p (lexer))
495     {
496       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498       putc ('\n', cp_lexer_debug_stream);
499     }
500   return lexer->next_token;
501 }
502
503 /* Return true if the next token has the indicated TYPE.  */
504
505 static inline bool
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 {
508   return cp_lexer_peek_token (lexer)->type == type;
509 }
510
511 /* Return true if the next token does not have the indicated TYPE.  */
512
513 static inline bool
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 {
516   return !cp_lexer_next_token_is (lexer, type);
517 }
518
519 /* Return true if the next token is the indicated KEYWORD.  */
520
521 static inline bool
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 {
524   return cp_lexer_peek_token (lexer)->keyword == keyword;
525 }
526
527 /* Return true if the next token is a keyword for a decl-specifier.  */
528
529 static bool
530 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
531 {
532   cp_token *token;
533
534   token = cp_lexer_peek_token (lexer);
535   switch (token->keyword) 
536     {
537       /* Storage classes.  */
538     case RID_AUTO:
539     case RID_REGISTER:
540     case RID_STATIC:
541     case RID_EXTERN:
542     case RID_MUTABLE:
543     case RID_THREAD:
544       /* Elaborated type specifiers.  */
545     case RID_ENUM:
546     case RID_CLASS:
547     case RID_STRUCT:
548     case RID_UNION:
549     case RID_TYPENAME:
550       /* Simple type specifiers.  */
551     case RID_CHAR:
552     case RID_WCHAR:
553     case RID_BOOL:
554     case RID_SHORT:
555     case RID_INT:
556     case RID_LONG:
557     case RID_SIGNED:
558     case RID_UNSIGNED:
559     case RID_FLOAT:
560     case RID_DOUBLE:
561     case RID_VOID:
562       /* GNU extensions.  */ 
563     case RID_ATTRIBUTE:
564     case RID_TYPEOF:
565       return true;
566
567     default:
568       return false;
569     }
570 }
571
572 /* Return a pointer to the Nth token in the token stream.  If N is 1,
573    then this is precisely equivalent to cp_lexer_peek_token (except
574    that it is not inline).  One would like to disallow that case, but
575    there is one case (cp_parser_nth_token_starts_template_id) where
576    the caller passes a variable for N and it might be 1.  */
577
578 static cp_token *
579 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
580 {
581   cp_token *token;
582
583   /* N is 1-based, not zero-based.  */
584   gcc_assert (n > 0);
585
586   if (cp_lexer_debugging_p (lexer))
587     fprintf (cp_lexer_debug_stream,
588              "cp_lexer: peeking ahead %ld at token: ", (long)n);
589
590   --n;
591   token = lexer->next_token;
592   gcc_assert (!n || token != &eof_token);
593   while (n != 0)
594     {
595       ++token;
596       if (token == lexer->last_token)
597         {
598           token = (cp_token *)&eof_token;
599           break;
600         }
601
602       if (token->type != CPP_PURGED)
603         --n;
604     }
605
606   if (cp_lexer_debugging_p (lexer))
607     {
608       cp_lexer_print_token (cp_lexer_debug_stream, token);
609       putc ('\n', cp_lexer_debug_stream);
610     }
611
612   return token;
613 }
614
615 /* Return the next token, and advance the lexer's next_token pointer
616    to point to the next non-purged token.  */
617
618 static cp_token *
619 cp_lexer_consume_token (cp_lexer* lexer)
620 {
621   cp_token *token = lexer->next_token;
622
623   gcc_assert (token != &eof_token);
624   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
625
626   do
627     {
628       lexer->next_token++;
629       if (lexer->next_token == lexer->last_token)
630         {
631           lexer->next_token = (cp_token *)&eof_token;
632           break;
633         }
634
635     }
636   while (lexer->next_token->type == CPP_PURGED);
637
638   cp_lexer_set_source_position_from_token (token);
639
640   /* Provide debugging output.  */
641   if (cp_lexer_debugging_p (lexer))
642     {
643       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
644       cp_lexer_print_token (cp_lexer_debug_stream, token);
645       putc ('\n', cp_lexer_debug_stream);
646     }
647
648   return token;
649 }
650
651 /* Permanently remove the next token from the token stream, and
652    advance the next_token pointer to refer to the next non-purged
653    token.  */
654
655 static void
656 cp_lexer_purge_token (cp_lexer *lexer)
657 {
658   cp_token *tok = lexer->next_token;
659
660   gcc_assert (tok != &eof_token);
661   tok->type = CPP_PURGED;
662   tok->location = UNKNOWN_LOCATION;
663   tok->u.value = NULL_TREE;
664   tok->keyword = RID_MAX;
665
666   do
667     {
668       tok++;
669       if (tok == lexer->last_token)
670         {
671           tok = (cp_token *)&eof_token;
672           break;
673         }
674     }
675   while (tok->type == CPP_PURGED);
676   lexer->next_token = tok;
677 }
678
679 /* Permanently remove all tokens after TOK, up to, but not
680    including, the token that will be returned next by
681    cp_lexer_peek_token.  */
682
683 static void
684 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
685 {
686   cp_token *peek = lexer->next_token;
687
688   if (peek == &eof_token)
689     peek = lexer->last_token;
690
691   gcc_assert (tok < peek);
692
693   for ( tok += 1; tok != peek; tok += 1)
694     {
695       tok->type = CPP_PURGED;
696       tok->location = UNKNOWN_LOCATION;
697       tok->u.value = NULL_TREE;
698       tok->keyword = RID_MAX;
699     }
700 }
701
702 /* Begin saving tokens.  All tokens consumed after this point will be
703    preserved.  */
704
705 static void
706 cp_lexer_save_tokens (cp_lexer* lexer)
707 {
708   /* Provide debugging output.  */
709   if (cp_lexer_debugging_p (lexer))
710     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
711
712   VEC_safe_push (cp_token_position, heap,
713                  lexer->saved_tokens, lexer->next_token);
714 }
715
716 /* Commit to the portion of the token stream most recently saved.  */
717
718 static void
719 cp_lexer_commit_tokens (cp_lexer* lexer)
720 {
721   /* Provide debugging output.  */
722   if (cp_lexer_debugging_p (lexer))
723     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
724
725   VEC_pop (cp_token_position, lexer->saved_tokens);
726 }
727
728 /* Return all tokens saved since the last call to cp_lexer_save_tokens
729    to the token stream.  Stop saving tokens.  */
730
731 static void
732 cp_lexer_rollback_tokens (cp_lexer* lexer)
733 {
734   /* Provide debugging output.  */
735   if (cp_lexer_debugging_p (lexer))
736     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
737
738   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
739 }
740
741 /* Print a representation of the TOKEN on the STREAM.  */
742
743 #ifdef ENABLE_CHECKING
744
745 static void
746 cp_lexer_print_token (FILE * stream, cp_token *token)
747 {
748   /* We don't use cpp_type2name here because the parser defines
749      a few tokens of its own.  */
750   static const char *const token_names[] = {
751     /* cpplib-defined token types */
752 #define OP(e, s) #e,
753 #define TK(e, s) #e,
754     TTYPE_TABLE
755 #undef OP
756 #undef TK
757     /* C++ parser token types - see "Manifest constants", above.  */
758     "KEYWORD",
759     "TEMPLATE_ID",
760     "NESTED_NAME_SPECIFIER",
761     "PURGED"
762   };
763
764   /* If we have a name for the token, print it out.  Otherwise, we
765      simply give the numeric code.  */
766   gcc_assert (token->type < ARRAY_SIZE(token_names));
767   fputs (token_names[token->type], stream);
768
769   /* For some tokens, print the associated data.  */
770   switch (token->type)
771     {
772     case CPP_KEYWORD:
773       /* Some keywords have a value that is not an IDENTIFIER_NODE.
774          For example, `struct' is mapped to an INTEGER_CST.  */
775       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
776         break;
777       /* else fall through */
778     case CPP_NAME:
779       fputs (IDENTIFIER_POINTER (token->u.value), stream);
780       break;
781
782     case CPP_STRING:
783     case CPP_WSTRING:
784       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
785       break;
786
787     default:
788       break;
789     }
790 }
791
792 /* Start emitting debugging information.  */
793
794 static void
795 cp_lexer_start_debugging (cp_lexer* lexer)
796 {
797   lexer->debugging_p = true;
798 }
799
800 /* Stop emitting debugging information.  */
801
802 static void
803 cp_lexer_stop_debugging (cp_lexer* lexer)
804 {
805   lexer->debugging_p = false;
806 }
807
808 #endif /* ENABLE_CHECKING */
809
810 /* Create a new cp_token_cache, representing a range of tokens.  */
811
812 static cp_token_cache *
813 cp_token_cache_new (cp_token *first, cp_token *last)
814 {
815   cp_token_cache *cache = GGC_NEW (cp_token_cache);
816   cache->first = first;
817   cache->last = last;
818   return cache;
819 }
820
821 \f
822 /* Decl-specifiers.  */
823
824 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
825
826 static void
827 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
828 {
829   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
830 }
831
832 /* Declarators.  */
833
834 /* Nothing other than the parser should be creating declarators;
835    declarators are a semi-syntactic representation of C++ entities.
836    Other parts of the front end that need to create entities (like
837    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
838
839 static cp_declarator *make_call_declarator
840   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
841 static cp_declarator *make_array_declarator
842   (cp_declarator *, tree);
843 static cp_declarator *make_pointer_declarator
844   (cp_cv_quals, cp_declarator *);
845 static cp_declarator *make_reference_declarator
846   (cp_cv_quals, cp_declarator *);
847 static cp_parameter_declarator *make_parameter_declarator
848   (cp_decl_specifier_seq *, cp_declarator *, tree);
849 static cp_declarator *make_ptrmem_declarator
850   (cp_cv_quals, tree, cp_declarator *);
851
852 /* An erroneous declarator.  */
853 static cp_declarator *cp_error_declarator;
854
855 /* The obstack on which declarators and related data structures are
856    allocated.  */
857 static struct obstack declarator_obstack;
858
859 /* Alloc BYTES from the declarator memory pool.  */
860
861 static inline void *
862 alloc_declarator (size_t bytes)
863 {
864   return obstack_alloc (&declarator_obstack, bytes);
865 }
866
867 /* Allocate a declarator of the indicated KIND.  Clear fields that are
868    common to all declarators.  */
869
870 static cp_declarator *
871 make_declarator (cp_declarator_kind kind)
872 {
873   cp_declarator *declarator;
874
875   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
876   declarator->kind = kind;
877   declarator->attributes = NULL_TREE;
878   declarator->declarator = NULL;
879
880   return declarator;
881 }
882
883 /* Make a declarator for a generalized identifier.  If
884    QUALIFYING_SCOPE is non-NULL, the identifier is
885    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
886    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
887    is, if any.   */
888
889 static cp_declarator *
890 make_id_declarator (tree qualifying_scope, tree unqualified_name,
891                     special_function_kind sfk)
892 {
893   cp_declarator *declarator;
894
895   /* It is valid to write:
896
897        class C { void f(); };
898        typedef C D;
899        void D::f();
900
901      The standard is not clear about whether `typedef const C D' is
902      legal; as of 2002-09-15 the committee is considering that
903      question.  EDG 3.0 allows that syntax.  Therefore, we do as
904      well.  */
905   if (qualifying_scope && TYPE_P (qualifying_scope))
906     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
907
908   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
909               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
910               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
911
912   declarator = make_declarator (cdk_id);
913   declarator->u.id.qualifying_scope = qualifying_scope;
914   declarator->u.id.unqualified_name = unqualified_name;
915   declarator->u.id.sfk = sfk;
916
917   return declarator;
918 }
919
920 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
921    of modifiers such as const or volatile to apply to the pointer
922    type, represented as identifiers.  */
923
924 cp_declarator *
925 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
926 {
927   cp_declarator *declarator;
928
929   declarator = make_declarator (cdk_pointer);
930   declarator->declarator = target;
931   declarator->u.pointer.qualifiers = cv_qualifiers;
932   declarator->u.pointer.class_type = NULL_TREE;
933
934   return declarator;
935 }
936
937 /* Like make_pointer_declarator -- but for references.  */
938
939 cp_declarator *
940 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941 {
942   cp_declarator *declarator;
943
944   declarator = make_declarator (cdk_reference);
945   declarator->declarator = target;
946   declarator->u.pointer.qualifiers = cv_qualifiers;
947   declarator->u.pointer.class_type = NULL_TREE;
948
949   return declarator;
950 }
951
952 /* Like make_pointer_declarator -- but for a pointer to a non-static
953    member of CLASS_TYPE.  */
954
955 cp_declarator *
956 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
957                         cp_declarator *pointee)
958 {
959   cp_declarator *declarator;
960
961   declarator = make_declarator (cdk_ptrmem);
962   declarator->declarator = pointee;
963   declarator->u.pointer.qualifiers = cv_qualifiers;
964   declarator->u.pointer.class_type = class_type;
965
966   return declarator;
967 }
968
969 /* Make a declarator for the function given by TARGET, with the
970    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
971    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
972    indicates what exceptions can be thrown.  */
973
974 cp_declarator *
975 make_call_declarator (cp_declarator *target,
976                       cp_parameter_declarator *parms,
977                       cp_cv_quals cv_qualifiers,
978                       tree exception_specification)
979 {
980   cp_declarator *declarator;
981
982   declarator = make_declarator (cdk_function);
983   declarator->declarator = target;
984   declarator->u.function.parameters = parms;
985   declarator->u.function.qualifiers = cv_qualifiers;
986   declarator->u.function.exception_specification = exception_specification;
987
988   return declarator;
989 }
990
991 /* Make a declarator for an array of BOUNDS elements, each of which is
992    defined by ELEMENT.  */
993
994 cp_declarator *
995 make_array_declarator (cp_declarator *element, tree bounds)
996 {
997   cp_declarator *declarator;
998
999   declarator = make_declarator (cdk_array);
1000   declarator->declarator = element;
1001   declarator->u.array.bounds = bounds;
1002
1003   return declarator;
1004 }
1005
1006 cp_parameter_declarator *no_parameters;
1007
1008 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1009    DECLARATOR and DEFAULT_ARGUMENT.  */
1010
1011 cp_parameter_declarator *
1012 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1013                            cp_declarator *declarator,
1014                            tree default_argument)
1015 {
1016   cp_parameter_declarator *parameter;
1017
1018   parameter = ((cp_parameter_declarator *)
1019                alloc_declarator (sizeof (cp_parameter_declarator)));
1020   parameter->next = NULL;
1021   if (decl_specifiers)
1022     parameter->decl_specifiers = *decl_specifiers;
1023   else
1024     clear_decl_specs (&parameter->decl_specifiers);
1025   parameter->declarator = declarator;
1026   parameter->default_argument = default_argument;
1027   parameter->ellipsis_p = false;
1028
1029   return parameter;
1030 }
1031
1032 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1033
1034 static bool
1035 function_declarator_p (const cp_declarator *declarator)
1036 {
1037   while (declarator)
1038     {
1039       if (declarator->kind == cdk_function
1040           && declarator->declarator->kind == cdk_id)
1041         return true;
1042       if (declarator->kind == cdk_id
1043           || declarator->kind == cdk_error)
1044         return false;
1045       declarator = declarator->declarator;
1046     }
1047   return false;
1048 }
1049  
1050 /* The parser.  */
1051
1052 /* Overview
1053    --------
1054
1055    A cp_parser parses the token stream as specified by the C++
1056    grammar.  Its job is purely parsing, not semantic analysis.  For
1057    example, the parser breaks the token stream into declarators,
1058    expressions, statements, and other similar syntactic constructs.
1059    It does not check that the types of the expressions on either side
1060    of an assignment-statement are compatible, or that a function is
1061    not declared with a parameter of type `void'.
1062
1063    The parser invokes routines elsewhere in the compiler to perform
1064    semantic analysis and to build up the abstract syntax tree for the
1065    code processed.
1066
1067    The parser (and the template instantiation code, which is, in a
1068    way, a close relative of parsing) are the only parts of the
1069    compiler that should be calling push_scope and pop_scope, or
1070    related functions.  The parser (and template instantiation code)
1071    keeps track of what scope is presently active; everything else
1072    should simply honor that.  (The code that generates static
1073    initializers may also need to set the scope, in order to check
1074    access control correctly when emitting the initializers.)
1075
1076    Methodology
1077    -----------
1078
1079    The parser is of the standard recursive-descent variety.  Upcoming
1080    tokens in the token stream are examined in order to determine which
1081    production to use when parsing a non-terminal.  Some C++ constructs
1082    require arbitrary look ahead to disambiguate.  For example, it is
1083    impossible, in the general case, to tell whether a statement is an
1084    expression or declaration without scanning the entire statement.
1085    Therefore, the parser is capable of "parsing tentatively."  When the
1086    parser is not sure what construct comes next, it enters this mode.
1087    Then, while we attempt to parse the construct, the parser queues up
1088    error messages, rather than issuing them immediately, and saves the
1089    tokens it consumes.  If the construct is parsed successfully, the
1090    parser "commits", i.e., it issues any queued error messages and
1091    the tokens that were being preserved are permanently discarded.
1092    If, however, the construct is not parsed successfully, the parser
1093    rolls back its state completely so that it can resume parsing using
1094    a different alternative.
1095
1096    Future Improvements
1097    -------------------
1098
1099    The performance of the parser could probably be improved substantially.
1100    We could often eliminate the need to parse tentatively by looking ahead
1101    a little bit.  In some places, this approach might not entirely eliminate
1102    the need to parse tentatively, but it might still speed up the average
1103    case.  */
1104
1105 /* Flags that are passed to some parsing functions.  These values can
1106    be bitwise-ored together.  */
1107
1108 typedef enum cp_parser_flags
1109 {
1110   /* No flags.  */
1111   CP_PARSER_FLAGS_NONE = 0x0,
1112   /* The construct is optional.  If it is not present, then no error
1113      should be issued.  */
1114   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1115   /* When parsing a type-specifier, do not allow user-defined types.  */
1116   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1117 } cp_parser_flags;
1118
1119 /* The different kinds of declarators we want to parse.  */
1120
1121 typedef enum cp_parser_declarator_kind
1122 {
1123   /* We want an abstract declarator.  */
1124   CP_PARSER_DECLARATOR_ABSTRACT,
1125   /* We want a named declarator.  */
1126   CP_PARSER_DECLARATOR_NAMED,
1127   /* We don't mind, but the name must be an unqualified-id.  */
1128   CP_PARSER_DECLARATOR_EITHER
1129 } cp_parser_declarator_kind;
1130
1131 /* The precedence values used to parse binary expressions.  The minimum value
1132    of PREC must be 1, because zero is reserved to quickly discriminate
1133    binary operators from other tokens.  */
1134
1135 enum cp_parser_prec
1136 {
1137   PREC_NOT_OPERATOR,
1138   PREC_LOGICAL_OR_EXPRESSION,
1139   PREC_LOGICAL_AND_EXPRESSION,
1140   PREC_INCLUSIVE_OR_EXPRESSION,
1141   PREC_EXCLUSIVE_OR_EXPRESSION,
1142   PREC_AND_EXPRESSION,
1143   PREC_EQUALITY_EXPRESSION,
1144   PREC_RELATIONAL_EXPRESSION,
1145   PREC_SHIFT_EXPRESSION,
1146   PREC_ADDITIVE_EXPRESSION,
1147   PREC_MULTIPLICATIVE_EXPRESSION,
1148   PREC_PM_EXPRESSION,
1149   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1150 };
1151
1152 /* A mapping from a token type to a corresponding tree node type, with a
1153    precedence value.  */
1154
1155 typedef struct cp_parser_binary_operations_map_node
1156 {
1157   /* The token type.  */
1158   enum cpp_ttype token_type;
1159   /* The corresponding tree code.  */
1160   enum tree_code tree_type;
1161   /* The precedence of this operator.  */
1162   enum cp_parser_prec prec;
1163 } cp_parser_binary_operations_map_node;
1164
1165 /* The status of a tentative parse.  */
1166
1167 typedef enum cp_parser_status_kind
1168 {
1169   /* No errors have occurred.  */
1170   CP_PARSER_STATUS_KIND_NO_ERROR,
1171   /* An error has occurred.  */
1172   CP_PARSER_STATUS_KIND_ERROR,
1173   /* We are committed to this tentative parse, whether or not an error
1174      has occurred.  */
1175   CP_PARSER_STATUS_KIND_COMMITTED
1176 } cp_parser_status_kind;
1177
1178 typedef struct cp_parser_expression_stack_entry
1179 {
1180   /* Left hand side of the binary operation we are currently
1181      parsing.  */
1182   tree lhs;
1183   /* Original tree code for left hand side, if it was a binary
1184      expression itself (used for -Wparentheses).  */
1185   enum tree_code lhs_type;
1186   /* Tree code for the binary operation we are parsing.  */
1187   enum tree_code tree_type;
1188   /* Precedence of the binary operation we are parsing.  */
1189   int prec;
1190 } cp_parser_expression_stack_entry;
1191
1192 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1193    entries because precedence levels on the stack are monotonically
1194    increasing.  */
1195 typedef struct cp_parser_expression_stack_entry
1196   cp_parser_expression_stack[NUM_PREC_VALUES];
1197
1198 /* Context that is saved and restored when parsing tentatively.  */
1199 typedef struct cp_parser_context GTY (())
1200 {
1201   /* If this is a tentative parsing context, the status of the
1202      tentative parse.  */
1203   enum cp_parser_status_kind status;
1204   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1205      that are looked up in this context must be looked up both in the
1206      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1207      the context of the containing expression.  */
1208   tree object_type;
1209
1210   /* The next parsing context in the stack.  */
1211   struct cp_parser_context *next;
1212 } cp_parser_context;
1213
1214 /* Prototypes.  */
1215
1216 /* Constructors and destructors.  */
1217
1218 static cp_parser_context *cp_parser_context_new
1219   (cp_parser_context *);
1220
1221 /* Class variables.  */
1222
1223 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1224
1225 /* The operator-precedence table used by cp_parser_binary_expression.
1226    Transformed into an associative array (binops_by_token) by
1227    cp_parser_new.  */
1228
1229 static const cp_parser_binary_operations_map_node binops[] = {
1230   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1231   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1232
1233   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1234   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1235   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1236
1237   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1238   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1239
1240   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1241   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1242
1243   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1244   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1245   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1246   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1247
1248   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1249   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1250
1251   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1252
1253   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1254
1255   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1256
1257   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1258
1259   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1260 };
1261
1262 /* The same as binops, but initialized by cp_parser_new so that
1263    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1264    for speed.  */
1265 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1266
1267 /* Constructors and destructors.  */
1268
1269 /* Construct a new context.  The context below this one on the stack
1270    is given by NEXT.  */
1271
1272 static cp_parser_context *
1273 cp_parser_context_new (cp_parser_context* next)
1274 {
1275   cp_parser_context *context;
1276
1277   /* Allocate the storage.  */
1278   if (cp_parser_context_free_list != NULL)
1279     {
1280       /* Pull the first entry from the free list.  */
1281       context = cp_parser_context_free_list;
1282       cp_parser_context_free_list = context->next;
1283       memset (context, 0, sizeof (*context));
1284     }
1285   else
1286     context = GGC_CNEW (cp_parser_context);
1287
1288   /* No errors have occurred yet in this context.  */
1289   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1290   /* If this is not the bottomost context, copy information that we
1291      need from the previous context.  */
1292   if (next)
1293     {
1294       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1295          expression, then we are parsing one in this context, too.  */
1296       context->object_type = next->object_type;
1297       /* Thread the stack.  */
1298       context->next = next;
1299     }
1300
1301   return context;
1302 }
1303
1304 /* The cp_parser structure represents the C++ parser.  */
1305
1306 typedef struct cp_parser GTY(())
1307 {
1308   /* The lexer from which we are obtaining tokens.  */
1309   cp_lexer *lexer;
1310
1311   /* The scope in which names should be looked up.  If NULL_TREE, then
1312      we look up names in the scope that is currently open in the
1313      source program.  If non-NULL, this is either a TYPE or
1314      NAMESPACE_DECL for the scope in which we should look.  It can
1315      also be ERROR_MARK, when we've parsed a bogus scope.
1316
1317      This value is not cleared automatically after a name is looked
1318      up, so we must be careful to clear it before starting a new look
1319      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1320      will look up `Z' in the scope of `X', rather than the current
1321      scope.)  Unfortunately, it is difficult to tell when name lookup
1322      is complete, because we sometimes peek at a token, look it up,
1323      and then decide not to consume it.   */
1324   tree scope;
1325
1326   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1327      last lookup took place.  OBJECT_SCOPE is used if an expression
1328      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1329      respectively.  QUALIFYING_SCOPE is used for an expression of the
1330      form "X::Y"; it refers to X.  */
1331   tree object_scope;
1332   tree qualifying_scope;
1333
1334   /* A stack of parsing contexts.  All but the bottom entry on the
1335      stack will be tentative contexts.
1336
1337      We parse tentatively in order to determine which construct is in
1338      use in some situations.  For example, in order to determine
1339      whether a statement is an expression-statement or a
1340      declaration-statement we parse it tentatively as a
1341      declaration-statement.  If that fails, we then reparse the same
1342      token stream as an expression-statement.  */
1343   cp_parser_context *context;
1344
1345   /* True if we are parsing GNU C++.  If this flag is not set, then
1346      GNU extensions are not recognized.  */
1347   bool allow_gnu_extensions_p;
1348
1349   /* TRUE if the `>' token should be interpreted as the greater-than
1350      operator.  FALSE if it is the end of a template-id or
1351      template-parameter-list.  */
1352   bool greater_than_is_operator_p;
1353
1354   /* TRUE if default arguments are allowed within a parameter list
1355      that starts at this point. FALSE if only a gnu extension makes
1356      them permissible.  */
1357   bool default_arg_ok_p;
1358
1359   /* TRUE if we are parsing an integral constant-expression.  See
1360      [expr.const] for a precise definition.  */
1361   bool integral_constant_expression_p;
1362
1363   /* TRUE if we are parsing an integral constant-expression -- but a
1364      non-constant expression should be permitted as well.  This flag
1365      is used when parsing an array bound so that GNU variable-length
1366      arrays are tolerated.  */
1367   bool allow_non_integral_constant_expression_p;
1368
1369   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1370      been seen that makes the expression non-constant.  */
1371   bool non_integral_constant_expression_p;
1372
1373   /* TRUE if local variable names and `this' are forbidden in the
1374      current context.  */
1375   bool local_variables_forbidden_p;
1376
1377   /* TRUE if the declaration we are parsing is part of a
1378      linkage-specification of the form `extern string-literal
1379      declaration'.  */
1380   bool in_unbraced_linkage_specification_p;
1381
1382   /* TRUE if we are presently parsing a declarator, after the
1383      direct-declarator.  */
1384   bool in_declarator_p;
1385
1386   /* TRUE if we are presently parsing a template-argument-list.  */
1387   bool in_template_argument_list_p;
1388
1389   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1390      to IN_OMP_BLOCK if parsing OpenMP structured block and
1391      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1392      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1393      iteration-statement, OpenMP block or loop within that switch.  */
1394 #define IN_SWITCH_STMT          1
1395 #define IN_ITERATION_STMT       2
1396 #define IN_OMP_BLOCK            4
1397 #define IN_OMP_FOR              8
1398   unsigned char in_statement;
1399
1400   /* TRUE if we are presently parsing the body of a switch statement.
1401      Note that this doesn't quite overlap with in_statement above.
1402      The difference relates to giving the right sets of error messages:
1403      "case not in switch" vs "break statement used with OpenMP...".  */
1404   bool in_switch_statement_p;
1405
1406   /* TRUE if we are parsing a type-id in an expression context.  In
1407      such a situation, both "type (expr)" and "type (type)" are valid
1408      alternatives.  */
1409   bool in_type_id_in_expr_p;
1410
1411   /* TRUE if we are currently in a header file where declarations are
1412      implicitly extern "C".  */
1413   bool implicit_extern_c;
1414
1415   /* TRUE if strings in expressions should be translated to the execution
1416      character set.  */
1417   bool translate_strings_p;
1418
1419   /* TRUE if we are presently parsing the body of a function, but not
1420      a local class.  */
1421   bool in_function_body;
1422
1423   /* If non-NULL, then we are parsing a construct where new type
1424      definitions are not permitted.  The string stored here will be
1425      issued as an error message if a type is defined.  */
1426   const char *type_definition_forbidden_message;
1427
1428   /* A list of lists. The outer list is a stack, used for member
1429      functions of local classes. At each level there are two sub-list,
1430      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1431      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1432      TREE_VALUE's. The functions are chained in reverse declaration
1433      order.
1434
1435      The TREE_PURPOSE sublist contains those functions with default
1436      arguments that need post processing, and the TREE_VALUE sublist
1437      contains those functions with definitions that need post
1438      processing.
1439
1440      These lists can only be processed once the outermost class being
1441      defined is complete.  */
1442   tree unparsed_functions_queues;
1443
1444   /* The number of classes whose definitions are currently in
1445      progress.  */
1446   unsigned num_classes_being_defined;
1447
1448   /* The number of template parameter lists that apply directly to the
1449      current declaration.  */
1450   unsigned num_template_parameter_lists;
1451 } cp_parser;
1452
1453 /* Prototypes.  */
1454
1455 /* Constructors and destructors.  */
1456
1457 static cp_parser *cp_parser_new
1458   (void);
1459
1460 /* Routines to parse various constructs.
1461
1462    Those that return `tree' will return the error_mark_node (rather
1463    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1464    Sometimes, they will return an ordinary node if error-recovery was
1465    attempted, even though a parse error occurred.  So, to check
1466    whether or not a parse error occurred, you should always use
1467    cp_parser_error_occurred.  If the construct is optional (indicated
1468    either by an `_opt' in the name of the function that does the
1469    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1470    the construct is not present.  */
1471
1472 /* Lexical conventions [gram.lex]  */
1473
1474 static tree cp_parser_identifier
1475   (cp_parser *);
1476 static tree cp_parser_string_literal
1477   (cp_parser *, bool, bool);
1478
1479 /* Basic concepts [gram.basic]  */
1480
1481 static bool cp_parser_translation_unit
1482   (cp_parser *);
1483
1484 /* Expressions [gram.expr]  */
1485
1486 static tree cp_parser_primary_expression
1487   (cp_parser *, bool, bool, bool, cp_id_kind *);
1488 static tree cp_parser_id_expression
1489   (cp_parser *, bool, bool, bool *, bool, bool);
1490 static tree cp_parser_unqualified_id
1491   (cp_parser *, bool, bool, bool, bool);
1492 static tree cp_parser_nested_name_specifier_opt
1493   (cp_parser *, bool, bool, bool, bool);
1494 static tree cp_parser_nested_name_specifier
1495   (cp_parser *, bool, bool, bool, bool);
1496 static tree cp_parser_class_or_namespace_name
1497   (cp_parser *, bool, bool, bool, bool, bool);
1498 static tree cp_parser_postfix_expression
1499   (cp_parser *, bool, bool);
1500 static tree cp_parser_postfix_open_square_expression
1501   (cp_parser *, tree, bool);
1502 static tree cp_parser_postfix_dot_deref_expression
1503   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1504 static tree cp_parser_parenthesized_expression_list
1505   (cp_parser *, bool, bool, bool *);
1506 static void cp_parser_pseudo_destructor_name
1507   (cp_parser *, tree *, tree *);
1508 static tree cp_parser_unary_expression
1509   (cp_parser *, bool, bool);
1510 static enum tree_code cp_parser_unary_operator
1511   (cp_token *);
1512 static tree cp_parser_new_expression
1513   (cp_parser *);
1514 static tree cp_parser_new_placement
1515   (cp_parser *);
1516 static tree cp_parser_new_type_id
1517   (cp_parser *, tree *);
1518 static cp_declarator *cp_parser_new_declarator_opt
1519   (cp_parser *);
1520 static cp_declarator *cp_parser_direct_new_declarator
1521   (cp_parser *);
1522 static tree cp_parser_new_initializer
1523   (cp_parser *);
1524 static tree cp_parser_delete_expression
1525   (cp_parser *);
1526 static tree cp_parser_cast_expression
1527   (cp_parser *, bool, bool);
1528 static tree cp_parser_binary_expression
1529   (cp_parser *, bool);
1530 static tree cp_parser_question_colon_clause
1531   (cp_parser *, tree);
1532 static tree cp_parser_assignment_expression
1533   (cp_parser *, bool);
1534 static enum tree_code cp_parser_assignment_operator_opt
1535   (cp_parser *);
1536 static tree cp_parser_expression
1537   (cp_parser *, bool);
1538 static tree cp_parser_constant_expression
1539   (cp_parser *, bool, bool *);
1540 static tree cp_parser_builtin_offsetof
1541   (cp_parser *);
1542
1543 /* Statements [gram.stmt.stmt]  */
1544
1545 static void cp_parser_statement
1546   (cp_parser *, tree, bool, bool *);
1547 static void cp_parser_label_for_labeled_statement
1548   (cp_parser *);
1549 static tree cp_parser_expression_statement
1550   (cp_parser *, tree);
1551 static tree cp_parser_compound_statement
1552   (cp_parser *, tree, bool);
1553 static void cp_parser_statement_seq_opt
1554   (cp_parser *, tree);
1555 static tree cp_parser_selection_statement
1556   (cp_parser *, bool *);
1557 static tree cp_parser_condition
1558   (cp_parser *);
1559 static tree cp_parser_iteration_statement
1560   (cp_parser *);
1561 static void cp_parser_for_init_statement
1562   (cp_parser *);
1563 static tree cp_parser_jump_statement
1564   (cp_parser *);
1565 static void cp_parser_declaration_statement
1566   (cp_parser *);
1567
1568 static tree cp_parser_implicitly_scoped_statement
1569   (cp_parser *, bool *);
1570 static void cp_parser_already_scoped_statement
1571   (cp_parser *);
1572
1573 /* Declarations [gram.dcl.dcl] */
1574
1575 static void cp_parser_declaration_seq_opt
1576   (cp_parser *);
1577 static void cp_parser_declaration
1578   (cp_parser *);
1579 static void cp_parser_block_declaration
1580   (cp_parser *, bool);
1581 static void cp_parser_simple_declaration
1582   (cp_parser *, bool);
1583 static void cp_parser_decl_specifier_seq
1584   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1585 static tree cp_parser_storage_class_specifier_opt
1586   (cp_parser *);
1587 static tree cp_parser_function_specifier_opt
1588   (cp_parser *, cp_decl_specifier_seq *);
1589 static tree cp_parser_type_specifier
1590   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1591    int *, bool *);
1592 static tree cp_parser_simple_type_specifier
1593   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1594 static tree cp_parser_type_name
1595   (cp_parser *);
1596 static tree cp_parser_elaborated_type_specifier
1597   (cp_parser *, bool, bool);
1598 static tree cp_parser_enum_specifier
1599   (cp_parser *);
1600 static void cp_parser_enumerator_list
1601   (cp_parser *, tree);
1602 static void cp_parser_enumerator_definition
1603   (cp_parser *, tree);
1604 static tree cp_parser_namespace_name
1605   (cp_parser *);
1606 static void cp_parser_namespace_definition
1607   (cp_parser *);
1608 static void cp_parser_namespace_body
1609   (cp_parser *);
1610 static tree cp_parser_qualified_namespace_specifier
1611   (cp_parser *);
1612 static void cp_parser_namespace_alias_definition
1613   (cp_parser *);
1614 static bool cp_parser_using_declaration
1615   (cp_parser *, bool);
1616 static void cp_parser_using_directive
1617   (cp_parser *);
1618 static void cp_parser_asm_definition
1619   (cp_parser *);
1620 static void cp_parser_linkage_specification
1621   (cp_parser *);
1622 static void cp_parser_static_assert
1623   (cp_parser *, bool);
1624
1625 /* Declarators [gram.dcl.decl] */
1626
1627 static tree cp_parser_init_declarator
1628   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1629 static cp_declarator *cp_parser_declarator
1630   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1631 static cp_declarator *cp_parser_direct_declarator
1632   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1633 static enum tree_code cp_parser_ptr_operator
1634   (cp_parser *, tree *, cp_cv_quals *);
1635 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1636   (cp_parser *);
1637 static tree cp_parser_declarator_id
1638   (cp_parser *, bool);
1639 static tree cp_parser_type_id
1640   (cp_parser *);
1641 static void cp_parser_type_specifier_seq
1642   (cp_parser *, bool, cp_decl_specifier_seq *);
1643 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1644   (cp_parser *);
1645 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1646   (cp_parser *, bool *);
1647 static cp_parameter_declarator *cp_parser_parameter_declaration
1648   (cp_parser *, bool, bool *);
1649 static void cp_parser_function_body
1650   (cp_parser *);
1651 static tree cp_parser_initializer
1652   (cp_parser *, bool *, bool *);
1653 static tree cp_parser_initializer_clause
1654   (cp_parser *, bool *);
1655 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1656   (cp_parser *, bool *);
1657
1658 static bool cp_parser_ctor_initializer_opt_and_function_body
1659   (cp_parser *);
1660
1661 /* Classes [gram.class] */
1662
1663 static tree cp_parser_class_name
1664   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1665 static tree cp_parser_class_specifier
1666   (cp_parser *);
1667 static tree cp_parser_class_head
1668   (cp_parser *, bool *, tree *, tree *);
1669 static enum tag_types cp_parser_class_key
1670   (cp_parser *);
1671 static void cp_parser_member_specification_opt
1672   (cp_parser *);
1673 static void cp_parser_member_declaration
1674   (cp_parser *);
1675 static tree cp_parser_pure_specifier
1676   (cp_parser *);
1677 static tree cp_parser_constant_initializer
1678   (cp_parser *);
1679
1680 /* Derived classes [gram.class.derived] */
1681
1682 static tree cp_parser_base_clause
1683   (cp_parser *);
1684 static tree cp_parser_base_specifier
1685   (cp_parser *);
1686
1687 /* Special member functions [gram.special] */
1688
1689 static tree cp_parser_conversion_function_id
1690   (cp_parser *);
1691 static tree cp_parser_conversion_type_id
1692   (cp_parser *);
1693 static cp_declarator *cp_parser_conversion_declarator_opt
1694   (cp_parser *);
1695 static bool cp_parser_ctor_initializer_opt
1696   (cp_parser *);
1697 static void cp_parser_mem_initializer_list
1698   (cp_parser *);
1699 static tree cp_parser_mem_initializer
1700   (cp_parser *);
1701 static tree cp_parser_mem_initializer_id
1702   (cp_parser *);
1703
1704 /* Overloading [gram.over] */
1705
1706 static tree cp_parser_operator_function_id
1707   (cp_parser *);
1708 static tree cp_parser_operator
1709   (cp_parser *);
1710
1711 /* Templates [gram.temp] */
1712
1713 static void cp_parser_template_declaration
1714   (cp_parser *, bool);
1715 static tree cp_parser_template_parameter_list
1716   (cp_parser *);
1717 static tree cp_parser_template_parameter
1718   (cp_parser *, bool *);
1719 static tree cp_parser_type_parameter
1720   (cp_parser *);
1721 static tree cp_parser_template_id
1722   (cp_parser *, bool, bool, bool);
1723 static tree cp_parser_template_name
1724   (cp_parser *, bool, bool, bool, bool *);
1725 static tree cp_parser_template_argument_list
1726   (cp_parser *);
1727 static tree cp_parser_template_argument
1728   (cp_parser *);
1729 static void cp_parser_explicit_instantiation
1730   (cp_parser *);
1731 static void cp_parser_explicit_specialization
1732   (cp_parser *);
1733
1734 /* Exception handling [gram.exception] */
1735
1736 static tree cp_parser_try_block
1737   (cp_parser *);
1738 static bool cp_parser_function_try_block
1739   (cp_parser *);
1740 static void cp_parser_handler_seq
1741   (cp_parser *);
1742 static void cp_parser_handler
1743   (cp_parser *);
1744 static tree cp_parser_exception_declaration
1745   (cp_parser *);
1746 static tree cp_parser_throw_expression
1747   (cp_parser *);
1748 static tree cp_parser_exception_specification_opt
1749   (cp_parser *);
1750 static tree cp_parser_type_id_list
1751   (cp_parser *);
1752
1753 /* GNU Extensions */
1754
1755 static tree cp_parser_asm_specification_opt
1756   (cp_parser *);
1757 static tree cp_parser_asm_operand_list
1758   (cp_parser *);
1759 static tree cp_parser_asm_clobber_list
1760   (cp_parser *);
1761 static tree cp_parser_attributes_opt
1762   (cp_parser *);
1763 static tree cp_parser_attribute_list
1764   (cp_parser *);
1765 static bool cp_parser_extension_opt
1766   (cp_parser *, int *);
1767 static void cp_parser_label_declaration
1768   (cp_parser *);
1769
1770 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1771 static bool cp_parser_pragma
1772   (cp_parser *, enum pragma_context);
1773
1774 /* Objective-C++ Productions */
1775
1776 static tree cp_parser_objc_message_receiver
1777   (cp_parser *);
1778 static tree cp_parser_objc_message_args
1779   (cp_parser *);
1780 static tree cp_parser_objc_message_expression
1781   (cp_parser *);
1782 static tree cp_parser_objc_encode_expression
1783   (cp_parser *);
1784 static tree cp_parser_objc_defs_expression
1785   (cp_parser *);
1786 static tree cp_parser_objc_protocol_expression
1787   (cp_parser *);
1788 static tree cp_parser_objc_selector_expression
1789   (cp_parser *);
1790 static tree cp_parser_objc_expression
1791   (cp_parser *);
1792 static bool cp_parser_objc_selector_p
1793   (enum cpp_ttype);
1794 static tree cp_parser_objc_selector
1795   (cp_parser *);
1796 static tree cp_parser_objc_protocol_refs_opt
1797   (cp_parser *);
1798 static void cp_parser_objc_declaration
1799   (cp_parser *);
1800 static tree cp_parser_objc_statement
1801   (cp_parser *);
1802
1803 /* Utility Routines */
1804
1805 static tree cp_parser_lookup_name
1806   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1807 static tree cp_parser_lookup_name_simple
1808   (cp_parser *, tree);
1809 static tree cp_parser_maybe_treat_template_as_class
1810   (tree, bool);
1811 static bool cp_parser_check_declarator_template_parameters
1812   (cp_parser *, cp_declarator *);
1813 static bool cp_parser_check_template_parameters
1814   (cp_parser *, unsigned);
1815 static tree cp_parser_simple_cast_expression
1816   (cp_parser *);
1817 static tree cp_parser_global_scope_opt
1818   (cp_parser *, bool);
1819 static bool cp_parser_constructor_declarator_p
1820   (cp_parser *, bool);
1821 static tree cp_parser_function_definition_from_specifiers_and_declarator
1822   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1823 static tree cp_parser_function_definition_after_declarator
1824   (cp_parser *, bool);
1825 static void cp_parser_template_declaration_after_export
1826   (cp_parser *, bool);
1827 static void cp_parser_perform_template_parameter_access_checks
1828   (VEC (deferred_access_check,gc)*);
1829 static tree cp_parser_single_declaration
1830   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1831 static tree cp_parser_functional_cast
1832   (cp_parser *, tree);
1833 static tree cp_parser_save_member_function_body
1834   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1835 static tree cp_parser_enclosed_template_argument_list
1836   (cp_parser *);
1837 static void cp_parser_save_default_args
1838   (cp_parser *, tree);
1839 static void cp_parser_late_parsing_for_member
1840   (cp_parser *, tree);
1841 static void cp_parser_late_parsing_default_args
1842   (cp_parser *, tree);
1843 static tree cp_parser_sizeof_operand
1844   (cp_parser *, enum rid);
1845 static bool cp_parser_declares_only_class_p
1846   (cp_parser *);
1847 static void cp_parser_set_storage_class
1848   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1849 static void cp_parser_set_decl_spec_type
1850   (cp_decl_specifier_seq *, tree, bool);
1851 static bool cp_parser_friend_p
1852   (const cp_decl_specifier_seq *);
1853 static cp_token *cp_parser_require
1854   (cp_parser *, enum cpp_ttype, const char *);
1855 static cp_token *cp_parser_require_keyword
1856   (cp_parser *, enum rid, const char *);
1857 static bool cp_parser_token_starts_function_definition_p
1858   (cp_token *);
1859 static bool cp_parser_next_token_starts_class_definition_p
1860   (cp_parser *);
1861 static bool cp_parser_next_token_ends_template_argument_p
1862   (cp_parser *);
1863 static bool cp_parser_nth_token_starts_template_argument_list_p
1864   (cp_parser *, size_t);
1865 static enum tag_types cp_parser_token_is_class_key
1866   (cp_token *);
1867 static void cp_parser_check_class_key
1868   (enum tag_types, tree type);
1869 static void cp_parser_check_access_in_redeclaration
1870   (tree type);
1871 static bool cp_parser_optional_template_keyword
1872   (cp_parser *);
1873 static void cp_parser_pre_parsed_nested_name_specifier
1874   (cp_parser *);
1875 static void cp_parser_cache_group
1876   (cp_parser *, enum cpp_ttype, unsigned);
1877 static void cp_parser_parse_tentatively
1878   (cp_parser *);
1879 static void cp_parser_commit_to_tentative_parse
1880   (cp_parser *);
1881 static void cp_parser_abort_tentative_parse
1882   (cp_parser *);
1883 static bool cp_parser_parse_definitely
1884   (cp_parser *);
1885 static inline bool cp_parser_parsing_tentatively
1886   (cp_parser *);
1887 static bool cp_parser_uncommitted_to_tentative_parse_p
1888   (cp_parser *);
1889 static void cp_parser_error
1890   (cp_parser *, const char *);
1891 static void cp_parser_name_lookup_error
1892   (cp_parser *, tree, tree, const char *);
1893 static bool cp_parser_simulate_error
1894   (cp_parser *);
1895 static bool cp_parser_check_type_definition
1896   (cp_parser *);
1897 static void cp_parser_check_for_definition_in_return_type
1898   (cp_declarator *, tree);
1899 static void cp_parser_check_for_invalid_template_id
1900   (cp_parser *, tree);
1901 static bool cp_parser_non_integral_constant_expression
1902   (cp_parser *, const char *);
1903 static void cp_parser_diagnose_invalid_type_name
1904   (cp_parser *, tree, tree);
1905 static bool cp_parser_parse_and_diagnose_invalid_type_name
1906   (cp_parser *);
1907 static int cp_parser_skip_to_closing_parenthesis
1908   (cp_parser *, bool, bool, bool);
1909 static void cp_parser_skip_to_end_of_statement
1910   (cp_parser *);
1911 static void cp_parser_consume_semicolon_at_end_of_statement
1912   (cp_parser *);
1913 static void cp_parser_skip_to_end_of_block_or_statement
1914   (cp_parser *);
1915 static void cp_parser_skip_to_closing_brace
1916   (cp_parser *);
1917 static void cp_parser_skip_to_end_of_template_parameter_list
1918   (cp_parser *);
1919 static void cp_parser_skip_to_pragma_eol
1920   (cp_parser*, cp_token *);
1921 static bool cp_parser_error_occurred
1922   (cp_parser *);
1923 static bool cp_parser_allow_gnu_extensions_p
1924   (cp_parser *);
1925 static bool cp_parser_is_string_literal
1926   (cp_token *);
1927 static bool cp_parser_is_keyword
1928   (cp_token *, enum rid);
1929 static tree cp_parser_make_typename_type
1930   (cp_parser *, tree, tree);
1931
1932 /* Returns nonzero if we are parsing tentatively.  */
1933
1934 static inline bool
1935 cp_parser_parsing_tentatively (cp_parser* parser)
1936 {
1937   return parser->context->next != NULL;
1938 }
1939
1940 /* Returns nonzero if TOKEN is a string literal.  */
1941
1942 static bool
1943 cp_parser_is_string_literal (cp_token* token)
1944 {
1945   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1946 }
1947
1948 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1949
1950 static bool
1951 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1952 {
1953   return token->keyword == keyword;
1954 }
1955
1956 /* If not parsing tentatively, issue a diagnostic of the form
1957       FILE:LINE: MESSAGE before TOKEN
1958    where TOKEN is the next token in the input stream.  MESSAGE
1959    (specified by the caller) is usually of the form "expected
1960    OTHER-TOKEN".  */
1961
1962 static void
1963 cp_parser_error (cp_parser* parser, const char* message)
1964 {
1965   if (!cp_parser_simulate_error (parser))
1966     {
1967       cp_token *token = cp_lexer_peek_token (parser->lexer);
1968       /* This diagnostic makes more sense if it is tagged to the line
1969          of the token we just peeked at.  */
1970       cp_lexer_set_source_position_from_token (token);
1971
1972       if (token->type == CPP_PRAGMA)
1973         {
1974           error ("%<#pragma%> is not allowed here");
1975           cp_parser_skip_to_pragma_eol (parser, token);
1976           return;
1977         }
1978
1979       c_parse_error (message,
1980                      /* Because c_parser_error does not understand
1981                         CPP_KEYWORD, keywords are treated like
1982                         identifiers.  */
1983                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1984                      token->u.value);
1985     }
1986 }
1987
1988 /* Issue an error about name-lookup failing.  NAME is the
1989    IDENTIFIER_NODE DECL is the result of
1990    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1991    the thing that we hoped to find.  */
1992
1993 static void
1994 cp_parser_name_lookup_error (cp_parser* parser,
1995                              tree name,
1996                              tree decl,
1997                              const char* desired)
1998 {
1999   /* If name lookup completely failed, tell the user that NAME was not
2000      declared.  */
2001   if (decl == error_mark_node)
2002     {
2003       if (parser->scope && parser->scope != global_namespace)
2004         error ("%<%D::%D%> has not been declared",
2005                parser->scope, name);
2006       else if (parser->scope == global_namespace)
2007         error ("%<::%D%> has not been declared", name);
2008       else if (parser->object_scope
2009                && !CLASS_TYPE_P (parser->object_scope))
2010         error ("request for member %qD in non-class type %qT",
2011                name, parser->object_scope);
2012       else if (parser->object_scope)
2013         error ("%<%T::%D%> has not been declared",
2014                parser->object_scope, name);
2015       else
2016         error ("%qD has not been declared", name);
2017     }
2018   else if (parser->scope && parser->scope != global_namespace)
2019     error ("%<%D::%D%> %s", parser->scope, name, desired);
2020   else if (parser->scope == global_namespace)
2021     error ("%<::%D%> %s", name, desired);
2022   else
2023     error ("%qD %s", name, desired);
2024 }
2025
2026 /* If we are parsing tentatively, remember that an error has occurred
2027    during this tentative parse.  Returns true if the error was
2028    simulated; false if a message should be issued by the caller.  */
2029
2030 static bool
2031 cp_parser_simulate_error (cp_parser* parser)
2032 {
2033   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2034     {
2035       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2036       return true;
2037     }
2038   return false;
2039 }
2040
2041 /* Check for repeated decl-specifiers.  */
2042
2043 static void
2044 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2045 {
2046   cp_decl_spec ds;
2047
2048   for (ds = ds_first; ds != ds_last; ++ds)
2049     {
2050       unsigned count = decl_specs->specs[(int)ds];
2051       if (count < 2)
2052         continue;
2053       /* The "long" specifier is a special case because of "long long".  */
2054       if (ds == ds_long)
2055         {
2056           if (count > 2)
2057             error ("%<long long long%> is too long for GCC");
2058           else if (pedantic && !in_system_header && warn_long_long)
2059             pedwarn ("ISO C++ does not support %<long long%>");
2060         }
2061       else if (count > 1)
2062         {
2063           static const char *const decl_spec_names[] = {
2064             "signed",
2065             "unsigned",
2066             "short",
2067             "long",
2068             "const",
2069             "volatile",
2070             "restrict",
2071             "inline",
2072             "virtual",
2073             "explicit",
2074             "friend",
2075             "typedef",
2076             "__complex",
2077             "__thread"
2078           };
2079           error ("duplicate %qs", decl_spec_names[(int)ds]);
2080         }
2081     }
2082 }
2083
2084 /* This function is called when a type is defined.  If type
2085    definitions are forbidden at this point, an error message is
2086    issued.  */
2087
2088 static bool
2089 cp_parser_check_type_definition (cp_parser* parser)
2090 {
2091   /* If types are forbidden here, issue a message.  */
2092   if (parser->type_definition_forbidden_message)
2093     {
2094       /* Use `%s' to print the string in case there are any escape
2095          characters in the message.  */
2096       error ("%s", parser->type_definition_forbidden_message);
2097       return false;
2098     }
2099   return true;
2100 }
2101
2102 /* This function is called when the DECLARATOR is processed.  The TYPE
2103    was a type defined in the decl-specifiers.  If it is invalid to
2104    define a type in the decl-specifiers for DECLARATOR, an error is
2105    issued.  */
2106
2107 static void
2108 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2109                                                tree type)
2110 {
2111   /* [dcl.fct] forbids type definitions in return types.
2112      Unfortunately, it's not easy to know whether or not we are
2113      processing a return type until after the fact.  */
2114   while (declarator
2115          && (declarator->kind == cdk_pointer
2116              || declarator->kind == cdk_reference
2117              || declarator->kind == cdk_ptrmem))
2118     declarator = declarator->declarator;
2119   if (declarator
2120       && declarator->kind == cdk_function)
2121     {
2122       error ("new types may not be defined in a return type");
2123       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2124               type);
2125     }
2126 }
2127
2128 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2129    "<" in any valid C++ program.  If the next token is indeed "<",
2130    issue a message warning the user about what appears to be an
2131    invalid attempt to form a template-id.  */
2132
2133 static void
2134 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2135                                          tree type)
2136 {
2137   cp_token_position start = 0;
2138
2139   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2140     {
2141       if (TYPE_P (type))
2142         error ("%qT is not a template", type);
2143       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2144         error ("%qE is not a template", type);
2145       else
2146         error ("invalid template-id");
2147       /* Remember the location of the invalid "<".  */
2148       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2149         start = cp_lexer_token_position (parser->lexer, true);
2150       /* Consume the "<".  */
2151       cp_lexer_consume_token (parser->lexer);
2152       /* Parse the template arguments.  */
2153       cp_parser_enclosed_template_argument_list (parser);
2154       /* Permanently remove the invalid template arguments so that
2155          this error message is not issued again.  */
2156       if (start)
2157         cp_lexer_purge_tokens_after (parser->lexer, start);
2158     }
2159 }
2160
2161 /* If parsing an integral constant-expression, issue an error message
2162    about the fact that THING appeared and return true.  Otherwise,
2163    return false.  In either case, set
2164    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2165
2166 static bool
2167 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2168                                             const char *thing)
2169 {
2170   parser->non_integral_constant_expression_p = true;
2171   if (parser->integral_constant_expression_p)
2172     {
2173       if (!parser->allow_non_integral_constant_expression_p)
2174         {
2175           error ("%s cannot appear in a constant-expression", thing);
2176           return true;
2177         }
2178     }
2179   return false;
2180 }
2181
2182 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2183    qualifying scope (or NULL, if none) for ID.  This function commits
2184    to the current active tentative parse, if any.  (Otherwise, the
2185    problematic construct might be encountered again later, resulting
2186    in duplicate error messages.)  */
2187
2188 static void
2189 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2190 {
2191   tree decl, old_scope;
2192   /* Try to lookup the identifier.  */
2193   old_scope = parser->scope;
2194   parser->scope = scope;
2195   decl = cp_parser_lookup_name_simple (parser, id);
2196   parser->scope = old_scope;
2197   /* If the lookup found a template-name, it means that the user forgot
2198   to specify an argument list. Emit a useful error message.  */
2199   if (TREE_CODE (decl) == TEMPLATE_DECL)
2200     error ("invalid use of template-name %qE without an argument list", decl);
2201   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2202     error ("invalid use of destructor %qD as a type", id);
2203   else if (TREE_CODE (decl) == TYPE_DECL)
2204     /* Something like 'unsigned A a;'  */
2205     error ("invalid combination of multiple type-specifiers");
2206   else if (!parser->scope)
2207     {
2208       /* Issue an error message.  */
2209       error ("%qE does not name a type", id);
2210       /* If we're in a template class, it's possible that the user was
2211          referring to a type from a base class.  For example:
2212
2213            template <typename T> struct A { typedef T X; };
2214            template <typename T> struct B : public A<T> { X x; };
2215
2216          The user should have said "typename A<T>::X".  */
2217       if (processing_template_decl && current_class_type
2218           && TYPE_BINFO (current_class_type))
2219         {
2220           tree b;
2221
2222           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2223                b;
2224                b = TREE_CHAIN (b))
2225             {
2226               tree base_type = BINFO_TYPE (b);
2227               if (CLASS_TYPE_P (base_type)
2228                   && dependent_type_p (base_type))
2229                 {
2230                   tree field;
2231                   /* Go from a particular instantiation of the
2232                      template (which will have an empty TYPE_FIELDs),
2233                      to the main version.  */
2234                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2235                   for (field = TYPE_FIELDS (base_type);
2236                        field;
2237                        field = TREE_CHAIN (field))
2238                     if (TREE_CODE (field) == TYPE_DECL
2239                         && DECL_NAME (field) == id)
2240                       {
2241                         inform ("(perhaps %<typename %T::%E%> was intended)",
2242                                 BINFO_TYPE (b), id);
2243                         break;
2244                       }
2245                   if (field)
2246                     break;
2247                 }
2248             }
2249         }
2250     }
2251   /* Here we diagnose qualified-ids where the scope is actually correct,
2252      but the identifier does not resolve to a valid type name.  */
2253   else if (parser->scope != error_mark_node)
2254     {
2255       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2256         error ("%qE in namespace %qE does not name a type",
2257                id, parser->scope);
2258       else if (TYPE_P (parser->scope))
2259         error ("%qE in class %qT does not name a type", id, parser->scope);
2260       else
2261         gcc_unreachable ();
2262     }
2263   cp_parser_commit_to_tentative_parse (parser);
2264 }
2265
2266 /* Check for a common situation where a type-name should be present,
2267    but is not, and issue a sensible error message.  Returns true if an
2268    invalid type-name was detected.
2269
2270    The situation handled by this function are variable declarations of the
2271    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2272    Usually, `ID' should name a type, but if we got here it means that it
2273    does not. We try to emit the best possible error message depending on
2274    how exactly the id-expression looks like.  */
2275
2276 static bool
2277 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2278 {
2279   tree id;
2280
2281   cp_parser_parse_tentatively (parser);
2282   id = cp_parser_id_expression (parser,
2283                                 /*template_keyword_p=*/false,
2284                                 /*check_dependency_p=*/true,
2285                                 /*template_p=*/NULL,
2286                                 /*declarator_p=*/true,
2287                                 /*optional_p=*/false);
2288   /* After the id-expression, there should be a plain identifier,
2289      otherwise this is not a simple variable declaration. Also, if
2290      the scope is dependent, we cannot do much.  */
2291   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2292       || (parser->scope && TYPE_P (parser->scope)
2293           && dependent_type_p (parser->scope)))
2294     {
2295       cp_parser_abort_tentative_parse (parser);
2296       return false;
2297     }
2298   if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2299     return false;
2300
2301   /* Emit a diagnostic for the invalid type.  */
2302   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2303   /* Skip to the end of the declaration; there's no point in
2304      trying to process it.  */
2305   cp_parser_skip_to_end_of_block_or_statement (parser);
2306   return true;
2307 }
2308
2309 /* Consume tokens up to, and including, the next non-nested closing `)'.
2310    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2311    are doing error recovery. Returns -1 if OR_COMMA is true and we
2312    found an unnested comma.  */
2313
2314 static int
2315 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2316                                        bool recovering,
2317                                        bool or_comma,
2318                                        bool consume_paren)
2319 {
2320   unsigned paren_depth = 0;
2321   unsigned brace_depth = 0;
2322
2323   if (recovering && !or_comma
2324       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2325     return 0;
2326
2327   while (true)
2328     {
2329       cp_token * token = cp_lexer_peek_token (parser->lexer);
2330
2331       switch (token->type)
2332         {
2333         case CPP_EOF:
2334         case CPP_PRAGMA_EOL:
2335           /* If we've run out of tokens, then there is no closing `)'.  */
2336           return 0;
2337
2338         case CPP_SEMICOLON:
2339           /* This matches the processing in skip_to_end_of_statement.  */
2340           if (!brace_depth)
2341             return 0;
2342           break;
2343
2344         case CPP_OPEN_BRACE:
2345           ++brace_depth;
2346           break;
2347         case CPP_CLOSE_BRACE:
2348           if (!brace_depth--)
2349             return 0;
2350           break;
2351
2352         case CPP_COMMA:
2353           if (recovering && or_comma && !brace_depth && !paren_depth)
2354             return -1;
2355           break;
2356
2357         case CPP_OPEN_PAREN:
2358           if (!brace_depth)
2359             ++paren_depth;
2360           break;
2361
2362         case CPP_CLOSE_PAREN:
2363           if (!brace_depth && !paren_depth--)
2364             {
2365               if (consume_paren)
2366                 cp_lexer_consume_token (parser->lexer);
2367               return 1;
2368             }
2369           break;
2370
2371         default:
2372           break;
2373         }
2374
2375       /* Consume the token.  */
2376       cp_lexer_consume_token (parser->lexer);
2377     }
2378 }
2379
2380 /* Consume tokens until we reach the end of the current statement.
2381    Normally, that will be just before consuming a `;'.  However, if a
2382    non-nested `}' comes first, then we stop before consuming that.  */
2383
2384 static void
2385 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2386 {
2387   unsigned nesting_depth = 0;
2388
2389   while (true)
2390     {
2391       cp_token *token = cp_lexer_peek_token (parser->lexer);
2392
2393       switch (token->type)
2394         {
2395         case CPP_EOF:
2396         case CPP_PRAGMA_EOL:
2397           /* If we've run out of tokens, stop.  */
2398           return;
2399
2400         case CPP_SEMICOLON:
2401           /* If the next token is a `;', we have reached the end of the
2402              statement.  */
2403           if (!nesting_depth)
2404             return;
2405           break;
2406
2407         case CPP_CLOSE_BRACE:
2408           /* If this is a non-nested '}', stop before consuming it.
2409              That way, when confronted with something like:
2410
2411                { 3 + }
2412
2413              we stop before consuming the closing '}', even though we
2414              have not yet reached a `;'.  */
2415           if (nesting_depth == 0)
2416             return;
2417
2418           /* If it is the closing '}' for a block that we have
2419              scanned, stop -- but only after consuming the token.
2420              That way given:
2421
2422                 void f g () { ... }
2423                 typedef int I;
2424
2425              we will stop after the body of the erroneously declared
2426              function, but before consuming the following `typedef'
2427              declaration.  */
2428           if (--nesting_depth == 0)
2429             {
2430               cp_lexer_consume_token (parser->lexer);
2431               return;
2432             }
2433
2434         case CPP_OPEN_BRACE:
2435           ++nesting_depth;
2436           break;
2437
2438         default:
2439           break;
2440         }
2441
2442       /* Consume the token.  */
2443       cp_lexer_consume_token (parser->lexer);
2444     }
2445 }
2446
2447 /* This function is called at the end of a statement or declaration.
2448    If the next token is a semicolon, it is consumed; otherwise, error
2449    recovery is attempted.  */
2450
2451 static void
2452 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2453 {
2454   /* Look for the trailing `;'.  */
2455   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2456     {
2457       /* If there is additional (erroneous) input, skip to the end of
2458          the statement.  */
2459       cp_parser_skip_to_end_of_statement (parser);
2460       /* If the next token is now a `;', consume it.  */
2461       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2462         cp_lexer_consume_token (parser->lexer);
2463     }
2464 }
2465
2466 /* Skip tokens until we have consumed an entire block, or until we
2467    have consumed a non-nested `;'.  */
2468
2469 static void
2470 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2471 {
2472   int nesting_depth = 0;
2473
2474   while (nesting_depth >= 0)
2475     {
2476       cp_token *token = cp_lexer_peek_token (parser->lexer);
2477
2478       switch (token->type)
2479         {
2480         case CPP_EOF:
2481         case CPP_PRAGMA_EOL:
2482           /* If we've run out of tokens, stop.  */
2483           return;
2484
2485         case CPP_SEMICOLON:
2486           /* Stop if this is an unnested ';'. */
2487           if (!nesting_depth)
2488             nesting_depth = -1;
2489           break;
2490
2491         case CPP_CLOSE_BRACE:
2492           /* Stop if this is an unnested '}', or closes the outermost
2493              nesting level.  */
2494           nesting_depth--;
2495           if (!nesting_depth)
2496             nesting_depth = -1;
2497           break;
2498
2499         case CPP_OPEN_BRACE:
2500           /* Nest. */
2501           nesting_depth++;
2502           break;
2503
2504         default:
2505           break;
2506         }
2507
2508       /* Consume the token.  */
2509       cp_lexer_consume_token (parser->lexer);
2510     }
2511 }
2512
2513 /* Skip tokens until a non-nested closing curly brace is the next
2514    token.  */
2515
2516 static void
2517 cp_parser_skip_to_closing_brace (cp_parser *parser)
2518 {
2519   unsigned nesting_depth = 0;
2520
2521   while (true)
2522     {
2523       cp_token *token = cp_lexer_peek_token (parser->lexer);
2524
2525       switch (token->type)
2526         {
2527         case CPP_EOF:
2528         case CPP_PRAGMA_EOL:
2529           /* If we've run out of tokens, stop.  */
2530           return;
2531
2532         case CPP_CLOSE_BRACE:
2533           /* If the next token is a non-nested `}', then we have reached
2534              the end of the current block.  */
2535           if (nesting_depth-- == 0)
2536             return;
2537           break;
2538
2539         case CPP_OPEN_BRACE:
2540           /* If it the next token is a `{', then we are entering a new
2541              block.  Consume the entire block.  */
2542           ++nesting_depth;
2543           break;
2544
2545         default:
2546           break;
2547         }
2548
2549       /* Consume the token.  */
2550       cp_lexer_consume_token (parser->lexer);
2551     }
2552 }
2553
2554 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2555    parameter is the PRAGMA token, allowing us to purge the entire pragma
2556    sequence.  */
2557
2558 static void
2559 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2560 {
2561   cp_token *token;
2562
2563   parser->lexer->in_pragma = false;
2564
2565   do
2566     token = cp_lexer_consume_token (parser->lexer);
2567   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2568
2569   /* Ensure that the pragma is not parsed again.  */
2570   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2571 }
2572
2573 /* Require pragma end of line, resyncing with it as necessary.  The
2574    arguments are as for cp_parser_skip_to_pragma_eol.  */
2575
2576 static void
2577 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2578 {
2579   parser->lexer->in_pragma = false;
2580   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2581     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2582 }
2583
2584 /* This is a simple wrapper around make_typename_type. When the id is
2585    an unresolved identifier node, we can provide a superior diagnostic
2586    using cp_parser_diagnose_invalid_type_name.  */
2587
2588 static tree
2589 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2590 {
2591   tree result;
2592   if (TREE_CODE (id) == IDENTIFIER_NODE)
2593     {
2594       result = make_typename_type (scope, id, typename_type,
2595                                    /*complain=*/tf_none);
2596       if (result == error_mark_node)
2597         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2598       return result;
2599     }
2600   return make_typename_type (scope, id, typename_type, tf_error);
2601 }
2602
2603
2604 /* Create a new C++ parser.  */
2605
2606 static cp_parser *
2607 cp_parser_new (void)
2608 {
2609   cp_parser *parser;
2610   cp_lexer *lexer;
2611   unsigned i;
2612
2613   /* cp_lexer_new_main is called before calling ggc_alloc because
2614      cp_lexer_new_main might load a PCH file.  */
2615   lexer = cp_lexer_new_main ();
2616
2617   /* Initialize the binops_by_token so that we can get the tree
2618      directly from the token.  */
2619   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2620     binops_by_token[binops[i].token_type] = binops[i];
2621
2622   parser = GGC_CNEW (cp_parser);
2623   parser->lexer = lexer;
2624   parser->context = cp_parser_context_new (NULL);
2625
2626   /* For now, we always accept GNU extensions.  */
2627   parser->allow_gnu_extensions_p = 1;
2628
2629   /* The `>' token is a greater-than operator, not the end of a
2630      template-id.  */
2631   parser->greater_than_is_operator_p = true;
2632
2633   parser->default_arg_ok_p = true;
2634
2635   /* We are not parsing a constant-expression.  */
2636   parser->integral_constant_expression_p = false;
2637   parser->allow_non_integral_constant_expression_p = false;
2638   parser->non_integral_constant_expression_p = false;
2639
2640   /* Local variable names are not forbidden.  */
2641   parser->local_variables_forbidden_p = false;
2642
2643   /* We are not processing an `extern "C"' declaration.  */
2644   parser->in_unbraced_linkage_specification_p = false;
2645
2646   /* We are not processing a declarator.  */
2647   parser->in_declarator_p = false;
2648
2649   /* We are not processing a template-argument-list.  */
2650   parser->in_template_argument_list_p = false;
2651
2652   /* We are not in an iteration statement.  */
2653   parser->in_statement = 0;
2654
2655   /* We are not in a switch statement.  */
2656   parser->in_switch_statement_p = false;
2657
2658   /* We are not parsing a type-id inside an expression.  */
2659   parser->in_type_id_in_expr_p = false;
2660
2661   /* Declarations aren't implicitly extern "C".  */
2662   parser->implicit_extern_c = false;
2663
2664   /* String literals should be translated to the execution character set.  */
2665   parser->translate_strings_p = true;
2666
2667   /* We are not parsing a function body.  */
2668   parser->in_function_body = false;
2669
2670   /* The unparsed function queue is empty.  */
2671   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2672
2673   /* There are no classes being defined.  */
2674   parser->num_classes_being_defined = 0;
2675
2676   /* No template parameters apply.  */
2677   parser->num_template_parameter_lists = 0;
2678
2679   return parser;
2680 }
2681
2682 /* Create a cp_lexer structure which will emit the tokens in CACHE
2683    and push it onto the parser's lexer stack.  This is used for delayed
2684    parsing of in-class method bodies and default arguments, and should
2685    not be confused with tentative parsing.  */
2686 static void
2687 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2688 {
2689   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2690   lexer->next = parser->lexer;
2691   parser->lexer = lexer;
2692
2693   /* Move the current source position to that of the first token in the
2694      new lexer.  */
2695   cp_lexer_set_source_position_from_token (lexer->next_token);
2696 }
2697
2698 /* Pop the top lexer off the parser stack.  This is never used for the
2699    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2700 static void
2701 cp_parser_pop_lexer (cp_parser *parser)
2702 {
2703   cp_lexer *lexer = parser->lexer;
2704   parser->lexer = lexer->next;
2705   cp_lexer_destroy (lexer);
2706
2707   /* Put the current source position back where it was before this
2708      lexer was pushed.  */
2709   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2710 }
2711
2712 /* Lexical conventions [gram.lex]  */
2713
2714 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2715    identifier.  */
2716
2717 static tree
2718 cp_parser_identifier (cp_parser* parser)
2719 {
2720   cp_token *token;
2721
2722   /* Look for the identifier.  */
2723   token = cp_parser_require (parser, CPP_NAME, "identifier");
2724   /* Return the value.  */
2725   return token ? token->u.value : error_mark_node;
2726 }
2727
2728 /* Parse a sequence of adjacent string constants.  Returns a
2729    TREE_STRING representing the combined, nul-terminated string
2730    constant.  If TRANSLATE is true, translate the string to the
2731    execution character set.  If WIDE_OK is true, a wide string is
2732    invalid here.
2733
2734    C++98 [lex.string] says that if a narrow string literal token is
2735    adjacent to a wide string literal token, the behavior is undefined.
2736    However, C99 6.4.5p4 says that this results in a wide string literal.
2737    We follow C99 here, for consistency with the C front end.
2738
2739    This code is largely lifted from lex_string() in c-lex.c.
2740
2741    FUTURE: ObjC++ will need to handle @-strings here.  */
2742 static tree
2743 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2744 {
2745   tree value;
2746   bool wide = false;
2747   size_t count;
2748   struct obstack str_ob;
2749   cpp_string str, istr, *strs;
2750   cp_token *tok;
2751
2752   tok = cp_lexer_peek_token (parser->lexer);
2753   if (!cp_parser_is_string_literal (tok))
2754     {
2755       cp_parser_error (parser, "expected string-literal");
2756       return error_mark_node;
2757     }
2758
2759   /* Try to avoid the overhead of creating and destroying an obstack
2760      for the common case of just one string.  */
2761   if (!cp_parser_is_string_literal
2762       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2763     {
2764       cp_lexer_consume_token (parser->lexer);
2765
2766       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2767       str.len = TREE_STRING_LENGTH (tok->u.value);
2768       count = 1;
2769       if (tok->type == CPP_WSTRING)
2770         wide = true;
2771
2772       strs = &str;
2773     }
2774   else
2775     {
2776       gcc_obstack_init (&str_ob);
2777       count = 0;
2778
2779       do
2780         {
2781           cp_lexer_consume_token (parser->lexer);
2782           count++;
2783           str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2784           str.len = TREE_STRING_LENGTH (tok->u.value);
2785           if (tok->type == CPP_WSTRING)
2786             wide = true;
2787
2788           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2789
2790           tok = cp_lexer_peek_token (parser->lexer);
2791         }
2792       while (cp_parser_is_string_literal (tok));
2793
2794       strs = (cpp_string *) obstack_finish (&str_ob);
2795     }
2796
2797   if (wide && !wide_ok)
2798     {
2799       cp_parser_error (parser, "a wide string is invalid in this context");
2800       wide = false;
2801     }
2802
2803   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2804       (parse_in, strs, count, &istr, wide))
2805     {
2806       value = build_string (istr.len, (char *)istr.text);
2807       free ((void *)istr.text);
2808
2809       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2810       value = fix_string_type (value);
2811     }
2812   else
2813     /* cpp_interpret_string has issued an error.  */
2814     value = error_mark_node;
2815
2816   if (count > 1)
2817     obstack_free (&str_ob, 0);
2818
2819   return value;
2820 }
2821
2822
2823 /* Basic concepts [gram.basic]  */
2824
2825 /* Parse a translation-unit.
2826
2827    translation-unit:
2828      declaration-seq [opt]
2829
2830    Returns TRUE if all went well.  */
2831
2832 static bool
2833 cp_parser_translation_unit (cp_parser* parser)
2834 {
2835   /* The address of the first non-permanent object on the declarator
2836      obstack.  */
2837   static void *declarator_obstack_base;
2838
2839   bool success;
2840
2841   /* Create the declarator obstack, if necessary.  */
2842   if (!cp_error_declarator)
2843     {
2844       gcc_obstack_init (&declarator_obstack);
2845       /* Create the error declarator.  */
2846       cp_error_declarator = make_declarator (cdk_error);
2847       /* Create the empty parameter list.  */
2848       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2849       /* Remember where the base of the declarator obstack lies.  */
2850       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2851     }
2852
2853   cp_parser_declaration_seq_opt (parser);
2854
2855   /* If there are no tokens left then all went well.  */
2856   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2857     {
2858       /* Get rid of the token array; we don't need it any more.  */
2859       cp_lexer_destroy (parser->lexer);
2860       parser->lexer = NULL;
2861
2862       /* This file might have been a context that's implicitly extern
2863          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2864       if (parser->implicit_extern_c)
2865         {
2866           pop_lang_context ();
2867           parser->implicit_extern_c = false;
2868         }
2869
2870       /* Finish up.  */
2871       finish_translation_unit ();
2872
2873       success = true;
2874     }
2875   else
2876     {
2877       cp_parser_error (parser, "expected declaration");
2878       success = false;
2879     }
2880
2881   /* Make sure the declarator obstack was fully cleaned up.  */
2882   gcc_assert (obstack_next_free (&declarator_obstack)
2883               == declarator_obstack_base);
2884
2885   /* All went well.  */
2886   return success;
2887 }
2888
2889 /* Expressions [gram.expr] */
2890
2891 /* Parse a primary-expression.
2892
2893    primary-expression:
2894      literal
2895      this
2896      ( expression )
2897      id-expression
2898
2899    GNU Extensions:
2900
2901    primary-expression:
2902      ( compound-statement )
2903      __builtin_va_arg ( assignment-expression , type-id )
2904      __builtin_offsetof ( type-id , offsetof-expression )
2905
2906    Objective-C++ Extension:
2907
2908    primary-expression:
2909      objc-expression
2910
2911    literal:
2912      __null
2913
2914    ADDRESS_P is true iff this expression was immediately preceded by
2915    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2916    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2917    true iff this expression is a template argument.
2918
2919    Returns a representation of the expression.  Upon return, *IDK
2920    indicates what kind of id-expression (if any) was present.  */
2921
2922 static tree
2923 cp_parser_primary_expression (cp_parser *parser,
2924                               bool address_p,
2925                               bool cast_p,
2926                               bool template_arg_p,
2927                               cp_id_kind *idk)
2928 {
2929   cp_token *token;
2930
2931   /* Assume the primary expression is not an id-expression.  */
2932   *idk = CP_ID_KIND_NONE;
2933
2934   /* Peek at the next token.  */
2935   token = cp_lexer_peek_token (parser->lexer);
2936   switch (token->type)
2937     {
2938       /* literal:
2939            integer-literal
2940            character-literal
2941            floating-literal
2942            string-literal
2943            boolean-literal  */
2944     case CPP_CHAR:
2945     case CPP_WCHAR:
2946     case CPP_NUMBER:
2947       token = cp_lexer_consume_token (parser->lexer);
2948       /* Floating-point literals are only allowed in an integral
2949          constant expression if they are cast to an integral or
2950          enumeration type.  */
2951       if (TREE_CODE (token->u.value) == REAL_CST
2952           && parser->integral_constant_expression_p
2953           && pedantic)
2954         {
2955           /* CAST_P will be set even in invalid code like "int(2.7 +
2956              ...)".   Therefore, we have to check that the next token
2957              is sure to end the cast.  */
2958           if (cast_p)
2959             {
2960               cp_token *next_token;
2961
2962               next_token = cp_lexer_peek_token (parser->lexer);
2963               if (/* The comma at the end of an
2964                      enumerator-definition.  */
2965                   next_token->type != CPP_COMMA
2966                   /* The curly brace at the end of an enum-specifier.  */
2967                   && next_token->type != CPP_CLOSE_BRACE
2968                   /* The end of a statement.  */
2969                   && next_token->type != CPP_SEMICOLON
2970                   /* The end of the cast-expression.  */
2971                   && next_token->type != CPP_CLOSE_PAREN
2972                   /* The end of an array bound.  */
2973                   && next_token->type != CPP_CLOSE_SQUARE
2974                   /* The closing ">" in a template-argument-list.  */
2975                   && (next_token->type != CPP_GREATER
2976                       || parser->greater_than_is_operator_p))
2977                 cast_p = false;
2978             }
2979
2980           /* If we are within a cast, then the constraint that the
2981              cast is to an integral or enumeration type will be
2982              checked at that point.  If we are not within a cast, then
2983              this code is invalid.  */
2984           if (!cast_p)
2985             cp_parser_non_integral_constant_expression
2986               (parser, "floating-point literal");
2987         }
2988       return token->u.value;
2989
2990     case CPP_STRING:
2991     case CPP_WSTRING:
2992       /* ??? Should wide strings be allowed when parser->translate_strings_p
2993          is false (i.e. in attributes)?  If not, we can kill the third
2994          argument to cp_parser_string_literal.  */
2995       return cp_parser_string_literal (parser,
2996                                        parser->translate_strings_p,
2997                                        true);
2998
2999     case CPP_OPEN_PAREN:
3000       {
3001         tree expr;
3002         bool saved_greater_than_is_operator_p;
3003
3004         /* Consume the `('.  */
3005         cp_lexer_consume_token (parser->lexer);
3006         /* Within a parenthesized expression, a `>' token is always
3007            the greater-than operator.  */
3008         saved_greater_than_is_operator_p
3009           = parser->greater_than_is_operator_p;
3010         parser->greater_than_is_operator_p = true;
3011         /* If we see `( { ' then we are looking at the beginning of
3012            a GNU statement-expression.  */
3013         if (cp_parser_allow_gnu_extensions_p (parser)
3014             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3015           {
3016             /* Statement-expressions are not allowed by the standard.  */
3017             if (pedantic)
3018               pedwarn ("ISO C++ forbids braced-groups within expressions");
3019
3020             /* And they're not allowed outside of a function-body; you
3021                cannot, for example, write:
3022
3023                  int i = ({ int j = 3; j + 1; });
3024
3025                at class or namespace scope.  */
3026             if (!parser->in_function_body)
3027               error ("statement-expressions are allowed only inside functions");
3028             /* Start the statement-expression.  */
3029             expr = begin_stmt_expr ();
3030             /* Parse the compound-statement.  */
3031             cp_parser_compound_statement (parser, expr, false);
3032             /* Finish up.  */
3033             expr = finish_stmt_expr (expr, false);
3034           }
3035         else
3036           {
3037             /* Parse the parenthesized expression.  */
3038             expr = cp_parser_expression (parser, cast_p);
3039             /* Let the front end know that this expression was
3040                enclosed in parentheses. This matters in case, for
3041                example, the expression is of the form `A::B', since
3042                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3043                not.  */
3044             finish_parenthesized_expr (expr);
3045           }
3046         /* The `>' token might be the end of a template-id or
3047            template-parameter-list now.  */
3048         parser->greater_than_is_operator_p
3049           = saved_greater_than_is_operator_p;
3050         /* Consume the `)'.  */
3051         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3052           cp_parser_skip_to_end_of_statement (parser);
3053
3054         return expr;
3055       }
3056
3057     case CPP_KEYWORD:
3058       switch (token->keyword)
3059         {
3060           /* These two are the boolean literals.  */
3061         case RID_TRUE:
3062           cp_lexer_consume_token (parser->lexer);
3063           return boolean_true_node;
3064         case RID_FALSE:
3065           cp_lexer_consume_token (parser->lexer);
3066           return boolean_false_node;
3067
3068           /* The `__null' literal.  */
3069         case RID_NULL:
3070           cp_lexer_consume_token (parser->lexer);
3071           return null_node;
3072
3073           /* Recognize the `this' keyword.  */
3074         case RID_THIS:
3075           cp_lexer_consume_token (parser->lexer);
3076           if (parser->local_variables_forbidden_p)
3077             {
3078               error ("%<this%> may not be used in this context");
3079               return error_mark_node;
3080             }
3081           /* Pointers cannot appear in constant-expressions.  */
3082           if (cp_parser_non_integral_constant_expression (parser,
3083                                                           "`this'"))
3084             return error_mark_node;
3085           return finish_this_expr ();
3086
3087           /* The `operator' keyword can be the beginning of an
3088              id-expression.  */
3089         case RID_OPERATOR:
3090           goto id_expression;
3091
3092         case RID_FUNCTION_NAME:
3093         case RID_PRETTY_FUNCTION_NAME:
3094         case RID_C99_FUNCTION_NAME:
3095           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3096              __func__ are the names of variables -- but they are
3097              treated specially.  Therefore, they are handled here,
3098              rather than relying on the generic id-expression logic
3099              below.  Grammatically, these names are id-expressions.
3100
3101              Consume the token.  */
3102           token = cp_lexer_consume_token (parser->lexer);
3103           /* Look up the name.  */
3104           return finish_fname (token->u.value);
3105
3106         case RID_VA_ARG:
3107           {
3108             tree expression;
3109             tree type;
3110
3111             /* The `__builtin_va_arg' construct is used to handle
3112                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3113             cp_lexer_consume_token (parser->lexer);
3114             /* Look for the opening `('.  */
3115             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3116             /* Now, parse the assignment-expression.  */
3117             expression = cp_parser_assignment_expression (parser,
3118                                                           /*cast_p=*/false);
3119             /* Look for the `,'.  */
3120             cp_parser_require (parser, CPP_COMMA, "`,'");
3121             /* Parse the type-id.  */
3122             type = cp_parser_type_id (parser);
3123             /* Look for the closing `)'.  */
3124             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3125             /* Using `va_arg' in a constant-expression is not
3126                allowed.  */
3127             if (cp_parser_non_integral_constant_expression (parser,
3128                                                             "`va_arg'"))
3129               return error_mark_node;
3130             return build_x_va_arg (expression, type);
3131           }
3132
3133         case RID_OFFSETOF:
3134           return cp_parser_builtin_offsetof (parser);
3135
3136           /* Objective-C++ expressions.  */
3137         case RID_AT_ENCODE:
3138         case RID_AT_PROTOCOL:
3139         case RID_AT_SELECTOR:
3140           return cp_parser_objc_expression (parser);
3141
3142         default:
3143           cp_parser_error (parser, "expected primary-expression");
3144           return error_mark_node;
3145         }
3146
3147       /* An id-expression can start with either an identifier, a
3148          `::' as the beginning of a qualified-id, or the "operator"
3149          keyword.  */
3150     case CPP_NAME:
3151     case CPP_SCOPE:
3152     case CPP_TEMPLATE_ID:
3153     case CPP_NESTED_NAME_SPECIFIER:
3154       {
3155         tree id_expression;
3156         tree decl;
3157         const char *error_msg;
3158         bool template_p;
3159         bool done;
3160
3161       id_expression:
3162         /* Parse the id-expression.  */
3163         id_expression
3164           = cp_parser_id_expression (parser,
3165                                      /*template_keyword_p=*/false,
3166                                      /*check_dependency_p=*/true,
3167                                      &template_p,
3168                                      /*declarator_p=*/false,
3169                                      /*optional_p=*/false);
3170         if (id_expression == error_mark_node)
3171           return error_mark_node;
3172         token = cp_lexer_peek_token (parser->lexer);
3173         done = (token->type != CPP_OPEN_SQUARE
3174                 && token->type != CPP_OPEN_PAREN
3175                 && token->type != CPP_DOT
3176                 && token->type != CPP_DEREF
3177                 && token->type != CPP_PLUS_PLUS
3178                 && token->type != CPP_MINUS_MINUS);
3179         /* If we have a template-id, then no further lookup is
3180            required.  If the template-id was for a template-class, we
3181            will sometimes have a TYPE_DECL at this point.  */
3182         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3183                  || TREE_CODE (id_expression) == TYPE_DECL)
3184           decl = id_expression;
3185         /* Look up the name.  */
3186         else
3187           {
3188             tree ambiguous_decls;
3189
3190             decl = cp_parser_lookup_name (parser, id_expression,
3191                                           none_type,
3192                                           template_p,
3193                                           /*is_namespace=*/false,
3194                                           /*check_dependency=*/true,
3195                                           &ambiguous_decls);
3196             /* If the lookup was ambiguous, an error will already have
3197                been issued.  */
3198             if (ambiguous_decls)
3199               return error_mark_node;
3200
3201             /* In Objective-C++, an instance variable (ivar) may be preferred
3202                to whatever cp_parser_lookup_name() found.  */
3203             decl = objc_lookup_ivar (decl, id_expression);
3204
3205             /* If name lookup gives us a SCOPE_REF, then the
3206                qualifying scope was dependent.  */
3207             if (TREE_CODE (decl) == SCOPE_REF)
3208               return decl;
3209             /* Check to see if DECL is a local variable in a context
3210                where that is forbidden.  */
3211             if (parser->local_variables_forbidden_p
3212                 && local_variable_p (decl))
3213               {
3214                 /* It might be that we only found DECL because we are
3215                    trying to be generous with pre-ISO scoping rules.
3216                    For example, consider:
3217
3218                      int i;
3219                      void g() {
3220                        for (int i = 0; i < 10; ++i) {}
3221                        extern void f(int j = i);
3222                      }
3223
3224                    Here, name look up will originally find the out
3225                    of scope `i'.  We need to issue a warning message,
3226                    but then use the global `i'.  */
3227                 decl = check_for_out_of_scope_variable (decl);
3228                 if (local_variable_p (decl))
3229                   {
3230                     error ("local variable %qD may not appear in this context",
3231                            decl);
3232                     return error_mark_node;
3233                   }
3234               }
3235           }
3236
3237         decl = (finish_id_expression
3238                 (id_expression, decl, parser->scope,
3239                  idk,
3240                  parser->integral_constant_expression_p,
3241                  parser->allow_non_integral_constant_expression_p,
3242                  &parser->non_integral_constant_expression_p,
3243                  template_p, done, address_p,
3244                  template_arg_p,
3245                  &error_msg));
3246         if (error_msg)
3247           cp_parser_error (parser, error_msg);
3248         return decl;
3249       }
3250
3251       /* Anything else is an error.  */
3252     default:
3253       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3254       if (c_dialect_objc ()
3255           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3256         return cp_parser_objc_expression (parser);
3257
3258       cp_parser_error (parser, "expected primary-expression");
3259       return error_mark_node;
3260     }
3261 }
3262
3263 /* Parse an id-expression.
3264
3265    id-expression:
3266      unqualified-id
3267      qualified-id
3268
3269    qualified-id:
3270      :: [opt] nested-name-specifier template [opt] unqualified-id
3271      :: identifier
3272      :: operator-function-id
3273      :: template-id
3274
3275    Return a representation of the unqualified portion of the
3276    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3277    a `::' or nested-name-specifier.
3278
3279    Often, if the id-expression was a qualified-id, the caller will
3280    want to make a SCOPE_REF to represent the qualified-id.  This
3281    function does not do this in order to avoid wastefully creating
3282    SCOPE_REFs when they are not required.
3283
3284    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3285    `template' keyword.
3286
3287    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3288    uninstantiated templates.
3289
3290    If *TEMPLATE_P is non-NULL, it is set to true iff the
3291    `template' keyword is used to explicitly indicate that the entity
3292    named is a template.
3293
3294    If DECLARATOR_P is true, the id-expression is appearing as part of
3295    a declarator, rather than as part of an expression.  */
3296
3297 static tree
3298 cp_parser_id_expression (cp_parser *parser,
3299                          bool template_keyword_p,
3300                          bool check_dependency_p,
3301                          bool *template_p,
3302                          bool declarator_p,
3303                          bool optional_p)
3304 {
3305   bool global_scope_p;
3306   bool nested_name_specifier_p;
3307
3308   /* Assume the `template' keyword was not used.  */
3309   if (template_p)
3310     *template_p = template_keyword_p;
3311
3312   /* Look for the optional `::' operator.  */
3313   global_scope_p
3314     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3315        != NULL_TREE);
3316   /* Look for the optional nested-name-specifier.  */
3317   nested_name_specifier_p
3318     = (cp_parser_nested_name_specifier_opt (parser,
3319                                             /*typename_keyword_p=*/false,
3320                                             check_dependency_p,
3321                                             /*type_p=*/false,
3322                                             declarator_p)
3323        != NULL_TREE);
3324   /* If there is a nested-name-specifier, then we are looking at
3325      the first qualified-id production.  */
3326   if (nested_name_specifier_p)
3327     {
3328       tree saved_scope;
3329       tree saved_object_scope;
3330       tree saved_qualifying_scope;
3331       tree unqualified_id;
3332       bool is_template;
3333
3334       /* See if the next token is the `template' keyword.  */
3335       if (!template_p)
3336         template_p = &is_template;
3337       *template_p = cp_parser_optional_template_keyword (parser);
3338       /* Name lookup we do during the processing of the
3339          unqualified-id might obliterate SCOPE.  */
3340       saved_scope = parser->scope;
3341       saved_object_scope = parser->object_scope;
3342       saved_qualifying_scope = parser->qualifying_scope;
3343       /* Process the final unqualified-id.  */
3344       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3345                                                  check_dependency_p,
3346                                                  declarator_p,
3347                                                  /*optional_p=*/false);
3348       /* Restore the SAVED_SCOPE for our caller.  */
3349       parser->scope = saved_scope;
3350       parser->object_scope = saved_object_scope;
3351       parser->qualifying_scope = saved_qualifying_scope;
3352
3353       return unqualified_id;
3354     }
3355   /* Otherwise, if we are in global scope, then we are looking at one
3356      of the other qualified-id productions.  */
3357   else if (global_scope_p)
3358     {
3359       cp_token *token;
3360       tree id;
3361
3362       /* Peek at the next token.  */
3363       token = cp_lexer_peek_token (parser->lexer);
3364
3365       /* If it's an identifier, and the next token is not a "<", then
3366          we can avoid the template-id case.  This is an optimization
3367          for this common case.  */
3368       if (token->type == CPP_NAME
3369           && !cp_parser_nth_token_starts_template_argument_list_p
3370                (parser, 2))
3371         return cp_parser_identifier (parser);
3372
3373       cp_parser_parse_tentatively (parser);
3374       /* Try a template-id.  */
3375       id = cp_parser_template_id (parser,
3376                                   /*template_keyword_p=*/false,
3377                                   /*check_dependency_p=*/true,
3378                                   declarator_p);
3379       /* If that worked, we're done.  */
3380       if (cp_parser_parse_definitely (parser))
3381         return id;
3382
3383       /* Peek at the next token.  (Changes in the token buffer may
3384          have invalidated the pointer obtained above.)  */
3385       token = cp_lexer_peek_token (parser->lexer);
3386
3387       switch (token->type)
3388         {
3389         case CPP_NAME:
3390           return cp_parser_identifier (parser);
3391
3392         case CPP_KEYWORD:
3393           if (token->keyword == RID_OPERATOR)
3394             return cp_parser_operator_function_id (parser);
3395           /* Fall through.  */
3396
3397         default:
3398           cp_parser_error (parser, "expected id-expression");
3399           return error_mark_node;
3400         }
3401     }
3402   else
3403     return cp_parser_unqualified_id (parser, template_keyword_p,
3404                                      /*check_dependency_p=*/true,
3405                                      declarator_p,
3406                                      optional_p);
3407 }
3408
3409 /* Parse an unqualified-id.
3410
3411    unqualified-id:
3412      identifier
3413      operator-function-id
3414      conversion-function-id
3415      ~ class-name
3416      template-id
3417
3418    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3419    keyword, in a construct like `A::template ...'.
3420
3421    Returns a representation of unqualified-id.  For the `identifier'
3422    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3423    production a BIT_NOT_EXPR is returned; the operand of the
3424    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3425    other productions, see the documentation accompanying the
3426    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3427    names are looked up in uninstantiated templates.  If DECLARATOR_P
3428    is true, the unqualified-id is appearing as part of a declarator,
3429    rather than as part of an expression.  */
3430
3431 static tree
3432 cp_parser_unqualified_id (cp_parser* parser,
3433                           bool template_keyword_p,
3434                           bool check_dependency_p,
3435                           bool declarator_p,
3436                           bool optional_p)
3437 {
3438   cp_token *token;
3439
3440   /* Peek at the next token.  */
3441   token = cp_lexer_peek_token (parser->lexer);
3442
3443   switch (token->type)
3444     {
3445     case CPP_NAME:
3446       {
3447         tree id;
3448
3449         /* We don't know yet whether or not this will be a
3450            template-id.  */
3451         cp_parser_parse_tentatively (parser);
3452         /* Try a template-id.  */
3453         id = cp_parser_template_id (parser, template_keyword_p,
3454                                     check_dependency_p,
3455                                     declarator_p);
3456         /* If it worked, we're done.  */
3457         if (cp_parser_parse_definitely (parser))
3458           return id;
3459         /* Otherwise, it's an ordinary identifier.  */
3460         return cp_parser_identifier (parser);
3461       }
3462
3463     case CPP_TEMPLATE_ID:
3464       return cp_parser_template_id (parser, template_keyword_p,
3465                                     check_dependency_p,
3466                                     declarator_p);
3467
3468     case CPP_COMPL:
3469       {
3470         tree type_decl;
3471         tree qualifying_scope;
3472         tree object_scope;
3473         tree scope;
3474         bool done;
3475
3476         /* Consume the `~' token.  */
3477         cp_lexer_consume_token (parser->lexer);
3478         /* Parse the class-name.  The standard, as written, seems to
3479            say that:
3480
3481              template <typename T> struct S { ~S (); };
3482              template <typename T> S<T>::~S() {}
3483
3484            is invalid, since `~' must be followed by a class-name, but
3485            `S<T>' is dependent, and so not known to be a class.
3486            That's not right; we need to look in uninstantiated
3487            templates.  A further complication arises from:
3488
3489              template <typename T> void f(T t) {
3490                t.T::~T();
3491              }
3492
3493            Here, it is not possible to look up `T' in the scope of `T'
3494            itself.  We must look in both the current scope, and the
3495            scope of the containing complete expression.
3496
3497            Yet another issue is:
3498
3499              struct S {
3500                int S;
3501                ~S();
3502              };
3503
3504              S::~S() {}
3505
3506            The standard does not seem to say that the `S' in `~S'
3507            should refer to the type `S' and not the data member
3508            `S::S'.  */
3509
3510         /* DR 244 says that we look up the name after the "~" in the
3511            same scope as we looked up the qualifying name.  That idea
3512            isn't fully worked out; it's more complicated than that.  */
3513         scope = parser->scope;
3514         object_scope = parser->object_scope;
3515         qualifying_scope = parser->qualifying_scope;
3516
3517         /* Check for invalid scopes.  */
3518         if (scope == error_mark_node)
3519           {
3520             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3521               cp_lexer_consume_token (parser->lexer);
3522             return error_mark_node;
3523           }
3524         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3525           {
3526             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3527               error ("scope %qT before %<~%> is not a class-name", scope);
3528             cp_parser_simulate_error (parser);
3529             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3530               cp_lexer_consume_token (parser->lexer);
3531             return error_mark_node;
3532           }
3533         gcc_assert (!scope || TYPE_P (scope));
3534
3535         /* If the name is of the form "X::~X" it's OK.  */
3536         token = cp_lexer_peek_token (parser->lexer);
3537         if (scope
3538             && token->type == CPP_NAME
3539             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3540                 == CPP_OPEN_PAREN)
3541             && constructor_name_p (token->u.value, scope))
3542           {
3543             cp_lexer_consume_token (parser->lexer);
3544             return build_nt (BIT_NOT_EXPR, scope);
3545           }
3546
3547         /* If there was an explicit qualification (S::~T), first look
3548            in the scope given by the qualification (i.e., S).  */
3549         done = false;
3550         type_decl = NULL_TREE;
3551         if (scope)
3552           {
3553             cp_parser_parse_tentatively (parser);
3554             type_decl = cp_parser_class_name (parser,
3555                                               /*typename_keyword_p=*/false,
3556                                               /*template_keyword_p=*/false,
3557                                               none_type,
3558                                               /*check_dependency=*/false,
3559                                               /*class_head_p=*/false,
3560                                               declarator_p);
3561             if (cp_parser_parse_definitely (parser))
3562               done = true;
3563           }
3564         /* In "N::S::~S", look in "N" as well.  */
3565         if (!done && scope && qualifying_scope)
3566           {
3567             cp_parser_parse_tentatively (parser);
3568             parser->scope = qualifying_scope;
3569             parser->object_scope = NULL_TREE;
3570             parser->qualifying_scope = NULL_TREE;
3571             type_decl
3572               = cp_parser_class_name (parser,
3573                                       /*typename_keyword_p=*/false,
3574                                       /*template_keyword_p=*/false,
3575                                       none_type,
3576                                       /*check_dependency=*/false,
3577                                       /*class_head_p=*/false,
3578                                       declarator_p);
3579             if (cp_parser_parse_definitely (parser))
3580               done = true;
3581           }
3582         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3583         else if (!done && object_scope)
3584           {
3585             cp_parser_parse_tentatively (parser);
3586             parser->scope = object_scope;
3587             parser->object_scope = NULL_TREE;
3588             parser->qualifying_scope = NULL_TREE;
3589             type_decl
3590               = cp_parser_class_name (parser,
3591                                       /*typename_keyword_p=*/false,
3592                                       /*template_keyword_p=*/false,
3593                                       none_type,
3594                                       /*check_dependency=*/false,
3595                                       /*class_head_p=*/false,
3596                                       declarator_p);
3597             if (cp_parser_parse_definitely (parser))
3598               done = true;
3599           }
3600         /* Look in the surrounding context.  */
3601         if (!done)
3602           {
3603             parser->scope = NULL_TREE;
3604             parser->object_scope = NULL_TREE;
3605             parser->qualifying_scope = NULL_TREE;
3606             type_decl
3607               = cp_parser_class_name (parser,
3608                                       /*typename_keyword_p=*/false,
3609                                       /*template_keyword_p=*/false,
3610                                       none_type,
3611                                       /*check_dependency=*/false,
3612                                       /*class_head_p=*/false,
3613                                       declarator_p);
3614           }
3615         /* If an error occurred, assume that the name of the
3616            destructor is the same as the name of the qualifying
3617            class.  That allows us to keep parsing after running
3618            into ill-formed destructor names.  */
3619         if (type_decl == error_mark_node && scope)
3620           return build_nt (BIT_NOT_EXPR, scope);
3621         else if (type_decl == error_mark_node)
3622           return error_mark_node;
3623
3624         /* Check that destructor name and scope match.  */
3625         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3626           {
3627             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3628               error ("declaration of %<~%T%> as member of %qT",
3629                      type_decl, scope);
3630             cp_parser_simulate_error (parser);
3631             return error_mark_node;
3632           }
3633
3634         /* [class.dtor]
3635
3636            A typedef-name that names a class shall not be used as the
3637            identifier in the declarator for a destructor declaration.  */
3638         if (declarator_p
3639             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3640             && !DECL_SELF_REFERENCE_P (type_decl)
3641             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3642           error ("typedef-name %qD used as destructor declarator",
3643                  type_decl);
3644
3645         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3646       }
3647
3648     case CPP_KEYWORD:
3649       if (token->keyword == RID_OPERATOR)
3650         {
3651           tree id;
3652
3653           /* This could be a template-id, so we try that first.  */
3654           cp_parser_parse_tentatively (parser);
3655           /* Try a template-id.  */
3656           id = cp_parser_template_id (parser, template_keyword_p,
3657                                       /*check_dependency_p=*/true,
3658                                       declarator_p);
3659           /* If that worked, we're done.  */
3660           if (cp_parser_parse_definitely (parser))
3661             return id;
3662           /* We still don't know whether we're looking at an
3663              operator-function-id or a conversion-function-id.  */
3664           cp_parser_parse_tentatively (parser);
3665           /* Try an operator-function-id.  */
3666           id = cp_parser_operator_function_id (parser);
3667           /* If that didn't work, try a conversion-function-id.  */
3668           if (!cp_parser_parse_definitely (parser))
3669             id = cp_parser_conversion_function_id (parser);
3670
3671           return id;
3672         }
3673       /* Fall through.  */
3674
3675     default:
3676       if (optional_p)
3677         return NULL_TREE;
3678       cp_parser_error (parser, "expected unqualified-id");
3679       return error_mark_node;
3680     }
3681 }
3682
3683 /* Parse an (optional) nested-name-specifier.
3684
3685    nested-name-specifier:
3686      class-or-namespace-name :: nested-name-specifier [opt]
3687      class-or-namespace-name :: template nested-name-specifier [opt]
3688
3689    PARSER->SCOPE should be set appropriately before this function is
3690    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3691    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3692    in name lookups.
3693
3694    Sets PARSER->SCOPE to the class (TYPE) or namespace
3695    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3696    it unchanged if there is no nested-name-specifier.  Returns the new
3697    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3698
3699    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3700    part of a declaration and/or decl-specifier.  */
3701
3702 static tree
3703 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3704                                      bool typename_keyword_p,
3705                                      bool check_dependency_p,
3706                                      bool type_p,
3707                                      bool is_declaration)
3708 {
3709   bool success = false;
3710   cp_token_position start = 0;
3711   cp_token *token;
3712
3713   /* Remember where the nested-name-specifier starts.  */
3714   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3715     {
3716       start = cp_lexer_token_position (parser->lexer, false);
3717       push_deferring_access_checks (dk_deferred);
3718     }
3719
3720   while (true)
3721     {
3722       tree new_scope;
3723       tree old_scope;
3724       tree saved_qualifying_scope;
3725       bool template_keyword_p;
3726
3727       /* Spot cases that cannot be the beginning of a
3728          nested-name-specifier.  */
3729       token = cp_lexer_peek_token (parser->lexer);
3730
3731       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3732          the already parsed nested-name-specifier.  */
3733       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3734         {
3735           /* Grab the nested-name-specifier and continue the loop.  */
3736           cp_parser_pre_parsed_nested_name_specifier (parser);
3737           /* If we originally encountered this nested-name-specifier
3738              with IS_DECLARATION set to false, we will not have
3739              resolved TYPENAME_TYPEs, so we must do so here.  */
3740           if (is_declaration
3741               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3742             {
3743               new_scope = resolve_typename_type (parser->scope,
3744                                                  /*only_current_p=*/false);
3745               if (new_scope != error_mark_node)
3746                 parser->scope = new_scope;
3747             }
3748           success = true;
3749           continue;
3750         }
3751
3752       /* Spot cases that cannot be the beginning of a
3753          nested-name-specifier.  On the second and subsequent times
3754          through the loop, we look for the `template' keyword.  */
3755       if (success && token->keyword == RID_TEMPLATE)
3756         ;
3757       /* A template-id can start a nested-name-specifier.  */
3758       else if (token->type == CPP_TEMPLATE_ID)
3759         ;
3760       else
3761         {
3762           /* If the next token is not an identifier, then it is
3763              definitely not a class-or-namespace-name.  */
3764           if (token->type != CPP_NAME)
3765             break;
3766           /* If the following token is neither a `<' (to begin a
3767              template-id), nor a `::', then we are not looking at a
3768              nested-name-specifier.  */
3769           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3770           if (token->type != CPP_SCOPE
3771               && !cp_parser_nth_token_starts_template_argument_list_p
3772                   (parser, 2))
3773             break;
3774         }
3775
3776       /* The nested-name-specifier is optional, so we parse
3777          tentatively.  */
3778       cp_parser_parse_tentatively (parser);
3779
3780       /* Look for the optional `template' keyword, if this isn't the
3781          first time through the loop.  */
3782       if (success)
3783         template_keyword_p = cp_parser_optional_template_keyword (parser);
3784       else
3785         template_keyword_p = false;
3786
3787       /* Save the old scope since the name lookup we are about to do
3788          might destroy it.  */
3789       old_scope = parser->scope;
3790       saved_qualifying_scope = parser->qualifying_scope;
3791       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3792          look up names in "X<T>::I" in order to determine that "Y" is
3793          a template.  So, if we have a typename at this point, we make
3794          an effort to look through it.  */
3795       if (is_declaration
3796           && !typename_keyword_p
3797           && parser->scope
3798           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3799         parser->scope = resolve_typename_type (parser->scope,
3800                                                /*only_current_p=*/false);
3801       /* Parse the qualifying entity.  */
3802       new_scope
3803         = cp_parser_class_or_namespace_name (parser,
3804                                              typename_keyword_p,
3805                                              template_keyword_p,
3806                                              check_dependency_p,
3807                                              type_p,
3808                                              is_declaration);
3809       /* Look for the `::' token.  */
3810       cp_parser_require (parser, CPP_SCOPE, "`::'");
3811
3812       /* If we found what we wanted, we keep going; otherwise, we're
3813          done.  */
3814       if (!cp_parser_parse_definitely (parser))
3815         {
3816           bool error_p = false;
3817
3818           /* Restore the OLD_SCOPE since it was valid before the
3819              failed attempt at finding the last
3820              class-or-namespace-name.  */
3821           parser->scope = old_scope;
3822           parser->qualifying_scope = saved_qualifying_scope;
3823           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3824             break;
3825           /* If the next token is an identifier, and the one after
3826              that is a `::', then any valid interpretation would have
3827              found a class-or-namespace-name.  */
3828           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3829                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3830                      == CPP_SCOPE)
3831                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3832                      != CPP_COMPL))
3833             {
3834               token = cp_lexer_consume_token (parser->lexer);
3835               if (!error_p)
3836                 {
3837                   if (!token->ambiguous_p)
3838                     {
3839                       tree decl;
3840                       tree ambiguous_decls;
3841
3842                       decl = cp_parser_lookup_name (parser, token->u.value,
3843                                                     none_type,
3844                                                     /*is_template=*/false,
3845                                                     /*is_namespace=*/false,
3846                                                     /*check_dependency=*/true,
3847                                                     &ambiguous_decls);
3848                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3849                         error ("%qD used without template parameters", decl);
3850                       else if (ambiguous_decls)
3851                         {
3852                           error ("reference to %qD is ambiguous",
3853                                  token->u.value);
3854                           print_candidates (ambiguous_decls);
3855                           decl = error_mark_node;
3856                         }
3857                       else
3858                         cp_parser_name_lookup_error
3859                           (parser, token->u.value, decl,
3860                            "is not a class or namespace");
3861                     }
3862                   parser->scope = error_mark_node;
3863                   error_p = true;
3864                   /* Treat this as a successful nested-name-specifier
3865                      due to:
3866
3867                      [basic.lookup.qual]
3868
3869                      If the name found is not a class-name (clause
3870                      _class_) or namespace-name (_namespace.def_), the
3871                      program is ill-formed.  */
3872                   success = true;
3873                 }
3874               cp_lexer_consume_token (parser->lexer);
3875             }
3876           break;
3877         }
3878       /* We've found one valid nested-name-specifier.  */
3879       success = true;
3880       /* Name lookup always gives us a DECL.  */
3881       if (TREE_CODE (new_scope) == TYPE_DECL)
3882         new_scope = TREE_TYPE (new_scope);
3883       /* Uses of "template" must be followed by actual templates.  */
3884       if (template_keyword_p
3885           && !(CLASS_TYPE_P (new_scope)
3886                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3887                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3888                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3889           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3890                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3891                    == TEMPLATE_ID_EXPR)))
3892         pedwarn (TYPE_P (new_scope)
3893                  ? "%qT is not a template"
3894                  : "%qD is not a template",
3895                  new_scope);
3896       /* If it is a class scope, try to complete it; we are about to
3897          be looking up names inside the class.  */
3898       if (TYPE_P (new_scope)
3899           /* Since checking types for dependency can be expensive,
3900              avoid doing it if the type is already complete.  */
3901           && !COMPLETE_TYPE_P (new_scope)
3902           /* Do not try to complete dependent types.  */
3903           && !dependent_type_p (new_scope))
3904         new_scope = complete_type (new_scope);
3905       /* Make sure we look in the right scope the next time through
3906          the loop.  */
3907       parser->scope = new_scope;
3908     }
3909
3910   /* If parsing tentatively, replace the sequence of tokens that makes
3911      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3912      token.  That way, should we re-parse the token stream, we will
3913      not have to repeat the effort required to do the parse, nor will
3914      we issue duplicate error messages.  */
3915   if (success && start)
3916     {
3917       cp_token *token;
3918
3919       token = cp_lexer_token_at (parser->lexer, start);
3920       /* Reset the contents of the START token.  */
3921       token->type = CPP_NESTED_NAME_SPECIFIER;
3922       /* Retrieve any deferred checks.  Do not pop this access checks yet
3923          so the memory will not be reclaimed during token replacing below.  */
3924       token->u.tree_check_value = GGC_CNEW (struct tree_check);
3925       token->u.tree_check_value->value = parser->scope;
3926       token->u.tree_check_value->checks = get_deferred_access_checks ();
3927       token->u.tree_check_value->qualifying_scope =
3928         parser->qualifying_scope;
3929       token->keyword = RID_MAX;
3930
3931       /* Purge all subsequent tokens.  */
3932       cp_lexer_purge_tokens_after (parser->lexer, start);
3933     }
3934
3935   if (start)
3936     pop_to_parent_deferring_access_checks ();
3937
3938   return success ? parser->scope : NULL_TREE;
3939 }
3940
3941 /* Parse a nested-name-specifier.  See
3942    cp_parser_nested_name_specifier_opt for details.  This function
3943    behaves identically, except that it will an issue an error if no
3944    nested-name-specifier is present.  */
3945
3946 static tree
3947 cp_parser_nested_name_specifier (cp_parser *parser,
3948                                  bool typename_keyword_p,
3949                                  bool check_dependency_p,
3950                                  bool type_p,
3951                                  bool is_declaration)
3952 {
3953   tree scope;
3954
3955   /* Look for the nested-name-specifier.  */
3956   scope = cp_parser_nested_name_specifier_opt (parser,
3957                                                typename_keyword_p,
3958                                                check_dependency_p,
3959                                                type_p,
3960                                                is_declaration);
3961   /* If it was not present, issue an error message.  */
3962   if (!scope)
3963     {
3964       cp_parser_error (parser, "expected nested-name-specifier");
3965       parser->scope = NULL_TREE;
3966     }
3967
3968   return scope;
3969 }
3970
3971 /* Parse a class-or-namespace-name.
3972
3973    class-or-namespace-name:
3974      class-name
3975      namespace-name
3976
3977    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3978    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3979    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3980    TYPE_P is TRUE iff the next name should be taken as a class-name,
3981    even the same name is declared to be another entity in the same
3982    scope.
3983
3984    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3985    specified by the class-or-namespace-name.  If neither is found the
3986    ERROR_MARK_NODE is returned.  */
3987
3988 static tree
3989 cp_parser_class_or_namespace_name (cp_parser *parser,
3990                                    bool typename_keyword_p,
3991                                    bool template_keyword_p,
3992                                    bool check_dependency_p,
3993                                    bool type_p,
3994                                    bool is_declaration)
3995 {
3996   tree saved_scope;
3997   tree saved_qualifying_scope;
3998   tree saved_object_scope;
3999   tree scope;
4000   bool only_class_p;
4001
4002   /* Before we try to parse the class-name, we must save away the
4003      current PARSER->SCOPE since cp_parser_class_name will destroy
4004      it.  */
4005   saved_scope = parser->scope;
4006   saved_qualifying_scope = parser->qualifying_scope;
4007   saved_object_scope = parser->object_scope;
4008   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4009      there is no need to look for a namespace-name.  */
4010   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4011   if (!only_class_p)
4012     cp_parser_parse_tentatively (parser);
4013   scope = cp_parser_class_name (parser,
4014                                 typename_keyword_p,
4015                                 template_keyword_p,
4016                                 type_p ? class_type : none_type,
4017                                 check_dependency_p,
4018                                 /*class_head_p=*/false,
4019                                 is_declaration);
4020   /* If that didn't work, try for a namespace-name.  */
4021   if (!only_class_p && !cp_parser_parse_definitely (parser))
4022     {
4023       /* Restore the saved scope.  */
4024       parser->scope = saved_scope;
4025       parser->qualifying_scope = saved_qualifying_scope;
4026       parser->object_scope = saved_object_scope;
4027       /* If we are not looking at an identifier followed by the scope
4028          resolution operator, then this is not part of a
4029          nested-name-specifier.  (Note that this function is only used
4030          to parse the components of a nested-name-specifier.)  */
4031       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4032           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4033         return error_mark_node;
4034       scope = cp_parser_namespace_name (parser);
4035     }
4036
4037   return scope;
4038 }
4039
4040 /* Parse a postfix-expression.
4041
4042    postfix-expression:
4043      primary-expression
4044      postfix-expression [ expression ]
4045      postfix-expression ( expression-list [opt] )
4046      simple-type-specifier ( expression-list [opt] )
4047      typename :: [opt] nested-name-specifier identifier
4048        ( expression-list [opt] )
4049      typename :: [opt] nested-name-specifier template [opt] template-id
4050        ( expression-list [opt] )
4051      postfix-expression . template [opt] id-expression
4052      postfix-expression -> template [opt] id-expression
4053      postfix-expression . pseudo-destructor-name
4054      postfix-expression -> pseudo-destructor-name
4055      postfix-expression ++
4056      postfix-expression --
4057      dynamic_cast < type-id > ( expression )
4058      static_cast < type-id > ( expression )
4059      reinterpret_cast < type-id > ( expression )
4060      const_cast < type-id > ( expression )
4061      typeid ( expression )
4062      typeid ( type-id )
4063
4064    GNU Extension:
4065
4066    postfix-expression:
4067      ( type-id ) { initializer-list , [opt] }
4068
4069    This extension is a GNU version of the C99 compound-literal
4070    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4071    but they are essentially the same concept.)
4072
4073    If ADDRESS_P is true, the postfix expression is the operand of the
4074    `&' operator.  CAST_P is true if this expression is the target of a
4075    cast.
4076
4077    Returns a representation of the expression.  */
4078
4079 static tree
4080 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4081 {
4082   cp_token *token;
4083   enum rid keyword;
4084   cp_id_kind idk = CP_ID_KIND_NONE;
4085   tree postfix_expression = NULL_TREE;
4086
4087   /* Peek at the next token.  */
4088   token = cp_lexer_peek_token (parser->lexer);
4089   /* Some of the productions are determined by keywords.  */
4090   keyword = token->keyword;
4091   switch (keyword)
4092     {
4093     case RID_DYNCAST:
4094     case RID_STATCAST:
4095     case RID_REINTCAST:
4096     case RID_CONSTCAST:
4097       {
4098         tree type;
4099         tree expression;
4100         const char *saved_message;
4101
4102         /* All of these can be handled in the same way from the point
4103            of view of parsing.  Begin by consuming the token
4104            identifying the cast.  */
4105         cp_lexer_consume_token (parser->lexer);
4106
4107         /* New types cannot be defined in the cast.  */
4108         saved_message = parser->type_definition_forbidden_message;
4109         parser->type_definition_forbidden_message
4110           = "types may not be defined in casts";
4111
4112         /* Look for the opening `<'.  */
4113         cp_parser_require (parser, CPP_LESS, "`<'");
4114         /* Parse the type to which we are casting.  */
4115         type = cp_parser_type_id (parser);
4116         /* Look for the closing `>'.  */
4117         cp_parser_require (parser, CPP_GREATER, "`>'");
4118         /* Restore the old message.  */
4119         parser->type_definition_forbidden_message = saved_message;
4120
4121         /* And the expression which is being cast.  */
4122         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4123         expression = cp_parser_expression (parser, /*cast_p=*/true);
4124         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4125
4126         /* Only type conversions to integral or enumeration types
4127            can be used in constant-expressions.  */
4128         if (!cast_valid_in_integral_constant_expression_p (type)
4129             && (cp_parser_non_integral_constant_expression
4130                 (parser,
4131                  "a cast to a type other than an integral or "
4132                  "enumeration type")))
4133           return error_mark_node;
4134
4135         switch (keyword)
4136           {
4137           case RID_DYNCAST:
4138             postfix_expression
4139               = build_dynamic_cast (type, expression);
4140             break;
4141           case RID_STATCAST:
4142             postfix_expression
4143               = build_static_cast (type, expression);
4144             break;
4145           case RID_REINTCAST:
4146             postfix_expression
4147               = build_reinterpret_cast (type, expression);
4148             break;
4149           case RID_CONSTCAST:
4150             postfix_expression
4151               = build_const_cast (type, expression);
4152             break;
4153           default:
4154             gcc_unreachable ();
4155           }
4156       }
4157       break;
4158
4159     case RID_TYPEID:
4160       {
4161         tree type;
4162         const char *saved_message;
4163         bool saved_in_type_id_in_expr_p;
4164
4165         /* Consume the `typeid' token.  */
4166         cp_lexer_consume_token (parser->lexer);
4167         /* Look for the `(' token.  */
4168         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4169         /* Types cannot be defined in a `typeid' expression.  */
4170         saved_message = parser->type_definition_forbidden_message;
4171         parser->type_definition_forbidden_message
4172           = "types may not be defined in a `typeid\' expression";
4173         /* We can't be sure yet whether we're looking at a type-id or an
4174            expression.  */
4175         cp_parser_parse_tentatively (parser);
4176         /* Try a type-id first.  */
4177         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4178         parser->in_type_id_in_expr_p = true;
4179         type = cp_parser_type_id (parser);
4180         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4181         /* Look for the `)' token.  Otherwise, we can't be sure that
4182            we're not looking at an expression: consider `typeid (int
4183            (3))', for example.  */
4184         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4185         /* If all went well, simply lookup the type-id.  */
4186         if (cp_parser_parse_definitely (parser))
4187           postfix_expression = get_typeid (type);
4188         /* Otherwise, fall back to the expression variant.  */
4189         else
4190           {
4191             tree expression;
4192
4193             /* Look for an expression.  */
4194             expression = cp_parser_expression (parser, /*cast_p=*/false);
4195             /* Compute its typeid.  */
4196             postfix_expression = build_typeid (expression);
4197             /* Look for the `)' token.  */
4198             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4199           }
4200         /* Restore the saved message.  */
4201         parser->type_definition_forbidden_message = saved_message;
4202         /* `typeid' may not appear in an integral constant expression.  */
4203         if (cp_parser_non_integral_constant_expression(parser,
4204                                                        "`typeid' operator"))
4205           return error_mark_node;
4206       }
4207       break;
4208
4209     case RID_TYPENAME:
4210       {
4211         tree type;
4212         /* The syntax permitted here is the same permitted for an
4213            elaborated-type-specifier.  */
4214         type = cp_parser_elaborated_type_specifier (parser,
4215                                                     /*is_friend=*/false,
4216                                                     /*is_declaration=*/false);
4217         postfix_expression = cp_parser_functional_cast (parser, type);
4218       }
4219       break;
4220
4221     default:
4222       {
4223         tree type;
4224
4225         /* If the next thing is a simple-type-specifier, we may be
4226            looking at a functional cast.  We could also be looking at
4227            an id-expression.  So, we try the functional cast, and if
4228            that doesn't work we fall back to the primary-expression.  */
4229         cp_parser_parse_tentatively (parser);
4230         /* Look for the simple-type-specifier.  */
4231         type = cp_parser_simple_type_specifier (parser,
4232                                                 /*decl_specs=*/NULL,
4233                                                 CP_PARSER_FLAGS_NONE);
4234         /* Parse the cast itself.  */
4235         if (!cp_parser_error_occurred (parser))
4236           postfix_expression
4237             = cp_parser_functional_cast (parser, type);
4238         /* If that worked, we're done.  */
4239         if (cp_parser_parse_definitely (parser))
4240           break;
4241
4242         /* If the functional-cast didn't work out, try a
4243            compound-literal.  */
4244         if (cp_parser_allow_gnu_extensions_p (parser)
4245             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4246           {
4247             VEC(constructor_elt,gc) *initializer_list = NULL;
4248             bool saved_in_type_id_in_expr_p;
4249
4250             cp_parser_parse_tentatively (parser);
4251             /* Consume the `('.  */
4252             cp_lexer_consume_token (parser->lexer);
4253             /* Parse the type.  */
4254             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4255             parser->in_type_id_in_expr_p = true;
4256             type = cp_parser_type_id (parser);
4257             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4258             /* Look for the `)'.  */
4259             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4260             /* Look for the `{'.  */
4261             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4262             /* If things aren't going well, there's no need to
4263                keep going.  */
4264             if (!cp_parser_error_occurred (parser))
4265               {
4266                 bool non_constant_p;
4267                 /* Parse the initializer-list.  */
4268                 initializer_list
4269                   = cp_parser_initializer_list (parser, &non_constant_p);
4270                 /* Allow a trailing `,'.  */
4271                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4272                   cp_lexer_consume_token (parser->lexer);
4273                 /* Look for the final `}'.  */
4274                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4275               }
4276             /* If that worked, we're definitely looking at a
4277                compound-literal expression.  */
4278             if (cp_parser_parse_definitely (parser))
4279               {
4280                 /* Warn the user that a compound literal is not
4281                    allowed in standard C++.  */
4282                 if (pedantic)
4283                   pedwarn ("ISO C++ forbids compound-literals");
4284                 /* Form the representation of the compound-literal.  */
4285                 postfix_expression
4286                   = finish_compound_literal (type, initializer_list);
4287                 break;
4288               }
4289           }
4290
4291         /* It must be a primary-expression.  */
4292         postfix_expression
4293           = cp_parser_primary_expression (parser, address_p, cast_p,
4294                                           /*template_arg_p=*/false,
4295                                           &idk);
4296       }
4297       break;
4298     }
4299
4300   /* Keep looping until the postfix-expression is complete.  */
4301   while (true)
4302     {
4303       if (idk == CP_ID_KIND_UNQUALIFIED
4304           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4305           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4306         /* It is not a Koenig lookup function call.  */
4307         postfix_expression
4308           = unqualified_name_lookup_error (postfix_expression);
4309
4310       /* Peek at the next token.  */
4311       token = cp_lexer_peek_token (parser->lexer);
4312
4313       switch (token->type)
4314         {
4315         case CPP_OPEN_SQUARE:
4316           postfix_expression
4317             = cp_parser_postfix_open_square_expression (parser,
4318                                                         postfix_expression,
4319                                                         false);
4320           idk = CP_ID_KIND_NONE;
4321           break;
4322
4323         case CPP_OPEN_PAREN:
4324           /* postfix-expression ( expression-list [opt] ) */
4325           {
4326             bool koenig_p;
4327             bool is_builtin_constant_p;
4328             bool saved_integral_constant_expression_p = false;
4329             bool saved_non_integral_constant_expression_p = false;
4330             tree args;
4331
4332             is_builtin_constant_p
4333               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4334             if (is_builtin_constant_p)
4335               {
4336                 /* The whole point of __builtin_constant_p is to allow
4337                    non-constant expressions to appear as arguments.  */
4338                 saved_integral_constant_expression_p
4339                   = parser->integral_constant_expression_p;
4340                 saved_non_integral_constant_expression_p
4341                   = parser->non_integral_constant_expression_p;
4342                 parser->integral_constant_expression_p = false;
4343               }
4344             args = (cp_parser_parenthesized_expression_list
4345                     (parser, /*is_attribute_list=*/false,
4346                      /*cast_p=*/false,
4347                      /*non_constant_p=*/NULL));
4348             if (is_builtin_constant_p)
4349               {
4350                 parser->integral_constant_expression_p
4351                   = saved_integral_constant_expression_p;
4352                 parser->non_integral_constant_expression_p
4353                   = saved_non_integral_constant_expression_p;
4354               }
4355
4356             if (args == error_mark_node)
4357               {
4358                 postfix_expression = error_mark_node;
4359                 break;
4360               }
4361
4362             /* Function calls are not permitted in
4363                constant-expressions.  */
4364             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4365                 && cp_parser_non_integral_constant_expression (parser,
4366                                                                "a function call"))
4367               {
4368                 postfix_expression = error_mark_node;
4369                 break;
4370               }
4371
4372             koenig_p = false;
4373             if (idk == CP_ID_KIND_UNQUALIFIED)
4374               {
4375                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4376                   {
4377                     if (args)
4378                       {
4379                         koenig_p = true;
4380                         postfix_expression
4381                           = perform_koenig_lookup (postfix_expression, args);
4382                       }
4383                     else
4384                       postfix_expression
4385                         = unqualified_fn_lookup_error (postfix_expression);
4386                   }
4387                 /* We do not perform argument-dependent lookup if
4388                    normal lookup finds a non-function, in accordance
4389                    with the expected resolution of DR 218.  */
4390                 else if (args && is_overloaded_fn (postfix_expression))
4391                   {
4392                     tree fn = get_first_fn (postfix_expression);
4393
4394                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4395                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4396
4397                     /* Only do argument dependent lookup if regular
4398                        lookup does not find a set of member functions.
4399                        [basic.lookup.koenig]/2a  */
4400                     if (!DECL_FUNCTION_MEMBER_P (fn))
4401                       {
4402                         koenig_p = true;
4403                         postfix_expression
4404                           = perform_koenig_lookup (postfix_expression, args);
4405                       }
4406                   }
4407               }
4408
4409             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4410               {
4411                 tree instance = TREE_OPERAND (postfix_expression, 0);
4412                 tree fn = TREE_OPERAND (postfix_expression, 1);
4413
4414                 if (processing_template_decl
4415                     && (type_dependent_expression_p (instance)
4416                         || (!BASELINK_P (fn)
4417                             && TREE_CODE (fn) != FIELD_DECL)
4418                         || type_dependent_expression_p (fn)
4419                         || any_type_dependent_arguments_p (args)))
4420                   {
4421                     postfix_expression
4422                       = build_min_nt (CALL_EXPR, postfix_expression,
4423                                       args, NULL_TREE);
4424                     break;
4425                   }
4426
4427                 if (BASELINK_P (fn))
4428                   postfix_expression
4429                     = (build_new_method_call
4430                        (instance, fn, args, NULL_TREE,
4431                         (idk == CP_ID_KIND_QUALIFIED
4432                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4433                         /*fn_p=*/NULL));
4434                 else
4435                   postfix_expression
4436                     = finish_call_expr (postfix_expression, args,
4437                                         /*disallow_virtual=*/false,
4438                                         /*koenig_p=*/false);
4439               }
4440             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4441                      || TREE_CODE (postfix_expression) == MEMBER_REF
4442                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4443               postfix_expression = (build_offset_ref_call_from_tree
4444                                     (postfix_expression, args));
4445             else if (idk == CP_ID_KIND_QUALIFIED)
4446               /* A call to a static class member, or a namespace-scope
4447                  function.  */
4448               postfix_expression
4449                 = finish_call_expr (postfix_expression, args,
4450                                     /*disallow_virtual=*/true,
4451                                     koenig_p);
4452             else
4453               /* All other function calls.  */
4454               postfix_expression
4455                 = finish_call_expr (postfix_expression, args,
4456                                     /*disallow_virtual=*/false,
4457                                     koenig_p);
4458
4459             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4460             idk = CP_ID_KIND_NONE;
4461           }
4462           break;
4463
4464         case CPP_DOT:
4465         case CPP_DEREF:
4466           /* postfix-expression . template [opt] id-expression
4467              postfix-expression . pseudo-destructor-name
4468              postfix-expression -> template [opt] id-expression
4469              postfix-expression -> pseudo-destructor-name */
4470
4471           /* Consume the `.' or `->' operator.  */
4472           cp_lexer_consume_token (parser->lexer);
4473
4474           postfix_expression
4475             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4476                                                       postfix_expression,
4477                                                       false, &idk);
4478           break;
4479
4480         case CPP_PLUS_PLUS:
4481           /* postfix-expression ++  */
4482           /* Consume the `++' token.  */
4483           cp_lexer_consume_token (parser->lexer);
4484           /* Generate a representation for the complete expression.  */
4485           postfix_expression
4486             = finish_increment_expr (postfix_expression,
4487                                      POSTINCREMENT_EXPR);
4488           /* Increments may not appear in constant-expressions.  */
4489           if (cp_parser_non_integral_constant_expression (parser,
4490                                                           "an increment"))
4491             postfix_expression = error_mark_node;
4492           idk = CP_ID_KIND_NONE;
4493           break;
4494
4495         case CPP_MINUS_MINUS:
4496           /* postfix-expression -- */
4497           /* Consume the `--' token.  */
4498           cp_lexer_consume_token (parser->lexer);
4499           /* Generate a representation for the complete expression.  */
4500           postfix_expression
4501             = finish_increment_expr (postfix_expression,
4502                                      POSTDECREMENT_EXPR);
4503           /* Decrements may not appear in constant-expressions.  */
4504           if (cp_parser_non_integral_constant_expression (parser,
4505                                                           "a decrement"))
4506             postfix_expression = error_mark_node;
4507           idk = CP_ID_KIND_NONE;
4508           break;
4509
4510         default:
4511           return postfix_expression;
4512         }
4513     }
4514
4515   /* We should never get here.  */
4516   gcc_unreachable ();
4517   return error_mark_node;
4518 }
4519
4520 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4521    by cp_parser_builtin_offsetof.  We're looking for
4522
4523      postfix-expression [ expression ]
4524
4525    FOR_OFFSETOF is set if we're being called in that context, which
4526    changes how we deal with integer constant expressions.  */
4527
4528 static tree
4529 cp_parser_postfix_open_square_expression (cp_parser *parser,
4530                                           tree postfix_expression,
4531                                           bool for_offsetof)
4532 {
4533   tree index;
4534
4535   /* Consume the `[' token.  */
4536   cp_lexer_consume_token (parser->lexer);
4537
4538   /* Parse the index expression.  */
4539   /* ??? For offsetof, there is a question of what to allow here.  If
4540      offsetof is not being used in an integral constant expression context,
4541      then we *could* get the right answer by computing the value at runtime.
4542      If we are in an integral constant expression context, then we might
4543      could accept any constant expression; hard to say without analysis.
4544      Rather than open the barn door too wide right away, allow only integer
4545      constant expressions here.  */
4546   if (for_offsetof)
4547     index = cp_parser_constant_expression (parser, false, NULL);
4548   else
4549     index = cp_parser_expression (parser, /*cast_p=*/false);
4550
4551   /* Look for the closing `]'.  */
4552   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4553
4554   /* Build the ARRAY_REF.  */
4555   postfix_expression = grok_array_decl (postfix_expression, index);
4556
4557   /* When not doing offsetof, array references are not permitted in
4558      constant-expressions.  */
4559   if (!for_offsetof
4560       && (cp_parser_non_integral_constant_expression
4561           (parser, "an array reference")))
4562     postfix_expression = error_mark_node;
4563
4564   return postfix_expression;
4565 }
4566
4567 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4568    by cp_parser_builtin_offsetof.  We're looking for
4569
4570      postfix-expression . template [opt] id-expression
4571      postfix-expression . pseudo-destructor-name
4572      postfix-expression -> template [opt] id-expression
4573      postfix-expression -> pseudo-destructor-name
4574
4575    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4576    limits what of the above we'll actually accept, but nevermind.
4577    TOKEN_TYPE is the "." or "->" token, which will already have been
4578    removed from the stream.  */
4579
4580 static tree
4581 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4582                                         enum cpp_ttype token_type,
4583                                         tree postfix_expression,
4584                                         bool for_offsetof, cp_id_kind *idk)
4585 {
4586   tree name;
4587   bool dependent_p;
4588   bool pseudo_destructor_p;
4589   tree scope = NULL_TREE;
4590
4591   /* If this is a `->' operator, dereference the pointer.  */
4592   if (token_type == CPP_DEREF)
4593     postfix_expression = build_x_arrow (postfix_expression);
4594   /* Check to see whether or not the expression is type-dependent.  */
4595   dependent_p = type_dependent_expression_p (postfix_expression);
4596   /* The identifier following the `->' or `.' is not qualified.  */
4597   parser->scope = NULL_TREE;
4598   parser->qualifying_scope = NULL_TREE;
4599   parser->object_scope = NULL_TREE;
4600   *idk = CP_ID_KIND_NONE;
4601   /* Enter the scope corresponding to the type of the object
4602      given by the POSTFIX_EXPRESSION.  */
4603   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4604     {
4605       scope = TREE_TYPE (postfix_expression);
4606       /* According to the standard, no expression should ever have
4607          reference type.  Unfortunately, we do not currently match
4608          the standard in this respect in that our internal representation
4609          of an expression may have reference type even when the standard
4610          says it does not.  Therefore, we have to manually obtain the
4611          underlying type here.  */
4612       scope = non_reference (scope);
4613       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4614       if (scope == unknown_type_node)
4615         {
4616           error ("%qE does not have class type", postfix_expression);
4617           scope = NULL_TREE;
4618         }
4619       else
4620         scope = complete_type_or_else (scope, NULL_TREE);
4621       /* Let the name lookup machinery know that we are processing a
4622          class member access expression.  */
4623       parser->context->object_type = scope;
4624       /* If something went wrong, we want to be able to discern that case,
4625          as opposed to the case where there was no SCOPE due to the type
4626          of expression being dependent.  */
4627       if (!scope)
4628         scope = error_mark_node;
4629       /* If the SCOPE was erroneous, make the various semantic analysis
4630          functions exit quickly -- and without issuing additional error
4631          messages.  */
4632       if (scope == error_mark_node)
4633         postfix_expression = error_mark_node;
4634     }
4635
4636   /* Assume this expression is not a pseudo-destructor access.  */
4637   pseudo_destructor_p = false;
4638
4639   /* If the SCOPE is a scalar type, then, if this is a valid program,
4640      we must be looking at a pseudo-destructor-name.  */
4641   if (scope && SCALAR_TYPE_P (scope))
4642     {
4643       tree s;
4644       tree type;
4645
4646       cp_parser_parse_tentatively (parser);
4647       /* Parse the pseudo-destructor-name.  */
4648       s = NULL_TREE;
4649       cp_parser_pseudo_destructor_name (parser, &s, &type);
4650       if (cp_parser_parse_definitely (parser))
4651         {
4652           pseudo_destructor_p = true;
4653           postfix_expression
4654             = finish_pseudo_destructor_expr (postfix_expression,
4655                                              s, TREE_TYPE (type));
4656         }
4657     }
4658
4659   if (!pseudo_destructor_p)
4660     {
4661       /* If the SCOPE is not a scalar type, we are looking at an
4662          ordinary class member access expression, rather than a
4663          pseudo-destructor-name.  */
4664       bool template_p;
4665       /* Parse the id-expression.  */
4666       name = (cp_parser_id_expression
4667               (parser,
4668                cp_parser_optional_template_keyword (parser),
4669                /*check_dependency_p=*/true,
4670                &template_p,
4671                /*declarator_p=*/false,
4672                /*optional_p=*/false));
4673       /* In general, build a SCOPE_REF if the member name is qualified.
4674          However, if the name was not dependent and has already been
4675          resolved; there is no need to build the SCOPE_REF.  For example;
4676
4677              struct X { void f(); };
4678              template <typename T> void f(T* t) { t->X::f(); }
4679
4680          Even though "t" is dependent, "X::f" is not and has been resolved
4681          to a BASELINK; there is no need to include scope information.  */
4682
4683       /* But we do need to remember that there was an explicit scope for
4684          virtual function calls.  */
4685       if (parser->scope)
4686         *idk = CP_ID_KIND_QUALIFIED;
4687
4688       /* If the name is a template-id that names a type, we will get a
4689          TYPE_DECL here.  That is invalid code.  */
4690       if (TREE_CODE (name) == TYPE_DECL)
4691         {
4692           error ("invalid use of %qD", name);
4693           postfix_expression = error_mark_node;
4694         }
4695       else
4696         {
4697           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4698             {
4699               name = build_qualified_name (/*type=*/NULL_TREE,
4700                                            parser->scope,
4701                                            name,
4702                                            template_p);
4703               parser->scope = NULL_TREE;
4704               parser->qualifying_scope = NULL_TREE;
4705               parser->object_scope = NULL_TREE;
4706             }
4707           if (scope && name && BASELINK_P (name))
4708             adjust_result_of_qualified_name_lookup
4709               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4710           postfix_expression
4711             = finish_class_member_access_expr (postfix_expression, name,
4712                                                template_p);
4713         }
4714     }
4715
4716   /* We no longer need to look up names in the scope of the object on
4717      the left-hand side of the `.' or `->' operator.  */
4718   parser->context->object_type = NULL_TREE;
4719
4720   /* Outside of offsetof, these operators may not appear in
4721      constant-expressions.  */
4722   if (!for_offsetof
4723       && (cp_parser_non_integral_constant_expression
4724           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4725     postfix_expression = error_mark_node;
4726
4727   return postfix_expression;
4728 }
4729
4730 /* Parse a parenthesized expression-list.
4731
4732    expression-list:
4733      assignment-expression
4734      expression-list, assignment-expression
4735
4736    attribute-list:
4737      expression-list
4738      identifier
4739      identifier, expression-list
4740
4741    CAST_P is true if this expression is the target of a cast.
4742
4743    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4744    representation of an assignment-expression.  Note that a TREE_LIST
4745    is returned even if there is only a single expression in the list.
4746    error_mark_node is returned if the ( and or ) are
4747    missing. NULL_TREE is returned on no expressions. The parentheses
4748    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4749    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4750    indicates whether or not all of the expressions in the list were
4751    constant.  */
4752
4753 static tree
4754 cp_parser_parenthesized_expression_list (cp_parser* parser,
4755                                          bool is_attribute_list,
4756                                          bool cast_p,
4757                                          bool *non_constant_p)
4758 {
4759   tree expression_list = NULL_TREE;
4760   bool fold_expr_p = is_attribute_list;
4761   tree identifier = NULL_TREE;
4762
4763   /* Assume all the expressions will be constant.  */
4764   if (non_constant_p)
4765     *non_constant_p = false;
4766
4767   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4768     return error_mark_node;
4769
4770   /* Consume expressions until there are no more.  */
4771   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4772     while (true)
4773       {
4774         tree expr;
4775
4776         /* At the beginning of attribute lists, check to see if the
4777            next token is an identifier.  */
4778         if (is_attribute_list
4779             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4780           {
4781             cp_token *token;
4782
4783             /* Consume the identifier.  */
4784             token = cp_lexer_consume_token (parser->lexer);
4785             /* Save the identifier.  */
4786             identifier = token->u.value;
4787           }
4788         else
4789           {
4790             /* Parse the next assignment-expression.  */
4791             if (non_constant_p)
4792               {
4793                 bool expr_non_constant_p;
4794                 expr = (cp_parser_constant_expression
4795                         (parser, /*allow_non_constant_p=*/true,
4796                          &expr_non_constant_p));
4797                 if (expr_non_constant_p)
4798                   *non_constant_p = true;
4799               }
4800             else
4801               expr = cp_parser_assignment_expression (parser, cast_p);
4802
4803             if (fold_expr_p)
4804               expr = fold_non_dependent_expr (expr);
4805
4806              /* Add it to the list.  We add error_mark_node
4807                 expressions to the list, so that we can still tell if
4808                 the correct form for a parenthesized expression-list
4809                 is found. That gives better errors.  */
4810             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4811
4812             if (expr == error_mark_node)
4813               goto skip_comma;
4814           }
4815
4816         /* After the first item, attribute lists look the same as
4817            expression lists.  */
4818         is_attribute_list = false;
4819
4820       get_comma:;
4821         /* If the next token isn't a `,', then we are done.  */
4822         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4823           break;
4824
4825         /* Otherwise, consume the `,' and keep going.  */
4826         cp_lexer_consume_token (parser->lexer);
4827       }
4828
4829   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4830     {
4831       int ending;
4832
4833     skip_comma:;
4834       /* We try and resync to an unnested comma, as that will give the
4835          user better diagnostics.  */
4836       ending = cp_parser_skip_to_closing_parenthesis (parser,
4837                                                       /*recovering=*/true,
4838                                                       /*or_comma=*/true,
4839                                                       /*consume_paren=*/true);
4840       if (ending < 0)
4841         goto get_comma;
4842       if (!ending)
4843         return error_mark_node;
4844     }
4845
4846   /* We built up the list in reverse order so we must reverse it now.  */
4847   expression_list = nreverse (expression_list);
4848   if (identifier)
4849     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4850
4851   return expression_list;
4852 }
4853
4854 /* Parse a pseudo-destructor-name.
4855
4856    pseudo-destructor-name:
4857      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4858      :: [opt] nested-name-specifier template template-id :: ~ type-name
4859      :: [opt] nested-name-specifier [opt] ~ type-name
4860
4861    If either of the first two productions is used, sets *SCOPE to the
4862    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4863    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4864    or ERROR_MARK_NODE if the parse fails.  */
4865
4866 static void
4867 cp_parser_pseudo_destructor_name (cp_parser* parser,
4868                                   tree* scope,
4869                                   tree* type)
4870 {
4871   bool nested_name_specifier_p;
4872
4873   /* Assume that things will not work out.  */
4874   *type = error_mark_node;
4875
4876   /* Look for the optional `::' operator.  */
4877   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4878   /* Look for the optional nested-name-specifier.  */
4879   nested_name_specifier_p
4880     = (cp_parser_nested_name_specifier_opt (parser,
4881                                             /*typename_keyword_p=*/false,
4882                                             /*check_dependency_p=*/true,
4883                                             /*type_p=*/false,
4884                                             /*is_declaration=*/true)
4885        != NULL_TREE);
4886   /* Now, if we saw a nested-name-specifier, we might be doing the
4887      second production.  */
4888   if (nested_name_specifier_p
4889       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4890     {
4891       /* Consume the `template' keyword.  */
4892       cp_lexer_consume_token (parser->lexer);
4893       /* Parse the template-id.  */
4894       cp_parser_template_id (parser,
4895                              /*template_keyword_p=*/true,
4896                              /*check_dependency_p=*/false,
4897                              /*is_declaration=*/true);
4898       /* Look for the `::' token.  */
4899       cp_parser_require (parser, CPP_SCOPE, "`::'");
4900     }
4901   /* If the next token is not a `~', then there might be some
4902      additional qualification.  */
4903   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4904     {
4905       /* Look for the type-name.  */
4906       *scope = TREE_TYPE (cp_parser_type_name (parser));
4907
4908       if (*scope == error_mark_node)
4909         return;
4910
4911       /* If we don't have ::~, then something has gone wrong.  Since
4912          the only caller of this function is looking for something
4913          after `.' or `->' after a scalar type, most likely the
4914          program is trying to get a member of a non-aggregate
4915          type.  */
4916       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4917           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4918         {
4919           cp_parser_error (parser, "request for member of non-aggregate type");
4920           return;
4921         }
4922
4923       /* Look for the `::' token.  */
4924       cp_parser_require (parser, CPP_SCOPE, "`::'");
4925     }
4926   else
4927     *scope = NULL_TREE;
4928
4929   /* Look for the `~'.  */
4930   cp_parser_require (parser, CPP_COMPL, "`~'");
4931   /* Look for the type-name again.  We are not responsible for
4932      checking that it matches the first type-name.  */
4933   *type = cp_parser_type_name (parser);
4934 }
4935
4936 /* Parse a unary-expression.
4937
4938    unary-expression:
4939      postfix-expression
4940      ++ cast-expression
4941      -- cast-expression
4942      unary-operator cast-expression
4943      sizeof unary-expression
4944      sizeof ( type-id )
4945      new-expression
4946      delete-expression
4947
4948    GNU Extensions:
4949
4950    unary-expression:
4951      __extension__ cast-expression
4952      __alignof__ unary-expression
4953      __alignof__ ( type-id )
4954      __real__ cast-expression
4955      __imag__ cast-expression
4956      && identifier
4957
4958    ADDRESS_P is true iff the unary-expression is appearing as the
4959    operand of the `&' operator.   CAST_P is true if this expression is
4960    the target of a cast.
4961
4962    Returns a representation of the expression.  */
4963
4964 static tree
4965 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4966 {
4967   cp_token *token;
4968   enum tree_code unary_operator;
4969
4970   /* Peek at the next token.  */
4971   token = cp_lexer_peek_token (parser->lexer);
4972   /* Some keywords give away the kind of expression.  */
4973   if (token->type == CPP_KEYWORD)
4974     {
4975       enum rid keyword = token->keyword;
4976
4977       switch (keyword)
4978         {
4979         case RID_ALIGNOF:
4980         case RID_SIZEOF:
4981           {
4982             tree operand;
4983             enum tree_code op;
4984
4985             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4986             /* Consume the token.  */
4987             cp_lexer_consume_token (parser->lexer);
4988             /* Parse the operand.  */
4989             operand = cp_parser_sizeof_operand (parser, keyword);
4990
4991             if (TYPE_P (operand))
4992               return cxx_sizeof_or_alignof_type (operand, op, true);
4993             else
4994               return cxx_sizeof_or_alignof_expr (operand, op);
4995           }
4996
4997         case RID_NEW:
4998           return cp_parser_new_expression (parser);
4999
5000         case RID_DELETE:
5001           return cp_parser_delete_expression (parser);
5002
5003         case RID_EXTENSION:
5004           {
5005             /* The saved value of the PEDANTIC flag.  */
5006             int saved_pedantic;
5007             tree expr;
5008
5009             /* Save away the PEDANTIC flag.  */
5010             cp_parser_extension_opt (parser, &saved_pedantic);
5011             /* Parse the cast-expression.  */
5012             expr = cp_parser_simple_cast_expression (parser);
5013             /* Restore the PEDANTIC flag.  */
5014             pedantic = saved_pedantic;
5015
5016             return expr;
5017           }
5018
5019         case RID_REALPART:
5020         case RID_IMAGPART:
5021           {
5022             tree expression;
5023
5024             /* Consume the `__real__' or `__imag__' token.  */
5025             cp_lexer_consume_token (parser->lexer);
5026             /* Parse the cast-expression.  */
5027             expression = cp_parser_simple_cast_expression (parser);
5028             /* Create the complete representation.  */
5029             return build_x_unary_op ((keyword == RID_REALPART
5030                                       ? REALPART_EXPR : IMAGPART_EXPR),
5031                                      expression);
5032           }
5033           break;
5034
5035         default:
5036           break;
5037         }
5038     }
5039
5040   /* Look for the `:: new' and `:: delete', which also signal the
5041      beginning of a new-expression, or delete-expression,
5042      respectively.  If the next token is `::', then it might be one of
5043      these.  */
5044   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5045     {
5046       enum rid keyword;
5047
5048       /* See if the token after the `::' is one of the keywords in
5049          which we're interested.  */
5050       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5051       /* If it's `new', we have a new-expression.  */
5052       if (keyword == RID_NEW)
5053         return cp_parser_new_expression (parser);
5054       /* Similarly, for `delete'.  */
5055       else if (keyword == RID_DELETE)
5056         return cp_parser_delete_expression (parser);
5057     }
5058
5059   /* Look for a unary operator.  */
5060   unary_operator = cp_parser_unary_operator (token);
5061   /* The `++' and `--' operators can be handled similarly, even though
5062      they are not technically unary-operators in the grammar.  */
5063   if (unary_operator == ERROR_MARK)
5064     {
5065       if (token->type == CPP_PLUS_PLUS)
5066         unary_operator = PREINCREMENT_EXPR;
5067       else if (token->type == CPP_MINUS_MINUS)
5068         unary_operator = PREDECREMENT_EXPR;
5069       /* Handle the GNU address-of-label extension.  */
5070       else if (cp_parser_allow_gnu_extensions_p (parser)
5071                && token->type == CPP_AND_AND)
5072         {
5073           tree identifier;
5074
5075           /* Consume the '&&' token.  */
5076           cp_lexer_consume_token (parser->lexer);
5077           /* Look for the identifier.  */
5078           identifier = cp_parser_identifier (parser);
5079           /* Create an expression representing the address.  */
5080           return finish_label_address_expr (identifier);
5081         }
5082     }
5083   if (unary_operator != ERROR_MARK)
5084     {
5085       tree cast_expression;
5086       tree expression = error_mark_node;
5087       const char *non_constant_p = NULL;
5088
5089       /* Consume the operator token.  */
5090       token = cp_lexer_consume_token (parser->lexer);
5091       /* Parse the cast-expression.  */
5092       cast_expression
5093         = cp_parser_cast_expression (parser,
5094                                      unary_operator == ADDR_EXPR,
5095                                      /*cast_p=*/false);
5096       /* Now, build an appropriate representation.  */
5097       switch (unary_operator)
5098         {
5099         case INDIRECT_REF:
5100           non_constant_p = "`*'";
5101           expression = build_x_indirect_ref (cast_expression, "unary *");
5102           break;
5103
5104         case ADDR_EXPR:
5105           non_constant_p = "`&'";
5106           /* Fall through.  */
5107         case BIT_NOT_EXPR:
5108           expression = build_x_unary_op (unary_operator, cast_expression);
5109           break;
5110
5111         case PREINCREMENT_EXPR:
5112         case PREDECREMENT_EXPR:
5113           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5114                             ? "`++'" : "`--'");
5115           /* Fall through.  */
5116         case UNARY_PLUS_EXPR:
5117         case NEGATE_EXPR:
5118         case TRUTH_NOT_EXPR:
5119           expression = finish_unary_op_expr (unary_operator, cast_expression);
5120           break;
5121
5122         default:
5123           gcc_unreachable ();
5124         }
5125
5126       if (non_constant_p
5127           && cp_parser_non_integral_constant_expression (parser,
5128                                                          non_constant_p))
5129         expression = error_mark_node;
5130
5131       return expression;
5132     }
5133
5134   return cp_parser_postfix_expression (parser, address_p, cast_p);
5135 }
5136
5137 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5138    unary-operator, the corresponding tree code is returned.  */
5139
5140 static enum tree_code
5141 cp_parser_unary_operator (cp_token* token)
5142 {
5143   switch (token->type)
5144     {
5145     case CPP_MULT:
5146       return INDIRECT_REF;
5147
5148     case CPP_AND:
5149       return ADDR_EXPR;
5150
5151     case CPP_PLUS:
5152       return UNARY_PLUS_EXPR;
5153
5154     case CPP_MINUS:
5155       return NEGATE_EXPR;
5156
5157     case CPP_NOT:
5158       return TRUTH_NOT_EXPR;
5159
5160     case CPP_COMPL:
5161       return BIT_NOT_EXPR;
5162
5163     default:
5164       return ERROR_MARK;
5165     }
5166 }
5167
5168 /* Parse a new-expression.
5169
5170    new-expression:
5171      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5172      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5173
5174    Returns a representation of the expression.  */
5175
5176 static tree
5177 cp_parser_new_expression (cp_parser* parser)
5178 {
5179   bool global_scope_p;
5180   tree placement;
5181   tree type;
5182   tree initializer;
5183   tree nelts;
5184
5185   /* Look for the optional `::' operator.  */
5186   global_scope_p
5187     = (cp_parser_global_scope_opt (parser,
5188                                    /*current_scope_valid_p=*/false)
5189        != NULL_TREE);
5190   /* Look for the `new' operator.  */
5191   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5192   /* There's no easy way to tell a new-placement from the
5193      `( type-id )' construct.  */
5194   cp_parser_parse_tentatively (parser);
5195   /* Look for a new-placement.  */
5196   placement = cp_parser_new_placement (parser);
5197   /* If that didn't work out, there's no new-placement.  */
5198   if (!cp_parser_parse_definitely (parser))
5199     placement = NULL_TREE;
5200
5201   /* If the next token is a `(', then we have a parenthesized
5202      type-id.  */
5203   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5204     {
5205       /* Consume the `('.  */
5206       cp_lexer_consume_token (parser->lexer);
5207       /* Parse the type-id.  */
5208       type = cp_parser_type_id (parser);
5209       /* Look for the closing `)'.  */
5210       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5211       /* There should not be a direct-new-declarator in this production,
5212          but GCC used to allowed this, so we check and emit a sensible error
5213          message for this case.  */
5214       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5215         {
5216           error ("array bound forbidden after parenthesized type-id");
5217           inform ("try removing the parentheses around the type-id");
5218           cp_parser_direct_new_declarator (parser);
5219         }
5220       nelts = NULL_TREE;
5221     }
5222   /* Otherwise, there must be a new-type-id.  */
5223   else
5224     type = cp_parser_new_type_id (parser, &nelts);
5225
5226   /* If the next token is a `(', then we have a new-initializer.  */
5227   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5228     initializer = cp_parser_new_initializer (parser);
5229   else
5230     initializer = NULL_TREE;
5231
5232   /* A new-expression may not appear in an integral constant
5233      expression.  */
5234   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5235     return error_mark_node;
5236
5237   /* Create a representation of the new-expression.  */
5238   return build_new (placement, type, nelts, initializer, global_scope_p);
5239 }
5240
5241 /* Parse a new-placement.
5242
5243    new-placement:
5244      ( expression-list )
5245
5246    Returns the same representation as for an expression-list.  */
5247
5248 static tree
5249 cp_parser_new_placement (cp_parser* parser)
5250 {
5251   tree expression_list;
5252
5253   /* Parse the expression-list.  */
5254   expression_list = (cp_parser_parenthesized_expression_list
5255                      (parser, false, /*cast_p=*/false,
5256                       /*non_constant_p=*/NULL));
5257
5258   return expression_list;
5259 }
5260
5261 /* Parse a new-type-id.
5262
5263    new-type-id:
5264      type-specifier-seq new-declarator [opt]
5265
5266    Returns the TYPE allocated.  If the new-type-id indicates an array
5267    type, *NELTS is set to the number of elements in the last array
5268    bound; the TYPE will not include the last array bound.  */
5269
5270 static tree
5271 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5272 {
5273   cp_decl_specifier_seq type_specifier_seq;
5274   cp_declarator *new_declarator;
5275   cp_declarator *declarator;
5276   cp_declarator *outer_declarator;
5277   const char *saved_message;
5278   tree type;
5279
5280   /* The type-specifier sequence must not contain type definitions.
5281      (It cannot contain declarations of new types either, but if they
5282      are not definitions we will catch that because they are not
5283      complete.)  */
5284   saved_message = parser->type_definition_forbidden_message;
5285   parser->type_definition_forbidden_message
5286     = "types may not be defined in a new-type-id";
5287   /* Parse the type-specifier-seq.  */
5288   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5289                                 &type_specifier_seq);
5290   /* Restore the old message.  */
5291   parser->type_definition_forbidden_message = saved_message;
5292   /* Parse the new-declarator.  */
5293   new_declarator = cp_parser_new_declarator_opt (parser);
5294
5295   /* Determine the number of elements in the last array dimension, if
5296      any.  */
5297   *nelts = NULL_TREE;
5298   /* Skip down to the last array dimension.  */
5299   declarator = new_declarator;
5300   outer_declarator = NULL;
5301   while (declarator && (declarator->kind == cdk_pointer
5302                         || declarator->kind == cdk_ptrmem))
5303     {
5304       outer_declarator = declarator;
5305       declarator = declarator->declarator;
5306     }
5307   while (declarator
5308          && declarator->kind == cdk_array
5309          && declarator->declarator
5310          && declarator->declarator->kind == cdk_array)
5311     {
5312       outer_declarator = declarator;
5313       declarator = declarator->declarator;
5314     }
5315
5316   if (declarator && declarator->kind == cdk_array)
5317     {
5318       *nelts = declarator->u.array.bounds;
5319       if (*nelts == error_mark_node)
5320         *nelts = integer_one_node;
5321
5322       if (outer_declarator)
5323         outer_declarator->declarator = declarator->declarator;
5324       else
5325         new_declarator = NULL;
5326     }
5327
5328   type = groktypename (&type_specifier_seq, new_declarator);
5329   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5330     {
5331       *nelts = array_type_nelts_top (type);
5332       type = TREE_TYPE (type);
5333     }
5334   return type;
5335 }
5336
5337 /* Parse an (optional) new-declarator.
5338
5339    new-declarator:
5340      ptr-operator new-declarator [opt]
5341      direct-new-declarator
5342
5343    Returns the declarator.  */
5344
5345 static cp_declarator *
5346 cp_parser_new_declarator_opt (cp_parser* parser)
5347 {
5348   enum tree_code code;
5349   tree type;
5350   cp_cv_quals cv_quals;
5351
5352   /* We don't know if there's a ptr-operator next, or not.  */
5353   cp_parser_parse_tentatively (parser);
5354   /* Look for a ptr-operator.  */
5355   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5356   /* If that worked, look for more new-declarators.  */
5357   if (cp_parser_parse_definitely (parser))
5358     {
5359       cp_declarator *declarator;
5360
5361       /* Parse another optional declarator.  */
5362       declarator = cp_parser_new_declarator_opt (parser);
5363
5364       /* Create the representation of the declarator.  */
5365       if (type)
5366         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5367       else if (code == INDIRECT_REF)
5368         declarator = make_pointer_declarator (cv_quals, declarator);
5369       else
5370         declarator = make_reference_declarator (cv_quals, declarator);
5371
5372       return declarator;
5373     }
5374
5375   /* If the next token is a `[', there is a direct-new-declarator.  */
5376   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5377     return cp_parser_direct_new_declarator (parser);
5378
5379   return NULL;
5380 }
5381
5382 /* Parse a direct-new-declarator.
5383
5384    direct-new-declarator:
5385      [ expression ]
5386      direct-new-declarator [constant-expression]
5387
5388    */
5389
5390 static cp_declarator *
5391 cp_parser_direct_new_declarator (cp_parser* parser)
5392 {
5393   cp_declarator *declarator = NULL;
5394
5395   while (true)
5396     {
5397       tree expression;
5398
5399       /* Look for the opening `['.  */
5400       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5401       /* The first expression is not required to be constant.  */
5402       if (!declarator)
5403         {
5404           expression = cp_parser_expression (parser, /*cast_p=*/false);
5405           /* The standard requires that the expression have integral
5406              type.  DR 74 adds enumeration types.  We believe that the
5407              real intent is that these expressions be handled like the
5408              expression in a `switch' condition, which also allows
5409              classes with a single conversion to integral or
5410              enumeration type.  */
5411           if (!processing_template_decl)
5412             {
5413               expression
5414                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5415                                               expression,
5416                                               /*complain=*/true);
5417               if (!expression)
5418                 {
5419                   error ("expression in new-declarator must have integral "
5420                          "or enumeration type");
5421                   expression = error_mark_node;
5422                 }
5423             }
5424         }
5425       /* But all the other expressions must be.  */
5426       else
5427         expression
5428           = cp_parser_constant_expression (parser,
5429                                            /*allow_non_constant=*/false,
5430                                            NULL);
5431       /* Look for the closing `]'.  */
5432       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5433
5434       /* Add this bound to the declarator.  */
5435       declarator = make_array_declarator (declarator, expression);
5436
5437       /* If the next token is not a `[', then there are no more
5438          bounds.  */
5439       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5440         break;
5441     }
5442
5443   return declarator;
5444 }
5445
5446 /* Parse a new-initializer.
5447
5448    new-initializer:
5449      ( expression-list [opt] )
5450
5451    Returns a representation of the expression-list.  If there is no
5452    expression-list, VOID_ZERO_NODE is returned.  */
5453
5454 static tree
5455 cp_parser_new_initializer (cp_parser* parser)
5456 {
5457   tree expression_list;
5458
5459   expression_list = (cp_parser_parenthesized_expression_list
5460                      (parser, false, /*cast_p=*/false,
5461                       /*non_constant_p=*/NULL));
5462   if (!expression_list)
5463     expression_list = void_zero_node;
5464
5465   return expression_list;
5466 }
5467
5468 /* Parse a delete-expression.
5469
5470    delete-expression:
5471      :: [opt] delete cast-expression
5472      :: [opt] delete [ ] cast-expression
5473
5474    Returns a representation of the expression.  */
5475
5476 static tree
5477 cp_parser_delete_expression (cp_parser* parser)
5478 {
5479   bool global_scope_p;
5480   bool array_p;
5481   tree expression;
5482
5483   /* Look for the optional `::' operator.  */
5484   global_scope_p
5485     = (cp_parser_global_scope_opt (parser,
5486                                    /*current_scope_valid_p=*/false)
5487        != NULL_TREE);
5488   /* Look for the `delete' keyword.  */
5489   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5490   /* See if the array syntax is in use.  */
5491   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5492     {
5493       /* Consume the `[' token.  */
5494       cp_lexer_consume_token (parser->lexer);
5495       /* Look for the `]' token.  */
5496       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5497       /* Remember that this is the `[]' construct.  */
5498       array_p = true;
5499     }
5500   else
5501     array_p = false;
5502
5503   /* Parse the cast-expression.  */
5504   expression = cp_parser_simple_cast_expression (parser);
5505
5506   /* A delete-expression may not appear in an integral constant
5507      expression.  */
5508   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5509     return error_mark_node;
5510
5511   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5512 }
5513
5514 /* Parse a cast-expression.
5515
5516    cast-expression:
5517      unary-expression
5518      ( type-id ) cast-expression
5519
5520    ADDRESS_P is true iff the unary-expression is appearing as the
5521    operand of the `&' operator.   CAST_P is true if this expression is
5522    the target of a cast.
5523
5524    Returns a representation of the expression.  */
5525
5526 static tree
5527 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5528 {
5529   /* If it's a `(', then we might be looking at a cast.  */
5530   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5531     {
5532       tree type = NULL_TREE;
5533       tree expr = NULL_TREE;
5534       bool compound_literal_p;
5535       const char *saved_message;
5536
5537       /* There's no way to know yet whether or not this is a cast.
5538          For example, `(int (3))' is a unary-expression, while `(int)
5539          3' is a cast.  So, we resort to parsing tentatively.  */
5540       cp_parser_parse_tentatively (parser);
5541       /* Types may not be defined in a cast.  */
5542       saved_message = parser->type_definition_forbidden_message;
5543       parser->type_definition_forbidden_message
5544         = "types may not be defined in casts";
5545       /* Consume the `('.  */
5546       cp_lexer_consume_token (parser->lexer);
5547       /* A very tricky bit is that `(struct S) { 3 }' is a
5548          compound-literal (which we permit in C++ as an extension).
5549          But, that construct is not a cast-expression -- it is a
5550          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5551          is legal; if the compound-literal were a cast-expression,
5552          you'd need an extra set of parentheses.)  But, if we parse
5553          the type-id, and it happens to be a class-specifier, then we
5554          will commit to the parse at that point, because we cannot
5555          undo the action that is done when creating a new class.  So,
5556          then we cannot back up and do a postfix-expression.
5557
5558          Therefore, we scan ahead to the closing `)', and check to see
5559          if the token after the `)' is a `{'.  If so, we are not
5560          looking at a cast-expression.
5561
5562          Save tokens so that we can put them back.  */
5563       cp_lexer_save_tokens (parser->lexer);
5564       /* Skip tokens until the next token is a closing parenthesis.
5565          If we find the closing `)', and the next token is a `{', then
5566          we are looking at a compound-literal.  */
5567       compound_literal_p
5568         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5569                                                   /*consume_paren=*/true)
5570            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5571       /* Roll back the tokens we skipped.  */
5572       cp_lexer_rollback_tokens (parser->lexer);
5573       /* If we were looking at a compound-literal, simulate an error
5574          so that the call to cp_parser_parse_definitely below will
5575          fail.  */
5576       if (compound_literal_p)
5577         cp_parser_simulate_error (parser);
5578       else
5579         {
5580           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5581           parser->in_type_id_in_expr_p = true;
5582           /* Look for the type-id.  */
5583           type = cp_parser_type_id (parser);
5584           /* Look for the closing `)'.  */
5585           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5586           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5587         }
5588
5589       /* Restore the saved message.  */
5590       parser->type_definition_forbidden_message = saved_message;
5591
5592       /* If ok so far, parse the dependent expression. We cannot be
5593          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5594          ctor of T, but looks like a cast to function returning T
5595          without a dependent expression.  */
5596       if (!cp_parser_error_occurred (parser))
5597         expr = cp_parser_cast_expression (parser,
5598                                           /*address_p=*/false,
5599                                           /*cast_p=*/true);
5600
5601       if (cp_parser_parse_definitely (parser))
5602         {
5603           /* Warn about old-style casts, if so requested.  */
5604           if (warn_old_style_cast
5605               && !in_system_header
5606               && !VOID_TYPE_P (type)
5607               && current_lang_name != lang_name_c)
5608             warning (OPT_Wold_style_cast, "use of old-style cast");
5609
5610           /* Only type conversions to integral or enumeration types
5611              can be used in constant-expressions.  */
5612           if (!cast_valid_in_integral_constant_expression_p (type)
5613               && (cp_parser_non_integral_constant_expression
5614                   (parser,
5615                    "a cast to a type other than an integral or "
5616                    "enumeration type")))
5617             return error_mark_node;
5618
5619           /* Perform the cast.  */
5620           expr = build_c_cast (type, expr);
5621           return expr;
5622         }
5623     }
5624
5625   /* If we get here, then it's not a cast, so it must be a
5626      unary-expression.  */
5627   return cp_parser_unary_expression (parser, address_p, cast_p);
5628 }
5629
5630 /* Parse a binary expression of the general form:
5631
5632    pm-expression:
5633      cast-expression
5634      pm-expression .* cast-expression
5635      pm-expression ->* cast-expression
5636
5637    multiplicative-expression:
5638      pm-expression
5639      multiplicative-expression * pm-expression
5640      multiplicative-expression / pm-expression
5641      multiplicative-expression % pm-expression
5642
5643    additive-expression:
5644      multiplicative-expression
5645      additive-expression + multiplicative-expression
5646      additive-expression - multiplicative-expression
5647
5648    shift-expression:
5649      additive-expression
5650      shift-expression << additive-expression
5651      shift-expression >> additive-expression
5652
5653    relational-expression:
5654      shift-expression
5655      relational-expression < shift-expression
5656      relational-expression > shift-expression
5657      relational-expression <= shift-expression
5658      relational-expression >= shift-expression
5659
5660   GNU Extension:
5661
5662    relational-expression:
5663      relational-expression <? shift-expression
5664      relational-expression >? shift-expression
5665
5666    equality-expression:
5667      relational-expression
5668      equality-expression == relational-expression
5669      equality-expression != relational-expression
5670
5671    and-expression:
5672      equality-expression
5673      and-expression & equality-expression
5674
5675    exclusive-or-expression:
5676      and-expression
5677      exclusive-or-expression ^ and-expression
5678
5679    inclusive-or-expression:
5680      exclusive-or-expression
5681      inclusive-or-expression | exclusive-or-expression
5682
5683    logical-and-expression:
5684      inclusive-or-expression
5685      logical-and-expression && inclusive-or-expression
5686
5687    logical-or-expression:
5688      logical-and-expression
5689      logical-or-expression || logical-and-expression
5690
5691    All these are implemented with a single function like:
5692
5693    binary-expression:
5694      simple-cast-expression
5695      binary-expression <token> binary-expression
5696
5697    CAST_P is true if this expression is the target of a cast.
5698
5699    The binops_by_token map is used to get the tree codes for each <token> type.
5700    binary-expressions are associated according to a precedence table.  */
5701
5702 #define TOKEN_PRECEDENCE(token) \
5703   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5704    ? PREC_NOT_OPERATOR \
5705    : binops_by_token[token->type].prec)
5706
5707 static tree
5708 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5709 {
5710   cp_parser_expression_stack stack;
5711   cp_parser_expression_stack_entry *sp = &stack[0];
5712   tree lhs, rhs;
5713   cp_token *token;
5714   enum tree_code tree_type, lhs_type, rhs_type;
5715   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5716   bool overloaded_p;
5717
5718   /* Parse the first expression.  */
5719   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5720   lhs_type = ERROR_MARK;
5721
5722   for (;;)
5723     {
5724       /* Get an operator token.  */
5725       token = cp_lexer_peek_token (parser->lexer);
5726
5727       new_prec = TOKEN_PRECEDENCE (token);
5728
5729       /* Popping an entry off the stack means we completed a subexpression:
5730          - either we found a token which is not an operator (`>' where it is not
5731            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5732            will happen repeatedly;
5733          - or, we found an operator which has lower priority.  This is the case
5734            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5735            parsing `3 * 4'.  */
5736       if (new_prec <= prec)
5737         {
5738           if (sp == stack)
5739             break;
5740           else
5741             goto pop;
5742         }
5743
5744      get_rhs:
5745       tree_type = binops_by_token[token->type].tree_type;
5746
5747       /* We used the operator token.  */
5748       cp_lexer_consume_token (parser->lexer);
5749
5750       /* Extract another operand.  It may be the RHS of this expression
5751          or the LHS of a new, higher priority expression.  */
5752       rhs = cp_parser_simple_cast_expression (parser);
5753       rhs_type = ERROR_MARK;
5754
5755       /* Get another operator token.  Look up its precedence to avoid
5756          building a useless (immediately popped) stack entry for common
5757          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5758       token = cp_lexer_peek_token (parser->lexer);
5759       lookahead_prec = TOKEN_PRECEDENCE (token);
5760       if (lookahead_prec > new_prec)
5761         {
5762           /* ... and prepare to parse the RHS of the new, higher priority
5763              expression.  Since precedence levels on the stack are
5764              monotonically increasing, we do not have to care about
5765              stack overflows.  */
5766           sp->prec = prec;
5767           sp->tree_type = tree_type;
5768           sp->lhs = lhs;
5769           sp->lhs_type = lhs_type;
5770           sp++;
5771           lhs = rhs;
5772           lhs_type = rhs_type;
5773           prec = new_prec;
5774           new_prec = lookahead_prec;
5775           goto get_rhs;
5776
5777          pop:
5778           /* If the stack is not empty, we have parsed into LHS the right side
5779              (`4' in the example above) of an expression we had suspended.
5780              We can use the information on the stack to recover the LHS (`3')
5781              from the stack together with the tree code (`MULT_EXPR'), and
5782              the precedence of the higher level subexpression
5783              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5784              which will be used to actually build the additive expression.  */
5785           --sp;
5786           prec = sp->prec;
5787           tree_type = sp->tree_type;
5788           rhs = lhs;
5789           rhs_type = lhs_type;
5790           lhs = sp->lhs;
5791           lhs_type = sp->lhs_type;
5792         }
5793
5794       overloaded_p = false;
5795       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5796                                &overloaded_p);
5797       lhs_type = tree_type;
5798
5799       /* If the binary operator required the use of an overloaded operator,
5800          then this expression cannot be an integral constant-expression.
5801          An overloaded operator can be used even if both operands are
5802          otherwise permissible in an integral constant-expression if at
5803          least one of the operands is of enumeration type.  */
5804
5805       if (overloaded_p
5806           && (cp_parser_non_integral_constant_expression
5807               (parser, "calls to overloaded operators")))
5808         return error_mark_node;
5809     }
5810
5811   return lhs;
5812 }
5813
5814
5815 /* Parse the `? expression : assignment-expression' part of a
5816    conditional-expression.  The LOGICAL_OR_EXPR is the
5817    logical-or-expression that started the conditional-expression.
5818    Returns a representation of the entire conditional-expression.
5819
5820    This routine is used by cp_parser_assignment_expression.
5821
5822      ? expression : assignment-expression
5823
5824    GNU Extensions:
5825
5826      ? : assignment-expression */
5827
5828 static tree
5829 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5830 {
5831   tree expr;
5832   tree assignment_expr;
5833
5834   /* Consume the `?' token.  */
5835   cp_lexer_consume_token (parser->lexer);
5836   if (cp_parser_allow_gnu_extensions_p (parser)
5837       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5838     /* Implicit true clause.  */
5839     expr = NULL_TREE;
5840   else
5841     /* Parse the expression.  */
5842     expr = cp_parser_expression (parser, /*cast_p=*/false);
5843
5844   /* The next token should be a `:'.  */
5845   cp_parser_require (parser, CPP_COLON, "`:'");
5846   /* Parse the assignment-expression.  */
5847   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5848
5849   /* Build the conditional-expression.  */
5850   return build_x_conditional_expr (logical_or_expr,
5851                                    expr,
5852                                    assignment_expr);
5853 }
5854
5855 /* Parse an assignment-expression.
5856
5857    assignment-expression:
5858      conditional-expression
5859      logical-or-expression assignment-operator assignment_expression
5860      throw-expression
5861
5862    CAST_P is true if this expression is the target of a cast.
5863
5864    Returns a representation for the expression.  */
5865
5866 static tree
5867 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5868 {
5869   tree expr;
5870
5871   /* If the next token is the `throw' keyword, then we're looking at
5872      a throw-expression.  */
5873   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5874     expr = cp_parser_throw_expression (parser);
5875   /* Otherwise, it must be that we are looking at a
5876      logical-or-expression.  */
5877   else
5878     {
5879       /* Parse the binary expressions (logical-or-expression).  */
5880       expr = cp_parser_binary_expression (parser, cast_p);
5881       /* If the next token is a `?' then we're actually looking at a
5882          conditional-expression.  */
5883       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5884         return cp_parser_question_colon_clause (parser, expr);
5885       else
5886         {
5887           enum tree_code assignment_operator;
5888
5889           /* If it's an assignment-operator, we're using the second
5890              production.  */
5891           assignment_operator
5892             = cp_parser_assignment_operator_opt (parser);
5893           if (assignment_operator != ERROR_MARK)
5894             {
5895               tree rhs;
5896
5897               /* Parse the right-hand side of the assignment.  */
5898               rhs = cp_parser_assignment_expression (parser, cast_p);
5899               /* An assignment may not appear in a
5900                  constant-expression.  */
5901               if (cp_parser_non_integral_constant_expression (parser,
5902                                                               "an assignment"))
5903                 return error_mark_node;
5904               /* Build the assignment expression.  */
5905               expr = build_x_modify_expr (expr,
5906                                           assignment_operator,
5907                                           rhs);
5908             }
5909         }
5910     }
5911
5912   return expr;
5913 }
5914
5915 /* Parse an (optional) assignment-operator.
5916
5917    assignment-operator: one of
5918      = *= /= %= += -= >>= <<= &= ^= |=
5919
5920    GNU Extension:
5921
5922    assignment-operator: one of
5923      <?= >?=
5924
5925    If the next token is an assignment operator, the corresponding tree
5926    code is returned, and the token is consumed.  For example, for
5927    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5928    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5929    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5930    operator, ERROR_MARK is returned.  */
5931
5932 static enum tree_code
5933 cp_parser_assignment_operator_opt (cp_parser* parser)
5934 {
5935   enum tree_code op;
5936   cp_token *token;
5937
5938   /* Peek at the next toen.  */
5939   token = cp_lexer_peek_token (parser->lexer);
5940
5941   switch (token->type)
5942     {
5943     case CPP_EQ:
5944       op = NOP_EXPR;
5945       break;
5946
5947     case CPP_MULT_EQ:
5948       op = MULT_EXPR;
5949       break;
5950
5951     case CPP_DIV_EQ:
5952       op = TRUNC_DIV_EXPR;
5953       break;
5954
5955     case CPP_MOD_EQ:
5956       op = TRUNC_MOD_EXPR;
5957       break;
5958
5959     case CPP_PLUS_EQ:
5960       op = PLUS_EXPR;
5961       break;
5962
5963     case CPP_MINUS_EQ:
5964       op = MINUS_EXPR;
5965       break;
5966
5967     case CPP_RSHIFT_EQ:
5968       op = RSHIFT_EXPR;
5969       break;
5970
5971     case CPP_LSHIFT_EQ:
5972       op = LSHIFT_EXPR;
5973       break;
5974
5975     case CPP_AND_EQ:
5976       op = BIT_AND_EXPR;
5977       break;
5978
5979     case CPP_XOR_EQ:
5980       op = BIT_XOR_EXPR;
5981       break;
5982
5983     case CPP_OR_EQ:
5984       op = BIT_IOR_EXPR;
5985       break;
5986
5987     default:
5988       /* Nothing else is an assignment operator.  */
5989       op = ERROR_MARK;
5990     }
5991
5992   /* If it was an assignment operator, consume it.  */
5993   if (op != ERROR_MARK)
5994     cp_lexer_consume_token (parser->lexer);
5995
5996   return op;
5997 }
5998
5999 /* Parse an expression.
6000
6001    expression:
6002      assignment-expression
6003      expression , assignment-expression
6004
6005    CAST_P is true if this expression is the target of a cast.
6006
6007    Returns a representation of the expression.  */
6008
6009 static tree
6010 cp_parser_expression (cp_parser* parser, bool cast_p)
6011 {
6012   tree expression = NULL_TREE;
6013
6014   while (true)
6015     {
6016       tree assignment_expression;
6017
6018       /* Parse the next assignment-expression.  */
6019       assignment_expression
6020         = cp_parser_assignment_expression (parser, cast_p);
6021       /* If this is the first assignment-expression, we can just
6022          save it away.  */
6023       if (!expression)
6024         expression = assignment_expression;
6025       else
6026         expression = build_x_compound_expr (expression,
6027                                             assignment_expression);
6028       /* If the next token is not a comma, then we are done with the
6029          expression.  */
6030       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6031         break;
6032       /* Consume the `,'.  */
6033       cp_lexer_consume_token (parser->lexer);
6034       /* A comma operator cannot appear in a constant-expression.  */
6035       if (cp_parser_non_integral_constant_expression (parser,
6036                                                       "a comma operator"))
6037         expression = error_mark_node;
6038     }
6039
6040   return expression;
6041 }
6042
6043 /* Parse a constant-expression.
6044
6045    constant-expression:
6046      conditional-expression
6047
6048   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6049   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6050   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6051   is false, NON_CONSTANT_P should be NULL.  */
6052
6053 static tree
6054 cp_parser_constant_expression (cp_parser* parser,
6055                                bool allow_non_constant_p,
6056                                bool *non_constant_p)
6057 {
6058   bool saved_integral_constant_expression_p;
6059   bool saved_allow_non_integral_constant_expression_p;
6060   bool saved_non_integral_constant_expression_p;
6061   tree expression;
6062
6063   /* It might seem that we could simply parse the
6064      conditional-expression, and then check to see if it were
6065      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6066      one that the compiler can figure out is constant, possibly after
6067      doing some simplifications or optimizations.  The standard has a
6068      precise definition of constant-expression, and we must honor
6069      that, even though it is somewhat more restrictive.
6070
6071      For example:
6072
6073        int i[(2, 3)];
6074
6075      is not a legal declaration, because `(2, 3)' is not a
6076      constant-expression.  The `,' operator is forbidden in a
6077      constant-expression.  However, GCC's constant-folding machinery
6078      will fold this operation to an INTEGER_CST for `3'.  */
6079
6080   /* Save the old settings.  */
6081   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6082   saved_allow_non_integral_constant_expression_p
6083     = parser->allow_non_integral_constant_expression_p;
6084   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6085   /* We are now parsing a constant-expression.  */
6086   parser->integral_constant_expression_p = true;
6087   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6088   parser->non_integral_constant_expression_p = false;
6089   /* Although the grammar says "conditional-expression", we parse an
6090      "assignment-expression", which also permits "throw-expression"
6091      and the use of assignment operators.  In the case that
6092      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6093      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6094      actually essential that we look for an assignment-expression.
6095      For example, cp_parser_initializer_clauses uses this function to
6096      determine whether a particular assignment-expression is in fact
6097      constant.  */
6098   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6099   /* Restore the old settings.  */
6100   parser->integral_constant_expression_p
6101     = saved_integral_constant_expression_p;
6102   parser->allow_non_integral_constant_expression_p
6103     = saved_allow_non_integral_constant_expression_p;
6104   if (allow_non_constant_p)
6105     *non_constant_p = parser->non_integral_constant_expression_p;
6106   else if (parser->non_integral_constant_expression_p)
6107     expression = error_mark_node;
6108   parser->non_integral_constant_expression_p
6109     = saved_non_integral_constant_expression_p;
6110
6111   return expression;
6112 }
6113
6114 /* Parse __builtin_offsetof.
6115
6116    offsetof-expression:
6117      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6118
6119    offsetof-member-designator:
6120      id-expression
6121      | offsetof-member-designator "." id-expression
6122      | offsetof-member-designator "[" expression "]"  */
6123
6124 static tree
6125 cp_parser_builtin_offsetof (cp_parser *parser)
6126 {
6127   int save_ice_p, save_non_ice_p;
6128   tree type, expr;
6129   cp_id_kind dummy;
6130
6131   /* We're about to accept non-integral-constant things, but will
6132      definitely yield an integral constant expression.  Save and
6133      restore these values around our local parsing.  */
6134   save_ice_p = parser->integral_constant_expression_p;
6135   save_non_ice_p = parser->non_integral_constant_expression_p;
6136
6137   /* Consume the "__builtin_offsetof" token.  */
6138   cp_lexer_consume_token (parser->lexer);
6139   /* Consume the opening `('.  */
6140   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6141   /* Parse the type-id.  */
6142   type = cp_parser_type_id (parser);
6143   /* Look for the `,'.  */
6144   cp_parser_require (parser, CPP_COMMA, "`,'");
6145
6146   /* Build the (type *)null that begins the traditional offsetof macro.  */
6147   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6148
6149   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6150   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6151                                                  true, &dummy);
6152   while (true)
6153     {
6154       cp_token *token = cp_lexer_peek_token (parser->lexer);
6155       switch (token->type)
6156         {
6157         case CPP_OPEN_SQUARE:
6158           /* offsetof-member-designator "[" expression "]" */
6159           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6160           break;
6161
6162         case CPP_DOT:
6163           /* offsetof-member-designator "." identifier */
6164           cp_lexer_consume_token (parser->lexer);
6165           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6166                                                          true, &dummy);
6167           break;
6168
6169         case CPP_CLOSE_PAREN:
6170           /* Consume the ")" token.  */
6171           cp_lexer_consume_token (parser->lexer);
6172           goto success;
6173
6174         default:
6175           /* Error.  We know the following require will fail, but
6176              that gives the proper error message.  */
6177           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6178           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6179           expr = error_mark_node;
6180           goto failure;
6181         }
6182     }
6183
6184  success:
6185   /* If we're processing a template, we can't finish the semantics yet.
6186      Otherwise we can fold the entire expression now.  */
6187   if (processing_template_decl)
6188     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6189   else
6190     expr = finish_offsetof (expr);
6191
6192  failure:
6193   parser->integral_constant_expression_p = save_ice_p;
6194   parser->non_integral_constant_expression_p = save_non_ice_p;
6195
6196   return expr;
6197 }
6198
6199 /* Statements [gram.stmt.stmt]  */
6200
6201 /* Parse a statement.
6202
6203    statement:
6204      labeled-statement
6205      expression-statement
6206      compound-statement
6207      selection-statement
6208      iteration-statement
6209      jump-statement
6210      declaration-statement
6211      try-block
6212
6213   IN_COMPOUND is true when the statement is nested inside a
6214   cp_parser_compound_statement; this matters for certain pragmas.
6215
6216   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6217   is a (possibly labeled) if statement which is not enclosed in braces
6218   and has an else clause.  This is used to implement -Wparentheses.  */
6219
6220 static void
6221 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6222                      bool in_compound, bool *if_p)
6223 {
6224   tree statement;
6225   cp_token *token;
6226   location_t statement_location;
6227
6228  restart:
6229   if (if_p != NULL)
6230     *if_p = false;
6231   /* There is no statement yet.  */
6232   statement = NULL_TREE;
6233   /* Peek at the next token.  */
6234   token = cp_lexer_peek_token (parser->lexer);
6235   /* Remember the location of the first token in the statement.  */
6236   statement_location = token->location;
6237   /* If this is a keyword, then that will often determine what kind of
6238      statement we have.  */
6239   if (token->type == CPP_KEYWORD)
6240     {
6241       enum rid keyword = token->keyword;
6242
6243       switch (keyword)
6244         {
6245         case RID_CASE:
6246         case RID_DEFAULT:
6247           /* Looks like a labeled-statement with a case label.
6248              Parse the label, and then use tail recursion to parse
6249              the statement.  */
6250           cp_parser_label_for_labeled_statement (parser);
6251           goto restart;
6252
6253         case RID_IF:
6254         case RID_SWITCH:
6255           statement = cp_parser_selection_statement (parser, if_p);
6256           break;
6257
6258         case RID_WHILE:
6259         case RID_DO:
6260         case RID_FOR:
6261           statement = cp_parser_iteration_statement (parser);
6262           break;
6263
6264         case RID_BREAK:
6265         case RID_CONTINUE:
6266         case RID_RETURN:
6267         case RID_GOTO:
6268           statement = cp_parser_jump_statement (parser);
6269           break;
6270
6271           /* Objective-C++ exception-handling constructs.  */
6272         case RID_AT_TRY:
6273         case RID_AT_CATCH:
6274         case RID_AT_FINALLY:
6275         case RID_AT_SYNCHRONIZED:
6276         case RID_AT_THROW:
6277           statement = cp_parser_objc_statement (parser);
6278           break;
6279
6280         case RID_TRY:
6281           statement = cp_parser_try_block (parser);
6282           break;
6283
6284         default:
6285           /* It might be a keyword like `int' that can start a
6286              declaration-statement.  */
6287           break;
6288         }
6289     }
6290   else if (token->type == CPP_NAME)
6291     {
6292       /* If the next token is a `:', then we are looking at a
6293          labeled-statement.  */
6294       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6295       if (token->type == CPP_COLON)
6296         {
6297           /* Looks like a labeled-statement with an ordinary label.
6298              Parse the label, and then use tail recursion to parse
6299              the statement.  */
6300           cp_parser_label_for_labeled_statement (parser);
6301           goto restart;
6302         }
6303     }
6304   /* Anything that starts with a `{' must be a compound-statement.  */
6305   else if (token->type == CPP_OPEN_BRACE)
6306     statement = cp_parser_compound_statement (parser, NULL, false);
6307   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6308      a statement all its own.  */
6309   else if (token->type == CPP_PRAGMA)
6310     {
6311       /* Only certain OpenMP pragmas are attached to statements, and thus
6312          are considered statements themselves.  All others are not.  In
6313          the context of a compound, accept the pragma as a "statement" and
6314          return so that we can check for a close brace.  Otherwise we
6315          require a real statement and must go back and read one.  */
6316       if (in_compound)
6317         cp_parser_pragma (parser, pragma_compound);
6318       else if (!cp_parser_pragma (parser, pragma_stmt))
6319         goto restart;
6320       return;
6321     }
6322   else if (token->type == CPP_EOF)
6323     {
6324       cp_parser_error (parser, "expected statement");
6325       return;
6326     }
6327
6328   /* Everything else must be a declaration-statement or an
6329      expression-statement.  Try for the declaration-statement
6330      first, unless we are looking at a `;', in which case we know that
6331      we have an expression-statement.  */
6332   if (!statement)
6333     {
6334       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6335         {
6336           cp_parser_parse_tentatively (parser);
6337           /* Try to parse the declaration-statement.  */
6338           cp_parser_declaration_statement (parser);
6339           /* If that worked, we're done.  */
6340           if (cp_parser_parse_definitely (parser))
6341             return;
6342         }
6343       /* Look for an expression-statement instead.  */
6344       statement = cp_parser_expression_statement (parser, in_statement_expr);
6345     }
6346
6347   /* Set the line number for the statement.  */
6348   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6349     SET_EXPR_LOCATION (statement, statement_location);
6350 }
6351
6352 /* Parse the label for a labeled-statement, i.e.
6353
6354    identifier :
6355    case constant-expression :
6356    default :
6357
6358    GNU Extension:
6359    case constant-expression ... constant-expression : statement
6360
6361    When a label is parsed without errors, the label is added to the
6362    parse tree by the finish_* functions, so this function doesn't
6363    have to return the label.  */
6364
6365 static void
6366 cp_parser_label_for_labeled_statement (cp_parser* parser)
6367 {
6368   cp_token *token;
6369
6370   /* The next token should be an identifier.  */
6371   token = cp_lexer_peek_token (parser->lexer);
6372   if (token->type != CPP_NAME
6373       && token->type != CPP_KEYWORD)
6374     {
6375       cp_parser_error (parser, "expected labeled-statement");
6376       return;
6377     }
6378
6379   switch (token->keyword)
6380     {
6381     case RID_CASE:
6382       {
6383         tree expr, expr_hi;
6384         cp_token *ellipsis;
6385
6386         /* Consume the `case' token.  */
6387         cp_lexer_consume_token (parser->lexer);
6388         /* Parse the constant-expression.  */
6389         expr = cp_parser_constant_expression (parser,
6390                                               /*allow_non_constant_p=*/false,
6391                                               NULL);
6392
6393         ellipsis = cp_lexer_peek_token (parser->lexer);
6394         if (ellipsis->type == CPP_ELLIPSIS)
6395           {
6396             /* Consume the `...' token.  */
6397             cp_lexer_consume_token (parser->lexer);
6398             expr_hi =
6399               cp_parser_constant_expression (parser,
6400                                              /*allow_non_constant_p=*/false,
6401                                              NULL);
6402             /* We don't need to emit warnings here, as the common code
6403                will do this for us.  */
6404           }
6405         else
6406           expr_hi = NULL_TREE;
6407
6408         if (parser->in_switch_statement_p)
6409           finish_case_label (expr, expr_hi);
6410         else
6411           error ("case label %qE not within a switch statement", expr);
6412       }
6413       break;
6414
6415     case RID_DEFAULT:
6416       /* Consume the `default' token.  */
6417       cp_lexer_consume_token (parser->lexer);
6418
6419       if (parser->in_switch_statement_p)
6420         finish_case_label (NULL_TREE, NULL_TREE);
6421       else
6422         error ("case label not within a switch statement");
6423       break;
6424
6425     default:
6426       /* Anything else must be an ordinary label.  */
6427       finish_label_stmt (cp_parser_identifier (parser));
6428       break;
6429     }
6430
6431   /* Require the `:' token.  */
6432   cp_parser_require (parser, CPP_COLON, "`:'");
6433 }
6434
6435 /* Parse an expression-statement.
6436
6437    expression-statement:
6438      expression [opt] ;
6439
6440    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6441    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6442    indicates whether this expression-statement is part of an
6443    expression statement.  */
6444
6445 static tree
6446 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6447 {
6448   tree statement = NULL_TREE;
6449
6450   /* If the next token is a ';', then there is no expression
6451      statement.  */
6452   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6453     statement = cp_parser_expression (parser, /*cast_p=*/false);
6454
6455   /* Consume the final `;'.  */
6456   cp_parser_consume_semicolon_at_end_of_statement (parser);
6457
6458   if (in_statement_expr
6459       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6460     /* This is the final expression statement of a statement
6461        expression.  */
6462     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6463   else if (statement)
6464     statement = finish_expr_stmt (statement);
6465   else
6466     finish_stmt ();
6467
6468   return statement;
6469 }
6470
6471 /* Parse a compound-statement.
6472
6473    compound-statement:
6474      { statement-seq [opt] }
6475
6476    Returns a tree representing the statement.  */
6477
6478 static tree
6479 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6480                               bool in_try)
6481 {
6482   tree compound_stmt;
6483
6484   /* Consume the `{'.  */
6485   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6486     return error_mark_node;
6487   /* Begin the compound-statement.  */
6488   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6489   /* Parse an (optional) statement-seq.  */
6490   cp_parser_statement_seq_opt (parser, in_statement_expr);
6491   /* Finish the compound-statement.  */
6492   finish_compound_stmt (compound_stmt);
6493   /* Consume the `}'.  */
6494   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6495
6496   return compound_stmt;
6497 }
6498
6499 /* Parse an (optional) statement-seq.
6500
6501    statement-seq:
6502      statement
6503      statement-seq [opt] statement  */
6504
6505 static void
6506 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6507 {
6508   /* Scan statements until there aren't any more.  */
6509   while (true)
6510     {
6511       cp_token *token = cp_lexer_peek_token (parser->lexer);
6512
6513       /* If we're looking at a `}', then we've run out of statements.  */
6514       if (token->type == CPP_CLOSE_BRACE
6515           || token->type == CPP_EOF
6516           || token->type == CPP_PRAGMA_EOL)
6517         break;
6518
6519       /* Parse the statement.  */
6520       cp_parser_statement (parser, in_statement_expr, true, NULL);
6521     }
6522 }
6523
6524 /* Parse a selection-statement.
6525
6526    selection-statement:
6527      if ( condition ) statement
6528      if ( condition ) statement else statement
6529      switch ( condition ) statement
6530
6531    Returns the new IF_STMT or SWITCH_STMT.
6532
6533    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6534    is a (possibly labeled) if statement which is not enclosed in
6535    braces and has an else clause.  This is used to implement
6536    -Wparentheses.  */
6537
6538 static tree
6539 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6540 {
6541   cp_token *token;
6542   enum rid keyword;
6543
6544   if (if_p != NULL)
6545     *if_p = false;
6546
6547   /* Peek at the next token.  */
6548   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6549
6550   /* See what kind of keyword it is.  */
6551   keyword = token->keyword;
6552   switch (keyword)
6553     {
6554     case RID_IF:
6555     case RID_SWITCH:
6556       {
6557         tree statement;
6558         tree condition;
6559
6560         /* Look for the `('.  */
6561         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6562           {
6563             cp_parser_skip_to_end_of_statement (parser);
6564             return error_mark_node;
6565           }
6566
6567         /* Begin the selection-statement.  */
6568         if (keyword == RID_IF)
6569           statement = begin_if_stmt ();
6570         else
6571           statement = begin_switch_stmt ();
6572
6573         /* Parse the condition.  */
6574         condition = cp_parser_condition (parser);
6575         /* Look for the `)'.  */
6576         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6577           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6578                                                  /*consume_paren=*/true);
6579
6580         if (keyword == RID_IF)
6581           {
6582             bool nested_if;
6583
6584             /* Add the condition.  */
6585             finish_if_stmt_cond (condition, statement);
6586
6587             /* Parse the then-clause.  */
6588             cp_parser_implicitly_scoped_statement (parser, &nested_if);
6589             finish_then_clause (statement);
6590
6591             /* If the next token is `else', parse the else-clause.  */
6592             if (cp_lexer_next_token_is_keyword (parser->lexer,
6593                                                 RID_ELSE))
6594               {
6595                 /* Consume the `else' keyword.  */
6596                 cp_lexer_consume_token (parser->lexer);
6597                 begin_else_clause (statement);
6598                 /* Parse the else-clause.  */
6599                 cp_parser_implicitly_scoped_statement (parser, NULL);
6600                 finish_else_clause (statement);
6601
6602                 /* If we are currently parsing a then-clause, then
6603                    IF_P will not be NULL.  We set it to true to
6604                    indicate that this if statement has an else clause.
6605                    This may trigger the Wparentheses warning below
6606                    when we get back up to the parent if statement.  */
6607                 if (if_p != NULL)
6608                   *if_p = true;
6609               }
6610             else
6611               {
6612                 /* This if statement does not have an else clause.  If
6613                    NESTED_IF is true, then the then-clause is an if
6614                    statement which does have an else clause.  We warn
6615                    about the potential ambiguity.  */
6616                 if (nested_if)
6617                   warning (OPT_Wparentheses,
6618                            ("%Hsuggest explicit braces "
6619                             "to avoid ambiguous %<else%>"),
6620                            EXPR_LOCUS (statement));
6621               }
6622
6623             /* Now we're all done with the if-statement.  */
6624             finish_if_stmt (statement);
6625           }
6626         else
6627           {
6628             bool in_switch_statement_p;
6629             unsigned char in_statement;
6630
6631             /* Add the condition.  */
6632             finish_switch_cond (condition, statement);
6633
6634             /* Parse the body of the switch-statement.  */
6635             in_switch_statement_p = parser->in_switch_statement_p;
6636             in_statement = parser->in_statement;
6637             parser->in_switch_statement_p = true;
6638             parser->in_statement |= IN_SWITCH_STMT;
6639             cp_parser_implicitly_scoped_statement (parser, NULL);
6640             parser->in_switch_statement_p = in_switch_statement_p;
6641             parser->in_statement = in_statement;
6642
6643             /* Now we're all done with the switch-statement.  */
6644             finish_switch_stmt (statement);
6645           }
6646
6647         return statement;
6648       }
6649       break;
6650
6651     default:
6652       cp_parser_error (parser, "expected selection-statement");
6653       return error_mark_node;
6654     }
6655 }
6656
6657 /* Parse a condition.
6658
6659    condition:
6660      expression
6661      type-specifier-seq declarator = assignment-expression
6662
6663    GNU Extension:
6664
6665    condition:
6666      type-specifier-seq declarator asm-specification [opt]
6667        attributes [opt] = assignment-expression
6668
6669    Returns the expression that should be tested.  */
6670
6671 static tree
6672 cp_parser_condition (cp_parser* parser)
6673 {
6674   cp_decl_specifier_seq type_specifiers;
6675   const char *saved_message;
6676
6677   /* Try the declaration first.  */
6678   cp_parser_parse_tentatively (parser);
6679   /* New types are not allowed in the type-specifier-seq for a
6680      condition.  */
6681   saved_message = parser->type_definition_forbidden_message;
6682   parser->type_definition_forbidden_message
6683     = "types may not be defined in conditions";
6684   /* Parse the type-specifier-seq.  */
6685   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6686                                 &type_specifiers);
6687   /* Restore the saved message.  */
6688   parser->type_definition_forbidden_message = saved_message;
6689   /* If all is well, we might be looking at a declaration.  */
6690   if (!cp_parser_error_occurred (parser))
6691     {
6692       tree decl;
6693       tree asm_specification;
6694       tree attributes;
6695       cp_declarator *declarator;
6696       tree initializer = NULL_TREE;
6697
6698       /* Parse the declarator.  */
6699       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6700                                          /*ctor_dtor_or_conv_p=*/NULL,
6701                                          /*parenthesized_p=*/NULL,
6702                                          /*member_p=*/false);
6703       /* Parse the attributes.  */
6704       attributes = cp_parser_attributes_opt (parser);
6705       /* Parse the asm-specification.  */
6706       asm_specification = cp_parser_asm_specification_opt (parser);
6707       /* If the next token is not an `=', then we might still be
6708          looking at an expression.  For example:
6709
6710            if (A(a).x)
6711
6712          looks like a decl-specifier-seq and a declarator -- but then
6713          there is no `=', so this is an expression.  */
6714       cp_parser_require (parser, CPP_EQ, "`='");
6715       /* If we did see an `=', then we are looking at a declaration
6716          for sure.  */
6717       if (cp_parser_parse_definitely (parser))
6718         {
6719           tree pushed_scope;
6720           bool non_constant_p;
6721
6722           /* Create the declaration.  */
6723           decl = start_decl (declarator, &type_specifiers,
6724                              /*initialized_p=*/true,
6725                              attributes, /*prefix_attributes=*/NULL_TREE,
6726                              &pushed_scope);
6727           /* Parse the assignment-expression.  */
6728           initializer
6729             = cp_parser_constant_expression (parser,
6730                                              /*allow_non_constant_p=*/true,
6731                                              &non_constant_p);
6732           if (!non_constant_p)
6733             initializer = fold_non_dependent_expr (initializer);
6734
6735           /* Process the initializer.  */
6736           cp_finish_decl (decl,
6737                           initializer, !non_constant_p,
6738                           asm_specification,
6739                           LOOKUP_ONLYCONVERTING);
6740
6741           if (pushed_scope)
6742             pop_scope (pushed_scope);
6743
6744           return convert_from_reference (decl);
6745         }
6746     }
6747   /* If we didn't even get past the declarator successfully, we are
6748      definitely not looking at a declaration.  */
6749   else
6750     cp_parser_abort_tentative_parse (parser);
6751
6752   /* Otherwise, we are looking at an expression.  */
6753   return cp_parser_expression (parser, /*cast_p=*/false);
6754 }
6755
6756 /* Parse an iteration-statement.
6757
6758    iteration-statement:
6759      while ( condition ) statement
6760      do statement while ( expression ) ;
6761      for ( for-init-statement condition [opt] ; expression [opt] )
6762        statement
6763
6764    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6765
6766 static tree
6767 cp_parser_iteration_statement (cp_parser* parser)
6768 {
6769   cp_token *token;
6770   enum rid keyword;
6771   tree statement;
6772   unsigned char in_statement;
6773
6774   /* Peek at the next token.  */
6775   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6776   if (!token)
6777     return error_mark_node;
6778
6779   /* Remember whether or not we are already within an iteration
6780      statement.  */
6781   in_statement = parser->in_statement;
6782
6783   /* See what kind of keyword it is.  */
6784   keyword = token->keyword;
6785   switch (keyword)
6786     {
6787     case RID_WHILE:
6788       {
6789         tree condition;
6790
6791         /* Begin the while-statement.  */
6792         statement = begin_while_stmt ();
6793         /* Look for the `('.  */
6794         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6795         /* Parse the condition.  */
6796         condition = cp_parser_condition (parser);
6797         finish_while_stmt_cond (condition, statement);
6798         /* Look for the `)'.  */
6799         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6800         /* Parse the dependent statement.  */
6801         parser->in_statement = IN_ITERATION_STMT;
6802         cp_parser_already_scoped_statement (parser);
6803         parser->in_statement = in_statement;
6804         /* We're done with the while-statement.  */
6805         finish_while_stmt (statement);
6806       }
6807       break;
6808
6809     case RID_DO:
6810       {
6811         tree expression;
6812
6813         /* Begin the do-statement.  */
6814         statement = begin_do_stmt ();
6815         /* Parse the body of the do-statement.  */
6816         parser->in_statement = IN_ITERATION_STMT;
6817         cp_parser_implicitly_scoped_statement (parser, NULL);
6818         parser->in_statement = in_statement;
6819         finish_do_body (statement);
6820         /* Look for the `while' keyword.  */
6821         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6822         /* Look for the `('.  */
6823         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6824         /* Parse the expression.  */
6825         expression = cp_parser_expression (parser, /*cast_p=*/false);
6826         /* We're done with the do-statement.  */
6827         finish_do_stmt (expression, statement);
6828         /* Look for the `)'.  */
6829         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6830         /* Look for the `;'.  */
6831         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6832       }
6833       break;
6834
6835     case RID_FOR:
6836       {
6837         tree condition = NULL_TREE;
6838         tree expression = NULL_TREE;
6839
6840         /* Begin the for-statement.  */
6841         statement = begin_for_stmt ();
6842         /* Look for the `('.  */
6843         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6844         /* Parse the initialization.  */
6845         cp_parser_for_init_statement (parser);
6846         finish_for_init_stmt (statement);
6847
6848         /* If there's a condition, process it.  */
6849         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6850           condition = cp_parser_condition (parser);
6851         finish_for_cond (condition, statement);
6852         /* Look for the `;'.  */
6853         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6854
6855         /* If there's an expression, process it.  */
6856         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6857           expression = cp_parser_expression (parser, /*cast_p=*/false);
6858         finish_for_expr (expression, statement);
6859         /* Look for the `)'.  */
6860         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6861
6862         /* Parse the body of the for-statement.  */
6863         parser->in_statement = IN_ITERATION_STMT;
6864         cp_parser_already_scoped_statement (parser);
6865         parser->in_statement = in_statement;
6866
6867         /* We're done with the for-statement.  */
6868         finish_for_stmt (statement);
6869       }
6870       break;
6871
6872     default:
6873       cp_parser_error (parser, "expected iteration-statement");
6874       statement = error_mark_node;
6875       break;
6876     }
6877
6878   return statement;
6879 }
6880
6881 /* Parse a for-init-statement.
6882
6883    for-init-statement:
6884      expression-statement
6885      simple-declaration  */
6886
6887 static void
6888 cp_parser_for_init_statement (cp_parser* parser)
6889 {
6890   /* If the next token is a `;', then we have an empty
6891      expression-statement.  Grammatically, this is also a
6892      simple-declaration, but an invalid one, because it does not
6893      declare anything.  Therefore, if we did not handle this case
6894      specially, we would issue an error message about an invalid
6895      declaration.  */
6896   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6897     {
6898       /* We're going to speculatively look for a declaration, falling back
6899          to an expression, if necessary.  */
6900       cp_parser_parse_tentatively (parser);
6901       /* Parse the declaration.  */
6902       cp_parser_simple_declaration (parser,
6903                                     /*function_definition_allowed_p=*/false);
6904       /* If the tentative parse failed, then we shall need to look for an
6905          expression-statement.  */
6906       if (cp_parser_parse_definitely (parser))
6907         return;
6908     }
6909
6910   cp_parser_expression_statement (parser, false);
6911 }
6912
6913 /* Parse a jump-statement.
6914
6915    jump-statement:
6916      break ;
6917      continue ;
6918      return expression [opt] ;
6919      goto identifier ;
6920
6921    GNU extension:
6922
6923    jump-statement:
6924      goto * expression ;
6925
6926    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6927
6928 static tree
6929 cp_parser_jump_statement (cp_parser* parser)
6930 {
6931   tree statement = error_mark_node;
6932   cp_token *token;
6933   enum rid keyword;
6934
6935   /* Peek at the next token.  */
6936   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6937   if (!token)
6938     return error_mark_node;
6939
6940   /* See what kind of keyword it is.  */
6941   keyword = token->keyword;
6942   switch (keyword)
6943     {
6944     case RID_BREAK:
6945       switch (parser->in_statement)
6946         {
6947         case 0:
6948           error ("break statement not within loop or switch");
6949           break;
6950         default:
6951           gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6952                       || parser->in_statement == IN_ITERATION_STMT);
6953           statement = finish_break_stmt ();
6954           break;
6955         case IN_OMP_BLOCK:
6956           error ("invalid exit from OpenMP structured block");
6957           break;
6958         case IN_OMP_FOR:
6959           error ("break statement used with OpenMP for loop");
6960           break;
6961         }
6962       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6963       break;
6964
6965     case RID_CONTINUE:
6966       switch (parser->in_statement & ~IN_SWITCH_STMT)
6967         {
6968         case 0:
6969           error ("continue statement not within a loop");
6970           break;
6971         case IN_ITERATION_STMT:
6972         case IN_OMP_FOR:
6973           statement = finish_continue_stmt ();
6974           break;
6975         case IN_OMP_BLOCK:
6976           error ("invalid exit from OpenMP structured block");
6977           break;
6978         default:
6979           gcc_unreachable ();
6980         }
6981       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6982       break;
6983
6984     case RID_RETURN:
6985       {
6986         tree expr;
6987
6988         /* If the next token is a `;', then there is no
6989            expression.  */
6990         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6991           expr = cp_parser_expression (parser, /*cast_p=*/false);
6992         else
6993           expr = NULL_TREE;
6994         /* Build the return-statement.  */
6995         statement = finish_return_stmt (expr);
6996         /* Look for the final `;'.  */
6997         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6998       }
6999       break;
7000
7001     case RID_GOTO:
7002       /* Create the goto-statement.  */
7003       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7004         {
7005           /* Issue a warning about this use of a GNU extension.  */
7006           if (pedantic)
7007             pedwarn ("ISO C++ forbids computed gotos");
7008           /* Consume the '*' token.  */
7009           cp_lexer_consume_token (parser->lexer);
7010           /* Parse the dependent expression.  */
7011           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7012         }
7013       else
7014         finish_goto_stmt (cp_parser_identifier (parser));
7015       /* Look for the final `;'.  */
7016       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7017       break;
7018
7019     default:
7020       cp_parser_error (parser, "expected jump-statement");
7021       break;
7022     }
7023
7024   return statement;
7025 }
7026
7027 /* Parse a declaration-statement.
7028
7029    declaration-statement:
7030      block-declaration  */
7031
7032 static void
7033 cp_parser_declaration_statement (cp_parser* parser)
7034 {
7035   void *p;
7036
7037   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7038   p = obstack_alloc (&declarator_obstack, 0);
7039
7040  /* Parse the block-declaration.  */
7041   cp_parser_block_declaration (parser, /*statement_p=*/true);
7042
7043   /* Free any declarators allocated.  */
7044   obstack_free (&declarator_obstack, p);
7045
7046   /* Finish off the statement.  */
7047   finish_stmt ();
7048 }
7049
7050 /* Some dependent statements (like `if (cond) statement'), are
7051    implicitly in their own scope.  In other words, if the statement is
7052    a single statement (as opposed to a compound-statement), it is
7053    none-the-less treated as if it were enclosed in braces.  Any
7054    declarations appearing in the dependent statement are out of scope
7055    after control passes that point.  This function parses a statement,
7056    but ensures that is in its own scope, even if it is not a
7057    compound-statement.
7058
7059    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7060    is a (possibly labeled) if statement which is not enclosed in
7061    braces and has an else clause.  This is used to implement
7062    -Wparentheses.
7063
7064    Returns the new statement.  */
7065
7066 static tree
7067 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7068 {
7069   tree statement;
7070
7071   if (if_p != NULL)
7072     *if_p = false;
7073
7074   /* Mark if () ; with a special NOP_EXPR.  */
7075   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7076     {
7077       cp_lexer_consume_token (parser->lexer);
7078       statement = add_stmt (build_empty_stmt ());
7079     }
7080   /* if a compound is opened, we simply parse the statement directly.  */
7081   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7082     statement = cp_parser_compound_statement (parser, NULL, false);
7083   /* If the token is not a `{', then we must take special action.  */
7084   else
7085     {
7086       /* Create a compound-statement.  */
7087       statement = begin_compound_stmt (0);
7088       /* Parse the dependent-statement.  */
7089       cp_parser_statement (parser, NULL_TREE, false, if_p);
7090       /* Finish the dummy compound-statement.  */
7091       finish_compound_stmt (statement);
7092     }
7093
7094   /* Return the statement.  */
7095   return statement;
7096 }
7097
7098 /* For some dependent statements (like `while (cond) statement'), we
7099    have already created a scope.  Therefore, even if the dependent
7100    statement is a compound-statement, we do not want to create another
7101    scope.  */
7102
7103 static void
7104 cp_parser_already_scoped_statement (cp_parser* parser)
7105 {
7106   /* If the token is a `{', then we must take special action.  */
7107   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7108     cp_parser_statement (parser, NULL_TREE, false, NULL);
7109   else
7110     {
7111       /* Avoid calling cp_parser_compound_statement, so that we
7112          don't create a new scope.  Do everything else by hand.  */
7113       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7114       cp_parser_statement_seq_opt (parser, NULL_TREE);
7115       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7116     }
7117 }
7118
7119 /* Declarations [gram.dcl.dcl] */
7120
7121 /* Parse an optional declaration-sequence.
7122
7123    declaration-seq:
7124      declaration
7125      declaration-seq declaration  */
7126
7127 static void
7128 cp_parser_declaration_seq_opt (cp_parser* parser)
7129 {
7130   while (true)
7131     {
7132       cp_token *token;
7133
7134       token = cp_lexer_peek_token (parser->lexer);
7135
7136       if (token->type == CPP_CLOSE_BRACE
7137           || token->type == CPP_EOF
7138           || token->type == CPP_PRAGMA_EOL)
7139         break;
7140
7141       if (token->type == CPP_SEMICOLON)
7142         {
7143           /* A declaration consisting of a single semicolon is
7144              invalid.  Allow it unless we're being pedantic.  */
7145           cp_lexer_consume_token (parser->lexer);
7146           if (pedantic && !in_system_header)
7147             pedwarn ("extra %<;%>");
7148           continue;
7149         }
7150
7151       /* If we're entering or exiting a region that's implicitly
7152          extern "C", modify the lang context appropriately.  */
7153       if (!parser->implicit_extern_c && token->implicit_extern_c)
7154         {
7155           push_lang_context (lang_name_c);
7156           parser->implicit_extern_c = true;
7157         }
7158       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7159         {
7160           pop_lang_context ();
7161           parser->implicit_extern_c = false;
7162         }
7163
7164       if (token->type == CPP_PRAGMA)
7165         {
7166           /* A top-level declaration can consist solely of a #pragma.
7167              A nested declaration cannot, so this is done here and not
7168              in cp_parser_declaration.  (A #pragma at block scope is
7169              handled in cp_parser_statement.)  */
7170           cp_parser_pragma (parser, pragma_external);
7171           continue;
7172         }
7173
7174       /* Parse the declaration itself.  */
7175       cp_parser_declaration (parser);
7176     }
7177 }
7178
7179 /* Parse a declaration.
7180
7181    declaration:
7182      block-declaration
7183      function-definition
7184      template-declaration
7185      explicit-instantiation
7186      explicit-specialization
7187      linkage-specification
7188      namespace-definition
7189
7190    GNU extension:
7191
7192    declaration:
7193       __extension__ declaration */
7194
7195 static void
7196 cp_parser_declaration (cp_parser* parser)
7197 {
7198   cp_token token1;
7199   cp_token token2;
7200   int saved_pedantic;
7201   void *p;
7202
7203   /* Check for the `__extension__' keyword.  */
7204   if (cp_parser_extension_opt (parser, &saved_pedantic))
7205     {
7206       /* Parse the qualified declaration.  */
7207       cp_parser_declaration (parser);
7208       /* Restore the PEDANTIC flag.  */
7209       pedantic = saved_pedantic;
7210
7211       return;
7212     }
7213
7214   /* Try to figure out what kind of declaration is present.  */
7215   token1 = *cp_lexer_peek_token (parser->lexer);
7216
7217   if (token1.type != CPP_EOF)
7218     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7219   else
7220     {
7221       token2.type = CPP_EOF;
7222       token2.keyword = RID_MAX;
7223     }
7224
7225   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7226   p = obstack_alloc (&declarator_obstack, 0);
7227
7228   /* If the next token is `extern' and the following token is a string
7229      literal, then we have a linkage specification.  */
7230   if (token1.keyword == RID_EXTERN
7231       && cp_parser_is_string_literal (&token2))
7232     cp_parser_linkage_specification (parser);
7233   /* If the next token is `template', then we have either a template
7234      declaration, an explicit instantiation, or an explicit
7235      specialization.  */
7236   else if (token1.keyword == RID_TEMPLATE)
7237     {
7238       /* `template <>' indicates a template specialization.  */
7239       if (token2.type == CPP_LESS
7240           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7241         cp_parser_explicit_specialization (parser);
7242       /* `template <' indicates a template declaration.  */
7243       else if (token2.type == CPP_LESS)
7244         cp_parser_template_declaration (parser, /*member_p=*/false);
7245       /* Anything else must be an explicit instantiation.  */
7246       else
7247         cp_parser_explicit_instantiation (parser);
7248     }
7249   /* If the next token is `export', then we have a template
7250      declaration.  */
7251   else if (token1.keyword == RID_EXPORT)
7252     cp_parser_template_declaration (parser, /*member_p=*/false);
7253   /* If the next token is `extern', 'static' or 'inline' and the one
7254      after that is `template', we have a GNU extended explicit
7255      instantiation directive.  */
7256   else if (cp_parser_allow_gnu_extensions_p (parser)
7257            && (token1.keyword == RID_EXTERN
7258                || token1.keyword == RID_STATIC
7259                || token1.keyword == RID_INLINE)
7260            && token2.keyword == RID_TEMPLATE)
7261     cp_parser_explicit_instantiation (parser);
7262   /* If the next token is `namespace', check for a named or unnamed
7263      namespace definition.  */
7264   else if (token1.keyword == RID_NAMESPACE
7265            && (/* A named namespace definition.  */
7266                (token2.type == CPP_NAME
7267                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7268                     != CPP_EQ))
7269                /* An unnamed namespace definition.  */
7270                || token2.type == CPP_OPEN_BRACE
7271                || token2.keyword == RID_ATTRIBUTE))
7272     cp_parser_namespace_definition (parser);
7273   /* Objective-C++ declaration/definition.  */
7274   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7275     cp_parser_objc_declaration (parser);
7276   /* We must have either a block declaration or a function
7277      definition.  */
7278   else
7279     /* Try to parse a block-declaration, or a function-definition.  */
7280     cp_parser_block_declaration (parser, /*statement_p=*/false);
7281
7282   /* Free any declarators allocated.  */
7283   obstack_free (&declarator_obstack, p);
7284 }
7285
7286 /* Parse a block-declaration.
7287
7288    block-declaration:
7289      simple-declaration
7290      asm-definition
7291      namespace-alias-definition
7292      using-declaration
7293      using-directive
7294
7295    GNU Extension:
7296
7297    block-declaration:
7298      __extension__ block-declaration
7299      label-declaration
7300
7301    C++0x Extension:
7302
7303    block-declaration:
7304      static_assert-declaration
7305
7306    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7307    part of a declaration-statement.  */
7308
7309 static void
7310 cp_parser_block_declaration (cp_parser *parser,
7311                              bool      statement_p)
7312 {
7313   cp_token *token1;
7314   int saved_pedantic;
7315
7316   /* Check for the `__extension__' keyword.  */
7317   if (cp_parser_extension_opt (parser, &saved_pedantic))
7318     {
7319       /* Parse the qualified declaration.  */
7320       cp_parser_block_declaration (parser, statement_p);
7321       /* Restore the PEDANTIC flag.  */
7322       pedantic = saved_pedantic;
7323
7324       return;
7325     }
7326
7327   /* Peek at the next token to figure out which kind of declaration is
7328      present.  */
7329   token1 = cp_lexer_peek_token (parser->lexer);
7330
7331   /* If the next keyword is `asm', we have an asm-definition.  */
7332   if (token1->keyword == RID_ASM)
7333     {
7334       if (statement_p)
7335         cp_parser_commit_to_tentative_parse (parser);
7336       cp_parser_asm_definition (parser);
7337     }
7338   /* If the next keyword is `namespace', we have a
7339      namespace-alias-definition.  */
7340   else if (token1->keyword == RID_NAMESPACE)
7341     cp_parser_namespace_alias_definition (parser);
7342   /* If the next keyword is `using', we have either a
7343      using-declaration or a using-directive.  */
7344   else if (token1->keyword == RID_USING)
7345     {
7346       cp_token *token2;
7347
7348       if (statement_p)
7349         cp_parser_commit_to_tentative_parse (parser);
7350       /* If the token after `using' is `namespace', then we have a
7351          using-directive.  */
7352       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7353       if (token2->keyword == RID_NAMESPACE)
7354         cp_parser_using_directive (parser);
7355       /* Otherwise, it's a using-declaration.  */
7356       else
7357         cp_parser_using_declaration (parser,
7358                                      /*access_declaration_p=*/false);
7359     }
7360   /* If the next keyword is `__label__' we have a label declaration.  */
7361   else if (token1->keyword == RID_LABEL)
7362     {
7363       if (statement_p)
7364         cp_parser_commit_to_tentative_parse (parser);
7365       cp_parser_label_declaration (parser);
7366     }
7367   /* If the next token is `static_assert' we have a static assertion.  */
7368   else if (token1->keyword == RID_STATIC_ASSERT)
7369     cp_parser_static_assert (parser, /*member_p=*/false);
7370   /* Anything else must be a simple-declaration.  */
7371   else
7372     cp_parser_simple_declaration (parser, !statement_p);
7373 }
7374
7375 /* Parse a simple-declaration.
7376
7377    simple-declaration:
7378      decl-specifier-seq [opt] init-declarator-list [opt] ;
7379
7380    init-declarator-list:
7381      init-declarator
7382      init-declarator-list , init-declarator
7383
7384    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7385    function-definition as a simple-declaration.  */
7386
7387 static void
7388 cp_parser_simple_declaration (cp_parser* parser,
7389                               bool function_definition_allowed_p)
7390 {
7391   cp_decl_specifier_seq decl_specifiers;
7392   int declares_class_or_enum;
7393   bool saw_declarator;
7394
7395   /* Defer access checks until we know what is being declared; the
7396      checks for names appearing in the decl-specifier-seq should be
7397      done as if we were in the scope of the thing being declared.  */
7398   push_deferring_access_checks (dk_deferred);
7399
7400   /* Parse the decl-specifier-seq.  We have to keep track of whether
7401      or not the decl-specifier-seq declares a named class or
7402      enumeration type, since that is the only case in which the
7403      init-declarator-list is allowed to be empty.
7404
7405      [dcl.dcl]
7406
7407      In a simple-declaration, the optional init-declarator-list can be
7408      omitted only when declaring a class or enumeration, that is when
7409      the decl-specifier-seq contains either a class-specifier, an
7410      elaborated-type-specifier, or an enum-specifier.  */
7411   cp_parser_decl_specifier_seq (parser,
7412                                 CP_PARSER_FLAGS_OPTIONAL,
7413                                 &decl_specifiers,
7414                                 &declares_class_or_enum);
7415   /* We no longer need to defer access checks.  */
7416   stop_deferring_access_checks ();
7417
7418   /* In a block scope, a valid declaration must always have a
7419      decl-specifier-seq.  By not trying to parse declarators, we can
7420      resolve the declaration/expression ambiguity more quickly.  */
7421   if (!function_definition_allowed_p
7422       && !decl_specifiers.any_specifiers_p)
7423     {
7424       cp_parser_error (parser, "expected declaration");
7425       goto done;
7426     }
7427
7428   /* If the next two tokens are both identifiers, the code is
7429      erroneous. The usual cause of this situation is code like:
7430
7431        T t;
7432
7433      where "T" should name a type -- but does not.  */
7434   if (!decl_specifiers.type
7435       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7436     {
7437       /* If parsing tentatively, we should commit; we really are
7438          looking at a declaration.  */
7439       cp_parser_commit_to_tentative_parse (parser);
7440       /* Give up.  */
7441       goto done;
7442     }
7443
7444   /* If we have seen at least one decl-specifier, and the next token
7445      is not a parenthesis, then we must be looking at a declaration.
7446      (After "int (" we might be looking at a functional cast.)  */
7447   if (decl_specifiers.any_specifiers_p
7448       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7449     cp_parser_commit_to_tentative_parse (parser);
7450
7451   /* Keep going until we hit the `;' at the end of the simple
7452      declaration.  */
7453   saw_declarator = false;
7454   while (cp_lexer_next_token_is_not (parser->lexer,
7455                                      CPP_SEMICOLON))
7456     {
7457       cp_token *token;
7458       bool function_definition_p;
7459       tree decl;
7460
7461       if (saw_declarator)
7462         {
7463           /* If we are processing next declarator, coma is expected */
7464           token = cp_lexer_peek_token (parser->lexer);
7465           gcc_assert (token->type == CPP_COMMA);
7466           cp_lexer_consume_token (parser->lexer);
7467         }
7468       else
7469         saw_declarator = true;
7470
7471       /* Parse the init-declarator.  */
7472       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7473                                         /*checks=*/NULL,
7474                                         function_definition_allowed_p,
7475                                         /*member_p=*/false,
7476                                         declares_class_or_enum,
7477                                         &function_definition_p);
7478       /* If an error occurred while parsing tentatively, exit quickly.
7479          (That usually happens when in the body of a function; each
7480          statement is treated as a declaration-statement until proven
7481          otherwise.)  */
7482       if (cp_parser_error_occurred (parser))
7483         goto done;
7484       /* Handle function definitions specially.  */
7485       if (function_definition_p)
7486         {
7487           /* If the next token is a `,', then we are probably
7488              processing something like:
7489
7490                void f() {}, *p;
7491
7492              which is erroneous.  */
7493           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7494             error ("mixing declarations and function-definitions is forbidden");
7495           /* Otherwise, we're done with the list of declarators.  */
7496           else
7497             {
7498               pop_deferring_access_checks ();
7499               return;
7500             }
7501         }
7502       /* The next token should be either a `,' or a `;'.  */
7503       token = cp_lexer_peek_token (parser->lexer);
7504       /* If it's a `,', there are more declarators to come.  */
7505       if (token->type == CPP_COMMA)
7506         /* will be consumed next time around */;
7507       /* If it's a `;', we are done.  */
7508       else if (token->type == CPP_SEMICOLON)
7509         break;
7510       /* Anything else is an error.  */
7511       else
7512         {
7513           /* If we have already issued an error message we don't need
7514              to issue another one.  */
7515           if (decl != error_mark_node
7516               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7517             cp_parser_error (parser, "expected %<,%> or %<;%>");
7518           /* Skip tokens until we reach the end of the statement.  */
7519           cp_parser_skip_to_end_of_statement (parser);
7520           /* If the next token is now a `;', consume it.  */
7521           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7522             cp_lexer_consume_token (parser->lexer);
7523           goto done;
7524         }
7525       /* After the first time around, a function-definition is not
7526          allowed -- even if it was OK at first.  For example:
7527
7528            int i, f() {}
7529
7530          is not valid.  */
7531       function_definition_allowed_p = false;
7532     }
7533
7534   /* Issue an error message if no declarators are present, and the
7535      decl-specifier-seq does not itself declare a class or
7536      enumeration.  */
7537   if (!saw_declarator)
7538     {
7539       if (cp_parser_declares_only_class_p (parser))
7540         shadow_tag (&decl_specifiers);
7541       /* Perform any deferred access checks.  */
7542       perform_deferred_access_checks ();
7543     }
7544
7545   /* Consume the `;'.  */
7546   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7547
7548  done:
7549   pop_deferring_access_checks ();
7550 }
7551
7552 /* Parse a decl-specifier-seq.
7553
7554    decl-specifier-seq:
7555      decl-specifier-seq [opt] decl-specifier
7556
7557    decl-specifier:
7558      storage-class-specifier
7559      type-specifier
7560      function-specifier
7561      friend
7562      typedef
7563
7564    GNU Extension:
7565
7566    decl-specifier:
7567      attributes
7568
7569    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7570
7571    The parser flags FLAGS is used to control type-specifier parsing.
7572
7573    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7574    flags:
7575
7576      1: one of the decl-specifiers is an elaborated-type-specifier
7577         (i.e., a type declaration)
7578      2: one of the decl-specifiers is an enum-specifier or a
7579         class-specifier (i.e., a type definition)
7580
7581    */
7582
7583 static void
7584 cp_parser_decl_specifier_seq (cp_parser* parser,
7585                               cp_parser_flags flags,
7586                               cp_decl_specifier_seq *decl_specs,
7587                               int* declares_class_or_enum)
7588 {
7589   bool constructor_possible_p = !parser->in_declarator_p;
7590
7591   /* Clear DECL_SPECS.  */
7592   clear_decl_specs (decl_specs);
7593
7594   /* Assume no class or enumeration type is declared.  */
7595   *declares_class_or_enum = 0;
7596
7597   /* Keep reading specifiers until there are no more to read.  */
7598   while (true)
7599     {
7600       bool constructor_p;
7601       bool found_decl_spec;
7602       cp_token *token;
7603
7604       /* Peek at the next token.  */
7605       token = cp_lexer_peek_token (parser->lexer);
7606       /* Handle attributes.  */
7607       if (token->keyword == RID_ATTRIBUTE)
7608         {
7609           /* Parse the attributes.  */
7610           decl_specs->attributes
7611             = chainon (decl_specs->attributes,
7612                        cp_parser_attributes_opt (parser));
7613           continue;
7614         }
7615       /* Assume we will find a decl-specifier keyword.  */
7616       found_decl_spec = true;
7617       /* If the next token is an appropriate keyword, we can simply
7618          add it to the list.  */
7619       switch (token->keyword)
7620         {
7621           /* decl-specifier:
7622                friend  */
7623         case RID_FRIEND:
7624           if (!at_class_scope_p ())
7625             {
7626               error ("%<friend%> used outside of class");
7627               cp_lexer_purge_token (parser->lexer);
7628             }
7629           else
7630             {
7631               ++decl_specs->specs[(int) ds_friend];
7632               /* Consume the token.  */
7633               cp_lexer_consume_token (parser->lexer);
7634             }
7635           break;
7636
7637           /* function-specifier:
7638                inline
7639                virtual
7640                explicit  */
7641         case RID_INLINE:
7642         case RID_VIRTUAL:
7643         case RID_EXPLICIT:
7644           cp_parser_function_specifier_opt (parser, decl_specs);
7645           break;
7646
7647           /* decl-specifier:
7648                typedef  */
7649         case RID_TYPEDEF:
7650           ++decl_specs->specs[(int) ds_typedef];
7651           /* Consume the token.  */
7652           cp_lexer_consume_token (parser->lexer);
7653           /* A constructor declarator cannot appear in a typedef.  */
7654           constructor_possible_p = false;
7655           /* The "typedef" keyword can only occur in a declaration; we
7656              may as well commit at this point.  */
7657           cp_parser_commit_to_tentative_parse (parser);
7658
7659           if (decl_specs->storage_class != sc_none)
7660             decl_specs->conflicting_specifiers_p = true;
7661           break;
7662
7663           /* storage-class-specifier:
7664                auto
7665                register
7666                static
7667                extern
7668                mutable
7669
7670              GNU Extension:
7671                thread  */
7672         case RID_AUTO:
7673         case RID_REGISTER:
7674         case RID_STATIC:
7675         case RID_EXTERN:
7676         case RID_MUTABLE:
7677           /* Consume the token.  */
7678           cp_lexer_consume_token (parser->lexer);
7679           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7680           break;
7681         case RID_THREAD:
7682           /* Consume the token.  */
7683           cp_lexer_consume_token (parser->lexer);
7684           ++decl_specs->specs[(int) ds_thread];
7685           break;
7686
7687         default:
7688           /* We did not yet find a decl-specifier yet.  */
7689           found_decl_spec = false;
7690           break;
7691         }
7692
7693       /* Constructors are a special case.  The `S' in `S()' is not a
7694          decl-specifier; it is the beginning of the declarator.  */
7695       constructor_p
7696         = (!found_decl_spec
7697            && constructor_possible_p
7698            && (cp_parser_constructor_declarator_p
7699                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7700
7701       /* If we don't have a DECL_SPEC yet, then we must be looking at
7702          a type-specifier.  */
7703       if (!found_decl_spec && !constructor_p)
7704         {
7705           int decl_spec_declares_class_or_enum;
7706           bool is_cv_qualifier;
7707           tree type_spec;
7708
7709           type_spec
7710             = cp_parser_type_specifier (parser, flags,
7711                                         decl_specs,
7712                                         /*is_declaration=*/true,
7713                                         &decl_spec_declares_class_or_enum,
7714                                         &is_cv_qualifier);
7715
7716           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7717
7718           /* If this type-specifier referenced a user-defined type
7719              (a typedef, class-name, etc.), then we can't allow any
7720              more such type-specifiers henceforth.
7721
7722              [dcl.spec]
7723
7724              The longest sequence of decl-specifiers that could
7725              possibly be a type name is taken as the
7726              decl-specifier-seq of a declaration.  The sequence shall
7727              be self-consistent as described below.
7728
7729              [dcl.type]
7730
7731              As a general rule, at most one type-specifier is allowed
7732              in the complete decl-specifier-seq of a declaration.  The
7733              only exceptions are the following:
7734
7735              -- const or volatile can be combined with any other
7736                 type-specifier.
7737
7738              -- signed or unsigned can be combined with char, long,
7739                 short, or int.
7740
7741              -- ..
7742
7743              Example:
7744
7745                typedef char* Pc;
7746                void g (const int Pc);
7747
7748              Here, Pc is *not* part of the decl-specifier seq; it's
7749              the declarator.  Therefore, once we see a type-specifier
7750              (other than a cv-qualifier), we forbid any additional
7751              user-defined types.  We *do* still allow things like `int
7752              int' to be considered a decl-specifier-seq, and issue the
7753              error message later.  */
7754           if (type_spec && !is_cv_qualifier)
7755             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7756           /* A constructor declarator cannot follow a type-specifier.  */
7757           if (type_spec)
7758             {
7759               constructor_possible_p = false;
7760               found_decl_spec = true;
7761             }
7762         }
7763
7764       /* If we still do not have a DECL_SPEC, then there are no more
7765          decl-specifiers.  */
7766       if (!found_decl_spec)
7767         break;
7768
7769       decl_specs->any_specifiers_p = true;
7770       /* After we see one decl-specifier, further decl-specifiers are
7771          always optional.  */
7772       flags |= CP_PARSER_FLAGS_OPTIONAL;
7773     }
7774
7775   cp_parser_check_decl_spec (decl_specs);
7776
7777   /* Don't allow a friend specifier with a class definition.  */
7778   if (decl_specs->specs[(int) ds_friend] != 0
7779       && (*declares_class_or_enum & 2))
7780     error ("class definition may not be declared a friend");
7781 }
7782
7783 /* Parse an (optional) storage-class-specifier.
7784
7785    storage-class-specifier:
7786      auto
7787      register
7788      static
7789      extern
7790      mutable
7791
7792    GNU Extension:
7793
7794    storage-class-specifier:
7795      thread
7796
7797    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7798
7799 static tree
7800 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7801 {
7802   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7803     {
7804     case RID_AUTO:
7805     case RID_REGISTER:
7806     case RID_STATIC:
7807     case RID_EXTERN:
7808     case RID_MUTABLE:
7809     case RID_THREAD:
7810       /* Consume the token.  */
7811       return cp_lexer_consume_token (parser->lexer)->u.value;
7812
7813     default:
7814       return NULL_TREE;
7815     }
7816 }
7817
7818 /* Parse an (optional) function-specifier.
7819
7820    function-specifier:
7821      inline
7822      virtual
7823      explicit
7824
7825    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7826    Updates DECL_SPECS, if it is non-NULL.  */
7827
7828 static tree
7829 cp_parser_function_specifier_opt (cp_parser* parser,
7830                                   cp_decl_specifier_seq *decl_specs)
7831 {
7832   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7833     {
7834     case RID_INLINE:
7835       if (decl_specs)
7836         ++decl_specs->specs[(int) ds_inline];
7837       break;
7838
7839     case RID_VIRTUAL:
7840       /* 14.5.2.3 [temp.mem]
7841
7842          A member function template shall not be virtual.  */
7843       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7844         error ("templates may not be %<virtual%>");
7845       else if (decl_specs)
7846         ++decl_specs->specs[(int) ds_virtual];
7847       break;
7848
7849     case RID_EXPLICIT:
7850       if (decl_specs)
7851         ++decl_specs->specs[(int) ds_explicit];
7852       break;
7853
7854     default:
7855       return NULL_TREE;
7856     }
7857
7858   /* Consume the token.  */
7859   return cp_lexer_consume_token (parser->lexer)->u.value;
7860 }
7861
7862 /* Parse a linkage-specification.
7863
7864    linkage-specification:
7865      extern string-literal { declaration-seq [opt] }
7866      extern string-literal declaration  */
7867
7868 static void
7869 cp_parser_linkage_specification (cp_parser* parser)
7870 {
7871   tree linkage;
7872
7873   /* Look for the `extern' keyword.  */
7874   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7875
7876   /* Look for the string-literal.  */
7877   linkage = cp_parser_string_literal (parser, false, false);
7878
7879   /* Transform the literal into an identifier.  If the literal is a
7880      wide-character string, or contains embedded NULs, then we can't
7881      handle it as the user wants.  */
7882   if (strlen (TREE_STRING_POINTER (linkage))
7883       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7884     {
7885       cp_parser_error (parser, "invalid linkage-specification");
7886       /* Assume C++ linkage.  */
7887       linkage = lang_name_cplusplus;
7888     }
7889   else
7890     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7891
7892   /* We're now using the new linkage.  */
7893   push_lang_context (linkage);
7894
7895   /* If the next token is a `{', then we're using the first
7896      production.  */
7897   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7898     {
7899       /* Consume the `{' token.  */
7900       cp_lexer_consume_token (parser->lexer);
7901       /* Parse the declarations.  */
7902       cp_parser_declaration_seq_opt (parser);
7903       /* Look for the closing `}'.  */
7904       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7905     }
7906   /* Otherwise, there's just one declaration.  */
7907   else
7908     {
7909       bool saved_in_unbraced_linkage_specification_p;
7910
7911       saved_in_unbraced_linkage_specification_p
7912         = parser->in_unbraced_linkage_specification_p;
7913       parser->in_unbraced_linkage_specification_p = true;
7914       cp_parser_declaration (parser);
7915       parser->in_unbraced_linkage_specification_p
7916         = saved_in_unbraced_linkage_specification_p;
7917     }
7918
7919   /* We're done with the linkage-specification.  */
7920   pop_lang_context ();
7921 }
7922
7923 /* Parse a static_assert-declaration.
7924
7925    static_assert-declaration:
7926      static_assert ( constant-expression , string-literal ) ; 
7927
7928    If MEMBER_P, this static_assert is a class member.  */
7929
7930 static void 
7931 cp_parser_static_assert(cp_parser *parser, bool member_p)
7932 {
7933   tree condition;
7934   tree message;
7935   cp_token *token;
7936   location_t saved_loc;
7937
7938   /* Peek at the `static_assert' token so we can keep track of exactly
7939      where the static assertion started.  */
7940   token = cp_lexer_peek_token (parser->lexer);
7941   saved_loc = token->location;
7942
7943   /* Look for the `static_assert' keyword.  */
7944   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
7945                                   "`static_assert'"))
7946     return;
7947
7948   /*  We know we are in a static assertion; commit to any tentative
7949       parse.  */
7950   if (cp_parser_parsing_tentatively (parser))
7951     cp_parser_commit_to_tentative_parse (parser);
7952
7953   /* Parse the `(' starting the static assertion condition.  */
7954   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7955
7956   /* Parse the constant-expression.  */
7957   condition = 
7958     cp_parser_constant_expression (parser,
7959                                    /*allow_non_constant_p=*/false,
7960                                    /*non_constant_p=*/NULL);
7961
7962   /* Parse the separating `,'.  */
7963   cp_parser_require (parser, CPP_COMMA, "`,'");
7964
7965   /* Parse the string-literal message.  */
7966   message = cp_parser_string_literal (parser, 
7967                                       /*translate=*/false,
7968                                       /*wide_ok=*/true);
7969
7970   /* A `)' completes the static assertion.  */
7971   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7972     cp_parser_skip_to_closing_parenthesis (parser, 
7973                                            /*recovering=*/true, 
7974                                            /*or_comma=*/false,
7975                                            /*consume_paren=*/true);
7976
7977   /* A semicolon terminates the declaration.  */
7978   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7979
7980   /* Complete the static assertion, which may mean either processing 
7981      the static assert now or saving it for template instantiation.  */
7982   finish_static_assert (condition, message, saved_loc, member_p);
7983 }
7984
7985 /* Special member functions [gram.special] */
7986
7987 /* Parse a conversion-function-id.
7988
7989    conversion-function-id:
7990      operator conversion-type-id
7991
7992    Returns an IDENTIFIER_NODE representing the operator.  */
7993
7994 static tree
7995 cp_parser_conversion_function_id (cp_parser* parser)
7996 {
7997   tree type;
7998   tree saved_scope;
7999   tree saved_qualifying_scope;
8000   tree saved_object_scope;
8001   tree pushed_scope = NULL_TREE;
8002
8003   /* Look for the `operator' token.  */
8004   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8005     return error_mark_node;
8006   /* When we parse the conversion-type-id, the current scope will be
8007      reset.  However, we need that information in able to look up the
8008      conversion function later, so we save it here.  */
8009   saved_scope = parser->scope;
8010   saved_qualifying_scope = parser->qualifying_scope;
8011   saved_object_scope = parser->object_scope;
8012   /* We must enter the scope of the class so that the names of
8013      entities declared within the class are available in the
8014      conversion-type-id.  For example, consider:
8015
8016        struct S {
8017          typedef int I;
8018          operator I();
8019        };
8020
8021        S::operator I() { ... }
8022
8023      In order to see that `I' is a type-name in the definition, we
8024      must be in the scope of `S'.  */
8025   if (saved_scope)
8026     pushed_scope = push_scope (saved_scope);
8027   /* Parse the conversion-type-id.  */
8028   type = cp_parser_conversion_type_id (parser);
8029   /* Leave the scope of the class, if any.  */
8030   if (pushed_scope)
8031     pop_scope (pushed_scope);
8032   /* Restore the saved scope.  */
8033   parser->scope = saved_scope;
8034   parser->qualifying_scope = saved_qualifying_scope;
8035   parser->object_scope = saved_object_scope;
8036   /* If the TYPE is invalid, indicate failure.  */
8037   if (type == error_mark_node)
8038     return error_mark_node;
8039   return mangle_conv_op_name_for_type (type);
8040 }
8041
8042 /* Parse a conversion-type-id:
8043
8044    conversion-type-id:
8045      type-specifier-seq conversion-declarator [opt]
8046
8047    Returns the TYPE specified.  */
8048
8049 static tree
8050 cp_parser_conversion_type_id (cp_parser* parser)
8051 {
8052   tree attributes;
8053   cp_decl_specifier_seq type_specifiers;
8054   cp_declarator *declarator;
8055   tree type_specified;
8056
8057   /* Parse the attributes.  */
8058   attributes = cp_parser_attributes_opt (parser);
8059   /* Parse the type-specifiers.  */
8060   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8061                                 &type_specifiers);
8062   /* If that didn't work, stop.  */
8063   if (type_specifiers.type == error_mark_node)
8064     return error_mark_node;
8065   /* Parse the conversion-declarator.  */
8066   declarator = cp_parser_conversion_declarator_opt (parser);
8067
8068   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8069                                     /*initialized=*/0, &attributes);
8070   if (attributes)
8071     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8072   return type_specified;
8073 }
8074
8075 /* Parse an (optional) conversion-declarator.
8076
8077    conversion-declarator:
8078      ptr-operator conversion-declarator [opt]
8079
8080    */
8081
8082 static cp_declarator *
8083 cp_parser_conversion_declarator_opt (cp_parser* parser)
8084 {
8085   enum tree_code code;
8086   tree class_type;
8087   cp_cv_quals cv_quals;
8088
8089   /* We don't know if there's a ptr-operator next, or not.  */
8090   cp_parser_parse_tentatively (parser);
8091   /* Try the ptr-operator.  */
8092   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8093   /* If it worked, look for more conversion-declarators.  */
8094   if (cp_parser_parse_definitely (parser))
8095     {
8096       cp_declarator *declarator;
8097
8098       /* Parse another optional declarator.  */
8099       declarator = cp_parser_conversion_declarator_opt (parser);
8100
8101       /* Create the representation of the declarator.  */
8102       if (class_type)
8103         declarator = make_ptrmem_declarator (cv_quals, class_type,
8104                                              declarator);
8105       else if (code == INDIRECT_REF)
8106         declarator = make_pointer_declarator (cv_quals, declarator);
8107       else
8108         declarator = make_reference_declarator (cv_quals, declarator);
8109
8110       return declarator;
8111    }
8112
8113   return NULL;
8114 }
8115
8116 /* Parse an (optional) ctor-initializer.
8117
8118    ctor-initializer:
8119      : mem-initializer-list
8120
8121    Returns TRUE iff the ctor-initializer was actually present.  */
8122
8123 static bool
8124 cp_parser_ctor_initializer_opt (cp_parser* parser)
8125 {
8126   /* If the next token is not a `:', then there is no
8127      ctor-initializer.  */
8128   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8129     {
8130       /* Do default initialization of any bases and members.  */
8131       if (DECL_CONSTRUCTOR_P (current_function_decl))
8132         finish_mem_initializers (NULL_TREE);
8133
8134       return false;
8135     }
8136
8137   /* Consume the `:' token.  */
8138   cp_lexer_consume_token (parser->lexer);
8139   /* And the mem-initializer-list.  */
8140   cp_parser_mem_initializer_list (parser);
8141
8142   return true;
8143 }
8144
8145 /* Parse a mem-initializer-list.
8146
8147    mem-initializer-list:
8148      mem-initializer
8149      mem-initializer , mem-initializer-list  */
8150
8151 static void
8152 cp_parser_mem_initializer_list (cp_parser* parser)
8153 {
8154   tree mem_initializer_list = NULL_TREE;
8155
8156   /* Let the semantic analysis code know that we are starting the
8157      mem-initializer-list.  */
8158   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8159     error ("only constructors take base initializers");
8160
8161   /* Loop through the list.  */
8162   while (true)
8163     {
8164       tree mem_initializer;
8165
8166       /* Parse the mem-initializer.  */
8167       mem_initializer = cp_parser_mem_initializer (parser);
8168       /* Add it to the list, unless it was erroneous.  */
8169       if (mem_initializer != error_mark_node)
8170         {
8171           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8172           mem_initializer_list = mem_initializer;
8173         }
8174       /* If the next token is not a `,', we're done.  */
8175       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8176         break;
8177       /* Consume the `,' token.  */
8178       cp_lexer_consume_token (parser->lexer);
8179     }
8180
8181   /* Perform semantic analysis.  */
8182   if (DECL_CONSTRUCTOR_P (current_function_decl))
8183     finish_mem_initializers (mem_initializer_list);
8184 }
8185
8186 /* Parse a mem-initializer.
8187
8188    mem-initializer:
8189      mem-initializer-id ( expression-list [opt] )
8190
8191    GNU extension:
8192
8193    mem-initializer:
8194      ( expression-list [opt] )
8195
8196    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8197    class) or FIELD_DECL (for a non-static data member) to initialize;
8198    the TREE_VALUE is the expression-list.  An empty initialization
8199    list is represented by void_list_node.  */
8200
8201 static tree
8202 cp_parser_mem_initializer (cp_parser* parser)
8203 {
8204   tree mem_initializer_id;
8205   tree expression_list;
8206   tree member;
8207
8208   /* Find out what is being initialized.  */
8209   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8210     {
8211       pedwarn ("anachronistic old-style base class initializer");
8212       mem_initializer_id = NULL_TREE;
8213     }
8214   else
8215     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8216   member = expand_member_init (mem_initializer_id);
8217   if (member && !DECL_P (member))
8218     in_base_initializer = 1;
8219
8220   expression_list
8221     = cp_parser_parenthesized_expression_list (parser, false,
8222                                                /*cast_p=*/false,
8223                                                /*non_constant_p=*/NULL);
8224   if (expression_list == error_mark_node)
8225     return error_mark_node;
8226   if (!expression_list)
8227     expression_list = void_type_node;
8228
8229   in_base_initializer = 0;
8230
8231   return member ? build_tree_list (member, expression_list) : error_mark_node;
8232 }
8233
8234 /* Parse a mem-initializer-id.
8235
8236    mem-initializer-id:
8237      :: [opt] nested-name-specifier [opt] class-name
8238      identifier
8239
8240    Returns a TYPE indicating the class to be initializer for the first
8241    production.  Returns an IDENTIFIER_NODE indicating the data member
8242    to be initialized for the second production.  */
8243
8244 static tree
8245 cp_parser_mem_initializer_id (cp_parser* parser)
8246 {
8247   bool global_scope_p;
8248   bool nested_name_specifier_p;
8249   bool template_p = false;
8250   tree id;
8251
8252   /* `typename' is not allowed in this context ([temp.res]).  */
8253   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8254     {
8255       error ("keyword %<typename%> not allowed in this context (a qualified "
8256              "member initializer is implicitly a type)");
8257       cp_lexer_consume_token (parser->lexer);
8258     }
8259   /* Look for the optional `::' operator.  */
8260   global_scope_p
8261     = (cp_parser_global_scope_opt (parser,
8262                                    /*current_scope_valid_p=*/false)
8263        != NULL_TREE);
8264   /* Look for the optional nested-name-specifier.  The simplest way to
8265      implement:
8266
8267        [temp.res]
8268
8269        The keyword `typename' is not permitted in a base-specifier or
8270        mem-initializer; in these contexts a qualified name that
8271        depends on a template-parameter is implicitly assumed to be a
8272        type name.
8273
8274      is to assume that we have seen the `typename' keyword at this
8275      point.  */
8276   nested_name_specifier_p
8277     = (cp_parser_nested_name_specifier_opt (parser,
8278                                             /*typename_keyword_p=*/true,
8279                                             /*check_dependency_p=*/true,
8280                                             /*type_p=*/true,
8281                                             /*is_declaration=*/true)
8282        != NULL_TREE);
8283   if (nested_name_specifier_p)
8284     template_p = cp_parser_optional_template_keyword (parser);
8285   /* If there is a `::' operator or a nested-name-specifier, then we
8286      are definitely looking for a class-name.  */
8287   if (global_scope_p || nested_name_specifier_p)
8288     return cp_parser_class_name (parser,
8289                                  /*typename_keyword_p=*/true,
8290                                  /*template_keyword_p=*/template_p,
8291                                  none_type,
8292                                  /*check_dependency_p=*/true,
8293                                  /*class_head_p=*/false,
8294                                  /*is_declaration=*/true);
8295   /* Otherwise, we could also be looking for an ordinary identifier.  */
8296   cp_parser_parse_tentatively (parser);
8297   /* Try a class-name.  */
8298   id = cp_parser_class_name (parser,
8299                              /*typename_keyword_p=*/true,
8300                              /*template_keyword_p=*/false,
8301                              none_type,
8302                              /*check_dependency_p=*/true,
8303                              /*class_head_p=*/false,
8304                              /*is_declaration=*/true);
8305   /* If we found one, we're done.  */
8306   if (cp_parser_parse_definitely (parser))
8307     return id;
8308   /* Otherwise, look for an ordinary identifier.  */
8309   return cp_parser_identifier (parser);
8310 }
8311
8312 /* Overloading [gram.over] */
8313
8314 /* Parse an operator-function-id.
8315
8316    operator-function-id:
8317      operator operator
8318
8319    Returns an IDENTIFIER_NODE for the operator which is a
8320    human-readable spelling of the identifier, e.g., `operator +'.  */
8321
8322 static tree
8323 cp_parser_operator_function_id (cp_parser* parser)
8324 {
8325   /* Look for the `operator' keyword.  */
8326   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8327     return error_mark_node;
8328   /* And then the name of the operator itself.  */
8329   return cp_parser_operator (parser);
8330 }
8331
8332 /* Parse an operator.
8333
8334    operator:
8335      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8336      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8337      || ++ -- , ->* -> () []
8338
8339    GNU Extensions:
8340
8341    operator:
8342      <? >? <?= >?=
8343
8344    Returns an IDENTIFIER_NODE for the operator which is a
8345    human-readable spelling of the identifier, e.g., `operator +'.  */
8346
8347 static tree
8348 cp_parser_operator (cp_parser* parser)
8349 {
8350   tree id = NULL_TREE;
8351   cp_token *token;
8352
8353   /* Peek at the next token.  */
8354   token = cp_lexer_peek_token (parser->lexer);
8355   /* Figure out which operator we have.  */
8356   switch (token->type)
8357     {
8358     case CPP_KEYWORD:
8359       {
8360         enum tree_code op;
8361
8362         /* The keyword should be either `new' or `delete'.  */
8363         if (token->keyword == RID_NEW)
8364           op = NEW_EXPR;
8365         else if (token->keyword == RID_DELETE)
8366           op = DELETE_EXPR;
8367         else
8368           break;
8369
8370         /* Consume the `new' or `delete' token.  */
8371         cp_lexer_consume_token (parser->lexer);
8372
8373         /* Peek at the next token.  */
8374         token = cp_lexer_peek_token (parser->lexer);
8375         /* If it's a `[' token then this is the array variant of the
8376            operator.  */
8377         if (token->type == CPP_OPEN_SQUARE)
8378           {
8379             /* Consume the `[' token.  */
8380             cp_lexer_consume_token (parser->lexer);
8381             /* Look for the `]' token.  */
8382             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8383             id = ansi_opname (op == NEW_EXPR
8384                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8385           }
8386         /* Otherwise, we have the non-array variant.  */
8387         else
8388           id = ansi_opname (op);
8389
8390         return id;
8391       }
8392
8393     case CPP_PLUS:
8394       id = ansi_opname (PLUS_EXPR);
8395       break;
8396
8397     case CPP_MINUS:
8398       id = ansi_opname (MINUS_EXPR);
8399       break;
8400
8401     case CPP_MULT:
8402       id = ansi_opname (MULT_EXPR);
8403       break;
8404
8405     case CPP_DIV:
8406       id = ansi_opname (TRUNC_DIV_EXPR);
8407       break;
8408
8409     case CPP_MOD:
8410       id = ansi_opname (TRUNC_MOD_EXPR);
8411       break;
8412
8413     case CPP_XOR:
8414       id = ansi_opname (BIT_XOR_EXPR);
8415       break;
8416
8417     case CPP_AND:
8418       id = ansi_opname (BIT_AND_EXPR);
8419       break;
8420
8421     case CPP_OR:
8422       id = ansi_opname (BIT_IOR_EXPR);
8423       break;
8424
8425     case CPP_COMPL:
8426       id = ansi_opname (BIT_NOT_EXPR);
8427       break;
8428
8429     case CPP_NOT:
8430       id = ansi_opname (TRUTH_NOT_EXPR);
8431       break;
8432
8433     case CPP_EQ:
8434       id = ansi_assopname (NOP_EXPR);
8435       break;
8436
8437     case CPP_LESS:
8438       id = ansi_opname (LT_EXPR);
8439       break;
8440
8441     case CPP_GREATER:
8442       id = ansi_opname (GT_EXPR);
8443       break;
8444
8445     case CPP_PLUS_EQ:
8446       id = ansi_assopname (PLUS_EXPR);
8447       break;
8448
8449     case CPP_MINUS_EQ:
8450       id = ansi_assopname (MINUS_EXPR);
8451       break;
8452
8453     case CPP_MULT_EQ:
8454       id = ansi_assopname (MULT_EXPR);
8455       break;
8456
8457     case CPP_DIV_EQ:
8458       id = ansi_assopname (TRUNC_DIV_EXPR);
8459       break;
8460
8461     case CPP_MOD_EQ:
8462       id = ansi_assopname (TRUNC_MOD_EXPR);
8463       break;
8464
8465     case CPP_XOR_EQ:
8466       id = ansi_assopname (BIT_XOR_EXPR);
8467       break;
8468
8469     case CPP_AND_EQ:
8470       id = ansi_assopname (BIT_AND_EXPR);
8471       break;
8472
8473     case CPP_OR_EQ:
8474       id = ansi_assopname (BIT_IOR_EXPR);
8475       break;
8476
8477     case CPP_LSHIFT:
8478       id = ansi_opname (LSHIFT_EXPR);
8479       break;
8480
8481     case CPP_RSHIFT:
8482       id = ansi_opname (RSHIFT_EXPR);
8483       break;
8484
8485     case CPP_LSHIFT_EQ:
8486       id = ansi_assopname (LSHIFT_EXPR);
8487       break;
8488
8489     case CPP_RSHIFT_EQ:
8490       id = ansi_assopname (RSHIFT_EXPR);
8491       break;
8492
8493     case CPP_EQ_EQ:
8494       id = ansi_opname (EQ_EXPR);
8495       break;
8496
8497     case CPP_NOT_EQ:
8498       id = ansi_opname (NE_EXPR);
8499       break;
8500
8501     case CPP_LESS_EQ:
8502       id = ansi_opname (LE_EXPR);
8503       break;
8504
8505     case CPP_GREATER_EQ:
8506       id = ansi_opname (GE_EXPR);
8507       break;
8508
8509     case CPP_AND_AND:
8510       id = ansi_opname (TRUTH_ANDIF_EXPR);
8511       break;
8512
8513     case CPP_OR_OR:
8514       id = ansi_opname (TRUTH_ORIF_EXPR);
8515       break;
8516
8517     case CPP_PLUS_PLUS:
8518       id = ansi_opname (POSTINCREMENT_EXPR);
8519       break;
8520
8521     case CPP_MINUS_MINUS:
8522       id = ansi_opname (PREDECREMENT_EXPR);
8523       break;
8524
8525     case CPP_COMMA:
8526       id = ansi_opname (COMPOUND_EXPR);
8527       break;
8528
8529     case CPP_DEREF_STAR:
8530       id = ansi_opname (MEMBER_REF);
8531       break;
8532
8533     case CPP_DEREF:
8534       id = ansi_opname (COMPONENT_REF);
8535       break;
8536
8537     case CPP_OPEN_PAREN:
8538       /* Consume the `('.  */
8539       cp_lexer_consume_token (parser->lexer);
8540       /* Look for the matching `)'.  */
8541       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8542       return ansi_opname (CALL_EXPR);
8543
8544     case CPP_OPEN_SQUARE:
8545       /* Consume the `['.  */
8546       cp_lexer_consume_token (parser->lexer);
8547       /* Look for the matching `]'.  */
8548       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8549       return ansi_opname (ARRAY_REF);
8550
8551     default:
8552       /* Anything else is an error.  */
8553       break;
8554     }
8555
8556   /* If we have selected an identifier, we need to consume the
8557      operator token.  */
8558   if (id)
8559     cp_lexer_consume_token (parser->lexer);
8560   /* Otherwise, no valid operator name was present.  */
8561   else
8562     {
8563       cp_parser_error (parser, "expected operator");
8564       id = error_mark_node;
8565     }
8566
8567   return id;
8568 }
8569
8570 /* Parse a template-declaration.
8571
8572    template-declaration:
8573      export [opt] template < template-parameter-list > declaration
8574
8575    If MEMBER_P is TRUE, this template-declaration occurs within a
8576    class-specifier.
8577
8578    The grammar rule given by the standard isn't correct.  What
8579    is really meant is:
8580
8581    template-declaration:
8582      export [opt] template-parameter-list-seq
8583        decl-specifier-seq [opt] init-declarator [opt] ;
8584      export [opt] template-parameter-list-seq
8585        function-definition
8586
8587    template-parameter-list-seq:
8588      template-parameter-list-seq [opt]
8589      template < template-parameter-list >  */
8590
8591 static void
8592 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8593 {
8594   /* Check for `export'.  */
8595   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8596     {
8597       /* Consume the `export' token.  */
8598       cp_lexer_consume_token (parser->lexer);
8599       /* Warn that we do not support `export'.  */
8600       warning (0, "keyword %<export%> not implemented, and will be ignored");
8601     }
8602
8603   cp_parser_template_declaration_after_export (parser, member_p);
8604 }
8605
8606 /* Parse a template-parameter-list.
8607
8608    template-parameter-list:
8609      template-parameter
8610      template-parameter-list , template-parameter
8611
8612    Returns a TREE_LIST.  Each node represents a template parameter.
8613    The nodes are connected via their TREE_CHAINs.  */
8614
8615 static tree
8616 cp_parser_template_parameter_list (cp_parser* parser)
8617 {
8618   tree parameter_list = NULL_TREE;
8619
8620   begin_template_parm_list ();
8621   while (true)
8622     {
8623       tree parameter;
8624       cp_token *token;
8625       bool is_non_type;
8626
8627       /* Parse the template-parameter.  */
8628       parameter = cp_parser_template_parameter (parser, &is_non_type);
8629       /* Add it to the list.  */
8630       if (parameter != error_mark_node)
8631         parameter_list = process_template_parm (parameter_list,
8632                                                 parameter,
8633                                                 is_non_type);
8634       else
8635        {
8636          tree err_parm = build_tree_list (parameter, parameter);
8637          TREE_VALUE (err_parm) = error_mark_node;
8638          parameter_list = chainon (parameter_list, err_parm);
8639        }
8640
8641       /* Peek at the next token.  */
8642       token = cp_lexer_peek_token (parser->lexer);
8643       /* If it's not a `,', we're done.  */
8644       if (token->type != CPP_COMMA)
8645         break;
8646       /* Otherwise, consume the `,' token.  */
8647       cp_lexer_consume_token (parser->lexer);
8648     }
8649
8650   return end_template_parm_list (parameter_list);
8651 }
8652
8653 /* Parse a template-parameter.
8654
8655    template-parameter:
8656      type-parameter
8657      parameter-declaration
8658
8659    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8660    the parameter.  The TREE_PURPOSE is the default value, if any.
8661    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8662    iff this parameter is a non-type parameter.  */
8663
8664 static tree
8665 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8666 {
8667   cp_token *token;
8668   cp_parameter_declarator *parameter_declarator;
8669   tree parm;
8670
8671   /* Assume it is a type parameter or a template parameter.  */
8672   *is_non_type = false;
8673   /* Peek at the next token.  */
8674   token = cp_lexer_peek_token (parser->lexer);
8675   /* If it is `class' or `template', we have a type-parameter.  */
8676   if (token->keyword == RID_TEMPLATE)
8677     return cp_parser_type_parameter (parser);
8678   /* If it is `class' or `typename' we do not know yet whether it is a
8679      type parameter or a non-type parameter.  Consider:
8680
8681        template <typename T, typename T::X X> ...
8682
8683      or:
8684
8685        template <class C, class D*> ...
8686
8687      Here, the first parameter is a type parameter, and the second is
8688      a non-type parameter.  We can tell by looking at the token after
8689      the identifier -- if it is a `,', `=', or `>' then we have a type
8690      parameter.  */
8691   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8692     {
8693       /* Peek at the token after `class' or `typename'.  */
8694       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8695       /* If it's an identifier, skip it.  */
8696       if (token->type == CPP_NAME)
8697         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8698       /* Now, see if the token looks like the end of a template
8699          parameter.  */
8700       if (token->type == CPP_COMMA
8701           || token->type == CPP_EQ
8702           || token->type == CPP_GREATER)
8703         return cp_parser_type_parameter (parser);
8704     }
8705
8706   /* Otherwise, it is a non-type parameter.
8707
8708      [temp.param]
8709
8710      When parsing a default template-argument for a non-type
8711      template-parameter, the first non-nested `>' is taken as the end
8712      of the template parameter-list rather than a greater-than
8713      operator.  */
8714   *is_non_type = true;
8715   parameter_declarator
8716      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8717                                         /*parenthesized_p=*/NULL);
8718   parm = grokdeclarator (parameter_declarator->declarator,
8719                          &parameter_declarator->decl_specifiers,
8720                          PARM, /*initialized=*/0,
8721                          /*attrlist=*/NULL);
8722   if (parm == error_mark_node)
8723     return error_mark_node;
8724   return build_tree_list (parameter_declarator->default_argument, parm);
8725 }
8726
8727 /* Parse a type-parameter.
8728
8729    type-parameter:
8730      class identifier [opt]
8731      class identifier [opt] = type-id
8732      typename identifier [opt]
8733      typename identifier [opt] = type-id
8734      template < template-parameter-list > class identifier [opt]
8735      template < template-parameter-list > class identifier [opt]
8736        = id-expression
8737
8738    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8739    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8740    the declaration of the parameter.  */
8741
8742 static tree
8743 cp_parser_type_parameter (cp_parser* parser)
8744 {
8745   cp_token *token;
8746   tree parameter;
8747
8748   /* Look for a keyword to tell us what kind of parameter this is.  */
8749   token = cp_parser_require (parser, CPP_KEYWORD,
8750                              "`class', `typename', or `template'");
8751   if (!token)
8752     return error_mark_node;
8753
8754   switch (token->keyword)
8755     {
8756     case RID_CLASS:
8757     case RID_TYPENAME:
8758       {
8759         tree identifier;
8760         tree default_argument;
8761
8762         /* If the next token is an identifier, then it names the
8763            parameter.  */
8764         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8765           identifier = cp_parser_identifier (parser);
8766         else
8767           identifier = NULL_TREE;
8768
8769         /* Create the parameter.  */
8770         parameter = finish_template_type_parm (class_type_node, identifier);
8771
8772         /* If the next token is an `=', we have a default argument.  */
8773         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8774           {
8775             /* Consume the `=' token.  */
8776             cp_lexer_consume_token (parser->lexer);
8777             /* Parse the default-argument.  */
8778             push_deferring_access_checks (dk_no_deferred);
8779             default_argument = cp_parser_type_id (parser);
8780             pop_deferring_access_checks ();
8781           }
8782         else
8783           default_argument = NULL_TREE;
8784
8785         /* Create the combined representation of the parameter and the
8786            default argument.  */
8787         parameter = build_tree_list (default_argument, parameter);
8788       }
8789       break;
8790
8791     case RID_TEMPLATE:
8792       {
8793         tree parameter_list;
8794         tree identifier;
8795         tree default_argument;
8796
8797         /* Look for the `<'.  */
8798         cp_parser_require (parser, CPP_LESS, "`<'");
8799         /* Parse the template-parameter-list.  */
8800         parameter_list = cp_parser_template_parameter_list (parser);
8801         /* Look for the `>'.  */
8802         cp_parser_require (parser, CPP_GREATER, "`>'");
8803         /* Look for the `class' keyword.  */
8804         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8805         /* If the next token is an `=', then there is a
8806            default-argument.  If the next token is a `>', we are at
8807            the end of the parameter-list.  If the next token is a `,',
8808            then we are at the end of this parameter.  */
8809         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8810             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8811             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8812           {
8813             identifier = cp_parser_identifier (parser);
8814             /* Treat invalid names as if the parameter were nameless.  */
8815             if (identifier == error_mark_node)
8816               identifier = NULL_TREE;
8817           }
8818         else
8819           identifier = NULL_TREE;
8820
8821         /* Create the template parameter.  */
8822         parameter = finish_template_template_parm (class_type_node,
8823                                                    identifier);
8824
8825         /* If the next token is an `=', then there is a
8826            default-argument.  */
8827         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8828           {
8829             bool is_template;
8830
8831             /* Consume the `='.  */
8832             cp_lexer_consume_token (parser->lexer);
8833             /* Parse the id-expression.  */
8834             push_deferring_access_checks (dk_no_deferred);
8835             default_argument
8836               = cp_parser_id_expression (parser,
8837                                          /*template_keyword_p=*/false,
8838                                          /*check_dependency_p=*/true,
8839                                          /*template_p=*/&is_template,
8840                                          /*declarator_p=*/false,
8841                                          /*optional_p=*/false);
8842             if (TREE_CODE (default_argument) == TYPE_DECL)
8843               /* If the id-expression was a template-id that refers to
8844                  a template-class, we already have the declaration here,
8845                  so no further lookup is needed.  */
8846                  ;
8847             else
8848               /* Look up the name.  */
8849               default_argument
8850                 = cp_parser_lookup_name (parser, default_argument,
8851                                          none_type,
8852                                          /*is_template=*/is_template,
8853                                          /*is_namespace=*/false,
8854                                          /*check_dependency=*/true,
8855                                          /*ambiguous_decls=*/NULL);
8856             /* See if the default argument is valid.  */
8857             default_argument
8858               = check_template_template_default_arg (default_argument);
8859             pop_deferring_access_checks ();
8860           }
8861         else
8862           default_argument = NULL_TREE;
8863
8864         /* Create the combined representation of the parameter and the
8865            default argument.  */
8866         parameter = build_tree_list (default_argument, parameter);
8867       }
8868       break;
8869
8870     default:
8871       gcc_unreachable ();
8872       break;
8873     }
8874
8875   return parameter;
8876 }
8877
8878 /* Parse a template-id.
8879
8880    template-id:
8881      template-name < template-argument-list [opt] >
8882
8883    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8884    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8885    returned.  Otherwise, if the template-name names a function, or set
8886    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8887    names a class, returns a TYPE_DECL for the specialization.
8888
8889    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8890    uninstantiated templates.  */
8891
8892 static tree
8893 cp_parser_template_id (cp_parser *parser,
8894                        bool template_keyword_p,
8895                        bool check_dependency_p,
8896                        bool is_declaration)
8897 {
8898   int i;
8899   tree template;
8900   tree arguments;
8901   tree template_id;
8902   cp_token_position start_of_id = 0;
8903   deferred_access_check *chk;
8904   VEC (deferred_access_check,gc) *access_check;
8905   cp_token *next_token, *next_token_2;
8906   bool is_identifier;
8907
8908   /* If the next token corresponds to a template-id, there is no need
8909      to reparse it.  */
8910   next_token = cp_lexer_peek_token (parser->lexer);
8911   if (next_token->type == CPP_TEMPLATE_ID)
8912     {
8913       struct tree_check *check_value;
8914
8915       /* Get the stored value.  */
8916       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
8917       /* Perform any access checks that were deferred.  */
8918       access_check = check_value->checks;
8919       if (access_check)
8920         {
8921           for (i = 0 ;
8922                VEC_iterate (deferred_access_check, access_check, i, chk) ;
8923                ++i)
8924             {
8925               perform_or_defer_access_check (chk->binfo,
8926                                              chk->decl,
8927                                              chk->diag_decl);
8928             }
8929         }
8930       /* Return the stored value.  */
8931       return check_value->value;
8932     }
8933
8934   /* Avoid performing name lookup if there is no possibility of
8935      finding a template-id.  */
8936   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8937       || (next_token->type == CPP_NAME
8938           && !cp_parser_nth_token_starts_template_argument_list_p
8939                (parser, 2)))
8940     {
8941       cp_parser_error (parser, "expected template-id");
8942       return error_mark_node;
8943     }
8944
8945   /* Remember where the template-id starts.  */
8946   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8947     start_of_id = cp_lexer_token_position (parser->lexer, false);
8948
8949   push_deferring_access_checks (dk_deferred);
8950
8951   /* Parse the template-name.  */
8952   is_identifier = false;
8953   template = cp_parser_template_name (parser, template_keyword_p,
8954                                       check_dependency_p,
8955                                       is_declaration,
8956                                       &is_identifier);
8957   if (template == error_mark_node || is_identifier)
8958     {
8959       pop_deferring_access_checks ();
8960       return template;
8961     }
8962
8963   /* If we find the sequence `[:' after a template-name, it's probably
8964      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8965      parse correctly the argument list.  */
8966   next_token = cp_lexer_peek_token (parser->lexer);
8967   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8968   if (next_token->type == CPP_OPEN_SQUARE
8969       && next_token->flags & DIGRAPH
8970       && next_token_2->type == CPP_COLON
8971       && !(next_token_2->flags & PREV_WHITE))
8972     {
8973       cp_parser_parse_tentatively (parser);
8974       /* Change `:' into `::'.  */
8975       next_token_2->type = CPP_SCOPE;
8976       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8977          CPP_LESS.  */
8978       cp_lexer_consume_token (parser->lexer);
8979       /* Parse the arguments.  */
8980       arguments = cp_parser_enclosed_template_argument_list (parser);
8981       if (!cp_parser_parse_definitely (parser))
8982         {
8983           /* If we couldn't parse an argument list, then we revert our changes
8984              and return simply an error. Maybe this is not a template-id
8985              after all.  */
8986           next_token_2->type = CPP_COLON;
8987           cp_parser_error (parser, "expected %<<%>");
8988           pop_deferring_access_checks ();
8989           return error_mark_node;
8990         }
8991       /* Otherwise, emit an error about the invalid digraph, but continue
8992          parsing because we got our argument list.  */
8993       pedwarn ("%<<::%> cannot begin a template-argument list");
8994       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8995               "between %<<%> and %<::%>");
8996       if (!flag_permissive)
8997         {
8998           static bool hint;
8999           if (!hint)
9000             {
9001               inform ("(if you use -fpermissive G++ will accept your code)");
9002               hint = true;
9003             }
9004         }
9005     }
9006   else
9007     {
9008       /* Look for the `<' that starts the template-argument-list.  */
9009       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9010         {
9011           pop_deferring_access_checks ();
9012           return error_mark_node;
9013         }
9014       /* Parse the arguments.  */
9015       arguments = cp_parser_enclosed_template_argument_list (parser);
9016     }
9017
9018   /* Build a representation of the specialization.  */
9019   if (TREE_CODE (template) == IDENTIFIER_NODE)
9020     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9021   else if (DECL_CLASS_TEMPLATE_P (template)
9022            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9023     {
9024       bool entering_scope;
9025       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9026          template (rather than some instantiation thereof) only if
9027          is not nested within some other construct.  For example, in
9028          "template <typename T> void f(T) { A<T>::", A<T> is just an
9029          instantiation of A.  */
9030       entering_scope = (template_parm_scope_p ()
9031                         && cp_lexer_next_token_is (parser->lexer,
9032                                                    CPP_SCOPE));
9033       template_id
9034         = finish_template_type (template, arguments, entering_scope);
9035     }
9036   else
9037     {
9038       /* If it's not a class-template or a template-template, it should be
9039          a function-template.  */
9040       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9041                    || TREE_CODE (template) == OVERLOAD
9042                    || BASELINK_P (template)));
9043
9044       template_id = lookup_template_function (template, arguments);
9045     }
9046
9047   /* If parsing tentatively, replace the sequence of tokens that makes
9048      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9049      should we re-parse the token stream, we will not have to repeat
9050      the effort required to do the parse, nor will we issue duplicate
9051      error messages about problems during instantiation of the
9052      template.  */
9053   if (start_of_id)
9054     {
9055       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9056
9057       /* Reset the contents of the START_OF_ID token.  */
9058       token->type = CPP_TEMPLATE_ID;
9059       /* Retrieve any deferred checks.  Do not pop this access checks yet
9060          so the memory will not be reclaimed during token replacing below.  */
9061       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9062       token->u.tree_check_value->value = template_id;
9063       token->u.tree_check_value->checks = get_deferred_access_checks ();
9064       token->keyword = RID_MAX;
9065
9066       /* Purge all subsequent tokens.  */
9067       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9068
9069       /* ??? Can we actually assume that, if template_id ==
9070          error_mark_node, we will have issued a diagnostic to the
9071          user, as opposed to simply marking the tentative parse as
9072          failed?  */
9073       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9074         error ("parse error in template argument list");
9075     }
9076
9077   pop_deferring_access_checks ();
9078   return template_id;
9079 }
9080
9081 /* Parse a template-name.
9082
9083    template-name:
9084      identifier
9085
9086    The standard should actually say:
9087
9088    template-name:
9089      identifier
9090      operator-function-id
9091
9092    A defect report has been filed about this issue.
9093
9094    A conversion-function-id cannot be a template name because they cannot
9095    be part of a template-id. In fact, looking at this code:
9096
9097    a.operator K<int>()
9098
9099    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9100    It is impossible to call a templated conversion-function-id with an
9101    explicit argument list, since the only allowed template parameter is
9102    the type to which it is converting.
9103
9104    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9105    `template' keyword, in a construction like:
9106
9107      T::template f<3>()
9108
9109    In that case `f' is taken to be a template-name, even though there
9110    is no way of knowing for sure.
9111
9112    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9113    name refers to a set of overloaded functions, at least one of which
9114    is a template, or an IDENTIFIER_NODE with the name of the template,
9115    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9116    names are looked up inside uninstantiated templates.  */
9117
9118 static tree
9119 cp_parser_template_name (cp_parser* parser,
9120                          bool template_keyword_p,
9121                          bool check_dependency_p,
9122                          bool is_declaration,
9123                          bool *is_identifier)
9124 {
9125   tree identifier;
9126   tree decl;
9127   tree fns;
9128
9129   /* If the next token is `operator', then we have either an
9130      operator-function-id or a conversion-function-id.  */
9131   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9132     {
9133       /* We don't know whether we're looking at an
9134          operator-function-id or a conversion-function-id.  */
9135       cp_parser_parse_tentatively (parser);
9136       /* Try an operator-function-id.  */
9137       identifier = cp_parser_operator_function_id (parser);
9138       /* If that didn't work, try a conversion-function-id.  */
9139       if (!cp_parser_parse_definitely (parser))
9140         {
9141           cp_parser_error (parser, "expected template-name");
9142           return error_mark_node;
9143         }
9144     }
9145   /* Look for the identifier.  */
9146   else
9147     identifier = cp_parser_identifier (parser);
9148
9149   /* If we didn't find an identifier, we don't have a template-id.  */
9150   if (identifier == error_mark_node)
9151     return error_mark_node;
9152
9153   /* If the name immediately followed the `template' keyword, then it
9154      is a template-name.  However, if the next token is not `<', then
9155      we do not treat it as a template-name, since it is not being used
9156      as part of a template-id.  This enables us to handle constructs
9157      like:
9158
9159        template <typename T> struct S { S(); };
9160        template <typename T> S<T>::S();
9161
9162      correctly.  We would treat `S' as a template -- if it were `S<T>'
9163      -- but we do not if there is no `<'.  */
9164
9165   if (processing_template_decl
9166       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9167     {
9168       /* In a declaration, in a dependent context, we pretend that the
9169          "template" keyword was present in order to improve error
9170          recovery.  For example, given:
9171
9172            template <typename T> void f(T::X<int>);
9173
9174          we want to treat "X<int>" as a template-id.  */
9175       if (is_declaration
9176           && !template_keyword_p
9177           && parser->scope && TYPE_P (parser->scope)
9178           && check_dependency_p
9179           && dependent_type_p (parser->scope)
9180           /* Do not do this for dtors (or ctors), since they never
9181              need the template keyword before their name.  */
9182           && !constructor_name_p (identifier, parser->scope))
9183         {
9184           cp_token_position start = 0;
9185
9186           /* Explain what went wrong.  */
9187           error ("non-template %qD used as template", identifier);
9188           inform ("use %<%T::template %D%> to indicate that it is a template",
9189                   parser->scope, identifier);
9190           /* If parsing tentatively, find the location of the "<" token.  */
9191           if (cp_parser_simulate_error (parser))
9192             start = cp_lexer_token_position (parser->lexer, true);
9193           /* Parse the template arguments so that we can issue error
9194              messages about them.  */
9195           cp_lexer_consume_token (parser->lexer);
9196           cp_parser_enclosed_template_argument_list (parser);
9197           /* Skip tokens until we find a good place from which to
9198              continue parsing.  */
9199           cp_parser_skip_to_closing_parenthesis (parser,
9200                                                  /*recovering=*/true,
9201                                                  /*or_comma=*/true,
9202                                                  /*consume_paren=*/false);
9203           /* If parsing tentatively, permanently remove the
9204              template argument list.  That will prevent duplicate
9205              error messages from being issued about the missing
9206              "template" keyword.  */
9207           if (start)
9208             cp_lexer_purge_tokens_after (parser->lexer, start);
9209           if (is_identifier)
9210             *is_identifier = true;
9211           return identifier;
9212         }
9213
9214       /* If the "template" keyword is present, then there is generally
9215          no point in doing name-lookup, so we just return IDENTIFIER.
9216          But, if the qualifying scope is non-dependent then we can
9217          (and must) do name-lookup normally.  */
9218       if (template_keyword_p
9219           && (!parser->scope
9220               || (TYPE_P (parser->scope)
9221                   && dependent_type_p (parser->scope))))
9222         return identifier;
9223     }
9224
9225   /* Look up the name.  */
9226   decl = cp_parser_lookup_name (parser, identifier,
9227                                 none_type,
9228                                 /*is_template=*/false,
9229                                 /*is_namespace=*/false,
9230                                 check_dependency_p,
9231                                 /*ambiguous_decls=*/NULL);
9232   decl = maybe_get_template_decl_from_type_decl (decl);
9233
9234   /* If DECL is a template, then the name was a template-name.  */
9235   if (TREE_CODE (decl) == TEMPLATE_DECL)
9236     ;
9237   else
9238     {
9239       tree fn = NULL_TREE;
9240
9241       /* The standard does not explicitly indicate whether a name that
9242          names a set of overloaded declarations, some of which are
9243          templates, is a template-name.  However, such a name should
9244          be a template-name; otherwise, there is no way to form a
9245          template-id for the overloaded templates.  */
9246       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9247       if (TREE_CODE (fns) == OVERLOAD)
9248         for (fn = fns; fn; fn = OVL_NEXT (fn))
9249           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9250             break;
9251
9252       if (!fn)
9253         {
9254           /* The name does not name a template.  */
9255           cp_parser_error (parser, "expected template-name");
9256           return error_mark_node;
9257         }
9258     }
9259
9260   /* If DECL is dependent, and refers to a function, then just return
9261      its name; we will look it up again during template instantiation.  */
9262   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9263     {
9264       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9265       if (TYPE_P (scope) && dependent_type_p (scope))
9266         return identifier;
9267     }
9268
9269   return decl;
9270 }
9271
9272 /* Parse a template-argument-list.
9273
9274    template-argument-list:
9275      template-argument
9276      template-argument-list , template-argument
9277
9278    Returns a TREE_VEC containing the arguments.  */
9279
9280 static tree
9281 cp_parser_template_argument_list (cp_parser* parser)
9282 {
9283   tree fixed_args[10];
9284   unsigned n_args = 0;
9285   unsigned alloced = 10;
9286   tree *arg_ary = fixed_args;
9287   tree vec;
9288   bool saved_in_template_argument_list_p;
9289   bool saved_ice_p;
9290   bool saved_non_ice_p;
9291
9292   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9293   parser->in_template_argument_list_p = true;
9294   /* Even if the template-id appears in an integral
9295      constant-expression, the contents of the argument list do
9296      not.  */
9297   saved_ice_p = parser->integral_constant_expression_p;
9298   parser->integral_constant_expression_p = false;
9299   saved_non_ice_p = parser->non_integral_constant_expression_p;
9300   parser->non_integral_constant_expression_p = false;
9301   /* Parse the arguments.  */
9302   do
9303     {
9304       tree argument;
9305
9306       if (n_args)
9307         /* Consume the comma.  */
9308         cp_lexer_consume_token (parser->lexer);
9309
9310       /* Parse the template-argument.  */
9311       argument = cp_parser_template_argument (parser);
9312       if (n_args == alloced)
9313         {
9314           alloced *= 2;
9315
9316           if (arg_ary == fixed_args)
9317             {
9318               arg_ary = XNEWVEC (tree, alloced);
9319               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9320             }
9321           else
9322             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9323         }
9324       arg_ary[n_args++] = argument;
9325     }
9326   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9327
9328   vec = make_tree_vec (n_args);
9329
9330   while (n_args--)
9331     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9332
9333   if (arg_ary != fixed_args)
9334     free (arg_ary);
9335   parser->non_integral_constant_expression_p = saved_non_ice_p;
9336   parser->integral_constant_expression_p = saved_ice_p;
9337   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9338   return vec;
9339 }
9340
9341 /* Parse a template-argument.
9342
9343    template-argument:
9344      assignment-expression
9345      type-id
9346      id-expression
9347
9348    The representation is that of an assignment-expression, type-id, or
9349    id-expression -- except that the qualified id-expression is
9350    evaluated, so that the value returned is either a DECL or an
9351    OVERLOAD.
9352
9353    Although the standard says "assignment-expression", it forbids
9354    throw-expressions or assignments in the template argument.
9355    Therefore, we use "conditional-expression" instead.  */
9356
9357 static tree
9358 cp_parser_template_argument (cp_parser* parser)
9359 {
9360   tree argument;
9361   bool template_p;
9362   bool address_p;
9363   bool maybe_type_id = false;
9364   cp_token *token;
9365   cp_id_kind idk;
9366
9367   /* There's really no way to know what we're looking at, so we just
9368      try each alternative in order.
9369
9370        [temp.arg]
9371
9372        In a template-argument, an ambiguity between a type-id and an
9373        expression is resolved to a type-id, regardless of the form of
9374        the corresponding template-parameter.
9375
9376      Therefore, we try a type-id first.  */
9377   cp_parser_parse_tentatively (parser);
9378   argument = cp_parser_type_id (parser);
9379   /* If there was no error parsing the type-id but the next token is a '>>',
9380      we probably found a typo for '> >'. But there are type-id which are
9381      also valid expressions. For instance:
9382
9383      struct X { int operator >> (int); };
9384      template <int V> struct Foo {};
9385      Foo<X () >> 5> r;
9386
9387      Here 'X()' is a valid type-id of a function type, but the user just
9388      wanted to write the expression "X() >> 5". Thus, we remember that we
9389      found a valid type-id, but we still try to parse the argument as an
9390      expression to see what happens.  */
9391   if (!cp_parser_error_occurred (parser)
9392       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9393     {
9394       maybe_type_id = true;
9395       cp_parser_abort_tentative_parse (parser);
9396     }
9397   else
9398     {
9399       /* If the next token isn't a `,' or a `>', then this argument wasn't
9400       really finished. This means that the argument is not a valid
9401       type-id.  */
9402       if (!cp_parser_next_token_ends_template_argument_p (parser))
9403         cp_parser_error (parser, "expected template-argument");
9404       /* If that worked, we're done.  */
9405       if (cp_parser_parse_definitely (parser))
9406         return argument;
9407     }
9408   /* We're still not sure what the argument will be.  */
9409   cp_parser_parse_tentatively (parser);
9410   /* Try a template.  */
9411   argument = cp_parser_id_expression (parser,
9412                                       /*template_keyword_p=*/false,
9413                                       /*check_dependency_p=*/true,
9414                                       &template_p,
9415                                       /*declarator_p=*/false,
9416                                       /*optional_p=*/false);
9417   /* If the next token isn't a `,' or a `>', then this argument wasn't
9418      really finished.  */
9419   if (!cp_parser_next_token_ends_template_argument_p (parser))
9420     cp_parser_error (parser, "expected template-argument");
9421   if (!cp_parser_error_occurred (parser))
9422     {
9423       /* Figure out what is being referred to.  If the id-expression
9424          was for a class template specialization, then we will have a
9425          TYPE_DECL at this point.  There is no need to do name lookup
9426          at this point in that case.  */
9427       if (TREE_CODE (argument) != TYPE_DECL)
9428         argument = cp_parser_lookup_name (parser, argument,
9429                                           none_type,
9430                                           /*is_template=*/template_p,
9431                                           /*is_namespace=*/false,
9432                                           /*check_dependency=*/true,
9433                                           /*ambiguous_decls=*/NULL);
9434       if (TREE_CODE (argument) != TEMPLATE_DECL
9435           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9436         cp_parser_error (parser, "expected template-name");
9437     }
9438   if (cp_parser_parse_definitely (parser))
9439     return argument;
9440   /* It must be a non-type argument.  There permitted cases are given
9441      in [temp.arg.nontype]:
9442
9443      -- an integral constant-expression of integral or enumeration
9444         type; or
9445
9446      -- the name of a non-type template-parameter; or
9447
9448      -- the name of an object or function with external linkage...
9449
9450      -- the address of an object or function with external linkage...
9451
9452      -- a pointer to member...  */
9453   /* Look for a non-type template parameter.  */
9454   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9455     {
9456       cp_parser_parse_tentatively (parser);
9457       argument = cp_parser_primary_expression (parser,
9458                                                /*adress_p=*/false,
9459                                                /*cast_p=*/false,
9460                                                /*template_arg_p=*/true,
9461                                                &idk);
9462       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9463           || !cp_parser_next_token_ends_template_argument_p (parser))
9464         cp_parser_simulate_error (parser);
9465       if (cp_parser_parse_definitely (parser))
9466         return argument;
9467     }
9468
9469   /* If the next token is "&", the argument must be the address of an
9470      object or function with external linkage.  */
9471   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9472   if (address_p)
9473     cp_lexer_consume_token (parser->lexer);
9474   /* See if we might have an id-expression.  */
9475   token = cp_lexer_peek_token (parser->lexer);
9476   if (token->type == CPP_NAME
9477       || token->keyword == RID_OPERATOR
9478       || token->type == CPP_SCOPE
9479       || token->type == CPP_TEMPLATE_ID
9480       || token->type == CPP_NESTED_NAME_SPECIFIER)
9481     {
9482       cp_parser_parse_tentatively (parser);
9483       argument = cp_parser_primary_expression (parser,
9484                                                address_p,
9485                                                /*cast_p=*/false,
9486                                                /*template_arg_p=*/true,
9487                                                &idk);
9488       if (cp_parser_error_occurred (parser)
9489           || !cp_parser_next_token_ends_template_argument_p (parser))
9490         cp_parser_abort_tentative_parse (parser);
9491       else
9492         {
9493           if (TREE_CODE (argument) == INDIRECT_REF)
9494             {
9495               gcc_assert (REFERENCE_REF_P (argument));
9496               argument = TREE_OPERAND (argument, 0);
9497             }
9498
9499           if (TREE_CODE (argument) == VAR_DECL)
9500             {
9501               /* A variable without external linkage might still be a
9502                  valid constant-expression, so no error is issued here
9503                  if the external-linkage check fails.  */
9504               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9505                 cp_parser_simulate_error (parser);
9506             }
9507           else if (is_overloaded_fn (argument))
9508             /* All overloaded functions are allowed; if the external
9509                linkage test does not pass, an error will be issued
9510                later.  */
9511             ;
9512           else if (address_p
9513                    && (TREE_CODE (argument) == OFFSET_REF
9514                        || TREE_CODE (argument) == SCOPE_REF))
9515             /* A pointer-to-member.  */
9516             ;
9517           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9518             ;
9519           else
9520             cp_parser_simulate_error (parser);
9521
9522           if (cp_parser_parse_definitely (parser))
9523             {
9524               if (address_p)
9525                 argument = build_x_unary_op (ADDR_EXPR, argument);
9526               return argument;
9527             }
9528         }
9529     }
9530   /* If the argument started with "&", there are no other valid
9531      alternatives at this point.  */
9532   if (address_p)
9533     {
9534       cp_parser_error (parser, "invalid non-type template argument");
9535       return error_mark_node;
9536     }
9537
9538   /* If the argument wasn't successfully parsed as a type-id followed
9539      by '>>', the argument can only be a constant expression now.
9540      Otherwise, we try parsing the constant-expression tentatively,
9541      because the argument could really be a type-id.  */
9542   if (maybe_type_id)
9543     cp_parser_parse_tentatively (parser);
9544   argument = cp_parser_constant_expression (parser,
9545                                             /*allow_non_constant_p=*/false,
9546                                             /*non_constant_p=*/NULL);
9547   argument = fold_non_dependent_expr (argument);
9548   if (!maybe_type_id)
9549     return argument;
9550   if (!cp_parser_next_token_ends_template_argument_p (parser))
9551     cp_parser_error (parser, "expected template-argument");
9552   if (cp_parser_parse_definitely (parser))
9553     return argument;
9554   /* We did our best to parse the argument as a non type-id, but that
9555      was the only alternative that matched (albeit with a '>' after
9556      it). We can assume it's just a typo from the user, and a
9557      diagnostic will then be issued.  */
9558   return cp_parser_type_id (parser);
9559 }
9560
9561 /* Parse an explicit-instantiation.
9562
9563    explicit-instantiation:
9564      template declaration
9565
9566    Although the standard says `declaration', what it really means is:
9567
9568    explicit-instantiation:
9569      template decl-specifier-seq [opt] declarator [opt] ;
9570
9571    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9572    supposed to be allowed.  A defect report has been filed about this
9573    issue.
9574
9575    GNU Extension:
9576
9577    explicit-instantiation:
9578      storage-class-specifier template
9579        decl-specifier-seq [opt] declarator [opt] ;
9580      function-specifier template
9581        decl-specifier-seq [opt] declarator [opt] ;  */
9582
9583 static void
9584 cp_parser_explicit_instantiation (cp_parser* parser)
9585 {
9586   int declares_class_or_enum;
9587   cp_decl_specifier_seq decl_specifiers;
9588   tree extension_specifier = NULL_TREE;
9589
9590   /* Look for an (optional) storage-class-specifier or
9591      function-specifier.  */
9592   if (cp_parser_allow_gnu_extensions_p (parser))
9593     {
9594       extension_specifier
9595         = cp_parser_storage_class_specifier_opt (parser);
9596       if (!extension_specifier)
9597         extension_specifier
9598           = cp_parser_function_specifier_opt (parser,
9599                                               /*decl_specs=*/NULL);
9600     }
9601
9602   /* Look for the `template' keyword.  */
9603   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9604   /* Let the front end know that we are processing an explicit
9605      instantiation.  */
9606   begin_explicit_instantiation ();
9607   /* [temp.explicit] says that we are supposed to ignore access
9608      control while processing explicit instantiation directives.  */
9609   push_deferring_access_checks (dk_no_check);
9610   /* Parse a decl-specifier-seq.  */
9611   cp_parser_decl_specifier_seq (parser,
9612                                 CP_PARSER_FLAGS_OPTIONAL,
9613                                 &decl_specifiers,
9614                                 &declares_class_or_enum);
9615   /* If there was exactly one decl-specifier, and it declared a class,
9616      and there's no declarator, then we have an explicit type
9617      instantiation.  */
9618   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9619     {
9620       tree type;
9621
9622       type = check_tag_decl (&decl_specifiers);
9623       /* Turn access control back on for names used during
9624          template instantiation.  */
9625       pop_deferring_access_checks ();
9626       if (type)
9627         do_type_instantiation (type, extension_specifier,
9628                                /*complain=*/tf_error);
9629     }
9630   else
9631     {
9632       cp_declarator *declarator;
9633       tree decl;
9634
9635       /* Parse the declarator.  */
9636       declarator
9637         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9638                                 /*ctor_dtor_or_conv_p=*/NULL,
9639                                 /*parenthesized_p=*/NULL,
9640                                 /*member_p=*/false);
9641       if (declares_class_or_enum & 2)
9642         cp_parser_check_for_definition_in_return_type (declarator,
9643                                                        decl_specifiers.type);
9644       if (declarator != cp_error_declarator)
9645         {
9646           decl = grokdeclarator (declarator, &decl_specifiers,
9647                                  NORMAL, 0, &decl_specifiers.attributes);
9648           /* Turn access control back on for names used during
9649              template instantiation.  */
9650           pop_deferring_access_checks ();
9651           /* Do the explicit instantiation.  */
9652           do_decl_instantiation (decl, extension_specifier);
9653         }
9654       else
9655         {
9656           pop_deferring_access_checks ();
9657           /* Skip the body of the explicit instantiation.  */
9658           cp_parser_skip_to_end_of_statement (parser);
9659         }
9660     }
9661   /* We're done with the instantiation.  */
9662   end_explicit_instantiation ();
9663
9664   cp_parser_consume_semicolon_at_end_of_statement (parser);
9665 }
9666
9667 /* Parse an explicit-specialization.
9668
9669    explicit-specialization:
9670      template < > declaration
9671
9672    Although the standard says `declaration', what it really means is:
9673
9674    explicit-specialization:
9675      template <> decl-specifier [opt] init-declarator [opt] ;
9676      template <> function-definition
9677      template <> explicit-specialization
9678      template <> template-declaration  */
9679
9680 static void
9681 cp_parser_explicit_specialization (cp_parser* parser)
9682 {
9683   bool need_lang_pop;
9684   /* Look for the `template' keyword.  */
9685   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9686   /* Look for the `<'.  */
9687   cp_parser_require (parser, CPP_LESS, "`<'");
9688   /* Look for the `>'.  */
9689   cp_parser_require (parser, CPP_GREATER, "`>'");
9690   /* We have processed another parameter list.  */
9691   ++parser->num_template_parameter_lists;
9692   /* [temp]
9693
9694      A template ... explicit specialization ... shall not have C
9695      linkage.  */
9696   if (current_lang_name == lang_name_c)
9697     {
9698       error ("template specialization with C linkage");
9699       /* Give it C++ linkage to avoid confusing other parts of the
9700          front end.  */
9701       push_lang_context (lang_name_cplusplus);
9702       need_lang_pop = true;
9703     }
9704   else
9705     need_lang_pop = false;
9706   /* Let the front end know that we are beginning a specialization.  */
9707   if (!begin_specialization ())
9708     {
9709       end_specialization ();
9710       cp_parser_skip_to_end_of_block_or_statement (parser);
9711       return;
9712     }
9713
9714   /* If the next keyword is `template', we need to figure out whether
9715      or not we're looking a template-declaration.  */
9716   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9717     {
9718       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9719           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9720         cp_parser_template_declaration_after_export (parser,
9721                                                      /*member_p=*/false);
9722       else
9723         cp_parser_explicit_specialization (parser);
9724     }
9725   else
9726     /* Parse the dependent declaration.  */
9727     cp_parser_single_declaration (parser,
9728                                   /*checks=*/NULL,
9729                                   /*member_p=*/false,
9730                                   /*friend_p=*/NULL);
9731   /* We're done with the specialization.  */
9732   end_specialization ();
9733   /* For the erroneous case of a template with C linkage, we pushed an
9734      implicit C++ linkage scope; exit that scope now.  */
9735   if (need_lang_pop)
9736     pop_lang_context ();
9737   /* We're done with this parameter list.  */
9738   --parser->num_template_parameter_lists;
9739 }
9740
9741 /* Parse a type-specifier.
9742
9743    type-specifier:
9744      simple-type-specifier
9745      class-specifier
9746      enum-specifier
9747      elaborated-type-specifier
9748      cv-qualifier
9749
9750    GNU Extension:
9751
9752    type-specifier:
9753      __complex__
9754
9755    Returns a representation of the type-specifier.  For a
9756    class-specifier, enum-specifier, or elaborated-type-specifier, a
9757    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9758
9759    The parser flags FLAGS is used to control type-specifier parsing.
9760
9761    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9762    in a decl-specifier-seq.
9763
9764    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9765    class-specifier, enum-specifier, or elaborated-type-specifier, then
9766    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9767    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9768    zero.
9769
9770    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9771    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9772    is set to FALSE.  */
9773
9774 static tree
9775 cp_parser_type_specifier (cp_parser* parser,
9776                           cp_parser_flags flags,
9777                           cp_decl_specifier_seq *decl_specs,
9778                           bool is_declaration,
9779                           int* declares_class_or_enum,
9780                           bool* is_cv_qualifier)
9781 {
9782   tree type_spec = NULL_TREE;
9783   cp_token *token;
9784   enum rid keyword;
9785   cp_decl_spec ds = ds_last;
9786
9787   /* Assume this type-specifier does not declare a new type.  */
9788   if (declares_class_or_enum)
9789     *declares_class_or_enum = 0;
9790   /* And that it does not specify a cv-qualifier.  */
9791   if (is_cv_qualifier)
9792     *is_cv_qualifier = false;
9793   /* Peek at the next token.  */
9794   token = cp_lexer_peek_token (parser->lexer);
9795
9796   /* If we're looking at a keyword, we can use that to guide the
9797      production we choose.  */
9798   keyword = token->keyword;
9799   switch (keyword)
9800     {
9801     case RID_ENUM:
9802       /* Look for the enum-specifier.  */
9803       type_spec = cp_parser_enum_specifier (parser);
9804       /* If that worked, we're done.  */
9805       if (type_spec)
9806         {
9807           if (declares_class_or_enum)
9808             *declares_class_or_enum = 2;
9809           if (decl_specs)
9810             cp_parser_set_decl_spec_type (decl_specs,
9811                                           type_spec,
9812                                           /*user_defined_p=*/true);
9813           return type_spec;
9814         }
9815       else
9816         goto elaborated_type_specifier;
9817
9818       /* Any of these indicate either a class-specifier, or an
9819          elaborated-type-specifier.  */
9820     case RID_CLASS:
9821     case RID_STRUCT:
9822     case RID_UNION:
9823       /* Parse tentatively so that we can back up if we don't find a
9824          class-specifier.  */
9825       cp_parser_parse_tentatively (parser);
9826       /* Look for the class-specifier.  */
9827       type_spec = cp_parser_class_specifier (parser);
9828       /* If that worked, we're done.  */
9829       if (cp_parser_parse_definitely (parser))
9830         {
9831           if (declares_class_or_enum)
9832             *declares_class_or_enum = 2;
9833           if (decl_specs)
9834             cp_parser_set_decl_spec_type (decl_specs,
9835                                           type_spec,
9836                                           /*user_defined_p=*/true);
9837           return type_spec;
9838         }
9839
9840       /* Fall through.  */
9841     elaborated_type_specifier:
9842       /* We're declaring (not defining) a class or enum.  */
9843       if (declares_class_or_enum)
9844         *declares_class_or_enum = 1;
9845
9846       /* Fall through.  */
9847     case RID_TYPENAME:
9848       /* Look for an elaborated-type-specifier.  */
9849       type_spec
9850         = (cp_parser_elaborated_type_specifier
9851            (parser,
9852             decl_specs && decl_specs->specs[(int) ds_friend],
9853             is_declaration));
9854       if (decl_specs)
9855         cp_parser_set_decl_spec_type (decl_specs,
9856                                       type_spec,
9857                                       /*user_defined_p=*/true);
9858       return type_spec;
9859
9860     case RID_CONST:
9861       ds = ds_const;
9862       if (is_cv_qualifier)
9863         *is_cv_qualifier = true;
9864       break;
9865
9866     case RID_VOLATILE:
9867       ds = ds_volatile;
9868       if (is_cv_qualifier)
9869         *is_cv_qualifier = true;
9870       break;
9871
9872     case RID_RESTRICT:
9873       ds = ds_restrict;
9874       if (is_cv_qualifier)
9875         *is_cv_qualifier = true;
9876       break;
9877
9878     case RID_COMPLEX:
9879       /* The `__complex__' keyword is a GNU extension.  */
9880       ds = ds_complex;
9881       break;
9882
9883     default:
9884       break;
9885     }
9886
9887   /* Handle simple keywords.  */
9888   if (ds != ds_last)
9889     {
9890       if (decl_specs)
9891         {
9892           ++decl_specs->specs[(int)ds];
9893           decl_specs->any_specifiers_p = true;
9894         }
9895       return cp_lexer_consume_token (parser->lexer)->u.value;
9896     }
9897
9898   /* If we do not already have a type-specifier, assume we are looking
9899      at a simple-type-specifier.  */
9900   type_spec = cp_parser_simple_type_specifier (parser,
9901                                                decl_specs,
9902                                                flags);
9903
9904   /* If we didn't find a type-specifier, and a type-specifier was not
9905      optional in this context, issue an error message.  */
9906   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9907     {
9908       cp_parser_error (parser, "expected type specifier");
9909       return error_mark_node;
9910     }
9911
9912   return type_spec;
9913 }
9914
9915 /* Parse a simple-type-specifier.
9916
9917    simple-type-specifier:
9918      :: [opt] nested-name-specifier [opt] type-name
9919      :: [opt] nested-name-specifier template template-id
9920      char
9921      wchar_t
9922      bool
9923      short
9924      int
9925      long
9926      signed
9927      unsigned
9928      float
9929      double
9930      void
9931
9932    GNU Extension:
9933
9934    simple-type-specifier:
9935      __typeof__ unary-expression
9936      __typeof__ ( type-id )
9937
9938    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9939    appropriately updated.  */
9940
9941 static tree
9942 cp_parser_simple_type_specifier (cp_parser* parser,
9943                                  cp_decl_specifier_seq *decl_specs,
9944                                  cp_parser_flags flags)
9945 {
9946   tree type = NULL_TREE;
9947   cp_token *token;
9948
9949   /* Peek at the next token.  */
9950   token = cp_lexer_peek_token (parser->lexer);
9951
9952   /* If we're looking at a keyword, things are easy.  */
9953   switch (token->keyword)
9954     {
9955     case RID_CHAR:
9956       if (decl_specs)
9957         decl_specs->explicit_char_p = true;
9958       type = char_type_node;
9959       break;
9960     case RID_WCHAR:
9961       type = wchar_type_node;
9962       break;
9963     case RID_BOOL:
9964       type = boolean_type_node;
9965       break;
9966     case RID_SHORT:
9967       if (decl_specs)
9968         ++decl_specs->specs[(int) ds_short];
9969       type = short_integer_type_node;
9970       break;
9971     case RID_INT:
9972       if (decl_specs)
9973         decl_specs->explicit_int_p = true;
9974       type = integer_type_node;
9975       break;
9976     case RID_LONG:
9977       if (decl_specs)
9978         ++decl_specs->specs[(int) ds_long];
9979       type = long_integer_type_node;
9980       break;
9981     case RID_SIGNED:
9982       if (decl_specs)
9983         ++decl_specs->specs[(int) ds_signed];
9984       type = integer_type_node;
9985       break;
9986     case RID_UNSIGNED:
9987       if (decl_specs)
9988         ++decl_specs->specs[(int) ds_unsigned];
9989       type = unsigned_type_node;
9990       break;
9991     case RID_FLOAT:
9992       type = float_type_node;
9993       break;
9994     case RID_DOUBLE:
9995       type = double_type_node;
9996       break;
9997     case RID_VOID:
9998       type = void_type_node;
9999       break;
10000
10001     case RID_TYPEOF:
10002       /* Consume the `typeof' token.  */
10003       cp_lexer_consume_token (parser->lexer);
10004       /* Parse the operand to `typeof'.  */
10005       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10006       /* If it is not already a TYPE, take its type.  */
10007       if (!TYPE_P (type))
10008         type = finish_typeof (type);
10009
10010       if (decl_specs)
10011         cp_parser_set_decl_spec_type (decl_specs, type,
10012                                       /*user_defined_p=*/true);
10013
10014       return type;
10015
10016     default:
10017       break;
10018     }
10019
10020   /* If the type-specifier was for a built-in type, we're done.  */
10021   if (type)
10022     {
10023       tree id;
10024
10025       /* Record the type.  */
10026       if (decl_specs
10027           && (token->keyword != RID_SIGNED
10028               && token->keyword != RID_UNSIGNED
10029               && token->keyword != RID_SHORT
10030               && token->keyword != RID_LONG))
10031         cp_parser_set_decl_spec_type (decl_specs,
10032                                       type,
10033                                       /*user_defined=*/false);
10034       if (decl_specs)
10035         decl_specs->any_specifiers_p = true;
10036
10037       /* Consume the token.  */
10038       id = cp_lexer_consume_token (parser->lexer)->u.value;
10039
10040       /* There is no valid C++ program where a non-template type is
10041          followed by a "<".  That usually indicates that the user thought
10042          that the type was a template.  */
10043       cp_parser_check_for_invalid_template_id (parser, type);
10044
10045       return TYPE_NAME (type);
10046     }
10047
10048   /* The type-specifier must be a user-defined type.  */
10049   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10050     {
10051       bool qualified_p;
10052       bool global_p;
10053
10054       /* Don't gobble tokens or issue error messages if this is an
10055          optional type-specifier.  */
10056       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10057         cp_parser_parse_tentatively (parser);
10058
10059       /* Look for the optional `::' operator.  */
10060       global_p
10061         = (cp_parser_global_scope_opt (parser,
10062                                        /*current_scope_valid_p=*/false)
10063            != NULL_TREE);
10064       /* Look for the nested-name specifier.  */
10065       qualified_p
10066         = (cp_parser_nested_name_specifier_opt (parser,
10067                                                 /*typename_keyword_p=*/false,
10068                                                 /*check_dependency_p=*/true,
10069                                                 /*type_p=*/false,
10070                                                 /*is_declaration=*/false)
10071            != NULL_TREE);
10072       /* If we have seen a nested-name-specifier, and the next token
10073          is `template', then we are using the template-id production.  */
10074       if (parser->scope
10075           && cp_parser_optional_template_keyword (parser))
10076         {
10077           /* Look for the template-id.  */
10078           type = cp_parser_template_id (parser,
10079                                         /*template_keyword_p=*/true,
10080                                         /*check_dependency_p=*/true,
10081                                         /*is_declaration=*/false);
10082           /* If the template-id did not name a type, we are out of
10083              luck.  */
10084           if (TREE_CODE (type) != TYPE_DECL)
10085             {
10086               cp_parser_error (parser, "expected template-id for type");
10087               type = NULL_TREE;
10088             }
10089         }
10090       /* Otherwise, look for a type-name.  */
10091       else
10092         type = cp_parser_type_name (parser);
10093       /* Keep track of all name-lookups performed in class scopes.  */
10094       if (type
10095           && !global_p
10096           && !qualified_p
10097           && TREE_CODE (type) == TYPE_DECL
10098           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10099         maybe_note_name_used_in_class (DECL_NAME (type), type);
10100       /* If it didn't work out, we don't have a TYPE.  */
10101       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10102           && !cp_parser_parse_definitely (parser))
10103         type = NULL_TREE;
10104       if (type && decl_specs)
10105         cp_parser_set_decl_spec_type (decl_specs, type,
10106                                       /*user_defined=*/true);
10107     }
10108
10109   /* If we didn't get a type-name, issue an error message.  */
10110   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10111     {
10112       cp_parser_error (parser, "expected type-name");
10113       return error_mark_node;
10114     }
10115
10116   /* There is no valid C++ program where a non-template type is
10117      followed by a "<".  That usually indicates that the user thought
10118      that the type was a template.  */
10119   if (type && type != error_mark_node)
10120     {
10121       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10122          If it is, then the '<'...'>' enclose protocol names rather than
10123          template arguments, and so everything is fine.  */
10124       if (c_dialect_objc ()
10125           && (objc_is_id (type) || objc_is_class_name (type)))
10126         {
10127           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10128           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10129
10130           /* Clobber the "unqualified" type previously entered into
10131              DECL_SPECS with the new, improved protocol-qualified version.  */
10132           if (decl_specs)
10133             decl_specs->type = qual_type;
10134
10135           return qual_type;
10136         }
10137
10138       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10139     }
10140
10141   return type;
10142 }
10143
10144 /* Parse a type-name.
10145
10146    type-name:
10147      class-name
10148      enum-name
10149      typedef-name
10150
10151    enum-name:
10152      identifier
10153
10154    typedef-name:
10155      identifier
10156
10157    Returns a TYPE_DECL for the type.  */
10158
10159 static tree
10160 cp_parser_type_name (cp_parser* parser)
10161 {
10162   tree type_decl;
10163   tree identifier;
10164
10165   /* We can't know yet whether it is a class-name or not.  */
10166   cp_parser_parse_tentatively (parser);
10167   /* Try a class-name.  */
10168   type_decl = cp_parser_class_name (parser,
10169                                     /*typename_keyword_p=*/false,
10170                                     /*template_keyword_p=*/false,
10171                                     none_type,
10172                                     /*check_dependency_p=*/true,
10173                                     /*class_head_p=*/false,
10174                                     /*is_declaration=*/false);
10175   /* If it's not a class-name, keep looking.  */
10176   if (!cp_parser_parse_definitely (parser))
10177     {
10178       /* It must be a typedef-name or an enum-name.  */
10179       identifier = cp_parser_identifier (parser);
10180       if (identifier == error_mark_node)
10181         return error_mark_node;
10182
10183       /* Look up the type-name.  */
10184       type_decl = cp_parser_lookup_name_simple (parser, identifier);
10185
10186       if (TREE_CODE (type_decl) != TYPE_DECL
10187           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10188         {
10189           /* See if this is an Objective-C type.  */
10190           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10191           tree type = objc_get_protocol_qualified_type (identifier, protos);
10192           if (type)
10193             type_decl = TYPE_NAME (type);
10194         }
10195
10196       /* Issue an error if we did not find a type-name.  */
10197       if (TREE_CODE (type_decl) != TYPE_DECL)
10198         {
10199           if (!cp_parser_simulate_error (parser))
10200             cp_parser_name_lookup_error (parser, identifier, type_decl,
10201                                          "is not a type");
10202           type_decl = error_mark_node;
10203         }
10204       /* Remember that the name was used in the definition of the
10205          current class so that we can check later to see if the
10206          meaning would have been different after the class was
10207          entirely defined.  */
10208       else if (type_decl != error_mark_node
10209                && !parser->scope)
10210         maybe_note_name_used_in_class (identifier, type_decl);
10211     }
10212
10213   return type_decl;
10214 }
10215
10216
10217 /* Parse an elaborated-type-specifier.  Note that the grammar given
10218    here incorporates the resolution to DR68.
10219
10220    elaborated-type-specifier:
10221      class-key :: [opt] nested-name-specifier [opt] identifier
10222      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10223      enum :: [opt] nested-name-specifier [opt] identifier
10224      typename :: [opt] nested-name-specifier identifier
10225      typename :: [opt] nested-name-specifier template [opt]
10226        template-id
10227
10228    GNU extension:
10229
10230    elaborated-type-specifier:
10231      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10232      class-key attributes :: [opt] nested-name-specifier [opt]
10233                template [opt] template-id
10234      enum attributes :: [opt] nested-name-specifier [opt] identifier
10235
10236    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10237    declared `friend'.  If IS_DECLARATION is TRUE, then this
10238    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10239    something is being declared.
10240
10241    Returns the TYPE specified.  */
10242
10243 static tree
10244 cp_parser_elaborated_type_specifier (cp_parser* parser,
10245                                      bool is_friend,
10246                                      bool is_declaration)
10247 {
10248   enum tag_types tag_type;
10249   tree identifier;
10250   tree type = NULL_TREE;
10251   tree attributes = NULL_TREE;
10252
10253   /* See if we're looking at the `enum' keyword.  */
10254   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10255     {
10256       /* Consume the `enum' token.  */
10257       cp_lexer_consume_token (parser->lexer);
10258       /* Remember that it's an enumeration type.  */
10259       tag_type = enum_type;
10260       /* Parse the attributes.  */
10261       attributes = cp_parser_attributes_opt (parser);
10262     }
10263   /* Or, it might be `typename'.  */
10264   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10265                                            RID_TYPENAME))
10266     {
10267       /* Consume the `typename' token.  */
10268       cp_lexer_consume_token (parser->lexer);
10269       /* Remember that it's a `typename' type.  */
10270       tag_type = typename_type;
10271       /* The `typename' keyword is only allowed in templates.  */
10272       if (!processing_template_decl)
10273         pedwarn ("using %<typename%> outside of template");
10274     }
10275   /* Otherwise it must be a class-key.  */
10276   else
10277     {
10278       tag_type = cp_parser_class_key (parser);
10279       if (tag_type == none_type)
10280         return error_mark_node;
10281       /* Parse the attributes.  */
10282       attributes = cp_parser_attributes_opt (parser);
10283     }
10284
10285   /* Look for the `::' operator.  */
10286   cp_parser_global_scope_opt (parser,
10287                               /*current_scope_valid_p=*/false);
10288   /* Look for the nested-name-specifier.  */
10289   if (tag_type == typename_type)
10290     {
10291       if (!cp_parser_nested_name_specifier (parser,
10292                                            /*typename_keyword_p=*/true,
10293                                            /*check_dependency_p=*/true,
10294                                            /*type_p=*/true,
10295                                             is_declaration))
10296         return error_mark_node;
10297     }
10298   else
10299     /* Even though `typename' is not present, the proposed resolution
10300        to Core Issue 180 says that in `class A<T>::B', `B' should be
10301        considered a type-name, even if `A<T>' is dependent.  */
10302     cp_parser_nested_name_specifier_opt (parser,
10303                                          /*typename_keyword_p=*/true,
10304                                          /*check_dependency_p=*/true,
10305                                          /*type_p=*/true,
10306                                          is_declaration);
10307   /* For everything but enumeration types, consider a template-id.  */
10308   /* For an enumeration type, consider only a plain identifier.  */
10309   if (tag_type != enum_type)
10310     {
10311       bool template_p = false;
10312       tree decl;
10313
10314       /* Allow the `template' keyword.  */
10315       template_p = cp_parser_optional_template_keyword (parser);
10316       /* If we didn't see `template', we don't know if there's a
10317          template-id or not.  */
10318       if (!template_p)
10319         cp_parser_parse_tentatively (parser);
10320       /* Parse the template-id.  */
10321       decl = cp_parser_template_id (parser, template_p,
10322                                     /*check_dependency_p=*/true,
10323                                     is_declaration);
10324       /* If we didn't find a template-id, look for an ordinary
10325          identifier.  */
10326       if (!template_p && !cp_parser_parse_definitely (parser))
10327         ;
10328       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10329          in effect, then we must assume that, upon instantiation, the
10330          template will correspond to a class.  */
10331       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10332                && tag_type == typename_type)
10333         type = make_typename_type (parser->scope, decl,
10334                                    typename_type,
10335                                    /*complain=*/tf_error);
10336       else
10337         type = TREE_TYPE (decl);
10338     }
10339
10340   if (!type)
10341     {
10342       identifier = cp_parser_identifier (parser);
10343
10344       if (identifier == error_mark_node)
10345         {
10346           parser->scope = NULL_TREE;
10347           return error_mark_node;
10348         }
10349
10350       /* For a `typename', we needn't call xref_tag.  */
10351       if (tag_type == typename_type
10352           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10353         return cp_parser_make_typename_type (parser, parser->scope,
10354                                              identifier);
10355       /* Look up a qualified name in the usual way.  */
10356       if (parser->scope)
10357         {
10358           tree decl;
10359
10360           decl = cp_parser_lookup_name (parser, identifier,
10361                                         tag_type,
10362                                         /*is_template=*/false,
10363                                         /*is_namespace=*/false,
10364                                         /*check_dependency=*/true,
10365                                         /*ambiguous_decls=*/NULL);
10366
10367           /* If we are parsing friend declaration, DECL may be a
10368              TEMPLATE_DECL tree node here.  However, we need to check
10369              whether this TEMPLATE_DECL results in valid code.  Consider
10370              the following example:
10371
10372                namespace N {
10373                  template <class T> class C {};
10374                }
10375                class X {
10376                  template <class T> friend class N::C; // #1, valid code
10377                };
10378                template <class T> class Y {
10379                  friend class N::C;                    // #2, invalid code
10380                };
10381
10382              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10383              name lookup of `N::C'.  We see that friend declaration must
10384              be template for the code to be valid.  Note that
10385              processing_template_decl does not work here since it is
10386              always 1 for the above two cases.  */
10387
10388           decl = (cp_parser_maybe_treat_template_as_class
10389                   (decl, /*tag_name_p=*/is_friend
10390                          && parser->num_template_parameter_lists));
10391
10392           if (TREE_CODE (decl) != TYPE_DECL)
10393             {
10394               cp_parser_diagnose_invalid_type_name (parser,
10395                                                     parser->scope,
10396                                                     identifier);
10397               return error_mark_node;
10398             }
10399
10400           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10401             {
10402               bool allow_template = (parser->num_template_parameter_lists
10403                                       || DECL_SELF_REFERENCE_P (decl));
10404               type = check_elaborated_type_specifier (tag_type, decl, 
10405                                                       allow_template);
10406
10407               if (type == error_mark_node)
10408                 return error_mark_node;
10409             }
10410
10411           type = TREE_TYPE (decl);
10412         }
10413       else
10414         {
10415           /* An elaborated-type-specifier sometimes introduces a new type and
10416              sometimes names an existing type.  Normally, the rule is that it
10417              introduces a new type only if there is not an existing type of
10418              the same name already in scope.  For example, given:
10419
10420                struct S {};
10421                void f() { struct S s; }
10422
10423              the `struct S' in the body of `f' is the same `struct S' as in
10424              the global scope; the existing definition is used.  However, if
10425              there were no global declaration, this would introduce a new
10426              local class named `S'.
10427
10428              An exception to this rule applies to the following code:
10429
10430                namespace N { struct S; }
10431
10432              Here, the elaborated-type-specifier names a new type
10433              unconditionally; even if there is already an `S' in the
10434              containing scope this declaration names a new type.
10435              This exception only applies if the elaborated-type-specifier
10436              forms the complete declaration:
10437
10438                [class.name]
10439
10440                A declaration consisting solely of `class-key identifier ;' is
10441                either a redeclaration of the name in the current scope or a
10442                forward declaration of the identifier as a class name.  It
10443                introduces the name into the current scope.
10444
10445              We are in this situation precisely when the next token is a `;'.
10446
10447              An exception to the exception is that a `friend' declaration does
10448              *not* name a new type; i.e., given:
10449
10450                struct S { friend struct T; };
10451
10452              `T' is not a new type in the scope of `S'.
10453
10454              Also, `new struct S' or `sizeof (struct S)' never results in the
10455              definition of a new type; a new type can only be declared in a
10456              declaration context.  */
10457
10458           tag_scope ts;
10459           bool template_p;
10460
10461           if (is_friend)
10462             /* Friends have special name lookup rules.  */
10463             ts = ts_within_enclosing_non_class;
10464           else if (is_declaration
10465                    && cp_lexer_next_token_is (parser->lexer,
10466                                               CPP_SEMICOLON))
10467             /* This is a `class-key identifier ;' */
10468             ts = ts_current;
10469           else
10470             ts = ts_global;
10471
10472           template_p =
10473             (parser->num_template_parameter_lists
10474              && (cp_parser_next_token_starts_class_definition_p (parser)
10475                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10476           /* An unqualified name was used to reference this type, so
10477              there were no qualifying templates.  */
10478           if (!cp_parser_check_template_parameters (parser,
10479                                                     /*num_templates=*/0))
10480             return error_mark_node;
10481           type = xref_tag (tag_type, identifier, ts, template_p);
10482         }
10483     }
10484
10485   if (type == error_mark_node)
10486     return error_mark_node;
10487
10488   /* Allow attributes on forward declarations of classes.  */
10489   if (attributes)
10490     {
10491       if (TREE_CODE (type) == TYPENAME_TYPE)
10492         warning (OPT_Wattributes,
10493                  "attributes ignored on uninstantiated type");
10494       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10495                && ! processing_explicit_instantiation)
10496         warning (OPT_Wattributes,
10497                  "attributes ignored on template instantiation");
10498       else if (is_declaration && cp_parser_declares_only_class_p (parser))
10499         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10500       else
10501         warning (OPT_Wattributes,
10502                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10503     }
10504
10505   if (tag_type != enum_type)
10506     cp_parser_check_class_key (tag_type, type);
10507
10508   /* A "<" cannot follow an elaborated type specifier.  If that
10509      happens, the user was probably trying to form a template-id.  */
10510   cp_parser_check_for_invalid_template_id (parser, type);
10511
10512   return type;
10513 }
10514
10515 /* Parse an enum-specifier.
10516
10517    enum-specifier:
10518      enum identifier [opt] { enumerator-list [opt] }
10519
10520    GNU Extensions:
10521      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10522        attributes[opt]
10523
10524    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10525    if the token stream isn't an enum-specifier after all.  */
10526
10527 static tree
10528 cp_parser_enum_specifier (cp_parser* parser)
10529 {
10530   tree identifier;
10531   tree type;
10532   tree attributes;
10533
10534   /* Parse tentatively so that we can back up if we don't find a
10535      enum-specifier.  */
10536   cp_parser_parse_tentatively (parser);
10537
10538   /* Caller guarantees that the current token is 'enum', an identifier
10539      possibly follows, and the token after that is an opening brace.
10540      If we don't have an identifier, fabricate an anonymous name for
10541      the enumeration being defined.  */
10542   cp_lexer_consume_token (parser->lexer);
10543
10544   attributes = cp_parser_attributes_opt (parser);
10545
10546   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10547     identifier = cp_parser_identifier (parser);
10548   else
10549     identifier = make_anon_name ();
10550
10551   /* Look for the `{' but don't consume it yet.  */
10552   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10553     cp_parser_simulate_error (parser);
10554
10555   if (!cp_parser_parse_definitely (parser))
10556     return NULL_TREE;
10557
10558   /* Issue an error message if type-definitions are forbidden here.  */
10559   if (!cp_parser_check_type_definition (parser))
10560     type = error_mark_node;
10561   else
10562     /* Create the new type.  We do this before consuming the opening
10563        brace so the enum will be recorded as being on the line of its
10564        tag (or the 'enum' keyword, if there is no tag).  */
10565     type = start_enum (identifier);
10566   
10567   /* Consume the opening brace.  */
10568   cp_lexer_consume_token (parser->lexer);
10569
10570   if (type == error_mark_node)
10571     {
10572       cp_parser_skip_to_end_of_block_or_statement (parser);
10573       return error_mark_node;
10574     }
10575
10576   /* If the next token is not '}', then there are some enumerators.  */
10577   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10578     cp_parser_enumerator_list (parser, type);
10579
10580   /* Consume the final '}'.  */
10581   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10582
10583   /* Look for trailing attributes to apply to this enumeration, and
10584      apply them if appropriate.  */
10585   if (cp_parser_allow_gnu_extensions_p (parser))
10586     {
10587       tree trailing_attr = cp_parser_attributes_opt (parser);
10588       cplus_decl_attributes (&type,
10589                              trailing_attr,
10590                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10591     }
10592
10593   /* Finish up the enumeration.  */
10594   finish_enum (type);
10595
10596   return type;
10597 }
10598
10599 /* Parse an enumerator-list.  The enumerators all have the indicated
10600    TYPE.
10601
10602    enumerator-list:
10603      enumerator-definition
10604      enumerator-list , enumerator-definition  */
10605
10606 static void
10607 cp_parser_enumerator_list (cp_parser* parser, tree type)
10608 {
10609   while (true)
10610     {
10611       /* Parse an enumerator-definition.  */
10612       cp_parser_enumerator_definition (parser, type);
10613
10614       /* If the next token is not a ',', we've reached the end of
10615          the list.  */
10616       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10617         break;
10618       /* Otherwise, consume the `,' and keep going.  */
10619       cp_lexer_consume_token (parser->lexer);
10620       /* If the next token is a `}', there is a trailing comma.  */
10621       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10622         {
10623           if (pedantic && !in_system_header)
10624             pedwarn ("comma at end of enumerator list");
10625           break;
10626         }
10627     }
10628 }
10629
10630 /* Parse an enumerator-definition.  The enumerator has the indicated
10631    TYPE.
10632
10633    enumerator-definition:
10634      enumerator
10635      enumerator = constant-expression
10636
10637    enumerator:
10638      identifier  */
10639
10640 static void
10641 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10642 {
10643   tree identifier;
10644   tree value;
10645
10646   /* Look for the identifier.  */
10647   identifier = cp_parser_identifier (parser);
10648   if (identifier == error_mark_node)
10649     return;
10650
10651   /* If the next token is an '=', then there is an explicit value.  */
10652   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10653     {
10654       /* Consume the `=' token.  */
10655       cp_lexer_consume_token (parser->lexer);
10656       /* Parse the value.  */
10657       value = cp_parser_constant_expression (parser,
10658                                              /*allow_non_constant_p=*/false,
10659                                              NULL);
10660     }
10661   else
10662     value = NULL_TREE;
10663
10664   /* Create the enumerator.  */
10665   build_enumerator (identifier, value, type);
10666 }
10667
10668 /* Parse a namespace-name.
10669
10670    namespace-name:
10671      original-namespace-name
10672      namespace-alias
10673
10674    Returns the NAMESPACE_DECL for the namespace.  */
10675
10676 static tree
10677 cp_parser_namespace_name (cp_parser* parser)
10678 {
10679   tree identifier;
10680   tree namespace_decl;
10681
10682   /* Get the name of the namespace.  */
10683   identifier = cp_parser_identifier (parser);
10684   if (identifier == error_mark_node)
10685     return error_mark_node;
10686
10687   /* Look up the identifier in the currently active scope.  Look only
10688      for namespaces, due to:
10689
10690        [basic.lookup.udir]
10691
10692        When looking up a namespace-name in a using-directive or alias
10693        definition, only namespace names are considered.
10694
10695      And:
10696
10697        [basic.lookup.qual]
10698
10699        During the lookup of a name preceding the :: scope resolution
10700        operator, object, function, and enumerator names are ignored.
10701
10702      (Note that cp_parser_class_or_namespace_name only calls this
10703      function if the token after the name is the scope resolution
10704      operator.)  */
10705   namespace_decl = cp_parser_lookup_name (parser, identifier,
10706                                           none_type,
10707                                           /*is_template=*/false,
10708                                           /*is_namespace=*/true,
10709                                           /*check_dependency=*/true,
10710                                           /*ambiguous_decls=*/NULL);
10711   /* If it's not a namespace, issue an error.  */
10712   if (namespace_decl == error_mark_node
10713       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10714     {
10715       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10716         error ("%qD is not a namespace-name", identifier);
10717       cp_parser_error (parser, "expected namespace-name");
10718       namespace_decl = error_mark_node;
10719     }
10720
10721   return namespace_decl;
10722 }
10723
10724 /* Parse a namespace-definition.
10725
10726    namespace-definition:
10727      named-namespace-definition
10728      unnamed-namespace-definition
10729
10730    named-namespace-definition:
10731      original-namespace-definition
10732      extension-namespace-definition
10733
10734    original-namespace-definition:
10735      namespace identifier { namespace-body }
10736
10737    extension-namespace-definition:
10738      namespace original-namespace-name { namespace-body }
10739
10740    unnamed-namespace-definition:
10741      namespace { namespace-body } */
10742
10743 static void
10744 cp_parser_namespace_definition (cp_parser* parser)
10745 {
10746   tree identifier, attribs;
10747
10748   /* Look for the `namespace' keyword.  */
10749   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10750
10751   /* Get the name of the namespace.  We do not attempt to distinguish
10752      between an original-namespace-definition and an
10753      extension-namespace-definition at this point.  The semantic
10754      analysis routines are responsible for that.  */
10755   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10756     identifier = cp_parser_identifier (parser);
10757   else
10758     identifier = NULL_TREE;
10759
10760   /* Parse any specified attributes.  */
10761   attribs = cp_parser_attributes_opt (parser);
10762
10763   /* Look for the `{' to start the namespace.  */
10764   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10765   /* Start the namespace.  */
10766   push_namespace_with_attribs (identifier, attribs);
10767   /* Parse the body of the namespace.  */
10768   cp_parser_namespace_body (parser);
10769   /* Finish the namespace.  */
10770   pop_namespace ();
10771   /* Look for the final `}'.  */
10772   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10773 }
10774
10775 /* Parse a namespace-body.
10776
10777    namespace-body:
10778      declaration-seq [opt]  */
10779
10780 static void
10781 cp_parser_namespace_body (cp_parser* parser)
10782 {
10783   cp_parser_declaration_seq_opt (parser);
10784 }
10785
10786 /* Parse a namespace-alias-definition.
10787
10788    namespace-alias-definition:
10789      namespace identifier = qualified-namespace-specifier ;  */
10790
10791 static void
10792 cp_parser_namespace_alias_definition (cp_parser* parser)
10793 {
10794   tree identifier;
10795   tree namespace_specifier;
10796
10797   /* Look for the `namespace' keyword.  */
10798   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10799   /* Look for the identifier.  */
10800   identifier = cp_parser_identifier (parser);
10801   if (identifier == error_mark_node)
10802     return;
10803   /* Look for the `=' token.  */
10804   cp_parser_require (parser, CPP_EQ, "`='");
10805   /* Look for the qualified-namespace-specifier.  */
10806   namespace_specifier
10807     = cp_parser_qualified_namespace_specifier (parser);
10808   /* Look for the `;' token.  */
10809   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10810
10811   /* Register the alias in the symbol table.  */
10812   do_namespace_alias (identifier, namespace_specifier);
10813 }
10814
10815 /* Parse a qualified-namespace-specifier.
10816
10817    qualified-namespace-specifier:
10818      :: [opt] nested-name-specifier [opt] namespace-name
10819
10820    Returns a NAMESPACE_DECL corresponding to the specified
10821    namespace.  */
10822
10823 static tree
10824 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10825 {
10826   /* Look for the optional `::'.  */
10827   cp_parser_global_scope_opt (parser,
10828                               /*current_scope_valid_p=*/false);
10829
10830   /* Look for the optional nested-name-specifier.  */
10831   cp_parser_nested_name_specifier_opt (parser,
10832                                        /*typename_keyword_p=*/false,
10833                                        /*check_dependency_p=*/true,
10834                                        /*type_p=*/false,
10835                                        /*is_declaration=*/true);
10836
10837   return cp_parser_namespace_name (parser);
10838 }
10839
10840 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10841    access declaration.
10842
10843    using-declaration:
10844      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10845      using :: unqualified-id ;  
10846
10847    access-declaration:
10848      qualified-id ;  
10849
10850    */
10851
10852 static bool
10853 cp_parser_using_declaration (cp_parser* parser, 
10854                              bool access_declaration_p)
10855 {
10856   cp_token *token;
10857   bool typename_p = false;
10858   bool global_scope_p;
10859   tree decl;
10860   tree identifier;
10861   tree qscope;
10862
10863   if (access_declaration_p)
10864     cp_parser_parse_tentatively (parser);
10865   else
10866     {
10867       /* Look for the `using' keyword.  */
10868       cp_parser_require_keyword (parser, RID_USING, "`using'");
10869       
10870       /* Peek at the next token.  */
10871       token = cp_lexer_peek_token (parser->lexer);
10872       /* See if it's `typename'.  */
10873       if (token->keyword == RID_TYPENAME)
10874         {
10875           /* Remember that we've seen it.  */
10876           typename_p = true;
10877           /* Consume the `typename' token.  */
10878           cp_lexer_consume_token (parser->lexer);
10879         }
10880     }
10881
10882   /* Look for the optional global scope qualification.  */
10883   global_scope_p
10884     = (cp_parser_global_scope_opt (parser,
10885                                    /*current_scope_valid_p=*/false)
10886        != NULL_TREE);
10887
10888   /* If we saw `typename', or didn't see `::', then there must be a
10889      nested-name-specifier present.  */
10890   if (typename_p || !global_scope_p)
10891     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10892                                               /*check_dependency_p=*/true,
10893                                               /*type_p=*/false,
10894                                               /*is_declaration=*/true);
10895   /* Otherwise, we could be in either of the two productions.  In that
10896      case, treat the nested-name-specifier as optional.  */
10897   else
10898     qscope = cp_parser_nested_name_specifier_opt (parser,
10899                                                   /*typename_keyword_p=*/false,
10900                                                   /*check_dependency_p=*/true,
10901                                                   /*type_p=*/false,
10902                                                   /*is_declaration=*/true);
10903   if (!qscope)
10904     qscope = global_namespace;
10905
10906   if (access_declaration_p && cp_parser_error_occurred (parser))
10907     /* Something has already gone wrong; there's no need to parse
10908        further.  Since an error has occurred, the return value of
10909        cp_parser_parse_definitely will be false, as required.  */
10910     return cp_parser_parse_definitely (parser);
10911
10912   /* Parse the unqualified-id.  */
10913   identifier = cp_parser_unqualified_id (parser,
10914                                          /*template_keyword_p=*/false,
10915                                          /*check_dependency_p=*/true,
10916                                          /*declarator_p=*/true,
10917                                          /*optional_p=*/false);
10918
10919   if (access_declaration_p)
10920     {
10921       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10922         cp_parser_simulate_error (parser);
10923       if (!cp_parser_parse_definitely (parser))
10924         return false;
10925     }
10926
10927   /* The function we call to handle a using-declaration is different
10928      depending on what scope we are in.  */
10929   if (qscope == error_mark_node || identifier == error_mark_node)
10930     ;
10931   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10932            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10933     /* [namespace.udecl]
10934
10935        A using declaration shall not name a template-id.  */
10936     error ("a template-id may not appear in a using-declaration");
10937   else
10938     {
10939       if (at_class_scope_p ())
10940         {
10941           /* Create the USING_DECL.  */
10942           decl = do_class_using_decl (parser->scope, identifier);
10943           /* Add it to the list of members in this class.  */
10944           finish_member_declaration (decl);
10945         }
10946       else
10947         {
10948           decl = cp_parser_lookup_name_simple (parser, identifier);
10949           if (decl == error_mark_node)
10950             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10951           else if (!at_namespace_scope_p ())
10952             do_local_using_decl (decl, qscope, identifier);
10953           else
10954             do_toplevel_using_decl (decl, qscope, identifier);
10955         }
10956     }
10957
10958   /* Look for the final `;'.  */
10959   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10960   
10961   return true;
10962 }
10963
10964 /* Parse a using-directive.
10965
10966    using-directive:
10967      using namespace :: [opt] nested-name-specifier [opt]
10968        namespace-name ;  */
10969
10970 static void
10971 cp_parser_using_directive (cp_parser* parser)
10972 {
10973   tree namespace_decl;
10974   tree attribs;
10975
10976   /* Look for the `using' keyword.  */
10977   cp_parser_require_keyword (parser, RID_USING, "`using'");
10978   /* And the `namespace' keyword.  */
10979   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10980   /* Look for the optional `::' operator.  */
10981   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10982   /* And the optional nested-name-specifier.  */
10983   cp_parser_nested_name_specifier_opt (parser,
10984                                        /*typename_keyword_p=*/false,
10985                                        /*check_dependency_p=*/true,
10986                                        /*type_p=*/false,
10987                                        /*is_declaration=*/true);
10988   /* Get the namespace being used.  */
10989   namespace_decl = cp_parser_namespace_name (parser);
10990   /* And any specified attributes.  */
10991   attribs = cp_parser_attributes_opt (parser);
10992   /* Update the symbol table.  */
10993   parse_using_directive (namespace_decl, attribs);
10994   /* Look for the final `;'.  */
10995   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10996 }
10997
10998 /* Parse an asm-definition.
10999
11000    asm-definition:
11001      asm ( string-literal ) ;
11002
11003    GNU Extension:
11004
11005    asm-definition:
11006      asm volatile [opt] ( string-literal ) ;
11007      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11008      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11009                           : asm-operand-list [opt] ) ;
11010      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11011                           : asm-operand-list [opt]
11012                           : asm-operand-list [opt] ) ;  */
11013
11014 static void
11015 cp_parser_asm_definition (cp_parser* parser)
11016 {
11017   tree string;
11018   tree outputs = NULL_TREE;
11019   tree inputs = NULL_TREE;
11020   tree clobbers = NULL_TREE;
11021   tree asm_stmt;
11022   bool volatile_p = false;
11023   bool extended_p = false;
11024
11025   /* Look for the `asm' keyword.  */
11026   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11027   /* See if the next token is `volatile'.  */
11028   if (cp_parser_allow_gnu_extensions_p (parser)
11029       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11030     {
11031       /* Remember that we saw the `volatile' keyword.  */
11032       volatile_p = true;
11033       /* Consume the token.  */
11034       cp_lexer_consume_token (parser->lexer);
11035     }
11036   /* Look for the opening `('.  */
11037   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11038     return;
11039   /* Look for the string.  */
11040   string = cp_parser_string_literal (parser, false, false);
11041   if (string == error_mark_node)
11042     {
11043       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11044                                              /*consume_paren=*/true);
11045       return;
11046     }
11047
11048   /* If we're allowing GNU extensions, check for the extended assembly
11049      syntax.  Unfortunately, the `:' tokens need not be separated by
11050      a space in C, and so, for compatibility, we tolerate that here
11051      too.  Doing that means that we have to treat the `::' operator as
11052      two `:' tokens.  */
11053   if (cp_parser_allow_gnu_extensions_p (parser)
11054       && parser->in_function_body
11055       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11056           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11057     {
11058       bool inputs_p = false;
11059       bool clobbers_p = false;
11060
11061       /* The extended syntax was used.  */
11062       extended_p = true;
11063
11064       /* Look for outputs.  */
11065       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11066         {
11067           /* Consume the `:'.  */
11068           cp_lexer_consume_token (parser->lexer);
11069           /* Parse the output-operands.  */
11070           if (cp_lexer_next_token_is_not (parser->lexer,
11071                                           CPP_COLON)
11072               && cp_lexer_next_token_is_not (parser->lexer,
11073                                              CPP_SCOPE)
11074               && cp_lexer_next_token_is_not (parser->lexer,
11075                                              CPP_CLOSE_PAREN))
11076             outputs = cp_parser_asm_operand_list (parser);
11077         }
11078       /* If the next token is `::', there are no outputs, and the
11079          next token is the beginning of the inputs.  */
11080       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11081         /* The inputs are coming next.  */
11082         inputs_p = true;
11083
11084       /* Look for inputs.  */
11085       if (inputs_p
11086           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11087         {
11088           /* Consume the `:' or `::'.  */
11089           cp_lexer_consume_token (parser->lexer);
11090           /* Parse the output-operands.  */
11091           if (cp_lexer_next_token_is_not (parser->lexer,
11092                                           CPP_COLON)
11093               && cp_lexer_next_token_is_not (parser->lexer,
11094                                              CPP_CLOSE_PAREN))
11095             inputs = cp_parser_asm_operand_list (parser);
11096         }
11097       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11098         /* The clobbers are coming next.  */
11099         clobbers_p = true;
11100
11101       /* Look for clobbers.  */
11102       if (clobbers_p
11103           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11104         {
11105           /* Consume the `:' or `::'.  */
11106           cp_lexer_consume_token (parser->lexer);
11107           /* Parse the clobbers.  */
11108           if (cp_lexer_next_token_is_not (parser->lexer,
11109                                           CPP_CLOSE_PAREN))
11110             clobbers = cp_parser_asm_clobber_list (parser);
11111         }
11112     }
11113   /* Look for the closing `)'.  */
11114   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11115     cp_parser_skip_to_closing_parenthesis (parser, true, false,
11116                                            /*consume_paren=*/true);
11117   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11118
11119   /* Create the ASM_EXPR.  */
11120   if (parser->in_function_body)
11121     {
11122       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11123                                   inputs, clobbers);
11124       /* If the extended syntax was not used, mark the ASM_EXPR.  */
11125       if (!extended_p)
11126         {
11127           tree temp = asm_stmt;
11128           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11129             temp = TREE_OPERAND (temp, 0);
11130
11131           ASM_INPUT_P (temp) = 1;
11132         }
11133     }
11134   else
11135     cgraph_add_asm_node (string);
11136 }
11137
11138 /* Declarators [gram.dcl.decl] */
11139
11140 /* Parse an init-declarator.
11141
11142    init-declarator:
11143      declarator initializer [opt]
11144
11145    GNU Extension:
11146
11147    init-declarator:
11148      declarator asm-specification [opt] attributes [opt] initializer [opt]
11149
11150    function-definition:
11151      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11152        function-body
11153      decl-specifier-seq [opt] declarator function-try-block
11154
11155    GNU Extension:
11156
11157    function-definition:
11158      __extension__ function-definition
11159
11160    The DECL_SPECIFIERS apply to this declarator.  Returns a
11161    representation of the entity declared.  If MEMBER_P is TRUE, then
11162    this declarator appears in a class scope.  The new DECL created by
11163    this declarator is returned.
11164
11165    The CHECKS are access checks that should be performed once we know
11166    what entity is being declared (and, therefore, what classes have
11167    befriended it).
11168
11169    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11170    for a function-definition here as well.  If the declarator is a
11171    declarator for a function-definition, *FUNCTION_DEFINITION_P will
11172    be TRUE upon return.  By that point, the function-definition will
11173    have been completely parsed.
11174
11175    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11176    is FALSE.  */
11177
11178 static tree
11179 cp_parser_init_declarator (cp_parser* parser,
11180                            cp_decl_specifier_seq *decl_specifiers,
11181                            VEC (deferred_access_check,gc)* checks,
11182                            bool function_definition_allowed_p,
11183                            bool member_p,
11184                            int declares_class_or_enum,
11185                            bool* function_definition_p)
11186 {
11187   cp_token *token;
11188   cp_declarator *declarator;
11189   tree prefix_attributes;
11190   tree attributes;
11191   tree asm_specification;
11192   tree initializer;
11193   tree decl = NULL_TREE;
11194   tree scope;
11195   bool is_initialized;
11196   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
11197      initialized with "= ..", CPP_OPEN_PAREN if initialized with
11198      "(...)".  */
11199   enum cpp_ttype initialization_kind;
11200   bool is_parenthesized_init = false;
11201   bool is_non_constant_init;
11202   int ctor_dtor_or_conv_p;
11203   bool friend_p;
11204   tree pushed_scope = NULL;
11205
11206   /* Gather the attributes that were provided with the
11207      decl-specifiers.  */
11208   prefix_attributes = decl_specifiers->attributes;
11209
11210   /* Assume that this is not the declarator for a function
11211      definition.  */
11212   if (function_definition_p)
11213     *function_definition_p = false;
11214
11215   /* Defer access checks while parsing the declarator; we cannot know
11216      what names are accessible until we know what is being
11217      declared.  */
11218   resume_deferring_access_checks ();
11219
11220   /* Parse the declarator.  */
11221   declarator
11222     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11223                             &ctor_dtor_or_conv_p,
11224                             /*parenthesized_p=*/NULL,
11225                             /*member_p=*/false);
11226   /* Gather up the deferred checks.  */
11227   stop_deferring_access_checks ();
11228
11229   /* If the DECLARATOR was erroneous, there's no need to go
11230      further.  */
11231   if (declarator == cp_error_declarator)
11232     return error_mark_node;
11233
11234   /* Check that the number of template-parameter-lists is OK.  */
11235   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11236     return error_mark_node;
11237
11238   if (declares_class_or_enum & 2)
11239     cp_parser_check_for_definition_in_return_type (declarator,
11240                                                    decl_specifiers->type);
11241
11242   /* Figure out what scope the entity declared by the DECLARATOR is
11243      located in.  `grokdeclarator' sometimes changes the scope, so
11244      we compute it now.  */
11245   scope = get_scope_of_declarator (declarator);
11246
11247   /* If we're allowing GNU extensions, look for an asm-specification
11248      and attributes.  */
11249   if (cp_parser_allow_gnu_extensions_p (parser))
11250     {
11251       /* Look for an asm-specification.  */
11252       asm_specification = cp_parser_asm_specification_opt (parser);
11253       /* And attributes.  */
11254       attributes = cp_parser_attributes_opt (parser);
11255     }
11256   else
11257     {
11258       asm_specification = NULL_TREE;
11259       attributes = NULL_TREE;
11260     }
11261
11262   /* Peek at the next token.  */
11263   token = cp_lexer_peek_token (parser->lexer);
11264   /* Check to see if the token indicates the start of a
11265      function-definition.  */
11266   if (cp_parser_token_starts_function_definition_p (token))
11267     {
11268       if (!function_definition_allowed_p)
11269         {
11270           /* If a function-definition should not appear here, issue an
11271              error message.  */
11272           cp_parser_error (parser,
11273                            "a function-definition is not allowed here");
11274           return error_mark_node;
11275         }
11276       else
11277         {
11278           /* Neither attributes nor an asm-specification are allowed
11279              on a function-definition.  */
11280           if (asm_specification)
11281             error ("an asm-specification is not allowed on a function-definition");
11282           if (attributes)
11283             error ("attributes are not allowed on a function-definition");
11284           /* This is a function-definition.  */
11285           *function_definition_p = true;
11286
11287           /* Parse the function definition.  */
11288           if (member_p)
11289             decl = cp_parser_save_member_function_body (parser,
11290                                                         decl_specifiers,
11291                                                         declarator,
11292                                                         prefix_attributes);
11293           else
11294             decl
11295               = (cp_parser_function_definition_from_specifiers_and_declarator
11296                  (parser, decl_specifiers, prefix_attributes, declarator));
11297
11298           return decl;
11299         }
11300     }
11301
11302   /* [dcl.dcl]
11303
11304      Only in function declarations for constructors, destructors, and
11305      type conversions can the decl-specifier-seq be omitted.
11306
11307      We explicitly postpone this check past the point where we handle
11308      function-definitions because we tolerate function-definitions
11309      that are missing their return types in some modes.  */
11310   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11311     {
11312       cp_parser_error (parser,
11313                        "expected constructor, destructor, or type conversion");
11314       return error_mark_node;
11315     }
11316
11317   /* An `=' or an `(' indicates an initializer.  */
11318   if (token->type == CPP_EQ
11319       || token->type == CPP_OPEN_PAREN)
11320     {
11321       is_initialized = true;
11322       initialization_kind = token->type;
11323     }
11324   else
11325     {
11326       /* If the init-declarator isn't initialized and isn't followed by a
11327          `,' or `;', it's not a valid init-declarator.  */
11328       if (token->type != CPP_COMMA
11329           && token->type != CPP_SEMICOLON)
11330         {
11331           cp_parser_error (parser, "expected initializer");
11332           return error_mark_node;
11333         }
11334       is_initialized = false;
11335       initialization_kind = CPP_EOF;
11336     }
11337
11338   /* Because start_decl has side-effects, we should only call it if we
11339      know we're going ahead.  By this point, we know that we cannot
11340      possibly be looking at any other construct.  */
11341   cp_parser_commit_to_tentative_parse (parser);
11342
11343   /* If the decl specifiers were bad, issue an error now that we're
11344      sure this was intended to be a declarator.  Then continue
11345      declaring the variable(s), as int, to try to cut down on further
11346      errors.  */
11347   if (decl_specifiers->any_specifiers_p
11348       && decl_specifiers->type == error_mark_node)
11349     {
11350       cp_parser_error (parser, "invalid type in declaration");
11351       decl_specifiers->type = integer_type_node;
11352     }
11353
11354   /* Check to see whether or not this declaration is a friend.  */
11355   friend_p = cp_parser_friend_p (decl_specifiers);
11356
11357   /* Enter the newly declared entry in the symbol table.  If we're
11358      processing a declaration in a class-specifier, we wait until
11359      after processing the initializer.  */
11360   if (!member_p)
11361     {
11362       if (parser->in_unbraced_linkage_specification_p)
11363         decl_specifiers->storage_class = sc_extern;
11364       decl = start_decl (declarator, decl_specifiers,
11365                          is_initialized, attributes, prefix_attributes,
11366                          &pushed_scope);
11367     }
11368   else if (scope)
11369     /* Enter the SCOPE.  That way unqualified names appearing in the
11370        initializer will be looked up in SCOPE.  */
11371     pushed_scope = push_scope (scope);
11372
11373   /* Perform deferred access control checks, now that we know in which
11374      SCOPE the declared entity resides.  */
11375   if (!member_p && decl)
11376     {
11377       tree saved_current_function_decl = NULL_TREE;
11378
11379       /* If the entity being declared is a function, pretend that we
11380          are in its scope.  If it is a `friend', it may have access to
11381          things that would not otherwise be accessible.  */
11382       if (TREE_CODE (decl) == FUNCTION_DECL)
11383         {
11384           saved_current_function_decl = current_function_decl;
11385           current_function_decl = decl;
11386         }
11387
11388       /* Perform access checks for template parameters.  */
11389       cp_parser_perform_template_parameter_access_checks (checks);
11390
11391       /* Perform the access control checks for the declarator and the
11392          the decl-specifiers.  */
11393       perform_deferred_access_checks ();
11394
11395       /* Restore the saved value.  */
11396       if (TREE_CODE (decl) == FUNCTION_DECL)
11397         current_function_decl = saved_current_function_decl;
11398     }
11399
11400   /* Parse the initializer.  */
11401   initializer = NULL_TREE;
11402   is_parenthesized_init = false;
11403   is_non_constant_init = true;
11404   if (is_initialized)
11405     {
11406       if (function_declarator_p (declarator))
11407         {
11408            if (initialization_kind == CPP_EQ)
11409              initializer = cp_parser_pure_specifier (parser);
11410            else
11411              {
11412                /* If the declaration was erroneous, we don't really
11413                   know what the user intended, so just silently
11414                   consume the initializer.  */
11415                if (decl != error_mark_node)
11416                  error ("initializer provided for function");
11417                cp_parser_skip_to_closing_parenthesis (parser,
11418                                                       /*recovering=*/true,
11419                                                       /*or_comma=*/false,
11420                                                       /*consume_paren=*/true);
11421              }
11422         }
11423       else
11424         initializer = cp_parser_initializer (parser,
11425                                              &is_parenthesized_init,
11426                                              &is_non_constant_init);
11427     }
11428
11429   /* The old parser allows attributes to appear after a parenthesized
11430      initializer.  Mark Mitchell proposed removing this functionality
11431      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11432      attributes -- but ignores them.  */
11433   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11434     if (cp_parser_attributes_opt (parser))
11435       warning (OPT_Wattributes,
11436                "attributes after parenthesized initializer ignored");
11437
11438   /* For an in-class declaration, use `grokfield' to create the
11439      declaration.  */
11440   if (member_p)
11441     {
11442       if (pushed_scope)
11443         {
11444           pop_scope (pushed_scope);
11445           pushed_scope = false;
11446         }
11447       decl = grokfield (declarator, decl_specifiers,
11448                         initializer, !is_non_constant_init,
11449                         /*asmspec=*/NULL_TREE,
11450                         prefix_attributes);
11451       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11452         cp_parser_save_default_args (parser, decl);
11453     }
11454
11455   /* Finish processing the declaration.  But, skip friend
11456      declarations.  */
11457   if (!friend_p && decl && decl != error_mark_node)
11458     {
11459       cp_finish_decl (decl,
11460                       initializer, !is_non_constant_init,
11461                       asm_specification,
11462                       /* If the initializer is in parentheses, then this is
11463                          a direct-initialization, which means that an
11464                          `explicit' constructor is OK.  Otherwise, an
11465                          `explicit' constructor cannot be used.  */
11466                       ((is_parenthesized_init || !is_initialized)
11467                      ? 0 : LOOKUP_ONLYCONVERTING));
11468     }
11469   if (!friend_p && pushed_scope)
11470     pop_scope (pushed_scope);
11471
11472   return decl;
11473 }
11474
11475 /* Parse a declarator.
11476
11477    declarator:
11478      direct-declarator
11479      ptr-operator declarator
11480
11481    abstract-declarator:
11482      ptr-operator abstract-declarator [opt]
11483      direct-abstract-declarator
11484
11485    GNU Extensions:
11486
11487    declarator:
11488      attributes [opt] direct-declarator
11489      attributes [opt] ptr-operator declarator
11490
11491    abstract-declarator:
11492      attributes [opt] ptr-operator abstract-declarator [opt]
11493      attributes [opt] direct-abstract-declarator
11494
11495    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11496    detect constructor, destructor or conversion operators. It is set
11497    to -1 if the declarator is a name, and +1 if it is a
11498    function. Otherwise it is set to zero. Usually you just want to
11499    test for >0, but internally the negative value is used.
11500
11501    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11502    a decl-specifier-seq unless it declares a constructor, destructor,
11503    or conversion.  It might seem that we could check this condition in
11504    semantic analysis, rather than parsing, but that makes it difficult
11505    to handle something like `f()'.  We want to notice that there are
11506    no decl-specifiers, and therefore realize that this is an
11507    expression, not a declaration.)
11508
11509    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11510    the declarator is a direct-declarator of the form "(...)".
11511
11512    MEMBER_P is true iff this declarator is a member-declarator.  */
11513
11514 static cp_declarator *
11515 cp_parser_declarator (cp_parser* parser,
11516                       cp_parser_declarator_kind dcl_kind,
11517                       int* ctor_dtor_or_conv_p,
11518                       bool* parenthesized_p,
11519                       bool member_p)
11520 {
11521   cp_token *token;
11522   cp_declarator *declarator;
11523   enum tree_code code;
11524   cp_cv_quals cv_quals;
11525   tree class_type;
11526   tree attributes = NULL_TREE;
11527
11528   /* Assume this is not a constructor, destructor, or type-conversion
11529      operator.  */
11530   if (ctor_dtor_or_conv_p)
11531     *ctor_dtor_or_conv_p = 0;
11532
11533   if (cp_parser_allow_gnu_extensions_p (parser))
11534     attributes = cp_parser_attributes_opt (parser);
11535
11536   /* Peek at the next token.  */
11537   token = cp_lexer_peek_token (parser->lexer);
11538
11539   /* Check for the ptr-operator production.  */
11540   cp_parser_parse_tentatively (parser);
11541   /* Parse the ptr-operator.  */
11542   code = cp_parser_ptr_operator (parser,
11543                                  &class_type,
11544                                  &cv_quals);
11545   /* If that worked, then we have a ptr-operator.  */
11546   if (cp_parser_parse_definitely (parser))
11547     {
11548       /* If a ptr-operator was found, then this declarator was not
11549          parenthesized.  */
11550       if (parenthesized_p)
11551         *parenthesized_p = true;
11552       /* The dependent declarator is optional if we are parsing an
11553          abstract-declarator.  */
11554       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11555         cp_parser_parse_tentatively (parser);
11556
11557       /* Parse the dependent declarator.  */
11558       declarator = cp_parser_declarator (parser, dcl_kind,
11559                                          /*ctor_dtor_or_conv_p=*/NULL,
11560                                          /*parenthesized_p=*/NULL,
11561                                          /*member_p=*/false);
11562
11563       /* If we are parsing an abstract-declarator, we must handle the
11564          case where the dependent declarator is absent.  */
11565       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11566           && !cp_parser_parse_definitely (parser))
11567         declarator = NULL;
11568
11569       /* Build the representation of the ptr-operator.  */
11570       if (class_type)
11571         declarator = make_ptrmem_declarator (cv_quals,
11572                                              class_type,
11573                                              declarator);
11574       else if (code == INDIRECT_REF)
11575         declarator = make_pointer_declarator (cv_quals, declarator);
11576       else
11577         declarator = make_reference_declarator (cv_quals, declarator);
11578     }
11579   /* Everything else is a direct-declarator.  */
11580   else
11581     {
11582       if (parenthesized_p)
11583         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11584                                                    CPP_OPEN_PAREN);
11585       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11586                                                 ctor_dtor_or_conv_p,
11587                                                 member_p);
11588     }
11589
11590   if (attributes && declarator && declarator != cp_error_declarator)
11591     declarator->attributes = attributes;
11592
11593   return declarator;
11594 }
11595
11596 /* Parse a direct-declarator or direct-abstract-declarator.
11597
11598    direct-declarator:
11599      declarator-id
11600      direct-declarator ( parameter-declaration-clause )
11601        cv-qualifier-seq [opt]
11602        exception-specification [opt]
11603      direct-declarator [ constant-expression [opt] ]
11604      ( declarator )
11605
11606    direct-abstract-declarator:
11607      direct-abstract-declarator [opt]
11608        ( parameter-declaration-clause )
11609        cv-qualifier-seq [opt]
11610        exception-specification [opt]
11611      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11612      ( abstract-declarator )
11613
11614    Returns a representation of the declarator.  DCL_KIND is
11615    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11616    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11617    we are parsing a direct-declarator.  It is
11618    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11619    of ambiguity we prefer an abstract declarator, as per
11620    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11621    cp_parser_declarator.  */
11622
11623 static cp_declarator *
11624 cp_parser_direct_declarator (cp_parser* parser,
11625                              cp_parser_declarator_kind dcl_kind,
11626                              int* ctor_dtor_or_conv_p,
11627                              bool member_p)
11628 {
11629   cp_token *token;
11630   cp_declarator *declarator = NULL;
11631   tree scope = NULL_TREE;
11632   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11633   bool saved_in_declarator_p = parser->in_declarator_p;
11634   bool first = true;
11635   tree pushed_scope = NULL_TREE;
11636
11637   while (true)
11638     {
11639       /* Peek at the next token.  */
11640       token = cp_lexer_peek_token (parser->lexer);
11641       if (token->type == CPP_OPEN_PAREN)
11642         {
11643           /* This is either a parameter-declaration-clause, or a
11644              parenthesized declarator. When we know we are parsing a
11645              named declarator, it must be a parenthesized declarator
11646              if FIRST is true. For instance, `(int)' is a
11647              parameter-declaration-clause, with an omitted
11648              direct-abstract-declarator. But `((*))', is a
11649              parenthesized abstract declarator. Finally, when T is a
11650              template parameter `(T)' is a
11651              parameter-declaration-clause, and not a parenthesized
11652              named declarator.
11653
11654              We first try and parse a parameter-declaration-clause,
11655              and then try a nested declarator (if FIRST is true).
11656
11657              It is not an error for it not to be a
11658              parameter-declaration-clause, even when FIRST is
11659              false. Consider,
11660
11661                int i (int);
11662                int i (3);
11663
11664              The first is the declaration of a function while the
11665              second is a the definition of a variable, including its
11666              initializer.
11667
11668              Having seen only the parenthesis, we cannot know which of
11669              these two alternatives should be selected.  Even more
11670              complex are examples like:
11671
11672                int i (int (a));
11673                int i (int (3));
11674
11675              The former is a function-declaration; the latter is a
11676              variable initialization.
11677
11678              Thus again, we try a parameter-declaration-clause, and if
11679              that fails, we back out and return.  */
11680
11681           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11682             {
11683               cp_parameter_declarator *params;
11684               unsigned saved_num_template_parameter_lists;
11685
11686               /* In a member-declarator, the only valid interpretation
11687                  of a parenthesis is the start of a
11688                  parameter-declaration-clause.  (It is invalid to
11689                  initialize a static data member with a parenthesized
11690                  initializer; only the "=" form of initialization is
11691                  permitted.)  */
11692               if (!member_p)
11693                 cp_parser_parse_tentatively (parser);
11694
11695               /* Consume the `('.  */
11696               cp_lexer_consume_token (parser->lexer);
11697               if (first)
11698                 {
11699                   /* If this is going to be an abstract declarator, we're
11700                      in a declarator and we can't have default args.  */
11701                   parser->default_arg_ok_p = false;
11702                   parser->in_declarator_p = true;
11703                 }
11704
11705               /* Inside the function parameter list, surrounding
11706                  template-parameter-lists do not apply.  */
11707               saved_num_template_parameter_lists
11708                 = parser->num_template_parameter_lists;
11709               parser->num_template_parameter_lists = 0;
11710
11711               /* Parse the parameter-declaration-clause.  */
11712               params = cp_parser_parameter_declaration_clause (parser);
11713
11714               parser->num_template_parameter_lists
11715                 = saved_num_template_parameter_lists;
11716
11717               /* If all went well, parse the cv-qualifier-seq and the
11718                  exception-specification.  */
11719               if (member_p || cp_parser_parse_definitely (parser))
11720                 {
11721                   cp_cv_quals cv_quals;
11722                   tree exception_specification;
11723
11724                   if (ctor_dtor_or_conv_p)
11725                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11726                   first = false;
11727                   /* Consume the `)'.  */
11728                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11729
11730                   /* Parse the cv-qualifier-seq.  */
11731                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11732                   /* And the exception-specification.  */
11733                   exception_specification
11734                     = cp_parser_exception_specification_opt (parser);
11735
11736                   /* Create the function-declarator.  */
11737                   declarator = make_call_declarator (declarator,
11738                                                      params,
11739                                                      cv_quals,
11740                                                      exception_specification);
11741                   /* Any subsequent parameter lists are to do with
11742                      return type, so are not those of the declared
11743                      function.  */
11744                   parser->default_arg_ok_p = false;
11745
11746                   /* Repeat the main loop.  */
11747                   continue;
11748                 }
11749             }
11750
11751           /* If this is the first, we can try a parenthesized
11752              declarator.  */
11753           if (first)
11754             {
11755               bool saved_in_type_id_in_expr_p;
11756
11757               parser->default_arg_ok_p = saved_default_arg_ok_p;
11758               parser->in_declarator_p = saved_in_declarator_p;
11759
11760               /* Consume the `('.  */
11761               cp_lexer_consume_token (parser->lexer);
11762               /* Parse the nested declarator.  */
11763               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11764               parser->in_type_id_in_expr_p = true;
11765               declarator
11766                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11767                                         /*parenthesized_p=*/NULL,
11768                                         member_p);
11769               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11770               first = false;
11771               /* Expect a `)'.  */
11772               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11773                 declarator = cp_error_declarator;
11774               if (declarator == cp_error_declarator)
11775                 break;
11776
11777               goto handle_declarator;
11778             }
11779           /* Otherwise, we must be done.  */
11780           else
11781             break;
11782         }
11783       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11784                && token->type == CPP_OPEN_SQUARE)
11785         {
11786           /* Parse an array-declarator.  */
11787           tree bounds;
11788
11789           if (ctor_dtor_or_conv_p)
11790             *ctor_dtor_or_conv_p = 0;
11791
11792           first = false;
11793           parser->default_arg_ok_p = false;
11794           parser->in_declarator_p = true;
11795           /* Consume the `['.  */
11796           cp_lexer_consume_token (parser->lexer);
11797           /* Peek at the next token.  */
11798           token = cp_lexer_peek_token (parser->lexer);
11799           /* If the next token is `]', then there is no
11800              constant-expression.  */
11801           if (token->type != CPP_CLOSE_SQUARE)
11802             {
11803               bool non_constant_p;
11804
11805               bounds
11806                 = cp_parser_constant_expression (parser,
11807                                                  /*allow_non_constant=*/true,
11808                                                  &non_constant_p);
11809               if (!non_constant_p)
11810                 bounds = fold_non_dependent_expr (bounds);
11811               /* Normally, the array bound must be an integral constant
11812                  expression.  However, as an extension, we allow VLAs
11813                  in function scopes.  */
11814               else if (!parser->in_function_body)
11815                 {
11816                   error ("array bound is not an integer constant");
11817                   bounds = error_mark_node;
11818                 }
11819             }
11820           else
11821             bounds = NULL_TREE;
11822           /* Look for the closing `]'.  */
11823           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11824             {
11825               declarator = cp_error_declarator;
11826               break;
11827             }
11828
11829           declarator = make_array_declarator (declarator, bounds);
11830         }
11831       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11832         {
11833           tree qualifying_scope;
11834           tree unqualified_name;
11835           special_function_kind sfk;
11836           bool abstract_ok;
11837
11838           /* Parse a declarator-id */
11839           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11840           if (abstract_ok)
11841             cp_parser_parse_tentatively (parser);
11842           unqualified_name
11843             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11844           qualifying_scope = parser->scope;
11845           if (abstract_ok)
11846             {
11847               if (!cp_parser_parse_definitely (parser))
11848                 unqualified_name = error_mark_node;
11849               else if (unqualified_name
11850                        && (qualifying_scope
11851                            || (TREE_CODE (unqualified_name)
11852                                != IDENTIFIER_NODE)))
11853                 {
11854                   cp_parser_error (parser, "expected unqualified-id");
11855                   unqualified_name = error_mark_node;
11856                 }
11857             }
11858
11859           if (!unqualified_name)
11860             return NULL;
11861           if (unqualified_name == error_mark_node)
11862             {
11863               declarator = cp_error_declarator;
11864               break;
11865             }
11866
11867           if (qualifying_scope && at_namespace_scope_p ()
11868               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11869             {
11870               /* In the declaration of a member of a template class
11871                  outside of the class itself, the SCOPE will sometimes
11872                  be a TYPENAME_TYPE.  For example, given:
11873
11874                  template <typename T>
11875                  int S<T>::R::i = 3;
11876
11877                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11878                  this context, we must resolve S<T>::R to an ordinary
11879                  type, rather than a typename type.
11880
11881                  The reason we normally avoid resolving TYPENAME_TYPEs
11882                  is that a specialization of `S' might render
11883                  `S<T>::R' not a type.  However, if `S' is
11884                  specialized, then this `i' will not be used, so there
11885                  is no harm in resolving the types here.  */
11886               tree type;
11887
11888               /* Resolve the TYPENAME_TYPE.  */
11889               type = resolve_typename_type (qualifying_scope,
11890                                             /*only_current_p=*/false);
11891               /* If that failed, the declarator is invalid.  */
11892               if (type == error_mark_node)
11893                 error ("%<%T::%D%> is not a type",
11894                        TYPE_CONTEXT (qualifying_scope),
11895                        TYPE_IDENTIFIER (qualifying_scope));
11896               qualifying_scope = type;
11897             }
11898
11899           sfk = sfk_none;
11900           if (unqualified_name)
11901             {
11902               tree class_type;
11903
11904               if (qualifying_scope
11905                   && CLASS_TYPE_P (qualifying_scope))
11906                 class_type = qualifying_scope;
11907               else
11908                 class_type = current_class_type;
11909
11910               if (TREE_CODE (unqualified_name) == TYPE_DECL)
11911                 {
11912                   tree name_type = TREE_TYPE (unqualified_name);
11913                   if (class_type && same_type_p (name_type, class_type))
11914                     {
11915                       if (qualifying_scope
11916                           && CLASSTYPE_USE_TEMPLATE (name_type))
11917                         {
11918                           error ("invalid use of constructor as a template");
11919                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11920                                   "name the constructor in a qualified name",
11921                                   class_type,
11922                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11923                                   class_type, name_type);
11924                           declarator = cp_error_declarator;
11925                           break;
11926                         }
11927                       else
11928                         unqualified_name = constructor_name (class_type);
11929                     }
11930                   else
11931                     {
11932                       /* We do not attempt to print the declarator
11933                          here because we do not have enough
11934                          information about its original syntactic
11935                          form.  */
11936                       cp_parser_error (parser, "invalid declarator");
11937                       declarator = cp_error_declarator;
11938                       break;
11939                     }
11940                 }
11941
11942               if (class_type)
11943                 {
11944                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11945                     sfk = sfk_destructor;
11946                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11947                     sfk = sfk_conversion;
11948                   else if (/* There's no way to declare a constructor
11949                               for an anonymous type, even if the type
11950                               got a name for linkage purposes.  */
11951                            !TYPE_WAS_ANONYMOUS (class_type)
11952                            && constructor_name_p (unqualified_name,
11953                                                   class_type))
11954                     {
11955                       unqualified_name = constructor_name (class_type);
11956                       sfk = sfk_constructor;
11957                     }
11958
11959                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
11960                     *ctor_dtor_or_conv_p = -1;
11961                 }
11962             }
11963           declarator = make_id_declarator (qualifying_scope,
11964                                            unqualified_name,
11965                                            sfk);
11966           declarator->id_loc = token->location;
11967
11968         handle_declarator:;
11969           scope = get_scope_of_declarator (declarator);
11970           if (scope)
11971             /* Any names that appear after the declarator-id for a
11972                member are looked up in the containing scope.  */
11973             pushed_scope = push_scope (scope);
11974           parser->in_declarator_p = true;
11975           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11976               || (declarator && declarator->kind == cdk_id))
11977             /* Default args are only allowed on function
11978                declarations.  */
11979             parser->default_arg_ok_p = saved_default_arg_ok_p;
11980           else
11981             parser->default_arg_ok_p = false;
11982
11983           first = false;
11984         }
11985       /* We're done.  */
11986       else
11987         break;
11988     }
11989
11990   /* For an abstract declarator, we might wind up with nothing at this
11991      point.  That's an error; the declarator is not optional.  */
11992   if (!declarator)
11993     cp_parser_error (parser, "expected declarator");
11994
11995   /* If we entered a scope, we must exit it now.  */
11996   if (pushed_scope)
11997     pop_scope (pushed_scope);
11998
11999   parser->default_arg_ok_p = saved_default_arg_ok_p;
12000   parser->in_declarator_p = saved_in_declarator_p;
12001
12002   return declarator;
12003 }
12004
12005 /* Parse a ptr-operator.
12006
12007    ptr-operator:
12008      * cv-qualifier-seq [opt]
12009      &
12010      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12011
12012    GNU Extension:
12013
12014    ptr-operator:
12015      & cv-qualifier-seq [opt]
12016
12017    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12018    Returns ADDR_EXPR if a reference was used.  In the case of a
12019    pointer-to-member, *TYPE is filled in with the TYPE containing the
12020    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
12021    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
12022    ERROR_MARK if an error occurred.  */
12023
12024 static enum tree_code
12025 cp_parser_ptr_operator (cp_parser* parser,
12026                         tree* type,
12027                         cp_cv_quals *cv_quals)
12028 {
12029   enum tree_code code = ERROR_MARK;
12030   cp_token *token;
12031
12032   /* Assume that it's not a pointer-to-member.  */
12033   *type = NULL_TREE;
12034   /* And that there are no cv-qualifiers.  */
12035   *cv_quals = TYPE_UNQUALIFIED;
12036
12037   /* Peek at the next token.  */
12038   token = cp_lexer_peek_token (parser->lexer);
12039   /* If it's a `*' or `&' we have a pointer or reference.  */
12040   if (token->type == CPP_MULT || token->type == CPP_AND)
12041     {
12042       /* Remember which ptr-operator we were processing.  */
12043       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12044
12045       /* Consume the `*' or `&'.  */
12046       cp_lexer_consume_token (parser->lexer);
12047
12048       /* A `*' can be followed by a cv-qualifier-seq, and so can a
12049          `&', if we are allowing GNU extensions.  (The only qualifier
12050          that can legally appear after `&' is `restrict', but that is
12051          enforced during semantic analysis.  */
12052       if (code == INDIRECT_REF
12053           || cp_parser_allow_gnu_extensions_p (parser))
12054         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12055     }
12056   else
12057     {
12058       /* Try the pointer-to-member case.  */
12059       cp_parser_parse_tentatively (parser);
12060       /* Look for the optional `::' operator.  */
12061       cp_parser_global_scope_opt (parser,
12062                                   /*current_scope_valid_p=*/false);
12063       /* Look for the nested-name specifier.  */
12064       cp_parser_nested_name_specifier (parser,
12065                                        /*typename_keyword_p=*/false,
12066                                        /*check_dependency_p=*/true,
12067                                        /*type_p=*/false,
12068                                        /*is_declaration=*/false);
12069       /* If we found it, and the next token is a `*', then we are
12070          indeed looking at a pointer-to-member operator.  */
12071       if (!cp_parser_error_occurred (parser)
12072           && cp_parser_require (parser, CPP_MULT, "`*'"))
12073         {
12074           /* Indicate that the `*' operator was used.  */
12075           code = INDIRECT_REF;
12076
12077           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12078             error ("%qD is a namespace", parser->scope);
12079           else
12080             {
12081               /* The type of which the member is a member is given by the
12082                  current SCOPE.  */
12083               *type = parser->scope;
12084               /* The next name will not be qualified.  */
12085               parser->scope = NULL_TREE;
12086               parser->qualifying_scope = NULL_TREE;
12087               parser->object_scope = NULL_TREE;
12088               /* Look for the optional cv-qualifier-seq.  */
12089               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12090             }
12091         }
12092       /* If that didn't work we don't have a ptr-operator.  */
12093       if (!cp_parser_parse_definitely (parser))
12094         cp_parser_error (parser, "expected ptr-operator");
12095     }
12096
12097   return code;
12098 }
12099
12100 /* Parse an (optional) cv-qualifier-seq.
12101
12102    cv-qualifier-seq:
12103      cv-qualifier cv-qualifier-seq [opt]
12104
12105    cv-qualifier:
12106      const
12107      volatile
12108
12109    GNU Extension:
12110
12111    cv-qualifier:
12112      __restrict__
12113
12114    Returns a bitmask representing the cv-qualifiers.  */
12115
12116 static cp_cv_quals
12117 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12118 {
12119   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12120
12121   while (true)
12122     {
12123       cp_token *token;
12124       cp_cv_quals cv_qualifier;
12125
12126       /* Peek at the next token.  */
12127       token = cp_lexer_peek_token (parser->lexer);
12128       /* See if it's a cv-qualifier.  */
12129       switch (token->keyword)
12130         {
12131         case RID_CONST:
12132           cv_qualifier = TYPE_QUAL_CONST;
12133           break;
12134
12135         case RID_VOLATILE:
12136           cv_qualifier = TYPE_QUAL_VOLATILE;
12137           break;
12138
12139         case RID_RESTRICT:
12140           cv_qualifier = TYPE_QUAL_RESTRICT;
12141           break;
12142
12143         default:
12144           cv_qualifier = TYPE_UNQUALIFIED;
12145           break;
12146         }
12147
12148       if (!cv_qualifier)
12149         break;
12150
12151       if (cv_quals & cv_qualifier)
12152         {
12153           error ("duplicate cv-qualifier");
12154           cp_lexer_purge_token (parser->lexer);
12155         }
12156       else
12157         {
12158           cp_lexer_consume_token (parser->lexer);
12159           cv_quals |= cv_qualifier;
12160         }
12161     }
12162
12163   return cv_quals;
12164 }
12165
12166 /* Parse a declarator-id.
12167
12168    declarator-id:
12169      id-expression
12170      :: [opt] nested-name-specifier [opt] type-name
12171
12172    In the `id-expression' case, the value returned is as for
12173    cp_parser_id_expression if the id-expression was an unqualified-id.
12174    If the id-expression was a qualified-id, then a SCOPE_REF is
12175    returned.  The first operand is the scope (either a NAMESPACE_DECL
12176    or TREE_TYPE), but the second is still just a representation of an
12177    unqualified-id.  */
12178
12179 static tree
12180 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12181 {
12182   tree id;
12183   /* The expression must be an id-expression.  Assume that qualified
12184      names are the names of types so that:
12185
12186        template <class T>
12187        int S<T>::R::i = 3;
12188
12189      will work; we must treat `S<T>::R' as the name of a type.
12190      Similarly, assume that qualified names are templates, where
12191      required, so that:
12192
12193        template <class T>
12194        int S<T>::R<T>::i = 3;
12195
12196      will work, too.  */
12197   id = cp_parser_id_expression (parser,
12198                                 /*template_keyword_p=*/false,
12199                                 /*check_dependency_p=*/false,
12200                                 /*template_p=*/NULL,
12201                                 /*declarator_p=*/true,
12202                                 optional_p);
12203   if (id && BASELINK_P (id))
12204     id = BASELINK_FUNCTIONS (id);
12205   return id;
12206 }
12207
12208 /* Parse a type-id.
12209
12210    type-id:
12211      type-specifier-seq abstract-declarator [opt]
12212
12213    Returns the TYPE specified.  */
12214
12215 static tree
12216 cp_parser_type_id (cp_parser* parser)
12217 {
12218   cp_decl_specifier_seq type_specifier_seq;
12219   cp_declarator *abstract_declarator;
12220
12221   /* Parse the type-specifier-seq.  */
12222   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12223                                 &type_specifier_seq);
12224   if (type_specifier_seq.type == error_mark_node)
12225     return error_mark_node;
12226
12227   /* There might or might not be an abstract declarator.  */
12228   cp_parser_parse_tentatively (parser);
12229   /* Look for the declarator.  */
12230   abstract_declarator
12231     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12232                             /*parenthesized_p=*/NULL,
12233                             /*member_p=*/false);
12234   /* Check to see if there really was a declarator.  */
12235   if (!cp_parser_parse_definitely (parser))
12236     abstract_declarator = NULL;
12237
12238   return groktypename (&type_specifier_seq, abstract_declarator);
12239 }
12240
12241 /* Parse a type-specifier-seq.
12242
12243    type-specifier-seq:
12244      type-specifier type-specifier-seq [opt]
12245
12246    GNU extension:
12247
12248    type-specifier-seq:
12249      attributes type-specifier-seq [opt]
12250
12251    If IS_CONDITION is true, we are at the start of a "condition",
12252    e.g., we've just seen "if (".
12253
12254    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
12255
12256 static void
12257 cp_parser_type_specifier_seq (cp_parser* parser,
12258                               bool is_condition,
12259                               cp_decl_specifier_seq *type_specifier_seq)
12260 {
12261   bool seen_type_specifier = false;
12262   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12263
12264   /* Clear the TYPE_SPECIFIER_SEQ.  */
12265   clear_decl_specs (type_specifier_seq);
12266
12267   /* Parse the type-specifiers and attributes.  */
12268   while (true)
12269     {
12270       tree type_specifier;
12271       bool is_cv_qualifier;
12272
12273       /* Check for attributes first.  */
12274       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12275         {
12276           type_specifier_seq->attributes =
12277             chainon (type_specifier_seq->attributes,
12278                      cp_parser_attributes_opt (parser));
12279           continue;
12280         }
12281
12282       /* Look for the type-specifier.  */
12283       type_specifier = cp_parser_type_specifier (parser,
12284                                                  flags,
12285                                                  type_specifier_seq,
12286                                                  /*is_declaration=*/false,
12287                                                  NULL,
12288                                                  &is_cv_qualifier);
12289       if (!type_specifier)
12290         {
12291           /* If the first type-specifier could not be found, this is not a
12292              type-specifier-seq at all.  */
12293           if (!seen_type_specifier)
12294             {
12295               cp_parser_error (parser, "expected type-specifier");
12296               type_specifier_seq->type = error_mark_node;
12297               return;
12298             }
12299           /* If subsequent type-specifiers could not be found, the
12300              type-specifier-seq is complete.  */
12301           break;
12302         }
12303
12304       seen_type_specifier = true;
12305       /* The standard says that a condition can be:
12306
12307             type-specifier-seq declarator = assignment-expression
12308
12309          However, given:
12310
12311            struct S {};
12312            if (int S = ...)
12313
12314          we should treat the "S" as a declarator, not as a
12315          type-specifier.  The standard doesn't say that explicitly for
12316          type-specifier-seq, but it does say that for
12317          decl-specifier-seq in an ordinary declaration.  Perhaps it
12318          would be clearer just to allow a decl-specifier-seq here, and
12319          then add a semantic restriction that if any decl-specifiers
12320          that are not type-specifiers appear, the program is invalid.  */
12321       if (is_condition && !is_cv_qualifier)
12322         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12323     }
12324
12325   cp_parser_check_decl_spec (type_specifier_seq);
12326 }
12327
12328 /* Parse a parameter-declaration-clause.
12329
12330    parameter-declaration-clause:
12331      parameter-declaration-list [opt] ... [opt]
12332      parameter-declaration-list , ...
12333
12334    Returns a representation for the parameter declarations.  A return
12335    value of NULL indicates a parameter-declaration-clause consisting
12336    only of an ellipsis.  */
12337
12338 static cp_parameter_declarator *
12339 cp_parser_parameter_declaration_clause (cp_parser* parser)
12340 {
12341   cp_parameter_declarator *parameters;
12342   cp_token *token;
12343   bool ellipsis_p;
12344   bool is_error;
12345
12346   /* Peek at the next token.  */
12347   token = cp_lexer_peek_token (parser->lexer);
12348   /* Check for trivial parameter-declaration-clauses.  */
12349   if (token->type == CPP_ELLIPSIS)
12350     {
12351       /* Consume the `...' token.  */
12352       cp_lexer_consume_token (parser->lexer);
12353       return NULL;
12354     }
12355   else if (token->type == CPP_CLOSE_PAREN)
12356     /* There are no parameters.  */
12357     {
12358 #ifndef NO_IMPLICIT_EXTERN_C
12359       if (in_system_header && current_class_type == NULL
12360           && current_lang_name == lang_name_c)
12361         return NULL;
12362       else
12363 #endif
12364         return no_parameters;
12365     }
12366   /* Check for `(void)', too, which is a special case.  */
12367   else if (token->keyword == RID_VOID
12368            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12369                == CPP_CLOSE_PAREN))
12370     {
12371       /* Consume the `void' token.  */
12372       cp_lexer_consume_token (parser->lexer);
12373       /* There are no parameters.  */
12374       return no_parameters;
12375     }
12376
12377   /* Parse the parameter-declaration-list.  */
12378   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12379   /* If a parse error occurred while parsing the
12380      parameter-declaration-list, then the entire
12381      parameter-declaration-clause is erroneous.  */
12382   if (is_error)
12383     return NULL;
12384
12385   /* Peek at the next token.  */
12386   token = cp_lexer_peek_token (parser->lexer);
12387   /* If it's a `,', the clause should terminate with an ellipsis.  */
12388   if (token->type == CPP_COMMA)
12389     {
12390       /* Consume the `,'.  */
12391       cp_lexer_consume_token (parser->lexer);
12392       /* Expect an ellipsis.  */
12393       ellipsis_p
12394         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12395     }
12396   /* It might also be `...' if the optional trailing `,' was
12397      omitted.  */
12398   else if (token->type == CPP_ELLIPSIS)
12399     {
12400       /* Consume the `...' token.  */
12401       cp_lexer_consume_token (parser->lexer);
12402       /* And remember that we saw it.  */
12403       ellipsis_p = true;
12404     }
12405   else
12406     ellipsis_p = false;
12407
12408   /* Finish the parameter list.  */
12409   if (parameters && ellipsis_p)
12410     parameters->ellipsis_p = true;
12411
12412   return parameters;
12413 }
12414
12415 /* Parse a parameter-declaration-list.
12416
12417    parameter-declaration-list:
12418      parameter-declaration
12419      parameter-declaration-list , parameter-declaration
12420
12421    Returns a representation of the parameter-declaration-list, as for
12422    cp_parser_parameter_declaration_clause.  However, the
12423    `void_list_node' is never appended to the list.  Upon return,
12424    *IS_ERROR will be true iff an error occurred.  */
12425
12426 static cp_parameter_declarator *
12427 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12428 {
12429   cp_parameter_declarator *parameters = NULL;
12430   cp_parameter_declarator **tail = &parameters;
12431   bool saved_in_unbraced_linkage_specification_p;
12432
12433   /* Assume all will go well.  */
12434   *is_error = false;
12435   /* The special considerations that apply to a function within an
12436      unbraced linkage specifications do not apply to the parameters
12437      to the function.  */
12438   saved_in_unbraced_linkage_specification_p 
12439     = parser->in_unbraced_linkage_specification_p;
12440   parser->in_unbraced_linkage_specification_p = false;
12441
12442   /* Look for more parameters.  */
12443   while (true)
12444     {
12445       cp_parameter_declarator *parameter;
12446       bool parenthesized_p;
12447       /* Parse the parameter.  */
12448       parameter
12449         = cp_parser_parameter_declaration (parser,
12450                                            /*template_parm_p=*/false,
12451                                            &parenthesized_p);
12452
12453       /* If a parse error occurred parsing the parameter declaration,
12454          then the entire parameter-declaration-list is erroneous.  */
12455       if (!parameter)
12456         {
12457           *is_error = true;
12458           parameters = NULL;
12459           break;
12460         }
12461       /* Add the new parameter to the list.  */
12462       *tail = parameter;
12463       tail = &parameter->next;
12464
12465       /* Peek at the next token.  */
12466       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12467           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12468           /* These are for Objective-C++ */
12469           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12470           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12471         /* The parameter-declaration-list is complete.  */
12472         break;
12473       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12474         {
12475           cp_token *token;
12476
12477           /* Peek at the next token.  */
12478           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12479           /* If it's an ellipsis, then the list is complete.  */
12480           if (token->type == CPP_ELLIPSIS)
12481             break;
12482           /* Otherwise, there must be more parameters.  Consume the
12483              `,'.  */
12484           cp_lexer_consume_token (parser->lexer);
12485           /* When parsing something like:
12486
12487                 int i(float f, double d)
12488
12489              we can tell after seeing the declaration for "f" that we
12490              are not looking at an initialization of a variable "i",
12491              but rather at the declaration of a function "i".
12492
12493              Due to the fact that the parsing of template arguments
12494              (as specified to a template-id) requires backtracking we
12495              cannot use this technique when inside a template argument
12496              list.  */
12497           if (!parser->in_template_argument_list_p
12498               && !parser->in_type_id_in_expr_p
12499               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12500               /* However, a parameter-declaration of the form
12501                  "foat(f)" (which is a valid declaration of a
12502                  parameter "f") can also be interpreted as an
12503                  expression (the conversion of "f" to "float").  */
12504               && !parenthesized_p)
12505             cp_parser_commit_to_tentative_parse (parser);
12506         }
12507       else
12508         {
12509           cp_parser_error (parser, "expected %<,%> or %<...%>");
12510           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12511             cp_parser_skip_to_closing_parenthesis (parser,
12512                                                    /*recovering=*/true,
12513                                                    /*or_comma=*/false,
12514                                                    /*consume_paren=*/false);
12515           break;
12516         }
12517     }
12518
12519   parser->in_unbraced_linkage_specification_p
12520     = saved_in_unbraced_linkage_specification_p;
12521
12522   return parameters;
12523 }
12524
12525 /* Parse a parameter declaration.
12526
12527    parameter-declaration:
12528      decl-specifier-seq declarator
12529      decl-specifier-seq declarator = assignment-expression
12530      decl-specifier-seq abstract-declarator [opt]
12531      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12532
12533    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12534    declares a template parameter.  (In that case, a non-nested `>'
12535    token encountered during the parsing of the assignment-expression
12536    is not interpreted as a greater-than operator.)
12537
12538    Returns a representation of the parameter, or NULL if an error
12539    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12540    true iff the declarator is of the form "(p)".  */
12541
12542 static cp_parameter_declarator *
12543 cp_parser_parameter_declaration (cp_parser *parser,
12544                                  bool template_parm_p,
12545                                  bool *parenthesized_p)
12546 {
12547   int declares_class_or_enum;
12548   bool greater_than_is_operator_p;
12549   cp_decl_specifier_seq decl_specifiers;
12550   cp_declarator *declarator;
12551   tree default_argument;
12552   cp_token *token;
12553   const char *saved_message;
12554
12555   /* In a template parameter, `>' is not an operator.
12556
12557      [temp.param]
12558
12559      When parsing a default template-argument for a non-type
12560      template-parameter, the first non-nested `>' is taken as the end
12561      of the template parameter-list rather than a greater-than
12562      operator.  */
12563   greater_than_is_operator_p = !template_parm_p;
12564
12565   /* Type definitions may not appear in parameter types.  */
12566   saved_message = parser->type_definition_forbidden_message;
12567   parser->type_definition_forbidden_message
12568     = "types may not be defined in parameter types";
12569
12570   /* Parse the declaration-specifiers.  */
12571   cp_parser_decl_specifier_seq (parser,
12572                                 CP_PARSER_FLAGS_NONE,
12573                                 &decl_specifiers,
12574                                 &declares_class_or_enum);
12575   /* If an error occurred, there's no reason to attempt to parse the
12576      rest of the declaration.  */
12577   if (cp_parser_error_occurred (parser))
12578     {
12579       parser->type_definition_forbidden_message = saved_message;
12580       return NULL;
12581     }
12582
12583   /* Peek at the next token.  */
12584   token = cp_lexer_peek_token (parser->lexer);
12585   /* If the next token is a `)', `,', `=', `>', or `...', then there
12586      is no declarator.  */
12587   if (token->type == CPP_CLOSE_PAREN
12588       || token->type == CPP_COMMA
12589       || token->type == CPP_EQ
12590       || token->type == CPP_ELLIPSIS
12591       || token->type == CPP_GREATER)
12592     {
12593       declarator = NULL;
12594       if (parenthesized_p)
12595         *parenthesized_p = false;
12596     }
12597   /* Otherwise, there should be a declarator.  */
12598   else
12599     {
12600       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12601       parser->default_arg_ok_p = false;
12602
12603       /* After seeing a decl-specifier-seq, if the next token is not a
12604          "(", there is no possibility that the code is a valid
12605          expression.  Therefore, if parsing tentatively, we commit at
12606          this point.  */
12607       if (!parser->in_template_argument_list_p
12608           /* In an expression context, having seen:
12609
12610                (int((char ...
12611
12612              we cannot be sure whether we are looking at a
12613              function-type (taking a "char" as a parameter) or a cast
12614              of some object of type "char" to "int".  */
12615           && !parser->in_type_id_in_expr_p
12616           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12617           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12618         cp_parser_commit_to_tentative_parse (parser);
12619       /* Parse the declarator.  */
12620       declarator = cp_parser_declarator (parser,
12621                                          CP_PARSER_DECLARATOR_EITHER,
12622                                          /*ctor_dtor_or_conv_p=*/NULL,
12623                                          parenthesized_p,
12624                                          /*member_p=*/false);
12625       parser->default_arg_ok_p = saved_default_arg_ok_p;
12626       /* After the declarator, allow more attributes.  */
12627       decl_specifiers.attributes
12628         = chainon (decl_specifiers.attributes,
12629                    cp_parser_attributes_opt (parser));
12630     }
12631
12632   /* The restriction on defining new types applies only to the type
12633      of the parameter, not to the default argument.  */
12634   parser->type_definition_forbidden_message = saved_message;
12635
12636   /* If the next token is `=', then process a default argument.  */
12637   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12638     {
12639       bool saved_greater_than_is_operator_p;
12640       /* Consume the `='.  */
12641       cp_lexer_consume_token (parser->lexer);
12642
12643       /* If we are defining a class, then the tokens that make up the
12644          default argument must be saved and processed later.  */
12645       if (!template_parm_p && at_class_scope_p ()
12646           && TYPE_BEING_DEFINED (current_class_type))
12647         {
12648           unsigned depth = 0;
12649           cp_token *first_token;
12650           cp_token *token;
12651
12652           /* Add tokens until we have processed the entire default
12653              argument.  We add the range [first_token, token).  */
12654           first_token = cp_lexer_peek_token (parser->lexer);
12655           while (true)
12656             {
12657               bool done = false;
12658
12659               /* Peek at the next token.  */
12660               token = cp_lexer_peek_token (parser->lexer);
12661               /* What we do depends on what token we have.  */
12662               switch (token->type)
12663                 {
12664                   /* In valid code, a default argument must be
12665                      immediately followed by a `,' `)', or `...'.  */
12666                 case CPP_COMMA:
12667                 case CPP_CLOSE_PAREN:
12668                 case CPP_ELLIPSIS:
12669                   /* If we run into a non-nested `;', `}', or `]',
12670                      then the code is invalid -- but the default
12671                      argument is certainly over.  */
12672                 case CPP_SEMICOLON:
12673                 case CPP_CLOSE_BRACE:
12674                 case CPP_CLOSE_SQUARE:
12675                   if (depth == 0)
12676                     done = true;
12677                   /* Update DEPTH, if necessary.  */
12678                   else if (token->type == CPP_CLOSE_PAREN
12679                            || token->type == CPP_CLOSE_BRACE
12680                            || token->type == CPP_CLOSE_SQUARE)
12681                     --depth;
12682                   break;
12683
12684                 case CPP_OPEN_PAREN:
12685                 case CPP_OPEN_SQUARE:
12686                 case CPP_OPEN_BRACE:
12687                   ++depth;
12688                   break;
12689
12690                 case CPP_GREATER:
12691                   /* If we see a non-nested `>', and `>' is not an
12692                      operator, then it marks the end of the default
12693                      argument.  */
12694                   if (!depth && !greater_than_is_operator_p)
12695                     done = true;
12696                   break;
12697
12698                   /* If we run out of tokens, issue an error message.  */
12699                 case CPP_EOF:
12700                 case CPP_PRAGMA_EOL:
12701                   error ("file ends in default argument");
12702                   done = true;
12703                   break;
12704
12705                 case CPP_NAME:
12706                 case CPP_SCOPE:
12707                   /* In these cases, we should look for template-ids.
12708                      For example, if the default argument is
12709                      `X<int, double>()', we need to do name lookup to
12710                      figure out whether or not `X' is a template; if
12711                      so, the `,' does not end the default argument.
12712
12713                      That is not yet done.  */
12714                   break;
12715
12716                 default:
12717                   break;
12718                 }
12719
12720               /* If we've reached the end, stop.  */
12721               if (done)
12722                 break;
12723
12724               /* Add the token to the token block.  */
12725               token = cp_lexer_consume_token (parser->lexer);
12726             }
12727
12728           /* Create a DEFAULT_ARG to represented the unparsed default
12729              argument.  */
12730           default_argument = make_node (DEFAULT_ARG);
12731           DEFARG_TOKENS (default_argument)
12732             = cp_token_cache_new (first_token, token);
12733           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12734         }
12735       /* Outside of a class definition, we can just parse the
12736          assignment-expression.  */
12737       else
12738         {
12739           bool saved_local_variables_forbidden_p;
12740
12741           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12742              set correctly.  */
12743           saved_greater_than_is_operator_p
12744             = parser->greater_than_is_operator_p;
12745           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12746           /* Local variable names (and the `this' keyword) may not
12747              appear in a default argument.  */
12748           saved_local_variables_forbidden_p
12749             = parser->local_variables_forbidden_p;
12750           parser->local_variables_forbidden_p = true;
12751           /* The default argument expression may cause implicitly
12752              defined member functions to be synthesized, which will
12753              result in garbage collection.  We must treat this
12754              situation as if we were within the body of function so as
12755              to avoid collecting live data on the stack.  */
12756           ++function_depth;
12757           /* Parse the assignment-expression.  */
12758           if (template_parm_p)
12759             push_deferring_access_checks (dk_no_deferred);
12760           default_argument
12761             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12762           if (template_parm_p)
12763             pop_deferring_access_checks ();
12764           /* Restore saved state.  */
12765           --function_depth;
12766           parser->greater_than_is_operator_p
12767             = saved_greater_than_is_operator_p;
12768           parser->local_variables_forbidden_p
12769             = saved_local_variables_forbidden_p;
12770         }
12771       if (!parser->default_arg_ok_p)
12772         {
12773           if (!flag_pedantic_errors)
12774             warning (0, "deprecated use of default argument for parameter of non-function");
12775           else
12776             {
12777               error ("default arguments are only permitted for function parameters");
12778               default_argument = NULL_TREE;
12779             }
12780         }
12781     }
12782   else
12783     default_argument = NULL_TREE;
12784
12785   return make_parameter_declarator (&decl_specifiers,
12786                                     declarator,
12787                                     default_argument);
12788 }
12789
12790 /* Parse a function-body.
12791
12792    function-body:
12793      compound_statement  */
12794
12795 static void
12796 cp_parser_function_body (cp_parser *parser)
12797 {
12798   cp_parser_compound_statement (parser, NULL, false);
12799 }
12800
12801 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12802    true if a ctor-initializer was present.  */
12803
12804 static bool
12805 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12806 {
12807   tree body;
12808   bool ctor_initializer_p;
12809
12810   /* Begin the function body.  */
12811   body = begin_function_body ();
12812   /* Parse the optional ctor-initializer.  */
12813   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12814   /* Parse the function-body.  */
12815   cp_parser_function_body (parser);
12816   /* Finish the function body.  */
12817   finish_function_body (body);
12818
12819   return ctor_initializer_p;
12820 }
12821
12822 /* Parse an initializer.
12823
12824    initializer:
12825      = initializer-clause
12826      ( expression-list )
12827
12828    Returns an expression representing the initializer.  If no
12829    initializer is present, NULL_TREE is returned.
12830
12831    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12832    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12833    set to FALSE if there is no initializer present.  If there is an
12834    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12835    is set to true; otherwise it is set to false.  */
12836
12837 static tree
12838 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12839                        bool* non_constant_p)
12840 {
12841   cp_token *token;
12842   tree init;
12843
12844   /* Peek at the next token.  */
12845   token = cp_lexer_peek_token (parser->lexer);
12846
12847   /* Let our caller know whether or not this initializer was
12848      parenthesized.  */
12849   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12850   /* Assume that the initializer is constant.  */
12851   *non_constant_p = false;
12852
12853   if (token->type == CPP_EQ)
12854     {
12855       /* Consume the `='.  */
12856       cp_lexer_consume_token (parser->lexer);
12857       /* Parse the initializer-clause.  */
12858       init = cp_parser_initializer_clause (parser, non_constant_p);
12859     }
12860   else if (token->type == CPP_OPEN_PAREN)
12861     init = cp_parser_parenthesized_expression_list (parser, false,
12862                                                     /*cast_p=*/false,
12863                                                     non_constant_p);
12864   else
12865     {
12866       /* Anything else is an error.  */
12867       cp_parser_error (parser, "expected initializer");
12868       init = error_mark_node;
12869     }
12870
12871   return init;
12872 }
12873
12874 /* Parse an initializer-clause.
12875
12876    initializer-clause:
12877      assignment-expression
12878      { initializer-list , [opt] }
12879      { }
12880
12881    Returns an expression representing the initializer.
12882
12883    If the `assignment-expression' production is used the value
12884    returned is simply a representation for the expression.
12885
12886    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12887    the elements of the initializer-list (or NULL, if the last
12888    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12889    NULL_TREE.  There is no way to detect whether or not the optional
12890    trailing `,' was provided.  NON_CONSTANT_P is as for
12891    cp_parser_initializer.  */
12892
12893 static tree
12894 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12895 {
12896   tree initializer;
12897
12898   /* Assume the expression is constant.  */
12899   *non_constant_p = false;
12900
12901   /* If it is not a `{', then we are looking at an
12902      assignment-expression.  */
12903   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12904     {
12905       initializer
12906         = cp_parser_constant_expression (parser,
12907                                         /*allow_non_constant_p=*/true,
12908                                         non_constant_p);
12909       if (!*non_constant_p)
12910         initializer = fold_non_dependent_expr (initializer);
12911     }
12912   else
12913     {
12914       /* Consume the `{' token.  */
12915       cp_lexer_consume_token (parser->lexer);
12916       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12917       initializer = make_node (CONSTRUCTOR);
12918       /* If it's not a `}', then there is a non-trivial initializer.  */
12919       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12920         {
12921           /* Parse the initializer list.  */
12922           CONSTRUCTOR_ELTS (initializer)
12923             = cp_parser_initializer_list (parser, non_constant_p);
12924           /* A trailing `,' token is allowed.  */
12925           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12926             cp_lexer_consume_token (parser->lexer);
12927         }
12928       /* Now, there should be a trailing `}'.  */
12929       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12930     }
12931
12932   return initializer;
12933 }
12934
12935 /* Parse an initializer-list.
12936
12937    initializer-list:
12938      initializer-clause
12939      initializer-list , initializer-clause
12940
12941    GNU Extension:
12942
12943    initializer-list:
12944      identifier : initializer-clause
12945      initializer-list, identifier : initializer-clause
12946
12947    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12948    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12949    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12950    as for cp_parser_initializer.  */
12951
12952 static VEC(constructor_elt,gc) *
12953 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12954 {
12955   VEC(constructor_elt,gc) *v = NULL;
12956
12957   /* Assume all of the expressions are constant.  */
12958   *non_constant_p = false;
12959
12960   /* Parse the rest of the list.  */
12961   while (true)
12962     {
12963       cp_token *token;
12964       tree identifier;
12965       tree initializer;
12966       bool clause_non_constant_p;
12967
12968       /* If the next token is an identifier and the following one is a
12969          colon, we are looking at the GNU designated-initializer
12970          syntax.  */
12971       if (cp_parser_allow_gnu_extensions_p (parser)
12972           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12973           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12974         {
12975           /* Warn the user that they are using an extension.  */
12976           if (pedantic)
12977             pedwarn ("ISO C++ does not allow designated initializers");
12978           /* Consume the identifier.  */
12979           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
12980           /* Consume the `:'.  */
12981           cp_lexer_consume_token (parser->lexer);
12982         }
12983       else
12984         identifier = NULL_TREE;
12985
12986       /* Parse the initializer.  */
12987       initializer = cp_parser_initializer_clause (parser,
12988                                                   &clause_non_constant_p);
12989       /* If any clause is non-constant, so is the entire initializer.  */
12990       if (clause_non_constant_p)
12991         *non_constant_p = true;
12992
12993       /* Add it to the vector.  */
12994       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12995
12996       /* If the next token is not a comma, we have reached the end of
12997          the list.  */
12998       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12999         break;
13000
13001       /* Peek at the next token.  */
13002       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13003       /* If the next token is a `}', then we're still done.  An
13004          initializer-clause can have a trailing `,' after the
13005          initializer-list and before the closing `}'.  */
13006       if (token->type == CPP_CLOSE_BRACE)
13007         break;
13008
13009       /* Consume the `,' token.  */
13010       cp_lexer_consume_token (parser->lexer);
13011     }
13012
13013   return v;
13014 }
13015
13016 /* Classes [gram.class] */
13017
13018 /* Parse a class-name.
13019
13020    class-name:
13021      identifier
13022      template-id
13023
13024    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13025    to indicate that names looked up in dependent types should be
13026    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
13027    keyword has been used to indicate that the name that appears next
13028    is a template.  TAG_TYPE indicates the explicit tag given before
13029    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
13030    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
13031    is the class being defined in a class-head.
13032
13033    Returns the TYPE_DECL representing the class.  */
13034
13035 static tree
13036 cp_parser_class_name (cp_parser *parser,
13037                       bool typename_keyword_p,
13038                       bool template_keyword_p,
13039                       enum tag_types tag_type,
13040                       bool check_dependency_p,
13041                       bool class_head_p,
13042                       bool is_declaration)
13043 {
13044   tree decl;
13045   tree scope;
13046   bool typename_p;
13047   cp_token *token;
13048
13049   /* All class-names start with an identifier.  */
13050   token = cp_lexer_peek_token (parser->lexer);
13051   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13052     {
13053       cp_parser_error (parser, "expected class-name");
13054       return error_mark_node;
13055     }
13056
13057   /* PARSER->SCOPE can be cleared when parsing the template-arguments
13058      to a template-id, so we save it here.  */
13059   scope = parser->scope;
13060   if (scope == error_mark_node)
13061     return error_mark_node;
13062
13063   /* Any name names a type if we're following the `typename' keyword
13064      in a qualified name where the enclosing scope is type-dependent.  */
13065   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13066                 && dependent_type_p (scope));
13067   /* Handle the common case (an identifier, but not a template-id)
13068      efficiently.  */
13069   if (token->type == CPP_NAME
13070       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13071     {
13072       cp_token *identifier_token;
13073       tree identifier;
13074       bool ambiguous_p;
13075
13076       /* Look for the identifier.  */
13077       identifier_token = cp_lexer_peek_token (parser->lexer);
13078       ambiguous_p = identifier_token->ambiguous_p;
13079       identifier = cp_parser_identifier (parser);
13080       /* If the next token isn't an identifier, we are certainly not
13081          looking at a class-name.  */
13082       if (identifier == error_mark_node)
13083         decl = error_mark_node;
13084       /* If we know this is a type-name, there's no need to look it
13085          up.  */
13086       else if (typename_p)
13087         decl = identifier;
13088       else
13089         {
13090           tree ambiguous_decls;
13091           /* If we already know that this lookup is ambiguous, then
13092              we've already issued an error message; there's no reason
13093              to check again.  */
13094           if (ambiguous_p)
13095             {
13096               cp_parser_simulate_error (parser);
13097               return error_mark_node;
13098             }
13099           /* If the next token is a `::', then the name must be a type
13100              name.
13101
13102              [basic.lookup.qual]
13103
13104              During the lookup for a name preceding the :: scope
13105              resolution operator, object, function, and enumerator
13106              names are ignored.  */
13107           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13108             tag_type = typename_type;
13109           /* Look up the name.  */
13110           decl = cp_parser_lookup_name (parser, identifier,
13111                                         tag_type,
13112                                         /*is_template=*/false,
13113                                         /*is_namespace=*/false,
13114                                         check_dependency_p,
13115                                         &ambiguous_decls);
13116           if (ambiguous_decls)
13117             {
13118               error ("reference to %qD is ambiguous", identifier);
13119               print_candidates (ambiguous_decls);
13120               if (cp_parser_parsing_tentatively (parser))
13121                 {
13122                   identifier_token->ambiguous_p = true;
13123                   cp_parser_simulate_error (parser);
13124                 }
13125               return error_mark_node;
13126             }
13127         }
13128     }
13129   else
13130     {
13131       /* Try a template-id.  */
13132       decl = cp_parser_template_id (parser, template_keyword_p,
13133                                     check_dependency_p,
13134                                     is_declaration);
13135       if (decl == error_mark_node)
13136         return error_mark_node;
13137     }
13138
13139   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13140
13141   /* If this is a typename, create a TYPENAME_TYPE.  */
13142   if (typename_p && decl != error_mark_node)
13143     {
13144       decl = make_typename_type (scope, decl, typename_type,
13145                                  /*complain=*/tf_error);
13146       if (decl != error_mark_node)
13147         decl = TYPE_NAME (decl);
13148     }
13149
13150   /* Check to see that it is really the name of a class.  */
13151   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13152       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13153       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13154     /* Situations like this:
13155
13156          template <typename T> struct A {
13157            typename T::template X<int>::I i;
13158          };
13159
13160        are problematic.  Is `T::template X<int>' a class-name?  The
13161        standard does not seem to be definitive, but there is no other
13162        valid interpretation of the following `::'.  Therefore, those
13163        names are considered class-names.  */
13164     {
13165       decl = make_typename_type (scope, decl, tag_type, tf_error);
13166       if (decl != error_mark_node)
13167         decl = TYPE_NAME (decl);
13168     }
13169   else if (TREE_CODE (decl) != TYPE_DECL
13170            || TREE_TYPE (decl) == error_mark_node
13171            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13172     decl = error_mark_node;
13173
13174   if (decl == error_mark_node)
13175     cp_parser_error (parser, "expected class-name");
13176
13177   return decl;
13178 }
13179
13180 /* Parse a class-specifier.
13181
13182    class-specifier:
13183      class-head { member-specification [opt] }
13184
13185    Returns the TREE_TYPE representing the class.  */
13186
13187 static tree
13188 cp_parser_class_specifier (cp_parser* parser)
13189 {
13190   cp_token *token;
13191   tree type;
13192   tree attributes = NULL_TREE;
13193   int has_trailing_semicolon;
13194   bool nested_name_specifier_p;
13195   unsigned saved_num_template_parameter_lists;
13196   bool saved_in_function_body;
13197   tree old_scope = NULL_TREE;
13198   tree scope = NULL_TREE;
13199   tree bases;
13200
13201   push_deferring_access_checks (dk_no_deferred);
13202
13203   /* Parse the class-head.  */
13204   type = cp_parser_class_head (parser,
13205                                &nested_name_specifier_p,
13206                                &attributes,
13207                                &bases);
13208   /* If the class-head was a semantic disaster, skip the entire body
13209      of the class.  */
13210   if (!type)
13211     {
13212       cp_parser_skip_to_end_of_block_or_statement (parser);
13213       pop_deferring_access_checks ();
13214       return error_mark_node;
13215     }
13216
13217   /* Look for the `{'.  */
13218   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13219     {
13220       pop_deferring_access_checks ();
13221       return error_mark_node;
13222     }
13223
13224   /* Process the base classes. If they're invalid, skip the 
13225      entire class body.  */
13226   if (!xref_basetypes (type, bases))
13227     {
13228       cp_parser_skip_to_closing_brace (parser);
13229
13230       /* Consuming the closing brace yields better error messages
13231          later on.  */
13232       cp_lexer_consume_token (parser->lexer);
13233       pop_deferring_access_checks ();
13234       return error_mark_node;
13235     }
13236
13237   /* Issue an error message if type-definitions are forbidden here.  */
13238   cp_parser_check_type_definition (parser);
13239   /* Remember that we are defining one more class.  */
13240   ++parser->num_classes_being_defined;
13241   /* Inside the class, surrounding template-parameter-lists do not
13242      apply.  */
13243   saved_num_template_parameter_lists
13244     = parser->num_template_parameter_lists;
13245   parser->num_template_parameter_lists = 0;
13246   /* We are not in a function body.  */
13247   saved_in_function_body = parser->in_function_body;
13248   parser->in_function_body = false;
13249
13250   /* Start the class.  */
13251   if (nested_name_specifier_p)
13252     {
13253       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13254       old_scope = push_inner_scope (scope);
13255     }
13256   type = begin_class_definition (type, attributes);
13257
13258   if (type == error_mark_node)
13259     /* If the type is erroneous, skip the entire body of the class.  */
13260     cp_parser_skip_to_closing_brace (parser);
13261   else
13262     /* Parse the member-specification.  */
13263     cp_parser_member_specification_opt (parser);
13264
13265   /* Look for the trailing `}'.  */
13266   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13267   /* We get better error messages by noticing a common problem: a
13268      missing trailing `;'.  */
13269   token = cp_lexer_peek_token (parser->lexer);
13270   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13271   /* Look for trailing attributes to apply to this class.  */
13272   if (cp_parser_allow_gnu_extensions_p (parser))
13273     attributes = cp_parser_attributes_opt (parser);
13274   if (type != error_mark_node)
13275     type = finish_struct (type, attributes);
13276   if (nested_name_specifier_p)
13277     pop_inner_scope (old_scope, scope);
13278   /* If this class is not itself within the scope of another class,
13279      then we need to parse the bodies of all of the queued function
13280      definitions.  Note that the queued functions defined in a class
13281      are not always processed immediately following the
13282      class-specifier for that class.  Consider:
13283
13284        struct A {
13285          struct B { void f() { sizeof (A); } };
13286        };
13287
13288      If `f' were processed before the processing of `A' were
13289      completed, there would be no way to compute the size of `A'.
13290      Note that the nesting we are interested in here is lexical --
13291      not the semantic nesting given by TYPE_CONTEXT.  In particular,
13292      for:
13293
13294        struct A { struct B; };
13295        struct A::B { void f() { } };
13296
13297      there is no need to delay the parsing of `A::B::f'.  */
13298   if (--parser->num_classes_being_defined == 0)
13299     {
13300       tree queue_entry;
13301       tree fn;
13302       tree class_type = NULL_TREE;
13303       tree pushed_scope = NULL_TREE;
13304
13305       /* In a first pass, parse default arguments to the functions.
13306          Then, in a second pass, parse the bodies of the functions.
13307          This two-phased approach handles cases like:
13308
13309             struct S {
13310               void f() { g(); }
13311               void g(int i = 3);
13312             };
13313
13314          */
13315       for (TREE_PURPOSE (parser->unparsed_functions_queues)
13316              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13317            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13318            TREE_PURPOSE (parser->unparsed_functions_queues)
13319              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13320         {
13321           fn = TREE_VALUE (queue_entry);
13322           /* If there are default arguments that have not yet been processed,
13323              take care of them now.  */
13324           if (class_type != TREE_PURPOSE (queue_entry))
13325             {
13326               if (pushed_scope)
13327                 pop_scope (pushed_scope);
13328               class_type = TREE_PURPOSE (queue_entry);
13329               pushed_scope = push_scope (class_type);
13330             }
13331           /* Make sure that any template parameters are in scope.  */
13332           maybe_begin_member_template_processing (fn);
13333           /* Parse the default argument expressions.  */
13334           cp_parser_late_parsing_default_args (parser, fn);
13335           /* Remove any template parameters from the symbol table.  */
13336           maybe_end_member_template_processing ();
13337         }
13338       if (pushed_scope)
13339         pop_scope (pushed_scope);
13340       /* Now parse the body of the functions.  */
13341       for (TREE_VALUE (parser->unparsed_functions_queues)
13342              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13343            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13344            TREE_VALUE (parser->unparsed_functions_queues)
13345              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13346         {
13347           /* Figure out which function we need to process.  */
13348           fn = TREE_VALUE (queue_entry);
13349           /* Parse the function.  */
13350           cp_parser_late_parsing_for_member (parser, fn);
13351         }
13352     }
13353
13354   /* Put back any saved access checks.  */
13355   pop_deferring_access_checks ();
13356
13357   /* Restore saved state.  */
13358   parser->in_function_body = saved_in_function_body;
13359   parser->num_template_parameter_lists
13360     = saved_num_template_parameter_lists;
13361
13362   return type;
13363 }
13364
13365 /* Parse a class-head.
13366
13367    class-head:
13368      class-key identifier [opt] base-clause [opt]
13369      class-key nested-name-specifier identifier base-clause [opt]
13370      class-key nested-name-specifier [opt] template-id
13371        base-clause [opt]
13372
13373    GNU Extensions:
13374      class-key attributes identifier [opt] base-clause [opt]
13375      class-key attributes nested-name-specifier identifier base-clause [opt]
13376      class-key attributes nested-name-specifier [opt] template-id
13377        base-clause [opt]
13378
13379    Returns the TYPE of the indicated class.  Sets
13380    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13381    involving a nested-name-specifier was used, and FALSE otherwise.
13382
13383    Returns error_mark_node if this is not a class-head.
13384
13385    Returns NULL_TREE if the class-head is syntactically valid, but
13386    semantically invalid in a way that means we should skip the entire
13387    body of the class.  */
13388
13389 static tree
13390 cp_parser_class_head (cp_parser* parser,
13391                       bool* nested_name_specifier_p,
13392                       tree *attributes_p,
13393                       tree *bases)
13394 {
13395   tree nested_name_specifier;
13396   enum tag_types class_key;
13397   tree id = NULL_TREE;
13398   tree type = NULL_TREE;
13399   tree attributes;
13400   bool template_id_p = false;
13401   bool qualified_p = false;
13402   bool invalid_nested_name_p = false;
13403   bool invalid_explicit_specialization_p = false;
13404   tree pushed_scope = NULL_TREE;
13405   unsigned num_templates;
13406
13407   /* Assume no nested-name-specifier will be present.  */
13408   *nested_name_specifier_p = false;
13409   /* Assume no template parameter lists will be used in defining the
13410      type.  */
13411   num_templates = 0;
13412
13413   /* Look for the class-key.  */
13414   class_key = cp_parser_class_key (parser);
13415   if (class_key == none_type)
13416     return error_mark_node;
13417
13418   /* Parse the attributes.  */
13419   attributes = cp_parser_attributes_opt (parser);
13420
13421   /* If the next token is `::', that is invalid -- but sometimes
13422      people do try to write:
13423
13424        struct ::S {};
13425
13426      Handle this gracefully by accepting the extra qualifier, and then
13427      issuing an error about it later if this really is a
13428      class-head.  If it turns out just to be an elaborated type
13429      specifier, remain silent.  */
13430   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13431     qualified_p = true;
13432
13433   push_deferring_access_checks (dk_no_check);
13434
13435   /* Determine the name of the class.  Begin by looking for an
13436      optional nested-name-specifier.  */
13437   nested_name_specifier
13438     = cp_parser_nested_name_specifier_opt (parser,
13439                                            /*typename_keyword_p=*/false,
13440                                            /*check_dependency_p=*/false,
13441                                            /*type_p=*/false,
13442                                            /*is_declaration=*/false);
13443   /* If there was a nested-name-specifier, then there *must* be an
13444      identifier.  */
13445   if (nested_name_specifier)
13446     {
13447       /* Although the grammar says `identifier', it really means
13448          `class-name' or `template-name'.  You are only allowed to
13449          define a class that has already been declared with this
13450          syntax.
13451
13452          The proposed resolution for Core Issue 180 says that wherever
13453          you see `class T::X' you should treat `X' as a type-name.
13454
13455          It is OK to define an inaccessible class; for example:
13456
13457            class A { class B; };
13458            class A::B {};
13459
13460          We do not know if we will see a class-name, or a
13461          template-name.  We look for a class-name first, in case the
13462          class-name is a template-id; if we looked for the
13463          template-name first we would stop after the template-name.  */
13464       cp_parser_parse_tentatively (parser);
13465       type = cp_parser_class_name (parser,
13466                                    /*typename_keyword_p=*/false,
13467                                    /*template_keyword_p=*/false,
13468                                    class_type,
13469                                    /*check_dependency_p=*/false,
13470                                    /*class_head_p=*/true,
13471                                    /*is_declaration=*/false);
13472       /* If that didn't work, ignore the nested-name-specifier.  */
13473       if (!cp_parser_parse_definitely (parser))
13474         {
13475           invalid_nested_name_p = true;
13476           id = cp_parser_identifier (parser);
13477           if (id == error_mark_node)
13478             id = NULL_TREE;
13479         }
13480       /* If we could not find a corresponding TYPE, treat this
13481          declaration like an unqualified declaration.  */
13482       if (type == error_mark_node)
13483         nested_name_specifier = NULL_TREE;
13484       /* Otherwise, count the number of templates used in TYPE and its
13485          containing scopes.  */
13486       else
13487         {
13488           tree scope;
13489
13490           for (scope = TREE_TYPE (type);
13491                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13492                scope = (TYPE_P (scope)
13493                         ? TYPE_CONTEXT (scope)
13494                         : DECL_CONTEXT (scope)))
13495             if (TYPE_P (scope)
13496                 && CLASS_TYPE_P (scope)
13497                 && CLASSTYPE_TEMPLATE_INFO (scope)
13498                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13499                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13500               ++num_templates;
13501         }
13502     }
13503   /* Otherwise, the identifier is optional.  */
13504   else
13505     {
13506       /* We don't know whether what comes next is a template-id,
13507          an identifier, or nothing at all.  */
13508       cp_parser_parse_tentatively (parser);
13509       /* Check for a template-id.  */
13510       id = cp_parser_template_id (parser,
13511                                   /*template_keyword_p=*/false,
13512                                   /*check_dependency_p=*/true,
13513                                   /*is_declaration=*/true);
13514       /* If that didn't work, it could still be an identifier.  */
13515       if (!cp_parser_parse_definitely (parser))
13516         {
13517           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13518             id = cp_parser_identifier (parser);
13519           else
13520             id = NULL_TREE;
13521         }
13522       else
13523         {
13524           template_id_p = true;
13525           ++num_templates;
13526         }
13527     }
13528
13529   pop_deferring_access_checks ();
13530
13531   if (id)
13532     cp_parser_check_for_invalid_template_id (parser, id);
13533
13534   /* If it's not a `:' or a `{' then we can't really be looking at a
13535      class-head, since a class-head only appears as part of a
13536      class-specifier.  We have to detect this situation before calling
13537      xref_tag, since that has irreversible side-effects.  */
13538   if (!cp_parser_next_token_starts_class_definition_p (parser))
13539     {
13540       cp_parser_error (parser, "expected %<{%> or %<:%>");
13541       return error_mark_node;
13542     }
13543
13544   /* At this point, we're going ahead with the class-specifier, even
13545      if some other problem occurs.  */
13546   cp_parser_commit_to_tentative_parse (parser);
13547   /* Issue the error about the overly-qualified name now.  */
13548   if (qualified_p)
13549     cp_parser_error (parser,
13550                      "global qualification of class name is invalid");
13551   else if (invalid_nested_name_p)
13552     cp_parser_error (parser,
13553                      "qualified name does not name a class");
13554   else if (nested_name_specifier)
13555     {
13556       tree scope;
13557
13558       /* Reject typedef-names in class heads.  */
13559       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13560         {
13561           error ("invalid class name in declaration of %qD", type);
13562           type = NULL_TREE;
13563           goto done;
13564         }
13565
13566       /* Figure out in what scope the declaration is being placed.  */
13567       scope = current_scope ();
13568       /* If that scope does not contain the scope in which the
13569          class was originally declared, the program is invalid.  */
13570       if (scope && !is_ancestor (scope, nested_name_specifier))
13571         {
13572           error ("declaration of %qD in %qD which does not enclose %qD",
13573                  type, scope, nested_name_specifier);
13574           type = NULL_TREE;
13575           goto done;
13576         }
13577       /* [dcl.meaning]
13578
13579          A declarator-id shall not be qualified exception of the
13580          definition of a ... nested class outside of its class
13581          ... [or] a the definition or explicit instantiation of a
13582          class member of a namespace outside of its namespace.  */
13583       if (scope == nested_name_specifier)
13584         {
13585           pedwarn ("extra qualification ignored");
13586           nested_name_specifier = NULL_TREE;
13587           num_templates = 0;
13588         }
13589     }
13590   /* An explicit-specialization must be preceded by "template <>".  If
13591      it is not, try to recover gracefully.  */
13592   if (at_namespace_scope_p ()
13593       && parser->num_template_parameter_lists == 0
13594       && template_id_p)
13595     {
13596       error ("an explicit specialization must be preceded by %<template <>%>");
13597       invalid_explicit_specialization_p = true;
13598       /* Take the same action that would have been taken by
13599          cp_parser_explicit_specialization.  */
13600       ++parser->num_template_parameter_lists;
13601       begin_specialization ();
13602     }
13603   /* There must be no "return" statements between this point and the
13604      end of this function; set "type "to the correct return value and
13605      use "goto done;" to return.  */
13606   /* Make sure that the right number of template parameters were
13607      present.  */
13608   if (!cp_parser_check_template_parameters (parser, num_templates))
13609     {
13610       /* If something went wrong, there is no point in even trying to
13611          process the class-definition.  */
13612       type = NULL_TREE;
13613       goto done;
13614     }
13615
13616   /* Look up the type.  */
13617   if (template_id_p)
13618     {
13619       type = TREE_TYPE (id);
13620       type = maybe_process_partial_specialization (type);
13621       if (nested_name_specifier)
13622         pushed_scope = push_scope (nested_name_specifier);
13623     }
13624   else if (nested_name_specifier)
13625     {
13626       tree class_type;
13627
13628       /* Given:
13629
13630             template <typename T> struct S { struct T };
13631             template <typename T> struct S<T>::T { };
13632
13633          we will get a TYPENAME_TYPE when processing the definition of
13634          `S::T'.  We need to resolve it to the actual type before we
13635          try to define it.  */
13636       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13637         {
13638           class_type = resolve_typename_type (TREE_TYPE (type),
13639                                               /*only_current_p=*/false);
13640           if (class_type != error_mark_node)
13641             type = TYPE_NAME (class_type);
13642           else
13643             {
13644               cp_parser_error (parser, "could not resolve typename type");
13645               type = error_mark_node;
13646             }
13647         }
13648
13649       maybe_process_partial_specialization (TREE_TYPE (type));
13650       class_type = current_class_type;
13651       /* Enter the scope indicated by the nested-name-specifier.  */
13652       pushed_scope = push_scope (nested_name_specifier);
13653       /* Get the canonical version of this type.  */
13654       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13655       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13656           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13657         {
13658           type = push_template_decl (type);
13659           if (type == error_mark_node)
13660             {
13661               type = NULL_TREE;
13662               goto done;
13663             }
13664         }
13665
13666       type = TREE_TYPE (type);
13667       *nested_name_specifier_p = true;
13668     }
13669   else      /* The name is not a nested name.  */
13670     {
13671       /* If the class was unnamed, create a dummy name.  */
13672       if (!id)
13673         id = make_anon_name ();
13674       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13675                        parser->num_template_parameter_lists);
13676     }
13677
13678   /* Indicate whether this class was declared as a `class' or as a
13679      `struct'.  */
13680   if (TREE_CODE (type) == RECORD_TYPE)
13681     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13682   cp_parser_check_class_key (class_key, type);
13683
13684   /* If this type was already complete, and we see another definition,
13685      that's an error.  */
13686   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13687     {
13688       error ("redefinition of %q#T", type);
13689       error ("previous definition of %q+#T", type);
13690       type = NULL_TREE;
13691       goto done;
13692     }
13693   else if (type == error_mark_node)
13694     type = NULL_TREE;
13695
13696   /* We will have entered the scope containing the class; the names of
13697      base classes should be looked up in that context.  For example:
13698
13699        struct A { struct B {}; struct C; };
13700        struct A::C : B {};
13701
13702      is valid.  */
13703   *bases = NULL_TREE;
13704
13705   /* Get the list of base-classes, if there is one.  */
13706   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13707     *bases = cp_parser_base_clause (parser);
13708
13709  done:
13710   /* Leave the scope given by the nested-name-specifier.  We will
13711      enter the class scope itself while processing the members.  */
13712   if (pushed_scope)
13713     pop_scope (pushed_scope);
13714
13715   if (invalid_explicit_specialization_p)
13716     {
13717       end_specialization ();
13718       --parser->num_template_parameter_lists;
13719     }
13720   *attributes_p = attributes;
13721   return type;
13722 }
13723
13724 /* Parse a class-key.
13725
13726    class-key:
13727      class
13728      struct
13729      union
13730
13731    Returns the kind of class-key specified, or none_type to indicate
13732    error.  */
13733
13734 static enum tag_types
13735 cp_parser_class_key (cp_parser* parser)
13736 {
13737   cp_token *token;
13738   enum tag_types tag_type;
13739
13740   /* Look for the class-key.  */
13741   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13742   if (!token)
13743     return none_type;
13744
13745   /* Check to see if the TOKEN is a class-key.  */
13746   tag_type = cp_parser_token_is_class_key (token);
13747   if (!tag_type)
13748     cp_parser_error (parser, "expected class-key");
13749   return tag_type;
13750 }
13751
13752 /* Parse an (optional) member-specification.
13753
13754    member-specification:
13755      member-declaration member-specification [opt]
13756      access-specifier : member-specification [opt]  */
13757
13758 static void
13759 cp_parser_member_specification_opt (cp_parser* parser)
13760 {
13761   while (true)
13762     {
13763       cp_token *token;
13764       enum rid keyword;
13765
13766       /* Peek at the next token.  */
13767       token = cp_lexer_peek_token (parser->lexer);
13768       /* If it's a `}', or EOF then we've seen all the members.  */
13769       if (token->type == CPP_CLOSE_BRACE
13770           || token->type == CPP_EOF
13771           || token->type == CPP_PRAGMA_EOL)
13772         break;
13773
13774       /* See if this token is a keyword.  */
13775       keyword = token->keyword;
13776       switch (keyword)
13777         {
13778         case RID_PUBLIC:
13779         case RID_PROTECTED:
13780         case RID_PRIVATE:
13781           /* Consume the access-specifier.  */
13782           cp_lexer_consume_token (parser->lexer);
13783           /* Remember which access-specifier is active.  */
13784           current_access_specifier = token->u.value;
13785           /* Look for the `:'.  */
13786           cp_parser_require (parser, CPP_COLON, "`:'");
13787           break;
13788
13789         default:
13790           /* Accept #pragmas at class scope.  */
13791           if (token->type == CPP_PRAGMA)
13792             {
13793               cp_parser_pragma (parser, pragma_external);
13794               break;
13795             }
13796
13797           /* Otherwise, the next construction must be a
13798              member-declaration.  */
13799           cp_parser_member_declaration (parser);
13800         }
13801     }
13802 }
13803
13804 /* Parse a member-declaration.
13805
13806    member-declaration:
13807      decl-specifier-seq [opt] member-declarator-list [opt] ;
13808      function-definition ; [opt]
13809      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13810      using-declaration
13811      template-declaration
13812
13813    member-declarator-list:
13814      member-declarator
13815      member-declarator-list , member-declarator
13816
13817    member-declarator:
13818      declarator pure-specifier [opt]
13819      declarator constant-initializer [opt]
13820      identifier [opt] : constant-expression
13821
13822    GNU Extensions:
13823
13824    member-declaration:
13825      __extension__ member-declaration
13826
13827    member-declarator:
13828      declarator attributes [opt] pure-specifier [opt]
13829      declarator attributes [opt] constant-initializer [opt]
13830      identifier [opt] attributes [opt] : constant-expression  
13831
13832    C++0x Extensions:
13833
13834    member-declaration:
13835      static_assert-declaration  */
13836
13837 static void
13838 cp_parser_member_declaration (cp_parser* parser)
13839 {
13840   cp_decl_specifier_seq decl_specifiers;
13841   tree prefix_attributes;
13842   tree decl;
13843   int declares_class_or_enum;
13844   bool friend_p;
13845   cp_token *token;
13846   int saved_pedantic;
13847
13848   /* Check for the `__extension__' keyword.  */
13849   if (cp_parser_extension_opt (parser, &saved_pedantic))
13850     {
13851       /* Recurse.  */
13852       cp_parser_member_declaration (parser);
13853       /* Restore the old value of the PEDANTIC flag.  */
13854       pedantic = saved_pedantic;
13855
13856       return;
13857     }
13858
13859   /* Check for a template-declaration.  */
13860   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13861     {
13862       /* An explicit specialization here is an error condition, and we
13863          expect the specialization handler to detect and report this.  */
13864       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13865           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13866         cp_parser_explicit_specialization (parser);
13867       else
13868         cp_parser_template_declaration (parser, /*member_p=*/true);
13869
13870       return;
13871     }
13872
13873   /* Check for a using-declaration.  */
13874   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13875     {
13876       /* Parse the using-declaration.  */
13877       cp_parser_using_declaration (parser,
13878                                    /*access_declaration_p=*/false);
13879       return;
13880     }
13881
13882   /* Check for @defs.  */
13883   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13884     {
13885       tree ivar, member;
13886       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13887       ivar = ivar_chains;
13888       while (ivar)
13889         {
13890           member = ivar;
13891           ivar = TREE_CHAIN (member);
13892           TREE_CHAIN (member) = NULL_TREE;
13893           finish_member_declaration (member);
13894         }
13895       return;
13896     }
13897
13898   /* If the next token is `static_assert' we have a static assertion.  */
13899   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
13900     {
13901       cp_parser_static_assert (parser, /*member_p=*/true);
13902       return;
13903     }
13904
13905   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13906     return;
13907
13908   /* Parse the decl-specifier-seq.  */
13909   cp_parser_decl_specifier_seq (parser,
13910                                 CP_PARSER_FLAGS_OPTIONAL,
13911                                 &decl_specifiers,
13912                                 &declares_class_or_enum);
13913   prefix_attributes = decl_specifiers.attributes;
13914   decl_specifiers.attributes = NULL_TREE;
13915   /* Check for an invalid type-name.  */
13916   if (!decl_specifiers.type
13917       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13918     return;
13919   /* If there is no declarator, then the decl-specifier-seq should
13920      specify a type.  */
13921   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13922     {
13923       /* If there was no decl-specifier-seq, and the next token is a
13924          `;', then we have something like:
13925
13926            struct S { ; };
13927
13928          [class.mem]
13929
13930          Each member-declaration shall declare at least one member
13931          name of the class.  */
13932       if (!decl_specifiers.any_specifiers_p)
13933         {
13934           cp_token *token = cp_lexer_peek_token (parser->lexer);
13935           if (pedantic && !token->in_system_header)
13936             pedwarn ("%Hextra %<;%>", &token->location);
13937         }
13938       else
13939         {
13940           tree type;
13941
13942           /* See if this declaration is a friend.  */
13943           friend_p = cp_parser_friend_p (&decl_specifiers);
13944           /* If there were decl-specifiers, check to see if there was
13945              a class-declaration.  */
13946           type = check_tag_decl (&decl_specifiers);
13947           /* Nested classes have already been added to the class, but
13948              a `friend' needs to be explicitly registered.  */
13949           if (friend_p)
13950             {
13951               /* If the `friend' keyword was present, the friend must
13952                  be introduced with a class-key.  */
13953                if (!declares_class_or_enum)
13954                  error ("a class-key must be used when declaring a friend");
13955                /* In this case:
13956
13957                     template <typename T> struct A {
13958                       friend struct A<T>::B;
13959                     };
13960
13961                   A<T>::B will be represented by a TYPENAME_TYPE, and
13962                   therefore not recognized by check_tag_decl.  */
13963                if (!type
13964                    && decl_specifiers.type
13965                    && TYPE_P (decl_specifiers.type))
13966                  type = decl_specifiers.type;
13967                if (!type || !TYPE_P (type))
13968                  error ("friend declaration does not name a class or "
13969                         "function");
13970                else
13971                  make_friend_class (current_class_type, type,
13972                                     /*complain=*/true);
13973             }
13974           /* If there is no TYPE, an error message will already have
13975              been issued.  */
13976           else if (!type || type == error_mark_node)
13977             ;
13978           /* An anonymous aggregate has to be handled specially; such
13979              a declaration really declares a data member (with a
13980              particular type), as opposed to a nested class.  */
13981           else if (ANON_AGGR_TYPE_P (type))
13982             {
13983               /* Remove constructors and such from TYPE, now that we
13984                  know it is an anonymous aggregate.  */
13985               fixup_anonymous_aggr (type);
13986               /* And make the corresponding data member.  */
13987               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13988               /* Add it to the class.  */
13989               finish_member_declaration (decl);
13990             }
13991           else
13992             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13993         }
13994     }
13995   else
13996     {
13997       /* See if these declarations will be friends.  */
13998       friend_p = cp_parser_friend_p (&decl_specifiers);
13999
14000       /* Keep going until we hit the `;' at the end of the
14001          declaration.  */
14002       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14003         {
14004           tree attributes = NULL_TREE;
14005           tree first_attribute;
14006
14007           /* Peek at the next token.  */
14008           token = cp_lexer_peek_token (parser->lexer);
14009
14010           /* Check for a bitfield declaration.  */
14011           if (token->type == CPP_COLON
14012               || (token->type == CPP_NAME
14013                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14014                   == CPP_COLON))
14015             {
14016               tree identifier;
14017               tree width;
14018
14019               /* Get the name of the bitfield.  Note that we cannot just
14020                  check TOKEN here because it may have been invalidated by
14021                  the call to cp_lexer_peek_nth_token above.  */
14022               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14023                 identifier = cp_parser_identifier (parser);
14024               else
14025                 identifier = NULL_TREE;
14026
14027               /* Consume the `:' token.  */
14028               cp_lexer_consume_token (parser->lexer);
14029               /* Get the width of the bitfield.  */
14030               width
14031                 = cp_parser_constant_expression (parser,
14032                                                  /*allow_non_constant=*/false,
14033                                                  NULL);
14034
14035               /* Look for attributes that apply to the bitfield.  */
14036               attributes = cp_parser_attributes_opt (parser);
14037               /* Remember which attributes are prefix attributes and
14038                  which are not.  */
14039               first_attribute = attributes;
14040               /* Combine the attributes.  */
14041               attributes = chainon (prefix_attributes, attributes);
14042
14043               /* Create the bitfield declaration.  */
14044               decl = grokbitfield (identifier
14045                                    ? make_id_declarator (NULL_TREE,
14046                                                          identifier,
14047                                                          sfk_none)
14048                                    : NULL,
14049                                    &decl_specifiers,
14050                                    width);
14051               /* Apply the attributes.  */
14052               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14053             }
14054           else
14055             {
14056               cp_declarator *declarator;
14057               tree initializer;
14058               tree asm_specification;
14059               int ctor_dtor_or_conv_p;
14060
14061               /* Parse the declarator.  */
14062               declarator
14063                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14064                                         &ctor_dtor_or_conv_p,
14065                                         /*parenthesized_p=*/NULL,
14066                                         /*member_p=*/true);
14067
14068               /* If something went wrong parsing the declarator, make sure
14069                  that we at least consume some tokens.  */
14070               if (declarator == cp_error_declarator)
14071                 {
14072                   /* Skip to the end of the statement.  */
14073                   cp_parser_skip_to_end_of_statement (parser);
14074                   /* If the next token is not a semicolon, that is
14075                      probably because we just skipped over the body of
14076                      a function.  So, we consume a semicolon if
14077                      present, but do not issue an error message if it
14078                      is not present.  */
14079                   if (cp_lexer_next_token_is (parser->lexer,
14080                                               CPP_SEMICOLON))
14081                     cp_lexer_consume_token (parser->lexer);
14082                   return;
14083                 }
14084
14085               if (declares_class_or_enum & 2)
14086                 cp_parser_check_for_definition_in_return_type
14087                   (declarator, decl_specifiers.type);
14088
14089               /* Look for an asm-specification.  */
14090               asm_specification = cp_parser_asm_specification_opt (parser);
14091               /* Look for attributes that apply to the declaration.  */
14092               attributes = cp_parser_attributes_opt (parser);
14093               /* Remember which attributes are prefix attributes and
14094                  which are not.  */
14095               first_attribute = attributes;
14096               /* Combine the attributes.  */
14097               attributes = chainon (prefix_attributes, attributes);
14098
14099               /* If it's an `=', then we have a constant-initializer or a
14100                  pure-specifier.  It is not correct to parse the
14101                  initializer before registering the member declaration
14102                  since the member declaration should be in scope while
14103                  its initializer is processed.  However, the rest of the
14104                  front end does not yet provide an interface that allows
14105                  us to handle this correctly.  */
14106               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14107                 {
14108                   /* In [class.mem]:
14109
14110                      A pure-specifier shall be used only in the declaration of
14111                      a virtual function.
14112
14113                      A member-declarator can contain a constant-initializer
14114                      only if it declares a static member of integral or
14115                      enumeration type.
14116
14117                      Therefore, if the DECLARATOR is for a function, we look
14118                      for a pure-specifier; otherwise, we look for a
14119                      constant-initializer.  When we call `grokfield', it will
14120                      perform more stringent semantics checks.  */
14121                   if (function_declarator_p (declarator))
14122                     initializer = cp_parser_pure_specifier (parser);
14123                   else
14124                     /* Parse the initializer.  */
14125                     initializer = cp_parser_constant_initializer (parser);
14126                 }
14127               /* Otherwise, there is no initializer.  */
14128               else
14129                 initializer = NULL_TREE;
14130
14131               /* See if we are probably looking at a function
14132                  definition.  We are certainly not looking at a
14133                  member-declarator.  Calling `grokfield' has
14134                  side-effects, so we must not do it unless we are sure
14135                  that we are looking at a member-declarator.  */
14136               if (cp_parser_token_starts_function_definition_p
14137                   (cp_lexer_peek_token (parser->lexer)))
14138                 {
14139                   /* The grammar does not allow a pure-specifier to be
14140                      used when a member function is defined.  (It is
14141                      possible that this fact is an oversight in the
14142                      standard, since a pure function may be defined
14143                      outside of the class-specifier.  */
14144                   if (initializer)
14145                     error ("pure-specifier on function-definition");
14146                   decl = cp_parser_save_member_function_body (parser,
14147                                                               &decl_specifiers,
14148                                                               declarator,
14149                                                               attributes);
14150                   /* If the member was not a friend, declare it here.  */
14151                   if (!friend_p)
14152                     finish_member_declaration (decl);
14153                   /* Peek at the next token.  */
14154                   token = cp_lexer_peek_token (parser->lexer);
14155                   /* If the next token is a semicolon, consume it.  */
14156                   if (token->type == CPP_SEMICOLON)
14157                     cp_lexer_consume_token (parser->lexer);
14158                   return;
14159                 }
14160               else
14161                 /* Create the declaration.  */
14162                 decl = grokfield (declarator, &decl_specifiers,
14163                                   initializer, /*init_const_expr_p=*/true,
14164                                   asm_specification,
14165                                   attributes);
14166             }
14167
14168           /* Reset PREFIX_ATTRIBUTES.  */
14169           while (attributes && TREE_CHAIN (attributes) != first_attribute)
14170             attributes = TREE_CHAIN (attributes);
14171           if (attributes)
14172             TREE_CHAIN (attributes) = NULL_TREE;
14173
14174           /* If there is any qualification still in effect, clear it
14175              now; we will be starting fresh with the next declarator.  */
14176           parser->scope = NULL_TREE;
14177           parser->qualifying_scope = NULL_TREE;
14178           parser->object_scope = NULL_TREE;
14179           /* If it's a `,', then there are more declarators.  */
14180           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14181             cp_lexer_consume_token (parser->lexer);
14182           /* If the next token isn't a `;', then we have a parse error.  */
14183           else if (cp_lexer_next_token_is_not (parser->lexer,
14184                                                CPP_SEMICOLON))
14185             {
14186               cp_parser_error (parser, "expected %<;%>");
14187               /* Skip tokens until we find a `;'.  */
14188               cp_parser_skip_to_end_of_statement (parser);
14189
14190               break;
14191             }
14192
14193           if (decl)
14194             {
14195               /* Add DECL to the list of members.  */
14196               if (!friend_p)
14197                 finish_member_declaration (decl);
14198
14199               if (TREE_CODE (decl) == FUNCTION_DECL)
14200                 cp_parser_save_default_args (parser, decl);
14201             }
14202         }
14203     }
14204
14205   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14206 }
14207
14208 /* Parse a pure-specifier.
14209
14210    pure-specifier:
14211      = 0
14212
14213    Returns INTEGER_ZERO_NODE if a pure specifier is found.
14214    Otherwise, ERROR_MARK_NODE is returned.  */
14215
14216 static tree
14217 cp_parser_pure_specifier (cp_parser* parser)
14218 {
14219   cp_token *token;
14220
14221   /* Look for the `=' token.  */
14222   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14223     return error_mark_node;
14224   /* Look for the `0' token.  */
14225   token = cp_lexer_consume_token (parser->lexer);
14226   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
14227   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14228     {
14229       cp_parser_error (parser,
14230                        "invalid pure specifier (only `= 0' is allowed)");
14231       cp_parser_skip_to_end_of_statement (parser);
14232       return error_mark_node;
14233     }
14234   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14235     {
14236       error ("templates may not be %<virtual%>");
14237       return error_mark_node;
14238     }
14239
14240   return integer_zero_node;
14241 }
14242
14243 /* Parse a constant-initializer.
14244
14245    constant-initializer:
14246      = constant-expression
14247
14248    Returns a representation of the constant-expression.  */
14249
14250 static tree
14251 cp_parser_constant_initializer (cp_parser* parser)
14252 {
14253   /* Look for the `=' token.  */
14254   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14255     return error_mark_node;
14256
14257   /* It is invalid to write:
14258
14259        struct S { static const int i = { 7 }; };
14260
14261      */
14262   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14263     {
14264       cp_parser_error (parser,
14265                        "a brace-enclosed initializer is not allowed here");
14266       /* Consume the opening brace.  */
14267       cp_lexer_consume_token (parser->lexer);
14268       /* Skip the initializer.  */
14269       cp_parser_skip_to_closing_brace (parser);
14270       /* Look for the trailing `}'.  */
14271       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14272
14273       return error_mark_node;
14274     }
14275
14276   return cp_parser_constant_expression (parser,
14277                                         /*allow_non_constant=*/false,
14278                                         NULL);
14279 }
14280
14281 /* Derived classes [gram.class.derived] */
14282
14283 /* Parse a base-clause.
14284
14285    base-clause:
14286      : base-specifier-list
14287
14288    base-specifier-list:
14289      base-specifier
14290      base-specifier-list , base-specifier
14291
14292    Returns a TREE_LIST representing the base-classes, in the order in
14293    which they were declared.  The representation of each node is as
14294    described by cp_parser_base_specifier.
14295
14296    In the case that no bases are specified, this function will return
14297    NULL_TREE, not ERROR_MARK_NODE.  */
14298
14299 static tree
14300 cp_parser_base_clause (cp_parser* parser)
14301 {
14302   tree bases = NULL_TREE;
14303
14304   /* Look for the `:' that begins the list.  */
14305   cp_parser_require (parser, CPP_COLON, "`:'");
14306
14307   /* Scan the base-specifier-list.  */
14308   while (true)
14309     {
14310       cp_token *token;
14311       tree base;
14312
14313       /* Look for the base-specifier.  */
14314       base = cp_parser_base_specifier (parser);
14315       /* Add BASE to the front of the list.  */
14316       if (base != error_mark_node)
14317         {
14318           TREE_CHAIN (base) = bases;
14319           bases = base;
14320         }
14321       /* Peek at the next token.  */
14322       token = cp_lexer_peek_token (parser->lexer);
14323       /* If it's not a comma, then the list is complete.  */
14324       if (token->type != CPP_COMMA)
14325         break;
14326       /* Consume the `,'.  */
14327       cp_lexer_consume_token (parser->lexer);
14328     }
14329
14330   /* PARSER->SCOPE may still be non-NULL at this point, if the last
14331      base class had a qualified name.  However, the next name that
14332      appears is certainly not qualified.  */
14333   parser->scope = NULL_TREE;
14334   parser->qualifying_scope = NULL_TREE;
14335   parser->object_scope = NULL_TREE;
14336
14337   return nreverse (bases);
14338 }
14339
14340 /* Parse a base-specifier.
14341
14342    base-specifier:
14343      :: [opt] nested-name-specifier [opt] class-name
14344      virtual access-specifier [opt] :: [opt] nested-name-specifier
14345        [opt] class-name
14346      access-specifier virtual [opt] :: [opt] nested-name-specifier
14347        [opt] class-name
14348
14349    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14350    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14351    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14352    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14353
14354 static tree
14355 cp_parser_base_specifier (cp_parser* parser)
14356 {
14357   cp_token *token;
14358   bool done = false;
14359   bool virtual_p = false;
14360   bool duplicate_virtual_error_issued_p = false;
14361   bool duplicate_access_error_issued_p = false;
14362   bool class_scope_p, template_p;
14363   tree access = access_default_node;
14364   tree type;
14365
14366   /* Process the optional `virtual' and `access-specifier'.  */
14367   while (!done)
14368     {
14369       /* Peek at the next token.  */
14370       token = cp_lexer_peek_token (parser->lexer);
14371       /* Process `virtual'.  */
14372       switch (token->keyword)
14373         {
14374         case RID_VIRTUAL:
14375           /* If `virtual' appears more than once, issue an error.  */
14376           if (virtual_p && !duplicate_virtual_error_issued_p)
14377             {
14378               cp_parser_error (parser,
14379                                "%<virtual%> specified more than once in base-specified");
14380               duplicate_virtual_error_issued_p = true;
14381             }
14382
14383           virtual_p = true;
14384
14385           /* Consume the `virtual' token.  */
14386           cp_lexer_consume_token (parser->lexer);
14387
14388           break;
14389
14390         case RID_PUBLIC:
14391         case RID_PROTECTED:
14392         case RID_PRIVATE:
14393           /* If more than one access specifier appears, issue an
14394              error.  */
14395           if (access != access_default_node
14396               && !duplicate_access_error_issued_p)
14397             {
14398               cp_parser_error (parser,
14399                                "more than one access specifier in base-specified");
14400               duplicate_access_error_issued_p = true;
14401             }
14402
14403           access = ridpointers[(int) token->keyword];
14404
14405           /* Consume the access-specifier.  */
14406           cp_lexer_consume_token (parser->lexer);
14407
14408           break;
14409
14410         default:
14411           done = true;
14412           break;
14413         }
14414     }
14415   /* It is not uncommon to see programs mechanically, erroneously, use
14416      the 'typename' keyword to denote (dependent) qualified types
14417      as base classes.  */
14418   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14419     {
14420       if (!processing_template_decl)
14421         error ("keyword %<typename%> not allowed outside of templates");
14422       else
14423         error ("keyword %<typename%> not allowed in this context "
14424                "(the base class is implicitly a type)");
14425       cp_lexer_consume_token (parser->lexer);
14426     }
14427
14428   /* Look for the optional `::' operator.  */
14429   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14430   /* Look for the nested-name-specifier.  The simplest way to
14431      implement:
14432
14433        [temp.res]
14434
14435        The keyword `typename' is not permitted in a base-specifier or
14436        mem-initializer; in these contexts a qualified name that
14437        depends on a template-parameter is implicitly assumed to be a
14438        type name.
14439
14440      is to pretend that we have seen the `typename' keyword at this
14441      point.  */
14442   cp_parser_nested_name_specifier_opt (parser,
14443                                        /*typename_keyword_p=*/true,
14444                                        /*check_dependency_p=*/true,
14445                                        typename_type,
14446                                        /*is_declaration=*/true);
14447   /* If the base class is given by a qualified name, assume that names
14448      we see are type names or templates, as appropriate.  */
14449   class_scope_p = (parser->scope && TYPE_P (parser->scope));
14450   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14451
14452   /* Finally, look for the class-name.  */
14453   type = cp_parser_class_name (parser,
14454                                class_scope_p,
14455                                template_p,
14456                                typename_type,
14457                                /*check_dependency_p=*/true,
14458                                /*class_head_p=*/false,
14459                                /*is_declaration=*/true);
14460
14461   if (type == error_mark_node)
14462     return error_mark_node;
14463
14464   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14465 }
14466
14467 /* Exception handling [gram.exception] */
14468
14469 /* Parse an (optional) exception-specification.
14470
14471    exception-specification:
14472      throw ( type-id-list [opt] )
14473
14474    Returns a TREE_LIST representing the exception-specification.  The
14475    TREE_VALUE of each node is a type.  */
14476
14477 static tree
14478 cp_parser_exception_specification_opt (cp_parser* parser)
14479 {
14480   cp_token *token;
14481   tree type_id_list;
14482
14483   /* Peek at the next token.  */
14484   token = cp_lexer_peek_token (parser->lexer);
14485   /* If it's not `throw', then there's no exception-specification.  */
14486   if (!cp_parser_is_keyword (token, RID_THROW))
14487     return NULL_TREE;
14488
14489   /* Consume the `throw'.  */
14490   cp_lexer_consume_token (parser->lexer);
14491
14492   /* Look for the `('.  */
14493   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14494
14495   /* Peek at the next token.  */
14496   token = cp_lexer_peek_token (parser->lexer);
14497   /* If it's not a `)', then there is a type-id-list.  */
14498   if (token->type != CPP_CLOSE_PAREN)
14499     {
14500       const char *saved_message;
14501
14502       /* Types may not be defined in an exception-specification.  */
14503       saved_message = parser->type_definition_forbidden_message;
14504       parser->type_definition_forbidden_message
14505         = "types may not be defined in an exception-specification";
14506       /* Parse the type-id-list.  */
14507       type_id_list = cp_parser_type_id_list (parser);
14508       /* Restore the saved message.  */
14509       parser->type_definition_forbidden_message = saved_message;
14510     }
14511   else
14512     type_id_list = empty_except_spec;
14513
14514   /* Look for the `)'.  */
14515   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14516
14517   return type_id_list;
14518 }
14519
14520 /* Parse an (optional) type-id-list.
14521
14522    type-id-list:
14523      type-id
14524      type-id-list , type-id
14525
14526    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14527    in the order that the types were presented.  */
14528
14529 static tree
14530 cp_parser_type_id_list (cp_parser* parser)
14531 {
14532   tree types = NULL_TREE;
14533
14534   while (true)
14535     {
14536       cp_token *token;
14537       tree type;
14538
14539       /* Get the next type-id.  */
14540       type = cp_parser_type_id (parser);
14541       /* Add it to the list.  */
14542       types = add_exception_specifier (types, type, /*complain=*/1);
14543       /* Peek at the next token.  */
14544       token = cp_lexer_peek_token (parser->lexer);
14545       /* If it is not a `,', we are done.  */
14546       if (token->type != CPP_COMMA)
14547         break;
14548       /* Consume the `,'.  */
14549       cp_lexer_consume_token (parser->lexer);
14550     }
14551
14552   return nreverse (types);
14553 }
14554
14555 /* Parse a try-block.
14556
14557    try-block:
14558      try compound-statement handler-seq  */
14559
14560 static tree
14561 cp_parser_try_block (cp_parser* parser)
14562 {
14563   tree try_block;
14564
14565   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14566   try_block = begin_try_block ();
14567   cp_parser_compound_statement (parser, NULL, true);
14568   finish_try_block (try_block);
14569   cp_parser_handler_seq (parser);
14570   finish_handler_sequence (try_block);
14571
14572   return try_block;
14573 }
14574
14575 /* Parse a function-try-block.
14576
14577    function-try-block:
14578      try ctor-initializer [opt] function-body handler-seq  */
14579
14580 static bool
14581 cp_parser_function_try_block (cp_parser* parser)
14582 {
14583   tree compound_stmt;
14584   tree try_block;
14585   bool ctor_initializer_p;
14586
14587   /* Look for the `try' keyword.  */
14588   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14589     return false;
14590   /* Let the rest of the front-end know where we are.  */
14591   try_block = begin_function_try_block (&compound_stmt);
14592   /* Parse the function-body.  */
14593   ctor_initializer_p
14594     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14595   /* We're done with the `try' part.  */
14596   finish_function_try_block (try_block);
14597   /* Parse the handlers.  */
14598   cp_parser_handler_seq (parser);
14599   /* We're done with the handlers.  */
14600   finish_function_handler_sequence (try_block, compound_stmt);
14601
14602   return ctor_initializer_p;
14603 }
14604
14605 /* Parse a handler-seq.
14606
14607    handler-seq:
14608      handler handler-seq [opt]  */
14609
14610 static void
14611 cp_parser_handler_seq (cp_parser* parser)
14612 {
14613   while (true)
14614     {
14615       cp_token *token;
14616
14617       /* Parse the handler.  */
14618       cp_parser_handler (parser);
14619       /* Peek at the next token.  */
14620       token = cp_lexer_peek_token (parser->lexer);
14621       /* If it's not `catch' then there are no more handlers.  */
14622       if (!cp_parser_is_keyword (token, RID_CATCH))
14623         break;
14624     }
14625 }
14626
14627 /* Parse a handler.
14628
14629    handler:
14630      catch ( exception-declaration ) compound-statement  */
14631
14632 static void
14633 cp_parser_handler (cp_parser* parser)
14634 {
14635   tree handler;
14636   tree declaration;
14637
14638   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14639   handler = begin_handler ();
14640   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14641   declaration = cp_parser_exception_declaration (parser);
14642   finish_handler_parms (declaration, handler);
14643   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14644   cp_parser_compound_statement (parser, NULL, false);
14645   finish_handler (handler);
14646 }
14647
14648 /* Parse an exception-declaration.
14649
14650    exception-declaration:
14651      type-specifier-seq declarator
14652      type-specifier-seq abstract-declarator
14653      type-specifier-seq
14654      ...
14655
14656    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14657    ellipsis variant is used.  */
14658
14659 static tree
14660 cp_parser_exception_declaration (cp_parser* parser)
14661 {
14662   cp_decl_specifier_seq type_specifiers;
14663   cp_declarator *declarator;
14664   const char *saved_message;
14665
14666   /* If it's an ellipsis, it's easy to handle.  */
14667   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14668     {
14669       /* Consume the `...' token.  */
14670       cp_lexer_consume_token (parser->lexer);
14671       return NULL_TREE;
14672     }
14673
14674   /* Types may not be defined in exception-declarations.  */
14675   saved_message = parser->type_definition_forbidden_message;
14676   parser->type_definition_forbidden_message
14677     = "types may not be defined in exception-declarations";
14678
14679   /* Parse the type-specifier-seq.  */
14680   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14681                                 &type_specifiers);
14682   /* If it's a `)', then there is no declarator.  */
14683   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14684     declarator = NULL;
14685   else
14686     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14687                                        /*ctor_dtor_or_conv_p=*/NULL,
14688                                        /*parenthesized_p=*/NULL,
14689                                        /*member_p=*/false);
14690
14691   /* Restore the saved message.  */
14692   parser->type_definition_forbidden_message = saved_message;
14693
14694   if (!type_specifiers.any_specifiers_p)
14695     return error_mark_node;
14696
14697   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14698 }
14699
14700 /* Parse a throw-expression.
14701
14702    throw-expression:
14703      throw assignment-expression [opt]
14704
14705    Returns a THROW_EXPR representing the throw-expression.  */
14706
14707 static tree
14708 cp_parser_throw_expression (cp_parser* parser)
14709 {
14710   tree expression;
14711   cp_token* token;
14712
14713   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14714   token = cp_lexer_peek_token (parser->lexer);
14715   /* Figure out whether or not there is an assignment-expression
14716      following the "throw" keyword.  */
14717   if (token->type == CPP_COMMA
14718       || token->type == CPP_SEMICOLON
14719       || token->type == CPP_CLOSE_PAREN
14720       || token->type == CPP_CLOSE_SQUARE
14721       || token->type == CPP_CLOSE_BRACE
14722       || token->type == CPP_COLON)
14723     expression = NULL_TREE;
14724   else
14725     expression = cp_parser_assignment_expression (parser,
14726                                                   /*cast_p=*/false);
14727
14728   return build_throw (expression);
14729 }
14730
14731 /* GNU Extensions */
14732
14733 /* Parse an (optional) asm-specification.
14734
14735    asm-specification:
14736      asm ( string-literal )
14737
14738    If the asm-specification is present, returns a STRING_CST
14739    corresponding to the string-literal.  Otherwise, returns
14740    NULL_TREE.  */
14741
14742 static tree
14743 cp_parser_asm_specification_opt (cp_parser* parser)
14744 {
14745   cp_token *token;
14746   tree asm_specification;
14747
14748   /* Peek at the next token.  */
14749   token = cp_lexer_peek_token (parser->lexer);
14750   /* If the next token isn't the `asm' keyword, then there's no
14751      asm-specification.  */
14752   if (!cp_parser_is_keyword (token, RID_ASM))
14753     return NULL_TREE;
14754
14755   /* Consume the `asm' token.  */
14756   cp_lexer_consume_token (parser->lexer);
14757   /* Look for the `('.  */
14758   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14759
14760   /* Look for the string-literal.  */
14761   asm_specification = cp_parser_string_literal (parser, false, false);
14762
14763   /* Look for the `)'.  */
14764   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14765
14766   return asm_specification;
14767 }
14768
14769 /* Parse an asm-operand-list.
14770
14771    asm-operand-list:
14772      asm-operand
14773      asm-operand-list , asm-operand
14774
14775    asm-operand:
14776      string-literal ( expression )
14777      [ string-literal ] string-literal ( expression )
14778
14779    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14780    each node is the expression.  The TREE_PURPOSE is itself a
14781    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14782    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14783    is a STRING_CST for the string literal before the parenthesis.  */
14784
14785 static tree
14786 cp_parser_asm_operand_list (cp_parser* parser)
14787 {
14788   tree asm_operands = NULL_TREE;
14789
14790   while (true)
14791     {
14792       tree string_literal;
14793       tree expression;
14794       tree name;
14795
14796       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14797         {
14798           /* Consume the `[' token.  */
14799           cp_lexer_consume_token (parser->lexer);
14800           /* Read the operand name.  */
14801           name = cp_parser_identifier (parser);
14802           if (name != error_mark_node)
14803             name = build_string (IDENTIFIER_LENGTH (name),
14804                                  IDENTIFIER_POINTER (name));
14805           /* Look for the closing `]'.  */
14806           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14807         }
14808       else
14809         name = NULL_TREE;
14810       /* Look for the string-literal.  */
14811       string_literal = cp_parser_string_literal (parser, false, false);
14812
14813       /* Look for the `('.  */
14814       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14815       /* Parse the expression.  */
14816       expression = cp_parser_expression (parser, /*cast_p=*/false);
14817       /* Look for the `)'.  */
14818       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14819
14820       /* Add this operand to the list.  */
14821       asm_operands = tree_cons (build_tree_list (name, string_literal),
14822                                 expression,
14823                                 asm_operands);
14824       /* If the next token is not a `,', there are no more
14825          operands.  */
14826       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14827         break;
14828       /* Consume the `,'.  */
14829       cp_lexer_consume_token (parser->lexer);
14830     }
14831
14832   return nreverse (asm_operands);
14833 }
14834
14835 /* Parse an asm-clobber-list.
14836
14837    asm-clobber-list:
14838      string-literal
14839      asm-clobber-list , string-literal
14840
14841    Returns a TREE_LIST, indicating the clobbers in the order that they
14842    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14843
14844 static tree
14845 cp_parser_asm_clobber_list (cp_parser* parser)
14846 {
14847   tree clobbers = NULL_TREE;
14848
14849   while (true)
14850     {
14851       tree string_literal;
14852
14853       /* Look for the string literal.  */
14854       string_literal = cp_parser_string_literal (parser, false, false);
14855       /* Add it to the list.  */
14856       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14857       /* If the next token is not a `,', then the list is
14858          complete.  */
14859       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14860         break;
14861       /* Consume the `,' token.  */
14862       cp_lexer_consume_token (parser->lexer);
14863     }
14864
14865   return clobbers;
14866 }
14867
14868 /* Parse an (optional) series of attributes.
14869
14870    attributes:
14871      attributes attribute
14872
14873    attribute:
14874      __attribute__ (( attribute-list [opt] ))
14875
14876    The return value is as for cp_parser_attribute_list.  */
14877
14878 static tree
14879 cp_parser_attributes_opt (cp_parser* parser)
14880 {
14881   tree attributes = NULL_TREE;
14882
14883   while (true)
14884     {
14885       cp_token *token;
14886       tree attribute_list;
14887
14888       /* Peek at the next token.  */
14889       token = cp_lexer_peek_token (parser->lexer);
14890       /* If it's not `__attribute__', then we're done.  */
14891       if (token->keyword != RID_ATTRIBUTE)
14892         break;
14893
14894       /* Consume the `__attribute__' keyword.  */
14895       cp_lexer_consume_token (parser->lexer);
14896       /* Look for the two `(' tokens.  */
14897       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14898       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14899
14900       /* Peek at the next token.  */
14901       token = cp_lexer_peek_token (parser->lexer);
14902       if (token->type != CPP_CLOSE_PAREN)
14903         /* Parse the attribute-list.  */
14904         attribute_list = cp_parser_attribute_list (parser);
14905       else
14906         /* If the next token is a `)', then there is no attribute
14907            list.  */
14908         attribute_list = NULL;
14909
14910       /* Look for the two `)' tokens.  */
14911       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14912       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14913
14914       /* Add these new attributes to the list.  */
14915       attributes = chainon (attributes, attribute_list);
14916     }
14917
14918   return attributes;
14919 }
14920
14921 /* Parse an attribute-list.
14922
14923    attribute-list:
14924      attribute
14925      attribute-list , attribute
14926
14927    attribute:
14928      identifier
14929      identifier ( identifier )
14930      identifier ( identifier , expression-list )
14931      identifier ( expression-list )
14932
14933    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14934    to an attribute.  The TREE_PURPOSE of each node is the identifier
14935    indicating which attribute is in use.  The TREE_VALUE represents
14936    the arguments, if any.  */
14937
14938 static tree
14939 cp_parser_attribute_list (cp_parser* parser)
14940 {
14941   tree attribute_list = NULL_TREE;
14942   bool save_translate_strings_p = parser->translate_strings_p;
14943
14944   parser->translate_strings_p = false;
14945   while (true)
14946     {
14947       cp_token *token;
14948       tree identifier;
14949       tree attribute;
14950
14951       /* Look for the identifier.  We also allow keywords here; for
14952          example `__attribute__ ((const))' is legal.  */
14953       token = cp_lexer_peek_token (parser->lexer);
14954       if (token->type == CPP_NAME
14955           || token->type == CPP_KEYWORD)
14956         {
14957           tree arguments = NULL_TREE;
14958
14959           /* Consume the token.  */
14960           token = cp_lexer_consume_token (parser->lexer);
14961
14962           /* Save away the identifier that indicates which attribute
14963              this is.  */
14964           identifier = token->u.value;
14965           attribute = build_tree_list (identifier, NULL_TREE);
14966
14967           /* Peek at the next token.  */
14968           token = cp_lexer_peek_token (parser->lexer);
14969           /* If it's an `(', then parse the attribute arguments.  */
14970           if (token->type == CPP_OPEN_PAREN)
14971             {
14972               arguments = cp_parser_parenthesized_expression_list
14973                           (parser, true, /*cast_p=*/false,
14974                            /*non_constant_p=*/NULL);
14975               /* Save the arguments away.  */
14976               TREE_VALUE (attribute) = arguments;
14977             }
14978
14979           if (arguments != error_mark_node)
14980             {
14981               /* Add this attribute to the list.  */
14982               TREE_CHAIN (attribute) = attribute_list;
14983               attribute_list = attribute;
14984             }
14985
14986           token = cp_lexer_peek_token (parser->lexer);
14987         }
14988       /* Now, look for more attributes.  If the next token isn't a
14989          `,', we're done.  */
14990       if (token->type != CPP_COMMA)
14991         break;
14992
14993       /* Consume the comma and keep going.  */
14994       cp_lexer_consume_token (parser->lexer);
14995     }
14996   parser->translate_strings_p = save_translate_strings_p;
14997
14998   /* We built up the list in reverse order.  */
14999   return nreverse (attribute_list);
15000 }
15001
15002 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
15003    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
15004    current value of the PEDANTIC flag, regardless of whether or not
15005    the `__extension__' keyword is present.  The caller is responsible
15006    for restoring the value of the PEDANTIC flag.  */
15007
15008 static bool
15009 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15010 {
15011   /* Save the old value of the PEDANTIC flag.  */
15012   *saved_pedantic = pedantic;
15013
15014   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15015     {
15016       /* Consume the `__extension__' token.  */
15017       cp_lexer_consume_token (parser->lexer);
15018       /* We're not being pedantic while the `__extension__' keyword is
15019          in effect.  */
15020       pedantic = 0;
15021
15022       return true;
15023     }
15024
15025   return false;
15026 }
15027
15028 /* Parse a label declaration.
15029
15030    label-declaration:
15031      __label__ label-declarator-seq ;
15032
15033    label-declarator-seq:
15034      identifier , label-declarator-seq
15035      identifier  */
15036
15037 static void
15038 cp_parser_label_declaration (cp_parser* parser)
15039 {
15040   /* Look for the `__label__' keyword.  */
15041   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15042
15043   while (true)
15044     {
15045       tree identifier;
15046
15047       /* Look for an identifier.  */
15048       identifier = cp_parser_identifier (parser);
15049       /* If we failed, stop.  */
15050       if (identifier == error_mark_node)
15051         break;
15052       /* Declare it as a label.  */
15053       finish_label_decl (identifier);
15054       /* If the next token is a `;', stop.  */
15055       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15056         break;
15057       /* Look for the `,' separating the label declarations.  */
15058       cp_parser_require (parser, CPP_COMMA, "`,'");
15059     }
15060
15061   /* Look for the final `;'.  */
15062   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15063 }
15064
15065 /* Support Functions */
15066
15067 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15068    NAME should have one of the representations used for an
15069    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15070    is returned.  If PARSER->SCOPE is a dependent type, then a
15071    SCOPE_REF is returned.
15072
15073    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15074    returned; the name was already resolved when the TEMPLATE_ID_EXPR
15075    was formed.  Abstractly, such entities should not be passed to this
15076    function, because they do not need to be looked up, but it is
15077    simpler to check for this special case here, rather than at the
15078    call-sites.
15079
15080    In cases not explicitly covered above, this function returns a
15081    DECL, OVERLOAD, or baselink representing the result of the lookup.
15082    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15083    is returned.
15084
15085    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15086    (e.g., "struct") that was used.  In that case bindings that do not
15087    refer to types are ignored.
15088
15089    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15090    ignored.
15091
15092    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15093    are ignored.
15094
15095    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15096    types.
15097
15098    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15099    TREE_LIST of candidates if name-lookup results in an ambiguity, and
15100    NULL_TREE otherwise.  */
15101
15102 static tree
15103 cp_parser_lookup_name (cp_parser *parser, tree name,
15104                        enum tag_types tag_type,
15105                        bool is_template,
15106                        bool is_namespace,
15107                        bool check_dependency,
15108                        tree *ambiguous_decls)
15109 {
15110   int flags = 0;
15111   tree decl;
15112   tree object_type = parser->context->object_type;
15113
15114   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15115     flags |= LOOKUP_COMPLAIN;
15116
15117   /* Assume that the lookup will be unambiguous.  */
15118   if (ambiguous_decls)
15119     *ambiguous_decls = NULL_TREE;
15120
15121   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15122      no longer valid.  Note that if we are parsing tentatively, and
15123      the parse fails, OBJECT_TYPE will be automatically restored.  */
15124   parser->context->object_type = NULL_TREE;
15125
15126   if (name == error_mark_node)
15127     return error_mark_node;
15128
15129   /* A template-id has already been resolved; there is no lookup to
15130      do.  */
15131   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15132     return name;
15133   if (BASELINK_P (name))
15134     {
15135       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15136                   == TEMPLATE_ID_EXPR);
15137       return name;
15138     }
15139
15140   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
15141      it should already have been checked to make sure that the name
15142      used matches the type being destroyed.  */
15143   if (TREE_CODE (name) == BIT_NOT_EXPR)
15144     {
15145       tree type;
15146
15147       /* Figure out to which type this destructor applies.  */
15148       if (parser->scope)
15149         type = parser->scope;
15150       else if (object_type)
15151         type = object_type;
15152       else
15153         type = current_class_type;
15154       /* If that's not a class type, there is no destructor.  */
15155       if (!type || !CLASS_TYPE_P (type))
15156         return error_mark_node;
15157       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15158         lazily_declare_fn (sfk_destructor, type);
15159       if (!CLASSTYPE_DESTRUCTORS (type))
15160           return error_mark_node;
15161       /* If it was a class type, return the destructor.  */
15162       return CLASSTYPE_DESTRUCTORS (type);
15163     }
15164
15165   /* By this point, the NAME should be an ordinary identifier.  If
15166      the id-expression was a qualified name, the qualifying scope is
15167      stored in PARSER->SCOPE at this point.  */
15168   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15169
15170   /* Perform the lookup.  */
15171   if (parser->scope)
15172     {
15173       bool dependent_p;
15174
15175       if (parser->scope == error_mark_node)
15176         return error_mark_node;
15177
15178       /* If the SCOPE is dependent, the lookup must be deferred until
15179          the template is instantiated -- unless we are explicitly
15180          looking up names in uninstantiated templates.  Even then, we
15181          cannot look up the name if the scope is not a class type; it
15182          might, for example, be a template type parameter.  */
15183       dependent_p = (TYPE_P (parser->scope)
15184                      && !(parser->in_declarator_p
15185                           && currently_open_class (parser->scope))
15186                      && dependent_type_p (parser->scope));
15187       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15188            && dependent_p)
15189         {
15190           if (tag_type)
15191             {
15192               tree type;
15193
15194               /* The resolution to Core Issue 180 says that `struct
15195                  A::B' should be considered a type-name, even if `A'
15196                  is dependent.  */
15197               type = make_typename_type (parser->scope, name, tag_type,
15198                                          /*complain=*/tf_error);
15199               decl = TYPE_NAME (type);
15200             }
15201           else if (is_template
15202                    && (cp_parser_next_token_ends_template_argument_p (parser)
15203                        || cp_lexer_next_token_is (parser->lexer,
15204                                                   CPP_CLOSE_PAREN)))
15205             decl = make_unbound_class_template (parser->scope,
15206                                                 name, NULL_TREE,
15207                                                 /*complain=*/tf_error);
15208           else
15209             decl = build_qualified_name (/*type=*/NULL_TREE,
15210                                          parser->scope, name,
15211                                          is_template);
15212         }
15213       else
15214         {
15215           tree pushed_scope = NULL_TREE;
15216
15217           /* If PARSER->SCOPE is a dependent type, then it must be a
15218              class type, and we must not be checking dependencies;
15219              otherwise, we would have processed this lookup above.  So
15220              that PARSER->SCOPE is not considered a dependent base by
15221              lookup_member, we must enter the scope here.  */
15222           if (dependent_p)
15223             pushed_scope = push_scope (parser->scope);
15224           /* If the PARSER->SCOPE is a template specialization, it
15225              may be instantiated during name lookup.  In that case,
15226              errors may be issued.  Even if we rollback the current
15227              tentative parse, those errors are valid.  */
15228           decl = lookup_qualified_name (parser->scope, name,
15229                                         tag_type != none_type,
15230                                         /*complain=*/true);
15231           if (pushed_scope)
15232             pop_scope (pushed_scope);
15233         }
15234       parser->qualifying_scope = parser->scope;
15235       parser->object_scope = NULL_TREE;
15236     }
15237   else if (object_type)
15238     {
15239       tree object_decl = NULL_TREE;
15240       /* Look up the name in the scope of the OBJECT_TYPE, unless the
15241          OBJECT_TYPE is not a class.  */
15242       if (CLASS_TYPE_P (object_type))
15243         /* If the OBJECT_TYPE is a template specialization, it may
15244            be instantiated during name lookup.  In that case, errors
15245            may be issued.  Even if we rollback the current tentative
15246            parse, those errors are valid.  */
15247         object_decl = lookup_member (object_type,
15248                                      name,
15249                                      /*protect=*/0,
15250                                      tag_type != none_type);
15251       /* Look it up in the enclosing context, too.  */
15252       decl = lookup_name_real (name, tag_type != none_type,
15253                                /*nonclass=*/0,
15254                                /*block_p=*/true, is_namespace, flags);
15255       parser->object_scope = object_type;
15256       parser->qualifying_scope = NULL_TREE;
15257       if (object_decl)
15258         decl = object_decl;
15259     }
15260   else
15261     {
15262       decl = lookup_name_real (name, tag_type != none_type,
15263                                /*nonclass=*/0,
15264                                /*block_p=*/true, is_namespace, flags);
15265       parser->qualifying_scope = NULL_TREE;
15266       parser->object_scope = NULL_TREE;
15267     }
15268
15269   /* If the lookup failed, let our caller know.  */
15270   if (!decl || decl == error_mark_node)
15271     return error_mark_node;
15272
15273   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
15274   if (TREE_CODE (decl) == TREE_LIST)
15275     {
15276       if (ambiguous_decls)
15277         *ambiguous_decls = decl;
15278       /* The error message we have to print is too complicated for
15279          cp_parser_error, so we incorporate its actions directly.  */
15280       if (!cp_parser_simulate_error (parser))
15281         {
15282           error ("reference to %qD is ambiguous", name);
15283           print_candidates (decl);
15284         }
15285       return error_mark_node;
15286     }
15287
15288   gcc_assert (DECL_P (decl)
15289               || TREE_CODE (decl) == OVERLOAD
15290               || TREE_CODE (decl) == SCOPE_REF
15291               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15292               || BASELINK_P (decl));
15293
15294   /* If we have resolved the name of a member declaration, check to
15295      see if the declaration is accessible.  When the name resolves to
15296      set of overloaded functions, accessibility is checked when
15297      overload resolution is done.
15298
15299      During an explicit instantiation, access is not checked at all,
15300      as per [temp.explicit].  */
15301   if (DECL_P (decl))
15302     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15303
15304   return decl;
15305 }
15306
15307 /* Like cp_parser_lookup_name, but for use in the typical case where
15308    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15309    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
15310
15311 static tree
15312 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15313 {
15314   return cp_parser_lookup_name (parser, name,
15315                                 none_type,
15316                                 /*is_template=*/false,
15317                                 /*is_namespace=*/false,
15318                                 /*check_dependency=*/true,
15319                                 /*ambiguous_decls=*/NULL);
15320 }
15321
15322 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15323    the current context, return the TYPE_DECL.  If TAG_NAME_P is
15324    true, the DECL indicates the class being defined in a class-head,
15325    or declared in an elaborated-type-specifier.
15326
15327    Otherwise, return DECL.  */
15328
15329 static tree
15330 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15331 {
15332   /* If the TEMPLATE_DECL is being declared as part of a class-head,
15333      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15334
15335        struct A {
15336          template <typename T> struct B;
15337        };
15338
15339        template <typename T> struct A::B {};
15340
15341      Similarly, in an elaborated-type-specifier:
15342
15343        namespace N { struct X{}; }
15344
15345        struct A {
15346          template <typename T> friend struct N::X;
15347        };
15348
15349      However, if the DECL refers to a class type, and we are in
15350      the scope of the class, then the name lookup automatically
15351      finds the TYPE_DECL created by build_self_reference rather
15352      than a TEMPLATE_DECL.  For example, in:
15353
15354        template <class T> struct S {
15355          S s;
15356        };
15357
15358      there is no need to handle such case.  */
15359
15360   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15361     return DECL_TEMPLATE_RESULT (decl);
15362
15363   return decl;
15364 }
15365
15366 /* If too many, or too few, template-parameter lists apply to the
15367    declarator, issue an error message.  Returns TRUE if all went well,
15368    and FALSE otherwise.  */
15369
15370 static bool
15371 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15372                                                 cp_declarator *declarator)
15373 {
15374   unsigned num_templates;
15375
15376   /* We haven't seen any classes that involve template parameters yet.  */
15377   num_templates = 0;
15378
15379   switch (declarator->kind)
15380     {
15381     case cdk_id:
15382       if (declarator->u.id.qualifying_scope)
15383         {
15384           tree scope;
15385           tree member;
15386
15387           scope = declarator->u.id.qualifying_scope;
15388           member = declarator->u.id.unqualified_name;
15389
15390           while (scope && CLASS_TYPE_P (scope))
15391             {
15392               /* You're supposed to have one `template <...>'
15393                  for every template class, but you don't need one
15394                  for a full specialization.  For example:
15395
15396                  template <class T> struct S{};
15397                  template <> struct S<int> { void f(); };
15398                  void S<int>::f () {}
15399
15400                  is correct; there shouldn't be a `template <>' for
15401                  the definition of `S<int>::f'.  */
15402               if (!CLASSTYPE_TEMPLATE_INFO (scope))
15403                 /* If SCOPE does not have template information of any
15404                    kind, then it is not a template, nor is it nested
15405                    within a template.  */
15406                 break;
15407               if (explicit_class_specialization_p (scope))
15408                 break;
15409               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15410                 ++num_templates;
15411
15412               scope = TYPE_CONTEXT (scope);
15413             }
15414         }
15415       else if (TREE_CODE (declarator->u.id.unqualified_name)
15416                == TEMPLATE_ID_EXPR)
15417         /* If the DECLARATOR has the form `X<y>' then it uses one
15418            additional level of template parameters.  */
15419         ++num_templates;
15420
15421       return cp_parser_check_template_parameters (parser,
15422                                                   num_templates);
15423
15424     case cdk_function:
15425     case cdk_array:
15426     case cdk_pointer:
15427     case cdk_reference:
15428     case cdk_ptrmem:
15429       return (cp_parser_check_declarator_template_parameters
15430               (parser, declarator->declarator));
15431
15432     case cdk_error:
15433       return true;
15434
15435     default:
15436       gcc_unreachable ();
15437     }
15438   return false;
15439 }
15440
15441 /* NUM_TEMPLATES were used in the current declaration.  If that is
15442    invalid, return FALSE and issue an error messages.  Otherwise,
15443    return TRUE.  */
15444
15445 static bool
15446 cp_parser_check_template_parameters (cp_parser* parser,
15447                                      unsigned num_templates)
15448 {
15449   /* If there are more template classes than parameter lists, we have
15450      something like:
15451
15452        template <class T> void S<T>::R<T>::f ();  */
15453   if (parser->num_template_parameter_lists < num_templates)
15454     {
15455       error ("too few template-parameter-lists");
15456       return false;
15457     }
15458   /* If there are the same number of template classes and parameter
15459      lists, that's OK.  */
15460   if (parser->num_template_parameter_lists == num_templates)
15461     return true;
15462   /* If there are more, but only one more, then we are referring to a
15463      member template.  That's OK too.  */
15464   if (parser->num_template_parameter_lists == num_templates + 1)
15465       return true;
15466   /* Otherwise, there are too many template parameter lists.  We have
15467      something like:
15468
15469      template <class T> template <class U> void S::f();  */
15470   error ("too many template-parameter-lists");
15471   return false;
15472 }
15473
15474 /* Parse an optional `::' token indicating that the following name is
15475    from the global namespace.  If so, PARSER->SCOPE is set to the
15476    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15477    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15478    Returns the new value of PARSER->SCOPE, if the `::' token is
15479    present, and NULL_TREE otherwise.  */
15480
15481 static tree
15482 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15483 {
15484   cp_token *token;
15485
15486   /* Peek at the next token.  */
15487   token = cp_lexer_peek_token (parser->lexer);
15488   /* If we're looking at a `::' token then we're starting from the
15489      global namespace, not our current location.  */
15490   if (token->type == CPP_SCOPE)
15491     {
15492       /* Consume the `::' token.  */
15493       cp_lexer_consume_token (parser->lexer);
15494       /* Set the SCOPE so that we know where to start the lookup.  */
15495       parser->scope = global_namespace;
15496       parser->qualifying_scope = global_namespace;
15497       parser->object_scope = NULL_TREE;
15498
15499       return parser->scope;
15500     }
15501   else if (!current_scope_valid_p)
15502     {
15503       parser->scope = NULL_TREE;
15504       parser->qualifying_scope = NULL_TREE;
15505       parser->object_scope = NULL_TREE;
15506     }
15507
15508   return NULL_TREE;
15509 }
15510
15511 /* Returns TRUE if the upcoming token sequence is the start of a
15512    constructor declarator.  If FRIEND_P is true, the declarator is
15513    preceded by the `friend' specifier.  */
15514
15515 static bool
15516 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15517 {
15518   bool constructor_p;
15519   tree type_decl = NULL_TREE;
15520   bool nested_name_p;
15521   cp_token *next_token;
15522
15523   /* The common case is that this is not a constructor declarator, so
15524      try to avoid doing lots of work if at all possible.  It's not
15525      valid declare a constructor at function scope.  */
15526   if (parser->in_function_body)
15527     return false;
15528   /* And only certain tokens can begin a constructor declarator.  */
15529   next_token = cp_lexer_peek_token (parser->lexer);
15530   if (next_token->type != CPP_NAME
15531       && next_token->type != CPP_SCOPE
15532       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15533       && next_token->type != CPP_TEMPLATE_ID)
15534     return false;
15535
15536   /* Parse tentatively; we are going to roll back all of the tokens
15537      consumed here.  */
15538   cp_parser_parse_tentatively (parser);
15539   /* Assume that we are looking at a constructor declarator.  */
15540   constructor_p = true;
15541
15542   /* Look for the optional `::' operator.  */
15543   cp_parser_global_scope_opt (parser,
15544                               /*current_scope_valid_p=*/false);
15545   /* Look for the nested-name-specifier.  */
15546   nested_name_p
15547     = (cp_parser_nested_name_specifier_opt (parser,
15548                                             /*typename_keyword_p=*/false,
15549                                             /*check_dependency_p=*/false,
15550                                             /*type_p=*/false,
15551                                             /*is_declaration=*/false)
15552        != NULL_TREE);
15553   /* Outside of a class-specifier, there must be a
15554      nested-name-specifier.  */
15555   if (!nested_name_p &&
15556       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15557        || friend_p))
15558     constructor_p = false;
15559   /* If we still think that this might be a constructor-declarator,
15560      look for a class-name.  */
15561   if (constructor_p)
15562     {
15563       /* If we have:
15564
15565            template <typename T> struct S { S(); };
15566            template <typename T> S<T>::S ();
15567
15568          we must recognize that the nested `S' names a class.
15569          Similarly, for:
15570
15571            template <typename T> S<T>::S<T> ();
15572
15573          we must recognize that the nested `S' names a template.  */
15574       type_decl = cp_parser_class_name (parser,
15575                                         /*typename_keyword_p=*/false,
15576                                         /*template_keyword_p=*/false,
15577                                         none_type,
15578                                         /*check_dependency_p=*/false,
15579                                         /*class_head_p=*/false,
15580                                         /*is_declaration=*/false);
15581       /* If there was no class-name, then this is not a constructor.  */
15582       constructor_p = !cp_parser_error_occurred (parser);
15583     }
15584
15585   /* If we're still considering a constructor, we have to see a `(',
15586      to begin the parameter-declaration-clause, followed by either a
15587      `)', an `...', or a decl-specifier.  We need to check for a
15588      type-specifier to avoid being fooled into thinking that:
15589
15590        S::S (f) (int);
15591
15592      is a constructor.  (It is actually a function named `f' that
15593      takes one parameter (of type `int') and returns a value of type
15594      `S::S'.  */
15595   if (constructor_p
15596       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15597     {
15598       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15599           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15600           /* A parameter declaration begins with a decl-specifier,
15601              which is either the "attribute" keyword, a storage class
15602              specifier, or (usually) a type-specifier.  */
15603           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15604         {
15605           tree type;
15606           tree pushed_scope = NULL_TREE;
15607           unsigned saved_num_template_parameter_lists;
15608
15609           /* Names appearing in the type-specifier should be looked up
15610              in the scope of the class.  */
15611           if (current_class_type)
15612             type = NULL_TREE;
15613           else
15614             {
15615               type = TREE_TYPE (type_decl);
15616               if (TREE_CODE (type) == TYPENAME_TYPE)
15617                 {
15618                   type = resolve_typename_type (type,
15619                                                 /*only_current_p=*/false);
15620                   if (type == error_mark_node)
15621                     {
15622                       cp_parser_abort_tentative_parse (parser);
15623                       return false;
15624                     }
15625                 }
15626               pushed_scope = push_scope (type);
15627             }
15628
15629           /* Inside the constructor parameter list, surrounding
15630              template-parameter-lists do not apply.  */
15631           saved_num_template_parameter_lists
15632             = parser->num_template_parameter_lists;
15633           parser->num_template_parameter_lists = 0;
15634
15635           /* Look for the type-specifier.  */
15636           cp_parser_type_specifier (parser,
15637                                     CP_PARSER_FLAGS_NONE,
15638                                     /*decl_specs=*/NULL,
15639                                     /*is_declarator=*/true,
15640                                     /*declares_class_or_enum=*/NULL,
15641                                     /*is_cv_qualifier=*/NULL);
15642
15643           parser->num_template_parameter_lists
15644             = saved_num_template_parameter_lists;
15645
15646           /* Leave the scope of the class.  */
15647           if (pushed_scope)
15648             pop_scope (pushed_scope);
15649
15650           constructor_p = !cp_parser_error_occurred (parser);
15651         }
15652     }
15653   else
15654     constructor_p = false;
15655   /* We did not really want to consume any tokens.  */
15656   cp_parser_abort_tentative_parse (parser);
15657
15658   return constructor_p;
15659 }
15660
15661 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15662    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15663    they must be performed once we are in the scope of the function.
15664
15665    Returns the function defined.  */
15666
15667 static tree
15668 cp_parser_function_definition_from_specifiers_and_declarator
15669   (cp_parser* parser,
15670    cp_decl_specifier_seq *decl_specifiers,
15671    tree attributes,
15672    const cp_declarator *declarator)
15673 {
15674   tree fn;
15675   bool success_p;
15676
15677   /* Begin the function-definition.  */
15678   success_p = start_function (decl_specifiers, declarator, attributes);
15679
15680   /* The things we're about to see are not directly qualified by any
15681      template headers we've seen thus far.  */
15682   reset_specialization ();
15683
15684   /* If there were names looked up in the decl-specifier-seq that we
15685      did not check, check them now.  We must wait until we are in the
15686      scope of the function to perform the checks, since the function
15687      might be a friend.  */
15688   perform_deferred_access_checks ();
15689
15690   if (!success_p)
15691     {
15692       /* Skip the entire function.  */
15693       cp_parser_skip_to_end_of_block_or_statement (parser);
15694       fn = error_mark_node;
15695     }
15696   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
15697     {
15698       /* Seen already, skip it.  An error message has already been output.  */
15699       cp_parser_skip_to_end_of_block_or_statement (parser);
15700       fn = current_function_decl;
15701       current_function_decl = NULL_TREE;
15702       /* If this is a function from a class, pop the nested class.  */
15703       if (current_class_name)
15704         pop_nested_class ();
15705     }
15706   else
15707     fn = cp_parser_function_definition_after_declarator (parser,
15708                                                          /*inline_p=*/false);
15709
15710   return fn;
15711 }
15712
15713 /* Parse the part of a function-definition that follows the
15714    declarator.  INLINE_P is TRUE iff this function is an inline
15715    function defined with a class-specifier.
15716
15717    Returns the function defined.  */
15718
15719 static tree
15720 cp_parser_function_definition_after_declarator (cp_parser* parser,
15721                                                 bool inline_p)
15722 {
15723   tree fn;
15724   bool ctor_initializer_p = false;
15725   bool saved_in_unbraced_linkage_specification_p;
15726   bool saved_in_function_body;
15727   unsigned saved_num_template_parameter_lists;
15728
15729   saved_in_function_body = parser->in_function_body;
15730   parser->in_function_body = true;
15731   /* If the next token is `return', then the code may be trying to
15732      make use of the "named return value" extension that G++ used to
15733      support.  */
15734   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15735     {
15736       /* Consume the `return' keyword.  */
15737       cp_lexer_consume_token (parser->lexer);
15738       /* Look for the identifier that indicates what value is to be
15739          returned.  */
15740       cp_parser_identifier (parser);
15741       /* Issue an error message.  */
15742       error ("named return values are no longer supported");
15743       /* Skip tokens until we reach the start of the function body.  */
15744       while (true)
15745         {
15746           cp_token *token = cp_lexer_peek_token (parser->lexer);
15747           if (token->type == CPP_OPEN_BRACE
15748               || token->type == CPP_EOF
15749               || token->type == CPP_PRAGMA_EOL)
15750             break;
15751           cp_lexer_consume_token (parser->lexer);
15752         }
15753     }
15754   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15755      anything declared inside `f'.  */
15756   saved_in_unbraced_linkage_specification_p
15757     = parser->in_unbraced_linkage_specification_p;
15758   parser->in_unbraced_linkage_specification_p = false;
15759   /* Inside the function, surrounding template-parameter-lists do not
15760      apply.  */
15761   saved_num_template_parameter_lists
15762     = parser->num_template_parameter_lists;
15763   parser->num_template_parameter_lists = 0;
15764   /* If the next token is `try', then we are looking at a
15765      function-try-block.  */
15766   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15767     ctor_initializer_p = cp_parser_function_try_block (parser);
15768   /* A function-try-block includes the function-body, so we only do
15769      this next part if we're not processing a function-try-block.  */
15770   else
15771     ctor_initializer_p
15772       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15773
15774   /* Finish the function.  */
15775   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15776                         (inline_p ? 2 : 0));
15777   /* Generate code for it, if necessary.  */
15778   expand_or_defer_fn (fn);
15779   /* Restore the saved values.  */
15780   parser->in_unbraced_linkage_specification_p
15781     = saved_in_unbraced_linkage_specification_p;
15782   parser->num_template_parameter_lists
15783     = saved_num_template_parameter_lists;
15784   parser->in_function_body = saved_in_function_body;
15785
15786   return fn;
15787 }
15788
15789 /* Parse a template-declaration, assuming that the `export' (and
15790    `extern') keywords, if present, has already been scanned.  MEMBER_P
15791    is as for cp_parser_template_declaration.  */
15792
15793 static void
15794 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15795 {
15796   tree decl = NULL_TREE;
15797   VEC (deferred_access_check,gc) *checks;
15798   tree parameter_list;
15799   bool friend_p = false;
15800   bool need_lang_pop;
15801
15802   /* Look for the `template' keyword.  */
15803   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15804     return;
15805
15806   /* And the `<'.  */
15807   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15808     return;
15809   if (at_class_scope_p () && current_function_decl)
15810     {
15811       /* 14.5.2.2 [temp.mem]
15812
15813          A local class shall not have member templates.  */
15814       error ("invalid declaration of member template in local class");
15815       cp_parser_skip_to_end_of_block_or_statement (parser);
15816       return;
15817     }
15818   /* [temp]
15819
15820      A template ... shall not have C linkage.  */
15821   if (current_lang_name == lang_name_c)
15822     {
15823       error ("template with C linkage");
15824       /* Give it C++ linkage to avoid confusing other parts of the
15825          front end.  */
15826       push_lang_context (lang_name_cplusplus);
15827       need_lang_pop = true;
15828     }
15829   else
15830     need_lang_pop = false;
15831
15832   /* We cannot perform access checks on the template parameter
15833      declarations until we know what is being declared, just as we
15834      cannot check the decl-specifier list.  */
15835   push_deferring_access_checks (dk_deferred);
15836
15837   /* If the next token is `>', then we have an invalid
15838      specialization.  Rather than complain about an invalid template
15839      parameter, issue an error message here.  */
15840   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15841     {
15842       cp_parser_error (parser, "invalid explicit specialization");
15843       begin_specialization ();
15844       parameter_list = NULL_TREE;
15845     }
15846   else
15847     /* Parse the template parameters.  */
15848     parameter_list = cp_parser_template_parameter_list (parser);
15849
15850   /* Get the deferred access checks from the parameter list.  These
15851      will be checked once we know what is being declared, as for a
15852      member template the checks must be performed in the scope of the
15853      class containing the member.  */
15854   checks = get_deferred_access_checks ();
15855
15856   /* Look for the `>'.  */
15857   cp_parser_skip_to_end_of_template_parameter_list (parser);
15858   /* We just processed one more parameter list.  */
15859   ++parser->num_template_parameter_lists;
15860   /* If the next token is `template', there are more template
15861      parameters.  */
15862   if (cp_lexer_next_token_is_keyword (parser->lexer,
15863                                       RID_TEMPLATE))
15864     cp_parser_template_declaration_after_export (parser, member_p);
15865   else
15866     {
15867       /* There are no access checks when parsing a template, as we do not
15868          know if a specialization will be a friend.  */
15869       push_deferring_access_checks (dk_no_check);
15870       decl = cp_parser_single_declaration (parser,
15871                                            checks,
15872                                            member_p,
15873                                            &friend_p);
15874       pop_deferring_access_checks ();
15875
15876       /* If this is a member template declaration, let the front
15877          end know.  */
15878       if (member_p && !friend_p && decl)
15879         {
15880           if (TREE_CODE (decl) == TYPE_DECL)
15881             cp_parser_check_access_in_redeclaration (decl);
15882
15883           decl = finish_member_template_decl (decl);
15884         }
15885       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15886         make_friend_class (current_class_type, TREE_TYPE (decl),
15887                            /*complain=*/true);
15888     }
15889   /* We are done with the current parameter list.  */
15890   --parser->num_template_parameter_lists;
15891
15892   pop_deferring_access_checks ();
15893
15894   /* Finish up.  */
15895   finish_template_decl (parameter_list);
15896
15897   /* Register member declarations.  */
15898   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15899     finish_member_declaration (decl);
15900   /* For the erroneous case of a template with C linkage, we pushed an
15901      implicit C++ linkage scope; exit that scope now.  */
15902   if (need_lang_pop)
15903     pop_lang_context ();
15904   /* If DECL is a function template, we must return to parse it later.
15905      (Even though there is no definition, there might be default
15906      arguments that need handling.)  */
15907   if (member_p && decl
15908       && (TREE_CODE (decl) == FUNCTION_DECL
15909           || DECL_FUNCTION_TEMPLATE_P (decl)))
15910     TREE_VALUE (parser->unparsed_functions_queues)
15911       = tree_cons (NULL_TREE, decl,
15912                    TREE_VALUE (parser->unparsed_functions_queues));
15913 }
15914
15915 /* Perform the deferred access checks from a template-parameter-list.
15916    CHECKS is a TREE_LIST of access checks, as returned by
15917    get_deferred_access_checks.  */
15918
15919 static void
15920 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
15921 {
15922   ++processing_template_parmlist;
15923   perform_access_checks (checks);
15924   --processing_template_parmlist;
15925 }
15926
15927 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15928    `function-definition' sequence.  MEMBER_P is true, this declaration
15929    appears in a class scope.
15930
15931    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15932    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15933
15934 static tree
15935 cp_parser_single_declaration (cp_parser* parser,
15936                               VEC (deferred_access_check,gc)* checks,
15937                               bool member_p,
15938                               bool* friend_p)
15939 {
15940   int declares_class_or_enum;
15941   tree decl = NULL_TREE;
15942   cp_decl_specifier_seq decl_specifiers;
15943   bool function_definition_p = false;
15944
15945   /* This function is only used when processing a template
15946      declaration.  */
15947   gcc_assert (innermost_scope_kind () == sk_template_parms
15948               || innermost_scope_kind () == sk_template_spec);
15949
15950   /* Defer access checks until we know what is being declared.  */
15951   push_deferring_access_checks (dk_deferred);
15952
15953   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15954      alternative.  */
15955   cp_parser_decl_specifier_seq (parser,
15956                                 CP_PARSER_FLAGS_OPTIONAL,
15957                                 &decl_specifiers,
15958                                 &declares_class_or_enum);
15959   if (friend_p)
15960     *friend_p = cp_parser_friend_p (&decl_specifiers);
15961
15962   /* There are no template typedefs.  */
15963   if (decl_specifiers.specs[(int) ds_typedef])
15964     {
15965       error ("template declaration of %qs", "typedef");
15966       decl = error_mark_node;
15967     }
15968
15969   /* Gather up the access checks that occurred the
15970      decl-specifier-seq.  */
15971   stop_deferring_access_checks ();
15972
15973   /* Check for the declaration of a template class.  */
15974   if (declares_class_or_enum)
15975     {
15976       if (cp_parser_declares_only_class_p (parser))
15977         {
15978           decl = shadow_tag (&decl_specifiers);
15979
15980           /* In this case:
15981
15982                struct C {
15983                  friend template <typename T> struct A<T>::B;
15984                };
15985
15986              A<T>::B will be represented by a TYPENAME_TYPE, and
15987              therefore not recognized by shadow_tag.  */
15988           if (friend_p && *friend_p
15989               && !decl
15990               && decl_specifiers.type
15991               && TYPE_P (decl_specifiers.type))
15992             decl = decl_specifiers.type;
15993
15994           if (decl && decl != error_mark_node)
15995             decl = TYPE_NAME (decl);
15996           else
15997             decl = error_mark_node;
15998
15999           /* Perform access checks for template parameters.  */
16000           cp_parser_perform_template_parameter_access_checks (checks);
16001         }
16002     }
16003   /* If it's not a template class, try for a template function.  If
16004      the next token is a `;', then this declaration does not declare
16005      anything.  But, if there were errors in the decl-specifiers, then
16006      the error might well have come from an attempted class-specifier.
16007      In that case, there's no need to warn about a missing declarator.  */
16008   if (!decl
16009       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16010           || decl_specifiers.type != error_mark_node))
16011     decl = cp_parser_init_declarator (parser,
16012                                       &decl_specifiers,
16013                                       checks,
16014                                       /*function_definition_allowed_p=*/true,
16015                                       member_p,
16016                                       declares_class_or_enum,
16017                                       &function_definition_p);
16018
16019   pop_deferring_access_checks ();
16020
16021   /* Clear any current qualification; whatever comes next is the start
16022      of something new.  */
16023   parser->scope = NULL_TREE;
16024   parser->qualifying_scope = NULL_TREE;
16025   parser->object_scope = NULL_TREE;
16026   /* Look for a trailing `;' after the declaration.  */
16027   if (!function_definition_p
16028       && (decl == error_mark_node
16029           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16030     cp_parser_skip_to_end_of_block_or_statement (parser);
16031
16032   return decl;
16033 }
16034
16035 /* Parse a cast-expression that is not the operand of a unary "&".  */
16036
16037 static tree
16038 cp_parser_simple_cast_expression (cp_parser *parser)
16039 {
16040   return cp_parser_cast_expression (parser, /*address_p=*/false,
16041                                     /*cast_p=*/false);
16042 }
16043
16044 /* Parse a functional cast to TYPE.  Returns an expression
16045    representing the cast.  */
16046
16047 static tree
16048 cp_parser_functional_cast (cp_parser* parser, tree type)
16049 {
16050   tree expression_list;
16051   tree cast;
16052
16053   expression_list
16054     = cp_parser_parenthesized_expression_list (parser, false,
16055                                                /*cast_p=*/true,
16056                                                /*non_constant_p=*/NULL);
16057
16058   cast = build_functional_cast (type, expression_list);
16059   /* [expr.const]/1: In an integral constant expression "only type
16060      conversions to integral or enumeration type can be used".  */
16061   if (TREE_CODE (type) == TYPE_DECL)
16062     type = TREE_TYPE (type);
16063   if (cast != error_mark_node
16064       && !cast_valid_in_integral_constant_expression_p (type)
16065       && (cp_parser_non_integral_constant_expression
16066           (parser, "a call to a constructor")))
16067     return error_mark_node;
16068   return cast;
16069 }
16070
16071 /* Save the tokens that make up the body of a member function defined
16072    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
16073    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
16074    specifiers applied to the declaration.  Returns the FUNCTION_DECL
16075    for the member function.  */
16076
16077 static tree
16078 cp_parser_save_member_function_body (cp_parser* parser,
16079                                      cp_decl_specifier_seq *decl_specifiers,
16080                                      cp_declarator *declarator,
16081                                      tree attributes)
16082 {
16083   cp_token *first;
16084   cp_token *last;
16085   tree fn;
16086
16087   /* Create the function-declaration.  */
16088   fn = start_method (decl_specifiers, declarator, attributes);
16089   /* If something went badly wrong, bail out now.  */
16090   if (fn == error_mark_node)
16091     {
16092       /* If there's a function-body, skip it.  */
16093       if (cp_parser_token_starts_function_definition_p
16094           (cp_lexer_peek_token (parser->lexer)))
16095         cp_parser_skip_to_end_of_block_or_statement (parser);
16096       return error_mark_node;
16097     }
16098
16099   /* Remember it, if there default args to post process.  */
16100   cp_parser_save_default_args (parser, fn);
16101
16102   /* Save away the tokens that make up the body of the
16103      function.  */
16104   first = parser->lexer->next_token;
16105   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16106   /* Handle function try blocks.  */
16107   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16108     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16109   last = parser->lexer->next_token;
16110
16111   /* Save away the inline definition; we will process it when the
16112      class is complete.  */
16113   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16114   DECL_PENDING_INLINE_P (fn) = 1;
16115
16116   /* We need to know that this was defined in the class, so that
16117      friend templates are handled correctly.  */
16118   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16119
16120   /* We're done with the inline definition.  */
16121   finish_method (fn);
16122
16123   /* Add FN to the queue of functions to be parsed later.  */
16124   TREE_VALUE (parser->unparsed_functions_queues)
16125     = tree_cons (NULL_TREE, fn,
16126                  TREE_VALUE (parser->unparsed_functions_queues));
16127
16128   return fn;
16129 }
16130
16131 /* Parse a template-argument-list, as well as the trailing ">" (but
16132    not the opening ">").  See cp_parser_template_argument_list for the
16133    return value.  */
16134
16135 static tree
16136 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16137 {
16138   tree arguments;
16139   tree saved_scope;
16140   tree saved_qualifying_scope;
16141   tree saved_object_scope;
16142   bool saved_greater_than_is_operator_p;
16143   bool saved_skip_evaluation;
16144
16145   /* [temp.names]
16146
16147      When parsing a template-id, the first non-nested `>' is taken as
16148      the end of the template-argument-list rather than a greater-than
16149      operator.  */
16150   saved_greater_than_is_operator_p
16151     = parser->greater_than_is_operator_p;
16152   parser->greater_than_is_operator_p = false;
16153   /* Parsing the argument list may modify SCOPE, so we save it
16154      here.  */
16155   saved_scope = parser->scope;
16156   saved_qualifying_scope = parser->qualifying_scope;
16157   saved_object_scope = parser->object_scope;
16158   /* We need to evaluate the template arguments, even though this
16159      template-id may be nested within a "sizeof".  */
16160   saved_skip_evaluation = skip_evaluation;
16161   skip_evaluation = false;
16162   /* Parse the template-argument-list itself.  */
16163   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16164     arguments = NULL_TREE;
16165   else
16166     arguments = cp_parser_template_argument_list (parser);
16167   /* Look for the `>' that ends the template-argument-list. If we find
16168      a '>>' instead, it's probably just a typo.  */
16169   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16170     {
16171       if (!saved_greater_than_is_operator_p)
16172         {
16173           /* If we're in a nested template argument list, the '>>' has
16174             to be a typo for '> >'. We emit the error message, but we
16175             continue parsing and we push a '>' as next token, so that
16176             the argument list will be parsed correctly.  Note that the
16177             global source location is still on the token before the
16178             '>>', so we need to say explicitly where we want it.  */
16179           cp_token *token = cp_lexer_peek_token (parser->lexer);
16180           error ("%H%<>>%> should be %<> >%> "
16181                  "within a nested template argument list",
16182                  &token->location);
16183
16184           /* ??? Proper recovery should terminate two levels of
16185              template argument list here.  */
16186           token->type = CPP_GREATER;
16187         }
16188       else
16189         {
16190           /* If this is not a nested template argument list, the '>>'
16191             is a typo for '>'. Emit an error message and continue.
16192             Same deal about the token location, but here we can get it
16193             right by consuming the '>>' before issuing the diagnostic.  */
16194           cp_lexer_consume_token (parser->lexer);
16195           error ("spurious %<>>%>, use %<>%> to terminate "
16196                  "a template argument list");
16197         }
16198     }
16199   else
16200     cp_parser_skip_to_end_of_template_parameter_list (parser);
16201   /* The `>' token might be a greater-than operator again now.  */
16202   parser->greater_than_is_operator_p
16203     = saved_greater_than_is_operator_p;
16204   /* Restore the SAVED_SCOPE.  */
16205   parser->scope = saved_scope;
16206   parser->qualifying_scope = saved_qualifying_scope;
16207   parser->object_scope = saved_object_scope;
16208   skip_evaluation = saved_skip_evaluation;
16209
16210   return arguments;
16211 }
16212
16213 /* MEMBER_FUNCTION is a member function, or a friend.  If default
16214    arguments, or the body of the function have not yet been parsed,
16215    parse them now.  */
16216
16217 static void
16218 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16219 {
16220   /* If this member is a template, get the underlying
16221      FUNCTION_DECL.  */
16222   if (DECL_FUNCTION_TEMPLATE_P (member_function))
16223     member_function = DECL_TEMPLATE_RESULT (member_function);
16224
16225   /* There should not be any class definitions in progress at this
16226      point; the bodies of members are only parsed outside of all class
16227      definitions.  */
16228   gcc_assert (parser->num_classes_being_defined == 0);
16229   /* While we're parsing the member functions we might encounter more
16230      classes.  We want to handle them right away, but we don't want
16231      them getting mixed up with functions that are currently in the
16232      queue.  */
16233   parser->unparsed_functions_queues
16234     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16235
16236   /* Make sure that any template parameters are in scope.  */
16237   maybe_begin_member_template_processing (member_function);
16238
16239   /* If the body of the function has not yet been parsed, parse it
16240      now.  */
16241   if (DECL_PENDING_INLINE_P (member_function))
16242     {
16243       tree function_scope;
16244       cp_token_cache *tokens;
16245
16246       /* The function is no longer pending; we are processing it.  */
16247       tokens = DECL_PENDING_INLINE_INFO (member_function);
16248       DECL_PENDING_INLINE_INFO (member_function) = NULL;
16249       DECL_PENDING_INLINE_P (member_function) = 0;
16250
16251       /* If this is a local class, enter the scope of the containing
16252          function.  */
16253       function_scope = current_function_decl;
16254       if (function_scope)
16255         push_function_context_to (function_scope);
16256
16257
16258       /* Push the body of the function onto the lexer stack.  */
16259       cp_parser_push_lexer_for_tokens (parser, tokens);
16260
16261       /* Let the front end know that we going to be defining this
16262          function.  */
16263       start_preparsed_function (member_function, NULL_TREE,
16264                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
16265
16266       /* Don't do access checking if it is a templated function.  */
16267       if (processing_template_decl)
16268         push_deferring_access_checks (dk_no_check);
16269
16270       /* Now, parse the body of the function.  */
16271       cp_parser_function_definition_after_declarator (parser,
16272                                                       /*inline_p=*/true);
16273
16274       if (processing_template_decl)
16275         pop_deferring_access_checks ();
16276
16277       /* Leave the scope of the containing function.  */
16278       if (function_scope)
16279         pop_function_context_from (function_scope);
16280       cp_parser_pop_lexer (parser);
16281     }
16282
16283   /* Remove any template parameters from the symbol table.  */
16284   maybe_end_member_template_processing ();
16285
16286   /* Restore the queue.  */
16287   parser->unparsed_functions_queues
16288     = TREE_CHAIN (parser->unparsed_functions_queues);
16289 }
16290
16291 /* If DECL contains any default args, remember it on the unparsed
16292    functions queue.  */
16293
16294 static void
16295 cp_parser_save_default_args (cp_parser* parser, tree decl)
16296 {
16297   tree probe;
16298
16299   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16300        probe;
16301        probe = TREE_CHAIN (probe))
16302     if (TREE_PURPOSE (probe))
16303       {
16304         TREE_PURPOSE (parser->unparsed_functions_queues)
16305           = tree_cons (current_class_type, decl,
16306                        TREE_PURPOSE (parser->unparsed_functions_queues));
16307         break;
16308       }
16309 }
16310
16311 /* FN is a FUNCTION_DECL which may contains a parameter with an
16312    unparsed DEFAULT_ARG.  Parse the default args now.  This function
16313    assumes that the current scope is the scope in which the default
16314    argument should be processed.  */
16315
16316 static void
16317 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16318 {
16319   bool saved_local_variables_forbidden_p;
16320   tree parm;
16321
16322   /* While we're parsing the default args, we might (due to the
16323      statement expression extension) encounter more classes.  We want
16324      to handle them right away, but we don't want them getting mixed
16325      up with default args that are currently in the queue.  */
16326   parser->unparsed_functions_queues
16327     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16328
16329   /* Local variable names (and the `this' keyword) may not appear
16330      in a default argument.  */
16331   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16332   parser->local_variables_forbidden_p = true;
16333
16334   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16335        parm;
16336        parm = TREE_CHAIN (parm))
16337     {
16338       cp_token_cache *tokens;
16339       tree default_arg = TREE_PURPOSE (parm);
16340       tree parsed_arg;
16341       VEC(tree,gc) *insts;
16342       tree copy;
16343       unsigned ix;
16344
16345       if (!default_arg)
16346         continue;
16347
16348       if (TREE_CODE (default_arg) != DEFAULT_ARG)
16349         /* This can happen for a friend declaration for a function
16350            already declared with default arguments.  */
16351         continue;
16352
16353        /* Push the saved tokens for the default argument onto the parser's
16354           lexer stack.  */
16355       tokens = DEFARG_TOKENS (default_arg);
16356       cp_parser_push_lexer_for_tokens (parser, tokens);
16357
16358       /* Parse the assignment-expression.  */
16359       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16360
16361       if (!processing_template_decl)
16362         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16363
16364       TREE_PURPOSE (parm) = parsed_arg;
16365
16366       /* Update any instantiations we've already created.  */
16367       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16368            VEC_iterate (tree, insts, ix, copy); ix++)
16369         TREE_PURPOSE (copy) = parsed_arg;
16370
16371       /* If the token stream has not been completely used up, then
16372          there was extra junk after the end of the default
16373          argument.  */
16374       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16375         cp_parser_error (parser, "expected %<,%>");
16376
16377       /* Revert to the main lexer.  */
16378       cp_parser_pop_lexer (parser);
16379     }
16380
16381   /* Make sure no default arg is missing.  */
16382   check_default_args (fn);
16383
16384   /* Restore the state of local_variables_forbidden_p.  */
16385   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16386
16387   /* Restore the queue.  */
16388   parser->unparsed_functions_queues
16389     = TREE_CHAIN (parser->unparsed_functions_queues);
16390 }
16391
16392 /* Parse the operand of `sizeof' (or a similar operator).  Returns
16393    either a TYPE or an expression, depending on the form of the
16394    input.  The KEYWORD indicates which kind of expression we have
16395    encountered.  */
16396
16397 static tree
16398 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16399 {
16400   static const char *format;
16401   tree expr = NULL_TREE;
16402   const char *saved_message;
16403   bool saved_integral_constant_expression_p;
16404   bool saved_non_integral_constant_expression_p;
16405
16406   /* Initialize FORMAT the first time we get here.  */
16407   if (!format)
16408     format = "types may not be defined in '%s' expressions";
16409
16410   /* Types cannot be defined in a `sizeof' expression.  Save away the
16411      old message.  */
16412   saved_message = parser->type_definition_forbidden_message;
16413   /* And create the new one.  */
16414   parser->type_definition_forbidden_message
16415     = XNEWVEC (const char, strlen (format)
16416                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16417                + 1 /* `\0' */);
16418   sprintf ((char *) parser->type_definition_forbidden_message,
16419            format, IDENTIFIER_POINTER (ridpointers[keyword]));
16420
16421   /* The restrictions on constant-expressions do not apply inside
16422      sizeof expressions.  */
16423   saved_integral_constant_expression_p
16424     = parser->integral_constant_expression_p;
16425   saved_non_integral_constant_expression_p
16426     = parser->non_integral_constant_expression_p;
16427   parser->integral_constant_expression_p = false;
16428
16429   /* Do not actually evaluate the expression.  */
16430   ++skip_evaluation;
16431   /* If it's a `(', then we might be looking at the type-id
16432      construction.  */
16433   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16434     {
16435       tree type;
16436       bool saved_in_type_id_in_expr_p;
16437
16438       /* We can't be sure yet whether we're looking at a type-id or an
16439          expression.  */
16440       cp_parser_parse_tentatively (parser);
16441       /* Consume the `('.  */
16442       cp_lexer_consume_token (parser->lexer);
16443       /* Parse the type-id.  */
16444       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16445       parser->in_type_id_in_expr_p = true;
16446       type = cp_parser_type_id (parser);
16447       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16448       /* Now, look for the trailing `)'.  */
16449       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16450       /* If all went well, then we're done.  */
16451       if (cp_parser_parse_definitely (parser))
16452         {
16453           cp_decl_specifier_seq decl_specs;
16454
16455           /* Build a trivial decl-specifier-seq.  */
16456           clear_decl_specs (&decl_specs);
16457           decl_specs.type = type;
16458
16459           /* Call grokdeclarator to figure out what type this is.  */
16460           expr = grokdeclarator (NULL,
16461                                  &decl_specs,
16462                                  TYPENAME,
16463                                  /*initialized=*/0,
16464                                  /*attrlist=*/NULL);
16465         }
16466     }
16467
16468   /* If the type-id production did not work out, then we must be
16469      looking at the unary-expression production.  */
16470   if (!expr)
16471     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16472                                        /*cast_p=*/false);
16473   /* Go back to evaluating expressions.  */
16474   --skip_evaluation;
16475
16476   /* Free the message we created.  */
16477   free ((char *) parser->type_definition_forbidden_message);
16478   /* And restore the old one.  */
16479   parser->type_definition_forbidden_message = saved_message;
16480   parser->integral_constant_expression_p
16481     = saved_integral_constant_expression_p;
16482   parser->non_integral_constant_expression_p
16483     = saved_non_integral_constant_expression_p;
16484
16485   return expr;
16486 }
16487
16488 /* If the current declaration has no declarator, return true.  */
16489
16490 static bool
16491 cp_parser_declares_only_class_p (cp_parser *parser)
16492 {
16493   /* If the next token is a `;' or a `,' then there is no
16494      declarator.  */
16495   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16496           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16497 }
16498
16499 /* Update the DECL_SPECS to reflect the storage class indicated by
16500    KEYWORD.  */
16501
16502 static void
16503 cp_parser_set_storage_class (cp_parser *parser,
16504                              cp_decl_specifier_seq *decl_specs,
16505                              enum rid keyword)
16506 {
16507   cp_storage_class storage_class;
16508
16509   if (parser->in_unbraced_linkage_specification_p)
16510     {
16511       error ("invalid use of %qD in linkage specification",
16512              ridpointers[keyword]);
16513       return;
16514     }
16515   else if (decl_specs->storage_class != sc_none)
16516     {
16517       decl_specs->conflicting_specifiers_p = true;
16518       return;
16519     }
16520
16521   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16522       && decl_specs->specs[(int) ds_thread])
16523     {
16524       error ("%<__thread%> before %qD", ridpointers[keyword]);
16525       decl_specs->specs[(int) ds_thread] = 0;
16526     }
16527
16528   switch (keyword)
16529     {
16530     case RID_AUTO:
16531       storage_class = sc_auto;
16532       break;
16533     case RID_REGISTER:
16534       storage_class = sc_register;
16535       break;
16536     case RID_STATIC:
16537       storage_class = sc_static;
16538       break;
16539     case RID_EXTERN:
16540       storage_class = sc_extern;
16541       break;
16542     case RID_MUTABLE:
16543       storage_class = sc_mutable;
16544       break;
16545     default:
16546       gcc_unreachable ();
16547     }
16548   decl_specs->storage_class = storage_class;
16549
16550   /* A storage class specifier cannot be applied alongside a typedef 
16551      specifier. If there is a typedef specifier present then set 
16552      conflicting_specifiers_p which will trigger an error later
16553      on in grokdeclarator. */
16554   if (decl_specs->specs[(int)ds_typedef])
16555     decl_specs->conflicting_specifiers_p = true;
16556 }
16557
16558 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16559    is true, the type is a user-defined type; otherwise it is a
16560    built-in type specified by a keyword.  */
16561
16562 static void
16563 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16564                               tree type_spec,
16565                               bool user_defined_p)
16566 {
16567   decl_specs->any_specifiers_p = true;
16568
16569   /* If the user tries to redeclare bool or wchar_t (with, for
16570      example, in "typedef int wchar_t;") we remember that this is what
16571      happened.  In system headers, we ignore these declarations so
16572      that G++ can work with system headers that are not C++-safe.  */
16573   if (decl_specs->specs[(int) ds_typedef]
16574       && !user_defined_p
16575       && (type_spec == boolean_type_node
16576           || type_spec == wchar_type_node)
16577       && (decl_specs->type
16578           || decl_specs->specs[(int) ds_long]
16579           || decl_specs->specs[(int) ds_short]
16580           || decl_specs->specs[(int) ds_unsigned]
16581           || decl_specs->specs[(int) ds_signed]))
16582     {
16583       decl_specs->redefined_builtin_type = type_spec;
16584       if (!decl_specs->type)
16585         {
16586           decl_specs->type = type_spec;
16587           decl_specs->user_defined_type_p = false;
16588         }
16589     }
16590   else if (decl_specs->type)
16591     decl_specs->multiple_types_p = true;
16592   else
16593     {
16594       decl_specs->type = type_spec;
16595       decl_specs->user_defined_type_p = user_defined_p;
16596       decl_specs->redefined_builtin_type = NULL_TREE;
16597     }
16598 }
16599
16600 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16601    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16602
16603 static bool
16604 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16605 {
16606   return decl_specifiers->specs[(int) ds_friend] != 0;
16607 }
16608
16609 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16610    issue an error message indicating that TOKEN_DESC was expected.
16611
16612    Returns the token consumed, if the token had the appropriate type.
16613    Otherwise, returns NULL.  */
16614
16615 static cp_token *
16616 cp_parser_require (cp_parser* parser,
16617                    enum cpp_ttype type,
16618                    const char* token_desc)
16619 {
16620   if (cp_lexer_next_token_is (parser->lexer, type))
16621     return cp_lexer_consume_token (parser->lexer);
16622   else
16623     {
16624       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16625       if (!cp_parser_simulate_error (parser))
16626         {
16627           char *message = concat ("expected ", token_desc, NULL);
16628           cp_parser_error (parser, message);
16629           free (message);
16630         }
16631       return NULL;
16632     }
16633 }
16634
16635 /* An error message is produced if the next token is not '>'.
16636    All further tokens are skipped until the desired token is
16637    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
16638
16639 static void
16640 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16641 {
16642   /* Current level of '< ... >'.  */
16643   unsigned level = 0;
16644   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
16645   unsigned nesting_depth = 0;
16646
16647   /* Are we ready, yet?  If not, issue error message.  */
16648   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16649     return;
16650
16651   /* Skip tokens until the desired token is found.  */
16652   while (true)
16653     {
16654       /* Peek at the next token.  */
16655       switch (cp_lexer_peek_token (parser->lexer)->type)
16656         {
16657         case CPP_LESS:
16658           if (!nesting_depth)
16659             ++level;
16660           break;
16661
16662         case CPP_GREATER:
16663           if (!nesting_depth && level-- == 0)
16664             {
16665               /* We've reached the token we want, consume it and stop.  */
16666               cp_lexer_consume_token (parser->lexer);
16667               return;
16668             }
16669           break;
16670
16671         case CPP_OPEN_PAREN:
16672         case CPP_OPEN_SQUARE:
16673           ++nesting_depth;
16674           break;
16675
16676         case CPP_CLOSE_PAREN:
16677         case CPP_CLOSE_SQUARE:
16678           if (nesting_depth-- == 0)
16679             return;
16680           break;
16681
16682         case CPP_EOF:
16683         case CPP_PRAGMA_EOL:
16684         case CPP_SEMICOLON:
16685         case CPP_OPEN_BRACE:
16686         case CPP_CLOSE_BRACE:
16687           /* The '>' was probably forgotten, don't look further.  */
16688           return;
16689
16690         default:
16691           break;
16692         }
16693
16694       /* Consume this token.  */
16695       cp_lexer_consume_token (parser->lexer);
16696     }
16697 }
16698
16699 /* If the next token is the indicated keyword, consume it.  Otherwise,
16700    issue an error message indicating that TOKEN_DESC was expected.
16701
16702    Returns the token consumed, if the token had the appropriate type.
16703    Otherwise, returns NULL.  */
16704
16705 static cp_token *
16706 cp_parser_require_keyword (cp_parser* parser,
16707                            enum rid keyword,
16708                            const char* token_desc)
16709 {
16710   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16711
16712   if (token && token->keyword != keyword)
16713     {
16714       dyn_string_t error_msg;
16715
16716       /* Format the error message.  */
16717       error_msg = dyn_string_new (0);
16718       dyn_string_append_cstr (error_msg, "expected ");
16719       dyn_string_append_cstr (error_msg, token_desc);
16720       cp_parser_error (parser, error_msg->s);
16721       dyn_string_delete (error_msg);
16722       return NULL;
16723     }
16724
16725   return token;
16726 }
16727
16728 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16729    function-definition.  */
16730
16731 static bool
16732 cp_parser_token_starts_function_definition_p (cp_token* token)
16733 {
16734   return (/* An ordinary function-body begins with an `{'.  */
16735           token->type == CPP_OPEN_BRACE
16736           /* A ctor-initializer begins with a `:'.  */
16737           || token->type == CPP_COLON
16738           /* A function-try-block begins with `try'.  */
16739           || token->keyword == RID_TRY
16740           /* The named return value extension begins with `return'.  */
16741           || token->keyword == RID_RETURN);
16742 }
16743
16744 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16745    definition.  */
16746
16747 static bool
16748 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16749 {
16750   cp_token *token;
16751
16752   token = cp_lexer_peek_token (parser->lexer);
16753   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16754 }
16755
16756 /* Returns TRUE iff the next token is the "," or ">" ending a
16757    template-argument.  */
16758
16759 static bool
16760 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16761 {
16762   cp_token *token;
16763
16764   token = cp_lexer_peek_token (parser->lexer);
16765   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16766 }
16767
16768 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16769    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16770
16771 static bool
16772 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16773                                                      size_t n)
16774 {
16775   cp_token *token;
16776
16777   token = cp_lexer_peek_nth_token (parser->lexer, n);
16778   if (token->type == CPP_LESS)
16779     return true;
16780   /* Check for the sequence `<::' in the original code. It would be lexed as
16781      `[:', where `[' is a digraph, and there is no whitespace before
16782      `:'.  */
16783   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16784     {
16785       cp_token *token2;
16786       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16787       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16788         return true;
16789     }
16790   return false;
16791 }
16792
16793 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16794    or none_type otherwise.  */
16795
16796 static enum tag_types
16797 cp_parser_token_is_class_key (cp_token* token)
16798 {
16799   switch (token->keyword)
16800     {
16801     case RID_CLASS:
16802       return class_type;
16803     case RID_STRUCT:
16804       return record_type;
16805     case RID_UNION:
16806       return union_type;
16807
16808     default:
16809       return none_type;
16810     }
16811 }
16812
16813 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16814
16815 static void
16816 cp_parser_check_class_key (enum tag_types class_key, tree type)
16817 {
16818   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16819     pedwarn ("%qs tag used in naming %q#T",
16820             class_key == union_type ? "union"
16821              : class_key == record_type ? "struct" : "class",
16822              type);
16823 }
16824
16825 /* Issue an error message if DECL is redeclared with different
16826    access than its original declaration [class.access.spec/3].
16827    This applies to nested classes and nested class templates.
16828    [class.mem/1].  */
16829
16830 static void
16831 cp_parser_check_access_in_redeclaration (tree decl)
16832 {
16833   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16834     return;
16835
16836   if ((TREE_PRIVATE (decl)
16837        != (current_access_specifier == access_private_node))
16838       || (TREE_PROTECTED (decl)
16839           != (current_access_specifier == access_protected_node)))
16840     error ("%qD redeclared with different access", decl);
16841 }
16842
16843 /* Look for the `template' keyword, as a syntactic disambiguator.
16844    Return TRUE iff it is present, in which case it will be
16845    consumed.  */
16846
16847 static bool
16848 cp_parser_optional_template_keyword (cp_parser *parser)
16849 {
16850   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16851     {
16852       /* The `template' keyword can only be used within templates;
16853          outside templates the parser can always figure out what is a
16854          template and what is not.  */
16855       if (!processing_template_decl)
16856         {
16857           error ("%<template%> (as a disambiguator) is only allowed "
16858                  "within templates");
16859           /* If this part of the token stream is rescanned, the same
16860              error message would be generated.  So, we purge the token
16861              from the stream.  */
16862           cp_lexer_purge_token (parser->lexer);
16863           return false;
16864         }
16865       else
16866         {
16867           /* Consume the `template' keyword.  */
16868           cp_lexer_consume_token (parser->lexer);
16869           return true;
16870         }
16871     }
16872
16873   return false;
16874 }
16875
16876 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16877    set PARSER->SCOPE, and perform other related actions.  */
16878
16879 static void
16880 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16881 {
16882   int i;
16883   struct tree_check *check_value;
16884   deferred_access_check *chk;
16885   VEC (deferred_access_check,gc) *checks;
16886
16887   /* Get the stored value.  */
16888   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
16889   /* Perform any access checks that were deferred.  */
16890   checks = check_value->checks;
16891   if (checks)
16892     {
16893       for (i = 0 ;
16894            VEC_iterate (deferred_access_check, checks, i, chk) ;
16895            ++i)
16896         {
16897           perform_or_defer_access_check (chk->binfo,
16898                                          chk->decl,
16899                                          chk->diag_decl);
16900         }
16901     }
16902   /* Set the scope from the stored value.  */
16903   parser->scope = check_value->value;
16904   parser->qualifying_scope = check_value->qualifying_scope;
16905   parser->object_scope = NULL_TREE;
16906 }
16907
16908 /* Consume tokens up through a non-nested END token.  */
16909
16910 static void
16911 cp_parser_cache_group (cp_parser *parser,
16912                        enum cpp_ttype end,
16913                        unsigned depth)
16914 {
16915   while (true)
16916     {
16917       cp_token *token;
16918
16919       /* Abort a parenthesized expression if we encounter a brace.  */
16920       if ((end == CPP_CLOSE_PAREN || depth == 0)
16921           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16922         return;
16923       /* If we've reached the end of the file, stop.  */
16924       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16925           || (end != CPP_PRAGMA_EOL
16926               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16927         return;
16928       /* Consume the next token.  */
16929       token = cp_lexer_consume_token (parser->lexer);
16930       /* See if it starts a new group.  */
16931       if (token->type == CPP_OPEN_BRACE)
16932         {
16933           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16934           if (depth == 0)
16935             return;
16936         }
16937       else if (token->type == CPP_OPEN_PAREN)
16938         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16939       else if (token->type == CPP_PRAGMA)
16940         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16941       else if (token->type == end)
16942         return;
16943     }
16944 }
16945
16946 /* Begin parsing tentatively.  We always save tokens while parsing
16947    tentatively so that if the tentative parsing fails we can restore the
16948    tokens.  */
16949
16950 static void
16951 cp_parser_parse_tentatively (cp_parser* parser)
16952 {
16953   /* Enter a new parsing context.  */
16954   parser->context = cp_parser_context_new (parser->context);
16955   /* Begin saving tokens.  */
16956   cp_lexer_save_tokens (parser->lexer);
16957   /* In order to avoid repetitive access control error messages,
16958      access checks are queued up until we are no longer parsing
16959      tentatively.  */
16960   push_deferring_access_checks (dk_deferred);
16961 }
16962
16963 /* Commit to the currently active tentative parse.  */
16964
16965 static void
16966 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16967 {
16968   cp_parser_context *context;
16969   cp_lexer *lexer;
16970
16971   /* Mark all of the levels as committed.  */
16972   lexer = parser->lexer;
16973   for (context = parser->context; context->next; context = context->next)
16974     {
16975       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16976         break;
16977       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16978       while (!cp_lexer_saving_tokens (lexer))
16979         lexer = lexer->next;
16980       cp_lexer_commit_tokens (lexer);
16981     }
16982 }
16983
16984 /* Abort the currently active tentative parse.  All consumed tokens
16985    will be rolled back, and no diagnostics will be issued.  */
16986
16987 static void
16988 cp_parser_abort_tentative_parse (cp_parser* parser)
16989 {
16990   cp_parser_simulate_error (parser);
16991   /* Now, pretend that we want to see if the construct was
16992      successfully parsed.  */
16993   cp_parser_parse_definitely (parser);
16994 }
16995
16996 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16997    token stream.  Otherwise, commit to the tokens we have consumed.
16998    Returns true if no error occurred; false otherwise.  */
16999
17000 static bool
17001 cp_parser_parse_definitely (cp_parser* parser)
17002 {
17003   bool error_occurred;
17004   cp_parser_context *context;
17005
17006   /* Remember whether or not an error occurred, since we are about to
17007      destroy that information.  */
17008   error_occurred = cp_parser_error_occurred (parser);
17009   /* Remove the topmost context from the stack.  */
17010   context = parser->context;
17011   parser->context = context->next;
17012   /* If no parse errors occurred, commit to the tentative parse.  */
17013   if (!error_occurred)
17014     {
17015       /* Commit to the tokens read tentatively, unless that was
17016          already done.  */
17017       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17018         cp_lexer_commit_tokens (parser->lexer);
17019
17020       pop_to_parent_deferring_access_checks ();
17021     }
17022   /* Otherwise, if errors occurred, roll back our state so that things
17023      are just as they were before we began the tentative parse.  */
17024   else
17025     {
17026       cp_lexer_rollback_tokens (parser->lexer);
17027       pop_deferring_access_checks ();
17028     }
17029   /* Add the context to the front of the free list.  */
17030   context->next = cp_parser_context_free_list;
17031   cp_parser_context_free_list = context;
17032
17033   return !error_occurred;
17034 }
17035
17036 /* Returns true if we are parsing tentatively and are not committed to
17037    this tentative parse.  */
17038
17039 static bool
17040 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17041 {
17042   return (cp_parser_parsing_tentatively (parser)
17043           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17044 }
17045
17046 /* Returns nonzero iff an error has occurred during the most recent
17047    tentative parse.  */
17048
17049 static bool
17050 cp_parser_error_occurred (cp_parser* parser)
17051 {
17052   return (cp_parser_parsing_tentatively (parser)
17053           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17054 }
17055
17056 /* Returns nonzero if GNU extensions are allowed.  */
17057
17058 static bool
17059 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17060 {
17061   return parser->allow_gnu_extensions_p;
17062 }
17063 \f
17064 /* Objective-C++ Productions */
17065
17066
17067 /* Parse an Objective-C expression, which feeds into a primary-expression
17068    above.
17069
17070    objc-expression:
17071      objc-message-expression
17072      objc-string-literal
17073      objc-encode-expression
17074      objc-protocol-expression
17075      objc-selector-expression
17076
17077   Returns a tree representation of the expression.  */
17078
17079 static tree
17080 cp_parser_objc_expression (cp_parser* parser)
17081 {
17082   /* Try to figure out what kind of declaration is present.  */
17083   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17084
17085   switch (kwd->type)
17086     {
17087     case CPP_OPEN_SQUARE:
17088       return cp_parser_objc_message_expression (parser);
17089
17090     case CPP_OBJC_STRING:
17091       kwd = cp_lexer_consume_token (parser->lexer);
17092       return objc_build_string_object (kwd->u.value);
17093
17094     case CPP_KEYWORD:
17095       switch (kwd->keyword)
17096         {
17097         case RID_AT_ENCODE:
17098           return cp_parser_objc_encode_expression (parser);
17099
17100         case RID_AT_PROTOCOL:
17101           return cp_parser_objc_protocol_expression (parser);
17102
17103         case RID_AT_SELECTOR:
17104           return cp_parser_objc_selector_expression (parser);
17105
17106         default:
17107           break;
17108         }
17109     default:
17110       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17111       cp_parser_skip_to_end_of_block_or_statement (parser);
17112     }
17113
17114   return error_mark_node;
17115 }
17116
17117 /* Parse an Objective-C message expression.
17118
17119    objc-message-expression:
17120      [ objc-message-receiver objc-message-args ]
17121
17122    Returns a representation of an Objective-C message.  */
17123
17124 static tree
17125 cp_parser_objc_message_expression (cp_parser* parser)
17126 {
17127   tree receiver, messageargs;
17128
17129   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
17130   receiver = cp_parser_objc_message_receiver (parser);
17131   messageargs = cp_parser_objc_message_args (parser);
17132   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17133
17134   return objc_build_message_expr (build_tree_list (receiver, messageargs));
17135 }
17136
17137 /* Parse an objc-message-receiver.
17138
17139    objc-message-receiver:
17140      expression
17141      simple-type-specifier
17142
17143   Returns a representation of the type or expression.  */
17144
17145 static tree
17146 cp_parser_objc_message_receiver (cp_parser* parser)
17147 {
17148   tree rcv;
17149
17150   /* An Objective-C message receiver may be either (1) a type
17151      or (2) an expression.  */
17152   cp_parser_parse_tentatively (parser);
17153   rcv = cp_parser_expression (parser, false);
17154
17155   if (cp_parser_parse_definitely (parser))
17156     return rcv;
17157
17158   rcv = cp_parser_simple_type_specifier (parser,
17159                                          /*decl_specs=*/NULL,
17160                                          CP_PARSER_FLAGS_NONE);
17161
17162   return objc_get_class_reference (rcv);
17163 }
17164
17165 /* Parse the arguments and selectors comprising an Objective-C message.
17166
17167    objc-message-args:
17168      objc-selector
17169      objc-selector-args
17170      objc-selector-args , objc-comma-args
17171
17172    objc-selector-args:
17173      objc-selector [opt] : assignment-expression
17174      objc-selector-args objc-selector [opt] : assignment-expression
17175
17176    objc-comma-args:
17177      assignment-expression
17178      objc-comma-args , assignment-expression
17179
17180    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17181    selector arguments and TREE_VALUE containing a list of comma
17182    arguments.  */
17183
17184 static tree
17185 cp_parser_objc_message_args (cp_parser* parser)
17186 {
17187   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17188   bool maybe_unary_selector_p = true;
17189   cp_token *token = cp_lexer_peek_token (parser->lexer);
17190
17191   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17192     {
17193       tree selector = NULL_TREE, arg;
17194
17195       if (token->type != CPP_COLON)
17196         selector = cp_parser_objc_selector (parser);
17197
17198       /* Detect if we have a unary selector.  */
17199       if (maybe_unary_selector_p
17200           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17201         return build_tree_list (selector, NULL_TREE);
17202
17203       maybe_unary_selector_p = false;
17204       cp_parser_require (parser, CPP_COLON, "`:'");
17205       arg = cp_parser_assignment_expression (parser, false);
17206
17207       sel_args
17208         = chainon (sel_args,
17209                    build_tree_list (selector, arg));
17210
17211       token = cp_lexer_peek_token (parser->lexer);
17212     }
17213
17214   /* Handle non-selector arguments, if any. */
17215   while (token->type == CPP_COMMA)
17216     {
17217       tree arg;
17218
17219       cp_lexer_consume_token (parser->lexer);
17220       arg = cp_parser_assignment_expression (parser, false);
17221
17222       addl_args
17223         = chainon (addl_args,
17224                    build_tree_list (NULL_TREE, arg));
17225
17226       token = cp_lexer_peek_token (parser->lexer);
17227     }
17228
17229   return build_tree_list (sel_args, addl_args);
17230 }
17231
17232 /* Parse an Objective-C encode expression.
17233
17234    objc-encode-expression:
17235      @encode objc-typename
17236
17237    Returns an encoded representation of the type argument.  */
17238
17239 static tree
17240 cp_parser_objc_encode_expression (cp_parser* parser)
17241 {
17242   tree type;
17243
17244   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
17245   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17246   type = complete_type (cp_parser_type_id (parser));
17247   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17248
17249   if (!type)
17250     {
17251       error ("%<@encode%> must specify a type as an argument");
17252       return error_mark_node;
17253     }
17254
17255   return objc_build_encode_expr (type);
17256 }
17257
17258 /* Parse an Objective-C @defs expression.  */
17259
17260 static tree
17261 cp_parser_objc_defs_expression (cp_parser *parser)
17262 {
17263   tree name;
17264
17265   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
17266   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17267   name = cp_parser_identifier (parser);
17268   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17269
17270   return objc_get_class_ivars (name);
17271 }
17272
17273 /* Parse an Objective-C protocol expression.
17274
17275   objc-protocol-expression:
17276     @protocol ( identifier )
17277
17278   Returns a representation of the protocol expression.  */
17279
17280 static tree
17281 cp_parser_objc_protocol_expression (cp_parser* parser)
17282 {
17283   tree proto;
17284
17285   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17286   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17287   proto = cp_parser_identifier (parser);
17288   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17289
17290   return objc_build_protocol_expr (proto);
17291 }
17292
17293 /* Parse an Objective-C selector expression.
17294
17295    objc-selector-expression:
17296      @selector ( objc-method-signature )
17297
17298    objc-method-signature:
17299      objc-selector
17300      objc-selector-seq
17301
17302    objc-selector-seq:
17303      objc-selector :
17304      objc-selector-seq objc-selector :
17305
17306   Returns a representation of the method selector.  */
17307
17308 static tree
17309 cp_parser_objc_selector_expression (cp_parser* parser)
17310 {
17311   tree sel_seq = NULL_TREE;
17312   bool maybe_unary_selector_p = true;
17313   cp_token *token;
17314
17315   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
17316   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17317   token = cp_lexer_peek_token (parser->lexer);
17318
17319   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17320          || token->type == CPP_SCOPE)
17321     {
17322       tree selector = NULL_TREE;
17323
17324       if (token->type != CPP_COLON
17325           || token->type == CPP_SCOPE)
17326         selector = cp_parser_objc_selector (parser);
17327
17328       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17329           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17330         {
17331           /* Detect if we have a unary selector.  */
17332           if (maybe_unary_selector_p)
17333             {
17334               sel_seq = selector;
17335               goto finish_selector;
17336             }
17337           else
17338             {
17339               cp_parser_error (parser, "expected %<:%>");
17340             }
17341         }
17342       maybe_unary_selector_p = false;
17343       token = cp_lexer_consume_token (parser->lexer);
17344
17345       if (token->type == CPP_SCOPE)
17346         {
17347           sel_seq
17348             = chainon (sel_seq,
17349                        build_tree_list (selector, NULL_TREE));
17350           sel_seq
17351             = chainon (sel_seq,
17352                        build_tree_list (NULL_TREE, NULL_TREE));
17353         }
17354       else
17355         sel_seq
17356           = chainon (sel_seq,
17357                      build_tree_list (selector, NULL_TREE));
17358
17359       token = cp_lexer_peek_token (parser->lexer);
17360     }
17361
17362  finish_selector:
17363   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17364
17365   return objc_build_selector_expr (sel_seq);
17366 }
17367
17368 /* Parse a list of identifiers.
17369
17370    objc-identifier-list:
17371      identifier
17372      objc-identifier-list , identifier
17373
17374    Returns a TREE_LIST of identifier nodes.  */
17375
17376 static tree
17377 cp_parser_objc_identifier_list (cp_parser* parser)
17378 {
17379   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17380   cp_token *sep = cp_lexer_peek_token (parser->lexer);
17381
17382   while (sep->type == CPP_COMMA)
17383     {
17384       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17385       list = chainon (list,
17386                       build_tree_list (NULL_TREE,
17387                                        cp_parser_identifier (parser)));
17388       sep = cp_lexer_peek_token (parser->lexer);
17389     }
17390
17391   return list;
17392 }
17393
17394 /* Parse an Objective-C alias declaration.
17395
17396    objc-alias-declaration:
17397      @compatibility_alias identifier identifier ;
17398
17399    This function registers the alias mapping with the Objective-C front-end.
17400    It returns nothing.  */
17401
17402 static void
17403 cp_parser_objc_alias_declaration (cp_parser* parser)
17404 {
17405   tree alias, orig;
17406
17407   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17408   alias = cp_parser_identifier (parser);
17409   orig = cp_parser_identifier (parser);
17410   objc_declare_alias (alias, orig);
17411   cp_parser_consume_semicolon_at_end_of_statement (parser);
17412 }
17413
17414 /* Parse an Objective-C class forward-declaration.
17415
17416    objc-class-declaration:
17417      @class objc-identifier-list ;
17418
17419    The function registers the forward declarations with the Objective-C
17420    front-end.  It returns nothing.  */
17421
17422 static void
17423 cp_parser_objc_class_declaration (cp_parser* parser)
17424 {
17425   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17426   objc_declare_class (cp_parser_objc_identifier_list (parser));
17427   cp_parser_consume_semicolon_at_end_of_statement (parser);
17428 }
17429
17430 /* Parse a list of Objective-C protocol references.
17431
17432    objc-protocol-refs-opt:
17433      objc-protocol-refs [opt]
17434
17435    objc-protocol-refs:
17436      < objc-identifier-list >
17437
17438    Returns a TREE_LIST of identifiers, if any.  */
17439
17440 static tree
17441 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17442 {
17443   tree protorefs = NULL_TREE;
17444
17445   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17446     {
17447       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17448       protorefs = cp_parser_objc_identifier_list (parser);
17449       cp_parser_require (parser, CPP_GREATER, "`>'");
17450     }
17451
17452   return protorefs;
17453 }
17454
17455 /* Parse a Objective-C visibility specification.  */
17456
17457 static void
17458 cp_parser_objc_visibility_spec (cp_parser* parser)
17459 {
17460   cp_token *vis = cp_lexer_peek_token (parser->lexer);
17461
17462   switch (vis->keyword)
17463     {
17464     case RID_AT_PRIVATE:
17465       objc_set_visibility (2);
17466       break;
17467     case RID_AT_PROTECTED:
17468       objc_set_visibility (0);
17469       break;
17470     case RID_AT_PUBLIC:
17471       objc_set_visibility (1);
17472       break;
17473     default:
17474       return;
17475     }
17476
17477   /* Eat '@private'/'@protected'/'@public'.  */
17478   cp_lexer_consume_token (parser->lexer);
17479 }
17480
17481 /* Parse an Objective-C method type.  */
17482
17483 static void
17484 cp_parser_objc_method_type (cp_parser* parser)
17485 {
17486   objc_set_method_type
17487    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17488     ? PLUS_EXPR
17489     : MINUS_EXPR);
17490 }
17491
17492 /* Parse an Objective-C protocol qualifier.  */
17493
17494 static tree
17495 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17496 {
17497   tree quals = NULL_TREE, node;
17498   cp_token *token = cp_lexer_peek_token (parser->lexer);
17499
17500   node = token->u.value;
17501
17502   while (node && TREE_CODE (node) == IDENTIFIER_NODE
17503          && (node == ridpointers [(int) RID_IN]
17504              || node == ridpointers [(int) RID_OUT]
17505              || node == ridpointers [(int) RID_INOUT]
17506              || node == ridpointers [(int) RID_BYCOPY]
17507              || node == ridpointers [(int) RID_BYREF]
17508              || node == ridpointers [(int) RID_ONEWAY]))
17509     {
17510       quals = tree_cons (NULL_TREE, node, quals);
17511       cp_lexer_consume_token (parser->lexer);
17512       token = cp_lexer_peek_token (parser->lexer);
17513       node = token->u.value;
17514     }
17515
17516   return quals;
17517 }
17518
17519 /* Parse an Objective-C typename.  */
17520
17521 static tree
17522 cp_parser_objc_typename (cp_parser* parser)
17523 {
17524   tree typename = NULL_TREE;
17525
17526   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17527     {
17528       tree proto_quals, cp_type = NULL_TREE;
17529
17530       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17531       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17532
17533       /* An ObjC type name may consist of just protocol qualifiers, in which
17534          case the type shall default to 'id'.  */
17535       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17536         cp_type = cp_parser_type_id (parser);
17537
17538       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17539       typename = build_tree_list (proto_quals, cp_type);
17540     }
17541
17542   return typename;
17543 }
17544
17545 /* Check to see if TYPE refers to an Objective-C selector name.  */
17546
17547 static bool
17548 cp_parser_objc_selector_p (enum cpp_ttype type)
17549 {
17550   return (type == CPP_NAME || type == CPP_KEYWORD
17551           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17552           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17553           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17554           || type == CPP_XOR || type == CPP_XOR_EQ);
17555 }
17556
17557 /* Parse an Objective-C selector.  */
17558
17559 static tree
17560 cp_parser_objc_selector (cp_parser* parser)
17561 {
17562   cp_token *token = cp_lexer_consume_token (parser->lexer);
17563
17564   if (!cp_parser_objc_selector_p (token->type))
17565     {
17566       error ("invalid Objective-C++ selector name");
17567       return error_mark_node;
17568     }
17569
17570   /* C++ operator names are allowed to appear in ObjC selectors.  */
17571   switch (token->type)
17572     {
17573     case CPP_AND_AND: return get_identifier ("and");
17574     case CPP_AND_EQ: return get_identifier ("and_eq");
17575     case CPP_AND: return get_identifier ("bitand");
17576     case CPP_OR: return get_identifier ("bitor");
17577     case CPP_COMPL: return get_identifier ("compl");
17578     case CPP_NOT: return get_identifier ("not");
17579     case CPP_NOT_EQ: return get_identifier ("not_eq");
17580     case CPP_OR_OR: return get_identifier ("or");
17581     case CPP_OR_EQ: return get_identifier ("or_eq");
17582     case CPP_XOR: return get_identifier ("xor");
17583     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17584     default: return token->u.value;
17585     }
17586 }
17587
17588 /* Parse an Objective-C params list.  */
17589
17590 static tree
17591 cp_parser_objc_method_keyword_params (cp_parser* parser)
17592 {
17593   tree params = NULL_TREE;
17594   bool maybe_unary_selector_p = true;
17595   cp_token *token = cp_lexer_peek_token (parser->lexer);
17596
17597   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17598     {
17599       tree selector = NULL_TREE, typename, identifier;
17600
17601       if (token->type != CPP_COLON)
17602         selector = cp_parser_objc_selector (parser);
17603
17604       /* Detect if we have a unary selector.  */
17605       if (maybe_unary_selector_p
17606           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17607         return selector;
17608
17609       maybe_unary_selector_p = false;
17610       cp_parser_require (parser, CPP_COLON, "`:'");
17611       typename = cp_parser_objc_typename (parser);
17612       identifier = cp_parser_identifier (parser);
17613
17614       params
17615         = chainon (params,
17616                    objc_build_keyword_decl (selector,
17617                                             typename,
17618                                             identifier));
17619
17620       token = cp_lexer_peek_token (parser->lexer);
17621     }
17622
17623   return params;
17624 }
17625
17626 /* Parse the non-keyword Objective-C params.  */
17627
17628 static tree
17629 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17630 {
17631   tree params = make_node (TREE_LIST);
17632   cp_token *token = cp_lexer_peek_token (parser->lexer);
17633   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17634
17635   while (token->type == CPP_COMMA)
17636     {
17637       cp_parameter_declarator *parmdecl;
17638       tree parm;
17639
17640       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17641       token = cp_lexer_peek_token (parser->lexer);
17642
17643       if (token->type == CPP_ELLIPSIS)
17644         {
17645           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17646           *ellipsisp = true;
17647           break;
17648         }
17649
17650       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17651       parm = grokdeclarator (parmdecl->declarator,
17652                              &parmdecl->decl_specifiers,
17653                              PARM, /*initialized=*/0,
17654                              /*attrlist=*/NULL);
17655
17656       chainon (params, build_tree_list (NULL_TREE, parm));
17657       token = cp_lexer_peek_token (parser->lexer);
17658     }
17659
17660   return params;
17661 }
17662
17663 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17664
17665 static void
17666 cp_parser_objc_interstitial_code (cp_parser* parser)
17667 {
17668   cp_token *token = cp_lexer_peek_token (parser->lexer);
17669
17670   /* If the next token is `extern' and the following token is a string
17671      literal, then we have a linkage specification.  */
17672   if (token->keyword == RID_EXTERN
17673       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17674     cp_parser_linkage_specification (parser);
17675   /* Handle #pragma, if any.  */
17676   else if (token->type == CPP_PRAGMA)
17677     cp_parser_pragma (parser, pragma_external);
17678   /* Allow stray semicolons.  */
17679   else if (token->type == CPP_SEMICOLON)
17680     cp_lexer_consume_token (parser->lexer);
17681   /* Finally, try to parse a block-declaration, or a function-definition.  */
17682   else
17683     cp_parser_block_declaration (parser, /*statement_p=*/false);
17684 }
17685
17686 /* Parse a method signature.  */
17687
17688 static tree
17689 cp_parser_objc_method_signature (cp_parser* parser)
17690 {
17691   tree rettype, kwdparms, optparms;
17692   bool ellipsis = false;
17693
17694   cp_parser_objc_method_type (parser);
17695   rettype = cp_parser_objc_typename (parser);
17696   kwdparms = cp_parser_objc_method_keyword_params (parser);
17697   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17698
17699   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17700 }
17701
17702 /* Pars an Objective-C method prototype list.  */
17703
17704 static void
17705 cp_parser_objc_method_prototype_list (cp_parser* parser)
17706 {
17707   cp_token *token = cp_lexer_peek_token (parser->lexer);
17708
17709   while (token->keyword != RID_AT_END)
17710     {
17711       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17712         {
17713           objc_add_method_declaration
17714            (cp_parser_objc_method_signature (parser));
17715           cp_parser_consume_semicolon_at_end_of_statement (parser);
17716         }
17717       else
17718         /* Allow for interspersed non-ObjC++ code.  */
17719         cp_parser_objc_interstitial_code (parser);
17720
17721       token = cp_lexer_peek_token (parser->lexer);
17722     }
17723
17724   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17725   objc_finish_interface ();
17726 }
17727
17728 /* Parse an Objective-C method definition list.  */
17729
17730 static void
17731 cp_parser_objc_method_definition_list (cp_parser* parser)
17732 {
17733   cp_token *token = cp_lexer_peek_token (parser->lexer);
17734
17735   while (token->keyword != RID_AT_END)
17736     {
17737       tree meth;
17738
17739       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17740         {
17741           push_deferring_access_checks (dk_deferred);
17742           objc_start_method_definition
17743            (cp_parser_objc_method_signature (parser));
17744
17745           /* For historical reasons, we accept an optional semicolon.  */
17746           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17747             cp_lexer_consume_token (parser->lexer);
17748
17749           perform_deferred_access_checks ();
17750           stop_deferring_access_checks ();
17751           meth = cp_parser_function_definition_after_declarator (parser,
17752                                                                  false);
17753           pop_deferring_access_checks ();
17754           objc_finish_method_definition (meth);
17755         }
17756       else
17757         /* Allow for interspersed non-ObjC++ code.  */
17758         cp_parser_objc_interstitial_code (parser);
17759
17760       token = cp_lexer_peek_token (parser->lexer);
17761     }
17762
17763   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17764   objc_finish_implementation ();
17765 }
17766
17767 /* Parse Objective-C ivars.  */
17768
17769 static void
17770 cp_parser_objc_class_ivars (cp_parser* parser)
17771 {
17772   cp_token *token = cp_lexer_peek_token (parser->lexer);
17773
17774   if (token->type != CPP_OPEN_BRACE)
17775     return;     /* No ivars specified.  */
17776
17777   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17778   token = cp_lexer_peek_token (parser->lexer);
17779
17780   while (token->type != CPP_CLOSE_BRACE)
17781     {
17782       cp_decl_specifier_seq declspecs;
17783       int decl_class_or_enum_p;
17784       tree prefix_attributes;
17785
17786       cp_parser_objc_visibility_spec (parser);
17787
17788       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17789         break;
17790
17791       cp_parser_decl_specifier_seq (parser,
17792                                     CP_PARSER_FLAGS_OPTIONAL,
17793                                     &declspecs,
17794                                     &decl_class_or_enum_p);
17795       prefix_attributes = declspecs.attributes;
17796       declspecs.attributes = NULL_TREE;
17797
17798       /* Keep going until we hit the `;' at the end of the
17799          declaration.  */
17800       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17801         {
17802           tree width = NULL_TREE, attributes, first_attribute, decl;
17803           cp_declarator *declarator = NULL;
17804           int ctor_dtor_or_conv_p;
17805
17806           /* Check for a (possibly unnamed) bitfield declaration.  */
17807           token = cp_lexer_peek_token (parser->lexer);
17808           if (token->type == CPP_COLON)
17809             goto eat_colon;
17810
17811           if (token->type == CPP_NAME
17812               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17813                   == CPP_COLON))
17814             {
17815               /* Get the name of the bitfield.  */
17816               declarator = make_id_declarator (NULL_TREE,
17817                                                cp_parser_identifier (parser),
17818                                                sfk_none);
17819
17820              eat_colon:
17821               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17822               /* Get the width of the bitfield.  */
17823               width
17824                 = cp_parser_constant_expression (parser,
17825                                                  /*allow_non_constant=*/false,
17826                                                  NULL);
17827             }
17828           else
17829             {
17830               /* Parse the declarator.  */
17831               declarator
17832                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17833                                         &ctor_dtor_or_conv_p,
17834                                         /*parenthesized_p=*/NULL,
17835                                         /*member_p=*/false);
17836             }
17837
17838           /* Look for attributes that apply to the ivar.  */
17839           attributes = cp_parser_attributes_opt (parser);
17840           /* Remember which attributes are prefix attributes and
17841              which are not.  */
17842           first_attribute = attributes;
17843           /* Combine the attributes.  */
17844           attributes = chainon (prefix_attributes, attributes);
17845
17846           if (width)
17847             {
17848               /* Create the bitfield declaration.  */
17849               decl = grokbitfield (declarator, &declspecs, width);
17850               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17851             }
17852           else
17853             decl = grokfield (declarator, &declspecs,
17854                               NULL_TREE, /*init_const_expr_p=*/false,
17855                               NULL_TREE, attributes);
17856
17857           /* Add the instance variable.  */
17858           objc_add_instance_variable (decl);
17859
17860           /* Reset PREFIX_ATTRIBUTES.  */
17861           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17862             attributes = TREE_CHAIN (attributes);
17863           if (attributes)
17864             TREE_CHAIN (attributes) = NULL_TREE;
17865
17866           token = cp_lexer_peek_token (parser->lexer);
17867
17868           if (token->type == CPP_COMMA)
17869             {
17870               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17871               continue;
17872             }
17873           break;
17874         }
17875
17876       cp_parser_consume_semicolon_at_end_of_statement (parser);
17877       token = cp_lexer_peek_token (parser->lexer);
17878     }
17879
17880   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17881   /* For historical reasons, we accept an optional semicolon.  */
17882   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17883     cp_lexer_consume_token (parser->lexer);
17884 }
17885
17886 /* Parse an Objective-C protocol declaration.  */
17887
17888 static void
17889 cp_parser_objc_protocol_declaration (cp_parser* parser)
17890 {
17891   tree proto, protorefs;
17892   cp_token *tok;
17893
17894   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17895   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17896     {
17897       error ("identifier expected after %<@protocol%>");
17898       goto finish;
17899     }
17900
17901   /* See if we have a forward declaration or a definition.  */
17902   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17903
17904   /* Try a forward declaration first.  */
17905   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17906     {
17907       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17908      finish:
17909       cp_parser_consume_semicolon_at_end_of_statement (parser);
17910     }
17911
17912   /* Ok, we got a full-fledged definition (or at least should).  */
17913   else
17914     {
17915       proto = cp_parser_identifier (parser);
17916       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17917       objc_start_protocol (proto, protorefs);
17918       cp_parser_objc_method_prototype_list (parser);
17919     }
17920 }
17921
17922 /* Parse an Objective-C superclass or category.  */
17923
17924 static void
17925 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17926                                                           tree *categ)
17927 {
17928   cp_token *next = cp_lexer_peek_token (parser->lexer);
17929
17930   *super = *categ = NULL_TREE;
17931   if (next->type == CPP_COLON)
17932     {
17933       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17934       *super = cp_parser_identifier (parser);
17935     }
17936   else if (next->type == CPP_OPEN_PAREN)
17937     {
17938       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17939       *categ = cp_parser_identifier (parser);
17940       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17941     }
17942 }
17943
17944 /* Parse an Objective-C class interface.  */
17945
17946 static void
17947 cp_parser_objc_class_interface (cp_parser* parser)
17948 {
17949   tree name, super, categ, protos;
17950
17951   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17952   name = cp_parser_identifier (parser);
17953   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17954   protos = cp_parser_objc_protocol_refs_opt (parser);
17955
17956   /* We have either a class or a category on our hands.  */
17957   if (categ)
17958     objc_start_category_interface (name, categ, protos);
17959   else
17960     {
17961       objc_start_class_interface (name, super, protos);
17962       /* Handle instance variable declarations, if any.  */
17963       cp_parser_objc_class_ivars (parser);
17964       objc_continue_interface ();
17965     }
17966
17967   cp_parser_objc_method_prototype_list (parser);
17968 }
17969
17970 /* Parse an Objective-C class implementation.  */
17971
17972 static void
17973 cp_parser_objc_class_implementation (cp_parser* parser)
17974 {
17975   tree name, super, categ;
17976
17977   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17978   name = cp_parser_identifier (parser);
17979   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17980
17981   /* We have either a class or a category on our hands.  */
17982   if (categ)
17983     objc_start_category_implementation (name, categ);
17984   else
17985     {
17986       objc_start_class_implementation (name, super);
17987       /* Handle instance variable declarations, if any.  */
17988       cp_parser_objc_class_ivars (parser);
17989       objc_continue_implementation ();
17990     }
17991
17992   cp_parser_objc_method_definition_list (parser);
17993 }
17994
17995 /* Consume the @end token and finish off the implementation.  */
17996
17997 static void
17998 cp_parser_objc_end_implementation (cp_parser* parser)
17999 {
18000   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18001   objc_finish_implementation ();
18002 }
18003
18004 /* Parse an Objective-C declaration.  */
18005
18006 static void
18007 cp_parser_objc_declaration (cp_parser* parser)
18008 {
18009   /* Try to figure out what kind of declaration is present.  */
18010   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18011
18012   switch (kwd->keyword)
18013     {
18014     case RID_AT_ALIAS:
18015       cp_parser_objc_alias_declaration (parser);
18016       break;
18017     case RID_AT_CLASS:
18018       cp_parser_objc_class_declaration (parser);
18019       break;
18020     case RID_AT_PROTOCOL:
18021       cp_parser_objc_protocol_declaration (parser);
18022       break;
18023     case RID_AT_INTERFACE:
18024       cp_parser_objc_class_interface (parser);
18025       break;
18026     case RID_AT_IMPLEMENTATION:
18027       cp_parser_objc_class_implementation (parser);
18028       break;
18029     case RID_AT_END:
18030       cp_parser_objc_end_implementation (parser);
18031       break;
18032     default:
18033       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18034       cp_parser_skip_to_end_of_block_or_statement (parser);
18035     }
18036 }
18037
18038 /* Parse an Objective-C try-catch-finally statement.
18039
18040    objc-try-catch-finally-stmt:
18041      @try compound-statement objc-catch-clause-seq [opt]
18042        objc-finally-clause [opt]
18043
18044    objc-catch-clause-seq:
18045      objc-catch-clause objc-catch-clause-seq [opt]
18046
18047    objc-catch-clause:
18048      @catch ( exception-declaration ) compound-statement
18049
18050    objc-finally-clause
18051      @finally compound-statement
18052
18053    Returns NULL_TREE.  */
18054
18055 static tree
18056 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18057   location_t location;
18058   tree stmt;
18059
18060   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18061   location = cp_lexer_peek_token (parser->lexer)->location;
18062   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18063      node, lest it get absorbed into the surrounding block.  */
18064   stmt = push_stmt_list ();
18065   cp_parser_compound_statement (parser, NULL, false);
18066   objc_begin_try_stmt (location, pop_stmt_list (stmt));
18067
18068   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18069     {
18070       cp_parameter_declarator *parmdecl;
18071       tree parm;
18072
18073       cp_lexer_consume_token (parser->lexer);
18074       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18075       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18076       parm = grokdeclarator (parmdecl->declarator,
18077                              &parmdecl->decl_specifiers,
18078                              PARM, /*initialized=*/0,
18079                              /*attrlist=*/NULL);
18080       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18081       objc_begin_catch_clause (parm);
18082       cp_parser_compound_statement (parser, NULL, false);
18083       objc_finish_catch_clause ();
18084     }
18085
18086   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18087     {
18088       cp_lexer_consume_token (parser->lexer);
18089       location = cp_lexer_peek_token (parser->lexer)->location;
18090       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18091          node, lest it get absorbed into the surrounding block.  */
18092       stmt = push_stmt_list ();
18093       cp_parser_compound_statement (parser, NULL, false);
18094       objc_build_finally_clause (location, pop_stmt_list (stmt));
18095     }
18096
18097   return objc_finish_try_stmt ();
18098 }
18099
18100 /* Parse an Objective-C synchronized statement.
18101
18102    objc-synchronized-stmt:
18103      @synchronized ( expression ) compound-statement
18104
18105    Returns NULL_TREE.  */
18106
18107 static tree
18108 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18109   location_t location;
18110   tree lock, stmt;
18111
18112   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18113
18114   location = cp_lexer_peek_token (parser->lexer)->location;
18115   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18116   lock = cp_parser_expression (parser, false);
18117   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18118
18119   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18120      node, lest it get absorbed into the surrounding block.  */
18121   stmt = push_stmt_list ();
18122   cp_parser_compound_statement (parser, NULL, false);
18123
18124   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18125 }
18126
18127 /* Parse an Objective-C throw statement.
18128
18129    objc-throw-stmt:
18130      @throw assignment-expression [opt] ;
18131
18132    Returns a constructed '@throw' statement.  */
18133
18134 static tree
18135 cp_parser_objc_throw_statement (cp_parser *parser) {
18136   tree expr = NULL_TREE;
18137
18138   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18139
18140   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18141     expr = cp_parser_assignment_expression (parser, false);
18142
18143   cp_parser_consume_semicolon_at_end_of_statement (parser);
18144
18145   return objc_build_throw_stmt (expr);
18146 }
18147
18148 /* Parse an Objective-C statement.  */
18149
18150 static tree
18151 cp_parser_objc_statement (cp_parser * parser) {
18152   /* Try to figure out what kind of declaration is present.  */
18153   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18154
18155   switch (kwd->keyword)
18156     {
18157     case RID_AT_TRY:
18158       return cp_parser_objc_try_catch_finally_statement (parser);
18159     case RID_AT_SYNCHRONIZED:
18160       return cp_parser_objc_synchronized_statement (parser);
18161     case RID_AT_THROW:
18162       return cp_parser_objc_throw_statement (parser);
18163     default:
18164       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18165       cp_parser_skip_to_end_of_block_or_statement (parser);
18166     }
18167
18168   return error_mark_node;
18169 }
18170 \f
18171 /* OpenMP 2.5 parsing routines.  */
18172
18173 /* Returns name of the next clause.
18174    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18175    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
18176    returned and the token is consumed.  */
18177
18178 static pragma_omp_clause
18179 cp_parser_omp_clause_name (cp_parser *parser)
18180 {
18181   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18182
18183   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18184     result = PRAGMA_OMP_CLAUSE_IF;
18185   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18186     result = PRAGMA_OMP_CLAUSE_DEFAULT;
18187   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18188     result = PRAGMA_OMP_CLAUSE_PRIVATE;
18189   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18190     {
18191       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18192       const char *p = IDENTIFIER_POINTER (id);
18193
18194       switch (p[0])
18195         {
18196         case 'c':
18197           if (!strcmp ("copyin", p))
18198             result = PRAGMA_OMP_CLAUSE_COPYIN;
18199           else if (!strcmp ("copyprivate", p))
18200             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18201           break;
18202         case 'f':
18203           if (!strcmp ("firstprivate", p))
18204             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18205           break;
18206         case 'l':
18207           if (!strcmp ("lastprivate", p))
18208             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18209           break;
18210         case 'n':
18211           if (!strcmp ("nowait", p))
18212             result = PRAGMA_OMP_CLAUSE_NOWAIT;
18213           else if (!strcmp ("num_threads", p))
18214             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18215           break;
18216         case 'o':
18217           if (!strcmp ("ordered", p))
18218             result = PRAGMA_OMP_CLAUSE_ORDERED;
18219           break;
18220         case 'r':
18221           if (!strcmp ("reduction", p))
18222             result = PRAGMA_OMP_CLAUSE_REDUCTION;
18223           break;
18224         case 's':
18225           if (!strcmp ("schedule", p))
18226             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18227           else if (!strcmp ("shared", p))
18228             result = PRAGMA_OMP_CLAUSE_SHARED;
18229           break;
18230         }
18231     }
18232
18233   if (result != PRAGMA_OMP_CLAUSE_NONE)
18234     cp_lexer_consume_token (parser->lexer);
18235
18236   return result;
18237 }
18238
18239 /* Validate that a clause of the given type does not already exist.  */
18240
18241 static void
18242 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18243 {
18244   tree c;
18245
18246   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18247     if (OMP_CLAUSE_CODE (c) == code)
18248       {
18249         error ("too many %qs clauses", name);
18250         break;
18251       }
18252 }
18253
18254 /* OpenMP 2.5:
18255    variable-list:
18256      identifier
18257      variable-list , identifier
18258
18259    In addition, we match a closing parenthesis.  An opening parenthesis
18260    will have been consumed by the caller.
18261
18262    If KIND is nonzero, create the appropriate node and install the decl
18263    in OMP_CLAUSE_DECL and add the node to the head of the list.
18264
18265    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18266    return the list created.  */
18267
18268 static tree
18269 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18270                                 tree list)
18271 {
18272   while (1)
18273     {
18274       tree name, decl;
18275
18276       name = cp_parser_id_expression (parser, /*template_p=*/false,
18277                                       /*check_dependency_p=*/true,
18278                                       /*template_p=*/NULL,
18279                                       /*declarator_p=*/false,
18280                                       /*optional_p=*/false);
18281       if (name == error_mark_node)
18282         goto skip_comma;
18283
18284       decl = cp_parser_lookup_name_simple (parser, name);
18285       if (decl == error_mark_node)
18286         cp_parser_name_lookup_error (parser, name, decl, NULL);
18287       else if (kind != 0)
18288         {
18289           tree u = build_omp_clause (kind);
18290           OMP_CLAUSE_DECL (u) = decl;
18291           OMP_CLAUSE_CHAIN (u) = list;
18292           list = u;
18293         }
18294       else
18295         list = tree_cons (decl, NULL_TREE, list);
18296
18297     get_comma:
18298       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18299         break;
18300       cp_lexer_consume_token (parser->lexer);
18301     }
18302
18303   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18304     {
18305       int ending;
18306
18307       /* Try to resync to an unnested comma.  Copied from
18308          cp_parser_parenthesized_expression_list.  */
18309     skip_comma:
18310       ending = cp_parser_skip_to_closing_parenthesis (parser,
18311                                                       /*recovering=*/true,
18312                                                       /*or_comma=*/true,
18313                                                       /*consume_paren=*/true);
18314       if (ending < 0)
18315         goto get_comma;
18316     }
18317
18318   return list;
18319 }
18320
18321 /* Similarly, but expect leading and trailing parenthesis.  This is a very
18322    common case for omp clauses.  */
18323
18324 static tree
18325 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18326 {
18327   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18328     return cp_parser_omp_var_list_no_open (parser, kind, list);
18329   return list;
18330 }
18331
18332 /* OpenMP 2.5:
18333    default ( shared | none ) */
18334
18335 static tree
18336 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18337 {
18338   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18339   tree c;
18340
18341   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18342     return list;
18343   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18344     {
18345       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18346       const char *p = IDENTIFIER_POINTER (id);
18347
18348       switch (p[0])
18349         {
18350         case 'n':
18351           if (strcmp ("none", p) != 0)
18352             goto invalid_kind;
18353           kind = OMP_CLAUSE_DEFAULT_NONE;
18354           break;
18355
18356         case 's':
18357           if (strcmp ("shared", p) != 0)
18358             goto invalid_kind;
18359           kind = OMP_CLAUSE_DEFAULT_SHARED;
18360           break;
18361
18362         default:
18363           goto invalid_kind;
18364         }
18365
18366       cp_lexer_consume_token (parser->lexer);
18367     }
18368   else
18369     {
18370     invalid_kind:
18371       cp_parser_error (parser, "expected %<none%> or %<shared%>");
18372     }
18373
18374   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18375     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18376                                            /*or_comma=*/false,
18377                                            /*consume_paren=*/true);
18378
18379   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18380     return list;
18381
18382   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18383   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18384   OMP_CLAUSE_CHAIN (c) = list;
18385   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18386
18387   return c;
18388 }
18389
18390 /* OpenMP 2.5:
18391    if ( expression ) */
18392
18393 static tree
18394 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18395 {
18396   tree t, c;
18397
18398   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18399     return list;
18400
18401   t = cp_parser_condition (parser);
18402
18403   if (t == error_mark_node
18404       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18405     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18406                                            /*or_comma=*/false,
18407                                            /*consume_paren=*/true);
18408
18409   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18410
18411   c = build_omp_clause (OMP_CLAUSE_IF);
18412   OMP_CLAUSE_IF_EXPR (c) = t;
18413   OMP_CLAUSE_CHAIN (c) = list;
18414
18415   return c;
18416 }
18417
18418 /* OpenMP 2.5:
18419    nowait */
18420
18421 static tree
18422 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18423 {
18424   tree c;
18425
18426   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18427
18428   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18429   OMP_CLAUSE_CHAIN (c) = list;
18430   return c;
18431 }
18432
18433 /* OpenMP 2.5:
18434    num_threads ( expression ) */
18435
18436 static tree
18437 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18438 {
18439   tree t, c;
18440
18441   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18442     return list;
18443
18444   t = cp_parser_expression (parser, false);
18445
18446   if (t == error_mark_node
18447       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18448     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18449                                            /*or_comma=*/false,
18450                                            /*consume_paren=*/true);
18451
18452   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18453
18454   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18455   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18456   OMP_CLAUSE_CHAIN (c) = list;
18457
18458   return c;
18459 }
18460
18461 /* OpenMP 2.5:
18462    ordered */
18463
18464 static tree
18465 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18466 {
18467   tree c;
18468
18469   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18470
18471   c = build_omp_clause (OMP_CLAUSE_ORDERED);
18472   OMP_CLAUSE_CHAIN (c) = list;
18473   return c;
18474 }
18475
18476 /* OpenMP 2.5:
18477    reduction ( reduction-operator : variable-list )
18478
18479    reduction-operator:
18480      One of: + * - & ^ | && || */
18481
18482 static tree
18483 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18484 {
18485   enum tree_code code;
18486   tree nlist, c;
18487
18488   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18489     return list;
18490
18491   switch (cp_lexer_peek_token (parser->lexer)->type)
18492     {
18493     case CPP_PLUS:
18494       code = PLUS_EXPR;
18495       break;
18496     case CPP_MULT:
18497       code = MULT_EXPR;
18498       break;
18499     case CPP_MINUS:
18500       code = MINUS_EXPR;
18501       break;
18502     case CPP_AND:
18503       code = BIT_AND_EXPR;
18504       break;
18505     case CPP_XOR:
18506       code = BIT_XOR_EXPR;
18507       break;
18508     case CPP_OR:
18509       code = BIT_IOR_EXPR;
18510       break;
18511     case CPP_AND_AND:
18512       code = TRUTH_ANDIF_EXPR;
18513       break;
18514     case CPP_OR_OR:
18515       code = TRUTH_ORIF_EXPR;
18516       break;
18517     default:
18518       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18519     resync_fail:
18520       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18521                                              /*or_comma=*/false,
18522                                              /*consume_paren=*/true);
18523       return list;
18524     }
18525   cp_lexer_consume_token (parser->lexer);
18526
18527   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18528     goto resync_fail;
18529
18530   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18531   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18532     OMP_CLAUSE_REDUCTION_CODE (c) = code;
18533
18534   return nlist;
18535 }
18536
18537 /* OpenMP 2.5:
18538    schedule ( schedule-kind )
18539    schedule ( schedule-kind , expression )
18540
18541    schedule-kind:
18542      static | dynamic | guided | runtime  */
18543
18544 static tree
18545 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18546 {
18547   tree c, t;
18548
18549   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18550     return list;
18551
18552   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18553
18554   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18555     {
18556       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18557       const char *p = IDENTIFIER_POINTER (id);
18558
18559       switch (p[0])
18560         {
18561         case 'd':
18562           if (strcmp ("dynamic", p) != 0)
18563             goto invalid_kind;
18564           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18565           break;
18566
18567         case 'g':
18568           if (strcmp ("guided", p) != 0)
18569             goto invalid_kind;
18570           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18571           break;
18572
18573         case 'r':
18574           if (strcmp ("runtime", p) != 0)
18575             goto invalid_kind;
18576           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18577           break;
18578
18579         default:
18580           goto invalid_kind;
18581         }
18582     }
18583   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18584     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18585   else
18586     goto invalid_kind;
18587   cp_lexer_consume_token (parser->lexer);
18588
18589   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18590     {
18591       cp_lexer_consume_token (parser->lexer);
18592
18593       t = cp_parser_assignment_expression (parser, false);
18594
18595       if (t == error_mark_node)
18596         goto resync_fail;
18597       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18598         error ("schedule %<runtime%> does not take "
18599                "a %<chunk_size%> parameter");
18600       else
18601         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18602
18603       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18604         goto resync_fail;
18605     }
18606   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18607     goto resync_fail;
18608
18609   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18610   OMP_CLAUSE_CHAIN (c) = list;
18611   return c;
18612
18613  invalid_kind:
18614   cp_parser_error (parser, "invalid schedule kind");
18615  resync_fail:
18616   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18617                                          /*or_comma=*/false,
18618                                          /*consume_paren=*/true);
18619   return list;
18620 }
18621
18622 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
18623    is a bitmask in MASK.  Return the list of clauses found; the result
18624    of clause default goes in *pdefault.  */
18625
18626 static tree
18627 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18628                            const char *where, cp_token *pragma_tok)
18629 {
18630   tree clauses = NULL;
18631
18632   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18633     {
18634       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18635       const char *c_name;
18636       tree prev = clauses;
18637
18638       switch (c_kind)
18639         {
18640         case PRAGMA_OMP_CLAUSE_COPYIN:
18641           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18642           c_name = "copyin";
18643           break;
18644         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18645           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18646                                             clauses);
18647           c_name = "copyprivate";
18648           break;
18649         case PRAGMA_OMP_CLAUSE_DEFAULT:
18650           clauses = cp_parser_omp_clause_default (parser, clauses);
18651           c_name = "default";
18652           break;
18653         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18654           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18655                                             clauses);
18656           c_name = "firstprivate";
18657           break;
18658         case PRAGMA_OMP_CLAUSE_IF:
18659           clauses = cp_parser_omp_clause_if (parser, clauses);
18660           c_name = "if";
18661           break;
18662         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18663           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18664                                             clauses);
18665           c_name = "lastprivate";
18666           break;
18667         case PRAGMA_OMP_CLAUSE_NOWAIT:
18668           clauses = cp_parser_omp_clause_nowait (parser, clauses);
18669           c_name = "nowait";
18670           break;
18671         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18672           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18673           c_name = "num_threads";
18674           break;
18675         case PRAGMA_OMP_CLAUSE_ORDERED:
18676           clauses = cp_parser_omp_clause_ordered (parser, clauses);
18677           c_name = "ordered";
18678           break;
18679         case PRAGMA_OMP_CLAUSE_PRIVATE:
18680           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18681                                             clauses);
18682           c_name = "private";
18683           break;
18684         case PRAGMA_OMP_CLAUSE_REDUCTION:
18685           clauses = cp_parser_omp_clause_reduction (parser, clauses);
18686           c_name = "reduction";
18687           break;
18688         case PRAGMA_OMP_CLAUSE_SCHEDULE:
18689           clauses = cp_parser_omp_clause_schedule (parser, clauses);
18690           c_name = "schedule";
18691           break;
18692         case PRAGMA_OMP_CLAUSE_SHARED:
18693           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18694                                             clauses);
18695           c_name = "shared";
18696           break;
18697         default:
18698           cp_parser_error (parser, "expected %<#pragma omp%> clause");
18699           goto saw_error;
18700         }
18701
18702       if (((mask >> c_kind) & 1) == 0)
18703         {
18704           /* Remove the invalid clause(s) from the list to avoid
18705              confusing the rest of the compiler.  */
18706           clauses = prev;
18707           error ("%qs is not valid for %qs", c_name, where);
18708         }
18709     }
18710  saw_error:
18711   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18712   return finish_omp_clauses (clauses);
18713 }
18714
18715 /* OpenMP 2.5:
18716    structured-block:
18717      statement
18718
18719    In practice, we're also interested in adding the statement to an
18720    outer node.  So it is convenient if we work around the fact that
18721    cp_parser_statement calls add_stmt.  */
18722
18723 static unsigned
18724 cp_parser_begin_omp_structured_block (cp_parser *parser)
18725 {
18726   unsigned save = parser->in_statement;
18727
18728   /* Only move the values to IN_OMP_BLOCK if they weren't false.
18729      This preserves the "not within loop or switch" style error messages
18730      for nonsense cases like
18731         void foo() {
18732         #pragma omp single
18733           break;
18734         }
18735   */
18736   if (parser->in_statement)
18737     parser->in_statement = IN_OMP_BLOCK;
18738
18739   return save;
18740 }
18741
18742 static void
18743 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18744 {
18745   parser->in_statement = save;
18746 }
18747
18748 static tree
18749 cp_parser_omp_structured_block (cp_parser *parser)
18750 {
18751   tree stmt = begin_omp_structured_block ();
18752   unsigned int save = cp_parser_begin_omp_structured_block (parser);
18753
18754   cp_parser_statement (parser, NULL_TREE, false, NULL);
18755
18756   cp_parser_end_omp_structured_block (parser, save);
18757   return finish_omp_structured_block (stmt);
18758 }
18759
18760 /* OpenMP 2.5:
18761    # pragma omp atomic new-line
18762      expression-stmt
18763
18764    expression-stmt:
18765      x binop= expr | x++ | ++x | x-- | --x
18766    binop:
18767      +, *, -, /, &, ^, |, <<, >>
18768
18769   where x is an lvalue expression with scalar type.  */
18770
18771 static void
18772 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18773 {
18774   tree lhs, rhs;
18775   enum tree_code code;
18776
18777   cp_parser_require_pragma_eol (parser, pragma_tok);
18778
18779   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18780                                     /*cast_p=*/false);
18781   switch (TREE_CODE (lhs))
18782     {
18783     case ERROR_MARK:
18784       goto saw_error;
18785
18786     case PREINCREMENT_EXPR:
18787     case POSTINCREMENT_EXPR:
18788       lhs = TREE_OPERAND (lhs, 0);
18789       code = PLUS_EXPR;
18790       rhs = integer_one_node;
18791       break;
18792
18793     case PREDECREMENT_EXPR:
18794     case POSTDECREMENT_EXPR:
18795       lhs = TREE_OPERAND (lhs, 0);
18796       code = MINUS_EXPR;
18797       rhs = integer_one_node;
18798       break;
18799
18800     default:
18801       switch (cp_lexer_peek_token (parser->lexer)->type)
18802         {
18803         case CPP_MULT_EQ:
18804           code = MULT_EXPR;
18805           break;
18806         case CPP_DIV_EQ:
18807           code = TRUNC_DIV_EXPR;
18808           break;
18809         case CPP_PLUS_EQ:
18810           code = PLUS_EXPR;
18811           break;
18812         case CPP_MINUS_EQ:
18813           code = MINUS_EXPR;
18814           break;
18815         case CPP_LSHIFT_EQ:
18816           code = LSHIFT_EXPR;
18817           break;
18818         case CPP_RSHIFT_EQ:
18819           code = RSHIFT_EXPR;
18820           break;
18821         case CPP_AND_EQ:
18822           code = BIT_AND_EXPR;
18823           break;
18824         case CPP_OR_EQ:
18825           code = BIT_IOR_EXPR;
18826           break;
18827         case CPP_XOR_EQ:
18828           code = BIT_XOR_EXPR;
18829           break;
18830         default:
18831           cp_parser_error (parser,
18832                            "invalid operator for %<#pragma omp atomic%>");
18833           goto saw_error;
18834         }
18835       cp_lexer_consume_token (parser->lexer);
18836
18837       rhs = cp_parser_expression (parser, false);
18838       if (rhs == error_mark_node)
18839         goto saw_error;
18840       break;
18841     }
18842   finish_omp_atomic (code, lhs, rhs);
18843   cp_parser_consume_semicolon_at_end_of_statement (parser);
18844   return;
18845
18846  saw_error:
18847   cp_parser_skip_to_end_of_block_or_statement (parser);
18848 }
18849
18850
18851 /* OpenMP 2.5:
18852    # pragma omp barrier new-line  */
18853
18854 static void
18855 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18856 {
18857   cp_parser_require_pragma_eol (parser, pragma_tok);
18858   finish_omp_barrier ();
18859 }
18860
18861 /* OpenMP 2.5:
18862    # pragma omp critical [(name)] new-line
18863      structured-block  */
18864
18865 static tree
18866 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18867 {
18868   tree stmt, name = NULL;
18869
18870   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18871     {
18872       cp_lexer_consume_token (parser->lexer);
18873
18874       name = cp_parser_identifier (parser);
18875
18876       if (name == error_mark_node
18877           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18878         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18879                                                /*or_comma=*/false,
18880                                                /*consume_paren=*/true);
18881       if (name == error_mark_node)
18882         name = NULL;
18883     }
18884   cp_parser_require_pragma_eol (parser, pragma_tok);
18885
18886   stmt = cp_parser_omp_structured_block (parser);
18887   return c_finish_omp_critical (stmt, name);
18888 }
18889
18890 /* OpenMP 2.5:
18891    # pragma omp flush flush-vars[opt] new-line
18892
18893    flush-vars:
18894      ( variable-list ) */
18895
18896 static void
18897 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18898 {
18899   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18900     (void) cp_parser_omp_var_list (parser, 0, NULL);
18901   cp_parser_require_pragma_eol (parser, pragma_tok);
18902
18903   finish_omp_flush ();
18904 }
18905
18906 /* Parse the restricted form of the for statment allowed by OpenMP.  */
18907
18908 static tree
18909 cp_parser_omp_for_loop (cp_parser *parser)
18910 {
18911   tree init, cond, incr, body, decl, pre_body;
18912   location_t loc;
18913
18914   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18915     {
18916       cp_parser_error (parser, "for statement expected");
18917       return NULL;
18918     }
18919   loc = cp_lexer_consume_token (parser->lexer)->location;
18920   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18921     return NULL;
18922
18923   init = decl = NULL;
18924   pre_body = push_stmt_list ();
18925   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18926     {
18927       cp_decl_specifier_seq type_specifiers;
18928
18929       /* First, try to parse as an initialized declaration.  See
18930          cp_parser_condition, from whence the bulk of this is copied.  */
18931
18932       cp_parser_parse_tentatively (parser);
18933       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18934                                     &type_specifiers);
18935       if (!cp_parser_error_occurred (parser))
18936         {
18937           tree asm_specification, attributes;
18938           cp_declarator *declarator;
18939
18940           declarator = cp_parser_declarator (parser,
18941                                              CP_PARSER_DECLARATOR_NAMED,
18942                                              /*ctor_dtor_or_conv_p=*/NULL,
18943                                              /*parenthesized_p=*/NULL,
18944                                              /*member_p=*/false);
18945           attributes = cp_parser_attributes_opt (parser);
18946           asm_specification = cp_parser_asm_specification_opt (parser);
18947
18948           cp_parser_require (parser, CPP_EQ, "`='");
18949           if (cp_parser_parse_definitely (parser))
18950             {
18951               tree pushed_scope;
18952
18953               decl = start_decl (declarator, &type_specifiers,
18954                                  /*initialized_p=*/false, attributes,
18955                                  /*prefix_attributes=*/NULL_TREE,
18956                                  &pushed_scope);
18957
18958               init = cp_parser_assignment_expression (parser, false);
18959
18960               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18961                               asm_specification, LOOKUP_ONLYCONVERTING);
18962
18963               if (pushed_scope)
18964                 pop_scope (pushed_scope);
18965             }
18966         }
18967       else
18968         cp_parser_abort_tentative_parse (parser);
18969
18970       /* If parsing as an initialized declaration failed, try again as
18971          a simple expression.  */
18972       if (decl == NULL)
18973         init = cp_parser_expression (parser, false);
18974     }
18975   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18976   pre_body = pop_stmt_list (pre_body);
18977
18978   cond = NULL;
18979   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18980     cond = cp_parser_condition (parser);
18981   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18982
18983   incr = NULL;
18984   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18985     incr = cp_parser_expression (parser, false);
18986
18987   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18988     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18989                                            /*or_comma=*/false,
18990                                            /*consume_paren=*/true);
18991
18992   /* Note that we saved the original contents of this flag when we entered
18993      the structured block, and so we don't need to re-save it here.  */
18994   parser->in_statement = IN_OMP_FOR;
18995
18996   /* Note that the grammar doesn't call for a structured block here,
18997      though the loop as a whole is a structured block.  */
18998   body = push_stmt_list ();
18999   cp_parser_statement (parser, NULL_TREE, false, NULL);
19000   body = pop_stmt_list (body);
19001
19002   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19003 }
19004
19005 /* OpenMP 2.5:
19006    #pragma omp for for-clause[optseq] new-line
19007      for-loop  */
19008
19009 #define OMP_FOR_CLAUSE_MASK                             \
19010         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19011         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19012         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19013         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19014         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
19015         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
19016         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19017
19018 static tree
19019 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19020 {
19021   tree clauses, sb, ret;
19022   unsigned int save;
19023
19024   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19025                                        "#pragma omp for", pragma_tok);
19026
19027   sb = begin_omp_structured_block ();
19028   save = cp_parser_begin_omp_structured_block (parser);
19029
19030   ret = cp_parser_omp_for_loop (parser);
19031   if (ret)
19032     OMP_FOR_CLAUSES (ret) = clauses;
19033
19034   cp_parser_end_omp_structured_block (parser, save);
19035   add_stmt (finish_omp_structured_block (sb));
19036
19037   return ret;
19038 }
19039
19040 /* OpenMP 2.5:
19041    # pragma omp master new-line
19042      structured-block  */
19043
19044 static tree
19045 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19046 {
19047   cp_parser_require_pragma_eol (parser, pragma_tok);
19048   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19049 }
19050
19051 /* OpenMP 2.5:
19052    # pragma omp ordered new-line
19053      structured-block  */
19054
19055 static tree
19056 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19057 {
19058   cp_parser_require_pragma_eol (parser, pragma_tok);
19059   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19060 }
19061
19062 /* OpenMP 2.5:
19063
19064    section-scope:
19065      { section-sequence }
19066
19067    section-sequence:
19068      section-directive[opt] structured-block
19069      section-sequence section-directive structured-block  */
19070
19071 static tree
19072 cp_parser_omp_sections_scope (cp_parser *parser)
19073 {
19074   tree stmt, substmt;
19075   bool error_suppress = false;
19076   cp_token *tok;
19077
19078   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19079     return NULL_TREE;
19080
19081   stmt = push_stmt_list ();
19082
19083   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19084     {
19085       unsigned save;
19086
19087       substmt = begin_omp_structured_block ();
19088       save = cp_parser_begin_omp_structured_block (parser);
19089
19090       while (1)
19091         {
19092           cp_parser_statement (parser, NULL_TREE, false, NULL);
19093
19094           tok = cp_lexer_peek_token (parser->lexer);
19095           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19096             break;
19097           if (tok->type == CPP_CLOSE_BRACE)
19098             break;
19099           if (tok->type == CPP_EOF)
19100             break;
19101         }
19102
19103       cp_parser_end_omp_structured_block (parser, save);
19104       substmt = finish_omp_structured_block (substmt);
19105       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19106       add_stmt (substmt);
19107     }
19108
19109   while (1)
19110     {
19111       tok = cp_lexer_peek_token (parser->lexer);
19112       if (tok->type == CPP_CLOSE_BRACE)
19113         break;
19114       if (tok->type == CPP_EOF)
19115         break;
19116
19117       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19118         {
19119           cp_lexer_consume_token (parser->lexer);
19120           cp_parser_require_pragma_eol (parser, tok);
19121           error_suppress = false;
19122         }
19123       else if (!error_suppress)
19124         {
19125           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19126           error_suppress = true;
19127         }
19128
19129       substmt = cp_parser_omp_structured_block (parser);
19130       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19131       add_stmt (substmt);
19132     }
19133   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19134
19135   substmt = pop_stmt_list (stmt);
19136
19137   stmt = make_node (OMP_SECTIONS);
19138   TREE_TYPE (stmt) = void_type_node;
19139   OMP_SECTIONS_BODY (stmt) = substmt;
19140
19141   add_stmt (stmt);
19142   return stmt;
19143 }
19144
19145 /* OpenMP 2.5:
19146    # pragma omp sections sections-clause[optseq] newline
19147      sections-scope  */
19148
19149 #define OMP_SECTIONS_CLAUSE_MASK                        \
19150         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19151         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19152         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19153         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19154         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19155
19156 static tree
19157 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19158 {
19159   tree clauses, ret;
19160
19161   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19162                                        "#pragma omp sections", pragma_tok);
19163
19164   ret = cp_parser_omp_sections_scope (parser);
19165   if (ret)
19166     OMP_SECTIONS_CLAUSES (ret) = clauses;
19167
19168   return ret;
19169 }
19170
19171 /* OpenMP 2.5:
19172    # pragma parallel parallel-clause new-line
19173    # pragma parallel for parallel-for-clause new-line
19174    # pragma parallel sections parallel-sections-clause new-line  */
19175
19176 #define OMP_PARALLEL_CLAUSE_MASK                        \
19177         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
19178         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19179         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19180         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
19181         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
19182         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
19183         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19184         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19185
19186 static tree
19187 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19188 {
19189   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19190   const char *p_name = "#pragma omp parallel";
19191   tree stmt, clauses, par_clause, ws_clause, block;
19192   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19193   unsigned int save;
19194
19195   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19196     {
19197       cp_lexer_consume_token (parser->lexer);
19198       p_kind = PRAGMA_OMP_PARALLEL_FOR;
19199       p_name = "#pragma omp parallel for";
19200       mask |= OMP_FOR_CLAUSE_MASK;
19201       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19202     }
19203   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19204     {
19205       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19206       const char *p = IDENTIFIER_POINTER (id);
19207       if (strcmp (p, "sections") == 0)
19208         {
19209           cp_lexer_consume_token (parser->lexer);
19210           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19211           p_name = "#pragma omp parallel sections";
19212           mask |= OMP_SECTIONS_CLAUSE_MASK;
19213           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19214         }
19215     }
19216
19217   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19218   block = begin_omp_parallel ();
19219   save = cp_parser_begin_omp_structured_block (parser);
19220
19221   switch (p_kind)
19222     {
19223     case PRAGMA_OMP_PARALLEL:
19224       cp_parser_already_scoped_statement (parser);
19225       par_clause = clauses;
19226       break;
19227
19228     case PRAGMA_OMP_PARALLEL_FOR:
19229       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19230       stmt = cp_parser_omp_for_loop (parser);
19231       if (stmt)
19232         OMP_FOR_CLAUSES (stmt) = ws_clause;
19233       break;
19234
19235     case PRAGMA_OMP_PARALLEL_SECTIONS:
19236       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19237       stmt = cp_parser_omp_sections_scope (parser);
19238       if (stmt)
19239         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19240       break;
19241
19242     default:
19243       gcc_unreachable ();
19244     }
19245
19246   cp_parser_end_omp_structured_block (parser, save);
19247   stmt = finish_omp_parallel (par_clause, block);
19248   if (p_kind != PRAGMA_OMP_PARALLEL)
19249     OMP_PARALLEL_COMBINED (stmt) = 1;
19250   return stmt;
19251 }
19252
19253 /* OpenMP 2.5:
19254    # pragma omp single single-clause[optseq] new-line
19255      structured-block  */
19256
19257 #define OMP_SINGLE_CLAUSE_MASK                          \
19258         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19259         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19260         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
19261         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19262
19263 static tree
19264 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19265 {
19266   tree stmt = make_node (OMP_SINGLE);
19267   TREE_TYPE (stmt) = void_type_node;
19268
19269   OMP_SINGLE_CLAUSES (stmt)
19270     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19271                                  "#pragma omp single", pragma_tok);
19272   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19273
19274   return add_stmt (stmt);
19275 }
19276
19277 /* OpenMP 2.5:
19278    # pragma omp threadprivate (variable-list) */
19279
19280 static void
19281 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19282 {
19283   tree vars;
19284
19285   vars = cp_parser_omp_var_list (parser, 0, NULL);
19286   cp_parser_require_pragma_eol (parser, pragma_tok);
19287
19288   if (!targetm.have_tls)
19289     sorry ("threadprivate variables not supported in this target");
19290
19291   finish_omp_threadprivate (vars);
19292 }
19293
19294 /* Main entry point to OpenMP statement pragmas.  */
19295
19296 static void
19297 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19298 {
19299   tree stmt;
19300
19301   switch (pragma_tok->pragma_kind)
19302     {
19303     case PRAGMA_OMP_ATOMIC:
19304       cp_parser_omp_atomic (parser, pragma_tok);
19305       return;
19306     case PRAGMA_OMP_CRITICAL:
19307       stmt = cp_parser_omp_critical (parser, pragma_tok);
19308       break;
19309     case PRAGMA_OMP_FOR:
19310       stmt = cp_parser_omp_for (parser, pragma_tok);
19311       break;
19312     case PRAGMA_OMP_MASTER:
19313       stmt = cp_parser_omp_master (parser, pragma_tok);
19314       break;
19315     case PRAGMA_OMP_ORDERED:
19316       stmt = cp_parser_omp_ordered (parser, pragma_tok);
19317       break;
19318     case PRAGMA_OMP_PARALLEL:
19319       stmt = cp_parser_omp_parallel (parser, pragma_tok);
19320       break;
19321     case PRAGMA_OMP_SECTIONS:
19322       stmt = cp_parser_omp_sections (parser, pragma_tok);
19323       break;
19324     case PRAGMA_OMP_SINGLE:
19325       stmt = cp_parser_omp_single (parser, pragma_tok);
19326       break;
19327     default:
19328       gcc_unreachable ();
19329     }
19330
19331   if (stmt)
19332     SET_EXPR_LOCATION (stmt, pragma_tok->location);
19333 }
19334 \f
19335 /* The parser.  */
19336
19337 static GTY (()) cp_parser *the_parser;
19338
19339 \f
19340 /* Special handling for the first token or line in the file.  The first
19341    thing in the file might be #pragma GCC pch_preprocess, which loads a
19342    PCH file, which is a GC collection point.  So we need to handle this
19343    first pragma without benefit of an existing lexer structure.
19344
19345    Always returns one token to the caller in *FIRST_TOKEN.  This is
19346    either the true first token of the file, or the first token after
19347    the initial pragma.  */
19348
19349 static void
19350 cp_parser_initial_pragma (cp_token *first_token)
19351 {
19352   tree name = NULL;
19353
19354   cp_lexer_get_preprocessor_token (NULL, first_token);
19355   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19356     return;
19357
19358   cp_lexer_get_preprocessor_token (NULL, first_token);
19359   if (first_token->type == CPP_STRING)
19360     {
19361       name = first_token->u.value;
19362
19363       cp_lexer_get_preprocessor_token (NULL, first_token);
19364       if (first_token->type != CPP_PRAGMA_EOL)
19365         error ("junk at end of %<#pragma GCC pch_preprocess%>");
19366     }
19367   else
19368     error ("expected string literal");
19369
19370   /* Skip to the end of the pragma.  */
19371   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19372     cp_lexer_get_preprocessor_token (NULL, first_token);
19373
19374   /* Now actually load the PCH file.  */
19375   if (name)
19376     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19377
19378   /* Read one more token to return to our caller.  We have to do this
19379      after reading the PCH file in, since its pointers have to be
19380      live.  */
19381   cp_lexer_get_preprocessor_token (NULL, first_token);
19382 }
19383
19384 /* Normal parsing of a pragma token.  Here we can (and must) use the
19385    regular lexer.  */
19386
19387 static bool
19388 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19389 {
19390   cp_token *pragma_tok;
19391   unsigned int id;
19392
19393   pragma_tok = cp_lexer_consume_token (parser->lexer);
19394   gcc_assert (pragma_tok->type == CPP_PRAGMA);
19395   parser->lexer->in_pragma = true;
19396
19397   id = pragma_tok->pragma_kind;
19398   switch (id)
19399     {
19400     case PRAGMA_GCC_PCH_PREPROCESS:
19401       error ("%<#pragma GCC pch_preprocess%> must be first");
19402       break;
19403
19404     case PRAGMA_OMP_BARRIER:
19405       switch (context)
19406         {
19407         case pragma_compound:
19408           cp_parser_omp_barrier (parser, pragma_tok);
19409           return false;
19410         case pragma_stmt:
19411           error ("%<#pragma omp barrier%> may only be "
19412                  "used in compound statements");
19413           break;
19414         default:
19415           goto bad_stmt;
19416         }
19417       break;
19418
19419     case PRAGMA_OMP_FLUSH:
19420       switch (context)
19421         {
19422         case pragma_compound:
19423           cp_parser_omp_flush (parser, pragma_tok);
19424           return false;
19425         case pragma_stmt:
19426           error ("%<#pragma omp flush%> may only be "
19427                  "used in compound statements");
19428           break;
19429         default:
19430           goto bad_stmt;
19431         }
19432       break;
19433
19434     case PRAGMA_OMP_THREADPRIVATE:
19435       cp_parser_omp_threadprivate (parser, pragma_tok);
19436       return false;
19437
19438     case PRAGMA_OMP_ATOMIC:
19439     case PRAGMA_OMP_CRITICAL:
19440     case PRAGMA_OMP_FOR:
19441     case PRAGMA_OMP_MASTER:
19442     case PRAGMA_OMP_ORDERED:
19443     case PRAGMA_OMP_PARALLEL:
19444     case PRAGMA_OMP_SECTIONS:
19445     case PRAGMA_OMP_SINGLE:
19446       if (context == pragma_external)
19447         goto bad_stmt;
19448       cp_parser_omp_construct (parser, pragma_tok);
19449       return true;
19450
19451     case PRAGMA_OMP_SECTION:
19452       error ("%<#pragma omp section%> may only be used in "
19453              "%<#pragma omp sections%> construct");
19454       break;
19455
19456     default:
19457       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19458       c_invoke_pragma_handler (id);
19459       break;
19460
19461     bad_stmt:
19462       cp_parser_error (parser, "expected declaration specifiers");
19463       break;
19464     }
19465
19466   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19467   return false;
19468 }
19469
19470 /* The interface the pragma parsers have to the lexer.  */
19471
19472 enum cpp_ttype
19473 pragma_lex (tree *value)
19474 {
19475   cp_token *tok;
19476   enum cpp_ttype ret;
19477
19478   tok = cp_lexer_peek_token (the_parser->lexer);
19479
19480   ret = tok->type;
19481   *value = tok->u.value;
19482
19483   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19484     ret = CPP_EOF;
19485   else if (ret == CPP_STRING)
19486     *value = cp_parser_string_literal (the_parser, false, false);
19487   else
19488     {
19489       cp_lexer_consume_token (the_parser->lexer);
19490       if (ret == CPP_KEYWORD)
19491         ret = CPP_NAME;
19492     }
19493
19494   return ret;
19495 }
19496
19497 \f
19498 /* External interface.  */
19499
19500 /* Parse one entire translation unit.  */
19501
19502 void
19503 c_parse_file (void)
19504 {
19505   bool error_occurred;
19506   static bool already_called = false;
19507
19508   if (already_called)
19509     {
19510       sorry ("inter-module optimizations not implemented for C++");
19511       return;
19512     }
19513   already_called = true;
19514
19515   the_parser = cp_parser_new ();
19516   push_deferring_access_checks (flag_access_control
19517                                 ? dk_no_deferred : dk_no_check);
19518   error_occurred = cp_parser_translation_unit (the_parser);
19519   the_parser = NULL;
19520 }
19521
19522 #include "gt-cp-parser.h"