OSDN Git Service

PR c++/28999
[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               {
3028                 error ("statement-expressions are allowed only inside functions");
3029                 cp_parser_skip_to_end_of_block_or_statement (parser);
3030                 expr = error_mark_node;
3031               }
3032             else
3033               {
3034                 /* Start the statement-expression.  */
3035                 expr = begin_stmt_expr ();
3036                 /* Parse the compound-statement.  */
3037                 cp_parser_compound_statement (parser, expr, false);
3038                 /* Finish up.  */
3039                 expr = finish_stmt_expr (expr, false);
3040               }
3041           }
3042         else
3043           {
3044             /* Parse the parenthesized expression.  */
3045             expr = cp_parser_expression (parser, cast_p);
3046             /* Let the front end know that this expression was
3047                enclosed in parentheses. This matters in case, for
3048                example, the expression is of the form `A::B', since
3049                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3050                not.  */
3051             finish_parenthesized_expr (expr);
3052           }
3053         /* The `>' token might be the end of a template-id or
3054            template-parameter-list now.  */
3055         parser->greater_than_is_operator_p
3056           = saved_greater_than_is_operator_p;
3057         /* Consume the `)'.  */
3058         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3059           cp_parser_skip_to_end_of_statement (parser);
3060
3061         return expr;
3062       }
3063
3064     case CPP_KEYWORD:
3065       switch (token->keyword)
3066         {
3067           /* These two are the boolean literals.  */
3068         case RID_TRUE:
3069           cp_lexer_consume_token (parser->lexer);
3070           return boolean_true_node;
3071         case RID_FALSE:
3072           cp_lexer_consume_token (parser->lexer);
3073           return boolean_false_node;
3074
3075           /* The `__null' literal.  */
3076         case RID_NULL:
3077           cp_lexer_consume_token (parser->lexer);
3078           return null_node;
3079
3080           /* Recognize the `this' keyword.  */
3081         case RID_THIS:
3082           cp_lexer_consume_token (parser->lexer);
3083           if (parser->local_variables_forbidden_p)
3084             {
3085               error ("%<this%> may not be used in this context");
3086               return error_mark_node;
3087             }
3088           /* Pointers cannot appear in constant-expressions.  */
3089           if (cp_parser_non_integral_constant_expression (parser,
3090                                                           "`this'"))
3091             return error_mark_node;
3092           return finish_this_expr ();
3093
3094           /* The `operator' keyword can be the beginning of an
3095              id-expression.  */
3096         case RID_OPERATOR:
3097           goto id_expression;
3098
3099         case RID_FUNCTION_NAME:
3100         case RID_PRETTY_FUNCTION_NAME:
3101         case RID_C99_FUNCTION_NAME:
3102           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3103              __func__ are the names of variables -- but they are
3104              treated specially.  Therefore, they are handled here,
3105              rather than relying on the generic id-expression logic
3106              below.  Grammatically, these names are id-expressions.
3107
3108              Consume the token.  */
3109           token = cp_lexer_consume_token (parser->lexer);
3110           /* Look up the name.  */
3111           return finish_fname (token->u.value);
3112
3113         case RID_VA_ARG:
3114           {
3115             tree expression;
3116             tree type;
3117
3118             /* The `__builtin_va_arg' construct is used to handle
3119                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3120             cp_lexer_consume_token (parser->lexer);
3121             /* Look for the opening `('.  */
3122             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3123             /* Now, parse the assignment-expression.  */
3124             expression = cp_parser_assignment_expression (parser,
3125                                                           /*cast_p=*/false);
3126             /* Look for the `,'.  */
3127             cp_parser_require (parser, CPP_COMMA, "`,'");
3128             /* Parse the type-id.  */
3129             type = cp_parser_type_id (parser);
3130             /* Look for the closing `)'.  */
3131             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3132             /* Using `va_arg' in a constant-expression is not
3133                allowed.  */
3134             if (cp_parser_non_integral_constant_expression (parser,
3135                                                             "`va_arg'"))
3136               return error_mark_node;
3137             return build_x_va_arg (expression, type);
3138           }
3139
3140         case RID_OFFSETOF:
3141           return cp_parser_builtin_offsetof (parser);
3142
3143           /* Objective-C++ expressions.  */
3144         case RID_AT_ENCODE:
3145         case RID_AT_PROTOCOL:
3146         case RID_AT_SELECTOR:
3147           return cp_parser_objc_expression (parser);
3148
3149         default:
3150           cp_parser_error (parser, "expected primary-expression");
3151           return error_mark_node;
3152         }
3153
3154       /* An id-expression can start with either an identifier, a
3155          `::' as the beginning of a qualified-id, or the "operator"
3156          keyword.  */
3157     case CPP_NAME:
3158     case CPP_SCOPE:
3159     case CPP_TEMPLATE_ID:
3160     case CPP_NESTED_NAME_SPECIFIER:
3161       {
3162         tree id_expression;
3163         tree decl;
3164         const char *error_msg;
3165         bool template_p;
3166         bool done;
3167
3168       id_expression:
3169         /* Parse the id-expression.  */
3170         id_expression
3171           = cp_parser_id_expression (parser,
3172                                      /*template_keyword_p=*/false,
3173                                      /*check_dependency_p=*/true,
3174                                      &template_p,
3175                                      /*declarator_p=*/false,
3176                                      /*optional_p=*/false);
3177         if (id_expression == error_mark_node)
3178           return error_mark_node;
3179         token = cp_lexer_peek_token (parser->lexer);
3180         done = (token->type != CPP_OPEN_SQUARE
3181                 && token->type != CPP_OPEN_PAREN
3182                 && token->type != CPP_DOT
3183                 && token->type != CPP_DEREF
3184                 && token->type != CPP_PLUS_PLUS
3185                 && token->type != CPP_MINUS_MINUS);
3186         /* If we have a template-id, then no further lookup is
3187            required.  If the template-id was for a template-class, we
3188            will sometimes have a TYPE_DECL at this point.  */
3189         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3190                  || TREE_CODE (id_expression) == TYPE_DECL)
3191           decl = id_expression;
3192         /* Look up the name.  */
3193         else
3194           {
3195             tree ambiguous_decls;
3196
3197             decl = cp_parser_lookup_name (parser, id_expression,
3198                                           none_type,
3199                                           template_p,
3200                                           /*is_namespace=*/false,
3201                                           /*check_dependency=*/true,
3202                                           &ambiguous_decls);
3203             /* If the lookup was ambiguous, an error will already have
3204                been issued.  */
3205             if (ambiguous_decls)
3206               return error_mark_node;
3207
3208             /* In Objective-C++, an instance variable (ivar) may be preferred
3209                to whatever cp_parser_lookup_name() found.  */
3210             decl = objc_lookup_ivar (decl, id_expression);
3211
3212             /* If name lookup gives us a SCOPE_REF, then the
3213                qualifying scope was dependent.  */
3214             if (TREE_CODE (decl) == SCOPE_REF)
3215               return decl;
3216             /* Check to see if DECL is a local variable in a context
3217                where that is forbidden.  */
3218             if (parser->local_variables_forbidden_p
3219                 && local_variable_p (decl))
3220               {
3221                 /* It might be that we only found DECL because we are
3222                    trying to be generous with pre-ISO scoping rules.
3223                    For example, consider:
3224
3225                      int i;
3226                      void g() {
3227                        for (int i = 0; i < 10; ++i) {}
3228                        extern void f(int j = i);
3229                      }
3230
3231                    Here, name look up will originally find the out
3232                    of scope `i'.  We need to issue a warning message,
3233                    but then use the global `i'.  */
3234                 decl = check_for_out_of_scope_variable (decl);
3235                 if (local_variable_p (decl))
3236                   {
3237                     error ("local variable %qD may not appear in this context",
3238                            decl);
3239                     return error_mark_node;
3240                   }
3241               }
3242           }
3243
3244         decl = (finish_id_expression
3245                 (id_expression, decl, parser->scope,
3246                  idk,
3247                  parser->integral_constant_expression_p,
3248                  parser->allow_non_integral_constant_expression_p,
3249                  &parser->non_integral_constant_expression_p,
3250                  template_p, done, address_p,
3251                  template_arg_p,
3252                  &error_msg));
3253         if (error_msg)
3254           cp_parser_error (parser, error_msg);
3255         return decl;
3256       }
3257
3258       /* Anything else is an error.  */
3259     default:
3260       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3261       if (c_dialect_objc ()
3262           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3263         return cp_parser_objc_expression (parser);
3264
3265       cp_parser_error (parser, "expected primary-expression");
3266       return error_mark_node;
3267     }
3268 }
3269
3270 /* Parse an id-expression.
3271
3272    id-expression:
3273      unqualified-id
3274      qualified-id
3275
3276    qualified-id:
3277      :: [opt] nested-name-specifier template [opt] unqualified-id
3278      :: identifier
3279      :: operator-function-id
3280      :: template-id
3281
3282    Return a representation of the unqualified portion of the
3283    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3284    a `::' or nested-name-specifier.
3285
3286    Often, if the id-expression was a qualified-id, the caller will
3287    want to make a SCOPE_REF to represent the qualified-id.  This
3288    function does not do this in order to avoid wastefully creating
3289    SCOPE_REFs when they are not required.
3290
3291    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3292    `template' keyword.
3293
3294    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3295    uninstantiated templates.
3296
3297    If *TEMPLATE_P is non-NULL, it is set to true iff the
3298    `template' keyword is used to explicitly indicate that the entity
3299    named is a template.
3300
3301    If DECLARATOR_P is true, the id-expression is appearing as part of
3302    a declarator, rather than as part of an expression.  */
3303
3304 static tree
3305 cp_parser_id_expression (cp_parser *parser,
3306                          bool template_keyword_p,
3307                          bool check_dependency_p,
3308                          bool *template_p,
3309                          bool declarator_p,
3310                          bool optional_p)
3311 {
3312   bool global_scope_p;
3313   bool nested_name_specifier_p;
3314
3315   /* Assume the `template' keyword was not used.  */
3316   if (template_p)
3317     *template_p = template_keyword_p;
3318
3319   /* Look for the optional `::' operator.  */
3320   global_scope_p
3321     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3322        != NULL_TREE);
3323   /* Look for the optional nested-name-specifier.  */
3324   nested_name_specifier_p
3325     = (cp_parser_nested_name_specifier_opt (parser,
3326                                             /*typename_keyword_p=*/false,
3327                                             check_dependency_p,
3328                                             /*type_p=*/false,
3329                                             declarator_p)
3330        != NULL_TREE);
3331   /* If there is a nested-name-specifier, then we are looking at
3332      the first qualified-id production.  */
3333   if (nested_name_specifier_p)
3334     {
3335       tree saved_scope;
3336       tree saved_object_scope;
3337       tree saved_qualifying_scope;
3338       tree unqualified_id;
3339       bool is_template;
3340
3341       /* See if the next token is the `template' keyword.  */
3342       if (!template_p)
3343         template_p = &is_template;
3344       *template_p = cp_parser_optional_template_keyword (parser);
3345       /* Name lookup we do during the processing of the
3346          unqualified-id might obliterate SCOPE.  */
3347       saved_scope = parser->scope;
3348       saved_object_scope = parser->object_scope;
3349       saved_qualifying_scope = parser->qualifying_scope;
3350       /* Process the final unqualified-id.  */
3351       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3352                                                  check_dependency_p,
3353                                                  declarator_p,
3354                                                  /*optional_p=*/false);
3355       /* Restore the SAVED_SCOPE for our caller.  */
3356       parser->scope = saved_scope;
3357       parser->object_scope = saved_object_scope;
3358       parser->qualifying_scope = saved_qualifying_scope;
3359
3360       return unqualified_id;
3361     }
3362   /* Otherwise, if we are in global scope, then we are looking at one
3363      of the other qualified-id productions.  */
3364   else if (global_scope_p)
3365     {
3366       cp_token *token;
3367       tree id;
3368
3369       /* Peek at the next token.  */
3370       token = cp_lexer_peek_token (parser->lexer);
3371
3372       /* If it's an identifier, and the next token is not a "<", then
3373          we can avoid the template-id case.  This is an optimization
3374          for this common case.  */
3375       if (token->type == CPP_NAME
3376           && !cp_parser_nth_token_starts_template_argument_list_p
3377                (parser, 2))
3378         return cp_parser_identifier (parser);
3379
3380       cp_parser_parse_tentatively (parser);
3381       /* Try a template-id.  */
3382       id = cp_parser_template_id (parser,
3383                                   /*template_keyword_p=*/false,
3384                                   /*check_dependency_p=*/true,
3385                                   declarator_p);
3386       /* If that worked, we're done.  */
3387       if (cp_parser_parse_definitely (parser))
3388         return id;
3389
3390       /* Peek at the next token.  (Changes in the token buffer may
3391          have invalidated the pointer obtained above.)  */
3392       token = cp_lexer_peek_token (parser->lexer);
3393
3394       switch (token->type)
3395         {
3396         case CPP_NAME:
3397           return cp_parser_identifier (parser);
3398
3399         case CPP_KEYWORD:
3400           if (token->keyword == RID_OPERATOR)
3401             return cp_parser_operator_function_id (parser);
3402           /* Fall through.  */
3403
3404         default:
3405           cp_parser_error (parser, "expected id-expression");
3406           return error_mark_node;
3407         }
3408     }
3409   else
3410     return cp_parser_unqualified_id (parser, template_keyword_p,
3411                                      /*check_dependency_p=*/true,
3412                                      declarator_p,
3413                                      optional_p);
3414 }
3415
3416 /* Parse an unqualified-id.
3417
3418    unqualified-id:
3419      identifier
3420      operator-function-id
3421      conversion-function-id
3422      ~ class-name
3423      template-id
3424
3425    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3426    keyword, in a construct like `A::template ...'.
3427
3428    Returns a representation of unqualified-id.  For the `identifier'
3429    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3430    production a BIT_NOT_EXPR is returned; the operand of the
3431    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3432    other productions, see the documentation accompanying the
3433    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3434    names are looked up in uninstantiated templates.  If DECLARATOR_P
3435    is true, the unqualified-id is appearing as part of a declarator,
3436    rather than as part of an expression.  */
3437
3438 static tree
3439 cp_parser_unqualified_id (cp_parser* parser,
3440                           bool template_keyword_p,
3441                           bool check_dependency_p,
3442                           bool declarator_p,
3443                           bool optional_p)
3444 {
3445   cp_token *token;
3446
3447   /* Peek at the next token.  */
3448   token = cp_lexer_peek_token (parser->lexer);
3449
3450   switch (token->type)
3451     {
3452     case CPP_NAME:
3453       {
3454         tree id;
3455
3456         /* We don't know yet whether or not this will be a
3457            template-id.  */
3458         cp_parser_parse_tentatively (parser);
3459         /* Try a template-id.  */
3460         id = cp_parser_template_id (parser, template_keyword_p,
3461                                     check_dependency_p,
3462                                     declarator_p);
3463         /* If it worked, we're done.  */
3464         if (cp_parser_parse_definitely (parser))
3465           return id;
3466         /* Otherwise, it's an ordinary identifier.  */
3467         return cp_parser_identifier (parser);
3468       }
3469
3470     case CPP_TEMPLATE_ID:
3471       return cp_parser_template_id (parser, template_keyword_p,
3472                                     check_dependency_p,
3473                                     declarator_p);
3474
3475     case CPP_COMPL:
3476       {
3477         tree type_decl;
3478         tree qualifying_scope;
3479         tree object_scope;
3480         tree scope;
3481         bool done;
3482
3483         /* Consume the `~' token.  */
3484         cp_lexer_consume_token (parser->lexer);
3485         /* Parse the class-name.  The standard, as written, seems to
3486            say that:
3487
3488              template <typename T> struct S { ~S (); };
3489              template <typename T> S<T>::~S() {}
3490
3491            is invalid, since `~' must be followed by a class-name, but
3492            `S<T>' is dependent, and so not known to be a class.
3493            That's not right; we need to look in uninstantiated
3494            templates.  A further complication arises from:
3495
3496              template <typename T> void f(T t) {
3497                t.T::~T();
3498              }
3499
3500            Here, it is not possible to look up `T' in the scope of `T'
3501            itself.  We must look in both the current scope, and the
3502            scope of the containing complete expression.
3503
3504            Yet another issue is:
3505
3506              struct S {
3507                int S;
3508                ~S();
3509              };
3510
3511              S::~S() {}
3512
3513            The standard does not seem to say that the `S' in `~S'
3514            should refer to the type `S' and not the data member
3515            `S::S'.  */
3516
3517         /* DR 244 says that we look up the name after the "~" in the
3518            same scope as we looked up the qualifying name.  That idea
3519            isn't fully worked out; it's more complicated than that.  */
3520         scope = parser->scope;
3521         object_scope = parser->object_scope;
3522         qualifying_scope = parser->qualifying_scope;
3523
3524         /* Check for invalid scopes.  */
3525         if (scope == error_mark_node)
3526           {
3527             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3528               cp_lexer_consume_token (parser->lexer);
3529             return error_mark_node;
3530           }
3531         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3532           {
3533             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3534               error ("scope %qT before %<~%> is not a class-name", scope);
3535             cp_parser_simulate_error (parser);
3536             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3537               cp_lexer_consume_token (parser->lexer);
3538             return error_mark_node;
3539           }
3540         gcc_assert (!scope || TYPE_P (scope));
3541
3542         /* If the name is of the form "X::~X" it's OK.  */
3543         token = cp_lexer_peek_token (parser->lexer);
3544         if (scope
3545             && token->type == CPP_NAME
3546             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3547                 == CPP_OPEN_PAREN)
3548             && constructor_name_p (token->u.value, scope))
3549           {
3550             cp_lexer_consume_token (parser->lexer);
3551             return build_nt (BIT_NOT_EXPR, scope);
3552           }
3553
3554         /* If there was an explicit qualification (S::~T), first look
3555            in the scope given by the qualification (i.e., S).  */
3556         done = false;
3557         type_decl = NULL_TREE;
3558         if (scope)
3559           {
3560             cp_parser_parse_tentatively (parser);
3561             type_decl = cp_parser_class_name (parser,
3562                                               /*typename_keyword_p=*/false,
3563                                               /*template_keyword_p=*/false,
3564                                               none_type,
3565                                               /*check_dependency=*/false,
3566                                               /*class_head_p=*/false,
3567                                               declarator_p);
3568             if (cp_parser_parse_definitely (parser))
3569               done = true;
3570           }
3571         /* In "N::S::~S", look in "N" as well.  */
3572         if (!done && scope && qualifying_scope)
3573           {
3574             cp_parser_parse_tentatively (parser);
3575             parser->scope = qualifying_scope;
3576             parser->object_scope = NULL_TREE;
3577             parser->qualifying_scope = NULL_TREE;
3578             type_decl
3579               = cp_parser_class_name (parser,
3580                                       /*typename_keyword_p=*/false,
3581                                       /*template_keyword_p=*/false,
3582                                       none_type,
3583                                       /*check_dependency=*/false,
3584                                       /*class_head_p=*/false,
3585                                       declarator_p);
3586             if (cp_parser_parse_definitely (parser))
3587               done = true;
3588           }
3589         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3590         else if (!done && object_scope)
3591           {
3592             cp_parser_parse_tentatively (parser);
3593             parser->scope = object_scope;
3594             parser->object_scope = NULL_TREE;
3595             parser->qualifying_scope = NULL_TREE;
3596             type_decl
3597               = cp_parser_class_name (parser,
3598                                       /*typename_keyword_p=*/false,
3599                                       /*template_keyword_p=*/false,
3600                                       none_type,
3601                                       /*check_dependency=*/false,
3602                                       /*class_head_p=*/false,
3603                                       declarator_p);
3604             if (cp_parser_parse_definitely (parser))
3605               done = true;
3606           }
3607         /* Look in the surrounding context.  */
3608         if (!done)
3609           {
3610             parser->scope = NULL_TREE;
3611             parser->object_scope = NULL_TREE;
3612             parser->qualifying_scope = NULL_TREE;
3613             type_decl
3614               = cp_parser_class_name (parser,
3615                                       /*typename_keyword_p=*/false,
3616                                       /*template_keyword_p=*/false,
3617                                       none_type,
3618                                       /*check_dependency=*/false,
3619                                       /*class_head_p=*/false,
3620                                       declarator_p);
3621           }
3622         /* If an error occurred, assume that the name of the
3623            destructor is the same as the name of the qualifying
3624            class.  That allows us to keep parsing after running
3625            into ill-formed destructor names.  */
3626         if (type_decl == error_mark_node && scope)
3627           return build_nt (BIT_NOT_EXPR, scope);
3628         else if (type_decl == error_mark_node)
3629           return error_mark_node;
3630
3631         /* Check that destructor name and scope match.  */
3632         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3633           {
3634             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3635               error ("declaration of %<~%T%> as member of %qT",
3636                      type_decl, scope);
3637             cp_parser_simulate_error (parser);
3638             return error_mark_node;
3639           }
3640
3641         /* [class.dtor]
3642
3643            A typedef-name that names a class shall not be used as the
3644            identifier in the declarator for a destructor declaration.  */
3645         if (declarator_p
3646             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3647             && !DECL_SELF_REFERENCE_P (type_decl)
3648             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3649           error ("typedef-name %qD used as destructor declarator",
3650                  type_decl);
3651
3652         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3653       }
3654
3655     case CPP_KEYWORD:
3656       if (token->keyword == RID_OPERATOR)
3657         {
3658           tree id;
3659
3660           /* This could be a template-id, so we try that first.  */
3661           cp_parser_parse_tentatively (parser);
3662           /* Try a template-id.  */
3663           id = cp_parser_template_id (parser, template_keyword_p,
3664                                       /*check_dependency_p=*/true,
3665                                       declarator_p);
3666           /* If that worked, we're done.  */
3667           if (cp_parser_parse_definitely (parser))
3668             return id;
3669           /* We still don't know whether we're looking at an
3670              operator-function-id or a conversion-function-id.  */
3671           cp_parser_parse_tentatively (parser);
3672           /* Try an operator-function-id.  */
3673           id = cp_parser_operator_function_id (parser);
3674           /* If that didn't work, try a conversion-function-id.  */
3675           if (!cp_parser_parse_definitely (parser))
3676             id = cp_parser_conversion_function_id (parser);
3677
3678           return id;
3679         }
3680       /* Fall through.  */
3681
3682     default:
3683       if (optional_p)
3684         return NULL_TREE;
3685       cp_parser_error (parser, "expected unqualified-id");
3686       return error_mark_node;
3687     }
3688 }
3689
3690 /* Parse an (optional) nested-name-specifier.
3691
3692    nested-name-specifier:
3693      class-or-namespace-name :: nested-name-specifier [opt]
3694      class-or-namespace-name :: template nested-name-specifier [opt]
3695
3696    PARSER->SCOPE should be set appropriately before this function is
3697    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3698    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3699    in name lookups.
3700
3701    Sets PARSER->SCOPE to the class (TYPE) or namespace
3702    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3703    it unchanged if there is no nested-name-specifier.  Returns the new
3704    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3705
3706    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3707    part of a declaration and/or decl-specifier.  */
3708
3709 static tree
3710 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3711                                      bool typename_keyword_p,
3712                                      bool check_dependency_p,
3713                                      bool type_p,
3714                                      bool is_declaration)
3715 {
3716   bool success = false;
3717   cp_token_position start = 0;
3718   cp_token *token;
3719
3720   /* Remember where the nested-name-specifier starts.  */
3721   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3722     {
3723       start = cp_lexer_token_position (parser->lexer, false);
3724       push_deferring_access_checks (dk_deferred);
3725     }
3726
3727   while (true)
3728     {
3729       tree new_scope;
3730       tree old_scope;
3731       tree saved_qualifying_scope;
3732       bool template_keyword_p;
3733
3734       /* Spot cases that cannot be the beginning of a
3735          nested-name-specifier.  */
3736       token = cp_lexer_peek_token (parser->lexer);
3737
3738       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3739          the already parsed nested-name-specifier.  */
3740       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3741         {
3742           /* Grab the nested-name-specifier and continue the loop.  */
3743           cp_parser_pre_parsed_nested_name_specifier (parser);
3744           /* If we originally encountered this nested-name-specifier
3745              with IS_DECLARATION set to false, we will not have
3746              resolved TYPENAME_TYPEs, so we must do so here.  */
3747           if (is_declaration
3748               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3749             {
3750               new_scope = resolve_typename_type (parser->scope,
3751                                                  /*only_current_p=*/false);
3752               if (new_scope != error_mark_node)
3753                 parser->scope = new_scope;
3754             }
3755           success = true;
3756           continue;
3757         }
3758
3759       /* Spot cases that cannot be the beginning of a
3760          nested-name-specifier.  On the second and subsequent times
3761          through the loop, we look for the `template' keyword.  */
3762       if (success && token->keyword == RID_TEMPLATE)
3763         ;
3764       /* A template-id can start a nested-name-specifier.  */
3765       else if (token->type == CPP_TEMPLATE_ID)
3766         ;
3767       else
3768         {
3769           /* If the next token is not an identifier, then it is
3770              definitely not a class-or-namespace-name.  */
3771           if (token->type != CPP_NAME)
3772             break;
3773           /* If the following token is neither a `<' (to begin a
3774              template-id), nor a `::', then we are not looking at a
3775              nested-name-specifier.  */
3776           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3777           if (token->type != CPP_SCOPE
3778               && !cp_parser_nth_token_starts_template_argument_list_p
3779                   (parser, 2))
3780             break;
3781         }
3782
3783       /* The nested-name-specifier is optional, so we parse
3784          tentatively.  */
3785       cp_parser_parse_tentatively (parser);
3786
3787       /* Look for the optional `template' keyword, if this isn't the
3788          first time through the loop.  */
3789       if (success)
3790         template_keyword_p = cp_parser_optional_template_keyword (parser);
3791       else
3792         template_keyword_p = false;
3793
3794       /* Save the old scope since the name lookup we are about to do
3795          might destroy it.  */
3796       old_scope = parser->scope;
3797       saved_qualifying_scope = parser->qualifying_scope;
3798       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3799          look up names in "X<T>::I" in order to determine that "Y" is
3800          a template.  So, if we have a typename at this point, we make
3801          an effort to look through it.  */
3802       if (is_declaration
3803           && !typename_keyword_p
3804           && parser->scope
3805           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3806         parser->scope = resolve_typename_type (parser->scope,
3807                                                /*only_current_p=*/false);
3808       /* Parse the qualifying entity.  */
3809       new_scope
3810         = cp_parser_class_or_namespace_name (parser,
3811                                              typename_keyword_p,
3812                                              template_keyword_p,
3813                                              check_dependency_p,
3814                                              type_p,
3815                                              is_declaration);
3816       /* Look for the `::' token.  */
3817       cp_parser_require (parser, CPP_SCOPE, "`::'");
3818
3819       /* If we found what we wanted, we keep going; otherwise, we're
3820          done.  */
3821       if (!cp_parser_parse_definitely (parser))
3822         {
3823           bool error_p = false;
3824
3825           /* Restore the OLD_SCOPE since it was valid before the
3826              failed attempt at finding the last
3827              class-or-namespace-name.  */
3828           parser->scope = old_scope;
3829           parser->qualifying_scope = saved_qualifying_scope;
3830           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3831             break;
3832           /* If the next token is an identifier, and the one after
3833              that is a `::', then any valid interpretation would have
3834              found a class-or-namespace-name.  */
3835           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3836                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3837                      == CPP_SCOPE)
3838                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3839                      != CPP_COMPL))
3840             {
3841               token = cp_lexer_consume_token (parser->lexer);
3842               if (!error_p)
3843                 {
3844                   if (!token->ambiguous_p)
3845                     {
3846                       tree decl;
3847                       tree ambiguous_decls;
3848
3849                       decl = cp_parser_lookup_name (parser, token->u.value,
3850                                                     none_type,
3851                                                     /*is_template=*/false,
3852                                                     /*is_namespace=*/false,
3853                                                     /*check_dependency=*/true,
3854                                                     &ambiguous_decls);
3855                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3856                         error ("%qD used without template parameters", decl);
3857                       else if (ambiguous_decls)
3858                         {
3859                           error ("reference to %qD is ambiguous",
3860                                  token->u.value);
3861                           print_candidates (ambiguous_decls);
3862                           decl = error_mark_node;
3863                         }
3864                       else
3865                         cp_parser_name_lookup_error
3866                           (parser, token->u.value, decl,
3867                            "is not a class or namespace");
3868                     }
3869                   parser->scope = error_mark_node;
3870                   error_p = true;
3871                   /* Treat this as a successful nested-name-specifier
3872                      due to:
3873
3874                      [basic.lookup.qual]
3875
3876                      If the name found is not a class-name (clause
3877                      _class_) or namespace-name (_namespace.def_), the
3878                      program is ill-formed.  */
3879                   success = true;
3880                 }
3881               cp_lexer_consume_token (parser->lexer);
3882             }
3883           break;
3884         }
3885       /* We've found one valid nested-name-specifier.  */
3886       success = true;
3887       /* Name lookup always gives us a DECL.  */
3888       if (TREE_CODE (new_scope) == TYPE_DECL)
3889         new_scope = TREE_TYPE (new_scope);
3890       /* Uses of "template" must be followed by actual templates.  */
3891       if (template_keyword_p
3892           && !(CLASS_TYPE_P (new_scope)
3893                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3894                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3895                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3896           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3897                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3898                    == TEMPLATE_ID_EXPR)))
3899         pedwarn (TYPE_P (new_scope)
3900                  ? "%qT is not a template"
3901                  : "%qD is not a template",
3902                  new_scope);
3903       /* If it is a class scope, try to complete it; we are about to
3904          be looking up names inside the class.  */
3905       if (TYPE_P (new_scope)
3906           /* Since checking types for dependency can be expensive,
3907              avoid doing it if the type is already complete.  */
3908           && !COMPLETE_TYPE_P (new_scope)
3909           /* Do not try to complete dependent types.  */
3910           && !dependent_type_p (new_scope))
3911         new_scope = complete_type (new_scope);
3912       /* Make sure we look in the right scope the next time through
3913          the loop.  */
3914       parser->scope = new_scope;
3915     }
3916
3917   /* If parsing tentatively, replace the sequence of tokens that makes
3918      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3919      token.  That way, should we re-parse the token stream, we will
3920      not have to repeat the effort required to do the parse, nor will
3921      we issue duplicate error messages.  */
3922   if (success && start)
3923     {
3924       cp_token *token;
3925
3926       token = cp_lexer_token_at (parser->lexer, start);
3927       /* Reset the contents of the START token.  */
3928       token->type = CPP_NESTED_NAME_SPECIFIER;
3929       /* Retrieve any deferred checks.  Do not pop this access checks yet
3930          so the memory will not be reclaimed during token replacing below.  */
3931       token->u.tree_check_value = GGC_CNEW (struct tree_check);
3932       token->u.tree_check_value->value = parser->scope;
3933       token->u.tree_check_value->checks = get_deferred_access_checks ();
3934       token->u.tree_check_value->qualifying_scope =
3935         parser->qualifying_scope;
3936       token->keyword = RID_MAX;
3937
3938       /* Purge all subsequent tokens.  */
3939       cp_lexer_purge_tokens_after (parser->lexer, start);
3940     }
3941
3942   if (start)
3943     pop_to_parent_deferring_access_checks ();
3944
3945   return success ? parser->scope : NULL_TREE;
3946 }
3947
3948 /* Parse a nested-name-specifier.  See
3949    cp_parser_nested_name_specifier_opt for details.  This function
3950    behaves identically, except that it will an issue an error if no
3951    nested-name-specifier is present.  */
3952
3953 static tree
3954 cp_parser_nested_name_specifier (cp_parser *parser,
3955                                  bool typename_keyword_p,
3956                                  bool check_dependency_p,
3957                                  bool type_p,
3958                                  bool is_declaration)
3959 {
3960   tree scope;
3961
3962   /* Look for the nested-name-specifier.  */
3963   scope = cp_parser_nested_name_specifier_opt (parser,
3964                                                typename_keyword_p,
3965                                                check_dependency_p,
3966                                                type_p,
3967                                                is_declaration);
3968   /* If it was not present, issue an error message.  */
3969   if (!scope)
3970     {
3971       cp_parser_error (parser, "expected nested-name-specifier");
3972       parser->scope = NULL_TREE;
3973     }
3974
3975   return scope;
3976 }
3977
3978 /* Parse a class-or-namespace-name.
3979
3980    class-or-namespace-name:
3981      class-name
3982      namespace-name
3983
3984    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3985    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3986    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3987    TYPE_P is TRUE iff the next name should be taken as a class-name,
3988    even the same name is declared to be another entity in the same
3989    scope.
3990
3991    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3992    specified by the class-or-namespace-name.  If neither is found the
3993    ERROR_MARK_NODE is returned.  */
3994
3995 static tree
3996 cp_parser_class_or_namespace_name (cp_parser *parser,
3997                                    bool typename_keyword_p,
3998                                    bool template_keyword_p,
3999                                    bool check_dependency_p,
4000                                    bool type_p,
4001                                    bool is_declaration)
4002 {
4003   tree saved_scope;
4004   tree saved_qualifying_scope;
4005   tree saved_object_scope;
4006   tree scope;
4007   bool only_class_p;
4008
4009   /* Before we try to parse the class-name, we must save away the
4010      current PARSER->SCOPE since cp_parser_class_name will destroy
4011      it.  */
4012   saved_scope = parser->scope;
4013   saved_qualifying_scope = parser->qualifying_scope;
4014   saved_object_scope = parser->object_scope;
4015   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4016      there is no need to look for a namespace-name.  */
4017   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4018   if (!only_class_p)
4019     cp_parser_parse_tentatively (parser);
4020   scope = cp_parser_class_name (parser,
4021                                 typename_keyword_p,
4022                                 template_keyword_p,
4023                                 type_p ? class_type : none_type,
4024                                 check_dependency_p,
4025                                 /*class_head_p=*/false,
4026                                 is_declaration);
4027   /* If that didn't work, try for a namespace-name.  */
4028   if (!only_class_p && !cp_parser_parse_definitely (parser))
4029     {
4030       /* Restore the saved scope.  */
4031       parser->scope = saved_scope;
4032       parser->qualifying_scope = saved_qualifying_scope;
4033       parser->object_scope = saved_object_scope;
4034       /* If we are not looking at an identifier followed by the scope
4035          resolution operator, then this is not part of a
4036          nested-name-specifier.  (Note that this function is only used
4037          to parse the components of a nested-name-specifier.)  */
4038       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4039           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4040         return error_mark_node;
4041       scope = cp_parser_namespace_name (parser);
4042     }
4043
4044   return scope;
4045 }
4046
4047 /* Parse a postfix-expression.
4048
4049    postfix-expression:
4050      primary-expression
4051      postfix-expression [ expression ]
4052      postfix-expression ( expression-list [opt] )
4053      simple-type-specifier ( expression-list [opt] )
4054      typename :: [opt] nested-name-specifier identifier
4055        ( expression-list [opt] )
4056      typename :: [opt] nested-name-specifier template [opt] template-id
4057        ( expression-list [opt] )
4058      postfix-expression . template [opt] id-expression
4059      postfix-expression -> template [opt] id-expression
4060      postfix-expression . pseudo-destructor-name
4061      postfix-expression -> pseudo-destructor-name
4062      postfix-expression ++
4063      postfix-expression --
4064      dynamic_cast < type-id > ( expression )
4065      static_cast < type-id > ( expression )
4066      reinterpret_cast < type-id > ( expression )
4067      const_cast < type-id > ( expression )
4068      typeid ( expression )
4069      typeid ( type-id )
4070
4071    GNU Extension:
4072
4073    postfix-expression:
4074      ( type-id ) { initializer-list , [opt] }
4075
4076    This extension is a GNU version of the C99 compound-literal
4077    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4078    but they are essentially the same concept.)
4079
4080    If ADDRESS_P is true, the postfix expression is the operand of the
4081    `&' operator.  CAST_P is true if this expression is the target of a
4082    cast.
4083
4084    Returns a representation of the expression.  */
4085
4086 static tree
4087 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4088 {
4089   cp_token *token;
4090   enum rid keyword;
4091   cp_id_kind idk = CP_ID_KIND_NONE;
4092   tree postfix_expression = NULL_TREE;
4093
4094   /* Peek at the next token.  */
4095   token = cp_lexer_peek_token (parser->lexer);
4096   /* Some of the productions are determined by keywords.  */
4097   keyword = token->keyword;
4098   switch (keyword)
4099     {
4100     case RID_DYNCAST:
4101     case RID_STATCAST:
4102     case RID_REINTCAST:
4103     case RID_CONSTCAST:
4104       {
4105         tree type;
4106         tree expression;
4107         const char *saved_message;
4108
4109         /* All of these can be handled in the same way from the point
4110            of view of parsing.  Begin by consuming the token
4111            identifying the cast.  */
4112         cp_lexer_consume_token (parser->lexer);
4113
4114         /* New types cannot be defined in the cast.  */
4115         saved_message = parser->type_definition_forbidden_message;
4116         parser->type_definition_forbidden_message
4117           = "types may not be defined in casts";
4118
4119         /* Look for the opening `<'.  */
4120         cp_parser_require (parser, CPP_LESS, "`<'");
4121         /* Parse the type to which we are casting.  */
4122         type = cp_parser_type_id (parser);
4123         /* Look for the closing `>'.  */
4124         cp_parser_require (parser, CPP_GREATER, "`>'");
4125         /* Restore the old message.  */
4126         parser->type_definition_forbidden_message = saved_message;
4127
4128         /* And the expression which is being cast.  */
4129         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4130         expression = cp_parser_expression (parser, /*cast_p=*/true);
4131         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4132
4133         /* Only type conversions to integral or enumeration types
4134            can be used in constant-expressions.  */
4135         if (!cast_valid_in_integral_constant_expression_p (type)
4136             && (cp_parser_non_integral_constant_expression
4137                 (parser,
4138                  "a cast to a type other than an integral or "
4139                  "enumeration type")))
4140           return error_mark_node;
4141
4142         switch (keyword)
4143           {
4144           case RID_DYNCAST:
4145             postfix_expression
4146               = build_dynamic_cast (type, expression);
4147             break;
4148           case RID_STATCAST:
4149             postfix_expression
4150               = build_static_cast (type, expression);
4151             break;
4152           case RID_REINTCAST:
4153             postfix_expression
4154               = build_reinterpret_cast (type, expression);
4155             break;
4156           case RID_CONSTCAST:
4157             postfix_expression
4158               = build_const_cast (type, expression);
4159             break;
4160           default:
4161             gcc_unreachable ();
4162           }
4163       }
4164       break;
4165
4166     case RID_TYPEID:
4167       {
4168         tree type;
4169         const char *saved_message;
4170         bool saved_in_type_id_in_expr_p;
4171
4172         /* Consume the `typeid' token.  */
4173         cp_lexer_consume_token (parser->lexer);
4174         /* Look for the `(' token.  */
4175         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4176         /* Types cannot be defined in a `typeid' expression.  */
4177         saved_message = parser->type_definition_forbidden_message;
4178         parser->type_definition_forbidden_message
4179           = "types may not be defined in a `typeid\' expression";
4180         /* We can't be sure yet whether we're looking at a type-id or an
4181            expression.  */
4182         cp_parser_parse_tentatively (parser);
4183         /* Try a type-id first.  */
4184         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4185         parser->in_type_id_in_expr_p = true;
4186         type = cp_parser_type_id (parser);
4187         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4188         /* Look for the `)' token.  Otherwise, we can't be sure that
4189            we're not looking at an expression: consider `typeid (int
4190            (3))', for example.  */
4191         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4192         /* If all went well, simply lookup the type-id.  */
4193         if (cp_parser_parse_definitely (parser))
4194           postfix_expression = get_typeid (type);
4195         /* Otherwise, fall back to the expression variant.  */
4196         else
4197           {
4198             tree expression;
4199
4200             /* Look for an expression.  */
4201             expression = cp_parser_expression (parser, /*cast_p=*/false);
4202             /* Compute its typeid.  */
4203             postfix_expression = build_typeid (expression);
4204             /* Look for the `)' token.  */
4205             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4206           }
4207         /* Restore the saved message.  */
4208         parser->type_definition_forbidden_message = saved_message;
4209         /* `typeid' may not appear in an integral constant expression.  */
4210         if (cp_parser_non_integral_constant_expression(parser,
4211                                                        "`typeid' operator"))
4212           return error_mark_node;
4213       }
4214       break;
4215
4216     case RID_TYPENAME:
4217       {
4218         tree type;
4219         /* The syntax permitted here is the same permitted for an
4220            elaborated-type-specifier.  */
4221         type = cp_parser_elaborated_type_specifier (parser,
4222                                                     /*is_friend=*/false,
4223                                                     /*is_declaration=*/false);
4224         postfix_expression = cp_parser_functional_cast (parser, type);
4225       }
4226       break;
4227
4228     default:
4229       {
4230         tree type;
4231
4232         /* If the next thing is a simple-type-specifier, we may be
4233            looking at a functional cast.  We could also be looking at
4234            an id-expression.  So, we try the functional cast, and if
4235            that doesn't work we fall back to the primary-expression.  */
4236         cp_parser_parse_tentatively (parser);
4237         /* Look for the simple-type-specifier.  */
4238         type = cp_parser_simple_type_specifier (parser,
4239                                                 /*decl_specs=*/NULL,
4240                                                 CP_PARSER_FLAGS_NONE);
4241         /* Parse the cast itself.  */
4242         if (!cp_parser_error_occurred (parser))
4243           postfix_expression
4244             = cp_parser_functional_cast (parser, type);
4245         /* If that worked, we're done.  */
4246         if (cp_parser_parse_definitely (parser))
4247           break;
4248
4249         /* If the functional-cast didn't work out, try a
4250            compound-literal.  */
4251         if (cp_parser_allow_gnu_extensions_p (parser)
4252             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4253           {
4254             VEC(constructor_elt,gc) *initializer_list = NULL;
4255             bool saved_in_type_id_in_expr_p;
4256
4257             cp_parser_parse_tentatively (parser);
4258             /* Consume the `('.  */
4259             cp_lexer_consume_token (parser->lexer);
4260             /* Parse the type.  */
4261             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4262             parser->in_type_id_in_expr_p = true;
4263             type = cp_parser_type_id (parser);
4264             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4265             /* Look for the `)'.  */
4266             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4267             /* Look for the `{'.  */
4268             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4269             /* If things aren't going well, there's no need to
4270                keep going.  */
4271             if (!cp_parser_error_occurred (parser))
4272               {
4273                 bool non_constant_p;
4274                 /* Parse the initializer-list.  */
4275                 initializer_list
4276                   = cp_parser_initializer_list (parser, &non_constant_p);
4277                 /* Allow a trailing `,'.  */
4278                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4279                   cp_lexer_consume_token (parser->lexer);
4280                 /* Look for the final `}'.  */
4281                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4282               }
4283             /* If that worked, we're definitely looking at a
4284                compound-literal expression.  */
4285             if (cp_parser_parse_definitely (parser))
4286               {
4287                 /* Warn the user that a compound literal is not
4288                    allowed in standard C++.  */
4289                 if (pedantic)
4290                   pedwarn ("ISO C++ forbids compound-literals");
4291                 /* Form the representation of the compound-literal.  */
4292                 postfix_expression
4293                   = finish_compound_literal (type, initializer_list);
4294                 break;
4295               }
4296           }
4297
4298         /* It must be a primary-expression.  */
4299         postfix_expression
4300           = cp_parser_primary_expression (parser, address_p, cast_p,
4301                                           /*template_arg_p=*/false,
4302                                           &idk);
4303       }
4304       break;
4305     }
4306
4307   /* Keep looping until the postfix-expression is complete.  */
4308   while (true)
4309     {
4310       if (idk == CP_ID_KIND_UNQUALIFIED
4311           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4312           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4313         /* It is not a Koenig lookup function call.  */
4314         postfix_expression
4315           = unqualified_name_lookup_error (postfix_expression);
4316
4317       /* Peek at the next token.  */
4318       token = cp_lexer_peek_token (parser->lexer);
4319
4320       switch (token->type)
4321         {
4322         case CPP_OPEN_SQUARE:
4323           postfix_expression
4324             = cp_parser_postfix_open_square_expression (parser,
4325                                                         postfix_expression,
4326                                                         false);
4327           idk = CP_ID_KIND_NONE;
4328           break;
4329
4330         case CPP_OPEN_PAREN:
4331           /* postfix-expression ( expression-list [opt] ) */
4332           {
4333             bool koenig_p;
4334             bool is_builtin_constant_p;
4335             bool saved_integral_constant_expression_p = false;
4336             bool saved_non_integral_constant_expression_p = false;
4337             tree args;
4338
4339             is_builtin_constant_p
4340               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4341             if (is_builtin_constant_p)
4342               {
4343                 /* The whole point of __builtin_constant_p is to allow
4344                    non-constant expressions to appear as arguments.  */
4345                 saved_integral_constant_expression_p
4346                   = parser->integral_constant_expression_p;
4347                 saved_non_integral_constant_expression_p
4348                   = parser->non_integral_constant_expression_p;
4349                 parser->integral_constant_expression_p = false;
4350               }
4351             args = (cp_parser_parenthesized_expression_list
4352                     (parser, /*is_attribute_list=*/false,
4353                      /*cast_p=*/false,
4354                      /*non_constant_p=*/NULL));
4355             if (is_builtin_constant_p)
4356               {
4357                 parser->integral_constant_expression_p
4358                   = saved_integral_constant_expression_p;
4359                 parser->non_integral_constant_expression_p
4360                   = saved_non_integral_constant_expression_p;
4361               }
4362
4363             if (args == error_mark_node)
4364               {
4365                 postfix_expression = error_mark_node;
4366                 break;
4367               }
4368
4369             /* Function calls are not permitted in
4370                constant-expressions.  */
4371             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4372                 && cp_parser_non_integral_constant_expression (parser,
4373                                                                "a function call"))
4374               {
4375                 postfix_expression = error_mark_node;
4376                 break;
4377               }
4378
4379             koenig_p = false;
4380             if (idk == CP_ID_KIND_UNQUALIFIED)
4381               {
4382                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4383                   {
4384                     if (args)
4385                       {
4386                         koenig_p = true;
4387                         postfix_expression
4388                           = perform_koenig_lookup (postfix_expression, args);
4389                       }
4390                     else
4391                       postfix_expression
4392                         = unqualified_fn_lookup_error (postfix_expression);
4393                   }
4394                 /* We do not perform argument-dependent lookup if
4395                    normal lookup finds a non-function, in accordance
4396                    with the expected resolution of DR 218.  */
4397                 else if (args && is_overloaded_fn (postfix_expression))
4398                   {
4399                     tree fn = get_first_fn (postfix_expression);
4400
4401                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4402                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4403
4404                     /* Only do argument dependent lookup if regular
4405                        lookup does not find a set of member functions.
4406                        [basic.lookup.koenig]/2a  */
4407                     if (!DECL_FUNCTION_MEMBER_P (fn))
4408                       {
4409                         koenig_p = true;
4410                         postfix_expression
4411                           = perform_koenig_lookup (postfix_expression, args);
4412                       }
4413                   }
4414               }
4415
4416             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4417               {
4418                 tree instance = TREE_OPERAND (postfix_expression, 0);
4419                 tree fn = TREE_OPERAND (postfix_expression, 1);
4420
4421                 if (processing_template_decl
4422                     && (type_dependent_expression_p (instance)
4423                         || (!BASELINK_P (fn)
4424                             && TREE_CODE (fn) != FIELD_DECL)
4425                         || type_dependent_expression_p (fn)
4426                         || any_type_dependent_arguments_p (args)))
4427                   {
4428                     postfix_expression
4429                       = build_min_nt (CALL_EXPR, postfix_expression,
4430                                       args, NULL_TREE);
4431                     break;
4432                   }
4433
4434                 if (BASELINK_P (fn))
4435                   postfix_expression
4436                     = (build_new_method_call
4437                        (instance, fn, args, NULL_TREE,
4438                         (idk == CP_ID_KIND_QUALIFIED
4439                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4440                         /*fn_p=*/NULL));
4441                 else
4442                   postfix_expression
4443                     = finish_call_expr (postfix_expression, args,
4444                                         /*disallow_virtual=*/false,
4445                                         /*koenig_p=*/false);
4446               }
4447             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4448                      || TREE_CODE (postfix_expression) == MEMBER_REF
4449                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4450               postfix_expression = (build_offset_ref_call_from_tree
4451                                     (postfix_expression, args));
4452             else if (idk == CP_ID_KIND_QUALIFIED)
4453               /* A call to a static class member, or a namespace-scope
4454                  function.  */
4455               postfix_expression
4456                 = finish_call_expr (postfix_expression, args,
4457                                     /*disallow_virtual=*/true,
4458                                     koenig_p);
4459             else
4460               /* All other function calls.  */
4461               postfix_expression
4462                 = finish_call_expr (postfix_expression, args,
4463                                     /*disallow_virtual=*/false,
4464                                     koenig_p);
4465
4466             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4467             idk = CP_ID_KIND_NONE;
4468           }
4469           break;
4470
4471         case CPP_DOT:
4472         case CPP_DEREF:
4473           /* postfix-expression . template [opt] id-expression
4474              postfix-expression . pseudo-destructor-name
4475              postfix-expression -> template [opt] id-expression
4476              postfix-expression -> pseudo-destructor-name */
4477
4478           /* Consume the `.' or `->' operator.  */
4479           cp_lexer_consume_token (parser->lexer);
4480
4481           postfix_expression
4482             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4483                                                       postfix_expression,
4484                                                       false, &idk);
4485           break;
4486
4487         case CPP_PLUS_PLUS:
4488           /* postfix-expression ++  */
4489           /* Consume the `++' token.  */
4490           cp_lexer_consume_token (parser->lexer);
4491           /* Generate a representation for the complete expression.  */
4492           postfix_expression
4493             = finish_increment_expr (postfix_expression,
4494                                      POSTINCREMENT_EXPR);
4495           /* Increments may not appear in constant-expressions.  */
4496           if (cp_parser_non_integral_constant_expression (parser,
4497                                                           "an increment"))
4498             postfix_expression = error_mark_node;
4499           idk = CP_ID_KIND_NONE;
4500           break;
4501
4502         case CPP_MINUS_MINUS:
4503           /* postfix-expression -- */
4504           /* Consume the `--' token.  */
4505           cp_lexer_consume_token (parser->lexer);
4506           /* Generate a representation for the complete expression.  */
4507           postfix_expression
4508             = finish_increment_expr (postfix_expression,
4509                                      POSTDECREMENT_EXPR);
4510           /* Decrements may not appear in constant-expressions.  */
4511           if (cp_parser_non_integral_constant_expression (parser,
4512                                                           "a decrement"))
4513             postfix_expression = error_mark_node;
4514           idk = CP_ID_KIND_NONE;
4515           break;
4516
4517         default:
4518           return postfix_expression;
4519         }
4520     }
4521
4522   /* We should never get here.  */
4523   gcc_unreachable ();
4524   return error_mark_node;
4525 }
4526
4527 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4528    by cp_parser_builtin_offsetof.  We're looking for
4529
4530      postfix-expression [ expression ]
4531
4532    FOR_OFFSETOF is set if we're being called in that context, which
4533    changes how we deal with integer constant expressions.  */
4534
4535 static tree
4536 cp_parser_postfix_open_square_expression (cp_parser *parser,
4537                                           tree postfix_expression,
4538                                           bool for_offsetof)
4539 {
4540   tree index;
4541
4542   /* Consume the `[' token.  */
4543   cp_lexer_consume_token (parser->lexer);
4544
4545   /* Parse the index expression.  */
4546   /* ??? For offsetof, there is a question of what to allow here.  If
4547      offsetof is not being used in an integral constant expression context,
4548      then we *could* get the right answer by computing the value at runtime.
4549      If we are in an integral constant expression context, then we might
4550      could accept any constant expression; hard to say without analysis.
4551      Rather than open the barn door too wide right away, allow only integer
4552      constant expressions here.  */
4553   if (for_offsetof)
4554     index = cp_parser_constant_expression (parser, false, NULL);
4555   else
4556     index = cp_parser_expression (parser, /*cast_p=*/false);
4557
4558   /* Look for the closing `]'.  */
4559   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4560
4561   /* Build the ARRAY_REF.  */
4562   postfix_expression = grok_array_decl (postfix_expression, index);
4563
4564   /* When not doing offsetof, array references are not permitted in
4565      constant-expressions.  */
4566   if (!for_offsetof
4567       && (cp_parser_non_integral_constant_expression
4568           (parser, "an array reference")))
4569     postfix_expression = error_mark_node;
4570
4571   return postfix_expression;
4572 }
4573
4574 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4575    by cp_parser_builtin_offsetof.  We're looking for
4576
4577      postfix-expression . template [opt] id-expression
4578      postfix-expression . pseudo-destructor-name
4579      postfix-expression -> template [opt] id-expression
4580      postfix-expression -> pseudo-destructor-name
4581
4582    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4583    limits what of the above we'll actually accept, but nevermind.
4584    TOKEN_TYPE is the "." or "->" token, which will already have been
4585    removed from the stream.  */
4586
4587 static tree
4588 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4589                                         enum cpp_ttype token_type,
4590                                         tree postfix_expression,
4591                                         bool for_offsetof, cp_id_kind *idk)
4592 {
4593   tree name;
4594   bool dependent_p;
4595   bool pseudo_destructor_p;
4596   tree scope = NULL_TREE;
4597
4598   /* If this is a `->' operator, dereference the pointer.  */
4599   if (token_type == CPP_DEREF)
4600     postfix_expression = build_x_arrow (postfix_expression);
4601   /* Check to see whether or not the expression is type-dependent.  */
4602   dependent_p = type_dependent_expression_p (postfix_expression);
4603   /* The identifier following the `->' or `.' is not qualified.  */
4604   parser->scope = NULL_TREE;
4605   parser->qualifying_scope = NULL_TREE;
4606   parser->object_scope = NULL_TREE;
4607   *idk = CP_ID_KIND_NONE;
4608   /* Enter the scope corresponding to the type of the object
4609      given by the POSTFIX_EXPRESSION.  */
4610   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4611     {
4612       scope = TREE_TYPE (postfix_expression);
4613       /* According to the standard, no expression should ever have
4614          reference type.  Unfortunately, we do not currently match
4615          the standard in this respect in that our internal representation
4616          of an expression may have reference type even when the standard
4617          says it does not.  Therefore, we have to manually obtain the
4618          underlying type here.  */
4619       scope = non_reference (scope);
4620       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4621       if (scope == unknown_type_node)
4622         {
4623           error ("%qE does not have class type", postfix_expression);
4624           scope = NULL_TREE;
4625         }
4626       else
4627         scope = complete_type_or_else (scope, NULL_TREE);
4628       /* Let the name lookup machinery know that we are processing a
4629          class member access expression.  */
4630       parser->context->object_type = scope;
4631       /* If something went wrong, we want to be able to discern that case,
4632          as opposed to the case where there was no SCOPE due to the type
4633          of expression being dependent.  */
4634       if (!scope)
4635         scope = error_mark_node;
4636       /* If the SCOPE was erroneous, make the various semantic analysis
4637          functions exit quickly -- and without issuing additional error
4638          messages.  */
4639       if (scope == error_mark_node)
4640         postfix_expression = error_mark_node;
4641     }
4642
4643   /* Assume this expression is not a pseudo-destructor access.  */
4644   pseudo_destructor_p = false;
4645
4646   /* If the SCOPE is a scalar type, then, if this is a valid program,
4647      we must be looking at a pseudo-destructor-name.  */
4648   if (scope && SCALAR_TYPE_P (scope))
4649     {
4650       tree s;
4651       tree type;
4652
4653       cp_parser_parse_tentatively (parser);
4654       /* Parse the pseudo-destructor-name.  */
4655       s = NULL_TREE;
4656       cp_parser_pseudo_destructor_name (parser, &s, &type);
4657       if (cp_parser_parse_definitely (parser))
4658         {
4659           pseudo_destructor_p = true;
4660           postfix_expression
4661             = finish_pseudo_destructor_expr (postfix_expression,
4662                                              s, TREE_TYPE (type));
4663         }
4664     }
4665
4666   if (!pseudo_destructor_p)
4667     {
4668       /* If the SCOPE is not a scalar type, we are looking at an
4669          ordinary class member access expression, rather than a
4670          pseudo-destructor-name.  */
4671       bool template_p;
4672       /* Parse the id-expression.  */
4673       name = (cp_parser_id_expression
4674               (parser,
4675                cp_parser_optional_template_keyword (parser),
4676                /*check_dependency_p=*/true,
4677                &template_p,
4678                /*declarator_p=*/false,
4679                /*optional_p=*/false));
4680       /* In general, build a SCOPE_REF if the member name is qualified.
4681          However, if the name was not dependent and has already been
4682          resolved; there is no need to build the SCOPE_REF.  For example;
4683
4684              struct X { void f(); };
4685              template <typename T> void f(T* t) { t->X::f(); }
4686
4687          Even though "t" is dependent, "X::f" is not and has been resolved
4688          to a BASELINK; there is no need to include scope information.  */
4689
4690       /* But we do need to remember that there was an explicit scope for
4691          virtual function calls.  */
4692       if (parser->scope)
4693         *idk = CP_ID_KIND_QUALIFIED;
4694
4695       /* If the name is a template-id that names a type, we will get a
4696          TYPE_DECL here.  That is invalid code.  */
4697       if (TREE_CODE (name) == TYPE_DECL)
4698         {
4699           error ("invalid use of %qD", name);
4700           postfix_expression = error_mark_node;
4701         }
4702       else
4703         {
4704           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4705             {
4706               name = build_qualified_name (/*type=*/NULL_TREE,
4707                                            parser->scope,
4708                                            name,
4709                                            template_p);
4710               parser->scope = NULL_TREE;
4711               parser->qualifying_scope = NULL_TREE;
4712               parser->object_scope = NULL_TREE;
4713             }
4714           if (scope && name && BASELINK_P (name))
4715             adjust_result_of_qualified_name_lookup
4716               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4717           postfix_expression
4718             = finish_class_member_access_expr (postfix_expression, name,
4719                                                template_p);
4720         }
4721     }
4722
4723   /* We no longer need to look up names in the scope of the object on
4724      the left-hand side of the `.' or `->' operator.  */
4725   parser->context->object_type = NULL_TREE;
4726
4727   /* Outside of offsetof, these operators may not appear in
4728      constant-expressions.  */
4729   if (!for_offsetof
4730       && (cp_parser_non_integral_constant_expression
4731           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4732     postfix_expression = error_mark_node;
4733
4734   return postfix_expression;
4735 }
4736
4737 /* Parse a parenthesized expression-list.
4738
4739    expression-list:
4740      assignment-expression
4741      expression-list, assignment-expression
4742
4743    attribute-list:
4744      expression-list
4745      identifier
4746      identifier, expression-list
4747
4748    CAST_P is true if this expression is the target of a cast.
4749
4750    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4751    representation of an assignment-expression.  Note that a TREE_LIST
4752    is returned even if there is only a single expression in the list.
4753    error_mark_node is returned if the ( and or ) are
4754    missing. NULL_TREE is returned on no expressions. The parentheses
4755    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4756    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4757    indicates whether or not all of the expressions in the list were
4758    constant.  */
4759
4760 static tree
4761 cp_parser_parenthesized_expression_list (cp_parser* parser,
4762                                          bool is_attribute_list,
4763                                          bool cast_p,
4764                                          bool *non_constant_p)
4765 {
4766   tree expression_list = NULL_TREE;
4767   bool fold_expr_p = is_attribute_list;
4768   tree identifier = NULL_TREE;
4769
4770   /* Assume all the expressions will be constant.  */
4771   if (non_constant_p)
4772     *non_constant_p = false;
4773
4774   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4775     return error_mark_node;
4776
4777   /* Consume expressions until there are no more.  */
4778   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4779     while (true)
4780       {
4781         tree expr;
4782
4783         /* At the beginning of attribute lists, check to see if the
4784            next token is an identifier.  */
4785         if (is_attribute_list
4786             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4787           {
4788             cp_token *token;
4789
4790             /* Consume the identifier.  */
4791             token = cp_lexer_consume_token (parser->lexer);
4792             /* Save the identifier.  */
4793             identifier = token->u.value;
4794           }
4795         else
4796           {
4797             /* Parse the next assignment-expression.  */
4798             if (non_constant_p)
4799               {
4800                 bool expr_non_constant_p;
4801                 expr = (cp_parser_constant_expression
4802                         (parser, /*allow_non_constant_p=*/true,
4803                          &expr_non_constant_p));
4804                 if (expr_non_constant_p)
4805                   *non_constant_p = true;
4806               }
4807             else
4808               expr = cp_parser_assignment_expression (parser, cast_p);
4809
4810             if (fold_expr_p)
4811               expr = fold_non_dependent_expr (expr);
4812
4813              /* Add it to the list.  We add error_mark_node
4814                 expressions to the list, so that we can still tell if
4815                 the correct form for a parenthesized expression-list
4816                 is found. That gives better errors.  */
4817             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4818
4819             if (expr == error_mark_node)
4820               goto skip_comma;
4821           }
4822
4823         /* After the first item, attribute lists look the same as
4824            expression lists.  */
4825         is_attribute_list = false;
4826
4827       get_comma:;
4828         /* If the next token isn't a `,', then we are done.  */
4829         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4830           break;
4831
4832         /* Otherwise, consume the `,' and keep going.  */
4833         cp_lexer_consume_token (parser->lexer);
4834       }
4835
4836   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4837     {
4838       int ending;
4839
4840     skip_comma:;
4841       /* We try and resync to an unnested comma, as that will give the
4842          user better diagnostics.  */
4843       ending = cp_parser_skip_to_closing_parenthesis (parser,
4844                                                       /*recovering=*/true,
4845                                                       /*or_comma=*/true,
4846                                                       /*consume_paren=*/true);
4847       if (ending < 0)
4848         goto get_comma;
4849       if (!ending)
4850         return error_mark_node;
4851     }
4852
4853   /* We built up the list in reverse order so we must reverse it now.  */
4854   expression_list = nreverse (expression_list);
4855   if (identifier)
4856     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4857
4858   return expression_list;
4859 }
4860
4861 /* Parse a pseudo-destructor-name.
4862
4863    pseudo-destructor-name:
4864      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4865      :: [opt] nested-name-specifier template template-id :: ~ type-name
4866      :: [opt] nested-name-specifier [opt] ~ type-name
4867
4868    If either of the first two productions is used, sets *SCOPE to the
4869    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4870    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4871    or ERROR_MARK_NODE if the parse fails.  */
4872
4873 static void
4874 cp_parser_pseudo_destructor_name (cp_parser* parser,
4875                                   tree* scope,
4876                                   tree* type)
4877 {
4878   bool nested_name_specifier_p;
4879
4880   /* Assume that things will not work out.  */
4881   *type = error_mark_node;
4882
4883   /* Look for the optional `::' operator.  */
4884   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4885   /* Look for the optional nested-name-specifier.  */
4886   nested_name_specifier_p
4887     = (cp_parser_nested_name_specifier_opt (parser,
4888                                             /*typename_keyword_p=*/false,
4889                                             /*check_dependency_p=*/true,
4890                                             /*type_p=*/false,
4891                                             /*is_declaration=*/true)
4892        != NULL_TREE);
4893   /* Now, if we saw a nested-name-specifier, we might be doing the
4894      second production.  */
4895   if (nested_name_specifier_p
4896       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4897     {
4898       /* Consume the `template' keyword.  */
4899       cp_lexer_consume_token (parser->lexer);
4900       /* Parse the template-id.  */
4901       cp_parser_template_id (parser,
4902                              /*template_keyword_p=*/true,
4903                              /*check_dependency_p=*/false,
4904                              /*is_declaration=*/true);
4905       /* Look for the `::' token.  */
4906       cp_parser_require (parser, CPP_SCOPE, "`::'");
4907     }
4908   /* If the next token is not a `~', then there might be some
4909      additional qualification.  */
4910   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4911     {
4912       /* Look for the type-name.  */
4913       *scope = TREE_TYPE (cp_parser_type_name (parser));
4914
4915       if (*scope == error_mark_node)
4916         return;
4917
4918       /* If we don't have ::~, then something has gone wrong.  Since
4919          the only caller of this function is looking for something
4920          after `.' or `->' after a scalar type, most likely the
4921          program is trying to get a member of a non-aggregate
4922          type.  */
4923       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4924           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4925         {
4926           cp_parser_error (parser, "request for member of non-aggregate type");
4927           return;
4928         }
4929
4930       /* Look for the `::' token.  */
4931       cp_parser_require (parser, CPP_SCOPE, "`::'");
4932     }
4933   else
4934     *scope = NULL_TREE;
4935
4936   /* Look for the `~'.  */
4937   cp_parser_require (parser, CPP_COMPL, "`~'");
4938   /* Look for the type-name again.  We are not responsible for
4939      checking that it matches the first type-name.  */
4940   *type = cp_parser_type_name (parser);
4941 }
4942
4943 /* Parse a unary-expression.
4944
4945    unary-expression:
4946      postfix-expression
4947      ++ cast-expression
4948      -- cast-expression
4949      unary-operator cast-expression
4950      sizeof unary-expression
4951      sizeof ( type-id )
4952      new-expression
4953      delete-expression
4954
4955    GNU Extensions:
4956
4957    unary-expression:
4958      __extension__ cast-expression
4959      __alignof__ unary-expression
4960      __alignof__ ( type-id )
4961      __real__ cast-expression
4962      __imag__ cast-expression
4963      && identifier
4964
4965    ADDRESS_P is true iff the unary-expression is appearing as the
4966    operand of the `&' operator.   CAST_P is true if this expression is
4967    the target of a cast.
4968
4969    Returns a representation of the expression.  */
4970
4971 static tree
4972 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4973 {
4974   cp_token *token;
4975   enum tree_code unary_operator;
4976
4977   /* Peek at the next token.  */
4978   token = cp_lexer_peek_token (parser->lexer);
4979   /* Some keywords give away the kind of expression.  */
4980   if (token->type == CPP_KEYWORD)
4981     {
4982       enum rid keyword = token->keyword;
4983
4984       switch (keyword)
4985         {
4986         case RID_ALIGNOF:
4987         case RID_SIZEOF:
4988           {
4989             tree operand;
4990             enum tree_code op;
4991
4992             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4993             /* Consume the token.  */
4994             cp_lexer_consume_token (parser->lexer);
4995             /* Parse the operand.  */
4996             operand = cp_parser_sizeof_operand (parser, keyword);
4997
4998             if (TYPE_P (operand))
4999               return cxx_sizeof_or_alignof_type (operand, op, true);
5000             else
5001               return cxx_sizeof_or_alignof_expr (operand, op);
5002           }
5003
5004         case RID_NEW:
5005           return cp_parser_new_expression (parser);
5006
5007         case RID_DELETE:
5008           return cp_parser_delete_expression (parser);
5009
5010         case RID_EXTENSION:
5011           {
5012             /* The saved value of the PEDANTIC flag.  */
5013             int saved_pedantic;
5014             tree expr;
5015
5016             /* Save away the PEDANTIC flag.  */
5017             cp_parser_extension_opt (parser, &saved_pedantic);
5018             /* Parse the cast-expression.  */
5019             expr = cp_parser_simple_cast_expression (parser);
5020             /* Restore the PEDANTIC flag.  */
5021             pedantic = saved_pedantic;
5022
5023             return expr;
5024           }
5025
5026         case RID_REALPART:
5027         case RID_IMAGPART:
5028           {
5029             tree expression;
5030
5031             /* Consume the `__real__' or `__imag__' token.  */
5032             cp_lexer_consume_token (parser->lexer);
5033             /* Parse the cast-expression.  */
5034             expression = cp_parser_simple_cast_expression (parser);
5035             /* Create the complete representation.  */
5036             return build_x_unary_op ((keyword == RID_REALPART
5037                                       ? REALPART_EXPR : IMAGPART_EXPR),
5038                                      expression);
5039           }
5040           break;
5041
5042         default:
5043           break;
5044         }
5045     }
5046
5047   /* Look for the `:: new' and `:: delete', which also signal the
5048      beginning of a new-expression, or delete-expression,
5049      respectively.  If the next token is `::', then it might be one of
5050      these.  */
5051   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5052     {
5053       enum rid keyword;
5054
5055       /* See if the token after the `::' is one of the keywords in
5056          which we're interested.  */
5057       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5058       /* If it's `new', we have a new-expression.  */
5059       if (keyword == RID_NEW)
5060         return cp_parser_new_expression (parser);
5061       /* Similarly, for `delete'.  */
5062       else if (keyword == RID_DELETE)
5063         return cp_parser_delete_expression (parser);
5064     }
5065
5066   /* Look for a unary operator.  */
5067   unary_operator = cp_parser_unary_operator (token);
5068   /* The `++' and `--' operators can be handled similarly, even though
5069      they are not technically unary-operators in the grammar.  */
5070   if (unary_operator == ERROR_MARK)
5071     {
5072       if (token->type == CPP_PLUS_PLUS)
5073         unary_operator = PREINCREMENT_EXPR;
5074       else if (token->type == CPP_MINUS_MINUS)
5075         unary_operator = PREDECREMENT_EXPR;
5076       /* Handle the GNU address-of-label extension.  */
5077       else if (cp_parser_allow_gnu_extensions_p (parser)
5078                && token->type == CPP_AND_AND)
5079         {
5080           tree identifier;
5081
5082           /* Consume the '&&' token.  */
5083           cp_lexer_consume_token (parser->lexer);
5084           /* Look for the identifier.  */
5085           identifier = cp_parser_identifier (parser);
5086           /* Create an expression representing the address.  */
5087           return finish_label_address_expr (identifier);
5088         }
5089     }
5090   if (unary_operator != ERROR_MARK)
5091     {
5092       tree cast_expression;
5093       tree expression = error_mark_node;
5094       const char *non_constant_p = NULL;
5095
5096       /* Consume the operator token.  */
5097       token = cp_lexer_consume_token (parser->lexer);
5098       /* Parse the cast-expression.  */
5099       cast_expression
5100         = cp_parser_cast_expression (parser,
5101                                      unary_operator == ADDR_EXPR,
5102                                      /*cast_p=*/false);
5103       /* Now, build an appropriate representation.  */
5104       switch (unary_operator)
5105         {
5106         case INDIRECT_REF:
5107           non_constant_p = "`*'";
5108           expression = build_x_indirect_ref (cast_expression, "unary *");
5109           break;
5110
5111         case ADDR_EXPR:
5112           non_constant_p = "`&'";
5113           /* Fall through.  */
5114         case BIT_NOT_EXPR:
5115           expression = build_x_unary_op (unary_operator, cast_expression);
5116           break;
5117
5118         case PREINCREMENT_EXPR:
5119         case PREDECREMENT_EXPR:
5120           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5121                             ? "`++'" : "`--'");
5122           /* Fall through.  */
5123         case UNARY_PLUS_EXPR:
5124         case NEGATE_EXPR:
5125         case TRUTH_NOT_EXPR:
5126           expression = finish_unary_op_expr (unary_operator, cast_expression);
5127           break;
5128
5129         default:
5130           gcc_unreachable ();
5131         }
5132
5133       if (non_constant_p
5134           && cp_parser_non_integral_constant_expression (parser,
5135                                                          non_constant_p))
5136         expression = error_mark_node;
5137
5138       return expression;
5139     }
5140
5141   return cp_parser_postfix_expression (parser, address_p, cast_p);
5142 }
5143
5144 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5145    unary-operator, the corresponding tree code is returned.  */
5146
5147 static enum tree_code
5148 cp_parser_unary_operator (cp_token* token)
5149 {
5150   switch (token->type)
5151     {
5152     case CPP_MULT:
5153       return INDIRECT_REF;
5154
5155     case CPP_AND:
5156       return ADDR_EXPR;
5157
5158     case CPP_PLUS:
5159       return UNARY_PLUS_EXPR;
5160
5161     case CPP_MINUS:
5162       return NEGATE_EXPR;
5163
5164     case CPP_NOT:
5165       return TRUTH_NOT_EXPR;
5166
5167     case CPP_COMPL:
5168       return BIT_NOT_EXPR;
5169
5170     default:
5171       return ERROR_MARK;
5172     }
5173 }
5174
5175 /* Parse a new-expression.
5176
5177    new-expression:
5178      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5179      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5180
5181    Returns a representation of the expression.  */
5182
5183 static tree
5184 cp_parser_new_expression (cp_parser* parser)
5185 {
5186   bool global_scope_p;
5187   tree placement;
5188   tree type;
5189   tree initializer;
5190   tree nelts;
5191
5192   /* Look for the optional `::' operator.  */
5193   global_scope_p
5194     = (cp_parser_global_scope_opt (parser,
5195                                    /*current_scope_valid_p=*/false)
5196        != NULL_TREE);
5197   /* Look for the `new' operator.  */
5198   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5199   /* There's no easy way to tell a new-placement from the
5200      `( type-id )' construct.  */
5201   cp_parser_parse_tentatively (parser);
5202   /* Look for a new-placement.  */
5203   placement = cp_parser_new_placement (parser);
5204   /* If that didn't work out, there's no new-placement.  */
5205   if (!cp_parser_parse_definitely (parser))
5206     placement = NULL_TREE;
5207
5208   /* If the next token is a `(', then we have a parenthesized
5209      type-id.  */
5210   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5211     {
5212       /* Consume the `('.  */
5213       cp_lexer_consume_token (parser->lexer);
5214       /* Parse the type-id.  */
5215       type = cp_parser_type_id (parser);
5216       /* Look for the closing `)'.  */
5217       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5218       /* There should not be a direct-new-declarator in this production,
5219          but GCC used to allowed this, so we check and emit a sensible error
5220          message for this case.  */
5221       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5222         {
5223           error ("array bound forbidden after parenthesized type-id");
5224           inform ("try removing the parentheses around the type-id");
5225           cp_parser_direct_new_declarator (parser);
5226         }
5227       nelts = NULL_TREE;
5228     }
5229   /* Otherwise, there must be a new-type-id.  */
5230   else
5231     type = cp_parser_new_type_id (parser, &nelts);
5232
5233   /* If the next token is a `(', then we have a new-initializer.  */
5234   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5235     initializer = cp_parser_new_initializer (parser);
5236   else
5237     initializer = NULL_TREE;
5238
5239   /* A new-expression may not appear in an integral constant
5240      expression.  */
5241   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5242     return error_mark_node;
5243
5244   /* Create a representation of the new-expression.  */
5245   return build_new (placement, type, nelts, initializer, global_scope_p);
5246 }
5247
5248 /* Parse a new-placement.
5249
5250    new-placement:
5251      ( expression-list )
5252
5253    Returns the same representation as for an expression-list.  */
5254
5255 static tree
5256 cp_parser_new_placement (cp_parser* parser)
5257 {
5258   tree expression_list;
5259
5260   /* Parse the expression-list.  */
5261   expression_list = (cp_parser_parenthesized_expression_list
5262                      (parser, false, /*cast_p=*/false,
5263                       /*non_constant_p=*/NULL));
5264
5265   return expression_list;
5266 }
5267
5268 /* Parse a new-type-id.
5269
5270    new-type-id:
5271      type-specifier-seq new-declarator [opt]
5272
5273    Returns the TYPE allocated.  If the new-type-id indicates an array
5274    type, *NELTS is set to the number of elements in the last array
5275    bound; the TYPE will not include the last array bound.  */
5276
5277 static tree
5278 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5279 {
5280   cp_decl_specifier_seq type_specifier_seq;
5281   cp_declarator *new_declarator;
5282   cp_declarator *declarator;
5283   cp_declarator *outer_declarator;
5284   const char *saved_message;
5285   tree type;
5286
5287   /* The type-specifier sequence must not contain type definitions.
5288      (It cannot contain declarations of new types either, but if they
5289      are not definitions we will catch that because they are not
5290      complete.)  */
5291   saved_message = parser->type_definition_forbidden_message;
5292   parser->type_definition_forbidden_message
5293     = "types may not be defined in a new-type-id";
5294   /* Parse the type-specifier-seq.  */
5295   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5296                                 &type_specifier_seq);
5297   /* Restore the old message.  */
5298   parser->type_definition_forbidden_message = saved_message;
5299   /* Parse the new-declarator.  */
5300   new_declarator = cp_parser_new_declarator_opt (parser);
5301
5302   /* Determine the number of elements in the last array dimension, if
5303      any.  */
5304   *nelts = NULL_TREE;
5305   /* Skip down to the last array dimension.  */
5306   declarator = new_declarator;
5307   outer_declarator = NULL;
5308   while (declarator && (declarator->kind == cdk_pointer
5309                         || declarator->kind == cdk_ptrmem))
5310     {
5311       outer_declarator = declarator;
5312       declarator = declarator->declarator;
5313     }
5314   while (declarator
5315          && declarator->kind == cdk_array
5316          && declarator->declarator
5317          && declarator->declarator->kind == cdk_array)
5318     {
5319       outer_declarator = declarator;
5320       declarator = declarator->declarator;
5321     }
5322
5323   if (declarator && declarator->kind == cdk_array)
5324     {
5325       *nelts = declarator->u.array.bounds;
5326       if (*nelts == error_mark_node)
5327         *nelts = integer_one_node;
5328
5329       if (outer_declarator)
5330         outer_declarator->declarator = declarator->declarator;
5331       else
5332         new_declarator = NULL;
5333     }
5334
5335   type = groktypename (&type_specifier_seq, new_declarator);
5336   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5337     {
5338       *nelts = array_type_nelts_top (type);
5339       type = TREE_TYPE (type);
5340     }
5341   return type;
5342 }
5343
5344 /* Parse an (optional) new-declarator.
5345
5346    new-declarator:
5347      ptr-operator new-declarator [opt]
5348      direct-new-declarator
5349
5350    Returns the declarator.  */
5351
5352 static cp_declarator *
5353 cp_parser_new_declarator_opt (cp_parser* parser)
5354 {
5355   enum tree_code code;
5356   tree type;
5357   cp_cv_quals cv_quals;
5358
5359   /* We don't know if there's a ptr-operator next, or not.  */
5360   cp_parser_parse_tentatively (parser);
5361   /* Look for a ptr-operator.  */
5362   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5363   /* If that worked, look for more new-declarators.  */
5364   if (cp_parser_parse_definitely (parser))
5365     {
5366       cp_declarator *declarator;
5367
5368       /* Parse another optional declarator.  */
5369       declarator = cp_parser_new_declarator_opt (parser);
5370
5371       /* Create the representation of the declarator.  */
5372       if (type)
5373         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5374       else if (code == INDIRECT_REF)
5375         declarator = make_pointer_declarator (cv_quals, declarator);
5376       else
5377         declarator = make_reference_declarator (cv_quals, declarator);
5378
5379       return declarator;
5380     }
5381
5382   /* If the next token is a `[', there is a direct-new-declarator.  */
5383   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5384     return cp_parser_direct_new_declarator (parser);
5385
5386   return NULL;
5387 }
5388
5389 /* Parse a direct-new-declarator.
5390
5391    direct-new-declarator:
5392      [ expression ]
5393      direct-new-declarator [constant-expression]
5394
5395    */
5396
5397 static cp_declarator *
5398 cp_parser_direct_new_declarator (cp_parser* parser)
5399 {
5400   cp_declarator *declarator = NULL;
5401
5402   while (true)
5403     {
5404       tree expression;
5405
5406       /* Look for the opening `['.  */
5407       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5408       /* The first expression is not required to be constant.  */
5409       if (!declarator)
5410         {
5411           expression = cp_parser_expression (parser, /*cast_p=*/false);
5412           /* The standard requires that the expression have integral
5413              type.  DR 74 adds enumeration types.  We believe that the
5414              real intent is that these expressions be handled like the
5415              expression in a `switch' condition, which also allows
5416              classes with a single conversion to integral or
5417              enumeration type.  */
5418           if (!processing_template_decl)
5419             {
5420               expression
5421                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5422                                               expression,
5423                                               /*complain=*/true);
5424               if (!expression)
5425                 {
5426                   error ("expression in new-declarator must have integral "
5427                          "or enumeration type");
5428                   expression = error_mark_node;
5429                 }
5430             }
5431         }
5432       /* But all the other expressions must be.  */
5433       else
5434         expression
5435           = cp_parser_constant_expression (parser,
5436                                            /*allow_non_constant=*/false,
5437                                            NULL);
5438       /* Look for the closing `]'.  */
5439       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5440
5441       /* Add this bound to the declarator.  */
5442       declarator = make_array_declarator (declarator, expression);
5443
5444       /* If the next token is not a `[', then there are no more
5445          bounds.  */
5446       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5447         break;
5448     }
5449
5450   return declarator;
5451 }
5452
5453 /* Parse a new-initializer.
5454
5455    new-initializer:
5456      ( expression-list [opt] )
5457
5458    Returns a representation of the expression-list.  If there is no
5459    expression-list, VOID_ZERO_NODE is returned.  */
5460
5461 static tree
5462 cp_parser_new_initializer (cp_parser* parser)
5463 {
5464   tree expression_list;
5465
5466   expression_list = (cp_parser_parenthesized_expression_list
5467                      (parser, false, /*cast_p=*/false,
5468                       /*non_constant_p=*/NULL));
5469   if (!expression_list)
5470     expression_list = void_zero_node;
5471
5472   return expression_list;
5473 }
5474
5475 /* Parse a delete-expression.
5476
5477    delete-expression:
5478      :: [opt] delete cast-expression
5479      :: [opt] delete [ ] cast-expression
5480
5481    Returns a representation of the expression.  */
5482
5483 static tree
5484 cp_parser_delete_expression (cp_parser* parser)
5485 {
5486   bool global_scope_p;
5487   bool array_p;
5488   tree expression;
5489
5490   /* Look for the optional `::' operator.  */
5491   global_scope_p
5492     = (cp_parser_global_scope_opt (parser,
5493                                    /*current_scope_valid_p=*/false)
5494        != NULL_TREE);
5495   /* Look for the `delete' keyword.  */
5496   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5497   /* See if the array syntax is in use.  */
5498   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5499     {
5500       /* Consume the `[' token.  */
5501       cp_lexer_consume_token (parser->lexer);
5502       /* Look for the `]' token.  */
5503       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5504       /* Remember that this is the `[]' construct.  */
5505       array_p = true;
5506     }
5507   else
5508     array_p = false;
5509
5510   /* Parse the cast-expression.  */
5511   expression = cp_parser_simple_cast_expression (parser);
5512
5513   /* A delete-expression may not appear in an integral constant
5514      expression.  */
5515   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5516     return error_mark_node;
5517
5518   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5519 }
5520
5521 /* Parse a cast-expression.
5522
5523    cast-expression:
5524      unary-expression
5525      ( type-id ) cast-expression
5526
5527    ADDRESS_P is true iff the unary-expression is appearing as the
5528    operand of the `&' operator.   CAST_P is true if this expression is
5529    the target of a cast.
5530
5531    Returns a representation of the expression.  */
5532
5533 static tree
5534 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5535 {
5536   /* If it's a `(', then we might be looking at a cast.  */
5537   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5538     {
5539       tree type = NULL_TREE;
5540       tree expr = NULL_TREE;
5541       bool compound_literal_p;
5542       const char *saved_message;
5543
5544       /* There's no way to know yet whether or not this is a cast.
5545          For example, `(int (3))' is a unary-expression, while `(int)
5546          3' is a cast.  So, we resort to parsing tentatively.  */
5547       cp_parser_parse_tentatively (parser);
5548       /* Types may not be defined in a cast.  */
5549       saved_message = parser->type_definition_forbidden_message;
5550       parser->type_definition_forbidden_message
5551         = "types may not be defined in casts";
5552       /* Consume the `('.  */
5553       cp_lexer_consume_token (parser->lexer);
5554       /* A very tricky bit is that `(struct S) { 3 }' is a
5555          compound-literal (which we permit in C++ as an extension).
5556          But, that construct is not a cast-expression -- it is a
5557          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5558          is legal; if the compound-literal were a cast-expression,
5559          you'd need an extra set of parentheses.)  But, if we parse
5560          the type-id, and it happens to be a class-specifier, then we
5561          will commit to the parse at that point, because we cannot
5562          undo the action that is done when creating a new class.  So,
5563          then we cannot back up and do a postfix-expression.
5564
5565          Therefore, we scan ahead to the closing `)', and check to see
5566          if the token after the `)' is a `{'.  If so, we are not
5567          looking at a cast-expression.
5568
5569          Save tokens so that we can put them back.  */
5570       cp_lexer_save_tokens (parser->lexer);
5571       /* Skip tokens until the next token is a closing parenthesis.
5572          If we find the closing `)', and the next token is a `{', then
5573          we are looking at a compound-literal.  */
5574       compound_literal_p
5575         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5576                                                   /*consume_paren=*/true)
5577            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5578       /* Roll back the tokens we skipped.  */
5579       cp_lexer_rollback_tokens (parser->lexer);
5580       /* If we were looking at a compound-literal, simulate an error
5581          so that the call to cp_parser_parse_definitely below will
5582          fail.  */
5583       if (compound_literal_p)
5584         cp_parser_simulate_error (parser);
5585       else
5586         {
5587           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5588           parser->in_type_id_in_expr_p = true;
5589           /* Look for the type-id.  */
5590           type = cp_parser_type_id (parser);
5591           /* Look for the closing `)'.  */
5592           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5593           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5594         }
5595
5596       /* Restore the saved message.  */
5597       parser->type_definition_forbidden_message = saved_message;
5598
5599       /* If ok so far, parse the dependent expression. We cannot be
5600          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5601          ctor of T, but looks like a cast to function returning T
5602          without a dependent expression.  */
5603       if (!cp_parser_error_occurred (parser))
5604         expr = cp_parser_cast_expression (parser,
5605                                           /*address_p=*/false,
5606                                           /*cast_p=*/true);
5607
5608       if (cp_parser_parse_definitely (parser))
5609         {
5610           /* Warn about old-style casts, if so requested.  */
5611           if (warn_old_style_cast
5612               && !in_system_header
5613               && !VOID_TYPE_P (type)
5614               && current_lang_name != lang_name_c)
5615             warning (OPT_Wold_style_cast, "use of old-style cast");
5616
5617           /* Only type conversions to integral or enumeration types
5618              can be used in constant-expressions.  */
5619           if (!cast_valid_in_integral_constant_expression_p (type)
5620               && (cp_parser_non_integral_constant_expression
5621                   (parser,
5622                    "a cast to a type other than an integral or "
5623                    "enumeration type")))
5624             return error_mark_node;
5625
5626           /* Perform the cast.  */
5627           expr = build_c_cast (type, expr);
5628           return expr;
5629         }
5630     }
5631
5632   /* If we get here, then it's not a cast, so it must be a
5633      unary-expression.  */
5634   return cp_parser_unary_expression (parser, address_p, cast_p);
5635 }
5636
5637 /* Parse a binary expression of the general form:
5638
5639    pm-expression:
5640      cast-expression
5641      pm-expression .* cast-expression
5642      pm-expression ->* cast-expression
5643
5644    multiplicative-expression:
5645      pm-expression
5646      multiplicative-expression * pm-expression
5647      multiplicative-expression / pm-expression
5648      multiplicative-expression % pm-expression
5649
5650    additive-expression:
5651      multiplicative-expression
5652      additive-expression + multiplicative-expression
5653      additive-expression - multiplicative-expression
5654
5655    shift-expression:
5656      additive-expression
5657      shift-expression << additive-expression
5658      shift-expression >> additive-expression
5659
5660    relational-expression:
5661      shift-expression
5662      relational-expression < shift-expression
5663      relational-expression > shift-expression
5664      relational-expression <= shift-expression
5665      relational-expression >= shift-expression
5666
5667   GNU Extension:
5668
5669    relational-expression:
5670      relational-expression <? shift-expression
5671      relational-expression >? shift-expression
5672
5673    equality-expression:
5674      relational-expression
5675      equality-expression == relational-expression
5676      equality-expression != relational-expression
5677
5678    and-expression:
5679      equality-expression
5680      and-expression & equality-expression
5681
5682    exclusive-or-expression:
5683      and-expression
5684      exclusive-or-expression ^ and-expression
5685
5686    inclusive-or-expression:
5687      exclusive-or-expression
5688      inclusive-or-expression | exclusive-or-expression
5689
5690    logical-and-expression:
5691      inclusive-or-expression
5692      logical-and-expression && inclusive-or-expression
5693
5694    logical-or-expression:
5695      logical-and-expression
5696      logical-or-expression || logical-and-expression
5697
5698    All these are implemented with a single function like:
5699
5700    binary-expression:
5701      simple-cast-expression
5702      binary-expression <token> binary-expression
5703
5704    CAST_P is true if this expression is the target of a cast.
5705
5706    The binops_by_token map is used to get the tree codes for each <token> type.
5707    binary-expressions are associated according to a precedence table.  */
5708
5709 #define TOKEN_PRECEDENCE(token) \
5710   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5711    ? PREC_NOT_OPERATOR \
5712    : binops_by_token[token->type].prec)
5713
5714 static tree
5715 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5716 {
5717   cp_parser_expression_stack stack;
5718   cp_parser_expression_stack_entry *sp = &stack[0];
5719   tree lhs, rhs;
5720   cp_token *token;
5721   enum tree_code tree_type, lhs_type, rhs_type;
5722   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5723   bool overloaded_p;
5724
5725   /* Parse the first expression.  */
5726   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5727   lhs_type = ERROR_MARK;
5728
5729   for (;;)
5730     {
5731       /* Get an operator token.  */
5732       token = cp_lexer_peek_token (parser->lexer);
5733
5734       new_prec = TOKEN_PRECEDENCE (token);
5735
5736       /* Popping an entry off the stack means we completed a subexpression:
5737          - either we found a token which is not an operator (`>' where it is not
5738            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5739            will happen repeatedly;
5740          - or, we found an operator which has lower priority.  This is the case
5741            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5742            parsing `3 * 4'.  */
5743       if (new_prec <= prec)
5744         {
5745           if (sp == stack)
5746             break;
5747           else
5748             goto pop;
5749         }
5750
5751      get_rhs:
5752       tree_type = binops_by_token[token->type].tree_type;
5753
5754       /* We used the operator token.  */
5755       cp_lexer_consume_token (parser->lexer);
5756
5757       /* Extract another operand.  It may be the RHS of this expression
5758          or the LHS of a new, higher priority expression.  */
5759       rhs = cp_parser_simple_cast_expression (parser);
5760       rhs_type = ERROR_MARK;
5761
5762       /* Get another operator token.  Look up its precedence to avoid
5763          building a useless (immediately popped) stack entry for common
5764          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5765       token = cp_lexer_peek_token (parser->lexer);
5766       lookahead_prec = TOKEN_PRECEDENCE (token);
5767       if (lookahead_prec > new_prec)
5768         {
5769           /* ... and prepare to parse the RHS of the new, higher priority
5770              expression.  Since precedence levels on the stack are
5771              monotonically increasing, we do not have to care about
5772              stack overflows.  */
5773           sp->prec = prec;
5774           sp->tree_type = tree_type;
5775           sp->lhs = lhs;
5776           sp->lhs_type = lhs_type;
5777           sp++;
5778           lhs = rhs;
5779           lhs_type = rhs_type;
5780           prec = new_prec;
5781           new_prec = lookahead_prec;
5782           goto get_rhs;
5783
5784          pop:
5785           /* If the stack is not empty, we have parsed into LHS the right side
5786              (`4' in the example above) of an expression we had suspended.
5787              We can use the information on the stack to recover the LHS (`3')
5788              from the stack together with the tree code (`MULT_EXPR'), and
5789              the precedence of the higher level subexpression
5790              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5791              which will be used to actually build the additive expression.  */
5792           --sp;
5793           prec = sp->prec;
5794           tree_type = sp->tree_type;
5795           rhs = lhs;
5796           rhs_type = lhs_type;
5797           lhs = sp->lhs;
5798           lhs_type = sp->lhs_type;
5799         }
5800
5801       overloaded_p = false;
5802       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5803                                &overloaded_p);
5804       lhs_type = tree_type;
5805
5806       /* If the binary operator required the use of an overloaded operator,
5807          then this expression cannot be an integral constant-expression.
5808          An overloaded operator can be used even if both operands are
5809          otherwise permissible in an integral constant-expression if at
5810          least one of the operands is of enumeration type.  */
5811
5812       if (overloaded_p
5813           && (cp_parser_non_integral_constant_expression
5814               (parser, "calls to overloaded operators")))
5815         return error_mark_node;
5816     }
5817
5818   return lhs;
5819 }
5820
5821
5822 /* Parse the `? expression : assignment-expression' part of a
5823    conditional-expression.  The LOGICAL_OR_EXPR is the
5824    logical-or-expression that started the conditional-expression.
5825    Returns a representation of the entire conditional-expression.
5826
5827    This routine is used by cp_parser_assignment_expression.
5828
5829      ? expression : assignment-expression
5830
5831    GNU Extensions:
5832
5833      ? : assignment-expression */
5834
5835 static tree
5836 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5837 {
5838   tree expr;
5839   tree assignment_expr;
5840
5841   /* Consume the `?' token.  */
5842   cp_lexer_consume_token (parser->lexer);
5843   if (cp_parser_allow_gnu_extensions_p (parser)
5844       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5845     /* Implicit true clause.  */
5846     expr = NULL_TREE;
5847   else
5848     /* Parse the expression.  */
5849     expr = cp_parser_expression (parser, /*cast_p=*/false);
5850
5851   /* The next token should be a `:'.  */
5852   cp_parser_require (parser, CPP_COLON, "`:'");
5853   /* Parse the assignment-expression.  */
5854   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5855
5856   /* Build the conditional-expression.  */
5857   return build_x_conditional_expr (logical_or_expr,
5858                                    expr,
5859                                    assignment_expr);
5860 }
5861
5862 /* Parse an assignment-expression.
5863
5864    assignment-expression:
5865      conditional-expression
5866      logical-or-expression assignment-operator assignment_expression
5867      throw-expression
5868
5869    CAST_P is true if this expression is the target of a cast.
5870
5871    Returns a representation for the expression.  */
5872
5873 static tree
5874 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5875 {
5876   tree expr;
5877
5878   /* If the next token is the `throw' keyword, then we're looking at
5879      a throw-expression.  */
5880   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5881     expr = cp_parser_throw_expression (parser);
5882   /* Otherwise, it must be that we are looking at a
5883      logical-or-expression.  */
5884   else
5885     {
5886       /* Parse the binary expressions (logical-or-expression).  */
5887       expr = cp_parser_binary_expression (parser, cast_p);
5888       /* If the next token is a `?' then we're actually looking at a
5889          conditional-expression.  */
5890       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5891         return cp_parser_question_colon_clause (parser, expr);
5892       else
5893         {
5894           enum tree_code assignment_operator;
5895
5896           /* If it's an assignment-operator, we're using the second
5897              production.  */
5898           assignment_operator
5899             = cp_parser_assignment_operator_opt (parser);
5900           if (assignment_operator != ERROR_MARK)
5901             {
5902               tree rhs;
5903
5904               /* Parse the right-hand side of the assignment.  */
5905               rhs = cp_parser_assignment_expression (parser, cast_p);
5906               /* An assignment may not appear in a
5907                  constant-expression.  */
5908               if (cp_parser_non_integral_constant_expression (parser,
5909                                                               "an assignment"))
5910                 return error_mark_node;
5911               /* Build the assignment expression.  */
5912               expr = build_x_modify_expr (expr,
5913                                           assignment_operator,
5914                                           rhs);
5915             }
5916         }
5917     }
5918
5919   return expr;
5920 }
5921
5922 /* Parse an (optional) assignment-operator.
5923
5924    assignment-operator: one of
5925      = *= /= %= += -= >>= <<= &= ^= |=
5926
5927    GNU Extension:
5928
5929    assignment-operator: one of
5930      <?= >?=
5931
5932    If the next token is an assignment operator, the corresponding tree
5933    code is returned, and the token is consumed.  For example, for
5934    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5935    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5936    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5937    operator, ERROR_MARK is returned.  */
5938
5939 static enum tree_code
5940 cp_parser_assignment_operator_opt (cp_parser* parser)
5941 {
5942   enum tree_code op;
5943   cp_token *token;
5944
5945   /* Peek at the next toen.  */
5946   token = cp_lexer_peek_token (parser->lexer);
5947
5948   switch (token->type)
5949     {
5950     case CPP_EQ:
5951       op = NOP_EXPR;
5952       break;
5953
5954     case CPP_MULT_EQ:
5955       op = MULT_EXPR;
5956       break;
5957
5958     case CPP_DIV_EQ:
5959       op = TRUNC_DIV_EXPR;
5960       break;
5961
5962     case CPP_MOD_EQ:
5963       op = TRUNC_MOD_EXPR;
5964       break;
5965
5966     case CPP_PLUS_EQ:
5967       op = PLUS_EXPR;
5968       break;
5969
5970     case CPP_MINUS_EQ:
5971       op = MINUS_EXPR;
5972       break;
5973
5974     case CPP_RSHIFT_EQ:
5975       op = RSHIFT_EXPR;
5976       break;
5977
5978     case CPP_LSHIFT_EQ:
5979       op = LSHIFT_EXPR;
5980       break;
5981
5982     case CPP_AND_EQ:
5983       op = BIT_AND_EXPR;
5984       break;
5985
5986     case CPP_XOR_EQ:
5987       op = BIT_XOR_EXPR;
5988       break;
5989
5990     case CPP_OR_EQ:
5991       op = BIT_IOR_EXPR;
5992       break;
5993
5994     default:
5995       /* Nothing else is an assignment operator.  */
5996       op = ERROR_MARK;
5997     }
5998
5999   /* If it was an assignment operator, consume it.  */
6000   if (op != ERROR_MARK)
6001     cp_lexer_consume_token (parser->lexer);
6002
6003   return op;
6004 }
6005
6006 /* Parse an expression.
6007
6008    expression:
6009      assignment-expression
6010      expression , assignment-expression
6011
6012    CAST_P is true if this expression is the target of a cast.
6013
6014    Returns a representation of the expression.  */
6015
6016 static tree
6017 cp_parser_expression (cp_parser* parser, bool cast_p)
6018 {
6019   tree expression = NULL_TREE;
6020
6021   while (true)
6022     {
6023       tree assignment_expression;
6024
6025       /* Parse the next assignment-expression.  */
6026       assignment_expression
6027         = cp_parser_assignment_expression (parser, cast_p);
6028       /* If this is the first assignment-expression, we can just
6029          save it away.  */
6030       if (!expression)
6031         expression = assignment_expression;
6032       else
6033         expression = build_x_compound_expr (expression,
6034                                             assignment_expression);
6035       /* If the next token is not a comma, then we are done with the
6036          expression.  */
6037       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6038         break;
6039       /* Consume the `,'.  */
6040       cp_lexer_consume_token (parser->lexer);
6041       /* A comma operator cannot appear in a constant-expression.  */
6042       if (cp_parser_non_integral_constant_expression (parser,
6043                                                       "a comma operator"))
6044         expression = error_mark_node;
6045     }
6046
6047   return expression;
6048 }
6049
6050 /* Parse a constant-expression.
6051
6052    constant-expression:
6053      conditional-expression
6054
6055   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6056   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6057   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6058   is false, NON_CONSTANT_P should be NULL.  */
6059
6060 static tree
6061 cp_parser_constant_expression (cp_parser* parser,
6062                                bool allow_non_constant_p,
6063                                bool *non_constant_p)
6064 {
6065   bool saved_integral_constant_expression_p;
6066   bool saved_allow_non_integral_constant_expression_p;
6067   bool saved_non_integral_constant_expression_p;
6068   tree expression;
6069
6070   /* It might seem that we could simply parse the
6071      conditional-expression, and then check to see if it were
6072      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6073      one that the compiler can figure out is constant, possibly after
6074      doing some simplifications or optimizations.  The standard has a
6075      precise definition of constant-expression, and we must honor
6076      that, even though it is somewhat more restrictive.
6077
6078      For example:
6079
6080        int i[(2, 3)];
6081
6082      is not a legal declaration, because `(2, 3)' is not a
6083      constant-expression.  The `,' operator is forbidden in a
6084      constant-expression.  However, GCC's constant-folding machinery
6085      will fold this operation to an INTEGER_CST for `3'.  */
6086
6087   /* Save the old settings.  */
6088   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6089   saved_allow_non_integral_constant_expression_p
6090     = parser->allow_non_integral_constant_expression_p;
6091   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6092   /* We are now parsing a constant-expression.  */
6093   parser->integral_constant_expression_p = true;
6094   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6095   parser->non_integral_constant_expression_p = false;
6096   integral_constant_expr_p = true;
6097   /* Although the grammar says "conditional-expression", we parse an
6098      "assignment-expression", which also permits "throw-expression"
6099      and the use of assignment operators.  In the case that
6100      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6101      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6102      actually essential that we look for an assignment-expression.
6103      For example, cp_parser_initializer_clauses uses this function to
6104      determine whether a particular assignment-expression is in fact
6105      constant.  */
6106   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6107   /* Restore the old settings.  */
6108   integral_constant_expr_p = false;
6109   parser->integral_constant_expression_p
6110     = saved_integral_constant_expression_p;
6111   parser->allow_non_integral_constant_expression_p
6112     = saved_allow_non_integral_constant_expression_p;
6113   if (allow_non_constant_p)
6114     *non_constant_p = parser->non_integral_constant_expression_p;
6115   else if (parser->non_integral_constant_expression_p)
6116     expression = error_mark_node;
6117   parser->non_integral_constant_expression_p
6118     = saved_non_integral_constant_expression_p;
6119
6120   return expression;
6121 }
6122
6123 /* Parse __builtin_offsetof.
6124
6125    offsetof-expression:
6126      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6127
6128    offsetof-member-designator:
6129      id-expression
6130      | offsetof-member-designator "." id-expression
6131      | offsetof-member-designator "[" expression "]"  */
6132
6133 static tree
6134 cp_parser_builtin_offsetof (cp_parser *parser)
6135 {
6136   int save_ice_p, save_non_ice_p;
6137   tree type, expr;
6138   cp_id_kind dummy;
6139
6140   /* We're about to accept non-integral-constant things, but will
6141      definitely yield an integral constant expression.  Save and
6142      restore these values around our local parsing.  */
6143   save_ice_p = parser->integral_constant_expression_p;
6144   save_non_ice_p = parser->non_integral_constant_expression_p;
6145
6146   /* Consume the "__builtin_offsetof" token.  */
6147   cp_lexer_consume_token (parser->lexer);
6148   /* Consume the opening `('.  */
6149   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6150   /* Parse the type-id.  */
6151   type = cp_parser_type_id (parser);
6152   /* Look for the `,'.  */
6153   cp_parser_require (parser, CPP_COMMA, "`,'");
6154
6155   /* Build the (type *)null that begins the traditional offsetof macro.  */
6156   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6157
6158   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6159   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6160                                                  true, &dummy);
6161   while (true)
6162     {
6163       cp_token *token = cp_lexer_peek_token (parser->lexer);
6164       switch (token->type)
6165         {
6166         case CPP_OPEN_SQUARE:
6167           /* offsetof-member-designator "[" expression "]" */
6168           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6169           break;
6170
6171         case CPP_DOT:
6172           /* offsetof-member-designator "." identifier */
6173           cp_lexer_consume_token (parser->lexer);
6174           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6175                                                          true, &dummy);
6176           break;
6177
6178         case CPP_CLOSE_PAREN:
6179           /* Consume the ")" token.  */
6180           cp_lexer_consume_token (parser->lexer);
6181           goto success;
6182
6183         default:
6184           /* Error.  We know the following require will fail, but
6185              that gives the proper error message.  */
6186           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6187           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6188           expr = error_mark_node;
6189           goto failure;
6190         }
6191     }
6192
6193  success:
6194   /* If we're processing a template, we can't finish the semantics yet.
6195      Otherwise we can fold the entire expression now.  */
6196   if (processing_template_decl)
6197     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6198   else
6199     expr = finish_offsetof (expr);
6200
6201  failure:
6202   parser->integral_constant_expression_p = save_ice_p;
6203   parser->non_integral_constant_expression_p = save_non_ice_p;
6204
6205   return expr;
6206 }
6207
6208 /* Statements [gram.stmt.stmt]  */
6209
6210 /* Parse a statement.
6211
6212    statement:
6213      labeled-statement
6214      expression-statement
6215      compound-statement
6216      selection-statement
6217      iteration-statement
6218      jump-statement
6219      declaration-statement
6220      try-block
6221
6222   IN_COMPOUND is true when the statement is nested inside a
6223   cp_parser_compound_statement; this matters for certain pragmas.
6224
6225   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6226   is a (possibly labeled) if statement which is not enclosed in braces
6227   and has an else clause.  This is used to implement -Wparentheses.  */
6228
6229 static void
6230 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6231                      bool in_compound, bool *if_p)
6232 {
6233   tree statement;
6234   cp_token *token;
6235   location_t statement_location;
6236
6237  restart:
6238   if (if_p != NULL)
6239     *if_p = false;
6240   /* There is no statement yet.  */
6241   statement = NULL_TREE;
6242   /* Peek at the next token.  */
6243   token = cp_lexer_peek_token (parser->lexer);
6244   /* Remember the location of the first token in the statement.  */
6245   statement_location = token->location;
6246   /* If this is a keyword, then that will often determine what kind of
6247      statement we have.  */
6248   if (token->type == CPP_KEYWORD)
6249     {
6250       enum rid keyword = token->keyword;
6251
6252       switch (keyword)
6253         {
6254         case RID_CASE:
6255         case RID_DEFAULT:
6256           /* Looks like a labeled-statement with a case label.
6257              Parse the label, and then use tail recursion to parse
6258              the statement.  */
6259           cp_parser_label_for_labeled_statement (parser);
6260           goto restart;
6261
6262         case RID_IF:
6263         case RID_SWITCH:
6264           statement = cp_parser_selection_statement (parser, if_p);
6265           break;
6266
6267         case RID_WHILE:
6268         case RID_DO:
6269         case RID_FOR:
6270           statement = cp_parser_iteration_statement (parser);
6271           break;
6272
6273         case RID_BREAK:
6274         case RID_CONTINUE:
6275         case RID_RETURN:
6276         case RID_GOTO:
6277           statement = cp_parser_jump_statement (parser);
6278           break;
6279
6280           /* Objective-C++ exception-handling constructs.  */
6281         case RID_AT_TRY:
6282         case RID_AT_CATCH:
6283         case RID_AT_FINALLY:
6284         case RID_AT_SYNCHRONIZED:
6285         case RID_AT_THROW:
6286           statement = cp_parser_objc_statement (parser);
6287           break;
6288
6289         case RID_TRY:
6290           statement = cp_parser_try_block (parser);
6291           break;
6292
6293         default:
6294           /* It might be a keyword like `int' that can start a
6295              declaration-statement.  */
6296           break;
6297         }
6298     }
6299   else if (token->type == CPP_NAME)
6300     {
6301       /* If the next token is a `:', then we are looking at a
6302          labeled-statement.  */
6303       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6304       if (token->type == CPP_COLON)
6305         {
6306           /* Looks like a labeled-statement with an ordinary label.
6307              Parse the label, and then use tail recursion to parse
6308              the statement.  */
6309           cp_parser_label_for_labeled_statement (parser);
6310           goto restart;
6311         }
6312     }
6313   /* Anything that starts with a `{' must be a compound-statement.  */
6314   else if (token->type == CPP_OPEN_BRACE)
6315     statement = cp_parser_compound_statement (parser, NULL, false);
6316   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6317      a statement all its own.  */
6318   else if (token->type == CPP_PRAGMA)
6319     {
6320       /* Only certain OpenMP pragmas are attached to statements, and thus
6321          are considered statements themselves.  All others are not.  In
6322          the context of a compound, accept the pragma as a "statement" and
6323          return so that we can check for a close brace.  Otherwise we
6324          require a real statement and must go back and read one.  */
6325       if (in_compound)
6326         cp_parser_pragma (parser, pragma_compound);
6327       else if (!cp_parser_pragma (parser, pragma_stmt))
6328         goto restart;
6329       return;
6330     }
6331   else if (token->type == CPP_EOF)
6332     {
6333       cp_parser_error (parser, "expected statement");
6334       return;
6335     }
6336
6337   /* Everything else must be a declaration-statement or an
6338      expression-statement.  Try for the declaration-statement
6339      first, unless we are looking at a `;', in which case we know that
6340      we have an expression-statement.  */
6341   if (!statement)
6342     {
6343       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6344         {
6345           cp_parser_parse_tentatively (parser);
6346           /* Try to parse the declaration-statement.  */
6347           cp_parser_declaration_statement (parser);
6348           /* If that worked, we're done.  */
6349           if (cp_parser_parse_definitely (parser))
6350             return;
6351         }
6352       /* Look for an expression-statement instead.  */
6353       statement = cp_parser_expression_statement (parser, in_statement_expr);
6354     }
6355
6356   /* Set the line number for the statement.  */
6357   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6358     SET_EXPR_LOCATION (statement, statement_location);
6359 }
6360
6361 /* Parse the label for a labeled-statement, i.e.
6362
6363    identifier :
6364    case constant-expression :
6365    default :
6366
6367    GNU Extension:
6368    case constant-expression ... constant-expression : statement
6369
6370    When a label is parsed without errors, the label is added to the
6371    parse tree by the finish_* functions, so this function doesn't
6372    have to return the label.  */
6373
6374 static void
6375 cp_parser_label_for_labeled_statement (cp_parser* parser)
6376 {
6377   cp_token *token;
6378
6379   /* The next token should be an identifier.  */
6380   token = cp_lexer_peek_token (parser->lexer);
6381   if (token->type != CPP_NAME
6382       && token->type != CPP_KEYWORD)
6383     {
6384       cp_parser_error (parser, "expected labeled-statement");
6385       return;
6386     }
6387
6388   switch (token->keyword)
6389     {
6390     case RID_CASE:
6391       {
6392         tree expr, expr_hi;
6393         cp_token *ellipsis;
6394
6395         /* Consume the `case' token.  */
6396         cp_lexer_consume_token (parser->lexer);
6397         /* Parse the constant-expression.  */
6398         expr = cp_parser_constant_expression (parser,
6399                                               /*allow_non_constant_p=*/false,
6400                                               NULL);
6401
6402         ellipsis = cp_lexer_peek_token (parser->lexer);
6403         if (ellipsis->type == CPP_ELLIPSIS)
6404           {
6405             /* Consume the `...' token.  */
6406             cp_lexer_consume_token (parser->lexer);
6407             expr_hi =
6408               cp_parser_constant_expression (parser,
6409                                              /*allow_non_constant_p=*/false,
6410                                              NULL);
6411             /* We don't need to emit warnings here, as the common code
6412                will do this for us.  */
6413           }
6414         else
6415           expr_hi = NULL_TREE;
6416
6417         if (parser->in_switch_statement_p)
6418           finish_case_label (expr, expr_hi);
6419         else
6420           error ("case label %qE not within a switch statement", expr);
6421       }
6422       break;
6423
6424     case RID_DEFAULT:
6425       /* Consume the `default' token.  */
6426       cp_lexer_consume_token (parser->lexer);
6427
6428       if (parser->in_switch_statement_p)
6429         finish_case_label (NULL_TREE, NULL_TREE);
6430       else
6431         error ("case label not within a switch statement");
6432       break;
6433
6434     default:
6435       /* Anything else must be an ordinary label.  */
6436       finish_label_stmt (cp_parser_identifier (parser));
6437       break;
6438     }
6439
6440   /* Require the `:' token.  */
6441   cp_parser_require (parser, CPP_COLON, "`:'");
6442 }
6443
6444 /* Parse an expression-statement.
6445
6446    expression-statement:
6447      expression [opt] ;
6448
6449    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6450    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6451    indicates whether this expression-statement is part of an
6452    expression statement.  */
6453
6454 static tree
6455 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6456 {
6457   tree statement = NULL_TREE;
6458
6459   /* If the next token is a ';', then there is no expression
6460      statement.  */
6461   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6462     statement = cp_parser_expression (parser, /*cast_p=*/false);
6463
6464   /* Consume the final `;'.  */
6465   cp_parser_consume_semicolon_at_end_of_statement (parser);
6466
6467   if (in_statement_expr
6468       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6469     /* This is the final expression statement of a statement
6470        expression.  */
6471     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6472   else if (statement)
6473     statement = finish_expr_stmt (statement);
6474   else
6475     finish_stmt ();
6476
6477   return statement;
6478 }
6479
6480 /* Parse a compound-statement.
6481
6482    compound-statement:
6483      { statement-seq [opt] }
6484
6485    Returns a tree representing the statement.  */
6486
6487 static tree
6488 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6489                               bool in_try)
6490 {
6491   tree compound_stmt;
6492
6493   /* Consume the `{'.  */
6494   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6495     return error_mark_node;
6496   /* Begin the compound-statement.  */
6497   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6498   /* Parse an (optional) statement-seq.  */
6499   cp_parser_statement_seq_opt (parser, in_statement_expr);
6500   /* Finish the compound-statement.  */
6501   finish_compound_stmt (compound_stmt);
6502   /* Consume the `}'.  */
6503   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6504
6505   return compound_stmt;
6506 }
6507
6508 /* Parse an (optional) statement-seq.
6509
6510    statement-seq:
6511      statement
6512      statement-seq [opt] statement  */
6513
6514 static void
6515 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6516 {
6517   /* Scan statements until there aren't any more.  */
6518   while (true)
6519     {
6520       cp_token *token = cp_lexer_peek_token (parser->lexer);
6521
6522       /* If we're looking at a `}', then we've run out of statements.  */
6523       if (token->type == CPP_CLOSE_BRACE
6524           || token->type == CPP_EOF
6525           || token->type == CPP_PRAGMA_EOL)
6526         break;
6527
6528       /* Parse the statement.  */
6529       cp_parser_statement (parser, in_statement_expr, true, NULL);
6530     }
6531 }
6532
6533 /* Parse a selection-statement.
6534
6535    selection-statement:
6536      if ( condition ) statement
6537      if ( condition ) statement else statement
6538      switch ( condition ) statement
6539
6540    Returns the new IF_STMT or SWITCH_STMT.
6541
6542    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6543    is a (possibly labeled) if statement which is not enclosed in
6544    braces and has an else clause.  This is used to implement
6545    -Wparentheses.  */
6546
6547 static tree
6548 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6549 {
6550   cp_token *token;
6551   enum rid keyword;
6552
6553   if (if_p != NULL)
6554     *if_p = false;
6555
6556   /* Peek at the next token.  */
6557   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6558
6559   /* See what kind of keyword it is.  */
6560   keyword = token->keyword;
6561   switch (keyword)
6562     {
6563     case RID_IF:
6564     case RID_SWITCH:
6565       {
6566         tree statement;
6567         tree condition;
6568
6569         /* Look for the `('.  */
6570         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6571           {
6572             cp_parser_skip_to_end_of_statement (parser);
6573             return error_mark_node;
6574           }
6575
6576         /* Begin the selection-statement.  */
6577         if (keyword == RID_IF)
6578           statement = begin_if_stmt ();
6579         else
6580           statement = begin_switch_stmt ();
6581
6582         /* Parse the condition.  */
6583         condition = cp_parser_condition (parser);
6584         /* Look for the `)'.  */
6585         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6586           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6587                                                  /*consume_paren=*/true);
6588
6589         if (keyword == RID_IF)
6590           {
6591             bool nested_if;
6592
6593             /* Add the condition.  */
6594             finish_if_stmt_cond (condition, statement);
6595
6596             /* Parse the then-clause.  */
6597             cp_parser_implicitly_scoped_statement (parser, &nested_if);
6598             finish_then_clause (statement);
6599
6600             /* If the next token is `else', parse the else-clause.  */
6601             if (cp_lexer_next_token_is_keyword (parser->lexer,
6602                                                 RID_ELSE))
6603               {
6604                 /* Consume the `else' keyword.  */
6605                 cp_lexer_consume_token (parser->lexer);
6606                 begin_else_clause (statement);
6607                 /* Parse the else-clause.  */
6608                 cp_parser_implicitly_scoped_statement (parser, NULL);
6609                 finish_else_clause (statement);
6610
6611                 /* If we are currently parsing a then-clause, then
6612                    IF_P will not be NULL.  We set it to true to
6613                    indicate that this if statement has an else clause.
6614                    This may trigger the Wparentheses warning below
6615                    when we get back up to the parent if statement.  */
6616                 if (if_p != NULL)
6617                   *if_p = true;
6618               }
6619             else
6620               {
6621                 /* This if statement does not have an else clause.  If
6622                    NESTED_IF is true, then the then-clause is an if
6623                    statement which does have an else clause.  We warn
6624                    about the potential ambiguity.  */
6625                 if (nested_if)
6626                   warning (OPT_Wparentheses,
6627                            ("%Hsuggest explicit braces "
6628                             "to avoid ambiguous %<else%>"),
6629                            EXPR_LOCUS (statement));
6630               }
6631
6632             /* Now we're all done with the if-statement.  */
6633             finish_if_stmt (statement);
6634           }
6635         else
6636           {
6637             bool in_switch_statement_p;
6638             unsigned char in_statement;
6639
6640             /* Add the condition.  */
6641             finish_switch_cond (condition, statement);
6642
6643             /* Parse the body of the switch-statement.  */
6644             in_switch_statement_p = parser->in_switch_statement_p;
6645             in_statement = parser->in_statement;
6646             parser->in_switch_statement_p = true;
6647             parser->in_statement |= IN_SWITCH_STMT;
6648             cp_parser_implicitly_scoped_statement (parser, NULL);
6649             parser->in_switch_statement_p = in_switch_statement_p;
6650             parser->in_statement = in_statement;
6651
6652             /* Now we're all done with the switch-statement.  */
6653             finish_switch_stmt (statement);
6654           }
6655
6656         return statement;
6657       }
6658       break;
6659
6660     default:
6661       cp_parser_error (parser, "expected selection-statement");
6662       return error_mark_node;
6663     }
6664 }
6665
6666 /* Parse a condition.
6667
6668    condition:
6669      expression
6670      type-specifier-seq declarator = assignment-expression
6671
6672    GNU Extension:
6673
6674    condition:
6675      type-specifier-seq declarator asm-specification [opt]
6676        attributes [opt] = assignment-expression
6677
6678    Returns the expression that should be tested.  */
6679
6680 static tree
6681 cp_parser_condition (cp_parser* parser)
6682 {
6683   cp_decl_specifier_seq type_specifiers;
6684   const char *saved_message;
6685
6686   /* Try the declaration first.  */
6687   cp_parser_parse_tentatively (parser);
6688   /* New types are not allowed in the type-specifier-seq for a
6689      condition.  */
6690   saved_message = parser->type_definition_forbidden_message;
6691   parser->type_definition_forbidden_message
6692     = "types may not be defined in conditions";
6693   /* Parse the type-specifier-seq.  */
6694   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6695                                 &type_specifiers);
6696   /* Restore the saved message.  */
6697   parser->type_definition_forbidden_message = saved_message;
6698   /* If all is well, we might be looking at a declaration.  */
6699   if (!cp_parser_error_occurred (parser))
6700     {
6701       tree decl;
6702       tree asm_specification;
6703       tree attributes;
6704       cp_declarator *declarator;
6705       tree initializer = NULL_TREE;
6706
6707       /* Parse the declarator.  */
6708       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6709                                          /*ctor_dtor_or_conv_p=*/NULL,
6710                                          /*parenthesized_p=*/NULL,
6711                                          /*member_p=*/false);
6712       /* Parse the attributes.  */
6713       attributes = cp_parser_attributes_opt (parser);
6714       /* Parse the asm-specification.  */
6715       asm_specification = cp_parser_asm_specification_opt (parser);
6716       /* If the next token is not an `=', then we might still be
6717          looking at an expression.  For example:
6718
6719            if (A(a).x)
6720
6721          looks like a decl-specifier-seq and a declarator -- but then
6722          there is no `=', so this is an expression.  */
6723       cp_parser_require (parser, CPP_EQ, "`='");
6724       /* If we did see an `=', then we are looking at a declaration
6725          for sure.  */
6726       if (cp_parser_parse_definitely (parser))
6727         {
6728           tree pushed_scope;
6729           bool non_constant_p;
6730
6731           /* Create the declaration.  */
6732           decl = start_decl (declarator, &type_specifiers,
6733                              /*initialized_p=*/true,
6734                              attributes, /*prefix_attributes=*/NULL_TREE,
6735                              &pushed_scope);
6736           /* Parse the assignment-expression.  */
6737           initializer
6738             = cp_parser_constant_expression (parser,
6739                                              /*allow_non_constant_p=*/true,
6740                                              &non_constant_p);
6741           if (!non_constant_p)
6742             initializer = fold_non_dependent_expr (initializer);
6743
6744           /* Process the initializer.  */
6745           cp_finish_decl (decl,
6746                           initializer, !non_constant_p,
6747                           asm_specification,
6748                           LOOKUP_ONLYCONVERTING);
6749
6750           if (pushed_scope)
6751             pop_scope (pushed_scope);
6752
6753           return convert_from_reference (decl);
6754         }
6755     }
6756   /* If we didn't even get past the declarator successfully, we are
6757      definitely not looking at a declaration.  */
6758   else
6759     cp_parser_abort_tentative_parse (parser);
6760
6761   /* Otherwise, we are looking at an expression.  */
6762   return cp_parser_expression (parser, /*cast_p=*/false);
6763 }
6764
6765 /* Parse an iteration-statement.
6766
6767    iteration-statement:
6768      while ( condition ) statement
6769      do statement while ( expression ) ;
6770      for ( for-init-statement condition [opt] ; expression [opt] )
6771        statement
6772
6773    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6774
6775 static tree
6776 cp_parser_iteration_statement (cp_parser* parser)
6777 {
6778   cp_token *token;
6779   enum rid keyword;
6780   tree statement;
6781   unsigned char in_statement;
6782
6783   /* Peek at the next token.  */
6784   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6785   if (!token)
6786     return error_mark_node;
6787
6788   /* Remember whether or not we are already within an iteration
6789      statement.  */
6790   in_statement = parser->in_statement;
6791
6792   /* See what kind of keyword it is.  */
6793   keyword = token->keyword;
6794   switch (keyword)
6795     {
6796     case RID_WHILE:
6797       {
6798         tree condition;
6799
6800         /* Begin the while-statement.  */
6801         statement = begin_while_stmt ();
6802         /* Look for the `('.  */
6803         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6804         /* Parse the condition.  */
6805         condition = cp_parser_condition (parser);
6806         finish_while_stmt_cond (condition, statement);
6807         /* Look for the `)'.  */
6808         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6809         /* Parse the dependent statement.  */
6810         parser->in_statement = IN_ITERATION_STMT;
6811         cp_parser_already_scoped_statement (parser);
6812         parser->in_statement = in_statement;
6813         /* We're done with the while-statement.  */
6814         finish_while_stmt (statement);
6815       }
6816       break;
6817
6818     case RID_DO:
6819       {
6820         tree expression;
6821
6822         /* Begin the do-statement.  */
6823         statement = begin_do_stmt ();
6824         /* Parse the body of the do-statement.  */
6825         parser->in_statement = IN_ITERATION_STMT;
6826         cp_parser_implicitly_scoped_statement (parser, NULL);
6827         parser->in_statement = in_statement;
6828         finish_do_body (statement);
6829         /* Look for the `while' keyword.  */
6830         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6831         /* Look for the `('.  */
6832         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6833         /* Parse the expression.  */
6834         expression = cp_parser_expression (parser, /*cast_p=*/false);
6835         /* We're done with the do-statement.  */
6836         finish_do_stmt (expression, statement);
6837         /* Look for the `)'.  */
6838         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6839         /* Look for the `;'.  */
6840         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6841       }
6842       break;
6843
6844     case RID_FOR:
6845       {
6846         tree condition = NULL_TREE;
6847         tree expression = NULL_TREE;
6848
6849         /* Begin the for-statement.  */
6850         statement = begin_for_stmt ();
6851         /* Look for the `('.  */
6852         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6853         /* Parse the initialization.  */
6854         cp_parser_for_init_statement (parser);
6855         finish_for_init_stmt (statement);
6856
6857         /* If there's a condition, process it.  */
6858         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6859           condition = cp_parser_condition (parser);
6860         finish_for_cond (condition, statement);
6861         /* Look for the `;'.  */
6862         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6863
6864         /* If there's an expression, process it.  */
6865         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6866           expression = cp_parser_expression (parser, /*cast_p=*/false);
6867         finish_for_expr (expression, statement);
6868         /* Look for the `)'.  */
6869         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6870
6871         /* Parse the body of the for-statement.  */
6872         parser->in_statement = IN_ITERATION_STMT;
6873         cp_parser_already_scoped_statement (parser);
6874         parser->in_statement = in_statement;
6875
6876         /* We're done with the for-statement.  */
6877         finish_for_stmt (statement);
6878       }
6879       break;
6880
6881     default:
6882       cp_parser_error (parser, "expected iteration-statement");
6883       statement = error_mark_node;
6884       break;
6885     }
6886
6887   return statement;
6888 }
6889
6890 /* Parse a for-init-statement.
6891
6892    for-init-statement:
6893      expression-statement
6894      simple-declaration  */
6895
6896 static void
6897 cp_parser_for_init_statement (cp_parser* parser)
6898 {
6899   /* If the next token is a `;', then we have an empty
6900      expression-statement.  Grammatically, this is also a
6901      simple-declaration, but an invalid one, because it does not
6902      declare anything.  Therefore, if we did not handle this case
6903      specially, we would issue an error message about an invalid
6904      declaration.  */
6905   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6906     {
6907       /* We're going to speculatively look for a declaration, falling back
6908          to an expression, if necessary.  */
6909       cp_parser_parse_tentatively (parser);
6910       /* Parse the declaration.  */
6911       cp_parser_simple_declaration (parser,
6912                                     /*function_definition_allowed_p=*/false);
6913       /* If the tentative parse failed, then we shall need to look for an
6914          expression-statement.  */
6915       if (cp_parser_parse_definitely (parser))
6916         return;
6917     }
6918
6919   cp_parser_expression_statement (parser, false);
6920 }
6921
6922 /* Parse a jump-statement.
6923
6924    jump-statement:
6925      break ;
6926      continue ;
6927      return expression [opt] ;
6928      goto identifier ;
6929
6930    GNU extension:
6931
6932    jump-statement:
6933      goto * expression ;
6934
6935    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6936
6937 static tree
6938 cp_parser_jump_statement (cp_parser* parser)
6939 {
6940   tree statement = error_mark_node;
6941   cp_token *token;
6942   enum rid keyword;
6943
6944   /* Peek at the next token.  */
6945   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6946   if (!token)
6947     return error_mark_node;
6948
6949   /* See what kind of keyword it is.  */
6950   keyword = token->keyword;
6951   switch (keyword)
6952     {
6953     case RID_BREAK:
6954       switch (parser->in_statement)
6955         {
6956         case 0:
6957           error ("break statement not within loop or switch");
6958           break;
6959         default:
6960           gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6961                       || parser->in_statement == IN_ITERATION_STMT);
6962           statement = finish_break_stmt ();
6963           break;
6964         case IN_OMP_BLOCK:
6965           error ("invalid exit from OpenMP structured block");
6966           break;
6967         case IN_OMP_FOR:
6968           error ("break statement used with OpenMP for loop");
6969           break;
6970         }
6971       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6972       break;
6973
6974     case RID_CONTINUE:
6975       switch (parser->in_statement & ~IN_SWITCH_STMT)
6976         {
6977         case 0:
6978           error ("continue statement not within a loop");
6979           break;
6980         case IN_ITERATION_STMT:
6981         case IN_OMP_FOR:
6982           statement = finish_continue_stmt ();
6983           break;
6984         case IN_OMP_BLOCK:
6985           error ("invalid exit from OpenMP structured block");
6986           break;
6987         default:
6988           gcc_unreachable ();
6989         }
6990       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6991       break;
6992
6993     case RID_RETURN:
6994       {
6995         tree expr;
6996
6997         /* If the next token is a `;', then there is no
6998            expression.  */
6999         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7000           expr = cp_parser_expression (parser, /*cast_p=*/false);
7001         else
7002           expr = NULL_TREE;
7003         /* Build the return-statement.  */
7004         statement = finish_return_stmt (expr);
7005         /* Look for the final `;'.  */
7006         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7007       }
7008       break;
7009
7010     case RID_GOTO:
7011       /* Create the goto-statement.  */
7012       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7013         {
7014           /* Issue a warning about this use of a GNU extension.  */
7015           if (pedantic)
7016             pedwarn ("ISO C++ forbids computed gotos");
7017           /* Consume the '*' token.  */
7018           cp_lexer_consume_token (parser->lexer);
7019           /* Parse the dependent expression.  */
7020           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7021         }
7022       else
7023         finish_goto_stmt (cp_parser_identifier (parser));
7024       /* Look for the final `;'.  */
7025       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7026       break;
7027
7028     default:
7029       cp_parser_error (parser, "expected jump-statement");
7030       break;
7031     }
7032
7033   return statement;
7034 }
7035
7036 /* Parse a declaration-statement.
7037
7038    declaration-statement:
7039      block-declaration  */
7040
7041 static void
7042 cp_parser_declaration_statement (cp_parser* parser)
7043 {
7044   void *p;
7045
7046   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7047   p = obstack_alloc (&declarator_obstack, 0);
7048
7049  /* Parse the block-declaration.  */
7050   cp_parser_block_declaration (parser, /*statement_p=*/true);
7051
7052   /* Free any declarators allocated.  */
7053   obstack_free (&declarator_obstack, p);
7054
7055   /* Finish off the statement.  */
7056   finish_stmt ();
7057 }
7058
7059 /* Some dependent statements (like `if (cond) statement'), are
7060    implicitly in their own scope.  In other words, if the statement is
7061    a single statement (as opposed to a compound-statement), it is
7062    none-the-less treated as if it were enclosed in braces.  Any
7063    declarations appearing in the dependent statement are out of scope
7064    after control passes that point.  This function parses a statement,
7065    but ensures that is in its own scope, even if it is not a
7066    compound-statement.
7067
7068    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7069    is a (possibly labeled) if statement which is not enclosed in
7070    braces and has an else clause.  This is used to implement
7071    -Wparentheses.
7072
7073    Returns the new statement.  */
7074
7075 static tree
7076 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7077 {
7078   tree statement;
7079
7080   if (if_p != NULL)
7081     *if_p = false;
7082
7083   /* Mark if () ; with a special NOP_EXPR.  */
7084   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7085     {
7086       cp_lexer_consume_token (parser->lexer);
7087       statement = add_stmt (build_empty_stmt ());
7088     }
7089   /* if a compound is opened, we simply parse the statement directly.  */
7090   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7091     statement = cp_parser_compound_statement (parser, NULL, false);
7092   /* If the token is not a `{', then we must take special action.  */
7093   else
7094     {
7095       /* Create a compound-statement.  */
7096       statement = begin_compound_stmt (0);
7097       /* Parse the dependent-statement.  */
7098       cp_parser_statement (parser, NULL_TREE, false, if_p);
7099       /* Finish the dummy compound-statement.  */
7100       finish_compound_stmt (statement);
7101     }
7102
7103   /* Return the statement.  */
7104   return statement;
7105 }
7106
7107 /* For some dependent statements (like `while (cond) statement'), we
7108    have already created a scope.  Therefore, even if the dependent
7109    statement is a compound-statement, we do not want to create another
7110    scope.  */
7111
7112 static void
7113 cp_parser_already_scoped_statement (cp_parser* parser)
7114 {
7115   /* If the token is a `{', then we must take special action.  */
7116   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7117     cp_parser_statement (parser, NULL_TREE, false, NULL);
7118   else
7119     {
7120       /* Avoid calling cp_parser_compound_statement, so that we
7121          don't create a new scope.  Do everything else by hand.  */
7122       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7123       cp_parser_statement_seq_opt (parser, NULL_TREE);
7124       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7125     }
7126 }
7127
7128 /* Declarations [gram.dcl.dcl] */
7129
7130 /* Parse an optional declaration-sequence.
7131
7132    declaration-seq:
7133      declaration
7134      declaration-seq declaration  */
7135
7136 static void
7137 cp_parser_declaration_seq_opt (cp_parser* parser)
7138 {
7139   while (true)
7140     {
7141       cp_token *token;
7142
7143       token = cp_lexer_peek_token (parser->lexer);
7144
7145       if (token->type == CPP_CLOSE_BRACE
7146           || token->type == CPP_EOF
7147           || token->type == CPP_PRAGMA_EOL)
7148         break;
7149
7150       if (token->type == CPP_SEMICOLON)
7151         {
7152           /* A declaration consisting of a single semicolon is
7153              invalid.  Allow it unless we're being pedantic.  */
7154           cp_lexer_consume_token (parser->lexer);
7155           if (pedantic && !in_system_header)
7156             pedwarn ("extra %<;%>");
7157           continue;
7158         }
7159
7160       /* If we're entering or exiting a region that's implicitly
7161          extern "C", modify the lang context appropriately.  */
7162       if (!parser->implicit_extern_c && token->implicit_extern_c)
7163         {
7164           push_lang_context (lang_name_c);
7165           parser->implicit_extern_c = true;
7166         }
7167       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7168         {
7169           pop_lang_context ();
7170           parser->implicit_extern_c = false;
7171         }
7172
7173       if (token->type == CPP_PRAGMA)
7174         {
7175           /* A top-level declaration can consist solely of a #pragma.
7176              A nested declaration cannot, so this is done here and not
7177              in cp_parser_declaration.  (A #pragma at block scope is
7178              handled in cp_parser_statement.)  */
7179           cp_parser_pragma (parser, pragma_external);
7180           continue;
7181         }
7182
7183       /* Parse the declaration itself.  */
7184       cp_parser_declaration (parser);
7185     }
7186 }
7187
7188 /* Parse a declaration.
7189
7190    declaration:
7191      block-declaration
7192      function-definition
7193      template-declaration
7194      explicit-instantiation
7195      explicit-specialization
7196      linkage-specification
7197      namespace-definition
7198
7199    GNU extension:
7200
7201    declaration:
7202       __extension__ declaration */
7203
7204 static void
7205 cp_parser_declaration (cp_parser* parser)
7206 {
7207   cp_token token1;
7208   cp_token token2;
7209   int saved_pedantic;
7210   void *p;
7211
7212   /* Check for the `__extension__' keyword.  */
7213   if (cp_parser_extension_opt (parser, &saved_pedantic))
7214     {
7215       /* Parse the qualified declaration.  */
7216       cp_parser_declaration (parser);
7217       /* Restore the PEDANTIC flag.  */
7218       pedantic = saved_pedantic;
7219
7220       return;
7221     }
7222
7223   /* Try to figure out what kind of declaration is present.  */
7224   token1 = *cp_lexer_peek_token (parser->lexer);
7225
7226   if (token1.type != CPP_EOF)
7227     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7228   else
7229     {
7230       token2.type = CPP_EOF;
7231       token2.keyword = RID_MAX;
7232     }
7233
7234   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7235   p = obstack_alloc (&declarator_obstack, 0);
7236
7237   /* If the next token is `extern' and the following token is a string
7238      literal, then we have a linkage specification.  */
7239   if (token1.keyword == RID_EXTERN
7240       && cp_parser_is_string_literal (&token2))
7241     cp_parser_linkage_specification (parser);
7242   /* If the next token is `template', then we have either a template
7243      declaration, an explicit instantiation, or an explicit
7244      specialization.  */
7245   else if (token1.keyword == RID_TEMPLATE)
7246     {
7247       /* `template <>' indicates a template specialization.  */
7248       if (token2.type == CPP_LESS
7249           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7250         cp_parser_explicit_specialization (parser);
7251       /* `template <' indicates a template declaration.  */
7252       else if (token2.type == CPP_LESS)
7253         cp_parser_template_declaration (parser, /*member_p=*/false);
7254       /* Anything else must be an explicit instantiation.  */
7255       else
7256         cp_parser_explicit_instantiation (parser);
7257     }
7258   /* If the next token is `export', then we have a template
7259      declaration.  */
7260   else if (token1.keyword == RID_EXPORT)
7261     cp_parser_template_declaration (parser, /*member_p=*/false);
7262   /* If the next token is `extern', 'static' or 'inline' and the one
7263      after that is `template', we have a GNU extended explicit
7264      instantiation directive.  */
7265   else if (cp_parser_allow_gnu_extensions_p (parser)
7266            && (token1.keyword == RID_EXTERN
7267                || token1.keyword == RID_STATIC
7268                || token1.keyword == RID_INLINE)
7269            && token2.keyword == RID_TEMPLATE)
7270     cp_parser_explicit_instantiation (parser);
7271   /* If the next token is `namespace', check for a named or unnamed
7272      namespace definition.  */
7273   else if (token1.keyword == RID_NAMESPACE
7274            && (/* A named namespace definition.  */
7275                (token2.type == CPP_NAME
7276                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7277                     != CPP_EQ))
7278                /* An unnamed namespace definition.  */
7279                || token2.type == CPP_OPEN_BRACE
7280                || token2.keyword == RID_ATTRIBUTE))
7281     cp_parser_namespace_definition (parser);
7282   /* Objective-C++ declaration/definition.  */
7283   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7284     cp_parser_objc_declaration (parser);
7285   /* We must have either a block declaration or a function
7286      definition.  */
7287   else
7288     /* Try to parse a block-declaration, or a function-definition.  */
7289     cp_parser_block_declaration (parser, /*statement_p=*/false);
7290
7291   /* Free any declarators allocated.  */
7292   obstack_free (&declarator_obstack, p);
7293 }
7294
7295 /* Parse a block-declaration.
7296
7297    block-declaration:
7298      simple-declaration
7299      asm-definition
7300      namespace-alias-definition
7301      using-declaration
7302      using-directive
7303
7304    GNU Extension:
7305
7306    block-declaration:
7307      __extension__ block-declaration
7308      label-declaration
7309
7310    C++0x Extension:
7311
7312    block-declaration:
7313      static_assert-declaration
7314
7315    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7316    part of a declaration-statement.  */
7317
7318 static void
7319 cp_parser_block_declaration (cp_parser *parser,
7320                              bool      statement_p)
7321 {
7322   cp_token *token1;
7323   int saved_pedantic;
7324
7325   /* Check for the `__extension__' keyword.  */
7326   if (cp_parser_extension_opt (parser, &saved_pedantic))
7327     {
7328       /* Parse the qualified declaration.  */
7329       cp_parser_block_declaration (parser, statement_p);
7330       /* Restore the PEDANTIC flag.  */
7331       pedantic = saved_pedantic;
7332
7333       return;
7334     }
7335
7336   /* Peek at the next token to figure out which kind of declaration is
7337      present.  */
7338   token1 = cp_lexer_peek_token (parser->lexer);
7339
7340   /* If the next keyword is `asm', we have an asm-definition.  */
7341   if (token1->keyword == RID_ASM)
7342     {
7343       if (statement_p)
7344         cp_parser_commit_to_tentative_parse (parser);
7345       cp_parser_asm_definition (parser);
7346     }
7347   /* If the next keyword is `namespace', we have a
7348      namespace-alias-definition.  */
7349   else if (token1->keyword == RID_NAMESPACE)
7350     cp_parser_namespace_alias_definition (parser);
7351   /* If the next keyword is `using', we have either a
7352      using-declaration or a using-directive.  */
7353   else if (token1->keyword == RID_USING)
7354     {
7355       cp_token *token2;
7356
7357       if (statement_p)
7358         cp_parser_commit_to_tentative_parse (parser);
7359       /* If the token after `using' is `namespace', then we have a
7360          using-directive.  */
7361       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7362       if (token2->keyword == RID_NAMESPACE)
7363         cp_parser_using_directive (parser);
7364       /* Otherwise, it's a using-declaration.  */
7365       else
7366         cp_parser_using_declaration (parser,
7367                                      /*access_declaration_p=*/false);
7368     }
7369   /* If the next keyword is `__label__' we have a label declaration.  */
7370   else if (token1->keyword == RID_LABEL)
7371     {
7372       if (statement_p)
7373         cp_parser_commit_to_tentative_parse (parser);
7374       cp_parser_label_declaration (parser);
7375     }
7376   /* If the next token is `static_assert' we have a static assertion.  */
7377   else if (token1->keyword == RID_STATIC_ASSERT)
7378     cp_parser_static_assert (parser, /*member_p=*/false);
7379   /* Anything else must be a simple-declaration.  */
7380   else
7381     cp_parser_simple_declaration (parser, !statement_p);
7382 }
7383
7384 /* Parse a simple-declaration.
7385
7386    simple-declaration:
7387      decl-specifier-seq [opt] init-declarator-list [opt] ;
7388
7389    init-declarator-list:
7390      init-declarator
7391      init-declarator-list , init-declarator
7392
7393    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7394    function-definition as a simple-declaration.  */
7395
7396 static void
7397 cp_parser_simple_declaration (cp_parser* parser,
7398                               bool function_definition_allowed_p)
7399 {
7400   cp_decl_specifier_seq decl_specifiers;
7401   int declares_class_or_enum;
7402   bool saw_declarator;
7403
7404   /* Defer access checks until we know what is being declared; the
7405      checks for names appearing in the decl-specifier-seq should be
7406      done as if we were in the scope of the thing being declared.  */
7407   push_deferring_access_checks (dk_deferred);
7408
7409   /* Parse the decl-specifier-seq.  We have to keep track of whether
7410      or not the decl-specifier-seq declares a named class or
7411      enumeration type, since that is the only case in which the
7412      init-declarator-list is allowed to be empty.
7413
7414      [dcl.dcl]
7415
7416      In a simple-declaration, the optional init-declarator-list can be
7417      omitted only when declaring a class or enumeration, that is when
7418      the decl-specifier-seq contains either a class-specifier, an
7419      elaborated-type-specifier, or an enum-specifier.  */
7420   cp_parser_decl_specifier_seq (parser,
7421                                 CP_PARSER_FLAGS_OPTIONAL,
7422                                 &decl_specifiers,
7423                                 &declares_class_or_enum);
7424   /* We no longer need to defer access checks.  */
7425   stop_deferring_access_checks ();
7426
7427   /* In a block scope, a valid declaration must always have a
7428      decl-specifier-seq.  By not trying to parse declarators, we can
7429      resolve the declaration/expression ambiguity more quickly.  */
7430   if (!function_definition_allowed_p
7431       && !decl_specifiers.any_specifiers_p)
7432     {
7433       cp_parser_error (parser, "expected declaration");
7434       goto done;
7435     }
7436
7437   /* If the next two tokens are both identifiers, the code is
7438      erroneous. The usual cause of this situation is code like:
7439
7440        T t;
7441
7442      where "T" should name a type -- but does not.  */
7443   if (!decl_specifiers.type
7444       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7445     {
7446       /* If parsing tentatively, we should commit; we really are
7447          looking at a declaration.  */
7448       cp_parser_commit_to_tentative_parse (parser);
7449       /* Give up.  */
7450       goto done;
7451     }
7452
7453   /* If we have seen at least one decl-specifier, and the next token
7454      is not a parenthesis, then we must be looking at a declaration.
7455      (After "int (" we might be looking at a functional cast.)  */
7456   if (decl_specifiers.any_specifiers_p
7457       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7458     cp_parser_commit_to_tentative_parse (parser);
7459
7460   /* Keep going until we hit the `;' at the end of the simple
7461      declaration.  */
7462   saw_declarator = false;
7463   while (cp_lexer_next_token_is_not (parser->lexer,
7464                                      CPP_SEMICOLON))
7465     {
7466       cp_token *token;
7467       bool function_definition_p;
7468       tree decl;
7469
7470       if (saw_declarator)
7471         {
7472           /* If we are processing next declarator, coma is expected */
7473           token = cp_lexer_peek_token (parser->lexer);
7474           gcc_assert (token->type == CPP_COMMA);
7475           cp_lexer_consume_token (parser->lexer);
7476         }
7477       else
7478         saw_declarator = true;
7479
7480       /* Parse the init-declarator.  */
7481       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7482                                         /*checks=*/NULL,
7483                                         function_definition_allowed_p,
7484                                         /*member_p=*/false,
7485                                         declares_class_or_enum,
7486                                         &function_definition_p);
7487       /* If an error occurred while parsing tentatively, exit quickly.
7488          (That usually happens when in the body of a function; each
7489          statement is treated as a declaration-statement until proven
7490          otherwise.)  */
7491       if (cp_parser_error_occurred (parser))
7492         goto done;
7493       /* Handle function definitions specially.  */
7494       if (function_definition_p)
7495         {
7496           /* If the next token is a `,', then we are probably
7497              processing something like:
7498
7499                void f() {}, *p;
7500
7501              which is erroneous.  */
7502           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7503             error ("mixing declarations and function-definitions is forbidden");
7504           /* Otherwise, we're done with the list of declarators.  */
7505           else
7506             {
7507               pop_deferring_access_checks ();
7508               return;
7509             }
7510         }
7511       /* The next token should be either a `,' or a `;'.  */
7512       token = cp_lexer_peek_token (parser->lexer);
7513       /* If it's a `,', there are more declarators to come.  */
7514       if (token->type == CPP_COMMA)
7515         /* will be consumed next time around */;
7516       /* If it's a `;', we are done.  */
7517       else if (token->type == CPP_SEMICOLON)
7518         break;
7519       /* Anything else is an error.  */
7520       else
7521         {
7522           /* If we have already issued an error message we don't need
7523              to issue another one.  */
7524           if (decl != error_mark_node
7525               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7526             cp_parser_error (parser, "expected %<,%> or %<;%>");
7527           /* Skip tokens until we reach the end of the statement.  */
7528           cp_parser_skip_to_end_of_statement (parser);
7529           /* If the next token is now a `;', consume it.  */
7530           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7531             cp_lexer_consume_token (parser->lexer);
7532           goto done;
7533         }
7534       /* After the first time around, a function-definition is not
7535          allowed -- even if it was OK at first.  For example:
7536
7537            int i, f() {}
7538
7539          is not valid.  */
7540       function_definition_allowed_p = false;
7541     }
7542
7543   /* Issue an error message if no declarators are present, and the
7544      decl-specifier-seq does not itself declare a class or
7545      enumeration.  */
7546   if (!saw_declarator)
7547     {
7548       if (cp_parser_declares_only_class_p (parser))
7549         shadow_tag (&decl_specifiers);
7550       /* Perform any deferred access checks.  */
7551       perform_deferred_access_checks ();
7552     }
7553
7554   /* Consume the `;'.  */
7555   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7556
7557  done:
7558   pop_deferring_access_checks ();
7559 }
7560
7561 /* Parse a decl-specifier-seq.
7562
7563    decl-specifier-seq:
7564      decl-specifier-seq [opt] decl-specifier
7565
7566    decl-specifier:
7567      storage-class-specifier
7568      type-specifier
7569      function-specifier
7570      friend
7571      typedef
7572
7573    GNU Extension:
7574
7575    decl-specifier:
7576      attributes
7577
7578    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7579
7580    The parser flags FLAGS is used to control type-specifier parsing.
7581
7582    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7583    flags:
7584
7585      1: one of the decl-specifiers is an elaborated-type-specifier
7586         (i.e., a type declaration)
7587      2: one of the decl-specifiers is an enum-specifier or a
7588         class-specifier (i.e., a type definition)
7589
7590    */
7591
7592 static void
7593 cp_parser_decl_specifier_seq (cp_parser* parser,
7594                               cp_parser_flags flags,
7595                               cp_decl_specifier_seq *decl_specs,
7596                               int* declares_class_or_enum)
7597 {
7598   bool constructor_possible_p = !parser->in_declarator_p;
7599
7600   /* Clear DECL_SPECS.  */
7601   clear_decl_specs (decl_specs);
7602
7603   /* Assume no class or enumeration type is declared.  */
7604   *declares_class_or_enum = 0;
7605
7606   /* Keep reading specifiers until there are no more to read.  */
7607   while (true)
7608     {
7609       bool constructor_p;
7610       bool found_decl_spec;
7611       cp_token *token;
7612
7613       /* Peek at the next token.  */
7614       token = cp_lexer_peek_token (parser->lexer);
7615       /* Handle attributes.  */
7616       if (token->keyword == RID_ATTRIBUTE)
7617         {
7618           /* Parse the attributes.  */
7619           decl_specs->attributes
7620             = chainon (decl_specs->attributes,
7621                        cp_parser_attributes_opt (parser));
7622           continue;
7623         }
7624       /* Assume we will find a decl-specifier keyword.  */
7625       found_decl_spec = true;
7626       /* If the next token is an appropriate keyword, we can simply
7627          add it to the list.  */
7628       switch (token->keyword)
7629         {
7630           /* decl-specifier:
7631                friend  */
7632         case RID_FRIEND:
7633           if (!at_class_scope_p ())
7634             {
7635               error ("%<friend%> used outside of class");
7636               cp_lexer_purge_token (parser->lexer);
7637             }
7638           else
7639             {
7640               ++decl_specs->specs[(int) ds_friend];
7641               /* Consume the token.  */
7642               cp_lexer_consume_token (parser->lexer);
7643             }
7644           break;
7645
7646           /* function-specifier:
7647                inline
7648                virtual
7649                explicit  */
7650         case RID_INLINE:
7651         case RID_VIRTUAL:
7652         case RID_EXPLICIT:
7653           cp_parser_function_specifier_opt (parser, decl_specs);
7654           break;
7655
7656           /* decl-specifier:
7657                typedef  */
7658         case RID_TYPEDEF:
7659           ++decl_specs->specs[(int) ds_typedef];
7660           /* Consume the token.  */
7661           cp_lexer_consume_token (parser->lexer);
7662           /* A constructor declarator cannot appear in a typedef.  */
7663           constructor_possible_p = false;
7664           /* The "typedef" keyword can only occur in a declaration; we
7665              may as well commit at this point.  */
7666           cp_parser_commit_to_tentative_parse (parser);
7667
7668           if (decl_specs->storage_class != sc_none)
7669             decl_specs->conflicting_specifiers_p = true;
7670           break;
7671
7672           /* storage-class-specifier:
7673                auto
7674                register
7675                static
7676                extern
7677                mutable
7678
7679              GNU Extension:
7680                thread  */
7681         case RID_AUTO:
7682         case RID_REGISTER:
7683         case RID_STATIC:
7684         case RID_EXTERN:
7685         case RID_MUTABLE:
7686           /* Consume the token.  */
7687           cp_lexer_consume_token (parser->lexer);
7688           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7689           break;
7690         case RID_THREAD:
7691           /* Consume the token.  */
7692           cp_lexer_consume_token (parser->lexer);
7693           ++decl_specs->specs[(int) ds_thread];
7694           break;
7695
7696         default:
7697           /* We did not yet find a decl-specifier yet.  */
7698           found_decl_spec = false;
7699           break;
7700         }
7701
7702       /* Constructors are a special case.  The `S' in `S()' is not a
7703          decl-specifier; it is the beginning of the declarator.  */
7704       constructor_p
7705         = (!found_decl_spec
7706            && constructor_possible_p
7707            && (cp_parser_constructor_declarator_p
7708                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7709
7710       /* If we don't have a DECL_SPEC yet, then we must be looking at
7711          a type-specifier.  */
7712       if (!found_decl_spec && !constructor_p)
7713         {
7714           int decl_spec_declares_class_or_enum;
7715           bool is_cv_qualifier;
7716           tree type_spec;
7717
7718           type_spec
7719             = cp_parser_type_specifier (parser, flags,
7720                                         decl_specs,
7721                                         /*is_declaration=*/true,
7722                                         &decl_spec_declares_class_or_enum,
7723                                         &is_cv_qualifier);
7724
7725           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7726
7727           /* If this type-specifier referenced a user-defined type
7728              (a typedef, class-name, etc.), then we can't allow any
7729              more such type-specifiers henceforth.
7730
7731              [dcl.spec]
7732
7733              The longest sequence of decl-specifiers that could
7734              possibly be a type name is taken as the
7735              decl-specifier-seq of a declaration.  The sequence shall
7736              be self-consistent as described below.
7737
7738              [dcl.type]
7739
7740              As a general rule, at most one type-specifier is allowed
7741              in the complete decl-specifier-seq of a declaration.  The
7742              only exceptions are the following:
7743
7744              -- const or volatile can be combined with any other
7745                 type-specifier.
7746
7747              -- signed or unsigned can be combined with char, long,
7748                 short, or int.
7749
7750              -- ..
7751
7752              Example:
7753
7754                typedef char* Pc;
7755                void g (const int Pc);
7756
7757              Here, Pc is *not* part of the decl-specifier seq; it's
7758              the declarator.  Therefore, once we see a type-specifier
7759              (other than a cv-qualifier), we forbid any additional
7760              user-defined types.  We *do* still allow things like `int
7761              int' to be considered a decl-specifier-seq, and issue the
7762              error message later.  */
7763           if (type_spec && !is_cv_qualifier)
7764             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7765           /* A constructor declarator cannot follow a type-specifier.  */
7766           if (type_spec)
7767             {
7768               constructor_possible_p = false;
7769               found_decl_spec = true;
7770             }
7771         }
7772
7773       /* If we still do not have a DECL_SPEC, then there are no more
7774          decl-specifiers.  */
7775       if (!found_decl_spec)
7776         break;
7777
7778       decl_specs->any_specifiers_p = true;
7779       /* After we see one decl-specifier, further decl-specifiers are
7780          always optional.  */
7781       flags |= CP_PARSER_FLAGS_OPTIONAL;
7782     }
7783
7784   cp_parser_check_decl_spec (decl_specs);
7785
7786   /* Don't allow a friend specifier with a class definition.  */
7787   if (decl_specs->specs[(int) ds_friend] != 0
7788       && (*declares_class_or_enum & 2))
7789     error ("class definition may not be declared a friend");
7790 }
7791
7792 /* Parse an (optional) storage-class-specifier.
7793
7794    storage-class-specifier:
7795      auto
7796      register
7797      static
7798      extern
7799      mutable
7800
7801    GNU Extension:
7802
7803    storage-class-specifier:
7804      thread
7805
7806    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7807
7808 static tree
7809 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7810 {
7811   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7812     {
7813     case RID_AUTO:
7814     case RID_REGISTER:
7815     case RID_STATIC:
7816     case RID_EXTERN:
7817     case RID_MUTABLE:
7818     case RID_THREAD:
7819       /* Consume the token.  */
7820       return cp_lexer_consume_token (parser->lexer)->u.value;
7821
7822     default:
7823       return NULL_TREE;
7824     }
7825 }
7826
7827 /* Parse an (optional) function-specifier.
7828
7829    function-specifier:
7830      inline
7831      virtual
7832      explicit
7833
7834    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7835    Updates DECL_SPECS, if it is non-NULL.  */
7836
7837 static tree
7838 cp_parser_function_specifier_opt (cp_parser* parser,
7839                                   cp_decl_specifier_seq *decl_specs)
7840 {
7841   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7842     {
7843     case RID_INLINE:
7844       if (decl_specs)
7845         ++decl_specs->specs[(int) ds_inline];
7846       break;
7847
7848     case RID_VIRTUAL:
7849       /* 14.5.2.3 [temp.mem]
7850
7851          A member function template shall not be virtual.  */
7852       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7853         error ("templates may not be %<virtual%>");
7854       else if (decl_specs)
7855         ++decl_specs->specs[(int) ds_virtual];
7856       break;
7857
7858     case RID_EXPLICIT:
7859       if (decl_specs)
7860         ++decl_specs->specs[(int) ds_explicit];
7861       break;
7862
7863     default:
7864       return NULL_TREE;
7865     }
7866
7867   /* Consume the token.  */
7868   return cp_lexer_consume_token (parser->lexer)->u.value;
7869 }
7870
7871 /* Parse a linkage-specification.
7872
7873    linkage-specification:
7874      extern string-literal { declaration-seq [opt] }
7875      extern string-literal declaration  */
7876
7877 static void
7878 cp_parser_linkage_specification (cp_parser* parser)
7879 {
7880   tree linkage;
7881
7882   /* Look for the `extern' keyword.  */
7883   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7884
7885   /* Look for the string-literal.  */
7886   linkage = cp_parser_string_literal (parser, false, false);
7887
7888   /* Transform the literal into an identifier.  If the literal is a
7889      wide-character string, or contains embedded NULs, then we can't
7890      handle it as the user wants.  */
7891   if (strlen (TREE_STRING_POINTER (linkage))
7892       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7893     {
7894       cp_parser_error (parser, "invalid linkage-specification");
7895       /* Assume C++ linkage.  */
7896       linkage = lang_name_cplusplus;
7897     }
7898   else
7899     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7900
7901   /* We're now using the new linkage.  */
7902   push_lang_context (linkage);
7903
7904   /* If the next token is a `{', then we're using the first
7905      production.  */
7906   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7907     {
7908       /* Consume the `{' token.  */
7909       cp_lexer_consume_token (parser->lexer);
7910       /* Parse the declarations.  */
7911       cp_parser_declaration_seq_opt (parser);
7912       /* Look for the closing `}'.  */
7913       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7914     }
7915   /* Otherwise, there's just one declaration.  */
7916   else
7917     {
7918       bool saved_in_unbraced_linkage_specification_p;
7919
7920       saved_in_unbraced_linkage_specification_p
7921         = parser->in_unbraced_linkage_specification_p;
7922       parser->in_unbraced_linkage_specification_p = true;
7923       cp_parser_declaration (parser);
7924       parser->in_unbraced_linkage_specification_p
7925         = saved_in_unbraced_linkage_specification_p;
7926     }
7927
7928   /* We're done with the linkage-specification.  */
7929   pop_lang_context ();
7930 }
7931
7932 /* Parse a static_assert-declaration.
7933
7934    static_assert-declaration:
7935      static_assert ( constant-expression , string-literal ) ; 
7936
7937    If MEMBER_P, this static_assert is a class member.  */
7938
7939 static void 
7940 cp_parser_static_assert(cp_parser *parser, bool member_p)
7941 {
7942   tree condition;
7943   tree message;
7944   cp_token *token;
7945   location_t saved_loc;
7946
7947   /* Peek at the `static_assert' token so we can keep track of exactly
7948      where the static assertion started.  */
7949   token = cp_lexer_peek_token (parser->lexer);
7950   saved_loc = token->location;
7951
7952   /* Look for the `static_assert' keyword.  */
7953   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
7954                                   "`static_assert'"))
7955     return;
7956
7957   /*  We know we are in a static assertion; commit to any tentative
7958       parse.  */
7959   if (cp_parser_parsing_tentatively (parser))
7960     cp_parser_commit_to_tentative_parse (parser);
7961
7962   /* Parse the `(' starting the static assertion condition.  */
7963   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7964
7965   /* Parse the constant-expression.  */
7966   condition = 
7967     cp_parser_constant_expression (parser,
7968                                    /*allow_non_constant_p=*/false,
7969                                    /*non_constant_p=*/NULL);
7970
7971   /* Parse the separating `,'.  */
7972   cp_parser_require (parser, CPP_COMMA, "`,'");
7973
7974   /* Parse the string-literal message.  */
7975   message = cp_parser_string_literal (parser, 
7976                                       /*translate=*/false,
7977                                       /*wide_ok=*/true);
7978
7979   /* A `)' completes the static assertion.  */
7980   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7981     cp_parser_skip_to_closing_parenthesis (parser, 
7982                                            /*recovering=*/true, 
7983                                            /*or_comma=*/false,
7984                                            /*consume_paren=*/true);
7985
7986   /* A semicolon terminates the declaration.  */
7987   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7988
7989   /* Complete the static assertion, which may mean either processing 
7990      the static assert now or saving it for template instantiation.  */
7991   finish_static_assert (condition, message, saved_loc, member_p);
7992 }
7993
7994 /* Special member functions [gram.special] */
7995
7996 /* Parse a conversion-function-id.
7997
7998    conversion-function-id:
7999      operator conversion-type-id
8000
8001    Returns an IDENTIFIER_NODE representing the operator.  */
8002
8003 static tree
8004 cp_parser_conversion_function_id (cp_parser* parser)
8005 {
8006   tree type;
8007   tree saved_scope;
8008   tree saved_qualifying_scope;
8009   tree saved_object_scope;
8010   tree pushed_scope = NULL_TREE;
8011
8012   /* Look for the `operator' token.  */
8013   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8014     return error_mark_node;
8015   /* When we parse the conversion-type-id, the current scope will be
8016      reset.  However, we need that information in able to look up the
8017      conversion function later, so we save it here.  */
8018   saved_scope = parser->scope;
8019   saved_qualifying_scope = parser->qualifying_scope;
8020   saved_object_scope = parser->object_scope;
8021   /* We must enter the scope of the class so that the names of
8022      entities declared within the class are available in the
8023      conversion-type-id.  For example, consider:
8024
8025        struct S {
8026          typedef int I;
8027          operator I();
8028        };
8029
8030        S::operator I() { ... }
8031
8032      In order to see that `I' is a type-name in the definition, we
8033      must be in the scope of `S'.  */
8034   if (saved_scope)
8035     pushed_scope = push_scope (saved_scope);
8036   /* Parse the conversion-type-id.  */
8037   type = cp_parser_conversion_type_id (parser);
8038   /* Leave the scope of the class, if any.  */
8039   if (pushed_scope)
8040     pop_scope (pushed_scope);
8041   /* Restore the saved scope.  */
8042   parser->scope = saved_scope;
8043   parser->qualifying_scope = saved_qualifying_scope;
8044   parser->object_scope = saved_object_scope;
8045   /* If the TYPE is invalid, indicate failure.  */
8046   if (type == error_mark_node)
8047     return error_mark_node;
8048   return mangle_conv_op_name_for_type (type);
8049 }
8050
8051 /* Parse a conversion-type-id:
8052
8053    conversion-type-id:
8054      type-specifier-seq conversion-declarator [opt]
8055
8056    Returns the TYPE specified.  */
8057
8058 static tree
8059 cp_parser_conversion_type_id (cp_parser* parser)
8060 {
8061   tree attributes;
8062   cp_decl_specifier_seq type_specifiers;
8063   cp_declarator *declarator;
8064   tree type_specified;
8065
8066   /* Parse the attributes.  */
8067   attributes = cp_parser_attributes_opt (parser);
8068   /* Parse the type-specifiers.  */
8069   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8070                                 &type_specifiers);
8071   /* If that didn't work, stop.  */
8072   if (type_specifiers.type == error_mark_node)
8073     return error_mark_node;
8074   /* Parse the conversion-declarator.  */
8075   declarator = cp_parser_conversion_declarator_opt (parser);
8076
8077   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8078                                     /*initialized=*/0, &attributes);
8079   if (attributes)
8080     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8081   return type_specified;
8082 }
8083
8084 /* Parse an (optional) conversion-declarator.
8085
8086    conversion-declarator:
8087      ptr-operator conversion-declarator [opt]
8088
8089    */
8090
8091 static cp_declarator *
8092 cp_parser_conversion_declarator_opt (cp_parser* parser)
8093 {
8094   enum tree_code code;
8095   tree class_type;
8096   cp_cv_quals cv_quals;
8097
8098   /* We don't know if there's a ptr-operator next, or not.  */
8099   cp_parser_parse_tentatively (parser);
8100   /* Try the ptr-operator.  */
8101   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8102   /* If it worked, look for more conversion-declarators.  */
8103   if (cp_parser_parse_definitely (parser))
8104     {
8105       cp_declarator *declarator;
8106
8107       /* Parse another optional declarator.  */
8108       declarator = cp_parser_conversion_declarator_opt (parser);
8109
8110       /* Create the representation of the declarator.  */
8111       if (class_type)
8112         declarator = make_ptrmem_declarator (cv_quals, class_type,
8113                                              declarator);
8114       else if (code == INDIRECT_REF)
8115         declarator = make_pointer_declarator (cv_quals, declarator);
8116       else
8117         declarator = make_reference_declarator (cv_quals, declarator);
8118
8119       return declarator;
8120    }
8121
8122   return NULL;
8123 }
8124
8125 /* Parse an (optional) ctor-initializer.
8126
8127    ctor-initializer:
8128      : mem-initializer-list
8129
8130    Returns TRUE iff the ctor-initializer was actually present.  */
8131
8132 static bool
8133 cp_parser_ctor_initializer_opt (cp_parser* parser)
8134 {
8135   /* If the next token is not a `:', then there is no
8136      ctor-initializer.  */
8137   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8138     {
8139       /* Do default initialization of any bases and members.  */
8140       if (DECL_CONSTRUCTOR_P (current_function_decl))
8141         finish_mem_initializers (NULL_TREE);
8142
8143       return false;
8144     }
8145
8146   /* Consume the `:' token.  */
8147   cp_lexer_consume_token (parser->lexer);
8148   /* And the mem-initializer-list.  */
8149   cp_parser_mem_initializer_list (parser);
8150
8151   return true;
8152 }
8153
8154 /* Parse a mem-initializer-list.
8155
8156    mem-initializer-list:
8157      mem-initializer
8158      mem-initializer , mem-initializer-list  */
8159
8160 static void
8161 cp_parser_mem_initializer_list (cp_parser* parser)
8162 {
8163   tree mem_initializer_list = NULL_TREE;
8164
8165   /* Let the semantic analysis code know that we are starting the
8166      mem-initializer-list.  */
8167   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8168     error ("only constructors take base initializers");
8169
8170   /* Loop through the list.  */
8171   while (true)
8172     {
8173       tree mem_initializer;
8174
8175       /* Parse the mem-initializer.  */
8176       mem_initializer = cp_parser_mem_initializer (parser);
8177       /* Add it to the list, unless it was erroneous.  */
8178       if (mem_initializer != error_mark_node)
8179         {
8180           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8181           mem_initializer_list = mem_initializer;
8182         }
8183       /* If the next token is not a `,', we're done.  */
8184       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8185         break;
8186       /* Consume the `,' token.  */
8187       cp_lexer_consume_token (parser->lexer);
8188     }
8189
8190   /* Perform semantic analysis.  */
8191   if (DECL_CONSTRUCTOR_P (current_function_decl))
8192     finish_mem_initializers (mem_initializer_list);
8193 }
8194
8195 /* Parse a mem-initializer.
8196
8197    mem-initializer:
8198      mem-initializer-id ( expression-list [opt] )
8199
8200    GNU extension:
8201
8202    mem-initializer:
8203      ( expression-list [opt] )
8204
8205    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8206    class) or FIELD_DECL (for a non-static data member) to initialize;
8207    the TREE_VALUE is the expression-list.  An empty initialization
8208    list is represented by void_list_node.  */
8209
8210 static tree
8211 cp_parser_mem_initializer (cp_parser* parser)
8212 {
8213   tree mem_initializer_id;
8214   tree expression_list;
8215   tree member;
8216
8217   /* Find out what is being initialized.  */
8218   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8219     {
8220       pedwarn ("anachronistic old-style base class initializer");
8221       mem_initializer_id = NULL_TREE;
8222     }
8223   else
8224     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8225   member = expand_member_init (mem_initializer_id);
8226   if (member && !DECL_P (member))
8227     in_base_initializer = 1;
8228
8229   expression_list
8230     = cp_parser_parenthesized_expression_list (parser, false,
8231                                                /*cast_p=*/false,
8232                                                /*non_constant_p=*/NULL);
8233   if (expression_list == error_mark_node)
8234     return error_mark_node;
8235   if (!expression_list)
8236     expression_list = void_type_node;
8237
8238   in_base_initializer = 0;
8239
8240   return member ? build_tree_list (member, expression_list) : error_mark_node;
8241 }
8242
8243 /* Parse a mem-initializer-id.
8244
8245    mem-initializer-id:
8246      :: [opt] nested-name-specifier [opt] class-name
8247      identifier
8248
8249    Returns a TYPE indicating the class to be initializer for the first
8250    production.  Returns an IDENTIFIER_NODE indicating the data member
8251    to be initialized for the second production.  */
8252
8253 static tree
8254 cp_parser_mem_initializer_id (cp_parser* parser)
8255 {
8256   bool global_scope_p;
8257   bool nested_name_specifier_p;
8258   bool template_p = false;
8259   tree id;
8260
8261   /* `typename' is not allowed in this context ([temp.res]).  */
8262   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8263     {
8264       error ("keyword %<typename%> not allowed in this context (a qualified "
8265              "member initializer is implicitly a type)");
8266       cp_lexer_consume_token (parser->lexer);
8267     }
8268   /* Look for the optional `::' operator.  */
8269   global_scope_p
8270     = (cp_parser_global_scope_opt (parser,
8271                                    /*current_scope_valid_p=*/false)
8272        != NULL_TREE);
8273   /* Look for the optional nested-name-specifier.  The simplest way to
8274      implement:
8275
8276        [temp.res]
8277
8278        The keyword `typename' is not permitted in a base-specifier or
8279        mem-initializer; in these contexts a qualified name that
8280        depends on a template-parameter is implicitly assumed to be a
8281        type name.
8282
8283      is to assume that we have seen the `typename' keyword at this
8284      point.  */
8285   nested_name_specifier_p
8286     = (cp_parser_nested_name_specifier_opt (parser,
8287                                             /*typename_keyword_p=*/true,
8288                                             /*check_dependency_p=*/true,
8289                                             /*type_p=*/true,
8290                                             /*is_declaration=*/true)
8291        != NULL_TREE);
8292   if (nested_name_specifier_p)
8293     template_p = cp_parser_optional_template_keyword (parser);
8294   /* If there is a `::' operator or a nested-name-specifier, then we
8295      are definitely looking for a class-name.  */
8296   if (global_scope_p || nested_name_specifier_p)
8297     return cp_parser_class_name (parser,
8298                                  /*typename_keyword_p=*/true,
8299                                  /*template_keyword_p=*/template_p,
8300                                  none_type,
8301                                  /*check_dependency_p=*/true,
8302                                  /*class_head_p=*/false,
8303                                  /*is_declaration=*/true);
8304   /* Otherwise, we could also be looking for an ordinary identifier.  */
8305   cp_parser_parse_tentatively (parser);
8306   /* Try a class-name.  */
8307   id = cp_parser_class_name (parser,
8308                              /*typename_keyword_p=*/true,
8309                              /*template_keyword_p=*/false,
8310                              none_type,
8311                              /*check_dependency_p=*/true,
8312                              /*class_head_p=*/false,
8313                              /*is_declaration=*/true);
8314   /* If we found one, we're done.  */
8315   if (cp_parser_parse_definitely (parser))
8316     return id;
8317   /* Otherwise, look for an ordinary identifier.  */
8318   return cp_parser_identifier (parser);
8319 }
8320
8321 /* Overloading [gram.over] */
8322
8323 /* Parse an operator-function-id.
8324
8325    operator-function-id:
8326      operator operator
8327
8328    Returns an IDENTIFIER_NODE for the operator which is a
8329    human-readable spelling of the identifier, e.g., `operator +'.  */
8330
8331 static tree
8332 cp_parser_operator_function_id (cp_parser* parser)
8333 {
8334   /* Look for the `operator' keyword.  */
8335   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8336     return error_mark_node;
8337   /* And then the name of the operator itself.  */
8338   return cp_parser_operator (parser);
8339 }
8340
8341 /* Parse an operator.
8342
8343    operator:
8344      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8345      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8346      || ++ -- , ->* -> () []
8347
8348    GNU Extensions:
8349
8350    operator:
8351      <? >? <?= >?=
8352
8353    Returns an IDENTIFIER_NODE for the operator which is a
8354    human-readable spelling of the identifier, e.g., `operator +'.  */
8355
8356 static tree
8357 cp_parser_operator (cp_parser* parser)
8358 {
8359   tree id = NULL_TREE;
8360   cp_token *token;
8361
8362   /* Peek at the next token.  */
8363   token = cp_lexer_peek_token (parser->lexer);
8364   /* Figure out which operator we have.  */
8365   switch (token->type)
8366     {
8367     case CPP_KEYWORD:
8368       {
8369         enum tree_code op;
8370
8371         /* The keyword should be either `new' or `delete'.  */
8372         if (token->keyword == RID_NEW)
8373           op = NEW_EXPR;
8374         else if (token->keyword == RID_DELETE)
8375           op = DELETE_EXPR;
8376         else
8377           break;
8378
8379         /* Consume the `new' or `delete' token.  */
8380         cp_lexer_consume_token (parser->lexer);
8381
8382         /* Peek at the next token.  */
8383         token = cp_lexer_peek_token (parser->lexer);
8384         /* If it's a `[' token then this is the array variant of the
8385            operator.  */
8386         if (token->type == CPP_OPEN_SQUARE)
8387           {
8388             /* Consume the `[' token.  */
8389             cp_lexer_consume_token (parser->lexer);
8390             /* Look for the `]' token.  */
8391             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8392             id = ansi_opname (op == NEW_EXPR
8393                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8394           }
8395         /* Otherwise, we have the non-array variant.  */
8396         else
8397           id = ansi_opname (op);
8398
8399         return id;
8400       }
8401
8402     case CPP_PLUS:
8403       id = ansi_opname (PLUS_EXPR);
8404       break;
8405
8406     case CPP_MINUS:
8407       id = ansi_opname (MINUS_EXPR);
8408       break;
8409
8410     case CPP_MULT:
8411       id = ansi_opname (MULT_EXPR);
8412       break;
8413
8414     case CPP_DIV:
8415       id = ansi_opname (TRUNC_DIV_EXPR);
8416       break;
8417
8418     case CPP_MOD:
8419       id = ansi_opname (TRUNC_MOD_EXPR);
8420       break;
8421
8422     case CPP_XOR:
8423       id = ansi_opname (BIT_XOR_EXPR);
8424       break;
8425
8426     case CPP_AND:
8427       id = ansi_opname (BIT_AND_EXPR);
8428       break;
8429
8430     case CPP_OR:
8431       id = ansi_opname (BIT_IOR_EXPR);
8432       break;
8433
8434     case CPP_COMPL:
8435       id = ansi_opname (BIT_NOT_EXPR);
8436       break;
8437
8438     case CPP_NOT:
8439       id = ansi_opname (TRUTH_NOT_EXPR);
8440       break;
8441
8442     case CPP_EQ:
8443       id = ansi_assopname (NOP_EXPR);
8444       break;
8445
8446     case CPP_LESS:
8447       id = ansi_opname (LT_EXPR);
8448       break;
8449
8450     case CPP_GREATER:
8451       id = ansi_opname (GT_EXPR);
8452       break;
8453
8454     case CPP_PLUS_EQ:
8455       id = ansi_assopname (PLUS_EXPR);
8456       break;
8457
8458     case CPP_MINUS_EQ:
8459       id = ansi_assopname (MINUS_EXPR);
8460       break;
8461
8462     case CPP_MULT_EQ:
8463       id = ansi_assopname (MULT_EXPR);
8464       break;
8465
8466     case CPP_DIV_EQ:
8467       id = ansi_assopname (TRUNC_DIV_EXPR);
8468       break;
8469
8470     case CPP_MOD_EQ:
8471       id = ansi_assopname (TRUNC_MOD_EXPR);
8472       break;
8473
8474     case CPP_XOR_EQ:
8475       id = ansi_assopname (BIT_XOR_EXPR);
8476       break;
8477
8478     case CPP_AND_EQ:
8479       id = ansi_assopname (BIT_AND_EXPR);
8480       break;
8481
8482     case CPP_OR_EQ:
8483       id = ansi_assopname (BIT_IOR_EXPR);
8484       break;
8485
8486     case CPP_LSHIFT:
8487       id = ansi_opname (LSHIFT_EXPR);
8488       break;
8489
8490     case CPP_RSHIFT:
8491       id = ansi_opname (RSHIFT_EXPR);
8492       break;
8493
8494     case CPP_LSHIFT_EQ:
8495       id = ansi_assopname (LSHIFT_EXPR);
8496       break;
8497
8498     case CPP_RSHIFT_EQ:
8499       id = ansi_assopname (RSHIFT_EXPR);
8500       break;
8501
8502     case CPP_EQ_EQ:
8503       id = ansi_opname (EQ_EXPR);
8504       break;
8505
8506     case CPP_NOT_EQ:
8507       id = ansi_opname (NE_EXPR);
8508       break;
8509
8510     case CPP_LESS_EQ:
8511       id = ansi_opname (LE_EXPR);
8512       break;
8513
8514     case CPP_GREATER_EQ:
8515       id = ansi_opname (GE_EXPR);
8516       break;
8517
8518     case CPP_AND_AND:
8519       id = ansi_opname (TRUTH_ANDIF_EXPR);
8520       break;
8521
8522     case CPP_OR_OR:
8523       id = ansi_opname (TRUTH_ORIF_EXPR);
8524       break;
8525
8526     case CPP_PLUS_PLUS:
8527       id = ansi_opname (POSTINCREMENT_EXPR);
8528       break;
8529
8530     case CPP_MINUS_MINUS:
8531       id = ansi_opname (PREDECREMENT_EXPR);
8532       break;
8533
8534     case CPP_COMMA:
8535       id = ansi_opname (COMPOUND_EXPR);
8536       break;
8537
8538     case CPP_DEREF_STAR:
8539       id = ansi_opname (MEMBER_REF);
8540       break;
8541
8542     case CPP_DEREF:
8543       id = ansi_opname (COMPONENT_REF);
8544       break;
8545
8546     case CPP_OPEN_PAREN:
8547       /* Consume the `('.  */
8548       cp_lexer_consume_token (parser->lexer);
8549       /* Look for the matching `)'.  */
8550       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8551       return ansi_opname (CALL_EXPR);
8552
8553     case CPP_OPEN_SQUARE:
8554       /* Consume the `['.  */
8555       cp_lexer_consume_token (parser->lexer);
8556       /* Look for the matching `]'.  */
8557       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8558       return ansi_opname (ARRAY_REF);
8559
8560     default:
8561       /* Anything else is an error.  */
8562       break;
8563     }
8564
8565   /* If we have selected an identifier, we need to consume the
8566      operator token.  */
8567   if (id)
8568     cp_lexer_consume_token (parser->lexer);
8569   /* Otherwise, no valid operator name was present.  */
8570   else
8571     {
8572       cp_parser_error (parser, "expected operator");
8573       id = error_mark_node;
8574     }
8575
8576   return id;
8577 }
8578
8579 /* Parse a template-declaration.
8580
8581    template-declaration:
8582      export [opt] template < template-parameter-list > declaration
8583
8584    If MEMBER_P is TRUE, this template-declaration occurs within a
8585    class-specifier.
8586
8587    The grammar rule given by the standard isn't correct.  What
8588    is really meant is:
8589
8590    template-declaration:
8591      export [opt] template-parameter-list-seq
8592        decl-specifier-seq [opt] init-declarator [opt] ;
8593      export [opt] template-parameter-list-seq
8594        function-definition
8595
8596    template-parameter-list-seq:
8597      template-parameter-list-seq [opt]
8598      template < template-parameter-list >  */
8599
8600 static void
8601 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8602 {
8603   /* Check for `export'.  */
8604   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8605     {
8606       /* Consume the `export' token.  */
8607       cp_lexer_consume_token (parser->lexer);
8608       /* Warn that we do not support `export'.  */
8609       warning (0, "keyword %<export%> not implemented, and will be ignored");
8610     }
8611
8612   cp_parser_template_declaration_after_export (parser, member_p);
8613 }
8614
8615 /* Parse a template-parameter-list.
8616
8617    template-parameter-list:
8618      template-parameter
8619      template-parameter-list , template-parameter
8620
8621    Returns a TREE_LIST.  Each node represents a template parameter.
8622    The nodes are connected via their TREE_CHAINs.  */
8623
8624 static tree
8625 cp_parser_template_parameter_list (cp_parser* parser)
8626 {
8627   tree parameter_list = NULL_TREE;
8628
8629   begin_template_parm_list ();
8630   while (true)
8631     {
8632       tree parameter;
8633       cp_token *token;
8634       bool is_non_type;
8635
8636       /* Parse the template-parameter.  */
8637       parameter = cp_parser_template_parameter (parser, &is_non_type);
8638       /* Add it to the list.  */
8639       if (parameter != error_mark_node)
8640         parameter_list = process_template_parm (parameter_list,
8641                                                 parameter,
8642                                                 is_non_type);
8643       else
8644        {
8645          tree err_parm = build_tree_list (parameter, parameter);
8646          TREE_VALUE (err_parm) = error_mark_node;
8647          parameter_list = chainon (parameter_list, err_parm);
8648        }
8649
8650       /* Peek at the next token.  */
8651       token = cp_lexer_peek_token (parser->lexer);
8652       /* If it's not a `,', we're done.  */
8653       if (token->type != CPP_COMMA)
8654         break;
8655       /* Otherwise, consume the `,' token.  */
8656       cp_lexer_consume_token (parser->lexer);
8657     }
8658
8659   return end_template_parm_list (parameter_list);
8660 }
8661
8662 /* Parse a template-parameter.
8663
8664    template-parameter:
8665      type-parameter
8666      parameter-declaration
8667
8668    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8669    the parameter.  The TREE_PURPOSE is the default value, if any.
8670    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8671    iff this parameter is a non-type parameter.  */
8672
8673 static tree
8674 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8675 {
8676   cp_token *token;
8677   cp_parameter_declarator *parameter_declarator;
8678   tree parm;
8679
8680   /* Assume it is a type parameter or a template parameter.  */
8681   *is_non_type = false;
8682   /* Peek at the next token.  */
8683   token = cp_lexer_peek_token (parser->lexer);
8684   /* If it is `class' or `template', we have a type-parameter.  */
8685   if (token->keyword == RID_TEMPLATE)
8686     return cp_parser_type_parameter (parser);
8687   /* If it is `class' or `typename' we do not know yet whether it is a
8688      type parameter or a non-type parameter.  Consider:
8689
8690        template <typename T, typename T::X X> ...
8691
8692      or:
8693
8694        template <class C, class D*> ...
8695
8696      Here, the first parameter is a type parameter, and the second is
8697      a non-type parameter.  We can tell by looking at the token after
8698      the identifier -- if it is a `,', `=', or `>' then we have a type
8699      parameter.  */
8700   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8701     {
8702       /* Peek at the token after `class' or `typename'.  */
8703       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8704       /* If it's an identifier, skip it.  */
8705       if (token->type == CPP_NAME)
8706         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8707       /* Now, see if the token looks like the end of a template
8708          parameter.  */
8709       if (token->type == CPP_COMMA
8710           || token->type == CPP_EQ
8711           || token->type == CPP_GREATER)
8712         return cp_parser_type_parameter (parser);
8713     }
8714
8715   /* Otherwise, it is a non-type parameter.
8716
8717      [temp.param]
8718
8719      When parsing a default template-argument for a non-type
8720      template-parameter, the first non-nested `>' is taken as the end
8721      of the template parameter-list rather than a greater-than
8722      operator.  */
8723   *is_non_type = true;
8724   parameter_declarator
8725      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8726                                         /*parenthesized_p=*/NULL);
8727   parm = grokdeclarator (parameter_declarator->declarator,
8728                          &parameter_declarator->decl_specifiers,
8729                          PARM, /*initialized=*/0,
8730                          /*attrlist=*/NULL);
8731   if (parm == error_mark_node)
8732     return error_mark_node;
8733   return build_tree_list (parameter_declarator->default_argument, parm);
8734 }
8735
8736 /* Parse a type-parameter.
8737
8738    type-parameter:
8739      class identifier [opt]
8740      class identifier [opt] = type-id
8741      typename identifier [opt]
8742      typename identifier [opt] = type-id
8743      template < template-parameter-list > class identifier [opt]
8744      template < template-parameter-list > class identifier [opt]
8745        = id-expression
8746
8747    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8748    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8749    the declaration of the parameter.  */
8750
8751 static tree
8752 cp_parser_type_parameter (cp_parser* parser)
8753 {
8754   cp_token *token;
8755   tree parameter;
8756
8757   /* Look for a keyword to tell us what kind of parameter this is.  */
8758   token = cp_parser_require (parser, CPP_KEYWORD,
8759                              "`class', `typename', or `template'");
8760   if (!token)
8761     return error_mark_node;
8762
8763   switch (token->keyword)
8764     {
8765     case RID_CLASS:
8766     case RID_TYPENAME:
8767       {
8768         tree identifier;
8769         tree default_argument;
8770
8771         /* If the next token is an identifier, then it names the
8772            parameter.  */
8773         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8774           identifier = cp_parser_identifier (parser);
8775         else
8776           identifier = NULL_TREE;
8777
8778         /* Create the parameter.  */
8779         parameter = finish_template_type_parm (class_type_node, identifier);
8780
8781         /* If the next token is an `=', we have a default argument.  */
8782         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8783           {
8784             /* Consume the `=' token.  */
8785             cp_lexer_consume_token (parser->lexer);
8786             /* Parse the default-argument.  */
8787             push_deferring_access_checks (dk_no_deferred);
8788             default_argument = cp_parser_type_id (parser);
8789             pop_deferring_access_checks ();
8790           }
8791         else
8792           default_argument = NULL_TREE;
8793
8794         /* Create the combined representation of the parameter and the
8795            default argument.  */
8796         parameter = build_tree_list (default_argument, parameter);
8797       }
8798       break;
8799
8800     case RID_TEMPLATE:
8801       {
8802         tree parameter_list;
8803         tree identifier;
8804         tree default_argument;
8805
8806         /* Look for the `<'.  */
8807         cp_parser_require (parser, CPP_LESS, "`<'");
8808         /* Parse the template-parameter-list.  */
8809         parameter_list = cp_parser_template_parameter_list (parser);
8810         /* Look for the `>'.  */
8811         cp_parser_require (parser, CPP_GREATER, "`>'");
8812         /* Look for the `class' keyword.  */
8813         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8814         /* If the next token is an `=', then there is a
8815            default-argument.  If the next token is a `>', we are at
8816            the end of the parameter-list.  If the next token is a `,',
8817            then we are at the end of this parameter.  */
8818         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8819             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8820             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8821           {
8822             identifier = cp_parser_identifier (parser);
8823             /* Treat invalid names as if the parameter were nameless.  */
8824             if (identifier == error_mark_node)
8825               identifier = NULL_TREE;
8826           }
8827         else
8828           identifier = NULL_TREE;
8829
8830         /* Create the template parameter.  */
8831         parameter = finish_template_template_parm (class_type_node,
8832                                                    identifier);
8833
8834         /* If the next token is an `=', then there is a
8835            default-argument.  */
8836         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8837           {
8838             bool is_template;
8839
8840             /* Consume the `='.  */
8841             cp_lexer_consume_token (parser->lexer);
8842             /* Parse the id-expression.  */
8843             push_deferring_access_checks (dk_no_deferred);
8844             default_argument
8845               = cp_parser_id_expression (parser,
8846                                          /*template_keyword_p=*/false,
8847                                          /*check_dependency_p=*/true,
8848                                          /*template_p=*/&is_template,
8849                                          /*declarator_p=*/false,
8850                                          /*optional_p=*/false);
8851             if (TREE_CODE (default_argument) == TYPE_DECL)
8852               /* If the id-expression was a template-id that refers to
8853                  a template-class, we already have the declaration here,
8854                  so no further lookup is needed.  */
8855                  ;
8856             else
8857               /* Look up the name.  */
8858               default_argument
8859                 = cp_parser_lookup_name (parser, default_argument,
8860                                          none_type,
8861                                          /*is_template=*/is_template,
8862                                          /*is_namespace=*/false,
8863                                          /*check_dependency=*/true,
8864                                          /*ambiguous_decls=*/NULL);
8865             /* See if the default argument is valid.  */
8866             default_argument
8867               = check_template_template_default_arg (default_argument);
8868             pop_deferring_access_checks ();
8869           }
8870         else
8871           default_argument = NULL_TREE;
8872
8873         /* Create the combined representation of the parameter and the
8874            default argument.  */
8875         parameter = build_tree_list (default_argument, parameter);
8876       }
8877       break;
8878
8879     default:
8880       gcc_unreachable ();
8881       break;
8882     }
8883
8884   return parameter;
8885 }
8886
8887 /* Parse a template-id.
8888
8889    template-id:
8890      template-name < template-argument-list [opt] >
8891
8892    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8893    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8894    returned.  Otherwise, if the template-name names a function, or set
8895    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8896    names a class, returns a TYPE_DECL for the specialization.
8897
8898    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8899    uninstantiated templates.  */
8900
8901 static tree
8902 cp_parser_template_id (cp_parser *parser,
8903                        bool template_keyword_p,
8904                        bool check_dependency_p,
8905                        bool is_declaration)
8906 {
8907   int i;
8908   tree template;
8909   tree arguments;
8910   tree template_id;
8911   cp_token_position start_of_id = 0;
8912   deferred_access_check *chk;
8913   VEC (deferred_access_check,gc) *access_check;
8914   cp_token *next_token, *next_token_2;
8915   bool is_identifier;
8916
8917   /* If the next token corresponds to a template-id, there is no need
8918      to reparse it.  */
8919   next_token = cp_lexer_peek_token (parser->lexer);
8920   if (next_token->type == CPP_TEMPLATE_ID)
8921     {
8922       struct tree_check *check_value;
8923
8924       /* Get the stored value.  */
8925       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
8926       /* Perform any access checks that were deferred.  */
8927       access_check = check_value->checks;
8928       if (access_check)
8929         {
8930           for (i = 0 ;
8931                VEC_iterate (deferred_access_check, access_check, i, chk) ;
8932                ++i)
8933             {
8934               perform_or_defer_access_check (chk->binfo,
8935                                              chk->decl,
8936                                              chk->diag_decl);
8937             }
8938         }
8939       /* Return the stored value.  */
8940       return check_value->value;
8941     }
8942
8943   /* Avoid performing name lookup if there is no possibility of
8944      finding a template-id.  */
8945   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8946       || (next_token->type == CPP_NAME
8947           && !cp_parser_nth_token_starts_template_argument_list_p
8948                (parser, 2)))
8949     {
8950       cp_parser_error (parser, "expected template-id");
8951       return error_mark_node;
8952     }
8953
8954   /* Remember where the template-id starts.  */
8955   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8956     start_of_id = cp_lexer_token_position (parser->lexer, false);
8957
8958   push_deferring_access_checks (dk_deferred);
8959
8960   /* Parse the template-name.  */
8961   is_identifier = false;
8962   template = cp_parser_template_name (parser, template_keyword_p,
8963                                       check_dependency_p,
8964                                       is_declaration,
8965                                       &is_identifier);
8966   if (template == error_mark_node || is_identifier)
8967     {
8968       pop_deferring_access_checks ();
8969       return template;
8970     }
8971
8972   /* If we find the sequence `[:' after a template-name, it's probably
8973      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8974      parse correctly the argument list.  */
8975   next_token = cp_lexer_peek_token (parser->lexer);
8976   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8977   if (next_token->type == CPP_OPEN_SQUARE
8978       && next_token->flags & DIGRAPH
8979       && next_token_2->type == CPP_COLON
8980       && !(next_token_2->flags & PREV_WHITE))
8981     {
8982       cp_parser_parse_tentatively (parser);
8983       /* Change `:' into `::'.  */
8984       next_token_2->type = CPP_SCOPE;
8985       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8986          CPP_LESS.  */
8987       cp_lexer_consume_token (parser->lexer);
8988       /* Parse the arguments.  */
8989       arguments = cp_parser_enclosed_template_argument_list (parser);
8990       if (!cp_parser_parse_definitely (parser))
8991         {
8992           /* If we couldn't parse an argument list, then we revert our changes
8993              and return simply an error. Maybe this is not a template-id
8994              after all.  */
8995           next_token_2->type = CPP_COLON;
8996           cp_parser_error (parser, "expected %<<%>");
8997           pop_deferring_access_checks ();
8998           return error_mark_node;
8999         }
9000       /* Otherwise, emit an error about the invalid digraph, but continue
9001          parsing because we got our argument list.  */
9002       pedwarn ("%<<::%> cannot begin a template-argument list");
9003       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9004               "between %<<%> and %<::%>");
9005       if (!flag_permissive)
9006         {
9007           static bool hint;
9008           if (!hint)
9009             {
9010               inform ("(if you use -fpermissive G++ will accept your code)");
9011               hint = true;
9012             }
9013         }
9014     }
9015   else
9016     {
9017       /* Look for the `<' that starts the template-argument-list.  */
9018       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9019         {
9020           pop_deferring_access_checks ();
9021           return error_mark_node;
9022         }
9023       /* Parse the arguments.  */
9024       arguments = cp_parser_enclosed_template_argument_list (parser);
9025     }
9026
9027   /* Build a representation of the specialization.  */
9028   if (TREE_CODE (template) == IDENTIFIER_NODE)
9029     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9030   else if (DECL_CLASS_TEMPLATE_P (template)
9031            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9032     {
9033       bool entering_scope;
9034       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9035          template (rather than some instantiation thereof) only if
9036          is not nested within some other construct.  For example, in
9037          "template <typename T> void f(T) { A<T>::", A<T> is just an
9038          instantiation of A.  */
9039       entering_scope = (template_parm_scope_p ()
9040                         && cp_lexer_next_token_is (parser->lexer,
9041                                                    CPP_SCOPE));
9042       template_id
9043         = finish_template_type (template, arguments, entering_scope);
9044     }
9045   else
9046     {
9047       /* If it's not a class-template or a template-template, it should be
9048          a function-template.  */
9049       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9050                    || TREE_CODE (template) == OVERLOAD
9051                    || BASELINK_P (template)));
9052
9053       template_id = lookup_template_function (template, arguments);
9054     }
9055
9056   /* If parsing tentatively, replace the sequence of tokens that makes
9057      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9058      should we re-parse the token stream, we will not have to repeat
9059      the effort required to do the parse, nor will we issue duplicate
9060      error messages about problems during instantiation of the
9061      template.  */
9062   if (start_of_id)
9063     {
9064       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9065
9066       /* Reset the contents of the START_OF_ID token.  */
9067       token->type = CPP_TEMPLATE_ID;
9068       /* Retrieve any deferred checks.  Do not pop this access checks yet
9069          so the memory will not be reclaimed during token replacing below.  */
9070       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9071       token->u.tree_check_value->value = template_id;
9072       token->u.tree_check_value->checks = get_deferred_access_checks ();
9073       token->keyword = RID_MAX;
9074
9075       /* Purge all subsequent tokens.  */
9076       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9077
9078       /* ??? Can we actually assume that, if template_id ==
9079          error_mark_node, we will have issued a diagnostic to the
9080          user, as opposed to simply marking the tentative parse as
9081          failed?  */
9082       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9083         error ("parse error in template argument list");
9084     }
9085
9086   pop_deferring_access_checks ();
9087   return template_id;
9088 }
9089
9090 /* Parse a template-name.
9091
9092    template-name:
9093      identifier
9094
9095    The standard should actually say:
9096
9097    template-name:
9098      identifier
9099      operator-function-id
9100
9101    A defect report has been filed about this issue.
9102
9103    A conversion-function-id cannot be a template name because they cannot
9104    be part of a template-id. In fact, looking at this code:
9105
9106    a.operator K<int>()
9107
9108    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9109    It is impossible to call a templated conversion-function-id with an
9110    explicit argument list, since the only allowed template parameter is
9111    the type to which it is converting.
9112
9113    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9114    `template' keyword, in a construction like:
9115
9116      T::template f<3>()
9117
9118    In that case `f' is taken to be a template-name, even though there
9119    is no way of knowing for sure.
9120
9121    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9122    name refers to a set of overloaded functions, at least one of which
9123    is a template, or an IDENTIFIER_NODE with the name of the template,
9124    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9125    names are looked up inside uninstantiated templates.  */
9126
9127 static tree
9128 cp_parser_template_name (cp_parser* parser,
9129                          bool template_keyword_p,
9130                          bool check_dependency_p,
9131                          bool is_declaration,
9132                          bool *is_identifier)
9133 {
9134   tree identifier;
9135   tree decl;
9136   tree fns;
9137
9138   /* If the next token is `operator', then we have either an
9139      operator-function-id or a conversion-function-id.  */
9140   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9141     {
9142       /* We don't know whether we're looking at an
9143          operator-function-id or a conversion-function-id.  */
9144       cp_parser_parse_tentatively (parser);
9145       /* Try an operator-function-id.  */
9146       identifier = cp_parser_operator_function_id (parser);
9147       /* If that didn't work, try a conversion-function-id.  */
9148       if (!cp_parser_parse_definitely (parser))
9149         {
9150           cp_parser_error (parser, "expected template-name");
9151           return error_mark_node;
9152         }
9153     }
9154   /* Look for the identifier.  */
9155   else
9156     identifier = cp_parser_identifier (parser);
9157
9158   /* If we didn't find an identifier, we don't have a template-id.  */
9159   if (identifier == error_mark_node)
9160     return error_mark_node;
9161
9162   /* If the name immediately followed the `template' keyword, then it
9163      is a template-name.  However, if the next token is not `<', then
9164      we do not treat it as a template-name, since it is not being used
9165      as part of a template-id.  This enables us to handle constructs
9166      like:
9167
9168        template <typename T> struct S { S(); };
9169        template <typename T> S<T>::S();
9170
9171      correctly.  We would treat `S' as a template -- if it were `S<T>'
9172      -- but we do not if there is no `<'.  */
9173
9174   if (processing_template_decl
9175       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9176     {
9177       /* In a declaration, in a dependent context, we pretend that the
9178          "template" keyword was present in order to improve error
9179          recovery.  For example, given:
9180
9181            template <typename T> void f(T::X<int>);
9182
9183          we want to treat "X<int>" as a template-id.  */
9184       if (is_declaration
9185           && !template_keyword_p
9186           && parser->scope && TYPE_P (parser->scope)
9187           && check_dependency_p
9188           && dependent_type_p (parser->scope)
9189           /* Do not do this for dtors (or ctors), since they never
9190              need the template keyword before their name.  */
9191           && !constructor_name_p (identifier, parser->scope))
9192         {
9193           cp_token_position start = 0;
9194
9195           /* Explain what went wrong.  */
9196           error ("non-template %qD used as template", identifier);
9197           inform ("use %<%T::template %D%> to indicate that it is a template",
9198                   parser->scope, identifier);
9199           /* If parsing tentatively, find the location of the "<" token.  */
9200           if (cp_parser_simulate_error (parser))
9201             start = cp_lexer_token_position (parser->lexer, true);
9202           /* Parse the template arguments so that we can issue error
9203              messages about them.  */
9204           cp_lexer_consume_token (parser->lexer);
9205           cp_parser_enclosed_template_argument_list (parser);
9206           /* Skip tokens until we find a good place from which to
9207              continue parsing.  */
9208           cp_parser_skip_to_closing_parenthesis (parser,
9209                                                  /*recovering=*/true,
9210                                                  /*or_comma=*/true,
9211                                                  /*consume_paren=*/false);
9212           /* If parsing tentatively, permanently remove the
9213              template argument list.  That will prevent duplicate
9214              error messages from being issued about the missing
9215              "template" keyword.  */
9216           if (start)
9217             cp_lexer_purge_tokens_after (parser->lexer, start);
9218           if (is_identifier)
9219             *is_identifier = true;
9220           return identifier;
9221         }
9222
9223       /* If the "template" keyword is present, then there is generally
9224          no point in doing name-lookup, so we just return IDENTIFIER.
9225          But, if the qualifying scope is non-dependent then we can
9226          (and must) do name-lookup normally.  */
9227       if (template_keyword_p
9228           && (!parser->scope
9229               || (TYPE_P (parser->scope)
9230                   && dependent_type_p (parser->scope))))
9231         return identifier;
9232     }
9233
9234   /* Look up the name.  */
9235   decl = cp_parser_lookup_name (parser, identifier,
9236                                 none_type,
9237                                 /*is_template=*/false,
9238                                 /*is_namespace=*/false,
9239                                 check_dependency_p,
9240                                 /*ambiguous_decls=*/NULL);
9241   decl = maybe_get_template_decl_from_type_decl (decl);
9242
9243   /* If DECL is a template, then the name was a template-name.  */
9244   if (TREE_CODE (decl) == TEMPLATE_DECL)
9245     ;
9246   else
9247     {
9248       tree fn = NULL_TREE;
9249
9250       /* The standard does not explicitly indicate whether a name that
9251          names a set of overloaded declarations, some of which are
9252          templates, is a template-name.  However, such a name should
9253          be a template-name; otherwise, there is no way to form a
9254          template-id for the overloaded templates.  */
9255       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9256       if (TREE_CODE (fns) == OVERLOAD)
9257         for (fn = fns; fn; fn = OVL_NEXT (fn))
9258           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9259             break;
9260
9261       if (!fn)
9262         {
9263           /* The name does not name a template.  */
9264           cp_parser_error (parser, "expected template-name");
9265           return error_mark_node;
9266         }
9267     }
9268
9269   /* If DECL is dependent, and refers to a function, then just return
9270      its name; we will look it up again during template instantiation.  */
9271   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9272     {
9273       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9274       if (TYPE_P (scope) && dependent_type_p (scope))
9275         return identifier;
9276     }
9277
9278   return decl;
9279 }
9280
9281 /* Parse a template-argument-list.
9282
9283    template-argument-list:
9284      template-argument
9285      template-argument-list , template-argument
9286
9287    Returns a TREE_VEC containing the arguments.  */
9288
9289 static tree
9290 cp_parser_template_argument_list (cp_parser* parser)
9291 {
9292   tree fixed_args[10];
9293   unsigned n_args = 0;
9294   unsigned alloced = 10;
9295   tree *arg_ary = fixed_args;
9296   tree vec;
9297   bool saved_in_template_argument_list_p;
9298   bool saved_ice_p;
9299   bool saved_non_ice_p;
9300
9301   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9302   parser->in_template_argument_list_p = true;
9303   /* Even if the template-id appears in an integral
9304      constant-expression, the contents of the argument list do
9305      not.  */
9306   saved_ice_p = parser->integral_constant_expression_p;
9307   parser->integral_constant_expression_p = false;
9308   saved_non_ice_p = parser->non_integral_constant_expression_p;
9309   parser->non_integral_constant_expression_p = false;
9310   /* Parse the arguments.  */
9311   do
9312     {
9313       tree argument;
9314
9315       if (n_args)
9316         /* Consume the comma.  */
9317         cp_lexer_consume_token (parser->lexer);
9318
9319       /* Parse the template-argument.  */
9320       argument = cp_parser_template_argument (parser);
9321       if (n_args == alloced)
9322         {
9323           alloced *= 2;
9324
9325           if (arg_ary == fixed_args)
9326             {
9327               arg_ary = XNEWVEC (tree, alloced);
9328               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9329             }
9330           else
9331             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9332         }
9333       arg_ary[n_args++] = argument;
9334     }
9335   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9336
9337   vec = make_tree_vec (n_args);
9338
9339   while (n_args--)
9340     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9341
9342   if (arg_ary != fixed_args)
9343     free (arg_ary);
9344   parser->non_integral_constant_expression_p = saved_non_ice_p;
9345   parser->integral_constant_expression_p = saved_ice_p;
9346   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9347   return vec;
9348 }
9349
9350 /* Parse a template-argument.
9351
9352    template-argument:
9353      assignment-expression
9354      type-id
9355      id-expression
9356
9357    The representation is that of an assignment-expression, type-id, or
9358    id-expression -- except that the qualified id-expression is
9359    evaluated, so that the value returned is either a DECL or an
9360    OVERLOAD.
9361
9362    Although the standard says "assignment-expression", it forbids
9363    throw-expressions or assignments in the template argument.
9364    Therefore, we use "conditional-expression" instead.  */
9365
9366 static tree
9367 cp_parser_template_argument (cp_parser* parser)
9368 {
9369   tree argument;
9370   bool template_p;
9371   bool address_p;
9372   bool maybe_type_id = false;
9373   cp_token *token;
9374   cp_id_kind idk;
9375
9376   /* There's really no way to know what we're looking at, so we just
9377      try each alternative in order.
9378
9379        [temp.arg]
9380
9381        In a template-argument, an ambiguity between a type-id and an
9382        expression is resolved to a type-id, regardless of the form of
9383        the corresponding template-parameter.
9384
9385      Therefore, we try a type-id first.  */
9386   cp_parser_parse_tentatively (parser);
9387   argument = cp_parser_type_id (parser);
9388   /* If there was no error parsing the type-id but the next token is a '>>',
9389      we probably found a typo for '> >'. But there are type-id which are
9390      also valid expressions. For instance:
9391
9392      struct X { int operator >> (int); };
9393      template <int V> struct Foo {};
9394      Foo<X () >> 5> r;
9395
9396      Here 'X()' is a valid type-id of a function type, but the user just
9397      wanted to write the expression "X() >> 5". Thus, we remember that we
9398      found a valid type-id, but we still try to parse the argument as an
9399      expression to see what happens.  */
9400   if (!cp_parser_error_occurred (parser)
9401       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9402     {
9403       maybe_type_id = true;
9404       cp_parser_abort_tentative_parse (parser);
9405     }
9406   else
9407     {
9408       /* If the next token isn't a `,' or a `>', then this argument wasn't
9409       really finished. This means that the argument is not a valid
9410       type-id.  */
9411       if (!cp_parser_next_token_ends_template_argument_p (parser))
9412         cp_parser_error (parser, "expected template-argument");
9413       /* If that worked, we're done.  */
9414       if (cp_parser_parse_definitely (parser))
9415         return argument;
9416     }
9417   /* We're still not sure what the argument will be.  */
9418   cp_parser_parse_tentatively (parser);
9419   /* Try a template.  */
9420   argument = cp_parser_id_expression (parser,
9421                                       /*template_keyword_p=*/false,
9422                                       /*check_dependency_p=*/true,
9423                                       &template_p,
9424                                       /*declarator_p=*/false,
9425                                       /*optional_p=*/false);
9426   /* If the next token isn't a `,' or a `>', then this argument wasn't
9427      really finished.  */
9428   if (!cp_parser_next_token_ends_template_argument_p (parser))
9429     cp_parser_error (parser, "expected template-argument");
9430   if (!cp_parser_error_occurred (parser))
9431     {
9432       /* Figure out what is being referred to.  If the id-expression
9433          was for a class template specialization, then we will have a
9434          TYPE_DECL at this point.  There is no need to do name lookup
9435          at this point in that case.  */
9436       if (TREE_CODE (argument) != TYPE_DECL)
9437         argument = cp_parser_lookup_name (parser, argument,
9438                                           none_type,
9439                                           /*is_template=*/template_p,
9440                                           /*is_namespace=*/false,
9441                                           /*check_dependency=*/true,
9442                                           /*ambiguous_decls=*/NULL);
9443       if (TREE_CODE (argument) != TEMPLATE_DECL
9444           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9445         cp_parser_error (parser, "expected template-name");
9446     }
9447   if (cp_parser_parse_definitely (parser))
9448     return argument;
9449   /* It must be a non-type argument.  There permitted cases are given
9450      in [temp.arg.nontype]:
9451
9452      -- an integral constant-expression of integral or enumeration
9453         type; or
9454
9455      -- the name of a non-type template-parameter; or
9456
9457      -- the name of an object or function with external linkage...
9458
9459      -- the address of an object or function with external linkage...
9460
9461      -- a pointer to member...  */
9462   /* Look for a non-type template parameter.  */
9463   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9464     {
9465       cp_parser_parse_tentatively (parser);
9466       argument = cp_parser_primary_expression (parser,
9467                                                /*adress_p=*/false,
9468                                                /*cast_p=*/false,
9469                                                /*template_arg_p=*/true,
9470                                                &idk);
9471       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9472           || !cp_parser_next_token_ends_template_argument_p (parser))
9473         cp_parser_simulate_error (parser);
9474       if (cp_parser_parse_definitely (parser))
9475         return argument;
9476     }
9477
9478   /* If the next token is "&", the argument must be the address of an
9479      object or function with external linkage.  */
9480   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9481   if (address_p)
9482     cp_lexer_consume_token (parser->lexer);
9483   /* See if we might have an id-expression.  */
9484   token = cp_lexer_peek_token (parser->lexer);
9485   if (token->type == CPP_NAME
9486       || token->keyword == RID_OPERATOR
9487       || token->type == CPP_SCOPE
9488       || token->type == CPP_TEMPLATE_ID
9489       || token->type == CPP_NESTED_NAME_SPECIFIER)
9490     {
9491       cp_parser_parse_tentatively (parser);
9492       argument = cp_parser_primary_expression (parser,
9493                                                address_p,
9494                                                /*cast_p=*/false,
9495                                                /*template_arg_p=*/true,
9496                                                &idk);
9497       if (cp_parser_error_occurred (parser)
9498           || !cp_parser_next_token_ends_template_argument_p (parser))
9499         cp_parser_abort_tentative_parse (parser);
9500       else
9501         {
9502           if (TREE_CODE (argument) == INDIRECT_REF)
9503             {
9504               gcc_assert (REFERENCE_REF_P (argument));
9505               argument = TREE_OPERAND (argument, 0);
9506             }
9507
9508           if (TREE_CODE (argument) == VAR_DECL)
9509             {
9510               /* A variable without external linkage might still be a
9511                  valid constant-expression, so no error is issued here
9512                  if the external-linkage check fails.  */
9513               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9514                 cp_parser_simulate_error (parser);
9515             }
9516           else if (is_overloaded_fn (argument))
9517             /* All overloaded functions are allowed; if the external
9518                linkage test does not pass, an error will be issued
9519                later.  */
9520             ;
9521           else if (address_p
9522                    && (TREE_CODE (argument) == OFFSET_REF
9523                        || TREE_CODE (argument) == SCOPE_REF))
9524             /* A pointer-to-member.  */
9525             ;
9526           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9527             ;
9528           else
9529             cp_parser_simulate_error (parser);
9530
9531           if (cp_parser_parse_definitely (parser))
9532             {
9533               if (address_p)
9534                 argument = build_x_unary_op (ADDR_EXPR, argument);
9535               return argument;
9536             }
9537         }
9538     }
9539   /* If the argument started with "&", there are no other valid
9540      alternatives at this point.  */
9541   if (address_p)
9542     {
9543       cp_parser_error (parser, "invalid non-type template argument");
9544       return error_mark_node;
9545     }
9546
9547   /* If the argument wasn't successfully parsed as a type-id followed
9548      by '>>', the argument can only be a constant expression now.
9549      Otherwise, we try parsing the constant-expression tentatively,
9550      because the argument could really be a type-id.  */
9551   if (maybe_type_id)
9552     cp_parser_parse_tentatively (parser);
9553   argument = cp_parser_constant_expression (parser,
9554                                             /*allow_non_constant_p=*/false,
9555                                             /*non_constant_p=*/NULL);
9556   argument = fold_non_dependent_expr (argument);
9557   if (!maybe_type_id)
9558     return argument;
9559   if (!cp_parser_next_token_ends_template_argument_p (parser))
9560     cp_parser_error (parser, "expected template-argument");
9561   if (cp_parser_parse_definitely (parser))
9562     return argument;
9563   /* We did our best to parse the argument as a non type-id, but that
9564      was the only alternative that matched (albeit with a '>' after
9565      it). We can assume it's just a typo from the user, and a
9566      diagnostic will then be issued.  */
9567   return cp_parser_type_id (parser);
9568 }
9569
9570 /* Parse an explicit-instantiation.
9571
9572    explicit-instantiation:
9573      template declaration
9574
9575    Although the standard says `declaration', what it really means is:
9576
9577    explicit-instantiation:
9578      template decl-specifier-seq [opt] declarator [opt] ;
9579
9580    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9581    supposed to be allowed.  A defect report has been filed about this
9582    issue.
9583
9584    GNU Extension:
9585
9586    explicit-instantiation:
9587      storage-class-specifier template
9588        decl-specifier-seq [opt] declarator [opt] ;
9589      function-specifier template
9590        decl-specifier-seq [opt] declarator [opt] ;  */
9591
9592 static void
9593 cp_parser_explicit_instantiation (cp_parser* parser)
9594 {
9595   int declares_class_or_enum;
9596   cp_decl_specifier_seq decl_specifiers;
9597   tree extension_specifier = NULL_TREE;
9598
9599   /* Look for an (optional) storage-class-specifier or
9600      function-specifier.  */
9601   if (cp_parser_allow_gnu_extensions_p (parser))
9602     {
9603       extension_specifier
9604         = cp_parser_storage_class_specifier_opt (parser);
9605       if (!extension_specifier)
9606         extension_specifier
9607           = cp_parser_function_specifier_opt (parser,
9608                                               /*decl_specs=*/NULL);
9609     }
9610
9611   /* Look for the `template' keyword.  */
9612   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9613   /* Let the front end know that we are processing an explicit
9614      instantiation.  */
9615   begin_explicit_instantiation ();
9616   /* [temp.explicit] says that we are supposed to ignore access
9617      control while processing explicit instantiation directives.  */
9618   push_deferring_access_checks (dk_no_check);
9619   /* Parse a decl-specifier-seq.  */
9620   cp_parser_decl_specifier_seq (parser,
9621                                 CP_PARSER_FLAGS_OPTIONAL,
9622                                 &decl_specifiers,
9623                                 &declares_class_or_enum);
9624   /* If there was exactly one decl-specifier, and it declared a class,
9625      and there's no declarator, then we have an explicit type
9626      instantiation.  */
9627   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9628     {
9629       tree type;
9630
9631       type = check_tag_decl (&decl_specifiers);
9632       /* Turn access control back on for names used during
9633          template instantiation.  */
9634       pop_deferring_access_checks ();
9635       if (type)
9636         do_type_instantiation (type, extension_specifier,
9637                                /*complain=*/tf_error);
9638     }
9639   else
9640     {
9641       cp_declarator *declarator;
9642       tree decl;
9643
9644       /* Parse the declarator.  */
9645       declarator
9646         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9647                                 /*ctor_dtor_or_conv_p=*/NULL,
9648                                 /*parenthesized_p=*/NULL,
9649                                 /*member_p=*/false);
9650       if (declares_class_or_enum & 2)
9651         cp_parser_check_for_definition_in_return_type (declarator,
9652                                                        decl_specifiers.type);
9653       if (declarator != cp_error_declarator)
9654         {
9655           decl = grokdeclarator (declarator, &decl_specifiers,
9656                                  NORMAL, 0, &decl_specifiers.attributes);
9657           /* Turn access control back on for names used during
9658              template instantiation.  */
9659           pop_deferring_access_checks ();
9660           /* Do the explicit instantiation.  */
9661           do_decl_instantiation (decl, extension_specifier);
9662         }
9663       else
9664         {
9665           pop_deferring_access_checks ();
9666           /* Skip the body of the explicit instantiation.  */
9667           cp_parser_skip_to_end_of_statement (parser);
9668         }
9669     }
9670   /* We're done with the instantiation.  */
9671   end_explicit_instantiation ();
9672
9673   cp_parser_consume_semicolon_at_end_of_statement (parser);
9674 }
9675
9676 /* Parse an explicit-specialization.
9677
9678    explicit-specialization:
9679      template < > declaration
9680
9681    Although the standard says `declaration', what it really means is:
9682
9683    explicit-specialization:
9684      template <> decl-specifier [opt] init-declarator [opt] ;
9685      template <> function-definition
9686      template <> explicit-specialization
9687      template <> template-declaration  */
9688
9689 static void
9690 cp_parser_explicit_specialization (cp_parser* parser)
9691 {
9692   bool need_lang_pop;
9693   /* Look for the `template' keyword.  */
9694   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9695   /* Look for the `<'.  */
9696   cp_parser_require (parser, CPP_LESS, "`<'");
9697   /* Look for the `>'.  */
9698   cp_parser_require (parser, CPP_GREATER, "`>'");
9699   /* We have processed another parameter list.  */
9700   ++parser->num_template_parameter_lists;
9701   /* [temp]
9702
9703      A template ... explicit specialization ... shall not have C
9704      linkage.  */
9705   if (current_lang_name == lang_name_c)
9706     {
9707       error ("template specialization with C linkage");
9708       /* Give it C++ linkage to avoid confusing other parts of the
9709          front end.  */
9710       push_lang_context (lang_name_cplusplus);
9711       need_lang_pop = true;
9712     }
9713   else
9714     need_lang_pop = false;
9715   /* Let the front end know that we are beginning a specialization.  */
9716   if (!begin_specialization ())
9717     {
9718       end_specialization ();
9719       cp_parser_skip_to_end_of_block_or_statement (parser);
9720       return;
9721     }
9722
9723   /* If the next keyword is `template', we need to figure out whether
9724      or not we're looking a template-declaration.  */
9725   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9726     {
9727       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9728           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9729         cp_parser_template_declaration_after_export (parser,
9730                                                      /*member_p=*/false);
9731       else
9732         cp_parser_explicit_specialization (parser);
9733     }
9734   else
9735     /* Parse the dependent declaration.  */
9736     cp_parser_single_declaration (parser,
9737                                   /*checks=*/NULL,
9738                                   /*member_p=*/false,
9739                                   /*friend_p=*/NULL);
9740   /* We're done with the specialization.  */
9741   end_specialization ();
9742   /* For the erroneous case of a template with C linkage, we pushed an
9743      implicit C++ linkage scope; exit that scope now.  */
9744   if (need_lang_pop)
9745     pop_lang_context ();
9746   /* We're done with this parameter list.  */
9747   --parser->num_template_parameter_lists;
9748 }
9749
9750 /* Parse a type-specifier.
9751
9752    type-specifier:
9753      simple-type-specifier
9754      class-specifier
9755      enum-specifier
9756      elaborated-type-specifier
9757      cv-qualifier
9758
9759    GNU Extension:
9760
9761    type-specifier:
9762      __complex__
9763
9764    Returns a representation of the type-specifier.  For a
9765    class-specifier, enum-specifier, or elaborated-type-specifier, a
9766    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9767
9768    The parser flags FLAGS is used to control type-specifier parsing.
9769
9770    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9771    in a decl-specifier-seq.
9772
9773    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9774    class-specifier, enum-specifier, or elaborated-type-specifier, then
9775    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9776    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9777    zero.
9778
9779    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9780    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9781    is set to FALSE.  */
9782
9783 static tree
9784 cp_parser_type_specifier (cp_parser* parser,
9785                           cp_parser_flags flags,
9786                           cp_decl_specifier_seq *decl_specs,
9787                           bool is_declaration,
9788                           int* declares_class_or_enum,
9789                           bool* is_cv_qualifier)
9790 {
9791   tree type_spec = NULL_TREE;
9792   cp_token *token;
9793   enum rid keyword;
9794   cp_decl_spec ds = ds_last;
9795
9796   /* Assume this type-specifier does not declare a new type.  */
9797   if (declares_class_or_enum)
9798     *declares_class_or_enum = 0;
9799   /* And that it does not specify a cv-qualifier.  */
9800   if (is_cv_qualifier)
9801     *is_cv_qualifier = false;
9802   /* Peek at the next token.  */
9803   token = cp_lexer_peek_token (parser->lexer);
9804
9805   /* If we're looking at a keyword, we can use that to guide the
9806      production we choose.  */
9807   keyword = token->keyword;
9808   switch (keyword)
9809     {
9810     case RID_ENUM:
9811       /* Look for the enum-specifier.  */
9812       type_spec = cp_parser_enum_specifier (parser);
9813       /* If that worked, we're done.  */
9814       if (type_spec)
9815         {
9816           if (declares_class_or_enum)
9817             *declares_class_or_enum = 2;
9818           if (decl_specs)
9819             cp_parser_set_decl_spec_type (decl_specs,
9820                                           type_spec,
9821                                           /*user_defined_p=*/true);
9822           return type_spec;
9823         }
9824       else
9825         goto elaborated_type_specifier;
9826
9827       /* Any of these indicate either a class-specifier, or an
9828          elaborated-type-specifier.  */
9829     case RID_CLASS:
9830     case RID_STRUCT:
9831     case RID_UNION:
9832       /* Parse tentatively so that we can back up if we don't find a
9833          class-specifier.  */
9834       cp_parser_parse_tentatively (parser);
9835       /* Look for the class-specifier.  */
9836       type_spec = cp_parser_class_specifier (parser);
9837       /* If that worked, we're done.  */
9838       if (cp_parser_parse_definitely (parser))
9839         {
9840           if (declares_class_or_enum)
9841             *declares_class_or_enum = 2;
9842           if (decl_specs)
9843             cp_parser_set_decl_spec_type (decl_specs,
9844                                           type_spec,
9845                                           /*user_defined_p=*/true);
9846           return type_spec;
9847         }
9848
9849       /* Fall through.  */
9850     elaborated_type_specifier:
9851       /* We're declaring (not defining) a class or enum.  */
9852       if (declares_class_or_enum)
9853         *declares_class_or_enum = 1;
9854
9855       /* Fall through.  */
9856     case RID_TYPENAME:
9857       /* Look for an elaborated-type-specifier.  */
9858       type_spec
9859         = (cp_parser_elaborated_type_specifier
9860            (parser,
9861             decl_specs && decl_specs->specs[(int) ds_friend],
9862             is_declaration));
9863       if (decl_specs)
9864         cp_parser_set_decl_spec_type (decl_specs,
9865                                       type_spec,
9866                                       /*user_defined_p=*/true);
9867       return type_spec;
9868
9869     case RID_CONST:
9870       ds = ds_const;
9871       if (is_cv_qualifier)
9872         *is_cv_qualifier = true;
9873       break;
9874
9875     case RID_VOLATILE:
9876       ds = ds_volatile;
9877       if (is_cv_qualifier)
9878         *is_cv_qualifier = true;
9879       break;
9880
9881     case RID_RESTRICT:
9882       ds = ds_restrict;
9883       if (is_cv_qualifier)
9884         *is_cv_qualifier = true;
9885       break;
9886
9887     case RID_COMPLEX:
9888       /* The `__complex__' keyword is a GNU extension.  */
9889       ds = ds_complex;
9890       break;
9891
9892     default:
9893       break;
9894     }
9895
9896   /* Handle simple keywords.  */
9897   if (ds != ds_last)
9898     {
9899       if (decl_specs)
9900         {
9901           ++decl_specs->specs[(int)ds];
9902           decl_specs->any_specifiers_p = true;
9903         }
9904       return cp_lexer_consume_token (parser->lexer)->u.value;
9905     }
9906
9907   /* If we do not already have a type-specifier, assume we are looking
9908      at a simple-type-specifier.  */
9909   type_spec = cp_parser_simple_type_specifier (parser,
9910                                                decl_specs,
9911                                                flags);
9912
9913   /* If we didn't find a type-specifier, and a type-specifier was not
9914      optional in this context, issue an error message.  */
9915   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9916     {
9917       cp_parser_error (parser, "expected type specifier");
9918       return error_mark_node;
9919     }
9920
9921   return type_spec;
9922 }
9923
9924 /* Parse a simple-type-specifier.
9925
9926    simple-type-specifier:
9927      :: [opt] nested-name-specifier [opt] type-name
9928      :: [opt] nested-name-specifier template template-id
9929      char
9930      wchar_t
9931      bool
9932      short
9933      int
9934      long
9935      signed
9936      unsigned
9937      float
9938      double
9939      void
9940
9941    GNU Extension:
9942
9943    simple-type-specifier:
9944      __typeof__ unary-expression
9945      __typeof__ ( type-id )
9946
9947    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9948    appropriately updated.  */
9949
9950 static tree
9951 cp_parser_simple_type_specifier (cp_parser* parser,
9952                                  cp_decl_specifier_seq *decl_specs,
9953                                  cp_parser_flags flags)
9954 {
9955   tree type = NULL_TREE;
9956   cp_token *token;
9957
9958   /* Peek at the next token.  */
9959   token = cp_lexer_peek_token (parser->lexer);
9960
9961   /* If we're looking at a keyword, things are easy.  */
9962   switch (token->keyword)
9963     {
9964     case RID_CHAR:
9965       if (decl_specs)
9966         decl_specs->explicit_char_p = true;
9967       type = char_type_node;
9968       break;
9969     case RID_WCHAR:
9970       type = wchar_type_node;
9971       break;
9972     case RID_BOOL:
9973       type = boolean_type_node;
9974       break;
9975     case RID_SHORT:
9976       if (decl_specs)
9977         ++decl_specs->specs[(int) ds_short];
9978       type = short_integer_type_node;
9979       break;
9980     case RID_INT:
9981       if (decl_specs)
9982         decl_specs->explicit_int_p = true;
9983       type = integer_type_node;
9984       break;
9985     case RID_LONG:
9986       if (decl_specs)
9987         ++decl_specs->specs[(int) ds_long];
9988       type = long_integer_type_node;
9989       break;
9990     case RID_SIGNED:
9991       if (decl_specs)
9992         ++decl_specs->specs[(int) ds_signed];
9993       type = integer_type_node;
9994       break;
9995     case RID_UNSIGNED:
9996       if (decl_specs)
9997         ++decl_specs->specs[(int) ds_unsigned];
9998       type = unsigned_type_node;
9999       break;
10000     case RID_FLOAT:
10001       type = float_type_node;
10002       break;
10003     case RID_DOUBLE:
10004       type = double_type_node;
10005       break;
10006     case RID_VOID:
10007       type = void_type_node;
10008       break;
10009
10010     case RID_TYPEOF:
10011       /* Consume the `typeof' token.  */
10012       cp_lexer_consume_token (parser->lexer);
10013       /* Parse the operand to `typeof'.  */
10014       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10015       /* If it is not already a TYPE, take its type.  */
10016       if (!TYPE_P (type))
10017         type = finish_typeof (type);
10018
10019       if (decl_specs)
10020         cp_parser_set_decl_spec_type (decl_specs, type,
10021                                       /*user_defined_p=*/true);
10022
10023       return type;
10024
10025     default:
10026       break;
10027     }
10028
10029   /* If the type-specifier was for a built-in type, we're done.  */
10030   if (type)
10031     {
10032       tree id;
10033
10034       /* Record the type.  */
10035       if (decl_specs
10036           && (token->keyword != RID_SIGNED
10037               && token->keyword != RID_UNSIGNED
10038               && token->keyword != RID_SHORT
10039               && token->keyword != RID_LONG))
10040         cp_parser_set_decl_spec_type (decl_specs,
10041                                       type,
10042                                       /*user_defined=*/false);
10043       if (decl_specs)
10044         decl_specs->any_specifiers_p = true;
10045
10046       /* Consume the token.  */
10047       id = cp_lexer_consume_token (parser->lexer)->u.value;
10048
10049       /* There is no valid C++ program where a non-template type is
10050          followed by a "<".  That usually indicates that the user thought
10051          that the type was a template.  */
10052       cp_parser_check_for_invalid_template_id (parser, type);
10053
10054       return TYPE_NAME (type);
10055     }
10056
10057   /* The type-specifier must be a user-defined type.  */
10058   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10059     {
10060       bool qualified_p;
10061       bool global_p;
10062
10063       /* Don't gobble tokens or issue error messages if this is an
10064          optional type-specifier.  */
10065       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10066         cp_parser_parse_tentatively (parser);
10067
10068       /* Look for the optional `::' operator.  */
10069       global_p
10070         = (cp_parser_global_scope_opt (parser,
10071                                        /*current_scope_valid_p=*/false)
10072            != NULL_TREE);
10073       /* Look for the nested-name specifier.  */
10074       qualified_p
10075         = (cp_parser_nested_name_specifier_opt (parser,
10076                                                 /*typename_keyword_p=*/false,
10077                                                 /*check_dependency_p=*/true,
10078                                                 /*type_p=*/false,
10079                                                 /*is_declaration=*/false)
10080            != NULL_TREE);
10081       /* If we have seen a nested-name-specifier, and the next token
10082          is `template', then we are using the template-id production.  */
10083       if (parser->scope
10084           && cp_parser_optional_template_keyword (parser))
10085         {
10086           /* Look for the template-id.  */
10087           type = cp_parser_template_id (parser,
10088                                         /*template_keyword_p=*/true,
10089                                         /*check_dependency_p=*/true,
10090                                         /*is_declaration=*/false);
10091           /* If the template-id did not name a type, we are out of
10092              luck.  */
10093           if (TREE_CODE (type) != TYPE_DECL)
10094             {
10095               cp_parser_error (parser, "expected template-id for type");
10096               type = NULL_TREE;
10097             }
10098         }
10099       /* Otherwise, look for a type-name.  */
10100       else
10101         type = cp_parser_type_name (parser);
10102       /* Keep track of all name-lookups performed in class scopes.  */
10103       if (type
10104           && !global_p
10105           && !qualified_p
10106           && TREE_CODE (type) == TYPE_DECL
10107           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10108         maybe_note_name_used_in_class (DECL_NAME (type), type);
10109       /* If it didn't work out, we don't have a TYPE.  */
10110       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10111           && !cp_parser_parse_definitely (parser))
10112         type = NULL_TREE;
10113       if (type && decl_specs)
10114         cp_parser_set_decl_spec_type (decl_specs, type,
10115                                       /*user_defined=*/true);
10116     }
10117
10118   /* If we didn't get a type-name, issue an error message.  */
10119   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10120     {
10121       cp_parser_error (parser, "expected type-name");
10122       return error_mark_node;
10123     }
10124
10125   /* There is no valid C++ program where a non-template type is
10126      followed by a "<".  That usually indicates that the user thought
10127      that the type was a template.  */
10128   if (type && type != error_mark_node)
10129     {
10130       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10131          If it is, then the '<'...'>' enclose protocol names rather than
10132          template arguments, and so everything is fine.  */
10133       if (c_dialect_objc ()
10134           && (objc_is_id (type) || objc_is_class_name (type)))
10135         {
10136           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10137           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10138
10139           /* Clobber the "unqualified" type previously entered into
10140              DECL_SPECS with the new, improved protocol-qualified version.  */
10141           if (decl_specs)
10142             decl_specs->type = qual_type;
10143
10144           return qual_type;
10145         }
10146
10147       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10148     }
10149
10150   return type;
10151 }
10152
10153 /* Parse a type-name.
10154
10155    type-name:
10156      class-name
10157      enum-name
10158      typedef-name
10159
10160    enum-name:
10161      identifier
10162
10163    typedef-name:
10164      identifier
10165
10166    Returns a TYPE_DECL for the type.  */
10167
10168 static tree
10169 cp_parser_type_name (cp_parser* parser)
10170 {
10171   tree type_decl;
10172   tree identifier;
10173
10174   /* We can't know yet whether it is a class-name or not.  */
10175   cp_parser_parse_tentatively (parser);
10176   /* Try a class-name.  */
10177   type_decl = cp_parser_class_name (parser,
10178                                     /*typename_keyword_p=*/false,
10179                                     /*template_keyword_p=*/false,
10180                                     none_type,
10181                                     /*check_dependency_p=*/true,
10182                                     /*class_head_p=*/false,
10183                                     /*is_declaration=*/false);
10184   /* If it's not a class-name, keep looking.  */
10185   if (!cp_parser_parse_definitely (parser))
10186     {
10187       /* It must be a typedef-name or an enum-name.  */
10188       identifier = cp_parser_identifier (parser);
10189       if (identifier == error_mark_node)
10190         return error_mark_node;
10191
10192       /* Look up the type-name.  */
10193       type_decl = cp_parser_lookup_name_simple (parser, identifier);
10194
10195       if (TREE_CODE (type_decl) != TYPE_DECL
10196           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10197         {
10198           /* See if this is an Objective-C type.  */
10199           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10200           tree type = objc_get_protocol_qualified_type (identifier, protos);
10201           if (type)
10202             type_decl = TYPE_NAME (type);
10203         }
10204
10205       /* Issue an error if we did not find a type-name.  */
10206       if (TREE_CODE (type_decl) != TYPE_DECL)
10207         {
10208           if (!cp_parser_simulate_error (parser))
10209             cp_parser_name_lookup_error (parser, identifier, type_decl,
10210                                          "is not a type");
10211           type_decl = error_mark_node;
10212         }
10213       /* Remember that the name was used in the definition of the
10214          current class so that we can check later to see if the
10215          meaning would have been different after the class was
10216          entirely defined.  */
10217       else if (type_decl != error_mark_node
10218                && !parser->scope)
10219         maybe_note_name_used_in_class (identifier, type_decl);
10220     }
10221
10222   return type_decl;
10223 }
10224
10225
10226 /* Parse an elaborated-type-specifier.  Note that the grammar given
10227    here incorporates the resolution to DR68.
10228
10229    elaborated-type-specifier:
10230      class-key :: [opt] nested-name-specifier [opt] identifier
10231      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10232      enum :: [opt] nested-name-specifier [opt] identifier
10233      typename :: [opt] nested-name-specifier identifier
10234      typename :: [opt] nested-name-specifier template [opt]
10235        template-id
10236
10237    GNU extension:
10238
10239    elaborated-type-specifier:
10240      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10241      class-key attributes :: [opt] nested-name-specifier [opt]
10242                template [opt] template-id
10243      enum attributes :: [opt] nested-name-specifier [opt] identifier
10244
10245    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10246    declared `friend'.  If IS_DECLARATION is TRUE, then this
10247    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10248    something is being declared.
10249
10250    Returns the TYPE specified.  */
10251
10252 static tree
10253 cp_parser_elaborated_type_specifier (cp_parser* parser,
10254                                      bool is_friend,
10255                                      bool is_declaration)
10256 {
10257   enum tag_types tag_type;
10258   tree identifier;
10259   tree type = NULL_TREE;
10260   tree attributes = NULL_TREE;
10261
10262   /* See if we're looking at the `enum' keyword.  */
10263   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10264     {
10265       /* Consume the `enum' token.  */
10266       cp_lexer_consume_token (parser->lexer);
10267       /* Remember that it's an enumeration type.  */
10268       tag_type = enum_type;
10269       /* Parse the attributes.  */
10270       attributes = cp_parser_attributes_opt (parser);
10271     }
10272   /* Or, it might be `typename'.  */
10273   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10274                                            RID_TYPENAME))
10275     {
10276       /* Consume the `typename' token.  */
10277       cp_lexer_consume_token (parser->lexer);
10278       /* Remember that it's a `typename' type.  */
10279       tag_type = typename_type;
10280       /* The `typename' keyword is only allowed in templates.  */
10281       if (!processing_template_decl)
10282         pedwarn ("using %<typename%> outside of template");
10283     }
10284   /* Otherwise it must be a class-key.  */
10285   else
10286     {
10287       tag_type = cp_parser_class_key (parser);
10288       if (tag_type == none_type)
10289         return error_mark_node;
10290       /* Parse the attributes.  */
10291       attributes = cp_parser_attributes_opt (parser);
10292     }
10293
10294   /* Look for the `::' operator.  */
10295   cp_parser_global_scope_opt (parser,
10296                               /*current_scope_valid_p=*/false);
10297   /* Look for the nested-name-specifier.  */
10298   if (tag_type == typename_type)
10299     {
10300       if (!cp_parser_nested_name_specifier (parser,
10301                                            /*typename_keyword_p=*/true,
10302                                            /*check_dependency_p=*/true,
10303                                            /*type_p=*/true,
10304                                             is_declaration))
10305         return error_mark_node;
10306     }
10307   else
10308     /* Even though `typename' is not present, the proposed resolution
10309        to Core Issue 180 says that in `class A<T>::B', `B' should be
10310        considered a type-name, even if `A<T>' is dependent.  */
10311     cp_parser_nested_name_specifier_opt (parser,
10312                                          /*typename_keyword_p=*/true,
10313                                          /*check_dependency_p=*/true,
10314                                          /*type_p=*/true,
10315                                          is_declaration);
10316   /* For everything but enumeration types, consider a template-id.
10317      For an enumeration type, consider only a plain identifier.  */
10318   if (tag_type != enum_type)
10319     {
10320       bool template_p = false;
10321       tree decl;
10322
10323       /* Allow the `template' keyword.  */
10324       template_p = cp_parser_optional_template_keyword (parser);
10325       /* If we didn't see `template', we don't know if there's a
10326          template-id or not.  */
10327       if (!template_p)
10328         cp_parser_parse_tentatively (parser);
10329       /* Parse the template-id.  */
10330       decl = cp_parser_template_id (parser, template_p,
10331                                     /*check_dependency_p=*/true,
10332                                     is_declaration);
10333       /* If we didn't find a template-id, look for an ordinary
10334          identifier.  */
10335       if (!template_p && !cp_parser_parse_definitely (parser))
10336         ;
10337       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10338          in effect, then we must assume that, upon instantiation, the
10339          template will correspond to a class.  */
10340       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10341                && tag_type == typename_type)
10342         type = make_typename_type (parser->scope, decl,
10343                                    typename_type,
10344                                    /*complain=*/tf_error);
10345       else
10346         type = TREE_TYPE (decl);
10347     }
10348
10349   if (!type)
10350     {
10351       identifier = cp_parser_identifier (parser);
10352
10353       if (identifier == error_mark_node)
10354         {
10355           parser->scope = NULL_TREE;
10356           return error_mark_node;
10357         }
10358
10359       /* For a `typename', we needn't call xref_tag.  */
10360       if (tag_type == typename_type
10361           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10362         return cp_parser_make_typename_type (parser, parser->scope,
10363                                              identifier);
10364       /* Look up a qualified name in the usual way.  */
10365       if (parser->scope)
10366         {
10367           tree decl;
10368
10369           decl = cp_parser_lookup_name (parser, identifier,
10370                                         tag_type,
10371                                         /*is_template=*/false,
10372                                         /*is_namespace=*/false,
10373                                         /*check_dependency=*/true,
10374                                         /*ambiguous_decls=*/NULL);
10375
10376           /* If we are parsing friend declaration, DECL may be a
10377              TEMPLATE_DECL tree node here.  However, we need to check
10378              whether this TEMPLATE_DECL results in valid code.  Consider
10379              the following example:
10380
10381                namespace N {
10382                  template <class T> class C {};
10383                }
10384                class X {
10385                  template <class T> friend class N::C; // #1, valid code
10386                };
10387                template <class T> class Y {
10388                  friend class N::C;                    // #2, invalid code
10389                };
10390
10391              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10392              name lookup of `N::C'.  We see that friend declaration must
10393              be template for the code to be valid.  Note that
10394              processing_template_decl does not work here since it is
10395              always 1 for the above two cases.  */
10396
10397           decl = (cp_parser_maybe_treat_template_as_class
10398                   (decl, /*tag_name_p=*/is_friend
10399                          && parser->num_template_parameter_lists));
10400
10401           if (TREE_CODE (decl) != TYPE_DECL)
10402             {
10403               cp_parser_diagnose_invalid_type_name (parser,
10404                                                     parser->scope,
10405                                                     identifier);
10406               return error_mark_node;
10407             }
10408
10409           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10410             {
10411               bool allow_template = (parser->num_template_parameter_lists
10412                                       || DECL_SELF_REFERENCE_P (decl));
10413               type = check_elaborated_type_specifier (tag_type, decl, 
10414                                                       allow_template);
10415
10416               if (type == error_mark_node)
10417                 return error_mark_node;
10418             }
10419
10420           type = TREE_TYPE (decl);
10421         }
10422       else
10423         {
10424           /* An elaborated-type-specifier sometimes introduces a new type and
10425              sometimes names an existing type.  Normally, the rule is that it
10426              introduces a new type only if there is not an existing type of
10427              the same name already in scope.  For example, given:
10428
10429                struct S {};
10430                void f() { struct S s; }
10431
10432              the `struct S' in the body of `f' is the same `struct S' as in
10433              the global scope; the existing definition is used.  However, if
10434              there were no global declaration, this would introduce a new
10435              local class named `S'.
10436
10437              An exception to this rule applies to the following code:
10438
10439                namespace N { struct S; }
10440
10441              Here, the elaborated-type-specifier names a new type
10442              unconditionally; even if there is already an `S' in the
10443              containing scope this declaration names a new type.
10444              This exception only applies if the elaborated-type-specifier
10445              forms the complete declaration:
10446
10447                [class.name]
10448
10449                A declaration consisting solely of `class-key identifier ;' is
10450                either a redeclaration of the name in the current scope or a
10451                forward declaration of the identifier as a class name.  It
10452                introduces the name into the current scope.
10453
10454              We are in this situation precisely when the next token is a `;'.
10455
10456              An exception to the exception is that a `friend' declaration does
10457              *not* name a new type; i.e., given:
10458
10459                struct S { friend struct T; };
10460
10461              `T' is not a new type in the scope of `S'.
10462
10463              Also, `new struct S' or `sizeof (struct S)' never results in the
10464              definition of a new type; a new type can only be declared in a
10465              declaration context.  */
10466
10467           tag_scope ts;
10468           bool template_p;
10469
10470           if (is_friend)
10471             /* Friends have special name lookup rules.  */
10472             ts = ts_within_enclosing_non_class;
10473           else if (is_declaration
10474                    && cp_lexer_next_token_is (parser->lexer,
10475                                               CPP_SEMICOLON))
10476             /* This is a `class-key identifier ;' */
10477             ts = ts_current;
10478           else
10479             ts = ts_global;
10480
10481           template_p =
10482             (parser->num_template_parameter_lists
10483              && (cp_parser_next_token_starts_class_definition_p (parser)
10484                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10485           /* An unqualified name was used to reference this type, so
10486              there were no qualifying templates.  */
10487           if (!cp_parser_check_template_parameters (parser,
10488                                                     /*num_templates=*/0))
10489             return error_mark_node;
10490           type = xref_tag (tag_type, identifier, ts, template_p);
10491         }
10492     }
10493
10494   if (type == error_mark_node)
10495     return error_mark_node;
10496
10497   /* Allow attributes on forward declarations of classes.  */
10498   if (attributes)
10499     {
10500       if (TREE_CODE (type) == TYPENAME_TYPE)
10501         warning (OPT_Wattributes,
10502                  "attributes ignored on uninstantiated type");
10503       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10504                && ! processing_explicit_instantiation)
10505         warning (OPT_Wattributes,
10506                  "attributes ignored on template instantiation");
10507       else if (is_declaration && cp_parser_declares_only_class_p (parser))
10508         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10509       else
10510         warning (OPT_Wattributes,
10511                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10512     }
10513
10514   if (tag_type != enum_type)
10515     cp_parser_check_class_key (tag_type, type);
10516
10517   /* A "<" cannot follow an elaborated type specifier.  If that
10518      happens, the user was probably trying to form a template-id.  */
10519   cp_parser_check_for_invalid_template_id (parser, type);
10520
10521   return type;
10522 }
10523
10524 /* Parse an enum-specifier.
10525
10526    enum-specifier:
10527      enum identifier [opt] { enumerator-list [opt] }
10528
10529    GNU Extensions:
10530      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10531        attributes[opt]
10532
10533    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10534    if the token stream isn't an enum-specifier after all.  */
10535
10536 static tree
10537 cp_parser_enum_specifier (cp_parser* parser)
10538 {
10539   tree identifier;
10540   tree type;
10541   tree attributes;
10542
10543   /* Parse tentatively so that we can back up if we don't find a
10544      enum-specifier.  */
10545   cp_parser_parse_tentatively (parser);
10546
10547   /* Caller guarantees that the current token is 'enum', an identifier
10548      possibly follows, and the token after that is an opening brace.
10549      If we don't have an identifier, fabricate an anonymous name for
10550      the enumeration being defined.  */
10551   cp_lexer_consume_token (parser->lexer);
10552
10553   attributes = cp_parser_attributes_opt (parser);
10554
10555   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10556     identifier = cp_parser_identifier (parser);
10557   else
10558     identifier = make_anon_name ();
10559
10560   /* Look for the `{' but don't consume it yet.  */
10561   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10562     cp_parser_simulate_error (parser);
10563
10564   if (!cp_parser_parse_definitely (parser))
10565     return NULL_TREE;
10566
10567   /* Issue an error message if type-definitions are forbidden here.  */
10568   if (!cp_parser_check_type_definition (parser))
10569     type = error_mark_node;
10570   else
10571     /* Create the new type.  We do this before consuming the opening
10572        brace so the enum will be recorded as being on the line of its
10573        tag (or the 'enum' keyword, if there is no tag).  */
10574     type = start_enum (identifier);
10575   
10576   /* Consume the opening brace.  */
10577   cp_lexer_consume_token (parser->lexer);
10578
10579   if (type == error_mark_node)
10580     {
10581       cp_parser_skip_to_end_of_block_or_statement (parser);
10582       return error_mark_node;
10583     }
10584
10585   /* If the next token is not '}', then there are some enumerators.  */
10586   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10587     cp_parser_enumerator_list (parser, type);
10588
10589   /* Consume the final '}'.  */
10590   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10591
10592   /* Look for trailing attributes to apply to this enumeration, and
10593      apply them if appropriate.  */
10594   if (cp_parser_allow_gnu_extensions_p (parser))
10595     {
10596       tree trailing_attr = cp_parser_attributes_opt (parser);
10597       cplus_decl_attributes (&type,
10598                              trailing_attr,
10599                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10600     }
10601
10602   /* Finish up the enumeration.  */
10603   finish_enum (type);
10604
10605   return type;
10606 }
10607
10608 /* Parse an enumerator-list.  The enumerators all have the indicated
10609    TYPE.
10610
10611    enumerator-list:
10612      enumerator-definition
10613      enumerator-list , enumerator-definition  */
10614
10615 static void
10616 cp_parser_enumerator_list (cp_parser* parser, tree type)
10617 {
10618   while (true)
10619     {
10620       /* Parse an enumerator-definition.  */
10621       cp_parser_enumerator_definition (parser, type);
10622
10623       /* If the next token is not a ',', we've reached the end of
10624          the list.  */
10625       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10626         break;
10627       /* Otherwise, consume the `,' and keep going.  */
10628       cp_lexer_consume_token (parser->lexer);
10629       /* If the next token is a `}', there is a trailing comma.  */
10630       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10631         {
10632           if (pedantic && !in_system_header)
10633             pedwarn ("comma at end of enumerator list");
10634           break;
10635         }
10636     }
10637 }
10638
10639 /* Parse an enumerator-definition.  The enumerator has the indicated
10640    TYPE.
10641
10642    enumerator-definition:
10643      enumerator
10644      enumerator = constant-expression
10645
10646    enumerator:
10647      identifier  */
10648
10649 static void
10650 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10651 {
10652   tree identifier;
10653   tree value;
10654
10655   /* Look for the identifier.  */
10656   identifier = cp_parser_identifier (parser);
10657   if (identifier == error_mark_node)
10658     return;
10659
10660   /* If the next token is an '=', then there is an explicit value.  */
10661   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10662     {
10663       /* Consume the `=' token.  */
10664       cp_lexer_consume_token (parser->lexer);
10665       /* Parse the value.  */
10666       value = cp_parser_constant_expression (parser,
10667                                              /*allow_non_constant_p=*/false,
10668                                              NULL);
10669     }
10670   else
10671     value = NULL_TREE;
10672
10673   /* Create the enumerator.  */
10674   build_enumerator (identifier, value, type);
10675 }
10676
10677 /* Parse a namespace-name.
10678
10679    namespace-name:
10680      original-namespace-name
10681      namespace-alias
10682
10683    Returns the NAMESPACE_DECL for the namespace.  */
10684
10685 static tree
10686 cp_parser_namespace_name (cp_parser* parser)
10687 {
10688   tree identifier;
10689   tree namespace_decl;
10690
10691   /* Get the name of the namespace.  */
10692   identifier = cp_parser_identifier (parser);
10693   if (identifier == error_mark_node)
10694     return error_mark_node;
10695
10696   /* Look up the identifier in the currently active scope.  Look only
10697      for namespaces, due to:
10698
10699        [basic.lookup.udir]
10700
10701        When looking up a namespace-name in a using-directive or alias
10702        definition, only namespace names are considered.
10703
10704      And:
10705
10706        [basic.lookup.qual]
10707
10708        During the lookup of a name preceding the :: scope resolution
10709        operator, object, function, and enumerator names are ignored.
10710
10711      (Note that cp_parser_class_or_namespace_name only calls this
10712      function if the token after the name is the scope resolution
10713      operator.)  */
10714   namespace_decl = cp_parser_lookup_name (parser, identifier,
10715                                           none_type,
10716                                           /*is_template=*/false,
10717                                           /*is_namespace=*/true,
10718                                           /*check_dependency=*/true,
10719                                           /*ambiguous_decls=*/NULL);
10720   /* If it's not a namespace, issue an error.  */
10721   if (namespace_decl == error_mark_node
10722       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10723     {
10724       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10725         error ("%qD is not a namespace-name", identifier);
10726       cp_parser_error (parser, "expected namespace-name");
10727       namespace_decl = error_mark_node;
10728     }
10729
10730   return namespace_decl;
10731 }
10732
10733 /* Parse a namespace-definition.
10734
10735    namespace-definition:
10736      named-namespace-definition
10737      unnamed-namespace-definition
10738
10739    named-namespace-definition:
10740      original-namespace-definition
10741      extension-namespace-definition
10742
10743    original-namespace-definition:
10744      namespace identifier { namespace-body }
10745
10746    extension-namespace-definition:
10747      namespace original-namespace-name { namespace-body }
10748
10749    unnamed-namespace-definition:
10750      namespace { namespace-body } */
10751
10752 static void
10753 cp_parser_namespace_definition (cp_parser* parser)
10754 {
10755   tree identifier, attribs;
10756
10757   /* Look for the `namespace' keyword.  */
10758   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10759
10760   /* Get the name of the namespace.  We do not attempt to distinguish
10761      between an original-namespace-definition and an
10762      extension-namespace-definition at this point.  The semantic
10763      analysis routines are responsible for that.  */
10764   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10765     identifier = cp_parser_identifier (parser);
10766   else
10767     identifier = NULL_TREE;
10768
10769   /* Parse any specified attributes.  */
10770   attribs = cp_parser_attributes_opt (parser);
10771
10772   /* Look for the `{' to start the namespace.  */
10773   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10774   /* Start the namespace.  */
10775   push_namespace_with_attribs (identifier, attribs);
10776   /* Parse the body of the namespace.  */
10777   cp_parser_namespace_body (parser);
10778   /* Finish the namespace.  */
10779   pop_namespace ();
10780   /* Look for the final `}'.  */
10781   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10782 }
10783
10784 /* Parse a namespace-body.
10785
10786    namespace-body:
10787      declaration-seq [opt]  */
10788
10789 static void
10790 cp_parser_namespace_body (cp_parser* parser)
10791 {
10792   cp_parser_declaration_seq_opt (parser);
10793 }
10794
10795 /* Parse a namespace-alias-definition.
10796
10797    namespace-alias-definition:
10798      namespace identifier = qualified-namespace-specifier ;  */
10799
10800 static void
10801 cp_parser_namespace_alias_definition (cp_parser* parser)
10802 {
10803   tree identifier;
10804   tree namespace_specifier;
10805
10806   /* Look for the `namespace' keyword.  */
10807   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10808   /* Look for the identifier.  */
10809   identifier = cp_parser_identifier (parser);
10810   if (identifier == error_mark_node)
10811     return;
10812   /* Look for the `=' token.  */
10813   cp_parser_require (parser, CPP_EQ, "`='");
10814   /* Look for the qualified-namespace-specifier.  */
10815   namespace_specifier
10816     = cp_parser_qualified_namespace_specifier (parser);
10817   /* Look for the `;' token.  */
10818   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10819
10820   /* Register the alias in the symbol table.  */
10821   do_namespace_alias (identifier, namespace_specifier);
10822 }
10823
10824 /* Parse a qualified-namespace-specifier.
10825
10826    qualified-namespace-specifier:
10827      :: [opt] nested-name-specifier [opt] namespace-name
10828
10829    Returns a NAMESPACE_DECL corresponding to the specified
10830    namespace.  */
10831
10832 static tree
10833 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10834 {
10835   /* Look for the optional `::'.  */
10836   cp_parser_global_scope_opt (parser,
10837                               /*current_scope_valid_p=*/false);
10838
10839   /* Look for the optional nested-name-specifier.  */
10840   cp_parser_nested_name_specifier_opt (parser,
10841                                        /*typename_keyword_p=*/false,
10842                                        /*check_dependency_p=*/true,
10843                                        /*type_p=*/false,
10844                                        /*is_declaration=*/true);
10845
10846   return cp_parser_namespace_name (parser);
10847 }
10848
10849 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10850    access declaration.
10851
10852    using-declaration:
10853      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10854      using :: unqualified-id ;  
10855
10856    access-declaration:
10857      qualified-id ;  
10858
10859    */
10860
10861 static bool
10862 cp_parser_using_declaration (cp_parser* parser, 
10863                              bool access_declaration_p)
10864 {
10865   cp_token *token;
10866   bool typename_p = false;
10867   bool global_scope_p;
10868   tree decl;
10869   tree identifier;
10870   tree qscope;
10871
10872   if (access_declaration_p)
10873     cp_parser_parse_tentatively (parser);
10874   else
10875     {
10876       /* Look for the `using' keyword.  */
10877       cp_parser_require_keyword (parser, RID_USING, "`using'");
10878       
10879       /* Peek at the next token.  */
10880       token = cp_lexer_peek_token (parser->lexer);
10881       /* See if it's `typename'.  */
10882       if (token->keyword == RID_TYPENAME)
10883         {
10884           /* Remember that we've seen it.  */
10885           typename_p = true;
10886           /* Consume the `typename' token.  */
10887           cp_lexer_consume_token (parser->lexer);
10888         }
10889     }
10890
10891   /* Look for the optional global scope qualification.  */
10892   global_scope_p
10893     = (cp_parser_global_scope_opt (parser,
10894                                    /*current_scope_valid_p=*/false)
10895        != NULL_TREE);
10896
10897   /* If we saw `typename', or didn't see `::', then there must be a
10898      nested-name-specifier present.  */
10899   if (typename_p || !global_scope_p)
10900     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10901                                               /*check_dependency_p=*/true,
10902                                               /*type_p=*/false,
10903                                               /*is_declaration=*/true);
10904   /* Otherwise, we could be in either of the two productions.  In that
10905      case, treat the nested-name-specifier as optional.  */
10906   else
10907     qscope = cp_parser_nested_name_specifier_opt (parser,
10908                                                   /*typename_keyword_p=*/false,
10909                                                   /*check_dependency_p=*/true,
10910                                                   /*type_p=*/false,
10911                                                   /*is_declaration=*/true);
10912   if (!qscope)
10913     qscope = global_namespace;
10914
10915   if (access_declaration_p && cp_parser_error_occurred (parser))
10916     /* Something has already gone wrong; there's no need to parse
10917        further.  Since an error has occurred, the return value of
10918        cp_parser_parse_definitely will be false, as required.  */
10919     return cp_parser_parse_definitely (parser);
10920
10921   /* Parse the unqualified-id.  */
10922   identifier = cp_parser_unqualified_id (parser,
10923                                          /*template_keyword_p=*/false,
10924                                          /*check_dependency_p=*/true,
10925                                          /*declarator_p=*/true,
10926                                          /*optional_p=*/false);
10927
10928   if (access_declaration_p)
10929     {
10930       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10931         cp_parser_simulate_error (parser);
10932       if (!cp_parser_parse_definitely (parser))
10933         return false;
10934     }
10935
10936   /* The function we call to handle a using-declaration is different
10937      depending on what scope we are in.  */
10938   if (qscope == error_mark_node || identifier == error_mark_node)
10939     ;
10940   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10941            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10942     /* [namespace.udecl]
10943
10944        A using declaration shall not name a template-id.  */
10945     error ("a template-id may not appear in a using-declaration");
10946   else
10947     {
10948       if (at_class_scope_p ())
10949         {
10950           /* Create the USING_DECL.  */
10951           decl = do_class_using_decl (parser->scope, identifier);
10952           /* Add it to the list of members in this class.  */
10953           finish_member_declaration (decl);
10954         }
10955       else
10956         {
10957           decl = cp_parser_lookup_name_simple (parser, identifier);
10958           if (decl == error_mark_node)
10959             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10960           else if (!at_namespace_scope_p ())
10961             do_local_using_decl (decl, qscope, identifier);
10962           else
10963             do_toplevel_using_decl (decl, qscope, identifier);
10964         }
10965     }
10966
10967   /* Look for the final `;'.  */
10968   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10969   
10970   return true;
10971 }
10972
10973 /* Parse a using-directive.
10974
10975    using-directive:
10976      using namespace :: [opt] nested-name-specifier [opt]
10977        namespace-name ;  */
10978
10979 static void
10980 cp_parser_using_directive (cp_parser* parser)
10981 {
10982   tree namespace_decl;
10983   tree attribs;
10984
10985   /* Look for the `using' keyword.  */
10986   cp_parser_require_keyword (parser, RID_USING, "`using'");
10987   /* And the `namespace' keyword.  */
10988   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10989   /* Look for the optional `::' operator.  */
10990   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10991   /* And the optional nested-name-specifier.  */
10992   cp_parser_nested_name_specifier_opt (parser,
10993                                        /*typename_keyword_p=*/false,
10994                                        /*check_dependency_p=*/true,
10995                                        /*type_p=*/false,
10996                                        /*is_declaration=*/true);
10997   /* Get the namespace being used.  */
10998   namespace_decl = cp_parser_namespace_name (parser);
10999   /* And any specified attributes.  */
11000   attribs = cp_parser_attributes_opt (parser);
11001   /* Update the symbol table.  */
11002   parse_using_directive (namespace_decl, attribs);
11003   /* Look for the final `;'.  */
11004   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11005 }
11006
11007 /* Parse an asm-definition.
11008
11009    asm-definition:
11010      asm ( string-literal ) ;
11011
11012    GNU Extension:
11013
11014    asm-definition:
11015      asm volatile [opt] ( string-literal ) ;
11016      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11017      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11018                           : asm-operand-list [opt] ) ;
11019      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11020                           : asm-operand-list [opt]
11021                           : asm-operand-list [opt] ) ;  */
11022
11023 static void
11024 cp_parser_asm_definition (cp_parser* parser)
11025 {
11026   tree string;
11027   tree outputs = NULL_TREE;
11028   tree inputs = NULL_TREE;
11029   tree clobbers = NULL_TREE;
11030   tree asm_stmt;
11031   bool volatile_p = false;
11032   bool extended_p = false;
11033
11034   /* Look for the `asm' keyword.  */
11035   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11036   /* See if the next token is `volatile'.  */
11037   if (cp_parser_allow_gnu_extensions_p (parser)
11038       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11039     {
11040       /* Remember that we saw the `volatile' keyword.  */
11041       volatile_p = true;
11042       /* Consume the token.  */
11043       cp_lexer_consume_token (parser->lexer);
11044     }
11045   /* Look for the opening `('.  */
11046   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11047     return;
11048   /* Look for the string.  */
11049   string = cp_parser_string_literal (parser, false, false);
11050   if (string == error_mark_node)
11051     {
11052       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11053                                              /*consume_paren=*/true);
11054       return;
11055     }
11056
11057   /* If we're allowing GNU extensions, check for the extended assembly
11058      syntax.  Unfortunately, the `:' tokens need not be separated by
11059      a space in C, and so, for compatibility, we tolerate that here
11060      too.  Doing that means that we have to treat the `::' operator as
11061      two `:' tokens.  */
11062   if (cp_parser_allow_gnu_extensions_p (parser)
11063       && parser->in_function_body
11064       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11065           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11066     {
11067       bool inputs_p = false;
11068       bool clobbers_p = false;
11069
11070       /* The extended syntax was used.  */
11071       extended_p = true;
11072
11073       /* Look for outputs.  */
11074       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11075         {
11076           /* Consume the `:'.  */
11077           cp_lexer_consume_token (parser->lexer);
11078           /* Parse the output-operands.  */
11079           if (cp_lexer_next_token_is_not (parser->lexer,
11080                                           CPP_COLON)
11081               && cp_lexer_next_token_is_not (parser->lexer,
11082                                              CPP_SCOPE)
11083               && cp_lexer_next_token_is_not (parser->lexer,
11084                                              CPP_CLOSE_PAREN))
11085             outputs = cp_parser_asm_operand_list (parser);
11086         }
11087       /* If the next token is `::', there are no outputs, and the
11088          next token is the beginning of the inputs.  */
11089       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11090         /* The inputs are coming next.  */
11091         inputs_p = true;
11092
11093       /* Look for inputs.  */
11094       if (inputs_p
11095           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11096         {
11097           /* Consume the `:' or `::'.  */
11098           cp_lexer_consume_token (parser->lexer);
11099           /* Parse the output-operands.  */
11100           if (cp_lexer_next_token_is_not (parser->lexer,
11101                                           CPP_COLON)
11102               && cp_lexer_next_token_is_not (parser->lexer,
11103                                              CPP_CLOSE_PAREN))
11104             inputs = cp_parser_asm_operand_list (parser);
11105         }
11106       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11107         /* The clobbers are coming next.  */
11108         clobbers_p = true;
11109
11110       /* Look for clobbers.  */
11111       if (clobbers_p
11112           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11113         {
11114           /* Consume the `:' or `::'.  */
11115           cp_lexer_consume_token (parser->lexer);
11116           /* Parse the clobbers.  */
11117           if (cp_lexer_next_token_is_not (parser->lexer,
11118                                           CPP_CLOSE_PAREN))
11119             clobbers = cp_parser_asm_clobber_list (parser);
11120         }
11121     }
11122   /* Look for the closing `)'.  */
11123   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11124     cp_parser_skip_to_closing_parenthesis (parser, true, false,
11125                                            /*consume_paren=*/true);
11126   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11127
11128   /* Create the ASM_EXPR.  */
11129   if (parser->in_function_body)
11130     {
11131       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11132                                   inputs, clobbers);
11133       /* If the extended syntax was not used, mark the ASM_EXPR.  */
11134       if (!extended_p)
11135         {
11136           tree temp = asm_stmt;
11137           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11138             temp = TREE_OPERAND (temp, 0);
11139
11140           ASM_INPUT_P (temp) = 1;
11141         }
11142     }
11143   else
11144     cgraph_add_asm_node (string);
11145 }
11146
11147 /* Declarators [gram.dcl.decl] */
11148
11149 /* Parse an init-declarator.
11150
11151    init-declarator:
11152      declarator initializer [opt]
11153
11154    GNU Extension:
11155
11156    init-declarator:
11157      declarator asm-specification [opt] attributes [opt] initializer [opt]
11158
11159    function-definition:
11160      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11161        function-body
11162      decl-specifier-seq [opt] declarator function-try-block
11163
11164    GNU Extension:
11165
11166    function-definition:
11167      __extension__ function-definition
11168
11169    The DECL_SPECIFIERS apply to this declarator.  Returns a
11170    representation of the entity declared.  If MEMBER_P is TRUE, then
11171    this declarator appears in a class scope.  The new DECL created by
11172    this declarator is returned.
11173
11174    The CHECKS are access checks that should be performed once we know
11175    what entity is being declared (and, therefore, what classes have
11176    befriended it).
11177
11178    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11179    for a function-definition here as well.  If the declarator is a
11180    declarator for a function-definition, *FUNCTION_DEFINITION_P will
11181    be TRUE upon return.  By that point, the function-definition will
11182    have been completely parsed.
11183
11184    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11185    is FALSE.  */
11186
11187 static tree
11188 cp_parser_init_declarator (cp_parser* parser,
11189                            cp_decl_specifier_seq *decl_specifiers,
11190                            VEC (deferred_access_check,gc)* checks,
11191                            bool function_definition_allowed_p,
11192                            bool member_p,
11193                            int declares_class_or_enum,
11194                            bool* function_definition_p)
11195 {
11196   cp_token *token;
11197   cp_declarator *declarator;
11198   tree prefix_attributes;
11199   tree attributes;
11200   tree asm_specification;
11201   tree initializer;
11202   tree decl = NULL_TREE;
11203   tree scope;
11204   bool is_initialized;
11205   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
11206      initialized with "= ..", CPP_OPEN_PAREN if initialized with
11207      "(...)".  */
11208   enum cpp_ttype initialization_kind;
11209   bool is_parenthesized_init = false;
11210   bool is_non_constant_init;
11211   int ctor_dtor_or_conv_p;
11212   bool friend_p;
11213   tree pushed_scope = NULL;
11214
11215   /* Gather the attributes that were provided with the
11216      decl-specifiers.  */
11217   prefix_attributes = decl_specifiers->attributes;
11218
11219   /* Assume that this is not the declarator for a function
11220      definition.  */
11221   if (function_definition_p)
11222     *function_definition_p = false;
11223
11224   /* Defer access checks while parsing the declarator; we cannot know
11225      what names are accessible until we know what is being
11226      declared.  */
11227   resume_deferring_access_checks ();
11228
11229   /* Parse the declarator.  */
11230   declarator
11231     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11232                             &ctor_dtor_or_conv_p,
11233                             /*parenthesized_p=*/NULL,
11234                             /*member_p=*/false);
11235   /* Gather up the deferred checks.  */
11236   stop_deferring_access_checks ();
11237
11238   /* If the DECLARATOR was erroneous, there's no need to go
11239      further.  */
11240   if (declarator == cp_error_declarator)
11241     return error_mark_node;
11242
11243   /* Check that the number of template-parameter-lists is OK.  */
11244   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11245     return error_mark_node;
11246
11247   if (declares_class_or_enum & 2)
11248     cp_parser_check_for_definition_in_return_type (declarator,
11249                                                    decl_specifiers->type);
11250
11251   /* Figure out what scope the entity declared by the DECLARATOR is
11252      located in.  `grokdeclarator' sometimes changes the scope, so
11253      we compute it now.  */
11254   scope = get_scope_of_declarator (declarator);
11255
11256   /* If we're allowing GNU extensions, look for an asm-specification
11257      and attributes.  */
11258   if (cp_parser_allow_gnu_extensions_p (parser))
11259     {
11260       /* Look for an asm-specification.  */
11261       asm_specification = cp_parser_asm_specification_opt (parser);
11262       /* And attributes.  */
11263       attributes = cp_parser_attributes_opt (parser);
11264     }
11265   else
11266     {
11267       asm_specification = NULL_TREE;
11268       attributes = NULL_TREE;
11269     }
11270
11271   /* Peek at the next token.  */
11272   token = cp_lexer_peek_token (parser->lexer);
11273   /* Check to see if the token indicates the start of a
11274      function-definition.  */
11275   if (cp_parser_token_starts_function_definition_p (token))
11276     {
11277       if (!function_definition_allowed_p)
11278         {
11279           /* If a function-definition should not appear here, issue an
11280              error message.  */
11281           cp_parser_error (parser,
11282                            "a function-definition is not allowed here");
11283           return error_mark_node;
11284         }
11285       else
11286         {
11287           /* Neither attributes nor an asm-specification are allowed
11288              on a function-definition.  */
11289           if (asm_specification)
11290             error ("an asm-specification is not allowed on a function-definition");
11291           if (attributes)
11292             error ("attributes are not allowed on a function-definition");
11293           /* This is a function-definition.  */
11294           *function_definition_p = true;
11295
11296           /* Parse the function definition.  */
11297           if (member_p)
11298             decl = cp_parser_save_member_function_body (parser,
11299                                                         decl_specifiers,
11300                                                         declarator,
11301                                                         prefix_attributes);
11302           else
11303             decl
11304               = (cp_parser_function_definition_from_specifiers_and_declarator
11305                  (parser, decl_specifiers, prefix_attributes, declarator));
11306
11307           return decl;
11308         }
11309     }
11310
11311   /* [dcl.dcl]
11312
11313      Only in function declarations for constructors, destructors, and
11314      type conversions can the decl-specifier-seq be omitted.
11315
11316      We explicitly postpone this check past the point where we handle
11317      function-definitions because we tolerate function-definitions
11318      that are missing their return types in some modes.  */
11319   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11320     {
11321       cp_parser_error (parser,
11322                        "expected constructor, destructor, or type conversion");
11323       return error_mark_node;
11324     }
11325
11326   /* An `=' or an `(' indicates an initializer.  */
11327   if (token->type == CPP_EQ
11328       || token->type == CPP_OPEN_PAREN)
11329     {
11330       is_initialized = true;
11331       initialization_kind = token->type;
11332     }
11333   else
11334     {
11335       /* If the init-declarator isn't initialized and isn't followed by a
11336          `,' or `;', it's not a valid init-declarator.  */
11337       if (token->type != CPP_COMMA
11338           && token->type != CPP_SEMICOLON)
11339         {
11340           cp_parser_error (parser, "expected initializer");
11341           return error_mark_node;
11342         }
11343       is_initialized = false;
11344       initialization_kind = CPP_EOF;
11345     }
11346
11347   /* Because start_decl has side-effects, we should only call it if we
11348      know we're going ahead.  By this point, we know that we cannot
11349      possibly be looking at any other construct.  */
11350   cp_parser_commit_to_tentative_parse (parser);
11351
11352   /* If the decl specifiers were bad, issue an error now that we're
11353      sure this was intended to be a declarator.  Then continue
11354      declaring the variable(s), as int, to try to cut down on further
11355      errors.  */
11356   if (decl_specifiers->any_specifiers_p
11357       && decl_specifiers->type == error_mark_node)
11358     {
11359       cp_parser_error (parser, "invalid type in declaration");
11360       decl_specifiers->type = integer_type_node;
11361     }
11362
11363   /* Check to see whether or not this declaration is a friend.  */
11364   friend_p = cp_parser_friend_p (decl_specifiers);
11365
11366   /* Enter the newly declared entry in the symbol table.  If we're
11367      processing a declaration in a class-specifier, we wait until
11368      after processing the initializer.  */
11369   if (!member_p)
11370     {
11371       if (parser->in_unbraced_linkage_specification_p)
11372         decl_specifiers->storage_class = sc_extern;
11373       decl = start_decl (declarator, decl_specifiers,
11374                          is_initialized, attributes, prefix_attributes,
11375                          &pushed_scope);
11376     }
11377   else if (scope)
11378     /* Enter the SCOPE.  That way unqualified names appearing in the
11379        initializer will be looked up in SCOPE.  */
11380     pushed_scope = push_scope (scope);
11381
11382   /* Perform deferred access control checks, now that we know in which
11383      SCOPE the declared entity resides.  */
11384   if (!member_p && decl)
11385     {
11386       tree saved_current_function_decl = NULL_TREE;
11387
11388       /* If the entity being declared is a function, pretend that we
11389          are in its scope.  If it is a `friend', it may have access to
11390          things that would not otherwise be accessible.  */
11391       if (TREE_CODE (decl) == FUNCTION_DECL)
11392         {
11393           saved_current_function_decl = current_function_decl;
11394           current_function_decl = decl;
11395         }
11396
11397       /* Perform access checks for template parameters.  */
11398       cp_parser_perform_template_parameter_access_checks (checks);
11399
11400       /* Perform the access control checks for the declarator and the
11401          the decl-specifiers.  */
11402       perform_deferred_access_checks ();
11403
11404       /* Restore the saved value.  */
11405       if (TREE_CODE (decl) == FUNCTION_DECL)
11406         current_function_decl = saved_current_function_decl;
11407     }
11408
11409   /* Parse the initializer.  */
11410   initializer = NULL_TREE;
11411   is_parenthesized_init = false;
11412   is_non_constant_init = true;
11413   if (is_initialized)
11414     {
11415       if (function_declarator_p (declarator))
11416         {
11417            if (initialization_kind == CPP_EQ)
11418              initializer = cp_parser_pure_specifier (parser);
11419            else
11420              {
11421                /* If the declaration was erroneous, we don't really
11422                   know what the user intended, so just silently
11423                   consume the initializer.  */
11424                if (decl != error_mark_node)
11425                  error ("initializer provided for function");
11426                cp_parser_skip_to_closing_parenthesis (parser,
11427                                                       /*recovering=*/true,
11428                                                       /*or_comma=*/false,
11429                                                       /*consume_paren=*/true);
11430              }
11431         }
11432       else
11433         initializer = cp_parser_initializer (parser,
11434                                              &is_parenthesized_init,
11435                                              &is_non_constant_init);
11436     }
11437
11438   /* The old parser allows attributes to appear after a parenthesized
11439      initializer.  Mark Mitchell proposed removing this functionality
11440      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11441      attributes -- but ignores them.  */
11442   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11443     if (cp_parser_attributes_opt (parser))
11444       warning (OPT_Wattributes,
11445                "attributes after parenthesized initializer ignored");
11446
11447   /* For an in-class declaration, use `grokfield' to create the
11448      declaration.  */
11449   if (member_p)
11450     {
11451       if (pushed_scope)
11452         {
11453           pop_scope (pushed_scope);
11454           pushed_scope = false;
11455         }
11456       decl = grokfield (declarator, decl_specifiers,
11457                         initializer, !is_non_constant_init,
11458                         /*asmspec=*/NULL_TREE,
11459                         prefix_attributes);
11460       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11461         cp_parser_save_default_args (parser, decl);
11462     }
11463
11464   /* Finish processing the declaration.  But, skip friend
11465      declarations.  */
11466   if (!friend_p && decl && decl != error_mark_node)
11467     {
11468       cp_finish_decl (decl,
11469                       initializer, !is_non_constant_init,
11470                       asm_specification,
11471                       /* If the initializer is in parentheses, then this is
11472                          a direct-initialization, which means that an
11473                          `explicit' constructor is OK.  Otherwise, an
11474                          `explicit' constructor cannot be used.  */
11475                       ((is_parenthesized_init || !is_initialized)
11476                      ? 0 : LOOKUP_ONLYCONVERTING));
11477     }
11478   if (!friend_p && pushed_scope)
11479     pop_scope (pushed_scope);
11480
11481   return decl;
11482 }
11483
11484 /* Parse a declarator.
11485
11486    declarator:
11487      direct-declarator
11488      ptr-operator declarator
11489
11490    abstract-declarator:
11491      ptr-operator abstract-declarator [opt]
11492      direct-abstract-declarator
11493
11494    GNU Extensions:
11495
11496    declarator:
11497      attributes [opt] direct-declarator
11498      attributes [opt] ptr-operator declarator
11499
11500    abstract-declarator:
11501      attributes [opt] ptr-operator abstract-declarator [opt]
11502      attributes [opt] direct-abstract-declarator
11503
11504    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11505    detect constructor, destructor or conversion operators. It is set
11506    to -1 if the declarator is a name, and +1 if it is a
11507    function. Otherwise it is set to zero. Usually you just want to
11508    test for >0, but internally the negative value is used.
11509
11510    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11511    a decl-specifier-seq unless it declares a constructor, destructor,
11512    or conversion.  It might seem that we could check this condition in
11513    semantic analysis, rather than parsing, but that makes it difficult
11514    to handle something like `f()'.  We want to notice that there are
11515    no decl-specifiers, and therefore realize that this is an
11516    expression, not a declaration.)
11517
11518    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11519    the declarator is a direct-declarator of the form "(...)".
11520
11521    MEMBER_P is true iff this declarator is a member-declarator.  */
11522
11523 static cp_declarator *
11524 cp_parser_declarator (cp_parser* parser,
11525                       cp_parser_declarator_kind dcl_kind,
11526                       int* ctor_dtor_or_conv_p,
11527                       bool* parenthesized_p,
11528                       bool member_p)
11529 {
11530   cp_token *token;
11531   cp_declarator *declarator;
11532   enum tree_code code;
11533   cp_cv_quals cv_quals;
11534   tree class_type;
11535   tree attributes = NULL_TREE;
11536
11537   /* Assume this is not a constructor, destructor, or type-conversion
11538      operator.  */
11539   if (ctor_dtor_or_conv_p)
11540     *ctor_dtor_or_conv_p = 0;
11541
11542   if (cp_parser_allow_gnu_extensions_p (parser))
11543     attributes = cp_parser_attributes_opt (parser);
11544
11545   /* Peek at the next token.  */
11546   token = cp_lexer_peek_token (parser->lexer);
11547
11548   /* Check for the ptr-operator production.  */
11549   cp_parser_parse_tentatively (parser);
11550   /* Parse the ptr-operator.  */
11551   code = cp_parser_ptr_operator (parser,
11552                                  &class_type,
11553                                  &cv_quals);
11554   /* If that worked, then we have a ptr-operator.  */
11555   if (cp_parser_parse_definitely (parser))
11556     {
11557       /* If a ptr-operator was found, then this declarator was not
11558          parenthesized.  */
11559       if (parenthesized_p)
11560         *parenthesized_p = true;
11561       /* The dependent declarator is optional if we are parsing an
11562          abstract-declarator.  */
11563       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11564         cp_parser_parse_tentatively (parser);
11565
11566       /* Parse the dependent declarator.  */
11567       declarator = cp_parser_declarator (parser, dcl_kind,
11568                                          /*ctor_dtor_or_conv_p=*/NULL,
11569                                          /*parenthesized_p=*/NULL,
11570                                          /*member_p=*/false);
11571
11572       /* If we are parsing an abstract-declarator, we must handle the
11573          case where the dependent declarator is absent.  */
11574       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11575           && !cp_parser_parse_definitely (parser))
11576         declarator = NULL;
11577
11578       /* Build the representation of the ptr-operator.  */
11579       if (class_type)
11580         declarator = make_ptrmem_declarator (cv_quals,
11581                                              class_type,
11582                                              declarator);
11583       else if (code == INDIRECT_REF)
11584         declarator = make_pointer_declarator (cv_quals, declarator);
11585       else
11586         declarator = make_reference_declarator (cv_quals, declarator);
11587     }
11588   /* Everything else is a direct-declarator.  */
11589   else
11590     {
11591       if (parenthesized_p)
11592         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11593                                                    CPP_OPEN_PAREN);
11594       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11595                                                 ctor_dtor_or_conv_p,
11596                                                 member_p);
11597     }
11598
11599   if (attributes && declarator && declarator != cp_error_declarator)
11600     declarator->attributes = attributes;
11601
11602   return declarator;
11603 }
11604
11605 /* Parse a direct-declarator or direct-abstract-declarator.
11606
11607    direct-declarator:
11608      declarator-id
11609      direct-declarator ( parameter-declaration-clause )
11610        cv-qualifier-seq [opt]
11611        exception-specification [opt]
11612      direct-declarator [ constant-expression [opt] ]
11613      ( declarator )
11614
11615    direct-abstract-declarator:
11616      direct-abstract-declarator [opt]
11617        ( parameter-declaration-clause )
11618        cv-qualifier-seq [opt]
11619        exception-specification [opt]
11620      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11621      ( abstract-declarator )
11622
11623    Returns a representation of the declarator.  DCL_KIND is
11624    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11625    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11626    we are parsing a direct-declarator.  It is
11627    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11628    of ambiguity we prefer an abstract declarator, as per
11629    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11630    cp_parser_declarator.  */
11631
11632 static cp_declarator *
11633 cp_parser_direct_declarator (cp_parser* parser,
11634                              cp_parser_declarator_kind dcl_kind,
11635                              int* ctor_dtor_or_conv_p,
11636                              bool member_p)
11637 {
11638   cp_token *token;
11639   cp_declarator *declarator = NULL;
11640   tree scope = NULL_TREE;
11641   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11642   bool saved_in_declarator_p = parser->in_declarator_p;
11643   bool first = true;
11644   tree pushed_scope = NULL_TREE;
11645
11646   while (true)
11647     {
11648       /* Peek at the next token.  */
11649       token = cp_lexer_peek_token (parser->lexer);
11650       if (token->type == CPP_OPEN_PAREN)
11651         {
11652           /* This is either a parameter-declaration-clause, or a
11653              parenthesized declarator. When we know we are parsing a
11654              named declarator, it must be a parenthesized declarator
11655              if FIRST is true. For instance, `(int)' is a
11656              parameter-declaration-clause, with an omitted
11657              direct-abstract-declarator. But `((*))', is a
11658              parenthesized abstract declarator. Finally, when T is a
11659              template parameter `(T)' is a
11660              parameter-declaration-clause, and not a parenthesized
11661              named declarator.
11662
11663              We first try and parse a parameter-declaration-clause,
11664              and then try a nested declarator (if FIRST is true).
11665
11666              It is not an error for it not to be a
11667              parameter-declaration-clause, even when FIRST is
11668              false. Consider,
11669
11670                int i (int);
11671                int i (3);
11672
11673              The first is the declaration of a function while the
11674              second is a the definition of a variable, including its
11675              initializer.
11676
11677              Having seen only the parenthesis, we cannot know which of
11678              these two alternatives should be selected.  Even more
11679              complex are examples like:
11680
11681                int i (int (a));
11682                int i (int (3));
11683
11684              The former is a function-declaration; the latter is a
11685              variable initialization.
11686
11687              Thus again, we try a parameter-declaration-clause, and if
11688              that fails, we back out and return.  */
11689
11690           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11691             {
11692               cp_parameter_declarator *params;
11693               unsigned saved_num_template_parameter_lists;
11694
11695               /* In a member-declarator, the only valid interpretation
11696                  of a parenthesis is the start of a
11697                  parameter-declaration-clause.  (It is invalid to
11698                  initialize a static data member with a parenthesized
11699                  initializer; only the "=" form of initialization is
11700                  permitted.)  */
11701               if (!member_p)
11702                 cp_parser_parse_tentatively (parser);
11703
11704               /* Consume the `('.  */
11705               cp_lexer_consume_token (parser->lexer);
11706               if (first)
11707                 {
11708                   /* If this is going to be an abstract declarator, we're
11709                      in a declarator and we can't have default args.  */
11710                   parser->default_arg_ok_p = false;
11711                   parser->in_declarator_p = true;
11712                 }
11713
11714               /* Inside the function parameter list, surrounding
11715                  template-parameter-lists do not apply.  */
11716               saved_num_template_parameter_lists
11717                 = parser->num_template_parameter_lists;
11718               parser->num_template_parameter_lists = 0;
11719
11720               /* Parse the parameter-declaration-clause.  */
11721               params = cp_parser_parameter_declaration_clause (parser);
11722
11723               parser->num_template_parameter_lists
11724                 = saved_num_template_parameter_lists;
11725
11726               /* If all went well, parse the cv-qualifier-seq and the
11727                  exception-specification.  */
11728               if (member_p || cp_parser_parse_definitely (parser))
11729                 {
11730                   cp_cv_quals cv_quals;
11731                   tree exception_specification;
11732
11733                   if (ctor_dtor_or_conv_p)
11734                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11735                   first = false;
11736                   /* Consume the `)'.  */
11737                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11738
11739                   /* Parse the cv-qualifier-seq.  */
11740                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11741                   /* And the exception-specification.  */
11742                   exception_specification
11743                     = cp_parser_exception_specification_opt (parser);
11744
11745                   /* Create the function-declarator.  */
11746                   declarator = make_call_declarator (declarator,
11747                                                      params,
11748                                                      cv_quals,
11749                                                      exception_specification);
11750                   /* Any subsequent parameter lists are to do with
11751                      return type, so are not those of the declared
11752                      function.  */
11753                   parser->default_arg_ok_p = false;
11754
11755                   /* Repeat the main loop.  */
11756                   continue;
11757                 }
11758             }
11759
11760           /* If this is the first, we can try a parenthesized
11761              declarator.  */
11762           if (first)
11763             {
11764               bool saved_in_type_id_in_expr_p;
11765
11766               parser->default_arg_ok_p = saved_default_arg_ok_p;
11767               parser->in_declarator_p = saved_in_declarator_p;
11768
11769               /* Consume the `('.  */
11770               cp_lexer_consume_token (parser->lexer);
11771               /* Parse the nested declarator.  */
11772               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11773               parser->in_type_id_in_expr_p = true;
11774               declarator
11775                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11776                                         /*parenthesized_p=*/NULL,
11777                                         member_p);
11778               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11779               first = false;
11780               /* Expect a `)'.  */
11781               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11782                 declarator = cp_error_declarator;
11783               if (declarator == cp_error_declarator)
11784                 break;
11785
11786               goto handle_declarator;
11787             }
11788           /* Otherwise, we must be done.  */
11789           else
11790             break;
11791         }
11792       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11793                && token->type == CPP_OPEN_SQUARE)
11794         {
11795           /* Parse an array-declarator.  */
11796           tree bounds;
11797
11798           if (ctor_dtor_or_conv_p)
11799             *ctor_dtor_or_conv_p = 0;
11800
11801           first = false;
11802           parser->default_arg_ok_p = false;
11803           parser->in_declarator_p = true;
11804           /* Consume the `['.  */
11805           cp_lexer_consume_token (parser->lexer);
11806           /* Peek at the next token.  */
11807           token = cp_lexer_peek_token (parser->lexer);
11808           /* If the next token is `]', then there is no
11809              constant-expression.  */
11810           if (token->type != CPP_CLOSE_SQUARE)
11811             {
11812               bool non_constant_p;
11813
11814               bounds
11815                 = cp_parser_constant_expression (parser,
11816                                                  /*allow_non_constant=*/true,
11817                                                  &non_constant_p);
11818               if (!non_constant_p)
11819                 bounds = fold_non_dependent_expr (bounds);
11820               /* Normally, the array bound must be an integral constant
11821                  expression.  However, as an extension, we allow VLAs
11822                  in function scopes.  */
11823               else if (!parser->in_function_body)
11824                 {
11825                   error ("array bound is not an integer constant");
11826                   bounds = error_mark_node;
11827                 }
11828             }
11829           else
11830             bounds = NULL_TREE;
11831           /* Look for the closing `]'.  */
11832           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11833             {
11834               declarator = cp_error_declarator;
11835               break;
11836             }
11837
11838           declarator = make_array_declarator (declarator, bounds);
11839         }
11840       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11841         {
11842           tree qualifying_scope;
11843           tree unqualified_name;
11844           special_function_kind sfk;
11845           bool abstract_ok;
11846
11847           /* Parse a declarator-id */
11848           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11849           if (abstract_ok)
11850             cp_parser_parse_tentatively (parser);
11851           unqualified_name
11852             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11853           qualifying_scope = parser->scope;
11854           if (abstract_ok)
11855             {
11856               if (!cp_parser_parse_definitely (parser))
11857                 unqualified_name = error_mark_node;
11858               else if (unqualified_name
11859                        && (qualifying_scope
11860                            || (TREE_CODE (unqualified_name)
11861                                != IDENTIFIER_NODE)))
11862                 {
11863                   cp_parser_error (parser, "expected unqualified-id");
11864                   unqualified_name = error_mark_node;
11865                 }
11866             }
11867
11868           if (!unqualified_name)
11869             return NULL;
11870           if (unqualified_name == error_mark_node)
11871             {
11872               declarator = cp_error_declarator;
11873               break;
11874             }
11875
11876           if (qualifying_scope && at_namespace_scope_p ()
11877               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11878             {
11879               /* In the declaration of a member of a template class
11880                  outside of the class itself, the SCOPE will sometimes
11881                  be a TYPENAME_TYPE.  For example, given:
11882
11883                  template <typename T>
11884                  int S<T>::R::i = 3;
11885
11886                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11887                  this context, we must resolve S<T>::R to an ordinary
11888                  type, rather than a typename type.
11889
11890                  The reason we normally avoid resolving TYPENAME_TYPEs
11891                  is that a specialization of `S' might render
11892                  `S<T>::R' not a type.  However, if `S' is
11893                  specialized, then this `i' will not be used, so there
11894                  is no harm in resolving the types here.  */
11895               tree type;
11896
11897               /* Resolve the TYPENAME_TYPE.  */
11898               type = resolve_typename_type (qualifying_scope,
11899                                             /*only_current_p=*/false);
11900               /* If that failed, the declarator is invalid.  */
11901               if (type == error_mark_node)
11902                 error ("%<%T::%D%> is not a type",
11903                        TYPE_CONTEXT (qualifying_scope),
11904                        TYPE_IDENTIFIER (qualifying_scope));
11905               qualifying_scope = type;
11906             }
11907
11908           sfk = sfk_none;
11909           if (unqualified_name)
11910             {
11911               tree class_type;
11912
11913               if (qualifying_scope
11914                   && CLASS_TYPE_P (qualifying_scope))
11915                 class_type = qualifying_scope;
11916               else
11917                 class_type = current_class_type;
11918
11919               if (TREE_CODE (unqualified_name) == TYPE_DECL)
11920                 {
11921                   tree name_type = TREE_TYPE (unqualified_name);
11922                   if (class_type && same_type_p (name_type, class_type))
11923                     {
11924                       if (qualifying_scope
11925                           && CLASSTYPE_USE_TEMPLATE (name_type))
11926                         {
11927                           error ("invalid use of constructor as a template");
11928                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11929                                   "name the constructor in a qualified name",
11930                                   class_type,
11931                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11932                                   class_type, name_type);
11933                           declarator = cp_error_declarator;
11934                           break;
11935                         }
11936                       else
11937                         unqualified_name = constructor_name (class_type);
11938                     }
11939                   else
11940                     {
11941                       /* We do not attempt to print the declarator
11942                          here because we do not have enough
11943                          information about its original syntactic
11944                          form.  */
11945                       cp_parser_error (parser, "invalid declarator");
11946                       declarator = cp_error_declarator;
11947                       break;
11948                     }
11949                 }
11950
11951               if (class_type)
11952                 {
11953                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11954                     sfk = sfk_destructor;
11955                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11956                     sfk = sfk_conversion;
11957                   else if (/* There's no way to declare a constructor
11958                               for an anonymous type, even if the type
11959                               got a name for linkage purposes.  */
11960                            !TYPE_WAS_ANONYMOUS (class_type)
11961                            && constructor_name_p (unqualified_name,
11962                                                   class_type))
11963                     {
11964                       unqualified_name = constructor_name (class_type);
11965                       sfk = sfk_constructor;
11966                     }
11967
11968                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
11969                     *ctor_dtor_or_conv_p = -1;
11970                 }
11971             }
11972           declarator = make_id_declarator (qualifying_scope,
11973                                            unqualified_name,
11974                                            sfk);
11975           declarator->id_loc = token->location;
11976
11977         handle_declarator:;
11978           scope = get_scope_of_declarator (declarator);
11979           if (scope)
11980             /* Any names that appear after the declarator-id for a
11981                member are looked up in the containing scope.  */
11982             pushed_scope = push_scope (scope);
11983           parser->in_declarator_p = true;
11984           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11985               || (declarator && declarator->kind == cdk_id))
11986             /* Default args are only allowed on function
11987                declarations.  */
11988             parser->default_arg_ok_p = saved_default_arg_ok_p;
11989           else
11990             parser->default_arg_ok_p = false;
11991
11992           first = false;
11993         }
11994       /* We're done.  */
11995       else
11996         break;
11997     }
11998
11999   /* For an abstract declarator, we might wind up with nothing at this
12000      point.  That's an error; the declarator is not optional.  */
12001   if (!declarator)
12002     cp_parser_error (parser, "expected declarator");
12003
12004   /* If we entered a scope, we must exit it now.  */
12005   if (pushed_scope)
12006     pop_scope (pushed_scope);
12007
12008   parser->default_arg_ok_p = saved_default_arg_ok_p;
12009   parser->in_declarator_p = saved_in_declarator_p;
12010
12011   return declarator;
12012 }
12013
12014 /* Parse a ptr-operator.
12015
12016    ptr-operator:
12017      * cv-qualifier-seq [opt]
12018      &
12019      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12020
12021    GNU Extension:
12022
12023    ptr-operator:
12024      & cv-qualifier-seq [opt]
12025
12026    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12027    Returns ADDR_EXPR if a reference was used.  In the case of a
12028    pointer-to-member, *TYPE is filled in with the TYPE containing the
12029    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
12030    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
12031    ERROR_MARK if an error occurred.  */
12032
12033 static enum tree_code
12034 cp_parser_ptr_operator (cp_parser* parser,
12035                         tree* type,
12036                         cp_cv_quals *cv_quals)
12037 {
12038   enum tree_code code = ERROR_MARK;
12039   cp_token *token;
12040
12041   /* Assume that it's not a pointer-to-member.  */
12042   *type = NULL_TREE;
12043   /* And that there are no cv-qualifiers.  */
12044   *cv_quals = TYPE_UNQUALIFIED;
12045
12046   /* Peek at the next token.  */
12047   token = cp_lexer_peek_token (parser->lexer);
12048   /* If it's a `*' or `&' we have a pointer or reference.  */
12049   if (token->type == CPP_MULT || token->type == CPP_AND)
12050     {
12051       /* Remember which ptr-operator we were processing.  */
12052       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12053
12054       /* Consume the `*' or `&'.  */
12055       cp_lexer_consume_token (parser->lexer);
12056
12057       /* A `*' can be followed by a cv-qualifier-seq, and so can a
12058          `&', if we are allowing GNU extensions.  (The only qualifier
12059          that can legally appear after `&' is `restrict', but that is
12060          enforced during semantic analysis.  */
12061       if (code == INDIRECT_REF
12062           || cp_parser_allow_gnu_extensions_p (parser))
12063         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12064     }
12065   else
12066     {
12067       /* Try the pointer-to-member case.  */
12068       cp_parser_parse_tentatively (parser);
12069       /* Look for the optional `::' operator.  */
12070       cp_parser_global_scope_opt (parser,
12071                                   /*current_scope_valid_p=*/false);
12072       /* Look for the nested-name specifier.  */
12073       cp_parser_nested_name_specifier (parser,
12074                                        /*typename_keyword_p=*/false,
12075                                        /*check_dependency_p=*/true,
12076                                        /*type_p=*/false,
12077                                        /*is_declaration=*/false);
12078       /* If we found it, and the next token is a `*', then we are
12079          indeed looking at a pointer-to-member operator.  */
12080       if (!cp_parser_error_occurred (parser)
12081           && cp_parser_require (parser, CPP_MULT, "`*'"))
12082         {
12083           /* Indicate that the `*' operator was used.  */
12084           code = INDIRECT_REF;
12085
12086           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12087             error ("%qD is a namespace", parser->scope);
12088           else
12089             {
12090               /* The type of which the member is a member is given by the
12091                  current SCOPE.  */
12092               *type = parser->scope;
12093               /* The next name will not be qualified.  */
12094               parser->scope = NULL_TREE;
12095               parser->qualifying_scope = NULL_TREE;
12096               parser->object_scope = NULL_TREE;
12097               /* Look for the optional cv-qualifier-seq.  */
12098               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12099             }
12100         }
12101       /* If that didn't work we don't have a ptr-operator.  */
12102       if (!cp_parser_parse_definitely (parser))
12103         cp_parser_error (parser, "expected ptr-operator");
12104     }
12105
12106   return code;
12107 }
12108
12109 /* Parse an (optional) cv-qualifier-seq.
12110
12111    cv-qualifier-seq:
12112      cv-qualifier cv-qualifier-seq [opt]
12113
12114    cv-qualifier:
12115      const
12116      volatile
12117
12118    GNU Extension:
12119
12120    cv-qualifier:
12121      __restrict__
12122
12123    Returns a bitmask representing the cv-qualifiers.  */
12124
12125 static cp_cv_quals
12126 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12127 {
12128   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12129
12130   while (true)
12131     {
12132       cp_token *token;
12133       cp_cv_quals cv_qualifier;
12134
12135       /* Peek at the next token.  */
12136       token = cp_lexer_peek_token (parser->lexer);
12137       /* See if it's a cv-qualifier.  */
12138       switch (token->keyword)
12139         {
12140         case RID_CONST:
12141           cv_qualifier = TYPE_QUAL_CONST;
12142           break;
12143
12144         case RID_VOLATILE:
12145           cv_qualifier = TYPE_QUAL_VOLATILE;
12146           break;
12147
12148         case RID_RESTRICT:
12149           cv_qualifier = TYPE_QUAL_RESTRICT;
12150           break;
12151
12152         default:
12153           cv_qualifier = TYPE_UNQUALIFIED;
12154           break;
12155         }
12156
12157       if (!cv_qualifier)
12158         break;
12159
12160       if (cv_quals & cv_qualifier)
12161         {
12162           error ("duplicate cv-qualifier");
12163           cp_lexer_purge_token (parser->lexer);
12164         }
12165       else
12166         {
12167           cp_lexer_consume_token (parser->lexer);
12168           cv_quals |= cv_qualifier;
12169         }
12170     }
12171
12172   return cv_quals;
12173 }
12174
12175 /* Parse a declarator-id.
12176
12177    declarator-id:
12178      id-expression
12179      :: [opt] nested-name-specifier [opt] type-name
12180
12181    In the `id-expression' case, the value returned is as for
12182    cp_parser_id_expression if the id-expression was an unqualified-id.
12183    If the id-expression was a qualified-id, then a SCOPE_REF is
12184    returned.  The first operand is the scope (either a NAMESPACE_DECL
12185    or TREE_TYPE), but the second is still just a representation of an
12186    unqualified-id.  */
12187
12188 static tree
12189 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12190 {
12191   tree id;
12192   /* The expression must be an id-expression.  Assume that qualified
12193      names are the names of types so that:
12194
12195        template <class T>
12196        int S<T>::R::i = 3;
12197
12198      will work; we must treat `S<T>::R' as the name of a type.
12199      Similarly, assume that qualified names are templates, where
12200      required, so that:
12201
12202        template <class T>
12203        int S<T>::R<T>::i = 3;
12204
12205      will work, too.  */
12206   id = cp_parser_id_expression (parser,
12207                                 /*template_keyword_p=*/false,
12208                                 /*check_dependency_p=*/false,
12209                                 /*template_p=*/NULL,
12210                                 /*declarator_p=*/true,
12211                                 optional_p);
12212   if (id && BASELINK_P (id))
12213     id = BASELINK_FUNCTIONS (id);
12214   return id;
12215 }
12216
12217 /* Parse a type-id.
12218
12219    type-id:
12220      type-specifier-seq abstract-declarator [opt]
12221
12222    Returns the TYPE specified.  */
12223
12224 static tree
12225 cp_parser_type_id (cp_parser* parser)
12226 {
12227   cp_decl_specifier_seq type_specifier_seq;
12228   cp_declarator *abstract_declarator;
12229
12230   /* Parse the type-specifier-seq.  */
12231   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12232                                 &type_specifier_seq);
12233   if (type_specifier_seq.type == error_mark_node)
12234     return error_mark_node;
12235
12236   /* There might or might not be an abstract declarator.  */
12237   cp_parser_parse_tentatively (parser);
12238   /* Look for the declarator.  */
12239   abstract_declarator
12240     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12241                             /*parenthesized_p=*/NULL,
12242                             /*member_p=*/false);
12243   /* Check to see if there really was a declarator.  */
12244   if (!cp_parser_parse_definitely (parser))
12245     abstract_declarator = NULL;
12246
12247   return groktypename (&type_specifier_seq, abstract_declarator);
12248 }
12249
12250 /* Parse a type-specifier-seq.
12251
12252    type-specifier-seq:
12253      type-specifier type-specifier-seq [opt]
12254
12255    GNU extension:
12256
12257    type-specifier-seq:
12258      attributes type-specifier-seq [opt]
12259
12260    If IS_CONDITION is true, we are at the start of a "condition",
12261    e.g., we've just seen "if (".
12262
12263    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
12264
12265 static void
12266 cp_parser_type_specifier_seq (cp_parser* parser,
12267                               bool is_condition,
12268                               cp_decl_specifier_seq *type_specifier_seq)
12269 {
12270   bool seen_type_specifier = false;
12271   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12272
12273   /* Clear the TYPE_SPECIFIER_SEQ.  */
12274   clear_decl_specs (type_specifier_seq);
12275
12276   /* Parse the type-specifiers and attributes.  */
12277   while (true)
12278     {
12279       tree type_specifier;
12280       bool is_cv_qualifier;
12281
12282       /* Check for attributes first.  */
12283       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12284         {
12285           type_specifier_seq->attributes =
12286             chainon (type_specifier_seq->attributes,
12287                      cp_parser_attributes_opt (parser));
12288           continue;
12289         }
12290
12291       /* Look for the type-specifier.  */
12292       type_specifier = cp_parser_type_specifier (parser,
12293                                                  flags,
12294                                                  type_specifier_seq,
12295                                                  /*is_declaration=*/false,
12296                                                  NULL,
12297                                                  &is_cv_qualifier);
12298       if (!type_specifier)
12299         {
12300           /* If the first type-specifier could not be found, this is not a
12301              type-specifier-seq at all.  */
12302           if (!seen_type_specifier)
12303             {
12304               cp_parser_error (parser, "expected type-specifier");
12305               type_specifier_seq->type = error_mark_node;
12306               return;
12307             }
12308           /* If subsequent type-specifiers could not be found, the
12309              type-specifier-seq is complete.  */
12310           break;
12311         }
12312
12313       seen_type_specifier = true;
12314       /* The standard says that a condition can be:
12315
12316             type-specifier-seq declarator = assignment-expression
12317
12318          However, given:
12319
12320            struct S {};
12321            if (int S = ...)
12322
12323          we should treat the "S" as a declarator, not as a
12324          type-specifier.  The standard doesn't say that explicitly for
12325          type-specifier-seq, but it does say that for
12326          decl-specifier-seq in an ordinary declaration.  Perhaps it
12327          would be clearer just to allow a decl-specifier-seq here, and
12328          then add a semantic restriction that if any decl-specifiers
12329          that are not type-specifiers appear, the program is invalid.  */
12330       if (is_condition && !is_cv_qualifier)
12331         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12332     }
12333
12334   cp_parser_check_decl_spec (type_specifier_seq);
12335 }
12336
12337 /* Parse a parameter-declaration-clause.
12338
12339    parameter-declaration-clause:
12340      parameter-declaration-list [opt] ... [opt]
12341      parameter-declaration-list , ...
12342
12343    Returns a representation for the parameter declarations.  A return
12344    value of NULL indicates a parameter-declaration-clause consisting
12345    only of an ellipsis.  */
12346
12347 static cp_parameter_declarator *
12348 cp_parser_parameter_declaration_clause (cp_parser* parser)
12349 {
12350   cp_parameter_declarator *parameters;
12351   cp_token *token;
12352   bool ellipsis_p;
12353   bool is_error;
12354
12355   /* Peek at the next token.  */
12356   token = cp_lexer_peek_token (parser->lexer);
12357   /* Check for trivial parameter-declaration-clauses.  */
12358   if (token->type == CPP_ELLIPSIS)
12359     {
12360       /* Consume the `...' token.  */
12361       cp_lexer_consume_token (parser->lexer);
12362       return NULL;
12363     }
12364   else if (token->type == CPP_CLOSE_PAREN)
12365     /* There are no parameters.  */
12366     {
12367 #ifndef NO_IMPLICIT_EXTERN_C
12368       if (in_system_header && current_class_type == NULL
12369           && current_lang_name == lang_name_c)
12370         return NULL;
12371       else
12372 #endif
12373         return no_parameters;
12374     }
12375   /* Check for `(void)', too, which is a special case.  */
12376   else if (token->keyword == RID_VOID
12377            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12378                == CPP_CLOSE_PAREN))
12379     {
12380       /* Consume the `void' token.  */
12381       cp_lexer_consume_token (parser->lexer);
12382       /* There are no parameters.  */
12383       return no_parameters;
12384     }
12385
12386   /* Parse the parameter-declaration-list.  */
12387   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12388   /* If a parse error occurred while parsing the
12389      parameter-declaration-list, then the entire
12390      parameter-declaration-clause is erroneous.  */
12391   if (is_error)
12392     return NULL;
12393
12394   /* Peek at the next token.  */
12395   token = cp_lexer_peek_token (parser->lexer);
12396   /* If it's a `,', the clause should terminate with an ellipsis.  */
12397   if (token->type == CPP_COMMA)
12398     {
12399       /* Consume the `,'.  */
12400       cp_lexer_consume_token (parser->lexer);
12401       /* Expect an ellipsis.  */
12402       ellipsis_p
12403         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12404     }
12405   /* It might also be `...' if the optional trailing `,' was
12406      omitted.  */
12407   else if (token->type == CPP_ELLIPSIS)
12408     {
12409       /* Consume the `...' token.  */
12410       cp_lexer_consume_token (parser->lexer);
12411       /* And remember that we saw it.  */
12412       ellipsis_p = true;
12413     }
12414   else
12415     ellipsis_p = false;
12416
12417   /* Finish the parameter list.  */
12418   if (parameters && ellipsis_p)
12419     parameters->ellipsis_p = true;
12420
12421   return parameters;
12422 }
12423
12424 /* Parse a parameter-declaration-list.
12425
12426    parameter-declaration-list:
12427      parameter-declaration
12428      parameter-declaration-list , parameter-declaration
12429
12430    Returns a representation of the parameter-declaration-list, as for
12431    cp_parser_parameter_declaration_clause.  However, the
12432    `void_list_node' is never appended to the list.  Upon return,
12433    *IS_ERROR will be true iff an error occurred.  */
12434
12435 static cp_parameter_declarator *
12436 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12437 {
12438   cp_parameter_declarator *parameters = NULL;
12439   cp_parameter_declarator **tail = &parameters;
12440   bool saved_in_unbraced_linkage_specification_p;
12441
12442   /* Assume all will go well.  */
12443   *is_error = false;
12444   /* The special considerations that apply to a function within an
12445      unbraced linkage specifications do not apply to the parameters
12446      to the function.  */
12447   saved_in_unbraced_linkage_specification_p 
12448     = parser->in_unbraced_linkage_specification_p;
12449   parser->in_unbraced_linkage_specification_p = false;
12450
12451   /* Look for more parameters.  */
12452   while (true)
12453     {
12454       cp_parameter_declarator *parameter;
12455       bool parenthesized_p;
12456       /* Parse the parameter.  */
12457       parameter
12458         = cp_parser_parameter_declaration (parser,
12459                                            /*template_parm_p=*/false,
12460                                            &parenthesized_p);
12461
12462       /* If a parse error occurred parsing the parameter declaration,
12463          then the entire parameter-declaration-list is erroneous.  */
12464       if (!parameter)
12465         {
12466           *is_error = true;
12467           parameters = NULL;
12468           break;
12469         }
12470       /* Add the new parameter to the list.  */
12471       *tail = parameter;
12472       tail = &parameter->next;
12473
12474       /* Peek at the next token.  */
12475       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12476           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12477           /* These are for Objective-C++ */
12478           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12479           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12480         /* The parameter-declaration-list is complete.  */
12481         break;
12482       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12483         {
12484           cp_token *token;
12485
12486           /* Peek at the next token.  */
12487           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12488           /* If it's an ellipsis, then the list is complete.  */
12489           if (token->type == CPP_ELLIPSIS)
12490             break;
12491           /* Otherwise, there must be more parameters.  Consume the
12492              `,'.  */
12493           cp_lexer_consume_token (parser->lexer);
12494           /* When parsing something like:
12495
12496                 int i(float f, double d)
12497
12498              we can tell after seeing the declaration for "f" that we
12499              are not looking at an initialization of a variable "i",
12500              but rather at the declaration of a function "i".
12501
12502              Due to the fact that the parsing of template arguments
12503              (as specified to a template-id) requires backtracking we
12504              cannot use this technique when inside a template argument
12505              list.  */
12506           if (!parser->in_template_argument_list_p
12507               && !parser->in_type_id_in_expr_p
12508               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12509               /* However, a parameter-declaration of the form
12510                  "foat(f)" (which is a valid declaration of a
12511                  parameter "f") can also be interpreted as an
12512                  expression (the conversion of "f" to "float").  */
12513               && !parenthesized_p)
12514             cp_parser_commit_to_tentative_parse (parser);
12515         }
12516       else
12517         {
12518           cp_parser_error (parser, "expected %<,%> or %<...%>");
12519           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12520             cp_parser_skip_to_closing_parenthesis (parser,
12521                                                    /*recovering=*/true,
12522                                                    /*or_comma=*/false,
12523                                                    /*consume_paren=*/false);
12524           break;
12525         }
12526     }
12527
12528   parser->in_unbraced_linkage_specification_p
12529     = saved_in_unbraced_linkage_specification_p;
12530
12531   return parameters;
12532 }
12533
12534 /* Parse a parameter declaration.
12535
12536    parameter-declaration:
12537      decl-specifier-seq declarator
12538      decl-specifier-seq declarator = assignment-expression
12539      decl-specifier-seq abstract-declarator [opt]
12540      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12541
12542    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12543    declares a template parameter.  (In that case, a non-nested `>'
12544    token encountered during the parsing of the assignment-expression
12545    is not interpreted as a greater-than operator.)
12546
12547    Returns a representation of the parameter, or NULL if an error
12548    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12549    true iff the declarator is of the form "(p)".  */
12550
12551 static cp_parameter_declarator *
12552 cp_parser_parameter_declaration (cp_parser *parser,
12553                                  bool template_parm_p,
12554                                  bool *parenthesized_p)
12555 {
12556   int declares_class_or_enum;
12557   bool greater_than_is_operator_p;
12558   cp_decl_specifier_seq decl_specifiers;
12559   cp_declarator *declarator;
12560   tree default_argument;
12561   cp_token *token;
12562   const char *saved_message;
12563
12564   /* In a template parameter, `>' is not an operator.
12565
12566      [temp.param]
12567
12568      When parsing a default template-argument for a non-type
12569      template-parameter, the first non-nested `>' is taken as the end
12570      of the template parameter-list rather than a greater-than
12571      operator.  */
12572   greater_than_is_operator_p = !template_parm_p;
12573
12574   /* Type definitions may not appear in parameter types.  */
12575   saved_message = parser->type_definition_forbidden_message;
12576   parser->type_definition_forbidden_message
12577     = "types may not be defined in parameter types";
12578
12579   /* Parse the declaration-specifiers.  */
12580   cp_parser_decl_specifier_seq (parser,
12581                                 CP_PARSER_FLAGS_NONE,
12582                                 &decl_specifiers,
12583                                 &declares_class_or_enum);
12584   /* If an error occurred, there's no reason to attempt to parse the
12585      rest of the declaration.  */
12586   if (cp_parser_error_occurred (parser))
12587     {
12588       parser->type_definition_forbidden_message = saved_message;
12589       return NULL;
12590     }
12591
12592   /* Peek at the next token.  */
12593   token = cp_lexer_peek_token (parser->lexer);
12594   /* If the next token is a `)', `,', `=', `>', or `...', then there
12595      is no declarator.  */
12596   if (token->type == CPP_CLOSE_PAREN
12597       || token->type == CPP_COMMA
12598       || token->type == CPP_EQ
12599       || token->type == CPP_ELLIPSIS
12600       || token->type == CPP_GREATER)
12601     {
12602       declarator = NULL;
12603       if (parenthesized_p)
12604         *parenthesized_p = false;
12605     }
12606   /* Otherwise, there should be a declarator.  */
12607   else
12608     {
12609       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12610       parser->default_arg_ok_p = false;
12611
12612       /* After seeing a decl-specifier-seq, if the next token is not a
12613          "(", there is no possibility that the code is a valid
12614          expression.  Therefore, if parsing tentatively, we commit at
12615          this point.  */
12616       if (!parser->in_template_argument_list_p
12617           /* In an expression context, having seen:
12618
12619                (int((char ...
12620
12621              we cannot be sure whether we are looking at a
12622              function-type (taking a "char" as a parameter) or a cast
12623              of some object of type "char" to "int".  */
12624           && !parser->in_type_id_in_expr_p
12625           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12626           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12627         cp_parser_commit_to_tentative_parse (parser);
12628       /* Parse the declarator.  */
12629       declarator = cp_parser_declarator (parser,
12630                                          CP_PARSER_DECLARATOR_EITHER,
12631                                          /*ctor_dtor_or_conv_p=*/NULL,
12632                                          parenthesized_p,
12633                                          /*member_p=*/false);
12634       parser->default_arg_ok_p = saved_default_arg_ok_p;
12635       /* After the declarator, allow more attributes.  */
12636       decl_specifiers.attributes
12637         = chainon (decl_specifiers.attributes,
12638                    cp_parser_attributes_opt (parser));
12639     }
12640
12641   /* The restriction on defining new types applies only to the type
12642      of the parameter, not to the default argument.  */
12643   parser->type_definition_forbidden_message = saved_message;
12644
12645   /* If the next token is `=', then process a default argument.  */
12646   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12647     {
12648       bool saved_greater_than_is_operator_p;
12649       /* Consume the `='.  */
12650       cp_lexer_consume_token (parser->lexer);
12651
12652       /* If we are defining a class, then the tokens that make up the
12653          default argument must be saved and processed later.  */
12654       if (!template_parm_p && at_class_scope_p ()
12655           && TYPE_BEING_DEFINED (current_class_type))
12656         {
12657           unsigned depth = 0;
12658           cp_token *first_token;
12659           cp_token *token;
12660
12661           /* Add tokens until we have processed the entire default
12662              argument.  We add the range [first_token, token).  */
12663           first_token = cp_lexer_peek_token (parser->lexer);
12664           while (true)
12665             {
12666               bool done = false;
12667
12668               /* Peek at the next token.  */
12669               token = cp_lexer_peek_token (parser->lexer);
12670               /* What we do depends on what token we have.  */
12671               switch (token->type)
12672                 {
12673                   /* In valid code, a default argument must be
12674                      immediately followed by a `,' `)', or `...'.  */
12675                 case CPP_COMMA:
12676                 case CPP_CLOSE_PAREN:
12677                 case CPP_ELLIPSIS:
12678                   /* If we run into a non-nested `;', `}', or `]',
12679                      then the code is invalid -- but the default
12680                      argument is certainly over.  */
12681                 case CPP_SEMICOLON:
12682                 case CPP_CLOSE_BRACE:
12683                 case CPP_CLOSE_SQUARE:
12684                   if (depth == 0)
12685                     done = true;
12686                   /* Update DEPTH, if necessary.  */
12687                   else if (token->type == CPP_CLOSE_PAREN
12688                            || token->type == CPP_CLOSE_BRACE
12689                            || token->type == CPP_CLOSE_SQUARE)
12690                     --depth;
12691                   break;
12692
12693                 case CPP_OPEN_PAREN:
12694                 case CPP_OPEN_SQUARE:
12695                 case CPP_OPEN_BRACE:
12696                   ++depth;
12697                   break;
12698
12699                 case CPP_GREATER:
12700                   /* If we see a non-nested `>', and `>' is not an
12701                      operator, then it marks the end of the default
12702                      argument.  */
12703                   if (!depth && !greater_than_is_operator_p)
12704                     done = true;
12705                   break;
12706
12707                   /* If we run out of tokens, issue an error message.  */
12708                 case CPP_EOF:
12709                 case CPP_PRAGMA_EOL:
12710                   error ("file ends in default argument");
12711                   done = true;
12712                   break;
12713
12714                 case CPP_NAME:
12715                 case CPP_SCOPE:
12716                   /* In these cases, we should look for template-ids.
12717                      For example, if the default argument is
12718                      `X<int, double>()', we need to do name lookup to
12719                      figure out whether or not `X' is a template; if
12720                      so, the `,' does not end the default argument.
12721
12722                      That is not yet done.  */
12723                   break;
12724
12725                 default:
12726                   break;
12727                 }
12728
12729               /* If we've reached the end, stop.  */
12730               if (done)
12731                 break;
12732
12733               /* Add the token to the token block.  */
12734               token = cp_lexer_consume_token (parser->lexer);
12735             }
12736
12737           /* Create a DEFAULT_ARG to represented the unparsed default
12738              argument.  */
12739           default_argument = make_node (DEFAULT_ARG);
12740           DEFARG_TOKENS (default_argument)
12741             = cp_token_cache_new (first_token, token);
12742           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12743         }
12744       /* Outside of a class definition, we can just parse the
12745          assignment-expression.  */
12746       else
12747         {
12748           bool saved_local_variables_forbidden_p;
12749
12750           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12751              set correctly.  */
12752           saved_greater_than_is_operator_p
12753             = parser->greater_than_is_operator_p;
12754           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12755           /* Local variable names (and the `this' keyword) may not
12756              appear in a default argument.  */
12757           saved_local_variables_forbidden_p
12758             = parser->local_variables_forbidden_p;
12759           parser->local_variables_forbidden_p = true;
12760           /* The default argument expression may cause implicitly
12761              defined member functions to be synthesized, which will
12762              result in garbage collection.  We must treat this
12763              situation as if we were within the body of function so as
12764              to avoid collecting live data on the stack.  */
12765           ++function_depth;
12766           /* Parse the assignment-expression.  */
12767           if (template_parm_p)
12768             push_deferring_access_checks (dk_no_deferred);
12769           default_argument
12770             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12771           if (template_parm_p)
12772             pop_deferring_access_checks ();
12773           /* Restore saved state.  */
12774           --function_depth;
12775           parser->greater_than_is_operator_p
12776             = saved_greater_than_is_operator_p;
12777           parser->local_variables_forbidden_p
12778             = saved_local_variables_forbidden_p;
12779         }
12780       if (!parser->default_arg_ok_p)
12781         {
12782           if (!flag_pedantic_errors)
12783             warning (0, "deprecated use of default argument for parameter of non-function");
12784           else
12785             {
12786               error ("default arguments are only permitted for function parameters");
12787               default_argument = NULL_TREE;
12788             }
12789         }
12790     }
12791   else
12792     default_argument = NULL_TREE;
12793
12794   return make_parameter_declarator (&decl_specifiers,
12795                                     declarator,
12796                                     default_argument);
12797 }
12798
12799 /* Parse a function-body.
12800
12801    function-body:
12802      compound_statement  */
12803
12804 static void
12805 cp_parser_function_body (cp_parser *parser)
12806 {
12807   cp_parser_compound_statement (parser, NULL, false);
12808 }
12809
12810 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12811    true if a ctor-initializer was present.  */
12812
12813 static bool
12814 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12815 {
12816   tree body;
12817   bool ctor_initializer_p;
12818
12819   /* Begin the function body.  */
12820   body = begin_function_body ();
12821   /* Parse the optional ctor-initializer.  */
12822   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12823   /* Parse the function-body.  */
12824   cp_parser_function_body (parser);
12825   /* Finish the function body.  */
12826   finish_function_body (body);
12827
12828   return ctor_initializer_p;
12829 }
12830
12831 /* Parse an initializer.
12832
12833    initializer:
12834      = initializer-clause
12835      ( expression-list )
12836
12837    Returns an expression representing the initializer.  If no
12838    initializer is present, NULL_TREE is returned.
12839
12840    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12841    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12842    set to FALSE if there is no initializer present.  If there is an
12843    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12844    is set to true; otherwise it is set to false.  */
12845
12846 static tree
12847 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12848                        bool* non_constant_p)
12849 {
12850   cp_token *token;
12851   tree init;
12852
12853   /* Peek at the next token.  */
12854   token = cp_lexer_peek_token (parser->lexer);
12855
12856   /* Let our caller know whether or not this initializer was
12857      parenthesized.  */
12858   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12859   /* Assume that the initializer is constant.  */
12860   *non_constant_p = false;
12861
12862   if (token->type == CPP_EQ)
12863     {
12864       /* Consume the `='.  */
12865       cp_lexer_consume_token (parser->lexer);
12866       /* Parse the initializer-clause.  */
12867       init = cp_parser_initializer_clause (parser, non_constant_p);
12868     }
12869   else if (token->type == CPP_OPEN_PAREN)
12870     init = cp_parser_parenthesized_expression_list (parser, false,
12871                                                     /*cast_p=*/false,
12872                                                     non_constant_p);
12873   else
12874     {
12875       /* Anything else is an error.  */
12876       cp_parser_error (parser, "expected initializer");
12877       init = error_mark_node;
12878     }
12879
12880   return init;
12881 }
12882
12883 /* Parse an initializer-clause.
12884
12885    initializer-clause:
12886      assignment-expression
12887      { initializer-list , [opt] }
12888      { }
12889
12890    Returns an expression representing the initializer.
12891
12892    If the `assignment-expression' production is used the value
12893    returned is simply a representation for the expression.
12894
12895    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12896    the elements of the initializer-list (or NULL, if the last
12897    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12898    NULL_TREE.  There is no way to detect whether or not the optional
12899    trailing `,' was provided.  NON_CONSTANT_P is as for
12900    cp_parser_initializer.  */
12901
12902 static tree
12903 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12904 {
12905   tree initializer;
12906
12907   /* Assume the expression is constant.  */
12908   *non_constant_p = false;
12909
12910   /* If it is not a `{', then we are looking at an
12911      assignment-expression.  */
12912   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12913     {
12914       initializer
12915         = cp_parser_constant_expression (parser,
12916                                         /*allow_non_constant_p=*/true,
12917                                         non_constant_p);
12918       if (!*non_constant_p)
12919         initializer = fold_non_dependent_expr (initializer);
12920     }
12921   else
12922     {
12923       /* Consume the `{' token.  */
12924       cp_lexer_consume_token (parser->lexer);
12925       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12926       initializer = make_node (CONSTRUCTOR);
12927       /* If it's not a `}', then there is a non-trivial initializer.  */
12928       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12929         {
12930           /* Parse the initializer list.  */
12931           CONSTRUCTOR_ELTS (initializer)
12932             = cp_parser_initializer_list (parser, non_constant_p);
12933           /* A trailing `,' token is allowed.  */
12934           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12935             cp_lexer_consume_token (parser->lexer);
12936         }
12937       /* Now, there should be a trailing `}'.  */
12938       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12939     }
12940
12941   return initializer;
12942 }
12943
12944 /* Parse an initializer-list.
12945
12946    initializer-list:
12947      initializer-clause
12948      initializer-list , initializer-clause
12949
12950    GNU Extension:
12951
12952    initializer-list:
12953      identifier : initializer-clause
12954      initializer-list, identifier : initializer-clause
12955
12956    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12957    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12958    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12959    as for cp_parser_initializer.  */
12960
12961 static VEC(constructor_elt,gc) *
12962 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12963 {
12964   VEC(constructor_elt,gc) *v = NULL;
12965
12966   /* Assume all of the expressions are constant.  */
12967   *non_constant_p = false;
12968
12969   /* Parse the rest of the list.  */
12970   while (true)
12971     {
12972       cp_token *token;
12973       tree identifier;
12974       tree initializer;
12975       bool clause_non_constant_p;
12976
12977       /* If the next token is an identifier and the following one is a
12978          colon, we are looking at the GNU designated-initializer
12979          syntax.  */
12980       if (cp_parser_allow_gnu_extensions_p (parser)
12981           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12982           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12983         {
12984           /* Warn the user that they are using an extension.  */
12985           if (pedantic)
12986             pedwarn ("ISO C++ does not allow designated initializers");
12987           /* Consume the identifier.  */
12988           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
12989           /* Consume the `:'.  */
12990           cp_lexer_consume_token (parser->lexer);
12991         }
12992       else
12993         identifier = NULL_TREE;
12994
12995       /* Parse the initializer.  */
12996       initializer = cp_parser_initializer_clause (parser,
12997                                                   &clause_non_constant_p);
12998       /* If any clause is non-constant, so is the entire initializer.  */
12999       if (clause_non_constant_p)
13000         *non_constant_p = true;
13001
13002       /* Add it to the vector.  */
13003       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13004
13005       /* If the next token is not a comma, we have reached the end of
13006          the list.  */
13007       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13008         break;
13009
13010       /* Peek at the next token.  */
13011       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13012       /* If the next token is a `}', then we're still done.  An
13013          initializer-clause can have a trailing `,' after the
13014          initializer-list and before the closing `}'.  */
13015       if (token->type == CPP_CLOSE_BRACE)
13016         break;
13017
13018       /* Consume the `,' token.  */
13019       cp_lexer_consume_token (parser->lexer);
13020     }
13021
13022   return v;
13023 }
13024
13025 /* Classes [gram.class] */
13026
13027 /* Parse a class-name.
13028
13029    class-name:
13030      identifier
13031      template-id
13032
13033    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13034    to indicate that names looked up in dependent types should be
13035    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
13036    keyword has been used to indicate that the name that appears next
13037    is a template.  TAG_TYPE indicates the explicit tag given before
13038    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
13039    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
13040    is the class being defined in a class-head.
13041
13042    Returns the TYPE_DECL representing the class.  */
13043
13044 static tree
13045 cp_parser_class_name (cp_parser *parser,
13046                       bool typename_keyword_p,
13047                       bool template_keyword_p,
13048                       enum tag_types tag_type,
13049                       bool check_dependency_p,
13050                       bool class_head_p,
13051                       bool is_declaration)
13052 {
13053   tree decl;
13054   tree scope;
13055   bool typename_p;
13056   cp_token *token;
13057
13058   /* All class-names start with an identifier.  */
13059   token = cp_lexer_peek_token (parser->lexer);
13060   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13061     {
13062       cp_parser_error (parser, "expected class-name");
13063       return error_mark_node;
13064     }
13065
13066   /* PARSER->SCOPE can be cleared when parsing the template-arguments
13067      to a template-id, so we save it here.  */
13068   scope = parser->scope;
13069   if (scope == error_mark_node)
13070     return error_mark_node;
13071
13072   /* Any name names a type if we're following the `typename' keyword
13073      in a qualified name where the enclosing scope is type-dependent.  */
13074   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13075                 && dependent_type_p (scope));
13076   /* Handle the common case (an identifier, but not a template-id)
13077      efficiently.  */
13078   if (token->type == CPP_NAME
13079       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13080     {
13081       cp_token *identifier_token;
13082       tree identifier;
13083       bool ambiguous_p;
13084
13085       /* Look for the identifier.  */
13086       identifier_token = cp_lexer_peek_token (parser->lexer);
13087       ambiguous_p = identifier_token->ambiguous_p;
13088       identifier = cp_parser_identifier (parser);
13089       /* If the next token isn't an identifier, we are certainly not
13090          looking at a class-name.  */
13091       if (identifier == error_mark_node)
13092         decl = error_mark_node;
13093       /* If we know this is a type-name, there's no need to look it
13094          up.  */
13095       else if (typename_p)
13096         decl = identifier;
13097       else
13098         {
13099           tree ambiguous_decls;
13100           /* If we already know that this lookup is ambiguous, then
13101              we've already issued an error message; there's no reason
13102              to check again.  */
13103           if (ambiguous_p)
13104             {
13105               cp_parser_simulate_error (parser);
13106               return error_mark_node;
13107             }
13108           /* If the next token is a `::', then the name must be a type
13109              name.
13110
13111              [basic.lookup.qual]
13112
13113              During the lookup for a name preceding the :: scope
13114              resolution operator, object, function, and enumerator
13115              names are ignored.  */
13116           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13117             tag_type = typename_type;
13118           /* Look up the name.  */
13119           decl = cp_parser_lookup_name (parser, identifier,
13120                                         tag_type,
13121                                         /*is_template=*/false,
13122                                         /*is_namespace=*/false,
13123                                         check_dependency_p,
13124                                         &ambiguous_decls);
13125           if (ambiguous_decls)
13126             {
13127               error ("reference to %qD is ambiguous", identifier);
13128               print_candidates (ambiguous_decls);
13129               if (cp_parser_parsing_tentatively (parser))
13130                 {
13131                   identifier_token->ambiguous_p = true;
13132                   cp_parser_simulate_error (parser);
13133                 }
13134               return error_mark_node;
13135             }
13136         }
13137     }
13138   else
13139     {
13140       /* Try a template-id.  */
13141       decl = cp_parser_template_id (parser, template_keyword_p,
13142                                     check_dependency_p,
13143                                     is_declaration);
13144       if (decl == error_mark_node)
13145         return error_mark_node;
13146     }
13147
13148   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13149
13150   /* If this is a typename, create a TYPENAME_TYPE.  */
13151   if (typename_p && decl != error_mark_node)
13152     {
13153       decl = make_typename_type (scope, decl, typename_type,
13154                                  /*complain=*/tf_error);
13155       if (decl != error_mark_node)
13156         decl = TYPE_NAME (decl);
13157     }
13158
13159   /* Check to see that it is really the name of a class.  */
13160   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13161       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13162       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13163     /* Situations like this:
13164
13165          template <typename T> struct A {
13166            typename T::template X<int>::I i;
13167          };
13168
13169        are problematic.  Is `T::template X<int>' a class-name?  The
13170        standard does not seem to be definitive, but there is no other
13171        valid interpretation of the following `::'.  Therefore, those
13172        names are considered class-names.  */
13173     {
13174       decl = make_typename_type (scope, decl, tag_type, tf_error);
13175       if (decl != error_mark_node)
13176         decl = TYPE_NAME (decl);
13177     }
13178   else if (TREE_CODE (decl) != TYPE_DECL
13179            || TREE_TYPE (decl) == error_mark_node
13180            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13181     decl = error_mark_node;
13182
13183   if (decl == error_mark_node)
13184     cp_parser_error (parser, "expected class-name");
13185
13186   return decl;
13187 }
13188
13189 /* Parse a class-specifier.
13190
13191    class-specifier:
13192      class-head { member-specification [opt] }
13193
13194    Returns the TREE_TYPE representing the class.  */
13195
13196 static tree
13197 cp_parser_class_specifier (cp_parser* parser)
13198 {
13199   cp_token *token;
13200   tree type;
13201   tree attributes = NULL_TREE;
13202   int has_trailing_semicolon;
13203   bool nested_name_specifier_p;
13204   unsigned saved_num_template_parameter_lists;
13205   bool saved_in_function_body;
13206   tree old_scope = NULL_TREE;
13207   tree scope = NULL_TREE;
13208   tree bases;
13209
13210   push_deferring_access_checks (dk_no_deferred);
13211
13212   /* Parse the class-head.  */
13213   type = cp_parser_class_head (parser,
13214                                &nested_name_specifier_p,
13215                                &attributes,
13216                                &bases);
13217   /* If the class-head was a semantic disaster, skip the entire body
13218      of the class.  */
13219   if (!type)
13220     {
13221       cp_parser_skip_to_end_of_block_or_statement (parser);
13222       pop_deferring_access_checks ();
13223       return error_mark_node;
13224     }
13225
13226   /* Look for the `{'.  */
13227   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13228     {
13229       pop_deferring_access_checks ();
13230       return error_mark_node;
13231     }
13232
13233   /* Process the base classes. If they're invalid, skip the 
13234      entire class body.  */
13235   if (!xref_basetypes (type, bases))
13236     {
13237       cp_parser_skip_to_closing_brace (parser);
13238
13239       /* Consuming the closing brace yields better error messages
13240          later on.  */
13241       cp_lexer_consume_token (parser->lexer);
13242       pop_deferring_access_checks ();
13243       return error_mark_node;
13244     }
13245
13246   /* Issue an error message if type-definitions are forbidden here.  */
13247   cp_parser_check_type_definition (parser);
13248   /* Remember that we are defining one more class.  */
13249   ++parser->num_classes_being_defined;
13250   /* Inside the class, surrounding template-parameter-lists do not
13251      apply.  */
13252   saved_num_template_parameter_lists
13253     = parser->num_template_parameter_lists;
13254   parser->num_template_parameter_lists = 0;
13255   /* We are not in a function body.  */
13256   saved_in_function_body = parser->in_function_body;
13257   parser->in_function_body = false;
13258
13259   /* Start the class.  */
13260   if (nested_name_specifier_p)
13261     {
13262       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13263       old_scope = push_inner_scope (scope);
13264     }
13265   type = begin_class_definition (type, attributes);
13266
13267   if (type == error_mark_node)
13268     /* If the type is erroneous, skip the entire body of the class.  */
13269     cp_parser_skip_to_closing_brace (parser);
13270   else
13271     /* Parse the member-specification.  */
13272     cp_parser_member_specification_opt (parser);
13273
13274   /* Look for the trailing `}'.  */
13275   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13276   /* We get better error messages by noticing a common problem: a
13277      missing trailing `;'.  */
13278   token = cp_lexer_peek_token (parser->lexer);
13279   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13280   /* Look for trailing attributes to apply to this class.  */
13281   if (cp_parser_allow_gnu_extensions_p (parser))
13282     attributes = cp_parser_attributes_opt (parser);
13283   if (type != error_mark_node)
13284     type = finish_struct (type, attributes);
13285   if (nested_name_specifier_p)
13286     pop_inner_scope (old_scope, scope);
13287   /* If this class is not itself within the scope of another class,
13288      then we need to parse the bodies of all of the queued function
13289      definitions.  Note that the queued functions defined in a class
13290      are not always processed immediately following the
13291      class-specifier for that class.  Consider:
13292
13293        struct A {
13294          struct B { void f() { sizeof (A); } };
13295        };
13296
13297      If `f' were processed before the processing of `A' were
13298      completed, there would be no way to compute the size of `A'.
13299      Note that the nesting we are interested in here is lexical --
13300      not the semantic nesting given by TYPE_CONTEXT.  In particular,
13301      for:
13302
13303        struct A { struct B; };
13304        struct A::B { void f() { } };
13305
13306      there is no need to delay the parsing of `A::B::f'.  */
13307   if (--parser->num_classes_being_defined == 0)
13308     {
13309       tree queue_entry;
13310       tree fn;
13311       tree class_type = NULL_TREE;
13312       tree pushed_scope = NULL_TREE;
13313
13314       /* In a first pass, parse default arguments to the functions.
13315          Then, in a second pass, parse the bodies of the functions.
13316          This two-phased approach handles cases like:
13317
13318             struct S {
13319               void f() { g(); }
13320               void g(int i = 3);
13321             };
13322
13323          */
13324       for (TREE_PURPOSE (parser->unparsed_functions_queues)
13325              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13326            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13327            TREE_PURPOSE (parser->unparsed_functions_queues)
13328              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13329         {
13330           fn = TREE_VALUE (queue_entry);
13331           /* If there are default arguments that have not yet been processed,
13332              take care of them now.  */
13333           if (class_type != TREE_PURPOSE (queue_entry))
13334             {
13335               if (pushed_scope)
13336                 pop_scope (pushed_scope);
13337               class_type = TREE_PURPOSE (queue_entry);
13338               pushed_scope = push_scope (class_type);
13339             }
13340           /* Make sure that any template parameters are in scope.  */
13341           maybe_begin_member_template_processing (fn);
13342           /* Parse the default argument expressions.  */
13343           cp_parser_late_parsing_default_args (parser, fn);
13344           /* Remove any template parameters from the symbol table.  */
13345           maybe_end_member_template_processing ();
13346         }
13347       if (pushed_scope)
13348         pop_scope (pushed_scope);
13349       /* Now parse the body of the functions.  */
13350       for (TREE_VALUE (parser->unparsed_functions_queues)
13351              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13352            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13353            TREE_VALUE (parser->unparsed_functions_queues)
13354              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13355         {
13356           /* Figure out which function we need to process.  */
13357           fn = TREE_VALUE (queue_entry);
13358           /* Parse the function.  */
13359           cp_parser_late_parsing_for_member (parser, fn);
13360         }
13361     }
13362
13363   /* Put back any saved access checks.  */
13364   pop_deferring_access_checks ();
13365
13366   /* Restore saved state.  */
13367   parser->in_function_body = saved_in_function_body;
13368   parser->num_template_parameter_lists
13369     = saved_num_template_parameter_lists;
13370
13371   return type;
13372 }
13373
13374 /* Parse a class-head.
13375
13376    class-head:
13377      class-key identifier [opt] base-clause [opt]
13378      class-key nested-name-specifier identifier base-clause [opt]
13379      class-key nested-name-specifier [opt] template-id
13380        base-clause [opt]
13381
13382    GNU Extensions:
13383      class-key attributes identifier [opt] base-clause [opt]
13384      class-key attributes nested-name-specifier identifier base-clause [opt]
13385      class-key attributes nested-name-specifier [opt] template-id
13386        base-clause [opt]
13387
13388    Returns the TYPE of the indicated class.  Sets
13389    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13390    involving a nested-name-specifier was used, and FALSE otherwise.
13391
13392    Returns error_mark_node if this is not a class-head.
13393
13394    Returns NULL_TREE if the class-head is syntactically valid, but
13395    semantically invalid in a way that means we should skip the entire
13396    body of the class.  */
13397
13398 static tree
13399 cp_parser_class_head (cp_parser* parser,
13400                       bool* nested_name_specifier_p,
13401                       tree *attributes_p,
13402                       tree *bases)
13403 {
13404   tree nested_name_specifier;
13405   enum tag_types class_key;
13406   tree id = NULL_TREE;
13407   tree type = NULL_TREE;
13408   tree attributes;
13409   bool template_id_p = false;
13410   bool qualified_p = false;
13411   bool invalid_nested_name_p = false;
13412   bool invalid_explicit_specialization_p = false;
13413   tree pushed_scope = NULL_TREE;
13414   unsigned num_templates;
13415
13416   /* Assume no nested-name-specifier will be present.  */
13417   *nested_name_specifier_p = false;
13418   /* Assume no template parameter lists will be used in defining the
13419      type.  */
13420   num_templates = 0;
13421
13422   /* Look for the class-key.  */
13423   class_key = cp_parser_class_key (parser);
13424   if (class_key == none_type)
13425     return error_mark_node;
13426
13427   /* Parse the attributes.  */
13428   attributes = cp_parser_attributes_opt (parser);
13429
13430   /* If the next token is `::', that is invalid -- but sometimes
13431      people do try to write:
13432
13433        struct ::S {};
13434
13435      Handle this gracefully by accepting the extra qualifier, and then
13436      issuing an error about it later if this really is a
13437      class-head.  If it turns out just to be an elaborated type
13438      specifier, remain silent.  */
13439   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13440     qualified_p = true;
13441
13442   push_deferring_access_checks (dk_no_check);
13443
13444   /* Determine the name of the class.  Begin by looking for an
13445      optional nested-name-specifier.  */
13446   nested_name_specifier
13447     = cp_parser_nested_name_specifier_opt (parser,
13448                                            /*typename_keyword_p=*/false,
13449                                            /*check_dependency_p=*/false,
13450                                            /*type_p=*/false,
13451                                            /*is_declaration=*/false);
13452   /* If there was a nested-name-specifier, then there *must* be an
13453      identifier.  */
13454   if (nested_name_specifier)
13455     {
13456       /* Although the grammar says `identifier', it really means
13457          `class-name' or `template-name'.  You are only allowed to
13458          define a class that has already been declared with this
13459          syntax.
13460
13461          The proposed resolution for Core Issue 180 says that wherever
13462          you see `class T::X' you should treat `X' as a type-name.
13463
13464          It is OK to define an inaccessible class; for example:
13465
13466            class A { class B; };
13467            class A::B {};
13468
13469          We do not know if we will see a class-name, or a
13470          template-name.  We look for a class-name first, in case the
13471          class-name is a template-id; if we looked for the
13472          template-name first we would stop after the template-name.  */
13473       cp_parser_parse_tentatively (parser);
13474       type = cp_parser_class_name (parser,
13475                                    /*typename_keyword_p=*/false,
13476                                    /*template_keyword_p=*/false,
13477                                    class_type,
13478                                    /*check_dependency_p=*/false,
13479                                    /*class_head_p=*/true,
13480                                    /*is_declaration=*/false);
13481       /* If that didn't work, ignore the nested-name-specifier.  */
13482       if (!cp_parser_parse_definitely (parser))
13483         {
13484           invalid_nested_name_p = true;
13485           id = cp_parser_identifier (parser);
13486           if (id == error_mark_node)
13487             id = NULL_TREE;
13488         }
13489       /* If we could not find a corresponding TYPE, treat this
13490          declaration like an unqualified declaration.  */
13491       if (type == error_mark_node)
13492         nested_name_specifier = NULL_TREE;
13493       /* Otherwise, count the number of templates used in TYPE and its
13494          containing scopes.  */
13495       else
13496         {
13497           tree scope;
13498
13499           for (scope = TREE_TYPE (type);
13500                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13501                scope = (TYPE_P (scope)
13502                         ? TYPE_CONTEXT (scope)
13503                         : DECL_CONTEXT (scope)))
13504             if (TYPE_P (scope)
13505                 && CLASS_TYPE_P (scope)
13506                 && CLASSTYPE_TEMPLATE_INFO (scope)
13507                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13508                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13509               ++num_templates;
13510         }
13511     }
13512   /* Otherwise, the identifier is optional.  */
13513   else
13514     {
13515       /* We don't know whether what comes next is a template-id,
13516          an identifier, or nothing at all.  */
13517       cp_parser_parse_tentatively (parser);
13518       /* Check for a template-id.  */
13519       id = cp_parser_template_id (parser,
13520                                   /*template_keyword_p=*/false,
13521                                   /*check_dependency_p=*/true,
13522                                   /*is_declaration=*/true);
13523       /* If that didn't work, it could still be an identifier.  */
13524       if (!cp_parser_parse_definitely (parser))
13525         {
13526           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13527             id = cp_parser_identifier (parser);
13528           else
13529             id = NULL_TREE;
13530         }
13531       else
13532         {
13533           template_id_p = true;
13534           ++num_templates;
13535         }
13536     }
13537
13538   pop_deferring_access_checks ();
13539
13540   if (id)
13541     cp_parser_check_for_invalid_template_id (parser, id);
13542
13543   /* If it's not a `:' or a `{' then we can't really be looking at a
13544      class-head, since a class-head only appears as part of a
13545      class-specifier.  We have to detect this situation before calling
13546      xref_tag, since that has irreversible side-effects.  */
13547   if (!cp_parser_next_token_starts_class_definition_p (parser))
13548     {
13549       cp_parser_error (parser, "expected %<{%> or %<:%>");
13550       return error_mark_node;
13551     }
13552
13553   /* At this point, we're going ahead with the class-specifier, even
13554      if some other problem occurs.  */
13555   cp_parser_commit_to_tentative_parse (parser);
13556   /* Issue the error about the overly-qualified name now.  */
13557   if (qualified_p)
13558     cp_parser_error (parser,
13559                      "global qualification of class name is invalid");
13560   else if (invalid_nested_name_p)
13561     cp_parser_error (parser,
13562                      "qualified name does not name a class");
13563   else if (nested_name_specifier)
13564     {
13565       tree scope;
13566
13567       /* Reject typedef-names in class heads.  */
13568       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13569         {
13570           error ("invalid class name in declaration of %qD", type);
13571           type = NULL_TREE;
13572           goto done;
13573         }
13574
13575       /* Figure out in what scope the declaration is being placed.  */
13576       scope = current_scope ();
13577       /* If that scope does not contain the scope in which the
13578          class was originally declared, the program is invalid.  */
13579       if (scope && !is_ancestor (scope, nested_name_specifier))
13580         {
13581           error ("declaration of %qD in %qD which does not enclose %qD",
13582                  type, scope, nested_name_specifier);
13583           type = NULL_TREE;
13584           goto done;
13585         }
13586       /* [dcl.meaning]
13587
13588          A declarator-id shall not be qualified exception of the
13589          definition of a ... nested class outside of its class
13590          ... [or] a the definition or explicit instantiation of a
13591          class member of a namespace outside of its namespace.  */
13592       if (scope == nested_name_specifier)
13593         {
13594           pedwarn ("extra qualification ignored");
13595           nested_name_specifier = NULL_TREE;
13596           num_templates = 0;
13597         }
13598     }
13599   /* An explicit-specialization must be preceded by "template <>".  If
13600      it is not, try to recover gracefully.  */
13601   if (at_namespace_scope_p ()
13602       && parser->num_template_parameter_lists == 0
13603       && template_id_p)
13604     {
13605       error ("an explicit specialization must be preceded by %<template <>%>");
13606       invalid_explicit_specialization_p = true;
13607       /* Take the same action that would have been taken by
13608          cp_parser_explicit_specialization.  */
13609       ++parser->num_template_parameter_lists;
13610       begin_specialization ();
13611     }
13612   /* There must be no "return" statements between this point and the
13613      end of this function; set "type "to the correct return value and
13614      use "goto done;" to return.  */
13615   /* Make sure that the right number of template parameters were
13616      present.  */
13617   if (!cp_parser_check_template_parameters (parser, num_templates))
13618     {
13619       /* If something went wrong, there is no point in even trying to
13620          process the class-definition.  */
13621       type = NULL_TREE;
13622       goto done;
13623     }
13624
13625   /* Look up the type.  */
13626   if (template_id_p)
13627     {
13628       type = TREE_TYPE (id);
13629       type = maybe_process_partial_specialization (type);
13630       if (nested_name_specifier)
13631         pushed_scope = push_scope (nested_name_specifier);
13632     }
13633   else if (nested_name_specifier)
13634     {
13635       tree class_type;
13636
13637       /* Given:
13638
13639             template <typename T> struct S { struct T };
13640             template <typename T> struct S<T>::T { };
13641
13642          we will get a TYPENAME_TYPE when processing the definition of
13643          `S::T'.  We need to resolve it to the actual type before we
13644          try to define it.  */
13645       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13646         {
13647           class_type = resolve_typename_type (TREE_TYPE (type),
13648                                               /*only_current_p=*/false);
13649           if (class_type != error_mark_node)
13650             type = TYPE_NAME (class_type);
13651           else
13652             {
13653               cp_parser_error (parser, "could not resolve typename type");
13654               type = error_mark_node;
13655             }
13656         }
13657
13658       maybe_process_partial_specialization (TREE_TYPE (type));
13659       class_type = current_class_type;
13660       /* Enter the scope indicated by the nested-name-specifier.  */
13661       pushed_scope = push_scope (nested_name_specifier);
13662       /* Get the canonical version of this type.  */
13663       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13664       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13665           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13666         {
13667           type = push_template_decl (type);
13668           if (type == error_mark_node)
13669             {
13670               type = NULL_TREE;
13671               goto done;
13672             }
13673         }
13674
13675       type = TREE_TYPE (type);
13676       *nested_name_specifier_p = true;
13677     }
13678   else      /* The name is not a nested name.  */
13679     {
13680       /* If the class was unnamed, create a dummy name.  */
13681       if (!id)
13682         id = make_anon_name ();
13683       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13684                        parser->num_template_parameter_lists);
13685     }
13686
13687   /* Indicate whether this class was declared as a `class' or as a
13688      `struct'.  */
13689   if (TREE_CODE (type) == RECORD_TYPE)
13690     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13691   cp_parser_check_class_key (class_key, type);
13692
13693   /* If this type was already complete, and we see another definition,
13694      that's an error.  */
13695   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13696     {
13697       error ("redefinition of %q#T", type);
13698       error ("previous definition of %q+#T", type);
13699       type = NULL_TREE;
13700       goto done;
13701     }
13702   else if (type == error_mark_node)
13703     type = NULL_TREE;
13704
13705   /* We will have entered the scope containing the class; the names of
13706      base classes should be looked up in that context.  For example:
13707
13708        struct A { struct B {}; struct C; };
13709        struct A::C : B {};
13710
13711      is valid.  */
13712   *bases = NULL_TREE;
13713
13714   /* Get the list of base-classes, if there is one.  */
13715   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13716     *bases = cp_parser_base_clause (parser);
13717
13718  done:
13719   /* Leave the scope given by the nested-name-specifier.  We will
13720      enter the class scope itself while processing the members.  */
13721   if (pushed_scope)
13722     pop_scope (pushed_scope);
13723
13724   if (invalid_explicit_specialization_p)
13725     {
13726       end_specialization ();
13727       --parser->num_template_parameter_lists;
13728     }
13729   *attributes_p = attributes;
13730   return type;
13731 }
13732
13733 /* Parse a class-key.
13734
13735    class-key:
13736      class
13737      struct
13738      union
13739
13740    Returns the kind of class-key specified, or none_type to indicate
13741    error.  */
13742
13743 static enum tag_types
13744 cp_parser_class_key (cp_parser* parser)
13745 {
13746   cp_token *token;
13747   enum tag_types tag_type;
13748
13749   /* Look for the class-key.  */
13750   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13751   if (!token)
13752     return none_type;
13753
13754   /* Check to see if the TOKEN is a class-key.  */
13755   tag_type = cp_parser_token_is_class_key (token);
13756   if (!tag_type)
13757     cp_parser_error (parser, "expected class-key");
13758   return tag_type;
13759 }
13760
13761 /* Parse an (optional) member-specification.
13762
13763    member-specification:
13764      member-declaration member-specification [opt]
13765      access-specifier : member-specification [opt]  */
13766
13767 static void
13768 cp_parser_member_specification_opt (cp_parser* parser)
13769 {
13770   while (true)
13771     {
13772       cp_token *token;
13773       enum rid keyword;
13774
13775       /* Peek at the next token.  */
13776       token = cp_lexer_peek_token (parser->lexer);
13777       /* If it's a `}', or EOF then we've seen all the members.  */
13778       if (token->type == CPP_CLOSE_BRACE
13779           || token->type == CPP_EOF
13780           || token->type == CPP_PRAGMA_EOL)
13781         break;
13782
13783       /* See if this token is a keyword.  */
13784       keyword = token->keyword;
13785       switch (keyword)
13786         {
13787         case RID_PUBLIC:
13788         case RID_PROTECTED:
13789         case RID_PRIVATE:
13790           /* Consume the access-specifier.  */
13791           cp_lexer_consume_token (parser->lexer);
13792           /* Remember which access-specifier is active.  */
13793           current_access_specifier = token->u.value;
13794           /* Look for the `:'.  */
13795           cp_parser_require (parser, CPP_COLON, "`:'");
13796           break;
13797
13798         default:
13799           /* Accept #pragmas at class scope.  */
13800           if (token->type == CPP_PRAGMA)
13801             {
13802               cp_parser_pragma (parser, pragma_external);
13803               break;
13804             }
13805
13806           /* Otherwise, the next construction must be a
13807              member-declaration.  */
13808           cp_parser_member_declaration (parser);
13809         }
13810     }
13811 }
13812
13813 /* Parse a member-declaration.
13814
13815    member-declaration:
13816      decl-specifier-seq [opt] member-declarator-list [opt] ;
13817      function-definition ; [opt]
13818      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13819      using-declaration
13820      template-declaration
13821
13822    member-declarator-list:
13823      member-declarator
13824      member-declarator-list , member-declarator
13825
13826    member-declarator:
13827      declarator pure-specifier [opt]
13828      declarator constant-initializer [opt]
13829      identifier [opt] : constant-expression
13830
13831    GNU Extensions:
13832
13833    member-declaration:
13834      __extension__ member-declaration
13835
13836    member-declarator:
13837      declarator attributes [opt] pure-specifier [opt]
13838      declarator attributes [opt] constant-initializer [opt]
13839      identifier [opt] attributes [opt] : constant-expression  
13840
13841    C++0x Extensions:
13842
13843    member-declaration:
13844      static_assert-declaration  */
13845
13846 static void
13847 cp_parser_member_declaration (cp_parser* parser)
13848 {
13849   cp_decl_specifier_seq decl_specifiers;
13850   tree prefix_attributes;
13851   tree decl;
13852   int declares_class_or_enum;
13853   bool friend_p;
13854   cp_token *token;
13855   int saved_pedantic;
13856
13857   /* Check for the `__extension__' keyword.  */
13858   if (cp_parser_extension_opt (parser, &saved_pedantic))
13859     {
13860       /* Recurse.  */
13861       cp_parser_member_declaration (parser);
13862       /* Restore the old value of the PEDANTIC flag.  */
13863       pedantic = saved_pedantic;
13864
13865       return;
13866     }
13867
13868   /* Check for a template-declaration.  */
13869   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13870     {
13871       /* An explicit specialization here is an error condition, and we
13872          expect the specialization handler to detect and report this.  */
13873       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13874           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13875         cp_parser_explicit_specialization (parser);
13876       else
13877         cp_parser_template_declaration (parser, /*member_p=*/true);
13878
13879       return;
13880     }
13881
13882   /* Check for a using-declaration.  */
13883   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13884     {
13885       /* Parse the using-declaration.  */
13886       cp_parser_using_declaration (parser,
13887                                    /*access_declaration_p=*/false);
13888       return;
13889     }
13890
13891   /* Check for @defs.  */
13892   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13893     {
13894       tree ivar, member;
13895       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13896       ivar = ivar_chains;
13897       while (ivar)
13898         {
13899           member = ivar;
13900           ivar = TREE_CHAIN (member);
13901           TREE_CHAIN (member) = NULL_TREE;
13902           finish_member_declaration (member);
13903         }
13904       return;
13905     }
13906
13907   /* If the next token is `static_assert' we have a static assertion.  */
13908   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
13909     {
13910       cp_parser_static_assert (parser, /*member_p=*/true);
13911       return;
13912     }
13913
13914   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13915     return;
13916
13917   /* Parse the decl-specifier-seq.  */
13918   cp_parser_decl_specifier_seq (parser,
13919                                 CP_PARSER_FLAGS_OPTIONAL,
13920                                 &decl_specifiers,
13921                                 &declares_class_or_enum);
13922   prefix_attributes = decl_specifiers.attributes;
13923   decl_specifiers.attributes = NULL_TREE;
13924   /* Check for an invalid type-name.  */
13925   if (!decl_specifiers.type
13926       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13927     return;
13928   /* If there is no declarator, then the decl-specifier-seq should
13929      specify a type.  */
13930   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13931     {
13932       /* If there was no decl-specifier-seq, and the next token is a
13933          `;', then we have something like:
13934
13935            struct S { ; };
13936
13937          [class.mem]
13938
13939          Each member-declaration shall declare at least one member
13940          name of the class.  */
13941       if (!decl_specifiers.any_specifiers_p)
13942         {
13943           cp_token *token = cp_lexer_peek_token (parser->lexer);
13944           if (pedantic && !token->in_system_header)
13945             pedwarn ("%Hextra %<;%>", &token->location);
13946         }
13947       else
13948         {
13949           tree type;
13950
13951           /* See if this declaration is a friend.  */
13952           friend_p = cp_parser_friend_p (&decl_specifiers);
13953           /* If there were decl-specifiers, check to see if there was
13954              a class-declaration.  */
13955           type = check_tag_decl (&decl_specifiers);
13956           /* Nested classes have already been added to the class, but
13957              a `friend' needs to be explicitly registered.  */
13958           if (friend_p)
13959             {
13960               /* If the `friend' keyword was present, the friend must
13961                  be introduced with a class-key.  */
13962                if (!declares_class_or_enum)
13963                  error ("a class-key must be used when declaring a friend");
13964                /* In this case:
13965
13966                     template <typename T> struct A {
13967                       friend struct A<T>::B;
13968                     };
13969
13970                   A<T>::B will be represented by a TYPENAME_TYPE, and
13971                   therefore not recognized by check_tag_decl.  */
13972                if (!type
13973                    && decl_specifiers.type
13974                    && TYPE_P (decl_specifiers.type))
13975                  type = decl_specifiers.type;
13976                if (!type || !TYPE_P (type))
13977                  error ("friend declaration does not name a class or "
13978                         "function");
13979                else
13980                  make_friend_class (current_class_type, type,
13981                                     /*complain=*/true);
13982             }
13983           /* If there is no TYPE, an error message will already have
13984              been issued.  */
13985           else if (!type || type == error_mark_node)
13986             ;
13987           /* An anonymous aggregate has to be handled specially; such
13988              a declaration really declares a data member (with a
13989              particular type), as opposed to a nested class.  */
13990           else if (ANON_AGGR_TYPE_P (type))
13991             {
13992               /* Remove constructors and such from TYPE, now that we
13993                  know it is an anonymous aggregate.  */
13994               fixup_anonymous_aggr (type);
13995               /* And make the corresponding data member.  */
13996               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13997               /* Add it to the class.  */
13998               finish_member_declaration (decl);
13999             }
14000           else
14001             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14002         }
14003     }
14004   else
14005     {
14006       /* See if these declarations will be friends.  */
14007       friend_p = cp_parser_friend_p (&decl_specifiers);
14008
14009       /* Keep going until we hit the `;' at the end of the
14010          declaration.  */
14011       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14012         {
14013           tree attributes = NULL_TREE;
14014           tree first_attribute;
14015
14016           /* Peek at the next token.  */
14017           token = cp_lexer_peek_token (parser->lexer);
14018
14019           /* Check for a bitfield declaration.  */
14020           if (token->type == CPP_COLON
14021               || (token->type == CPP_NAME
14022                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14023                   == CPP_COLON))
14024             {
14025               tree identifier;
14026               tree width;
14027
14028               /* Get the name of the bitfield.  Note that we cannot just
14029                  check TOKEN here because it may have been invalidated by
14030                  the call to cp_lexer_peek_nth_token above.  */
14031               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14032                 identifier = cp_parser_identifier (parser);
14033               else
14034                 identifier = NULL_TREE;
14035
14036               /* Consume the `:' token.  */
14037               cp_lexer_consume_token (parser->lexer);
14038               /* Get the width of the bitfield.  */
14039               width
14040                 = cp_parser_constant_expression (parser,
14041                                                  /*allow_non_constant=*/false,
14042                                                  NULL);
14043
14044               /* Look for attributes that apply to the bitfield.  */
14045               attributes = cp_parser_attributes_opt (parser);
14046               /* Remember which attributes are prefix attributes and
14047                  which are not.  */
14048               first_attribute = attributes;
14049               /* Combine the attributes.  */
14050               attributes = chainon (prefix_attributes, attributes);
14051
14052               /* Create the bitfield declaration.  */
14053               decl = grokbitfield (identifier
14054                                    ? make_id_declarator (NULL_TREE,
14055                                                          identifier,
14056                                                          sfk_none)
14057                                    : NULL,
14058                                    &decl_specifiers,
14059                                    width);
14060               /* Apply the attributes.  */
14061               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14062             }
14063           else
14064             {
14065               cp_declarator *declarator;
14066               tree initializer;
14067               tree asm_specification;
14068               int ctor_dtor_or_conv_p;
14069
14070               /* Parse the declarator.  */
14071               declarator
14072                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14073                                         &ctor_dtor_or_conv_p,
14074                                         /*parenthesized_p=*/NULL,
14075                                         /*member_p=*/true);
14076
14077               /* If something went wrong parsing the declarator, make sure
14078                  that we at least consume some tokens.  */
14079               if (declarator == cp_error_declarator)
14080                 {
14081                   /* Skip to the end of the statement.  */
14082                   cp_parser_skip_to_end_of_statement (parser);
14083                   /* If the next token is not a semicolon, that is
14084                      probably because we just skipped over the body of
14085                      a function.  So, we consume a semicolon if
14086                      present, but do not issue an error message if it
14087                      is not present.  */
14088                   if (cp_lexer_next_token_is (parser->lexer,
14089                                               CPP_SEMICOLON))
14090                     cp_lexer_consume_token (parser->lexer);
14091                   return;
14092                 }
14093
14094               if (declares_class_or_enum & 2)
14095                 cp_parser_check_for_definition_in_return_type
14096                   (declarator, decl_specifiers.type);
14097
14098               /* Look for an asm-specification.  */
14099               asm_specification = cp_parser_asm_specification_opt (parser);
14100               /* Look for attributes that apply to the declaration.  */
14101               attributes = cp_parser_attributes_opt (parser);
14102               /* Remember which attributes are prefix attributes and
14103                  which are not.  */
14104               first_attribute = attributes;
14105               /* Combine the attributes.  */
14106               attributes = chainon (prefix_attributes, attributes);
14107
14108               /* If it's an `=', then we have a constant-initializer or a
14109                  pure-specifier.  It is not correct to parse the
14110                  initializer before registering the member declaration
14111                  since the member declaration should be in scope while
14112                  its initializer is processed.  However, the rest of the
14113                  front end does not yet provide an interface that allows
14114                  us to handle this correctly.  */
14115               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14116                 {
14117                   /* In [class.mem]:
14118
14119                      A pure-specifier shall be used only in the declaration of
14120                      a virtual function.
14121
14122                      A member-declarator can contain a constant-initializer
14123                      only if it declares a static member of integral or
14124                      enumeration type.
14125
14126                      Therefore, if the DECLARATOR is for a function, we look
14127                      for a pure-specifier; otherwise, we look for a
14128                      constant-initializer.  When we call `grokfield', it will
14129                      perform more stringent semantics checks.  */
14130                   if (function_declarator_p (declarator))
14131                     initializer = cp_parser_pure_specifier (parser);
14132                   else
14133                     /* Parse the initializer.  */
14134                     initializer = cp_parser_constant_initializer (parser);
14135                 }
14136               /* Otherwise, there is no initializer.  */
14137               else
14138                 initializer = NULL_TREE;
14139
14140               /* See if we are probably looking at a function
14141                  definition.  We are certainly not looking at a
14142                  member-declarator.  Calling `grokfield' has
14143                  side-effects, so we must not do it unless we are sure
14144                  that we are looking at a member-declarator.  */
14145               if (cp_parser_token_starts_function_definition_p
14146                   (cp_lexer_peek_token (parser->lexer)))
14147                 {
14148                   /* The grammar does not allow a pure-specifier to be
14149                      used when a member function is defined.  (It is
14150                      possible that this fact is an oversight in the
14151                      standard, since a pure function may be defined
14152                      outside of the class-specifier.  */
14153                   if (initializer)
14154                     error ("pure-specifier on function-definition");
14155                   decl = cp_parser_save_member_function_body (parser,
14156                                                               &decl_specifiers,
14157                                                               declarator,
14158                                                               attributes);
14159                   /* If the member was not a friend, declare it here.  */
14160                   if (!friend_p)
14161                     finish_member_declaration (decl);
14162                   /* Peek at the next token.  */
14163                   token = cp_lexer_peek_token (parser->lexer);
14164                   /* If the next token is a semicolon, consume it.  */
14165                   if (token->type == CPP_SEMICOLON)
14166                     cp_lexer_consume_token (parser->lexer);
14167                   return;
14168                 }
14169               else
14170                 /* Create the declaration.  */
14171                 decl = grokfield (declarator, &decl_specifiers,
14172                                   initializer, /*init_const_expr_p=*/true,
14173                                   asm_specification,
14174                                   attributes);
14175             }
14176
14177           /* Reset PREFIX_ATTRIBUTES.  */
14178           while (attributes && TREE_CHAIN (attributes) != first_attribute)
14179             attributes = TREE_CHAIN (attributes);
14180           if (attributes)
14181             TREE_CHAIN (attributes) = NULL_TREE;
14182
14183           /* If there is any qualification still in effect, clear it
14184              now; we will be starting fresh with the next declarator.  */
14185           parser->scope = NULL_TREE;
14186           parser->qualifying_scope = NULL_TREE;
14187           parser->object_scope = NULL_TREE;
14188           /* If it's a `,', then there are more declarators.  */
14189           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14190             cp_lexer_consume_token (parser->lexer);
14191           /* If the next token isn't a `;', then we have a parse error.  */
14192           else if (cp_lexer_next_token_is_not (parser->lexer,
14193                                                CPP_SEMICOLON))
14194             {
14195               cp_parser_error (parser, "expected %<;%>");
14196               /* Skip tokens until we find a `;'.  */
14197               cp_parser_skip_to_end_of_statement (parser);
14198
14199               break;
14200             }
14201
14202           if (decl)
14203             {
14204               /* Add DECL to the list of members.  */
14205               if (!friend_p)
14206                 finish_member_declaration (decl);
14207
14208               if (TREE_CODE (decl) == FUNCTION_DECL)
14209                 cp_parser_save_default_args (parser, decl);
14210             }
14211         }
14212     }
14213
14214   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14215 }
14216
14217 /* Parse a pure-specifier.
14218
14219    pure-specifier:
14220      = 0
14221
14222    Returns INTEGER_ZERO_NODE if a pure specifier is found.
14223    Otherwise, ERROR_MARK_NODE is returned.  */
14224
14225 static tree
14226 cp_parser_pure_specifier (cp_parser* parser)
14227 {
14228   cp_token *token;
14229
14230   /* Look for the `=' token.  */
14231   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14232     return error_mark_node;
14233   /* Look for the `0' token.  */
14234   token = cp_lexer_consume_token (parser->lexer);
14235   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
14236   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14237     {
14238       cp_parser_error (parser,
14239                        "invalid pure specifier (only `= 0' is allowed)");
14240       cp_parser_skip_to_end_of_statement (parser);
14241       return error_mark_node;
14242     }
14243   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14244     {
14245       error ("templates may not be %<virtual%>");
14246       return error_mark_node;
14247     }
14248
14249   return integer_zero_node;
14250 }
14251
14252 /* Parse a constant-initializer.
14253
14254    constant-initializer:
14255      = constant-expression
14256
14257    Returns a representation of the constant-expression.  */
14258
14259 static tree
14260 cp_parser_constant_initializer (cp_parser* parser)
14261 {
14262   /* Look for the `=' token.  */
14263   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14264     return error_mark_node;
14265
14266   /* It is invalid to write:
14267
14268        struct S { static const int i = { 7 }; };
14269
14270      */
14271   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14272     {
14273       cp_parser_error (parser,
14274                        "a brace-enclosed initializer is not allowed here");
14275       /* Consume the opening brace.  */
14276       cp_lexer_consume_token (parser->lexer);
14277       /* Skip the initializer.  */
14278       cp_parser_skip_to_closing_brace (parser);
14279       /* Look for the trailing `}'.  */
14280       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14281
14282       return error_mark_node;
14283     }
14284
14285   return cp_parser_constant_expression (parser,
14286                                         /*allow_non_constant=*/false,
14287                                         NULL);
14288 }
14289
14290 /* Derived classes [gram.class.derived] */
14291
14292 /* Parse a base-clause.
14293
14294    base-clause:
14295      : base-specifier-list
14296
14297    base-specifier-list:
14298      base-specifier
14299      base-specifier-list , base-specifier
14300
14301    Returns a TREE_LIST representing the base-classes, in the order in
14302    which they were declared.  The representation of each node is as
14303    described by cp_parser_base_specifier.
14304
14305    In the case that no bases are specified, this function will return
14306    NULL_TREE, not ERROR_MARK_NODE.  */
14307
14308 static tree
14309 cp_parser_base_clause (cp_parser* parser)
14310 {
14311   tree bases = NULL_TREE;
14312
14313   /* Look for the `:' that begins the list.  */
14314   cp_parser_require (parser, CPP_COLON, "`:'");
14315
14316   /* Scan the base-specifier-list.  */
14317   while (true)
14318     {
14319       cp_token *token;
14320       tree base;
14321
14322       /* Look for the base-specifier.  */
14323       base = cp_parser_base_specifier (parser);
14324       /* Add BASE to the front of the list.  */
14325       if (base != error_mark_node)
14326         {
14327           TREE_CHAIN (base) = bases;
14328           bases = base;
14329         }
14330       /* Peek at the next token.  */
14331       token = cp_lexer_peek_token (parser->lexer);
14332       /* If it's not a comma, then the list is complete.  */
14333       if (token->type != CPP_COMMA)
14334         break;
14335       /* Consume the `,'.  */
14336       cp_lexer_consume_token (parser->lexer);
14337     }
14338
14339   /* PARSER->SCOPE may still be non-NULL at this point, if the last
14340      base class had a qualified name.  However, the next name that
14341      appears is certainly not qualified.  */
14342   parser->scope = NULL_TREE;
14343   parser->qualifying_scope = NULL_TREE;
14344   parser->object_scope = NULL_TREE;
14345
14346   return nreverse (bases);
14347 }
14348
14349 /* Parse a base-specifier.
14350
14351    base-specifier:
14352      :: [opt] nested-name-specifier [opt] class-name
14353      virtual access-specifier [opt] :: [opt] nested-name-specifier
14354        [opt] class-name
14355      access-specifier virtual [opt] :: [opt] nested-name-specifier
14356        [opt] class-name
14357
14358    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14359    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14360    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14361    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14362
14363 static tree
14364 cp_parser_base_specifier (cp_parser* parser)
14365 {
14366   cp_token *token;
14367   bool done = false;
14368   bool virtual_p = false;
14369   bool duplicate_virtual_error_issued_p = false;
14370   bool duplicate_access_error_issued_p = false;
14371   bool class_scope_p, template_p;
14372   tree access = access_default_node;
14373   tree type;
14374
14375   /* Process the optional `virtual' and `access-specifier'.  */
14376   while (!done)
14377     {
14378       /* Peek at the next token.  */
14379       token = cp_lexer_peek_token (parser->lexer);
14380       /* Process `virtual'.  */
14381       switch (token->keyword)
14382         {
14383         case RID_VIRTUAL:
14384           /* If `virtual' appears more than once, issue an error.  */
14385           if (virtual_p && !duplicate_virtual_error_issued_p)
14386             {
14387               cp_parser_error (parser,
14388                                "%<virtual%> specified more than once in base-specified");
14389               duplicate_virtual_error_issued_p = true;
14390             }
14391
14392           virtual_p = true;
14393
14394           /* Consume the `virtual' token.  */
14395           cp_lexer_consume_token (parser->lexer);
14396
14397           break;
14398
14399         case RID_PUBLIC:
14400         case RID_PROTECTED:
14401         case RID_PRIVATE:
14402           /* If more than one access specifier appears, issue an
14403              error.  */
14404           if (access != access_default_node
14405               && !duplicate_access_error_issued_p)
14406             {
14407               cp_parser_error (parser,
14408                                "more than one access specifier in base-specified");
14409               duplicate_access_error_issued_p = true;
14410             }
14411
14412           access = ridpointers[(int) token->keyword];
14413
14414           /* Consume the access-specifier.  */
14415           cp_lexer_consume_token (parser->lexer);
14416
14417           break;
14418
14419         default:
14420           done = true;
14421           break;
14422         }
14423     }
14424   /* It is not uncommon to see programs mechanically, erroneously, use
14425      the 'typename' keyword to denote (dependent) qualified types
14426      as base classes.  */
14427   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14428     {
14429       if (!processing_template_decl)
14430         error ("keyword %<typename%> not allowed outside of templates");
14431       else
14432         error ("keyword %<typename%> not allowed in this context "
14433                "(the base class is implicitly a type)");
14434       cp_lexer_consume_token (parser->lexer);
14435     }
14436
14437   /* Look for the optional `::' operator.  */
14438   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14439   /* Look for the nested-name-specifier.  The simplest way to
14440      implement:
14441
14442        [temp.res]
14443
14444        The keyword `typename' is not permitted in a base-specifier or
14445        mem-initializer; in these contexts a qualified name that
14446        depends on a template-parameter is implicitly assumed to be a
14447        type name.
14448
14449      is to pretend that we have seen the `typename' keyword at this
14450      point.  */
14451   cp_parser_nested_name_specifier_opt (parser,
14452                                        /*typename_keyword_p=*/true,
14453                                        /*check_dependency_p=*/true,
14454                                        typename_type,
14455                                        /*is_declaration=*/true);
14456   /* If the base class is given by a qualified name, assume that names
14457      we see are type names or templates, as appropriate.  */
14458   class_scope_p = (parser->scope && TYPE_P (parser->scope));
14459   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14460
14461   /* Finally, look for the class-name.  */
14462   type = cp_parser_class_name (parser,
14463                                class_scope_p,
14464                                template_p,
14465                                typename_type,
14466                                /*check_dependency_p=*/true,
14467                                /*class_head_p=*/false,
14468                                /*is_declaration=*/true);
14469
14470   if (type == error_mark_node)
14471     return error_mark_node;
14472
14473   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14474 }
14475
14476 /* Exception handling [gram.exception] */
14477
14478 /* Parse an (optional) exception-specification.
14479
14480    exception-specification:
14481      throw ( type-id-list [opt] )
14482
14483    Returns a TREE_LIST representing the exception-specification.  The
14484    TREE_VALUE of each node is a type.  */
14485
14486 static tree
14487 cp_parser_exception_specification_opt (cp_parser* parser)
14488 {
14489   cp_token *token;
14490   tree type_id_list;
14491
14492   /* Peek at the next token.  */
14493   token = cp_lexer_peek_token (parser->lexer);
14494   /* If it's not `throw', then there's no exception-specification.  */
14495   if (!cp_parser_is_keyword (token, RID_THROW))
14496     return NULL_TREE;
14497
14498   /* Consume the `throw'.  */
14499   cp_lexer_consume_token (parser->lexer);
14500
14501   /* Look for the `('.  */
14502   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14503
14504   /* Peek at the next token.  */
14505   token = cp_lexer_peek_token (parser->lexer);
14506   /* If it's not a `)', then there is a type-id-list.  */
14507   if (token->type != CPP_CLOSE_PAREN)
14508     {
14509       const char *saved_message;
14510
14511       /* Types may not be defined in an exception-specification.  */
14512       saved_message = parser->type_definition_forbidden_message;
14513       parser->type_definition_forbidden_message
14514         = "types may not be defined in an exception-specification";
14515       /* Parse the type-id-list.  */
14516       type_id_list = cp_parser_type_id_list (parser);
14517       /* Restore the saved message.  */
14518       parser->type_definition_forbidden_message = saved_message;
14519     }
14520   else
14521     type_id_list = empty_except_spec;
14522
14523   /* Look for the `)'.  */
14524   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14525
14526   return type_id_list;
14527 }
14528
14529 /* Parse an (optional) type-id-list.
14530
14531    type-id-list:
14532      type-id
14533      type-id-list , type-id
14534
14535    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14536    in the order that the types were presented.  */
14537
14538 static tree
14539 cp_parser_type_id_list (cp_parser* parser)
14540 {
14541   tree types = NULL_TREE;
14542
14543   while (true)
14544     {
14545       cp_token *token;
14546       tree type;
14547
14548       /* Get the next type-id.  */
14549       type = cp_parser_type_id (parser);
14550       /* Add it to the list.  */
14551       types = add_exception_specifier (types, type, /*complain=*/1);
14552       /* Peek at the next token.  */
14553       token = cp_lexer_peek_token (parser->lexer);
14554       /* If it is not a `,', we are done.  */
14555       if (token->type != CPP_COMMA)
14556         break;
14557       /* Consume the `,'.  */
14558       cp_lexer_consume_token (parser->lexer);
14559     }
14560
14561   return nreverse (types);
14562 }
14563
14564 /* Parse a try-block.
14565
14566    try-block:
14567      try compound-statement handler-seq  */
14568
14569 static tree
14570 cp_parser_try_block (cp_parser* parser)
14571 {
14572   tree try_block;
14573
14574   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14575   try_block = begin_try_block ();
14576   cp_parser_compound_statement (parser, NULL, true);
14577   finish_try_block (try_block);
14578   cp_parser_handler_seq (parser);
14579   finish_handler_sequence (try_block);
14580
14581   return try_block;
14582 }
14583
14584 /* Parse a function-try-block.
14585
14586    function-try-block:
14587      try ctor-initializer [opt] function-body handler-seq  */
14588
14589 static bool
14590 cp_parser_function_try_block (cp_parser* parser)
14591 {
14592   tree compound_stmt;
14593   tree try_block;
14594   bool ctor_initializer_p;
14595
14596   /* Look for the `try' keyword.  */
14597   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14598     return false;
14599   /* Let the rest of the front-end know where we are.  */
14600   try_block = begin_function_try_block (&compound_stmt);
14601   /* Parse the function-body.  */
14602   ctor_initializer_p
14603     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14604   /* We're done with the `try' part.  */
14605   finish_function_try_block (try_block);
14606   /* Parse the handlers.  */
14607   cp_parser_handler_seq (parser);
14608   /* We're done with the handlers.  */
14609   finish_function_handler_sequence (try_block, compound_stmt);
14610
14611   return ctor_initializer_p;
14612 }
14613
14614 /* Parse a handler-seq.
14615
14616    handler-seq:
14617      handler handler-seq [opt]  */
14618
14619 static void
14620 cp_parser_handler_seq (cp_parser* parser)
14621 {
14622   while (true)
14623     {
14624       cp_token *token;
14625
14626       /* Parse the handler.  */
14627       cp_parser_handler (parser);
14628       /* Peek at the next token.  */
14629       token = cp_lexer_peek_token (parser->lexer);
14630       /* If it's not `catch' then there are no more handlers.  */
14631       if (!cp_parser_is_keyword (token, RID_CATCH))
14632         break;
14633     }
14634 }
14635
14636 /* Parse a handler.
14637
14638    handler:
14639      catch ( exception-declaration ) compound-statement  */
14640
14641 static void
14642 cp_parser_handler (cp_parser* parser)
14643 {
14644   tree handler;
14645   tree declaration;
14646
14647   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14648   handler = begin_handler ();
14649   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14650   declaration = cp_parser_exception_declaration (parser);
14651   finish_handler_parms (declaration, handler);
14652   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14653   cp_parser_compound_statement (parser, NULL, false);
14654   finish_handler (handler);
14655 }
14656
14657 /* Parse an exception-declaration.
14658
14659    exception-declaration:
14660      type-specifier-seq declarator
14661      type-specifier-seq abstract-declarator
14662      type-specifier-seq
14663      ...
14664
14665    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14666    ellipsis variant is used.  */
14667
14668 static tree
14669 cp_parser_exception_declaration (cp_parser* parser)
14670 {
14671   cp_decl_specifier_seq type_specifiers;
14672   cp_declarator *declarator;
14673   const char *saved_message;
14674
14675   /* If it's an ellipsis, it's easy to handle.  */
14676   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14677     {
14678       /* Consume the `...' token.  */
14679       cp_lexer_consume_token (parser->lexer);
14680       return NULL_TREE;
14681     }
14682
14683   /* Types may not be defined in exception-declarations.  */
14684   saved_message = parser->type_definition_forbidden_message;
14685   parser->type_definition_forbidden_message
14686     = "types may not be defined in exception-declarations";
14687
14688   /* Parse the type-specifier-seq.  */
14689   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14690                                 &type_specifiers);
14691   /* If it's a `)', then there is no declarator.  */
14692   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14693     declarator = NULL;
14694   else
14695     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14696                                        /*ctor_dtor_or_conv_p=*/NULL,
14697                                        /*parenthesized_p=*/NULL,
14698                                        /*member_p=*/false);
14699
14700   /* Restore the saved message.  */
14701   parser->type_definition_forbidden_message = saved_message;
14702
14703   if (!type_specifiers.any_specifiers_p)
14704     return error_mark_node;
14705
14706   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14707 }
14708
14709 /* Parse a throw-expression.
14710
14711    throw-expression:
14712      throw assignment-expression [opt]
14713
14714    Returns a THROW_EXPR representing the throw-expression.  */
14715
14716 static tree
14717 cp_parser_throw_expression (cp_parser* parser)
14718 {
14719   tree expression;
14720   cp_token* token;
14721
14722   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14723   token = cp_lexer_peek_token (parser->lexer);
14724   /* Figure out whether or not there is an assignment-expression
14725      following the "throw" keyword.  */
14726   if (token->type == CPP_COMMA
14727       || token->type == CPP_SEMICOLON
14728       || token->type == CPP_CLOSE_PAREN
14729       || token->type == CPP_CLOSE_SQUARE
14730       || token->type == CPP_CLOSE_BRACE
14731       || token->type == CPP_COLON)
14732     expression = NULL_TREE;
14733   else
14734     expression = cp_parser_assignment_expression (parser,
14735                                                   /*cast_p=*/false);
14736
14737   return build_throw (expression);
14738 }
14739
14740 /* GNU Extensions */
14741
14742 /* Parse an (optional) asm-specification.
14743
14744    asm-specification:
14745      asm ( string-literal )
14746
14747    If the asm-specification is present, returns a STRING_CST
14748    corresponding to the string-literal.  Otherwise, returns
14749    NULL_TREE.  */
14750
14751 static tree
14752 cp_parser_asm_specification_opt (cp_parser* parser)
14753 {
14754   cp_token *token;
14755   tree asm_specification;
14756
14757   /* Peek at the next token.  */
14758   token = cp_lexer_peek_token (parser->lexer);
14759   /* If the next token isn't the `asm' keyword, then there's no
14760      asm-specification.  */
14761   if (!cp_parser_is_keyword (token, RID_ASM))
14762     return NULL_TREE;
14763
14764   /* Consume the `asm' token.  */
14765   cp_lexer_consume_token (parser->lexer);
14766   /* Look for the `('.  */
14767   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14768
14769   /* Look for the string-literal.  */
14770   asm_specification = cp_parser_string_literal (parser, false, false);
14771
14772   /* Look for the `)'.  */
14773   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14774
14775   return asm_specification;
14776 }
14777
14778 /* Parse an asm-operand-list.
14779
14780    asm-operand-list:
14781      asm-operand
14782      asm-operand-list , asm-operand
14783
14784    asm-operand:
14785      string-literal ( expression )
14786      [ string-literal ] string-literal ( expression )
14787
14788    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14789    each node is the expression.  The TREE_PURPOSE is itself a
14790    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14791    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14792    is a STRING_CST for the string literal before the parenthesis.  */
14793
14794 static tree
14795 cp_parser_asm_operand_list (cp_parser* parser)
14796 {
14797   tree asm_operands = NULL_TREE;
14798
14799   while (true)
14800     {
14801       tree string_literal;
14802       tree expression;
14803       tree name;
14804
14805       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14806         {
14807           /* Consume the `[' token.  */
14808           cp_lexer_consume_token (parser->lexer);
14809           /* Read the operand name.  */
14810           name = cp_parser_identifier (parser);
14811           if (name != error_mark_node)
14812             name = build_string (IDENTIFIER_LENGTH (name),
14813                                  IDENTIFIER_POINTER (name));
14814           /* Look for the closing `]'.  */
14815           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14816         }
14817       else
14818         name = NULL_TREE;
14819       /* Look for the string-literal.  */
14820       string_literal = cp_parser_string_literal (parser, false, false);
14821
14822       /* Look for the `('.  */
14823       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14824       /* Parse the expression.  */
14825       expression = cp_parser_expression (parser, /*cast_p=*/false);
14826       /* Look for the `)'.  */
14827       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14828
14829       /* Add this operand to the list.  */
14830       asm_operands = tree_cons (build_tree_list (name, string_literal),
14831                                 expression,
14832                                 asm_operands);
14833       /* If the next token is not a `,', there are no more
14834          operands.  */
14835       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14836         break;
14837       /* Consume the `,'.  */
14838       cp_lexer_consume_token (parser->lexer);
14839     }
14840
14841   return nreverse (asm_operands);
14842 }
14843
14844 /* Parse an asm-clobber-list.
14845
14846    asm-clobber-list:
14847      string-literal
14848      asm-clobber-list , string-literal
14849
14850    Returns a TREE_LIST, indicating the clobbers in the order that they
14851    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14852
14853 static tree
14854 cp_parser_asm_clobber_list (cp_parser* parser)
14855 {
14856   tree clobbers = NULL_TREE;
14857
14858   while (true)
14859     {
14860       tree string_literal;
14861
14862       /* Look for the string literal.  */
14863       string_literal = cp_parser_string_literal (parser, false, false);
14864       /* Add it to the list.  */
14865       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14866       /* If the next token is not a `,', then the list is
14867          complete.  */
14868       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14869         break;
14870       /* Consume the `,' token.  */
14871       cp_lexer_consume_token (parser->lexer);
14872     }
14873
14874   return clobbers;
14875 }
14876
14877 /* Parse an (optional) series of attributes.
14878
14879    attributes:
14880      attributes attribute
14881
14882    attribute:
14883      __attribute__ (( attribute-list [opt] ))
14884
14885    The return value is as for cp_parser_attribute_list.  */
14886
14887 static tree
14888 cp_parser_attributes_opt (cp_parser* parser)
14889 {
14890   tree attributes = NULL_TREE;
14891
14892   while (true)
14893     {
14894       cp_token *token;
14895       tree attribute_list;
14896
14897       /* Peek at the next token.  */
14898       token = cp_lexer_peek_token (parser->lexer);
14899       /* If it's not `__attribute__', then we're done.  */
14900       if (token->keyword != RID_ATTRIBUTE)
14901         break;
14902
14903       /* Consume the `__attribute__' keyword.  */
14904       cp_lexer_consume_token (parser->lexer);
14905       /* Look for the two `(' tokens.  */
14906       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14907       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14908
14909       /* Peek at the next token.  */
14910       token = cp_lexer_peek_token (parser->lexer);
14911       if (token->type != CPP_CLOSE_PAREN)
14912         /* Parse the attribute-list.  */
14913         attribute_list = cp_parser_attribute_list (parser);
14914       else
14915         /* If the next token is a `)', then there is no attribute
14916            list.  */
14917         attribute_list = NULL;
14918
14919       /* Look for the two `)' tokens.  */
14920       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14921       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14922
14923       /* Add these new attributes to the list.  */
14924       attributes = chainon (attributes, attribute_list);
14925     }
14926
14927   return attributes;
14928 }
14929
14930 /* Parse an attribute-list.
14931
14932    attribute-list:
14933      attribute
14934      attribute-list , attribute
14935
14936    attribute:
14937      identifier
14938      identifier ( identifier )
14939      identifier ( identifier , expression-list )
14940      identifier ( expression-list )
14941
14942    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14943    to an attribute.  The TREE_PURPOSE of each node is the identifier
14944    indicating which attribute is in use.  The TREE_VALUE represents
14945    the arguments, if any.  */
14946
14947 static tree
14948 cp_parser_attribute_list (cp_parser* parser)
14949 {
14950   tree attribute_list = NULL_TREE;
14951   bool save_translate_strings_p = parser->translate_strings_p;
14952
14953   parser->translate_strings_p = false;
14954   while (true)
14955     {
14956       cp_token *token;
14957       tree identifier;
14958       tree attribute;
14959
14960       /* Look for the identifier.  We also allow keywords here; for
14961          example `__attribute__ ((const))' is legal.  */
14962       token = cp_lexer_peek_token (parser->lexer);
14963       if (token->type == CPP_NAME
14964           || token->type == CPP_KEYWORD)
14965         {
14966           tree arguments = NULL_TREE;
14967
14968           /* Consume the token.  */
14969           token = cp_lexer_consume_token (parser->lexer);
14970
14971           /* Save away the identifier that indicates which attribute
14972              this is.  */
14973           identifier = token->u.value;
14974           attribute = build_tree_list (identifier, NULL_TREE);
14975
14976           /* Peek at the next token.  */
14977           token = cp_lexer_peek_token (parser->lexer);
14978           /* If it's an `(', then parse the attribute arguments.  */
14979           if (token->type == CPP_OPEN_PAREN)
14980             {
14981               arguments = cp_parser_parenthesized_expression_list
14982                           (parser, true, /*cast_p=*/false,
14983                            /*non_constant_p=*/NULL);
14984               /* Save the arguments away.  */
14985               TREE_VALUE (attribute) = arguments;
14986             }
14987
14988           if (arguments != error_mark_node)
14989             {
14990               /* Add this attribute to the list.  */
14991               TREE_CHAIN (attribute) = attribute_list;
14992               attribute_list = attribute;
14993             }
14994
14995           token = cp_lexer_peek_token (parser->lexer);
14996         }
14997       /* Now, look for more attributes.  If the next token isn't a
14998          `,', we're done.  */
14999       if (token->type != CPP_COMMA)
15000         break;
15001
15002       /* Consume the comma and keep going.  */
15003       cp_lexer_consume_token (parser->lexer);
15004     }
15005   parser->translate_strings_p = save_translate_strings_p;
15006
15007   /* We built up the list in reverse order.  */
15008   return nreverse (attribute_list);
15009 }
15010
15011 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
15012    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
15013    current value of the PEDANTIC flag, regardless of whether or not
15014    the `__extension__' keyword is present.  The caller is responsible
15015    for restoring the value of the PEDANTIC flag.  */
15016
15017 static bool
15018 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15019 {
15020   /* Save the old value of the PEDANTIC flag.  */
15021   *saved_pedantic = pedantic;
15022
15023   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15024     {
15025       /* Consume the `__extension__' token.  */
15026       cp_lexer_consume_token (parser->lexer);
15027       /* We're not being pedantic while the `__extension__' keyword is
15028          in effect.  */
15029       pedantic = 0;
15030
15031       return true;
15032     }
15033
15034   return false;
15035 }
15036
15037 /* Parse a label declaration.
15038
15039    label-declaration:
15040      __label__ label-declarator-seq ;
15041
15042    label-declarator-seq:
15043      identifier , label-declarator-seq
15044      identifier  */
15045
15046 static void
15047 cp_parser_label_declaration (cp_parser* parser)
15048 {
15049   /* Look for the `__label__' keyword.  */
15050   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15051
15052   while (true)
15053     {
15054       tree identifier;
15055
15056       /* Look for an identifier.  */
15057       identifier = cp_parser_identifier (parser);
15058       /* If we failed, stop.  */
15059       if (identifier == error_mark_node)
15060         break;
15061       /* Declare it as a label.  */
15062       finish_label_decl (identifier);
15063       /* If the next token is a `;', stop.  */
15064       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15065         break;
15066       /* Look for the `,' separating the label declarations.  */
15067       cp_parser_require (parser, CPP_COMMA, "`,'");
15068     }
15069
15070   /* Look for the final `;'.  */
15071   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15072 }
15073
15074 /* Support Functions */
15075
15076 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15077    NAME should have one of the representations used for an
15078    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15079    is returned.  If PARSER->SCOPE is a dependent type, then a
15080    SCOPE_REF is returned.
15081
15082    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15083    returned; the name was already resolved when the TEMPLATE_ID_EXPR
15084    was formed.  Abstractly, such entities should not be passed to this
15085    function, because they do not need to be looked up, but it is
15086    simpler to check for this special case here, rather than at the
15087    call-sites.
15088
15089    In cases not explicitly covered above, this function returns a
15090    DECL, OVERLOAD, or baselink representing the result of the lookup.
15091    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15092    is returned.
15093
15094    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15095    (e.g., "struct") that was used.  In that case bindings that do not
15096    refer to types are ignored.
15097
15098    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15099    ignored.
15100
15101    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15102    are ignored.
15103
15104    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15105    types.
15106
15107    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15108    TREE_LIST of candidates if name-lookup results in an ambiguity, and
15109    NULL_TREE otherwise.  */
15110
15111 static tree
15112 cp_parser_lookup_name (cp_parser *parser, tree name,
15113                        enum tag_types tag_type,
15114                        bool is_template,
15115                        bool is_namespace,
15116                        bool check_dependency,
15117                        tree *ambiguous_decls)
15118 {
15119   int flags = 0;
15120   tree decl;
15121   tree object_type = parser->context->object_type;
15122
15123   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15124     flags |= LOOKUP_COMPLAIN;
15125
15126   /* Assume that the lookup will be unambiguous.  */
15127   if (ambiguous_decls)
15128     *ambiguous_decls = NULL_TREE;
15129
15130   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15131      no longer valid.  Note that if we are parsing tentatively, and
15132      the parse fails, OBJECT_TYPE will be automatically restored.  */
15133   parser->context->object_type = NULL_TREE;
15134
15135   if (name == error_mark_node)
15136     return error_mark_node;
15137
15138   /* A template-id has already been resolved; there is no lookup to
15139      do.  */
15140   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15141     return name;
15142   if (BASELINK_P (name))
15143     {
15144       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15145                   == TEMPLATE_ID_EXPR);
15146       return name;
15147     }
15148
15149   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
15150      it should already have been checked to make sure that the name
15151      used matches the type being destroyed.  */
15152   if (TREE_CODE (name) == BIT_NOT_EXPR)
15153     {
15154       tree type;
15155
15156       /* Figure out to which type this destructor applies.  */
15157       if (parser->scope)
15158         type = parser->scope;
15159       else if (object_type)
15160         type = object_type;
15161       else
15162         type = current_class_type;
15163       /* If that's not a class type, there is no destructor.  */
15164       if (!type || !CLASS_TYPE_P (type))
15165         return error_mark_node;
15166       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15167         lazily_declare_fn (sfk_destructor, type);
15168       if (!CLASSTYPE_DESTRUCTORS (type))
15169           return error_mark_node;
15170       /* If it was a class type, return the destructor.  */
15171       return CLASSTYPE_DESTRUCTORS (type);
15172     }
15173
15174   /* By this point, the NAME should be an ordinary identifier.  If
15175      the id-expression was a qualified name, the qualifying scope is
15176      stored in PARSER->SCOPE at this point.  */
15177   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15178
15179   /* Perform the lookup.  */
15180   if (parser->scope)
15181     {
15182       bool dependent_p;
15183
15184       if (parser->scope == error_mark_node)
15185         return error_mark_node;
15186
15187       /* If the SCOPE is dependent, the lookup must be deferred until
15188          the template is instantiated -- unless we are explicitly
15189          looking up names in uninstantiated templates.  Even then, we
15190          cannot look up the name if the scope is not a class type; it
15191          might, for example, be a template type parameter.  */
15192       dependent_p = (TYPE_P (parser->scope)
15193                      && !(parser->in_declarator_p
15194                           && currently_open_class (parser->scope))
15195                      && dependent_type_p (parser->scope));
15196       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15197            && dependent_p)
15198         {
15199           if (tag_type)
15200             {
15201               tree type;
15202
15203               /* The resolution to Core Issue 180 says that `struct
15204                  A::B' should be considered a type-name, even if `A'
15205                  is dependent.  */
15206               type = make_typename_type (parser->scope, name, tag_type,
15207                                          /*complain=*/tf_error);
15208               decl = TYPE_NAME (type);
15209             }
15210           else if (is_template
15211                    && (cp_parser_next_token_ends_template_argument_p (parser)
15212                        || cp_lexer_next_token_is (parser->lexer,
15213                                                   CPP_CLOSE_PAREN)))
15214             decl = make_unbound_class_template (parser->scope,
15215                                                 name, NULL_TREE,
15216                                                 /*complain=*/tf_error);
15217           else
15218             decl = build_qualified_name (/*type=*/NULL_TREE,
15219                                          parser->scope, name,
15220                                          is_template);
15221         }
15222       else
15223         {
15224           tree pushed_scope = NULL_TREE;
15225
15226           /* If PARSER->SCOPE is a dependent type, then it must be a
15227              class type, and we must not be checking dependencies;
15228              otherwise, we would have processed this lookup above.  So
15229              that PARSER->SCOPE is not considered a dependent base by
15230              lookup_member, we must enter the scope here.  */
15231           if (dependent_p)
15232             pushed_scope = push_scope (parser->scope);
15233           /* If the PARSER->SCOPE is a template specialization, it
15234              may be instantiated during name lookup.  In that case,
15235              errors may be issued.  Even if we rollback the current
15236              tentative parse, those errors are valid.  */
15237           decl = lookup_qualified_name (parser->scope, name,
15238                                         tag_type != none_type,
15239                                         /*complain=*/true);
15240           if (pushed_scope)
15241             pop_scope (pushed_scope);
15242         }
15243       parser->qualifying_scope = parser->scope;
15244       parser->object_scope = NULL_TREE;
15245     }
15246   else if (object_type)
15247     {
15248       tree object_decl = NULL_TREE;
15249       /* Look up the name in the scope of the OBJECT_TYPE, unless the
15250          OBJECT_TYPE is not a class.  */
15251       if (CLASS_TYPE_P (object_type))
15252         /* If the OBJECT_TYPE is a template specialization, it may
15253            be instantiated during name lookup.  In that case, errors
15254            may be issued.  Even if we rollback the current tentative
15255            parse, those errors are valid.  */
15256         object_decl = lookup_member (object_type,
15257                                      name,
15258                                      /*protect=*/0,
15259                                      tag_type != none_type);
15260       /* Look it up in the enclosing context, too.  */
15261       decl = lookup_name_real (name, tag_type != none_type,
15262                                /*nonclass=*/0,
15263                                /*block_p=*/true, is_namespace, flags);
15264       parser->object_scope = object_type;
15265       parser->qualifying_scope = NULL_TREE;
15266       if (object_decl)
15267         decl = object_decl;
15268     }
15269   else
15270     {
15271       decl = lookup_name_real (name, tag_type != none_type,
15272                                /*nonclass=*/0,
15273                                /*block_p=*/true, is_namespace, flags);
15274       parser->qualifying_scope = NULL_TREE;
15275       parser->object_scope = NULL_TREE;
15276     }
15277
15278   /* If the lookup failed, let our caller know.  */
15279   if (!decl || decl == error_mark_node)
15280     return error_mark_node;
15281
15282   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
15283   if (TREE_CODE (decl) == TREE_LIST)
15284     {
15285       if (ambiguous_decls)
15286         *ambiguous_decls = decl;
15287       /* The error message we have to print is too complicated for
15288          cp_parser_error, so we incorporate its actions directly.  */
15289       if (!cp_parser_simulate_error (parser))
15290         {
15291           error ("reference to %qD is ambiguous", name);
15292           print_candidates (decl);
15293         }
15294       return error_mark_node;
15295     }
15296
15297   gcc_assert (DECL_P (decl)
15298               || TREE_CODE (decl) == OVERLOAD
15299               || TREE_CODE (decl) == SCOPE_REF
15300               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15301               || BASELINK_P (decl));
15302
15303   /* If we have resolved the name of a member declaration, check to
15304      see if the declaration is accessible.  When the name resolves to
15305      set of overloaded functions, accessibility is checked when
15306      overload resolution is done.
15307
15308      During an explicit instantiation, access is not checked at all,
15309      as per [temp.explicit].  */
15310   if (DECL_P (decl))
15311     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15312
15313   return decl;
15314 }
15315
15316 /* Like cp_parser_lookup_name, but for use in the typical case where
15317    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15318    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
15319
15320 static tree
15321 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15322 {
15323   return cp_parser_lookup_name (parser, name,
15324                                 none_type,
15325                                 /*is_template=*/false,
15326                                 /*is_namespace=*/false,
15327                                 /*check_dependency=*/true,
15328                                 /*ambiguous_decls=*/NULL);
15329 }
15330
15331 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15332    the current context, return the TYPE_DECL.  If TAG_NAME_P is
15333    true, the DECL indicates the class being defined in a class-head,
15334    or declared in an elaborated-type-specifier.
15335
15336    Otherwise, return DECL.  */
15337
15338 static tree
15339 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15340 {
15341   /* If the TEMPLATE_DECL is being declared as part of a class-head,
15342      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15343
15344        struct A {
15345          template <typename T> struct B;
15346        };
15347
15348        template <typename T> struct A::B {};
15349
15350      Similarly, in an elaborated-type-specifier:
15351
15352        namespace N { struct X{}; }
15353
15354        struct A {
15355          template <typename T> friend struct N::X;
15356        };
15357
15358      However, if the DECL refers to a class type, and we are in
15359      the scope of the class, then the name lookup automatically
15360      finds the TYPE_DECL created by build_self_reference rather
15361      than a TEMPLATE_DECL.  For example, in:
15362
15363        template <class T> struct S {
15364          S s;
15365        };
15366
15367      there is no need to handle such case.  */
15368
15369   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15370     return DECL_TEMPLATE_RESULT (decl);
15371
15372   return decl;
15373 }
15374
15375 /* If too many, or too few, template-parameter lists apply to the
15376    declarator, issue an error message.  Returns TRUE if all went well,
15377    and FALSE otherwise.  */
15378
15379 static bool
15380 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15381                                                 cp_declarator *declarator)
15382 {
15383   unsigned num_templates;
15384
15385   /* We haven't seen any classes that involve template parameters yet.  */
15386   num_templates = 0;
15387
15388   switch (declarator->kind)
15389     {
15390     case cdk_id:
15391       if (declarator->u.id.qualifying_scope)
15392         {
15393           tree scope;
15394           tree member;
15395
15396           scope = declarator->u.id.qualifying_scope;
15397           member = declarator->u.id.unqualified_name;
15398
15399           while (scope && CLASS_TYPE_P (scope))
15400             {
15401               /* You're supposed to have one `template <...>'
15402                  for every template class, but you don't need one
15403                  for a full specialization.  For example:
15404
15405                  template <class T> struct S{};
15406                  template <> struct S<int> { void f(); };
15407                  void S<int>::f () {}
15408
15409                  is correct; there shouldn't be a `template <>' for
15410                  the definition of `S<int>::f'.  */
15411               if (!CLASSTYPE_TEMPLATE_INFO (scope))
15412                 /* If SCOPE does not have template information of any
15413                    kind, then it is not a template, nor is it nested
15414                    within a template.  */
15415                 break;
15416               if (explicit_class_specialization_p (scope))
15417                 break;
15418               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15419                 ++num_templates;
15420
15421               scope = TYPE_CONTEXT (scope);
15422             }
15423         }
15424       else if (TREE_CODE (declarator->u.id.unqualified_name)
15425                == TEMPLATE_ID_EXPR)
15426         /* If the DECLARATOR has the form `X<y>' then it uses one
15427            additional level of template parameters.  */
15428         ++num_templates;
15429
15430       return cp_parser_check_template_parameters (parser,
15431                                                   num_templates);
15432
15433     case cdk_function:
15434     case cdk_array:
15435     case cdk_pointer:
15436     case cdk_reference:
15437     case cdk_ptrmem:
15438       return (cp_parser_check_declarator_template_parameters
15439               (parser, declarator->declarator));
15440
15441     case cdk_error:
15442       return true;
15443
15444     default:
15445       gcc_unreachable ();
15446     }
15447   return false;
15448 }
15449
15450 /* NUM_TEMPLATES were used in the current declaration.  If that is
15451    invalid, return FALSE and issue an error messages.  Otherwise,
15452    return TRUE.  */
15453
15454 static bool
15455 cp_parser_check_template_parameters (cp_parser* parser,
15456                                      unsigned num_templates)
15457 {
15458   /* If there are more template classes than parameter lists, we have
15459      something like:
15460
15461        template <class T> void S<T>::R<T>::f ();  */
15462   if (parser->num_template_parameter_lists < num_templates)
15463     {
15464       error ("too few template-parameter-lists");
15465       return false;
15466     }
15467   /* If there are the same number of template classes and parameter
15468      lists, that's OK.  */
15469   if (parser->num_template_parameter_lists == num_templates)
15470     return true;
15471   /* If there are more, but only one more, then we are referring to a
15472      member template.  That's OK too.  */
15473   if (parser->num_template_parameter_lists == num_templates + 1)
15474       return true;
15475   /* Otherwise, there are too many template parameter lists.  We have
15476      something like:
15477
15478      template <class T> template <class U> void S::f();  */
15479   error ("too many template-parameter-lists");
15480   return false;
15481 }
15482
15483 /* Parse an optional `::' token indicating that the following name is
15484    from the global namespace.  If so, PARSER->SCOPE is set to the
15485    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15486    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15487    Returns the new value of PARSER->SCOPE, if the `::' token is
15488    present, and NULL_TREE otherwise.  */
15489
15490 static tree
15491 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15492 {
15493   cp_token *token;
15494
15495   /* Peek at the next token.  */
15496   token = cp_lexer_peek_token (parser->lexer);
15497   /* If we're looking at a `::' token then we're starting from the
15498      global namespace, not our current location.  */
15499   if (token->type == CPP_SCOPE)
15500     {
15501       /* Consume the `::' token.  */
15502       cp_lexer_consume_token (parser->lexer);
15503       /* Set the SCOPE so that we know where to start the lookup.  */
15504       parser->scope = global_namespace;
15505       parser->qualifying_scope = global_namespace;
15506       parser->object_scope = NULL_TREE;
15507
15508       return parser->scope;
15509     }
15510   else if (!current_scope_valid_p)
15511     {
15512       parser->scope = NULL_TREE;
15513       parser->qualifying_scope = NULL_TREE;
15514       parser->object_scope = NULL_TREE;
15515     }
15516
15517   return NULL_TREE;
15518 }
15519
15520 /* Returns TRUE if the upcoming token sequence is the start of a
15521    constructor declarator.  If FRIEND_P is true, the declarator is
15522    preceded by the `friend' specifier.  */
15523
15524 static bool
15525 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15526 {
15527   bool constructor_p;
15528   tree type_decl = NULL_TREE;
15529   bool nested_name_p;
15530   cp_token *next_token;
15531
15532   /* The common case is that this is not a constructor declarator, so
15533      try to avoid doing lots of work if at all possible.  It's not
15534      valid declare a constructor at function scope.  */
15535   if (parser->in_function_body)
15536     return false;
15537   /* And only certain tokens can begin a constructor declarator.  */
15538   next_token = cp_lexer_peek_token (parser->lexer);
15539   if (next_token->type != CPP_NAME
15540       && next_token->type != CPP_SCOPE
15541       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15542       && next_token->type != CPP_TEMPLATE_ID)
15543     return false;
15544
15545   /* Parse tentatively; we are going to roll back all of the tokens
15546      consumed here.  */
15547   cp_parser_parse_tentatively (parser);
15548   /* Assume that we are looking at a constructor declarator.  */
15549   constructor_p = true;
15550
15551   /* Look for the optional `::' operator.  */
15552   cp_parser_global_scope_opt (parser,
15553                               /*current_scope_valid_p=*/false);
15554   /* Look for the nested-name-specifier.  */
15555   nested_name_p
15556     = (cp_parser_nested_name_specifier_opt (parser,
15557                                             /*typename_keyword_p=*/false,
15558                                             /*check_dependency_p=*/false,
15559                                             /*type_p=*/false,
15560                                             /*is_declaration=*/false)
15561        != NULL_TREE);
15562   /* Outside of a class-specifier, there must be a
15563      nested-name-specifier.  */
15564   if (!nested_name_p &&
15565       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15566        || friend_p))
15567     constructor_p = false;
15568   /* If we still think that this might be a constructor-declarator,
15569      look for a class-name.  */
15570   if (constructor_p)
15571     {
15572       /* If we have:
15573
15574            template <typename T> struct S { S(); };
15575            template <typename T> S<T>::S ();
15576
15577          we must recognize that the nested `S' names a class.
15578          Similarly, for:
15579
15580            template <typename T> S<T>::S<T> ();
15581
15582          we must recognize that the nested `S' names a template.  */
15583       type_decl = cp_parser_class_name (parser,
15584                                         /*typename_keyword_p=*/false,
15585                                         /*template_keyword_p=*/false,
15586                                         none_type,
15587                                         /*check_dependency_p=*/false,
15588                                         /*class_head_p=*/false,
15589                                         /*is_declaration=*/false);
15590       /* If there was no class-name, then this is not a constructor.  */
15591       constructor_p = !cp_parser_error_occurred (parser);
15592     }
15593
15594   /* If we're still considering a constructor, we have to see a `(',
15595      to begin the parameter-declaration-clause, followed by either a
15596      `)', an `...', or a decl-specifier.  We need to check for a
15597      type-specifier to avoid being fooled into thinking that:
15598
15599        S::S (f) (int);
15600
15601      is a constructor.  (It is actually a function named `f' that
15602      takes one parameter (of type `int') and returns a value of type
15603      `S::S'.  */
15604   if (constructor_p
15605       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15606     {
15607       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15608           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15609           /* A parameter declaration begins with a decl-specifier,
15610              which is either the "attribute" keyword, a storage class
15611              specifier, or (usually) a type-specifier.  */
15612           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15613         {
15614           tree type;
15615           tree pushed_scope = NULL_TREE;
15616           unsigned saved_num_template_parameter_lists;
15617
15618           /* Names appearing in the type-specifier should be looked up
15619              in the scope of the class.  */
15620           if (current_class_type)
15621             type = NULL_TREE;
15622           else
15623             {
15624               type = TREE_TYPE (type_decl);
15625               if (TREE_CODE (type) == TYPENAME_TYPE)
15626                 {
15627                   type = resolve_typename_type (type,
15628                                                 /*only_current_p=*/false);
15629                   if (type == error_mark_node)
15630                     {
15631                       cp_parser_abort_tentative_parse (parser);
15632                       return false;
15633                     }
15634                 }
15635               pushed_scope = push_scope (type);
15636             }
15637
15638           /* Inside the constructor parameter list, surrounding
15639              template-parameter-lists do not apply.  */
15640           saved_num_template_parameter_lists
15641             = parser->num_template_parameter_lists;
15642           parser->num_template_parameter_lists = 0;
15643
15644           /* Look for the type-specifier.  */
15645           cp_parser_type_specifier (parser,
15646                                     CP_PARSER_FLAGS_NONE,
15647                                     /*decl_specs=*/NULL,
15648                                     /*is_declarator=*/true,
15649                                     /*declares_class_or_enum=*/NULL,
15650                                     /*is_cv_qualifier=*/NULL);
15651
15652           parser->num_template_parameter_lists
15653             = saved_num_template_parameter_lists;
15654
15655           /* Leave the scope of the class.  */
15656           if (pushed_scope)
15657             pop_scope (pushed_scope);
15658
15659           constructor_p = !cp_parser_error_occurred (parser);
15660         }
15661     }
15662   else
15663     constructor_p = false;
15664   /* We did not really want to consume any tokens.  */
15665   cp_parser_abort_tentative_parse (parser);
15666
15667   return constructor_p;
15668 }
15669
15670 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15671    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15672    they must be performed once we are in the scope of the function.
15673
15674    Returns the function defined.  */
15675
15676 static tree
15677 cp_parser_function_definition_from_specifiers_and_declarator
15678   (cp_parser* parser,
15679    cp_decl_specifier_seq *decl_specifiers,
15680    tree attributes,
15681    const cp_declarator *declarator)
15682 {
15683   tree fn;
15684   bool success_p;
15685
15686   /* Begin the function-definition.  */
15687   success_p = start_function (decl_specifiers, declarator, attributes);
15688
15689   /* The things we're about to see are not directly qualified by any
15690      template headers we've seen thus far.  */
15691   reset_specialization ();
15692
15693   /* If there were names looked up in the decl-specifier-seq that we
15694      did not check, check them now.  We must wait until we are in the
15695      scope of the function to perform the checks, since the function
15696      might be a friend.  */
15697   perform_deferred_access_checks ();
15698
15699   if (!success_p)
15700     {
15701       /* Skip the entire function.  */
15702       cp_parser_skip_to_end_of_block_or_statement (parser);
15703       fn = error_mark_node;
15704     }
15705   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
15706     {
15707       /* Seen already, skip it.  An error message has already been output.  */
15708       cp_parser_skip_to_end_of_block_or_statement (parser);
15709       fn = current_function_decl;
15710       current_function_decl = NULL_TREE;
15711       /* If this is a function from a class, pop the nested class.  */
15712       if (current_class_name)
15713         pop_nested_class ();
15714     }
15715   else
15716     fn = cp_parser_function_definition_after_declarator (parser,
15717                                                          /*inline_p=*/false);
15718
15719   return fn;
15720 }
15721
15722 /* Parse the part of a function-definition that follows the
15723    declarator.  INLINE_P is TRUE iff this function is an inline
15724    function defined with a class-specifier.
15725
15726    Returns the function defined.  */
15727
15728 static tree
15729 cp_parser_function_definition_after_declarator (cp_parser* parser,
15730                                                 bool inline_p)
15731 {
15732   tree fn;
15733   bool ctor_initializer_p = false;
15734   bool saved_in_unbraced_linkage_specification_p;
15735   bool saved_in_function_body;
15736   unsigned saved_num_template_parameter_lists;
15737
15738   saved_in_function_body = parser->in_function_body;
15739   parser->in_function_body = true;
15740   /* If the next token is `return', then the code may be trying to
15741      make use of the "named return value" extension that G++ used to
15742      support.  */
15743   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15744     {
15745       /* Consume the `return' keyword.  */
15746       cp_lexer_consume_token (parser->lexer);
15747       /* Look for the identifier that indicates what value is to be
15748          returned.  */
15749       cp_parser_identifier (parser);
15750       /* Issue an error message.  */
15751       error ("named return values are no longer supported");
15752       /* Skip tokens until we reach the start of the function body.  */
15753       while (true)
15754         {
15755           cp_token *token = cp_lexer_peek_token (parser->lexer);
15756           if (token->type == CPP_OPEN_BRACE
15757               || token->type == CPP_EOF
15758               || token->type == CPP_PRAGMA_EOL)
15759             break;
15760           cp_lexer_consume_token (parser->lexer);
15761         }
15762     }
15763   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15764      anything declared inside `f'.  */
15765   saved_in_unbraced_linkage_specification_p
15766     = parser->in_unbraced_linkage_specification_p;
15767   parser->in_unbraced_linkage_specification_p = false;
15768   /* Inside the function, surrounding template-parameter-lists do not
15769      apply.  */
15770   saved_num_template_parameter_lists
15771     = parser->num_template_parameter_lists;
15772   parser->num_template_parameter_lists = 0;
15773   /* If the next token is `try', then we are looking at a
15774      function-try-block.  */
15775   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15776     ctor_initializer_p = cp_parser_function_try_block (parser);
15777   /* A function-try-block includes the function-body, so we only do
15778      this next part if we're not processing a function-try-block.  */
15779   else
15780     ctor_initializer_p
15781       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15782
15783   /* Finish the function.  */
15784   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15785                         (inline_p ? 2 : 0));
15786   /* Generate code for it, if necessary.  */
15787   expand_or_defer_fn (fn);
15788   /* Restore the saved values.  */
15789   parser->in_unbraced_linkage_specification_p
15790     = saved_in_unbraced_linkage_specification_p;
15791   parser->num_template_parameter_lists
15792     = saved_num_template_parameter_lists;
15793   parser->in_function_body = saved_in_function_body;
15794
15795   return fn;
15796 }
15797
15798 /* Parse a template-declaration, assuming that the `export' (and
15799    `extern') keywords, if present, has already been scanned.  MEMBER_P
15800    is as for cp_parser_template_declaration.  */
15801
15802 static void
15803 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15804 {
15805   tree decl = NULL_TREE;
15806   VEC (deferred_access_check,gc) *checks;
15807   tree parameter_list;
15808   bool friend_p = false;
15809   bool need_lang_pop;
15810
15811   /* Look for the `template' keyword.  */
15812   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15813     return;
15814
15815   /* And the `<'.  */
15816   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15817     return;
15818   if (at_class_scope_p () && current_function_decl)
15819     {
15820       /* 14.5.2.2 [temp.mem]
15821
15822          A local class shall not have member templates.  */
15823       error ("invalid declaration of member template in local class");
15824       cp_parser_skip_to_end_of_block_or_statement (parser);
15825       return;
15826     }
15827   /* [temp]
15828
15829      A template ... shall not have C linkage.  */
15830   if (current_lang_name == lang_name_c)
15831     {
15832       error ("template with C linkage");
15833       /* Give it C++ linkage to avoid confusing other parts of the
15834          front end.  */
15835       push_lang_context (lang_name_cplusplus);
15836       need_lang_pop = true;
15837     }
15838   else
15839     need_lang_pop = false;
15840
15841   /* We cannot perform access checks on the template parameter
15842      declarations until we know what is being declared, just as we
15843      cannot check the decl-specifier list.  */
15844   push_deferring_access_checks (dk_deferred);
15845
15846   /* If the next token is `>', then we have an invalid
15847      specialization.  Rather than complain about an invalid template
15848      parameter, issue an error message here.  */
15849   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15850     {
15851       cp_parser_error (parser, "invalid explicit specialization");
15852       begin_specialization ();
15853       parameter_list = NULL_TREE;
15854     }
15855   else
15856     /* Parse the template parameters.  */
15857     parameter_list = cp_parser_template_parameter_list (parser);
15858
15859   /* Get the deferred access checks from the parameter list.  These
15860      will be checked once we know what is being declared, as for a
15861      member template the checks must be performed in the scope of the
15862      class containing the member.  */
15863   checks = get_deferred_access_checks ();
15864
15865   /* Look for the `>'.  */
15866   cp_parser_skip_to_end_of_template_parameter_list (parser);
15867   /* We just processed one more parameter list.  */
15868   ++parser->num_template_parameter_lists;
15869   /* If the next token is `template', there are more template
15870      parameters.  */
15871   if (cp_lexer_next_token_is_keyword (parser->lexer,
15872                                       RID_TEMPLATE))
15873     cp_parser_template_declaration_after_export (parser, member_p);
15874   else
15875     {
15876       /* There are no access checks when parsing a template, as we do not
15877          know if a specialization will be a friend.  */
15878       push_deferring_access_checks (dk_no_check);
15879       decl = cp_parser_single_declaration (parser,
15880                                            checks,
15881                                            member_p,
15882                                            &friend_p);
15883       pop_deferring_access_checks ();
15884
15885       /* If this is a member template declaration, let the front
15886          end know.  */
15887       if (member_p && !friend_p && decl)
15888         {
15889           if (TREE_CODE (decl) == TYPE_DECL)
15890             cp_parser_check_access_in_redeclaration (decl);
15891
15892           decl = finish_member_template_decl (decl);
15893         }
15894       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15895         make_friend_class (current_class_type, TREE_TYPE (decl),
15896                            /*complain=*/true);
15897     }
15898   /* We are done with the current parameter list.  */
15899   --parser->num_template_parameter_lists;
15900
15901   pop_deferring_access_checks ();
15902
15903   /* Finish up.  */
15904   finish_template_decl (parameter_list);
15905
15906   /* Register member declarations.  */
15907   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15908     finish_member_declaration (decl);
15909   /* For the erroneous case of a template with C linkage, we pushed an
15910      implicit C++ linkage scope; exit that scope now.  */
15911   if (need_lang_pop)
15912     pop_lang_context ();
15913   /* If DECL is a function template, we must return to parse it later.
15914      (Even though there is no definition, there might be default
15915      arguments that need handling.)  */
15916   if (member_p && decl
15917       && (TREE_CODE (decl) == FUNCTION_DECL
15918           || DECL_FUNCTION_TEMPLATE_P (decl)))
15919     TREE_VALUE (parser->unparsed_functions_queues)
15920       = tree_cons (NULL_TREE, decl,
15921                    TREE_VALUE (parser->unparsed_functions_queues));
15922 }
15923
15924 /* Perform the deferred access checks from a template-parameter-list.
15925    CHECKS is a TREE_LIST of access checks, as returned by
15926    get_deferred_access_checks.  */
15927
15928 static void
15929 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
15930 {
15931   ++processing_template_parmlist;
15932   perform_access_checks (checks);
15933   --processing_template_parmlist;
15934 }
15935
15936 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15937    `function-definition' sequence.  MEMBER_P is true, this declaration
15938    appears in a class scope.
15939
15940    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15941    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15942
15943 static tree
15944 cp_parser_single_declaration (cp_parser* parser,
15945                               VEC (deferred_access_check,gc)* checks,
15946                               bool member_p,
15947                               bool* friend_p)
15948 {
15949   int declares_class_or_enum;
15950   tree decl = NULL_TREE;
15951   cp_decl_specifier_seq decl_specifiers;
15952   bool function_definition_p = false;
15953
15954   /* This function is only used when processing a template
15955      declaration.  */
15956   gcc_assert (innermost_scope_kind () == sk_template_parms
15957               || innermost_scope_kind () == sk_template_spec);
15958
15959   /* Defer access checks until we know what is being declared.  */
15960   push_deferring_access_checks (dk_deferred);
15961
15962   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15963      alternative.  */
15964   cp_parser_decl_specifier_seq (parser,
15965                                 CP_PARSER_FLAGS_OPTIONAL,
15966                                 &decl_specifiers,
15967                                 &declares_class_or_enum);
15968   if (friend_p)
15969     *friend_p = cp_parser_friend_p (&decl_specifiers);
15970
15971   /* There are no template typedefs.  */
15972   if (decl_specifiers.specs[(int) ds_typedef])
15973     {
15974       error ("template declaration of %qs", "typedef");
15975       decl = error_mark_node;
15976     }
15977
15978   /* Gather up the access checks that occurred the
15979      decl-specifier-seq.  */
15980   stop_deferring_access_checks ();
15981
15982   /* Check for the declaration of a template class.  */
15983   if (declares_class_or_enum)
15984     {
15985       if (cp_parser_declares_only_class_p (parser))
15986         {
15987           decl = shadow_tag (&decl_specifiers);
15988
15989           /* In this case:
15990
15991                struct C {
15992                  friend template <typename T> struct A<T>::B;
15993                };
15994
15995              A<T>::B will be represented by a TYPENAME_TYPE, and
15996              therefore not recognized by shadow_tag.  */
15997           if (friend_p && *friend_p
15998               && !decl
15999               && decl_specifiers.type
16000               && TYPE_P (decl_specifiers.type))
16001             decl = decl_specifiers.type;
16002
16003           if (decl && decl != error_mark_node)
16004             decl = TYPE_NAME (decl);
16005           else
16006             decl = error_mark_node;
16007
16008           /* Perform access checks for template parameters.  */
16009           cp_parser_perform_template_parameter_access_checks (checks);
16010         }
16011     }
16012   /* If it's not a template class, try for a template function.  If
16013      the next token is a `;', then this declaration does not declare
16014      anything.  But, if there were errors in the decl-specifiers, then
16015      the error might well have come from an attempted class-specifier.
16016      In that case, there's no need to warn about a missing declarator.  */
16017   if (!decl
16018       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16019           || decl_specifiers.type != error_mark_node))
16020     decl = cp_parser_init_declarator (parser,
16021                                       &decl_specifiers,
16022                                       checks,
16023                                       /*function_definition_allowed_p=*/true,
16024                                       member_p,
16025                                       declares_class_or_enum,
16026                                       &function_definition_p);
16027
16028   pop_deferring_access_checks ();
16029
16030   /* Clear any current qualification; whatever comes next is the start
16031      of something new.  */
16032   parser->scope = NULL_TREE;
16033   parser->qualifying_scope = NULL_TREE;
16034   parser->object_scope = NULL_TREE;
16035   /* Look for a trailing `;' after the declaration.  */
16036   if (!function_definition_p
16037       && (decl == error_mark_node
16038           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16039     cp_parser_skip_to_end_of_block_or_statement (parser);
16040
16041   return decl;
16042 }
16043
16044 /* Parse a cast-expression that is not the operand of a unary "&".  */
16045
16046 static tree
16047 cp_parser_simple_cast_expression (cp_parser *parser)
16048 {
16049   return cp_parser_cast_expression (parser, /*address_p=*/false,
16050                                     /*cast_p=*/false);
16051 }
16052
16053 /* Parse a functional cast to TYPE.  Returns an expression
16054    representing the cast.  */
16055
16056 static tree
16057 cp_parser_functional_cast (cp_parser* parser, tree type)
16058 {
16059   tree expression_list;
16060   tree cast;
16061
16062   expression_list
16063     = cp_parser_parenthesized_expression_list (parser, false,
16064                                                /*cast_p=*/true,
16065                                                /*non_constant_p=*/NULL);
16066
16067   cast = build_functional_cast (type, expression_list);
16068   /* [expr.const]/1: In an integral constant expression "only type
16069      conversions to integral or enumeration type can be used".  */
16070   if (TREE_CODE (type) == TYPE_DECL)
16071     type = TREE_TYPE (type);
16072   if (cast != error_mark_node
16073       && !cast_valid_in_integral_constant_expression_p (type)
16074       && (cp_parser_non_integral_constant_expression
16075           (parser, "a call to a constructor")))
16076     return error_mark_node;
16077   return cast;
16078 }
16079
16080 /* Save the tokens that make up the body of a member function defined
16081    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
16082    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
16083    specifiers applied to the declaration.  Returns the FUNCTION_DECL
16084    for the member function.  */
16085
16086 static tree
16087 cp_parser_save_member_function_body (cp_parser* parser,
16088                                      cp_decl_specifier_seq *decl_specifiers,
16089                                      cp_declarator *declarator,
16090                                      tree attributes)
16091 {
16092   cp_token *first;
16093   cp_token *last;
16094   tree fn;
16095
16096   /* Create the function-declaration.  */
16097   fn = start_method (decl_specifiers, declarator, attributes);
16098   /* If something went badly wrong, bail out now.  */
16099   if (fn == error_mark_node)
16100     {
16101       /* If there's a function-body, skip it.  */
16102       if (cp_parser_token_starts_function_definition_p
16103           (cp_lexer_peek_token (parser->lexer)))
16104         cp_parser_skip_to_end_of_block_or_statement (parser);
16105       return error_mark_node;
16106     }
16107
16108   /* Remember it, if there default args to post process.  */
16109   cp_parser_save_default_args (parser, fn);
16110
16111   /* Save away the tokens that make up the body of the
16112      function.  */
16113   first = parser->lexer->next_token;
16114   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16115   /* Handle function try blocks.  */
16116   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16117     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16118   last = parser->lexer->next_token;
16119
16120   /* Save away the inline definition; we will process it when the
16121      class is complete.  */
16122   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16123   DECL_PENDING_INLINE_P (fn) = 1;
16124
16125   /* We need to know that this was defined in the class, so that
16126      friend templates are handled correctly.  */
16127   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16128
16129   /* We're done with the inline definition.  */
16130   finish_method (fn);
16131
16132   /* Add FN to the queue of functions to be parsed later.  */
16133   TREE_VALUE (parser->unparsed_functions_queues)
16134     = tree_cons (NULL_TREE, fn,
16135                  TREE_VALUE (parser->unparsed_functions_queues));
16136
16137   return fn;
16138 }
16139
16140 /* Parse a template-argument-list, as well as the trailing ">" (but
16141    not the opening ">").  See cp_parser_template_argument_list for the
16142    return value.  */
16143
16144 static tree
16145 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16146 {
16147   tree arguments;
16148   tree saved_scope;
16149   tree saved_qualifying_scope;
16150   tree saved_object_scope;
16151   bool saved_greater_than_is_operator_p;
16152   bool saved_skip_evaluation;
16153
16154   /* [temp.names]
16155
16156      When parsing a template-id, the first non-nested `>' is taken as
16157      the end of the template-argument-list rather than a greater-than
16158      operator.  */
16159   saved_greater_than_is_operator_p
16160     = parser->greater_than_is_operator_p;
16161   parser->greater_than_is_operator_p = false;
16162   /* Parsing the argument list may modify SCOPE, so we save it
16163      here.  */
16164   saved_scope = parser->scope;
16165   saved_qualifying_scope = parser->qualifying_scope;
16166   saved_object_scope = parser->object_scope;
16167   /* We need to evaluate the template arguments, even though this
16168      template-id may be nested within a "sizeof".  */
16169   saved_skip_evaluation = skip_evaluation;
16170   skip_evaluation = false;
16171   /* Parse the template-argument-list itself.  */
16172   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16173     arguments = NULL_TREE;
16174   else
16175     arguments = cp_parser_template_argument_list (parser);
16176   /* Look for the `>' that ends the template-argument-list. If we find
16177      a '>>' instead, it's probably just a typo.  */
16178   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16179     {
16180       if (!saved_greater_than_is_operator_p)
16181         {
16182           /* If we're in a nested template argument list, the '>>' has
16183             to be a typo for '> >'. We emit the error message, but we
16184             continue parsing and we push a '>' as next token, so that
16185             the argument list will be parsed correctly.  Note that the
16186             global source location is still on the token before the
16187             '>>', so we need to say explicitly where we want it.  */
16188           cp_token *token = cp_lexer_peek_token (parser->lexer);
16189           error ("%H%<>>%> should be %<> >%> "
16190                  "within a nested template argument list",
16191                  &token->location);
16192
16193           /* ??? Proper recovery should terminate two levels of
16194              template argument list here.  */
16195           token->type = CPP_GREATER;
16196         }
16197       else
16198         {
16199           /* If this is not a nested template argument list, the '>>'
16200             is a typo for '>'. Emit an error message and continue.
16201             Same deal about the token location, but here we can get it
16202             right by consuming the '>>' before issuing the diagnostic.  */
16203           cp_lexer_consume_token (parser->lexer);
16204           error ("spurious %<>>%>, use %<>%> to terminate "
16205                  "a template argument list");
16206         }
16207     }
16208   else
16209     cp_parser_skip_to_end_of_template_parameter_list (parser);
16210   /* The `>' token might be a greater-than operator again now.  */
16211   parser->greater_than_is_operator_p
16212     = saved_greater_than_is_operator_p;
16213   /* Restore the SAVED_SCOPE.  */
16214   parser->scope = saved_scope;
16215   parser->qualifying_scope = saved_qualifying_scope;
16216   parser->object_scope = saved_object_scope;
16217   skip_evaluation = saved_skip_evaluation;
16218
16219   return arguments;
16220 }
16221
16222 /* MEMBER_FUNCTION is a member function, or a friend.  If default
16223    arguments, or the body of the function have not yet been parsed,
16224    parse them now.  */
16225
16226 static void
16227 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16228 {
16229   /* If this member is a template, get the underlying
16230      FUNCTION_DECL.  */
16231   if (DECL_FUNCTION_TEMPLATE_P (member_function))
16232     member_function = DECL_TEMPLATE_RESULT (member_function);
16233
16234   /* There should not be any class definitions in progress at this
16235      point; the bodies of members are only parsed outside of all class
16236      definitions.  */
16237   gcc_assert (parser->num_classes_being_defined == 0);
16238   /* While we're parsing the member functions we might encounter more
16239      classes.  We want to handle them right away, but we don't want
16240      them getting mixed up with functions that are currently in the
16241      queue.  */
16242   parser->unparsed_functions_queues
16243     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16244
16245   /* Make sure that any template parameters are in scope.  */
16246   maybe_begin_member_template_processing (member_function);
16247
16248   /* If the body of the function has not yet been parsed, parse it
16249      now.  */
16250   if (DECL_PENDING_INLINE_P (member_function))
16251     {
16252       tree function_scope;
16253       cp_token_cache *tokens;
16254
16255       /* The function is no longer pending; we are processing it.  */
16256       tokens = DECL_PENDING_INLINE_INFO (member_function);
16257       DECL_PENDING_INLINE_INFO (member_function) = NULL;
16258       DECL_PENDING_INLINE_P (member_function) = 0;
16259
16260       /* If this is a local class, enter the scope of the containing
16261          function.  */
16262       function_scope = current_function_decl;
16263       if (function_scope)
16264         push_function_context_to (function_scope);
16265
16266
16267       /* Push the body of the function onto the lexer stack.  */
16268       cp_parser_push_lexer_for_tokens (parser, tokens);
16269
16270       /* Let the front end know that we going to be defining this
16271          function.  */
16272       start_preparsed_function (member_function, NULL_TREE,
16273                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
16274
16275       /* Don't do access checking if it is a templated function.  */
16276       if (processing_template_decl)
16277         push_deferring_access_checks (dk_no_check);
16278
16279       /* Now, parse the body of the function.  */
16280       cp_parser_function_definition_after_declarator (parser,
16281                                                       /*inline_p=*/true);
16282
16283       if (processing_template_decl)
16284         pop_deferring_access_checks ();
16285
16286       /* Leave the scope of the containing function.  */
16287       if (function_scope)
16288         pop_function_context_from (function_scope);
16289       cp_parser_pop_lexer (parser);
16290     }
16291
16292   /* Remove any template parameters from the symbol table.  */
16293   maybe_end_member_template_processing ();
16294
16295   /* Restore the queue.  */
16296   parser->unparsed_functions_queues
16297     = TREE_CHAIN (parser->unparsed_functions_queues);
16298 }
16299
16300 /* If DECL contains any default args, remember it on the unparsed
16301    functions queue.  */
16302
16303 static void
16304 cp_parser_save_default_args (cp_parser* parser, tree decl)
16305 {
16306   tree probe;
16307
16308   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16309        probe;
16310        probe = TREE_CHAIN (probe))
16311     if (TREE_PURPOSE (probe))
16312       {
16313         TREE_PURPOSE (parser->unparsed_functions_queues)
16314           = tree_cons (current_class_type, decl,
16315                        TREE_PURPOSE (parser->unparsed_functions_queues));
16316         break;
16317       }
16318 }
16319
16320 /* FN is a FUNCTION_DECL which may contains a parameter with an
16321    unparsed DEFAULT_ARG.  Parse the default args now.  This function
16322    assumes that the current scope is the scope in which the default
16323    argument should be processed.  */
16324
16325 static void
16326 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16327 {
16328   bool saved_local_variables_forbidden_p;
16329   tree parm;
16330
16331   /* While we're parsing the default args, we might (due to the
16332      statement expression extension) encounter more classes.  We want
16333      to handle them right away, but we don't want them getting mixed
16334      up with default args that are currently in the queue.  */
16335   parser->unparsed_functions_queues
16336     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16337
16338   /* Local variable names (and the `this' keyword) may not appear
16339      in a default argument.  */
16340   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16341   parser->local_variables_forbidden_p = true;
16342
16343   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16344        parm;
16345        parm = TREE_CHAIN (parm))
16346     {
16347       cp_token_cache *tokens;
16348       tree default_arg = TREE_PURPOSE (parm);
16349       tree parsed_arg;
16350       VEC(tree,gc) *insts;
16351       tree copy;
16352       unsigned ix;
16353
16354       if (!default_arg)
16355         continue;
16356
16357       if (TREE_CODE (default_arg) != DEFAULT_ARG)
16358         /* This can happen for a friend declaration for a function
16359            already declared with default arguments.  */
16360         continue;
16361
16362        /* Push the saved tokens for the default argument onto the parser's
16363           lexer stack.  */
16364       tokens = DEFARG_TOKENS (default_arg);
16365       cp_parser_push_lexer_for_tokens (parser, tokens);
16366
16367       /* Parse the assignment-expression.  */
16368       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16369
16370       if (!processing_template_decl)
16371         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16372
16373       TREE_PURPOSE (parm) = parsed_arg;
16374
16375       /* Update any instantiations we've already created.  */
16376       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16377            VEC_iterate (tree, insts, ix, copy); ix++)
16378         TREE_PURPOSE (copy) = parsed_arg;
16379
16380       /* If the token stream has not been completely used up, then
16381          there was extra junk after the end of the default
16382          argument.  */
16383       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16384         cp_parser_error (parser, "expected %<,%>");
16385
16386       /* Revert to the main lexer.  */
16387       cp_parser_pop_lexer (parser);
16388     }
16389
16390   /* Make sure no default arg is missing.  */
16391   check_default_args (fn);
16392
16393   /* Restore the state of local_variables_forbidden_p.  */
16394   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16395
16396   /* Restore the queue.  */
16397   parser->unparsed_functions_queues
16398     = TREE_CHAIN (parser->unparsed_functions_queues);
16399 }
16400
16401 /* Parse the operand of `sizeof' (or a similar operator).  Returns
16402    either a TYPE or an expression, depending on the form of the
16403    input.  The KEYWORD indicates which kind of expression we have
16404    encountered.  */
16405
16406 static tree
16407 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16408 {
16409   static const char *format;
16410   tree expr = NULL_TREE;
16411   const char *saved_message;
16412   bool saved_integral_constant_expression_p;
16413   bool saved_non_integral_constant_expression_p;
16414
16415   /* Initialize FORMAT the first time we get here.  */
16416   if (!format)
16417     format = "types may not be defined in '%s' expressions";
16418
16419   /* Types cannot be defined in a `sizeof' expression.  Save away the
16420      old message.  */
16421   saved_message = parser->type_definition_forbidden_message;
16422   /* And create the new one.  */
16423   parser->type_definition_forbidden_message
16424     = XNEWVEC (const char, strlen (format)
16425                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16426                + 1 /* `\0' */);
16427   sprintf ((char *) parser->type_definition_forbidden_message,
16428            format, IDENTIFIER_POINTER (ridpointers[keyword]));
16429
16430   /* The restrictions on constant-expressions do not apply inside
16431      sizeof expressions.  */
16432   saved_integral_constant_expression_p
16433     = parser->integral_constant_expression_p;
16434   saved_non_integral_constant_expression_p
16435     = parser->non_integral_constant_expression_p;
16436   parser->integral_constant_expression_p = false;
16437
16438   /* Do not actually evaluate the expression.  */
16439   ++skip_evaluation;
16440   /* If it's a `(', then we might be looking at the type-id
16441      construction.  */
16442   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16443     {
16444       tree type;
16445       bool saved_in_type_id_in_expr_p;
16446
16447       /* We can't be sure yet whether we're looking at a type-id or an
16448          expression.  */
16449       cp_parser_parse_tentatively (parser);
16450       /* Consume the `('.  */
16451       cp_lexer_consume_token (parser->lexer);
16452       /* Parse the type-id.  */
16453       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16454       parser->in_type_id_in_expr_p = true;
16455       type = cp_parser_type_id (parser);
16456       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16457       /* Now, look for the trailing `)'.  */
16458       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16459       /* If all went well, then we're done.  */
16460       if (cp_parser_parse_definitely (parser))
16461         {
16462           cp_decl_specifier_seq decl_specs;
16463
16464           /* Build a trivial decl-specifier-seq.  */
16465           clear_decl_specs (&decl_specs);
16466           decl_specs.type = type;
16467
16468           /* Call grokdeclarator to figure out what type this is.  */
16469           expr = grokdeclarator (NULL,
16470                                  &decl_specs,
16471                                  TYPENAME,
16472                                  /*initialized=*/0,
16473                                  /*attrlist=*/NULL);
16474         }
16475     }
16476
16477   /* If the type-id production did not work out, then we must be
16478      looking at the unary-expression production.  */
16479   if (!expr)
16480     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16481                                        /*cast_p=*/false);
16482   /* Go back to evaluating expressions.  */
16483   --skip_evaluation;
16484
16485   /* Free the message we created.  */
16486   free ((char *) parser->type_definition_forbidden_message);
16487   /* And restore the old one.  */
16488   parser->type_definition_forbidden_message = saved_message;
16489   parser->integral_constant_expression_p
16490     = saved_integral_constant_expression_p;
16491   parser->non_integral_constant_expression_p
16492     = saved_non_integral_constant_expression_p;
16493
16494   return expr;
16495 }
16496
16497 /* If the current declaration has no declarator, return true.  */
16498
16499 static bool
16500 cp_parser_declares_only_class_p (cp_parser *parser)
16501 {
16502   /* If the next token is a `;' or a `,' then there is no
16503      declarator.  */
16504   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16505           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16506 }
16507
16508 /* Update the DECL_SPECS to reflect the storage class indicated by
16509    KEYWORD.  */
16510
16511 static void
16512 cp_parser_set_storage_class (cp_parser *parser,
16513                              cp_decl_specifier_seq *decl_specs,
16514                              enum rid keyword)
16515 {
16516   cp_storage_class storage_class;
16517
16518   if (parser->in_unbraced_linkage_specification_p)
16519     {
16520       error ("invalid use of %qD in linkage specification",
16521              ridpointers[keyword]);
16522       return;
16523     }
16524   else if (decl_specs->storage_class != sc_none)
16525     {
16526       decl_specs->conflicting_specifiers_p = true;
16527       return;
16528     }
16529
16530   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16531       && decl_specs->specs[(int) ds_thread])
16532     {
16533       error ("%<__thread%> before %qD", ridpointers[keyword]);
16534       decl_specs->specs[(int) ds_thread] = 0;
16535     }
16536
16537   switch (keyword)
16538     {
16539     case RID_AUTO:
16540       storage_class = sc_auto;
16541       break;
16542     case RID_REGISTER:
16543       storage_class = sc_register;
16544       break;
16545     case RID_STATIC:
16546       storage_class = sc_static;
16547       break;
16548     case RID_EXTERN:
16549       storage_class = sc_extern;
16550       break;
16551     case RID_MUTABLE:
16552       storage_class = sc_mutable;
16553       break;
16554     default:
16555       gcc_unreachable ();
16556     }
16557   decl_specs->storage_class = storage_class;
16558
16559   /* A storage class specifier cannot be applied alongside a typedef 
16560      specifier. If there is a typedef specifier present then set 
16561      conflicting_specifiers_p which will trigger an error later
16562      on in grokdeclarator. */
16563   if (decl_specs->specs[(int)ds_typedef])
16564     decl_specs->conflicting_specifiers_p = true;
16565 }
16566
16567 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16568    is true, the type is a user-defined type; otherwise it is a
16569    built-in type specified by a keyword.  */
16570
16571 static void
16572 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16573                               tree type_spec,
16574                               bool user_defined_p)
16575 {
16576   decl_specs->any_specifiers_p = true;
16577
16578   /* If the user tries to redeclare bool or wchar_t (with, for
16579      example, in "typedef int wchar_t;") we remember that this is what
16580      happened.  In system headers, we ignore these declarations so
16581      that G++ can work with system headers that are not C++-safe.  */
16582   if (decl_specs->specs[(int) ds_typedef]
16583       && !user_defined_p
16584       && (type_spec == boolean_type_node
16585           || type_spec == wchar_type_node)
16586       && (decl_specs->type
16587           || decl_specs->specs[(int) ds_long]
16588           || decl_specs->specs[(int) ds_short]
16589           || decl_specs->specs[(int) ds_unsigned]
16590           || decl_specs->specs[(int) ds_signed]))
16591     {
16592       decl_specs->redefined_builtin_type = type_spec;
16593       if (!decl_specs->type)
16594         {
16595           decl_specs->type = type_spec;
16596           decl_specs->user_defined_type_p = false;
16597         }
16598     }
16599   else if (decl_specs->type)
16600     decl_specs->multiple_types_p = true;
16601   else
16602     {
16603       decl_specs->type = type_spec;
16604       decl_specs->user_defined_type_p = user_defined_p;
16605       decl_specs->redefined_builtin_type = NULL_TREE;
16606     }
16607 }
16608
16609 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16610    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16611
16612 static bool
16613 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16614 {
16615   return decl_specifiers->specs[(int) ds_friend] != 0;
16616 }
16617
16618 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16619    issue an error message indicating that TOKEN_DESC was expected.
16620
16621    Returns the token consumed, if the token had the appropriate type.
16622    Otherwise, returns NULL.  */
16623
16624 static cp_token *
16625 cp_parser_require (cp_parser* parser,
16626                    enum cpp_ttype type,
16627                    const char* token_desc)
16628 {
16629   if (cp_lexer_next_token_is (parser->lexer, type))
16630     return cp_lexer_consume_token (parser->lexer);
16631   else
16632     {
16633       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16634       if (!cp_parser_simulate_error (parser))
16635         {
16636           char *message = concat ("expected ", token_desc, NULL);
16637           cp_parser_error (parser, message);
16638           free (message);
16639         }
16640       return NULL;
16641     }
16642 }
16643
16644 /* An error message is produced if the next token is not '>'.
16645    All further tokens are skipped until the desired token is
16646    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
16647
16648 static void
16649 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16650 {
16651   /* Current level of '< ... >'.  */
16652   unsigned level = 0;
16653   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
16654   unsigned nesting_depth = 0;
16655
16656   /* Are we ready, yet?  If not, issue error message.  */
16657   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16658     return;
16659
16660   /* Skip tokens until the desired token is found.  */
16661   while (true)
16662     {
16663       /* Peek at the next token.  */
16664       switch (cp_lexer_peek_token (parser->lexer)->type)
16665         {
16666         case CPP_LESS:
16667           if (!nesting_depth)
16668             ++level;
16669           break;
16670
16671         case CPP_GREATER:
16672           if (!nesting_depth && level-- == 0)
16673             {
16674               /* We've reached the token we want, consume it and stop.  */
16675               cp_lexer_consume_token (parser->lexer);
16676               return;
16677             }
16678           break;
16679
16680         case CPP_OPEN_PAREN:
16681         case CPP_OPEN_SQUARE:
16682           ++nesting_depth;
16683           break;
16684
16685         case CPP_CLOSE_PAREN:
16686         case CPP_CLOSE_SQUARE:
16687           if (nesting_depth-- == 0)
16688             return;
16689           break;
16690
16691         case CPP_EOF:
16692         case CPP_PRAGMA_EOL:
16693         case CPP_SEMICOLON:
16694         case CPP_OPEN_BRACE:
16695         case CPP_CLOSE_BRACE:
16696           /* The '>' was probably forgotten, don't look further.  */
16697           return;
16698
16699         default:
16700           break;
16701         }
16702
16703       /* Consume this token.  */
16704       cp_lexer_consume_token (parser->lexer);
16705     }
16706 }
16707
16708 /* If the next token is the indicated keyword, consume it.  Otherwise,
16709    issue an error message indicating that TOKEN_DESC was expected.
16710
16711    Returns the token consumed, if the token had the appropriate type.
16712    Otherwise, returns NULL.  */
16713
16714 static cp_token *
16715 cp_parser_require_keyword (cp_parser* parser,
16716                            enum rid keyword,
16717                            const char* token_desc)
16718 {
16719   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16720
16721   if (token && token->keyword != keyword)
16722     {
16723       dyn_string_t error_msg;
16724
16725       /* Format the error message.  */
16726       error_msg = dyn_string_new (0);
16727       dyn_string_append_cstr (error_msg, "expected ");
16728       dyn_string_append_cstr (error_msg, token_desc);
16729       cp_parser_error (parser, error_msg->s);
16730       dyn_string_delete (error_msg);
16731       return NULL;
16732     }
16733
16734   return token;
16735 }
16736
16737 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16738    function-definition.  */
16739
16740 static bool
16741 cp_parser_token_starts_function_definition_p (cp_token* token)
16742 {
16743   return (/* An ordinary function-body begins with an `{'.  */
16744           token->type == CPP_OPEN_BRACE
16745           /* A ctor-initializer begins with a `:'.  */
16746           || token->type == CPP_COLON
16747           /* A function-try-block begins with `try'.  */
16748           || token->keyword == RID_TRY
16749           /* The named return value extension begins with `return'.  */
16750           || token->keyword == RID_RETURN);
16751 }
16752
16753 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16754    definition.  */
16755
16756 static bool
16757 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16758 {
16759   cp_token *token;
16760
16761   token = cp_lexer_peek_token (parser->lexer);
16762   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16763 }
16764
16765 /* Returns TRUE iff the next token is the "," or ">" ending a
16766    template-argument.  */
16767
16768 static bool
16769 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16770 {
16771   cp_token *token;
16772
16773   token = cp_lexer_peek_token (parser->lexer);
16774   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16775 }
16776
16777 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16778    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16779
16780 static bool
16781 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16782                                                      size_t n)
16783 {
16784   cp_token *token;
16785
16786   token = cp_lexer_peek_nth_token (parser->lexer, n);
16787   if (token->type == CPP_LESS)
16788     return true;
16789   /* Check for the sequence `<::' in the original code. It would be lexed as
16790      `[:', where `[' is a digraph, and there is no whitespace before
16791      `:'.  */
16792   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16793     {
16794       cp_token *token2;
16795       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16796       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16797         return true;
16798     }
16799   return false;
16800 }
16801
16802 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16803    or none_type otherwise.  */
16804
16805 static enum tag_types
16806 cp_parser_token_is_class_key (cp_token* token)
16807 {
16808   switch (token->keyword)
16809     {
16810     case RID_CLASS:
16811       return class_type;
16812     case RID_STRUCT:
16813       return record_type;
16814     case RID_UNION:
16815       return union_type;
16816
16817     default:
16818       return none_type;
16819     }
16820 }
16821
16822 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16823
16824 static void
16825 cp_parser_check_class_key (enum tag_types class_key, tree type)
16826 {
16827   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16828     pedwarn ("%qs tag used in naming %q#T",
16829             class_key == union_type ? "union"
16830              : class_key == record_type ? "struct" : "class",
16831              type);
16832 }
16833
16834 /* Issue an error message if DECL is redeclared with different
16835    access than its original declaration [class.access.spec/3].
16836    This applies to nested classes and nested class templates.
16837    [class.mem/1].  */
16838
16839 static void
16840 cp_parser_check_access_in_redeclaration (tree decl)
16841 {
16842   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16843     return;
16844
16845   if ((TREE_PRIVATE (decl)
16846        != (current_access_specifier == access_private_node))
16847       || (TREE_PROTECTED (decl)
16848           != (current_access_specifier == access_protected_node)))
16849     error ("%qD redeclared with different access", decl);
16850 }
16851
16852 /* Look for the `template' keyword, as a syntactic disambiguator.
16853    Return TRUE iff it is present, in which case it will be
16854    consumed.  */
16855
16856 static bool
16857 cp_parser_optional_template_keyword (cp_parser *parser)
16858 {
16859   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16860     {
16861       /* The `template' keyword can only be used within templates;
16862          outside templates the parser can always figure out what is a
16863          template and what is not.  */
16864       if (!processing_template_decl)
16865         {
16866           error ("%<template%> (as a disambiguator) is only allowed "
16867                  "within templates");
16868           /* If this part of the token stream is rescanned, the same
16869              error message would be generated.  So, we purge the token
16870              from the stream.  */
16871           cp_lexer_purge_token (parser->lexer);
16872           return false;
16873         }
16874       else
16875         {
16876           /* Consume the `template' keyword.  */
16877           cp_lexer_consume_token (parser->lexer);
16878           return true;
16879         }
16880     }
16881
16882   return false;
16883 }
16884
16885 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16886    set PARSER->SCOPE, and perform other related actions.  */
16887
16888 static void
16889 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16890 {
16891   int i;
16892   struct tree_check *check_value;
16893   deferred_access_check *chk;
16894   VEC (deferred_access_check,gc) *checks;
16895
16896   /* Get the stored value.  */
16897   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
16898   /* Perform any access checks that were deferred.  */
16899   checks = check_value->checks;
16900   if (checks)
16901     {
16902       for (i = 0 ;
16903            VEC_iterate (deferred_access_check, checks, i, chk) ;
16904            ++i)
16905         {
16906           perform_or_defer_access_check (chk->binfo,
16907                                          chk->decl,
16908                                          chk->diag_decl);
16909         }
16910     }
16911   /* Set the scope from the stored value.  */
16912   parser->scope = check_value->value;
16913   parser->qualifying_scope = check_value->qualifying_scope;
16914   parser->object_scope = NULL_TREE;
16915 }
16916
16917 /* Consume tokens up through a non-nested END token.  */
16918
16919 static void
16920 cp_parser_cache_group (cp_parser *parser,
16921                        enum cpp_ttype end,
16922                        unsigned depth)
16923 {
16924   while (true)
16925     {
16926       cp_token *token;
16927
16928       /* Abort a parenthesized expression if we encounter a brace.  */
16929       if ((end == CPP_CLOSE_PAREN || depth == 0)
16930           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16931         return;
16932       /* If we've reached the end of the file, stop.  */
16933       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16934           || (end != CPP_PRAGMA_EOL
16935               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16936         return;
16937       /* Consume the next token.  */
16938       token = cp_lexer_consume_token (parser->lexer);
16939       /* See if it starts a new group.  */
16940       if (token->type == CPP_OPEN_BRACE)
16941         {
16942           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16943           if (depth == 0)
16944             return;
16945         }
16946       else if (token->type == CPP_OPEN_PAREN)
16947         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16948       else if (token->type == CPP_PRAGMA)
16949         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16950       else if (token->type == end)
16951         return;
16952     }
16953 }
16954
16955 /* Begin parsing tentatively.  We always save tokens while parsing
16956    tentatively so that if the tentative parsing fails we can restore the
16957    tokens.  */
16958
16959 static void
16960 cp_parser_parse_tentatively (cp_parser* parser)
16961 {
16962   /* Enter a new parsing context.  */
16963   parser->context = cp_parser_context_new (parser->context);
16964   /* Begin saving tokens.  */
16965   cp_lexer_save_tokens (parser->lexer);
16966   /* In order to avoid repetitive access control error messages,
16967      access checks are queued up until we are no longer parsing
16968      tentatively.  */
16969   push_deferring_access_checks (dk_deferred);
16970 }
16971
16972 /* Commit to the currently active tentative parse.  */
16973
16974 static void
16975 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16976 {
16977   cp_parser_context *context;
16978   cp_lexer *lexer;
16979
16980   /* Mark all of the levels as committed.  */
16981   lexer = parser->lexer;
16982   for (context = parser->context; context->next; context = context->next)
16983     {
16984       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16985         break;
16986       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16987       while (!cp_lexer_saving_tokens (lexer))
16988         lexer = lexer->next;
16989       cp_lexer_commit_tokens (lexer);
16990     }
16991 }
16992
16993 /* Abort the currently active tentative parse.  All consumed tokens
16994    will be rolled back, and no diagnostics will be issued.  */
16995
16996 static void
16997 cp_parser_abort_tentative_parse (cp_parser* parser)
16998 {
16999   cp_parser_simulate_error (parser);
17000   /* Now, pretend that we want to see if the construct was
17001      successfully parsed.  */
17002   cp_parser_parse_definitely (parser);
17003 }
17004
17005 /* Stop parsing tentatively.  If a parse error has occurred, restore the
17006    token stream.  Otherwise, commit to the tokens we have consumed.
17007    Returns true if no error occurred; false otherwise.  */
17008
17009 static bool
17010 cp_parser_parse_definitely (cp_parser* parser)
17011 {
17012   bool error_occurred;
17013   cp_parser_context *context;
17014
17015   /* Remember whether or not an error occurred, since we are about to
17016      destroy that information.  */
17017   error_occurred = cp_parser_error_occurred (parser);
17018   /* Remove the topmost context from the stack.  */
17019   context = parser->context;
17020   parser->context = context->next;
17021   /* If no parse errors occurred, commit to the tentative parse.  */
17022   if (!error_occurred)
17023     {
17024       /* Commit to the tokens read tentatively, unless that was
17025          already done.  */
17026       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17027         cp_lexer_commit_tokens (parser->lexer);
17028
17029       pop_to_parent_deferring_access_checks ();
17030     }
17031   /* Otherwise, if errors occurred, roll back our state so that things
17032      are just as they were before we began the tentative parse.  */
17033   else
17034     {
17035       cp_lexer_rollback_tokens (parser->lexer);
17036       pop_deferring_access_checks ();
17037     }
17038   /* Add the context to the front of the free list.  */
17039   context->next = cp_parser_context_free_list;
17040   cp_parser_context_free_list = context;
17041
17042   return !error_occurred;
17043 }
17044
17045 /* Returns true if we are parsing tentatively and are not committed to
17046    this tentative parse.  */
17047
17048 static bool
17049 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17050 {
17051   return (cp_parser_parsing_tentatively (parser)
17052           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17053 }
17054
17055 /* Returns nonzero iff an error has occurred during the most recent
17056    tentative parse.  */
17057
17058 static bool
17059 cp_parser_error_occurred (cp_parser* parser)
17060 {
17061   return (cp_parser_parsing_tentatively (parser)
17062           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17063 }
17064
17065 /* Returns nonzero if GNU extensions are allowed.  */
17066
17067 static bool
17068 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17069 {
17070   return parser->allow_gnu_extensions_p;
17071 }
17072 \f
17073 /* Objective-C++ Productions */
17074
17075
17076 /* Parse an Objective-C expression, which feeds into a primary-expression
17077    above.
17078
17079    objc-expression:
17080      objc-message-expression
17081      objc-string-literal
17082      objc-encode-expression
17083      objc-protocol-expression
17084      objc-selector-expression
17085
17086   Returns a tree representation of the expression.  */
17087
17088 static tree
17089 cp_parser_objc_expression (cp_parser* parser)
17090 {
17091   /* Try to figure out what kind of declaration is present.  */
17092   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17093
17094   switch (kwd->type)
17095     {
17096     case CPP_OPEN_SQUARE:
17097       return cp_parser_objc_message_expression (parser);
17098
17099     case CPP_OBJC_STRING:
17100       kwd = cp_lexer_consume_token (parser->lexer);
17101       return objc_build_string_object (kwd->u.value);
17102
17103     case CPP_KEYWORD:
17104       switch (kwd->keyword)
17105         {
17106         case RID_AT_ENCODE:
17107           return cp_parser_objc_encode_expression (parser);
17108
17109         case RID_AT_PROTOCOL:
17110           return cp_parser_objc_protocol_expression (parser);
17111
17112         case RID_AT_SELECTOR:
17113           return cp_parser_objc_selector_expression (parser);
17114
17115         default:
17116           break;
17117         }
17118     default:
17119       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17120       cp_parser_skip_to_end_of_block_or_statement (parser);
17121     }
17122
17123   return error_mark_node;
17124 }
17125
17126 /* Parse an Objective-C message expression.
17127
17128    objc-message-expression:
17129      [ objc-message-receiver objc-message-args ]
17130
17131    Returns a representation of an Objective-C message.  */
17132
17133 static tree
17134 cp_parser_objc_message_expression (cp_parser* parser)
17135 {
17136   tree receiver, messageargs;
17137
17138   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
17139   receiver = cp_parser_objc_message_receiver (parser);
17140   messageargs = cp_parser_objc_message_args (parser);
17141   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17142
17143   return objc_build_message_expr (build_tree_list (receiver, messageargs));
17144 }
17145
17146 /* Parse an objc-message-receiver.
17147
17148    objc-message-receiver:
17149      expression
17150      simple-type-specifier
17151
17152   Returns a representation of the type or expression.  */
17153
17154 static tree
17155 cp_parser_objc_message_receiver (cp_parser* parser)
17156 {
17157   tree rcv;
17158
17159   /* An Objective-C message receiver may be either (1) a type
17160      or (2) an expression.  */
17161   cp_parser_parse_tentatively (parser);
17162   rcv = cp_parser_expression (parser, false);
17163
17164   if (cp_parser_parse_definitely (parser))
17165     return rcv;
17166
17167   rcv = cp_parser_simple_type_specifier (parser,
17168                                          /*decl_specs=*/NULL,
17169                                          CP_PARSER_FLAGS_NONE);
17170
17171   return objc_get_class_reference (rcv);
17172 }
17173
17174 /* Parse the arguments and selectors comprising an Objective-C message.
17175
17176    objc-message-args:
17177      objc-selector
17178      objc-selector-args
17179      objc-selector-args , objc-comma-args
17180
17181    objc-selector-args:
17182      objc-selector [opt] : assignment-expression
17183      objc-selector-args objc-selector [opt] : assignment-expression
17184
17185    objc-comma-args:
17186      assignment-expression
17187      objc-comma-args , assignment-expression
17188
17189    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17190    selector arguments and TREE_VALUE containing a list of comma
17191    arguments.  */
17192
17193 static tree
17194 cp_parser_objc_message_args (cp_parser* parser)
17195 {
17196   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17197   bool maybe_unary_selector_p = true;
17198   cp_token *token = cp_lexer_peek_token (parser->lexer);
17199
17200   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17201     {
17202       tree selector = NULL_TREE, arg;
17203
17204       if (token->type != CPP_COLON)
17205         selector = cp_parser_objc_selector (parser);
17206
17207       /* Detect if we have a unary selector.  */
17208       if (maybe_unary_selector_p
17209           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17210         return build_tree_list (selector, NULL_TREE);
17211
17212       maybe_unary_selector_p = false;
17213       cp_parser_require (parser, CPP_COLON, "`:'");
17214       arg = cp_parser_assignment_expression (parser, false);
17215
17216       sel_args
17217         = chainon (sel_args,
17218                    build_tree_list (selector, arg));
17219
17220       token = cp_lexer_peek_token (parser->lexer);
17221     }
17222
17223   /* Handle non-selector arguments, if any. */
17224   while (token->type == CPP_COMMA)
17225     {
17226       tree arg;
17227
17228       cp_lexer_consume_token (parser->lexer);
17229       arg = cp_parser_assignment_expression (parser, false);
17230
17231       addl_args
17232         = chainon (addl_args,
17233                    build_tree_list (NULL_TREE, arg));
17234
17235       token = cp_lexer_peek_token (parser->lexer);
17236     }
17237
17238   return build_tree_list (sel_args, addl_args);
17239 }
17240
17241 /* Parse an Objective-C encode expression.
17242
17243    objc-encode-expression:
17244      @encode objc-typename
17245
17246    Returns an encoded representation of the type argument.  */
17247
17248 static tree
17249 cp_parser_objc_encode_expression (cp_parser* parser)
17250 {
17251   tree type;
17252
17253   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
17254   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17255   type = complete_type (cp_parser_type_id (parser));
17256   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17257
17258   if (!type)
17259     {
17260       error ("%<@encode%> must specify a type as an argument");
17261       return error_mark_node;
17262     }
17263
17264   return objc_build_encode_expr (type);
17265 }
17266
17267 /* Parse an Objective-C @defs expression.  */
17268
17269 static tree
17270 cp_parser_objc_defs_expression (cp_parser *parser)
17271 {
17272   tree name;
17273
17274   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
17275   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17276   name = cp_parser_identifier (parser);
17277   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17278
17279   return objc_get_class_ivars (name);
17280 }
17281
17282 /* Parse an Objective-C protocol expression.
17283
17284   objc-protocol-expression:
17285     @protocol ( identifier )
17286
17287   Returns a representation of the protocol expression.  */
17288
17289 static tree
17290 cp_parser_objc_protocol_expression (cp_parser* parser)
17291 {
17292   tree proto;
17293
17294   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17295   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17296   proto = cp_parser_identifier (parser);
17297   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17298
17299   return objc_build_protocol_expr (proto);
17300 }
17301
17302 /* Parse an Objective-C selector expression.
17303
17304    objc-selector-expression:
17305      @selector ( objc-method-signature )
17306
17307    objc-method-signature:
17308      objc-selector
17309      objc-selector-seq
17310
17311    objc-selector-seq:
17312      objc-selector :
17313      objc-selector-seq objc-selector :
17314
17315   Returns a representation of the method selector.  */
17316
17317 static tree
17318 cp_parser_objc_selector_expression (cp_parser* parser)
17319 {
17320   tree sel_seq = NULL_TREE;
17321   bool maybe_unary_selector_p = true;
17322   cp_token *token;
17323
17324   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
17325   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17326   token = cp_lexer_peek_token (parser->lexer);
17327
17328   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17329          || token->type == CPP_SCOPE)
17330     {
17331       tree selector = NULL_TREE;
17332
17333       if (token->type != CPP_COLON
17334           || token->type == CPP_SCOPE)
17335         selector = cp_parser_objc_selector (parser);
17336
17337       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17338           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17339         {
17340           /* Detect if we have a unary selector.  */
17341           if (maybe_unary_selector_p)
17342             {
17343               sel_seq = selector;
17344               goto finish_selector;
17345             }
17346           else
17347             {
17348               cp_parser_error (parser, "expected %<:%>");
17349             }
17350         }
17351       maybe_unary_selector_p = false;
17352       token = cp_lexer_consume_token (parser->lexer);
17353
17354       if (token->type == CPP_SCOPE)
17355         {
17356           sel_seq
17357             = chainon (sel_seq,
17358                        build_tree_list (selector, NULL_TREE));
17359           sel_seq
17360             = chainon (sel_seq,
17361                        build_tree_list (NULL_TREE, NULL_TREE));
17362         }
17363       else
17364         sel_seq
17365           = chainon (sel_seq,
17366                      build_tree_list (selector, NULL_TREE));
17367
17368       token = cp_lexer_peek_token (parser->lexer);
17369     }
17370
17371  finish_selector:
17372   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17373
17374   return objc_build_selector_expr (sel_seq);
17375 }
17376
17377 /* Parse a list of identifiers.
17378
17379    objc-identifier-list:
17380      identifier
17381      objc-identifier-list , identifier
17382
17383    Returns a TREE_LIST of identifier nodes.  */
17384
17385 static tree
17386 cp_parser_objc_identifier_list (cp_parser* parser)
17387 {
17388   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17389   cp_token *sep = cp_lexer_peek_token (parser->lexer);
17390
17391   while (sep->type == CPP_COMMA)
17392     {
17393       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17394       list = chainon (list,
17395                       build_tree_list (NULL_TREE,
17396                                        cp_parser_identifier (parser)));
17397       sep = cp_lexer_peek_token (parser->lexer);
17398     }
17399
17400   return list;
17401 }
17402
17403 /* Parse an Objective-C alias declaration.
17404
17405    objc-alias-declaration:
17406      @compatibility_alias identifier identifier ;
17407
17408    This function registers the alias mapping with the Objective-C front-end.
17409    It returns nothing.  */
17410
17411 static void
17412 cp_parser_objc_alias_declaration (cp_parser* parser)
17413 {
17414   tree alias, orig;
17415
17416   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17417   alias = cp_parser_identifier (parser);
17418   orig = cp_parser_identifier (parser);
17419   objc_declare_alias (alias, orig);
17420   cp_parser_consume_semicolon_at_end_of_statement (parser);
17421 }
17422
17423 /* Parse an Objective-C class forward-declaration.
17424
17425    objc-class-declaration:
17426      @class objc-identifier-list ;
17427
17428    The function registers the forward declarations with the Objective-C
17429    front-end.  It returns nothing.  */
17430
17431 static void
17432 cp_parser_objc_class_declaration (cp_parser* parser)
17433 {
17434   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17435   objc_declare_class (cp_parser_objc_identifier_list (parser));
17436   cp_parser_consume_semicolon_at_end_of_statement (parser);
17437 }
17438
17439 /* Parse a list of Objective-C protocol references.
17440
17441    objc-protocol-refs-opt:
17442      objc-protocol-refs [opt]
17443
17444    objc-protocol-refs:
17445      < objc-identifier-list >
17446
17447    Returns a TREE_LIST of identifiers, if any.  */
17448
17449 static tree
17450 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17451 {
17452   tree protorefs = NULL_TREE;
17453
17454   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17455     {
17456       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17457       protorefs = cp_parser_objc_identifier_list (parser);
17458       cp_parser_require (parser, CPP_GREATER, "`>'");
17459     }
17460
17461   return protorefs;
17462 }
17463
17464 /* Parse a Objective-C visibility specification.  */
17465
17466 static void
17467 cp_parser_objc_visibility_spec (cp_parser* parser)
17468 {
17469   cp_token *vis = cp_lexer_peek_token (parser->lexer);
17470
17471   switch (vis->keyword)
17472     {
17473     case RID_AT_PRIVATE:
17474       objc_set_visibility (2);
17475       break;
17476     case RID_AT_PROTECTED:
17477       objc_set_visibility (0);
17478       break;
17479     case RID_AT_PUBLIC:
17480       objc_set_visibility (1);
17481       break;
17482     default:
17483       return;
17484     }
17485
17486   /* Eat '@private'/'@protected'/'@public'.  */
17487   cp_lexer_consume_token (parser->lexer);
17488 }
17489
17490 /* Parse an Objective-C method type.  */
17491
17492 static void
17493 cp_parser_objc_method_type (cp_parser* parser)
17494 {
17495   objc_set_method_type
17496    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17497     ? PLUS_EXPR
17498     : MINUS_EXPR);
17499 }
17500
17501 /* Parse an Objective-C protocol qualifier.  */
17502
17503 static tree
17504 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17505 {
17506   tree quals = NULL_TREE, node;
17507   cp_token *token = cp_lexer_peek_token (parser->lexer);
17508
17509   node = token->u.value;
17510
17511   while (node && TREE_CODE (node) == IDENTIFIER_NODE
17512          && (node == ridpointers [(int) RID_IN]
17513              || node == ridpointers [(int) RID_OUT]
17514              || node == ridpointers [(int) RID_INOUT]
17515              || node == ridpointers [(int) RID_BYCOPY]
17516              || node == ridpointers [(int) RID_BYREF]
17517              || node == ridpointers [(int) RID_ONEWAY]))
17518     {
17519       quals = tree_cons (NULL_TREE, node, quals);
17520       cp_lexer_consume_token (parser->lexer);
17521       token = cp_lexer_peek_token (parser->lexer);
17522       node = token->u.value;
17523     }
17524
17525   return quals;
17526 }
17527
17528 /* Parse an Objective-C typename.  */
17529
17530 static tree
17531 cp_parser_objc_typename (cp_parser* parser)
17532 {
17533   tree typename = NULL_TREE;
17534
17535   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17536     {
17537       tree proto_quals, cp_type = NULL_TREE;
17538
17539       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17540       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17541
17542       /* An ObjC type name may consist of just protocol qualifiers, in which
17543          case the type shall default to 'id'.  */
17544       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17545         cp_type = cp_parser_type_id (parser);
17546
17547       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17548       typename = build_tree_list (proto_quals, cp_type);
17549     }
17550
17551   return typename;
17552 }
17553
17554 /* Check to see if TYPE refers to an Objective-C selector name.  */
17555
17556 static bool
17557 cp_parser_objc_selector_p (enum cpp_ttype type)
17558 {
17559   return (type == CPP_NAME || type == CPP_KEYWORD
17560           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17561           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17562           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17563           || type == CPP_XOR || type == CPP_XOR_EQ);
17564 }
17565
17566 /* Parse an Objective-C selector.  */
17567
17568 static tree
17569 cp_parser_objc_selector (cp_parser* parser)
17570 {
17571   cp_token *token = cp_lexer_consume_token (parser->lexer);
17572
17573   if (!cp_parser_objc_selector_p (token->type))
17574     {
17575       error ("invalid Objective-C++ selector name");
17576       return error_mark_node;
17577     }
17578
17579   /* C++ operator names are allowed to appear in ObjC selectors.  */
17580   switch (token->type)
17581     {
17582     case CPP_AND_AND: return get_identifier ("and");
17583     case CPP_AND_EQ: return get_identifier ("and_eq");
17584     case CPP_AND: return get_identifier ("bitand");
17585     case CPP_OR: return get_identifier ("bitor");
17586     case CPP_COMPL: return get_identifier ("compl");
17587     case CPP_NOT: return get_identifier ("not");
17588     case CPP_NOT_EQ: return get_identifier ("not_eq");
17589     case CPP_OR_OR: return get_identifier ("or");
17590     case CPP_OR_EQ: return get_identifier ("or_eq");
17591     case CPP_XOR: return get_identifier ("xor");
17592     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17593     default: return token->u.value;
17594     }
17595 }
17596
17597 /* Parse an Objective-C params list.  */
17598
17599 static tree
17600 cp_parser_objc_method_keyword_params (cp_parser* parser)
17601 {
17602   tree params = NULL_TREE;
17603   bool maybe_unary_selector_p = true;
17604   cp_token *token = cp_lexer_peek_token (parser->lexer);
17605
17606   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17607     {
17608       tree selector = NULL_TREE, typename, identifier;
17609
17610       if (token->type != CPP_COLON)
17611         selector = cp_parser_objc_selector (parser);
17612
17613       /* Detect if we have a unary selector.  */
17614       if (maybe_unary_selector_p
17615           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17616         return selector;
17617
17618       maybe_unary_selector_p = false;
17619       cp_parser_require (parser, CPP_COLON, "`:'");
17620       typename = cp_parser_objc_typename (parser);
17621       identifier = cp_parser_identifier (parser);
17622
17623       params
17624         = chainon (params,
17625                    objc_build_keyword_decl (selector,
17626                                             typename,
17627                                             identifier));
17628
17629       token = cp_lexer_peek_token (parser->lexer);
17630     }
17631
17632   return params;
17633 }
17634
17635 /* Parse the non-keyword Objective-C params.  */
17636
17637 static tree
17638 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17639 {
17640   tree params = make_node (TREE_LIST);
17641   cp_token *token = cp_lexer_peek_token (parser->lexer);
17642   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17643
17644   while (token->type == CPP_COMMA)
17645     {
17646       cp_parameter_declarator *parmdecl;
17647       tree parm;
17648
17649       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17650       token = cp_lexer_peek_token (parser->lexer);
17651
17652       if (token->type == CPP_ELLIPSIS)
17653         {
17654           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17655           *ellipsisp = true;
17656           break;
17657         }
17658
17659       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17660       parm = grokdeclarator (parmdecl->declarator,
17661                              &parmdecl->decl_specifiers,
17662                              PARM, /*initialized=*/0,
17663                              /*attrlist=*/NULL);
17664
17665       chainon (params, build_tree_list (NULL_TREE, parm));
17666       token = cp_lexer_peek_token (parser->lexer);
17667     }
17668
17669   return params;
17670 }
17671
17672 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17673
17674 static void
17675 cp_parser_objc_interstitial_code (cp_parser* parser)
17676 {
17677   cp_token *token = cp_lexer_peek_token (parser->lexer);
17678
17679   /* If the next token is `extern' and the following token is a string
17680      literal, then we have a linkage specification.  */
17681   if (token->keyword == RID_EXTERN
17682       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17683     cp_parser_linkage_specification (parser);
17684   /* Handle #pragma, if any.  */
17685   else if (token->type == CPP_PRAGMA)
17686     cp_parser_pragma (parser, pragma_external);
17687   /* Allow stray semicolons.  */
17688   else if (token->type == CPP_SEMICOLON)
17689     cp_lexer_consume_token (parser->lexer);
17690   /* Finally, try to parse a block-declaration, or a function-definition.  */
17691   else
17692     cp_parser_block_declaration (parser, /*statement_p=*/false);
17693 }
17694
17695 /* Parse a method signature.  */
17696
17697 static tree
17698 cp_parser_objc_method_signature (cp_parser* parser)
17699 {
17700   tree rettype, kwdparms, optparms;
17701   bool ellipsis = false;
17702
17703   cp_parser_objc_method_type (parser);
17704   rettype = cp_parser_objc_typename (parser);
17705   kwdparms = cp_parser_objc_method_keyword_params (parser);
17706   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17707
17708   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17709 }
17710
17711 /* Pars an Objective-C method prototype list.  */
17712
17713 static void
17714 cp_parser_objc_method_prototype_list (cp_parser* parser)
17715 {
17716   cp_token *token = cp_lexer_peek_token (parser->lexer);
17717
17718   while (token->keyword != RID_AT_END)
17719     {
17720       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17721         {
17722           objc_add_method_declaration
17723            (cp_parser_objc_method_signature (parser));
17724           cp_parser_consume_semicolon_at_end_of_statement (parser);
17725         }
17726       else
17727         /* Allow for interspersed non-ObjC++ code.  */
17728         cp_parser_objc_interstitial_code (parser);
17729
17730       token = cp_lexer_peek_token (parser->lexer);
17731     }
17732
17733   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17734   objc_finish_interface ();
17735 }
17736
17737 /* Parse an Objective-C method definition list.  */
17738
17739 static void
17740 cp_parser_objc_method_definition_list (cp_parser* parser)
17741 {
17742   cp_token *token = cp_lexer_peek_token (parser->lexer);
17743
17744   while (token->keyword != RID_AT_END)
17745     {
17746       tree meth;
17747
17748       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17749         {
17750           push_deferring_access_checks (dk_deferred);
17751           objc_start_method_definition
17752            (cp_parser_objc_method_signature (parser));
17753
17754           /* For historical reasons, we accept an optional semicolon.  */
17755           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17756             cp_lexer_consume_token (parser->lexer);
17757
17758           perform_deferred_access_checks ();
17759           stop_deferring_access_checks ();
17760           meth = cp_parser_function_definition_after_declarator (parser,
17761                                                                  false);
17762           pop_deferring_access_checks ();
17763           objc_finish_method_definition (meth);
17764         }
17765       else
17766         /* Allow for interspersed non-ObjC++ code.  */
17767         cp_parser_objc_interstitial_code (parser);
17768
17769       token = cp_lexer_peek_token (parser->lexer);
17770     }
17771
17772   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17773   objc_finish_implementation ();
17774 }
17775
17776 /* Parse Objective-C ivars.  */
17777
17778 static void
17779 cp_parser_objc_class_ivars (cp_parser* parser)
17780 {
17781   cp_token *token = cp_lexer_peek_token (parser->lexer);
17782
17783   if (token->type != CPP_OPEN_BRACE)
17784     return;     /* No ivars specified.  */
17785
17786   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17787   token = cp_lexer_peek_token (parser->lexer);
17788
17789   while (token->type != CPP_CLOSE_BRACE)
17790     {
17791       cp_decl_specifier_seq declspecs;
17792       int decl_class_or_enum_p;
17793       tree prefix_attributes;
17794
17795       cp_parser_objc_visibility_spec (parser);
17796
17797       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17798         break;
17799
17800       cp_parser_decl_specifier_seq (parser,
17801                                     CP_PARSER_FLAGS_OPTIONAL,
17802                                     &declspecs,
17803                                     &decl_class_or_enum_p);
17804       prefix_attributes = declspecs.attributes;
17805       declspecs.attributes = NULL_TREE;
17806
17807       /* Keep going until we hit the `;' at the end of the
17808          declaration.  */
17809       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17810         {
17811           tree width = NULL_TREE, attributes, first_attribute, decl;
17812           cp_declarator *declarator = NULL;
17813           int ctor_dtor_or_conv_p;
17814
17815           /* Check for a (possibly unnamed) bitfield declaration.  */
17816           token = cp_lexer_peek_token (parser->lexer);
17817           if (token->type == CPP_COLON)
17818             goto eat_colon;
17819
17820           if (token->type == CPP_NAME
17821               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17822                   == CPP_COLON))
17823             {
17824               /* Get the name of the bitfield.  */
17825               declarator = make_id_declarator (NULL_TREE,
17826                                                cp_parser_identifier (parser),
17827                                                sfk_none);
17828
17829              eat_colon:
17830               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17831               /* Get the width of the bitfield.  */
17832               width
17833                 = cp_parser_constant_expression (parser,
17834                                                  /*allow_non_constant=*/false,
17835                                                  NULL);
17836             }
17837           else
17838             {
17839               /* Parse the declarator.  */
17840               declarator
17841                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17842                                         &ctor_dtor_or_conv_p,
17843                                         /*parenthesized_p=*/NULL,
17844                                         /*member_p=*/false);
17845             }
17846
17847           /* Look for attributes that apply to the ivar.  */
17848           attributes = cp_parser_attributes_opt (parser);
17849           /* Remember which attributes are prefix attributes and
17850              which are not.  */
17851           first_attribute = attributes;
17852           /* Combine the attributes.  */
17853           attributes = chainon (prefix_attributes, attributes);
17854
17855           if (width)
17856             {
17857               /* Create the bitfield declaration.  */
17858               decl = grokbitfield (declarator, &declspecs, width);
17859               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17860             }
17861           else
17862             decl = grokfield (declarator, &declspecs,
17863                               NULL_TREE, /*init_const_expr_p=*/false,
17864                               NULL_TREE, attributes);
17865
17866           /* Add the instance variable.  */
17867           objc_add_instance_variable (decl);
17868
17869           /* Reset PREFIX_ATTRIBUTES.  */
17870           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17871             attributes = TREE_CHAIN (attributes);
17872           if (attributes)
17873             TREE_CHAIN (attributes) = NULL_TREE;
17874
17875           token = cp_lexer_peek_token (parser->lexer);
17876
17877           if (token->type == CPP_COMMA)
17878             {
17879               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17880               continue;
17881             }
17882           break;
17883         }
17884
17885       cp_parser_consume_semicolon_at_end_of_statement (parser);
17886       token = cp_lexer_peek_token (parser->lexer);
17887     }
17888
17889   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17890   /* For historical reasons, we accept an optional semicolon.  */
17891   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17892     cp_lexer_consume_token (parser->lexer);
17893 }
17894
17895 /* Parse an Objective-C protocol declaration.  */
17896
17897 static void
17898 cp_parser_objc_protocol_declaration (cp_parser* parser)
17899 {
17900   tree proto, protorefs;
17901   cp_token *tok;
17902
17903   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17904   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17905     {
17906       error ("identifier expected after %<@protocol%>");
17907       goto finish;
17908     }
17909
17910   /* See if we have a forward declaration or a definition.  */
17911   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17912
17913   /* Try a forward declaration first.  */
17914   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17915     {
17916       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17917      finish:
17918       cp_parser_consume_semicolon_at_end_of_statement (parser);
17919     }
17920
17921   /* Ok, we got a full-fledged definition (or at least should).  */
17922   else
17923     {
17924       proto = cp_parser_identifier (parser);
17925       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17926       objc_start_protocol (proto, protorefs);
17927       cp_parser_objc_method_prototype_list (parser);
17928     }
17929 }
17930
17931 /* Parse an Objective-C superclass or category.  */
17932
17933 static void
17934 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17935                                                           tree *categ)
17936 {
17937   cp_token *next = cp_lexer_peek_token (parser->lexer);
17938
17939   *super = *categ = NULL_TREE;
17940   if (next->type == CPP_COLON)
17941     {
17942       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17943       *super = cp_parser_identifier (parser);
17944     }
17945   else if (next->type == CPP_OPEN_PAREN)
17946     {
17947       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17948       *categ = cp_parser_identifier (parser);
17949       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17950     }
17951 }
17952
17953 /* Parse an Objective-C class interface.  */
17954
17955 static void
17956 cp_parser_objc_class_interface (cp_parser* parser)
17957 {
17958   tree name, super, categ, protos;
17959
17960   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17961   name = cp_parser_identifier (parser);
17962   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17963   protos = cp_parser_objc_protocol_refs_opt (parser);
17964
17965   /* We have either a class or a category on our hands.  */
17966   if (categ)
17967     objc_start_category_interface (name, categ, protos);
17968   else
17969     {
17970       objc_start_class_interface (name, super, protos);
17971       /* Handle instance variable declarations, if any.  */
17972       cp_parser_objc_class_ivars (parser);
17973       objc_continue_interface ();
17974     }
17975
17976   cp_parser_objc_method_prototype_list (parser);
17977 }
17978
17979 /* Parse an Objective-C class implementation.  */
17980
17981 static void
17982 cp_parser_objc_class_implementation (cp_parser* parser)
17983 {
17984   tree name, super, categ;
17985
17986   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17987   name = cp_parser_identifier (parser);
17988   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17989
17990   /* We have either a class or a category on our hands.  */
17991   if (categ)
17992     objc_start_category_implementation (name, categ);
17993   else
17994     {
17995       objc_start_class_implementation (name, super);
17996       /* Handle instance variable declarations, if any.  */
17997       cp_parser_objc_class_ivars (parser);
17998       objc_continue_implementation ();
17999     }
18000
18001   cp_parser_objc_method_definition_list (parser);
18002 }
18003
18004 /* Consume the @end token and finish off the implementation.  */
18005
18006 static void
18007 cp_parser_objc_end_implementation (cp_parser* parser)
18008 {
18009   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18010   objc_finish_implementation ();
18011 }
18012
18013 /* Parse an Objective-C declaration.  */
18014
18015 static void
18016 cp_parser_objc_declaration (cp_parser* parser)
18017 {
18018   /* Try to figure out what kind of declaration is present.  */
18019   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18020
18021   switch (kwd->keyword)
18022     {
18023     case RID_AT_ALIAS:
18024       cp_parser_objc_alias_declaration (parser);
18025       break;
18026     case RID_AT_CLASS:
18027       cp_parser_objc_class_declaration (parser);
18028       break;
18029     case RID_AT_PROTOCOL:
18030       cp_parser_objc_protocol_declaration (parser);
18031       break;
18032     case RID_AT_INTERFACE:
18033       cp_parser_objc_class_interface (parser);
18034       break;
18035     case RID_AT_IMPLEMENTATION:
18036       cp_parser_objc_class_implementation (parser);
18037       break;
18038     case RID_AT_END:
18039       cp_parser_objc_end_implementation (parser);
18040       break;
18041     default:
18042       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18043       cp_parser_skip_to_end_of_block_or_statement (parser);
18044     }
18045 }
18046
18047 /* Parse an Objective-C try-catch-finally statement.
18048
18049    objc-try-catch-finally-stmt:
18050      @try compound-statement objc-catch-clause-seq [opt]
18051        objc-finally-clause [opt]
18052
18053    objc-catch-clause-seq:
18054      objc-catch-clause objc-catch-clause-seq [opt]
18055
18056    objc-catch-clause:
18057      @catch ( exception-declaration ) compound-statement
18058
18059    objc-finally-clause
18060      @finally compound-statement
18061
18062    Returns NULL_TREE.  */
18063
18064 static tree
18065 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18066   location_t location;
18067   tree stmt;
18068
18069   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18070   location = cp_lexer_peek_token (parser->lexer)->location;
18071   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18072      node, lest it get absorbed into the surrounding block.  */
18073   stmt = push_stmt_list ();
18074   cp_parser_compound_statement (parser, NULL, false);
18075   objc_begin_try_stmt (location, pop_stmt_list (stmt));
18076
18077   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18078     {
18079       cp_parameter_declarator *parmdecl;
18080       tree parm;
18081
18082       cp_lexer_consume_token (parser->lexer);
18083       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18084       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18085       parm = grokdeclarator (parmdecl->declarator,
18086                              &parmdecl->decl_specifiers,
18087                              PARM, /*initialized=*/0,
18088                              /*attrlist=*/NULL);
18089       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18090       objc_begin_catch_clause (parm);
18091       cp_parser_compound_statement (parser, NULL, false);
18092       objc_finish_catch_clause ();
18093     }
18094
18095   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18096     {
18097       cp_lexer_consume_token (parser->lexer);
18098       location = cp_lexer_peek_token (parser->lexer)->location;
18099       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18100          node, lest it get absorbed into the surrounding block.  */
18101       stmt = push_stmt_list ();
18102       cp_parser_compound_statement (parser, NULL, false);
18103       objc_build_finally_clause (location, pop_stmt_list (stmt));
18104     }
18105
18106   return objc_finish_try_stmt ();
18107 }
18108
18109 /* Parse an Objective-C synchronized statement.
18110
18111    objc-synchronized-stmt:
18112      @synchronized ( expression ) compound-statement
18113
18114    Returns NULL_TREE.  */
18115
18116 static tree
18117 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18118   location_t location;
18119   tree lock, stmt;
18120
18121   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18122
18123   location = cp_lexer_peek_token (parser->lexer)->location;
18124   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18125   lock = cp_parser_expression (parser, false);
18126   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18127
18128   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18129      node, lest it get absorbed into the surrounding block.  */
18130   stmt = push_stmt_list ();
18131   cp_parser_compound_statement (parser, NULL, false);
18132
18133   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18134 }
18135
18136 /* Parse an Objective-C throw statement.
18137
18138    objc-throw-stmt:
18139      @throw assignment-expression [opt] ;
18140
18141    Returns a constructed '@throw' statement.  */
18142
18143 static tree
18144 cp_parser_objc_throw_statement (cp_parser *parser) {
18145   tree expr = NULL_TREE;
18146
18147   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18148
18149   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18150     expr = cp_parser_assignment_expression (parser, false);
18151
18152   cp_parser_consume_semicolon_at_end_of_statement (parser);
18153
18154   return objc_build_throw_stmt (expr);
18155 }
18156
18157 /* Parse an Objective-C statement.  */
18158
18159 static tree
18160 cp_parser_objc_statement (cp_parser * parser) {
18161   /* Try to figure out what kind of declaration is present.  */
18162   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18163
18164   switch (kwd->keyword)
18165     {
18166     case RID_AT_TRY:
18167       return cp_parser_objc_try_catch_finally_statement (parser);
18168     case RID_AT_SYNCHRONIZED:
18169       return cp_parser_objc_synchronized_statement (parser);
18170     case RID_AT_THROW:
18171       return cp_parser_objc_throw_statement (parser);
18172     default:
18173       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18174       cp_parser_skip_to_end_of_block_or_statement (parser);
18175     }
18176
18177   return error_mark_node;
18178 }
18179 \f
18180 /* OpenMP 2.5 parsing routines.  */
18181
18182 /* Returns name of the next clause.
18183    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18184    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
18185    returned and the token is consumed.  */
18186
18187 static pragma_omp_clause
18188 cp_parser_omp_clause_name (cp_parser *parser)
18189 {
18190   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18191
18192   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18193     result = PRAGMA_OMP_CLAUSE_IF;
18194   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18195     result = PRAGMA_OMP_CLAUSE_DEFAULT;
18196   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18197     result = PRAGMA_OMP_CLAUSE_PRIVATE;
18198   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18199     {
18200       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18201       const char *p = IDENTIFIER_POINTER (id);
18202
18203       switch (p[0])
18204         {
18205         case 'c':
18206           if (!strcmp ("copyin", p))
18207             result = PRAGMA_OMP_CLAUSE_COPYIN;
18208           else if (!strcmp ("copyprivate", p))
18209             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18210           break;
18211         case 'f':
18212           if (!strcmp ("firstprivate", p))
18213             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18214           break;
18215         case 'l':
18216           if (!strcmp ("lastprivate", p))
18217             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18218           break;
18219         case 'n':
18220           if (!strcmp ("nowait", p))
18221             result = PRAGMA_OMP_CLAUSE_NOWAIT;
18222           else if (!strcmp ("num_threads", p))
18223             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18224           break;
18225         case 'o':
18226           if (!strcmp ("ordered", p))
18227             result = PRAGMA_OMP_CLAUSE_ORDERED;
18228           break;
18229         case 'r':
18230           if (!strcmp ("reduction", p))
18231             result = PRAGMA_OMP_CLAUSE_REDUCTION;
18232           break;
18233         case 's':
18234           if (!strcmp ("schedule", p))
18235             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18236           else if (!strcmp ("shared", p))
18237             result = PRAGMA_OMP_CLAUSE_SHARED;
18238           break;
18239         }
18240     }
18241
18242   if (result != PRAGMA_OMP_CLAUSE_NONE)
18243     cp_lexer_consume_token (parser->lexer);
18244
18245   return result;
18246 }
18247
18248 /* Validate that a clause of the given type does not already exist.  */
18249
18250 static void
18251 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18252 {
18253   tree c;
18254
18255   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18256     if (OMP_CLAUSE_CODE (c) == code)
18257       {
18258         error ("too many %qs clauses", name);
18259         break;
18260       }
18261 }
18262
18263 /* OpenMP 2.5:
18264    variable-list:
18265      identifier
18266      variable-list , identifier
18267
18268    In addition, we match a closing parenthesis.  An opening parenthesis
18269    will have been consumed by the caller.
18270
18271    If KIND is nonzero, create the appropriate node and install the decl
18272    in OMP_CLAUSE_DECL and add the node to the head of the list.
18273
18274    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18275    return the list created.  */
18276
18277 static tree
18278 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18279                                 tree list)
18280 {
18281   while (1)
18282     {
18283       tree name, decl;
18284
18285       name = cp_parser_id_expression (parser, /*template_p=*/false,
18286                                       /*check_dependency_p=*/true,
18287                                       /*template_p=*/NULL,
18288                                       /*declarator_p=*/false,
18289                                       /*optional_p=*/false);
18290       if (name == error_mark_node)
18291         goto skip_comma;
18292
18293       decl = cp_parser_lookup_name_simple (parser, name);
18294       if (decl == error_mark_node)
18295         cp_parser_name_lookup_error (parser, name, decl, NULL);
18296       else if (kind != 0)
18297         {
18298           tree u = build_omp_clause (kind);
18299           OMP_CLAUSE_DECL (u) = decl;
18300           OMP_CLAUSE_CHAIN (u) = list;
18301           list = u;
18302         }
18303       else
18304         list = tree_cons (decl, NULL_TREE, list);
18305
18306     get_comma:
18307       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18308         break;
18309       cp_lexer_consume_token (parser->lexer);
18310     }
18311
18312   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18313     {
18314       int ending;
18315
18316       /* Try to resync to an unnested comma.  Copied from
18317          cp_parser_parenthesized_expression_list.  */
18318     skip_comma:
18319       ending = cp_parser_skip_to_closing_parenthesis (parser,
18320                                                       /*recovering=*/true,
18321                                                       /*or_comma=*/true,
18322                                                       /*consume_paren=*/true);
18323       if (ending < 0)
18324         goto get_comma;
18325     }
18326
18327   return list;
18328 }
18329
18330 /* Similarly, but expect leading and trailing parenthesis.  This is a very
18331    common case for omp clauses.  */
18332
18333 static tree
18334 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18335 {
18336   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18337     return cp_parser_omp_var_list_no_open (parser, kind, list);
18338   return list;
18339 }
18340
18341 /* OpenMP 2.5:
18342    default ( shared | none ) */
18343
18344 static tree
18345 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18346 {
18347   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18348   tree c;
18349
18350   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18351     return list;
18352   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18353     {
18354       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18355       const char *p = IDENTIFIER_POINTER (id);
18356
18357       switch (p[0])
18358         {
18359         case 'n':
18360           if (strcmp ("none", p) != 0)
18361             goto invalid_kind;
18362           kind = OMP_CLAUSE_DEFAULT_NONE;
18363           break;
18364
18365         case 's':
18366           if (strcmp ("shared", p) != 0)
18367             goto invalid_kind;
18368           kind = OMP_CLAUSE_DEFAULT_SHARED;
18369           break;
18370
18371         default:
18372           goto invalid_kind;
18373         }
18374
18375       cp_lexer_consume_token (parser->lexer);
18376     }
18377   else
18378     {
18379     invalid_kind:
18380       cp_parser_error (parser, "expected %<none%> or %<shared%>");
18381     }
18382
18383   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18384     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18385                                            /*or_comma=*/false,
18386                                            /*consume_paren=*/true);
18387
18388   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18389     return list;
18390
18391   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18392   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18393   OMP_CLAUSE_CHAIN (c) = list;
18394   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18395
18396   return c;
18397 }
18398
18399 /* OpenMP 2.5:
18400    if ( expression ) */
18401
18402 static tree
18403 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18404 {
18405   tree t, c;
18406
18407   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18408     return list;
18409
18410   t = cp_parser_condition (parser);
18411
18412   if (t == error_mark_node
18413       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18414     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18415                                            /*or_comma=*/false,
18416                                            /*consume_paren=*/true);
18417
18418   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18419
18420   c = build_omp_clause (OMP_CLAUSE_IF);
18421   OMP_CLAUSE_IF_EXPR (c) = t;
18422   OMP_CLAUSE_CHAIN (c) = list;
18423
18424   return c;
18425 }
18426
18427 /* OpenMP 2.5:
18428    nowait */
18429
18430 static tree
18431 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18432 {
18433   tree c;
18434
18435   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18436
18437   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18438   OMP_CLAUSE_CHAIN (c) = list;
18439   return c;
18440 }
18441
18442 /* OpenMP 2.5:
18443    num_threads ( expression ) */
18444
18445 static tree
18446 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18447 {
18448   tree t, c;
18449
18450   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18451     return list;
18452
18453   t = cp_parser_expression (parser, false);
18454
18455   if (t == error_mark_node
18456       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18457     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18458                                            /*or_comma=*/false,
18459                                            /*consume_paren=*/true);
18460
18461   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18462
18463   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18464   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18465   OMP_CLAUSE_CHAIN (c) = list;
18466
18467   return c;
18468 }
18469
18470 /* OpenMP 2.5:
18471    ordered */
18472
18473 static tree
18474 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18475 {
18476   tree c;
18477
18478   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18479
18480   c = build_omp_clause (OMP_CLAUSE_ORDERED);
18481   OMP_CLAUSE_CHAIN (c) = list;
18482   return c;
18483 }
18484
18485 /* OpenMP 2.5:
18486    reduction ( reduction-operator : variable-list )
18487
18488    reduction-operator:
18489      One of: + * - & ^ | && || */
18490
18491 static tree
18492 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18493 {
18494   enum tree_code code;
18495   tree nlist, c;
18496
18497   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18498     return list;
18499
18500   switch (cp_lexer_peek_token (parser->lexer)->type)
18501     {
18502     case CPP_PLUS:
18503       code = PLUS_EXPR;
18504       break;
18505     case CPP_MULT:
18506       code = MULT_EXPR;
18507       break;
18508     case CPP_MINUS:
18509       code = MINUS_EXPR;
18510       break;
18511     case CPP_AND:
18512       code = BIT_AND_EXPR;
18513       break;
18514     case CPP_XOR:
18515       code = BIT_XOR_EXPR;
18516       break;
18517     case CPP_OR:
18518       code = BIT_IOR_EXPR;
18519       break;
18520     case CPP_AND_AND:
18521       code = TRUTH_ANDIF_EXPR;
18522       break;
18523     case CPP_OR_OR:
18524       code = TRUTH_ORIF_EXPR;
18525       break;
18526     default:
18527       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18528     resync_fail:
18529       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18530                                              /*or_comma=*/false,
18531                                              /*consume_paren=*/true);
18532       return list;
18533     }
18534   cp_lexer_consume_token (parser->lexer);
18535
18536   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18537     goto resync_fail;
18538
18539   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18540   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18541     OMP_CLAUSE_REDUCTION_CODE (c) = code;
18542
18543   return nlist;
18544 }
18545
18546 /* OpenMP 2.5:
18547    schedule ( schedule-kind )
18548    schedule ( schedule-kind , expression )
18549
18550    schedule-kind:
18551      static | dynamic | guided | runtime  */
18552
18553 static tree
18554 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18555 {
18556   tree c, t;
18557
18558   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18559     return list;
18560
18561   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18562
18563   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18564     {
18565       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18566       const char *p = IDENTIFIER_POINTER (id);
18567
18568       switch (p[0])
18569         {
18570         case 'd':
18571           if (strcmp ("dynamic", p) != 0)
18572             goto invalid_kind;
18573           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18574           break;
18575
18576         case 'g':
18577           if (strcmp ("guided", p) != 0)
18578             goto invalid_kind;
18579           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18580           break;
18581
18582         case 'r':
18583           if (strcmp ("runtime", p) != 0)
18584             goto invalid_kind;
18585           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18586           break;
18587
18588         default:
18589           goto invalid_kind;
18590         }
18591     }
18592   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18593     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18594   else
18595     goto invalid_kind;
18596   cp_lexer_consume_token (parser->lexer);
18597
18598   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18599     {
18600       cp_lexer_consume_token (parser->lexer);
18601
18602       t = cp_parser_assignment_expression (parser, false);
18603
18604       if (t == error_mark_node)
18605         goto resync_fail;
18606       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18607         error ("schedule %<runtime%> does not take "
18608                "a %<chunk_size%> parameter");
18609       else
18610         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18611
18612       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18613         goto resync_fail;
18614     }
18615   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18616     goto resync_fail;
18617
18618   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18619   OMP_CLAUSE_CHAIN (c) = list;
18620   return c;
18621
18622  invalid_kind:
18623   cp_parser_error (parser, "invalid schedule kind");
18624  resync_fail:
18625   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18626                                          /*or_comma=*/false,
18627                                          /*consume_paren=*/true);
18628   return list;
18629 }
18630
18631 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
18632    is a bitmask in MASK.  Return the list of clauses found; the result
18633    of clause default goes in *pdefault.  */
18634
18635 static tree
18636 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18637                            const char *where, cp_token *pragma_tok)
18638 {
18639   tree clauses = NULL;
18640
18641   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18642     {
18643       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18644       const char *c_name;
18645       tree prev = clauses;
18646
18647       switch (c_kind)
18648         {
18649         case PRAGMA_OMP_CLAUSE_COPYIN:
18650           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18651           c_name = "copyin";
18652           break;
18653         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18654           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18655                                             clauses);
18656           c_name = "copyprivate";
18657           break;
18658         case PRAGMA_OMP_CLAUSE_DEFAULT:
18659           clauses = cp_parser_omp_clause_default (parser, clauses);
18660           c_name = "default";
18661           break;
18662         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18663           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18664                                             clauses);
18665           c_name = "firstprivate";
18666           break;
18667         case PRAGMA_OMP_CLAUSE_IF:
18668           clauses = cp_parser_omp_clause_if (parser, clauses);
18669           c_name = "if";
18670           break;
18671         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18672           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18673                                             clauses);
18674           c_name = "lastprivate";
18675           break;
18676         case PRAGMA_OMP_CLAUSE_NOWAIT:
18677           clauses = cp_parser_omp_clause_nowait (parser, clauses);
18678           c_name = "nowait";
18679           break;
18680         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18681           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18682           c_name = "num_threads";
18683           break;
18684         case PRAGMA_OMP_CLAUSE_ORDERED:
18685           clauses = cp_parser_omp_clause_ordered (parser, clauses);
18686           c_name = "ordered";
18687           break;
18688         case PRAGMA_OMP_CLAUSE_PRIVATE:
18689           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18690                                             clauses);
18691           c_name = "private";
18692           break;
18693         case PRAGMA_OMP_CLAUSE_REDUCTION:
18694           clauses = cp_parser_omp_clause_reduction (parser, clauses);
18695           c_name = "reduction";
18696           break;
18697         case PRAGMA_OMP_CLAUSE_SCHEDULE:
18698           clauses = cp_parser_omp_clause_schedule (parser, clauses);
18699           c_name = "schedule";
18700           break;
18701         case PRAGMA_OMP_CLAUSE_SHARED:
18702           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18703                                             clauses);
18704           c_name = "shared";
18705           break;
18706         default:
18707           cp_parser_error (parser, "expected %<#pragma omp%> clause");
18708           goto saw_error;
18709         }
18710
18711       if (((mask >> c_kind) & 1) == 0)
18712         {
18713           /* Remove the invalid clause(s) from the list to avoid
18714              confusing the rest of the compiler.  */
18715           clauses = prev;
18716           error ("%qs is not valid for %qs", c_name, where);
18717         }
18718     }
18719  saw_error:
18720   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18721   return finish_omp_clauses (clauses);
18722 }
18723
18724 /* OpenMP 2.5:
18725    structured-block:
18726      statement
18727
18728    In practice, we're also interested in adding the statement to an
18729    outer node.  So it is convenient if we work around the fact that
18730    cp_parser_statement calls add_stmt.  */
18731
18732 static unsigned
18733 cp_parser_begin_omp_structured_block (cp_parser *parser)
18734 {
18735   unsigned save = parser->in_statement;
18736
18737   /* Only move the values to IN_OMP_BLOCK if they weren't false.
18738      This preserves the "not within loop or switch" style error messages
18739      for nonsense cases like
18740         void foo() {
18741         #pragma omp single
18742           break;
18743         }
18744   */
18745   if (parser->in_statement)
18746     parser->in_statement = IN_OMP_BLOCK;
18747
18748   return save;
18749 }
18750
18751 static void
18752 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18753 {
18754   parser->in_statement = save;
18755 }
18756
18757 static tree
18758 cp_parser_omp_structured_block (cp_parser *parser)
18759 {
18760   tree stmt = begin_omp_structured_block ();
18761   unsigned int save = cp_parser_begin_omp_structured_block (parser);
18762
18763   cp_parser_statement (parser, NULL_TREE, false, NULL);
18764
18765   cp_parser_end_omp_structured_block (parser, save);
18766   return finish_omp_structured_block (stmt);
18767 }
18768
18769 /* OpenMP 2.5:
18770    # pragma omp atomic new-line
18771      expression-stmt
18772
18773    expression-stmt:
18774      x binop= expr | x++ | ++x | x-- | --x
18775    binop:
18776      +, *, -, /, &, ^, |, <<, >>
18777
18778   where x is an lvalue expression with scalar type.  */
18779
18780 static void
18781 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18782 {
18783   tree lhs, rhs;
18784   enum tree_code code;
18785
18786   cp_parser_require_pragma_eol (parser, pragma_tok);
18787
18788   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18789                                     /*cast_p=*/false);
18790   switch (TREE_CODE (lhs))
18791     {
18792     case ERROR_MARK:
18793       goto saw_error;
18794
18795     case PREINCREMENT_EXPR:
18796     case POSTINCREMENT_EXPR:
18797       lhs = TREE_OPERAND (lhs, 0);
18798       code = PLUS_EXPR;
18799       rhs = integer_one_node;
18800       break;
18801
18802     case PREDECREMENT_EXPR:
18803     case POSTDECREMENT_EXPR:
18804       lhs = TREE_OPERAND (lhs, 0);
18805       code = MINUS_EXPR;
18806       rhs = integer_one_node;
18807       break;
18808
18809     default:
18810       switch (cp_lexer_peek_token (parser->lexer)->type)
18811         {
18812         case CPP_MULT_EQ:
18813           code = MULT_EXPR;
18814           break;
18815         case CPP_DIV_EQ:
18816           code = TRUNC_DIV_EXPR;
18817           break;
18818         case CPP_PLUS_EQ:
18819           code = PLUS_EXPR;
18820           break;
18821         case CPP_MINUS_EQ:
18822           code = MINUS_EXPR;
18823           break;
18824         case CPP_LSHIFT_EQ:
18825           code = LSHIFT_EXPR;
18826           break;
18827         case CPP_RSHIFT_EQ:
18828           code = RSHIFT_EXPR;
18829           break;
18830         case CPP_AND_EQ:
18831           code = BIT_AND_EXPR;
18832           break;
18833         case CPP_OR_EQ:
18834           code = BIT_IOR_EXPR;
18835           break;
18836         case CPP_XOR_EQ:
18837           code = BIT_XOR_EXPR;
18838           break;
18839         default:
18840           cp_parser_error (parser,
18841                            "invalid operator for %<#pragma omp atomic%>");
18842           goto saw_error;
18843         }
18844       cp_lexer_consume_token (parser->lexer);
18845
18846       rhs = cp_parser_expression (parser, false);
18847       if (rhs == error_mark_node)
18848         goto saw_error;
18849       break;
18850     }
18851   finish_omp_atomic (code, lhs, rhs);
18852   cp_parser_consume_semicolon_at_end_of_statement (parser);
18853   return;
18854
18855  saw_error:
18856   cp_parser_skip_to_end_of_block_or_statement (parser);
18857 }
18858
18859
18860 /* OpenMP 2.5:
18861    # pragma omp barrier new-line  */
18862
18863 static void
18864 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18865 {
18866   cp_parser_require_pragma_eol (parser, pragma_tok);
18867   finish_omp_barrier ();
18868 }
18869
18870 /* OpenMP 2.5:
18871    # pragma omp critical [(name)] new-line
18872      structured-block  */
18873
18874 static tree
18875 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18876 {
18877   tree stmt, name = NULL;
18878
18879   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18880     {
18881       cp_lexer_consume_token (parser->lexer);
18882
18883       name = cp_parser_identifier (parser);
18884
18885       if (name == error_mark_node
18886           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18887         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18888                                                /*or_comma=*/false,
18889                                                /*consume_paren=*/true);
18890       if (name == error_mark_node)
18891         name = NULL;
18892     }
18893   cp_parser_require_pragma_eol (parser, pragma_tok);
18894
18895   stmt = cp_parser_omp_structured_block (parser);
18896   return c_finish_omp_critical (stmt, name);
18897 }
18898
18899 /* OpenMP 2.5:
18900    # pragma omp flush flush-vars[opt] new-line
18901
18902    flush-vars:
18903      ( variable-list ) */
18904
18905 static void
18906 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18907 {
18908   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18909     (void) cp_parser_omp_var_list (parser, 0, NULL);
18910   cp_parser_require_pragma_eol (parser, pragma_tok);
18911
18912   finish_omp_flush ();
18913 }
18914
18915 /* Parse the restricted form of the for statment allowed by OpenMP.  */
18916
18917 static tree
18918 cp_parser_omp_for_loop (cp_parser *parser)
18919 {
18920   tree init, cond, incr, body, decl, pre_body;
18921   location_t loc;
18922
18923   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18924     {
18925       cp_parser_error (parser, "for statement expected");
18926       return NULL;
18927     }
18928   loc = cp_lexer_consume_token (parser->lexer)->location;
18929   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18930     return NULL;
18931
18932   init = decl = NULL;
18933   pre_body = push_stmt_list ();
18934   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18935     {
18936       cp_decl_specifier_seq type_specifiers;
18937
18938       /* First, try to parse as an initialized declaration.  See
18939          cp_parser_condition, from whence the bulk of this is copied.  */
18940
18941       cp_parser_parse_tentatively (parser);
18942       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18943                                     &type_specifiers);
18944       if (!cp_parser_error_occurred (parser))
18945         {
18946           tree asm_specification, attributes;
18947           cp_declarator *declarator;
18948
18949           declarator = cp_parser_declarator (parser,
18950                                              CP_PARSER_DECLARATOR_NAMED,
18951                                              /*ctor_dtor_or_conv_p=*/NULL,
18952                                              /*parenthesized_p=*/NULL,
18953                                              /*member_p=*/false);
18954           attributes = cp_parser_attributes_opt (parser);
18955           asm_specification = cp_parser_asm_specification_opt (parser);
18956
18957           cp_parser_require (parser, CPP_EQ, "`='");
18958           if (cp_parser_parse_definitely (parser))
18959             {
18960               tree pushed_scope;
18961
18962               decl = start_decl (declarator, &type_specifiers,
18963                                  /*initialized_p=*/false, attributes,
18964                                  /*prefix_attributes=*/NULL_TREE,
18965                                  &pushed_scope);
18966
18967               init = cp_parser_assignment_expression (parser, false);
18968
18969               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18970                               asm_specification, LOOKUP_ONLYCONVERTING);
18971
18972               if (pushed_scope)
18973                 pop_scope (pushed_scope);
18974             }
18975         }
18976       else
18977         cp_parser_abort_tentative_parse (parser);
18978
18979       /* If parsing as an initialized declaration failed, try again as
18980          a simple expression.  */
18981       if (decl == NULL)
18982         init = cp_parser_expression (parser, false);
18983     }
18984   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18985   pre_body = pop_stmt_list (pre_body);
18986
18987   cond = NULL;
18988   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18989     cond = cp_parser_condition (parser);
18990   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18991
18992   incr = NULL;
18993   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18994     incr = cp_parser_expression (parser, false);
18995
18996   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18997     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18998                                            /*or_comma=*/false,
18999                                            /*consume_paren=*/true);
19000
19001   /* Note that we saved the original contents of this flag when we entered
19002      the structured block, and so we don't need to re-save it here.  */
19003   parser->in_statement = IN_OMP_FOR;
19004
19005   /* Note that the grammar doesn't call for a structured block here,
19006      though the loop as a whole is a structured block.  */
19007   body = push_stmt_list ();
19008   cp_parser_statement (parser, NULL_TREE, false, NULL);
19009   body = pop_stmt_list (body);
19010
19011   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19012 }
19013
19014 /* OpenMP 2.5:
19015    #pragma omp for for-clause[optseq] new-line
19016      for-loop  */
19017
19018 #define OMP_FOR_CLAUSE_MASK                             \
19019         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19020         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19021         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19022         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19023         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
19024         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
19025         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19026
19027 static tree
19028 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19029 {
19030   tree clauses, sb, ret;
19031   unsigned int save;
19032
19033   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19034                                        "#pragma omp for", pragma_tok);
19035
19036   sb = begin_omp_structured_block ();
19037   save = cp_parser_begin_omp_structured_block (parser);
19038
19039   ret = cp_parser_omp_for_loop (parser);
19040   if (ret)
19041     OMP_FOR_CLAUSES (ret) = clauses;
19042
19043   cp_parser_end_omp_structured_block (parser, save);
19044   add_stmt (finish_omp_structured_block (sb));
19045
19046   return ret;
19047 }
19048
19049 /* OpenMP 2.5:
19050    # pragma omp master new-line
19051      structured-block  */
19052
19053 static tree
19054 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19055 {
19056   cp_parser_require_pragma_eol (parser, pragma_tok);
19057   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19058 }
19059
19060 /* OpenMP 2.5:
19061    # pragma omp ordered new-line
19062      structured-block  */
19063
19064 static tree
19065 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19066 {
19067   cp_parser_require_pragma_eol (parser, pragma_tok);
19068   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19069 }
19070
19071 /* OpenMP 2.5:
19072
19073    section-scope:
19074      { section-sequence }
19075
19076    section-sequence:
19077      section-directive[opt] structured-block
19078      section-sequence section-directive structured-block  */
19079
19080 static tree
19081 cp_parser_omp_sections_scope (cp_parser *parser)
19082 {
19083   tree stmt, substmt;
19084   bool error_suppress = false;
19085   cp_token *tok;
19086
19087   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19088     return NULL_TREE;
19089
19090   stmt = push_stmt_list ();
19091
19092   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19093     {
19094       unsigned save;
19095
19096       substmt = begin_omp_structured_block ();
19097       save = cp_parser_begin_omp_structured_block (parser);
19098
19099       while (1)
19100         {
19101           cp_parser_statement (parser, NULL_TREE, false, NULL);
19102
19103           tok = cp_lexer_peek_token (parser->lexer);
19104           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19105             break;
19106           if (tok->type == CPP_CLOSE_BRACE)
19107             break;
19108           if (tok->type == CPP_EOF)
19109             break;
19110         }
19111
19112       cp_parser_end_omp_structured_block (parser, save);
19113       substmt = finish_omp_structured_block (substmt);
19114       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19115       add_stmt (substmt);
19116     }
19117
19118   while (1)
19119     {
19120       tok = cp_lexer_peek_token (parser->lexer);
19121       if (tok->type == CPP_CLOSE_BRACE)
19122         break;
19123       if (tok->type == CPP_EOF)
19124         break;
19125
19126       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19127         {
19128           cp_lexer_consume_token (parser->lexer);
19129           cp_parser_require_pragma_eol (parser, tok);
19130           error_suppress = false;
19131         }
19132       else if (!error_suppress)
19133         {
19134           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19135           error_suppress = true;
19136         }
19137
19138       substmt = cp_parser_omp_structured_block (parser);
19139       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19140       add_stmt (substmt);
19141     }
19142   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19143
19144   substmt = pop_stmt_list (stmt);
19145
19146   stmt = make_node (OMP_SECTIONS);
19147   TREE_TYPE (stmt) = void_type_node;
19148   OMP_SECTIONS_BODY (stmt) = substmt;
19149
19150   add_stmt (stmt);
19151   return stmt;
19152 }
19153
19154 /* OpenMP 2.5:
19155    # pragma omp sections sections-clause[optseq] newline
19156      sections-scope  */
19157
19158 #define OMP_SECTIONS_CLAUSE_MASK                        \
19159         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19160         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19161         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19162         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19163         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19164
19165 static tree
19166 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19167 {
19168   tree clauses, ret;
19169
19170   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19171                                        "#pragma omp sections", pragma_tok);
19172
19173   ret = cp_parser_omp_sections_scope (parser);
19174   if (ret)
19175     OMP_SECTIONS_CLAUSES (ret) = clauses;
19176
19177   return ret;
19178 }
19179
19180 /* OpenMP 2.5:
19181    # pragma parallel parallel-clause new-line
19182    # pragma parallel for parallel-for-clause new-line
19183    # pragma parallel sections parallel-sections-clause new-line  */
19184
19185 #define OMP_PARALLEL_CLAUSE_MASK                        \
19186         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
19187         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19188         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19189         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
19190         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
19191         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
19192         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19193         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19194
19195 static tree
19196 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19197 {
19198   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19199   const char *p_name = "#pragma omp parallel";
19200   tree stmt, clauses, par_clause, ws_clause, block;
19201   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19202   unsigned int save;
19203
19204   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19205     {
19206       cp_lexer_consume_token (parser->lexer);
19207       p_kind = PRAGMA_OMP_PARALLEL_FOR;
19208       p_name = "#pragma omp parallel for";
19209       mask |= OMP_FOR_CLAUSE_MASK;
19210       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19211     }
19212   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19213     {
19214       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19215       const char *p = IDENTIFIER_POINTER (id);
19216       if (strcmp (p, "sections") == 0)
19217         {
19218           cp_lexer_consume_token (parser->lexer);
19219           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19220           p_name = "#pragma omp parallel sections";
19221           mask |= OMP_SECTIONS_CLAUSE_MASK;
19222           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19223         }
19224     }
19225
19226   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19227   block = begin_omp_parallel ();
19228   save = cp_parser_begin_omp_structured_block (parser);
19229
19230   switch (p_kind)
19231     {
19232     case PRAGMA_OMP_PARALLEL:
19233       cp_parser_already_scoped_statement (parser);
19234       par_clause = clauses;
19235       break;
19236
19237     case PRAGMA_OMP_PARALLEL_FOR:
19238       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19239       stmt = cp_parser_omp_for_loop (parser);
19240       if (stmt)
19241         OMP_FOR_CLAUSES (stmt) = ws_clause;
19242       break;
19243
19244     case PRAGMA_OMP_PARALLEL_SECTIONS:
19245       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19246       stmt = cp_parser_omp_sections_scope (parser);
19247       if (stmt)
19248         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19249       break;
19250
19251     default:
19252       gcc_unreachable ();
19253     }
19254
19255   cp_parser_end_omp_structured_block (parser, save);
19256   stmt = finish_omp_parallel (par_clause, block);
19257   if (p_kind != PRAGMA_OMP_PARALLEL)
19258     OMP_PARALLEL_COMBINED (stmt) = 1;
19259   return stmt;
19260 }
19261
19262 /* OpenMP 2.5:
19263    # pragma omp single single-clause[optseq] new-line
19264      structured-block  */
19265
19266 #define OMP_SINGLE_CLAUSE_MASK                          \
19267         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19268         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19269         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
19270         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19271
19272 static tree
19273 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19274 {
19275   tree stmt = make_node (OMP_SINGLE);
19276   TREE_TYPE (stmt) = void_type_node;
19277
19278   OMP_SINGLE_CLAUSES (stmt)
19279     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19280                                  "#pragma omp single", pragma_tok);
19281   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19282
19283   return add_stmt (stmt);
19284 }
19285
19286 /* OpenMP 2.5:
19287    # pragma omp threadprivate (variable-list) */
19288
19289 static void
19290 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19291 {
19292   tree vars;
19293
19294   vars = cp_parser_omp_var_list (parser, 0, NULL);
19295   cp_parser_require_pragma_eol (parser, pragma_tok);
19296
19297   if (!targetm.have_tls)
19298     sorry ("threadprivate variables not supported in this target");
19299
19300   finish_omp_threadprivate (vars);
19301 }
19302
19303 /* Main entry point to OpenMP statement pragmas.  */
19304
19305 static void
19306 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19307 {
19308   tree stmt;
19309
19310   switch (pragma_tok->pragma_kind)
19311     {
19312     case PRAGMA_OMP_ATOMIC:
19313       cp_parser_omp_atomic (parser, pragma_tok);
19314       return;
19315     case PRAGMA_OMP_CRITICAL:
19316       stmt = cp_parser_omp_critical (parser, pragma_tok);
19317       break;
19318     case PRAGMA_OMP_FOR:
19319       stmt = cp_parser_omp_for (parser, pragma_tok);
19320       break;
19321     case PRAGMA_OMP_MASTER:
19322       stmt = cp_parser_omp_master (parser, pragma_tok);
19323       break;
19324     case PRAGMA_OMP_ORDERED:
19325       stmt = cp_parser_omp_ordered (parser, pragma_tok);
19326       break;
19327     case PRAGMA_OMP_PARALLEL:
19328       stmt = cp_parser_omp_parallel (parser, pragma_tok);
19329       break;
19330     case PRAGMA_OMP_SECTIONS:
19331       stmt = cp_parser_omp_sections (parser, pragma_tok);
19332       break;
19333     case PRAGMA_OMP_SINGLE:
19334       stmt = cp_parser_omp_single (parser, pragma_tok);
19335       break;
19336     default:
19337       gcc_unreachable ();
19338     }
19339
19340   if (stmt)
19341     SET_EXPR_LOCATION (stmt, pragma_tok->location);
19342 }
19343 \f
19344 /* The parser.  */
19345
19346 static GTY (()) cp_parser *the_parser;
19347
19348 \f
19349 /* Special handling for the first token or line in the file.  The first
19350    thing in the file might be #pragma GCC pch_preprocess, which loads a
19351    PCH file, which is a GC collection point.  So we need to handle this
19352    first pragma without benefit of an existing lexer structure.
19353
19354    Always returns one token to the caller in *FIRST_TOKEN.  This is
19355    either the true first token of the file, or the first token after
19356    the initial pragma.  */
19357
19358 static void
19359 cp_parser_initial_pragma (cp_token *first_token)
19360 {
19361   tree name = NULL;
19362
19363   cp_lexer_get_preprocessor_token (NULL, first_token);
19364   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19365     return;
19366
19367   cp_lexer_get_preprocessor_token (NULL, first_token);
19368   if (first_token->type == CPP_STRING)
19369     {
19370       name = first_token->u.value;
19371
19372       cp_lexer_get_preprocessor_token (NULL, first_token);
19373       if (first_token->type != CPP_PRAGMA_EOL)
19374         error ("junk at end of %<#pragma GCC pch_preprocess%>");
19375     }
19376   else
19377     error ("expected string literal");
19378
19379   /* Skip to the end of the pragma.  */
19380   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19381     cp_lexer_get_preprocessor_token (NULL, first_token);
19382
19383   /* Now actually load the PCH file.  */
19384   if (name)
19385     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19386
19387   /* Read one more token to return to our caller.  We have to do this
19388      after reading the PCH file in, since its pointers have to be
19389      live.  */
19390   cp_lexer_get_preprocessor_token (NULL, first_token);
19391 }
19392
19393 /* Normal parsing of a pragma token.  Here we can (and must) use the
19394    regular lexer.  */
19395
19396 static bool
19397 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19398 {
19399   cp_token *pragma_tok;
19400   unsigned int id;
19401
19402   pragma_tok = cp_lexer_consume_token (parser->lexer);
19403   gcc_assert (pragma_tok->type == CPP_PRAGMA);
19404   parser->lexer->in_pragma = true;
19405
19406   id = pragma_tok->pragma_kind;
19407   switch (id)
19408     {
19409     case PRAGMA_GCC_PCH_PREPROCESS:
19410       error ("%<#pragma GCC pch_preprocess%> must be first");
19411       break;
19412
19413     case PRAGMA_OMP_BARRIER:
19414       switch (context)
19415         {
19416         case pragma_compound:
19417           cp_parser_omp_barrier (parser, pragma_tok);
19418           return false;
19419         case pragma_stmt:
19420           error ("%<#pragma omp barrier%> may only be "
19421                  "used in compound statements");
19422           break;
19423         default:
19424           goto bad_stmt;
19425         }
19426       break;
19427
19428     case PRAGMA_OMP_FLUSH:
19429       switch (context)
19430         {
19431         case pragma_compound:
19432           cp_parser_omp_flush (parser, pragma_tok);
19433           return false;
19434         case pragma_stmt:
19435           error ("%<#pragma omp flush%> may only be "
19436                  "used in compound statements");
19437           break;
19438         default:
19439           goto bad_stmt;
19440         }
19441       break;
19442
19443     case PRAGMA_OMP_THREADPRIVATE:
19444       cp_parser_omp_threadprivate (parser, pragma_tok);
19445       return false;
19446
19447     case PRAGMA_OMP_ATOMIC:
19448     case PRAGMA_OMP_CRITICAL:
19449     case PRAGMA_OMP_FOR:
19450     case PRAGMA_OMP_MASTER:
19451     case PRAGMA_OMP_ORDERED:
19452     case PRAGMA_OMP_PARALLEL:
19453     case PRAGMA_OMP_SECTIONS:
19454     case PRAGMA_OMP_SINGLE:
19455       if (context == pragma_external)
19456         goto bad_stmt;
19457       cp_parser_omp_construct (parser, pragma_tok);
19458       return true;
19459
19460     case PRAGMA_OMP_SECTION:
19461       error ("%<#pragma omp section%> may only be used in "
19462              "%<#pragma omp sections%> construct");
19463       break;
19464
19465     default:
19466       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19467       c_invoke_pragma_handler (id);
19468       break;
19469
19470     bad_stmt:
19471       cp_parser_error (parser, "expected declaration specifiers");
19472       break;
19473     }
19474
19475   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19476   return false;
19477 }
19478
19479 /* The interface the pragma parsers have to the lexer.  */
19480
19481 enum cpp_ttype
19482 pragma_lex (tree *value)
19483 {
19484   cp_token *tok;
19485   enum cpp_ttype ret;
19486
19487   tok = cp_lexer_peek_token (the_parser->lexer);
19488
19489   ret = tok->type;
19490   *value = tok->u.value;
19491
19492   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19493     ret = CPP_EOF;
19494   else if (ret == CPP_STRING)
19495     *value = cp_parser_string_literal (the_parser, false, false);
19496   else
19497     {
19498       cp_lexer_consume_token (the_parser->lexer);
19499       if (ret == CPP_KEYWORD)
19500         ret = CPP_NAME;
19501     }
19502
19503   return ret;
19504 }
19505
19506 \f
19507 /* External interface.  */
19508
19509 /* Parse one entire translation unit.  */
19510
19511 void
19512 c_parse_file (void)
19513 {
19514   bool error_occurred;
19515   static bool already_called = false;
19516
19517   if (already_called)
19518     {
19519       sorry ("inter-module optimizations not implemented for C++");
19520       return;
19521     }
19522   already_called = true;
19523
19524   the_parser = cp_parser_new ();
19525   push_deferring_access_checks (flag_access_control
19526                                 ? dk_no_deferred : dk_no_check);
19527   error_occurred = cp_parser_translation_unit (the_parser);
19528   the_parser = NULL;
19529 }
19530
19531 #include "gt-cp-parser.h"