OSDN Git Service

* c-opts.c (c_common_parse_file): Unconditionally give a warning,
[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 C++ token.  */
49
50 typedef struct cp_token GTY (())
51 {
52   /* The kind of token.  */
53   ENUM_BITFIELD (cpp_ttype) type : 8;
54   /* If this token is a keyword, this value indicates which keyword.
55      Otherwise, this value is RID_MAX.  */
56   ENUM_BITFIELD (rid) keyword : 8;
57   /* Token flags.  */
58   unsigned char flags;
59   /* Identifier for the pragma.  */
60   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
61   /* True if this token is from a system header.  */
62   BOOL_BITFIELD in_system_header : 1;
63   /* True if this token is from a context where it is implicitly extern "C" */
64   BOOL_BITFIELD implicit_extern_c : 1;
65   /* True for a CPP_NAME token that is not a keyword (i.e., for which
66      KEYWORD is RID_MAX) iff this name was looked up and found to be
67      ambiguous.  An error has already been reported.  */
68   BOOL_BITFIELD ambiguous_p : 1;
69   /* The input file stack index at which this token was found.  */
70   unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
71   /* The value associated with this token, if any.  */
72   tree value;
73   /* The location at which this token was found.  */
74   location_t location;
75 } cp_token;
76
77 /* We use a stack of token pointer for saving token sets.  */
78 typedef struct cp_token *cp_token_position;
79 DEF_VEC_P (cp_token_position);
80 DEF_VEC_ALLOC_P (cp_token_position,heap);
81
82 static const cp_token eof_token =
83 {
84   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, NULL_TREE,
85 #if USE_MAPPED_LOCATION
86   0
87 #else
88   {0, 0}
89 #endif
90 };
91
92 /* The cp_lexer structure represents the C++ lexer.  It is responsible
93    for managing the token stream from the preprocessor and supplying
94    it to the parser.  Tokens are never added to the cp_lexer after
95    it is created.  */
96
97 typedef struct cp_lexer GTY (())
98 {
99   /* The memory allocated for the buffer.  NULL if this lexer does not
100      own the token buffer.  */
101   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
102   /* If the lexer owns the buffer, this is the number of tokens in the
103      buffer.  */
104   size_t buffer_length;
105
106   /* A pointer just past the last available token.  The tokens
107      in this lexer are [buffer, last_token).  */
108   cp_token_position GTY ((skip)) last_token;
109
110   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
111      no more available tokens.  */
112   cp_token_position GTY ((skip)) next_token;
113
114   /* A stack indicating positions at which cp_lexer_save_tokens was
115      called.  The top entry is the most recent position at which we
116      began saving tokens.  If the stack is non-empty, we are saving
117      tokens.  */
118   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
119
120   /* The next lexer in a linked list of lexers.  */
121   struct cp_lexer *next;
122
123   /* True if we should output debugging information.  */
124   bool debugging_p;
125
126   /* True if we're in the context of parsing a pragma, and should not
127      increment past the end-of-line marker.  */
128   bool in_pragma;
129 } cp_lexer;
130
131 /* cp_token_cache is a range of tokens.  There is no need to represent
132    allocate heap memory for it, since tokens are never removed from the
133    lexer's array.  There is also no need for the GC to walk through
134    a cp_token_cache, since everything in here is referenced through
135    a lexer.  */
136
137 typedef struct cp_token_cache GTY(())
138 {
139   /* The beginning of the token range.  */
140   cp_token * GTY((skip)) first;
141
142   /* Points immediately after the last token in the range.  */
143   cp_token * GTY ((skip)) last;
144 } cp_token_cache;
145
146 /* Prototypes.  */
147
148 static cp_lexer *cp_lexer_new_main
149   (void);
150 static cp_lexer *cp_lexer_new_from_tokens
151   (cp_token_cache *tokens);
152 static void cp_lexer_destroy
153   (cp_lexer *);
154 static int cp_lexer_saving_tokens
155   (const cp_lexer *);
156 static cp_token_position cp_lexer_token_position
157   (cp_lexer *, bool);
158 static cp_token *cp_lexer_token_at
159   (cp_lexer *, cp_token_position);
160 static void cp_lexer_get_preprocessor_token
161   (cp_lexer *, cp_token *);
162 static inline cp_token *cp_lexer_peek_token
163   (cp_lexer *);
164 static cp_token *cp_lexer_peek_nth_token
165   (cp_lexer *, size_t);
166 static inline bool cp_lexer_next_token_is
167   (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_not
169   (cp_lexer *, enum cpp_ttype);
170 static bool cp_lexer_next_token_is_keyword
171   (cp_lexer *, enum rid);
172 static cp_token *cp_lexer_consume_token
173   (cp_lexer *);
174 static void cp_lexer_purge_token
175   (cp_lexer *);
176 static void cp_lexer_purge_tokens_after
177   (cp_lexer *, cp_token_position);
178 static void cp_lexer_save_tokens
179   (cp_lexer *);
180 static void cp_lexer_commit_tokens
181   (cp_lexer *);
182 static void cp_lexer_rollback_tokens
183   (cp_lexer *);
184 #ifdef ENABLE_CHECKING
185 static void cp_lexer_print_token
186   (FILE *, cp_token *);
187 static inline bool cp_lexer_debugging_p
188   (cp_lexer *);
189 static void cp_lexer_start_debugging
190   (cp_lexer *) ATTRIBUTE_UNUSED;
191 static void cp_lexer_stop_debugging
192   (cp_lexer *) ATTRIBUTE_UNUSED;
193 #else
194 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
195    about passing NULL to functions that require non-NULL arguments
196    (fputs, fprintf).  It will never be used, so all we need is a value
197    of the right type that's guaranteed not to be NULL.  */
198 #define cp_lexer_debug_stream stdout
199 #define cp_lexer_print_token(str, tok) (void) 0
200 #define cp_lexer_debugging_p(lexer) 0
201 #endif /* ENABLE_CHECKING */
202
203 static cp_token_cache *cp_token_cache_new
204   (cp_token *, cp_token *);
205
206 static void cp_parser_initial_pragma
207   (cp_token *);
208
209 /* Manifest constants.  */
210 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
211 #define CP_SAVED_TOKEN_STACK 5
212
213 /* A token type for keywords, as opposed to ordinary identifiers.  */
214 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
215
216 /* A token type for template-ids.  If a template-id is processed while
217    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
218    the value of the CPP_TEMPLATE_ID is whatever was returned by
219    cp_parser_template_id.  */
220 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
221
222 /* A token type for nested-name-specifiers.  If a
223    nested-name-specifier is processed while parsing tentatively, it is
224    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
225    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
226    cp_parser_nested_name_specifier_opt.  */
227 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
228
229 /* A token type for tokens that are not tokens at all; these are used
230    to represent slots in the array where there used to be a token
231    that has now been deleted.  */
232 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
233
234 /* The number of token types, including C++-specific ones.  */
235 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
236
237 /* Variables.  */
238
239 #ifdef ENABLE_CHECKING
240 /* The stream to which debugging output should be written.  */
241 static FILE *cp_lexer_debug_stream;
242 #endif /* ENABLE_CHECKING */
243
244 /* Create a new main C++ lexer, the lexer that gets tokens from the
245    preprocessor.  */
246
247 static cp_lexer *
248 cp_lexer_new_main (void)
249 {
250   cp_token first_token;
251   cp_lexer *lexer;
252   cp_token *pos;
253   size_t alloc;
254   size_t space;
255   cp_token *buffer;
256
257   /* It's possible that parsing the first pragma will load a PCH file,
258      which is a GC collection point.  So we have to do that before
259      allocating any memory.  */
260   cp_parser_initial_pragma (&first_token);
261
262   /* Tell c_lex_with_flags not to merge string constants.  */
263   c_lex_return_raw_strings = true;
264
265   c_common_no_more_pch ();
266
267   /* Allocate the memory.  */
268   lexer = GGC_CNEW (cp_lexer);
269
270 #ifdef ENABLE_CHECKING
271   /* Initially we are not debugging.  */
272   lexer->debugging_p = false;
273 #endif /* ENABLE_CHECKING */
274   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
275                                    CP_SAVED_TOKEN_STACK);
276
277   /* Create the buffer.  */
278   alloc = CP_LEXER_BUFFER_SIZE;
279   buffer = GGC_NEWVEC (cp_token, alloc);
280
281   /* Put the first token in the buffer.  */
282   space = alloc;
283   pos = buffer;
284   *pos = first_token;
285
286   /* Get the remaining tokens from the preprocessor.  */
287   while (pos->type != CPP_EOF)
288     {
289       pos++;
290       if (!--space)
291         {
292           space = alloc;
293           alloc *= 2;
294           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
295           pos = buffer + space;
296         }
297       cp_lexer_get_preprocessor_token (lexer, pos);
298     }
299   lexer->buffer = buffer;
300   lexer->buffer_length = alloc - space;
301   lexer->last_token = pos;
302   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
303
304   /* Subsequent preprocessor diagnostics should use compiler
305      diagnostic functions to get the compiler source location.  */
306   cpp_get_options (parse_in)->client_diagnostic = true;
307   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
308
309   gcc_assert (lexer->next_token->type != CPP_PURGED);
310   return lexer;
311 }
312
313 /* Create a new lexer whose token stream is primed with the tokens in
314    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
315
316 static cp_lexer *
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
318 {
319   cp_token *first = cache->first;
320   cp_token *last = cache->last;
321   cp_lexer *lexer = GGC_CNEW (cp_lexer);
322
323   /* We do not own the buffer.  */
324   lexer->buffer = NULL;
325   lexer->buffer_length = 0;
326   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327   lexer->last_token = last;
328
329   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330                                    CP_SAVED_TOKEN_STACK);
331
332 #ifdef ENABLE_CHECKING
333   /* Initially we are not debugging.  */
334   lexer->debugging_p = false;
335 #endif
336
337   gcc_assert (lexer->next_token->type != CPP_PURGED);
338   return lexer;
339 }
340
341 /* Frees all resources associated with LEXER.  */
342
343 static void
344 cp_lexer_destroy (cp_lexer *lexer)
345 {
346   if (lexer->buffer)
347     ggc_free (lexer->buffer);
348   VEC_free (cp_token_position, heap, lexer->saved_tokens);
349   ggc_free (lexer);
350 }
351
352 /* Returns nonzero if debugging information should be output.  */
353
354 #ifdef ENABLE_CHECKING
355
356 static inline bool
357 cp_lexer_debugging_p (cp_lexer *lexer)
358 {
359   return lexer->debugging_p;
360 }
361
362 #endif /* ENABLE_CHECKING */
363
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
366 {
367   gcc_assert (!previous_p || lexer->next_token != &eof_token);
368
369   return lexer->next_token - previous_p;
370 }
371
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
374 {
375   return pos;
376 }
377
378 /* nonzero if we are presently saving tokens.  */
379
380 static inline int
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
382 {
383   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
384 }
385
386 /* Store the next token from the preprocessor in *TOKEN.  Return true
387    if we reach EOF.  */
388
389 static void
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391                                  cp_token *token)
392 {
393   static int is_extern_c = 0;
394
395    /* Get a new token from the preprocessor.  */
396   token->type
397     = c_lex_with_flags (&token->value, &token->location, &token->flags);
398   token->input_file_stack_index = input_file_stack_tick;
399   token->keyword = RID_MAX;
400   token->pragma_kind = PRAGMA_NONE;
401   token->in_system_header = in_system_header;
402
403   /* On some systems, some header files are surrounded by an
404      implicit extern "C" block.  Set a flag in the token if it
405      comes from such a header.  */
406   is_extern_c += pending_lang_change;
407   pending_lang_change = 0;
408   token->implicit_extern_c = is_extern_c > 0;
409
410   /* Check to see if this token is a keyword.  */
411   if (token->type == CPP_NAME)
412     {
413       if (C_IS_RESERVED_WORD (token->value))
414         {
415           /* Mark this token as a keyword.  */
416           token->type = CPP_KEYWORD;
417           /* Record which keyword.  */
418           token->keyword = C_RID_CODE (token->value);
419           /* Update the value.  Some keywords are mapped to particular
420              entities, rather than simply having the value of the
421              corresponding IDENTIFIER_NODE.  For example, `__const' is
422              mapped to `const'.  */
423           token->value = ridpointers[token->keyword];
424         }
425       else
426         {
427           token->ambiguous_p = false;
428           token->keyword = RID_MAX;
429         }
430     }
431   /* Handle Objective-C++ keywords.  */
432   else if (token->type == CPP_AT_NAME)
433     {
434       token->type = CPP_KEYWORD;
435       switch (C_RID_CODE (token->value))
436         {
437         /* Map 'class' to '@class', 'private' to '@private', etc.  */
438         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
439         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
440         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
441         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
442         case RID_THROW: token->keyword = RID_AT_THROW; break;
443         case RID_TRY: token->keyword = RID_AT_TRY; break;
444         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
445         default: token->keyword = C_RID_CODE (token->value);
446         }
447     }
448   else if (token->type == CPP_PRAGMA)
449     {
450       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
451       token->pragma_kind = TREE_INT_CST_LOW (token->value);
452       token->value = NULL;
453     }
454 }
455
456 /* Update the globals input_location and in_system_header and the
457    input file stack from TOKEN.  */
458 static inline void
459 cp_lexer_set_source_position_from_token (cp_token *token)
460 {
461   if (token->type != CPP_EOF)
462     {
463       input_location = token->location;
464       in_system_header = token->in_system_header;
465       restore_input_file_stack (token->input_file_stack_index);
466     }
467 }
468
469 /* Return a pointer to the next token in the token stream, but do not
470    consume it.  */
471
472 static inline cp_token *
473 cp_lexer_peek_token (cp_lexer *lexer)
474 {
475   if (cp_lexer_debugging_p (lexer))
476     {
477       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
478       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
479       putc ('\n', cp_lexer_debug_stream);
480     }
481   return lexer->next_token;
482 }
483
484 /* Return true if the next token has the indicated TYPE.  */
485
486 static inline bool
487 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
488 {
489   return cp_lexer_peek_token (lexer)->type == type;
490 }
491
492 /* Return true if the next token does not have the indicated TYPE.  */
493
494 static inline bool
495 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
496 {
497   return !cp_lexer_next_token_is (lexer, type);
498 }
499
500 /* Return true if the next token is the indicated KEYWORD.  */
501
502 static inline bool
503 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
504 {
505   return cp_lexer_peek_token (lexer)->keyword == keyword;
506 }
507
508 /* Return true if the next token is a keyword for a decl-specifier.  */
509
510 static bool
511 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
512 {
513   cp_token *token;
514
515   token = cp_lexer_peek_token (lexer);
516   switch (token->keyword) 
517     {
518       /* Storage classes.  */
519     case RID_AUTO:
520     case RID_REGISTER:
521     case RID_STATIC:
522     case RID_EXTERN:
523     case RID_MUTABLE:
524     case RID_THREAD:
525       /* Elaborated type specifiers.  */
526     case RID_ENUM:
527     case RID_CLASS:
528     case RID_STRUCT:
529     case RID_UNION:
530     case RID_TYPENAME:
531       /* Simple type specifiers.  */
532     case RID_CHAR:
533     case RID_WCHAR:
534     case RID_BOOL:
535     case RID_SHORT:
536     case RID_INT:
537     case RID_LONG:
538     case RID_SIGNED:
539     case RID_UNSIGNED:
540     case RID_FLOAT:
541     case RID_DOUBLE:
542     case RID_VOID:
543       /* GNU extensions.  */ 
544     case RID_ATTRIBUTE:
545     case RID_TYPEOF:
546       return true;
547
548     default:
549       return false;
550     }
551 }
552
553 /* Return a pointer to the Nth token in the token stream.  If N is 1,
554    then this is precisely equivalent to cp_lexer_peek_token (except
555    that it is not inline).  One would like to disallow that case, but
556    there is one case (cp_parser_nth_token_starts_template_id) where
557    the caller passes a variable for N and it might be 1.  */
558
559 static cp_token *
560 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
561 {
562   cp_token *token;
563
564   /* N is 1-based, not zero-based.  */
565   gcc_assert (n > 0);
566
567   if (cp_lexer_debugging_p (lexer))
568     fprintf (cp_lexer_debug_stream,
569              "cp_lexer: peeking ahead %ld at token: ", (long)n);
570
571   --n;
572   token = lexer->next_token;
573   gcc_assert (!n || token != &eof_token);
574   while (n != 0)
575     {
576       ++token;
577       if (token == lexer->last_token)
578         {
579           token = (cp_token *)&eof_token;
580           break;
581         }
582
583       if (token->type != CPP_PURGED)
584         --n;
585     }
586
587   if (cp_lexer_debugging_p (lexer))
588     {
589       cp_lexer_print_token (cp_lexer_debug_stream, token);
590       putc ('\n', cp_lexer_debug_stream);
591     }
592
593   return token;
594 }
595
596 /* Return the next token, and advance the lexer's next_token pointer
597    to point to the next non-purged token.  */
598
599 static cp_token *
600 cp_lexer_consume_token (cp_lexer* lexer)
601 {
602   cp_token *token = lexer->next_token;
603
604   gcc_assert (token != &eof_token);
605   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
606
607   do
608     {
609       lexer->next_token++;
610       if (lexer->next_token == lexer->last_token)
611         {
612           lexer->next_token = (cp_token *)&eof_token;
613           break;
614         }
615
616     }
617   while (lexer->next_token->type == CPP_PURGED);
618
619   cp_lexer_set_source_position_from_token (token);
620
621   /* Provide debugging output.  */
622   if (cp_lexer_debugging_p (lexer))
623     {
624       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
625       cp_lexer_print_token (cp_lexer_debug_stream, token);
626       putc ('\n', cp_lexer_debug_stream);
627     }
628
629   return token;
630 }
631
632 /* Permanently remove the next token from the token stream, and
633    advance the next_token pointer to refer to the next non-purged
634    token.  */
635
636 static void
637 cp_lexer_purge_token (cp_lexer *lexer)
638 {
639   cp_token *tok = lexer->next_token;
640
641   gcc_assert (tok != &eof_token);
642   tok->type = CPP_PURGED;
643   tok->location = UNKNOWN_LOCATION;
644   tok->value = NULL_TREE;
645   tok->keyword = RID_MAX;
646
647   do
648     {
649       tok++;
650       if (tok == lexer->last_token)
651         {
652           tok = (cp_token *)&eof_token;
653           break;
654         }
655     }
656   while (tok->type == CPP_PURGED);
657   lexer->next_token = tok;
658 }
659
660 /* Permanently remove all tokens after TOK, up to, but not
661    including, the token that will be returned next by
662    cp_lexer_peek_token.  */
663
664 static void
665 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
666 {
667   cp_token *peek = lexer->next_token;
668
669   if (peek == &eof_token)
670     peek = lexer->last_token;
671
672   gcc_assert (tok < peek);
673
674   for ( tok += 1; tok != peek; tok += 1)
675     {
676       tok->type = CPP_PURGED;
677       tok->location = UNKNOWN_LOCATION;
678       tok->value = NULL_TREE;
679       tok->keyword = RID_MAX;
680     }
681 }
682
683 /* Begin saving tokens.  All tokens consumed after this point will be
684    preserved.  */
685
686 static void
687 cp_lexer_save_tokens (cp_lexer* lexer)
688 {
689   /* Provide debugging output.  */
690   if (cp_lexer_debugging_p (lexer))
691     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
692
693   VEC_safe_push (cp_token_position, heap,
694                  lexer->saved_tokens, lexer->next_token);
695 }
696
697 /* Commit to the portion of the token stream most recently saved.  */
698
699 static void
700 cp_lexer_commit_tokens (cp_lexer* lexer)
701 {
702   /* Provide debugging output.  */
703   if (cp_lexer_debugging_p (lexer))
704     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
705
706   VEC_pop (cp_token_position, lexer->saved_tokens);
707 }
708
709 /* Return all tokens saved since the last call to cp_lexer_save_tokens
710    to the token stream.  Stop saving tokens.  */
711
712 static void
713 cp_lexer_rollback_tokens (cp_lexer* lexer)
714 {
715   /* Provide debugging output.  */
716   if (cp_lexer_debugging_p (lexer))
717     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
718
719   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
720 }
721
722 /* Print a representation of the TOKEN on the STREAM.  */
723
724 #ifdef ENABLE_CHECKING
725
726 static void
727 cp_lexer_print_token (FILE * stream, cp_token *token)
728 {
729   /* We don't use cpp_type2name here because the parser defines
730      a few tokens of its own.  */
731   static const char *const token_names[] = {
732     /* cpplib-defined token types */
733 #define OP(e, s) #e,
734 #define TK(e, s) #e,
735     TTYPE_TABLE
736 #undef OP
737 #undef TK
738     /* C++ parser token types - see "Manifest constants", above.  */
739     "KEYWORD",
740     "TEMPLATE_ID",
741     "NESTED_NAME_SPECIFIER",
742     "PURGED"
743   };
744
745   /* If we have a name for the token, print it out.  Otherwise, we
746      simply give the numeric code.  */
747   gcc_assert (token->type < ARRAY_SIZE(token_names));
748   fputs (token_names[token->type], stream);
749
750   /* For some tokens, print the associated data.  */
751   switch (token->type)
752     {
753     case CPP_KEYWORD:
754       /* Some keywords have a value that is not an IDENTIFIER_NODE.
755          For example, `struct' is mapped to an INTEGER_CST.  */
756       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
757         break;
758       /* else fall through */
759     case CPP_NAME:
760       fputs (IDENTIFIER_POINTER (token->value), stream);
761       break;
762
763     case CPP_STRING:
764     case CPP_WSTRING:
765       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
766       break;
767
768     default:
769       break;
770     }
771 }
772
773 /* Start emitting debugging information.  */
774
775 static void
776 cp_lexer_start_debugging (cp_lexer* lexer)
777 {
778   lexer->debugging_p = true;
779 }
780
781 /* Stop emitting debugging information.  */
782
783 static void
784 cp_lexer_stop_debugging (cp_lexer* lexer)
785 {
786   lexer->debugging_p = false;
787 }
788
789 #endif /* ENABLE_CHECKING */
790
791 /* Create a new cp_token_cache, representing a range of tokens.  */
792
793 static cp_token_cache *
794 cp_token_cache_new (cp_token *first, cp_token *last)
795 {
796   cp_token_cache *cache = GGC_NEW (cp_token_cache);
797   cache->first = first;
798   cache->last = last;
799   return cache;
800 }
801
802 \f
803 /* Decl-specifiers.  */
804
805 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
806
807 static void
808 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
809 {
810   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
811 }
812
813 /* Declarators.  */
814
815 /* Nothing other than the parser should be creating declarators;
816    declarators are a semi-syntactic representation of C++ entities.
817    Other parts of the front end that need to create entities (like
818    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
819
820 static cp_declarator *make_call_declarator
821   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
822 static cp_declarator *make_array_declarator
823   (cp_declarator *, tree);
824 static cp_declarator *make_pointer_declarator
825   (cp_cv_quals, cp_declarator *);
826 static cp_declarator *make_reference_declarator
827   (cp_cv_quals, cp_declarator *);
828 static cp_parameter_declarator *make_parameter_declarator
829   (cp_decl_specifier_seq *, cp_declarator *, tree);
830 static cp_declarator *make_ptrmem_declarator
831   (cp_cv_quals, tree, cp_declarator *);
832
833 /* An erroneous declarator.  */
834 static cp_declarator *cp_error_declarator;
835
836 /* The obstack on which declarators and related data structures are
837    allocated.  */
838 static struct obstack declarator_obstack;
839
840 /* Alloc BYTES from the declarator memory pool.  */
841
842 static inline void *
843 alloc_declarator (size_t bytes)
844 {
845   return obstack_alloc (&declarator_obstack, bytes);
846 }
847
848 /* Allocate a declarator of the indicated KIND.  Clear fields that are
849    common to all declarators.  */
850
851 static cp_declarator *
852 make_declarator (cp_declarator_kind kind)
853 {
854   cp_declarator *declarator;
855
856   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
857   declarator->kind = kind;
858   declarator->attributes = NULL_TREE;
859   declarator->declarator = NULL;
860
861   return declarator;
862 }
863
864 /* Make a declarator for a generalized identifier.  If
865    QUALIFYING_SCOPE is non-NULL, the identifier is
866    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
867    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
868    is, if any.   */
869
870 static cp_declarator *
871 make_id_declarator (tree qualifying_scope, tree unqualified_name,
872                     special_function_kind sfk)
873 {
874   cp_declarator *declarator;
875
876   /* It is valid to write:
877
878        class C { void f(); };
879        typedef C D;
880        void D::f();
881
882      The standard is not clear about whether `typedef const C D' is
883      legal; as of 2002-09-15 the committee is considering that
884      question.  EDG 3.0 allows that syntax.  Therefore, we do as
885      well.  */
886   if (qualifying_scope && TYPE_P (qualifying_scope))
887     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
888
889   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
890               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
891               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
892
893   declarator = make_declarator (cdk_id);
894   declarator->u.id.qualifying_scope = qualifying_scope;
895   declarator->u.id.unqualified_name = unqualified_name;
896   declarator->u.id.sfk = sfk;
897
898   return declarator;
899 }
900
901 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
902    of modifiers such as const or volatile to apply to the pointer
903    type, represented as identifiers.  */
904
905 cp_declarator *
906 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
907 {
908   cp_declarator *declarator;
909
910   declarator = make_declarator (cdk_pointer);
911   declarator->declarator = target;
912   declarator->u.pointer.qualifiers = cv_qualifiers;
913   declarator->u.pointer.class_type = NULL_TREE;
914
915   return declarator;
916 }
917
918 /* Like make_pointer_declarator -- but for references.  */
919
920 cp_declarator *
921 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
922 {
923   cp_declarator *declarator;
924
925   declarator = make_declarator (cdk_reference);
926   declarator->declarator = target;
927   declarator->u.pointer.qualifiers = cv_qualifiers;
928   declarator->u.pointer.class_type = NULL_TREE;
929
930   return declarator;
931 }
932
933 /* Like make_pointer_declarator -- but for a pointer to a non-static
934    member of CLASS_TYPE.  */
935
936 cp_declarator *
937 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
938                         cp_declarator *pointee)
939 {
940   cp_declarator *declarator;
941
942   declarator = make_declarator (cdk_ptrmem);
943   declarator->declarator = pointee;
944   declarator->u.pointer.qualifiers = cv_qualifiers;
945   declarator->u.pointer.class_type = class_type;
946
947   return declarator;
948 }
949
950 /* Make a declarator for the function given by TARGET, with the
951    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
952    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
953    indicates what exceptions can be thrown.  */
954
955 cp_declarator *
956 make_call_declarator (cp_declarator *target,
957                       cp_parameter_declarator *parms,
958                       cp_cv_quals cv_qualifiers,
959                       tree exception_specification)
960 {
961   cp_declarator *declarator;
962
963   declarator = make_declarator (cdk_function);
964   declarator->declarator = target;
965   declarator->u.function.parameters = parms;
966   declarator->u.function.qualifiers = cv_qualifiers;
967   declarator->u.function.exception_specification = exception_specification;
968
969   return declarator;
970 }
971
972 /* Make a declarator for an array of BOUNDS elements, each of which is
973    defined by ELEMENT.  */
974
975 cp_declarator *
976 make_array_declarator (cp_declarator *element, tree bounds)
977 {
978   cp_declarator *declarator;
979
980   declarator = make_declarator (cdk_array);
981   declarator->declarator = element;
982   declarator->u.array.bounds = bounds;
983
984   return declarator;
985 }
986
987 cp_parameter_declarator *no_parameters;
988
989 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
990    DECLARATOR and DEFAULT_ARGUMENT.  */
991
992 cp_parameter_declarator *
993 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
994                            cp_declarator *declarator,
995                            tree default_argument)
996 {
997   cp_parameter_declarator *parameter;
998
999   parameter = ((cp_parameter_declarator *)
1000                alloc_declarator (sizeof (cp_parameter_declarator)));
1001   parameter->next = NULL;
1002   if (decl_specifiers)
1003     parameter->decl_specifiers = *decl_specifiers;
1004   else
1005     clear_decl_specs (&parameter->decl_specifiers);
1006   parameter->declarator = declarator;
1007   parameter->default_argument = default_argument;
1008   parameter->ellipsis_p = false;
1009
1010   return parameter;
1011 }
1012
1013 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1014
1015 static bool
1016 function_declarator_p (const cp_declarator *declarator)
1017 {
1018   while (declarator)
1019     {
1020       if (declarator->kind == cdk_function
1021           && declarator->declarator->kind == cdk_id)
1022         return true;
1023       if (declarator->kind == cdk_id
1024           || declarator->kind == cdk_error)
1025         return false;
1026       declarator = declarator->declarator;
1027     }
1028   return false;
1029 }
1030  
1031 /* The parser.  */
1032
1033 /* Overview
1034    --------
1035
1036    A cp_parser parses the token stream as specified by the C++
1037    grammar.  Its job is purely parsing, not semantic analysis.  For
1038    example, the parser breaks the token stream into declarators,
1039    expressions, statements, and other similar syntactic constructs.
1040    It does not check that the types of the expressions on either side
1041    of an assignment-statement are compatible, or that a function is
1042    not declared with a parameter of type `void'.
1043
1044    The parser invokes routines elsewhere in the compiler to perform
1045    semantic analysis and to build up the abstract syntax tree for the
1046    code processed.
1047
1048    The parser (and the template instantiation code, which is, in a
1049    way, a close relative of parsing) are the only parts of the
1050    compiler that should be calling push_scope and pop_scope, or
1051    related functions.  The parser (and template instantiation code)
1052    keeps track of what scope is presently active; everything else
1053    should simply honor that.  (The code that generates static
1054    initializers may also need to set the scope, in order to check
1055    access control correctly when emitting the initializers.)
1056
1057    Methodology
1058    -----------
1059
1060    The parser is of the standard recursive-descent variety.  Upcoming
1061    tokens in the token stream are examined in order to determine which
1062    production to use when parsing a non-terminal.  Some C++ constructs
1063    require arbitrary look ahead to disambiguate.  For example, it is
1064    impossible, in the general case, to tell whether a statement is an
1065    expression or declaration without scanning the entire statement.
1066    Therefore, the parser is capable of "parsing tentatively."  When the
1067    parser is not sure what construct comes next, it enters this mode.
1068    Then, while we attempt to parse the construct, the parser queues up
1069    error messages, rather than issuing them immediately, and saves the
1070    tokens it consumes.  If the construct is parsed successfully, the
1071    parser "commits", i.e., it issues any queued error messages and
1072    the tokens that were being preserved are permanently discarded.
1073    If, however, the construct is not parsed successfully, the parser
1074    rolls back its state completely so that it can resume parsing using
1075    a different alternative.
1076
1077    Future Improvements
1078    -------------------
1079
1080    The performance of the parser could probably be improved substantially.
1081    We could often eliminate the need to parse tentatively by looking ahead
1082    a little bit.  In some places, this approach might not entirely eliminate
1083    the need to parse tentatively, but it might still speed up the average
1084    case.  */
1085
1086 /* Flags that are passed to some parsing functions.  These values can
1087    be bitwise-ored together.  */
1088
1089 typedef enum cp_parser_flags
1090 {
1091   /* No flags.  */
1092   CP_PARSER_FLAGS_NONE = 0x0,
1093   /* The construct is optional.  If it is not present, then no error
1094      should be issued.  */
1095   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1096   /* When parsing a type-specifier, do not allow user-defined types.  */
1097   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1098 } cp_parser_flags;
1099
1100 /* The different kinds of declarators we want to parse.  */
1101
1102 typedef enum cp_parser_declarator_kind
1103 {
1104   /* We want an abstract declarator.  */
1105   CP_PARSER_DECLARATOR_ABSTRACT,
1106   /* We want a named declarator.  */
1107   CP_PARSER_DECLARATOR_NAMED,
1108   /* We don't mind, but the name must be an unqualified-id.  */
1109   CP_PARSER_DECLARATOR_EITHER
1110 } cp_parser_declarator_kind;
1111
1112 /* The precedence values used to parse binary expressions.  The minimum value
1113    of PREC must be 1, because zero is reserved to quickly discriminate
1114    binary operators from other tokens.  */
1115
1116 enum cp_parser_prec
1117 {
1118   PREC_NOT_OPERATOR,
1119   PREC_LOGICAL_OR_EXPRESSION,
1120   PREC_LOGICAL_AND_EXPRESSION,
1121   PREC_INCLUSIVE_OR_EXPRESSION,
1122   PREC_EXCLUSIVE_OR_EXPRESSION,
1123   PREC_AND_EXPRESSION,
1124   PREC_EQUALITY_EXPRESSION,
1125   PREC_RELATIONAL_EXPRESSION,
1126   PREC_SHIFT_EXPRESSION,
1127   PREC_ADDITIVE_EXPRESSION,
1128   PREC_MULTIPLICATIVE_EXPRESSION,
1129   PREC_PM_EXPRESSION,
1130   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1131 };
1132
1133 /* A mapping from a token type to a corresponding tree node type, with a
1134    precedence value.  */
1135
1136 typedef struct cp_parser_binary_operations_map_node
1137 {
1138   /* The token type.  */
1139   enum cpp_ttype token_type;
1140   /* The corresponding tree code.  */
1141   enum tree_code tree_type;
1142   /* The precedence of this operator.  */
1143   enum cp_parser_prec prec;
1144 } cp_parser_binary_operations_map_node;
1145
1146 /* The status of a tentative parse.  */
1147
1148 typedef enum cp_parser_status_kind
1149 {
1150   /* No errors have occurred.  */
1151   CP_PARSER_STATUS_KIND_NO_ERROR,
1152   /* An error has occurred.  */
1153   CP_PARSER_STATUS_KIND_ERROR,
1154   /* We are committed to this tentative parse, whether or not an error
1155      has occurred.  */
1156   CP_PARSER_STATUS_KIND_COMMITTED
1157 } cp_parser_status_kind;
1158
1159 typedef struct cp_parser_expression_stack_entry
1160 {
1161   tree lhs;
1162   enum tree_code tree_type;
1163   int prec;
1164 } cp_parser_expression_stack_entry;
1165
1166 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1167    entries because precedence levels on the stack are monotonically
1168    increasing.  */
1169 typedef struct cp_parser_expression_stack_entry
1170   cp_parser_expression_stack[NUM_PREC_VALUES];
1171
1172 /* Context that is saved and restored when parsing tentatively.  */
1173 typedef struct cp_parser_context GTY (())
1174 {
1175   /* If this is a tentative parsing context, the status of the
1176      tentative parse.  */
1177   enum cp_parser_status_kind status;
1178   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1179      that are looked up in this context must be looked up both in the
1180      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1181      the context of the containing expression.  */
1182   tree object_type;
1183
1184   /* The next parsing context in the stack.  */
1185   struct cp_parser_context *next;
1186 } cp_parser_context;
1187
1188 /* Prototypes.  */
1189
1190 /* Constructors and destructors.  */
1191
1192 static cp_parser_context *cp_parser_context_new
1193   (cp_parser_context *);
1194
1195 /* Class variables.  */
1196
1197 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1198
1199 /* The operator-precedence table used by cp_parser_binary_expression.
1200    Transformed into an associative array (binops_by_token) by
1201    cp_parser_new.  */
1202
1203 static const cp_parser_binary_operations_map_node binops[] = {
1204   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1205   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1206
1207   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1208   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1209   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1210
1211   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1212   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1213
1214   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1215   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1216
1217   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1218   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1219   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1220   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1221
1222   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1223   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1224
1225   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1226
1227   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1228
1229   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1230
1231   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1232
1233   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1234 };
1235
1236 /* The same as binops, but initialized by cp_parser_new so that
1237    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1238    for speed.  */
1239 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1240
1241 /* Constructors and destructors.  */
1242
1243 /* Construct a new context.  The context below this one on the stack
1244    is given by NEXT.  */
1245
1246 static cp_parser_context *
1247 cp_parser_context_new (cp_parser_context* next)
1248 {
1249   cp_parser_context *context;
1250
1251   /* Allocate the storage.  */
1252   if (cp_parser_context_free_list != NULL)
1253     {
1254       /* Pull the first entry from the free list.  */
1255       context = cp_parser_context_free_list;
1256       cp_parser_context_free_list = context->next;
1257       memset (context, 0, sizeof (*context));
1258     }
1259   else
1260     context = GGC_CNEW (cp_parser_context);
1261
1262   /* No errors have occurred yet in this context.  */
1263   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1264   /* If this is not the bottomost context, copy information that we
1265      need from the previous context.  */
1266   if (next)
1267     {
1268       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1269          expression, then we are parsing one in this context, too.  */
1270       context->object_type = next->object_type;
1271       /* Thread the stack.  */
1272       context->next = next;
1273     }
1274
1275   return context;
1276 }
1277
1278 /* The cp_parser structure represents the C++ parser.  */
1279
1280 typedef struct cp_parser GTY(())
1281 {
1282   /* The lexer from which we are obtaining tokens.  */
1283   cp_lexer *lexer;
1284
1285   /* The scope in which names should be looked up.  If NULL_TREE, then
1286      we look up names in the scope that is currently open in the
1287      source program.  If non-NULL, this is either a TYPE or
1288      NAMESPACE_DECL for the scope in which we should look.  It can
1289      also be ERROR_MARK, when we've parsed a bogus scope.
1290
1291      This value is not cleared automatically after a name is looked
1292      up, so we must be careful to clear it before starting a new look
1293      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1294      will look up `Z' in the scope of `X', rather than the current
1295      scope.)  Unfortunately, it is difficult to tell when name lookup
1296      is complete, because we sometimes peek at a token, look it up,
1297      and then decide not to consume it.   */
1298   tree scope;
1299
1300   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1301      last lookup took place.  OBJECT_SCOPE is used if an expression
1302      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1303      respectively.  QUALIFYING_SCOPE is used for an expression of the
1304      form "X::Y"; it refers to X.  */
1305   tree object_scope;
1306   tree qualifying_scope;
1307
1308   /* A stack of parsing contexts.  All but the bottom entry on the
1309      stack will be tentative contexts.
1310
1311      We parse tentatively in order to determine which construct is in
1312      use in some situations.  For example, in order to determine
1313      whether a statement is an expression-statement or a
1314      declaration-statement we parse it tentatively as a
1315      declaration-statement.  If that fails, we then reparse the same
1316      token stream as an expression-statement.  */
1317   cp_parser_context *context;
1318
1319   /* True if we are parsing GNU C++.  If this flag is not set, then
1320      GNU extensions are not recognized.  */
1321   bool allow_gnu_extensions_p;
1322
1323   /* TRUE if the `>' token should be interpreted as the greater-than
1324      operator.  FALSE if it is the end of a template-id or
1325      template-parameter-list.  */
1326   bool greater_than_is_operator_p;
1327
1328   /* TRUE if default arguments are allowed within a parameter list
1329      that starts at this point. FALSE if only a gnu extension makes
1330      them permissible.  */
1331   bool default_arg_ok_p;
1332
1333   /* TRUE if we are parsing an integral constant-expression.  See
1334      [expr.const] for a precise definition.  */
1335   bool integral_constant_expression_p;
1336
1337   /* TRUE if we are parsing an integral constant-expression -- but a
1338      non-constant expression should be permitted as well.  This flag
1339      is used when parsing an array bound so that GNU variable-length
1340      arrays are tolerated.  */
1341   bool allow_non_integral_constant_expression_p;
1342
1343   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1344      been seen that makes the expression non-constant.  */
1345   bool non_integral_constant_expression_p;
1346
1347   /* TRUE if local variable names and `this' are forbidden in the
1348      current context.  */
1349   bool local_variables_forbidden_p;
1350
1351   /* TRUE if the declaration we are parsing is part of a
1352      linkage-specification of the form `extern string-literal
1353      declaration'.  */
1354   bool in_unbraced_linkage_specification_p;
1355
1356   /* TRUE if we are presently parsing a declarator, after the
1357      direct-declarator.  */
1358   bool in_declarator_p;
1359
1360   /* TRUE if we are presently parsing a template-argument-list.  */
1361   bool in_template_argument_list_p;
1362
1363   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1364      to IN_OMP_BLOCK if parsing OpenMP structured block and
1365      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1366      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1367      iteration-statement, OpenMP block or loop within that switch.  */
1368 #define IN_SWITCH_STMT          1
1369 #define IN_ITERATION_STMT       2
1370 #define IN_OMP_BLOCK            4
1371 #define IN_OMP_FOR              8
1372   unsigned char in_statement;
1373
1374   /* TRUE if we are presently parsing the body of a switch statement.
1375      Note that this doesn't quite overlap with in_statement above.
1376      The difference relates to giving the right sets of error messages:
1377      "case not in switch" vs "break statement used with OpenMP...".  */
1378   bool in_switch_statement_p;
1379
1380   /* TRUE if we are parsing a type-id in an expression context.  In
1381      such a situation, both "type (expr)" and "type (type)" are valid
1382      alternatives.  */
1383   bool in_type_id_in_expr_p;
1384
1385   /* TRUE if we are currently in a header file where declarations are
1386      implicitly extern "C".  */
1387   bool implicit_extern_c;
1388
1389   /* TRUE if strings in expressions should be translated to the execution
1390      character set.  */
1391   bool translate_strings_p;
1392
1393   /* TRUE if we are presently parsing the body of a function, but not
1394      a local class.  */
1395   bool in_function_body;
1396
1397   /* If non-NULL, then we are parsing a construct where new type
1398      definitions are not permitted.  The string stored here will be
1399      issued as an error message if a type is defined.  */
1400   const char *type_definition_forbidden_message;
1401
1402   /* A list of lists. The outer list is a stack, used for member
1403      functions of local classes. At each level there are two sub-list,
1404      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1405      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1406      TREE_VALUE's. The functions are chained in reverse declaration
1407      order.
1408
1409      The TREE_PURPOSE sublist contains those functions with default
1410      arguments that need post processing, and the TREE_VALUE sublist
1411      contains those functions with definitions that need post
1412      processing.
1413
1414      These lists can only be processed once the outermost class being
1415      defined is complete.  */
1416   tree unparsed_functions_queues;
1417
1418   /* The number of classes whose definitions are currently in
1419      progress.  */
1420   unsigned num_classes_being_defined;
1421
1422   /* The number of template parameter lists that apply directly to the
1423      current declaration.  */
1424   unsigned num_template_parameter_lists;
1425 } cp_parser;
1426
1427 /* Prototypes.  */
1428
1429 /* Constructors and destructors.  */
1430
1431 static cp_parser *cp_parser_new
1432   (void);
1433
1434 /* Routines to parse various constructs.
1435
1436    Those that return `tree' will return the error_mark_node (rather
1437    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1438    Sometimes, they will return an ordinary node if error-recovery was
1439    attempted, even though a parse error occurred.  So, to check
1440    whether or not a parse error occurred, you should always use
1441    cp_parser_error_occurred.  If the construct is optional (indicated
1442    either by an `_opt' in the name of the function that does the
1443    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1444    the construct is not present.  */
1445
1446 /* Lexical conventions [gram.lex]  */
1447
1448 static tree cp_parser_identifier
1449   (cp_parser *);
1450 static tree cp_parser_string_literal
1451   (cp_parser *, bool, bool);
1452
1453 /* Basic concepts [gram.basic]  */
1454
1455 static bool cp_parser_translation_unit
1456   (cp_parser *);
1457
1458 /* Expressions [gram.expr]  */
1459
1460 static tree cp_parser_primary_expression
1461   (cp_parser *, bool, bool, bool, cp_id_kind *);
1462 static tree cp_parser_id_expression
1463   (cp_parser *, bool, bool, bool *, bool, bool);
1464 static tree cp_parser_unqualified_id
1465   (cp_parser *, bool, bool, bool, bool);
1466 static tree cp_parser_nested_name_specifier_opt
1467   (cp_parser *, bool, bool, bool, bool);
1468 static tree cp_parser_nested_name_specifier
1469   (cp_parser *, bool, bool, bool, bool);
1470 static tree cp_parser_class_or_namespace_name
1471   (cp_parser *, bool, bool, bool, bool, bool);
1472 static tree cp_parser_postfix_expression
1473   (cp_parser *, bool, bool);
1474 static tree cp_parser_postfix_open_square_expression
1475   (cp_parser *, tree, bool);
1476 static tree cp_parser_postfix_dot_deref_expression
1477   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1478 static tree cp_parser_parenthesized_expression_list
1479   (cp_parser *, bool, bool, bool *);
1480 static void cp_parser_pseudo_destructor_name
1481   (cp_parser *, tree *, tree *);
1482 static tree cp_parser_unary_expression
1483   (cp_parser *, bool, bool);
1484 static enum tree_code cp_parser_unary_operator
1485   (cp_token *);
1486 static tree cp_parser_new_expression
1487   (cp_parser *);
1488 static tree cp_parser_new_placement
1489   (cp_parser *);
1490 static tree cp_parser_new_type_id
1491   (cp_parser *, tree *);
1492 static cp_declarator *cp_parser_new_declarator_opt
1493   (cp_parser *);
1494 static cp_declarator *cp_parser_direct_new_declarator
1495   (cp_parser *);
1496 static tree cp_parser_new_initializer
1497   (cp_parser *);
1498 static tree cp_parser_delete_expression
1499   (cp_parser *);
1500 static tree cp_parser_cast_expression
1501   (cp_parser *, bool, bool);
1502 static tree cp_parser_binary_expression
1503   (cp_parser *, bool);
1504 static tree cp_parser_question_colon_clause
1505   (cp_parser *, tree);
1506 static tree cp_parser_assignment_expression
1507   (cp_parser *, bool);
1508 static enum tree_code cp_parser_assignment_operator_opt
1509   (cp_parser *);
1510 static tree cp_parser_expression
1511   (cp_parser *, bool);
1512 static tree cp_parser_constant_expression
1513   (cp_parser *, bool, bool *);
1514 static tree cp_parser_builtin_offsetof
1515   (cp_parser *);
1516
1517 /* Statements [gram.stmt.stmt]  */
1518
1519 static void cp_parser_statement
1520   (cp_parser *, tree, bool);
1521 static void cp_parser_label_for_labeled_statement
1522   (cp_parser *);
1523 static tree cp_parser_expression_statement
1524   (cp_parser *, tree);
1525 static tree cp_parser_compound_statement
1526   (cp_parser *, tree, bool);
1527 static void cp_parser_statement_seq_opt
1528   (cp_parser *, tree);
1529 static tree cp_parser_selection_statement
1530   (cp_parser *);
1531 static tree cp_parser_condition
1532   (cp_parser *);
1533 static tree cp_parser_iteration_statement
1534   (cp_parser *);
1535 static void cp_parser_for_init_statement
1536   (cp_parser *);
1537 static tree cp_parser_jump_statement
1538   (cp_parser *);
1539 static void cp_parser_declaration_statement
1540   (cp_parser *);
1541
1542 static tree cp_parser_implicitly_scoped_statement
1543   (cp_parser *);
1544 static void cp_parser_already_scoped_statement
1545   (cp_parser *);
1546
1547 /* Declarations [gram.dcl.dcl] */
1548
1549 static void cp_parser_declaration_seq_opt
1550   (cp_parser *);
1551 static void cp_parser_declaration
1552   (cp_parser *);
1553 static void cp_parser_block_declaration
1554   (cp_parser *, bool);
1555 static void cp_parser_simple_declaration
1556   (cp_parser *, bool);
1557 static void cp_parser_decl_specifier_seq
1558   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1559 static tree cp_parser_storage_class_specifier_opt
1560   (cp_parser *);
1561 static tree cp_parser_function_specifier_opt
1562   (cp_parser *, cp_decl_specifier_seq *);
1563 static tree cp_parser_type_specifier
1564   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1565    int *, bool *);
1566 static tree cp_parser_simple_type_specifier
1567   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1568 static tree cp_parser_type_name
1569   (cp_parser *);
1570 static tree cp_parser_elaborated_type_specifier
1571   (cp_parser *, bool, bool);
1572 static tree cp_parser_enum_specifier
1573   (cp_parser *);
1574 static void cp_parser_enumerator_list
1575   (cp_parser *, tree);
1576 static void cp_parser_enumerator_definition
1577   (cp_parser *, tree);
1578 static tree cp_parser_namespace_name
1579   (cp_parser *);
1580 static void cp_parser_namespace_definition
1581   (cp_parser *);
1582 static void cp_parser_namespace_body
1583   (cp_parser *);
1584 static tree cp_parser_qualified_namespace_specifier
1585   (cp_parser *);
1586 static void cp_parser_namespace_alias_definition
1587   (cp_parser *);
1588 static bool cp_parser_using_declaration
1589   (cp_parser *, bool);
1590 static void cp_parser_using_directive
1591   (cp_parser *);
1592 static void cp_parser_asm_definition
1593   (cp_parser *);
1594 static void cp_parser_linkage_specification
1595   (cp_parser *);
1596 static void cp_parser_static_assert
1597   (cp_parser *, bool);
1598
1599 /* Declarators [gram.dcl.decl] */
1600
1601 static tree cp_parser_init_declarator
1602   (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1603 static cp_declarator *cp_parser_declarator
1604   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1605 static cp_declarator *cp_parser_direct_declarator
1606   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1607 static enum tree_code cp_parser_ptr_operator
1608   (cp_parser *, tree *, cp_cv_quals *);
1609 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1610   (cp_parser *);
1611 static tree cp_parser_declarator_id
1612   (cp_parser *, bool);
1613 static tree cp_parser_type_id
1614   (cp_parser *);
1615 static void cp_parser_type_specifier_seq
1616   (cp_parser *, bool, cp_decl_specifier_seq *);
1617 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1618   (cp_parser *);
1619 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1620   (cp_parser *, bool *);
1621 static cp_parameter_declarator *cp_parser_parameter_declaration
1622   (cp_parser *, bool, bool *);
1623 static void cp_parser_function_body
1624   (cp_parser *);
1625 static tree cp_parser_initializer
1626   (cp_parser *, bool *, bool *);
1627 static tree cp_parser_initializer_clause
1628   (cp_parser *, bool *);
1629 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1630   (cp_parser *, bool *);
1631
1632 static bool cp_parser_ctor_initializer_opt_and_function_body
1633   (cp_parser *);
1634
1635 /* Classes [gram.class] */
1636
1637 static tree cp_parser_class_name
1638   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1639 static tree cp_parser_class_specifier
1640   (cp_parser *);
1641 static tree cp_parser_class_head
1642   (cp_parser *, bool *, tree *, tree *);
1643 static enum tag_types cp_parser_class_key
1644   (cp_parser *);
1645 static void cp_parser_member_specification_opt
1646   (cp_parser *);
1647 static void cp_parser_member_declaration
1648   (cp_parser *);
1649 static tree cp_parser_pure_specifier
1650   (cp_parser *);
1651 static tree cp_parser_constant_initializer
1652   (cp_parser *);
1653
1654 /* Derived classes [gram.class.derived] */
1655
1656 static tree cp_parser_base_clause
1657   (cp_parser *);
1658 static tree cp_parser_base_specifier
1659   (cp_parser *);
1660
1661 /* Special member functions [gram.special] */
1662
1663 static tree cp_parser_conversion_function_id
1664   (cp_parser *);
1665 static tree cp_parser_conversion_type_id
1666   (cp_parser *);
1667 static cp_declarator *cp_parser_conversion_declarator_opt
1668   (cp_parser *);
1669 static bool cp_parser_ctor_initializer_opt
1670   (cp_parser *);
1671 static void cp_parser_mem_initializer_list
1672   (cp_parser *);
1673 static tree cp_parser_mem_initializer
1674   (cp_parser *);
1675 static tree cp_parser_mem_initializer_id
1676   (cp_parser *);
1677
1678 /* Overloading [gram.over] */
1679
1680 static tree cp_parser_operator_function_id
1681   (cp_parser *);
1682 static tree cp_parser_operator
1683   (cp_parser *);
1684
1685 /* Templates [gram.temp] */
1686
1687 static void cp_parser_template_declaration
1688   (cp_parser *, bool);
1689 static tree cp_parser_template_parameter_list
1690   (cp_parser *);
1691 static tree cp_parser_template_parameter
1692   (cp_parser *, bool *);
1693 static tree cp_parser_type_parameter
1694   (cp_parser *);
1695 static tree cp_parser_template_id
1696   (cp_parser *, bool, bool, bool);
1697 static tree cp_parser_template_name
1698   (cp_parser *, bool, bool, bool, bool *);
1699 static tree cp_parser_template_argument_list
1700   (cp_parser *);
1701 static tree cp_parser_template_argument
1702   (cp_parser *);
1703 static void cp_parser_explicit_instantiation
1704   (cp_parser *);
1705 static void cp_parser_explicit_specialization
1706   (cp_parser *);
1707
1708 /* Exception handling [gram.exception] */
1709
1710 static tree cp_parser_try_block
1711   (cp_parser *);
1712 static bool cp_parser_function_try_block
1713   (cp_parser *);
1714 static void cp_parser_handler_seq
1715   (cp_parser *);
1716 static void cp_parser_handler
1717   (cp_parser *);
1718 static tree cp_parser_exception_declaration
1719   (cp_parser *);
1720 static tree cp_parser_throw_expression
1721   (cp_parser *);
1722 static tree cp_parser_exception_specification_opt
1723   (cp_parser *);
1724 static tree cp_parser_type_id_list
1725   (cp_parser *);
1726
1727 /* GNU Extensions */
1728
1729 static tree cp_parser_asm_specification_opt
1730   (cp_parser *);
1731 static tree cp_parser_asm_operand_list
1732   (cp_parser *);
1733 static tree cp_parser_asm_clobber_list
1734   (cp_parser *);
1735 static tree cp_parser_attributes_opt
1736   (cp_parser *);
1737 static tree cp_parser_attribute_list
1738   (cp_parser *);
1739 static bool cp_parser_extension_opt
1740   (cp_parser *, int *);
1741 static void cp_parser_label_declaration
1742   (cp_parser *);
1743
1744 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1745 static bool cp_parser_pragma
1746   (cp_parser *, enum pragma_context);
1747
1748 /* Objective-C++ Productions */
1749
1750 static tree cp_parser_objc_message_receiver
1751   (cp_parser *);
1752 static tree cp_parser_objc_message_args
1753   (cp_parser *);
1754 static tree cp_parser_objc_message_expression
1755   (cp_parser *);
1756 static tree cp_parser_objc_encode_expression
1757   (cp_parser *);
1758 static tree cp_parser_objc_defs_expression
1759   (cp_parser *);
1760 static tree cp_parser_objc_protocol_expression
1761   (cp_parser *);
1762 static tree cp_parser_objc_selector_expression
1763   (cp_parser *);
1764 static tree cp_parser_objc_expression
1765   (cp_parser *);
1766 static bool cp_parser_objc_selector_p
1767   (enum cpp_ttype);
1768 static tree cp_parser_objc_selector
1769   (cp_parser *);
1770 static tree cp_parser_objc_protocol_refs_opt
1771   (cp_parser *);
1772 static void cp_parser_objc_declaration
1773   (cp_parser *);
1774 static tree cp_parser_objc_statement
1775   (cp_parser *);
1776
1777 /* Utility Routines */
1778
1779 static tree cp_parser_lookup_name
1780   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1781 static tree cp_parser_lookup_name_simple
1782   (cp_parser *, tree);
1783 static tree cp_parser_maybe_treat_template_as_class
1784   (tree, bool);
1785 static bool cp_parser_check_declarator_template_parameters
1786   (cp_parser *, cp_declarator *);
1787 static bool cp_parser_check_template_parameters
1788   (cp_parser *, unsigned);
1789 static tree cp_parser_simple_cast_expression
1790   (cp_parser *);
1791 static tree cp_parser_global_scope_opt
1792   (cp_parser *, bool);
1793 static bool cp_parser_constructor_declarator_p
1794   (cp_parser *, bool);
1795 static tree cp_parser_function_definition_from_specifiers_and_declarator
1796   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1797 static tree cp_parser_function_definition_after_declarator
1798   (cp_parser *, bool);
1799 static void cp_parser_template_declaration_after_export
1800   (cp_parser *, bool);
1801 static void cp_parser_perform_template_parameter_access_checks
1802   (tree);
1803 static tree cp_parser_single_declaration
1804   (cp_parser *, tree, bool, bool *);
1805 static tree cp_parser_functional_cast
1806   (cp_parser *, tree);
1807 static tree cp_parser_save_member_function_body
1808   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1809 static tree cp_parser_enclosed_template_argument_list
1810   (cp_parser *);
1811 static void cp_parser_save_default_args
1812   (cp_parser *, tree);
1813 static void cp_parser_late_parsing_for_member
1814   (cp_parser *, tree);
1815 static void cp_parser_late_parsing_default_args
1816   (cp_parser *, tree);
1817 static tree cp_parser_sizeof_operand
1818   (cp_parser *, enum rid);
1819 static bool cp_parser_declares_only_class_p
1820   (cp_parser *);
1821 static void cp_parser_set_storage_class
1822   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1823 static void cp_parser_set_decl_spec_type
1824   (cp_decl_specifier_seq *, tree, bool);
1825 static bool cp_parser_friend_p
1826   (const cp_decl_specifier_seq *);
1827 static cp_token *cp_parser_require
1828   (cp_parser *, enum cpp_ttype, const char *);
1829 static cp_token *cp_parser_require_keyword
1830   (cp_parser *, enum rid, const char *);
1831 static bool cp_parser_token_starts_function_definition_p
1832   (cp_token *);
1833 static bool cp_parser_next_token_starts_class_definition_p
1834   (cp_parser *);
1835 static bool cp_parser_next_token_ends_template_argument_p
1836   (cp_parser *);
1837 static bool cp_parser_nth_token_starts_template_argument_list_p
1838   (cp_parser *, size_t);
1839 static enum tag_types cp_parser_token_is_class_key
1840   (cp_token *);
1841 static void cp_parser_check_class_key
1842   (enum tag_types, tree type);
1843 static void cp_parser_check_access_in_redeclaration
1844   (tree type);
1845 static bool cp_parser_optional_template_keyword
1846   (cp_parser *);
1847 static void cp_parser_pre_parsed_nested_name_specifier
1848   (cp_parser *);
1849 static void cp_parser_cache_group
1850   (cp_parser *, enum cpp_ttype, unsigned);
1851 static void cp_parser_parse_tentatively
1852   (cp_parser *);
1853 static void cp_parser_commit_to_tentative_parse
1854   (cp_parser *);
1855 static void cp_parser_abort_tentative_parse
1856   (cp_parser *);
1857 static bool cp_parser_parse_definitely
1858   (cp_parser *);
1859 static inline bool cp_parser_parsing_tentatively
1860   (cp_parser *);
1861 static bool cp_parser_uncommitted_to_tentative_parse_p
1862   (cp_parser *);
1863 static void cp_parser_error
1864   (cp_parser *, const char *);
1865 static void cp_parser_name_lookup_error
1866   (cp_parser *, tree, tree, const char *);
1867 static bool cp_parser_simulate_error
1868   (cp_parser *);
1869 static bool cp_parser_check_type_definition
1870   (cp_parser *);
1871 static void cp_parser_check_for_definition_in_return_type
1872   (cp_declarator *, tree);
1873 static void cp_parser_check_for_invalid_template_id
1874   (cp_parser *, tree);
1875 static bool cp_parser_non_integral_constant_expression
1876   (cp_parser *, const char *);
1877 static void cp_parser_diagnose_invalid_type_name
1878   (cp_parser *, tree, tree);
1879 static bool cp_parser_parse_and_diagnose_invalid_type_name
1880   (cp_parser *);
1881 static int cp_parser_skip_to_closing_parenthesis
1882   (cp_parser *, bool, bool, bool);
1883 static void cp_parser_skip_to_end_of_statement
1884   (cp_parser *);
1885 static void cp_parser_consume_semicolon_at_end_of_statement
1886   (cp_parser *);
1887 static void cp_parser_skip_to_end_of_block_or_statement
1888   (cp_parser *);
1889 static void cp_parser_skip_to_closing_brace
1890   (cp_parser *);
1891 static void cp_parser_skip_to_end_of_template_parameter_list
1892   (cp_parser *);
1893 static void cp_parser_skip_to_pragma_eol
1894   (cp_parser*, cp_token *);
1895 static bool cp_parser_error_occurred
1896   (cp_parser *);
1897 static bool cp_parser_allow_gnu_extensions_p
1898   (cp_parser *);
1899 static bool cp_parser_is_string_literal
1900   (cp_token *);
1901 static bool cp_parser_is_keyword
1902   (cp_token *, enum rid);
1903 static tree cp_parser_make_typename_type
1904   (cp_parser *, tree, tree);
1905
1906 /* Returns nonzero if we are parsing tentatively.  */
1907
1908 static inline bool
1909 cp_parser_parsing_tentatively (cp_parser* parser)
1910 {
1911   return parser->context->next != NULL;
1912 }
1913
1914 /* Returns nonzero if TOKEN is a string literal.  */
1915
1916 static bool
1917 cp_parser_is_string_literal (cp_token* token)
1918 {
1919   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1920 }
1921
1922 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1923
1924 static bool
1925 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1926 {
1927   return token->keyword == keyword;
1928 }
1929
1930 /* If not parsing tentatively, issue a diagnostic of the form
1931       FILE:LINE: MESSAGE before TOKEN
1932    where TOKEN is the next token in the input stream.  MESSAGE
1933    (specified by the caller) is usually of the form "expected
1934    OTHER-TOKEN".  */
1935
1936 static void
1937 cp_parser_error (cp_parser* parser, const char* message)
1938 {
1939   if (!cp_parser_simulate_error (parser))
1940     {
1941       cp_token *token = cp_lexer_peek_token (parser->lexer);
1942       /* This diagnostic makes more sense if it is tagged to the line
1943          of the token we just peeked at.  */
1944       cp_lexer_set_source_position_from_token (token);
1945
1946       if (token->type == CPP_PRAGMA)
1947         {
1948           error ("%<#pragma%> is not allowed here");
1949           cp_parser_skip_to_pragma_eol (parser, token);
1950           return;
1951         }
1952
1953       c_parse_error (message,
1954                      /* Because c_parser_error does not understand
1955                         CPP_KEYWORD, keywords are treated like
1956                         identifiers.  */
1957                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1958                      token->value);
1959     }
1960 }
1961
1962 /* Issue an error about name-lookup failing.  NAME is the
1963    IDENTIFIER_NODE DECL is the result of
1964    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1965    the thing that we hoped to find.  */
1966
1967 static void
1968 cp_parser_name_lookup_error (cp_parser* parser,
1969                              tree name,
1970                              tree decl,
1971                              const char* desired)
1972 {
1973   /* If name lookup completely failed, tell the user that NAME was not
1974      declared.  */
1975   if (decl == error_mark_node)
1976     {
1977       if (parser->scope && parser->scope != global_namespace)
1978         error ("%<%D::%D%> has not been declared",
1979                parser->scope, name);
1980       else if (parser->scope == global_namespace)
1981         error ("%<::%D%> has not been declared", name);
1982       else if (parser->object_scope
1983                && !CLASS_TYPE_P (parser->object_scope))
1984         error ("request for member %qD in non-class type %qT",
1985                name, parser->object_scope);
1986       else if (parser->object_scope)
1987         error ("%<%T::%D%> has not been declared",
1988                parser->object_scope, name);
1989       else
1990         error ("%qD has not been declared", name);
1991     }
1992   else if (parser->scope && parser->scope != global_namespace)
1993     error ("%<%D::%D%> %s", parser->scope, name, desired);
1994   else if (parser->scope == global_namespace)
1995     error ("%<::%D%> %s", name, desired);
1996   else
1997     error ("%qD %s", name, desired);
1998 }
1999
2000 /* If we are parsing tentatively, remember that an error has occurred
2001    during this tentative parse.  Returns true if the error was
2002    simulated; false if a message should be issued by the caller.  */
2003
2004 static bool
2005 cp_parser_simulate_error (cp_parser* parser)
2006 {
2007   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2008     {
2009       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2010       return true;
2011     }
2012   return false;
2013 }
2014
2015 /* Check for repeated decl-specifiers.  */
2016
2017 static void
2018 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2019 {
2020   cp_decl_spec ds;
2021
2022   for (ds = ds_first; ds != ds_last; ++ds)
2023     {
2024       unsigned count = decl_specs->specs[(int)ds];
2025       if (count < 2)
2026         continue;
2027       /* The "long" specifier is a special case because of "long long".  */
2028       if (ds == ds_long)
2029         {
2030           if (count > 2)
2031             error ("%<long long long%> is too long for GCC");
2032           else if (pedantic && !in_system_header && warn_long_long)
2033             pedwarn ("ISO C++ does not support %<long long%>");
2034         }
2035       else if (count > 1)
2036         {
2037           static const char *const decl_spec_names[] = {
2038             "signed",
2039             "unsigned",
2040             "short",
2041             "long",
2042             "const",
2043             "volatile",
2044             "restrict",
2045             "inline",
2046             "virtual",
2047             "explicit",
2048             "friend",
2049             "typedef",
2050             "__complex",
2051             "__thread"
2052           };
2053           error ("duplicate %qs", decl_spec_names[(int)ds]);
2054         }
2055     }
2056 }
2057
2058 /* This function is called when a type is defined.  If type
2059    definitions are forbidden at this point, an error message is
2060    issued.  */
2061
2062 static bool
2063 cp_parser_check_type_definition (cp_parser* parser)
2064 {
2065   /* If types are forbidden here, issue a message.  */
2066   if (parser->type_definition_forbidden_message)
2067     {
2068       /* Use `%s' to print the string in case there are any escape
2069          characters in the message.  */
2070       error ("%s", parser->type_definition_forbidden_message);
2071       return false;
2072     }
2073   return true;
2074 }
2075
2076 /* This function is called when the DECLARATOR is processed.  The TYPE
2077    was a type defined in the decl-specifiers.  If it is invalid to
2078    define a type in the decl-specifiers for DECLARATOR, an error is
2079    issued.  */
2080
2081 static void
2082 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2083                                                tree type)
2084 {
2085   /* [dcl.fct] forbids type definitions in return types.
2086      Unfortunately, it's not easy to know whether or not we are
2087      processing a return type until after the fact.  */
2088   while (declarator
2089          && (declarator->kind == cdk_pointer
2090              || declarator->kind == cdk_reference
2091              || declarator->kind == cdk_ptrmem))
2092     declarator = declarator->declarator;
2093   if (declarator
2094       && declarator->kind == cdk_function)
2095     {
2096       error ("new types may not be defined in a return type");
2097       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2098               type);
2099     }
2100 }
2101
2102 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2103    "<" in any valid C++ program.  If the next token is indeed "<",
2104    issue a message warning the user about what appears to be an
2105    invalid attempt to form a template-id.  */
2106
2107 static void
2108 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2109                                          tree type)
2110 {
2111   cp_token_position start = 0;
2112
2113   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2114     {
2115       if (TYPE_P (type))
2116         error ("%qT is not a template", type);
2117       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2118         error ("%qE is not a template", type);
2119       else
2120         error ("invalid template-id");
2121       /* Remember the location of the invalid "<".  */
2122       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2123         start = cp_lexer_token_position (parser->lexer, true);
2124       /* Consume the "<".  */
2125       cp_lexer_consume_token (parser->lexer);
2126       /* Parse the template arguments.  */
2127       cp_parser_enclosed_template_argument_list (parser);
2128       /* Permanently remove the invalid template arguments so that
2129          this error message is not issued again.  */
2130       if (start)
2131         cp_lexer_purge_tokens_after (parser->lexer, start);
2132     }
2133 }
2134
2135 /* If parsing an integral constant-expression, issue an error message
2136    about the fact that THING appeared and return true.  Otherwise,
2137    return false.  In either case, set
2138    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2139
2140 static bool
2141 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2142                                             const char *thing)
2143 {
2144   parser->non_integral_constant_expression_p = true;
2145   if (parser->integral_constant_expression_p)
2146     {
2147       if (!parser->allow_non_integral_constant_expression_p)
2148         {
2149           error ("%s cannot appear in a constant-expression", thing);
2150           return true;
2151         }
2152     }
2153   return false;
2154 }
2155
2156 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2157    qualifying scope (or NULL, if none) for ID.  This function commits
2158    to the current active tentative parse, if any.  (Otherwise, the
2159    problematic construct might be encountered again later, resulting
2160    in duplicate error messages.)  */
2161
2162 static void
2163 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2164 {
2165   tree decl, old_scope;
2166   /* Try to lookup the identifier.  */
2167   old_scope = parser->scope;
2168   parser->scope = scope;
2169   decl = cp_parser_lookup_name_simple (parser, id);
2170   parser->scope = old_scope;
2171   /* If the lookup found a template-name, it means that the user forgot
2172   to specify an argument list. Emit a useful error message.  */
2173   if (TREE_CODE (decl) == TEMPLATE_DECL)
2174     error ("invalid use of template-name %qE without an argument list", decl);
2175   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2176     error ("invalid use of destructor %qD as a type", id);
2177   else if (TREE_CODE (decl) == TYPE_DECL)
2178     /* Something like 'unsigned A a;'  */
2179     error ("invalid combination of multiple type-specifiers");
2180   else if (!parser->scope)
2181     {
2182       /* Issue an error message.  */
2183       error ("%qE does not name a type", id);
2184       /* If we're in a template class, it's possible that the user was
2185          referring to a type from a base class.  For example:
2186
2187            template <typename T> struct A { typedef T X; };
2188            template <typename T> struct B : public A<T> { X x; };
2189
2190          The user should have said "typename A<T>::X".  */
2191       if (processing_template_decl && current_class_type
2192           && TYPE_BINFO (current_class_type))
2193         {
2194           tree b;
2195
2196           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2197                b;
2198                b = TREE_CHAIN (b))
2199             {
2200               tree base_type = BINFO_TYPE (b);
2201               if (CLASS_TYPE_P (base_type)
2202                   && dependent_type_p (base_type))
2203                 {
2204                   tree field;
2205                   /* Go from a particular instantiation of the
2206                      template (which will have an empty TYPE_FIELDs),
2207                      to the main version.  */
2208                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2209                   for (field = TYPE_FIELDS (base_type);
2210                        field;
2211                        field = TREE_CHAIN (field))
2212                     if (TREE_CODE (field) == TYPE_DECL
2213                         && DECL_NAME (field) == id)
2214                       {
2215                         inform ("(perhaps %<typename %T::%E%> was intended)",
2216                                 BINFO_TYPE (b), id);
2217                         break;
2218                       }
2219                   if (field)
2220                     break;
2221                 }
2222             }
2223         }
2224     }
2225   /* Here we diagnose qualified-ids where the scope is actually correct,
2226      but the identifier does not resolve to a valid type name.  */
2227   else if (parser->scope != error_mark_node)
2228     {
2229       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2230         error ("%qE in namespace %qE does not name a type",
2231                id, parser->scope);
2232       else if (TYPE_P (parser->scope))
2233         error ("%qE in class %qT does not name a type", id, parser->scope);
2234       else
2235         gcc_unreachable ();
2236     }
2237   cp_parser_commit_to_tentative_parse (parser);
2238 }
2239
2240 /* Check for a common situation where a type-name should be present,
2241    but is not, and issue a sensible error message.  Returns true if an
2242    invalid type-name was detected.
2243
2244    The situation handled by this function are variable declarations of the
2245    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2246    Usually, `ID' should name a type, but if we got here it means that it
2247    does not. We try to emit the best possible error message depending on
2248    how exactly the id-expression looks like.  */
2249
2250 static bool
2251 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2252 {
2253   tree id;
2254
2255   cp_parser_parse_tentatively (parser);
2256   id = cp_parser_id_expression (parser,
2257                                 /*template_keyword_p=*/false,
2258                                 /*check_dependency_p=*/true,
2259                                 /*template_p=*/NULL,
2260                                 /*declarator_p=*/true,
2261                                 /*optional_p=*/false);
2262   /* After the id-expression, there should be a plain identifier,
2263      otherwise this is not a simple variable declaration. Also, if
2264      the scope is dependent, we cannot do much.  */
2265   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2266       || (parser->scope && TYPE_P (parser->scope)
2267           && dependent_type_p (parser->scope)))
2268     {
2269       cp_parser_abort_tentative_parse (parser);
2270       return false;
2271     }
2272   if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2273     return false;
2274
2275   /* Emit a diagnostic for the invalid type.  */
2276   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2277   /* Skip to the end of the declaration; there's no point in
2278      trying to process it.  */
2279   cp_parser_skip_to_end_of_block_or_statement (parser);
2280   return true;
2281 }
2282
2283 /* Consume tokens up to, and including, the next non-nested closing `)'.
2284    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2285    are doing error recovery. Returns -1 if OR_COMMA is true and we
2286    found an unnested comma.  */
2287
2288 static int
2289 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2290                                        bool recovering,
2291                                        bool or_comma,
2292                                        bool consume_paren)
2293 {
2294   unsigned paren_depth = 0;
2295   unsigned brace_depth = 0;
2296
2297   if (recovering && !or_comma
2298       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2299     return 0;
2300
2301   while (true)
2302     {
2303       cp_token * token = cp_lexer_peek_token (parser->lexer);
2304
2305       switch (token->type)
2306         {
2307         case CPP_EOF:
2308         case CPP_PRAGMA_EOL:
2309           /* If we've run out of tokens, then there is no closing `)'.  */
2310           return 0;
2311
2312         case CPP_SEMICOLON:
2313           /* This matches the processing in skip_to_end_of_statement.  */
2314           if (!brace_depth)
2315             return 0;
2316           break;
2317
2318         case CPP_OPEN_BRACE:
2319           ++brace_depth;
2320           break;
2321         case CPP_CLOSE_BRACE:
2322           if (!brace_depth--)
2323             return 0;
2324           break;
2325
2326         case CPP_COMMA:
2327           if (recovering && or_comma && !brace_depth && !paren_depth)
2328             return -1;
2329           break;
2330
2331         case CPP_OPEN_PAREN:
2332           if (!brace_depth)
2333             ++paren_depth;
2334           break;
2335
2336         case CPP_CLOSE_PAREN:
2337           if (!brace_depth && !paren_depth--)
2338             {
2339               if (consume_paren)
2340                 cp_lexer_consume_token (parser->lexer);
2341               return 1;
2342             }
2343           break;
2344
2345         default:
2346           break;
2347         }
2348
2349       /* Consume the token.  */
2350       cp_lexer_consume_token (parser->lexer);
2351     }
2352 }
2353
2354 /* Consume tokens until we reach the end of the current statement.
2355    Normally, that will be just before consuming a `;'.  However, if a
2356    non-nested `}' comes first, then we stop before consuming that.  */
2357
2358 static void
2359 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2360 {
2361   unsigned nesting_depth = 0;
2362
2363   while (true)
2364     {
2365       cp_token *token = cp_lexer_peek_token (parser->lexer);
2366
2367       switch (token->type)
2368         {
2369         case CPP_EOF:
2370         case CPP_PRAGMA_EOL:
2371           /* If we've run out of tokens, stop.  */
2372           return;
2373
2374         case CPP_SEMICOLON:
2375           /* If the next token is a `;', we have reached the end of the
2376              statement.  */
2377           if (!nesting_depth)
2378             return;
2379           break;
2380
2381         case CPP_CLOSE_BRACE:
2382           /* If this is a non-nested '}', stop before consuming it.
2383              That way, when confronted with something like:
2384
2385                { 3 + }
2386
2387              we stop before consuming the closing '}', even though we
2388              have not yet reached a `;'.  */
2389           if (nesting_depth == 0)
2390             return;
2391
2392           /* If it is the closing '}' for a block that we have
2393              scanned, stop -- but only after consuming the token.
2394              That way given:
2395
2396                 void f g () { ... }
2397                 typedef int I;
2398
2399              we will stop after the body of the erroneously declared
2400              function, but before consuming the following `typedef'
2401              declaration.  */
2402           if (--nesting_depth == 0)
2403             {
2404               cp_lexer_consume_token (parser->lexer);
2405               return;
2406             }
2407
2408         case CPP_OPEN_BRACE:
2409           ++nesting_depth;
2410           break;
2411
2412         default:
2413           break;
2414         }
2415
2416       /* Consume the token.  */
2417       cp_lexer_consume_token (parser->lexer);
2418     }
2419 }
2420
2421 /* This function is called at the end of a statement or declaration.
2422    If the next token is a semicolon, it is consumed; otherwise, error
2423    recovery is attempted.  */
2424
2425 static void
2426 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2427 {
2428   /* Look for the trailing `;'.  */
2429   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2430     {
2431       /* If there is additional (erroneous) input, skip to the end of
2432          the statement.  */
2433       cp_parser_skip_to_end_of_statement (parser);
2434       /* If the next token is now a `;', consume it.  */
2435       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2436         cp_lexer_consume_token (parser->lexer);
2437     }
2438 }
2439
2440 /* Skip tokens until we have consumed an entire block, or until we
2441    have consumed a non-nested `;'.  */
2442
2443 static void
2444 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2445 {
2446   int nesting_depth = 0;
2447
2448   while (nesting_depth >= 0)
2449     {
2450       cp_token *token = cp_lexer_peek_token (parser->lexer);
2451
2452       switch (token->type)
2453         {
2454         case CPP_EOF:
2455         case CPP_PRAGMA_EOL:
2456           /* If we've run out of tokens, stop.  */
2457           return;
2458
2459         case CPP_SEMICOLON:
2460           /* Stop if this is an unnested ';'. */
2461           if (!nesting_depth)
2462             nesting_depth = -1;
2463           break;
2464
2465         case CPP_CLOSE_BRACE:
2466           /* Stop if this is an unnested '}', or closes the outermost
2467              nesting level.  */
2468           nesting_depth--;
2469           if (!nesting_depth)
2470             nesting_depth = -1;
2471           break;
2472
2473         case CPP_OPEN_BRACE:
2474           /* Nest. */
2475           nesting_depth++;
2476           break;
2477
2478         default:
2479           break;
2480         }
2481
2482       /* Consume the token.  */
2483       cp_lexer_consume_token (parser->lexer);
2484     }
2485 }
2486
2487 /* Skip tokens until a non-nested closing curly brace is the next
2488    token.  */
2489
2490 static void
2491 cp_parser_skip_to_closing_brace (cp_parser *parser)
2492 {
2493   unsigned nesting_depth = 0;
2494
2495   while (true)
2496     {
2497       cp_token *token = cp_lexer_peek_token (parser->lexer);
2498
2499       switch (token->type)
2500         {
2501         case CPP_EOF:
2502         case CPP_PRAGMA_EOL:
2503           /* If we've run out of tokens, stop.  */
2504           return;
2505
2506         case CPP_CLOSE_BRACE:
2507           /* If the next token is a non-nested `}', then we have reached
2508              the end of the current block.  */
2509           if (nesting_depth-- == 0)
2510             return;
2511           break;
2512
2513         case CPP_OPEN_BRACE:
2514           /* If it the next token is a `{', then we are entering a new
2515              block.  Consume the entire block.  */
2516           ++nesting_depth;
2517           break;
2518
2519         default:
2520           break;
2521         }
2522
2523       /* Consume the token.  */
2524       cp_lexer_consume_token (parser->lexer);
2525     }
2526 }
2527
2528 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2529    parameter is the PRAGMA token, allowing us to purge the entire pragma
2530    sequence.  */
2531
2532 static void
2533 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2534 {
2535   cp_token *token;
2536
2537   parser->lexer->in_pragma = false;
2538
2539   do
2540     token = cp_lexer_consume_token (parser->lexer);
2541   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2542
2543   /* Ensure that the pragma is not parsed again.  */
2544   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2545 }
2546
2547 /* Require pragma end of line, resyncing with it as necessary.  The
2548    arguments are as for cp_parser_skip_to_pragma_eol.  */
2549
2550 static void
2551 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2552 {
2553   parser->lexer->in_pragma = false;
2554   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2555     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2556 }
2557
2558 /* This is a simple wrapper around make_typename_type. When the id is
2559    an unresolved identifier node, we can provide a superior diagnostic
2560    using cp_parser_diagnose_invalid_type_name.  */
2561
2562 static tree
2563 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2564 {
2565   tree result;
2566   if (TREE_CODE (id) == IDENTIFIER_NODE)
2567     {
2568       result = make_typename_type (scope, id, typename_type,
2569                                    /*complain=*/tf_none);
2570       if (result == error_mark_node)
2571         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2572       return result;
2573     }
2574   return make_typename_type (scope, id, typename_type, tf_error);
2575 }
2576
2577
2578 /* Create a new C++ parser.  */
2579
2580 static cp_parser *
2581 cp_parser_new (void)
2582 {
2583   cp_parser *parser;
2584   cp_lexer *lexer;
2585   unsigned i;
2586
2587   /* cp_lexer_new_main is called before calling ggc_alloc because
2588      cp_lexer_new_main might load a PCH file.  */
2589   lexer = cp_lexer_new_main ();
2590
2591   /* Initialize the binops_by_token so that we can get the tree
2592      directly from the token.  */
2593   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2594     binops_by_token[binops[i].token_type] = binops[i];
2595
2596   parser = GGC_CNEW (cp_parser);
2597   parser->lexer = lexer;
2598   parser->context = cp_parser_context_new (NULL);
2599
2600   /* For now, we always accept GNU extensions.  */
2601   parser->allow_gnu_extensions_p = 1;
2602
2603   /* The `>' token is a greater-than operator, not the end of a
2604      template-id.  */
2605   parser->greater_than_is_operator_p = true;
2606
2607   parser->default_arg_ok_p = true;
2608
2609   /* We are not parsing a constant-expression.  */
2610   parser->integral_constant_expression_p = false;
2611   parser->allow_non_integral_constant_expression_p = false;
2612   parser->non_integral_constant_expression_p = false;
2613
2614   /* Local variable names are not forbidden.  */
2615   parser->local_variables_forbidden_p = false;
2616
2617   /* We are not processing an `extern "C"' declaration.  */
2618   parser->in_unbraced_linkage_specification_p = false;
2619
2620   /* We are not processing a declarator.  */
2621   parser->in_declarator_p = false;
2622
2623   /* We are not processing a template-argument-list.  */
2624   parser->in_template_argument_list_p = false;
2625
2626   /* We are not in an iteration statement.  */
2627   parser->in_statement = 0;
2628
2629   /* We are not in a switch statement.  */
2630   parser->in_switch_statement_p = false;
2631
2632   /* We are not parsing a type-id inside an expression.  */
2633   parser->in_type_id_in_expr_p = false;
2634
2635   /* Declarations aren't implicitly extern "C".  */
2636   parser->implicit_extern_c = false;
2637
2638   /* String literals should be translated to the execution character set.  */
2639   parser->translate_strings_p = true;
2640
2641   /* We are not parsing a function body.  */
2642   parser->in_function_body = false;
2643
2644   /* The unparsed function queue is empty.  */
2645   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2646
2647   /* There are no classes being defined.  */
2648   parser->num_classes_being_defined = 0;
2649
2650   /* No template parameters apply.  */
2651   parser->num_template_parameter_lists = 0;
2652
2653   return parser;
2654 }
2655
2656 /* Create a cp_lexer structure which will emit the tokens in CACHE
2657    and push it onto the parser's lexer stack.  This is used for delayed
2658    parsing of in-class method bodies and default arguments, and should
2659    not be confused with tentative parsing.  */
2660 static void
2661 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2662 {
2663   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2664   lexer->next = parser->lexer;
2665   parser->lexer = lexer;
2666
2667   /* Move the current source position to that of the first token in the
2668      new lexer.  */
2669   cp_lexer_set_source_position_from_token (lexer->next_token);
2670 }
2671
2672 /* Pop the top lexer off the parser stack.  This is never used for the
2673    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2674 static void
2675 cp_parser_pop_lexer (cp_parser *parser)
2676 {
2677   cp_lexer *lexer = parser->lexer;
2678   parser->lexer = lexer->next;
2679   cp_lexer_destroy (lexer);
2680
2681   /* Put the current source position back where it was before this
2682      lexer was pushed.  */
2683   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2684 }
2685
2686 /* Lexical conventions [gram.lex]  */
2687
2688 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2689    identifier.  */
2690
2691 static tree
2692 cp_parser_identifier (cp_parser* parser)
2693 {
2694   cp_token *token;
2695
2696   /* Look for the identifier.  */
2697   token = cp_parser_require (parser, CPP_NAME, "identifier");
2698   /* Return the value.  */
2699   return token ? token->value : error_mark_node;
2700 }
2701
2702 /* Parse a sequence of adjacent string constants.  Returns a
2703    TREE_STRING representing the combined, nul-terminated string
2704    constant.  If TRANSLATE is true, translate the string to the
2705    execution character set.  If WIDE_OK is true, a wide string is
2706    invalid here.
2707
2708    C++98 [lex.string] says that if a narrow string literal token is
2709    adjacent to a wide string literal token, the behavior is undefined.
2710    However, C99 6.4.5p4 says that this results in a wide string literal.
2711    We follow C99 here, for consistency with the C front end.
2712
2713    This code is largely lifted from lex_string() in c-lex.c.
2714
2715    FUTURE: ObjC++ will need to handle @-strings here.  */
2716 static tree
2717 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2718 {
2719   tree value;
2720   bool wide = false;
2721   size_t count;
2722   struct obstack str_ob;
2723   cpp_string str, istr, *strs;
2724   cp_token *tok;
2725
2726   tok = cp_lexer_peek_token (parser->lexer);
2727   if (!cp_parser_is_string_literal (tok))
2728     {
2729       cp_parser_error (parser, "expected string-literal");
2730       return error_mark_node;
2731     }
2732
2733   /* Try to avoid the overhead of creating and destroying an obstack
2734      for the common case of just one string.  */
2735   if (!cp_parser_is_string_literal
2736       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2737     {
2738       cp_lexer_consume_token (parser->lexer);
2739
2740       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2741       str.len = TREE_STRING_LENGTH (tok->value);
2742       count = 1;
2743       if (tok->type == CPP_WSTRING)
2744         wide = true;
2745
2746       strs = &str;
2747     }
2748   else
2749     {
2750       gcc_obstack_init (&str_ob);
2751       count = 0;
2752
2753       do
2754         {
2755           cp_lexer_consume_token (parser->lexer);
2756           count++;
2757           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2758           str.len = TREE_STRING_LENGTH (tok->value);
2759           if (tok->type == CPP_WSTRING)
2760             wide = true;
2761
2762           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2763
2764           tok = cp_lexer_peek_token (parser->lexer);
2765         }
2766       while (cp_parser_is_string_literal (tok));
2767
2768       strs = (cpp_string *) obstack_finish (&str_ob);
2769     }
2770
2771   if (wide && !wide_ok)
2772     {
2773       cp_parser_error (parser, "a wide string is invalid in this context");
2774       wide = false;
2775     }
2776
2777   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2778       (parse_in, strs, count, &istr, wide))
2779     {
2780       value = build_string (istr.len, (char *)istr.text);
2781       free ((void *)istr.text);
2782
2783       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2784       value = fix_string_type (value);
2785     }
2786   else
2787     /* cpp_interpret_string has issued an error.  */
2788     value = error_mark_node;
2789
2790   if (count > 1)
2791     obstack_free (&str_ob, 0);
2792
2793   return value;
2794 }
2795
2796
2797 /* Basic concepts [gram.basic]  */
2798
2799 /* Parse a translation-unit.
2800
2801    translation-unit:
2802      declaration-seq [opt]
2803
2804    Returns TRUE if all went well.  */
2805
2806 static bool
2807 cp_parser_translation_unit (cp_parser* parser)
2808 {
2809   /* The address of the first non-permanent object on the declarator
2810      obstack.  */
2811   static void *declarator_obstack_base;
2812
2813   bool success;
2814
2815   /* Create the declarator obstack, if necessary.  */
2816   if (!cp_error_declarator)
2817     {
2818       gcc_obstack_init (&declarator_obstack);
2819       /* Create the error declarator.  */
2820       cp_error_declarator = make_declarator (cdk_error);
2821       /* Create the empty parameter list.  */
2822       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2823       /* Remember where the base of the declarator obstack lies.  */
2824       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2825     }
2826
2827   cp_parser_declaration_seq_opt (parser);
2828
2829   /* If there are no tokens left then all went well.  */
2830   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2831     {
2832       /* Get rid of the token array; we don't need it any more.  */
2833       cp_lexer_destroy (parser->lexer);
2834       parser->lexer = NULL;
2835
2836       /* This file might have been a context that's implicitly extern
2837          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2838       if (parser->implicit_extern_c)
2839         {
2840           pop_lang_context ();
2841           parser->implicit_extern_c = false;
2842         }
2843
2844       /* Finish up.  */
2845       finish_translation_unit ();
2846
2847       success = true;
2848     }
2849   else
2850     {
2851       cp_parser_error (parser, "expected declaration");
2852       success = false;
2853     }
2854
2855   /* Make sure the declarator obstack was fully cleaned up.  */
2856   gcc_assert (obstack_next_free (&declarator_obstack)
2857               == declarator_obstack_base);
2858
2859   /* All went well.  */
2860   return success;
2861 }
2862
2863 /* Expressions [gram.expr] */
2864
2865 /* Parse a primary-expression.
2866
2867    primary-expression:
2868      literal
2869      this
2870      ( expression )
2871      id-expression
2872
2873    GNU Extensions:
2874
2875    primary-expression:
2876      ( compound-statement )
2877      __builtin_va_arg ( assignment-expression , type-id )
2878      __builtin_offsetof ( type-id , offsetof-expression )
2879
2880    Objective-C++ Extension:
2881
2882    primary-expression:
2883      objc-expression
2884
2885    literal:
2886      __null
2887
2888    ADDRESS_P is true iff this expression was immediately preceded by
2889    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2890    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2891    true iff this expression is a template argument.
2892
2893    Returns a representation of the expression.  Upon return, *IDK
2894    indicates what kind of id-expression (if any) was present.  */
2895
2896 static tree
2897 cp_parser_primary_expression (cp_parser *parser,
2898                               bool address_p,
2899                               bool cast_p,
2900                               bool template_arg_p,
2901                               cp_id_kind *idk)
2902 {
2903   cp_token *token;
2904
2905   /* Assume the primary expression is not an id-expression.  */
2906   *idk = CP_ID_KIND_NONE;
2907
2908   /* Peek at the next token.  */
2909   token = cp_lexer_peek_token (parser->lexer);
2910   switch (token->type)
2911     {
2912       /* literal:
2913            integer-literal
2914            character-literal
2915            floating-literal
2916            string-literal
2917            boolean-literal  */
2918     case CPP_CHAR:
2919     case CPP_WCHAR:
2920     case CPP_NUMBER:
2921       token = cp_lexer_consume_token (parser->lexer);
2922       /* Floating-point literals are only allowed in an integral
2923          constant expression if they are cast to an integral or
2924          enumeration type.  */
2925       if (TREE_CODE (token->value) == REAL_CST
2926           && parser->integral_constant_expression_p
2927           && pedantic)
2928         {
2929           /* CAST_P will be set even in invalid code like "int(2.7 +
2930              ...)".   Therefore, we have to check that the next token
2931              is sure to end the cast.  */
2932           if (cast_p)
2933             {
2934               cp_token *next_token;
2935
2936               next_token = cp_lexer_peek_token (parser->lexer);
2937               if (/* The comma at the end of an
2938                      enumerator-definition.  */
2939                   next_token->type != CPP_COMMA
2940                   /* The curly brace at the end of an enum-specifier.  */
2941                   && next_token->type != CPP_CLOSE_BRACE
2942                   /* The end of a statement.  */
2943                   && next_token->type != CPP_SEMICOLON
2944                   /* The end of the cast-expression.  */
2945                   && next_token->type != CPP_CLOSE_PAREN
2946                   /* The end of an array bound.  */
2947                   && next_token->type != CPP_CLOSE_SQUARE
2948                   /* The closing ">" in a template-argument-list.  */
2949                   && (next_token->type != CPP_GREATER
2950                       || parser->greater_than_is_operator_p))
2951                 cast_p = false;
2952             }
2953
2954           /* If we are within a cast, then the constraint that the
2955              cast is to an integral or enumeration type will be
2956              checked at that point.  If we are not within a cast, then
2957              this code is invalid.  */
2958           if (!cast_p)
2959             cp_parser_non_integral_constant_expression
2960               (parser, "floating-point literal");
2961         }
2962       return token->value;
2963
2964     case CPP_STRING:
2965     case CPP_WSTRING:
2966       /* ??? Should wide strings be allowed when parser->translate_strings_p
2967          is false (i.e. in attributes)?  If not, we can kill the third
2968          argument to cp_parser_string_literal.  */
2969       return cp_parser_string_literal (parser,
2970                                        parser->translate_strings_p,
2971                                        true);
2972
2973     case CPP_OPEN_PAREN:
2974       {
2975         tree expr;
2976         bool saved_greater_than_is_operator_p;
2977
2978         /* Consume the `('.  */
2979         cp_lexer_consume_token (parser->lexer);
2980         /* Within a parenthesized expression, a `>' token is always
2981            the greater-than operator.  */
2982         saved_greater_than_is_operator_p
2983           = parser->greater_than_is_operator_p;
2984         parser->greater_than_is_operator_p = true;
2985         /* If we see `( { ' then we are looking at the beginning of
2986            a GNU statement-expression.  */
2987         if (cp_parser_allow_gnu_extensions_p (parser)
2988             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2989           {
2990             /* Statement-expressions are not allowed by the standard.  */
2991             if (pedantic)
2992               pedwarn ("ISO C++ forbids braced-groups within expressions");
2993
2994             /* And they're not allowed outside of a function-body; you
2995                cannot, for example, write:
2996
2997                  int i = ({ int j = 3; j + 1; });
2998
2999                at class or namespace scope.  */
3000             if (!parser->in_function_body)
3001               error ("statement-expressions are allowed only inside functions");
3002             /* Start the statement-expression.  */
3003             expr = begin_stmt_expr ();
3004             /* Parse the compound-statement.  */
3005             cp_parser_compound_statement (parser, expr, false);
3006             /* Finish up.  */
3007             expr = finish_stmt_expr (expr, false);
3008           }
3009         else
3010           {
3011             /* Parse the parenthesized expression.  */
3012             expr = cp_parser_expression (parser, cast_p);
3013             /* Let the front end know that this expression was
3014                enclosed in parentheses. This matters in case, for
3015                example, the expression is of the form `A::B', since
3016                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3017                not.  */
3018             finish_parenthesized_expr (expr);
3019           }
3020         /* The `>' token might be the end of a template-id or
3021            template-parameter-list now.  */
3022         parser->greater_than_is_operator_p
3023           = saved_greater_than_is_operator_p;
3024         /* Consume the `)'.  */
3025         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3026           cp_parser_skip_to_end_of_statement (parser);
3027
3028         return expr;
3029       }
3030
3031     case CPP_KEYWORD:
3032       switch (token->keyword)
3033         {
3034           /* These two are the boolean literals.  */
3035         case RID_TRUE:
3036           cp_lexer_consume_token (parser->lexer);
3037           return boolean_true_node;
3038         case RID_FALSE:
3039           cp_lexer_consume_token (parser->lexer);
3040           return boolean_false_node;
3041
3042           /* The `__null' literal.  */
3043         case RID_NULL:
3044           cp_lexer_consume_token (parser->lexer);
3045           return null_node;
3046
3047           /* Recognize the `this' keyword.  */
3048         case RID_THIS:
3049           cp_lexer_consume_token (parser->lexer);
3050           if (parser->local_variables_forbidden_p)
3051             {
3052               error ("%<this%> may not be used in this context");
3053               return error_mark_node;
3054             }
3055           /* Pointers cannot appear in constant-expressions.  */
3056           if (cp_parser_non_integral_constant_expression (parser,
3057                                                           "`this'"))
3058             return error_mark_node;
3059           return finish_this_expr ();
3060
3061           /* The `operator' keyword can be the beginning of an
3062              id-expression.  */
3063         case RID_OPERATOR:
3064           goto id_expression;
3065
3066         case RID_FUNCTION_NAME:
3067         case RID_PRETTY_FUNCTION_NAME:
3068         case RID_C99_FUNCTION_NAME:
3069           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3070              __func__ are the names of variables -- but they are
3071              treated specially.  Therefore, they are handled here,
3072              rather than relying on the generic id-expression logic
3073              below.  Grammatically, these names are id-expressions.
3074
3075              Consume the token.  */
3076           token = cp_lexer_consume_token (parser->lexer);
3077           /* Look up the name.  */
3078           return finish_fname (token->value);
3079
3080         case RID_VA_ARG:
3081           {
3082             tree expression;
3083             tree type;
3084
3085             /* The `__builtin_va_arg' construct is used to handle
3086                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3087             cp_lexer_consume_token (parser->lexer);
3088             /* Look for the opening `('.  */
3089             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3090             /* Now, parse the assignment-expression.  */
3091             expression = cp_parser_assignment_expression (parser,
3092                                                           /*cast_p=*/false);
3093             /* Look for the `,'.  */
3094             cp_parser_require (parser, CPP_COMMA, "`,'");
3095             /* Parse the type-id.  */
3096             type = cp_parser_type_id (parser);
3097             /* Look for the closing `)'.  */
3098             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3099             /* Using `va_arg' in a constant-expression is not
3100                allowed.  */
3101             if (cp_parser_non_integral_constant_expression (parser,
3102                                                             "`va_arg'"))
3103               return error_mark_node;
3104             return build_x_va_arg (expression, type);
3105           }
3106
3107         case RID_OFFSETOF:
3108           return cp_parser_builtin_offsetof (parser);
3109
3110           /* Objective-C++ expressions.  */
3111         case RID_AT_ENCODE:
3112         case RID_AT_PROTOCOL:
3113         case RID_AT_SELECTOR:
3114           return cp_parser_objc_expression (parser);
3115
3116         default:
3117           cp_parser_error (parser, "expected primary-expression");
3118           return error_mark_node;
3119         }
3120
3121       /* An id-expression can start with either an identifier, a
3122          `::' as the beginning of a qualified-id, or the "operator"
3123          keyword.  */
3124     case CPP_NAME:
3125     case CPP_SCOPE:
3126     case CPP_TEMPLATE_ID:
3127     case CPP_NESTED_NAME_SPECIFIER:
3128       {
3129         tree id_expression;
3130         tree decl;
3131         const char *error_msg;
3132         bool template_p;
3133         bool done;
3134
3135       id_expression:
3136         /* Parse the id-expression.  */
3137         id_expression
3138           = cp_parser_id_expression (parser,
3139                                      /*template_keyword_p=*/false,
3140                                      /*check_dependency_p=*/true,
3141                                      &template_p,
3142                                      /*declarator_p=*/false,
3143                                      /*optional_p=*/false);
3144         if (id_expression == error_mark_node)
3145           return error_mark_node;
3146         token = cp_lexer_peek_token (parser->lexer);
3147         done = (token->type != CPP_OPEN_SQUARE
3148                 && token->type != CPP_OPEN_PAREN
3149                 && token->type != CPP_DOT
3150                 && token->type != CPP_DEREF
3151                 && token->type != CPP_PLUS_PLUS
3152                 && token->type != CPP_MINUS_MINUS);
3153         /* If we have a template-id, then no further lookup is
3154            required.  If the template-id was for a template-class, we
3155            will sometimes have a TYPE_DECL at this point.  */
3156         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3157                  || TREE_CODE (id_expression) == TYPE_DECL)
3158           decl = id_expression;
3159         /* Look up the name.  */
3160         else
3161           {
3162             tree ambiguous_decls;
3163
3164             decl = cp_parser_lookup_name (parser, id_expression,
3165                                           none_type,
3166                                           template_p,
3167                                           /*is_namespace=*/false,
3168                                           /*check_dependency=*/true,
3169                                           &ambiguous_decls);
3170             /* If the lookup was ambiguous, an error will already have
3171                been issued.  */
3172             if (ambiguous_decls)
3173               return error_mark_node;
3174
3175             /* In Objective-C++, an instance variable (ivar) may be preferred
3176                to whatever cp_parser_lookup_name() found.  */
3177             decl = objc_lookup_ivar (decl, id_expression);
3178
3179             /* If name lookup gives us a SCOPE_REF, then the
3180                qualifying scope was dependent.  */
3181             if (TREE_CODE (decl) == SCOPE_REF)
3182               return decl;
3183             /* Check to see if DECL is a local variable in a context
3184                where that is forbidden.  */
3185             if (parser->local_variables_forbidden_p
3186                 && local_variable_p (decl))
3187               {
3188                 /* It might be that we only found DECL because we are
3189                    trying to be generous with pre-ISO scoping rules.
3190                    For example, consider:
3191
3192                      int i;
3193                      void g() {
3194                        for (int i = 0; i < 10; ++i) {}
3195                        extern void f(int j = i);
3196                      }
3197
3198                    Here, name look up will originally find the out
3199                    of scope `i'.  We need to issue a warning message,
3200                    but then use the global `i'.  */
3201                 decl = check_for_out_of_scope_variable (decl);
3202                 if (local_variable_p (decl))
3203                   {
3204                     error ("local variable %qD may not appear in this context",
3205                            decl);
3206                     return error_mark_node;
3207                   }
3208               }
3209           }
3210
3211         decl = (finish_id_expression
3212                 (id_expression, decl, parser->scope,
3213                  idk,
3214                  parser->integral_constant_expression_p,
3215                  parser->allow_non_integral_constant_expression_p,
3216                  &parser->non_integral_constant_expression_p,
3217                  template_p, done, address_p,
3218                  template_arg_p,
3219                  &error_msg));
3220         if (error_msg)
3221           cp_parser_error (parser, error_msg);
3222         return decl;
3223       }
3224
3225       /* Anything else is an error.  */
3226     default:
3227       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3228       if (c_dialect_objc ()
3229           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3230         return cp_parser_objc_expression (parser);
3231
3232       cp_parser_error (parser, "expected primary-expression");
3233       return error_mark_node;
3234     }
3235 }
3236
3237 /* Parse an id-expression.
3238
3239    id-expression:
3240      unqualified-id
3241      qualified-id
3242
3243    qualified-id:
3244      :: [opt] nested-name-specifier template [opt] unqualified-id
3245      :: identifier
3246      :: operator-function-id
3247      :: template-id
3248
3249    Return a representation of the unqualified portion of the
3250    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3251    a `::' or nested-name-specifier.
3252
3253    Often, if the id-expression was a qualified-id, the caller will
3254    want to make a SCOPE_REF to represent the qualified-id.  This
3255    function does not do this in order to avoid wastefully creating
3256    SCOPE_REFs when they are not required.
3257
3258    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3259    `template' keyword.
3260
3261    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3262    uninstantiated templates.
3263
3264    If *TEMPLATE_P is non-NULL, it is set to true iff the
3265    `template' keyword is used to explicitly indicate that the entity
3266    named is a template.
3267
3268    If DECLARATOR_P is true, the id-expression is appearing as part of
3269    a declarator, rather than as part of an expression.  */
3270
3271 static tree
3272 cp_parser_id_expression (cp_parser *parser,
3273                          bool template_keyword_p,
3274                          bool check_dependency_p,
3275                          bool *template_p,
3276                          bool declarator_p,
3277                          bool optional_p)
3278 {
3279   bool global_scope_p;
3280   bool nested_name_specifier_p;
3281
3282   /* Assume the `template' keyword was not used.  */
3283   if (template_p)
3284     *template_p = template_keyword_p;
3285
3286   /* Look for the optional `::' operator.  */
3287   global_scope_p
3288     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3289        != NULL_TREE);
3290   /* Look for the optional nested-name-specifier.  */
3291   nested_name_specifier_p
3292     = (cp_parser_nested_name_specifier_opt (parser,
3293                                             /*typename_keyword_p=*/false,
3294                                             check_dependency_p,
3295                                             /*type_p=*/false,
3296                                             declarator_p)
3297        != NULL_TREE);
3298   /* If there is a nested-name-specifier, then we are looking at
3299      the first qualified-id production.  */
3300   if (nested_name_specifier_p)
3301     {
3302       tree saved_scope;
3303       tree saved_object_scope;
3304       tree saved_qualifying_scope;
3305       tree unqualified_id;
3306       bool is_template;
3307
3308       /* See if the next token is the `template' keyword.  */
3309       if (!template_p)
3310         template_p = &is_template;
3311       *template_p = cp_parser_optional_template_keyword (parser);
3312       /* Name lookup we do during the processing of the
3313          unqualified-id might obliterate SCOPE.  */
3314       saved_scope = parser->scope;
3315       saved_object_scope = parser->object_scope;
3316       saved_qualifying_scope = parser->qualifying_scope;
3317       /* Process the final unqualified-id.  */
3318       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3319                                                  check_dependency_p,
3320                                                  declarator_p,
3321                                                  /*optional_p=*/false);
3322       /* Restore the SAVED_SCOPE for our caller.  */
3323       parser->scope = saved_scope;
3324       parser->object_scope = saved_object_scope;
3325       parser->qualifying_scope = saved_qualifying_scope;
3326
3327       return unqualified_id;
3328     }
3329   /* Otherwise, if we are in global scope, then we are looking at one
3330      of the other qualified-id productions.  */
3331   else if (global_scope_p)
3332     {
3333       cp_token *token;
3334       tree id;
3335
3336       /* Peek at the next token.  */
3337       token = cp_lexer_peek_token (parser->lexer);
3338
3339       /* If it's an identifier, and the next token is not a "<", then
3340          we can avoid the template-id case.  This is an optimization
3341          for this common case.  */
3342       if (token->type == CPP_NAME
3343           && !cp_parser_nth_token_starts_template_argument_list_p
3344                (parser, 2))
3345         return cp_parser_identifier (parser);
3346
3347       cp_parser_parse_tentatively (parser);
3348       /* Try a template-id.  */
3349       id = cp_parser_template_id (parser,
3350                                   /*template_keyword_p=*/false,
3351                                   /*check_dependency_p=*/true,
3352                                   declarator_p);
3353       /* If that worked, we're done.  */
3354       if (cp_parser_parse_definitely (parser))
3355         return id;
3356
3357       /* Peek at the next token.  (Changes in the token buffer may
3358          have invalidated the pointer obtained above.)  */
3359       token = cp_lexer_peek_token (parser->lexer);
3360
3361       switch (token->type)
3362         {
3363         case CPP_NAME:
3364           return cp_parser_identifier (parser);
3365
3366         case CPP_KEYWORD:
3367           if (token->keyword == RID_OPERATOR)
3368             return cp_parser_operator_function_id (parser);
3369           /* Fall through.  */
3370
3371         default:
3372           cp_parser_error (parser, "expected id-expression");
3373           return error_mark_node;
3374         }
3375     }
3376   else
3377     return cp_parser_unqualified_id (parser, template_keyword_p,
3378                                      /*check_dependency_p=*/true,
3379                                      declarator_p,
3380                                      optional_p);
3381 }
3382
3383 /* Parse an unqualified-id.
3384
3385    unqualified-id:
3386      identifier
3387      operator-function-id
3388      conversion-function-id
3389      ~ class-name
3390      template-id
3391
3392    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3393    keyword, in a construct like `A::template ...'.
3394
3395    Returns a representation of unqualified-id.  For the `identifier'
3396    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3397    production a BIT_NOT_EXPR is returned; the operand of the
3398    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3399    other productions, see the documentation accompanying the
3400    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3401    names are looked up in uninstantiated templates.  If DECLARATOR_P
3402    is true, the unqualified-id is appearing as part of a declarator,
3403    rather than as part of an expression.  */
3404
3405 static tree
3406 cp_parser_unqualified_id (cp_parser* parser,
3407                           bool template_keyword_p,
3408                           bool check_dependency_p,
3409                           bool declarator_p,
3410                           bool optional_p)
3411 {
3412   cp_token *token;
3413
3414   /* Peek at the next token.  */
3415   token = cp_lexer_peek_token (parser->lexer);
3416
3417   switch (token->type)
3418     {
3419     case CPP_NAME:
3420       {
3421         tree id;
3422
3423         /* We don't know yet whether or not this will be a
3424            template-id.  */
3425         cp_parser_parse_tentatively (parser);
3426         /* Try a template-id.  */
3427         id = cp_parser_template_id (parser, template_keyword_p,
3428                                     check_dependency_p,
3429                                     declarator_p);
3430         /* If it worked, we're done.  */
3431         if (cp_parser_parse_definitely (parser))
3432           return id;
3433         /* Otherwise, it's an ordinary identifier.  */
3434         return cp_parser_identifier (parser);
3435       }
3436
3437     case CPP_TEMPLATE_ID:
3438       return cp_parser_template_id (parser, template_keyword_p,
3439                                     check_dependency_p,
3440                                     declarator_p);
3441
3442     case CPP_COMPL:
3443       {
3444         tree type_decl;
3445         tree qualifying_scope;
3446         tree object_scope;
3447         tree scope;
3448         bool done;
3449
3450         /* Consume the `~' token.  */
3451         cp_lexer_consume_token (parser->lexer);
3452         /* Parse the class-name.  The standard, as written, seems to
3453            say that:
3454
3455              template <typename T> struct S { ~S (); };
3456              template <typename T> S<T>::~S() {}
3457
3458            is invalid, since `~' must be followed by a class-name, but
3459            `S<T>' is dependent, and so not known to be a class.
3460            That's not right; we need to look in uninstantiated
3461            templates.  A further complication arises from:
3462
3463              template <typename T> void f(T t) {
3464                t.T::~T();
3465              }
3466
3467            Here, it is not possible to look up `T' in the scope of `T'
3468            itself.  We must look in both the current scope, and the
3469            scope of the containing complete expression.
3470
3471            Yet another issue is:
3472
3473              struct S {
3474                int S;
3475                ~S();
3476              };
3477
3478              S::~S() {}
3479
3480            The standard does not seem to say that the `S' in `~S'
3481            should refer to the type `S' and not the data member
3482            `S::S'.  */
3483
3484         /* DR 244 says that we look up the name after the "~" in the
3485            same scope as we looked up the qualifying name.  That idea
3486            isn't fully worked out; it's more complicated than that.  */
3487         scope = parser->scope;
3488         object_scope = parser->object_scope;
3489         qualifying_scope = parser->qualifying_scope;
3490
3491         /* Check for invalid scopes.  */
3492         if (scope == error_mark_node)
3493           {
3494             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3495               cp_lexer_consume_token (parser->lexer);
3496             return error_mark_node;
3497           }
3498         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3499           {
3500             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3501               error ("scope %qT before %<~%> is not a class-name", scope);
3502             cp_parser_simulate_error (parser);
3503             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3504               cp_lexer_consume_token (parser->lexer);
3505             return error_mark_node;
3506           }
3507         gcc_assert (!scope || TYPE_P (scope));
3508
3509         /* If the name is of the form "X::~X" it's OK.  */
3510         token = cp_lexer_peek_token (parser->lexer);
3511         if (scope
3512             && token->type == CPP_NAME
3513             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3514                 == CPP_OPEN_PAREN)
3515             && constructor_name_p (token->value, scope))
3516           {
3517             cp_lexer_consume_token (parser->lexer);
3518             return build_nt (BIT_NOT_EXPR, scope);
3519           }
3520
3521         /* If there was an explicit qualification (S::~T), first look
3522            in the scope given by the qualification (i.e., S).  */
3523         done = false;
3524         type_decl = NULL_TREE;
3525         if (scope)
3526           {
3527             cp_parser_parse_tentatively (parser);
3528             type_decl = cp_parser_class_name (parser,
3529                                               /*typename_keyword_p=*/false,
3530                                               /*template_keyword_p=*/false,
3531                                               none_type,
3532                                               /*check_dependency=*/false,
3533                                               /*class_head_p=*/false,
3534                                               declarator_p);
3535             if (cp_parser_parse_definitely (parser))
3536               done = true;
3537           }
3538         /* In "N::S::~S", look in "N" as well.  */
3539         if (!done && scope && qualifying_scope)
3540           {
3541             cp_parser_parse_tentatively (parser);
3542             parser->scope = qualifying_scope;
3543             parser->object_scope = NULL_TREE;
3544             parser->qualifying_scope = NULL_TREE;
3545             type_decl
3546               = cp_parser_class_name (parser,
3547                                       /*typename_keyword_p=*/false,
3548                                       /*template_keyword_p=*/false,
3549                                       none_type,
3550                                       /*check_dependency=*/false,
3551                                       /*class_head_p=*/false,
3552                                       declarator_p);
3553             if (cp_parser_parse_definitely (parser))
3554               done = true;
3555           }
3556         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3557         else if (!done && object_scope)
3558           {
3559             cp_parser_parse_tentatively (parser);
3560             parser->scope = object_scope;
3561             parser->object_scope = NULL_TREE;
3562             parser->qualifying_scope = NULL_TREE;
3563             type_decl
3564               = cp_parser_class_name (parser,
3565                                       /*typename_keyword_p=*/false,
3566                                       /*template_keyword_p=*/false,
3567                                       none_type,
3568                                       /*check_dependency=*/false,
3569                                       /*class_head_p=*/false,
3570                                       declarator_p);
3571             if (cp_parser_parse_definitely (parser))
3572               done = true;
3573           }
3574         /* Look in the surrounding context.  */
3575         if (!done)
3576           {
3577             parser->scope = NULL_TREE;
3578             parser->object_scope = NULL_TREE;
3579             parser->qualifying_scope = NULL_TREE;
3580             type_decl
3581               = cp_parser_class_name (parser,
3582                                       /*typename_keyword_p=*/false,
3583                                       /*template_keyword_p=*/false,
3584                                       none_type,
3585                                       /*check_dependency=*/false,
3586                                       /*class_head_p=*/false,
3587                                       declarator_p);
3588           }
3589         /* If an error occurred, assume that the name of the
3590            destructor is the same as the name of the qualifying
3591            class.  That allows us to keep parsing after running
3592            into ill-formed destructor names.  */
3593         if (type_decl == error_mark_node && scope)
3594           return build_nt (BIT_NOT_EXPR, scope);
3595         else if (type_decl == error_mark_node)
3596           return error_mark_node;
3597
3598         /* Check that destructor name and scope match.  */
3599         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3600           {
3601             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3602               error ("declaration of %<~%T%> as member of %qT",
3603                      type_decl, scope);
3604             cp_parser_simulate_error (parser);
3605             return error_mark_node;
3606           }
3607
3608         /* [class.dtor]
3609
3610            A typedef-name that names a class shall not be used as the
3611            identifier in the declarator for a destructor declaration.  */
3612         if (declarator_p
3613             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3614             && !DECL_SELF_REFERENCE_P (type_decl)
3615             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3616           error ("typedef-name %qD used as destructor declarator",
3617                  type_decl);
3618
3619         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3620       }
3621
3622     case CPP_KEYWORD:
3623       if (token->keyword == RID_OPERATOR)
3624         {
3625           tree id;
3626
3627           /* This could be a template-id, so we try that first.  */
3628           cp_parser_parse_tentatively (parser);
3629           /* Try a template-id.  */
3630           id = cp_parser_template_id (parser, template_keyword_p,
3631                                       /*check_dependency_p=*/true,
3632                                       declarator_p);
3633           /* If that worked, we're done.  */
3634           if (cp_parser_parse_definitely (parser))
3635             return id;
3636           /* We still don't know whether we're looking at an
3637              operator-function-id or a conversion-function-id.  */
3638           cp_parser_parse_tentatively (parser);
3639           /* Try an operator-function-id.  */
3640           id = cp_parser_operator_function_id (parser);
3641           /* If that didn't work, try a conversion-function-id.  */
3642           if (!cp_parser_parse_definitely (parser))
3643             id = cp_parser_conversion_function_id (parser);
3644
3645           return id;
3646         }
3647       /* Fall through.  */
3648
3649     default:
3650       if (optional_p)
3651         return NULL_TREE;
3652       cp_parser_error (parser, "expected unqualified-id");
3653       return error_mark_node;
3654     }
3655 }
3656
3657 /* Parse an (optional) nested-name-specifier.
3658
3659    nested-name-specifier:
3660      class-or-namespace-name :: nested-name-specifier [opt]
3661      class-or-namespace-name :: template nested-name-specifier [opt]
3662
3663    PARSER->SCOPE should be set appropriately before this function is
3664    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3665    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3666    in name lookups.
3667
3668    Sets PARSER->SCOPE to the class (TYPE) or namespace
3669    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3670    it unchanged if there is no nested-name-specifier.  Returns the new
3671    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3672
3673    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3674    part of a declaration and/or decl-specifier.  */
3675
3676 static tree
3677 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3678                                      bool typename_keyword_p,
3679                                      bool check_dependency_p,
3680                                      bool type_p,
3681                                      bool is_declaration)
3682 {
3683   bool success = false;
3684   cp_token_position start = 0;
3685   cp_token *token;
3686
3687   /* Remember where the nested-name-specifier starts.  */
3688   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3689     {
3690       start = cp_lexer_token_position (parser->lexer, false);
3691       push_deferring_access_checks (dk_deferred);
3692     }
3693
3694   while (true)
3695     {
3696       tree new_scope;
3697       tree old_scope;
3698       tree saved_qualifying_scope;
3699       bool template_keyword_p;
3700
3701       /* Spot cases that cannot be the beginning of a
3702          nested-name-specifier.  */
3703       token = cp_lexer_peek_token (parser->lexer);
3704
3705       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3706          the already parsed nested-name-specifier.  */
3707       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3708         {
3709           /* Grab the nested-name-specifier and continue the loop.  */
3710           cp_parser_pre_parsed_nested_name_specifier (parser);
3711           /* If we originally encountered this nested-name-specifier
3712              with IS_DECLARATION set to false, we will not have
3713              resolved TYPENAME_TYPEs, so we must do so here.  */
3714           if (is_declaration
3715               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3716             {
3717               new_scope = resolve_typename_type (parser->scope,
3718                                                  /*only_current_p=*/false);
3719               if (new_scope != error_mark_node)
3720                 parser->scope = new_scope;
3721             }
3722           success = true;
3723           continue;
3724         }
3725
3726       /* Spot cases that cannot be the beginning of a
3727          nested-name-specifier.  On the second and subsequent times
3728          through the loop, we look for the `template' keyword.  */
3729       if (success && token->keyword == RID_TEMPLATE)
3730         ;
3731       /* A template-id can start a nested-name-specifier.  */
3732       else if (token->type == CPP_TEMPLATE_ID)
3733         ;
3734       else
3735         {
3736           /* If the next token is not an identifier, then it is
3737              definitely not a class-or-namespace-name.  */
3738           if (token->type != CPP_NAME)
3739             break;
3740           /* If the following token is neither a `<' (to begin a
3741              template-id), nor a `::', then we are not looking at a
3742              nested-name-specifier.  */
3743           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3744           if (token->type != CPP_SCOPE
3745               && !cp_parser_nth_token_starts_template_argument_list_p
3746                   (parser, 2))
3747             break;
3748         }
3749
3750       /* The nested-name-specifier is optional, so we parse
3751          tentatively.  */
3752       cp_parser_parse_tentatively (parser);
3753
3754       /* Look for the optional `template' keyword, if this isn't the
3755          first time through the loop.  */
3756       if (success)
3757         template_keyword_p = cp_parser_optional_template_keyword (parser);
3758       else
3759         template_keyword_p = false;
3760
3761       /* Save the old scope since the name lookup we are about to do
3762          might destroy it.  */
3763       old_scope = parser->scope;
3764       saved_qualifying_scope = parser->qualifying_scope;
3765       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3766          look up names in "X<T>::I" in order to determine that "Y" is
3767          a template.  So, if we have a typename at this point, we make
3768          an effort to look through it.  */
3769       if (is_declaration
3770           && !typename_keyword_p
3771           && parser->scope
3772           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3773         parser->scope = resolve_typename_type (parser->scope,
3774                                                /*only_current_p=*/false);
3775       /* Parse the qualifying entity.  */
3776       new_scope
3777         = cp_parser_class_or_namespace_name (parser,
3778                                              typename_keyword_p,
3779                                              template_keyword_p,
3780                                              check_dependency_p,
3781                                              type_p,
3782                                              is_declaration);
3783       /* Look for the `::' token.  */
3784       cp_parser_require (parser, CPP_SCOPE, "`::'");
3785
3786       /* If we found what we wanted, we keep going; otherwise, we're
3787          done.  */
3788       if (!cp_parser_parse_definitely (parser))
3789         {
3790           bool error_p = false;
3791
3792           /* Restore the OLD_SCOPE since it was valid before the
3793              failed attempt at finding the last
3794              class-or-namespace-name.  */
3795           parser->scope = old_scope;
3796           parser->qualifying_scope = saved_qualifying_scope;
3797           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3798             break;
3799           /* If the next token is an identifier, and the one after
3800              that is a `::', then any valid interpretation would have
3801              found a class-or-namespace-name.  */
3802           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3803                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3804                      == CPP_SCOPE)
3805                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3806                      != CPP_COMPL))
3807             {
3808               token = cp_lexer_consume_token (parser->lexer);
3809               if (!error_p)
3810                 {
3811                   if (!token->ambiguous_p)
3812                     {
3813                       tree decl;
3814                       tree ambiguous_decls;
3815
3816                       decl = cp_parser_lookup_name (parser, token->value,
3817                                                     none_type,
3818                                                     /*is_template=*/false,
3819                                                     /*is_namespace=*/false,
3820                                                     /*check_dependency=*/true,
3821                                                     &ambiguous_decls);
3822                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3823                         error ("%qD used without template parameters", decl);
3824                       else if (ambiguous_decls)
3825                         {
3826                           error ("reference to %qD is ambiguous",
3827                                  token->value);
3828                           print_candidates (ambiguous_decls);
3829                           decl = error_mark_node;
3830                         }
3831                       else
3832                         cp_parser_name_lookup_error
3833                           (parser, token->value, decl,
3834                            "is not a class or namespace");
3835                     }
3836                   parser->scope = error_mark_node;
3837                   error_p = true;
3838                   /* Treat this as a successful nested-name-specifier
3839                      due to:
3840
3841                      [basic.lookup.qual]
3842
3843                      If the name found is not a class-name (clause
3844                      _class_) or namespace-name (_namespace.def_), the
3845                      program is ill-formed.  */
3846                   success = true;
3847                 }
3848               cp_lexer_consume_token (parser->lexer);
3849             }
3850           break;
3851         }
3852       /* We've found one valid nested-name-specifier.  */
3853       success = true;
3854       /* Name lookup always gives us a DECL.  */
3855       if (TREE_CODE (new_scope) == TYPE_DECL)
3856         new_scope = TREE_TYPE (new_scope);
3857       /* Uses of "template" must be followed by actual templates.  */
3858       if (template_keyword_p
3859           && !(CLASS_TYPE_P (new_scope)
3860                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3861                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3862                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3863           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3864                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3865                    == TEMPLATE_ID_EXPR)))
3866         pedwarn (TYPE_P (new_scope)
3867                  ? "%qT is not a template"
3868                  : "%qD is not a template",
3869                  new_scope);
3870       /* If it is a class scope, try to complete it; we are about to
3871          be looking up names inside the class.  */
3872       if (TYPE_P (new_scope)
3873           /* Since checking types for dependency can be expensive,
3874              avoid doing it if the type is already complete.  */
3875           && !COMPLETE_TYPE_P (new_scope)
3876           /* Do not try to complete dependent types.  */
3877           && !dependent_type_p (new_scope))
3878         new_scope = complete_type (new_scope);
3879       /* Make sure we look in the right scope the next time through
3880          the loop.  */
3881       parser->scope = new_scope;
3882     }
3883
3884   /* If parsing tentatively, replace the sequence of tokens that makes
3885      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3886      token.  That way, should we re-parse the token stream, we will
3887      not have to repeat the effort required to do the parse, nor will
3888      we issue duplicate error messages.  */
3889   if (success && start)
3890     {
3891       cp_token *token;
3892       tree access_checks;
3893
3894       token = cp_lexer_token_at (parser->lexer, start);
3895       /* Reset the contents of the START token.  */
3896       token->type = CPP_NESTED_NAME_SPECIFIER;
3897       /* Retrieve any deferred checks.  Do not pop this access checks yet
3898          so the memory will not be reclaimed during token replacing below.  */
3899       access_checks = get_deferred_access_checks ();
3900       token->value = build_tree_list (copy_list (access_checks),
3901                                       parser->scope);
3902       TREE_TYPE (token->value) = parser->qualifying_scope;
3903       token->keyword = RID_MAX;
3904
3905       /* Purge all subsequent tokens.  */
3906       cp_lexer_purge_tokens_after (parser->lexer, start);
3907     }
3908
3909   if (start)
3910     pop_to_parent_deferring_access_checks ();
3911
3912   return success ? parser->scope : NULL_TREE;
3913 }
3914
3915 /* Parse a nested-name-specifier.  See
3916    cp_parser_nested_name_specifier_opt for details.  This function
3917    behaves identically, except that it will an issue an error if no
3918    nested-name-specifier is present.  */
3919
3920 static tree
3921 cp_parser_nested_name_specifier (cp_parser *parser,
3922                                  bool typename_keyword_p,
3923                                  bool check_dependency_p,
3924                                  bool type_p,
3925                                  bool is_declaration)
3926 {
3927   tree scope;
3928
3929   /* Look for the nested-name-specifier.  */
3930   scope = cp_parser_nested_name_specifier_opt (parser,
3931                                                typename_keyword_p,
3932                                                check_dependency_p,
3933                                                type_p,
3934                                                is_declaration);
3935   /* If it was not present, issue an error message.  */
3936   if (!scope)
3937     {
3938       cp_parser_error (parser, "expected nested-name-specifier");
3939       parser->scope = NULL_TREE;
3940     }
3941
3942   return scope;
3943 }
3944
3945 /* Parse a class-or-namespace-name.
3946
3947    class-or-namespace-name:
3948      class-name
3949      namespace-name
3950
3951    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3952    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3953    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3954    TYPE_P is TRUE iff the next name should be taken as a class-name,
3955    even the same name is declared to be another entity in the same
3956    scope.
3957
3958    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3959    specified by the class-or-namespace-name.  If neither is found the
3960    ERROR_MARK_NODE is returned.  */
3961
3962 static tree
3963 cp_parser_class_or_namespace_name (cp_parser *parser,
3964                                    bool typename_keyword_p,
3965                                    bool template_keyword_p,
3966                                    bool check_dependency_p,
3967                                    bool type_p,
3968                                    bool is_declaration)
3969 {
3970   tree saved_scope;
3971   tree saved_qualifying_scope;
3972   tree saved_object_scope;
3973   tree scope;
3974   bool only_class_p;
3975
3976   /* Before we try to parse the class-name, we must save away the
3977      current PARSER->SCOPE since cp_parser_class_name will destroy
3978      it.  */
3979   saved_scope = parser->scope;
3980   saved_qualifying_scope = parser->qualifying_scope;
3981   saved_object_scope = parser->object_scope;
3982   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3983      there is no need to look for a namespace-name.  */
3984   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3985   if (!only_class_p)
3986     cp_parser_parse_tentatively (parser);
3987   scope = cp_parser_class_name (parser,
3988                                 typename_keyword_p,
3989                                 template_keyword_p,
3990                                 type_p ? class_type : none_type,
3991                                 check_dependency_p,
3992                                 /*class_head_p=*/false,
3993                                 is_declaration);
3994   /* If that didn't work, try for a namespace-name.  */
3995   if (!only_class_p && !cp_parser_parse_definitely (parser))
3996     {
3997       /* Restore the saved scope.  */
3998       parser->scope = saved_scope;
3999       parser->qualifying_scope = saved_qualifying_scope;
4000       parser->object_scope = saved_object_scope;
4001       /* If we are not looking at an identifier followed by the scope
4002          resolution operator, then this is not part of a
4003          nested-name-specifier.  (Note that this function is only used
4004          to parse the components of a nested-name-specifier.)  */
4005       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4006           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4007         return error_mark_node;
4008       scope = cp_parser_namespace_name (parser);
4009     }
4010
4011   return scope;
4012 }
4013
4014 /* Parse a postfix-expression.
4015
4016    postfix-expression:
4017      primary-expression
4018      postfix-expression [ expression ]
4019      postfix-expression ( expression-list [opt] )
4020      simple-type-specifier ( expression-list [opt] )
4021      typename :: [opt] nested-name-specifier identifier
4022        ( expression-list [opt] )
4023      typename :: [opt] nested-name-specifier template [opt] template-id
4024        ( expression-list [opt] )
4025      postfix-expression . template [opt] id-expression
4026      postfix-expression -> template [opt] id-expression
4027      postfix-expression . pseudo-destructor-name
4028      postfix-expression -> pseudo-destructor-name
4029      postfix-expression ++
4030      postfix-expression --
4031      dynamic_cast < type-id > ( expression )
4032      static_cast < type-id > ( expression )
4033      reinterpret_cast < type-id > ( expression )
4034      const_cast < type-id > ( expression )
4035      typeid ( expression )
4036      typeid ( type-id )
4037
4038    GNU Extension:
4039
4040    postfix-expression:
4041      ( type-id ) { initializer-list , [opt] }
4042
4043    This extension is a GNU version of the C99 compound-literal
4044    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4045    but they are essentially the same concept.)
4046
4047    If ADDRESS_P is true, the postfix expression is the operand of the
4048    `&' operator.  CAST_P is true if this expression is the target of a
4049    cast.
4050
4051    Returns a representation of the expression.  */
4052
4053 static tree
4054 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4055 {
4056   cp_token *token;
4057   enum rid keyword;
4058   cp_id_kind idk = CP_ID_KIND_NONE;
4059   tree postfix_expression = NULL_TREE;
4060
4061   /* Peek at the next token.  */
4062   token = cp_lexer_peek_token (parser->lexer);
4063   /* Some of the productions are determined by keywords.  */
4064   keyword = token->keyword;
4065   switch (keyword)
4066     {
4067     case RID_DYNCAST:
4068     case RID_STATCAST:
4069     case RID_REINTCAST:
4070     case RID_CONSTCAST:
4071       {
4072         tree type;
4073         tree expression;
4074         const char *saved_message;
4075
4076         /* All of these can be handled in the same way from the point
4077            of view of parsing.  Begin by consuming the token
4078            identifying the cast.  */
4079         cp_lexer_consume_token (parser->lexer);
4080
4081         /* New types cannot be defined in the cast.  */
4082         saved_message = parser->type_definition_forbidden_message;
4083         parser->type_definition_forbidden_message
4084           = "types may not be defined in casts";
4085
4086         /* Look for the opening `<'.  */
4087         cp_parser_require (parser, CPP_LESS, "`<'");
4088         /* Parse the type to which we are casting.  */
4089         type = cp_parser_type_id (parser);
4090         /* Look for the closing `>'.  */
4091         cp_parser_require (parser, CPP_GREATER, "`>'");
4092         /* Restore the old message.  */
4093         parser->type_definition_forbidden_message = saved_message;
4094
4095         /* And the expression which is being cast.  */
4096         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4097         expression = cp_parser_expression (parser, /*cast_p=*/true);
4098         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4099
4100         /* Only type conversions to integral or enumeration types
4101            can be used in constant-expressions.  */
4102         if (!cast_valid_in_integral_constant_expression_p (type)
4103             && (cp_parser_non_integral_constant_expression
4104                 (parser,
4105                  "a cast to a type other than an integral or "
4106                  "enumeration type")))
4107           return error_mark_node;
4108
4109         switch (keyword)
4110           {
4111           case RID_DYNCAST:
4112             postfix_expression
4113               = build_dynamic_cast (type, expression);
4114             break;
4115           case RID_STATCAST:
4116             postfix_expression
4117               = build_static_cast (type, expression);
4118             break;
4119           case RID_REINTCAST:
4120             postfix_expression
4121               = build_reinterpret_cast (type, expression);
4122             break;
4123           case RID_CONSTCAST:
4124             postfix_expression
4125               = build_const_cast (type, expression);
4126             break;
4127           default:
4128             gcc_unreachable ();
4129           }
4130       }
4131       break;
4132
4133     case RID_TYPEID:
4134       {
4135         tree type;
4136         const char *saved_message;
4137         bool saved_in_type_id_in_expr_p;
4138
4139         /* Consume the `typeid' token.  */
4140         cp_lexer_consume_token (parser->lexer);
4141         /* Look for the `(' token.  */
4142         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4143         /* Types cannot be defined in a `typeid' expression.  */
4144         saved_message = parser->type_definition_forbidden_message;
4145         parser->type_definition_forbidden_message
4146           = "types may not be defined in a `typeid\' expression";
4147         /* We can't be sure yet whether we're looking at a type-id or an
4148            expression.  */
4149         cp_parser_parse_tentatively (parser);
4150         /* Try a type-id first.  */
4151         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4152         parser->in_type_id_in_expr_p = true;
4153         type = cp_parser_type_id (parser);
4154         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4155         /* Look for the `)' token.  Otherwise, we can't be sure that
4156            we're not looking at an expression: consider `typeid (int
4157            (3))', for example.  */
4158         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4159         /* If all went well, simply lookup the type-id.  */
4160         if (cp_parser_parse_definitely (parser))
4161           postfix_expression = get_typeid (type);
4162         /* Otherwise, fall back to the expression variant.  */
4163         else
4164           {
4165             tree expression;
4166
4167             /* Look for an expression.  */
4168             expression = cp_parser_expression (parser, /*cast_p=*/false);
4169             /* Compute its typeid.  */
4170             postfix_expression = build_typeid (expression);
4171             /* Look for the `)' token.  */
4172             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4173           }
4174         /* Restore the saved message.  */
4175         parser->type_definition_forbidden_message = saved_message;
4176         /* `typeid' may not appear in an integral constant expression.  */
4177         if (cp_parser_non_integral_constant_expression(parser,
4178                                                        "`typeid' operator"))
4179           return error_mark_node;
4180       }
4181       break;
4182
4183     case RID_TYPENAME:
4184       {
4185         tree type;
4186         /* The syntax permitted here is the same permitted for an
4187            elaborated-type-specifier.  */
4188         type = cp_parser_elaborated_type_specifier (parser,
4189                                                     /*is_friend=*/false,
4190                                                     /*is_declaration=*/false);
4191         postfix_expression = cp_parser_functional_cast (parser, type);
4192       }
4193       break;
4194
4195     default:
4196       {
4197         tree type;
4198
4199         /* If the next thing is a simple-type-specifier, we may be
4200            looking at a functional cast.  We could also be looking at
4201            an id-expression.  So, we try the functional cast, and if
4202            that doesn't work we fall back to the primary-expression.  */
4203         cp_parser_parse_tentatively (parser);
4204         /* Look for the simple-type-specifier.  */
4205         type = cp_parser_simple_type_specifier (parser,
4206                                                 /*decl_specs=*/NULL,
4207                                                 CP_PARSER_FLAGS_NONE);
4208         /* Parse the cast itself.  */
4209         if (!cp_parser_error_occurred (parser))
4210           postfix_expression
4211             = cp_parser_functional_cast (parser, type);
4212         /* If that worked, we're done.  */
4213         if (cp_parser_parse_definitely (parser))
4214           break;
4215
4216         /* If the functional-cast didn't work out, try a
4217            compound-literal.  */
4218         if (cp_parser_allow_gnu_extensions_p (parser)
4219             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4220           {
4221             VEC(constructor_elt,gc) *initializer_list = NULL;
4222             bool saved_in_type_id_in_expr_p;
4223
4224             cp_parser_parse_tentatively (parser);
4225             /* Consume the `('.  */
4226             cp_lexer_consume_token (parser->lexer);
4227             /* Parse the type.  */
4228             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4229             parser->in_type_id_in_expr_p = true;
4230             type = cp_parser_type_id (parser);
4231             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4232             /* Look for the `)'.  */
4233             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4234             /* Look for the `{'.  */
4235             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4236             /* If things aren't going well, there's no need to
4237                keep going.  */
4238             if (!cp_parser_error_occurred (parser))
4239               {
4240                 bool non_constant_p;
4241                 /* Parse the initializer-list.  */
4242                 initializer_list
4243                   = cp_parser_initializer_list (parser, &non_constant_p);
4244                 /* Allow a trailing `,'.  */
4245                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4246                   cp_lexer_consume_token (parser->lexer);
4247                 /* Look for the final `}'.  */
4248                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4249               }
4250             /* If that worked, we're definitely looking at a
4251                compound-literal expression.  */
4252             if (cp_parser_parse_definitely (parser))
4253               {
4254                 /* Warn the user that a compound literal is not
4255                    allowed in standard C++.  */
4256                 if (pedantic)
4257                   pedwarn ("ISO C++ forbids compound-literals");
4258                 /* Form the representation of the compound-literal.  */
4259                 postfix_expression
4260                   = finish_compound_literal (type, initializer_list);
4261                 break;
4262               }
4263           }
4264
4265         /* It must be a primary-expression.  */
4266         postfix_expression
4267           = cp_parser_primary_expression (parser, address_p, cast_p,
4268                                           /*template_arg_p=*/false,
4269                                           &idk);
4270       }
4271       break;
4272     }
4273
4274   /* Keep looping until the postfix-expression is complete.  */
4275   while (true)
4276     {
4277       if (idk == CP_ID_KIND_UNQUALIFIED
4278           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4279           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4280         /* It is not a Koenig lookup function call.  */
4281         postfix_expression
4282           = unqualified_name_lookup_error (postfix_expression);
4283
4284       /* Peek at the next token.  */
4285       token = cp_lexer_peek_token (parser->lexer);
4286
4287       switch (token->type)
4288         {
4289         case CPP_OPEN_SQUARE:
4290           postfix_expression
4291             = cp_parser_postfix_open_square_expression (parser,
4292                                                         postfix_expression,
4293                                                         false);
4294           idk = CP_ID_KIND_NONE;
4295           break;
4296
4297         case CPP_OPEN_PAREN:
4298           /* postfix-expression ( expression-list [opt] ) */
4299           {
4300             bool koenig_p;
4301             bool is_builtin_constant_p;
4302             bool saved_integral_constant_expression_p = false;
4303             bool saved_non_integral_constant_expression_p = false;
4304             tree args;
4305
4306             is_builtin_constant_p
4307               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4308             if (is_builtin_constant_p)
4309               {
4310                 /* The whole point of __builtin_constant_p is to allow
4311                    non-constant expressions to appear as arguments.  */
4312                 saved_integral_constant_expression_p
4313                   = parser->integral_constant_expression_p;
4314                 saved_non_integral_constant_expression_p
4315                   = parser->non_integral_constant_expression_p;
4316                 parser->integral_constant_expression_p = false;
4317               }
4318             args = (cp_parser_parenthesized_expression_list
4319                     (parser, /*is_attribute_list=*/false,
4320                      /*cast_p=*/false,
4321                      /*non_constant_p=*/NULL));
4322             if (is_builtin_constant_p)
4323               {
4324                 parser->integral_constant_expression_p
4325                   = saved_integral_constant_expression_p;
4326                 parser->non_integral_constant_expression_p
4327                   = saved_non_integral_constant_expression_p;
4328               }
4329
4330             if (args == error_mark_node)
4331               {
4332                 postfix_expression = error_mark_node;
4333                 break;
4334               }
4335
4336             /* Function calls are not permitted in
4337                constant-expressions.  */
4338             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4339                 && cp_parser_non_integral_constant_expression (parser,
4340                                                                "a function call"))
4341               {
4342                 postfix_expression = error_mark_node;
4343                 break;
4344               }
4345
4346             koenig_p = false;
4347             if (idk == CP_ID_KIND_UNQUALIFIED)
4348               {
4349                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4350                   {
4351                     if (args)
4352                       {
4353                         koenig_p = true;
4354                         postfix_expression
4355                           = perform_koenig_lookup (postfix_expression, args);
4356                       }
4357                     else
4358                       postfix_expression
4359                         = unqualified_fn_lookup_error (postfix_expression);
4360                   }
4361                 /* We do not perform argument-dependent lookup if
4362                    normal lookup finds a non-function, in accordance
4363                    with the expected resolution of DR 218.  */
4364                 else if (args && is_overloaded_fn (postfix_expression))
4365                   {
4366                     tree fn = get_first_fn (postfix_expression);
4367
4368                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4369                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4370
4371                     /* Only do argument dependent lookup if regular
4372                        lookup does not find a set of member functions.
4373                        [basic.lookup.koenig]/2a  */
4374                     if (!DECL_FUNCTION_MEMBER_P (fn))
4375                       {
4376                         koenig_p = true;
4377                         postfix_expression
4378                           = perform_koenig_lookup (postfix_expression, args);
4379                       }
4380                   }
4381               }
4382
4383             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4384               {
4385                 tree instance = TREE_OPERAND (postfix_expression, 0);
4386                 tree fn = TREE_OPERAND (postfix_expression, 1);
4387
4388                 if (processing_template_decl
4389                     && (type_dependent_expression_p (instance)
4390                         || (!BASELINK_P (fn)
4391                             && TREE_CODE (fn) != FIELD_DECL)
4392                         || type_dependent_expression_p (fn)
4393                         || any_type_dependent_arguments_p (args)))
4394                   {
4395                     postfix_expression
4396                       = build_min_nt (CALL_EXPR, postfix_expression,
4397                                       args, NULL_TREE);
4398                     break;
4399                   }
4400
4401                 if (BASELINK_P (fn))
4402                   postfix_expression
4403                     = (build_new_method_call
4404                        (instance, fn, args, NULL_TREE,
4405                         (idk == CP_ID_KIND_QUALIFIED
4406                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4407                         /*fn_p=*/NULL));
4408                 else
4409                   postfix_expression
4410                     = finish_call_expr (postfix_expression, args,
4411                                         /*disallow_virtual=*/false,
4412                                         /*koenig_p=*/false);
4413               }
4414             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4415                      || TREE_CODE (postfix_expression) == MEMBER_REF
4416                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4417               postfix_expression = (build_offset_ref_call_from_tree
4418                                     (postfix_expression, args));
4419             else if (idk == CP_ID_KIND_QUALIFIED)
4420               /* A call to a static class member, or a namespace-scope
4421                  function.  */
4422               postfix_expression
4423                 = finish_call_expr (postfix_expression, args,
4424                                     /*disallow_virtual=*/true,
4425                                     koenig_p);
4426             else
4427               /* All other function calls.  */
4428               postfix_expression
4429                 = finish_call_expr (postfix_expression, args,
4430                                     /*disallow_virtual=*/false,
4431                                     koenig_p);
4432
4433             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4434             idk = CP_ID_KIND_NONE;
4435           }
4436           break;
4437
4438         case CPP_DOT:
4439         case CPP_DEREF:
4440           /* postfix-expression . template [opt] id-expression
4441              postfix-expression . pseudo-destructor-name
4442              postfix-expression -> template [opt] id-expression
4443              postfix-expression -> pseudo-destructor-name */
4444
4445           /* Consume the `.' or `->' operator.  */
4446           cp_lexer_consume_token (parser->lexer);
4447
4448           postfix_expression
4449             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4450                                                       postfix_expression,
4451                                                       false, &idk);
4452           break;
4453
4454         case CPP_PLUS_PLUS:
4455           /* postfix-expression ++  */
4456           /* Consume the `++' token.  */
4457           cp_lexer_consume_token (parser->lexer);
4458           /* Generate a representation for the complete expression.  */
4459           postfix_expression
4460             = finish_increment_expr (postfix_expression,
4461                                      POSTINCREMENT_EXPR);
4462           /* Increments may not appear in constant-expressions.  */
4463           if (cp_parser_non_integral_constant_expression (parser,
4464                                                           "an increment"))
4465             postfix_expression = error_mark_node;
4466           idk = CP_ID_KIND_NONE;
4467           break;
4468
4469         case CPP_MINUS_MINUS:
4470           /* postfix-expression -- */
4471           /* Consume the `--' token.  */
4472           cp_lexer_consume_token (parser->lexer);
4473           /* Generate a representation for the complete expression.  */
4474           postfix_expression
4475             = finish_increment_expr (postfix_expression,
4476                                      POSTDECREMENT_EXPR);
4477           /* Decrements may not appear in constant-expressions.  */
4478           if (cp_parser_non_integral_constant_expression (parser,
4479                                                           "a decrement"))
4480             postfix_expression = error_mark_node;
4481           idk = CP_ID_KIND_NONE;
4482           break;
4483
4484         default:
4485           return postfix_expression;
4486         }
4487     }
4488
4489   /* We should never get here.  */
4490   gcc_unreachable ();
4491   return error_mark_node;
4492 }
4493
4494 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4495    by cp_parser_builtin_offsetof.  We're looking for
4496
4497      postfix-expression [ expression ]
4498
4499    FOR_OFFSETOF is set if we're being called in that context, which
4500    changes how we deal with integer constant expressions.  */
4501
4502 static tree
4503 cp_parser_postfix_open_square_expression (cp_parser *parser,
4504                                           tree postfix_expression,
4505                                           bool for_offsetof)
4506 {
4507   tree index;
4508
4509   /* Consume the `[' token.  */
4510   cp_lexer_consume_token (parser->lexer);
4511
4512   /* Parse the index expression.  */
4513   /* ??? For offsetof, there is a question of what to allow here.  If
4514      offsetof is not being used in an integral constant expression context,
4515      then we *could* get the right answer by computing the value at runtime.
4516      If we are in an integral constant expression context, then we might
4517      could accept any constant expression; hard to say without analysis.
4518      Rather than open the barn door too wide right away, allow only integer
4519      constant expressions here.  */
4520   if (for_offsetof)
4521     index = cp_parser_constant_expression (parser, false, NULL);
4522   else
4523     index = cp_parser_expression (parser, /*cast_p=*/false);
4524
4525   /* Look for the closing `]'.  */
4526   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4527
4528   /* Build the ARRAY_REF.  */
4529   postfix_expression = grok_array_decl (postfix_expression, index);
4530
4531   /* When not doing offsetof, array references are not permitted in
4532      constant-expressions.  */
4533   if (!for_offsetof
4534       && (cp_parser_non_integral_constant_expression
4535           (parser, "an array reference")))
4536     postfix_expression = error_mark_node;
4537
4538   return postfix_expression;
4539 }
4540
4541 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4542    by cp_parser_builtin_offsetof.  We're looking for
4543
4544      postfix-expression . template [opt] id-expression
4545      postfix-expression . pseudo-destructor-name
4546      postfix-expression -> template [opt] id-expression
4547      postfix-expression -> pseudo-destructor-name
4548
4549    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4550    limits what of the above we'll actually accept, but nevermind.
4551    TOKEN_TYPE is the "." or "->" token, which will already have been
4552    removed from the stream.  */
4553
4554 static tree
4555 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4556                                         enum cpp_ttype token_type,
4557                                         tree postfix_expression,
4558                                         bool for_offsetof, cp_id_kind *idk)
4559 {
4560   tree name;
4561   bool dependent_p;
4562   bool pseudo_destructor_p;
4563   tree scope = NULL_TREE;
4564
4565   /* If this is a `->' operator, dereference the pointer.  */
4566   if (token_type == CPP_DEREF)
4567     postfix_expression = build_x_arrow (postfix_expression);
4568   /* Check to see whether or not the expression is type-dependent.  */
4569   dependent_p = type_dependent_expression_p (postfix_expression);
4570   /* The identifier following the `->' or `.' is not qualified.  */
4571   parser->scope = NULL_TREE;
4572   parser->qualifying_scope = NULL_TREE;
4573   parser->object_scope = NULL_TREE;
4574   *idk = CP_ID_KIND_NONE;
4575   /* Enter the scope corresponding to the type of the object
4576      given by the POSTFIX_EXPRESSION.  */
4577   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4578     {
4579       scope = TREE_TYPE (postfix_expression);
4580       /* According to the standard, no expression should ever have
4581          reference type.  Unfortunately, we do not currently match
4582          the standard in this respect in that our internal representation
4583          of an expression may have reference type even when the standard
4584          says it does not.  Therefore, we have to manually obtain the
4585          underlying type here.  */
4586       scope = non_reference (scope);
4587       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4588       if (scope == unknown_type_node)
4589         {
4590           error ("%qE does not have class type", postfix_expression);
4591           scope = NULL_TREE;
4592         }
4593       else
4594         scope = complete_type_or_else (scope, NULL_TREE);
4595       /* Let the name lookup machinery know that we are processing a
4596          class member access expression.  */
4597       parser->context->object_type = scope;
4598       /* If something went wrong, we want to be able to discern that case,
4599          as opposed to the case where there was no SCOPE due to the type
4600          of expression being dependent.  */
4601       if (!scope)
4602         scope = error_mark_node;
4603       /* If the SCOPE was erroneous, make the various semantic analysis
4604          functions exit quickly -- and without issuing additional error
4605          messages.  */
4606       if (scope == error_mark_node)
4607         postfix_expression = error_mark_node;
4608     }
4609
4610   /* Assume this expression is not a pseudo-destructor access.  */
4611   pseudo_destructor_p = false;
4612
4613   /* If the SCOPE is a scalar type, then, if this is a valid program,
4614      we must be looking at a pseudo-destructor-name.  */
4615   if (scope && SCALAR_TYPE_P (scope))
4616     {
4617       tree s;
4618       tree type;
4619
4620       cp_parser_parse_tentatively (parser);
4621       /* Parse the pseudo-destructor-name.  */
4622       s = NULL_TREE;
4623       cp_parser_pseudo_destructor_name (parser, &s, &type);
4624       if (cp_parser_parse_definitely (parser))
4625         {
4626           pseudo_destructor_p = true;
4627           postfix_expression
4628             = finish_pseudo_destructor_expr (postfix_expression,
4629                                              s, TREE_TYPE (type));
4630         }
4631     }
4632
4633   if (!pseudo_destructor_p)
4634     {
4635       /* If the SCOPE is not a scalar type, we are looking at an
4636          ordinary class member access expression, rather than a
4637          pseudo-destructor-name.  */
4638       bool template_p;
4639       /* Parse the id-expression.  */
4640       name = (cp_parser_id_expression
4641               (parser,
4642                cp_parser_optional_template_keyword (parser),
4643                /*check_dependency_p=*/true,
4644                &template_p,
4645                /*declarator_p=*/false,
4646                /*optional_p=*/false));
4647       /* In general, build a SCOPE_REF if the member name is qualified.
4648          However, if the name was not dependent and has already been
4649          resolved; there is no need to build the SCOPE_REF.  For example;
4650
4651              struct X { void f(); };
4652              template <typename T> void f(T* t) { t->X::f(); }
4653
4654          Even though "t" is dependent, "X::f" is not and has been resolved
4655          to a BASELINK; there is no need to include scope information.  */
4656
4657       /* But we do need to remember that there was an explicit scope for
4658          virtual function calls.  */
4659       if (parser->scope)
4660         *idk = CP_ID_KIND_QUALIFIED;
4661
4662       /* If the name is a template-id that names a type, we will get a
4663          TYPE_DECL here.  That is invalid code.  */
4664       if (TREE_CODE (name) == TYPE_DECL)
4665         {
4666           error ("invalid use of %qD", name);
4667           postfix_expression = error_mark_node;
4668         }
4669       else
4670         {
4671           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4672             {
4673               name = build_qualified_name (/*type=*/NULL_TREE,
4674                                            parser->scope,
4675                                            name,
4676                                            template_p);
4677               parser->scope = NULL_TREE;
4678               parser->qualifying_scope = NULL_TREE;
4679               parser->object_scope = NULL_TREE;
4680             }
4681           if (scope && name && BASELINK_P (name))
4682             adjust_result_of_qualified_name_lookup
4683               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4684           postfix_expression
4685             = finish_class_member_access_expr (postfix_expression, name,
4686                                                template_p);
4687         }
4688     }
4689
4690   /* We no longer need to look up names in the scope of the object on
4691      the left-hand side of the `.' or `->' operator.  */
4692   parser->context->object_type = NULL_TREE;
4693
4694   /* Outside of offsetof, these operators may not appear in
4695      constant-expressions.  */
4696   if (!for_offsetof
4697       && (cp_parser_non_integral_constant_expression
4698           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4699     postfix_expression = error_mark_node;
4700
4701   return postfix_expression;
4702 }
4703
4704 /* Parse a parenthesized expression-list.
4705
4706    expression-list:
4707      assignment-expression
4708      expression-list, assignment-expression
4709
4710    attribute-list:
4711      expression-list
4712      identifier
4713      identifier, expression-list
4714
4715    CAST_P is true if this expression is the target of a cast.
4716
4717    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4718    representation of an assignment-expression.  Note that a TREE_LIST
4719    is returned even if there is only a single expression in the list.
4720    error_mark_node is returned if the ( and or ) are
4721    missing. NULL_TREE is returned on no expressions. The parentheses
4722    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4723    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4724    indicates whether or not all of the expressions in the list were
4725    constant.  */
4726
4727 static tree
4728 cp_parser_parenthesized_expression_list (cp_parser* parser,
4729                                          bool is_attribute_list,
4730                                          bool cast_p,
4731                                          bool *non_constant_p)
4732 {
4733   tree expression_list = NULL_TREE;
4734   bool fold_expr_p = is_attribute_list;
4735   tree identifier = NULL_TREE;
4736
4737   /* Assume all the expressions will be constant.  */
4738   if (non_constant_p)
4739     *non_constant_p = false;
4740
4741   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4742     return error_mark_node;
4743
4744   /* Consume expressions until there are no more.  */
4745   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4746     while (true)
4747       {
4748         tree expr;
4749
4750         /* At the beginning of attribute lists, check to see if the
4751            next token is an identifier.  */
4752         if (is_attribute_list
4753             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4754           {
4755             cp_token *token;
4756
4757             /* Consume the identifier.  */
4758             token = cp_lexer_consume_token (parser->lexer);
4759             /* Save the identifier.  */
4760             identifier = token->value;
4761           }
4762         else
4763           {
4764             /* Parse the next assignment-expression.  */
4765             if (non_constant_p)
4766               {
4767                 bool expr_non_constant_p;
4768                 expr = (cp_parser_constant_expression
4769                         (parser, /*allow_non_constant_p=*/true,
4770                          &expr_non_constant_p));
4771                 if (expr_non_constant_p)
4772                   *non_constant_p = true;
4773               }
4774             else
4775               expr = cp_parser_assignment_expression (parser, cast_p);
4776
4777             if (fold_expr_p)
4778               expr = fold_non_dependent_expr (expr);
4779
4780              /* Add it to the list.  We add error_mark_node
4781                 expressions to the list, so that we can still tell if
4782                 the correct form for a parenthesized expression-list
4783                 is found. That gives better errors.  */
4784             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4785
4786             if (expr == error_mark_node)
4787               goto skip_comma;
4788           }
4789
4790         /* After the first item, attribute lists look the same as
4791            expression lists.  */
4792         is_attribute_list = false;
4793
4794       get_comma:;
4795         /* If the next token isn't a `,', then we are done.  */
4796         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4797           break;
4798
4799         /* Otherwise, consume the `,' and keep going.  */
4800         cp_lexer_consume_token (parser->lexer);
4801       }
4802
4803   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4804     {
4805       int ending;
4806
4807     skip_comma:;
4808       /* We try and resync to an unnested comma, as that will give the
4809          user better diagnostics.  */
4810       ending = cp_parser_skip_to_closing_parenthesis (parser,
4811                                                       /*recovering=*/true,
4812                                                       /*or_comma=*/true,
4813                                                       /*consume_paren=*/true);
4814       if (ending < 0)
4815         goto get_comma;
4816       if (!ending)
4817         return error_mark_node;
4818     }
4819
4820   /* We built up the list in reverse order so we must reverse it now.  */
4821   expression_list = nreverse (expression_list);
4822   if (identifier)
4823     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4824
4825   return expression_list;
4826 }
4827
4828 /* Parse a pseudo-destructor-name.
4829
4830    pseudo-destructor-name:
4831      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4832      :: [opt] nested-name-specifier template template-id :: ~ type-name
4833      :: [opt] nested-name-specifier [opt] ~ type-name
4834
4835    If either of the first two productions is used, sets *SCOPE to the
4836    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4837    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4838    or ERROR_MARK_NODE if the parse fails.  */
4839
4840 static void
4841 cp_parser_pseudo_destructor_name (cp_parser* parser,
4842                                   tree* scope,
4843                                   tree* type)
4844 {
4845   bool nested_name_specifier_p;
4846
4847   /* Assume that things will not work out.  */
4848   *type = error_mark_node;
4849
4850   /* Look for the optional `::' operator.  */
4851   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4852   /* Look for the optional nested-name-specifier.  */
4853   nested_name_specifier_p
4854     = (cp_parser_nested_name_specifier_opt (parser,
4855                                             /*typename_keyword_p=*/false,
4856                                             /*check_dependency_p=*/true,
4857                                             /*type_p=*/false,
4858                                             /*is_declaration=*/true)
4859        != NULL_TREE);
4860   /* Now, if we saw a nested-name-specifier, we might be doing the
4861      second production.  */
4862   if (nested_name_specifier_p
4863       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4864     {
4865       /* Consume the `template' keyword.  */
4866       cp_lexer_consume_token (parser->lexer);
4867       /* Parse the template-id.  */
4868       cp_parser_template_id (parser,
4869                              /*template_keyword_p=*/true,
4870                              /*check_dependency_p=*/false,
4871                              /*is_declaration=*/true);
4872       /* Look for the `::' token.  */
4873       cp_parser_require (parser, CPP_SCOPE, "`::'");
4874     }
4875   /* If the next token is not a `~', then there might be some
4876      additional qualification.  */
4877   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4878     {
4879       /* Look for the type-name.  */
4880       *scope = TREE_TYPE (cp_parser_type_name (parser));
4881
4882       if (*scope == error_mark_node)
4883         return;
4884
4885       /* If we don't have ::~, then something has gone wrong.  Since
4886          the only caller of this function is looking for something
4887          after `.' or `->' after a scalar type, most likely the
4888          program is trying to get a member of a non-aggregate
4889          type.  */
4890       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4891           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4892         {
4893           cp_parser_error (parser, "request for member of non-aggregate type");
4894           return;
4895         }
4896
4897       /* Look for the `::' token.  */
4898       cp_parser_require (parser, CPP_SCOPE, "`::'");
4899     }
4900   else
4901     *scope = NULL_TREE;
4902
4903   /* Look for the `~'.  */
4904   cp_parser_require (parser, CPP_COMPL, "`~'");
4905   /* Look for the type-name again.  We are not responsible for
4906      checking that it matches the first type-name.  */
4907   *type = cp_parser_type_name (parser);
4908 }
4909
4910 /* Parse a unary-expression.
4911
4912    unary-expression:
4913      postfix-expression
4914      ++ cast-expression
4915      -- cast-expression
4916      unary-operator cast-expression
4917      sizeof unary-expression
4918      sizeof ( type-id )
4919      new-expression
4920      delete-expression
4921
4922    GNU Extensions:
4923
4924    unary-expression:
4925      __extension__ cast-expression
4926      __alignof__ unary-expression
4927      __alignof__ ( type-id )
4928      __real__ cast-expression
4929      __imag__ cast-expression
4930      && identifier
4931
4932    ADDRESS_P is true iff the unary-expression is appearing as the
4933    operand of the `&' operator.   CAST_P is true if this expression is
4934    the target of a cast.
4935
4936    Returns a representation of the expression.  */
4937
4938 static tree
4939 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4940 {
4941   cp_token *token;
4942   enum tree_code unary_operator;
4943
4944   /* Peek at the next token.  */
4945   token = cp_lexer_peek_token (parser->lexer);
4946   /* Some keywords give away the kind of expression.  */
4947   if (token->type == CPP_KEYWORD)
4948     {
4949       enum rid keyword = token->keyword;
4950
4951       switch (keyword)
4952         {
4953         case RID_ALIGNOF:
4954         case RID_SIZEOF:
4955           {
4956             tree operand;
4957             enum tree_code op;
4958
4959             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4960             /* Consume the token.  */
4961             cp_lexer_consume_token (parser->lexer);
4962             /* Parse the operand.  */
4963             operand = cp_parser_sizeof_operand (parser, keyword);
4964
4965             if (TYPE_P (operand))
4966               return cxx_sizeof_or_alignof_type (operand, op, true);
4967             else
4968               return cxx_sizeof_or_alignof_expr (operand, op);
4969           }
4970
4971         case RID_NEW:
4972           return cp_parser_new_expression (parser);
4973
4974         case RID_DELETE:
4975           return cp_parser_delete_expression (parser);
4976
4977         case RID_EXTENSION:
4978           {
4979             /* The saved value of the PEDANTIC flag.  */
4980             int saved_pedantic;
4981             tree expr;
4982
4983             /* Save away the PEDANTIC flag.  */
4984             cp_parser_extension_opt (parser, &saved_pedantic);
4985             /* Parse the cast-expression.  */
4986             expr = cp_parser_simple_cast_expression (parser);
4987             /* Restore the PEDANTIC flag.  */
4988             pedantic = saved_pedantic;
4989
4990             return expr;
4991           }
4992
4993         case RID_REALPART:
4994         case RID_IMAGPART:
4995           {
4996             tree expression;
4997
4998             /* Consume the `__real__' or `__imag__' token.  */
4999             cp_lexer_consume_token (parser->lexer);
5000             /* Parse the cast-expression.  */
5001             expression = cp_parser_simple_cast_expression (parser);
5002             /* Create the complete representation.  */
5003             return build_x_unary_op ((keyword == RID_REALPART
5004                                       ? REALPART_EXPR : IMAGPART_EXPR),
5005                                      expression);
5006           }
5007           break;
5008
5009         default:
5010           break;
5011         }
5012     }
5013
5014   /* Look for the `:: new' and `:: delete', which also signal the
5015      beginning of a new-expression, or delete-expression,
5016      respectively.  If the next token is `::', then it might be one of
5017      these.  */
5018   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5019     {
5020       enum rid keyword;
5021
5022       /* See if the token after the `::' is one of the keywords in
5023          which we're interested.  */
5024       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5025       /* If it's `new', we have a new-expression.  */
5026       if (keyword == RID_NEW)
5027         return cp_parser_new_expression (parser);
5028       /* Similarly, for `delete'.  */
5029       else if (keyword == RID_DELETE)
5030         return cp_parser_delete_expression (parser);
5031     }
5032
5033   /* Look for a unary operator.  */
5034   unary_operator = cp_parser_unary_operator (token);
5035   /* The `++' and `--' operators can be handled similarly, even though
5036      they are not technically unary-operators in the grammar.  */
5037   if (unary_operator == ERROR_MARK)
5038     {
5039       if (token->type == CPP_PLUS_PLUS)
5040         unary_operator = PREINCREMENT_EXPR;
5041       else if (token->type == CPP_MINUS_MINUS)
5042         unary_operator = PREDECREMENT_EXPR;
5043       /* Handle the GNU address-of-label extension.  */
5044       else if (cp_parser_allow_gnu_extensions_p (parser)
5045                && token->type == CPP_AND_AND)
5046         {
5047           tree identifier;
5048
5049           /* Consume the '&&' token.  */
5050           cp_lexer_consume_token (parser->lexer);
5051           /* Look for the identifier.  */
5052           identifier = cp_parser_identifier (parser);
5053           /* Create an expression representing the address.  */
5054           return finish_label_address_expr (identifier);
5055         }
5056     }
5057   if (unary_operator != ERROR_MARK)
5058     {
5059       tree cast_expression;
5060       tree expression = error_mark_node;
5061       const char *non_constant_p = NULL;
5062
5063       /* Consume the operator token.  */
5064       token = cp_lexer_consume_token (parser->lexer);
5065       /* Parse the cast-expression.  */
5066       cast_expression
5067         = cp_parser_cast_expression (parser,
5068                                      unary_operator == ADDR_EXPR,
5069                                      /*cast_p=*/false);
5070       /* Now, build an appropriate representation.  */
5071       switch (unary_operator)
5072         {
5073         case INDIRECT_REF:
5074           non_constant_p = "`*'";
5075           expression = build_x_indirect_ref (cast_expression, "unary *");
5076           break;
5077
5078         case ADDR_EXPR:
5079           non_constant_p = "`&'";
5080           /* Fall through.  */
5081         case BIT_NOT_EXPR:
5082           expression = build_x_unary_op (unary_operator, cast_expression);
5083           break;
5084
5085         case PREINCREMENT_EXPR:
5086         case PREDECREMENT_EXPR:
5087           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5088                             ? "`++'" : "`--'");
5089           /* Fall through.  */
5090         case UNARY_PLUS_EXPR:
5091         case NEGATE_EXPR:
5092         case TRUTH_NOT_EXPR:
5093           expression = finish_unary_op_expr (unary_operator, cast_expression);
5094           break;
5095
5096         default:
5097           gcc_unreachable ();
5098         }
5099
5100       if (non_constant_p
5101           && cp_parser_non_integral_constant_expression (parser,
5102                                                          non_constant_p))
5103         expression = error_mark_node;
5104
5105       return expression;
5106     }
5107
5108   return cp_parser_postfix_expression (parser, address_p, cast_p);
5109 }
5110
5111 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5112    unary-operator, the corresponding tree code is returned.  */
5113
5114 static enum tree_code
5115 cp_parser_unary_operator (cp_token* token)
5116 {
5117   switch (token->type)
5118     {
5119     case CPP_MULT:
5120       return INDIRECT_REF;
5121
5122     case CPP_AND:
5123       return ADDR_EXPR;
5124
5125     case CPP_PLUS:
5126       return UNARY_PLUS_EXPR;
5127
5128     case CPP_MINUS:
5129       return NEGATE_EXPR;
5130
5131     case CPP_NOT:
5132       return TRUTH_NOT_EXPR;
5133
5134     case CPP_COMPL:
5135       return BIT_NOT_EXPR;
5136
5137     default:
5138       return ERROR_MARK;
5139     }
5140 }
5141
5142 /* Parse a new-expression.
5143
5144    new-expression:
5145      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5146      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5147
5148    Returns a representation of the expression.  */
5149
5150 static tree
5151 cp_parser_new_expression (cp_parser* parser)
5152 {
5153   bool global_scope_p;
5154   tree placement;
5155   tree type;
5156   tree initializer;
5157   tree nelts;
5158
5159   /* Look for the optional `::' operator.  */
5160   global_scope_p
5161     = (cp_parser_global_scope_opt (parser,
5162                                    /*current_scope_valid_p=*/false)
5163        != NULL_TREE);
5164   /* Look for the `new' operator.  */
5165   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5166   /* There's no easy way to tell a new-placement from the
5167      `( type-id )' construct.  */
5168   cp_parser_parse_tentatively (parser);
5169   /* Look for a new-placement.  */
5170   placement = cp_parser_new_placement (parser);
5171   /* If that didn't work out, there's no new-placement.  */
5172   if (!cp_parser_parse_definitely (parser))
5173     placement = NULL_TREE;
5174
5175   /* If the next token is a `(', then we have a parenthesized
5176      type-id.  */
5177   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5178     {
5179       /* Consume the `('.  */
5180       cp_lexer_consume_token (parser->lexer);
5181       /* Parse the type-id.  */
5182       type = cp_parser_type_id (parser);
5183       /* Look for the closing `)'.  */
5184       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5185       /* There should not be a direct-new-declarator in this production,
5186          but GCC used to allowed this, so we check and emit a sensible error
5187          message for this case.  */
5188       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5189         {
5190           error ("array bound forbidden after parenthesized type-id");
5191           inform ("try removing the parentheses around the type-id");
5192           cp_parser_direct_new_declarator (parser);
5193         }
5194       nelts = NULL_TREE;
5195     }
5196   /* Otherwise, there must be a new-type-id.  */
5197   else
5198     type = cp_parser_new_type_id (parser, &nelts);
5199
5200   /* If the next token is a `(', then we have a new-initializer.  */
5201   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5202     initializer = cp_parser_new_initializer (parser);
5203   else
5204     initializer = NULL_TREE;
5205
5206   /* A new-expression may not appear in an integral constant
5207      expression.  */
5208   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5209     return error_mark_node;
5210
5211   /* Create a representation of the new-expression.  */
5212   return build_new (placement, type, nelts, initializer, global_scope_p);
5213 }
5214
5215 /* Parse a new-placement.
5216
5217    new-placement:
5218      ( expression-list )
5219
5220    Returns the same representation as for an expression-list.  */
5221
5222 static tree
5223 cp_parser_new_placement (cp_parser* parser)
5224 {
5225   tree expression_list;
5226
5227   /* Parse the expression-list.  */
5228   expression_list = (cp_parser_parenthesized_expression_list
5229                      (parser, false, /*cast_p=*/false,
5230                       /*non_constant_p=*/NULL));
5231
5232   return expression_list;
5233 }
5234
5235 /* Parse a new-type-id.
5236
5237    new-type-id:
5238      type-specifier-seq new-declarator [opt]
5239
5240    Returns the TYPE allocated.  If the new-type-id indicates an array
5241    type, *NELTS is set to the number of elements in the last array
5242    bound; the TYPE will not include the last array bound.  */
5243
5244 static tree
5245 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5246 {
5247   cp_decl_specifier_seq type_specifier_seq;
5248   cp_declarator *new_declarator;
5249   cp_declarator *declarator;
5250   cp_declarator *outer_declarator;
5251   const char *saved_message;
5252   tree type;
5253
5254   /* The type-specifier sequence must not contain type definitions.
5255      (It cannot contain declarations of new types either, but if they
5256      are not definitions we will catch that because they are not
5257      complete.)  */
5258   saved_message = parser->type_definition_forbidden_message;
5259   parser->type_definition_forbidden_message
5260     = "types may not be defined in a new-type-id";
5261   /* Parse the type-specifier-seq.  */
5262   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5263                                 &type_specifier_seq);
5264   /* Restore the old message.  */
5265   parser->type_definition_forbidden_message = saved_message;
5266   /* Parse the new-declarator.  */
5267   new_declarator = cp_parser_new_declarator_opt (parser);
5268
5269   /* Determine the number of elements in the last array dimension, if
5270      any.  */
5271   *nelts = NULL_TREE;
5272   /* Skip down to the last array dimension.  */
5273   declarator = new_declarator;
5274   outer_declarator = NULL;
5275   while (declarator && (declarator->kind == cdk_pointer
5276                         || declarator->kind == cdk_ptrmem))
5277     {
5278       outer_declarator = declarator;
5279       declarator = declarator->declarator;
5280     }
5281   while (declarator
5282          && declarator->kind == cdk_array
5283          && declarator->declarator
5284          && declarator->declarator->kind == cdk_array)
5285     {
5286       outer_declarator = declarator;
5287       declarator = declarator->declarator;
5288     }
5289
5290   if (declarator && declarator->kind == cdk_array)
5291     {
5292       *nelts = declarator->u.array.bounds;
5293       if (*nelts == error_mark_node)
5294         *nelts = integer_one_node;
5295
5296       if (outer_declarator)
5297         outer_declarator->declarator = declarator->declarator;
5298       else
5299         new_declarator = NULL;
5300     }
5301
5302   type = groktypename (&type_specifier_seq, new_declarator);
5303   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5304     {
5305       *nelts = array_type_nelts_top (type);
5306       type = TREE_TYPE (type);
5307     }
5308   return type;
5309 }
5310
5311 /* Parse an (optional) new-declarator.
5312
5313    new-declarator:
5314      ptr-operator new-declarator [opt]
5315      direct-new-declarator
5316
5317    Returns the declarator.  */
5318
5319 static cp_declarator *
5320 cp_parser_new_declarator_opt (cp_parser* parser)
5321 {
5322   enum tree_code code;
5323   tree type;
5324   cp_cv_quals cv_quals;
5325
5326   /* We don't know if there's a ptr-operator next, or not.  */
5327   cp_parser_parse_tentatively (parser);
5328   /* Look for a ptr-operator.  */
5329   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5330   /* If that worked, look for more new-declarators.  */
5331   if (cp_parser_parse_definitely (parser))
5332     {
5333       cp_declarator *declarator;
5334
5335       /* Parse another optional declarator.  */
5336       declarator = cp_parser_new_declarator_opt (parser);
5337
5338       /* Create the representation of the declarator.  */
5339       if (type)
5340         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5341       else if (code == INDIRECT_REF)
5342         declarator = make_pointer_declarator (cv_quals, declarator);
5343       else
5344         declarator = make_reference_declarator (cv_quals, declarator);
5345
5346       return declarator;
5347     }
5348
5349   /* If the next token is a `[', there is a direct-new-declarator.  */
5350   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5351     return cp_parser_direct_new_declarator (parser);
5352
5353   return NULL;
5354 }
5355
5356 /* Parse a direct-new-declarator.
5357
5358    direct-new-declarator:
5359      [ expression ]
5360      direct-new-declarator [constant-expression]
5361
5362    */
5363
5364 static cp_declarator *
5365 cp_parser_direct_new_declarator (cp_parser* parser)
5366 {
5367   cp_declarator *declarator = NULL;
5368
5369   while (true)
5370     {
5371       tree expression;
5372
5373       /* Look for the opening `['.  */
5374       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5375       /* The first expression is not required to be constant.  */
5376       if (!declarator)
5377         {
5378           expression = cp_parser_expression (parser, /*cast_p=*/false);
5379           /* The standard requires that the expression have integral
5380              type.  DR 74 adds enumeration types.  We believe that the
5381              real intent is that these expressions be handled like the
5382              expression in a `switch' condition, which also allows
5383              classes with a single conversion to integral or
5384              enumeration type.  */
5385           if (!processing_template_decl)
5386             {
5387               expression
5388                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5389                                               expression,
5390                                               /*complain=*/true);
5391               if (!expression)
5392                 {
5393                   error ("expression in new-declarator must have integral "
5394                          "or enumeration type");
5395                   expression = error_mark_node;
5396                 }
5397             }
5398         }
5399       /* But all the other expressions must be.  */
5400       else
5401         expression
5402           = cp_parser_constant_expression (parser,
5403                                            /*allow_non_constant=*/false,
5404                                            NULL);
5405       /* Look for the closing `]'.  */
5406       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5407
5408       /* Add this bound to the declarator.  */
5409       declarator = make_array_declarator (declarator, expression);
5410
5411       /* If the next token is not a `[', then there are no more
5412          bounds.  */
5413       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5414         break;
5415     }
5416
5417   return declarator;
5418 }
5419
5420 /* Parse a new-initializer.
5421
5422    new-initializer:
5423      ( expression-list [opt] )
5424
5425    Returns a representation of the expression-list.  If there is no
5426    expression-list, VOID_ZERO_NODE is returned.  */
5427
5428 static tree
5429 cp_parser_new_initializer (cp_parser* parser)
5430 {
5431   tree expression_list;
5432
5433   expression_list = (cp_parser_parenthesized_expression_list
5434                      (parser, false, /*cast_p=*/false,
5435                       /*non_constant_p=*/NULL));
5436   if (!expression_list)
5437     expression_list = void_zero_node;
5438
5439   return expression_list;
5440 }
5441
5442 /* Parse a delete-expression.
5443
5444    delete-expression:
5445      :: [opt] delete cast-expression
5446      :: [opt] delete [ ] cast-expression
5447
5448    Returns a representation of the expression.  */
5449
5450 static tree
5451 cp_parser_delete_expression (cp_parser* parser)
5452 {
5453   bool global_scope_p;
5454   bool array_p;
5455   tree expression;
5456
5457   /* Look for the optional `::' operator.  */
5458   global_scope_p
5459     = (cp_parser_global_scope_opt (parser,
5460                                    /*current_scope_valid_p=*/false)
5461        != NULL_TREE);
5462   /* Look for the `delete' keyword.  */
5463   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5464   /* See if the array syntax is in use.  */
5465   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5466     {
5467       /* Consume the `[' token.  */
5468       cp_lexer_consume_token (parser->lexer);
5469       /* Look for the `]' token.  */
5470       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5471       /* Remember that this is the `[]' construct.  */
5472       array_p = true;
5473     }
5474   else
5475     array_p = false;
5476
5477   /* Parse the cast-expression.  */
5478   expression = cp_parser_simple_cast_expression (parser);
5479
5480   /* A delete-expression may not appear in an integral constant
5481      expression.  */
5482   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5483     return error_mark_node;
5484
5485   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5486 }
5487
5488 /* Parse a cast-expression.
5489
5490    cast-expression:
5491      unary-expression
5492      ( type-id ) cast-expression
5493
5494    ADDRESS_P is true iff the unary-expression is appearing as the
5495    operand of the `&' operator.   CAST_P is true if this expression is
5496    the target of a cast.
5497
5498    Returns a representation of the expression.  */
5499
5500 static tree
5501 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5502 {
5503   /* If it's a `(', then we might be looking at a cast.  */
5504   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5505     {
5506       tree type = NULL_TREE;
5507       tree expr = NULL_TREE;
5508       bool compound_literal_p;
5509       const char *saved_message;
5510
5511       /* There's no way to know yet whether or not this is a cast.
5512          For example, `(int (3))' is a unary-expression, while `(int)
5513          3' is a cast.  So, we resort to parsing tentatively.  */
5514       cp_parser_parse_tentatively (parser);
5515       /* Types may not be defined in a cast.  */
5516       saved_message = parser->type_definition_forbidden_message;
5517       parser->type_definition_forbidden_message
5518         = "types may not be defined in casts";
5519       /* Consume the `('.  */
5520       cp_lexer_consume_token (parser->lexer);
5521       /* A very tricky bit is that `(struct S) { 3 }' is a
5522          compound-literal (which we permit in C++ as an extension).
5523          But, that construct is not a cast-expression -- it is a
5524          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5525          is legal; if the compound-literal were a cast-expression,
5526          you'd need an extra set of parentheses.)  But, if we parse
5527          the type-id, and it happens to be a class-specifier, then we
5528          will commit to the parse at that point, because we cannot
5529          undo the action that is done when creating a new class.  So,
5530          then we cannot back up and do a postfix-expression.
5531
5532          Therefore, we scan ahead to the closing `)', and check to see
5533          if the token after the `)' is a `{'.  If so, we are not
5534          looking at a cast-expression.
5535
5536          Save tokens so that we can put them back.  */
5537       cp_lexer_save_tokens (parser->lexer);
5538       /* Skip tokens until the next token is a closing parenthesis.
5539          If we find the closing `)', and the next token is a `{', then
5540          we are looking at a compound-literal.  */
5541       compound_literal_p
5542         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5543                                                   /*consume_paren=*/true)
5544            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5545       /* Roll back the tokens we skipped.  */
5546       cp_lexer_rollback_tokens (parser->lexer);
5547       /* If we were looking at a compound-literal, simulate an error
5548          so that the call to cp_parser_parse_definitely below will
5549          fail.  */
5550       if (compound_literal_p)
5551         cp_parser_simulate_error (parser);
5552       else
5553         {
5554           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5555           parser->in_type_id_in_expr_p = true;
5556           /* Look for the type-id.  */
5557           type = cp_parser_type_id (parser);
5558           /* Look for the closing `)'.  */
5559           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5560           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5561         }
5562
5563       /* Restore the saved message.  */
5564       parser->type_definition_forbidden_message = saved_message;
5565
5566       /* If ok so far, parse the dependent expression. We cannot be
5567          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5568          ctor of T, but looks like a cast to function returning T
5569          without a dependent expression.  */
5570       if (!cp_parser_error_occurred (parser))
5571         expr = cp_parser_cast_expression (parser,
5572                                           /*address_p=*/false,
5573                                           /*cast_p=*/true);
5574
5575       if (cp_parser_parse_definitely (parser))
5576         {
5577           /* Warn about old-style casts, if so requested.  */
5578           if (warn_old_style_cast
5579               && !in_system_header
5580               && !VOID_TYPE_P (type)
5581               && current_lang_name != lang_name_c)
5582             warning (OPT_Wold_style_cast, "use of old-style cast");
5583
5584           /* Only type conversions to integral or enumeration types
5585              can be used in constant-expressions.  */
5586           if (!cast_valid_in_integral_constant_expression_p (type)
5587               && (cp_parser_non_integral_constant_expression
5588                   (parser,
5589                    "a cast to a type other than an integral or "
5590                    "enumeration type")))
5591             return error_mark_node;
5592
5593           /* Perform the cast.  */
5594           expr = build_c_cast (type, expr);
5595           return expr;
5596         }
5597     }
5598
5599   /* If we get here, then it's not a cast, so it must be a
5600      unary-expression.  */
5601   return cp_parser_unary_expression (parser, address_p, cast_p);
5602 }
5603
5604 /* Parse a binary expression of the general form:
5605
5606    pm-expression:
5607      cast-expression
5608      pm-expression .* cast-expression
5609      pm-expression ->* cast-expression
5610
5611    multiplicative-expression:
5612      pm-expression
5613      multiplicative-expression * pm-expression
5614      multiplicative-expression / pm-expression
5615      multiplicative-expression % pm-expression
5616
5617    additive-expression:
5618      multiplicative-expression
5619      additive-expression + multiplicative-expression
5620      additive-expression - multiplicative-expression
5621
5622    shift-expression:
5623      additive-expression
5624      shift-expression << additive-expression
5625      shift-expression >> additive-expression
5626
5627    relational-expression:
5628      shift-expression
5629      relational-expression < shift-expression
5630      relational-expression > shift-expression
5631      relational-expression <= shift-expression
5632      relational-expression >= shift-expression
5633
5634   GNU Extension:
5635
5636    relational-expression:
5637      relational-expression <? shift-expression
5638      relational-expression >? shift-expression
5639
5640    equality-expression:
5641      relational-expression
5642      equality-expression == relational-expression
5643      equality-expression != relational-expression
5644
5645    and-expression:
5646      equality-expression
5647      and-expression & equality-expression
5648
5649    exclusive-or-expression:
5650      and-expression
5651      exclusive-or-expression ^ and-expression
5652
5653    inclusive-or-expression:
5654      exclusive-or-expression
5655      inclusive-or-expression | exclusive-or-expression
5656
5657    logical-and-expression:
5658      inclusive-or-expression
5659      logical-and-expression && inclusive-or-expression
5660
5661    logical-or-expression:
5662      logical-and-expression
5663      logical-or-expression || logical-and-expression
5664
5665    All these are implemented with a single function like:
5666
5667    binary-expression:
5668      simple-cast-expression
5669      binary-expression <token> binary-expression
5670
5671    CAST_P is true if this expression is the target of a cast.
5672
5673    The binops_by_token map is used to get the tree codes for each <token> type.
5674    binary-expressions are associated according to a precedence table.  */
5675
5676 #define TOKEN_PRECEDENCE(token) \
5677   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5678    ? PREC_NOT_OPERATOR \
5679    : binops_by_token[token->type].prec)
5680
5681 static tree
5682 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5683 {
5684   cp_parser_expression_stack stack;
5685   cp_parser_expression_stack_entry *sp = &stack[0];
5686   tree lhs, rhs;
5687   cp_token *token;
5688   enum tree_code tree_type;
5689   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5690   bool overloaded_p;
5691
5692   /* Parse the first expression.  */
5693   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5694
5695   for (;;)
5696     {
5697       /* Get an operator token.  */
5698       token = cp_lexer_peek_token (parser->lexer);
5699
5700       new_prec = TOKEN_PRECEDENCE (token);
5701
5702       /* Popping an entry off the stack means we completed a subexpression:
5703          - either we found a token which is not an operator (`>' where it is not
5704            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5705            will happen repeatedly;
5706          - or, we found an operator which has lower priority.  This is the case
5707            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5708            parsing `3 * 4'.  */
5709       if (new_prec <= prec)
5710         {
5711           if (sp == stack)
5712             break;
5713           else
5714             goto pop;
5715         }
5716
5717      get_rhs:
5718       tree_type = binops_by_token[token->type].tree_type;
5719
5720       /* We used the operator token.  */
5721       cp_lexer_consume_token (parser->lexer);
5722
5723       /* Extract another operand.  It may be the RHS of this expression
5724          or the LHS of a new, higher priority expression.  */
5725       rhs = cp_parser_simple_cast_expression (parser);
5726
5727       /* Get another operator token.  Look up its precedence to avoid
5728          building a useless (immediately popped) stack entry for common
5729          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5730       token = cp_lexer_peek_token (parser->lexer);
5731       lookahead_prec = TOKEN_PRECEDENCE (token);
5732       if (lookahead_prec > new_prec)
5733         {
5734           /* ... and prepare to parse the RHS of the new, higher priority
5735              expression.  Since precedence levels on the stack are
5736              monotonically increasing, we do not have to care about
5737              stack overflows.  */
5738           sp->prec = prec;
5739           sp->tree_type = tree_type;
5740           sp->lhs = lhs;
5741           sp++;
5742           lhs = rhs;
5743           prec = new_prec;
5744           new_prec = lookahead_prec;
5745           goto get_rhs;
5746
5747          pop:
5748           /* If the stack is not empty, we have parsed into LHS the right side
5749              (`4' in the example above) of an expression we had suspended.
5750              We can use the information on the stack to recover the LHS (`3')
5751              from the stack together with the tree code (`MULT_EXPR'), and
5752              the precedence of the higher level subexpression
5753              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5754              which will be used to actually build the additive expression.  */
5755           --sp;
5756           prec = sp->prec;
5757           tree_type = sp->tree_type;
5758           rhs = lhs;
5759           lhs = sp->lhs;
5760         }
5761
5762       overloaded_p = false;
5763       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5764
5765       /* If the binary operator required the use of an overloaded operator,
5766          then this expression cannot be an integral constant-expression.
5767          An overloaded operator can be used even if both operands are
5768          otherwise permissible in an integral constant-expression if at
5769          least one of the operands is of enumeration type.  */
5770
5771       if (overloaded_p
5772           && (cp_parser_non_integral_constant_expression
5773               (parser, "calls to overloaded operators")))
5774         return error_mark_node;
5775     }
5776
5777   return lhs;
5778 }
5779
5780
5781 /* Parse the `? expression : assignment-expression' part of a
5782    conditional-expression.  The LOGICAL_OR_EXPR is the
5783    logical-or-expression that started the conditional-expression.
5784    Returns a representation of the entire conditional-expression.
5785
5786    This routine is used by cp_parser_assignment_expression.
5787
5788      ? expression : assignment-expression
5789
5790    GNU Extensions:
5791
5792      ? : assignment-expression */
5793
5794 static tree
5795 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5796 {
5797   tree expr;
5798   tree assignment_expr;
5799
5800   /* Consume the `?' token.  */
5801   cp_lexer_consume_token (parser->lexer);
5802   if (cp_parser_allow_gnu_extensions_p (parser)
5803       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5804     /* Implicit true clause.  */
5805     expr = NULL_TREE;
5806   else
5807     /* Parse the expression.  */
5808     expr = cp_parser_expression (parser, /*cast_p=*/false);
5809
5810   /* The next token should be a `:'.  */
5811   cp_parser_require (parser, CPP_COLON, "`:'");
5812   /* Parse the assignment-expression.  */
5813   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5814
5815   /* Build the conditional-expression.  */
5816   return build_x_conditional_expr (logical_or_expr,
5817                                    expr,
5818                                    assignment_expr);
5819 }
5820
5821 /* Parse an assignment-expression.
5822
5823    assignment-expression:
5824      conditional-expression
5825      logical-or-expression assignment-operator assignment_expression
5826      throw-expression
5827
5828    CAST_P is true if this expression is the target of a cast.
5829
5830    Returns a representation for the expression.  */
5831
5832 static tree
5833 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5834 {
5835   tree expr;
5836
5837   /* If the next token is the `throw' keyword, then we're looking at
5838      a throw-expression.  */
5839   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5840     expr = cp_parser_throw_expression (parser);
5841   /* Otherwise, it must be that we are looking at a
5842      logical-or-expression.  */
5843   else
5844     {
5845       /* Parse the binary expressions (logical-or-expression).  */
5846       expr = cp_parser_binary_expression (parser, cast_p);
5847       /* If the next token is a `?' then we're actually looking at a
5848          conditional-expression.  */
5849       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5850         return cp_parser_question_colon_clause (parser, expr);
5851       else
5852         {
5853           enum tree_code assignment_operator;
5854
5855           /* If it's an assignment-operator, we're using the second
5856              production.  */
5857           assignment_operator
5858             = cp_parser_assignment_operator_opt (parser);
5859           if (assignment_operator != ERROR_MARK)
5860             {
5861               tree rhs;
5862
5863               /* Parse the right-hand side of the assignment.  */
5864               rhs = cp_parser_assignment_expression (parser, cast_p);
5865               /* An assignment may not appear in a
5866                  constant-expression.  */
5867               if (cp_parser_non_integral_constant_expression (parser,
5868                                                               "an assignment"))
5869                 return error_mark_node;
5870               /* Build the assignment expression.  */
5871               expr = build_x_modify_expr (expr,
5872                                           assignment_operator,
5873                                           rhs);
5874             }
5875         }
5876     }
5877
5878   return expr;
5879 }
5880
5881 /* Parse an (optional) assignment-operator.
5882
5883    assignment-operator: one of
5884      = *= /= %= += -= >>= <<= &= ^= |=
5885
5886    GNU Extension:
5887
5888    assignment-operator: one of
5889      <?= >?=
5890
5891    If the next token is an assignment operator, the corresponding tree
5892    code is returned, and the token is consumed.  For example, for
5893    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5894    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5895    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5896    operator, ERROR_MARK is returned.  */
5897
5898 static enum tree_code
5899 cp_parser_assignment_operator_opt (cp_parser* parser)
5900 {
5901   enum tree_code op;
5902   cp_token *token;
5903
5904   /* Peek at the next toen.  */
5905   token = cp_lexer_peek_token (parser->lexer);
5906
5907   switch (token->type)
5908     {
5909     case CPP_EQ:
5910       op = NOP_EXPR;
5911       break;
5912
5913     case CPP_MULT_EQ:
5914       op = MULT_EXPR;
5915       break;
5916
5917     case CPP_DIV_EQ:
5918       op = TRUNC_DIV_EXPR;
5919       break;
5920
5921     case CPP_MOD_EQ:
5922       op = TRUNC_MOD_EXPR;
5923       break;
5924
5925     case CPP_PLUS_EQ:
5926       op = PLUS_EXPR;
5927       break;
5928
5929     case CPP_MINUS_EQ:
5930       op = MINUS_EXPR;
5931       break;
5932
5933     case CPP_RSHIFT_EQ:
5934       op = RSHIFT_EXPR;
5935       break;
5936
5937     case CPP_LSHIFT_EQ:
5938       op = LSHIFT_EXPR;
5939       break;
5940
5941     case CPP_AND_EQ:
5942       op = BIT_AND_EXPR;
5943       break;
5944
5945     case CPP_XOR_EQ:
5946       op = BIT_XOR_EXPR;
5947       break;
5948
5949     case CPP_OR_EQ:
5950       op = BIT_IOR_EXPR;
5951       break;
5952
5953     default:
5954       /* Nothing else is an assignment operator.  */
5955       op = ERROR_MARK;
5956     }
5957
5958   /* If it was an assignment operator, consume it.  */
5959   if (op != ERROR_MARK)
5960     cp_lexer_consume_token (parser->lexer);
5961
5962   return op;
5963 }
5964
5965 /* Parse an expression.
5966
5967    expression:
5968      assignment-expression
5969      expression , assignment-expression
5970
5971    CAST_P is true if this expression is the target of a cast.
5972
5973    Returns a representation of the expression.  */
5974
5975 static tree
5976 cp_parser_expression (cp_parser* parser, bool cast_p)
5977 {
5978   tree expression = NULL_TREE;
5979
5980   while (true)
5981     {
5982       tree assignment_expression;
5983
5984       /* Parse the next assignment-expression.  */
5985       assignment_expression
5986         = cp_parser_assignment_expression (parser, cast_p);
5987       /* If this is the first assignment-expression, we can just
5988          save it away.  */
5989       if (!expression)
5990         expression = assignment_expression;
5991       else
5992         expression = build_x_compound_expr (expression,
5993                                             assignment_expression);
5994       /* If the next token is not a comma, then we are done with the
5995          expression.  */
5996       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5997         break;
5998       /* Consume the `,'.  */
5999       cp_lexer_consume_token (parser->lexer);
6000       /* A comma operator cannot appear in a constant-expression.  */
6001       if (cp_parser_non_integral_constant_expression (parser,
6002                                                       "a comma operator"))
6003         expression = error_mark_node;
6004     }
6005
6006   return expression;
6007 }
6008
6009 /* Parse a constant-expression.
6010
6011    constant-expression:
6012      conditional-expression
6013
6014   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6015   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6016   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6017   is false, NON_CONSTANT_P should be NULL.  */
6018
6019 static tree
6020 cp_parser_constant_expression (cp_parser* parser,
6021                                bool allow_non_constant_p,
6022                                bool *non_constant_p)
6023 {
6024   bool saved_integral_constant_expression_p;
6025   bool saved_allow_non_integral_constant_expression_p;
6026   bool saved_non_integral_constant_expression_p;
6027   tree expression;
6028
6029   /* It might seem that we could simply parse the
6030      conditional-expression, and then check to see if it were
6031      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6032      one that the compiler can figure out is constant, possibly after
6033      doing some simplifications or optimizations.  The standard has a
6034      precise definition of constant-expression, and we must honor
6035      that, even though it is somewhat more restrictive.
6036
6037      For example:
6038
6039        int i[(2, 3)];
6040
6041      is not a legal declaration, because `(2, 3)' is not a
6042      constant-expression.  The `,' operator is forbidden in a
6043      constant-expression.  However, GCC's constant-folding machinery
6044      will fold this operation to an INTEGER_CST for `3'.  */
6045
6046   /* Save the old settings.  */
6047   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6048   saved_allow_non_integral_constant_expression_p
6049     = parser->allow_non_integral_constant_expression_p;
6050   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6051   /* We are now parsing a constant-expression.  */
6052   parser->integral_constant_expression_p = true;
6053   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6054   parser->non_integral_constant_expression_p = false;
6055   /* Although the grammar says "conditional-expression", we parse an
6056      "assignment-expression", which also permits "throw-expression"
6057      and the use of assignment operators.  In the case that
6058      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6059      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6060      actually essential that we look for an assignment-expression.
6061      For example, cp_parser_initializer_clauses uses this function to
6062      determine whether a particular assignment-expression is in fact
6063      constant.  */
6064   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6065   /* Restore the old settings.  */
6066   parser->integral_constant_expression_p
6067     = saved_integral_constant_expression_p;
6068   parser->allow_non_integral_constant_expression_p
6069     = saved_allow_non_integral_constant_expression_p;
6070   if (allow_non_constant_p)
6071     *non_constant_p = parser->non_integral_constant_expression_p;
6072   else if (parser->non_integral_constant_expression_p)
6073     expression = error_mark_node;
6074   parser->non_integral_constant_expression_p
6075     = saved_non_integral_constant_expression_p;
6076
6077   return expression;
6078 }
6079
6080 /* Parse __builtin_offsetof.
6081
6082    offsetof-expression:
6083      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6084
6085    offsetof-member-designator:
6086      id-expression
6087      | offsetof-member-designator "." id-expression
6088      | offsetof-member-designator "[" expression "]"  */
6089
6090 static tree
6091 cp_parser_builtin_offsetof (cp_parser *parser)
6092 {
6093   int save_ice_p, save_non_ice_p;
6094   tree type, expr;
6095   cp_id_kind dummy;
6096
6097   /* We're about to accept non-integral-constant things, but will
6098      definitely yield an integral constant expression.  Save and
6099      restore these values around our local parsing.  */
6100   save_ice_p = parser->integral_constant_expression_p;
6101   save_non_ice_p = parser->non_integral_constant_expression_p;
6102
6103   /* Consume the "__builtin_offsetof" token.  */
6104   cp_lexer_consume_token (parser->lexer);
6105   /* Consume the opening `('.  */
6106   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6107   /* Parse the type-id.  */
6108   type = cp_parser_type_id (parser);
6109   /* Look for the `,'.  */
6110   cp_parser_require (parser, CPP_COMMA, "`,'");
6111
6112   /* Build the (type *)null that begins the traditional offsetof macro.  */
6113   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6114
6115   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6116   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6117                                                  true, &dummy);
6118   while (true)
6119     {
6120       cp_token *token = cp_lexer_peek_token (parser->lexer);
6121       switch (token->type)
6122         {
6123         case CPP_OPEN_SQUARE:
6124           /* offsetof-member-designator "[" expression "]" */
6125           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6126           break;
6127
6128         case CPP_DOT:
6129           /* offsetof-member-designator "." identifier */
6130           cp_lexer_consume_token (parser->lexer);
6131           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6132                                                          true, &dummy);
6133           break;
6134
6135         case CPP_CLOSE_PAREN:
6136           /* Consume the ")" token.  */
6137           cp_lexer_consume_token (parser->lexer);
6138           goto success;
6139
6140         default:
6141           /* Error.  We know the following require will fail, but
6142              that gives the proper error message.  */
6143           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6144           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6145           expr = error_mark_node;
6146           goto failure;
6147         }
6148     }
6149
6150  success:
6151   /* If we're processing a template, we can't finish the semantics yet.
6152      Otherwise we can fold the entire expression now.  */
6153   if (processing_template_decl)
6154     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6155   else
6156     expr = finish_offsetof (expr);
6157
6158  failure:
6159   parser->integral_constant_expression_p = save_ice_p;
6160   parser->non_integral_constant_expression_p = save_non_ice_p;
6161
6162   return expr;
6163 }
6164
6165 /* Statements [gram.stmt.stmt]  */
6166
6167 /* Parse a statement.
6168
6169    statement:
6170      labeled-statement
6171      expression-statement
6172      compound-statement
6173      selection-statement
6174      iteration-statement
6175      jump-statement
6176      declaration-statement
6177      try-block
6178
6179   IN_COMPOUND is true when the statement is nested inside a
6180   cp_parser_compound_statement; this matters for certain pragmas.  */
6181
6182 static void
6183 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6184                      bool in_compound)
6185 {
6186   tree statement;
6187   cp_token *token;
6188   location_t statement_location;
6189
6190  restart:
6191   /* There is no statement yet.  */
6192   statement = NULL_TREE;
6193   /* Peek at the next token.  */
6194   token = cp_lexer_peek_token (parser->lexer);
6195   /* Remember the location of the first token in the statement.  */
6196   statement_location = token->location;
6197   /* If this is a keyword, then that will often determine what kind of
6198      statement we have.  */
6199   if (token->type == CPP_KEYWORD)
6200     {
6201       enum rid keyword = token->keyword;
6202
6203       switch (keyword)
6204         {
6205         case RID_CASE:
6206         case RID_DEFAULT:
6207           /* Looks like a labeled-statement with a case label.
6208              Parse the label, and then use tail recursion to parse
6209              the statement.  */
6210           cp_parser_label_for_labeled_statement (parser);
6211           goto restart;
6212
6213         case RID_IF:
6214         case RID_SWITCH:
6215           statement = cp_parser_selection_statement (parser);
6216           break;
6217
6218         case RID_WHILE:
6219         case RID_DO:
6220         case RID_FOR:
6221           statement = cp_parser_iteration_statement (parser);
6222           break;
6223
6224         case RID_BREAK:
6225         case RID_CONTINUE:
6226         case RID_RETURN:
6227         case RID_GOTO:
6228           statement = cp_parser_jump_statement (parser);
6229           break;
6230
6231           /* Objective-C++ exception-handling constructs.  */
6232         case RID_AT_TRY:
6233         case RID_AT_CATCH:
6234         case RID_AT_FINALLY:
6235         case RID_AT_SYNCHRONIZED:
6236         case RID_AT_THROW:
6237           statement = cp_parser_objc_statement (parser);
6238           break;
6239
6240         case RID_TRY:
6241           statement = cp_parser_try_block (parser);
6242           break;
6243
6244         default:
6245           /* It might be a keyword like `int' that can start a
6246              declaration-statement.  */
6247           break;
6248         }
6249     }
6250   else if (token->type == CPP_NAME)
6251     {
6252       /* If the next token is a `:', then we are looking at a
6253          labeled-statement.  */
6254       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6255       if (token->type == CPP_COLON)
6256         {
6257           /* Looks like a labeled-statement with an ordinary label.
6258              Parse the label, and then use tail recursion to parse
6259              the statement.  */
6260           cp_parser_label_for_labeled_statement (parser);
6261           goto restart;
6262         }
6263     }
6264   /* Anything that starts with a `{' must be a compound-statement.  */
6265   else if (token->type == CPP_OPEN_BRACE)
6266     statement = cp_parser_compound_statement (parser, NULL, false);
6267   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6268      a statement all its own.  */
6269   else if (token->type == CPP_PRAGMA)
6270     {
6271       /* Only certain OpenMP pragmas are attached to statements, and thus
6272          are considered statements themselves.  All others are not.  In
6273          the context of a compound, accept the pragma as a "statement" and
6274          return so that we can check for a close brace.  Otherwise we
6275          require a real statement and must go back and read one.  */
6276       if (in_compound)
6277         cp_parser_pragma (parser, pragma_compound);
6278       else if (!cp_parser_pragma (parser, pragma_stmt))
6279         goto restart;
6280       return;
6281     }
6282   else if (token->type == CPP_EOF)
6283     {
6284       cp_parser_error (parser, "expected statement");
6285       return;
6286     }
6287
6288   /* Everything else must be a declaration-statement or an
6289      expression-statement.  Try for the declaration-statement
6290      first, unless we are looking at a `;', in which case we know that
6291      we have an expression-statement.  */
6292   if (!statement)
6293     {
6294       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6295         {
6296           cp_parser_parse_tentatively (parser);
6297           /* Try to parse the declaration-statement.  */
6298           cp_parser_declaration_statement (parser);
6299           /* If that worked, we're done.  */
6300           if (cp_parser_parse_definitely (parser))
6301             return;
6302         }
6303       /* Look for an expression-statement instead.  */
6304       statement = cp_parser_expression_statement (parser, in_statement_expr);
6305     }
6306
6307   /* Set the line number for the statement.  */
6308   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6309     SET_EXPR_LOCATION (statement, statement_location);
6310 }
6311
6312 /* Parse the label for a labeled-statement, i.e.
6313
6314    identifier :
6315    case constant-expression :
6316    default :
6317
6318    GNU Extension:
6319    case constant-expression ... constant-expression : statement
6320
6321    When a label is parsed without errors, the label is added to the
6322    parse tree by the finish_* functions, so this function doesn't
6323    have to return the label.  */
6324
6325 static void
6326 cp_parser_label_for_labeled_statement (cp_parser* parser)
6327 {
6328   cp_token *token;
6329
6330   /* The next token should be an identifier.  */
6331   token = cp_lexer_peek_token (parser->lexer);
6332   if (token->type != CPP_NAME
6333       && token->type != CPP_KEYWORD)
6334     {
6335       cp_parser_error (parser, "expected labeled-statement");
6336       return;
6337     }
6338
6339   switch (token->keyword)
6340     {
6341     case RID_CASE:
6342       {
6343         tree expr, expr_hi;
6344         cp_token *ellipsis;
6345
6346         /* Consume the `case' token.  */
6347         cp_lexer_consume_token (parser->lexer);
6348         /* Parse the constant-expression.  */
6349         expr = cp_parser_constant_expression (parser,
6350                                               /*allow_non_constant_p=*/false,
6351                                               NULL);
6352
6353         ellipsis = cp_lexer_peek_token (parser->lexer);
6354         if (ellipsis->type == CPP_ELLIPSIS)
6355           {
6356             /* Consume the `...' token.  */
6357             cp_lexer_consume_token (parser->lexer);
6358             expr_hi =
6359               cp_parser_constant_expression (parser,
6360                                              /*allow_non_constant_p=*/false,
6361                                              NULL);
6362             /* We don't need to emit warnings here, as the common code
6363                will do this for us.  */
6364           }
6365         else
6366           expr_hi = NULL_TREE;
6367
6368         if (parser->in_switch_statement_p)
6369           finish_case_label (expr, expr_hi);
6370         else
6371           error ("case label %qE not within a switch statement", expr);
6372       }
6373       break;
6374
6375     case RID_DEFAULT:
6376       /* Consume the `default' token.  */
6377       cp_lexer_consume_token (parser->lexer);
6378
6379       if (parser->in_switch_statement_p)
6380         finish_case_label (NULL_TREE, NULL_TREE);
6381       else
6382         error ("case label not within a switch statement");
6383       break;
6384
6385     default:
6386       /* Anything else must be an ordinary label.  */
6387       finish_label_stmt (cp_parser_identifier (parser));
6388       break;
6389     }
6390
6391   /* Require the `:' token.  */
6392   cp_parser_require (parser, CPP_COLON, "`:'");
6393 }
6394
6395 /* Parse an expression-statement.
6396
6397    expression-statement:
6398      expression [opt] ;
6399
6400    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6401    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6402    indicates whether this expression-statement is part of an
6403    expression statement.  */
6404
6405 static tree
6406 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6407 {
6408   tree statement = NULL_TREE;
6409
6410   /* If the next token is a ';', then there is no expression
6411      statement.  */
6412   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6413     statement = cp_parser_expression (parser, /*cast_p=*/false);
6414
6415   /* Consume the final `;'.  */
6416   cp_parser_consume_semicolon_at_end_of_statement (parser);
6417
6418   if (in_statement_expr
6419       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6420     /* This is the final expression statement of a statement
6421        expression.  */
6422     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6423   else if (statement)
6424     statement = finish_expr_stmt (statement);
6425   else
6426     finish_stmt ();
6427
6428   return statement;
6429 }
6430
6431 /* Parse a compound-statement.
6432
6433    compound-statement:
6434      { statement-seq [opt] }
6435
6436    Returns a tree representing the statement.  */
6437
6438 static tree
6439 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6440                               bool in_try)
6441 {
6442   tree compound_stmt;
6443
6444   /* Consume the `{'.  */
6445   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6446     return error_mark_node;
6447   /* Begin the compound-statement.  */
6448   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6449   /* Parse an (optional) statement-seq.  */
6450   cp_parser_statement_seq_opt (parser, in_statement_expr);
6451   /* Finish the compound-statement.  */
6452   finish_compound_stmt (compound_stmt);
6453   /* Consume the `}'.  */
6454   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6455
6456   return compound_stmt;
6457 }
6458
6459 /* Parse an (optional) statement-seq.
6460
6461    statement-seq:
6462      statement
6463      statement-seq [opt] statement  */
6464
6465 static void
6466 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6467 {
6468   /* Scan statements until there aren't any more.  */
6469   while (true)
6470     {
6471       cp_token *token = cp_lexer_peek_token (parser->lexer);
6472
6473       /* If we're looking at a `}', then we've run out of statements.  */
6474       if (token->type == CPP_CLOSE_BRACE
6475           || token->type == CPP_EOF
6476           || token->type == CPP_PRAGMA_EOL)
6477         break;
6478
6479       /* Parse the statement.  */
6480       cp_parser_statement (parser, in_statement_expr, true);
6481     }
6482 }
6483
6484 /* Parse a selection-statement.
6485
6486    selection-statement:
6487      if ( condition ) statement
6488      if ( condition ) statement else statement
6489      switch ( condition ) statement
6490
6491    Returns the new IF_STMT or SWITCH_STMT.  */
6492
6493 static tree
6494 cp_parser_selection_statement (cp_parser* parser)
6495 {
6496   cp_token *token;
6497   enum rid keyword;
6498
6499   /* Peek at the next token.  */
6500   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6501
6502   /* See what kind of keyword it is.  */
6503   keyword = token->keyword;
6504   switch (keyword)
6505     {
6506     case RID_IF:
6507     case RID_SWITCH:
6508       {
6509         tree statement;
6510         tree condition;
6511
6512         /* Look for the `('.  */
6513         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6514           {
6515             cp_parser_skip_to_end_of_statement (parser);
6516             return error_mark_node;
6517           }
6518
6519         /* Begin the selection-statement.  */
6520         if (keyword == RID_IF)
6521           statement = begin_if_stmt ();
6522         else
6523           statement = begin_switch_stmt ();
6524
6525         /* Parse the condition.  */
6526         condition = cp_parser_condition (parser);
6527         /* Look for the `)'.  */
6528         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6529           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6530                                                  /*consume_paren=*/true);
6531
6532         if (keyword == RID_IF)
6533           {
6534             /* Add the condition.  */
6535             finish_if_stmt_cond (condition, statement);
6536
6537             /* Parse the then-clause.  */
6538             cp_parser_implicitly_scoped_statement (parser);
6539             finish_then_clause (statement);
6540
6541             /* If the next token is `else', parse the else-clause.  */
6542             if (cp_lexer_next_token_is_keyword (parser->lexer,
6543                                                 RID_ELSE))
6544               {
6545                 /* Consume the `else' keyword.  */
6546                 cp_lexer_consume_token (parser->lexer);
6547                 begin_else_clause (statement);
6548                 /* Parse the else-clause.  */
6549                 cp_parser_implicitly_scoped_statement (parser);
6550                 finish_else_clause (statement);
6551               }
6552
6553             /* Now we're all done with the if-statement.  */
6554             finish_if_stmt (statement);
6555           }
6556         else
6557           {
6558             bool in_switch_statement_p;
6559             unsigned char in_statement;
6560
6561             /* Add the condition.  */
6562             finish_switch_cond (condition, statement);
6563
6564             /* Parse the body of the switch-statement.  */
6565             in_switch_statement_p = parser->in_switch_statement_p;
6566             in_statement = parser->in_statement;
6567             parser->in_switch_statement_p = true;
6568             parser->in_statement |= IN_SWITCH_STMT;
6569             cp_parser_implicitly_scoped_statement (parser);
6570             parser->in_switch_statement_p = in_switch_statement_p;
6571             parser->in_statement = in_statement;
6572
6573             /* Now we're all done with the switch-statement.  */
6574             finish_switch_stmt (statement);
6575           }
6576
6577         return statement;
6578       }
6579       break;
6580
6581     default:
6582       cp_parser_error (parser, "expected selection-statement");
6583       return error_mark_node;
6584     }
6585 }
6586
6587 /* Parse a condition.
6588
6589    condition:
6590      expression
6591      type-specifier-seq declarator = assignment-expression
6592
6593    GNU Extension:
6594
6595    condition:
6596      type-specifier-seq declarator asm-specification [opt]
6597        attributes [opt] = assignment-expression
6598
6599    Returns the expression that should be tested.  */
6600
6601 static tree
6602 cp_parser_condition (cp_parser* parser)
6603 {
6604   cp_decl_specifier_seq type_specifiers;
6605   const char *saved_message;
6606
6607   /* Try the declaration first.  */
6608   cp_parser_parse_tentatively (parser);
6609   /* New types are not allowed in the type-specifier-seq for a
6610      condition.  */
6611   saved_message = parser->type_definition_forbidden_message;
6612   parser->type_definition_forbidden_message
6613     = "types may not be defined in conditions";
6614   /* Parse the type-specifier-seq.  */
6615   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6616                                 &type_specifiers);
6617   /* Restore the saved message.  */
6618   parser->type_definition_forbidden_message = saved_message;
6619   /* If all is well, we might be looking at a declaration.  */
6620   if (!cp_parser_error_occurred (parser))
6621     {
6622       tree decl;
6623       tree asm_specification;
6624       tree attributes;
6625       cp_declarator *declarator;
6626       tree initializer = NULL_TREE;
6627
6628       /* Parse the declarator.  */
6629       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6630                                          /*ctor_dtor_or_conv_p=*/NULL,
6631                                          /*parenthesized_p=*/NULL,
6632                                          /*member_p=*/false);
6633       /* Parse the attributes.  */
6634       attributes = cp_parser_attributes_opt (parser);
6635       /* Parse the asm-specification.  */
6636       asm_specification = cp_parser_asm_specification_opt (parser);
6637       /* If the next token is not an `=', then we might still be
6638          looking at an expression.  For example:
6639
6640            if (A(a).x)
6641
6642          looks like a decl-specifier-seq and a declarator -- but then
6643          there is no `=', so this is an expression.  */
6644       cp_parser_require (parser, CPP_EQ, "`='");
6645       /* If we did see an `=', then we are looking at a declaration
6646          for sure.  */
6647       if (cp_parser_parse_definitely (parser))
6648         {
6649           tree pushed_scope;
6650           bool non_constant_p;
6651
6652           /* Create the declaration.  */
6653           decl = start_decl (declarator, &type_specifiers,
6654                              /*initialized_p=*/true,
6655                              attributes, /*prefix_attributes=*/NULL_TREE,
6656                              &pushed_scope);
6657           /* Parse the assignment-expression.  */
6658           initializer
6659             = cp_parser_constant_expression (parser,
6660                                              /*allow_non_constant_p=*/true,
6661                                              &non_constant_p);
6662           if (!non_constant_p)
6663             initializer = fold_non_dependent_expr (initializer);
6664
6665           /* Process the initializer.  */
6666           cp_finish_decl (decl,
6667                           initializer, !non_constant_p,
6668                           asm_specification,
6669                           LOOKUP_ONLYCONVERTING);
6670
6671           if (pushed_scope)
6672             pop_scope (pushed_scope);
6673
6674           return convert_from_reference (decl);
6675         }
6676     }
6677   /* If we didn't even get past the declarator successfully, we are
6678      definitely not looking at a declaration.  */
6679   else
6680     cp_parser_abort_tentative_parse (parser);
6681
6682   /* Otherwise, we are looking at an expression.  */
6683   return cp_parser_expression (parser, /*cast_p=*/false);
6684 }
6685
6686 /* Parse an iteration-statement.
6687
6688    iteration-statement:
6689      while ( condition ) statement
6690      do statement while ( expression ) ;
6691      for ( for-init-statement condition [opt] ; expression [opt] )
6692        statement
6693
6694    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6695
6696 static tree
6697 cp_parser_iteration_statement (cp_parser* parser)
6698 {
6699   cp_token *token;
6700   enum rid keyword;
6701   tree statement;
6702   unsigned char in_statement;
6703
6704   /* Peek at the next token.  */
6705   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6706   if (!token)
6707     return error_mark_node;
6708
6709   /* Remember whether or not we are already within an iteration
6710      statement.  */
6711   in_statement = parser->in_statement;
6712
6713   /* See what kind of keyword it is.  */
6714   keyword = token->keyword;
6715   switch (keyword)
6716     {
6717     case RID_WHILE:
6718       {
6719         tree condition;
6720
6721         /* Begin the while-statement.  */
6722         statement = begin_while_stmt ();
6723         /* Look for the `('.  */
6724         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6725         /* Parse the condition.  */
6726         condition = cp_parser_condition (parser);
6727         finish_while_stmt_cond (condition, statement);
6728         /* Look for the `)'.  */
6729         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6730         /* Parse the dependent statement.  */
6731         parser->in_statement = IN_ITERATION_STMT;
6732         cp_parser_already_scoped_statement (parser);
6733         parser->in_statement = in_statement;
6734         /* We're done with the while-statement.  */
6735         finish_while_stmt (statement);
6736       }
6737       break;
6738
6739     case RID_DO:
6740       {
6741         tree expression;
6742
6743         /* Begin the do-statement.  */
6744         statement = begin_do_stmt ();
6745         /* Parse the body of the do-statement.  */
6746         parser->in_statement = IN_ITERATION_STMT;
6747         cp_parser_implicitly_scoped_statement (parser);
6748         parser->in_statement = in_statement;
6749         finish_do_body (statement);
6750         /* Look for the `while' keyword.  */
6751         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6752         /* Look for the `('.  */
6753         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6754         /* Parse the expression.  */
6755         expression = cp_parser_expression (parser, /*cast_p=*/false);
6756         /* We're done with the do-statement.  */
6757         finish_do_stmt (expression, statement);
6758         /* Look for the `)'.  */
6759         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6760         /* Look for the `;'.  */
6761         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6762       }
6763       break;
6764
6765     case RID_FOR:
6766       {
6767         tree condition = NULL_TREE;
6768         tree expression = NULL_TREE;
6769
6770         /* Begin the for-statement.  */
6771         statement = begin_for_stmt ();
6772         /* Look for the `('.  */
6773         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6774         /* Parse the initialization.  */
6775         cp_parser_for_init_statement (parser);
6776         finish_for_init_stmt (statement);
6777
6778         /* If there's a condition, process it.  */
6779         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6780           condition = cp_parser_condition (parser);
6781         finish_for_cond (condition, statement);
6782         /* Look for the `;'.  */
6783         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6784
6785         /* If there's an expression, process it.  */
6786         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6787           expression = cp_parser_expression (parser, /*cast_p=*/false);
6788         finish_for_expr (expression, statement);
6789         /* Look for the `)'.  */
6790         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6791
6792         /* Parse the body of the for-statement.  */
6793         parser->in_statement = IN_ITERATION_STMT;
6794         cp_parser_already_scoped_statement (parser);
6795         parser->in_statement = in_statement;
6796
6797         /* We're done with the for-statement.  */
6798         finish_for_stmt (statement);
6799       }
6800       break;
6801
6802     default:
6803       cp_parser_error (parser, "expected iteration-statement");
6804       statement = error_mark_node;
6805       break;
6806     }
6807
6808   return statement;
6809 }
6810
6811 /* Parse a for-init-statement.
6812
6813    for-init-statement:
6814      expression-statement
6815      simple-declaration  */
6816
6817 static void
6818 cp_parser_for_init_statement (cp_parser* parser)
6819 {
6820   /* If the next token is a `;', then we have an empty
6821      expression-statement.  Grammatically, this is also a
6822      simple-declaration, but an invalid one, because it does not
6823      declare anything.  Therefore, if we did not handle this case
6824      specially, we would issue an error message about an invalid
6825      declaration.  */
6826   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6827     {
6828       /* We're going to speculatively look for a declaration, falling back
6829          to an expression, if necessary.  */
6830       cp_parser_parse_tentatively (parser);
6831       /* Parse the declaration.  */
6832       cp_parser_simple_declaration (parser,
6833                                     /*function_definition_allowed_p=*/false);
6834       /* If the tentative parse failed, then we shall need to look for an
6835          expression-statement.  */
6836       if (cp_parser_parse_definitely (parser))
6837         return;
6838     }
6839
6840   cp_parser_expression_statement (parser, false);
6841 }
6842
6843 /* Parse a jump-statement.
6844
6845    jump-statement:
6846      break ;
6847      continue ;
6848      return expression [opt] ;
6849      goto identifier ;
6850
6851    GNU extension:
6852
6853    jump-statement:
6854      goto * expression ;
6855
6856    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6857
6858 static tree
6859 cp_parser_jump_statement (cp_parser* parser)
6860 {
6861   tree statement = error_mark_node;
6862   cp_token *token;
6863   enum rid keyword;
6864
6865   /* Peek at the next token.  */
6866   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6867   if (!token)
6868     return error_mark_node;
6869
6870   /* See what kind of keyword it is.  */
6871   keyword = token->keyword;
6872   switch (keyword)
6873     {
6874     case RID_BREAK:
6875       switch (parser->in_statement)
6876         {
6877         case 0:
6878           error ("break statement not within loop or switch");
6879           break;
6880         default:
6881           gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6882                       || parser->in_statement == IN_ITERATION_STMT);
6883           statement = finish_break_stmt ();
6884           break;
6885         case IN_OMP_BLOCK:
6886           error ("invalid exit from OpenMP structured block");
6887           break;
6888         case IN_OMP_FOR:
6889           error ("break statement used with OpenMP for loop");
6890           break;
6891         }
6892       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6893       break;
6894
6895     case RID_CONTINUE:
6896       switch (parser->in_statement & ~IN_SWITCH_STMT)
6897         {
6898         case 0:
6899           error ("continue statement not within a loop");
6900           break;
6901         case IN_ITERATION_STMT:
6902         case IN_OMP_FOR:
6903           statement = finish_continue_stmt ();
6904           break;
6905         case IN_OMP_BLOCK:
6906           error ("invalid exit from OpenMP structured block");
6907           break;
6908         default:
6909           gcc_unreachable ();
6910         }
6911       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6912       break;
6913
6914     case RID_RETURN:
6915       {
6916         tree expr;
6917
6918         /* If the next token is a `;', then there is no
6919            expression.  */
6920         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6921           expr = cp_parser_expression (parser, /*cast_p=*/false);
6922         else
6923           expr = NULL_TREE;
6924         /* Build the return-statement.  */
6925         statement = finish_return_stmt (expr);
6926         /* Look for the final `;'.  */
6927         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6928       }
6929       break;
6930
6931     case RID_GOTO:
6932       /* Create the goto-statement.  */
6933       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6934         {
6935           /* Issue a warning about this use of a GNU extension.  */
6936           if (pedantic)
6937             pedwarn ("ISO C++ forbids computed gotos");
6938           /* Consume the '*' token.  */
6939           cp_lexer_consume_token (parser->lexer);
6940           /* Parse the dependent expression.  */
6941           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6942         }
6943       else
6944         finish_goto_stmt (cp_parser_identifier (parser));
6945       /* Look for the final `;'.  */
6946       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6947       break;
6948
6949     default:
6950       cp_parser_error (parser, "expected jump-statement");
6951       break;
6952     }
6953
6954   return statement;
6955 }
6956
6957 /* Parse a declaration-statement.
6958
6959    declaration-statement:
6960      block-declaration  */
6961
6962 static void
6963 cp_parser_declaration_statement (cp_parser* parser)
6964 {
6965   void *p;
6966
6967   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6968   p = obstack_alloc (&declarator_obstack, 0);
6969
6970  /* Parse the block-declaration.  */
6971   cp_parser_block_declaration (parser, /*statement_p=*/true);
6972
6973   /* Free any declarators allocated.  */
6974   obstack_free (&declarator_obstack, p);
6975
6976   /* Finish off the statement.  */
6977   finish_stmt ();
6978 }
6979
6980 /* Some dependent statements (like `if (cond) statement'), are
6981    implicitly in their own scope.  In other words, if the statement is
6982    a single statement (as opposed to a compound-statement), it is
6983    none-the-less treated as if it were enclosed in braces.  Any
6984    declarations appearing in the dependent statement are out of scope
6985    after control passes that point.  This function parses a statement,
6986    but ensures that is in its own scope, even if it is not a
6987    compound-statement.
6988
6989    Returns the new statement.  */
6990
6991 static tree
6992 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6993 {
6994   tree statement;
6995
6996   /* Mark if () ; with a special NOP_EXPR.  */
6997   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6998     {
6999       cp_lexer_consume_token (parser->lexer);
7000       statement = add_stmt (build_empty_stmt ());
7001     }
7002   /* if a compound is opened, we simply parse the statement directly.  */
7003   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7004     statement = cp_parser_compound_statement (parser, NULL, false);
7005   /* If the token is not a `{', then we must take special action.  */
7006   else
7007     {
7008       /* Create a compound-statement.  */
7009       statement = begin_compound_stmt (0);
7010       /* Parse the dependent-statement.  */
7011       cp_parser_statement (parser, NULL_TREE, false);
7012       /* Finish the dummy compound-statement.  */
7013       finish_compound_stmt (statement);
7014     }
7015
7016   /* Return the statement.  */
7017   return statement;
7018 }
7019
7020 /* For some dependent statements (like `while (cond) statement'), we
7021    have already created a scope.  Therefore, even if the dependent
7022    statement is a compound-statement, we do not want to create another
7023    scope.  */
7024
7025 static void
7026 cp_parser_already_scoped_statement (cp_parser* parser)
7027 {
7028   /* If the token is a `{', then we must take special action.  */
7029   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7030     cp_parser_statement (parser, NULL_TREE, false);
7031   else
7032     {
7033       /* Avoid calling cp_parser_compound_statement, so that we
7034          don't create a new scope.  Do everything else by hand.  */
7035       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7036       cp_parser_statement_seq_opt (parser, NULL_TREE);
7037       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7038     }
7039 }
7040
7041 /* Declarations [gram.dcl.dcl] */
7042
7043 /* Parse an optional declaration-sequence.
7044
7045    declaration-seq:
7046      declaration
7047      declaration-seq declaration  */
7048
7049 static void
7050 cp_parser_declaration_seq_opt (cp_parser* parser)
7051 {
7052   while (true)
7053     {
7054       cp_token *token;
7055
7056       token = cp_lexer_peek_token (parser->lexer);
7057
7058       if (token->type == CPP_CLOSE_BRACE
7059           || token->type == CPP_EOF
7060           || token->type == CPP_PRAGMA_EOL)
7061         break;
7062
7063       if (token->type == CPP_SEMICOLON)
7064         {
7065           /* A declaration consisting of a single semicolon is
7066              invalid.  Allow it unless we're being pedantic.  */
7067           cp_lexer_consume_token (parser->lexer);
7068           if (pedantic && !in_system_header)
7069             pedwarn ("extra %<;%>");
7070           continue;
7071         }
7072
7073       /* If we're entering or exiting a region that's implicitly
7074          extern "C", modify the lang context appropriately.  */
7075       if (!parser->implicit_extern_c && token->implicit_extern_c)
7076         {
7077           push_lang_context (lang_name_c);
7078           parser->implicit_extern_c = true;
7079         }
7080       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7081         {
7082           pop_lang_context ();
7083           parser->implicit_extern_c = false;
7084         }
7085
7086       if (token->type == CPP_PRAGMA)
7087         {
7088           /* A top-level declaration can consist solely of a #pragma.
7089              A nested declaration cannot, so this is done here and not
7090              in cp_parser_declaration.  (A #pragma at block scope is
7091              handled in cp_parser_statement.)  */
7092           cp_parser_pragma (parser, pragma_external);
7093           continue;
7094         }
7095
7096       /* Parse the declaration itself.  */
7097       cp_parser_declaration (parser);
7098     }
7099 }
7100
7101 /* Parse a declaration.
7102
7103    declaration:
7104      block-declaration
7105      function-definition
7106      template-declaration
7107      explicit-instantiation
7108      explicit-specialization
7109      linkage-specification
7110      namespace-definition
7111
7112    GNU extension:
7113
7114    declaration:
7115       __extension__ declaration */
7116
7117 static void
7118 cp_parser_declaration (cp_parser* parser)
7119 {
7120   cp_token token1;
7121   cp_token token2;
7122   int saved_pedantic;
7123   void *p;
7124
7125   /* Check for the `__extension__' keyword.  */
7126   if (cp_parser_extension_opt (parser, &saved_pedantic))
7127     {
7128       /* Parse the qualified declaration.  */
7129       cp_parser_declaration (parser);
7130       /* Restore the PEDANTIC flag.  */
7131       pedantic = saved_pedantic;
7132
7133       return;
7134     }
7135
7136   /* Try to figure out what kind of declaration is present.  */
7137   token1 = *cp_lexer_peek_token (parser->lexer);
7138
7139   if (token1.type != CPP_EOF)
7140     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7141   else
7142     {
7143       token2.type = CPP_EOF;
7144       token2.keyword = RID_MAX;
7145     }
7146
7147   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7148   p = obstack_alloc (&declarator_obstack, 0);
7149
7150   /* If the next token is `extern' and the following token is a string
7151      literal, then we have a linkage specification.  */
7152   if (token1.keyword == RID_EXTERN
7153       && cp_parser_is_string_literal (&token2))
7154     cp_parser_linkage_specification (parser);
7155   /* If the next token is `template', then we have either a template
7156      declaration, an explicit instantiation, or an explicit
7157      specialization.  */
7158   else if (token1.keyword == RID_TEMPLATE)
7159     {
7160       /* `template <>' indicates a template specialization.  */
7161       if (token2.type == CPP_LESS
7162           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7163         cp_parser_explicit_specialization (parser);
7164       /* `template <' indicates a template declaration.  */
7165       else if (token2.type == CPP_LESS)
7166         cp_parser_template_declaration (parser, /*member_p=*/false);
7167       /* Anything else must be an explicit instantiation.  */
7168       else
7169         cp_parser_explicit_instantiation (parser);
7170     }
7171   /* If the next token is `export', then we have a template
7172      declaration.  */
7173   else if (token1.keyword == RID_EXPORT)
7174     cp_parser_template_declaration (parser, /*member_p=*/false);
7175   /* If the next token is `extern', 'static' or 'inline' and the one
7176      after that is `template', we have a GNU extended explicit
7177      instantiation directive.  */
7178   else if (cp_parser_allow_gnu_extensions_p (parser)
7179            && (token1.keyword == RID_EXTERN
7180                || token1.keyword == RID_STATIC
7181                || token1.keyword == RID_INLINE)
7182            && token2.keyword == RID_TEMPLATE)
7183     cp_parser_explicit_instantiation (parser);
7184   /* If the next token is `namespace', check for a named or unnamed
7185      namespace definition.  */
7186   else if (token1.keyword == RID_NAMESPACE
7187            && (/* A named namespace definition.  */
7188                (token2.type == CPP_NAME
7189                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7190                     != CPP_EQ))
7191                /* An unnamed namespace definition.  */
7192                || token2.type == CPP_OPEN_BRACE
7193                || token2.keyword == RID_ATTRIBUTE))
7194     cp_parser_namespace_definition (parser);
7195   /* Objective-C++ declaration/definition.  */
7196   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7197     cp_parser_objc_declaration (parser);
7198   /* We must have either a block declaration or a function
7199      definition.  */
7200   else
7201     /* Try to parse a block-declaration, or a function-definition.  */
7202     cp_parser_block_declaration (parser, /*statement_p=*/false);
7203
7204   /* Free any declarators allocated.  */
7205   obstack_free (&declarator_obstack, p);
7206 }
7207
7208 /* Parse a block-declaration.
7209
7210    block-declaration:
7211      simple-declaration
7212      asm-definition
7213      namespace-alias-definition
7214      using-declaration
7215      using-directive
7216
7217    GNU Extension:
7218
7219    block-declaration:
7220      __extension__ block-declaration
7221      label-declaration
7222
7223    C++0x Extension:
7224
7225    block-declaration:
7226      static_assert-declaration
7227
7228    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7229    part of a declaration-statement.  */
7230
7231 static void
7232 cp_parser_block_declaration (cp_parser *parser,
7233                              bool      statement_p)
7234 {
7235   cp_token *token1;
7236   int saved_pedantic;
7237
7238   /* Check for the `__extension__' keyword.  */
7239   if (cp_parser_extension_opt (parser, &saved_pedantic))
7240     {
7241       /* Parse the qualified declaration.  */
7242       cp_parser_block_declaration (parser, statement_p);
7243       /* Restore the PEDANTIC flag.  */
7244       pedantic = saved_pedantic;
7245
7246       return;
7247     }
7248
7249   /* Peek at the next token to figure out which kind of declaration is
7250      present.  */
7251   token1 = cp_lexer_peek_token (parser->lexer);
7252
7253   /* If the next keyword is `asm', we have an asm-definition.  */
7254   if (token1->keyword == RID_ASM)
7255     {
7256       if (statement_p)
7257         cp_parser_commit_to_tentative_parse (parser);
7258       cp_parser_asm_definition (parser);
7259     }
7260   /* If the next keyword is `namespace', we have a
7261      namespace-alias-definition.  */
7262   else if (token1->keyword == RID_NAMESPACE)
7263     cp_parser_namespace_alias_definition (parser);
7264   /* If the next keyword is `using', we have either a
7265      using-declaration or a using-directive.  */
7266   else if (token1->keyword == RID_USING)
7267     {
7268       cp_token *token2;
7269
7270       if (statement_p)
7271         cp_parser_commit_to_tentative_parse (parser);
7272       /* If the token after `using' is `namespace', then we have a
7273          using-directive.  */
7274       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7275       if (token2->keyword == RID_NAMESPACE)
7276         cp_parser_using_directive (parser);
7277       /* Otherwise, it's a using-declaration.  */
7278       else
7279         cp_parser_using_declaration (parser,
7280                                      /*access_declaration_p=*/false);
7281     }
7282   /* If the next keyword is `__label__' we have a label declaration.  */
7283   else if (token1->keyword == RID_LABEL)
7284     {
7285       if (statement_p)
7286         cp_parser_commit_to_tentative_parse (parser);
7287       cp_parser_label_declaration (parser);
7288     }
7289   /* If the next token is `static_assert' we have a static assertion.  */
7290   else if (token1->keyword == RID_STATIC_ASSERT)
7291     cp_parser_static_assert (parser, /*member_p=*/false);
7292   /* Anything else must be a simple-declaration.  */
7293   else
7294     cp_parser_simple_declaration (parser, !statement_p);
7295 }
7296
7297 /* Parse a simple-declaration.
7298
7299    simple-declaration:
7300      decl-specifier-seq [opt] init-declarator-list [opt] ;
7301
7302    init-declarator-list:
7303      init-declarator
7304      init-declarator-list , init-declarator
7305
7306    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7307    function-definition as a simple-declaration.  */
7308
7309 static void
7310 cp_parser_simple_declaration (cp_parser* parser,
7311                               bool function_definition_allowed_p)
7312 {
7313   cp_decl_specifier_seq decl_specifiers;
7314   int declares_class_or_enum;
7315   bool saw_declarator;
7316
7317   /* Defer access checks until we know what is being declared; the
7318      checks for names appearing in the decl-specifier-seq should be
7319      done as if we were in the scope of the thing being declared.  */
7320   push_deferring_access_checks (dk_deferred);
7321
7322   /* Parse the decl-specifier-seq.  We have to keep track of whether
7323      or not the decl-specifier-seq declares a named class or
7324      enumeration type, since that is the only case in which the
7325      init-declarator-list is allowed to be empty.
7326
7327      [dcl.dcl]
7328
7329      In a simple-declaration, the optional init-declarator-list can be
7330      omitted only when declaring a class or enumeration, that is when
7331      the decl-specifier-seq contains either a class-specifier, an
7332      elaborated-type-specifier, or an enum-specifier.  */
7333   cp_parser_decl_specifier_seq (parser,
7334                                 CP_PARSER_FLAGS_OPTIONAL,
7335                                 &decl_specifiers,
7336                                 &declares_class_or_enum);
7337   /* We no longer need to defer access checks.  */
7338   stop_deferring_access_checks ();
7339
7340   /* In a block scope, a valid declaration must always have a
7341      decl-specifier-seq.  By not trying to parse declarators, we can
7342      resolve the declaration/expression ambiguity more quickly.  */
7343   if (!function_definition_allowed_p
7344       && !decl_specifiers.any_specifiers_p)
7345     {
7346       cp_parser_error (parser, "expected declaration");
7347       goto done;
7348     }
7349
7350   /* If the next two tokens are both identifiers, the code is
7351      erroneous. The usual cause of this situation is code like:
7352
7353        T t;
7354
7355      where "T" should name a type -- but does not.  */
7356   if (!decl_specifiers.type
7357       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7358     {
7359       /* If parsing tentatively, we should commit; we really are
7360          looking at a declaration.  */
7361       cp_parser_commit_to_tentative_parse (parser);
7362       /* Give up.  */
7363       goto done;
7364     }
7365
7366   /* If we have seen at least one decl-specifier, and the next token
7367      is not a parenthesis, then we must be looking at a declaration.
7368      (After "int (" we might be looking at a functional cast.)  */
7369   if (decl_specifiers.any_specifiers_p
7370       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7371     cp_parser_commit_to_tentative_parse (parser);
7372
7373   /* Keep going until we hit the `;' at the end of the simple
7374      declaration.  */
7375   saw_declarator = false;
7376   while (cp_lexer_next_token_is_not (parser->lexer,
7377                                      CPP_SEMICOLON))
7378     {
7379       cp_token *token;
7380       bool function_definition_p;
7381       tree decl;
7382
7383       if (saw_declarator)
7384         {
7385           /* If we are processing next declarator, coma is expected */
7386           token = cp_lexer_peek_token (parser->lexer);
7387           gcc_assert (token->type == CPP_COMMA);
7388           cp_lexer_consume_token (parser->lexer);
7389         }
7390       else
7391         saw_declarator = true;
7392
7393       /* Parse the init-declarator.  */
7394       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7395                                         /*checks=*/NULL_TREE,
7396                                         function_definition_allowed_p,
7397                                         /*member_p=*/false,
7398                                         declares_class_or_enum,
7399                                         &function_definition_p);
7400       /* If an error occurred while parsing tentatively, exit quickly.
7401          (That usually happens when in the body of a function; each
7402          statement is treated as a declaration-statement until proven
7403          otherwise.)  */
7404       if (cp_parser_error_occurred (parser))
7405         goto done;
7406       /* Handle function definitions specially.  */
7407       if (function_definition_p)
7408         {
7409           /* If the next token is a `,', then we are probably
7410              processing something like:
7411
7412                void f() {}, *p;
7413
7414              which is erroneous.  */
7415           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7416             error ("mixing declarations and function-definitions is forbidden");
7417           /* Otherwise, we're done with the list of declarators.  */
7418           else
7419             {
7420               pop_deferring_access_checks ();
7421               return;
7422             }
7423         }
7424       /* The next token should be either a `,' or a `;'.  */
7425       token = cp_lexer_peek_token (parser->lexer);
7426       /* If it's a `,', there are more declarators to come.  */
7427       if (token->type == CPP_COMMA)
7428         /* will be consumed next time around */;
7429       /* If it's a `;', we are done.  */
7430       else if (token->type == CPP_SEMICOLON)
7431         break;
7432       /* Anything else is an error.  */
7433       else
7434         {
7435           /* If we have already issued an error message we don't need
7436              to issue another one.  */
7437           if (decl != error_mark_node
7438               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7439             cp_parser_error (parser, "expected %<,%> or %<;%>");
7440           /* Skip tokens until we reach the end of the statement.  */
7441           cp_parser_skip_to_end_of_statement (parser);
7442           /* If the next token is now a `;', consume it.  */
7443           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7444             cp_lexer_consume_token (parser->lexer);
7445           goto done;
7446         }
7447       /* After the first time around, a function-definition is not
7448          allowed -- even if it was OK at first.  For example:
7449
7450            int i, f() {}
7451
7452          is not valid.  */
7453       function_definition_allowed_p = false;
7454     }
7455
7456   /* Issue an error message if no declarators are present, and the
7457      decl-specifier-seq does not itself declare a class or
7458      enumeration.  */
7459   if (!saw_declarator)
7460     {
7461       if (cp_parser_declares_only_class_p (parser))
7462         shadow_tag (&decl_specifiers);
7463       /* Perform any deferred access checks.  */
7464       perform_deferred_access_checks ();
7465     }
7466
7467   /* Consume the `;'.  */
7468   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7469
7470  done:
7471   pop_deferring_access_checks ();
7472 }
7473
7474 /* Parse a decl-specifier-seq.
7475
7476    decl-specifier-seq:
7477      decl-specifier-seq [opt] decl-specifier
7478
7479    decl-specifier:
7480      storage-class-specifier
7481      type-specifier
7482      function-specifier
7483      friend
7484      typedef
7485
7486    GNU Extension:
7487
7488    decl-specifier:
7489      attributes
7490
7491    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7492
7493    The parser flags FLAGS is used to control type-specifier parsing.
7494
7495    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7496    flags:
7497
7498      1: one of the decl-specifiers is an elaborated-type-specifier
7499         (i.e., a type declaration)
7500      2: one of the decl-specifiers is an enum-specifier or a
7501         class-specifier (i.e., a type definition)
7502
7503    */
7504
7505 static void
7506 cp_parser_decl_specifier_seq (cp_parser* parser,
7507                               cp_parser_flags flags,
7508                               cp_decl_specifier_seq *decl_specs,
7509                               int* declares_class_or_enum)
7510 {
7511   bool constructor_possible_p = !parser->in_declarator_p;
7512
7513   /* Clear DECL_SPECS.  */
7514   clear_decl_specs (decl_specs);
7515
7516   /* Assume no class or enumeration type is declared.  */
7517   *declares_class_or_enum = 0;
7518
7519   /* Keep reading specifiers until there are no more to read.  */
7520   while (true)
7521     {
7522       bool constructor_p;
7523       bool found_decl_spec;
7524       cp_token *token;
7525
7526       /* Peek at the next token.  */
7527       token = cp_lexer_peek_token (parser->lexer);
7528       /* Handle attributes.  */
7529       if (token->keyword == RID_ATTRIBUTE)
7530         {
7531           /* Parse the attributes.  */
7532           decl_specs->attributes
7533             = chainon (decl_specs->attributes,
7534                        cp_parser_attributes_opt (parser));
7535           continue;
7536         }
7537       /* Assume we will find a decl-specifier keyword.  */
7538       found_decl_spec = true;
7539       /* If the next token is an appropriate keyword, we can simply
7540          add it to the list.  */
7541       switch (token->keyword)
7542         {
7543           /* decl-specifier:
7544                friend  */
7545         case RID_FRIEND:
7546           if (!at_class_scope_p ())
7547             {
7548               error ("%<friend%> used outside of class");
7549               cp_lexer_purge_token (parser->lexer);
7550             }
7551           else
7552             {
7553               ++decl_specs->specs[(int) ds_friend];
7554               /* Consume the token.  */
7555               cp_lexer_consume_token (parser->lexer);
7556             }
7557           break;
7558
7559           /* function-specifier:
7560                inline
7561                virtual
7562                explicit  */
7563         case RID_INLINE:
7564         case RID_VIRTUAL:
7565         case RID_EXPLICIT:
7566           cp_parser_function_specifier_opt (parser, decl_specs);
7567           break;
7568
7569           /* decl-specifier:
7570                typedef  */
7571         case RID_TYPEDEF:
7572           ++decl_specs->specs[(int) ds_typedef];
7573           /* Consume the token.  */
7574           cp_lexer_consume_token (parser->lexer);
7575           /* A constructor declarator cannot appear in a typedef.  */
7576           constructor_possible_p = false;
7577           /* The "typedef" keyword can only occur in a declaration; we
7578              may as well commit at this point.  */
7579           cp_parser_commit_to_tentative_parse (parser);
7580
7581           if (decl_specs->storage_class != sc_none)
7582             decl_specs->conflicting_specifiers_p = true;
7583           break;
7584
7585           /* storage-class-specifier:
7586                auto
7587                register
7588                static
7589                extern
7590                mutable
7591
7592              GNU Extension:
7593                thread  */
7594         case RID_AUTO:
7595         case RID_REGISTER:
7596         case RID_STATIC:
7597         case RID_EXTERN:
7598         case RID_MUTABLE:
7599           /* Consume the token.  */
7600           cp_lexer_consume_token (parser->lexer);
7601           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7602           break;
7603         case RID_THREAD:
7604           /* Consume the token.  */
7605           cp_lexer_consume_token (parser->lexer);
7606           ++decl_specs->specs[(int) ds_thread];
7607           break;
7608
7609         default:
7610           /* We did not yet find a decl-specifier yet.  */
7611           found_decl_spec = false;
7612           break;
7613         }
7614
7615       /* Constructors are a special case.  The `S' in `S()' is not a
7616          decl-specifier; it is the beginning of the declarator.  */
7617       constructor_p
7618         = (!found_decl_spec
7619            && constructor_possible_p
7620            && (cp_parser_constructor_declarator_p
7621                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7622
7623       /* If we don't have a DECL_SPEC yet, then we must be looking at
7624          a type-specifier.  */
7625       if (!found_decl_spec && !constructor_p)
7626         {
7627           int decl_spec_declares_class_or_enum;
7628           bool is_cv_qualifier;
7629           tree type_spec;
7630
7631           type_spec
7632             = cp_parser_type_specifier (parser, flags,
7633                                         decl_specs,
7634                                         /*is_declaration=*/true,
7635                                         &decl_spec_declares_class_or_enum,
7636                                         &is_cv_qualifier);
7637
7638           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7639
7640           /* If this type-specifier referenced a user-defined type
7641              (a typedef, class-name, etc.), then we can't allow any
7642              more such type-specifiers henceforth.
7643
7644              [dcl.spec]
7645
7646              The longest sequence of decl-specifiers that could
7647              possibly be a type name is taken as the
7648              decl-specifier-seq of a declaration.  The sequence shall
7649              be self-consistent as described below.
7650
7651              [dcl.type]
7652
7653              As a general rule, at most one type-specifier is allowed
7654              in the complete decl-specifier-seq of a declaration.  The
7655              only exceptions are the following:
7656
7657              -- const or volatile can be combined with any other
7658                 type-specifier.
7659
7660              -- signed or unsigned can be combined with char, long,
7661                 short, or int.
7662
7663              -- ..
7664
7665              Example:
7666
7667                typedef char* Pc;
7668                void g (const int Pc);
7669
7670              Here, Pc is *not* part of the decl-specifier seq; it's
7671              the declarator.  Therefore, once we see a type-specifier
7672              (other than a cv-qualifier), we forbid any additional
7673              user-defined types.  We *do* still allow things like `int
7674              int' to be considered a decl-specifier-seq, and issue the
7675              error message later.  */
7676           if (type_spec && !is_cv_qualifier)
7677             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7678           /* A constructor declarator cannot follow a type-specifier.  */
7679           if (type_spec)
7680             {
7681               constructor_possible_p = false;
7682               found_decl_spec = true;
7683             }
7684         }
7685
7686       /* If we still do not have a DECL_SPEC, then there are no more
7687          decl-specifiers.  */
7688       if (!found_decl_spec)
7689         break;
7690
7691       decl_specs->any_specifiers_p = true;
7692       /* After we see one decl-specifier, further decl-specifiers are
7693          always optional.  */
7694       flags |= CP_PARSER_FLAGS_OPTIONAL;
7695     }
7696
7697   cp_parser_check_decl_spec (decl_specs);
7698
7699   /* Don't allow a friend specifier with a class definition.  */
7700   if (decl_specs->specs[(int) ds_friend] != 0
7701       && (*declares_class_or_enum & 2))
7702     error ("class definition may not be declared a friend");
7703 }
7704
7705 /* Parse an (optional) storage-class-specifier.
7706
7707    storage-class-specifier:
7708      auto
7709      register
7710      static
7711      extern
7712      mutable
7713
7714    GNU Extension:
7715
7716    storage-class-specifier:
7717      thread
7718
7719    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7720
7721 static tree
7722 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7723 {
7724   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7725     {
7726     case RID_AUTO:
7727     case RID_REGISTER:
7728     case RID_STATIC:
7729     case RID_EXTERN:
7730     case RID_MUTABLE:
7731     case RID_THREAD:
7732       /* Consume the token.  */
7733       return cp_lexer_consume_token (parser->lexer)->value;
7734
7735     default:
7736       return NULL_TREE;
7737     }
7738 }
7739
7740 /* Parse an (optional) function-specifier.
7741
7742    function-specifier:
7743      inline
7744      virtual
7745      explicit
7746
7747    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7748    Updates DECL_SPECS, if it is non-NULL.  */
7749
7750 static tree
7751 cp_parser_function_specifier_opt (cp_parser* parser,
7752                                   cp_decl_specifier_seq *decl_specs)
7753 {
7754   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7755     {
7756     case RID_INLINE:
7757       if (decl_specs)
7758         ++decl_specs->specs[(int) ds_inline];
7759       break;
7760
7761     case RID_VIRTUAL:
7762       /* 14.5.2.3 [temp.mem]
7763
7764          A member function template shall not be virtual.  */
7765       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7766         error ("templates may not be %<virtual%>");
7767       else if (decl_specs)
7768         ++decl_specs->specs[(int) ds_virtual];
7769       break;
7770
7771     case RID_EXPLICIT:
7772       if (decl_specs)
7773         ++decl_specs->specs[(int) ds_explicit];
7774       break;
7775
7776     default:
7777       return NULL_TREE;
7778     }
7779
7780   /* Consume the token.  */
7781   return cp_lexer_consume_token (parser->lexer)->value;
7782 }
7783
7784 /* Parse a linkage-specification.
7785
7786    linkage-specification:
7787      extern string-literal { declaration-seq [opt] }
7788      extern string-literal declaration  */
7789
7790 static void
7791 cp_parser_linkage_specification (cp_parser* parser)
7792 {
7793   tree linkage;
7794
7795   /* Look for the `extern' keyword.  */
7796   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7797
7798   /* Look for the string-literal.  */
7799   linkage = cp_parser_string_literal (parser, false, false);
7800
7801   /* Transform the literal into an identifier.  If the literal is a
7802      wide-character string, or contains embedded NULs, then we can't
7803      handle it as the user wants.  */
7804   if (strlen (TREE_STRING_POINTER (linkage))
7805       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7806     {
7807       cp_parser_error (parser, "invalid linkage-specification");
7808       /* Assume C++ linkage.  */
7809       linkage = lang_name_cplusplus;
7810     }
7811   else
7812     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7813
7814   /* We're now using the new linkage.  */
7815   push_lang_context (linkage);
7816
7817   /* If the next token is a `{', then we're using the first
7818      production.  */
7819   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7820     {
7821       /* Consume the `{' token.  */
7822       cp_lexer_consume_token (parser->lexer);
7823       /* Parse the declarations.  */
7824       cp_parser_declaration_seq_opt (parser);
7825       /* Look for the closing `}'.  */
7826       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7827     }
7828   /* Otherwise, there's just one declaration.  */
7829   else
7830     {
7831       bool saved_in_unbraced_linkage_specification_p;
7832
7833       saved_in_unbraced_linkage_specification_p
7834         = parser->in_unbraced_linkage_specification_p;
7835       parser->in_unbraced_linkage_specification_p = true;
7836       cp_parser_declaration (parser);
7837       parser->in_unbraced_linkage_specification_p
7838         = saved_in_unbraced_linkage_specification_p;
7839     }
7840
7841   /* We're done with the linkage-specification.  */
7842   pop_lang_context ();
7843 }
7844
7845 /* Parse a static_assert-declaration.
7846
7847    static_assert-declaration:
7848      static_assert ( constant-expression , string-literal ) ; 
7849
7850    If MEMBER_P, this static_assert is a class member.  */
7851
7852 static void 
7853 cp_parser_static_assert(cp_parser *parser, bool member_p)
7854 {
7855   tree condition;
7856   tree message;
7857   cp_token *token;
7858   location_t saved_loc;
7859
7860   /* Peek at the `static_assert' token so we can keep track of exactly
7861      where the static assertion started.  */
7862   token = cp_lexer_peek_token (parser->lexer);
7863   saved_loc = token->location;
7864
7865   /* Look for the `static_assert' keyword.  */
7866   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
7867                                   "`static_assert'"))
7868     return;
7869
7870   /*  We know we are in a static assertion; commit to any tentative
7871       parse.  */
7872   if (cp_parser_parsing_tentatively (parser))
7873     cp_parser_commit_to_tentative_parse (parser);
7874
7875   /* Parse the `(' starting the static assertion condition.  */
7876   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7877
7878   /* Parse the constant-expression.  */
7879   condition = 
7880     cp_parser_constant_expression (parser,
7881                                    /*allow_non_constant_p=*/false,
7882                                    /*non_constant_p=*/NULL);
7883
7884   /* Parse the separating `,'.  */
7885   cp_parser_require (parser, CPP_COMMA, "`,'");
7886
7887   /* Parse the string-literal message.  */
7888   message = cp_parser_string_literal (parser, 
7889                                       /*translate=*/false,
7890                                       /*wide_ok=*/true);
7891
7892   /* A `)' completes the static assertion.  */
7893   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7894     cp_parser_skip_to_closing_parenthesis (parser, 
7895                                            /*recovering=*/true, 
7896                                            /*or_comma=*/false,
7897                                            /*consume_paren=*/true);
7898
7899   /* A semicolon terminates the declaration.  */
7900   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7901
7902   /* Complete the static assertion, which may mean either processing 
7903      the static assert now or saving it for template instantiation.  */
7904   finish_static_assert (condition, message, saved_loc, member_p);
7905 }
7906
7907 /* Special member functions [gram.special] */
7908
7909 /* Parse a conversion-function-id.
7910
7911    conversion-function-id:
7912      operator conversion-type-id
7913
7914    Returns an IDENTIFIER_NODE representing the operator.  */
7915
7916 static tree
7917 cp_parser_conversion_function_id (cp_parser* parser)
7918 {
7919   tree type;
7920   tree saved_scope;
7921   tree saved_qualifying_scope;
7922   tree saved_object_scope;
7923   tree pushed_scope = NULL_TREE;
7924
7925   /* Look for the `operator' token.  */
7926   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7927     return error_mark_node;
7928   /* When we parse the conversion-type-id, the current scope will be
7929      reset.  However, we need that information in able to look up the
7930      conversion function later, so we save it here.  */
7931   saved_scope = parser->scope;
7932   saved_qualifying_scope = parser->qualifying_scope;
7933   saved_object_scope = parser->object_scope;
7934   /* We must enter the scope of the class so that the names of
7935      entities declared within the class are available in the
7936      conversion-type-id.  For example, consider:
7937
7938        struct S {
7939          typedef int I;
7940          operator I();
7941        };
7942
7943        S::operator I() { ... }
7944
7945      In order to see that `I' is a type-name in the definition, we
7946      must be in the scope of `S'.  */
7947   if (saved_scope)
7948     pushed_scope = push_scope (saved_scope);
7949   /* Parse the conversion-type-id.  */
7950   type = cp_parser_conversion_type_id (parser);
7951   /* Leave the scope of the class, if any.  */
7952   if (pushed_scope)
7953     pop_scope (pushed_scope);
7954   /* Restore the saved scope.  */
7955   parser->scope = saved_scope;
7956   parser->qualifying_scope = saved_qualifying_scope;
7957   parser->object_scope = saved_object_scope;
7958   /* If the TYPE is invalid, indicate failure.  */
7959   if (type == error_mark_node)
7960     return error_mark_node;
7961   return mangle_conv_op_name_for_type (type);
7962 }
7963
7964 /* Parse a conversion-type-id:
7965
7966    conversion-type-id:
7967      type-specifier-seq conversion-declarator [opt]
7968
7969    Returns the TYPE specified.  */
7970
7971 static tree
7972 cp_parser_conversion_type_id (cp_parser* parser)
7973 {
7974   tree attributes;
7975   cp_decl_specifier_seq type_specifiers;
7976   cp_declarator *declarator;
7977   tree type_specified;
7978
7979   /* Parse the attributes.  */
7980   attributes = cp_parser_attributes_opt (parser);
7981   /* Parse the type-specifiers.  */
7982   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7983                                 &type_specifiers);
7984   /* If that didn't work, stop.  */
7985   if (type_specifiers.type == error_mark_node)
7986     return error_mark_node;
7987   /* Parse the conversion-declarator.  */
7988   declarator = cp_parser_conversion_declarator_opt (parser);
7989
7990   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7991                                     /*initialized=*/0, &attributes);
7992   if (attributes)
7993     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7994   return type_specified;
7995 }
7996
7997 /* Parse an (optional) conversion-declarator.
7998
7999    conversion-declarator:
8000      ptr-operator conversion-declarator [opt]
8001
8002    */
8003
8004 static cp_declarator *
8005 cp_parser_conversion_declarator_opt (cp_parser* parser)
8006 {
8007   enum tree_code code;
8008   tree class_type;
8009   cp_cv_quals cv_quals;
8010
8011   /* We don't know if there's a ptr-operator next, or not.  */
8012   cp_parser_parse_tentatively (parser);
8013   /* Try the ptr-operator.  */
8014   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8015   /* If it worked, look for more conversion-declarators.  */
8016   if (cp_parser_parse_definitely (parser))
8017     {
8018       cp_declarator *declarator;
8019
8020       /* Parse another optional declarator.  */
8021       declarator = cp_parser_conversion_declarator_opt (parser);
8022
8023       /* Create the representation of the declarator.  */
8024       if (class_type)
8025         declarator = make_ptrmem_declarator (cv_quals, class_type,
8026                                              declarator);
8027       else if (code == INDIRECT_REF)
8028         declarator = make_pointer_declarator (cv_quals, declarator);
8029       else
8030         declarator = make_reference_declarator (cv_quals, declarator);
8031
8032       return declarator;
8033    }
8034
8035   return NULL;
8036 }
8037
8038 /* Parse an (optional) ctor-initializer.
8039
8040    ctor-initializer:
8041      : mem-initializer-list
8042
8043    Returns TRUE iff the ctor-initializer was actually present.  */
8044
8045 static bool
8046 cp_parser_ctor_initializer_opt (cp_parser* parser)
8047 {
8048   /* If the next token is not a `:', then there is no
8049      ctor-initializer.  */
8050   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8051     {
8052       /* Do default initialization of any bases and members.  */
8053       if (DECL_CONSTRUCTOR_P (current_function_decl))
8054         finish_mem_initializers (NULL_TREE);
8055
8056       return false;
8057     }
8058
8059   /* Consume the `:' token.  */
8060   cp_lexer_consume_token (parser->lexer);
8061   /* And the mem-initializer-list.  */
8062   cp_parser_mem_initializer_list (parser);
8063
8064   return true;
8065 }
8066
8067 /* Parse a mem-initializer-list.
8068
8069    mem-initializer-list:
8070      mem-initializer
8071      mem-initializer , mem-initializer-list  */
8072
8073 static void
8074 cp_parser_mem_initializer_list (cp_parser* parser)
8075 {
8076   tree mem_initializer_list = NULL_TREE;
8077
8078   /* Let the semantic analysis code know that we are starting the
8079      mem-initializer-list.  */
8080   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8081     error ("only constructors take base initializers");
8082
8083   /* Loop through the list.  */
8084   while (true)
8085     {
8086       tree mem_initializer;
8087
8088       /* Parse the mem-initializer.  */
8089       mem_initializer = cp_parser_mem_initializer (parser);
8090       /* Add it to the list, unless it was erroneous.  */
8091       if (mem_initializer != error_mark_node)
8092         {
8093           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8094           mem_initializer_list = mem_initializer;
8095         }
8096       /* If the next token is not a `,', we're done.  */
8097       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8098         break;
8099       /* Consume the `,' token.  */
8100       cp_lexer_consume_token (parser->lexer);
8101     }
8102
8103   /* Perform semantic analysis.  */
8104   if (DECL_CONSTRUCTOR_P (current_function_decl))
8105     finish_mem_initializers (mem_initializer_list);
8106 }
8107
8108 /* Parse a mem-initializer.
8109
8110    mem-initializer:
8111      mem-initializer-id ( expression-list [opt] )
8112
8113    GNU extension:
8114
8115    mem-initializer:
8116      ( expression-list [opt] )
8117
8118    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8119    class) or FIELD_DECL (for a non-static data member) to initialize;
8120    the TREE_VALUE is the expression-list.  An empty initialization
8121    list is represented by void_list_node.  */
8122
8123 static tree
8124 cp_parser_mem_initializer (cp_parser* parser)
8125 {
8126   tree mem_initializer_id;
8127   tree expression_list;
8128   tree member;
8129
8130   /* Find out what is being initialized.  */
8131   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8132     {
8133       pedwarn ("anachronistic old-style base class initializer");
8134       mem_initializer_id = NULL_TREE;
8135     }
8136   else
8137     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8138   member = expand_member_init (mem_initializer_id);
8139   if (member && !DECL_P (member))
8140     in_base_initializer = 1;
8141
8142   expression_list
8143     = cp_parser_parenthesized_expression_list (parser, false,
8144                                                /*cast_p=*/false,
8145                                                /*non_constant_p=*/NULL);
8146   if (expression_list == error_mark_node)
8147     return error_mark_node;
8148   if (!expression_list)
8149     expression_list = void_type_node;
8150
8151   in_base_initializer = 0;
8152
8153   return member ? build_tree_list (member, expression_list) : error_mark_node;
8154 }
8155
8156 /* Parse a mem-initializer-id.
8157
8158    mem-initializer-id:
8159      :: [opt] nested-name-specifier [opt] class-name
8160      identifier
8161
8162    Returns a TYPE indicating the class to be initializer for the first
8163    production.  Returns an IDENTIFIER_NODE indicating the data member
8164    to be initialized for the second production.  */
8165
8166 static tree
8167 cp_parser_mem_initializer_id (cp_parser* parser)
8168 {
8169   bool global_scope_p;
8170   bool nested_name_specifier_p;
8171   bool template_p = false;
8172   tree id;
8173
8174   /* `typename' is not allowed in this context ([temp.res]).  */
8175   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8176     {
8177       error ("keyword %<typename%> not allowed in this context (a qualified "
8178              "member initializer is implicitly a type)");
8179       cp_lexer_consume_token (parser->lexer);
8180     }
8181   /* Look for the optional `::' operator.  */
8182   global_scope_p
8183     = (cp_parser_global_scope_opt (parser,
8184                                    /*current_scope_valid_p=*/false)
8185        != NULL_TREE);
8186   /* Look for the optional nested-name-specifier.  The simplest way to
8187      implement:
8188
8189        [temp.res]
8190
8191        The keyword `typename' is not permitted in a base-specifier or
8192        mem-initializer; in these contexts a qualified name that
8193        depends on a template-parameter is implicitly assumed to be a
8194        type name.
8195
8196      is to assume that we have seen the `typename' keyword at this
8197      point.  */
8198   nested_name_specifier_p
8199     = (cp_parser_nested_name_specifier_opt (parser,
8200                                             /*typename_keyword_p=*/true,
8201                                             /*check_dependency_p=*/true,
8202                                             /*type_p=*/true,
8203                                             /*is_declaration=*/true)
8204        != NULL_TREE);
8205   if (nested_name_specifier_p)
8206     template_p = cp_parser_optional_template_keyword (parser);
8207   /* If there is a `::' operator or a nested-name-specifier, then we
8208      are definitely looking for a class-name.  */
8209   if (global_scope_p || nested_name_specifier_p)
8210     return cp_parser_class_name (parser,
8211                                  /*typename_keyword_p=*/true,
8212                                  /*template_keyword_p=*/template_p,
8213                                  none_type,
8214                                  /*check_dependency_p=*/true,
8215                                  /*class_head_p=*/false,
8216                                  /*is_declaration=*/true);
8217   /* Otherwise, we could also be looking for an ordinary identifier.  */
8218   cp_parser_parse_tentatively (parser);
8219   /* Try a class-name.  */
8220   id = cp_parser_class_name (parser,
8221                              /*typename_keyword_p=*/true,
8222                              /*template_keyword_p=*/false,
8223                              none_type,
8224                              /*check_dependency_p=*/true,
8225                              /*class_head_p=*/false,
8226                              /*is_declaration=*/true);
8227   /* If we found one, we're done.  */
8228   if (cp_parser_parse_definitely (parser))
8229     return id;
8230   /* Otherwise, look for an ordinary identifier.  */
8231   return cp_parser_identifier (parser);
8232 }
8233
8234 /* Overloading [gram.over] */
8235
8236 /* Parse an operator-function-id.
8237
8238    operator-function-id:
8239      operator operator
8240
8241    Returns an IDENTIFIER_NODE for the operator which is a
8242    human-readable spelling of the identifier, e.g., `operator +'.  */
8243
8244 static tree
8245 cp_parser_operator_function_id (cp_parser* parser)
8246 {
8247   /* Look for the `operator' keyword.  */
8248   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8249     return error_mark_node;
8250   /* And then the name of the operator itself.  */
8251   return cp_parser_operator (parser);
8252 }
8253
8254 /* Parse an operator.
8255
8256    operator:
8257      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8258      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8259      || ++ -- , ->* -> () []
8260
8261    GNU Extensions:
8262
8263    operator:
8264      <? >? <?= >?=
8265
8266    Returns an IDENTIFIER_NODE for the operator which is a
8267    human-readable spelling of the identifier, e.g., `operator +'.  */
8268
8269 static tree
8270 cp_parser_operator (cp_parser* parser)
8271 {
8272   tree id = NULL_TREE;
8273   cp_token *token;
8274
8275   /* Peek at the next token.  */
8276   token = cp_lexer_peek_token (parser->lexer);
8277   /* Figure out which operator we have.  */
8278   switch (token->type)
8279     {
8280     case CPP_KEYWORD:
8281       {
8282         enum tree_code op;
8283
8284         /* The keyword should be either `new' or `delete'.  */
8285         if (token->keyword == RID_NEW)
8286           op = NEW_EXPR;
8287         else if (token->keyword == RID_DELETE)
8288           op = DELETE_EXPR;
8289         else
8290           break;
8291
8292         /* Consume the `new' or `delete' token.  */
8293         cp_lexer_consume_token (parser->lexer);
8294
8295         /* Peek at the next token.  */
8296         token = cp_lexer_peek_token (parser->lexer);
8297         /* If it's a `[' token then this is the array variant of the
8298            operator.  */
8299         if (token->type == CPP_OPEN_SQUARE)
8300           {
8301             /* Consume the `[' token.  */
8302             cp_lexer_consume_token (parser->lexer);
8303             /* Look for the `]' token.  */
8304             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8305             id = ansi_opname (op == NEW_EXPR
8306                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8307           }
8308         /* Otherwise, we have the non-array variant.  */
8309         else
8310           id = ansi_opname (op);
8311
8312         return id;
8313       }
8314
8315     case CPP_PLUS:
8316       id = ansi_opname (PLUS_EXPR);
8317       break;
8318
8319     case CPP_MINUS:
8320       id = ansi_opname (MINUS_EXPR);
8321       break;
8322
8323     case CPP_MULT:
8324       id = ansi_opname (MULT_EXPR);
8325       break;
8326
8327     case CPP_DIV:
8328       id = ansi_opname (TRUNC_DIV_EXPR);
8329       break;
8330
8331     case CPP_MOD:
8332       id = ansi_opname (TRUNC_MOD_EXPR);
8333       break;
8334
8335     case CPP_XOR:
8336       id = ansi_opname (BIT_XOR_EXPR);
8337       break;
8338
8339     case CPP_AND:
8340       id = ansi_opname (BIT_AND_EXPR);
8341       break;
8342
8343     case CPP_OR:
8344       id = ansi_opname (BIT_IOR_EXPR);
8345       break;
8346
8347     case CPP_COMPL:
8348       id = ansi_opname (BIT_NOT_EXPR);
8349       break;
8350
8351     case CPP_NOT:
8352       id = ansi_opname (TRUTH_NOT_EXPR);
8353       break;
8354
8355     case CPP_EQ:
8356       id = ansi_assopname (NOP_EXPR);
8357       break;
8358
8359     case CPP_LESS:
8360       id = ansi_opname (LT_EXPR);
8361       break;
8362
8363     case CPP_GREATER:
8364       id = ansi_opname (GT_EXPR);
8365       break;
8366
8367     case CPP_PLUS_EQ:
8368       id = ansi_assopname (PLUS_EXPR);
8369       break;
8370
8371     case CPP_MINUS_EQ:
8372       id = ansi_assopname (MINUS_EXPR);
8373       break;
8374
8375     case CPP_MULT_EQ:
8376       id = ansi_assopname (MULT_EXPR);
8377       break;
8378
8379     case CPP_DIV_EQ:
8380       id = ansi_assopname (TRUNC_DIV_EXPR);
8381       break;
8382
8383     case CPP_MOD_EQ:
8384       id = ansi_assopname (TRUNC_MOD_EXPR);
8385       break;
8386
8387     case CPP_XOR_EQ:
8388       id = ansi_assopname (BIT_XOR_EXPR);
8389       break;
8390
8391     case CPP_AND_EQ:
8392       id = ansi_assopname (BIT_AND_EXPR);
8393       break;
8394
8395     case CPP_OR_EQ:
8396       id = ansi_assopname (BIT_IOR_EXPR);
8397       break;
8398
8399     case CPP_LSHIFT:
8400       id = ansi_opname (LSHIFT_EXPR);
8401       break;
8402
8403     case CPP_RSHIFT:
8404       id = ansi_opname (RSHIFT_EXPR);
8405       break;
8406
8407     case CPP_LSHIFT_EQ:
8408       id = ansi_assopname (LSHIFT_EXPR);
8409       break;
8410
8411     case CPP_RSHIFT_EQ:
8412       id = ansi_assopname (RSHIFT_EXPR);
8413       break;
8414
8415     case CPP_EQ_EQ:
8416       id = ansi_opname (EQ_EXPR);
8417       break;
8418
8419     case CPP_NOT_EQ:
8420       id = ansi_opname (NE_EXPR);
8421       break;
8422
8423     case CPP_LESS_EQ:
8424       id = ansi_opname (LE_EXPR);
8425       break;
8426
8427     case CPP_GREATER_EQ:
8428       id = ansi_opname (GE_EXPR);
8429       break;
8430
8431     case CPP_AND_AND:
8432       id = ansi_opname (TRUTH_ANDIF_EXPR);
8433       break;
8434
8435     case CPP_OR_OR:
8436       id = ansi_opname (TRUTH_ORIF_EXPR);
8437       break;
8438
8439     case CPP_PLUS_PLUS:
8440       id = ansi_opname (POSTINCREMENT_EXPR);
8441       break;
8442
8443     case CPP_MINUS_MINUS:
8444       id = ansi_opname (PREDECREMENT_EXPR);
8445       break;
8446
8447     case CPP_COMMA:
8448       id = ansi_opname (COMPOUND_EXPR);
8449       break;
8450
8451     case CPP_DEREF_STAR:
8452       id = ansi_opname (MEMBER_REF);
8453       break;
8454
8455     case CPP_DEREF:
8456       id = ansi_opname (COMPONENT_REF);
8457       break;
8458
8459     case CPP_OPEN_PAREN:
8460       /* Consume the `('.  */
8461       cp_lexer_consume_token (parser->lexer);
8462       /* Look for the matching `)'.  */
8463       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8464       return ansi_opname (CALL_EXPR);
8465
8466     case CPP_OPEN_SQUARE:
8467       /* Consume the `['.  */
8468       cp_lexer_consume_token (parser->lexer);
8469       /* Look for the matching `]'.  */
8470       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8471       return ansi_opname (ARRAY_REF);
8472
8473     default:
8474       /* Anything else is an error.  */
8475       break;
8476     }
8477
8478   /* If we have selected an identifier, we need to consume the
8479      operator token.  */
8480   if (id)
8481     cp_lexer_consume_token (parser->lexer);
8482   /* Otherwise, no valid operator name was present.  */
8483   else
8484     {
8485       cp_parser_error (parser, "expected operator");
8486       id = error_mark_node;
8487     }
8488
8489   return id;
8490 }
8491
8492 /* Parse a template-declaration.
8493
8494    template-declaration:
8495      export [opt] template < template-parameter-list > declaration
8496
8497    If MEMBER_P is TRUE, this template-declaration occurs within a
8498    class-specifier.
8499
8500    The grammar rule given by the standard isn't correct.  What
8501    is really meant is:
8502
8503    template-declaration:
8504      export [opt] template-parameter-list-seq
8505        decl-specifier-seq [opt] init-declarator [opt] ;
8506      export [opt] template-parameter-list-seq
8507        function-definition
8508
8509    template-parameter-list-seq:
8510      template-parameter-list-seq [opt]
8511      template < template-parameter-list >  */
8512
8513 static void
8514 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8515 {
8516   /* Check for `export'.  */
8517   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8518     {
8519       /* Consume the `export' token.  */
8520       cp_lexer_consume_token (parser->lexer);
8521       /* Warn that we do not support `export'.  */
8522       warning (0, "keyword %<export%> not implemented, and will be ignored");
8523     }
8524
8525   cp_parser_template_declaration_after_export (parser, member_p);
8526 }
8527
8528 /* Parse a template-parameter-list.
8529
8530    template-parameter-list:
8531      template-parameter
8532      template-parameter-list , template-parameter
8533
8534    Returns a TREE_LIST.  Each node represents a template parameter.
8535    The nodes are connected via their TREE_CHAINs.  */
8536
8537 static tree
8538 cp_parser_template_parameter_list (cp_parser* parser)
8539 {
8540   tree parameter_list = NULL_TREE;
8541
8542   begin_template_parm_list ();
8543   while (true)
8544     {
8545       tree parameter;
8546       cp_token *token;
8547       bool is_non_type;
8548
8549       /* Parse the template-parameter.  */
8550       parameter = cp_parser_template_parameter (parser, &is_non_type);
8551       /* Add it to the list.  */
8552       if (parameter != error_mark_node)
8553         parameter_list = process_template_parm (parameter_list,
8554                                                 parameter,
8555                                                 is_non_type);
8556       else
8557        {
8558          tree err_parm = build_tree_list (parameter, parameter);
8559          TREE_VALUE (err_parm) = error_mark_node;
8560          parameter_list = chainon (parameter_list, err_parm);
8561        }
8562
8563       /* Peek at the next token.  */
8564       token = cp_lexer_peek_token (parser->lexer);
8565       /* If it's not a `,', we're done.  */
8566       if (token->type != CPP_COMMA)
8567         break;
8568       /* Otherwise, consume the `,' token.  */
8569       cp_lexer_consume_token (parser->lexer);
8570     }
8571
8572   return end_template_parm_list (parameter_list);
8573 }
8574
8575 /* Parse a template-parameter.
8576
8577    template-parameter:
8578      type-parameter
8579      parameter-declaration
8580
8581    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8582    the parameter.  The TREE_PURPOSE is the default value, if any.
8583    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8584    iff this parameter is a non-type parameter.  */
8585
8586 static tree
8587 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8588 {
8589   cp_token *token;
8590   cp_parameter_declarator *parameter_declarator;
8591   tree parm;
8592
8593   /* Assume it is a type parameter or a template parameter.  */
8594   *is_non_type = false;
8595   /* Peek at the next token.  */
8596   token = cp_lexer_peek_token (parser->lexer);
8597   /* If it is `class' or `template', we have a type-parameter.  */
8598   if (token->keyword == RID_TEMPLATE)
8599     return cp_parser_type_parameter (parser);
8600   /* If it is `class' or `typename' we do not know yet whether it is a
8601      type parameter or a non-type parameter.  Consider:
8602
8603        template <typename T, typename T::X X> ...
8604
8605      or:
8606
8607        template <class C, class D*> ...
8608
8609      Here, the first parameter is a type parameter, and the second is
8610      a non-type parameter.  We can tell by looking at the token after
8611      the identifier -- if it is a `,', `=', or `>' then we have a type
8612      parameter.  */
8613   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8614     {
8615       /* Peek at the token after `class' or `typename'.  */
8616       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8617       /* If it's an identifier, skip it.  */
8618       if (token->type == CPP_NAME)
8619         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8620       /* Now, see if the token looks like the end of a template
8621          parameter.  */
8622       if (token->type == CPP_COMMA
8623           || token->type == CPP_EQ
8624           || token->type == CPP_GREATER)
8625         return cp_parser_type_parameter (parser);
8626     }
8627
8628   /* Otherwise, it is a non-type parameter.
8629
8630      [temp.param]
8631
8632      When parsing a default template-argument for a non-type
8633      template-parameter, the first non-nested `>' is taken as the end
8634      of the template parameter-list rather than a greater-than
8635      operator.  */
8636   *is_non_type = true;
8637   parameter_declarator
8638      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8639                                         /*parenthesized_p=*/NULL);
8640   parm = grokdeclarator (parameter_declarator->declarator,
8641                          &parameter_declarator->decl_specifiers,
8642                          PARM, /*initialized=*/0,
8643                          /*attrlist=*/NULL);
8644   if (parm == error_mark_node)
8645     return error_mark_node;
8646   return build_tree_list (parameter_declarator->default_argument, parm);
8647 }
8648
8649 /* Parse a type-parameter.
8650
8651    type-parameter:
8652      class identifier [opt]
8653      class identifier [opt] = type-id
8654      typename identifier [opt]
8655      typename identifier [opt] = type-id
8656      template < template-parameter-list > class identifier [opt]
8657      template < template-parameter-list > class identifier [opt]
8658        = id-expression
8659
8660    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8661    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8662    the declaration of the parameter.  */
8663
8664 static tree
8665 cp_parser_type_parameter (cp_parser* parser)
8666 {
8667   cp_token *token;
8668   tree parameter;
8669
8670   /* Look for a keyword to tell us what kind of parameter this is.  */
8671   token = cp_parser_require (parser, CPP_KEYWORD,
8672                              "`class', `typename', or `template'");
8673   if (!token)
8674     return error_mark_node;
8675
8676   switch (token->keyword)
8677     {
8678     case RID_CLASS:
8679     case RID_TYPENAME:
8680       {
8681         tree identifier;
8682         tree default_argument;
8683
8684         /* If the next token is an identifier, then it names the
8685            parameter.  */
8686         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8687           identifier = cp_parser_identifier (parser);
8688         else
8689           identifier = NULL_TREE;
8690
8691         /* Create the parameter.  */
8692         parameter = finish_template_type_parm (class_type_node, identifier);
8693
8694         /* If the next token is an `=', we have a default argument.  */
8695         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8696           {
8697             /* Consume the `=' token.  */
8698             cp_lexer_consume_token (parser->lexer);
8699             /* Parse the default-argument.  */
8700             push_deferring_access_checks (dk_no_deferred);
8701             default_argument = cp_parser_type_id (parser);
8702             pop_deferring_access_checks ();
8703           }
8704         else
8705           default_argument = NULL_TREE;
8706
8707         /* Create the combined representation of the parameter and the
8708            default argument.  */
8709         parameter = build_tree_list (default_argument, parameter);
8710       }
8711       break;
8712
8713     case RID_TEMPLATE:
8714       {
8715         tree parameter_list;
8716         tree identifier;
8717         tree default_argument;
8718
8719         /* Look for the `<'.  */
8720         cp_parser_require (parser, CPP_LESS, "`<'");
8721         /* Parse the template-parameter-list.  */
8722         parameter_list = cp_parser_template_parameter_list (parser);
8723         /* Look for the `>'.  */
8724         cp_parser_require (parser, CPP_GREATER, "`>'");
8725         /* Look for the `class' keyword.  */
8726         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8727         /* If the next token is an `=', then there is a
8728            default-argument.  If the next token is a `>', we are at
8729            the end of the parameter-list.  If the next token is a `,',
8730            then we are at the end of this parameter.  */
8731         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8732             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8733             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8734           {
8735             identifier = cp_parser_identifier (parser);
8736             /* Treat invalid names as if the parameter were nameless.  */
8737             if (identifier == error_mark_node)
8738               identifier = NULL_TREE;
8739           }
8740         else
8741           identifier = NULL_TREE;
8742
8743         /* Create the template parameter.  */
8744         parameter = finish_template_template_parm (class_type_node,
8745                                                    identifier);
8746
8747         /* If the next token is an `=', then there is a
8748            default-argument.  */
8749         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8750           {
8751             bool is_template;
8752
8753             /* Consume the `='.  */
8754             cp_lexer_consume_token (parser->lexer);
8755             /* Parse the id-expression.  */
8756             push_deferring_access_checks (dk_no_deferred);
8757             default_argument
8758               = cp_parser_id_expression (parser,
8759                                          /*template_keyword_p=*/false,
8760                                          /*check_dependency_p=*/true,
8761                                          /*template_p=*/&is_template,
8762                                          /*declarator_p=*/false,
8763                                          /*optional_p=*/false);
8764             if (TREE_CODE (default_argument) == TYPE_DECL)
8765               /* If the id-expression was a template-id that refers to
8766                  a template-class, we already have the declaration here,
8767                  so no further lookup is needed.  */
8768                  ;
8769             else
8770               /* Look up the name.  */
8771               default_argument
8772                 = cp_parser_lookup_name (parser, default_argument,
8773                                          none_type,
8774                                          /*is_template=*/is_template,
8775                                          /*is_namespace=*/false,
8776                                          /*check_dependency=*/true,
8777                                          /*ambiguous_decls=*/NULL);
8778             /* See if the default argument is valid.  */
8779             default_argument
8780               = check_template_template_default_arg (default_argument);
8781             pop_deferring_access_checks ();
8782           }
8783         else
8784           default_argument = NULL_TREE;
8785
8786         /* Create the combined representation of the parameter and the
8787            default argument.  */
8788         parameter = build_tree_list (default_argument, parameter);
8789       }
8790       break;
8791
8792     default:
8793       gcc_unreachable ();
8794       break;
8795     }
8796
8797   return parameter;
8798 }
8799
8800 /* Parse a template-id.
8801
8802    template-id:
8803      template-name < template-argument-list [opt] >
8804
8805    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8806    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8807    returned.  Otherwise, if the template-name names a function, or set
8808    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8809    names a class, returns a TYPE_DECL for the specialization.
8810
8811    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8812    uninstantiated templates.  */
8813
8814 static tree
8815 cp_parser_template_id (cp_parser *parser,
8816                        bool template_keyword_p,
8817                        bool check_dependency_p,
8818                        bool is_declaration)
8819 {
8820   tree template;
8821   tree arguments;
8822   tree template_id;
8823   cp_token_position start_of_id = 0;
8824   tree access_check = NULL_TREE;
8825   cp_token *next_token, *next_token_2;
8826   bool is_identifier;
8827
8828   /* If the next token corresponds to a template-id, there is no need
8829      to reparse it.  */
8830   next_token = cp_lexer_peek_token (parser->lexer);
8831   if (next_token->type == CPP_TEMPLATE_ID)
8832     {
8833       tree value;
8834       tree check;
8835
8836       /* Get the stored value.  */
8837       value = cp_lexer_consume_token (parser->lexer)->value;
8838       /* Perform any access checks that were deferred.  */
8839       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8840         perform_or_defer_access_check (TREE_PURPOSE (check),
8841                                        TREE_VALUE (check),
8842                                        TREE_VALUE (check));
8843       /* Return the stored value.  */
8844       return TREE_VALUE (value);
8845     }
8846
8847   /* Avoid performing name lookup if there is no possibility of
8848      finding a template-id.  */
8849   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8850       || (next_token->type == CPP_NAME
8851           && !cp_parser_nth_token_starts_template_argument_list_p
8852                (parser, 2)))
8853     {
8854       cp_parser_error (parser, "expected template-id");
8855       return error_mark_node;
8856     }
8857
8858   /* Remember where the template-id starts.  */
8859   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8860     start_of_id = cp_lexer_token_position (parser->lexer, false);
8861
8862   push_deferring_access_checks (dk_deferred);
8863
8864   /* Parse the template-name.  */
8865   is_identifier = false;
8866   template = cp_parser_template_name (parser, template_keyword_p,
8867                                       check_dependency_p,
8868                                       is_declaration,
8869                                       &is_identifier);
8870   if (template == error_mark_node || is_identifier)
8871     {
8872       pop_deferring_access_checks ();
8873       return template;
8874     }
8875
8876   /* If we find the sequence `[:' after a template-name, it's probably
8877      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8878      parse correctly the argument list.  */
8879   next_token = cp_lexer_peek_token (parser->lexer);
8880   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8881   if (next_token->type == CPP_OPEN_SQUARE
8882       && next_token->flags & DIGRAPH
8883       && next_token_2->type == CPP_COLON
8884       && !(next_token_2->flags & PREV_WHITE))
8885     {
8886       cp_parser_parse_tentatively (parser);
8887       /* Change `:' into `::'.  */
8888       next_token_2->type = CPP_SCOPE;
8889       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8890          CPP_LESS.  */
8891       cp_lexer_consume_token (parser->lexer);
8892       /* Parse the arguments.  */
8893       arguments = cp_parser_enclosed_template_argument_list (parser);
8894       if (!cp_parser_parse_definitely (parser))
8895         {
8896           /* If we couldn't parse an argument list, then we revert our changes
8897              and return simply an error. Maybe this is not a template-id
8898              after all.  */
8899           next_token_2->type = CPP_COLON;
8900           cp_parser_error (parser, "expected %<<%>");
8901           pop_deferring_access_checks ();
8902           return error_mark_node;
8903         }
8904       /* Otherwise, emit an error about the invalid digraph, but continue
8905          parsing because we got our argument list.  */
8906       pedwarn ("%<<::%> cannot begin a template-argument list");
8907       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8908               "between %<<%> and %<::%>");
8909       if (!flag_permissive)
8910         {
8911           static bool hint;
8912           if (!hint)
8913             {
8914               inform ("(if you use -fpermissive G++ will accept your code)");
8915               hint = true;
8916             }
8917         }
8918     }
8919   else
8920     {
8921       /* Look for the `<' that starts the template-argument-list.  */
8922       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8923         {
8924           pop_deferring_access_checks ();
8925           return error_mark_node;
8926         }
8927       /* Parse the arguments.  */
8928       arguments = cp_parser_enclosed_template_argument_list (parser);
8929     }
8930
8931   /* Build a representation of the specialization.  */
8932   if (TREE_CODE (template) == IDENTIFIER_NODE)
8933     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8934   else if (DECL_CLASS_TEMPLATE_P (template)
8935            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8936     {
8937       bool entering_scope;
8938       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8939          template (rather than some instantiation thereof) only if
8940          is not nested within some other construct.  For example, in
8941          "template <typename T> void f(T) { A<T>::", A<T> is just an
8942          instantiation of A.  */
8943       entering_scope = (template_parm_scope_p ()
8944                         && cp_lexer_next_token_is (parser->lexer,
8945                                                    CPP_SCOPE));
8946       template_id
8947         = finish_template_type (template, arguments, entering_scope);
8948     }
8949   else
8950     {
8951       /* If it's not a class-template or a template-template, it should be
8952          a function-template.  */
8953       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8954                    || TREE_CODE (template) == OVERLOAD
8955                    || BASELINK_P (template)));
8956
8957       template_id = lookup_template_function (template, arguments);
8958     }
8959
8960   /* Retrieve any deferred checks.  Do not pop this access checks yet
8961      so the memory will not be reclaimed during token replacing below.  */
8962   access_check = get_deferred_access_checks ();
8963
8964   /* If parsing tentatively, replace the sequence of tokens that makes
8965      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8966      should we re-parse the token stream, we will not have to repeat
8967      the effort required to do the parse, nor will we issue duplicate
8968      error messages about problems during instantiation of the
8969      template.  */
8970   if (start_of_id)
8971     {
8972       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8973
8974       /* Reset the contents of the START_OF_ID token.  */
8975       token->type = CPP_TEMPLATE_ID;
8976       token->value = build_tree_list (access_check, template_id);
8977       token->keyword = RID_MAX;
8978
8979       /* Purge all subsequent tokens.  */
8980       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8981
8982       /* ??? Can we actually assume that, if template_id ==
8983          error_mark_node, we will have issued a diagnostic to the
8984          user, as opposed to simply marking the tentative parse as
8985          failed?  */
8986       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8987         error ("parse error in template argument list");
8988     }
8989
8990   pop_deferring_access_checks ();
8991   return template_id;
8992 }
8993
8994 /* Parse a template-name.
8995
8996    template-name:
8997      identifier
8998
8999    The standard should actually say:
9000
9001    template-name:
9002      identifier
9003      operator-function-id
9004
9005    A defect report has been filed about this issue.
9006
9007    A conversion-function-id cannot be a template name because they cannot
9008    be part of a template-id. In fact, looking at this code:
9009
9010    a.operator K<int>()
9011
9012    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9013    It is impossible to call a templated conversion-function-id with an
9014    explicit argument list, since the only allowed template parameter is
9015    the type to which it is converting.
9016
9017    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9018    `template' keyword, in a construction like:
9019
9020      T::template f<3>()
9021
9022    In that case `f' is taken to be a template-name, even though there
9023    is no way of knowing for sure.
9024
9025    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9026    name refers to a set of overloaded functions, at least one of which
9027    is a template, or an IDENTIFIER_NODE with the name of the template,
9028    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9029    names are looked up inside uninstantiated templates.  */
9030
9031 static tree
9032 cp_parser_template_name (cp_parser* parser,
9033                          bool template_keyword_p,
9034                          bool check_dependency_p,
9035                          bool is_declaration,
9036                          bool *is_identifier)
9037 {
9038   tree identifier;
9039   tree decl;
9040   tree fns;
9041
9042   /* If the next token is `operator', then we have either an
9043      operator-function-id or a conversion-function-id.  */
9044   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9045     {
9046       /* We don't know whether we're looking at an
9047          operator-function-id or a conversion-function-id.  */
9048       cp_parser_parse_tentatively (parser);
9049       /* Try an operator-function-id.  */
9050       identifier = cp_parser_operator_function_id (parser);
9051       /* If that didn't work, try a conversion-function-id.  */
9052       if (!cp_parser_parse_definitely (parser))
9053         {
9054           cp_parser_error (parser, "expected template-name");
9055           return error_mark_node;
9056         }
9057     }
9058   /* Look for the identifier.  */
9059   else
9060     identifier = cp_parser_identifier (parser);
9061
9062   /* If we didn't find an identifier, we don't have a template-id.  */
9063   if (identifier == error_mark_node)
9064     return error_mark_node;
9065
9066   /* If the name immediately followed the `template' keyword, then it
9067      is a template-name.  However, if the next token is not `<', then
9068      we do not treat it as a template-name, since it is not being used
9069      as part of a template-id.  This enables us to handle constructs
9070      like:
9071
9072        template <typename T> struct S { S(); };
9073        template <typename T> S<T>::S();
9074
9075      correctly.  We would treat `S' as a template -- if it were `S<T>'
9076      -- but we do not if there is no `<'.  */
9077
9078   if (processing_template_decl
9079       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9080     {
9081       /* In a declaration, in a dependent context, we pretend that the
9082          "template" keyword was present in order to improve error
9083          recovery.  For example, given:
9084
9085            template <typename T> void f(T::X<int>);
9086
9087          we want to treat "X<int>" as a template-id.  */
9088       if (is_declaration
9089           && !template_keyword_p
9090           && parser->scope && TYPE_P (parser->scope)
9091           && check_dependency_p
9092           && dependent_type_p (parser->scope)
9093           /* Do not do this for dtors (or ctors), since they never
9094              need the template keyword before their name.  */
9095           && !constructor_name_p (identifier, parser->scope))
9096         {
9097           cp_token_position start = 0;
9098
9099           /* Explain what went wrong.  */
9100           error ("non-template %qD used as template", identifier);
9101           inform ("use %<%T::template %D%> to indicate that it is a template",
9102                   parser->scope, identifier);
9103           /* If parsing tentatively, find the location of the "<" token.  */
9104           if (cp_parser_simulate_error (parser))
9105             start = cp_lexer_token_position (parser->lexer, true);
9106           /* Parse the template arguments so that we can issue error
9107              messages about them.  */
9108           cp_lexer_consume_token (parser->lexer);
9109           cp_parser_enclosed_template_argument_list (parser);
9110           /* Skip tokens until we find a good place from which to
9111              continue parsing.  */
9112           cp_parser_skip_to_closing_parenthesis (parser,
9113                                                  /*recovering=*/true,
9114                                                  /*or_comma=*/true,
9115                                                  /*consume_paren=*/false);
9116           /* If parsing tentatively, permanently remove the
9117              template argument list.  That will prevent duplicate
9118              error messages from being issued about the missing
9119              "template" keyword.  */
9120           if (start)
9121             cp_lexer_purge_tokens_after (parser->lexer, start);
9122           if (is_identifier)
9123             *is_identifier = true;
9124           return identifier;
9125         }
9126
9127       /* If the "template" keyword is present, then there is generally
9128          no point in doing name-lookup, so we just return IDENTIFIER.
9129          But, if the qualifying scope is non-dependent then we can
9130          (and must) do name-lookup normally.  */
9131       if (template_keyword_p
9132           && (!parser->scope
9133               || (TYPE_P (parser->scope)
9134                   && dependent_type_p (parser->scope))))
9135         return identifier;
9136     }
9137
9138   /* Look up the name.  */
9139   decl = cp_parser_lookup_name (parser, identifier,
9140                                 none_type,
9141                                 /*is_template=*/false,
9142                                 /*is_namespace=*/false,
9143                                 check_dependency_p,
9144                                 /*ambiguous_decls=*/NULL);
9145   decl = maybe_get_template_decl_from_type_decl (decl);
9146
9147   /* If DECL is a template, then the name was a template-name.  */
9148   if (TREE_CODE (decl) == TEMPLATE_DECL)
9149     ;
9150   else
9151     {
9152       tree fn = NULL_TREE;
9153
9154       /* The standard does not explicitly indicate whether a name that
9155          names a set of overloaded declarations, some of which are
9156          templates, is a template-name.  However, such a name should
9157          be a template-name; otherwise, there is no way to form a
9158          template-id for the overloaded templates.  */
9159       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9160       if (TREE_CODE (fns) == OVERLOAD)
9161         for (fn = fns; fn; fn = OVL_NEXT (fn))
9162           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9163             break;
9164
9165       if (!fn)
9166         {
9167           /* The name does not name a template.  */
9168           cp_parser_error (parser, "expected template-name");
9169           return error_mark_node;
9170         }
9171     }
9172
9173   /* If DECL is dependent, and refers to a function, then just return
9174      its name; we will look it up again during template instantiation.  */
9175   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9176     {
9177       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9178       if (TYPE_P (scope) && dependent_type_p (scope))
9179         return identifier;
9180     }
9181
9182   return decl;
9183 }
9184
9185 /* Parse a template-argument-list.
9186
9187    template-argument-list:
9188      template-argument
9189      template-argument-list , template-argument
9190
9191    Returns a TREE_VEC containing the arguments.  */
9192
9193 static tree
9194 cp_parser_template_argument_list (cp_parser* parser)
9195 {
9196   tree fixed_args[10];
9197   unsigned n_args = 0;
9198   unsigned alloced = 10;
9199   tree *arg_ary = fixed_args;
9200   tree vec;
9201   bool saved_in_template_argument_list_p;
9202   bool saved_ice_p;
9203   bool saved_non_ice_p;
9204
9205   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9206   parser->in_template_argument_list_p = true;
9207   /* Even if the template-id appears in an integral
9208      constant-expression, the contents of the argument list do
9209      not.  */
9210   saved_ice_p = parser->integral_constant_expression_p;
9211   parser->integral_constant_expression_p = false;
9212   saved_non_ice_p = parser->non_integral_constant_expression_p;
9213   parser->non_integral_constant_expression_p = false;
9214   /* Parse the arguments.  */
9215   do
9216     {
9217       tree argument;
9218
9219       if (n_args)
9220         /* Consume the comma.  */
9221         cp_lexer_consume_token (parser->lexer);
9222
9223       /* Parse the template-argument.  */
9224       argument = cp_parser_template_argument (parser);
9225       if (n_args == alloced)
9226         {
9227           alloced *= 2;
9228
9229           if (arg_ary == fixed_args)
9230             {
9231               arg_ary = XNEWVEC (tree, alloced);
9232               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9233             }
9234           else
9235             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9236         }
9237       arg_ary[n_args++] = argument;
9238     }
9239   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9240
9241   vec = make_tree_vec (n_args);
9242
9243   while (n_args--)
9244     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9245
9246   if (arg_ary != fixed_args)
9247     free (arg_ary);
9248   parser->non_integral_constant_expression_p = saved_non_ice_p;
9249   parser->integral_constant_expression_p = saved_ice_p;
9250   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9251   return vec;
9252 }
9253
9254 /* Parse a template-argument.
9255
9256    template-argument:
9257      assignment-expression
9258      type-id
9259      id-expression
9260
9261    The representation is that of an assignment-expression, type-id, or
9262    id-expression -- except that the qualified id-expression is
9263    evaluated, so that the value returned is either a DECL or an
9264    OVERLOAD.
9265
9266    Although the standard says "assignment-expression", it forbids
9267    throw-expressions or assignments in the template argument.
9268    Therefore, we use "conditional-expression" instead.  */
9269
9270 static tree
9271 cp_parser_template_argument (cp_parser* parser)
9272 {
9273   tree argument;
9274   bool template_p;
9275   bool address_p;
9276   bool maybe_type_id = false;
9277   cp_token *token;
9278   cp_id_kind idk;
9279
9280   /* There's really no way to know what we're looking at, so we just
9281      try each alternative in order.
9282
9283        [temp.arg]
9284
9285        In a template-argument, an ambiguity between a type-id and an
9286        expression is resolved to a type-id, regardless of the form of
9287        the corresponding template-parameter.
9288
9289      Therefore, we try a type-id first.  */
9290   cp_parser_parse_tentatively (parser);
9291   argument = cp_parser_type_id (parser);
9292   /* If there was no error parsing the type-id but the next token is a '>>',
9293      we probably found a typo for '> >'. But there are type-id which are
9294      also valid expressions. For instance:
9295
9296      struct X { int operator >> (int); };
9297      template <int V> struct Foo {};
9298      Foo<X () >> 5> r;
9299
9300      Here 'X()' is a valid type-id of a function type, but the user just
9301      wanted to write the expression "X() >> 5". Thus, we remember that we
9302      found a valid type-id, but we still try to parse the argument as an
9303      expression to see what happens.  */
9304   if (!cp_parser_error_occurred (parser)
9305       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9306     {
9307       maybe_type_id = true;
9308       cp_parser_abort_tentative_parse (parser);
9309     }
9310   else
9311     {
9312       /* If the next token isn't a `,' or a `>', then this argument wasn't
9313       really finished. This means that the argument is not a valid
9314       type-id.  */
9315       if (!cp_parser_next_token_ends_template_argument_p (parser))
9316         cp_parser_error (parser, "expected template-argument");
9317       /* If that worked, we're done.  */
9318       if (cp_parser_parse_definitely (parser))
9319         return argument;
9320     }
9321   /* We're still not sure what the argument will be.  */
9322   cp_parser_parse_tentatively (parser);
9323   /* Try a template.  */
9324   argument = cp_parser_id_expression (parser,
9325                                       /*template_keyword_p=*/false,
9326                                       /*check_dependency_p=*/true,
9327                                       &template_p,
9328                                       /*declarator_p=*/false,
9329                                       /*optional_p=*/false);
9330   /* If the next token isn't a `,' or a `>', then this argument wasn't
9331      really finished.  */
9332   if (!cp_parser_next_token_ends_template_argument_p (parser))
9333     cp_parser_error (parser, "expected template-argument");
9334   if (!cp_parser_error_occurred (parser))
9335     {
9336       /* Figure out what is being referred to.  If the id-expression
9337          was for a class template specialization, then we will have a
9338          TYPE_DECL at this point.  There is no need to do name lookup
9339          at this point in that case.  */
9340       if (TREE_CODE (argument) != TYPE_DECL)
9341         argument = cp_parser_lookup_name (parser, argument,
9342                                           none_type,
9343                                           /*is_template=*/template_p,
9344                                           /*is_namespace=*/false,
9345                                           /*check_dependency=*/true,
9346                                           /*ambiguous_decls=*/NULL);
9347       if (TREE_CODE (argument) != TEMPLATE_DECL
9348           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9349         cp_parser_error (parser, "expected template-name");
9350     }
9351   if (cp_parser_parse_definitely (parser))
9352     return argument;
9353   /* It must be a non-type argument.  There permitted cases are given
9354      in [temp.arg.nontype]:
9355
9356      -- an integral constant-expression of integral or enumeration
9357         type; or
9358
9359      -- the name of a non-type template-parameter; or
9360
9361      -- the name of an object or function with external linkage...
9362
9363      -- the address of an object or function with external linkage...
9364
9365      -- a pointer to member...  */
9366   /* Look for a non-type template parameter.  */
9367   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9368     {
9369       cp_parser_parse_tentatively (parser);
9370       argument = cp_parser_primary_expression (parser,
9371                                                /*adress_p=*/false,
9372                                                /*cast_p=*/false,
9373                                                /*template_arg_p=*/true,
9374                                                &idk);
9375       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9376           || !cp_parser_next_token_ends_template_argument_p (parser))
9377         cp_parser_simulate_error (parser);
9378       if (cp_parser_parse_definitely (parser))
9379         return argument;
9380     }
9381
9382   /* If the next token is "&", the argument must be the address of an
9383      object or function with external linkage.  */
9384   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9385   if (address_p)
9386     cp_lexer_consume_token (parser->lexer);
9387   /* See if we might have an id-expression.  */
9388   token = cp_lexer_peek_token (parser->lexer);
9389   if (token->type == CPP_NAME
9390       || token->keyword == RID_OPERATOR
9391       || token->type == CPP_SCOPE
9392       || token->type == CPP_TEMPLATE_ID
9393       || token->type == CPP_NESTED_NAME_SPECIFIER)
9394     {
9395       cp_parser_parse_tentatively (parser);
9396       argument = cp_parser_primary_expression (parser,
9397                                                address_p,
9398                                                /*cast_p=*/false,
9399                                                /*template_arg_p=*/true,
9400                                                &idk);
9401       if (cp_parser_error_occurred (parser)
9402           || !cp_parser_next_token_ends_template_argument_p (parser))
9403         cp_parser_abort_tentative_parse (parser);
9404       else
9405         {
9406           if (TREE_CODE (argument) == INDIRECT_REF)
9407             {
9408               gcc_assert (REFERENCE_REF_P (argument));
9409               argument = TREE_OPERAND (argument, 0);
9410             }
9411
9412           if (TREE_CODE (argument) == VAR_DECL)
9413             {
9414               /* A variable without external linkage might still be a
9415                  valid constant-expression, so no error is issued here
9416                  if the external-linkage check fails.  */
9417               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9418                 cp_parser_simulate_error (parser);
9419             }
9420           else if (is_overloaded_fn (argument))
9421             /* All overloaded functions are allowed; if the external
9422                linkage test does not pass, an error will be issued
9423                later.  */
9424             ;
9425           else if (address_p
9426                    && (TREE_CODE (argument) == OFFSET_REF
9427                        || TREE_CODE (argument) == SCOPE_REF))
9428             /* A pointer-to-member.  */
9429             ;
9430           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9431             ;
9432           else
9433             cp_parser_simulate_error (parser);
9434
9435           if (cp_parser_parse_definitely (parser))
9436             {
9437               if (address_p)
9438                 argument = build_x_unary_op (ADDR_EXPR, argument);
9439               return argument;
9440             }
9441         }
9442     }
9443   /* If the argument started with "&", there are no other valid
9444      alternatives at this point.  */
9445   if (address_p)
9446     {
9447       cp_parser_error (parser, "invalid non-type template argument");
9448       return error_mark_node;
9449     }
9450
9451   /* If the argument wasn't successfully parsed as a type-id followed
9452      by '>>', the argument can only be a constant expression now.
9453      Otherwise, we try parsing the constant-expression tentatively,
9454      because the argument could really be a type-id.  */
9455   if (maybe_type_id)
9456     cp_parser_parse_tentatively (parser);
9457   argument = cp_parser_constant_expression (parser,
9458                                             /*allow_non_constant_p=*/false,
9459                                             /*non_constant_p=*/NULL);
9460   argument = fold_non_dependent_expr (argument);
9461   if (!maybe_type_id)
9462     return argument;
9463   if (!cp_parser_next_token_ends_template_argument_p (parser))
9464     cp_parser_error (parser, "expected template-argument");
9465   if (cp_parser_parse_definitely (parser))
9466     return argument;
9467   /* We did our best to parse the argument as a non type-id, but that
9468      was the only alternative that matched (albeit with a '>' after
9469      it). We can assume it's just a typo from the user, and a
9470      diagnostic will then be issued.  */
9471   return cp_parser_type_id (parser);
9472 }
9473
9474 /* Parse an explicit-instantiation.
9475
9476    explicit-instantiation:
9477      template declaration
9478
9479    Although the standard says `declaration', what it really means is:
9480
9481    explicit-instantiation:
9482      template decl-specifier-seq [opt] declarator [opt] ;
9483
9484    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9485    supposed to be allowed.  A defect report has been filed about this
9486    issue.
9487
9488    GNU Extension:
9489
9490    explicit-instantiation:
9491      storage-class-specifier template
9492        decl-specifier-seq [opt] declarator [opt] ;
9493      function-specifier template
9494        decl-specifier-seq [opt] declarator [opt] ;  */
9495
9496 static void
9497 cp_parser_explicit_instantiation (cp_parser* parser)
9498 {
9499   int declares_class_or_enum;
9500   cp_decl_specifier_seq decl_specifiers;
9501   tree extension_specifier = NULL_TREE;
9502
9503   /* Look for an (optional) storage-class-specifier or
9504      function-specifier.  */
9505   if (cp_parser_allow_gnu_extensions_p (parser))
9506     {
9507       extension_specifier
9508         = cp_parser_storage_class_specifier_opt (parser);
9509       if (!extension_specifier)
9510         extension_specifier
9511           = cp_parser_function_specifier_opt (parser,
9512                                               /*decl_specs=*/NULL);
9513     }
9514
9515   /* Look for the `template' keyword.  */
9516   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9517   /* Let the front end know that we are processing an explicit
9518      instantiation.  */
9519   begin_explicit_instantiation ();
9520   /* [temp.explicit] says that we are supposed to ignore access
9521      control while processing explicit instantiation directives.  */
9522   push_deferring_access_checks (dk_no_check);
9523   /* Parse a decl-specifier-seq.  */
9524   cp_parser_decl_specifier_seq (parser,
9525                                 CP_PARSER_FLAGS_OPTIONAL,
9526                                 &decl_specifiers,
9527                                 &declares_class_or_enum);
9528   /* If there was exactly one decl-specifier, and it declared a class,
9529      and there's no declarator, then we have an explicit type
9530      instantiation.  */
9531   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9532     {
9533       tree type;
9534
9535       type = check_tag_decl (&decl_specifiers);
9536       /* Turn access control back on for names used during
9537          template instantiation.  */
9538       pop_deferring_access_checks ();
9539       if (type)
9540         do_type_instantiation (type, extension_specifier,
9541                                /*complain=*/tf_error);
9542     }
9543   else
9544     {
9545       cp_declarator *declarator;
9546       tree decl;
9547
9548       /* Parse the declarator.  */
9549       declarator
9550         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9551                                 /*ctor_dtor_or_conv_p=*/NULL,
9552                                 /*parenthesized_p=*/NULL,
9553                                 /*member_p=*/false);
9554       if (declares_class_or_enum & 2)
9555         cp_parser_check_for_definition_in_return_type (declarator,
9556                                                        decl_specifiers.type);
9557       if (declarator != cp_error_declarator)
9558         {
9559           decl = grokdeclarator (declarator, &decl_specifiers,
9560                                  NORMAL, 0, &decl_specifiers.attributes);
9561           /* Turn access control back on for names used during
9562              template instantiation.  */
9563           pop_deferring_access_checks ();
9564           /* Do the explicit instantiation.  */
9565           do_decl_instantiation (decl, extension_specifier);
9566         }
9567       else
9568         {
9569           pop_deferring_access_checks ();
9570           /* Skip the body of the explicit instantiation.  */
9571           cp_parser_skip_to_end_of_statement (parser);
9572         }
9573     }
9574   /* We're done with the instantiation.  */
9575   end_explicit_instantiation ();
9576
9577   cp_parser_consume_semicolon_at_end_of_statement (parser);
9578 }
9579
9580 /* Parse an explicit-specialization.
9581
9582    explicit-specialization:
9583      template < > declaration
9584
9585    Although the standard says `declaration', what it really means is:
9586
9587    explicit-specialization:
9588      template <> decl-specifier [opt] init-declarator [opt] ;
9589      template <> function-definition
9590      template <> explicit-specialization
9591      template <> template-declaration  */
9592
9593 static void
9594 cp_parser_explicit_specialization (cp_parser* parser)
9595 {
9596   bool need_lang_pop;
9597   /* Look for the `template' keyword.  */
9598   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9599   /* Look for the `<'.  */
9600   cp_parser_require (parser, CPP_LESS, "`<'");
9601   /* Look for the `>'.  */
9602   cp_parser_require (parser, CPP_GREATER, "`>'");
9603   /* We have processed another parameter list.  */
9604   ++parser->num_template_parameter_lists;
9605   /* [temp]
9606
9607      A template ... explicit specialization ... shall not have C
9608      linkage.  */
9609   if (current_lang_name == lang_name_c)
9610     {
9611       error ("template specialization with C linkage");
9612       /* Give it C++ linkage to avoid confusing other parts of the
9613          front end.  */
9614       push_lang_context (lang_name_cplusplus);
9615       need_lang_pop = true;
9616     }
9617   else
9618     need_lang_pop = false;
9619   /* Let the front end know that we are beginning a specialization.  */
9620   if (!begin_specialization ())
9621     {
9622       end_specialization ();
9623       cp_parser_skip_to_end_of_block_or_statement (parser);
9624       return;
9625     }
9626
9627   /* If the next keyword is `template', we need to figure out whether
9628      or not we're looking a template-declaration.  */
9629   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9630     {
9631       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9632           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9633         cp_parser_template_declaration_after_export (parser,
9634                                                      /*member_p=*/false);
9635       else
9636         cp_parser_explicit_specialization (parser);
9637     }
9638   else
9639     /* Parse the dependent declaration.  */
9640     cp_parser_single_declaration (parser,
9641                                   /*checks=*/NULL_TREE,
9642                                   /*member_p=*/false,
9643                                   /*friend_p=*/NULL);
9644   /* We're done with the specialization.  */
9645   end_specialization ();
9646   /* For the erroneous case of a template with C linkage, we pushed an
9647      implicit C++ linkage scope; exit that scope now.  */
9648   if (need_lang_pop)
9649     pop_lang_context ();
9650   /* We're done with this parameter list.  */
9651   --parser->num_template_parameter_lists;
9652 }
9653
9654 /* Parse a type-specifier.
9655
9656    type-specifier:
9657      simple-type-specifier
9658      class-specifier
9659      enum-specifier
9660      elaborated-type-specifier
9661      cv-qualifier
9662
9663    GNU Extension:
9664
9665    type-specifier:
9666      __complex__
9667
9668    Returns a representation of the type-specifier.  For a
9669    class-specifier, enum-specifier, or elaborated-type-specifier, a
9670    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9671
9672    The parser flags FLAGS is used to control type-specifier parsing.
9673
9674    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9675    in a decl-specifier-seq.
9676
9677    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9678    class-specifier, enum-specifier, or elaborated-type-specifier, then
9679    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9680    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9681    zero.
9682
9683    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9684    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9685    is set to FALSE.  */
9686
9687 static tree
9688 cp_parser_type_specifier (cp_parser* parser,
9689                           cp_parser_flags flags,
9690                           cp_decl_specifier_seq *decl_specs,
9691                           bool is_declaration,
9692                           int* declares_class_or_enum,
9693                           bool* is_cv_qualifier)
9694 {
9695   tree type_spec = NULL_TREE;
9696   cp_token *token;
9697   enum rid keyword;
9698   cp_decl_spec ds = ds_last;
9699
9700   /* Assume this type-specifier does not declare a new type.  */
9701   if (declares_class_or_enum)
9702     *declares_class_or_enum = 0;
9703   /* And that it does not specify a cv-qualifier.  */
9704   if (is_cv_qualifier)
9705     *is_cv_qualifier = false;
9706   /* Peek at the next token.  */
9707   token = cp_lexer_peek_token (parser->lexer);
9708
9709   /* If we're looking at a keyword, we can use that to guide the
9710      production we choose.  */
9711   keyword = token->keyword;
9712   switch (keyword)
9713     {
9714     case RID_ENUM:
9715       /* Look for the enum-specifier.  */
9716       type_spec = cp_parser_enum_specifier (parser);
9717       /* If that worked, we're done.  */
9718       if (type_spec)
9719         {
9720           if (declares_class_or_enum)
9721             *declares_class_or_enum = 2;
9722           if (decl_specs)
9723             cp_parser_set_decl_spec_type (decl_specs,
9724                                           type_spec,
9725                                           /*user_defined_p=*/true);
9726           return type_spec;
9727         }
9728       else
9729         goto elaborated_type_specifier;
9730
9731       /* Any of these indicate either a class-specifier, or an
9732          elaborated-type-specifier.  */
9733     case RID_CLASS:
9734     case RID_STRUCT:
9735     case RID_UNION:
9736       /* Parse tentatively so that we can back up if we don't find a
9737          class-specifier.  */
9738       cp_parser_parse_tentatively (parser);
9739       /* Look for the class-specifier.  */
9740       type_spec = cp_parser_class_specifier (parser);
9741       /* If that worked, we're done.  */
9742       if (cp_parser_parse_definitely (parser))
9743         {
9744           if (declares_class_or_enum)
9745             *declares_class_or_enum = 2;
9746           if (decl_specs)
9747             cp_parser_set_decl_spec_type (decl_specs,
9748                                           type_spec,
9749                                           /*user_defined_p=*/true);
9750           return type_spec;
9751         }
9752
9753       /* Fall through.  */
9754     elaborated_type_specifier:
9755       /* We're declaring (not defining) a class or enum.  */
9756       if (declares_class_or_enum)
9757         *declares_class_or_enum = 1;
9758
9759       /* Fall through.  */
9760     case RID_TYPENAME:
9761       /* Look for an elaborated-type-specifier.  */
9762       type_spec
9763         = (cp_parser_elaborated_type_specifier
9764            (parser,
9765             decl_specs && decl_specs->specs[(int) ds_friend],
9766             is_declaration));
9767       if (decl_specs)
9768         cp_parser_set_decl_spec_type (decl_specs,
9769                                       type_spec,
9770                                       /*user_defined_p=*/true);
9771       return type_spec;
9772
9773     case RID_CONST:
9774       ds = ds_const;
9775       if (is_cv_qualifier)
9776         *is_cv_qualifier = true;
9777       break;
9778
9779     case RID_VOLATILE:
9780       ds = ds_volatile;
9781       if (is_cv_qualifier)
9782         *is_cv_qualifier = true;
9783       break;
9784
9785     case RID_RESTRICT:
9786       ds = ds_restrict;
9787       if (is_cv_qualifier)
9788         *is_cv_qualifier = true;
9789       break;
9790
9791     case RID_COMPLEX:
9792       /* The `__complex__' keyword is a GNU extension.  */
9793       ds = ds_complex;
9794       break;
9795
9796     default:
9797       break;
9798     }
9799
9800   /* Handle simple keywords.  */
9801   if (ds != ds_last)
9802     {
9803       if (decl_specs)
9804         {
9805           ++decl_specs->specs[(int)ds];
9806           decl_specs->any_specifiers_p = true;
9807         }
9808       return cp_lexer_consume_token (parser->lexer)->value;
9809     }
9810
9811   /* If we do not already have a type-specifier, assume we are looking
9812      at a simple-type-specifier.  */
9813   type_spec = cp_parser_simple_type_specifier (parser,
9814                                                decl_specs,
9815                                                flags);
9816
9817   /* If we didn't find a type-specifier, and a type-specifier was not
9818      optional in this context, issue an error message.  */
9819   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9820     {
9821       cp_parser_error (parser, "expected type specifier");
9822       return error_mark_node;
9823     }
9824
9825   return type_spec;
9826 }
9827
9828 /* Parse a simple-type-specifier.
9829
9830    simple-type-specifier:
9831      :: [opt] nested-name-specifier [opt] type-name
9832      :: [opt] nested-name-specifier template template-id
9833      char
9834      wchar_t
9835      bool
9836      short
9837      int
9838      long
9839      signed
9840      unsigned
9841      float
9842      double
9843      void
9844
9845    GNU Extension:
9846
9847    simple-type-specifier:
9848      __typeof__ unary-expression
9849      __typeof__ ( type-id )
9850
9851    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9852    appropriately updated.  */
9853
9854 static tree
9855 cp_parser_simple_type_specifier (cp_parser* parser,
9856                                  cp_decl_specifier_seq *decl_specs,
9857                                  cp_parser_flags flags)
9858 {
9859   tree type = NULL_TREE;
9860   cp_token *token;
9861
9862   /* Peek at the next token.  */
9863   token = cp_lexer_peek_token (parser->lexer);
9864
9865   /* If we're looking at a keyword, things are easy.  */
9866   switch (token->keyword)
9867     {
9868     case RID_CHAR:
9869       if (decl_specs)
9870         decl_specs->explicit_char_p = true;
9871       type = char_type_node;
9872       break;
9873     case RID_WCHAR:
9874       type = wchar_type_node;
9875       break;
9876     case RID_BOOL:
9877       type = boolean_type_node;
9878       break;
9879     case RID_SHORT:
9880       if (decl_specs)
9881         ++decl_specs->specs[(int) ds_short];
9882       type = short_integer_type_node;
9883       break;
9884     case RID_INT:
9885       if (decl_specs)
9886         decl_specs->explicit_int_p = true;
9887       type = integer_type_node;
9888       break;
9889     case RID_LONG:
9890       if (decl_specs)
9891         ++decl_specs->specs[(int) ds_long];
9892       type = long_integer_type_node;
9893       break;
9894     case RID_SIGNED:
9895       if (decl_specs)
9896         ++decl_specs->specs[(int) ds_signed];
9897       type = integer_type_node;
9898       break;
9899     case RID_UNSIGNED:
9900       if (decl_specs)
9901         ++decl_specs->specs[(int) ds_unsigned];
9902       type = unsigned_type_node;
9903       break;
9904     case RID_FLOAT:
9905       type = float_type_node;
9906       break;
9907     case RID_DOUBLE:
9908       type = double_type_node;
9909       break;
9910     case RID_VOID:
9911       type = void_type_node;
9912       break;
9913
9914     case RID_TYPEOF:
9915       /* Consume the `typeof' token.  */
9916       cp_lexer_consume_token (parser->lexer);
9917       /* Parse the operand to `typeof'.  */
9918       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9919       /* If it is not already a TYPE, take its type.  */
9920       if (!TYPE_P (type))
9921         type = finish_typeof (type);
9922
9923       if (decl_specs)
9924         cp_parser_set_decl_spec_type (decl_specs, type,
9925                                       /*user_defined_p=*/true);
9926
9927       return type;
9928
9929     default:
9930       break;
9931     }
9932
9933   /* If the type-specifier was for a built-in type, we're done.  */
9934   if (type)
9935     {
9936       tree id;
9937
9938       /* Record the type.  */
9939       if (decl_specs
9940           && (token->keyword != RID_SIGNED
9941               && token->keyword != RID_UNSIGNED
9942               && token->keyword != RID_SHORT
9943               && token->keyword != RID_LONG))
9944         cp_parser_set_decl_spec_type (decl_specs,
9945                                       type,
9946                                       /*user_defined=*/false);
9947       if (decl_specs)
9948         decl_specs->any_specifiers_p = true;
9949
9950       /* Consume the token.  */
9951       id = cp_lexer_consume_token (parser->lexer)->value;
9952
9953       /* There is no valid C++ program where a non-template type is
9954          followed by a "<".  That usually indicates that the user thought
9955          that the type was a template.  */
9956       cp_parser_check_for_invalid_template_id (parser, type);
9957
9958       return TYPE_NAME (type);
9959     }
9960
9961   /* The type-specifier must be a user-defined type.  */
9962   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9963     {
9964       bool qualified_p;
9965       bool global_p;
9966
9967       /* Don't gobble tokens or issue error messages if this is an
9968          optional type-specifier.  */
9969       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9970         cp_parser_parse_tentatively (parser);
9971
9972       /* Look for the optional `::' operator.  */
9973       global_p
9974         = (cp_parser_global_scope_opt (parser,
9975                                        /*current_scope_valid_p=*/false)
9976            != NULL_TREE);
9977       /* Look for the nested-name specifier.  */
9978       qualified_p
9979         = (cp_parser_nested_name_specifier_opt (parser,
9980                                                 /*typename_keyword_p=*/false,
9981                                                 /*check_dependency_p=*/true,
9982                                                 /*type_p=*/false,
9983                                                 /*is_declaration=*/false)
9984            != NULL_TREE);
9985       /* If we have seen a nested-name-specifier, and the next token
9986          is `template', then we are using the template-id production.  */
9987       if (parser->scope
9988           && cp_parser_optional_template_keyword (parser))
9989         {
9990           /* Look for the template-id.  */
9991           type = cp_parser_template_id (parser,
9992                                         /*template_keyword_p=*/true,
9993                                         /*check_dependency_p=*/true,
9994                                         /*is_declaration=*/false);
9995           /* If the template-id did not name a type, we are out of
9996              luck.  */
9997           if (TREE_CODE (type) != TYPE_DECL)
9998             {
9999               cp_parser_error (parser, "expected template-id for type");
10000               type = NULL_TREE;
10001             }
10002         }
10003       /* Otherwise, look for a type-name.  */
10004       else
10005         type = cp_parser_type_name (parser);
10006       /* Keep track of all name-lookups performed in class scopes.  */
10007       if (type
10008           && !global_p
10009           && !qualified_p
10010           && TREE_CODE (type) == TYPE_DECL
10011           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10012         maybe_note_name_used_in_class (DECL_NAME (type), type);
10013       /* If it didn't work out, we don't have a TYPE.  */
10014       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10015           && !cp_parser_parse_definitely (parser))
10016         type = NULL_TREE;
10017       if (type && decl_specs)
10018         cp_parser_set_decl_spec_type (decl_specs, type,
10019                                       /*user_defined=*/true);
10020     }
10021
10022   /* If we didn't get a type-name, issue an error message.  */
10023   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10024     {
10025       cp_parser_error (parser, "expected type-name");
10026       return error_mark_node;
10027     }
10028
10029   /* There is no valid C++ program where a non-template type is
10030      followed by a "<".  That usually indicates that the user thought
10031      that the type was a template.  */
10032   if (type && type != error_mark_node)
10033     {
10034       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10035          If it is, then the '<'...'>' enclose protocol names rather than
10036          template arguments, and so everything is fine.  */
10037       if (c_dialect_objc ()
10038           && (objc_is_id (type) || objc_is_class_name (type)))
10039         {
10040           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10041           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10042
10043           /* Clobber the "unqualified" type previously entered into
10044              DECL_SPECS with the new, improved protocol-qualified version.  */
10045           if (decl_specs)
10046             decl_specs->type = qual_type;
10047
10048           return qual_type;
10049         }
10050
10051       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10052     }
10053
10054   return type;
10055 }
10056
10057 /* Parse a type-name.
10058
10059    type-name:
10060      class-name
10061      enum-name
10062      typedef-name
10063
10064    enum-name:
10065      identifier
10066
10067    typedef-name:
10068      identifier
10069
10070    Returns a TYPE_DECL for the type.  */
10071
10072 static tree
10073 cp_parser_type_name (cp_parser* parser)
10074 {
10075   tree type_decl;
10076   tree identifier;
10077
10078   /* We can't know yet whether it is a class-name or not.  */
10079   cp_parser_parse_tentatively (parser);
10080   /* Try a class-name.  */
10081   type_decl = cp_parser_class_name (parser,
10082                                     /*typename_keyword_p=*/false,
10083                                     /*template_keyword_p=*/false,
10084                                     none_type,
10085                                     /*check_dependency_p=*/true,
10086                                     /*class_head_p=*/false,
10087                                     /*is_declaration=*/false);
10088   /* If it's not a class-name, keep looking.  */
10089   if (!cp_parser_parse_definitely (parser))
10090     {
10091       /* It must be a typedef-name or an enum-name.  */
10092       identifier = cp_parser_identifier (parser);
10093       if (identifier == error_mark_node)
10094         return error_mark_node;
10095
10096       /* Look up the type-name.  */
10097       type_decl = cp_parser_lookup_name_simple (parser, identifier);
10098
10099       if (TREE_CODE (type_decl) != TYPE_DECL
10100           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10101         {
10102           /* See if this is an Objective-C type.  */
10103           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10104           tree type = objc_get_protocol_qualified_type (identifier, protos);
10105           if (type)
10106             type_decl = TYPE_NAME (type);
10107         }
10108
10109       /* Issue an error if we did not find a type-name.  */
10110       if (TREE_CODE (type_decl) != TYPE_DECL)
10111         {
10112           if (!cp_parser_simulate_error (parser))
10113             cp_parser_name_lookup_error (parser, identifier, type_decl,
10114                                          "is not a type");
10115           type_decl = error_mark_node;
10116         }
10117       /* Remember that the name was used in the definition of the
10118          current class so that we can check later to see if the
10119          meaning would have been different after the class was
10120          entirely defined.  */
10121       else if (type_decl != error_mark_node
10122                && !parser->scope)
10123         maybe_note_name_used_in_class (identifier, type_decl);
10124     }
10125
10126   return type_decl;
10127 }
10128
10129
10130 /* Parse an elaborated-type-specifier.  Note that the grammar given
10131    here incorporates the resolution to DR68.
10132
10133    elaborated-type-specifier:
10134      class-key :: [opt] nested-name-specifier [opt] identifier
10135      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10136      enum :: [opt] nested-name-specifier [opt] identifier
10137      typename :: [opt] nested-name-specifier identifier
10138      typename :: [opt] nested-name-specifier template [opt]
10139        template-id
10140
10141    GNU extension:
10142
10143    elaborated-type-specifier:
10144      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10145      class-key attributes :: [opt] nested-name-specifier [opt]
10146                template [opt] template-id
10147      enum attributes :: [opt] nested-name-specifier [opt] identifier
10148
10149    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10150    declared `friend'.  If IS_DECLARATION is TRUE, then this
10151    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10152    something is being declared.
10153
10154    Returns the TYPE specified.  */
10155
10156 static tree
10157 cp_parser_elaborated_type_specifier (cp_parser* parser,
10158                                      bool is_friend,
10159                                      bool is_declaration)
10160 {
10161   enum tag_types tag_type;
10162   tree identifier;
10163   tree type = NULL_TREE;
10164   tree attributes = NULL_TREE;
10165
10166   /* See if we're looking at the `enum' keyword.  */
10167   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10168     {
10169       /* Consume the `enum' token.  */
10170       cp_lexer_consume_token (parser->lexer);
10171       /* Remember that it's an enumeration type.  */
10172       tag_type = enum_type;
10173       /* Parse the attributes.  */
10174       attributes = cp_parser_attributes_opt (parser);
10175     }
10176   /* Or, it might be `typename'.  */
10177   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10178                                            RID_TYPENAME))
10179     {
10180       /* Consume the `typename' token.  */
10181       cp_lexer_consume_token (parser->lexer);
10182       /* Remember that it's a `typename' type.  */
10183       tag_type = typename_type;
10184       /* The `typename' keyword is only allowed in templates.  */
10185       if (!processing_template_decl)
10186         pedwarn ("using %<typename%> outside of template");
10187     }
10188   /* Otherwise it must be a class-key.  */
10189   else
10190     {
10191       tag_type = cp_parser_class_key (parser);
10192       if (tag_type == none_type)
10193         return error_mark_node;
10194       /* Parse the attributes.  */
10195       attributes = cp_parser_attributes_opt (parser);
10196     }
10197
10198   /* Look for the `::' operator.  */
10199   cp_parser_global_scope_opt (parser,
10200                               /*current_scope_valid_p=*/false);
10201   /* Look for the nested-name-specifier.  */
10202   if (tag_type == typename_type)
10203     {
10204       if (!cp_parser_nested_name_specifier (parser,
10205                                            /*typename_keyword_p=*/true,
10206                                            /*check_dependency_p=*/true,
10207                                            /*type_p=*/true,
10208                                             is_declaration))
10209         return error_mark_node;
10210     }
10211   else
10212     /* Even though `typename' is not present, the proposed resolution
10213        to Core Issue 180 says that in `class A<T>::B', `B' should be
10214        considered a type-name, even if `A<T>' is dependent.  */
10215     cp_parser_nested_name_specifier_opt (parser,
10216                                          /*typename_keyword_p=*/true,
10217                                          /*check_dependency_p=*/true,
10218                                          /*type_p=*/true,
10219                                          is_declaration);
10220   /* For everything but enumeration types, consider a template-id.  */
10221   /* For an enumeration type, consider only a plain identifier.  */
10222   if (tag_type != enum_type)
10223     {
10224       bool template_p = false;
10225       tree decl;
10226
10227       /* Allow the `template' keyword.  */
10228       template_p = cp_parser_optional_template_keyword (parser);
10229       /* If we didn't see `template', we don't know if there's a
10230          template-id or not.  */
10231       if (!template_p)
10232         cp_parser_parse_tentatively (parser);
10233       /* Parse the template-id.  */
10234       decl = cp_parser_template_id (parser, template_p,
10235                                     /*check_dependency_p=*/true,
10236                                     is_declaration);
10237       /* If we didn't find a template-id, look for an ordinary
10238          identifier.  */
10239       if (!template_p && !cp_parser_parse_definitely (parser))
10240         ;
10241       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10242          in effect, then we must assume that, upon instantiation, the
10243          template will correspond to a class.  */
10244       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10245                && tag_type == typename_type)
10246         type = make_typename_type (parser->scope, decl,
10247                                    typename_type,
10248                                    /*complain=*/tf_error);
10249       else
10250         type = TREE_TYPE (decl);
10251     }
10252
10253   if (!type)
10254     {
10255       identifier = cp_parser_identifier (parser);
10256
10257       if (identifier == error_mark_node)
10258         {
10259           parser->scope = NULL_TREE;
10260           return error_mark_node;
10261         }
10262
10263       /* For a `typename', we needn't call xref_tag.  */
10264       if (tag_type == typename_type
10265           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10266         return cp_parser_make_typename_type (parser, parser->scope,
10267                                              identifier);
10268       /* Look up a qualified name in the usual way.  */
10269       if (parser->scope)
10270         {
10271           tree decl;
10272
10273           decl = cp_parser_lookup_name (parser, identifier,
10274                                         tag_type,
10275                                         /*is_template=*/false,
10276                                         /*is_namespace=*/false,
10277                                         /*check_dependency=*/true,
10278                                         /*ambiguous_decls=*/NULL);
10279
10280           /* If we are parsing friend declaration, DECL may be a
10281              TEMPLATE_DECL tree node here.  However, we need to check
10282              whether this TEMPLATE_DECL results in valid code.  Consider
10283              the following example:
10284
10285                namespace N {
10286                  template <class T> class C {};
10287                }
10288                class X {
10289                  template <class T> friend class N::C; // #1, valid code
10290                };
10291                template <class T> class Y {
10292                  friend class N::C;                    // #2, invalid code
10293                };
10294
10295              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10296              name lookup of `N::C'.  We see that friend declaration must
10297              be template for the code to be valid.  Note that
10298              processing_template_decl does not work here since it is
10299              always 1 for the above two cases.  */
10300
10301           decl = (cp_parser_maybe_treat_template_as_class
10302                   (decl, /*tag_name_p=*/is_friend
10303                          && parser->num_template_parameter_lists));
10304
10305           if (TREE_CODE (decl) != TYPE_DECL)
10306             {
10307               cp_parser_diagnose_invalid_type_name (parser,
10308                                                     parser->scope,
10309                                                     identifier);
10310               return error_mark_node;
10311             }
10312
10313           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10314             {
10315               bool allow_template = (parser->num_template_parameter_lists
10316                                       || DECL_SELF_REFERENCE_P (decl));
10317               type = check_elaborated_type_specifier (tag_type, decl, 
10318                                                       allow_template);
10319
10320               if (type == error_mark_node)
10321                 return error_mark_node;
10322             }
10323
10324           type = TREE_TYPE (decl);
10325         }
10326       else
10327         {
10328           /* An elaborated-type-specifier sometimes introduces a new type and
10329              sometimes names an existing type.  Normally, the rule is that it
10330              introduces a new type only if there is not an existing type of
10331              the same name already in scope.  For example, given:
10332
10333                struct S {};
10334                void f() { struct S s; }
10335
10336              the `struct S' in the body of `f' is the same `struct S' as in
10337              the global scope; the existing definition is used.  However, if
10338              there were no global declaration, this would introduce a new
10339              local class named `S'.
10340
10341              An exception to this rule applies to the following code:
10342
10343                namespace N { struct S; }
10344
10345              Here, the elaborated-type-specifier names a new type
10346              unconditionally; even if there is already an `S' in the
10347              containing scope this declaration names a new type.
10348              This exception only applies if the elaborated-type-specifier
10349              forms the complete declaration:
10350
10351                [class.name]
10352
10353                A declaration consisting solely of `class-key identifier ;' is
10354                either a redeclaration of the name in the current scope or a
10355                forward declaration of the identifier as a class name.  It
10356                introduces the name into the current scope.
10357
10358              We are in this situation precisely when the next token is a `;'.
10359
10360              An exception to the exception is that a `friend' declaration does
10361              *not* name a new type; i.e., given:
10362
10363                struct S { friend struct T; };
10364
10365              `T' is not a new type in the scope of `S'.
10366
10367              Also, `new struct S' or `sizeof (struct S)' never results in the
10368              definition of a new type; a new type can only be declared in a
10369              declaration context.  */
10370
10371           tag_scope ts;
10372           bool template_p;
10373
10374           if (is_friend)
10375             /* Friends have special name lookup rules.  */
10376             ts = ts_within_enclosing_non_class;
10377           else if (is_declaration
10378                    && cp_lexer_next_token_is (parser->lexer,
10379                                               CPP_SEMICOLON))
10380             /* This is a `class-key identifier ;' */
10381             ts = ts_current;
10382           else
10383             ts = ts_global;
10384
10385           template_p =
10386             (parser->num_template_parameter_lists
10387              && (cp_parser_next_token_starts_class_definition_p (parser)
10388                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10389           /* An unqualified name was used to reference this type, so
10390              there were no qualifying templates.  */
10391           if (!cp_parser_check_template_parameters (parser,
10392                                                     /*num_templates=*/0))
10393             return error_mark_node;
10394           type = xref_tag (tag_type, identifier, ts, template_p);
10395         }
10396     }
10397
10398   if (type == error_mark_node)
10399     return error_mark_node;
10400
10401   /* Allow attributes on forward declarations of classes.  */
10402   if (attributes)
10403     {
10404       if (TREE_CODE (type) == TYPENAME_TYPE)
10405         warning (OPT_Wattributes,
10406                  "attributes ignored on uninstantiated type");
10407       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10408                && ! processing_explicit_instantiation)
10409         warning (OPT_Wattributes,
10410                  "attributes ignored on template instantiation");
10411       else if (is_declaration && cp_parser_declares_only_class_p (parser))
10412         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10413       else
10414         warning (OPT_Wattributes,
10415                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10416     }
10417
10418   if (tag_type != enum_type)
10419     cp_parser_check_class_key (tag_type, type);
10420
10421   /* A "<" cannot follow an elaborated type specifier.  If that
10422      happens, the user was probably trying to form a template-id.  */
10423   cp_parser_check_for_invalid_template_id (parser, type);
10424
10425   return type;
10426 }
10427
10428 /* Parse an enum-specifier.
10429
10430    enum-specifier:
10431      enum identifier [opt] { enumerator-list [opt] }
10432
10433    GNU Extensions:
10434      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10435        attributes[opt]
10436
10437    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10438    if the token stream isn't an enum-specifier after all.  */
10439
10440 static tree
10441 cp_parser_enum_specifier (cp_parser* parser)
10442 {
10443   tree identifier;
10444   tree type;
10445   tree attributes;
10446
10447   /* Parse tentatively so that we can back up if we don't find a
10448      enum-specifier.  */
10449   cp_parser_parse_tentatively (parser);
10450
10451   /* Caller guarantees that the current token is 'enum', an identifier
10452      possibly follows, and the token after that is an opening brace.
10453      If we don't have an identifier, fabricate an anonymous name for
10454      the enumeration being defined.  */
10455   cp_lexer_consume_token (parser->lexer);
10456
10457   attributes = cp_parser_attributes_opt (parser);
10458
10459   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10460     identifier = cp_parser_identifier (parser);
10461   else
10462     identifier = make_anon_name ();
10463
10464   /* Look for the `{' but don't consume it yet.  */
10465   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10466     cp_parser_simulate_error (parser);
10467
10468   if (!cp_parser_parse_definitely (parser))
10469     return NULL_TREE;
10470
10471   /* Issue an error message if type-definitions are forbidden here.  */
10472   if (!cp_parser_check_type_definition (parser))
10473     type = error_mark_node;
10474   else
10475     /* Create the new type.  We do this before consuming the opening
10476        brace so the enum will be recorded as being on the line of its
10477        tag (or the 'enum' keyword, if there is no tag).  */
10478     type = start_enum (identifier);
10479   
10480   /* Consume the opening brace.  */
10481   cp_lexer_consume_token (parser->lexer);
10482
10483   if (type == error_mark_node)
10484     {
10485       cp_parser_skip_to_end_of_block_or_statement (parser);
10486       return error_mark_node;
10487     }
10488
10489   /* If the next token is not '}', then there are some enumerators.  */
10490   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10491     cp_parser_enumerator_list (parser, type);
10492
10493   /* Consume the final '}'.  */
10494   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10495
10496   /* Look for trailing attributes to apply to this enumeration, and
10497      apply them if appropriate.  */
10498   if (cp_parser_allow_gnu_extensions_p (parser))
10499     {
10500       tree trailing_attr = cp_parser_attributes_opt (parser);
10501       cplus_decl_attributes (&type,
10502                              trailing_attr,
10503                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10504     }
10505
10506   /* Finish up the enumeration.  */
10507   finish_enum (type);
10508
10509   return type;
10510 }
10511
10512 /* Parse an enumerator-list.  The enumerators all have the indicated
10513    TYPE.
10514
10515    enumerator-list:
10516      enumerator-definition
10517      enumerator-list , enumerator-definition  */
10518
10519 static void
10520 cp_parser_enumerator_list (cp_parser* parser, tree type)
10521 {
10522   while (true)
10523     {
10524       /* Parse an enumerator-definition.  */
10525       cp_parser_enumerator_definition (parser, type);
10526
10527       /* If the next token is not a ',', we've reached the end of
10528          the list.  */
10529       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10530         break;
10531       /* Otherwise, consume the `,' and keep going.  */
10532       cp_lexer_consume_token (parser->lexer);
10533       /* If the next token is a `}', there is a trailing comma.  */
10534       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10535         {
10536           if (pedantic && !in_system_header)
10537             pedwarn ("comma at end of enumerator list");
10538           break;
10539         }
10540     }
10541 }
10542
10543 /* Parse an enumerator-definition.  The enumerator has the indicated
10544    TYPE.
10545
10546    enumerator-definition:
10547      enumerator
10548      enumerator = constant-expression
10549
10550    enumerator:
10551      identifier  */
10552
10553 static void
10554 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10555 {
10556   tree identifier;
10557   tree value;
10558
10559   /* Look for the identifier.  */
10560   identifier = cp_parser_identifier (parser);
10561   if (identifier == error_mark_node)
10562     return;
10563
10564   /* If the next token is an '=', then there is an explicit value.  */
10565   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10566     {
10567       /* Consume the `=' token.  */
10568       cp_lexer_consume_token (parser->lexer);
10569       /* Parse the value.  */
10570       value = cp_parser_constant_expression (parser,
10571                                              /*allow_non_constant_p=*/false,
10572                                              NULL);
10573     }
10574   else
10575     value = NULL_TREE;
10576
10577   /* Create the enumerator.  */
10578   build_enumerator (identifier, value, type);
10579 }
10580
10581 /* Parse a namespace-name.
10582
10583    namespace-name:
10584      original-namespace-name
10585      namespace-alias
10586
10587    Returns the NAMESPACE_DECL for the namespace.  */
10588
10589 static tree
10590 cp_parser_namespace_name (cp_parser* parser)
10591 {
10592   tree identifier;
10593   tree namespace_decl;
10594
10595   /* Get the name of the namespace.  */
10596   identifier = cp_parser_identifier (parser);
10597   if (identifier == error_mark_node)
10598     return error_mark_node;
10599
10600   /* Look up the identifier in the currently active scope.  Look only
10601      for namespaces, due to:
10602
10603        [basic.lookup.udir]
10604
10605        When looking up a namespace-name in a using-directive or alias
10606        definition, only namespace names are considered.
10607
10608      And:
10609
10610        [basic.lookup.qual]
10611
10612        During the lookup of a name preceding the :: scope resolution
10613        operator, object, function, and enumerator names are ignored.
10614
10615      (Note that cp_parser_class_or_namespace_name only calls this
10616      function if the token after the name is the scope resolution
10617      operator.)  */
10618   namespace_decl = cp_parser_lookup_name (parser, identifier,
10619                                           none_type,
10620                                           /*is_template=*/false,
10621                                           /*is_namespace=*/true,
10622                                           /*check_dependency=*/true,
10623                                           /*ambiguous_decls=*/NULL);
10624   /* If it's not a namespace, issue an error.  */
10625   if (namespace_decl == error_mark_node
10626       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10627     {
10628       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10629         error ("%qD is not a namespace-name", identifier);
10630       cp_parser_error (parser, "expected namespace-name");
10631       namespace_decl = error_mark_node;
10632     }
10633
10634   return namespace_decl;
10635 }
10636
10637 /* Parse a namespace-definition.
10638
10639    namespace-definition:
10640      named-namespace-definition
10641      unnamed-namespace-definition
10642
10643    named-namespace-definition:
10644      original-namespace-definition
10645      extension-namespace-definition
10646
10647    original-namespace-definition:
10648      namespace identifier { namespace-body }
10649
10650    extension-namespace-definition:
10651      namespace original-namespace-name { namespace-body }
10652
10653    unnamed-namespace-definition:
10654      namespace { namespace-body } */
10655
10656 static void
10657 cp_parser_namespace_definition (cp_parser* parser)
10658 {
10659   tree identifier, attribs;
10660
10661   /* Look for the `namespace' keyword.  */
10662   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10663
10664   /* Get the name of the namespace.  We do not attempt to distinguish
10665      between an original-namespace-definition and an
10666      extension-namespace-definition at this point.  The semantic
10667      analysis routines are responsible for that.  */
10668   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10669     identifier = cp_parser_identifier (parser);
10670   else
10671     identifier = NULL_TREE;
10672
10673   /* Parse any specified attributes.  */
10674   attribs = cp_parser_attributes_opt (parser);
10675
10676   /* Look for the `{' to start the namespace.  */
10677   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10678   /* Start the namespace.  */
10679   push_namespace_with_attribs (identifier, attribs);
10680   /* Parse the body of the namespace.  */
10681   cp_parser_namespace_body (parser);
10682   /* Finish the namespace.  */
10683   pop_namespace ();
10684   /* Look for the final `}'.  */
10685   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10686 }
10687
10688 /* Parse a namespace-body.
10689
10690    namespace-body:
10691      declaration-seq [opt]  */
10692
10693 static void
10694 cp_parser_namespace_body (cp_parser* parser)
10695 {
10696   cp_parser_declaration_seq_opt (parser);
10697 }
10698
10699 /* Parse a namespace-alias-definition.
10700
10701    namespace-alias-definition:
10702      namespace identifier = qualified-namespace-specifier ;  */
10703
10704 static void
10705 cp_parser_namespace_alias_definition (cp_parser* parser)
10706 {
10707   tree identifier;
10708   tree namespace_specifier;
10709
10710   /* Look for the `namespace' keyword.  */
10711   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10712   /* Look for the identifier.  */
10713   identifier = cp_parser_identifier (parser);
10714   if (identifier == error_mark_node)
10715     return;
10716   /* Look for the `=' token.  */
10717   cp_parser_require (parser, CPP_EQ, "`='");
10718   /* Look for the qualified-namespace-specifier.  */
10719   namespace_specifier
10720     = cp_parser_qualified_namespace_specifier (parser);
10721   /* Look for the `;' token.  */
10722   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10723
10724   /* Register the alias in the symbol table.  */
10725   do_namespace_alias (identifier, namespace_specifier);
10726 }
10727
10728 /* Parse a qualified-namespace-specifier.
10729
10730    qualified-namespace-specifier:
10731      :: [opt] nested-name-specifier [opt] namespace-name
10732
10733    Returns a NAMESPACE_DECL corresponding to the specified
10734    namespace.  */
10735
10736 static tree
10737 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10738 {
10739   /* Look for the optional `::'.  */
10740   cp_parser_global_scope_opt (parser,
10741                               /*current_scope_valid_p=*/false);
10742
10743   /* Look for the optional nested-name-specifier.  */
10744   cp_parser_nested_name_specifier_opt (parser,
10745                                        /*typename_keyword_p=*/false,
10746                                        /*check_dependency_p=*/true,
10747                                        /*type_p=*/false,
10748                                        /*is_declaration=*/true);
10749
10750   return cp_parser_namespace_name (parser);
10751 }
10752
10753 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10754    access declaration.
10755
10756    using-declaration:
10757      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10758      using :: unqualified-id ;  
10759
10760    access-declaration:
10761      qualified-id ;  
10762
10763    */
10764
10765 static bool
10766 cp_parser_using_declaration (cp_parser* parser, 
10767                              bool access_declaration_p)
10768 {
10769   cp_token *token;
10770   bool typename_p = false;
10771   bool global_scope_p;
10772   tree decl;
10773   tree identifier;
10774   tree qscope;
10775
10776   if (access_declaration_p)
10777     cp_parser_parse_tentatively (parser);
10778   else
10779     {
10780       /* Look for the `using' keyword.  */
10781       cp_parser_require_keyword (parser, RID_USING, "`using'");
10782       
10783       /* Peek at the next token.  */
10784       token = cp_lexer_peek_token (parser->lexer);
10785       /* See if it's `typename'.  */
10786       if (token->keyword == RID_TYPENAME)
10787         {
10788           /* Remember that we've seen it.  */
10789           typename_p = true;
10790           /* Consume the `typename' token.  */
10791           cp_lexer_consume_token (parser->lexer);
10792         }
10793     }
10794
10795   /* Look for the optional global scope qualification.  */
10796   global_scope_p
10797     = (cp_parser_global_scope_opt (parser,
10798                                    /*current_scope_valid_p=*/false)
10799        != NULL_TREE);
10800
10801   /* If we saw `typename', or didn't see `::', then there must be a
10802      nested-name-specifier present.  */
10803   if (typename_p || !global_scope_p)
10804     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10805                                               /*check_dependency_p=*/true,
10806                                               /*type_p=*/false,
10807                                               /*is_declaration=*/true);
10808   /* Otherwise, we could be in either of the two productions.  In that
10809      case, treat the nested-name-specifier as optional.  */
10810   else
10811     qscope = cp_parser_nested_name_specifier_opt (parser,
10812                                                   /*typename_keyword_p=*/false,
10813                                                   /*check_dependency_p=*/true,
10814                                                   /*type_p=*/false,
10815                                                   /*is_declaration=*/true);
10816   if (!qscope)
10817     qscope = global_namespace;
10818
10819   if (access_declaration_p && cp_parser_error_occurred (parser))
10820     /* Something has already gone wrong; there's no need to parse
10821        further.  Since an error has occurred, the return value of
10822        cp_parser_parse_definitely will be false, as required.  */
10823     return cp_parser_parse_definitely (parser);
10824
10825   /* Parse the unqualified-id.  */
10826   identifier = cp_parser_unqualified_id (parser,
10827                                          /*template_keyword_p=*/false,
10828                                          /*check_dependency_p=*/true,
10829                                          /*declarator_p=*/true,
10830                                          /*optional_p=*/false);
10831
10832   if (access_declaration_p)
10833     {
10834       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10835         cp_parser_simulate_error (parser);
10836       if (!cp_parser_parse_definitely (parser))
10837         return false;
10838     }
10839
10840   /* The function we call to handle a using-declaration is different
10841      depending on what scope we are in.  */
10842   if (qscope == error_mark_node || identifier == error_mark_node)
10843     ;
10844   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10845            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10846     /* [namespace.udecl]
10847
10848        A using declaration shall not name a template-id.  */
10849     error ("a template-id may not appear in a using-declaration");
10850   else
10851     {
10852       if (at_class_scope_p ())
10853         {
10854           /* Create the USING_DECL.  */
10855           decl = do_class_using_decl (parser->scope, identifier);
10856           /* Add it to the list of members in this class.  */
10857           finish_member_declaration (decl);
10858         }
10859       else
10860         {
10861           decl = cp_parser_lookup_name_simple (parser, identifier);
10862           if (decl == error_mark_node)
10863             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10864           else if (!at_namespace_scope_p ())
10865             do_local_using_decl (decl, qscope, identifier);
10866           else
10867             do_toplevel_using_decl (decl, qscope, identifier);
10868         }
10869     }
10870
10871   /* Look for the final `;'.  */
10872   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10873   
10874   return true;
10875 }
10876
10877 /* Parse a using-directive.
10878
10879    using-directive:
10880      using namespace :: [opt] nested-name-specifier [opt]
10881        namespace-name ;  */
10882
10883 static void
10884 cp_parser_using_directive (cp_parser* parser)
10885 {
10886   tree namespace_decl;
10887   tree attribs;
10888
10889   /* Look for the `using' keyword.  */
10890   cp_parser_require_keyword (parser, RID_USING, "`using'");
10891   /* And the `namespace' keyword.  */
10892   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10893   /* Look for the optional `::' operator.  */
10894   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10895   /* And the optional nested-name-specifier.  */
10896   cp_parser_nested_name_specifier_opt (parser,
10897                                        /*typename_keyword_p=*/false,
10898                                        /*check_dependency_p=*/true,
10899                                        /*type_p=*/false,
10900                                        /*is_declaration=*/true);
10901   /* Get the namespace being used.  */
10902   namespace_decl = cp_parser_namespace_name (parser);
10903   /* And any specified attributes.  */
10904   attribs = cp_parser_attributes_opt (parser);
10905   /* Update the symbol table.  */
10906   parse_using_directive (namespace_decl, attribs);
10907   /* Look for the final `;'.  */
10908   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10909 }
10910
10911 /* Parse an asm-definition.
10912
10913    asm-definition:
10914      asm ( string-literal ) ;
10915
10916    GNU Extension:
10917
10918    asm-definition:
10919      asm volatile [opt] ( string-literal ) ;
10920      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10921      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10922                           : asm-operand-list [opt] ) ;
10923      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10924                           : asm-operand-list [opt]
10925                           : asm-operand-list [opt] ) ;  */
10926
10927 static void
10928 cp_parser_asm_definition (cp_parser* parser)
10929 {
10930   tree string;
10931   tree outputs = NULL_TREE;
10932   tree inputs = NULL_TREE;
10933   tree clobbers = NULL_TREE;
10934   tree asm_stmt;
10935   bool volatile_p = false;
10936   bool extended_p = false;
10937
10938   /* Look for the `asm' keyword.  */
10939   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10940   /* See if the next token is `volatile'.  */
10941   if (cp_parser_allow_gnu_extensions_p (parser)
10942       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10943     {
10944       /* Remember that we saw the `volatile' keyword.  */
10945       volatile_p = true;
10946       /* Consume the token.  */
10947       cp_lexer_consume_token (parser->lexer);
10948     }
10949   /* Look for the opening `('.  */
10950   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10951     return;
10952   /* Look for the string.  */
10953   string = cp_parser_string_literal (parser, false, false);
10954   if (string == error_mark_node)
10955     {
10956       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10957                                              /*consume_paren=*/true);
10958       return;
10959     }
10960
10961   /* If we're allowing GNU extensions, check for the extended assembly
10962      syntax.  Unfortunately, the `:' tokens need not be separated by
10963      a space in C, and so, for compatibility, we tolerate that here
10964      too.  Doing that means that we have to treat the `::' operator as
10965      two `:' tokens.  */
10966   if (cp_parser_allow_gnu_extensions_p (parser)
10967       && parser->in_function_body
10968       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10969           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10970     {
10971       bool inputs_p = false;
10972       bool clobbers_p = false;
10973
10974       /* The extended syntax was used.  */
10975       extended_p = true;
10976
10977       /* Look for outputs.  */
10978       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10979         {
10980           /* Consume the `:'.  */
10981           cp_lexer_consume_token (parser->lexer);
10982           /* Parse the output-operands.  */
10983           if (cp_lexer_next_token_is_not (parser->lexer,
10984                                           CPP_COLON)
10985               && cp_lexer_next_token_is_not (parser->lexer,
10986                                              CPP_SCOPE)
10987               && cp_lexer_next_token_is_not (parser->lexer,
10988                                              CPP_CLOSE_PAREN))
10989             outputs = cp_parser_asm_operand_list (parser);
10990         }
10991       /* If the next token is `::', there are no outputs, and the
10992          next token is the beginning of the inputs.  */
10993       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10994         /* The inputs are coming next.  */
10995         inputs_p = true;
10996
10997       /* Look for inputs.  */
10998       if (inputs_p
10999           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11000         {
11001           /* Consume the `:' or `::'.  */
11002           cp_lexer_consume_token (parser->lexer);
11003           /* Parse the output-operands.  */
11004           if (cp_lexer_next_token_is_not (parser->lexer,
11005                                           CPP_COLON)
11006               && cp_lexer_next_token_is_not (parser->lexer,
11007                                              CPP_CLOSE_PAREN))
11008             inputs = cp_parser_asm_operand_list (parser);
11009         }
11010       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11011         /* The clobbers are coming next.  */
11012         clobbers_p = true;
11013
11014       /* Look for clobbers.  */
11015       if (clobbers_p
11016           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11017         {
11018           /* Consume the `:' or `::'.  */
11019           cp_lexer_consume_token (parser->lexer);
11020           /* Parse the clobbers.  */
11021           if (cp_lexer_next_token_is_not (parser->lexer,
11022                                           CPP_CLOSE_PAREN))
11023             clobbers = cp_parser_asm_clobber_list (parser);
11024         }
11025     }
11026   /* Look for the closing `)'.  */
11027   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11028     cp_parser_skip_to_closing_parenthesis (parser, true, false,
11029                                            /*consume_paren=*/true);
11030   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11031
11032   /* Create the ASM_EXPR.  */
11033   if (parser->in_function_body)
11034     {
11035       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11036                                   inputs, clobbers);
11037       /* If the extended syntax was not used, mark the ASM_EXPR.  */
11038       if (!extended_p)
11039         {
11040           tree temp = asm_stmt;
11041           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11042             temp = TREE_OPERAND (temp, 0);
11043
11044           ASM_INPUT_P (temp) = 1;
11045         }
11046     }
11047   else
11048     cgraph_add_asm_node (string);
11049 }
11050
11051 /* Declarators [gram.dcl.decl] */
11052
11053 /* Parse an init-declarator.
11054
11055    init-declarator:
11056      declarator initializer [opt]
11057
11058    GNU Extension:
11059
11060    init-declarator:
11061      declarator asm-specification [opt] attributes [opt] initializer [opt]
11062
11063    function-definition:
11064      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11065        function-body
11066      decl-specifier-seq [opt] declarator function-try-block
11067
11068    GNU Extension:
11069
11070    function-definition:
11071      __extension__ function-definition
11072
11073    The DECL_SPECIFIERS apply to this declarator.  Returns a
11074    representation of the entity declared.  If MEMBER_P is TRUE, then
11075    this declarator appears in a class scope.  The new DECL created by
11076    this declarator is returned.
11077
11078    The CHECKS are access checks that should be performed once we know
11079    what entity is being declared (and, therefore, what classes have
11080    befriended it).
11081
11082    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11083    for a function-definition here as well.  If the declarator is a
11084    declarator for a function-definition, *FUNCTION_DEFINITION_P will
11085    be TRUE upon return.  By that point, the function-definition will
11086    have been completely parsed.
11087
11088    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11089    is FALSE.  */
11090
11091 static tree
11092 cp_parser_init_declarator (cp_parser* parser,
11093                            cp_decl_specifier_seq *decl_specifiers,
11094                            tree checks,
11095                            bool function_definition_allowed_p,
11096                            bool member_p,
11097                            int declares_class_or_enum,
11098                            bool* function_definition_p)
11099 {
11100   cp_token *token;
11101   cp_declarator *declarator;
11102   tree prefix_attributes;
11103   tree attributes;
11104   tree asm_specification;
11105   tree initializer;
11106   tree decl = NULL_TREE;
11107   tree scope;
11108   bool is_initialized;
11109   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
11110      initialized with "= ..", CPP_OPEN_PAREN if initialized with
11111      "(...)".  */
11112   enum cpp_ttype initialization_kind;
11113   bool is_parenthesized_init = false;
11114   bool is_non_constant_init;
11115   int ctor_dtor_or_conv_p;
11116   bool friend_p;
11117   tree pushed_scope = NULL;
11118
11119   /* Gather the attributes that were provided with the
11120      decl-specifiers.  */
11121   prefix_attributes = decl_specifiers->attributes;
11122
11123   /* Assume that this is not the declarator for a function
11124      definition.  */
11125   if (function_definition_p)
11126     *function_definition_p = false;
11127
11128   /* Defer access checks while parsing the declarator; we cannot know
11129      what names are accessible until we know what is being
11130      declared.  */
11131   resume_deferring_access_checks ();
11132
11133   /* Parse the declarator.  */
11134   declarator
11135     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11136                             &ctor_dtor_or_conv_p,
11137                             /*parenthesized_p=*/NULL,
11138                             /*member_p=*/false);
11139   /* Gather up the deferred checks.  */
11140   stop_deferring_access_checks ();
11141
11142   /* If the DECLARATOR was erroneous, there's no need to go
11143      further.  */
11144   if (declarator == cp_error_declarator)
11145     return error_mark_node;
11146
11147   /* Check that the number of template-parameter-lists is OK.  */
11148   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11149     return error_mark_node;
11150
11151   if (declares_class_or_enum & 2)
11152     cp_parser_check_for_definition_in_return_type (declarator,
11153                                                    decl_specifiers->type);
11154
11155   /* Figure out what scope the entity declared by the DECLARATOR is
11156      located in.  `grokdeclarator' sometimes changes the scope, so
11157      we compute it now.  */
11158   scope = get_scope_of_declarator (declarator);
11159
11160   /* If we're allowing GNU extensions, look for an asm-specification
11161      and attributes.  */
11162   if (cp_parser_allow_gnu_extensions_p (parser))
11163     {
11164       /* Look for an asm-specification.  */
11165       asm_specification = cp_parser_asm_specification_opt (parser);
11166       /* And attributes.  */
11167       attributes = cp_parser_attributes_opt (parser);
11168     }
11169   else
11170     {
11171       asm_specification = NULL_TREE;
11172       attributes = NULL_TREE;
11173     }
11174
11175   /* Peek at the next token.  */
11176   token = cp_lexer_peek_token (parser->lexer);
11177   /* Check to see if the token indicates the start of a
11178      function-definition.  */
11179   if (cp_parser_token_starts_function_definition_p (token))
11180     {
11181       if (!function_definition_allowed_p)
11182         {
11183           /* If a function-definition should not appear here, issue an
11184              error message.  */
11185           cp_parser_error (parser,
11186                            "a function-definition is not allowed here");
11187           return error_mark_node;
11188         }
11189       else
11190         {
11191           /* Neither attributes nor an asm-specification are allowed
11192              on a function-definition.  */
11193           if (asm_specification)
11194             error ("an asm-specification is not allowed on a function-definition");
11195           if (attributes)
11196             error ("attributes are not allowed on a function-definition");
11197           /* This is a function-definition.  */
11198           *function_definition_p = true;
11199
11200           /* Parse the function definition.  */
11201           if (member_p)
11202             decl = cp_parser_save_member_function_body (parser,
11203                                                         decl_specifiers,
11204                                                         declarator,
11205                                                         prefix_attributes);
11206           else
11207             decl
11208               = (cp_parser_function_definition_from_specifiers_and_declarator
11209                  (parser, decl_specifiers, prefix_attributes, declarator));
11210
11211           return decl;
11212         }
11213     }
11214
11215   /* [dcl.dcl]
11216
11217      Only in function declarations for constructors, destructors, and
11218      type conversions can the decl-specifier-seq be omitted.
11219
11220      We explicitly postpone this check past the point where we handle
11221      function-definitions because we tolerate function-definitions
11222      that are missing their return types in some modes.  */
11223   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11224     {
11225       cp_parser_error (parser,
11226                        "expected constructor, destructor, or type conversion");
11227       return error_mark_node;
11228     }
11229
11230   /* An `=' or an `(' indicates an initializer.  */
11231   if (token->type == CPP_EQ
11232       || token->type == CPP_OPEN_PAREN)
11233     {
11234       is_initialized = true;
11235       initialization_kind = token->type;
11236     }
11237   else
11238     {
11239       /* If the init-declarator isn't initialized and isn't followed by a
11240          `,' or `;', it's not a valid init-declarator.  */
11241       if (token->type != CPP_COMMA
11242           && token->type != CPP_SEMICOLON)
11243         {
11244           cp_parser_error (parser, "expected initializer");
11245           return error_mark_node;
11246         }
11247       is_initialized = false;
11248       initialization_kind = CPP_EOF;
11249     }
11250
11251   /* Because start_decl has side-effects, we should only call it if we
11252      know we're going ahead.  By this point, we know that we cannot
11253      possibly be looking at any other construct.  */
11254   cp_parser_commit_to_tentative_parse (parser);
11255
11256   /* If the decl specifiers were bad, issue an error now that we're
11257      sure this was intended to be a declarator.  Then continue
11258      declaring the variable(s), as int, to try to cut down on further
11259      errors.  */
11260   if (decl_specifiers->any_specifiers_p
11261       && decl_specifiers->type == error_mark_node)
11262     {
11263       cp_parser_error (parser, "invalid type in declaration");
11264       decl_specifiers->type = integer_type_node;
11265     }
11266
11267   /* Check to see whether or not this declaration is a friend.  */
11268   friend_p = cp_parser_friend_p (decl_specifiers);
11269
11270   /* Enter the newly declared entry in the symbol table.  If we're
11271      processing a declaration in a class-specifier, we wait until
11272      after processing the initializer.  */
11273   if (!member_p)
11274     {
11275       if (parser->in_unbraced_linkage_specification_p)
11276         decl_specifiers->storage_class = sc_extern;
11277       decl = start_decl (declarator, decl_specifiers,
11278                          is_initialized, attributes, prefix_attributes,
11279                          &pushed_scope);
11280     }
11281   else if (scope)
11282     /* Enter the SCOPE.  That way unqualified names appearing in the
11283        initializer will be looked up in SCOPE.  */
11284     pushed_scope = push_scope (scope);
11285
11286   /* Perform deferred access control checks, now that we know in which
11287      SCOPE the declared entity resides.  */
11288   if (!member_p && decl)
11289     {
11290       tree saved_current_function_decl = NULL_TREE;
11291
11292       /* If the entity being declared is a function, pretend that we
11293          are in its scope.  If it is a `friend', it may have access to
11294          things that would not otherwise be accessible.  */
11295       if (TREE_CODE (decl) == FUNCTION_DECL)
11296         {
11297           saved_current_function_decl = current_function_decl;
11298           current_function_decl = decl;
11299         }
11300
11301       /* Perform access checks for template parameters.  */
11302       cp_parser_perform_template_parameter_access_checks (checks);
11303
11304       /* Perform the access control checks for the declarator and the
11305          the decl-specifiers.  */
11306       perform_deferred_access_checks ();
11307
11308       /* Restore the saved value.  */
11309       if (TREE_CODE (decl) == FUNCTION_DECL)
11310         current_function_decl = saved_current_function_decl;
11311     }
11312
11313   /* Parse the initializer.  */
11314   initializer = NULL_TREE;
11315   is_parenthesized_init = false;
11316   is_non_constant_init = true;
11317   if (is_initialized)
11318     {
11319       if (function_declarator_p (declarator))
11320         {
11321            if (initialization_kind == CPP_EQ)
11322              initializer = cp_parser_pure_specifier (parser);
11323            else
11324              {
11325                /* If the declaration was erroneous, we don't really
11326                   know what the user intended, so just silently
11327                   consume the initializer.  */
11328                if (decl != error_mark_node)
11329                  error ("initializer provided for function");
11330                cp_parser_skip_to_closing_parenthesis (parser,
11331                                                       /*recovering=*/true,
11332                                                       /*or_comma=*/false,
11333                                                       /*consume_paren=*/true);
11334              }
11335         }
11336       else
11337         initializer = cp_parser_initializer (parser,
11338                                              &is_parenthesized_init,
11339                                              &is_non_constant_init);
11340     }
11341
11342   /* The old parser allows attributes to appear after a parenthesized
11343      initializer.  Mark Mitchell proposed removing this functionality
11344      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11345      attributes -- but ignores them.  */
11346   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11347     if (cp_parser_attributes_opt (parser))
11348       warning (OPT_Wattributes,
11349                "attributes after parenthesized initializer ignored");
11350
11351   /* For an in-class declaration, use `grokfield' to create the
11352      declaration.  */
11353   if (member_p)
11354     {
11355       if (pushed_scope)
11356         {
11357           pop_scope (pushed_scope);
11358           pushed_scope = false;
11359         }
11360       decl = grokfield (declarator, decl_specifiers,
11361                         initializer, !is_non_constant_init,
11362                         /*asmspec=*/NULL_TREE,
11363                         prefix_attributes);
11364       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11365         cp_parser_save_default_args (parser, decl);
11366     }
11367
11368   /* Finish processing the declaration.  But, skip friend
11369      declarations.  */
11370   if (!friend_p && decl && decl != error_mark_node)
11371     {
11372       cp_finish_decl (decl,
11373                       initializer, !is_non_constant_init,
11374                       asm_specification,
11375                       /* If the initializer is in parentheses, then this is
11376                          a direct-initialization, which means that an
11377                          `explicit' constructor is OK.  Otherwise, an
11378                          `explicit' constructor cannot be used.  */
11379                       ((is_parenthesized_init || !is_initialized)
11380                      ? 0 : LOOKUP_ONLYCONVERTING));
11381     }
11382   if (!friend_p && pushed_scope)
11383     pop_scope (pushed_scope);
11384
11385   return decl;
11386 }
11387
11388 /* Parse a declarator.
11389
11390    declarator:
11391      direct-declarator
11392      ptr-operator declarator
11393
11394    abstract-declarator:
11395      ptr-operator abstract-declarator [opt]
11396      direct-abstract-declarator
11397
11398    GNU Extensions:
11399
11400    declarator:
11401      attributes [opt] direct-declarator
11402      attributes [opt] ptr-operator declarator
11403
11404    abstract-declarator:
11405      attributes [opt] ptr-operator abstract-declarator [opt]
11406      attributes [opt] direct-abstract-declarator
11407
11408    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11409    detect constructor, destructor or conversion operators. It is set
11410    to -1 if the declarator is a name, and +1 if it is a
11411    function. Otherwise it is set to zero. Usually you just want to
11412    test for >0, but internally the negative value is used.
11413
11414    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11415    a decl-specifier-seq unless it declares a constructor, destructor,
11416    or conversion.  It might seem that we could check this condition in
11417    semantic analysis, rather than parsing, but that makes it difficult
11418    to handle something like `f()'.  We want to notice that there are
11419    no decl-specifiers, and therefore realize that this is an
11420    expression, not a declaration.)
11421
11422    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11423    the declarator is a direct-declarator of the form "(...)".
11424
11425    MEMBER_P is true iff this declarator is a member-declarator.  */
11426
11427 static cp_declarator *
11428 cp_parser_declarator (cp_parser* parser,
11429                       cp_parser_declarator_kind dcl_kind,
11430                       int* ctor_dtor_or_conv_p,
11431                       bool* parenthesized_p,
11432                       bool member_p)
11433 {
11434   cp_token *token;
11435   cp_declarator *declarator;
11436   enum tree_code code;
11437   cp_cv_quals cv_quals;
11438   tree class_type;
11439   tree attributes = NULL_TREE;
11440
11441   /* Assume this is not a constructor, destructor, or type-conversion
11442      operator.  */
11443   if (ctor_dtor_or_conv_p)
11444     *ctor_dtor_or_conv_p = 0;
11445
11446   if (cp_parser_allow_gnu_extensions_p (parser))
11447     attributes = cp_parser_attributes_opt (parser);
11448
11449   /* Peek at the next token.  */
11450   token = cp_lexer_peek_token (parser->lexer);
11451
11452   /* Check for the ptr-operator production.  */
11453   cp_parser_parse_tentatively (parser);
11454   /* Parse the ptr-operator.  */
11455   code = cp_parser_ptr_operator (parser,
11456                                  &class_type,
11457                                  &cv_quals);
11458   /* If that worked, then we have a ptr-operator.  */
11459   if (cp_parser_parse_definitely (parser))
11460     {
11461       /* If a ptr-operator was found, then this declarator was not
11462          parenthesized.  */
11463       if (parenthesized_p)
11464         *parenthesized_p = true;
11465       /* The dependent declarator is optional if we are parsing an
11466          abstract-declarator.  */
11467       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11468         cp_parser_parse_tentatively (parser);
11469
11470       /* Parse the dependent declarator.  */
11471       declarator = cp_parser_declarator (parser, dcl_kind,
11472                                          /*ctor_dtor_or_conv_p=*/NULL,
11473                                          /*parenthesized_p=*/NULL,
11474                                          /*member_p=*/false);
11475
11476       /* If we are parsing an abstract-declarator, we must handle the
11477          case where the dependent declarator is absent.  */
11478       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11479           && !cp_parser_parse_definitely (parser))
11480         declarator = NULL;
11481
11482       /* Build the representation of the ptr-operator.  */
11483       if (class_type)
11484         declarator = make_ptrmem_declarator (cv_quals,
11485                                              class_type,
11486                                              declarator);
11487       else if (code == INDIRECT_REF)
11488         declarator = make_pointer_declarator (cv_quals, declarator);
11489       else
11490         declarator = make_reference_declarator (cv_quals, declarator);
11491     }
11492   /* Everything else is a direct-declarator.  */
11493   else
11494     {
11495       if (parenthesized_p)
11496         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11497                                                    CPP_OPEN_PAREN);
11498       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11499                                                 ctor_dtor_or_conv_p,
11500                                                 member_p);
11501     }
11502
11503   if (attributes && declarator && declarator != cp_error_declarator)
11504     declarator->attributes = attributes;
11505
11506   return declarator;
11507 }
11508
11509 /* Parse a direct-declarator or direct-abstract-declarator.
11510
11511    direct-declarator:
11512      declarator-id
11513      direct-declarator ( parameter-declaration-clause )
11514        cv-qualifier-seq [opt]
11515        exception-specification [opt]
11516      direct-declarator [ constant-expression [opt] ]
11517      ( declarator )
11518
11519    direct-abstract-declarator:
11520      direct-abstract-declarator [opt]
11521        ( parameter-declaration-clause )
11522        cv-qualifier-seq [opt]
11523        exception-specification [opt]
11524      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11525      ( abstract-declarator )
11526
11527    Returns a representation of the declarator.  DCL_KIND is
11528    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11529    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11530    we are parsing a direct-declarator.  It is
11531    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11532    of ambiguity we prefer an abstract declarator, as per
11533    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11534    cp_parser_declarator.  */
11535
11536 static cp_declarator *
11537 cp_parser_direct_declarator (cp_parser* parser,
11538                              cp_parser_declarator_kind dcl_kind,
11539                              int* ctor_dtor_or_conv_p,
11540                              bool member_p)
11541 {
11542   cp_token *token;
11543   cp_declarator *declarator = NULL;
11544   tree scope = NULL_TREE;
11545   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11546   bool saved_in_declarator_p = parser->in_declarator_p;
11547   bool first = true;
11548   tree pushed_scope = NULL_TREE;
11549
11550   while (true)
11551     {
11552       /* Peek at the next token.  */
11553       token = cp_lexer_peek_token (parser->lexer);
11554       if (token->type == CPP_OPEN_PAREN)
11555         {
11556           /* This is either a parameter-declaration-clause, or a
11557              parenthesized declarator. When we know we are parsing a
11558              named declarator, it must be a parenthesized declarator
11559              if FIRST is true. For instance, `(int)' is a
11560              parameter-declaration-clause, with an omitted
11561              direct-abstract-declarator. But `((*))', is a
11562              parenthesized abstract declarator. Finally, when T is a
11563              template parameter `(T)' is a
11564              parameter-declaration-clause, and not a parenthesized
11565              named declarator.
11566
11567              We first try and parse a parameter-declaration-clause,
11568              and then try a nested declarator (if FIRST is true).
11569
11570              It is not an error for it not to be a
11571              parameter-declaration-clause, even when FIRST is
11572              false. Consider,
11573
11574                int i (int);
11575                int i (3);
11576
11577              The first is the declaration of a function while the
11578              second is a the definition of a variable, including its
11579              initializer.
11580
11581              Having seen only the parenthesis, we cannot know which of
11582              these two alternatives should be selected.  Even more
11583              complex are examples like:
11584
11585                int i (int (a));
11586                int i (int (3));
11587
11588              The former is a function-declaration; the latter is a
11589              variable initialization.
11590
11591              Thus again, we try a parameter-declaration-clause, and if
11592              that fails, we back out and return.  */
11593
11594           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11595             {
11596               cp_parameter_declarator *params;
11597               unsigned saved_num_template_parameter_lists;
11598
11599               /* In a member-declarator, the only valid interpretation
11600                  of a parenthesis is the start of a
11601                  parameter-declaration-clause.  (It is invalid to
11602                  initialize a static data member with a parenthesized
11603                  initializer; only the "=" form of initialization is
11604                  permitted.)  */
11605               if (!member_p)
11606                 cp_parser_parse_tentatively (parser);
11607
11608               /* Consume the `('.  */
11609               cp_lexer_consume_token (parser->lexer);
11610               if (first)
11611                 {
11612                   /* If this is going to be an abstract declarator, we're
11613                      in a declarator and we can't have default args.  */
11614                   parser->default_arg_ok_p = false;
11615                   parser->in_declarator_p = true;
11616                 }
11617
11618               /* Inside the function parameter list, surrounding
11619                  template-parameter-lists do not apply.  */
11620               saved_num_template_parameter_lists
11621                 = parser->num_template_parameter_lists;
11622               parser->num_template_parameter_lists = 0;
11623
11624               /* Parse the parameter-declaration-clause.  */
11625               params = cp_parser_parameter_declaration_clause (parser);
11626
11627               parser->num_template_parameter_lists
11628                 = saved_num_template_parameter_lists;
11629
11630               /* If all went well, parse the cv-qualifier-seq and the
11631                  exception-specification.  */
11632               if (member_p || cp_parser_parse_definitely (parser))
11633                 {
11634                   cp_cv_quals cv_quals;
11635                   tree exception_specification;
11636
11637                   if (ctor_dtor_or_conv_p)
11638                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11639                   first = false;
11640                   /* Consume the `)'.  */
11641                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11642
11643                   /* Parse the cv-qualifier-seq.  */
11644                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11645                   /* And the exception-specification.  */
11646                   exception_specification
11647                     = cp_parser_exception_specification_opt (parser);
11648
11649                   /* Create the function-declarator.  */
11650                   declarator = make_call_declarator (declarator,
11651                                                      params,
11652                                                      cv_quals,
11653                                                      exception_specification);
11654                   /* Any subsequent parameter lists are to do with
11655                      return type, so are not those of the declared
11656                      function.  */
11657                   parser->default_arg_ok_p = false;
11658
11659                   /* Repeat the main loop.  */
11660                   continue;
11661                 }
11662             }
11663
11664           /* If this is the first, we can try a parenthesized
11665              declarator.  */
11666           if (first)
11667             {
11668               bool saved_in_type_id_in_expr_p;
11669
11670               parser->default_arg_ok_p = saved_default_arg_ok_p;
11671               parser->in_declarator_p = saved_in_declarator_p;
11672
11673               /* Consume the `('.  */
11674               cp_lexer_consume_token (parser->lexer);
11675               /* Parse the nested declarator.  */
11676               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11677               parser->in_type_id_in_expr_p = true;
11678               declarator
11679                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11680                                         /*parenthesized_p=*/NULL,
11681                                         member_p);
11682               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11683               first = false;
11684               /* Expect a `)'.  */
11685               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11686                 declarator = cp_error_declarator;
11687               if (declarator == cp_error_declarator)
11688                 break;
11689
11690               goto handle_declarator;
11691             }
11692           /* Otherwise, we must be done.  */
11693           else
11694             break;
11695         }
11696       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11697                && token->type == CPP_OPEN_SQUARE)
11698         {
11699           /* Parse an array-declarator.  */
11700           tree bounds;
11701
11702           if (ctor_dtor_or_conv_p)
11703             *ctor_dtor_or_conv_p = 0;
11704
11705           first = false;
11706           parser->default_arg_ok_p = false;
11707           parser->in_declarator_p = true;
11708           /* Consume the `['.  */
11709           cp_lexer_consume_token (parser->lexer);
11710           /* Peek at the next token.  */
11711           token = cp_lexer_peek_token (parser->lexer);
11712           /* If the next token is `]', then there is no
11713              constant-expression.  */
11714           if (token->type != CPP_CLOSE_SQUARE)
11715             {
11716               bool non_constant_p;
11717
11718               bounds
11719                 = cp_parser_constant_expression (parser,
11720                                                  /*allow_non_constant=*/true,
11721                                                  &non_constant_p);
11722               if (!non_constant_p)
11723                 bounds = fold_non_dependent_expr (bounds);
11724               /* Normally, the array bound must be an integral constant
11725                  expression.  However, as an extension, we allow VLAs
11726                  in function scopes.  */
11727               else if (!parser->in_function_body)
11728                 {
11729                   error ("array bound is not an integer constant");
11730                   bounds = error_mark_node;
11731                 }
11732             }
11733           else
11734             bounds = NULL_TREE;
11735           /* Look for the closing `]'.  */
11736           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11737             {
11738               declarator = cp_error_declarator;
11739               break;
11740             }
11741
11742           declarator = make_array_declarator (declarator, bounds);
11743         }
11744       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11745         {
11746           tree qualifying_scope;
11747           tree unqualified_name;
11748           special_function_kind sfk;
11749           bool abstract_ok;
11750
11751           /* Parse a declarator-id */
11752           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11753           if (abstract_ok)
11754             cp_parser_parse_tentatively (parser);
11755           unqualified_name
11756             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11757           qualifying_scope = parser->scope;
11758           if (abstract_ok)
11759             {
11760               if (!cp_parser_parse_definitely (parser))
11761                 unqualified_name = error_mark_node;
11762               else if (unqualified_name
11763                        && (qualifying_scope
11764                            || (TREE_CODE (unqualified_name)
11765                                != IDENTIFIER_NODE)))
11766                 {
11767                   cp_parser_error (parser, "expected unqualified-id");
11768                   unqualified_name = error_mark_node;
11769                 }
11770             }
11771
11772           if (!unqualified_name)
11773             return NULL;
11774           if (unqualified_name == error_mark_node)
11775             {
11776               declarator = cp_error_declarator;
11777               break;
11778             }
11779
11780           if (qualifying_scope && at_namespace_scope_p ()
11781               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11782             {
11783               /* In the declaration of a member of a template class
11784                  outside of the class itself, the SCOPE will sometimes
11785                  be a TYPENAME_TYPE.  For example, given:
11786
11787                  template <typename T>
11788                  int S<T>::R::i = 3;
11789
11790                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11791                  this context, we must resolve S<T>::R to an ordinary
11792                  type, rather than a typename type.
11793
11794                  The reason we normally avoid resolving TYPENAME_TYPEs
11795                  is that a specialization of `S' might render
11796                  `S<T>::R' not a type.  However, if `S' is
11797                  specialized, then this `i' will not be used, so there
11798                  is no harm in resolving the types here.  */
11799               tree type;
11800
11801               /* Resolve the TYPENAME_TYPE.  */
11802               type = resolve_typename_type (qualifying_scope,
11803                                             /*only_current_p=*/false);
11804               /* If that failed, the declarator is invalid.  */
11805               if (type == error_mark_node)
11806                 error ("%<%T::%D%> is not a type",
11807                        TYPE_CONTEXT (qualifying_scope),
11808                        TYPE_IDENTIFIER (qualifying_scope));
11809               qualifying_scope = type;
11810             }
11811
11812           sfk = sfk_none;
11813           if (unqualified_name)
11814             {
11815               tree class_type;
11816
11817               if (qualifying_scope
11818                   && CLASS_TYPE_P (qualifying_scope))
11819                 class_type = qualifying_scope;
11820               else
11821                 class_type = current_class_type;
11822
11823               if (TREE_CODE (unqualified_name) == TYPE_DECL)
11824                 {
11825                   tree name_type = TREE_TYPE (unqualified_name);
11826                   if (class_type && same_type_p (name_type, class_type))
11827                     {
11828                       if (qualifying_scope
11829                           && CLASSTYPE_USE_TEMPLATE (name_type))
11830                         {
11831                           error ("invalid use of constructor as a template");
11832                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11833                                   "name the constructor in a qualified name",
11834                                   class_type,
11835                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11836                                   class_type, name_type);
11837                           declarator = cp_error_declarator;
11838                           break;
11839                         }
11840                       else
11841                         unqualified_name = constructor_name (class_type);
11842                     }
11843                   else
11844                     {
11845                       /* We do not attempt to print the declarator
11846                          here because we do not have enough
11847                          information about its original syntactic
11848                          form.  */
11849                       cp_parser_error (parser, "invalid declarator");
11850                       declarator = cp_error_declarator;
11851                       break;
11852                     }
11853                 }
11854
11855               if (class_type)
11856                 {
11857                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11858                     sfk = sfk_destructor;
11859                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11860                     sfk = sfk_conversion;
11861                   else if (/* There's no way to declare a constructor
11862                               for an anonymous type, even if the type
11863                               got a name for linkage purposes.  */
11864                            !TYPE_WAS_ANONYMOUS (class_type)
11865                            && constructor_name_p (unqualified_name,
11866                                                   class_type))
11867                     {
11868                       unqualified_name = constructor_name (class_type);
11869                       sfk = sfk_constructor;
11870                     }
11871
11872                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
11873                     *ctor_dtor_or_conv_p = -1;
11874                 }
11875             }
11876           declarator = make_id_declarator (qualifying_scope,
11877                                            unqualified_name,
11878                                            sfk);
11879           declarator->id_loc = token->location;
11880
11881         handle_declarator:;
11882           scope = get_scope_of_declarator (declarator);
11883           if (scope)
11884             /* Any names that appear after the declarator-id for a
11885                member are looked up in the containing scope.  */
11886             pushed_scope = push_scope (scope);
11887           parser->in_declarator_p = true;
11888           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11889               || (declarator && declarator->kind == cdk_id))
11890             /* Default args are only allowed on function
11891                declarations.  */
11892             parser->default_arg_ok_p = saved_default_arg_ok_p;
11893           else
11894             parser->default_arg_ok_p = false;
11895
11896           first = false;
11897         }
11898       /* We're done.  */
11899       else
11900         break;
11901     }
11902
11903   /* For an abstract declarator, we might wind up with nothing at this
11904      point.  That's an error; the declarator is not optional.  */
11905   if (!declarator)
11906     cp_parser_error (parser, "expected declarator");
11907
11908   /* If we entered a scope, we must exit it now.  */
11909   if (pushed_scope)
11910     pop_scope (pushed_scope);
11911
11912   parser->default_arg_ok_p = saved_default_arg_ok_p;
11913   parser->in_declarator_p = saved_in_declarator_p;
11914
11915   return declarator;
11916 }
11917
11918 /* Parse a ptr-operator.
11919
11920    ptr-operator:
11921      * cv-qualifier-seq [opt]
11922      &
11923      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11924
11925    GNU Extension:
11926
11927    ptr-operator:
11928      & cv-qualifier-seq [opt]
11929
11930    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11931    Returns ADDR_EXPR if a reference was used.  In the case of a
11932    pointer-to-member, *TYPE is filled in with the TYPE containing the
11933    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11934    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11935    ERROR_MARK if an error occurred.  */
11936
11937 static enum tree_code
11938 cp_parser_ptr_operator (cp_parser* parser,
11939                         tree* type,
11940                         cp_cv_quals *cv_quals)
11941 {
11942   enum tree_code code = ERROR_MARK;
11943   cp_token *token;
11944
11945   /* Assume that it's not a pointer-to-member.  */
11946   *type = NULL_TREE;
11947   /* And that there are no cv-qualifiers.  */
11948   *cv_quals = TYPE_UNQUALIFIED;
11949
11950   /* Peek at the next token.  */
11951   token = cp_lexer_peek_token (parser->lexer);
11952   /* If it's a `*' or `&' we have a pointer or reference.  */
11953   if (token->type == CPP_MULT || token->type == CPP_AND)
11954     {
11955       /* Remember which ptr-operator we were processing.  */
11956       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11957
11958       /* Consume the `*' or `&'.  */
11959       cp_lexer_consume_token (parser->lexer);
11960
11961       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11962          `&', if we are allowing GNU extensions.  (The only qualifier
11963          that can legally appear after `&' is `restrict', but that is
11964          enforced during semantic analysis.  */
11965       if (code == INDIRECT_REF
11966           || cp_parser_allow_gnu_extensions_p (parser))
11967         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11968     }
11969   else
11970     {
11971       /* Try the pointer-to-member case.  */
11972       cp_parser_parse_tentatively (parser);
11973       /* Look for the optional `::' operator.  */
11974       cp_parser_global_scope_opt (parser,
11975                                   /*current_scope_valid_p=*/false);
11976       /* Look for the nested-name specifier.  */
11977       cp_parser_nested_name_specifier (parser,
11978                                        /*typename_keyword_p=*/false,
11979                                        /*check_dependency_p=*/true,
11980                                        /*type_p=*/false,
11981                                        /*is_declaration=*/false);
11982       /* If we found it, and the next token is a `*', then we are
11983          indeed looking at a pointer-to-member operator.  */
11984       if (!cp_parser_error_occurred (parser)
11985           && cp_parser_require (parser, CPP_MULT, "`*'"))
11986         {
11987           /* Indicate that the `*' operator was used.  */
11988           code = INDIRECT_REF;
11989
11990           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11991             error ("%qD is a namespace", parser->scope);
11992           else
11993             {
11994               /* The type of which the member is a member is given by the
11995                  current SCOPE.  */
11996               *type = parser->scope;
11997               /* The next name will not be qualified.  */
11998               parser->scope = NULL_TREE;
11999               parser->qualifying_scope = NULL_TREE;
12000               parser->object_scope = NULL_TREE;
12001               /* Look for the optional cv-qualifier-seq.  */
12002               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12003             }
12004         }
12005       /* If that didn't work we don't have a ptr-operator.  */
12006       if (!cp_parser_parse_definitely (parser))
12007         cp_parser_error (parser, "expected ptr-operator");
12008     }
12009
12010   return code;
12011 }
12012
12013 /* Parse an (optional) cv-qualifier-seq.
12014
12015    cv-qualifier-seq:
12016      cv-qualifier cv-qualifier-seq [opt]
12017
12018    cv-qualifier:
12019      const
12020      volatile
12021
12022    GNU Extension:
12023
12024    cv-qualifier:
12025      __restrict__
12026
12027    Returns a bitmask representing the cv-qualifiers.  */
12028
12029 static cp_cv_quals
12030 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12031 {
12032   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12033
12034   while (true)
12035     {
12036       cp_token *token;
12037       cp_cv_quals cv_qualifier;
12038
12039       /* Peek at the next token.  */
12040       token = cp_lexer_peek_token (parser->lexer);
12041       /* See if it's a cv-qualifier.  */
12042       switch (token->keyword)
12043         {
12044         case RID_CONST:
12045           cv_qualifier = TYPE_QUAL_CONST;
12046           break;
12047
12048         case RID_VOLATILE:
12049           cv_qualifier = TYPE_QUAL_VOLATILE;
12050           break;
12051
12052         case RID_RESTRICT:
12053           cv_qualifier = TYPE_QUAL_RESTRICT;
12054           break;
12055
12056         default:
12057           cv_qualifier = TYPE_UNQUALIFIED;
12058           break;
12059         }
12060
12061       if (!cv_qualifier)
12062         break;
12063
12064       if (cv_quals & cv_qualifier)
12065         {
12066           error ("duplicate cv-qualifier");
12067           cp_lexer_purge_token (parser->lexer);
12068         }
12069       else
12070         {
12071           cp_lexer_consume_token (parser->lexer);
12072           cv_quals |= cv_qualifier;
12073         }
12074     }
12075
12076   return cv_quals;
12077 }
12078
12079 /* Parse a declarator-id.
12080
12081    declarator-id:
12082      id-expression
12083      :: [opt] nested-name-specifier [opt] type-name
12084
12085    In the `id-expression' case, the value returned is as for
12086    cp_parser_id_expression if the id-expression was an unqualified-id.
12087    If the id-expression was a qualified-id, then a SCOPE_REF is
12088    returned.  The first operand is the scope (either a NAMESPACE_DECL
12089    or TREE_TYPE), but the second is still just a representation of an
12090    unqualified-id.  */
12091
12092 static tree
12093 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12094 {
12095   tree id;
12096   /* The expression must be an id-expression.  Assume that qualified
12097      names are the names of types so that:
12098
12099        template <class T>
12100        int S<T>::R::i = 3;
12101
12102      will work; we must treat `S<T>::R' as the name of a type.
12103      Similarly, assume that qualified names are templates, where
12104      required, so that:
12105
12106        template <class T>
12107        int S<T>::R<T>::i = 3;
12108
12109      will work, too.  */
12110   id = cp_parser_id_expression (parser,
12111                                 /*template_keyword_p=*/false,
12112                                 /*check_dependency_p=*/false,
12113                                 /*template_p=*/NULL,
12114                                 /*declarator_p=*/true,
12115                                 optional_p);
12116   if (id && BASELINK_P (id))
12117     id = BASELINK_FUNCTIONS (id);
12118   return id;
12119 }
12120
12121 /* Parse a type-id.
12122
12123    type-id:
12124      type-specifier-seq abstract-declarator [opt]
12125
12126    Returns the TYPE specified.  */
12127
12128 static tree
12129 cp_parser_type_id (cp_parser* parser)
12130 {
12131   cp_decl_specifier_seq type_specifier_seq;
12132   cp_declarator *abstract_declarator;
12133
12134   /* Parse the type-specifier-seq.  */
12135   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12136                                 &type_specifier_seq);
12137   if (type_specifier_seq.type == error_mark_node)
12138     return error_mark_node;
12139
12140   /* There might or might not be an abstract declarator.  */
12141   cp_parser_parse_tentatively (parser);
12142   /* Look for the declarator.  */
12143   abstract_declarator
12144     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12145                             /*parenthesized_p=*/NULL,
12146                             /*member_p=*/false);
12147   /* Check to see if there really was a declarator.  */
12148   if (!cp_parser_parse_definitely (parser))
12149     abstract_declarator = NULL;
12150
12151   return groktypename (&type_specifier_seq, abstract_declarator);
12152 }
12153
12154 /* Parse a type-specifier-seq.
12155
12156    type-specifier-seq:
12157      type-specifier type-specifier-seq [opt]
12158
12159    GNU extension:
12160
12161    type-specifier-seq:
12162      attributes type-specifier-seq [opt]
12163
12164    If IS_CONDITION is true, we are at the start of a "condition",
12165    e.g., we've just seen "if (".
12166
12167    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
12168
12169 static void
12170 cp_parser_type_specifier_seq (cp_parser* parser,
12171                               bool is_condition,
12172                               cp_decl_specifier_seq *type_specifier_seq)
12173 {
12174   bool seen_type_specifier = false;
12175   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12176
12177   /* Clear the TYPE_SPECIFIER_SEQ.  */
12178   clear_decl_specs (type_specifier_seq);
12179
12180   /* Parse the type-specifiers and attributes.  */
12181   while (true)
12182     {
12183       tree type_specifier;
12184       bool is_cv_qualifier;
12185
12186       /* Check for attributes first.  */
12187       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12188         {
12189           type_specifier_seq->attributes =
12190             chainon (type_specifier_seq->attributes,
12191                      cp_parser_attributes_opt (parser));
12192           continue;
12193         }
12194
12195       /* Look for the type-specifier.  */
12196       type_specifier = cp_parser_type_specifier (parser,
12197                                                  flags,
12198                                                  type_specifier_seq,
12199                                                  /*is_declaration=*/false,
12200                                                  NULL,
12201                                                  &is_cv_qualifier);
12202       if (!type_specifier)
12203         {
12204           /* If the first type-specifier could not be found, this is not a
12205              type-specifier-seq at all.  */
12206           if (!seen_type_specifier)
12207             {
12208               cp_parser_error (parser, "expected type-specifier");
12209               type_specifier_seq->type = error_mark_node;
12210               return;
12211             }
12212           /* If subsequent type-specifiers could not be found, the
12213              type-specifier-seq is complete.  */
12214           break;
12215         }
12216
12217       seen_type_specifier = true;
12218       /* The standard says that a condition can be:
12219
12220             type-specifier-seq declarator = assignment-expression
12221
12222          However, given:
12223
12224            struct S {};
12225            if (int S = ...)
12226
12227          we should treat the "S" as a declarator, not as a
12228          type-specifier.  The standard doesn't say that explicitly for
12229          type-specifier-seq, but it does say that for
12230          decl-specifier-seq in an ordinary declaration.  Perhaps it
12231          would be clearer just to allow a decl-specifier-seq here, and
12232          then add a semantic restriction that if any decl-specifiers
12233          that are not type-specifiers appear, the program is invalid.  */
12234       if (is_condition && !is_cv_qualifier)
12235         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12236     }
12237
12238   cp_parser_check_decl_spec (type_specifier_seq);
12239 }
12240
12241 /* Parse a parameter-declaration-clause.
12242
12243    parameter-declaration-clause:
12244      parameter-declaration-list [opt] ... [opt]
12245      parameter-declaration-list , ...
12246
12247    Returns a representation for the parameter declarations.  A return
12248    value of NULL indicates a parameter-declaration-clause consisting
12249    only of an ellipsis.  */
12250
12251 static cp_parameter_declarator *
12252 cp_parser_parameter_declaration_clause (cp_parser* parser)
12253 {
12254   cp_parameter_declarator *parameters;
12255   cp_token *token;
12256   bool ellipsis_p;
12257   bool is_error;
12258
12259   /* Peek at the next token.  */
12260   token = cp_lexer_peek_token (parser->lexer);
12261   /* Check for trivial parameter-declaration-clauses.  */
12262   if (token->type == CPP_ELLIPSIS)
12263     {
12264       /* Consume the `...' token.  */
12265       cp_lexer_consume_token (parser->lexer);
12266       return NULL;
12267     }
12268   else if (token->type == CPP_CLOSE_PAREN)
12269     /* There are no parameters.  */
12270     {
12271 #ifndef NO_IMPLICIT_EXTERN_C
12272       if (in_system_header && current_class_type == NULL
12273           && current_lang_name == lang_name_c)
12274         return NULL;
12275       else
12276 #endif
12277         return no_parameters;
12278     }
12279   /* Check for `(void)', too, which is a special case.  */
12280   else if (token->keyword == RID_VOID
12281            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12282                == CPP_CLOSE_PAREN))
12283     {
12284       /* Consume the `void' token.  */
12285       cp_lexer_consume_token (parser->lexer);
12286       /* There are no parameters.  */
12287       return no_parameters;
12288     }
12289
12290   /* Parse the parameter-declaration-list.  */
12291   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12292   /* If a parse error occurred while parsing the
12293      parameter-declaration-list, then the entire
12294      parameter-declaration-clause is erroneous.  */
12295   if (is_error)
12296     return NULL;
12297
12298   /* Peek at the next token.  */
12299   token = cp_lexer_peek_token (parser->lexer);
12300   /* If it's a `,', the clause should terminate with an ellipsis.  */
12301   if (token->type == CPP_COMMA)
12302     {
12303       /* Consume the `,'.  */
12304       cp_lexer_consume_token (parser->lexer);
12305       /* Expect an ellipsis.  */
12306       ellipsis_p
12307         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12308     }
12309   /* It might also be `...' if the optional trailing `,' was
12310      omitted.  */
12311   else if (token->type == CPP_ELLIPSIS)
12312     {
12313       /* Consume the `...' token.  */
12314       cp_lexer_consume_token (parser->lexer);
12315       /* And remember that we saw it.  */
12316       ellipsis_p = true;
12317     }
12318   else
12319     ellipsis_p = false;
12320
12321   /* Finish the parameter list.  */
12322   if (parameters && ellipsis_p)
12323     parameters->ellipsis_p = true;
12324
12325   return parameters;
12326 }
12327
12328 /* Parse a parameter-declaration-list.
12329
12330    parameter-declaration-list:
12331      parameter-declaration
12332      parameter-declaration-list , parameter-declaration
12333
12334    Returns a representation of the parameter-declaration-list, as for
12335    cp_parser_parameter_declaration_clause.  However, the
12336    `void_list_node' is never appended to the list.  Upon return,
12337    *IS_ERROR will be true iff an error occurred.  */
12338
12339 static cp_parameter_declarator *
12340 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12341 {
12342   cp_parameter_declarator *parameters = NULL;
12343   cp_parameter_declarator **tail = &parameters;
12344   bool saved_in_unbraced_linkage_specification_p;
12345
12346   /* Assume all will go well.  */
12347   *is_error = false;
12348   /* The special considerations that apply to a function within an
12349      unbraced linkage specifications do not apply to the parameters
12350      to the function.  */
12351   saved_in_unbraced_linkage_specification_p 
12352     = parser->in_unbraced_linkage_specification_p;
12353   parser->in_unbraced_linkage_specification_p = false;
12354
12355   /* Look for more parameters.  */
12356   while (true)
12357     {
12358       cp_parameter_declarator *parameter;
12359       bool parenthesized_p;
12360       /* Parse the parameter.  */
12361       parameter
12362         = cp_parser_parameter_declaration (parser,
12363                                            /*template_parm_p=*/false,
12364                                            &parenthesized_p);
12365
12366       /* If a parse error occurred parsing the parameter declaration,
12367          then the entire parameter-declaration-list is erroneous.  */
12368       if (!parameter)
12369         {
12370           *is_error = true;
12371           parameters = NULL;
12372           break;
12373         }
12374       /* Add the new parameter to the list.  */
12375       *tail = parameter;
12376       tail = &parameter->next;
12377
12378       /* Peek at the next token.  */
12379       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12380           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12381           /* These are for Objective-C++ */
12382           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12383           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12384         /* The parameter-declaration-list is complete.  */
12385         break;
12386       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12387         {
12388           cp_token *token;
12389
12390           /* Peek at the next token.  */
12391           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12392           /* If it's an ellipsis, then the list is complete.  */
12393           if (token->type == CPP_ELLIPSIS)
12394             break;
12395           /* Otherwise, there must be more parameters.  Consume the
12396              `,'.  */
12397           cp_lexer_consume_token (parser->lexer);
12398           /* When parsing something like:
12399
12400                 int i(float f, double d)
12401
12402              we can tell after seeing the declaration for "f" that we
12403              are not looking at an initialization of a variable "i",
12404              but rather at the declaration of a function "i".
12405
12406              Due to the fact that the parsing of template arguments
12407              (as specified to a template-id) requires backtracking we
12408              cannot use this technique when inside a template argument
12409              list.  */
12410           if (!parser->in_template_argument_list_p
12411               && !parser->in_type_id_in_expr_p
12412               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12413               /* However, a parameter-declaration of the form
12414                  "foat(f)" (which is a valid declaration of a
12415                  parameter "f") can also be interpreted as an
12416                  expression (the conversion of "f" to "float").  */
12417               && !parenthesized_p)
12418             cp_parser_commit_to_tentative_parse (parser);
12419         }
12420       else
12421         {
12422           cp_parser_error (parser, "expected %<,%> or %<...%>");
12423           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12424             cp_parser_skip_to_closing_parenthesis (parser,
12425                                                    /*recovering=*/true,
12426                                                    /*or_comma=*/false,
12427                                                    /*consume_paren=*/false);
12428           break;
12429         }
12430     }
12431
12432   parser->in_unbraced_linkage_specification_p
12433     = saved_in_unbraced_linkage_specification_p;
12434
12435   return parameters;
12436 }
12437
12438 /* Parse a parameter declaration.
12439
12440    parameter-declaration:
12441      decl-specifier-seq declarator
12442      decl-specifier-seq declarator = assignment-expression
12443      decl-specifier-seq abstract-declarator [opt]
12444      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12445
12446    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12447    declares a template parameter.  (In that case, a non-nested `>'
12448    token encountered during the parsing of the assignment-expression
12449    is not interpreted as a greater-than operator.)
12450
12451    Returns a representation of the parameter, or NULL if an error
12452    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12453    true iff the declarator is of the form "(p)".  */
12454
12455 static cp_parameter_declarator *
12456 cp_parser_parameter_declaration (cp_parser *parser,
12457                                  bool template_parm_p,
12458                                  bool *parenthesized_p)
12459 {
12460   int declares_class_or_enum;
12461   bool greater_than_is_operator_p;
12462   cp_decl_specifier_seq decl_specifiers;
12463   cp_declarator *declarator;
12464   tree default_argument;
12465   cp_token *token;
12466   const char *saved_message;
12467
12468   /* In a template parameter, `>' is not an operator.
12469
12470      [temp.param]
12471
12472      When parsing a default template-argument for a non-type
12473      template-parameter, the first non-nested `>' is taken as the end
12474      of the template parameter-list rather than a greater-than
12475      operator.  */
12476   greater_than_is_operator_p = !template_parm_p;
12477
12478   /* Type definitions may not appear in parameter types.  */
12479   saved_message = parser->type_definition_forbidden_message;
12480   parser->type_definition_forbidden_message
12481     = "types may not be defined in parameter types";
12482
12483   /* Parse the declaration-specifiers.  */
12484   cp_parser_decl_specifier_seq (parser,
12485                                 CP_PARSER_FLAGS_NONE,
12486                                 &decl_specifiers,
12487                                 &declares_class_or_enum);
12488   /* If an error occurred, there's no reason to attempt to parse the
12489      rest of the declaration.  */
12490   if (cp_parser_error_occurred (parser))
12491     {
12492       parser->type_definition_forbidden_message = saved_message;
12493       return NULL;
12494     }
12495
12496   /* Peek at the next token.  */
12497   token = cp_lexer_peek_token (parser->lexer);
12498   /* If the next token is a `)', `,', `=', `>', or `...', then there
12499      is no declarator.  */
12500   if (token->type == CPP_CLOSE_PAREN
12501       || token->type == CPP_COMMA
12502       || token->type == CPP_EQ
12503       || token->type == CPP_ELLIPSIS
12504       || token->type == CPP_GREATER)
12505     {
12506       declarator = NULL;
12507       if (parenthesized_p)
12508         *parenthesized_p = false;
12509     }
12510   /* Otherwise, there should be a declarator.  */
12511   else
12512     {
12513       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12514       parser->default_arg_ok_p = false;
12515
12516       /* After seeing a decl-specifier-seq, if the next token is not a
12517          "(", there is no possibility that the code is a valid
12518          expression.  Therefore, if parsing tentatively, we commit at
12519          this point.  */
12520       if (!parser->in_template_argument_list_p
12521           /* In an expression context, having seen:
12522
12523                (int((char ...
12524
12525              we cannot be sure whether we are looking at a
12526              function-type (taking a "char" as a parameter) or a cast
12527              of some object of type "char" to "int".  */
12528           && !parser->in_type_id_in_expr_p
12529           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12530           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12531         cp_parser_commit_to_tentative_parse (parser);
12532       /* Parse the declarator.  */
12533       declarator = cp_parser_declarator (parser,
12534                                          CP_PARSER_DECLARATOR_EITHER,
12535                                          /*ctor_dtor_or_conv_p=*/NULL,
12536                                          parenthesized_p,
12537                                          /*member_p=*/false);
12538       parser->default_arg_ok_p = saved_default_arg_ok_p;
12539       /* After the declarator, allow more attributes.  */
12540       decl_specifiers.attributes
12541         = chainon (decl_specifiers.attributes,
12542                    cp_parser_attributes_opt (parser));
12543     }
12544
12545   /* The restriction on defining new types applies only to the type
12546      of the parameter, not to the default argument.  */
12547   parser->type_definition_forbidden_message = saved_message;
12548
12549   /* If the next token is `=', then process a default argument.  */
12550   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12551     {
12552       bool saved_greater_than_is_operator_p;
12553       /* Consume the `='.  */
12554       cp_lexer_consume_token (parser->lexer);
12555
12556       /* If we are defining a class, then the tokens that make up the
12557          default argument must be saved and processed later.  */
12558       if (!template_parm_p && at_class_scope_p ()
12559           && TYPE_BEING_DEFINED (current_class_type))
12560         {
12561           unsigned depth = 0;
12562           cp_token *first_token;
12563           cp_token *token;
12564
12565           /* Add tokens until we have processed the entire default
12566              argument.  We add the range [first_token, token).  */
12567           first_token = cp_lexer_peek_token (parser->lexer);
12568           while (true)
12569             {
12570               bool done = false;
12571
12572               /* Peek at the next token.  */
12573               token = cp_lexer_peek_token (parser->lexer);
12574               /* What we do depends on what token we have.  */
12575               switch (token->type)
12576                 {
12577                   /* In valid code, a default argument must be
12578                      immediately followed by a `,' `)', or `...'.  */
12579                 case CPP_COMMA:
12580                 case CPP_CLOSE_PAREN:
12581                 case CPP_ELLIPSIS:
12582                   /* If we run into a non-nested `;', `}', or `]',
12583                      then the code is invalid -- but the default
12584                      argument is certainly over.  */
12585                 case CPP_SEMICOLON:
12586                 case CPP_CLOSE_BRACE:
12587                 case CPP_CLOSE_SQUARE:
12588                   if (depth == 0)
12589                     done = true;
12590                   /* Update DEPTH, if necessary.  */
12591                   else if (token->type == CPP_CLOSE_PAREN
12592                            || token->type == CPP_CLOSE_BRACE
12593                            || token->type == CPP_CLOSE_SQUARE)
12594                     --depth;
12595                   break;
12596
12597                 case CPP_OPEN_PAREN:
12598                 case CPP_OPEN_SQUARE:
12599                 case CPP_OPEN_BRACE:
12600                   ++depth;
12601                   break;
12602
12603                 case CPP_GREATER:
12604                   /* If we see a non-nested `>', and `>' is not an
12605                      operator, then it marks the end of the default
12606                      argument.  */
12607                   if (!depth && !greater_than_is_operator_p)
12608                     done = true;
12609                   break;
12610
12611                   /* If we run out of tokens, issue an error message.  */
12612                 case CPP_EOF:
12613                 case CPP_PRAGMA_EOL:
12614                   error ("file ends in default argument");
12615                   done = true;
12616                   break;
12617
12618                 case CPP_NAME:
12619                 case CPP_SCOPE:
12620                   /* In these cases, we should look for template-ids.
12621                      For example, if the default argument is
12622                      `X<int, double>()', we need to do name lookup to
12623                      figure out whether or not `X' is a template; if
12624                      so, the `,' does not end the default argument.
12625
12626                      That is not yet done.  */
12627                   break;
12628
12629                 default:
12630                   break;
12631                 }
12632
12633               /* If we've reached the end, stop.  */
12634               if (done)
12635                 break;
12636
12637               /* Add the token to the token block.  */
12638               token = cp_lexer_consume_token (parser->lexer);
12639             }
12640
12641           /* Create a DEFAULT_ARG to represented the unparsed default
12642              argument.  */
12643           default_argument = make_node (DEFAULT_ARG);
12644           DEFARG_TOKENS (default_argument)
12645             = cp_token_cache_new (first_token, token);
12646           DEFARG_INSTANTIATIONS (default_argument) = NULL;
12647         }
12648       /* Outside of a class definition, we can just parse the
12649          assignment-expression.  */
12650       else
12651         {
12652           bool saved_local_variables_forbidden_p;
12653
12654           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12655              set correctly.  */
12656           saved_greater_than_is_operator_p
12657             = parser->greater_than_is_operator_p;
12658           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12659           /* Local variable names (and the `this' keyword) may not
12660              appear in a default argument.  */
12661           saved_local_variables_forbidden_p
12662             = parser->local_variables_forbidden_p;
12663           parser->local_variables_forbidden_p = true;
12664           /* The default argument expression may cause implicitly
12665              defined member functions to be synthesized, which will
12666              result in garbage collection.  We must treat this
12667              situation as if we were within the body of function so as
12668              to avoid collecting live data on the stack.  */
12669           ++function_depth;
12670           /* Parse the assignment-expression.  */
12671           if (template_parm_p)
12672             push_deferring_access_checks (dk_no_deferred);
12673           default_argument
12674             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12675           if (template_parm_p)
12676             pop_deferring_access_checks ();
12677           /* Restore saved state.  */
12678           --function_depth;
12679           parser->greater_than_is_operator_p
12680             = saved_greater_than_is_operator_p;
12681           parser->local_variables_forbidden_p
12682             = saved_local_variables_forbidden_p;
12683         }
12684       if (!parser->default_arg_ok_p)
12685         {
12686           if (!flag_pedantic_errors)
12687             warning (0, "deprecated use of default argument for parameter of non-function");
12688           else
12689             {
12690               error ("default arguments are only permitted for function parameters");
12691               default_argument = NULL_TREE;
12692             }
12693         }
12694     }
12695   else
12696     default_argument = NULL_TREE;
12697
12698   return make_parameter_declarator (&decl_specifiers,
12699                                     declarator,
12700                                     default_argument);
12701 }
12702
12703 /* Parse a function-body.
12704
12705    function-body:
12706      compound_statement  */
12707
12708 static void
12709 cp_parser_function_body (cp_parser *parser)
12710 {
12711   cp_parser_compound_statement (parser, NULL, false);
12712 }
12713
12714 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12715    true if a ctor-initializer was present.  */
12716
12717 static bool
12718 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12719 {
12720   tree body;
12721   bool ctor_initializer_p;
12722
12723   /* Begin the function body.  */
12724   body = begin_function_body ();
12725   /* Parse the optional ctor-initializer.  */
12726   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12727   /* Parse the function-body.  */
12728   cp_parser_function_body (parser);
12729   /* Finish the function body.  */
12730   finish_function_body (body);
12731
12732   return ctor_initializer_p;
12733 }
12734
12735 /* Parse an initializer.
12736
12737    initializer:
12738      = initializer-clause
12739      ( expression-list )
12740
12741    Returns an expression representing the initializer.  If no
12742    initializer is present, NULL_TREE is returned.
12743
12744    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12745    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12746    set to FALSE if there is no initializer present.  If there is an
12747    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12748    is set to true; otherwise it is set to false.  */
12749
12750 static tree
12751 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12752                        bool* non_constant_p)
12753 {
12754   cp_token *token;
12755   tree init;
12756
12757   /* Peek at the next token.  */
12758   token = cp_lexer_peek_token (parser->lexer);
12759
12760   /* Let our caller know whether or not this initializer was
12761      parenthesized.  */
12762   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12763   /* Assume that the initializer is constant.  */
12764   *non_constant_p = false;
12765
12766   if (token->type == CPP_EQ)
12767     {
12768       /* Consume the `='.  */
12769       cp_lexer_consume_token (parser->lexer);
12770       /* Parse the initializer-clause.  */
12771       init = cp_parser_initializer_clause (parser, non_constant_p);
12772     }
12773   else if (token->type == CPP_OPEN_PAREN)
12774     init = cp_parser_parenthesized_expression_list (parser, false,
12775                                                     /*cast_p=*/false,
12776                                                     non_constant_p);
12777   else
12778     {
12779       /* Anything else is an error.  */
12780       cp_parser_error (parser, "expected initializer");
12781       init = error_mark_node;
12782     }
12783
12784   return init;
12785 }
12786
12787 /* Parse an initializer-clause.
12788
12789    initializer-clause:
12790      assignment-expression
12791      { initializer-list , [opt] }
12792      { }
12793
12794    Returns an expression representing the initializer.
12795
12796    If the `assignment-expression' production is used the value
12797    returned is simply a representation for the expression.
12798
12799    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12800    the elements of the initializer-list (or NULL, if the last
12801    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12802    NULL_TREE.  There is no way to detect whether or not the optional
12803    trailing `,' was provided.  NON_CONSTANT_P is as for
12804    cp_parser_initializer.  */
12805
12806 static tree
12807 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12808 {
12809   tree initializer;
12810
12811   /* Assume the expression is constant.  */
12812   *non_constant_p = false;
12813
12814   /* If it is not a `{', then we are looking at an
12815      assignment-expression.  */
12816   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12817     {
12818       initializer
12819         = cp_parser_constant_expression (parser,
12820                                         /*allow_non_constant_p=*/true,
12821                                         non_constant_p);
12822       if (!*non_constant_p)
12823         initializer = fold_non_dependent_expr (initializer);
12824     }
12825   else
12826     {
12827       /* Consume the `{' token.  */
12828       cp_lexer_consume_token (parser->lexer);
12829       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12830       initializer = make_node (CONSTRUCTOR);
12831       /* If it's not a `}', then there is a non-trivial initializer.  */
12832       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12833         {
12834           /* Parse the initializer list.  */
12835           CONSTRUCTOR_ELTS (initializer)
12836             = cp_parser_initializer_list (parser, non_constant_p);
12837           /* A trailing `,' token is allowed.  */
12838           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12839             cp_lexer_consume_token (parser->lexer);
12840         }
12841       /* Now, there should be a trailing `}'.  */
12842       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12843     }
12844
12845   return initializer;
12846 }
12847
12848 /* Parse an initializer-list.
12849
12850    initializer-list:
12851      initializer-clause
12852      initializer-list , initializer-clause
12853
12854    GNU Extension:
12855
12856    initializer-list:
12857      identifier : initializer-clause
12858      initializer-list, identifier : initializer-clause
12859
12860    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12861    for the initializer.  If the INDEX of the elt is non-NULL, it is the
12862    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12863    as for cp_parser_initializer.  */
12864
12865 static VEC(constructor_elt,gc) *
12866 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12867 {
12868   VEC(constructor_elt,gc) *v = NULL;
12869
12870   /* Assume all of the expressions are constant.  */
12871   *non_constant_p = false;
12872
12873   /* Parse the rest of the list.  */
12874   while (true)
12875     {
12876       cp_token *token;
12877       tree identifier;
12878       tree initializer;
12879       bool clause_non_constant_p;
12880
12881       /* If the next token is an identifier and the following one is a
12882          colon, we are looking at the GNU designated-initializer
12883          syntax.  */
12884       if (cp_parser_allow_gnu_extensions_p (parser)
12885           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12886           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12887         {
12888           /* Warn the user that they are using an extension.  */
12889           if (pedantic)
12890             pedwarn ("ISO C++ does not allow designated initializers");
12891           /* Consume the identifier.  */
12892           identifier = cp_lexer_consume_token (parser->lexer)->value;
12893           /* Consume the `:'.  */
12894           cp_lexer_consume_token (parser->lexer);
12895         }
12896       else
12897         identifier = NULL_TREE;
12898
12899       /* Parse the initializer.  */
12900       initializer = cp_parser_initializer_clause (parser,
12901                                                   &clause_non_constant_p);
12902       /* If any clause is non-constant, so is the entire initializer.  */
12903       if (clause_non_constant_p)
12904         *non_constant_p = true;
12905
12906       /* Add it to the vector.  */
12907       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12908
12909       /* If the next token is not a comma, we have reached the end of
12910          the list.  */
12911       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12912         break;
12913
12914       /* Peek at the next token.  */
12915       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12916       /* If the next token is a `}', then we're still done.  An
12917          initializer-clause can have a trailing `,' after the
12918          initializer-list and before the closing `}'.  */
12919       if (token->type == CPP_CLOSE_BRACE)
12920         break;
12921
12922       /* Consume the `,' token.  */
12923       cp_lexer_consume_token (parser->lexer);
12924     }
12925
12926   return v;
12927 }
12928
12929 /* Classes [gram.class] */
12930
12931 /* Parse a class-name.
12932
12933    class-name:
12934      identifier
12935      template-id
12936
12937    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12938    to indicate that names looked up in dependent types should be
12939    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12940    keyword has been used to indicate that the name that appears next
12941    is a template.  TAG_TYPE indicates the explicit tag given before
12942    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12943    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12944    is the class being defined in a class-head.
12945
12946    Returns the TYPE_DECL representing the class.  */
12947
12948 static tree
12949 cp_parser_class_name (cp_parser *parser,
12950                       bool typename_keyword_p,
12951                       bool template_keyword_p,
12952                       enum tag_types tag_type,
12953                       bool check_dependency_p,
12954                       bool class_head_p,
12955                       bool is_declaration)
12956 {
12957   tree decl;
12958   tree scope;
12959   bool typename_p;
12960   cp_token *token;
12961
12962   /* All class-names start with an identifier.  */
12963   token = cp_lexer_peek_token (parser->lexer);
12964   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12965     {
12966       cp_parser_error (parser, "expected class-name");
12967       return error_mark_node;
12968     }
12969
12970   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12971      to a template-id, so we save it here.  */
12972   scope = parser->scope;
12973   if (scope == error_mark_node)
12974     return error_mark_node;
12975
12976   /* Any name names a type if we're following the `typename' keyword
12977      in a qualified name where the enclosing scope is type-dependent.  */
12978   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12979                 && dependent_type_p (scope));
12980   /* Handle the common case (an identifier, but not a template-id)
12981      efficiently.  */
12982   if (token->type == CPP_NAME
12983       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12984     {
12985       cp_token *identifier_token;
12986       tree identifier;
12987       bool ambiguous_p;
12988
12989       /* Look for the identifier.  */
12990       identifier_token = cp_lexer_peek_token (parser->lexer);
12991       ambiguous_p = identifier_token->ambiguous_p;
12992       identifier = cp_parser_identifier (parser);
12993       /* If the next token isn't an identifier, we are certainly not
12994          looking at a class-name.  */
12995       if (identifier == error_mark_node)
12996         decl = error_mark_node;
12997       /* If we know this is a type-name, there's no need to look it
12998          up.  */
12999       else if (typename_p)
13000         decl = identifier;
13001       else
13002         {
13003           tree ambiguous_decls;
13004           /* If we already know that this lookup is ambiguous, then
13005              we've already issued an error message; there's no reason
13006              to check again.  */
13007           if (ambiguous_p)
13008             {
13009               cp_parser_simulate_error (parser);
13010               return error_mark_node;
13011             }
13012           /* If the next token is a `::', then the name must be a type
13013              name.
13014
13015              [basic.lookup.qual]
13016
13017              During the lookup for a name preceding the :: scope
13018              resolution operator, object, function, and enumerator
13019              names are ignored.  */
13020           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13021             tag_type = typename_type;
13022           /* Look up the name.  */
13023           decl = cp_parser_lookup_name (parser, identifier,
13024                                         tag_type,
13025                                         /*is_template=*/false,
13026                                         /*is_namespace=*/false,
13027                                         check_dependency_p,
13028                                         &ambiguous_decls);
13029           if (ambiguous_decls)
13030             {
13031               error ("reference to %qD is ambiguous", identifier);
13032               print_candidates (ambiguous_decls);
13033               if (cp_parser_parsing_tentatively (parser))
13034                 {
13035                   identifier_token->ambiguous_p = true;
13036                   cp_parser_simulate_error (parser);
13037                 }
13038               return error_mark_node;
13039             }
13040         }
13041     }
13042   else
13043     {
13044       /* Try a template-id.  */
13045       decl = cp_parser_template_id (parser, template_keyword_p,
13046                                     check_dependency_p,
13047                                     is_declaration);
13048       if (decl == error_mark_node)
13049         return error_mark_node;
13050     }
13051
13052   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13053
13054   /* If this is a typename, create a TYPENAME_TYPE.  */
13055   if (typename_p && decl != error_mark_node)
13056     {
13057       decl = make_typename_type (scope, decl, typename_type,
13058                                  /*complain=*/tf_error);
13059       if (decl != error_mark_node)
13060         decl = TYPE_NAME (decl);
13061     }
13062
13063   /* Check to see that it is really the name of a class.  */
13064   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13065       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13066       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13067     /* Situations like this:
13068
13069          template <typename T> struct A {
13070            typename T::template X<int>::I i;
13071          };
13072
13073        are problematic.  Is `T::template X<int>' a class-name?  The
13074        standard does not seem to be definitive, but there is no other
13075        valid interpretation of the following `::'.  Therefore, those
13076        names are considered class-names.  */
13077     {
13078       decl = make_typename_type (scope, decl, tag_type, tf_error);
13079       if (decl != error_mark_node)
13080         decl = TYPE_NAME (decl);
13081     }
13082   else if (TREE_CODE (decl) != TYPE_DECL
13083            || TREE_TYPE (decl) == error_mark_node
13084            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13085     decl = error_mark_node;
13086
13087   if (decl == error_mark_node)
13088     cp_parser_error (parser, "expected class-name");
13089
13090   return decl;
13091 }
13092
13093 /* Parse a class-specifier.
13094
13095    class-specifier:
13096      class-head { member-specification [opt] }
13097
13098    Returns the TREE_TYPE representing the class.  */
13099
13100 static tree
13101 cp_parser_class_specifier (cp_parser* parser)
13102 {
13103   cp_token *token;
13104   tree type;
13105   tree attributes = NULL_TREE;
13106   int has_trailing_semicolon;
13107   bool nested_name_specifier_p;
13108   unsigned saved_num_template_parameter_lists;
13109   bool saved_in_function_body;
13110   tree old_scope = NULL_TREE;
13111   tree scope = NULL_TREE;
13112   tree bases;
13113
13114   push_deferring_access_checks (dk_no_deferred);
13115
13116   /* Parse the class-head.  */
13117   type = cp_parser_class_head (parser,
13118                                &nested_name_specifier_p,
13119                                &attributes,
13120                                &bases);
13121   /* If the class-head was a semantic disaster, skip the entire body
13122      of the class.  */
13123   if (!type)
13124     {
13125       cp_parser_skip_to_end_of_block_or_statement (parser);
13126       pop_deferring_access_checks ();
13127       return error_mark_node;
13128     }
13129
13130   /* Look for the `{'.  */
13131   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13132     {
13133       pop_deferring_access_checks ();
13134       return error_mark_node;
13135     }
13136
13137   /* Process the base classes. If they're invalid, skip the 
13138      entire class body.  */
13139   if (!xref_basetypes (type, bases))
13140     {
13141       cp_parser_skip_to_closing_brace (parser);
13142
13143       /* Consuming the closing brace yields better error messages
13144          later on.  */
13145       cp_lexer_consume_token (parser->lexer);
13146       pop_deferring_access_checks ();
13147       return error_mark_node;
13148     }
13149
13150   /* Issue an error message if type-definitions are forbidden here.  */
13151   cp_parser_check_type_definition (parser);
13152   /* Remember that we are defining one more class.  */
13153   ++parser->num_classes_being_defined;
13154   /* Inside the class, surrounding template-parameter-lists do not
13155      apply.  */
13156   saved_num_template_parameter_lists
13157     = parser->num_template_parameter_lists;
13158   parser->num_template_parameter_lists = 0;
13159   /* We are not in a function body.  */
13160   saved_in_function_body = parser->in_function_body;
13161   parser->in_function_body = false;
13162
13163   /* Start the class.  */
13164   if (nested_name_specifier_p)
13165     {
13166       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13167       old_scope = push_inner_scope (scope);
13168     }
13169   type = begin_class_definition (type, attributes);
13170
13171   if (type == error_mark_node)
13172     /* If the type is erroneous, skip the entire body of the class.  */
13173     cp_parser_skip_to_closing_brace (parser);
13174   else
13175     /* Parse the member-specification.  */
13176     cp_parser_member_specification_opt (parser);
13177
13178   /* Look for the trailing `}'.  */
13179   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13180   /* We get better error messages by noticing a common problem: a
13181      missing trailing `;'.  */
13182   token = cp_lexer_peek_token (parser->lexer);
13183   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13184   /* Look for trailing attributes to apply to this class.  */
13185   if (cp_parser_allow_gnu_extensions_p (parser))
13186     attributes = cp_parser_attributes_opt (parser);
13187   if (type != error_mark_node)
13188     type = finish_struct (type, attributes);
13189   if (nested_name_specifier_p)
13190     pop_inner_scope (old_scope, scope);
13191   /* If this class is not itself within the scope of another class,
13192      then we need to parse the bodies of all of the queued function
13193      definitions.  Note that the queued functions defined in a class
13194      are not always processed immediately following the
13195      class-specifier for that class.  Consider:
13196
13197        struct A {
13198          struct B { void f() { sizeof (A); } };
13199        };
13200
13201      If `f' were processed before the processing of `A' were
13202      completed, there would be no way to compute the size of `A'.
13203      Note that the nesting we are interested in here is lexical --
13204      not the semantic nesting given by TYPE_CONTEXT.  In particular,
13205      for:
13206
13207        struct A { struct B; };
13208        struct A::B { void f() { } };
13209
13210      there is no need to delay the parsing of `A::B::f'.  */
13211   if (--parser->num_classes_being_defined == 0)
13212     {
13213       tree queue_entry;
13214       tree fn;
13215       tree class_type = NULL_TREE;
13216       tree pushed_scope = NULL_TREE;
13217
13218       /* In a first pass, parse default arguments to the functions.
13219          Then, in a second pass, parse the bodies of the functions.
13220          This two-phased approach handles cases like:
13221
13222             struct S {
13223               void f() { g(); }
13224               void g(int i = 3);
13225             };
13226
13227          */
13228       for (TREE_PURPOSE (parser->unparsed_functions_queues)
13229              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13230            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13231            TREE_PURPOSE (parser->unparsed_functions_queues)
13232              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13233         {
13234           fn = TREE_VALUE (queue_entry);
13235           /* If there are default arguments that have not yet been processed,
13236              take care of them now.  */
13237           if (class_type != TREE_PURPOSE (queue_entry))
13238             {
13239               if (pushed_scope)
13240                 pop_scope (pushed_scope);
13241               class_type = TREE_PURPOSE (queue_entry);
13242               pushed_scope = push_scope (class_type);
13243             }
13244           /* Make sure that any template parameters are in scope.  */
13245           maybe_begin_member_template_processing (fn);
13246           /* Parse the default argument expressions.  */
13247           cp_parser_late_parsing_default_args (parser, fn);
13248           /* Remove any template parameters from the symbol table.  */
13249           maybe_end_member_template_processing ();
13250         }
13251       if (pushed_scope)
13252         pop_scope (pushed_scope);
13253       /* Now parse the body of the functions.  */
13254       for (TREE_VALUE (parser->unparsed_functions_queues)
13255              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13256            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13257            TREE_VALUE (parser->unparsed_functions_queues)
13258              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13259         {
13260           /* Figure out which function we need to process.  */
13261           fn = TREE_VALUE (queue_entry);
13262           /* Parse the function.  */
13263           cp_parser_late_parsing_for_member (parser, fn);
13264         }
13265     }
13266
13267   /* Put back any saved access checks.  */
13268   pop_deferring_access_checks ();
13269
13270   /* Restore saved state.  */
13271   parser->in_function_body = saved_in_function_body;
13272   parser->num_template_parameter_lists
13273     = saved_num_template_parameter_lists;
13274
13275   return type;
13276 }
13277
13278 /* Parse a class-head.
13279
13280    class-head:
13281      class-key identifier [opt] base-clause [opt]
13282      class-key nested-name-specifier identifier base-clause [opt]
13283      class-key nested-name-specifier [opt] template-id
13284        base-clause [opt]
13285
13286    GNU Extensions:
13287      class-key attributes identifier [opt] base-clause [opt]
13288      class-key attributes nested-name-specifier identifier base-clause [opt]
13289      class-key attributes nested-name-specifier [opt] template-id
13290        base-clause [opt]
13291
13292    Returns the TYPE of the indicated class.  Sets
13293    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13294    involving a nested-name-specifier was used, and FALSE otherwise.
13295
13296    Returns error_mark_node if this is not a class-head.
13297
13298    Returns NULL_TREE if the class-head is syntactically valid, but
13299    semantically invalid in a way that means we should skip the entire
13300    body of the class.  */
13301
13302 static tree
13303 cp_parser_class_head (cp_parser* parser,
13304                       bool* nested_name_specifier_p,
13305                       tree *attributes_p,
13306                       tree *bases)
13307 {
13308   tree nested_name_specifier;
13309   enum tag_types class_key;
13310   tree id = NULL_TREE;
13311   tree type = NULL_TREE;
13312   tree attributes;
13313   bool template_id_p = false;
13314   bool qualified_p = false;
13315   bool invalid_nested_name_p = false;
13316   bool invalid_explicit_specialization_p = false;
13317   tree pushed_scope = NULL_TREE;
13318   unsigned num_templates;
13319
13320   /* Assume no nested-name-specifier will be present.  */
13321   *nested_name_specifier_p = false;
13322   /* Assume no template parameter lists will be used in defining the
13323      type.  */
13324   num_templates = 0;
13325
13326   /* Look for the class-key.  */
13327   class_key = cp_parser_class_key (parser);
13328   if (class_key == none_type)
13329     return error_mark_node;
13330
13331   /* Parse the attributes.  */
13332   attributes = cp_parser_attributes_opt (parser);
13333
13334   /* If the next token is `::', that is invalid -- but sometimes
13335      people do try to write:
13336
13337        struct ::S {};
13338
13339      Handle this gracefully by accepting the extra qualifier, and then
13340      issuing an error about it later if this really is a
13341      class-head.  If it turns out just to be an elaborated type
13342      specifier, remain silent.  */
13343   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13344     qualified_p = true;
13345
13346   push_deferring_access_checks (dk_no_check);
13347
13348   /* Determine the name of the class.  Begin by looking for an
13349      optional nested-name-specifier.  */
13350   nested_name_specifier
13351     = cp_parser_nested_name_specifier_opt (parser,
13352                                            /*typename_keyword_p=*/false,
13353                                            /*check_dependency_p=*/false,
13354                                            /*type_p=*/false,
13355                                            /*is_declaration=*/false);
13356   /* If there was a nested-name-specifier, then there *must* be an
13357      identifier.  */
13358   if (nested_name_specifier)
13359     {
13360       /* Although the grammar says `identifier', it really means
13361          `class-name' or `template-name'.  You are only allowed to
13362          define a class that has already been declared with this
13363          syntax.
13364
13365          The proposed resolution for Core Issue 180 says that wherever
13366          you see `class T::X' you should treat `X' as a type-name.
13367
13368          It is OK to define an inaccessible class; for example:
13369
13370            class A { class B; };
13371            class A::B {};
13372
13373          We do not know if we will see a class-name, or a
13374          template-name.  We look for a class-name first, in case the
13375          class-name is a template-id; if we looked for the
13376          template-name first we would stop after the template-name.  */
13377       cp_parser_parse_tentatively (parser);
13378       type = cp_parser_class_name (parser,
13379                                    /*typename_keyword_p=*/false,
13380                                    /*template_keyword_p=*/false,
13381                                    class_type,
13382                                    /*check_dependency_p=*/false,
13383                                    /*class_head_p=*/true,
13384                                    /*is_declaration=*/false);
13385       /* If that didn't work, ignore the nested-name-specifier.  */
13386       if (!cp_parser_parse_definitely (parser))
13387         {
13388           invalid_nested_name_p = true;
13389           id = cp_parser_identifier (parser);
13390           if (id == error_mark_node)
13391             id = NULL_TREE;
13392         }
13393       /* If we could not find a corresponding TYPE, treat this
13394          declaration like an unqualified declaration.  */
13395       if (type == error_mark_node)
13396         nested_name_specifier = NULL_TREE;
13397       /* Otherwise, count the number of templates used in TYPE and its
13398          containing scopes.  */
13399       else
13400         {
13401           tree scope;
13402
13403           for (scope = TREE_TYPE (type);
13404                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13405                scope = (TYPE_P (scope)
13406                         ? TYPE_CONTEXT (scope)
13407                         : DECL_CONTEXT (scope)))
13408             if (TYPE_P (scope)
13409                 && CLASS_TYPE_P (scope)
13410                 && CLASSTYPE_TEMPLATE_INFO (scope)
13411                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13412                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13413               ++num_templates;
13414         }
13415     }
13416   /* Otherwise, the identifier is optional.  */
13417   else
13418     {
13419       /* We don't know whether what comes next is a template-id,
13420          an identifier, or nothing at all.  */
13421       cp_parser_parse_tentatively (parser);
13422       /* Check for a template-id.  */
13423       id = cp_parser_template_id (parser,
13424                                   /*template_keyword_p=*/false,
13425                                   /*check_dependency_p=*/true,
13426                                   /*is_declaration=*/true);
13427       /* If that didn't work, it could still be an identifier.  */
13428       if (!cp_parser_parse_definitely (parser))
13429         {
13430           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13431             id = cp_parser_identifier (parser);
13432           else
13433             id = NULL_TREE;
13434         }
13435       else
13436         {
13437           template_id_p = true;
13438           ++num_templates;
13439         }
13440     }
13441
13442   pop_deferring_access_checks ();
13443
13444   if (id)
13445     cp_parser_check_for_invalid_template_id (parser, id);
13446
13447   /* If it's not a `:' or a `{' then we can't really be looking at a
13448      class-head, since a class-head only appears as part of a
13449      class-specifier.  We have to detect this situation before calling
13450      xref_tag, since that has irreversible side-effects.  */
13451   if (!cp_parser_next_token_starts_class_definition_p (parser))
13452     {
13453       cp_parser_error (parser, "expected %<{%> or %<:%>");
13454       return error_mark_node;
13455     }
13456
13457   /* At this point, we're going ahead with the class-specifier, even
13458      if some other problem occurs.  */
13459   cp_parser_commit_to_tentative_parse (parser);
13460   /* Issue the error about the overly-qualified name now.  */
13461   if (qualified_p)
13462     cp_parser_error (parser,
13463                      "global qualification of class name is invalid");
13464   else if (invalid_nested_name_p)
13465     cp_parser_error (parser,
13466                      "qualified name does not name a class");
13467   else if (nested_name_specifier)
13468     {
13469       tree scope;
13470
13471       /* Reject typedef-names in class heads.  */
13472       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13473         {
13474           error ("invalid class name in declaration of %qD", type);
13475           type = NULL_TREE;
13476           goto done;
13477         }
13478
13479       /* Figure out in what scope the declaration is being placed.  */
13480       scope = current_scope ();
13481       /* If that scope does not contain the scope in which the
13482          class was originally declared, the program is invalid.  */
13483       if (scope && !is_ancestor (scope, nested_name_specifier))
13484         {
13485           error ("declaration of %qD in %qD which does not enclose %qD",
13486                  type, scope, nested_name_specifier);
13487           type = NULL_TREE;
13488           goto done;
13489         }
13490       /* [dcl.meaning]
13491
13492          A declarator-id shall not be qualified exception of the
13493          definition of a ... nested class outside of its class
13494          ... [or] a the definition or explicit instantiation of a
13495          class member of a namespace outside of its namespace.  */
13496       if (scope == nested_name_specifier)
13497         {
13498           pedwarn ("extra qualification ignored");
13499           nested_name_specifier = NULL_TREE;
13500           num_templates = 0;
13501         }
13502     }
13503   /* An explicit-specialization must be preceded by "template <>".  If
13504      it is not, try to recover gracefully.  */
13505   if (at_namespace_scope_p ()
13506       && parser->num_template_parameter_lists == 0
13507       && template_id_p)
13508     {
13509       error ("an explicit specialization must be preceded by %<template <>%>");
13510       invalid_explicit_specialization_p = true;
13511       /* Take the same action that would have been taken by
13512          cp_parser_explicit_specialization.  */
13513       ++parser->num_template_parameter_lists;
13514       begin_specialization ();
13515     }
13516   /* There must be no "return" statements between this point and the
13517      end of this function; set "type "to the correct return value and
13518      use "goto done;" to return.  */
13519   /* Make sure that the right number of template parameters were
13520      present.  */
13521   if (!cp_parser_check_template_parameters (parser, num_templates))
13522     {
13523       /* If something went wrong, there is no point in even trying to
13524          process the class-definition.  */
13525       type = NULL_TREE;
13526       goto done;
13527     }
13528
13529   /* Look up the type.  */
13530   if (template_id_p)
13531     {
13532       type = TREE_TYPE (id);
13533       type = maybe_process_partial_specialization (type);
13534       if (nested_name_specifier)
13535         pushed_scope = push_scope (nested_name_specifier);
13536     }
13537   else if (nested_name_specifier)
13538     {
13539       tree class_type;
13540
13541       /* Given:
13542
13543             template <typename T> struct S { struct T };
13544             template <typename T> struct S<T>::T { };
13545
13546          we will get a TYPENAME_TYPE when processing the definition of
13547          `S::T'.  We need to resolve it to the actual type before we
13548          try to define it.  */
13549       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13550         {
13551           class_type = resolve_typename_type (TREE_TYPE (type),
13552                                               /*only_current_p=*/false);
13553           if (class_type != error_mark_node)
13554             type = TYPE_NAME (class_type);
13555           else
13556             {
13557               cp_parser_error (parser, "could not resolve typename type");
13558               type = error_mark_node;
13559             }
13560         }
13561
13562       maybe_process_partial_specialization (TREE_TYPE (type));
13563       class_type = current_class_type;
13564       /* Enter the scope indicated by the nested-name-specifier.  */
13565       pushed_scope = push_scope (nested_name_specifier);
13566       /* Get the canonical version of this type.  */
13567       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13568       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13569           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13570         {
13571           type = push_template_decl (type);
13572           if (type == error_mark_node)
13573             {
13574               type = NULL_TREE;
13575               goto done;
13576             }
13577         }
13578
13579       type = TREE_TYPE (type);
13580       *nested_name_specifier_p = true;
13581     }
13582   else      /* The name is not a nested name.  */
13583     {
13584       /* If the class was unnamed, create a dummy name.  */
13585       if (!id)
13586         id = make_anon_name ();
13587       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13588                        parser->num_template_parameter_lists);
13589     }
13590
13591   /* Indicate whether this class was declared as a `class' or as a
13592      `struct'.  */
13593   if (TREE_CODE (type) == RECORD_TYPE)
13594     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13595   cp_parser_check_class_key (class_key, type);
13596
13597   /* If this type was already complete, and we see another definition,
13598      that's an error.  */
13599   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13600     {
13601       error ("redefinition of %q#T", type);
13602       error ("previous definition of %q+#T", type);
13603       type = NULL_TREE;
13604       goto done;
13605     }
13606   else if (type == error_mark_node)
13607     type = NULL_TREE;
13608
13609   /* We will have entered the scope containing the class; the names of
13610      base classes should be looked up in that context.  For example:
13611
13612        struct A { struct B {}; struct C; };
13613        struct A::C : B {};
13614
13615      is valid.  */
13616   *bases = NULL_TREE;
13617
13618   /* Get the list of base-classes, if there is one.  */
13619   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13620     *bases = cp_parser_base_clause (parser);
13621
13622  done:
13623   /* Leave the scope given by the nested-name-specifier.  We will
13624      enter the class scope itself while processing the members.  */
13625   if (pushed_scope)
13626     pop_scope (pushed_scope);
13627
13628   if (invalid_explicit_specialization_p)
13629     {
13630       end_specialization ();
13631       --parser->num_template_parameter_lists;
13632     }
13633   *attributes_p = attributes;
13634   return type;
13635 }
13636
13637 /* Parse a class-key.
13638
13639    class-key:
13640      class
13641      struct
13642      union
13643
13644    Returns the kind of class-key specified, or none_type to indicate
13645    error.  */
13646
13647 static enum tag_types
13648 cp_parser_class_key (cp_parser* parser)
13649 {
13650   cp_token *token;
13651   enum tag_types tag_type;
13652
13653   /* Look for the class-key.  */
13654   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13655   if (!token)
13656     return none_type;
13657
13658   /* Check to see if the TOKEN is a class-key.  */
13659   tag_type = cp_parser_token_is_class_key (token);
13660   if (!tag_type)
13661     cp_parser_error (parser, "expected class-key");
13662   return tag_type;
13663 }
13664
13665 /* Parse an (optional) member-specification.
13666
13667    member-specification:
13668      member-declaration member-specification [opt]
13669      access-specifier : member-specification [opt]  */
13670
13671 static void
13672 cp_parser_member_specification_opt (cp_parser* parser)
13673 {
13674   while (true)
13675     {
13676       cp_token *token;
13677       enum rid keyword;
13678
13679       /* Peek at the next token.  */
13680       token = cp_lexer_peek_token (parser->lexer);
13681       /* If it's a `}', or EOF then we've seen all the members.  */
13682       if (token->type == CPP_CLOSE_BRACE
13683           || token->type == CPP_EOF
13684           || token->type == CPP_PRAGMA_EOL)
13685         break;
13686
13687       /* See if this token is a keyword.  */
13688       keyword = token->keyword;
13689       switch (keyword)
13690         {
13691         case RID_PUBLIC:
13692         case RID_PROTECTED:
13693         case RID_PRIVATE:
13694           /* Consume the access-specifier.  */
13695           cp_lexer_consume_token (parser->lexer);
13696           /* Remember which access-specifier is active.  */
13697           current_access_specifier = token->value;
13698           /* Look for the `:'.  */
13699           cp_parser_require (parser, CPP_COLON, "`:'");
13700           break;
13701
13702         default:
13703           /* Accept #pragmas at class scope.  */
13704           if (token->type == CPP_PRAGMA)
13705             {
13706               cp_parser_pragma (parser, pragma_external);
13707               break;
13708             }
13709
13710           /* Otherwise, the next construction must be a
13711              member-declaration.  */
13712           cp_parser_member_declaration (parser);
13713         }
13714     }
13715 }
13716
13717 /* Parse a member-declaration.
13718
13719    member-declaration:
13720      decl-specifier-seq [opt] member-declarator-list [opt] ;
13721      function-definition ; [opt]
13722      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13723      using-declaration
13724      template-declaration
13725
13726    member-declarator-list:
13727      member-declarator
13728      member-declarator-list , member-declarator
13729
13730    member-declarator:
13731      declarator pure-specifier [opt]
13732      declarator constant-initializer [opt]
13733      identifier [opt] : constant-expression
13734
13735    GNU Extensions:
13736
13737    member-declaration:
13738      __extension__ member-declaration
13739
13740    member-declarator:
13741      declarator attributes [opt] pure-specifier [opt]
13742      declarator attributes [opt] constant-initializer [opt]
13743      identifier [opt] attributes [opt] : constant-expression  
13744
13745    C++0x Extensions:
13746
13747    member-declaration:
13748      static_assert-declaration  */
13749
13750 static void
13751 cp_parser_member_declaration (cp_parser* parser)
13752 {
13753   cp_decl_specifier_seq decl_specifiers;
13754   tree prefix_attributes;
13755   tree decl;
13756   int declares_class_or_enum;
13757   bool friend_p;
13758   cp_token *token;
13759   int saved_pedantic;
13760
13761   /* Check for the `__extension__' keyword.  */
13762   if (cp_parser_extension_opt (parser, &saved_pedantic))
13763     {
13764       /* Recurse.  */
13765       cp_parser_member_declaration (parser);
13766       /* Restore the old value of the PEDANTIC flag.  */
13767       pedantic = saved_pedantic;
13768
13769       return;
13770     }
13771
13772   /* Check for a template-declaration.  */
13773   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13774     {
13775       /* An explicit specialization here is an error condition, and we
13776          expect the specialization handler to detect and report this.  */
13777       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13778           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13779         cp_parser_explicit_specialization (parser);
13780       else
13781         cp_parser_template_declaration (parser, /*member_p=*/true);
13782
13783       return;
13784     }
13785
13786   /* Check for a using-declaration.  */
13787   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13788     {
13789       /* Parse the using-declaration.  */
13790       cp_parser_using_declaration (parser,
13791                                    /*access_declaration_p=*/false);
13792       return;
13793     }
13794
13795   /* Check for @defs.  */
13796   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13797     {
13798       tree ivar, member;
13799       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13800       ivar = ivar_chains;
13801       while (ivar)
13802         {
13803           member = ivar;
13804           ivar = TREE_CHAIN (member);
13805           TREE_CHAIN (member) = NULL_TREE;
13806           finish_member_declaration (member);
13807         }
13808       return;
13809     }
13810
13811   /* If the next token is `static_assert' we have a static assertion.  */
13812   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
13813     {
13814       cp_parser_static_assert (parser, /*member_p=*/true);
13815       return;
13816     }
13817
13818   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13819     return;
13820
13821   /* Parse the decl-specifier-seq.  */
13822   cp_parser_decl_specifier_seq (parser,
13823                                 CP_PARSER_FLAGS_OPTIONAL,
13824                                 &decl_specifiers,
13825                                 &declares_class_or_enum);
13826   prefix_attributes = decl_specifiers.attributes;
13827   decl_specifiers.attributes = NULL_TREE;
13828   /* Check for an invalid type-name.  */
13829   if (!decl_specifiers.type
13830       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13831     return;
13832   /* If there is no declarator, then the decl-specifier-seq should
13833      specify a type.  */
13834   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13835     {
13836       /* If there was no decl-specifier-seq, and the next token is a
13837          `;', then we have something like:
13838
13839            struct S { ; };
13840
13841          [class.mem]
13842
13843          Each member-declaration shall declare at least one member
13844          name of the class.  */
13845       if (!decl_specifiers.any_specifiers_p)
13846         {
13847           cp_token *token = cp_lexer_peek_token (parser->lexer);
13848           if (pedantic && !token->in_system_header)
13849             pedwarn ("%Hextra %<;%>", &token->location);
13850         }
13851       else
13852         {
13853           tree type;
13854
13855           /* See if this declaration is a friend.  */
13856           friend_p = cp_parser_friend_p (&decl_specifiers);
13857           /* If there were decl-specifiers, check to see if there was
13858              a class-declaration.  */
13859           type = check_tag_decl (&decl_specifiers);
13860           /* Nested classes have already been added to the class, but
13861              a `friend' needs to be explicitly registered.  */
13862           if (friend_p)
13863             {
13864               /* If the `friend' keyword was present, the friend must
13865                  be introduced with a class-key.  */
13866                if (!declares_class_or_enum)
13867                  error ("a class-key must be used when declaring a friend");
13868                /* In this case:
13869
13870                     template <typename T> struct A {
13871                       friend struct A<T>::B;
13872                     };
13873
13874                   A<T>::B will be represented by a TYPENAME_TYPE, and
13875                   therefore not recognized by check_tag_decl.  */
13876                if (!type
13877                    && decl_specifiers.type
13878                    && TYPE_P (decl_specifiers.type))
13879                  type = decl_specifiers.type;
13880                if (!type || !TYPE_P (type))
13881                  error ("friend declaration does not name a class or "
13882                         "function");
13883                else
13884                  make_friend_class (current_class_type, type,
13885                                     /*complain=*/true);
13886             }
13887           /* If there is no TYPE, an error message will already have
13888              been issued.  */
13889           else if (!type || type == error_mark_node)
13890             ;
13891           /* An anonymous aggregate has to be handled specially; such
13892              a declaration really declares a data member (with a
13893              particular type), as opposed to a nested class.  */
13894           else if (ANON_AGGR_TYPE_P (type))
13895             {
13896               /* Remove constructors and such from TYPE, now that we
13897                  know it is an anonymous aggregate.  */
13898               fixup_anonymous_aggr (type);
13899               /* And make the corresponding data member.  */
13900               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13901               /* Add it to the class.  */
13902               finish_member_declaration (decl);
13903             }
13904           else
13905             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13906         }
13907     }
13908   else
13909     {
13910       /* See if these declarations will be friends.  */
13911       friend_p = cp_parser_friend_p (&decl_specifiers);
13912
13913       /* Keep going until we hit the `;' at the end of the
13914          declaration.  */
13915       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13916         {
13917           tree attributes = NULL_TREE;
13918           tree first_attribute;
13919
13920           /* Peek at the next token.  */
13921           token = cp_lexer_peek_token (parser->lexer);
13922
13923           /* Check for a bitfield declaration.  */
13924           if (token->type == CPP_COLON
13925               || (token->type == CPP_NAME
13926                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13927                   == CPP_COLON))
13928             {
13929               tree identifier;
13930               tree width;
13931
13932               /* Get the name of the bitfield.  Note that we cannot just
13933                  check TOKEN here because it may have been invalidated by
13934                  the call to cp_lexer_peek_nth_token above.  */
13935               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13936                 identifier = cp_parser_identifier (parser);
13937               else
13938                 identifier = NULL_TREE;
13939
13940               /* Consume the `:' token.  */
13941               cp_lexer_consume_token (parser->lexer);
13942               /* Get the width of the bitfield.  */
13943               width
13944                 = cp_parser_constant_expression (parser,
13945                                                  /*allow_non_constant=*/false,
13946                                                  NULL);
13947
13948               /* Look for attributes that apply to the bitfield.  */
13949               attributes = cp_parser_attributes_opt (parser);
13950               /* Remember which attributes are prefix attributes and
13951                  which are not.  */
13952               first_attribute = attributes;
13953               /* Combine the attributes.  */
13954               attributes = chainon (prefix_attributes, attributes);
13955
13956               /* Create the bitfield declaration.  */
13957               decl = grokbitfield (identifier
13958                                    ? make_id_declarator (NULL_TREE,
13959                                                          identifier,
13960                                                          sfk_none)
13961                                    : NULL,
13962                                    &decl_specifiers,
13963                                    width);
13964               /* Apply the attributes.  */
13965               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13966             }
13967           else
13968             {
13969               cp_declarator *declarator;
13970               tree initializer;
13971               tree asm_specification;
13972               int ctor_dtor_or_conv_p;
13973
13974               /* Parse the declarator.  */
13975               declarator
13976                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13977                                         &ctor_dtor_or_conv_p,
13978                                         /*parenthesized_p=*/NULL,
13979                                         /*member_p=*/true);
13980
13981               /* If something went wrong parsing the declarator, make sure
13982                  that we at least consume some tokens.  */
13983               if (declarator == cp_error_declarator)
13984                 {
13985                   /* Skip to the end of the statement.  */
13986                   cp_parser_skip_to_end_of_statement (parser);
13987                   /* If the next token is not a semicolon, that is
13988                      probably because we just skipped over the body of
13989                      a function.  So, we consume a semicolon if
13990                      present, but do not issue an error message if it
13991                      is not present.  */
13992                   if (cp_lexer_next_token_is (parser->lexer,
13993                                               CPP_SEMICOLON))
13994                     cp_lexer_consume_token (parser->lexer);
13995                   return;
13996                 }
13997
13998               if (declares_class_or_enum & 2)
13999                 cp_parser_check_for_definition_in_return_type
14000                   (declarator, decl_specifiers.type);
14001
14002               /* Look for an asm-specification.  */
14003               asm_specification = cp_parser_asm_specification_opt (parser);
14004               /* Look for attributes that apply to the declaration.  */
14005               attributes = cp_parser_attributes_opt (parser);
14006               /* Remember which attributes are prefix attributes and
14007                  which are not.  */
14008               first_attribute = attributes;
14009               /* Combine the attributes.  */
14010               attributes = chainon (prefix_attributes, attributes);
14011
14012               /* If it's an `=', then we have a constant-initializer or a
14013                  pure-specifier.  It is not correct to parse the
14014                  initializer before registering the member declaration
14015                  since the member declaration should be in scope while
14016                  its initializer is processed.  However, the rest of the
14017                  front end does not yet provide an interface that allows
14018                  us to handle this correctly.  */
14019               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14020                 {
14021                   /* In [class.mem]:
14022
14023                      A pure-specifier shall be used only in the declaration of
14024                      a virtual function.
14025
14026                      A member-declarator can contain a constant-initializer
14027                      only if it declares a static member of integral or
14028                      enumeration type.
14029
14030                      Therefore, if the DECLARATOR is for a function, we look
14031                      for a pure-specifier; otherwise, we look for a
14032                      constant-initializer.  When we call `grokfield', it will
14033                      perform more stringent semantics checks.  */
14034                   if (function_declarator_p (declarator))
14035                     initializer = cp_parser_pure_specifier (parser);
14036                   else
14037                     /* Parse the initializer.  */
14038                     initializer = cp_parser_constant_initializer (parser);
14039                 }
14040               /* Otherwise, there is no initializer.  */
14041               else
14042                 initializer = NULL_TREE;
14043
14044               /* See if we are probably looking at a function
14045                  definition.  We are certainly not looking at a
14046                  member-declarator.  Calling `grokfield' has
14047                  side-effects, so we must not do it unless we are sure
14048                  that we are looking at a member-declarator.  */
14049               if (cp_parser_token_starts_function_definition_p
14050                   (cp_lexer_peek_token (parser->lexer)))
14051                 {
14052                   /* The grammar does not allow a pure-specifier to be
14053                      used when a member function is defined.  (It is
14054                      possible that this fact is an oversight in the
14055                      standard, since a pure function may be defined
14056                      outside of the class-specifier.  */
14057                   if (initializer)
14058                     error ("pure-specifier on function-definition");
14059                   decl = cp_parser_save_member_function_body (parser,
14060                                                               &decl_specifiers,
14061                                                               declarator,
14062                                                               attributes);
14063                   /* If the member was not a friend, declare it here.  */
14064                   if (!friend_p)
14065                     finish_member_declaration (decl);
14066                   /* Peek at the next token.  */
14067                   token = cp_lexer_peek_token (parser->lexer);
14068                   /* If the next token is a semicolon, consume it.  */
14069                   if (token->type == CPP_SEMICOLON)
14070                     cp_lexer_consume_token (parser->lexer);
14071                   return;
14072                 }
14073               else
14074                 /* Create the declaration.  */
14075                 decl = grokfield (declarator, &decl_specifiers,
14076                                   initializer, /*init_const_expr_p=*/true,
14077                                   asm_specification,
14078                                   attributes);
14079             }
14080
14081           /* Reset PREFIX_ATTRIBUTES.  */
14082           while (attributes && TREE_CHAIN (attributes) != first_attribute)
14083             attributes = TREE_CHAIN (attributes);
14084           if (attributes)
14085             TREE_CHAIN (attributes) = NULL_TREE;
14086
14087           /* If there is any qualification still in effect, clear it
14088              now; we will be starting fresh with the next declarator.  */
14089           parser->scope = NULL_TREE;
14090           parser->qualifying_scope = NULL_TREE;
14091           parser->object_scope = NULL_TREE;
14092           /* If it's a `,', then there are more declarators.  */
14093           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14094             cp_lexer_consume_token (parser->lexer);
14095           /* If the next token isn't a `;', then we have a parse error.  */
14096           else if (cp_lexer_next_token_is_not (parser->lexer,
14097                                                CPP_SEMICOLON))
14098             {
14099               cp_parser_error (parser, "expected %<;%>");
14100               /* Skip tokens until we find a `;'.  */
14101               cp_parser_skip_to_end_of_statement (parser);
14102
14103               break;
14104             }
14105
14106           if (decl)
14107             {
14108               /* Add DECL to the list of members.  */
14109               if (!friend_p)
14110                 finish_member_declaration (decl);
14111
14112               if (TREE_CODE (decl) == FUNCTION_DECL)
14113                 cp_parser_save_default_args (parser, decl);
14114             }
14115         }
14116     }
14117
14118   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14119 }
14120
14121 /* Parse a pure-specifier.
14122
14123    pure-specifier:
14124      = 0
14125
14126    Returns INTEGER_ZERO_NODE if a pure specifier is found.
14127    Otherwise, ERROR_MARK_NODE is returned.  */
14128
14129 static tree
14130 cp_parser_pure_specifier (cp_parser* parser)
14131 {
14132   cp_token *token;
14133
14134   /* Look for the `=' token.  */
14135   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14136     return error_mark_node;
14137   /* Look for the `0' token.  */
14138   token = cp_lexer_consume_token (parser->lexer);
14139   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
14140   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14141     {
14142       cp_parser_error (parser,
14143                        "invalid pure specifier (only `= 0' is allowed)");
14144       cp_parser_skip_to_end_of_statement (parser);
14145       return error_mark_node;
14146     }
14147   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14148     {
14149       error ("templates may not be %<virtual%>");
14150       return error_mark_node;
14151     }
14152
14153   return integer_zero_node;
14154 }
14155
14156 /* Parse a constant-initializer.
14157
14158    constant-initializer:
14159      = constant-expression
14160
14161    Returns a representation of the constant-expression.  */
14162
14163 static tree
14164 cp_parser_constant_initializer (cp_parser* parser)
14165 {
14166   /* Look for the `=' token.  */
14167   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14168     return error_mark_node;
14169
14170   /* It is invalid to write:
14171
14172        struct S { static const int i = { 7 }; };
14173
14174      */
14175   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14176     {
14177       cp_parser_error (parser,
14178                        "a brace-enclosed initializer is not allowed here");
14179       /* Consume the opening brace.  */
14180       cp_lexer_consume_token (parser->lexer);
14181       /* Skip the initializer.  */
14182       cp_parser_skip_to_closing_brace (parser);
14183       /* Look for the trailing `}'.  */
14184       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14185
14186       return error_mark_node;
14187     }
14188
14189   return cp_parser_constant_expression (parser,
14190                                         /*allow_non_constant=*/false,
14191                                         NULL);
14192 }
14193
14194 /* Derived classes [gram.class.derived] */
14195
14196 /* Parse a base-clause.
14197
14198    base-clause:
14199      : base-specifier-list
14200
14201    base-specifier-list:
14202      base-specifier
14203      base-specifier-list , base-specifier
14204
14205    Returns a TREE_LIST representing the base-classes, in the order in
14206    which they were declared.  The representation of each node is as
14207    described by cp_parser_base_specifier.
14208
14209    In the case that no bases are specified, this function will return
14210    NULL_TREE, not ERROR_MARK_NODE.  */
14211
14212 static tree
14213 cp_parser_base_clause (cp_parser* parser)
14214 {
14215   tree bases = NULL_TREE;
14216
14217   /* Look for the `:' that begins the list.  */
14218   cp_parser_require (parser, CPP_COLON, "`:'");
14219
14220   /* Scan the base-specifier-list.  */
14221   while (true)
14222     {
14223       cp_token *token;
14224       tree base;
14225
14226       /* Look for the base-specifier.  */
14227       base = cp_parser_base_specifier (parser);
14228       /* Add BASE to the front of the list.  */
14229       if (base != error_mark_node)
14230         {
14231           TREE_CHAIN (base) = bases;
14232           bases = base;
14233         }
14234       /* Peek at the next token.  */
14235       token = cp_lexer_peek_token (parser->lexer);
14236       /* If it's not a comma, then the list is complete.  */
14237       if (token->type != CPP_COMMA)
14238         break;
14239       /* Consume the `,'.  */
14240       cp_lexer_consume_token (parser->lexer);
14241     }
14242
14243   /* PARSER->SCOPE may still be non-NULL at this point, if the last
14244      base class had a qualified name.  However, the next name that
14245      appears is certainly not qualified.  */
14246   parser->scope = NULL_TREE;
14247   parser->qualifying_scope = NULL_TREE;
14248   parser->object_scope = NULL_TREE;
14249
14250   return nreverse (bases);
14251 }
14252
14253 /* Parse a base-specifier.
14254
14255    base-specifier:
14256      :: [opt] nested-name-specifier [opt] class-name
14257      virtual access-specifier [opt] :: [opt] nested-name-specifier
14258        [opt] class-name
14259      access-specifier virtual [opt] :: [opt] nested-name-specifier
14260        [opt] class-name
14261
14262    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14263    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14264    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14265    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14266
14267 static tree
14268 cp_parser_base_specifier (cp_parser* parser)
14269 {
14270   cp_token *token;
14271   bool done = false;
14272   bool virtual_p = false;
14273   bool duplicate_virtual_error_issued_p = false;
14274   bool duplicate_access_error_issued_p = false;
14275   bool class_scope_p, template_p;
14276   tree access = access_default_node;
14277   tree type;
14278
14279   /* Process the optional `virtual' and `access-specifier'.  */
14280   while (!done)
14281     {
14282       /* Peek at the next token.  */
14283       token = cp_lexer_peek_token (parser->lexer);
14284       /* Process `virtual'.  */
14285       switch (token->keyword)
14286         {
14287         case RID_VIRTUAL:
14288           /* If `virtual' appears more than once, issue an error.  */
14289           if (virtual_p && !duplicate_virtual_error_issued_p)
14290             {
14291               cp_parser_error (parser,
14292                                "%<virtual%> specified more than once in base-specified");
14293               duplicate_virtual_error_issued_p = true;
14294             }
14295
14296           virtual_p = true;
14297
14298           /* Consume the `virtual' token.  */
14299           cp_lexer_consume_token (parser->lexer);
14300
14301           break;
14302
14303         case RID_PUBLIC:
14304         case RID_PROTECTED:
14305         case RID_PRIVATE:
14306           /* If more than one access specifier appears, issue an
14307              error.  */
14308           if (access != access_default_node
14309               && !duplicate_access_error_issued_p)
14310             {
14311               cp_parser_error (parser,
14312                                "more than one access specifier in base-specified");
14313               duplicate_access_error_issued_p = true;
14314             }
14315
14316           access = ridpointers[(int) token->keyword];
14317
14318           /* Consume the access-specifier.  */
14319           cp_lexer_consume_token (parser->lexer);
14320
14321           break;
14322
14323         default:
14324           done = true;
14325           break;
14326         }
14327     }
14328   /* It is not uncommon to see programs mechanically, erroneously, use
14329      the 'typename' keyword to denote (dependent) qualified types
14330      as base classes.  */
14331   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14332     {
14333       if (!processing_template_decl)
14334         error ("keyword %<typename%> not allowed outside of templates");
14335       else
14336         error ("keyword %<typename%> not allowed in this context "
14337                "(the base class is implicitly a type)");
14338       cp_lexer_consume_token (parser->lexer);
14339     }
14340
14341   /* Look for the optional `::' operator.  */
14342   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14343   /* Look for the nested-name-specifier.  The simplest way to
14344      implement:
14345
14346        [temp.res]
14347
14348        The keyword `typename' is not permitted in a base-specifier or
14349        mem-initializer; in these contexts a qualified name that
14350        depends on a template-parameter is implicitly assumed to be a
14351        type name.
14352
14353      is to pretend that we have seen the `typename' keyword at this
14354      point.  */
14355   cp_parser_nested_name_specifier_opt (parser,
14356                                        /*typename_keyword_p=*/true,
14357                                        /*check_dependency_p=*/true,
14358                                        typename_type,
14359                                        /*is_declaration=*/true);
14360   /* If the base class is given by a qualified name, assume that names
14361      we see are type names or templates, as appropriate.  */
14362   class_scope_p = (parser->scope && TYPE_P (parser->scope));
14363   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14364
14365   /* Finally, look for the class-name.  */
14366   type = cp_parser_class_name (parser,
14367                                class_scope_p,
14368                                template_p,
14369                                typename_type,
14370                                /*check_dependency_p=*/true,
14371                                /*class_head_p=*/false,
14372                                /*is_declaration=*/true);
14373
14374   if (type == error_mark_node)
14375     return error_mark_node;
14376
14377   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14378 }
14379
14380 /* Exception handling [gram.exception] */
14381
14382 /* Parse an (optional) exception-specification.
14383
14384    exception-specification:
14385      throw ( type-id-list [opt] )
14386
14387    Returns a TREE_LIST representing the exception-specification.  The
14388    TREE_VALUE of each node is a type.  */
14389
14390 static tree
14391 cp_parser_exception_specification_opt (cp_parser* parser)
14392 {
14393   cp_token *token;
14394   tree type_id_list;
14395
14396   /* Peek at the next token.  */
14397   token = cp_lexer_peek_token (parser->lexer);
14398   /* If it's not `throw', then there's no exception-specification.  */
14399   if (!cp_parser_is_keyword (token, RID_THROW))
14400     return NULL_TREE;
14401
14402   /* Consume the `throw'.  */
14403   cp_lexer_consume_token (parser->lexer);
14404
14405   /* Look for the `('.  */
14406   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14407
14408   /* Peek at the next token.  */
14409   token = cp_lexer_peek_token (parser->lexer);
14410   /* If it's not a `)', then there is a type-id-list.  */
14411   if (token->type != CPP_CLOSE_PAREN)
14412     {
14413       const char *saved_message;
14414
14415       /* Types may not be defined in an exception-specification.  */
14416       saved_message = parser->type_definition_forbidden_message;
14417       parser->type_definition_forbidden_message
14418         = "types may not be defined in an exception-specification";
14419       /* Parse the type-id-list.  */
14420       type_id_list = cp_parser_type_id_list (parser);
14421       /* Restore the saved message.  */
14422       parser->type_definition_forbidden_message = saved_message;
14423     }
14424   else
14425     type_id_list = empty_except_spec;
14426
14427   /* Look for the `)'.  */
14428   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14429
14430   return type_id_list;
14431 }
14432
14433 /* Parse an (optional) type-id-list.
14434
14435    type-id-list:
14436      type-id
14437      type-id-list , type-id
14438
14439    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14440    in the order that the types were presented.  */
14441
14442 static tree
14443 cp_parser_type_id_list (cp_parser* parser)
14444 {
14445   tree types = NULL_TREE;
14446
14447   while (true)
14448     {
14449       cp_token *token;
14450       tree type;
14451
14452       /* Get the next type-id.  */
14453       type = cp_parser_type_id (parser);
14454       /* Add it to the list.  */
14455       types = add_exception_specifier (types, type, /*complain=*/1);
14456       /* Peek at the next token.  */
14457       token = cp_lexer_peek_token (parser->lexer);
14458       /* If it is not a `,', we are done.  */
14459       if (token->type != CPP_COMMA)
14460         break;
14461       /* Consume the `,'.  */
14462       cp_lexer_consume_token (parser->lexer);
14463     }
14464
14465   return nreverse (types);
14466 }
14467
14468 /* Parse a try-block.
14469
14470    try-block:
14471      try compound-statement handler-seq  */
14472
14473 static tree
14474 cp_parser_try_block (cp_parser* parser)
14475 {
14476   tree try_block;
14477
14478   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14479   try_block = begin_try_block ();
14480   cp_parser_compound_statement (parser, NULL, true);
14481   finish_try_block (try_block);
14482   cp_parser_handler_seq (parser);
14483   finish_handler_sequence (try_block);
14484
14485   return try_block;
14486 }
14487
14488 /* Parse a function-try-block.
14489
14490    function-try-block:
14491      try ctor-initializer [opt] function-body handler-seq  */
14492
14493 static bool
14494 cp_parser_function_try_block (cp_parser* parser)
14495 {
14496   tree compound_stmt;
14497   tree try_block;
14498   bool ctor_initializer_p;
14499
14500   /* Look for the `try' keyword.  */
14501   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14502     return false;
14503   /* Let the rest of the front-end know where we are.  */
14504   try_block = begin_function_try_block (&compound_stmt);
14505   /* Parse the function-body.  */
14506   ctor_initializer_p
14507     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14508   /* We're done with the `try' part.  */
14509   finish_function_try_block (try_block);
14510   /* Parse the handlers.  */
14511   cp_parser_handler_seq (parser);
14512   /* We're done with the handlers.  */
14513   finish_function_handler_sequence (try_block, compound_stmt);
14514
14515   return ctor_initializer_p;
14516 }
14517
14518 /* Parse a handler-seq.
14519
14520    handler-seq:
14521      handler handler-seq [opt]  */
14522
14523 static void
14524 cp_parser_handler_seq (cp_parser* parser)
14525 {
14526   while (true)
14527     {
14528       cp_token *token;
14529
14530       /* Parse the handler.  */
14531       cp_parser_handler (parser);
14532       /* Peek at the next token.  */
14533       token = cp_lexer_peek_token (parser->lexer);
14534       /* If it's not `catch' then there are no more handlers.  */
14535       if (!cp_parser_is_keyword (token, RID_CATCH))
14536         break;
14537     }
14538 }
14539
14540 /* Parse a handler.
14541
14542    handler:
14543      catch ( exception-declaration ) compound-statement  */
14544
14545 static void
14546 cp_parser_handler (cp_parser* parser)
14547 {
14548   tree handler;
14549   tree declaration;
14550
14551   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14552   handler = begin_handler ();
14553   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14554   declaration = cp_parser_exception_declaration (parser);
14555   finish_handler_parms (declaration, handler);
14556   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14557   cp_parser_compound_statement (parser, NULL, false);
14558   finish_handler (handler);
14559 }
14560
14561 /* Parse an exception-declaration.
14562
14563    exception-declaration:
14564      type-specifier-seq declarator
14565      type-specifier-seq abstract-declarator
14566      type-specifier-seq
14567      ...
14568
14569    Returns a VAR_DECL for the declaration, or NULL_TREE if the
14570    ellipsis variant is used.  */
14571
14572 static tree
14573 cp_parser_exception_declaration (cp_parser* parser)
14574 {
14575   cp_decl_specifier_seq type_specifiers;
14576   cp_declarator *declarator;
14577   const char *saved_message;
14578
14579   /* If it's an ellipsis, it's easy to handle.  */
14580   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14581     {
14582       /* Consume the `...' token.  */
14583       cp_lexer_consume_token (parser->lexer);
14584       return NULL_TREE;
14585     }
14586
14587   /* Types may not be defined in exception-declarations.  */
14588   saved_message = parser->type_definition_forbidden_message;
14589   parser->type_definition_forbidden_message
14590     = "types may not be defined in exception-declarations";
14591
14592   /* Parse the type-specifier-seq.  */
14593   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14594                                 &type_specifiers);
14595   /* If it's a `)', then there is no declarator.  */
14596   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14597     declarator = NULL;
14598   else
14599     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14600                                        /*ctor_dtor_or_conv_p=*/NULL,
14601                                        /*parenthesized_p=*/NULL,
14602                                        /*member_p=*/false);
14603
14604   /* Restore the saved message.  */
14605   parser->type_definition_forbidden_message = saved_message;
14606
14607   if (!type_specifiers.any_specifiers_p)
14608     return error_mark_node;
14609
14610   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14611 }
14612
14613 /* Parse a throw-expression.
14614
14615    throw-expression:
14616      throw assignment-expression [opt]
14617
14618    Returns a THROW_EXPR representing the throw-expression.  */
14619
14620 static tree
14621 cp_parser_throw_expression (cp_parser* parser)
14622 {
14623   tree expression;
14624   cp_token* token;
14625
14626   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14627   token = cp_lexer_peek_token (parser->lexer);
14628   /* Figure out whether or not there is an assignment-expression
14629      following the "throw" keyword.  */
14630   if (token->type == CPP_COMMA
14631       || token->type == CPP_SEMICOLON
14632       || token->type == CPP_CLOSE_PAREN
14633       || token->type == CPP_CLOSE_SQUARE
14634       || token->type == CPP_CLOSE_BRACE
14635       || token->type == CPP_COLON)
14636     expression = NULL_TREE;
14637   else
14638     expression = cp_parser_assignment_expression (parser,
14639                                                   /*cast_p=*/false);
14640
14641   return build_throw (expression);
14642 }
14643
14644 /* GNU Extensions */
14645
14646 /* Parse an (optional) asm-specification.
14647
14648    asm-specification:
14649      asm ( string-literal )
14650
14651    If the asm-specification is present, returns a STRING_CST
14652    corresponding to the string-literal.  Otherwise, returns
14653    NULL_TREE.  */
14654
14655 static tree
14656 cp_parser_asm_specification_opt (cp_parser* parser)
14657 {
14658   cp_token *token;
14659   tree asm_specification;
14660
14661   /* Peek at the next token.  */
14662   token = cp_lexer_peek_token (parser->lexer);
14663   /* If the next token isn't the `asm' keyword, then there's no
14664      asm-specification.  */
14665   if (!cp_parser_is_keyword (token, RID_ASM))
14666     return NULL_TREE;
14667
14668   /* Consume the `asm' token.  */
14669   cp_lexer_consume_token (parser->lexer);
14670   /* Look for the `('.  */
14671   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14672
14673   /* Look for the string-literal.  */
14674   asm_specification = cp_parser_string_literal (parser, false, false);
14675
14676   /* Look for the `)'.  */
14677   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14678
14679   return asm_specification;
14680 }
14681
14682 /* Parse an asm-operand-list.
14683
14684    asm-operand-list:
14685      asm-operand
14686      asm-operand-list , asm-operand
14687
14688    asm-operand:
14689      string-literal ( expression )
14690      [ string-literal ] string-literal ( expression )
14691
14692    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14693    each node is the expression.  The TREE_PURPOSE is itself a
14694    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14695    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14696    is a STRING_CST for the string literal before the parenthesis.  */
14697
14698 static tree
14699 cp_parser_asm_operand_list (cp_parser* parser)
14700 {
14701   tree asm_operands = NULL_TREE;
14702
14703   while (true)
14704     {
14705       tree string_literal;
14706       tree expression;
14707       tree name;
14708
14709       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14710         {
14711           /* Consume the `[' token.  */
14712           cp_lexer_consume_token (parser->lexer);
14713           /* Read the operand name.  */
14714           name = cp_parser_identifier (parser);
14715           if (name != error_mark_node)
14716             name = build_string (IDENTIFIER_LENGTH (name),
14717                                  IDENTIFIER_POINTER (name));
14718           /* Look for the closing `]'.  */
14719           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14720         }
14721       else
14722         name = NULL_TREE;
14723       /* Look for the string-literal.  */
14724       string_literal = cp_parser_string_literal (parser, false, false);
14725
14726       /* Look for the `('.  */
14727       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14728       /* Parse the expression.  */
14729       expression = cp_parser_expression (parser, /*cast_p=*/false);
14730       /* Look for the `)'.  */
14731       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14732
14733       /* Add this operand to the list.  */
14734       asm_operands = tree_cons (build_tree_list (name, string_literal),
14735                                 expression,
14736                                 asm_operands);
14737       /* If the next token is not a `,', there are no more
14738          operands.  */
14739       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14740         break;
14741       /* Consume the `,'.  */
14742       cp_lexer_consume_token (parser->lexer);
14743     }
14744
14745   return nreverse (asm_operands);
14746 }
14747
14748 /* Parse an asm-clobber-list.
14749
14750    asm-clobber-list:
14751      string-literal
14752      asm-clobber-list , string-literal
14753
14754    Returns a TREE_LIST, indicating the clobbers in the order that they
14755    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14756
14757 static tree
14758 cp_parser_asm_clobber_list (cp_parser* parser)
14759 {
14760   tree clobbers = NULL_TREE;
14761
14762   while (true)
14763     {
14764       tree string_literal;
14765
14766       /* Look for the string literal.  */
14767       string_literal = cp_parser_string_literal (parser, false, false);
14768       /* Add it to the list.  */
14769       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14770       /* If the next token is not a `,', then the list is
14771          complete.  */
14772       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14773         break;
14774       /* Consume the `,' token.  */
14775       cp_lexer_consume_token (parser->lexer);
14776     }
14777
14778   return clobbers;
14779 }
14780
14781 /* Parse an (optional) series of attributes.
14782
14783    attributes:
14784      attributes attribute
14785
14786    attribute:
14787      __attribute__ (( attribute-list [opt] ))
14788
14789    The return value is as for cp_parser_attribute_list.  */
14790
14791 static tree
14792 cp_parser_attributes_opt (cp_parser* parser)
14793 {
14794   tree attributes = NULL_TREE;
14795
14796   while (true)
14797     {
14798       cp_token *token;
14799       tree attribute_list;
14800
14801       /* Peek at the next token.  */
14802       token = cp_lexer_peek_token (parser->lexer);
14803       /* If it's not `__attribute__', then we're done.  */
14804       if (token->keyword != RID_ATTRIBUTE)
14805         break;
14806
14807       /* Consume the `__attribute__' keyword.  */
14808       cp_lexer_consume_token (parser->lexer);
14809       /* Look for the two `(' tokens.  */
14810       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14811       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14812
14813       /* Peek at the next token.  */
14814       token = cp_lexer_peek_token (parser->lexer);
14815       if (token->type != CPP_CLOSE_PAREN)
14816         /* Parse the attribute-list.  */
14817         attribute_list = cp_parser_attribute_list (parser);
14818       else
14819         /* If the next token is a `)', then there is no attribute
14820            list.  */
14821         attribute_list = NULL;
14822
14823       /* Look for the two `)' tokens.  */
14824       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14825       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14826
14827       /* Add these new attributes to the list.  */
14828       attributes = chainon (attributes, attribute_list);
14829     }
14830
14831   return attributes;
14832 }
14833
14834 /* Parse an attribute-list.
14835
14836    attribute-list:
14837      attribute
14838      attribute-list , attribute
14839
14840    attribute:
14841      identifier
14842      identifier ( identifier )
14843      identifier ( identifier , expression-list )
14844      identifier ( expression-list )
14845
14846    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14847    to an attribute.  The TREE_PURPOSE of each node is the identifier
14848    indicating which attribute is in use.  The TREE_VALUE represents
14849    the arguments, if any.  */
14850
14851 static tree
14852 cp_parser_attribute_list (cp_parser* parser)
14853 {
14854   tree attribute_list = NULL_TREE;
14855   bool save_translate_strings_p = parser->translate_strings_p;
14856
14857   parser->translate_strings_p = false;
14858   while (true)
14859     {
14860       cp_token *token;
14861       tree identifier;
14862       tree attribute;
14863
14864       /* Look for the identifier.  We also allow keywords here; for
14865          example `__attribute__ ((const))' is legal.  */
14866       token = cp_lexer_peek_token (parser->lexer);
14867       if (token->type == CPP_NAME
14868           || token->type == CPP_KEYWORD)
14869         {
14870           tree arguments = NULL_TREE;
14871
14872           /* Consume the token.  */
14873           token = cp_lexer_consume_token (parser->lexer);
14874
14875           /* Save away the identifier that indicates which attribute
14876              this is.  */
14877           identifier = token->value;
14878           attribute = build_tree_list (identifier, NULL_TREE);
14879
14880           /* Peek at the next token.  */
14881           token = cp_lexer_peek_token (parser->lexer);
14882           /* If it's an `(', then parse the attribute arguments.  */
14883           if (token->type == CPP_OPEN_PAREN)
14884             {
14885               arguments = cp_parser_parenthesized_expression_list
14886                           (parser, true, /*cast_p=*/false,
14887                            /*non_constant_p=*/NULL);
14888               /* Save the arguments away.  */
14889               TREE_VALUE (attribute) = arguments;
14890             }
14891
14892           if (arguments != error_mark_node)
14893             {
14894               /* Add this attribute to the list.  */
14895               TREE_CHAIN (attribute) = attribute_list;
14896               attribute_list = attribute;
14897             }
14898
14899           token = cp_lexer_peek_token (parser->lexer);
14900         }
14901       /* Now, look for more attributes.  If the next token isn't a
14902          `,', we're done.  */
14903       if (token->type != CPP_COMMA)
14904         break;
14905
14906       /* Consume the comma and keep going.  */
14907       cp_lexer_consume_token (parser->lexer);
14908     }
14909   parser->translate_strings_p = save_translate_strings_p;
14910
14911   /* We built up the list in reverse order.  */
14912   return nreverse (attribute_list);
14913 }
14914
14915 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14916    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14917    current value of the PEDANTIC flag, regardless of whether or not
14918    the `__extension__' keyword is present.  The caller is responsible
14919    for restoring the value of the PEDANTIC flag.  */
14920
14921 static bool
14922 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14923 {
14924   /* Save the old value of the PEDANTIC flag.  */
14925   *saved_pedantic = pedantic;
14926
14927   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14928     {
14929       /* Consume the `__extension__' token.  */
14930       cp_lexer_consume_token (parser->lexer);
14931       /* We're not being pedantic while the `__extension__' keyword is
14932          in effect.  */
14933       pedantic = 0;
14934
14935       return true;
14936     }
14937
14938   return false;
14939 }
14940
14941 /* Parse a label declaration.
14942
14943    label-declaration:
14944      __label__ label-declarator-seq ;
14945
14946    label-declarator-seq:
14947      identifier , label-declarator-seq
14948      identifier  */
14949
14950 static void
14951 cp_parser_label_declaration (cp_parser* parser)
14952 {
14953   /* Look for the `__label__' keyword.  */
14954   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14955
14956   while (true)
14957     {
14958       tree identifier;
14959
14960       /* Look for an identifier.  */
14961       identifier = cp_parser_identifier (parser);
14962       /* If we failed, stop.  */
14963       if (identifier == error_mark_node)
14964         break;
14965       /* Declare it as a label.  */
14966       finish_label_decl (identifier);
14967       /* If the next token is a `;', stop.  */
14968       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14969         break;
14970       /* Look for the `,' separating the label declarations.  */
14971       cp_parser_require (parser, CPP_COMMA, "`,'");
14972     }
14973
14974   /* Look for the final `;'.  */
14975   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14976 }
14977
14978 /* Support Functions */
14979
14980 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14981    NAME should have one of the representations used for an
14982    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14983    is returned.  If PARSER->SCOPE is a dependent type, then a
14984    SCOPE_REF is returned.
14985
14986    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14987    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14988    was formed.  Abstractly, such entities should not be passed to this
14989    function, because they do not need to be looked up, but it is
14990    simpler to check for this special case here, rather than at the
14991    call-sites.
14992
14993    In cases not explicitly covered above, this function returns a
14994    DECL, OVERLOAD, or baselink representing the result of the lookup.
14995    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14996    is returned.
14997
14998    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14999    (e.g., "struct") that was used.  In that case bindings that do not
15000    refer to types are ignored.
15001
15002    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15003    ignored.
15004
15005    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15006    are ignored.
15007
15008    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15009    types.
15010
15011    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15012    TREE_LIST of candidates if name-lookup results in an ambiguity, and
15013    NULL_TREE otherwise.  */
15014
15015 static tree
15016 cp_parser_lookup_name (cp_parser *parser, tree name,
15017                        enum tag_types tag_type,
15018                        bool is_template,
15019                        bool is_namespace,
15020                        bool check_dependency,
15021                        tree *ambiguous_decls)
15022 {
15023   int flags = 0;
15024   tree decl;
15025   tree object_type = parser->context->object_type;
15026
15027   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15028     flags |= LOOKUP_COMPLAIN;
15029
15030   /* Assume that the lookup will be unambiguous.  */
15031   if (ambiguous_decls)
15032     *ambiguous_decls = NULL_TREE;
15033
15034   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15035      no longer valid.  Note that if we are parsing tentatively, and
15036      the parse fails, OBJECT_TYPE will be automatically restored.  */
15037   parser->context->object_type = NULL_TREE;
15038
15039   if (name == error_mark_node)
15040     return error_mark_node;
15041
15042   /* A template-id has already been resolved; there is no lookup to
15043      do.  */
15044   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15045     return name;
15046   if (BASELINK_P (name))
15047     {
15048       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15049                   == TEMPLATE_ID_EXPR);
15050       return name;
15051     }
15052
15053   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
15054      it should already have been checked to make sure that the name
15055      used matches the type being destroyed.  */
15056   if (TREE_CODE (name) == BIT_NOT_EXPR)
15057     {
15058       tree type;
15059
15060       /* Figure out to which type this destructor applies.  */
15061       if (parser->scope)
15062         type = parser->scope;
15063       else if (object_type)
15064         type = object_type;
15065       else
15066         type = current_class_type;
15067       /* If that's not a class type, there is no destructor.  */
15068       if (!type || !CLASS_TYPE_P (type))
15069         return error_mark_node;
15070       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15071         lazily_declare_fn (sfk_destructor, type);
15072       if (!CLASSTYPE_DESTRUCTORS (type))
15073           return error_mark_node;
15074       /* If it was a class type, return the destructor.  */
15075       return CLASSTYPE_DESTRUCTORS (type);
15076     }
15077
15078   /* By this point, the NAME should be an ordinary identifier.  If
15079      the id-expression was a qualified name, the qualifying scope is
15080      stored in PARSER->SCOPE at this point.  */
15081   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15082
15083   /* Perform the lookup.  */
15084   if (parser->scope)
15085     {
15086       bool dependent_p;
15087
15088       if (parser->scope == error_mark_node)
15089         return error_mark_node;
15090
15091       /* If the SCOPE is dependent, the lookup must be deferred until
15092          the template is instantiated -- unless we are explicitly
15093          looking up names in uninstantiated templates.  Even then, we
15094          cannot look up the name if the scope is not a class type; it
15095          might, for example, be a template type parameter.  */
15096       dependent_p = (TYPE_P (parser->scope)
15097                      && !(parser->in_declarator_p
15098                           && currently_open_class (parser->scope))
15099                      && dependent_type_p (parser->scope));
15100       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15101            && dependent_p)
15102         {
15103           if (tag_type)
15104             {
15105               tree type;
15106
15107               /* The resolution to Core Issue 180 says that `struct
15108                  A::B' should be considered a type-name, even if `A'
15109                  is dependent.  */
15110               type = make_typename_type (parser->scope, name, tag_type,
15111                                          /*complain=*/tf_error);
15112               decl = TYPE_NAME (type);
15113             }
15114           else if (is_template
15115                    && (cp_parser_next_token_ends_template_argument_p (parser)
15116                        || cp_lexer_next_token_is (parser->lexer,
15117                                                   CPP_CLOSE_PAREN)))
15118             decl = make_unbound_class_template (parser->scope,
15119                                                 name, NULL_TREE,
15120                                                 /*complain=*/tf_error);
15121           else
15122             decl = build_qualified_name (/*type=*/NULL_TREE,
15123                                          parser->scope, name,
15124                                          is_template);
15125         }
15126       else
15127         {
15128           tree pushed_scope = NULL_TREE;
15129
15130           /* If PARSER->SCOPE is a dependent type, then it must be a
15131              class type, and we must not be checking dependencies;
15132              otherwise, we would have processed this lookup above.  So
15133              that PARSER->SCOPE is not considered a dependent base by
15134              lookup_member, we must enter the scope here.  */
15135           if (dependent_p)
15136             pushed_scope = push_scope (parser->scope);
15137           /* If the PARSER->SCOPE is a template specialization, it
15138              may be instantiated during name lookup.  In that case,
15139              errors may be issued.  Even if we rollback the current
15140              tentative parse, those errors are valid.  */
15141           decl = lookup_qualified_name (parser->scope, name,
15142                                         tag_type != none_type,
15143                                         /*complain=*/true);
15144           if (pushed_scope)
15145             pop_scope (pushed_scope);
15146         }
15147       parser->qualifying_scope = parser->scope;
15148       parser->object_scope = NULL_TREE;
15149     }
15150   else if (object_type)
15151     {
15152       tree object_decl = NULL_TREE;
15153       /* Look up the name in the scope of the OBJECT_TYPE, unless the
15154          OBJECT_TYPE is not a class.  */
15155       if (CLASS_TYPE_P (object_type))
15156         /* If the OBJECT_TYPE is a template specialization, it may
15157            be instantiated during name lookup.  In that case, errors
15158            may be issued.  Even if we rollback the current tentative
15159            parse, those errors are valid.  */
15160         object_decl = lookup_member (object_type,
15161                                      name,
15162                                      /*protect=*/0,
15163                                      tag_type != none_type);
15164       /* Look it up in the enclosing context, too.  */
15165       decl = lookup_name_real (name, tag_type != none_type,
15166                                /*nonclass=*/0,
15167                                /*block_p=*/true, is_namespace, flags);
15168       parser->object_scope = object_type;
15169       parser->qualifying_scope = NULL_TREE;
15170       if (object_decl)
15171         decl = object_decl;
15172     }
15173   else
15174     {
15175       decl = lookup_name_real (name, tag_type != none_type,
15176                                /*nonclass=*/0,
15177                                /*block_p=*/true, is_namespace, flags);
15178       parser->qualifying_scope = NULL_TREE;
15179       parser->object_scope = NULL_TREE;
15180     }
15181
15182   /* If the lookup failed, let our caller know.  */
15183   if (!decl || decl == error_mark_node)
15184     return error_mark_node;
15185
15186   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
15187   if (TREE_CODE (decl) == TREE_LIST)
15188     {
15189       if (ambiguous_decls)
15190         *ambiguous_decls = decl;
15191       /* The error message we have to print is too complicated for
15192          cp_parser_error, so we incorporate its actions directly.  */
15193       if (!cp_parser_simulate_error (parser))
15194         {
15195           error ("reference to %qD is ambiguous", name);
15196           print_candidates (decl);
15197         }
15198       return error_mark_node;
15199     }
15200
15201   gcc_assert (DECL_P (decl)
15202               || TREE_CODE (decl) == OVERLOAD
15203               || TREE_CODE (decl) == SCOPE_REF
15204               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15205               || BASELINK_P (decl));
15206
15207   /* If we have resolved the name of a member declaration, check to
15208      see if the declaration is accessible.  When the name resolves to
15209      set of overloaded functions, accessibility is checked when
15210      overload resolution is done.
15211
15212      During an explicit instantiation, access is not checked at all,
15213      as per [temp.explicit].  */
15214   if (DECL_P (decl))
15215     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15216
15217   return decl;
15218 }
15219
15220 /* Like cp_parser_lookup_name, but for use in the typical case where
15221    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15222    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
15223
15224 static tree
15225 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15226 {
15227   return cp_parser_lookup_name (parser, name,
15228                                 none_type,
15229                                 /*is_template=*/false,
15230                                 /*is_namespace=*/false,
15231                                 /*check_dependency=*/true,
15232                                 /*ambiguous_decls=*/NULL);
15233 }
15234
15235 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15236    the current context, return the TYPE_DECL.  If TAG_NAME_P is
15237    true, the DECL indicates the class being defined in a class-head,
15238    or declared in an elaborated-type-specifier.
15239
15240    Otherwise, return DECL.  */
15241
15242 static tree
15243 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15244 {
15245   /* If the TEMPLATE_DECL is being declared as part of a class-head,
15246      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15247
15248        struct A {
15249          template <typename T> struct B;
15250        };
15251
15252        template <typename T> struct A::B {};
15253
15254      Similarly, in an elaborated-type-specifier:
15255
15256        namespace N { struct X{}; }
15257
15258        struct A {
15259          template <typename T> friend struct N::X;
15260        };
15261
15262      However, if the DECL refers to a class type, and we are in
15263      the scope of the class, then the name lookup automatically
15264      finds the TYPE_DECL created by build_self_reference rather
15265      than a TEMPLATE_DECL.  For example, in:
15266
15267        template <class T> struct S {
15268          S s;
15269        };
15270
15271      there is no need to handle such case.  */
15272
15273   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15274     return DECL_TEMPLATE_RESULT (decl);
15275
15276   return decl;
15277 }
15278
15279 /* If too many, or too few, template-parameter lists apply to the
15280    declarator, issue an error message.  Returns TRUE if all went well,
15281    and FALSE otherwise.  */
15282
15283 static bool
15284 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15285                                                 cp_declarator *declarator)
15286 {
15287   unsigned num_templates;
15288
15289   /* We haven't seen any classes that involve template parameters yet.  */
15290   num_templates = 0;
15291
15292   switch (declarator->kind)
15293     {
15294     case cdk_id:
15295       if (declarator->u.id.qualifying_scope)
15296         {
15297           tree scope;
15298           tree member;
15299
15300           scope = declarator->u.id.qualifying_scope;
15301           member = declarator->u.id.unqualified_name;
15302
15303           while (scope && CLASS_TYPE_P (scope))
15304             {
15305               /* You're supposed to have one `template <...>'
15306                  for every template class, but you don't need one
15307                  for a full specialization.  For example:
15308
15309                  template <class T> struct S{};
15310                  template <> struct S<int> { void f(); };
15311                  void S<int>::f () {}
15312
15313                  is correct; there shouldn't be a `template <>' for
15314                  the definition of `S<int>::f'.  */
15315               if (!CLASSTYPE_TEMPLATE_INFO (scope))
15316                 /* If SCOPE does not have template information of any
15317                    kind, then it is not a template, nor is it nested
15318                    within a template.  */
15319                 break;
15320               if (explicit_class_specialization_p (scope))
15321                 break;
15322               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15323                 ++num_templates;
15324
15325               scope = TYPE_CONTEXT (scope);
15326             }
15327         }
15328       else if (TREE_CODE (declarator->u.id.unqualified_name)
15329                == TEMPLATE_ID_EXPR)
15330         /* If the DECLARATOR has the form `X<y>' then it uses one
15331            additional level of template parameters.  */
15332         ++num_templates;
15333
15334       return cp_parser_check_template_parameters (parser,
15335                                                   num_templates);
15336
15337     case cdk_function:
15338     case cdk_array:
15339     case cdk_pointer:
15340     case cdk_reference:
15341     case cdk_ptrmem:
15342       return (cp_parser_check_declarator_template_parameters
15343               (parser, declarator->declarator));
15344
15345     case cdk_error:
15346       return true;
15347
15348     default:
15349       gcc_unreachable ();
15350     }
15351   return false;
15352 }
15353
15354 /* NUM_TEMPLATES were used in the current declaration.  If that is
15355    invalid, return FALSE and issue an error messages.  Otherwise,
15356    return TRUE.  */
15357
15358 static bool
15359 cp_parser_check_template_parameters (cp_parser* parser,
15360                                      unsigned num_templates)
15361 {
15362   /* If there are more template classes than parameter lists, we have
15363      something like:
15364
15365        template <class T> void S<T>::R<T>::f ();  */
15366   if (parser->num_template_parameter_lists < num_templates)
15367     {
15368       error ("too few template-parameter-lists");
15369       return false;
15370     }
15371   /* If there are the same number of template classes and parameter
15372      lists, that's OK.  */
15373   if (parser->num_template_parameter_lists == num_templates)
15374     return true;
15375   /* If there are more, but only one more, then we are referring to a
15376      member template.  That's OK too.  */
15377   if (parser->num_template_parameter_lists == num_templates + 1)
15378       return true;
15379   /* Otherwise, there are too many template parameter lists.  We have
15380      something like:
15381
15382      template <class T> template <class U> void S::f();  */
15383   error ("too many template-parameter-lists");
15384   return false;
15385 }
15386
15387 /* Parse an optional `::' token indicating that the following name is
15388    from the global namespace.  If so, PARSER->SCOPE is set to the
15389    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15390    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15391    Returns the new value of PARSER->SCOPE, if the `::' token is
15392    present, and NULL_TREE otherwise.  */
15393
15394 static tree
15395 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15396 {
15397   cp_token *token;
15398
15399   /* Peek at the next token.  */
15400   token = cp_lexer_peek_token (parser->lexer);
15401   /* If we're looking at a `::' token then we're starting from the
15402      global namespace, not our current location.  */
15403   if (token->type == CPP_SCOPE)
15404     {
15405       /* Consume the `::' token.  */
15406       cp_lexer_consume_token (parser->lexer);
15407       /* Set the SCOPE so that we know where to start the lookup.  */
15408       parser->scope = global_namespace;
15409       parser->qualifying_scope = global_namespace;
15410       parser->object_scope = NULL_TREE;
15411
15412       return parser->scope;
15413     }
15414   else if (!current_scope_valid_p)
15415     {
15416       parser->scope = NULL_TREE;
15417       parser->qualifying_scope = NULL_TREE;
15418       parser->object_scope = NULL_TREE;
15419     }
15420
15421   return NULL_TREE;
15422 }
15423
15424 /* Returns TRUE if the upcoming token sequence is the start of a
15425    constructor declarator.  If FRIEND_P is true, the declarator is
15426    preceded by the `friend' specifier.  */
15427
15428 static bool
15429 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15430 {
15431   bool constructor_p;
15432   tree type_decl = NULL_TREE;
15433   bool nested_name_p;
15434   cp_token *next_token;
15435
15436   /* The common case is that this is not a constructor declarator, so
15437      try to avoid doing lots of work if at all possible.  It's not
15438      valid declare a constructor at function scope.  */
15439   if (parser->in_function_body)
15440     return false;
15441   /* And only certain tokens can begin a constructor declarator.  */
15442   next_token = cp_lexer_peek_token (parser->lexer);
15443   if (next_token->type != CPP_NAME
15444       && next_token->type != CPP_SCOPE
15445       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15446       && next_token->type != CPP_TEMPLATE_ID)
15447     return false;
15448
15449   /* Parse tentatively; we are going to roll back all of the tokens
15450      consumed here.  */
15451   cp_parser_parse_tentatively (parser);
15452   /* Assume that we are looking at a constructor declarator.  */
15453   constructor_p = true;
15454
15455   /* Look for the optional `::' operator.  */
15456   cp_parser_global_scope_opt (parser,
15457                               /*current_scope_valid_p=*/false);
15458   /* Look for the nested-name-specifier.  */
15459   nested_name_p
15460     = (cp_parser_nested_name_specifier_opt (parser,
15461                                             /*typename_keyword_p=*/false,
15462                                             /*check_dependency_p=*/false,
15463                                             /*type_p=*/false,
15464                                             /*is_declaration=*/false)
15465        != NULL_TREE);
15466   /* Outside of a class-specifier, there must be a
15467      nested-name-specifier.  */
15468   if (!nested_name_p &&
15469       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15470        || friend_p))
15471     constructor_p = false;
15472   /* If we still think that this might be a constructor-declarator,
15473      look for a class-name.  */
15474   if (constructor_p)
15475     {
15476       /* If we have:
15477
15478            template <typename T> struct S { S(); };
15479            template <typename T> S<T>::S ();
15480
15481          we must recognize that the nested `S' names a class.
15482          Similarly, for:
15483
15484            template <typename T> S<T>::S<T> ();
15485
15486          we must recognize that the nested `S' names a template.  */
15487       type_decl = cp_parser_class_name (parser,
15488                                         /*typename_keyword_p=*/false,
15489                                         /*template_keyword_p=*/false,
15490                                         none_type,
15491                                         /*check_dependency_p=*/false,
15492                                         /*class_head_p=*/false,
15493                                         /*is_declaration=*/false);
15494       /* If there was no class-name, then this is not a constructor.  */
15495       constructor_p = !cp_parser_error_occurred (parser);
15496     }
15497
15498   /* If we're still considering a constructor, we have to see a `(',
15499      to begin the parameter-declaration-clause, followed by either a
15500      `)', an `...', or a decl-specifier.  We need to check for a
15501      type-specifier to avoid being fooled into thinking that:
15502
15503        S::S (f) (int);
15504
15505      is a constructor.  (It is actually a function named `f' that
15506      takes one parameter (of type `int') and returns a value of type
15507      `S::S'.  */
15508   if (constructor_p
15509       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15510     {
15511       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15512           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15513           /* A parameter declaration begins with a decl-specifier,
15514              which is either the "attribute" keyword, a storage class
15515              specifier, or (usually) a type-specifier.  */
15516           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15517         {
15518           tree type;
15519           tree pushed_scope = NULL_TREE;
15520           unsigned saved_num_template_parameter_lists;
15521
15522           /* Names appearing in the type-specifier should be looked up
15523              in the scope of the class.  */
15524           if (current_class_type)
15525             type = NULL_TREE;
15526           else
15527             {
15528               type = TREE_TYPE (type_decl);
15529               if (TREE_CODE (type) == TYPENAME_TYPE)
15530                 {
15531                   type = resolve_typename_type (type,
15532                                                 /*only_current_p=*/false);
15533                   if (type == error_mark_node)
15534                     {
15535                       cp_parser_abort_tentative_parse (parser);
15536                       return false;
15537                     }
15538                 }
15539               pushed_scope = push_scope (type);
15540             }
15541
15542           /* Inside the constructor parameter list, surrounding
15543              template-parameter-lists do not apply.  */
15544           saved_num_template_parameter_lists
15545             = parser->num_template_parameter_lists;
15546           parser->num_template_parameter_lists = 0;
15547
15548           /* Look for the type-specifier.  */
15549           cp_parser_type_specifier (parser,
15550                                     CP_PARSER_FLAGS_NONE,
15551                                     /*decl_specs=*/NULL,
15552                                     /*is_declarator=*/true,
15553                                     /*declares_class_or_enum=*/NULL,
15554                                     /*is_cv_qualifier=*/NULL);
15555
15556           parser->num_template_parameter_lists
15557             = saved_num_template_parameter_lists;
15558
15559           /* Leave the scope of the class.  */
15560           if (pushed_scope)
15561             pop_scope (pushed_scope);
15562
15563           constructor_p = !cp_parser_error_occurred (parser);
15564         }
15565     }
15566   else
15567     constructor_p = false;
15568   /* We did not really want to consume any tokens.  */
15569   cp_parser_abort_tentative_parse (parser);
15570
15571   return constructor_p;
15572 }
15573
15574 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15575    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15576    they must be performed once we are in the scope of the function.
15577
15578    Returns the function defined.  */
15579
15580 static tree
15581 cp_parser_function_definition_from_specifiers_and_declarator
15582   (cp_parser* parser,
15583    cp_decl_specifier_seq *decl_specifiers,
15584    tree attributes,
15585    const cp_declarator *declarator)
15586 {
15587   tree fn;
15588   bool success_p;
15589
15590   /* Begin the function-definition.  */
15591   success_p = start_function (decl_specifiers, declarator, attributes);
15592
15593   /* The things we're about to see are not directly qualified by any
15594      template headers we've seen thus far.  */
15595   reset_specialization ();
15596
15597   /* If there were names looked up in the decl-specifier-seq that we
15598      did not check, check them now.  We must wait until we are in the
15599      scope of the function to perform the checks, since the function
15600      might be a friend.  */
15601   perform_deferred_access_checks ();
15602
15603   if (!success_p)
15604     {
15605       /* Skip the entire function.  */
15606       cp_parser_skip_to_end_of_block_or_statement (parser);
15607       fn = error_mark_node;
15608     }
15609   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
15610     {
15611       /* Seen already, skip it.  An error message has already been output.  */
15612       cp_parser_skip_to_end_of_block_or_statement (parser);
15613       fn = current_function_decl;
15614       current_function_decl = NULL_TREE;
15615       /* If this is a function from a class, pop the nested class.  */
15616       if (current_class_name)
15617         pop_nested_class ();
15618     }
15619   else
15620     fn = cp_parser_function_definition_after_declarator (parser,
15621                                                          /*inline_p=*/false);
15622
15623   return fn;
15624 }
15625
15626 /* Parse the part of a function-definition that follows the
15627    declarator.  INLINE_P is TRUE iff this function is an inline
15628    function defined with a class-specifier.
15629
15630    Returns the function defined.  */
15631
15632 static tree
15633 cp_parser_function_definition_after_declarator (cp_parser* parser,
15634                                                 bool inline_p)
15635 {
15636   tree fn;
15637   bool ctor_initializer_p = false;
15638   bool saved_in_unbraced_linkage_specification_p;
15639   bool saved_in_function_body;
15640   unsigned saved_num_template_parameter_lists;
15641
15642   saved_in_function_body = parser->in_function_body;
15643   parser->in_function_body = true;
15644   /* If the next token is `return', then the code may be trying to
15645      make use of the "named return value" extension that G++ used to
15646      support.  */
15647   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15648     {
15649       /* Consume the `return' keyword.  */
15650       cp_lexer_consume_token (parser->lexer);
15651       /* Look for the identifier that indicates what value is to be
15652          returned.  */
15653       cp_parser_identifier (parser);
15654       /* Issue an error message.  */
15655       error ("named return values are no longer supported");
15656       /* Skip tokens until we reach the start of the function body.  */
15657       while (true)
15658         {
15659           cp_token *token = cp_lexer_peek_token (parser->lexer);
15660           if (token->type == CPP_OPEN_BRACE
15661               || token->type == CPP_EOF
15662               || token->type == CPP_PRAGMA_EOL)
15663             break;
15664           cp_lexer_consume_token (parser->lexer);
15665         }
15666     }
15667   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15668      anything declared inside `f'.  */
15669   saved_in_unbraced_linkage_specification_p
15670     = parser->in_unbraced_linkage_specification_p;
15671   parser->in_unbraced_linkage_specification_p = false;
15672   /* Inside the function, surrounding template-parameter-lists do not
15673      apply.  */
15674   saved_num_template_parameter_lists
15675     = parser->num_template_parameter_lists;
15676   parser->num_template_parameter_lists = 0;
15677   /* If the next token is `try', then we are looking at a
15678      function-try-block.  */
15679   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15680     ctor_initializer_p = cp_parser_function_try_block (parser);
15681   /* A function-try-block includes the function-body, so we only do
15682      this next part if we're not processing a function-try-block.  */
15683   else
15684     ctor_initializer_p
15685       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15686
15687   /* Finish the function.  */
15688   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15689                         (inline_p ? 2 : 0));
15690   /* Generate code for it, if necessary.  */
15691   expand_or_defer_fn (fn);
15692   /* Restore the saved values.  */
15693   parser->in_unbraced_linkage_specification_p
15694     = saved_in_unbraced_linkage_specification_p;
15695   parser->num_template_parameter_lists
15696     = saved_num_template_parameter_lists;
15697   parser->in_function_body = saved_in_function_body;
15698
15699   return fn;
15700 }
15701
15702 /* Parse a template-declaration, assuming that the `export' (and
15703    `extern') keywords, if present, has already been scanned.  MEMBER_P
15704    is as for cp_parser_template_declaration.  */
15705
15706 static void
15707 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15708 {
15709   tree decl = NULL_TREE;
15710   tree checks;
15711   tree parameter_list;
15712   bool friend_p = false;
15713   bool need_lang_pop;
15714
15715   /* Look for the `template' keyword.  */
15716   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15717     return;
15718
15719   /* And the `<'.  */
15720   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15721     return;
15722   if (at_class_scope_p () && current_function_decl)
15723     {
15724       /* 14.5.2.2 [temp.mem]
15725
15726          A local class shall not have member templates.  */
15727       error ("invalid declaration of member template in local class");
15728       cp_parser_skip_to_end_of_block_or_statement (parser);
15729       return;
15730     }
15731   /* [temp]
15732
15733      A template ... shall not have C linkage.  */
15734   if (current_lang_name == lang_name_c)
15735     {
15736       error ("template with C linkage");
15737       /* Give it C++ linkage to avoid confusing other parts of the
15738          front end.  */
15739       push_lang_context (lang_name_cplusplus);
15740       need_lang_pop = true;
15741     }
15742   else
15743     need_lang_pop = false;
15744
15745   /* We cannot perform access checks on the template parameter
15746      declarations until we know what is being declared, just as we
15747      cannot check the decl-specifier list.  */
15748   push_deferring_access_checks (dk_deferred);
15749
15750   /* If the next token is `>', then we have an invalid
15751      specialization.  Rather than complain about an invalid template
15752      parameter, issue an error message here.  */
15753   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15754     {
15755       cp_parser_error (parser, "invalid explicit specialization");
15756       begin_specialization ();
15757       parameter_list = NULL_TREE;
15758     }
15759   else
15760     /* Parse the template parameters.  */
15761     parameter_list = cp_parser_template_parameter_list (parser);
15762
15763   /* Get the deferred access checks from the parameter list.  These
15764      will be checked once we know what is being declared, as for a
15765      member template the checks must be performed in the scope of the
15766      class containing the member.  */
15767   checks = get_deferred_access_checks ();
15768
15769   /* Look for the `>'.  */
15770   cp_parser_skip_to_end_of_template_parameter_list (parser);
15771   /* We just processed one more parameter list.  */
15772   ++parser->num_template_parameter_lists;
15773   /* If the next token is `template', there are more template
15774      parameters.  */
15775   if (cp_lexer_next_token_is_keyword (parser->lexer,
15776                                       RID_TEMPLATE))
15777     cp_parser_template_declaration_after_export (parser, member_p);
15778   else
15779     {
15780       /* There are no access checks when parsing a template, as we do not
15781          know if a specialization will be a friend.  */
15782       push_deferring_access_checks (dk_no_check);
15783       decl = cp_parser_single_declaration (parser,
15784                                            checks,
15785                                            member_p,
15786                                            &friend_p);
15787       pop_deferring_access_checks ();
15788
15789       /* If this is a member template declaration, let the front
15790          end know.  */
15791       if (member_p && !friend_p && decl)
15792         {
15793           if (TREE_CODE (decl) == TYPE_DECL)
15794             cp_parser_check_access_in_redeclaration (decl);
15795
15796           decl = finish_member_template_decl (decl);
15797         }
15798       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15799         make_friend_class (current_class_type, TREE_TYPE (decl),
15800                            /*complain=*/true);
15801     }
15802   /* We are done with the current parameter list.  */
15803   --parser->num_template_parameter_lists;
15804
15805   pop_deferring_access_checks ();
15806
15807   /* Finish up.  */
15808   finish_template_decl (parameter_list);
15809
15810   /* Register member declarations.  */
15811   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15812     finish_member_declaration (decl);
15813   /* For the erroneous case of a template with C linkage, we pushed an
15814      implicit C++ linkage scope; exit that scope now.  */
15815   if (need_lang_pop)
15816     pop_lang_context ();
15817   /* If DECL is a function template, we must return to parse it later.
15818      (Even though there is no definition, there might be default
15819      arguments that need handling.)  */
15820   if (member_p && decl
15821       && (TREE_CODE (decl) == FUNCTION_DECL
15822           || DECL_FUNCTION_TEMPLATE_P (decl)))
15823     TREE_VALUE (parser->unparsed_functions_queues)
15824       = tree_cons (NULL_TREE, decl,
15825                    TREE_VALUE (parser->unparsed_functions_queues));
15826 }
15827
15828 /* Perform the deferred access checks from a template-parameter-list.
15829    CHECKS is a TREE_LIST of access checks, as returned by
15830    get_deferred_access_checks.  */
15831
15832 static void
15833 cp_parser_perform_template_parameter_access_checks (tree checks)
15834 {
15835   ++processing_template_parmlist;
15836   perform_access_checks (checks);
15837   --processing_template_parmlist;
15838 }
15839
15840 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15841    `function-definition' sequence.  MEMBER_P is true, this declaration
15842    appears in a class scope.
15843
15844    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15845    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15846
15847 static tree
15848 cp_parser_single_declaration (cp_parser* parser,
15849                               tree checks,
15850                               bool member_p,
15851                               bool* friend_p)
15852 {
15853   int declares_class_or_enum;
15854   tree decl = NULL_TREE;
15855   cp_decl_specifier_seq decl_specifiers;
15856   bool function_definition_p = false;
15857
15858   /* This function is only used when processing a template
15859      declaration.  */
15860   gcc_assert (innermost_scope_kind () == sk_template_parms
15861               || innermost_scope_kind () == sk_template_spec);
15862
15863   /* Defer access checks until we know what is being declared.  */
15864   push_deferring_access_checks (dk_deferred);
15865
15866   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15867      alternative.  */
15868   cp_parser_decl_specifier_seq (parser,
15869                                 CP_PARSER_FLAGS_OPTIONAL,
15870                                 &decl_specifiers,
15871                                 &declares_class_or_enum);
15872   if (friend_p)
15873     *friend_p = cp_parser_friend_p (&decl_specifiers);
15874
15875   /* There are no template typedefs.  */
15876   if (decl_specifiers.specs[(int) ds_typedef])
15877     {
15878       error ("template declaration of %qs", "typedef");
15879       decl = error_mark_node;
15880     }
15881
15882   /* Gather up the access checks that occurred the
15883      decl-specifier-seq.  */
15884   stop_deferring_access_checks ();
15885
15886   /* Check for the declaration of a template class.  */
15887   if (declares_class_or_enum)
15888     {
15889       if (cp_parser_declares_only_class_p (parser))
15890         {
15891           decl = shadow_tag (&decl_specifiers);
15892
15893           /* In this case:
15894
15895                struct C {
15896                  friend template <typename T> struct A<T>::B;
15897                };
15898
15899              A<T>::B will be represented by a TYPENAME_TYPE, and
15900              therefore not recognized by shadow_tag.  */
15901           if (friend_p && *friend_p
15902               && !decl
15903               && decl_specifiers.type
15904               && TYPE_P (decl_specifiers.type))
15905             decl = decl_specifiers.type;
15906
15907           if (decl && decl != error_mark_node)
15908             decl = TYPE_NAME (decl);
15909           else
15910             decl = error_mark_node;
15911
15912           /* Perform access checks for template parameters.  */
15913           cp_parser_perform_template_parameter_access_checks (checks);
15914         }
15915     }
15916   /* If it's not a template class, try for a template function.  If
15917      the next token is a `;', then this declaration does not declare
15918      anything.  But, if there were errors in the decl-specifiers, then
15919      the error might well have come from an attempted class-specifier.
15920      In that case, there's no need to warn about a missing declarator.  */
15921   if (!decl
15922       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15923           || decl_specifiers.type != error_mark_node))
15924     decl = cp_parser_init_declarator (parser,
15925                                       &decl_specifiers,
15926                                       checks,
15927                                       /*function_definition_allowed_p=*/true,
15928                                       member_p,
15929                                       declares_class_or_enum,
15930                                       &function_definition_p);
15931
15932   pop_deferring_access_checks ();
15933
15934   /* Clear any current qualification; whatever comes next is the start
15935      of something new.  */
15936   parser->scope = NULL_TREE;
15937   parser->qualifying_scope = NULL_TREE;
15938   parser->object_scope = NULL_TREE;
15939   /* Look for a trailing `;' after the declaration.  */
15940   if (!function_definition_p
15941       && (decl == error_mark_node
15942           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15943     cp_parser_skip_to_end_of_block_or_statement (parser);
15944
15945   return decl;
15946 }
15947
15948 /* Parse a cast-expression that is not the operand of a unary "&".  */
15949
15950 static tree
15951 cp_parser_simple_cast_expression (cp_parser *parser)
15952 {
15953   return cp_parser_cast_expression (parser, /*address_p=*/false,
15954                                     /*cast_p=*/false);
15955 }
15956
15957 /* Parse a functional cast to TYPE.  Returns an expression
15958    representing the cast.  */
15959
15960 static tree
15961 cp_parser_functional_cast (cp_parser* parser, tree type)
15962 {
15963   tree expression_list;
15964   tree cast;
15965
15966   expression_list
15967     = cp_parser_parenthesized_expression_list (parser, false,
15968                                                /*cast_p=*/true,
15969                                                /*non_constant_p=*/NULL);
15970
15971   cast = build_functional_cast (type, expression_list);
15972   /* [expr.const]/1: In an integral constant expression "only type
15973      conversions to integral or enumeration type can be used".  */
15974   if (TREE_CODE (type) == TYPE_DECL)
15975     type = TREE_TYPE (type);
15976   if (cast != error_mark_node
15977       && !cast_valid_in_integral_constant_expression_p (type)
15978       && (cp_parser_non_integral_constant_expression
15979           (parser, "a call to a constructor")))
15980     return error_mark_node;
15981   return cast;
15982 }
15983
15984 /* Save the tokens that make up the body of a member function defined
15985    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15986    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15987    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15988    for the member function.  */
15989
15990 static tree
15991 cp_parser_save_member_function_body (cp_parser* parser,
15992                                      cp_decl_specifier_seq *decl_specifiers,
15993                                      cp_declarator *declarator,
15994                                      tree attributes)
15995 {
15996   cp_token *first;
15997   cp_token *last;
15998   tree fn;
15999
16000   /* Create the function-declaration.  */
16001   fn = start_method (decl_specifiers, declarator, attributes);
16002   /* If something went badly wrong, bail out now.  */
16003   if (fn == error_mark_node)
16004     {
16005       /* If there's a function-body, skip it.  */
16006       if (cp_parser_token_starts_function_definition_p
16007           (cp_lexer_peek_token (parser->lexer)))
16008         cp_parser_skip_to_end_of_block_or_statement (parser);
16009       return error_mark_node;
16010     }
16011
16012   /* Remember it, if there default args to post process.  */
16013   cp_parser_save_default_args (parser, fn);
16014
16015   /* Save away the tokens that make up the body of the
16016      function.  */
16017   first = parser->lexer->next_token;
16018   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16019   /* Handle function try blocks.  */
16020   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16021     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16022   last = parser->lexer->next_token;
16023
16024   /* Save away the inline definition; we will process it when the
16025      class is complete.  */
16026   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16027   DECL_PENDING_INLINE_P (fn) = 1;
16028
16029   /* We need to know that this was defined in the class, so that
16030      friend templates are handled correctly.  */
16031   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16032
16033   /* We're done with the inline definition.  */
16034   finish_method (fn);
16035
16036   /* Add FN to the queue of functions to be parsed later.  */
16037   TREE_VALUE (parser->unparsed_functions_queues)
16038     = tree_cons (NULL_TREE, fn,
16039                  TREE_VALUE (parser->unparsed_functions_queues));
16040
16041   return fn;
16042 }
16043
16044 /* Parse a template-argument-list, as well as the trailing ">" (but
16045    not the opening ">").  See cp_parser_template_argument_list for the
16046    return value.  */
16047
16048 static tree
16049 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16050 {
16051   tree arguments;
16052   tree saved_scope;
16053   tree saved_qualifying_scope;
16054   tree saved_object_scope;
16055   bool saved_greater_than_is_operator_p;
16056   bool saved_skip_evaluation;
16057
16058   /* [temp.names]
16059
16060      When parsing a template-id, the first non-nested `>' is taken as
16061      the end of the template-argument-list rather than a greater-than
16062      operator.  */
16063   saved_greater_than_is_operator_p
16064     = parser->greater_than_is_operator_p;
16065   parser->greater_than_is_operator_p = false;
16066   /* Parsing the argument list may modify SCOPE, so we save it
16067      here.  */
16068   saved_scope = parser->scope;
16069   saved_qualifying_scope = parser->qualifying_scope;
16070   saved_object_scope = parser->object_scope;
16071   /* We need to evaluate the template arguments, even though this
16072      template-id may be nested within a "sizeof".  */
16073   saved_skip_evaluation = skip_evaluation;
16074   skip_evaluation = false;
16075   /* Parse the template-argument-list itself.  */
16076   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16077     arguments = NULL_TREE;
16078   else
16079     arguments = cp_parser_template_argument_list (parser);
16080   /* Look for the `>' that ends the template-argument-list. If we find
16081      a '>>' instead, it's probably just a typo.  */
16082   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16083     {
16084       if (!saved_greater_than_is_operator_p)
16085         {
16086           /* If we're in a nested template argument list, the '>>' has
16087             to be a typo for '> >'. We emit the error message, but we
16088             continue parsing and we push a '>' as next token, so that
16089             the argument list will be parsed correctly.  Note that the
16090             global source location is still on the token before the
16091             '>>', so we need to say explicitly where we want it.  */
16092           cp_token *token = cp_lexer_peek_token (parser->lexer);
16093           error ("%H%<>>%> should be %<> >%> "
16094                  "within a nested template argument list",
16095                  &token->location);
16096
16097           /* ??? Proper recovery should terminate two levels of
16098              template argument list here.  */
16099           token->type = CPP_GREATER;
16100         }
16101       else
16102         {
16103           /* If this is not a nested template argument list, the '>>'
16104             is a typo for '>'. Emit an error message and continue.
16105             Same deal about the token location, but here we can get it
16106             right by consuming the '>>' before issuing the diagnostic.  */
16107           cp_lexer_consume_token (parser->lexer);
16108           error ("spurious %<>>%>, use %<>%> to terminate "
16109                  "a template argument list");
16110         }
16111     }
16112   else
16113     cp_parser_skip_to_end_of_template_parameter_list (parser);
16114   /* The `>' token might be a greater-than operator again now.  */
16115   parser->greater_than_is_operator_p
16116     = saved_greater_than_is_operator_p;
16117   /* Restore the SAVED_SCOPE.  */
16118   parser->scope = saved_scope;
16119   parser->qualifying_scope = saved_qualifying_scope;
16120   parser->object_scope = saved_object_scope;
16121   skip_evaluation = saved_skip_evaluation;
16122
16123   return arguments;
16124 }
16125
16126 /* MEMBER_FUNCTION is a member function, or a friend.  If default
16127    arguments, or the body of the function have not yet been parsed,
16128    parse them now.  */
16129
16130 static void
16131 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16132 {
16133   /* If this member is a template, get the underlying
16134      FUNCTION_DECL.  */
16135   if (DECL_FUNCTION_TEMPLATE_P (member_function))
16136     member_function = DECL_TEMPLATE_RESULT (member_function);
16137
16138   /* There should not be any class definitions in progress at this
16139      point; the bodies of members are only parsed outside of all class
16140      definitions.  */
16141   gcc_assert (parser->num_classes_being_defined == 0);
16142   /* While we're parsing the member functions we might encounter more
16143      classes.  We want to handle them right away, but we don't want
16144      them getting mixed up with functions that are currently in the
16145      queue.  */
16146   parser->unparsed_functions_queues
16147     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16148
16149   /* Make sure that any template parameters are in scope.  */
16150   maybe_begin_member_template_processing (member_function);
16151
16152   /* If the body of the function has not yet been parsed, parse it
16153      now.  */
16154   if (DECL_PENDING_INLINE_P (member_function))
16155     {
16156       tree function_scope;
16157       cp_token_cache *tokens;
16158
16159       /* The function is no longer pending; we are processing it.  */
16160       tokens = DECL_PENDING_INLINE_INFO (member_function);
16161       DECL_PENDING_INLINE_INFO (member_function) = NULL;
16162       DECL_PENDING_INLINE_P (member_function) = 0;
16163
16164       /* If this is a local class, enter the scope of the containing
16165          function.  */
16166       function_scope = current_function_decl;
16167       if (function_scope)
16168         push_function_context_to (function_scope);
16169
16170
16171       /* Push the body of the function onto the lexer stack.  */
16172       cp_parser_push_lexer_for_tokens (parser, tokens);
16173
16174       /* Let the front end know that we going to be defining this
16175          function.  */
16176       start_preparsed_function (member_function, NULL_TREE,
16177                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
16178
16179       /* Don't do access checking if it is a templated function.  */
16180       if (processing_template_decl)
16181         push_deferring_access_checks (dk_no_check);
16182
16183       /* Now, parse the body of the function.  */
16184       cp_parser_function_definition_after_declarator (parser,
16185                                                       /*inline_p=*/true);
16186
16187       if (processing_template_decl)
16188         pop_deferring_access_checks ();
16189
16190       /* Leave the scope of the containing function.  */
16191       if (function_scope)
16192         pop_function_context_from (function_scope);
16193       cp_parser_pop_lexer (parser);
16194     }
16195
16196   /* Remove any template parameters from the symbol table.  */
16197   maybe_end_member_template_processing ();
16198
16199   /* Restore the queue.  */
16200   parser->unparsed_functions_queues
16201     = TREE_CHAIN (parser->unparsed_functions_queues);
16202 }
16203
16204 /* If DECL contains any default args, remember it on the unparsed
16205    functions queue.  */
16206
16207 static void
16208 cp_parser_save_default_args (cp_parser* parser, tree decl)
16209 {
16210   tree probe;
16211
16212   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16213        probe;
16214        probe = TREE_CHAIN (probe))
16215     if (TREE_PURPOSE (probe))
16216       {
16217         TREE_PURPOSE (parser->unparsed_functions_queues)
16218           = tree_cons (current_class_type, decl,
16219                        TREE_PURPOSE (parser->unparsed_functions_queues));
16220         break;
16221       }
16222 }
16223
16224 /* FN is a FUNCTION_DECL which may contains a parameter with an
16225    unparsed DEFAULT_ARG.  Parse the default args now.  This function
16226    assumes that the current scope is the scope in which the default
16227    argument should be processed.  */
16228
16229 static void
16230 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16231 {
16232   bool saved_local_variables_forbidden_p;
16233   tree parm;
16234
16235   /* While we're parsing the default args, we might (due to the
16236      statement expression extension) encounter more classes.  We want
16237      to handle them right away, but we don't want them getting mixed
16238      up with default args that are currently in the queue.  */
16239   parser->unparsed_functions_queues
16240     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16241
16242   /* Local variable names (and the `this' keyword) may not appear
16243      in a default argument.  */
16244   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16245   parser->local_variables_forbidden_p = true;
16246
16247   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16248        parm;
16249        parm = TREE_CHAIN (parm))
16250     {
16251       cp_token_cache *tokens;
16252       tree default_arg = TREE_PURPOSE (parm);
16253       tree parsed_arg;
16254       VEC(tree,gc) *insts;
16255       tree copy;
16256       unsigned ix;
16257
16258       if (!default_arg)
16259         continue;
16260
16261       if (TREE_CODE (default_arg) != DEFAULT_ARG)
16262         /* This can happen for a friend declaration for a function
16263            already declared with default arguments.  */
16264         continue;
16265
16266        /* Push the saved tokens for the default argument onto the parser's
16267           lexer stack.  */
16268       tokens = DEFARG_TOKENS (default_arg);
16269       cp_parser_push_lexer_for_tokens (parser, tokens);
16270
16271       /* Parse the assignment-expression.  */
16272       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16273
16274       if (!processing_template_decl)
16275         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16276
16277       TREE_PURPOSE (parm) = parsed_arg;
16278
16279       /* Update any instantiations we've already created.  */
16280       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16281            VEC_iterate (tree, insts, ix, copy); ix++)
16282         TREE_PURPOSE (copy) = parsed_arg;
16283
16284       /* If the token stream has not been completely used up, then
16285          there was extra junk after the end of the default
16286          argument.  */
16287       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16288         cp_parser_error (parser, "expected %<,%>");
16289
16290       /* Revert to the main lexer.  */
16291       cp_parser_pop_lexer (parser);
16292     }
16293
16294   /* Make sure no default arg is missing.  */
16295   check_default_args (fn);
16296
16297   /* Restore the state of local_variables_forbidden_p.  */
16298   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16299
16300   /* Restore the queue.  */
16301   parser->unparsed_functions_queues
16302     = TREE_CHAIN (parser->unparsed_functions_queues);
16303 }
16304
16305 /* Parse the operand of `sizeof' (or a similar operator).  Returns
16306    either a TYPE or an expression, depending on the form of the
16307    input.  The KEYWORD indicates which kind of expression we have
16308    encountered.  */
16309
16310 static tree
16311 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16312 {
16313   static const char *format;
16314   tree expr = NULL_TREE;
16315   const char *saved_message;
16316   bool saved_integral_constant_expression_p;
16317   bool saved_non_integral_constant_expression_p;
16318
16319   /* Initialize FORMAT the first time we get here.  */
16320   if (!format)
16321     format = "types may not be defined in '%s' expressions";
16322
16323   /* Types cannot be defined in a `sizeof' expression.  Save away the
16324      old message.  */
16325   saved_message = parser->type_definition_forbidden_message;
16326   /* And create the new one.  */
16327   parser->type_definition_forbidden_message
16328     = XNEWVEC (const char, strlen (format)
16329                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16330                + 1 /* `\0' */);
16331   sprintf ((char *) parser->type_definition_forbidden_message,
16332            format, IDENTIFIER_POINTER (ridpointers[keyword]));
16333
16334   /* The restrictions on constant-expressions do not apply inside
16335      sizeof expressions.  */
16336   saved_integral_constant_expression_p
16337     = parser->integral_constant_expression_p;
16338   saved_non_integral_constant_expression_p
16339     = parser->non_integral_constant_expression_p;
16340   parser->integral_constant_expression_p = false;
16341
16342   /* Do not actually evaluate the expression.  */
16343   ++skip_evaluation;
16344   /* If it's a `(', then we might be looking at the type-id
16345      construction.  */
16346   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16347     {
16348       tree type;
16349       bool saved_in_type_id_in_expr_p;
16350
16351       /* We can't be sure yet whether we're looking at a type-id or an
16352          expression.  */
16353       cp_parser_parse_tentatively (parser);
16354       /* Consume the `('.  */
16355       cp_lexer_consume_token (parser->lexer);
16356       /* Parse the type-id.  */
16357       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16358       parser->in_type_id_in_expr_p = true;
16359       type = cp_parser_type_id (parser);
16360       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16361       /* Now, look for the trailing `)'.  */
16362       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16363       /* If all went well, then we're done.  */
16364       if (cp_parser_parse_definitely (parser))
16365         {
16366           cp_decl_specifier_seq decl_specs;
16367
16368           /* Build a trivial decl-specifier-seq.  */
16369           clear_decl_specs (&decl_specs);
16370           decl_specs.type = type;
16371
16372           /* Call grokdeclarator to figure out what type this is.  */
16373           expr = grokdeclarator (NULL,
16374                                  &decl_specs,
16375                                  TYPENAME,
16376                                  /*initialized=*/0,
16377                                  /*attrlist=*/NULL);
16378         }
16379     }
16380
16381   /* If the type-id production did not work out, then we must be
16382      looking at the unary-expression production.  */
16383   if (!expr)
16384     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16385                                        /*cast_p=*/false);
16386   /* Go back to evaluating expressions.  */
16387   --skip_evaluation;
16388
16389   /* Free the message we created.  */
16390   free ((char *) parser->type_definition_forbidden_message);
16391   /* And restore the old one.  */
16392   parser->type_definition_forbidden_message = saved_message;
16393   parser->integral_constant_expression_p
16394     = saved_integral_constant_expression_p;
16395   parser->non_integral_constant_expression_p
16396     = saved_non_integral_constant_expression_p;
16397
16398   return expr;
16399 }
16400
16401 /* If the current declaration has no declarator, return true.  */
16402
16403 static bool
16404 cp_parser_declares_only_class_p (cp_parser *parser)
16405 {
16406   /* If the next token is a `;' or a `,' then there is no
16407      declarator.  */
16408   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16409           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16410 }
16411
16412 /* Update the DECL_SPECS to reflect the storage class indicated by
16413    KEYWORD.  */
16414
16415 static void
16416 cp_parser_set_storage_class (cp_parser *parser,
16417                              cp_decl_specifier_seq *decl_specs,
16418                              enum rid keyword)
16419 {
16420   cp_storage_class storage_class;
16421
16422   if (parser->in_unbraced_linkage_specification_p)
16423     {
16424       error ("invalid use of %qD in linkage specification",
16425              ridpointers[keyword]);
16426       return;
16427     }
16428   else if (decl_specs->storage_class != sc_none)
16429     {
16430       decl_specs->conflicting_specifiers_p = true;
16431       return;
16432     }
16433
16434   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16435       && decl_specs->specs[(int) ds_thread])
16436     {
16437       error ("%<__thread%> before %qD", ridpointers[keyword]);
16438       decl_specs->specs[(int) ds_thread] = 0;
16439     }
16440
16441   switch (keyword)
16442     {
16443     case RID_AUTO:
16444       storage_class = sc_auto;
16445       break;
16446     case RID_REGISTER:
16447       storage_class = sc_register;
16448       break;
16449     case RID_STATIC:
16450       storage_class = sc_static;
16451       break;
16452     case RID_EXTERN:
16453       storage_class = sc_extern;
16454       break;
16455     case RID_MUTABLE:
16456       storage_class = sc_mutable;
16457       break;
16458     default:
16459       gcc_unreachable ();
16460     }
16461   decl_specs->storage_class = storage_class;
16462
16463   /* A storage class specifier cannot be applied alongside a typedef 
16464      specifier. If there is a typedef specifier present then set 
16465      conflicting_specifiers_p which will trigger an error later
16466      on in grokdeclarator. */
16467   if (decl_specs->specs[(int)ds_typedef])
16468     decl_specs->conflicting_specifiers_p = true;
16469 }
16470
16471 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16472    is true, the type is a user-defined type; otherwise it is a
16473    built-in type specified by a keyword.  */
16474
16475 static void
16476 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16477                               tree type_spec,
16478                               bool user_defined_p)
16479 {
16480   decl_specs->any_specifiers_p = true;
16481
16482   /* If the user tries to redeclare bool or wchar_t (with, for
16483      example, in "typedef int wchar_t;") we remember that this is what
16484      happened.  In system headers, we ignore these declarations so
16485      that G++ can work with system headers that are not C++-safe.  */
16486   if (decl_specs->specs[(int) ds_typedef]
16487       && !user_defined_p
16488       && (type_spec == boolean_type_node
16489           || type_spec == wchar_type_node)
16490       && (decl_specs->type
16491           || decl_specs->specs[(int) ds_long]
16492           || decl_specs->specs[(int) ds_short]
16493           || decl_specs->specs[(int) ds_unsigned]
16494           || decl_specs->specs[(int) ds_signed]))
16495     {
16496       decl_specs->redefined_builtin_type = type_spec;
16497       if (!decl_specs->type)
16498         {
16499           decl_specs->type = type_spec;
16500           decl_specs->user_defined_type_p = false;
16501         }
16502     }
16503   else if (decl_specs->type)
16504     decl_specs->multiple_types_p = true;
16505   else
16506     {
16507       decl_specs->type = type_spec;
16508       decl_specs->user_defined_type_p = user_defined_p;
16509       decl_specs->redefined_builtin_type = NULL_TREE;
16510     }
16511 }
16512
16513 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16514    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16515
16516 static bool
16517 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16518 {
16519   return decl_specifiers->specs[(int) ds_friend] != 0;
16520 }
16521
16522 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16523    issue an error message indicating that TOKEN_DESC was expected.
16524
16525    Returns the token consumed, if the token had the appropriate type.
16526    Otherwise, returns NULL.  */
16527
16528 static cp_token *
16529 cp_parser_require (cp_parser* parser,
16530                    enum cpp_ttype type,
16531                    const char* token_desc)
16532 {
16533   if (cp_lexer_next_token_is (parser->lexer, type))
16534     return cp_lexer_consume_token (parser->lexer);
16535   else
16536     {
16537       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16538       if (!cp_parser_simulate_error (parser))
16539         {
16540           char *message = concat ("expected ", token_desc, NULL);
16541           cp_parser_error (parser, message);
16542           free (message);
16543         }
16544       return NULL;
16545     }
16546 }
16547
16548 /* An error message is produced if the next token is not '>'.
16549    All further tokens are skipped until the desired token is
16550    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
16551
16552 static void
16553 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16554 {
16555   /* Current level of '< ... >'.  */
16556   unsigned level = 0;
16557   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
16558   unsigned nesting_depth = 0;
16559
16560   /* Are we ready, yet?  If not, issue error message.  */
16561   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16562     return;
16563
16564   /* Skip tokens until the desired token is found.  */
16565   while (true)
16566     {
16567       /* Peek at the next token.  */
16568       switch (cp_lexer_peek_token (parser->lexer)->type)
16569         {
16570         case CPP_LESS:
16571           if (!nesting_depth)
16572             ++level;
16573           break;
16574
16575         case CPP_GREATER:
16576           if (!nesting_depth && level-- == 0)
16577             {
16578               /* We've reached the token we want, consume it and stop.  */
16579               cp_lexer_consume_token (parser->lexer);
16580               return;
16581             }
16582           break;
16583
16584         case CPP_OPEN_PAREN:
16585         case CPP_OPEN_SQUARE:
16586           ++nesting_depth;
16587           break;
16588
16589         case CPP_CLOSE_PAREN:
16590         case CPP_CLOSE_SQUARE:
16591           if (nesting_depth-- == 0)
16592             return;
16593           break;
16594
16595         case CPP_EOF:
16596         case CPP_PRAGMA_EOL:
16597         case CPP_SEMICOLON:
16598         case CPP_OPEN_BRACE:
16599         case CPP_CLOSE_BRACE:
16600           /* The '>' was probably forgotten, don't look further.  */
16601           return;
16602
16603         default:
16604           break;
16605         }
16606
16607       /* Consume this token.  */
16608       cp_lexer_consume_token (parser->lexer);
16609     }
16610 }
16611
16612 /* If the next token is the indicated keyword, consume it.  Otherwise,
16613    issue an error message indicating that TOKEN_DESC was expected.
16614
16615    Returns the token consumed, if the token had the appropriate type.
16616    Otherwise, returns NULL.  */
16617
16618 static cp_token *
16619 cp_parser_require_keyword (cp_parser* parser,
16620                            enum rid keyword,
16621                            const char* token_desc)
16622 {
16623   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16624
16625   if (token && token->keyword != keyword)
16626     {
16627       dyn_string_t error_msg;
16628
16629       /* Format the error message.  */
16630       error_msg = dyn_string_new (0);
16631       dyn_string_append_cstr (error_msg, "expected ");
16632       dyn_string_append_cstr (error_msg, token_desc);
16633       cp_parser_error (parser, error_msg->s);
16634       dyn_string_delete (error_msg);
16635       return NULL;
16636     }
16637
16638   return token;
16639 }
16640
16641 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16642    function-definition.  */
16643
16644 static bool
16645 cp_parser_token_starts_function_definition_p (cp_token* token)
16646 {
16647   return (/* An ordinary function-body begins with an `{'.  */
16648           token->type == CPP_OPEN_BRACE
16649           /* A ctor-initializer begins with a `:'.  */
16650           || token->type == CPP_COLON
16651           /* A function-try-block begins with `try'.  */
16652           || token->keyword == RID_TRY
16653           /* The named return value extension begins with `return'.  */
16654           || token->keyword == RID_RETURN);
16655 }
16656
16657 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16658    definition.  */
16659
16660 static bool
16661 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16662 {
16663   cp_token *token;
16664
16665   token = cp_lexer_peek_token (parser->lexer);
16666   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16667 }
16668
16669 /* Returns TRUE iff the next token is the "," or ">" ending a
16670    template-argument.  */
16671
16672 static bool
16673 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16674 {
16675   cp_token *token;
16676
16677   token = cp_lexer_peek_token (parser->lexer);
16678   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16679 }
16680
16681 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16682    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16683
16684 static bool
16685 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16686                                                      size_t n)
16687 {
16688   cp_token *token;
16689
16690   token = cp_lexer_peek_nth_token (parser->lexer, n);
16691   if (token->type == CPP_LESS)
16692     return true;
16693   /* Check for the sequence `<::' in the original code. It would be lexed as
16694      `[:', where `[' is a digraph, and there is no whitespace before
16695      `:'.  */
16696   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16697     {
16698       cp_token *token2;
16699       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16700       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16701         return true;
16702     }
16703   return false;
16704 }
16705
16706 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16707    or none_type otherwise.  */
16708
16709 static enum tag_types
16710 cp_parser_token_is_class_key (cp_token* token)
16711 {
16712   switch (token->keyword)
16713     {
16714     case RID_CLASS:
16715       return class_type;
16716     case RID_STRUCT:
16717       return record_type;
16718     case RID_UNION:
16719       return union_type;
16720
16721     default:
16722       return none_type;
16723     }
16724 }
16725
16726 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16727
16728 static void
16729 cp_parser_check_class_key (enum tag_types class_key, tree type)
16730 {
16731   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16732     pedwarn ("%qs tag used in naming %q#T",
16733             class_key == union_type ? "union"
16734              : class_key == record_type ? "struct" : "class",
16735              type);
16736 }
16737
16738 /* Issue an error message if DECL is redeclared with different
16739    access than its original declaration [class.access.spec/3].
16740    This applies to nested classes and nested class templates.
16741    [class.mem/1].  */
16742
16743 static void
16744 cp_parser_check_access_in_redeclaration (tree decl)
16745 {
16746   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16747     return;
16748
16749   if ((TREE_PRIVATE (decl)
16750        != (current_access_specifier == access_private_node))
16751       || (TREE_PROTECTED (decl)
16752           != (current_access_specifier == access_protected_node)))
16753     error ("%qD redeclared with different access", decl);
16754 }
16755
16756 /* Look for the `template' keyword, as a syntactic disambiguator.
16757    Return TRUE iff it is present, in which case it will be
16758    consumed.  */
16759
16760 static bool
16761 cp_parser_optional_template_keyword (cp_parser *parser)
16762 {
16763   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16764     {
16765       /* The `template' keyword can only be used within templates;
16766          outside templates the parser can always figure out what is a
16767          template and what is not.  */
16768       if (!processing_template_decl)
16769         {
16770           error ("%<template%> (as a disambiguator) is only allowed "
16771                  "within templates");
16772           /* If this part of the token stream is rescanned, the same
16773              error message would be generated.  So, we purge the token
16774              from the stream.  */
16775           cp_lexer_purge_token (parser->lexer);
16776           return false;
16777         }
16778       else
16779         {
16780           /* Consume the `template' keyword.  */
16781           cp_lexer_consume_token (parser->lexer);
16782           return true;
16783         }
16784     }
16785
16786   return false;
16787 }
16788
16789 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16790    set PARSER->SCOPE, and perform other related actions.  */
16791
16792 static void
16793 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16794 {
16795   tree value;
16796   tree check;
16797
16798   /* Get the stored value.  */
16799   value = cp_lexer_consume_token (parser->lexer)->value;
16800   /* Perform any access checks that were deferred.  */
16801   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16802     perform_or_defer_access_check (TREE_PURPOSE (check),
16803                                    TREE_VALUE (check),
16804                                    TREE_VALUE (check));
16805   /* Set the scope from the stored value.  */
16806   parser->scope = TREE_VALUE (value);
16807   parser->qualifying_scope = TREE_TYPE (value);
16808   parser->object_scope = NULL_TREE;
16809 }
16810
16811 /* Consume tokens up through a non-nested END token.  */
16812
16813 static void
16814 cp_parser_cache_group (cp_parser *parser,
16815                        enum cpp_ttype end,
16816                        unsigned depth)
16817 {
16818   while (true)
16819     {
16820       cp_token *token;
16821
16822       /* Abort a parenthesized expression if we encounter a brace.  */
16823       if ((end == CPP_CLOSE_PAREN || depth == 0)
16824           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16825         return;
16826       /* If we've reached the end of the file, stop.  */
16827       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16828           || (end != CPP_PRAGMA_EOL
16829               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16830         return;
16831       /* Consume the next token.  */
16832       token = cp_lexer_consume_token (parser->lexer);
16833       /* See if it starts a new group.  */
16834       if (token->type == CPP_OPEN_BRACE)
16835         {
16836           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16837           if (depth == 0)
16838             return;
16839         }
16840       else if (token->type == CPP_OPEN_PAREN)
16841         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16842       else if (token->type == CPP_PRAGMA)
16843         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16844       else if (token->type == end)
16845         return;
16846     }
16847 }
16848
16849 /* Begin parsing tentatively.  We always save tokens while parsing
16850    tentatively so that if the tentative parsing fails we can restore the
16851    tokens.  */
16852
16853 static void
16854 cp_parser_parse_tentatively (cp_parser* parser)
16855 {
16856   /* Enter a new parsing context.  */
16857   parser->context = cp_parser_context_new (parser->context);
16858   /* Begin saving tokens.  */
16859   cp_lexer_save_tokens (parser->lexer);
16860   /* In order to avoid repetitive access control error messages,
16861      access checks are queued up until we are no longer parsing
16862      tentatively.  */
16863   push_deferring_access_checks (dk_deferred);
16864 }
16865
16866 /* Commit to the currently active tentative parse.  */
16867
16868 static void
16869 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16870 {
16871   cp_parser_context *context;
16872   cp_lexer *lexer;
16873
16874   /* Mark all of the levels as committed.  */
16875   lexer = parser->lexer;
16876   for (context = parser->context; context->next; context = context->next)
16877     {
16878       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16879         break;
16880       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16881       while (!cp_lexer_saving_tokens (lexer))
16882         lexer = lexer->next;
16883       cp_lexer_commit_tokens (lexer);
16884     }
16885 }
16886
16887 /* Abort the currently active tentative parse.  All consumed tokens
16888    will be rolled back, and no diagnostics will be issued.  */
16889
16890 static void
16891 cp_parser_abort_tentative_parse (cp_parser* parser)
16892 {
16893   cp_parser_simulate_error (parser);
16894   /* Now, pretend that we want to see if the construct was
16895      successfully parsed.  */
16896   cp_parser_parse_definitely (parser);
16897 }
16898
16899 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16900    token stream.  Otherwise, commit to the tokens we have consumed.
16901    Returns true if no error occurred; false otherwise.  */
16902
16903 static bool
16904 cp_parser_parse_definitely (cp_parser* parser)
16905 {
16906   bool error_occurred;
16907   cp_parser_context *context;
16908
16909   /* Remember whether or not an error occurred, since we are about to
16910      destroy that information.  */
16911   error_occurred = cp_parser_error_occurred (parser);
16912   /* Remove the topmost context from the stack.  */
16913   context = parser->context;
16914   parser->context = context->next;
16915   /* If no parse errors occurred, commit to the tentative parse.  */
16916   if (!error_occurred)
16917     {
16918       /* Commit to the tokens read tentatively, unless that was
16919          already done.  */
16920       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16921         cp_lexer_commit_tokens (parser->lexer);
16922
16923       pop_to_parent_deferring_access_checks ();
16924     }
16925   /* Otherwise, if errors occurred, roll back our state so that things
16926      are just as they were before we began the tentative parse.  */
16927   else
16928     {
16929       cp_lexer_rollback_tokens (parser->lexer);
16930       pop_deferring_access_checks ();
16931     }
16932   /* Add the context to the front of the free list.  */
16933   context->next = cp_parser_context_free_list;
16934   cp_parser_context_free_list = context;
16935
16936   return !error_occurred;
16937 }
16938
16939 /* Returns true if we are parsing tentatively and are not committed to
16940    this tentative parse.  */
16941
16942 static bool
16943 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16944 {
16945   return (cp_parser_parsing_tentatively (parser)
16946           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16947 }
16948
16949 /* Returns nonzero iff an error has occurred during the most recent
16950    tentative parse.  */
16951
16952 static bool
16953 cp_parser_error_occurred (cp_parser* parser)
16954 {
16955   return (cp_parser_parsing_tentatively (parser)
16956           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16957 }
16958
16959 /* Returns nonzero if GNU extensions are allowed.  */
16960
16961 static bool
16962 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16963 {
16964   return parser->allow_gnu_extensions_p;
16965 }
16966 \f
16967 /* Objective-C++ Productions */
16968
16969
16970 /* Parse an Objective-C expression, which feeds into a primary-expression
16971    above.
16972
16973    objc-expression:
16974      objc-message-expression
16975      objc-string-literal
16976      objc-encode-expression
16977      objc-protocol-expression
16978      objc-selector-expression
16979
16980   Returns a tree representation of the expression.  */
16981
16982 static tree
16983 cp_parser_objc_expression (cp_parser* parser)
16984 {
16985   /* Try to figure out what kind of declaration is present.  */
16986   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16987
16988   switch (kwd->type)
16989     {
16990     case CPP_OPEN_SQUARE:
16991       return cp_parser_objc_message_expression (parser);
16992
16993     case CPP_OBJC_STRING:
16994       kwd = cp_lexer_consume_token (parser->lexer);
16995       return objc_build_string_object (kwd->value);
16996
16997     case CPP_KEYWORD:
16998       switch (kwd->keyword)
16999         {
17000         case RID_AT_ENCODE:
17001           return cp_parser_objc_encode_expression (parser);
17002
17003         case RID_AT_PROTOCOL:
17004           return cp_parser_objc_protocol_expression (parser);
17005
17006         case RID_AT_SELECTOR:
17007           return cp_parser_objc_selector_expression (parser);
17008
17009         default:
17010           break;
17011         }
17012     default:
17013       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17014       cp_parser_skip_to_end_of_block_or_statement (parser);
17015     }
17016
17017   return error_mark_node;
17018 }
17019
17020 /* Parse an Objective-C message expression.
17021
17022    objc-message-expression:
17023      [ objc-message-receiver objc-message-args ]
17024
17025    Returns a representation of an Objective-C message.  */
17026
17027 static tree
17028 cp_parser_objc_message_expression (cp_parser* parser)
17029 {
17030   tree receiver, messageargs;
17031
17032   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
17033   receiver = cp_parser_objc_message_receiver (parser);
17034   messageargs = cp_parser_objc_message_args (parser);
17035   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17036
17037   return objc_build_message_expr (build_tree_list (receiver, messageargs));
17038 }
17039
17040 /* Parse an objc-message-receiver.
17041
17042    objc-message-receiver:
17043      expression
17044      simple-type-specifier
17045
17046   Returns a representation of the type or expression.  */
17047
17048 static tree
17049 cp_parser_objc_message_receiver (cp_parser* parser)
17050 {
17051   tree rcv;
17052
17053   /* An Objective-C message receiver may be either (1) a type
17054      or (2) an expression.  */
17055   cp_parser_parse_tentatively (parser);
17056   rcv = cp_parser_expression (parser, false);
17057
17058   if (cp_parser_parse_definitely (parser))
17059     return rcv;
17060
17061   rcv = cp_parser_simple_type_specifier (parser,
17062                                          /*decl_specs=*/NULL,
17063                                          CP_PARSER_FLAGS_NONE);
17064
17065   return objc_get_class_reference (rcv);
17066 }
17067
17068 /* Parse the arguments and selectors comprising an Objective-C message.
17069
17070    objc-message-args:
17071      objc-selector
17072      objc-selector-args
17073      objc-selector-args , objc-comma-args
17074
17075    objc-selector-args:
17076      objc-selector [opt] : assignment-expression
17077      objc-selector-args objc-selector [opt] : assignment-expression
17078
17079    objc-comma-args:
17080      assignment-expression
17081      objc-comma-args , assignment-expression
17082
17083    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17084    selector arguments and TREE_VALUE containing a list of comma
17085    arguments.  */
17086
17087 static tree
17088 cp_parser_objc_message_args (cp_parser* parser)
17089 {
17090   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17091   bool maybe_unary_selector_p = true;
17092   cp_token *token = cp_lexer_peek_token (parser->lexer);
17093
17094   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17095     {
17096       tree selector = NULL_TREE, arg;
17097
17098       if (token->type != CPP_COLON)
17099         selector = cp_parser_objc_selector (parser);
17100
17101       /* Detect if we have a unary selector.  */
17102       if (maybe_unary_selector_p
17103           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17104         return build_tree_list (selector, NULL_TREE);
17105
17106       maybe_unary_selector_p = false;
17107       cp_parser_require (parser, CPP_COLON, "`:'");
17108       arg = cp_parser_assignment_expression (parser, false);
17109
17110       sel_args
17111         = chainon (sel_args,
17112                    build_tree_list (selector, arg));
17113
17114       token = cp_lexer_peek_token (parser->lexer);
17115     }
17116
17117   /* Handle non-selector arguments, if any. */
17118   while (token->type == CPP_COMMA)
17119     {
17120       tree arg;
17121
17122       cp_lexer_consume_token (parser->lexer);
17123       arg = cp_parser_assignment_expression (parser, false);
17124
17125       addl_args
17126         = chainon (addl_args,
17127                    build_tree_list (NULL_TREE, arg));
17128
17129       token = cp_lexer_peek_token (parser->lexer);
17130     }
17131
17132   return build_tree_list (sel_args, addl_args);
17133 }
17134
17135 /* Parse an Objective-C encode expression.
17136
17137    objc-encode-expression:
17138      @encode objc-typename
17139
17140    Returns an encoded representation of the type argument.  */
17141
17142 static tree
17143 cp_parser_objc_encode_expression (cp_parser* parser)
17144 {
17145   tree type;
17146
17147   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
17148   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17149   type = complete_type (cp_parser_type_id (parser));
17150   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17151
17152   if (!type)
17153     {
17154       error ("%<@encode%> must specify a type as an argument");
17155       return error_mark_node;
17156     }
17157
17158   return objc_build_encode_expr (type);
17159 }
17160
17161 /* Parse an Objective-C @defs expression.  */
17162
17163 static tree
17164 cp_parser_objc_defs_expression (cp_parser *parser)
17165 {
17166   tree name;
17167
17168   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
17169   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17170   name = cp_parser_identifier (parser);
17171   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17172
17173   return objc_get_class_ivars (name);
17174 }
17175
17176 /* Parse an Objective-C protocol expression.
17177
17178   objc-protocol-expression:
17179     @protocol ( identifier )
17180
17181   Returns a representation of the protocol expression.  */
17182
17183 static tree
17184 cp_parser_objc_protocol_expression (cp_parser* parser)
17185 {
17186   tree proto;
17187
17188   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17189   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17190   proto = cp_parser_identifier (parser);
17191   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17192
17193   return objc_build_protocol_expr (proto);
17194 }
17195
17196 /* Parse an Objective-C selector expression.
17197
17198    objc-selector-expression:
17199      @selector ( objc-method-signature )
17200
17201    objc-method-signature:
17202      objc-selector
17203      objc-selector-seq
17204
17205    objc-selector-seq:
17206      objc-selector :
17207      objc-selector-seq objc-selector :
17208
17209   Returns a representation of the method selector.  */
17210
17211 static tree
17212 cp_parser_objc_selector_expression (cp_parser* parser)
17213 {
17214   tree sel_seq = NULL_TREE;
17215   bool maybe_unary_selector_p = true;
17216   cp_token *token;
17217
17218   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
17219   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17220   token = cp_lexer_peek_token (parser->lexer);
17221
17222   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17223          || token->type == CPP_SCOPE)
17224     {
17225       tree selector = NULL_TREE;
17226
17227       if (token->type != CPP_COLON
17228           || token->type == CPP_SCOPE)
17229         selector = cp_parser_objc_selector (parser);
17230
17231       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17232           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17233         {
17234           /* Detect if we have a unary selector.  */
17235           if (maybe_unary_selector_p)
17236             {
17237               sel_seq = selector;
17238               goto finish_selector;
17239             }
17240           else
17241             {
17242               cp_parser_error (parser, "expected %<:%>");
17243             }
17244         }
17245       maybe_unary_selector_p = false;
17246       token = cp_lexer_consume_token (parser->lexer);
17247
17248       if (token->type == CPP_SCOPE)
17249         {
17250           sel_seq
17251             = chainon (sel_seq,
17252                        build_tree_list (selector, NULL_TREE));
17253           sel_seq
17254             = chainon (sel_seq,
17255                        build_tree_list (NULL_TREE, NULL_TREE));
17256         }
17257       else
17258         sel_seq
17259           = chainon (sel_seq,
17260                      build_tree_list (selector, NULL_TREE));
17261
17262       token = cp_lexer_peek_token (parser->lexer);
17263     }
17264
17265  finish_selector:
17266   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17267
17268   return objc_build_selector_expr (sel_seq);
17269 }
17270
17271 /* Parse a list of identifiers.
17272
17273    objc-identifier-list:
17274      identifier
17275      objc-identifier-list , identifier
17276
17277    Returns a TREE_LIST of identifier nodes.  */
17278
17279 static tree
17280 cp_parser_objc_identifier_list (cp_parser* parser)
17281 {
17282   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17283   cp_token *sep = cp_lexer_peek_token (parser->lexer);
17284
17285   while (sep->type == CPP_COMMA)
17286     {
17287       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17288       list = chainon (list,
17289                       build_tree_list (NULL_TREE,
17290                                        cp_parser_identifier (parser)));
17291       sep = cp_lexer_peek_token (parser->lexer);
17292     }
17293
17294   return list;
17295 }
17296
17297 /* Parse an Objective-C alias declaration.
17298
17299    objc-alias-declaration:
17300      @compatibility_alias identifier identifier ;
17301
17302    This function registers the alias mapping with the Objective-C front-end.
17303    It returns nothing.  */
17304
17305 static void
17306 cp_parser_objc_alias_declaration (cp_parser* parser)
17307 {
17308   tree alias, orig;
17309
17310   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17311   alias = cp_parser_identifier (parser);
17312   orig = cp_parser_identifier (parser);
17313   objc_declare_alias (alias, orig);
17314   cp_parser_consume_semicolon_at_end_of_statement (parser);
17315 }
17316
17317 /* Parse an Objective-C class forward-declaration.
17318
17319    objc-class-declaration:
17320      @class objc-identifier-list ;
17321
17322    The function registers the forward declarations with the Objective-C
17323    front-end.  It returns nothing.  */
17324
17325 static void
17326 cp_parser_objc_class_declaration (cp_parser* parser)
17327 {
17328   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17329   objc_declare_class (cp_parser_objc_identifier_list (parser));
17330   cp_parser_consume_semicolon_at_end_of_statement (parser);
17331 }
17332
17333 /* Parse a list of Objective-C protocol references.
17334
17335    objc-protocol-refs-opt:
17336      objc-protocol-refs [opt]
17337
17338    objc-protocol-refs:
17339      < objc-identifier-list >
17340
17341    Returns a TREE_LIST of identifiers, if any.  */
17342
17343 static tree
17344 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17345 {
17346   tree protorefs = NULL_TREE;
17347
17348   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17349     {
17350       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17351       protorefs = cp_parser_objc_identifier_list (parser);
17352       cp_parser_require (parser, CPP_GREATER, "`>'");
17353     }
17354
17355   return protorefs;
17356 }
17357
17358 /* Parse a Objective-C visibility specification.  */
17359
17360 static void
17361 cp_parser_objc_visibility_spec (cp_parser* parser)
17362 {
17363   cp_token *vis = cp_lexer_peek_token (parser->lexer);
17364
17365   switch (vis->keyword)
17366     {
17367     case RID_AT_PRIVATE:
17368       objc_set_visibility (2);
17369       break;
17370     case RID_AT_PROTECTED:
17371       objc_set_visibility (0);
17372       break;
17373     case RID_AT_PUBLIC:
17374       objc_set_visibility (1);
17375       break;
17376     default:
17377       return;
17378     }
17379
17380   /* Eat '@private'/'@protected'/'@public'.  */
17381   cp_lexer_consume_token (parser->lexer);
17382 }
17383
17384 /* Parse an Objective-C method type.  */
17385
17386 static void
17387 cp_parser_objc_method_type (cp_parser* parser)
17388 {
17389   objc_set_method_type
17390    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17391     ? PLUS_EXPR
17392     : MINUS_EXPR);
17393 }
17394
17395 /* Parse an Objective-C protocol qualifier.  */
17396
17397 static tree
17398 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17399 {
17400   tree quals = NULL_TREE, node;
17401   cp_token *token = cp_lexer_peek_token (parser->lexer);
17402
17403   node = token->value;
17404
17405   while (node && TREE_CODE (node) == IDENTIFIER_NODE
17406          && (node == ridpointers [(int) RID_IN]
17407              || node == ridpointers [(int) RID_OUT]
17408              || node == ridpointers [(int) RID_INOUT]
17409              || node == ridpointers [(int) RID_BYCOPY]
17410              || node == ridpointers [(int) RID_BYREF]
17411              || node == ridpointers [(int) RID_ONEWAY]))
17412     {
17413       quals = tree_cons (NULL_TREE, node, quals);
17414       cp_lexer_consume_token (parser->lexer);
17415       token = cp_lexer_peek_token (parser->lexer);
17416       node = token->value;
17417     }
17418
17419   return quals;
17420 }
17421
17422 /* Parse an Objective-C typename.  */
17423
17424 static tree
17425 cp_parser_objc_typename (cp_parser* parser)
17426 {
17427   tree typename = NULL_TREE;
17428
17429   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17430     {
17431       tree proto_quals, cp_type = NULL_TREE;
17432
17433       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17434       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17435
17436       /* An ObjC type name may consist of just protocol qualifiers, in which
17437          case the type shall default to 'id'.  */
17438       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17439         cp_type = cp_parser_type_id (parser);
17440
17441       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17442       typename = build_tree_list (proto_quals, cp_type);
17443     }
17444
17445   return typename;
17446 }
17447
17448 /* Check to see if TYPE refers to an Objective-C selector name.  */
17449
17450 static bool
17451 cp_parser_objc_selector_p (enum cpp_ttype type)
17452 {
17453   return (type == CPP_NAME || type == CPP_KEYWORD
17454           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17455           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17456           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17457           || type == CPP_XOR || type == CPP_XOR_EQ);
17458 }
17459
17460 /* Parse an Objective-C selector.  */
17461
17462 static tree
17463 cp_parser_objc_selector (cp_parser* parser)
17464 {
17465   cp_token *token = cp_lexer_consume_token (parser->lexer);
17466
17467   if (!cp_parser_objc_selector_p (token->type))
17468     {
17469       error ("invalid Objective-C++ selector name");
17470       return error_mark_node;
17471     }
17472
17473   /* C++ operator names are allowed to appear in ObjC selectors.  */
17474   switch (token->type)
17475     {
17476     case CPP_AND_AND: return get_identifier ("and");
17477     case CPP_AND_EQ: return get_identifier ("and_eq");
17478     case CPP_AND: return get_identifier ("bitand");
17479     case CPP_OR: return get_identifier ("bitor");
17480     case CPP_COMPL: return get_identifier ("compl");
17481     case CPP_NOT: return get_identifier ("not");
17482     case CPP_NOT_EQ: return get_identifier ("not_eq");
17483     case CPP_OR_OR: return get_identifier ("or");
17484     case CPP_OR_EQ: return get_identifier ("or_eq");
17485     case CPP_XOR: return get_identifier ("xor");
17486     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17487     default: return token->value;
17488     }
17489 }
17490
17491 /* Parse an Objective-C params list.  */
17492
17493 static tree
17494 cp_parser_objc_method_keyword_params (cp_parser* parser)
17495 {
17496   tree params = NULL_TREE;
17497   bool maybe_unary_selector_p = true;
17498   cp_token *token = cp_lexer_peek_token (parser->lexer);
17499
17500   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17501     {
17502       tree selector = NULL_TREE, typename, identifier;
17503
17504       if (token->type != CPP_COLON)
17505         selector = cp_parser_objc_selector (parser);
17506
17507       /* Detect if we have a unary selector.  */
17508       if (maybe_unary_selector_p
17509           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17510         return selector;
17511
17512       maybe_unary_selector_p = false;
17513       cp_parser_require (parser, CPP_COLON, "`:'");
17514       typename = cp_parser_objc_typename (parser);
17515       identifier = cp_parser_identifier (parser);
17516
17517       params
17518         = chainon (params,
17519                    objc_build_keyword_decl (selector,
17520                                             typename,
17521                                             identifier));
17522
17523       token = cp_lexer_peek_token (parser->lexer);
17524     }
17525
17526   return params;
17527 }
17528
17529 /* Parse the non-keyword Objective-C params.  */
17530
17531 static tree
17532 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17533 {
17534   tree params = make_node (TREE_LIST);
17535   cp_token *token = cp_lexer_peek_token (parser->lexer);
17536   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17537
17538   while (token->type == CPP_COMMA)
17539     {
17540       cp_parameter_declarator *parmdecl;
17541       tree parm;
17542
17543       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17544       token = cp_lexer_peek_token (parser->lexer);
17545
17546       if (token->type == CPP_ELLIPSIS)
17547         {
17548           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17549           *ellipsisp = true;
17550           break;
17551         }
17552
17553       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17554       parm = grokdeclarator (parmdecl->declarator,
17555                              &parmdecl->decl_specifiers,
17556                              PARM, /*initialized=*/0,
17557                              /*attrlist=*/NULL);
17558
17559       chainon (params, build_tree_list (NULL_TREE, parm));
17560       token = cp_lexer_peek_token (parser->lexer);
17561     }
17562
17563   return params;
17564 }
17565
17566 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17567
17568 static void
17569 cp_parser_objc_interstitial_code (cp_parser* parser)
17570 {
17571   cp_token *token = cp_lexer_peek_token (parser->lexer);
17572
17573   /* If the next token is `extern' and the following token is a string
17574      literal, then we have a linkage specification.  */
17575   if (token->keyword == RID_EXTERN
17576       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17577     cp_parser_linkage_specification (parser);
17578   /* Handle #pragma, if any.  */
17579   else if (token->type == CPP_PRAGMA)
17580     cp_parser_pragma (parser, pragma_external);
17581   /* Allow stray semicolons.  */
17582   else if (token->type == CPP_SEMICOLON)
17583     cp_lexer_consume_token (parser->lexer);
17584   /* Finally, try to parse a block-declaration, or a function-definition.  */
17585   else
17586     cp_parser_block_declaration (parser, /*statement_p=*/false);
17587 }
17588
17589 /* Parse a method signature.  */
17590
17591 static tree
17592 cp_parser_objc_method_signature (cp_parser* parser)
17593 {
17594   tree rettype, kwdparms, optparms;
17595   bool ellipsis = false;
17596
17597   cp_parser_objc_method_type (parser);
17598   rettype = cp_parser_objc_typename (parser);
17599   kwdparms = cp_parser_objc_method_keyword_params (parser);
17600   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17601
17602   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17603 }
17604
17605 /* Pars an Objective-C method prototype list.  */
17606
17607 static void
17608 cp_parser_objc_method_prototype_list (cp_parser* parser)
17609 {
17610   cp_token *token = cp_lexer_peek_token (parser->lexer);
17611
17612   while (token->keyword != RID_AT_END)
17613     {
17614       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17615         {
17616           objc_add_method_declaration
17617            (cp_parser_objc_method_signature (parser));
17618           cp_parser_consume_semicolon_at_end_of_statement (parser);
17619         }
17620       else
17621         /* Allow for interspersed non-ObjC++ code.  */
17622         cp_parser_objc_interstitial_code (parser);
17623
17624       token = cp_lexer_peek_token (parser->lexer);
17625     }
17626
17627   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17628   objc_finish_interface ();
17629 }
17630
17631 /* Parse an Objective-C method definition list.  */
17632
17633 static void
17634 cp_parser_objc_method_definition_list (cp_parser* parser)
17635 {
17636   cp_token *token = cp_lexer_peek_token (parser->lexer);
17637
17638   while (token->keyword != RID_AT_END)
17639     {
17640       tree meth;
17641
17642       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17643         {
17644           push_deferring_access_checks (dk_deferred);
17645           objc_start_method_definition
17646            (cp_parser_objc_method_signature (parser));
17647
17648           /* For historical reasons, we accept an optional semicolon.  */
17649           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17650             cp_lexer_consume_token (parser->lexer);
17651
17652           perform_deferred_access_checks ();
17653           stop_deferring_access_checks ();
17654           meth = cp_parser_function_definition_after_declarator (parser,
17655                                                                  false);
17656           pop_deferring_access_checks ();
17657           objc_finish_method_definition (meth);
17658         }
17659       else
17660         /* Allow for interspersed non-ObjC++ code.  */
17661         cp_parser_objc_interstitial_code (parser);
17662
17663       token = cp_lexer_peek_token (parser->lexer);
17664     }
17665
17666   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17667   objc_finish_implementation ();
17668 }
17669
17670 /* Parse Objective-C ivars.  */
17671
17672 static void
17673 cp_parser_objc_class_ivars (cp_parser* parser)
17674 {
17675   cp_token *token = cp_lexer_peek_token (parser->lexer);
17676
17677   if (token->type != CPP_OPEN_BRACE)
17678     return;     /* No ivars specified.  */
17679
17680   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17681   token = cp_lexer_peek_token (parser->lexer);
17682
17683   while (token->type != CPP_CLOSE_BRACE)
17684     {
17685       cp_decl_specifier_seq declspecs;
17686       int decl_class_or_enum_p;
17687       tree prefix_attributes;
17688
17689       cp_parser_objc_visibility_spec (parser);
17690
17691       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17692         break;
17693
17694       cp_parser_decl_specifier_seq (parser,
17695                                     CP_PARSER_FLAGS_OPTIONAL,
17696                                     &declspecs,
17697                                     &decl_class_or_enum_p);
17698       prefix_attributes = declspecs.attributes;
17699       declspecs.attributes = NULL_TREE;
17700
17701       /* Keep going until we hit the `;' at the end of the
17702          declaration.  */
17703       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17704         {
17705           tree width = NULL_TREE, attributes, first_attribute, decl;
17706           cp_declarator *declarator = NULL;
17707           int ctor_dtor_or_conv_p;
17708
17709           /* Check for a (possibly unnamed) bitfield declaration.  */
17710           token = cp_lexer_peek_token (parser->lexer);
17711           if (token->type == CPP_COLON)
17712             goto eat_colon;
17713
17714           if (token->type == CPP_NAME
17715               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17716                   == CPP_COLON))
17717             {
17718               /* Get the name of the bitfield.  */
17719               declarator = make_id_declarator (NULL_TREE,
17720                                                cp_parser_identifier (parser),
17721                                                sfk_none);
17722
17723              eat_colon:
17724               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17725               /* Get the width of the bitfield.  */
17726               width
17727                 = cp_parser_constant_expression (parser,
17728                                                  /*allow_non_constant=*/false,
17729                                                  NULL);
17730             }
17731           else
17732             {
17733               /* Parse the declarator.  */
17734               declarator
17735                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17736                                         &ctor_dtor_or_conv_p,
17737                                         /*parenthesized_p=*/NULL,
17738                                         /*member_p=*/false);
17739             }
17740
17741           /* Look for attributes that apply to the ivar.  */
17742           attributes = cp_parser_attributes_opt (parser);
17743           /* Remember which attributes are prefix attributes and
17744              which are not.  */
17745           first_attribute = attributes;
17746           /* Combine the attributes.  */
17747           attributes = chainon (prefix_attributes, attributes);
17748
17749           if (width)
17750             {
17751               /* Create the bitfield declaration.  */
17752               decl = grokbitfield (declarator, &declspecs, width);
17753               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17754             }
17755           else
17756             decl = grokfield (declarator, &declspecs,
17757                               NULL_TREE, /*init_const_expr_p=*/false,
17758                               NULL_TREE, attributes);
17759
17760           /* Add the instance variable.  */
17761           objc_add_instance_variable (decl);
17762
17763           /* Reset PREFIX_ATTRIBUTES.  */
17764           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17765             attributes = TREE_CHAIN (attributes);
17766           if (attributes)
17767             TREE_CHAIN (attributes) = NULL_TREE;
17768
17769           token = cp_lexer_peek_token (parser->lexer);
17770
17771           if (token->type == CPP_COMMA)
17772             {
17773               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17774               continue;
17775             }
17776           break;
17777         }
17778
17779       cp_parser_consume_semicolon_at_end_of_statement (parser);
17780       token = cp_lexer_peek_token (parser->lexer);
17781     }
17782
17783   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17784   /* For historical reasons, we accept an optional semicolon.  */
17785   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17786     cp_lexer_consume_token (parser->lexer);
17787 }
17788
17789 /* Parse an Objective-C protocol declaration.  */
17790
17791 static void
17792 cp_parser_objc_protocol_declaration (cp_parser* parser)
17793 {
17794   tree proto, protorefs;
17795   cp_token *tok;
17796
17797   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17798   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17799     {
17800       error ("identifier expected after %<@protocol%>");
17801       goto finish;
17802     }
17803
17804   /* See if we have a forward declaration or a definition.  */
17805   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17806
17807   /* Try a forward declaration first.  */
17808   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17809     {
17810       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17811      finish:
17812       cp_parser_consume_semicolon_at_end_of_statement (parser);
17813     }
17814
17815   /* Ok, we got a full-fledged definition (or at least should).  */
17816   else
17817     {
17818       proto = cp_parser_identifier (parser);
17819       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17820       objc_start_protocol (proto, protorefs);
17821       cp_parser_objc_method_prototype_list (parser);
17822     }
17823 }
17824
17825 /* Parse an Objective-C superclass or category.  */
17826
17827 static void
17828 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17829                                                           tree *categ)
17830 {
17831   cp_token *next = cp_lexer_peek_token (parser->lexer);
17832
17833   *super = *categ = NULL_TREE;
17834   if (next->type == CPP_COLON)
17835     {
17836       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17837       *super = cp_parser_identifier (parser);
17838     }
17839   else if (next->type == CPP_OPEN_PAREN)
17840     {
17841       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17842       *categ = cp_parser_identifier (parser);
17843       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17844     }
17845 }
17846
17847 /* Parse an Objective-C class interface.  */
17848
17849 static void
17850 cp_parser_objc_class_interface (cp_parser* parser)
17851 {
17852   tree name, super, categ, protos;
17853
17854   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17855   name = cp_parser_identifier (parser);
17856   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17857   protos = cp_parser_objc_protocol_refs_opt (parser);
17858
17859   /* We have either a class or a category on our hands.  */
17860   if (categ)
17861     objc_start_category_interface (name, categ, protos);
17862   else
17863     {
17864       objc_start_class_interface (name, super, protos);
17865       /* Handle instance variable declarations, if any.  */
17866       cp_parser_objc_class_ivars (parser);
17867       objc_continue_interface ();
17868     }
17869
17870   cp_parser_objc_method_prototype_list (parser);
17871 }
17872
17873 /* Parse an Objective-C class implementation.  */
17874
17875 static void
17876 cp_parser_objc_class_implementation (cp_parser* parser)
17877 {
17878   tree name, super, categ;
17879
17880   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17881   name = cp_parser_identifier (parser);
17882   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17883
17884   /* We have either a class or a category on our hands.  */
17885   if (categ)
17886     objc_start_category_implementation (name, categ);
17887   else
17888     {
17889       objc_start_class_implementation (name, super);
17890       /* Handle instance variable declarations, if any.  */
17891       cp_parser_objc_class_ivars (parser);
17892       objc_continue_implementation ();
17893     }
17894
17895   cp_parser_objc_method_definition_list (parser);
17896 }
17897
17898 /* Consume the @end token and finish off the implementation.  */
17899
17900 static void
17901 cp_parser_objc_end_implementation (cp_parser* parser)
17902 {
17903   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17904   objc_finish_implementation ();
17905 }
17906
17907 /* Parse an Objective-C declaration.  */
17908
17909 static void
17910 cp_parser_objc_declaration (cp_parser* parser)
17911 {
17912   /* Try to figure out what kind of declaration is present.  */
17913   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17914
17915   switch (kwd->keyword)
17916     {
17917     case RID_AT_ALIAS:
17918       cp_parser_objc_alias_declaration (parser);
17919       break;
17920     case RID_AT_CLASS:
17921       cp_parser_objc_class_declaration (parser);
17922       break;
17923     case RID_AT_PROTOCOL:
17924       cp_parser_objc_protocol_declaration (parser);
17925       break;
17926     case RID_AT_INTERFACE:
17927       cp_parser_objc_class_interface (parser);
17928       break;
17929     case RID_AT_IMPLEMENTATION:
17930       cp_parser_objc_class_implementation (parser);
17931       break;
17932     case RID_AT_END:
17933       cp_parser_objc_end_implementation (parser);
17934       break;
17935     default:
17936       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17937       cp_parser_skip_to_end_of_block_or_statement (parser);
17938     }
17939 }
17940
17941 /* Parse an Objective-C try-catch-finally statement.
17942
17943    objc-try-catch-finally-stmt:
17944      @try compound-statement objc-catch-clause-seq [opt]
17945        objc-finally-clause [opt]
17946
17947    objc-catch-clause-seq:
17948      objc-catch-clause objc-catch-clause-seq [opt]
17949
17950    objc-catch-clause:
17951      @catch ( exception-declaration ) compound-statement
17952
17953    objc-finally-clause
17954      @finally compound-statement
17955
17956    Returns NULL_TREE.  */
17957
17958 static tree
17959 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17960   location_t location;
17961   tree stmt;
17962
17963   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17964   location = cp_lexer_peek_token (parser->lexer)->location;
17965   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17966      node, lest it get absorbed into the surrounding block.  */
17967   stmt = push_stmt_list ();
17968   cp_parser_compound_statement (parser, NULL, false);
17969   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17970
17971   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17972     {
17973       cp_parameter_declarator *parmdecl;
17974       tree parm;
17975
17976       cp_lexer_consume_token (parser->lexer);
17977       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17978       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17979       parm = grokdeclarator (parmdecl->declarator,
17980                              &parmdecl->decl_specifiers,
17981                              PARM, /*initialized=*/0,
17982                              /*attrlist=*/NULL);
17983       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17984       objc_begin_catch_clause (parm);
17985       cp_parser_compound_statement (parser, NULL, false);
17986       objc_finish_catch_clause ();
17987     }
17988
17989   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17990     {
17991       cp_lexer_consume_token (parser->lexer);
17992       location = cp_lexer_peek_token (parser->lexer)->location;
17993       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17994          node, lest it get absorbed into the surrounding block.  */
17995       stmt = push_stmt_list ();
17996       cp_parser_compound_statement (parser, NULL, false);
17997       objc_build_finally_clause (location, pop_stmt_list (stmt));
17998     }
17999
18000   return objc_finish_try_stmt ();
18001 }
18002
18003 /* Parse an Objective-C synchronized statement.
18004
18005    objc-synchronized-stmt:
18006      @synchronized ( expression ) compound-statement
18007
18008    Returns NULL_TREE.  */
18009
18010 static tree
18011 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18012   location_t location;
18013   tree lock, stmt;
18014
18015   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18016
18017   location = cp_lexer_peek_token (parser->lexer)->location;
18018   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18019   lock = cp_parser_expression (parser, false);
18020   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18021
18022   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18023      node, lest it get absorbed into the surrounding block.  */
18024   stmt = push_stmt_list ();
18025   cp_parser_compound_statement (parser, NULL, false);
18026
18027   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18028 }
18029
18030 /* Parse an Objective-C throw statement.
18031
18032    objc-throw-stmt:
18033      @throw assignment-expression [opt] ;
18034
18035    Returns a constructed '@throw' statement.  */
18036
18037 static tree
18038 cp_parser_objc_throw_statement (cp_parser *parser) {
18039   tree expr = NULL_TREE;
18040
18041   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18042
18043   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18044     expr = cp_parser_assignment_expression (parser, false);
18045
18046   cp_parser_consume_semicolon_at_end_of_statement (parser);
18047
18048   return objc_build_throw_stmt (expr);
18049 }
18050
18051 /* Parse an Objective-C statement.  */
18052
18053 static tree
18054 cp_parser_objc_statement (cp_parser * parser) {
18055   /* Try to figure out what kind of declaration is present.  */
18056   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18057
18058   switch (kwd->keyword)
18059     {
18060     case RID_AT_TRY:
18061       return cp_parser_objc_try_catch_finally_statement (parser);
18062     case RID_AT_SYNCHRONIZED:
18063       return cp_parser_objc_synchronized_statement (parser);
18064     case RID_AT_THROW:
18065       return cp_parser_objc_throw_statement (parser);
18066     default:
18067       error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
18068       cp_parser_skip_to_end_of_block_or_statement (parser);
18069     }
18070
18071   return error_mark_node;
18072 }
18073 \f
18074 /* OpenMP 2.5 parsing routines.  */
18075
18076 /* Returns name of the next clause.
18077    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18078    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
18079    returned and the token is consumed.  */
18080
18081 static pragma_omp_clause
18082 cp_parser_omp_clause_name (cp_parser *parser)
18083 {
18084   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18085
18086   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18087     result = PRAGMA_OMP_CLAUSE_IF;
18088   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18089     result = PRAGMA_OMP_CLAUSE_DEFAULT;
18090   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18091     result = PRAGMA_OMP_CLAUSE_PRIVATE;
18092   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18093     {
18094       tree id = cp_lexer_peek_token (parser->lexer)->value;
18095       const char *p = IDENTIFIER_POINTER (id);
18096
18097       switch (p[0])
18098         {
18099         case 'c':
18100           if (!strcmp ("copyin", p))
18101             result = PRAGMA_OMP_CLAUSE_COPYIN;
18102           else if (!strcmp ("copyprivate", p))
18103             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18104           break;
18105         case 'f':
18106           if (!strcmp ("firstprivate", p))
18107             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18108           break;
18109         case 'l':
18110           if (!strcmp ("lastprivate", p))
18111             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18112           break;
18113         case 'n':
18114           if (!strcmp ("nowait", p))
18115             result = PRAGMA_OMP_CLAUSE_NOWAIT;
18116           else if (!strcmp ("num_threads", p))
18117             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18118           break;
18119         case 'o':
18120           if (!strcmp ("ordered", p))
18121             result = PRAGMA_OMP_CLAUSE_ORDERED;
18122           break;
18123         case 'r':
18124           if (!strcmp ("reduction", p))
18125             result = PRAGMA_OMP_CLAUSE_REDUCTION;
18126           break;
18127         case 's':
18128           if (!strcmp ("schedule", p))
18129             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18130           else if (!strcmp ("shared", p))
18131             result = PRAGMA_OMP_CLAUSE_SHARED;
18132           break;
18133         }
18134     }
18135
18136   if (result != PRAGMA_OMP_CLAUSE_NONE)
18137     cp_lexer_consume_token (parser->lexer);
18138
18139   return result;
18140 }
18141
18142 /* Validate that a clause of the given type does not already exist.  */
18143
18144 static void
18145 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18146 {
18147   tree c;
18148
18149   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18150     if (OMP_CLAUSE_CODE (c) == code)
18151       {
18152         error ("too many %qs clauses", name);
18153         break;
18154       }
18155 }
18156
18157 /* OpenMP 2.5:
18158    variable-list:
18159      identifier
18160      variable-list , identifier
18161
18162    In addition, we match a closing parenthesis.  An opening parenthesis
18163    will have been consumed by the caller.
18164
18165    If KIND is nonzero, create the appropriate node and install the decl
18166    in OMP_CLAUSE_DECL and add the node to the head of the list.
18167
18168    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18169    return the list created.  */
18170
18171 static tree
18172 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18173                                 tree list)
18174 {
18175   while (1)
18176     {
18177       tree name, decl;
18178
18179       name = cp_parser_id_expression (parser, /*template_p=*/false,
18180                                       /*check_dependency_p=*/true,
18181                                       /*template_p=*/NULL,
18182                                       /*declarator_p=*/false,
18183                                       /*optional_p=*/false);
18184       if (name == error_mark_node)
18185         goto skip_comma;
18186
18187       decl = cp_parser_lookup_name_simple (parser, name);
18188       if (decl == error_mark_node)
18189         cp_parser_name_lookup_error (parser, name, decl, NULL);
18190       else if (kind != 0)
18191         {
18192           tree u = build_omp_clause (kind);
18193           OMP_CLAUSE_DECL (u) = decl;
18194           OMP_CLAUSE_CHAIN (u) = list;
18195           list = u;
18196         }
18197       else
18198         list = tree_cons (decl, NULL_TREE, list);
18199
18200     get_comma:
18201       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18202         break;
18203       cp_lexer_consume_token (parser->lexer);
18204     }
18205
18206   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18207     {
18208       int ending;
18209
18210       /* Try to resync to an unnested comma.  Copied from
18211          cp_parser_parenthesized_expression_list.  */
18212     skip_comma:
18213       ending = cp_parser_skip_to_closing_parenthesis (parser,
18214                                                       /*recovering=*/true,
18215                                                       /*or_comma=*/true,
18216                                                       /*consume_paren=*/true);
18217       if (ending < 0)
18218         goto get_comma;
18219     }
18220
18221   return list;
18222 }
18223
18224 /* Similarly, but expect leading and trailing parenthesis.  This is a very
18225    common case for omp clauses.  */
18226
18227 static tree
18228 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18229 {
18230   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18231     return cp_parser_omp_var_list_no_open (parser, kind, list);
18232   return list;
18233 }
18234
18235 /* OpenMP 2.5:
18236    default ( shared | none ) */
18237
18238 static tree
18239 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18240 {
18241   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18242   tree c;
18243
18244   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18245     return list;
18246   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18247     {
18248       tree id = cp_lexer_peek_token (parser->lexer)->value;
18249       const char *p = IDENTIFIER_POINTER (id);
18250
18251       switch (p[0])
18252         {
18253         case 'n':
18254           if (strcmp ("none", p) != 0)
18255             goto invalid_kind;
18256           kind = OMP_CLAUSE_DEFAULT_NONE;
18257           break;
18258
18259         case 's':
18260           if (strcmp ("shared", p) != 0)
18261             goto invalid_kind;
18262           kind = OMP_CLAUSE_DEFAULT_SHARED;
18263           break;
18264
18265         default:
18266           goto invalid_kind;
18267         }
18268
18269       cp_lexer_consume_token (parser->lexer);
18270     }
18271   else
18272     {
18273     invalid_kind:
18274       cp_parser_error (parser, "expected %<none%> or %<shared%>");
18275     }
18276
18277   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18278     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18279                                            /*or_comma=*/false,
18280                                            /*consume_paren=*/true);
18281
18282   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18283     return list;
18284
18285   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18286   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18287   OMP_CLAUSE_CHAIN (c) = list;
18288   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18289
18290   return c;
18291 }
18292
18293 /* OpenMP 2.5:
18294    if ( expression ) */
18295
18296 static tree
18297 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18298 {
18299   tree t, c;
18300
18301   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18302     return list;
18303
18304   t = cp_parser_condition (parser);
18305
18306   if (t == error_mark_node
18307       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18308     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18309                                            /*or_comma=*/false,
18310                                            /*consume_paren=*/true);
18311
18312   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18313
18314   c = build_omp_clause (OMP_CLAUSE_IF);
18315   OMP_CLAUSE_IF_EXPR (c) = t;
18316   OMP_CLAUSE_CHAIN (c) = list;
18317
18318   return c;
18319 }
18320
18321 /* OpenMP 2.5:
18322    nowait */
18323
18324 static tree
18325 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18326 {
18327   tree c;
18328
18329   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18330
18331   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18332   OMP_CLAUSE_CHAIN (c) = list;
18333   return c;
18334 }
18335
18336 /* OpenMP 2.5:
18337    num_threads ( expression ) */
18338
18339 static tree
18340 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18341 {
18342   tree t, c;
18343
18344   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18345     return list;
18346
18347   t = cp_parser_expression (parser, false);
18348
18349   if (t == error_mark_node
18350       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18351     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18352                                            /*or_comma=*/false,
18353                                            /*consume_paren=*/true);
18354
18355   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18356
18357   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18358   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18359   OMP_CLAUSE_CHAIN (c) = list;
18360
18361   return c;
18362 }
18363
18364 /* OpenMP 2.5:
18365    ordered */
18366
18367 static tree
18368 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18369 {
18370   tree c;
18371
18372   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18373
18374   c = build_omp_clause (OMP_CLAUSE_ORDERED);
18375   OMP_CLAUSE_CHAIN (c) = list;
18376   return c;
18377 }
18378
18379 /* OpenMP 2.5:
18380    reduction ( reduction-operator : variable-list )
18381
18382    reduction-operator:
18383      One of: + * - & ^ | && || */
18384
18385 static tree
18386 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18387 {
18388   enum tree_code code;
18389   tree nlist, c;
18390
18391   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18392     return list;
18393
18394   switch (cp_lexer_peek_token (parser->lexer)->type)
18395     {
18396     case CPP_PLUS:
18397       code = PLUS_EXPR;
18398       break;
18399     case CPP_MULT:
18400       code = MULT_EXPR;
18401       break;
18402     case CPP_MINUS:
18403       code = MINUS_EXPR;
18404       break;
18405     case CPP_AND:
18406       code = BIT_AND_EXPR;
18407       break;
18408     case CPP_XOR:
18409       code = BIT_XOR_EXPR;
18410       break;
18411     case CPP_OR:
18412       code = BIT_IOR_EXPR;
18413       break;
18414     case CPP_AND_AND:
18415       code = TRUTH_ANDIF_EXPR;
18416       break;
18417     case CPP_OR_OR:
18418       code = TRUTH_ORIF_EXPR;
18419       break;
18420     default:
18421       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18422     resync_fail:
18423       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18424                                              /*or_comma=*/false,
18425                                              /*consume_paren=*/true);
18426       return list;
18427     }
18428   cp_lexer_consume_token (parser->lexer);
18429
18430   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18431     goto resync_fail;
18432
18433   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18434   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18435     OMP_CLAUSE_REDUCTION_CODE (c) = code;
18436
18437   return nlist;
18438 }
18439
18440 /* OpenMP 2.5:
18441    schedule ( schedule-kind )
18442    schedule ( schedule-kind , expression )
18443
18444    schedule-kind:
18445      static | dynamic | guided | runtime  */
18446
18447 static tree
18448 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18449 {
18450   tree c, t;
18451
18452   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18453     return list;
18454
18455   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18456
18457   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18458     {
18459       tree id = cp_lexer_peek_token (parser->lexer)->value;
18460       const char *p = IDENTIFIER_POINTER (id);
18461
18462       switch (p[0])
18463         {
18464         case 'd':
18465           if (strcmp ("dynamic", p) != 0)
18466             goto invalid_kind;
18467           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18468           break;
18469
18470         case 'g':
18471           if (strcmp ("guided", p) != 0)
18472             goto invalid_kind;
18473           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18474           break;
18475
18476         case 'r':
18477           if (strcmp ("runtime", p) != 0)
18478             goto invalid_kind;
18479           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18480           break;
18481
18482         default:
18483           goto invalid_kind;
18484         }
18485     }
18486   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18487     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18488   else
18489     goto invalid_kind;
18490   cp_lexer_consume_token (parser->lexer);
18491
18492   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18493     {
18494       cp_lexer_consume_token (parser->lexer);
18495
18496       t = cp_parser_assignment_expression (parser, false);
18497
18498       if (t == error_mark_node)
18499         goto resync_fail;
18500       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18501         error ("schedule %<runtime%> does not take "
18502                "a %<chunk_size%> parameter");
18503       else
18504         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18505
18506       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18507         goto resync_fail;
18508     }
18509   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18510     goto resync_fail;
18511
18512   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18513   OMP_CLAUSE_CHAIN (c) = list;
18514   return c;
18515
18516  invalid_kind:
18517   cp_parser_error (parser, "invalid schedule kind");
18518  resync_fail:
18519   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18520                                          /*or_comma=*/false,
18521                                          /*consume_paren=*/true);
18522   return list;
18523 }
18524
18525 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
18526    is a bitmask in MASK.  Return the list of clauses found; the result
18527    of clause default goes in *pdefault.  */
18528
18529 static tree
18530 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18531                            const char *where, cp_token *pragma_tok)
18532 {
18533   tree clauses = NULL;
18534
18535   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18536     {
18537       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18538       const char *c_name;
18539       tree prev = clauses;
18540
18541       switch (c_kind)
18542         {
18543         case PRAGMA_OMP_CLAUSE_COPYIN:
18544           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18545           c_name = "copyin";
18546           break;
18547         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18548           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18549                                             clauses);
18550           c_name = "copyprivate";
18551           break;
18552         case PRAGMA_OMP_CLAUSE_DEFAULT:
18553           clauses = cp_parser_omp_clause_default (parser, clauses);
18554           c_name = "default";
18555           break;
18556         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18557           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18558                                             clauses);
18559           c_name = "firstprivate";
18560           break;
18561         case PRAGMA_OMP_CLAUSE_IF:
18562           clauses = cp_parser_omp_clause_if (parser, clauses);
18563           c_name = "if";
18564           break;
18565         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18566           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18567                                             clauses);
18568           c_name = "lastprivate";
18569           break;
18570         case PRAGMA_OMP_CLAUSE_NOWAIT:
18571           clauses = cp_parser_omp_clause_nowait (parser, clauses);
18572           c_name = "nowait";
18573           break;
18574         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18575           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18576           c_name = "num_threads";
18577           break;
18578         case PRAGMA_OMP_CLAUSE_ORDERED:
18579           clauses = cp_parser_omp_clause_ordered (parser, clauses);
18580           c_name = "ordered";
18581           break;
18582         case PRAGMA_OMP_CLAUSE_PRIVATE:
18583           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18584                                             clauses);
18585           c_name = "private";
18586           break;
18587         case PRAGMA_OMP_CLAUSE_REDUCTION:
18588           clauses = cp_parser_omp_clause_reduction (parser, clauses);
18589           c_name = "reduction";
18590           break;
18591         case PRAGMA_OMP_CLAUSE_SCHEDULE:
18592           clauses = cp_parser_omp_clause_schedule (parser, clauses);
18593           c_name = "schedule";
18594           break;
18595         case PRAGMA_OMP_CLAUSE_SHARED:
18596           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18597                                             clauses);
18598           c_name = "shared";
18599           break;
18600         default:
18601           cp_parser_error (parser, "expected %<#pragma omp%> clause");
18602           goto saw_error;
18603         }
18604
18605       if (((mask >> c_kind) & 1) == 0)
18606         {
18607           /* Remove the invalid clause(s) from the list to avoid
18608              confusing the rest of the compiler.  */
18609           clauses = prev;
18610           error ("%qs is not valid for %qs", c_name, where);
18611         }
18612     }
18613  saw_error:
18614   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18615   return finish_omp_clauses (clauses);
18616 }
18617
18618 /* OpenMP 2.5:
18619    structured-block:
18620      statement
18621
18622    In practice, we're also interested in adding the statement to an
18623    outer node.  So it is convenient if we work around the fact that
18624    cp_parser_statement calls add_stmt.  */
18625
18626 static unsigned
18627 cp_parser_begin_omp_structured_block (cp_parser *parser)
18628 {
18629   unsigned save = parser->in_statement;
18630
18631   /* Only move the values to IN_OMP_BLOCK if they weren't false.
18632      This preserves the "not within loop or switch" style error messages
18633      for nonsense cases like
18634         void foo() {
18635         #pragma omp single
18636           break;
18637         }
18638   */
18639   if (parser->in_statement)
18640     parser->in_statement = IN_OMP_BLOCK;
18641
18642   return save;
18643 }
18644
18645 static void
18646 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18647 {
18648   parser->in_statement = save;
18649 }
18650
18651 static tree
18652 cp_parser_omp_structured_block (cp_parser *parser)
18653 {
18654   tree stmt = begin_omp_structured_block ();
18655   unsigned int save = cp_parser_begin_omp_structured_block (parser);
18656
18657   cp_parser_statement (parser, NULL_TREE, false);
18658
18659   cp_parser_end_omp_structured_block (parser, save);
18660   return finish_omp_structured_block (stmt);
18661 }
18662
18663 /* OpenMP 2.5:
18664    # pragma omp atomic new-line
18665      expression-stmt
18666
18667    expression-stmt:
18668      x binop= expr | x++ | ++x | x-- | --x
18669    binop:
18670      +, *, -, /, &, ^, |, <<, >>
18671
18672   where x is an lvalue expression with scalar type.  */
18673
18674 static void
18675 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18676 {
18677   tree lhs, rhs;
18678   enum tree_code code;
18679
18680   cp_parser_require_pragma_eol (parser, pragma_tok);
18681
18682   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18683                                     /*cast_p=*/false);
18684   switch (TREE_CODE (lhs))
18685     {
18686     case ERROR_MARK:
18687       goto saw_error;
18688
18689     case PREINCREMENT_EXPR:
18690     case POSTINCREMENT_EXPR:
18691       lhs = TREE_OPERAND (lhs, 0);
18692       code = PLUS_EXPR;
18693       rhs = integer_one_node;
18694       break;
18695
18696     case PREDECREMENT_EXPR:
18697     case POSTDECREMENT_EXPR:
18698       lhs = TREE_OPERAND (lhs, 0);
18699       code = MINUS_EXPR;
18700       rhs = integer_one_node;
18701       break;
18702
18703     default:
18704       switch (cp_lexer_peek_token (parser->lexer)->type)
18705         {
18706         case CPP_MULT_EQ:
18707           code = MULT_EXPR;
18708           break;
18709         case CPP_DIV_EQ:
18710           code = TRUNC_DIV_EXPR;
18711           break;
18712         case CPP_PLUS_EQ:
18713           code = PLUS_EXPR;
18714           break;
18715         case CPP_MINUS_EQ:
18716           code = MINUS_EXPR;
18717           break;
18718         case CPP_LSHIFT_EQ:
18719           code = LSHIFT_EXPR;
18720           break;
18721         case CPP_RSHIFT_EQ:
18722           code = RSHIFT_EXPR;
18723           break;
18724         case CPP_AND_EQ:
18725           code = BIT_AND_EXPR;
18726           break;
18727         case CPP_OR_EQ:
18728           code = BIT_IOR_EXPR;
18729           break;
18730         case CPP_XOR_EQ:
18731           code = BIT_XOR_EXPR;
18732           break;
18733         default:
18734           cp_parser_error (parser,
18735                            "invalid operator for %<#pragma omp atomic%>");
18736           goto saw_error;
18737         }
18738       cp_lexer_consume_token (parser->lexer);
18739
18740       rhs = cp_parser_expression (parser, false);
18741       if (rhs == error_mark_node)
18742         goto saw_error;
18743       break;
18744     }
18745   finish_omp_atomic (code, lhs, rhs);
18746   cp_parser_consume_semicolon_at_end_of_statement (parser);
18747   return;
18748
18749  saw_error:
18750   cp_parser_skip_to_end_of_block_or_statement (parser);
18751 }
18752
18753
18754 /* OpenMP 2.5:
18755    # pragma omp barrier new-line  */
18756
18757 static void
18758 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18759 {
18760   cp_parser_require_pragma_eol (parser, pragma_tok);
18761   finish_omp_barrier ();
18762 }
18763
18764 /* OpenMP 2.5:
18765    # pragma omp critical [(name)] new-line
18766      structured-block  */
18767
18768 static tree
18769 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18770 {
18771   tree stmt, name = NULL;
18772
18773   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18774     {
18775       cp_lexer_consume_token (parser->lexer);
18776
18777       name = cp_parser_identifier (parser);
18778
18779       if (name == error_mark_node
18780           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18781         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18782                                                /*or_comma=*/false,
18783                                                /*consume_paren=*/true);
18784       if (name == error_mark_node)
18785         name = NULL;
18786     }
18787   cp_parser_require_pragma_eol (parser, pragma_tok);
18788
18789   stmt = cp_parser_omp_structured_block (parser);
18790   return c_finish_omp_critical (stmt, name);
18791 }
18792
18793 /* OpenMP 2.5:
18794    # pragma omp flush flush-vars[opt] new-line
18795
18796    flush-vars:
18797      ( variable-list ) */
18798
18799 static void
18800 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18801 {
18802   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18803     (void) cp_parser_omp_var_list (parser, 0, NULL);
18804   cp_parser_require_pragma_eol (parser, pragma_tok);
18805
18806   finish_omp_flush ();
18807 }
18808
18809 /* Parse the restricted form of the for statment allowed by OpenMP.  */
18810
18811 static tree
18812 cp_parser_omp_for_loop (cp_parser *parser)
18813 {
18814   tree init, cond, incr, body, decl, pre_body;
18815   location_t loc;
18816
18817   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18818     {
18819       cp_parser_error (parser, "for statement expected");
18820       return NULL;
18821     }
18822   loc = cp_lexer_consume_token (parser->lexer)->location;
18823   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18824     return NULL;
18825
18826   init = decl = NULL;
18827   pre_body = push_stmt_list ();
18828   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18829     {
18830       cp_decl_specifier_seq type_specifiers;
18831
18832       /* First, try to parse as an initialized declaration.  See
18833          cp_parser_condition, from whence the bulk of this is copied.  */
18834
18835       cp_parser_parse_tentatively (parser);
18836       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18837                                     &type_specifiers);
18838       if (!cp_parser_error_occurred (parser))
18839         {
18840           tree asm_specification, attributes;
18841           cp_declarator *declarator;
18842
18843           declarator = cp_parser_declarator (parser,
18844                                              CP_PARSER_DECLARATOR_NAMED,
18845                                              /*ctor_dtor_or_conv_p=*/NULL,
18846                                              /*parenthesized_p=*/NULL,
18847                                              /*member_p=*/false);
18848           attributes = cp_parser_attributes_opt (parser);
18849           asm_specification = cp_parser_asm_specification_opt (parser);
18850
18851           cp_parser_require (parser, CPP_EQ, "`='");
18852           if (cp_parser_parse_definitely (parser))
18853             {
18854               tree pushed_scope;
18855
18856               decl = start_decl (declarator, &type_specifiers,
18857                                  /*initialized_p=*/false, attributes,
18858                                  /*prefix_attributes=*/NULL_TREE,
18859                                  &pushed_scope);
18860
18861               init = cp_parser_assignment_expression (parser, false);
18862
18863               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18864                               asm_specification, LOOKUP_ONLYCONVERTING);
18865
18866               if (pushed_scope)
18867                 pop_scope (pushed_scope);
18868             }
18869         }
18870       else
18871         cp_parser_abort_tentative_parse (parser);
18872
18873       /* If parsing as an initialized declaration failed, try again as
18874          a simple expression.  */
18875       if (decl == NULL)
18876         init = cp_parser_expression (parser, false);
18877     }
18878   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18879   pre_body = pop_stmt_list (pre_body);
18880
18881   cond = NULL;
18882   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18883     cond = cp_parser_condition (parser);
18884   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18885
18886   incr = NULL;
18887   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18888     incr = cp_parser_expression (parser, false);
18889
18890   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18891     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18892                                            /*or_comma=*/false,
18893                                            /*consume_paren=*/true);
18894
18895   /* Note that we saved the original contents of this flag when we entered
18896      the structured block, and so we don't need to re-save it here.  */
18897   parser->in_statement = IN_OMP_FOR;
18898
18899   /* Note that the grammar doesn't call for a structured block here,
18900      though the loop as a whole is a structured block.  */
18901   body = push_stmt_list ();
18902   cp_parser_statement (parser, NULL_TREE, false);
18903   body = pop_stmt_list (body);
18904
18905   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18906 }
18907
18908 /* OpenMP 2.5:
18909    #pragma omp for for-clause[optseq] new-line
18910      for-loop  */
18911
18912 #define OMP_FOR_CLAUSE_MASK                             \
18913         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
18914         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
18915         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
18916         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
18917         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
18918         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
18919         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18920
18921 static tree
18922 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18923 {
18924   tree clauses, sb, ret;
18925   unsigned int save;
18926
18927   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18928                                        "#pragma omp for", pragma_tok);
18929
18930   sb = begin_omp_structured_block ();
18931   save = cp_parser_begin_omp_structured_block (parser);
18932
18933   ret = cp_parser_omp_for_loop (parser);
18934   if (ret)
18935     OMP_FOR_CLAUSES (ret) = clauses;
18936
18937   cp_parser_end_omp_structured_block (parser, save);
18938   add_stmt (finish_omp_structured_block (sb));
18939
18940   return ret;
18941 }
18942
18943 /* OpenMP 2.5:
18944    # pragma omp master new-line
18945      structured-block  */
18946
18947 static tree
18948 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18949 {
18950   cp_parser_require_pragma_eol (parser, pragma_tok);
18951   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18952 }
18953
18954 /* OpenMP 2.5:
18955    # pragma omp ordered new-line
18956      structured-block  */
18957
18958 static tree
18959 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18960 {
18961   cp_parser_require_pragma_eol (parser, pragma_tok);
18962   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18963 }
18964
18965 /* OpenMP 2.5:
18966
18967    section-scope:
18968      { section-sequence }
18969
18970    section-sequence:
18971      section-directive[opt] structured-block
18972      section-sequence section-directive structured-block  */
18973
18974 static tree
18975 cp_parser_omp_sections_scope (cp_parser *parser)
18976 {
18977   tree stmt, substmt;
18978   bool error_suppress = false;
18979   cp_token *tok;
18980
18981   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18982     return NULL_TREE;
18983
18984   stmt = push_stmt_list ();
18985
18986   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18987     {
18988       unsigned save;
18989
18990       substmt = begin_omp_structured_block ();
18991       save = cp_parser_begin_omp_structured_block (parser);
18992
18993       while (1)
18994         {
18995           cp_parser_statement (parser, NULL_TREE, false);
18996
18997           tok = cp_lexer_peek_token (parser->lexer);
18998           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18999             break;
19000           if (tok->type == CPP_CLOSE_BRACE)
19001             break;
19002           if (tok->type == CPP_EOF)
19003             break;
19004         }
19005
19006       cp_parser_end_omp_structured_block (parser, save);
19007       substmt = finish_omp_structured_block (substmt);
19008       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19009       add_stmt (substmt);
19010     }
19011
19012   while (1)
19013     {
19014       tok = cp_lexer_peek_token (parser->lexer);
19015       if (tok->type == CPP_CLOSE_BRACE)
19016         break;
19017       if (tok->type == CPP_EOF)
19018         break;
19019
19020       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19021         {
19022           cp_lexer_consume_token (parser->lexer);
19023           cp_parser_require_pragma_eol (parser, tok);
19024           error_suppress = false;
19025         }
19026       else if (!error_suppress)
19027         {
19028           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19029           error_suppress = true;
19030         }
19031
19032       substmt = cp_parser_omp_structured_block (parser);
19033       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19034       add_stmt (substmt);
19035     }
19036   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19037
19038   substmt = pop_stmt_list (stmt);
19039
19040   stmt = make_node (OMP_SECTIONS);
19041   TREE_TYPE (stmt) = void_type_node;
19042   OMP_SECTIONS_BODY (stmt) = substmt;
19043
19044   add_stmt (stmt);
19045   return stmt;
19046 }
19047
19048 /* OpenMP 2.5:
19049    # pragma omp sections sections-clause[optseq] newline
19050      sections-scope  */
19051
19052 #define OMP_SECTIONS_CLAUSE_MASK                        \
19053         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19054         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19055         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19056         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19057         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19058
19059 static tree
19060 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19061 {
19062   tree clauses, ret;
19063
19064   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19065                                        "#pragma omp sections", pragma_tok);
19066
19067   ret = cp_parser_omp_sections_scope (parser);
19068   if (ret)
19069     OMP_SECTIONS_CLAUSES (ret) = clauses;
19070
19071   return ret;
19072 }
19073
19074 /* OpenMP 2.5:
19075    # pragma parallel parallel-clause new-line
19076    # pragma parallel for parallel-for-clause new-line
19077    # pragma parallel sections parallel-sections-clause new-line  */
19078
19079 #define OMP_PARALLEL_CLAUSE_MASK                        \
19080         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
19081         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19082         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19083         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
19084         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
19085         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
19086         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19087         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19088
19089 static tree
19090 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19091 {
19092   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19093   const char *p_name = "#pragma omp parallel";
19094   tree stmt, clauses, par_clause, ws_clause, block;
19095   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19096   unsigned int save;
19097
19098   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19099     {
19100       cp_lexer_consume_token (parser->lexer);
19101       p_kind = PRAGMA_OMP_PARALLEL_FOR;
19102       p_name = "#pragma omp parallel for";
19103       mask |= OMP_FOR_CLAUSE_MASK;
19104       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19105     }
19106   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19107     {
19108       tree id = cp_lexer_peek_token (parser->lexer)->value;
19109       const char *p = IDENTIFIER_POINTER (id);
19110       if (strcmp (p, "sections") == 0)
19111         {
19112           cp_lexer_consume_token (parser->lexer);
19113           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19114           p_name = "#pragma omp parallel sections";
19115           mask |= OMP_SECTIONS_CLAUSE_MASK;
19116           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19117         }
19118     }
19119
19120   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19121   block = begin_omp_parallel ();
19122   save = cp_parser_begin_omp_structured_block (parser);
19123
19124   switch (p_kind)
19125     {
19126     case PRAGMA_OMP_PARALLEL:
19127       cp_parser_already_scoped_statement (parser);
19128       par_clause = clauses;
19129       break;
19130
19131     case PRAGMA_OMP_PARALLEL_FOR:
19132       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19133       stmt = cp_parser_omp_for_loop (parser);
19134       if (stmt)
19135         OMP_FOR_CLAUSES (stmt) = ws_clause;
19136       break;
19137
19138     case PRAGMA_OMP_PARALLEL_SECTIONS:
19139       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19140       stmt = cp_parser_omp_sections_scope (parser);
19141       if (stmt)
19142         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19143       break;
19144
19145     default:
19146       gcc_unreachable ();
19147     }
19148
19149   cp_parser_end_omp_structured_block (parser, save);
19150   stmt = finish_omp_parallel (par_clause, block);
19151   if (p_kind != PRAGMA_OMP_PARALLEL)
19152     OMP_PARALLEL_COMBINED (stmt) = 1;
19153   return stmt;
19154 }
19155
19156 /* OpenMP 2.5:
19157    # pragma omp single single-clause[optseq] new-line
19158      structured-block  */
19159
19160 #define OMP_SINGLE_CLAUSE_MASK                          \
19161         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19162         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19163         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
19164         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19165
19166 static tree
19167 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19168 {
19169   tree stmt = make_node (OMP_SINGLE);
19170   TREE_TYPE (stmt) = void_type_node;
19171
19172   OMP_SINGLE_CLAUSES (stmt)
19173     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19174                                  "#pragma omp single", pragma_tok);
19175   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19176
19177   return add_stmt (stmt);
19178 }
19179
19180 /* OpenMP 2.5:
19181    # pragma omp threadprivate (variable-list) */
19182
19183 static void
19184 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19185 {
19186   tree vars;
19187
19188   vars = cp_parser_omp_var_list (parser, 0, NULL);
19189   cp_parser_require_pragma_eol (parser, pragma_tok);
19190
19191   if (!targetm.have_tls)
19192     sorry ("threadprivate variables not supported in this target");
19193
19194   finish_omp_threadprivate (vars);
19195 }
19196
19197 /* Main entry point to OpenMP statement pragmas.  */
19198
19199 static void
19200 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19201 {
19202   tree stmt;
19203
19204   switch (pragma_tok->pragma_kind)
19205     {
19206     case PRAGMA_OMP_ATOMIC:
19207       cp_parser_omp_atomic (parser, pragma_tok);
19208       return;
19209     case PRAGMA_OMP_CRITICAL:
19210       stmt = cp_parser_omp_critical (parser, pragma_tok);
19211       break;
19212     case PRAGMA_OMP_FOR:
19213       stmt = cp_parser_omp_for (parser, pragma_tok);
19214       break;
19215     case PRAGMA_OMP_MASTER:
19216       stmt = cp_parser_omp_master (parser, pragma_tok);
19217       break;
19218     case PRAGMA_OMP_ORDERED:
19219       stmt = cp_parser_omp_ordered (parser, pragma_tok);
19220       break;
19221     case PRAGMA_OMP_PARALLEL:
19222       stmt = cp_parser_omp_parallel (parser, pragma_tok);
19223       break;
19224     case PRAGMA_OMP_SECTIONS:
19225       stmt = cp_parser_omp_sections (parser, pragma_tok);
19226       break;
19227     case PRAGMA_OMP_SINGLE:
19228       stmt = cp_parser_omp_single (parser, pragma_tok);
19229       break;
19230     default:
19231       gcc_unreachable ();
19232     }
19233
19234   if (stmt)
19235     SET_EXPR_LOCATION (stmt, pragma_tok->location);
19236 }
19237 \f
19238 /* The parser.  */
19239
19240 static GTY (()) cp_parser *the_parser;
19241
19242 \f
19243 /* Special handling for the first token or line in the file.  The first
19244    thing in the file might be #pragma GCC pch_preprocess, which loads a
19245    PCH file, which is a GC collection point.  So we need to handle this
19246    first pragma without benefit of an existing lexer structure.
19247
19248    Always returns one token to the caller in *FIRST_TOKEN.  This is
19249    either the true first token of the file, or the first token after
19250    the initial pragma.  */
19251
19252 static void
19253 cp_parser_initial_pragma (cp_token *first_token)
19254 {
19255   tree name = NULL;
19256
19257   cp_lexer_get_preprocessor_token (NULL, first_token);
19258   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19259     return;
19260
19261   cp_lexer_get_preprocessor_token (NULL, first_token);
19262   if (first_token->type == CPP_STRING)
19263     {
19264       name = first_token->value;
19265
19266       cp_lexer_get_preprocessor_token (NULL, first_token);
19267       if (first_token->type != CPP_PRAGMA_EOL)
19268         error ("junk at end of %<#pragma GCC pch_preprocess%>");
19269     }
19270   else
19271     error ("expected string literal");
19272
19273   /* Skip to the end of the pragma.  */
19274   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19275     cp_lexer_get_preprocessor_token (NULL, first_token);
19276
19277   /* Now actually load the PCH file.  */
19278   if (name)
19279     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19280
19281   /* Read one more token to return to our caller.  We have to do this
19282      after reading the PCH file in, since its pointers have to be
19283      live.  */
19284   cp_lexer_get_preprocessor_token (NULL, first_token);
19285 }
19286
19287 /* Normal parsing of a pragma token.  Here we can (and must) use the
19288    regular lexer.  */
19289
19290 static bool
19291 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19292 {
19293   cp_token *pragma_tok;
19294   unsigned int id;
19295
19296   pragma_tok = cp_lexer_consume_token (parser->lexer);
19297   gcc_assert (pragma_tok->type == CPP_PRAGMA);
19298   parser->lexer->in_pragma = true;
19299
19300   id = pragma_tok->pragma_kind;
19301   switch (id)
19302     {
19303     case PRAGMA_GCC_PCH_PREPROCESS:
19304       error ("%<#pragma GCC pch_preprocess%> must be first");
19305       break;
19306
19307     case PRAGMA_OMP_BARRIER:
19308       switch (context)
19309         {
19310         case pragma_compound:
19311           cp_parser_omp_barrier (parser, pragma_tok);
19312           return false;
19313         case pragma_stmt:
19314           error ("%<#pragma omp barrier%> may only be "
19315                  "used in compound statements");
19316           break;
19317         default:
19318           goto bad_stmt;
19319         }
19320       break;
19321
19322     case PRAGMA_OMP_FLUSH:
19323       switch (context)
19324         {
19325         case pragma_compound:
19326           cp_parser_omp_flush (parser, pragma_tok);
19327           return false;
19328         case pragma_stmt:
19329           error ("%<#pragma omp flush%> may only be "
19330                  "used in compound statements");
19331           break;
19332         default:
19333           goto bad_stmt;
19334         }
19335       break;
19336
19337     case PRAGMA_OMP_THREADPRIVATE:
19338       cp_parser_omp_threadprivate (parser, pragma_tok);
19339       return false;
19340
19341     case PRAGMA_OMP_ATOMIC:
19342     case PRAGMA_OMP_CRITICAL:
19343     case PRAGMA_OMP_FOR:
19344     case PRAGMA_OMP_MASTER:
19345     case PRAGMA_OMP_ORDERED:
19346     case PRAGMA_OMP_PARALLEL:
19347     case PRAGMA_OMP_SECTIONS:
19348     case PRAGMA_OMP_SINGLE:
19349       if (context == pragma_external)
19350         goto bad_stmt;
19351       cp_parser_omp_construct (parser, pragma_tok);
19352       return true;
19353
19354     case PRAGMA_OMP_SECTION:
19355       error ("%<#pragma omp section%> may only be used in "
19356              "%<#pragma omp sections%> construct");
19357       break;
19358
19359     default:
19360       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19361       c_invoke_pragma_handler (id);
19362       break;
19363
19364     bad_stmt:
19365       cp_parser_error (parser, "expected declaration specifiers");
19366       break;
19367     }
19368
19369   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19370   return false;
19371 }
19372
19373 /* The interface the pragma parsers have to the lexer.  */
19374
19375 enum cpp_ttype
19376 pragma_lex (tree *value)
19377 {
19378   cp_token *tok;
19379   enum cpp_ttype ret;
19380
19381   tok = cp_lexer_peek_token (the_parser->lexer);
19382
19383   ret = tok->type;
19384   *value = tok->value;
19385
19386   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19387     ret = CPP_EOF;
19388   else if (ret == CPP_STRING)
19389     *value = cp_parser_string_literal (the_parser, false, false);
19390   else
19391     {
19392       cp_lexer_consume_token (the_parser->lexer);
19393       if (ret == CPP_KEYWORD)
19394         ret = CPP_NAME;
19395     }
19396
19397   return ret;
19398 }
19399
19400 \f
19401 /* External interface.  */
19402
19403 /* Parse one entire translation unit.  */
19404
19405 void
19406 c_parse_file (void)
19407 {
19408   bool error_occurred;
19409   static bool already_called = false;
19410
19411   if (already_called)
19412     {
19413       sorry ("inter-module optimizations not implemented for C++");
19414       return;
19415     }
19416   already_called = true;
19417
19418   the_parser = cp_parser_new ();
19419   push_deferring_access_checks (flag_access_control
19420                                 ? dk_no_deferred : dk_no_check);
19421   error_occurred = cp_parser_translation_unit (the_parser);
19422   the_parser = NULL;
19423 }
19424
19425 #include "gt-cp-parser.h"