OSDN Git Service

2007-03-28 Douglas Gregor <doug.gregor@gmail.com>
[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 ("%<%E::%E%> has not been declared",
2058                parser->scope, name);
2059       else if (parser->scope == global_namespace)
2060         error ("%<::%E%> has not been declared", name);
2061       else if (parser->object_scope
2062                && !CLASS_TYPE_P (parser->object_scope))
2063         error ("request for member %qE in non-class type %qT",
2064                name, parser->object_scope);
2065       else if (parser->object_scope)
2066         error ("%<%T::%E%> has not been declared",
2067                parser->object_scope, name);
2068       else
2069         error ("%qE has not been declared", name);
2070     }
2071   else if (parser->scope && parser->scope != global_namespace)
2072     error ("%<%E::%E%> %s", parser->scope, name, desired);
2073   else if (parser->scope == global_namespace)
2074     error ("%<::%E%> %s", name, desired);
2075   else
2076     error ("%qE %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       || TREE_CODE (id) == TYPE_DECL)
2348     {
2349       cp_parser_abort_tentative_parse (parser);
2350       return false;
2351     }
2352   if (!cp_parser_parse_definitely (parser))
2353     return false;
2354
2355   /* Emit a diagnostic for the invalid type.  */
2356   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2357   /* Skip to the end of the declaration; there's no point in
2358      trying to process it.  */
2359   cp_parser_skip_to_end_of_block_or_statement (parser);
2360   return true;
2361 }
2362
2363 /* Consume tokens up to, and including, the next non-nested closing `)'.
2364    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2365    are doing error recovery. Returns -1 if OR_COMMA is true and we
2366    found an unnested comma.  */
2367
2368 static int
2369 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2370                                        bool recovering,
2371                                        bool or_comma,
2372                                        bool consume_paren)
2373 {
2374   unsigned paren_depth = 0;
2375   unsigned brace_depth = 0;
2376
2377   if (recovering && !or_comma
2378       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2379     return 0;
2380
2381   while (true)
2382     {
2383       cp_token * token = cp_lexer_peek_token (parser->lexer);
2384
2385       switch (token->type)
2386         {
2387         case CPP_EOF:
2388         case CPP_PRAGMA_EOL:
2389           /* If we've run out of tokens, then there is no closing `)'.  */
2390           return 0;
2391
2392         case CPP_SEMICOLON:
2393           /* This matches the processing in skip_to_end_of_statement.  */
2394           if (!brace_depth)
2395             return 0;
2396           break;
2397
2398         case CPP_OPEN_BRACE:
2399           ++brace_depth;
2400           break;
2401         case CPP_CLOSE_BRACE:
2402           if (!brace_depth--)
2403             return 0;
2404           break;
2405
2406         case CPP_COMMA:
2407           if (recovering && or_comma && !brace_depth && !paren_depth)
2408             return -1;
2409           break;
2410
2411         case CPP_OPEN_PAREN:
2412           if (!brace_depth)
2413             ++paren_depth;
2414           break;
2415
2416         case CPP_CLOSE_PAREN:
2417           if (!brace_depth && !paren_depth--)
2418             {
2419               if (consume_paren)
2420                 cp_lexer_consume_token (parser->lexer);
2421               return 1;
2422             }
2423           break;
2424
2425         default:
2426           break;
2427         }
2428
2429       /* Consume the token.  */
2430       cp_lexer_consume_token (parser->lexer);
2431     }
2432 }
2433
2434 /* Consume tokens until we reach the end of the current statement.
2435    Normally, that will be just before consuming a `;'.  However, if a
2436    non-nested `}' comes first, then we stop before consuming that.  */
2437
2438 static void
2439 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2440 {
2441   unsigned nesting_depth = 0;
2442
2443   while (true)
2444     {
2445       cp_token *token = cp_lexer_peek_token (parser->lexer);
2446
2447       switch (token->type)
2448         {
2449         case CPP_EOF:
2450         case CPP_PRAGMA_EOL:
2451           /* If we've run out of tokens, stop.  */
2452           return;
2453
2454         case CPP_SEMICOLON:
2455           /* If the next token is a `;', we have reached the end of the
2456              statement.  */
2457           if (!nesting_depth)
2458             return;
2459           break;
2460
2461         case CPP_CLOSE_BRACE:
2462           /* If this is a non-nested '}', stop before consuming it.
2463              That way, when confronted with something like:
2464
2465                { 3 + }
2466
2467              we stop before consuming the closing '}', even though we
2468              have not yet reached a `;'.  */
2469           if (nesting_depth == 0)
2470             return;
2471
2472           /* If it is the closing '}' for a block that we have
2473              scanned, stop -- but only after consuming the token.
2474              That way given:
2475
2476                 void f g () { ... }
2477                 typedef int I;
2478
2479              we will stop after the body of the erroneously declared
2480              function, but before consuming the following `typedef'
2481              declaration.  */
2482           if (--nesting_depth == 0)
2483             {
2484               cp_lexer_consume_token (parser->lexer);
2485               return;
2486             }
2487
2488         case CPP_OPEN_BRACE:
2489           ++nesting_depth;
2490           break;
2491
2492         default:
2493           break;
2494         }
2495
2496       /* Consume the token.  */
2497       cp_lexer_consume_token (parser->lexer);
2498     }
2499 }
2500
2501 /* This function is called at the end of a statement or declaration.
2502    If the next token is a semicolon, it is consumed; otherwise, error
2503    recovery is attempted.  */
2504
2505 static void
2506 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2507 {
2508   /* Look for the trailing `;'.  */
2509   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2510     {
2511       /* If there is additional (erroneous) input, skip to the end of
2512          the statement.  */
2513       cp_parser_skip_to_end_of_statement (parser);
2514       /* If the next token is now a `;', consume it.  */
2515       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2516         cp_lexer_consume_token (parser->lexer);
2517     }
2518 }
2519
2520 /* Skip tokens until we have consumed an entire block, or until we
2521    have consumed a non-nested `;'.  */
2522
2523 static void
2524 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2525 {
2526   int nesting_depth = 0;
2527
2528   while (nesting_depth >= 0)
2529     {
2530       cp_token *token = cp_lexer_peek_token (parser->lexer);
2531
2532       switch (token->type)
2533         {
2534         case CPP_EOF:
2535         case CPP_PRAGMA_EOL:
2536           /* If we've run out of tokens, stop.  */
2537           return;
2538
2539         case CPP_SEMICOLON:
2540           /* Stop if this is an unnested ';'. */
2541           if (!nesting_depth)
2542             nesting_depth = -1;
2543           break;
2544
2545         case CPP_CLOSE_BRACE:
2546           /* Stop if this is an unnested '}', or closes the outermost
2547              nesting level.  */
2548           nesting_depth--;
2549           if (!nesting_depth)
2550             nesting_depth = -1;
2551           break;
2552
2553         case CPP_OPEN_BRACE:
2554           /* Nest. */
2555           nesting_depth++;
2556           break;
2557
2558         default:
2559           break;
2560         }
2561
2562       /* Consume the token.  */
2563       cp_lexer_consume_token (parser->lexer);
2564     }
2565 }
2566
2567 /* Skip tokens until a non-nested closing curly brace is the next
2568    token.  */
2569
2570 static void
2571 cp_parser_skip_to_closing_brace (cp_parser *parser)
2572 {
2573   unsigned nesting_depth = 0;
2574
2575   while (true)
2576     {
2577       cp_token *token = cp_lexer_peek_token (parser->lexer);
2578
2579       switch (token->type)
2580         {
2581         case CPP_EOF:
2582         case CPP_PRAGMA_EOL:
2583           /* If we've run out of tokens, stop.  */
2584           return;
2585
2586         case CPP_CLOSE_BRACE:
2587           /* If the next token is a non-nested `}', then we have reached
2588              the end of the current block.  */
2589           if (nesting_depth-- == 0)
2590             return;
2591           break;
2592
2593         case CPP_OPEN_BRACE:
2594           /* If it the next token is a `{', then we are entering a new
2595              block.  Consume the entire block.  */
2596           ++nesting_depth;
2597           break;
2598
2599         default:
2600           break;
2601         }
2602
2603       /* Consume the token.  */
2604       cp_lexer_consume_token (parser->lexer);
2605     }
2606 }
2607
2608 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2609    parameter is the PRAGMA token, allowing us to purge the entire pragma
2610    sequence.  */
2611
2612 static void
2613 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2614 {
2615   cp_token *token;
2616
2617   parser->lexer->in_pragma = false;
2618
2619   do
2620     token = cp_lexer_consume_token (parser->lexer);
2621   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2622
2623   /* Ensure that the pragma is not parsed again.  */
2624   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2625 }
2626
2627 /* Require pragma end of line, resyncing with it as necessary.  The
2628    arguments are as for cp_parser_skip_to_pragma_eol.  */
2629
2630 static void
2631 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2632 {
2633   parser->lexer->in_pragma = false;
2634   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2635     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2636 }
2637
2638 /* This is a simple wrapper around make_typename_type. When the id is
2639    an unresolved identifier node, we can provide a superior diagnostic
2640    using cp_parser_diagnose_invalid_type_name.  */
2641
2642 static tree
2643 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2644 {
2645   tree result;
2646   if (TREE_CODE (id) == IDENTIFIER_NODE)
2647     {
2648       result = make_typename_type (scope, id, typename_type,
2649                                    /*complain=*/tf_none);
2650       if (result == error_mark_node)
2651         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2652       return result;
2653     }
2654   return make_typename_type (scope, id, typename_type, tf_error);
2655 }
2656
2657
2658 /* Create a new C++ parser.  */
2659
2660 static cp_parser *
2661 cp_parser_new (void)
2662 {
2663   cp_parser *parser;
2664   cp_lexer *lexer;
2665   unsigned i;
2666
2667   /* cp_lexer_new_main is called before calling ggc_alloc because
2668      cp_lexer_new_main might load a PCH file.  */
2669   lexer = cp_lexer_new_main ();
2670
2671   /* Initialize the binops_by_token so that we can get the tree
2672      directly from the token.  */
2673   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2674     binops_by_token[binops[i].token_type] = binops[i];
2675
2676   parser = GGC_CNEW (cp_parser);
2677   parser->lexer = lexer;
2678   parser->context = cp_parser_context_new (NULL);
2679
2680   /* For now, we always accept GNU extensions.  */
2681   parser->allow_gnu_extensions_p = 1;
2682
2683   /* The `>' token is a greater-than operator, not the end of a
2684      template-id.  */
2685   parser->greater_than_is_operator_p = true;
2686
2687   parser->default_arg_ok_p = true;
2688
2689   /* We are not parsing a constant-expression.  */
2690   parser->integral_constant_expression_p = false;
2691   parser->allow_non_integral_constant_expression_p = false;
2692   parser->non_integral_constant_expression_p = false;
2693
2694   /* Local variable names are not forbidden.  */
2695   parser->local_variables_forbidden_p = false;
2696
2697   /* We are not processing an `extern "C"' declaration.  */
2698   parser->in_unbraced_linkage_specification_p = false;
2699
2700   /* We are not processing a declarator.  */
2701   parser->in_declarator_p = false;
2702
2703   /* We are not processing a template-argument-list.  */
2704   parser->in_template_argument_list_p = false;
2705
2706   /* We are not in an iteration statement.  */
2707   parser->in_statement = 0;
2708
2709   /* We are not in a switch statement.  */
2710   parser->in_switch_statement_p = false;
2711
2712   /* We are not parsing a type-id inside an expression.  */
2713   parser->in_type_id_in_expr_p = false;
2714
2715   /* Declarations aren't implicitly extern "C".  */
2716   parser->implicit_extern_c = false;
2717
2718   /* String literals should be translated to the execution character set.  */
2719   parser->translate_strings_p = true;
2720
2721   /* We are not parsing a function body.  */
2722   parser->in_function_body = false;
2723
2724   /* The unparsed function queue is empty.  */
2725   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2726
2727   /* There are no classes being defined.  */
2728   parser->num_classes_being_defined = 0;
2729
2730   /* No template parameters apply.  */
2731   parser->num_template_parameter_lists = 0;
2732
2733   return parser;
2734 }
2735
2736 /* Create a cp_lexer structure which will emit the tokens in CACHE
2737    and push it onto the parser's lexer stack.  This is used for delayed
2738    parsing of in-class method bodies and default arguments, and should
2739    not be confused with tentative parsing.  */
2740 static void
2741 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2742 {
2743   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2744   lexer->next = parser->lexer;
2745   parser->lexer = lexer;
2746
2747   /* Move the current source position to that of the first token in the
2748      new lexer.  */
2749   cp_lexer_set_source_position_from_token (lexer->next_token);
2750 }
2751
2752 /* Pop the top lexer off the parser stack.  This is never used for the
2753    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2754 static void
2755 cp_parser_pop_lexer (cp_parser *parser)
2756 {
2757   cp_lexer *lexer = parser->lexer;
2758   parser->lexer = lexer->next;
2759   cp_lexer_destroy (lexer);
2760
2761   /* Put the current source position back where it was before this
2762      lexer was pushed.  */
2763   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2764 }
2765
2766 /* Lexical conventions [gram.lex]  */
2767
2768 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2769    identifier.  */
2770
2771 static tree
2772 cp_parser_identifier (cp_parser* parser)
2773 {
2774   cp_token *token;
2775
2776   /* Look for the identifier.  */
2777   token = cp_parser_require (parser, CPP_NAME, "identifier");
2778   /* Return the value.  */
2779   return token ? token->u.value : error_mark_node;
2780 }
2781
2782 /* Parse a sequence of adjacent string constants.  Returns a
2783    TREE_STRING representing the combined, nul-terminated string
2784    constant.  If TRANSLATE is true, translate the string to the
2785    execution character set.  If WIDE_OK is true, a wide string is
2786    invalid here.
2787
2788    C++98 [lex.string] says that if a narrow string literal token is
2789    adjacent to a wide string literal token, the behavior is undefined.
2790    However, C99 6.4.5p4 says that this results in a wide string literal.
2791    We follow C99 here, for consistency with the C front end.
2792
2793    This code is largely lifted from lex_string() in c-lex.c.
2794
2795    FUTURE: ObjC++ will need to handle @-strings here.  */
2796 static tree
2797 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2798 {
2799   tree value;
2800   bool wide = false;
2801   size_t count;
2802   struct obstack str_ob;
2803   cpp_string str, istr, *strs;
2804   cp_token *tok;
2805
2806   tok = cp_lexer_peek_token (parser->lexer);
2807   if (!cp_parser_is_string_literal (tok))
2808     {
2809       cp_parser_error (parser, "expected string-literal");
2810       return error_mark_node;
2811     }
2812
2813   /* Try to avoid the overhead of creating and destroying an obstack
2814      for the common case of just one string.  */
2815   if (!cp_parser_is_string_literal
2816       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2817     {
2818       cp_lexer_consume_token (parser->lexer);
2819
2820       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2821       str.len = TREE_STRING_LENGTH (tok->u.value);
2822       count = 1;
2823       if (tok->type == CPP_WSTRING)
2824         wide = true;
2825
2826       strs = &str;
2827     }
2828   else
2829     {
2830       gcc_obstack_init (&str_ob);
2831       count = 0;
2832
2833       do
2834         {
2835           cp_lexer_consume_token (parser->lexer);
2836           count++;
2837           str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2838           str.len = TREE_STRING_LENGTH (tok->u.value);
2839           if (tok->type == CPP_WSTRING)
2840             wide = true;
2841
2842           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2843
2844           tok = cp_lexer_peek_token (parser->lexer);
2845         }
2846       while (cp_parser_is_string_literal (tok));
2847
2848       strs = (cpp_string *) obstack_finish (&str_ob);
2849     }
2850
2851   if (wide && !wide_ok)
2852     {
2853       cp_parser_error (parser, "a wide string is invalid in this context");
2854       wide = false;
2855     }
2856
2857   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2858       (parse_in, strs, count, &istr, wide))
2859     {
2860       value = build_string (istr.len, (char *)istr.text);
2861       free ((void *)istr.text);
2862
2863       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2864       value = fix_string_type (value);
2865     }
2866   else
2867     /* cpp_interpret_string has issued an error.  */
2868     value = error_mark_node;
2869
2870   if (count > 1)
2871     obstack_free (&str_ob, 0);
2872
2873   return value;
2874 }
2875
2876
2877 /* Basic concepts [gram.basic]  */
2878
2879 /* Parse a translation-unit.
2880
2881    translation-unit:
2882      declaration-seq [opt]
2883
2884    Returns TRUE if all went well.  */
2885
2886 static bool
2887 cp_parser_translation_unit (cp_parser* parser)
2888 {
2889   /* The address of the first non-permanent object on the declarator
2890      obstack.  */
2891   static void *declarator_obstack_base;
2892
2893   bool success;
2894
2895   /* Create the declarator obstack, if necessary.  */
2896   if (!cp_error_declarator)
2897     {
2898       gcc_obstack_init (&declarator_obstack);
2899       /* Create the error declarator.  */
2900       cp_error_declarator = make_declarator (cdk_error);
2901       /* Create the empty parameter list.  */
2902       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2903       /* Remember where the base of the declarator obstack lies.  */
2904       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2905     }
2906
2907   cp_parser_declaration_seq_opt (parser);
2908
2909   /* If there are no tokens left then all went well.  */
2910   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2911     {
2912       /* Get rid of the token array; we don't need it any more.  */
2913       cp_lexer_destroy (parser->lexer);
2914       parser->lexer = NULL;
2915
2916       /* This file might have been a context that's implicitly extern
2917          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2918       if (parser->implicit_extern_c)
2919         {
2920           pop_lang_context ();
2921           parser->implicit_extern_c = false;
2922         }
2923
2924       /* Finish up.  */
2925       finish_translation_unit ();
2926
2927       success = true;
2928     }
2929   else
2930     {
2931       cp_parser_error (parser, "expected declaration");
2932       success = false;
2933     }
2934
2935   /* Make sure the declarator obstack was fully cleaned up.  */
2936   gcc_assert (obstack_next_free (&declarator_obstack)
2937               == declarator_obstack_base);
2938
2939   /* All went well.  */
2940   return success;
2941 }
2942
2943 /* Expressions [gram.expr] */
2944
2945 /* Parse a primary-expression.
2946
2947    primary-expression:
2948      literal
2949      this
2950      ( expression )
2951      id-expression
2952
2953    GNU Extensions:
2954
2955    primary-expression:
2956      ( compound-statement )
2957      __builtin_va_arg ( assignment-expression , type-id )
2958      __builtin_offsetof ( type-id , offsetof-expression )
2959
2960    Objective-C++ Extension:
2961
2962    primary-expression:
2963      objc-expression
2964
2965    literal:
2966      __null
2967
2968    ADDRESS_P is true iff this expression was immediately preceded by
2969    "&" and therefore might denote a pointer-to-member.  CAST_P is true
2970    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2971    true iff this expression is a template argument.
2972
2973    Returns a representation of the expression.  Upon return, *IDK
2974    indicates what kind of id-expression (if any) was present.  */
2975
2976 static tree
2977 cp_parser_primary_expression (cp_parser *parser,
2978                               bool address_p,
2979                               bool cast_p,
2980                               bool template_arg_p,
2981                               cp_id_kind *idk)
2982 {
2983   cp_token *token;
2984
2985   /* Assume the primary expression is not an id-expression.  */
2986   *idk = CP_ID_KIND_NONE;
2987
2988   /* Peek at the next token.  */
2989   token = cp_lexer_peek_token (parser->lexer);
2990   switch (token->type)
2991     {
2992       /* literal:
2993            integer-literal
2994            character-literal
2995            floating-literal
2996            string-literal
2997            boolean-literal  */
2998     case CPP_CHAR:
2999     case CPP_WCHAR:
3000     case CPP_NUMBER:
3001       token = cp_lexer_consume_token (parser->lexer);
3002       /* Floating-point literals are only allowed in an integral
3003          constant expression if they are cast to an integral or
3004          enumeration type.  */
3005       if (TREE_CODE (token->u.value) == REAL_CST
3006           && parser->integral_constant_expression_p
3007           && pedantic)
3008         {
3009           /* CAST_P will be set even in invalid code like "int(2.7 +
3010              ...)".   Therefore, we have to check that the next token
3011              is sure to end the cast.  */
3012           if (cast_p)
3013             {
3014               cp_token *next_token;
3015
3016               next_token = cp_lexer_peek_token (parser->lexer);
3017               if (/* The comma at the end of an
3018                      enumerator-definition.  */
3019                   next_token->type != CPP_COMMA
3020                   /* The curly brace at the end of an enum-specifier.  */
3021                   && next_token->type != CPP_CLOSE_BRACE
3022                   /* The end of a statement.  */
3023                   && next_token->type != CPP_SEMICOLON
3024                   /* The end of the cast-expression.  */
3025                   && next_token->type != CPP_CLOSE_PAREN
3026                   /* The end of an array bound.  */
3027                   && next_token->type != CPP_CLOSE_SQUARE
3028                   /* The closing ">" in a template-argument-list.  */
3029                   && (next_token->type != CPP_GREATER
3030                       || parser->greater_than_is_operator_p))
3031                 cast_p = false;
3032             }
3033
3034           /* If we are within a cast, then the constraint that the
3035              cast is to an integral or enumeration type will be
3036              checked at that point.  If we are not within a cast, then
3037              this code is invalid.  */
3038           if (!cast_p)
3039             cp_parser_non_integral_constant_expression
3040               (parser, "floating-point literal");
3041         }
3042       return token->u.value;
3043
3044     case CPP_STRING:
3045     case CPP_WSTRING:
3046       /* ??? Should wide strings be allowed when parser->translate_strings_p
3047          is false (i.e. in attributes)?  If not, we can kill the third
3048          argument to cp_parser_string_literal.  */
3049       return cp_parser_string_literal (parser,
3050                                        parser->translate_strings_p,
3051                                        true);
3052
3053     case CPP_OPEN_PAREN:
3054       {
3055         tree expr;
3056         bool saved_greater_than_is_operator_p;
3057
3058         /* Consume the `('.  */
3059         cp_lexer_consume_token (parser->lexer);
3060         /* Within a parenthesized expression, a `>' token is always
3061            the greater-than operator.  */
3062         saved_greater_than_is_operator_p
3063           = parser->greater_than_is_operator_p;
3064         parser->greater_than_is_operator_p = true;
3065         /* If we see `( { ' then we are looking at the beginning of
3066            a GNU statement-expression.  */
3067         if (cp_parser_allow_gnu_extensions_p (parser)
3068             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3069           {
3070             /* Statement-expressions are not allowed by the standard.  */
3071             if (pedantic)
3072               pedwarn ("ISO C++ forbids braced-groups within expressions");
3073
3074             /* And they're not allowed outside of a function-body; you
3075                cannot, for example, write:
3076
3077                  int i = ({ int j = 3; j + 1; });
3078
3079                at class or namespace scope.  */
3080             if (!parser->in_function_body)
3081               {
3082                 error ("statement-expressions are allowed only inside functions");
3083                 cp_parser_skip_to_end_of_block_or_statement (parser);
3084                 expr = error_mark_node;
3085               }
3086             else
3087               {
3088                 /* Start the statement-expression.  */
3089                 expr = begin_stmt_expr ();
3090                 /* Parse the compound-statement.  */
3091                 cp_parser_compound_statement (parser, expr, false);
3092                 /* Finish up.  */
3093                 expr = finish_stmt_expr (expr, false);
3094               }
3095           }
3096         else
3097           {
3098             /* Parse the parenthesized expression.  */
3099             expr = cp_parser_expression (parser, cast_p);
3100             /* Let the front end know that this expression was
3101                enclosed in parentheses. This matters in case, for
3102                example, the expression is of the form `A::B', since
3103                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3104                not.  */
3105             finish_parenthesized_expr (expr);
3106           }
3107         /* The `>' token might be the end of a template-id or
3108            template-parameter-list now.  */
3109         parser->greater_than_is_operator_p
3110           = saved_greater_than_is_operator_p;
3111         /* Consume the `)'.  */
3112         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3113           cp_parser_skip_to_end_of_statement (parser);
3114
3115         return expr;
3116       }
3117
3118     case CPP_KEYWORD:
3119       switch (token->keyword)
3120         {
3121           /* These two are the boolean literals.  */
3122         case RID_TRUE:
3123           cp_lexer_consume_token (parser->lexer);
3124           return boolean_true_node;
3125         case RID_FALSE:
3126           cp_lexer_consume_token (parser->lexer);
3127           return boolean_false_node;
3128
3129           /* The `__null' literal.  */
3130         case RID_NULL:
3131           cp_lexer_consume_token (parser->lexer);
3132           return null_node;
3133
3134           /* Recognize the `this' keyword.  */
3135         case RID_THIS:
3136           cp_lexer_consume_token (parser->lexer);
3137           if (parser->local_variables_forbidden_p)
3138             {
3139               error ("%<this%> may not be used in this context");
3140               return error_mark_node;
3141             }
3142           /* Pointers cannot appear in constant-expressions.  */
3143           if (cp_parser_non_integral_constant_expression (parser,
3144                                                           "`this'"))
3145             return error_mark_node;
3146           return finish_this_expr ();
3147
3148           /* The `operator' keyword can be the beginning of an
3149              id-expression.  */
3150         case RID_OPERATOR:
3151           goto id_expression;
3152
3153         case RID_FUNCTION_NAME:
3154         case RID_PRETTY_FUNCTION_NAME:
3155         case RID_C99_FUNCTION_NAME:
3156           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3157              __func__ are the names of variables -- but they are
3158              treated specially.  Therefore, they are handled here,
3159              rather than relying on the generic id-expression logic
3160              below.  Grammatically, these names are id-expressions.
3161
3162              Consume the token.  */
3163           token = cp_lexer_consume_token (parser->lexer);
3164           /* Look up the name.  */
3165           return finish_fname (token->u.value);
3166
3167         case RID_VA_ARG:
3168           {
3169             tree expression;
3170             tree type;
3171
3172             /* The `__builtin_va_arg' construct is used to handle
3173                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3174             cp_lexer_consume_token (parser->lexer);
3175             /* Look for the opening `('.  */
3176             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3177             /* Now, parse the assignment-expression.  */
3178             expression = cp_parser_assignment_expression (parser,
3179                                                           /*cast_p=*/false);
3180             /* Look for the `,'.  */
3181             cp_parser_require (parser, CPP_COMMA, "`,'");
3182             /* Parse the type-id.  */
3183             type = cp_parser_type_id (parser);
3184             /* Look for the closing `)'.  */
3185             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3186             /* Using `va_arg' in a constant-expression is not
3187                allowed.  */
3188             if (cp_parser_non_integral_constant_expression (parser,
3189                                                             "`va_arg'"))
3190               return error_mark_node;
3191             return build_x_va_arg (expression, type);
3192           }
3193
3194         case RID_OFFSETOF:
3195           return cp_parser_builtin_offsetof (parser);
3196
3197           /* Objective-C++ expressions.  */
3198         case RID_AT_ENCODE:
3199         case RID_AT_PROTOCOL:
3200         case RID_AT_SELECTOR:
3201           return cp_parser_objc_expression (parser);
3202
3203         default:
3204           cp_parser_error (parser, "expected primary-expression");
3205           return error_mark_node;
3206         }
3207
3208       /* An id-expression can start with either an identifier, a
3209          `::' as the beginning of a qualified-id, or the "operator"
3210          keyword.  */
3211     case CPP_NAME:
3212     case CPP_SCOPE:
3213     case CPP_TEMPLATE_ID:
3214     case CPP_NESTED_NAME_SPECIFIER:
3215       {
3216         tree id_expression;
3217         tree decl;
3218         const char *error_msg;
3219         bool template_p;
3220         bool done;
3221
3222       id_expression:
3223         /* Parse the id-expression.  */
3224         id_expression
3225           = cp_parser_id_expression (parser,
3226                                      /*template_keyword_p=*/false,
3227                                      /*check_dependency_p=*/true,
3228                                      &template_p,
3229                                      /*declarator_p=*/false,
3230                                      /*optional_p=*/false);
3231         if (id_expression == error_mark_node)
3232           return error_mark_node;
3233         token = cp_lexer_peek_token (parser->lexer);
3234         done = (token->type != CPP_OPEN_SQUARE
3235                 && token->type != CPP_OPEN_PAREN
3236                 && token->type != CPP_DOT
3237                 && token->type != CPP_DEREF
3238                 && token->type != CPP_PLUS_PLUS
3239                 && token->type != CPP_MINUS_MINUS);
3240         /* If we have a template-id, then no further lookup is
3241            required.  If the template-id was for a template-class, we
3242            will sometimes have a TYPE_DECL at this point.  */
3243         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3244                  || TREE_CODE (id_expression) == TYPE_DECL)
3245           decl = id_expression;
3246         /* Look up the name.  */
3247         else
3248           {
3249             tree ambiguous_decls;
3250
3251             decl = cp_parser_lookup_name (parser, id_expression,
3252                                           none_type,
3253                                           template_p,
3254                                           /*is_namespace=*/false,
3255                                           /*check_dependency=*/true,
3256                                           &ambiguous_decls);
3257             /* If the lookup was ambiguous, an error will already have
3258                been issued.  */
3259             if (ambiguous_decls)
3260               return error_mark_node;
3261
3262             /* In Objective-C++, an instance variable (ivar) may be preferred
3263                to whatever cp_parser_lookup_name() found.  */
3264             decl = objc_lookup_ivar (decl, id_expression);
3265
3266             /* If name lookup gives us a SCOPE_REF, then the
3267                qualifying scope was dependent.  */
3268             if (TREE_CODE (decl) == SCOPE_REF)
3269               return decl;
3270             /* Check to see if DECL is a local variable in a context
3271                where that is forbidden.  */
3272             if (parser->local_variables_forbidden_p
3273                 && local_variable_p (decl))
3274               {
3275                 /* It might be that we only found DECL because we are
3276                    trying to be generous with pre-ISO scoping rules.
3277                    For example, consider:
3278
3279                      int i;
3280                      void g() {
3281                        for (int i = 0; i < 10; ++i) {}
3282                        extern void f(int j = i);
3283                      }
3284
3285                    Here, name look up will originally find the out
3286                    of scope `i'.  We need to issue a warning message,
3287                    but then use the global `i'.  */
3288                 decl = check_for_out_of_scope_variable (decl);
3289                 if (local_variable_p (decl))
3290                   {
3291                     error ("local variable %qD may not appear in this context",
3292                            decl);
3293                     return error_mark_node;
3294                   }
3295               }
3296           }
3297
3298         decl = (finish_id_expression
3299                 (id_expression, decl, parser->scope,
3300                  idk,
3301                  parser->integral_constant_expression_p,
3302                  parser->allow_non_integral_constant_expression_p,
3303                  &parser->non_integral_constant_expression_p,
3304                  template_p, done, address_p,
3305                  template_arg_p,
3306                  &error_msg));
3307         if (error_msg)
3308           cp_parser_error (parser, error_msg);
3309         return decl;
3310       }
3311
3312       /* Anything else is an error.  */
3313     default:
3314       /* ...unless we have an Objective-C++ message or string literal,
3315          that is.  */
3316       if (c_dialect_objc ()
3317           && (token->type == CPP_OPEN_SQUARE
3318               || token->type == CPP_OBJC_STRING))
3319         return cp_parser_objc_expression (parser);
3320
3321       cp_parser_error (parser, "expected primary-expression");
3322       return error_mark_node;
3323     }
3324 }
3325
3326 /* Parse an id-expression.
3327
3328    id-expression:
3329      unqualified-id
3330      qualified-id
3331
3332    qualified-id:
3333      :: [opt] nested-name-specifier template [opt] unqualified-id
3334      :: identifier
3335      :: operator-function-id
3336      :: template-id
3337
3338    Return a representation of the unqualified portion of the
3339    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3340    a `::' or nested-name-specifier.
3341
3342    Often, if the id-expression was a qualified-id, the caller will
3343    want to make a SCOPE_REF to represent the qualified-id.  This
3344    function does not do this in order to avoid wastefully creating
3345    SCOPE_REFs when they are not required.
3346
3347    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3348    `template' keyword.
3349
3350    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3351    uninstantiated templates.
3352
3353    If *TEMPLATE_P is non-NULL, it is set to true iff the
3354    `template' keyword is used to explicitly indicate that the entity
3355    named is a template.
3356
3357    If DECLARATOR_P is true, the id-expression is appearing as part of
3358    a declarator, rather than as part of an expression.  */
3359
3360 static tree
3361 cp_parser_id_expression (cp_parser *parser,
3362                          bool template_keyword_p,
3363                          bool check_dependency_p,
3364                          bool *template_p,
3365                          bool declarator_p,
3366                          bool optional_p)
3367 {
3368   bool global_scope_p;
3369   bool nested_name_specifier_p;
3370
3371   /* Assume the `template' keyword was not used.  */
3372   if (template_p)
3373     *template_p = template_keyword_p;
3374
3375   /* Look for the optional `::' operator.  */
3376   global_scope_p
3377     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3378        != NULL_TREE);
3379   /* Look for the optional nested-name-specifier.  */
3380   nested_name_specifier_p
3381     = (cp_parser_nested_name_specifier_opt (parser,
3382                                             /*typename_keyword_p=*/false,
3383                                             check_dependency_p,
3384                                             /*type_p=*/false,
3385                                             declarator_p)
3386        != NULL_TREE);
3387   /* If there is a nested-name-specifier, then we are looking at
3388      the first qualified-id production.  */
3389   if (nested_name_specifier_p)
3390     {
3391       tree saved_scope;
3392       tree saved_object_scope;
3393       tree saved_qualifying_scope;
3394       tree unqualified_id;
3395       bool is_template;
3396
3397       /* See if the next token is the `template' keyword.  */
3398       if (!template_p)
3399         template_p = &is_template;
3400       *template_p = cp_parser_optional_template_keyword (parser);
3401       /* Name lookup we do during the processing of the
3402          unqualified-id might obliterate SCOPE.  */
3403       saved_scope = parser->scope;
3404       saved_object_scope = parser->object_scope;
3405       saved_qualifying_scope = parser->qualifying_scope;
3406       /* Process the final unqualified-id.  */
3407       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3408                                                  check_dependency_p,
3409                                                  declarator_p,
3410                                                  /*optional_p=*/false);
3411       /* Restore the SAVED_SCOPE for our caller.  */
3412       parser->scope = saved_scope;
3413       parser->object_scope = saved_object_scope;
3414       parser->qualifying_scope = saved_qualifying_scope;
3415
3416       return unqualified_id;
3417     }
3418   /* Otherwise, if we are in global scope, then we are looking at one
3419      of the other qualified-id productions.  */
3420   else if (global_scope_p)
3421     {
3422       cp_token *token;
3423       tree id;
3424
3425       /* Peek at the next token.  */
3426       token = cp_lexer_peek_token (parser->lexer);
3427
3428       /* If it's an identifier, and the next token is not a "<", then
3429          we can avoid the template-id case.  This is an optimization
3430          for this common case.  */
3431       if (token->type == CPP_NAME
3432           && !cp_parser_nth_token_starts_template_argument_list_p
3433                (parser, 2))
3434         return cp_parser_identifier (parser);
3435
3436       cp_parser_parse_tentatively (parser);
3437       /* Try a template-id.  */
3438       id = cp_parser_template_id (parser,
3439                                   /*template_keyword_p=*/false,
3440                                   /*check_dependency_p=*/true,
3441                                   declarator_p);
3442       /* If that worked, we're done.  */
3443       if (cp_parser_parse_definitely (parser))
3444         return id;
3445
3446       /* Peek at the next token.  (Changes in the token buffer may
3447          have invalidated the pointer obtained above.)  */
3448       token = cp_lexer_peek_token (parser->lexer);
3449
3450       switch (token->type)
3451         {
3452         case CPP_NAME:
3453           return cp_parser_identifier (parser);
3454
3455         case CPP_KEYWORD:
3456           if (token->keyword == RID_OPERATOR)
3457             return cp_parser_operator_function_id (parser);
3458           /* Fall through.  */
3459
3460         default:
3461           cp_parser_error (parser, "expected id-expression");
3462           return error_mark_node;
3463         }
3464     }
3465   else
3466     return cp_parser_unqualified_id (parser, template_keyword_p,
3467                                      /*check_dependency_p=*/true,
3468                                      declarator_p,
3469                                      optional_p);
3470 }
3471
3472 /* Parse an unqualified-id.
3473
3474    unqualified-id:
3475      identifier
3476      operator-function-id
3477      conversion-function-id
3478      ~ class-name
3479      template-id
3480
3481    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3482    keyword, in a construct like `A::template ...'.
3483
3484    Returns a representation of unqualified-id.  For the `identifier'
3485    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3486    production a BIT_NOT_EXPR is returned; the operand of the
3487    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3488    other productions, see the documentation accompanying the
3489    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3490    names are looked up in uninstantiated templates.  If DECLARATOR_P
3491    is true, the unqualified-id is appearing as part of a declarator,
3492    rather than as part of an expression.  */
3493
3494 static tree
3495 cp_parser_unqualified_id (cp_parser* parser,
3496                           bool template_keyword_p,
3497                           bool check_dependency_p,
3498                           bool declarator_p,
3499                           bool optional_p)
3500 {
3501   cp_token *token;
3502
3503   /* Peek at the next token.  */
3504   token = cp_lexer_peek_token (parser->lexer);
3505
3506   switch (token->type)
3507     {
3508     case CPP_NAME:
3509       {
3510         tree id;
3511
3512         /* We don't know yet whether or not this will be a
3513            template-id.  */
3514         cp_parser_parse_tentatively (parser);
3515         /* Try a template-id.  */
3516         id = cp_parser_template_id (parser, template_keyword_p,
3517                                     check_dependency_p,
3518                                     declarator_p);
3519         /* If it worked, we're done.  */
3520         if (cp_parser_parse_definitely (parser))
3521           return id;
3522         /* Otherwise, it's an ordinary identifier.  */
3523         return cp_parser_identifier (parser);
3524       }
3525
3526     case CPP_TEMPLATE_ID:
3527       return cp_parser_template_id (parser, template_keyword_p,
3528                                     check_dependency_p,
3529                                     declarator_p);
3530
3531     case CPP_COMPL:
3532       {
3533         tree type_decl;
3534         tree qualifying_scope;
3535         tree object_scope;
3536         tree scope;
3537         bool done;
3538
3539         /* Consume the `~' token.  */
3540         cp_lexer_consume_token (parser->lexer);
3541         /* Parse the class-name.  The standard, as written, seems to
3542            say that:
3543
3544              template <typename T> struct S { ~S (); };
3545              template <typename T> S<T>::~S() {}
3546
3547            is invalid, since `~' must be followed by a class-name, but
3548            `S<T>' is dependent, and so not known to be a class.
3549            That's not right; we need to look in uninstantiated
3550            templates.  A further complication arises from:
3551
3552              template <typename T> void f(T t) {
3553                t.T::~T();
3554              }
3555
3556            Here, it is not possible to look up `T' in the scope of `T'
3557            itself.  We must look in both the current scope, and the
3558            scope of the containing complete expression.
3559
3560            Yet another issue is:
3561
3562              struct S {
3563                int S;
3564                ~S();
3565              };
3566
3567              S::~S() {}
3568
3569            The standard does not seem to say that the `S' in `~S'
3570            should refer to the type `S' and not the data member
3571            `S::S'.  */
3572
3573         /* DR 244 says that we look up the name after the "~" in the
3574            same scope as we looked up the qualifying name.  That idea
3575            isn't fully worked out; it's more complicated than that.  */
3576         scope = parser->scope;
3577         object_scope = parser->object_scope;
3578         qualifying_scope = parser->qualifying_scope;
3579
3580         /* Check for invalid scopes.  */
3581         if (scope == error_mark_node)
3582           {
3583             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3584               cp_lexer_consume_token (parser->lexer);
3585             return error_mark_node;
3586           }
3587         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3588           {
3589             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3590               error ("scope %qT before %<~%> is not a class-name", scope);
3591             cp_parser_simulate_error (parser);
3592             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3593               cp_lexer_consume_token (parser->lexer);
3594             return error_mark_node;
3595           }
3596         gcc_assert (!scope || TYPE_P (scope));
3597
3598         /* If the name is of the form "X::~X" it's OK.  */
3599         token = cp_lexer_peek_token (parser->lexer);
3600         if (scope
3601             && token->type == CPP_NAME
3602             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3603                 == CPP_OPEN_PAREN)
3604             && constructor_name_p (token->u.value, scope))
3605           {
3606             cp_lexer_consume_token (parser->lexer);
3607             return build_nt (BIT_NOT_EXPR, scope);
3608           }
3609
3610         /* If there was an explicit qualification (S::~T), first look
3611            in the scope given by the qualification (i.e., S).  */
3612         done = false;
3613         type_decl = NULL_TREE;
3614         if (scope)
3615           {
3616             cp_parser_parse_tentatively (parser);
3617             type_decl = cp_parser_class_name (parser,
3618                                               /*typename_keyword_p=*/false,
3619                                               /*template_keyword_p=*/false,
3620                                               none_type,
3621                                               /*check_dependency=*/false,
3622                                               /*class_head_p=*/false,
3623                                               declarator_p);
3624             if (cp_parser_parse_definitely (parser))
3625               done = true;
3626           }
3627         /* In "N::S::~S", look in "N" as well.  */
3628         if (!done && scope && qualifying_scope)
3629           {
3630             cp_parser_parse_tentatively (parser);
3631             parser->scope = qualifying_scope;
3632             parser->object_scope = NULL_TREE;
3633             parser->qualifying_scope = NULL_TREE;
3634             type_decl
3635               = cp_parser_class_name (parser,
3636                                       /*typename_keyword_p=*/false,
3637                                       /*template_keyword_p=*/false,
3638                                       none_type,
3639                                       /*check_dependency=*/false,
3640                                       /*class_head_p=*/false,
3641                                       declarator_p);
3642             if (cp_parser_parse_definitely (parser))
3643               done = true;
3644           }
3645         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3646         else if (!done && object_scope)
3647           {
3648             cp_parser_parse_tentatively (parser);
3649             parser->scope = object_scope;
3650             parser->object_scope = NULL_TREE;
3651             parser->qualifying_scope = NULL_TREE;
3652             type_decl
3653               = cp_parser_class_name (parser,
3654                                       /*typename_keyword_p=*/false,
3655                                       /*template_keyword_p=*/false,
3656                                       none_type,
3657                                       /*check_dependency=*/false,
3658                                       /*class_head_p=*/false,
3659                                       declarator_p);
3660             if (cp_parser_parse_definitely (parser))
3661               done = true;
3662           }
3663         /* Look in the surrounding context.  */
3664         if (!done)
3665           {
3666             parser->scope = NULL_TREE;
3667             parser->object_scope = NULL_TREE;
3668             parser->qualifying_scope = NULL_TREE;
3669             type_decl
3670               = cp_parser_class_name (parser,
3671                                       /*typename_keyword_p=*/false,
3672                                       /*template_keyword_p=*/false,
3673                                       none_type,
3674                                       /*check_dependency=*/false,
3675                                       /*class_head_p=*/false,
3676                                       declarator_p);
3677           }
3678         /* If an error occurred, assume that the name of the
3679            destructor is the same as the name of the qualifying
3680            class.  That allows us to keep parsing after running
3681            into ill-formed destructor names.  */
3682         if (type_decl == error_mark_node && scope)
3683           return build_nt (BIT_NOT_EXPR, scope);
3684         else if (type_decl == error_mark_node)
3685           return error_mark_node;
3686
3687         /* Check that destructor name and scope match.  */
3688         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3689           {
3690             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3691               error ("declaration of %<~%T%> as member of %qT",
3692                      type_decl, scope);
3693             cp_parser_simulate_error (parser);
3694             return error_mark_node;
3695           }
3696
3697         /* [class.dtor]
3698
3699            A typedef-name that names a class shall not be used as the
3700            identifier in the declarator for a destructor declaration.  */
3701         if (declarator_p
3702             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3703             && !DECL_SELF_REFERENCE_P (type_decl)
3704             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3705           error ("typedef-name %qD used as destructor declarator",
3706                  type_decl);
3707
3708         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3709       }
3710
3711     case CPP_KEYWORD:
3712       if (token->keyword == RID_OPERATOR)
3713         {
3714           tree id;
3715
3716           /* This could be a template-id, so we try that first.  */
3717           cp_parser_parse_tentatively (parser);
3718           /* Try a template-id.  */
3719           id = cp_parser_template_id (parser, template_keyword_p,
3720                                       /*check_dependency_p=*/true,
3721                                       declarator_p);
3722           /* If that worked, we're done.  */
3723           if (cp_parser_parse_definitely (parser))
3724             return id;
3725           /* We still don't know whether we're looking at an
3726              operator-function-id or a conversion-function-id.  */
3727           cp_parser_parse_tentatively (parser);
3728           /* Try an operator-function-id.  */
3729           id = cp_parser_operator_function_id (parser);
3730           /* If that didn't work, try a conversion-function-id.  */
3731           if (!cp_parser_parse_definitely (parser))
3732             id = cp_parser_conversion_function_id (parser);
3733
3734           return id;
3735         }
3736       /* Fall through.  */
3737
3738     default:
3739       if (optional_p)
3740         return NULL_TREE;
3741       cp_parser_error (parser, "expected unqualified-id");
3742       return error_mark_node;
3743     }
3744 }
3745
3746 /* Parse an (optional) nested-name-specifier.
3747
3748    nested-name-specifier:
3749      class-or-namespace-name :: nested-name-specifier [opt]
3750      class-or-namespace-name :: template nested-name-specifier [opt]
3751
3752    PARSER->SCOPE should be set appropriately before this function is
3753    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3754    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3755    in name lookups.
3756
3757    Sets PARSER->SCOPE to the class (TYPE) or namespace
3758    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3759    it unchanged if there is no nested-name-specifier.  Returns the new
3760    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3761
3762    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3763    part of a declaration and/or decl-specifier.  */
3764
3765 static tree
3766 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3767                                      bool typename_keyword_p,
3768                                      bool check_dependency_p,
3769                                      bool type_p,
3770                                      bool is_declaration)
3771 {
3772   bool success = false;
3773   cp_token_position start = 0;
3774   cp_token *token;
3775
3776   /* Remember where the nested-name-specifier starts.  */
3777   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3778     {
3779       start = cp_lexer_token_position (parser->lexer, false);
3780       push_deferring_access_checks (dk_deferred);
3781     }
3782
3783   while (true)
3784     {
3785       tree new_scope;
3786       tree old_scope;
3787       tree saved_qualifying_scope;
3788       bool template_keyword_p;
3789
3790       /* Spot cases that cannot be the beginning of a
3791          nested-name-specifier.  */
3792       token = cp_lexer_peek_token (parser->lexer);
3793
3794       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3795          the already parsed nested-name-specifier.  */
3796       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3797         {
3798           /* Grab the nested-name-specifier and continue the loop.  */
3799           cp_parser_pre_parsed_nested_name_specifier (parser);
3800           /* If we originally encountered this nested-name-specifier
3801              with IS_DECLARATION set to false, we will not have
3802              resolved TYPENAME_TYPEs, so we must do so here.  */
3803           if (is_declaration
3804               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3805             {
3806               new_scope = resolve_typename_type (parser->scope,
3807                                                  /*only_current_p=*/false);
3808               if (new_scope != error_mark_node)
3809                 parser->scope = new_scope;
3810             }
3811           success = true;
3812           continue;
3813         }
3814
3815       /* Spot cases that cannot be the beginning of a
3816          nested-name-specifier.  On the second and subsequent times
3817          through the loop, we look for the `template' keyword.  */
3818       if (success && token->keyword == RID_TEMPLATE)
3819         ;
3820       /* A template-id can start a nested-name-specifier.  */
3821       else if (token->type == CPP_TEMPLATE_ID)
3822         ;
3823       else
3824         {
3825           /* If the next token is not an identifier, then it is
3826              definitely not a class-or-namespace-name.  */
3827           if (token->type != CPP_NAME)
3828             break;
3829           /* If the following token is neither a `<' (to begin a
3830              template-id), nor a `::', then we are not looking at a
3831              nested-name-specifier.  */
3832           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3833           if (token->type != CPP_SCOPE
3834               && !cp_parser_nth_token_starts_template_argument_list_p
3835                   (parser, 2))
3836             break;
3837         }
3838
3839       /* The nested-name-specifier is optional, so we parse
3840          tentatively.  */
3841       cp_parser_parse_tentatively (parser);
3842
3843       /* Look for the optional `template' keyword, if this isn't the
3844          first time through the loop.  */
3845       if (success)
3846         template_keyword_p = cp_parser_optional_template_keyword (parser);
3847       else
3848         template_keyword_p = false;
3849
3850       /* Save the old scope since the name lookup we are about to do
3851          might destroy it.  */
3852       old_scope = parser->scope;
3853       saved_qualifying_scope = parser->qualifying_scope;
3854       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3855          look up names in "X<T>::I" in order to determine that "Y" is
3856          a template.  So, if we have a typename at this point, we make
3857          an effort to look through it.  */
3858       if (is_declaration
3859           && !typename_keyword_p
3860           && parser->scope
3861           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3862         parser->scope = resolve_typename_type (parser->scope,
3863                                                /*only_current_p=*/false);
3864       /* Parse the qualifying entity.  */
3865       new_scope
3866         = cp_parser_class_or_namespace_name (parser,
3867                                              typename_keyword_p,
3868                                              template_keyword_p,
3869                                              check_dependency_p,
3870                                              type_p,
3871                                              is_declaration);
3872       /* Look for the `::' token.  */
3873       cp_parser_require (parser, CPP_SCOPE, "`::'");
3874
3875       /* If we found what we wanted, we keep going; otherwise, we're
3876          done.  */
3877       if (!cp_parser_parse_definitely (parser))
3878         {
3879           bool error_p = false;
3880
3881           /* Restore the OLD_SCOPE since it was valid before the
3882              failed attempt at finding the last
3883              class-or-namespace-name.  */
3884           parser->scope = old_scope;
3885           parser->qualifying_scope = saved_qualifying_scope;
3886           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3887             break;
3888           /* If the next token is an identifier, and the one after
3889              that is a `::', then any valid interpretation would have
3890              found a class-or-namespace-name.  */
3891           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3892                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3893                      == CPP_SCOPE)
3894                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3895                      != CPP_COMPL))
3896             {
3897               token = cp_lexer_consume_token (parser->lexer);
3898               if (!error_p)
3899                 {
3900                   if (!token->ambiguous_p)
3901                     {
3902                       tree decl;
3903                       tree ambiguous_decls;
3904
3905                       decl = cp_parser_lookup_name (parser, token->u.value,
3906                                                     none_type,
3907                                                     /*is_template=*/false,
3908                                                     /*is_namespace=*/false,
3909                                                     /*check_dependency=*/true,
3910                                                     &ambiguous_decls);
3911                       if (TREE_CODE (decl) == TEMPLATE_DECL)
3912                         error ("%qD used without template parameters", decl);
3913                       else if (ambiguous_decls)
3914                         {
3915                           error ("reference to %qD is ambiguous",
3916                                  token->u.value);
3917                           print_candidates (ambiguous_decls);
3918                           decl = error_mark_node;
3919                         }
3920                       else
3921                         cp_parser_name_lookup_error
3922                           (parser, token->u.value, decl,
3923                            "is not a class or namespace");
3924                     }
3925                   parser->scope = error_mark_node;
3926                   error_p = true;
3927                   /* Treat this as a successful nested-name-specifier
3928                      due to:
3929
3930                      [basic.lookup.qual]
3931
3932                      If the name found is not a class-name (clause
3933                      _class_) or namespace-name (_namespace.def_), the
3934                      program is ill-formed.  */
3935                   success = true;
3936                 }
3937               cp_lexer_consume_token (parser->lexer);
3938             }
3939           break;
3940         }
3941       /* We've found one valid nested-name-specifier.  */
3942       success = true;
3943       /* Name lookup always gives us a DECL.  */
3944       if (TREE_CODE (new_scope) == TYPE_DECL)
3945         new_scope = TREE_TYPE (new_scope);
3946       /* Uses of "template" must be followed by actual templates.  */
3947       if (template_keyword_p
3948           && !(CLASS_TYPE_P (new_scope)
3949                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3950                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3951                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
3952           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3953                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3954                    == TEMPLATE_ID_EXPR)))
3955         pedwarn (TYPE_P (new_scope)
3956                  ? "%qT is not a template"
3957                  : "%qD is not a template",
3958                  new_scope);
3959       /* If it is a class scope, try to complete it; we are about to
3960          be looking up names inside the class.  */
3961       if (TYPE_P (new_scope)
3962           /* Since checking types for dependency can be expensive,
3963              avoid doing it if the type is already complete.  */
3964           && !COMPLETE_TYPE_P (new_scope)
3965           /* Do not try to complete dependent types.  */
3966           && !dependent_type_p (new_scope))
3967         new_scope = complete_type (new_scope);
3968       /* Make sure we look in the right scope the next time through
3969          the loop.  */
3970       parser->scope = new_scope;
3971     }
3972
3973   /* If parsing tentatively, replace the sequence of tokens that makes
3974      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3975      token.  That way, should we re-parse the token stream, we will
3976      not have to repeat the effort required to do the parse, nor will
3977      we issue duplicate error messages.  */
3978   if (success && start)
3979     {
3980       cp_token *token;
3981
3982       token = cp_lexer_token_at (parser->lexer, start);
3983       /* Reset the contents of the START token.  */
3984       token->type = CPP_NESTED_NAME_SPECIFIER;
3985       /* Retrieve any deferred checks.  Do not pop this access checks yet
3986          so the memory will not be reclaimed during token replacing below.  */
3987       token->u.tree_check_value = GGC_CNEW (struct tree_check);
3988       token->u.tree_check_value->value = parser->scope;
3989       token->u.tree_check_value->checks = get_deferred_access_checks ();
3990       token->u.tree_check_value->qualifying_scope =
3991         parser->qualifying_scope;
3992       token->keyword = RID_MAX;
3993
3994       /* Purge all subsequent tokens.  */
3995       cp_lexer_purge_tokens_after (parser->lexer, start);
3996     }
3997
3998   if (start)
3999     pop_to_parent_deferring_access_checks ();
4000
4001   return success ? parser->scope : NULL_TREE;
4002 }
4003
4004 /* Parse a nested-name-specifier.  See
4005    cp_parser_nested_name_specifier_opt for details.  This function
4006    behaves identically, except that it will an issue an error if no
4007    nested-name-specifier is present.  */
4008
4009 static tree
4010 cp_parser_nested_name_specifier (cp_parser *parser,
4011                                  bool typename_keyword_p,
4012                                  bool check_dependency_p,
4013                                  bool type_p,
4014                                  bool is_declaration)
4015 {
4016   tree scope;
4017
4018   /* Look for the nested-name-specifier.  */
4019   scope = cp_parser_nested_name_specifier_opt (parser,
4020                                                typename_keyword_p,
4021                                                check_dependency_p,
4022                                                type_p,
4023                                                is_declaration);
4024   /* If it was not present, issue an error message.  */
4025   if (!scope)
4026     {
4027       cp_parser_error (parser, "expected nested-name-specifier");
4028       parser->scope = NULL_TREE;
4029     }
4030
4031   return scope;
4032 }
4033
4034 /* Parse a class-or-namespace-name.
4035
4036    class-or-namespace-name:
4037      class-name
4038      namespace-name
4039
4040    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4041    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4042    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4043    TYPE_P is TRUE iff the next name should be taken as a class-name,
4044    even the same name is declared to be another entity in the same
4045    scope.
4046
4047    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4048    specified by the class-or-namespace-name.  If neither is found the
4049    ERROR_MARK_NODE is returned.  */
4050
4051 static tree
4052 cp_parser_class_or_namespace_name (cp_parser *parser,
4053                                    bool typename_keyword_p,
4054                                    bool template_keyword_p,
4055                                    bool check_dependency_p,
4056                                    bool type_p,
4057                                    bool is_declaration)
4058 {
4059   tree saved_scope;
4060   tree saved_qualifying_scope;
4061   tree saved_object_scope;
4062   tree scope;
4063   bool only_class_p;
4064
4065   /* Before we try to parse the class-name, we must save away the
4066      current PARSER->SCOPE since cp_parser_class_name will destroy
4067      it.  */
4068   saved_scope = parser->scope;
4069   saved_qualifying_scope = parser->qualifying_scope;
4070   saved_object_scope = parser->object_scope;
4071   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4072      there is no need to look for a namespace-name.  */
4073   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4074   if (!only_class_p)
4075     cp_parser_parse_tentatively (parser);
4076   scope = cp_parser_class_name (parser,
4077                                 typename_keyword_p,
4078                                 template_keyword_p,
4079                                 type_p ? class_type : none_type,
4080                                 check_dependency_p,
4081                                 /*class_head_p=*/false,
4082                                 is_declaration);
4083   /* If that didn't work, try for a namespace-name.  */
4084   if (!only_class_p && !cp_parser_parse_definitely (parser))
4085     {
4086       /* Restore the saved scope.  */
4087       parser->scope = saved_scope;
4088       parser->qualifying_scope = saved_qualifying_scope;
4089       parser->object_scope = saved_object_scope;
4090       /* If we are not looking at an identifier followed by the scope
4091          resolution operator, then this is not part of a
4092          nested-name-specifier.  (Note that this function is only used
4093          to parse the components of a nested-name-specifier.)  */
4094       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4095           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4096         return error_mark_node;
4097       scope = cp_parser_namespace_name (parser);
4098     }
4099
4100   return scope;
4101 }
4102
4103 /* Parse a postfix-expression.
4104
4105    postfix-expression:
4106      primary-expression
4107      postfix-expression [ expression ]
4108      postfix-expression ( expression-list [opt] )
4109      simple-type-specifier ( expression-list [opt] )
4110      typename :: [opt] nested-name-specifier identifier
4111        ( expression-list [opt] )
4112      typename :: [opt] nested-name-specifier template [opt] template-id
4113        ( expression-list [opt] )
4114      postfix-expression . template [opt] id-expression
4115      postfix-expression -> template [opt] id-expression
4116      postfix-expression . pseudo-destructor-name
4117      postfix-expression -> pseudo-destructor-name
4118      postfix-expression ++
4119      postfix-expression --
4120      dynamic_cast < type-id > ( expression )
4121      static_cast < type-id > ( expression )
4122      reinterpret_cast < type-id > ( expression )
4123      const_cast < type-id > ( expression )
4124      typeid ( expression )
4125      typeid ( type-id )
4126
4127    GNU Extension:
4128
4129    postfix-expression:
4130      ( type-id ) { initializer-list , [opt] }
4131
4132    This extension is a GNU version of the C99 compound-literal
4133    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4134    but they are essentially the same concept.)
4135
4136    If ADDRESS_P is true, the postfix expression is the operand of the
4137    `&' operator.  CAST_P is true if this expression is the target of a
4138    cast.
4139
4140    Returns a representation of the expression.  */
4141
4142 static tree
4143 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4144 {
4145   cp_token *token;
4146   enum rid keyword;
4147   cp_id_kind idk = CP_ID_KIND_NONE;
4148   tree postfix_expression = NULL_TREE;
4149
4150   /* Peek at the next token.  */
4151   token = cp_lexer_peek_token (parser->lexer);
4152   /* Some of the productions are determined by keywords.  */
4153   keyword = token->keyword;
4154   switch (keyword)
4155     {
4156     case RID_DYNCAST:
4157     case RID_STATCAST:
4158     case RID_REINTCAST:
4159     case RID_CONSTCAST:
4160       {
4161         tree type;
4162         tree expression;
4163         const char *saved_message;
4164
4165         /* All of these can be handled in the same way from the point
4166            of view of parsing.  Begin by consuming the token
4167            identifying the cast.  */
4168         cp_lexer_consume_token (parser->lexer);
4169
4170         /* New types cannot be defined in the cast.  */
4171         saved_message = parser->type_definition_forbidden_message;
4172         parser->type_definition_forbidden_message
4173           = "types may not be defined in casts";
4174
4175         /* Look for the opening `<'.  */
4176         cp_parser_require (parser, CPP_LESS, "`<'");
4177         /* Parse the type to which we are casting.  */
4178         type = cp_parser_type_id (parser);
4179         /* Look for the closing `>'.  */
4180         cp_parser_require (parser, CPP_GREATER, "`>'");
4181         /* Restore the old message.  */
4182         parser->type_definition_forbidden_message = saved_message;
4183
4184         /* And the expression which is being cast.  */
4185         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4186         expression = cp_parser_expression (parser, /*cast_p=*/true);
4187         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4188
4189         /* Only type conversions to integral or enumeration types
4190            can be used in constant-expressions.  */
4191         if (!cast_valid_in_integral_constant_expression_p (type)
4192             && (cp_parser_non_integral_constant_expression
4193                 (parser,
4194                  "a cast to a type other than an integral or "
4195                  "enumeration type")))
4196           return error_mark_node;
4197
4198         switch (keyword)
4199           {
4200           case RID_DYNCAST:
4201             postfix_expression
4202               = build_dynamic_cast (type, expression);
4203             break;
4204           case RID_STATCAST:
4205             postfix_expression
4206               = build_static_cast (type, expression);
4207             break;
4208           case RID_REINTCAST:
4209             postfix_expression
4210               = build_reinterpret_cast (type, expression);
4211             break;
4212           case RID_CONSTCAST:
4213             postfix_expression
4214               = build_const_cast (type, expression);
4215             break;
4216           default:
4217             gcc_unreachable ();
4218           }
4219       }
4220       break;
4221
4222     case RID_TYPEID:
4223       {
4224         tree type;
4225         const char *saved_message;
4226         bool saved_in_type_id_in_expr_p;
4227
4228         /* Consume the `typeid' token.  */
4229         cp_lexer_consume_token (parser->lexer);
4230         /* Look for the `(' token.  */
4231         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4232         /* Types cannot be defined in a `typeid' expression.  */
4233         saved_message = parser->type_definition_forbidden_message;
4234         parser->type_definition_forbidden_message
4235           = "types may not be defined in a `typeid\' expression";
4236         /* We can't be sure yet whether we're looking at a type-id or an
4237            expression.  */
4238         cp_parser_parse_tentatively (parser);
4239         /* Try a type-id first.  */
4240         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4241         parser->in_type_id_in_expr_p = true;
4242         type = cp_parser_type_id (parser);
4243         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4244         /* Look for the `)' token.  Otherwise, we can't be sure that
4245            we're not looking at an expression: consider `typeid (int
4246            (3))', for example.  */
4247         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4248         /* If all went well, simply lookup the type-id.  */
4249         if (cp_parser_parse_definitely (parser))
4250           postfix_expression = get_typeid (type);
4251         /* Otherwise, fall back to the expression variant.  */
4252         else
4253           {
4254             tree expression;
4255
4256             /* Look for an expression.  */
4257             expression = cp_parser_expression (parser, /*cast_p=*/false);
4258             /* Compute its typeid.  */
4259             postfix_expression = build_typeid (expression);
4260             /* Look for the `)' token.  */
4261             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4262           }
4263         /* Restore the saved message.  */
4264         parser->type_definition_forbidden_message = saved_message;
4265         /* `typeid' may not appear in an integral constant expression.  */
4266         if (cp_parser_non_integral_constant_expression(parser,
4267                                                        "`typeid' operator"))
4268           return error_mark_node;
4269       }
4270       break;
4271
4272     case RID_TYPENAME:
4273       {
4274         tree type;
4275         /* The syntax permitted here is the same permitted for an
4276            elaborated-type-specifier.  */
4277         type = cp_parser_elaborated_type_specifier (parser,
4278                                                     /*is_friend=*/false,
4279                                                     /*is_declaration=*/false);
4280         postfix_expression = cp_parser_functional_cast (parser, type);
4281       }
4282       break;
4283
4284     default:
4285       {
4286         tree type;
4287
4288         /* If the next thing is a simple-type-specifier, we may be
4289            looking at a functional cast.  We could also be looking at
4290            an id-expression.  So, we try the functional cast, and if
4291            that doesn't work we fall back to the primary-expression.  */
4292         cp_parser_parse_tentatively (parser);
4293         /* Look for the simple-type-specifier.  */
4294         type = cp_parser_simple_type_specifier (parser,
4295                                                 /*decl_specs=*/NULL,
4296                                                 CP_PARSER_FLAGS_NONE);
4297         /* Parse the cast itself.  */
4298         if (!cp_parser_error_occurred (parser))
4299           postfix_expression
4300             = cp_parser_functional_cast (parser, type);
4301         /* If that worked, we're done.  */
4302         if (cp_parser_parse_definitely (parser))
4303           break;
4304
4305         /* If the functional-cast didn't work out, try a
4306            compound-literal.  */
4307         if (cp_parser_allow_gnu_extensions_p (parser)
4308             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4309           {
4310             VEC(constructor_elt,gc) *initializer_list = NULL;
4311             bool saved_in_type_id_in_expr_p;
4312
4313             cp_parser_parse_tentatively (parser);
4314             /* Consume the `('.  */
4315             cp_lexer_consume_token (parser->lexer);
4316             /* Parse the type.  */
4317             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4318             parser->in_type_id_in_expr_p = true;
4319             type = cp_parser_type_id (parser);
4320             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4321             /* Look for the `)'.  */
4322             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4323             /* Look for the `{'.  */
4324             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4325             /* If things aren't going well, there's no need to
4326                keep going.  */
4327             if (!cp_parser_error_occurred (parser))
4328               {
4329                 bool non_constant_p;
4330                 /* Parse the initializer-list.  */
4331                 initializer_list
4332                   = cp_parser_initializer_list (parser, &non_constant_p);
4333                 /* Allow a trailing `,'.  */
4334                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4335                   cp_lexer_consume_token (parser->lexer);
4336                 /* Look for the final `}'.  */
4337                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4338               }
4339             /* If that worked, we're definitely looking at a
4340                compound-literal expression.  */
4341             if (cp_parser_parse_definitely (parser))
4342               {
4343                 /* Warn the user that a compound literal is not
4344                    allowed in standard C++.  */
4345                 if (pedantic)
4346                   pedwarn ("ISO C++ forbids compound-literals");
4347                 /* For simplicity, we disallow compound literals in
4348                    constant-expressions.  We could
4349                    allow compound literals of integer type, whose
4350                    initializer was a constant, in constant
4351                    expressions.  Permitting that usage, as a further
4352                    extension, would not change the meaning of any
4353                    currently accepted programs.  (Of course, as
4354                    compound literals are not part of ISO C++, the
4355                    standard has nothing to say.)  */
4356                 if (cp_parser_non_integral_constant_expression 
4357                     (parser, "non-constant compound literals"))
4358                   {
4359                     postfix_expression = error_mark_node;
4360                     break;
4361                   }
4362                 /* Form the representation of the compound-literal.  */
4363                 postfix_expression
4364                   = finish_compound_literal (type, initializer_list);
4365                 break;
4366               }
4367           }
4368
4369         /* It must be a primary-expression.  */
4370         postfix_expression
4371           = cp_parser_primary_expression (parser, address_p, cast_p,
4372                                           /*template_arg_p=*/false,
4373                                           &idk);
4374       }
4375       break;
4376     }
4377
4378   /* Keep looping until the postfix-expression is complete.  */
4379   while (true)
4380     {
4381       if (idk == CP_ID_KIND_UNQUALIFIED
4382           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4383           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4384         /* It is not a Koenig lookup function call.  */
4385         postfix_expression
4386           = unqualified_name_lookup_error (postfix_expression);
4387
4388       /* Peek at the next token.  */
4389       token = cp_lexer_peek_token (parser->lexer);
4390
4391       switch (token->type)
4392         {
4393         case CPP_OPEN_SQUARE:
4394           postfix_expression
4395             = cp_parser_postfix_open_square_expression (parser,
4396                                                         postfix_expression,
4397                                                         false);
4398           idk = CP_ID_KIND_NONE;
4399           break;
4400
4401         case CPP_OPEN_PAREN:
4402           /* postfix-expression ( expression-list [opt] ) */
4403           {
4404             bool koenig_p;
4405             bool is_builtin_constant_p;
4406             bool saved_integral_constant_expression_p = false;
4407             bool saved_non_integral_constant_expression_p = false;
4408             tree args;
4409
4410             is_builtin_constant_p
4411               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4412             if (is_builtin_constant_p)
4413               {
4414                 /* The whole point of __builtin_constant_p is to allow
4415                    non-constant expressions to appear as arguments.  */
4416                 saved_integral_constant_expression_p
4417                   = parser->integral_constant_expression_p;
4418                 saved_non_integral_constant_expression_p
4419                   = parser->non_integral_constant_expression_p;
4420                 parser->integral_constant_expression_p = false;
4421               }
4422             args = (cp_parser_parenthesized_expression_list
4423                     (parser, /*is_attribute_list=*/false,
4424                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4425                      /*non_constant_p=*/NULL));
4426             if (is_builtin_constant_p)
4427               {
4428                 parser->integral_constant_expression_p
4429                   = saved_integral_constant_expression_p;
4430                 parser->non_integral_constant_expression_p
4431                   = saved_non_integral_constant_expression_p;
4432               }
4433
4434             if (args == error_mark_node)
4435               {
4436                 postfix_expression = error_mark_node;
4437                 break;
4438               }
4439
4440             /* Function calls are not permitted in
4441                constant-expressions.  */
4442             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4443                 && cp_parser_non_integral_constant_expression (parser,
4444                                                                "a function call"))
4445               {
4446                 postfix_expression = error_mark_node;
4447                 break;
4448               }
4449
4450             koenig_p = false;
4451             if (idk == CP_ID_KIND_UNQUALIFIED)
4452               {
4453                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4454                   {
4455                     if (args)
4456                       {
4457                         koenig_p = true;
4458                         postfix_expression
4459                           = perform_koenig_lookup (postfix_expression, args);
4460                       }
4461                     else
4462                       postfix_expression
4463                         = unqualified_fn_lookup_error (postfix_expression);
4464                   }
4465                 /* We do not perform argument-dependent lookup if
4466                    normal lookup finds a non-function, in accordance
4467                    with the expected resolution of DR 218.  */
4468                 else if (args && is_overloaded_fn (postfix_expression))
4469                   {
4470                     tree fn = get_first_fn (postfix_expression);
4471
4472                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4473                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4474
4475                     /* Only do argument dependent lookup if regular
4476                        lookup does not find a set of member functions.
4477                        [basic.lookup.koenig]/2a  */
4478                     if (!DECL_FUNCTION_MEMBER_P (fn))
4479                       {
4480                         koenig_p = true;
4481                         postfix_expression
4482                           = perform_koenig_lookup (postfix_expression, args);
4483                       }
4484                   }
4485               }
4486
4487             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4488               {
4489                 tree instance = TREE_OPERAND (postfix_expression, 0);
4490                 tree fn = TREE_OPERAND (postfix_expression, 1);
4491
4492                 if (processing_template_decl
4493                     && (type_dependent_expression_p (instance)
4494                         || (!BASELINK_P (fn)
4495                             && TREE_CODE (fn) != FIELD_DECL)
4496                         || type_dependent_expression_p (fn)
4497                         || any_type_dependent_arguments_p (args)))
4498                   {
4499                     postfix_expression
4500                       = build_nt_call_list (postfix_expression, args);
4501                     break;
4502                   }
4503
4504                 if (BASELINK_P (fn))
4505                   postfix_expression
4506                     = (build_new_method_call
4507                        (instance, fn, args, NULL_TREE,
4508                         (idk == CP_ID_KIND_QUALIFIED
4509                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4510                         /*fn_p=*/NULL));
4511                 else
4512                   postfix_expression
4513                     = finish_call_expr (postfix_expression, args,
4514                                         /*disallow_virtual=*/false,
4515                                         /*koenig_p=*/false);
4516               }
4517             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4518                      || TREE_CODE (postfix_expression) == MEMBER_REF
4519                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4520               postfix_expression = (build_offset_ref_call_from_tree
4521                                     (postfix_expression, args));
4522             else if (idk == CP_ID_KIND_QUALIFIED)
4523               /* A call to a static class member, or a namespace-scope
4524                  function.  */
4525               postfix_expression
4526                 = finish_call_expr (postfix_expression, args,
4527                                     /*disallow_virtual=*/true,
4528                                     koenig_p);
4529             else
4530               /* All other function calls.  */
4531               postfix_expression
4532                 = finish_call_expr (postfix_expression, args,
4533                                     /*disallow_virtual=*/false,
4534                                     koenig_p);
4535
4536             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4537             idk = CP_ID_KIND_NONE;
4538           }
4539           break;
4540
4541         case CPP_DOT:
4542         case CPP_DEREF:
4543           /* postfix-expression . template [opt] id-expression
4544              postfix-expression . pseudo-destructor-name
4545              postfix-expression -> template [opt] id-expression
4546              postfix-expression -> pseudo-destructor-name */
4547
4548           /* Consume the `.' or `->' operator.  */
4549           cp_lexer_consume_token (parser->lexer);
4550
4551           postfix_expression
4552             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4553                                                       postfix_expression,
4554                                                       false, &idk);
4555           break;
4556
4557         case CPP_PLUS_PLUS:
4558           /* postfix-expression ++  */
4559           /* Consume the `++' token.  */
4560           cp_lexer_consume_token (parser->lexer);
4561           /* Generate a representation for the complete expression.  */
4562           postfix_expression
4563             = finish_increment_expr (postfix_expression,
4564                                      POSTINCREMENT_EXPR);
4565           /* Increments may not appear in constant-expressions.  */
4566           if (cp_parser_non_integral_constant_expression (parser,
4567                                                           "an increment"))
4568             postfix_expression = error_mark_node;
4569           idk = CP_ID_KIND_NONE;
4570           break;
4571
4572         case CPP_MINUS_MINUS:
4573           /* postfix-expression -- */
4574           /* Consume the `--' token.  */
4575           cp_lexer_consume_token (parser->lexer);
4576           /* Generate a representation for the complete expression.  */
4577           postfix_expression
4578             = finish_increment_expr (postfix_expression,
4579                                      POSTDECREMENT_EXPR);
4580           /* Decrements may not appear in constant-expressions.  */
4581           if (cp_parser_non_integral_constant_expression (parser,
4582                                                           "a decrement"))
4583             postfix_expression = error_mark_node;
4584           idk = CP_ID_KIND_NONE;
4585           break;
4586
4587         default:
4588           return postfix_expression;
4589         }
4590     }
4591
4592   /* We should never get here.  */
4593   gcc_unreachable ();
4594   return error_mark_node;
4595 }
4596
4597 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4598    by cp_parser_builtin_offsetof.  We're looking for
4599
4600      postfix-expression [ expression ]
4601
4602    FOR_OFFSETOF is set if we're being called in that context, which
4603    changes how we deal with integer constant expressions.  */
4604
4605 static tree
4606 cp_parser_postfix_open_square_expression (cp_parser *parser,
4607                                           tree postfix_expression,
4608                                           bool for_offsetof)
4609 {
4610   tree index;
4611
4612   /* Consume the `[' token.  */
4613   cp_lexer_consume_token (parser->lexer);
4614
4615   /* Parse the index expression.  */
4616   /* ??? For offsetof, there is a question of what to allow here.  If
4617      offsetof is not being used in an integral constant expression context,
4618      then we *could* get the right answer by computing the value at runtime.
4619      If we are in an integral constant expression context, then we might
4620      could accept any constant expression; hard to say without analysis.
4621      Rather than open the barn door too wide right away, allow only integer
4622      constant expressions here.  */
4623   if (for_offsetof)
4624     index = cp_parser_constant_expression (parser, false, NULL);
4625   else
4626     index = cp_parser_expression (parser, /*cast_p=*/false);
4627
4628   /* Look for the closing `]'.  */
4629   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4630
4631   /* Build the ARRAY_REF.  */
4632   postfix_expression = grok_array_decl (postfix_expression, index);
4633
4634   /* When not doing offsetof, array references are not permitted in
4635      constant-expressions.  */
4636   if (!for_offsetof
4637       && (cp_parser_non_integral_constant_expression
4638           (parser, "an array reference")))
4639     postfix_expression = error_mark_node;
4640
4641   return postfix_expression;
4642 }
4643
4644 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4645    by cp_parser_builtin_offsetof.  We're looking for
4646
4647      postfix-expression . template [opt] id-expression
4648      postfix-expression . pseudo-destructor-name
4649      postfix-expression -> template [opt] id-expression
4650      postfix-expression -> pseudo-destructor-name
4651
4652    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4653    limits what of the above we'll actually accept, but nevermind.
4654    TOKEN_TYPE is the "." or "->" token, which will already have been
4655    removed from the stream.  */
4656
4657 static tree
4658 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4659                                         enum cpp_ttype token_type,
4660                                         tree postfix_expression,
4661                                         bool for_offsetof, cp_id_kind *idk)
4662 {
4663   tree name;
4664   bool dependent_p;
4665   bool pseudo_destructor_p;
4666   tree scope = NULL_TREE;
4667
4668   /* If this is a `->' operator, dereference the pointer.  */
4669   if (token_type == CPP_DEREF)
4670     postfix_expression = build_x_arrow (postfix_expression);
4671   /* Check to see whether or not the expression is type-dependent.  */
4672   dependent_p = type_dependent_expression_p (postfix_expression);
4673   /* The identifier following the `->' or `.' is not qualified.  */
4674   parser->scope = NULL_TREE;
4675   parser->qualifying_scope = NULL_TREE;
4676   parser->object_scope = NULL_TREE;
4677   *idk = CP_ID_KIND_NONE;
4678   /* Enter the scope corresponding to the type of the object
4679      given by the POSTFIX_EXPRESSION.  */
4680   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4681     {
4682       scope = TREE_TYPE (postfix_expression);
4683       /* According to the standard, no expression should ever have
4684          reference type.  Unfortunately, we do not currently match
4685          the standard in this respect in that our internal representation
4686          of an expression may have reference type even when the standard
4687          says it does not.  Therefore, we have to manually obtain the
4688          underlying type here.  */
4689       scope = non_reference (scope);
4690       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4691       if (scope == unknown_type_node)
4692         {
4693           error ("%qE does not have class type", postfix_expression);
4694           scope = NULL_TREE;
4695         }
4696       else
4697         scope = complete_type_or_else (scope, NULL_TREE);
4698       /* Let the name lookup machinery know that we are processing a
4699          class member access expression.  */
4700       parser->context->object_type = scope;
4701       /* If something went wrong, we want to be able to discern that case,
4702          as opposed to the case where there was no SCOPE due to the type
4703          of expression being dependent.  */
4704       if (!scope)
4705         scope = error_mark_node;
4706       /* If the SCOPE was erroneous, make the various semantic analysis
4707          functions exit quickly -- and without issuing additional error
4708          messages.  */
4709       if (scope == error_mark_node)
4710         postfix_expression = error_mark_node;
4711     }
4712
4713   /* Assume this expression is not a pseudo-destructor access.  */
4714   pseudo_destructor_p = false;
4715
4716   /* If the SCOPE is a scalar type, then, if this is a valid program,
4717      we must be looking at a pseudo-destructor-name.  */
4718   if (scope && SCALAR_TYPE_P (scope))
4719     {
4720       tree s;
4721       tree type;
4722
4723       cp_parser_parse_tentatively (parser);
4724       /* Parse the pseudo-destructor-name.  */
4725       s = NULL_TREE;
4726       cp_parser_pseudo_destructor_name (parser, &s, &type);
4727       if (cp_parser_parse_definitely (parser))
4728         {
4729           pseudo_destructor_p = true;
4730           postfix_expression
4731             = finish_pseudo_destructor_expr (postfix_expression,
4732                                              s, TREE_TYPE (type));
4733         }
4734     }
4735
4736   if (!pseudo_destructor_p)
4737     {
4738       /* If the SCOPE is not a scalar type, we are looking at an
4739          ordinary class member access expression, rather than a
4740          pseudo-destructor-name.  */
4741       bool template_p;
4742       /* Parse the id-expression.  */
4743       name = (cp_parser_id_expression
4744               (parser,
4745                cp_parser_optional_template_keyword (parser),
4746                /*check_dependency_p=*/true,
4747                &template_p,
4748                /*declarator_p=*/false,
4749                /*optional_p=*/false));
4750       /* In general, build a SCOPE_REF if the member name is qualified.
4751          However, if the name was not dependent and has already been
4752          resolved; there is no need to build the SCOPE_REF.  For example;
4753
4754              struct X { void f(); };
4755              template <typename T> void f(T* t) { t->X::f(); }
4756
4757          Even though "t" is dependent, "X::f" is not and has been resolved
4758          to a BASELINK; there is no need to include scope information.  */
4759
4760       /* But we do need to remember that there was an explicit scope for
4761          virtual function calls.  */
4762       if (parser->scope)
4763         *idk = CP_ID_KIND_QUALIFIED;
4764
4765       /* If the name is a template-id that names a type, we will get a
4766          TYPE_DECL here.  That is invalid code.  */
4767       if (TREE_CODE (name) == TYPE_DECL)
4768         {
4769           error ("invalid use of %qD", name);
4770           postfix_expression = error_mark_node;
4771         }
4772       else
4773         {
4774           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4775             {
4776               name = build_qualified_name (/*type=*/NULL_TREE,
4777                                            parser->scope,
4778                                            name,
4779                                            template_p);
4780               parser->scope = NULL_TREE;
4781               parser->qualifying_scope = NULL_TREE;
4782               parser->object_scope = NULL_TREE;
4783             }
4784           if (scope && name && BASELINK_P (name))
4785             adjust_result_of_qualified_name_lookup
4786               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4787           postfix_expression
4788             = finish_class_member_access_expr (postfix_expression, name,
4789                                                template_p);
4790         }
4791     }
4792
4793   /* We no longer need to look up names in the scope of the object on
4794      the left-hand side of the `.' or `->' operator.  */
4795   parser->context->object_type = NULL_TREE;
4796
4797   /* Outside of offsetof, these operators may not appear in
4798      constant-expressions.  */
4799   if (!for_offsetof
4800       && (cp_parser_non_integral_constant_expression
4801           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4802     postfix_expression = error_mark_node;
4803
4804   return postfix_expression;
4805 }
4806
4807 /* Parse a parenthesized expression-list.
4808
4809    expression-list:
4810      assignment-expression
4811      expression-list, assignment-expression
4812
4813    attribute-list:
4814      expression-list
4815      identifier
4816      identifier, expression-list
4817
4818    CAST_P is true if this expression is the target of a cast.
4819
4820    ALLOW_EXPANSION_P is true if this expression allows expansion of an
4821    argument pack.
4822
4823    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4824    representation of an assignment-expression.  Note that a TREE_LIST
4825    is returned even if there is only a single expression in the list.
4826    error_mark_node is returned if the ( and or ) are
4827    missing. NULL_TREE is returned on no expressions. The parentheses
4828    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4829    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4830    indicates whether or not all of the expressions in the list were
4831    constant.  */
4832
4833 static tree
4834 cp_parser_parenthesized_expression_list (cp_parser* parser,
4835                                          bool is_attribute_list,
4836                                          bool cast_p,
4837                                          bool allow_expansion_p,
4838                                          bool *non_constant_p)
4839 {
4840   tree expression_list = NULL_TREE;
4841   bool fold_expr_p = is_attribute_list;
4842   tree identifier = NULL_TREE;
4843
4844   /* Assume all the expressions will be constant.  */
4845   if (non_constant_p)
4846     *non_constant_p = false;
4847
4848   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4849     return error_mark_node;
4850
4851   /* Consume expressions until there are no more.  */
4852   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4853     while (true)
4854       {
4855         tree expr;
4856
4857         /* At the beginning of attribute lists, check to see if the
4858            next token is an identifier.  */
4859         if (is_attribute_list
4860             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4861           {
4862             cp_token *token;
4863
4864             /* Consume the identifier.  */
4865             token = cp_lexer_consume_token (parser->lexer);
4866             /* Save the identifier.  */
4867             identifier = token->u.value;
4868           }
4869         else
4870           {
4871             /* Parse the next assignment-expression.  */
4872             if (non_constant_p)
4873               {
4874                 bool expr_non_constant_p;
4875                 expr = (cp_parser_constant_expression
4876                         (parser, /*allow_non_constant_p=*/true,
4877                          &expr_non_constant_p));
4878                 if (expr_non_constant_p)
4879                   *non_constant_p = true;
4880               }
4881             else
4882               expr = cp_parser_assignment_expression (parser, cast_p);
4883
4884             if (fold_expr_p)
4885               expr = fold_non_dependent_expr (expr);
4886
4887             /* If we have an ellipsis, then this is an expression
4888                expansion.  */
4889             if (allow_expansion_p
4890                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4891               {
4892                 /* Consume the `...'.  */
4893                 cp_lexer_consume_token (parser->lexer);
4894
4895                 /* Build the argument pack.  */
4896                 expr = make_pack_expansion (expr);
4897               }
4898
4899              /* Add it to the list.  We add error_mark_node
4900                 expressions to the list, so that we can still tell if
4901                 the correct form for a parenthesized expression-list
4902                 is found. That gives better errors.  */
4903             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4904
4905             if (expr == error_mark_node)
4906               goto skip_comma;
4907           }
4908
4909         /* After the first item, attribute lists look the same as
4910            expression lists.  */
4911         is_attribute_list = false;
4912
4913       get_comma:;
4914         /* If the next token isn't a `,', then we are done.  */
4915         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4916           break;
4917
4918         /* Otherwise, consume the `,' and keep going.  */
4919         cp_lexer_consume_token (parser->lexer);
4920       }
4921
4922   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4923     {
4924       int ending;
4925
4926     skip_comma:;
4927       /* We try and resync to an unnested comma, as that will give the
4928          user better diagnostics.  */
4929       ending = cp_parser_skip_to_closing_parenthesis (parser,
4930                                                       /*recovering=*/true,
4931                                                       /*or_comma=*/true,
4932                                                       /*consume_paren=*/true);
4933       if (ending < 0)
4934         goto get_comma;
4935       if (!ending)
4936         return error_mark_node;
4937     }
4938
4939   /* We built up the list in reverse order so we must reverse it now.  */
4940   expression_list = nreverse (expression_list);
4941   if (identifier)
4942     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4943
4944   return expression_list;
4945 }
4946
4947 /* Parse a pseudo-destructor-name.
4948
4949    pseudo-destructor-name:
4950      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4951      :: [opt] nested-name-specifier template template-id :: ~ type-name
4952      :: [opt] nested-name-specifier [opt] ~ type-name
4953
4954    If either of the first two productions is used, sets *SCOPE to the
4955    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4956    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4957    or ERROR_MARK_NODE if the parse fails.  */
4958
4959 static void
4960 cp_parser_pseudo_destructor_name (cp_parser* parser,
4961                                   tree* scope,
4962                                   tree* type)
4963 {
4964   bool nested_name_specifier_p;
4965
4966   /* Assume that things will not work out.  */
4967   *type = error_mark_node;
4968
4969   /* Look for the optional `::' operator.  */
4970   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4971   /* Look for the optional nested-name-specifier.  */
4972   nested_name_specifier_p
4973     = (cp_parser_nested_name_specifier_opt (parser,
4974                                             /*typename_keyword_p=*/false,
4975                                             /*check_dependency_p=*/true,
4976                                             /*type_p=*/false,
4977                                             /*is_declaration=*/true)
4978        != NULL_TREE);
4979   /* Now, if we saw a nested-name-specifier, we might be doing the
4980      second production.  */
4981   if (nested_name_specifier_p
4982       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4983     {
4984       /* Consume the `template' keyword.  */
4985       cp_lexer_consume_token (parser->lexer);
4986       /* Parse the template-id.  */
4987       cp_parser_template_id (parser,
4988                              /*template_keyword_p=*/true,
4989                              /*check_dependency_p=*/false,
4990                              /*is_declaration=*/true);
4991       /* Look for the `::' token.  */
4992       cp_parser_require (parser, CPP_SCOPE, "`::'");
4993     }
4994   /* If the next token is not a `~', then there might be some
4995      additional qualification.  */
4996   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4997     {
4998       /* Look for the type-name.  */
4999       *scope = TREE_TYPE (cp_parser_type_name (parser));
5000
5001       if (*scope == error_mark_node)
5002         return;
5003
5004       /* If we don't have ::~, then something has gone wrong.  Since
5005          the only caller of this function is looking for something
5006          after `.' or `->' after a scalar type, most likely the
5007          program is trying to get a member of a non-aggregate
5008          type.  */
5009       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5010           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5011         {
5012           cp_parser_error (parser, "request for member of non-aggregate type");
5013           return;
5014         }
5015
5016       /* Look for the `::' token.  */
5017       cp_parser_require (parser, CPP_SCOPE, "`::'");
5018     }
5019   else
5020     *scope = NULL_TREE;
5021
5022   /* Look for the `~'.  */
5023   cp_parser_require (parser, CPP_COMPL, "`~'");
5024   /* Look for the type-name again.  We are not responsible for
5025      checking that it matches the first type-name.  */
5026   *type = cp_parser_type_name (parser);
5027 }
5028
5029 /* Parse a unary-expression.
5030
5031    unary-expression:
5032      postfix-expression
5033      ++ cast-expression
5034      -- cast-expression
5035      unary-operator cast-expression
5036      sizeof unary-expression
5037      sizeof ( type-id )
5038      new-expression
5039      delete-expression
5040
5041    GNU Extensions:
5042
5043    unary-expression:
5044      __extension__ cast-expression
5045      __alignof__ unary-expression
5046      __alignof__ ( type-id )
5047      __real__ cast-expression
5048      __imag__ cast-expression
5049      && identifier
5050
5051    ADDRESS_P is true iff the unary-expression is appearing as the
5052    operand of the `&' operator.   CAST_P is true if this expression is
5053    the target of a cast.
5054
5055    Returns a representation of the expression.  */
5056
5057 static tree
5058 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5059 {
5060   cp_token *token;
5061   enum tree_code unary_operator;
5062
5063   /* Peek at the next token.  */
5064   token = cp_lexer_peek_token (parser->lexer);
5065   /* Some keywords give away the kind of expression.  */
5066   if (token->type == CPP_KEYWORD)
5067     {
5068       enum rid keyword = token->keyword;
5069
5070       switch (keyword)
5071         {
5072         case RID_ALIGNOF:
5073         case RID_SIZEOF:
5074           {
5075             tree operand;
5076             enum tree_code op;
5077
5078             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5079             /* Consume the token.  */
5080             cp_lexer_consume_token (parser->lexer);
5081             /* Parse the operand.  */
5082             operand = cp_parser_sizeof_operand (parser, keyword);
5083
5084             if (TYPE_P (operand))
5085               return cxx_sizeof_or_alignof_type (operand, op, true);
5086             else
5087               return cxx_sizeof_or_alignof_expr (operand, op);
5088           }
5089
5090         case RID_NEW:
5091           return cp_parser_new_expression (parser);
5092
5093         case RID_DELETE:
5094           return cp_parser_delete_expression (parser);
5095
5096         case RID_EXTENSION:
5097           {
5098             /* The saved value of the PEDANTIC flag.  */
5099             int saved_pedantic;
5100             tree expr;
5101
5102             /* Save away the PEDANTIC flag.  */
5103             cp_parser_extension_opt (parser, &saved_pedantic);
5104             /* Parse the cast-expression.  */
5105             expr = cp_parser_simple_cast_expression (parser);
5106             /* Restore the PEDANTIC flag.  */
5107             pedantic = saved_pedantic;
5108
5109             return expr;
5110           }
5111
5112         case RID_REALPART:
5113         case RID_IMAGPART:
5114           {
5115             tree expression;
5116
5117             /* Consume the `__real__' or `__imag__' token.  */
5118             cp_lexer_consume_token (parser->lexer);
5119             /* Parse the cast-expression.  */
5120             expression = cp_parser_simple_cast_expression (parser);
5121             /* Create the complete representation.  */
5122             return build_x_unary_op ((keyword == RID_REALPART
5123                                       ? REALPART_EXPR : IMAGPART_EXPR),
5124                                      expression);
5125           }
5126           break;
5127
5128         default:
5129           break;
5130         }
5131     }
5132
5133   /* Look for the `:: new' and `:: delete', which also signal the
5134      beginning of a new-expression, or delete-expression,
5135      respectively.  If the next token is `::', then it might be one of
5136      these.  */
5137   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5138     {
5139       enum rid keyword;
5140
5141       /* See if the token after the `::' is one of the keywords in
5142          which we're interested.  */
5143       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5144       /* If it's `new', we have a new-expression.  */
5145       if (keyword == RID_NEW)
5146         return cp_parser_new_expression (parser);
5147       /* Similarly, for `delete'.  */
5148       else if (keyword == RID_DELETE)
5149         return cp_parser_delete_expression (parser);
5150     }
5151
5152   /* Look for a unary operator.  */
5153   unary_operator = cp_parser_unary_operator (token);
5154   /* The `++' and `--' operators can be handled similarly, even though
5155      they are not technically unary-operators in the grammar.  */
5156   if (unary_operator == ERROR_MARK)
5157     {
5158       if (token->type == CPP_PLUS_PLUS)
5159         unary_operator = PREINCREMENT_EXPR;
5160       else if (token->type == CPP_MINUS_MINUS)
5161         unary_operator = PREDECREMENT_EXPR;
5162       /* Handle the GNU address-of-label extension.  */
5163       else if (cp_parser_allow_gnu_extensions_p (parser)
5164                && token->type == CPP_AND_AND)
5165         {
5166           tree identifier;
5167
5168           /* Consume the '&&' token.  */
5169           cp_lexer_consume_token (parser->lexer);
5170           /* Look for the identifier.  */
5171           identifier = cp_parser_identifier (parser);
5172           /* Create an expression representing the address.  */
5173           return finish_label_address_expr (identifier);
5174         }
5175     }
5176   if (unary_operator != ERROR_MARK)
5177     {
5178       tree cast_expression;
5179       tree expression = error_mark_node;
5180       const char *non_constant_p = NULL;
5181
5182       /* Consume the operator token.  */
5183       token = cp_lexer_consume_token (parser->lexer);
5184       /* Parse the cast-expression.  */
5185       cast_expression
5186         = cp_parser_cast_expression (parser,
5187                                      unary_operator == ADDR_EXPR,
5188                                      /*cast_p=*/false);
5189       /* Now, build an appropriate representation.  */
5190       switch (unary_operator)
5191         {
5192         case INDIRECT_REF:
5193           non_constant_p = "`*'";
5194           expression = build_x_indirect_ref (cast_expression, "unary *");
5195           break;
5196
5197         case ADDR_EXPR:
5198           non_constant_p = "`&'";
5199           /* Fall through.  */
5200         case BIT_NOT_EXPR:
5201           expression = build_x_unary_op (unary_operator, cast_expression);
5202           break;
5203
5204         case PREINCREMENT_EXPR:
5205         case PREDECREMENT_EXPR:
5206           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5207                             ? "`++'" : "`--'");
5208           /* Fall through.  */
5209         case UNARY_PLUS_EXPR:
5210         case NEGATE_EXPR:
5211         case TRUTH_NOT_EXPR:
5212           expression = finish_unary_op_expr (unary_operator, cast_expression);
5213           break;
5214
5215         default:
5216           gcc_unreachable ();
5217         }
5218
5219       if (non_constant_p
5220           && cp_parser_non_integral_constant_expression (parser,
5221                                                          non_constant_p))
5222         expression = error_mark_node;
5223
5224       return expression;
5225     }
5226
5227   return cp_parser_postfix_expression (parser, address_p, cast_p);
5228 }
5229
5230 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5231    unary-operator, the corresponding tree code is returned.  */
5232
5233 static enum tree_code
5234 cp_parser_unary_operator (cp_token* token)
5235 {
5236   switch (token->type)
5237     {
5238     case CPP_MULT:
5239       return INDIRECT_REF;
5240
5241     case CPP_AND:
5242       return ADDR_EXPR;
5243
5244     case CPP_PLUS:
5245       return UNARY_PLUS_EXPR;
5246
5247     case CPP_MINUS:
5248       return NEGATE_EXPR;
5249
5250     case CPP_NOT:
5251       return TRUTH_NOT_EXPR;
5252
5253     case CPP_COMPL:
5254       return BIT_NOT_EXPR;
5255
5256     default:
5257       return ERROR_MARK;
5258     }
5259 }
5260
5261 /* Parse a new-expression.
5262
5263    new-expression:
5264      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5265      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5266
5267    Returns a representation of the expression.  */
5268
5269 static tree
5270 cp_parser_new_expression (cp_parser* parser)
5271 {
5272   bool global_scope_p;
5273   tree placement;
5274   tree type;
5275   tree initializer;
5276   tree nelts;
5277
5278   /* Look for the optional `::' operator.  */
5279   global_scope_p
5280     = (cp_parser_global_scope_opt (parser,
5281                                    /*current_scope_valid_p=*/false)
5282        != NULL_TREE);
5283   /* Look for the `new' operator.  */
5284   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5285   /* There's no easy way to tell a new-placement from the
5286      `( type-id )' construct.  */
5287   cp_parser_parse_tentatively (parser);
5288   /* Look for a new-placement.  */
5289   placement = cp_parser_new_placement (parser);
5290   /* If that didn't work out, there's no new-placement.  */
5291   if (!cp_parser_parse_definitely (parser))
5292     placement = NULL_TREE;
5293
5294   /* If the next token is a `(', then we have a parenthesized
5295      type-id.  */
5296   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5297     {
5298       /* Consume the `('.  */
5299       cp_lexer_consume_token (parser->lexer);
5300       /* Parse the type-id.  */
5301       type = cp_parser_type_id (parser);
5302       /* Look for the closing `)'.  */
5303       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5304       /* There should not be a direct-new-declarator in this production,
5305          but GCC used to allowed this, so we check and emit a sensible error
5306          message for this case.  */
5307       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5308         {
5309           error ("array bound forbidden after parenthesized type-id");
5310           inform ("try removing the parentheses around the type-id");
5311           cp_parser_direct_new_declarator (parser);
5312         }
5313       nelts = NULL_TREE;
5314     }
5315   /* Otherwise, there must be a new-type-id.  */
5316   else
5317     type = cp_parser_new_type_id (parser, &nelts);
5318
5319   /* If the next token is a `(', then we have a new-initializer.  */
5320   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5321     initializer = cp_parser_new_initializer (parser);
5322   else
5323     initializer = NULL_TREE;
5324
5325   /* A new-expression may not appear in an integral constant
5326      expression.  */
5327   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5328     return error_mark_node;
5329
5330   /* Create a representation of the new-expression.  */
5331   return build_new (placement, type, nelts, initializer, global_scope_p);
5332 }
5333
5334 /* Parse a new-placement.
5335
5336    new-placement:
5337      ( expression-list )
5338
5339    Returns the same representation as for an expression-list.  */
5340
5341 static tree
5342 cp_parser_new_placement (cp_parser* parser)
5343 {
5344   tree expression_list;
5345
5346   /* Parse the expression-list.  */
5347   expression_list = (cp_parser_parenthesized_expression_list
5348                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5349                       /*non_constant_p=*/NULL));
5350
5351   return expression_list;
5352 }
5353
5354 /* Parse a new-type-id.
5355
5356    new-type-id:
5357      type-specifier-seq new-declarator [opt]
5358
5359    Returns the TYPE allocated.  If the new-type-id indicates an array
5360    type, *NELTS is set to the number of elements in the last array
5361    bound; the TYPE will not include the last array bound.  */
5362
5363 static tree
5364 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5365 {
5366   cp_decl_specifier_seq type_specifier_seq;
5367   cp_declarator *new_declarator;
5368   cp_declarator *declarator;
5369   cp_declarator *outer_declarator;
5370   const char *saved_message;
5371   tree type;
5372
5373   /* The type-specifier sequence must not contain type definitions.
5374      (It cannot contain declarations of new types either, but if they
5375      are not definitions we will catch that because they are not
5376      complete.)  */
5377   saved_message = parser->type_definition_forbidden_message;
5378   parser->type_definition_forbidden_message
5379     = "types may not be defined in a new-type-id";
5380   /* Parse the type-specifier-seq.  */
5381   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5382                                 &type_specifier_seq);
5383   /* Restore the old message.  */
5384   parser->type_definition_forbidden_message = saved_message;
5385   /* Parse the new-declarator.  */
5386   new_declarator = cp_parser_new_declarator_opt (parser);
5387
5388   /* Determine the number of elements in the last array dimension, if
5389      any.  */
5390   *nelts = NULL_TREE;
5391   /* Skip down to the last array dimension.  */
5392   declarator = new_declarator;
5393   outer_declarator = NULL;
5394   while (declarator && (declarator->kind == cdk_pointer
5395                         || declarator->kind == cdk_ptrmem))
5396     {
5397       outer_declarator = declarator;
5398       declarator = declarator->declarator;
5399     }
5400   while (declarator
5401          && declarator->kind == cdk_array
5402          && declarator->declarator
5403          && declarator->declarator->kind == cdk_array)
5404     {
5405       outer_declarator = declarator;
5406       declarator = declarator->declarator;
5407     }
5408
5409   if (declarator && declarator->kind == cdk_array)
5410     {
5411       *nelts = declarator->u.array.bounds;
5412       if (*nelts == error_mark_node)
5413         *nelts = integer_one_node;
5414
5415       if (outer_declarator)
5416         outer_declarator->declarator = declarator->declarator;
5417       else
5418         new_declarator = NULL;
5419     }
5420
5421   type = groktypename (&type_specifier_seq, new_declarator);
5422   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5423     {
5424       *nelts = array_type_nelts_top (type);
5425       type = TREE_TYPE (type);
5426     }
5427   return type;
5428 }
5429
5430 /* Parse an (optional) new-declarator.
5431
5432    new-declarator:
5433      ptr-operator new-declarator [opt]
5434      direct-new-declarator
5435
5436    Returns the declarator.  */
5437
5438 static cp_declarator *
5439 cp_parser_new_declarator_opt (cp_parser* parser)
5440 {
5441   enum tree_code code;
5442   tree type;
5443   cp_cv_quals cv_quals;
5444
5445   /* We don't know if there's a ptr-operator next, or not.  */
5446   cp_parser_parse_tentatively (parser);
5447   /* Look for a ptr-operator.  */
5448   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5449   /* If that worked, look for more new-declarators.  */
5450   if (cp_parser_parse_definitely (parser))
5451     {
5452       cp_declarator *declarator;
5453
5454       /* Parse another optional declarator.  */
5455       declarator = cp_parser_new_declarator_opt (parser);
5456
5457       /* Create the representation of the declarator.  */
5458       if (type)
5459         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5460       else if (code == INDIRECT_REF)
5461         declarator = make_pointer_declarator (cv_quals, declarator);
5462       else
5463         declarator = make_reference_declarator (cv_quals, declarator);
5464
5465       return declarator;
5466     }
5467
5468   /* If the next token is a `[', there is a direct-new-declarator.  */
5469   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5470     return cp_parser_direct_new_declarator (parser);
5471
5472   return NULL;
5473 }
5474
5475 /* Parse a direct-new-declarator.
5476
5477    direct-new-declarator:
5478      [ expression ]
5479      direct-new-declarator [constant-expression]
5480
5481    */
5482
5483 static cp_declarator *
5484 cp_parser_direct_new_declarator (cp_parser* parser)
5485 {
5486   cp_declarator *declarator = NULL;
5487
5488   while (true)
5489     {
5490       tree expression;
5491
5492       /* Look for the opening `['.  */
5493       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5494       /* The first expression is not required to be constant.  */
5495       if (!declarator)
5496         {
5497           expression = cp_parser_expression (parser, /*cast_p=*/false);
5498           /* The standard requires that the expression have integral
5499              type.  DR 74 adds enumeration types.  We believe that the
5500              real intent is that these expressions be handled like the
5501              expression in a `switch' condition, which also allows
5502              classes with a single conversion to integral or
5503              enumeration type.  */
5504           if (!processing_template_decl)
5505             {
5506               expression
5507                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5508                                               expression,
5509                                               /*complain=*/true);
5510               if (!expression)
5511                 {
5512                   error ("expression in new-declarator must have integral "
5513                          "or enumeration type");
5514                   expression = error_mark_node;
5515                 }
5516             }
5517         }
5518       /* But all the other expressions must be.  */
5519       else
5520         expression
5521           = cp_parser_constant_expression (parser,
5522                                            /*allow_non_constant=*/false,
5523                                            NULL);
5524       /* Look for the closing `]'.  */
5525       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5526
5527       /* Add this bound to the declarator.  */
5528       declarator = make_array_declarator (declarator, expression);
5529
5530       /* If the next token is not a `[', then there are no more
5531          bounds.  */
5532       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5533         break;
5534     }
5535
5536   return declarator;
5537 }
5538
5539 /* Parse a new-initializer.
5540
5541    new-initializer:
5542      ( expression-list [opt] )
5543
5544    Returns a representation of the expression-list.  If there is no
5545    expression-list, VOID_ZERO_NODE is returned.  */
5546
5547 static tree
5548 cp_parser_new_initializer (cp_parser* parser)
5549 {
5550   tree expression_list;
5551
5552   expression_list = (cp_parser_parenthesized_expression_list
5553                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5554                       /*non_constant_p=*/NULL));
5555   if (!expression_list)
5556     expression_list = void_zero_node;
5557
5558   return expression_list;
5559 }
5560
5561 /* Parse a delete-expression.
5562
5563    delete-expression:
5564      :: [opt] delete cast-expression
5565      :: [opt] delete [ ] cast-expression
5566
5567    Returns a representation of the expression.  */
5568
5569 static tree
5570 cp_parser_delete_expression (cp_parser* parser)
5571 {
5572   bool global_scope_p;
5573   bool array_p;
5574   tree expression;
5575
5576   /* Look for the optional `::' operator.  */
5577   global_scope_p
5578     = (cp_parser_global_scope_opt (parser,
5579                                    /*current_scope_valid_p=*/false)
5580        != NULL_TREE);
5581   /* Look for the `delete' keyword.  */
5582   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5583   /* See if the array syntax is in use.  */
5584   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5585     {
5586       /* Consume the `[' token.  */
5587       cp_lexer_consume_token (parser->lexer);
5588       /* Look for the `]' token.  */
5589       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5590       /* Remember that this is the `[]' construct.  */
5591       array_p = true;
5592     }
5593   else
5594     array_p = false;
5595
5596   /* Parse the cast-expression.  */
5597   expression = cp_parser_simple_cast_expression (parser);
5598
5599   /* A delete-expression may not appear in an integral constant
5600      expression.  */
5601   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5602     return error_mark_node;
5603
5604   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5605 }
5606
5607 /* Parse a cast-expression.
5608
5609    cast-expression:
5610      unary-expression
5611      ( type-id ) cast-expression
5612
5613    ADDRESS_P is true iff the unary-expression is appearing as the
5614    operand of the `&' operator.   CAST_P is true if this expression is
5615    the target of a cast.
5616
5617    Returns a representation of the expression.  */
5618
5619 static tree
5620 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5621 {
5622   /* If it's a `(', then we might be looking at a cast.  */
5623   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5624     {
5625       tree type = NULL_TREE;
5626       tree expr = NULL_TREE;
5627       bool compound_literal_p;
5628       const char *saved_message;
5629
5630       /* There's no way to know yet whether or not this is a cast.
5631          For example, `(int (3))' is a unary-expression, while `(int)
5632          3' is a cast.  So, we resort to parsing tentatively.  */
5633       cp_parser_parse_tentatively (parser);
5634       /* Types may not be defined in a cast.  */
5635       saved_message = parser->type_definition_forbidden_message;
5636       parser->type_definition_forbidden_message
5637         = "types may not be defined in casts";
5638       /* Consume the `('.  */
5639       cp_lexer_consume_token (parser->lexer);
5640       /* A very tricky bit is that `(struct S) { 3 }' is a
5641          compound-literal (which we permit in C++ as an extension).
5642          But, that construct is not a cast-expression -- it is a
5643          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5644          is legal; if the compound-literal were a cast-expression,
5645          you'd need an extra set of parentheses.)  But, if we parse
5646          the type-id, and it happens to be a class-specifier, then we
5647          will commit to the parse at that point, because we cannot
5648          undo the action that is done when creating a new class.  So,
5649          then we cannot back up and do a postfix-expression.
5650
5651          Therefore, we scan ahead to the closing `)', and check to see
5652          if the token after the `)' is a `{'.  If so, we are not
5653          looking at a cast-expression.
5654
5655          Save tokens so that we can put them back.  */
5656       cp_lexer_save_tokens (parser->lexer);
5657       /* Skip tokens until the next token is a closing parenthesis.
5658          If we find the closing `)', and the next token is a `{', then
5659          we are looking at a compound-literal.  */
5660       compound_literal_p
5661         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5662                                                   /*consume_paren=*/true)
5663            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5664       /* Roll back the tokens we skipped.  */
5665       cp_lexer_rollback_tokens (parser->lexer);
5666       /* If we were looking at a compound-literal, simulate an error
5667          so that the call to cp_parser_parse_definitely below will
5668          fail.  */
5669       if (compound_literal_p)
5670         cp_parser_simulate_error (parser);
5671       else
5672         {
5673           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5674           parser->in_type_id_in_expr_p = true;
5675           /* Look for the type-id.  */
5676           type = cp_parser_type_id (parser);
5677           /* Look for the closing `)'.  */
5678           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5679           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5680         }
5681
5682       /* Restore the saved message.  */
5683       parser->type_definition_forbidden_message = saved_message;
5684
5685       /* If ok so far, parse the dependent expression. We cannot be
5686          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5687          ctor of T, but looks like a cast to function returning T
5688          without a dependent expression.  */
5689       if (!cp_parser_error_occurred (parser))
5690         expr = cp_parser_cast_expression (parser,
5691                                           /*address_p=*/false,
5692                                           /*cast_p=*/true);
5693
5694       if (cp_parser_parse_definitely (parser))
5695         {
5696           /* Warn about old-style casts, if so requested.  */
5697           if (warn_old_style_cast
5698               && !in_system_header
5699               && !VOID_TYPE_P (type)
5700               && current_lang_name != lang_name_c)
5701             warning (OPT_Wold_style_cast, "use of old-style cast");
5702
5703           /* Only type conversions to integral or enumeration types
5704              can be used in constant-expressions.  */
5705           if (!cast_valid_in_integral_constant_expression_p (type)
5706               && (cp_parser_non_integral_constant_expression
5707                   (parser,
5708                    "a cast to a type other than an integral or "
5709                    "enumeration type")))
5710             return error_mark_node;
5711
5712           /* Perform the cast.  */
5713           expr = build_c_cast (type, expr);
5714           return expr;
5715         }
5716     }
5717
5718   /* If we get here, then it's not a cast, so it must be a
5719      unary-expression.  */
5720   return cp_parser_unary_expression (parser, address_p, cast_p);
5721 }
5722
5723 /* Parse a binary expression of the general form:
5724
5725    pm-expression:
5726      cast-expression
5727      pm-expression .* cast-expression
5728      pm-expression ->* cast-expression
5729
5730    multiplicative-expression:
5731      pm-expression
5732      multiplicative-expression * pm-expression
5733      multiplicative-expression / pm-expression
5734      multiplicative-expression % pm-expression
5735
5736    additive-expression:
5737      multiplicative-expression
5738      additive-expression + multiplicative-expression
5739      additive-expression - multiplicative-expression
5740
5741    shift-expression:
5742      additive-expression
5743      shift-expression << additive-expression
5744      shift-expression >> additive-expression
5745
5746    relational-expression:
5747      shift-expression
5748      relational-expression < shift-expression
5749      relational-expression > shift-expression
5750      relational-expression <= shift-expression
5751      relational-expression >= shift-expression
5752
5753   GNU Extension:
5754
5755    relational-expression:
5756      relational-expression <? shift-expression
5757      relational-expression >? shift-expression
5758
5759    equality-expression:
5760      relational-expression
5761      equality-expression == relational-expression
5762      equality-expression != relational-expression
5763
5764    and-expression:
5765      equality-expression
5766      and-expression & equality-expression
5767
5768    exclusive-or-expression:
5769      and-expression
5770      exclusive-or-expression ^ and-expression
5771
5772    inclusive-or-expression:
5773      exclusive-or-expression
5774      inclusive-or-expression | exclusive-or-expression
5775
5776    logical-and-expression:
5777      inclusive-or-expression
5778      logical-and-expression && inclusive-or-expression
5779
5780    logical-or-expression:
5781      logical-and-expression
5782      logical-or-expression || logical-and-expression
5783
5784    All these are implemented with a single function like:
5785
5786    binary-expression:
5787      simple-cast-expression
5788      binary-expression <token> binary-expression
5789
5790    CAST_P is true if this expression is the target of a cast.
5791
5792    The binops_by_token map is used to get the tree codes for each <token> type.
5793    binary-expressions are associated according to a precedence table.  */
5794
5795 #define TOKEN_PRECEDENCE(token) \
5796   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5797    ? PREC_NOT_OPERATOR \
5798    : binops_by_token[token->type].prec)
5799
5800 static tree
5801 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5802 {
5803   cp_parser_expression_stack stack;
5804   cp_parser_expression_stack_entry *sp = &stack[0];
5805   tree lhs, rhs;
5806   cp_token *token;
5807   enum tree_code tree_type, lhs_type, rhs_type;
5808   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5809   bool overloaded_p;
5810
5811   /* Parse the first expression.  */
5812   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5813   lhs_type = ERROR_MARK;
5814
5815   for (;;)
5816     {
5817       /* Get an operator token.  */
5818       token = cp_lexer_peek_token (parser->lexer);
5819
5820       new_prec = TOKEN_PRECEDENCE (token);
5821
5822       /* Popping an entry off the stack means we completed a subexpression:
5823          - either we found a token which is not an operator (`>' where it is not
5824            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5825            will happen repeatedly;
5826          - or, we found an operator which has lower priority.  This is the case
5827            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5828            parsing `3 * 4'.  */
5829       if (new_prec <= prec)
5830         {
5831           if (sp == stack)
5832             break;
5833           else
5834             goto pop;
5835         }
5836
5837      get_rhs:
5838       tree_type = binops_by_token[token->type].tree_type;
5839
5840       /* We used the operator token.  */
5841       cp_lexer_consume_token (parser->lexer);
5842
5843       /* Extract another operand.  It may be the RHS of this expression
5844          or the LHS of a new, higher priority expression.  */
5845       rhs = cp_parser_simple_cast_expression (parser);
5846       rhs_type = ERROR_MARK;
5847
5848       /* Get another operator token.  Look up its precedence to avoid
5849          building a useless (immediately popped) stack entry for common
5850          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5851       token = cp_lexer_peek_token (parser->lexer);
5852       lookahead_prec = TOKEN_PRECEDENCE (token);
5853       if (lookahead_prec > new_prec)
5854         {
5855           /* ... and prepare to parse the RHS of the new, higher priority
5856              expression.  Since precedence levels on the stack are
5857              monotonically increasing, we do not have to care about
5858              stack overflows.  */
5859           sp->prec = prec;
5860           sp->tree_type = tree_type;
5861           sp->lhs = lhs;
5862           sp->lhs_type = lhs_type;
5863           sp++;
5864           lhs = rhs;
5865           lhs_type = rhs_type;
5866           prec = new_prec;
5867           new_prec = lookahead_prec;
5868           goto get_rhs;
5869
5870          pop:
5871           /* If the stack is not empty, we have parsed into LHS the right side
5872              (`4' in the example above) of an expression we had suspended.
5873              We can use the information on the stack to recover the LHS (`3')
5874              from the stack together with the tree code (`MULT_EXPR'), and
5875              the precedence of the higher level subexpression
5876              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5877              which will be used to actually build the additive expression.  */
5878           --sp;
5879           prec = sp->prec;
5880           tree_type = sp->tree_type;
5881           rhs = lhs;
5882           rhs_type = lhs_type;
5883           lhs = sp->lhs;
5884           lhs_type = sp->lhs_type;
5885         }
5886
5887       overloaded_p = false;
5888       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5889                                &overloaded_p);
5890       lhs_type = tree_type;
5891
5892       /* If the binary operator required the use of an overloaded operator,
5893          then this expression cannot be an integral constant-expression.
5894          An overloaded operator can be used even if both operands are
5895          otherwise permissible in an integral constant-expression if at
5896          least one of the operands is of enumeration type.  */
5897
5898       if (overloaded_p
5899           && (cp_parser_non_integral_constant_expression
5900               (parser, "calls to overloaded operators")))
5901         return error_mark_node;
5902     }
5903
5904   return lhs;
5905 }
5906
5907
5908 /* Parse the `? expression : assignment-expression' part of a
5909    conditional-expression.  The LOGICAL_OR_EXPR is the
5910    logical-or-expression that started the conditional-expression.
5911    Returns a representation of the entire conditional-expression.
5912
5913    This routine is used by cp_parser_assignment_expression.
5914
5915      ? expression : assignment-expression
5916
5917    GNU Extensions:
5918
5919      ? : assignment-expression */
5920
5921 static tree
5922 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5923 {
5924   tree expr;
5925   tree assignment_expr;
5926
5927   /* Consume the `?' token.  */
5928   cp_lexer_consume_token (parser->lexer);
5929   if (cp_parser_allow_gnu_extensions_p (parser)
5930       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5931     /* Implicit true clause.  */
5932     expr = NULL_TREE;
5933   else
5934     /* Parse the expression.  */
5935     expr = cp_parser_expression (parser, /*cast_p=*/false);
5936
5937   /* The next token should be a `:'.  */
5938   cp_parser_require (parser, CPP_COLON, "`:'");
5939   /* Parse the assignment-expression.  */
5940   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5941
5942   /* Build the conditional-expression.  */
5943   return build_x_conditional_expr (logical_or_expr,
5944                                    expr,
5945                                    assignment_expr);
5946 }
5947
5948 /* Parse an assignment-expression.
5949
5950    assignment-expression:
5951      conditional-expression
5952      logical-or-expression assignment-operator assignment_expression
5953      throw-expression
5954
5955    CAST_P is true if this expression is the target of a cast.
5956
5957    Returns a representation for the expression.  */
5958
5959 static tree
5960 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5961 {
5962   tree expr;
5963
5964   /* If the next token is the `throw' keyword, then we're looking at
5965      a throw-expression.  */
5966   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5967     expr = cp_parser_throw_expression (parser);
5968   /* Otherwise, it must be that we are looking at a
5969      logical-or-expression.  */
5970   else
5971     {
5972       /* Parse the binary expressions (logical-or-expression).  */
5973       expr = cp_parser_binary_expression (parser, cast_p);
5974       /* If the next token is a `?' then we're actually looking at a
5975          conditional-expression.  */
5976       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5977         return cp_parser_question_colon_clause (parser, expr);
5978       else
5979         {
5980           enum tree_code assignment_operator;
5981
5982           /* If it's an assignment-operator, we're using the second
5983              production.  */
5984           assignment_operator
5985             = cp_parser_assignment_operator_opt (parser);
5986           if (assignment_operator != ERROR_MARK)
5987             {
5988               tree rhs;
5989
5990               /* Parse the right-hand side of the assignment.  */
5991               rhs = cp_parser_assignment_expression (parser, cast_p);
5992               /* An assignment may not appear in a
5993                  constant-expression.  */
5994               if (cp_parser_non_integral_constant_expression (parser,
5995                                                               "an assignment"))
5996                 return error_mark_node;
5997               /* Build the assignment expression.  */
5998               expr = build_x_modify_expr (expr,
5999                                           assignment_operator,
6000                                           rhs);
6001             }
6002         }
6003     }
6004
6005   return expr;
6006 }
6007
6008 /* Parse an (optional) assignment-operator.
6009
6010    assignment-operator: one of
6011      = *= /= %= += -= >>= <<= &= ^= |=
6012
6013    GNU Extension:
6014
6015    assignment-operator: one of
6016      <?= >?=
6017
6018    If the next token is an assignment operator, the corresponding tree
6019    code is returned, and the token is consumed.  For example, for
6020    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6021    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6022    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6023    operator, ERROR_MARK is returned.  */
6024
6025 static enum tree_code
6026 cp_parser_assignment_operator_opt (cp_parser* parser)
6027 {
6028   enum tree_code op;
6029   cp_token *token;
6030
6031   /* Peek at the next toen.  */
6032   token = cp_lexer_peek_token (parser->lexer);
6033
6034   switch (token->type)
6035     {
6036     case CPP_EQ:
6037       op = NOP_EXPR;
6038       break;
6039
6040     case CPP_MULT_EQ:
6041       op = MULT_EXPR;
6042       break;
6043
6044     case CPP_DIV_EQ:
6045       op = TRUNC_DIV_EXPR;
6046       break;
6047
6048     case CPP_MOD_EQ:
6049       op = TRUNC_MOD_EXPR;
6050       break;
6051
6052     case CPP_PLUS_EQ:
6053       op = PLUS_EXPR;
6054       break;
6055
6056     case CPP_MINUS_EQ:
6057       op = MINUS_EXPR;
6058       break;
6059
6060     case CPP_RSHIFT_EQ:
6061       op = RSHIFT_EXPR;
6062       break;
6063
6064     case CPP_LSHIFT_EQ:
6065       op = LSHIFT_EXPR;
6066       break;
6067
6068     case CPP_AND_EQ:
6069       op = BIT_AND_EXPR;
6070       break;
6071
6072     case CPP_XOR_EQ:
6073       op = BIT_XOR_EXPR;
6074       break;
6075
6076     case CPP_OR_EQ:
6077       op = BIT_IOR_EXPR;
6078       break;
6079
6080     default:
6081       /* Nothing else is an assignment operator.  */
6082       op = ERROR_MARK;
6083     }
6084
6085   /* If it was an assignment operator, consume it.  */
6086   if (op != ERROR_MARK)
6087     cp_lexer_consume_token (parser->lexer);
6088
6089   return op;
6090 }
6091
6092 /* Parse an expression.
6093
6094    expression:
6095      assignment-expression
6096      expression , assignment-expression
6097
6098    CAST_P is true if this expression is the target of a cast.
6099
6100    Returns a representation of the expression.  */
6101
6102 static tree
6103 cp_parser_expression (cp_parser* parser, bool cast_p)
6104 {
6105   tree expression = NULL_TREE;
6106
6107   while (true)
6108     {
6109       tree assignment_expression;
6110
6111       /* Parse the next assignment-expression.  */
6112       assignment_expression
6113         = cp_parser_assignment_expression (parser, cast_p);
6114       /* If this is the first assignment-expression, we can just
6115          save it away.  */
6116       if (!expression)
6117         expression = assignment_expression;
6118       else
6119         expression = build_x_compound_expr (expression,
6120                                             assignment_expression);
6121       /* If the next token is not a comma, then we are done with the
6122          expression.  */
6123       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6124         break;
6125       /* Consume the `,'.  */
6126       cp_lexer_consume_token (parser->lexer);
6127       /* A comma operator cannot appear in a constant-expression.  */
6128       if (cp_parser_non_integral_constant_expression (parser,
6129                                                       "a comma operator"))
6130         expression = error_mark_node;
6131     }
6132
6133   return expression;
6134 }
6135
6136 /* Parse a constant-expression.
6137
6138    constant-expression:
6139      conditional-expression
6140
6141   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6142   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6143   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6144   is false, NON_CONSTANT_P should be NULL.  */
6145
6146 static tree
6147 cp_parser_constant_expression (cp_parser* parser,
6148                                bool allow_non_constant_p,
6149                                bool *non_constant_p)
6150 {
6151   bool saved_integral_constant_expression_p;
6152   bool saved_allow_non_integral_constant_expression_p;
6153   bool saved_non_integral_constant_expression_p;
6154   tree expression;
6155
6156   /* It might seem that we could simply parse the
6157      conditional-expression, and then check to see if it were
6158      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6159      one that the compiler can figure out is constant, possibly after
6160      doing some simplifications or optimizations.  The standard has a
6161      precise definition of constant-expression, and we must honor
6162      that, even though it is somewhat more restrictive.
6163
6164      For example:
6165
6166        int i[(2, 3)];
6167
6168      is not a legal declaration, because `(2, 3)' is not a
6169      constant-expression.  The `,' operator is forbidden in a
6170      constant-expression.  However, GCC's constant-folding machinery
6171      will fold this operation to an INTEGER_CST for `3'.  */
6172
6173   /* Save the old settings.  */
6174   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6175   saved_allow_non_integral_constant_expression_p
6176     = parser->allow_non_integral_constant_expression_p;
6177   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6178   /* We are now parsing a constant-expression.  */
6179   parser->integral_constant_expression_p = true;
6180   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6181   parser->non_integral_constant_expression_p = false;
6182   /* Although the grammar says "conditional-expression", we parse an
6183      "assignment-expression", which also permits "throw-expression"
6184      and the use of assignment operators.  In the case that
6185      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6186      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6187      actually essential that we look for an assignment-expression.
6188      For example, cp_parser_initializer_clauses uses this function to
6189      determine whether a particular assignment-expression is in fact
6190      constant.  */
6191   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6192   /* Restore the old settings.  */
6193   parser->integral_constant_expression_p
6194     = saved_integral_constant_expression_p;
6195   parser->allow_non_integral_constant_expression_p
6196     = saved_allow_non_integral_constant_expression_p;
6197   if (allow_non_constant_p)
6198     *non_constant_p = parser->non_integral_constant_expression_p;
6199   else if (parser->non_integral_constant_expression_p)
6200     expression = error_mark_node;
6201   parser->non_integral_constant_expression_p
6202     = saved_non_integral_constant_expression_p;
6203
6204   return expression;
6205 }
6206
6207 /* Parse __builtin_offsetof.
6208
6209    offsetof-expression:
6210      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6211
6212    offsetof-member-designator:
6213      id-expression
6214      | offsetof-member-designator "." id-expression
6215      | offsetof-member-designator "[" expression "]"  */
6216
6217 static tree
6218 cp_parser_builtin_offsetof (cp_parser *parser)
6219 {
6220   int save_ice_p, save_non_ice_p;
6221   tree type, expr;
6222   cp_id_kind dummy;
6223
6224   /* We're about to accept non-integral-constant things, but will
6225      definitely yield an integral constant expression.  Save and
6226      restore these values around our local parsing.  */
6227   save_ice_p = parser->integral_constant_expression_p;
6228   save_non_ice_p = parser->non_integral_constant_expression_p;
6229
6230   /* Consume the "__builtin_offsetof" token.  */
6231   cp_lexer_consume_token (parser->lexer);
6232   /* Consume the opening `('.  */
6233   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6234   /* Parse the type-id.  */
6235   type = cp_parser_type_id (parser);
6236   /* Look for the `,'.  */
6237   cp_parser_require (parser, CPP_COMMA, "`,'");
6238
6239   /* Build the (type *)null that begins the traditional offsetof macro.  */
6240   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6241
6242   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6243   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6244                                                  true, &dummy);
6245   while (true)
6246     {
6247       cp_token *token = cp_lexer_peek_token (parser->lexer);
6248       switch (token->type)
6249         {
6250         case CPP_OPEN_SQUARE:
6251           /* offsetof-member-designator "[" expression "]" */
6252           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6253           break;
6254
6255         case CPP_DOT:
6256           /* offsetof-member-designator "." identifier */
6257           cp_lexer_consume_token (parser->lexer);
6258           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6259                                                          true, &dummy);
6260           break;
6261
6262         case CPP_CLOSE_PAREN:
6263           /* Consume the ")" token.  */
6264           cp_lexer_consume_token (parser->lexer);
6265           goto success;
6266
6267         default:
6268           /* Error.  We know the following require will fail, but
6269              that gives the proper error message.  */
6270           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6271           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6272           expr = error_mark_node;
6273           goto failure;
6274         }
6275     }
6276
6277  success:
6278   /* If we're processing a template, we can't finish the semantics yet.
6279      Otherwise we can fold the entire expression now.  */
6280   if (processing_template_decl)
6281     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6282   else
6283     expr = finish_offsetof (expr);
6284
6285  failure:
6286   parser->integral_constant_expression_p = save_ice_p;
6287   parser->non_integral_constant_expression_p = save_non_ice_p;
6288
6289   return expr;
6290 }
6291
6292 /* Statements [gram.stmt.stmt]  */
6293
6294 /* Parse a statement.
6295
6296    statement:
6297      labeled-statement
6298      expression-statement
6299      compound-statement
6300      selection-statement
6301      iteration-statement
6302      jump-statement
6303      declaration-statement
6304      try-block
6305
6306   IN_COMPOUND is true when the statement is nested inside a
6307   cp_parser_compound_statement; this matters for certain pragmas.
6308
6309   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6310   is a (possibly labeled) if statement which is not enclosed in braces
6311   and has an else clause.  This is used to implement -Wparentheses.  */
6312
6313 static void
6314 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6315                      bool in_compound, bool *if_p)
6316 {
6317   tree statement;
6318   cp_token *token;
6319   location_t statement_location;
6320
6321  restart:
6322   if (if_p != NULL)
6323     *if_p = false;
6324   /* There is no statement yet.  */
6325   statement = NULL_TREE;
6326   /* Peek at the next token.  */
6327   token = cp_lexer_peek_token (parser->lexer);
6328   /* Remember the location of the first token in the statement.  */
6329   statement_location = token->location;
6330   /* If this is a keyword, then that will often determine what kind of
6331      statement we have.  */
6332   if (token->type == CPP_KEYWORD)
6333     {
6334       enum rid keyword = token->keyword;
6335
6336       switch (keyword)
6337         {
6338         case RID_CASE:
6339         case RID_DEFAULT:
6340           /* Looks like a labeled-statement with a case label.
6341              Parse the label, and then use tail recursion to parse
6342              the statement.  */
6343           cp_parser_label_for_labeled_statement (parser);
6344           goto restart;
6345
6346         case RID_IF:
6347         case RID_SWITCH:
6348           statement = cp_parser_selection_statement (parser, if_p);
6349           break;
6350
6351         case RID_WHILE:
6352         case RID_DO:
6353         case RID_FOR:
6354           statement = cp_parser_iteration_statement (parser);
6355           break;
6356
6357         case RID_BREAK:
6358         case RID_CONTINUE:
6359         case RID_RETURN:
6360         case RID_GOTO:
6361           statement = cp_parser_jump_statement (parser);
6362           break;
6363
6364           /* Objective-C++ exception-handling constructs.  */
6365         case RID_AT_TRY:
6366         case RID_AT_CATCH:
6367         case RID_AT_FINALLY:
6368         case RID_AT_SYNCHRONIZED:
6369         case RID_AT_THROW:
6370           statement = cp_parser_objc_statement (parser);
6371           break;
6372
6373         case RID_TRY:
6374           statement = cp_parser_try_block (parser);
6375           break;
6376
6377         case RID_NAMESPACE:
6378           /* This must be a namespace alias definition.  */
6379           cp_parser_declaration_statement (parser);
6380           return;
6381           
6382         default:
6383           /* It might be a keyword like `int' that can start a
6384              declaration-statement.  */
6385           break;
6386         }
6387     }
6388   else if (token->type == CPP_NAME)
6389     {
6390       /* If the next token is a `:', then we are looking at a
6391          labeled-statement.  */
6392       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6393       if (token->type == CPP_COLON)
6394         {
6395           /* Looks like a labeled-statement with an ordinary label.
6396              Parse the label, and then use tail recursion to parse
6397              the statement.  */
6398           cp_parser_label_for_labeled_statement (parser);
6399           goto restart;
6400         }
6401     }
6402   /* Anything that starts with a `{' must be a compound-statement.  */
6403   else if (token->type == CPP_OPEN_BRACE)
6404     statement = cp_parser_compound_statement (parser, NULL, false);
6405   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6406      a statement all its own.  */
6407   else if (token->type == CPP_PRAGMA)
6408     {
6409       /* Only certain OpenMP pragmas are attached to statements, and thus
6410          are considered statements themselves.  All others are not.  In
6411          the context of a compound, accept the pragma as a "statement" and
6412          return so that we can check for a close brace.  Otherwise we
6413          require a real statement and must go back and read one.  */
6414       if (in_compound)
6415         cp_parser_pragma (parser, pragma_compound);
6416       else if (!cp_parser_pragma (parser, pragma_stmt))
6417         goto restart;
6418       return;
6419     }
6420   else if (token->type == CPP_EOF)
6421     {
6422       cp_parser_error (parser, "expected statement");
6423       return;
6424     }
6425
6426   /* Everything else must be a declaration-statement or an
6427      expression-statement.  Try for the declaration-statement
6428      first, unless we are looking at a `;', in which case we know that
6429      we have an expression-statement.  */
6430   if (!statement)
6431     {
6432       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6433         {
6434           cp_parser_parse_tentatively (parser);
6435           /* Try to parse the declaration-statement.  */
6436           cp_parser_declaration_statement (parser);
6437           /* If that worked, we're done.  */
6438           if (cp_parser_parse_definitely (parser))
6439             return;
6440         }
6441       /* Look for an expression-statement instead.  */
6442       statement = cp_parser_expression_statement (parser, in_statement_expr);
6443     }
6444
6445   /* Set the line number for the statement.  */
6446   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6447     SET_EXPR_LOCATION (statement, statement_location);
6448 }
6449
6450 /* Parse the label for a labeled-statement, i.e.
6451
6452    identifier :
6453    case constant-expression :
6454    default :
6455
6456    GNU Extension:
6457    case constant-expression ... constant-expression : statement
6458
6459    When a label is parsed without errors, the label is added to the
6460    parse tree by the finish_* functions, so this function doesn't
6461    have to return the label.  */
6462
6463 static void
6464 cp_parser_label_for_labeled_statement (cp_parser* parser)
6465 {
6466   cp_token *token;
6467
6468   /* The next token should be an identifier.  */
6469   token = cp_lexer_peek_token (parser->lexer);
6470   if (token->type != CPP_NAME
6471       && token->type != CPP_KEYWORD)
6472     {
6473       cp_parser_error (parser, "expected labeled-statement");
6474       return;
6475     }
6476
6477   switch (token->keyword)
6478     {
6479     case RID_CASE:
6480       {
6481         tree expr, expr_hi;
6482         cp_token *ellipsis;
6483
6484         /* Consume the `case' token.  */
6485         cp_lexer_consume_token (parser->lexer);
6486         /* Parse the constant-expression.  */
6487         expr = cp_parser_constant_expression (parser,
6488                                               /*allow_non_constant_p=*/false,
6489                                               NULL);
6490
6491         ellipsis = cp_lexer_peek_token (parser->lexer);
6492         if (ellipsis->type == CPP_ELLIPSIS)
6493           {
6494             /* Consume the `...' token.  */
6495             cp_lexer_consume_token (parser->lexer);
6496             expr_hi =
6497               cp_parser_constant_expression (parser,
6498                                              /*allow_non_constant_p=*/false,
6499                                              NULL);
6500             /* We don't need to emit warnings here, as the common code
6501                will do this for us.  */
6502           }
6503         else
6504           expr_hi = NULL_TREE;
6505
6506         if (parser->in_switch_statement_p)
6507           finish_case_label (expr, expr_hi);
6508         else
6509           error ("case label %qE not within a switch statement", expr);
6510       }
6511       break;
6512
6513     case RID_DEFAULT:
6514       /* Consume the `default' token.  */
6515       cp_lexer_consume_token (parser->lexer);
6516
6517       if (parser->in_switch_statement_p)
6518         finish_case_label (NULL_TREE, NULL_TREE);
6519       else
6520         error ("case label not within a switch statement");
6521       break;
6522
6523     default:
6524       /* Anything else must be an ordinary label.  */
6525       finish_label_stmt (cp_parser_identifier (parser));
6526       break;
6527     }
6528
6529   /* Require the `:' token.  */
6530   cp_parser_require (parser, CPP_COLON, "`:'");
6531 }
6532
6533 /* Parse an expression-statement.
6534
6535    expression-statement:
6536      expression [opt] ;
6537
6538    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6539    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6540    indicates whether this expression-statement is part of an
6541    expression statement.  */
6542
6543 static tree
6544 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6545 {
6546   tree statement = NULL_TREE;
6547
6548   /* If the next token is a ';', then there is no expression
6549      statement.  */
6550   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6551     statement = cp_parser_expression (parser, /*cast_p=*/false);
6552
6553   /* Consume the final `;'.  */
6554   cp_parser_consume_semicolon_at_end_of_statement (parser);
6555
6556   if (in_statement_expr
6557       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6558     /* This is the final expression statement of a statement
6559        expression.  */
6560     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6561   else if (statement)
6562     statement = finish_expr_stmt (statement);
6563   else
6564     finish_stmt ();
6565
6566   return statement;
6567 }
6568
6569 /* Parse a compound-statement.
6570
6571    compound-statement:
6572      { statement-seq [opt] }
6573
6574    Returns a tree representing the statement.  */
6575
6576 static tree
6577 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6578                               bool in_try)
6579 {
6580   tree compound_stmt;
6581
6582   /* Consume the `{'.  */
6583   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6584     return error_mark_node;
6585   /* Begin the compound-statement.  */
6586   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6587   /* Parse an (optional) statement-seq.  */
6588   cp_parser_statement_seq_opt (parser, in_statement_expr);
6589   /* Finish the compound-statement.  */
6590   finish_compound_stmt (compound_stmt);
6591   /* Consume the `}'.  */
6592   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6593
6594   return compound_stmt;
6595 }
6596
6597 /* Parse an (optional) statement-seq.
6598
6599    statement-seq:
6600      statement
6601      statement-seq [opt] statement  */
6602
6603 static void
6604 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6605 {
6606   /* Scan statements until there aren't any more.  */
6607   while (true)
6608     {
6609       cp_token *token = cp_lexer_peek_token (parser->lexer);
6610
6611       /* If we're looking at a `}', then we've run out of statements.  */
6612       if (token->type == CPP_CLOSE_BRACE
6613           || token->type == CPP_EOF
6614           || token->type == CPP_PRAGMA_EOL)
6615         break;
6616       
6617       /* If we are in a compound statement and find 'else' then
6618          something went wrong.  */
6619       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6620         {
6621           if (parser->in_statement & IN_IF_STMT) 
6622             break;
6623           else
6624             {
6625               token = cp_lexer_consume_token (parser->lexer);
6626               error ("%<else%> without a previous %<if%>");
6627             }
6628         }
6629
6630       /* Parse the statement.  */
6631       cp_parser_statement (parser, in_statement_expr, true, NULL);
6632     }
6633 }
6634
6635 /* Parse a selection-statement.
6636
6637    selection-statement:
6638      if ( condition ) statement
6639      if ( condition ) statement else statement
6640      switch ( condition ) statement
6641
6642    Returns the new IF_STMT or SWITCH_STMT.
6643
6644    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6645    is a (possibly labeled) if statement which is not enclosed in
6646    braces and has an else clause.  This is used to implement
6647    -Wparentheses.  */
6648
6649 static tree
6650 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6651 {
6652   cp_token *token;
6653   enum rid keyword;
6654
6655   if (if_p != NULL)
6656     *if_p = false;
6657
6658   /* Peek at the next token.  */
6659   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6660
6661   /* See what kind of keyword it is.  */
6662   keyword = token->keyword;
6663   switch (keyword)
6664     {
6665     case RID_IF:
6666     case RID_SWITCH:
6667       {
6668         tree statement;
6669         tree condition;
6670
6671         /* Look for the `('.  */
6672         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6673           {
6674             cp_parser_skip_to_end_of_statement (parser);
6675             return error_mark_node;
6676           }
6677
6678         /* Begin the selection-statement.  */
6679         if (keyword == RID_IF)
6680           statement = begin_if_stmt ();
6681         else
6682           statement = begin_switch_stmt ();
6683
6684         /* Parse the condition.  */
6685         condition = cp_parser_condition (parser);
6686         /* Look for the `)'.  */
6687         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6688           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6689                                                  /*consume_paren=*/true);
6690
6691         if (keyword == RID_IF)
6692           {
6693             bool nested_if;
6694             unsigned char in_statement;
6695
6696             /* Add the condition.  */
6697             finish_if_stmt_cond (condition, statement);
6698
6699             /* Parse the then-clause.  */
6700             in_statement = parser->in_statement;
6701             parser->in_statement |= IN_IF_STMT;
6702             cp_parser_implicitly_scoped_statement (parser, &nested_if);
6703             parser->in_statement = in_statement;
6704
6705             finish_then_clause (statement);
6706
6707             /* If the next token is `else', parse the else-clause.  */
6708             if (cp_lexer_next_token_is_keyword (parser->lexer,
6709                                                 RID_ELSE))
6710               {
6711                 /* Consume the `else' keyword.  */
6712                 cp_lexer_consume_token (parser->lexer);
6713                 begin_else_clause (statement);
6714                 /* Parse the else-clause.  */
6715                 cp_parser_implicitly_scoped_statement (parser, NULL);
6716                 finish_else_clause (statement);
6717
6718                 /* If we are currently parsing a then-clause, then
6719                    IF_P will not be NULL.  We set it to true to
6720                    indicate that this if statement has an else clause.
6721                    This may trigger the Wparentheses warning below
6722                    when we get back up to the parent if statement.  */
6723                 if (if_p != NULL)
6724                   *if_p = true;
6725               }
6726             else
6727               {
6728                 /* This if statement does not have an else clause.  If
6729                    NESTED_IF is true, then the then-clause is an if
6730                    statement which does have an else clause.  We warn
6731                    about the potential ambiguity.  */
6732                 if (nested_if)
6733                   warning (OPT_Wparentheses,
6734                            ("%Hsuggest explicit braces "
6735                             "to avoid ambiguous %<else%>"),
6736                            EXPR_LOCUS (statement));
6737               }
6738
6739             /* Now we're all done with the if-statement.  */
6740             finish_if_stmt (statement);
6741           }
6742         else
6743           {
6744             bool in_switch_statement_p;
6745             unsigned char in_statement;
6746
6747             /* Add the condition.  */
6748             finish_switch_cond (condition, statement);
6749
6750             /* Parse the body of the switch-statement.  */
6751             in_switch_statement_p = parser->in_switch_statement_p;
6752             in_statement = parser->in_statement;
6753             parser->in_switch_statement_p = true;
6754             parser->in_statement |= IN_SWITCH_STMT;
6755             cp_parser_implicitly_scoped_statement (parser, NULL);
6756             parser->in_switch_statement_p = in_switch_statement_p;
6757             parser->in_statement = in_statement;
6758
6759             /* Now we're all done with the switch-statement.  */
6760             finish_switch_stmt (statement);
6761           }
6762
6763         return statement;
6764       }
6765       break;
6766
6767     default:
6768       cp_parser_error (parser, "expected selection-statement");
6769       return error_mark_node;
6770     }
6771 }
6772
6773 /* Parse a condition.
6774
6775    condition:
6776      expression
6777      type-specifier-seq declarator = assignment-expression
6778
6779    GNU Extension:
6780
6781    condition:
6782      type-specifier-seq declarator asm-specification [opt]
6783        attributes [opt] = assignment-expression
6784
6785    Returns the expression that should be tested.  */
6786
6787 static tree
6788 cp_parser_condition (cp_parser* parser)
6789 {
6790   cp_decl_specifier_seq type_specifiers;
6791   const char *saved_message;
6792
6793   /* Try the declaration first.  */
6794   cp_parser_parse_tentatively (parser);
6795   /* New types are not allowed in the type-specifier-seq for a
6796      condition.  */
6797   saved_message = parser->type_definition_forbidden_message;
6798   parser->type_definition_forbidden_message
6799     = "types may not be defined in conditions";
6800   /* Parse the type-specifier-seq.  */
6801   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6802                                 &type_specifiers);
6803   /* Restore the saved message.  */
6804   parser->type_definition_forbidden_message = saved_message;
6805   /* If all is well, we might be looking at a declaration.  */
6806   if (!cp_parser_error_occurred (parser))
6807     {
6808       tree decl;
6809       tree asm_specification;
6810       tree attributes;
6811       cp_declarator *declarator;
6812       tree initializer = NULL_TREE;
6813
6814       /* Parse the declarator.  */
6815       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6816                                          /*ctor_dtor_or_conv_p=*/NULL,
6817                                          /*parenthesized_p=*/NULL,
6818                                          /*member_p=*/false);
6819       /* Parse the attributes.  */
6820       attributes = cp_parser_attributes_opt (parser);
6821       /* Parse the asm-specification.  */
6822       asm_specification = cp_parser_asm_specification_opt (parser);
6823       /* If the next token is not an `=', then we might still be
6824          looking at an expression.  For example:
6825
6826            if (A(a).x)
6827
6828          looks like a decl-specifier-seq and a declarator -- but then
6829          there is no `=', so this is an expression.  */
6830       cp_parser_require (parser, CPP_EQ, "`='");
6831       /* If we did see an `=', then we are looking at a declaration
6832          for sure.  */
6833       if (cp_parser_parse_definitely (parser))
6834         {
6835           tree pushed_scope;
6836           bool non_constant_p;
6837
6838           /* Create the declaration.  */
6839           decl = start_decl (declarator, &type_specifiers,
6840                              /*initialized_p=*/true,
6841                              attributes, /*prefix_attributes=*/NULL_TREE,
6842                              &pushed_scope);
6843           /* Parse the assignment-expression.  */
6844           initializer
6845             = cp_parser_constant_expression (parser,
6846                                              /*allow_non_constant_p=*/true,
6847                                              &non_constant_p);
6848           if (!non_constant_p)
6849             initializer = fold_non_dependent_expr (initializer);
6850
6851           /* Process the initializer.  */
6852           cp_finish_decl (decl,
6853                           initializer, !non_constant_p,
6854                           asm_specification,
6855                           LOOKUP_ONLYCONVERTING);
6856
6857           if (pushed_scope)
6858             pop_scope (pushed_scope);
6859
6860           return convert_from_reference (decl);
6861         }
6862     }
6863   /* If we didn't even get past the declarator successfully, we are
6864      definitely not looking at a declaration.  */
6865   else
6866     cp_parser_abort_tentative_parse (parser);
6867
6868   /* Otherwise, we are looking at an expression.  */
6869   return cp_parser_expression (parser, /*cast_p=*/false);
6870 }
6871
6872 /* Parse an iteration-statement.
6873
6874    iteration-statement:
6875      while ( condition ) statement
6876      do statement while ( expression ) ;
6877      for ( for-init-statement condition [opt] ; expression [opt] )
6878        statement
6879
6880    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6881
6882 static tree
6883 cp_parser_iteration_statement (cp_parser* parser)
6884 {
6885   cp_token *token;
6886   enum rid keyword;
6887   tree statement;
6888   unsigned char in_statement;
6889
6890   /* Peek at the next token.  */
6891   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6892   if (!token)
6893     return error_mark_node;
6894
6895   /* Remember whether or not we are already within an iteration
6896      statement.  */
6897   in_statement = parser->in_statement;
6898
6899   /* See what kind of keyword it is.  */
6900   keyword = token->keyword;
6901   switch (keyword)
6902     {
6903     case RID_WHILE:
6904       {
6905         tree condition;
6906
6907         /* Begin the while-statement.  */
6908         statement = begin_while_stmt ();
6909         /* Look for the `('.  */
6910         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6911         /* Parse the condition.  */
6912         condition = cp_parser_condition (parser);
6913         finish_while_stmt_cond (condition, statement);
6914         /* Look for the `)'.  */
6915         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6916         /* Parse the dependent statement.  */
6917         parser->in_statement = IN_ITERATION_STMT;
6918         cp_parser_already_scoped_statement (parser);
6919         parser->in_statement = in_statement;
6920         /* We're done with the while-statement.  */
6921         finish_while_stmt (statement);
6922       }
6923       break;
6924
6925     case RID_DO:
6926       {
6927         tree expression;
6928
6929         /* Begin the do-statement.  */
6930         statement = begin_do_stmt ();
6931         /* Parse the body of the do-statement.  */
6932         parser->in_statement = IN_ITERATION_STMT;
6933         cp_parser_implicitly_scoped_statement (parser, NULL);
6934         parser->in_statement = in_statement;
6935         finish_do_body (statement);
6936         /* Look for the `while' keyword.  */
6937         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6938         /* Look for the `('.  */
6939         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6940         /* Parse the expression.  */
6941         expression = cp_parser_expression (parser, /*cast_p=*/false);
6942         /* We're done with the do-statement.  */
6943         finish_do_stmt (expression, statement);
6944         /* Look for the `)'.  */
6945         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6946         /* Look for the `;'.  */
6947         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6948       }
6949       break;
6950
6951     case RID_FOR:
6952       {
6953         tree condition = NULL_TREE;
6954         tree expression = NULL_TREE;
6955
6956         /* Begin the for-statement.  */
6957         statement = begin_for_stmt ();
6958         /* Look for the `('.  */
6959         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6960         /* Parse the initialization.  */
6961         cp_parser_for_init_statement (parser);
6962         finish_for_init_stmt (statement);
6963
6964         /* If there's a condition, process it.  */
6965         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6966           condition = cp_parser_condition (parser);
6967         finish_for_cond (condition, statement);
6968         /* Look for the `;'.  */
6969         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6970
6971         /* If there's an expression, process it.  */
6972         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6973           expression = cp_parser_expression (parser, /*cast_p=*/false);
6974         finish_for_expr (expression, statement);
6975         /* Look for the `)'.  */
6976         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6977
6978         /* Parse the body of the for-statement.  */
6979         parser->in_statement = IN_ITERATION_STMT;
6980         cp_parser_already_scoped_statement (parser);
6981         parser->in_statement = in_statement;
6982
6983         /* We're done with the for-statement.  */
6984         finish_for_stmt (statement);
6985       }
6986       break;
6987
6988     default:
6989       cp_parser_error (parser, "expected iteration-statement");
6990       statement = error_mark_node;
6991       break;
6992     }
6993
6994   return statement;
6995 }
6996
6997 /* Parse a for-init-statement.
6998
6999    for-init-statement:
7000      expression-statement
7001      simple-declaration  */
7002
7003 static void
7004 cp_parser_for_init_statement (cp_parser* parser)
7005 {
7006   /* If the next token is a `;', then we have an empty
7007      expression-statement.  Grammatically, this is also a
7008      simple-declaration, but an invalid one, because it does not
7009      declare anything.  Therefore, if we did not handle this case
7010      specially, we would issue an error message about an invalid
7011      declaration.  */
7012   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7013     {
7014       /* We're going to speculatively look for a declaration, falling back
7015          to an expression, if necessary.  */
7016       cp_parser_parse_tentatively (parser);
7017       /* Parse the declaration.  */
7018       cp_parser_simple_declaration (parser,
7019                                     /*function_definition_allowed_p=*/false);
7020       /* If the tentative parse failed, then we shall need to look for an
7021          expression-statement.  */
7022       if (cp_parser_parse_definitely (parser))
7023         return;
7024     }
7025
7026   cp_parser_expression_statement (parser, false);
7027 }
7028
7029 /* Parse a jump-statement.
7030
7031    jump-statement:
7032      break ;
7033      continue ;
7034      return expression [opt] ;
7035      goto identifier ;
7036
7037    GNU extension:
7038
7039    jump-statement:
7040      goto * expression ;
7041
7042    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7043
7044 static tree
7045 cp_parser_jump_statement (cp_parser* parser)
7046 {
7047   tree statement = error_mark_node;
7048   cp_token *token;
7049   enum rid keyword;
7050   unsigned char in_statement;
7051
7052   /* Peek at the next token.  */
7053   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7054   if (!token)
7055     return error_mark_node;
7056
7057   /* See what kind of keyword it is.  */
7058   keyword = token->keyword;
7059   switch (keyword)
7060     {
7061     case RID_BREAK:
7062       in_statement = parser->in_statement & ~IN_IF_STMT;      
7063       switch (in_statement)
7064         {
7065         case 0:
7066           error ("break statement not within loop or switch");
7067           break;
7068         default:
7069           gcc_assert ((in_statement & IN_SWITCH_STMT)
7070                       || in_statement == IN_ITERATION_STMT);
7071           statement = finish_break_stmt ();
7072           break;
7073         case IN_OMP_BLOCK:
7074           error ("invalid exit from OpenMP structured block");
7075           break;
7076         case IN_OMP_FOR:
7077           error ("break statement used with OpenMP for loop");
7078           break;
7079         }
7080       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7081       break;
7082
7083     case RID_CONTINUE:
7084       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7085         {
7086         case 0:
7087           error ("continue statement not within a loop");
7088           break;
7089         case IN_ITERATION_STMT:
7090         case IN_OMP_FOR:
7091           statement = finish_continue_stmt ();
7092           break;
7093         case IN_OMP_BLOCK:
7094           error ("invalid exit from OpenMP structured block");
7095           break;
7096         default:
7097           gcc_unreachable ();
7098         }
7099       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7100       break;
7101
7102     case RID_RETURN:
7103       {
7104         tree expr;
7105
7106         /* If the next token is a `;', then there is no
7107            expression.  */
7108         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7109           expr = cp_parser_expression (parser, /*cast_p=*/false);
7110         else
7111           expr = NULL_TREE;
7112         /* Build the return-statement.  */
7113         statement = finish_return_stmt (expr);
7114         /* Look for the final `;'.  */
7115         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7116       }
7117       break;
7118
7119     case RID_GOTO:
7120       /* Create the goto-statement.  */
7121       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7122         {
7123           /* Issue a warning about this use of a GNU extension.  */
7124           if (pedantic)
7125             pedwarn ("ISO C++ forbids computed gotos");
7126           /* Consume the '*' token.  */
7127           cp_lexer_consume_token (parser->lexer);
7128           /* Parse the dependent expression.  */
7129           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7130         }
7131       else
7132         finish_goto_stmt (cp_parser_identifier (parser));
7133       /* Look for the final `;'.  */
7134       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7135       break;
7136
7137     default:
7138       cp_parser_error (parser, "expected jump-statement");
7139       break;
7140     }
7141
7142   return statement;
7143 }
7144
7145 /* Parse a declaration-statement.
7146
7147    declaration-statement:
7148      block-declaration  */
7149
7150 static void
7151 cp_parser_declaration_statement (cp_parser* parser)
7152 {
7153   void *p;
7154
7155   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7156   p = obstack_alloc (&declarator_obstack, 0);
7157
7158  /* Parse the block-declaration.  */
7159   cp_parser_block_declaration (parser, /*statement_p=*/true);
7160
7161   /* Free any declarators allocated.  */
7162   obstack_free (&declarator_obstack, p);
7163
7164   /* Finish off the statement.  */
7165   finish_stmt ();
7166 }
7167
7168 /* Some dependent statements (like `if (cond) statement'), are
7169    implicitly in their own scope.  In other words, if the statement is
7170    a single statement (as opposed to a compound-statement), it is
7171    none-the-less treated as if it were enclosed in braces.  Any
7172    declarations appearing in the dependent statement are out of scope
7173    after control passes that point.  This function parses a statement,
7174    but ensures that is in its own scope, even if it is not a
7175    compound-statement.
7176
7177    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7178    is a (possibly labeled) if statement which is not enclosed in
7179    braces and has an else clause.  This is used to implement
7180    -Wparentheses.
7181
7182    Returns the new statement.  */
7183
7184 static tree
7185 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7186 {
7187   tree statement;
7188
7189   if (if_p != NULL)
7190     *if_p = false;
7191
7192   /* Mark if () ; with a special NOP_EXPR.  */
7193   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7194     {
7195       cp_lexer_consume_token (parser->lexer);
7196       statement = add_stmt (build_empty_stmt ());
7197     }
7198   /* if a compound is opened, we simply parse the statement directly.  */
7199   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7200     statement = cp_parser_compound_statement (parser, NULL, false);
7201   /* If the token is not a `{', then we must take special action.  */
7202   else
7203     {
7204       /* Create a compound-statement.  */
7205       statement = begin_compound_stmt (0);
7206       /* Parse the dependent-statement.  */
7207       cp_parser_statement (parser, NULL_TREE, false, if_p);
7208       /* Finish the dummy compound-statement.  */
7209       finish_compound_stmt (statement);
7210     }
7211
7212   /* Return the statement.  */
7213   return statement;
7214 }
7215
7216 /* For some dependent statements (like `while (cond) statement'), we
7217    have already created a scope.  Therefore, even if the dependent
7218    statement is a compound-statement, we do not want to create another
7219    scope.  */
7220
7221 static void
7222 cp_parser_already_scoped_statement (cp_parser* parser)
7223 {
7224   /* If the token is a `{', then we must take special action.  */
7225   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7226     cp_parser_statement (parser, NULL_TREE, false, NULL);
7227   else
7228     {
7229       /* Avoid calling cp_parser_compound_statement, so that we
7230          don't create a new scope.  Do everything else by hand.  */
7231       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7232       cp_parser_statement_seq_opt (parser, NULL_TREE);
7233       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7234     }
7235 }
7236
7237 /* Declarations [gram.dcl.dcl] */
7238
7239 /* Parse an optional declaration-sequence.
7240
7241    declaration-seq:
7242      declaration
7243      declaration-seq declaration  */
7244
7245 static void
7246 cp_parser_declaration_seq_opt (cp_parser* parser)
7247 {
7248   while (true)
7249     {
7250       cp_token *token;
7251
7252       token = cp_lexer_peek_token (parser->lexer);
7253
7254       if (token->type == CPP_CLOSE_BRACE
7255           || token->type == CPP_EOF
7256           || token->type == CPP_PRAGMA_EOL)
7257         break;
7258
7259       if (token->type == CPP_SEMICOLON)
7260         {
7261           /* A declaration consisting of a single semicolon is
7262              invalid.  Allow it unless we're being pedantic.  */
7263           cp_lexer_consume_token (parser->lexer);
7264           if (pedantic && !in_system_header)
7265             pedwarn ("extra %<;%>");
7266           continue;
7267         }
7268
7269       /* If we're entering or exiting a region that's implicitly
7270          extern "C", modify the lang context appropriately.  */
7271       if (!parser->implicit_extern_c && token->implicit_extern_c)
7272         {
7273           push_lang_context (lang_name_c);
7274           parser->implicit_extern_c = true;
7275         }
7276       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7277         {
7278           pop_lang_context ();
7279           parser->implicit_extern_c = false;
7280         }
7281
7282       if (token->type == CPP_PRAGMA)
7283         {
7284           /* A top-level declaration can consist solely of a #pragma.
7285              A nested declaration cannot, so this is done here and not
7286              in cp_parser_declaration.  (A #pragma at block scope is
7287              handled in cp_parser_statement.)  */
7288           cp_parser_pragma (parser, pragma_external);
7289           continue;
7290         }
7291
7292       /* Parse the declaration itself.  */
7293       cp_parser_declaration (parser);
7294     }
7295 }
7296
7297 /* Parse a declaration.
7298
7299    declaration:
7300      block-declaration
7301      function-definition
7302      template-declaration
7303      explicit-instantiation
7304      explicit-specialization
7305      linkage-specification
7306      namespace-definition
7307
7308    GNU extension:
7309
7310    declaration:
7311       __extension__ declaration */
7312
7313 static void
7314 cp_parser_declaration (cp_parser* parser)
7315 {
7316   cp_token token1;
7317   cp_token token2;
7318   int saved_pedantic;
7319   void *p;
7320
7321   /* Check for the `__extension__' keyword.  */
7322   if (cp_parser_extension_opt (parser, &saved_pedantic))
7323     {
7324       /* Parse the qualified declaration.  */
7325       cp_parser_declaration (parser);
7326       /* Restore the PEDANTIC flag.  */
7327       pedantic = saved_pedantic;
7328
7329       return;
7330     }
7331
7332   /* Try to figure out what kind of declaration is present.  */
7333   token1 = *cp_lexer_peek_token (parser->lexer);
7334
7335   if (token1.type != CPP_EOF)
7336     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7337   else
7338     {
7339       token2.type = CPP_EOF;
7340       token2.keyword = RID_MAX;
7341     }
7342
7343   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7344   p = obstack_alloc (&declarator_obstack, 0);
7345
7346   /* If the next token is `extern' and the following token is a string
7347      literal, then we have a linkage specification.  */
7348   if (token1.keyword == RID_EXTERN
7349       && cp_parser_is_string_literal (&token2))
7350     cp_parser_linkage_specification (parser);
7351   /* If the next token is `template', then we have either a template
7352      declaration, an explicit instantiation, or an explicit
7353      specialization.  */
7354   else if (token1.keyword == RID_TEMPLATE)
7355     {
7356       /* `template <>' indicates a template specialization.  */
7357       if (token2.type == CPP_LESS
7358           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7359         cp_parser_explicit_specialization (parser);
7360       /* `template <' indicates a template declaration.  */
7361       else if (token2.type == CPP_LESS)
7362         cp_parser_template_declaration (parser, /*member_p=*/false);
7363       /* Anything else must be an explicit instantiation.  */
7364       else
7365         cp_parser_explicit_instantiation (parser);
7366     }
7367   /* If the next token is `export', then we have a template
7368      declaration.  */
7369   else if (token1.keyword == RID_EXPORT)
7370     cp_parser_template_declaration (parser, /*member_p=*/false);
7371   /* If the next token is `extern', 'static' or 'inline' and the one
7372      after that is `template', we have a GNU extended explicit
7373      instantiation directive.  */
7374   else if (cp_parser_allow_gnu_extensions_p (parser)
7375            && (token1.keyword == RID_EXTERN
7376                || token1.keyword == RID_STATIC
7377                || token1.keyword == RID_INLINE)
7378            && token2.keyword == RID_TEMPLATE)
7379     cp_parser_explicit_instantiation (parser);
7380   /* If the next token is `namespace', check for a named or unnamed
7381      namespace definition.  */
7382   else if (token1.keyword == RID_NAMESPACE
7383            && (/* A named namespace definition.  */
7384                (token2.type == CPP_NAME
7385                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7386                     != CPP_EQ))
7387                /* An unnamed namespace definition.  */
7388                || token2.type == CPP_OPEN_BRACE
7389                || token2.keyword == RID_ATTRIBUTE))
7390     cp_parser_namespace_definition (parser);
7391   /* Objective-C++ declaration/definition.  */
7392   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7393     cp_parser_objc_declaration (parser);
7394   /* We must have either a block declaration or a function
7395      definition.  */
7396   else
7397     /* Try to parse a block-declaration, or a function-definition.  */
7398     cp_parser_block_declaration (parser, /*statement_p=*/false);
7399
7400   /* Free any declarators allocated.  */
7401   obstack_free (&declarator_obstack, p);
7402 }
7403
7404 /* Parse a block-declaration.
7405
7406    block-declaration:
7407      simple-declaration
7408      asm-definition
7409      namespace-alias-definition
7410      using-declaration
7411      using-directive
7412
7413    GNU Extension:
7414
7415    block-declaration:
7416      __extension__ block-declaration
7417      label-declaration
7418
7419    C++0x Extension:
7420
7421    block-declaration:
7422      static_assert-declaration
7423
7424    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7425    part of a declaration-statement.  */
7426
7427 static void
7428 cp_parser_block_declaration (cp_parser *parser,
7429                              bool      statement_p)
7430 {
7431   cp_token *token1;
7432   int saved_pedantic;
7433
7434   /* Check for the `__extension__' keyword.  */
7435   if (cp_parser_extension_opt (parser, &saved_pedantic))
7436     {
7437       /* Parse the qualified declaration.  */
7438       cp_parser_block_declaration (parser, statement_p);
7439       /* Restore the PEDANTIC flag.  */
7440       pedantic = saved_pedantic;
7441
7442       return;
7443     }
7444
7445   /* Peek at the next token to figure out which kind of declaration is
7446      present.  */
7447   token1 = cp_lexer_peek_token (parser->lexer);
7448
7449   /* If the next keyword is `asm', we have an asm-definition.  */
7450   if (token1->keyword == RID_ASM)
7451     {
7452       if (statement_p)
7453         cp_parser_commit_to_tentative_parse (parser);
7454       cp_parser_asm_definition (parser);
7455     }
7456   /* If the next keyword is `namespace', we have a
7457      namespace-alias-definition.  */
7458   else if (token1->keyword == RID_NAMESPACE)
7459     cp_parser_namespace_alias_definition (parser);
7460   /* If the next keyword is `using', we have either a
7461      using-declaration or a using-directive.  */
7462   else if (token1->keyword == RID_USING)
7463     {
7464       cp_token *token2;
7465
7466       if (statement_p)
7467         cp_parser_commit_to_tentative_parse (parser);
7468       /* If the token after `using' is `namespace', then we have a
7469          using-directive.  */
7470       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7471       if (token2->keyword == RID_NAMESPACE)
7472         cp_parser_using_directive (parser);
7473       /* Otherwise, it's a using-declaration.  */
7474       else
7475         cp_parser_using_declaration (parser,
7476                                      /*access_declaration_p=*/false);
7477     }
7478   /* If the next keyword is `__label__' we have a label declaration.  */
7479   else if (token1->keyword == RID_LABEL)
7480     {
7481       if (statement_p)
7482         cp_parser_commit_to_tentative_parse (parser);
7483       cp_parser_label_declaration (parser);
7484     }
7485   /* If the next token is `static_assert' we have a static assertion.  */
7486   else if (token1->keyword == RID_STATIC_ASSERT)
7487     cp_parser_static_assert (parser, /*member_p=*/false);
7488   /* Anything else must be a simple-declaration.  */
7489   else
7490     cp_parser_simple_declaration (parser, !statement_p);
7491 }
7492
7493 /* Parse a simple-declaration.
7494
7495    simple-declaration:
7496      decl-specifier-seq [opt] init-declarator-list [opt] ;
7497
7498    init-declarator-list:
7499      init-declarator
7500      init-declarator-list , init-declarator
7501
7502    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7503    function-definition as a simple-declaration.  */
7504
7505 static void
7506 cp_parser_simple_declaration (cp_parser* parser,
7507                               bool function_definition_allowed_p)
7508 {
7509   cp_decl_specifier_seq decl_specifiers;
7510   int declares_class_or_enum;
7511   bool saw_declarator;
7512
7513   /* Defer access checks until we know what is being declared; the
7514      checks for names appearing in the decl-specifier-seq should be
7515      done as if we were in the scope of the thing being declared.  */
7516   push_deferring_access_checks (dk_deferred);
7517
7518   /* Parse the decl-specifier-seq.  We have to keep track of whether
7519      or not the decl-specifier-seq declares a named class or
7520      enumeration type, since that is the only case in which the
7521      init-declarator-list is allowed to be empty.
7522
7523      [dcl.dcl]
7524
7525      In a simple-declaration, the optional init-declarator-list can be
7526      omitted only when declaring a class or enumeration, that is when
7527      the decl-specifier-seq contains either a class-specifier, an
7528      elaborated-type-specifier, or an enum-specifier.  */
7529   cp_parser_decl_specifier_seq (parser,
7530                                 CP_PARSER_FLAGS_OPTIONAL,
7531                                 &decl_specifiers,
7532                                 &declares_class_or_enum);
7533   /* We no longer need to defer access checks.  */
7534   stop_deferring_access_checks ();
7535
7536   /* In a block scope, a valid declaration must always have a
7537      decl-specifier-seq.  By not trying to parse declarators, we can
7538      resolve the declaration/expression ambiguity more quickly.  */
7539   if (!function_definition_allowed_p
7540       && !decl_specifiers.any_specifiers_p)
7541     {
7542       cp_parser_error (parser, "expected declaration");
7543       goto done;
7544     }
7545
7546   /* If the next two tokens are both identifiers, the code is
7547      erroneous. The usual cause of this situation is code like:
7548
7549        T t;
7550
7551      where "T" should name a type -- but does not.  */
7552   if (!decl_specifiers.type
7553       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7554     {
7555       /* If parsing tentatively, we should commit; we really are
7556          looking at a declaration.  */
7557       cp_parser_commit_to_tentative_parse (parser);
7558       /* Give up.  */
7559       goto done;
7560     }
7561
7562   /* If we have seen at least one decl-specifier, and the next token
7563      is not a parenthesis, then we must be looking at a declaration.
7564      (After "int (" we might be looking at a functional cast.)  */
7565   if (decl_specifiers.any_specifiers_p
7566       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7567     cp_parser_commit_to_tentative_parse (parser);
7568
7569   /* Keep going until we hit the `;' at the end of the simple
7570      declaration.  */
7571   saw_declarator = false;
7572   while (cp_lexer_next_token_is_not (parser->lexer,
7573                                      CPP_SEMICOLON))
7574     {
7575       cp_token *token;
7576       bool function_definition_p;
7577       tree decl;
7578
7579       if (saw_declarator)
7580         {
7581           /* If we are processing next declarator, coma is expected */
7582           token = cp_lexer_peek_token (parser->lexer);
7583           gcc_assert (token->type == CPP_COMMA);
7584           cp_lexer_consume_token (parser->lexer);
7585         }
7586       else
7587         saw_declarator = true;
7588
7589       /* Parse the init-declarator.  */
7590       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7591                                         /*checks=*/NULL,
7592                                         function_definition_allowed_p,
7593                                         /*member_p=*/false,
7594                                         declares_class_or_enum,
7595                                         &function_definition_p);
7596       /* If an error occurred while parsing tentatively, exit quickly.
7597          (That usually happens when in the body of a function; each
7598          statement is treated as a declaration-statement until proven
7599          otherwise.)  */
7600       if (cp_parser_error_occurred (parser))
7601         goto done;
7602       /* Handle function definitions specially.  */
7603       if (function_definition_p)
7604         {
7605           /* If the next token is a `,', then we are probably
7606              processing something like:
7607
7608                void f() {}, *p;
7609
7610              which is erroneous.  */
7611           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7612             error ("mixing declarations and function-definitions is forbidden");
7613           /* Otherwise, we're done with the list of declarators.  */
7614           else
7615             {
7616               pop_deferring_access_checks ();
7617               return;
7618             }
7619         }
7620       /* The next token should be either a `,' or a `;'.  */
7621       token = cp_lexer_peek_token (parser->lexer);
7622       /* If it's a `,', there are more declarators to come.  */
7623       if (token->type == CPP_COMMA)
7624         /* will be consumed next time around */;
7625       /* If it's a `;', we are done.  */
7626       else if (token->type == CPP_SEMICOLON)
7627         break;
7628       /* Anything else is an error.  */
7629       else
7630         {
7631           /* If we have already issued an error message we don't need
7632              to issue another one.  */
7633           if (decl != error_mark_node
7634               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7635             cp_parser_error (parser, "expected %<,%> or %<;%>");
7636           /* Skip tokens until we reach the end of the statement.  */
7637           cp_parser_skip_to_end_of_statement (parser);
7638           /* If the next token is now a `;', consume it.  */
7639           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7640             cp_lexer_consume_token (parser->lexer);
7641           goto done;
7642         }
7643       /* After the first time around, a function-definition is not
7644          allowed -- even if it was OK at first.  For example:
7645
7646            int i, f() {}
7647
7648          is not valid.  */
7649       function_definition_allowed_p = false;
7650     }
7651
7652   /* Issue an error message if no declarators are present, and the
7653      decl-specifier-seq does not itself declare a class or
7654      enumeration.  */
7655   if (!saw_declarator)
7656     {
7657       if (cp_parser_declares_only_class_p (parser))
7658         shadow_tag (&decl_specifiers);
7659       /* Perform any deferred access checks.  */
7660       perform_deferred_access_checks ();
7661     }
7662
7663   /* Consume the `;'.  */
7664   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7665
7666  done:
7667   pop_deferring_access_checks ();
7668 }
7669
7670 /* Parse a decl-specifier-seq.
7671
7672    decl-specifier-seq:
7673      decl-specifier-seq [opt] decl-specifier
7674
7675    decl-specifier:
7676      storage-class-specifier
7677      type-specifier
7678      function-specifier
7679      friend
7680      typedef
7681
7682    GNU Extension:
7683
7684    decl-specifier:
7685      attributes
7686
7687    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7688
7689    The parser flags FLAGS is used to control type-specifier parsing.
7690
7691    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7692    flags:
7693
7694      1: one of the decl-specifiers is an elaborated-type-specifier
7695         (i.e., a type declaration)
7696      2: one of the decl-specifiers is an enum-specifier or a
7697         class-specifier (i.e., a type definition)
7698
7699    */
7700
7701 static void
7702 cp_parser_decl_specifier_seq (cp_parser* parser,
7703                               cp_parser_flags flags,
7704                               cp_decl_specifier_seq *decl_specs,
7705                               int* declares_class_or_enum)
7706 {
7707   bool constructor_possible_p = !parser->in_declarator_p;
7708
7709   /* Clear DECL_SPECS.  */
7710   clear_decl_specs (decl_specs);
7711
7712   /* Assume no class or enumeration type is declared.  */
7713   *declares_class_or_enum = 0;
7714
7715   /* Keep reading specifiers until there are no more to read.  */
7716   while (true)
7717     {
7718       bool constructor_p;
7719       bool found_decl_spec;
7720       cp_token *token;
7721
7722       /* Peek at the next token.  */
7723       token = cp_lexer_peek_token (parser->lexer);
7724       /* Handle attributes.  */
7725       if (token->keyword == RID_ATTRIBUTE)
7726         {
7727           /* Parse the attributes.  */
7728           decl_specs->attributes
7729             = chainon (decl_specs->attributes,
7730                        cp_parser_attributes_opt (parser));
7731           continue;
7732         }
7733       /* Assume we will find a decl-specifier keyword.  */
7734       found_decl_spec = true;
7735       /* If the next token is an appropriate keyword, we can simply
7736          add it to the list.  */
7737       switch (token->keyword)
7738         {
7739           /* decl-specifier:
7740                friend  */
7741         case RID_FRIEND:
7742           if (!at_class_scope_p ())
7743             {
7744               error ("%<friend%> used outside of class");
7745               cp_lexer_purge_token (parser->lexer);
7746             }
7747           else
7748             {
7749               ++decl_specs->specs[(int) ds_friend];
7750               /* Consume the token.  */
7751               cp_lexer_consume_token (parser->lexer);
7752             }
7753           break;
7754
7755           /* function-specifier:
7756                inline
7757                virtual
7758                explicit  */
7759         case RID_INLINE:
7760         case RID_VIRTUAL:
7761         case RID_EXPLICIT:
7762           cp_parser_function_specifier_opt (parser, decl_specs);
7763           break;
7764
7765           /* decl-specifier:
7766                typedef  */
7767         case RID_TYPEDEF:
7768           ++decl_specs->specs[(int) ds_typedef];
7769           /* Consume the token.  */
7770           cp_lexer_consume_token (parser->lexer);
7771           /* A constructor declarator cannot appear in a typedef.  */
7772           constructor_possible_p = false;
7773           /* The "typedef" keyword can only occur in a declaration; we
7774              may as well commit at this point.  */
7775           cp_parser_commit_to_tentative_parse (parser);
7776
7777           if (decl_specs->storage_class != sc_none)
7778             decl_specs->conflicting_specifiers_p = true;
7779           break;
7780
7781           /* storage-class-specifier:
7782                auto
7783                register
7784                static
7785                extern
7786                mutable
7787
7788              GNU Extension:
7789                thread  */
7790         case RID_AUTO:
7791         case RID_REGISTER:
7792         case RID_STATIC:
7793         case RID_EXTERN:
7794         case RID_MUTABLE:
7795           /* Consume the token.  */
7796           cp_lexer_consume_token (parser->lexer);
7797           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7798           break;
7799         case RID_THREAD:
7800           /* Consume the token.  */
7801           cp_lexer_consume_token (parser->lexer);
7802           ++decl_specs->specs[(int) ds_thread];
7803           break;
7804
7805         default:
7806           /* We did not yet find a decl-specifier yet.  */
7807           found_decl_spec = false;
7808           break;
7809         }
7810
7811       /* Constructors are a special case.  The `S' in `S()' is not a
7812          decl-specifier; it is the beginning of the declarator.  */
7813       constructor_p
7814         = (!found_decl_spec
7815            && constructor_possible_p
7816            && (cp_parser_constructor_declarator_p
7817                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7818
7819       /* If we don't have a DECL_SPEC yet, then we must be looking at
7820          a type-specifier.  */
7821       if (!found_decl_spec && !constructor_p)
7822         {
7823           int decl_spec_declares_class_or_enum;
7824           bool is_cv_qualifier;
7825           tree type_spec;
7826
7827           type_spec
7828             = cp_parser_type_specifier (parser, flags,
7829                                         decl_specs,
7830                                         /*is_declaration=*/true,
7831                                         &decl_spec_declares_class_or_enum,
7832                                         &is_cv_qualifier);
7833
7834           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7835
7836           /* If this type-specifier referenced a user-defined type
7837              (a typedef, class-name, etc.), then we can't allow any
7838              more such type-specifiers henceforth.
7839
7840              [dcl.spec]
7841
7842              The longest sequence of decl-specifiers that could
7843              possibly be a type name is taken as the
7844              decl-specifier-seq of a declaration.  The sequence shall
7845              be self-consistent as described below.
7846
7847              [dcl.type]
7848
7849              As a general rule, at most one type-specifier is allowed
7850              in the complete decl-specifier-seq of a declaration.  The
7851              only exceptions are the following:
7852
7853              -- const or volatile can be combined with any other
7854                 type-specifier.
7855
7856              -- signed or unsigned can be combined with char, long,
7857                 short, or int.
7858
7859              -- ..
7860
7861              Example:
7862
7863                typedef char* Pc;
7864                void g (const int Pc);
7865
7866              Here, Pc is *not* part of the decl-specifier seq; it's
7867              the declarator.  Therefore, once we see a type-specifier
7868              (other than a cv-qualifier), we forbid any additional
7869              user-defined types.  We *do* still allow things like `int
7870              int' to be considered a decl-specifier-seq, and issue the
7871              error message later.  */
7872           if (type_spec && !is_cv_qualifier)
7873             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7874           /* A constructor declarator cannot follow a type-specifier.  */
7875           if (type_spec)
7876             {
7877               constructor_possible_p = false;
7878               found_decl_spec = true;
7879             }
7880         }
7881
7882       /* If we still do not have a DECL_SPEC, then there are no more
7883          decl-specifiers.  */
7884       if (!found_decl_spec)
7885         break;
7886
7887       decl_specs->any_specifiers_p = true;
7888       /* After we see one decl-specifier, further decl-specifiers are
7889          always optional.  */
7890       flags |= CP_PARSER_FLAGS_OPTIONAL;
7891     }
7892
7893   cp_parser_check_decl_spec (decl_specs);
7894
7895   /* Don't allow a friend specifier with a class definition.  */
7896   if (decl_specs->specs[(int) ds_friend] != 0
7897       && (*declares_class_or_enum & 2))
7898     error ("class definition may not be declared a friend");
7899 }
7900
7901 /* Parse an (optional) storage-class-specifier.
7902
7903    storage-class-specifier:
7904      auto
7905      register
7906      static
7907      extern
7908      mutable
7909
7910    GNU Extension:
7911
7912    storage-class-specifier:
7913      thread
7914
7915    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7916
7917 static tree
7918 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7919 {
7920   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7921     {
7922     case RID_AUTO:
7923     case RID_REGISTER:
7924     case RID_STATIC:
7925     case RID_EXTERN:
7926     case RID_MUTABLE:
7927     case RID_THREAD:
7928       /* Consume the token.  */
7929       return cp_lexer_consume_token (parser->lexer)->u.value;
7930
7931     default:
7932       return NULL_TREE;
7933     }
7934 }
7935
7936 /* Parse an (optional) function-specifier.
7937
7938    function-specifier:
7939      inline
7940      virtual
7941      explicit
7942
7943    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7944    Updates DECL_SPECS, if it is non-NULL.  */
7945
7946 static tree
7947 cp_parser_function_specifier_opt (cp_parser* parser,
7948                                   cp_decl_specifier_seq *decl_specs)
7949 {
7950   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7951     {
7952     case RID_INLINE:
7953       if (decl_specs)
7954         ++decl_specs->specs[(int) ds_inline];
7955       break;
7956
7957     case RID_VIRTUAL:
7958       /* 14.5.2.3 [temp.mem]
7959
7960          A member function template shall not be virtual.  */
7961       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7962         error ("templates may not be %<virtual%>");
7963       else if (decl_specs)
7964         ++decl_specs->specs[(int) ds_virtual];
7965       break;
7966
7967     case RID_EXPLICIT:
7968       if (decl_specs)
7969         ++decl_specs->specs[(int) ds_explicit];
7970       break;
7971
7972     default:
7973       return NULL_TREE;
7974     }
7975
7976   /* Consume the token.  */
7977   return cp_lexer_consume_token (parser->lexer)->u.value;
7978 }
7979
7980 /* Parse a linkage-specification.
7981
7982    linkage-specification:
7983      extern string-literal { declaration-seq [opt] }
7984      extern string-literal declaration  */
7985
7986 static void
7987 cp_parser_linkage_specification (cp_parser* parser)
7988 {
7989   tree linkage;
7990
7991   /* Look for the `extern' keyword.  */
7992   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7993
7994   /* Look for the string-literal.  */
7995   linkage = cp_parser_string_literal (parser, false, false);
7996
7997   /* Transform the literal into an identifier.  If the literal is a
7998      wide-character string, or contains embedded NULs, then we can't
7999      handle it as the user wants.  */
8000   if (strlen (TREE_STRING_POINTER (linkage))
8001       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8002     {
8003       cp_parser_error (parser, "invalid linkage-specification");
8004       /* Assume C++ linkage.  */
8005       linkage = lang_name_cplusplus;
8006     }
8007   else
8008     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8009
8010   /* We're now using the new linkage.  */
8011   push_lang_context (linkage);
8012
8013   /* If the next token is a `{', then we're using the first
8014      production.  */
8015   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8016     {
8017       /* Consume the `{' token.  */
8018       cp_lexer_consume_token (parser->lexer);
8019       /* Parse the declarations.  */
8020       cp_parser_declaration_seq_opt (parser);
8021       /* Look for the closing `}'.  */
8022       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8023     }
8024   /* Otherwise, there's just one declaration.  */
8025   else
8026     {
8027       bool saved_in_unbraced_linkage_specification_p;
8028
8029       saved_in_unbraced_linkage_specification_p
8030         = parser->in_unbraced_linkage_specification_p;
8031       parser->in_unbraced_linkage_specification_p = true;
8032       cp_parser_declaration (parser);
8033       parser->in_unbraced_linkage_specification_p
8034         = saved_in_unbraced_linkage_specification_p;
8035     }
8036
8037   /* We're done with the linkage-specification.  */
8038   pop_lang_context ();
8039 }
8040
8041 /* Parse a static_assert-declaration.
8042
8043    static_assert-declaration:
8044      static_assert ( constant-expression , string-literal ) ; 
8045
8046    If MEMBER_P, this static_assert is a class member.  */
8047
8048 static void 
8049 cp_parser_static_assert(cp_parser *parser, bool member_p)
8050 {
8051   tree condition;
8052   tree message;
8053   cp_token *token;
8054   location_t saved_loc;
8055
8056   /* Peek at the `static_assert' token so we can keep track of exactly
8057      where the static assertion started.  */
8058   token = cp_lexer_peek_token (parser->lexer);
8059   saved_loc = token->location;
8060
8061   /* Look for the `static_assert' keyword.  */
8062   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8063                                   "`static_assert'"))
8064     return;
8065
8066   /*  We know we are in a static assertion; commit to any tentative
8067       parse.  */
8068   if (cp_parser_parsing_tentatively (parser))
8069     cp_parser_commit_to_tentative_parse (parser);
8070
8071   /* Parse the `(' starting the static assertion condition.  */
8072   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8073
8074   /* Parse the constant-expression.  */
8075   condition = 
8076     cp_parser_constant_expression (parser,
8077                                    /*allow_non_constant_p=*/false,
8078                                    /*non_constant_p=*/NULL);
8079
8080   /* Parse the separating `,'.  */
8081   cp_parser_require (parser, CPP_COMMA, "`,'");
8082
8083   /* Parse the string-literal message.  */
8084   message = cp_parser_string_literal (parser, 
8085                                       /*translate=*/false,
8086                                       /*wide_ok=*/true);
8087
8088   /* A `)' completes the static assertion.  */
8089   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8090     cp_parser_skip_to_closing_parenthesis (parser, 
8091                                            /*recovering=*/true, 
8092                                            /*or_comma=*/false,
8093                                            /*consume_paren=*/true);
8094
8095   /* A semicolon terminates the declaration.  */
8096   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8097
8098   /* Complete the static assertion, which may mean either processing 
8099      the static assert now or saving it for template instantiation.  */
8100   finish_static_assert (condition, message, saved_loc, member_p);
8101 }
8102
8103 /* Special member functions [gram.special] */
8104
8105 /* Parse a conversion-function-id.
8106
8107    conversion-function-id:
8108      operator conversion-type-id
8109
8110    Returns an IDENTIFIER_NODE representing the operator.  */
8111
8112 static tree
8113 cp_parser_conversion_function_id (cp_parser* parser)
8114 {
8115   tree type;
8116   tree saved_scope;
8117   tree saved_qualifying_scope;
8118   tree saved_object_scope;
8119   tree pushed_scope = NULL_TREE;
8120
8121   /* Look for the `operator' token.  */
8122   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8123     return error_mark_node;
8124   /* When we parse the conversion-type-id, the current scope will be
8125      reset.  However, we need that information in able to look up the
8126      conversion function later, so we save it here.  */
8127   saved_scope = parser->scope;
8128   saved_qualifying_scope = parser->qualifying_scope;
8129   saved_object_scope = parser->object_scope;
8130   /* We must enter the scope of the class so that the names of
8131      entities declared within the class are available in the
8132      conversion-type-id.  For example, consider:
8133
8134        struct S {
8135          typedef int I;
8136          operator I();
8137        };
8138
8139        S::operator I() { ... }
8140
8141      In order to see that `I' is a type-name in the definition, we
8142      must be in the scope of `S'.  */
8143   if (saved_scope)
8144     pushed_scope = push_scope (saved_scope);
8145   /* Parse the conversion-type-id.  */
8146   type = cp_parser_conversion_type_id (parser);
8147   /* Leave the scope of the class, if any.  */
8148   if (pushed_scope)
8149     pop_scope (pushed_scope);
8150   /* Restore the saved scope.  */
8151   parser->scope = saved_scope;
8152   parser->qualifying_scope = saved_qualifying_scope;
8153   parser->object_scope = saved_object_scope;
8154   /* If the TYPE is invalid, indicate failure.  */
8155   if (type == error_mark_node)
8156     return error_mark_node;
8157   return mangle_conv_op_name_for_type (type);
8158 }
8159
8160 /* Parse a conversion-type-id:
8161
8162    conversion-type-id:
8163      type-specifier-seq conversion-declarator [opt]
8164
8165    Returns the TYPE specified.  */
8166
8167 static tree
8168 cp_parser_conversion_type_id (cp_parser* parser)
8169 {
8170   tree attributes;
8171   cp_decl_specifier_seq type_specifiers;
8172   cp_declarator *declarator;
8173   tree type_specified;
8174
8175   /* Parse the attributes.  */
8176   attributes = cp_parser_attributes_opt (parser);
8177   /* Parse the type-specifiers.  */
8178   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8179                                 &type_specifiers);
8180   /* If that didn't work, stop.  */
8181   if (type_specifiers.type == error_mark_node)
8182     return error_mark_node;
8183   /* Parse the conversion-declarator.  */
8184   declarator = cp_parser_conversion_declarator_opt (parser);
8185
8186   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8187                                     /*initialized=*/0, &attributes);
8188   if (attributes)
8189     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8190   return type_specified;
8191 }
8192
8193 /* Parse an (optional) conversion-declarator.
8194
8195    conversion-declarator:
8196      ptr-operator conversion-declarator [opt]
8197
8198    */
8199
8200 static cp_declarator *
8201 cp_parser_conversion_declarator_opt (cp_parser* parser)
8202 {
8203   enum tree_code code;
8204   tree class_type;
8205   cp_cv_quals cv_quals;
8206
8207   /* We don't know if there's a ptr-operator next, or not.  */
8208   cp_parser_parse_tentatively (parser);
8209   /* Try the ptr-operator.  */
8210   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8211   /* If it worked, look for more conversion-declarators.  */
8212   if (cp_parser_parse_definitely (parser))
8213     {
8214       cp_declarator *declarator;
8215
8216       /* Parse another optional declarator.  */
8217       declarator = cp_parser_conversion_declarator_opt (parser);
8218
8219       /* Create the representation of the declarator.  */
8220       if (class_type)
8221         declarator = make_ptrmem_declarator (cv_quals, class_type,
8222                                              declarator);
8223       else if (code == INDIRECT_REF)
8224         declarator = make_pointer_declarator (cv_quals, declarator);
8225       else
8226         declarator = make_reference_declarator (cv_quals, declarator);
8227
8228       return declarator;
8229    }
8230
8231   return NULL;
8232 }
8233
8234 /* Parse an (optional) ctor-initializer.
8235
8236    ctor-initializer:
8237      : mem-initializer-list
8238
8239    Returns TRUE iff the ctor-initializer was actually present.  */
8240
8241 static bool
8242 cp_parser_ctor_initializer_opt (cp_parser* parser)
8243 {
8244   /* If the next token is not a `:', then there is no
8245      ctor-initializer.  */
8246   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8247     {
8248       /* Do default initialization of any bases and members.  */
8249       if (DECL_CONSTRUCTOR_P (current_function_decl))
8250         finish_mem_initializers (NULL_TREE);
8251
8252       return false;
8253     }
8254
8255   /* Consume the `:' token.  */
8256   cp_lexer_consume_token (parser->lexer);
8257   /* And the mem-initializer-list.  */
8258   cp_parser_mem_initializer_list (parser);
8259
8260   return true;
8261 }
8262
8263 /* Parse a mem-initializer-list.
8264
8265    mem-initializer-list:
8266      mem-initializer ... [opt]
8267      mem-initializer ... [opt] , mem-initializer-list  */
8268
8269 static void
8270 cp_parser_mem_initializer_list (cp_parser* parser)
8271 {
8272   tree mem_initializer_list = NULL_TREE;
8273
8274   /* Let the semantic analysis code know that we are starting the
8275      mem-initializer-list.  */
8276   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8277     error ("only constructors take base initializers");
8278
8279   /* Loop through the list.  */
8280   while (true)
8281     {
8282       tree mem_initializer;
8283
8284       /* Parse the mem-initializer.  */
8285       mem_initializer = cp_parser_mem_initializer (parser);
8286       /* If the next token is a `...', we're expanding member initializers. */
8287       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8288         {
8289           /* Consume the `...'. */
8290           cp_lexer_consume_token (parser->lexer);
8291
8292           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8293              can be expanded but members cannot. */
8294           if (mem_initializer != error_mark_node
8295               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8296             {
8297               error ("cannot expand initializer for member %<%D%>", 
8298                      TREE_PURPOSE (mem_initializer));
8299               mem_initializer = error_mark_node;
8300             }
8301
8302           /* Construct the pack expansion type. */
8303           if (mem_initializer != error_mark_node)
8304             mem_initializer = make_pack_expansion (mem_initializer);
8305         }
8306       /* Add it to the list, unless it was erroneous.  */
8307       if (mem_initializer != error_mark_node)
8308         {
8309           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8310           mem_initializer_list = mem_initializer;
8311         }
8312       /* If the next token is not a `,', we're done.  */
8313       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8314         break;
8315       /* Consume the `,' token.  */
8316       cp_lexer_consume_token (parser->lexer);
8317     }
8318
8319   /* Perform semantic analysis.  */
8320   if (DECL_CONSTRUCTOR_P (current_function_decl))
8321     finish_mem_initializers (mem_initializer_list);
8322 }
8323
8324 /* Parse a mem-initializer.
8325
8326    mem-initializer:
8327      mem-initializer-id ( expression-list [opt] )
8328
8329    GNU extension:
8330
8331    mem-initializer:
8332      ( expression-list [opt] )
8333
8334    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8335    class) or FIELD_DECL (for a non-static data member) to initialize;
8336    the TREE_VALUE is the expression-list.  An empty initialization
8337    list is represented by void_list_node.  */
8338
8339 static tree
8340 cp_parser_mem_initializer (cp_parser* parser)
8341 {
8342   tree mem_initializer_id;
8343   tree expression_list;
8344   tree member;
8345
8346   /* Find out what is being initialized.  */
8347   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8348     {
8349       pedwarn ("anachronistic old-style base class initializer");
8350       mem_initializer_id = NULL_TREE;
8351     }
8352   else
8353     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8354   member = expand_member_init (mem_initializer_id);
8355   if (member && !DECL_P (member))
8356     in_base_initializer = 1;
8357
8358   expression_list
8359     = cp_parser_parenthesized_expression_list (parser, false,
8360                                                /*cast_p=*/false,
8361                                                /*allow_expansion_p=*/true,
8362                                                /*non_constant_p=*/NULL);
8363   if (expression_list == error_mark_node)
8364     return error_mark_node;
8365   if (!expression_list)
8366     expression_list = void_type_node;
8367
8368   in_base_initializer = 0;
8369
8370   return member ? build_tree_list (member, expression_list) : error_mark_node;
8371 }
8372
8373 /* Parse a mem-initializer-id.
8374
8375    mem-initializer-id:
8376      :: [opt] nested-name-specifier [opt] class-name
8377      identifier
8378
8379    Returns a TYPE indicating the class to be initializer for the first
8380    production.  Returns an IDENTIFIER_NODE indicating the data member
8381    to be initialized for the second production.  */
8382
8383 static tree
8384 cp_parser_mem_initializer_id (cp_parser* parser)
8385 {
8386   bool global_scope_p;
8387   bool nested_name_specifier_p;
8388   bool template_p = false;
8389   tree id;
8390
8391   /* `typename' is not allowed in this context ([temp.res]).  */
8392   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8393     {
8394       error ("keyword %<typename%> not allowed in this context (a qualified "
8395              "member initializer is implicitly a type)");
8396       cp_lexer_consume_token (parser->lexer);
8397     }
8398   /* Look for the optional `::' operator.  */
8399   global_scope_p
8400     = (cp_parser_global_scope_opt (parser,
8401                                    /*current_scope_valid_p=*/false)
8402        != NULL_TREE);
8403   /* Look for the optional nested-name-specifier.  The simplest way to
8404      implement:
8405
8406        [temp.res]
8407
8408        The keyword `typename' is not permitted in a base-specifier or
8409        mem-initializer; in these contexts a qualified name that
8410        depends on a template-parameter is implicitly assumed to be a
8411        type name.
8412
8413      is to assume that we have seen the `typename' keyword at this
8414      point.  */
8415   nested_name_specifier_p
8416     = (cp_parser_nested_name_specifier_opt (parser,
8417                                             /*typename_keyword_p=*/true,
8418                                             /*check_dependency_p=*/true,
8419                                             /*type_p=*/true,
8420                                             /*is_declaration=*/true)
8421        != NULL_TREE);
8422   if (nested_name_specifier_p)
8423     template_p = cp_parser_optional_template_keyword (parser);
8424   /* If there is a `::' operator or a nested-name-specifier, then we
8425      are definitely looking for a class-name.  */
8426   if (global_scope_p || nested_name_specifier_p)
8427     return cp_parser_class_name (parser,
8428                                  /*typename_keyword_p=*/true,
8429                                  /*template_keyword_p=*/template_p,
8430                                  none_type,
8431                                  /*check_dependency_p=*/true,
8432                                  /*class_head_p=*/false,
8433                                  /*is_declaration=*/true);
8434   /* Otherwise, we could also be looking for an ordinary identifier.  */
8435   cp_parser_parse_tentatively (parser);
8436   /* Try a class-name.  */
8437   id = cp_parser_class_name (parser,
8438                              /*typename_keyword_p=*/true,
8439                              /*template_keyword_p=*/false,
8440                              none_type,
8441                              /*check_dependency_p=*/true,
8442                              /*class_head_p=*/false,
8443                              /*is_declaration=*/true);
8444   /* If we found one, we're done.  */
8445   if (cp_parser_parse_definitely (parser))
8446     return id;
8447   /* Otherwise, look for an ordinary identifier.  */
8448   return cp_parser_identifier (parser);
8449 }
8450
8451 /* Overloading [gram.over] */
8452
8453 /* Parse an operator-function-id.
8454
8455    operator-function-id:
8456      operator operator
8457
8458    Returns an IDENTIFIER_NODE for the operator which is a
8459    human-readable spelling of the identifier, e.g., `operator +'.  */
8460
8461 static tree
8462 cp_parser_operator_function_id (cp_parser* parser)
8463 {
8464   /* Look for the `operator' keyword.  */
8465   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8466     return error_mark_node;
8467   /* And then the name of the operator itself.  */
8468   return cp_parser_operator (parser);
8469 }
8470
8471 /* Parse an operator.
8472
8473    operator:
8474      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8475      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8476      || ++ -- , ->* -> () []
8477
8478    GNU Extensions:
8479
8480    operator:
8481      <? >? <?= >?=
8482
8483    Returns an IDENTIFIER_NODE for the operator which is a
8484    human-readable spelling of the identifier, e.g., `operator +'.  */
8485
8486 static tree
8487 cp_parser_operator (cp_parser* parser)
8488 {
8489   tree id = NULL_TREE;
8490   cp_token *token;
8491
8492   /* Peek at the next token.  */
8493   token = cp_lexer_peek_token (parser->lexer);
8494   /* Figure out which operator we have.  */
8495   switch (token->type)
8496     {
8497     case CPP_KEYWORD:
8498       {
8499         enum tree_code op;
8500
8501         /* The keyword should be either `new' or `delete'.  */
8502         if (token->keyword == RID_NEW)
8503           op = NEW_EXPR;
8504         else if (token->keyword == RID_DELETE)
8505           op = DELETE_EXPR;
8506         else
8507           break;
8508
8509         /* Consume the `new' or `delete' token.  */
8510         cp_lexer_consume_token (parser->lexer);
8511
8512         /* Peek at the next token.  */
8513         token = cp_lexer_peek_token (parser->lexer);
8514         /* If it's a `[' token then this is the array variant of the
8515            operator.  */
8516         if (token->type == CPP_OPEN_SQUARE)
8517           {
8518             /* Consume the `[' token.  */
8519             cp_lexer_consume_token (parser->lexer);
8520             /* Look for the `]' token.  */
8521             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8522             id = ansi_opname (op == NEW_EXPR
8523                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8524           }
8525         /* Otherwise, we have the non-array variant.  */
8526         else
8527           id = ansi_opname (op);
8528
8529         return id;
8530       }
8531
8532     case CPP_PLUS:
8533       id = ansi_opname (PLUS_EXPR);
8534       break;
8535
8536     case CPP_MINUS:
8537       id = ansi_opname (MINUS_EXPR);
8538       break;
8539
8540     case CPP_MULT:
8541       id = ansi_opname (MULT_EXPR);
8542       break;
8543
8544     case CPP_DIV:
8545       id = ansi_opname (TRUNC_DIV_EXPR);
8546       break;
8547
8548     case CPP_MOD:
8549       id = ansi_opname (TRUNC_MOD_EXPR);
8550       break;
8551
8552     case CPP_XOR:
8553       id = ansi_opname (BIT_XOR_EXPR);
8554       break;
8555
8556     case CPP_AND:
8557       id = ansi_opname (BIT_AND_EXPR);
8558       break;
8559
8560     case CPP_OR:
8561       id = ansi_opname (BIT_IOR_EXPR);
8562       break;
8563
8564     case CPP_COMPL:
8565       id = ansi_opname (BIT_NOT_EXPR);
8566       break;
8567
8568     case CPP_NOT:
8569       id = ansi_opname (TRUTH_NOT_EXPR);
8570       break;
8571
8572     case CPP_EQ:
8573       id = ansi_assopname (NOP_EXPR);
8574       break;
8575
8576     case CPP_LESS:
8577       id = ansi_opname (LT_EXPR);
8578       break;
8579
8580     case CPP_GREATER:
8581       id = ansi_opname (GT_EXPR);
8582       break;
8583
8584     case CPP_PLUS_EQ:
8585       id = ansi_assopname (PLUS_EXPR);
8586       break;
8587
8588     case CPP_MINUS_EQ:
8589       id = ansi_assopname (MINUS_EXPR);
8590       break;
8591
8592     case CPP_MULT_EQ:
8593       id = ansi_assopname (MULT_EXPR);
8594       break;
8595
8596     case CPP_DIV_EQ:
8597       id = ansi_assopname (TRUNC_DIV_EXPR);
8598       break;
8599
8600     case CPP_MOD_EQ:
8601       id = ansi_assopname (TRUNC_MOD_EXPR);
8602       break;
8603
8604     case CPP_XOR_EQ:
8605       id = ansi_assopname (BIT_XOR_EXPR);
8606       break;
8607
8608     case CPP_AND_EQ:
8609       id = ansi_assopname (BIT_AND_EXPR);
8610       break;
8611
8612     case CPP_OR_EQ:
8613       id = ansi_assopname (BIT_IOR_EXPR);
8614       break;
8615
8616     case CPP_LSHIFT:
8617       id = ansi_opname (LSHIFT_EXPR);
8618       break;
8619
8620     case CPP_RSHIFT:
8621       id = ansi_opname (RSHIFT_EXPR);
8622       break;
8623
8624     case CPP_LSHIFT_EQ:
8625       id = ansi_assopname (LSHIFT_EXPR);
8626       break;
8627
8628     case CPP_RSHIFT_EQ:
8629       id = ansi_assopname (RSHIFT_EXPR);
8630       break;
8631
8632     case CPP_EQ_EQ:
8633       id = ansi_opname (EQ_EXPR);
8634       break;
8635
8636     case CPP_NOT_EQ:
8637       id = ansi_opname (NE_EXPR);
8638       break;
8639
8640     case CPP_LESS_EQ:
8641       id = ansi_opname (LE_EXPR);
8642       break;
8643
8644     case CPP_GREATER_EQ:
8645       id = ansi_opname (GE_EXPR);
8646       break;
8647
8648     case CPP_AND_AND:
8649       id = ansi_opname (TRUTH_ANDIF_EXPR);
8650       break;
8651
8652     case CPP_OR_OR:
8653       id = ansi_opname (TRUTH_ORIF_EXPR);
8654       break;
8655
8656     case CPP_PLUS_PLUS:
8657       id = ansi_opname (POSTINCREMENT_EXPR);
8658       break;
8659
8660     case CPP_MINUS_MINUS:
8661       id = ansi_opname (PREDECREMENT_EXPR);
8662       break;
8663
8664     case CPP_COMMA:
8665       id = ansi_opname (COMPOUND_EXPR);
8666       break;
8667
8668     case CPP_DEREF_STAR:
8669       id = ansi_opname (MEMBER_REF);
8670       break;
8671
8672     case CPP_DEREF:
8673       id = ansi_opname (COMPONENT_REF);
8674       break;
8675
8676     case CPP_OPEN_PAREN:
8677       /* Consume the `('.  */
8678       cp_lexer_consume_token (parser->lexer);
8679       /* Look for the matching `)'.  */
8680       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8681       return ansi_opname (CALL_EXPR);
8682
8683     case CPP_OPEN_SQUARE:
8684       /* Consume the `['.  */
8685       cp_lexer_consume_token (parser->lexer);
8686       /* Look for the matching `]'.  */
8687       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8688       return ansi_opname (ARRAY_REF);
8689
8690     default:
8691       /* Anything else is an error.  */
8692       break;
8693     }
8694
8695   /* If we have selected an identifier, we need to consume the
8696      operator token.  */
8697   if (id)
8698     cp_lexer_consume_token (parser->lexer);
8699   /* Otherwise, no valid operator name was present.  */
8700   else
8701     {
8702       cp_parser_error (parser, "expected operator");
8703       id = error_mark_node;
8704     }
8705
8706   return id;
8707 }
8708
8709 /* Parse a template-declaration.
8710
8711    template-declaration:
8712      export [opt] template < template-parameter-list > declaration
8713
8714    If MEMBER_P is TRUE, this template-declaration occurs within a
8715    class-specifier.
8716
8717    The grammar rule given by the standard isn't correct.  What
8718    is really meant is:
8719
8720    template-declaration:
8721      export [opt] template-parameter-list-seq
8722        decl-specifier-seq [opt] init-declarator [opt] ;
8723      export [opt] template-parameter-list-seq
8724        function-definition
8725
8726    template-parameter-list-seq:
8727      template-parameter-list-seq [opt]
8728      template < template-parameter-list >  */
8729
8730 static void
8731 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8732 {
8733   /* Check for `export'.  */
8734   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8735     {
8736       /* Consume the `export' token.  */
8737       cp_lexer_consume_token (parser->lexer);
8738       /* Warn that we do not support `export'.  */
8739       warning (0, "keyword %<export%> not implemented, and will be ignored");
8740     }
8741
8742   cp_parser_template_declaration_after_export (parser, member_p);
8743 }
8744
8745 /* Parse a template-parameter-list.
8746
8747    template-parameter-list:
8748      template-parameter
8749      template-parameter-list , template-parameter
8750
8751    Returns a TREE_LIST.  Each node represents a template parameter.
8752    The nodes are connected via their TREE_CHAINs.  */
8753
8754 static tree
8755 cp_parser_template_parameter_list (cp_parser* parser)
8756 {
8757   tree parameter_list = NULL_TREE;
8758
8759   begin_template_parm_list ();
8760   while (true)
8761     {
8762       tree parameter;
8763       cp_token *token;
8764       bool is_non_type;
8765       bool is_parameter_pack;
8766
8767       /* Parse the template-parameter.  */
8768       parameter = cp_parser_template_parameter (parser, 
8769                                                 &is_non_type,
8770                                                 &is_parameter_pack);
8771       /* Add it to the list.  */
8772       if (parameter != error_mark_node)
8773         parameter_list = process_template_parm (parameter_list,
8774                                                 parameter,
8775                                                 is_non_type,
8776                                                 is_parameter_pack);
8777       else
8778        {
8779          tree err_parm = build_tree_list (parameter, parameter);
8780          TREE_VALUE (err_parm) = error_mark_node;
8781          parameter_list = chainon (parameter_list, err_parm);
8782        }
8783
8784       /* Peek at the next token.  */
8785       token = cp_lexer_peek_token (parser->lexer);
8786       /* If it's not a `,', we're done.  */
8787       if (token->type != CPP_COMMA)
8788         break;
8789       /* Otherwise, consume the `,' token.  */
8790       cp_lexer_consume_token (parser->lexer);
8791     }
8792
8793   return end_template_parm_list (parameter_list);
8794 }
8795
8796 /* Parse a template-parameter.
8797
8798    template-parameter:
8799      type-parameter
8800      parameter-declaration
8801
8802    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8803    the parameter.  The TREE_PURPOSE is the default value, if any.
8804    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8805    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
8806    set to true iff this parameter is a parameter pack. */
8807
8808 static tree
8809 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
8810                               bool *is_parameter_pack)
8811 {
8812   cp_token *token;
8813   cp_parameter_declarator *parameter_declarator;
8814   tree parm;
8815
8816   /* Assume it is a type parameter or a template parameter.  */
8817   *is_non_type = false;
8818   /* Assume it not a parameter pack. */
8819   *is_parameter_pack = false;
8820   /* Peek at the next token.  */
8821   token = cp_lexer_peek_token (parser->lexer);
8822   /* If it is `class' or `template', we have a type-parameter.  */
8823   if (token->keyword == RID_TEMPLATE)
8824     return cp_parser_type_parameter (parser, is_parameter_pack);
8825   /* If it is `class' or `typename' we do not know yet whether it is a
8826      type parameter or a non-type parameter.  Consider:
8827
8828        template <typename T, typename T::X X> ...
8829
8830      or:
8831
8832        template <class C, class D*> ...
8833
8834      Here, the first parameter is a type parameter, and the second is
8835      a non-type parameter.  We can tell by looking at the token after
8836      the identifier -- if it is a `,', `=', or `>' then we have a type
8837      parameter.  */
8838   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8839     {
8840       /* Peek at the token after `class' or `typename'.  */
8841       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8842       /* If it's an ellipsis, we have a template type parameter
8843          pack. */
8844       if (token->type == CPP_ELLIPSIS)
8845         return cp_parser_type_parameter (parser, is_parameter_pack);
8846       /* If it's an identifier, skip it.  */
8847       if (token->type == CPP_NAME)
8848         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8849       /* Now, see if the token looks like the end of a template
8850          parameter.  */
8851       if (token->type == CPP_COMMA
8852           || token->type == CPP_EQ
8853           || token->type == CPP_GREATER)
8854         return cp_parser_type_parameter (parser, is_parameter_pack);
8855     }
8856
8857   /* Otherwise, it is a non-type parameter.
8858
8859      [temp.param]
8860
8861      When parsing a default template-argument for a non-type
8862      template-parameter, the first non-nested `>' is taken as the end
8863      of the template parameter-list rather than a greater-than
8864      operator.  */
8865   *is_non_type = true;
8866   parameter_declarator
8867      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8868                                         /*parenthesized_p=*/NULL);
8869
8870   /* If the parameter declaration is marked as a parameter pack, set
8871      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
8872      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
8873      grokdeclarator. */
8874   if (parameter_declarator
8875       && parameter_declarator->declarator
8876       && parameter_declarator->declarator->parameter_pack_p)
8877     {
8878       *is_parameter_pack = true;
8879       parameter_declarator->declarator->parameter_pack_p = false;
8880     }
8881
8882   /* If the next token is an ellipsis, and we don't already have it
8883      marked as a parameter pack, then we have a parameter pack (that
8884      has no declarator); */
8885   if (!*is_parameter_pack
8886       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8887     {
8888
8889       /* Consume the `...'. */
8890       cp_lexer_consume_token (parser->lexer);
8891       maybe_warn_variadic_templates ();
8892       
8893       *is_parameter_pack = true;
8894     }
8895
8896   parm = grokdeclarator (parameter_declarator->declarator,
8897                          &parameter_declarator->decl_specifiers,
8898                          PARM, /*initialized=*/0,
8899                          /*attrlist=*/NULL);
8900   if (parm == error_mark_node)
8901     return error_mark_node;
8902
8903   return build_tree_list (parameter_declarator->default_argument, parm);
8904 }
8905
8906 /* Parse a type-parameter.
8907
8908    type-parameter:
8909      class identifier [opt]
8910      class identifier [opt] = type-id
8911      typename identifier [opt]
8912      typename identifier [opt] = type-id
8913      template < template-parameter-list > class identifier [opt]
8914      template < template-parameter-list > class identifier [opt]
8915        = id-expression
8916
8917    GNU Extension (variadic templates):
8918
8919    type-parameter:
8920      class ... identifier [opt]
8921      typename ... identifier [opt]
8922
8923    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8924    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8925    the declaration of the parameter.
8926
8927    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
8928
8929 static tree
8930 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
8931 {
8932   cp_token *token;
8933   tree parameter;
8934
8935   /* Look for a keyword to tell us what kind of parameter this is.  */
8936   token = cp_parser_require (parser, CPP_KEYWORD,
8937                              "`class', `typename', or `template'");
8938   if (!token)
8939     return error_mark_node;
8940
8941   switch (token->keyword)
8942     {
8943     case RID_CLASS:
8944     case RID_TYPENAME:
8945       {
8946         tree identifier;
8947         tree default_argument;
8948
8949         /* If the next token is an ellipsis, we have a template
8950            argument pack. */
8951         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8952           {
8953             /* Consume the `...' token. */
8954             cp_lexer_consume_token (parser->lexer);
8955             maybe_warn_variadic_templates ();
8956
8957             *is_parameter_pack = true;
8958           }
8959
8960         /* If the next token is an identifier, then it names the
8961            parameter.  */
8962         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8963           identifier = cp_parser_identifier (parser);
8964         else
8965           identifier = NULL_TREE;
8966
8967         /* Create the parameter.  */
8968         parameter = finish_template_type_parm (class_type_node, identifier);
8969
8970         /* If the next token is an `=', we have a default argument.  */
8971         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8972           {
8973             /* Consume the `=' token.  */
8974             cp_lexer_consume_token (parser->lexer);
8975             /* Parse the default-argument.  */
8976             push_deferring_access_checks (dk_no_deferred);
8977             default_argument = cp_parser_type_id (parser);
8978
8979             /* Template parameter packs cannot have default
8980                arguments. */
8981             if (*is_parameter_pack)
8982               {
8983                 if (identifier)
8984                   error ("template parameter pack %qD cannot have a default argument", 
8985                          identifier);
8986                 else
8987                   error ("template parameter packs cannot have default arguments");
8988                 default_argument = NULL_TREE;
8989               }
8990             pop_deferring_access_checks ();
8991           }
8992         else
8993           default_argument = NULL_TREE;
8994
8995         /* Create the combined representation of the parameter and the
8996            default argument.  */
8997         parameter = build_tree_list (default_argument, parameter);
8998       }
8999       break;
9000
9001     case RID_TEMPLATE:
9002       {
9003         tree parameter_list;
9004         tree identifier;
9005         tree default_argument;
9006
9007         /* Look for the `<'.  */
9008         cp_parser_require (parser, CPP_LESS, "`<'");
9009         /* Parse the template-parameter-list.  */
9010         parameter_list = cp_parser_template_parameter_list (parser);
9011         /* Look for the `>'.  */
9012         cp_parser_require (parser, CPP_GREATER, "`>'");
9013         /* Look for the `class' keyword.  */
9014         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9015         /* If the next token is an ellipsis, we have a template
9016            argument pack. */
9017         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9018           {
9019             /* Consume the `...' token. */
9020             cp_lexer_consume_token (parser->lexer);
9021             maybe_warn_variadic_templates ();
9022
9023             *is_parameter_pack = true;
9024           }
9025         /* If the next token is an `=', then there is a
9026            default-argument.  If the next token is a `>', we are at
9027            the end of the parameter-list.  If the next token is a `,',
9028            then we are at the end of this parameter.  */
9029         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9030             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9031             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9032           {
9033             identifier = cp_parser_identifier (parser);
9034             /* Treat invalid names as if the parameter were nameless.  */
9035             if (identifier == error_mark_node)
9036               identifier = NULL_TREE;
9037           }
9038         else
9039           identifier = NULL_TREE;
9040
9041         /* Create the template parameter.  */
9042         parameter = finish_template_template_parm (class_type_node,
9043                                                    identifier);
9044
9045         /* If the next token is an `=', then there is a
9046            default-argument.  */
9047         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9048           {
9049             bool is_template;
9050
9051             /* Consume the `='.  */
9052             cp_lexer_consume_token (parser->lexer);
9053             /* Parse the id-expression.  */
9054             push_deferring_access_checks (dk_no_deferred);
9055             default_argument
9056               = cp_parser_id_expression (parser,
9057                                          /*template_keyword_p=*/false,
9058                                          /*check_dependency_p=*/true,
9059                                          /*template_p=*/&is_template,
9060                                          /*declarator_p=*/false,
9061                                          /*optional_p=*/false);
9062             if (TREE_CODE (default_argument) == TYPE_DECL)
9063               /* If the id-expression was a template-id that refers to
9064                  a template-class, we already have the declaration here,
9065                  so no further lookup is needed.  */
9066                  ;
9067             else
9068               /* Look up the name.  */
9069               default_argument
9070                 = cp_parser_lookup_name (parser, default_argument,
9071                                          none_type,
9072                                          /*is_template=*/is_template,
9073                                          /*is_namespace=*/false,
9074                                          /*check_dependency=*/true,
9075                                          /*ambiguous_decls=*/NULL);
9076             /* See if the default argument is valid.  */
9077             default_argument
9078               = check_template_template_default_arg (default_argument);
9079
9080             /* Template parameter packs cannot have default
9081                arguments. */
9082             if (*is_parameter_pack)
9083               {
9084                 if (identifier)
9085                   error ("template parameter pack %qD cannot have a default argument", 
9086                          identifier);
9087                 else
9088                   error ("template parameter packs cannot have default arguments");
9089                 default_argument = NULL_TREE;
9090               }
9091             pop_deferring_access_checks ();
9092           }
9093         else
9094           default_argument = NULL_TREE;
9095
9096         /* Create the combined representation of the parameter and the
9097            default argument.  */
9098         parameter = build_tree_list (default_argument, parameter);
9099       }
9100       break;
9101
9102     default:
9103       gcc_unreachable ();
9104       break;
9105     }
9106
9107   return parameter;
9108 }
9109
9110 /* Parse a template-id.
9111
9112    template-id:
9113      template-name < template-argument-list [opt] >
9114
9115    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9116    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9117    returned.  Otherwise, if the template-name names a function, or set
9118    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9119    names a class, returns a TYPE_DECL for the specialization.
9120
9121    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9122    uninstantiated templates.  */
9123
9124 static tree
9125 cp_parser_template_id (cp_parser *parser,
9126                        bool template_keyword_p,
9127                        bool check_dependency_p,
9128                        bool is_declaration)
9129 {
9130   int i;
9131   tree template;
9132   tree arguments;
9133   tree template_id;
9134   cp_token_position start_of_id = 0;
9135   deferred_access_check *chk;
9136   VEC (deferred_access_check,gc) *access_check;
9137   cp_token *next_token, *next_token_2;
9138   bool is_identifier;
9139
9140   /* If the next token corresponds to a template-id, there is no need
9141      to reparse it.  */
9142   next_token = cp_lexer_peek_token (parser->lexer);
9143   if (next_token->type == CPP_TEMPLATE_ID)
9144     {
9145       struct tree_check *check_value;
9146
9147       /* Get the stored value.  */
9148       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9149       /* Perform any access checks that were deferred.  */
9150       access_check = check_value->checks;
9151       if (access_check)
9152         {
9153           for (i = 0 ;
9154                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9155                ++i)
9156             {
9157               perform_or_defer_access_check (chk->binfo,
9158                                              chk->decl,
9159                                              chk->diag_decl);
9160             }
9161         }
9162       /* Return the stored value.  */
9163       return check_value->value;
9164     }
9165
9166   /* Avoid performing name lookup if there is no possibility of
9167      finding a template-id.  */
9168   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9169       || (next_token->type == CPP_NAME
9170           && !cp_parser_nth_token_starts_template_argument_list_p
9171                (parser, 2)))
9172     {
9173       cp_parser_error (parser, "expected template-id");
9174       return error_mark_node;
9175     }
9176
9177   /* Remember where the template-id starts.  */
9178   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9179     start_of_id = cp_lexer_token_position (parser->lexer, false);
9180
9181   push_deferring_access_checks (dk_deferred);
9182
9183   /* Parse the template-name.  */
9184   is_identifier = false;
9185   template = cp_parser_template_name (parser, template_keyword_p,
9186                                       check_dependency_p,
9187                                       is_declaration,
9188                                       &is_identifier);
9189   if (template == error_mark_node || is_identifier)
9190     {
9191       pop_deferring_access_checks ();
9192       return template;
9193     }
9194
9195   /* If we find the sequence `[:' after a template-name, it's probably
9196      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9197      parse correctly the argument list.  */
9198   next_token = cp_lexer_peek_token (parser->lexer);
9199   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9200   if (next_token->type == CPP_OPEN_SQUARE
9201       && next_token->flags & DIGRAPH
9202       && next_token_2->type == CPP_COLON
9203       && !(next_token_2->flags & PREV_WHITE))
9204     {
9205       cp_parser_parse_tentatively (parser);
9206       /* Change `:' into `::'.  */
9207       next_token_2->type = CPP_SCOPE;
9208       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9209          CPP_LESS.  */
9210       cp_lexer_consume_token (parser->lexer);
9211       /* Parse the arguments.  */
9212       arguments = cp_parser_enclosed_template_argument_list (parser);
9213       if (!cp_parser_parse_definitely (parser))
9214         {
9215           /* If we couldn't parse an argument list, then we revert our changes
9216              and return simply an error. Maybe this is not a template-id
9217              after all.  */
9218           next_token_2->type = CPP_COLON;
9219           cp_parser_error (parser, "expected %<<%>");
9220           pop_deferring_access_checks ();
9221           return error_mark_node;
9222         }
9223       /* Otherwise, emit an error about the invalid digraph, but continue
9224          parsing because we got our argument list.  */
9225       pedwarn ("%<<::%> cannot begin a template-argument list");
9226       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9227               "between %<<%> and %<::%>");
9228       if (!flag_permissive)
9229         {
9230           static bool hint;
9231           if (!hint)
9232             {
9233               inform ("(if you use -fpermissive G++ will accept your code)");
9234               hint = true;
9235             }
9236         }
9237     }
9238   else
9239     {
9240       /* Look for the `<' that starts the template-argument-list.  */
9241       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9242         {
9243           pop_deferring_access_checks ();
9244           return error_mark_node;
9245         }
9246       /* Parse the arguments.  */
9247       arguments = cp_parser_enclosed_template_argument_list (parser);
9248     }
9249
9250   /* Build a representation of the specialization.  */
9251   if (TREE_CODE (template) == IDENTIFIER_NODE)
9252     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9253   else if (DECL_CLASS_TEMPLATE_P (template)
9254            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9255     {
9256       bool entering_scope;
9257       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9258          template (rather than some instantiation thereof) only if
9259          is not nested within some other construct.  For example, in
9260          "template <typename T> void f(T) { A<T>::", A<T> is just an
9261          instantiation of A.  */
9262       entering_scope = (template_parm_scope_p ()
9263                         && cp_lexer_next_token_is (parser->lexer,
9264                                                    CPP_SCOPE));
9265       template_id
9266         = finish_template_type (template, arguments, entering_scope);
9267     }
9268   else
9269     {
9270       /* If it's not a class-template or a template-template, it should be
9271          a function-template.  */
9272       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9273                    || TREE_CODE (template) == OVERLOAD
9274                    || BASELINK_P (template)));
9275
9276       template_id = lookup_template_function (template, arguments);
9277     }
9278
9279   /* If parsing tentatively, replace the sequence of tokens that makes
9280      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9281      should we re-parse the token stream, we will not have to repeat
9282      the effort required to do the parse, nor will we issue duplicate
9283      error messages about problems during instantiation of the
9284      template.  */
9285   if (start_of_id)
9286     {
9287       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9288
9289       /* Reset the contents of the START_OF_ID token.  */
9290       token->type = CPP_TEMPLATE_ID;
9291       /* Retrieve any deferred checks.  Do not pop this access checks yet
9292          so the memory will not be reclaimed during token replacing below.  */
9293       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9294       token->u.tree_check_value->value = template_id;
9295       token->u.tree_check_value->checks = get_deferred_access_checks ();
9296       token->keyword = RID_MAX;
9297
9298       /* Purge all subsequent tokens.  */
9299       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9300
9301       /* ??? Can we actually assume that, if template_id ==
9302          error_mark_node, we will have issued a diagnostic to the
9303          user, as opposed to simply marking the tentative parse as
9304          failed?  */
9305       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9306         error ("parse error in template argument list");
9307     }
9308
9309   pop_deferring_access_checks ();
9310   return template_id;
9311 }
9312
9313 /* Parse a template-name.
9314
9315    template-name:
9316      identifier
9317
9318    The standard should actually say:
9319
9320    template-name:
9321      identifier
9322      operator-function-id
9323
9324    A defect report has been filed about this issue.
9325
9326    A conversion-function-id cannot be a template name because they cannot
9327    be part of a template-id. In fact, looking at this code:
9328
9329    a.operator K<int>()
9330
9331    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9332    It is impossible to call a templated conversion-function-id with an
9333    explicit argument list, since the only allowed template parameter is
9334    the type to which it is converting.
9335
9336    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9337    `template' keyword, in a construction like:
9338
9339      T::template f<3>()
9340
9341    In that case `f' is taken to be a template-name, even though there
9342    is no way of knowing for sure.
9343
9344    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9345    name refers to a set of overloaded functions, at least one of which
9346    is a template, or an IDENTIFIER_NODE with the name of the template,
9347    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9348    names are looked up inside uninstantiated templates.  */
9349
9350 static tree
9351 cp_parser_template_name (cp_parser* parser,
9352                          bool template_keyword_p,
9353                          bool check_dependency_p,
9354                          bool is_declaration,
9355                          bool *is_identifier)
9356 {
9357   tree identifier;
9358   tree decl;
9359   tree fns;
9360
9361   /* If the next token is `operator', then we have either an
9362      operator-function-id or a conversion-function-id.  */
9363   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9364     {
9365       /* We don't know whether we're looking at an
9366          operator-function-id or a conversion-function-id.  */
9367       cp_parser_parse_tentatively (parser);
9368       /* Try an operator-function-id.  */
9369       identifier = cp_parser_operator_function_id (parser);
9370       /* If that didn't work, try a conversion-function-id.  */
9371       if (!cp_parser_parse_definitely (parser))
9372         {
9373           cp_parser_error (parser, "expected template-name");
9374           return error_mark_node;
9375         }
9376     }
9377   /* Look for the identifier.  */
9378   else
9379     identifier = cp_parser_identifier (parser);
9380
9381   /* If we didn't find an identifier, we don't have a template-id.  */
9382   if (identifier == error_mark_node)
9383     return error_mark_node;
9384
9385   /* If the name immediately followed the `template' keyword, then it
9386      is a template-name.  However, if the next token is not `<', then
9387      we do not treat it as a template-name, since it is not being used
9388      as part of a template-id.  This enables us to handle constructs
9389      like:
9390
9391        template <typename T> struct S { S(); };
9392        template <typename T> S<T>::S();
9393
9394      correctly.  We would treat `S' as a template -- if it were `S<T>'
9395      -- but we do not if there is no `<'.  */
9396
9397   if (processing_template_decl
9398       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9399     {
9400       /* In a declaration, in a dependent context, we pretend that the
9401          "template" keyword was present in order to improve error
9402          recovery.  For example, given:
9403
9404            template <typename T> void f(T::X<int>);
9405
9406          we want to treat "X<int>" as a template-id.  */
9407       if (is_declaration
9408           && !template_keyword_p
9409           && parser->scope && TYPE_P (parser->scope)
9410           && check_dependency_p
9411           && dependent_type_p (parser->scope)
9412           /* Do not do this for dtors (or ctors), since they never
9413              need the template keyword before their name.  */
9414           && !constructor_name_p (identifier, parser->scope))
9415         {
9416           cp_token_position start = 0;
9417
9418           /* Explain what went wrong.  */
9419           error ("non-template %qD used as template", identifier);
9420           inform ("use %<%T::template %D%> to indicate that it is a template",
9421                   parser->scope, identifier);
9422           /* If parsing tentatively, find the location of the "<" token.  */
9423           if (cp_parser_simulate_error (parser))
9424             start = cp_lexer_token_position (parser->lexer, true);
9425           /* Parse the template arguments so that we can issue error
9426              messages about them.  */
9427           cp_lexer_consume_token (parser->lexer);
9428           cp_parser_enclosed_template_argument_list (parser);
9429           /* Skip tokens until we find a good place from which to
9430              continue parsing.  */
9431           cp_parser_skip_to_closing_parenthesis (parser,
9432                                                  /*recovering=*/true,
9433                                                  /*or_comma=*/true,
9434                                                  /*consume_paren=*/false);
9435           /* If parsing tentatively, permanently remove the
9436              template argument list.  That will prevent duplicate
9437              error messages from being issued about the missing
9438              "template" keyword.  */
9439           if (start)
9440             cp_lexer_purge_tokens_after (parser->lexer, start);
9441           if (is_identifier)
9442             *is_identifier = true;
9443           return identifier;
9444         }
9445
9446       /* If the "template" keyword is present, then there is generally
9447          no point in doing name-lookup, so we just return IDENTIFIER.
9448          But, if the qualifying scope is non-dependent then we can
9449          (and must) do name-lookup normally.  */
9450       if (template_keyword_p
9451           && (!parser->scope
9452               || (TYPE_P (parser->scope)
9453                   && dependent_type_p (parser->scope))))
9454         return identifier;
9455     }
9456
9457   /* Look up the name.  */
9458   decl = cp_parser_lookup_name (parser, identifier,
9459                                 none_type,
9460                                 /*is_template=*/false,
9461                                 /*is_namespace=*/false,
9462                                 check_dependency_p,
9463                                 /*ambiguous_decls=*/NULL);
9464   decl = maybe_get_template_decl_from_type_decl (decl);
9465
9466   /* If DECL is a template, then the name was a template-name.  */
9467   if (TREE_CODE (decl) == TEMPLATE_DECL)
9468     ;
9469   else
9470     {
9471       tree fn = NULL_TREE;
9472
9473       /* The standard does not explicitly indicate whether a name that
9474          names a set of overloaded declarations, some of which are
9475          templates, is a template-name.  However, such a name should
9476          be a template-name; otherwise, there is no way to form a
9477          template-id for the overloaded templates.  */
9478       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9479       if (TREE_CODE (fns) == OVERLOAD)
9480         for (fn = fns; fn; fn = OVL_NEXT (fn))
9481           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9482             break;
9483
9484       if (!fn)
9485         {
9486           /* The name does not name a template.  */
9487           cp_parser_error (parser, "expected template-name");
9488           return error_mark_node;
9489         }
9490     }
9491
9492   /* If DECL is dependent, and refers to a function, then just return
9493      its name; we will look it up again during template instantiation.  */
9494   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9495     {
9496       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9497       if (TYPE_P (scope) && dependent_type_p (scope))
9498         return identifier;
9499     }
9500
9501   return decl;
9502 }
9503
9504 /* Parse a template-argument-list.
9505
9506    template-argument-list:
9507      template-argument ... [opt]
9508      template-argument-list , template-argument ... [opt]
9509
9510    Returns a TREE_VEC containing the arguments.  */
9511
9512 static tree
9513 cp_parser_template_argument_list (cp_parser* parser)
9514 {
9515   tree fixed_args[10];
9516   unsigned n_args = 0;
9517   unsigned alloced = 10;
9518   tree *arg_ary = fixed_args;
9519   tree vec;
9520   bool saved_in_template_argument_list_p;
9521   bool saved_ice_p;
9522   bool saved_non_ice_p;
9523
9524   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9525   parser->in_template_argument_list_p = true;
9526   /* Even if the template-id appears in an integral
9527      constant-expression, the contents of the argument list do
9528      not.  */
9529   saved_ice_p = parser->integral_constant_expression_p;
9530   parser->integral_constant_expression_p = false;
9531   saved_non_ice_p = parser->non_integral_constant_expression_p;
9532   parser->non_integral_constant_expression_p = false;
9533   /* Parse the arguments.  */
9534   do
9535     {
9536       tree argument;
9537
9538       if (n_args)
9539         /* Consume the comma.  */
9540         cp_lexer_consume_token (parser->lexer);
9541
9542       /* Parse the template-argument.  */
9543       argument = cp_parser_template_argument (parser);
9544
9545       /* If the next token is an ellipsis, we're expanding a template
9546          argument pack. */
9547       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9548         {
9549           /* Consume the `...' token. */
9550           cp_lexer_consume_token (parser->lexer);
9551
9552           /* Make the argument into a TYPE_PACK_EXPANSION or
9553              EXPR_PACK_EXPANSION. */
9554           argument = make_pack_expansion (argument);
9555         }
9556
9557       if (n_args == alloced)
9558         {
9559           alloced *= 2;
9560
9561           if (arg_ary == fixed_args)
9562             {
9563               arg_ary = XNEWVEC (tree, alloced);
9564               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9565             }
9566           else
9567             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9568         }
9569       arg_ary[n_args++] = argument;
9570     }
9571   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9572
9573   vec = make_tree_vec (n_args);
9574
9575   while (n_args--)
9576     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9577
9578   if (arg_ary != fixed_args)
9579     free (arg_ary);
9580   parser->non_integral_constant_expression_p = saved_non_ice_p;
9581   parser->integral_constant_expression_p = saved_ice_p;
9582   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9583   return vec;
9584 }
9585
9586 /* Parse a template-argument.
9587
9588    template-argument:
9589      assignment-expression
9590      type-id
9591      id-expression
9592
9593    The representation is that of an assignment-expression, type-id, or
9594    id-expression -- except that the qualified id-expression is
9595    evaluated, so that the value returned is either a DECL or an
9596    OVERLOAD.
9597
9598    Although the standard says "assignment-expression", it forbids
9599    throw-expressions or assignments in the template argument.
9600    Therefore, we use "conditional-expression" instead.  */
9601
9602 static tree
9603 cp_parser_template_argument (cp_parser* parser)
9604 {
9605   tree argument;
9606   bool template_p;
9607   bool address_p;
9608   bool maybe_type_id = false;
9609   cp_token *token;
9610   cp_id_kind idk;
9611
9612   /* There's really no way to know what we're looking at, so we just
9613      try each alternative in order.
9614
9615        [temp.arg]
9616
9617        In a template-argument, an ambiguity between a type-id and an
9618        expression is resolved to a type-id, regardless of the form of
9619        the corresponding template-parameter.
9620
9621      Therefore, we try a type-id first.  */
9622   cp_parser_parse_tentatively (parser);
9623   argument = cp_parser_type_id (parser);
9624   /* If there was no error parsing the type-id but the next token is a '>>',
9625      we probably found a typo for '> >'. But there are type-id which are
9626      also valid expressions. For instance:
9627
9628      struct X { int operator >> (int); };
9629      template <int V> struct Foo {};
9630      Foo<X () >> 5> r;
9631
9632      Here 'X()' is a valid type-id of a function type, but the user just
9633      wanted to write the expression "X() >> 5". Thus, we remember that we
9634      found a valid type-id, but we still try to parse the argument as an
9635      expression to see what happens.  */
9636   if (!cp_parser_error_occurred (parser)
9637       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9638     {
9639       maybe_type_id = true;
9640       cp_parser_abort_tentative_parse (parser);
9641     }
9642   else
9643     {
9644       /* If the next token isn't a `,' or a `>', then this argument wasn't
9645       really finished. This means that the argument is not a valid
9646       type-id.  */
9647       if (!cp_parser_next_token_ends_template_argument_p (parser))
9648         cp_parser_error (parser, "expected template-argument");
9649       /* If that worked, we're done.  */
9650       if (cp_parser_parse_definitely (parser))
9651         return argument;
9652     }
9653   /* We're still not sure what the argument will be.  */
9654   cp_parser_parse_tentatively (parser);
9655   /* Try a template.  */
9656   argument = cp_parser_id_expression (parser,
9657                                       /*template_keyword_p=*/false,
9658                                       /*check_dependency_p=*/true,
9659                                       &template_p,
9660                                       /*declarator_p=*/false,
9661                                       /*optional_p=*/false);
9662   /* If the next token isn't a `,' or a `>', then this argument wasn't
9663      really finished.  */
9664   if (!cp_parser_next_token_ends_template_argument_p (parser))
9665     cp_parser_error (parser, "expected template-argument");
9666   if (!cp_parser_error_occurred (parser))
9667     {
9668       /* Figure out what is being referred to.  If the id-expression
9669          was for a class template specialization, then we will have a
9670          TYPE_DECL at this point.  There is no need to do name lookup
9671          at this point in that case.  */
9672       if (TREE_CODE (argument) != TYPE_DECL)
9673         argument = cp_parser_lookup_name (parser, argument,
9674                                           none_type,
9675                                           /*is_template=*/template_p,
9676                                           /*is_namespace=*/false,
9677                                           /*check_dependency=*/true,
9678                                           /*ambiguous_decls=*/NULL);
9679       if (TREE_CODE (argument) != TEMPLATE_DECL
9680           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9681         cp_parser_error (parser, "expected template-name");
9682     }
9683   if (cp_parser_parse_definitely (parser))
9684     return argument;
9685   /* It must be a non-type argument.  There permitted cases are given
9686      in [temp.arg.nontype]:
9687
9688      -- an integral constant-expression of integral or enumeration
9689         type; or
9690
9691      -- the name of a non-type template-parameter; or
9692
9693      -- the name of an object or function with external linkage...
9694
9695      -- the address of an object or function with external linkage...
9696
9697      -- a pointer to member...  */
9698   /* Look for a non-type template parameter.  */
9699   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9700     {
9701       cp_parser_parse_tentatively (parser);
9702       argument = cp_parser_primary_expression (parser,
9703                                                /*adress_p=*/false,
9704                                                /*cast_p=*/false,
9705                                                /*template_arg_p=*/true,
9706                                                &idk);
9707       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9708           || !cp_parser_next_token_ends_template_argument_p (parser))
9709         cp_parser_simulate_error (parser);
9710       if (cp_parser_parse_definitely (parser))
9711         return argument;
9712     }
9713
9714   /* If the next token is "&", the argument must be the address of an
9715      object or function with external linkage.  */
9716   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9717   if (address_p)
9718     cp_lexer_consume_token (parser->lexer);
9719   /* See if we might have an id-expression.  */
9720   token = cp_lexer_peek_token (parser->lexer);
9721   if (token->type == CPP_NAME
9722       || token->keyword == RID_OPERATOR
9723       || token->type == CPP_SCOPE
9724       || token->type == CPP_TEMPLATE_ID
9725       || token->type == CPP_NESTED_NAME_SPECIFIER)
9726     {
9727       cp_parser_parse_tentatively (parser);
9728       argument = cp_parser_primary_expression (parser,
9729                                                address_p,
9730                                                /*cast_p=*/false,
9731                                                /*template_arg_p=*/true,
9732                                                &idk);
9733       if (cp_parser_error_occurred (parser)
9734           || !cp_parser_next_token_ends_template_argument_p (parser))
9735         cp_parser_abort_tentative_parse (parser);
9736       else
9737         {
9738           if (TREE_CODE (argument) == INDIRECT_REF)
9739             {
9740               gcc_assert (REFERENCE_REF_P (argument));
9741               argument = TREE_OPERAND (argument, 0);
9742             }
9743
9744           if (TREE_CODE (argument) == VAR_DECL)
9745             {
9746               /* A variable without external linkage might still be a
9747                  valid constant-expression, so no error is issued here
9748                  if the external-linkage check fails.  */
9749               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9750                 cp_parser_simulate_error (parser);
9751             }
9752           else if (is_overloaded_fn (argument))
9753             /* All overloaded functions are allowed; if the external
9754                linkage test does not pass, an error will be issued
9755                later.  */
9756             ;
9757           else if (address_p
9758                    && (TREE_CODE (argument) == OFFSET_REF
9759                        || TREE_CODE (argument) == SCOPE_REF))
9760             /* A pointer-to-member.  */
9761             ;
9762           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9763             ;
9764           else
9765             cp_parser_simulate_error (parser);
9766
9767           if (cp_parser_parse_definitely (parser))
9768             {
9769               if (address_p)
9770                 argument = build_x_unary_op (ADDR_EXPR, argument);
9771               return argument;
9772             }
9773         }
9774     }
9775   /* If the argument started with "&", there are no other valid
9776      alternatives at this point.  */
9777   if (address_p)
9778     {
9779       cp_parser_error (parser, "invalid non-type template argument");
9780       return error_mark_node;
9781     }
9782
9783   /* If the argument wasn't successfully parsed as a type-id followed
9784      by '>>', the argument can only be a constant expression now.
9785      Otherwise, we try parsing the constant-expression tentatively,
9786      because the argument could really be a type-id.  */
9787   if (maybe_type_id)
9788     cp_parser_parse_tentatively (parser);
9789   argument = cp_parser_constant_expression (parser,
9790                                             /*allow_non_constant_p=*/false,
9791                                             /*non_constant_p=*/NULL);
9792   argument = fold_non_dependent_expr (argument);
9793   if (!maybe_type_id)
9794     return argument;
9795   if (!cp_parser_next_token_ends_template_argument_p (parser))
9796     cp_parser_error (parser, "expected template-argument");
9797   if (cp_parser_parse_definitely (parser))
9798     return argument;
9799   /* We did our best to parse the argument as a non type-id, but that
9800      was the only alternative that matched (albeit with a '>' after
9801      it). We can assume it's just a typo from the user, and a
9802      diagnostic will then be issued.  */
9803   return cp_parser_type_id (parser);
9804 }
9805
9806 /* Parse an explicit-instantiation.
9807
9808    explicit-instantiation:
9809      template declaration
9810
9811    Although the standard says `declaration', what it really means is:
9812
9813    explicit-instantiation:
9814      template decl-specifier-seq [opt] declarator [opt] ;
9815
9816    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9817    supposed to be allowed.  A defect report has been filed about this
9818    issue.
9819
9820    GNU Extension:
9821
9822    explicit-instantiation:
9823      storage-class-specifier template
9824        decl-specifier-seq [opt] declarator [opt] ;
9825      function-specifier template
9826        decl-specifier-seq [opt] declarator [opt] ;  */
9827
9828 static void
9829 cp_parser_explicit_instantiation (cp_parser* parser)
9830 {
9831   int declares_class_or_enum;
9832   cp_decl_specifier_seq decl_specifiers;
9833   tree extension_specifier = NULL_TREE;
9834
9835   /* Look for an (optional) storage-class-specifier or
9836      function-specifier.  */
9837   if (cp_parser_allow_gnu_extensions_p (parser))
9838     {
9839       extension_specifier
9840         = cp_parser_storage_class_specifier_opt (parser);
9841       if (!extension_specifier)
9842         extension_specifier
9843           = cp_parser_function_specifier_opt (parser,
9844                                               /*decl_specs=*/NULL);
9845     }
9846
9847   /* Look for the `template' keyword.  */
9848   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9849   /* Let the front end know that we are processing an explicit
9850      instantiation.  */
9851   begin_explicit_instantiation ();
9852   /* [temp.explicit] says that we are supposed to ignore access
9853      control while processing explicit instantiation directives.  */
9854   push_deferring_access_checks (dk_no_check);
9855   /* Parse a decl-specifier-seq.  */
9856   cp_parser_decl_specifier_seq (parser,
9857                                 CP_PARSER_FLAGS_OPTIONAL,
9858                                 &decl_specifiers,
9859                                 &declares_class_or_enum);
9860   /* If there was exactly one decl-specifier, and it declared a class,
9861      and there's no declarator, then we have an explicit type
9862      instantiation.  */
9863   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9864     {
9865       tree type;
9866
9867       type = check_tag_decl (&decl_specifiers);
9868       /* Turn access control back on for names used during
9869          template instantiation.  */
9870       pop_deferring_access_checks ();
9871       if (type)
9872         do_type_instantiation (type, extension_specifier,
9873                                /*complain=*/tf_error);
9874     }
9875   else
9876     {
9877       cp_declarator *declarator;
9878       tree decl;
9879
9880       /* Parse the declarator.  */
9881       declarator
9882         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9883                                 /*ctor_dtor_or_conv_p=*/NULL,
9884                                 /*parenthesized_p=*/NULL,
9885                                 /*member_p=*/false);
9886       if (declares_class_or_enum & 2)
9887         cp_parser_check_for_definition_in_return_type (declarator,
9888                                                        decl_specifiers.type);
9889       if (declarator != cp_error_declarator)
9890         {
9891           decl = grokdeclarator (declarator, &decl_specifiers,
9892                                  NORMAL, 0, &decl_specifiers.attributes);
9893           /* Turn access control back on for names used during
9894              template instantiation.  */
9895           pop_deferring_access_checks ();
9896           /* Do the explicit instantiation.  */
9897           do_decl_instantiation (decl, extension_specifier);
9898         }
9899       else
9900         {
9901           pop_deferring_access_checks ();
9902           /* Skip the body of the explicit instantiation.  */
9903           cp_parser_skip_to_end_of_statement (parser);
9904         }
9905     }
9906   /* We're done with the instantiation.  */
9907   end_explicit_instantiation ();
9908
9909   cp_parser_consume_semicolon_at_end_of_statement (parser);
9910 }
9911
9912 /* Parse an explicit-specialization.
9913
9914    explicit-specialization:
9915      template < > declaration
9916
9917    Although the standard says `declaration', what it really means is:
9918
9919    explicit-specialization:
9920      template <> decl-specifier [opt] init-declarator [opt] ;
9921      template <> function-definition
9922      template <> explicit-specialization
9923      template <> template-declaration  */
9924
9925 static void
9926 cp_parser_explicit_specialization (cp_parser* parser)
9927 {
9928   bool need_lang_pop;
9929   /* Look for the `template' keyword.  */
9930   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9931   /* Look for the `<'.  */
9932   cp_parser_require (parser, CPP_LESS, "`<'");
9933   /* Look for the `>'.  */
9934   cp_parser_require (parser, CPP_GREATER, "`>'");
9935   /* We have processed another parameter list.  */
9936   ++parser->num_template_parameter_lists;
9937   /* [temp]
9938
9939      A template ... explicit specialization ... shall not have C
9940      linkage.  */
9941   if (current_lang_name == lang_name_c)
9942     {
9943       error ("template specialization with C linkage");
9944       /* Give it C++ linkage to avoid confusing other parts of the
9945          front end.  */
9946       push_lang_context (lang_name_cplusplus);
9947       need_lang_pop = true;
9948     }
9949   else
9950     need_lang_pop = false;
9951   /* Let the front end know that we are beginning a specialization.  */
9952   if (!begin_specialization ())
9953     {
9954       end_specialization ();
9955       cp_parser_skip_to_end_of_block_or_statement (parser);
9956       return;
9957     }
9958
9959   /* If the next keyword is `template', we need to figure out whether
9960      or not we're looking a template-declaration.  */
9961   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9962     {
9963       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9964           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9965         cp_parser_template_declaration_after_export (parser,
9966                                                      /*member_p=*/false);
9967       else
9968         cp_parser_explicit_specialization (parser);
9969     }
9970   else
9971     /* Parse the dependent declaration.  */
9972     cp_parser_single_declaration (parser,
9973                                   /*checks=*/NULL,
9974                                   /*member_p=*/false,
9975                                   /*friend_p=*/NULL);
9976   /* We're done with the specialization.  */
9977   end_specialization ();
9978   /* For the erroneous case of a template with C linkage, we pushed an
9979      implicit C++ linkage scope; exit that scope now.  */
9980   if (need_lang_pop)
9981     pop_lang_context ();
9982   /* We're done with this parameter list.  */
9983   --parser->num_template_parameter_lists;
9984 }
9985
9986 /* Parse a type-specifier.
9987
9988    type-specifier:
9989      simple-type-specifier
9990      class-specifier
9991      enum-specifier
9992      elaborated-type-specifier
9993      cv-qualifier
9994
9995    GNU Extension:
9996
9997    type-specifier:
9998      __complex__
9999
10000    Returns a representation of the type-specifier.  For a
10001    class-specifier, enum-specifier, or elaborated-type-specifier, a
10002    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10003
10004    The parser flags FLAGS is used to control type-specifier parsing.
10005
10006    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10007    in a decl-specifier-seq.
10008
10009    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10010    class-specifier, enum-specifier, or elaborated-type-specifier, then
10011    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10012    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10013    zero.
10014
10015    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10016    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10017    is set to FALSE.  */
10018
10019 static tree
10020 cp_parser_type_specifier (cp_parser* parser,
10021                           cp_parser_flags flags,
10022                           cp_decl_specifier_seq *decl_specs,
10023                           bool is_declaration,
10024                           int* declares_class_or_enum,
10025                           bool* is_cv_qualifier)
10026 {
10027   tree type_spec = NULL_TREE;
10028   cp_token *token;
10029   enum rid keyword;
10030   cp_decl_spec ds = ds_last;
10031
10032   /* Assume this type-specifier does not declare a new type.  */
10033   if (declares_class_or_enum)
10034     *declares_class_or_enum = 0;
10035   /* And that it does not specify a cv-qualifier.  */
10036   if (is_cv_qualifier)
10037     *is_cv_qualifier = false;
10038   /* Peek at the next token.  */
10039   token = cp_lexer_peek_token (parser->lexer);
10040
10041   /* If we're looking at a keyword, we can use that to guide the
10042      production we choose.  */
10043   keyword = token->keyword;
10044   switch (keyword)
10045     {
10046     case RID_ENUM:
10047       /* Look for the enum-specifier.  */
10048       type_spec = cp_parser_enum_specifier (parser);
10049       /* If that worked, we're done.  */
10050       if (type_spec)
10051         {
10052           if (declares_class_or_enum)
10053             *declares_class_or_enum = 2;
10054           if (decl_specs)
10055             cp_parser_set_decl_spec_type (decl_specs,
10056                                           type_spec,
10057                                           /*user_defined_p=*/true);
10058           return type_spec;
10059         }
10060       else
10061         goto elaborated_type_specifier;
10062
10063       /* Any of these indicate either a class-specifier, or an
10064          elaborated-type-specifier.  */
10065     case RID_CLASS:
10066     case RID_STRUCT:
10067     case RID_UNION:
10068       /* Parse tentatively so that we can back up if we don't find a
10069          class-specifier.  */
10070       cp_parser_parse_tentatively (parser);
10071       /* Look for the class-specifier.  */
10072       type_spec = cp_parser_class_specifier (parser);
10073       /* If that worked, we're done.  */
10074       if (cp_parser_parse_definitely (parser))
10075         {
10076           if (declares_class_or_enum)
10077             *declares_class_or_enum = 2;
10078           if (decl_specs)
10079             cp_parser_set_decl_spec_type (decl_specs,
10080                                           type_spec,
10081                                           /*user_defined_p=*/true);
10082           return type_spec;
10083         }
10084
10085       /* Fall through.  */
10086     elaborated_type_specifier:
10087       /* We're declaring (not defining) a class or enum.  */
10088       if (declares_class_or_enum)
10089         *declares_class_or_enum = 1;
10090
10091       /* Fall through.  */
10092     case RID_TYPENAME:
10093       /* Look for an elaborated-type-specifier.  */
10094       type_spec
10095         = (cp_parser_elaborated_type_specifier
10096            (parser,
10097             decl_specs && decl_specs->specs[(int) ds_friend],
10098             is_declaration));
10099       if (decl_specs)
10100         cp_parser_set_decl_spec_type (decl_specs,
10101                                       type_spec,
10102                                       /*user_defined_p=*/true);
10103       return type_spec;
10104
10105     case RID_CONST:
10106       ds = ds_const;
10107       if (is_cv_qualifier)
10108         *is_cv_qualifier = true;
10109       break;
10110
10111     case RID_VOLATILE:
10112       ds = ds_volatile;
10113       if (is_cv_qualifier)
10114         *is_cv_qualifier = true;
10115       break;
10116
10117     case RID_RESTRICT:
10118       ds = ds_restrict;
10119       if (is_cv_qualifier)
10120         *is_cv_qualifier = true;
10121       break;
10122
10123     case RID_COMPLEX:
10124       /* The `__complex__' keyword is a GNU extension.  */
10125       ds = ds_complex;
10126       break;
10127
10128     default:
10129       break;
10130     }
10131
10132   /* Handle simple keywords.  */
10133   if (ds != ds_last)
10134     {
10135       if (decl_specs)
10136         {
10137           ++decl_specs->specs[(int)ds];
10138           decl_specs->any_specifiers_p = true;
10139         }
10140       return cp_lexer_consume_token (parser->lexer)->u.value;
10141     }
10142
10143   /* If we do not already have a type-specifier, assume we are looking
10144      at a simple-type-specifier.  */
10145   type_spec = cp_parser_simple_type_specifier (parser,
10146                                                decl_specs,
10147                                                flags);
10148
10149   /* If we didn't find a type-specifier, and a type-specifier was not
10150      optional in this context, issue an error message.  */
10151   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10152     {
10153       cp_parser_error (parser, "expected type specifier");
10154       return error_mark_node;
10155     }
10156
10157   return type_spec;
10158 }
10159
10160 /* Parse a simple-type-specifier.
10161
10162    simple-type-specifier:
10163      :: [opt] nested-name-specifier [opt] type-name
10164      :: [opt] nested-name-specifier template template-id
10165      char
10166      wchar_t
10167      bool
10168      short
10169      int
10170      long
10171      signed
10172      unsigned
10173      float
10174      double
10175      void
10176
10177    GNU Extension:
10178
10179    simple-type-specifier:
10180      __typeof__ unary-expression
10181      __typeof__ ( type-id )
10182
10183    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10184    appropriately updated.  */
10185
10186 static tree
10187 cp_parser_simple_type_specifier (cp_parser* parser,
10188                                  cp_decl_specifier_seq *decl_specs,
10189                                  cp_parser_flags flags)
10190 {
10191   tree type = NULL_TREE;
10192   cp_token *token;
10193
10194   /* Peek at the next token.  */
10195   token = cp_lexer_peek_token (parser->lexer);
10196
10197   /* If we're looking at a keyword, things are easy.  */
10198   switch (token->keyword)
10199     {
10200     case RID_CHAR:
10201       if (decl_specs)
10202         decl_specs->explicit_char_p = true;
10203       type = char_type_node;
10204       break;
10205     case RID_WCHAR:
10206       type = wchar_type_node;
10207       break;
10208     case RID_BOOL:
10209       type = boolean_type_node;
10210       break;
10211     case RID_SHORT:
10212       if (decl_specs)
10213         ++decl_specs->specs[(int) ds_short];
10214       type = short_integer_type_node;
10215       break;
10216     case RID_INT:
10217       if (decl_specs)
10218         decl_specs->explicit_int_p = true;
10219       type = integer_type_node;
10220       break;
10221     case RID_LONG:
10222       if (decl_specs)
10223         ++decl_specs->specs[(int) ds_long];
10224       type = long_integer_type_node;
10225       break;
10226     case RID_SIGNED:
10227       if (decl_specs)
10228         ++decl_specs->specs[(int) ds_signed];
10229       type = integer_type_node;
10230       break;
10231     case RID_UNSIGNED:
10232       if (decl_specs)
10233         ++decl_specs->specs[(int) ds_unsigned];
10234       type = unsigned_type_node;
10235       break;
10236     case RID_FLOAT:
10237       type = float_type_node;
10238       break;
10239     case RID_DOUBLE:
10240       type = double_type_node;
10241       break;
10242     case RID_VOID:
10243       type = void_type_node;
10244       break;
10245
10246     case RID_TYPEOF:
10247       /* Consume the `typeof' token.  */
10248       cp_lexer_consume_token (parser->lexer);
10249       /* Parse the operand to `typeof'.  */
10250       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10251       /* If it is not already a TYPE, take its type.  */
10252       if (!TYPE_P (type))
10253         type = finish_typeof (type);
10254
10255       if (decl_specs)
10256         cp_parser_set_decl_spec_type (decl_specs, type,
10257                                       /*user_defined_p=*/true);
10258
10259       return type;
10260
10261     default:
10262       break;
10263     }
10264
10265   /* If the type-specifier was for a built-in type, we're done.  */
10266   if (type)
10267     {
10268       tree id;
10269
10270       /* Record the type.  */
10271       if (decl_specs
10272           && (token->keyword != RID_SIGNED
10273               && token->keyword != RID_UNSIGNED
10274               && token->keyword != RID_SHORT
10275               && token->keyword != RID_LONG))
10276         cp_parser_set_decl_spec_type (decl_specs,
10277                                       type,
10278                                       /*user_defined=*/false);
10279       if (decl_specs)
10280         decl_specs->any_specifiers_p = true;
10281
10282       /* Consume the token.  */
10283       id = cp_lexer_consume_token (parser->lexer)->u.value;
10284
10285       /* There is no valid C++ program where a non-template type is
10286          followed by a "<".  That usually indicates that the user thought
10287          that the type was a template.  */
10288       cp_parser_check_for_invalid_template_id (parser, type);
10289
10290       return TYPE_NAME (type);
10291     }
10292
10293   /* The type-specifier must be a user-defined type.  */
10294   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10295     {
10296       bool qualified_p;
10297       bool global_p;
10298
10299       /* Don't gobble tokens or issue error messages if this is an
10300          optional type-specifier.  */
10301       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10302         cp_parser_parse_tentatively (parser);
10303
10304       /* Look for the optional `::' operator.  */
10305       global_p
10306         = (cp_parser_global_scope_opt (parser,
10307                                        /*current_scope_valid_p=*/false)
10308            != NULL_TREE);
10309       /* Look for the nested-name specifier.  */
10310       qualified_p
10311         = (cp_parser_nested_name_specifier_opt (parser,
10312                                                 /*typename_keyword_p=*/false,
10313                                                 /*check_dependency_p=*/true,
10314                                                 /*type_p=*/false,
10315                                                 /*is_declaration=*/false)
10316            != NULL_TREE);
10317       /* If we have seen a nested-name-specifier, and the next token
10318          is `template', then we are using the template-id production.  */
10319       if (parser->scope
10320           && cp_parser_optional_template_keyword (parser))
10321         {
10322           /* Look for the template-id.  */
10323           type = cp_parser_template_id (parser,
10324                                         /*template_keyword_p=*/true,
10325                                         /*check_dependency_p=*/true,
10326                                         /*is_declaration=*/false);
10327           /* If the template-id did not name a type, we are out of
10328              luck.  */
10329           if (TREE_CODE (type) != TYPE_DECL)
10330             {
10331               cp_parser_error (parser, "expected template-id for type");
10332               type = NULL_TREE;
10333             }
10334         }
10335       /* Otherwise, look for a type-name.  */
10336       else
10337         type = cp_parser_type_name (parser);
10338       /* Keep track of all name-lookups performed in class scopes.  */
10339       if (type
10340           && !global_p
10341           && !qualified_p
10342           && TREE_CODE (type) == TYPE_DECL
10343           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10344         maybe_note_name_used_in_class (DECL_NAME (type), type);
10345       /* If it didn't work out, we don't have a TYPE.  */
10346       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10347           && !cp_parser_parse_definitely (parser))
10348         type = NULL_TREE;
10349       if (type && decl_specs)
10350         cp_parser_set_decl_spec_type (decl_specs, type,
10351                                       /*user_defined=*/true);
10352     }
10353
10354   /* If we didn't get a type-name, issue an error message.  */
10355   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10356     {
10357       cp_parser_error (parser, "expected type-name");
10358       return error_mark_node;
10359     }
10360
10361   /* There is no valid C++ program where a non-template type is
10362      followed by a "<".  That usually indicates that the user thought
10363      that the type was a template.  */
10364   if (type && type != error_mark_node)
10365     {
10366       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10367          If it is, then the '<'...'>' enclose protocol names rather than
10368          template arguments, and so everything is fine.  */
10369       if (c_dialect_objc ()
10370           && (objc_is_id (type) || objc_is_class_name (type)))
10371         {
10372           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10373           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10374
10375           /* Clobber the "unqualified" type previously entered into
10376              DECL_SPECS with the new, improved protocol-qualified version.  */
10377           if (decl_specs)
10378             decl_specs->type = qual_type;
10379
10380           return qual_type;
10381         }
10382
10383       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10384     }
10385
10386   return type;
10387 }
10388
10389 /* Parse a type-name.
10390
10391    type-name:
10392      class-name
10393      enum-name
10394      typedef-name
10395
10396    enum-name:
10397      identifier
10398
10399    typedef-name:
10400      identifier
10401
10402    Returns a TYPE_DECL for the type.  */
10403
10404 static tree
10405 cp_parser_type_name (cp_parser* parser)
10406 {
10407   tree type_decl;
10408   tree identifier;
10409
10410   /* We can't know yet whether it is a class-name or not.  */
10411   cp_parser_parse_tentatively (parser);
10412   /* Try a class-name.  */
10413   type_decl = cp_parser_class_name (parser,
10414                                     /*typename_keyword_p=*/false,
10415                                     /*template_keyword_p=*/false,
10416                                     none_type,
10417                                     /*check_dependency_p=*/true,
10418                                     /*class_head_p=*/false,
10419                                     /*is_declaration=*/false);
10420   /* If it's not a class-name, keep looking.  */
10421   if (!cp_parser_parse_definitely (parser))
10422     {
10423       /* It must be a typedef-name or an enum-name.  */
10424       identifier = cp_parser_identifier (parser);
10425       if (identifier == error_mark_node)
10426         return error_mark_node;
10427
10428       /* Look up the type-name.  */
10429       type_decl = cp_parser_lookup_name_simple (parser, identifier);
10430
10431       if (TREE_CODE (type_decl) != TYPE_DECL
10432           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10433         {
10434           /* See if this is an Objective-C type.  */
10435           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10436           tree type = objc_get_protocol_qualified_type (identifier, protos);
10437           if (type)
10438             type_decl = TYPE_NAME (type);
10439         }
10440
10441       /* Issue an error if we did not find a type-name.  */
10442       if (TREE_CODE (type_decl) != TYPE_DECL)
10443         {
10444           if (!cp_parser_simulate_error (parser))
10445             cp_parser_name_lookup_error (parser, identifier, type_decl,
10446                                          "is not a type");
10447           type_decl = error_mark_node;
10448         }
10449       /* Remember that the name was used in the definition of the
10450          current class so that we can check later to see if the
10451          meaning would have been different after the class was
10452          entirely defined.  */
10453       else if (type_decl != error_mark_node
10454                && !parser->scope)
10455         maybe_note_name_used_in_class (identifier, type_decl);
10456     }
10457
10458   return type_decl;
10459 }
10460
10461
10462 /* Parse an elaborated-type-specifier.  Note that the grammar given
10463    here incorporates the resolution to DR68.
10464
10465    elaborated-type-specifier:
10466      class-key :: [opt] nested-name-specifier [opt] identifier
10467      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10468      enum :: [opt] nested-name-specifier [opt] identifier
10469      typename :: [opt] nested-name-specifier identifier
10470      typename :: [opt] nested-name-specifier template [opt]
10471        template-id
10472
10473    GNU extension:
10474
10475    elaborated-type-specifier:
10476      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10477      class-key attributes :: [opt] nested-name-specifier [opt]
10478                template [opt] template-id
10479      enum attributes :: [opt] nested-name-specifier [opt] identifier
10480
10481    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10482    declared `friend'.  If IS_DECLARATION is TRUE, then this
10483    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10484    something is being declared.
10485
10486    Returns the TYPE specified.  */
10487
10488 static tree
10489 cp_parser_elaborated_type_specifier (cp_parser* parser,
10490                                      bool is_friend,
10491                                      bool is_declaration)
10492 {
10493   enum tag_types tag_type;
10494   tree identifier;
10495   tree type = NULL_TREE;
10496   tree attributes = NULL_TREE;
10497
10498   /* See if we're looking at the `enum' keyword.  */
10499   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10500     {
10501       /* Consume the `enum' token.  */
10502       cp_lexer_consume_token (parser->lexer);
10503       /* Remember that it's an enumeration type.  */
10504       tag_type = enum_type;
10505       /* Parse the attributes.  */
10506       attributes = cp_parser_attributes_opt (parser);
10507     }
10508   /* Or, it might be `typename'.  */
10509   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10510                                            RID_TYPENAME))
10511     {
10512       /* Consume the `typename' token.  */
10513       cp_lexer_consume_token (parser->lexer);
10514       /* Remember that it's a `typename' type.  */
10515       tag_type = typename_type;
10516       /* The `typename' keyword is only allowed in templates.  */
10517       if (!processing_template_decl)
10518         pedwarn ("using %<typename%> outside of template");
10519     }
10520   /* Otherwise it must be a class-key.  */
10521   else
10522     {
10523       tag_type = cp_parser_class_key (parser);
10524       if (tag_type == none_type)
10525         return error_mark_node;
10526       /* Parse the attributes.  */
10527       attributes = cp_parser_attributes_opt (parser);
10528     }
10529
10530   /* Look for the `::' operator.  */
10531   cp_parser_global_scope_opt (parser,
10532                               /*current_scope_valid_p=*/false);
10533   /* Look for the nested-name-specifier.  */
10534   if (tag_type == typename_type)
10535     {
10536       if (!cp_parser_nested_name_specifier (parser,
10537                                            /*typename_keyword_p=*/true,
10538                                            /*check_dependency_p=*/true,
10539                                            /*type_p=*/true,
10540                                             is_declaration))
10541         return error_mark_node;
10542     }
10543   else
10544     /* Even though `typename' is not present, the proposed resolution
10545        to Core Issue 180 says that in `class A<T>::B', `B' should be
10546        considered a type-name, even if `A<T>' is dependent.  */
10547     cp_parser_nested_name_specifier_opt (parser,
10548                                          /*typename_keyword_p=*/true,
10549                                          /*check_dependency_p=*/true,
10550                                          /*type_p=*/true,
10551                                          is_declaration);
10552  /* For everything but enumeration types, consider a template-id.
10553     For an enumeration type, consider only a plain identifier.  */
10554   if (tag_type != enum_type)
10555     {
10556       bool template_p = false;
10557       tree decl;
10558
10559       /* Allow the `template' keyword.  */
10560       template_p = cp_parser_optional_template_keyword (parser);
10561       /* If we didn't see `template', we don't know if there's a
10562          template-id or not.  */
10563       if (!template_p)
10564         cp_parser_parse_tentatively (parser);
10565       /* Parse the template-id.  */
10566       decl = cp_parser_template_id (parser, template_p,
10567                                     /*check_dependency_p=*/true,
10568                                     is_declaration);
10569       /* If we didn't find a template-id, look for an ordinary
10570          identifier.  */
10571       if (!template_p && !cp_parser_parse_definitely (parser))
10572         ;
10573       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10574          in effect, then we must assume that, upon instantiation, the
10575          template will correspond to a class.  */
10576       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10577                && tag_type == typename_type)
10578         type = make_typename_type (parser->scope, decl,
10579                                    typename_type,
10580                                    /*complain=*/tf_error);
10581       else
10582         type = TREE_TYPE (decl);
10583     }
10584
10585   if (!type)
10586     {
10587       identifier = cp_parser_identifier (parser);
10588
10589       if (identifier == error_mark_node)
10590         {
10591           parser->scope = NULL_TREE;
10592           return error_mark_node;
10593         }
10594
10595       /* For a `typename', we needn't call xref_tag.  */
10596       if (tag_type == typename_type
10597           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10598         return cp_parser_make_typename_type (parser, parser->scope,
10599                                              identifier);
10600       /* Look up a qualified name in the usual way.  */
10601       if (parser->scope)
10602         {
10603           tree decl;
10604
10605           decl = cp_parser_lookup_name (parser, identifier,
10606                                         tag_type,
10607                                         /*is_template=*/false,
10608                                         /*is_namespace=*/false,
10609                                         /*check_dependency=*/true,
10610                                         /*ambiguous_decls=*/NULL);
10611
10612           /* If we are parsing friend declaration, DECL may be a
10613              TEMPLATE_DECL tree node here.  However, we need to check
10614              whether this TEMPLATE_DECL results in valid code.  Consider
10615              the following example:
10616
10617                namespace N {
10618                  template <class T> class C {};
10619                }
10620                class X {
10621                  template <class T> friend class N::C; // #1, valid code
10622                };
10623                template <class T> class Y {
10624                  friend class N::C;                    // #2, invalid code
10625                };
10626
10627              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10628              name lookup of `N::C'.  We see that friend declaration must
10629              be template for the code to be valid.  Note that
10630              processing_template_decl does not work here since it is
10631              always 1 for the above two cases.  */
10632
10633           decl = (cp_parser_maybe_treat_template_as_class
10634                   (decl, /*tag_name_p=*/is_friend
10635                          && parser->num_template_parameter_lists));
10636
10637           if (TREE_CODE (decl) != TYPE_DECL)
10638             {
10639               cp_parser_diagnose_invalid_type_name (parser,
10640                                                     parser->scope,
10641                                                     identifier);
10642               return error_mark_node;
10643             }
10644
10645           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10646             {
10647               bool allow_template = (parser->num_template_parameter_lists
10648                                       || DECL_SELF_REFERENCE_P (decl));
10649               type = check_elaborated_type_specifier (tag_type, decl, 
10650                                                       allow_template);
10651
10652               if (type == error_mark_node)
10653                 return error_mark_node;
10654             }
10655
10656           type = TREE_TYPE (decl);
10657         }
10658       else
10659         {
10660           /* An elaborated-type-specifier sometimes introduces a new type and
10661              sometimes names an existing type.  Normally, the rule is that it
10662              introduces a new type only if there is not an existing type of
10663              the same name already in scope.  For example, given:
10664
10665                struct S {};
10666                void f() { struct S s; }
10667
10668              the `struct S' in the body of `f' is the same `struct S' as in
10669              the global scope; the existing definition is used.  However, if
10670              there were no global declaration, this would introduce a new
10671              local class named `S'.
10672
10673              An exception to this rule applies to the following code:
10674
10675                namespace N { struct S; }
10676
10677              Here, the elaborated-type-specifier names a new type
10678              unconditionally; even if there is already an `S' in the
10679              containing scope this declaration names a new type.
10680              This exception only applies if the elaborated-type-specifier
10681              forms the complete declaration:
10682
10683                [class.name]
10684
10685                A declaration consisting solely of `class-key identifier ;' is
10686                either a redeclaration of the name in the current scope or a
10687                forward declaration of the identifier as a class name.  It
10688                introduces the name into the current scope.
10689
10690              We are in this situation precisely when the next token is a `;'.
10691
10692              An exception to the exception is that a `friend' declaration does
10693              *not* name a new type; i.e., given:
10694
10695                struct S { friend struct T; };
10696
10697              `T' is not a new type in the scope of `S'.
10698
10699              Also, `new struct S' or `sizeof (struct S)' never results in the
10700              definition of a new type; a new type can only be declared in a
10701              declaration context.  */
10702
10703           tag_scope ts;
10704           bool template_p;
10705
10706           if (is_friend)
10707             /* Friends have special name lookup rules.  */
10708             ts = ts_within_enclosing_non_class;
10709           else if (is_declaration
10710                    && cp_lexer_next_token_is (parser->lexer,
10711                                               CPP_SEMICOLON))
10712             /* This is a `class-key identifier ;' */
10713             ts = ts_current;
10714           else
10715             ts = ts_global;
10716
10717           template_p =
10718             (parser->num_template_parameter_lists
10719              && (cp_parser_next_token_starts_class_definition_p (parser)
10720                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10721           /* An unqualified name was used to reference this type, so
10722              there were no qualifying templates.  */
10723           if (!cp_parser_check_template_parameters (parser,
10724                                                     /*num_templates=*/0))
10725             return error_mark_node;
10726           type = xref_tag (tag_type, identifier, ts, template_p);
10727         }
10728     }
10729
10730   if (type == error_mark_node)
10731     return error_mark_node;
10732
10733   /* Allow attributes on forward declarations of classes.  */
10734   if (attributes)
10735     {
10736       if (TREE_CODE (type) == TYPENAME_TYPE)
10737         warning (OPT_Wattributes,
10738                  "attributes ignored on uninstantiated type");
10739       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10740                && ! processing_explicit_instantiation)
10741         warning (OPT_Wattributes,
10742                  "attributes ignored on template instantiation");
10743       else if (is_declaration && cp_parser_declares_only_class_p (parser))
10744         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10745       else
10746         warning (OPT_Wattributes,
10747                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10748     }
10749
10750   if (tag_type != enum_type)
10751     cp_parser_check_class_key (tag_type, type);
10752
10753   /* A "<" cannot follow an elaborated type specifier.  If that
10754      happens, the user was probably trying to form a template-id.  */
10755   cp_parser_check_for_invalid_template_id (parser, type);
10756
10757   return type;
10758 }
10759
10760 /* Parse an enum-specifier.
10761
10762    enum-specifier:
10763      enum identifier [opt] { enumerator-list [opt] }
10764
10765    GNU Extensions:
10766      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10767        attributes[opt]
10768
10769    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10770    if the token stream isn't an enum-specifier after all.  */
10771
10772 static tree
10773 cp_parser_enum_specifier (cp_parser* parser)
10774 {
10775   tree identifier;
10776   tree type;
10777   tree attributes;
10778
10779   /* Parse tentatively so that we can back up if we don't find a
10780      enum-specifier.  */
10781   cp_parser_parse_tentatively (parser);
10782
10783   /* Caller guarantees that the current token is 'enum', an identifier
10784      possibly follows, and the token after that is an opening brace.
10785      If we don't have an identifier, fabricate an anonymous name for
10786      the enumeration being defined.  */
10787   cp_lexer_consume_token (parser->lexer);
10788
10789   attributes = cp_parser_attributes_opt (parser);
10790
10791   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10792     identifier = cp_parser_identifier (parser);
10793   else
10794     identifier = make_anon_name ();
10795
10796   /* Look for the `{' but don't consume it yet.  */
10797   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10798     cp_parser_simulate_error (parser);
10799
10800   if (!cp_parser_parse_definitely (parser))
10801     return NULL_TREE;
10802
10803   /* Issue an error message if type-definitions are forbidden here.  */
10804   if (!cp_parser_check_type_definition (parser))
10805     type = error_mark_node;
10806   else
10807     /* Create the new type.  We do this before consuming the opening
10808        brace so the enum will be recorded as being on the line of its
10809        tag (or the 'enum' keyword, if there is no tag).  */
10810     type = start_enum (identifier);
10811   
10812   /* Consume the opening brace.  */
10813   cp_lexer_consume_token (parser->lexer);
10814
10815   if (type == error_mark_node)
10816     {
10817       cp_parser_skip_to_end_of_block_or_statement (parser);
10818       return error_mark_node;
10819     }
10820
10821   /* If the next token is not '}', then there are some enumerators.  */
10822   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10823     cp_parser_enumerator_list (parser, type);
10824
10825   /* Consume the final '}'.  */
10826   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10827
10828   /* Look for trailing attributes to apply to this enumeration, and
10829      apply them if appropriate.  */
10830   if (cp_parser_allow_gnu_extensions_p (parser))
10831     {
10832       tree trailing_attr = cp_parser_attributes_opt (parser);
10833       cplus_decl_attributes (&type,
10834                              trailing_attr,
10835                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10836     }
10837
10838   /* Finish up the enumeration.  */
10839   finish_enum (type);
10840
10841   return type;
10842 }
10843
10844 /* Parse an enumerator-list.  The enumerators all have the indicated
10845    TYPE.
10846
10847    enumerator-list:
10848      enumerator-definition
10849      enumerator-list , enumerator-definition  */
10850
10851 static void
10852 cp_parser_enumerator_list (cp_parser* parser, tree type)
10853 {
10854   while (true)
10855     {
10856       /* Parse an enumerator-definition.  */
10857       cp_parser_enumerator_definition (parser, type);
10858
10859       /* If the next token is not a ',', we've reached the end of
10860          the list.  */
10861       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10862         break;
10863       /* Otherwise, consume the `,' and keep going.  */
10864       cp_lexer_consume_token (parser->lexer);
10865       /* If the next token is a `}', there is a trailing comma.  */
10866       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10867         {
10868           if (pedantic && !in_system_header)
10869             pedwarn ("comma at end of enumerator list");
10870           break;
10871         }
10872     }
10873 }
10874
10875 /* Parse an enumerator-definition.  The enumerator has the indicated
10876    TYPE.
10877
10878    enumerator-definition:
10879      enumerator
10880      enumerator = constant-expression
10881
10882    enumerator:
10883      identifier  */
10884
10885 static void
10886 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10887 {
10888   tree identifier;
10889   tree value;
10890
10891   /* Look for the identifier.  */
10892   identifier = cp_parser_identifier (parser);
10893   if (identifier == error_mark_node)
10894     return;
10895
10896   /* If the next token is an '=', then there is an explicit value.  */
10897   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10898     {
10899       /* Consume the `=' token.  */
10900       cp_lexer_consume_token (parser->lexer);
10901       /* Parse the value.  */
10902       value = cp_parser_constant_expression (parser,
10903                                              /*allow_non_constant_p=*/false,
10904                                              NULL);
10905     }
10906   else
10907     value = NULL_TREE;
10908
10909   /* Create the enumerator.  */
10910   build_enumerator (identifier, value, type);
10911 }
10912
10913 /* Parse a namespace-name.
10914
10915    namespace-name:
10916      original-namespace-name
10917      namespace-alias
10918
10919    Returns the NAMESPACE_DECL for the namespace.  */
10920
10921 static tree
10922 cp_parser_namespace_name (cp_parser* parser)
10923 {
10924   tree identifier;
10925   tree namespace_decl;
10926
10927   /* Get the name of the namespace.  */
10928   identifier = cp_parser_identifier (parser);
10929   if (identifier == error_mark_node)
10930     return error_mark_node;
10931
10932   /* Look up the identifier in the currently active scope.  Look only
10933      for namespaces, due to:
10934
10935        [basic.lookup.udir]
10936
10937        When looking up a namespace-name in a using-directive or alias
10938        definition, only namespace names are considered.
10939
10940      And:
10941
10942        [basic.lookup.qual]
10943
10944        During the lookup of a name preceding the :: scope resolution
10945        operator, object, function, and enumerator names are ignored.
10946
10947      (Note that cp_parser_class_or_namespace_name only calls this
10948      function if the token after the name is the scope resolution
10949      operator.)  */
10950   namespace_decl = cp_parser_lookup_name (parser, identifier,
10951                                           none_type,
10952                                           /*is_template=*/false,
10953                                           /*is_namespace=*/true,
10954                                           /*check_dependency=*/true,
10955                                           /*ambiguous_decls=*/NULL);
10956   /* If it's not a namespace, issue an error.  */
10957   if (namespace_decl == error_mark_node
10958       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10959     {
10960       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10961         error ("%qD is not a namespace-name", identifier);
10962       cp_parser_error (parser, "expected namespace-name");
10963       namespace_decl = error_mark_node;
10964     }
10965
10966   return namespace_decl;
10967 }
10968
10969 /* Parse a namespace-definition.
10970
10971    namespace-definition:
10972      named-namespace-definition
10973      unnamed-namespace-definition
10974
10975    named-namespace-definition:
10976      original-namespace-definition
10977      extension-namespace-definition
10978
10979    original-namespace-definition:
10980      namespace identifier { namespace-body }
10981
10982    extension-namespace-definition:
10983      namespace original-namespace-name { namespace-body }
10984
10985    unnamed-namespace-definition:
10986      namespace { namespace-body } */
10987
10988 static void
10989 cp_parser_namespace_definition (cp_parser* parser)
10990 {
10991   tree identifier, attribs;
10992
10993   /* Look for the `namespace' keyword.  */
10994   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10995
10996   /* Get the name of the namespace.  We do not attempt to distinguish
10997      between an original-namespace-definition and an
10998      extension-namespace-definition at this point.  The semantic
10999      analysis routines are responsible for that.  */
11000   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11001     identifier = cp_parser_identifier (parser);
11002   else
11003     identifier = NULL_TREE;
11004
11005   /* Parse any specified attributes.  */
11006   attribs = cp_parser_attributes_opt (parser);
11007
11008   /* Look for the `{' to start the namespace.  */
11009   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11010   /* Start the namespace.  */
11011   push_namespace_with_attribs (identifier, attribs);
11012   /* Parse the body of the namespace.  */
11013   cp_parser_namespace_body (parser);
11014   /* Finish the namespace.  */
11015   pop_namespace ();
11016   /* Look for the final `}'.  */
11017   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11018 }
11019
11020 /* Parse a namespace-body.
11021
11022    namespace-body:
11023      declaration-seq [opt]  */
11024
11025 static void
11026 cp_parser_namespace_body (cp_parser* parser)
11027 {
11028   cp_parser_declaration_seq_opt (parser);
11029 }
11030
11031 /* Parse a namespace-alias-definition.
11032
11033    namespace-alias-definition:
11034      namespace identifier = qualified-namespace-specifier ;  */
11035
11036 static void
11037 cp_parser_namespace_alias_definition (cp_parser* parser)
11038 {
11039   tree identifier;
11040   tree namespace_specifier;
11041
11042   /* Look for the `namespace' keyword.  */
11043   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11044   /* Look for the identifier.  */
11045   identifier = cp_parser_identifier (parser);
11046   if (identifier == error_mark_node)
11047     return;
11048   /* Look for the `=' token.  */
11049   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11050       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11051     {
11052       error ("%<namespace%> definition is not allowed here");
11053       /* Skip the definition.  */
11054       cp_lexer_consume_token (parser->lexer);
11055       cp_parser_skip_to_closing_brace (parser);
11056       cp_lexer_consume_token (parser->lexer);
11057       return;
11058     }
11059   cp_parser_require (parser, CPP_EQ, "`='");
11060   /* Look for the qualified-namespace-specifier.  */
11061   namespace_specifier
11062     = cp_parser_qualified_namespace_specifier (parser);
11063   /* Look for the `;' token.  */
11064   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11065
11066   /* Register the alias in the symbol table.  */
11067   do_namespace_alias (identifier, namespace_specifier);
11068 }
11069
11070 /* Parse a qualified-namespace-specifier.
11071
11072    qualified-namespace-specifier:
11073      :: [opt] nested-name-specifier [opt] namespace-name
11074
11075    Returns a NAMESPACE_DECL corresponding to the specified
11076    namespace.  */
11077
11078 static tree
11079 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11080 {
11081   /* Look for the optional `::'.  */
11082   cp_parser_global_scope_opt (parser,
11083                               /*current_scope_valid_p=*/false);
11084
11085   /* Look for the optional nested-name-specifier.  */
11086   cp_parser_nested_name_specifier_opt (parser,
11087                                        /*typename_keyword_p=*/false,
11088                                        /*check_dependency_p=*/true,
11089                                        /*type_p=*/false,
11090                                        /*is_declaration=*/true);
11091
11092   return cp_parser_namespace_name (parser);
11093 }
11094
11095 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11096    access declaration.
11097
11098    using-declaration:
11099      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11100      using :: unqualified-id ;  
11101
11102    access-declaration:
11103      qualified-id ;  
11104
11105    */
11106
11107 static bool
11108 cp_parser_using_declaration (cp_parser* parser, 
11109                              bool access_declaration_p)
11110 {
11111   cp_token *token;
11112   bool typename_p = false;
11113   bool global_scope_p;
11114   tree decl;
11115   tree identifier;
11116   tree qscope;
11117
11118   if (access_declaration_p)
11119     cp_parser_parse_tentatively (parser);
11120   else
11121     {
11122       /* Look for the `using' keyword.  */
11123       cp_parser_require_keyword (parser, RID_USING, "`using'");
11124       
11125       /* Peek at the next token.  */
11126       token = cp_lexer_peek_token (parser->lexer);
11127       /* See if it's `typename'.  */
11128       if (token->keyword == RID_TYPENAME)
11129         {
11130           /* Remember that we've seen it.  */
11131           typename_p = true;
11132           /* Consume the `typename' token.  */
11133           cp_lexer_consume_token (parser->lexer);
11134         }
11135     }
11136
11137   /* Look for the optional global scope qualification.  */
11138   global_scope_p
11139     = (cp_parser_global_scope_opt (parser,
11140                                    /*current_scope_valid_p=*/false)
11141        != NULL_TREE);
11142
11143   /* If we saw `typename', or didn't see `::', then there must be a
11144      nested-name-specifier present.  */
11145   if (typename_p || !global_scope_p)
11146     qscope = cp_parser_nested_name_specifier (parser, typename_p,
11147                                               /*check_dependency_p=*/true,
11148                                               /*type_p=*/false,
11149                                               /*is_declaration=*/true);
11150   /* Otherwise, we could be in either of the two productions.  In that
11151      case, treat the nested-name-specifier as optional.  */
11152   else
11153     qscope = cp_parser_nested_name_specifier_opt (parser,
11154                                                   /*typename_keyword_p=*/false,
11155                                                   /*check_dependency_p=*/true,
11156                                                   /*type_p=*/false,
11157                                                   /*is_declaration=*/true);
11158   if (!qscope)
11159     qscope = global_namespace;
11160
11161   if (access_declaration_p && cp_parser_error_occurred (parser))
11162     /* Something has already gone wrong; there's no need to parse
11163        further.  Since an error has occurred, the return value of
11164        cp_parser_parse_definitely will be false, as required.  */
11165     return cp_parser_parse_definitely (parser);
11166
11167   /* Parse the unqualified-id.  */
11168   identifier = cp_parser_unqualified_id (parser,
11169                                          /*template_keyword_p=*/false,
11170                                          /*check_dependency_p=*/true,
11171                                          /*declarator_p=*/true,
11172                                          /*optional_p=*/false);
11173
11174   if (access_declaration_p)
11175     {
11176       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11177         cp_parser_simulate_error (parser);
11178       if (!cp_parser_parse_definitely (parser))
11179         return false;
11180     }
11181
11182   /* The function we call to handle a using-declaration is different
11183      depending on what scope we are in.  */
11184   if (qscope == error_mark_node || identifier == error_mark_node)
11185     ;
11186   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11187            && TREE_CODE (identifier) != BIT_NOT_EXPR)
11188     /* [namespace.udecl]
11189
11190        A using declaration shall not name a template-id.  */
11191     error ("a template-id may not appear in a using-declaration");
11192   else
11193     {
11194       if (at_class_scope_p ())
11195         {
11196           /* Create the USING_DECL.  */
11197           decl = do_class_using_decl (parser->scope, identifier);
11198           /* Add it to the list of members in this class.  */
11199           finish_member_declaration (decl);
11200         }
11201       else
11202         {
11203           decl = cp_parser_lookup_name_simple (parser, identifier);
11204           if (decl == error_mark_node)
11205             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11206           else if (!at_namespace_scope_p ())
11207             do_local_using_decl (decl, qscope, identifier);
11208           else
11209             do_toplevel_using_decl (decl, qscope, identifier);
11210         }
11211     }
11212
11213   /* Look for the final `;'.  */
11214   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11215   
11216   return true;
11217 }
11218
11219 /* Parse a using-directive.
11220
11221    using-directive:
11222      using namespace :: [opt] nested-name-specifier [opt]
11223        namespace-name ;  */
11224
11225 static void
11226 cp_parser_using_directive (cp_parser* parser)
11227 {
11228   tree namespace_decl;
11229   tree attribs;
11230
11231   /* Look for the `using' keyword.  */
11232   cp_parser_require_keyword (parser, RID_USING, "`using'");
11233   /* And the `namespace' keyword.  */
11234   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11235   /* Look for the optional `::' operator.  */
11236   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11237   /* And the optional nested-name-specifier.  */
11238   cp_parser_nested_name_specifier_opt (parser,
11239                                        /*typename_keyword_p=*/false,
11240                                        /*check_dependency_p=*/true,
11241                                        /*type_p=*/false,
11242                                        /*is_declaration=*/true);
11243   /* Get the namespace being used.  */
11244   namespace_decl = cp_parser_namespace_name (parser);
11245   /* And any specified attributes.  */
11246   attribs = cp_parser_attributes_opt (parser);
11247   /* Update the symbol table.  */
11248   parse_using_directive (namespace_decl, attribs);
11249   /* Look for the final `;'.  */
11250   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11251 }
11252
11253 /* Parse an asm-definition.
11254
11255    asm-definition:
11256      asm ( string-literal ) ;
11257
11258    GNU Extension:
11259
11260    asm-definition:
11261      asm volatile [opt] ( string-literal ) ;
11262      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11263      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11264                           : asm-operand-list [opt] ) ;
11265      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11266                           : asm-operand-list [opt]
11267                           : asm-operand-list [opt] ) ;  */
11268
11269 static void
11270 cp_parser_asm_definition (cp_parser* parser)
11271 {
11272   tree string;
11273   tree outputs = NULL_TREE;
11274   tree inputs = NULL_TREE;
11275   tree clobbers = NULL_TREE;
11276   tree asm_stmt;
11277   bool volatile_p = false;
11278   bool extended_p = false;
11279
11280   /* Look for the `asm' keyword.  */
11281   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11282   /* See if the next token is `volatile'.  */
11283   if (cp_parser_allow_gnu_extensions_p (parser)
11284       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11285     {
11286       /* Remember that we saw the `volatile' keyword.  */
11287       volatile_p = true;
11288       /* Consume the token.  */
11289       cp_lexer_consume_token (parser->lexer);
11290     }
11291   /* Look for the opening `('.  */
11292   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11293     return;
11294   /* Look for the string.  */
11295   string = cp_parser_string_literal (parser, false, false);
11296   if (string == error_mark_node)
11297     {
11298       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11299                                              /*consume_paren=*/true);
11300       return;
11301     }
11302
11303   /* If we're allowing GNU extensions, check for the extended assembly
11304      syntax.  Unfortunately, the `:' tokens need not be separated by
11305      a space in C, and so, for compatibility, we tolerate that here
11306      too.  Doing that means that we have to treat the `::' operator as
11307      two `:' tokens.  */
11308   if (cp_parser_allow_gnu_extensions_p (parser)
11309       && parser->in_function_body
11310       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11311           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11312     {
11313       bool inputs_p = false;
11314       bool clobbers_p = false;
11315
11316       /* The extended syntax was used.  */
11317       extended_p = true;
11318
11319       /* Look for outputs.  */
11320       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11321         {
11322           /* Consume the `:'.  */
11323           cp_lexer_consume_token (parser->lexer);
11324           /* Parse the output-operands.  */
11325           if (cp_lexer_next_token_is_not (parser->lexer,
11326                                           CPP_COLON)
11327               && cp_lexer_next_token_is_not (parser->lexer,
11328                                              CPP_SCOPE)
11329               && cp_lexer_next_token_is_not (parser->lexer,
11330                                              CPP_CLOSE_PAREN))
11331             outputs = cp_parser_asm_operand_list (parser);
11332         }
11333       /* If the next token is `::', there are no outputs, and the
11334          next token is the beginning of the inputs.  */
11335       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11336         /* The inputs are coming next.  */
11337         inputs_p = true;
11338
11339       /* Look for inputs.  */
11340       if (inputs_p
11341           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11342         {
11343           /* Consume the `:' or `::'.  */
11344           cp_lexer_consume_token (parser->lexer);
11345           /* Parse the output-operands.  */
11346           if (cp_lexer_next_token_is_not (parser->lexer,
11347                                           CPP_COLON)
11348               && cp_lexer_next_token_is_not (parser->lexer,
11349                                              CPP_CLOSE_PAREN))
11350             inputs = cp_parser_asm_operand_list (parser);
11351         }
11352       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11353         /* The clobbers are coming next.  */
11354         clobbers_p = true;
11355
11356       /* Look for clobbers.  */
11357       if (clobbers_p
11358           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11359         {
11360           /* Consume the `:' or `::'.  */
11361           cp_lexer_consume_token (parser->lexer);
11362           /* Parse the clobbers.  */
11363           if (cp_lexer_next_token_is_not (parser->lexer,
11364                                           CPP_CLOSE_PAREN))
11365             clobbers = cp_parser_asm_clobber_list (parser);
11366         }
11367     }
11368   /* Look for the closing `)'.  */
11369   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11370     cp_parser_skip_to_closing_parenthesis (parser, true, false,
11371                                            /*consume_paren=*/true);
11372   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11373
11374   /* Create the ASM_EXPR.  */
11375   if (parser->in_function_body)
11376     {
11377       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11378                                   inputs, clobbers);
11379       /* If the extended syntax was not used, mark the ASM_EXPR.  */
11380       if (!extended_p)
11381         {
11382           tree temp = asm_stmt;
11383           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11384             temp = TREE_OPERAND (temp, 0);
11385
11386           ASM_INPUT_P (temp) = 1;
11387         }
11388     }
11389   else
11390     cgraph_add_asm_node (string);
11391 }
11392
11393 /* Declarators [gram.dcl.decl] */
11394
11395 /* Parse an init-declarator.
11396
11397    init-declarator:
11398      declarator initializer [opt]
11399
11400    GNU Extension:
11401
11402    init-declarator:
11403      declarator asm-specification [opt] attributes [opt] initializer [opt]
11404
11405    function-definition:
11406      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11407        function-body
11408      decl-specifier-seq [opt] declarator function-try-block
11409
11410    GNU Extension:
11411
11412    function-definition:
11413      __extension__ function-definition
11414
11415    The DECL_SPECIFIERS apply to this declarator.  Returns a
11416    representation of the entity declared.  If MEMBER_P is TRUE, then
11417    this declarator appears in a class scope.  The new DECL created by
11418    this declarator is returned.
11419
11420    The CHECKS are access checks that should be performed once we know
11421    what entity is being declared (and, therefore, what classes have
11422    befriended it).
11423
11424    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11425    for a function-definition here as well.  If the declarator is a
11426    declarator for a function-definition, *FUNCTION_DEFINITION_P will
11427    be TRUE upon return.  By that point, the function-definition will
11428    have been completely parsed.
11429
11430    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11431    is FALSE.  */
11432
11433 static tree
11434 cp_parser_init_declarator (cp_parser* parser,
11435                            cp_decl_specifier_seq *decl_specifiers,
11436                            VEC (deferred_access_check,gc)* checks,
11437                            bool function_definition_allowed_p,
11438                            bool member_p,
11439                            int declares_class_or_enum,
11440                            bool* function_definition_p)
11441 {
11442   cp_token *token;
11443   cp_declarator *declarator;
11444   tree prefix_attributes;
11445   tree attributes;
11446   tree asm_specification;
11447   tree initializer;
11448   tree decl = NULL_TREE;
11449   tree scope;
11450   bool is_initialized;
11451   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
11452      initialized with "= ..", CPP_OPEN_PAREN if initialized with
11453      "(...)".  */
11454   enum cpp_ttype initialization_kind;
11455   bool is_parenthesized_init = false;
11456   bool is_non_constant_init;
11457   int ctor_dtor_or_conv_p;
11458   bool friend_p;
11459   tree pushed_scope = NULL;
11460
11461   /* Gather the attributes that were provided with the
11462      decl-specifiers.  */
11463   prefix_attributes = decl_specifiers->attributes;
11464
11465   /* Assume that this is not the declarator for a function
11466      definition.  */
11467   if (function_definition_p)
11468     *function_definition_p = false;
11469
11470   /* Defer access checks while parsing the declarator; we cannot know
11471      what names are accessible until we know what is being
11472      declared.  */
11473   resume_deferring_access_checks ();
11474
11475   /* Parse the declarator.  */
11476   declarator
11477     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11478                             &ctor_dtor_or_conv_p,
11479                             /*parenthesized_p=*/NULL,
11480                             /*member_p=*/false);
11481   /* Gather up the deferred checks.  */
11482   stop_deferring_access_checks ();
11483
11484   /* If the DECLARATOR was erroneous, there's no need to go
11485      further.  */
11486   if (declarator == cp_error_declarator)
11487     return error_mark_node;
11488
11489   /* Check that the number of template-parameter-lists is OK.  */
11490   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11491     return error_mark_node;
11492
11493   if (declares_class_or_enum & 2)
11494     cp_parser_check_for_definition_in_return_type (declarator,
11495                                                    decl_specifiers->type);
11496
11497   /* Figure out what scope the entity declared by the DECLARATOR is
11498      located in.  `grokdeclarator' sometimes changes the scope, so
11499      we compute it now.  */
11500   scope = get_scope_of_declarator (declarator);
11501
11502   /* If we're allowing GNU extensions, look for an asm-specification
11503      and attributes.  */
11504   if (cp_parser_allow_gnu_extensions_p (parser))
11505     {
11506       /* Look for an asm-specification.  */
11507       asm_specification = cp_parser_asm_specification_opt (parser);
11508       /* And attributes.  */
11509       attributes = cp_parser_attributes_opt (parser);
11510     }
11511   else
11512     {
11513       asm_specification = NULL_TREE;
11514       attributes = NULL_TREE;
11515     }
11516
11517   /* Peek at the next token.  */
11518   token = cp_lexer_peek_token (parser->lexer);
11519   /* Check to see if the token indicates the start of a
11520      function-definition.  */
11521   if (cp_parser_token_starts_function_definition_p (token))
11522     {
11523       if (!function_definition_allowed_p)
11524         {
11525           /* If a function-definition should not appear here, issue an
11526              error message.  */
11527           cp_parser_error (parser,
11528                            "a function-definition is not allowed here");
11529           return error_mark_node;
11530         }
11531       else
11532         {
11533           /* Neither attributes nor an asm-specification are allowed
11534              on a function-definition.  */
11535           if (asm_specification)
11536             error ("an asm-specification is not allowed on a function-definition");
11537           if (attributes)
11538             error ("attributes are not allowed on a function-definition");
11539           /* This is a function-definition.  */
11540           *function_definition_p = true;
11541
11542           /* Parse the function definition.  */
11543           if (member_p)
11544             decl = cp_parser_save_member_function_body (parser,
11545                                                         decl_specifiers,
11546                                                         declarator,
11547                                                         prefix_attributes);
11548           else
11549             decl
11550               = (cp_parser_function_definition_from_specifiers_and_declarator
11551                  (parser, decl_specifiers, prefix_attributes, declarator));
11552
11553           return decl;
11554         }
11555     }
11556
11557   /* [dcl.dcl]
11558
11559      Only in function declarations for constructors, destructors, and
11560      type conversions can the decl-specifier-seq be omitted.
11561
11562      We explicitly postpone this check past the point where we handle
11563      function-definitions because we tolerate function-definitions
11564      that are missing their return types in some modes.  */
11565   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11566     {
11567       cp_parser_error (parser,
11568                        "expected constructor, destructor, or type conversion");
11569       return error_mark_node;
11570     }
11571
11572   /* An `=' or an `(' indicates an initializer.  */
11573   if (token->type == CPP_EQ
11574       || token->type == CPP_OPEN_PAREN)
11575     {
11576       is_initialized = true;
11577       initialization_kind = token->type;
11578     }
11579   else
11580     {
11581       /* If the init-declarator isn't initialized and isn't followed by a
11582          `,' or `;', it's not a valid init-declarator.  */
11583       if (token->type != CPP_COMMA
11584           && token->type != CPP_SEMICOLON)
11585         {
11586           cp_parser_error (parser, "expected initializer");
11587           return error_mark_node;
11588         }
11589       is_initialized = false;
11590       initialization_kind = CPP_EOF;
11591     }
11592
11593   /* Because start_decl has side-effects, we should only call it if we
11594      know we're going ahead.  By this point, we know that we cannot
11595      possibly be looking at any other construct.  */
11596   cp_parser_commit_to_tentative_parse (parser);
11597
11598   /* If the decl specifiers were bad, issue an error now that we're
11599      sure this was intended to be a declarator.  Then continue
11600      declaring the variable(s), as int, to try to cut down on further
11601      errors.  */
11602   if (decl_specifiers->any_specifiers_p
11603       && decl_specifiers->type == error_mark_node)
11604     {
11605       cp_parser_error (parser, "invalid type in declaration");
11606       decl_specifiers->type = integer_type_node;
11607     }
11608
11609   /* Check to see whether or not this declaration is a friend.  */
11610   friend_p = cp_parser_friend_p (decl_specifiers);
11611
11612   /* Enter the newly declared entry in the symbol table.  If we're
11613      processing a declaration in a class-specifier, we wait until
11614      after processing the initializer.  */
11615   if (!member_p)
11616     {
11617       if (parser->in_unbraced_linkage_specification_p)
11618         decl_specifiers->storage_class = sc_extern;
11619       decl = start_decl (declarator, decl_specifiers,
11620                          is_initialized, attributes, prefix_attributes,
11621                          &pushed_scope);
11622     }
11623   else if (scope)
11624     /* Enter the SCOPE.  That way unqualified names appearing in the
11625        initializer will be looked up in SCOPE.  */
11626     pushed_scope = push_scope (scope);
11627
11628   /* Perform deferred access control checks, now that we know in which
11629      SCOPE the declared entity resides.  */
11630   if (!member_p && decl)
11631     {
11632       tree saved_current_function_decl = NULL_TREE;
11633
11634       /* If the entity being declared is a function, pretend that we
11635          are in its scope.  If it is a `friend', it may have access to
11636          things that would not otherwise be accessible.  */
11637       if (TREE_CODE (decl) == FUNCTION_DECL)
11638         {
11639           saved_current_function_decl = current_function_decl;
11640           current_function_decl = decl;
11641         }
11642
11643       /* Perform access checks for template parameters.  */
11644       cp_parser_perform_template_parameter_access_checks (checks);
11645
11646       /* Perform the access control checks for the declarator and the
11647          the decl-specifiers.  */
11648       perform_deferred_access_checks ();
11649
11650       /* Restore the saved value.  */
11651       if (TREE_CODE (decl) == FUNCTION_DECL)
11652         current_function_decl = saved_current_function_decl;
11653     }
11654
11655   /* Parse the initializer.  */
11656   initializer = NULL_TREE;
11657   is_parenthesized_init = false;
11658   is_non_constant_init = true;
11659   if (is_initialized)
11660     {
11661       if (function_declarator_p (declarator))
11662         {
11663            if (initialization_kind == CPP_EQ)
11664              initializer = cp_parser_pure_specifier (parser);
11665            else
11666              {
11667                /* If the declaration was erroneous, we don't really
11668                   know what the user intended, so just silently
11669                   consume the initializer.  */
11670                if (decl != error_mark_node)
11671                  error ("initializer provided for function");
11672                cp_parser_skip_to_closing_parenthesis (parser,
11673                                                       /*recovering=*/true,
11674                                                       /*or_comma=*/false,
11675                                                       /*consume_paren=*/true);
11676              }
11677         }
11678       else
11679         initializer = cp_parser_initializer (parser,
11680                                              &is_parenthesized_init,
11681                                              &is_non_constant_init);
11682     }
11683
11684   /* The old parser allows attributes to appear after a parenthesized
11685      initializer.  Mark Mitchell proposed removing this functionality
11686      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11687      attributes -- but ignores them.  */
11688   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11689     if (cp_parser_attributes_opt (parser))
11690       warning (OPT_Wattributes,
11691                "attributes after parenthesized initializer ignored");
11692
11693   /* For an in-class declaration, use `grokfield' to create the
11694      declaration.  */
11695   if (member_p)
11696     {
11697       if (pushed_scope)
11698         {
11699           pop_scope (pushed_scope);
11700           pushed_scope = false;
11701         }
11702       decl = grokfield (declarator, decl_specifiers,
11703                         initializer, !is_non_constant_init,
11704                         /*asmspec=*/NULL_TREE,
11705                         prefix_attributes);
11706       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11707         cp_parser_save_default_args (parser, decl);
11708     }
11709
11710   /* Finish processing the declaration.  But, skip friend
11711      declarations.  */
11712   if (!friend_p && decl && decl != error_mark_node)
11713     {
11714       cp_finish_decl (decl,
11715                       initializer, !is_non_constant_init,
11716                       asm_specification,
11717                       /* If the initializer is in parentheses, then this is
11718                          a direct-initialization, which means that an
11719                          `explicit' constructor is OK.  Otherwise, an
11720                          `explicit' constructor cannot be used.  */
11721                       ((is_parenthesized_init || !is_initialized)
11722                      ? 0 : LOOKUP_ONLYCONVERTING));
11723     }
11724   else if (flag_cpp0x && friend_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
11725     /* Core issue #226 (C++0x only): A default template-argument
11726        shall not be specified in a friend class template
11727        declaration. */
11728     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
11729                              /*is_partial=*/0, /*is_friend_decl=*/1);
11730
11731   if (!friend_p && pushed_scope)
11732     pop_scope (pushed_scope);
11733
11734   return decl;
11735 }
11736
11737 /* Parse a declarator.
11738
11739    declarator:
11740      direct-declarator
11741      ptr-operator declarator
11742
11743    abstract-declarator:
11744      ptr-operator abstract-declarator [opt]
11745      direct-abstract-declarator
11746
11747    GNU Extensions:
11748
11749    declarator:
11750      attributes [opt] direct-declarator
11751      attributes [opt] ptr-operator declarator
11752
11753    abstract-declarator:
11754      attributes [opt] ptr-operator abstract-declarator [opt]
11755      attributes [opt] direct-abstract-declarator
11756
11757    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11758    detect constructor, destructor or conversion operators. It is set
11759    to -1 if the declarator is a name, and +1 if it is a
11760    function. Otherwise it is set to zero. Usually you just want to
11761    test for >0, but internally the negative value is used.
11762
11763    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11764    a decl-specifier-seq unless it declares a constructor, destructor,
11765    or conversion.  It might seem that we could check this condition in
11766    semantic analysis, rather than parsing, but that makes it difficult
11767    to handle something like `f()'.  We want to notice that there are
11768    no decl-specifiers, and therefore realize that this is an
11769    expression, not a declaration.)
11770
11771    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11772    the declarator is a direct-declarator of the form "(...)".
11773
11774    MEMBER_P is true iff this declarator is a member-declarator.  */
11775
11776 static cp_declarator *
11777 cp_parser_declarator (cp_parser* parser,
11778                       cp_parser_declarator_kind dcl_kind,
11779                       int* ctor_dtor_or_conv_p,
11780                       bool* parenthesized_p,
11781                       bool member_p)
11782 {
11783   cp_token *token;
11784   cp_declarator *declarator;
11785   enum tree_code code;
11786   cp_cv_quals cv_quals;
11787   tree class_type;
11788   tree attributes = NULL_TREE;
11789
11790   /* Assume this is not a constructor, destructor, or type-conversion
11791      operator.  */
11792   if (ctor_dtor_or_conv_p)
11793     *ctor_dtor_or_conv_p = 0;
11794
11795   if (cp_parser_allow_gnu_extensions_p (parser))
11796     attributes = cp_parser_attributes_opt (parser);
11797
11798   /* Peek at the next token.  */
11799   token = cp_lexer_peek_token (parser->lexer);
11800
11801   /* Check for the ptr-operator production.  */
11802   cp_parser_parse_tentatively (parser);
11803   /* Parse the ptr-operator.  */
11804   code = cp_parser_ptr_operator (parser,
11805                                  &class_type,
11806                                  &cv_quals);
11807   /* If that worked, then we have a ptr-operator.  */
11808   if (cp_parser_parse_definitely (parser))
11809     {
11810       /* If a ptr-operator was found, then this declarator was not
11811          parenthesized.  */
11812       if (parenthesized_p)
11813         *parenthesized_p = true;
11814       /* The dependent declarator is optional if we are parsing an
11815          abstract-declarator.  */
11816       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11817         cp_parser_parse_tentatively (parser);
11818
11819       /* Parse the dependent declarator.  */
11820       declarator = cp_parser_declarator (parser, dcl_kind,
11821                                          /*ctor_dtor_or_conv_p=*/NULL,
11822                                          /*parenthesized_p=*/NULL,
11823                                          /*member_p=*/false);
11824
11825       /* If we are parsing an abstract-declarator, we must handle the
11826          case where the dependent declarator is absent.  */
11827       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11828           && !cp_parser_parse_definitely (parser))
11829         declarator = NULL;
11830
11831       /* Build the representation of the ptr-operator.  */
11832       if (class_type)
11833         declarator = make_ptrmem_declarator (cv_quals,
11834                                              class_type,
11835                                              declarator);
11836       else if (code == INDIRECT_REF)
11837         declarator = make_pointer_declarator (cv_quals, declarator);
11838       else
11839         declarator = make_reference_declarator (cv_quals, declarator);
11840     }
11841   /* Everything else is a direct-declarator.  */
11842   else
11843     {
11844       if (parenthesized_p)
11845         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11846                                                    CPP_OPEN_PAREN);
11847       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11848                                                 ctor_dtor_or_conv_p,
11849                                                 member_p);
11850     }
11851
11852   if (attributes && declarator && declarator != cp_error_declarator)
11853     declarator->attributes = attributes;
11854
11855   return declarator;
11856 }
11857
11858 /* Parse a direct-declarator or direct-abstract-declarator.
11859
11860    direct-declarator:
11861      declarator-id
11862      direct-declarator ( parameter-declaration-clause )
11863        cv-qualifier-seq [opt]
11864        exception-specification [opt]
11865      direct-declarator [ constant-expression [opt] ]
11866      ( declarator )
11867
11868    direct-abstract-declarator:
11869      direct-abstract-declarator [opt]
11870        ( parameter-declaration-clause )
11871        cv-qualifier-seq [opt]
11872        exception-specification [opt]
11873      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11874      ( abstract-declarator )
11875
11876    Returns a representation of the declarator.  DCL_KIND is
11877    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11878    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11879    we are parsing a direct-declarator.  It is
11880    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11881    of ambiguity we prefer an abstract declarator, as per
11882    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11883    cp_parser_declarator.  */
11884
11885 static cp_declarator *
11886 cp_parser_direct_declarator (cp_parser* parser,
11887                              cp_parser_declarator_kind dcl_kind,
11888                              int* ctor_dtor_or_conv_p,
11889                              bool member_p)
11890 {
11891   cp_token *token;
11892   cp_declarator *declarator = NULL;
11893   tree scope = NULL_TREE;
11894   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11895   bool saved_in_declarator_p = parser->in_declarator_p;
11896   bool first = true;
11897   tree pushed_scope = NULL_TREE;
11898
11899   while (true)
11900     {
11901       /* Peek at the next token.  */
11902       token = cp_lexer_peek_token (parser->lexer);
11903       if (token->type == CPP_OPEN_PAREN)
11904         {
11905           /* This is either a parameter-declaration-clause, or a
11906              parenthesized declarator. When we know we are parsing a
11907              named declarator, it must be a parenthesized declarator
11908              if FIRST is true. For instance, `(int)' is a
11909              parameter-declaration-clause, with an omitted
11910              direct-abstract-declarator. But `((*))', is a
11911              parenthesized abstract declarator. Finally, when T is a
11912              template parameter `(T)' is a
11913              parameter-declaration-clause, and not a parenthesized
11914              named declarator.
11915
11916              We first try and parse a parameter-declaration-clause,
11917              and then try a nested declarator (if FIRST is true).
11918
11919              It is not an error for it not to be a
11920              parameter-declaration-clause, even when FIRST is
11921              false. Consider,
11922
11923                int i (int);
11924                int i (3);
11925
11926              The first is the declaration of a function while the
11927              second is a the definition of a variable, including its
11928              initializer.
11929
11930              Having seen only the parenthesis, we cannot know which of
11931              these two alternatives should be selected.  Even more
11932              complex are examples like:
11933
11934                int i (int (a));
11935                int i (int (3));
11936
11937              The former is a function-declaration; the latter is a
11938              variable initialization.
11939
11940              Thus again, we try a parameter-declaration-clause, and if
11941              that fails, we back out and return.  */
11942
11943           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11944             {
11945               cp_parameter_declarator *params;
11946               unsigned saved_num_template_parameter_lists;
11947
11948               /* In a member-declarator, the only valid interpretation
11949                  of a parenthesis is the start of a
11950                  parameter-declaration-clause.  (It is invalid to
11951                  initialize a static data member with a parenthesized
11952                  initializer; only the "=" form of initialization is
11953                  permitted.)  */
11954               if (!member_p)
11955                 cp_parser_parse_tentatively (parser);
11956
11957               /* Consume the `('.  */
11958               cp_lexer_consume_token (parser->lexer);
11959               if (first)
11960                 {
11961                   /* If this is going to be an abstract declarator, we're
11962                      in a declarator and we can't have default args.  */
11963                   parser->default_arg_ok_p = false;
11964                   parser->in_declarator_p = true;
11965                 }
11966
11967               /* Inside the function parameter list, surrounding
11968                  template-parameter-lists do not apply.  */
11969               saved_num_template_parameter_lists
11970                 = parser->num_template_parameter_lists;
11971               parser->num_template_parameter_lists = 0;
11972
11973               /* Parse the parameter-declaration-clause.  */
11974               params = cp_parser_parameter_declaration_clause (parser);
11975
11976               parser->num_template_parameter_lists
11977                 = saved_num_template_parameter_lists;
11978
11979               /* If all went well, parse the cv-qualifier-seq and the
11980                  exception-specification.  */
11981               if (member_p || cp_parser_parse_definitely (parser))
11982                 {
11983                   cp_cv_quals cv_quals;
11984                   tree exception_specification;
11985
11986                   if (ctor_dtor_or_conv_p)
11987                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11988                   first = false;
11989                   /* Consume the `)'.  */
11990                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11991
11992                   /* Parse the cv-qualifier-seq.  */
11993                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11994                   /* And the exception-specification.  */
11995                   exception_specification
11996                     = cp_parser_exception_specification_opt (parser);
11997
11998                   /* Create the function-declarator.  */
11999                   declarator = make_call_declarator (declarator,
12000                                                      params,
12001                                                      cv_quals,
12002                                                      exception_specification);
12003                   /* Any subsequent parameter lists are to do with
12004                      return type, so are not those of the declared
12005                      function.  */
12006                   parser->default_arg_ok_p = false;
12007
12008                   /* Repeat the main loop.  */
12009                   continue;
12010                 }
12011             }
12012
12013           /* If this is the first, we can try a parenthesized
12014              declarator.  */
12015           if (first)
12016             {
12017               bool saved_in_type_id_in_expr_p;
12018
12019               parser->default_arg_ok_p = saved_default_arg_ok_p;
12020               parser->in_declarator_p = saved_in_declarator_p;
12021
12022               /* Consume the `('.  */
12023               cp_lexer_consume_token (parser->lexer);
12024               /* Parse the nested declarator.  */
12025               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12026               parser->in_type_id_in_expr_p = true;
12027               declarator
12028                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12029                                         /*parenthesized_p=*/NULL,
12030                                         member_p);
12031               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12032               first = false;
12033               /* Expect a `)'.  */
12034               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12035                 declarator = cp_error_declarator;
12036               if (declarator == cp_error_declarator)
12037                 break;
12038
12039               goto handle_declarator;
12040             }
12041           /* Otherwise, we must be done.  */
12042           else
12043             break;
12044         }
12045       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12046                && token->type == CPP_OPEN_SQUARE)
12047         {
12048           /* Parse an array-declarator.  */
12049           tree bounds;
12050
12051           if (ctor_dtor_or_conv_p)
12052             *ctor_dtor_or_conv_p = 0;
12053
12054           first = false;
12055           parser->default_arg_ok_p = false;
12056           parser->in_declarator_p = true;
12057           /* Consume the `['.  */
12058           cp_lexer_consume_token (parser->lexer);
12059           /* Peek at the next token.  */
12060           token = cp_lexer_peek_token (parser->lexer);
12061           /* If the next token is `]', then there is no
12062              constant-expression.  */
12063           if (token->type != CPP_CLOSE_SQUARE)
12064             {
12065               bool non_constant_p;
12066
12067               bounds
12068                 = cp_parser_constant_expression (parser,
12069                                                  /*allow_non_constant=*/true,
12070                                                  &non_constant_p);
12071               if (!non_constant_p)
12072                 bounds = fold_non_dependent_expr (bounds);
12073               /* Normally, the array bound must be an integral constant
12074                  expression.  However, as an extension, we allow VLAs
12075                  in function scopes.  */
12076               else if (!parser->in_function_body)
12077                 {
12078                   error ("array bound is not an integer constant");
12079                   bounds = error_mark_node;
12080                 }
12081             }
12082           else
12083             bounds = NULL_TREE;
12084           /* Look for the closing `]'.  */
12085           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12086             {
12087               declarator = cp_error_declarator;
12088               break;
12089             }
12090
12091           declarator = make_array_declarator (declarator, bounds);
12092         }
12093       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12094         {
12095           tree qualifying_scope;
12096           tree unqualified_name;
12097           special_function_kind sfk;
12098           bool abstract_ok;
12099           bool pack_expansion_p = false;
12100
12101           /* Parse a declarator-id */
12102           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12103           if (abstract_ok)
12104             {
12105               cp_parser_parse_tentatively (parser);
12106
12107               /* If we see an ellipsis, we should be looking at a
12108                  parameter pack. */
12109               if (token->type == CPP_ELLIPSIS)
12110                 {
12111                   /* Consume the `...' */
12112                   cp_lexer_consume_token (parser->lexer);
12113
12114                   pack_expansion_p = true;
12115                 }
12116             }
12117
12118           unqualified_name
12119             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12120           qualifying_scope = parser->scope;
12121           if (abstract_ok)
12122             {
12123               bool okay = false;
12124
12125               if (!unqualified_name && pack_expansion_p)
12126                 {
12127                   /* Check whether an error occurred. */
12128                   okay = !cp_parser_error_occurred (parser);
12129
12130                   /* We already consumed the ellipsis to mark a
12131                      parameter pack, but we have no way to report it,
12132                      so abort the tentative parse. We will be exiting
12133                      immediately anyway. */
12134                   cp_parser_abort_tentative_parse (parser);
12135                 }
12136               else
12137                 okay = cp_parser_parse_definitely (parser);
12138
12139               if (!okay)
12140                 unqualified_name = error_mark_node;
12141               else if (unqualified_name
12142                        && (qualifying_scope
12143                            || (TREE_CODE (unqualified_name)
12144                                != IDENTIFIER_NODE)))
12145                 {
12146                   cp_parser_error (parser, "expected unqualified-id");
12147                   unqualified_name = error_mark_node;
12148                 }
12149             }
12150
12151           if (!unqualified_name)
12152             return NULL;
12153           if (unqualified_name == error_mark_node)
12154             {
12155               declarator = cp_error_declarator;
12156               pack_expansion_p = false;
12157               declarator->parameter_pack_p = false;
12158               break;
12159             }
12160
12161           if (qualifying_scope && at_namespace_scope_p ()
12162               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12163             {
12164               /* In the declaration of a member of a template class
12165                  outside of the class itself, the SCOPE will sometimes
12166                  be a TYPENAME_TYPE.  For example, given:
12167
12168                  template <typename T>
12169                  int S<T>::R::i = 3;
12170
12171                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
12172                  this context, we must resolve S<T>::R to an ordinary
12173                  type, rather than a typename type.
12174
12175                  The reason we normally avoid resolving TYPENAME_TYPEs
12176                  is that a specialization of `S' might render
12177                  `S<T>::R' not a type.  However, if `S' is
12178                  specialized, then this `i' will not be used, so there
12179                  is no harm in resolving the types here.  */
12180               tree type;
12181
12182               /* Resolve the TYPENAME_TYPE.  */
12183               type = resolve_typename_type (qualifying_scope,
12184                                             /*only_current_p=*/false);
12185               /* If that failed, the declarator is invalid.  */
12186               if (type == error_mark_node)
12187                 error ("%<%T::%E%> is not a type",
12188                        TYPE_CONTEXT (qualifying_scope),
12189                        TYPE_IDENTIFIER (qualifying_scope));
12190               qualifying_scope = type;
12191             }
12192
12193           sfk = sfk_none;
12194
12195           if (unqualified_name)
12196             {
12197               tree class_type;
12198
12199               if (qualifying_scope
12200                   && CLASS_TYPE_P (qualifying_scope))
12201                 class_type = qualifying_scope;
12202               else
12203                 class_type = current_class_type;
12204
12205               if (TREE_CODE (unqualified_name) == TYPE_DECL)
12206                 {
12207                   tree name_type = TREE_TYPE (unqualified_name);
12208                   if (class_type && same_type_p (name_type, class_type))
12209                     {
12210                       if (qualifying_scope
12211                           && CLASSTYPE_USE_TEMPLATE (name_type))
12212                         {
12213                           error ("invalid use of constructor as a template");
12214                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12215                                   "name the constructor in a qualified name",
12216                                   class_type,
12217                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12218                                   class_type, name_type);
12219                           declarator = cp_error_declarator;
12220                           break;
12221                         }
12222                       else
12223                         unqualified_name = constructor_name (class_type);
12224                     }
12225                   else
12226                     {
12227                       /* We do not attempt to print the declarator
12228                          here because we do not have enough
12229                          information about its original syntactic
12230                          form.  */
12231                       cp_parser_error (parser, "invalid declarator");
12232                       declarator = cp_error_declarator;
12233                       break;
12234                     }
12235                 }
12236
12237               if (class_type)
12238                 {
12239                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12240                     sfk = sfk_destructor;
12241                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12242                     sfk = sfk_conversion;
12243                   else if (/* There's no way to declare a constructor
12244                               for an anonymous type, even if the type
12245                               got a name for linkage purposes.  */
12246                            !TYPE_WAS_ANONYMOUS (class_type)
12247                            && constructor_name_p (unqualified_name,
12248                                                   class_type))
12249                     {
12250                       unqualified_name = constructor_name (class_type);
12251                       sfk = sfk_constructor;
12252                     }
12253
12254                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
12255                     *ctor_dtor_or_conv_p = -1;
12256                 }
12257             }
12258           declarator = make_id_declarator (qualifying_scope,
12259                                            unqualified_name,
12260                                            sfk);
12261           declarator->id_loc = token->location;
12262           declarator->parameter_pack_p = pack_expansion_p;
12263
12264           if (pack_expansion_p)
12265             maybe_warn_variadic_templates ();
12266
12267         handle_declarator:;
12268           scope = get_scope_of_declarator (declarator);
12269           if (scope)
12270             /* Any names that appear after the declarator-id for a
12271                member are looked up in the containing scope.  */
12272             pushed_scope = push_scope (scope);
12273           parser->in_declarator_p = true;
12274           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12275               || (declarator && declarator->kind == cdk_id))
12276             /* Default args are only allowed on function
12277                declarations.  */
12278             parser->default_arg_ok_p = saved_default_arg_ok_p;
12279           else
12280             parser->default_arg_ok_p = false;
12281
12282           first = false;
12283         }
12284       /* We're done.  */
12285       else
12286         break;
12287     }
12288
12289   /* For an abstract declarator, we might wind up with nothing at this
12290      point.  That's an error; the declarator is not optional.  */
12291   if (!declarator)
12292     cp_parser_error (parser, "expected declarator");
12293
12294   /* If we entered a scope, we must exit it now.  */
12295   if (pushed_scope)
12296     pop_scope (pushed_scope);
12297
12298   parser->default_arg_ok_p = saved_default_arg_ok_p;
12299   parser->in_declarator_p = saved_in_declarator_p;
12300
12301   return declarator;
12302 }
12303
12304 /* Parse a ptr-operator.
12305
12306    ptr-operator:
12307      * cv-qualifier-seq [opt]
12308      &
12309      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12310
12311    GNU Extension:
12312
12313    ptr-operator:
12314      & cv-qualifier-seq [opt]
12315
12316    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12317    Returns ADDR_EXPR if a reference was used.  In the case of a
12318    pointer-to-member, *TYPE is filled in with the TYPE containing the
12319    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
12320    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
12321    ERROR_MARK if an error occurred.  */
12322
12323 static enum tree_code
12324 cp_parser_ptr_operator (cp_parser* parser,
12325                         tree* type,
12326                         cp_cv_quals *cv_quals)
12327 {
12328   enum tree_code code = ERROR_MARK;
12329   cp_token *token;
12330
12331   /* Assume that it's not a pointer-to-member.  */
12332   *type = NULL_TREE;
12333   /* And that there are no cv-qualifiers.  */
12334   *cv_quals = TYPE_UNQUALIFIED;
12335
12336   /* Peek at the next token.  */
12337   token = cp_lexer_peek_token (parser->lexer);
12338   /* If it's a `*' or `&' we have a pointer or reference.  */
12339   if (token->type == CPP_MULT || token->type == CPP_AND)
12340     {
12341       /* Remember which ptr-operator we were processing.  */
12342       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12343
12344       /* Consume the `*' or `&'.  */
12345       cp_lexer_consume_token (parser->lexer);
12346
12347       /* A `*' can be followed by a cv-qualifier-seq, and so can a
12348          `&', if we are allowing GNU extensions.  (The only qualifier
12349          that can legally appear after `&' is `restrict', but that is
12350          enforced during semantic analysis.  */
12351       if (code == INDIRECT_REF
12352           || cp_parser_allow_gnu_extensions_p (parser))
12353         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12354     }
12355   else
12356     {
12357       /* Try the pointer-to-member case.  */
12358       cp_parser_parse_tentatively (parser);
12359       /* Look for the optional `::' operator.  */
12360       cp_parser_global_scope_opt (parser,
12361                                   /*current_scope_valid_p=*/false);
12362       /* Look for the nested-name specifier.  */
12363       cp_parser_nested_name_specifier (parser,
12364                                        /*typename_keyword_p=*/false,
12365                                        /*check_dependency_p=*/true,
12366                                        /*type_p=*/false,
12367                                        /*is_declaration=*/false);
12368       /* If we found it, and the next token is a `*', then we are
12369          indeed looking at a pointer-to-member operator.  */
12370       if (!cp_parser_error_occurred (parser)
12371           && cp_parser_require (parser, CPP_MULT, "`*'"))
12372         {
12373           /* Indicate that the `*' operator was used.  */
12374           code = INDIRECT_REF;
12375
12376           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12377             error ("%qD is a namespace", parser->scope);
12378           else
12379             {
12380               /* The type of which the member is a member is given by the
12381                  current SCOPE.  */
12382               *type = parser->scope;
12383               /* The next name will not be qualified.  */
12384               parser->scope = NULL_TREE;
12385               parser->qualifying_scope = NULL_TREE;
12386               parser->object_scope = NULL_TREE;
12387               /* Look for the optional cv-qualifier-seq.  */
12388               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12389             }
12390         }
12391       /* If that didn't work we don't have a ptr-operator.  */
12392       if (!cp_parser_parse_definitely (parser))
12393         cp_parser_error (parser, "expected ptr-operator");
12394     }
12395
12396   return code;
12397 }
12398
12399 /* Parse an (optional) cv-qualifier-seq.
12400
12401    cv-qualifier-seq:
12402      cv-qualifier cv-qualifier-seq [opt]
12403
12404    cv-qualifier:
12405      const
12406      volatile
12407
12408    GNU Extension:
12409
12410    cv-qualifier:
12411      __restrict__
12412
12413    Returns a bitmask representing the cv-qualifiers.  */
12414
12415 static cp_cv_quals
12416 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12417 {
12418   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12419
12420   while (true)
12421     {
12422       cp_token *token;
12423       cp_cv_quals cv_qualifier;
12424
12425       /* Peek at the next token.  */
12426       token = cp_lexer_peek_token (parser->lexer);
12427       /* See if it's a cv-qualifier.  */
12428       switch (token->keyword)
12429         {
12430         case RID_CONST:
12431           cv_qualifier = TYPE_QUAL_CONST;
12432           break;
12433
12434         case RID_VOLATILE:
12435           cv_qualifier = TYPE_QUAL_VOLATILE;
12436           break;
12437
12438         case RID_RESTRICT:
12439           cv_qualifier = TYPE_QUAL_RESTRICT;
12440           break;
12441
12442         default:
12443           cv_qualifier = TYPE_UNQUALIFIED;
12444           break;
12445         }
12446
12447       if (!cv_qualifier)
12448         break;
12449
12450       if (cv_quals & cv_qualifier)
12451         {
12452           error ("duplicate cv-qualifier");
12453           cp_lexer_purge_token (parser->lexer);
12454         }
12455       else
12456         {
12457           cp_lexer_consume_token (parser->lexer);
12458           cv_quals |= cv_qualifier;
12459         }
12460     }
12461
12462   return cv_quals;
12463 }
12464
12465 /* Parse a declarator-id.
12466
12467    declarator-id:
12468      id-expression
12469      :: [opt] nested-name-specifier [opt] type-name
12470
12471    In the `id-expression' case, the value returned is as for
12472    cp_parser_id_expression if the id-expression was an unqualified-id.
12473    If the id-expression was a qualified-id, then a SCOPE_REF is
12474    returned.  The first operand is the scope (either a NAMESPACE_DECL
12475    or TREE_TYPE), but the second is still just a representation of an
12476    unqualified-id.  */
12477
12478 static tree
12479 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12480 {
12481   tree id;
12482   /* The expression must be an id-expression.  Assume that qualified
12483      names are the names of types so that:
12484
12485        template <class T>
12486        int S<T>::R::i = 3;
12487
12488      will work; we must treat `S<T>::R' as the name of a type.
12489      Similarly, assume that qualified names are templates, where
12490      required, so that:
12491
12492        template <class T>
12493        int S<T>::R<T>::i = 3;
12494
12495      will work, too.  */
12496   id = cp_parser_id_expression (parser,
12497                                 /*template_keyword_p=*/false,
12498                                 /*check_dependency_p=*/false,
12499                                 /*template_p=*/NULL,
12500                                 /*declarator_p=*/true,
12501                                 optional_p);
12502   if (id && BASELINK_P (id))
12503     id = BASELINK_FUNCTIONS (id);
12504   return id;
12505 }
12506
12507 /* Parse a type-id.
12508
12509    type-id:
12510      type-specifier-seq abstract-declarator [opt]
12511
12512    Returns the TYPE specified.  */
12513
12514 static tree
12515 cp_parser_type_id (cp_parser* parser)
12516 {
12517   cp_decl_specifier_seq type_specifier_seq;
12518   cp_declarator *abstract_declarator;
12519
12520   /* Parse the type-specifier-seq.  */
12521   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12522                                 &type_specifier_seq);
12523   if (type_specifier_seq.type == error_mark_node)
12524     return error_mark_node;
12525
12526   /* There might or might not be an abstract declarator.  */
12527   cp_parser_parse_tentatively (parser);
12528   /* Look for the declarator.  */
12529   abstract_declarator
12530     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12531                             /*parenthesized_p=*/NULL,
12532                             /*member_p=*/false);
12533   /* Check to see if there really was a declarator.  */
12534   if (!cp_parser_parse_definitely (parser))
12535     abstract_declarator = NULL;
12536
12537   return groktypename (&type_specifier_seq, abstract_declarator);
12538 }
12539
12540 /* Parse a type-specifier-seq.
12541
12542    type-specifier-seq:
12543      type-specifier type-specifier-seq [opt]
12544
12545    GNU extension:
12546
12547    type-specifier-seq:
12548      attributes type-specifier-seq [opt]
12549
12550    If IS_CONDITION is true, we are at the start of a "condition",
12551    e.g., we've just seen "if (".
12552
12553    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
12554
12555 static void
12556 cp_parser_type_specifier_seq (cp_parser* parser,
12557                               bool is_condition,
12558                               cp_decl_specifier_seq *type_specifier_seq)
12559 {
12560   bool seen_type_specifier = false;
12561   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12562
12563   /* Clear the TYPE_SPECIFIER_SEQ.  */
12564   clear_decl_specs (type_specifier_seq);
12565
12566   /* Parse the type-specifiers and attributes.  */
12567   while (true)
12568     {
12569       tree type_specifier;
12570       bool is_cv_qualifier;
12571
12572       /* Check for attributes first.  */
12573       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12574         {
12575           type_specifier_seq->attributes =
12576             chainon (type_specifier_seq->attributes,
12577                      cp_parser_attributes_opt (parser));
12578           continue;
12579         }
12580
12581       /* Look for the type-specifier.  */
12582       type_specifier = cp_parser_type_specifier (parser,
12583                                                  flags,
12584                                                  type_specifier_seq,
12585                                                  /*is_declaration=*/false,
12586                                                  NULL,
12587                                                  &is_cv_qualifier);
12588       if (!type_specifier)
12589         {
12590           /* If the first type-specifier could not be found, this is not a
12591              type-specifier-seq at all.  */
12592           if (!seen_type_specifier)
12593             {
12594               cp_parser_error (parser, "expected type-specifier");
12595               type_specifier_seq->type = error_mark_node;
12596               return;
12597             }
12598           /* If subsequent type-specifiers could not be found, the
12599              type-specifier-seq is complete.  */
12600           break;
12601         }
12602
12603       seen_type_specifier = true;
12604       /* The standard says that a condition can be:
12605
12606             type-specifier-seq declarator = assignment-expression
12607
12608          However, given:
12609
12610            struct S {};
12611            if (int S = ...)
12612
12613          we should treat the "S" as a declarator, not as a
12614          type-specifier.  The standard doesn't say that explicitly for
12615          type-specifier-seq, but it does say that for
12616          decl-specifier-seq in an ordinary declaration.  Perhaps it
12617          would be clearer just to allow a decl-specifier-seq here, and
12618          then add a semantic restriction that if any decl-specifiers
12619          that are not type-specifiers appear, the program is invalid.  */
12620       if (is_condition && !is_cv_qualifier)
12621         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12622     }
12623
12624   cp_parser_check_decl_spec (type_specifier_seq);
12625 }
12626
12627 /* Parse a parameter-declaration-clause.
12628
12629    parameter-declaration-clause:
12630      parameter-declaration-list [opt] ... [opt]
12631      parameter-declaration-list , ...
12632
12633    Returns a representation for the parameter declarations.  A return
12634    value of NULL indicates a parameter-declaration-clause consisting
12635    only of an ellipsis.  */
12636
12637 static cp_parameter_declarator *
12638 cp_parser_parameter_declaration_clause (cp_parser* parser)
12639 {
12640   cp_parameter_declarator *parameters;
12641   cp_token *token;
12642   bool ellipsis_p;
12643   bool is_error;
12644
12645   /* Peek at the next token.  */
12646   token = cp_lexer_peek_token (parser->lexer);
12647   /* Check for trivial parameter-declaration-clauses.  */
12648   if (token->type == CPP_ELLIPSIS)
12649     {
12650       /* Consume the `...' token.  */
12651       cp_lexer_consume_token (parser->lexer);
12652       return NULL;
12653     }
12654   else if (token->type == CPP_CLOSE_PAREN)
12655     /* There are no parameters.  */
12656     {
12657 #ifndef NO_IMPLICIT_EXTERN_C
12658       if (in_system_header && current_class_type == NULL
12659           && current_lang_name == lang_name_c)
12660         return NULL;
12661       else
12662 #endif
12663         return no_parameters;
12664     }
12665   /* Check for `(void)', too, which is a special case.  */
12666   else if (token->keyword == RID_VOID
12667            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12668                == CPP_CLOSE_PAREN))
12669     {
12670       /* Consume the `void' token.  */
12671       cp_lexer_consume_token (parser->lexer);
12672       /* There are no parameters.  */
12673       return no_parameters;
12674     }
12675
12676   /* Parse the parameter-declaration-list.  */
12677   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12678   /* If a parse error occurred while parsing the
12679      parameter-declaration-list, then the entire
12680      parameter-declaration-clause is erroneous.  */
12681   if (is_error)
12682     return NULL;
12683
12684   /* Peek at the next token.  */
12685   token = cp_lexer_peek_token (parser->lexer);
12686   /* If it's a `,', the clause should terminate with an ellipsis.  */
12687   if (token->type == CPP_COMMA)
12688     {
12689       /* Consume the `,'.  */
12690       cp_lexer_consume_token (parser->lexer);
12691       /* Expect an ellipsis.  */
12692       ellipsis_p
12693         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12694     }
12695   /* It might also be `...' if the optional trailing `,' was
12696      omitted.  */
12697   else if (token->type == CPP_ELLIPSIS)
12698     {
12699       /* Consume the `...' token.  */
12700       cp_lexer_consume_token (parser->lexer);
12701       /* And remember that we saw it.  */
12702       ellipsis_p = true;
12703     }
12704   else
12705     ellipsis_p = false;
12706
12707   /* Finish the parameter list.  */
12708   if (parameters && ellipsis_p)
12709     parameters->ellipsis_p = true;
12710
12711   return parameters;
12712 }
12713
12714 /* Parse a parameter-declaration-list.
12715
12716    parameter-declaration-list:
12717      parameter-declaration
12718      parameter-declaration-list , parameter-declaration
12719
12720    Returns a representation of the parameter-declaration-list, as for
12721    cp_parser_parameter_declaration_clause.  However, the
12722    `void_list_node' is never appended to the list.  Upon return,
12723    *IS_ERROR will be true iff an error occurred.  */
12724
12725 static cp_parameter_declarator *
12726 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12727 {
12728   cp_parameter_declarator *parameters = NULL;
12729   cp_parameter_declarator **tail = &parameters;
12730   bool saved_in_unbraced_linkage_specification_p;
12731
12732   /* Assume all will go well.  */
12733   *is_error = false;
12734   /* The special considerations that apply to a function within an
12735      unbraced linkage specifications do not apply to the parameters
12736      to the function.  */
12737   saved_in_unbraced_linkage_specification_p 
12738     = parser->in_unbraced_linkage_specification_p;
12739   parser->in_unbraced_linkage_specification_p = false;
12740
12741   /* Look for more parameters.  */
12742   while (true)
12743     {
12744       cp_parameter_declarator *parameter;
12745       bool parenthesized_p;
12746       /* Parse the parameter.  */
12747       parameter
12748         = cp_parser_parameter_declaration (parser,
12749                                            /*template_parm_p=*/false,
12750                                            &parenthesized_p);
12751
12752       /* If a parse error occurred parsing the parameter declaration,
12753          then the entire parameter-declaration-list is erroneous.  */
12754       if (!parameter)
12755         {
12756           *is_error = true;
12757           parameters = NULL;
12758           break;
12759         }
12760       /* Add the new parameter to the list.  */
12761       *tail = parameter;
12762       tail = &parameter->next;
12763
12764       /* Peek at the next token.  */
12765       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12766           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12767           /* These are for Objective-C++ */
12768           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12769           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12770         /* The parameter-declaration-list is complete.  */
12771         break;
12772       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12773         {
12774           cp_token *token;
12775
12776           /* Peek at the next token.  */
12777           token = cp_lexer_peek_nth_token (parser->lexer, 2);
12778           /* If it's an ellipsis, then the list is complete.  */
12779           if (token->type == CPP_ELLIPSIS)
12780             break;
12781           /* Otherwise, there must be more parameters.  Consume the
12782              `,'.  */
12783           cp_lexer_consume_token (parser->lexer);
12784           /* When parsing something like:
12785
12786                 int i(float f, double d)
12787
12788              we can tell after seeing the declaration for "f" that we
12789              are not looking at an initialization of a variable "i",
12790              but rather at the declaration of a function "i".
12791
12792              Due to the fact that the parsing of template arguments
12793              (as specified to a template-id) requires backtracking we
12794              cannot use this technique when inside a template argument
12795              list.  */
12796           if (!parser->in_template_argument_list_p
12797               && !parser->in_type_id_in_expr_p
12798               && cp_parser_uncommitted_to_tentative_parse_p (parser)
12799               /* However, a parameter-declaration of the form
12800                  "foat(f)" (which is a valid declaration of a
12801                  parameter "f") can also be interpreted as an
12802                  expression (the conversion of "f" to "float").  */
12803               && !parenthesized_p)
12804             cp_parser_commit_to_tentative_parse (parser);
12805         }
12806       else
12807         {
12808           cp_parser_error (parser, "expected %<,%> or %<...%>");
12809           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12810             cp_parser_skip_to_closing_parenthesis (parser,
12811                                                    /*recovering=*/true,
12812                                                    /*or_comma=*/false,
12813                                                    /*consume_paren=*/false);
12814           break;
12815         }
12816     }
12817
12818   parser->in_unbraced_linkage_specification_p
12819     = saved_in_unbraced_linkage_specification_p;
12820
12821   return parameters;
12822 }
12823
12824 /* Parse a parameter declaration.
12825
12826    parameter-declaration:
12827      decl-specifier-seq ... [opt] declarator
12828      decl-specifier-seq declarator = assignment-expression
12829      decl-specifier-seq ... [opt] abstract-declarator [opt]
12830      decl-specifier-seq abstract-declarator [opt] = assignment-expression
12831
12832    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12833    declares a template parameter.  (In that case, a non-nested `>'
12834    token encountered during the parsing of the assignment-expression
12835    is not interpreted as a greater-than operator.)
12836
12837    Returns a representation of the parameter, or NULL if an error
12838    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12839    true iff the declarator is of the form "(p)".  */
12840
12841 static cp_parameter_declarator *
12842 cp_parser_parameter_declaration (cp_parser *parser,
12843                                  bool template_parm_p,
12844                                  bool *parenthesized_p)
12845 {
12846   int declares_class_or_enum;
12847   bool greater_than_is_operator_p;
12848   cp_decl_specifier_seq decl_specifiers;
12849   cp_declarator *declarator;
12850   tree default_argument;
12851   cp_token *token;
12852   const char *saved_message;
12853
12854   /* In a template parameter, `>' is not an operator.
12855
12856      [temp.param]
12857
12858      When parsing a default template-argument for a non-type
12859      template-parameter, the first non-nested `>' is taken as the end
12860      of the template parameter-list rather than a greater-than
12861      operator.  */
12862   greater_than_is_operator_p = !template_parm_p;
12863
12864   /* Type definitions may not appear in parameter types.  */
12865   saved_message = parser->type_definition_forbidden_message;
12866   parser->type_definition_forbidden_message
12867     = "types may not be defined in parameter types";
12868
12869   /* Parse the declaration-specifiers.  */
12870   cp_parser_decl_specifier_seq (parser,
12871                                 CP_PARSER_FLAGS_NONE,
12872                                 &decl_specifiers,
12873                                 &declares_class_or_enum);
12874   /* If an error occurred, there's no reason to attempt to parse the
12875      rest of the declaration.  */
12876   if (cp_parser_error_occurred (parser))
12877     {
12878       parser->type_definition_forbidden_message = saved_message;
12879       return NULL;
12880     }
12881
12882   /* Peek at the next token.  */
12883   token = cp_lexer_peek_token (parser->lexer);
12884
12885   /* If the next token is a `)', `,', `=', `>', or `...', then there
12886      is no declarator. However, when variadic templates are enabled,
12887      there may be a declarator following `...'.  */
12888   if (token->type == CPP_CLOSE_PAREN
12889       || token->type == CPP_COMMA
12890       || token->type == CPP_EQ
12891       || token->type == CPP_GREATER)
12892     {
12893       declarator = NULL;
12894       if (parenthesized_p)
12895         *parenthesized_p = false;
12896     }
12897   /* Otherwise, there should be a declarator.  */
12898   else
12899     {
12900       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12901       parser->default_arg_ok_p = false;
12902
12903       /* After seeing a decl-specifier-seq, if the next token is not a
12904          "(", there is no possibility that the code is a valid
12905          expression.  Therefore, if parsing tentatively, we commit at
12906          this point.  */
12907       if (!parser->in_template_argument_list_p
12908           /* In an expression context, having seen:
12909
12910                (int((char ...
12911
12912              we cannot be sure whether we are looking at a
12913              function-type (taking a "char" as a parameter) or a cast
12914              of some object of type "char" to "int".  */
12915           && !parser->in_type_id_in_expr_p
12916           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12917           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12918         cp_parser_commit_to_tentative_parse (parser);
12919       /* Parse the declarator.  */
12920       declarator = cp_parser_declarator (parser,
12921                                          CP_PARSER_DECLARATOR_EITHER,
12922                                          /*ctor_dtor_or_conv_p=*/NULL,
12923                                          parenthesized_p,
12924                                          /*member_p=*/false);
12925       parser->default_arg_ok_p = saved_default_arg_ok_p;
12926       /* After the declarator, allow more attributes.  */
12927       decl_specifiers.attributes
12928         = chainon (decl_specifiers.attributes,
12929                    cp_parser_attributes_opt (parser));
12930     }
12931
12932   /* If the next token is an ellipsis, and the type of the declarator
12933      contains parameter packs but it is not a TYPE_PACK_EXPANSION, then
12934      we actually have a parameter pack expansion expression. Otherwise,
12935      leave the ellipsis for a C-style variadic function. */
12936   token = cp_lexer_peek_token (parser->lexer);
12937   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12938     {
12939       tree type = decl_specifiers.type;
12940
12941       if (DECL_P (type))
12942         type = TREE_TYPE (type);
12943
12944       if (TREE_CODE (type) != TYPE_PACK_EXPANSION
12945           && (!declarator || !declarator->parameter_pack_p)
12946           && uses_parameter_packs (type))
12947         {
12948           /* Consume the `...'. */
12949           cp_lexer_consume_token (parser->lexer);
12950           maybe_warn_variadic_templates ();
12951           
12952           /* Build a pack expansion type */
12953           if (declarator)
12954             declarator->parameter_pack_p = true;
12955           else
12956             decl_specifiers.type = make_pack_expansion (type);
12957         }
12958     }
12959
12960   /* The restriction on defining new types applies only to the type
12961      of the parameter, not to the default argument.  */
12962   parser->type_definition_forbidden_message = saved_message;
12963
12964   /* If the next token is `=', then process a default argument.  */
12965   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12966     {
12967       bool saved_greater_than_is_operator_p;
12968       /* Consume the `='.  */
12969       cp_lexer_consume_token (parser->lexer);
12970
12971       /* If we are defining a class, then the tokens that make up the
12972          default argument must be saved and processed later.  */
12973       if (!template_parm_p && at_class_scope_p ()
12974           && TYPE_BEING_DEFINED (current_class_type))
12975         {
12976           unsigned depth = 0;
12977           cp_token *first_token;
12978           cp_token *token;
12979
12980           /* Add tokens until we have processed the entire default
12981              argument.  We add the range [first_token, token).  */
12982           first_token = cp_lexer_peek_token (parser->lexer);
12983           while (true)
12984             {
12985               bool done = false;
12986
12987               /* Peek at the next token.  */
12988               token = cp_lexer_peek_token (parser->lexer);
12989               /* What we do depends on what token we have.  */
12990               switch (token->type)
12991                 {
12992                   /* In valid code, a default argument must be
12993                      immediately followed by a `,' `)', or `...'.  */
12994                 case CPP_COMMA:
12995                 case CPP_CLOSE_PAREN:
12996                 case CPP_ELLIPSIS:
12997                   /* If we run into a non-nested `;', `}', or `]',
12998                      then the code is invalid -- but the default
12999                      argument is certainly over.  */
13000                 case CPP_SEMICOLON:
13001                 case CPP_CLOSE_BRACE:
13002                 case CPP_CLOSE_SQUARE:
13003                   if (depth == 0)
13004                     done = true;
13005                   /* Update DEPTH, if necessary.  */
13006                   else if (token->type == CPP_CLOSE_PAREN
13007                            || token->type == CPP_CLOSE_BRACE
13008                            || token->type == CPP_CLOSE_SQUARE)
13009                     --depth;
13010                   break;
13011
13012                 case CPP_OPEN_PAREN:
13013                 case CPP_OPEN_SQUARE:
13014                 case CPP_OPEN_BRACE:
13015                   ++depth;
13016                   break;
13017
13018                 case CPP_GREATER:
13019                   /* If we see a non-nested `>', and `>' is not an
13020                      operator, then it marks the end of the default
13021                      argument.  */
13022                   if (!depth && !greater_than_is_operator_p)
13023                     done = true;
13024                   break;
13025
13026                   /* If we run out of tokens, issue an error message.  */
13027                 case CPP_EOF:
13028                 case CPP_PRAGMA_EOL:
13029                   error ("file ends in default argument");
13030                   done = true;
13031                   break;
13032
13033                 case CPP_NAME:
13034                 case CPP_SCOPE:
13035                   /* In these cases, we should look for template-ids.
13036                      For example, if the default argument is
13037                      `X<int, double>()', we need to do name lookup to
13038                      figure out whether or not `X' is a template; if
13039                      so, the `,' does not end the default argument.
13040
13041                      That is not yet done.  */
13042                   break;
13043
13044                 default:
13045                   break;
13046                 }
13047
13048               /* If we've reached the end, stop.  */
13049               if (done)
13050                 break;
13051
13052               /* Add the token to the token block.  */
13053               token = cp_lexer_consume_token (parser->lexer);
13054             }
13055
13056           /* Create a DEFAULT_ARG to represented the unparsed default
13057              argument.  */
13058           default_argument = make_node (DEFAULT_ARG);
13059           DEFARG_TOKENS (default_argument)
13060             = cp_token_cache_new (first_token, token);
13061           DEFARG_INSTANTIATIONS (default_argument) = NULL;
13062         }
13063       /* Outside of a class definition, we can just parse the
13064          assignment-expression.  */
13065       else
13066         {
13067           bool saved_local_variables_forbidden_p;
13068
13069           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13070              set correctly.  */
13071           saved_greater_than_is_operator_p
13072             = parser->greater_than_is_operator_p;
13073           parser->greater_than_is_operator_p = greater_than_is_operator_p;
13074           /* Local variable names (and the `this' keyword) may not
13075              appear in a default argument.  */
13076           saved_local_variables_forbidden_p
13077             = parser->local_variables_forbidden_p;
13078           parser->local_variables_forbidden_p = true;
13079           /* The default argument expression may cause implicitly
13080              defined member functions to be synthesized, which will
13081              result in garbage collection.  We must treat this
13082              situation as if we were within the body of function so as
13083              to avoid collecting live data on the stack.  */
13084           ++function_depth;
13085           /* Parse the assignment-expression.  */
13086           if (template_parm_p)
13087             push_deferring_access_checks (dk_no_deferred);
13088           default_argument
13089             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13090           if (template_parm_p)
13091             pop_deferring_access_checks ();
13092           /* Restore saved state.  */
13093           --function_depth;
13094           parser->greater_than_is_operator_p
13095             = saved_greater_than_is_operator_p;
13096           parser->local_variables_forbidden_p
13097             = saved_local_variables_forbidden_p;
13098         }
13099       if (!parser->default_arg_ok_p)
13100         {
13101           if (!flag_pedantic_errors)
13102             warning (0, "deprecated use of default argument for parameter of non-function");
13103           else
13104             {
13105               error ("default arguments are only permitted for function parameters");
13106               default_argument = NULL_TREE;
13107             }
13108         }
13109     }
13110   else
13111     default_argument = NULL_TREE;
13112
13113   return make_parameter_declarator (&decl_specifiers,
13114                                     declarator,
13115                                     default_argument);
13116 }
13117
13118 /* Parse a function-body.
13119
13120    function-body:
13121      compound_statement  */
13122
13123 static void
13124 cp_parser_function_body (cp_parser *parser)
13125 {
13126   cp_parser_compound_statement (parser, NULL, false);
13127 }
13128
13129 /* Parse a ctor-initializer-opt followed by a function-body.  Return
13130    true if a ctor-initializer was present.  */
13131
13132 static bool
13133 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13134 {
13135   tree body;
13136   bool ctor_initializer_p;
13137
13138   /* Begin the function body.  */
13139   body = begin_function_body ();
13140   /* Parse the optional ctor-initializer.  */
13141   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13142   /* Parse the function-body.  */
13143   cp_parser_function_body (parser);
13144   /* Finish the function body.  */
13145   finish_function_body (body);
13146
13147   return ctor_initializer_p;
13148 }
13149
13150 /* Parse an initializer.
13151
13152    initializer:
13153      = initializer-clause
13154      ( expression-list )
13155
13156    Returns an expression representing the initializer.  If no
13157    initializer is present, NULL_TREE is returned.
13158
13159    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13160    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
13161    set to FALSE if there is no initializer present.  If there is an
13162    initializer, and it is not a constant-expression, *NON_CONSTANT_P
13163    is set to true; otherwise it is set to false.  */
13164
13165 static tree
13166 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13167                        bool* non_constant_p)
13168 {
13169   cp_token *token;
13170   tree init;
13171
13172   /* Peek at the next token.  */
13173   token = cp_lexer_peek_token (parser->lexer);
13174
13175   /* Let our caller know whether or not this initializer was
13176      parenthesized.  */
13177   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13178   /* Assume that the initializer is constant.  */
13179   *non_constant_p = false;
13180
13181   if (token->type == CPP_EQ)
13182     {
13183       /* Consume the `='.  */
13184       cp_lexer_consume_token (parser->lexer);
13185       /* Parse the initializer-clause.  */
13186       init = cp_parser_initializer_clause (parser, non_constant_p);
13187     }
13188   else if (token->type == CPP_OPEN_PAREN)
13189     init = cp_parser_parenthesized_expression_list (parser, false,
13190                                                     /*cast_p=*/false,
13191                                                     /*allow_expansion_p=*/true,
13192                                                     non_constant_p);
13193   else
13194     {
13195       /* Anything else is an error.  */
13196       cp_parser_error (parser, "expected initializer");
13197       init = error_mark_node;
13198     }
13199
13200   return init;
13201 }
13202
13203 /* Parse an initializer-clause.
13204
13205    initializer-clause:
13206      assignment-expression
13207      { initializer-list , [opt] }
13208      { }
13209
13210    Returns an expression representing the initializer.
13211
13212    If the `assignment-expression' production is used the value
13213    returned is simply a representation for the expression.
13214
13215    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
13216    the elements of the initializer-list (or NULL, if the last
13217    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
13218    NULL_TREE.  There is no way to detect whether or not the optional
13219    trailing `,' was provided.  NON_CONSTANT_P is as for
13220    cp_parser_initializer.  */
13221
13222 static tree
13223 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13224 {
13225   tree initializer;
13226
13227   /* Assume the expression is constant.  */
13228   *non_constant_p = false;
13229
13230   /* If it is not a `{', then we are looking at an
13231      assignment-expression.  */
13232   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13233     {
13234       initializer
13235         = cp_parser_constant_expression (parser,
13236                                         /*allow_non_constant_p=*/true,
13237                                         non_constant_p);
13238       if (!*non_constant_p)
13239         initializer = fold_non_dependent_expr (initializer);
13240     }
13241   else
13242     {
13243       /* Consume the `{' token.  */
13244       cp_lexer_consume_token (parser->lexer);
13245       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
13246       initializer = make_node (CONSTRUCTOR);
13247       /* If it's not a `}', then there is a non-trivial initializer.  */
13248       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13249         {
13250           /* Parse the initializer list.  */
13251           CONSTRUCTOR_ELTS (initializer)
13252             = cp_parser_initializer_list (parser, non_constant_p);
13253           /* A trailing `,' token is allowed.  */
13254           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13255             cp_lexer_consume_token (parser->lexer);
13256         }
13257       /* Now, there should be a trailing `}'.  */
13258       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13259     }
13260
13261   return initializer;
13262 }
13263
13264 /* Parse an initializer-list.
13265
13266    initializer-list:
13267      initializer-clause ... [opt]
13268      initializer-list , initializer-clause ... [opt]
13269
13270    GNU Extension:
13271
13272    initializer-list:
13273      identifier : initializer-clause
13274      initializer-list, identifier : initializer-clause
13275
13276    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
13277    for the initializer.  If the INDEX of the elt is non-NULL, it is the
13278    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
13279    as for cp_parser_initializer.  */
13280
13281 static VEC(constructor_elt,gc) *
13282 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13283 {
13284   VEC(constructor_elt,gc) *v = NULL;
13285
13286   /* Assume all of the expressions are constant.  */
13287   *non_constant_p = false;
13288
13289   /* Parse the rest of the list.  */
13290   while (true)
13291     {
13292       cp_token *token;
13293       tree identifier;
13294       tree initializer;
13295       bool clause_non_constant_p;
13296
13297       /* If the next token is an identifier and the following one is a
13298          colon, we are looking at the GNU designated-initializer
13299          syntax.  */
13300       if (cp_parser_allow_gnu_extensions_p (parser)
13301           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13302           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13303         {
13304           /* Warn the user that they are using an extension.  */
13305           if (pedantic)
13306             pedwarn ("ISO C++ does not allow designated initializers");
13307           /* Consume the identifier.  */
13308           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13309           /* Consume the `:'.  */
13310           cp_lexer_consume_token (parser->lexer);
13311         }
13312       else
13313         identifier = NULL_TREE;
13314
13315       /* Parse the initializer.  */
13316       initializer = cp_parser_initializer_clause (parser,
13317                                                   &clause_non_constant_p);
13318       /* If any clause is non-constant, so is the entire initializer.  */
13319       if (clause_non_constant_p)
13320         *non_constant_p = true;
13321
13322       /* If we have an ellipsis, this is an initializer pack
13323          expansion.  */
13324       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13325         {
13326           /* Consume the `...'.  */
13327           cp_lexer_consume_token (parser->lexer);
13328
13329           /* Turn the initializer into an initializer expansion.  */
13330           initializer = make_pack_expansion (initializer);
13331         }
13332
13333       /* Add it to the vector.  */
13334       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13335
13336       /* If the next token is not a comma, we have reached the end of
13337          the list.  */
13338       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13339         break;
13340
13341       /* Peek at the next token.  */
13342       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13343       /* If the next token is a `}', then we're still done.  An
13344          initializer-clause can have a trailing `,' after the
13345          initializer-list and before the closing `}'.  */
13346       if (token->type == CPP_CLOSE_BRACE)
13347         break;
13348
13349       /* Consume the `,' token.  */
13350       cp_lexer_consume_token (parser->lexer);
13351     }
13352
13353   return v;
13354 }
13355
13356 /* Classes [gram.class] */
13357
13358 /* Parse a class-name.
13359
13360    class-name:
13361      identifier
13362      template-id
13363
13364    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13365    to indicate that names looked up in dependent types should be
13366    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
13367    keyword has been used to indicate that the name that appears next
13368    is a template.  TAG_TYPE indicates the explicit tag given before
13369    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
13370    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
13371    is the class being defined in a class-head.
13372
13373    Returns the TYPE_DECL representing the class.  */
13374
13375 static tree
13376 cp_parser_class_name (cp_parser *parser,
13377                       bool typename_keyword_p,
13378                       bool template_keyword_p,
13379                       enum tag_types tag_type,
13380                       bool check_dependency_p,
13381                       bool class_head_p,
13382                       bool is_declaration)
13383 {
13384   tree decl;
13385   tree scope;
13386   bool typename_p;
13387   cp_token *token;
13388
13389   /* All class-names start with an identifier.  */
13390   token = cp_lexer_peek_token (parser->lexer);
13391   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13392     {
13393       cp_parser_error (parser, "expected class-name");
13394       return error_mark_node;
13395     }
13396
13397   /* PARSER->SCOPE can be cleared when parsing the template-arguments
13398      to a template-id, so we save it here.  */
13399   scope = parser->scope;
13400   if (scope == error_mark_node)
13401     return error_mark_node;
13402
13403   /* Any name names a type if we're following the `typename' keyword
13404      in a qualified name where the enclosing scope is type-dependent.  */
13405   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13406                 && dependent_type_p (scope));
13407   /* Handle the common case (an identifier, but not a template-id)
13408      efficiently.  */
13409   if (token->type == CPP_NAME
13410       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13411     {
13412       cp_token *identifier_token;
13413       tree identifier;
13414       bool ambiguous_p;
13415
13416       /* Look for the identifier.  */
13417       identifier_token = cp_lexer_peek_token (parser->lexer);
13418       ambiguous_p = identifier_token->ambiguous_p;
13419       identifier = cp_parser_identifier (parser);
13420       /* If the next token isn't an identifier, we are certainly not
13421          looking at a class-name.  */
13422       if (identifier == error_mark_node)
13423         decl = error_mark_node;
13424       /* If we know this is a type-name, there's no need to look it
13425          up.  */
13426       else if (typename_p)
13427         decl = identifier;
13428       else
13429         {
13430           tree ambiguous_decls;
13431           /* If we already know that this lookup is ambiguous, then
13432              we've already issued an error message; there's no reason
13433              to check again.  */
13434           if (ambiguous_p)
13435             {
13436               cp_parser_simulate_error (parser);
13437               return error_mark_node;
13438             }
13439           /* If the next token is a `::', then the name must be a type
13440              name.
13441
13442              [basic.lookup.qual]
13443
13444              During the lookup for a name preceding the :: scope
13445              resolution operator, object, function, and enumerator
13446              names are ignored.  */
13447           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13448             tag_type = typename_type;
13449           /* Look up the name.  */
13450           decl = cp_parser_lookup_name (parser, identifier,
13451                                         tag_type,
13452                                         /*is_template=*/false,
13453                                         /*is_namespace=*/false,
13454                                         check_dependency_p,
13455                                         &ambiguous_decls);
13456           if (ambiguous_decls)
13457             {
13458               error ("reference to %qD is ambiguous", identifier);
13459               print_candidates (ambiguous_decls);
13460               if (cp_parser_parsing_tentatively (parser))
13461                 {
13462                   identifier_token->ambiguous_p = true;
13463                   cp_parser_simulate_error (parser);
13464                 }
13465               return error_mark_node;
13466             }
13467         }
13468     }
13469   else
13470     {
13471       /* Try a template-id.  */
13472       decl = cp_parser_template_id (parser, template_keyword_p,
13473                                     check_dependency_p,
13474                                     is_declaration);
13475       if (decl == error_mark_node)
13476         return error_mark_node;
13477     }
13478
13479   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13480
13481   /* If this is a typename, create a TYPENAME_TYPE.  */
13482   if (typename_p && decl != error_mark_node)
13483     {
13484       decl = make_typename_type (scope, decl, typename_type,
13485                                  /*complain=*/tf_error);
13486       if (decl != error_mark_node)
13487         decl = TYPE_NAME (decl);
13488     }
13489
13490   /* Check to see that it is really the name of a class.  */
13491   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13492       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13493       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13494     /* Situations like this:
13495
13496          template <typename T> struct A {
13497            typename T::template X<int>::I i;
13498          };
13499
13500        are problematic.  Is `T::template X<int>' a class-name?  The
13501        standard does not seem to be definitive, but there is no other
13502        valid interpretation of the following `::'.  Therefore, those
13503        names are considered class-names.  */
13504     {
13505       decl = make_typename_type (scope, decl, tag_type, tf_error);
13506       if (decl != error_mark_node)
13507         decl = TYPE_NAME (decl);
13508     }
13509   else if (TREE_CODE (decl) != TYPE_DECL
13510            || TREE_TYPE (decl) == error_mark_node
13511            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13512     decl = error_mark_node;
13513
13514   if (decl == error_mark_node)
13515     cp_parser_error (parser, "expected class-name");
13516
13517   return decl;
13518 }
13519
13520 /* Parse a class-specifier.
13521
13522    class-specifier:
13523      class-head { member-specification [opt] }
13524
13525    Returns the TREE_TYPE representing the class.  */
13526
13527 static tree
13528 cp_parser_class_specifier (cp_parser* parser)
13529 {
13530   cp_token *token;
13531   tree type;
13532   tree attributes = NULL_TREE;
13533   int has_trailing_semicolon;
13534   bool nested_name_specifier_p;
13535   unsigned saved_num_template_parameter_lists;
13536   bool saved_in_function_body;
13537   tree old_scope = NULL_TREE;
13538   tree scope = NULL_TREE;
13539   tree bases;
13540
13541   push_deferring_access_checks (dk_no_deferred);
13542
13543   /* Parse the class-head.  */
13544   type = cp_parser_class_head (parser,
13545                                &nested_name_specifier_p,
13546                                &attributes,
13547                                &bases);
13548   /* If the class-head was a semantic disaster, skip the entire body
13549      of the class.  */
13550   if (!type)
13551     {
13552       cp_parser_skip_to_end_of_block_or_statement (parser);
13553       pop_deferring_access_checks ();
13554       return error_mark_node;
13555     }
13556
13557   /* Look for the `{'.  */
13558   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13559     {
13560       pop_deferring_access_checks ();
13561       return error_mark_node;
13562     }
13563
13564   /* Process the base classes. If they're invalid, skip the 
13565      entire class body.  */
13566   if (!xref_basetypes (type, bases))
13567     {
13568       cp_parser_skip_to_closing_brace (parser);
13569
13570       /* Consuming the closing brace yields better error messages
13571          later on.  */
13572       cp_lexer_consume_token (parser->lexer);
13573       pop_deferring_access_checks ();
13574       return error_mark_node;
13575     }
13576
13577   /* Issue an error message if type-definitions are forbidden here.  */
13578   cp_parser_check_type_definition (parser);
13579   /* Remember that we are defining one more class.  */
13580   ++parser->num_classes_being_defined;
13581   /* Inside the class, surrounding template-parameter-lists do not
13582      apply.  */
13583   saved_num_template_parameter_lists
13584     = parser->num_template_parameter_lists;
13585   parser->num_template_parameter_lists = 0;
13586   /* We are not in a function body.  */
13587   saved_in_function_body = parser->in_function_body;
13588   parser->in_function_body = false;
13589
13590   /* Start the class.  */
13591   if (nested_name_specifier_p)
13592     {
13593       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13594       old_scope = push_inner_scope (scope);
13595     }
13596   type = begin_class_definition (type, attributes);
13597
13598   if (type == error_mark_node)
13599     /* If the type is erroneous, skip the entire body of the class.  */
13600     cp_parser_skip_to_closing_brace (parser);
13601   else
13602     /* Parse the member-specification.  */
13603     cp_parser_member_specification_opt (parser);
13604
13605   /* Look for the trailing `}'.  */
13606   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13607   /* We get better error messages by noticing a common problem: a
13608      missing trailing `;'.  */
13609   token = cp_lexer_peek_token (parser->lexer);
13610   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13611   /* Look for trailing attributes to apply to this class.  */
13612   if (cp_parser_allow_gnu_extensions_p (parser))
13613     attributes = cp_parser_attributes_opt (parser);
13614   if (type != error_mark_node)
13615     type = finish_struct (type, attributes);
13616   if (nested_name_specifier_p)
13617     pop_inner_scope (old_scope, scope);
13618   /* If this class is not itself within the scope of another class,
13619      then we need to parse the bodies of all of the queued function
13620      definitions.  Note that the queued functions defined in a class
13621      are not always processed immediately following the
13622      class-specifier for that class.  Consider:
13623
13624        struct A {
13625          struct B { void f() { sizeof (A); } };
13626        };
13627
13628      If `f' were processed before the processing of `A' were
13629      completed, there would be no way to compute the size of `A'.
13630      Note that the nesting we are interested in here is lexical --
13631      not the semantic nesting given by TYPE_CONTEXT.  In particular,
13632      for:
13633
13634        struct A { struct B; };
13635        struct A::B { void f() { } };
13636
13637      there is no need to delay the parsing of `A::B::f'.  */
13638   if (--parser->num_classes_being_defined == 0)
13639     {
13640       tree queue_entry;
13641       tree fn;
13642       tree class_type = NULL_TREE;
13643       tree pushed_scope = NULL_TREE;
13644
13645       /* In a first pass, parse default arguments to the functions.
13646          Then, in a second pass, parse the bodies of the functions.
13647          This two-phased approach handles cases like:
13648
13649             struct S {
13650               void f() { g(); }
13651               void g(int i = 3);
13652             };
13653
13654          */
13655       for (TREE_PURPOSE (parser->unparsed_functions_queues)
13656              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13657            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13658            TREE_PURPOSE (parser->unparsed_functions_queues)
13659              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13660         {
13661           fn = TREE_VALUE (queue_entry);
13662           /* If there are default arguments that have not yet been processed,
13663              take care of them now.  */
13664           if (class_type != TREE_PURPOSE (queue_entry))
13665             {
13666               if (pushed_scope)
13667                 pop_scope (pushed_scope);
13668               class_type = TREE_PURPOSE (queue_entry);
13669               pushed_scope = push_scope (class_type);
13670             }
13671           /* Make sure that any template parameters are in scope.  */
13672           maybe_begin_member_template_processing (fn);
13673           /* Parse the default argument expressions.  */
13674           cp_parser_late_parsing_default_args (parser, fn);
13675           /* Remove any template parameters from the symbol table.  */
13676           maybe_end_member_template_processing ();
13677         }
13678       if (pushed_scope)
13679         pop_scope (pushed_scope);
13680       /* Now parse the body of the functions.  */
13681       for (TREE_VALUE (parser->unparsed_functions_queues)
13682              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13683            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13684            TREE_VALUE (parser->unparsed_functions_queues)
13685              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13686         {
13687           /* Figure out which function we need to process.  */
13688           fn = TREE_VALUE (queue_entry);
13689           /* Parse the function.  */
13690           cp_parser_late_parsing_for_member (parser, fn);
13691         }
13692     }
13693
13694   /* Put back any saved access checks.  */
13695   pop_deferring_access_checks ();
13696
13697   /* Restore saved state.  */
13698   parser->in_function_body = saved_in_function_body;
13699   parser->num_template_parameter_lists
13700     = saved_num_template_parameter_lists;
13701
13702   return type;
13703 }
13704
13705 /* Parse a class-head.
13706
13707    class-head:
13708      class-key identifier [opt] base-clause [opt]
13709      class-key nested-name-specifier identifier base-clause [opt]
13710      class-key nested-name-specifier [opt] template-id
13711        base-clause [opt]
13712
13713    GNU Extensions:
13714      class-key attributes identifier [opt] base-clause [opt]
13715      class-key attributes nested-name-specifier identifier base-clause [opt]
13716      class-key attributes nested-name-specifier [opt] template-id
13717        base-clause [opt]
13718
13719    Upon return BASES is initialized to the list of base classes (or
13720    NULL, if there are none) in the same form returned by
13721    cp_parser_base_clause.
13722
13723    Returns the TYPE of the indicated class.  Sets
13724    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13725    involving a nested-name-specifier was used, and FALSE otherwise.
13726
13727    Returns error_mark_node if this is not a class-head.
13728
13729    Returns NULL_TREE if the class-head is syntactically valid, but
13730    semantically invalid in a way that means we should skip the entire
13731    body of the class.  */
13732
13733 static tree
13734 cp_parser_class_head (cp_parser* parser,
13735                       bool* nested_name_specifier_p,
13736                       tree *attributes_p,
13737                       tree *bases)
13738 {
13739   tree nested_name_specifier;
13740   enum tag_types class_key;
13741   tree id = NULL_TREE;
13742   tree type = NULL_TREE;
13743   tree attributes;
13744   bool template_id_p = false;
13745   bool qualified_p = false;
13746   bool invalid_nested_name_p = false;
13747   bool invalid_explicit_specialization_p = false;
13748   tree pushed_scope = NULL_TREE;
13749   unsigned num_templates;
13750
13751   /* Assume no nested-name-specifier will be present.  */
13752   *nested_name_specifier_p = false;
13753   /* Assume no template parameter lists will be used in defining the
13754      type.  */
13755   num_templates = 0;
13756
13757   *bases = NULL_TREE;
13758
13759   /* Look for the class-key.  */
13760   class_key = cp_parser_class_key (parser);
13761   if (class_key == none_type)
13762     return error_mark_node;
13763
13764   /* Parse the attributes.  */
13765   attributes = cp_parser_attributes_opt (parser);
13766
13767   /* If the next token is `::', that is invalid -- but sometimes
13768      people do try to write:
13769
13770        struct ::S {};
13771
13772      Handle this gracefully by accepting the extra qualifier, and then
13773      issuing an error about it later if this really is a
13774      class-head.  If it turns out just to be an elaborated type
13775      specifier, remain silent.  */
13776   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13777     qualified_p = true;
13778
13779   push_deferring_access_checks (dk_no_check);
13780
13781   /* Determine the name of the class.  Begin by looking for an
13782      optional nested-name-specifier.  */
13783   nested_name_specifier
13784     = cp_parser_nested_name_specifier_opt (parser,
13785                                            /*typename_keyword_p=*/false,
13786                                            /*check_dependency_p=*/false,
13787                                            /*type_p=*/false,
13788                                            /*is_declaration=*/false);
13789   /* If there was a nested-name-specifier, then there *must* be an
13790      identifier.  */
13791   if (nested_name_specifier)
13792     {
13793       /* Although the grammar says `identifier', it really means
13794          `class-name' or `template-name'.  You are only allowed to
13795          define a class that has already been declared with this
13796          syntax.
13797
13798          The proposed resolution for Core Issue 180 says that wherever
13799          you see `class T::X' you should treat `X' as a type-name.
13800
13801          It is OK to define an inaccessible class; for example:
13802
13803            class A { class B; };
13804            class A::B {};
13805
13806          We do not know if we will see a class-name, or a
13807          template-name.  We look for a class-name first, in case the
13808          class-name is a template-id; if we looked for the
13809          template-name first we would stop after the template-name.  */
13810       cp_parser_parse_tentatively (parser);
13811       type = cp_parser_class_name (parser,
13812                                    /*typename_keyword_p=*/false,
13813                                    /*template_keyword_p=*/false,
13814                                    class_type,
13815                                    /*check_dependency_p=*/false,
13816                                    /*class_head_p=*/true,
13817                                    /*is_declaration=*/false);
13818       /* If that didn't work, ignore the nested-name-specifier.  */
13819       if (!cp_parser_parse_definitely (parser))
13820         {
13821           invalid_nested_name_p = true;
13822           id = cp_parser_identifier (parser);
13823           if (id == error_mark_node)
13824             id = NULL_TREE;
13825         }
13826       /* If we could not find a corresponding TYPE, treat this
13827          declaration like an unqualified declaration.  */
13828       if (type == error_mark_node)
13829         nested_name_specifier = NULL_TREE;
13830       /* Otherwise, count the number of templates used in TYPE and its
13831          containing scopes.  */
13832       else
13833         {
13834           tree scope;
13835
13836           for (scope = TREE_TYPE (type);
13837                scope && TREE_CODE (scope) != NAMESPACE_DECL;
13838                scope = (TYPE_P (scope)
13839                         ? TYPE_CONTEXT (scope)
13840                         : DECL_CONTEXT (scope)))
13841             if (TYPE_P (scope)
13842                 && CLASS_TYPE_P (scope)
13843                 && CLASSTYPE_TEMPLATE_INFO (scope)
13844                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13845                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13846               ++num_templates;
13847         }
13848     }
13849   /* Otherwise, the identifier is optional.  */
13850   else
13851     {
13852       /* We don't know whether what comes next is a template-id,
13853          an identifier, or nothing at all.  */
13854       cp_parser_parse_tentatively (parser);
13855       /* Check for a template-id.  */
13856       id = cp_parser_template_id (parser,
13857                                   /*template_keyword_p=*/false,
13858                                   /*check_dependency_p=*/true,
13859                                   /*is_declaration=*/true);
13860       /* If that didn't work, it could still be an identifier.  */
13861       if (!cp_parser_parse_definitely (parser))
13862         {
13863           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13864             id = cp_parser_identifier (parser);
13865           else
13866             id = NULL_TREE;
13867         }
13868       else
13869         {
13870           template_id_p = true;
13871           ++num_templates;
13872         }
13873     }
13874
13875   pop_deferring_access_checks ();
13876
13877   if (id)
13878     cp_parser_check_for_invalid_template_id (parser, id);
13879
13880   /* If it's not a `:' or a `{' then we can't really be looking at a
13881      class-head, since a class-head only appears as part of a
13882      class-specifier.  We have to detect this situation before calling
13883      xref_tag, since that has irreversible side-effects.  */
13884   if (!cp_parser_next_token_starts_class_definition_p (parser))
13885     {
13886       cp_parser_error (parser, "expected %<{%> or %<:%>");
13887       return error_mark_node;
13888     }
13889
13890   /* At this point, we're going ahead with the class-specifier, even
13891      if some other problem occurs.  */
13892   cp_parser_commit_to_tentative_parse (parser);
13893   /* Issue the error about the overly-qualified name now.  */
13894   if (qualified_p)
13895     cp_parser_error (parser,
13896                      "global qualification of class name is invalid");
13897   else if (invalid_nested_name_p)
13898     cp_parser_error (parser,
13899                      "qualified name does not name a class");
13900   else if (nested_name_specifier)
13901     {
13902       tree scope;
13903
13904       /* Reject typedef-names in class heads.  */
13905       if (!DECL_IMPLICIT_TYPEDEF_P (type))
13906         {
13907           error ("invalid class name in declaration of %qD", type);
13908           type = NULL_TREE;
13909           goto done;
13910         }
13911
13912       /* Figure out in what scope the declaration is being placed.  */
13913       scope = current_scope ();
13914       /* If that scope does not contain the scope in which the
13915          class was originally declared, the program is invalid.  */
13916       if (scope && !is_ancestor (scope, nested_name_specifier))
13917         {
13918           error ("declaration of %qD in %qD which does not enclose %qD",
13919                  type, scope, nested_name_specifier);
13920           type = NULL_TREE;
13921           goto done;
13922         }
13923       /* [dcl.meaning]
13924
13925          A declarator-id shall not be qualified exception of the
13926          definition of a ... nested class outside of its class
13927          ... [or] a the definition or explicit instantiation of a
13928          class member of a namespace outside of its namespace.  */
13929       if (scope == nested_name_specifier)
13930         {
13931           pedwarn ("extra qualification ignored");
13932           nested_name_specifier = NULL_TREE;
13933           num_templates = 0;
13934         }
13935     }
13936   /* An explicit-specialization must be preceded by "template <>".  If
13937      it is not, try to recover gracefully.  */
13938   if (at_namespace_scope_p ()
13939       && parser->num_template_parameter_lists == 0
13940       && template_id_p)
13941     {
13942       error ("an explicit specialization must be preceded by %<template <>%>");
13943       invalid_explicit_specialization_p = true;
13944       /* Take the same action that would have been taken by
13945          cp_parser_explicit_specialization.  */
13946       ++parser->num_template_parameter_lists;
13947       begin_specialization ();
13948     }
13949   /* There must be no "return" statements between this point and the
13950      end of this function; set "type "to the correct return value and
13951      use "goto done;" to return.  */
13952   /* Make sure that the right number of template parameters were
13953      present.  */
13954   if (!cp_parser_check_template_parameters (parser, num_templates))
13955     {
13956       /* If something went wrong, there is no point in even trying to
13957          process the class-definition.  */
13958       type = NULL_TREE;
13959       goto done;
13960     }
13961
13962   /* Look up the type.  */
13963   if (template_id_p)
13964     {
13965       type = TREE_TYPE (id);
13966       type = maybe_process_partial_specialization (type);
13967       if (nested_name_specifier)
13968         pushed_scope = push_scope (nested_name_specifier);
13969     }
13970   else if (nested_name_specifier)
13971     {
13972       tree class_type;
13973
13974       /* Given:
13975
13976             template <typename T> struct S { struct T };
13977             template <typename T> struct S<T>::T { };
13978
13979          we will get a TYPENAME_TYPE when processing the definition of
13980          `S::T'.  We need to resolve it to the actual type before we
13981          try to define it.  */
13982       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13983         {
13984           class_type = resolve_typename_type (TREE_TYPE (type),
13985                                               /*only_current_p=*/false);
13986           if (class_type != error_mark_node)
13987             type = TYPE_NAME (class_type);
13988           else
13989             {
13990               cp_parser_error (parser, "could not resolve typename type");
13991               type = error_mark_node;
13992             }
13993         }
13994
13995       maybe_process_partial_specialization (TREE_TYPE (type));
13996       class_type = current_class_type;
13997       /* Enter the scope indicated by the nested-name-specifier.  */
13998       pushed_scope = push_scope (nested_name_specifier);
13999       /* Get the canonical version of this type.  */
14000       type = TYPE_MAIN_DECL (TREE_TYPE (type));
14001       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14002           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14003         {
14004           type = push_template_decl (type);
14005           if (type == error_mark_node)
14006             {
14007               type = NULL_TREE;
14008               goto done;
14009             }
14010         }
14011
14012       type = TREE_TYPE (type);
14013       *nested_name_specifier_p = true;
14014     }
14015   else      /* The name is not a nested name.  */
14016     {
14017       /* If the class was unnamed, create a dummy name.  */
14018       if (!id)
14019         id = make_anon_name ();
14020       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14021                        parser->num_template_parameter_lists);
14022     }
14023
14024   /* Indicate whether this class was declared as a `class' or as a
14025      `struct'.  */
14026   if (TREE_CODE (type) == RECORD_TYPE)
14027     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14028   cp_parser_check_class_key (class_key, type);
14029
14030   /* If this type was already complete, and we see another definition,
14031      that's an error.  */
14032   if (type != error_mark_node && COMPLETE_TYPE_P (type))
14033     {
14034       error ("redefinition of %q#T", type);
14035       error ("previous definition of %q+#T", type);
14036       type = NULL_TREE;
14037       goto done;
14038     }
14039   else if (type == error_mark_node)
14040     type = NULL_TREE;
14041
14042   /* We will have entered the scope containing the class; the names of
14043      base classes should be looked up in that context.  For example:
14044
14045        struct A { struct B {}; struct C; };
14046        struct A::C : B {};
14047
14048      is valid.  */
14049
14050   /* Get the list of base-classes, if there is one.  */
14051   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14052     *bases = cp_parser_base_clause (parser);
14053
14054  done:
14055   /* Leave the scope given by the nested-name-specifier.  We will
14056      enter the class scope itself while processing the members.  */
14057   if (pushed_scope)
14058     pop_scope (pushed_scope);
14059
14060   if (invalid_explicit_specialization_p)
14061     {
14062       end_specialization ();
14063       --parser->num_template_parameter_lists;
14064     }
14065   *attributes_p = attributes;
14066   return type;
14067 }
14068
14069 /* Parse a class-key.
14070
14071    class-key:
14072      class
14073      struct
14074      union
14075
14076    Returns the kind of class-key specified, or none_type to indicate
14077    error.  */
14078
14079 static enum tag_types
14080 cp_parser_class_key (cp_parser* parser)
14081 {
14082   cp_token *token;
14083   enum tag_types tag_type;
14084
14085   /* Look for the class-key.  */
14086   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14087   if (!token)
14088     return none_type;
14089
14090   /* Check to see if the TOKEN is a class-key.  */
14091   tag_type = cp_parser_token_is_class_key (token);
14092   if (!tag_type)
14093     cp_parser_error (parser, "expected class-key");
14094   return tag_type;
14095 }
14096
14097 /* Parse an (optional) member-specification.
14098
14099    member-specification:
14100      member-declaration member-specification [opt]
14101      access-specifier : member-specification [opt]  */
14102
14103 static void
14104 cp_parser_member_specification_opt (cp_parser* parser)
14105 {
14106   while (true)
14107     {
14108       cp_token *token;
14109       enum rid keyword;
14110
14111       /* Peek at the next token.  */
14112       token = cp_lexer_peek_token (parser->lexer);
14113       /* If it's a `}', or EOF then we've seen all the members.  */
14114       if (token->type == CPP_CLOSE_BRACE
14115           || token->type == CPP_EOF
14116           || token->type == CPP_PRAGMA_EOL)
14117         break;
14118
14119       /* See if this token is a keyword.  */
14120       keyword = token->keyword;
14121       switch (keyword)
14122         {
14123         case RID_PUBLIC:
14124         case RID_PROTECTED:
14125         case RID_PRIVATE:
14126           /* Consume the access-specifier.  */
14127           cp_lexer_consume_token (parser->lexer);
14128           /* Remember which access-specifier is active.  */
14129           current_access_specifier = token->u.value;
14130           /* Look for the `:'.  */
14131           cp_parser_require (parser, CPP_COLON, "`:'");
14132           break;
14133
14134         default:
14135           /* Accept #pragmas at class scope.  */
14136           if (token->type == CPP_PRAGMA)
14137             {
14138               cp_parser_pragma (parser, pragma_external);
14139               break;
14140             }
14141
14142           /* Otherwise, the next construction must be a
14143              member-declaration.  */
14144           cp_parser_member_declaration (parser);
14145         }
14146     }
14147 }
14148
14149 /* Parse a member-declaration.
14150
14151    member-declaration:
14152      decl-specifier-seq [opt] member-declarator-list [opt] ;
14153      function-definition ; [opt]
14154      :: [opt] nested-name-specifier template [opt] unqualified-id ;
14155      using-declaration
14156      template-declaration
14157
14158    member-declarator-list:
14159      member-declarator
14160      member-declarator-list , member-declarator
14161
14162    member-declarator:
14163      declarator pure-specifier [opt]
14164      declarator constant-initializer [opt]
14165      identifier [opt] : constant-expression
14166
14167    GNU Extensions:
14168
14169    member-declaration:
14170      __extension__ member-declaration
14171
14172    member-declarator:
14173      declarator attributes [opt] pure-specifier [opt]
14174      declarator attributes [opt] constant-initializer [opt]
14175      identifier [opt] attributes [opt] : constant-expression  
14176
14177    C++0x Extensions:
14178
14179    member-declaration:
14180      static_assert-declaration  */
14181
14182 static void
14183 cp_parser_member_declaration (cp_parser* parser)
14184 {
14185   cp_decl_specifier_seq decl_specifiers;
14186   tree prefix_attributes;
14187   tree decl;
14188   int declares_class_or_enum;
14189   bool friend_p;
14190   cp_token *token;
14191   int saved_pedantic;
14192
14193   /* Check for the `__extension__' keyword.  */
14194   if (cp_parser_extension_opt (parser, &saved_pedantic))
14195     {
14196       /* Recurse.  */
14197       cp_parser_member_declaration (parser);
14198       /* Restore the old value of the PEDANTIC flag.  */
14199       pedantic = saved_pedantic;
14200
14201       return;
14202     }
14203
14204   /* Check for a template-declaration.  */
14205   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14206     {
14207       /* An explicit specialization here is an error condition, and we
14208          expect the specialization handler to detect and report this.  */
14209       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14210           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14211         cp_parser_explicit_specialization (parser);
14212       else
14213         cp_parser_template_declaration (parser, /*member_p=*/true);
14214
14215       return;
14216     }
14217
14218   /* Check for a using-declaration.  */
14219   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14220     {
14221       /* Parse the using-declaration.  */
14222       cp_parser_using_declaration (parser,
14223                                    /*access_declaration_p=*/false);
14224       return;
14225     }
14226
14227   /* Check for @defs.  */
14228   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14229     {
14230       tree ivar, member;
14231       tree ivar_chains = cp_parser_objc_defs_expression (parser);
14232       ivar = ivar_chains;
14233       while (ivar)
14234         {
14235           member = ivar;
14236           ivar = TREE_CHAIN (member);
14237           TREE_CHAIN (member) = NULL_TREE;
14238           finish_member_declaration (member);
14239         }
14240       return;
14241     }
14242
14243   /* If the next token is `static_assert' we have a static assertion.  */
14244   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14245     {
14246       cp_parser_static_assert (parser, /*member_p=*/true);
14247       return;
14248     }
14249
14250   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14251     return;
14252
14253   /* Parse the decl-specifier-seq.  */
14254   cp_parser_decl_specifier_seq (parser,
14255                                 CP_PARSER_FLAGS_OPTIONAL,
14256                                 &decl_specifiers,
14257                                 &declares_class_or_enum);
14258   prefix_attributes = decl_specifiers.attributes;
14259   decl_specifiers.attributes = NULL_TREE;
14260   /* Check for an invalid type-name.  */
14261   if (!decl_specifiers.type
14262       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14263     return;
14264   /* If there is no declarator, then the decl-specifier-seq should
14265      specify a type.  */
14266   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14267     {
14268       /* If there was no decl-specifier-seq, and the next token is a
14269          `;', then we have something like:
14270
14271            struct S { ; };
14272
14273          [class.mem]
14274
14275          Each member-declaration shall declare at least one member
14276          name of the class.  */
14277       if (!decl_specifiers.any_specifiers_p)
14278         {
14279           cp_token *token = cp_lexer_peek_token (parser->lexer);
14280           if (pedantic && !token->in_system_header)
14281             pedwarn ("%Hextra %<;%>", &token->location);
14282         }
14283       else
14284         {
14285           tree type;
14286
14287           /* See if this declaration is a friend.  */
14288           friend_p = cp_parser_friend_p (&decl_specifiers);
14289           /* If there were decl-specifiers, check to see if there was
14290              a class-declaration.  */
14291           type = check_tag_decl (&decl_specifiers);
14292           /* Nested classes have already been added to the class, but
14293              a `friend' needs to be explicitly registered.  */
14294           if (friend_p)
14295             {
14296               /* If the `friend' keyword was present, the friend must
14297                  be introduced with a class-key.  */
14298                if (!declares_class_or_enum)
14299                  error ("a class-key must be used when declaring a friend");
14300                /* In this case:
14301
14302                     template <typename T> struct A {
14303                       friend struct A<T>::B;
14304                     };
14305
14306                   A<T>::B will be represented by a TYPENAME_TYPE, and
14307                   therefore not recognized by check_tag_decl.  */
14308                if (!type
14309                    && decl_specifiers.type
14310                    && TYPE_P (decl_specifiers.type))
14311                  type = decl_specifiers.type;
14312                if (!type || !TYPE_P (type))
14313                  error ("friend declaration does not name a class or "
14314                         "function");
14315                else
14316                  make_friend_class (current_class_type, type,
14317                                     /*complain=*/true);
14318             }
14319           /* If there is no TYPE, an error message will already have
14320              been issued.  */
14321           else if (!type || type == error_mark_node)
14322             ;
14323           /* An anonymous aggregate has to be handled specially; such
14324              a declaration really declares a data member (with a
14325              particular type), as opposed to a nested class.  */
14326           else if (ANON_AGGR_TYPE_P (type))
14327             {
14328               /* Remove constructors and such from TYPE, now that we
14329                  know it is an anonymous aggregate.  */
14330               fixup_anonymous_aggr (type);
14331               /* And make the corresponding data member.  */
14332               decl = build_decl (FIELD_DECL, NULL_TREE, type);
14333               /* Add it to the class.  */
14334               finish_member_declaration (decl);
14335             }
14336           else
14337             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14338         }
14339     }
14340   else
14341     {
14342       /* See if these declarations will be friends.  */
14343       friend_p = cp_parser_friend_p (&decl_specifiers);
14344
14345       /* Keep going until we hit the `;' at the end of the
14346          declaration.  */
14347       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14348         {
14349           tree attributes = NULL_TREE;
14350           tree first_attribute;
14351
14352           /* Peek at the next token.  */
14353           token = cp_lexer_peek_token (parser->lexer);
14354
14355           /* Check for a bitfield declaration.  */
14356           if (token->type == CPP_COLON
14357               || (token->type == CPP_NAME
14358                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14359                   == CPP_COLON))
14360             {
14361               tree identifier;
14362               tree width;
14363
14364               /* Get the name of the bitfield.  Note that we cannot just
14365                  check TOKEN here because it may have been invalidated by
14366                  the call to cp_lexer_peek_nth_token above.  */
14367               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14368                 identifier = cp_parser_identifier (parser);
14369               else
14370                 identifier = NULL_TREE;
14371
14372               /* Consume the `:' token.  */
14373               cp_lexer_consume_token (parser->lexer);
14374               /* Get the width of the bitfield.  */
14375               width
14376                 = cp_parser_constant_expression (parser,
14377                                                  /*allow_non_constant=*/false,
14378                                                  NULL);
14379
14380               /* Look for attributes that apply to the bitfield.  */
14381               attributes = cp_parser_attributes_opt (parser);
14382               /* Remember which attributes are prefix attributes and
14383                  which are not.  */
14384               first_attribute = attributes;
14385               /* Combine the attributes.  */
14386               attributes = chainon (prefix_attributes, attributes);
14387
14388               /* Create the bitfield declaration.  */
14389               decl = grokbitfield (identifier
14390                                    ? make_id_declarator (NULL_TREE,
14391                                                          identifier,
14392                                                          sfk_none)
14393                                    : NULL,
14394                                    &decl_specifiers,
14395                                    width);
14396               /* Apply the attributes.  */
14397               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14398             }
14399           else
14400             {
14401               cp_declarator *declarator;
14402               tree initializer;
14403               tree asm_specification;
14404               int ctor_dtor_or_conv_p;
14405
14406               /* Parse the declarator.  */
14407               declarator
14408                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14409                                         &ctor_dtor_or_conv_p,
14410                                         /*parenthesized_p=*/NULL,
14411                                         /*member_p=*/true);
14412
14413               /* If something went wrong parsing the declarator, make sure
14414                  that we at least consume some tokens.  */
14415               if (declarator == cp_error_declarator)
14416                 {
14417                   /* Skip to the end of the statement.  */
14418                   cp_parser_skip_to_end_of_statement (parser);
14419                   /* If the next token is not a semicolon, that is
14420                      probably because we just skipped over the body of
14421                      a function.  So, we consume a semicolon if
14422                      present, but do not issue an error message if it
14423                      is not present.  */
14424                   if (cp_lexer_next_token_is (parser->lexer,
14425                                               CPP_SEMICOLON))
14426                     cp_lexer_consume_token (parser->lexer);
14427                   return;
14428                 }
14429
14430               if (declares_class_or_enum & 2)
14431                 cp_parser_check_for_definition_in_return_type
14432                   (declarator, decl_specifiers.type);
14433
14434               /* Look for an asm-specification.  */
14435               asm_specification = cp_parser_asm_specification_opt (parser);
14436               /* Look for attributes that apply to the declaration.  */
14437               attributes = cp_parser_attributes_opt (parser);
14438               /* Remember which attributes are prefix attributes and
14439                  which are not.  */
14440               first_attribute = attributes;
14441               /* Combine the attributes.  */
14442               attributes = chainon (prefix_attributes, attributes);
14443
14444               /* If it's an `=', then we have a constant-initializer or a
14445                  pure-specifier.  It is not correct to parse the
14446                  initializer before registering the member declaration
14447                  since the member declaration should be in scope while
14448                  its initializer is processed.  However, the rest of the
14449                  front end does not yet provide an interface that allows
14450                  us to handle this correctly.  */
14451               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14452                 {
14453                   /* In [class.mem]:
14454
14455                      A pure-specifier shall be used only in the declaration of
14456                      a virtual function.
14457
14458                      A member-declarator can contain a constant-initializer
14459                      only if it declares a static member of integral or
14460                      enumeration type.
14461
14462                      Therefore, if the DECLARATOR is for a function, we look
14463                      for a pure-specifier; otherwise, we look for a
14464                      constant-initializer.  When we call `grokfield', it will
14465                      perform more stringent semantics checks.  */
14466                   if (function_declarator_p (declarator))
14467                     initializer = cp_parser_pure_specifier (parser);
14468                   else
14469                     /* Parse the initializer.  */
14470                     initializer = cp_parser_constant_initializer (parser);
14471                 }
14472               /* Otherwise, there is no initializer.  */
14473               else
14474                 initializer = NULL_TREE;
14475
14476               /* See if we are probably looking at a function
14477                  definition.  We are certainly not looking at a
14478                  member-declarator.  Calling `grokfield' has
14479                  side-effects, so we must not do it unless we are sure
14480                  that we are looking at a member-declarator.  */
14481               if (cp_parser_token_starts_function_definition_p
14482                   (cp_lexer_peek_token (parser->lexer)))
14483                 {
14484                   /* The grammar does not allow a pure-specifier to be
14485                      used when a member function is defined.  (It is
14486                      possible that this fact is an oversight in the
14487                      standard, since a pure function may be defined
14488                      outside of the class-specifier.  */
14489                   if (initializer)
14490                     error ("pure-specifier on function-definition");
14491                   decl = cp_parser_save_member_function_body (parser,
14492                                                               &decl_specifiers,
14493                                                               declarator,
14494                                                               attributes);
14495                   /* If the member was not a friend, declare it here.  */
14496                   if (!friend_p)
14497                     finish_member_declaration (decl);
14498                   /* Peek at the next token.  */
14499                   token = cp_lexer_peek_token (parser->lexer);
14500                   /* If the next token is a semicolon, consume it.  */
14501                   if (token->type == CPP_SEMICOLON)
14502                     {
14503                       if (pedantic && !in_system_header)
14504                         pedwarn ("extra %<;%>");
14505                       cp_lexer_consume_token (parser->lexer);
14506                     }
14507                   return;
14508                 }
14509               else
14510                 /* Create the declaration.  */
14511                 decl = grokfield (declarator, &decl_specifiers,
14512                                   initializer, /*init_const_expr_p=*/true,
14513                                   asm_specification,
14514                                   attributes);
14515             }
14516
14517           /* Reset PREFIX_ATTRIBUTES.  */
14518           while (attributes && TREE_CHAIN (attributes) != first_attribute)
14519             attributes = TREE_CHAIN (attributes);
14520           if (attributes)
14521             TREE_CHAIN (attributes) = NULL_TREE;
14522
14523           /* If there is any qualification still in effect, clear it
14524              now; we will be starting fresh with the next declarator.  */
14525           parser->scope = NULL_TREE;
14526           parser->qualifying_scope = NULL_TREE;
14527           parser->object_scope = NULL_TREE;
14528           /* If it's a `,', then there are more declarators.  */
14529           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14530             cp_lexer_consume_token (parser->lexer);
14531           /* If the next token isn't a `;', then we have a parse error.  */
14532           else if (cp_lexer_next_token_is_not (parser->lexer,
14533                                                CPP_SEMICOLON))
14534             {
14535               cp_parser_error (parser, "expected %<;%>");
14536               /* Skip tokens until we find a `;'.  */
14537               cp_parser_skip_to_end_of_statement (parser);
14538
14539               break;
14540             }
14541
14542           if (decl)
14543             {
14544               /* Add DECL to the list of members.  */
14545               if (!friend_p)
14546                 finish_member_declaration (decl);
14547
14548               if (TREE_CODE (decl) == FUNCTION_DECL)
14549                 cp_parser_save_default_args (parser, decl);
14550             }
14551         }
14552     }
14553
14554   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14555 }
14556
14557 /* Parse a pure-specifier.
14558
14559    pure-specifier:
14560      = 0
14561
14562    Returns INTEGER_ZERO_NODE if a pure specifier is found.
14563    Otherwise, ERROR_MARK_NODE is returned.  */
14564
14565 static tree
14566 cp_parser_pure_specifier (cp_parser* parser)
14567 {
14568   cp_token *token;
14569
14570   /* Look for the `=' token.  */
14571   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14572     return error_mark_node;
14573   /* Look for the `0' token.  */
14574   token = cp_lexer_consume_token (parser->lexer);
14575   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
14576   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14577     {
14578       cp_parser_error (parser,
14579                        "invalid pure specifier (only `= 0' is allowed)");
14580       cp_parser_skip_to_end_of_statement (parser);
14581       return error_mark_node;
14582     }
14583   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14584     {
14585       error ("templates may not be %<virtual%>");
14586       return error_mark_node;
14587     }
14588
14589   return integer_zero_node;
14590 }
14591
14592 /* Parse a constant-initializer.
14593
14594    constant-initializer:
14595      = constant-expression
14596
14597    Returns a representation of the constant-expression.  */
14598
14599 static tree
14600 cp_parser_constant_initializer (cp_parser* parser)
14601 {
14602   /* Look for the `=' token.  */
14603   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14604     return error_mark_node;
14605
14606   /* It is invalid to write:
14607
14608        struct S { static const int i = { 7 }; };
14609
14610      */
14611   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14612     {
14613       cp_parser_error (parser,
14614                        "a brace-enclosed initializer is not allowed here");
14615       /* Consume the opening brace.  */
14616       cp_lexer_consume_token (parser->lexer);
14617       /* Skip the initializer.  */
14618       cp_parser_skip_to_closing_brace (parser);
14619       /* Look for the trailing `}'.  */
14620       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14621
14622       return error_mark_node;
14623     }
14624
14625   return cp_parser_constant_expression (parser,
14626                                         /*allow_non_constant=*/false,
14627                                         NULL);
14628 }
14629
14630 /* Derived classes [gram.class.derived] */
14631
14632 /* Parse a base-clause.
14633
14634    base-clause:
14635      : base-specifier-list
14636
14637    base-specifier-list:
14638      base-specifier ... [opt]
14639      base-specifier-list , base-specifier ... [opt]
14640
14641    Returns a TREE_LIST representing the base-classes, in the order in
14642    which they were declared.  The representation of each node is as
14643    described by cp_parser_base_specifier.
14644
14645    In the case that no bases are specified, this function will return
14646    NULL_TREE, not ERROR_MARK_NODE.  */
14647
14648 static tree
14649 cp_parser_base_clause (cp_parser* parser)
14650 {
14651   tree bases = NULL_TREE;
14652
14653   /* Look for the `:' that begins the list.  */
14654   cp_parser_require (parser, CPP_COLON, "`:'");
14655
14656   /* Scan the base-specifier-list.  */
14657   while (true)
14658     {
14659       cp_token *token;
14660       tree base;
14661       bool pack_expansion_p = false;
14662
14663       /* Look for the base-specifier.  */
14664       base = cp_parser_base_specifier (parser);
14665       /* Look for the (optional) ellipsis. */
14666       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14667         {
14668           /* Consume the `...'. */
14669           cp_lexer_consume_token (parser->lexer);
14670
14671           pack_expansion_p = true;
14672         }
14673
14674       /* Add BASE to the front of the list.  */
14675       if (base != error_mark_node)
14676         {
14677           if (pack_expansion_p)
14678             /* Make this a pack expansion type. */
14679             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
14680           else
14681             check_for_bare_parameter_packs (TREE_VALUE (base));
14682
14683           TREE_CHAIN (base) = bases;
14684           bases = base;
14685         }
14686       /* Peek at the next token.  */
14687       token = cp_lexer_peek_token (parser->lexer);
14688       /* If it's not a comma, then the list is complete.  */
14689       if (token->type != CPP_COMMA)
14690         break;
14691       /* Consume the `,'.  */
14692       cp_lexer_consume_token (parser->lexer);
14693     }
14694
14695   /* PARSER->SCOPE may still be non-NULL at this point, if the last
14696      base class had a qualified name.  However, the next name that
14697      appears is certainly not qualified.  */
14698   parser->scope = NULL_TREE;
14699   parser->qualifying_scope = NULL_TREE;
14700   parser->object_scope = NULL_TREE;
14701
14702   return nreverse (bases);
14703 }
14704
14705 /* Parse a base-specifier.
14706
14707    base-specifier:
14708      :: [opt] nested-name-specifier [opt] class-name
14709      virtual access-specifier [opt] :: [opt] nested-name-specifier
14710        [opt] class-name
14711      access-specifier virtual [opt] :: [opt] nested-name-specifier
14712        [opt] class-name
14713
14714    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14715    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14716    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14717    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14718
14719 static tree
14720 cp_parser_base_specifier (cp_parser* parser)
14721 {
14722   cp_token *token;
14723   bool done = false;
14724   bool virtual_p = false;
14725   bool duplicate_virtual_error_issued_p = false;
14726   bool duplicate_access_error_issued_p = false;
14727   bool class_scope_p, template_p;
14728   tree access = access_default_node;
14729   tree type;
14730
14731   /* Process the optional `virtual' and `access-specifier'.  */
14732   while (!done)
14733     {
14734       /* Peek at the next token.  */
14735       token = cp_lexer_peek_token (parser->lexer);
14736       /* Process `virtual'.  */
14737       switch (token->keyword)
14738         {
14739         case RID_VIRTUAL:
14740           /* If `virtual' appears more than once, issue an error.  */
14741           if (virtual_p && !duplicate_virtual_error_issued_p)
14742             {
14743               cp_parser_error (parser,
14744                                "%<virtual%> specified more than once in base-specified");
14745               duplicate_virtual_error_issued_p = true;
14746             }
14747
14748           virtual_p = true;
14749
14750           /* Consume the `virtual' token.  */
14751           cp_lexer_consume_token (parser->lexer);
14752
14753           break;
14754
14755         case RID_PUBLIC:
14756         case RID_PROTECTED:
14757         case RID_PRIVATE:
14758           /* If more than one access specifier appears, issue an
14759              error.  */
14760           if (access != access_default_node
14761               && !duplicate_access_error_issued_p)
14762             {
14763               cp_parser_error (parser,
14764                                "more than one access specifier in base-specified");
14765               duplicate_access_error_issued_p = true;
14766             }
14767
14768           access = ridpointers[(int) token->keyword];
14769
14770           /* Consume the access-specifier.  */
14771           cp_lexer_consume_token (parser->lexer);
14772
14773           break;
14774
14775         default:
14776           done = true;
14777           break;
14778         }
14779     }
14780   /* It is not uncommon to see programs mechanically, erroneously, use
14781      the 'typename' keyword to denote (dependent) qualified types
14782      as base classes.  */
14783   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14784     {
14785       if (!processing_template_decl)
14786         error ("keyword %<typename%> not allowed outside of templates");
14787       else
14788         error ("keyword %<typename%> not allowed in this context "
14789                "(the base class is implicitly a type)");
14790       cp_lexer_consume_token (parser->lexer);
14791     }
14792
14793   /* Look for the optional `::' operator.  */
14794   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14795   /* Look for the nested-name-specifier.  The simplest way to
14796      implement:
14797
14798        [temp.res]
14799
14800        The keyword `typename' is not permitted in a base-specifier or
14801        mem-initializer; in these contexts a qualified name that
14802        depends on a template-parameter is implicitly assumed to be a
14803        type name.
14804
14805      is to pretend that we have seen the `typename' keyword at this
14806      point.  */
14807   cp_parser_nested_name_specifier_opt (parser,
14808                                        /*typename_keyword_p=*/true,
14809                                        /*check_dependency_p=*/true,
14810                                        typename_type,
14811                                        /*is_declaration=*/true);
14812   /* If the base class is given by a qualified name, assume that names
14813      we see are type names or templates, as appropriate.  */
14814   class_scope_p = (parser->scope && TYPE_P (parser->scope));
14815   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14816
14817   /* Finally, look for the class-name.  */
14818   type = cp_parser_class_name (parser,
14819                                class_scope_p,
14820                                template_p,
14821                                typename_type,
14822                                /*check_dependency_p=*/true,
14823                                /*class_head_p=*/false,
14824                                /*is_declaration=*/true);
14825
14826   if (type == error_mark_node)
14827     return error_mark_node;
14828
14829   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14830 }
14831
14832 /* Exception handling [gram.exception] */
14833
14834 /* Parse an (optional) exception-specification.
14835
14836    exception-specification:
14837      throw ( type-id-list [opt] )
14838
14839    Returns a TREE_LIST representing the exception-specification.  The
14840    TREE_VALUE of each node is a type.  */
14841
14842 static tree
14843 cp_parser_exception_specification_opt (cp_parser* parser)
14844 {
14845   cp_token *token;
14846   tree type_id_list;
14847
14848   /* Peek at the next token.  */
14849   token = cp_lexer_peek_token (parser->lexer);
14850   /* If it's not `throw', then there's no exception-specification.  */
14851   if (!cp_parser_is_keyword (token, RID_THROW))
14852     return NULL_TREE;
14853
14854   /* Consume the `throw'.  */
14855   cp_lexer_consume_token (parser->lexer);
14856
14857   /* Look for the `('.  */
14858   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14859
14860   /* Peek at the next token.  */
14861   token = cp_lexer_peek_token (parser->lexer);
14862   /* If it's not a `)', then there is a type-id-list.  */
14863   if (token->type != CPP_CLOSE_PAREN)
14864     {
14865       const char *saved_message;
14866
14867       /* Types may not be defined in an exception-specification.  */
14868       saved_message = parser->type_definition_forbidden_message;
14869       parser->type_definition_forbidden_message
14870         = "types may not be defined in an exception-specification";
14871       /* Parse the type-id-list.  */
14872       type_id_list = cp_parser_type_id_list (parser);
14873       /* Restore the saved message.  */
14874       parser->type_definition_forbidden_message = saved_message;
14875     }
14876   else
14877     type_id_list = empty_except_spec;
14878
14879   /* Look for the `)'.  */
14880   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14881
14882   return type_id_list;
14883 }
14884
14885 /* Parse an (optional) type-id-list.
14886
14887    type-id-list:
14888      type-id ... [opt]
14889      type-id-list , type-id ... [opt]
14890
14891    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14892    in the order that the types were presented.  */
14893
14894 static tree
14895 cp_parser_type_id_list (cp_parser* parser)
14896 {
14897   tree types = NULL_TREE;
14898
14899   while (true)
14900     {
14901       cp_token *token;
14902       tree type;
14903
14904       /* Get the next type-id.  */
14905       type = cp_parser_type_id (parser);
14906       /* Parse the optional ellipsis. */
14907       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14908         {
14909           /* Consume the `...'. */
14910           cp_lexer_consume_token (parser->lexer);
14911
14912           /* Turn the type into a pack expansion expression. */
14913           type = make_pack_expansion (type);
14914         }
14915       /* Add it to the list.  */
14916       types = add_exception_specifier (types, type, /*complain=*/1);
14917       /* Peek at the next token.  */
14918       token = cp_lexer_peek_token (parser->lexer);
14919       /* If it is not a `,', we are done.  */
14920       if (token->type != CPP_COMMA)
14921         break;
14922       /* Consume the `,'.  */
14923       cp_lexer_consume_token (parser->lexer);
14924     }
14925
14926   return nreverse (types);
14927 }
14928
14929 /* Parse a try-block.
14930
14931    try-block:
14932      try compound-statement handler-seq  */
14933
14934 static tree
14935 cp_parser_try_block (cp_parser* parser)
14936 {
14937   tree try_block;
14938
14939   cp_parser_require_keyword (parser, RID_TRY, "`try'");
14940   try_block = begin_try_block ();
14941   cp_parser_compound_statement (parser, NULL, true);
14942   finish_try_block (try_block);
14943   cp_parser_handler_seq (parser);
14944   finish_handler_sequence (try_block);
14945
14946   return try_block;
14947 }
14948
14949 /* Parse a function-try-block.
14950
14951    function-try-block:
14952      try ctor-initializer [opt] function-body handler-seq  */
14953
14954 static bool
14955 cp_parser_function_try_block (cp_parser* parser)
14956 {
14957   tree compound_stmt;
14958   tree try_block;
14959   bool ctor_initializer_p;
14960
14961   /* Look for the `try' keyword.  */
14962   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14963     return false;
14964   /* Let the rest of the front end know where we are.  */
14965   try_block = begin_function_try_block (&compound_stmt);
14966   /* Parse the function-body.  */
14967   ctor_initializer_p
14968     = cp_parser_ctor_initializer_opt_and_function_body (parser);
14969   /* We're done with the `try' part.  */
14970   finish_function_try_block (try_block);
14971   /* Parse the handlers.  */
14972   cp_parser_handler_seq (parser);
14973   /* We're done with the handlers.  */
14974   finish_function_handler_sequence (try_block, compound_stmt);
14975
14976   return ctor_initializer_p;
14977 }
14978
14979 /* Parse a handler-seq.
14980
14981    handler-seq:
14982      handler handler-seq [opt]  */
14983
14984 static void
14985 cp_parser_handler_seq (cp_parser* parser)
14986 {
14987   while (true)
14988     {
14989       cp_token *token;
14990
14991       /* Parse the handler.  */
14992       cp_parser_handler (parser);
14993       /* Peek at the next token.  */
14994       token = cp_lexer_peek_token (parser->lexer);
14995       /* If it's not `catch' then there are no more handlers.  */
14996       if (!cp_parser_is_keyword (token, RID_CATCH))
14997         break;
14998     }
14999 }
15000
15001 /* Parse a handler.
15002
15003    handler:
15004      catch ( exception-declaration ) compound-statement  */
15005
15006 static void
15007 cp_parser_handler (cp_parser* parser)
15008 {
15009   tree handler;
15010   tree declaration;
15011
15012   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15013   handler = begin_handler ();
15014   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15015   declaration = cp_parser_exception_declaration (parser);
15016   finish_handler_parms (declaration, handler);
15017   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15018   cp_parser_compound_statement (parser, NULL, false);
15019   finish_handler (handler);
15020 }
15021
15022 /* Parse an exception-declaration.
15023
15024    exception-declaration:
15025      type-specifier-seq declarator
15026      type-specifier-seq abstract-declarator
15027      type-specifier-seq
15028      ...
15029
15030    Returns a VAR_DECL for the declaration, or NULL_TREE if the
15031    ellipsis variant is used.  */
15032
15033 static tree
15034 cp_parser_exception_declaration (cp_parser* parser)
15035 {
15036   cp_decl_specifier_seq type_specifiers;
15037   cp_declarator *declarator;
15038   const char *saved_message;
15039
15040   /* If it's an ellipsis, it's easy to handle.  */
15041   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15042     {
15043       /* Consume the `...' token.  */
15044       cp_lexer_consume_token (parser->lexer);
15045       return NULL_TREE;
15046     }
15047
15048   /* Types may not be defined in exception-declarations.  */
15049   saved_message = parser->type_definition_forbidden_message;
15050   parser->type_definition_forbidden_message
15051     = "types may not be defined in exception-declarations";
15052
15053   /* Parse the type-specifier-seq.  */
15054   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15055                                 &type_specifiers);
15056   /* If it's a `)', then there is no declarator.  */
15057   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15058     declarator = NULL;
15059   else
15060     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15061                                        /*ctor_dtor_or_conv_p=*/NULL,
15062                                        /*parenthesized_p=*/NULL,
15063                                        /*member_p=*/false);
15064
15065   /* Restore the saved message.  */
15066   parser->type_definition_forbidden_message = saved_message;
15067
15068   if (!type_specifiers.any_specifiers_p)
15069     return error_mark_node;
15070
15071   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15072 }
15073
15074 /* Parse a throw-expression.
15075
15076    throw-expression:
15077      throw assignment-expression [opt]
15078
15079    Returns a THROW_EXPR representing the throw-expression.  */
15080
15081 static tree
15082 cp_parser_throw_expression (cp_parser* parser)
15083 {
15084   tree expression;
15085   cp_token* token;
15086
15087   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15088   token = cp_lexer_peek_token (parser->lexer);
15089   /* Figure out whether or not there is an assignment-expression
15090      following the "throw" keyword.  */
15091   if (token->type == CPP_COMMA
15092       || token->type == CPP_SEMICOLON
15093       || token->type == CPP_CLOSE_PAREN
15094       || token->type == CPP_CLOSE_SQUARE
15095       || token->type == CPP_CLOSE_BRACE
15096       || token->type == CPP_COLON)
15097     expression = NULL_TREE;
15098   else
15099     expression = cp_parser_assignment_expression (parser,
15100                                                   /*cast_p=*/false);
15101
15102   return build_throw (expression);
15103 }
15104
15105 /* GNU Extensions */
15106
15107 /* Parse an (optional) asm-specification.
15108
15109    asm-specification:
15110      asm ( string-literal )
15111
15112    If the asm-specification is present, returns a STRING_CST
15113    corresponding to the string-literal.  Otherwise, returns
15114    NULL_TREE.  */
15115
15116 static tree
15117 cp_parser_asm_specification_opt (cp_parser* parser)
15118 {
15119   cp_token *token;
15120   tree asm_specification;
15121
15122   /* Peek at the next token.  */
15123   token = cp_lexer_peek_token (parser->lexer);
15124   /* If the next token isn't the `asm' keyword, then there's no
15125      asm-specification.  */
15126   if (!cp_parser_is_keyword (token, RID_ASM))
15127     return NULL_TREE;
15128
15129   /* Consume the `asm' token.  */
15130   cp_lexer_consume_token (parser->lexer);
15131   /* Look for the `('.  */
15132   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15133
15134   /* Look for the string-literal.  */
15135   asm_specification = cp_parser_string_literal (parser, false, false);
15136
15137   /* Look for the `)'.  */
15138   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15139
15140   return asm_specification;
15141 }
15142
15143 /* Parse an asm-operand-list.
15144
15145    asm-operand-list:
15146      asm-operand
15147      asm-operand-list , asm-operand
15148
15149    asm-operand:
15150      string-literal ( expression )
15151      [ string-literal ] string-literal ( expression )
15152
15153    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
15154    each node is the expression.  The TREE_PURPOSE is itself a
15155    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15156    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15157    is a STRING_CST for the string literal before the parenthesis.  */
15158
15159 static tree
15160 cp_parser_asm_operand_list (cp_parser* parser)
15161 {
15162   tree asm_operands = NULL_TREE;
15163
15164   while (true)
15165     {
15166       tree string_literal;
15167       tree expression;
15168       tree name;
15169
15170       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15171         {
15172           /* Consume the `[' token.  */
15173           cp_lexer_consume_token (parser->lexer);
15174           /* Read the operand name.  */
15175           name = cp_parser_identifier (parser);
15176           if (name != error_mark_node)
15177             name = build_string (IDENTIFIER_LENGTH (name),
15178                                  IDENTIFIER_POINTER (name));
15179           /* Look for the closing `]'.  */
15180           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15181         }
15182       else
15183         name = NULL_TREE;
15184       /* Look for the string-literal.  */
15185       string_literal = cp_parser_string_literal (parser, false, false);
15186
15187       /* Look for the `('.  */
15188       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15189       /* Parse the expression.  */
15190       expression = cp_parser_expression (parser, /*cast_p=*/false);
15191       /* Look for the `)'.  */
15192       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15193
15194       /* Add this operand to the list.  */
15195       asm_operands = tree_cons (build_tree_list (name, string_literal),
15196                                 expression,
15197                                 asm_operands);
15198       /* If the next token is not a `,', there are no more
15199          operands.  */
15200       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15201         break;
15202       /* Consume the `,'.  */
15203       cp_lexer_consume_token (parser->lexer);
15204     }
15205
15206   return nreverse (asm_operands);
15207 }
15208
15209 /* Parse an asm-clobber-list.
15210
15211    asm-clobber-list:
15212      string-literal
15213      asm-clobber-list , string-literal
15214
15215    Returns a TREE_LIST, indicating the clobbers in the order that they
15216    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
15217
15218 static tree
15219 cp_parser_asm_clobber_list (cp_parser* parser)
15220 {
15221   tree clobbers = NULL_TREE;
15222
15223   while (true)
15224     {
15225       tree string_literal;
15226
15227       /* Look for the string literal.  */
15228       string_literal = cp_parser_string_literal (parser, false, false);
15229       /* Add it to the list.  */
15230       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15231       /* If the next token is not a `,', then the list is
15232          complete.  */
15233       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15234         break;
15235       /* Consume the `,' token.  */
15236       cp_lexer_consume_token (parser->lexer);
15237     }
15238
15239   return clobbers;
15240 }
15241
15242 /* Parse an (optional) series of attributes.
15243
15244    attributes:
15245      attributes attribute
15246
15247    attribute:
15248      __attribute__ (( attribute-list [opt] ))
15249
15250    The return value is as for cp_parser_attribute_list.  */
15251
15252 static tree
15253 cp_parser_attributes_opt (cp_parser* parser)
15254 {
15255   tree attributes = NULL_TREE;
15256
15257   while (true)
15258     {
15259       cp_token *token;
15260       tree attribute_list;
15261
15262       /* Peek at the next token.  */
15263       token = cp_lexer_peek_token (parser->lexer);
15264       /* If it's not `__attribute__', then we're done.  */
15265       if (token->keyword != RID_ATTRIBUTE)
15266         break;
15267
15268       /* Consume the `__attribute__' keyword.  */
15269       cp_lexer_consume_token (parser->lexer);
15270       /* Look for the two `(' tokens.  */
15271       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15272       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15273
15274       /* Peek at the next token.  */
15275       token = cp_lexer_peek_token (parser->lexer);
15276       if (token->type != CPP_CLOSE_PAREN)
15277         /* Parse the attribute-list.  */
15278         attribute_list = cp_parser_attribute_list (parser);
15279       else
15280         /* If the next token is a `)', then there is no attribute
15281            list.  */
15282         attribute_list = NULL;
15283
15284       /* Look for the two `)' tokens.  */
15285       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15286       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15287
15288       /* Add these new attributes to the list.  */
15289       attributes = chainon (attributes, attribute_list);
15290     }
15291
15292   return attributes;
15293 }
15294
15295 /* Parse an attribute-list.
15296
15297    attribute-list:
15298      attribute
15299      attribute-list , attribute
15300
15301    attribute:
15302      identifier
15303      identifier ( identifier )
15304      identifier ( identifier , expression-list )
15305      identifier ( expression-list )
15306
15307    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
15308    to an attribute.  The TREE_PURPOSE of each node is the identifier
15309    indicating which attribute is in use.  The TREE_VALUE represents
15310    the arguments, if any.  */
15311
15312 static tree
15313 cp_parser_attribute_list (cp_parser* parser)
15314 {
15315   tree attribute_list = NULL_TREE;
15316   bool save_translate_strings_p = parser->translate_strings_p;
15317
15318   parser->translate_strings_p = false;
15319   while (true)
15320     {
15321       cp_token *token;
15322       tree identifier;
15323       tree attribute;
15324
15325       /* Look for the identifier.  We also allow keywords here; for
15326          example `__attribute__ ((const))' is legal.  */
15327       token = cp_lexer_peek_token (parser->lexer);
15328       if (token->type == CPP_NAME
15329           || token->type == CPP_KEYWORD)
15330         {
15331           tree arguments = NULL_TREE;
15332
15333           /* Consume the token.  */
15334           token = cp_lexer_consume_token (parser->lexer);
15335
15336           /* Save away the identifier that indicates which attribute
15337              this is.  */
15338           identifier = token->u.value;
15339           attribute = build_tree_list (identifier, NULL_TREE);
15340
15341           /* Peek at the next token.  */
15342           token = cp_lexer_peek_token (parser->lexer);
15343           /* If it's an `(', then parse the attribute arguments.  */
15344           if (token->type == CPP_OPEN_PAREN)
15345             {
15346               arguments = cp_parser_parenthesized_expression_list
15347                           (parser, true, /*cast_p=*/false,
15348                            /*allow_expansion_p=*/false,
15349                            /*non_constant_p=*/NULL);
15350               /* Save the arguments away.  */
15351               TREE_VALUE (attribute) = arguments;
15352             }
15353
15354           if (arguments != error_mark_node)
15355             {
15356               /* Add this attribute to the list.  */
15357               TREE_CHAIN (attribute) = attribute_list;
15358               attribute_list = attribute;
15359             }
15360
15361           token = cp_lexer_peek_token (parser->lexer);
15362         }
15363       /* Now, look for more attributes.  If the next token isn't a
15364          `,', we're done.  */
15365       if (token->type != CPP_COMMA)
15366         break;
15367
15368       /* Consume the comma and keep going.  */
15369       cp_lexer_consume_token (parser->lexer);
15370     }
15371   parser->translate_strings_p = save_translate_strings_p;
15372
15373   /* We built up the list in reverse order.  */
15374   return nreverse (attribute_list);
15375 }
15376
15377 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
15378    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
15379    current value of the PEDANTIC flag, regardless of whether or not
15380    the `__extension__' keyword is present.  The caller is responsible
15381    for restoring the value of the PEDANTIC flag.  */
15382
15383 static bool
15384 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15385 {
15386   /* Save the old value of the PEDANTIC flag.  */
15387   *saved_pedantic = pedantic;
15388
15389   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15390     {
15391       /* Consume the `__extension__' token.  */
15392       cp_lexer_consume_token (parser->lexer);
15393       /* We're not being pedantic while the `__extension__' keyword is
15394          in effect.  */
15395       pedantic = 0;
15396
15397       return true;
15398     }
15399
15400   return false;
15401 }
15402
15403 /* Parse a label declaration.
15404
15405    label-declaration:
15406      __label__ label-declarator-seq ;
15407
15408    label-declarator-seq:
15409      identifier , label-declarator-seq
15410      identifier  */
15411
15412 static void
15413 cp_parser_label_declaration (cp_parser* parser)
15414 {
15415   /* Look for the `__label__' keyword.  */
15416   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15417
15418   while (true)
15419     {
15420       tree identifier;
15421
15422       /* Look for an identifier.  */
15423       identifier = cp_parser_identifier (parser);
15424       /* If we failed, stop.  */
15425       if (identifier == error_mark_node)
15426         break;
15427       /* Declare it as a label.  */
15428       finish_label_decl (identifier);
15429       /* If the next token is a `;', stop.  */
15430       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15431         break;
15432       /* Look for the `,' separating the label declarations.  */
15433       cp_parser_require (parser, CPP_COMMA, "`,'");
15434     }
15435
15436   /* Look for the final `;'.  */
15437   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15438 }
15439
15440 /* Support Functions */
15441
15442 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15443    NAME should have one of the representations used for an
15444    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15445    is returned.  If PARSER->SCOPE is a dependent type, then a
15446    SCOPE_REF is returned.
15447
15448    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15449    returned; the name was already resolved when the TEMPLATE_ID_EXPR
15450    was formed.  Abstractly, such entities should not be passed to this
15451    function, because they do not need to be looked up, but it is
15452    simpler to check for this special case here, rather than at the
15453    call-sites.
15454
15455    In cases not explicitly covered above, this function returns a
15456    DECL, OVERLOAD, or baselink representing the result of the lookup.
15457    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15458    is returned.
15459
15460    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15461    (e.g., "struct") that was used.  In that case bindings that do not
15462    refer to types are ignored.
15463
15464    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15465    ignored.
15466
15467    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15468    are ignored.
15469
15470    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15471    types.
15472
15473    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15474    TREE_LIST of candidates if name-lookup results in an ambiguity, and
15475    NULL_TREE otherwise.  */
15476
15477 static tree
15478 cp_parser_lookup_name (cp_parser *parser, tree name,
15479                        enum tag_types tag_type,
15480                        bool is_template,
15481                        bool is_namespace,
15482                        bool check_dependency,
15483                        tree *ambiguous_decls)
15484 {
15485   int flags = 0;
15486   tree decl;
15487   tree object_type = parser->context->object_type;
15488
15489   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15490     flags |= LOOKUP_COMPLAIN;
15491
15492   /* Assume that the lookup will be unambiguous.  */
15493   if (ambiguous_decls)
15494     *ambiguous_decls = NULL_TREE;
15495
15496   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15497      no longer valid.  Note that if we are parsing tentatively, and
15498      the parse fails, OBJECT_TYPE will be automatically restored.  */
15499   parser->context->object_type = NULL_TREE;
15500
15501   if (name == error_mark_node)
15502     return error_mark_node;
15503
15504   /* A template-id has already been resolved; there is no lookup to
15505      do.  */
15506   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15507     return name;
15508   if (BASELINK_P (name))
15509     {
15510       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15511                   == TEMPLATE_ID_EXPR);
15512       return name;
15513     }
15514
15515   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
15516      it should already have been checked to make sure that the name
15517      used matches the type being destroyed.  */
15518   if (TREE_CODE (name) == BIT_NOT_EXPR)
15519     {
15520       tree type;
15521
15522       /* Figure out to which type this destructor applies.  */
15523       if (parser->scope)
15524         type = parser->scope;
15525       else if (object_type)
15526         type = object_type;
15527       else
15528         type = current_class_type;
15529       /* If that's not a class type, there is no destructor.  */
15530       if (!type || !CLASS_TYPE_P (type))
15531         return error_mark_node;
15532       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15533         lazily_declare_fn (sfk_destructor, type);
15534       if (!CLASSTYPE_DESTRUCTORS (type))
15535           return error_mark_node;
15536       /* If it was a class type, return the destructor.  */
15537       return CLASSTYPE_DESTRUCTORS (type);
15538     }
15539
15540   /* By this point, the NAME should be an ordinary identifier.  If
15541      the id-expression was a qualified name, the qualifying scope is
15542      stored in PARSER->SCOPE at this point.  */
15543   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15544
15545   /* Perform the lookup.  */
15546   if (parser->scope)
15547     {
15548       bool dependent_p;
15549
15550       if (parser->scope == error_mark_node)
15551         return error_mark_node;
15552
15553       /* If the SCOPE is dependent, the lookup must be deferred until
15554          the template is instantiated -- unless we are explicitly
15555          looking up names in uninstantiated templates.  Even then, we
15556          cannot look up the name if the scope is not a class type; it
15557          might, for example, be a template type parameter.  */
15558       dependent_p = (TYPE_P (parser->scope)
15559                      && !(parser->in_declarator_p
15560                           && currently_open_class (parser->scope))
15561                      && dependent_type_p (parser->scope));
15562       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15563            && dependent_p)
15564         {
15565           if (tag_type)
15566             {
15567               tree type;
15568
15569               /* The resolution to Core Issue 180 says that `struct
15570                  A::B' should be considered a type-name, even if `A'
15571                  is dependent.  */
15572               type = make_typename_type (parser->scope, name, tag_type,
15573                                          /*complain=*/tf_error);
15574               decl = TYPE_NAME (type);
15575             }
15576           else if (is_template
15577                    && (cp_parser_next_token_ends_template_argument_p (parser)
15578                        || cp_lexer_next_token_is (parser->lexer,
15579                                                   CPP_CLOSE_PAREN)))
15580             decl = make_unbound_class_template (parser->scope,
15581                                                 name, NULL_TREE,
15582                                                 /*complain=*/tf_error);
15583           else
15584             decl = build_qualified_name (/*type=*/NULL_TREE,
15585                                          parser->scope, name,
15586                                          is_template);
15587         }
15588       else
15589         {
15590           tree pushed_scope = NULL_TREE;
15591
15592           /* If PARSER->SCOPE is a dependent type, then it must be a
15593              class type, and we must not be checking dependencies;
15594              otherwise, we would have processed this lookup above.  So
15595              that PARSER->SCOPE is not considered a dependent base by
15596              lookup_member, we must enter the scope here.  */
15597           if (dependent_p)
15598             pushed_scope = push_scope (parser->scope);
15599           /* If the PARSER->SCOPE is a template specialization, it
15600              may be instantiated during name lookup.  In that case,
15601              errors may be issued.  Even if we rollback the current
15602              tentative parse, those errors are valid.  */
15603           decl = lookup_qualified_name (parser->scope, name,
15604                                         tag_type != none_type,
15605                                         /*complain=*/true);
15606           if (pushed_scope)
15607             pop_scope (pushed_scope);
15608         }
15609       parser->qualifying_scope = parser->scope;
15610       parser->object_scope = NULL_TREE;
15611     }
15612   else if (object_type)
15613     {
15614       tree object_decl = NULL_TREE;
15615       /* Look up the name in the scope of the OBJECT_TYPE, unless the
15616          OBJECT_TYPE is not a class.  */
15617       if (CLASS_TYPE_P (object_type))
15618         /* If the OBJECT_TYPE is a template specialization, it may
15619            be instantiated during name lookup.  In that case, errors
15620            may be issued.  Even if we rollback the current tentative
15621            parse, those errors are valid.  */
15622         object_decl = lookup_member (object_type,
15623                                      name,
15624                                      /*protect=*/0,
15625                                      tag_type != none_type);
15626       /* Look it up in the enclosing context, too.  */
15627       decl = lookup_name_real (name, tag_type != none_type,
15628                                /*nonclass=*/0,
15629                                /*block_p=*/true, is_namespace, flags);
15630       parser->object_scope = object_type;
15631       parser->qualifying_scope = NULL_TREE;
15632       if (object_decl)
15633         decl = object_decl;
15634     }
15635   else
15636     {
15637       decl = lookup_name_real (name, tag_type != none_type,
15638                                /*nonclass=*/0,
15639                                /*block_p=*/true, is_namespace, flags);
15640       parser->qualifying_scope = NULL_TREE;
15641       parser->object_scope = NULL_TREE;
15642     }
15643
15644   /* If the lookup failed, let our caller know.  */
15645   if (!decl || decl == error_mark_node)
15646     return error_mark_node;
15647
15648   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
15649   if (TREE_CODE (decl) == TREE_LIST)
15650     {
15651       if (ambiguous_decls)
15652         *ambiguous_decls = decl;
15653       /* The error message we have to print is too complicated for
15654          cp_parser_error, so we incorporate its actions directly.  */
15655       if (!cp_parser_simulate_error (parser))
15656         {
15657           error ("reference to %qD is ambiguous", name);
15658           print_candidates (decl);
15659         }
15660       return error_mark_node;
15661     }
15662
15663   gcc_assert (DECL_P (decl)
15664               || TREE_CODE (decl) == OVERLOAD
15665               || TREE_CODE (decl) == SCOPE_REF
15666               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15667               || BASELINK_P (decl));
15668
15669   /* If we have resolved the name of a member declaration, check to
15670      see if the declaration is accessible.  When the name resolves to
15671      set of overloaded functions, accessibility is checked when
15672      overload resolution is done.
15673
15674      During an explicit instantiation, access is not checked at all,
15675      as per [temp.explicit].  */
15676   if (DECL_P (decl))
15677     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15678
15679   return decl;
15680 }
15681
15682 /* Like cp_parser_lookup_name, but for use in the typical case where
15683    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15684    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
15685
15686 static tree
15687 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15688 {
15689   return cp_parser_lookup_name (parser, name,
15690                                 none_type,
15691                                 /*is_template=*/false,
15692                                 /*is_namespace=*/false,
15693                                 /*check_dependency=*/true,
15694                                 /*ambiguous_decls=*/NULL);
15695 }
15696
15697 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15698    the current context, return the TYPE_DECL.  If TAG_NAME_P is
15699    true, the DECL indicates the class being defined in a class-head,
15700    or declared in an elaborated-type-specifier.
15701
15702    Otherwise, return DECL.  */
15703
15704 static tree
15705 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15706 {
15707   /* If the TEMPLATE_DECL is being declared as part of a class-head,
15708      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15709
15710        struct A {
15711          template <typename T> struct B;
15712        };
15713
15714        template <typename T> struct A::B {};
15715
15716      Similarly, in an elaborated-type-specifier:
15717
15718        namespace N { struct X{}; }
15719
15720        struct A {
15721          template <typename T> friend struct N::X;
15722        };
15723
15724      However, if the DECL refers to a class type, and we are in
15725      the scope of the class, then the name lookup automatically
15726      finds the TYPE_DECL created by build_self_reference rather
15727      than a TEMPLATE_DECL.  For example, in:
15728
15729        template <class T> struct S {
15730          S s;
15731        };
15732
15733      there is no need to handle such case.  */
15734
15735   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15736     return DECL_TEMPLATE_RESULT (decl);
15737
15738   return decl;
15739 }
15740
15741 /* If too many, or too few, template-parameter lists apply to the
15742    declarator, issue an error message.  Returns TRUE if all went well,
15743    and FALSE otherwise.  */
15744
15745 static bool
15746 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15747                                                 cp_declarator *declarator)
15748 {
15749   unsigned num_templates;
15750
15751   /* We haven't seen any classes that involve template parameters yet.  */
15752   num_templates = 0;
15753
15754   switch (declarator->kind)
15755     {
15756     case cdk_id:
15757       if (declarator->u.id.qualifying_scope)
15758         {
15759           tree scope;
15760           tree member;
15761
15762           scope = declarator->u.id.qualifying_scope;
15763           member = declarator->u.id.unqualified_name;
15764
15765           while (scope && CLASS_TYPE_P (scope))
15766             {
15767               /* You're supposed to have one `template <...>'
15768                  for every template class, but you don't need one
15769                  for a full specialization.  For example:
15770
15771                  template <class T> struct S{};
15772                  template <> struct S<int> { void f(); };
15773                  void S<int>::f () {}
15774
15775                  is correct; there shouldn't be a `template <>' for
15776                  the definition of `S<int>::f'.  */
15777               if (!CLASSTYPE_TEMPLATE_INFO (scope))
15778                 /* If SCOPE does not have template information of any
15779                    kind, then it is not a template, nor is it nested
15780                    within a template.  */
15781                 break;
15782               if (explicit_class_specialization_p (scope))
15783                 break;
15784               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15785                 ++num_templates;
15786
15787               scope = TYPE_CONTEXT (scope);
15788             }
15789         }
15790       else if (TREE_CODE (declarator->u.id.unqualified_name)
15791                == TEMPLATE_ID_EXPR)
15792         /* If the DECLARATOR has the form `X<y>' then it uses one
15793            additional level of template parameters.  */
15794         ++num_templates;
15795
15796       return cp_parser_check_template_parameters (parser,
15797                                                   num_templates);
15798
15799     case cdk_function:
15800     case cdk_array:
15801     case cdk_pointer:
15802     case cdk_reference:
15803     case cdk_ptrmem:
15804       return (cp_parser_check_declarator_template_parameters
15805               (parser, declarator->declarator));
15806
15807     case cdk_error:
15808       return true;
15809
15810     default:
15811       gcc_unreachable ();
15812     }
15813   return false;
15814 }
15815
15816 /* NUM_TEMPLATES were used in the current declaration.  If that is
15817    invalid, return FALSE and issue an error messages.  Otherwise,
15818    return TRUE.  */
15819
15820 static bool
15821 cp_parser_check_template_parameters (cp_parser* parser,
15822                                      unsigned num_templates)
15823 {
15824   /* If there are more template classes than parameter lists, we have
15825      something like:
15826
15827        template <class T> void S<T>::R<T>::f ();  */
15828   if (parser->num_template_parameter_lists < num_templates)
15829     {
15830       error ("too few template-parameter-lists");
15831       return false;
15832     }
15833   /* If there are the same number of template classes and parameter
15834      lists, that's OK.  */
15835   if (parser->num_template_parameter_lists == num_templates)
15836     return true;
15837   /* If there are more, but only one more, then we are referring to a
15838      member template.  That's OK too.  */
15839   if (parser->num_template_parameter_lists == num_templates + 1)
15840       return true;
15841   /* Otherwise, there are too many template parameter lists.  We have
15842      something like:
15843
15844      template <class T> template <class U> void S::f();  */
15845   error ("too many template-parameter-lists");
15846   return false;
15847 }
15848
15849 /* Parse an optional `::' token indicating that the following name is
15850    from the global namespace.  If so, PARSER->SCOPE is set to the
15851    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15852    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15853    Returns the new value of PARSER->SCOPE, if the `::' token is
15854    present, and NULL_TREE otherwise.  */
15855
15856 static tree
15857 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15858 {
15859   cp_token *token;
15860
15861   /* Peek at the next token.  */
15862   token = cp_lexer_peek_token (parser->lexer);
15863   /* If we're looking at a `::' token then we're starting from the
15864      global namespace, not our current location.  */
15865   if (token->type == CPP_SCOPE)
15866     {
15867       /* Consume the `::' token.  */
15868       cp_lexer_consume_token (parser->lexer);
15869       /* Set the SCOPE so that we know where to start the lookup.  */
15870       parser->scope = global_namespace;
15871       parser->qualifying_scope = global_namespace;
15872       parser->object_scope = NULL_TREE;
15873
15874       return parser->scope;
15875     }
15876   else if (!current_scope_valid_p)
15877     {
15878       parser->scope = NULL_TREE;
15879       parser->qualifying_scope = NULL_TREE;
15880       parser->object_scope = NULL_TREE;
15881     }
15882
15883   return NULL_TREE;
15884 }
15885
15886 /* Returns TRUE if the upcoming token sequence is the start of a
15887    constructor declarator.  If FRIEND_P is true, the declarator is
15888    preceded by the `friend' specifier.  */
15889
15890 static bool
15891 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15892 {
15893   bool constructor_p;
15894   tree type_decl = NULL_TREE;
15895   bool nested_name_p;
15896   cp_token *next_token;
15897
15898   /* The common case is that this is not a constructor declarator, so
15899      try to avoid doing lots of work if at all possible.  It's not
15900      valid declare a constructor at function scope.  */
15901   if (parser->in_function_body)
15902     return false;
15903   /* And only certain tokens can begin a constructor declarator.  */
15904   next_token = cp_lexer_peek_token (parser->lexer);
15905   if (next_token->type != CPP_NAME
15906       && next_token->type != CPP_SCOPE
15907       && next_token->type != CPP_NESTED_NAME_SPECIFIER
15908       && next_token->type != CPP_TEMPLATE_ID)
15909     return false;
15910
15911   /* Parse tentatively; we are going to roll back all of the tokens
15912      consumed here.  */
15913   cp_parser_parse_tentatively (parser);
15914   /* Assume that we are looking at a constructor declarator.  */
15915   constructor_p = true;
15916
15917   /* Look for the optional `::' operator.  */
15918   cp_parser_global_scope_opt (parser,
15919                               /*current_scope_valid_p=*/false);
15920   /* Look for the nested-name-specifier.  */
15921   nested_name_p
15922     = (cp_parser_nested_name_specifier_opt (parser,
15923                                             /*typename_keyword_p=*/false,
15924                                             /*check_dependency_p=*/false,
15925                                             /*type_p=*/false,
15926                                             /*is_declaration=*/false)
15927        != NULL_TREE);
15928   /* Outside of a class-specifier, there must be a
15929      nested-name-specifier.  */
15930   if (!nested_name_p &&
15931       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15932        || friend_p))
15933     constructor_p = false;
15934   /* If we still think that this might be a constructor-declarator,
15935      look for a class-name.  */
15936   if (constructor_p)
15937     {
15938       /* If we have:
15939
15940            template <typename T> struct S { S(); };
15941            template <typename T> S<T>::S ();
15942
15943          we must recognize that the nested `S' names a class.
15944          Similarly, for:
15945
15946            template <typename T> S<T>::S<T> ();
15947
15948          we must recognize that the nested `S' names a template.  */
15949       type_decl = cp_parser_class_name (parser,
15950                                         /*typename_keyword_p=*/false,
15951                                         /*template_keyword_p=*/false,
15952                                         none_type,
15953                                         /*check_dependency_p=*/false,
15954                                         /*class_head_p=*/false,
15955                                         /*is_declaration=*/false);
15956       /* If there was no class-name, then this is not a constructor.  */
15957       constructor_p = !cp_parser_error_occurred (parser);
15958     }
15959
15960   /* If we're still considering a constructor, we have to see a `(',
15961      to begin the parameter-declaration-clause, followed by either a
15962      `)', an `...', or a decl-specifier.  We need to check for a
15963      type-specifier to avoid being fooled into thinking that:
15964
15965        S::S (f) (int);
15966
15967      is a constructor.  (It is actually a function named `f' that
15968      takes one parameter (of type `int') and returns a value of type
15969      `S::S'.  */
15970   if (constructor_p
15971       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15972     {
15973       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15974           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15975           /* A parameter declaration begins with a decl-specifier,
15976              which is either the "attribute" keyword, a storage class
15977              specifier, or (usually) a type-specifier.  */
15978           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15979         {
15980           tree type;
15981           tree pushed_scope = NULL_TREE;
15982           unsigned saved_num_template_parameter_lists;
15983
15984           /* Names appearing in the type-specifier should be looked up
15985              in the scope of the class.  */
15986           if (current_class_type)
15987             type = NULL_TREE;
15988           else
15989             {
15990               type = TREE_TYPE (type_decl);
15991               if (TREE_CODE (type) == TYPENAME_TYPE)
15992                 {
15993                   type = resolve_typename_type (type,
15994                                                 /*only_current_p=*/false);
15995                   if (type == error_mark_node)
15996                     {
15997                       cp_parser_abort_tentative_parse (parser);
15998                       return false;
15999                     }
16000                 }
16001               pushed_scope = push_scope (type);
16002             }
16003
16004           /* Inside the constructor parameter list, surrounding
16005              template-parameter-lists do not apply.  */
16006           saved_num_template_parameter_lists
16007             = parser->num_template_parameter_lists;
16008           parser->num_template_parameter_lists = 0;
16009
16010           /* Look for the type-specifier.  */
16011           cp_parser_type_specifier (parser,
16012                                     CP_PARSER_FLAGS_NONE,
16013                                     /*decl_specs=*/NULL,
16014                                     /*is_declarator=*/true,
16015                                     /*declares_class_or_enum=*/NULL,
16016                                     /*is_cv_qualifier=*/NULL);
16017
16018           parser->num_template_parameter_lists
16019             = saved_num_template_parameter_lists;
16020
16021           /* Leave the scope of the class.  */
16022           if (pushed_scope)
16023             pop_scope (pushed_scope);
16024
16025           constructor_p = !cp_parser_error_occurred (parser);
16026         }
16027     }
16028   else
16029     constructor_p = false;
16030   /* We did not really want to consume any tokens.  */
16031   cp_parser_abort_tentative_parse (parser);
16032
16033   return constructor_p;
16034 }
16035
16036 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16037    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
16038    they must be performed once we are in the scope of the function.
16039
16040    Returns the function defined.  */
16041
16042 static tree
16043 cp_parser_function_definition_from_specifiers_and_declarator
16044   (cp_parser* parser,
16045    cp_decl_specifier_seq *decl_specifiers,
16046    tree attributes,
16047    const cp_declarator *declarator)
16048 {
16049   tree fn;
16050   bool success_p;
16051
16052   /* Begin the function-definition.  */
16053   success_p = start_function (decl_specifiers, declarator, attributes);
16054
16055   /* The things we're about to see are not directly qualified by any
16056      template headers we've seen thus far.  */
16057   reset_specialization ();
16058
16059   /* If there were names looked up in the decl-specifier-seq that we
16060      did not check, check them now.  We must wait until we are in the
16061      scope of the function to perform the checks, since the function
16062      might be a friend.  */
16063   perform_deferred_access_checks ();
16064
16065   if (!success_p)
16066     {
16067       /* Skip the entire function.  */
16068       cp_parser_skip_to_end_of_block_or_statement (parser);
16069       fn = error_mark_node;
16070     }
16071   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16072     {
16073       /* Seen already, skip it.  An error message has already been output.  */
16074       cp_parser_skip_to_end_of_block_or_statement (parser);
16075       fn = current_function_decl;
16076       current_function_decl = NULL_TREE;
16077       /* If this is a function from a class, pop the nested class.  */
16078       if (current_class_name)
16079         pop_nested_class ();
16080     }
16081   else
16082     fn = cp_parser_function_definition_after_declarator (parser,
16083                                                          /*inline_p=*/false);
16084
16085   return fn;
16086 }
16087
16088 /* Parse the part of a function-definition that follows the
16089    declarator.  INLINE_P is TRUE iff this function is an inline
16090    function defined with a class-specifier.
16091
16092    Returns the function defined.  */
16093
16094 static tree
16095 cp_parser_function_definition_after_declarator (cp_parser* parser,
16096                                                 bool inline_p)
16097 {
16098   tree fn;
16099   bool ctor_initializer_p = false;
16100   bool saved_in_unbraced_linkage_specification_p;
16101   bool saved_in_function_body;
16102   unsigned saved_num_template_parameter_lists;
16103
16104   saved_in_function_body = parser->in_function_body;
16105   parser->in_function_body = true;
16106   /* If the next token is `return', then the code may be trying to
16107      make use of the "named return value" extension that G++ used to
16108      support.  */
16109   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16110     {
16111       /* Consume the `return' keyword.  */
16112       cp_lexer_consume_token (parser->lexer);
16113       /* Look for the identifier that indicates what value is to be
16114          returned.  */
16115       cp_parser_identifier (parser);
16116       /* Issue an error message.  */
16117       error ("named return values are no longer supported");
16118       /* Skip tokens until we reach the start of the function body.  */
16119       while (true)
16120         {
16121           cp_token *token = cp_lexer_peek_token (parser->lexer);
16122           if (token->type == CPP_OPEN_BRACE
16123               || token->type == CPP_EOF
16124               || token->type == CPP_PRAGMA_EOL)
16125             break;
16126           cp_lexer_consume_token (parser->lexer);
16127         }
16128     }
16129   /* The `extern' in `extern "C" void f () { ... }' does not apply to
16130      anything declared inside `f'.  */
16131   saved_in_unbraced_linkage_specification_p
16132     = parser->in_unbraced_linkage_specification_p;
16133   parser->in_unbraced_linkage_specification_p = false;
16134   /* Inside the function, surrounding template-parameter-lists do not
16135      apply.  */
16136   saved_num_template_parameter_lists
16137     = parser->num_template_parameter_lists;
16138   parser->num_template_parameter_lists = 0;
16139   /* If the next token is `try', then we are looking at a
16140      function-try-block.  */
16141   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16142     ctor_initializer_p = cp_parser_function_try_block (parser);
16143   /* A function-try-block includes the function-body, so we only do
16144      this next part if we're not processing a function-try-block.  */
16145   else
16146     ctor_initializer_p
16147       = cp_parser_ctor_initializer_opt_and_function_body (parser);
16148
16149   /* Finish the function.  */
16150   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16151                         (inline_p ? 2 : 0));
16152   /* Generate code for it, if necessary.  */
16153   expand_or_defer_fn (fn);
16154   /* Restore the saved values.  */
16155   parser->in_unbraced_linkage_specification_p
16156     = saved_in_unbraced_linkage_specification_p;
16157   parser->num_template_parameter_lists
16158     = saved_num_template_parameter_lists;
16159   parser->in_function_body = saved_in_function_body;
16160
16161   return fn;
16162 }
16163
16164 /* Parse a template-declaration, assuming that the `export' (and
16165    `extern') keywords, if present, has already been scanned.  MEMBER_P
16166    is as for cp_parser_template_declaration.  */
16167
16168 static void
16169 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16170 {
16171   tree decl = NULL_TREE;
16172   VEC (deferred_access_check,gc) *checks;
16173   tree parameter_list;
16174   bool friend_p = false;
16175   bool need_lang_pop;
16176
16177   /* Look for the `template' keyword.  */
16178   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16179     return;
16180
16181   /* And the `<'.  */
16182   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16183     return;
16184   if (at_class_scope_p () && current_function_decl)
16185     {
16186       /* 14.5.2.2 [temp.mem]
16187
16188          A local class shall not have member templates.  */
16189       error ("invalid declaration of member template in local class");
16190       cp_parser_skip_to_end_of_block_or_statement (parser);
16191       return;
16192     }
16193   /* [temp]
16194
16195      A template ... shall not have C linkage.  */
16196   if (current_lang_name == lang_name_c)
16197     {
16198       error ("template with C linkage");
16199       /* Give it C++ linkage to avoid confusing other parts of the
16200          front end.  */
16201       push_lang_context (lang_name_cplusplus);
16202       need_lang_pop = true;
16203     }
16204   else
16205     need_lang_pop = false;
16206
16207   /* We cannot perform access checks on the template parameter
16208      declarations until we know what is being declared, just as we
16209      cannot check the decl-specifier list.  */
16210   push_deferring_access_checks (dk_deferred);
16211
16212   /* If the next token is `>', then we have an invalid
16213      specialization.  Rather than complain about an invalid template
16214      parameter, issue an error message here.  */
16215   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16216     {
16217       cp_parser_error (parser, "invalid explicit specialization");
16218       begin_specialization ();
16219       parameter_list = NULL_TREE;
16220     }
16221   else
16222     /* Parse the template parameters.  */
16223     parameter_list = cp_parser_template_parameter_list (parser);
16224
16225   /* Get the deferred access checks from the parameter list.  These
16226      will be checked once we know what is being declared, as for a
16227      member template the checks must be performed in the scope of the
16228      class containing the member.  */
16229   checks = get_deferred_access_checks ();
16230
16231   /* Look for the `>'.  */
16232   cp_parser_skip_to_end_of_template_parameter_list (parser);
16233   /* We just processed one more parameter list.  */
16234   ++parser->num_template_parameter_lists;
16235   /* If the next token is `template', there are more template
16236      parameters.  */
16237   if (cp_lexer_next_token_is_keyword (parser->lexer,
16238                                       RID_TEMPLATE))
16239     cp_parser_template_declaration_after_export (parser, member_p);
16240   else
16241     {
16242       /* There are no access checks when parsing a template, as we do not
16243          know if a specialization will be a friend.  */
16244       push_deferring_access_checks (dk_no_check);
16245       decl = cp_parser_single_declaration (parser,
16246                                            checks,
16247                                            member_p,
16248                                            &friend_p);
16249       pop_deferring_access_checks ();
16250
16251       /* If this is a member template declaration, let the front
16252          end know.  */
16253       if (member_p && !friend_p && decl)
16254         {
16255           if (TREE_CODE (decl) == TYPE_DECL)
16256             cp_parser_check_access_in_redeclaration (decl);
16257
16258           decl = finish_member_template_decl (decl);
16259         }
16260       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16261         make_friend_class (current_class_type, TREE_TYPE (decl),
16262                            /*complain=*/true);
16263     }
16264   /* We are done with the current parameter list.  */
16265   --parser->num_template_parameter_lists;
16266
16267   pop_deferring_access_checks ();
16268
16269   /* Finish up.  */
16270   finish_template_decl (parameter_list);
16271
16272   /* Register member declarations.  */
16273   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16274     finish_member_declaration (decl);
16275   /* For the erroneous case of a template with C linkage, we pushed an
16276      implicit C++ linkage scope; exit that scope now.  */
16277   if (need_lang_pop)
16278     pop_lang_context ();
16279   /* If DECL is a function template, we must return to parse it later.
16280      (Even though there is no definition, there might be default
16281      arguments that need handling.)  */
16282   if (member_p && decl
16283       && (TREE_CODE (decl) == FUNCTION_DECL
16284           || DECL_FUNCTION_TEMPLATE_P (decl)))
16285     TREE_VALUE (parser->unparsed_functions_queues)
16286       = tree_cons (NULL_TREE, decl,
16287                    TREE_VALUE (parser->unparsed_functions_queues));
16288 }
16289
16290 /* Perform the deferred access checks from a template-parameter-list.
16291    CHECKS is a TREE_LIST of access checks, as returned by
16292    get_deferred_access_checks.  */
16293
16294 static void
16295 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16296 {
16297   ++processing_template_parmlist;
16298   perform_access_checks (checks);
16299   --processing_template_parmlist;
16300 }
16301
16302 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16303    `function-definition' sequence.  MEMBER_P is true, this declaration
16304    appears in a class scope.
16305
16306    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
16307    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
16308
16309 static tree
16310 cp_parser_single_declaration (cp_parser* parser,
16311                               VEC (deferred_access_check,gc)* checks,
16312                               bool member_p,
16313                               bool* friend_p)
16314 {
16315   int declares_class_or_enum;
16316   tree decl = NULL_TREE;
16317   cp_decl_specifier_seq decl_specifiers;
16318   bool function_definition_p = false;
16319
16320   /* This function is only used when processing a template
16321      declaration.  */
16322   gcc_assert (innermost_scope_kind () == sk_template_parms
16323               || innermost_scope_kind () == sk_template_spec);
16324
16325   /* Defer access checks until we know what is being declared.  */
16326   push_deferring_access_checks (dk_deferred);
16327
16328   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16329      alternative.  */
16330   cp_parser_decl_specifier_seq (parser,
16331                                 CP_PARSER_FLAGS_OPTIONAL,
16332                                 &decl_specifiers,
16333                                 &declares_class_or_enum);
16334   if (friend_p)
16335     *friend_p = cp_parser_friend_p (&decl_specifiers);
16336
16337   /* There are no template typedefs.  */
16338   if (decl_specifiers.specs[(int) ds_typedef])
16339     {
16340       error ("template declaration of %qs", "typedef");
16341       decl = error_mark_node;
16342     }
16343
16344   /* Gather up the access checks that occurred the
16345      decl-specifier-seq.  */
16346   stop_deferring_access_checks ();
16347
16348   /* Check for the declaration of a template class.  */
16349   if (declares_class_or_enum)
16350     {
16351       if (cp_parser_declares_only_class_p (parser))
16352         {
16353           decl = shadow_tag (&decl_specifiers);
16354
16355           /* In this case:
16356
16357                struct C {
16358                  friend template <typename T> struct A<T>::B;
16359                };
16360
16361              A<T>::B will be represented by a TYPENAME_TYPE, and
16362              therefore not recognized by shadow_tag.  */
16363           if (friend_p && *friend_p
16364               && !decl
16365               && decl_specifiers.type
16366               && TYPE_P (decl_specifiers.type))
16367             decl = decl_specifiers.type;
16368
16369           if (decl && decl != error_mark_node)
16370             decl = TYPE_NAME (decl);
16371           else
16372             decl = error_mark_node;
16373
16374           /* Perform access checks for template parameters.  */
16375           cp_parser_perform_template_parameter_access_checks (checks);
16376         }
16377     }
16378   /* If it's not a template class, try for a template function.  If
16379      the next token is a `;', then this declaration does not declare
16380      anything.  But, if there were errors in the decl-specifiers, then
16381      the error might well have come from an attempted class-specifier.
16382      In that case, there's no need to warn about a missing declarator.  */
16383   if (!decl
16384       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16385           || decl_specifiers.type != error_mark_node))
16386     decl = cp_parser_init_declarator (parser,
16387                                       &decl_specifiers,
16388                                       checks,
16389                                       /*function_definition_allowed_p=*/true,
16390                                       member_p,
16391                                       declares_class_or_enum,
16392                                       &function_definition_p);
16393
16394   pop_deferring_access_checks ();
16395
16396   /* Clear any current qualification; whatever comes next is the start
16397      of something new.  */
16398   parser->scope = NULL_TREE;
16399   parser->qualifying_scope = NULL_TREE;
16400   parser->object_scope = NULL_TREE;
16401   /* Look for a trailing `;' after the declaration.  */
16402   if (!function_definition_p
16403       && (decl == error_mark_node
16404           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16405     cp_parser_skip_to_end_of_block_or_statement (parser);
16406
16407   return decl;
16408 }
16409
16410 /* Parse a cast-expression that is not the operand of a unary "&".  */
16411
16412 static tree
16413 cp_parser_simple_cast_expression (cp_parser *parser)
16414 {
16415   return cp_parser_cast_expression (parser, /*address_p=*/false,
16416                                     /*cast_p=*/false);
16417 }
16418
16419 /* Parse a functional cast to TYPE.  Returns an expression
16420    representing the cast.  */
16421
16422 static tree
16423 cp_parser_functional_cast (cp_parser* parser, tree type)
16424 {
16425   tree expression_list;
16426   tree cast;
16427
16428   expression_list
16429     = cp_parser_parenthesized_expression_list (parser, false,
16430                                                /*cast_p=*/true,
16431                                                /*allow_expansion_p=*/true,
16432                                                /*non_constant_p=*/NULL);
16433
16434   cast = build_functional_cast (type, expression_list);
16435   /* [expr.const]/1: In an integral constant expression "only type
16436      conversions to integral or enumeration type can be used".  */
16437   if (TREE_CODE (type) == TYPE_DECL)
16438     type = TREE_TYPE (type);
16439   if (cast != error_mark_node
16440       && !cast_valid_in_integral_constant_expression_p (type)
16441       && (cp_parser_non_integral_constant_expression
16442           (parser, "a call to a constructor")))
16443     return error_mark_node;
16444   return cast;
16445 }
16446
16447 /* Save the tokens that make up the body of a member function defined
16448    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
16449    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
16450    specifiers applied to the declaration.  Returns the FUNCTION_DECL
16451    for the member function.  */
16452
16453 static tree
16454 cp_parser_save_member_function_body (cp_parser* parser,
16455                                      cp_decl_specifier_seq *decl_specifiers,
16456                                      cp_declarator *declarator,
16457                                      tree attributes)
16458 {
16459   cp_token *first;
16460   cp_token *last;
16461   tree fn;
16462
16463   /* Create the function-declaration.  */
16464   fn = start_method (decl_specifiers, declarator, attributes);
16465   /* If something went badly wrong, bail out now.  */
16466   if (fn == error_mark_node)
16467     {
16468       /* If there's a function-body, skip it.  */
16469       if (cp_parser_token_starts_function_definition_p
16470           (cp_lexer_peek_token (parser->lexer)))
16471         cp_parser_skip_to_end_of_block_or_statement (parser);
16472       return error_mark_node;
16473     }
16474
16475   /* Remember it, if there default args to post process.  */
16476   cp_parser_save_default_args (parser, fn);
16477
16478   /* Save away the tokens that make up the body of the
16479      function.  */
16480   first = parser->lexer->next_token;
16481   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16482   /* Handle function try blocks.  */
16483   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16484     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16485   last = parser->lexer->next_token;
16486
16487   /* Save away the inline definition; we will process it when the
16488      class is complete.  */
16489   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16490   DECL_PENDING_INLINE_P (fn) = 1;
16491
16492   /* We need to know that this was defined in the class, so that
16493      friend templates are handled correctly.  */
16494   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16495
16496   /* We're done with the inline definition.  */
16497   finish_method (fn);
16498
16499   /* Add FN to the queue of functions to be parsed later.  */
16500   TREE_VALUE (parser->unparsed_functions_queues)
16501     = tree_cons (NULL_TREE, fn,
16502                  TREE_VALUE (parser->unparsed_functions_queues));
16503
16504   return fn;
16505 }
16506
16507 /* Parse a template-argument-list, as well as the trailing ">" (but
16508    not the opening ">").  See cp_parser_template_argument_list for the
16509    return value.  */
16510
16511 static tree
16512 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16513 {
16514   tree arguments;
16515   tree saved_scope;
16516   tree saved_qualifying_scope;
16517   tree saved_object_scope;
16518   bool saved_greater_than_is_operator_p;
16519   bool saved_skip_evaluation;
16520
16521   /* [temp.names]
16522
16523      When parsing a template-id, the first non-nested `>' is taken as
16524      the end of the template-argument-list rather than a greater-than
16525      operator.  */
16526   saved_greater_than_is_operator_p
16527     = parser->greater_than_is_operator_p;
16528   parser->greater_than_is_operator_p = false;
16529   /* Parsing the argument list may modify SCOPE, so we save it
16530      here.  */
16531   saved_scope = parser->scope;
16532   saved_qualifying_scope = parser->qualifying_scope;
16533   saved_object_scope = parser->object_scope;
16534   /* We need to evaluate the template arguments, even though this
16535      template-id may be nested within a "sizeof".  */
16536   saved_skip_evaluation = skip_evaluation;
16537   skip_evaluation = false;
16538   /* Parse the template-argument-list itself.  */
16539   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16540     arguments = NULL_TREE;
16541   else
16542     arguments = cp_parser_template_argument_list (parser);
16543   /* Look for the `>' that ends the template-argument-list. If we find
16544      a '>>' instead, it's probably just a typo.  */
16545   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16546     {
16547       if (!saved_greater_than_is_operator_p)
16548         {
16549           /* If we're in a nested template argument list, the '>>' has
16550             to be a typo for '> >'. We emit the error message, but we
16551             continue parsing and we push a '>' as next token, so that
16552             the argument list will be parsed correctly.  Note that the
16553             global source location is still on the token before the
16554             '>>', so we need to say explicitly where we want it.  */
16555           cp_token *token = cp_lexer_peek_token (parser->lexer);
16556           error ("%H%<>>%> should be %<> >%> "
16557                  "within a nested template argument list",
16558                  &token->location);
16559
16560           /* ??? Proper recovery should terminate two levels of
16561              template argument list here.  */
16562           token->type = CPP_GREATER;
16563         }
16564       else
16565         {
16566           /* If this is not a nested template argument list, the '>>'
16567             is a typo for '>'. Emit an error message and continue.
16568             Same deal about the token location, but here we can get it
16569             right by consuming the '>>' before issuing the diagnostic.  */
16570           cp_lexer_consume_token (parser->lexer);
16571           error ("spurious %<>>%>, use %<>%> to terminate "
16572                  "a template argument list");
16573         }
16574     }
16575   else
16576     cp_parser_skip_to_end_of_template_parameter_list (parser);
16577   /* The `>' token might be a greater-than operator again now.  */
16578   parser->greater_than_is_operator_p
16579     = saved_greater_than_is_operator_p;
16580   /* Restore the SAVED_SCOPE.  */
16581   parser->scope = saved_scope;
16582   parser->qualifying_scope = saved_qualifying_scope;
16583   parser->object_scope = saved_object_scope;
16584   skip_evaluation = saved_skip_evaluation;
16585
16586   return arguments;
16587 }
16588
16589 /* MEMBER_FUNCTION is a member function, or a friend.  If default
16590    arguments, or the body of the function have not yet been parsed,
16591    parse them now.  */
16592
16593 static void
16594 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16595 {
16596   /* If this member is a template, get the underlying
16597      FUNCTION_DECL.  */
16598   if (DECL_FUNCTION_TEMPLATE_P (member_function))
16599     member_function = DECL_TEMPLATE_RESULT (member_function);
16600
16601   /* There should not be any class definitions in progress at this
16602      point; the bodies of members are only parsed outside of all class
16603      definitions.  */
16604   gcc_assert (parser->num_classes_being_defined == 0);
16605   /* While we're parsing the member functions we might encounter more
16606      classes.  We want to handle them right away, but we don't want
16607      them getting mixed up with functions that are currently in the
16608      queue.  */
16609   parser->unparsed_functions_queues
16610     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16611
16612   /* Make sure that any template parameters are in scope.  */
16613   maybe_begin_member_template_processing (member_function);
16614
16615   /* If the body of the function has not yet been parsed, parse it
16616      now.  */
16617   if (DECL_PENDING_INLINE_P (member_function))
16618     {
16619       tree function_scope;
16620       cp_token_cache *tokens;
16621
16622       /* The function is no longer pending; we are processing it.  */
16623       tokens = DECL_PENDING_INLINE_INFO (member_function);
16624       DECL_PENDING_INLINE_INFO (member_function) = NULL;
16625       DECL_PENDING_INLINE_P (member_function) = 0;
16626
16627       /* If this is a local class, enter the scope of the containing
16628          function.  */
16629       function_scope = current_function_decl;
16630       if (function_scope)
16631         push_function_context_to (function_scope);
16632
16633
16634       /* Push the body of the function onto the lexer stack.  */
16635       cp_parser_push_lexer_for_tokens (parser, tokens);
16636
16637       /* Let the front end know that we going to be defining this
16638          function.  */
16639       start_preparsed_function (member_function, NULL_TREE,
16640                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
16641
16642       /* Don't do access checking if it is a templated function.  */
16643       if (processing_template_decl)
16644         push_deferring_access_checks (dk_no_check);
16645
16646       /* Now, parse the body of the function.  */
16647       cp_parser_function_definition_after_declarator (parser,
16648                                                       /*inline_p=*/true);
16649
16650       if (processing_template_decl)
16651         pop_deferring_access_checks ();
16652
16653       /* Leave the scope of the containing function.  */
16654       if (function_scope)
16655         pop_function_context_from (function_scope);
16656       cp_parser_pop_lexer (parser);
16657     }
16658
16659   /* Remove any template parameters from the symbol table.  */
16660   maybe_end_member_template_processing ();
16661
16662   /* Restore the queue.  */
16663   parser->unparsed_functions_queues
16664     = TREE_CHAIN (parser->unparsed_functions_queues);
16665 }
16666
16667 /* If DECL contains any default args, remember it on the unparsed
16668    functions queue.  */
16669
16670 static void
16671 cp_parser_save_default_args (cp_parser* parser, tree decl)
16672 {
16673   tree probe;
16674
16675   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16676        probe;
16677        probe = TREE_CHAIN (probe))
16678     if (TREE_PURPOSE (probe))
16679       {
16680         TREE_PURPOSE (parser->unparsed_functions_queues)
16681           = tree_cons (current_class_type, decl,
16682                        TREE_PURPOSE (parser->unparsed_functions_queues));
16683         break;
16684       }
16685 }
16686
16687 /* FN is a FUNCTION_DECL which may contains a parameter with an
16688    unparsed DEFAULT_ARG.  Parse the default args now.  This function
16689    assumes that the current scope is the scope in which the default
16690    argument should be processed.  */
16691
16692 static void
16693 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16694 {
16695   bool saved_local_variables_forbidden_p;
16696   tree parm;
16697
16698   /* While we're parsing the default args, we might (due to the
16699      statement expression extension) encounter more classes.  We want
16700      to handle them right away, but we don't want them getting mixed
16701      up with default args that are currently in the queue.  */
16702   parser->unparsed_functions_queues
16703     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16704
16705   /* Local variable names (and the `this' keyword) may not appear
16706      in a default argument.  */
16707   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16708   parser->local_variables_forbidden_p = true;
16709
16710   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16711        parm;
16712        parm = TREE_CHAIN (parm))
16713     {
16714       cp_token_cache *tokens;
16715       tree default_arg = TREE_PURPOSE (parm);
16716       tree parsed_arg;
16717       VEC(tree,gc) *insts;
16718       tree copy;
16719       unsigned ix;
16720
16721       if (!default_arg)
16722         continue;
16723
16724       if (TREE_CODE (default_arg) != DEFAULT_ARG)
16725         /* This can happen for a friend declaration for a function
16726            already declared with default arguments.  */
16727         continue;
16728
16729        /* Push the saved tokens for the default argument onto the parser's
16730           lexer stack.  */
16731       tokens = DEFARG_TOKENS (default_arg);
16732       cp_parser_push_lexer_for_tokens (parser, tokens);
16733
16734       /* Parse the assignment-expression.  */
16735       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16736
16737       if (!processing_template_decl)
16738         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16739
16740       TREE_PURPOSE (parm) = parsed_arg;
16741
16742       /* Update any instantiations we've already created.  */
16743       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16744            VEC_iterate (tree, insts, ix, copy); ix++)
16745         TREE_PURPOSE (copy) = parsed_arg;
16746
16747       /* If the token stream has not been completely used up, then
16748          there was extra junk after the end of the default
16749          argument.  */
16750       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16751         cp_parser_error (parser, "expected %<,%>");
16752
16753       /* Revert to the main lexer.  */
16754       cp_parser_pop_lexer (parser);
16755     }
16756
16757   /* Make sure no default arg is missing.  */
16758   check_default_args (fn);
16759
16760   /* Restore the state of local_variables_forbidden_p.  */
16761   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16762
16763   /* Restore the queue.  */
16764   parser->unparsed_functions_queues
16765     = TREE_CHAIN (parser->unparsed_functions_queues);
16766 }
16767
16768 /* Parse the operand of `sizeof' (or a similar operator).  Returns
16769    either a TYPE or an expression, depending on the form of the
16770    input.  The KEYWORD indicates which kind of expression we have
16771    encountered.  */
16772
16773 static tree
16774 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16775 {
16776   static const char *format;
16777   tree expr = NULL_TREE;
16778   const char *saved_message;
16779   bool saved_integral_constant_expression_p;
16780   bool saved_non_integral_constant_expression_p;
16781   bool pack_expansion_p = false;
16782
16783   /* Initialize FORMAT the first time we get here.  */
16784   if (!format)
16785     format = "types may not be defined in '%s' expressions";
16786
16787   /* Types cannot be defined in a `sizeof' expression.  Save away the
16788      old message.  */
16789   saved_message = parser->type_definition_forbidden_message;
16790   /* And create the new one.  */
16791   parser->type_definition_forbidden_message
16792     = XNEWVEC (const char, strlen (format)
16793                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16794                + 1 /* `\0' */);
16795   sprintf ((char *) parser->type_definition_forbidden_message,
16796            format, IDENTIFIER_POINTER (ridpointers[keyword]));
16797
16798   /* The restrictions on constant-expressions do not apply inside
16799      sizeof expressions.  */
16800   saved_integral_constant_expression_p
16801     = parser->integral_constant_expression_p;
16802   saved_non_integral_constant_expression_p
16803     = parser->non_integral_constant_expression_p;
16804   parser->integral_constant_expression_p = false;
16805
16806   /* If it's a `...', then we are computing the length of a parameter
16807      pack.  */
16808   if (keyword == RID_SIZEOF
16809       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16810     {
16811       /* Consume the `...'.  */
16812       cp_lexer_consume_token (parser->lexer);
16813       maybe_warn_variadic_templates ();
16814
16815       /* Note that this is an expansion.  */
16816       pack_expansion_p = true;
16817     }
16818
16819   /* Do not actually evaluate the expression.  */
16820   ++skip_evaluation;
16821   /* If it's a `(', then we might be looking at the type-id
16822      construction.  */
16823   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16824     {
16825       tree type;
16826       bool saved_in_type_id_in_expr_p;
16827
16828       /* We can't be sure yet whether we're looking at a type-id or an
16829          expression.  */
16830       cp_parser_parse_tentatively (parser);
16831       /* Consume the `('.  */
16832       cp_lexer_consume_token (parser->lexer);
16833       /* Parse the type-id.  */
16834       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16835       parser->in_type_id_in_expr_p = true;
16836       type = cp_parser_type_id (parser);
16837       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16838       /* Now, look for the trailing `)'.  */
16839       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16840       /* If all went well, then we're done.  */
16841       if (cp_parser_parse_definitely (parser))
16842         {
16843           cp_decl_specifier_seq decl_specs;
16844
16845           /* Build a trivial decl-specifier-seq.  */
16846           clear_decl_specs (&decl_specs);
16847           decl_specs.type = type;
16848
16849           /* Call grokdeclarator to figure out what type this is.  */
16850           expr = grokdeclarator (NULL,
16851                                  &decl_specs,
16852                                  TYPENAME,
16853                                  /*initialized=*/0,
16854                                  /*attrlist=*/NULL);
16855         }
16856     }
16857
16858   /* If the type-id production did not work out, then we must be
16859      looking at the unary-expression production.  */
16860   if (!expr)
16861     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16862                                        /*cast_p=*/false);
16863
16864   if (pack_expansion_p)
16865     /* Build a pack expansion. */
16866     expr = make_pack_expansion (expr);
16867
16868   /* Go back to evaluating expressions.  */
16869   --skip_evaluation;
16870
16871   /* Free the message we created.  */
16872   free ((char *) parser->type_definition_forbidden_message);
16873   /* And restore the old one.  */
16874   parser->type_definition_forbidden_message = saved_message;
16875   parser->integral_constant_expression_p
16876     = saved_integral_constant_expression_p;
16877   parser->non_integral_constant_expression_p
16878     = saved_non_integral_constant_expression_p;
16879
16880   return expr;
16881 }
16882
16883 /* If the current declaration has no declarator, return true.  */
16884
16885 static bool
16886 cp_parser_declares_only_class_p (cp_parser *parser)
16887 {
16888   /* If the next token is a `;' or a `,' then there is no
16889      declarator.  */
16890   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16891           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16892 }
16893
16894 /* Update the DECL_SPECS to reflect the storage class indicated by
16895    KEYWORD.  */
16896
16897 static void
16898 cp_parser_set_storage_class (cp_parser *parser,
16899                              cp_decl_specifier_seq *decl_specs,
16900                              enum rid keyword)
16901 {
16902   cp_storage_class storage_class;
16903
16904   if (parser->in_unbraced_linkage_specification_p)
16905     {
16906       error ("invalid use of %qD in linkage specification",
16907              ridpointers[keyword]);
16908       return;
16909     }
16910   else if (decl_specs->storage_class != sc_none)
16911     {
16912       decl_specs->conflicting_specifiers_p = true;
16913       return;
16914     }
16915
16916   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16917       && decl_specs->specs[(int) ds_thread])
16918     {
16919       error ("%<__thread%> before %qD", ridpointers[keyword]);
16920       decl_specs->specs[(int) ds_thread] = 0;
16921     }
16922
16923   switch (keyword)
16924     {
16925     case RID_AUTO:
16926       storage_class = sc_auto;
16927       break;
16928     case RID_REGISTER:
16929       storage_class = sc_register;
16930       break;
16931     case RID_STATIC:
16932       storage_class = sc_static;
16933       break;
16934     case RID_EXTERN:
16935       storage_class = sc_extern;
16936       break;
16937     case RID_MUTABLE:
16938       storage_class = sc_mutable;
16939       break;
16940     default:
16941       gcc_unreachable ();
16942     }
16943   decl_specs->storage_class = storage_class;
16944
16945   /* A storage class specifier cannot be applied alongside a typedef 
16946      specifier. If there is a typedef specifier present then set 
16947      conflicting_specifiers_p which will trigger an error later
16948      on in grokdeclarator. */
16949   if (decl_specs->specs[(int)ds_typedef])
16950     decl_specs->conflicting_specifiers_p = true;
16951 }
16952
16953 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16954    is true, the type is a user-defined type; otherwise it is a
16955    built-in type specified by a keyword.  */
16956
16957 static void
16958 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16959                               tree type_spec,
16960                               bool user_defined_p)
16961 {
16962   decl_specs->any_specifiers_p = true;
16963
16964   /* If the user tries to redeclare bool or wchar_t (with, for
16965      example, in "typedef int wchar_t;") we remember that this is what
16966      happened.  In system headers, we ignore these declarations so
16967      that G++ can work with system headers that are not C++-safe.  */
16968   if (decl_specs->specs[(int) ds_typedef]
16969       && !user_defined_p
16970       && (type_spec == boolean_type_node
16971           || type_spec == wchar_type_node)
16972       && (decl_specs->type
16973           || decl_specs->specs[(int) ds_long]
16974           || decl_specs->specs[(int) ds_short]
16975           || decl_specs->specs[(int) ds_unsigned]
16976           || decl_specs->specs[(int) ds_signed]))
16977     {
16978       decl_specs->redefined_builtin_type = type_spec;
16979       if (!decl_specs->type)
16980         {
16981           decl_specs->type = type_spec;
16982           decl_specs->user_defined_type_p = false;
16983         }
16984     }
16985   else if (decl_specs->type)
16986     decl_specs->multiple_types_p = true;
16987   else
16988     {
16989       decl_specs->type = type_spec;
16990       decl_specs->user_defined_type_p = user_defined_p;
16991       decl_specs->redefined_builtin_type = NULL_TREE;
16992     }
16993 }
16994
16995 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16996    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16997
16998 static bool
16999 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17000 {
17001   return decl_specifiers->specs[(int) ds_friend] != 0;
17002 }
17003
17004 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
17005    issue an error message indicating that TOKEN_DESC was expected.
17006
17007    Returns the token consumed, if the token had the appropriate type.
17008    Otherwise, returns NULL.  */
17009
17010 static cp_token *
17011 cp_parser_require (cp_parser* parser,
17012                    enum cpp_ttype type,
17013                    const char* token_desc)
17014 {
17015   if (cp_lexer_next_token_is (parser->lexer, type))
17016     return cp_lexer_consume_token (parser->lexer);
17017   else
17018     {
17019       /* Output the MESSAGE -- unless we're parsing tentatively.  */
17020       if (!cp_parser_simulate_error (parser))
17021         {
17022           char *message = concat ("expected ", token_desc, NULL);
17023           cp_parser_error (parser, message);
17024           free (message);
17025         }
17026       return NULL;
17027     }
17028 }
17029
17030 /* An error message is produced if the next token is not '>'.
17031    All further tokens are skipped until the desired token is
17032    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
17033
17034 static void
17035 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17036 {
17037   /* Current level of '< ... >'.  */
17038   unsigned level = 0;
17039   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
17040   unsigned nesting_depth = 0;
17041
17042   /* Are we ready, yet?  If not, issue error message.  */
17043   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17044     return;
17045
17046   /* Skip tokens until the desired token is found.  */
17047   while (true)
17048     {
17049       /* Peek at the next token.  */
17050       switch (cp_lexer_peek_token (parser->lexer)->type)
17051         {
17052         case CPP_LESS:
17053           if (!nesting_depth)
17054             ++level;
17055           break;
17056
17057         case CPP_GREATER:
17058           if (!nesting_depth && level-- == 0)
17059             {
17060               /* We've reached the token we want, consume it and stop.  */
17061               cp_lexer_consume_token (parser->lexer);
17062               return;
17063             }
17064           break;
17065
17066         case CPP_OPEN_PAREN:
17067         case CPP_OPEN_SQUARE:
17068           ++nesting_depth;
17069           break;
17070
17071         case CPP_CLOSE_PAREN:
17072         case CPP_CLOSE_SQUARE:
17073           if (nesting_depth-- == 0)
17074             return;
17075           break;
17076
17077         case CPP_EOF:
17078         case CPP_PRAGMA_EOL:
17079         case CPP_SEMICOLON:
17080         case CPP_OPEN_BRACE:
17081         case CPP_CLOSE_BRACE:
17082           /* The '>' was probably forgotten, don't look further.  */
17083           return;
17084
17085         default:
17086           break;
17087         }
17088
17089       /* Consume this token.  */
17090       cp_lexer_consume_token (parser->lexer);
17091     }
17092 }
17093
17094 /* If the next token is the indicated keyword, consume it.  Otherwise,
17095    issue an error message indicating that TOKEN_DESC was expected.
17096
17097    Returns the token consumed, if the token had the appropriate type.
17098    Otherwise, returns NULL.  */
17099
17100 static cp_token *
17101 cp_parser_require_keyword (cp_parser* parser,
17102                            enum rid keyword,
17103                            const char* token_desc)
17104 {
17105   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17106
17107   if (token && token->keyword != keyword)
17108     {
17109       dyn_string_t error_msg;
17110
17111       /* Format the error message.  */
17112       error_msg = dyn_string_new (0);
17113       dyn_string_append_cstr (error_msg, "expected ");
17114       dyn_string_append_cstr (error_msg, token_desc);
17115       cp_parser_error (parser, error_msg->s);
17116       dyn_string_delete (error_msg);
17117       return NULL;
17118     }
17119
17120   return token;
17121 }
17122
17123 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17124    function-definition.  */
17125
17126 static bool
17127 cp_parser_token_starts_function_definition_p (cp_token* token)
17128 {
17129   return (/* An ordinary function-body begins with an `{'.  */
17130           token->type == CPP_OPEN_BRACE
17131           /* A ctor-initializer begins with a `:'.  */
17132           || token->type == CPP_COLON
17133           /* A function-try-block begins with `try'.  */
17134           || token->keyword == RID_TRY
17135           /* The named return value extension begins with `return'.  */
17136           || token->keyword == RID_RETURN);
17137 }
17138
17139 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17140    definition.  */
17141
17142 static bool
17143 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17144 {
17145   cp_token *token;
17146
17147   token = cp_lexer_peek_token (parser->lexer);
17148   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17149 }
17150
17151 /* Returns TRUE iff the next token is the "," or ">" ending a
17152    template-argument.  */
17153
17154 static bool
17155 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17156 {
17157   cp_token *token;
17158
17159   token = cp_lexer_peek_token (parser->lexer);
17160   return (token->type == CPP_COMMA 
17161           || token->type == CPP_GREATER
17162           || token->type == CPP_ELLIPSIS);
17163 }
17164
17165 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17166    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
17167
17168 static bool
17169 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17170                                                      size_t n)
17171 {
17172   cp_token *token;
17173
17174   token = cp_lexer_peek_nth_token (parser->lexer, n);
17175   if (token->type == CPP_LESS)
17176     return true;
17177   /* Check for the sequence `<::' in the original code. It would be lexed as
17178      `[:', where `[' is a digraph, and there is no whitespace before
17179      `:'.  */
17180   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17181     {
17182       cp_token *token2;
17183       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17184       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17185         return true;
17186     }
17187   return false;
17188 }
17189
17190 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17191    or none_type otherwise.  */
17192
17193 static enum tag_types
17194 cp_parser_token_is_class_key (cp_token* token)
17195 {
17196   switch (token->keyword)
17197     {
17198     case RID_CLASS:
17199       return class_type;
17200     case RID_STRUCT:
17201       return record_type;
17202     case RID_UNION:
17203       return union_type;
17204
17205     default:
17206       return none_type;
17207     }
17208 }
17209
17210 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
17211
17212 static void
17213 cp_parser_check_class_key (enum tag_types class_key, tree type)
17214 {
17215   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17216     pedwarn ("%qs tag used in naming %q#T",
17217             class_key == union_type ? "union"
17218              : class_key == record_type ? "struct" : "class",
17219              type);
17220 }
17221
17222 /* Issue an error message if DECL is redeclared with different
17223    access than its original declaration [class.access.spec/3].
17224    This applies to nested classes and nested class templates.
17225    [class.mem/1].  */
17226
17227 static void
17228 cp_parser_check_access_in_redeclaration (tree decl)
17229 {
17230   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17231     return;
17232
17233   if ((TREE_PRIVATE (decl)
17234        != (current_access_specifier == access_private_node))
17235       || (TREE_PROTECTED (decl)
17236           != (current_access_specifier == access_protected_node)))
17237     error ("%qD redeclared with different access", decl);
17238 }
17239
17240 /* Look for the `template' keyword, as a syntactic disambiguator.
17241    Return TRUE iff it is present, in which case it will be
17242    consumed.  */
17243
17244 static bool
17245 cp_parser_optional_template_keyword (cp_parser *parser)
17246 {
17247   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17248     {
17249       /* The `template' keyword can only be used within templates;
17250          outside templates the parser can always figure out what is a
17251          template and what is not.  */
17252       if (!processing_template_decl)
17253         {
17254           error ("%<template%> (as a disambiguator) is only allowed "
17255                  "within templates");
17256           /* If this part of the token stream is rescanned, the same
17257              error message would be generated.  So, we purge the token
17258              from the stream.  */
17259           cp_lexer_purge_token (parser->lexer);
17260           return false;
17261         }
17262       else
17263         {
17264           /* Consume the `template' keyword.  */
17265           cp_lexer_consume_token (parser->lexer);
17266           return true;
17267         }
17268     }
17269
17270   return false;
17271 }
17272
17273 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
17274    set PARSER->SCOPE, and perform other related actions.  */
17275
17276 static void
17277 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17278 {
17279   int i;
17280   struct tree_check *check_value;
17281   deferred_access_check *chk;
17282   VEC (deferred_access_check,gc) *checks;
17283
17284   /* Get the stored value.  */
17285   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17286   /* Perform any access checks that were deferred.  */
17287   checks = check_value->checks;
17288   if (checks)
17289     {
17290       for (i = 0 ;
17291            VEC_iterate (deferred_access_check, checks, i, chk) ;
17292            ++i)
17293         {
17294           perform_or_defer_access_check (chk->binfo,
17295                                          chk->decl,
17296                                          chk->diag_decl);
17297         }
17298     }
17299   /* Set the scope from the stored value.  */
17300   parser->scope = check_value->value;
17301   parser->qualifying_scope = check_value->qualifying_scope;
17302   parser->object_scope = NULL_TREE;
17303 }
17304
17305 /* Consume tokens up through a non-nested END token.  */
17306
17307 static void
17308 cp_parser_cache_group (cp_parser *parser,
17309                        enum cpp_ttype end,
17310                        unsigned depth)
17311 {
17312   while (true)
17313     {
17314       cp_token *token;
17315
17316       /* Abort a parenthesized expression if we encounter a brace.  */
17317       if ((end == CPP_CLOSE_PAREN || depth == 0)
17318           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17319         return;
17320       /* If we've reached the end of the file, stop.  */
17321       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17322           || (end != CPP_PRAGMA_EOL
17323               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17324         return;
17325       /* Consume the next token.  */
17326       token = cp_lexer_consume_token (parser->lexer);
17327       /* See if it starts a new group.  */
17328       if (token->type == CPP_OPEN_BRACE)
17329         {
17330           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17331           if (depth == 0)
17332             return;
17333         }
17334       else if (token->type == CPP_OPEN_PAREN)
17335         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17336       else if (token->type == CPP_PRAGMA)
17337         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17338       else if (token->type == end)
17339         return;
17340     }
17341 }
17342
17343 /* Begin parsing tentatively.  We always save tokens while parsing
17344    tentatively so that if the tentative parsing fails we can restore the
17345    tokens.  */
17346
17347 static void
17348 cp_parser_parse_tentatively (cp_parser* parser)
17349 {
17350   /* Enter a new parsing context.  */
17351   parser->context = cp_parser_context_new (parser->context);
17352   /* Begin saving tokens.  */
17353   cp_lexer_save_tokens (parser->lexer);
17354   /* In order to avoid repetitive access control error messages,
17355      access checks are queued up until we are no longer parsing
17356      tentatively.  */
17357   push_deferring_access_checks (dk_deferred);
17358 }
17359
17360 /* Commit to the currently active tentative parse.  */
17361
17362 static void
17363 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17364 {
17365   cp_parser_context *context;
17366   cp_lexer *lexer;
17367
17368   /* Mark all of the levels as committed.  */
17369   lexer = parser->lexer;
17370   for (context = parser->context; context->next; context = context->next)
17371     {
17372       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17373         break;
17374       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17375       while (!cp_lexer_saving_tokens (lexer))
17376         lexer = lexer->next;
17377       cp_lexer_commit_tokens (lexer);
17378     }
17379 }
17380
17381 /* Abort the currently active tentative parse.  All consumed tokens
17382    will be rolled back, and no diagnostics will be issued.  */
17383
17384 static void
17385 cp_parser_abort_tentative_parse (cp_parser* parser)
17386 {
17387   cp_parser_simulate_error (parser);
17388   /* Now, pretend that we want to see if the construct was
17389      successfully parsed.  */
17390   cp_parser_parse_definitely (parser);
17391 }
17392
17393 /* Stop parsing tentatively.  If a parse error has occurred, restore the
17394    token stream.  Otherwise, commit to the tokens we have consumed.
17395    Returns true if no error occurred; false otherwise.  */
17396
17397 static bool
17398 cp_parser_parse_definitely (cp_parser* parser)
17399 {
17400   bool error_occurred;
17401   cp_parser_context *context;
17402
17403   /* Remember whether or not an error occurred, since we are about to
17404      destroy that information.  */
17405   error_occurred = cp_parser_error_occurred (parser);
17406   /* Remove the topmost context from the stack.  */
17407   context = parser->context;
17408   parser->context = context->next;
17409   /* If no parse errors occurred, commit to the tentative parse.  */
17410   if (!error_occurred)
17411     {
17412       /* Commit to the tokens read tentatively, unless that was
17413          already done.  */
17414       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17415         cp_lexer_commit_tokens (parser->lexer);
17416
17417       pop_to_parent_deferring_access_checks ();
17418     }
17419   /* Otherwise, if errors occurred, roll back our state so that things
17420      are just as they were before we began the tentative parse.  */
17421   else
17422     {
17423       cp_lexer_rollback_tokens (parser->lexer);
17424       pop_deferring_access_checks ();
17425     }
17426   /* Add the context to the front of the free list.  */
17427   context->next = cp_parser_context_free_list;
17428   cp_parser_context_free_list = context;
17429
17430   return !error_occurred;
17431 }
17432
17433 /* Returns true if we are parsing tentatively and are not committed to
17434    this tentative parse.  */
17435
17436 static bool
17437 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17438 {
17439   return (cp_parser_parsing_tentatively (parser)
17440           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17441 }
17442
17443 /* Returns nonzero iff an error has occurred during the most recent
17444    tentative parse.  */
17445
17446 static bool
17447 cp_parser_error_occurred (cp_parser* parser)
17448 {
17449   return (cp_parser_parsing_tentatively (parser)
17450           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17451 }
17452
17453 /* Returns nonzero if GNU extensions are allowed.  */
17454
17455 static bool
17456 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17457 {
17458   return parser->allow_gnu_extensions_p;
17459 }
17460 \f
17461 /* Objective-C++ Productions */
17462
17463
17464 /* Parse an Objective-C expression, which feeds into a primary-expression
17465    above.
17466
17467    objc-expression:
17468      objc-message-expression
17469      objc-string-literal
17470      objc-encode-expression
17471      objc-protocol-expression
17472      objc-selector-expression
17473
17474   Returns a tree representation of the expression.  */
17475
17476 static tree
17477 cp_parser_objc_expression (cp_parser* parser)
17478 {
17479   /* Try to figure out what kind of declaration is present.  */
17480   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17481
17482   switch (kwd->type)
17483     {
17484     case CPP_OPEN_SQUARE:
17485       return cp_parser_objc_message_expression (parser);
17486
17487     case CPP_OBJC_STRING:
17488       kwd = cp_lexer_consume_token (parser->lexer);
17489       return objc_build_string_object (kwd->u.value);
17490
17491     case CPP_KEYWORD:
17492       switch (kwd->keyword)
17493         {
17494         case RID_AT_ENCODE:
17495           return cp_parser_objc_encode_expression (parser);
17496
17497         case RID_AT_PROTOCOL:
17498           return cp_parser_objc_protocol_expression (parser);
17499
17500         case RID_AT_SELECTOR:
17501           return cp_parser_objc_selector_expression (parser);
17502
17503         default:
17504           break;
17505         }
17506     default:
17507       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17508       cp_parser_skip_to_end_of_block_or_statement (parser);
17509     }
17510
17511   return error_mark_node;
17512 }
17513
17514 /* Parse an Objective-C message expression.
17515
17516    objc-message-expression:
17517      [ objc-message-receiver objc-message-args ]
17518
17519    Returns a representation of an Objective-C message.  */
17520
17521 static tree
17522 cp_parser_objc_message_expression (cp_parser* parser)
17523 {
17524   tree receiver, messageargs;
17525
17526   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
17527   receiver = cp_parser_objc_message_receiver (parser);
17528   messageargs = cp_parser_objc_message_args (parser);
17529   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17530
17531   return objc_build_message_expr (build_tree_list (receiver, messageargs));
17532 }
17533
17534 /* Parse an objc-message-receiver.
17535
17536    objc-message-receiver:
17537      expression
17538      simple-type-specifier
17539
17540   Returns a representation of the type or expression.  */
17541
17542 static tree
17543 cp_parser_objc_message_receiver (cp_parser* parser)
17544 {
17545   tree rcv;
17546
17547   /* An Objective-C message receiver may be either (1) a type
17548      or (2) an expression.  */
17549   cp_parser_parse_tentatively (parser);
17550   rcv = cp_parser_expression (parser, false);
17551
17552   if (cp_parser_parse_definitely (parser))
17553     return rcv;
17554
17555   rcv = cp_parser_simple_type_specifier (parser,
17556                                          /*decl_specs=*/NULL,
17557                                          CP_PARSER_FLAGS_NONE);
17558
17559   return objc_get_class_reference (rcv);
17560 }
17561
17562 /* Parse the arguments and selectors comprising an Objective-C message.
17563
17564    objc-message-args:
17565      objc-selector
17566      objc-selector-args
17567      objc-selector-args , objc-comma-args
17568
17569    objc-selector-args:
17570      objc-selector [opt] : assignment-expression
17571      objc-selector-args objc-selector [opt] : assignment-expression
17572
17573    objc-comma-args:
17574      assignment-expression
17575      objc-comma-args , assignment-expression
17576
17577    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17578    selector arguments and TREE_VALUE containing a list of comma
17579    arguments.  */
17580
17581 static tree
17582 cp_parser_objc_message_args (cp_parser* parser)
17583 {
17584   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17585   bool maybe_unary_selector_p = true;
17586   cp_token *token = cp_lexer_peek_token (parser->lexer);
17587
17588   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17589     {
17590       tree selector = NULL_TREE, arg;
17591
17592       if (token->type != CPP_COLON)
17593         selector = cp_parser_objc_selector (parser);
17594
17595       /* Detect if we have a unary selector.  */
17596       if (maybe_unary_selector_p
17597           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17598         return build_tree_list (selector, NULL_TREE);
17599
17600       maybe_unary_selector_p = false;
17601       cp_parser_require (parser, CPP_COLON, "`:'");
17602       arg = cp_parser_assignment_expression (parser, false);
17603
17604       sel_args
17605         = chainon (sel_args,
17606                    build_tree_list (selector, arg));
17607
17608       token = cp_lexer_peek_token (parser->lexer);
17609     }
17610
17611   /* Handle non-selector arguments, if any. */
17612   while (token->type == CPP_COMMA)
17613     {
17614       tree arg;
17615
17616       cp_lexer_consume_token (parser->lexer);
17617       arg = cp_parser_assignment_expression (parser, false);
17618
17619       addl_args
17620         = chainon (addl_args,
17621                    build_tree_list (NULL_TREE, arg));
17622
17623       token = cp_lexer_peek_token (parser->lexer);
17624     }
17625
17626   return build_tree_list (sel_args, addl_args);
17627 }
17628
17629 /* Parse an Objective-C encode expression.
17630
17631    objc-encode-expression:
17632      @encode objc-typename
17633
17634    Returns an encoded representation of the type argument.  */
17635
17636 static tree
17637 cp_parser_objc_encode_expression (cp_parser* parser)
17638 {
17639   tree type;
17640
17641   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
17642   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17643   type = complete_type (cp_parser_type_id (parser));
17644   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17645
17646   if (!type)
17647     {
17648       error ("%<@encode%> must specify a type as an argument");
17649       return error_mark_node;
17650     }
17651
17652   return objc_build_encode_expr (type);
17653 }
17654
17655 /* Parse an Objective-C @defs expression.  */
17656
17657 static tree
17658 cp_parser_objc_defs_expression (cp_parser *parser)
17659 {
17660   tree name;
17661
17662   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
17663   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17664   name = cp_parser_identifier (parser);
17665   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17666
17667   return objc_get_class_ivars (name);
17668 }
17669
17670 /* Parse an Objective-C protocol expression.
17671
17672   objc-protocol-expression:
17673     @protocol ( identifier )
17674
17675   Returns a representation of the protocol expression.  */
17676
17677 static tree
17678 cp_parser_objc_protocol_expression (cp_parser* parser)
17679 {
17680   tree proto;
17681
17682   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17683   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17684   proto = cp_parser_identifier (parser);
17685   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17686
17687   return objc_build_protocol_expr (proto);
17688 }
17689
17690 /* Parse an Objective-C selector expression.
17691
17692    objc-selector-expression:
17693      @selector ( objc-method-signature )
17694
17695    objc-method-signature:
17696      objc-selector
17697      objc-selector-seq
17698
17699    objc-selector-seq:
17700      objc-selector :
17701      objc-selector-seq objc-selector :
17702
17703   Returns a representation of the method selector.  */
17704
17705 static tree
17706 cp_parser_objc_selector_expression (cp_parser* parser)
17707 {
17708   tree sel_seq = NULL_TREE;
17709   bool maybe_unary_selector_p = true;
17710   cp_token *token;
17711
17712   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
17713   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17714   token = cp_lexer_peek_token (parser->lexer);
17715
17716   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17717          || token->type == CPP_SCOPE)
17718     {
17719       tree selector = NULL_TREE;
17720
17721       if (token->type != CPP_COLON
17722           || token->type == CPP_SCOPE)
17723         selector = cp_parser_objc_selector (parser);
17724
17725       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17726           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17727         {
17728           /* Detect if we have a unary selector.  */
17729           if (maybe_unary_selector_p)
17730             {
17731               sel_seq = selector;
17732               goto finish_selector;
17733             }
17734           else
17735             {
17736               cp_parser_error (parser, "expected %<:%>");
17737             }
17738         }
17739       maybe_unary_selector_p = false;
17740       token = cp_lexer_consume_token (parser->lexer);
17741
17742       if (token->type == CPP_SCOPE)
17743         {
17744           sel_seq
17745             = chainon (sel_seq,
17746                        build_tree_list (selector, NULL_TREE));
17747           sel_seq
17748             = chainon (sel_seq,
17749                        build_tree_list (NULL_TREE, NULL_TREE));
17750         }
17751       else
17752         sel_seq
17753           = chainon (sel_seq,
17754                      build_tree_list (selector, NULL_TREE));
17755
17756       token = cp_lexer_peek_token (parser->lexer);
17757     }
17758
17759  finish_selector:
17760   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17761
17762   return objc_build_selector_expr (sel_seq);
17763 }
17764
17765 /* Parse a list of identifiers.
17766
17767    objc-identifier-list:
17768      identifier
17769      objc-identifier-list , identifier
17770
17771    Returns a TREE_LIST of identifier nodes.  */
17772
17773 static tree
17774 cp_parser_objc_identifier_list (cp_parser* parser)
17775 {
17776   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17777   cp_token *sep = cp_lexer_peek_token (parser->lexer);
17778
17779   while (sep->type == CPP_COMMA)
17780     {
17781       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17782       list = chainon (list,
17783                       build_tree_list (NULL_TREE,
17784                                        cp_parser_identifier (parser)));
17785       sep = cp_lexer_peek_token (parser->lexer);
17786     }
17787
17788   return list;
17789 }
17790
17791 /* Parse an Objective-C alias declaration.
17792
17793    objc-alias-declaration:
17794      @compatibility_alias identifier identifier ;
17795
17796    This function registers the alias mapping with the Objective-C front end.
17797    It returns nothing.  */
17798
17799 static void
17800 cp_parser_objc_alias_declaration (cp_parser* parser)
17801 {
17802   tree alias, orig;
17803
17804   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17805   alias = cp_parser_identifier (parser);
17806   orig = cp_parser_identifier (parser);
17807   objc_declare_alias (alias, orig);
17808   cp_parser_consume_semicolon_at_end_of_statement (parser);
17809 }
17810
17811 /* Parse an Objective-C class forward-declaration.
17812
17813    objc-class-declaration:
17814      @class objc-identifier-list ;
17815
17816    The function registers the forward declarations with the Objective-C
17817    front end.  It returns nothing.  */
17818
17819 static void
17820 cp_parser_objc_class_declaration (cp_parser* parser)
17821 {
17822   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17823   objc_declare_class (cp_parser_objc_identifier_list (parser));
17824   cp_parser_consume_semicolon_at_end_of_statement (parser);
17825 }
17826
17827 /* Parse a list of Objective-C protocol references.
17828
17829    objc-protocol-refs-opt:
17830      objc-protocol-refs [opt]
17831
17832    objc-protocol-refs:
17833      < objc-identifier-list >
17834
17835    Returns a TREE_LIST of identifiers, if any.  */
17836
17837 static tree
17838 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17839 {
17840   tree protorefs = NULL_TREE;
17841
17842   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17843     {
17844       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17845       protorefs = cp_parser_objc_identifier_list (parser);
17846       cp_parser_require (parser, CPP_GREATER, "`>'");
17847     }
17848
17849   return protorefs;
17850 }
17851
17852 /* Parse a Objective-C visibility specification.  */
17853
17854 static void
17855 cp_parser_objc_visibility_spec (cp_parser* parser)
17856 {
17857   cp_token *vis = cp_lexer_peek_token (parser->lexer);
17858
17859   switch (vis->keyword)
17860     {
17861     case RID_AT_PRIVATE:
17862       objc_set_visibility (2);
17863       break;
17864     case RID_AT_PROTECTED:
17865       objc_set_visibility (0);
17866       break;
17867     case RID_AT_PUBLIC:
17868       objc_set_visibility (1);
17869       break;
17870     default:
17871       return;
17872     }
17873
17874   /* Eat '@private'/'@protected'/'@public'.  */
17875   cp_lexer_consume_token (parser->lexer);
17876 }
17877
17878 /* Parse an Objective-C method type.  */
17879
17880 static void
17881 cp_parser_objc_method_type (cp_parser* parser)
17882 {
17883   objc_set_method_type
17884    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17885     ? PLUS_EXPR
17886     : MINUS_EXPR);
17887 }
17888
17889 /* Parse an Objective-C protocol qualifier.  */
17890
17891 static tree
17892 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17893 {
17894   tree quals = NULL_TREE, node;
17895   cp_token *token = cp_lexer_peek_token (parser->lexer);
17896
17897   node = token->u.value;
17898
17899   while (node && TREE_CODE (node) == IDENTIFIER_NODE
17900          && (node == ridpointers [(int) RID_IN]
17901              || node == ridpointers [(int) RID_OUT]
17902              || node == ridpointers [(int) RID_INOUT]
17903              || node == ridpointers [(int) RID_BYCOPY]
17904              || node == ridpointers [(int) RID_BYREF]
17905              || node == ridpointers [(int) RID_ONEWAY]))
17906     {
17907       quals = tree_cons (NULL_TREE, node, quals);
17908       cp_lexer_consume_token (parser->lexer);
17909       token = cp_lexer_peek_token (parser->lexer);
17910       node = token->u.value;
17911     }
17912
17913   return quals;
17914 }
17915
17916 /* Parse an Objective-C typename.  */
17917
17918 static tree
17919 cp_parser_objc_typename (cp_parser* parser)
17920 {
17921   tree typename = NULL_TREE;
17922
17923   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17924     {
17925       tree proto_quals, cp_type = NULL_TREE;
17926
17927       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17928       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17929
17930       /* An ObjC type name may consist of just protocol qualifiers, in which
17931          case the type shall default to 'id'.  */
17932       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17933         cp_type = cp_parser_type_id (parser);
17934
17935       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17936       typename = build_tree_list (proto_quals, cp_type);
17937     }
17938
17939   return typename;
17940 }
17941
17942 /* Check to see if TYPE refers to an Objective-C selector name.  */
17943
17944 static bool
17945 cp_parser_objc_selector_p (enum cpp_ttype type)
17946 {
17947   return (type == CPP_NAME || type == CPP_KEYWORD
17948           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17949           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17950           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17951           || type == CPP_XOR || type == CPP_XOR_EQ);
17952 }
17953
17954 /* Parse an Objective-C selector.  */
17955
17956 static tree
17957 cp_parser_objc_selector (cp_parser* parser)
17958 {
17959   cp_token *token = cp_lexer_consume_token (parser->lexer);
17960
17961   if (!cp_parser_objc_selector_p (token->type))
17962     {
17963       error ("invalid Objective-C++ selector name");
17964       return error_mark_node;
17965     }
17966
17967   /* C++ operator names are allowed to appear in ObjC selectors.  */
17968   switch (token->type)
17969     {
17970     case CPP_AND_AND: return get_identifier ("and");
17971     case CPP_AND_EQ: return get_identifier ("and_eq");
17972     case CPP_AND: return get_identifier ("bitand");
17973     case CPP_OR: return get_identifier ("bitor");
17974     case CPP_COMPL: return get_identifier ("compl");
17975     case CPP_NOT: return get_identifier ("not");
17976     case CPP_NOT_EQ: return get_identifier ("not_eq");
17977     case CPP_OR_OR: return get_identifier ("or");
17978     case CPP_OR_EQ: return get_identifier ("or_eq");
17979     case CPP_XOR: return get_identifier ("xor");
17980     case CPP_XOR_EQ: return get_identifier ("xor_eq");
17981     default: return token->u.value;
17982     }
17983 }
17984
17985 /* Parse an Objective-C params list.  */
17986
17987 static tree
17988 cp_parser_objc_method_keyword_params (cp_parser* parser)
17989 {
17990   tree params = NULL_TREE;
17991   bool maybe_unary_selector_p = true;
17992   cp_token *token = cp_lexer_peek_token (parser->lexer);
17993
17994   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17995     {
17996       tree selector = NULL_TREE, typename, identifier;
17997
17998       if (token->type != CPP_COLON)
17999         selector = cp_parser_objc_selector (parser);
18000
18001       /* Detect if we have a unary selector.  */
18002       if (maybe_unary_selector_p
18003           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18004         return selector;
18005
18006       maybe_unary_selector_p = false;
18007       cp_parser_require (parser, CPP_COLON, "`:'");
18008       typename = cp_parser_objc_typename (parser);
18009       identifier = cp_parser_identifier (parser);
18010
18011       params
18012         = chainon (params,
18013                    objc_build_keyword_decl (selector,
18014                                             typename,
18015                                             identifier));
18016
18017       token = cp_lexer_peek_token (parser->lexer);
18018     }
18019
18020   return params;
18021 }
18022
18023 /* Parse the non-keyword Objective-C params.  */
18024
18025 static tree
18026 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18027 {
18028   tree params = make_node (TREE_LIST);
18029   cp_token *token = cp_lexer_peek_token (parser->lexer);
18030   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
18031
18032   while (token->type == CPP_COMMA)
18033     {
18034       cp_parameter_declarator *parmdecl;
18035       tree parm;
18036
18037       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18038       token = cp_lexer_peek_token (parser->lexer);
18039
18040       if (token->type == CPP_ELLIPSIS)
18041         {
18042           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
18043           *ellipsisp = true;
18044           break;
18045         }
18046
18047       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18048       parm = grokdeclarator (parmdecl->declarator,
18049                              &parmdecl->decl_specifiers,
18050                              PARM, /*initialized=*/0,
18051                              /*attrlist=*/NULL);
18052
18053       chainon (params, build_tree_list (NULL_TREE, parm));
18054       token = cp_lexer_peek_token (parser->lexer);
18055     }
18056
18057   return params;
18058 }
18059
18060 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
18061
18062 static void
18063 cp_parser_objc_interstitial_code (cp_parser* parser)
18064 {
18065   cp_token *token = cp_lexer_peek_token (parser->lexer);
18066
18067   /* If the next token is `extern' and the following token is a string
18068      literal, then we have a linkage specification.  */
18069   if (token->keyword == RID_EXTERN
18070       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18071     cp_parser_linkage_specification (parser);
18072   /* Handle #pragma, if any.  */
18073   else if (token->type == CPP_PRAGMA)
18074     cp_parser_pragma (parser, pragma_external);
18075   /* Allow stray semicolons.  */
18076   else if (token->type == CPP_SEMICOLON)
18077     cp_lexer_consume_token (parser->lexer);
18078   /* Finally, try to parse a block-declaration, or a function-definition.  */
18079   else
18080     cp_parser_block_declaration (parser, /*statement_p=*/false);
18081 }
18082
18083 /* Parse a method signature.  */
18084
18085 static tree
18086 cp_parser_objc_method_signature (cp_parser* parser)
18087 {
18088   tree rettype, kwdparms, optparms;
18089   bool ellipsis = false;
18090
18091   cp_parser_objc_method_type (parser);
18092   rettype = cp_parser_objc_typename (parser);
18093   kwdparms = cp_parser_objc_method_keyword_params (parser);
18094   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18095
18096   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18097 }
18098
18099 /* Pars an Objective-C method prototype list.  */
18100
18101 static void
18102 cp_parser_objc_method_prototype_list (cp_parser* parser)
18103 {
18104   cp_token *token = cp_lexer_peek_token (parser->lexer);
18105
18106   while (token->keyword != RID_AT_END)
18107     {
18108       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18109         {
18110           objc_add_method_declaration
18111            (cp_parser_objc_method_signature (parser));
18112           cp_parser_consume_semicolon_at_end_of_statement (parser);
18113         }
18114       else
18115         /* Allow for interspersed non-ObjC++ code.  */
18116         cp_parser_objc_interstitial_code (parser);
18117
18118       token = cp_lexer_peek_token (parser->lexer);
18119     }
18120
18121   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18122   objc_finish_interface ();
18123 }
18124
18125 /* Parse an Objective-C method definition list.  */
18126
18127 static void
18128 cp_parser_objc_method_definition_list (cp_parser* parser)
18129 {
18130   cp_token *token = cp_lexer_peek_token (parser->lexer);
18131
18132   while (token->keyword != RID_AT_END)
18133     {
18134       tree meth;
18135
18136       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18137         {
18138           push_deferring_access_checks (dk_deferred);
18139           objc_start_method_definition
18140            (cp_parser_objc_method_signature (parser));
18141
18142           /* For historical reasons, we accept an optional semicolon.  */
18143           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18144             cp_lexer_consume_token (parser->lexer);
18145
18146           perform_deferred_access_checks ();
18147           stop_deferring_access_checks ();
18148           meth = cp_parser_function_definition_after_declarator (parser,
18149                                                                  false);
18150           pop_deferring_access_checks ();
18151           objc_finish_method_definition (meth);
18152         }
18153       else
18154         /* Allow for interspersed non-ObjC++ code.  */
18155         cp_parser_objc_interstitial_code (parser);
18156
18157       token = cp_lexer_peek_token (parser->lexer);
18158     }
18159
18160   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18161   objc_finish_implementation ();
18162 }
18163
18164 /* Parse Objective-C ivars.  */
18165
18166 static void
18167 cp_parser_objc_class_ivars (cp_parser* parser)
18168 {
18169   cp_token *token = cp_lexer_peek_token (parser->lexer);
18170
18171   if (token->type != CPP_OPEN_BRACE)
18172     return;     /* No ivars specified.  */
18173
18174   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
18175   token = cp_lexer_peek_token (parser->lexer);
18176
18177   while (token->type != CPP_CLOSE_BRACE)
18178     {
18179       cp_decl_specifier_seq declspecs;
18180       int decl_class_or_enum_p;
18181       tree prefix_attributes;
18182
18183       cp_parser_objc_visibility_spec (parser);
18184
18185       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18186         break;
18187
18188       cp_parser_decl_specifier_seq (parser,
18189                                     CP_PARSER_FLAGS_OPTIONAL,
18190                                     &declspecs,
18191                                     &decl_class_or_enum_p);
18192       prefix_attributes = declspecs.attributes;
18193       declspecs.attributes = NULL_TREE;
18194
18195       /* Keep going until we hit the `;' at the end of the
18196          declaration.  */
18197       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18198         {
18199           tree width = NULL_TREE, attributes, first_attribute, decl;
18200           cp_declarator *declarator = NULL;
18201           int ctor_dtor_or_conv_p;
18202
18203           /* Check for a (possibly unnamed) bitfield declaration.  */
18204           token = cp_lexer_peek_token (parser->lexer);
18205           if (token->type == CPP_COLON)
18206             goto eat_colon;
18207
18208           if (token->type == CPP_NAME
18209               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18210                   == CPP_COLON))
18211             {
18212               /* Get the name of the bitfield.  */
18213               declarator = make_id_declarator (NULL_TREE,
18214                                                cp_parser_identifier (parser),
18215                                                sfk_none);
18216
18217              eat_colon:
18218               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18219               /* Get the width of the bitfield.  */
18220               width
18221                 = cp_parser_constant_expression (parser,
18222                                                  /*allow_non_constant=*/false,
18223                                                  NULL);
18224             }
18225           else
18226             {
18227               /* Parse the declarator.  */
18228               declarator
18229                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18230                                         &ctor_dtor_or_conv_p,
18231                                         /*parenthesized_p=*/NULL,
18232                                         /*member_p=*/false);
18233             }
18234
18235           /* Look for attributes that apply to the ivar.  */
18236           attributes = cp_parser_attributes_opt (parser);
18237           /* Remember which attributes are prefix attributes and
18238              which are not.  */
18239           first_attribute = attributes;
18240           /* Combine the attributes.  */
18241           attributes = chainon (prefix_attributes, attributes);
18242
18243           if (width)
18244             {
18245               /* Create the bitfield declaration.  */
18246               decl = grokbitfield (declarator, &declspecs, width);
18247               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18248             }
18249           else
18250             decl = grokfield (declarator, &declspecs,
18251                               NULL_TREE, /*init_const_expr_p=*/false,
18252                               NULL_TREE, attributes);
18253
18254           /* Add the instance variable.  */
18255           objc_add_instance_variable (decl);
18256
18257           /* Reset PREFIX_ATTRIBUTES.  */
18258           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18259             attributes = TREE_CHAIN (attributes);
18260           if (attributes)
18261             TREE_CHAIN (attributes) = NULL_TREE;
18262
18263           token = cp_lexer_peek_token (parser->lexer);
18264
18265           if (token->type == CPP_COMMA)
18266             {
18267               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18268               continue;
18269             }
18270           break;
18271         }
18272
18273       cp_parser_consume_semicolon_at_end_of_statement (parser);
18274       token = cp_lexer_peek_token (parser->lexer);
18275     }
18276
18277   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
18278   /* For historical reasons, we accept an optional semicolon.  */
18279   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18280     cp_lexer_consume_token (parser->lexer);
18281 }
18282
18283 /* Parse an Objective-C protocol declaration.  */
18284
18285 static void
18286 cp_parser_objc_protocol_declaration (cp_parser* parser)
18287 {
18288   tree proto, protorefs;
18289   cp_token *tok;
18290
18291   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18292   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18293     {
18294       error ("identifier expected after %<@protocol%>");
18295       goto finish;
18296     }
18297
18298   /* See if we have a forward declaration or a definition.  */
18299   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18300
18301   /* Try a forward declaration first.  */
18302   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18303     {
18304       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18305      finish:
18306       cp_parser_consume_semicolon_at_end_of_statement (parser);
18307     }
18308
18309   /* Ok, we got a full-fledged definition (or at least should).  */
18310   else
18311     {
18312       proto = cp_parser_identifier (parser);
18313       protorefs = cp_parser_objc_protocol_refs_opt (parser);
18314       objc_start_protocol (proto, protorefs);
18315       cp_parser_objc_method_prototype_list (parser);
18316     }
18317 }
18318
18319 /* Parse an Objective-C superclass or category.  */
18320
18321 static void
18322 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18323                                                           tree *categ)
18324 {
18325   cp_token *next = cp_lexer_peek_token (parser->lexer);
18326
18327   *super = *categ = NULL_TREE;
18328   if (next->type == CPP_COLON)
18329     {
18330       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18331       *super = cp_parser_identifier (parser);
18332     }
18333   else if (next->type == CPP_OPEN_PAREN)
18334     {
18335       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18336       *categ = cp_parser_identifier (parser);
18337       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18338     }
18339 }
18340
18341 /* Parse an Objective-C class interface.  */
18342
18343 static void
18344 cp_parser_objc_class_interface (cp_parser* parser)
18345 {
18346   tree name, super, categ, protos;
18347
18348   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
18349   name = cp_parser_identifier (parser);
18350   cp_parser_objc_superclass_or_category (parser, &super, &categ);
18351   protos = cp_parser_objc_protocol_refs_opt (parser);
18352
18353   /* We have either a class or a category on our hands.  */
18354   if (categ)
18355     objc_start_category_interface (name, categ, protos);
18356   else
18357     {
18358       objc_start_class_interface (name, super, protos);
18359       /* Handle instance variable declarations, if any.  */
18360       cp_parser_objc_class_ivars (parser);
18361       objc_continue_interface ();
18362     }
18363
18364   cp_parser_objc_method_prototype_list (parser);
18365 }
18366
18367 /* Parse an Objective-C class implementation.  */
18368
18369 static void
18370 cp_parser_objc_class_implementation (cp_parser* parser)
18371 {
18372   tree name, super, categ;
18373
18374   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
18375   name = cp_parser_identifier (parser);
18376   cp_parser_objc_superclass_or_category (parser, &super, &categ);
18377
18378   /* We have either a class or a category on our hands.  */
18379   if (categ)
18380     objc_start_category_implementation (name, categ);
18381   else
18382     {
18383       objc_start_class_implementation (name, super);
18384       /* Handle instance variable declarations, if any.  */
18385       cp_parser_objc_class_ivars (parser);
18386       objc_continue_implementation ();
18387     }
18388
18389   cp_parser_objc_method_definition_list (parser);
18390 }
18391
18392 /* Consume the @end token and finish off the implementation.  */
18393
18394 static void
18395 cp_parser_objc_end_implementation (cp_parser* parser)
18396 {
18397   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18398   objc_finish_implementation ();
18399 }
18400
18401 /* Parse an Objective-C declaration.  */
18402
18403 static void
18404 cp_parser_objc_declaration (cp_parser* parser)
18405 {
18406   /* Try to figure out what kind of declaration is present.  */
18407   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18408
18409   switch (kwd->keyword)
18410     {
18411     case RID_AT_ALIAS:
18412       cp_parser_objc_alias_declaration (parser);
18413       break;
18414     case RID_AT_CLASS:
18415       cp_parser_objc_class_declaration (parser);
18416       break;
18417     case RID_AT_PROTOCOL:
18418       cp_parser_objc_protocol_declaration (parser);
18419       break;
18420     case RID_AT_INTERFACE:
18421       cp_parser_objc_class_interface (parser);
18422       break;
18423     case RID_AT_IMPLEMENTATION:
18424       cp_parser_objc_class_implementation (parser);
18425       break;
18426     case RID_AT_END:
18427       cp_parser_objc_end_implementation (parser);
18428       break;
18429     default:
18430       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18431       cp_parser_skip_to_end_of_block_or_statement (parser);
18432     }
18433 }
18434
18435 /* Parse an Objective-C try-catch-finally statement.
18436
18437    objc-try-catch-finally-stmt:
18438      @try compound-statement objc-catch-clause-seq [opt]
18439        objc-finally-clause [opt]
18440
18441    objc-catch-clause-seq:
18442      objc-catch-clause objc-catch-clause-seq [opt]
18443
18444    objc-catch-clause:
18445      @catch ( exception-declaration ) compound-statement
18446
18447    objc-finally-clause
18448      @finally compound-statement
18449
18450    Returns NULL_TREE.  */
18451
18452 static tree
18453 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18454   location_t location;
18455   tree stmt;
18456
18457   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18458   location = cp_lexer_peek_token (parser->lexer)->location;
18459   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18460      node, lest it get absorbed into the surrounding block.  */
18461   stmt = push_stmt_list ();
18462   cp_parser_compound_statement (parser, NULL, false);
18463   objc_begin_try_stmt (location, pop_stmt_list (stmt));
18464
18465   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18466     {
18467       cp_parameter_declarator *parmdecl;
18468       tree parm;
18469
18470       cp_lexer_consume_token (parser->lexer);
18471       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18472       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18473       parm = grokdeclarator (parmdecl->declarator,
18474                              &parmdecl->decl_specifiers,
18475                              PARM, /*initialized=*/0,
18476                              /*attrlist=*/NULL);
18477       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18478       objc_begin_catch_clause (parm);
18479       cp_parser_compound_statement (parser, NULL, false);
18480       objc_finish_catch_clause ();
18481     }
18482
18483   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18484     {
18485       cp_lexer_consume_token (parser->lexer);
18486       location = cp_lexer_peek_token (parser->lexer)->location;
18487       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18488          node, lest it get absorbed into the surrounding block.  */
18489       stmt = push_stmt_list ();
18490       cp_parser_compound_statement (parser, NULL, false);
18491       objc_build_finally_clause (location, pop_stmt_list (stmt));
18492     }
18493
18494   return objc_finish_try_stmt ();
18495 }
18496
18497 /* Parse an Objective-C synchronized statement.
18498
18499    objc-synchronized-stmt:
18500      @synchronized ( expression ) compound-statement
18501
18502    Returns NULL_TREE.  */
18503
18504 static tree
18505 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18506   location_t location;
18507   tree lock, stmt;
18508
18509   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18510
18511   location = cp_lexer_peek_token (parser->lexer)->location;
18512   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18513   lock = cp_parser_expression (parser, false);
18514   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18515
18516   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18517      node, lest it get absorbed into the surrounding block.  */
18518   stmt = push_stmt_list ();
18519   cp_parser_compound_statement (parser, NULL, false);
18520
18521   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18522 }
18523
18524 /* Parse an Objective-C throw statement.
18525
18526    objc-throw-stmt:
18527      @throw assignment-expression [opt] ;
18528
18529    Returns a constructed '@throw' statement.  */
18530
18531 static tree
18532 cp_parser_objc_throw_statement (cp_parser *parser) {
18533   tree expr = NULL_TREE;
18534
18535   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18536
18537   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18538     expr = cp_parser_assignment_expression (parser, false);
18539
18540   cp_parser_consume_semicolon_at_end_of_statement (parser);
18541
18542   return objc_build_throw_stmt (expr);
18543 }
18544
18545 /* Parse an Objective-C statement.  */
18546
18547 static tree
18548 cp_parser_objc_statement (cp_parser * parser) {
18549   /* Try to figure out what kind of declaration is present.  */
18550   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18551
18552   switch (kwd->keyword)
18553     {
18554     case RID_AT_TRY:
18555       return cp_parser_objc_try_catch_finally_statement (parser);
18556     case RID_AT_SYNCHRONIZED:
18557       return cp_parser_objc_synchronized_statement (parser);
18558     case RID_AT_THROW:
18559       return cp_parser_objc_throw_statement (parser);
18560     default:
18561       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18562       cp_parser_skip_to_end_of_block_or_statement (parser);
18563     }
18564
18565   return error_mark_node;
18566 }
18567 \f
18568 /* OpenMP 2.5 parsing routines.  */
18569
18570 /* Returns name of the next clause.
18571    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18572    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
18573    returned and the token is consumed.  */
18574
18575 static pragma_omp_clause
18576 cp_parser_omp_clause_name (cp_parser *parser)
18577 {
18578   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18579
18580   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18581     result = PRAGMA_OMP_CLAUSE_IF;
18582   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18583     result = PRAGMA_OMP_CLAUSE_DEFAULT;
18584   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18585     result = PRAGMA_OMP_CLAUSE_PRIVATE;
18586   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18587     {
18588       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18589       const char *p = IDENTIFIER_POINTER (id);
18590
18591       switch (p[0])
18592         {
18593         case 'c':
18594           if (!strcmp ("copyin", p))
18595             result = PRAGMA_OMP_CLAUSE_COPYIN;
18596           else if (!strcmp ("copyprivate", p))
18597             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18598           break;
18599         case 'f':
18600           if (!strcmp ("firstprivate", p))
18601             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18602           break;
18603         case 'l':
18604           if (!strcmp ("lastprivate", p))
18605             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18606           break;
18607         case 'n':
18608           if (!strcmp ("nowait", p))
18609             result = PRAGMA_OMP_CLAUSE_NOWAIT;
18610           else if (!strcmp ("num_threads", p))
18611             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18612           break;
18613         case 'o':
18614           if (!strcmp ("ordered", p))
18615             result = PRAGMA_OMP_CLAUSE_ORDERED;
18616           break;
18617         case 'r':
18618           if (!strcmp ("reduction", p))
18619             result = PRAGMA_OMP_CLAUSE_REDUCTION;
18620           break;
18621         case 's':
18622           if (!strcmp ("schedule", p))
18623             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18624           else if (!strcmp ("shared", p))
18625             result = PRAGMA_OMP_CLAUSE_SHARED;
18626           break;
18627         }
18628     }
18629
18630   if (result != PRAGMA_OMP_CLAUSE_NONE)
18631     cp_lexer_consume_token (parser->lexer);
18632
18633   return result;
18634 }
18635
18636 /* Validate that a clause of the given type does not already exist.  */
18637
18638 static void
18639 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18640 {
18641   tree c;
18642
18643   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18644     if (OMP_CLAUSE_CODE (c) == code)
18645       {
18646         error ("too many %qs clauses", name);
18647         break;
18648       }
18649 }
18650
18651 /* OpenMP 2.5:
18652    variable-list:
18653      identifier
18654      variable-list , identifier
18655
18656    In addition, we match a closing parenthesis.  An opening parenthesis
18657    will have been consumed by the caller.
18658
18659    If KIND is nonzero, create the appropriate node and install the decl
18660    in OMP_CLAUSE_DECL and add the node to the head of the list.
18661
18662    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18663    return the list created.  */
18664
18665 static tree
18666 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18667                                 tree list)
18668 {
18669   while (1)
18670     {
18671       tree name, decl;
18672
18673       name = cp_parser_id_expression (parser, /*template_p=*/false,
18674                                       /*check_dependency_p=*/true,
18675                                       /*template_p=*/NULL,
18676                                       /*declarator_p=*/false,
18677                                       /*optional_p=*/false);
18678       if (name == error_mark_node)
18679         goto skip_comma;
18680
18681       decl = cp_parser_lookup_name_simple (parser, name);
18682       if (decl == error_mark_node)
18683         cp_parser_name_lookup_error (parser, name, decl, NULL);
18684       else if (kind != 0)
18685         {
18686           tree u = build_omp_clause (kind);
18687           OMP_CLAUSE_DECL (u) = decl;
18688           OMP_CLAUSE_CHAIN (u) = list;
18689           list = u;
18690         }
18691       else
18692         list = tree_cons (decl, NULL_TREE, list);
18693
18694     get_comma:
18695       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18696         break;
18697       cp_lexer_consume_token (parser->lexer);
18698     }
18699
18700   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18701     {
18702       int ending;
18703
18704       /* Try to resync to an unnested comma.  Copied from
18705          cp_parser_parenthesized_expression_list.  */
18706     skip_comma:
18707       ending = cp_parser_skip_to_closing_parenthesis (parser,
18708                                                       /*recovering=*/true,
18709                                                       /*or_comma=*/true,
18710                                                       /*consume_paren=*/true);
18711       if (ending < 0)
18712         goto get_comma;
18713     }
18714
18715   return list;
18716 }
18717
18718 /* Similarly, but expect leading and trailing parenthesis.  This is a very
18719    common case for omp clauses.  */
18720
18721 static tree
18722 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18723 {
18724   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18725     return cp_parser_omp_var_list_no_open (parser, kind, list);
18726   return list;
18727 }
18728
18729 /* OpenMP 2.5:
18730    default ( shared | none ) */
18731
18732 static tree
18733 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18734 {
18735   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18736   tree c;
18737
18738   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18739     return list;
18740   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18741     {
18742       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18743       const char *p = IDENTIFIER_POINTER (id);
18744
18745       switch (p[0])
18746         {
18747         case 'n':
18748           if (strcmp ("none", p) != 0)
18749             goto invalid_kind;
18750           kind = OMP_CLAUSE_DEFAULT_NONE;
18751           break;
18752
18753         case 's':
18754           if (strcmp ("shared", p) != 0)
18755             goto invalid_kind;
18756           kind = OMP_CLAUSE_DEFAULT_SHARED;
18757           break;
18758
18759         default:
18760           goto invalid_kind;
18761         }
18762
18763       cp_lexer_consume_token (parser->lexer);
18764     }
18765   else
18766     {
18767     invalid_kind:
18768       cp_parser_error (parser, "expected %<none%> or %<shared%>");
18769     }
18770
18771   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18772     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18773                                            /*or_comma=*/false,
18774                                            /*consume_paren=*/true);
18775
18776   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18777     return list;
18778
18779   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18780   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18781   OMP_CLAUSE_CHAIN (c) = list;
18782   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18783
18784   return c;
18785 }
18786
18787 /* OpenMP 2.5:
18788    if ( expression ) */
18789
18790 static tree
18791 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18792 {
18793   tree t, c;
18794
18795   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18796     return list;
18797
18798   t = cp_parser_condition (parser);
18799
18800   if (t == error_mark_node
18801       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18802     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18803                                            /*or_comma=*/false,
18804                                            /*consume_paren=*/true);
18805
18806   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18807
18808   c = build_omp_clause (OMP_CLAUSE_IF);
18809   OMP_CLAUSE_IF_EXPR (c) = t;
18810   OMP_CLAUSE_CHAIN (c) = list;
18811
18812   return c;
18813 }
18814
18815 /* OpenMP 2.5:
18816    nowait */
18817
18818 static tree
18819 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18820 {
18821   tree c;
18822
18823   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18824
18825   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18826   OMP_CLAUSE_CHAIN (c) = list;
18827   return c;
18828 }
18829
18830 /* OpenMP 2.5:
18831    num_threads ( expression ) */
18832
18833 static tree
18834 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18835 {
18836   tree t, c;
18837
18838   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18839     return list;
18840
18841   t = cp_parser_expression (parser, false);
18842
18843   if (t == error_mark_node
18844       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18845     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18846                                            /*or_comma=*/false,
18847                                            /*consume_paren=*/true);
18848
18849   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18850
18851   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18852   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18853   OMP_CLAUSE_CHAIN (c) = list;
18854
18855   return c;
18856 }
18857
18858 /* OpenMP 2.5:
18859    ordered */
18860
18861 static tree
18862 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18863 {
18864   tree c;
18865
18866   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18867
18868   c = build_omp_clause (OMP_CLAUSE_ORDERED);
18869   OMP_CLAUSE_CHAIN (c) = list;
18870   return c;
18871 }
18872
18873 /* OpenMP 2.5:
18874    reduction ( reduction-operator : variable-list )
18875
18876    reduction-operator:
18877      One of: + * - & ^ | && || */
18878
18879 static tree
18880 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18881 {
18882   enum tree_code code;
18883   tree nlist, c;
18884
18885   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18886     return list;
18887
18888   switch (cp_lexer_peek_token (parser->lexer)->type)
18889     {
18890     case CPP_PLUS:
18891       code = PLUS_EXPR;
18892       break;
18893     case CPP_MULT:
18894       code = MULT_EXPR;
18895       break;
18896     case CPP_MINUS:
18897       code = MINUS_EXPR;
18898       break;
18899     case CPP_AND:
18900       code = BIT_AND_EXPR;
18901       break;
18902     case CPP_XOR:
18903       code = BIT_XOR_EXPR;
18904       break;
18905     case CPP_OR:
18906       code = BIT_IOR_EXPR;
18907       break;
18908     case CPP_AND_AND:
18909       code = TRUTH_ANDIF_EXPR;
18910       break;
18911     case CPP_OR_OR:
18912       code = TRUTH_ORIF_EXPR;
18913       break;
18914     default:
18915       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18916     resync_fail:
18917       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18918                                              /*or_comma=*/false,
18919                                              /*consume_paren=*/true);
18920       return list;
18921     }
18922   cp_lexer_consume_token (parser->lexer);
18923
18924   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18925     goto resync_fail;
18926
18927   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18928   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18929     OMP_CLAUSE_REDUCTION_CODE (c) = code;
18930
18931   return nlist;
18932 }
18933
18934 /* OpenMP 2.5:
18935    schedule ( schedule-kind )
18936    schedule ( schedule-kind , expression )
18937
18938    schedule-kind:
18939      static | dynamic | guided | runtime  */
18940
18941 static tree
18942 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18943 {
18944   tree c, t;
18945
18946   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18947     return list;
18948
18949   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18950
18951   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18952     {
18953       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18954       const char *p = IDENTIFIER_POINTER (id);
18955
18956       switch (p[0])
18957         {
18958         case 'd':
18959           if (strcmp ("dynamic", p) != 0)
18960             goto invalid_kind;
18961           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18962           break;
18963
18964         case 'g':
18965           if (strcmp ("guided", p) != 0)
18966             goto invalid_kind;
18967           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18968           break;
18969
18970         case 'r':
18971           if (strcmp ("runtime", p) != 0)
18972             goto invalid_kind;
18973           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18974           break;
18975
18976         default:
18977           goto invalid_kind;
18978         }
18979     }
18980   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18981     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18982   else
18983     goto invalid_kind;
18984   cp_lexer_consume_token (parser->lexer);
18985
18986   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18987     {
18988       cp_lexer_consume_token (parser->lexer);
18989
18990       t = cp_parser_assignment_expression (parser, false);
18991
18992       if (t == error_mark_node)
18993         goto resync_fail;
18994       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18995         error ("schedule %<runtime%> does not take "
18996                "a %<chunk_size%> parameter");
18997       else
18998         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18999
19000       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19001         goto resync_fail;
19002     }
19003   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19004     goto resync_fail;
19005
19006   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19007   OMP_CLAUSE_CHAIN (c) = list;
19008   return c;
19009
19010  invalid_kind:
19011   cp_parser_error (parser, "invalid schedule kind");
19012  resync_fail:
19013   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19014                                          /*or_comma=*/false,
19015                                          /*consume_paren=*/true);
19016   return list;
19017 }
19018
19019 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
19020    is a bitmask in MASK.  Return the list of clauses found; the result
19021    of clause default goes in *pdefault.  */
19022
19023 static tree
19024 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19025                            const char *where, cp_token *pragma_tok)
19026 {
19027   tree clauses = NULL;
19028
19029   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19030     {
19031       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19032       const char *c_name;
19033       tree prev = clauses;
19034
19035       switch (c_kind)
19036         {
19037         case PRAGMA_OMP_CLAUSE_COPYIN:
19038           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19039           c_name = "copyin";
19040           break;
19041         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19042           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19043                                             clauses);
19044           c_name = "copyprivate";
19045           break;
19046         case PRAGMA_OMP_CLAUSE_DEFAULT:
19047           clauses = cp_parser_omp_clause_default (parser, clauses);
19048           c_name = "default";
19049           break;
19050         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19051           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19052                                             clauses);
19053           c_name = "firstprivate";
19054           break;
19055         case PRAGMA_OMP_CLAUSE_IF:
19056           clauses = cp_parser_omp_clause_if (parser, clauses);
19057           c_name = "if";
19058           break;
19059         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19060           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19061                                             clauses);
19062           c_name = "lastprivate";
19063           break;
19064         case PRAGMA_OMP_CLAUSE_NOWAIT:
19065           clauses = cp_parser_omp_clause_nowait (parser, clauses);
19066           c_name = "nowait";
19067           break;
19068         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19069           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19070           c_name = "num_threads";
19071           break;
19072         case PRAGMA_OMP_CLAUSE_ORDERED:
19073           clauses = cp_parser_omp_clause_ordered (parser, clauses);
19074           c_name = "ordered";
19075           break;
19076         case PRAGMA_OMP_CLAUSE_PRIVATE:
19077           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19078                                             clauses);
19079           c_name = "private";
19080           break;
19081         case PRAGMA_OMP_CLAUSE_REDUCTION:
19082           clauses = cp_parser_omp_clause_reduction (parser, clauses);
19083           c_name = "reduction";
19084           break;
19085         case PRAGMA_OMP_CLAUSE_SCHEDULE:
19086           clauses = cp_parser_omp_clause_schedule (parser, clauses);
19087           c_name = "schedule";
19088           break;
19089         case PRAGMA_OMP_CLAUSE_SHARED:
19090           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19091                                             clauses);
19092           c_name = "shared";
19093           break;
19094         default:
19095           cp_parser_error (parser, "expected %<#pragma omp%> clause");
19096           goto saw_error;
19097         }
19098
19099       if (((mask >> c_kind) & 1) == 0)
19100         {
19101           /* Remove the invalid clause(s) from the list to avoid
19102              confusing the rest of the compiler.  */
19103           clauses = prev;
19104           error ("%qs is not valid for %qs", c_name, where);
19105         }
19106     }
19107  saw_error:
19108   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19109   return finish_omp_clauses (clauses);
19110 }
19111
19112 /* OpenMP 2.5:
19113    structured-block:
19114      statement
19115
19116    In practice, we're also interested in adding the statement to an
19117    outer node.  So it is convenient if we work around the fact that
19118    cp_parser_statement calls add_stmt.  */
19119
19120 static unsigned
19121 cp_parser_begin_omp_structured_block (cp_parser *parser)
19122 {
19123   unsigned save = parser->in_statement;
19124
19125   /* Only move the values to IN_OMP_BLOCK if they weren't false.
19126      This preserves the "not within loop or switch" style error messages
19127      for nonsense cases like
19128         void foo() {
19129         #pragma omp single
19130           break;
19131         }
19132   */
19133   if (parser->in_statement)
19134     parser->in_statement = IN_OMP_BLOCK;
19135
19136   return save;
19137 }
19138
19139 static void
19140 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19141 {
19142   parser->in_statement = save;
19143 }
19144
19145 static tree
19146 cp_parser_omp_structured_block (cp_parser *parser)
19147 {
19148   tree stmt = begin_omp_structured_block ();
19149   unsigned int save = cp_parser_begin_omp_structured_block (parser);
19150
19151   cp_parser_statement (parser, NULL_TREE, false, NULL);
19152
19153   cp_parser_end_omp_structured_block (parser, save);
19154   return finish_omp_structured_block (stmt);
19155 }
19156
19157 /* OpenMP 2.5:
19158    # pragma omp atomic new-line
19159      expression-stmt
19160
19161    expression-stmt:
19162      x binop= expr | x++ | ++x | x-- | --x
19163    binop:
19164      +, *, -, /, &, ^, |, <<, >>
19165
19166   where x is an lvalue expression with scalar type.  */
19167
19168 static void
19169 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19170 {
19171   tree lhs, rhs;
19172   enum tree_code code;
19173
19174   cp_parser_require_pragma_eol (parser, pragma_tok);
19175
19176   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19177                                     /*cast_p=*/false);
19178   switch (TREE_CODE (lhs))
19179     {
19180     case ERROR_MARK:
19181       goto saw_error;
19182
19183     case PREINCREMENT_EXPR:
19184     case POSTINCREMENT_EXPR:
19185       lhs = TREE_OPERAND (lhs, 0);
19186       code = PLUS_EXPR;
19187       rhs = integer_one_node;
19188       break;
19189
19190     case PREDECREMENT_EXPR:
19191     case POSTDECREMENT_EXPR:
19192       lhs = TREE_OPERAND (lhs, 0);
19193       code = MINUS_EXPR;
19194       rhs = integer_one_node;
19195       break;
19196
19197     default:
19198       switch (cp_lexer_peek_token (parser->lexer)->type)
19199         {
19200         case CPP_MULT_EQ:
19201           code = MULT_EXPR;
19202           break;
19203         case CPP_DIV_EQ:
19204           code = TRUNC_DIV_EXPR;
19205           break;
19206         case CPP_PLUS_EQ:
19207           code = PLUS_EXPR;
19208           break;
19209         case CPP_MINUS_EQ:
19210           code = MINUS_EXPR;
19211           break;
19212         case CPP_LSHIFT_EQ:
19213           code = LSHIFT_EXPR;
19214           break;
19215         case CPP_RSHIFT_EQ:
19216           code = RSHIFT_EXPR;
19217           break;
19218         case CPP_AND_EQ:
19219           code = BIT_AND_EXPR;
19220           break;
19221         case CPP_OR_EQ:
19222           code = BIT_IOR_EXPR;
19223           break;
19224         case CPP_XOR_EQ:
19225           code = BIT_XOR_EXPR;
19226           break;
19227         default:
19228           cp_parser_error (parser,
19229                            "invalid operator for %<#pragma omp atomic%>");
19230           goto saw_error;
19231         }
19232       cp_lexer_consume_token (parser->lexer);
19233
19234       rhs = cp_parser_expression (parser, false);
19235       if (rhs == error_mark_node)
19236         goto saw_error;
19237       break;
19238     }
19239   finish_omp_atomic (code, lhs, rhs);
19240   cp_parser_consume_semicolon_at_end_of_statement (parser);
19241   return;
19242
19243  saw_error:
19244   cp_parser_skip_to_end_of_block_or_statement (parser);
19245 }
19246
19247
19248 /* OpenMP 2.5:
19249    # pragma omp barrier new-line  */
19250
19251 static void
19252 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19253 {
19254   cp_parser_require_pragma_eol (parser, pragma_tok);
19255   finish_omp_barrier ();
19256 }
19257
19258 /* OpenMP 2.5:
19259    # pragma omp critical [(name)] new-line
19260      structured-block  */
19261
19262 static tree
19263 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19264 {
19265   tree stmt, name = NULL;
19266
19267   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19268     {
19269       cp_lexer_consume_token (parser->lexer);
19270
19271       name = cp_parser_identifier (parser);
19272
19273       if (name == error_mark_node
19274           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19275         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19276                                                /*or_comma=*/false,
19277                                                /*consume_paren=*/true);
19278       if (name == error_mark_node)
19279         name = NULL;
19280     }
19281   cp_parser_require_pragma_eol (parser, pragma_tok);
19282
19283   stmt = cp_parser_omp_structured_block (parser);
19284   return c_finish_omp_critical (stmt, name);
19285 }
19286
19287 /* OpenMP 2.5:
19288    # pragma omp flush flush-vars[opt] new-line
19289
19290    flush-vars:
19291      ( variable-list ) */
19292
19293 static void
19294 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19295 {
19296   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19297     (void) cp_parser_omp_var_list (parser, 0, NULL);
19298   cp_parser_require_pragma_eol (parser, pragma_tok);
19299
19300   finish_omp_flush ();
19301 }
19302
19303 /* Parse the restricted form of the for statment allowed by OpenMP.  */
19304
19305 static tree
19306 cp_parser_omp_for_loop (cp_parser *parser)
19307 {
19308   tree init, cond, incr, body, decl, pre_body;
19309   location_t loc;
19310
19311   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19312     {
19313       cp_parser_error (parser, "for statement expected");
19314       return NULL;
19315     }
19316   loc = cp_lexer_consume_token (parser->lexer)->location;
19317   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19318     return NULL;
19319
19320   init = decl = NULL;
19321   pre_body = push_stmt_list ();
19322   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19323     {
19324       cp_decl_specifier_seq type_specifiers;
19325
19326       /* First, try to parse as an initialized declaration.  See
19327          cp_parser_condition, from whence the bulk of this is copied.  */
19328
19329       cp_parser_parse_tentatively (parser);
19330       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19331                                     &type_specifiers);
19332       if (!cp_parser_error_occurred (parser))
19333         {
19334           tree asm_specification, attributes;
19335           cp_declarator *declarator;
19336
19337           declarator = cp_parser_declarator (parser,
19338                                              CP_PARSER_DECLARATOR_NAMED,
19339                                              /*ctor_dtor_or_conv_p=*/NULL,
19340                                              /*parenthesized_p=*/NULL,
19341                                              /*member_p=*/false);
19342           attributes = cp_parser_attributes_opt (parser);
19343           asm_specification = cp_parser_asm_specification_opt (parser);
19344
19345           cp_parser_require (parser, CPP_EQ, "`='");
19346           if (cp_parser_parse_definitely (parser))
19347             {
19348               tree pushed_scope;
19349
19350               decl = start_decl (declarator, &type_specifiers,
19351                                  /*initialized_p=*/false, attributes,
19352                                  /*prefix_attributes=*/NULL_TREE,
19353                                  &pushed_scope);
19354
19355               init = cp_parser_assignment_expression (parser, false);
19356
19357               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19358                               asm_specification, LOOKUP_ONLYCONVERTING);
19359
19360               if (pushed_scope)
19361                 pop_scope (pushed_scope);
19362             }
19363         }
19364       else
19365         cp_parser_abort_tentative_parse (parser);
19366
19367       /* If parsing as an initialized declaration failed, try again as
19368          a simple expression.  */
19369       if (decl == NULL)
19370         init = cp_parser_expression (parser, false);
19371     }
19372   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19373   pre_body = pop_stmt_list (pre_body);
19374
19375   cond = NULL;
19376   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19377     cond = cp_parser_condition (parser);
19378   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19379
19380   incr = NULL;
19381   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19382     incr = cp_parser_expression (parser, false);
19383
19384   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19385     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19386                                            /*or_comma=*/false,
19387                                            /*consume_paren=*/true);
19388
19389   /* Note that we saved the original contents of this flag when we entered
19390      the structured block, and so we don't need to re-save it here.  */
19391   parser->in_statement = IN_OMP_FOR;
19392
19393   /* Note that the grammar doesn't call for a structured block here,
19394      though the loop as a whole is a structured block.  */
19395   body = push_stmt_list ();
19396   cp_parser_statement (parser, NULL_TREE, false, NULL);
19397   body = pop_stmt_list (body);
19398
19399   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19400 }
19401
19402 /* OpenMP 2.5:
19403    #pragma omp for for-clause[optseq] new-line
19404      for-loop  */
19405
19406 #define OMP_FOR_CLAUSE_MASK                             \
19407         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19408         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19409         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19410         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19411         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
19412         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
19413         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19414
19415 static tree
19416 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19417 {
19418   tree clauses, sb, ret;
19419   unsigned int save;
19420
19421   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19422                                        "#pragma omp for", pragma_tok);
19423
19424   sb = begin_omp_structured_block ();
19425   save = cp_parser_begin_omp_structured_block (parser);
19426
19427   ret = cp_parser_omp_for_loop (parser);
19428   if (ret)
19429     OMP_FOR_CLAUSES (ret) = clauses;
19430
19431   cp_parser_end_omp_structured_block (parser, save);
19432   add_stmt (finish_omp_structured_block (sb));
19433
19434   return ret;
19435 }
19436
19437 /* OpenMP 2.5:
19438    # pragma omp master new-line
19439      structured-block  */
19440
19441 static tree
19442 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19443 {
19444   cp_parser_require_pragma_eol (parser, pragma_tok);
19445   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19446 }
19447
19448 /* OpenMP 2.5:
19449    # pragma omp ordered new-line
19450      structured-block  */
19451
19452 static tree
19453 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19454 {
19455   cp_parser_require_pragma_eol (parser, pragma_tok);
19456   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19457 }
19458
19459 /* OpenMP 2.5:
19460
19461    section-scope:
19462      { section-sequence }
19463
19464    section-sequence:
19465      section-directive[opt] structured-block
19466      section-sequence section-directive structured-block  */
19467
19468 static tree
19469 cp_parser_omp_sections_scope (cp_parser *parser)
19470 {
19471   tree stmt, substmt;
19472   bool error_suppress = false;
19473   cp_token *tok;
19474
19475   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19476     return NULL_TREE;
19477
19478   stmt = push_stmt_list ();
19479
19480   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19481     {
19482       unsigned save;
19483
19484       substmt = begin_omp_structured_block ();
19485       save = cp_parser_begin_omp_structured_block (parser);
19486
19487       while (1)
19488         {
19489           cp_parser_statement (parser, NULL_TREE, false, NULL);
19490
19491           tok = cp_lexer_peek_token (parser->lexer);
19492           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19493             break;
19494           if (tok->type == CPP_CLOSE_BRACE)
19495             break;
19496           if (tok->type == CPP_EOF)
19497             break;
19498         }
19499
19500       cp_parser_end_omp_structured_block (parser, save);
19501       substmt = finish_omp_structured_block (substmt);
19502       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19503       add_stmt (substmt);
19504     }
19505
19506   while (1)
19507     {
19508       tok = cp_lexer_peek_token (parser->lexer);
19509       if (tok->type == CPP_CLOSE_BRACE)
19510         break;
19511       if (tok->type == CPP_EOF)
19512         break;
19513
19514       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19515         {
19516           cp_lexer_consume_token (parser->lexer);
19517           cp_parser_require_pragma_eol (parser, tok);
19518           error_suppress = false;
19519         }
19520       else if (!error_suppress)
19521         {
19522           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19523           error_suppress = true;
19524         }
19525
19526       substmt = cp_parser_omp_structured_block (parser);
19527       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19528       add_stmt (substmt);
19529     }
19530   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19531
19532   substmt = pop_stmt_list (stmt);
19533
19534   stmt = make_node (OMP_SECTIONS);
19535   TREE_TYPE (stmt) = void_type_node;
19536   OMP_SECTIONS_BODY (stmt) = substmt;
19537
19538   add_stmt (stmt);
19539   return stmt;
19540 }
19541
19542 /* OpenMP 2.5:
19543    # pragma omp sections sections-clause[optseq] newline
19544      sections-scope  */
19545
19546 #define OMP_SECTIONS_CLAUSE_MASK                        \
19547         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19548         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19549         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19550         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19551         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19552
19553 static tree
19554 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19555 {
19556   tree clauses, ret;
19557
19558   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19559                                        "#pragma omp sections", pragma_tok);
19560
19561   ret = cp_parser_omp_sections_scope (parser);
19562   if (ret)
19563     OMP_SECTIONS_CLAUSES (ret) = clauses;
19564
19565   return ret;
19566 }
19567
19568 /* OpenMP 2.5:
19569    # pragma parallel parallel-clause new-line
19570    # pragma parallel for parallel-for-clause new-line
19571    # pragma parallel sections parallel-sections-clause new-line  */
19572
19573 #define OMP_PARALLEL_CLAUSE_MASK                        \
19574         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
19575         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19576         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19577         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
19578         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
19579         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
19580         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19581         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19582
19583 static tree
19584 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19585 {
19586   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19587   const char *p_name = "#pragma omp parallel";
19588   tree stmt, clauses, par_clause, ws_clause, block;
19589   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19590   unsigned int save;
19591
19592   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19593     {
19594       cp_lexer_consume_token (parser->lexer);
19595       p_kind = PRAGMA_OMP_PARALLEL_FOR;
19596       p_name = "#pragma omp parallel for";
19597       mask |= OMP_FOR_CLAUSE_MASK;
19598       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19599     }
19600   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19601     {
19602       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19603       const char *p = IDENTIFIER_POINTER (id);
19604       if (strcmp (p, "sections") == 0)
19605         {
19606           cp_lexer_consume_token (parser->lexer);
19607           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19608           p_name = "#pragma omp parallel sections";
19609           mask |= OMP_SECTIONS_CLAUSE_MASK;
19610           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19611         }
19612     }
19613
19614   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19615   block = begin_omp_parallel ();
19616   save = cp_parser_begin_omp_structured_block (parser);
19617
19618   switch (p_kind)
19619     {
19620     case PRAGMA_OMP_PARALLEL:
19621       cp_parser_already_scoped_statement (parser);
19622       par_clause = clauses;
19623       break;
19624
19625     case PRAGMA_OMP_PARALLEL_FOR:
19626       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19627       stmt = cp_parser_omp_for_loop (parser);
19628       if (stmt)
19629         OMP_FOR_CLAUSES (stmt) = ws_clause;
19630       break;
19631
19632     case PRAGMA_OMP_PARALLEL_SECTIONS:
19633       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19634       stmt = cp_parser_omp_sections_scope (parser);
19635       if (stmt)
19636         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19637       break;
19638
19639     default:
19640       gcc_unreachable ();
19641     }
19642
19643   cp_parser_end_omp_structured_block (parser, save);
19644   stmt = finish_omp_parallel (par_clause, block);
19645   if (p_kind != PRAGMA_OMP_PARALLEL)
19646     OMP_PARALLEL_COMBINED (stmt) = 1;
19647   return stmt;
19648 }
19649
19650 /* OpenMP 2.5:
19651    # pragma omp single single-clause[optseq] new-line
19652      structured-block  */
19653
19654 #define OMP_SINGLE_CLAUSE_MASK                          \
19655         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19656         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19657         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
19658         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19659
19660 static tree
19661 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19662 {
19663   tree stmt = make_node (OMP_SINGLE);
19664   TREE_TYPE (stmt) = void_type_node;
19665
19666   OMP_SINGLE_CLAUSES (stmt)
19667     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19668                                  "#pragma omp single", pragma_tok);
19669   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19670
19671   return add_stmt (stmt);
19672 }
19673
19674 /* OpenMP 2.5:
19675    # pragma omp threadprivate (variable-list) */
19676
19677 static void
19678 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19679 {
19680   tree vars;
19681
19682   vars = cp_parser_omp_var_list (parser, 0, NULL);
19683   cp_parser_require_pragma_eol (parser, pragma_tok);
19684
19685   finish_omp_threadprivate (vars);
19686 }
19687
19688 /* Main entry point to OpenMP statement pragmas.  */
19689
19690 static void
19691 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19692 {
19693   tree stmt;
19694
19695   switch (pragma_tok->pragma_kind)
19696     {
19697     case PRAGMA_OMP_ATOMIC:
19698       cp_parser_omp_atomic (parser, pragma_tok);
19699       return;
19700     case PRAGMA_OMP_CRITICAL:
19701       stmt = cp_parser_omp_critical (parser, pragma_tok);
19702       break;
19703     case PRAGMA_OMP_FOR:
19704       stmt = cp_parser_omp_for (parser, pragma_tok);
19705       break;
19706     case PRAGMA_OMP_MASTER:
19707       stmt = cp_parser_omp_master (parser, pragma_tok);
19708       break;
19709     case PRAGMA_OMP_ORDERED:
19710       stmt = cp_parser_omp_ordered (parser, pragma_tok);
19711       break;
19712     case PRAGMA_OMP_PARALLEL:
19713       stmt = cp_parser_omp_parallel (parser, pragma_tok);
19714       break;
19715     case PRAGMA_OMP_SECTIONS:
19716       stmt = cp_parser_omp_sections (parser, pragma_tok);
19717       break;
19718     case PRAGMA_OMP_SINGLE:
19719       stmt = cp_parser_omp_single (parser, pragma_tok);
19720       break;
19721     default:
19722       gcc_unreachable ();
19723     }
19724
19725   if (stmt)
19726     SET_EXPR_LOCATION (stmt, pragma_tok->location);
19727 }
19728 \f
19729 /* The parser.  */
19730
19731 static GTY (()) cp_parser *the_parser;
19732
19733 \f
19734 /* Special handling for the first token or line in the file.  The first
19735    thing in the file might be #pragma GCC pch_preprocess, which loads a
19736    PCH file, which is a GC collection point.  So we need to handle this
19737    first pragma without benefit of an existing lexer structure.
19738
19739    Always returns one token to the caller in *FIRST_TOKEN.  This is
19740    either the true first token of the file, or the first token after
19741    the initial pragma.  */
19742
19743 static void
19744 cp_parser_initial_pragma (cp_token *first_token)
19745 {
19746   tree name = NULL;
19747
19748   cp_lexer_get_preprocessor_token (NULL, first_token);
19749   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19750     return;
19751
19752   cp_lexer_get_preprocessor_token (NULL, first_token);
19753   if (first_token->type == CPP_STRING)
19754     {
19755       name = first_token->u.value;
19756
19757       cp_lexer_get_preprocessor_token (NULL, first_token);
19758       if (first_token->type != CPP_PRAGMA_EOL)
19759         error ("junk at end of %<#pragma GCC pch_preprocess%>");
19760     }
19761   else
19762     error ("expected string literal");
19763
19764   /* Skip to the end of the pragma.  */
19765   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19766     cp_lexer_get_preprocessor_token (NULL, first_token);
19767
19768   /* Now actually load the PCH file.  */
19769   if (name)
19770     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19771
19772   /* Read one more token to return to our caller.  We have to do this
19773      after reading the PCH file in, since its pointers have to be
19774      live.  */
19775   cp_lexer_get_preprocessor_token (NULL, first_token);
19776 }
19777
19778 /* Normal parsing of a pragma token.  Here we can (and must) use the
19779    regular lexer.  */
19780
19781 static bool
19782 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19783 {
19784   cp_token *pragma_tok;
19785   unsigned int id;
19786
19787   pragma_tok = cp_lexer_consume_token (parser->lexer);
19788   gcc_assert (pragma_tok->type == CPP_PRAGMA);
19789   parser->lexer->in_pragma = true;
19790
19791   id = pragma_tok->pragma_kind;
19792   switch (id)
19793     {
19794     case PRAGMA_GCC_PCH_PREPROCESS:
19795       error ("%<#pragma GCC pch_preprocess%> must be first");
19796       break;
19797
19798     case PRAGMA_OMP_BARRIER:
19799       switch (context)
19800         {
19801         case pragma_compound:
19802           cp_parser_omp_barrier (parser, pragma_tok);
19803           return false;
19804         case pragma_stmt:
19805           error ("%<#pragma omp barrier%> may only be "
19806                  "used in compound statements");
19807           break;
19808         default:
19809           goto bad_stmt;
19810         }
19811       break;
19812
19813     case PRAGMA_OMP_FLUSH:
19814       switch (context)
19815         {
19816         case pragma_compound:
19817           cp_parser_omp_flush (parser, pragma_tok);
19818           return false;
19819         case pragma_stmt:
19820           error ("%<#pragma omp flush%> may only be "
19821                  "used in compound statements");
19822           break;
19823         default:
19824           goto bad_stmt;
19825         }
19826       break;
19827
19828     case PRAGMA_OMP_THREADPRIVATE:
19829       cp_parser_omp_threadprivate (parser, pragma_tok);
19830       return false;
19831
19832     case PRAGMA_OMP_ATOMIC:
19833     case PRAGMA_OMP_CRITICAL:
19834     case PRAGMA_OMP_FOR:
19835     case PRAGMA_OMP_MASTER:
19836     case PRAGMA_OMP_ORDERED:
19837     case PRAGMA_OMP_PARALLEL:
19838     case PRAGMA_OMP_SECTIONS:
19839     case PRAGMA_OMP_SINGLE:
19840       if (context == pragma_external)
19841         goto bad_stmt;
19842       cp_parser_omp_construct (parser, pragma_tok);
19843       return true;
19844
19845     case PRAGMA_OMP_SECTION:
19846       error ("%<#pragma omp section%> may only be used in "
19847              "%<#pragma omp sections%> construct");
19848       break;
19849
19850     default:
19851       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19852       c_invoke_pragma_handler (id);
19853       break;
19854
19855     bad_stmt:
19856       cp_parser_error (parser, "expected declaration specifiers");
19857       break;
19858     }
19859
19860   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19861   return false;
19862 }
19863
19864 /* The interface the pragma parsers have to the lexer.  */
19865
19866 enum cpp_ttype
19867 pragma_lex (tree *value)
19868 {
19869   cp_token *tok;
19870   enum cpp_ttype ret;
19871
19872   tok = cp_lexer_peek_token (the_parser->lexer);
19873
19874   ret = tok->type;
19875   *value = tok->u.value;
19876
19877   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19878     ret = CPP_EOF;
19879   else if (ret == CPP_STRING)
19880     *value = cp_parser_string_literal (the_parser, false, false);
19881   else
19882     {
19883       cp_lexer_consume_token (the_parser->lexer);
19884       if (ret == CPP_KEYWORD)
19885         ret = CPP_NAME;
19886     }
19887
19888   return ret;
19889 }
19890
19891 \f
19892 /* External interface.  */
19893
19894 /* Parse one entire translation unit.  */
19895
19896 void
19897 c_parse_file (void)
19898 {
19899   bool error_occurred;
19900   static bool already_called = false;
19901
19902   if (already_called)
19903     {
19904       sorry ("inter-module optimizations not implemented for C++");
19905       return;
19906     }
19907   already_called = true;
19908
19909   the_parser = cp_parser_new ();
19910   push_deferring_access_checks (flag_access_control
19911                                 ? dk_no_deferred : dk_no_check);
19912   error_occurred = cp_parser_translation_unit (the_parser);
19913   the_parser = NULL;
19914 }
19915
19916 #include "gt-cp-parser.h"