OSDN Git Service

PR c++/30328
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING.  If not, write to the Free
20    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41
42 \f
43 /* The lexer.  */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46    and c-lex.c) and the C++ parser.  */
47
48 /* A token's value and its associated deferred access checks and
49    qualifying scope.  */
50
51 struct tree_check GTY(())
52 {
53   /* The value associated with the token.  */
54   tree value;
55   /* The checks that have been associated with value.  */
56   VEC (deferred_access_check, gc)* checks;
57   /* The token's qualifying scope (used when it is a
58      CPP_NESTED_NAME_SPECIFIER).  */
59   tree qualifying_scope;
60 };
61
62 /* A C++ token.  */
63
64 typedef struct cp_token GTY (())
65 {
66   /* The kind of token.  */
67   ENUM_BITFIELD (cpp_ttype) type : 8;
68   /* If this token is a keyword, this value indicates which keyword.
69      Otherwise, this value is RID_MAX.  */
70   ENUM_BITFIELD (rid) keyword : 8;
71   /* Token flags.  */
72   unsigned char flags;
73   /* Identifier for the pragma.  */
74   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
75   /* True if this token is from a system header.  */
76   BOOL_BITFIELD in_system_header : 1;
77   /* True if this token is from a context where it is implicitly extern "C" */
78   BOOL_BITFIELD implicit_extern_c : 1;
79   /* True for a CPP_NAME token that is not a keyword (i.e., for which
80      KEYWORD is RID_MAX) iff this name was looked up and found to be
81      ambiguous.  An error has already been reported.  */
82   BOOL_BITFIELD ambiguous_p : 1;
83   /* The input file stack index at which this token was found.  */
84   unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
85   /* The value associated with this token, if any.  */
86   union cp_token_value {
87     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
88     struct tree_check* GTY((tag ("1"))) tree_check_value;
89     /* Use for all other tokens.  */
90     tree GTY((tag ("0"))) value;
91   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
92   /* The location at which this token was found.  */
93   location_t location;
94 } cp_token;
95
96 /* We use a stack of token pointer for saving token sets.  */
97 typedef struct cp_token *cp_token_position;
98 DEF_VEC_P (cp_token_position);
99 DEF_VEC_ALLOC_P (cp_token_position,heap);
100
101 static const cp_token eof_token =
102 {
103   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
104 #if USE_MAPPED_LOCATION
105   0
106 #else
107   {0, 0}
108 #endif
109 };
110
111 /* The cp_lexer structure represents the C++ lexer.  It is responsible
112    for managing the token stream from the preprocessor and supplying
113    it to the parser.  Tokens are never added to the cp_lexer after
114    it is created.  */
115
116 typedef struct cp_lexer GTY (())
117 {
118   /* The memory allocated for the buffer.  NULL if this lexer does not
119      own the token buffer.  */
120   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
121   /* If the lexer owns the buffer, this is the number of tokens in the
122      buffer.  */
123   size_t buffer_length;
124
125   /* A pointer just past the last available token.  The tokens
126      in this lexer are [buffer, last_token).  */
127   cp_token_position GTY ((skip)) last_token;
128
129   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
130      no more available tokens.  */
131   cp_token_position GTY ((skip)) next_token;
132
133   /* A stack indicating positions at which cp_lexer_save_tokens was
134      called.  The top entry is the most recent position at which we
135      began saving tokens.  If the stack is non-empty, we are saving
136      tokens.  */
137   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
138
139   /* The next lexer in a linked list of lexers.  */
140   struct cp_lexer *next;
141
142   /* True if we should output debugging information.  */
143   bool debugging_p;
144
145   /* True if we're in the context of parsing a pragma, and should not
146      increment past the end-of-line marker.  */
147   bool in_pragma;
148 } cp_lexer;
149
150 /* cp_token_cache is a range of tokens.  There is no need to represent
151    allocate heap memory for it, since tokens are never removed from the
152    lexer's array.  There is also no need for the GC to walk through
153    a cp_token_cache, since everything in here is referenced through
154    a lexer.  */
155
156 typedef struct cp_token_cache GTY(())
157 {
158   /* The beginning of the token range.  */
159   cp_token * GTY((skip)) first;
160
161   /* Points immediately after the last token in the range.  */
162   cp_token * GTY ((skip)) last;
163 } cp_token_cache;
164
165 /* Prototypes.  */
166
167 static cp_lexer *cp_lexer_new_main
168   (void);
169 static cp_lexer *cp_lexer_new_from_tokens
170   (cp_token_cache *tokens);
171 static void cp_lexer_destroy
172   (cp_lexer *);
173 static int cp_lexer_saving_tokens
174   (const cp_lexer *);
175 static cp_token_position cp_lexer_token_position
176   (cp_lexer *, bool);
177 static cp_token *cp_lexer_token_at
178   (cp_lexer *, cp_token_position);
179 static void cp_lexer_get_preprocessor_token
180   (cp_lexer *, cp_token *);
181 static inline cp_token *cp_lexer_peek_token
182   (cp_lexer *);
183 static cp_token *cp_lexer_peek_nth_token
184   (cp_lexer *, size_t);
185 static inline bool cp_lexer_next_token_is
186   (cp_lexer *, enum cpp_ttype);
187 static bool cp_lexer_next_token_is_not
188   (cp_lexer *, enum cpp_ttype);
189 static bool cp_lexer_next_token_is_keyword
190   (cp_lexer *, enum rid);
191 static cp_token *cp_lexer_consume_token
192   (cp_lexer *);
193 static void cp_lexer_purge_token
194   (cp_lexer *);
195 static void cp_lexer_purge_tokens_after
196   (cp_lexer *, cp_token_position);
197 static void cp_lexer_save_tokens
198   (cp_lexer *);
199 static void cp_lexer_commit_tokens
200   (cp_lexer *);
201 static void cp_lexer_rollback_tokens
202   (cp_lexer *);
203 #ifdef ENABLE_CHECKING
204 static void cp_lexer_print_token
205   (FILE *, cp_token *);
206 static inline bool cp_lexer_debugging_p
207   (cp_lexer *);
208 static void cp_lexer_start_debugging
209   (cp_lexer *) ATTRIBUTE_UNUSED;
210 static void cp_lexer_stop_debugging
211   (cp_lexer *) ATTRIBUTE_UNUSED;
212 #else
213 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
214    about passing NULL to functions that require non-NULL arguments
215    (fputs, fprintf).  It will never be used, so all we need is a value
216    of the right type that's guaranteed not to be NULL.  */
217 #define cp_lexer_debug_stream stdout
218 #define cp_lexer_print_token(str, tok) (void) 0
219 #define cp_lexer_debugging_p(lexer) 0
220 #endif /* ENABLE_CHECKING */
221
222 static cp_token_cache *cp_token_cache_new
223   (cp_token *, cp_token *);
224
225 static void cp_parser_initial_pragma
226   (cp_token *);
227
228 /* Manifest constants.  */
229 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
230 #define CP_SAVED_TOKEN_STACK 5
231
232 /* A token type for keywords, as opposed to ordinary identifiers.  */
233 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
234
235 /* A token type for template-ids.  If a template-id is processed while
236    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
237    the value of the CPP_TEMPLATE_ID is whatever was returned by
238    cp_parser_template_id.  */
239 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
240
241 /* A token type for nested-name-specifiers.  If a
242    nested-name-specifier is processed while parsing tentatively, it is
243    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
244    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
245    cp_parser_nested_name_specifier_opt.  */
246 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
247
248 /* A token type for tokens that are not tokens at all; these are used
249    to represent slots in the array where there used to be a token
250    that has now been deleted.  */
251 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
252
253 /* The number of token types, including C++-specific ones.  */
254 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
255
256 /* Variables.  */
257
258 #ifdef ENABLE_CHECKING
259 /* The stream to which debugging output should be written.  */
260 static FILE *cp_lexer_debug_stream;
261 #endif /* ENABLE_CHECKING */
262
263 /* Create a new main C++ lexer, the lexer that gets tokens from the
264    preprocessor.  */
265
266 static cp_lexer *
267 cp_lexer_new_main (void)
268 {
269   cp_token first_token;
270   cp_lexer *lexer;
271   cp_token *pos;
272   size_t alloc;
273   size_t space;
274   cp_token *buffer;
275
276   /* It's possible that parsing the first pragma will load a PCH file,
277      which is a GC collection point.  So we have to do that before
278      allocating any memory.  */
279   cp_parser_initial_pragma (&first_token);
280
281   /* Tell c_lex_with_flags not to merge string constants.  */
282   c_lex_return_raw_strings = true;
283
284   c_common_no_more_pch ();
285
286   /* Allocate the memory.  */
287   lexer = GGC_CNEW (cp_lexer);
288
289 #ifdef ENABLE_CHECKING
290   /* Initially we are not debugging.  */
291   lexer->debugging_p = false;
292 #endif /* ENABLE_CHECKING */
293   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
294                                    CP_SAVED_TOKEN_STACK);
295
296   /* Create the buffer.  */
297   alloc = CP_LEXER_BUFFER_SIZE;
298   buffer = GGC_NEWVEC (cp_token, alloc);
299
300   /* Put the first token in the buffer.  */
301   space = alloc;
302   pos = buffer;
303   *pos = first_token;
304
305   /* Get the remaining tokens from the preprocessor.  */
306   while (pos->type != CPP_EOF)
307     {
308       pos++;
309       if (!--space)
310         {
311           space = alloc;
312           alloc *= 2;
313           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
314           pos = buffer + space;
315         }
316       cp_lexer_get_preprocessor_token (lexer, pos);
317     }
318   lexer->buffer = buffer;
319   lexer->buffer_length = alloc - space;
320   lexer->last_token = pos;
321   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
322
323   /* Subsequent preprocessor diagnostics should use compiler
324      diagnostic functions to get the compiler source location.  */
325   cpp_get_options (parse_in)->client_diagnostic = true;
326   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
327
328   gcc_assert (lexer->next_token->type != CPP_PURGED);
329   return lexer;
330 }
331
332 /* Create a new lexer whose token stream is primed with the tokens in
333    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
334
335 static cp_lexer *
336 cp_lexer_new_from_tokens (cp_token_cache *cache)
337 {
338   cp_token *first = cache->first;
339   cp_token *last = cache->last;
340   cp_lexer *lexer = GGC_CNEW (cp_lexer);
341
342   /* We do not own the buffer.  */
343   lexer->buffer = NULL;
344   lexer->buffer_length = 0;
345   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
346   lexer->last_token = last;
347
348   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
349                                    CP_SAVED_TOKEN_STACK);
350
351 #ifdef ENABLE_CHECKING
352   /* Initially we are not debugging.  */
353   lexer->debugging_p = false;
354 #endif
355
356   gcc_assert (lexer->next_token->type != CPP_PURGED);
357   return lexer;
358 }
359
360 /* Frees all resources associated with LEXER.  */
361
362 static void
363 cp_lexer_destroy (cp_lexer *lexer)
364 {
365   if (lexer->buffer)
366     ggc_free (lexer->buffer);
367   VEC_free (cp_token_position, heap, lexer->saved_tokens);
368   ggc_free (lexer);
369 }
370
371 /* Returns nonzero if debugging information should be output.  */
372
373 #ifdef ENABLE_CHECKING
374
375 static inline bool
376 cp_lexer_debugging_p (cp_lexer *lexer)
377 {
378   return lexer->debugging_p;
379 }
380
381 #endif /* ENABLE_CHECKING */
382
383 static inline cp_token_position
384 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
385 {
386   gcc_assert (!previous_p || lexer->next_token != &eof_token);
387
388   return lexer->next_token - previous_p;
389 }
390
391 static inline cp_token *
392 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
393 {
394   return pos;
395 }
396
397 /* nonzero if we are presently saving tokens.  */
398
399 static inline int
400 cp_lexer_saving_tokens (const cp_lexer* lexer)
401 {
402   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
403 }
404
405 /* Store the next token from the preprocessor in *TOKEN.  Return true
406    if we reach EOF.  */
407
408 static void
409 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
410                                  cp_token *token)
411 {
412   static int is_extern_c = 0;
413
414    /* Get a new token from the preprocessor.  */
415   token->type
416     = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
417   token->input_file_stack_index = input_file_stack_tick;
418   token->keyword = RID_MAX;
419   token->pragma_kind = PRAGMA_NONE;
420   token->in_system_header = in_system_header;
421
422   /* On some systems, some header files are surrounded by an
423      implicit extern "C" block.  Set a flag in the token if it
424      comes from such a header.  */
425   is_extern_c += pending_lang_change;
426   pending_lang_change = 0;
427   token->implicit_extern_c = is_extern_c > 0;
428
429   /* Check to see if this token is a keyword.  */
430   if (token->type == CPP_NAME)
431     {
432       if (C_IS_RESERVED_WORD (token->u.value))
433         {
434           /* Mark this token as a keyword.  */
435           token->type = CPP_KEYWORD;
436           /* Record which keyword.  */
437           token->keyword = C_RID_CODE (token->u.value);
438           /* Update the value.  Some keywords are mapped to particular
439              entities, rather than simply having the value of the
440              corresponding IDENTIFIER_NODE.  For example, `__const' is
441              mapped to `const'.  */
442           token->u.value = ridpointers[token->keyword];
443         }
444       else
445         {
446           if (warn_cxx0x_compat
447               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
448               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
449             {
450               /* Warn about the C++0x keyword (but still treat it as
451                  an identifier).  */
452               warning (OPT_Wc__0x_compat, 
453                        "identifier %<%s%> will become a keyword in C++0x",
454                        IDENTIFIER_POINTER (token->u.value));
455
456               /* Clear out the C_RID_CODE so we don't warn about this
457                  particular identifier-turned-keyword again.  */
458               C_RID_CODE (token->u.value) = RID_MAX;
459             }
460
461           token->ambiguous_p = false;
462           token->keyword = RID_MAX;
463         }
464     }
465   /* Handle Objective-C++ keywords.  */
466   else if (token->type == CPP_AT_NAME)
467     {
468       token->type = CPP_KEYWORD;
469       switch (C_RID_CODE (token->u.value))
470         {
471         /* Map 'class' to '@class', 'private' to '@private', etc.  */
472         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
473         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
474         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
475         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
476         case RID_THROW: token->keyword = RID_AT_THROW; break;
477         case RID_TRY: token->keyword = RID_AT_TRY; break;
478         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
479         default: token->keyword = C_RID_CODE (token->u.value);
480         }
481     }
482   else if (token->type == CPP_PRAGMA)
483     {
484       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
485       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
486       token->u.value = NULL_TREE;
487     }
488 }
489
490 /* Update the globals input_location and in_system_header and the
491    input file stack from TOKEN.  */
492 static inline void
493 cp_lexer_set_source_position_from_token (cp_token *token)
494 {
495   if (token->type != CPP_EOF)
496     {
497       input_location = token->location;
498       in_system_header = token->in_system_header;
499       restore_input_file_stack (token->input_file_stack_index);
500     }
501 }
502
503 /* Return a pointer to the next token in the token stream, but do not
504    consume it.  */
505
506 static inline cp_token *
507 cp_lexer_peek_token (cp_lexer *lexer)
508 {
509   if (cp_lexer_debugging_p (lexer))
510     {
511       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
512       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
513       putc ('\n', cp_lexer_debug_stream);
514     }
515   return lexer->next_token;
516 }
517
518 /* Return true if the next token has the indicated TYPE.  */
519
520 static inline bool
521 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
522 {
523   return cp_lexer_peek_token (lexer)->type == type;
524 }
525
526 /* Return true if the next token does not have the indicated TYPE.  */
527
528 static inline bool
529 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
530 {
531   return !cp_lexer_next_token_is (lexer, type);
532 }
533
534 /* Return true if the next token is the indicated KEYWORD.  */
535
536 static inline bool
537 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
538 {
539   return cp_lexer_peek_token (lexer)->keyword == keyword;
540 }
541
542 /* Return true if the next token is a keyword for a decl-specifier.  */
543
544 static bool
545 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
546 {
547   cp_token *token;
548
549   token = cp_lexer_peek_token (lexer);
550   switch (token->keyword) 
551     {
552       /* Storage classes.  */
553     case RID_AUTO:
554     case RID_REGISTER:
555     case RID_STATIC:
556     case RID_EXTERN:
557     case RID_MUTABLE:
558     case RID_THREAD:
559       /* Elaborated type specifiers.  */
560     case RID_ENUM:
561     case RID_CLASS:
562     case RID_STRUCT:
563     case RID_UNION:
564     case RID_TYPENAME:
565       /* Simple type specifiers.  */
566     case RID_CHAR:
567     case RID_WCHAR:
568     case RID_BOOL:
569     case RID_SHORT:
570     case RID_INT:
571     case RID_LONG:
572     case RID_SIGNED:
573     case RID_UNSIGNED:
574     case RID_FLOAT:
575     case RID_DOUBLE:
576     case RID_VOID:
577       /* GNU extensions.  */ 
578     case RID_ATTRIBUTE:
579     case RID_TYPEOF:
580       return true;
581
582     default:
583       return false;
584     }
585 }
586
587 /* Return a pointer to the Nth token in the token stream.  If N is 1,
588    then this is precisely equivalent to cp_lexer_peek_token (except
589    that it is not inline).  One would like to disallow that case, but
590    there is one case (cp_parser_nth_token_starts_template_id) where
591    the caller passes a variable for N and it might be 1.  */
592
593 static cp_token *
594 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
595 {
596   cp_token *token;
597
598   /* N is 1-based, not zero-based.  */
599   gcc_assert (n > 0);
600
601   if (cp_lexer_debugging_p (lexer))
602     fprintf (cp_lexer_debug_stream,
603              "cp_lexer: peeking ahead %ld at token: ", (long)n);
604
605   --n;
606   token = lexer->next_token;
607   gcc_assert (!n || token != &eof_token);
608   while (n != 0)
609     {
610       ++token;
611       if (token == lexer->last_token)
612         {
613           token = (cp_token *)&eof_token;
614           break;
615         }
616
617       if (token->type != CPP_PURGED)
618         --n;
619     }
620
621   if (cp_lexer_debugging_p (lexer))
622     {
623       cp_lexer_print_token (cp_lexer_debug_stream, token);
624       putc ('\n', cp_lexer_debug_stream);
625     }
626
627   return token;
628 }
629
630 /* Return the next token, and advance the lexer's next_token pointer
631    to point to the next non-purged token.  */
632
633 static cp_token *
634 cp_lexer_consume_token (cp_lexer* lexer)
635 {
636   cp_token *token = lexer->next_token;
637
638   gcc_assert (token != &eof_token);
639   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
640
641   do
642     {
643       lexer->next_token++;
644       if (lexer->next_token == lexer->last_token)
645         {
646           lexer->next_token = (cp_token *)&eof_token;
647           break;
648         }
649
650     }
651   while (lexer->next_token->type == CPP_PURGED);
652
653   cp_lexer_set_source_position_from_token (token);
654
655   /* Provide debugging output.  */
656   if (cp_lexer_debugging_p (lexer))
657     {
658       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
659       cp_lexer_print_token (cp_lexer_debug_stream, token);
660       putc ('\n', cp_lexer_debug_stream);
661     }
662
663   return token;
664 }
665
666 /* Permanently remove the next token from the token stream, and
667    advance the next_token pointer to refer to the next non-purged
668    token.  */
669
670 static void
671 cp_lexer_purge_token (cp_lexer *lexer)
672 {
673   cp_token *tok = lexer->next_token;
674
675   gcc_assert (tok != &eof_token);
676   tok->type = CPP_PURGED;
677   tok->location = UNKNOWN_LOCATION;
678   tok->u.value = NULL_TREE;
679   tok->keyword = RID_MAX;
680
681   do
682     {
683       tok++;
684       if (tok == lexer->last_token)
685         {
686           tok = (cp_token *)&eof_token;
687           break;
688         }
689     }
690   while (tok->type == CPP_PURGED);
691   lexer->next_token = tok;
692 }
693
694 /* Permanently remove all tokens after TOK, up to, but not
695    including, the token that will be returned next by
696    cp_lexer_peek_token.  */
697
698 static void
699 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
700 {
701   cp_token *peek = lexer->next_token;
702
703   if (peek == &eof_token)
704     peek = lexer->last_token;
705
706   gcc_assert (tok < peek);
707
708   for ( tok += 1; tok != peek; tok += 1)
709     {
710       tok->type = CPP_PURGED;
711       tok->location = UNKNOWN_LOCATION;
712       tok->u.value = NULL_TREE;
713       tok->keyword = RID_MAX;
714     }
715 }
716
717 /* Begin saving tokens.  All tokens consumed after this point will be
718    preserved.  */
719
720 static void
721 cp_lexer_save_tokens (cp_lexer* lexer)
722 {
723   /* Provide debugging output.  */
724   if (cp_lexer_debugging_p (lexer))
725     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
726
727   VEC_safe_push (cp_token_position, heap,
728                  lexer->saved_tokens, lexer->next_token);
729 }
730
731 /* Commit to the portion of the token stream most recently saved.  */
732
733 static void
734 cp_lexer_commit_tokens (cp_lexer* lexer)
735 {
736   /* Provide debugging output.  */
737   if (cp_lexer_debugging_p (lexer))
738     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
739
740   VEC_pop (cp_token_position, lexer->saved_tokens);
741 }
742
743 /* Return all tokens saved since the last call to cp_lexer_save_tokens
744    to the token stream.  Stop saving tokens.  */
745
746 static void
747 cp_lexer_rollback_tokens (cp_lexer* lexer)
748 {
749   /* Provide debugging output.  */
750   if (cp_lexer_debugging_p (lexer))
751     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
752
753   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
754 }
755
756 /* Print a representation of the TOKEN on the STREAM.  */
757
758 #ifdef ENABLE_CHECKING
759
760 static void
761 cp_lexer_print_token (FILE * stream, cp_token *token)
762 {
763   /* We don't use cpp_type2name here because the parser defines
764      a few tokens of its own.  */
765   static const char *const token_names[] = {
766     /* cpplib-defined token types */
767 #define OP(e, s) #e,
768 #define TK(e, s) #e,
769     TTYPE_TABLE
770 #undef OP
771 #undef TK
772     /* C++ parser token types - see "Manifest constants", above.  */
773     "KEYWORD",
774     "TEMPLATE_ID",
775     "NESTED_NAME_SPECIFIER",
776     "PURGED"
777   };
778
779   /* If we have a name for the token, print it out.  Otherwise, we
780      simply give the numeric code.  */
781   gcc_assert (token->type < ARRAY_SIZE(token_names));
782   fputs (token_names[token->type], stream);
783
784   /* For some tokens, print the associated data.  */
785   switch (token->type)
786     {
787     case CPP_KEYWORD:
788       /* Some keywords have a value that is not an IDENTIFIER_NODE.
789          For example, `struct' is mapped to an INTEGER_CST.  */
790       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
791         break;
792       /* else fall through */
793     case CPP_NAME:
794       fputs (IDENTIFIER_POINTER (token->u.value), stream);
795       break;
796
797     case CPP_STRING:
798     case CPP_WSTRING:
799       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
800       break;
801
802     default:
803       break;
804     }
805 }
806
807 /* Start emitting debugging information.  */
808
809 static void
810 cp_lexer_start_debugging (cp_lexer* lexer)
811 {
812   lexer->debugging_p = true;
813 }
814
815 /* Stop emitting debugging information.  */
816
817 static void
818 cp_lexer_stop_debugging (cp_lexer* lexer)
819 {
820   lexer->debugging_p = false;
821 }
822
823 #endif /* ENABLE_CHECKING */
824
825 /* Create a new cp_token_cache, representing a range of tokens.  */
826
827 static cp_token_cache *
828 cp_token_cache_new (cp_token *first, cp_token *last)
829 {
830   cp_token_cache *cache = GGC_NEW (cp_token_cache);
831   cache->first = first;
832   cache->last = last;
833   return cache;
834 }
835
836 \f
837 /* Decl-specifiers.  */
838
839 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
840
841 static void
842 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
843 {
844   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 }
846
847 /* Declarators.  */
848
849 /* Nothing other than the parser should be creating declarators;
850    declarators are a semi-syntactic representation of C++ entities.
851    Other parts of the front end that need to create entities (like
852    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
853
854 static cp_declarator *make_call_declarator
855   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
856 static cp_declarator *make_array_declarator
857   (cp_declarator *, tree);
858 static cp_declarator *make_pointer_declarator
859   (cp_cv_quals, cp_declarator *);
860 static cp_declarator *make_reference_declarator
861   (cp_cv_quals, cp_declarator *);
862 static cp_parameter_declarator *make_parameter_declarator
863   (cp_decl_specifier_seq *, cp_declarator *, tree);
864 static cp_declarator *make_ptrmem_declarator
865   (cp_cv_quals, tree, cp_declarator *);
866
867 /* An erroneous declarator.  */
868 static cp_declarator *cp_error_declarator;
869
870 /* The obstack on which declarators and related data structures are
871    allocated.  */
872 static struct obstack declarator_obstack;
873
874 /* Alloc BYTES from the declarator memory pool.  */
875
876 static inline void *
877 alloc_declarator (size_t bytes)
878 {
879   return obstack_alloc (&declarator_obstack, bytes);
880 }
881
882 /* Allocate a declarator of the indicated KIND.  Clear fields that are
883    common to all declarators.  */
884
885 static cp_declarator *
886 make_declarator (cp_declarator_kind kind)
887 {
888   cp_declarator *declarator;
889
890   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
891   declarator->kind = kind;
892   declarator->attributes = NULL_TREE;
893   declarator->declarator = NULL;
894   declarator->parameter_pack_p = false;
895
896   return declarator;
897 }
898
899 /* Make a declarator for a generalized identifier.  If
900    QUALIFYING_SCOPE is non-NULL, the identifier is
901    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
902    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
903    is, if any.   */
904
905 static cp_declarator *
906 make_id_declarator (tree qualifying_scope, tree unqualified_name,
907                     special_function_kind sfk)
908 {
909   cp_declarator *declarator;
910
911   /* It is valid to write:
912
913        class C { void f(); };
914        typedef C D;
915        void D::f();
916
917      The standard is not clear about whether `typedef const C D' is
918      legal; as of 2002-09-15 the committee is considering that
919      question.  EDG 3.0 allows that syntax.  Therefore, we do as
920      well.  */
921   if (qualifying_scope && TYPE_P (qualifying_scope))
922     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
923
924   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
925               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
926               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
927
928   declarator = make_declarator (cdk_id);
929   declarator->u.id.qualifying_scope = qualifying_scope;
930   declarator->u.id.unqualified_name = unqualified_name;
931   declarator->u.id.sfk = sfk;
932   
933   return declarator;
934 }
935
936 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
937    of modifiers such as const or volatile to apply to the pointer
938    type, represented as identifiers.  */
939
940 cp_declarator *
941 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 {
943   cp_declarator *declarator;
944
945   declarator = make_declarator (cdk_pointer);
946   declarator->declarator = target;
947   declarator->u.pointer.qualifiers = cv_qualifiers;
948   declarator->u.pointer.class_type = NULL_TREE;
949   if (target)
950     {
951       declarator->parameter_pack_p = target->parameter_pack_p;
952       target->parameter_pack_p = false;
953     }
954   else
955     declarator->parameter_pack_p = false;
956
957   return declarator;
958 }
959
960 /* Like make_pointer_declarator -- but for references.  */
961
962 cp_declarator *
963 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
964 {
965   cp_declarator *declarator;
966
967   declarator = make_declarator (cdk_reference);
968   declarator->declarator = target;
969   declarator->u.pointer.qualifiers = cv_qualifiers;
970   declarator->u.pointer.class_type = NULL_TREE;
971   if (target)
972     {
973       declarator->parameter_pack_p = target->parameter_pack_p;
974       target->parameter_pack_p = false;
975     }
976   else
977     declarator->parameter_pack_p = false;
978
979   return declarator;
980 }
981
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983    member of CLASS_TYPE.  */
984
985 cp_declarator *
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987                         cp_declarator *pointee)
988 {
989   cp_declarator *declarator;
990
991   declarator = make_declarator (cdk_ptrmem);
992   declarator->declarator = pointee;
993   declarator->u.pointer.qualifiers = cv_qualifiers;
994   declarator->u.pointer.class_type = class_type;
995
996   if (pointee)
997     {
998       declarator->parameter_pack_p = pointee->parameter_pack_p;
999       pointee->parameter_pack_p = false;
1000     }
1001   else
1002     declarator->parameter_pack_p = false;
1003
1004   return declarator;
1005 }
1006
1007 /* Make a declarator for the function given by TARGET, with the
1008    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1009    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1010    indicates what exceptions can be thrown.  */
1011
1012 cp_declarator *
1013 make_call_declarator (cp_declarator *target,
1014                       cp_parameter_declarator *parms,
1015                       cp_cv_quals cv_qualifiers,
1016                       tree exception_specification)
1017 {
1018   cp_declarator *declarator;
1019
1020   declarator = make_declarator (cdk_function);
1021   declarator->declarator = target;
1022   declarator->u.function.parameters = parms;
1023   declarator->u.function.qualifiers = cv_qualifiers;
1024   declarator->u.function.exception_specification = exception_specification;
1025   if (target)
1026     {
1027       declarator->parameter_pack_p = target->parameter_pack_p;
1028       target->parameter_pack_p = false;
1029     }
1030   else
1031     declarator->parameter_pack_p = false;
1032
1033   return declarator;
1034 }
1035
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037    defined by ELEMENT.  */
1038
1039 cp_declarator *
1040 make_array_declarator (cp_declarator *element, tree bounds)
1041 {
1042   cp_declarator *declarator;
1043
1044   declarator = make_declarator (cdk_array);
1045   declarator->declarator = element;
1046   declarator->u.array.bounds = bounds;
1047   if (element)
1048     {
1049       declarator->parameter_pack_p = element->parameter_pack_p;
1050       element->parameter_pack_p = false;
1051     }
1052   else
1053     declarator->parameter_pack_p = false;
1054
1055   return declarator;
1056 }
1057
1058 cp_parameter_declarator *no_parameters;
1059
1060 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1061    DECLARATOR and DEFAULT_ARGUMENT.  */
1062
1063 cp_parameter_declarator *
1064 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1065                            cp_declarator *declarator,
1066                            tree default_argument)
1067 {
1068   cp_parameter_declarator *parameter;
1069
1070   parameter = ((cp_parameter_declarator *)
1071                alloc_declarator (sizeof (cp_parameter_declarator)));
1072   parameter->next = NULL;
1073   if (decl_specifiers)
1074     parameter->decl_specifiers = *decl_specifiers;
1075   else
1076     clear_decl_specs (&parameter->decl_specifiers);
1077   parameter->declarator = declarator;
1078   parameter->default_argument = default_argument;
1079   parameter->ellipsis_p = false;
1080
1081   return parameter;
1082 }
1083
1084 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1085
1086 static bool
1087 function_declarator_p (const cp_declarator *declarator)
1088 {
1089   while (declarator)
1090     {
1091       if (declarator->kind == cdk_function
1092           && declarator->declarator->kind == cdk_id)
1093         return true;
1094       if (declarator->kind == cdk_id
1095           || declarator->kind == cdk_error)
1096         return false;
1097       declarator = declarator->declarator;
1098     }
1099   return false;
1100 }
1101  
1102 /* The parser.  */
1103
1104 /* Overview
1105    --------
1106
1107    A cp_parser parses the token stream as specified by the C++
1108    grammar.  Its job is purely parsing, not semantic analysis.  For
1109    example, the parser breaks the token stream into declarators,
1110    expressions, statements, and other similar syntactic constructs.
1111    It does not check that the types of the expressions on either side
1112    of an assignment-statement are compatible, or that a function is
1113    not declared with a parameter of type `void'.
1114
1115    The parser invokes routines elsewhere in the compiler to perform
1116    semantic analysis and to build up the abstract syntax tree for the
1117    code processed.
1118
1119    The parser (and the template instantiation code, which is, in a
1120    way, a close relative of parsing) are the only parts of the
1121    compiler that should be calling push_scope and pop_scope, or
1122    related functions.  The parser (and template instantiation code)
1123    keeps track of what scope is presently active; everything else
1124    should simply honor that.  (The code that generates static
1125    initializers may also need to set the scope, in order to check
1126    access control correctly when emitting the initializers.)
1127
1128    Methodology
1129    -----------
1130
1131    The parser is of the standard recursive-descent variety.  Upcoming
1132    tokens in the token stream are examined in order to determine which
1133    production to use when parsing a non-terminal.  Some C++ constructs
1134    require arbitrary look ahead to disambiguate.  For example, it is
1135    impossible, in the general case, to tell whether a statement is an
1136    expression or declaration without scanning the entire statement.
1137    Therefore, the parser is capable of "parsing tentatively."  When the
1138    parser is not sure what construct comes next, it enters this mode.
1139    Then, while we attempt to parse the construct, the parser queues up
1140    error messages, rather than issuing them immediately, and saves the
1141    tokens it consumes.  If the construct is parsed successfully, the
1142    parser "commits", i.e., it issues any queued error messages and
1143    the tokens that were being preserved are permanently discarded.
1144    If, however, the construct is not parsed successfully, the parser
1145    rolls back its state completely so that it can resume parsing using
1146    a different alternative.
1147
1148    Future Improvements
1149    -------------------
1150
1151    The performance of the parser could probably be improved substantially.
1152    We could often eliminate the need to parse tentatively by looking ahead
1153    a little bit.  In some places, this approach might not entirely eliminate
1154    the need to parse tentatively, but it might still speed up the average
1155    case.  */
1156
1157 /* Flags that are passed to some parsing functions.  These values can
1158    be bitwise-ored together.  */
1159
1160 typedef enum cp_parser_flags
1161 {
1162   /* No flags.  */
1163   CP_PARSER_FLAGS_NONE = 0x0,
1164   /* The construct is optional.  If it is not present, then no error
1165      should be issued.  */
1166   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1167   /* When parsing a type-specifier, do not allow user-defined types.  */
1168   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1169 } cp_parser_flags;
1170
1171 /* The different kinds of declarators we want to parse.  */
1172
1173 typedef enum cp_parser_declarator_kind
1174 {
1175   /* We want an abstract declarator.  */
1176   CP_PARSER_DECLARATOR_ABSTRACT,
1177   /* We want a named declarator.  */
1178   CP_PARSER_DECLARATOR_NAMED,
1179   /* We don't mind, but the name must be an unqualified-id.  */
1180   CP_PARSER_DECLARATOR_EITHER
1181 } cp_parser_declarator_kind;
1182
1183 /* The precedence values used to parse binary expressions.  The minimum value
1184    of PREC must be 1, because zero is reserved to quickly discriminate
1185    binary operators from other tokens.  */
1186
1187 enum cp_parser_prec
1188 {
1189   PREC_NOT_OPERATOR,
1190   PREC_LOGICAL_OR_EXPRESSION,
1191   PREC_LOGICAL_AND_EXPRESSION,
1192   PREC_INCLUSIVE_OR_EXPRESSION,
1193   PREC_EXCLUSIVE_OR_EXPRESSION,
1194   PREC_AND_EXPRESSION,
1195   PREC_EQUALITY_EXPRESSION,
1196   PREC_RELATIONAL_EXPRESSION,
1197   PREC_SHIFT_EXPRESSION,
1198   PREC_ADDITIVE_EXPRESSION,
1199   PREC_MULTIPLICATIVE_EXPRESSION,
1200   PREC_PM_EXPRESSION,
1201   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1202 };
1203
1204 /* A mapping from a token type to a corresponding tree node type, with a
1205    precedence value.  */
1206
1207 typedef struct cp_parser_binary_operations_map_node
1208 {
1209   /* The token type.  */
1210   enum cpp_ttype token_type;
1211   /* The corresponding tree code.  */
1212   enum tree_code tree_type;
1213   /* The precedence of this operator.  */
1214   enum cp_parser_prec prec;
1215 } cp_parser_binary_operations_map_node;
1216
1217 /* The status of a tentative parse.  */
1218
1219 typedef enum cp_parser_status_kind
1220 {
1221   /* No errors have occurred.  */
1222   CP_PARSER_STATUS_KIND_NO_ERROR,
1223   /* An error has occurred.  */
1224   CP_PARSER_STATUS_KIND_ERROR,
1225   /* We are committed to this tentative parse, whether or not an error
1226      has occurred.  */
1227   CP_PARSER_STATUS_KIND_COMMITTED
1228 } cp_parser_status_kind;
1229
1230 typedef struct cp_parser_expression_stack_entry
1231 {
1232   /* Left hand side of the binary operation we are currently
1233      parsing.  */
1234   tree lhs;
1235   /* Original tree code for left hand side, if it was a binary
1236      expression itself (used for -Wparentheses).  */
1237   enum tree_code lhs_type;
1238   /* Tree code for the binary operation we are parsing.  */
1239   enum tree_code tree_type;
1240   /* Precedence of the binary operation we are parsing.  */
1241   int prec;
1242 } cp_parser_expression_stack_entry;
1243
1244 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1245    entries because precedence levels on the stack are monotonically
1246    increasing.  */
1247 typedef struct cp_parser_expression_stack_entry
1248   cp_parser_expression_stack[NUM_PREC_VALUES];
1249
1250 /* Context that is saved and restored when parsing tentatively.  */
1251 typedef struct cp_parser_context GTY (())
1252 {
1253   /* If this is a tentative parsing context, the status of the
1254      tentative parse.  */
1255   enum cp_parser_status_kind status;
1256   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1257      that are looked up in this context must be looked up both in the
1258      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1259      the context of the containing expression.  */
1260   tree object_type;
1261
1262   /* The next parsing context in the stack.  */
1263   struct cp_parser_context *next;
1264 } cp_parser_context;
1265
1266 /* Prototypes.  */
1267
1268 /* Constructors and destructors.  */
1269
1270 static cp_parser_context *cp_parser_context_new
1271   (cp_parser_context *);
1272
1273 /* Class variables.  */
1274
1275 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1276
1277 /* The operator-precedence table used by cp_parser_binary_expression.
1278    Transformed into an associative array (binops_by_token) by
1279    cp_parser_new.  */
1280
1281 static const cp_parser_binary_operations_map_node binops[] = {
1282   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1283   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1284
1285   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1286   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1287   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1288
1289   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1290   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1291
1292   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1293   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1294
1295   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1296   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1297   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1298   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1299
1300   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1301   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1302
1303   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1304
1305   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1306
1307   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1308
1309   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1310
1311   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1312 };
1313
1314 /* The same as binops, but initialized by cp_parser_new so that
1315    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1316    for speed.  */
1317 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1318
1319 /* Constructors and destructors.  */
1320
1321 /* Construct a new context.  The context below this one on the stack
1322    is given by NEXT.  */
1323
1324 static cp_parser_context *
1325 cp_parser_context_new (cp_parser_context* next)
1326 {
1327   cp_parser_context *context;
1328
1329   /* Allocate the storage.  */
1330   if (cp_parser_context_free_list != NULL)
1331     {
1332       /* Pull the first entry from the free list.  */
1333       context = cp_parser_context_free_list;
1334       cp_parser_context_free_list = context->next;
1335       memset (context, 0, sizeof (*context));
1336     }
1337   else
1338     context = GGC_CNEW (cp_parser_context);
1339
1340   /* No errors have occurred yet in this context.  */
1341   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1342   /* If this is not the bottomost context, copy information that we
1343      need from the previous context.  */
1344   if (next)
1345     {
1346       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1347          expression, then we are parsing one in this context, too.  */
1348       context->object_type = next->object_type;
1349       /* Thread the stack.  */
1350       context->next = next;
1351     }
1352
1353   return context;
1354 }
1355
1356 /* The cp_parser structure represents the C++ parser.  */
1357
1358 typedef struct cp_parser GTY(())
1359 {
1360   /* The lexer from which we are obtaining tokens.  */
1361   cp_lexer *lexer;
1362
1363   /* The scope in which names should be looked up.  If NULL_TREE, then
1364      we look up names in the scope that is currently open in the
1365      source program.  If non-NULL, this is either a TYPE or
1366      NAMESPACE_DECL for the scope in which we should look.  It can
1367      also be ERROR_MARK, when we've parsed a bogus scope.
1368
1369      This value is not cleared automatically after a name is looked
1370      up, so we must be careful to clear it before starting a new look
1371      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1372      will look up `Z' in the scope of `X', rather than the current
1373      scope.)  Unfortunately, it is difficult to tell when name lookup
1374      is complete, because we sometimes peek at a token, look it up,
1375      and then decide not to consume it.   */
1376   tree scope;
1377
1378   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1379      last lookup took place.  OBJECT_SCOPE is used if an expression
1380      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1381      respectively.  QUALIFYING_SCOPE is used for an expression of the
1382      form "X::Y"; it refers to X.  */
1383   tree object_scope;
1384   tree qualifying_scope;
1385
1386   /* A stack of parsing contexts.  All but the bottom entry on the
1387      stack will be tentative contexts.
1388
1389      We parse tentatively in order to determine which construct is in
1390      use in some situations.  For example, in order to determine
1391      whether a statement is an expression-statement or a
1392      declaration-statement we parse it tentatively as a
1393      declaration-statement.  If that fails, we then reparse the same
1394      token stream as an expression-statement.  */
1395   cp_parser_context *context;
1396
1397   /* True if we are parsing GNU C++.  If this flag is not set, then
1398      GNU extensions are not recognized.  */
1399   bool allow_gnu_extensions_p;
1400
1401   /* TRUE if the `>' token should be interpreted as the greater-than
1402      operator.  FALSE if it is the end of a template-id or
1403      template-parameter-list.  */
1404   bool greater_than_is_operator_p;
1405
1406   /* TRUE if default arguments are allowed within a parameter list
1407      that starts at this point. FALSE if only a gnu extension makes
1408      them permissible.  */
1409   bool default_arg_ok_p;
1410
1411   /* TRUE if we are parsing an integral constant-expression.  See
1412      [expr.const] for a precise definition.  */
1413   bool integral_constant_expression_p;
1414
1415   /* TRUE if we are parsing an integral constant-expression -- but a
1416      non-constant expression should be permitted as well.  This flag
1417      is used when parsing an array bound so that GNU variable-length
1418      arrays are tolerated.  */
1419   bool allow_non_integral_constant_expression_p;
1420
1421   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1422      been seen that makes the expression non-constant.  */
1423   bool non_integral_constant_expression_p;
1424
1425   /* TRUE if local variable names and `this' are forbidden in the
1426      current context.  */
1427   bool local_variables_forbidden_p;
1428
1429   /* TRUE if the declaration we are parsing is part of a
1430      linkage-specification of the form `extern string-literal
1431      declaration'.  */
1432   bool in_unbraced_linkage_specification_p;
1433
1434   /* TRUE if we are presently parsing a declarator, after the
1435      direct-declarator.  */
1436   bool in_declarator_p;
1437
1438   /* TRUE if we are presently parsing a template-argument-list.  */
1439   bool in_template_argument_list_p;
1440
1441   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1442      to IN_OMP_BLOCK if parsing OpenMP structured block and
1443      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1444      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1445      iteration-statement, OpenMP block or loop within that switch.  */
1446 #define IN_SWITCH_STMT          1
1447 #define IN_ITERATION_STMT       2
1448 #define IN_OMP_BLOCK            4
1449 #define IN_OMP_FOR              8
1450 #define IN_IF_STMT             16
1451   unsigned char in_statement;
1452
1453   /* TRUE if we are presently parsing the body of a switch statement.
1454      Note that this doesn't quite overlap with in_statement above.
1455      The difference relates to giving the right sets of error messages:
1456      "case not in switch" vs "break statement used with OpenMP...".  */
1457   bool in_switch_statement_p;
1458
1459   /* TRUE if we are parsing a type-id in an expression context.  In
1460      such a situation, both "type (expr)" and "type (type)" are valid
1461      alternatives.  */
1462   bool in_type_id_in_expr_p;
1463
1464   /* TRUE if we are currently in a header file where declarations are
1465      implicitly extern "C".  */
1466   bool implicit_extern_c;
1467
1468   /* TRUE if strings in expressions should be translated to the execution
1469      character set.  */
1470   bool translate_strings_p;
1471
1472   /* TRUE if we are presently parsing the body of a function, but not
1473      a local class.  */
1474   bool in_function_body;
1475
1476   /* If non-NULL, then we are parsing a construct where new type
1477      definitions are not permitted.  The string stored here will be
1478      issued as an error message if a type is defined.  */
1479   const char *type_definition_forbidden_message;
1480
1481   /* A list of lists. The outer list is a stack, used for member
1482      functions of local classes. At each level there are two sub-list,
1483      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1484      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1485      TREE_VALUE's. The functions are chained in reverse declaration
1486      order.
1487
1488      The TREE_PURPOSE sublist contains those functions with default
1489      arguments that need post processing, and the TREE_VALUE sublist
1490      contains those functions with definitions that need post
1491      processing.
1492
1493      These lists can only be processed once the outermost class being
1494      defined is complete.  */
1495   tree unparsed_functions_queues;
1496
1497   /* The number of classes whose definitions are currently in
1498      progress.  */
1499   unsigned num_classes_being_defined;
1500
1501   /* The number of template parameter lists that apply directly to the
1502      current declaration.  */
1503   unsigned num_template_parameter_lists;
1504 } cp_parser;
1505
1506 /* Prototypes.  */
1507
1508 /* Constructors and destructors.  */
1509
1510 static cp_parser *cp_parser_new
1511   (void);
1512
1513 /* Routines to parse various constructs.
1514
1515    Those that return `tree' will return the error_mark_node (rather
1516    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1517    Sometimes, they will return an ordinary node if error-recovery was
1518    attempted, even though a parse error occurred.  So, to check
1519    whether or not a parse error occurred, you should always use
1520    cp_parser_error_occurred.  If the construct is optional (indicated
1521    either by an `_opt' in the name of the function that does the
1522    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1523    the construct is not present.  */
1524
1525 /* Lexical conventions [gram.lex]  */
1526
1527 static tree cp_parser_identifier
1528   (cp_parser *);
1529 static tree cp_parser_string_literal
1530   (cp_parser *, bool, bool);
1531
1532 /* Basic concepts [gram.basic]  */
1533
1534 static bool cp_parser_translation_unit
1535   (cp_parser *);
1536
1537 /* Expressions [gram.expr]  */
1538
1539 static tree cp_parser_primary_expression
1540   (cp_parser *, bool, bool, bool, cp_id_kind *);
1541 static tree cp_parser_id_expression
1542   (cp_parser *, bool, bool, bool *, bool, bool);
1543 static tree cp_parser_unqualified_id
1544   (cp_parser *, bool, bool, bool, bool);
1545 static tree cp_parser_nested_name_specifier_opt
1546   (cp_parser *, bool, bool, bool, bool);
1547 static tree cp_parser_nested_name_specifier
1548   (cp_parser *, bool, bool, bool, bool);
1549 static tree cp_parser_class_or_namespace_name
1550   (cp_parser *, bool, bool, bool, bool, bool);
1551 static tree cp_parser_postfix_expression
1552   (cp_parser *, bool, bool);
1553 static tree cp_parser_postfix_open_square_expression
1554   (cp_parser *, tree, bool);
1555 static tree cp_parser_postfix_dot_deref_expression
1556   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1557 static tree cp_parser_parenthesized_expression_list
1558   (cp_parser *, bool, bool, bool, bool *);
1559 static void cp_parser_pseudo_destructor_name
1560   (cp_parser *, tree *, tree *);
1561 static tree cp_parser_unary_expression
1562   (cp_parser *, bool, bool);
1563 static enum tree_code cp_parser_unary_operator
1564   (cp_token *);
1565 static tree cp_parser_new_expression
1566   (cp_parser *);
1567 static tree cp_parser_new_placement
1568   (cp_parser *);
1569 static tree cp_parser_new_type_id
1570   (cp_parser *, tree *);
1571 static cp_declarator *cp_parser_new_declarator_opt
1572   (cp_parser *);
1573 static cp_declarator *cp_parser_direct_new_declarator
1574   (cp_parser *);
1575 static tree cp_parser_new_initializer
1576   (cp_parser *);
1577 static tree cp_parser_delete_expression
1578   (cp_parser *);
1579 static tree cp_parser_cast_expression
1580   (cp_parser *, bool, bool);
1581 static tree cp_parser_binary_expression
1582   (cp_parser *, bool);
1583 static tree cp_parser_question_colon_clause
1584   (cp_parser *, tree);
1585 static tree cp_parser_assignment_expression
1586   (cp_parser *, bool);
1587 static enum tree_code cp_parser_assignment_operator_opt
1588   (cp_parser *);
1589 static tree cp_parser_expression
1590   (cp_parser *, bool);
1591 static tree cp_parser_constant_expression
1592   (cp_parser *, bool, bool *);
1593 static tree cp_parser_builtin_offsetof
1594   (cp_parser *);
1595
1596 /* Statements [gram.stmt.stmt]  */
1597
1598 static void cp_parser_statement
1599   (cp_parser *, tree, bool, bool *);
1600 static void cp_parser_label_for_labeled_statement
1601   (cp_parser *);
1602 static tree cp_parser_expression_statement
1603   (cp_parser *, tree);
1604 static tree cp_parser_compound_statement
1605   (cp_parser *, tree, bool);
1606 static void cp_parser_statement_seq_opt
1607   (cp_parser *, tree);
1608 static tree cp_parser_selection_statement
1609   (cp_parser *, bool *);
1610 static tree cp_parser_condition
1611   (cp_parser *);
1612 static tree cp_parser_iteration_statement
1613   (cp_parser *);
1614 static void cp_parser_for_init_statement
1615   (cp_parser *);
1616 static tree cp_parser_jump_statement
1617   (cp_parser *);
1618 static void cp_parser_declaration_statement
1619   (cp_parser *);
1620
1621 static tree cp_parser_implicitly_scoped_statement
1622   (cp_parser *, bool *);
1623 static void cp_parser_already_scoped_statement
1624   (cp_parser *);
1625
1626 /* Declarations [gram.dcl.dcl] */
1627
1628 static void cp_parser_declaration_seq_opt
1629   (cp_parser *);
1630 static void cp_parser_declaration
1631   (cp_parser *);
1632 static void cp_parser_block_declaration
1633   (cp_parser *, bool);
1634 static void cp_parser_simple_declaration
1635   (cp_parser *, bool);
1636 static void cp_parser_decl_specifier_seq
1637   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1638 static tree cp_parser_storage_class_specifier_opt
1639   (cp_parser *);
1640 static tree cp_parser_function_specifier_opt
1641   (cp_parser *, cp_decl_specifier_seq *);
1642 static tree cp_parser_type_specifier
1643   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1644    int *, bool *);
1645 static tree cp_parser_simple_type_specifier
1646   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1647 static tree cp_parser_type_name
1648   (cp_parser *);
1649 static tree cp_parser_elaborated_type_specifier
1650   (cp_parser *, bool, bool);
1651 static tree cp_parser_enum_specifier
1652   (cp_parser *);
1653 static void cp_parser_enumerator_list
1654   (cp_parser *, tree);
1655 static void cp_parser_enumerator_definition
1656   (cp_parser *, tree);
1657 static tree cp_parser_namespace_name
1658   (cp_parser *);
1659 static void cp_parser_namespace_definition
1660   (cp_parser *);
1661 static void cp_parser_namespace_body
1662   (cp_parser *);
1663 static tree cp_parser_qualified_namespace_specifier
1664   (cp_parser *);
1665 static void cp_parser_namespace_alias_definition
1666   (cp_parser *);
1667 static bool cp_parser_using_declaration
1668   (cp_parser *, bool);
1669 static void cp_parser_using_directive
1670   (cp_parser *);
1671 static void cp_parser_asm_definition
1672   (cp_parser *);
1673 static void cp_parser_linkage_specification
1674   (cp_parser *);
1675 static void cp_parser_static_assert
1676   (cp_parser *, bool);
1677
1678 /* Declarators [gram.dcl.decl] */
1679
1680 static tree cp_parser_init_declarator
1681   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1682 static cp_declarator *cp_parser_declarator
1683   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1684 static cp_declarator *cp_parser_direct_declarator
1685   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1686 static enum tree_code cp_parser_ptr_operator
1687   (cp_parser *, tree *, cp_cv_quals *);
1688 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1689   (cp_parser *);
1690 static tree cp_parser_declarator_id
1691   (cp_parser *, bool);
1692 static tree cp_parser_type_id
1693   (cp_parser *);
1694 static void cp_parser_type_specifier_seq
1695   (cp_parser *, bool, cp_decl_specifier_seq *);
1696 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1697   (cp_parser *);
1698 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1699   (cp_parser *, bool *);
1700 static cp_parameter_declarator *cp_parser_parameter_declaration
1701   (cp_parser *, bool, bool *);
1702 static void cp_parser_function_body
1703   (cp_parser *);
1704 static tree cp_parser_initializer
1705   (cp_parser *, bool *, bool *);
1706 static tree cp_parser_initializer_clause
1707   (cp_parser *, bool *);
1708 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1709   (cp_parser *, bool *);
1710
1711 static bool cp_parser_ctor_initializer_opt_and_function_body
1712   (cp_parser *);
1713
1714 /* Classes [gram.class] */
1715
1716 static tree cp_parser_class_name
1717   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1718 static tree cp_parser_class_specifier
1719   (cp_parser *);
1720 static tree cp_parser_class_head
1721   (cp_parser *, bool *, tree *, tree *);
1722 static enum tag_types cp_parser_class_key
1723   (cp_parser *);
1724 static void cp_parser_member_specification_opt
1725   (cp_parser *);
1726 static void cp_parser_member_declaration
1727   (cp_parser *);
1728 static tree cp_parser_pure_specifier
1729   (cp_parser *);
1730 static tree cp_parser_constant_initializer
1731   (cp_parser *);
1732
1733 /* Derived classes [gram.class.derived] */
1734
1735 static tree cp_parser_base_clause
1736   (cp_parser *);
1737 static tree cp_parser_base_specifier
1738   (cp_parser *);
1739
1740 /* Special member functions [gram.special] */
1741
1742 static tree cp_parser_conversion_function_id
1743   (cp_parser *);
1744 static tree cp_parser_conversion_type_id
1745   (cp_parser *);
1746 static cp_declarator *cp_parser_conversion_declarator_opt
1747   (cp_parser *);
1748 static bool cp_parser_ctor_initializer_opt
1749   (cp_parser *);
1750 static void cp_parser_mem_initializer_list
1751   (cp_parser *);
1752 static tree cp_parser_mem_initializer
1753   (cp_parser *);
1754 static tree cp_parser_mem_initializer_id
1755   (cp_parser *);
1756
1757 /* Overloading [gram.over] */
1758
1759 static tree cp_parser_operator_function_id
1760   (cp_parser *);
1761 static tree cp_parser_operator
1762   (cp_parser *);
1763
1764 /* Templates [gram.temp] */
1765
1766 static void cp_parser_template_declaration
1767   (cp_parser *, bool);
1768 static tree cp_parser_template_parameter_list
1769   (cp_parser *);
1770 static tree cp_parser_template_parameter
1771   (cp_parser *, bool *, bool *);
1772 static tree cp_parser_type_parameter
1773   (cp_parser *, bool *);
1774 static tree cp_parser_template_id
1775   (cp_parser *, bool, bool, bool);
1776 static tree cp_parser_template_name
1777   (cp_parser *, bool, bool, bool, bool *);
1778 static tree cp_parser_template_argument_list
1779   (cp_parser *);
1780 static tree cp_parser_template_argument
1781   (cp_parser *);
1782 static void cp_parser_explicit_instantiation
1783   (cp_parser *);
1784 static void cp_parser_explicit_specialization
1785   (cp_parser *);
1786
1787 /* Exception handling [gram.exception] */
1788
1789 static tree cp_parser_try_block
1790   (cp_parser *);
1791 static bool cp_parser_function_try_block
1792   (cp_parser *);
1793 static void cp_parser_handler_seq
1794   (cp_parser *);
1795 static void cp_parser_handler
1796   (cp_parser *);
1797 static tree cp_parser_exception_declaration
1798   (cp_parser *);
1799 static tree cp_parser_throw_expression
1800   (cp_parser *);
1801 static tree cp_parser_exception_specification_opt
1802   (cp_parser *);
1803 static tree cp_parser_type_id_list
1804   (cp_parser *);
1805
1806 /* GNU Extensions */
1807
1808 static tree cp_parser_asm_specification_opt
1809   (cp_parser *);
1810 static tree cp_parser_asm_operand_list
1811   (cp_parser *);
1812 static tree cp_parser_asm_clobber_list
1813   (cp_parser *);
1814 static tree cp_parser_attributes_opt
1815   (cp_parser *);
1816 static tree cp_parser_attribute_list
1817   (cp_parser *);
1818 static bool cp_parser_extension_opt
1819   (cp_parser *, int *);
1820 static void cp_parser_label_declaration
1821   (cp_parser *);
1822
1823 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1824 static bool cp_parser_pragma
1825   (cp_parser *, enum pragma_context);
1826
1827 /* Objective-C++ Productions */
1828
1829 static tree cp_parser_objc_message_receiver
1830   (cp_parser *);
1831 static tree cp_parser_objc_message_args
1832   (cp_parser *);
1833 static tree cp_parser_objc_message_expression
1834   (cp_parser *);
1835 static tree cp_parser_objc_encode_expression
1836   (cp_parser *);
1837 static tree cp_parser_objc_defs_expression
1838   (cp_parser *);
1839 static tree cp_parser_objc_protocol_expression
1840   (cp_parser *);
1841 static tree cp_parser_objc_selector_expression
1842   (cp_parser *);
1843 static tree cp_parser_objc_expression
1844   (cp_parser *);
1845 static bool cp_parser_objc_selector_p
1846   (enum cpp_ttype);
1847 static tree cp_parser_objc_selector
1848   (cp_parser *);
1849 static tree cp_parser_objc_protocol_refs_opt
1850   (cp_parser *);
1851 static void cp_parser_objc_declaration
1852   (cp_parser *);
1853 static tree cp_parser_objc_statement
1854   (cp_parser *);
1855
1856 /* Utility Routines */
1857
1858 static tree cp_parser_lookup_name
1859   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1860 static tree cp_parser_lookup_name_simple
1861   (cp_parser *, tree);
1862 static tree cp_parser_maybe_treat_template_as_class
1863   (tree, bool);
1864 static bool cp_parser_check_declarator_template_parameters
1865   (cp_parser *, cp_declarator *);
1866 static bool cp_parser_check_template_parameters
1867   (cp_parser *, unsigned);
1868 static tree cp_parser_simple_cast_expression
1869   (cp_parser *);
1870 static tree cp_parser_global_scope_opt
1871   (cp_parser *, bool);
1872 static bool cp_parser_constructor_declarator_p
1873   (cp_parser *, bool);
1874 static tree cp_parser_function_definition_from_specifiers_and_declarator
1875   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1876 static tree cp_parser_function_definition_after_declarator
1877   (cp_parser *, bool);
1878 static void cp_parser_template_declaration_after_export
1879   (cp_parser *, bool);
1880 static void cp_parser_perform_template_parameter_access_checks
1881   (VEC (deferred_access_check,gc)*);
1882 static tree cp_parser_single_declaration
1883   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1884 static tree cp_parser_functional_cast
1885   (cp_parser *, tree);
1886 static tree cp_parser_save_member_function_body
1887   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1888 static tree cp_parser_enclosed_template_argument_list
1889   (cp_parser *);
1890 static void cp_parser_save_default_args
1891   (cp_parser *, tree);
1892 static void cp_parser_late_parsing_for_member
1893   (cp_parser *, tree);
1894 static void cp_parser_late_parsing_default_args
1895   (cp_parser *, tree);
1896 static tree cp_parser_sizeof_operand
1897   (cp_parser *, enum rid);
1898 static bool cp_parser_declares_only_class_p
1899   (cp_parser *);
1900 static void cp_parser_set_storage_class
1901   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1902 static void cp_parser_set_decl_spec_type
1903   (cp_decl_specifier_seq *, tree, bool);
1904 static bool cp_parser_friend_p
1905   (const cp_decl_specifier_seq *);
1906 static cp_token *cp_parser_require
1907   (cp_parser *, enum cpp_ttype, const char *);
1908 static cp_token *cp_parser_require_keyword
1909   (cp_parser *, enum rid, const char *);
1910 static bool cp_parser_token_starts_function_definition_p
1911   (cp_token *);
1912 static bool cp_parser_next_token_starts_class_definition_p
1913   (cp_parser *);
1914 static bool cp_parser_next_token_ends_template_argument_p
1915   (cp_parser *);
1916 static bool cp_parser_nth_token_starts_template_argument_list_p
1917   (cp_parser *, size_t);
1918 static enum tag_types cp_parser_token_is_class_key
1919   (cp_token *);
1920 static void cp_parser_check_class_key
1921   (enum tag_types, tree type);
1922 static void cp_parser_check_access_in_redeclaration
1923   (tree type);
1924 static bool cp_parser_optional_template_keyword
1925   (cp_parser *);
1926 static void cp_parser_pre_parsed_nested_name_specifier
1927   (cp_parser *);
1928 static void cp_parser_cache_group
1929   (cp_parser *, enum cpp_ttype, unsigned);
1930 static void cp_parser_parse_tentatively
1931   (cp_parser *);
1932 static void cp_parser_commit_to_tentative_parse
1933   (cp_parser *);
1934 static void cp_parser_abort_tentative_parse
1935   (cp_parser *);
1936 static bool cp_parser_parse_definitely
1937   (cp_parser *);
1938 static inline bool cp_parser_parsing_tentatively
1939   (cp_parser *);
1940 static bool cp_parser_uncommitted_to_tentative_parse_p
1941   (cp_parser *);
1942 static void cp_parser_error
1943   (cp_parser *, const char *);
1944 static void cp_parser_name_lookup_error
1945   (cp_parser *, tree, tree, const char *);
1946 static bool cp_parser_simulate_error
1947   (cp_parser *);
1948 static bool cp_parser_check_type_definition
1949   (cp_parser *);
1950 static void cp_parser_check_for_definition_in_return_type
1951   (cp_declarator *, tree);
1952 static void cp_parser_check_for_invalid_template_id
1953   (cp_parser *, tree);
1954 static bool cp_parser_non_integral_constant_expression
1955   (cp_parser *, const char *);
1956 static void cp_parser_diagnose_invalid_type_name
1957   (cp_parser *, tree, tree);
1958 static bool cp_parser_parse_and_diagnose_invalid_type_name
1959   (cp_parser *);
1960 static int cp_parser_skip_to_closing_parenthesis
1961   (cp_parser *, bool, bool, bool);
1962 static void cp_parser_skip_to_end_of_statement
1963   (cp_parser *);
1964 static void cp_parser_consume_semicolon_at_end_of_statement
1965   (cp_parser *);
1966 static void cp_parser_skip_to_end_of_block_or_statement
1967   (cp_parser *);
1968 static void cp_parser_skip_to_closing_brace
1969   (cp_parser *);
1970 static void cp_parser_skip_to_end_of_template_parameter_list
1971   (cp_parser *);
1972 static void cp_parser_skip_to_pragma_eol
1973   (cp_parser*, cp_token *);
1974 static bool cp_parser_error_occurred
1975   (cp_parser *);
1976 static bool cp_parser_allow_gnu_extensions_p
1977   (cp_parser *);
1978 static bool cp_parser_is_string_literal
1979   (cp_token *);
1980 static bool cp_parser_is_keyword
1981   (cp_token *, enum rid);
1982 static tree cp_parser_make_typename_type
1983   (cp_parser *, tree, tree);
1984
1985 /* Returns nonzero if we are parsing tentatively.  */
1986
1987 static inline bool
1988 cp_parser_parsing_tentatively (cp_parser* parser)
1989 {
1990   return parser->context->next != NULL;
1991 }
1992
1993 /* Returns nonzero if TOKEN is a string literal.  */
1994
1995 static bool
1996 cp_parser_is_string_literal (cp_token* token)
1997 {
1998   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1999 }
2000
2001 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2002
2003 static bool
2004 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2005 {
2006   return token->keyword == keyword;
2007 }
2008
2009 /* If not parsing tentatively, issue a diagnostic of the form
2010       FILE:LINE: MESSAGE before TOKEN
2011    where TOKEN is the next token in the input stream.  MESSAGE
2012    (specified by the caller) is usually of the form "expected
2013    OTHER-TOKEN".  */
2014
2015 static void
2016 cp_parser_error (cp_parser* parser, const char* message)
2017 {
2018   if (!cp_parser_simulate_error (parser))
2019     {
2020       cp_token *token = cp_lexer_peek_token (parser->lexer);
2021       /* This diagnostic makes more sense if it is tagged to the line
2022          of the token we just peeked at.  */
2023       cp_lexer_set_source_position_from_token (token);
2024
2025       if (token->type == CPP_PRAGMA)
2026         {
2027           error ("%<#pragma%> is not allowed here");
2028           cp_parser_skip_to_pragma_eol (parser, token);
2029           return;
2030         }
2031
2032       c_parse_error (message,
2033                      /* Because c_parser_error does not understand
2034                         CPP_KEYWORD, keywords are treated like
2035                         identifiers.  */
2036                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2037                      token->u.value);
2038     }
2039 }
2040
2041 /* Issue an error about name-lookup failing.  NAME is the
2042    IDENTIFIER_NODE DECL is the result of
2043    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2044    the thing that we hoped to find.  */
2045
2046 static void
2047 cp_parser_name_lookup_error (cp_parser* parser,
2048                              tree name,
2049                              tree decl,
2050                              const char* desired)
2051 {
2052   /* If name lookup completely failed, tell the user that NAME was not
2053      declared.  */
2054   if (decl == error_mark_node)
2055     {
2056       if (parser->scope && parser->scope != global_namespace)
2057         error ("%<%D::%D%> has not been declared",
2058                parser->scope, name);
2059       else if (parser->scope == global_namespace)
2060         error ("%<::%D%> has not been declared", name);
2061       else if (parser->object_scope
2062                && !CLASS_TYPE_P (parser->object_scope))
2063         error ("request for member %qD in non-class type %qT",
2064                name, parser->object_scope);
2065       else if (parser->object_scope)
2066         error ("%<%T::%D%> has not been declared",
2067                parser->object_scope, name);
2068       else
2069         error ("%qD has not been declared", name);
2070     }
2071   else if (parser->scope && parser->scope != global_namespace)
2072     error ("%<%D::%D%> %s", parser->scope, name, desired);
2073   else if (parser->scope == global_namespace)
2074     error ("%<::%D%> %s", name, desired);
2075   else
2076     error ("%qD %s", name, desired);
2077 }
2078
2079 /* If we are parsing tentatively, remember that an error has occurred
2080    during this tentative parse.  Returns true if the error was
2081    simulated; false if a message should be issued by the caller.  */
2082
2083 static bool
2084 cp_parser_simulate_error (cp_parser* parser)
2085 {
2086   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2087     {
2088       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2089       return true;
2090     }
2091   return false;
2092 }
2093
2094 /* Check for repeated decl-specifiers.  */
2095
2096 static void
2097 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2098 {
2099   cp_decl_spec ds;
2100
2101   for (ds = ds_first; ds != ds_last; ++ds)
2102     {
2103       unsigned count = decl_specs->specs[(int)ds];
2104       if (count < 2)
2105         continue;
2106       /* The "long" specifier is a special case because of "long long".  */
2107       if (ds == ds_long)
2108         {
2109           if (count > 2)
2110             error ("%<long long long%> is too long for GCC");
2111           else if (pedantic && !in_system_header && warn_long_long)
2112             pedwarn ("ISO C++ does not support %<long long%>");
2113         }
2114       else if (count > 1)
2115         {
2116           static const char *const decl_spec_names[] = {
2117             "signed",
2118             "unsigned",
2119             "short",
2120             "long",
2121             "const",
2122             "volatile",
2123             "restrict",
2124             "inline",
2125             "virtual",
2126             "explicit",
2127             "friend",
2128             "typedef",
2129             "__complex",
2130             "__thread"
2131           };
2132           error ("duplicate %qs", decl_spec_names[(int)ds]);
2133         }
2134     }
2135 }
2136
2137 /* This function is called when a type is defined.  If type
2138    definitions are forbidden at this point, an error message is
2139    issued.  */
2140
2141 static bool
2142 cp_parser_check_type_definition (cp_parser* parser)
2143 {
2144   /* If types are forbidden here, issue a message.  */
2145   if (parser->type_definition_forbidden_message)
2146     {
2147       /* Use `%s' to print the string in case there are any escape
2148          characters in the message.  */
2149       error ("%s", parser->type_definition_forbidden_message);
2150       return false;
2151     }
2152   return true;
2153 }
2154
2155 /* This function is called when the DECLARATOR is processed.  The TYPE
2156    was a type defined in the decl-specifiers.  If it is invalid to
2157    define a type in the decl-specifiers for DECLARATOR, an error is
2158    issued.  */
2159
2160 static void
2161 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2162                                                tree type)
2163 {
2164   /* [dcl.fct] forbids type definitions in return types.
2165      Unfortunately, it's not easy to know whether or not we are
2166      processing a return type until after the fact.  */
2167   while (declarator
2168          && (declarator->kind == cdk_pointer
2169              || declarator->kind == cdk_reference
2170              || declarator->kind == cdk_ptrmem))
2171     declarator = declarator->declarator;
2172   if (declarator
2173       && declarator->kind == cdk_function)
2174     {
2175       error ("new types may not be defined in a return type");
2176       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2177               type);
2178     }
2179 }
2180
2181 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2182    "<" in any valid C++ program.  If the next token is indeed "<",
2183    issue a message warning the user about what appears to be an
2184    invalid attempt to form a template-id.  */
2185
2186 static void
2187 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2188                                          tree type)
2189 {
2190   cp_token_position start = 0;
2191
2192   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2193     {
2194       if (TYPE_P (type))
2195         error ("%qT is not a template", type);
2196       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2197         error ("%qE is not a template", type);
2198       else
2199         error ("invalid template-id");
2200       /* Remember the location of the invalid "<".  */
2201       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2202         start = cp_lexer_token_position (parser->lexer, true);
2203       /* Consume the "<".  */
2204       cp_lexer_consume_token (parser->lexer);
2205       /* Parse the template arguments.  */
2206       cp_parser_enclosed_template_argument_list (parser);
2207       /* Permanently remove the invalid template arguments so that
2208          this error message is not issued again.  */
2209       if (start)
2210         cp_lexer_purge_tokens_after (parser->lexer, start);
2211     }
2212 }
2213
2214 /* If parsing an integral constant-expression, issue an error message
2215    about the fact that THING appeared and return true.  Otherwise,
2216    return false.  In either case, set
2217    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2218
2219 static bool
2220 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2221                                             const char *thing)
2222 {
2223   parser->non_integral_constant_expression_p = true;
2224   if (parser->integral_constant_expression_p)
2225     {
2226       if (!parser->allow_non_integral_constant_expression_p)
2227         {
2228           error ("%s cannot appear in a constant-expression", thing);
2229           return true;
2230         }
2231     }
2232   return false;
2233 }
2234
2235 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2236    qualifying scope (or NULL, if none) for ID.  This function commits
2237    to the current active tentative parse, if any.  (Otherwise, the
2238    problematic construct might be encountered again later, resulting
2239    in duplicate error messages.)  */
2240
2241 static void
2242 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2243 {
2244   tree decl, old_scope;
2245   /* Try to lookup the identifier.  */
2246   old_scope = parser->scope;
2247   parser->scope = scope;
2248   decl = cp_parser_lookup_name_simple (parser, id);
2249   parser->scope = old_scope;
2250   /* If the lookup found a template-name, it means that the user forgot
2251   to specify an argument list. Emit a useful error message.  */
2252   if (TREE_CODE (decl) == TEMPLATE_DECL)
2253     error ("invalid use of template-name %qE without an argument list", decl);
2254   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2255     error ("invalid use of destructor %qD as a type", id);
2256   else if (TREE_CODE (decl) == TYPE_DECL)
2257     /* Something like 'unsigned A a;'  */
2258     error ("invalid combination of multiple type-specifiers");
2259   else if (!parser->scope)
2260     {
2261       /* Issue an error message.  */
2262       error ("%qE does not name a type", id);
2263       /* If we're in a template class, it's possible that the user was
2264          referring to a type from a base class.  For example:
2265
2266            template <typename T> struct A { typedef T X; };
2267            template <typename T> struct B : public A<T> { X x; };
2268
2269          The user should have said "typename A<T>::X".  */
2270       if (processing_template_decl && current_class_type
2271           && TYPE_BINFO (current_class_type))
2272         {
2273           tree b;
2274
2275           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2276                b;
2277                b = TREE_CHAIN (b))
2278             {
2279               tree base_type = BINFO_TYPE (b);
2280               if (CLASS_TYPE_P (base_type)
2281                   && dependent_type_p (base_type))
2282                 {
2283                   tree field;
2284                   /* Go from a particular instantiation of the
2285                      template (which will have an empty TYPE_FIELDs),
2286                      to the main version.  */
2287                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2288                   for (field = TYPE_FIELDS (base_type);
2289                        field;
2290                        field = TREE_CHAIN (field))
2291                     if (TREE_CODE (field) == TYPE_DECL
2292                         && DECL_NAME (field) == id)
2293                       {
2294                         inform ("(perhaps %<typename %T::%E%> was intended)",
2295                                 BINFO_TYPE (b), id);
2296                         break;
2297                       }
2298                   if (field)
2299                     break;
2300                 }
2301             }
2302         }
2303     }
2304   /* Here we diagnose qualified-ids where the scope is actually correct,
2305      but the identifier does not resolve to a valid type name.  */
2306   else if (parser->scope != error_mark_node)
2307     {
2308       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2309         error ("%qE in namespace %qE does not name a type",
2310                id, parser->scope);
2311       else if (TYPE_P (parser->scope))
2312         error ("%qE in class %qT does not name a type", id, parser->scope);
2313       else
2314         gcc_unreachable ();
2315     }
2316   cp_parser_commit_to_tentative_parse (parser);
2317 }
2318
2319 /* Check for a common situation where a type-name should be present,
2320    but is not, and issue a sensible error message.  Returns true if an
2321    invalid type-name was detected.
2322
2323    The situation handled by this function are variable declarations of the
2324    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2325    Usually, `ID' should name a type, but if we got here it means that it
2326    does not. We try to emit the best possible error message depending on
2327    how exactly the id-expression looks like.  */
2328
2329 static bool
2330 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2331 {
2332   tree id;
2333
2334   cp_parser_parse_tentatively (parser);
2335   id = cp_parser_id_expression (parser,
2336                                 /*template_keyword_p=*/false,
2337                                 /*check_dependency_p=*/true,
2338                                 /*template_p=*/NULL,
2339                                 /*declarator_p=*/true,
2340                                 /*optional_p=*/false);
2341   /* After the id-expression, there should be a plain identifier,
2342      otherwise this is not a simple variable declaration. Also, if
2343      the scope is dependent, we cannot do much.  */
2344   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2345       || (parser->scope && TYPE_P (parser->scope)
2346           && dependent_type_p (parser->scope)))
2347     {
2348       cp_parser_abort_tentative_parse (parser);
2349       return false;
2350     }
2351   if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2352     return false;
2353
2354   /* Emit a diagnostic for the invalid type.  */
2355   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2356   /* Skip to the end of the declaration; there's no point in
2357      trying to process it.  */
2358   cp_parser_skip_to_end_of_block_or_statement (parser);
2359   return true;
2360 }
2361
2362 /* Consume tokens up to, and including, the next non-nested closing `)'.
2363    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2364    are doing error recovery. Returns -1 if OR_COMMA is true and we
2365    found an unnested comma.  */
2366
2367 static int
2368 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2369                                        bool recovering,
2370                                        bool or_comma,
2371                                        bool consume_paren)
2372 {
2373   unsigned paren_depth = 0;
2374   unsigned brace_depth = 0;
2375
2376   if (recovering && !or_comma
2377       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2378     return 0;
2379
2380   while (true)
2381     {
2382       cp_token * token = cp_lexer_peek_token (parser->lexer);
2383
2384       switch (token->type)
2385         {
2386         case CPP_EOF:
2387         case CPP_PRAGMA_EOL:
2388           /* If we've run out of tokens, then there is no closing `)'.  */
2389           return 0;
2390
2391         case CPP_SEMICOLON:
2392           /* This matches the processing in skip_to_end_of_statement.  */
2393           if (!brace_depth)
2394             return 0;
2395           break;
2396
2397         case CPP_OPEN_BRACE:
2398           ++brace_depth;
2399           break;
2400         case CPP_CLOSE_BRACE:
2401           if (!brace_depth--)
2402             return 0;
2403           break;
2404
2405         case CPP_COMMA:
2406           if (recovering && or_comma && !brace_depth && !paren_depth)
2407             return -1;
2408           break;
2409
2410         case CPP_OPEN_PAREN:
2411           if (!brace_depth)
2412             ++paren_depth;
2413           break;
2414
2415         case CPP_CLOSE_PAREN:
2416           if (!brace_depth && !paren_depth--)
2417             {
2418               if (consume_paren)
2419                 cp_lexer_consume_token (parser->lexer);
2420               return 1;
2421             }
2422           break;
2423
2424         default:
2425           break;
2426         }
2427
2428       /* Consume the token.  */
2429       cp_lexer_consume_token (parser->lexer);
2430     }
2431 }
2432
2433 /* Consume tokens until we reach the end of the current statement.
2434    Normally, that will be just before consuming a `;'.  However, if a
2435    non-nested `}' comes first, then we stop before consuming that.  */
2436
2437 static void
2438 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2439 {
2440   unsigned nesting_depth = 0;
2441
2442   while (true)
2443     {
2444       cp_token *token = cp_lexer_peek_token (parser->lexer);
2445
2446       switch (token->type)
2447         {
2448         case CPP_EOF:
2449         case CPP_PRAGMA_EOL:
2450           /* If we've run out of tokens, stop.  */
2451           return;
2452
2453         case CPP_SEMICOLON:
2454           /* If the next token is a `;', we have reached the end of the
2455              statement.  */
2456           if (!nesting_depth)
2457             return;
2458           break;
2459
2460         case CPP_CLOSE_BRACE:
2461           /* If this is a non-nested '}', stop before consuming it.
2462              That way, when confronted with something like:
2463
2464                { 3 + }
2465
2466              we stop before consuming the closing '}', even though we
2467              have not yet reached a `;'.  */
2468           if (nesting_depth == 0)
2469             return;
2470
2471           /* If it is the closing '}' for a block that we have
2472              scanned, stop -- but only after consuming the token.
2473              That way given:
2474
2475                 void f g () { ... }
2476                 typedef int I;
2477
2478              we will stop after the body of the erroneously declared
2479              function, but before consuming the following `typedef'
2480              declaration.  */
2481           if (--nesting_depth == 0)
2482             {
2483               cp_lexer_consume_token (parser->lexer);
2484               return;
2485             }
2486
2487         case CPP_OPEN_BRACE:
2488           ++nesting_depth;
2489           break;
2490
2491         default:
2492           break;
2493         }
2494
2495       /* Consume the token.  */
2496       cp_lexer_consume_token (parser->lexer);
2497     }
2498 }
2499
2500 /* This function is called at the end of a statement or declaration.
2501    If the next token is a semicolon, it is consumed; otherwise, error
2502    recovery is attempted.  */
2503
2504 static void
2505 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2506 {
2507   /* Look for the trailing `;'.  */
2508   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2509     {
2510       /* If there is additional (erroneous) input, skip to the end of
2511          the statement.  */
2512       cp_parser_skip_to_end_of_statement (parser);
2513       /* If the next token is now a `;', consume it.  */
2514       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2515         cp_lexer_consume_token (parser->lexer);
2516     }
2517 }
2518
2519 /* Skip tokens until we have consumed an entire block, or until we
2520    have consumed a non-nested `;'.  */
2521
2522 static void
2523 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2524 {
2525   int nesting_depth = 0;
2526
2527   while (nesting_depth >= 0)
2528     {
2529       cp_token *token = cp_lexer_peek_token (parser->lexer);
2530
2531       switch (token->type)
2532         {
2533         case CPP_EOF:
2534         case CPP_PRAGMA_EOL:
2535           /* If we've run out of tokens, stop.  */
2536           return;
2537
2538         case CPP_SEMICOLON:
2539           /* Stop if this is an unnested ';'. */
2540           if (!nesting_depth)
2541             nesting_depth = -1;
2542           break;
2543
2544         case CPP_CLOSE_BRACE:
2545           /* Stop if this is an unnested '}', or closes the outermost
2546              nesting level.  */
2547           nesting_depth--;
2548           if (!nesting_depth)
2549             nesting_depth = -1;
2550           break;
2551
2552         case CPP_OPEN_BRACE:
2553           /* Nest. */
2554           nesting_depth++;
2555           break;
2556
2557         default:
2558           break;
2559         }
2560
2561       /* Consume the token.  */
2562       cp_lexer_consume_token (parser->lexer);
2563     }
2564 }
2565
2566 /* Skip tokens until a non-nested closing curly brace is the next
2567    token.  */
2568
2569 static void
2570 cp_parser_skip_to_closing_brace (cp_parser *parser)
2571 {
2572   unsigned nesting_depth = 0;
2573
2574   while (true)
2575     {
2576       cp_token *token = cp_lexer_peek_token (parser->lexer);
2577
2578       switch (token->type)
2579         {
2580         case CPP_EOF:
2581         case CPP_PRAGMA_EOL:
2582           /* If we've run out of tokens, stop.  */
2583           return;
2584
2585         case CPP_CLOSE_BRACE:
2586           /* If the next token is a non-nested `}', then we have reached
2587              the end of the current block.  */
2588           if (nesting_depth-- == 0)
2589             return;
2590           break;
2591
2592         case CPP_OPEN_BRACE:
2593           /* If it the next token is a `{', then we are entering a new
2594              block.  Consume the entire block.  */
2595           ++nesting_depth;
2596           break;
2597
2598         default:
2599           break;
2600         }
2601
2602       /* Consume the token.  */
2603       cp_lexer_consume_token (parser->lexer);
2604     }
2605 }
2606
2607 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2608    parameter is the PRAGMA token, allowing us to purge the entire pragma
2609    sequence.  */
2610
2611 static void
2612 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2613 {
2614   cp_token *token;
2615
2616   parser->lexer->in_pragma = false;
2617
2618   do
2619     token = cp_lexer_consume_token (parser->lexer);
2620   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2621
2622   /* Ensure that the pragma is not parsed again.  */
2623   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2624 }
2625
2626 /* Require pragma end of line, resyncing with it as necessary.  The
2627    arguments are as for cp_parser_skip_to_pragma_eol.  */
2628
2629 static void
2630 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2631 {
2632   parser->lexer->in_pragma = false;
2633   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2634     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2635 }
2636
2637 /* This is a simple wrapper around make_typename_type. When the id is
2638    an unresolved identifier node, we can provide a superior diagnostic
2639    using cp_parser_diagnose_invalid_type_name.  */
2640
2641 static tree
2642 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2643 {
2644   tree result;
2645   if (TREE_CODE (id) == IDENTIFIER_NODE)
2646     {
2647       result = make_typename_type (scope, id, typename_type,
2648                                    /*complain=*/tf_none);
2649       if (result == error_mark_node)
2650         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2651       return result;
2652     }
2653   return make_typename_type (scope, id, typename_type, tf_error);
2654 }
2655
2656
2657 /* Create a new C++ parser.  */
2658
2659 static cp_parser *
2660 cp_parser_new (void)
2661 {
2662   cp_parser *parser;
2663   cp_lexer *lexer;
2664   unsigned i;
2665
2666   /* cp_lexer_new_main is called before calling ggc_alloc because
2667      cp_lexer_new_main might load a PCH file.  */
2668   lexer = cp_lexer_new_main ();
2669
2670   /* Initialize the binops_by_token so that we can get the tree
2671      directly from the token.  */
2672   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2673     binops_by_token[binops[i].token_type] = binops[i];
2674
2675   parser = GGC_CNEW (cp_parser);
2676   parser->lexer = lexer;
2677   parser->context = cp_parser_context_new (NULL);
2678
2679   /* For now, we always accept GNU extensions.  */
2680   parser->allow_gnu_extensions_p = 1;
2681
2682   /* The `>' token is a greater-than operator, not the end of a
2683      template-id.  */
2684   parser->greater_than_is_operator_p = true;
2685
2686   parser->default_arg_ok_p = true;
2687
2688   /* We are not parsing a constant-expression.  */
2689   parser->integral_constant_expression_p = false;
2690   parser->allow_non_integral_constant_expression_p = false;
2691   parser->non_integral_constant_expression_p = false;
2692
2693   /* Local variable names are not forbidden.  */
2694   parser->local_variables_forbidden_p = false;
2695
2696   /* We are not processing an `extern "C"' declaration.  */
2697   parser->in_unbraced_linkage_specification_p = false;
2698
2699   /* We are not processing a declarator.  */
2700   parser->in_declarator_p = false;
2701
2702   /* We are not processing a template-argument-list.  */
2703   parser->in_template_argument_list_p = false;
2704
2705   /* We are not in an iteration statement.  */
2706   parser->in_statement = 0;
2707
2708   /* We are not in a switch statement.  */
2709   parser->in_switch_statement_p = false;
2710
2711   /* We are not parsing a type-id inside an expression.  */
2712   parser->in_type_id_in_expr_p = false;
2713
2714   /* Declarations aren't implicitly extern "C".  */
2715   parser->implicit_extern_c = false;
2716
2717   /* String literals should be translated to the execution character set.  */
2718   parser->translate_strings_p = true;
2719
2720   /* We are not parsing a function body.  */
2721   parser->in_function_body = false;
2722
2723   /* The unparsed function queue is empty.  */
2724   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2725
2726   /* There are no classes being defined.  */
2727   parser->num_classes_being_defined = 0;
2728
2729   /* No template parameters apply.  */
2730   parser->num_template_parameter_lists = 0;
2731
2732   return parser;
2733 }
2734
2735 /* Create a cp_lexer structure which will emit the tokens in CACHE
2736    and push it onto the parser's lexer stack.  This is used for delayed
2737    parsing of in-class method bodies and default arguments, and should
2738    not be confused with tentative parsing.  */
2739 static void
2740 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2741 {
2742   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2743   lexer->next = parser->lexer;
2744   parser->lexer = lexer;
2745
2746   /* Move the current source position to that of the first token in the
2747      new lexer.  */
2748   cp_lexer_set_source_position_from_token (lexer->next_token);
2749 }
2750
2751 /* Pop the top lexer off the parser stack.  This is never used for the
2752    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2753 static void
2754 cp_parser_pop_lexer (cp_parser *parser)
2755 {
2756   cp_lexer *lexer = parser->lexer;
2757   parser->lexer = lexer->next;
2758   cp_lexer_destroy (lexer);
2759
2760   /* Put the current source position back where it was before this
2761      lexer was pushed.  */
2762   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2763 }
2764
2765 /* Lexical conventions [gram.lex]  */
2766
2767 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2768    identifier.  */
2769
2770 static tree
2771 cp_parser_identifier (cp_parser* parser)
2772 {
2773   cp_token *token;
2774
2775   /* Look for the identifier.  */
2776   token = cp_parser_require (parser, CPP_NAME, "identifier");
2777   /* Return the value.  */
2778   return token ? token->u.value : error_mark_node;
2779 }
2780
2781 /* Parse a sequence of adjacent string constants.  Returns a
2782    TREE_STRING representing the combined, nul-terminated string
2783    constant.  If TRANSLATE is true, translate the string to the
2784    execution character set.  If WIDE_OK is true, a wide string is
2785    invalid here.
2786
2787    C++98 [lex.string] says that if a narrow string literal token is
2788    adjacent to a wide string literal token, the behavior is undefined.
2789    However, C99 6.4.5p4 says that this results in a wide string literal.
2790    We follow C99 here, for consistency with the C front end.
2791
2792    This code is largely lifted from lex_string() in c-lex.c.
2793
2794    FUTURE: ObjC++ will need to handle @-strings here.  */
2795 static tree
2796 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2797 {
2798   tree value;
2799   bool wide = false;
2800   size_t count;
2801   struct obstack str_ob;
2802   cpp_string str, istr, *strs;
2803   cp_token *tok;
2804
2805   tok = cp_lexer_peek_token (parser->lexer);
2806   if (!cp_parser_is_string_literal (tok))
2807     {
2808       cp_parser_error (parser, "expected string-literal");
2809       return error_mark_node;
2810     }
2811
2812   /* Try to avoid the overhead of creating and destroying an obstack
2813      for the common case of just one string.  */
2814   if (!cp_parser_is_string_literal
2815       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2816     {
2817       cp_lexer_consume_token (parser->lexer);
2818
2819       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2820       str.len = TREE_STRING_LENGTH (tok->u.value);
2821       count = 1;
2822       if (tok->type == CPP_WSTRING)
2823         wide = true;
2824
2825       strs = &str;
2826     }
2827   else
2828     {
2829       gcc_obstack_init (&str_ob);
2830       count = 0;
2831
2832       do
2833         {
2834           cp_lexer_consume_token (parser->lexer);
2835           count++;
2836           str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2837           str.len = TREE_STRING_LENGTH (tok->u.value);
2838           if (tok->type == CPP_WSTRING)
2839             wide = true;
2840
2841           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2842
2843           tok = cp_lexer_peek_token (parser->lexer);
2844         }
2845       while (cp_parser_is_string_literal (tok));
2846
2847       strs = (cpp_string *) obstack_finish (&str_ob);
2848     }
2849
2850   if (wide && !wide_ok)
2851     {
2852       cp_parser_error (parser, "a wide string is invalid in this context");
2853       wide = false;
2854     }
2855
2856   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2857       (parse_in, strs, count, &istr, wide))
2858     {
2859       value = build_string (istr.len, (char *)istr.text);
2860       free ((void *)istr.text);
2861
2862       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2863       value = fix_string_type (value);
2864     }
2865   else
2866     /* cpp_interpret_string has issued an error.  */
2867     value = error_mark_node;
2868
2869   if (count > 1)
2870     obstack_free (&str_ob, 0);
2871
2872   return value;
2873 }
2874
2875
2876 /* Basic concepts [gram.basic]  */
2877
2878 /* Parse a translation-unit.
2879
2880    translation-unit:
2881      declaration-seq [opt]
2882
2883    Returns TRUE if all went well.  */
2884
2885 static bool
2886 cp_parser_translation_unit (cp_parser* parser)
2887 {
2888   /* The address of the first non-permanent object on the declarator
2889      obstack.  */
2890   static void *declarator_obstack_base;
2891
2892   bool success;
2893
2894   /* Create the declarator obstack, if necessary.  */
2895   if (!cp_error_declarator)
2896     {
2897       gcc_obstack_init (&declarator_obstack);
2898       /* Create the error declarator.  */
2899       cp_error_declarator = make_declarator (cdk_error);
2900       /* Create the empty parameter list.  */
2901       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2902       /* Remember where the base of the declarator obstack lies.  */
2903       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2904     }
2905
2906   cp_parser_declaration_seq_opt (parser);
2907
2908   /* If there are no tokens left then all went well.  */
2909   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2910     {
2911       /* Get rid of the token array; we don't need it any more.  */
2912       cp_lexer_destroy (parser->lexer);
2913       parser->lexer = NULL;
2914
2915       /* This file might have been a context that's implicitly extern
2916          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2917       if (parser->implicit_extern_c)
2918         {
2919           pop_lang_context ();
2920           parser->implicit_extern_c = false;
2921         }
2922
2923       /* Finish up.  */
2924       finish_translation_unit ();
2925
2926       success = true;
2927     }
2928   else
2929     {
2930       cp_parser_error (parser, "expected declaration");
2931       success = false;
2932     }
2933
2934   /* Make sure the declarator obstack was fully cleaned up.  */
2935   gcc_assert (obstack_next_free (&declarator_obstack)
2936               == declarator_obstack_base);
2937
2938   /* All went well.  */
2939   return success;
2940 }
2941
2942 /* Expressions [gram.expr] */
2943
2944 /* Parse a primary-expression.
2945
2946    primary-expression:
2947      literal
2948      this
2949      ( expression )
2950      id-expression
2951
2952    GNU Extensions:
2953
2954    primary-expression:
2955      ( compound-statement )
2956      __builtin_va_arg ( assignment-expression , type-id )
2957      __builtin_offsetof ( type-id , offsetof-expression )
2958
2959    Objective-C++ Extension:
2960
2961    primary-expression:
2962      objc-expression
2963
2964    literal:
2965      __null
2966
2967    ADDRESS_P is true iff this expression was immediately preceded by
2968    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2969    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2970    true iff this expression is a template argument.
2971
2972    Returns a representation of the expression.  Upon return, *IDK
2973    indicates what kind of id-expression (if any) was present.  */
2974
2975 static tree
2976 cp_parser_primary_expression (cp_parser *parser,
2977                               bool address_p,
2978                               bool cast_p,
2979                               bool template_arg_p,
2980                               cp_id_kind *idk)
2981 {
2982   cp_token *token;
2983
2984   /* Assume the primary expression is not an id-expression.  */
2985   *idk = CP_ID_KIND_NONE;
2986
2987   /* Peek at the next token.  */
2988   token = cp_lexer_peek_token (parser->lexer);
2989   switch (token->type)
2990     {
2991       /* literal:
2992            integer-literal
2993            character-literal
2994            floating-literal
2995            string-literal
2996            boolean-literal  */
2997     case CPP_CHAR:
2998     case CPP_WCHAR:
2999     case CPP_NUMBER:
3000       token = cp_lexer_consume_token (parser->lexer);
3001       /* Floating-point literals are only allowed in an integral
3002          constant expression if they are cast to an integral or
3003          enumeration type.  */
3004       if (TREE_CODE (token->u.value) == REAL_CST
3005           && parser->integral_constant_expression_p
3006           && pedantic)
3007         {
3008           /* CAST_P will be set even in invalid code like "int(2.7 +
3009              ...)".   Therefore, we have to check that the next token
3010              is sure to end the cast.  */
3011           if (cast_p)
3012             {
3013               cp_token *next_token;
3014
3015               next_token = cp_lexer_peek_token (parser->lexer);
3016               if (/* The comma at the end of an
3017                      enumerator-definition.  */
3018                   next_token->type != CPP_COMMA
3019                   /* The curly brace at the end of an enum-specifier.  */
3020                   && next_token->type != CPP_CLOSE_BRACE
3021                   /* The end of a statement.  */
3022                   && next_token->type != CPP_SEMICOLON
3023                   /* The end of the cast-expression.  */
3024                   && next_token->type != CPP_CLOSE_PAREN
3025                   /* The end of an array bound.  */
3026                   && next_token->type != CPP_CLOSE_SQUARE
3027                   /* The closing ">" in a template-argument-list.  */
3028                   && (next_token->type != CPP_GREATER
3029                       || parser->greater_than_is_operator_p))
3030                 cast_p = false;
3031             }
3032
3033           /* If we are within a cast, then the constraint that the
3034              cast is to an integral or enumeration type will be
3035              checked at that point.  If we are not within a cast, then
3036              this code is invalid.  */
3037           if (!cast_p)
3038             cp_parser_non_integral_constant_expression
3039               (parser, "floating-point literal");
3040         }
3041       return token->u.value;
3042
3043     case CPP_STRING:
3044     case CPP_WSTRING:
3045       /* ??? Should wide strings be allowed when parser->translate_strings_p
3046          is false (i.e. in attributes)?  If not, we can kill the third
3047          argument to cp_parser_string_literal.  */
3048       return cp_parser_string_literal (parser,
3049                                        parser->translate_strings_p,
3050                                        true);
3051
3052     case CPP_OPEN_PAREN:
3053       {
3054         tree expr;
3055         bool saved_greater_than_is_operator_p;
3056
3057         /* Consume the `('.  */
3058         cp_lexer_consume_token (parser->lexer);
3059         /* Within a parenthesized expression, a `>' token is always
3060            the greater-than operator.  */
3061         saved_greater_than_is_operator_p
3062           = parser->greater_than_is_operator_p;
3063         parser->greater_than_is_operator_p = true;
3064         /* If we see `( { ' then we are looking at the beginning of
3065            a GNU statement-expression.  */
3066         if (cp_parser_allow_gnu_extensions_p (parser)
3067             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3068           {
3069             /* Statement-expressions are not allowed by the standard.  */
3070             if (pedantic)
3071               pedwarn ("ISO C++ forbids braced-groups within expressions");
3072
3073             /* And they're not allowed outside of a function-body; you
3074                cannot, for example, write:
3075
3076                  int i = ({ int j = 3; j + 1; });
3077
3078                at class or namespace scope.  */
3079             if (!parser->in_function_body)
3080               {
3081                 error ("statement-expressions are allowed only inside functions");
3082                 cp_parser_skip_to_end_of_block_or_statement (parser);
3083                 expr = error_mark_node;
3084               }
3085             else
3086               {
3087                 /* Start the statement-expression.  */
3088                 expr = begin_stmt_expr ();
3089                 /* Parse the compound-statement.  */
3090                 cp_parser_compound_statement (parser, expr, false);
3091                 /* Finish up.  */
3092                 expr = finish_stmt_expr (expr, false);
3093               }
3094           }
3095         else
3096           {
3097             /* Parse the parenthesized expression.  */
3098             expr = cp_parser_expression (parser, cast_p);
3099             /* Let the front end know that this expression was
3100                enclosed in parentheses. This matters in case, for
3101                example, the expression is of the form `A::B', since
3102                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3103                not.  */
3104             finish_parenthesized_expr (expr);
3105           }
3106         /* The `>' token might be the end of a template-id or
3107            template-parameter-list now.  */
3108         parser->greater_than_is_operator_p
3109           = saved_greater_than_is_operator_p;
3110         /* Consume the `)'.  */
3111         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3112           cp_parser_skip_to_end_of_statement (parser);
3113
3114         return expr;
3115       }
3116
3117     case CPP_KEYWORD:
3118       switch (token->keyword)
3119         {
3120           /* These two are the boolean literals.  */
3121         case RID_TRUE:
3122           cp_lexer_consume_token (parser->lexer);
3123           return boolean_true_node;
3124         case RID_FALSE:
3125           cp_lexer_consume_token (parser->lexer);
3126           return boolean_false_node;
3127
3128           /* The `__null' literal.  */
3129         case RID_NULL:
3130           cp_lexer_consume_token (parser->lexer);
3131           return null_node;
3132
3133           /* Recognize the `this' keyword.  */
3134         case RID_THIS:
3135           cp_lexer_consume_token (parser->lexer);
3136           if (parser->local_variables_forbidden_p)
3137             {
3138               error ("%<this%> may not be used in this context");
3139               return error_mark_node;
3140             }
3141           /* Pointers cannot appear in constant-expressions.  */
3142           if (cp_parser_non_integral_constant_expression (parser,
3143                                                           "`this'"))
3144             return error_mark_node;
3145           return finish_this_expr ();
3146
3147           /* The `operator' keyword can be the beginning of an
3148              id-expression.  */
3149         case RID_OPERATOR:
3150           goto id_expression;
3151
3152         case RID_FUNCTION_NAME:
3153         case RID_PRETTY_FUNCTION_NAME:
3154         case RID_C99_FUNCTION_NAME:
3155           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3156              __func__ are the names of variables -- but they are
3157              treated specially.  Therefore, they are handled here,
3158              rather than relying on the generic id-expression logic
3159              below.  Grammatically, these names are id-expressions.
3160
3161              Consume the token.  */
3162           token = cp_lexer_consume_token (parser->lexer);
3163           /* Look up the name.  */
3164           return finish_fname (token->u.value);
3165
3166         case RID_VA_ARG:
3167           {
3168             tree expression;
3169             tree type;
3170
3171             /* The `__builtin_va_arg' construct is used to handle
3172                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3173             cp_lexer_consume_token (parser->lexer);
3174             /* Look for the opening `('.  */
3175             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3176             /* Now, parse the assignment-expression.  */
3177             expression = cp_parser_assignment_expression (parser,
3178                                                           /*cast_p=*/false);
3179             /* Look for the `,'.  */
3180             cp_parser_require (parser, CPP_COMMA, "`,'");
3181             /* Parse the type-id.  */
3182             type = cp_parser_type_id (parser);
3183             /* Look for the closing `)'.  */
3184             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3185             /* Using `va_arg' in a constant-expression is not
3186                allowed.  */
3187             if (cp_parser_non_integral_constant_expression (parser,
3188                                                             "`va_arg'"))
3189               return error_mark_node;
3190             return build_x_va_arg (expression, type);
3191           }
3192
3193         case RID_OFFSETOF:
3194           return cp_parser_builtin_offsetof (parser);
3195
3196           /* Objective-C++ expressions.  */
3197         case RID_AT_ENCODE:
3198         case RID_AT_PROTOCOL:
3199         case RID_AT_SELECTOR:
3200           return cp_parser_objc_expression (parser);
3201
3202         default:
3203           cp_parser_error (parser, "expected primary-expression");
3204           return error_mark_node;
3205         }
3206
3207       /* An id-expression can start with either an identifier, a
3208          `::' as the beginning of a qualified-id, or the "operator"
3209          keyword.  */
3210     case CPP_NAME:
3211     case CPP_SCOPE:
3212     case CPP_TEMPLATE_ID:
3213     case CPP_NESTED_NAME_SPECIFIER:
3214       {
3215         tree id_expression;
3216         tree decl;
3217         const char *error_msg;
3218         bool template_p;
3219         bool done;
3220
3221       id_expression:
3222         /* Parse the id-expression.  */
3223         id_expression
3224           = cp_parser_id_expression (parser,
3225                                      /*template_keyword_p=*/false,
3226                                      /*check_dependency_p=*/true,
3227                                      &template_p,
3228                                      /*declarator_p=*/false,
3229                                      /*optional_p=*/false);
3230         if (id_expression == error_mark_node)
3231           return error_mark_node;
3232         token = cp_lexer_peek_token (parser->lexer);
3233         done = (token->type != CPP_OPEN_SQUARE
3234                 && token->type != CPP_OPEN_PAREN
3235                 && token->type != CPP_DOT
3236                 && token->type != CPP_DEREF
3237                 && token->type != CPP_PLUS_PLUS
3238                 && token->type != CPP_MINUS_MINUS);
3239         /* If we have a template-id, then no further lookup is
3240            required.  If the template-id was for a template-class, we
3241            will sometimes have a TYPE_DECL at this point.  */
3242         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3243                  || TREE_CODE (id_expression) == TYPE_DECL)
3244           decl = id_expression;
3245         /* Look up the name.  */
3246         else
3247           {
3248             tree ambiguous_decls;
3249
3250             decl = cp_parser_lookup_name (parser, id_expression,
3251                                           none_type,
3252                                           template_p,
3253                                           /*is_namespace=*/false,
3254                                           /*check_dependency=*/true,
3255                                           &ambiguous_decls);
3256             /* If the lookup was ambiguous, an error will already have
3257                been issued.  */
3258             if (ambiguous_decls)
3259               return error_mark_node;
3260
3261             /* In Objective-C++, an instance variable (ivar) may be preferred
3262                to whatever cp_parser_lookup_name() found.  */
3263             decl = objc_lookup_ivar (decl, id_expression);
3264
3265             /* If name lookup gives us a SCOPE_REF, then the
3266                qualifying scope was dependent.  */
3267             if (TREE_CODE (decl) == SCOPE_REF)
3268               return decl;
3269             /* Check to see if DECL is a local variable in a context
3270                where that is forbidden.  */
3271             if (parser->local_variables_forbidden_p
3272                 && local_variable_p (decl))
3273               {
3274                 /* It might be that we only found DECL because we are
3275                    trying to be generous with pre-ISO scoping rules.
3276                    For example, consider:
3277
3278                      int i;
3279                      void g() {
3280                        for (int i = 0; i < 10; ++i) {}
3281                        extern void f(int j = i);
3282                      }
3283
3284                    Here, name look up will originally find the out
3285                    of scope `i'.  We need to issue a warning message,
3286                    but then use the global `i'.  */
3287                 decl = check_for_out_of_scope_variable (decl);
3288                 if (local_variable_p (decl))
3289                   {
3290                     error ("local variable %qD may not appear in this context",
3291                            decl);
3292                     return error_mark_node;
3293                   }
3294               }
3295           }
3296
3297         decl = (finish_id_expression
3298                 (id_expression, decl, parser->scope,
3299                  idk,
3300                  parser->integral_constant_expression_p,
3301                  parser->allow_non_integral_constant_expression_p,
3302                  &parser->non_integral_constant_expression_p,
3303                  template_p, done, address_p,
3304                  template_arg_p,
3305                  &error_msg));
3306         if (error_msg)
3307           cp_parser_error (parser, error_msg);
3308         return decl;
3309       }
3310
3311       /* Anything else is an error.  */
3312     default:
3313       /* ...unless we have an Objective-C++ message or string literal,
3314          that is.  */
3315       if (c_dialect_objc ()
3316           && (token->type == CPP_OPEN_SQUARE
3317               || token->type == CPP_OBJC_STRING))
3318         return cp_parser_objc_expression (parser);
3319
3320       cp_parser_error (parser, "expected primary-expression");
3321       return error_mark_node;
3322     }
3323 }
3324
3325 /* Parse an id-expression.
3326
3327    id-expression:
3328      unqualified-id
3329      qualified-id
3330
3331    qualified-id:
3332      :: [opt] nested-name-specifier template [opt] unqualified-id
3333      :: identifier
3334      :: operator-function-id
3335      :: template-id
3336
3337    Return a representation of the unqualified portion of the
3338    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3339    a `::' or nested-name-specifier.
3340
3341    Often, if the id-expression was a qualified-id, the caller will
3342    want to make a SCOPE_REF to represent the qualified-id.  This
3343    function does not do this in order to avoid wastefully creating
3344    SCOPE_REFs when they are not required.
3345
3346    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3347    `template' keyword.
3348
3349    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3350    uninstantiated templates.
3351
3352    If *TEMPLATE_P is non-NULL, it is set to true iff the
3353    `template' keyword is used to explicitly indicate that the entity
3354    named is a template.
3355
3356    If DECLARATOR_P is true, the id-expression is appearing as part of
3357    a declarator, rather than as part of an expression.  */
3358
3359 static tree
3360 cp_parser_id_expression (cp_parser *parser,
3361                          bool template_keyword_p,
3362                          bool check_dependency_p,
3363                          bool *template_p,
3364                          bool declarator_p,
3365                          bool optional_p)
3366 {
3367   bool global_scope_p;
3368   bool nested_name_specifier_p;
3369
3370   /* Assume the `template' keyword was not used.  */
3371   if (template_p)
3372     *template_p = template_keyword_p;
3373
3374   /* Look for the optional `::' operator.  */
3375   global_scope_p
3376     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3377        != NULL_TREE);
3378   /* Look for the optional nested-name-specifier.  */
3379   nested_name_specifier_p
3380     = (cp_parser_nested_name_specifier_opt (parser,
3381                                             /*typename_keyword_p=*/false,
3382                                             check_dependency_p,
3383                                             /*type_p=*/false,
3384                                             declarator_p)
3385        != NULL_TREE);
3386   /* If there is a nested-name-specifier, then we are looking at
3387      the first qualified-id production.  */
3388   if (nested_name_specifier_p)
3389     {
3390       tree saved_scope;
3391       tree saved_object_scope;
3392       tree saved_qualifying_scope;
3393       tree unqualified_id;
3394       bool is_template;
3395
3396       /* See if the next token is the `template' keyword.  */
3397       if (!template_p)
3398         template_p = &is_template;
3399       *template_p = cp_parser_optional_template_keyword (parser);
3400       /* Name lookup we do during the processing of the
3401          unqualified-id might obliterate SCOPE.  */
3402       saved_scope = parser->scope;
3403       saved_object_scope = parser->object_scope;
3404       saved_qualifying_scope = parser->qualifying_scope;
3405       /* Process the final unqualified-id.  */
3406       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3407                                                  check_dependency_p,
3408                                                  declarator_p,
3409                                                  /*optional_p=*/false);
3410       /* Restore the SAVED_SCOPE for our caller.  */
3411       parser->scope = saved_scope;
3412       parser->object_scope = saved_object_scope;
3413       parser->qualifying_scope = saved_qualifying_scope;
3414
3415       return unqualified_id;
3416     }
3417   /* Otherwise, if we are in global scope, then we are looking at one
3418      of the other qualified-id productions.  */
3419   else if (global_scope_p)
3420     {
3421       cp_token *token;
3422       tree id;
3423
3424       /* Peek at the next token.  */
3425       token = cp_lexer_peek_token (parser->lexer);
3426
3427       /* If it's an identifier, and the next token is not a "<", then
3428          we can avoid the template-id case.  This is an optimization
3429          for this common case.  */
3430       if (token->type == CPP_NAME
3431           && !cp_parser_nth_token_starts_template_argument_list_p
3432                (parser, 2))
3433         return cp_parser_identifier (parser);
3434
3435       cp_parser_parse_tentatively (parser);
3436       /* Try a template-id.  */
3437       id = cp_parser_template_id (parser,
3438                                   /*template_keyword_p=*/false,
3439                                   /*check_dependency_p=*/true,
3440                                   declarator_p);
3441       /* If that worked, we're done.  */
3442       if (cp_parser_parse_definitely (parser))
3443         return id;
3444
3445       /* Peek at the next token.  (Changes in the token buffer may
3446          have invalidated the pointer obtained above.)  */
3447       token = cp_lexer_peek_token (parser->lexer);
3448
3449       switch (token->type)
3450         {
3451         case CPP_NAME:
3452           return cp_parser_identifier (parser);
3453
3454         case CPP_KEYWORD:
3455           if (token->keyword == RID_OPERATOR)
3456             return cp_parser_operator_function_id (parser);
3457           /* Fall through.  */
3458
3459         default:
3460           cp_parser_error (parser, "expected id-expression");
3461           return error_mark_node;
3462         }
3463     }
3464   else
3465     return cp_parser_unqualified_id (parser, template_keyword_p,
3466                                      /*check_dependency_p=*/true,
3467                                      declarator_p,
3468                                      optional_p);
3469 }
3470
3471 /* Parse an unqualified-id.
3472
3473    unqualified-id:
3474      identifier
3475      operator-function-id
3476      conversion-function-id
3477      ~ class-name
3478      template-id
3479
3480    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3481    keyword, in a construct like `A::template ...'.
3482
3483    Returns a representation of unqualified-id.  For the `identifier'
3484    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3485    production a BIT_NOT_EXPR is returned; the operand of the
3486    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3487    other productions, see the documentation accompanying the
3488    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3489    names are looked up in uninstantiated templates.  If DECLARATOR_P
3490    is true, the unqualified-id is appearing as part of a declarator,
3491    rather than as part of an expression.  */
3492
3493 static tree
3494 cp_parser_unqualified_id (cp_parser* parser,
3495                           bool template_keyword_p,
3496                           bool check_dependency_p,
3497                           bool declarator_p,
3498                           bool optional_p)
3499 {
3500   cp_token *token;
3501
3502   /* Peek at the next token.  */
3503   token = cp_lexer_peek_token (parser->lexer);
3504
3505   switch (token->type)
3506     {
3507     case CPP_NAME:
3508       {
3509         tree id;
3510
3511         /* We don't know yet whether or not this will be a
3512            template-id.  */
3513         cp_parser_parse_tentatively (parser);
3514         /* Try a template-id.  */
3515         id = cp_parser_template_id (parser, template_keyword_p,
3516                                     check_dependency_p,
3517                                     declarator_p);
3518         /* If it worked, we're done.  */
3519         if (cp_parser_parse_definitely (parser))
3520           return id;
3521         /* Otherwise, it's an ordinary identifier.  */
3522         return cp_parser_identifier (parser);
3523       }
3524
3525     case CPP_TEMPLATE_ID:
3526       return cp_parser_template_id (parser, template_keyword_p,
3527                                     check_dependency_p,
3528                                     declarator_p);
3529
3530     case CPP_COMPL:
3531       {
3532         tree type_decl;
3533         tree qualifying_scope;
3534         tree object_scope;
3535         tree scope;
3536         bool done;
3537
3538         /* Consume the `~' token.  */
3539         cp_lexer_consume_token (parser->lexer);
3540         /* Parse the class-name.  The standard, as written, seems to
3541            say that:
3542
3543              template <typename T> struct S { ~S (); };
3544              template <typename T> S<T>::~S() {}
3545
3546            is invalid, since `~' must be followed by a class-name, but
3547            `S<T>' is dependent, and so not known to be a class.
3548            That's not right; we need to look in uninstantiated
3549            templates.  A further complication arises from:
3550
3551              template <typename T> void f(T t) {
3552                t.T::~T();
3553              }
3554
3555            Here, it is not possible to look up `T' in the scope of `T'
3556            itself.  We must look in both the current scope, and the
3557            scope of the containing complete expression.
3558
3559            Yet another issue is:
3560
3561              struct S {
3562                int S;
3563                ~S();
3564              };
3565
3566              S::~S() {}
3567
3568            The standard does not seem to say that the `S' in `~S'
3569            should refer to the type `S' and not the data member
3570            `S::S'.  */
3571
3572         /* DR 244 says that we look up the name after the "~" in the
3573            same scope as we looked up the qualifying name.  That idea
3574            isn't fully worked out; it's more complicated than that.  */
3575         scope = parser->scope;
3576         object_scope = parser->object_scope;
3577         qualifying_scope = parser->qualifying_scope;
3578
3579         /* Check for invalid scopes.  */
3580         if (scope == error_mark_node)
3581           {
3582             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3583               cp_lexer_consume_token (parser->lexer);
3584             return error_mark_node;
3585           }
3586         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3587           {
3588             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3589               error ("scope %qT before %<~%> is not a class-name", scope);
3590             cp_parser_simulate_error (parser);
3591             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3592               cp_lexer_consume_token (parser->lexer);
3593             return error_mark_node;
3594           }
3595         gcc_assert (!scope || TYPE_P (scope));
3596
3597         /* If the name is of the form "X::~X" it's OK.  */
3598         token = cp_lexer_peek_token (parser->lexer);
3599         if (scope
3600             && token->type == CPP_NAME
3601             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3602                 == CPP_OPEN_PAREN)
3603             && constructor_name_p (token->u.value, scope))
3604           {
3605             cp_lexer_consume_token (parser->lexer);
3606             return build_nt (BIT_NOT_EXPR, scope);
3607           }
3608
3609         /* If there was an explicit qualification (S::~T), first look
3610            in the scope given by the qualification (i.e., S).  */
3611         done = false;
3612         type_decl = NULL_TREE;
3613         if (scope)
3614           {
3615             cp_parser_parse_tentatively (parser);
3616             type_decl = cp_parser_class_name (parser,
3617                                               /*typename_keyword_p=*/false,
3618                                               /*template_keyword_p=*/false,
3619                                               none_type,
3620                                               /*check_dependency=*/false,
3621                                               /*class_head_p=*/false,
3622                                               declarator_p);
3623             if (cp_parser_parse_definitely (parser))
3624               done = true;
3625           }
3626         /* In "N::S::~S", look in "N" as well.  */
3627         if (!done && scope && qualifying_scope)
3628           {
3629             cp_parser_parse_tentatively (parser);
3630             parser->scope = qualifying_scope;
3631             parser->object_scope = NULL_TREE;
3632             parser->qualifying_scope = NULL_TREE;
3633             type_decl
3634               = cp_parser_class_name (parser,
3635                                       /*typename_keyword_p=*/false,
3636                                       /*template_keyword_p=*/false,
3637                                       none_type,
3638                                       /*check_dependency=*/false,
3639                                       /*class_head_p=*/false,
3640                                       declarator_p);
3641             if (cp_parser_parse_definitely (parser))
3642               done = true;
3643           }
3644         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3645         else if (!done && object_scope)
3646           {
3647             cp_parser_parse_tentatively (parser);
3648             parser->scope = object_scope;
3649             parser->object_scope = NULL_TREE;
3650             parser->qualifying_scope = NULL_TREE;
3651             type_decl
3652               = cp_parser_class_name (parser,
3653                                       /*typename_keyword_p=*/false,
3654                                       /*template_keyword_p=*/false,
3655                                       none_type,
3656                                       /*check_dependency=*/false,
3657                                       /*class_head_p=*/false,
3658                                       declarator_p);
3659             if (cp_parser_parse_definitely (parser))
3660               done = true;
3661           }
3662         /* Look in the surrounding context.  */
3663         if (!done)
3664           {
3665             parser->scope = NULL_TREE;
3666             parser->object_scope = NULL_TREE;
3667             parser->qualifying_scope = NULL_TREE;
3668             type_decl
3669               = cp_parser_class_name (parser,
3670                                       /*typename_keyword_p=*/false,
3671                                       /*template_keyword_p=*/false,
3672                                       none_type,
3673                                       /*check_dependency=*/false,
3674                                       /*class_head_p=*/false,
3675                                       declarator_p);
3676           }
3677         /* If an error occurred, assume that the name of the
3678            destructor is the same as the name of the qualifying
3679            class.  That allows us to keep parsing after running
3680            into ill-formed destructor names.  */
3681         if (type_decl == error_mark_node && scope)
3682           return build_nt (BIT_NOT_EXPR, scope);
3683         else if (type_decl == error_mark_node)
3684           return error_mark_node;
3685
3686         /* Check that destructor name and scope match.  */
3687         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3688           {
3689             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3690               error ("declaration of %<~%T%> as member of %qT",
3691                      type_decl, scope);
3692             cp_parser_simulate_error (parser);
3693             return error_mark_node;
3694           }
3695
3696         /* [class.dtor]
3697
3698            A typedef-name that names a class shall not be used as the
3699            identifier in the declarator for a destructor declaration.  */
3700         if (declarator_p
3701             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3702             && !DECL_SELF_REFERENCE_P (type_decl)
3703             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3704           error ("typedef-name %qD used as destructor declarator",
3705                  type_decl);
3706
3707         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3708       }
3709
3710     case CPP_KEYWORD:
3711       if (token->keyword == RID_OPERATOR)
3712         {
3713           tree id;
3714
3715           /* This could be a template-id, so we try that first.  */
3716           cp_parser_parse_tentatively (parser);
3717           /* Try a template-id.  */
3718           id = cp_parser_template_id (parser, template_keyword_p,
3719                                       /*check_dependency_p=*/true,
3720                                       declarator_p);
3721           /* If that worked, we're done.  */
3722           if (cp_parser_parse_definitely (parser))
3723             return id;
3724           /* We still don't know whether we're looking at an
3725              operator-function-id or a conversion-function-id.  */
3726           cp_parser_parse_tentatively (parser);
3727           /* Try an operator-function-id.  */
3728           id = cp_parser_operator_function_id (parser);
3729           /* If that didn't work, try a conversion-function-id.  */
3730           if (!cp_parser_parse_definitely (parser))
3731             id = cp_parser_conversion_function_id (parser);
3732
3733           return id;
3734         }
3735       /* Fall through.  */
3736
3737     default:
3738       if (optional_p)
3739         return NULL_TREE;
3740       cp_parser_error (parser, "expected unqualified-id");
3741       return error_mark_node;
3742     }
3743 }
3744
3745 /* Parse an (optional) nested-name-specifier.
3746
3747    nested-name-specifier:
3748      class-or-namespace-name :: nested-name-specifier [opt]
3749      class-or-namespace-name :: template nested-name-specifier [opt]
3750
3751    PARSER->SCOPE should be set appropriately before this function is
3752    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3753    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3754    in name lookups.
3755
3756    Sets PARSER->SCOPE to the class (TYPE) or namespace
3757    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3758    it unchanged if there is no nested-name-specifier.  Returns the new
3759    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3760
3761    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3762    part of a declaration and/or decl-specifier.  */
3763
3764 static tree
3765 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3766                                      bool typename_keyword_p,
3767                                      bool check_dependency_p,
3768                                      bool type_p,
3769                                      bool is_declaration)
3770 {
3771   bool success = false;
3772   cp_token_position start = 0;
3773   cp_token *token;
3774
3775   /* Remember where the nested-name-specifier starts.  */
3776   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3777     {
3778       start = cp_lexer_token_position (parser->lexer, false);
3779       push_deferring_access_checks (dk_deferred);
3780     }
3781
3782   while (true)
3783     {
3784       tree new_scope;
3785       tree old_scope;
3786       tree saved_qualifying_scope;
3787       bool template_keyword_p;
3788
3789       /* Spot cases that cannot be the beginning of a
3790          nested-name-specifier.  */
3791       token = cp_lexer_peek_token (parser->lexer);
3792
3793       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3794          the already parsed nested-name-specifier.  */
3795       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3796         {
3797           /* Grab the nested-name-specifier and continue the loop.  */
3798           cp_parser_pre_parsed_nested_name_specifier (parser);
3799           /* If we originally encountered this nested-name-specifier
3800              with IS_DECLARATION set to false, we will not have
3801              resolved TYPENAME_TYPEs, so we must do so here.  */
3802           if (is_declaration
3803               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3804             {
3805               new_scope = resolve_typename_type (parser->scope,
3806                                                  /*only_current_p=*/false);
3807               if (new_scope != error_mark_node)
3808                 parser->scope = new_scope;
3809             }
3810           success = true;
3811           continue;
3812         }
3813
3814       /* Spot cases that cannot be the beginning of a
3815          nested-name-specifier.  On the second and subsequent times
3816          through the loop, we look for the `template' keyword.  */
3817       if (success && token->keyword == RID_TEMPLATE)
3818         ;
3819       /* A template-id can start a nested-name-specifier.  */
3820       else if (token->type == CPP_TEMPLATE_ID)
3821         ;
3822       else
3823         {
3824           /* If the next token is not an identifier, then it is
3825              definitely not a class-or-namespace-name.  */
3826           if (token->type != CPP_NAME)
3827             break;
3828           /* If the following token is neither a `<' (to begin a
3829              template-id), nor a `::', then we are not looking at a
3830              nested-name-specifier.  */
3831           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3832           if (token->type != CPP_SCOPE
3833               && !cp_parser_nth_token_starts_template_argument_list_p
3834                   (parser, 2))
3835             break;
3836         }
3837
3838       /* The nested-name-specifier is optional, so we parse
3839          tentatively.  */
3840       cp_parser_parse_tentatively (parser);
3841
3842       /* Look for the optional `template' keyword, if this isn't the
3843          first time through the loop.  */
3844       if (success)
3845         template_keyword_p = cp_parser_optional_template_keyword (parser);
3846       else
3847         template_keyword_p = false;
3848
3849       /* Save the old scope since the name lookup we are about to do
3850          might destroy it.  */
3851       old_scope = parser->scope;
3852       saved_qualifying_scope = parser->qualifying_scope;
3853       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3854          look up names in "X<T>::I" in order to determine that "Y" is
3855          a template.  So, if we have a typename at this point, we make
3856          an effort to look through it.  */
3857       if (is_declaration
3858           && !typename_keyword_p
3859           && parser->scope
3860           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3861         parser->scope = resolve_typename_type (parser->scope,
3862                                                /*only_current_p=*/false);
3863       /* Parse the qualifying entity.  */
3864       new_scope
3865         = cp_parser_class_or_namespace_name (parser,
3866                                              typename_keyword_p,
3867                                              template_keyword_p,
3868                                              check_dependency_p,
3869                                              type_p,
3870                                              is_declaration);
3871       /* Look for the `::' token.  */
3872       cp_parser_require (parser, CPP_SCOPE, "`::'");
3873
3874       /* If we found what we wanted, we keep going; otherwise, we're
3875          done.  */
3876       if (!cp_parser_parse_definitely (parser))
3877         {
3878           bool error_p = false;
3879
3880           /* Restore the OLD_SCOPE since it was valid before the
3881              failed attempt at finding the last
3882              class-or-namespace-name.  */
3883           parser->scope = old_scope;
3884           parser->qualifying_scope = saved_qualifying_scope;
3885           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3886             break;
3887           /* If the next token is an identifier, and the one after
3888              that is a `::', then any valid interpretation would have
3889              found a class-or-namespace-name.  */
3890           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3891                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3892                      == CPP_SCOPE)
3893                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3894                      != CPP_COMPL))
3895             {
3896               token = cp_lexer_consume_token (parser->lexer);
3897               if (!error_p)
3898                 {
3899                   if (!token->ambiguous_p)
3900                     {
3901                       tree decl;
3902                       tree ambiguous_decls;
3903
3904                       decl = cp_parser_lookup_name (parser, token->u.value,
3905                                                     none_type,
3906                                                     /*is_template=*/false,
3907                                                     /*is_namespace=*/false,
3908                                                     /*check_dependency=*/true,
3909                                                     &ambiguous_decls);
3910                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3911                         error ("%qD used without template parameters", decl);
3912                       else if (ambiguous_decls)
3913                         {
3914                           error ("reference to %qD is ambiguous",
3915                                  token->u.value);
3916                           print_candidates (ambiguous_decls);
3917                           decl = error_mark_node;
3918                         }
3919                       else
3920                         cp_parser_name_lookup_error
3921                           (parser, token->u.value, decl,
3922                            "is not a class or namespace");
3923                     }
3924                   parser->scope = error_mark_node;
3925                   error_p = true;
3926                   /* Treat this as a successful nested-name-specifier
3927                      due to:
3928
3929                      [basic.lookup.qual]
3930
3931                      If the name found is not a class-name (clause
3932                      _class_) or namespace-name (_namespace.def_), the
3933                      program is ill-formed.  */
3934                   success = true;
3935                 }
3936               cp_lexer_consume_token (parser->lexer);
3937             }
3938           break;
3939         }
3940       /* We've found one valid nested-name-specifier.  */
3941       success = true;
3942       /* Name lookup always gives us a DECL.  */
3943       if (TREE_CODE (new_scope) == TYPE_DECL)
3944         new_scope = TREE_TYPE (new_scope);
3945       /* Uses of "template" must be followed by actual templates.  */
3946       if (template_keyword_p
3947           && !(CLASS_TYPE_P (new_scope)
3948                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3949                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3950                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3951           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3952                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3953                    == TEMPLATE_ID_EXPR)))
3954         pedwarn (TYPE_P (new_scope)
3955                  ? "%qT is not a template"
3956                  : "%qD is not a template",
3957                  new_scope);
3958       /* If it is a class scope, try to complete it; we are about to
3959          be looking up names inside the class.  */
3960       if (TYPE_P (new_scope)
3961           /* Since checking types for dependency can be expensive,
3962              avoid doing it if the type is already complete.  */
3963           && !COMPLETE_TYPE_P (new_scope)
3964           /* Do not try to complete dependent types.  */
3965           && !dependent_type_p (new_scope))
3966         new_scope = complete_type (new_scope);
3967       /* Make sure we look in the right scope the next time through
3968          the loop.  */
3969       parser->scope = new_scope;
3970     }
3971
3972   /* If parsing tentatively, replace the sequence of tokens that makes
3973      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3974      token.  That way, should we re-parse the token stream, we will
3975      not have to repeat the effort required to do the parse, nor will
3976      we issue duplicate error messages.  */
3977   if (success && start)
3978     {
3979       cp_token *token;
3980
3981       token = cp_lexer_token_at (parser->lexer, start);
3982       /* Reset the contents of the START token.  */
3983       token->type = CPP_NESTED_NAME_SPECIFIER;
3984       /* Retrieve any deferred checks.  Do not pop this access checks yet
3985          so the memory will not be reclaimed during token replacing below.  */
3986       token->u.tree_check_value = GGC_CNEW (struct tree_check);
3987       token->u.tree_check_value->value = parser->scope;
3988       token->u.tree_check_value->checks = get_deferred_access_checks ();
3989       token->u.tree_check_value->qualifying_scope =
3990         parser->qualifying_scope;
3991       token->keyword = RID_MAX;
3992
3993       /* Purge all subsequent tokens.  */
3994       cp_lexer_purge_tokens_after (parser->lexer, start);
3995     }
3996
3997   if (start)
3998     pop_to_parent_deferring_access_checks ();
3999
4000   return success ? parser->scope : NULL_TREE;
4001 }
4002
4003 /* Parse a nested-name-specifier.  See
4004    cp_parser_nested_name_specifier_opt for details.  This function
4005    behaves identically, except that it will an issue an error if no
4006    nested-name-specifier is present.  */
4007
4008 static tree
4009 cp_parser_nested_name_specifier (cp_parser *parser,
4010                                  bool typename_keyword_p,
4011                                  bool check_dependency_p,
4012                                  bool type_p,
4013                                  bool is_declaration)
4014 {
4015   tree scope;
4016
4017   /* Look for the nested-name-specifier.  */
4018   scope = cp_parser_nested_name_specifier_opt (parser,
4019                                                typename_keyword_p,
4020                                                check_dependency_p,
4021                                                type_p,
4022                                                is_declaration);
4023   /* If it was not present, issue an error message.  */
4024   if (!scope)
4025     {
4026       cp_parser_error (parser, "expected nested-name-specifier");
4027       parser->scope = NULL_TREE;
4028     }
4029
4030   return scope;
4031 }
4032
4033 /* Parse a class-or-namespace-name.
4034
4035    class-or-namespace-name:
4036      class-name
4037      namespace-name
4038
4039    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4040    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4041    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4042    TYPE_P is TRUE iff the next name should be taken as a class-name,
4043    even the same name is declared to be another entity in the same
4044    scope.
4045
4046    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4047    specified by the class-or-namespace-name.  If neither is found the
4048    ERROR_MARK_NODE is returned.  */
4049
4050 static tree
4051 cp_parser_class_or_namespace_name (cp_parser *parser,
4052                                    bool typename_keyword_p,
4053                                    bool template_keyword_p,
4054                                    bool check_dependency_p,
4055                                    bool type_p,
4056                                    bool is_declaration)
4057 {
4058   tree saved_scope;
4059   tree saved_qualifying_scope;
4060   tree saved_object_scope;
4061   tree scope;
4062   bool only_class_p;
4063
4064   /* Before we try to parse the class-name, we must save away the
4065      current PARSER->SCOPE since cp_parser_class_name will destroy
4066      it.  */
4067   saved_scope = parser->scope;
4068   saved_qualifying_scope = parser->qualifying_scope;
4069   saved_object_scope = parser->object_scope;
4070   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4071      there is no need to look for a namespace-name.  */
4072   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4073   if (!only_class_p)
4074     cp_parser_parse_tentatively (parser);
4075   scope = cp_parser_class_name (parser,
4076                                 typename_keyword_p,
4077                                 template_keyword_p,
4078                                 type_p ? class_type : none_type,
4079                                 check_dependency_p,
4080                                 /*class_head_p=*/false,
4081                                 is_declaration);
4082   /* If that didn't work, try for a namespace-name.  */
4083   if (!only_class_p && !cp_parser_parse_definitely (parser))
4084     {
4085       /* Restore the saved scope.  */
4086       parser->scope = saved_scope;
4087       parser->qualifying_scope = saved_qualifying_scope;
4088       parser->object_scope = saved_object_scope;
4089       /* If we are not looking at an identifier followed by the scope
4090          resolution operator, then this is not part of a
4091          nested-name-specifier.  (Note that this function is only used
4092          to parse the components of a nested-name-specifier.)  */
4093       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4094           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4095         return error_mark_node;
4096       scope = cp_parser_namespace_name (parser);
4097     }
4098
4099   return scope;
4100 }
4101
4102 /* Parse a postfix-expression.
4103
4104    postfix-expression:
4105      primary-expression
4106      postfix-expression [ expression ]
4107      postfix-expression ( expression-list [opt] )
4108      simple-type-specifier ( expression-list [opt] )
4109      typename :: [opt] nested-name-specifier identifier
4110        ( expression-list [opt] )
4111      typename :: [opt] nested-name-specifier template [opt] template-id
4112        ( expression-list [opt] )
4113      postfix-expression . template [opt] id-expression
4114      postfix-expression -> template [opt] id-expression
4115      postfix-expression . pseudo-destructor-name
4116      postfix-expression -> pseudo-destructor-name
4117      postfix-expression ++
4118      postfix-expression --
4119      dynamic_cast < type-id > ( expression )
4120      static_cast < type-id > ( expression )
4121      reinterpret_cast < type-id > ( expression )
4122      const_cast < type-id > ( expression )
4123      typeid ( expression )
4124      typeid ( type-id )
4125
4126    GNU Extension:
4127
4128    postfix-expression:
4129      ( type-id ) { initializer-list , [opt] }
4130
4131    This extension is a GNU version of the C99 compound-literal
4132    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4133    but they are essentially the same concept.)
4134
4135    If ADDRESS_P is true, the postfix expression is the operand of the
4136    `&' operator.  CAST_P is true if this expression is the target of a
4137    cast.
4138
4139    Returns a representation of the expression.  */
4140
4141 static tree
4142 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4143 {
4144   cp_token *token;
4145   enum rid keyword;
4146   cp_id_kind idk = CP_ID_KIND_NONE;
4147   tree postfix_expression = NULL_TREE;
4148
4149   /* Peek at the next token.  */
4150   token = cp_lexer_peek_token (parser->lexer);
4151   /* Some of the productions are determined by keywords.  */
4152   keyword = token->keyword;
4153   switch (keyword)
4154     {
4155     case RID_DYNCAST:
4156     case RID_STATCAST:
4157     case RID_REINTCAST:
4158     case RID_CONSTCAST:
4159       {
4160         tree type;
4161         tree expression;
4162         const char *saved_message;
4163
4164         /* All of these can be handled in the same way from the point
4165            of view of parsing.  Begin by consuming the token
4166            identifying the cast.  */
4167         cp_lexer_consume_token (parser->lexer);
4168
4169         /* New types cannot be defined in the cast.  */
4170         saved_message = parser->type_definition_forbidden_message;
4171         parser->type_definition_forbidden_message
4172           = "types may not be defined in casts";
4173
4174         /* Look for the opening `<'.  */
4175         cp_parser_require (parser, CPP_LESS, "`<'");
4176         /* Parse the type to which we are casting.  */
4177         type = cp_parser_type_id (parser);
4178         /* Look for the closing `>'.  */
4179         cp_parser_require (parser, CPP_GREATER, "`>'");
4180         /* Restore the old message.  */
4181         parser->type_definition_forbidden_message = saved_message;
4182
4183         /* And the expression which is being cast.  */
4184         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4185         expression = cp_parser_expression (parser, /*cast_p=*/true);
4186         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4187
4188         /* Only type conversions to integral or enumeration types
4189            can be used in constant-expressions.  */
4190         if (!cast_valid_in_integral_constant_expression_p (type)
4191             && (cp_parser_non_integral_constant_expression
4192                 (parser,
4193                  "a cast to a type other than an integral or "
4194                  "enumeration type")))
4195           return error_mark_node;
4196
4197         switch (keyword)
4198           {
4199           case RID_DYNCAST:
4200             postfix_expression
4201               = build_dynamic_cast (type, expression);
4202             break;
4203           case RID_STATCAST:
4204             postfix_expression
4205               = build_static_cast (type, expression);
4206             break;
4207           case RID_REINTCAST:
4208             postfix_expression
4209               = build_reinterpret_cast (type, expression);
4210             break;
4211           case RID_CONSTCAST:
4212             postfix_expression
4213               = build_const_cast (type, expression);
4214             break;
4215           default:
4216             gcc_unreachable ();
4217           }
4218       }
4219       break;
4220
4221     case RID_TYPEID:
4222       {
4223         tree type;
4224         const char *saved_message;
4225         bool saved_in_type_id_in_expr_p;
4226
4227         /* Consume the `typeid' token.  */
4228         cp_lexer_consume_token (parser->lexer);
4229         /* Look for the `(' token.  */
4230         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4231         /* Types cannot be defined in a `typeid' expression.  */
4232         saved_message = parser->type_definition_forbidden_message;
4233         parser->type_definition_forbidden_message
4234           = "types may not be defined in a `typeid\' expression";
4235         /* We can't be sure yet whether we're looking at a type-id or an
4236            expression.  */
4237         cp_parser_parse_tentatively (parser);
4238         /* Try a type-id first.  */
4239         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4240         parser->in_type_id_in_expr_p = true;
4241         type = cp_parser_type_id (parser);
4242         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4243         /* Look for the `)' token.  Otherwise, we can't be sure that
4244            we're not looking at an expression: consider `typeid (int
4245            (3))', for example.  */
4246         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4247         /* If all went well, simply lookup the type-id.  */
4248         if (cp_parser_parse_definitely (parser))
4249           postfix_expression = get_typeid (type);
4250         /* Otherwise, fall back to the expression variant.  */
4251         else
4252           {
4253             tree expression;
4254
4255             /* Look for an expression.  */
4256             expression = cp_parser_expression (parser, /*cast_p=*/false);
4257             /* Compute its typeid.  */
4258             postfix_expression = build_typeid (expression);
4259             /* Look for the `)' token.  */
4260             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4261           }
4262         /* Restore the saved message.  */
4263         parser->type_definition_forbidden_message = saved_message;
4264         /* `typeid' may not appear in an integral constant expression.  */
4265         if (cp_parser_non_integral_constant_expression(parser,
4266                                                        "`typeid' operator"))
4267           return error_mark_node;
4268       }
4269       break;
4270
4271     case RID_TYPENAME:
4272       {
4273         tree type;
4274         /* The syntax permitted here is the same permitted for an
4275            elaborated-type-specifier.  */
4276         type = cp_parser_elaborated_type_specifier (parser,
4277                                                     /*is_friend=*/false,
4278                                                     /*is_declaration=*/false);
4279         postfix_expression = cp_parser_functional_cast (parser, type);
4280       }
4281       break;
4282
4283     default:
4284       {
4285         tree type;
4286
4287         /* If the next thing is a simple-type-specifier, we may be
4288            looking at a functional cast.  We could also be looking at
4289            an id-expression.  So, we try the functional cast, and if
4290            that doesn't work we fall back to the primary-expression.  */
4291         cp_parser_parse_tentatively (parser);
4292         /* Look for the simple-type-specifier.  */
4293         type = cp_parser_simple_type_specifier (parser,
4294                                                 /*decl_specs=*/NULL,
4295                                                 CP_PARSER_FLAGS_NONE);
4296         /* Parse the cast itself.  */
4297         if (!cp_parser_error_occurred (parser))
4298           postfix_expression
4299             = cp_parser_functional_cast (parser, type);
4300         /* If that worked, we're done.  */
4301         if (cp_parser_parse_definitely (parser))
4302           break;
4303
4304         /* If the functional-cast didn't work out, try a
4305            compound-literal.  */
4306         if (cp_parser_allow_gnu_extensions_p (parser)
4307             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4308           {
4309             VEC(constructor_elt,gc) *initializer_list = NULL;
4310             bool saved_in_type_id_in_expr_p;
4311
4312             cp_parser_parse_tentatively (parser);
4313             /* Consume the `('.  */
4314             cp_lexer_consume_token (parser->lexer);
4315             /* Parse the type.  */
4316             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4317             parser->in_type_id_in_expr_p = true;
4318             type = cp_parser_type_id (parser);
4319             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4320             /* Look for the `)'.  */
4321             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4322             /* Look for the `{'.  */
4323             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4324             /* If things aren't going well, there's no need to
4325                keep going.  */
4326             if (!cp_parser_error_occurred (parser))
4327               {
4328                 bool non_constant_p;
4329                 /* Parse the initializer-list.  */
4330                 initializer_list
4331                   = cp_parser_initializer_list (parser, &non_constant_p);
4332                 /* Allow a trailing `,'.  */
4333                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4334                   cp_lexer_consume_token (parser->lexer);
4335                 /* Look for the final `}'.  */
4336                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4337               }
4338             /* If that worked, we're definitely looking at a
4339                compound-literal expression.  */
4340             if (cp_parser_parse_definitely (parser))
4341               {
4342                 /* Warn the user that a compound literal is not
4343                    allowed in standard C++.  */
4344                 if (pedantic)
4345                   pedwarn ("ISO C++ forbids compound-literals");
4346                 /* For simplicitly, we disallow compound literals in
4347                    constant-expressions for simpliicitly.  We could
4348                    allow compound literals of integer type, whose
4349                    initializer was a constant, in constant
4350                    expressions.  Permitting that usage, as a further
4351                    extension, would not change the meaning of any
4352                    currently accepted programs.  (Of course, as
4353                    compound literals are not part of ISO C++, the
4354                    standard has nothing to say.)  */
4355                 if (cp_parser_non_integral_constant_expression 
4356                     (parser, "non-constant compound literals"))
4357                   {
4358                     postfix_expression = error_mark_node;
4359                     break;
4360                   }
4361                 /* Form the representation of the compound-literal.  */
4362                 postfix_expression
4363                   = finish_compound_literal (type, initializer_list);
4364                 break;
4365               }
4366           }
4367
4368         /* It must be a primary-expression.  */
4369         postfix_expression
4370           = cp_parser_primary_expression (parser, address_p, cast_p,
4371                                           /*template_arg_p=*/false,
4372                                           &idk);
4373       }
4374       break;
4375     }
4376
4377   /* Keep looping until the postfix-expression is complete.  */
4378   while (true)
4379     {
4380       if (idk == CP_ID_KIND_UNQUALIFIED
4381           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4382           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4383         /* It is not a Koenig lookup function call.  */
4384         postfix_expression
4385           = unqualified_name_lookup_error (postfix_expression);
4386
4387       /* Peek at the next token.  */
4388       token = cp_lexer_peek_token (parser->lexer);
4389
4390       switch (token->type)
4391         {
4392         case CPP_OPEN_SQUARE:
4393           postfix_expression
4394             = cp_parser_postfix_open_square_expression (parser,
4395                                                         postfix_expression,
4396                                                         false);
4397           idk = CP_ID_KIND_NONE;
4398           break;
4399
4400         case CPP_OPEN_PAREN:
4401           /* postfix-expression ( expression-list [opt] ) */
4402           {
4403             bool koenig_p;
4404             bool is_builtin_constant_p;
4405             bool saved_integral_constant_expression_p = false;
4406             bool saved_non_integral_constant_expression_p = false;
4407             tree args;
4408
4409             is_builtin_constant_p
4410               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4411             if (is_builtin_constant_p)
4412               {
4413                 /* The whole point of __builtin_constant_p is to allow
4414                    non-constant expressions to appear as arguments.  */
4415                 saved_integral_constant_expression_p
4416                   = parser->integral_constant_expression_p;
4417                 saved_non_integral_constant_expression_p
4418                   = parser->non_integral_constant_expression_p;
4419                 parser->integral_constant_expression_p = false;
4420               }
4421             args = (cp_parser_parenthesized_expression_list
4422                     (parser, /*is_attribute_list=*/false,
4423                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4424                      /*non_constant_p=*/NULL));
4425             if (is_builtin_constant_p)
4426               {
4427                 parser->integral_constant_expression_p
4428                   = saved_integral_constant_expression_p;
4429                 parser->non_integral_constant_expression_p
4430                   = saved_non_integral_constant_expression_p;
4431               }
4432
4433             if (args == error_mark_node)
4434               {
4435                 postfix_expression = error_mark_node;
4436                 break;
4437               }
4438
4439             /* Function calls are not permitted in
4440                constant-expressions.  */
4441             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4442                 && cp_parser_non_integral_constant_expression (parser,
4443                                                                "a function call"))
4444               {
4445                 postfix_expression = error_mark_node;
4446                 break;
4447               }
4448
4449             koenig_p = false;
4450             if (idk == CP_ID_KIND_UNQUALIFIED)
4451               {
4452                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4453                   {
4454                     if (args)
4455                       {
4456                         koenig_p = true;
4457                         postfix_expression
4458                           = perform_koenig_lookup (postfix_expression, args);
4459                       }
4460                     else
4461                       postfix_expression
4462                         = unqualified_fn_lookup_error (postfix_expression);
4463                   }
4464                 /* We do not perform argument-dependent lookup if
4465                    normal lookup finds a non-function, in accordance
4466                    with the expected resolution of DR 218.  */
4467                 else if (args && is_overloaded_fn (postfix_expression))
4468                   {
4469                     tree fn = get_first_fn (postfix_expression);
4470
4471                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4472                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4473
4474                     /* Only do argument dependent lookup if regular
4475                        lookup does not find a set of member functions.
4476                        [basic.lookup.koenig]/2a  */
4477                     if (!DECL_FUNCTION_MEMBER_P (fn))
4478                       {
4479                         koenig_p = true;
4480                         postfix_expression
4481                           = perform_koenig_lookup (postfix_expression, args);
4482                       }
4483                   }
4484               }
4485
4486             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4487               {
4488                 tree instance = TREE_OPERAND (postfix_expression, 0);
4489                 tree fn = TREE_OPERAND (postfix_expression, 1);
4490
4491                 if (processing_template_decl
4492                     && (type_dependent_expression_p (instance)
4493                         || (!BASELINK_P (fn)
4494                             && TREE_CODE (fn) != FIELD_DECL)
4495                         || type_dependent_expression_p (fn)
4496                         || any_type_dependent_arguments_p (args)))
4497                   {
4498                     postfix_expression
4499                       = build_nt_call_list (postfix_expression, args);
4500                     break;
4501                   }
4502
4503                 if (BASELINK_P (fn))
4504                   postfix_expression
4505                     = (build_new_method_call
4506                        (instance, fn, args, NULL_TREE,
4507                         (idk == CP_ID_KIND_QUALIFIED
4508                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4509                         /*fn_p=*/NULL));
4510                 else
4511                   postfix_expression
4512                     = finish_call_expr (postfix_expression, args,
4513                                         /*disallow_virtual=*/false,
4514                                         /*koenig_p=*/false);
4515               }
4516             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4517                      || TREE_CODE (postfix_expression) == MEMBER_REF
4518                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4519               postfix_expression = (build_offset_ref_call_from_tree
4520                                     (postfix_expression, args));
4521             else if (idk == CP_ID_KIND_QUALIFIED)
4522               /* A call to a static class member, or a namespace-scope
4523                  function.  */
4524               postfix_expression
4525                 = finish_call_expr (postfix_expression, args,
4526                                     /*disallow_virtual=*/true,
4527                                     koenig_p);
4528             else
4529               /* All other function calls.  */
4530               postfix_expression
4531                 = finish_call_expr (postfix_expression, args,
4532                                     /*disallow_virtual=*/false,
4533                                     koenig_p);
4534
4535             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4536             idk = CP_ID_KIND_NONE;
4537           }
4538           break;
4539
4540         case CPP_DOT:
4541         case CPP_DEREF:
4542           /* postfix-expression . template [opt] id-expression
4543              postfix-expression . pseudo-destructor-name
4544              postfix-expression -> template [opt] id-expression
4545              postfix-expression -> pseudo-destructor-name */
4546
4547           /* Consume the `.' or `->' operator.  */
4548           cp_lexer_consume_token (parser->lexer);
4549
4550           postfix_expression
4551             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4552                                                       postfix_expression,
4553                                                       false, &idk);
4554           break;
4555
4556         case CPP_PLUS_PLUS:
4557           /* postfix-expression ++  */
4558           /* Consume the `++' token.  */
4559           cp_lexer_consume_token (parser->lexer);
4560           /* Generate a representation for the complete expression.  */
4561           postfix_expression
4562             = finish_increment_expr (postfix_expression,
4563                                      POSTINCREMENT_EXPR);
4564           /* Increments may not appear in constant-expressions.  */
4565           if (cp_parser_non_integral_constant_expression (parser,
4566                                                           "an increment"))
4567             postfix_expression = error_mark_node;
4568           idk = CP_ID_KIND_NONE;
4569           break;
4570
4571         case CPP_MINUS_MINUS:
4572           /* postfix-expression -- */
4573           /* Consume the `--' token.  */
4574           cp_lexer_consume_token (parser->lexer);
4575           /* Generate a representation for the complete expression.  */
4576           postfix_expression
4577             = finish_increment_expr (postfix_expression,
4578                                      POSTDECREMENT_EXPR);
4579           /* Decrements may not appear in constant-expressions.  */
4580           if (cp_parser_non_integral_constant_expression (parser,
4581                                                           "a decrement"))
4582             postfix_expression = error_mark_node;
4583           idk = CP_ID_KIND_NONE;
4584           break;
4585
4586         default:
4587           return postfix_expression;
4588         }
4589     }
4590
4591   /* We should never get here.  */
4592   gcc_unreachable ();
4593   return error_mark_node;
4594 }
4595
4596 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4597    by cp_parser_builtin_offsetof.  We're looking for
4598
4599      postfix-expression [ expression ]
4600
4601    FOR_OFFSETOF is set if we're being called in that context, which
4602    changes how we deal with integer constant expressions.  */
4603
4604 static tree
4605 cp_parser_postfix_open_square_expression (cp_parser *parser,
4606                                           tree postfix_expression,
4607                                           bool for_offsetof)
4608 {
4609   tree index;
4610
4611   /* Consume the `[' token.  */
4612   cp_lexer_consume_token (parser->lexer);
4613
4614   /* Parse the index expression.  */
4615   /* ??? For offsetof, there is a question of what to allow here.  If
4616      offsetof is not being used in an integral constant expression context,
4617      then we *could* get the right answer by computing the value at runtime.
4618      If we are in an integral constant expression context, then we might
4619      could accept any constant expression; hard to say without analysis.
4620      Rather than open the barn door too wide right away, allow only integer
4621      constant expressions here.  */
4622   if (for_offsetof)
4623     index = cp_parser_constant_expression (parser, false, NULL);
4624   else
4625     index = cp_parser_expression (parser, /*cast_p=*/false);
4626
4627   /* Look for the closing `]'.  */
4628   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4629
4630   /* Build the ARRAY_REF.  */
4631   postfix_expression = grok_array_decl (postfix_expression, index);
4632
4633   /* When not doing offsetof, array references are not permitted in
4634      constant-expressions.  */
4635   if (!for_offsetof
4636       && (cp_parser_non_integral_constant_expression
4637           (parser, "an array reference")))
4638     postfix_expression = error_mark_node;
4639
4640   return postfix_expression;
4641 }
4642
4643 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4644    by cp_parser_builtin_offsetof.  We're looking for
4645
4646      postfix-expression . template [opt] id-expression
4647      postfix-expression . pseudo-destructor-name
4648      postfix-expression -> template [opt] id-expression
4649      postfix-expression -> pseudo-destructor-name
4650
4651    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4652    limits what of the above we'll actually accept, but nevermind.
4653    TOKEN_TYPE is the "." or "->" token, which will already have been
4654    removed from the stream.  */
4655
4656 static tree
4657 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4658                                         enum cpp_ttype token_type,
4659                                         tree postfix_expression,
4660                                         bool for_offsetof, cp_id_kind *idk)
4661 {
4662   tree name;
4663   bool dependent_p;
4664   bool pseudo_destructor_p;
4665   tree scope = NULL_TREE;
4666
4667   /* If this is a `->' operator, dereference the pointer.  */
4668   if (token_type == CPP_DEREF)
4669     postfix_expression = build_x_arrow (postfix_expression);
4670   /* Check to see whether or not the expression is type-dependent.  */
4671   dependent_p = type_dependent_expression_p (postfix_expression);
4672   /* The identifier following the `->' or `.' is not qualified.  */
4673   parser->scope = NULL_TREE;
4674   parser->qualifying_scope = NULL_TREE;
4675   parser->object_scope = NULL_TREE;
4676   *idk = CP_ID_KIND_NONE;
4677   /* Enter the scope corresponding to the type of the object
4678      given by the POSTFIX_EXPRESSION.  */
4679   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4680     {
4681       scope = TREE_TYPE (postfix_expression);
4682       /* According to the standard, no expression should ever have
4683          reference type.  Unfortunately, we do not currently match
4684          the standard in this respect in that our internal representation
4685          of an expression may have reference type even when the standard
4686          says it does not.  Therefore, we have to manually obtain the
4687          underlying type here.  */
4688       scope = non_reference (scope);
4689       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4690       if (scope == unknown_type_node)
4691         {
4692           error ("%qE does not have class type", postfix_expression);
4693           scope = NULL_TREE;
4694         }
4695       else
4696         scope = complete_type_or_else (scope, NULL_TREE);
4697       /* Let the name lookup machinery know that we are processing a
4698          class member access expression.  */
4699       parser->context->object_type = scope;
4700       /* If something went wrong, we want to be able to discern that case,
4701          as opposed to the case where there was no SCOPE due to the type
4702          of expression being dependent.  */
4703       if (!scope)
4704         scope = error_mark_node;
4705       /* If the SCOPE was erroneous, make the various semantic analysis
4706          functions exit quickly -- and without issuing additional error
4707          messages.  */
4708       if (scope == error_mark_node)
4709         postfix_expression = error_mark_node;
4710     }
4711
4712   /* Assume this expression is not a pseudo-destructor access.  */
4713   pseudo_destructor_p = false;
4714
4715   /* If the SCOPE is a scalar type, then, if this is a valid program,
4716      we must be looking at a pseudo-destructor-name.  */
4717   if (scope && SCALAR_TYPE_P (scope))
4718     {
4719       tree s;
4720       tree type;
4721
4722       cp_parser_parse_tentatively (parser);
4723       /* Parse the pseudo-destructor-name.  */
4724       s = NULL_TREE;
4725       cp_parser_pseudo_destructor_name (parser, &s, &type);
4726       if (cp_parser_parse_definitely (parser))
4727         {
4728           pseudo_destructor_p = true;
4729           postfix_expression
4730             = finish_pseudo_destructor_expr (postfix_expression,
4731                                              s, TREE_TYPE (type));
4732         }
4733     }
4734
4735   if (!pseudo_destructor_p)
4736     {
4737       /* If the SCOPE is not a scalar type, we are looking at an
4738          ordinary class member access expression, rather than a
4739          pseudo-destructor-name.  */
4740       bool template_p;
4741       /* Parse the id-expression.  */
4742       name = (cp_parser_id_expression
4743               (parser,
4744                cp_parser_optional_template_keyword (parser),
4745                /*check_dependency_p=*/true,
4746                &template_p,
4747                /*declarator_p=*/false,
4748                /*optional_p=*/false));
4749       /* In general, build a SCOPE_REF if the member name is qualified.
4750          However, if the name was not dependent and has already been
4751          resolved; there is no need to build the SCOPE_REF.  For example;
4752
4753              struct X { void f(); };
4754              template <typename T> void f(T* t) { t->X::f(); }
4755
4756          Even though "t" is dependent, "X::f" is not and has been resolved
4757          to a BASELINK; there is no need to include scope information.  */
4758
4759       /* But we do need to remember that there was an explicit scope for
4760          virtual function calls.  */
4761       if (parser->scope)
4762         *idk = CP_ID_KIND_QUALIFIED;
4763
4764       /* If the name is a template-id that names a type, we will get a
4765          TYPE_DECL here.  That is invalid code.  */
4766       if (TREE_CODE (name) == TYPE_DECL)
4767         {
4768           error ("invalid use of %qD", name);
4769           postfix_expression = error_mark_node;
4770         }
4771       else
4772         {
4773           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4774             {
4775               name = build_qualified_name (/*type=*/NULL_TREE,
4776                                            parser->scope,
4777                                            name,
4778                                            template_p);
4779               parser->scope = NULL_TREE;
4780               parser->qualifying_scope = NULL_TREE;
4781               parser->object_scope = NULL_TREE;
4782             }
4783           if (scope && name && BASELINK_P (name))
4784             adjust_result_of_qualified_name_lookup
4785               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4786           postfix_expression
4787             = finish_class_member_access_expr (postfix_expression, name,
4788                                                template_p);
4789         }
4790     }
4791
4792   /* We no longer need to look up names in the scope of the object on
4793      the left-hand side of the `.' or `->' operator.  */
4794   parser->context->object_type = NULL_TREE;
4795
4796   /* Outside of offsetof, these operators may not appear in
4797      constant-expressions.  */
4798   if (!for_offsetof
4799       && (cp_parser_non_integral_constant_expression
4800           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4801     postfix_expression = error_mark_node;
4802
4803   return postfix_expression;
4804 }
4805
4806 /* Parse a parenthesized expression-list.
4807
4808    expression-list:
4809      assignment-expression
4810      expression-list, assignment-expression
4811
4812    attribute-list:
4813      expression-list
4814      identifier
4815      identifier, expression-list
4816
4817    CAST_P is true if this expression is the target of a cast.
4818
4819    ALLOW_EXPANSION_P is true if this expression allows expansion of an
4820    argument pack.
4821
4822    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4823    representation of an assignment-expression.  Note that a TREE_LIST
4824    is returned even if there is only a single expression in the list.
4825    error_mark_node is returned if the ( and or ) are
4826    missing. NULL_TREE is returned on no expressions. The parentheses
4827    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4828    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4829    indicates whether or not all of the expressions in the list were
4830    constant.  */
4831
4832 static tree
4833 cp_parser_parenthesized_expression_list (cp_parser* parser,
4834                                          bool is_attribute_list,
4835                                          bool cast_p,
4836                                          bool allow_expansion_p,
4837                                          bool *non_constant_p)
4838 {
4839   tree expression_list = NULL_TREE;
4840   bool fold_expr_p = is_attribute_list;
4841   tree identifier = NULL_TREE;
4842
4843   /* Assume all the expressions will be constant.  */
4844   if (non_constant_p)
4845     *non_constant_p = false;
4846
4847   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4848     return error_mark_node;
4849
4850   /* Consume expressions until there are no more.  */
4851   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4852     while (true)
4853       {
4854         tree expr;
4855
4856         /* At the beginning of attribute lists, check to see if the
4857            next token is an identifier.  */
4858         if (is_attribute_list
4859             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4860           {
4861             cp_token *token;
4862
4863             /* Consume the identifier.  */
4864             token = cp_lexer_consume_token (parser->lexer);
4865             /* Save the identifier.  */
4866             identifier = token->u.value;
4867           }
4868         else
4869           {
4870             /* Parse the next assignment-expression.  */
4871             if (non_constant_p)
4872               {
4873                 bool expr_non_constant_p;
4874                 expr = (cp_parser_constant_expression
4875                         (parser, /*allow_non_constant_p=*/true,
4876                          &expr_non_constant_p));
4877                 if (expr_non_constant_p)
4878                   *non_constant_p = true;
4879               }
4880             else
4881               expr = cp_parser_assignment_expression (parser, cast_p);
4882
4883             if (fold_expr_p)
4884               expr = fold_non_dependent_expr (expr);
4885
4886             /* If we have an ellipsis, then this is an expression
4887                expansion.  */
4888             if (allow_expansion_p
4889                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4890               {
4891                 /* Consume the `...'.  */
4892                 cp_lexer_consume_token (parser->lexer);
4893
4894                 /* Build the argument pack.  */
4895                 expr = make_pack_expansion (expr);
4896               }
4897
4898              /* Add it to the list.  We add error_mark_node
4899                 expressions to the list, so that we can still tell if
4900                 the correct form for a parenthesized expression-list
4901                 is found. That gives better errors.  */
4902             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4903
4904             if (expr == error_mark_node)
4905               goto skip_comma;
4906           }
4907
4908         /* After the first item, attribute lists look the same as
4909            expression lists.  */
4910         is_attribute_list = false;
4911
4912       get_comma:;
4913         /* If the next token isn't a `,', then we are done.  */
4914         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4915           break;
4916
4917         /* Otherwise, consume the `,' and keep going.  */
4918         cp_lexer_consume_token (parser->lexer);
4919       }
4920
4921   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4922     {
4923       int ending;
4924
4925     skip_comma:;
4926       /* We try and resync to an unnested comma, as that will give the
4927          user better diagnostics.  */
4928       ending = cp_parser_skip_to_closing_parenthesis (parser,
4929                                                       /*recovering=*/true,
4930                                                       /*or_comma=*/true,
4931                                                       /*consume_paren=*/true);
4932       if (ending < 0)
4933         goto get_comma;
4934       if (!ending)
4935         return error_mark_node;
4936     }
4937
4938   /* We built up the list in reverse order so we must reverse it now.  */
4939   expression_list = nreverse (expression_list);
4940   if (identifier)
4941     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4942
4943   return expression_list;
4944 }
4945
4946 /* Parse a pseudo-destructor-name.
4947
4948    pseudo-destructor-name:
4949      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4950      :: [opt] nested-name-specifier template template-id :: ~ type-name
4951      :: [opt] nested-name-specifier [opt] ~ type-name
4952
4953    If either of the first two productions is used, sets *SCOPE to the
4954    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4955    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4956    or ERROR_MARK_NODE if the parse fails.  */
4957
4958 static void
4959 cp_parser_pseudo_destructor_name (cp_parser* parser,
4960                                   tree* scope,
4961                                   tree* type)
4962 {
4963   bool nested_name_specifier_p;
4964
4965   /* Assume that things will not work out.  */
4966   *type = error_mark_node;
4967
4968   /* Look for the optional `::' operator.  */
4969   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4970   /* Look for the optional nested-name-specifier.  */
4971   nested_name_specifier_p
4972     = (cp_parser_nested_name_specifier_opt (parser,
4973                                             /*typename_keyword_p=*/false,
4974                                             /*check_dependency_p=*/true,
4975                                             /*type_p=*/false,
4976                                             /*is_declaration=*/true)
4977        != NULL_TREE);
4978   /* Now, if we saw a nested-name-specifier, we might be doing the
4979      second production.  */
4980   if (nested_name_specifier_p
4981       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4982     {
4983       /* Consume the `template' keyword.  */
4984       cp_lexer_consume_token (parser->lexer);
4985       /* Parse the template-id.  */
4986       cp_parser_template_id (parser,
4987                              /*template_keyword_p=*/true,
4988                              /*check_dependency_p=*/false,
4989                              /*is_declaration=*/true);
4990       /* Look for the `::' token.  */
4991       cp_parser_require (parser, CPP_SCOPE, "`::'");
4992     }
4993   /* If the next token is not a `~', then there might be some
4994      additional qualification.  */
4995   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4996     {
4997       /* Look for the type-name.  */
4998       *scope = TREE_TYPE (cp_parser_type_name (parser));
4999
5000       if (*scope == error_mark_node)
5001         return;
5002
5003       /* If we don't have ::~, then something has gone wrong.  Since
5004          the only caller of this function is looking for something
5005          after `.' or `->' after a scalar type, most likely the
5006          program is trying to get a member of a non-aggregate
5007          type.  */
5008       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5009           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5010         {
5011           cp_parser_error (parser, "request for member of non-aggregate type");
5012           return;
5013         }
5014
5015       /* Look for the `::' token.  */
5016       cp_parser_require (parser, CPP_SCOPE, "`::'");
5017     }
5018   else
5019     *scope = NULL_TREE;
5020
5021   /* Look for the `~'.  */
5022   cp_parser_require (parser, CPP_COMPL, "`~'");
5023   /* Look for the type-name again.  We are not responsible for
5024      checking that it matches the first type-name.  */
5025   *type = cp_parser_type_name (parser);
5026 }
5027
5028 /* Parse a unary-expression.
5029
5030    unary-expression:
5031      postfix-expression
5032      ++ cast-expression
5033      -- cast-expression
5034      unary-operator cast-expression
5035      sizeof unary-expression
5036      sizeof ( type-id )
5037      new-expression
5038      delete-expression
5039
5040    GNU Extensions:
5041
5042    unary-expression:
5043      __extension__ cast-expression
5044      __alignof__ unary-expression
5045      __alignof__ ( type-id )
5046      __real__ cast-expression
5047      __imag__ cast-expression
5048      && identifier
5049
5050    ADDRESS_P is true iff the unary-expression is appearing as the
5051    operand of the `&' operator.   CAST_P is true if this expression is
5052    the target of a cast.
5053
5054    Returns a representation of the expression.  */
5055
5056 static tree
5057 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5058 {
5059   cp_token *token;
5060   enum tree_code unary_operator;
5061
5062   /* Peek at the next token.  */
5063   token = cp_lexer_peek_token (parser->lexer);
5064   /* Some keywords give away the kind of expression.  */
5065   if (token->type == CPP_KEYWORD)
5066     {
5067       enum rid keyword = token->keyword;
5068
5069       switch (keyword)
5070         {
5071         case RID_ALIGNOF:
5072         case RID_SIZEOF:
5073           {
5074             tree operand;
5075             enum tree_code op;
5076
5077             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5078             /* Consume the token.  */
5079             cp_lexer_consume_token (parser->lexer);
5080             /* Parse the operand.  */
5081             operand = cp_parser_sizeof_operand (parser, keyword);
5082
5083             if (TYPE_P (operand))
5084               return cxx_sizeof_or_alignof_type (operand, op, true);
5085             else
5086               return cxx_sizeof_or_alignof_expr (operand, op);
5087           }
5088
5089         case RID_NEW:
5090           return cp_parser_new_expression (parser);
5091
5092         case RID_DELETE:
5093           return cp_parser_delete_expression (parser);
5094
5095         case RID_EXTENSION:
5096           {
5097             /* The saved value of the PEDANTIC flag.  */
5098             int saved_pedantic;
5099             tree expr;
5100
5101             /* Save away the PEDANTIC flag.  */
5102             cp_parser_extension_opt (parser, &saved_pedantic);
5103             /* Parse the cast-expression.  */
5104             expr = cp_parser_simple_cast_expression (parser);
5105             /* Restore the PEDANTIC flag.  */
5106             pedantic = saved_pedantic;
5107
5108             return expr;
5109           }
5110
5111         case RID_REALPART:
5112         case RID_IMAGPART:
5113           {
5114             tree expression;
5115
5116             /* Consume the `__real__' or `__imag__' token.  */
5117             cp_lexer_consume_token (parser->lexer);
5118             /* Parse the cast-expression.  */
5119             expression = cp_parser_simple_cast_expression (parser);
5120             /* Create the complete representation.  */
5121             return build_x_unary_op ((keyword == RID_REALPART
5122                                       ? REALPART_EXPR : IMAGPART_EXPR),
5123                                      expression);
5124           }
5125           break;
5126
5127         default:
5128           break;
5129         }
5130     }
5131
5132   /* Look for the `:: new' and `:: delete', which also signal the
5133      beginning of a new-expression, or delete-expression,
5134      respectively.  If the next token is `::', then it might be one of
5135      these.  */
5136   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5137     {
5138       enum rid keyword;
5139
5140       /* See if the token after the `::' is one of the keywords in
5141          which we're interested.  */
5142       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5143       /* If it's `new', we have a new-expression.  */
5144       if (keyword == RID_NEW)
5145         return cp_parser_new_expression (parser);
5146       /* Similarly, for `delete'.  */
5147       else if (keyword == RID_DELETE)
5148         return cp_parser_delete_expression (parser);
5149     }
5150
5151   /* Look for a unary operator.  */
5152   unary_operator = cp_parser_unary_operator (token);
5153   /* The `++' and `--' operators can be handled similarly, even though
5154      they are not technically unary-operators in the grammar.  */
5155   if (unary_operator == ERROR_MARK)
5156     {
5157       if (token->type == CPP_PLUS_PLUS)
5158         unary_operator = PREINCREMENT_EXPR;
5159       else if (token->type == CPP_MINUS_MINUS)
5160         unary_operator = PREDECREMENT_EXPR;
5161       /* Handle the GNU address-of-label extension.  */
5162       else if (cp_parser_allow_gnu_extensions_p (parser)
5163                && token->type == CPP_AND_AND)
5164         {
5165           tree identifier;
5166
5167           /* Consume the '&&' token.  */
5168           cp_lexer_consume_token (parser->lexer);
5169           /* Look for the identifier.  */
5170           identifier = cp_parser_identifier (parser);
5171           /* Create an expression representing the address.  */
5172           return finish_label_address_expr (identifier);
5173         }
5174     }
5175   if (unary_operator != ERROR_MARK)
5176     {
5177       tree cast_expression;
5178       tree expression = error_mark_node;
5179       const char *non_constant_p = NULL;
5180
5181       /* Consume the operator token.  */
5182       token = cp_lexer_consume_token (parser->lexer);
5183       /* Parse the cast-expression.  */
5184       cast_expression
5185         = cp_parser_cast_expression (parser,
5186                                      unary_operator == ADDR_EXPR,
5187                                      /*cast_p=*/false);
5188       /* Now, build an appropriate representation.  */
5189       switch (unary_operator)
5190         {
5191         case INDIRECT_REF:
5192           non_constant_p = "`*'";
5193           expression = build_x_indirect_ref (cast_expression, "unary *");
5194           break;
5195
5196         case ADDR_EXPR:
5197           non_constant_p = "`&'";
5198           /* Fall through.  */
5199         case BIT_NOT_EXPR:
5200           expression = build_x_unary_op (unary_operator, cast_expression);
5201           break;
5202
5203         case PREINCREMENT_EXPR:
5204         case PREDECREMENT_EXPR:
5205           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5206                             ? "`++'" : "`--'");
5207           /* Fall through.  */
5208         case UNARY_PLUS_EXPR:
5209         case NEGATE_EXPR:
5210         case TRUTH_NOT_EXPR:
5211           expression = finish_unary_op_expr (unary_operator, cast_expression);
5212           break;
5213
5214         default:
5215           gcc_unreachable ();
5216         }
5217
5218       if (non_constant_p
5219           && cp_parser_non_integral_constant_expression (parser,
5220                                                          non_constant_p))
5221         expression = error_mark_node;
5222
5223       return expression;
5224     }
5225
5226   return cp_parser_postfix_expression (parser, address_p, cast_p);
5227 }
5228
5229 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5230    unary-operator, the corresponding tree code is returned.  */
5231
5232 static enum tree_code
5233 cp_parser_unary_operator (cp_token* token)
5234 {
5235   switch (token->type)
5236     {
5237     case CPP_MULT:
5238       return INDIRECT_REF;
5239
5240     case CPP_AND:
5241       return ADDR_EXPR;
5242
5243     case CPP_PLUS:
5244       return UNARY_PLUS_EXPR;
5245
5246     case CPP_MINUS:
5247       return NEGATE_EXPR;
5248
5249     case CPP_NOT:
5250       return TRUTH_NOT_EXPR;
5251
5252     case CPP_COMPL:
5253       return BIT_NOT_EXPR;
5254
5255     default:
5256       return ERROR_MARK;
5257     }
5258 }
5259
5260 /* Parse a new-expression.
5261
5262    new-expression:
5263      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5264      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5265
5266    Returns a representation of the expression.  */
5267
5268 static tree
5269 cp_parser_new_expression (cp_parser* parser)
5270 {
5271   bool global_scope_p;
5272   tree placement;
5273   tree type;
5274   tree initializer;
5275   tree nelts;
5276
5277   /* Look for the optional `::' operator.  */
5278   global_scope_p
5279     = (cp_parser_global_scope_opt (parser,
5280                                    /*current_scope_valid_p=*/false)
5281        != NULL_TREE);
5282   /* Look for the `new' operator.  */
5283   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5284   /* There's no easy way to tell a new-placement from the
5285      `( type-id )' construct.  */
5286   cp_parser_parse_tentatively (parser);
5287   /* Look for a new-placement.  */
5288   placement = cp_parser_new_placement (parser);
5289   /* If that didn't work out, there's no new-placement.  */
5290   if (!cp_parser_parse_definitely (parser))
5291     placement = NULL_TREE;
5292
5293   /* If the next token is a `(', then we have a parenthesized
5294      type-id.  */
5295   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5296     {
5297       /* Consume the `('.  */
5298       cp_lexer_consume_token (parser->lexer);
5299       /* Parse the type-id.  */
5300       type = cp_parser_type_id (parser);
5301       /* Look for the closing `)'.  */
5302       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5303       /* There should not be a direct-new-declarator in this production,
5304          but GCC used to allowed this, so we check and emit a sensible error
5305          message for this case.  */
5306       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5307         {
5308           error ("array bound forbidden after parenthesized type-id");
5309           inform ("try removing the parentheses around the type-id");
5310           cp_parser_direct_new_declarator (parser);
5311         }
5312       nelts = NULL_TREE;
5313     }
5314   /* Otherwise, there must be a new-type-id.  */
5315   else
5316     type = cp_parser_new_type_id (parser, &nelts);
5317
5318   /* If the next token is a `(', then we have a new-initializer.  */
5319   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5320     initializer = cp_parser_new_initializer (parser);
5321   else
5322     initializer = NULL_TREE;
5323
5324   /* A new-expression may not appear in an integral constant
5325      expression.  */
5326   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5327     return error_mark_node;
5328
5329   /* Create a representation of the new-expression.  */
5330   return build_new (placement, type, nelts, initializer, global_scope_p);
5331 }
5332
5333 /* Parse a new-placement.
5334
5335    new-placement:
5336      ( expression-list )
5337
5338    Returns the same representation as for an expression-list.  */
5339
5340 static tree
5341 cp_parser_new_placement (cp_parser* parser)
5342 {
5343   tree expression_list;
5344
5345   /* Parse the expression-list.  */
5346   expression_list = (cp_parser_parenthesized_expression_list
5347                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5348                       /*non_constant_p=*/NULL));
5349
5350   return expression_list;
5351 }
5352
5353 /* Parse a new-type-id.
5354
5355    new-type-id:
5356      type-specifier-seq new-declarator [opt]
5357
5358    Returns the TYPE allocated.  If the new-type-id indicates an array
5359    type, *NELTS is set to the number of elements in the last array
5360    bound; the TYPE will not include the last array bound.  */
5361
5362 static tree
5363 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5364 {
5365   cp_decl_specifier_seq type_specifier_seq;
5366   cp_declarator *new_declarator;
5367   cp_declarator *declarator;
5368   cp_declarator *outer_declarator;
5369   const char *saved_message;
5370   tree type;
5371
5372   /* The type-specifier sequence must not contain type definitions.
5373      (It cannot contain declarations of new types either, but if they
5374      are not definitions we will catch that because they are not
5375      complete.)  */
5376   saved_message = parser->type_definition_forbidden_message;
5377   parser->type_definition_forbidden_message
5378     = "types may not be defined in a new-type-id";
5379   /* Parse the type-specifier-seq.  */
5380   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5381                                 &type_specifier_seq);
5382   /* Restore the old message.  */
5383   parser->type_definition_forbidden_message = saved_message;
5384   /* Parse the new-declarator.  */
5385   new_declarator = cp_parser_new_declarator_opt (parser);
5386
5387   /* Determine the number of elements in the last array dimension, if
5388      any.  */
5389   *nelts = NULL_TREE;
5390   /* Skip down to the last array dimension.  */
5391   declarator = new_declarator;
5392   outer_declarator = NULL;
5393   while (declarator && (declarator->kind == cdk_pointer
5394                         || declarator->kind == cdk_ptrmem))
5395     {
5396       outer_declarator = declarator;
5397       declarator = declarator->declarator;
5398     }
5399   while (declarator
5400          && declarator->kind == cdk_array
5401          && declarator->declarator
5402          && declarator->declarator->kind == cdk_array)
5403     {
5404       outer_declarator = declarator;
5405       declarator = declarator->declarator;
5406     }
5407
5408   if (declarator && declarator->kind == cdk_array)
5409     {
5410       *nelts = declarator->u.array.bounds;
5411       if (*nelts == error_mark_node)
5412         *nelts = integer_one_node;
5413
5414       if (outer_declarator)
5415         outer_declarator->declarator = declarator->declarator;
5416       else
5417         new_declarator = NULL;
5418     }
5419
5420   type = groktypename (&type_specifier_seq, new_declarator);
5421   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5422     {
5423       *nelts = array_type_nelts_top (type);
5424       type = TREE_TYPE (type);
5425     }
5426   return type;
5427 }
5428
5429 /* Parse an (optional) new-declarator.
5430
5431    new-declarator:
5432      ptr-operator new-declarator [opt]
5433      direct-new-declarator
5434
5435    Returns the declarator.  */
5436
5437 static cp_declarator *
5438 cp_parser_new_declarator_opt (cp_parser* parser)
5439 {
5440   enum tree_code code;
5441   tree type;
5442   cp_cv_quals cv_quals;
5443
5444   /* We don't know if there's a ptr-operator next, or not.  */
5445   cp_parser_parse_tentatively (parser);
5446   /* Look for a ptr-operator.  */
5447   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5448   /* If that worked, look for more new-declarators.  */
5449   if (cp_parser_parse_definitely (parser))
5450     {
5451       cp_declarator *declarator;
5452
5453       /* Parse another optional declarator.  */
5454       declarator = cp_parser_new_declarator_opt (parser);
5455
5456       /* Create the representation of the declarator.  */
5457       if (type)
5458         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5459       else if (code == INDIRECT_REF)
5460         declarator = make_pointer_declarator (cv_quals, declarator);
5461       else
5462         declarator = make_reference_declarator (cv_quals, declarator);
5463
5464       return declarator;
5465     }
5466
5467   /* If the next token is a `[', there is a direct-new-declarator.  */
5468   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5469     return cp_parser_direct_new_declarator (parser);
5470
5471   return NULL;
5472 }
5473
5474 /* Parse a direct-new-declarator.
5475
5476    direct-new-declarator:
5477      [ expression ]
5478      direct-new-declarator [constant-expression]
5479
5480    */
5481
5482 static cp_declarator *
5483 cp_parser_direct_new_declarator (cp_parser* parser)
5484 {
5485   cp_declarator *declarator = NULL;
5486
5487   while (true)
5488     {
5489       tree expression;
5490
5491       /* Look for the opening `['.  */
5492       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5493       /* The first expression is not required to be constant.  */
5494       if (!declarator)
5495         {
5496           expression = cp_parser_expression (parser, /*cast_p=*/false);
5497           /* The standard requires that the expression have integral
5498              type.  DR 74 adds enumeration types.  We believe that the
5499              real intent is that these expressions be handled like the
5500              expression in a `switch' condition, which also allows
5501              classes with a single conversion to integral or
5502              enumeration type.  */
5503           if (!processing_template_decl)
5504             {
5505               expression
5506                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5507                                               expression,
5508                                               /*complain=*/true);
5509               if (!expression)
5510                 {
5511                   error ("expression in new-declarator must have integral "
5512                          "or enumeration type");
5513                   expression = error_mark_node;
5514                 }
5515             }
5516         }
5517       /* But all the other expressions must be.  */
5518       else
5519         expression
5520           = cp_parser_constant_expression (parser,
5521                                            /*allow_non_constant=*/false,
5522                                            NULL);
5523       /* Look for the closing `]'.  */
5524       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5525
5526       /* Add this bound to the declarator.  */
5527       declarator = make_array_declarator (declarator, expression);
5528
5529       /* If the next token is not a `[', then there are no more
5530          bounds.  */
5531       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5532         break;
5533     }
5534
5535   return declarator;
5536 }
5537
5538 /* Parse a new-initializer.
5539
5540    new-initializer:
5541      ( expression-list [opt] )
5542
5543    Returns a representation of the expression-list.  If there is no
5544    expression-list, VOID_ZERO_NODE is returned.  */
5545
5546 static tree
5547 cp_parser_new_initializer (cp_parser* parser)
5548 {
5549   tree expression_list;
5550
5551   expression_list = (cp_parser_parenthesized_expression_list
5552                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5553                       /*non_constant_p=*/NULL));
5554   if (!expression_list)
5555     expression_list = void_zero_node;
5556
5557   return expression_list;
5558 }
5559
5560 /* Parse a delete-expression.
5561
5562    delete-expression:
5563      :: [opt] delete cast-expression
5564      :: [opt] delete [ ] cast-expression
5565
5566    Returns a representation of the expression.  */
5567
5568 static tree
5569 cp_parser_delete_expression (cp_parser* parser)
5570 {
5571   bool global_scope_p;
5572   bool array_p;
5573   tree expression;
5574
5575   /* Look for the optional `::' operator.  */
5576   global_scope_p
5577     = (cp_parser_global_scope_opt (parser,
5578                                    /*current_scope_valid_p=*/false)
5579        != NULL_TREE);
5580   /* Look for the `delete' keyword.  */
5581   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5582   /* See if the array syntax is in use.  */
5583   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5584     {
5585       /* Consume the `[' token.  */
5586       cp_lexer_consume_token (parser->lexer);
5587       /* Look for the `]' token.  */
5588       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5589       /* Remember that this is the `[]' construct.  */
5590       array_p = true;
5591     }
5592   else
5593     array_p = false;
5594
5595   /* Parse the cast-expression.  */
5596   expression = cp_parser_simple_cast_expression (parser);
5597
5598   /* A delete-expression may not appear in an integral constant
5599      expression.  */
5600   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5601     return error_mark_node;
5602
5603   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5604 }
5605
5606 /* Parse a cast-expression.
5607
5608    cast-expression:
5609      unary-expression
5610      ( type-id ) cast-expression
5611
5612    ADDRESS_P is true iff the unary-expression is appearing as the
5613    operand of the `&' operator.   CAST_P is true if this expression is
5614    the target of a cast.
5615
5616    Returns a representation of the expression.  */
5617
5618 static tree
5619 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5620 {
5621   /* If it's a `(', then we might be looking at a cast.  */
5622   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5623     {
5624       tree type = NULL_TREE;
5625       tree expr = NULL_TREE;
5626       bool compound_literal_p;
5627       const char *saved_message;
5628
5629       /* There's no way to know yet whether or not this is a cast.
5630          For example, `(int (3))' is a unary-expression, while `(int)
5631          3' is a cast.  So, we resort to parsing tentatively.  */
5632       cp_parser_parse_tentatively (parser);
5633       /* Types may not be defined in a cast.  */
5634       saved_message = parser->type_definition_forbidden_message;
5635       parser->type_definition_forbidden_message
5636         = "types may not be defined in casts";
5637       /* Consume the `('.  */
5638       cp_lexer_consume_token (parser->lexer);
5639       /* A very tricky bit is that `(struct S) { 3 }' is a
5640          compound-literal (which we permit in C++ as an extension).
5641          But, that construct is not a cast-expression -- it is a
5642          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5643          is legal; if the compound-literal were a cast-expression,
5644          you'd need an extra set of parentheses.)  But, if we parse
5645          the type-id, and it happens to be a class-specifier, then we
5646          will commit to the parse at that point, because we cannot
5647          undo the action that is done when creating a new class.  So,
5648          then we cannot back up and do a postfix-expression.
5649
5650          Therefore, we scan ahead to the closing `)', and check to see
5651          if the token after the `)' is a `{'.  If so, we are not
5652          looking at a cast-expression.
5653
5654          Save tokens so that we can put them back.  */
5655       cp_lexer_save_tokens (parser->lexer);
5656       /* Skip tokens until the next token is a closing parenthesis.
5657          If we find the closing `)', and the next token is a `{', then
5658          we are looking at a compound-literal.  */
5659       compound_literal_p
5660         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5661                                                   /*consume_paren=*/true)
5662            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5663       /* Roll back the tokens we skipped.  */
5664       cp_lexer_rollback_tokens (parser->lexer);
5665       /* If we were looking at a compound-literal, simulate an error
5666          so that the call to cp_parser_parse_definitely below will
5667          fail.  */
5668       if (compound_literal_p)
5669         cp_parser_simulate_error (parser);
5670       else
5671         {
5672           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5673           parser->in_type_id_in_expr_p = true;
5674           /* Look for the type-id.  */
5675           type = cp_parser_type_id (parser);
5676           /* Look for the closing `)'.  */
5677           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5678           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5679         }
5680
5681       /* Restore the saved message.  */
5682       parser->type_definition_forbidden_message = saved_message;
5683
5684       /* If ok so far, parse the dependent expression. We cannot be
5685          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5686          ctor of T, but looks like a cast to function returning T
5687          without a dependent expression.  */
5688       if (!cp_parser_error_occurred (parser))
5689         expr = cp_parser_cast_expression (parser,
5690                                           /*address_p=*/false,
5691                                           /*cast_p=*/true);
5692
5693       if (cp_parser_parse_definitely (parser))
5694         {
5695           /* Warn about old-style casts, if so requested.  */
5696           if (warn_old_style_cast
5697               && !in_system_header
5698               && !VOID_TYPE_P (type)
5699               && current_lang_name != lang_name_c)
5700             warning (OPT_Wold_style_cast, "use of old-style cast");
5701
5702           /* Only type conversions to integral or enumeration types
5703              can be used in constant-expressions.  */
5704           if (!cast_valid_in_integral_constant_expression_p (type)
5705               && (cp_parser_non_integral_constant_expression
5706                   (parser,
5707                    "a cast to a type other than an integral or "
5708                    "enumeration type")))
5709             return error_mark_node;
5710
5711           /* Perform the cast.  */
5712           expr = build_c_cast (type, expr);
5713           return expr;
5714         }
5715     }
5716
5717   /* If we get here, then it's not a cast, so it must be a
5718      unary-expression.  */
5719   return cp_parser_unary_expression (parser, address_p, cast_p);
5720 }
5721
5722 /* Parse a binary expression of the general form:
5723
5724    pm-expression:
5725      cast-expression
5726      pm-expression .* cast-expression
5727      pm-expression ->* cast-expression
5728
5729    multiplicative-expression:
5730      pm-expression
5731      multiplicative-expression * pm-expression
5732      multiplicative-expression / pm-expression
5733      multiplicative-expression % pm-expression
5734
5735    additive-expression:
5736      multiplicative-expression
5737      additive-expression + multiplicative-expression
5738      additive-expression - multiplicative-expression
5739
5740    shift-expression:
5741      additive-expression
5742      shift-expression << additive-expression
5743      shift-expression >> additive-expression
5744
5745    relational-expression:
5746      shift-expression
5747      relational-expression < shift-expression
5748      relational-expression > shift-expression
5749      relational-expression <= shift-expression
5750      relational-expression >= shift-expression
5751
5752   GNU Extension:
5753
5754    relational-expression:
5755      relational-expression <? shift-expression
5756      relational-expression >? shift-expression
5757
5758    equality-expression:
5759      relational-expression
5760      equality-expression == relational-expression
5761      equality-expression != relational-expression
5762
5763    and-expression:
5764      equality-expression
5765      and-expression & equality-expression
5766
5767    exclusive-or-expression:
5768      and-expression
5769      exclusive-or-expression ^ and-expression
5770
5771    inclusive-or-expression:
5772      exclusive-or-expression
5773      inclusive-or-expression | exclusive-or-expression
5774
5775    logical-and-expression:
5776      inclusive-or-expression
5777      logical-and-expression && inclusive-or-expression
5778
5779    logical-or-expression:
5780      logical-and-expression
5781      logical-or-expression || logical-and-expression
5782
5783    All these are implemented with a single function like:
5784
5785    binary-expression:
5786      simple-cast-expression
5787      binary-expression <token> binary-expression
5788
5789    CAST_P is true if this expression is the target of a cast.
5790
5791    The binops_by_token map is used to get the tree codes for each <token> type.
5792    binary-expressions are associated according to a precedence table.  */
5793
5794 #define TOKEN_PRECEDENCE(token) \
5795   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5796    ? PREC_NOT_OPERATOR \
5797    : binops_by_token[token->type].prec)
5798
5799 static tree
5800 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5801 {
5802   cp_parser_expression_stack stack;
5803   cp_parser_expression_stack_entry *sp = &stack[0];
5804   tree lhs, rhs;
5805   cp_token *token;
5806   enum tree_code tree_type, lhs_type, rhs_type;
5807   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5808   bool overloaded_p;
5809
5810   /* Parse the first expression.  */
5811   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5812   lhs_type = ERROR_MARK;
5813
5814   for (;;)
5815     {
5816       /* Get an operator token.  */
5817       token = cp_lexer_peek_token (parser->lexer);
5818
5819       new_prec = TOKEN_PRECEDENCE (token);
5820
5821       /* Popping an entry off the stack means we completed a subexpression:
5822          - either we found a token which is not an operator (`>' where it is not
5823            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5824            will happen repeatedly;
5825          - or, we found an operator which has lower priority.  This is the case
5826            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5827            parsing `3 * 4'.  */
5828       if (new_prec <= prec)
5829         {
5830           if (sp == stack)
5831             break;
5832           else
5833             goto pop;
5834         }
5835
5836      get_rhs:
5837       tree_type = binops_by_token[token->type].tree_type;
5838
5839       /* We used the operator token.  */
5840       cp_lexer_consume_token (parser->lexer);
5841
5842       /* Extract another operand.  It may be the RHS of this expression
5843          or the LHS of a new, higher priority expression.  */
5844       rhs = cp_parser_simple_cast_expression (parser);
5845       rhs_type = ERROR_MARK;
5846
5847       /* Get another operator token.  Look up its precedence to avoid
5848          building a useless (immediately popped) stack entry for common
5849          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5850       token = cp_lexer_peek_token (parser->lexer);
5851       lookahead_prec = TOKEN_PRECEDENCE (token);
5852       if (lookahead_prec > new_prec)
5853         {
5854           /* ... and prepare to parse the RHS of the new, higher priority
5855              expression.  Since precedence levels on the stack are
5856              monotonically increasing, we do not have to care about
5857              stack overflows.  */
5858           sp->prec = prec;
5859           sp->tree_type = tree_type;
5860           sp->lhs = lhs;
5861           sp->lhs_type = lhs_type;
5862           sp++;
5863           lhs = rhs;
5864           lhs_type = rhs_type;
5865           prec = new_prec;
5866           new_prec = lookahead_prec;
5867           goto get_rhs;
5868
5869          pop:
5870           /* If the stack is not empty, we have parsed into LHS the right side
5871              (`4' in the example above) of an expression we had suspended.
5872              We can use the information on the stack to recover the LHS (`3')
5873              from the stack together with the tree code (`MULT_EXPR'), and
5874              the precedence of the higher level subexpression
5875              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5876              which will be used to actually build the additive expression.  */
5877           --sp;
5878           prec = sp->prec;
5879           tree_type = sp->tree_type;
5880           rhs = lhs;
5881           rhs_type = lhs_type;
5882           lhs = sp->lhs;
5883           lhs_type = sp->lhs_type;
5884         }
5885
5886       overloaded_p = false;
5887       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5888                                &overloaded_p);
5889       lhs_type = tree_type;
5890
5891       /* If the binary operator required the use of an overloaded operator,
5892          then this expression cannot be an integral constant-expression.
5893          An overloaded operator can be used even if both operands are
5894          otherwise permissible in an integral constant-expression if at
5895          least one of the operands is of enumeration type.  */
5896
5897       if (overloaded_p
5898           && (cp_parser_non_integral_constant_expression
5899               (parser, "calls to overloaded operators")))
5900         return error_mark_node;
5901     }
5902
5903   return lhs;
5904 }
5905
5906
5907 /* Parse the `? expression : assignment-expression' part of a
5908    conditional-expression.  The LOGICAL_OR_EXPR is the
5909    logical-or-expression that started the conditional-expression.
5910    Returns a representation of the entire conditional-expression.
5911
5912    This routine is used by cp_parser_assignment_expression.
5913
5914      ? expression : assignment-expression
5915
5916    GNU Extensions:
5917
5918      ? : assignment-expression */
5919
5920 static tree
5921 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5922 {
5923   tree expr;
5924   tree assignment_expr;
5925
5926   /* Consume the `?' token.  */
5927   cp_lexer_consume_token (parser->lexer);
5928   if (cp_parser_allow_gnu_extensions_p (parser)
5929       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5930     /* Implicit true clause.  */
5931     expr = NULL_TREE;
5932   else
5933     /* Parse the expression.  */
5934     expr = cp_parser_expression (parser, /*cast_p=*/false);
5935
5936   /* The next token should be a `:'.  */
5937   cp_parser_require (parser, CPP_COLON, "`:'");
5938   /* Parse the assignment-expression.  */
5939   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5940
5941   /* Build the conditional-expression.  */
5942   return build_x_conditional_expr (logical_or_expr,
5943                                    expr,
5944                                    assignment_expr);
5945 }
5946
5947 /* Parse an assignment-expression.
5948
5949    assignment-expression:
5950      conditional-expression
5951      logical-or-expression assignment-operator assignment_expression
5952      throw-expression
5953
5954    CAST_P is true if this expression is the target of a cast.
5955
5956    Returns a representation for the expression.  */
5957
5958 static tree
5959 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5960 {
5961   tree expr;
5962
5963   /* If the next token is the `throw' keyword, then we're looking at
5964      a throw-expression.  */
5965   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5966     expr = cp_parser_throw_expression (parser);
5967   /* Otherwise, it must be that we are looking at a
5968      logical-or-expression.  */
5969   else
5970     {
5971       /* Parse the binary expressions (logical-or-expression).  */
5972       expr = cp_parser_binary_expression (parser, cast_p);
5973       /* If the next token is a `?' then we're actually looking at a
5974          conditional-expression.  */
5975       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5976         return cp_parser_question_colon_clause (parser, expr);
5977       else
5978         {
5979           enum tree_code assignment_operator;
5980
5981           /* If it's an assignment-operator, we're using the second
5982              production.  */
5983           assignment_operator
5984             = cp_parser_assignment_operator_opt (parser);
5985           if (assignment_operator != ERROR_MARK)
5986             {
5987               tree rhs;
5988
5989               /* Parse the right-hand side of the assignment.  */
5990               rhs = cp_parser_assignment_expression (parser, cast_p);
5991               /* An assignment may not appear in a
5992                  constant-expression.  */
5993               if (cp_parser_non_integral_constant_expression (parser,
5994                                                               "an assignment"))
5995                 return error_mark_node;
5996               /* Build the assignment expression.  */
5997               expr = build_x_modify_expr (expr,
5998                                           assignment_operator,
5999                                           rhs);
6000             }
6001         }
6002     }
6003
6004   return expr;
6005 }
6006
6007 /* Parse an (optional) assignment-operator.
6008
6009    assignment-operator: one of
6010      = *= /= %= += -= >>= <<= &= ^= |=
6011
6012    GNU Extension:
6013
6014    assignment-operator: one of
6015      <?= >?=
6016
6017    If the next token is an assignment operator, the corresponding tree
6018    code is returned, and the token is consumed.  For example, for
6019    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6020    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6021    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6022    operator, ERROR_MARK is returned.  */
6023
6024 static enum tree_code
6025 cp_parser_assignment_operator_opt (cp_parser* parser)
6026 {
6027   enum tree_code op;
6028   cp_token *token;
6029
6030   /* Peek at the next toen.  */
6031   token = cp_lexer_peek_token (parser->lexer);
6032
6033   switch (token->type)
6034     {
6035     case CPP_EQ:
6036       op = NOP_EXPR;
6037       break;
6038
6039     case CPP_MULT_EQ:
6040       op = MULT_EXPR;
6041       break;
6042
6043     case CPP_DIV_EQ:
6044       op = TRUNC_DIV_EXPR;
6045       break;
6046
6047     case CPP_MOD_EQ:
6048       op = TRUNC_MOD_EXPR;
6049       break;
6050
6051     case CPP_PLUS_EQ:
6052       op = PLUS_EXPR;
6053       break;
6054
6055     case CPP_MINUS_EQ:
6056       op = MINUS_EXPR;
6057       break;
6058
6059     case CPP_RSHIFT_EQ:
6060       op = RSHIFT_EXPR;
6061       break;
6062
6063     case CPP_LSHIFT_EQ:
6064       op = LSHIFT_EXPR;
6065       break;
6066
6067     case CPP_AND_EQ:
6068       op = BIT_AND_EXPR;
6069       break;
6070
6071     case CPP_XOR_EQ:
6072       op = BIT_XOR_EXPR;
6073       break;
6074
6075     case CPP_OR_EQ:
6076       op = BIT_IOR_EXPR;
6077       break;
6078
6079     default:
6080       /* Nothing else is an assignment operator.  */
6081       op = ERROR_MARK;
6082     }
6083
6084   /* If it was an assignment operator, consume it.  */
6085   if (op != ERROR_MARK)
6086     cp_lexer_consume_token (parser->lexer);
6087
6088   return op;
6089 }
6090
6091 /* Parse an expression.
6092
6093    expression:
6094      assignment-expression
6095      expression , assignment-expression
6096
6097    CAST_P is true if this expression is the target of a cast.
6098
6099    Returns a representation of the expression.  */
6100
6101 static tree
6102 cp_parser_expression (cp_parser* parser, bool cast_p)
6103 {
6104   tree expression = NULL_TREE;
6105
6106   while (true)
6107     {
6108       tree assignment_expression;
6109
6110       /* Parse the next assignment-expression.  */
6111       assignment_expression
6112         = cp_parser_assignment_expression (parser, cast_p);
6113       /* If this is the first assignment-expression, we can just
6114          save it away.  */
6115       if (!expression)
6116         expression = assignment_expression;
6117       else
6118         expression = build_x_compound_expr (expression,
6119                                             assignment_expression);
6120       /* If the next token is not a comma, then we are done with the
6121          expression.  */
6122       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6123         break;
6124       /* Consume the `,'.  */
6125       cp_lexer_consume_token (parser->lexer);
6126       /* A comma operator cannot appear in a constant-expression.  */
6127       if (cp_parser_non_integral_constant_expression (parser,
6128                                                       "a comma operator"))
6129         expression = error_mark_node;
6130     }
6131
6132   return expression;
6133 }
6134
6135 /* Parse a constant-expression.
6136
6137    constant-expression:
6138      conditional-expression
6139
6140   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6141   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6142   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6143   is false, NON_CONSTANT_P should be NULL.  */
6144
6145 static tree
6146 cp_parser_constant_expression (cp_parser* parser,
6147                                bool allow_non_constant_p,
6148                                bool *non_constant_p)
6149 {
6150   bool saved_integral_constant_expression_p;
6151   bool saved_allow_non_integral_constant_expression_p;
6152   bool saved_non_integral_constant_expression_p;
6153   tree expression;
6154
6155   /* It might seem that we could simply parse the
6156      conditional-expression, and then check to see if it were
6157      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6158      one that the compiler can figure out is constant, possibly after
6159      doing some simplifications or optimizations.  The standard has a
6160      precise definition of constant-expression, and we must honor
6161      that, even though it is somewhat more restrictive.
6162
6163      For example:
6164
6165        int i[(2, 3)];
6166
6167      is not a legal declaration, because `(2, 3)' is not a
6168      constant-expression.  The `,' operator is forbidden in a
6169      constant-expression.  However, GCC's constant-folding machinery
6170      will fold this operation to an INTEGER_CST for `3'.  */
6171
6172   /* Save the old settings.  */
6173   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6174   saved_allow_non_integral_constant_expression_p
6175     = parser->allow_non_integral_constant_expression_p;
6176   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6177   /* We are now parsing a constant-expression.  */
6178   parser->integral_constant_expression_p = true;
6179   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6180   parser->non_integral_constant_expression_p = false;
6181   /* Although the grammar says "conditional-expression", we parse an
6182      "assignment-expression", which also permits "throw-expression"
6183      and the use of assignment operators.  In the case that
6184      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6185      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6186      actually essential that we look for an assignment-expression.
6187      For example, cp_parser_initializer_clauses uses this function to
6188      determine whether a particular assignment-expression is in fact
6189      constant.  */
6190   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6191   /* Restore the old settings.  */
6192   parser->integral_constant_expression_p
6193     = saved_integral_constant_expression_p;
6194   parser->allow_non_integral_constant_expression_p
6195     = saved_allow_non_integral_constant_expression_p;
6196   if (allow_non_constant_p)
6197     *non_constant_p = parser->non_integral_constant_expression_p;
6198   else if (parser->non_integral_constant_expression_p)
6199     expression = error_mark_node;
6200   parser->non_integral_constant_expression_p
6201     = saved_non_integral_constant_expression_p;
6202
6203   return expression;
6204 }
6205
6206 /* Parse __builtin_offsetof.
6207
6208    offsetof-expression:
6209      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6210
6211    offsetof-member-designator:
6212      id-expression
6213      | offsetof-member-designator "." id-expression
6214      | offsetof-member-designator "[" expression "]"  */
6215
6216 static tree
6217 cp_parser_builtin_offsetof (cp_parser *parser)
6218 {
6219   int save_ice_p, save_non_ice_p;
6220   tree type, expr;
6221   cp_id_kind dummy;
6222
6223   /* We're about to accept non-integral-constant things, but will
6224      definitely yield an integral constant expression.  Save and
6225      restore these values around our local parsing.  */
6226   save_ice_p = parser->integral_constant_expression_p;
6227   save_non_ice_p = parser->non_integral_constant_expression_p;
6228
6229   /* Consume the "__builtin_offsetof" token.  */
6230   cp_lexer_consume_token (parser->lexer);
6231   /* Consume the opening `('.  */
6232   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6233   /* Parse the type-id.  */
6234   type = cp_parser_type_id (parser);
6235   /* Look for the `,'.  */
6236   cp_parser_require (parser, CPP_COMMA, "`,'");
6237
6238   /* Build the (type *)null that begins the traditional offsetof macro.  */
6239   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6240
6241   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6242   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6243                                                  true, &dummy);
6244   while (true)
6245     {
6246       cp_token *token = cp_lexer_peek_token (parser->lexer);
6247       switch (token->type)
6248         {
6249         case CPP_OPEN_SQUARE:
6250           /* offsetof-member-designator "[" expression "]" */
6251           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6252           break;
6253
6254         case CPP_DOT:
6255           /* offsetof-member-designator "." identifier */
6256           cp_lexer_consume_token (parser->lexer);
6257           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6258                                                          true, &dummy);
6259           break;
6260
6261         case CPP_CLOSE_PAREN:
6262           /* Consume the ")" token.  */
6263           cp_lexer_consume_token (parser->lexer);
6264           goto success;
6265
6266         default:
6267           /* Error.  We know the following require will fail, but
6268              that gives the proper error message.  */
6269           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6270           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6271           expr = error_mark_node;
6272           goto failure;
6273         }
6274     }
6275
6276  success:
6277   /* If we're processing a template, we can't finish the semantics yet.
6278      Otherwise we can fold the entire expression now.  */
6279   if (processing_template_decl)
6280     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6281   else
6282     expr = finish_offsetof (expr);
6283
6284  failure:
6285   parser->integral_constant_expression_p = save_ice_p;
6286   parser->non_integral_constant_expression_p = save_non_ice_p;
6287
6288   return expr;
6289 }
6290
6291 /* Statements [gram.stmt.stmt]  */
6292
6293 /* Parse a statement.
6294
6295    statement:
6296      labeled-statement
6297      expression-statement
6298      compound-statement
6299      selection-statement
6300      iteration-statement
6301      jump-statement
6302      declaration-statement
6303      try-block
6304
6305   IN_COMPOUND is true when the statement is nested inside a
6306   cp_parser_compound_statement; this matters for certain pragmas.
6307
6308   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6309   is a (possibly labeled) if statement which is not enclosed in braces
6310   and has an else clause.  This is used to implement -Wparentheses.  */
6311
6312 static void
6313 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6314                      bool in_compound, bool *if_p)
6315 {
6316   tree statement;
6317   cp_token *token;
6318   location_t statement_location;
6319
6320  restart:
6321   if (if_p != NULL)
6322     *if_p = false;
6323   /* There is no statement yet.  */
6324   statement = NULL_TREE;
6325   /* Peek at the next token.  */
6326   token = cp_lexer_peek_token (parser->lexer);
6327   /* Remember the location of the first token in the statement.  */
6328   statement_location = token->location;
6329   /* If this is a keyword, then that will often determine what kind of
6330      statement we have.  */
6331   if (token->type == CPP_KEYWORD)
6332     {
6333       enum rid keyword = token->keyword;
6334
6335       switch (keyword)
6336         {
6337         case RID_CASE:
6338         case RID_DEFAULT:
6339           /* Looks like a labeled-statement with a case label.
6340              Parse the label, and then use tail recursion to parse
6341              the statement.  */
6342           cp_parser_label_for_labeled_statement (parser);
6343           goto restart;
6344
6345         case RID_IF:
6346         case RID_SWITCH:
6347           statement = cp_parser_selection_statement (parser, if_p);
6348           break;
6349
6350         case RID_WHILE:
6351         case RID_DO:
6352         case RID_FOR:
6353           statement = cp_parser_iteration_statement (parser);
6354           break;
6355
6356         case RID_BREAK:
6357         case RID_CONTINUE:
6358         case RID_RETURN:
6359         case RID_GOTO:
6360           statement = cp_parser_jump_statement (parser);
6361           break;
6362
6363           /* Objective-C++ exception-handling constructs.  */
6364         case RID_AT_TRY:
6365         case RID_AT_CATCH:
6366         case RID_AT_FINALLY:
6367         case RID_AT_SYNCHRONIZED:
6368         case RID_AT_THROW:
6369           statement = cp_parser_objc_statement (parser);
6370           break;
6371
6372         case RID_TRY:
6373           statement = cp_parser_try_block (parser);
6374           break;
6375
6376         default:
6377           /* It might be a keyword like `int' that can start a
6378              declaration-statement.  */
6379           break;
6380         }
6381     }
6382   else if (token->type == CPP_NAME)
6383     {
6384       /* If the next token is a `:', then we are looking at a
6385          labeled-statement.  */
6386       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6387       if (token->type == CPP_COLON)
6388         {
6389           /* Looks like a labeled-statement with an ordinary label.
6390              Parse the label, and then use tail recursion to parse
6391              the statement.  */
6392           cp_parser_label_for_labeled_statement (parser);
6393           goto restart;
6394         }
6395     }
6396   /* Anything that starts with a `{' must be a compound-statement.  */
6397   else if (token->type == CPP_OPEN_BRACE)
6398     statement = cp_parser_compound_statement (parser, NULL, false);
6399   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6400      a statement all its own.  */
6401   else if (token->type == CPP_PRAGMA)
6402     {
6403       /* Only certain OpenMP pragmas are attached to statements, and thus
6404          are considered statements themselves.  All others are not.  In
6405          the context of a compound, accept the pragma as a "statement" and
6406          return so that we can check for a close brace.  Otherwise we
6407          require a real statement and must go back and read one.  */
6408       if (in_compound)
6409         cp_parser_pragma (parser, pragma_compound);
6410       else if (!cp_parser_pragma (parser, pragma_stmt))
6411         goto restart;
6412       return;
6413     }
6414   else if (token->type == CPP_EOF)
6415     {
6416       cp_parser_error (parser, "expected statement");
6417       return;
6418     }
6419
6420   /* Everything else must be a declaration-statement or an
6421      expression-statement.  Try for the declaration-statement
6422      first, unless we are looking at a `;', in which case we know that
6423      we have an expression-statement.  */
6424   if (!statement)
6425     {
6426       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6427         {
6428           cp_parser_parse_tentatively (parser);
6429           /* Try to parse the declaration-statement.  */
6430           cp_parser_declaration_statement (parser);
6431           /* If that worked, we're done.  */
6432           if (cp_parser_parse_definitely (parser))
6433             return;
6434         }
6435       /* Look for an expression-statement instead.  */
6436       statement = cp_parser_expression_statement (parser, in_statement_expr);
6437     }
6438
6439   /* Set the line number for the statement.  */
6440   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6441     SET_EXPR_LOCATION (statement, statement_location);
6442 }
6443
6444 /* Parse the label for a labeled-statement, i.e.
6445
6446    identifier :
6447    case constant-expression :
6448    default :
6449
6450    GNU Extension:
6451    case constant-expression ... constant-expression : statement
6452
6453    When a label is parsed without errors, the label is added to the
6454    parse tree by the finish_* functions, so this function doesn't
6455    have to return the label.  */
6456
6457 static void
6458 cp_parser_label_for_labeled_statement (cp_parser* parser)
6459 {
6460   cp_token *token;
6461
6462   /* The next token should be an identifier.  */
6463   token = cp_lexer_peek_token (parser->lexer);
6464   if (token->type != CPP_NAME
6465       && token->type != CPP_KEYWORD)
6466     {
6467       cp_parser_error (parser, "expected labeled-statement");
6468       return;
6469     }
6470
6471   switch (token->keyword)
6472     {
6473     case RID_CASE:
6474       {
6475         tree expr, expr_hi;
6476         cp_token *ellipsis;
6477
6478         /* Consume the `case' token.  */
6479         cp_lexer_consume_token (parser->lexer);
6480         /* Parse the constant-expression.  */
6481         expr = cp_parser_constant_expression (parser,
6482                                               /*allow_non_constant_p=*/false,
6483                                               NULL);
6484
6485         ellipsis = cp_lexer_peek_token (parser->lexer);
6486         if (ellipsis->type == CPP_ELLIPSIS)
6487           {
6488             /* Consume the `...' token.  */
6489             cp_lexer_consume_token (parser->lexer);
6490             expr_hi =
6491               cp_parser_constant_expression (parser,
6492                                              /*allow_non_constant_p=*/false,
6493                                              NULL);
6494             /* We don't need to emit warnings here, as the common code
6495                will do this for us.  */
6496           }
6497         else
6498           expr_hi = NULL_TREE;
6499
6500         if (parser->in_switch_statement_p)
6501           finish_case_label (expr, expr_hi);
6502         else
6503           error ("case label %qE not within a switch statement", expr);
6504       }
6505       break;
6506
6507     case RID_DEFAULT:
6508       /* Consume the `default' token.  */
6509       cp_lexer_consume_token (parser->lexer);
6510
6511       if (parser->in_switch_statement_p)
6512         finish_case_label (NULL_TREE, NULL_TREE);
6513       else
6514         error ("case label not within a switch statement");
6515       break;
6516
6517     default:
6518       /* Anything else must be an ordinary label.  */
6519       finish_label_stmt (cp_parser_identifier (parser));
6520       break;
6521     }
6522
6523   /* Require the `:' token.  */
6524   cp_parser_require (parser, CPP_COLON, "`:'");
6525 }
6526
6527 /* Parse an expression-statement.
6528
6529    expression-statement:
6530      expression [opt] ;
6531
6532    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6533    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6534    indicates whether this expression-statement is part of an
6535    expression statement.  */
6536
6537 static tree
6538 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6539 {
6540   tree statement = NULL_TREE;
6541
6542   /* If the next token is a ';', then there is no expression
6543      statement.  */
6544   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6545     statement = cp_parser_expression (parser, /*cast_p=*/false);
6546
6547   /* Consume the final `;'.  */
6548   cp_parser_consume_semicolon_at_end_of_statement (parser);
6549
6550   if (in_statement_expr
6551       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6552     /* This is the final expression statement of a statement
6553        expression.  */
6554     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6555   else if (statement)
6556     statement = finish_expr_stmt (statement);
6557   else
6558     finish_stmt ();
6559
6560   return statement;
6561 }
6562
6563 /* Parse a compound-statement.
6564
6565    compound-statement:
6566      { statement-seq [opt] }
6567
6568    Returns a tree representing the statement.  */
6569
6570 static tree
6571 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6572                               bool in_try)
6573 {
6574   tree compound_stmt;
6575
6576   /* Consume the `{'.  */
6577   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6578     return error_mark_node;
6579   /* Begin the compound-statement.  */
6580   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6581   /* Parse an (optional) statement-seq.  */
6582   cp_parser_statement_seq_opt (parser, in_statement_expr);
6583   /* Finish the compound-statement.  */
6584   finish_compound_stmt (compound_stmt);
6585   /* Consume the `}'.  */
6586   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6587
6588   return compound_stmt;
6589 }
6590
6591 /* Parse an (optional) statement-seq.
6592
6593    statement-seq:
6594      statement
6595      statement-seq [opt] statement  */
6596
6597 static void
6598 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6599 {
6600   /* Scan statements until there aren't any more.  */
6601   while (true)
6602     {
6603       cp_token *token = cp_lexer_peek_token (parser->lexer);
6604
6605       /* If we're looking at a `}', then we've run out of statements.  */
6606       if (token->type == CPP_CLOSE_BRACE
6607           || token->type == CPP_EOF
6608           || token->type == CPP_PRAGMA_EOL)
6609         break;
6610       
6611       /* If we are in a compound statement and find 'else' then
6612          something went wrong.  */
6613       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6614         {
6615           if (parser->in_statement & IN_IF_STMT) 
6616             break;
6617           else
6618             {
6619               token = cp_lexer_consume_token (parser->lexer);
6620               error ("%<else%> without a previous %<if%>");
6621             }
6622         }
6623
6624       /* Parse the statement.  */
6625       cp_parser_statement (parser, in_statement_expr, true, NULL);
6626     }
6627 }
6628
6629 /* Parse a selection-statement.
6630
6631    selection-statement:
6632      if ( condition ) statement
6633      if ( condition ) statement else statement
6634      switch ( condition ) statement
6635
6636    Returns the new IF_STMT or SWITCH_STMT.
6637
6638    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6639    is a (possibly labeled) if statement which is not enclosed in
6640    braces and has an else clause.  This is used to implement
6641    -Wparentheses.  */
6642
6643 static tree
6644 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6645 {
6646   cp_token *token;
6647   enum rid keyword;
6648
6649   if (if_p != NULL)
6650     *if_p = false;
6651
6652   /* Peek at the next token.  */
6653   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6654
6655   /* See what kind of keyword it is.  */
6656   keyword = token->keyword;
6657   switch (keyword)
6658     {
6659     case RID_IF:
6660     case RID_SWITCH:
6661       {
6662         tree statement;
6663         tree condition;
6664
6665         /* Look for the `('.  */
6666         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6667           {
6668             cp_parser_skip_to_end_of_statement (parser);
6669             return error_mark_node;
6670           }
6671
6672         /* Begin the selection-statement.  */
6673         if (keyword == RID_IF)
6674           statement = begin_if_stmt ();
6675         else
6676           statement = begin_switch_stmt ();
6677
6678         /* Parse the condition.  */
6679         condition = cp_parser_condition (parser);
6680         /* Look for the `)'.  */
6681         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6682           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6683                                                  /*consume_paren=*/true);
6684
6685         if (keyword == RID_IF)
6686           {
6687             bool nested_if;
6688             unsigned char in_statement;
6689
6690             /* Add the condition.  */
6691             finish_if_stmt_cond (condition, statement);
6692
6693             /* Parse the then-clause.  */
6694             in_statement = parser->in_statement;
6695             parser->in_statement |= IN_IF_STMT;
6696             cp_parser_implicitly_scoped_statement (parser, &nested_if);
6697             parser->in_statement = in_statement;
6698
6699             finish_then_clause (statement);
6700
6701             /* If the next token is `else', parse the else-clause.  */
6702             if (cp_lexer_next_token_is_keyword (parser->lexer,
6703                                                 RID_ELSE))
6704               {
6705                 /* Consume the `else' keyword.  */
6706                 cp_lexer_consume_token (parser->lexer);
6707                 begin_else_clause (statement);
6708                 /* Parse the else-clause.  */
6709                 cp_parser_implicitly_scoped_statement (parser, NULL);
6710                 finish_else_clause (statement);
6711
6712                 /* If we are currently parsing a then-clause, then
6713                    IF_P will not be NULL.  We set it to true to
6714                    indicate that this if statement has an else clause.
6715                    This may trigger the Wparentheses warning below
6716                    when we get back up to the parent if statement.  */
6717                 if (if_p != NULL)
6718                   *if_p = true;
6719               }
6720             else
6721               {
6722                 /* This if statement does not have an else clause.  If
6723                    NESTED_IF is true, then the then-clause is an if
6724                    statement which does have an else clause.  We warn
6725                    about the potential ambiguity.  */
6726                 if (nested_if)
6727                   warning (OPT_Wparentheses,
6728                            ("%Hsuggest explicit braces "
6729                             "to avoid ambiguous %<else%>"),
6730                            EXPR_LOCUS (statement));
6731               }
6732
6733             /* Now we're all done with the if-statement.  */
6734             finish_if_stmt (statement);
6735           }
6736         else
6737           {
6738             bool in_switch_statement_p;
6739             unsigned char in_statement;
6740
6741             /* Add the condition.  */
6742             finish_switch_cond (condition, statement);
6743
6744             /* Parse the body of the switch-statement.  */
6745             in_switch_statement_p = parser->in_switch_statement_p;
6746             in_statement = parser->in_statement;
6747             parser->in_switch_statement_p = true;
6748             parser->in_statement |= IN_SWITCH_STMT;
6749             cp_parser_implicitly_scoped_statement (parser, NULL);
6750             parser->in_switch_statement_p = in_switch_statement_p;
6751             parser->in_statement = in_statement;
6752
6753             /* Now we're all done with the switch-statement.  */
6754             finish_switch_stmt (statement);
6755           }
6756
6757         return statement;
6758       }
6759       break;
6760
6761     default:
6762       cp_parser_error (parser, "expected selection-statement");
6763       return error_mark_node;
6764     }
6765 }
6766
6767 /* Parse a condition.
6768
6769    condition:
6770      expression
6771      type-specifier-seq declarator = assignment-expression
6772
6773    GNU Extension:
6774
6775    condition:
6776      type-specifier-seq declarator asm-specification [opt]
6777        attributes [opt] = assignment-expression
6778
6779    Returns the expression that should be tested.  */
6780
6781 static tree
6782 cp_parser_condition (cp_parser* parser)
6783 {
6784   cp_decl_specifier_seq type_specifiers;
6785   const char *saved_message;
6786
6787   /* Try the declaration first.  */
6788   cp_parser_parse_tentatively (parser);
6789   /* New types are not allowed in the type-specifier-seq for a
6790      condition.  */
6791   saved_message = parser->type_definition_forbidden_message;
6792   parser->type_definition_forbidden_message
6793     = "types may not be defined in conditions";
6794   /* Parse the type-specifier-seq.  */
6795   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6796                                 &type_specifiers);
6797   /* Restore the saved message.  */
6798   parser->type_definition_forbidden_message = saved_message;
6799   /* If all is well, we might be looking at a declaration.  */
6800   if (!cp_parser_error_occurred (parser))
6801     {
6802       tree decl;
6803       tree asm_specification;
6804       tree attributes;
6805       cp_declarator *declarator;
6806       tree initializer = NULL_TREE;
6807
6808       /* Parse the declarator.  */
6809       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6810                                          /*ctor_dtor_or_conv_p=*/NULL,
6811                                          /*parenthesized_p=*/NULL,
6812                                          /*member_p=*/false);
6813       /* Parse the attributes.  */
6814       attributes = cp_parser_attributes_opt (parser);
6815       /* Parse the asm-specification.  */
6816       asm_specification = cp_parser_asm_specification_opt (parser);
6817       /* If the next token is not an `=', then we might still be
6818          looking at an expression.  For example:
6819
6820            if (A(a).x)
6821
6822          looks like a decl-specifier-seq and a declarator -- but then
6823          there is no `=', so this is an expression.  */
6824       cp_parser_require (parser, CPP_EQ, "`='");
6825       /* If we did see an `=', then we are looking at a declaration
6826          for sure.  */
6827       if (cp_parser_parse_definitely (parser))
6828         {
6829           tree pushed_scope;
6830           bool non_constant_p;
6831
6832           /* Create the declaration.  */
6833           decl = start_decl (declarator, &type_specifiers,
6834                              /*initialized_p=*/true,
6835                              attributes, /*prefix_attributes=*/NULL_TREE,
6836                              &pushed_scope);
6837           /* Parse the assignment-expression.  */
6838           initializer
6839             = cp_parser_constant_expression (parser,
6840                                              /*allow_non_constant_p=*/true,
6841                                              &non_constant_p);
6842           if (!non_constant_p)
6843             initializer = fold_non_dependent_expr (initializer);
6844
6845           /* Process the initializer.  */
6846           cp_finish_decl (decl,
6847                           initializer, !non_constant_p,
6848                           asm_specification,
6849                           LOOKUP_ONLYCONVERTING);
6850
6851           if (pushed_scope)
6852             pop_scope (pushed_scope);
6853
6854           return convert_from_reference (decl);
6855         }
6856     }
6857   /* If we didn't even get past the declarator successfully, we are
6858      definitely not looking at a declaration.  */
6859   else
6860     cp_parser_abort_tentative_parse (parser);
6861
6862   /* Otherwise, we are looking at an expression.  */
6863   return cp_parser_expression (parser, /*cast_p=*/false);
6864 }
6865
6866 /* Parse an iteration-statement.
6867
6868    iteration-statement:
6869      while ( condition ) statement
6870      do statement while ( expression ) ;
6871      for ( for-init-statement condition [opt] ; expression [opt] )
6872        statement
6873
6874    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6875
6876 static tree
6877 cp_parser_iteration_statement (cp_parser* parser)
6878 {
6879   cp_token *token;
6880   enum rid keyword;
6881   tree statement;
6882   unsigned char in_statement;
6883
6884   /* Peek at the next token.  */
6885   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6886   if (!token)
6887     return error_mark_node;
6888
6889   /* Remember whether or not we are already within an iteration
6890      statement.  */
6891   in_statement = parser->in_statement;
6892
6893   /* See what kind of keyword it is.  */
6894   keyword = token->keyword;
6895   switch (keyword)
6896     {
6897     case RID_WHILE:
6898       {
6899         tree condition;
6900
6901         /* Begin the while-statement.  */
6902         statement = begin_while_stmt ();
6903         /* Look for the `('.  */
6904         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6905         /* Parse the condition.  */
6906         condition = cp_parser_condition (parser);
6907         finish_while_stmt_cond (condition, statement);
6908         /* Look for the `)'.  */
6909         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6910         /* Parse the dependent statement.  */
6911         parser->in_statement = IN_ITERATION_STMT;
6912         cp_parser_already_scoped_statement (parser);
6913         parser->in_statement = in_statement;
6914         /* We're done with the while-statement.  */
6915         finish_while_stmt (statement);
6916       }
6917       break;
6918
6919     case RID_DO:
6920       {
6921         tree expression;
6922
6923         /* Begin the do-statement.  */
6924         statement = begin_do_stmt ();
6925         /* Parse the body of the do-statement.  */
6926         parser->in_statement = IN_ITERATION_STMT;
6927         cp_parser_implicitly_scoped_statement (parser, NULL);
6928         parser->in_statement = in_statement;
6929         finish_do_body (statement);
6930         /* Look for the `while' keyword.  */
6931         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6932         /* Look for the `('.  */
6933         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6934         /* Parse the expression.  */
6935         expression = cp_parser_expression (parser, /*cast_p=*/false);
6936         /* We're done with the do-statement.  */
6937         finish_do_stmt (expression, statement);
6938         /* Look for the `)'.  */
6939         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6940         /* Look for the `;'.  */
6941         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6942       }
6943       break;
6944
6945     case RID_FOR:
6946       {
6947         tree condition = NULL_TREE;
6948         tree expression = NULL_TREE;
6949
6950         /* Begin the for-statement.  */
6951         statement = begin_for_stmt ();
6952         /* Look for the `('.  */
6953         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6954         /* Parse the initialization.  */
6955         cp_parser_for_init_statement (parser);
6956         finish_for_init_stmt (statement);
6957
6958         /* If there's a condition, process it.  */
6959         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6960           condition = cp_parser_condition (parser);
6961         finish_for_cond (condition, statement);
6962         /* Look for the `;'.  */
6963         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6964
6965         /* If there's an expression, process it.  */
6966         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6967           expression = cp_parser_expression (parser, /*cast_p=*/false);
6968         finish_for_expr (expression, statement);
6969         /* Look for the `)'.  */
6970         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6971
6972         /* Parse the body of the for-statement.  */
6973         parser->in_statement = IN_ITERATION_STMT;
6974         cp_parser_already_scoped_statement (parser);
6975         parser->in_statement = in_statement;
6976
6977         /* We're done with the for-statement.  */
6978         finish_for_stmt (statement);
6979       }
6980       break;
6981
6982     default:
6983       cp_parser_error (parser, "expected iteration-statement");
6984       statement = error_mark_node;
6985       break;
6986     }
6987
6988   return statement;
6989 }
6990
6991 /* Parse a for-init-statement.
6992
6993    for-init-statement:
6994      expression-statement
6995      simple-declaration  */
6996
6997 static void
6998 cp_parser_for_init_statement (cp_parser* parser)
6999 {
7000   /* If the next token is a `;', then we have an empty
7001      expression-statement.  Grammatically, this is also a
7002      simple-declaration, but an invalid one, because it does not
7003      declare anything.  Therefore, if we did not handle this case
7004      specially, we would issue an error message about an invalid
7005      declaration.  */
7006   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7007     {
7008       /* We're going to speculatively look for a declaration, falling back
7009          to an expression, if necessary.  */
7010       cp_parser_parse_tentatively (parser);
7011       /* Parse the declaration.  */
7012       cp_parser_simple_declaration (parser,
7013                                     /*function_definition_allowed_p=*/false);
7014       /* If the tentative parse failed, then we shall need to look for an
7015          expression-statement.  */
7016       if (cp_parser_parse_definitely (parser))
7017         return;
7018     }
7019
7020   cp_parser_expression_statement (parser, false);
7021 }
7022
7023 /* Parse a jump-statement.
7024
7025    jump-statement:
7026      break ;
7027      continue ;
7028      return expression [opt] ;
7029      goto identifier ;
7030
7031    GNU extension:
7032
7033    jump-statement:
7034      goto * expression ;
7035
7036    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7037
7038 static tree
7039 cp_parser_jump_statement (cp_parser* parser)
7040 {
7041   tree statement = error_mark_node;
7042   cp_token *token;
7043   enum rid keyword;
7044   unsigned char in_statement;
7045
7046   /* Peek at the next token.  */
7047   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7048   if (!token)
7049     return error_mark_node;
7050
7051   /* See what kind of keyword it is.  */
7052   keyword = token->keyword;
7053   switch (keyword)
7054     {
7055     case RID_BREAK:
7056       in_statement = parser->in_statement & ~IN_IF_STMT;      
7057       switch (in_statement)
7058         {
7059         case 0:
7060           error ("break statement not within loop or switch");
7061           break;
7062         default:
7063           gcc_assert ((in_statement & IN_SWITCH_STMT)
7064                       || in_statement == IN_ITERATION_STMT);
7065           statement = finish_break_stmt ();
7066           break;
7067         case IN_OMP_BLOCK:
7068           error ("invalid exit from OpenMP structured block");
7069           break;
7070         case IN_OMP_FOR:
7071           error ("break statement used with OpenMP for loop");
7072           break;
7073         }
7074       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7075       break;
7076
7077     case RID_CONTINUE:
7078       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7079         {
7080         case 0:
7081           error ("continue statement not within a loop");
7082           break;
7083         case IN_ITERATION_STMT:
7084         case IN_OMP_FOR:
7085           statement = finish_continue_stmt ();
7086           break;
7087         case IN_OMP_BLOCK:
7088           error ("invalid exit from OpenMP structured block");
7089           break;
7090         default:
7091           gcc_unreachable ();
7092         }
7093       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7094       break;
7095
7096     case RID_RETURN:
7097       {
7098         tree expr;
7099
7100         /* If the next token is a `;', then there is no
7101            expression.  */
7102         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7103           expr = cp_parser_expression (parser, /*cast_p=*/false);
7104         else
7105           expr = NULL_TREE;
7106         /* Build the return-statement.  */
7107         statement = finish_return_stmt (expr);
7108         /* Look for the final `;'.  */
7109         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7110       }
7111       break;
7112
7113     case RID_GOTO:
7114       /* Create the goto-statement.  */
7115       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7116         {
7117           /* Issue a warning about this use of a GNU extension.  */
7118           if (pedantic)
7119             pedwarn ("ISO C++ forbids computed gotos");
7120           /* Consume the '*' token.  */
7121           cp_lexer_consume_token (parser->lexer);
7122           /* Parse the dependent expression.  */
7123           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7124         }
7125       else
7126         finish_goto_stmt (cp_parser_identifier (parser));
7127       /* Look for the final `;'.  */
7128       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7129       break;
7130
7131     default:
7132       cp_parser_error (parser, "expected jump-statement");
7133       break;
7134     }
7135
7136   return statement;
7137 }
7138
7139 /* Parse a declaration-statement.
7140
7141    declaration-statement:
7142      block-declaration  */
7143
7144 static void
7145 cp_parser_declaration_statement (cp_parser* parser)
7146 {
7147   void *p;
7148
7149   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7150   p = obstack_alloc (&declarator_obstack, 0);
7151
7152  /* Parse the block-declaration.  */
7153   cp_parser_block_declaration (parser, /*statement_p=*/true);
7154
7155   /* Free any declarators allocated.  */
7156   obstack_free (&declarator_obstack, p);
7157
7158   /* Finish off the statement.  */
7159   finish_stmt ();
7160 }
7161
7162 /* Some dependent statements (like `if (cond) statement'), are
7163    implicitly in their own scope.  In other words, if the statement is
7164    a single statement (as opposed to a compound-statement), it is
7165    none-the-less treated as if it were enclosed in braces.  Any
7166    declarations appearing in the dependent statement are out of scope
7167    after control passes that point.  This function parses a statement,
7168    but ensures that is in its own scope, even if it is not a
7169    compound-statement.
7170
7171    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7172    is a (possibly labeled) if statement which is not enclosed in
7173    braces and has an else clause.  This is used to implement
7174    -Wparentheses.
7175
7176    Returns the new statement.  */
7177
7178 static tree
7179 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7180 {
7181   tree statement;
7182
7183   if (if_p != NULL)
7184     *if_p = false;
7185
7186   /* Mark if () ; with a special NOP_EXPR.  */
7187   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7188     {
7189       cp_lexer_consume_token (parser->lexer);
7190       statement = add_stmt (build_empty_stmt ());
7191     }
7192   /* if a compound is opened, we simply parse the statement directly.  */
7193   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7194     statement = cp_parser_compound_statement (parser, NULL, false);
7195   /* If the token is not a `{', then we must take special action.  */
7196   else
7197     {
7198       /* Create a compound-statement.  */
7199       statement = begin_compound_stmt (0);
7200       /* Parse the dependent-statement.  */
7201       cp_parser_statement (parser, NULL_TREE, false, if_p);
7202       /* Finish the dummy compound-statement.  */
7203       finish_compound_stmt (statement);
7204     }
7205
7206   /* Return the statement.  */
7207   return statement;
7208 }
7209
7210 /* For some dependent statements (like `while (cond) statement'), we
7211    have already created a scope.  Therefore, even if the dependent
7212    statement is a compound-statement, we do not want to create another
7213    scope.  */
7214
7215 static void
7216 cp_parser_already_scoped_statement (cp_parser* parser)
7217 {
7218   /* If the token is a `{', then we must take special action.  */
7219   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7220     cp_parser_statement (parser, NULL_TREE, false, NULL);
7221   else
7222     {
7223       /* Avoid calling cp_parser_compound_statement, so that we
7224          don't create a new scope.  Do everything else by hand.  */
7225       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7226       cp_parser_statement_seq_opt (parser, NULL_TREE);
7227       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7228     }
7229 }
7230
7231 /* Declarations [gram.dcl.dcl] */
7232
7233 /* Parse an optional declaration-sequence.
7234
7235    declaration-seq:
7236      declaration
7237      declaration-seq declaration  */
7238
7239 static void
7240 cp_parser_declaration_seq_opt (cp_parser* parser)
7241 {
7242   while (true)
7243     {
7244       cp_token *token;
7245
7246       token = cp_lexer_peek_token (parser->lexer);
7247
7248       if (token->type == CPP_CLOSE_BRACE
7249           || token->type == CPP_EOF
7250           || token->type == CPP_PRAGMA_EOL)
7251         break;
7252
7253       if (token->type == CPP_SEMICOLON)
7254         {
7255           /* A declaration consisting of a single semicolon is
7256              invalid.  Allow it unless we're being pedantic.  */
7257           cp_lexer_consume_token (parser->lexer);
7258           if (pedantic && !in_system_header)
7259             pedwarn ("extra %<;%>");
7260           continue;
7261         }
7262
7263       /* If we're entering or exiting a region that's implicitly
7264          extern "C", modify the lang context appropriately.  */
7265       if (!parser->implicit_extern_c && token->implicit_extern_c)
7266         {
7267           push_lang_context (lang_name_c);
7268           parser->implicit_extern_c = true;
7269         }
7270       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7271         {
7272           pop_lang_context ();
7273           parser->implicit_extern_c = false;
7274         }
7275
7276       if (token->type == CPP_PRAGMA)
7277         {
7278           /* A top-level declaration can consist solely of a #pragma.
7279              A nested declaration cannot, so this is done here and not
7280              in cp_parser_declaration.  (A #pragma at block scope is
7281              handled in cp_parser_statement.)  */
7282           cp_parser_pragma (parser, pragma_external);
7283           continue;
7284         }
7285
7286       /* Parse the declaration itself.  */
7287       cp_parser_declaration (parser);
7288     }
7289 }
7290
7291 /* Parse a declaration.
7292
7293    declaration:
7294      block-declaration
7295      function-definition
7296      template-declaration
7297      explicit-instantiation
7298      explicit-specialization
7299      linkage-specification
7300      namespace-definition
7301
7302    GNU extension:
7303
7304    declaration:
7305       __extension__ declaration */
7306
7307 static void
7308 cp_parser_declaration (cp_parser* parser)
7309 {
7310   cp_token token1;
7311   cp_token token2;
7312   int saved_pedantic;
7313   void *p;
7314
7315   /* Check for the `__extension__' keyword.  */
7316   if (cp_parser_extension_opt (parser, &saved_pedantic))
7317     {
7318       /* Parse the qualified declaration.  */
7319       cp_parser_declaration (parser);
7320       /* Restore the PEDANTIC flag.  */
7321       pedantic = saved_pedantic;
7322
7323       return;
7324     }
7325
7326   /* Try to figure out what kind of declaration is present.  */
7327   token1 = *cp_lexer_peek_token (parser->lexer);
7328
7329   if (token1.type != CPP_EOF)
7330     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7331   else
7332     {
7333       token2.type = CPP_EOF;
7334       token2.keyword = RID_MAX;
7335     }
7336
7337   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7338   p = obstack_alloc (&declarator_obstack, 0);
7339
7340   /* If the next token is `extern' and the following token is a string
7341      literal, then we have a linkage specification.  */
7342   if (token1.keyword == RID_EXTERN
7343       && cp_parser_is_string_literal (&token2))
7344     cp_parser_linkage_specification (parser);
7345   /* If the next token is `template', then we have either a template
7346      declaration, an explicit instantiation, or an explicit
7347      specialization.  */
7348   else if (token1.keyword == RID_TEMPLATE)
7349     {
7350       /* `template <>' indicates a template specialization.  */
7351       if (token2.type == CPP_LESS
7352           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7353         cp_parser_explicit_specialization (parser);
7354       /* `template <' indicates a template declaration.  */
7355       else if (token2.type == CPP_LESS)
7356         cp_parser_template_declaration (parser, /*member_p=*/false);
7357       /* Anything else must be an explicit instantiation.  */
7358       else
7359         cp_parser_explicit_instantiation (parser);
7360     }
7361   /* If the next token is `export', then we have a template
7362      declaration.  */
7363   else if (token1.keyword == RID_EXPORT)
7364     cp_parser_template_declaration (parser, /*member_p=*/false);
7365   /* If the next token is `extern', 'static' or 'inline' and the one
7366      after that is `template', we have a GNU extended explicit
7367      instantiation directive.  */
7368   else if (cp_parser_allow_gnu_extensions_p (parser)
7369            && (token1.keyword == RID_EXTERN
7370                || token1.keyword == RID_STATIC
7371                || token1.keyword == RID_INLINE)
7372            && token2.keyword == RID_TEMPLATE)
7373     cp_parser_explicit_instantiation (parser);
7374   /* If the next token is `namespace', check for a named or unnamed
7375      namespace definition.  */
7376   else if (token1.keyword == RID_NAMESPACE
7377            && (/* A named namespace definition.  */
7378                (token2.type == CPP_NAME
7379                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7380                     != CPP_EQ))
7381                /* An unnamed namespace definition.  */
7382                || token2.type == CPP_OPEN_BRACE
7383                || token2.keyword == RID_ATTRIBUTE))
7384     cp_parser_namespace_definition (parser);
7385   /* Objective-C++ declaration/definition.  */
7386   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7387     cp_parser_objc_declaration (parser);
7388   /* We must have either a block declaration or a function
7389      definition.  */
7390   else
7391     /* Try to parse a block-declaration, or a function-definition.  */
7392     cp_parser_block_declaration (parser, /*statement_p=*/false);
7393
7394   /* Free any declarators allocated.  */
7395   obstack_free (&declarator_obstack, p);
7396 }
7397
7398 /* Parse a block-declaration.
7399
7400    block-declaration:
7401      simple-declaration
7402      asm-definition
7403      namespace-alias-definition
7404      using-declaration
7405      using-directive
7406
7407    GNU Extension:
7408
7409    block-declaration:
7410      __extension__ block-declaration
7411      label-declaration
7412
7413    C++0x Extension:
7414
7415    block-declaration:
7416      static_assert-declaration
7417
7418    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7419    part of a declaration-statement.  */
7420
7421 static void
7422 cp_parser_block_declaration (cp_parser *parser,
7423                              bool      statement_p)
7424 {
7425   cp_token *token1;
7426   int saved_pedantic;
7427
7428   /* Check for the `__extension__' keyword.  */
7429   if (cp_parser_extension_opt (parser, &saved_pedantic))
7430     {
7431       /* Parse the qualified declaration.  */
7432       cp_parser_block_declaration (parser, statement_p);
7433       /* Restore the PEDANTIC flag.  */
7434       pedantic = saved_pedantic;
7435
7436       return;
7437     }
7438
7439   /* Peek at the next token to figure out which kind of declaration is
7440      present.  */
7441   token1 = cp_lexer_peek_token (parser->lexer);
7442
7443   /* If the next keyword is `asm', we have an asm-definition.  */
7444   if (token1->keyword == RID_ASM)
7445     {
7446       if (statement_p)
7447         cp_parser_commit_to_tentative_parse (parser);
7448       cp_parser_asm_definition (parser);
7449     }
7450   /* If the next keyword is `namespace', we have a
7451      namespace-alias-definition.  */
7452   else if (token1->keyword == RID_NAMESPACE)
7453     cp_parser_namespace_alias_definition (parser);
7454   /* If the next keyword is `using', we have either a
7455      using-declaration or a using-directive.  */
7456   else if (token1->keyword == RID_USING)
7457     {
7458       cp_token *token2;
7459
7460       if (statement_p)
7461         cp_parser_commit_to_tentative_parse (parser);
7462       /* If the token after `using' is `namespace', then we have a
7463          using-directive.  */
7464       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7465       if (token2->keyword == RID_NAMESPACE)
7466         cp_parser_using_directive (parser);
7467       /* Otherwise, it's a using-declaration.  */
7468       else
7469         cp_parser_using_declaration (parser,
7470                                      /*access_declaration_p=*/false);
7471     }
7472   /* If the next keyword is `__label__' we have a label declaration.  */
7473   else if (token1->keyword == RID_LABEL)
7474     {
7475       if (statement_p)
7476         cp_parser_commit_to_tentative_parse (parser);
7477       cp_parser_label_declaration (parser);
7478     }
7479   /* If the next token is `static_assert' we have a static assertion.  */
7480   else if (token1->keyword == RID_STATIC_ASSERT)
7481     cp_parser_static_assert (parser, /*member_p=*/false);
7482   /* Anything else must be a simple-declaration.  */
7483   else
7484     cp_parser_simple_declaration (parser, !statement_p);
7485 }
7486
7487 /* Parse a simple-declaration.
7488
7489    simple-declaration:
7490      decl-specifier-seq [opt] init-declarator-list [opt] ;
7491
7492    init-declarator-list:
7493      init-declarator
7494      init-declarator-list , init-declarator
7495
7496    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7497    function-definition as a simple-declaration.  */
7498
7499 static void
7500 cp_parser_simple_declaration (cp_parser* parser,
7501                               bool function_definition_allowed_p)
7502 {
7503   cp_decl_specifier_seq decl_specifiers;
7504   int declares_class_or_enum;
7505   bool saw_declarator;
7506
7507   /* Defer access checks until we know what is being declared; the
7508      checks for names appearing in the decl-specifier-seq should be
7509      done as if we were in the scope of the thing being declared.  */
7510   push_deferring_access_checks (dk_deferred);
7511
7512   /* Parse the decl-specifier-seq.  We have to keep track of whether
7513      or not the decl-specifier-seq declares a named class or
7514      enumeration type, since that is the only case in which the
7515      init-declarator-list is allowed to be empty.
7516
7517      [dcl.dcl]
7518
7519      In a simple-declaration, the optional init-declarator-list can be
7520      omitted only when declaring a class or enumeration, that is when
7521      the decl-specifier-seq contains either a class-specifier, an
7522      elaborated-type-specifier, or an enum-specifier.  */
7523   cp_parser_decl_specifier_seq (parser,
7524                                 CP_PARSER_FLAGS_OPTIONAL,
7525                                 &decl_specifiers,
7526                                 &declares_class_or_enum);
7527   /* We no longer need to defer access checks.  */
7528   stop_deferring_access_checks ();
7529
7530   /* In a block scope, a valid declaration must always have a
7531      decl-specifier-seq.  By not trying to parse declarators, we can
7532      resolve the declaration/expression ambiguity more quickly.  */
7533   if (!function_definition_allowed_p
7534       && !decl_specifiers.any_specifiers_p)
7535     {
7536       cp_parser_error (parser, "expected declaration");
7537       goto done;
7538     }
7539
7540   /* If the next two tokens are both identifiers, the code is
7541      erroneous. The usual cause of this situation is code like:
7542
7543        T t;
7544
7545      where "T" should name a type -- but does not.  */
7546   if (!decl_specifiers.type
7547       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7548     {
7549       /* If parsing tentatively, we should commit; we really are
7550          looking at a declaration.  */
7551       cp_parser_commit_to_tentative_parse (parser);
7552       /* Give up.  */
7553       goto done;
7554     }
7555
7556   /* If we have seen at least one decl-specifier, and the next token
7557      is not a parenthesis, then we must be looking at a declaration.
7558      (After "int (" we might be looking at a functional cast.)  */
7559   if (decl_specifiers.any_specifiers_p
7560       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7561     cp_parser_commit_to_tentative_parse (parser);
7562
7563   /* Keep going until we hit the `;' at the end of the simple
7564      declaration.  */
7565   saw_declarator = false;
7566   while (cp_lexer_next_token_is_not (parser->lexer,
7567                                      CPP_SEMICOLON))
7568     {
7569       cp_token *token;
7570       bool function_definition_p;
7571       tree decl;
7572
7573       if (saw_declarator)
7574         {
7575           /* If we are processing next declarator, coma is expected */
7576           token = cp_lexer_peek_token (parser->lexer);
7577           gcc_assert (token->type == CPP_COMMA);
7578           cp_lexer_consume_token (parser->lexer);
7579         }
7580       else
7581         saw_declarator = true;
7582
7583       /* Parse the init-declarator.  */
7584       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7585                                         /*checks=*/NULL,
7586                                         function_definition_allowed_p,
7587                                         /*member_p=*/false,
7588                                         declares_class_or_enum,
7589                                         &function_definition_p);
7590       /* If an error occurred while parsing tentatively, exit quickly.
7591          (That usually happens when in the body of a function; each
7592          statement is treated as a declaration-statement until proven
7593          otherwise.)  */
7594       if (cp_parser_error_occurred (parser))
7595         goto done;
7596       /* Handle function definitions specially.  */
7597       if (function_definition_p)
7598         {
7599           /* If the next token is a `,', then we are probably
7600              processing something like:
7601
7602                void f() {}, *p;
7603
7604              which is erroneous.  */
7605           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7606             error ("mixing declarations and function-definitions is forbidden");
7607           /* Otherwise, we're done with the list of declarators.  */
7608           else
7609             {
7610               pop_deferring_access_checks ();
7611               return;
7612             }
7613         }
7614       /* The next token should be either a `,' or a `;'.  */
7615       token = cp_lexer_peek_token (parser->lexer);
7616       /* If it's a `,', there are more declarators to come.  */
7617       if (token->type == CPP_COMMA)
7618         /* will be consumed next time around */;
7619       /* If it's a `;', we are done.  */
7620       else if (token->type == CPP_SEMICOLON)
7621         break;
7622       /* Anything else is an error.  */
7623       else
7624         {
7625           /* If we have already issued an error message we don't need
7626              to issue another one.  */
7627           if (decl != error_mark_node
7628               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7629             cp_parser_error (parser, "expected %<,%> or %<;%>");
7630           /* Skip tokens until we reach the end of the statement.  */
7631           cp_parser_skip_to_end_of_statement (parser);
7632           /* If the next token is now a `;', consume it.  */
7633           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7634             cp_lexer_consume_token (parser->lexer);
7635           goto done;
7636         }
7637       /* After the first time around, a function-definition is not
7638          allowed -- even if it was OK at first.  For example:
7639
7640            int i, f() {}
7641
7642          is not valid.  */
7643       function_definition_allowed_p = false;
7644     }
7645
7646   /* Issue an error message if no declarators are present, and the
7647      decl-specifier-seq does not itself declare a class or
7648      enumeration.  */
7649   if (!saw_declarator)
7650     {
7651       if (cp_parser_declares_only_class_p (parser))
7652         shadow_tag (&decl_specifiers);
7653       /* Perform any deferred access checks.  */
7654       perform_deferred_access_checks ();
7655     }
7656
7657   /* Consume the `;'.  */
7658   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7659
7660  done:
7661   pop_deferring_access_checks ();
7662 }
7663
7664 /* Parse a decl-specifier-seq.
7665
7666    decl-specifier-seq:
7667      decl-specifier-seq [opt] decl-specifier
7668
7669    decl-specifier:
7670      storage-class-specifier
7671      type-specifier
7672      function-specifier
7673      friend
7674      typedef
7675
7676    GNU Extension:
7677
7678    decl-specifier:
7679      attributes
7680
7681    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7682
7683    The parser flags FLAGS is used to control type-specifier parsing.
7684
7685    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7686    flags:
7687
7688      1: one of the decl-specifiers is an elaborated-type-specifier
7689         (i.e., a type declaration)
7690      2: one of the decl-specifiers is an enum-specifier or a
7691         class-specifier (i.e., a type definition)
7692
7693    */
7694
7695 static void
7696 cp_parser_decl_specifier_seq (cp_parser* parser,
7697                               cp_parser_flags flags,
7698                               cp_decl_specifier_seq *decl_specs,
7699                               int* declares_class_or_enum)
7700 {
7701   bool constructor_possible_p = !parser->in_declarator_p;
7702
7703   /* Clear DECL_SPECS.  */
7704   clear_decl_specs (decl_specs);
7705
7706   /* Assume no class or enumeration type is declared.  */
7707   *declares_class_or_enum = 0;
7708
7709   /* Keep reading specifiers until there are no more to read.  */
7710   while (true)
7711     {
7712       bool constructor_p;
7713       bool found_decl_spec;
7714       cp_token *token;
7715
7716       /* Peek at the next token.  */
7717       token = cp_lexer_peek_token (parser->lexer);
7718       /* Handle attributes.  */
7719       if (token->keyword == RID_ATTRIBUTE)
7720         {
7721           /* Parse the attributes.  */
7722           decl_specs->attributes
7723             = chainon (decl_specs->attributes,
7724                        cp_parser_attributes_opt (parser));
7725           continue;
7726         }
7727       /* Assume we will find a decl-specifier keyword.  */
7728       found_decl_spec = true;
7729       /* If the next token is an appropriate keyword, we can simply
7730          add it to the list.  */
7731       switch (token->keyword)
7732         {
7733           /* decl-specifier:
7734                friend  */
7735         case RID_FRIEND:
7736           if (!at_class_scope_p ())
7737             {
7738               error ("%<friend%> used outside of class");
7739               cp_lexer_purge_token (parser->lexer);
7740             }
7741           else
7742             {
7743               ++decl_specs->specs[(int) ds_friend];
7744               /* Consume the token.  */
7745               cp_lexer_consume_token (parser->lexer);
7746             }
7747           break;
7748
7749           /* function-specifier:
7750                inline
7751                virtual
7752                explicit  */
7753         case RID_INLINE:
7754         case RID_VIRTUAL:
7755         case RID_EXPLICIT:
7756           cp_parser_function_specifier_opt (parser, decl_specs);
7757           break;
7758
7759           /* decl-specifier:
7760                typedef  */
7761         case RID_TYPEDEF:
7762           ++decl_specs->specs[(int) ds_typedef];
7763           /* Consume the token.  */
7764           cp_lexer_consume_token (parser->lexer);
7765           /* A constructor declarator cannot appear in a typedef.  */
7766           constructor_possible_p = false;
7767           /* The "typedef" keyword can only occur in a declaration; we
7768              may as well commit at this point.  */
7769           cp_parser_commit_to_tentative_parse (parser);
7770
7771           if (decl_specs->storage_class != sc_none)
7772             decl_specs->conflicting_specifiers_p = true;
7773           break;
7774
7775           /* storage-class-specifier:
7776                auto
7777                register
7778                static
7779                extern
7780                mutable
7781
7782              GNU Extension:
7783                thread  */
7784         case RID_AUTO:
7785         case RID_REGISTER:
7786         case RID_STATIC:
7787         case RID_EXTERN:
7788         case RID_MUTABLE:
7789           /* Consume the token.  */
7790           cp_lexer_consume_token (parser->lexer);
7791           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7792           break;
7793         case RID_THREAD:
7794           /* Consume the token.  */
7795           cp_lexer_consume_token (parser->lexer);
7796           ++decl_specs->specs[(int) ds_thread];
7797           break;
7798
7799         default:
7800           /* We did not yet find a decl-specifier yet.  */
7801           found_decl_spec = false;
7802           break;
7803         }
7804
7805       /* Constructors are a special case.  The `S' in `S()' is not a
7806          decl-specifier; it is the beginning of the declarator.  */
7807       constructor_p
7808         = (!found_decl_spec
7809            && constructor_possible_p
7810            && (cp_parser_constructor_declarator_p
7811                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7812
7813       /* If we don't have a DECL_SPEC yet, then we must be looking at
7814          a type-specifier.  */
7815       if (!found_decl_spec && !constructor_p)
7816         {
7817           int decl_spec_declares_class_or_enum;
7818           bool is_cv_qualifier;
7819           tree type_spec;
7820
7821           type_spec
7822             = cp_parser_type_specifier (parser, flags,
7823                                         decl_specs,
7824                                         /*is_declaration=*/true,
7825                                         &decl_spec_declares_class_or_enum,
7826                                         &is_cv_qualifier);
7827
7828           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7829
7830           /* If this type-specifier referenced a user-defined type
7831              (a typedef, class-name, etc.), then we can't allow any
7832              more such type-specifiers henceforth.
7833
7834              [dcl.spec]
7835
7836              The longest sequence of decl-specifiers that could
7837              possibly be a type name is taken as the
7838              decl-specifier-seq of a declaration.  The sequence shall
7839              be self-consistent as described below.
7840
7841              [dcl.type]
7842
7843              As a general rule, at most one type-specifier is allowed
7844              in the complete decl-specifier-seq of a declaration.  The
7845              only exceptions are the following:
7846
7847              -- const or volatile can be combined with any other
7848                 type-specifier.
7849
7850              -- signed or unsigned can be combined with char, long,
7851                 short, or int.
7852
7853              -- ..
7854
7855              Example:
7856
7857                typedef char* Pc;
7858                void g (const int Pc);
7859
7860              Here, Pc is *not* part of the decl-specifier seq; it's
7861              the declarator.  Therefore, once we see a type-specifier
7862              (other than a cv-qualifier), we forbid any additional
7863              user-defined types.  We *do* still allow things like `int
7864              int' to be considered a decl-specifier-seq, and issue the
7865              error message later.  */
7866           if (type_spec && !is_cv_qualifier)
7867             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7868           /* A constructor declarator cannot follow a type-specifier.  */
7869           if (type_spec)
7870             {
7871               constructor_possible_p = false;
7872               found_decl_spec = true;
7873             }
7874         }
7875
7876       /* If we still do not have a DECL_SPEC, then there are no more
7877          decl-specifiers.  */
7878       if (!found_decl_spec)
7879         break;
7880
7881       decl_specs->any_specifiers_p = true;
7882       /* After we see one decl-specifier, further decl-specifiers are
7883          always optional.  */
7884       flags |= CP_PARSER_FLAGS_OPTIONAL;
7885     }
7886
7887   cp_parser_check_decl_spec (decl_specs);
7888
7889   /* Don't allow a friend specifier with a class definition.  */
7890   if (decl_specs->specs[(int) ds_friend] != 0
7891       && (*declares_class_or_enum & 2))
7892     error ("class definition may not be declared a friend");
7893 }
7894
7895 /* Parse an (optional) storage-class-specifier.
7896
7897    storage-class-specifier:
7898      auto
7899      register
7900      static
7901      extern
7902      mutable
7903
7904    GNU Extension:
7905
7906    storage-class-specifier:
7907      thread
7908
7909    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7910
7911 static tree
7912 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7913 {
7914   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7915     {
7916     case RID_AUTO:
7917     case RID_REGISTER:
7918     case RID_STATIC:
7919     case RID_EXTERN:
7920     case RID_MUTABLE:
7921     case RID_THREAD:
7922       /* Consume the token.  */
7923       return cp_lexer_consume_token (parser->lexer)->u.value;
7924
7925     default:
7926       return NULL_TREE;
7927     }
7928 }
7929
7930 /* Parse an (optional) function-specifier.
7931
7932    function-specifier:
7933      inline
7934      virtual
7935      explicit
7936
7937    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7938    Updates DECL_SPECS, if it is non-NULL.  */
7939
7940 static tree
7941 cp_parser_function_specifier_opt (cp_parser* parser,
7942                                   cp_decl_specifier_seq *decl_specs)
7943 {
7944   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7945     {
7946     case RID_INLINE:
7947       if (decl_specs)
7948         ++decl_specs->specs[(int) ds_inline];
7949       break;
7950
7951     case RID_VIRTUAL:
7952       /* 14.5.2.3 [temp.mem]
7953
7954          A member function template shall not be virtual.  */
7955       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7956         error ("templates may not be %<virtual%>");
7957       else if (decl_specs)
7958         ++decl_specs->specs[(int) ds_virtual];
7959       break;
7960
7961     case RID_EXPLICIT:
7962       if (decl_specs)
7963         ++decl_specs->specs[(int) ds_explicit];
7964       break;
7965
7966     default:
7967       return NULL_TREE;
7968     }
7969
7970   /* Consume the token.  */
7971   return cp_lexer_consume_token (parser->lexer)->u.value;
7972 }
7973
7974 /* Parse a linkage-specification.
7975
7976    linkage-specification:
7977      extern string-literal { declaration-seq [opt] }
7978      extern string-literal declaration  */
7979
7980 static void
7981 cp_parser_linkage_specification (cp_parser* parser)
7982 {
7983   tree linkage;
7984
7985   /* Look for the `extern' keyword.  */
7986   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7987
7988   /* Look for the string-literal.  */
7989   linkage = cp_parser_string_literal (parser, false, false);
7990
7991   /* Transform the literal into an identifier.  If the literal is a
7992      wide-character string, or contains embedded NULs, then we can't
7993      handle it as the user wants.  */
7994   if (strlen (TREE_STRING_POINTER (linkage))
7995       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7996     {
7997       cp_parser_error (parser, "invalid linkage-specification");
7998       /* Assume C++ linkage.  */
7999       linkage = lang_name_cplusplus;
8000     }
8001   else
8002     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8003
8004   /* We're now using the new linkage.  */
8005   push_lang_context (linkage);
8006
8007   /* If the next token is a `{', then we're using the first
8008      production.  */
8009   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8010     {
8011       /* Consume the `{' token.  */
8012       cp_lexer_consume_token (parser->lexer);
8013       /* Parse the declarations.  */
8014       cp_parser_declaration_seq_opt (parser);
8015       /* Look for the closing `}'.  */
8016       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8017     }
8018   /* Otherwise, there's just one declaration.  */
8019   else
8020     {
8021       bool saved_in_unbraced_linkage_specification_p;
8022
8023       saved_in_unbraced_linkage_specification_p
8024         = parser->in_unbraced_linkage_specification_p;
8025       parser->in_unbraced_linkage_specification_p = true;
8026       cp_parser_declaration (parser);
8027       parser->in_unbraced_linkage_specification_p
8028         = saved_in_unbraced_linkage_specification_p;
8029     }
8030
8031   /* We're done with the linkage-specification.  */
8032   pop_lang_context ();
8033 }
8034
8035 /* Parse a static_assert-declaration.
8036
8037    static_assert-declaration:
8038      static_assert ( constant-expression , string-literal ) ; 
8039
8040    If MEMBER_P, this static_assert is a class member.  */
8041
8042 static void 
8043 cp_parser_static_assert(cp_parser *parser, bool member_p)
8044 {
8045   tree condition;
8046   tree message;
8047   cp_token *token;
8048   location_t saved_loc;
8049
8050   /* Peek at the `static_assert' token so we can keep track of exactly
8051      where the static assertion started.  */
8052   token = cp_lexer_peek_token (parser->lexer);
8053   saved_loc = token->location;
8054
8055   /* Look for the `static_assert' keyword.  */
8056   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8057                                   "`static_assert'"))
8058     return;
8059
8060   /*  We know we are in a static assertion; commit to any tentative
8061       parse.  */
8062   if (cp_parser_parsing_tentatively (parser))
8063     cp_parser_commit_to_tentative_parse (parser);
8064
8065   /* Parse the `(' starting the static assertion condition.  */
8066   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8067
8068   /* Parse the constant-expression.  */
8069   condition = 
8070     cp_parser_constant_expression (parser,
8071                                    /*allow_non_constant_p=*/false,
8072                                    /*non_constant_p=*/NULL);
8073
8074   /* Parse the separating `,'.  */
8075   cp_parser_require (parser, CPP_COMMA, "`,'");
8076
8077   /* Parse the string-literal message.  */
8078   message = cp_parser_string_literal (parser, 
8079                                       /*translate=*/false,
8080                                       /*wide_ok=*/true);
8081
8082   /* A `)' completes the static assertion.  */
8083   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8084     cp_parser_skip_to_closing_parenthesis (parser, 
8085                                            /*recovering=*/true, 
8086                                            /*or_comma=*/false,
8087                                            /*consume_paren=*/true);
8088
8089   /* A semicolon terminates the declaration.  */
8090   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8091
8092   /* Complete the static assertion, which may mean either processing 
8093      the static assert now or saving it for template instantiation.  */
8094   finish_static_assert (condition, message, saved_loc, member_p);
8095 }
8096
8097 /* Special member functions [gram.special] */
8098
8099 /* Parse a conversion-function-id.
8100
8101    conversion-function-id:
8102      operator conversion-type-id
8103
8104    Returns an IDENTIFIER_NODE representing the operator.  */
8105
8106 static tree
8107 cp_parser_conversion_function_id (cp_parser* parser)
8108 {
8109   tree type;
8110   tree saved_scope;
8111   tree saved_qualifying_scope;
8112   tree saved_object_scope;
8113   tree pushed_scope = NULL_TREE;
8114
8115   /* Look for the `operator' token.  */
8116   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8117     return error_mark_node;
8118   /* When we parse the conversion-type-id, the current scope will be
8119      reset.  However, we need that information in able to look up the
8120      conversion function later, so we save it here.  */
8121   saved_scope = parser->scope;
8122   saved_qualifying_scope = parser->qualifying_scope;
8123   saved_object_scope = parser->object_scope;
8124   /* We must enter the scope of the class so that the names of
8125      entities declared within the class are available in the
8126      conversion-type-id.  For example, consider:
8127
8128        struct S {
8129          typedef int I;
8130          operator I();
8131        };
8132
8133        S::operator I() { ... }
8134
8135      In order to see that `I' is a type-name in the definition, we
8136      must be in the scope of `S'.  */
8137   if (saved_scope)
8138     pushed_scope = push_scope (saved_scope);
8139   /* Parse the conversion-type-id.  */
8140   type = cp_parser_conversion_type_id (parser);
8141   /* Leave the scope of the class, if any.  */
8142   if (pushed_scope)
8143     pop_scope (pushed_scope);
8144   /* Restore the saved scope.  */
8145   parser->scope = saved_scope;
8146   parser->qualifying_scope = saved_qualifying_scope;
8147   parser->object_scope = saved_object_scope;
8148   /* If the TYPE is invalid, indicate failure.  */
8149   if (type == error_mark_node)
8150     return error_mark_node;
8151   return mangle_conv_op_name_for_type (type);
8152 }
8153
8154 /* Parse a conversion-type-id:
8155
8156    conversion-type-id:
8157      type-specifier-seq conversion-declarator [opt]
8158
8159    Returns the TYPE specified.  */
8160
8161 static tree
8162 cp_parser_conversion_type_id (cp_parser* parser)
8163 {
8164   tree attributes;
8165   cp_decl_specifier_seq type_specifiers;
8166   cp_declarator *declarator;
8167   tree type_specified;
8168
8169   /* Parse the attributes.  */
8170   attributes = cp_parser_attributes_opt (parser);
8171   /* Parse the type-specifiers.  */
8172   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8173                                 &type_specifiers);
8174   /* If that didn't work, stop.  */
8175   if (type_specifiers.type == error_mark_node)
8176     return error_mark_node;
8177   /* Parse the conversion-declarator.  */
8178   declarator = cp_parser_conversion_declarator_opt (parser);
8179
8180   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8181                                     /*initialized=*/0, &attributes);
8182   if (attributes)
8183     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8184   return type_specified;
8185 }
8186
8187 /* Parse an (optional) conversion-declarator.
8188
8189    conversion-declarator:
8190      ptr-operator conversion-declarator [opt]
8191
8192    */
8193
8194 static cp_declarator *
8195 cp_parser_conversion_declarator_opt (cp_parser* parser)
8196 {
8197   enum tree_code code;
8198   tree class_type;
8199   cp_cv_quals cv_quals;
8200
8201   /* We don't know if there's a ptr-operator next, or not.  */
8202   cp_parser_parse_tentatively (parser);
8203   /* Try the ptr-operator.  */
8204   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8205   /* If it worked, look for more conversion-declarators.  */
8206   if (cp_parser_parse_definitely (parser))
8207     {
8208       cp_declarator *declarator;
8209
8210       /* Parse another optional declarator.  */
8211       declarator = cp_parser_conversion_declarator_opt (parser);
8212
8213       /* Create the representation of the declarator.  */
8214       if (class_type)
8215         declarator = make_ptrmem_declarator (cv_quals, class_type,
8216                                              declarator);
8217       else if (code == INDIRECT_REF)
8218         declarator = make_pointer_declarator (cv_quals, declarator);
8219       else
8220         declarator = make_reference_declarator (cv_quals, declarator);
8221
8222       return declarator;
8223    }
8224
8225   return NULL;
8226 }
8227
8228 /* Parse an (optional) ctor-initializer.
8229
8230    ctor-initializer:
8231      : mem-initializer-list
8232
8233    Returns TRUE iff the ctor-initializer was actually present.  */
8234
8235 static bool
8236 cp_parser_ctor_initializer_opt (cp_parser* parser)
8237 {
8238   /* If the next token is not a `:', then there is no
8239      ctor-initializer.  */
8240   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8241     {
8242       /* Do default initialization of any bases and members.  */
8243       if (DECL_CONSTRUCTOR_P (current_function_decl))
8244         finish_mem_initializers (NULL_TREE);
8245
8246       return false;
8247     }
8248
8249   /* Consume the `:' token.  */
8250   cp_lexer_consume_token (parser->lexer);
8251   /* And the mem-initializer-list.  */
8252   cp_parser_mem_initializer_list (parser);
8253
8254   return true;
8255 }
8256
8257 /* Parse a mem-initializer-list.
8258
8259    mem-initializer-list:
8260      mem-initializer ... [opt]
8261      mem-initializer ... [opt] , mem-initializer-list  */
8262
8263 static void
8264 cp_parser_mem_initializer_list (cp_parser* parser)
8265 {
8266   tree mem_initializer_list = NULL_TREE;
8267
8268   /* Let the semantic analysis code know that we are starting the
8269      mem-initializer-list.  */
8270   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8271     error ("only constructors take base initializers");
8272
8273   /* Loop through the list.  */
8274   while (true)
8275     {
8276       tree mem_initializer;
8277
8278       /* Parse the mem-initializer.  */
8279       mem_initializer = cp_parser_mem_initializer (parser);
8280       /* If the next token is a `...', we're expanding member initializers. */
8281       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8282         {
8283           /* Consume the `...'. */
8284           cp_lexer_consume_token (parser->lexer);
8285
8286           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8287              can be expanded but members cannot. */
8288           if (mem_initializer != error_mark_node
8289               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8290             {
8291               error ("cannot expand initializer for member %<%D%>", 
8292                      TREE_PURPOSE (mem_initializer));
8293               mem_initializer = error_mark_node;
8294             }
8295
8296           /* Construct the pack expansion type. */
8297           if (mem_initializer != error_mark_node)
8298             mem_initializer = make_pack_expansion (mem_initializer);
8299         }
8300       /* Add it to the list, unless it was erroneous.  */
8301       if (mem_initializer != error_mark_node)
8302         {
8303           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8304           mem_initializer_list = mem_initializer;
8305         }
8306       /* If the next token is not a `,', we're done.  */
8307       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8308         break;
8309       /* Consume the `,' token.  */
8310       cp_lexer_consume_token (parser->lexer);
8311     }
8312
8313   /* Perform semantic analysis.  */
8314   if (DECL_CONSTRUCTOR_P (current_function_decl))
8315     finish_mem_initializers (mem_initializer_list);
8316 }
8317
8318 /* Parse a mem-initializer.
8319
8320    mem-initializer:
8321      mem-initializer-id ( expression-list [opt] )
8322
8323    GNU extension:
8324
8325    mem-initializer:
8326      ( expression-list [opt] )
8327
8328    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8329    class) or FIELD_DECL (for a non-static data member) to initialize;
8330    the TREE_VALUE is the expression-list.  An empty initialization
8331    list is represented by void_list_node.  */
8332
8333 static tree
8334 cp_parser_mem_initializer (cp_parser* parser)
8335 {
8336   tree mem_initializer_id;
8337   tree expression_list;
8338   tree member;
8339
8340   /* Find out what is being initialized.  */
8341   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8342     {
8343       pedwarn ("anachronistic old-style base class initializer");
8344       mem_initializer_id = NULL_TREE;
8345     }
8346   else
8347     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8348   member = expand_member_init (mem_initializer_id);
8349   if (member && !DECL_P (member))
8350     in_base_initializer = 1;
8351
8352   expression_list
8353     = cp_parser_parenthesized_expression_list (parser, false,
8354                                                /*cast_p=*/false,
8355                                                /*allow_expansion_p=*/true,
8356                                                /*non_constant_p=*/NULL);
8357   if (expression_list == error_mark_node)
8358     return error_mark_node;
8359   if (!expression_list)
8360     expression_list = void_type_node;
8361
8362   in_base_initializer = 0;
8363
8364   return member ? build_tree_list (member, expression_list) : error_mark_node;
8365 }
8366
8367 /* Parse a mem-initializer-id.
8368
8369    mem-initializer-id:
8370      :: [opt] nested-name-specifier [opt] class-name
8371      identifier
8372
8373    Returns a TYPE indicating the class to be initializer for the first
8374    production.  Returns an IDENTIFIER_NODE indicating the data member
8375    to be initialized for the second production.  */
8376
8377 static tree
8378 cp_parser_mem_initializer_id (cp_parser* parser)
8379 {
8380   bool global_scope_p;
8381   bool nested_name_specifier_p;
8382   bool template_p = false;
8383   tree id;
8384
8385   /* `typename' is not allowed in this context ([temp.res]).  */
8386   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8387     {
8388       error ("keyword %<typename%> not allowed in this context (a qualified "
8389              "member initializer is implicitly a type)");
8390       cp_lexer_consume_token (parser->lexer);
8391     }
8392   /* Look for the optional `::' operator.  */
8393   global_scope_p
8394     = (cp_parser_global_scope_opt (parser,
8395                                    /*current_scope_valid_p=*/false)
8396        != NULL_TREE);
8397   /* Look for the optional nested-name-specifier.  The simplest way to
8398      implement:
8399
8400        [temp.res]
8401
8402        The keyword `typename' is not permitted in a base-specifier or
8403        mem-initializer; in these contexts a qualified name that
8404        depends on a template-parameter is implicitly assumed to be a
8405        type name.
8406
8407      is to assume that we have seen the `typename' keyword at this
8408      point.  */
8409   nested_name_specifier_p
8410     = (cp_parser_nested_name_specifier_opt (parser,
8411                                             /*typename_keyword_p=*/true,
8412                                             /*check_dependency_p=*/true,
8413                                             /*type_p=*/true,
8414                                             /*is_declaration=*/true)
8415        != NULL_TREE);
8416   if (nested_name_specifier_p)
8417     template_p = cp_parser_optional_template_keyword (parser);
8418   /* If there is a `::' operator or a nested-name-specifier, then we
8419      are definitely looking for a class-name.  */
8420   if (global_scope_p || nested_name_specifier_p)
8421     return cp_parser_class_name (parser,
8422                                  /*typename_keyword_p=*/true,
8423                                  /*template_keyword_p=*/template_p,
8424                                  none_type,
8425                                  /*check_dependency_p=*/true,
8426                                  /*class_head_p=*/false,
8427                                  /*is_declaration=*/true);
8428   /* Otherwise, we could also be looking for an ordinary identifier.  */
8429   cp_parser_parse_tentatively (parser);
8430   /* Try a class-name.  */
8431   id = cp_parser_class_name (parser,
8432                              /*typename_keyword_p=*/true,
8433                              /*template_keyword_p=*/false,
8434                              none_type,
8435                              /*check_dependency_p=*/true,
8436                              /*class_head_p=*/false,
8437                              /*is_declaration=*/true);
8438   /* If we found one, we're done.  */
8439   if (cp_parser_parse_definitely (parser))
8440     return id;
8441   /* Otherwise, look for an ordinary identifier.  */
8442   return cp_parser_identifier (parser);
8443 }
8444
8445 /* Overloading [gram.over] */
8446
8447 /* Parse an operator-function-id.
8448
8449    operator-function-id:
8450      operator operator
8451
8452    Returns an IDENTIFIER_NODE for the operator which is a
8453    human-readable spelling of the identifier, e.g., `operator +'.  */
8454
8455 static tree
8456 cp_parser_operator_function_id (cp_parser* parser)
8457 {
8458   /* Look for the `operator' keyword.  */
8459   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8460     return error_mark_node;
8461   /* And then the name of the operator itself.  */
8462   return cp_parser_operator (parser);
8463 }
8464
8465 /* Parse an operator.
8466
8467    operator:
8468      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8469      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8470      || ++ -- , ->* -> () []
8471
8472    GNU Extensions:
8473
8474    operator:
8475      <? >? <?= >?=
8476
8477    Returns an IDENTIFIER_NODE for the operator which is a
8478    human-readable spelling of the identifier, e.g., `operator +'.  */
8479
8480 static tree
8481 cp_parser_operator (cp_parser* parser)
8482 {
8483   tree id = NULL_TREE;
8484   cp_token *token;
8485
8486   /* Peek at the next token.  */
8487   token = cp_lexer_peek_token (parser->lexer);
8488   /* Figure out which operator we have.  */
8489   switch (token->type)
8490     {
8491     case CPP_KEYWORD:
8492       {
8493         enum tree_code op;
8494
8495         /* The keyword should be either `new' or `delete'.  */
8496         if (token->keyword == RID_NEW)
8497           op = NEW_EXPR;
8498         else if (token->keyword == RID_DELETE)
8499           op = DELETE_EXPR;
8500         else
8501           break;
8502
8503         /* Consume the `new' or `delete' token.  */
8504         cp_lexer_consume_token (parser->lexer);
8505
8506         /* Peek at the next token.  */
8507         token = cp_lexer_peek_token (parser->lexer);
8508         /* If it's a `[' token then this is the array variant of the
8509            operator.  */
8510         if (token->type == CPP_OPEN_SQUARE)
8511           {
8512             /* Consume the `[' token.  */
8513             cp_lexer_consume_token (parser->lexer);
8514             /* Look for the `]' token.  */
8515             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8516             id = ansi_opname (op == NEW_EXPR
8517                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8518           }
8519         /* Otherwise, we have the non-array variant.  */
8520         else
8521           id = ansi_opname (op);
8522
8523         return id;
8524       }
8525
8526     case CPP_PLUS:
8527       id = ansi_opname (PLUS_EXPR);
8528       break;
8529
8530     case CPP_MINUS:
8531       id = ansi_opname (MINUS_EXPR);
8532       break;
8533
8534     case CPP_MULT:
8535       id = ansi_opname (MULT_EXPR);
8536       break;
8537
8538     case CPP_DIV:
8539       id = ansi_opname (TRUNC_DIV_EXPR);
8540       break;
8541
8542     case CPP_MOD:
8543       id = ansi_opname (TRUNC_MOD_EXPR);
8544       break;
8545
8546     case CPP_XOR:
8547       id = ansi_opname (BIT_XOR_EXPR);
8548       break;
8549
8550     case CPP_AND:
8551       id = ansi_opname (BIT_AND_EXPR);
8552       break;
8553
8554     case CPP_OR:
8555       id = ansi_opname (BIT_IOR_EXPR);
8556       break;
8557
8558     case CPP_COMPL:
8559       id = ansi_opname (BIT_NOT_EXPR);
8560       break;
8561
8562     case CPP_NOT:
8563       id = ansi_opname (TRUTH_NOT_EXPR);
8564       break;
8565
8566     case CPP_EQ:
8567       id = ansi_assopname (NOP_EXPR);
8568       break;
8569
8570     case CPP_LESS:
8571       id = ansi_opname (LT_EXPR);
8572       break;
8573
8574     case CPP_GREATER:
8575       id = ansi_opname (GT_EXPR);
8576       break;
8577
8578     case CPP_PLUS_EQ:
8579       id = ansi_assopname (PLUS_EXPR);
8580       break;
8581
8582     case CPP_MINUS_EQ:
8583       id = ansi_assopname (MINUS_EXPR);
8584       break;
8585
8586     case CPP_MULT_EQ:
8587       id = ansi_assopname (MULT_EXPR);
8588       break;
8589
8590     case CPP_DIV_EQ:
8591       id = ansi_assopname (TRUNC_DIV_EXPR);
8592       break;
8593
8594     case CPP_MOD_EQ:
8595       id = ansi_assopname (TRUNC_MOD_EXPR);
8596       break;
8597
8598     case CPP_XOR_EQ:
8599       id = ansi_assopname (BIT_XOR_EXPR);
8600       break;
8601
8602     case CPP_AND_EQ:
8603       id = ansi_assopname (BIT_AND_EXPR);
8604       break;
8605
8606     case CPP_OR_EQ:
8607       id = ansi_assopname (BIT_IOR_EXPR);
8608       break;
8609
8610     case CPP_LSHIFT:
8611       id = ansi_opname (LSHIFT_EXPR);
8612       break;
8613
8614     case CPP_RSHIFT:
8615       id = ansi_opname (RSHIFT_EXPR);
8616       break;
8617
8618     case CPP_LSHIFT_EQ:
8619       id = ansi_assopname (LSHIFT_EXPR);
8620       break;
8621
8622     case CPP_RSHIFT_EQ:
8623       id = ansi_assopname (RSHIFT_EXPR);
8624       break;
8625
8626     case CPP_EQ_EQ:
8627       id = ansi_opname (EQ_EXPR);
8628       break;
8629
8630     case CPP_NOT_EQ:
8631       id = ansi_opname (NE_EXPR);
8632       break;
8633
8634     case CPP_LESS_EQ:
8635       id = ansi_opname (LE_EXPR);
8636       break;
8637
8638     case CPP_GREATER_EQ:
8639       id = ansi_opname (GE_EXPR);
8640       break;
8641
8642     case CPP_AND_AND:
8643       id = ansi_opname (TRUTH_ANDIF_EXPR);
8644       break;
8645
8646     case CPP_OR_OR:
8647       id = ansi_opname (TRUTH_ORIF_EXPR);
8648       break;
8649
8650     case CPP_PLUS_PLUS:
8651       id = ansi_opname (POSTINCREMENT_EXPR);
8652       break;
8653
8654     case CPP_MINUS_MINUS:
8655       id = ansi_opname (PREDECREMENT_EXPR);
8656       break;
8657
8658     case CPP_COMMA:
8659       id = ansi_opname (COMPOUND_EXPR);
8660       break;
8661
8662     case CPP_DEREF_STAR:
8663       id = ansi_opname (MEMBER_REF);
8664       break;
8665
8666     case CPP_DEREF:
8667       id = ansi_opname (COMPONENT_REF);
8668       break;
8669
8670     case CPP_OPEN_PAREN:
8671       /* Consume the `('.  */
8672       cp_lexer_consume_token (parser->lexer);
8673       /* Look for the matching `)'.  */
8674       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8675       return ansi_opname (CALL_EXPR);
8676
8677     case CPP_OPEN_SQUARE:
8678       /* Consume the `['.  */
8679       cp_lexer_consume_token (parser->lexer);
8680       /* Look for the matching `]'.  */
8681       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8682       return ansi_opname (ARRAY_REF);
8683
8684     default:
8685       /* Anything else is an error.  */
8686       break;
8687     }
8688
8689   /* If we have selected an identifier, we need to consume the
8690      operator token.  */
8691   if (id)
8692     cp_lexer_consume_token (parser->lexer);
8693   /* Otherwise, no valid operator name was present.  */
8694   else
8695     {
8696       cp_parser_error (parser, "expected operator");
8697       id = error_mark_node;
8698     }
8699
8700   return id;
8701 }
8702
8703 /* Parse a template-declaration.
8704
8705    template-declaration:
8706      export [opt] template < template-parameter-list > declaration
8707
8708    If MEMBER_P is TRUE, this template-declaration occurs within a
8709    class-specifier.
8710
8711    The grammar rule given by the standard isn't correct.  What
8712    is really meant is:
8713
8714    template-declaration:
8715      export [opt] template-parameter-list-seq
8716        decl-specifier-seq [opt] init-declarator [opt] ;
8717      export [opt] template-parameter-list-seq
8718        function-definition
8719
8720    template-parameter-list-seq:
8721      template-parameter-list-seq [opt]
8722      template < template-parameter-list >  */
8723
8724 static void
8725 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8726 {
8727   /* Check for `export'.  */
8728   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8729     {
8730       /* Consume the `export' token.  */
8731       cp_lexer_consume_token (parser->lexer);
8732       /* Warn that we do not support `export'.  */
8733       warning (0, "keyword %<export%> not implemented, and will be ignored");
8734     }
8735
8736   cp_parser_template_declaration_after_export (parser, member_p);
8737 }
8738
8739 /* Parse a template-parameter-list.
8740
8741    template-parameter-list:
8742      template-parameter
8743      template-parameter-list , template-parameter
8744
8745    Returns a TREE_LIST.  Each node represents a template parameter.
8746    The nodes are connected via their TREE_CHAINs.  */
8747
8748 static tree
8749 cp_parser_template_parameter_list (cp_parser* parser)
8750 {
8751   tree parameter_list = NULL_TREE;
8752
8753   begin_template_parm_list ();
8754   while (true)
8755     {
8756       tree parameter;
8757       cp_token *token;
8758       bool is_non_type;
8759       bool is_parameter_pack;
8760
8761       /* Parse the template-parameter.  */
8762       parameter = cp_parser_template_parameter (parser, 
8763                                                 &is_non_type,
8764                                                 &is_parameter_pack);
8765       /* Add it to the list.  */
8766       if (parameter != error_mark_node)
8767         parameter_list = process_template_parm (parameter_list,
8768                                                 parameter,
8769                                                 is_non_type,
8770                                                 is_parameter_pack);
8771       else
8772        {
8773          tree err_parm = build_tree_list (parameter, parameter);
8774          TREE_VALUE (err_parm) = error_mark_node;
8775          parameter_list = chainon (parameter_list, err_parm);
8776        }
8777
8778       /* Peek at the next token.  */
8779       token = cp_lexer_peek_token (parser->lexer);
8780       /* If it's not a `,', we're done.  */
8781       if (token->type != CPP_COMMA)
8782         break;
8783       /* Otherwise, consume the `,' token.  */
8784       cp_lexer_consume_token (parser->lexer);
8785     }
8786
8787   return end_template_parm_list (parameter_list);
8788 }
8789
8790 /* Parse a template-parameter.
8791
8792    template-parameter:
8793      type-parameter
8794      parameter-declaration
8795
8796    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8797    the parameter.  The TREE_PURPOSE is the default value, if any.
8798    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8799    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
8800    set to true iff this parameter is a parameter pack. */
8801
8802 static tree
8803 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
8804                               bool *is_parameter_pack)
8805 {
8806   cp_token *token;
8807   cp_parameter_declarator *parameter_declarator;
8808   tree parm;
8809
8810   /* Assume it is a type parameter or a template parameter.  */
8811   *is_non_type = false;
8812   /* Assume it not a parameter pack. */
8813   *is_parameter_pack = false;
8814   /* Peek at the next token.  */
8815   token = cp_lexer_peek_token (parser->lexer);
8816   /* If it is `class' or `template', we have a type-parameter.  */
8817   if (token->keyword == RID_TEMPLATE)
8818     return cp_parser_type_parameter (parser, is_parameter_pack);
8819   /* If it is `class' or `typename' we do not know yet whether it is a
8820      type parameter or a non-type parameter.  Consider:
8821
8822        template <typename T, typename T::X X> ...
8823
8824      or:
8825
8826        template <class C, class D*> ...
8827
8828      Here, the first parameter is a type parameter, and the second is
8829      a non-type parameter.  We can tell by looking at the token after
8830      the identifier -- if it is a `,', `=', or `>' then we have a type
8831      parameter.  */
8832   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8833     {
8834       /* Peek at the token after `class' or `typename'.  */
8835       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8836       /* If it's an ellipsis, we have a template type parameter
8837          pack. */
8838       if (token->type == CPP_ELLIPSIS)
8839         return cp_parser_type_parameter (parser, is_parameter_pack);
8840       /* If it's an identifier, skip it.  */
8841       if (token->type == CPP_NAME)
8842         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8843       /* Now, see if the token looks like the end of a template
8844          parameter.  */
8845       if (token->type == CPP_COMMA
8846           || token->type == CPP_EQ
8847           || token->type == CPP_GREATER)
8848         return cp_parser_type_parameter (parser, is_parameter_pack);
8849     }
8850
8851   /* Otherwise, it is a non-type parameter.
8852
8853      [temp.param]
8854
8855      When parsing a default template-argument for a non-type
8856      template-parameter, the first non-nested `>' is taken as the end
8857      of the template parameter-list rather than a greater-than
8858      operator.  */
8859   *is_non_type = true;
8860   parameter_declarator
8861      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8862                                         /*parenthesized_p=*/NULL);
8863
8864   /* If the parameter declaration is marked as a parameter pack, set
8865      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
8866      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
8867      grokdeclarator. */
8868   if (parameter_declarator
8869       && parameter_declarator->declarator
8870       && parameter_declarator->declarator->parameter_pack_p)
8871     {
8872       *is_parameter_pack = true;
8873       parameter_declarator->declarator->parameter_pack_p = false;
8874     }
8875
8876   /* If the next token is an ellipsis, and we don't already have it
8877      marked as a parameter pack, then we have a parameter pack (that
8878      has no declarator); */
8879   if (!*is_parameter_pack
8880       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8881     {
8882
8883       /* Consume the `...'. */
8884       cp_lexer_consume_token (parser->lexer);
8885       maybe_warn_variadic_templates ();
8886       
8887       *is_parameter_pack = true;
8888     }
8889
8890   parm = grokdeclarator (parameter_declarator->declarator,
8891                          &parameter_declarator->decl_specifiers,
8892                          PARM, /*initialized=*/0,
8893                          /*attrlist=*/NULL);
8894   if (parm == error_mark_node)
8895     return error_mark_node;
8896
8897   return build_tree_list (parameter_declarator->default_argument, parm);
8898 }
8899
8900 /* Parse a type-parameter.
8901
8902    type-parameter:
8903      class identifier [opt]
8904      class identifier [opt] = type-id
8905      typename identifier [opt]
8906      typename identifier [opt] = type-id
8907      template < template-parameter-list > class identifier [opt]
8908      template < template-parameter-list > class identifier [opt]
8909        = id-expression
8910
8911    GNU Extension (variadic templates):
8912
8913    type-parameter:
8914      class ... identifier [opt]
8915      typename ... identifier [opt]
8916
8917    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8918    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8919    the declaration of the parameter.
8920
8921    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
8922
8923 static tree
8924 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
8925 {
8926   cp_token *token;
8927   tree parameter;
8928
8929   /* Look for a keyword to tell us what kind of parameter this is.  */
8930   token = cp_parser_require (parser, CPP_KEYWORD,
8931                              "`class', `typename', or `template'");
8932   if (!token)
8933     return error_mark_node;
8934
8935   switch (token->keyword)
8936     {
8937     case RID_CLASS:
8938     case RID_TYPENAME:
8939       {
8940         tree identifier;
8941         tree default_argument;
8942
8943         /* If the next token is an ellipsis, we have a template
8944            argument pack. */
8945         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8946           {
8947             /* Consume the `...' token. */
8948             cp_lexer_consume_token (parser->lexer);
8949             maybe_warn_variadic_templates ();
8950
8951             *is_parameter_pack = true;
8952           }
8953
8954         /* If the next token is an identifier, then it names the
8955            parameter.  */
8956         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8957           identifier = cp_parser_identifier (parser);
8958         else
8959           identifier = NULL_TREE;
8960
8961         /* Create the parameter.  */
8962         parameter = finish_template_type_parm (class_type_node, identifier);
8963
8964         /* If the next token is an `=', we have a default argument.  */
8965         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8966           {
8967             /* Consume the `=' token.  */
8968             cp_lexer_consume_token (parser->lexer);
8969             /* Parse the default-argument.  */
8970             push_deferring_access_checks (dk_no_deferred);
8971             default_argument = cp_parser_type_id (parser);
8972
8973             /* Template parameter packs cannot have default
8974                arguments. */
8975             if (*is_parameter_pack)
8976               {
8977                 if (identifier)
8978                   error ("template parameter pack %qD cannot have a default argument", 
8979                          identifier);
8980                 else
8981                   error ("template parameter packs cannot have default arguments");
8982                 default_argument = NULL_TREE;
8983               }
8984             pop_deferring_access_checks ();
8985           }
8986         else
8987           default_argument = NULL_TREE;
8988
8989         /* Create the combined representation of the parameter and the
8990            default argument.  */
8991         parameter = build_tree_list (default_argument, parameter);
8992       }
8993       break;
8994
8995     case RID_TEMPLATE:
8996       {
8997         tree parameter_list;
8998         tree identifier;
8999         tree default_argument;
9000
9001         /* Look for the `<'.  */
9002         cp_parser_require (parser, CPP_LESS, "`<'");
9003         /* Parse the template-parameter-list.  */
9004         parameter_list = cp_parser_template_parameter_list (parser);
9005         /* Look for the `>'.  */
9006         cp_parser_require (parser, CPP_GREATER, "`>'");
9007         /* Look for the `class' keyword.  */
9008         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9009         /* If the next token is an ellipsis, we have a template
9010            argument pack. */
9011         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9012           {
9013             /* Consume the `...' token. */
9014             cp_lexer_consume_token (parser->lexer);
9015             maybe_warn_variadic_templates ();
9016
9017             *is_parameter_pack = true;
9018           }
9019         /* If the next token is an `=', then there is a
9020            default-argument.  If the next token is a `>', we are at
9021            the end of the parameter-list.  If the next token is a `,',
9022            then we are at the end of this parameter.  */
9023         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9024             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9025             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9026           {
9027             identifier = cp_parser_identifier (parser);
9028             /* Treat invalid names as if the parameter were nameless.  */
9029             if (identifier == error_mark_node)
9030               identifier = NULL_TREE;
9031           }
9032         else
9033           identifier = NULL_TREE;
9034
9035         /* Create the template parameter.  */
9036         parameter = finish_template_template_parm (class_type_node,
9037                                                    identifier);
9038
9039         /* If the next token is an `=', then there is a
9040            default-argument.  */
9041         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9042           {
9043             bool is_template;
9044
9045             /* Consume the `='.  */
9046             cp_lexer_consume_token (parser->lexer);
9047             /* Parse the id-expression.  */
9048             push_deferring_access_checks (dk_no_deferred);
9049             default_argument
9050               = cp_parser_id_expression (parser,
9051                                          /*template_keyword_p=*/false,
9052                                          /*check_dependency_p=*/true,
9053                                          /*template_p=*/&is_template,
9054                                          /*declarator_p=*/false,
9055                                          /*optional_p=*/false);
9056             if (TREE_CODE (default_argument) == TYPE_DECL)
9057               /* If the id-expression was a template-id that refers to
9058                  a template-class, we already have the declaration here,
9059                  so no further lookup is needed.  */
9060                  ;
9061             else
9062               /* Look up the name.  */
9063               default_argument
9064                 = cp_parser_lookup_name (parser, default_argument,
9065                                          none_type,
9066                                          /*is_template=*/is_template,
9067                                          /*is_namespace=*/false,
9068                                          /*check_dependency=*/true,
9069                                          /*ambiguous_decls=*/NULL);
9070             /* See if the default argument is valid.  */
9071             default_argument
9072               = check_template_template_default_arg (default_argument);
9073
9074             /* Template parameter packs cannot have default
9075                arguments. */
9076             if (*is_parameter_pack)
9077               {
9078                 if (identifier)
9079                   error ("template parameter pack %qD cannot have a default argument", 
9080                          identifier);
9081                 else
9082                   error ("template parameter packs cannot have default arguments");
9083                 default_argument = NULL_TREE;
9084               }
9085             pop_deferring_access_checks ();
9086           }
9087         else
9088           default_argument = NULL_TREE;
9089
9090         /* Create the combined representation of the parameter and the
9091            default argument.  */
9092         parameter = build_tree_list (default_argument, parameter);
9093       }
9094       break;
9095
9096     default:
9097       gcc_unreachable ();
9098       break;
9099     }
9100
9101   return parameter;
9102 }
9103
9104 /* Parse a template-id.
9105
9106    template-id:
9107      template-name < template-argument-list [opt] >
9108
9109    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9110    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9111    returned.  Otherwise, if the template-name names a function, or set
9112    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9113    names a class, returns a TYPE_DECL for the specialization.
9114
9115    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9116    uninstantiated templates.  */
9117
9118 static tree
9119 cp_parser_template_id (cp_parser *parser,
9120                        bool template_keyword_p,
9121                        bool check_dependency_p,
9122                        bool is_declaration)
9123 {
9124   int i;
9125   tree template;
9126   tree arguments;
9127   tree template_id;
9128   cp_token_position start_of_id = 0;
9129   deferred_access_check *chk;
9130   VEC (deferred_access_check,gc) *access_check;
9131   cp_token *next_token, *next_token_2;
9132   bool is_identifier;
9133
9134   /* If the next token corresponds to a template-id, there is no need
9135      to reparse it.  */
9136   next_token = cp_lexer_peek_token (parser->lexer);
9137   if (next_token->type == CPP_TEMPLATE_ID)
9138     {
9139       struct tree_check *check_value;
9140
9141       /* Get the stored value.  */
9142       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9143       /* Perform any access checks that were deferred.  */
9144       access_check = check_value->checks;
9145       if (access_check)
9146         {
9147           for (i = 0 ;
9148                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9149                ++i)
9150             {
9151               perform_or_defer_access_check (chk->binfo,
9152                                              chk->decl,
9153                                              chk->diag_decl);
9154             }
9155         }
9156       /* Return the stored value.  */
9157       return check_value->value;
9158     }
9159
9160   /* Avoid performing name lookup if there is no possibility of
9161      finding a template-id.  */
9162   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9163       || (next_token->type == CPP_NAME
9164           && !cp_parser_nth_token_starts_template_argument_list_p
9165                (parser, 2)))
9166     {
9167       cp_parser_error (parser, "expected template-id");
9168       return error_mark_node;
9169     }
9170
9171   /* Remember where the template-id starts.  */
9172   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9173     start_of_id = cp_lexer_token_position (parser->lexer, false);
9174
9175   push_deferring_access_checks (dk_deferred);
9176
9177   /* Parse the template-name.  */
9178   is_identifier = false;
9179   template = cp_parser_template_name (parser, template_keyword_p,
9180                                       check_dependency_p,
9181                                       is_declaration,
9182                                       &is_identifier);
9183   if (template == error_mark_node || is_identifier)
9184     {
9185       pop_deferring_access_checks ();
9186       return template;
9187     }
9188
9189   /* If we find the sequence `[:' after a template-name, it's probably
9190      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9191      parse correctly the argument list.  */
9192   next_token = cp_lexer_peek_token (parser->lexer);
9193   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9194   if (next_token->type == CPP_OPEN_SQUARE
9195       && next_token->flags & DIGRAPH
9196       && next_token_2->type == CPP_COLON
9197       && !(next_token_2->flags & PREV_WHITE))
9198     {
9199       cp_parser_parse_tentatively (parser);
9200       /* Change `:' into `::'.  */
9201       next_token_2->type = CPP_SCOPE;
9202       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9203          CPP_LESS.  */
9204       cp_lexer_consume_token (parser->lexer);
9205       /* Parse the arguments.  */
9206       arguments = cp_parser_enclosed_template_argument_list (parser);
9207       if (!cp_parser_parse_definitely (parser))
9208         {
9209           /* If we couldn't parse an argument list, then we revert our changes
9210              and return simply an error. Maybe this is not a template-id
9211              after all.  */
9212           next_token_2->type = CPP_COLON;
9213           cp_parser_error (parser, "expected %<<%>");
9214           pop_deferring_access_checks ();
9215           return error_mark_node;
9216         }
9217       /* Otherwise, emit an error about the invalid digraph, but continue
9218          parsing because we got our argument list.  */
9219       pedwarn ("%<<::%> cannot begin a template-argument list");
9220       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9221               "between %<<%> and %<::%>");
9222       if (!flag_permissive)
9223         {
9224           static bool hint;
9225           if (!hint)
9226             {
9227               inform ("(if you use -fpermissive G++ will accept your code)");
9228               hint = true;
9229             }
9230         }
9231     }
9232   else
9233     {
9234       /* Look for the `<' that starts the template-argument-list.  */
9235       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9236         {
9237           pop_deferring_access_checks ();
9238           return error_mark_node;
9239         }
9240       /* Parse the arguments.  */
9241       arguments = cp_parser_enclosed_template_argument_list (parser);
9242     }
9243
9244   /* Build a representation of the specialization.  */
9245   if (TREE_CODE (template) == IDENTIFIER_NODE)
9246     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9247   else if (DECL_CLASS_TEMPLATE_P (template)
9248            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9249     {
9250       bool entering_scope;
9251       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9252          template (rather than some instantiation thereof) only if
9253          is not nested within some other construct.  For example, in
9254          "template <typename T> void f(T) { A<T>::", A<T> is just an
9255          instantiation of A.  */
9256       entering_scope = (template_parm_scope_p ()
9257                         && cp_lexer_next_token_is (parser->lexer,
9258                                                    CPP_SCOPE));
9259       template_id
9260         = finish_template_type (template, arguments, entering_scope);
9261     }
9262   else
9263     {
9264       /* If it's not a class-template or a template-template, it should be
9265          a function-template.  */
9266       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9267                    || TREE_CODE (template) == OVERLOAD
9268                    || BASELINK_P (template)));
9269
9270       template_id = lookup_template_function (template, arguments);
9271     }
9272
9273   /* If parsing tentatively, replace the sequence of tokens that makes
9274      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9275      should we re-parse the token stream, we will not have to repeat
9276      the effort required to do the parse, nor will we issue duplicate
9277      error messages about problems during instantiation of the
9278      template.  */
9279   if (start_of_id)
9280     {
9281       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9282
9283       /* Reset the contents of the START_OF_ID token.  */
9284       token->type = CPP_TEMPLATE_ID;
9285       /* Retrieve any deferred checks.  Do not pop this access checks yet
9286          so the memory will not be reclaimed during token replacing below.  */
9287       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9288       token->u.tree_check_value->value = template_id;
9289       token->u.tree_check_value->checks = get_deferred_access_checks ();
9290       token->keyword = RID_MAX;
9291
9292       /* Purge all subsequent tokens.  */
9293       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9294
9295       /* ??? Can we actually assume that, if template_id ==
9296          error_mark_node, we will have issued a diagnostic to the
9297          user, as opposed to simply marking the tentative parse as
9298          failed?  */
9299       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9300         error ("parse error in template argument list");
9301     }
9302
9303   pop_deferring_access_checks ();
9304   return template_id;
9305 }
9306
9307 /* Parse a template-name.
9308
9309    template-name:
9310      identifier
9311
9312    The standard should actually say:
9313
9314    template-name:
9315      identifier
9316      operator-function-id
9317
9318    A defect report has been filed about this issue.
9319
9320    A conversion-function-id cannot be a template name because they cannot
9321    be part of a template-id. In fact, looking at this code:
9322
9323    a.operator K<int>()
9324
9325    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9326    It is impossible to call a templated conversion-function-id with an
9327    explicit argument list, since the only allowed template parameter is
9328    the type to which it is converting.
9329
9330    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9331    `template' keyword, in a construction like:
9332
9333      T::template f<3>()
9334
9335    In that case `f' is taken to be a template-name, even though there
9336    is no way of knowing for sure.
9337
9338    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9339    name refers to a set of overloaded functions, at least one of which
9340    is a template, or an IDENTIFIER_NODE with the name of the template,
9341    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9342    names are looked up inside uninstantiated templates.  */
9343
9344 static tree
9345 cp_parser_template_name (cp_parser* parser,
9346                          bool template_keyword_p,
9347                          bool check_dependency_p,
9348                          bool is_declaration,
9349                          bool *is_identifier)
9350 {
9351   tree identifier;
9352   tree decl;
9353   tree fns;
9354
9355   /* If the next token is `operator', then we have either an
9356      operator-function-id or a conversion-function-id.  */
9357   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9358     {
9359       /* We don't know whether we're looking at an
9360          operator-function-id or a conversion-function-id.  */
9361       cp_parser_parse_tentatively (parser);
9362       /* Try an operator-function-id.  */
9363       identifier = cp_parser_operator_function_id (parser);
9364       /* If that didn't work, try a conversion-function-id.  */
9365       if (!cp_parser_parse_definitely (parser))
9366         {
9367           cp_parser_error (parser, "expected template-name");
9368           return error_mark_node;
9369         }
9370     }
9371   /* Look for the identifier.  */
9372   else
9373     identifier = cp_parser_identifier (parser);
9374
9375   /* If we didn't find an identifier, we don't have a template-id.  */
9376   if (identifier == error_mark_node)
9377     return error_mark_node;
9378
9379   /* If the name immediately followed the `template' keyword, then it
9380      is a template-name.  However, if the next token is not `<', then
9381      we do not treat it as a template-name, since it is not being used
9382      as part of a template-id.  This enables us to handle constructs
9383      like:
9384
9385        template <typename T> struct S { S(); };
9386        template <typename T> S<T>::S();
9387
9388      correctly.  We would treat `S' as a template -- if it were `S<T>'
9389      -- but we do not if there is no `<'.  */
9390
9391   if (processing_template_decl
9392       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9393     {
9394       /* In a declaration, in a dependent context, we pretend that the
9395          "template" keyword was present in order to improve error
9396          recovery.  For example, given:
9397
9398            template <typename T> void f(T::X<int>);
9399
9400          we want to treat "X<int>" as a template-id.  */
9401       if (is_declaration
9402           && !template_keyword_p
9403           && parser->scope && TYPE_P (parser->scope)
9404           && check_dependency_p
9405           && dependent_type_p (parser->scope)
9406           /* Do not do this for dtors (or ctors), since they never
9407              need the template keyword before their name.  */
9408           && !constructor_name_p (identifier, parser->scope))
9409         {
9410           cp_token_position start = 0;
9411
9412           /* Explain what went wrong.  */
9413           error ("non-template %qD used as template", identifier);
9414           inform ("use %<%T::template %D%> to indicate that it is a template",
9415                   parser->scope, identifier);
9416           /* If parsing tentatively, find the location of the "<" token.  */
9417           if (cp_parser_simulate_error (parser))
9418             start = cp_lexer_token_position (parser->lexer, true);
9419           /* Parse the template arguments so that we can issue error
9420              messages about them.  */
9421           cp_lexer_consume_token (parser->lexer);
9422           cp_parser_enclosed_template_argument_list (parser);
9423           /* Skip tokens until we find a good place from which to
9424              continue parsing.  */
9425           cp_parser_skip_to_closing_parenthesis (parser,
9426                                                  /*recovering=*/true,
9427                                                  /*or_comma=*/true,
9428                                                  /*consume_paren=*/false);
9429           /* If parsing tentatively, permanently remove the
9430              template argument list.  That will prevent duplicate
9431              error messages from being issued about the missing
9432              "template" keyword.  */
9433           if (start)
9434             cp_lexer_purge_tokens_after (parser->lexer, start);
9435           if (is_identifier)
9436             *is_identifier = true;
9437           return identifier;
9438         }
9439
9440       /* If the "template" keyword is present, then there is generally
9441          no point in doing name-lookup, so we just return IDENTIFIER.
9442          But, if the qualifying scope is non-dependent then we can
9443          (and must) do name-lookup normally.  */
9444       if (template_keyword_p
9445           && (!parser->scope
9446               || (TYPE_P (parser->scope)
9447                   && dependent_type_p (parser->scope))))
9448         return identifier;
9449     }
9450
9451   /* Look up the name.  */
9452   decl = cp_parser_lookup_name (parser, identifier,
9453                                 none_type,
9454                                 /*is_template=*/false,
9455                                 /*is_namespace=*/false,
9456                                 check_dependency_p,
9457                                 /*ambiguous_decls=*/NULL);
9458   decl = maybe_get_template_decl_from_type_decl (decl);
9459
9460   /* If DECL is a template, then the name was a template-name.  */
9461   if (TREE_CODE (decl) == TEMPLATE_DECL)
9462     ;
9463   else
9464     {
9465       tree fn = NULL_TREE;
9466
9467       /* The standard does not explicitly indicate whether a name that
9468          names a set of overloaded declarations, some of which are
9469          templates, is a template-name.  However, such a name should
9470          be a template-name; otherwise, there is no way to form a
9471          template-id for the overloaded templates.  */
9472       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9473       if (TREE_CODE (fns) == OVERLOAD)
9474         for (fn = fns; fn; fn = OVL_NEXT (fn))
9475           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9476             break;
9477
9478       if (!fn)
9479         {
9480           /* The name does not name a template.  */
9481           cp_parser_error (parser, "expected template-name");
9482           return error_mark_node;
9483         }
9484     }
9485
9486   /* If DECL is dependent, and refers to a function, then just return
9487      its name; we will look it up again during template instantiation.  */
9488   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9489     {
9490       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9491       if (TYPE_P (scope) && dependent_type_p (scope))
9492         return identifier;
9493     }
9494
9495   return decl;
9496 }
9497
9498 /* Parse a template-argument-list.
9499
9500    template-argument-list:
9501      template-argument ... [opt]
9502      template-argument-list , template-argument ... [opt]
9503
9504    Returns a TREE_VEC containing the arguments.  */
9505
9506 static tree
9507 cp_parser_template_argument_list (cp_parser* parser)
9508 {
9509   tree fixed_args[10];
9510   unsigned n_args = 0;
9511   unsigned alloced = 10;
9512   tree *arg_ary = fixed_args;
9513   tree vec;
9514   bool saved_in_template_argument_list_p;
9515   bool saved_ice_p;
9516   bool saved_non_ice_p;
9517
9518   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9519   parser->in_template_argument_list_p = true;
9520   /* Even if the template-id appears in an integral
9521      constant-expression, the contents of the argument list do
9522      not.  */
9523   saved_ice_p = parser->integral_constant_expression_p;
9524   parser->integral_constant_expression_p = false;
9525   saved_non_ice_p = parser->non_integral_constant_expression_p;
9526   parser->non_integral_constant_expression_p = false;
9527   /* Parse the arguments.  */
9528   do
9529     {
9530       tree argument;
9531
9532       if (n_args)
9533         /* Consume the comma.  */
9534         cp_lexer_consume_token (parser->lexer);
9535
9536       /* Parse the template-argument.  */
9537       argument = cp_parser_template_argument (parser);
9538
9539       /* If the next token is an ellipsis, we're expanding a template
9540          argument pack. */
9541       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9542         {
9543           /* Consume the `...' token. */
9544           cp_lexer_consume_token (parser->lexer);
9545
9546           /* Make the argument into a TYPE_PACK_EXPANSION or
9547              EXPR_PACK_EXPANSION. */
9548           argument = make_pack_expansion (argument);
9549         }
9550
9551       if (n_args == alloced)
9552         {
9553           alloced *= 2;
9554
9555           if (arg_ary == fixed_args)
9556             {
9557               arg_ary = XNEWVEC (tree, alloced);
9558               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9559             }
9560           else
9561             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9562         }
9563       arg_ary[n_args++] = argument;
9564     }
9565   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9566
9567   vec = make_tree_vec (n_args);
9568
9569   while (n_args--)
9570     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9571
9572   if (arg_ary != fixed_args)
9573     free (arg_ary);
9574   parser->non_integral_constant_expression_p = saved_non_ice_p;
9575   parser->integral_constant_expression_p = saved_ice_p;
9576   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9577   return vec;
9578 }
9579
9580 /* Parse a template-argument.
9581
9582    template-argument:
9583      assignment-expression
9584      type-id
9585      id-expression
9586
9587    The representation is that of an assignment-expression, type-id, or
9588    id-expression -- except that the qualified id-expression is
9589    evaluated, so that the value returned is either a DECL or an
9590    OVERLOAD.
9591
9592    Although the standard says "assignment-expression", it forbids
9593    throw-expressions or assignments in the template argument.
9594    Therefore, we use "conditional-expression" instead.  */
9595
9596 static tree
9597 cp_parser_template_argument (cp_parser* parser)
9598 {
9599   tree argument;
9600   bool template_p;
9601   bool address_p;
9602   bool maybe_type_id = false;
9603   cp_token *token;
9604   cp_id_kind idk;
9605
9606   /* There's really no way to know what we're looking at, so we just
9607      try each alternative in order.
9608
9609        [temp.arg]
9610
9611        In a template-argument, an ambiguity between a type-id and an
9612        expression is resolved to a type-id, regardless of the form of
9613        the corresponding template-parameter.
9614
9615      Therefore, we try a type-id first.  */
9616   cp_parser_parse_tentatively (parser);
9617   argument = cp_parser_type_id (parser);
9618   /* If there was no error parsing the type-id but the next token is a '>>',
9619      we probably found a typo for '> >'. But there are type-id which are
9620      also valid expressions. For instance:
9621
9622      struct X { int operator >> (int); };
9623      template <int V> struct Foo {};
9624      Foo<X () >> 5> r;
9625
9626      Here 'X()' is a valid type-id of a function type, but the user just
9627      wanted to write the expression "X() >> 5". Thus, we remember that we
9628      found a valid type-id, but we still try to parse the argument as an
9629      expression to see what happens.  */
9630   if (!cp_parser_error_occurred (parser)
9631       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9632     {
9633       maybe_type_id = true;
9634       cp_parser_abort_tentative_parse (parser);
9635     }
9636   else
9637     {
9638       /* If the next token isn't a `,' or a `>', then this argument wasn't
9639       really finished. This means that the argument is not a valid
9640       type-id.  */
9641       if (!cp_parser_next_token_ends_template_argument_p (parser))
9642         cp_parser_error (parser, "expected template-argument");
9643       /* If that worked, we're done.  */
9644       if (cp_parser_parse_definitely (parser))
9645         return argument;
9646     }
9647   /* We're still not sure what the argument will be.  */
9648   cp_parser_parse_tentatively (parser);
9649   /* Try a template.  */
9650   argument = cp_parser_id_expression (parser,
9651                                       /*template_keyword_p=*/false,
9652                                       /*check_dependency_p=*/true,
9653                                       &template_p,
9654                                       /*declarator_p=*/false,
9655                                       /*optional_p=*/false);
9656   /* If the next token isn't a `,' or a `>', then this argument wasn't
9657      really finished.  */
9658   if (!cp_parser_next_token_ends_template_argument_p (parser))
9659     cp_parser_error (parser, "expected template-argument");
9660   if (!cp_parser_error_occurred (parser))
9661     {
9662       /* Figure out what is being referred to.  If the id-expression
9663          was for a class template specialization, then we will have a
9664          TYPE_DECL at this point.  There is no need to do name lookup
9665          at this point in that case.  */
9666       if (TREE_CODE (argument) != TYPE_DECL)
9667         argument = cp_parser_lookup_name (parser, argument,
9668                                           none_type,
9669                                           /*is_template=*/template_p,
9670                                           /*is_namespace=*/false,
9671                                           /*check_dependency=*/true,
9672                                           /*ambiguous_decls=*/NULL);
9673       if (TREE_CODE (argument) != TEMPLATE_DECL
9674           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9675         cp_parser_error (parser, "expected template-name");
9676     }
9677   if (cp_parser_parse_definitely (parser))
9678     return argument;
9679   /* It must be a non-type argument.  There permitted cases are given
9680      in [temp.arg.nontype]:
9681
9682      -- an integral constant-expression of integral or enumeration
9683         type; or
9684
9685      -- the name of a non-type template-parameter; or
9686
9687      -- the name of an object or function with external linkage...
9688
9689      -- the address of an object or function with external linkage...
9690
9691      -- a pointer to member...  */
9692   /* Look for a non-type template parameter.  */
9693   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9694     {
9695       cp_parser_parse_tentatively (parser);
9696       argument = cp_parser_primary_expression (parser,
9697                                                /*adress_p=*/false,
9698                                                /*cast_p=*/false,
9699                                                /*template_arg_p=*/true,
9700                                                &idk);
9701       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9702           || !cp_parser_next_token_ends_template_argument_p (parser))
9703         cp_parser_simulate_error (parser);
9704       if (cp_parser_parse_definitely (parser))
9705         return argument;
9706     }
9707
9708   /* If the next token is "&", the argument must be the address of an
9709      object or function with external linkage.  */
9710   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9711   if (address_p)
9712     cp_lexer_consume_token (parser->lexer);
9713   /* See if we might have an id-expression.  */
9714   token = cp_lexer_peek_token (parser->lexer);
9715   if (token->type == CPP_NAME
9716       || token->keyword == RID_OPERATOR
9717       || token->type == CPP_SCOPE
9718       || token->type == CPP_TEMPLATE_ID
9719       || token->type == CPP_NESTED_NAME_SPECIFIER)
9720     {
9721       cp_parser_parse_tentatively (parser);
9722       argument = cp_parser_primary_expression (parser,
9723                                                address_p,
9724                                                /*cast_p=*/false,
9725                                                /*template_arg_p=*/true,
9726                                                &idk);
9727       if (cp_parser_error_occurred (parser)
9728           || !cp_parser_next_token_ends_template_argument_p (parser))
9729         cp_parser_abort_tentative_parse (parser);
9730       else
9731         {
9732           if (TREE_CODE (argument) == INDIRECT_REF)
9733             {
9734               gcc_assert (REFERENCE_REF_P (argument));
9735               argument = TREE_OPERAND (argument, 0);
9736             }
9737
9738           if (TREE_CODE (argument) == VAR_DECL)
9739             {
9740               /* A variable without external linkage might still be a
9741                  valid constant-expression, so no error is issued here
9742                  if the external-linkage check fails.  */
9743               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9744                 cp_parser_simulate_error (parser);
9745             }
9746           else if (is_overloaded_fn (argument))
9747             /* All overloaded functions are allowed; if the external
9748                linkage test does not pass, an error will be issued
9749                later.  */
9750             ;
9751           else if (address_p
9752                    && (TREE_CODE (argument) == OFFSET_REF
9753                        || TREE_CODE (argument) == SCOPE_REF))
9754             /* A pointer-to-member.  */
9755             ;
9756           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9757             ;
9758           else
9759             cp_parser_simulate_error (parser);
9760
9761           if (cp_parser_parse_definitely (parser))
9762             {
9763               if (address_p)
9764                 argument = build_x_unary_op (ADDR_EXPR, argument);
9765               return argument;
9766             }
9767         }
9768     }
9769   /* If the argument started with "&", there are no other valid
9770      alternatives at this point.  */
9771   if (address_p)
9772     {
9773       cp_parser_error (parser, "invalid non-type template argument");
9774       return error_mark_node;
9775     }
9776
9777   /* If the argument wasn't successfully parsed as a type-id followed
9778      by '>>', the argument can only be a constant expression now.
9779      Otherwise, we try parsing the constant-expression tentatively,
9780      because the argument could really be a type-id.  */
9781   if (maybe_type_id)
9782     cp_parser_parse_tentatively (parser);
9783   argument = cp_parser_constant_expression (parser,
9784                                             /*allow_non_constant_p=*/false,
9785                                             /*non_constant_p=*/NULL);
9786   argument = fold_non_dependent_expr (argument);
9787   if (!maybe_type_id)
9788     return argument;
9789   if (!cp_parser_next_token_ends_template_argument_p (parser))
9790     cp_parser_error (parser, "expected template-argument");
9791   if (cp_parser_parse_definitely (parser))
9792     return argument;
9793   /* We did our best to parse the argument as a non type-id, but that
9794      was the only alternative that matched (albeit with a '>' after
9795      it). We can assume it's just a typo from the user, and a
9796      diagnostic will then be issued.  */
9797   return cp_parser_type_id (parser);
9798 }
9799
9800 /* Parse an explicit-instantiation.
9801
9802    explicit-instantiation:
9803      template declaration
9804
9805    Although the standard says `declaration', what it really means is:
9806
9807    explicit-instantiation:
9808      template decl-specifier-seq [opt] declarator [opt] ;
9809
9810    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9811    supposed to be allowed.  A defect report has been filed about this
9812    issue.
9813
9814    GNU Extension:
9815
9816    explicit-instantiation:
9817      storage-class-specifier template
9818        decl-specifier-seq [opt] declarator [opt] ;
9819      function-specifier template
9820        decl-specifier-seq [opt] declarator [opt] ;  */
9821
9822 static void
9823 cp_parser_explicit_instantiation (cp_parser* parser)
9824 {
9825   int declares_class_or_enum;
9826   cp_decl_specifier_seq decl_specifiers;
9827   tree extension_specifier = NULL_TREE;
9828
9829   /* Look for an (optional) storage-class-specifier or
9830      function-specifier.  */
9831   if (cp_parser_allow_gnu_extensions_p (parser))
9832     {
9833       extension_specifier
9834         = cp_parser_storage_class_specifier_opt (parser);
9835       if (!extension_specifier)
9836         extension_specifier
9837           = cp_parser_function_specifier_opt (parser,
9838                                               /*decl_specs=*/NULL);
9839     }
9840
9841   /* Look for the `template' keyword.  */
9842   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9843   /* Let the front end know that we are processing an explicit
9844      instantiation.  */
9845   begin_explicit_instantiation ();
9846   /* [temp.explicit] says that we are supposed to ignore access
9847      control while processing explicit instantiation directives.  */
9848   push_deferring_access_checks (dk_no_check);
9849   /* Parse a decl-specifier-seq.  */
9850   cp_parser_decl_specifier_seq (parser,
9851                                 CP_PARSER_FLAGS_OPTIONAL,
9852                                 &decl_specifiers,
9853                                 &declares_class_or_enum);
9854   /* If there was exactly one decl-specifier, and it declared a class,
9855      and there's no declarator, then we have an explicit type
9856      instantiation.  */
9857   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9858     {
9859       tree type;
9860
9861       type = check_tag_decl (&decl_specifiers);
9862       /* Turn access control back on for names used during
9863          template instantiation.  */
9864       pop_deferring_access_checks ();
9865       if (type)
9866         do_type_instantiation (type, extension_specifier,
9867                                /*complain=*/tf_error);
9868     }
9869   else
9870     {
9871       cp_declarator *declarator;
9872       tree decl;
9873
9874       /* Parse the declarator.  */
9875       declarator
9876         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9877                                 /*ctor_dtor_or_conv_p=*/NULL,
9878                                 /*parenthesized_p=*/NULL,
9879                                 /*member_p=*/false);
9880       if (declares_class_or_enum & 2)
9881         cp_parser_check_for_definition_in_return_type (declarator,
9882                                                        decl_specifiers.type);
9883       if (declarator != cp_error_declarator)
9884         {
9885           decl = grokdeclarator (declarator, &decl_specifiers,
9886                                  NORMAL, 0, &decl_specifiers.attributes);
9887           /* Turn access control back on for names used during
9888              template instantiation.  */
9889           pop_deferring_access_checks ();
9890           /* Do the explicit instantiation.  */
9891           do_decl_instantiation (decl, extension_specifier);
9892         }
9893       else
9894         {
9895           pop_deferring_access_checks ();
9896           /* Skip the body of the explicit instantiation.  */
9897           cp_parser_skip_to_end_of_statement (parser);
9898         }
9899     }
9900   /* We're done with the instantiation.  */
9901   end_explicit_instantiation ();
9902
9903   cp_parser_consume_semicolon_at_end_of_statement (parser);
9904 }
9905
9906 /* Parse an explicit-specialization.
9907
9908    explicit-specialization:
9909      template < > declaration
9910
9911    Although the standard says `declaration', what it really means is:
9912
9913    explicit-specialization:
9914      template <> decl-specifier [opt] init-declarator [opt] ;
9915      template <> function-definition
9916      template <> explicit-specialization
9917      template <> template-declaration  */
9918
9919 static void
9920 cp_parser_explicit_specialization (cp_parser* parser)
9921 {
9922   bool need_lang_pop;
9923   /* Look for the `template' keyword.  */
9924   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9925   /* Look for the `<'.  */
9926   cp_parser_require (parser, CPP_LESS, "`<'");
9927   /* Look for the `>'.  */
9928   cp_parser_require (parser, CPP_GREATER, "`>'");
9929   /* We have processed another parameter list.  */
9930   ++parser->num_template_parameter_lists;
9931   /* [temp]
9932
9933      A template ... explicit specialization ... shall not have C
9934      linkage.  */
9935   if (current_lang_name == lang_name_c)
9936     {
9937       error ("template specialization with C linkage");
9938       /* Give it C++ linkage to avoid confusing other parts of the
9939          front end.  */
9940       push_lang_context (lang_name_cplusplus);
9941       need_lang_pop = true;
9942     }
9943   else
9944     need_lang_pop = false;
9945   /* Let the front end know that we are beginning a specialization.  */
9946   if (!begin_specialization ())
9947     {
9948       end_specialization ();
9949       cp_parser_skip_to_end_of_block_or_statement (parser);
9950       return;
9951     }
9952
9953   /* If the next keyword is `template', we need to figure out whether
9954      or not we're looking a template-declaration.  */
9955   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9956     {
9957       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9958           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9959         cp_parser_template_declaration_after_export (parser,
9960                                                      /*member_p=*/false);
9961       else
9962         cp_parser_explicit_specialization (parser);
9963     }
9964   else
9965     /* Parse the dependent declaration.  */
9966     cp_parser_single_declaration (parser,
9967                                   /*checks=*/NULL,
9968                                   /*member_p=*/false,
9969                                   /*friend_p=*/NULL);
9970   /* We're done with the specialization.  */
9971   end_specialization ();
9972   /* For the erroneous case of a template with C linkage, we pushed an
9973      implicit C++ linkage scope; exit that scope now.  */
9974   if (need_lang_pop)
9975     pop_lang_context ();
9976   /* We're done with this parameter list.  */
9977   --parser->num_template_parameter_lists;
9978 }
9979
9980 /* Parse a type-specifier.
9981
9982    type-specifier:
9983      simple-type-specifier
9984      class-specifier
9985      enum-specifier
9986      elaborated-type-specifier
9987      cv-qualifier
9988
9989    GNU Extension:
9990
9991    type-specifier:
9992      __complex__
9993
9994    Returns a representation of the type-specifier.  For a
9995    class-specifier, enum-specifier, or elaborated-type-specifier, a
9996    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9997
9998    The parser flags FLAGS is used to control type-specifier parsing.
9999
10000    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10001    in a decl-specifier-seq.
10002
10003    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10004    class-specifier, enum-specifier, or elaborated-type-specifier, then
10005    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10006    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10007    zero.
10008
10009    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10010    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10011    is set to FALSE.  */
10012
10013 static tree
10014 cp_parser_type_specifier (cp_parser* parser,
10015                           cp_parser_flags flags,
10016                           cp_decl_specifier_seq *decl_specs,
10017                           bool is_declaration,
10018                           int* declares_class_or_enum,
10019                           bool* is_cv_qualifier)
10020 {
10021   tree type_spec = NULL_TREE;
10022   cp_token *token;
10023   enum rid keyword;
10024   cp_decl_spec ds = ds_last;
10025
10026   /* Assume this type-specifier does not declare a new type.  */
10027   if (declares_class_or_enum)
10028     *declares_class_or_enum = 0;
10029   /* And that it does not specify a cv-qualifier.  */
10030   if (is_cv_qualifier)
10031     *is_cv_qualifier = false;
10032   /* Peek at the next token.  */
10033   token = cp_lexer_peek_token (parser->lexer);
10034
10035   /* If we're looking at a keyword, we can use that to guide the
10036      production we choose.  */
10037   keyword = token->keyword;
10038   switch (keyword)
10039     {
10040     case RID_ENUM:
10041       /* Look for the enum-specifier.  */
10042       type_spec = cp_parser_enum_specifier (parser);
10043       /* If that worked, we're done.  */
10044       if (type_spec)
10045         {
10046           if (declares_class_or_enum)
10047             *declares_class_or_enum = 2;
10048           if (decl_specs)
10049             cp_parser_set_decl_spec_type (decl_specs,
10050                                           type_spec,
10051                                           /*user_defined_p=*/true);
10052           return type_spec;
10053         }
10054       else
10055         goto elaborated_type_specifier;
10056
10057       /* Any of these indicate either a class-specifier, or an
10058          elaborated-type-specifier.  */
10059     case RID_CLASS:
10060     case RID_STRUCT:
10061     case RID_UNION:
10062       /* Parse tentatively so that we can back up if we don't find a
10063          class-specifier.  */
10064       cp_parser_parse_tentatively (parser);
10065       /* Look for the class-specifier.  */
10066       type_spec = cp_parser_class_specifier (parser);
10067       /* If that worked, we're done.  */
10068       if (cp_parser_parse_definitely (parser))
10069         {
10070           if (declares_class_or_enum)
10071             *declares_class_or_enum = 2;
10072           if (decl_specs)
10073             cp_parser_set_decl_spec_type (decl_specs,
10074                                           type_spec,
10075                                           /*user_defined_p=*/true);
10076           return type_spec;
10077         }
10078
10079       /* Fall through.  */
10080     elaborated_type_specifier:
10081       /* We're declaring (not defining) a class or enum.  */
10082       if (declares_class_or_enum)
10083         *declares_class_or_enum = 1;
10084
10085       /* Fall through.  */
10086     case RID_TYPENAME:
10087       /* Look for an elaborated-type-specifier.  */
10088       type_spec
10089         = (cp_parser_elaborated_type_specifier
10090            (parser,
10091             decl_specs && decl_specs->specs[(int) ds_friend],
10092             is_declaration));
10093       if (decl_specs)
10094         cp_parser_set_decl_spec_type (decl_specs,
10095                                       type_spec,
10096                                       /*user_defined_p=*/true);
10097       return type_spec;
10098
10099     case RID_CONST:
10100       ds = ds_const;
10101       if (is_cv_qualifier)
10102         *is_cv_qualifier = true;
10103       break;
10104
10105     case RID_VOLATILE:
10106       ds = ds_volatile;
10107       if (is_cv_qualifier)
10108         *is_cv_qualifier = true;
10109       break;
10110
10111     case RID_RESTRICT:
10112       ds = ds_restrict;
10113       if (is_cv_qualifier)
10114         *is_cv_qualifier = true;
10115       break;
10116
10117     case RID_COMPLEX:
10118       /* The `__complex__' keyword is a GNU extension.  */
10119       ds = ds_complex;
10120       break;
10121
10122     default:
10123       break;
10124     }
10125
10126   /* Handle simple keywords.  */
10127   if (ds != ds_last)
10128     {
10129       if (decl_specs)
10130         {
10131           ++decl_specs->specs[(int)ds];
10132           decl_specs->any_specifiers_p = true;
10133         }
10134       return cp_lexer_consume_token (parser->lexer)->u.value;
10135     }
10136
10137   /* If we do not already have a type-specifier, assume we are looking
10138      at a simple-type-specifier.  */
10139   type_spec = cp_parser_simple_type_specifier (parser,
10140                                                decl_specs,
10141                                                flags);
10142
10143   /* If we didn't find a type-specifier, and a type-specifier was not
10144      optional in this context, issue an error message.  */
10145   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10146     {
10147       cp_parser_error (parser, "expected type specifier");
10148       return error_mark_node;
10149     }
10150
10151   return type_spec;
10152 }
10153
10154 /* Parse a simple-type-specifier.
10155
10156    simple-type-specifier:
10157      :: [opt] nested-name-specifier [opt] type-name
10158      :: [opt] nested-name-specifier template template-id
10159      char
10160      wchar_t
10161      bool
10162      short
10163      int
10164      long
10165      signed
10166      unsigned
10167      float
10168      double
10169      void
10170
10171    GNU Extension:
10172
10173    simple-type-specifier:
10174      __typeof__ unary-expression
10175      __typeof__ ( type-id )
10176
10177    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10178    appropriately updated.  */
10179
10180 static tree
10181 cp_parser_simple_type_specifier (cp_parser* parser,
10182                                  cp_decl_specifier_seq *decl_specs,
10183                                  cp_parser_flags flags)
10184 {
10185   tree type = NULL_TREE;
10186   cp_token *token;
10187
10188   /* Peek at the next token.  */
10189   token = cp_lexer_peek_token (parser->lexer);
10190
10191   /* If we're looking at a keyword, things are easy.  */
10192   switch (token->keyword)
10193     {
10194     case RID_CHAR:
10195       if (decl_specs)
10196         decl_specs->explicit_char_p = true;
10197       type = char_type_node;
10198       break;
10199     case RID_WCHAR:
10200       type = wchar_type_node;
10201       break;
10202     case RID_BOOL:
10203       type = boolean_type_node;
10204       break;
10205     case RID_SHORT:
10206       if (decl_specs)
10207         ++decl_specs->specs[(int) ds_short];
10208       type = short_integer_type_node;
10209       break;
10210     case RID_INT:
10211       if (decl_specs)
10212         decl_specs->explicit_int_p = true;
10213       type = integer_type_node;
10214       break;
10215     case RID_LONG:
10216       if (decl_specs)
10217         ++decl_specs->specs[(int) ds_long];
10218       type = long_integer_type_node;
10219       break;
10220     case RID_SIGNED:
10221       if (decl_specs)
10222         ++decl_specs->specs[(int) ds_signed];
10223       type = integer_type_node;
10224       break;
10225     case RID_UNSIGNED:
10226       if (decl_specs)
10227         ++decl_specs->specs[(int) ds_unsigned];
10228       type = unsigned_type_node;
10229       break;
10230     case RID_FLOAT:
10231       type = float_type_node;
10232       break;
10233     case RID_DOUBLE:
10234       type = double_type_node;
10235       break;
10236     case RID_VOID:
10237       type = void_type_node;
10238       break;
10239
10240     case RID_TYPEOF:
10241       /* Consume the `typeof' token.  */
10242       cp_lexer_consume_token (parser->lexer);
10243       /* Parse the operand to `typeof'.  */
10244       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10245       /* If it is not already a TYPE, take its type.  */
10246       if (!TYPE_P (type))
10247         type = finish_typeof (type);
10248
10249       if (decl_specs)
10250         cp_parser_set_decl_spec_type (decl_specs, type,
10251                                       /*user_defined_p=*/true);
10252
10253       return type;
10254
10255     default:
10256       break;
10257     }
10258
10259   /* If the type-specifier was for a built-in type, we're done.  */
10260   if (type)
10261     {
10262       tree id;
10263
10264       /* Record the type.  */
10265       if (decl_specs
10266           && (token->keyword != RID_SIGNED
10267               && token->keyword != RID_UNSIGNED
10268               && token->keyword != RID_SHORT
10269               && token->keyword != RID_LONG))
10270         cp_parser_set_decl_spec_type (decl_specs,
10271                                       type,
10272                                       /*user_defined=*/false);
10273       if (decl_specs)
10274         decl_specs->any_specifiers_p = true;
10275
10276       /* Consume the token.  */
10277       id = cp_lexer_consume_token (parser->lexer)->u.value;
10278
10279       /* There is no valid C++ program where a non-template type is
10280          followed by a "<".  That usually indicates that the user thought
10281          that the type was a template.  */
10282       cp_parser_check_for_invalid_template_id (parser, type);
10283
10284       return TYPE_NAME (type);
10285     }
10286
10287   /* The type-specifier must be a user-defined type.  */
10288   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10289     {
10290       bool qualified_p;
10291       bool global_p;
10292
10293       /* Don't gobble tokens or issue error messages if this is an
10294          optional type-specifier.  */
10295       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10296         cp_parser_parse_tentatively (parser);
10297
10298       /* Look for the optional `::' operator.  */
10299       global_p
10300         = (cp_parser_global_scope_opt (parser,
10301                                        /*current_scope_valid_p=*/false)
10302            != NULL_TREE);
10303       /* Look for the nested-name specifier.  */
10304       qualified_p
10305         = (cp_parser_nested_name_specifier_opt (parser,
10306                                                 /*typename_keyword_p=*/false,
10307                                                 /*check_dependency_p=*/true,
10308                                                 /*type_p=*/false,
10309                                                 /*is_declaration=*/false)
10310            != NULL_TREE);
10311       /* If we have seen a nested-name-specifier, and the next token
10312          is `template', then we are using the template-id production.  */
10313       if (parser->scope
10314           && cp_parser_optional_template_keyword (parser))
10315         {
10316           /* Look for the template-id.  */
10317           type = cp_parser_template_id (parser,
10318                                         /*template_keyword_p=*/true,
10319                                         /*check_dependency_p=*/true,
10320                                         /*is_declaration=*/false);
10321           /* If the template-id did not name a type, we are out of
10322              luck.  */
10323           if (TREE_CODE (type) != TYPE_DECL)
10324             {
10325               cp_parser_error (parser, "expected template-id for type");
10326               type = NULL_TREE;
10327             }
10328         }
10329       /* Otherwise, look for a type-name.  */
10330       else
10331         type = cp_parser_type_name (parser);
10332       /* Keep track of all name-lookups performed in class scopes.  */
10333       if (type
10334           && !global_p
10335           && !qualified_p
10336           && TREE_CODE (type) == TYPE_DECL
10337           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10338         maybe_note_name_used_in_class (DECL_NAME (type), type);
10339       /* If it didn't work out, we don't have a TYPE.  */
10340       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10341           && !cp_parser_parse_definitely (parser))
10342         type = NULL_TREE;
10343       if (type && decl_specs)
10344         cp_parser_set_decl_spec_type (decl_specs, type,
10345                                       /*user_defined=*/true);
10346     }
10347
10348   /* If we didn't get a type-name, issue an error message.  */
10349   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10350     {
10351       cp_parser_error (parser, "expected type-name");
10352       return error_mark_node;
10353     }
10354
10355   /* There is no valid C++ program where a non-template type is
10356      followed by a "<".  That usually indicates that the user thought
10357      that the type was a template.  */
10358   if (type && type != error_mark_node)
10359     {
10360       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10361          If it is, then the '<'...'>' enclose protocol names rather than
10362          template arguments, and so everything is fine.  */
10363       if (c_dialect_objc ()
10364           && (objc_is_id (type) || objc_is_class_name (type)))
10365         {
10366           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10367           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10368
10369           /* Clobber the "unqualified" type previously entered into
10370              DECL_SPECS with the new, improved protocol-qualified version.  */
10371           if (decl_specs)
10372             decl_specs->type = qual_type;
10373
10374           return qual_type;
10375         }
10376
10377       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10378     }
10379
10380   return type;
10381 }
10382
10383 /* Parse a type-name.
10384
10385    type-name:
10386      class-name
10387      enum-name
10388      typedef-name
10389
10390    enum-name:
10391      identifier
10392
10393    typedef-name:
10394      identifier
10395
10396    Returns a TYPE_DECL for the type.  */
10397
10398 static tree
10399 cp_parser_type_name (cp_parser* parser)
10400 {
10401   tree type_decl;
10402   tree identifier;
10403
10404   /* We can't know yet whether it is a class-name or not.  */
10405   cp_parser_parse_tentatively (parser);
10406   /* Try a class-name.  */
10407   type_decl = cp_parser_class_name (parser,
10408                                     /*typename_keyword_p=*/false,
10409                                     /*template_keyword_p=*/false,
10410                                     none_type,
10411                                     /*check_dependency_p=*/true,
10412                                     /*class_head_p=*/false,
10413                                     /*is_declaration=*/false);
10414   /* If it's not a class-name, keep looking.  */
10415   if (!cp_parser_parse_definitely (parser))
10416     {
10417       /* It must be a typedef-name or an enum-name.  */
10418       identifier = cp_parser_identifier (parser);
10419       if (identifier == error_mark_node)
10420         return error_mark_node;
10421
10422       /* Look up the type-name.  */
10423       type_decl = cp_parser_lookup_name_simple (parser, identifier);
10424
10425       if (TREE_CODE (type_decl) != TYPE_DECL
10426           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10427         {
10428           /* See if this is an Objective-C type.  */
10429           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10430           tree type = objc_get_protocol_qualified_type (identifier, protos);
10431           if (type)
10432             type_decl = TYPE_NAME (type);
10433         }
10434
10435       /* Issue an error if we did not find a type-name.  */
10436       if (TREE_CODE (type_decl) != TYPE_DECL)
10437         {
10438           if (!cp_parser_simulate_error (parser))
10439             cp_parser_name_lookup_error (parser, identifier, type_decl,
10440                                          "is not a type");
10441           type_decl = error_mark_node;
10442         }
10443       /* Remember that the name was used in the definition of the
10444          current class so that we can check later to see if the
10445          meaning would have been different after the class was
10446          entirely defined.  */
10447       else if (type_decl != error_mark_node
10448                && !parser->scope)
10449         maybe_note_name_used_in_class (identifier, type_decl);
10450     }
10451
10452   return type_decl;
10453 }
10454
10455
10456 /* Parse an elaborated-type-specifier.  Note that the grammar given
10457    here incorporates the resolution to DR68.
10458
10459    elaborated-type-specifier:
10460      class-key :: [opt] nested-name-specifier [opt] identifier
10461      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10462      enum :: [opt] nested-name-specifier [opt] identifier
10463      typename :: [opt] nested-name-specifier identifier
10464      typename :: [opt] nested-name-specifier template [opt]
10465        template-id
10466
10467    GNU extension:
10468
10469    elaborated-type-specifier:
10470      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10471      class-key attributes :: [opt] nested-name-specifier [opt]
10472                template [opt] template-id
10473      enum attributes :: [opt] nested-name-specifier [opt] identifier
10474
10475    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10476    declared `friend'.  If IS_DECLARATION is TRUE, then this
10477    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10478    something is being declared.
10479
10480    Returns the TYPE specified.  */
10481
10482 static tree
10483 cp_parser_elaborated_type_specifier (cp_parser* parser,
10484                                      bool is_friend,
10485                                      bool is_declaration)
10486 {
10487   enum tag_types tag_type;
10488   tree identifier;
10489   tree type = NULL_TREE;
10490   tree attributes = NULL_TREE;
10491
10492   /* See if we're looking at the `enum' keyword.  */
10493   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10494     {
10495       /* Consume the `enum' token.  */
10496       cp_lexer_consume_token (parser->lexer);
10497       /* Remember that it's an enumeration type.  */
10498       tag_type = enum_type;
10499       /* Parse the attributes.  */
10500       attributes = cp_parser_attributes_opt (parser);
10501     }
10502   /* Or, it might be `typename'.  */
10503   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10504                                            RID_TYPENAME))
10505     {
10506       /* Consume the `typename' token.  */
10507       cp_lexer_consume_token (parser->lexer);
10508       /* Remember that it's a `typename' type.  */
10509       tag_type = typename_type;
10510       /* The `typename' keyword is only allowed in templates.  */
10511       if (!processing_template_decl)
10512         pedwarn ("using %<typename%> outside of template");
10513     }
10514   /* Otherwise it must be a class-key.  */
10515   else
10516     {
10517       tag_type = cp_parser_class_key (parser);
10518       if (tag_type == none_type)
10519         return error_mark_node;
10520       /* Parse the attributes.  */
10521       attributes = cp_parser_attributes_opt (parser);
10522     }
10523
10524   /* Look for the `::' operator.  */
10525   cp_parser_global_scope_opt (parser,
10526                               /*current_scope_valid_p=*/false);
10527   /* Look for the nested-name-specifier.  */
10528   if (tag_type == typename_type)
10529     {
10530       if (!cp_parser_nested_name_specifier (parser,
10531                                            /*typename_keyword_p=*/true,
10532                                            /*check_dependency_p=*/true,
10533                                            /*type_p=*/true,
10534                                             is_declaration))
10535         return error_mark_node;
10536     }
10537   else
10538     /* Even though `typename' is not present, the proposed resolution
10539        to Core Issue 180 says that in `class A<T>::B', `B' should be
10540        considered a type-name, even if `A<T>' is dependent.  */
10541     cp_parser_nested_name_specifier_opt (parser,
10542                                          /*typename_keyword_p=*/true,
10543                                          /*check_dependency_p=*/true,
10544                                          /*type_p=*/true,
10545                                          is_declaration);
10546  /* For everything but enumeration types, consider a template-id.
10547     For an enumeration type, consider only a plain identifier.  */
10548   if (tag_type != enum_type)
10549     {
10550       bool template_p = false;
10551       tree decl;
10552
10553       /* Allow the `template' keyword.  */
10554       template_p = cp_parser_optional_template_keyword (parser);
10555       /* If we didn't see `template', we don't know if there's a
10556          template-id or not.  */
10557       if (!template_p)
10558         cp_parser_parse_tentatively (parser);
10559       /* Parse the template-id.  */
10560       decl = cp_parser_template_id (parser, template_p,
10561                                     /*check_dependency_p=*/true,
10562                                     is_declaration);
10563       /* If we didn't find a template-id, look for an ordinary
10564          identifier.  */
10565       if (!template_p && !cp_parser_parse_definitely (parser))
10566         ;
10567       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10568          in effect, then we must assume that, upon instantiation, the
10569          template will correspond to a class.  */
10570       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10571                && tag_type == typename_type)
10572         type = make_typename_type (parser->scope, decl,
10573                                    typename_type,
10574                                    /*complain=*/tf_error);
10575       else
10576         type = TREE_TYPE (decl);
10577     }
10578
10579   if (!type)
10580     {
10581       identifier = cp_parser_identifier (parser);
10582
10583       if (identifier == error_mark_node)
10584         {
10585           parser->scope = NULL_TREE;
10586           return error_mark_node;
10587         }
10588
10589       /* For a `typename', we needn't call xref_tag.  */
10590       if (tag_type == typename_type
10591           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10592         return cp_parser_make_typename_type (parser, parser->scope,
10593                                              identifier);
10594       /* Look up a qualified name in the usual way.  */
10595       if (parser->scope)
10596         {
10597           tree decl;
10598
10599           decl = cp_parser_lookup_name (parser, identifier,
10600                                         tag_type,
10601                                         /*is_template=*/false,
10602                                         /*is_namespace=*/false,
10603                                         /*check_dependency=*/true,
10604                                         /*ambiguous_decls=*/NULL);
10605
10606           /* If we are parsing friend declaration, DECL may be a
10607              TEMPLATE_DECL tree node here.  However, we need to check
10608              whether this TEMPLATE_DECL results in valid code.  Consider
10609              the following example:
10610
10611                namespace N {
10612                  template <class T> class C {};
10613                }
10614                class X {
10615                  template <class T> friend class N::C; // #1, valid code
10616                };
10617                template <class T> class Y {
10618                  friend class N::C;                    // #2, invalid code
10619                };
10620
10621              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10622              name lookup of `N::C'.  We see that friend declaration must
10623              be template for the code to be valid.  Note that
10624              processing_template_decl does not work here since it is
10625              always 1 for the above two cases.  */
10626
10627           decl = (cp_parser_maybe_treat_template_as_class
10628                   (decl, /*tag_name_p=*/is_friend
10629                          && parser->num_template_parameter_lists));
10630
10631           if (TREE_CODE (decl) != TYPE_DECL)
10632             {
10633               cp_parser_diagnose_invalid_type_name (parser,
10634                                                     parser->scope,
10635                                                     identifier);
10636               return error_mark_node;
10637             }
10638
10639           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10640             {
10641               bool allow_template = (parser->num_template_parameter_lists
10642                                       || DECL_SELF_REFERENCE_P (decl));
10643               type = check_elaborated_type_specifier (tag_type, decl, 
10644                                                       allow_template);
10645
10646               if (type == error_mark_node)
10647                 return error_mark_node;
10648             }
10649
10650           type = TREE_TYPE (decl);
10651         }
10652       else
10653         {
10654           /* An elaborated-type-specifier sometimes introduces a new type and
10655              sometimes names an existing type.  Normally, the rule is that it
10656              introduces a new type only if there is not an existing type of
10657              the same name already in scope.  For example, given:
10658
10659                struct S {};
10660                void f() { struct S s; }
10661
10662              the `struct S' in the body of `f' is the same `struct S' as in
10663              the global scope; the existing definition is used.  However, if
10664              there were no global declaration, this would introduce a new
10665              local class named `S'.
10666
10667              An exception to this rule applies to the following code:
10668
10669                namespace N { struct S; }
10670
10671              Here, the elaborated-type-specifier names a new type
10672              unconditionally; even if there is already an `S' in the
10673              containing scope this declaration names a new type.
10674              This exception only applies if the elaborated-type-specifier
10675              forms the complete declaration:
10676
10677                [class.name]
10678
10679                A declaration consisting solely of `class-key identifier ;' is
10680                either a redeclaration of the name in the current scope or a
10681                forward declaration of the identifier as a class name.  It
10682                introduces the name into the current scope.
10683
10684              We are in this situation precisely when the next token is a `;'.
10685
10686              An exception to the exception is that a `friend' declaration does
10687              *not* name a new type; i.e., given:
10688
10689                struct S { friend struct T; };
10690
10691              `T' is not a new type in the scope of `S'.
10692
10693              Also, `new struct S' or `sizeof (struct S)' never results in the
10694              definition of a new type; a new type can only be declared in a
10695              declaration context.  */
10696
10697           tag_scope ts;
10698           bool template_p;
10699
10700           if (is_friend)
10701             /* Friends have special name lookup rules.  */
10702             ts = ts_within_enclosing_non_class;
10703           else if (is_declaration
10704                    && cp_lexer_next_token_is (parser->lexer,
10705                                               CPP_SEMICOLON))
10706             /* This is a `class-key identifier ;' */
10707             ts = ts_current;
10708           else
10709             ts = ts_global;
10710
10711           template_p =
10712             (parser->num_template_parameter_lists
10713              && (cp_parser_next_token_starts_class_definition_p (parser)
10714                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10715           /* An unqualified name was used to reference this type, so
10716              there were no qualifying templates.  */
10717           if (!cp_parser_check_template_parameters (parser,
10718                                                     /*num_templates=*/0))
10719             return error_mark_node;
10720           type = xref_tag (tag_type, identifier, ts, template_p);
10721         }
10722     }
10723
10724   if (type == error_mark_node)
10725     return error_mark_node;
10726
10727   /* Allow attributes on forward declarations of classes.  */
10728   if (attributes)
10729     {
10730       if (TREE_CODE (type) == TYPENAME_TYPE)
10731         warning (OPT_Wattributes,
10732                  "attributes ignored on uninstantiated type");
10733       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10734                && ! processing_explicit_instantiation)
10735         warning (OPT_Wattributes,
10736                  "attributes ignored on template instantiation");
10737       else if (is_declaration && cp_parser_declares_only_class_p (parser))
10738         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10739       else
10740         warning (OPT_Wattributes,
10741                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10742     }
10743
10744   if (tag_type != enum_type)
10745     cp_parser_check_class_key (tag_type, type);
10746
10747   /* A "<" cannot follow an elaborated type specifier.  If that
10748      happens, the user was probably trying to form a template-id.  */
10749   cp_parser_check_for_invalid_template_id (parser, type);
10750
10751   return type;
10752 }
10753
10754 /* Parse an enum-specifier.
10755
10756    enum-specifier:
10757      enum identifier [opt] { enumerator-list [opt] }
10758
10759    GNU Extensions:
10760      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10761        attributes[opt]
10762
10763    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10764    if the token stream isn't an enum-specifier after all.  */
10765
10766 static tree
10767 cp_parser_enum_specifier (cp_parser* parser)
10768 {
10769   tree identifier;
10770   tree type;
10771   tree attributes;
10772
10773   /* Parse tentatively so that we can back up if we don't find a
10774      enum-specifier.  */
10775   cp_parser_parse_tentatively (parser);
10776
10777   /* Caller guarantees that the current token is 'enum', an identifier
10778      possibly follows, and the token after that is an opening brace.
10779      If we don't have an identifier, fabricate an anonymous name for
10780      the enumeration being defined.  */
10781   cp_lexer_consume_token (parser->lexer);
10782
10783   attributes = cp_parser_attributes_opt (parser);
10784
10785   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10786     identifier = cp_parser_identifier (parser);
10787   else
10788     identifier = make_anon_name ();
10789
10790   /* Look for the `{' but don't consume it yet.  */
10791   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10792     cp_parser_simulate_error (parser);
10793
10794   if (!cp_parser_parse_definitely (parser))
10795     return NULL_TREE;
10796
10797   /* Issue an error message if type-definitions are forbidden here.  */
10798   if (!cp_parser_check_type_definition (parser))
10799     type = error_mark_node;
10800   else
10801     /* Create the new type.  We do this before consuming the opening
10802        brace so the enum will be recorded as being on the line of its
10803        tag (or the 'enum' keyword, if there is no tag).  */
10804     type = start_enum (identifier);
10805   
10806   /* Consume the opening brace.  */
10807   cp_lexer_consume_token (parser->lexer);
10808
10809   if (type == error_mark_node)
10810     {
10811       cp_parser_skip_to_end_of_block_or_statement (parser);
10812       return error_mark_node;
10813     }
10814
10815   /* If the next token is not '}', then there are some enumerators.  */
10816   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10817     cp_parser_enumerator_list (parser, type);
10818
10819   /* Consume the final '}'.  */
10820   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10821
10822   /* Look for trailing attributes to apply to this enumeration, and
10823      apply them if appropriate.  */
10824   if (cp_parser_allow_gnu_extensions_p (parser))
10825     {
10826       tree trailing_attr = cp_parser_attributes_opt (parser);
10827       cplus_decl_attributes (&type,
10828                              trailing_attr,
10829                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10830     }
10831
10832   /* Finish up the enumeration.  */
10833   finish_enum (type);
10834
10835   return type;
10836 }
10837
10838 /* Parse an enumerator-list.  The enumerators all have the indicated
10839    TYPE.
10840
10841    enumerator-list:
10842      enumerator-definition
10843      enumerator-list , enumerator-definition  */
10844
10845 static void
10846 cp_parser_enumerator_list (cp_parser* parser, tree type)
10847 {
10848   while (true)
10849     {
10850       /* Parse an enumerator-definition.  */
10851       cp_parser_enumerator_definition (parser, type);
10852
10853       /* If the next token is not a ',', we've reached the end of
10854          the list.  */
10855       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10856         break;
10857       /* Otherwise, consume the `,' and keep going.  */
10858       cp_lexer_consume_token (parser->lexer);
10859       /* If the next token is a `}', there is a trailing comma.  */
10860       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10861         {
10862           if (pedantic && !in_system_header)
10863             pedwarn ("comma at end of enumerator list");
10864           break;
10865         }
10866     }
10867 }
10868
10869 /* Parse an enumerator-definition.  The enumerator has the indicated
10870    TYPE.
10871
10872    enumerator-definition:
10873      enumerator
10874      enumerator = constant-expression
10875
10876    enumerator:
10877      identifier  */
10878
10879 static void
10880 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10881 {
10882   tree identifier;
10883   tree value;
10884
10885   /* Look for the identifier.  */
10886   identifier = cp_parser_identifier (parser);
10887   if (identifier == error_mark_node)
10888     return;
10889
10890   /* If the next token is an '=', then there is an explicit value.  */
10891   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10892     {
10893       /* Consume the `=' token.  */
10894       cp_lexer_consume_token (parser->lexer);
10895       /* Parse the value.  */
10896       value = cp_parser_constant_expression (parser,
10897                                              /*allow_non_constant_p=*/false,
10898                                              NULL);
10899     }
10900   else
10901     value = NULL_TREE;
10902
10903   /* Create the enumerator.  */
10904   build_enumerator (identifier, value, type);
10905 }
10906
10907 /* Parse a namespace-name.
10908
10909    namespace-name:
10910      original-namespace-name
10911      namespace-alias
10912
10913    Returns the NAMESPACE_DECL for the namespace.  */
10914
10915 static tree
10916 cp_parser_namespace_name (cp_parser* parser)
10917 {
10918   tree identifier;
10919   tree namespace_decl;
10920
10921   /* Get the name of the namespace.  */
10922   identifier = cp_parser_identifier (parser);
10923   if (identifier == error_mark_node)
10924     return error_mark_node;
10925
10926   /* Look up the identifier in the currently active scope.  Look only
10927      for namespaces, due to:
10928
10929        [basic.lookup.udir]
10930
10931        When looking up a namespace-name in a using-directive or alias
10932        definition, only namespace names are considered.
10933
10934      And:
10935
10936        [basic.lookup.qual]
10937
10938        During the lookup of a name preceding the :: scope resolution
10939        operator, object, function, and enumerator names are ignored.
10940
10941      (Note that cp_parser_class_or_namespace_name only calls this
10942      function if the token after the name is the scope resolution
10943      operator.)  */
10944   namespace_decl = cp_parser_lookup_name (parser, identifier,
10945                                           none_type,
10946                                           /*is_template=*/false,
10947                                           /*is_namespace=*/true,
10948                                           /*check_dependency=*/true,
10949                                           /*ambiguous_decls=*/NULL);
10950   /* If it's not a namespace, issue an error.  */
10951   if (namespace_decl == error_mark_node
10952       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10953     {
10954       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10955         error ("%qD is not a namespace-name", identifier);
10956       cp_parser_error (parser, "expected namespace-name");
10957       namespace_decl = error_mark_node;
10958     }
10959
10960   return namespace_decl;
10961 }
10962
10963 /* Parse a namespace-definition.
10964
10965    namespace-definition:
10966      named-namespace-definition
10967      unnamed-namespace-definition
10968
10969    named-namespace-definition:
10970      original-namespace-definition
10971      extension-namespace-definition
10972
10973    original-namespace-definition:
10974      namespace identifier { namespace-body }
10975
10976    extension-namespace-definition:
10977      namespace original-namespace-name { namespace-body }
10978
10979    unnamed-namespace-definition:
10980      namespace { namespace-body } */
10981
10982 static void
10983 cp_parser_namespace_definition (cp_parser* parser)
10984 {
10985   tree identifier, attribs;
10986
10987   /* Look for the `namespace' keyword.  */
10988   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10989
10990   /* Get the name of the namespace.  We do not attempt to distinguish
10991      between an original-namespace-definition and an
10992      extension-namespace-definition at this point.  The semantic
10993      analysis routines are responsible for that.  */
10994   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10995     identifier = cp_parser_identifier (parser);
10996   else
10997     identifier = NULL_TREE;
10998
10999   /* Parse any specified attributes.  */
11000   attribs = cp_parser_attributes_opt (parser);
11001
11002   /* Look for the `{' to start the namespace.  */
11003   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11004   /* Start the namespace.  */
11005   push_namespace_with_attribs (identifier, attribs);
11006   /* Parse the body of the namespace.  */
11007   cp_parser_namespace_body (parser);
11008   /* Finish the namespace.  */
11009   pop_namespace ();
11010   /* Look for the final `}'.  */
11011   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11012 }
11013
11014 /* Parse a namespace-body.
11015
11016    namespace-body:
11017      declaration-seq [opt]  */
11018
11019 static void
11020 cp_parser_namespace_body (cp_parser* parser)
11021 {
11022   cp_parser_declaration_seq_opt (parser);
11023 }
11024
11025 /* Parse a namespace-alias-definition.
11026
11027    namespace-alias-definition:
11028      namespace identifier = qualified-namespace-specifier ;  */
11029
11030 static void
11031 cp_parser_namespace_alias_definition (cp_parser* parser)
11032 {
11033   tree identifier;
11034   tree namespace_specifier;
11035
11036   /* Look for the `namespace' keyword.  */
11037   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11038   /* Look for the identifier.  */
11039   identifier = cp_parser_identifier (parser);
11040   if (identifier == error_mark_node)
11041     return;
11042   /* Look for the `=' token.  */
11043   cp_parser_require (parser, CPP_EQ, "`='");
11044   /* Look for the qualified-namespace-specifier.  */
11045   namespace_specifier
11046     = cp_parser_qualified_namespace_specifier (parser);
11047   /* Look for the `;' token.  */
11048   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11049
11050   /* Register the alias in the symbol table.  */
11051   do_namespace_alias (identifier, namespace_specifier);
11052 }
11053
11054 /* Parse a qualified-namespace-specifier.
11055
11056    qualified-namespace-specifier:
11057      :: [opt] nested-name-specifier [opt] namespace-name
11058
11059    Returns a NAMESPACE_DECL corresponding to the specified
11060    namespace.  */
11061
11062 static tree
11063 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11064 {
11065   /* Look for the optional `::'.  */
11066   cp_parser_global_scope_opt (parser,
11067                               /*current_scope_valid_p=*/false);
11068
11069   /* Look for the optional nested-name-specifier.  */
11070   cp_parser_nested_name_specifier_opt (parser,
11071                                        /*typename_keyword_p=*/false,
11072                                        /*check_dependency_p=*/true,
11073                                        /*type_p=*/false,
11074                                        /*is_declaration=*/true);
11075
11076   return cp_parser_namespace_name (parser);
11077 }
11078
11079 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11080    access declaration.
11081
11082    using-declaration:
11083      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11084      using :: unqualified-id ;  
11085
11086    access-declaration:
11087      qualified-id ;  
11088
11089    */
11090
11091 static bool
11092 cp_parser_using_declaration (cp_parser* parser, 
11093                              bool access_declaration_p)
11094 {
11095   cp_token *token;
11096   bool typename_p = false;
11097   bool global_scope_p;
11098   tree decl;
11099   tree identifier;
11100   tree qscope;
11101
11102   if (access_declaration_p)
11103     cp_parser_parse_tentatively (parser);
11104   else
11105     {
11106       /* Look for the `using' keyword.  */
11107       cp_parser_require_keyword (parser, RID_USING, "`using'");
11108       
11109       /* Peek at the next token.  */
11110       token = cp_lexer_peek_token (parser->lexer);
11111       /* See if it's `typename'.  */
11112       if (token->keyword == RID_TYPENAME)
11113         {
11114           /* Remember that we've seen it.  */
11115           typename_p = true;
11116           /* Consume the `typename' token.  */
11117           cp_lexer_consume_token (parser->lexer);
11118         }
11119     }
11120
11121   /* Look for the optional global scope qualification.  */
11122   global_scope_p
11123     = (cp_parser_global_scope_opt (parser,
11124                                    /*current_scope_valid_p=*/false)
11125        != NULL_TREE);
11126
11127   /* If we saw `typename', or didn't see `::', then there must be a
11128      nested-name-specifier present.  */
11129   if (typename_p || !global_scope_p)
11130     qscope = cp_parser_nested_name_specifier (parser, typename_p,
11131                                               /*check_dependency_p=*/true,
11132                                               /*type_p=*/false,
11133                                               /*is_declaration=*/true);
11134   /* Otherwise, we could be in either of the two productions.  In that
11135      case, treat the nested-name-specifier as optional.  */
11136   else
11137     qscope = cp_parser_nested_name_specifier_opt (parser,
11138                                                   /*typename_keyword_p=*/false,
11139                                                   /*check_dependency_p=*/true,
11140                                                   /*type_p=*/false,
11141                                                   /*is_declaration=*/true);
11142   if (!qscope)
11143     qscope = global_namespace;
11144
11145   if (access_declaration_p && cp_parser_error_occurred (parser))
11146     /* Something has already gone wrong; there's no need to parse
11147        further.  Since an error has occurred, the return value of
11148        cp_parser_parse_definitely will be false, as required.  */
11149     return cp_parser_parse_definitely (parser);
11150
11151   /* Parse the unqualified-id.  */
11152   identifier = cp_parser_unqualified_id (parser,
11153                                          /*template_keyword_p=*/false,
11154                                          /*check_dependency_p=*/true,
11155                                          /*declarator_p=*/true,
11156                                          /*optional_p=*/false);
11157
11158   if (access_declaration_p)
11159     {
11160       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11161         cp_parser_simulate_error (parser);
11162       if (!cp_parser_parse_definitely (parser))
11163         return false;
11164     }
11165
11166   /* The function we call to handle a using-declaration is different
11167      depending on what scope we are in.  */
11168   if (qscope == error_mark_node || identifier == error_mark_node)
11169     ;
11170   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11171            && TREE_CODE (identifier) != BIT_NOT_EXPR)
11172     /* [namespace.udecl]
11173
11174        A using declaration shall not name a template-id.  */
11175     error ("a template-id may not appear in a using-declaration");
11176   else
11177     {
11178       if (at_class_scope_p ())
11179         {
11180           /* Create the USING_DECL.  */
11181           decl = do_class_using_decl (parser->scope, identifier);
11182           /* Add it to the list of members in this class.  */
11183           finish_member_declaration (decl);
11184         }
11185       else
11186         {
11187           decl = cp_parser_lookup_name_simple (parser, identifier);
11188           if (decl == error_mark_node)
11189             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11190           else if (!at_namespace_scope_p ())
11191             do_local_using_decl (decl, qscope, identifier);
11192           else
11193             do_toplevel_using_decl (decl, qscope, identifier);
11194         }
11195     }
11196
11197   /* Look for the final `;'.  */
11198   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11199   
11200   return true;
11201 }
11202
11203 /* Parse a using-directive.
11204
11205    using-directive:
11206      using namespace :: [opt] nested-name-specifier [opt]
11207        namespace-name ;  */
11208
11209 static void
11210 cp_parser_using_directive (cp_parser* parser)
11211 {
11212   tree namespace_decl;
11213   tree attribs;
11214
11215   /* Look for the `using' keyword.  */
11216   cp_parser_require_keyword (parser, RID_USING, "`using'");
11217   /* And the `namespace' keyword.  */
11218   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11219   /* Look for the optional `::' operator.  */
11220   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11221   /* And the optional nested-name-specifier.  */
11222   cp_parser_nested_name_specifier_opt (parser,
11223                                        /*typename_keyword_p=*/false,
11224                                        /*check_dependency_p=*/true,
11225                                        /*type_p=*/false,
11226                                        /*is_declaration=*/true);
11227   /* Get the namespace being used.  */
11228   namespace_decl = cp_parser_namespace_name (parser);
11229   /* And any specified attributes.  */
11230   attribs = cp_parser_attributes_opt (parser);
11231   /* Update the symbol table.  */
11232   parse_using_directive (namespace_decl, attribs);
11233   /* Look for the final `;'.  */
11234   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11235 }
11236
11237 /* Parse an asm-definition.
11238
11239    asm-definition:
11240      asm ( string-literal ) ;
11241
11242    GNU Extension:
11243
11244    asm-definition:
11245      asm volatile [opt] ( string-literal ) ;
11246      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11247      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11248                           : asm-operand-list [opt] ) ;
11249      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11250                           : asm-operand-list [opt]
11251                           : asm-operand-list [opt] ) ;  */
11252
11253 static void
11254 cp_parser_asm_definition (cp_parser* parser)
11255 {
11256   tree string;
11257   tree outputs = NULL_TREE;
11258   tree inputs = NULL_TREE;
11259   tree clobbers = NULL_TREE;
11260   tree asm_stmt;
11261   bool volatile_p = false;
11262   bool extended_p = false;
11263
11264   /* Look for the `asm' keyword.  */
11265   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11266   /* See if the next token is `volatile'.  */
11267   if (cp_parser_allow_gnu_extensions_p (parser)
11268       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11269     {
11270       /* Remember that we saw the `volatile' keyword.  */
11271       volatile_p = true;
11272       /* Consume the token.  */
11273       cp_lexer_consume_token (parser->lexer);
11274     }
11275   /* Look for the opening `('.  */
11276   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11277     return;
11278   /* Look for the string.  */
11279   string = cp_parser_string_literal (parser, false, false);
11280   if (string == error_mark_node)
11281     {
11282       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11283                                              /*consume_paren=*/true);
11284       return;
11285     }
11286
11287   /* If we're allowing GNU extensions, check for the extended assembly
11288      syntax.  Unfortunately, the `:' tokens need not be separated by
11289      a space in C, and so, for compatibility, we tolerate that here
11290      too.  Doing that means that we have to treat the `::' operator as
11291      two `:' tokens.  */
11292   if (cp_parser_allow_gnu_extensions_p (parser)
11293       && parser->in_function_body
11294       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11295           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11296     {
11297       bool inputs_p = false;
11298       bool clobbers_p = false;
11299
11300       /* The extended syntax was used.  */
11301       extended_p = true;
11302
11303       /* Look for outputs.  */
11304       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11305         {
11306           /* Consume the `:'.  */
11307           cp_lexer_consume_token (parser->lexer);
11308           /* Parse the output-operands.  */
11309           if (cp_lexer_next_token_is_not (parser->lexer,
11310                                           CPP_COLON)
11311               && cp_lexer_next_token_is_not (parser->lexer,
11312                                              CPP_SCOPE)
11313               && cp_lexer_next_token_is_not (parser->lexer,
11314                                              CPP_CLOSE_PAREN))
11315             outputs = cp_parser_asm_operand_list (parser);
11316         }
11317       /* If the next token is `::', there are no outputs, and the
11318          next token is the beginning of the inputs.  */
11319       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11320         /* The inputs are coming next.  */
11321         inputs_p = true;
11322
11323       /* Look for inputs.  */
11324       if (inputs_p
11325           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11326         {
11327           /* Consume the `:' or `::'.  */
11328           cp_lexer_consume_token (parser->lexer);
11329           /* Parse the output-operands.  */
11330           if (cp_lexer_next_token_is_not (parser->lexer,
11331                                           CPP_COLON)
11332               && cp_lexer_next_token_is_not (parser->lexer,
11333                                              CPP_CLOSE_PAREN))
11334             inputs = cp_parser_asm_operand_list (parser);
11335         }
11336       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11337         /* The clobbers are coming next.  */
11338         clobbers_p = true;
11339
11340       /* Look for clobbers.  */
11341       if (clobbers_p
11342           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11343         {
11344           /* Consume the `:' or `::'.  */
11345           cp_lexer_consume_token (parser->lexer);
11346           /* Parse the clobbers.  */
11347           if (cp_lexer_next_token_is_not (parser->lexer,
11348                                           CPP_CLOSE_PAREN))
11349             clobbers = cp_parser_asm_clobber_list (parser);
11350         }
11351     }
11352   /* Look for the closing `)'.  */
11353   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11354     cp_parser_skip_to_closing_parenthesis (parser, true, false,
11355                                            /*consume_paren=*/true);
11356   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11357
11358   /* Create the ASM_EXPR.  */
11359   if (parser->in_function_body)
11360     {
11361       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11362                                   inputs, clobbers);
11363       /* If the extended syntax was not used, mark the ASM_EXPR.  */
11364       if (!extended_p)
11365         {
11366           tree temp = asm_stmt;
11367           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11368             temp = TREE_OPERAND (temp, 0);
11369
11370           ASM_INPUT_P (temp) = 1;
11371         }
11372     }
11373   else
11374     cgraph_add_asm_node (string);
11375 }
11376
11377 /* Declarators [gram.dcl.decl] */
11378
11379 /* Parse an init-declarator.
11380
11381    init-declarator:
11382      declarator initializer [opt]
11383
11384    GNU Extension:
11385
11386    init-declarator:
11387      declarator asm-specification [opt] attributes [opt] initializer [opt]
11388
11389    function-definition:
11390      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11391        function-body
11392      decl-specifier-seq [opt] declarator function-try-block
11393
11394    GNU Extension:
11395
11396    function-definition:
11397      __extension__ function-definition
11398
11399    The DECL_SPECIFIERS apply to this declarator.  Returns a
11400    representation of the entity declared.  If MEMBER_P is TRUE, then
11401    this declarator appears in a class scope.  The new DECL created by
11402    this declarator is returned.
11403
11404    The CHECKS are access checks that should be performed once we know
11405    what entity is being declared (and, therefore, what classes have
11406    befriended it).
11407
11408    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11409    for a function-definition here as well.  If the declarator is a
11410    declarator for a function-definition, *FUNCTION_DEFINITION_P will
11411    be TRUE upon return.  By that point, the function-definition will
11412    have been completely parsed.
11413
11414    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11415    is FALSE.  */
11416
11417 static tree
11418 cp_parser_init_declarator (cp_parser* parser,
11419                            cp_decl_specifier_seq *decl_specifiers,
11420                            VEC (deferred_access_check,gc)* checks,
11421                            bool function_definition_allowed_p,
11422                            bool member_p,
11423                            int declares_class_or_enum,
11424                            bool* function_definition_p)
11425 {
11426   cp_token *token;
11427   cp_declarator *declarator;
11428   tree prefix_attributes;
11429   tree attributes;
11430   tree asm_specification;
11431   tree initializer;
11432   tree decl = NULL_TREE;
11433   tree scope;
11434   bool is_initialized;
11435   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
11436      initialized with "= ..", CPP_OPEN_PAREN if initialized with
11437      "(...)".  */
11438   enum cpp_ttype initialization_kind;
11439   bool is_parenthesized_init = false;
11440   bool is_non_constant_init;
11441   int ctor_dtor_or_conv_p;
11442   bool friend_p;
11443   tree pushed_scope = NULL;
11444
11445   /* Gather the attributes that were provided with the
11446      decl-specifiers.  */
11447   prefix_attributes = decl_specifiers->attributes;
11448
11449   /* Assume that this is not the declarator for a function
11450      definition.  */
11451   if (function_definition_p)
11452     *function_definition_p = false;
11453
11454   /* Defer access checks while parsing the declarator; we cannot know
11455      what names are accessible until we know what is being
11456      declared.  */
11457   resume_deferring_access_checks ();
11458
11459   /* Parse the declarator.  */
11460   declarator
11461     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11462                             &ctor_dtor_or_conv_p,
11463                             /*parenthesized_p=*/NULL,
11464                             /*member_p=*/false);
11465   /* Gather up the deferred checks.  */
11466   stop_deferring_access_checks ();
11467
11468   /* If the DECLARATOR was erroneous, there's no need to go
11469      further.  */
11470   if (declarator == cp_error_declarator)
11471     return error_mark_node;
11472
11473   /* Check that the number of template-parameter-lists is OK.  */
11474   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11475     return error_mark_node;
11476
11477   if (declares_class_or_enum & 2)
11478     cp_parser_check_for_definition_in_return_type (declarator,
11479                                                    decl_specifiers->type);
11480
11481   /* Figure out what scope the entity declared by the DECLARATOR is
11482      located in.  `grokdeclarator' sometimes changes the scope, so
11483      we compute it now.  */
11484   scope = get_scope_of_declarator (declarator);
11485
11486   /* If we're allowing GNU extensions, look for an asm-specification
11487      and attributes.  */
11488   if (cp_parser_allow_gnu_extensions_p (parser))
11489     {
11490       /* Look for an asm-specification.  */
11491       asm_specification = cp_parser_asm_specification_opt (parser);
11492       /* And attributes.  */
11493       attributes = cp_parser_attributes_opt (parser);
11494     }
11495   else
11496     {
11497       asm_specification = NULL_TREE;
11498       attributes = NULL_TREE;
11499     }
11500
11501   /* Peek at the next token.  */
11502   token = cp_lexer_peek_token (parser->lexer);
11503   /* Check to see if the token indicates the start of a
11504      function-definition.  */
11505   if (cp_parser_token_starts_function_definition_p (token))
11506     {
11507       if (!function_definition_allowed_p)
11508         {
11509           /* If a function-definition should not appear here, issue an
11510              error message.  */
11511           cp_parser_error (parser,
11512                            "a function-definition is not allowed here");
11513           return error_mark_node;
11514         }
11515       else
11516         {
11517           /* Neither attributes nor an asm-specification are allowed
11518              on a function-definition.  */
11519           if (asm_specification)
11520             error ("an asm-specification is not allowed on a function-definition");
11521           if (attributes)
11522             error ("attributes are not allowed on a function-definition");
11523           /* This is a function-definition.  */
11524           *function_definition_p = true;
11525
11526           /* Parse the function definition.  */
11527           if (member_p)
11528             decl = cp_parser_save_member_function_body (parser,
11529                                                         decl_specifiers,
11530                                                         declarator,
11531                                                         prefix_attributes);
11532           else
11533             decl
11534               = (cp_parser_function_definition_from_specifiers_and_declarator
11535                  (parser, decl_specifiers, prefix_attributes, declarator));
11536
11537           return decl;
11538         }
11539     }
11540
11541   /* [dcl.dcl]
11542
11543      Only in function declarations for constructors, destructors, and
11544      type conversions can the decl-specifier-seq be omitted.
11545
11546      We explicitly postpone this check past the point where we handle
11547      function-definitions because we tolerate function-definitions
11548      that are missing their return types in some modes.  */
11549   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11550     {
11551       cp_parser_error (parser,
11552                        "expected constructor, destructor, or type conversion");
11553       return error_mark_node;
11554     }
11555
11556   /* An `=' or an `(' indicates an initializer.  */
11557   if (token->type == CPP_EQ
11558       || token->type == CPP_OPEN_PAREN)
11559     {
11560       is_initialized = true;
11561       initialization_kind = token->type;
11562     }
11563   else
11564     {
11565       /* If the init-declarator isn't initialized and isn't followed by a
11566          `,' or `;', it's not a valid init-declarator.  */
11567       if (token->type != CPP_COMMA
11568           && token->type != CPP_SEMICOLON)
11569         {
11570           cp_parser_error (parser, "expected initializer");
11571           return error_mark_node;
11572         }
11573       is_initialized = false;
11574       initialization_kind = CPP_EOF;
11575     }
11576
11577   /* Because start_decl has side-effects, we should only call it if we
11578      know we're going ahead.  By this point, we know that we cannot
11579      possibly be looking at any other construct.  */
11580   cp_parser_commit_to_tentative_parse (parser);
11581
11582   /* If the decl specifiers were bad, issue an error now that we're
11583      sure this was intended to be a declarator.  Then continue
11584      declaring the variable(s), as int, to try to cut down on further
11585      errors.  */
11586   if (decl_specifiers->any_specifiers_p
11587       && decl_specifiers->type == error_mark_node)
11588     {
11589       cp_parser_error (parser, "invalid type in declaration");
11590       decl_specifiers->type = integer_type_node;
11591     }
11592
11593   /* Check to see whether or not this declaration is a friend.  */
11594   friend_p = cp_parser_friend_p (decl_specifiers);
11595
11596   /* Enter the newly declared entry in the symbol table.  If we're
11597      processing a declaration in a class-specifier, we wait until
11598      after processing the initializer.  */
11599   if (!member_p)
11600     {
11601       if (parser->in_unbraced_linkage_specification_p)
11602         decl_specifiers->storage_class = sc_extern;
11603       decl = start_decl (declarator, decl_specifiers,
11604                          is_initialized, attributes, prefix_attributes,
11605                          &pushed_scope);
11606     }
11607   else if (scope)
11608     /* Enter the SCOPE.  That way unqualified names appearing in the
11609        initializer will be looked up in SCOPE.  */
11610     pushed_scope = push_scope (scope);
11611
11612   /* Perform deferred access control checks, now that we know in which
11613      SCOPE the declared entity resides.  */
11614   if (!member_p && decl)
11615     {
11616       tree saved_current_function_decl = NULL_TREE;
11617
11618       /* If the entity being declared is a function, pretend that we
11619          are in its scope.  If it is a `friend', it may have access to
11620          things that would not otherwise be accessible.  */
11621       if (TREE_CODE (decl) == FUNCTION_DECL)
11622         {
11623           saved_current_function_decl = current_function_decl;
11624           current_function_decl = decl;
11625         }
11626
11627       /* Perform access checks for template parameters.  */
11628       cp_parser_perform_template_parameter_access_checks (checks);
11629
11630       /* Perform the access control checks for the declarator and the
11631          the decl-specifiers.  */
11632       perform_deferred_access_checks ();
11633
11634       /* Restore the saved value.  */
11635       if (TREE_CODE (decl) == FUNCTION_DECL)
11636         current_function_decl = saved_current_function_decl;
11637     }
11638
11639   /* Parse the initializer.  */
11640   initializer = NULL_TREE;
11641   is_parenthesized_init = false;
11642   is_non_constant_init = true;
11643   if (is_initialized)
11644     {
11645       if (function_declarator_p (declarator))
11646         {
11647            if (initialization_kind == CPP_EQ)
11648              initializer = cp_parser_pure_specifier (parser);
11649            else
11650              {
11651                /* If the declaration was erroneous, we don't really
11652                   know what the user intended, so just silently
11653                   consume the initializer.  */
11654                if (decl != error_mark_node)
11655                  error ("initializer provided for function");
11656                cp_parser_skip_to_closing_parenthesis (parser,
11657                                                       /*recovering=*/true,
11658                                                       /*or_comma=*/false,
11659                                                       /*consume_paren=*/true);
11660              }
11661         }
11662       else
11663         initializer = cp_parser_initializer (parser,
11664                                              &is_parenthesized_init,
11665                                              &is_non_constant_init);
11666     }
11667
11668   /* The old parser allows attributes to appear after a parenthesized
11669      initializer.  Mark Mitchell proposed removing this functionality
11670      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11671      attributes -- but ignores them.  */
11672   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11673     if (cp_parser_attributes_opt (parser))
11674       warning (OPT_Wattributes,
11675                "attributes after parenthesized initializer ignored");
11676
11677   /* For an in-class declaration, use `grokfield' to create the
11678      declaration.  */
11679   if (member_p)
11680     {
11681       if (pushed_scope)
11682         {
11683           pop_scope (pushed_scope);
11684           pushed_scope = false;
11685         }
11686       decl = grokfield (declarator, decl_specifiers,
11687                         initializer, !is_non_constant_init,
11688                         /*asmspec=*/NULL_TREE,
11689                         prefix_attributes);
11690       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11691         cp_parser_save_default_args (parser, decl);
11692     }
11693
11694   /* Finish processing the declaration.  But, skip friend
11695      declarations.  */
11696   if (!friend_p && decl && decl != error_mark_node)
11697     {
11698       cp_finish_decl (decl,
11699                       initializer, !is_non_constant_init,
11700                       asm_specification,
11701                       /* If the initializer is in parentheses, then this is
11702                          a direct-initialization, which means that an
11703                          `explicit' constructor is OK.  Otherwise, an
11704                          `explicit' constructor cannot be used.  */
11705                       ((is_parenthesized_init || !is_initialized)
11706                      ? 0 : LOOKUP_ONLYCONVERTING));
11707     }
11708   if (!friend_p && pushed_scope)
11709     pop_scope (pushed_scope);
11710
11711   return decl;
11712 }
11713
11714 /* Parse a declarator.
11715
11716    declarator:
11717      direct-declarator
11718      ptr-operator declarator
11719
11720    abstract-declarator:
11721      ptr-operator abstract-declarator [opt]
11722      direct-abstract-declarator
11723
11724    GNU Extensions:
11725
11726    declarator:
11727      attributes [opt] direct-declarator
11728      attributes [opt] ptr-operator declarator
11729
11730    abstract-declarator:
11731      attributes [opt] ptr-operator abstract-declarator [opt]
11732      attributes [opt] direct-abstract-declarator
11733
11734    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11735    detect constructor, destructor or conversion operators. It is set
11736    to -1 if the declarator is a name, and +1 if it is a
11737    function. Otherwise it is set to zero. Usually you just want to
11738    test for >0, but internally the negative value is used.
11739
11740    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11741    a decl-specifier-seq unless it declares a constructor, destructor,
11742    or conversion.  It might seem that we could check this condition in
11743    semantic analysis, rather than parsing, but that makes it difficult
11744    to handle something like `f()'.  We want to notice that there are
11745    no decl-specifiers, and therefore realize that this is an
11746    expression, not a declaration.)
11747
11748    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11749    the declarator is a direct-declarator of the form "(...)".
11750
11751    MEMBER_P is true iff this declarator is a member-declarator.  */
11752
11753 static cp_declarator *
11754 cp_parser_declarator (cp_parser* parser,
11755                       cp_parser_declarator_kind dcl_kind,
11756                       int* ctor_dtor_or_conv_p,
11757                       bool* parenthesized_p,
11758                       bool member_p)
11759 {
11760   cp_token *token;
11761   cp_declarator *declarator;
11762   enum tree_code code;
11763   cp_cv_quals cv_quals;
11764   tree class_type;
11765   tree attributes = NULL_TREE;
11766
11767   /* Assume this is not a constructor, destructor, or type-conversion
11768      operator.  */
11769   if (ctor_dtor_or_conv_p)
11770     *ctor_dtor_or_conv_p = 0;
11771
11772   if (cp_parser_allow_gnu_extensions_p (parser))
11773     attributes = cp_parser_attributes_opt (parser);
11774
11775   /* Peek at the next token.  */
11776   token = cp_lexer_peek_token (parser->lexer);
11777
11778   /* Check for the ptr-operator production.  */
11779   cp_parser_parse_tentatively (parser);
11780   /* Parse the ptr-operator.  */
11781   code = cp_parser_ptr_operator (parser,
11782                                  &class_type,
11783                                  &cv_quals);
11784   /* If that worked, then we have a ptr-operator.  */
11785   if (cp_parser_parse_definitely (parser))
11786     {
11787       /* If a ptr-operator was found, then this declarator was not
11788          parenthesized.  */
11789       if (parenthesized_p)
11790         *parenthesized_p = true;
11791       /* The dependent declarator is optional if we are parsing an
11792          abstract-declarator.  */
11793       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11794         cp_parser_parse_tentatively (parser);
11795
11796       /* Parse the dependent declarator.  */
11797       declarator = cp_parser_declarator (parser, dcl_kind,
11798                                          /*ctor_dtor_or_conv_p=*/NULL,
11799                                          /*parenthesized_p=*/NULL,
11800                                          /*member_p=*/false);
11801
11802       /* If we are parsing an abstract-declarator, we must handle the
11803          case where the dependent declarator is absent.  */
11804       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11805           && !cp_parser_parse_definitely (parser))
11806         declarator = NULL;
11807
11808       /* Build the representation of the ptr-operator.  */
11809       if (class_type)
11810         declarator = make_ptrmem_declarator (cv_quals,
11811                                              class_type,
11812                                              declarator);
11813       else if (code == INDIRECT_REF)
11814         declarator = make_pointer_declarator (cv_quals, declarator);
11815       else
11816         declarator = make_reference_declarator (cv_quals, declarator);
11817     }
11818   /* Everything else is a direct-declarator.  */
11819   else
11820     {
11821       if (parenthesized_p)
11822         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11823                                                    CPP_OPEN_PAREN);
11824       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11825                                                 ctor_dtor_or_conv_p,
11826                                                 member_p);
11827     }
11828
11829   if (attributes && declarator && declarator != cp_error_declarator)
11830     declarator->attributes = attributes;
11831
11832   return declarator;
11833 }
11834
11835 /* Parse a direct-declarator or direct-abstract-declarator.
11836
11837    direct-declarator:
11838      declarator-id
11839      direct-declarator ( parameter-declaration-clause )
11840        cv-qualifier-seq [opt]
11841        exception-specification [opt]
11842      direct-declarator [ constant-expression [opt] ]
11843      ( declarator )
11844
11845    direct-abstract-declarator:
11846      direct-abstract-declarator [opt]
11847        ( parameter-declaration-clause )
11848        cv-qualifier-seq [opt]
11849        exception-specification [opt]
11850      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11851      ( abstract-declarator )
11852
11853    Returns a representation of the declarator.  DCL_KIND is
11854    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11855    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11856    we are parsing a direct-declarator.  It is
11857    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11858    of ambiguity we prefer an abstract declarator, as per
11859    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11860    cp_parser_declarator.  */
11861
11862 static cp_declarator *
11863 cp_parser_direct_declarator (cp_parser* parser,
11864                              cp_parser_declarator_kind dcl_kind,
11865                              int* ctor_dtor_or_conv_p,
11866                              bool member_p)
11867 {
11868   cp_token *token;
11869   cp_declarator *declarator = NULL;
11870   tree scope = NULL_TREE;
11871   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11872   bool saved_in_declarator_p = parser->in_declarator_p;
11873   bool first = true;
11874   tree pushed_scope = NULL_TREE;
11875
11876   while (true)
11877     {
11878       /* Peek at the next token.  */
11879       token = cp_lexer_peek_token (parser->lexer);
11880       if (token->type == CPP_OPEN_PAREN)
11881         {
11882           /* This is either a parameter-declaration-clause, or a
11883              parenthesized declarator. When we know we are parsing a
11884              named declarator, it must be a parenthesized declarator
11885              if FIRST is true. For instance, `(int)' is a
11886              parameter-declaration-clause, with an omitted
11887              direct-abstract-declarator. But `((*))', is a
11888              parenthesized abstract declarator. Finally, when T is a
11889              template parameter `(T)' is a
11890              parameter-declaration-clause, and not a parenthesized
11891              named declarator.
11892
11893              We first try and parse a parameter-declaration-clause,
11894              and then try a nested declarator (if FIRST is true).
11895
11896              It is not an error for it not to be a
11897              parameter-declaration-clause, even when FIRST is
11898              false. Consider,
11899
11900                int i (int);
11901                int i (3);
11902
11903              The first is the declaration of a function while the
11904              second is a the definition of a variable, including its
11905              initializer.
11906
11907              Having seen only the parenthesis, we cannot know which of
11908              these two alternatives should be selected.  Even more
11909              complex are examples like:
11910
11911                int i (int (a));
11912                int i (int (3));
11913
11914              The former is a function-declaration; the latter is a
11915              variable initialization.
11916
11917              Thus again, we try a parameter-declaration-clause, and if
11918              that fails, we back out and return.  */
11919
11920           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11921             {
11922               cp_parameter_declarator *params;
11923               unsigned saved_num_template_parameter_lists;
11924
11925               /* In a member-declarator, the only valid interpretation
11926                  of a parenthesis is the start of a
11927                  parameter-declaration-clause.  (It is invalid to
11928                  initialize a static data member with a parenthesized
11929                  initializer; only the "=" form of initialization is
11930                  permitted.)  */
11931               if (!member_p)
11932                 cp_parser_parse_tentatively (parser);
11933
11934               /* Consume the `('.  */
11935               cp_lexer_consume_token (parser->lexer);
11936               if (first)
11937                 {
11938                   /* If this is going to be an abstract declarator, we're
11939                      in a declarator and we can't have default args.  */
11940                   parser->default_arg_ok_p = false;
11941                   parser->in_declarator_p = true;
11942                 }
11943
11944               /* Inside the function parameter list, surrounding
11945                  template-parameter-lists do not apply.  */
11946               saved_num_template_parameter_lists
11947                 = parser->num_template_parameter_lists;
11948               parser->num_template_parameter_lists = 0;
11949
11950               /* Parse the parameter-declaration-clause.  */
11951               params = cp_parser_parameter_declaration_clause (parser);
11952
11953               parser->num_template_parameter_lists
11954                 = saved_num_template_parameter_lists;
11955
11956               /* If all went well, parse the cv-qualifier-seq and the
11957                  exception-specification.  */
11958               if (member_p || cp_parser_parse_definitely (parser))
11959                 {
11960                   cp_cv_quals cv_quals;
11961                   tree exception_specification;
11962
11963                   if (ctor_dtor_or_conv_p)
11964                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11965                   first = false;
11966                   /* Consume the `)'.  */
11967                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11968
11969                   /* Parse the cv-qualifier-seq.  */
11970                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11971                   /* And the exception-specification.  */
11972                   exception_specification
11973                     = cp_parser_exception_specification_opt (parser);
11974
11975                   /* Create the function-declarator.  */
11976                   declarator = make_call_declarator (declarator,
11977                                                      params,
11978                                                      cv_quals,
11979                                                      exception_specification);
11980                   /* Any subsequent parameter lists are to do with
11981                      return type, so are not those of the declared
11982                      function.  */
11983                   parser->default_arg_ok_p = false;
11984
11985                   /* Repeat the main loop.  */
11986                   continue;
11987                 }
11988             }
11989
11990           /* If this is the first, we can try a parenthesized
11991              declarator.  */
11992           if (first)
11993             {
11994               bool saved_in_type_id_in_expr_p;
11995
11996               parser->default_arg_ok_p = saved_default_arg_ok_p;
11997               parser->in_declarator_p = saved_in_declarator_p;
11998
11999               /* Consume the `('.  */
12000               cp_lexer_consume_token (parser->lexer);
12001               /* Parse the nested declarator.  */
12002               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12003               parser->in_type_id_in_expr_p = true;
12004               declarator
12005                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12006                                         /*parenthesized_p=*/NULL,
12007                                         member_p);
12008               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12009               first = false;
12010               /* Expect a `)'.  */
12011               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12012                 declarator = cp_error_declarator;
12013               if (declarator == cp_error_declarator)
12014                 break;
12015
12016               goto handle_declarator;
12017             }
12018           /* Otherwise, we must be done.  */
12019           else
12020             break;
12021         }
12022       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12023                && token->type == CPP_OPEN_SQUARE)
12024         {
12025           /* Parse an array-declarator.  */
12026           tree bounds;
12027
12028           if (ctor_dtor_or_conv_p)
12029             *ctor_dtor_or_conv_p = 0;
12030
12031           first = false;
12032           parser->default_arg_ok_p = false;
12033           parser->in_declarator_p = true;
12034           /* Consume the `['.  */
12035           cp_lexer_consume_token (parser->lexer);
12036           /* Peek at the next token.  */
12037           token = cp_lexer_peek_token (parser->lexer);
12038           /* If the next token is `]', then there is no
12039              constant-expression.  */
12040           if (token->type != CPP_CLOSE_SQUARE)
12041             {
12042               bool non_constant_p;
12043
12044               bounds
12045                 = cp_parser_constant_expression (parser,
12046                                                  /*allow_non_constant=*/true,
12047                                                  &non_constant_p);
12048               if (!non_constant_p)
12049                 bounds = fold_non_dependent_expr (bounds);
12050               /* Normally, the array bound must be an integral constant
12051                  expression.  However, as an extension, we allow VLAs
12052                  in function scopes.  */
12053               else if (!parser->in_function_body)
12054                 {
12055                   error ("array bound is not an integer constant");
12056                   bounds = error_mark_node;
12057                 }
12058             }
12059           else
12060             bounds = NULL_TREE;
12061           /* Look for the closing `]'.  */
12062           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12063             {
12064               declarator = cp_error_declarator;
12065               break;
12066             }
12067
12068           declarator = make_array_declarator (declarator, bounds);
12069         }
12070       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12071         {
12072           tree qualifying_scope;
12073           tree unqualified_name;
12074           special_function_kind sfk;
12075           bool abstract_ok;
12076           bool pack_expansion_p = false;
12077
12078           /* Parse a declarator-id */
12079           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12080           if (abstract_ok)
12081             {
12082               cp_parser_parse_tentatively (parser);
12083
12084               /* If we see an ellipsis, we should be looking at a
12085                  parameter pack. */
12086               if (token->type == CPP_ELLIPSIS)
12087                 {
12088                   /* Consume the `...' */
12089                   cp_lexer_consume_token (parser->lexer);
12090
12091                   pack_expansion_p = true;
12092                 }
12093             }
12094
12095           unqualified_name
12096             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12097           qualifying_scope = parser->scope;
12098           if (abstract_ok)
12099             {
12100               bool okay = false;
12101
12102               if (!unqualified_name && pack_expansion_p)
12103                 {
12104                   /* Check whether an error occurred. */
12105                   okay = !cp_parser_error_occurred (parser);
12106
12107                   /* We already consumed the ellipsis to mark a
12108                      parameter pack, but we have no way to report it,
12109                      so abort the tentative parse. We will be exiting
12110                      immediately anyway. */
12111                   cp_parser_abort_tentative_parse (parser);
12112                 }
12113               else
12114                 okay = cp_parser_parse_definitely (parser);
12115
12116               if (!okay)
12117                 unqualified_name = error_mark_node;
12118               else if (unqualified_name
12119                        && (qualifying_scope
12120                            || (TREE_CODE (unqualified_name)
12121                                != IDENTIFIER_NODE)))
12122                 {
12123                   cp_parser_error (parser, "expected unqualified-id");
12124                   unqualified_name = error_mark_node;
12125                 }
12126             }
12127
12128           if (!unqualified_name)
12129             return NULL;
12130           if (unqualified_name == error_mark_node)
12131             {
12132               declarator = cp_error_declarator;
12133               pack_expansion_p = false;
12134               declarator->parameter_pack_p = false;
12135               break;
12136             }
12137
12138           if (qualifying_scope && at_namespace_scope_p ()
12139               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12140             {
12141               /* In the declaration of a member of a template class
12142                  outside of the class itself, the SCOPE will sometimes
12143                  be a TYPENAME_TYPE.  For example, given:
12144
12145                  template <typename T>
12146                  int S<T>::R::i = 3;
12147
12148                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
12149                  this context, we must resolve S<T>::R to an ordinary
12150                  type, rather than a typename type.
12151
12152                  The reason we normally avoid resolving TYPENAME_TYPEs
12153                  is that a specialization of `S' might render
12154                  `S<T>::R' not a type.  However, if `S' is
12155                  specialized, then this `i' will not be used, so there
12156                  is no harm in resolving the types here.  */
12157               tree type;
12158
12159               /* Resolve the TYPENAME_TYPE.  */
12160               type = resolve_typename_type (qualifying_scope,
12161                                             /*only_current_p=*/false);
12162               /* If that failed, the declarator is invalid.  */
12163               if (type == error_mark_node)
12164                 error ("%<%T::%D%> is not a type",
12165                        TYPE_CONTEXT (qualifying_scope),
12166                        TYPE_IDENTIFIER (qualifying_scope));
12167               qualifying_scope = type;
12168             }
12169
12170           sfk = sfk_none;
12171
12172           if (unqualified_name)
12173             {
12174               tree class_type;
12175
12176               if (qualifying_scope
12177                   && CLASS_TYPE_P (qualifying_scope))
12178                 class_type = qualifying_scope;
12179               else
12180                 class_type = current_class_type;
12181
12182               if (TREE_CODE (unqualified_name) == TYPE_DECL)
12183                 {
12184                   tree name_type = TREE_TYPE (unqualified_name);
12185                   if (class_type && same_type_p (name_type, class_type))
12186                     {
12187                       if (qualifying_scope
12188                           && CLASSTYPE_USE_TEMPLATE (name_type))
12189                         {
12190                           error ("invalid use of constructor as a template");
12191                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12192                                   "name the constructor in a qualified name",
12193                                   class_type,
12194                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12195                                   class_type, name_type);
12196                           declarator = cp_error_declarator;
12197                           break;
12198                         }
12199                       else
12200                         unqualified_name = constructor_name (class_type);
12201                     }
12202                   else
12203                     {
12204                       /* We do not attempt to print the declarator
12205                          here because we do not have enough
12206                          information about its original syntactic
12207                          form.  */
12208                       cp_parser_error (parser, "invalid declarator");
12209                       declarator = cp_error_declarator;
12210                       break;
12211                     }
12212                 }
12213
12214               if (class_type)
12215                 {
12216                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12217                     sfk = sfk_destructor;
12218                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12219                     sfk = sfk_conversion;
12220                   else if (/* There's no way to declare a constructor
12221                               for an anonymous type, even if the type
12222                               got a name for linkage purposes.  */
12223                            !TYPE_WAS_ANONYMOUS (class_type)
12224                            && constructor_name_p (unqualified_name,
12225                                                   class_type))
12226                     {
12227                       unqualified_name = constructor_name (class_type);
12228                       sfk = sfk_constructor;
12229                     }
12230
12231                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
12232                     *ctor_dtor_or_conv_p = -1;
12233                 }
12234             }
12235           declarator = make_id_declarator (qualifying_scope,
12236                                            unqualified_name,
12237                                            sfk);
12238           declarator->id_loc = token->location;
12239           declarator->parameter_pack_p = pack_expansion_p;
12240
12241           if (pack_expansion_p)
12242             maybe_warn_variadic_templates ();
12243
12244         handle_declarator:;
12245           scope = get_scope_of_declarator (declarator);
12246           if (scope)
12247             /* Any names that appear after the declarator-id for a
12248                member are looked up in the containing scope.  */
12249             pushed_scope = push_scope (scope);
12250           parser->in_declarator_p = true;
12251           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12252               || (declarator && declarator->kind == cdk_id))
12253             /* Default args are only allowed on function
12254                declarations.  */
12255             parser->default_arg_ok_p = saved_default_arg_ok_p;
12256           else
12257             parser->default_arg_ok_p = false;
12258
12259           first = false;
12260         }
12261       /* We're done.  */
12262       else
12263         break;
12264     }
12265
12266   /* For an abstract declarator, we might wind up with nothing at this
12267      point.  That's an error; the declarator is not optional.  */
12268   if (!declarator)
12269     cp_parser_error (parser, "expected declarator");
12270
12271   /* If we entered a scope, we must exit it now.  */
12272   if (pushed_scope)
12273     pop_scope (pushed_scope);
12274
12275   parser->default_arg_ok_p = saved_default_arg_ok_p;
12276   parser->in_declarator_p = saved_in_declarator_p;
12277
12278   return declarator;
12279 }
12280
12281 /* Parse a ptr-operator.
12282
12283    ptr-operator:
12284      * cv-qualifier-seq [opt]
12285      &
12286      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12287
12288    GNU Extension:
12289
12290    ptr-operator:
12291      & cv-qualifier-seq [opt]
12292
12293    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12294    Returns ADDR_EXPR if a reference was used.  In the case of a
12295    pointer-to-member, *TYPE is filled in with the TYPE containing the
12296    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
12297    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
12298    ERROR_MARK if an error occurred.  */
12299
12300 static enum tree_code
12301 cp_parser_ptr_operator (cp_parser* parser,
12302                         tree* type,
12303                         cp_cv_quals *cv_quals)
12304 {
12305   enum tree_code code = ERROR_MARK;
12306   cp_token *token;
12307
12308   /* Assume that it's not a pointer-to-member.  */
12309   *type = NULL_TREE;
12310   /* And that there are no cv-qualifiers.  */
12311   *cv_quals = TYPE_UNQUALIFIED;
12312
12313   /* Peek at the next token.  */
12314   token = cp_lexer_peek_token (parser->lexer);
12315   /* If it's a `*' or `&' we have a pointer or reference.  */
12316   if (token->type == CPP_MULT || token->type == CPP_AND)
12317     {
12318       /* Remember which ptr-operator we were processing.  */
12319       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12320
12321       /* Consume the `*' or `&'.  */
12322       cp_lexer_consume_token (parser->lexer);
12323
12324       /* A `*' can be followed by a cv-qualifier-seq, and so can a
12325          `&', if we are allowing GNU extensions.  (The only qualifier
12326          that can legally appear after `&' is `restrict', but that is
12327          enforced during semantic analysis.  */
12328       if (code == INDIRECT_REF
12329           || cp_parser_allow_gnu_extensions_p (parser))
12330         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12331     }
12332   else
12333     {
12334       /* Try the pointer-to-member case.  */
12335       cp_parser_parse_tentatively (parser);
12336       /* Look for the optional `::' operator.  */
12337       cp_parser_global_scope_opt (parser,
12338                                   /*current_scope_valid_p=*/false);
12339       /* Look for the nested-name specifier.  */
12340       cp_parser_nested_name_specifier (parser,
12341                                        /*typename_keyword_p=*/false,
12342                                        /*check_dependency_p=*/true,
12343                                        /*type_p=*/false,
12344                                        /*is_declaration=*/false);
12345       /* If we found it, and the next token is a `*', then we are
12346          indeed looking at a pointer-to-member operator.  */
12347       if (!cp_parser_error_occurred (parser)
12348           && cp_parser_require (parser, CPP_MULT, "`*'"))
12349         {
12350           /* Indicate that the `*' operator was used.  */
12351           code = INDIRECT_REF;
12352
12353           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12354             error ("%qD is a namespace", parser->scope);
12355           else
12356             {
12357               /* The type of which the member is a member is given by the
12358                  current SCOPE.  */
12359               *type = parser->scope;
12360               /* The next name will not be qualified.  */
12361               parser->scope = NULL_TREE;
12362               parser->qualifying_scope = NULL_TREE;
12363               parser->object_scope = NULL_TREE;
12364               /* Look for the optional cv-qualifier-seq.  */
12365               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12366             }
12367         }
12368       /* If that didn't work we don't have a ptr-operator.  */
12369       if (!cp_parser_parse_definitely (parser))
12370         cp_parser_error (parser, "expected ptr-operator");
12371     }
12372
12373   return code;
12374 }
12375
12376 /* Parse an (optional) cv-qualifier-seq.
12377
12378    cv-qualifier-seq:
12379      cv-qualifier cv-qualifier-seq [opt]
12380
12381    cv-qualifier:
12382      const
12383      volatile
12384
12385    GNU Extension:
12386
12387    cv-qualifier:
12388      __restrict__
12389
12390    Returns a bitmask representing the cv-qualifiers.  */
12391
12392 static cp_cv_quals
12393 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12394 {
12395   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12396
12397   while (true)
12398     {
12399       cp_token *token;
12400       cp_cv_quals cv_qualifier;
12401
12402       /* Peek at the next token.  */
12403       token = cp_lexer_peek_token (parser->lexer);
12404       /* See if it's a cv-qualifier.  */
12405       switch (token->keyword)
12406         {
12407         case RID_CONST:
12408           cv_qualifier = TYPE_QUAL_CONST;
12409           break;
12410
12411         case RID_VOLATILE:
12412           cv_qualifier = TYPE_QUAL_VOLATILE;
12413           break;
12414
12415         case RID_RESTRICT:
12416           cv_qualifier = TYPE_QUAL_RESTRICT;
12417           break;
12418
12419         default:
12420           cv_qualifier = TYPE_UNQUALIFIED;
12421           break;
12422         }
12423
12424       if (!cv_qualifier)
12425         break;
12426
12427       if (cv_quals & cv_qualifier)
12428         {
12429           error ("duplicate cv-qualifier");
12430           cp_lexer_purge_token (parser->lexer);
12431         }
12432       else
12433         {
12434           cp_lexer_consume_token (parser->lexer);
12435           cv_quals |= cv_qualifier;
12436         }
12437     }
12438
12439   return cv_quals;
12440 }
12441
12442 /* Parse a declarator-id.
12443
12444    declarator-id:
12445      id-expression
12446      :: [opt] nested-name-specifier [opt] type-name
12447
12448    In the `id-expression' case, the value returned is as for
12449    cp_parser_id_expression if the id-expression was an unqualified-id.
12450    If the id-expression was a qualified-id, then a SCOPE_REF is
12451    returned.  The first operand is the scope (either a NAMESPACE_DECL
12452    or TREE_TYPE), but the second is still just a representation of an
12453    unqualified-id.  */
12454
12455 static tree
12456 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12457 {
12458   tree id;
12459   /* The expression must be an id-expression.  Assume that qualified
12460      names are the names of types so that:
12461
12462        template <class T>
12463        int S<T>::R::i = 3;
12464
12465      will work; we must treat `S<T>::R' as the name of a type.
12466      Similarly, assume that qualified names are templates, where
12467      required, so that:
12468
12469        template <class T>
12470        int S<T>::R<T>::i = 3;
12471
12472      will work, too.  */
12473   id = cp_parser_id_expression (parser,
12474                                 /*template_keyword_p=*/false,
12475                                 /*check_dependency_p=*/false,
12476                                 /*template_p=*/NULL,
12477                                 /*declarator_p=*/true,
12478                                 optional_p);
12479   if (id && BASELINK_P (id))
12480     id = BASELINK_FUNCTIONS (id);
12481   return id;
12482 }
12483
12484 /* Parse a type-id.
12485
12486    type-id:
12487      type-specifier-seq abstract-declarator [opt]
12488
12489    Returns the TYPE specified.  */
12490
12491 static tree
12492 cp_parser_type_id (cp_parser* parser)
12493 {
12494   cp_decl_specifier_seq type_specifier_seq;
12495   cp_declarator *abstract_declarator;
12496
12497   /* Parse the type-specifier-seq.  */
12498   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12499                                 &type_specifier_seq);
12500   if (type_specifier_seq.type == error_mark_node)
12501     return error_mark_node;
12502
12503   /* There might or might not be an abstract declarator.  */
12504   cp_parser_parse_tentatively (parser);
12505   /* Look for the declarator.  */
12506   abstract_declarator
12507     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12508                             /*parenthesized_p=*/NULL,
12509                             /*member_p=*/false);
12510   /* Check to see if there really was a declarator.  */
12511   if (!cp_parser_parse_definitely (parser))
12512     abstract_declarator = NULL;
12513
12514   return groktypename (&type_specifier_seq, abstract_declarator);
12515 }
12516
12517 /* Parse a type-specifier-seq.
12518
12519    type-specifier-seq:
12520      type-specifier type-specifier-seq [opt]
12521
12522    GNU extension:
12523
12524    type-specifier-seq:
12525      attributes type-specifier-seq [opt]
12526
12527    If IS_CONDITION is true, we are at the start of a "condition",
12528    e.g., we've just seen "if (".
12529
12530    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
12531
12532 static void
12533 cp_parser_type_specifier_seq (cp_parser* parser,
12534                               bool is_condition,
12535                               cp_decl_specifier_seq *type_specifier_seq)
12536 {
12537   bool seen_type_specifier = false;
12538   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12539
12540   /* Clear the TYPE_SPECIFIER_SEQ.  */
12541   clear_decl_specs (type_specifier_seq);
12542
12543   /* Parse the type-specifiers and attributes.  */
12544   while (true)
12545     {
12546       tree type_specifier;
12547       bool is_cv_qualifier;
12548
12549       /* Check for attributes first.  */
12550       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12551         {
12552           type_specifier_seq->attributes =
12553             chainon (type_specifier_seq->attributes,
12554                      cp_parser_attributes_opt (parser));
12555           continue;
12556         }
12557
12558       /* Look for the type-specifier.  */
12559       type_specifier = cp_parser_type_specifier (parser,
12560                                                  flags,
12561                                                  type_specifier_seq,
12562                                                  /*is_declaration=*/false,
12563                                                  NULL,
12564                                                  &is_cv_qualifier);
12565       if (!type_specifier)
12566         {
12567           /* If the first type-specifier could not be found, this is not a
12568              type-specifier-seq at all.  */
12569           if (!seen_type_specifier)
12570             {
12571               cp_parser_error (parser, "expected type-specifier");
12572               type_specifier_seq->type = error_mark_node;
12573               return;
12574             }
12575           /* If subsequent type-specifiers could not be found, the
12576              type-specifier-seq is complete.  */
12577           break;
12578         }
12579
12580       seen_type_specifier = true;
12581       /* The standard says that a condition can be:
12582
12583             type-specifier-seq declarator = assignment-expression
12584
12585          However, given:
12586
12587            struct S {};
12588            if (int S = ...)
12589
12590          we should treat the "S" as a declarator, not as a
12591          type-specifier.  The standard doesn't say that explicitly for
12592          type-specifier-seq, but it does say that for
12593          decl-specifier-seq in an ordinary declaration.  Perhaps it
12594          would be clearer just to allow a decl-specifier-seq here, and
12595          then add a semantic restriction that if any decl-specifiers
12596          that are not type-specifiers appear, the program is invalid.  */
12597       if (is_condition && !is_cv_qualifier)
12598         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12599     }
12600
12601   cp_parser_check_decl_spec (type_specifier_seq);
12602 }
12603
12604 /* Parse a parameter-declaration-clause.
12605
12606    parameter-declaration-clause:
12607      parameter-declaration-list [opt] ... [opt]
12608      parameter-declaration-list , ...
12609
12610    Returns a representation for the parameter declarations.  A return
12611    value of NULL indicates a parameter-declaration-clause consisting
12612    only of an ellipsis.  */
12613
12614 static cp_parameter_declarator *
12615 cp_parser_parameter_declaration_clause (cp_parser* parser)
12616 {
12617   cp_parameter_declarator *parameters;
12618   cp_token *token;
12619   bool ellipsis_p;
12620   bool is_error;
12621
12622   /* Peek at the next token.  */
12623   token = cp_lexer_peek_token (parser->lexer);
12624   /* Check for trivial parameter-declaration-clauses.  */
12625   if (token->type == CPP_ELLIPSIS)
12626     {
12627       /* Consume the `...' token.  */
12628       cp_lexer_consume_token (parser->lexer);
12629       return NULL;
12630     }
12631   else if (token->type == CPP_CLOSE_PAREN)
12632     /* There are no parameters.  */
12633     {
12634 #ifndef NO_IMPLICIT_EXTERN_C
12635       if (in_system_header && current_class_type == NULL
12636           && current_lang_name == lang_name_c)
12637         return NULL;
12638       else
12639 #endif
12640         return no_parameters;
12641     }
12642   /* Check for `(void)', too, which is a special case.  */
12643   else if (token->keyword == RID_VOID
12644            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12645                == CPP_CLOSE_PAREN))
12646     {
12647       /* Consume the `void' token.  */
12648       cp_lexer_consume_token (parser->lexer);
12649       /* There are no parameters.  */
12650       return no_parameters;
12651     }
12652
12653   /* Parse the parameter-declaration-list.  */
12654   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12655   /* If a parse error occurred while parsing the
12656      parameter-declaration-list, then the entire
12657      parameter-declaration-clause is erroneous.  */
12658   if (is_error)
12659     return NULL;
12660
12661   /* Peek at the next token.  */
12662   token = cp_lexer_peek_token (parser->lexer);
12663   /* If it's a `,', the clause should terminate with an ellipsis.  */
12664   if (token->type == CPP_COMMA)
12665     {
12666       /* Consume the `,'.  */
12667       cp_lexer_consume_token (parser->lexer);
12668       /* Expect an ellipsis.  */
12669       ellipsis_p
12670         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12671     }
12672   /* It might also be `...' if the optional trailing `,' was
12673      omitted.  */
12674   else if (token->type == CPP_ELLIPSIS)
12675     {
12676       /* Consume the `...' token.  */
12677       cp_lexer_consume_token (parser->lexer);
12678       /* And remember that we saw it.  */
12679       ellipsis_p = true;
12680     }
12681   else
12682     ellipsis_p = false;
12683
12684   /* Finish the parameter list.  */
12685   if (parameters && ellipsis_p)
12686     parameters->ellipsis_p = true;
12687
12688   return parameters;
12689 }
12690
12691 /* Parse a parameter-declaration-list.
12692
12693    parameter-declaration-list:
12694      parameter-declaration
12695      parameter-declaration-list , parameter-declaration
12696
12697    Returns a representation of the parameter-declaration-list, as for
12698    cp_parser_parameter_declaration_clause.  However, the
12699    `void_list_node' is never appended to the list.  Upon return,
12700    *IS_ERROR will be true iff an error occurred.  */
12701
12702 static cp_parameter_declarator *
12703 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12704 {
12705   cp_parameter_declarator *parameters = NULL;
12706   cp_parameter_declarator **tail = &parameters;
12707   bool saved_in_unbraced_linkage_specification_p;
12708
12709   /* Assume all will go well.  */
12710   *is_error = false;
12711   /* The special considerations that apply to a function within an
12712      unbraced linkage specifications do not apply to the parameters
12713      to the function.  */
12714   saved_in_unbraced_linkage_specification_p 
12715     = parser->in_unbraced_linkage_specification_p;
12716   parser->in_unbraced_linkage_specification_p = false;
12717
12718   /* Look for more parameters.  */
12719   while (true)
12720     {
12721       cp_parameter_declarator *parameter;
12722       bool parenthesized_p;
12723       /* Parse the parameter.  */
12724       parameter
12725         = cp_parser_parameter_declaration (parser,
12726                                            /*template_parm_p=*/false,
12727                                            &parenthesized_p);
12728
12729       /* If a parse error occurred parsing the parameter declaration,
12730          then the entire parameter-declaration-list is erroneous.  */
12731       if (!parameter)
12732         {
12733           *is_error = true;
12734           parameters = NULL;
12735           break;
12736         }
12737       /* Add the new parameter to the list.  */
12738       *tail = parameter;
12739       tail = &parameter->next;
12740
12741       /* Peek at the next token.  */
12742       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12743           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12744           /* These are for Objective-C++ */
12745           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12746           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12747         /* The parameter-declaration-list is complete.  */
12748         break;
12749       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12750         {
12751           cp_token *token;
12752
12753           /* Peek at the next token.  */
12754           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12755           /* If it's an ellipsis, then the list is complete.  */
12756           if (token->type == CPP_ELLIPSIS)
12757             break;
12758           /* Otherwise, there must be more parameters.  Consume the
12759              `,'.  */
12760           cp_lexer_consume_token (parser->lexer);
12761           /* When parsing something like:
12762
12763                 int i(float f, double d)
12764
12765              we can tell after seeing the declaration for "f" that we
12766              are not looking at an initialization of a variable "i",
12767              but rather at the declaration of a function "i".
12768
12769              Due to the fact that the parsing of template arguments
12770              (as specified to a template-id) requires backtracking we
12771              cannot use this technique when inside a template argument
12772              list.  */
12773           if (!parser->in_template_argument_list_p
12774               && !parser->in_type_id_in_expr_p
12775               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12776               /* However, a parameter-declaration of the form
12777                  "foat(f)" (which is a valid declaration of a
12778                  parameter "f") can also be interpreted as an
12779                  expression (the conversion of "f" to "float").  */
12780               && !parenthesized_p)
12781             cp_parser_commit_to_tentative_parse (parser);
12782         }
12783       else
12784         {
12785           cp_parser_error (parser, "expected %<,%> or %<...%>");
12786           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12787             cp_parser_skip_to_closing_parenthesis (parser,
12788                                                    /*recovering=*/true,
12789                                                    /*or_comma=*/false,
12790                                                    /*consume_paren=*/false);
12791           break;
12792         }
12793     }
12794
12795   parser->in_unbraced_linkage_specification_p
12796     = saved_in_unbraced_linkage_specification_p;
12797
12798   return parameters;
12799 }
12800
12801 /* Parse a parameter declaration.
12802
12803    parameter-declaration:
12804      decl-specifier-seq ... [opt] declarator
12805      decl-specifier-seq declarator = assignment-expression
12806      decl-specifier-seq ... [opt] abstract-declarator [opt]
12807      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12808
12809    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12810    declares a template parameter.  (In that case, a non-nested `>'
12811    token encountered during the parsing of the assignment-expression
12812    is not interpreted as a greater-than operator.)
12813
12814    Returns a representation of the parameter, or NULL if an error
12815    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12816    true iff the declarator is of the form "(p)".  */
12817
12818 static cp_parameter_declarator *
12819 cp_parser_parameter_declaration (cp_parser *parser,
12820                                  bool template_parm_p,
12821                                  bool *parenthesized_p)
12822 {
12823   int declares_class_or_enum;
12824   bool greater_than_is_operator_p;
12825   cp_decl_specifier_seq decl_specifiers;
12826   cp_declarator *declarator;
12827   tree default_argument;
12828   cp_token *token;
12829   const char *saved_message;
12830
12831   /* In a template parameter, `>' is not an operator.
12832
12833      [temp.param]
12834
12835      When parsing a default template-argument for a non-type
12836      template-parameter, the first non-nested `>' is taken as the end
12837      of the template parameter-list rather than a greater-than
12838      operator.  */
12839   greater_than_is_operator_p = !template_parm_p;
12840
12841   /* Type definitions may not appear in parameter types.  */
12842   saved_message = parser->type_definition_forbidden_message;
12843   parser->type_definition_forbidden_message
12844     = "types may not be defined in parameter types";
12845
12846   /* Parse the declaration-specifiers.  */
12847   cp_parser_decl_specifier_seq (parser,
12848                                 CP_PARSER_FLAGS_NONE,
12849                                 &decl_specifiers,
12850                                 &declares_class_or_enum);
12851   /* If an error occurred, there's no reason to attempt to parse the
12852      rest of the declaration.  */
12853   if (cp_parser_error_occurred (parser))
12854     {
12855       parser->type_definition_forbidden_message = saved_message;
12856       return NULL;
12857     }
12858
12859   /* Peek at the next token.  */
12860   token = cp_lexer_peek_token (parser->lexer);
12861
12862   /* If the next token is a `)', `,', `=', `>', or `...', then there
12863      is no declarator. However, when variadic templates are enabled,
12864      there may be a declarator following `...'.  */
12865   if (token->type == CPP_CLOSE_PAREN
12866       || token->type == CPP_COMMA
12867       || token->type == CPP_EQ
12868       || token->type == CPP_GREATER)
12869     {
12870       declarator = NULL;
12871       if (parenthesized_p)
12872         *parenthesized_p = false;
12873     }
12874   /* Otherwise, there should be a declarator.  */
12875   else
12876     {
12877       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12878       parser->default_arg_ok_p = false;
12879
12880       /* After seeing a decl-specifier-seq, if the next token is not a
12881          "(", there is no possibility that the code is a valid
12882          expression.  Therefore, if parsing tentatively, we commit at
12883          this point.  */
12884       if (!parser->in_template_argument_list_p
12885           /* In an expression context, having seen:
12886
12887                (int((char ...
12888
12889              we cannot be sure whether we are looking at a
12890              function-type (taking a "char" as a parameter) or a cast
12891              of some object of type "char" to "int".  */
12892           && !parser->in_type_id_in_expr_p
12893           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12894           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12895         cp_parser_commit_to_tentative_parse (parser);
12896       /* Parse the declarator.  */
12897       declarator = cp_parser_declarator (parser,
12898                                          CP_PARSER_DECLARATOR_EITHER,
12899                                          /*ctor_dtor_or_conv_p=*/NULL,
12900                                          parenthesized_p,
12901                                          /*member_p=*/false);
12902       parser->default_arg_ok_p = saved_default_arg_ok_p;
12903       /* After the declarator, allow more attributes.  */
12904       decl_specifiers.attributes
12905         = chainon (decl_specifiers.attributes,
12906                    cp_parser_attributes_opt (parser));
12907     }
12908
12909   /* If the next token is an ellipsis, and the type of the declarator
12910      contains parameter packs but it is not a TYPE_PACK_EXPANSION, then
12911      we actually have a parameter pack expansion expression. Otherwise,
12912      leave the ellipsis for a C-style variadic function. */
12913   token = cp_lexer_peek_token (parser->lexer);
12914   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12915     {
12916       tree type = decl_specifiers.type;
12917
12918       if (DECL_P (type))
12919         type = TREE_TYPE (type);
12920
12921       if (TREE_CODE (type) != TYPE_PACK_EXPANSION
12922           && (!declarator || !declarator->parameter_pack_p)
12923           && uses_parameter_packs (type))
12924         {
12925           /* Consume the `...'. */
12926           cp_lexer_consume_token (parser->lexer);
12927           maybe_warn_variadic_templates ();
12928           
12929           /* Build a pack expansion type */
12930           if (declarator)
12931             declarator->parameter_pack_p = true;
12932           else
12933             decl_specifiers.type = make_pack_expansion (type);
12934         }
12935     }
12936
12937   /* The restriction on defining new types applies only to the type
12938      of the parameter, not to the default argument.  */
12939   parser->type_definition_forbidden_message = saved_message;
12940
12941   /* If the next token is `=', then process a default argument.  */
12942   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12943     {
12944       bool saved_greater_than_is_operator_p;
12945       /* Consume the `='.  */
12946       cp_lexer_consume_token (parser->lexer);
12947
12948       /* If we are defining a class, then the tokens that make up the
12949          default argument must be saved and processed later.  */
12950       if (!template_parm_p && at_class_scope_p ()
12951           && TYPE_BEING_DEFINED (current_class_type))
12952         {
12953           unsigned depth = 0;
12954           cp_token *first_token;
12955           cp_token *token;
12956
12957           /* Add tokens until we have processed the entire default
12958              argument.  We add the range [first_token, token).  */
12959           first_token = cp_lexer_peek_token (parser->lexer);
12960           while (true)
12961             {
12962               bool done = false;
12963
12964               /* Peek at the next token.  */
12965               token = cp_lexer_peek_token (parser->lexer);
12966               /* What we do depends on what token we have.  */
12967               switch (token->type)
12968                 {
12969                   /* In valid code, a default argument must be
12970                      immediately followed by a `,' `)', or `...'.  */
12971                 case CPP_COMMA:
12972                 case CPP_CLOSE_PAREN:
12973                 case CPP_ELLIPSIS:
12974                   /* If we run into a non-nested `;', `}', or `]',
12975                      then the code is invalid -- but the default
12976                      argument is certainly over.  */
12977                 case CPP_SEMICOLON:
12978                 case CPP_CLOSE_BRACE:
12979                 case CPP_CLOSE_SQUARE:
12980                   if (depth == 0)
12981                     done = true;
12982                   /* Update DEPTH, if necessary.  */
12983                   else if (token->type == CPP_CLOSE_PAREN
12984                            || token->type == CPP_CLOSE_BRACE
12985                            || token->type == CPP_CLOSE_SQUARE)
12986                     --depth;
12987                   break;
12988
12989                 case CPP_OPEN_PAREN:
12990                 case CPP_OPEN_SQUARE:
12991                 case CPP_OPEN_BRACE:
12992                   ++depth;
12993                   break;
12994
12995                 case CPP_GREATER:
12996                   /* If we see a non-nested `>', and `>' is not an
12997                      operator, then it marks the end of the default
12998                      argument.  */
12999                   if (!depth && !greater_than_is_operator_p)
13000                     done = true;
13001                   break;
13002
13003                   /* If we run out of tokens, issue an error message.  */
13004                 case CPP_EOF:
13005                 case CPP_PRAGMA_EOL:
13006                   error ("file ends in default argument");
13007                   done = true;
13008                   break;
13009
13010                 case CPP_NAME:
13011                 case CPP_SCOPE:
13012                   /* In these cases, we should look for template-ids.
13013                      For example, if the default argument is
13014                      `X<int, double>()', we need to do name lookup to
13015                      figure out whether or not `X' is a template; if
13016                      so, the `,' does not end the default argument.
13017
13018                      That is not yet done.  */
13019                   break;
13020
13021                 default:
13022                   break;
13023                 }
13024
13025               /* If we've reached the end, stop.  */
13026               if (done)
13027                 break;
13028
13029               /* Add the token to the token block.  */
13030               token = cp_lexer_consume_token (parser->lexer);
13031             }
13032
13033           /* Create a DEFAULT_ARG to represented the unparsed default
13034              argument.  */
13035           default_argument = make_node (DEFAULT_ARG);
13036           DEFARG_TOKENS (default_argument)
13037             = cp_token_cache_new (first_token, token);
13038           DEFARG_INSTANTIATIONS (default_argument) = NULL;
13039         }
13040       /* Outside of a class definition, we can just parse the
13041          assignment-expression.  */
13042       else
13043         {
13044           bool saved_local_variables_forbidden_p;
13045
13046           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13047              set correctly.  */
13048           saved_greater_than_is_operator_p
13049             = parser->greater_than_is_operator_p;
13050           parser->greater_than_is_operator_p = greater_than_is_operator_p;
13051           /* Local variable names (and the `this' keyword) may not
13052              appear in a default argument.  */
13053           saved_local_variables_forbidden_p
13054             = parser->local_variables_forbidden_p;
13055           parser->local_variables_forbidden_p = true;
13056           /* The default argument expression may cause implicitly
13057              defined member functions to be synthesized, which will
13058              result in garbage collection.  We must treat this
13059              situation as if we were within the body of function so as
13060              to avoid collecting live data on the stack.  */
13061           ++function_depth;
13062           /* Parse the assignment-expression.  */
13063           if (template_parm_p)
13064             push_deferring_access_checks (dk_no_deferred);
13065           default_argument
13066             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13067           if (template_parm_p)
13068             pop_deferring_access_checks ();
13069           /* Restore saved state.  */
13070           --function_depth;
13071           parser->greater_than_is_operator_p
13072             = saved_greater_than_is_operator_p;
13073           parser->local_variables_forbidden_p
13074             = saved_local_variables_forbidden_p;
13075         }
13076       if (!parser->default_arg_ok_p)
13077         {
13078           if (!flag_pedantic_errors)
13079             warning (0, "deprecated use of default argument for parameter of non-function");
13080           else
13081             {
13082               error ("default arguments are only permitted for function parameters");
13083               default_argument = NULL_TREE;
13084             }
13085         }
13086     }
13087   else
13088     default_argument = NULL_TREE;
13089
13090   return make_parameter_declarator (&decl_specifiers,
13091                                     declarator,
13092                                     default_argument);
13093 }
13094
13095 /* Parse a function-body.
13096
13097    function-body:
13098      compound_statement  */
13099
13100 static void
13101 cp_parser_function_body (cp_parser *parser)
13102 {
13103   cp_parser_compound_statement (parser, NULL, false);
13104 }
13105
13106 /* Parse a ctor-initializer-opt followed by a function-body.  Return
13107    true if a ctor-initializer was present.  */
13108
13109 static bool
13110 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13111 {
13112   tree body;
13113   bool ctor_initializer_p;
13114
13115   /* Begin the function body.  */
13116   body = begin_function_body ();
13117   /* Parse the optional ctor-initializer.  */
13118   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13119   /* Parse the function-body.  */
13120   cp_parser_function_body (parser);
13121   /* Finish the function body.  */
13122   finish_function_body (body);
13123
13124   return ctor_initializer_p;
13125 }
13126
13127 /* Parse an initializer.
13128
13129    initializer:
13130      = initializer-clause
13131      ( expression-list )
13132
13133    Returns an expression representing the initializer.  If no
13134    initializer is present, NULL_TREE is returned.
13135
13136    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13137    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
13138    set to FALSE if there is no initializer present.  If there is an
13139    initializer, and it is not a constant-expression, *NON_CONSTANT_P
13140    is set to true; otherwise it is set to false.  */
13141
13142 static tree
13143 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13144                        bool* non_constant_p)
13145 {
13146   cp_token *token;
13147   tree init;
13148
13149   /* Peek at the next token.  */
13150   token = cp_lexer_peek_token (parser->lexer);
13151
13152   /* Let our caller know whether or not this initializer was
13153      parenthesized.  */
13154   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13155   /* Assume that the initializer is constant.  */
13156   *non_constant_p = false;
13157
13158   if (token->type == CPP_EQ)
13159     {
13160       /* Consume the `='.  */
13161       cp_lexer_consume_token (parser->lexer);
13162       /* Parse the initializer-clause.  */
13163       init = cp_parser_initializer_clause (parser, non_constant_p);
13164     }
13165   else if (token->type == CPP_OPEN_PAREN)
13166     init = cp_parser_parenthesized_expression_list (parser, false,
13167                                                     /*cast_p=*/false,
13168                                                     /*allow_expansion_p=*/true,
13169                                                     non_constant_p);
13170   else
13171     {
13172       /* Anything else is an error.  */
13173       cp_parser_error (parser, "expected initializer");
13174       init = error_mark_node;
13175     }
13176
13177   return init;
13178 }
13179
13180 /* Parse an initializer-clause.
13181
13182    initializer-clause:
13183      assignment-expression
13184      { initializer-list , [opt] }
13185      { }
13186
13187    Returns an expression representing the initializer.
13188
13189    If the `assignment-expression' production is used the value
13190    returned is simply a representation for the expression.
13191
13192    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
13193    the elements of the initializer-list (or NULL, if the last
13194    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
13195    NULL_TREE.  There is no way to detect whether or not the optional
13196    trailing `,' was provided.  NON_CONSTANT_P is as for
13197    cp_parser_initializer.  */
13198
13199 static tree
13200 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13201 {
13202   tree initializer;
13203
13204   /* Assume the expression is constant.  */
13205   *non_constant_p = false;
13206
13207   /* If it is not a `{', then we are looking at an
13208      assignment-expression.  */
13209   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13210     {
13211       initializer
13212         = cp_parser_constant_expression (parser,
13213                                         /*allow_non_constant_p=*/true,
13214                                         non_constant_p);
13215       if (!*non_constant_p)
13216         initializer = fold_non_dependent_expr (initializer);
13217     }
13218   else
13219     {
13220       /* Consume the `{' token.  */
13221       cp_lexer_consume_token (parser->lexer);
13222       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
13223       initializer = make_node (CONSTRUCTOR);
13224       /* If it's not a `}', then there is a non-trivial initializer.  */
13225       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13226         {
13227           /* Parse the initializer list.  */
13228           CONSTRUCTOR_ELTS (initializer)
13229             = cp_parser_initializer_list (parser, non_constant_p);
13230           /* A trailing `,' token is allowed.  */
13231           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13232             cp_lexer_consume_token (parser->lexer);
13233         }
13234       /* Now, there should be a trailing `}'.  */
13235       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13236     }
13237
13238   return initializer;
13239 }
13240
13241 /* Parse an initializer-list.
13242
13243    initializer-list:
13244      initializer-clause ... [opt]
13245      initializer-list , initializer-clause ... [opt]
13246
13247    GNU Extension:
13248
13249    initializer-list:
13250      identifier : initializer-clause
13251      initializer-list, identifier : initializer-clause
13252
13253    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
13254    for the initializer.  If the INDEX of the elt is non-NULL, it is the
13255    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
13256    as for cp_parser_initializer.  */
13257
13258 static VEC(constructor_elt,gc) *
13259 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13260 {
13261   VEC(constructor_elt,gc) *v = NULL;
13262
13263   /* Assume all of the expressions are constant.  */
13264   *non_constant_p = false;
13265
13266   /* Parse the rest of the list.  */
13267   while (true)
13268     {
13269       cp_token *token;
13270       tree identifier;
13271       tree initializer;
13272       bool clause_non_constant_p;
13273
13274       /* If the next token is an identifier and the following one is a
13275          colon, we are looking at the GNU designated-initializer
13276          syntax.  */
13277       if (cp_parser_allow_gnu_extensions_p (parser)
13278           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13279           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13280         {
13281           /* Warn the user that they are using an extension.  */
13282           if (pedantic)
13283             pedwarn ("ISO C++ does not allow designated initializers");
13284           /* Consume the identifier.  */
13285           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13286           /* Consume the `:'.  */
13287           cp_lexer_consume_token (parser->lexer);
13288         }
13289       else
13290         identifier = NULL_TREE;
13291
13292       /* Parse the initializer.  */
13293       initializer = cp_parser_initializer_clause (parser,
13294                                                   &clause_non_constant_p);
13295       /* If any clause is non-constant, so is the entire initializer.  */
13296       if (clause_non_constant_p)
13297         *non_constant_p = true;
13298
13299       /* If we have an ellipsis, this is an initializer pack
13300          expansion.  */
13301       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13302         {
13303           /* Consume the `...'.  */
13304           cp_lexer_consume_token (parser->lexer);
13305
13306           /* Turn the initializer into an initializer expansion.  */
13307           initializer = make_pack_expansion (initializer);
13308         }
13309
13310       /* Add it to the vector.  */
13311       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13312
13313       /* If the next token is not a comma, we have reached the end of
13314          the list.  */
13315       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13316         break;
13317
13318       /* Peek at the next token.  */
13319       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13320       /* If the next token is a `}', then we're still done.  An
13321          initializer-clause can have a trailing `,' after the
13322          initializer-list and before the closing `}'.  */
13323       if (token->type == CPP_CLOSE_BRACE)
13324         break;
13325
13326       /* Consume the `,' token.  */
13327       cp_lexer_consume_token (parser->lexer);
13328     }
13329
13330   return v;
13331 }
13332
13333 /* Classes [gram.class] */
13334
13335 /* Parse a class-name.
13336
13337    class-name:
13338      identifier
13339      template-id
13340
13341    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13342    to indicate that names looked up in dependent types should be
13343    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
13344    keyword has been used to indicate that the name that appears next
13345    is a template.  TAG_TYPE indicates the explicit tag given before
13346    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
13347    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
13348    is the class being defined in a class-head.
13349
13350    Returns the TYPE_DECL representing the class.  */
13351
13352 static tree
13353 cp_parser_class_name (cp_parser *parser,
13354                       bool typename_keyword_p,
13355                       bool template_keyword_p,
13356                       enum tag_types tag_type,
13357                       bool check_dependency_p,
13358                       bool class_head_p,
13359                       bool is_declaration)
13360 {
13361   tree decl;
13362   tree scope;
13363   bool typename_p;
13364   cp_token *token;
13365
13366   /* All class-names start with an identifier.  */
13367   token = cp_lexer_peek_token (parser->lexer);
13368   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13369     {
13370       cp_parser_error (parser, "expected class-name");
13371       return error_mark_node;
13372     }
13373
13374   /* PARSER->SCOPE can be cleared when parsing the template-arguments
13375      to a template-id, so we save it here.  */
13376   scope = parser->scope;
13377   if (scope == error_mark_node)
13378     return error_mark_node;
13379
13380   /* Any name names a type if we're following the `typename' keyword
13381      in a qualified name where the enclosing scope is type-dependent.  */
13382   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13383                 && dependent_type_p (scope));
13384   /* Handle the common case (an identifier, but not a template-id)
13385      efficiently.  */
13386   if (token->type == CPP_NAME
13387       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13388     {
13389       cp_token *identifier_token;
13390       tree identifier;
13391       bool ambiguous_p;
13392
13393       /* Look for the identifier.  */
13394       identifier_token = cp_lexer_peek_token (parser->lexer);
13395       ambiguous_p = identifier_token->ambiguous_p;
13396       identifier = cp_parser_identifier (parser);
13397       /* If the next token isn't an identifier, we are certainly not
13398          looking at a class-name.  */
13399       if (identifier == error_mark_node)
13400         decl = error_mark_node;
13401       /* If we know this is a type-name, there's no need to look it
13402          up.  */
13403       else if (typename_p)
13404         decl = identifier;
13405       else
13406         {
13407           tree ambiguous_decls;
13408           /* If we already know that this lookup is ambiguous, then
13409              we've already issued an error message; there's no reason
13410              to check again.  */
13411           if (ambiguous_p)
13412             {
13413               cp_parser_simulate_error (parser);
13414               return error_mark_node;
13415             }
13416           /* If the next token is a `::', then the name must be a type
13417              name.
13418
13419              [basic.lookup.qual]
13420
13421              During the lookup for a name preceding the :: scope
13422              resolution operator, object, function, and enumerator
13423              names are ignored.  */
13424           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13425             tag_type = typename_type;
13426           /* Look up the name.  */
13427           decl = cp_parser_lookup_name (parser, identifier,
13428                                         tag_type,
13429                                         /*is_template=*/false,
13430                                         /*is_namespace=*/false,
13431                                         check_dependency_p,
13432                                         &ambiguous_decls);
13433           if (ambiguous_decls)
13434             {
13435               error ("reference to %qD is ambiguous", identifier);
13436               print_candidates (ambiguous_decls);
13437               if (cp_parser_parsing_tentatively (parser))
13438                 {
13439                   identifier_token->ambiguous_p = true;
13440                   cp_parser_simulate_error (parser);
13441                 }
13442               return error_mark_node;
13443             }
13444         }
13445     }
13446   else
13447     {
13448       /* Try a template-id.  */
13449       decl = cp_parser_template_id (parser, template_keyword_p,
13450                                     check_dependency_p,
13451                                     is_declaration);
13452       if (decl == error_mark_node)
13453         return error_mark_node;
13454     }
13455
13456   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13457
13458   /* If this is a typename, create a TYPENAME_TYPE.  */
13459   if (typename_p && decl != error_mark_node)
13460     {
13461       decl = make_typename_type (scope, decl, typename_type,
13462                                  /*complain=*/tf_error);
13463       if (decl != error_mark_node)
13464         decl = TYPE_NAME (decl);
13465     }
13466
13467   /* Check to see that it is really the name of a class.  */
13468   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13469       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13470       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13471     /* Situations like this:
13472
13473          template <typename T> struct A {
13474            typename T::template X<int>::I i;
13475          };
13476
13477        are problematic.  Is `T::template X<int>' a class-name?  The
13478        standard does not seem to be definitive, but there is no other
13479        valid interpretation of the following `::'.  Therefore, those
13480        names are considered class-names.  */
13481     {
13482       decl = make_typename_type (scope, decl, tag_type, tf_error);
13483       if (decl != error_mark_node)
13484         decl = TYPE_NAME (decl);
13485     }
13486   else if (TREE_CODE (decl) != TYPE_DECL
13487            || TREE_TYPE (decl) == error_mark_node
13488            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13489     decl = error_mark_node;
13490
13491   if (decl == error_mark_node)
13492     cp_parser_error (parser, "expected class-name");
13493
13494   return decl;
13495 }
13496
13497 /* Parse a class-specifier.
13498
13499    class-specifier:
13500      class-head { member-specification [opt] }
13501
13502    Returns the TREE_TYPE representing the class.  */
13503
13504 static tree
13505 cp_parser_class_specifier (cp_parser* parser)
13506 {
13507   cp_token *token;
13508   tree type;
13509   tree attributes = NULL_TREE;
13510   int has_trailing_semicolon;
13511   bool nested_name_specifier_p;
13512   unsigned saved_num_template_parameter_lists;
13513   bool saved_in_function_body;
13514   tree old_scope = NULL_TREE;
13515   tree scope = NULL_TREE;
13516   tree bases;
13517
13518   push_deferring_access_checks (dk_no_deferred);
13519
13520   /* Parse the class-head.  */
13521   type = cp_parser_class_head (parser,
13522                                &nested_name_specifier_p,
13523                                &attributes,
13524                                &bases);
13525   /* If the class-head was a semantic disaster, skip the entire body
13526      of the class.  */
13527   if (!type)
13528     {
13529       cp_parser_skip_to_end_of_block_or_statement (parser);
13530       pop_deferring_access_checks ();
13531       return error_mark_node;
13532     }
13533
13534   /* Look for the `{'.  */
13535   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13536     {
13537       pop_deferring_access_checks ();
13538       return error_mark_node;
13539     }
13540
13541   /* Process the base classes. If they're invalid, skip the 
13542      entire class body.  */
13543   if (!xref_basetypes (type, bases))
13544     {
13545       cp_parser_skip_to_closing_brace (parser);
13546
13547       /* Consuming the closing brace yields better error messages
13548          later on.  */
13549       cp_lexer_consume_token (parser->lexer);
13550       pop_deferring_access_checks ();
13551       return error_mark_node;
13552     }
13553
13554   /* Issue an error message if type-definitions are forbidden here.  */
13555   cp_parser_check_type_definition (parser);
13556   /* Remember that we are defining one more class.  */
13557   ++parser->num_classes_being_defined;
13558   /* Inside the class, surrounding template-parameter-lists do not
13559      apply.  */
13560   saved_num_template_parameter_lists
13561     = parser->num_template_parameter_lists;
13562   parser->num_template_parameter_lists = 0;
13563   /* We are not in a function body.  */
13564   saved_in_function_body = parser->in_function_body;
13565   parser->in_function_body = false;
13566
13567   /* Start the class.  */
13568   if (nested_name_specifier_p)
13569     {
13570       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13571       old_scope = push_inner_scope (scope);
13572     }
13573   type = begin_class_definition (type, attributes);
13574
13575   if (type == error_mark_node)
13576     /* If the type is erroneous, skip the entire body of the class.  */
13577     cp_parser_skip_to_closing_brace (parser);
13578   else
13579     /* Parse the member-specification.  */
13580     cp_parser_member_specification_opt (parser);
13581
13582   /* Look for the trailing `}'.  */
13583   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13584   /* We get better error messages by noticing a common problem: a
13585      missing trailing `;'.  */
13586   token = cp_lexer_peek_token (parser->lexer);
13587   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13588   /* Look for trailing attributes to apply to this class.  */
13589   if (cp_parser_allow_gnu_extensions_p (parser))
13590     attributes = cp_parser_attributes_opt (parser);
13591   if (type != error_mark_node)
13592     type = finish_struct (type, attributes);
13593   if (nested_name_specifier_p)
13594     pop_inner_scope (old_scope, scope);
13595   /* If this class is not itself within the scope of another class,
13596      then we need to parse the bodies of all of the queued function
13597      definitions.  Note that the queued functions defined in a class
13598      are not always processed immediately following the
13599      class-specifier for that class.  Consider:
13600
13601        struct A {
13602          struct B { void f() { sizeof (A); } };
13603        };
13604
13605      If `f' were processed before the processing of `A' were
13606      completed, there would be no way to compute the size of `A'.
13607      Note that the nesting we are interested in here is lexical --
13608      not the semantic nesting given by TYPE_CONTEXT.  In particular,
13609      for:
13610
13611        struct A { struct B; };
13612        struct A::B { void f() { } };
13613
13614      there is no need to delay the parsing of `A::B::f'.  */
13615   if (--parser->num_classes_being_defined == 0)
13616     {
13617       tree queue_entry;
13618       tree fn;
13619       tree class_type = NULL_TREE;
13620       tree pushed_scope = NULL_TREE;
13621
13622       /* In a first pass, parse default arguments to the functions.
13623          Then, in a second pass, parse the bodies of the functions.
13624          This two-phased approach handles cases like:
13625
13626             struct S {
13627               void f() { g(); }
13628               void g(int i = 3);
13629             };
13630
13631          */
13632       for (TREE_PURPOSE (parser->unparsed_functions_queues)
13633              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13634            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13635            TREE_PURPOSE (parser->unparsed_functions_queues)
13636              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13637         {
13638           fn = TREE_VALUE (queue_entry);
13639           /* If there are default arguments that have not yet been processed,
13640              take care of them now.  */
13641           if (class_type != TREE_PURPOSE (queue_entry))
13642             {
13643               if (pushed_scope)
13644                 pop_scope (pushed_scope);
13645               class_type = TREE_PURPOSE (queue_entry);
13646               pushed_scope = push_scope (class_type);
13647             }
13648           /* Make sure that any template parameters are in scope.  */
13649           maybe_begin_member_template_processing (fn);
13650           /* Parse the default argument expressions.  */
13651           cp_parser_late_parsing_default_args (parser, fn);
13652           /* Remove any template parameters from the symbol table.  */
13653           maybe_end_member_template_processing ();
13654         }
13655       if (pushed_scope)
13656         pop_scope (pushed_scope);
13657       /* Now parse the body of the functions.  */
13658       for (TREE_VALUE (parser->unparsed_functions_queues)
13659              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13660            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13661            TREE_VALUE (parser->unparsed_functions_queues)
13662              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13663         {
13664           /* Figure out which function we need to process.  */
13665           fn = TREE_VALUE (queue_entry);
13666           /* Parse the function.  */
13667           cp_parser_late_parsing_for_member (parser, fn);
13668         }
13669     }
13670
13671   /* Put back any saved access checks.  */
13672   pop_deferring_access_checks ();
13673
13674   /* Restore saved state.  */
13675   parser->in_function_body = saved_in_function_body;
13676   parser->num_template_parameter_lists
13677     = saved_num_template_parameter_lists;
13678
13679   return type;
13680 }
13681
13682 /* Parse a class-head.
13683
13684    class-head:
13685      class-key identifier [opt] base-clause [opt]
13686      class-key nested-name-specifier identifier base-clause [opt]
13687      class-key nested-name-specifier [opt] template-id
13688        base-clause [opt]
13689
13690    GNU Extensions:
13691      class-key attributes identifier [opt] base-clause [opt]
13692      class-key attributes nested-name-specifier identifier base-clause [opt]
13693      class-key attributes nested-name-specifier [opt] template-id
13694        base-clause [opt]
13695
13696    Upon return BASES is initialized to the list of base classes (or
13697    NULL, if there are none) in the same form returned by
13698    cp_parser_base_clause.
13699
13700    Returns the TYPE of the indicated class.  Sets
13701    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13702    involving a nested-name-specifier was used, and FALSE otherwise.
13703
13704    Returns error_mark_node if this is not a class-head.
13705
13706    Returns NULL_TREE if the class-head is syntactically valid, but
13707    semantically invalid in a way that means we should skip the entire
13708    body of the class.  */
13709
13710 static tree
13711 cp_parser_class_head (cp_parser* parser,
13712                       bool* nested_name_specifier_p,
13713                       tree *attributes_p,
13714                       tree *bases)
13715 {
13716   tree nested_name_specifier;
13717   enum tag_types class_key;
13718   tree id = NULL_TREE;
13719   tree type = NULL_TREE;
13720   tree attributes;
13721   bool template_id_p = false;
13722   bool qualified_p = false;
13723   bool invalid_nested_name_p = false;
13724   bool invalid_explicit_specialization_p = false;
13725   tree pushed_scope = NULL_TREE;
13726   unsigned num_templates;
13727
13728   /* Assume no nested-name-specifier will be present.  */
13729   *nested_name_specifier_p = false;
13730   /* Assume no template parameter lists will be used in defining the
13731      type.  */
13732   num_templates = 0;
13733
13734   *bases = NULL_TREE;
13735
13736   /* Look for the class-key.  */
13737   class_key = cp_parser_class_key (parser);
13738   if (class_key == none_type)
13739     return error_mark_node;
13740
13741   /* Parse the attributes.  */
13742   attributes = cp_parser_attributes_opt (parser);
13743
13744   /* If the next token is `::', that is invalid -- but sometimes
13745      people do try to write:
13746
13747        struct ::S {};
13748
13749      Handle this gracefully by accepting the extra qualifier, and then
13750      issuing an error about it later if this really is a
13751      class-head.  If it turns out just to be an elaborated type
13752      specifier, remain silent.  */
13753   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13754     qualified_p = true;
13755
13756   push_deferring_access_checks (dk_no_check);
13757
13758   /* Determine the name of the class.  Begin by looking for an
13759      optional nested-name-specifier.  */
13760   nested_name_specifier
13761     = cp_parser_nested_name_specifier_opt (parser,
13762                                            /*typename_keyword_p=*/false,
13763                                            /*check_dependency_p=*/false,
13764                                            /*type_p=*/false,
13765                                            /*is_declaration=*/false);
13766   /* If there was a nested-name-specifier, then there *must* be an
13767      identifier.  */
13768   if (nested_name_specifier)
13769     {
13770       /* Although the grammar says `identifier', it really means
13771          `class-name' or `template-name'.  You are only allowed to
13772          define a class that has already been declared with this
13773          syntax.
13774
13775          The proposed resolution for Core Issue 180 says that wherever
13776          you see `class T::X' you should treat `X' as a type-name.
13777
13778          It is OK to define an inaccessible class; for example:
13779
13780            class A { class B; };
13781            class A::B {};
13782
13783          We do not know if we will see a class-name, or a
13784          template-name.  We look for a class-name first, in case the
13785          class-name is a template-id; if we looked for the
13786          template-name first we would stop after the template-name.  */
13787       cp_parser_parse_tentatively (parser);
13788       type = cp_parser_class_name (parser,
13789                                    /*typename_keyword_p=*/false,
13790                                    /*template_keyword_p=*/false,
13791                                    class_type,
13792                                    /*check_dependency_p=*/false,
13793                                    /*class_head_p=*/true,
13794                                    /*is_declaration=*/false);
13795       /* If that didn't work, ignore the nested-name-specifier.  */
13796       if (!cp_parser_parse_definitely (parser))
13797         {
13798           invalid_nested_name_p = true;
13799           id = cp_parser_identifier (parser);
13800           if (id == error_mark_node)
13801             id = NULL_TREE;
13802         }
13803       /* If we could not find a corresponding TYPE, treat this
13804          declaration like an unqualified declaration.  */
13805       if (type == error_mark_node)
13806         nested_name_specifier = NULL_TREE;
13807       /* Otherwise, count the number of templates used in TYPE and its
13808          containing scopes.  */
13809       else
13810         {
13811           tree scope;
13812
13813           for (scope = TREE_TYPE (type);
13814                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13815                scope = (TYPE_P (scope)
13816                         ? TYPE_CONTEXT (scope)
13817                         : DECL_CONTEXT (scope)))
13818             if (TYPE_P (scope)
13819                 && CLASS_TYPE_P (scope)
13820                 && CLASSTYPE_TEMPLATE_INFO (scope)
13821                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13822                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13823               ++num_templates;
13824         }
13825     }
13826   /* Otherwise, the identifier is optional.  */
13827   else
13828     {
13829       /* We don't know whether what comes next is a template-id,
13830          an identifier, or nothing at all.  */
13831       cp_parser_parse_tentatively (parser);
13832       /* Check for a template-id.  */
13833       id = cp_parser_template_id (parser,
13834                                   /*template_keyword_p=*/false,
13835                                   /*check_dependency_p=*/true,
13836                                   /*is_declaration=*/true);
13837       /* If that didn't work, it could still be an identifier.  */
13838       if (!cp_parser_parse_definitely (parser))
13839         {
13840           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13841             id = cp_parser_identifier (parser);
13842           else
13843             id = NULL_TREE;
13844         }
13845       else
13846         {
13847           template_id_p = true;
13848           ++num_templates;
13849         }
13850     }
13851
13852   pop_deferring_access_checks ();
13853
13854   if (id)
13855     cp_parser_check_for_invalid_template_id (parser, id);
13856
13857   /* If it's not a `:' or a `{' then we can't really be looking at a
13858      class-head, since a class-head only appears as part of a
13859      class-specifier.  We have to detect this situation before calling
13860      xref_tag, since that has irreversible side-effects.  */
13861   if (!cp_parser_next_token_starts_class_definition_p (parser))
13862     {
13863       cp_parser_error (parser, "expected %<{%> or %<:%>");
13864       return error_mark_node;
13865     }
13866
13867   /* At this point, we're going ahead with the class-specifier, even
13868      if some other problem occurs.  */
13869   cp_parser_commit_to_tentative_parse (parser);
13870   /* Issue the error about the overly-qualified name now.  */
13871   if (qualified_p)
13872     cp_parser_error (parser,
13873                      "global qualification of class name is invalid");
13874   else if (invalid_nested_name_p)
13875     cp_parser_error (parser,
13876                      "qualified name does not name a class");
13877   else if (nested_name_specifier)
13878     {
13879       tree scope;
13880
13881       /* Reject typedef-names in class heads.  */
13882       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13883         {
13884           error ("invalid class name in declaration of %qD", type);
13885           type = NULL_TREE;
13886           goto done;
13887         }
13888
13889       /* Figure out in what scope the declaration is being placed.  */
13890       scope = current_scope ();
13891       /* If that scope does not contain the scope in which the
13892          class was originally declared, the program is invalid.  */
13893       if (scope && !is_ancestor (scope, nested_name_specifier))
13894         {
13895           error ("declaration of %qD in %qD which does not enclose %qD",
13896                  type, scope, nested_name_specifier);
13897           type = NULL_TREE;
13898           goto done;
13899         }
13900       /* [dcl.meaning]
13901
13902          A declarator-id shall not be qualified exception of the
13903          definition of a ... nested class outside of its class
13904          ... [or] a the definition or explicit instantiation of a
13905          class member of a namespace outside of its namespace.  */
13906       if (scope == nested_name_specifier)
13907         {
13908           pedwarn ("extra qualification ignored");
13909           nested_name_specifier = NULL_TREE;
13910           num_templates = 0;
13911         }
13912     }
13913   /* An explicit-specialization must be preceded by "template <>".  If
13914      it is not, try to recover gracefully.  */
13915   if (at_namespace_scope_p ()
13916       && parser->num_template_parameter_lists == 0
13917       && template_id_p)
13918     {
13919       error ("an explicit specialization must be preceded by %<template <>%>");
13920       invalid_explicit_specialization_p = true;
13921       /* Take the same action that would have been taken by
13922          cp_parser_explicit_specialization.  */
13923       ++parser->num_template_parameter_lists;
13924       begin_specialization ();
13925     }
13926   /* There must be no "return" statements between this point and the
13927      end of this function; set "type "to the correct return value and
13928      use "goto done;" to return.  */
13929   /* Make sure that the right number of template parameters were
13930      present.  */
13931   if (!cp_parser_check_template_parameters (parser, num_templates))
13932     {
13933       /* If something went wrong, there is no point in even trying to
13934          process the class-definition.  */
13935       type = NULL_TREE;
13936       goto done;
13937     }
13938
13939   /* Look up the type.  */
13940   if (template_id_p)
13941     {
13942       type = TREE_TYPE (id);
13943       type = maybe_process_partial_specialization (type);
13944       if (nested_name_specifier)
13945         pushed_scope = push_scope (nested_name_specifier);
13946     }
13947   else if (nested_name_specifier)
13948     {
13949       tree class_type;
13950
13951       /* Given:
13952
13953             template <typename T> struct S { struct T };
13954             template <typename T> struct S<T>::T { };
13955
13956          we will get a TYPENAME_TYPE when processing the definition of
13957          `S::T'.  We need to resolve it to the actual type before we
13958          try to define it.  */
13959       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13960         {
13961           class_type = resolve_typename_type (TREE_TYPE (type),
13962                                               /*only_current_p=*/false);
13963           if (class_type != error_mark_node)
13964             type = TYPE_NAME (class_type);
13965           else
13966             {
13967               cp_parser_error (parser, "could not resolve typename type");
13968               type = error_mark_node;
13969             }
13970         }
13971
13972       maybe_process_partial_specialization (TREE_TYPE (type));
13973       class_type = current_class_type;
13974       /* Enter the scope indicated by the nested-name-specifier.  */
13975       pushed_scope = push_scope (nested_name_specifier);
13976       /* Get the canonical version of this type.  */
13977       type = TYPE_MAIN_DECL (TREE_TYPE (type));
13978       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13979           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13980         {
13981           type = push_template_decl (type);
13982           if (type == error_mark_node)
13983             {
13984               type = NULL_TREE;
13985               goto done;
13986             }
13987         }
13988
13989       type = TREE_TYPE (type);
13990       *nested_name_specifier_p = true;
13991     }
13992   else      /* The name is not a nested name.  */
13993     {
13994       /* If the class was unnamed, create a dummy name.  */
13995       if (!id)
13996         id = make_anon_name ();
13997       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13998                        parser->num_template_parameter_lists);
13999     }
14000
14001   /* Indicate whether this class was declared as a `class' or as a
14002      `struct'.  */
14003   if (TREE_CODE (type) == RECORD_TYPE)
14004     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14005   cp_parser_check_class_key (class_key, type);
14006
14007   /* If this type was already complete, and we see another definition,
14008      that's an error.  */
14009   if (type != error_mark_node && COMPLETE_TYPE_P (type))
14010     {
14011       error ("redefinition of %q#T", type);
14012       error ("previous definition of %q+#T", type);
14013       type = NULL_TREE;
14014       goto done;
14015     }
14016   else if (type == error_mark_node)
14017     type = NULL_TREE;
14018
14019   /* We will have entered the scope containing the class; the names of
14020      base classes should be looked up in that context.  For example:
14021
14022        struct A { struct B {}; struct C; };
14023        struct A::C : B {};
14024
14025      is valid.  */
14026
14027   /* Get the list of base-classes, if there is one.  */
14028   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14029     *bases = cp_parser_base_clause (parser);
14030
14031  done:
14032   /* Leave the scope given by the nested-name-specifier.  We will
14033      enter the class scope itself while processing the members.  */
14034   if (pushed_scope)
14035     pop_scope (pushed_scope);
14036
14037   if (invalid_explicit_specialization_p)
14038     {
14039       end_specialization ();
14040       --parser->num_template_parameter_lists;
14041     }
14042   *attributes_p = attributes;
14043   return type;
14044 }
14045
14046 /* Parse a class-key.
14047
14048    class-key:
14049      class
14050      struct
14051      union
14052
14053    Returns the kind of class-key specified, or none_type to indicate
14054    error.  */
14055
14056 static enum tag_types
14057 cp_parser_class_key (cp_parser* parser)
14058 {
14059   cp_token *token;
14060   enum tag_types tag_type;
14061
14062   /* Look for the class-key.  */
14063   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14064   if (!token)
14065     return none_type;
14066
14067   /* Check to see if the TOKEN is a class-key.  */
14068   tag_type = cp_parser_token_is_class_key (token);
14069   if (!tag_type)
14070     cp_parser_error (parser, "expected class-key");
14071   return tag_type;
14072 }
14073
14074 /* Parse an (optional) member-specification.
14075
14076    member-specification:
14077      member-declaration member-specification [opt]
14078      access-specifier : member-specification [opt]  */
14079
14080 static void
14081 cp_parser_member_specification_opt (cp_parser* parser)
14082 {
14083   while (true)
14084     {
14085       cp_token *token;
14086       enum rid keyword;
14087
14088       /* Peek at the next token.  */
14089       token = cp_lexer_peek_token (parser->lexer);
14090       /* If it's a `}', or EOF then we've seen all the members.  */
14091       if (token->type == CPP_CLOSE_BRACE
14092           || token->type == CPP_EOF
14093           || token->type == CPP_PRAGMA_EOL)
14094         break;
14095
14096       /* See if this token is a keyword.  */
14097       keyword = token->keyword;
14098       switch (keyword)
14099         {
14100         case RID_PUBLIC:
14101         case RID_PROTECTED:
14102         case RID_PRIVATE:
14103           /* Consume the access-specifier.  */
14104           cp_lexer_consume_token (parser->lexer);
14105           /* Remember which access-specifier is active.  */
14106           current_access_specifier = token->u.value;
14107           /* Look for the `:'.  */
14108           cp_parser_require (parser, CPP_COLON, "`:'");
14109           break;
14110
14111         default:
14112           /* Accept #pragmas at class scope.  */
14113           if (token->type == CPP_PRAGMA)
14114             {
14115               cp_parser_pragma (parser, pragma_external);
14116               break;
14117             }
14118
14119           /* Otherwise, the next construction must be a
14120              member-declaration.  */
14121           cp_parser_member_declaration (parser);
14122         }
14123     }
14124 }
14125
14126 /* Parse a member-declaration.
14127
14128    member-declaration:
14129      decl-specifier-seq [opt] member-declarator-list [opt] ;
14130      function-definition ; [opt]
14131      :: [opt] nested-name-specifier template [opt] unqualified-id ;
14132      using-declaration
14133      template-declaration
14134
14135    member-declarator-list:
14136      member-declarator
14137      member-declarator-list , member-declarator
14138
14139    member-declarator:
14140      declarator pure-specifier [opt]
14141      declarator constant-initializer [opt]
14142      identifier [opt] : constant-expression
14143
14144    GNU Extensions:
14145
14146    member-declaration:
14147      __extension__ member-declaration
14148
14149    member-declarator:
14150      declarator attributes [opt] pure-specifier [opt]
14151      declarator attributes [opt] constant-initializer [opt]
14152      identifier [opt] attributes [opt] : constant-expression  
14153
14154    C++0x Extensions:
14155
14156    member-declaration:
14157      static_assert-declaration  */
14158
14159 static void
14160 cp_parser_member_declaration (cp_parser* parser)
14161 {
14162   cp_decl_specifier_seq decl_specifiers;
14163   tree prefix_attributes;
14164   tree decl;
14165   int declares_class_or_enum;
14166   bool friend_p;
14167   cp_token *token;
14168   int saved_pedantic;
14169
14170   /* Check for the `__extension__' keyword.  */
14171   if (cp_parser_extension_opt (parser, &saved_pedantic))
14172     {
14173       /* Recurse.  */
14174       cp_parser_member_declaration (parser);
14175       /* Restore the old value of the PEDANTIC flag.  */
14176       pedantic = saved_pedantic;
14177
14178       return;
14179     }
14180
14181   /* Check for a template-declaration.  */
14182   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14183     {
14184       /* An explicit specialization here is an error condition, and we
14185          expect the specialization handler to detect and report this.  */
14186       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14187           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14188         cp_parser_explicit_specialization (parser);
14189       else
14190         cp_parser_template_declaration (parser, /*member_p=*/true);
14191
14192       return;
14193     }
14194
14195   /* Check for a using-declaration.  */
14196   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14197     {
14198       /* Parse the using-declaration.  */
14199       cp_parser_using_declaration (parser,
14200                                    /*access_declaration_p=*/false);
14201       return;
14202     }
14203
14204   /* Check for @defs.  */
14205   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14206     {
14207       tree ivar, member;
14208       tree ivar_chains = cp_parser_objc_defs_expression (parser);
14209       ivar = ivar_chains;
14210       while (ivar)
14211         {
14212           member = ivar;
14213           ivar = TREE_CHAIN (member);
14214           TREE_CHAIN (member) = NULL_TREE;
14215           finish_member_declaration (member);
14216         }
14217       return;
14218     }
14219
14220   /* If the next token is `static_assert' we have a static assertion.  */
14221   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14222     {
14223       cp_parser_static_assert (parser, /*member_p=*/true);
14224       return;
14225     }
14226
14227   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14228     return;
14229
14230   /* Parse the decl-specifier-seq.  */
14231   cp_parser_decl_specifier_seq (parser,
14232                                 CP_PARSER_FLAGS_OPTIONAL,
14233                                 &decl_specifiers,
14234                                 &declares_class_or_enum);
14235   prefix_attributes = decl_specifiers.attributes;
14236   decl_specifiers.attributes = NULL_TREE;
14237   /* Check for an invalid type-name.  */
14238   if (!decl_specifiers.type
14239       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14240     return;
14241   /* If there is no declarator, then the decl-specifier-seq should
14242      specify a type.  */
14243   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14244     {
14245       /* If there was no decl-specifier-seq, and the next token is a
14246          `;', then we have something like:
14247
14248            struct S { ; };
14249
14250          [class.mem]
14251
14252          Each member-declaration shall declare at least one member
14253          name of the class.  */
14254       if (!decl_specifiers.any_specifiers_p)
14255         {
14256           cp_token *token = cp_lexer_peek_token (parser->lexer);
14257           if (pedantic && !token->in_system_header)
14258             pedwarn ("%Hextra %<;%>", &token->location);
14259         }
14260       else
14261         {
14262           tree type;
14263
14264           /* See if this declaration is a friend.  */
14265           friend_p = cp_parser_friend_p (&decl_specifiers);
14266           /* If there were decl-specifiers, check to see if there was
14267              a class-declaration.  */
14268           type = check_tag_decl (&decl_specifiers);
14269           /* Nested classes have already been added to the class, but
14270              a `friend' needs to be explicitly registered.  */
14271           if (friend_p)
14272             {
14273               /* If the `friend' keyword was present, the friend must
14274                  be introduced with a class-key.  */
14275                if (!declares_class_or_enum)
14276                  error ("a class-key must be used when declaring a friend");
14277                /* In this case:
14278
14279                     template <typename T> struct A {
14280                       friend struct A<T>::B;
14281                     };
14282
14283                   A<T>::B will be represented by a TYPENAME_TYPE, and
14284                   therefore not recognized by check_tag_decl.  */
14285                if (!type
14286                    && decl_specifiers.type
14287                    && TYPE_P (decl_specifiers.type))
14288                  type = decl_specifiers.type;
14289                if (!type || !TYPE_P (type))
14290                  error ("friend declaration does not name a class or "
14291                         "function");
14292                else
14293                  make_friend_class (current_class_type, type,
14294                                     /*complain=*/true);
14295             }
14296           /* If there is no TYPE, an error message will already have
14297              been issued.  */
14298           else if (!type || type == error_mark_node)
14299             ;
14300           /* An anonymous aggregate has to be handled specially; such
14301              a declaration really declares a data member (with a
14302              particular type), as opposed to a nested class.  */
14303           else if (ANON_AGGR_TYPE_P (type))
14304             {
14305               /* Remove constructors and such from TYPE, now that we
14306                  know it is an anonymous aggregate.  */
14307               fixup_anonymous_aggr (type);
14308               /* And make the corresponding data member.  */
14309               decl = build_decl (FIELD_DECL, NULL_TREE, type);
14310               /* Add it to the class.  */
14311               finish_member_declaration (decl);
14312             }
14313           else
14314             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14315         }
14316     }
14317   else
14318     {
14319       /* See if these declarations will be friends.  */
14320       friend_p = cp_parser_friend_p (&decl_specifiers);
14321
14322       /* Keep going until we hit the `;' at the end of the
14323          declaration.  */
14324       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14325         {
14326           tree attributes = NULL_TREE;
14327           tree first_attribute;
14328
14329           /* Peek at the next token.  */
14330           token = cp_lexer_peek_token (parser->lexer);
14331
14332           /* Check for a bitfield declaration.  */
14333           if (token->type == CPP_COLON
14334               || (token->type == CPP_NAME
14335                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14336                   == CPP_COLON))
14337             {
14338               tree identifier;
14339               tree width;
14340
14341               /* Get the name of the bitfield.  Note that we cannot just
14342                  check TOKEN here because it may have been invalidated by
14343                  the call to cp_lexer_peek_nth_token above.  */
14344               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14345                 identifier = cp_parser_identifier (parser);
14346               else
14347                 identifier = NULL_TREE;
14348
14349               /* Consume the `:' token.  */
14350               cp_lexer_consume_token (parser->lexer);
14351               /* Get the width of the bitfield.  */
14352               width
14353                 = cp_parser_constant_expression (parser,
14354                                                  /*allow_non_constant=*/false,
14355                                                  NULL);
14356
14357               /* Look for attributes that apply to the bitfield.  */
14358               attributes = cp_parser_attributes_opt (parser);
14359               /* Remember which attributes are prefix attributes and
14360                  which are not.  */
14361               first_attribute = attributes;
14362               /* Combine the attributes.  */
14363               attributes = chainon (prefix_attributes, attributes);
14364
14365               /* Create the bitfield declaration.  */
14366               decl = grokbitfield (identifier
14367                                    ? make_id_declarator (NULL_TREE,
14368                                                          identifier,
14369                                                          sfk_none)
14370                                    : NULL,
14371                                    &decl_specifiers,
14372                                    width);
14373               /* Apply the attributes.  */
14374               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14375             }
14376           else
14377             {
14378               cp_declarator *declarator;
14379               tree initializer;
14380               tree asm_specification;
14381               int ctor_dtor_or_conv_p;
14382
14383               /* Parse the declarator.  */
14384               declarator
14385                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14386                                         &ctor_dtor_or_conv_p,
14387                                         /*parenthesized_p=*/NULL,
14388                                         /*member_p=*/true);
14389
14390               /* If something went wrong parsing the declarator, make sure
14391                  that we at least consume some tokens.  */
14392               if (declarator == cp_error_declarator)
14393                 {
14394                   /* Skip to the end of the statement.  */
14395                   cp_parser_skip_to_end_of_statement (parser);
14396                   /* If the next token is not a semicolon, that is
14397                      probably because we just skipped over the body of
14398                      a function.  So, we consume a semicolon if
14399                      present, but do not issue an error message if it
14400                      is not present.  */
14401                   if (cp_lexer_next_token_is (parser->lexer,
14402                                               CPP_SEMICOLON))
14403                     cp_lexer_consume_token (parser->lexer);
14404                   return;
14405                 }
14406
14407               if (declares_class_or_enum & 2)
14408                 cp_parser_check_for_definition_in_return_type
14409                   (declarator, decl_specifiers.type);
14410
14411               /* Look for an asm-specification.  */
14412               asm_specification = cp_parser_asm_specification_opt (parser);
14413               /* Look for attributes that apply to the declaration.  */
14414               attributes = cp_parser_attributes_opt (parser);
14415               /* Remember which attributes are prefix attributes and
14416                  which are not.  */
14417               first_attribute = attributes;
14418               /* Combine the attributes.  */
14419               attributes = chainon (prefix_attributes, attributes);
14420
14421               /* If it's an `=', then we have a constant-initializer or a
14422                  pure-specifier.  It is not correct to parse the
14423                  initializer before registering the member declaration
14424                  since the member declaration should be in scope while
14425                  its initializer is processed.  However, the rest of the
14426                  front end does not yet provide an interface that allows
14427                  us to handle this correctly.  */
14428               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14429                 {
14430                   /* In [class.mem]:
14431
14432                      A pure-specifier shall be used only in the declaration of
14433                      a virtual function.
14434
14435                      A member-declarator can contain a constant-initializer
14436                      only if it declares a static member of integral or
14437                      enumeration type.
14438
14439                      Therefore, if the DECLARATOR is for a function, we look
14440                      for a pure-specifier; otherwise, we look for a
14441                      constant-initializer.  When we call `grokfield', it will
14442                      perform more stringent semantics checks.  */
14443                   if (function_declarator_p (declarator))
14444                     initializer = cp_parser_pure_specifier (parser);
14445                   else
14446                     /* Parse the initializer.  */
14447                     initializer = cp_parser_constant_initializer (parser);
14448                 }
14449               /* Otherwise, there is no initializer.  */
14450               else
14451                 initializer = NULL_TREE;
14452
14453               /* See if we are probably looking at a function
14454                  definition.  We are certainly not looking at a
14455                  member-declarator.  Calling `grokfield' has
14456                  side-effects, so we must not do it unless we are sure
14457                  that we are looking at a member-declarator.  */
14458               if (cp_parser_token_starts_function_definition_p
14459                   (cp_lexer_peek_token (parser->lexer)))
14460                 {
14461                   /* The grammar does not allow a pure-specifier to be
14462                      used when a member function is defined.  (It is
14463                      possible that this fact is an oversight in the
14464                      standard, since a pure function may be defined
14465                      outside of the class-specifier.  */
14466                   if (initializer)
14467                     error ("pure-specifier on function-definition");
14468                   decl = cp_parser_save_member_function_body (parser,
14469                                                               &decl_specifiers,
14470                                                               declarator,
14471                                                               attributes);
14472                   /* If the member was not a friend, declare it here.  */
14473                   if (!friend_p)
14474                     finish_member_declaration (decl);
14475                   /* Peek at the next token.  */
14476                   token = cp_lexer_peek_token (parser->lexer);
14477                   /* If the next token is a semicolon, consume it.  */
14478                   if (token->type == CPP_SEMICOLON)
14479                     cp_lexer_consume_token (parser->lexer);
14480                   return;
14481                 }
14482               else
14483                 /* Create the declaration.  */
14484                 decl = grokfield (declarator, &decl_specifiers,
14485                                   initializer, /*init_const_expr_p=*/true,
14486                                   asm_specification,
14487                                   attributes);
14488             }
14489
14490           /* Reset PREFIX_ATTRIBUTES.  */
14491           while (attributes && TREE_CHAIN (attributes) != first_attribute)
14492             attributes = TREE_CHAIN (attributes);
14493           if (attributes)
14494             TREE_CHAIN (attributes) = NULL_TREE;
14495
14496           /* If there is any qualification still in effect, clear it
14497              now; we will be starting fresh with the next declarator.  */
14498           parser->scope = NULL_TREE;
14499           parser->qualifying_scope = NULL_TREE;
14500           parser->object_scope = NULL_TREE;
14501           /* If it's a `,', then there are more declarators.  */
14502           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14503             cp_lexer_consume_token (parser->lexer);
14504           /* If the next token isn't a `;', then we have a parse error.  */
14505           else if (cp_lexer_next_token_is_not (parser->lexer,
14506                                                CPP_SEMICOLON))
14507             {
14508               cp_parser_error (parser, "expected %<;%>");
14509               /* Skip tokens until we find a `;'.  */
14510               cp_parser_skip_to_end_of_statement (parser);
14511
14512               break;
14513             }
14514
14515           if (decl)
14516             {
14517               /* Add DECL to the list of members.  */
14518               if (!friend_p)
14519                 finish_member_declaration (decl);
14520
14521               if (TREE_CODE (decl) == FUNCTION_DECL)
14522                 cp_parser_save_default_args (parser, decl);
14523             }
14524         }
14525     }
14526
14527   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14528 }
14529
14530 /* Parse a pure-specifier.
14531
14532    pure-specifier:
14533      = 0
14534
14535    Returns INTEGER_ZERO_NODE if a pure specifier is found.
14536    Otherwise, ERROR_MARK_NODE is returned.  */
14537
14538 static tree
14539 cp_parser_pure_specifier (cp_parser* parser)
14540 {
14541   cp_token *token;
14542
14543   /* Look for the `=' token.  */
14544   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14545     return error_mark_node;
14546   /* Look for the `0' token.  */
14547   token = cp_lexer_consume_token (parser->lexer);
14548   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
14549   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14550     {
14551       cp_parser_error (parser,
14552                        "invalid pure specifier (only `= 0' is allowed)");
14553       cp_parser_skip_to_end_of_statement (parser);
14554       return error_mark_node;
14555     }
14556   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14557     {
14558       error ("templates may not be %<virtual%>");
14559       return error_mark_node;
14560     }
14561
14562   return integer_zero_node;
14563 }
14564
14565 /* Parse a constant-initializer.
14566
14567    constant-initializer:
14568      = constant-expression
14569
14570    Returns a representation of the constant-expression.  */
14571
14572 static tree
14573 cp_parser_constant_initializer (cp_parser* parser)
14574 {
14575   /* Look for the `=' token.  */
14576   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14577     return error_mark_node;
14578
14579   /* It is invalid to write:
14580
14581        struct S { static const int i = { 7 }; };
14582
14583      */
14584   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14585     {
14586       cp_parser_error (parser,
14587                        "a brace-enclosed initializer is not allowed here");
14588       /* Consume the opening brace.  */
14589       cp_lexer_consume_token (parser->lexer);
14590       /* Skip the initializer.  */
14591       cp_parser_skip_to_closing_brace (parser);
14592       /* Look for the trailing `}'.  */
14593       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14594
14595       return error_mark_node;
14596     }
14597
14598   return cp_parser_constant_expression (parser,
14599                                         /*allow_non_constant=*/false,
14600                                         NULL);
14601 }
14602
14603 /* Derived classes [gram.class.derived] */
14604
14605 /* Parse a base-clause.
14606
14607    base-clause:
14608      : base-specifier-list
14609
14610    base-specifier-list:
14611      base-specifier ... [opt]
14612      base-specifier-list , base-specifier ... [opt]
14613
14614    Returns a TREE_LIST representing the base-classes, in the order in
14615    which they were declared.  The representation of each node is as
14616    described by cp_parser_base_specifier.
14617
14618    In the case that no bases are specified, this function will return
14619    NULL_TREE, not ERROR_MARK_NODE.  */
14620
14621 static tree
14622 cp_parser_base_clause (cp_parser* parser)
14623 {
14624   tree bases = NULL_TREE;
14625
14626   /* Look for the `:' that begins the list.  */
14627   cp_parser_require (parser, CPP_COLON, "`:'");
14628
14629   /* Scan the base-specifier-list.  */
14630   while (true)
14631     {
14632       cp_token *token;
14633       tree base;
14634       bool pack_expansion_p = false;
14635
14636       /* Look for the base-specifier.  */
14637       base = cp_parser_base_specifier (parser);
14638       /* Look for the (optional) ellipsis. */
14639       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14640         {
14641           /* Consume the `...'. */
14642           cp_lexer_consume_token (parser->lexer);
14643
14644           pack_expansion_p = true;
14645         }
14646
14647       /* Add BASE to the front of the list.  */
14648       if (base != error_mark_node)
14649         {
14650           if (pack_expansion_p)
14651             /* Make this a pack expansion type. */
14652             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
14653           else
14654             check_for_bare_parameter_packs (TREE_VALUE (base));
14655
14656           TREE_CHAIN (base) = bases;
14657           bases = base;
14658         }
14659       /* Peek at the next token.  */
14660       token = cp_lexer_peek_token (parser->lexer);
14661       /* If it's not a comma, then the list is complete.  */
14662       if (token->type != CPP_COMMA)
14663         break;
14664       /* Consume the `,'.  */
14665       cp_lexer_consume_token (parser->lexer);
14666     }
14667
14668   /* PARSER->SCOPE may still be non-NULL at this point, if the last
14669      base class had a qualified name.  However, the next name that
14670      appears is certainly not qualified.  */
14671   parser->scope = NULL_TREE;
14672   parser->qualifying_scope = NULL_TREE;
14673   parser->object_scope = NULL_TREE;
14674
14675   return nreverse (bases);
14676 }
14677
14678 /* Parse a base-specifier.
14679
14680    base-specifier:
14681      :: [opt] nested-name-specifier [opt] class-name
14682      virtual access-specifier [opt] :: [opt] nested-name-specifier
14683        [opt] class-name
14684      access-specifier virtual [opt] :: [opt] nested-name-specifier
14685        [opt] class-name
14686
14687    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14688    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14689    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14690    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14691
14692 static tree
14693 cp_parser_base_specifier (cp_parser* parser)
14694 {
14695   cp_token *token;
14696   bool done = false;
14697   bool virtual_p = false;
14698   bool duplicate_virtual_error_issued_p = false;
14699   bool duplicate_access_error_issued_p = false;
14700   bool class_scope_p, template_p;
14701   tree access = access_default_node;
14702   tree type;
14703
14704   /* Process the optional `virtual' and `access-specifier'.  */
14705   while (!done)
14706     {
14707       /* Peek at the next token.  */
14708       token = cp_lexer_peek_token (parser->lexer);
14709       /* Process `virtual'.  */
14710       switch (token->keyword)
14711         {
14712         case RID_VIRTUAL:
14713           /* If `virtual' appears more than once, issue an error.  */
14714           if (virtual_p && !duplicate_virtual_error_issued_p)
14715             {
14716               cp_parser_error (parser,
14717                                "%<virtual%> specified more than once in base-specified");
14718               duplicate_virtual_error_issued_p = true;
14719             }
14720
14721           virtual_p = true;
14722
14723           /* Consume the `virtual' token.  */
14724           cp_lexer_consume_token (parser->lexer);
14725
14726           break;
14727
14728         case RID_PUBLIC:
14729         case RID_PROTECTED:
14730         case RID_PRIVATE:
14731           /* If more than one access specifier appears, issue an
14732              error.  */
14733           if (access != access_default_node
14734               && !duplicate_access_error_issued_p)
14735             {
14736               cp_parser_error (parser,
14737                                "more than one access specifier in base-specified");
14738               duplicate_access_error_issued_p = true;
14739             }
14740
14741           access = ridpointers[(int) token->keyword];
14742
14743           /* Consume the access-specifier.  */
14744           cp_lexer_consume_token (parser->lexer);
14745
14746           break;
14747
14748         default:
14749           done = true;
14750           break;
14751         }
14752     }
14753   /* It is not uncommon to see programs mechanically, erroneously, use
14754      the 'typename' keyword to denote (dependent) qualified types
14755      as base classes.  */
14756   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14757     {
14758       if (!processing_template_decl)
14759         error ("keyword %<typename%> not allowed outside of templates");
14760       else
14761         error ("keyword %<typename%> not allowed in this context "
14762                "(the base class is implicitly a type)");
14763       cp_lexer_consume_token (parser->lexer);
14764     }
14765
14766   /* Look for the optional `::' operator.  */
14767   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14768   /* Look for the nested-name-specifier.  The simplest way to
14769      implement:
14770
14771        [temp.res]
14772
14773        The keyword `typename' is not permitted in a base-specifier or
14774        mem-initializer; in these contexts a qualified name that
14775        depends on a template-parameter is implicitly assumed to be a
14776        type name.
14777
14778      is to pretend that we have seen the `typename' keyword at this
14779      point.  */
14780   cp_parser_nested_name_specifier_opt (parser,
14781                                        /*typename_keyword_p=*/true,
14782                                        /*check_dependency_p=*/true,
14783                                        typename_type,
14784                                        /*is_declaration=*/true);
14785   /* If the base class is given by a qualified name, assume that names
14786      we see are type names or templates, as appropriate.  */
14787   class_scope_p = (parser->scope && TYPE_P (parser->scope));
14788   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14789
14790   /* Finally, look for the class-name.  */
14791   type = cp_parser_class_name (parser,
14792                                class_scope_p,
14793                                template_p,
14794                                typename_type,
14795                                /*check_dependency_p=*/true,
14796                                /*class_head_p=*/false,
14797                                /*is_declaration=*/true);
14798
14799   if (type == error_mark_node)
14800     return error_mark_node;
14801
14802   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14803 }
14804
14805 /* Exception handling [gram.exception] */
14806
14807 /* Parse an (optional) exception-specification.
14808
14809    exception-specification:
14810      throw ( type-id-list [opt] )
14811
14812    Returns a TREE_LIST representing the exception-specification.  The
14813    TREE_VALUE of each node is a type.  */
14814
14815 static tree
14816 cp_parser_exception_specification_opt (cp_parser* parser)
14817 {
14818   cp_token *token;
14819   tree type_id_list;
14820
14821   /* Peek at the next token.  */
14822   token = cp_lexer_peek_token (parser->lexer);
14823   /* If it's not `throw', then there's no exception-specification.  */
14824   if (!cp_parser_is_keyword (token, RID_THROW))
14825     return NULL_TREE;
14826
14827   /* Consume the `throw'.  */
14828   cp_lexer_consume_token (parser->lexer);
14829
14830   /* Look for the `('.  */
14831   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14832
14833   /* Peek at the next token.  */
14834   token = cp_lexer_peek_token (parser->lexer);
14835   /* If it's not a `)', then there is a type-id-list.  */
14836   if (token->type != CPP_CLOSE_PAREN)
14837     {
14838       const char *saved_message;
14839
14840       /* Types may not be defined in an exception-specification.  */
14841       saved_message = parser->type_definition_forbidden_message;
14842       parser->type_definition_forbidden_message
14843         = "types may not be defined in an exception-specification";
14844       /* Parse the type-id-list.  */
14845       type_id_list = cp_parser_type_id_list (parser);
14846       /* Restore the saved message.  */
14847       parser->type_definition_forbidden_message = saved_message;
14848     }
14849   else
14850     type_id_list = empty_except_spec;
14851
14852   /* Look for the `)'.  */
14853   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14854
14855   return type_id_list;
14856 }
14857
14858 /* Parse an (optional) type-id-list.
14859
14860    type-id-list:
14861      type-id ... [opt]
14862      type-id-list , type-id ... [opt]
14863
14864    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14865    in the order that the types were presented.  */
14866
14867 static tree
14868 cp_parser_type_id_list (cp_parser* parser)
14869 {
14870   tree types = NULL_TREE;
14871
14872   while (true)
14873     {
14874       cp_token *token;
14875       tree type;
14876
14877       /* Get the next type-id.  */
14878       type = cp_parser_type_id (parser);
14879       /* Parse the optional ellipsis. */
14880       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14881         {
14882           /* Consume the `...'. */
14883           cp_lexer_consume_token (parser->lexer);
14884
14885           /* Turn the type into a pack expansion expression. */
14886           type = make_pack_expansion (type);
14887         }
14888       /* Add it to the list.  */
14889       types = add_exception_specifier (types, type, /*complain=*/1);
14890       /* Peek at the next token.  */
14891       token = cp_lexer_peek_token (parser->lexer);
14892       /* If it is not a `,', we are done.  */
14893       if (token->type != CPP_COMMA)
14894         break;
14895       /* Consume the `,'.  */
14896       cp_lexer_consume_token (parser->lexer);
14897     }
14898
14899   return nreverse (types);
14900 }
14901
14902 /* Parse a try-block.
14903
14904    try-block:
14905      try compound-statement handler-seq  */
14906
14907 static tree
14908 cp_parser_try_block (cp_parser* parser)
14909 {
14910   tree try_block;
14911
14912   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14913   try_block = begin_try_block ();
14914   cp_parser_compound_statement (parser, NULL, true);
14915   finish_try_block (try_block);
14916   cp_parser_handler_seq (parser);
14917   finish_handler_sequence (try_block);
14918
14919   return try_block;
14920 }
14921
14922 /* Parse a function-try-block.
14923
14924    function-try-block:
14925      try ctor-initializer [opt] function-body handler-seq  */
14926
14927 static bool
14928 cp_parser_function_try_block (cp_parser* parser)
14929 {
14930   tree compound_stmt;
14931   tree try_block;
14932   bool ctor_initializer_p;
14933
14934   /* Look for the `try' keyword.  */
14935   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14936     return false;
14937   /* Let the rest of the front end know where we are.  */
14938   try_block = begin_function_try_block (&compound_stmt);
14939   /* Parse the function-body.  */
14940   ctor_initializer_p
14941     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14942   /* We're done with the `try' part.  */
14943   finish_function_try_block (try_block);
14944   /* Parse the handlers.  */
14945   cp_parser_handler_seq (parser);
14946   /* We're done with the handlers.  */
14947   finish_function_handler_sequence (try_block, compound_stmt);
14948
14949   return ctor_initializer_p;
14950 }
14951
14952 /* Parse a handler-seq.
14953
14954    handler-seq:
14955      handler handler-seq [opt]  */
14956
14957 static void
14958 cp_parser_handler_seq (cp_parser* parser)
14959 {
14960   while (true)
14961     {
14962       cp_token *token;
14963
14964       /* Parse the handler.  */
14965       cp_parser_handler (parser);
14966       /* Peek at the next token.  */
14967       token = cp_lexer_peek_token (parser->lexer);
14968       /* If it's not `catch' then there are no more handlers.  */
14969       if (!cp_parser_is_keyword (token, RID_CATCH))
14970         break;
14971     }
14972 }
14973
14974 /* Parse a handler.
14975
14976    handler:
14977      catch ( exception-declaration ) compound-statement  */
14978
14979 static void
14980 cp_parser_handler (cp_parser* parser)
14981 {
14982   tree handler;
14983   tree declaration;
14984
14985   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14986   handler = begin_handler ();
14987   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14988   declaration = cp_parser_exception_declaration (parser);
14989   finish_handler_parms (declaration, handler);
14990   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14991   cp_parser_compound_statement (parser, NULL, false);
14992   finish_handler (handler);
14993 }
14994
14995 /* Parse an exception-declaration.
14996
14997    exception-declaration:
14998      type-specifier-seq declarator
14999      type-specifier-seq abstract-declarator
15000      type-specifier-seq
15001      ...
15002
15003    Returns a VAR_DECL for the declaration, or NULL_TREE if the
15004    ellipsis variant is used.  */
15005
15006 static tree
15007 cp_parser_exception_declaration (cp_parser* parser)
15008 {
15009   cp_decl_specifier_seq type_specifiers;
15010   cp_declarator *declarator;
15011   const char *saved_message;
15012
15013   /* If it's an ellipsis, it's easy to handle.  */
15014   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15015     {
15016       /* Consume the `...' token.  */
15017       cp_lexer_consume_token (parser->lexer);
15018       return NULL_TREE;
15019     }
15020
15021   /* Types may not be defined in exception-declarations.  */
15022   saved_message = parser->type_definition_forbidden_message;
15023   parser->type_definition_forbidden_message
15024     = "types may not be defined in exception-declarations";
15025
15026   /* Parse the type-specifier-seq.  */
15027   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15028                                 &type_specifiers);
15029   /* If it's a `)', then there is no declarator.  */
15030   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15031     declarator = NULL;
15032   else
15033     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15034                                        /*ctor_dtor_or_conv_p=*/NULL,
15035                                        /*parenthesized_p=*/NULL,
15036                                        /*member_p=*/false);
15037
15038   /* Restore the saved message.  */
15039   parser->type_definition_forbidden_message = saved_message;
15040
15041   if (!type_specifiers.any_specifiers_p)
15042     return error_mark_node;
15043
15044   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15045 }
15046
15047 /* Parse a throw-expression.
15048
15049    throw-expression:
15050      throw assignment-expression [opt]
15051
15052    Returns a THROW_EXPR representing the throw-expression.  */
15053
15054 static tree
15055 cp_parser_throw_expression (cp_parser* parser)
15056 {
15057   tree expression;
15058   cp_token* token;
15059
15060   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15061   token = cp_lexer_peek_token (parser->lexer);
15062   /* Figure out whether or not there is an assignment-expression
15063      following the "throw" keyword.  */
15064   if (token->type == CPP_COMMA
15065       || token->type == CPP_SEMICOLON
15066       || token->type == CPP_CLOSE_PAREN
15067       || token->type == CPP_CLOSE_SQUARE
15068       || token->type == CPP_CLOSE_BRACE
15069       || token->type == CPP_COLON)
15070     expression = NULL_TREE;
15071   else
15072     expression = cp_parser_assignment_expression (parser,
15073                                                   /*cast_p=*/false);
15074
15075   return build_throw (expression);
15076 }
15077
15078 /* GNU Extensions */
15079
15080 /* Parse an (optional) asm-specification.
15081
15082    asm-specification:
15083      asm ( string-literal )
15084
15085    If the asm-specification is present, returns a STRING_CST
15086    corresponding to the string-literal.  Otherwise, returns
15087    NULL_TREE.  */
15088
15089 static tree
15090 cp_parser_asm_specification_opt (cp_parser* parser)
15091 {
15092   cp_token *token;
15093   tree asm_specification;
15094
15095   /* Peek at the next token.  */
15096   token = cp_lexer_peek_token (parser->lexer);
15097   /* If the next token isn't the `asm' keyword, then there's no
15098      asm-specification.  */
15099   if (!cp_parser_is_keyword (token, RID_ASM))
15100     return NULL_TREE;
15101
15102   /* Consume the `asm' token.  */
15103   cp_lexer_consume_token (parser->lexer);
15104   /* Look for the `('.  */
15105   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15106
15107   /* Look for the string-literal.  */
15108   asm_specification = cp_parser_string_literal (parser, false, false);
15109
15110   /* Look for the `)'.  */
15111   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15112
15113   return asm_specification;
15114 }
15115
15116 /* Parse an asm-operand-list.
15117
15118    asm-operand-list:
15119      asm-operand
15120      asm-operand-list , asm-operand
15121
15122    asm-operand:
15123      string-literal ( expression )
15124      [ string-literal ] string-literal ( expression )
15125
15126    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
15127    each node is the expression.  The TREE_PURPOSE is itself a
15128    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15129    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15130    is a STRING_CST for the string literal before the parenthesis.  */
15131
15132 static tree
15133 cp_parser_asm_operand_list (cp_parser* parser)
15134 {
15135   tree asm_operands = NULL_TREE;
15136
15137   while (true)
15138     {
15139       tree string_literal;
15140       tree expression;
15141       tree name;
15142
15143       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15144         {
15145           /* Consume the `[' token.  */
15146           cp_lexer_consume_token (parser->lexer);
15147           /* Read the operand name.  */
15148           name = cp_parser_identifier (parser);
15149           if (name != error_mark_node)
15150             name = build_string (IDENTIFIER_LENGTH (name),
15151                                  IDENTIFIER_POINTER (name));
15152           /* Look for the closing `]'.  */
15153           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15154         }
15155       else
15156         name = NULL_TREE;
15157       /* Look for the string-literal.  */
15158       string_literal = cp_parser_string_literal (parser, false, false);
15159
15160       /* Look for the `('.  */
15161       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15162       /* Parse the expression.  */
15163       expression = cp_parser_expression (parser, /*cast_p=*/false);
15164       /* Look for the `)'.  */
15165       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15166
15167       /* Add this operand to the list.  */
15168       asm_operands = tree_cons (build_tree_list (name, string_literal),
15169                                 expression,
15170                                 asm_operands);
15171       /* If the next token is not a `,', there are no more
15172          operands.  */
15173       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15174         break;
15175       /* Consume the `,'.  */
15176       cp_lexer_consume_token (parser->lexer);
15177     }
15178
15179   return nreverse (asm_operands);
15180 }
15181
15182 /* Parse an asm-clobber-list.
15183
15184    asm-clobber-list:
15185      string-literal
15186      asm-clobber-list , string-literal
15187
15188    Returns a TREE_LIST, indicating the clobbers in the order that they
15189    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
15190
15191 static tree
15192 cp_parser_asm_clobber_list (cp_parser* parser)
15193 {
15194   tree clobbers = NULL_TREE;
15195
15196   while (true)
15197     {
15198       tree string_literal;
15199
15200       /* Look for the string literal.  */
15201       string_literal = cp_parser_string_literal (parser, false, false);
15202       /* Add it to the list.  */
15203       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15204       /* If the next token is not a `,', then the list is
15205          complete.  */
15206       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15207         break;
15208       /* Consume the `,' token.  */
15209       cp_lexer_consume_token (parser->lexer);
15210     }
15211
15212   return clobbers;
15213 }
15214
15215 /* Parse an (optional) series of attributes.
15216
15217    attributes:
15218      attributes attribute
15219
15220    attribute:
15221      __attribute__ (( attribute-list [opt] ))
15222
15223    The return value is as for cp_parser_attribute_list.  */
15224
15225 static tree
15226 cp_parser_attributes_opt (cp_parser* parser)
15227 {
15228   tree attributes = NULL_TREE;
15229
15230   while (true)
15231     {
15232       cp_token *token;
15233       tree attribute_list;
15234
15235       /* Peek at the next token.  */
15236       token = cp_lexer_peek_token (parser->lexer);
15237       /* If it's not `__attribute__', then we're done.  */
15238       if (token->keyword != RID_ATTRIBUTE)
15239         break;
15240
15241       /* Consume the `__attribute__' keyword.  */
15242       cp_lexer_consume_token (parser->lexer);
15243       /* Look for the two `(' tokens.  */
15244       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15245       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15246
15247       /* Peek at the next token.  */
15248       token = cp_lexer_peek_token (parser->lexer);
15249       if (token->type != CPP_CLOSE_PAREN)
15250         /* Parse the attribute-list.  */
15251         attribute_list = cp_parser_attribute_list (parser);
15252       else
15253         /* If the next token is a `)', then there is no attribute
15254            list.  */
15255         attribute_list = NULL;
15256
15257       /* Look for the two `)' tokens.  */
15258       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15259       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15260
15261       /* Add these new attributes to the list.  */
15262       attributes = chainon (attributes, attribute_list);
15263     }
15264
15265   return attributes;
15266 }
15267
15268 /* Parse an attribute-list.
15269
15270    attribute-list:
15271      attribute
15272      attribute-list , attribute
15273
15274    attribute:
15275      identifier
15276      identifier ( identifier )
15277      identifier ( identifier , expression-list )
15278      identifier ( expression-list )
15279
15280    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
15281    to an attribute.  The TREE_PURPOSE of each node is the identifier
15282    indicating which attribute is in use.  The TREE_VALUE represents
15283    the arguments, if any.  */
15284
15285 static tree
15286 cp_parser_attribute_list (cp_parser* parser)
15287 {
15288   tree attribute_list = NULL_TREE;
15289   bool save_translate_strings_p = parser->translate_strings_p;
15290
15291   parser->translate_strings_p = false;
15292   while (true)
15293     {
15294       cp_token *token;
15295       tree identifier;
15296       tree attribute;
15297
15298       /* Look for the identifier.  We also allow keywords here; for
15299          example `__attribute__ ((const))' is legal.  */
15300       token = cp_lexer_peek_token (parser->lexer);
15301       if (token->type == CPP_NAME
15302           || token->type == CPP_KEYWORD)
15303         {
15304           tree arguments = NULL_TREE;
15305
15306           /* Consume the token.  */
15307           token = cp_lexer_consume_token (parser->lexer);
15308
15309           /* Save away the identifier that indicates which attribute
15310              this is.  */
15311           identifier = token->u.value;
15312           attribute = build_tree_list (identifier, NULL_TREE);
15313
15314           /* Peek at the next token.  */
15315           token = cp_lexer_peek_token (parser->lexer);
15316           /* If it's an `(', then parse the attribute arguments.  */
15317           if (token->type == CPP_OPEN_PAREN)
15318             {
15319               arguments = cp_parser_parenthesized_expression_list
15320                           (parser, true, /*cast_p=*/false,
15321                            /*allow_expansion_p=*/false,
15322                            /*non_constant_p=*/NULL);
15323               /* Save the arguments away.  */
15324               TREE_VALUE (attribute) = arguments;
15325             }
15326
15327           if (arguments != error_mark_node)
15328             {
15329               /* Add this attribute to the list.  */
15330               TREE_CHAIN (attribute) = attribute_list;
15331               attribute_list = attribute;
15332             }
15333
15334           token = cp_lexer_peek_token (parser->lexer);
15335         }
15336       /* Now, look for more attributes.  If the next token isn't a
15337          `,', we're done.  */
15338       if (token->type != CPP_COMMA)
15339         break;
15340
15341       /* Consume the comma and keep going.  */
15342       cp_lexer_consume_token (parser->lexer);
15343     }
15344   parser->translate_strings_p = save_translate_strings_p;
15345
15346   /* We built up the list in reverse order.  */
15347   return nreverse (attribute_list);
15348 }
15349
15350 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
15351    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
15352    current value of the PEDANTIC flag, regardless of whether or not
15353    the `__extension__' keyword is present.  The caller is responsible
15354    for restoring the value of the PEDANTIC flag.  */
15355
15356 static bool
15357 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15358 {
15359   /* Save the old value of the PEDANTIC flag.  */
15360   *saved_pedantic = pedantic;
15361
15362   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15363     {
15364       /* Consume the `__extension__' token.  */
15365       cp_lexer_consume_token (parser->lexer);
15366       /* We're not being pedantic while the `__extension__' keyword is
15367          in effect.  */
15368       pedantic = 0;
15369
15370       return true;
15371     }
15372
15373   return false;
15374 }
15375
15376 /* Parse a label declaration.
15377
15378    label-declaration:
15379      __label__ label-declarator-seq ;
15380
15381    label-declarator-seq:
15382      identifier , label-declarator-seq
15383      identifier  */
15384
15385 static void
15386 cp_parser_label_declaration (cp_parser* parser)
15387 {
15388   /* Look for the `__label__' keyword.  */
15389   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15390
15391   while (true)
15392     {
15393       tree identifier;
15394
15395       /* Look for an identifier.  */
15396       identifier = cp_parser_identifier (parser);
15397       /* If we failed, stop.  */
15398       if (identifier == error_mark_node)
15399         break;
15400       /* Declare it as a label.  */
15401       finish_label_decl (identifier);
15402       /* If the next token is a `;', stop.  */
15403       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15404         break;
15405       /* Look for the `,' separating the label declarations.  */
15406       cp_parser_require (parser, CPP_COMMA, "`,'");
15407     }
15408
15409   /* Look for the final `;'.  */
15410   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15411 }
15412
15413 /* Support Functions */
15414
15415 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15416    NAME should have one of the representations used for an
15417    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15418    is returned.  If PARSER->SCOPE is a dependent type, then a
15419    SCOPE_REF is returned.
15420
15421    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15422    returned; the name was already resolved when the TEMPLATE_ID_EXPR
15423    was formed.  Abstractly, such entities should not be passed to this
15424    function, because they do not need to be looked up, but it is
15425    simpler to check for this special case here, rather than at the
15426    call-sites.
15427
15428    In cases not explicitly covered above, this function returns a
15429    DECL, OVERLOAD, or baselink representing the result of the lookup.
15430    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15431    is returned.
15432
15433    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15434    (e.g., "struct") that was used.  In that case bindings that do not
15435    refer to types are ignored.
15436
15437    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15438    ignored.
15439
15440    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15441    are ignored.
15442
15443    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15444    types.
15445
15446    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15447    TREE_LIST of candidates if name-lookup results in an ambiguity, and
15448    NULL_TREE otherwise.  */
15449
15450 static tree
15451 cp_parser_lookup_name (cp_parser *parser, tree name,
15452                        enum tag_types tag_type,
15453                        bool is_template,
15454                        bool is_namespace,
15455                        bool check_dependency,
15456                        tree *ambiguous_decls)
15457 {
15458   int flags = 0;
15459   tree decl;
15460   tree object_type = parser->context->object_type;
15461
15462   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15463     flags |= LOOKUP_COMPLAIN;
15464
15465   /* Assume that the lookup will be unambiguous.  */
15466   if (ambiguous_decls)
15467     *ambiguous_decls = NULL_TREE;
15468
15469   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15470      no longer valid.  Note that if we are parsing tentatively, and
15471      the parse fails, OBJECT_TYPE will be automatically restored.  */
15472   parser->context->object_type = NULL_TREE;
15473
15474   if (name == error_mark_node)
15475     return error_mark_node;
15476
15477   /* A template-id has already been resolved; there is no lookup to
15478      do.  */
15479   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15480     return name;
15481   if (BASELINK_P (name))
15482     {
15483       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15484                   == TEMPLATE_ID_EXPR);
15485       return name;
15486     }
15487
15488   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
15489      it should already have been checked to make sure that the name
15490      used matches the type being destroyed.  */
15491   if (TREE_CODE (name) == BIT_NOT_EXPR)
15492     {
15493       tree type;
15494
15495       /* Figure out to which type this destructor applies.  */
15496       if (parser->scope)
15497         type = parser->scope;
15498       else if (object_type)
15499         type = object_type;
15500       else
15501         type = current_class_type;
15502       /* If that's not a class type, there is no destructor.  */
15503       if (!type || !CLASS_TYPE_P (type))
15504         return error_mark_node;
15505       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15506         lazily_declare_fn (sfk_destructor, type);
15507       if (!CLASSTYPE_DESTRUCTORS (type))
15508           return error_mark_node;
15509       /* If it was a class type, return the destructor.  */
15510       return CLASSTYPE_DESTRUCTORS (type);
15511     }
15512
15513   /* By this point, the NAME should be an ordinary identifier.  If
15514      the id-expression was a qualified name, the qualifying scope is
15515      stored in PARSER->SCOPE at this point.  */
15516   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15517
15518   /* Perform the lookup.  */
15519   if (parser->scope)
15520     {
15521       bool dependent_p;
15522
15523       if (parser->scope == error_mark_node)
15524         return error_mark_node;
15525
15526       /* If the SCOPE is dependent, the lookup must be deferred until
15527          the template is instantiated -- unless we are explicitly
15528          looking up names in uninstantiated templates.  Even then, we
15529          cannot look up the name if the scope is not a class type; it
15530          might, for example, be a template type parameter.  */
15531       dependent_p = (TYPE_P (parser->scope)
15532                      && !(parser->in_declarator_p
15533                           && currently_open_class (parser->scope))
15534                      && dependent_type_p (parser->scope));
15535       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15536            && dependent_p)
15537         {
15538           if (tag_type)
15539             {
15540               tree type;
15541
15542               /* The resolution to Core Issue 180 says that `struct
15543                  A::B' should be considered a type-name, even if `A'
15544                  is dependent.  */
15545               type = make_typename_type (parser->scope, name, tag_type,
15546                                          /*complain=*/tf_error);
15547               decl = TYPE_NAME (type);
15548             }
15549           else if (is_template
15550                    && (cp_parser_next_token_ends_template_argument_p (parser)
15551                        || cp_lexer_next_token_is (parser->lexer,
15552                                                   CPP_CLOSE_PAREN)))
15553             decl = make_unbound_class_template (parser->scope,
15554                                                 name, NULL_TREE,
15555                                                 /*complain=*/tf_error);
15556           else
15557             decl = build_qualified_name (/*type=*/NULL_TREE,
15558                                          parser->scope, name,
15559                                          is_template);
15560         }
15561       else
15562         {
15563           tree pushed_scope = NULL_TREE;
15564
15565           /* If PARSER->SCOPE is a dependent type, then it must be a
15566              class type, and we must not be checking dependencies;
15567              otherwise, we would have processed this lookup above.  So
15568              that PARSER->SCOPE is not considered a dependent base by
15569              lookup_member, we must enter the scope here.  */
15570           if (dependent_p)
15571             pushed_scope = push_scope (parser->scope);
15572           /* If the PARSER->SCOPE is a template specialization, it
15573              may be instantiated during name lookup.  In that case,
15574              errors may be issued.  Even if we rollback the current
15575              tentative parse, those errors are valid.  */
15576           decl = lookup_qualified_name (parser->scope, name,
15577                                         tag_type != none_type,
15578                                         /*complain=*/true);
15579           if (pushed_scope)
15580             pop_scope (pushed_scope);
15581         }
15582       parser->qualifying_scope = parser->scope;
15583       parser->object_scope = NULL_TREE;
15584     }
15585   else if (object_type)
15586     {
15587       tree object_decl = NULL_TREE;
15588       /* Look up the name in the scope of the OBJECT_TYPE, unless the
15589          OBJECT_TYPE is not a class.  */
15590       if (CLASS_TYPE_P (object_type))
15591         /* If the OBJECT_TYPE is a template specialization, it may
15592            be instantiated during name lookup.  In that case, errors
15593            may be issued.  Even if we rollback the current tentative
15594            parse, those errors are valid.  */
15595         object_decl = lookup_member (object_type,
15596                                      name,
15597                                      /*protect=*/0,
15598                                      tag_type != none_type);
15599       /* Look it up in the enclosing context, too.  */
15600       decl = lookup_name_real (name, tag_type != none_type,
15601                                /*nonclass=*/0,
15602                                /*block_p=*/true, is_namespace, flags);
15603       parser->object_scope = object_type;
15604       parser->qualifying_scope = NULL_TREE;
15605       if (object_decl)
15606         decl = object_decl;
15607     }
15608   else
15609     {
15610       decl = lookup_name_real (name, tag_type != none_type,
15611                                /*nonclass=*/0,
15612                                /*block_p=*/true, is_namespace, flags);
15613       parser->qualifying_scope = NULL_TREE;
15614       parser->object_scope = NULL_TREE;
15615     }
15616
15617   /* If the lookup failed, let our caller know.  */
15618   if (!decl || decl == error_mark_node)
15619     return error_mark_node;
15620
15621   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
15622   if (TREE_CODE (decl) == TREE_LIST)
15623     {
15624       if (ambiguous_decls)
15625         *ambiguous_decls = decl;
15626       /* The error message we have to print is too complicated for
15627          cp_parser_error, so we incorporate its actions directly.  */
15628       if (!cp_parser_simulate_error (parser))
15629         {
15630           error ("reference to %qD is ambiguous", name);
15631           print_candidates (decl);
15632         }
15633       return error_mark_node;
15634     }
15635
15636   gcc_assert (DECL_P (decl)
15637               || TREE_CODE (decl) == OVERLOAD
15638               || TREE_CODE (decl) == SCOPE_REF
15639               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15640               || BASELINK_P (decl));
15641
15642   /* If we have resolved the name of a member declaration, check to
15643      see if the declaration is accessible.  When the name resolves to
15644      set of overloaded functions, accessibility is checked when
15645      overload resolution is done.
15646
15647      During an explicit instantiation, access is not checked at all,
15648      as per [temp.explicit].  */
15649   if (DECL_P (decl))
15650     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15651
15652   return decl;
15653 }
15654
15655 /* Like cp_parser_lookup_name, but for use in the typical case where
15656    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15657    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
15658
15659 static tree
15660 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15661 {
15662   return cp_parser_lookup_name (parser, name,
15663                                 none_type,
15664                                 /*is_template=*/false,
15665                                 /*is_namespace=*/false,
15666                                 /*check_dependency=*/true,
15667                                 /*ambiguous_decls=*/NULL);
15668 }
15669
15670 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15671    the current context, return the TYPE_DECL.  If TAG_NAME_P is
15672    true, the DECL indicates the class being defined in a class-head,
15673    or declared in an elaborated-type-specifier.
15674
15675    Otherwise, return DECL.  */
15676
15677 static tree
15678 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15679 {
15680   /* If the TEMPLATE_DECL is being declared as part of a class-head,
15681      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15682
15683        struct A {
15684          template <typename T> struct B;
15685        };
15686
15687        template <typename T> struct A::B {};
15688
15689      Similarly, in an elaborated-type-specifier:
15690
15691        namespace N { struct X{}; }
15692
15693        struct A {
15694          template <typename T> friend struct N::X;
15695        };
15696
15697      However, if the DECL refers to a class type, and we are in
15698      the scope of the class, then the name lookup automatically
15699      finds the TYPE_DECL created by build_self_reference rather
15700      than a TEMPLATE_DECL.  For example, in:
15701
15702        template <class T> struct S {
15703          S s;
15704        };
15705
15706      there is no need to handle such case.  */
15707
15708   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15709     return DECL_TEMPLATE_RESULT (decl);
15710
15711   return decl;
15712 }
15713
15714 /* If too many, or too few, template-parameter lists apply to the
15715    declarator, issue an error message.  Returns TRUE if all went well,
15716    and FALSE otherwise.  */
15717
15718 static bool
15719 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15720                                                 cp_declarator *declarator)
15721 {
15722   unsigned num_templates;
15723
15724   /* We haven't seen any classes that involve template parameters yet.  */
15725   num_templates = 0;
15726
15727   switch (declarator->kind)
15728     {
15729     case cdk_id:
15730       if (declarator->u.id.qualifying_scope)
15731         {
15732           tree scope;
15733           tree member;
15734
15735           scope = declarator->u.id.qualifying_scope;
15736           member = declarator->u.id.unqualified_name;
15737
15738           while (scope && CLASS_TYPE_P (scope))
15739             {
15740               /* You're supposed to have one `template <...>'
15741                  for every template class, but you don't need one
15742                  for a full specialization.  For example:
15743
15744                  template <class T> struct S{};
15745                  template <> struct S<int> { void f(); };
15746                  void S<int>::f () {}
15747
15748                  is correct; there shouldn't be a `template <>' for
15749                  the definition of `S<int>::f'.  */
15750               if (!CLASSTYPE_TEMPLATE_INFO (scope))
15751                 /* If SCOPE does not have template information of any
15752                    kind, then it is not a template, nor is it nested
15753                    within a template.  */
15754                 break;
15755               if (explicit_class_specialization_p (scope))
15756                 break;
15757               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15758                 ++num_templates;
15759
15760               scope = TYPE_CONTEXT (scope);
15761             }
15762         }
15763       else if (TREE_CODE (declarator->u.id.unqualified_name)
15764                == TEMPLATE_ID_EXPR)
15765         /* If the DECLARATOR has the form `X<y>' then it uses one
15766            additional level of template parameters.  */
15767         ++num_templates;
15768
15769       return cp_parser_check_template_parameters (parser,
15770                                                   num_templates);
15771
15772     case cdk_function:
15773     case cdk_array:
15774     case cdk_pointer:
15775     case cdk_reference:
15776     case cdk_ptrmem:
15777       return (cp_parser_check_declarator_template_parameters
15778               (parser, declarator->declarator));
15779
15780     case cdk_error:
15781       return true;
15782
15783     default:
15784       gcc_unreachable ();
15785     }
15786   return false;
15787 }
15788
15789 /* NUM_TEMPLATES were used in the current declaration.  If that is
15790    invalid, return FALSE and issue an error messages.  Otherwise,
15791    return TRUE.  */
15792
15793 static bool
15794 cp_parser_check_template_parameters (cp_parser* parser,
15795                                      unsigned num_templates)
15796 {
15797   /* If there are more template classes than parameter lists, we have
15798      something like:
15799
15800        template <class T> void S<T>::R<T>::f ();  */
15801   if (parser->num_template_parameter_lists < num_templates)
15802     {
15803       error ("too few template-parameter-lists");
15804       return false;
15805     }
15806   /* If there are the same number of template classes and parameter
15807      lists, that's OK.  */
15808   if (parser->num_template_parameter_lists == num_templates)
15809     return true;
15810   /* If there are more, but only one more, then we are referring to a
15811      member template.  That's OK too.  */
15812   if (parser->num_template_parameter_lists == num_templates + 1)
15813       return true;
15814   /* Otherwise, there are too many template parameter lists.  We have
15815      something like:
15816
15817      template <class T> template <class U> void S::f();  */
15818   error ("too many template-parameter-lists");
15819   return false;
15820 }
15821
15822 /* Parse an optional `::' token indicating that the following name is
15823    from the global namespace.  If so, PARSER->SCOPE is set to the
15824    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15825    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15826    Returns the new value of PARSER->SCOPE, if the `::' token is
15827    present, and NULL_TREE otherwise.  */
15828
15829 static tree
15830 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15831 {
15832   cp_token *token;
15833
15834   /* Peek at the next token.  */
15835   token = cp_lexer_peek_token (parser->lexer);
15836   /* If we're looking at a `::' token then we're starting from the
15837      global namespace, not our current location.  */
15838   if (token->type == CPP_SCOPE)
15839     {
15840       /* Consume the `::' token.  */
15841       cp_lexer_consume_token (parser->lexer);
15842       /* Set the SCOPE so that we know where to start the lookup.  */
15843       parser->scope = global_namespace;
15844       parser->qualifying_scope = global_namespace;
15845       parser->object_scope = NULL_TREE;
15846
15847       return parser->scope;
15848     }
15849   else if (!current_scope_valid_p)
15850     {
15851       parser->scope = NULL_TREE;
15852       parser->qualifying_scope = NULL_TREE;
15853       parser->object_scope = NULL_TREE;
15854     }
15855
15856   return NULL_TREE;
15857 }
15858
15859 /* Returns TRUE if the upcoming token sequence is the start of a
15860    constructor declarator.  If FRIEND_P is true, the declarator is
15861    preceded by the `friend' specifier.  */
15862
15863 static bool
15864 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15865 {
15866   bool constructor_p;
15867   tree type_decl = NULL_TREE;
15868   bool nested_name_p;
15869   cp_token *next_token;
15870
15871   /* The common case is that this is not a constructor declarator, so
15872      try to avoid doing lots of work if at all possible.  It's not
15873      valid declare a constructor at function scope.  */
15874   if (parser->in_function_body)
15875     return false;
15876   /* And only certain tokens can begin a constructor declarator.  */
15877   next_token = cp_lexer_peek_token (parser->lexer);
15878   if (next_token->type != CPP_NAME
15879       && next_token->type != CPP_SCOPE
15880       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15881       && next_token->type != CPP_TEMPLATE_ID)
15882     return false;
15883
15884   /* Parse tentatively; we are going to roll back all of the tokens
15885      consumed here.  */
15886   cp_parser_parse_tentatively (parser);
15887   /* Assume that we are looking at a constructor declarator.  */
15888   constructor_p = true;
15889
15890   /* Look for the optional `::' operator.  */
15891   cp_parser_global_scope_opt (parser,
15892                               /*current_scope_valid_p=*/false);
15893   /* Look for the nested-name-specifier.  */
15894   nested_name_p
15895     = (cp_parser_nested_name_specifier_opt (parser,
15896                                             /*typename_keyword_p=*/false,
15897                                             /*check_dependency_p=*/false,
15898                                             /*type_p=*/false,
15899                                             /*is_declaration=*/false)
15900        != NULL_TREE);
15901   /* Outside of a class-specifier, there must be a
15902      nested-name-specifier.  */
15903   if (!nested_name_p &&
15904       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15905        || friend_p))
15906     constructor_p = false;
15907   /* If we still think that this might be a constructor-declarator,
15908      look for a class-name.  */
15909   if (constructor_p)
15910     {
15911       /* If we have:
15912
15913            template <typename T> struct S { S(); };
15914            template <typename T> S<T>::S ();
15915
15916          we must recognize that the nested `S' names a class.
15917          Similarly, for:
15918
15919            template <typename T> S<T>::S<T> ();
15920
15921          we must recognize that the nested `S' names a template.  */
15922       type_decl = cp_parser_class_name (parser,
15923                                         /*typename_keyword_p=*/false,
15924                                         /*template_keyword_p=*/false,
15925                                         none_type,
15926                                         /*check_dependency_p=*/false,
15927                                         /*class_head_p=*/false,
15928                                         /*is_declaration=*/false);
15929       /* If there was no class-name, then this is not a constructor.  */
15930       constructor_p = !cp_parser_error_occurred (parser);
15931     }
15932
15933   /* If we're still considering a constructor, we have to see a `(',
15934      to begin the parameter-declaration-clause, followed by either a
15935      `)', an `...', or a decl-specifier.  We need to check for a
15936      type-specifier to avoid being fooled into thinking that:
15937
15938        S::S (f) (int);
15939
15940      is a constructor.  (It is actually a function named `f' that
15941      takes one parameter (of type `int') and returns a value of type
15942      `S::S'.  */
15943   if (constructor_p
15944       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15945     {
15946       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15947           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15948           /* A parameter declaration begins with a decl-specifier,
15949              which is either the "attribute" keyword, a storage class
15950              specifier, or (usually) a type-specifier.  */
15951           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15952         {
15953           tree type;
15954           tree pushed_scope = NULL_TREE;
15955           unsigned saved_num_template_parameter_lists;
15956
15957           /* Names appearing in the type-specifier should be looked up
15958              in the scope of the class.  */
15959           if (current_class_type)
15960             type = NULL_TREE;
15961           else
15962             {
15963               type = TREE_TYPE (type_decl);
15964               if (TREE_CODE (type) == TYPENAME_TYPE)
15965                 {
15966                   type = resolve_typename_type (type,
15967                                                 /*only_current_p=*/false);
15968                   if (type == error_mark_node)
15969                     {
15970                       cp_parser_abort_tentative_parse (parser);
15971                       return false;
15972                     }
15973                 }
15974               pushed_scope = push_scope (type);
15975             }
15976
15977           /* Inside the constructor parameter list, surrounding
15978              template-parameter-lists do not apply.  */
15979           saved_num_template_parameter_lists
15980             = parser->num_template_parameter_lists;
15981           parser->num_template_parameter_lists = 0;
15982
15983           /* Look for the type-specifier.  */
15984           cp_parser_type_specifier (parser,
15985                                     CP_PARSER_FLAGS_NONE,
15986                                     /*decl_specs=*/NULL,
15987                                     /*is_declarator=*/true,
15988                                     /*declares_class_or_enum=*/NULL,
15989                                     /*is_cv_qualifier=*/NULL);
15990
15991           parser->num_template_parameter_lists
15992             = saved_num_template_parameter_lists;
15993
15994           /* Leave the scope of the class.  */
15995           if (pushed_scope)
15996             pop_scope (pushed_scope);
15997
15998           constructor_p = !cp_parser_error_occurred (parser);
15999         }
16000     }
16001   else
16002     constructor_p = false;
16003   /* We did not really want to consume any tokens.  */
16004   cp_parser_abort_tentative_parse (parser);
16005
16006   return constructor_p;
16007 }
16008
16009 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16010    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
16011    they must be performed once we are in the scope of the function.
16012
16013    Returns the function defined.  */
16014
16015 static tree
16016 cp_parser_function_definition_from_specifiers_and_declarator
16017   (cp_parser* parser,
16018    cp_decl_specifier_seq *decl_specifiers,
16019    tree attributes,
16020    const cp_declarator *declarator)
16021 {
16022   tree fn;
16023   bool success_p;
16024
16025   /* Begin the function-definition.  */
16026   success_p = start_function (decl_specifiers, declarator, attributes);
16027
16028   /* The things we're about to see are not directly qualified by any
16029      template headers we've seen thus far.  */
16030   reset_specialization ();
16031
16032   /* If there were names looked up in the decl-specifier-seq that we
16033      did not check, check them now.  We must wait until we are in the
16034      scope of the function to perform the checks, since the function
16035      might be a friend.  */
16036   perform_deferred_access_checks ();
16037
16038   if (!success_p)
16039     {
16040       /* Skip the entire function.  */
16041       cp_parser_skip_to_end_of_block_or_statement (parser);
16042       fn = error_mark_node;
16043     }
16044   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16045     {
16046       /* Seen already, skip it.  An error message has already been output.  */
16047       cp_parser_skip_to_end_of_block_or_statement (parser);
16048       fn = current_function_decl;
16049       current_function_decl = NULL_TREE;
16050       /* If this is a function from a class, pop the nested class.  */
16051       if (current_class_name)
16052         pop_nested_class ();
16053     }
16054   else
16055     fn = cp_parser_function_definition_after_declarator (parser,
16056                                                          /*inline_p=*/false);
16057
16058   return fn;
16059 }
16060
16061 /* Parse the part of a function-definition that follows the
16062    declarator.  INLINE_P is TRUE iff this function is an inline
16063    function defined with a class-specifier.
16064
16065    Returns the function defined.  */
16066
16067 static tree
16068 cp_parser_function_definition_after_declarator (cp_parser* parser,
16069                                                 bool inline_p)
16070 {
16071   tree fn;
16072   bool ctor_initializer_p = false;
16073   bool saved_in_unbraced_linkage_specification_p;
16074   bool saved_in_function_body;
16075   unsigned saved_num_template_parameter_lists;
16076
16077   saved_in_function_body = parser->in_function_body;
16078   parser->in_function_body = true;
16079   /* If the next token is `return', then the code may be trying to
16080      make use of the "named return value" extension that G++ used to
16081      support.  */
16082   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16083     {
16084       /* Consume the `return' keyword.  */
16085       cp_lexer_consume_token (parser->lexer);
16086       /* Look for the identifier that indicates what value is to be
16087          returned.  */
16088       cp_parser_identifier (parser);
16089       /* Issue an error message.  */
16090       error ("named return values are no longer supported");
16091       /* Skip tokens until we reach the start of the function body.  */
16092       while (true)
16093         {
16094           cp_token *token = cp_lexer_peek_token (parser->lexer);
16095           if (token->type == CPP_OPEN_BRACE
16096               || token->type == CPP_EOF
16097               || token->type == CPP_PRAGMA_EOL)
16098             break;
16099           cp_lexer_consume_token (parser->lexer);
16100         }
16101     }
16102   /* The `extern' in `extern "C" void f () { ... }' does not apply to
16103      anything declared inside `f'.  */
16104   saved_in_unbraced_linkage_specification_p
16105     = parser->in_unbraced_linkage_specification_p;
16106   parser->in_unbraced_linkage_specification_p = false;
16107   /* Inside the function, surrounding template-parameter-lists do not
16108      apply.  */
16109   saved_num_template_parameter_lists
16110     = parser->num_template_parameter_lists;
16111   parser->num_template_parameter_lists = 0;
16112   /* If the next token is `try', then we are looking at a
16113      function-try-block.  */
16114   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16115     ctor_initializer_p = cp_parser_function_try_block (parser);
16116   /* A function-try-block includes the function-body, so we only do
16117      this next part if we're not processing a function-try-block.  */
16118   else
16119     ctor_initializer_p
16120       = cp_parser_ctor_initializer_opt_and_function_body (parser);
16121
16122   /* Finish the function.  */
16123   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16124                         (inline_p ? 2 : 0));
16125   /* Generate code for it, if necessary.  */
16126   expand_or_defer_fn (fn);
16127   /* Restore the saved values.  */
16128   parser->in_unbraced_linkage_specification_p
16129     = saved_in_unbraced_linkage_specification_p;
16130   parser->num_template_parameter_lists
16131     = saved_num_template_parameter_lists;
16132   parser->in_function_body = saved_in_function_body;
16133
16134   return fn;
16135 }
16136
16137 /* Parse a template-declaration, assuming that the `export' (and
16138    `extern') keywords, if present, has already been scanned.  MEMBER_P
16139    is as for cp_parser_template_declaration.  */
16140
16141 static void
16142 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16143 {
16144   tree decl = NULL_TREE;
16145   VEC (deferred_access_check,gc) *checks;
16146   tree parameter_list;
16147   bool friend_p = false;
16148   bool need_lang_pop;
16149
16150   /* Look for the `template' keyword.  */
16151   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16152     return;
16153
16154   /* And the `<'.  */
16155   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16156     return;
16157   if (at_class_scope_p () && current_function_decl)
16158     {
16159       /* 14.5.2.2 [temp.mem]
16160
16161          A local class shall not have member templates.  */
16162       error ("invalid declaration of member template in local class");
16163       cp_parser_skip_to_end_of_block_or_statement (parser);
16164       return;
16165     }
16166   /* [temp]
16167
16168      A template ... shall not have C linkage.  */
16169   if (current_lang_name == lang_name_c)
16170     {
16171       error ("template with C linkage");
16172       /* Give it C++ linkage to avoid confusing other parts of the
16173          front end.  */
16174       push_lang_context (lang_name_cplusplus);
16175       need_lang_pop = true;
16176     }
16177   else
16178     need_lang_pop = false;
16179
16180   /* We cannot perform access checks on the template parameter
16181      declarations until we know what is being declared, just as we
16182      cannot check the decl-specifier list.  */
16183   push_deferring_access_checks (dk_deferred);
16184
16185   /* If the next token is `>', then we have an invalid
16186      specialization.  Rather than complain about an invalid template
16187      parameter, issue an error message here.  */
16188   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16189     {
16190       cp_parser_error (parser, "invalid explicit specialization");
16191       begin_specialization ();
16192       parameter_list = NULL_TREE;
16193     }
16194   else
16195     /* Parse the template parameters.  */
16196     parameter_list = cp_parser_template_parameter_list (parser);
16197
16198   /* Get the deferred access checks from the parameter list.  These
16199      will be checked once we know what is being declared, as for a
16200      member template the checks must be performed in the scope of the
16201      class containing the member.  */
16202   checks = get_deferred_access_checks ();
16203
16204   /* Look for the `>'.  */
16205   cp_parser_skip_to_end_of_template_parameter_list (parser);
16206   /* We just processed one more parameter list.  */
16207   ++parser->num_template_parameter_lists;
16208   /* If the next token is `template', there are more template
16209      parameters.  */
16210   if (cp_lexer_next_token_is_keyword (parser->lexer,
16211                                       RID_TEMPLATE))
16212     cp_parser_template_declaration_after_export (parser, member_p);
16213   else
16214     {
16215       /* There are no access checks when parsing a template, as we do not
16216          know if a specialization will be a friend.  */
16217       push_deferring_access_checks (dk_no_check);
16218       decl = cp_parser_single_declaration (parser,
16219                                            checks,
16220                                            member_p,
16221                                            &friend_p);
16222       pop_deferring_access_checks ();
16223
16224       /* If this is a member template declaration, let the front
16225          end know.  */
16226       if (member_p && !friend_p && decl)
16227         {
16228           if (TREE_CODE (decl) == TYPE_DECL)
16229             cp_parser_check_access_in_redeclaration (decl);
16230
16231           decl = finish_member_template_decl (decl);
16232         }
16233       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16234         make_friend_class (current_class_type, TREE_TYPE (decl),
16235                            /*complain=*/true);
16236     }
16237   /* We are done with the current parameter list.  */
16238   --parser->num_template_parameter_lists;
16239
16240   pop_deferring_access_checks ();
16241
16242   /* Finish up.  */
16243   finish_template_decl (parameter_list);
16244
16245   /* Register member declarations.  */
16246   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16247     finish_member_declaration (decl);
16248   /* For the erroneous case of a template with C linkage, we pushed an
16249      implicit C++ linkage scope; exit that scope now.  */
16250   if (need_lang_pop)
16251     pop_lang_context ();
16252   /* If DECL is a function template, we must return to parse it later.
16253      (Even though there is no definition, there might be default
16254      arguments that need handling.)  */
16255   if (member_p && decl
16256       && (TREE_CODE (decl) == FUNCTION_DECL
16257           || DECL_FUNCTION_TEMPLATE_P (decl)))
16258     TREE_VALUE (parser->unparsed_functions_queues)
16259       = tree_cons (NULL_TREE, decl,
16260                    TREE_VALUE (parser->unparsed_functions_queues));
16261 }
16262
16263 /* Perform the deferred access checks from a template-parameter-list.
16264    CHECKS is a TREE_LIST of access checks, as returned by
16265    get_deferred_access_checks.  */
16266
16267 static void
16268 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16269 {
16270   ++processing_template_parmlist;
16271   perform_access_checks (checks);
16272   --processing_template_parmlist;
16273 }
16274
16275 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16276    `function-definition' sequence.  MEMBER_P is true, this declaration
16277    appears in a class scope.
16278
16279    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
16280    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
16281
16282 static tree
16283 cp_parser_single_declaration (cp_parser* parser,
16284                               VEC (deferred_access_check,gc)* checks,
16285                               bool member_p,
16286                               bool* friend_p)
16287 {
16288   int declares_class_or_enum;
16289   tree decl = NULL_TREE;
16290   cp_decl_specifier_seq decl_specifiers;
16291   bool function_definition_p = false;
16292
16293   /* This function is only used when processing a template
16294      declaration.  */
16295   gcc_assert (innermost_scope_kind () == sk_template_parms
16296               || innermost_scope_kind () == sk_template_spec);
16297
16298   /* Defer access checks until we know what is being declared.  */
16299   push_deferring_access_checks (dk_deferred);
16300
16301   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16302      alternative.  */
16303   cp_parser_decl_specifier_seq (parser,
16304                                 CP_PARSER_FLAGS_OPTIONAL,
16305                                 &decl_specifiers,
16306                                 &declares_class_or_enum);
16307   if (friend_p)
16308     *friend_p = cp_parser_friend_p (&decl_specifiers);
16309
16310   /* There are no template typedefs.  */
16311   if (decl_specifiers.specs[(int) ds_typedef])
16312     {
16313       error ("template declaration of %qs", "typedef");
16314       decl = error_mark_node;
16315     }
16316
16317   /* Gather up the access checks that occurred the
16318      decl-specifier-seq.  */
16319   stop_deferring_access_checks ();
16320
16321   /* Check for the declaration of a template class.  */
16322   if (declares_class_or_enum)
16323     {
16324       if (cp_parser_declares_only_class_p (parser))
16325         {
16326           decl = shadow_tag (&decl_specifiers);
16327
16328           /* In this case:
16329
16330                struct C {
16331                  friend template <typename T> struct A<T>::B;
16332                };
16333
16334              A<T>::B will be represented by a TYPENAME_TYPE, and
16335              therefore not recognized by shadow_tag.  */
16336           if (friend_p && *friend_p
16337               && !decl
16338               && decl_specifiers.type
16339               && TYPE_P (decl_specifiers.type))
16340             decl = decl_specifiers.type;
16341
16342           if (decl && decl != error_mark_node)
16343             decl = TYPE_NAME (decl);
16344           else
16345             decl = error_mark_node;
16346
16347           /* Perform access checks for template parameters.  */
16348           cp_parser_perform_template_parameter_access_checks (checks);
16349         }
16350     }
16351   /* If it's not a template class, try for a template function.  If
16352      the next token is a `;', then this declaration does not declare
16353      anything.  But, if there were errors in the decl-specifiers, then
16354      the error might well have come from an attempted class-specifier.
16355      In that case, there's no need to warn about a missing declarator.  */
16356   if (!decl
16357       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16358           || decl_specifiers.type != error_mark_node))
16359     decl = cp_parser_init_declarator (parser,
16360                                       &decl_specifiers,
16361                                       checks,
16362                                       /*function_definition_allowed_p=*/true,
16363                                       member_p,
16364                                       declares_class_or_enum,
16365                                       &function_definition_p);
16366
16367   pop_deferring_access_checks ();
16368
16369   /* Clear any current qualification; whatever comes next is the start
16370      of something new.  */
16371   parser->scope = NULL_TREE;
16372   parser->qualifying_scope = NULL_TREE;
16373   parser->object_scope = NULL_TREE;
16374   /* Look for a trailing `;' after the declaration.  */
16375   if (!function_definition_p
16376       && (decl == error_mark_node
16377           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16378     cp_parser_skip_to_end_of_block_or_statement (parser);
16379
16380   return decl;
16381 }
16382
16383 /* Parse a cast-expression that is not the operand of a unary "&".  */
16384
16385 static tree
16386 cp_parser_simple_cast_expression (cp_parser *parser)
16387 {
16388   return cp_parser_cast_expression (parser, /*address_p=*/false,
16389                                     /*cast_p=*/false);
16390 }
16391
16392 /* Parse a functional cast to TYPE.  Returns an expression
16393    representing the cast.  */
16394
16395 static tree
16396 cp_parser_functional_cast (cp_parser* parser, tree type)
16397 {
16398   tree expression_list;
16399   tree cast;
16400
16401   expression_list
16402     = cp_parser_parenthesized_expression_list (parser, false,
16403                                                /*cast_p=*/true,
16404                                                /*allow_expansion_p=*/true,
16405                                                /*non_constant_p=*/NULL);
16406
16407   cast = build_functional_cast (type, expression_list);
16408   /* [expr.const]/1: In an integral constant expression "only type
16409      conversions to integral or enumeration type can be used".  */
16410   if (TREE_CODE (type) == TYPE_DECL)
16411     type = TREE_TYPE (type);
16412   if (cast != error_mark_node
16413       && !cast_valid_in_integral_constant_expression_p (type)
16414       && (cp_parser_non_integral_constant_expression
16415           (parser, "a call to a constructor")))
16416     return error_mark_node;
16417   return cast;
16418 }
16419
16420 /* Save the tokens that make up the body of a member function defined
16421    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
16422    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
16423    specifiers applied to the declaration.  Returns the FUNCTION_DECL
16424    for the member function.  */
16425
16426 static tree
16427 cp_parser_save_member_function_body (cp_parser* parser,
16428                                      cp_decl_specifier_seq *decl_specifiers,
16429                                      cp_declarator *declarator,
16430                                      tree attributes)
16431 {
16432   cp_token *first;
16433   cp_token *last;
16434   tree fn;
16435
16436   /* Create the function-declaration.  */
16437   fn = start_method (decl_specifiers, declarator, attributes);
16438   /* If something went badly wrong, bail out now.  */
16439   if (fn == error_mark_node)
16440     {
16441       /* If there's a function-body, skip it.  */
16442       if (cp_parser_token_starts_function_definition_p
16443           (cp_lexer_peek_token (parser->lexer)))
16444         cp_parser_skip_to_end_of_block_or_statement (parser);
16445       return error_mark_node;
16446     }
16447
16448   /* Remember it, if there default args to post process.  */
16449   cp_parser_save_default_args (parser, fn);
16450
16451   /* Save away the tokens that make up the body of the
16452      function.  */
16453   first = parser->lexer->next_token;
16454   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16455   /* Handle function try blocks.  */
16456   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16457     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16458   last = parser->lexer->next_token;
16459
16460   /* Save away the inline definition; we will process it when the
16461      class is complete.  */
16462   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16463   DECL_PENDING_INLINE_P (fn) = 1;
16464
16465   /* We need to know that this was defined in the class, so that
16466      friend templates are handled correctly.  */
16467   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16468
16469   /* We're done with the inline definition.  */
16470   finish_method (fn);
16471
16472   /* Add FN to the queue of functions to be parsed later.  */
16473   TREE_VALUE (parser->unparsed_functions_queues)
16474     = tree_cons (NULL_TREE, fn,
16475                  TREE_VALUE (parser->unparsed_functions_queues));
16476
16477   return fn;
16478 }
16479
16480 /* Parse a template-argument-list, as well as the trailing ">" (but
16481    not the opening ">").  See cp_parser_template_argument_list for the
16482    return value.  */
16483
16484 static tree
16485 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16486 {
16487   tree arguments;
16488   tree saved_scope;
16489   tree saved_qualifying_scope;
16490   tree saved_object_scope;
16491   bool saved_greater_than_is_operator_p;
16492   bool saved_skip_evaluation;
16493
16494   /* [temp.names]
16495
16496      When parsing a template-id, the first non-nested `>' is taken as
16497      the end of the template-argument-list rather than a greater-than
16498      operator.  */
16499   saved_greater_than_is_operator_p
16500     = parser->greater_than_is_operator_p;
16501   parser->greater_than_is_operator_p = false;
16502   /* Parsing the argument list may modify SCOPE, so we save it
16503      here.  */
16504   saved_scope = parser->scope;
16505   saved_qualifying_scope = parser->qualifying_scope;
16506   saved_object_scope = parser->object_scope;
16507   /* We need to evaluate the template arguments, even though this
16508      template-id may be nested within a "sizeof".  */
16509   saved_skip_evaluation = skip_evaluation;
16510   skip_evaluation = false;
16511   /* Parse the template-argument-list itself.  */
16512   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16513     arguments = NULL_TREE;
16514   else
16515     arguments = cp_parser_template_argument_list (parser);
16516   /* Look for the `>' that ends the template-argument-list. If we find
16517      a '>>' instead, it's probably just a typo.  */
16518   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16519     {
16520       if (!saved_greater_than_is_operator_p)
16521         {
16522           /* If we're in a nested template argument list, the '>>' has
16523             to be a typo for '> >'. We emit the error message, but we
16524             continue parsing and we push a '>' as next token, so that
16525             the argument list will be parsed correctly.  Note that the
16526             global source location is still on the token before the
16527             '>>', so we need to say explicitly where we want it.  */
16528           cp_token *token = cp_lexer_peek_token (parser->lexer);
16529           error ("%H%<>>%> should be %<> >%> "
16530                  "within a nested template argument list",
16531                  &token->location);
16532
16533           /* ??? Proper recovery should terminate two levels of
16534              template argument list here.  */
16535           token->type = CPP_GREATER;
16536         }
16537       else
16538         {
16539           /* If this is not a nested template argument list, the '>>'
16540             is a typo for '>'. Emit an error message and continue.
16541             Same deal about the token location, but here we can get it
16542             right by consuming the '>>' before issuing the diagnostic.  */
16543           cp_lexer_consume_token (parser->lexer);
16544           error ("spurious %<>>%>, use %<>%> to terminate "
16545                  "a template argument list");
16546         }
16547     }
16548   else
16549     cp_parser_skip_to_end_of_template_parameter_list (parser);
16550   /* The `>' token might be a greater-than operator again now.  */
16551   parser->greater_than_is_operator_p
16552     = saved_greater_than_is_operator_p;
16553   /* Restore the SAVED_SCOPE.  */
16554   parser->scope = saved_scope;
16555   parser->qualifying_scope = saved_qualifying_scope;
16556   parser->object_scope = saved_object_scope;
16557   skip_evaluation = saved_skip_evaluation;
16558
16559   return arguments;
16560 }
16561
16562 /* MEMBER_FUNCTION is a member function, or a friend.  If default
16563    arguments, or the body of the function have not yet been parsed,
16564    parse them now.  */
16565
16566 static void
16567 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16568 {
16569   /* If this member is a template, get the underlying
16570      FUNCTION_DECL.  */
16571   if (DECL_FUNCTION_TEMPLATE_P (member_function))
16572     member_function = DECL_TEMPLATE_RESULT (member_function);
16573
16574   /* There should not be any class definitions in progress at this
16575      point; the bodies of members are only parsed outside of all class
16576      definitions.  */
16577   gcc_assert (parser->num_classes_being_defined == 0);
16578   /* While we're parsing the member functions we might encounter more
16579      classes.  We want to handle them right away, but we don't want
16580      them getting mixed up with functions that are currently in the
16581      queue.  */
16582   parser->unparsed_functions_queues
16583     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16584
16585   /* Make sure that any template parameters are in scope.  */
16586   maybe_begin_member_template_processing (member_function);
16587
16588   /* If the body of the function has not yet been parsed, parse it
16589      now.  */
16590   if (DECL_PENDING_INLINE_P (member_function))
16591     {
16592       tree function_scope;
16593       cp_token_cache *tokens;
16594
16595       /* The function is no longer pending; we are processing it.  */
16596       tokens = DECL_PENDING_INLINE_INFO (member_function);
16597       DECL_PENDING_INLINE_INFO (member_function) = NULL;
16598       DECL_PENDING_INLINE_P (member_function) = 0;
16599
16600       /* If this is a local class, enter the scope of the containing
16601          function.  */
16602       function_scope = current_function_decl;
16603       if (function_scope)
16604         push_function_context_to (function_scope);
16605
16606
16607       /* Push the body of the function onto the lexer stack.  */
16608       cp_parser_push_lexer_for_tokens (parser, tokens);
16609
16610       /* Let the front end know that we going to be defining this
16611          function.  */
16612       start_preparsed_function (member_function, NULL_TREE,
16613                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
16614
16615       /* Don't do access checking if it is a templated function.  */
16616       if (processing_template_decl)
16617         push_deferring_access_checks (dk_no_check);
16618
16619       /* Now, parse the body of the function.  */
16620       cp_parser_function_definition_after_declarator (parser,
16621                                                       /*inline_p=*/true);
16622
16623       if (processing_template_decl)
16624         pop_deferring_access_checks ();
16625
16626       /* Leave the scope of the containing function.  */
16627       if (function_scope)
16628         pop_function_context_from (function_scope);
16629       cp_parser_pop_lexer (parser);
16630     }
16631
16632   /* Remove any template parameters from the symbol table.  */
16633   maybe_end_member_template_processing ();
16634
16635   /* Restore the queue.  */
16636   parser->unparsed_functions_queues
16637     = TREE_CHAIN (parser->unparsed_functions_queues);
16638 }
16639
16640 /* If DECL contains any default args, remember it on the unparsed
16641    functions queue.  */
16642
16643 static void
16644 cp_parser_save_default_args (cp_parser* parser, tree decl)
16645 {
16646   tree probe;
16647
16648   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16649        probe;
16650        probe = TREE_CHAIN (probe))
16651     if (TREE_PURPOSE (probe))
16652       {
16653         TREE_PURPOSE (parser->unparsed_functions_queues)
16654           = tree_cons (current_class_type, decl,
16655                        TREE_PURPOSE (parser->unparsed_functions_queues));
16656         break;
16657       }
16658 }
16659
16660 /* FN is a FUNCTION_DECL which may contains a parameter with an
16661    unparsed DEFAULT_ARG.  Parse the default args now.  This function
16662    assumes that the current scope is the scope in which the default
16663    argument should be processed.  */
16664
16665 static void
16666 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16667 {
16668   bool saved_local_variables_forbidden_p;
16669   tree parm;
16670
16671   /* While we're parsing the default args, we might (due to the
16672      statement expression extension) encounter more classes.  We want
16673      to handle them right away, but we don't want them getting mixed
16674      up with default args that are currently in the queue.  */
16675   parser->unparsed_functions_queues
16676     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16677
16678   /* Local variable names (and the `this' keyword) may not appear
16679      in a default argument.  */
16680   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16681   parser->local_variables_forbidden_p = true;
16682
16683   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16684        parm;
16685        parm = TREE_CHAIN (parm))
16686     {
16687       cp_token_cache *tokens;
16688       tree default_arg = TREE_PURPOSE (parm);
16689       tree parsed_arg;
16690       VEC(tree,gc) *insts;
16691       tree copy;
16692       unsigned ix;
16693
16694       if (!default_arg)
16695         continue;
16696
16697       if (TREE_CODE (default_arg) != DEFAULT_ARG)
16698         /* This can happen for a friend declaration for a function
16699            already declared with default arguments.  */
16700         continue;
16701
16702        /* Push the saved tokens for the default argument onto the parser's
16703           lexer stack.  */
16704       tokens = DEFARG_TOKENS (default_arg);
16705       cp_parser_push_lexer_for_tokens (parser, tokens);
16706
16707       /* Parse the assignment-expression.  */
16708       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16709
16710       if (!processing_template_decl)
16711         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16712
16713       TREE_PURPOSE (parm) = parsed_arg;
16714
16715       /* Update any instantiations we've already created.  */
16716       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16717            VEC_iterate (tree, insts, ix, copy); ix++)
16718         TREE_PURPOSE (copy) = parsed_arg;
16719
16720       /* If the token stream has not been completely used up, then
16721          there was extra junk after the end of the default
16722          argument.  */
16723       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16724         cp_parser_error (parser, "expected %<,%>");
16725
16726       /* Revert to the main lexer.  */
16727       cp_parser_pop_lexer (parser);
16728     }
16729
16730   /* Make sure no default arg is missing.  */
16731   check_default_args (fn);
16732
16733   /* Restore the state of local_variables_forbidden_p.  */
16734   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16735
16736   /* Restore the queue.  */
16737   parser->unparsed_functions_queues
16738     = TREE_CHAIN (parser->unparsed_functions_queues);
16739 }
16740
16741 /* Parse the operand of `sizeof' (or a similar operator).  Returns
16742    either a TYPE or an expression, depending on the form of the
16743    input.  The KEYWORD indicates which kind of expression we have
16744    encountered.  */
16745
16746 static tree
16747 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16748 {
16749   static const char *format;
16750   tree expr = NULL_TREE;
16751   const char *saved_message;
16752   bool saved_integral_constant_expression_p;
16753   bool saved_non_integral_constant_expression_p;
16754   bool pack_expansion_p = false;
16755
16756   /* Initialize FORMAT the first time we get here.  */
16757   if (!format)
16758     format = "types may not be defined in '%s' expressions";
16759
16760   /* Types cannot be defined in a `sizeof' expression.  Save away the
16761      old message.  */
16762   saved_message = parser->type_definition_forbidden_message;
16763   /* And create the new one.  */
16764   parser->type_definition_forbidden_message
16765     = XNEWVEC (const char, strlen (format)
16766                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16767                + 1 /* `\0' */);
16768   sprintf ((char *) parser->type_definition_forbidden_message,
16769            format, IDENTIFIER_POINTER (ridpointers[keyword]));
16770
16771   /* The restrictions on constant-expressions do not apply inside
16772      sizeof expressions.  */
16773   saved_integral_constant_expression_p
16774     = parser->integral_constant_expression_p;
16775   saved_non_integral_constant_expression_p
16776     = parser->non_integral_constant_expression_p;
16777   parser->integral_constant_expression_p = false;
16778
16779   /* If it's a `...', then we are computing the length of a parameter
16780      pack.  */
16781   if (keyword == RID_SIZEOF
16782       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16783     {
16784       /* Consume the `...'.  */
16785       cp_lexer_consume_token (parser->lexer);
16786       maybe_warn_variadic_templates ();
16787
16788       /* Note that this is an expansion.  */
16789       pack_expansion_p = true;
16790     }
16791
16792   /* Do not actually evaluate the expression.  */
16793   ++skip_evaluation;
16794   /* If it's a `(', then we might be looking at the type-id
16795      construction.  */
16796   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16797     {
16798       tree type;
16799       bool saved_in_type_id_in_expr_p;
16800
16801       /* We can't be sure yet whether we're looking at a type-id or an
16802          expression.  */
16803       cp_parser_parse_tentatively (parser);
16804       /* Consume the `('.  */
16805       cp_lexer_consume_token (parser->lexer);
16806       /* Parse the type-id.  */
16807       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16808       parser->in_type_id_in_expr_p = true;
16809       type = cp_parser_type_id (parser);
16810       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16811       /* Now, look for the trailing `)'.  */
16812       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16813       /* If all went well, then we're done.  */
16814       if (cp_parser_parse_definitely (parser))
16815         {
16816           cp_decl_specifier_seq decl_specs;
16817
16818           /* Build a trivial decl-specifier-seq.  */
16819           clear_decl_specs (&decl_specs);
16820           decl_specs.type = type;
16821
16822           /* Call grokdeclarator to figure out what type this is.  */
16823           expr = grokdeclarator (NULL,
16824                                  &decl_specs,
16825                                  TYPENAME,
16826                                  /*initialized=*/0,
16827                                  /*attrlist=*/NULL);
16828         }
16829     }
16830
16831   /* If the type-id production did not work out, then we must be
16832      looking at the unary-expression production.  */
16833   if (!expr)
16834     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16835                                        /*cast_p=*/false);
16836
16837   if (pack_expansion_p)
16838     /* Build a pack expansion. */
16839     expr = make_pack_expansion (expr);
16840
16841   /* Go back to evaluating expressions.  */
16842   --skip_evaluation;
16843
16844   /* Free the message we created.  */
16845   free ((char *) parser->type_definition_forbidden_message);
16846   /* And restore the old one.  */
16847   parser->type_definition_forbidden_message = saved_message;
16848   parser->integral_constant_expression_p
16849     = saved_integral_constant_expression_p;
16850   parser->non_integral_constant_expression_p
16851     = saved_non_integral_constant_expression_p;
16852
16853   return expr;
16854 }
16855
16856 /* If the current declaration has no declarator, return true.  */
16857
16858 static bool
16859 cp_parser_declares_only_class_p (cp_parser *parser)
16860 {
16861   /* If the next token is a `;' or a `,' then there is no
16862      declarator.  */
16863   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16864           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16865 }
16866
16867 /* Update the DECL_SPECS to reflect the storage class indicated by
16868    KEYWORD.  */
16869
16870 static void
16871 cp_parser_set_storage_class (cp_parser *parser,
16872                              cp_decl_specifier_seq *decl_specs,
16873                              enum rid keyword)
16874 {
16875   cp_storage_class storage_class;
16876
16877   if (parser->in_unbraced_linkage_specification_p)
16878     {
16879       error ("invalid use of %qD in linkage specification",
16880              ridpointers[keyword]);
16881       return;
16882     }
16883   else if (decl_specs->storage_class != sc_none)
16884     {
16885       decl_specs->conflicting_specifiers_p = true;
16886       return;
16887     }
16888
16889   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16890       && decl_specs->specs[(int) ds_thread])
16891     {
16892       error ("%<__thread%> before %qD", ridpointers[keyword]);
16893       decl_specs->specs[(int) ds_thread] = 0;
16894     }
16895
16896   switch (keyword)
16897     {
16898     case RID_AUTO:
16899       storage_class = sc_auto;
16900       break;
16901     case RID_REGISTER:
16902       storage_class = sc_register;
16903       break;
16904     case RID_STATIC:
16905       storage_class = sc_static;
16906       break;
16907     case RID_EXTERN:
16908       storage_class = sc_extern;
16909       break;
16910     case RID_MUTABLE:
16911       storage_class = sc_mutable;
16912       break;
16913     default:
16914       gcc_unreachable ();
16915     }
16916   decl_specs->storage_class = storage_class;
16917
16918   /* A storage class specifier cannot be applied alongside a typedef 
16919      specifier. If there is a typedef specifier present then set 
16920      conflicting_specifiers_p which will trigger an error later
16921      on in grokdeclarator. */
16922   if (decl_specs->specs[(int)ds_typedef])
16923     decl_specs->conflicting_specifiers_p = true;
16924 }
16925
16926 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16927    is true, the type is a user-defined type; otherwise it is a
16928    built-in type specified by a keyword.  */
16929
16930 static void
16931 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16932                               tree type_spec,
16933                               bool user_defined_p)
16934 {
16935   decl_specs->any_specifiers_p = true;
16936
16937   /* If the user tries to redeclare bool or wchar_t (with, for
16938      example, in "typedef int wchar_t;") we remember that this is what
16939      happened.  In system headers, we ignore these declarations so
16940      that G++ can work with system headers that are not C++-safe.  */
16941   if (decl_specs->specs[(int) ds_typedef]
16942       && !user_defined_p
16943       && (type_spec == boolean_type_node
16944           || type_spec == wchar_type_node)
16945       && (decl_specs->type
16946           || decl_specs->specs[(int) ds_long]
16947           || decl_specs->specs[(int) ds_short]
16948           || decl_specs->specs[(int) ds_unsigned]
16949           || decl_specs->specs[(int) ds_signed]))
16950     {
16951       decl_specs->redefined_builtin_type = type_spec;
16952       if (!decl_specs->type)
16953         {
16954           decl_specs->type = type_spec;
16955           decl_specs->user_defined_type_p = false;
16956         }
16957     }
16958   else if (decl_specs->type)
16959     decl_specs->multiple_types_p = true;
16960   else
16961     {
16962       decl_specs->type = type_spec;
16963       decl_specs->user_defined_type_p = user_defined_p;
16964       decl_specs->redefined_builtin_type = NULL_TREE;
16965     }
16966 }
16967
16968 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16969    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16970
16971 static bool
16972 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16973 {
16974   return decl_specifiers->specs[(int) ds_friend] != 0;
16975 }
16976
16977 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
16978    issue an error message indicating that TOKEN_DESC was expected.
16979
16980    Returns the token consumed, if the token had the appropriate type.
16981    Otherwise, returns NULL.  */
16982
16983 static cp_token *
16984 cp_parser_require (cp_parser* parser,
16985                    enum cpp_ttype type,
16986                    const char* token_desc)
16987 {
16988   if (cp_lexer_next_token_is (parser->lexer, type))
16989     return cp_lexer_consume_token (parser->lexer);
16990   else
16991     {
16992       /* Output the MESSAGE -- unless we're parsing tentatively.  */
16993       if (!cp_parser_simulate_error (parser))
16994         {
16995           char *message = concat ("expected ", token_desc, NULL);
16996           cp_parser_error (parser, message);
16997           free (message);
16998         }
16999       return NULL;
17000     }
17001 }
17002
17003 /* An error message is produced if the next token is not '>'.
17004    All further tokens are skipped until the desired token is
17005    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
17006
17007 static void
17008 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17009 {
17010   /* Current level of '< ... >'.  */
17011   unsigned level = 0;
17012   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
17013   unsigned nesting_depth = 0;
17014
17015   /* Are we ready, yet?  If not, issue error message.  */
17016   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17017     return;
17018
17019   /* Skip tokens until the desired token is found.  */
17020   while (true)
17021     {
17022       /* Peek at the next token.  */
17023       switch (cp_lexer_peek_token (parser->lexer)->type)
17024         {
17025         case CPP_LESS:
17026           if (!nesting_depth)
17027             ++level;
17028           break;
17029
17030         case CPP_GREATER:
17031           if (!nesting_depth && level-- == 0)
17032             {
17033               /* We've reached the token we want, consume it and stop.  */
17034               cp_lexer_consume_token (parser->lexer);
17035               return;
17036             }
17037           break;
17038
17039         case CPP_OPEN_PAREN:
17040         case CPP_OPEN_SQUARE:
17041           ++nesting_depth;
17042           break;
17043
17044         case CPP_CLOSE_PAREN:
17045         case CPP_CLOSE_SQUARE:
17046           if (nesting_depth-- == 0)
17047             return;
17048           break;
17049
17050         case CPP_EOF:
17051         case CPP_PRAGMA_EOL:
17052         case CPP_SEMICOLON:
17053         case CPP_OPEN_BRACE:
17054         case CPP_CLOSE_BRACE:
17055           /* The '>' was probably forgotten, don't look further.  */
17056           return;
17057
17058         default:
17059           break;
17060         }
17061
17062       /* Consume this token.  */
17063       cp_lexer_consume_token (parser->lexer);
17064     }
17065 }
17066
17067 /* If the next token is the indicated keyword, consume it.  Otherwise,
17068    issue an error message indicating that TOKEN_DESC was expected.
17069
17070    Returns the token consumed, if the token had the appropriate type.
17071    Otherwise, returns NULL.  */
17072
17073 static cp_token *
17074 cp_parser_require_keyword (cp_parser* parser,
17075                            enum rid keyword,
17076                            const char* token_desc)
17077 {
17078   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17079
17080   if (token && token->keyword != keyword)
17081     {
17082       dyn_string_t error_msg;
17083
17084       /* Format the error message.  */
17085       error_msg = dyn_string_new (0);
17086       dyn_string_append_cstr (error_msg, "expected ");
17087       dyn_string_append_cstr (error_msg, token_desc);
17088       cp_parser_error (parser, error_msg->s);
17089       dyn_string_delete (error_msg);
17090       return NULL;
17091     }
17092
17093   return token;
17094 }
17095
17096 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17097    function-definition.  */
17098
17099 static bool
17100 cp_parser_token_starts_function_definition_p (cp_token* token)
17101 {
17102   return (/* An ordinary function-body begins with an `{'.  */
17103           token->type == CPP_OPEN_BRACE
17104           /* A ctor-initializer begins with a `:'.  */
17105           || token->type == CPP_COLON
17106           /* A function-try-block begins with `try'.  */
17107           || token->keyword == RID_TRY
17108           /* The named return value extension begins with `return'.  */
17109           || token->keyword == RID_RETURN);
17110 }
17111
17112 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17113    definition.  */
17114
17115 static bool
17116 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17117 {
17118   cp_token *token;
17119
17120   token = cp_lexer_peek_token (parser->lexer);
17121   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17122 }
17123
17124 /* Returns TRUE iff the next token is the "," or ">" ending a
17125    template-argument.  */
17126
17127 static bool
17128 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17129 {
17130   cp_token *token;
17131
17132   token = cp_lexer_peek_token (parser->lexer);
17133   return (token->type == CPP_COMMA 
17134           || token->type == CPP_GREATER
17135           || token->type == CPP_ELLIPSIS);
17136 }
17137
17138 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17139    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
17140
17141 static bool
17142 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17143                                                      size_t n)
17144 {
17145   cp_token *token;
17146
17147   token = cp_lexer_peek_nth_token (parser->lexer, n);
17148   if (token->type == CPP_LESS)
17149     return true;
17150   /* Check for the sequence `<::' in the original code. It would be lexed as
17151      `[:', where `[' is a digraph, and there is no whitespace before
17152      `:'.  */
17153   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17154     {
17155       cp_token *token2;
17156       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17157       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17158         return true;
17159     }
17160   return false;
17161 }
17162
17163 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17164    or none_type otherwise.  */
17165
17166 static enum tag_types
17167 cp_parser_token_is_class_key (cp_token* token)
17168 {
17169   switch (token->keyword)
17170     {
17171     case RID_CLASS:
17172       return class_type;
17173     case RID_STRUCT:
17174       return record_type;
17175     case RID_UNION:
17176       return union_type;
17177
17178     default:
17179       return none_type;
17180     }
17181 }
17182
17183 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
17184
17185 static void
17186 cp_parser_check_class_key (enum tag_types class_key, tree type)
17187 {
17188   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17189     pedwarn ("%qs tag used in naming %q#T",
17190             class_key == union_type ? "union"
17191              : class_key == record_type ? "struct" : "class",
17192              type);
17193 }
17194
17195 /* Issue an error message if DECL is redeclared with different
17196    access than its original declaration [class.access.spec/3].
17197    This applies to nested classes and nested class templates.
17198    [class.mem/1].  */
17199
17200 static void
17201 cp_parser_check_access_in_redeclaration (tree decl)
17202 {
17203   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17204     return;
17205
17206   if ((TREE_PRIVATE (decl)
17207        != (current_access_specifier == access_private_node))
17208       || (TREE_PROTECTED (decl)
17209           != (current_access_specifier == access_protected_node)))
17210     error ("%qD redeclared with different access", decl);
17211 }
17212
17213 /* Look for the `template' keyword, as a syntactic disambiguator.
17214    Return TRUE iff it is present, in which case it will be
17215    consumed.  */
17216
17217 static bool
17218 cp_parser_optional_template_keyword (cp_parser *parser)
17219 {
17220   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17221     {
17222       /* The `template' keyword can only be used within templates;
17223          outside templates the parser can always figure out what is a
17224          template and what is not.  */
17225       if (!processing_template_decl)
17226         {
17227           error ("%<template%> (as a disambiguator) is only allowed "
17228                  "within templates");
17229           /* If this part of the token stream is rescanned, the same
17230              error message would be generated.  So, we purge the token
17231              from the stream.  */
17232           cp_lexer_purge_token (parser->lexer);
17233           return false;
17234         }
17235       else
17236         {
17237           /* Consume the `template' keyword.  */
17238           cp_lexer_consume_token (parser->lexer);
17239           return true;
17240         }
17241     }
17242
17243   return false;
17244 }
17245
17246 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
17247    set PARSER->SCOPE, and perform other related actions.  */
17248
17249 static void
17250 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17251 {
17252   int i;
17253   struct tree_check *check_value;
17254   deferred_access_check *chk;
17255   VEC (deferred_access_check,gc) *checks;
17256
17257   /* Get the stored value.  */
17258   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17259   /* Perform any access checks that were deferred.  */
17260   checks = check_value->checks;
17261   if (checks)
17262     {
17263       for (i = 0 ;
17264            VEC_iterate (deferred_access_check, checks, i, chk) ;
17265            ++i)
17266         {
17267           perform_or_defer_access_check (chk->binfo,
17268                                          chk->decl,
17269                                          chk->diag_decl);
17270         }
17271     }
17272   /* Set the scope from the stored value.  */
17273   parser->scope = check_value->value;
17274   parser->qualifying_scope = check_value->qualifying_scope;
17275   parser->object_scope = NULL_TREE;
17276 }
17277
17278 /* Consume tokens up through a non-nested END token.  */
17279
17280 static void
17281 cp_parser_cache_group (cp_parser *parser,
17282                        enum cpp_ttype end,
17283                        unsigned depth)
17284 {
17285   while (true)
17286     {
17287       cp_token *token;
17288
17289       /* Abort a parenthesized expression if we encounter a brace.  */
17290       if ((end == CPP_CLOSE_PAREN || depth == 0)
17291           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17292         return;
17293       /* If we've reached the end of the file, stop.  */
17294       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17295           || (end != CPP_PRAGMA_EOL
17296               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17297         return;
17298       /* Consume the next token.  */
17299       token = cp_lexer_consume_token (parser->lexer);
17300       /* See if it starts a new group.  */
17301       if (token->type == CPP_OPEN_BRACE)
17302         {
17303           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17304           if (depth == 0)
17305             return;
17306         }
17307       else if (token->type == CPP_OPEN_PAREN)
17308         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17309       else if (token->type == CPP_PRAGMA)
17310         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17311       else if (token->type == end)
17312         return;
17313     }
17314 }
17315
17316 /* Begin parsing tentatively.  We always save tokens while parsing
17317    tentatively so that if the tentative parsing fails we can restore the
17318    tokens.  */
17319
17320 static void
17321 cp_parser_parse_tentatively (cp_parser* parser)
17322 {
17323   /* Enter a new parsing context.  */
17324   parser->context = cp_parser_context_new (parser->context);
17325   /* Begin saving tokens.  */
17326   cp_lexer_save_tokens (parser->lexer);
17327   /* In order to avoid repetitive access control error messages,
17328      access checks are queued up until we are no longer parsing
17329      tentatively.  */
17330   push_deferring_access_checks (dk_deferred);
17331 }
17332
17333 /* Commit to the currently active tentative parse.  */
17334
17335 static void
17336 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17337 {
17338   cp_parser_context *context;
17339   cp_lexer *lexer;
17340
17341   /* Mark all of the levels as committed.  */
17342   lexer = parser->lexer;
17343   for (context = parser->context; context->next; context = context->next)
17344     {
17345       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17346         break;
17347       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17348       while (!cp_lexer_saving_tokens (lexer))
17349         lexer = lexer->next;
17350       cp_lexer_commit_tokens (lexer);
17351     }
17352 }
17353
17354 /* Abort the currently active tentative parse.  All consumed tokens
17355    will be rolled back, and no diagnostics will be issued.  */
17356
17357 static void
17358 cp_parser_abort_tentative_parse (cp_parser* parser)
17359 {
17360   cp_parser_simulate_error (parser);
17361   /* Now, pretend that we want to see if the construct was
17362      successfully parsed.  */
17363   cp_parser_parse_definitely (parser);
17364 }
17365
17366 /* Stop parsing tentatively.  If a parse error has occurred, restore the
17367    token stream.  Otherwise, commit to the tokens we have consumed.
17368    Returns true if no error occurred; false otherwise.  */
17369
17370 static bool
17371 cp_parser_parse_definitely (cp_parser* parser)
17372 {
17373   bool error_occurred;
17374   cp_parser_context *context;
17375
17376   /* Remember whether or not an error occurred, since we are about to
17377      destroy that information.  */
17378   error_occurred = cp_parser_error_occurred (parser);
17379   /* Remove the topmost context from the stack.  */
17380   context = parser->context;
17381   parser->context = context->next;
17382   /* If no parse errors occurred, commit to the tentative parse.  */
17383   if (!error_occurred)
17384     {
17385       /* Commit to the tokens read tentatively, unless that was
17386          already done.  */
17387       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17388         cp_lexer_commit_tokens (parser->lexer);
17389
17390       pop_to_parent_deferring_access_checks ();
17391     }
17392   /* Otherwise, if errors occurred, roll back our state so that things
17393      are just as they were before we began the tentative parse.  */
17394   else
17395     {
17396       cp_lexer_rollback_tokens (parser->lexer);
17397       pop_deferring_access_checks ();
17398     }
17399   /* Add the context to the front of the free list.  */
17400   context->next = cp_parser_context_free_list;
17401   cp_parser_context_free_list = context;
17402
17403   return !error_occurred;
17404 }
17405
17406 /* Returns true if we are parsing tentatively and are not committed to
17407    this tentative parse.  */
17408
17409 static bool
17410 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17411 {
17412   return (cp_parser_parsing_tentatively (parser)
17413           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17414 }
17415
17416 /* Returns nonzero iff an error has occurred during the most recent
17417    tentative parse.  */
17418
17419 static bool
17420 cp_parser_error_occurred (cp_parser* parser)
17421 {
17422   return (cp_parser_parsing_tentatively (parser)
17423           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17424 }
17425
17426 /* Returns nonzero if GNU extensions are allowed.  */
17427
17428 static bool
17429 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17430 {
17431   return parser->allow_gnu_extensions_p;
17432 }
17433 \f
17434 /* Objective-C++ Productions */
17435
17436
17437 /* Parse an Objective-C expression, which feeds into a primary-expression
17438    above.
17439
17440    objc-expression:
17441      objc-message-expression
17442      objc-string-literal
17443      objc-encode-expression
17444      objc-protocol-expression
17445      objc-selector-expression
17446
17447   Returns a tree representation of the expression.  */
17448
17449 static tree
17450 cp_parser_objc_expression (cp_parser* parser)
17451 {
17452   /* Try to figure out what kind of declaration is present.  */
17453   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17454
17455   switch (kwd->type)
17456     {
17457     case CPP_OPEN_SQUARE:
17458       return cp_parser_objc_message_expression (parser);
17459
17460     case CPP_OBJC_STRING:
17461       kwd = cp_lexer_consume_token (parser->lexer);
17462       return objc_build_string_object (kwd->u.value);
17463
17464     case CPP_KEYWORD:
17465       switch (kwd->keyword)
17466         {
17467         case RID_AT_ENCODE:
17468           return cp_parser_objc_encode_expression (parser);
17469
17470         case RID_AT_PROTOCOL:
17471           return cp_parser_objc_protocol_expression (parser);
17472
17473         case RID_AT_SELECTOR:
17474           return cp_parser_objc_selector_expression (parser);
17475
17476         default:
17477           break;
17478         }
17479     default:
17480       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17481       cp_parser_skip_to_end_of_block_or_statement (parser);
17482     }
17483
17484   return error_mark_node;
17485 }
17486
17487 /* Parse an Objective-C message expression.
17488
17489    objc-message-expression:
17490      [ objc-message-receiver objc-message-args ]
17491
17492    Returns a representation of an Objective-C message.  */
17493
17494 static tree
17495 cp_parser_objc_message_expression (cp_parser* parser)
17496 {
17497   tree receiver, messageargs;
17498
17499   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
17500   receiver = cp_parser_objc_message_receiver (parser);
17501   messageargs = cp_parser_objc_message_args (parser);
17502   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17503
17504   return objc_build_message_expr (build_tree_list (receiver, messageargs));
17505 }
17506
17507 /* Parse an objc-message-receiver.
17508
17509    objc-message-receiver:
17510      expression
17511      simple-type-specifier
17512
17513   Returns a representation of the type or expression.  */
17514
17515 static tree
17516 cp_parser_objc_message_receiver (cp_parser* parser)
17517 {
17518   tree rcv;
17519
17520   /* An Objective-C message receiver may be either (1) a type
17521      or (2) an expression.  */
17522   cp_parser_parse_tentatively (parser);
17523   rcv = cp_parser_expression (parser, false);
17524
17525   if (cp_parser_parse_definitely (parser))
17526     return rcv;
17527
17528   rcv = cp_parser_simple_type_specifier (parser,
17529                                          /*decl_specs=*/NULL,
17530                                          CP_PARSER_FLAGS_NONE);
17531
17532   return objc_get_class_reference (rcv);
17533 }
17534
17535 /* Parse the arguments and selectors comprising an Objective-C message.
17536
17537    objc-message-args:
17538      objc-selector
17539      objc-selector-args
17540      objc-selector-args , objc-comma-args
17541
17542    objc-selector-args:
17543      objc-selector [opt] : assignment-expression
17544      objc-selector-args objc-selector [opt] : assignment-expression
17545
17546    objc-comma-args:
17547      assignment-expression
17548      objc-comma-args , assignment-expression
17549
17550    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17551    selector arguments and TREE_VALUE containing a list of comma
17552    arguments.  */
17553
17554 static tree
17555 cp_parser_objc_message_args (cp_parser* parser)
17556 {
17557   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17558   bool maybe_unary_selector_p = true;
17559   cp_token *token = cp_lexer_peek_token (parser->lexer);
17560
17561   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17562     {
17563       tree selector = NULL_TREE, arg;
17564
17565       if (token->type != CPP_COLON)
17566         selector = cp_parser_objc_selector (parser);
17567
17568       /* Detect if we have a unary selector.  */
17569       if (maybe_unary_selector_p
17570           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17571         return build_tree_list (selector, NULL_TREE);
17572
17573       maybe_unary_selector_p = false;
17574       cp_parser_require (parser, CPP_COLON, "`:'");
17575       arg = cp_parser_assignment_expression (parser, false);
17576
17577       sel_args
17578         = chainon (sel_args,
17579                    build_tree_list (selector, arg));
17580
17581       token = cp_lexer_peek_token (parser->lexer);
17582     }
17583
17584   /* Handle non-selector arguments, if any. */
17585   while (token->type == CPP_COMMA)
17586     {
17587       tree arg;
17588
17589       cp_lexer_consume_token (parser->lexer);
17590       arg = cp_parser_assignment_expression (parser, false);
17591
17592       addl_args
17593         = chainon (addl_args,
17594                    build_tree_list (NULL_TREE, arg));
17595
17596       token = cp_lexer_peek_token (parser->lexer);
17597     }
17598
17599   return build_tree_list (sel_args, addl_args);
17600 }
17601
17602 /* Parse an Objective-C encode expression.
17603
17604    objc-encode-expression:
17605      @encode objc-typename
17606
17607    Returns an encoded representation of the type argument.  */
17608
17609 static tree
17610 cp_parser_objc_encode_expression (cp_parser* parser)
17611 {
17612   tree type;
17613
17614   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
17615   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17616   type = complete_type (cp_parser_type_id (parser));
17617   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17618
17619   if (!type)
17620     {
17621       error ("%<@encode%> must specify a type as an argument");
17622       return error_mark_node;
17623     }
17624
17625   return objc_build_encode_expr (type);
17626 }
17627
17628 /* Parse an Objective-C @defs expression.  */
17629
17630 static tree
17631 cp_parser_objc_defs_expression (cp_parser *parser)
17632 {
17633   tree name;
17634
17635   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
17636   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17637   name = cp_parser_identifier (parser);
17638   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17639
17640   return objc_get_class_ivars (name);
17641 }
17642
17643 /* Parse an Objective-C protocol expression.
17644
17645   objc-protocol-expression:
17646     @protocol ( identifier )
17647
17648   Returns a representation of the protocol expression.  */
17649
17650 static tree
17651 cp_parser_objc_protocol_expression (cp_parser* parser)
17652 {
17653   tree proto;
17654
17655   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17656   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17657   proto = cp_parser_identifier (parser);
17658   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17659
17660   return objc_build_protocol_expr (proto);
17661 }
17662
17663 /* Parse an Objective-C selector expression.
17664
17665    objc-selector-expression:
17666      @selector ( objc-method-signature )
17667
17668    objc-method-signature:
17669      objc-selector
17670      objc-selector-seq
17671
17672    objc-selector-seq:
17673      objc-selector :
17674      objc-selector-seq objc-selector :
17675
17676   Returns a representation of the method selector.  */
17677
17678 static tree
17679 cp_parser_objc_selector_expression (cp_parser* parser)
17680 {
17681   tree sel_seq = NULL_TREE;
17682   bool maybe_unary_selector_p = true;
17683   cp_token *token;
17684
17685   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
17686   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17687   token = cp_lexer_peek_token (parser->lexer);
17688
17689   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17690          || token->type == CPP_SCOPE)
17691     {
17692       tree selector = NULL_TREE;
17693
17694       if (token->type != CPP_COLON
17695           || token->type == CPP_SCOPE)
17696         selector = cp_parser_objc_selector (parser);
17697
17698       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17699           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17700         {
17701           /* Detect if we have a unary selector.  */
17702           if (maybe_unary_selector_p)
17703             {
17704               sel_seq = selector;
17705               goto finish_selector;
17706             }
17707           else
17708             {
17709               cp_parser_error (parser, "expected %<:%>");
17710             }
17711         }
17712       maybe_unary_selector_p = false;
17713       token = cp_lexer_consume_token (parser->lexer);
17714
17715       if (token->type == CPP_SCOPE)
17716         {
17717           sel_seq
17718             = chainon (sel_seq,
17719                        build_tree_list (selector, NULL_TREE));
17720           sel_seq
17721             = chainon (sel_seq,
17722                        build_tree_list (NULL_TREE, NULL_TREE));
17723         }
17724       else
17725         sel_seq
17726           = chainon (sel_seq,
17727                      build_tree_list (selector, NULL_TREE));
17728
17729       token = cp_lexer_peek_token (parser->lexer);
17730     }
17731
17732  finish_selector:
17733   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17734
17735   return objc_build_selector_expr (sel_seq);
17736 }
17737
17738 /* Parse a list of identifiers.
17739
17740    objc-identifier-list:
17741      identifier
17742      objc-identifier-list , identifier
17743
17744    Returns a TREE_LIST of identifier nodes.  */
17745
17746 static tree
17747 cp_parser_objc_identifier_list (cp_parser* parser)
17748 {
17749   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17750   cp_token *sep = cp_lexer_peek_token (parser->lexer);
17751
17752   while (sep->type == CPP_COMMA)
17753     {
17754       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17755       list = chainon (list,
17756                       build_tree_list (NULL_TREE,
17757                                        cp_parser_identifier (parser)));
17758       sep = cp_lexer_peek_token (parser->lexer);
17759     }
17760
17761   return list;
17762 }
17763
17764 /* Parse an Objective-C alias declaration.
17765
17766    objc-alias-declaration:
17767      @compatibility_alias identifier identifier ;
17768
17769    This function registers the alias mapping with the Objective-C front end.
17770    It returns nothing.  */
17771
17772 static void
17773 cp_parser_objc_alias_declaration (cp_parser* parser)
17774 {
17775   tree alias, orig;
17776
17777   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17778   alias = cp_parser_identifier (parser);
17779   orig = cp_parser_identifier (parser);
17780   objc_declare_alias (alias, orig);
17781   cp_parser_consume_semicolon_at_end_of_statement (parser);
17782 }
17783
17784 /* Parse an Objective-C class forward-declaration.
17785
17786    objc-class-declaration:
17787      @class objc-identifier-list ;
17788
17789    The function registers the forward declarations with the Objective-C
17790    front end.  It returns nothing.  */
17791
17792 static void
17793 cp_parser_objc_class_declaration (cp_parser* parser)
17794 {
17795   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17796   objc_declare_class (cp_parser_objc_identifier_list (parser));
17797   cp_parser_consume_semicolon_at_end_of_statement (parser);
17798 }
17799
17800 /* Parse a list of Objective-C protocol references.
17801
17802    objc-protocol-refs-opt:
17803      objc-protocol-refs [opt]
17804
17805    objc-protocol-refs:
17806      < objc-identifier-list >
17807
17808    Returns a TREE_LIST of identifiers, if any.  */
17809
17810 static tree
17811 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17812 {
17813   tree protorefs = NULL_TREE;
17814
17815   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17816     {
17817       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17818       protorefs = cp_parser_objc_identifier_list (parser);
17819       cp_parser_require (parser, CPP_GREATER, "`>'");
17820     }
17821
17822   return protorefs;
17823 }
17824
17825 /* Parse a Objective-C visibility specification.  */
17826
17827 static void
17828 cp_parser_objc_visibility_spec (cp_parser* parser)
17829 {
17830   cp_token *vis = cp_lexer_peek_token (parser->lexer);
17831
17832   switch (vis->keyword)
17833     {
17834     case RID_AT_PRIVATE:
17835       objc_set_visibility (2);
17836       break;
17837     case RID_AT_PROTECTED:
17838       objc_set_visibility (0);
17839       break;
17840     case RID_AT_PUBLIC:
17841       objc_set_visibility (1);
17842       break;
17843     default:
17844       return;
17845     }
17846
17847   /* Eat '@private'/'@protected'/'@public'.  */
17848   cp_lexer_consume_token (parser->lexer);
17849 }
17850
17851 /* Parse an Objective-C method type.  */
17852
17853 static void
17854 cp_parser_objc_method_type (cp_parser* parser)
17855 {
17856   objc_set_method_type
17857    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17858     ? PLUS_EXPR
17859     : MINUS_EXPR);
17860 }
17861
17862 /* Parse an Objective-C protocol qualifier.  */
17863
17864 static tree
17865 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17866 {
17867   tree quals = NULL_TREE, node;
17868   cp_token *token = cp_lexer_peek_token (parser->lexer);
17869
17870   node = token->u.value;
17871
17872   while (node && TREE_CODE (node) == IDENTIFIER_NODE
17873          && (node == ridpointers [(int) RID_IN]
17874              || node == ridpointers [(int) RID_OUT]
17875              || node == ridpointers [(int) RID_INOUT]
17876              || node == ridpointers [(int) RID_BYCOPY]
17877              || node == ridpointers [(int) RID_BYREF]
17878              || node == ridpointers [(int) RID_ONEWAY]))
17879     {
17880       quals = tree_cons (NULL_TREE, node, quals);
17881       cp_lexer_consume_token (parser->lexer);
17882       token = cp_lexer_peek_token (parser->lexer);
17883       node = token->u.value;
17884     }
17885
17886   return quals;
17887 }
17888
17889 /* Parse an Objective-C typename.  */
17890
17891 static tree
17892 cp_parser_objc_typename (cp_parser* parser)
17893 {
17894   tree typename = NULL_TREE;
17895
17896   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17897     {
17898       tree proto_quals, cp_type = NULL_TREE;
17899
17900       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17901       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17902
17903       /* An ObjC type name may consist of just protocol qualifiers, in which
17904          case the type shall default to 'id'.  */
17905       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17906         cp_type = cp_parser_type_id (parser);
17907
17908       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17909       typename = build_tree_list (proto_quals, cp_type);
17910     }
17911
17912   return typename;
17913 }
17914
17915 /* Check to see if TYPE refers to an Objective-C selector name.  */
17916
17917 static bool
17918 cp_parser_objc_selector_p (enum cpp_ttype type)
17919 {
17920   return (type == CPP_NAME || type == CPP_KEYWORD
17921           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17922           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17923           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17924           || type == CPP_XOR || type == CPP_XOR_EQ);
17925 }
17926
17927 /* Parse an Objective-C selector.  */
17928
17929 static tree
17930 cp_parser_objc_selector (cp_parser* parser)
17931 {
17932   cp_token *token = cp_lexer_consume_token (parser->lexer);
17933
17934   if (!cp_parser_objc_selector_p (token->type))
17935     {
17936       error ("invalid Objective-C++ selector name");
17937       return error_mark_node;
17938     }
17939
17940   /* C++ operator names are allowed to appear in ObjC selectors.  */
17941   switch (token->type)
17942     {
17943     case CPP_AND_AND: return get_identifier ("and");
17944     case CPP_AND_EQ: return get_identifier ("and_eq");
17945     case CPP_AND: return get_identifier ("bitand");
17946     case CPP_OR: return get_identifier ("bitor");
17947     case CPP_COMPL: return get_identifier ("compl");
17948     case CPP_NOT: return get_identifier ("not");
17949     case CPP_NOT_EQ: return get_identifier ("not_eq");
17950     case CPP_OR_OR: return get_identifier ("or");
17951     case CPP_OR_EQ: return get_identifier ("or_eq");
17952     case CPP_XOR: return get_identifier ("xor");
17953     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17954     default: return token->u.value;
17955     }
17956 }
17957
17958 /* Parse an Objective-C params list.  */
17959
17960 static tree
17961 cp_parser_objc_method_keyword_params (cp_parser* parser)
17962 {
17963   tree params = NULL_TREE;
17964   bool maybe_unary_selector_p = true;
17965   cp_token *token = cp_lexer_peek_token (parser->lexer);
17966
17967   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17968     {
17969       tree selector = NULL_TREE, typename, identifier;
17970
17971       if (token->type != CPP_COLON)
17972         selector = cp_parser_objc_selector (parser);
17973
17974       /* Detect if we have a unary selector.  */
17975       if (maybe_unary_selector_p
17976           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17977         return selector;
17978
17979       maybe_unary_selector_p = false;
17980       cp_parser_require (parser, CPP_COLON, "`:'");
17981       typename = cp_parser_objc_typename (parser);
17982       identifier = cp_parser_identifier (parser);
17983
17984       params
17985         = chainon (params,
17986                    objc_build_keyword_decl (selector,
17987                                             typename,
17988                                             identifier));
17989
17990       token = cp_lexer_peek_token (parser->lexer);
17991     }
17992
17993   return params;
17994 }
17995
17996 /* Parse the non-keyword Objective-C params.  */
17997
17998 static tree
17999 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18000 {
18001   tree params = make_node (TREE_LIST);
18002   cp_token *token = cp_lexer_peek_token (parser->lexer);
18003   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
18004
18005   while (token->type == CPP_COMMA)
18006     {
18007       cp_parameter_declarator *parmdecl;
18008       tree parm;
18009
18010       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18011       token = cp_lexer_peek_token (parser->lexer);
18012
18013       if (token->type == CPP_ELLIPSIS)
18014         {
18015           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
18016           *ellipsisp = true;
18017           break;
18018         }
18019
18020       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18021       parm = grokdeclarator (parmdecl->declarator,
18022                              &parmdecl->decl_specifiers,
18023                              PARM, /*initialized=*/0,
18024                              /*attrlist=*/NULL);
18025
18026       chainon (params, build_tree_list (NULL_TREE, parm));
18027       token = cp_lexer_peek_token (parser->lexer);
18028     }
18029
18030   return params;
18031 }
18032
18033 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
18034
18035 static void
18036 cp_parser_objc_interstitial_code (cp_parser* parser)
18037 {
18038   cp_token *token = cp_lexer_peek_token (parser->lexer);
18039
18040   /* If the next token is `extern' and the following token is a string
18041      literal, then we have a linkage specification.  */
18042   if (token->keyword == RID_EXTERN
18043       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18044     cp_parser_linkage_specification (parser);
18045   /* Handle #pragma, if any.  */
18046   else if (token->type == CPP_PRAGMA)
18047     cp_parser_pragma (parser, pragma_external);
18048   /* Allow stray semicolons.  */
18049   else if (token->type == CPP_SEMICOLON)
18050     cp_lexer_consume_token (parser->lexer);
18051   /* Finally, try to parse a block-declaration, or a function-definition.  */
18052   else
18053     cp_parser_block_declaration (parser, /*statement_p=*/false);
18054 }
18055
18056 /* Parse a method signature.  */
18057
18058 static tree
18059 cp_parser_objc_method_signature (cp_parser* parser)
18060 {
18061   tree rettype, kwdparms, optparms;
18062   bool ellipsis = false;
18063
18064   cp_parser_objc_method_type (parser);
18065   rettype = cp_parser_objc_typename (parser);
18066   kwdparms = cp_parser_objc_method_keyword_params (parser);
18067   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18068
18069   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18070 }
18071
18072 /* Pars an Objective-C method prototype list.  */
18073
18074 static void
18075 cp_parser_objc_method_prototype_list (cp_parser* parser)
18076 {
18077   cp_token *token = cp_lexer_peek_token (parser->lexer);
18078
18079   while (token->keyword != RID_AT_END)
18080     {
18081       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18082         {
18083           objc_add_method_declaration
18084            (cp_parser_objc_method_signature (parser));
18085           cp_parser_consume_semicolon_at_end_of_statement (parser);
18086         }
18087       else
18088         /* Allow for interspersed non-ObjC++ code.  */
18089         cp_parser_objc_interstitial_code (parser);
18090
18091       token = cp_lexer_peek_token (parser->lexer);
18092     }
18093
18094   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18095   objc_finish_interface ();
18096 }
18097
18098 /* Parse an Objective-C method definition list.  */
18099
18100 static void
18101 cp_parser_objc_method_definition_list (cp_parser* parser)
18102 {
18103   cp_token *token = cp_lexer_peek_token (parser->lexer);
18104
18105   while (token->keyword != RID_AT_END)
18106     {
18107       tree meth;
18108
18109       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18110         {
18111           push_deferring_access_checks (dk_deferred);
18112           objc_start_method_definition
18113            (cp_parser_objc_method_signature (parser));
18114
18115           /* For historical reasons, we accept an optional semicolon.  */
18116           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18117             cp_lexer_consume_token (parser->lexer);
18118
18119           perform_deferred_access_checks ();
18120           stop_deferring_access_checks ();
18121           meth = cp_parser_function_definition_after_declarator (parser,
18122                                                                  false);
18123           pop_deferring_access_checks ();
18124           objc_finish_method_definition (meth);
18125         }
18126       else
18127         /* Allow for interspersed non-ObjC++ code.  */
18128         cp_parser_objc_interstitial_code (parser);
18129
18130       token = cp_lexer_peek_token (parser->lexer);
18131     }
18132
18133   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18134   objc_finish_implementation ();
18135 }
18136
18137 /* Parse Objective-C ivars.  */
18138
18139 static void
18140 cp_parser_objc_class_ivars (cp_parser* parser)
18141 {
18142   cp_token *token = cp_lexer_peek_token (parser->lexer);
18143
18144   if (token->type != CPP_OPEN_BRACE)
18145     return;     /* No ivars specified.  */
18146
18147   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
18148   token = cp_lexer_peek_token (parser->lexer);
18149
18150   while (token->type != CPP_CLOSE_BRACE)
18151     {
18152       cp_decl_specifier_seq declspecs;
18153       int decl_class_or_enum_p;
18154       tree prefix_attributes;
18155
18156       cp_parser_objc_visibility_spec (parser);
18157
18158       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18159         break;
18160
18161       cp_parser_decl_specifier_seq (parser,
18162                                     CP_PARSER_FLAGS_OPTIONAL,
18163                                     &declspecs,
18164                                     &decl_class_or_enum_p);
18165       prefix_attributes = declspecs.attributes;
18166       declspecs.attributes = NULL_TREE;
18167
18168       /* Keep going until we hit the `;' at the end of the
18169          declaration.  */
18170       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18171         {
18172           tree width = NULL_TREE, attributes, first_attribute, decl;
18173           cp_declarator *declarator = NULL;
18174           int ctor_dtor_or_conv_p;
18175
18176           /* Check for a (possibly unnamed) bitfield declaration.  */
18177           token = cp_lexer_peek_token (parser->lexer);
18178           if (token->type == CPP_COLON)
18179             goto eat_colon;
18180
18181           if (token->type == CPP_NAME
18182               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18183                   == CPP_COLON))
18184             {
18185               /* Get the name of the bitfield.  */
18186               declarator = make_id_declarator (NULL_TREE,
18187                                                cp_parser_identifier (parser),
18188                                                sfk_none);
18189
18190              eat_colon:
18191               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18192               /* Get the width of the bitfield.  */
18193               width
18194                 = cp_parser_constant_expression (parser,
18195                                                  /*allow_non_constant=*/false,
18196                                                  NULL);
18197             }
18198           else
18199             {
18200               /* Parse the declarator.  */
18201               declarator
18202                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18203                                         &ctor_dtor_or_conv_p,
18204                                         /*parenthesized_p=*/NULL,
18205                                         /*member_p=*/false);
18206             }
18207
18208           /* Look for attributes that apply to the ivar.  */
18209           attributes = cp_parser_attributes_opt (parser);
18210           /* Remember which attributes are prefix attributes and
18211              which are not.  */
18212           first_attribute = attributes;
18213           /* Combine the attributes.  */
18214           attributes = chainon (prefix_attributes, attributes);
18215
18216           if (width)
18217             {
18218               /* Create the bitfield declaration.  */
18219               decl = grokbitfield (declarator, &declspecs, width);
18220               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18221             }
18222           else
18223             decl = grokfield (declarator, &declspecs,
18224                               NULL_TREE, /*init_const_expr_p=*/false,
18225                               NULL_TREE, attributes);
18226
18227           /* Add the instance variable.  */
18228           objc_add_instance_variable (decl);
18229
18230           /* Reset PREFIX_ATTRIBUTES.  */
18231           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18232             attributes = TREE_CHAIN (attributes);
18233           if (attributes)
18234             TREE_CHAIN (attributes) = NULL_TREE;
18235
18236           token = cp_lexer_peek_token (parser->lexer);
18237
18238           if (token->type == CPP_COMMA)
18239             {
18240               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18241               continue;
18242             }
18243           break;
18244         }
18245
18246       cp_parser_consume_semicolon_at_end_of_statement (parser);
18247       token = cp_lexer_peek_token (parser->lexer);
18248     }
18249
18250   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
18251   /* For historical reasons, we accept an optional semicolon.  */
18252   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18253     cp_lexer_consume_token (parser->lexer);
18254 }
18255
18256 /* Parse an Objective-C protocol declaration.  */
18257
18258 static void
18259 cp_parser_objc_protocol_declaration (cp_parser* parser)
18260 {
18261   tree proto, protorefs;
18262   cp_token *tok;
18263
18264   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18265   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18266     {
18267       error ("identifier expected after %<@protocol%>");
18268       goto finish;
18269     }
18270
18271   /* See if we have a forward declaration or a definition.  */
18272   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18273
18274   /* Try a forward declaration first.  */
18275   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18276     {
18277       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18278      finish:
18279       cp_parser_consume_semicolon_at_end_of_statement (parser);
18280     }
18281
18282   /* Ok, we got a full-fledged definition (or at least should).  */
18283   else
18284     {
18285       proto = cp_parser_identifier (parser);
18286       protorefs = cp_parser_objc_protocol_refs_opt (parser);
18287       objc_start_protocol (proto, protorefs);
18288       cp_parser_objc_method_prototype_list (parser);
18289     }
18290 }
18291
18292 /* Parse an Objective-C superclass or category.  */
18293
18294 static void
18295 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18296                                                           tree *categ)
18297 {
18298   cp_token *next = cp_lexer_peek_token (parser->lexer);
18299
18300   *super = *categ = NULL_TREE;
18301   if (next->type == CPP_COLON)
18302     {
18303       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18304       *super = cp_parser_identifier (parser);
18305     }
18306   else if (next->type == CPP_OPEN_PAREN)
18307     {
18308       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18309       *categ = cp_parser_identifier (parser);
18310       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18311     }
18312 }
18313
18314 /* Parse an Objective-C class interface.  */
18315
18316 static void
18317 cp_parser_objc_class_interface (cp_parser* parser)
18318 {
18319   tree name, super, categ, protos;
18320
18321   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
18322   name = cp_parser_identifier (parser);
18323   cp_parser_objc_superclass_or_category (parser, &super, &categ);
18324   protos = cp_parser_objc_protocol_refs_opt (parser);
18325
18326   /* We have either a class or a category on our hands.  */
18327   if (categ)
18328     objc_start_category_interface (name, categ, protos);
18329   else
18330     {
18331       objc_start_class_interface (name, super, protos);
18332       /* Handle instance variable declarations, if any.  */
18333       cp_parser_objc_class_ivars (parser);
18334       objc_continue_interface ();
18335     }
18336
18337   cp_parser_objc_method_prototype_list (parser);
18338 }
18339
18340 /* Parse an Objective-C class implementation.  */
18341
18342 static void
18343 cp_parser_objc_class_implementation (cp_parser* parser)
18344 {
18345   tree name, super, categ;
18346
18347   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
18348   name = cp_parser_identifier (parser);
18349   cp_parser_objc_superclass_or_category (parser, &super, &categ);
18350
18351   /* We have either a class or a category on our hands.  */
18352   if (categ)
18353     objc_start_category_implementation (name, categ);
18354   else
18355     {
18356       objc_start_class_implementation (name, super);
18357       /* Handle instance variable declarations, if any.  */
18358       cp_parser_objc_class_ivars (parser);
18359       objc_continue_implementation ();
18360     }
18361
18362   cp_parser_objc_method_definition_list (parser);
18363 }
18364
18365 /* Consume the @end token and finish off the implementation.  */
18366
18367 static void
18368 cp_parser_objc_end_implementation (cp_parser* parser)
18369 {
18370   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18371   objc_finish_implementation ();
18372 }
18373
18374 /* Parse an Objective-C declaration.  */
18375
18376 static void
18377 cp_parser_objc_declaration (cp_parser* parser)
18378 {
18379   /* Try to figure out what kind of declaration is present.  */
18380   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18381
18382   switch (kwd->keyword)
18383     {
18384     case RID_AT_ALIAS:
18385       cp_parser_objc_alias_declaration (parser);
18386       break;
18387     case RID_AT_CLASS:
18388       cp_parser_objc_class_declaration (parser);
18389       break;
18390     case RID_AT_PROTOCOL:
18391       cp_parser_objc_protocol_declaration (parser);
18392       break;
18393     case RID_AT_INTERFACE:
18394       cp_parser_objc_class_interface (parser);
18395       break;
18396     case RID_AT_IMPLEMENTATION:
18397       cp_parser_objc_class_implementation (parser);
18398       break;
18399     case RID_AT_END:
18400       cp_parser_objc_end_implementation (parser);
18401       break;
18402     default:
18403       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18404       cp_parser_skip_to_end_of_block_or_statement (parser);
18405     }
18406 }
18407
18408 /* Parse an Objective-C try-catch-finally statement.
18409
18410    objc-try-catch-finally-stmt:
18411      @try compound-statement objc-catch-clause-seq [opt]
18412        objc-finally-clause [opt]
18413
18414    objc-catch-clause-seq:
18415      objc-catch-clause objc-catch-clause-seq [opt]
18416
18417    objc-catch-clause:
18418      @catch ( exception-declaration ) compound-statement
18419
18420    objc-finally-clause
18421      @finally compound-statement
18422
18423    Returns NULL_TREE.  */
18424
18425 static tree
18426 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18427   location_t location;
18428   tree stmt;
18429
18430   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18431   location = cp_lexer_peek_token (parser->lexer)->location;
18432   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18433      node, lest it get absorbed into the surrounding block.  */
18434   stmt = push_stmt_list ();
18435   cp_parser_compound_statement (parser, NULL, false);
18436   objc_begin_try_stmt (location, pop_stmt_list (stmt));
18437
18438   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18439     {
18440       cp_parameter_declarator *parmdecl;
18441       tree parm;
18442
18443       cp_lexer_consume_token (parser->lexer);
18444       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18445       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18446       parm = grokdeclarator (parmdecl->declarator,
18447                              &parmdecl->decl_specifiers,
18448                              PARM, /*initialized=*/0,
18449                              /*attrlist=*/NULL);
18450       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18451       objc_begin_catch_clause (parm);
18452       cp_parser_compound_statement (parser, NULL, false);
18453       objc_finish_catch_clause ();
18454     }
18455
18456   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18457     {
18458       cp_lexer_consume_token (parser->lexer);
18459       location = cp_lexer_peek_token (parser->lexer)->location;
18460       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18461          node, lest it get absorbed into the surrounding block.  */
18462       stmt = push_stmt_list ();
18463       cp_parser_compound_statement (parser, NULL, false);
18464       objc_build_finally_clause (location, pop_stmt_list (stmt));
18465     }
18466
18467   return objc_finish_try_stmt ();
18468 }
18469
18470 /* Parse an Objective-C synchronized statement.
18471
18472    objc-synchronized-stmt:
18473      @synchronized ( expression ) compound-statement
18474
18475    Returns NULL_TREE.  */
18476
18477 static tree
18478 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18479   location_t location;
18480   tree lock, stmt;
18481
18482   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18483
18484   location = cp_lexer_peek_token (parser->lexer)->location;
18485   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18486   lock = cp_parser_expression (parser, false);
18487   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18488
18489   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18490      node, lest it get absorbed into the surrounding block.  */
18491   stmt = push_stmt_list ();
18492   cp_parser_compound_statement (parser, NULL, false);
18493
18494   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18495 }
18496
18497 /* Parse an Objective-C throw statement.
18498
18499    objc-throw-stmt:
18500      @throw assignment-expression [opt] ;
18501
18502    Returns a constructed '@throw' statement.  */
18503
18504 static tree
18505 cp_parser_objc_throw_statement (cp_parser *parser) {
18506   tree expr = NULL_TREE;
18507
18508   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18509
18510   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18511     expr = cp_parser_assignment_expression (parser, false);
18512
18513   cp_parser_consume_semicolon_at_end_of_statement (parser);
18514
18515   return objc_build_throw_stmt (expr);
18516 }
18517
18518 /* Parse an Objective-C statement.  */
18519
18520 static tree
18521 cp_parser_objc_statement (cp_parser * parser) {
18522   /* Try to figure out what kind of declaration is present.  */
18523   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18524
18525   switch (kwd->keyword)
18526     {
18527     case RID_AT_TRY:
18528       return cp_parser_objc_try_catch_finally_statement (parser);
18529     case RID_AT_SYNCHRONIZED:
18530       return cp_parser_objc_synchronized_statement (parser);
18531     case RID_AT_THROW:
18532       return cp_parser_objc_throw_statement (parser);
18533     default:
18534       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18535       cp_parser_skip_to_end_of_block_or_statement (parser);
18536     }
18537
18538   return error_mark_node;
18539 }
18540 \f
18541 /* OpenMP 2.5 parsing routines.  */
18542
18543 /* Returns name of the next clause.
18544    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18545    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
18546    returned and the token is consumed.  */
18547
18548 static pragma_omp_clause
18549 cp_parser_omp_clause_name (cp_parser *parser)
18550 {
18551   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18552
18553   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18554     result = PRAGMA_OMP_CLAUSE_IF;
18555   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18556     result = PRAGMA_OMP_CLAUSE_DEFAULT;
18557   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18558     result = PRAGMA_OMP_CLAUSE_PRIVATE;
18559   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18560     {
18561       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18562       const char *p = IDENTIFIER_POINTER (id);
18563
18564       switch (p[0])
18565         {
18566         case 'c':
18567           if (!strcmp ("copyin", p))
18568             result = PRAGMA_OMP_CLAUSE_COPYIN;
18569           else if (!strcmp ("copyprivate", p))
18570             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18571           break;
18572         case 'f':
18573           if (!strcmp ("firstprivate", p))
18574             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18575           break;
18576         case 'l':
18577           if (!strcmp ("lastprivate", p))
18578             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18579           break;
18580         case 'n':
18581           if (!strcmp ("nowait", p))
18582             result = PRAGMA_OMP_CLAUSE_NOWAIT;
18583           else if (!strcmp ("num_threads", p))
18584             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18585           break;
18586         case 'o':
18587           if (!strcmp ("ordered", p))
18588             result = PRAGMA_OMP_CLAUSE_ORDERED;
18589           break;
18590         case 'r':
18591           if (!strcmp ("reduction", p))
18592             result = PRAGMA_OMP_CLAUSE_REDUCTION;
18593           break;
18594         case 's':
18595           if (!strcmp ("schedule", p))
18596             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18597           else if (!strcmp ("shared", p))
18598             result = PRAGMA_OMP_CLAUSE_SHARED;
18599           break;
18600         }
18601     }
18602
18603   if (result != PRAGMA_OMP_CLAUSE_NONE)
18604     cp_lexer_consume_token (parser->lexer);
18605
18606   return result;
18607 }
18608
18609 /* Validate that a clause of the given type does not already exist.  */
18610
18611 static void
18612 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18613 {
18614   tree c;
18615
18616   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18617     if (OMP_CLAUSE_CODE (c) == code)
18618       {
18619         error ("too many %qs clauses", name);
18620         break;
18621       }
18622 }
18623
18624 /* OpenMP 2.5:
18625    variable-list:
18626      identifier
18627      variable-list , identifier
18628
18629    In addition, we match a closing parenthesis.  An opening parenthesis
18630    will have been consumed by the caller.
18631
18632    If KIND is nonzero, create the appropriate node and install the decl
18633    in OMP_CLAUSE_DECL and add the node to the head of the list.
18634
18635    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18636    return the list created.  */
18637
18638 static tree
18639 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18640                                 tree list)
18641 {
18642   while (1)
18643     {
18644       tree name, decl;
18645
18646       name = cp_parser_id_expression (parser, /*template_p=*/false,
18647                                       /*check_dependency_p=*/true,
18648                                       /*template_p=*/NULL,
18649                                       /*declarator_p=*/false,
18650                                       /*optional_p=*/false);
18651       if (name == error_mark_node)
18652         goto skip_comma;
18653
18654       decl = cp_parser_lookup_name_simple (parser, name);
18655       if (decl == error_mark_node)
18656         cp_parser_name_lookup_error (parser, name, decl, NULL);
18657       else if (kind != 0)
18658         {
18659           tree u = build_omp_clause (kind);
18660           OMP_CLAUSE_DECL (u) = decl;
18661           OMP_CLAUSE_CHAIN (u) = list;
18662           list = u;
18663         }
18664       else
18665         list = tree_cons (decl, NULL_TREE, list);
18666
18667     get_comma:
18668       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18669         break;
18670       cp_lexer_consume_token (parser->lexer);
18671     }
18672
18673   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18674     {
18675       int ending;
18676
18677       /* Try to resync to an unnested comma.  Copied from
18678          cp_parser_parenthesized_expression_list.  */
18679     skip_comma:
18680       ending = cp_parser_skip_to_closing_parenthesis (parser,
18681                                                       /*recovering=*/true,
18682                                                       /*or_comma=*/true,
18683                                                       /*consume_paren=*/true);
18684       if (ending < 0)
18685         goto get_comma;
18686     }
18687
18688   return list;
18689 }
18690
18691 /* Similarly, but expect leading and trailing parenthesis.  This is a very
18692    common case for omp clauses.  */
18693
18694 static tree
18695 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18696 {
18697   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18698     return cp_parser_omp_var_list_no_open (parser, kind, list);
18699   return list;
18700 }
18701
18702 /* OpenMP 2.5:
18703    default ( shared | none ) */
18704
18705 static tree
18706 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18707 {
18708   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18709   tree c;
18710
18711   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18712     return list;
18713   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18714     {
18715       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18716       const char *p = IDENTIFIER_POINTER (id);
18717
18718       switch (p[0])
18719         {
18720         case 'n':
18721           if (strcmp ("none", p) != 0)
18722             goto invalid_kind;
18723           kind = OMP_CLAUSE_DEFAULT_NONE;
18724           break;
18725
18726         case 's':
18727           if (strcmp ("shared", p) != 0)
18728             goto invalid_kind;
18729           kind = OMP_CLAUSE_DEFAULT_SHARED;
18730           break;
18731
18732         default:
18733           goto invalid_kind;
18734         }
18735
18736       cp_lexer_consume_token (parser->lexer);
18737     }
18738   else
18739     {
18740     invalid_kind:
18741       cp_parser_error (parser, "expected %<none%> or %<shared%>");
18742     }
18743
18744   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18745     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18746                                            /*or_comma=*/false,
18747                                            /*consume_paren=*/true);
18748
18749   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18750     return list;
18751
18752   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18753   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18754   OMP_CLAUSE_CHAIN (c) = list;
18755   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18756
18757   return c;
18758 }
18759
18760 /* OpenMP 2.5:
18761    if ( expression ) */
18762
18763 static tree
18764 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18765 {
18766   tree t, c;
18767
18768   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18769     return list;
18770
18771   t = cp_parser_condition (parser);
18772
18773   if (t == error_mark_node
18774       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18775     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18776                                            /*or_comma=*/false,
18777                                            /*consume_paren=*/true);
18778
18779   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18780
18781   c = build_omp_clause (OMP_CLAUSE_IF);
18782   OMP_CLAUSE_IF_EXPR (c) = t;
18783   OMP_CLAUSE_CHAIN (c) = list;
18784
18785   return c;
18786 }
18787
18788 /* OpenMP 2.5:
18789    nowait */
18790
18791 static tree
18792 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18793 {
18794   tree c;
18795
18796   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18797
18798   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18799   OMP_CLAUSE_CHAIN (c) = list;
18800   return c;
18801 }
18802
18803 /* OpenMP 2.5:
18804    num_threads ( expression ) */
18805
18806 static tree
18807 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18808 {
18809   tree t, c;
18810
18811   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18812     return list;
18813
18814   t = cp_parser_expression (parser, false);
18815
18816   if (t == error_mark_node
18817       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18818     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18819                                            /*or_comma=*/false,
18820                                            /*consume_paren=*/true);
18821
18822   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18823
18824   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18825   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18826   OMP_CLAUSE_CHAIN (c) = list;
18827
18828   return c;
18829 }
18830
18831 /* OpenMP 2.5:
18832    ordered */
18833
18834 static tree
18835 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18836 {
18837   tree c;
18838
18839   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18840
18841   c = build_omp_clause (OMP_CLAUSE_ORDERED);
18842   OMP_CLAUSE_CHAIN (c) = list;
18843   return c;
18844 }
18845
18846 /* OpenMP 2.5:
18847    reduction ( reduction-operator : variable-list )
18848
18849    reduction-operator:
18850      One of: + * - & ^ | && || */
18851
18852 static tree
18853 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18854 {
18855   enum tree_code code;
18856   tree nlist, c;
18857
18858   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18859     return list;
18860
18861   switch (cp_lexer_peek_token (parser->lexer)->type)
18862     {
18863     case CPP_PLUS:
18864       code = PLUS_EXPR;
18865       break;
18866     case CPP_MULT:
18867       code = MULT_EXPR;
18868       break;
18869     case CPP_MINUS:
18870       code = MINUS_EXPR;
18871       break;
18872     case CPP_AND:
18873       code = BIT_AND_EXPR;
18874       break;
18875     case CPP_XOR:
18876       code = BIT_XOR_EXPR;
18877       break;
18878     case CPP_OR:
18879       code = BIT_IOR_EXPR;
18880       break;
18881     case CPP_AND_AND:
18882       code = TRUTH_ANDIF_EXPR;
18883       break;
18884     case CPP_OR_OR:
18885       code = TRUTH_ORIF_EXPR;
18886       break;
18887     default:
18888       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18889     resync_fail:
18890       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18891                                              /*or_comma=*/false,
18892                                              /*consume_paren=*/true);
18893       return list;
18894     }
18895   cp_lexer_consume_token (parser->lexer);
18896
18897   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18898     goto resync_fail;
18899
18900   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18901   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18902     OMP_CLAUSE_REDUCTION_CODE (c) = code;
18903
18904   return nlist;
18905 }
18906
18907 /* OpenMP 2.5:
18908    schedule ( schedule-kind )
18909    schedule ( schedule-kind , expression )
18910
18911    schedule-kind:
18912      static | dynamic | guided | runtime  */
18913
18914 static tree
18915 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18916 {
18917   tree c, t;
18918
18919   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18920     return list;
18921
18922   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18923
18924   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18925     {
18926       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18927       const char *p = IDENTIFIER_POINTER (id);
18928
18929       switch (p[0])
18930         {
18931         case 'd':
18932           if (strcmp ("dynamic", p) != 0)
18933             goto invalid_kind;
18934           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18935           break;
18936
18937         case 'g':
18938           if (strcmp ("guided", p) != 0)
18939             goto invalid_kind;
18940           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18941           break;
18942
18943         case 'r':
18944           if (strcmp ("runtime", p) != 0)
18945             goto invalid_kind;
18946           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18947           break;
18948
18949         default:
18950           goto invalid_kind;
18951         }
18952     }
18953   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18954     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18955   else
18956     goto invalid_kind;
18957   cp_lexer_consume_token (parser->lexer);
18958
18959   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18960     {
18961       cp_lexer_consume_token (parser->lexer);
18962
18963       t = cp_parser_assignment_expression (parser, false);
18964
18965       if (t == error_mark_node)
18966         goto resync_fail;
18967       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18968         error ("schedule %<runtime%> does not take "
18969                "a %<chunk_size%> parameter");
18970       else
18971         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18972
18973       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18974         goto resync_fail;
18975     }
18976   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18977     goto resync_fail;
18978
18979   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18980   OMP_CLAUSE_CHAIN (c) = list;
18981   return c;
18982
18983  invalid_kind:
18984   cp_parser_error (parser, "invalid schedule kind");
18985  resync_fail:
18986   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18987                                          /*or_comma=*/false,
18988                                          /*consume_paren=*/true);
18989   return list;
18990 }
18991
18992 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
18993    is a bitmask in MASK.  Return the list of clauses found; the result
18994    of clause default goes in *pdefault.  */
18995
18996 static tree
18997 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18998                            const char *where, cp_token *pragma_tok)
18999 {
19000   tree clauses = NULL;
19001
19002   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19003     {
19004       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19005       const char *c_name;
19006       tree prev = clauses;
19007
19008       switch (c_kind)
19009         {
19010         case PRAGMA_OMP_CLAUSE_COPYIN:
19011           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19012           c_name = "copyin";
19013           break;
19014         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19015           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19016                                             clauses);
19017           c_name = "copyprivate";
19018           break;
19019         case PRAGMA_OMP_CLAUSE_DEFAULT:
19020           clauses = cp_parser_omp_clause_default (parser, clauses);
19021           c_name = "default";
19022           break;
19023         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19024           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19025                                             clauses);
19026           c_name = "firstprivate";
19027           break;
19028         case PRAGMA_OMP_CLAUSE_IF:
19029           clauses = cp_parser_omp_clause_if (parser, clauses);
19030           c_name = "if";
19031           break;
19032         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19033           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19034                                             clauses);
19035           c_name = "lastprivate";
19036           break;
19037         case PRAGMA_OMP_CLAUSE_NOWAIT:
19038           clauses = cp_parser_omp_clause_nowait (parser, clauses);
19039           c_name = "nowait";
19040           break;
19041         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19042           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19043           c_name = "num_threads";
19044           break;
19045         case PRAGMA_OMP_CLAUSE_ORDERED:
19046           clauses = cp_parser_omp_clause_ordered (parser, clauses);
19047           c_name = "ordered";
19048           break;
19049         case PRAGMA_OMP_CLAUSE_PRIVATE:
19050           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19051                                             clauses);
19052           c_name = "private";
19053           break;
19054         case PRAGMA_OMP_CLAUSE_REDUCTION:
19055           clauses = cp_parser_omp_clause_reduction (parser, clauses);
19056           c_name = "reduction";
19057           break;
19058         case PRAGMA_OMP_CLAUSE_SCHEDULE:
19059           clauses = cp_parser_omp_clause_schedule (parser, clauses);
19060           c_name = "schedule";
19061           break;
19062         case PRAGMA_OMP_CLAUSE_SHARED:
19063           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19064                                             clauses);
19065           c_name = "shared";
19066           break;
19067         default:
19068           cp_parser_error (parser, "expected %<#pragma omp%> clause");
19069           goto saw_error;
19070         }
19071
19072       if (((mask >> c_kind) & 1) == 0)
19073         {
19074           /* Remove the invalid clause(s) from the list to avoid
19075              confusing the rest of the compiler.  */
19076           clauses = prev;
19077           error ("%qs is not valid for %qs", c_name, where);
19078         }
19079     }
19080  saw_error:
19081   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19082   return finish_omp_clauses (clauses);
19083 }
19084
19085 /* OpenMP 2.5:
19086    structured-block:
19087      statement
19088
19089    In practice, we're also interested in adding the statement to an
19090    outer node.  So it is convenient if we work around the fact that
19091    cp_parser_statement calls add_stmt.  */
19092
19093 static unsigned
19094 cp_parser_begin_omp_structured_block (cp_parser *parser)
19095 {
19096   unsigned save = parser->in_statement;
19097
19098   /* Only move the values to IN_OMP_BLOCK if they weren't false.
19099      This preserves the "not within loop or switch" style error messages
19100      for nonsense cases like
19101         void foo() {
19102         #pragma omp single
19103           break;
19104         }
19105   */
19106   if (parser->in_statement)
19107     parser->in_statement = IN_OMP_BLOCK;
19108
19109   return save;
19110 }
19111
19112 static void
19113 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19114 {
19115   parser->in_statement = save;
19116 }
19117
19118 static tree
19119 cp_parser_omp_structured_block (cp_parser *parser)
19120 {
19121   tree stmt = begin_omp_structured_block ();
19122   unsigned int save = cp_parser_begin_omp_structured_block (parser);
19123
19124   cp_parser_statement (parser, NULL_TREE, false, NULL);
19125
19126   cp_parser_end_omp_structured_block (parser, save);
19127   return finish_omp_structured_block (stmt);
19128 }
19129
19130 /* OpenMP 2.5:
19131    # pragma omp atomic new-line
19132      expression-stmt
19133
19134    expression-stmt:
19135      x binop= expr | x++ | ++x | x-- | --x
19136    binop:
19137      +, *, -, /, &, ^, |, <<, >>
19138
19139   where x is an lvalue expression with scalar type.  */
19140
19141 static void
19142 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19143 {
19144   tree lhs, rhs;
19145   enum tree_code code;
19146
19147   cp_parser_require_pragma_eol (parser, pragma_tok);
19148
19149   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19150                                     /*cast_p=*/false);
19151   switch (TREE_CODE (lhs))
19152     {
19153     case ERROR_MARK:
19154       goto saw_error;
19155
19156     case PREINCREMENT_EXPR:
19157     case POSTINCREMENT_EXPR:
19158       lhs = TREE_OPERAND (lhs, 0);
19159       code = PLUS_EXPR;
19160       rhs = integer_one_node;
19161       break;
19162
19163     case PREDECREMENT_EXPR:
19164     case POSTDECREMENT_EXPR:
19165       lhs = TREE_OPERAND (lhs, 0);
19166       code = MINUS_EXPR;
19167       rhs = integer_one_node;
19168       break;
19169
19170     default:
19171       switch (cp_lexer_peek_token (parser->lexer)->type)
19172         {
19173         case CPP_MULT_EQ:
19174           code = MULT_EXPR;
19175           break;
19176         case CPP_DIV_EQ:
19177           code = TRUNC_DIV_EXPR;
19178           break;
19179         case CPP_PLUS_EQ:
19180           code = PLUS_EXPR;
19181           break;
19182         case CPP_MINUS_EQ:
19183           code = MINUS_EXPR;
19184           break;
19185         case CPP_LSHIFT_EQ:
19186           code = LSHIFT_EXPR;
19187           break;
19188         case CPP_RSHIFT_EQ:
19189           code = RSHIFT_EXPR;
19190           break;
19191         case CPP_AND_EQ:
19192           code = BIT_AND_EXPR;
19193           break;
19194         case CPP_OR_EQ:
19195           code = BIT_IOR_EXPR;
19196           break;
19197         case CPP_XOR_EQ:
19198           code = BIT_XOR_EXPR;
19199           break;
19200         default:
19201           cp_parser_error (parser,
19202                            "invalid operator for %<#pragma omp atomic%>");
19203           goto saw_error;
19204         }
19205       cp_lexer_consume_token (parser->lexer);
19206
19207       rhs = cp_parser_expression (parser, false);
19208       if (rhs == error_mark_node)
19209         goto saw_error;
19210       break;
19211     }
19212   finish_omp_atomic (code, lhs, rhs);
19213   cp_parser_consume_semicolon_at_end_of_statement (parser);
19214   return;
19215
19216  saw_error:
19217   cp_parser_skip_to_end_of_block_or_statement (parser);
19218 }
19219
19220
19221 /* OpenMP 2.5:
19222    # pragma omp barrier new-line  */
19223
19224 static void
19225 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19226 {
19227   cp_parser_require_pragma_eol (parser, pragma_tok);
19228   finish_omp_barrier ();
19229 }
19230
19231 /* OpenMP 2.5:
19232    # pragma omp critical [(name)] new-line
19233      structured-block  */
19234
19235 static tree
19236 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19237 {
19238   tree stmt, name = NULL;
19239
19240   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19241     {
19242       cp_lexer_consume_token (parser->lexer);
19243
19244       name = cp_parser_identifier (parser);
19245
19246       if (name == error_mark_node
19247           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19248         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19249                                                /*or_comma=*/false,
19250                                                /*consume_paren=*/true);
19251       if (name == error_mark_node)
19252         name = NULL;
19253     }
19254   cp_parser_require_pragma_eol (parser, pragma_tok);
19255
19256   stmt = cp_parser_omp_structured_block (parser);
19257   return c_finish_omp_critical (stmt, name);
19258 }
19259
19260 /* OpenMP 2.5:
19261    # pragma omp flush flush-vars[opt] new-line
19262
19263    flush-vars:
19264      ( variable-list ) */
19265
19266 static void
19267 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19268 {
19269   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19270     (void) cp_parser_omp_var_list (parser, 0, NULL);
19271   cp_parser_require_pragma_eol (parser, pragma_tok);
19272
19273   finish_omp_flush ();
19274 }
19275
19276 /* Parse the restricted form of the for statment allowed by OpenMP.  */
19277
19278 static tree
19279 cp_parser_omp_for_loop (cp_parser *parser)
19280 {
19281   tree init, cond, incr, body, decl, pre_body;
19282   location_t loc;
19283
19284   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19285     {
19286       cp_parser_error (parser, "for statement expected");
19287       return NULL;
19288     }
19289   loc = cp_lexer_consume_token (parser->lexer)->location;
19290   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19291     return NULL;
19292
19293   init = decl = NULL;
19294   pre_body = push_stmt_list ();
19295   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19296     {
19297       cp_decl_specifier_seq type_specifiers;
19298
19299       /* First, try to parse as an initialized declaration.  See
19300          cp_parser_condition, from whence the bulk of this is copied.  */
19301
19302       cp_parser_parse_tentatively (parser);
19303       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19304                                     &type_specifiers);
19305       if (!cp_parser_error_occurred (parser))
19306         {
19307           tree asm_specification, attributes;
19308           cp_declarator *declarator;
19309
19310           declarator = cp_parser_declarator (parser,
19311                                              CP_PARSER_DECLARATOR_NAMED,
19312                                              /*ctor_dtor_or_conv_p=*/NULL,
19313                                              /*parenthesized_p=*/NULL,
19314                                              /*member_p=*/false);
19315           attributes = cp_parser_attributes_opt (parser);
19316           asm_specification = cp_parser_asm_specification_opt (parser);
19317
19318           cp_parser_require (parser, CPP_EQ, "`='");
19319           if (cp_parser_parse_definitely (parser))
19320             {
19321               tree pushed_scope;
19322
19323               decl = start_decl (declarator, &type_specifiers,
19324                                  /*initialized_p=*/false, attributes,
19325                                  /*prefix_attributes=*/NULL_TREE,
19326                                  &pushed_scope);
19327
19328               init = cp_parser_assignment_expression (parser, false);
19329
19330               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19331                               asm_specification, LOOKUP_ONLYCONVERTING);
19332
19333               if (pushed_scope)
19334                 pop_scope (pushed_scope);
19335             }
19336         }
19337       else
19338         cp_parser_abort_tentative_parse (parser);
19339
19340       /* If parsing as an initialized declaration failed, try again as
19341          a simple expression.  */
19342       if (decl == NULL)
19343         init = cp_parser_expression (parser, false);
19344     }
19345   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19346   pre_body = pop_stmt_list (pre_body);
19347
19348   cond = NULL;
19349   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19350     cond = cp_parser_condition (parser);
19351   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19352
19353   incr = NULL;
19354   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19355     incr = cp_parser_expression (parser, false);
19356
19357   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19358     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19359                                            /*or_comma=*/false,
19360                                            /*consume_paren=*/true);
19361
19362   /* Note that we saved the original contents of this flag when we entered
19363      the structured block, and so we don't need to re-save it here.  */
19364   parser->in_statement = IN_OMP_FOR;
19365
19366   /* Note that the grammar doesn't call for a structured block here,
19367      though the loop as a whole is a structured block.  */
19368   body = push_stmt_list ();
19369   cp_parser_statement (parser, NULL_TREE, false, NULL);
19370   body = pop_stmt_list (body);
19371
19372   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19373 }
19374
19375 /* OpenMP 2.5:
19376    #pragma omp for for-clause[optseq] new-line
19377      for-loop  */
19378
19379 #define OMP_FOR_CLAUSE_MASK                             \
19380         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19381         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19382         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19383         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19384         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
19385         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
19386         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19387
19388 static tree
19389 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19390 {
19391   tree clauses, sb, ret;
19392   unsigned int save;
19393
19394   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19395                                        "#pragma omp for", pragma_tok);
19396
19397   sb = begin_omp_structured_block ();
19398   save = cp_parser_begin_omp_structured_block (parser);
19399
19400   ret = cp_parser_omp_for_loop (parser);
19401   if (ret)
19402     OMP_FOR_CLAUSES (ret) = clauses;
19403
19404   cp_parser_end_omp_structured_block (parser, save);
19405   add_stmt (finish_omp_structured_block (sb));
19406
19407   return ret;
19408 }
19409
19410 /* OpenMP 2.5:
19411    # pragma omp master new-line
19412      structured-block  */
19413
19414 static tree
19415 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19416 {
19417   cp_parser_require_pragma_eol (parser, pragma_tok);
19418   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19419 }
19420
19421 /* OpenMP 2.5:
19422    # pragma omp ordered new-line
19423      structured-block  */
19424
19425 static tree
19426 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19427 {
19428   cp_parser_require_pragma_eol (parser, pragma_tok);
19429   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19430 }
19431
19432 /* OpenMP 2.5:
19433
19434    section-scope:
19435      { section-sequence }
19436
19437    section-sequence:
19438      section-directive[opt] structured-block
19439      section-sequence section-directive structured-block  */
19440
19441 static tree
19442 cp_parser_omp_sections_scope (cp_parser *parser)
19443 {
19444   tree stmt, substmt;
19445   bool error_suppress = false;
19446   cp_token *tok;
19447
19448   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19449     return NULL_TREE;
19450
19451   stmt = push_stmt_list ();
19452
19453   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19454     {
19455       unsigned save;
19456
19457       substmt = begin_omp_structured_block ();
19458       save = cp_parser_begin_omp_structured_block (parser);
19459
19460       while (1)
19461         {
19462           cp_parser_statement (parser, NULL_TREE, false, NULL);
19463
19464           tok = cp_lexer_peek_token (parser->lexer);
19465           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19466             break;
19467           if (tok->type == CPP_CLOSE_BRACE)
19468             break;
19469           if (tok->type == CPP_EOF)
19470             break;
19471         }
19472
19473       cp_parser_end_omp_structured_block (parser, save);
19474       substmt = finish_omp_structured_block (substmt);
19475       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19476       add_stmt (substmt);
19477     }
19478
19479   while (1)
19480     {
19481       tok = cp_lexer_peek_token (parser->lexer);
19482       if (tok->type == CPP_CLOSE_BRACE)
19483         break;
19484       if (tok->type == CPP_EOF)
19485         break;
19486
19487       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19488         {
19489           cp_lexer_consume_token (parser->lexer);
19490           cp_parser_require_pragma_eol (parser, tok);
19491           error_suppress = false;
19492         }
19493       else if (!error_suppress)
19494         {
19495           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19496           error_suppress = true;
19497         }
19498
19499       substmt = cp_parser_omp_structured_block (parser);
19500       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19501       add_stmt (substmt);
19502     }
19503   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19504
19505   substmt = pop_stmt_list (stmt);
19506
19507   stmt = make_node (OMP_SECTIONS);
19508   TREE_TYPE (stmt) = void_type_node;
19509   OMP_SECTIONS_BODY (stmt) = substmt;
19510
19511   add_stmt (stmt);
19512   return stmt;
19513 }
19514
19515 /* OpenMP 2.5:
19516    # pragma omp sections sections-clause[optseq] newline
19517      sections-scope  */
19518
19519 #define OMP_SECTIONS_CLAUSE_MASK                        \
19520         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19521         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19522         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19523         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19524         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19525
19526 static tree
19527 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19528 {
19529   tree clauses, ret;
19530
19531   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19532                                        "#pragma omp sections", pragma_tok);
19533
19534   ret = cp_parser_omp_sections_scope (parser);
19535   if (ret)
19536     OMP_SECTIONS_CLAUSES (ret) = clauses;
19537
19538   return ret;
19539 }
19540
19541 /* OpenMP 2.5:
19542    # pragma parallel parallel-clause new-line
19543    # pragma parallel for parallel-for-clause new-line
19544    # pragma parallel sections parallel-sections-clause new-line  */
19545
19546 #define OMP_PARALLEL_CLAUSE_MASK                        \
19547         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
19548         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19549         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19550         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
19551         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
19552         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
19553         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19554         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19555
19556 static tree
19557 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19558 {
19559   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19560   const char *p_name = "#pragma omp parallel";
19561   tree stmt, clauses, par_clause, ws_clause, block;
19562   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19563   unsigned int save;
19564
19565   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19566     {
19567       cp_lexer_consume_token (parser->lexer);
19568       p_kind = PRAGMA_OMP_PARALLEL_FOR;
19569       p_name = "#pragma omp parallel for";
19570       mask |= OMP_FOR_CLAUSE_MASK;
19571       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19572     }
19573   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19574     {
19575       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19576       const char *p = IDENTIFIER_POINTER (id);
19577       if (strcmp (p, "sections") == 0)
19578         {
19579           cp_lexer_consume_token (parser->lexer);
19580           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19581           p_name = "#pragma omp parallel sections";
19582           mask |= OMP_SECTIONS_CLAUSE_MASK;
19583           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19584         }
19585     }
19586
19587   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19588   block = begin_omp_parallel ();
19589   save = cp_parser_begin_omp_structured_block (parser);
19590
19591   switch (p_kind)
19592     {
19593     case PRAGMA_OMP_PARALLEL:
19594       cp_parser_already_scoped_statement (parser);
19595       par_clause = clauses;
19596       break;
19597
19598     case PRAGMA_OMP_PARALLEL_FOR:
19599       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19600       stmt = cp_parser_omp_for_loop (parser);
19601       if (stmt)
19602         OMP_FOR_CLAUSES (stmt) = ws_clause;
19603       break;
19604
19605     case PRAGMA_OMP_PARALLEL_SECTIONS:
19606       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19607       stmt = cp_parser_omp_sections_scope (parser);
19608       if (stmt)
19609         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19610       break;
19611
19612     default:
19613       gcc_unreachable ();
19614     }
19615
19616   cp_parser_end_omp_structured_block (parser, save);
19617   stmt = finish_omp_parallel (par_clause, block);
19618   if (p_kind != PRAGMA_OMP_PARALLEL)
19619     OMP_PARALLEL_COMBINED (stmt) = 1;
19620   return stmt;
19621 }
19622
19623 /* OpenMP 2.5:
19624    # pragma omp single single-clause[optseq] new-line
19625      structured-block  */
19626
19627 #define OMP_SINGLE_CLAUSE_MASK                          \
19628         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19629         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19630         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
19631         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19632
19633 static tree
19634 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19635 {
19636   tree stmt = make_node (OMP_SINGLE);
19637   TREE_TYPE (stmt) = void_type_node;
19638
19639   OMP_SINGLE_CLAUSES (stmt)
19640     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19641                                  "#pragma omp single", pragma_tok);
19642   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19643
19644   return add_stmt (stmt);
19645 }
19646
19647 /* OpenMP 2.5:
19648    # pragma omp threadprivate (variable-list) */
19649
19650 static void
19651 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19652 {
19653   tree vars;
19654
19655   vars = cp_parser_omp_var_list (parser, 0, NULL);
19656   cp_parser_require_pragma_eol (parser, pragma_tok);
19657
19658   finish_omp_threadprivate (vars);
19659 }
19660
19661 /* Main entry point to OpenMP statement pragmas.  */
19662
19663 static void
19664 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19665 {
19666   tree stmt;
19667
19668   switch (pragma_tok->pragma_kind)
19669     {
19670     case PRAGMA_OMP_ATOMIC:
19671       cp_parser_omp_atomic (parser, pragma_tok);
19672       return;
19673     case PRAGMA_OMP_CRITICAL:
19674       stmt = cp_parser_omp_critical (parser, pragma_tok);
19675       break;
19676     case PRAGMA_OMP_FOR:
19677       stmt = cp_parser_omp_for (parser, pragma_tok);
19678       break;
19679     case PRAGMA_OMP_MASTER:
19680       stmt = cp_parser_omp_master (parser, pragma_tok);
19681       break;
19682     case PRAGMA_OMP_ORDERED:
19683       stmt = cp_parser_omp_ordered (parser, pragma_tok);
19684       break;
19685     case PRAGMA_OMP_PARALLEL:
19686       stmt = cp_parser_omp_parallel (parser, pragma_tok);
19687       break;
19688     case PRAGMA_OMP_SECTIONS:
19689       stmt = cp_parser_omp_sections (parser, pragma_tok);
19690       break;
19691     case PRAGMA_OMP_SINGLE:
19692       stmt = cp_parser_omp_single (parser, pragma_tok);
19693       break;
19694     default:
19695       gcc_unreachable ();
19696     }
19697
19698   if (stmt)
19699     SET_EXPR_LOCATION (stmt, pragma_tok->location);
19700 }
19701 \f
19702 /* The parser.  */
19703
19704 static GTY (()) cp_parser *the_parser;
19705
19706 \f
19707 /* Special handling for the first token or line in the file.  The first
19708    thing in the file might be #pragma GCC pch_preprocess, which loads a
19709    PCH file, which is a GC collection point.  So we need to handle this
19710    first pragma without benefit of an existing lexer structure.
19711
19712    Always returns one token to the caller in *FIRST_TOKEN.  This is
19713    either the true first token of the file, or the first token after
19714    the initial pragma.  */
19715
19716 static void
19717 cp_parser_initial_pragma (cp_token *first_token)
19718 {
19719   tree name = NULL;
19720
19721   cp_lexer_get_preprocessor_token (NULL, first_token);
19722   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19723     return;
19724
19725   cp_lexer_get_preprocessor_token (NULL, first_token);
19726   if (first_token->type == CPP_STRING)
19727     {
19728       name = first_token->u.value;
19729
19730       cp_lexer_get_preprocessor_token (NULL, first_token);
19731       if (first_token->type != CPP_PRAGMA_EOL)
19732         error ("junk at end of %<#pragma GCC pch_preprocess%>");
19733     }
19734   else
19735     error ("expected string literal");
19736
19737   /* Skip to the end of the pragma.  */
19738   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19739     cp_lexer_get_preprocessor_token (NULL, first_token);
19740
19741   /* Now actually load the PCH file.  */
19742   if (name)
19743     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19744
19745   /* Read one more token to return to our caller.  We have to do this
19746      after reading the PCH file in, since its pointers have to be
19747      live.  */
19748   cp_lexer_get_preprocessor_token (NULL, first_token);
19749 }
19750
19751 /* Normal parsing of a pragma token.  Here we can (and must) use the
19752    regular lexer.  */
19753
19754 static bool
19755 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19756 {
19757   cp_token *pragma_tok;
19758   unsigned int id;
19759
19760   pragma_tok = cp_lexer_consume_token (parser->lexer);
19761   gcc_assert (pragma_tok->type == CPP_PRAGMA);
19762   parser->lexer->in_pragma = true;
19763
19764   id = pragma_tok->pragma_kind;
19765   switch (id)
19766     {
19767     case PRAGMA_GCC_PCH_PREPROCESS:
19768       error ("%<#pragma GCC pch_preprocess%> must be first");
19769       break;
19770
19771     case PRAGMA_OMP_BARRIER:
19772       switch (context)
19773         {
19774         case pragma_compound:
19775           cp_parser_omp_barrier (parser, pragma_tok);
19776           return false;
19777         case pragma_stmt:
19778           error ("%<#pragma omp barrier%> may only be "
19779                  "used in compound statements");
19780           break;
19781         default:
19782           goto bad_stmt;
19783         }
19784       break;
19785
19786     case PRAGMA_OMP_FLUSH:
19787       switch (context)
19788         {
19789         case pragma_compound:
19790           cp_parser_omp_flush (parser, pragma_tok);
19791           return false;
19792         case pragma_stmt:
19793           error ("%<#pragma omp flush%> may only be "
19794                  "used in compound statements");
19795           break;
19796         default:
19797           goto bad_stmt;
19798         }
19799       break;
19800
19801     case PRAGMA_OMP_THREADPRIVATE:
19802       cp_parser_omp_threadprivate (parser, pragma_tok);
19803       return false;
19804
19805     case PRAGMA_OMP_ATOMIC:
19806     case PRAGMA_OMP_CRITICAL:
19807     case PRAGMA_OMP_FOR:
19808     case PRAGMA_OMP_MASTER:
19809     case PRAGMA_OMP_ORDERED:
19810     case PRAGMA_OMP_PARALLEL:
19811     case PRAGMA_OMP_SECTIONS:
19812     case PRAGMA_OMP_SINGLE:
19813       if (context == pragma_external)
19814         goto bad_stmt;
19815       cp_parser_omp_construct (parser, pragma_tok);
19816       return true;
19817
19818     case PRAGMA_OMP_SECTION:
19819       error ("%<#pragma omp section%> may only be used in "
19820              "%<#pragma omp sections%> construct");
19821       break;
19822
19823     default:
19824       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19825       c_invoke_pragma_handler (id);
19826       break;
19827
19828     bad_stmt:
19829       cp_parser_error (parser, "expected declaration specifiers");
19830       break;
19831     }
19832
19833   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19834   return false;
19835 }
19836
19837 /* The interface the pragma parsers have to the lexer.  */
19838
19839 enum cpp_ttype
19840 pragma_lex (tree *value)
19841 {
19842   cp_token *tok;
19843   enum cpp_ttype ret;
19844
19845   tok = cp_lexer_peek_token (the_parser->lexer);
19846
19847   ret = tok->type;
19848   *value = tok->u.value;
19849
19850   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19851     ret = CPP_EOF;
19852   else if (ret == CPP_STRING)
19853     *value = cp_parser_string_literal (the_parser, false, false);
19854   else
19855     {
19856       cp_lexer_consume_token (the_parser->lexer);
19857       if (ret == CPP_KEYWORD)
19858         ret = CPP_NAME;
19859     }
19860
19861   return ret;
19862 }
19863
19864 \f
19865 /* External interface.  */
19866
19867 /* Parse one entire translation unit.  */
19868
19869 void
19870 c_parse_file (void)
19871 {
19872   bool error_occurred;
19873   static bool already_called = false;
19874
19875   if (already_called)
19876     {
19877       sorry ("inter-module optimizations not implemented for C++");
19878       return;
19879     }
19880   already_called = true;
19881
19882   the_parser = cp_parser_new ();
19883   push_deferring_access_checks (flag_access_control
19884                                 ? dk_no_deferred : dk_no_check);
19885   error_occurred = cp_parser_translation_unit (the_parser);
19886   the_parser = NULL;
19887 }
19888
19889 #include "gt-cp-parser.h"