OSDN Git Service

484c6b5a5cc665697fb88c4570b5d631c7ec9ac9
[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 *, bool);
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                            bool rvalue_ref)
965 {
966   cp_declarator *declarator;
967
968   declarator = make_declarator (cdk_reference);
969   declarator->declarator = target;
970   declarator->u.reference.qualifiers = cv_qualifiers;
971   declarator->u.reference.rvalue_ref = rvalue_ref;
972   if (target)
973     {
974       declarator->parameter_pack_p = target->parameter_pack_p;
975       target->parameter_pack_p = false;
976     }
977   else
978     declarator->parameter_pack_p = false;
979
980   return declarator;
981 }
982
983 /* Like make_pointer_declarator -- but for a pointer to a non-static
984    member of CLASS_TYPE.  */
985
986 cp_declarator *
987 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
988                         cp_declarator *pointee)
989 {
990   cp_declarator *declarator;
991
992   declarator = make_declarator (cdk_ptrmem);
993   declarator->declarator = pointee;
994   declarator->u.pointer.qualifiers = cv_qualifiers;
995   declarator->u.pointer.class_type = class_type;
996
997   if (pointee)
998     {
999       declarator->parameter_pack_p = pointee->parameter_pack_p;
1000       pointee->parameter_pack_p = false;
1001     }
1002   else
1003     declarator->parameter_pack_p = false;
1004
1005   return declarator;
1006 }
1007
1008 /* Make a declarator for the function given by TARGET, with the
1009    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1010    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1011    indicates what exceptions can be thrown.  */
1012
1013 cp_declarator *
1014 make_call_declarator (cp_declarator *target,
1015                       cp_parameter_declarator *parms,
1016                       cp_cv_quals cv_qualifiers,
1017                       tree exception_specification)
1018 {
1019   cp_declarator *declarator;
1020
1021   declarator = make_declarator (cdk_function);
1022   declarator->declarator = target;
1023   declarator->u.function.parameters = parms;
1024   declarator->u.function.qualifiers = cv_qualifiers;
1025   declarator->u.function.exception_specification = exception_specification;
1026   if (target)
1027     {
1028       declarator->parameter_pack_p = target->parameter_pack_p;
1029       target->parameter_pack_p = false;
1030     }
1031   else
1032     declarator->parameter_pack_p = false;
1033
1034   return declarator;
1035 }
1036
1037 /* Make a declarator for an array of BOUNDS elements, each of which is
1038    defined by ELEMENT.  */
1039
1040 cp_declarator *
1041 make_array_declarator (cp_declarator *element, tree bounds)
1042 {
1043   cp_declarator *declarator;
1044
1045   declarator = make_declarator (cdk_array);
1046   declarator->declarator = element;
1047   declarator->u.array.bounds = bounds;
1048   if (element)
1049     {
1050       declarator->parameter_pack_p = element->parameter_pack_p;
1051       element->parameter_pack_p = false;
1052     }
1053   else
1054     declarator->parameter_pack_p = false;
1055
1056   return declarator;
1057 }
1058
1059 /* Determine whether the declarator we've seen so far can be a
1060    parameter pack, when followed by an ellipsis.  */
1061 static bool 
1062 declarator_can_be_parameter_pack (cp_declarator *declarator)
1063 {
1064   /* Search for a declarator name, or any other declarator that goes
1065      after the point where the ellipsis could appear in a parameter
1066      pack. If we find any of these, then this declarator can not be
1067      made into a parameter pack.  */
1068   bool found = false;
1069   while (declarator && !found)
1070     {
1071       switch ((int)declarator->kind)
1072         {
1073         case cdk_id:
1074         case cdk_error:
1075         case cdk_array:
1076         case cdk_ptrmem:
1077           found = true;
1078           break;
1079           
1080         default:
1081           declarator = declarator->declarator;
1082           break;
1083         }
1084     }
1085
1086   return !found;
1087 }
1088
1089 cp_parameter_declarator *no_parameters;
1090
1091 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1092    DECLARATOR and DEFAULT_ARGUMENT.  */
1093
1094 cp_parameter_declarator *
1095 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1096                            cp_declarator *declarator,
1097                            tree default_argument)
1098 {
1099   cp_parameter_declarator *parameter;
1100
1101   parameter = ((cp_parameter_declarator *)
1102                alloc_declarator (sizeof (cp_parameter_declarator)));
1103   parameter->next = NULL;
1104   if (decl_specifiers)
1105     parameter->decl_specifiers = *decl_specifiers;
1106   else
1107     clear_decl_specs (&parameter->decl_specifiers);
1108   parameter->declarator = declarator;
1109   parameter->default_argument = default_argument;
1110   parameter->ellipsis_p = false;
1111
1112   return parameter;
1113 }
1114
1115 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1116
1117 static bool
1118 function_declarator_p (const cp_declarator *declarator)
1119 {
1120   while (declarator)
1121     {
1122       if (declarator->kind == cdk_function
1123           && declarator->declarator->kind == cdk_id)
1124         return true;
1125       if (declarator->kind == cdk_id
1126           || declarator->kind == cdk_error)
1127         return false;
1128       declarator = declarator->declarator;
1129     }
1130   return false;
1131 }
1132  
1133 /* The parser.  */
1134
1135 /* Overview
1136    --------
1137
1138    A cp_parser parses the token stream as specified by the C++
1139    grammar.  Its job is purely parsing, not semantic analysis.  For
1140    example, the parser breaks the token stream into declarators,
1141    expressions, statements, and other similar syntactic constructs.
1142    It does not check that the types of the expressions on either side
1143    of an assignment-statement are compatible, or that a function is
1144    not declared with a parameter of type `void'.
1145
1146    The parser invokes routines elsewhere in the compiler to perform
1147    semantic analysis and to build up the abstract syntax tree for the
1148    code processed.
1149
1150    The parser (and the template instantiation code, which is, in a
1151    way, a close relative of parsing) are the only parts of the
1152    compiler that should be calling push_scope and pop_scope, or
1153    related functions.  The parser (and template instantiation code)
1154    keeps track of what scope is presently active; everything else
1155    should simply honor that.  (The code that generates static
1156    initializers may also need to set the scope, in order to check
1157    access control correctly when emitting the initializers.)
1158
1159    Methodology
1160    -----------
1161
1162    The parser is of the standard recursive-descent variety.  Upcoming
1163    tokens in the token stream are examined in order to determine which
1164    production to use when parsing a non-terminal.  Some C++ constructs
1165    require arbitrary look ahead to disambiguate.  For example, it is
1166    impossible, in the general case, to tell whether a statement is an
1167    expression or declaration without scanning the entire statement.
1168    Therefore, the parser is capable of "parsing tentatively."  When the
1169    parser is not sure what construct comes next, it enters this mode.
1170    Then, while we attempt to parse the construct, the parser queues up
1171    error messages, rather than issuing them immediately, and saves the
1172    tokens it consumes.  If the construct is parsed successfully, the
1173    parser "commits", i.e., it issues any queued error messages and
1174    the tokens that were being preserved are permanently discarded.
1175    If, however, the construct is not parsed successfully, the parser
1176    rolls back its state completely so that it can resume parsing using
1177    a different alternative.
1178
1179    Future Improvements
1180    -------------------
1181
1182    The performance of the parser could probably be improved substantially.
1183    We could often eliminate the need to parse tentatively by looking ahead
1184    a little bit.  In some places, this approach might not entirely eliminate
1185    the need to parse tentatively, but it might still speed up the average
1186    case.  */
1187
1188 /* Flags that are passed to some parsing functions.  These values can
1189    be bitwise-ored together.  */
1190
1191 typedef enum cp_parser_flags
1192 {
1193   /* No flags.  */
1194   CP_PARSER_FLAGS_NONE = 0x0,
1195   /* The construct is optional.  If it is not present, then no error
1196      should be issued.  */
1197   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1198   /* When parsing a type-specifier, do not allow user-defined types.  */
1199   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1200 } cp_parser_flags;
1201
1202 /* The different kinds of declarators we want to parse.  */
1203
1204 typedef enum cp_parser_declarator_kind
1205 {
1206   /* We want an abstract declarator.  */
1207   CP_PARSER_DECLARATOR_ABSTRACT,
1208   /* We want a named declarator.  */
1209   CP_PARSER_DECLARATOR_NAMED,
1210   /* We don't mind, but the name must be an unqualified-id.  */
1211   CP_PARSER_DECLARATOR_EITHER
1212 } cp_parser_declarator_kind;
1213
1214 /* The precedence values used to parse binary expressions.  The minimum value
1215    of PREC must be 1, because zero is reserved to quickly discriminate
1216    binary operators from other tokens.  */
1217
1218 enum cp_parser_prec
1219 {
1220   PREC_NOT_OPERATOR,
1221   PREC_LOGICAL_OR_EXPRESSION,
1222   PREC_LOGICAL_AND_EXPRESSION,
1223   PREC_INCLUSIVE_OR_EXPRESSION,
1224   PREC_EXCLUSIVE_OR_EXPRESSION,
1225   PREC_AND_EXPRESSION,
1226   PREC_EQUALITY_EXPRESSION,
1227   PREC_RELATIONAL_EXPRESSION,
1228   PREC_SHIFT_EXPRESSION,
1229   PREC_ADDITIVE_EXPRESSION,
1230   PREC_MULTIPLICATIVE_EXPRESSION,
1231   PREC_PM_EXPRESSION,
1232   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1233 };
1234
1235 /* A mapping from a token type to a corresponding tree node type, with a
1236    precedence value.  */
1237
1238 typedef struct cp_parser_binary_operations_map_node
1239 {
1240   /* The token type.  */
1241   enum cpp_ttype token_type;
1242   /* The corresponding tree code.  */
1243   enum tree_code tree_type;
1244   /* The precedence of this operator.  */
1245   enum cp_parser_prec prec;
1246 } cp_parser_binary_operations_map_node;
1247
1248 /* The status of a tentative parse.  */
1249
1250 typedef enum cp_parser_status_kind
1251 {
1252   /* No errors have occurred.  */
1253   CP_PARSER_STATUS_KIND_NO_ERROR,
1254   /* An error has occurred.  */
1255   CP_PARSER_STATUS_KIND_ERROR,
1256   /* We are committed to this tentative parse, whether or not an error
1257      has occurred.  */
1258   CP_PARSER_STATUS_KIND_COMMITTED
1259 } cp_parser_status_kind;
1260
1261 typedef struct cp_parser_expression_stack_entry
1262 {
1263   /* Left hand side of the binary operation we are currently
1264      parsing.  */
1265   tree lhs;
1266   /* Original tree code for left hand side, if it was a binary
1267      expression itself (used for -Wparentheses).  */
1268   enum tree_code lhs_type;
1269   /* Tree code for the binary operation we are parsing.  */
1270   enum tree_code tree_type;
1271   /* Precedence of the binary operation we are parsing.  */
1272   int prec;
1273 } cp_parser_expression_stack_entry;
1274
1275 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1276    entries because precedence levels on the stack are monotonically
1277    increasing.  */
1278 typedef struct cp_parser_expression_stack_entry
1279   cp_parser_expression_stack[NUM_PREC_VALUES];
1280
1281 /* Context that is saved and restored when parsing tentatively.  */
1282 typedef struct cp_parser_context GTY (())
1283 {
1284   /* If this is a tentative parsing context, the status of the
1285      tentative parse.  */
1286   enum cp_parser_status_kind status;
1287   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1288      that are looked up in this context must be looked up both in the
1289      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1290      the context of the containing expression.  */
1291   tree object_type;
1292
1293   /* The next parsing context in the stack.  */
1294   struct cp_parser_context *next;
1295 } cp_parser_context;
1296
1297 /* Prototypes.  */
1298
1299 /* Constructors and destructors.  */
1300
1301 static cp_parser_context *cp_parser_context_new
1302   (cp_parser_context *);
1303
1304 /* Class variables.  */
1305
1306 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1307
1308 /* The operator-precedence table used by cp_parser_binary_expression.
1309    Transformed into an associative array (binops_by_token) by
1310    cp_parser_new.  */
1311
1312 static const cp_parser_binary_operations_map_node binops[] = {
1313   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1314   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1315
1316   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1317   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1318   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319
1320   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1321   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1322
1323   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1324   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1325
1326   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1327   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1328   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1329   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1330
1331   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1332   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1333
1334   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1335
1336   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1337
1338   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1339
1340   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1341
1342   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1343 };
1344
1345 /* The same as binops, but initialized by cp_parser_new so that
1346    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1347    for speed.  */
1348 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1349
1350 /* Constructors and destructors.  */
1351
1352 /* Construct a new context.  The context below this one on the stack
1353    is given by NEXT.  */
1354
1355 static cp_parser_context *
1356 cp_parser_context_new (cp_parser_context* next)
1357 {
1358   cp_parser_context *context;
1359
1360   /* Allocate the storage.  */
1361   if (cp_parser_context_free_list != NULL)
1362     {
1363       /* Pull the first entry from the free list.  */
1364       context = cp_parser_context_free_list;
1365       cp_parser_context_free_list = context->next;
1366       memset (context, 0, sizeof (*context));
1367     }
1368   else
1369     context = GGC_CNEW (cp_parser_context);
1370
1371   /* No errors have occurred yet in this context.  */
1372   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1373   /* If this is not the bottomost context, copy information that we
1374      need from the previous context.  */
1375   if (next)
1376     {
1377       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1378          expression, then we are parsing one in this context, too.  */
1379       context->object_type = next->object_type;
1380       /* Thread the stack.  */
1381       context->next = next;
1382     }
1383
1384   return context;
1385 }
1386
1387 /* The cp_parser structure represents the C++ parser.  */
1388
1389 typedef struct cp_parser GTY(())
1390 {
1391   /* The lexer from which we are obtaining tokens.  */
1392   cp_lexer *lexer;
1393
1394   /* The scope in which names should be looked up.  If NULL_TREE, then
1395      we look up names in the scope that is currently open in the
1396      source program.  If non-NULL, this is either a TYPE or
1397      NAMESPACE_DECL for the scope in which we should look.  It can
1398      also be ERROR_MARK, when we've parsed a bogus scope.
1399
1400      This value is not cleared automatically after a name is looked
1401      up, so we must be careful to clear it before starting a new look
1402      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1403      will look up `Z' in the scope of `X', rather than the current
1404      scope.)  Unfortunately, it is difficult to tell when name lookup
1405      is complete, because we sometimes peek at a token, look it up,
1406      and then decide not to consume it.   */
1407   tree scope;
1408
1409   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1410      last lookup took place.  OBJECT_SCOPE is used if an expression
1411      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1412      respectively.  QUALIFYING_SCOPE is used for an expression of the
1413      form "X::Y"; it refers to X.  */
1414   tree object_scope;
1415   tree qualifying_scope;
1416
1417   /* A stack of parsing contexts.  All but the bottom entry on the
1418      stack will be tentative contexts.
1419
1420      We parse tentatively in order to determine which construct is in
1421      use in some situations.  For example, in order to determine
1422      whether a statement is an expression-statement or a
1423      declaration-statement we parse it tentatively as a
1424      declaration-statement.  If that fails, we then reparse the same
1425      token stream as an expression-statement.  */
1426   cp_parser_context *context;
1427
1428   /* True if we are parsing GNU C++.  If this flag is not set, then
1429      GNU extensions are not recognized.  */
1430   bool allow_gnu_extensions_p;
1431
1432   /* TRUE if the `>' token should be interpreted as the greater-than
1433      operator.  FALSE if it is the end of a template-id or
1434      template-parameter-list. In C++0x mode, this flag also applies to
1435      `>>' tokens, which are viewed as two consecutive `>' tokens when
1436      this flag is FALSE.  */
1437   bool greater_than_is_operator_p;
1438
1439   /* TRUE if default arguments are allowed within a parameter list
1440      that starts at this point. FALSE if only a gnu extension makes
1441      them permissible.  */
1442   bool default_arg_ok_p;
1443
1444   /* TRUE if we are parsing an integral constant-expression.  See
1445      [expr.const] for a precise definition.  */
1446   bool integral_constant_expression_p;
1447
1448   /* TRUE if we are parsing an integral constant-expression -- but a
1449      non-constant expression should be permitted as well.  This flag
1450      is used when parsing an array bound so that GNU variable-length
1451      arrays are tolerated.  */
1452   bool allow_non_integral_constant_expression_p;
1453
1454   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1455      been seen that makes the expression non-constant.  */
1456   bool non_integral_constant_expression_p;
1457
1458   /* TRUE if local variable names and `this' are forbidden in the
1459      current context.  */
1460   bool local_variables_forbidden_p;
1461
1462   /* TRUE if the declaration we are parsing is part of a
1463      linkage-specification of the form `extern string-literal
1464      declaration'.  */
1465   bool in_unbraced_linkage_specification_p;
1466
1467   /* TRUE if we are presently parsing a declarator, after the
1468      direct-declarator.  */
1469   bool in_declarator_p;
1470
1471   /* TRUE if we are presently parsing a template-argument-list.  */
1472   bool in_template_argument_list_p;
1473
1474   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1475      to IN_OMP_BLOCK if parsing OpenMP structured block and
1476      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1477      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1478      iteration-statement, OpenMP block or loop within that switch.  */
1479 #define IN_SWITCH_STMT          1
1480 #define IN_ITERATION_STMT       2
1481 #define IN_OMP_BLOCK            4
1482 #define IN_OMP_FOR              8
1483 #define IN_IF_STMT             16
1484   unsigned char in_statement;
1485
1486   /* TRUE if we are presently parsing the body of a switch statement.
1487      Note that this doesn't quite overlap with in_statement above.
1488      The difference relates to giving the right sets of error messages:
1489      "case not in switch" vs "break statement used with OpenMP...".  */
1490   bool in_switch_statement_p;
1491
1492   /* TRUE if we are parsing a type-id in an expression context.  In
1493      such a situation, both "type (expr)" and "type (type)" are valid
1494      alternatives.  */
1495   bool in_type_id_in_expr_p;
1496
1497   /* TRUE if we are currently in a header file where declarations are
1498      implicitly extern "C".  */
1499   bool implicit_extern_c;
1500
1501   /* TRUE if strings in expressions should be translated to the execution
1502      character set.  */
1503   bool translate_strings_p;
1504
1505   /* TRUE if we are presently parsing the body of a function, but not
1506      a local class.  */
1507   bool in_function_body;
1508
1509   /* If non-NULL, then we are parsing a construct where new type
1510      definitions are not permitted.  The string stored here will be
1511      issued as an error message if a type is defined.  */
1512   const char *type_definition_forbidden_message;
1513
1514   /* A list of lists. The outer list is a stack, used for member
1515      functions of local classes. At each level there are two sub-list,
1516      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1517      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1518      TREE_VALUE's. The functions are chained in reverse declaration
1519      order.
1520
1521      The TREE_PURPOSE sublist contains those functions with default
1522      arguments that need post processing, and the TREE_VALUE sublist
1523      contains those functions with definitions that need post
1524      processing.
1525
1526      These lists can only be processed once the outermost class being
1527      defined is complete.  */
1528   tree unparsed_functions_queues;
1529
1530   /* The number of classes whose definitions are currently in
1531      progress.  */
1532   unsigned num_classes_being_defined;
1533
1534   /* The number of template parameter lists that apply directly to the
1535      current declaration.  */
1536   unsigned num_template_parameter_lists;
1537 } cp_parser;
1538
1539 /* Prototypes.  */
1540
1541 /* Constructors and destructors.  */
1542
1543 static cp_parser *cp_parser_new
1544   (void);
1545
1546 /* Routines to parse various constructs.
1547
1548    Those that return `tree' will return the error_mark_node (rather
1549    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1550    Sometimes, they will return an ordinary node if error-recovery was
1551    attempted, even though a parse error occurred.  So, to check
1552    whether or not a parse error occurred, you should always use
1553    cp_parser_error_occurred.  If the construct is optional (indicated
1554    either by an `_opt' in the name of the function that does the
1555    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1556    the construct is not present.  */
1557
1558 /* Lexical conventions [gram.lex]  */
1559
1560 static tree cp_parser_identifier
1561   (cp_parser *);
1562 static tree cp_parser_string_literal
1563   (cp_parser *, bool, bool);
1564
1565 /* Basic concepts [gram.basic]  */
1566
1567 static bool cp_parser_translation_unit
1568   (cp_parser *);
1569
1570 /* Expressions [gram.expr]  */
1571
1572 static tree cp_parser_primary_expression
1573   (cp_parser *, bool, bool, bool, cp_id_kind *);
1574 static tree cp_parser_id_expression
1575   (cp_parser *, bool, bool, bool *, bool, bool);
1576 static tree cp_parser_unqualified_id
1577   (cp_parser *, bool, bool, bool, bool);
1578 static tree cp_parser_nested_name_specifier_opt
1579   (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_class_or_namespace_name
1583   (cp_parser *, bool, bool, bool, bool, bool);
1584 static tree cp_parser_postfix_expression
1585   (cp_parser *, bool, bool);
1586 static tree cp_parser_postfix_open_square_expression
1587   (cp_parser *, tree, bool);
1588 static tree cp_parser_postfix_dot_deref_expression
1589   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1590 static tree cp_parser_parenthesized_expression_list
1591   (cp_parser *, bool, bool, bool, bool *);
1592 static void cp_parser_pseudo_destructor_name
1593   (cp_parser *, tree *, tree *);
1594 static tree cp_parser_unary_expression
1595   (cp_parser *, bool, bool);
1596 static enum tree_code cp_parser_unary_operator
1597   (cp_token *);
1598 static tree cp_parser_new_expression
1599   (cp_parser *);
1600 static tree cp_parser_new_placement
1601   (cp_parser *);
1602 static tree cp_parser_new_type_id
1603   (cp_parser *, tree *);
1604 static cp_declarator *cp_parser_new_declarator_opt
1605   (cp_parser *);
1606 static cp_declarator *cp_parser_direct_new_declarator
1607   (cp_parser *);
1608 static tree cp_parser_new_initializer
1609   (cp_parser *);
1610 static tree cp_parser_delete_expression
1611   (cp_parser *);
1612 static tree cp_parser_cast_expression
1613   (cp_parser *, bool, bool);
1614 static tree cp_parser_binary_expression
1615   (cp_parser *, bool);
1616 static tree cp_parser_question_colon_clause
1617   (cp_parser *, tree);
1618 static tree cp_parser_assignment_expression
1619   (cp_parser *, bool);
1620 static enum tree_code cp_parser_assignment_operator_opt
1621   (cp_parser *);
1622 static tree cp_parser_expression
1623   (cp_parser *, bool);
1624 static tree cp_parser_constant_expression
1625   (cp_parser *, bool, bool *);
1626 static tree cp_parser_builtin_offsetof
1627   (cp_parser *);
1628
1629 /* Statements [gram.stmt.stmt]  */
1630
1631 static void cp_parser_statement
1632   (cp_parser *, tree, bool, bool *);
1633 static void cp_parser_label_for_labeled_statement
1634   (cp_parser *);
1635 static tree cp_parser_expression_statement
1636   (cp_parser *, tree);
1637 static tree cp_parser_compound_statement
1638   (cp_parser *, tree, bool);
1639 static void cp_parser_statement_seq_opt
1640   (cp_parser *, tree);
1641 static tree cp_parser_selection_statement
1642   (cp_parser *, bool *);
1643 static tree cp_parser_condition
1644   (cp_parser *);
1645 static tree cp_parser_iteration_statement
1646   (cp_parser *);
1647 static void cp_parser_for_init_statement
1648   (cp_parser *);
1649 static tree cp_parser_jump_statement
1650   (cp_parser *);
1651 static void cp_parser_declaration_statement
1652   (cp_parser *);
1653
1654 static tree cp_parser_implicitly_scoped_statement
1655   (cp_parser *, bool *);
1656 static void cp_parser_already_scoped_statement
1657   (cp_parser *);
1658
1659 /* Declarations [gram.dcl.dcl] */
1660
1661 static void cp_parser_declaration_seq_opt
1662   (cp_parser *);
1663 static void cp_parser_declaration
1664   (cp_parser *);
1665 static void cp_parser_block_declaration
1666   (cp_parser *, bool);
1667 static void cp_parser_simple_declaration
1668   (cp_parser *, bool);
1669 static void cp_parser_decl_specifier_seq
1670   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1671 static tree cp_parser_storage_class_specifier_opt
1672   (cp_parser *);
1673 static tree cp_parser_function_specifier_opt
1674   (cp_parser *, cp_decl_specifier_seq *);
1675 static tree cp_parser_type_specifier
1676   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1677    int *, bool *);
1678 static tree cp_parser_simple_type_specifier
1679   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1680 static tree cp_parser_type_name
1681   (cp_parser *);
1682 static tree cp_parser_elaborated_type_specifier
1683   (cp_parser *, bool, bool);
1684 static tree cp_parser_enum_specifier
1685   (cp_parser *);
1686 static void cp_parser_enumerator_list
1687   (cp_parser *, tree);
1688 static void cp_parser_enumerator_definition
1689   (cp_parser *, tree);
1690 static tree cp_parser_namespace_name
1691   (cp_parser *);
1692 static void cp_parser_namespace_definition
1693   (cp_parser *);
1694 static void cp_parser_namespace_body
1695   (cp_parser *);
1696 static tree cp_parser_qualified_namespace_specifier
1697   (cp_parser *);
1698 static void cp_parser_namespace_alias_definition
1699   (cp_parser *);
1700 static bool cp_parser_using_declaration
1701   (cp_parser *, bool);
1702 static void cp_parser_using_directive
1703   (cp_parser *);
1704 static void cp_parser_asm_definition
1705   (cp_parser *);
1706 static void cp_parser_linkage_specification
1707   (cp_parser *);
1708 static void cp_parser_static_assert
1709   (cp_parser *, bool);
1710
1711 /* Declarators [gram.dcl.decl] */
1712
1713 static tree cp_parser_init_declarator
1714   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1715 static cp_declarator *cp_parser_declarator
1716   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1717 static cp_declarator *cp_parser_direct_declarator
1718   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1719 static enum tree_code cp_parser_ptr_operator
1720   (cp_parser *, tree *, cp_cv_quals *);
1721 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1722   (cp_parser *);
1723 static tree cp_parser_declarator_id
1724   (cp_parser *, bool);
1725 static tree cp_parser_type_id
1726   (cp_parser *);
1727 static void cp_parser_type_specifier_seq
1728   (cp_parser *, bool, cp_decl_specifier_seq *);
1729 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1730   (cp_parser *);
1731 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1732   (cp_parser *, bool *);
1733 static cp_parameter_declarator *cp_parser_parameter_declaration
1734   (cp_parser *, bool, bool *);
1735 static void cp_parser_function_body
1736   (cp_parser *);
1737 static tree cp_parser_initializer
1738   (cp_parser *, bool *, bool *);
1739 static tree cp_parser_initializer_clause
1740   (cp_parser *, bool *);
1741 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1742   (cp_parser *, bool *);
1743
1744 static bool cp_parser_ctor_initializer_opt_and_function_body
1745   (cp_parser *);
1746
1747 /* Classes [gram.class] */
1748
1749 static tree cp_parser_class_name
1750   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1751 static tree cp_parser_class_specifier
1752   (cp_parser *);
1753 static tree cp_parser_class_head
1754   (cp_parser *, bool *, tree *, tree *);
1755 static enum tag_types cp_parser_class_key
1756   (cp_parser *);
1757 static void cp_parser_member_specification_opt
1758   (cp_parser *);
1759 static void cp_parser_member_declaration
1760   (cp_parser *);
1761 static tree cp_parser_pure_specifier
1762   (cp_parser *);
1763 static tree cp_parser_constant_initializer
1764   (cp_parser *);
1765
1766 /* Derived classes [gram.class.derived] */
1767
1768 static tree cp_parser_base_clause
1769   (cp_parser *);
1770 static tree cp_parser_base_specifier
1771   (cp_parser *);
1772
1773 /* Special member functions [gram.special] */
1774
1775 static tree cp_parser_conversion_function_id
1776   (cp_parser *);
1777 static tree cp_parser_conversion_type_id
1778   (cp_parser *);
1779 static cp_declarator *cp_parser_conversion_declarator_opt
1780   (cp_parser *);
1781 static bool cp_parser_ctor_initializer_opt
1782   (cp_parser *);
1783 static void cp_parser_mem_initializer_list
1784   (cp_parser *);
1785 static tree cp_parser_mem_initializer
1786   (cp_parser *);
1787 static tree cp_parser_mem_initializer_id
1788   (cp_parser *);
1789
1790 /* Overloading [gram.over] */
1791
1792 static tree cp_parser_operator_function_id
1793   (cp_parser *);
1794 static tree cp_parser_operator
1795   (cp_parser *);
1796
1797 /* Templates [gram.temp] */
1798
1799 static void cp_parser_template_declaration
1800   (cp_parser *, bool);
1801 static tree cp_parser_template_parameter_list
1802   (cp_parser *);
1803 static tree cp_parser_template_parameter
1804   (cp_parser *, bool *, bool *);
1805 static tree cp_parser_type_parameter
1806   (cp_parser *, bool *);
1807 static tree cp_parser_template_id
1808   (cp_parser *, bool, bool, bool);
1809 static tree cp_parser_template_name
1810   (cp_parser *, bool, bool, bool, bool *);
1811 static tree cp_parser_template_argument_list
1812   (cp_parser *);
1813 static tree cp_parser_template_argument
1814   (cp_parser *);
1815 static void cp_parser_explicit_instantiation
1816   (cp_parser *);
1817 static void cp_parser_explicit_specialization
1818   (cp_parser *);
1819
1820 /* Exception handling [gram.exception] */
1821
1822 static tree cp_parser_try_block
1823   (cp_parser *);
1824 static bool cp_parser_function_try_block
1825   (cp_parser *);
1826 static void cp_parser_handler_seq
1827   (cp_parser *);
1828 static void cp_parser_handler
1829   (cp_parser *);
1830 static tree cp_parser_exception_declaration
1831   (cp_parser *);
1832 static tree cp_parser_throw_expression
1833   (cp_parser *);
1834 static tree cp_parser_exception_specification_opt
1835   (cp_parser *);
1836 static tree cp_parser_type_id_list
1837   (cp_parser *);
1838
1839 /* GNU Extensions */
1840
1841 static tree cp_parser_asm_specification_opt
1842   (cp_parser *);
1843 static tree cp_parser_asm_operand_list
1844   (cp_parser *);
1845 static tree cp_parser_asm_clobber_list
1846   (cp_parser *);
1847 static tree cp_parser_attributes_opt
1848   (cp_parser *);
1849 static tree cp_parser_attribute_list
1850   (cp_parser *);
1851 static bool cp_parser_extension_opt
1852   (cp_parser *, int *);
1853 static void cp_parser_label_declaration
1854   (cp_parser *);
1855
1856 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1857 static bool cp_parser_pragma
1858   (cp_parser *, enum pragma_context);
1859
1860 /* Objective-C++ Productions */
1861
1862 static tree cp_parser_objc_message_receiver
1863   (cp_parser *);
1864 static tree cp_parser_objc_message_args
1865   (cp_parser *);
1866 static tree cp_parser_objc_message_expression
1867   (cp_parser *);
1868 static tree cp_parser_objc_encode_expression
1869   (cp_parser *);
1870 static tree cp_parser_objc_defs_expression
1871   (cp_parser *);
1872 static tree cp_parser_objc_protocol_expression
1873   (cp_parser *);
1874 static tree cp_parser_objc_selector_expression
1875   (cp_parser *);
1876 static tree cp_parser_objc_expression
1877   (cp_parser *);
1878 static bool cp_parser_objc_selector_p
1879   (enum cpp_ttype);
1880 static tree cp_parser_objc_selector
1881   (cp_parser *);
1882 static tree cp_parser_objc_protocol_refs_opt
1883   (cp_parser *);
1884 static void cp_parser_objc_declaration
1885   (cp_parser *);
1886 static tree cp_parser_objc_statement
1887   (cp_parser *);
1888
1889 /* Utility Routines */
1890
1891 static tree cp_parser_lookup_name
1892   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1893 static tree cp_parser_lookup_name_simple
1894   (cp_parser *, tree);
1895 static tree cp_parser_maybe_treat_template_as_class
1896   (tree, bool);
1897 static bool cp_parser_check_declarator_template_parameters
1898   (cp_parser *, cp_declarator *);
1899 static bool cp_parser_check_template_parameters
1900   (cp_parser *, unsigned);
1901 static tree cp_parser_simple_cast_expression
1902   (cp_parser *);
1903 static tree cp_parser_global_scope_opt
1904   (cp_parser *, bool);
1905 static bool cp_parser_constructor_declarator_p
1906   (cp_parser *, bool);
1907 static tree cp_parser_function_definition_from_specifiers_and_declarator
1908   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1909 static tree cp_parser_function_definition_after_declarator
1910   (cp_parser *, bool);
1911 static void cp_parser_template_declaration_after_export
1912   (cp_parser *, bool);
1913 static void cp_parser_perform_template_parameter_access_checks
1914   (VEC (deferred_access_check,gc)*);
1915 static tree cp_parser_single_declaration
1916   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1917 static tree cp_parser_functional_cast
1918   (cp_parser *, tree);
1919 static tree cp_parser_save_member_function_body
1920   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1921 static tree cp_parser_enclosed_template_argument_list
1922   (cp_parser *);
1923 static void cp_parser_save_default_args
1924   (cp_parser *, tree);
1925 static void cp_parser_late_parsing_for_member
1926   (cp_parser *, tree);
1927 static void cp_parser_late_parsing_default_args
1928   (cp_parser *, tree);
1929 static tree cp_parser_sizeof_operand
1930   (cp_parser *, enum rid);
1931 static tree cp_parser_trait_expr
1932   (cp_parser *, enum rid);
1933 static bool cp_parser_declares_only_class_p
1934   (cp_parser *);
1935 static void cp_parser_set_storage_class
1936   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1937 static void cp_parser_set_decl_spec_type
1938   (cp_decl_specifier_seq *, tree, bool);
1939 static bool cp_parser_friend_p
1940   (const cp_decl_specifier_seq *);
1941 static cp_token *cp_parser_require
1942   (cp_parser *, enum cpp_ttype, const char *);
1943 static cp_token *cp_parser_require_keyword
1944   (cp_parser *, enum rid, const char *);
1945 static bool cp_parser_token_starts_function_definition_p
1946   (cp_token *);
1947 static bool cp_parser_next_token_starts_class_definition_p
1948   (cp_parser *);
1949 static bool cp_parser_next_token_ends_template_argument_p
1950   (cp_parser *);
1951 static bool cp_parser_nth_token_starts_template_argument_list_p
1952   (cp_parser *, size_t);
1953 static enum tag_types cp_parser_token_is_class_key
1954   (cp_token *);
1955 static void cp_parser_check_class_key
1956   (enum tag_types, tree type);
1957 static void cp_parser_check_access_in_redeclaration
1958   (tree type);
1959 static bool cp_parser_optional_template_keyword
1960   (cp_parser *);
1961 static void cp_parser_pre_parsed_nested_name_specifier
1962   (cp_parser *);
1963 static void cp_parser_cache_group
1964   (cp_parser *, enum cpp_ttype, unsigned);
1965 static void cp_parser_parse_tentatively
1966   (cp_parser *);
1967 static void cp_parser_commit_to_tentative_parse
1968   (cp_parser *);
1969 static void cp_parser_abort_tentative_parse
1970   (cp_parser *);
1971 static bool cp_parser_parse_definitely
1972   (cp_parser *);
1973 static inline bool cp_parser_parsing_tentatively
1974   (cp_parser *);
1975 static bool cp_parser_uncommitted_to_tentative_parse_p
1976   (cp_parser *);
1977 static void cp_parser_error
1978   (cp_parser *, const char *);
1979 static void cp_parser_name_lookup_error
1980   (cp_parser *, tree, tree, const char *);
1981 static bool cp_parser_simulate_error
1982   (cp_parser *);
1983 static bool cp_parser_check_type_definition
1984   (cp_parser *);
1985 static void cp_parser_check_for_definition_in_return_type
1986   (cp_declarator *, tree);
1987 static void cp_parser_check_for_invalid_template_id
1988   (cp_parser *, tree);
1989 static bool cp_parser_non_integral_constant_expression
1990   (cp_parser *, const char *);
1991 static void cp_parser_diagnose_invalid_type_name
1992   (cp_parser *, tree, tree);
1993 static bool cp_parser_parse_and_diagnose_invalid_type_name
1994   (cp_parser *);
1995 static int cp_parser_skip_to_closing_parenthesis
1996   (cp_parser *, bool, bool, bool);
1997 static void cp_parser_skip_to_end_of_statement
1998   (cp_parser *);
1999 static void cp_parser_consume_semicolon_at_end_of_statement
2000   (cp_parser *);
2001 static void cp_parser_skip_to_end_of_block_or_statement
2002   (cp_parser *);
2003 static bool cp_parser_skip_to_closing_brace
2004   (cp_parser *);
2005 static void cp_parser_skip_to_end_of_template_parameter_list
2006   (cp_parser *);
2007 static void cp_parser_skip_to_pragma_eol
2008   (cp_parser*, cp_token *);
2009 static bool cp_parser_error_occurred
2010   (cp_parser *);
2011 static bool cp_parser_allow_gnu_extensions_p
2012   (cp_parser *);
2013 static bool cp_parser_is_string_literal
2014   (cp_token *);
2015 static bool cp_parser_is_keyword
2016   (cp_token *, enum rid);
2017 static tree cp_parser_make_typename_type
2018   (cp_parser *, tree, tree);
2019 static cp_declarator * cp_parser_make_indirect_declarator
2020   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2021
2022 /* Returns nonzero if we are parsing tentatively.  */
2023
2024 static inline bool
2025 cp_parser_parsing_tentatively (cp_parser* parser)
2026 {
2027   return parser->context->next != NULL;
2028 }
2029
2030 /* Returns nonzero if TOKEN is a string literal.  */
2031
2032 static bool
2033 cp_parser_is_string_literal (cp_token* token)
2034 {
2035   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2036 }
2037
2038 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2039
2040 static bool
2041 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2042 {
2043   return token->keyword == keyword;
2044 }
2045
2046 /* If not parsing tentatively, issue a diagnostic of the form
2047       FILE:LINE: MESSAGE before TOKEN
2048    where TOKEN is the next token in the input stream.  MESSAGE
2049    (specified by the caller) is usually of the form "expected
2050    OTHER-TOKEN".  */
2051
2052 static void
2053 cp_parser_error (cp_parser* parser, const char* message)
2054 {
2055   if (!cp_parser_simulate_error (parser))
2056     {
2057       cp_token *token = cp_lexer_peek_token (parser->lexer);
2058       /* This diagnostic makes more sense if it is tagged to the line
2059          of the token we just peeked at.  */
2060       cp_lexer_set_source_position_from_token (token);
2061
2062       if (token->type == CPP_PRAGMA)
2063         {
2064           error ("%<#pragma%> is not allowed here");
2065           cp_parser_skip_to_pragma_eol (parser, token);
2066           return;
2067         }
2068
2069       c_parse_error (message,
2070                      /* Because c_parser_error does not understand
2071                         CPP_KEYWORD, keywords are treated like
2072                         identifiers.  */
2073                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2074                      token->u.value);
2075     }
2076 }
2077
2078 /* Issue an error about name-lookup failing.  NAME is the
2079    IDENTIFIER_NODE DECL is the result of
2080    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2081    the thing that we hoped to find.  */
2082
2083 static void
2084 cp_parser_name_lookup_error (cp_parser* parser,
2085                              tree name,
2086                              tree decl,
2087                              const char* desired)
2088 {
2089   /* If name lookup completely failed, tell the user that NAME was not
2090      declared.  */
2091   if (decl == error_mark_node)
2092     {
2093       if (parser->scope && parser->scope != global_namespace)
2094         error ("%<%E::%E%> has not been declared",
2095                parser->scope, name);
2096       else if (parser->scope == global_namespace)
2097         error ("%<::%E%> has not been declared", name);
2098       else if (parser->object_scope
2099                && !CLASS_TYPE_P (parser->object_scope))
2100         error ("request for member %qE in non-class type %qT",
2101                name, parser->object_scope);
2102       else if (parser->object_scope)
2103         error ("%<%T::%E%> has not been declared",
2104                parser->object_scope, name);
2105       else
2106         error ("%qE has not been declared", name);
2107     }
2108   else if (parser->scope && parser->scope != global_namespace)
2109     error ("%<%E::%E%> %s", parser->scope, name, desired);
2110   else if (parser->scope == global_namespace)
2111     error ("%<::%E%> %s", name, desired);
2112   else
2113     error ("%qE %s", name, desired);
2114 }
2115
2116 /* If we are parsing tentatively, remember that an error has occurred
2117    during this tentative parse.  Returns true if the error was
2118    simulated; false if a message should be issued by the caller.  */
2119
2120 static bool
2121 cp_parser_simulate_error (cp_parser* parser)
2122 {
2123   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2124     {
2125       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2126       return true;
2127     }
2128   return false;
2129 }
2130
2131 /* Check for repeated decl-specifiers.  */
2132
2133 static void
2134 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2135 {
2136   cp_decl_spec ds;
2137
2138   for (ds = ds_first; ds != ds_last; ++ds)
2139     {
2140       unsigned count = decl_specs->specs[(int)ds];
2141       if (count < 2)
2142         continue;
2143       /* The "long" specifier is a special case because of "long long".  */
2144       if (ds == ds_long)
2145         {
2146           if (count > 2)
2147             error ("%<long long long%> is too long for GCC");
2148           else if (pedantic && !in_system_header && warn_long_long)
2149             pedwarn ("ISO C++ does not support %<long long%>");
2150         }
2151       else if (count > 1)
2152         {
2153           static const char *const decl_spec_names[] = {
2154             "signed",
2155             "unsigned",
2156             "short",
2157             "long",
2158             "const",
2159             "volatile",
2160             "restrict",
2161             "inline",
2162             "virtual",
2163             "explicit",
2164             "friend",
2165             "typedef",
2166             "__complex",
2167             "__thread"
2168           };
2169           error ("duplicate %qs", decl_spec_names[(int)ds]);
2170         }
2171     }
2172 }
2173
2174 /* This function is called when a type is defined.  If type
2175    definitions are forbidden at this point, an error message is
2176    issued.  */
2177
2178 static bool
2179 cp_parser_check_type_definition (cp_parser* parser)
2180 {
2181   /* If types are forbidden here, issue a message.  */
2182   if (parser->type_definition_forbidden_message)
2183     {
2184       /* Use `%s' to print the string in case there are any escape
2185          characters in the message.  */
2186       error ("%s", parser->type_definition_forbidden_message);
2187       return false;
2188     }
2189   return true;
2190 }
2191
2192 /* This function is called when the DECLARATOR is processed.  The TYPE
2193    was a type defined in the decl-specifiers.  If it is invalid to
2194    define a type in the decl-specifiers for DECLARATOR, an error is
2195    issued.  */
2196
2197 static void
2198 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2199                                                tree type)
2200 {
2201   /* [dcl.fct] forbids type definitions in return types.
2202      Unfortunately, it's not easy to know whether or not we are
2203      processing a return type until after the fact.  */
2204   while (declarator
2205          && (declarator->kind == cdk_pointer
2206              || declarator->kind == cdk_reference
2207              || declarator->kind == cdk_ptrmem))
2208     declarator = declarator->declarator;
2209   if (declarator
2210       && declarator->kind == cdk_function)
2211     {
2212       error ("new types may not be defined in a return type");
2213       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2214               type);
2215     }
2216 }
2217
2218 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2219    "<" in any valid C++ program.  If the next token is indeed "<",
2220    issue a message warning the user about what appears to be an
2221    invalid attempt to form a template-id.  */
2222
2223 static void
2224 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2225                                          tree type)
2226 {
2227   cp_token_position start = 0;
2228
2229   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2230     {
2231       if (TYPE_P (type))
2232         error ("%qT is not a template", type);
2233       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2234         error ("%qE is not a template", type);
2235       else
2236         error ("invalid template-id");
2237       /* Remember the location of the invalid "<".  */
2238       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2239         start = cp_lexer_token_position (parser->lexer, true);
2240       /* Consume the "<".  */
2241       cp_lexer_consume_token (parser->lexer);
2242       /* Parse the template arguments.  */
2243       cp_parser_enclosed_template_argument_list (parser);
2244       /* Permanently remove the invalid template arguments so that
2245          this error message is not issued again.  */
2246       if (start)
2247         cp_lexer_purge_tokens_after (parser->lexer, start);
2248     }
2249 }
2250
2251 /* If parsing an integral constant-expression, issue an error message
2252    about the fact that THING appeared and return true.  Otherwise,
2253    return false.  In either case, set
2254    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2255
2256 static bool
2257 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2258                                             const char *thing)
2259 {
2260   parser->non_integral_constant_expression_p = true;
2261   if (parser->integral_constant_expression_p)
2262     {
2263       if (!parser->allow_non_integral_constant_expression_p)
2264         {
2265           error ("%s cannot appear in a constant-expression", thing);
2266           return true;
2267         }
2268     }
2269   return false;
2270 }
2271
2272 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2273    qualifying scope (or NULL, if none) for ID.  This function commits
2274    to the current active tentative parse, if any.  (Otherwise, the
2275    problematic construct might be encountered again later, resulting
2276    in duplicate error messages.)  */
2277
2278 static void
2279 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2280 {
2281   tree decl, old_scope;
2282   /* Try to lookup the identifier.  */
2283   old_scope = parser->scope;
2284   parser->scope = scope;
2285   decl = cp_parser_lookup_name_simple (parser, id);
2286   parser->scope = old_scope;
2287   /* If the lookup found a template-name, it means that the user forgot
2288   to specify an argument list. Emit a useful error message.  */
2289   if (TREE_CODE (decl) == TEMPLATE_DECL)
2290     error ("invalid use of template-name %qE without an argument list", decl);
2291   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2292     error ("invalid use of destructor %qD as a type", id);
2293   else if (TREE_CODE (decl) == TYPE_DECL)
2294     /* Something like 'unsigned A a;'  */
2295     error ("invalid combination of multiple type-specifiers");
2296   else if (!parser->scope)
2297     {
2298       /* Issue an error message.  */
2299       error ("%qE does not name a type", id);
2300       /* If we're in a template class, it's possible that the user was
2301          referring to a type from a base class.  For example:
2302
2303            template <typename T> struct A { typedef T X; };
2304            template <typename T> struct B : public A<T> { X x; };
2305
2306          The user should have said "typename A<T>::X".  */
2307       if (processing_template_decl && current_class_type
2308           && TYPE_BINFO (current_class_type))
2309         {
2310           tree b;
2311
2312           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2313                b;
2314                b = TREE_CHAIN (b))
2315             {
2316               tree base_type = BINFO_TYPE (b);
2317               if (CLASS_TYPE_P (base_type)
2318                   && dependent_type_p (base_type))
2319                 {
2320                   tree field;
2321                   /* Go from a particular instantiation of the
2322                      template (which will have an empty TYPE_FIELDs),
2323                      to the main version.  */
2324                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2325                   for (field = TYPE_FIELDS (base_type);
2326                        field;
2327                        field = TREE_CHAIN (field))
2328                     if (TREE_CODE (field) == TYPE_DECL
2329                         && DECL_NAME (field) == id)
2330                       {
2331                         inform ("(perhaps %<typename %T::%E%> was intended)",
2332                                 BINFO_TYPE (b), id);
2333                         break;
2334                       }
2335                   if (field)
2336                     break;
2337                 }
2338             }
2339         }
2340     }
2341   /* Here we diagnose qualified-ids where the scope is actually correct,
2342      but the identifier does not resolve to a valid type name.  */
2343   else if (parser->scope != error_mark_node)
2344     {
2345       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2346         error ("%qE in namespace %qE does not name a type",
2347                id, parser->scope);
2348       else if (TYPE_P (parser->scope))
2349         error ("%qE in class %qT does not name a type", id, parser->scope);
2350       else
2351         gcc_unreachable ();
2352     }
2353   cp_parser_commit_to_tentative_parse (parser);
2354 }
2355
2356 /* Check for a common situation where a type-name should be present,
2357    but is not, and issue a sensible error message.  Returns true if an
2358    invalid type-name was detected.
2359
2360    The situation handled by this function are variable declarations of the
2361    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2362    Usually, `ID' should name a type, but if we got here it means that it
2363    does not. We try to emit the best possible error message depending on
2364    how exactly the id-expression looks like.  */
2365
2366 static bool
2367 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2368 {
2369   tree id;
2370
2371   cp_parser_parse_tentatively (parser);
2372   id = cp_parser_id_expression (parser,
2373                                 /*template_keyword_p=*/false,
2374                                 /*check_dependency_p=*/true,
2375                                 /*template_p=*/NULL,
2376                                 /*declarator_p=*/true,
2377                                 /*optional_p=*/false);
2378   /* After the id-expression, there should be a plain identifier,
2379      otherwise this is not a simple variable declaration. Also, if
2380      the scope is dependent, we cannot do much.  */
2381   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2382       || (parser->scope && TYPE_P (parser->scope)
2383           && dependent_type_p (parser->scope))
2384       || TREE_CODE (id) == TYPE_DECL)
2385     {
2386       cp_parser_abort_tentative_parse (parser);
2387       return false;
2388     }
2389   if (!cp_parser_parse_definitely (parser))
2390     return false;
2391
2392   /* Emit a diagnostic for the invalid type.  */
2393   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2394   /* Skip to the end of the declaration; there's no point in
2395      trying to process it.  */
2396   cp_parser_skip_to_end_of_block_or_statement (parser);
2397   return true;
2398 }
2399
2400 /* Consume tokens up to, and including, the next non-nested closing `)'.
2401    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2402    are doing error recovery. Returns -1 if OR_COMMA is true and we
2403    found an unnested comma.  */
2404
2405 static int
2406 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2407                                        bool recovering,
2408                                        bool or_comma,
2409                                        bool consume_paren)
2410 {
2411   unsigned paren_depth = 0;
2412   unsigned brace_depth = 0;
2413
2414   if (recovering && !or_comma
2415       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2416     return 0;
2417
2418   while (true)
2419     {
2420       cp_token * token = cp_lexer_peek_token (parser->lexer);
2421
2422       switch (token->type)
2423         {
2424         case CPP_EOF:
2425         case CPP_PRAGMA_EOL:
2426           /* If we've run out of tokens, then there is no closing `)'.  */
2427           return 0;
2428
2429         case CPP_SEMICOLON:
2430           /* This matches the processing in skip_to_end_of_statement.  */
2431           if (!brace_depth)
2432             return 0;
2433           break;
2434
2435         case CPP_OPEN_BRACE:
2436           ++brace_depth;
2437           break;
2438         case CPP_CLOSE_BRACE:
2439           if (!brace_depth--)
2440             return 0;
2441           break;
2442
2443         case CPP_COMMA:
2444           if (recovering && or_comma && !brace_depth && !paren_depth)
2445             return -1;
2446           break;
2447
2448         case CPP_OPEN_PAREN:
2449           if (!brace_depth)
2450             ++paren_depth;
2451           break;
2452
2453         case CPP_CLOSE_PAREN:
2454           if (!brace_depth && !paren_depth--)
2455             {
2456               if (consume_paren)
2457                 cp_lexer_consume_token (parser->lexer);
2458               return 1;
2459             }
2460           break;
2461
2462         default:
2463           break;
2464         }
2465
2466       /* Consume the token.  */
2467       cp_lexer_consume_token (parser->lexer);
2468     }
2469 }
2470
2471 /* Consume tokens until we reach the end of the current statement.
2472    Normally, that will be just before consuming a `;'.  However, if a
2473    non-nested `}' comes first, then we stop before consuming that.  */
2474
2475 static void
2476 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2477 {
2478   unsigned nesting_depth = 0;
2479
2480   while (true)
2481     {
2482       cp_token *token = cp_lexer_peek_token (parser->lexer);
2483
2484       switch (token->type)
2485         {
2486         case CPP_EOF:
2487         case CPP_PRAGMA_EOL:
2488           /* If we've run out of tokens, stop.  */
2489           return;
2490
2491         case CPP_SEMICOLON:
2492           /* If the next token is a `;', we have reached the end of the
2493              statement.  */
2494           if (!nesting_depth)
2495             return;
2496           break;
2497
2498         case CPP_CLOSE_BRACE:
2499           /* If this is a non-nested '}', stop before consuming it.
2500              That way, when confronted with something like:
2501
2502                { 3 + }
2503
2504              we stop before consuming the closing '}', even though we
2505              have not yet reached a `;'.  */
2506           if (nesting_depth == 0)
2507             return;
2508
2509           /* If it is the closing '}' for a block that we have
2510              scanned, stop -- but only after consuming the token.
2511              That way given:
2512
2513                 void f g () { ... }
2514                 typedef int I;
2515
2516              we will stop after the body of the erroneously declared
2517              function, but before consuming the following `typedef'
2518              declaration.  */
2519           if (--nesting_depth == 0)
2520             {
2521               cp_lexer_consume_token (parser->lexer);
2522               return;
2523             }
2524
2525         case CPP_OPEN_BRACE:
2526           ++nesting_depth;
2527           break;
2528
2529         default:
2530           break;
2531         }
2532
2533       /* Consume the token.  */
2534       cp_lexer_consume_token (parser->lexer);
2535     }
2536 }
2537
2538 /* This function is called at the end of a statement or declaration.
2539    If the next token is a semicolon, it is consumed; otherwise, error
2540    recovery is attempted.  */
2541
2542 static void
2543 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2544 {
2545   /* Look for the trailing `;'.  */
2546   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2547     {
2548       /* If there is additional (erroneous) input, skip to the end of
2549          the statement.  */
2550       cp_parser_skip_to_end_of_statement (parser);
2551       /* If the next token is now a `;', consume it.  */
2552       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2553         cp_lexer_consume_token (parser->lexer);
2554     }
2555 }
2556
2557 /* Skip tokens until we have consumed an entire block, or until we
2558    have consumed a non-nested `;'.  */
2559
2560 static void
2561 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2562 {
2563   int nesting_depth = 0;
2564
2565   while (nesting_depth >= 0)
2566     {
2567       cp_token *token = cp_lexer_peek_token (parser->lexer);
2568
2569       switch (token->type)
2570         {
2571         case CPP_EOF:
2572         case CPP_PRAGMA_EOL:
2573           /* If we've run out of tokens, stop.  */
2574           return;
2575
2576         case CPP_SEMICOLON:
2577           /* Stop if this is an unnested ';'. */
2578           if (!nesting_depth)
2579             nesting_depth = -1;
2580           break;
2581
2582         case CPP_CLOSE_BRACE:
2583           /* Stop if this is an unnested '}', or closes the outermost
2584              nesting level.  */
2585           nesting_depth--;
2586           if (!nesting_depth)
2587             nesting_depth = -1;
2588           break;
2589
2590         case CPP_OPEN_BRACE:
2591           /* Nest. */
2592           nesting_depth++;
2593           break;
2594
2595         default:
2596           break;
2597         }
2598
2599       /* Consume the token.  */
2600       cp_lexer_consume_token (parser->lexer);
2601     }
2602 }
2603
2604 /* Skip tokens until a non-nested closing curly brace is the next
2605    token, or there are no more tokens. Return true in the first case,
2606    false otherwise.  */
2607
2608 static bool
2609 cp_parser_skip_to_closing_brace (cp_parser *parser)
2610 {
2611   unsigned nesting_depth = 0;
2612
2613   while (true)
2614     {
2615       cp_token *token = cp_lexer_peek_token (parser->lexer);
2616
2617       switch (token->type)
2618         {
2619         case CPP_EOF:
2620         case CPP_PRAGMA_EOL:
2621           /* If we've run out of tokens, stop.  */
2622           return false;
2623
2624         case CPP_CLOSE_BRACE:
2625           /* If the next token is a non-nested `}', then we have reached
2626              the end of the current block.  */
2627           if (nesting_depth-- == 0)
2628             return true;
2629           break;
2630
2631         case CPP_OPEN_BRACE:
2632           /* If it the next token is a `{', then we are entering a new
2633              block.  Consume the entire block.  */
2634           ++nesting_depth;
2635           break;
2636
2637         default:
2638           break;
2639         }
2640
2641       /* Consume the token.  */
2642       cp_lexer_consume_token (parser->lexer);
2643     }
2644 }
2645
2646 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2647    parameter is the PRAGMA token, allowing us to purge the entire pragma
2648    sequence.  */
2649
2650 static void
2651 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2652 {
2653   cp_token *token;
2654
2655   parser->lexer->in_pragma = false;
2656
2657   do
2658     token = cp_lexer_consume_token (parser->lexer);
2659   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2660
2661   /* Ensure that the pragma is not parsed again.  */
2662   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2663 }
2664
2665 /* Require pragma end of line, resyncing with it as necessary.  The
2666    arguments are as for cp_parser_skip_to_pragma_eol.  */
2667
2668 static void
2669 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2670 {
2671   parser->lexer->in_pragma = false;
2672   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2673     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2674 }
2675
2676 /* This is a simple wrapper around make_typename_type. When the id is
2677    an unresolved identifier node, we can provide a superior diagnostic
2678    using cp_parser_diagnose_invalid_type_name.  */
2679
2680 static tree
2681 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2682 {
2683   tree result;
2684   if (TREE_CODE (id) == IDENTIFIER_NODE)
2685     {
2686       result = make_typename_type (scope, id, typename_type,
2687                                    /*complain=*/tf_none);
2688       if (result == error_mark_node)
2689         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2690       return result;
2691     }
2692   return make_typename_type (scope, id, typename_type, tf_error);
2693 }
2694
2695 /* This is a wrapper around the
2696    make_{pointer,ptrmem,reference}_declarator functions that decides
2697    which one to call based on the CODE and CLASS_TYPE arguments. The
2698    CODE argument should be one of the values returned by
2699    cp_parser_ptr_operator. */
2700 static cp_declarator *
2701 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2702                                     cp_cv_quals cv_qualifiers,
2703                                     cp_declarator *target)
2704 {
2705   if (code == INDIRECT_REF)
2706     if (class_type == NULL_TREE)
2707       return make_pointer_declarator (cv_qualifiers, target);
2708     else
2709       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2710   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2711     return make_reference_declarator (cv_qualifiers, target, false);
2712   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2713     return make_reference_declarator (cv_qualifiers, target, true);
2714   gcc_unreachable ();
2715 }
2716
2717 /* Create a new C++ parser.  */
2718
2719 static cp_parser *
2720 cp_parser_new (void)
2721 {
2722   cp_parser *parser;
2723   cp_lexer *lexer;
2724   unsigned i;
2725
2726   /* cp_lexer_new_main is called before calling ggc_alloc because
2727      cp_lexer_new_main might load a PCH file.  */
2728   lexer = cp_lexer_new_main ();
2729
2730   /* Initialize the binops_by_token so that we can get the tree
2731      directly from the token.  */
2732   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2733     binops_by_token[binops[i].token_type] = binops[i];
2734
2735   parser = GGC_CNEW (cp_parser);
2736   parser->lexer = lexer;
2737   parser->context = cp_parser_context_new (NULL);
2738
2739   /* For now, we always accept GNU extensions.  */
2740   parser->allow_gnu_extensions_p = 1;
2741
2742   /* The `>' token is a greater-than operator, not the end of a
2743      template-id.  */
2744   parser->greater_than_is_operator_p = true;
2745
2746   parser->default_arg_ok_p = true;
2747
2748   /* We are not parsing a constant-expression.  */
2749   parser->integral_constant_expression_p = false;
2750   parser->allow_non_integral_constant_expression_p = false;
2751   parser->non_integral_constant_expression_p = false;
2752
2753   /* Local variable names are not forbidden.  */
2754   parser->local_variables_forbidden_p = false;
2755
2756   /* We are not processing an `extern "C"' declaration.  */
2757   parser->in_unbraced_linkage_specification_p = false;
2758
2759   /* We are not processing a declarator.  */
2760   parser->in_declarator_p = false;
2761
2762   /* We are not processing a template-argument-list.  */
2763   parser->in_template_argument_list_p = false;
2764
2765   /* We are not in an iteration statement.  */
2766   parser->in_statement = 0;
2767
2768   /* We are not in a switch statement.  */
2769   parser->in_switch_statement_p = false;
2770
2771   /* We are not parsing a type-id inside an expression.  */
2772   parser->in_type_id_in_expr_p = false;
2773
2774   /* Declarations aren't implicitly extern "C".  */
2775   parser->implicit_extern_c = false;
2776
2777   /* String literals should be translated to the execution character set.  */
2778   parser->translate_strings_p = true;
2779
2780   /* We are not parsing a function body.  */
2781   parser->in_function_body = false;
2782
2783   /* The unparsed function queue is empty.  */
2784   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2785
2786   /* There are no classes being defined.  */
2787   parser->num_classes_being_defined = 0;
2788
2789   /* No template parameters apply.  */
2790   parser->num_template_parameter_lists = 0;
2791
2792   return parser;
2793 }
2794
2795 /* Create a cp_lexer structure which will emit the tokens in CACHE
2796    and push it onto the parser's lexer stack.  This is used for delayed
2797    parsing of in-class method bodies and default arguments, and should
2798    not be confused with tentative parsing.  */
2799 static void
2800 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2801 {
2802   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2803   lexer->next = parser->lexer;
2804   parser->lexer = lexer;
2805
2806   /* Move the current source position to that of the first token in the
2807      new lexer.  */
2808   cp_lexer_set_source_position_from_token (lexer->next_token);
2809 }
2810
2811 /* Pop the top lexer off the parser stack.  This is never used for the
2812    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2813 static void
2814 cp_parser_pop_lexer (cp_parser *parser)
2815 {
2816   cp_lexer *lexer = parser->lexer;
2817   parser->lexer = lexer->next;
2818   cp_lexer_destroy (lexer);
2819
2820   /* Put the current source position back where it was before this
2821      lexer was pushed.  */
2822   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2823 }
2824
2825 /* Lexical conventions [gram.lex]  */
2826
2827 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2828    identifier.  */
2829
2830 static tree
2831 cp_parser_identifier (cp_parser* parser)
2832 {
2833   cp_token *token;
2834
2835   /* Look for the identifier.  */
2836   token = cp_parser_require (parser, CPP_NAME, "identifier");
2837   /* Return the value.  */
2838   return token ? token->u.value : error_mark_node;
2839 }
2840
2841 /* Parse a sequence of adjacent string constants.  Returns a
2842    TREE_STRING representing the combined, nul-terminated string
2843    constant.  If TRANSLATE is true, translate the string to the
2844    execution character set.  If WIDE_OK is true, a wide string is
2845    invalid here.
2846
2847    C++98 [lex.string] says that if a narrow string literal token is
2848    adjacent to a wide string literal token, the behavior is undefined.
2849    However, C99 6.4.5p4 says that this results in a wide string literal.
2850    We follow C99 here, for consistency with the C front end.
2851
2852    This code is largely lifted from lex_string() in c-lex.c.
2853
2854    FUTURE: ObjC++ will need to handle @-strings here.  */
2855 static tree
2856 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2857 {
2858   tree value;
2859   bool wide = false;
2860   size_t count;
2861   struct obstack str_ob;
2862   cpp_string str, istr, *strs;
2863   cp_token *tok;
2864
2865   tok = cp_lexer_peek_token (parser->lexer);
2866   if (!cp_parser_is_string_literal (tok))
2867     {
2868       cp_parser_error (parser, "expected string-literal");
2869       return error_mark_node;
2870     }
2871
2872   /* Try to avoid the overhead of creating and destroying an obstack
2873      for the common case of just one string.  */
2874   if (!cp_parser_is_string_literal
2875       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2876     {
2877       cp_lexer_consume_token (parser->lexer);
2878
2879       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2880       str.len = TREE_STRING_LENGTH (tok->u.value);
2881       count = 1;
2882       if (tok->type == CPP_WSTRING)
2883         wide = true;
2884
2885       strs = &str;
2886     }
2887   else
2888     {
2889       gcc_obstack_init (&str_ob);
2890       count = 0;
2891
2892       do
2893         {
2894           cp_lexer_consume_token (parser->lexer);
2895           count++;
2896           str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2897           str.len = TREE_STRING_LENGTH (tok->u.value);
2898           if (tok->type == CPP_WSTRING)
2899             wide = true;
2900
2901           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2902
2903           tok = cp_lexer_peek_token (parser->lexer);
2904         }
2905       while (cp_parser_is_string_literal (tok));
2906
2907       strs = (cpp_string *) obstack_finish (&str_ob);
2908     }
2909
2910   if (wide && !wide_ok)
2911     {
2912       cp_parser_error (parser, "a wide string is invalid in this context");
2913       wide = false;
2914     }
2915
2916   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2917       (parse_in, strs, count, &istr, wide))
2918     {
2919       value = build_string (istr.len, (char *)istr.text);
2920       free ((void *)istr.text);
2921
2922       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2923       value = fix_string_type (value);
2924     }
2925   else
2926     /* cpp_interpret_string has issued an error.  */
2927     value = error_mark_node;
2928
2929   if (count > 1)
2930     obstack_free (&str_ob, 0);
2931
2932   return value;
2933 }
2934
2935
2936 /* Basic concepts [gram.basic]  */
2937
2938 /* Parse a translation-unit.
2939
2940    translation-unit:
2941      declaration-seq [opt]
2942
2943    Returns TRUE if all went well.  */
2944
2945 static bool
2946 cp_parser_translation_unit (cp_parser* parser)
2947 {
2948   /* The address of the first non-permanent object on the declarator
2949      obstack.  */
2950   static void *declarator_obstack_base;
2951
2952   bool success;
2953
2954   /* Create the declarator obstack, if necessary.  */
2955   if (!cp_error_declarator)
2956     {
2957       gcc_obstack_init (&declarator_obstack);
2958       /* Create the error declarator.  */
2959       cp_error_declarator = make_declarator (cdk_error);
2960       /* Create the empty parameter list.  */
2961       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2962       /* Remember where the base of the declarator obstack lies.  */
2963       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2964     }
2965
2966   cp_parser_declaration_seq_opt (parser);
2967
2968   /* If there are no tokens left then all went well.  */
2969   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2970     {
2971       /* Get rid of the token array; we don't need it any more.  */
2972       cp_lexer_destroy (parser->lexer);
2973       parser->lexer = NULL;
2974
2975       /* This file might have been a context that's implicitly extern
2976          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2977       if (parser->implicit_extern_c)
2978         {
2979           pop_lang_context ();
2980           parser->implicit_extern_c = false;
2981         }
2982
2983       /* Finish up.  */
2984       finish_translation_unit ();
2985
2986       success = true;
2987     }
2988   else
2989     {
2990       cp_parser_error (parser, "expected declaration");
2991       success = false;
2992     }
2993
2994   /* Make sure the declarator obstack was fully cleaned up.  */
2995   gcc_assert (obstack_next_free (&declarator_obstack)
2996               == declarator_obstack_base);
2997
2998   /* All went well.  */
2999   return success;
3000 }
3001
3002 /* Expressions [gram.expr] */
3003
3004 /* Parse a primary-expression.
3005
3006    primary-expression:
3007      literal
3008      this
3009      ( expression )
3010      id-expression
3011
3012    GNU Extensions:
3013
3014    primary-expression:
3015      ( compound-statement )
3016      __builtin_va_arg ( assignment-expression , type-id )
3017      __builtin_offsetof ( type-id , offsetof-expression )
3018
3019    C++ Extensions:
3020      __has_nothrow_assign ( type-id )   
3021      __has_nothrow_constructor ( type-id )
3022      __has_nothrow_copy ( type-id )
3023      __has_trivial_assign ( type-id )   
3024      __has_trivial_constructor ( type-id )
3025      __has_trivial_copy ( type-id )
3026      __has_trivial_destructor ( type-id )
3027      __has_virtual_destructor ( type-id )     
3028      __is_abstract ( type-id )
3029      __is_base_of ( type-id , type-id )
3030      __is_class ( type-id )
3031      __is_convertible_to ( type-id , type-id )     
3032      __is_empty ( type-id )
3033      __is_enum ( type-id )
3034      __is_pod ( type-id )
3035      __is_polymorphic ( type-id )
3036      __is_union ( type-id )
3037
3038    Objective-C++ Extension:
3039
3040    primary-expression:
3041      objc-expression
3042
3043    literal:
3044      __null
3045
3046    ADDRESS_P is true iff this expression was immediately preceded by
3047    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3048    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3049    true iff this expression is a template argument.
3050
3051    Returns a representation of the expression.  Upon return, *IDK
3052    indicates what kind of id-expression (if any) was present.  */
3053
3054 static tree
3055 cp_parser_primary_expression (cp_parser *parser,
3056                               bool address_p,
3057                               bool cast_p,
3058                               bool template_arg_p,
3059                               cp_id_kind *idk)
3060 {
3061   cp_token *token;
3062
3063   /* Assume the primary expression is not an id-expression.  */
3064   *idk = CP_ID_KIND_NONE;
3065
3066   /* Peek at the next token.  */
3067   token = cp_lexer_peek_token (parser->lexer);
3068   switch (token->type)
3069     {
3070       /* literal:
3071            integer-literal
3072            character-literal
3073            floating-literal
3074            string-literal
3075            boolean-literal  */
3076     case CPP_CHAR:
3077     case CPP_WCHAR:
3078     case CPP_NUMBER:
3079       token = cp_lexer_consume_token (parser->lexer);
3080       /* Floating-point literals are only allowed in an integral
3081          constant expression if they are cast to an integral or
3082          enumeration type.  */
3083       if (TREE_CODE (token->u.value) == REAL_CST
3084           && parser->integral_constant_expression_p
3085           && pedantic)
3086         {
3087           /* CAST_P will be set even in invalid code like "int(2.7 +
3088              ...)".   Therefore, we have to check that the next token
3089              is sure to end the cast.  */
3090           if (cast_p)
3091             {
3092               cp_token *next_token;
3093
3094               next_token = cp_lexer_peek_token (parser->lexer);
3095               if (/* The comma at the end of an
3096                      enumerator-definition.  */
3097                   next_token->type != CPP_COMMA
3098                   /* The curly brace at the end of an enum-specifier.  */
3099                   && next_token->type != CPP_CLOSE_BRACE
3100                   /* The end of a statement.  */
3101                   && next_token->type != CPP_SEMICOLON
3102                   /* The end of the cast-expression.  */
3103                   && next_token->type != CPP_CLOSE_PAREN
3104                   /* The end of an array bound.  */
3105                   && next_token->type != CPP_CLOSE_SQUARE
3106                   /* The closing ">" in a template-argument-list.  */
3107                   && (next_token->type != CPP_GREATER
3108                       || parser->greater_than_is_operator_p)
3109                   /* C++0x only: A ">>" treated like two ">" tokens,
3110                      in a template-argument-list.  */
3111                   && (next_token->type != CPP_RSHIFT
3112                       || (cxx_dialect == cxx98)
3113                       || parser->greater_than_is_operator_p))
3114                 cast_p = false;
3115             }
3116
3117           /* If we are within a cast, then the constraint that the
3118              cast is to an integral or enumeration type will be
3119              checked at that point.  If we are not within a cast, then
3120              this code is invalid.  */
3121           if (!cast_p)
3122             cp_parser_non_integral_constant_expression
3123               (parser, "floating-point literal");
3124         }
3125       return token->u.value;
3126
3127     case CPP_STRING:
3128     case CPP_WSTRING:
3129       /* ??? Should wide strings be allowed when parser->translate_strings_p
3130          is false (i.e. in attributes)?  If not, we can kill the third
3131          argument to cp_parser_string_literal.  */
3132       return cp_parser_string_literal (parser,
3133                                        parser->translate_strings_p,
3134                                        true);
3135
3136     case CPP_OPEN_PAREN:
3137       {
3138         tree expr;
3139         bool saved_greater_than_is_operator_p;
3140
3141         /* Consume the `('.  */
3142         cp_lexer_consume_token (parser->lexer);
3143         /* Within a parenthesized expression, a `>' token is always
3144            the greater-than operator.  */
3145         saved_greater_than_is_operator_p
3146           = parser->greater_than_is_operator_p;
3147         parser->greater_than_is_operator_p = true;
3148         /* If we see `( { ' then we are looking at the beginning of
3149            a GNU statement-expression.  */
3150         if (cp_parser_allow_gnu_extensions_p (parser)
3151             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3152           {
3153             /* Statement-expressions are not allowed by the standard.  */
3154             if (pedantic)
3155               pedwarn ("ISO C++ forbids braced-groups within expressions");
3156
3157             /* And they're not allowed outside of a function-body; you
3158                cannot, for example, write:
3159
3160                  int i = ({ int j = 3; j + 1; });
3161
3162                at class or namespace scope.  */
3163             if (!parser->in_function_body)
3164               {
3165                 error ("statement-expressions are allowed only inside functions");
3166                 cp_parser_skip_to_end_of_block_or_statement (parser);
3167                 expr = error_mark_node;
3168               }
3169             else
3170               {
3171                 /* Start the statement-expression.  */
3172                 expr = begin_stmt_expr ();
3173                 /* Parse the compound-statement.  */
3174                 cp_parser_compound_statement (parser, expr, false);
3175                 /* Finish up.  */
3176                 expr = finish_stmt_expr (expr, false);
3177               }
3178           }
3179         else
3180           {
3181             /* Parse the parenthesized expression.  */
3182             expr = cp_parser_expression (parser, cast_p);
3183             /* Let the front end know that this expression was
3184                enclosed in parentheses. This matters in case, for
3185                example, the expression is of the form `A::B', since
3186                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3187                not.  */
3188             finish_parenthesized_expr (expr);
3189           }
3190         /* The `>' token might be the end of a template-id or
3191            template-parameter-list now.  */
3192         parser->greater_than_is_operator_p
3193           = saved_greater_than_is_operator_p;
3194         /* Consume the `)'.  */
3195         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3196           cp_parser_skip_to_end_of_statement (parser);
3197
3198         return expr;
3199       }
3200
3201     case CPP_KEYWORD:
3202       switch (token->keyword)
3203         {
3204           /* These two are the boolean literals.  */
3205         case RID_TRUE:
3206           cp_lexer_consume_token (parser->lexer);
3207           return boolean_true_node;
3208         case RID_FALSE:
3209           cp_lexer_consume_token (parser->lexer);
3210           return boolean_false_node;
3211
3212           /* The `__null' literal.  */
3213         case RID_NULL:
3214           cp_lexer_consume_token (parser->lexer);
3215           return null_node;
3216
3217           /* Recognize the `this' keyword.  */
3218         case RID_THIS:
3219           cp_lexer_consume_token (parser->lexer);
3220           if (parser->local_variables_forbidden_p)
3221             {
3222               error ("%<this%> may not be used in this context");
3223               return error_mark_node;
3224             }
3225           /* Pointers cannot appear in constant-expressions.  */
3226           if (cp_parser_non_integral_constant_expression (parser,
3227                                                           "`this'"))
3228             return error_mark_node;
3229           return finish_this_expr ();
3230
3231           /* The `operator' keyword can be the beginning of an
3232              id-expression.  */
3233         case RID_OPERATOR:
3234           goto id_expression;
3235
3236         case RID_FUNCTION_NAME:
3237         case RID_PRETTY_FUNCTION_NAME:
3238         case RID_C99_FUNCTION_NAME:
3239           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3240              __func__ are the names of variables -- but they are
3241              treated specially.  Therefore, they are handled here,
3242              rather than relying on the generic id-expression logic
3243              below.  Grammatically, these names are id-expressions.
3244
3245              Consume the token.  */
3246           token = cp_lexer_consume_token (parser->lexer);
3247           /* Look up the name.  */
3248           return finish_fname (token->u.value);
3249
3250         case RID_VA_ARG:
3251           {
3252             tree expression;
3253             tree type;
3254
3255             /* The `__builtin_va_arg' construct is used to handle
3256                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3257             cp_lexer_consume_token (parser->lexer);
3258             /* Look for the opening `('.  */
3259             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3260             /* Now, parse the assignment-expression.  */
3261             expression = cp_parser_assignment_expression (parser,
3262                                                           /*cast_p=*/false);
3263             /* Look for the `,'.  */
3264             cp_parser_require (parser, CPP_COMMA, "`,'");
3265             /* Parse the type-id.  */
3266             type = cp_parser_type_id (parser);
3267             /* Look for the closing `)'.  */
3268             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3269             /* Using `va_arg' in a constant-expression is not
3270                allowed.  */
3271             if (cp_parser_non_integral_constant_expression (parser,
3272                                                             "`va_arg'"))
3273               return error_mark_node;
3274             return build_x_va_arg (expression, type);
3275           }
3276
3277         case RID_OFFSETOF:
3278           return cp_parser_builtin_offsetof (parser);
3279
3280         case RID_HAS_NOTHROW_ASSIGN:
3281         case RID_HAS_NOTHROW_CONSTRUCTOR:
3282         case RID_HAS_NOTHROW_COPY:        
3283         case RID_HAS_TRIVIAL_ASSIGN:
3284         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3285         case RID_HAS_TRIVIAL_COPY:        
3286         case RID_HAS_TRIVIAL_DESTRUCTOR:
3287         case RID_HAS_VIRTUAL_DESTRUCTOR:
3288         case RID_IS_ABSTRACT:
3289         case RID_IS_BASE_OF:
3290         case RID_IS_CLASS:
3291         case RID_IS_CONVERTIBLE_TO:
3292         case RID_IS_EMPTY:
3293         case RID_IS_ENUM:
3294         case RID_IS_POD:
3295         case RID_IS_POLYMORPHIC:
3296         case RID_IS_UNION:
3297           return cp_parser_trait_expr (parser, token->keyword);
3298
3299         /* Objective-C++ expressions.  */
3300         case RID_AT_ENCODE:
3301         case RID_AT_PROTOCOL:
3302         case RID_AT_SELECTOR:
3303           return cp_parser_objc_expression (parser);
3304
3305         default:
3306           cp_parser_error (parser, "expected primary-expression");
3307           return error_mark_node;
3308         }
3309
3310       /* An id-expression can start with either an identifier, a
3311          `::' as the beginning of a qualified-id, or the "operator"
3312          keyword.  */
3313     case CPP_NAME:
3314     case CPP_SCOPE:
3315     case CPP_TEMPLATE_ID:
3316     case CPP_NESTED_NAME_SPECIFIER:
3317       {
3318         tree id_expression;
3319         tree decl;
3320         const char *error_msg;
3321         bool template_p;
3322         bool done;
3323
3324       id_expression:
3325         /* Parse the id-expression.  */
3326         id_expression
3327           = cp_parser_id_expression (parser,
3328                                      /*template_keyword_p=*/false,
3329                                      /*check_dependency_p=*/true,
3330                                      &template_p,
3331                                      /*declarator_p=*/false,
3332                                      /*optional_p=*/false);
3333         if (id_expression == error_mark_node)
3334           return error_mark_node;
3335         token = cp_lexer_peek_token (parser->lexer);
3336         done = (token->type != CPP_OPEN_SQUARE
3337                 && token->type != CPP_OPEN_PAREN
3338                 && token->type != CPP_DOT
3339                 && token->type != CPP_DEREF
3340                 && token->type != CPP_PLUS_PLUS
3341                 && token->type != CPP_MINUS_MINUS);
3342         /* If we have a template-id, then no further lookup is
3343            required.  If the template-id was for a template-class, we
3344            will sometimes have a TYPE_DECL at this point.  */
3345         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3346                  || TREE_CODE (id_expression) == TYPE_DECL)
3347           decl = id_expression;
3348         /* Look up the name.  */
3349         else
3350           {
3351             tree ambiguous_decls;
3352
3353             decl = cp_parser_lookup_name (parser, id_expression,
3354                                           none_type,
3355                                           template_p,
3356                                           /*is_namespace=*/false,
3357                                           /*check_dependency=*/true,
3358                                           &ambiguous_decls);
3359             /* If the lookup was ambiguous, an error will already have
3360                been issued.  */
3361             if (ambiguous_decls)
3362               return error_mark_node;
3363
3364             /* In Objective-C++, an instance variable (ivar) may be preferred
3365                to whatever cp_parser_lookup_name() found.  */
3366             decl = objc_lookup_ivar (decl, id_expression);
3367
3368             /* If name lookup gives us a SCOPE_REF, then the
3369                qualifying scope was dependent.  */
3370             if (TREE_CODE (decl) == SCOPE_REF)
3371               return decl;
3372             /* Check to see if DECL is a local variable in a context
3373                where that is forbidden.  */
3374             if (parser->local_variables_forbidden_p
3375                 && local_variable_p (decl))
3376               {
3377                 /* It might be that we only found DECL because we are
3378                    trying to be generous with pre-ISO scoping rules.
3379                    For example, consider:
3380
3381                      int i;
3382                      void g() {
3383                        for (int i = 0; i < 10; ++i) {}
3384                        extern void f(int j = i);
3385                      }
3386
3387                    Here, name look up will originally find the out
3388                    of scope `i'.  We need to issue a warning message,
3389                    but then use the global `i'.  */
3390                 decl = check_for_out_of_scope_variable (decl);
3391                 if (local_variable_p (decl))
3392                   {
3393                     error ("local variable %qD may not appear in this context",
3394                            decl);
3395                     return error_mark_node;
3396                   }
3397               }
3398           }
3399
3400         decl = (finish_id_expression
3401                 (id_expression, decl, parser->scope,
3402                  idk,
3403                  parser->integral_constant_expression_p,
3404                  parser->allow_non_integral_constant_expression_p,
3405                  &parser->non_integral_constant_expression_p,
3406                  template_p, done, address_p,
3407                  template_arg_p,
3408                  &error_msg));
3409         if (error_msg)
3410           cp_parser_error (parser, error_msg);
3411         return decl;
3412       }
3413
3414       /* Anything else is an error.  */
3415     default:
3416       /* ...unless we have an Objective-C++ message or string literal,
3417          that is.  */
3418       if (c_dialect_objc ()
3419           && (token->type == CPP_OPEN_SQUARE
3420               || token->type == CPP_OBJC_STRING))
3421         return cp_parser_objc_expression (parser);
3422
3423       cp_parser_error (parser, "expected primary-expression");
3424       return error_mark_node;
3425     }
3426 }
3427
3428 /* Parse an id-expression.
3429
3430    id-expression:
3431      unqualified-id
3432      qualified-id
3433
3434    qualified-id:
3435      :: [opt] nested-name-specifier template [opt] unqualified-id
3436      :: identifier
3437      :: operator-function-id
3438      :: template-id
3439
3440    Return a representation of the unqualified portion of the
3441    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3442    a `::' or nested-name-specifier.
3443
3444    Often, if the id-expression was a qualified-id, the caller will
3445    want to make a SCOPE_REF to represent the qualified-id.  This
3446    function does not do this in order to avoid wastefully creating
3447    SCOPE_REFs when they are not required.
3448
3449    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3450    `template' keyword.
3451
3452    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3453    uninstantiated templates.
3454
3455    If *TEMPLATE_P is non-NULL, it is set to true iff the
3456    `template' keyword is used to explicitly indicate that the entity
3457    named is a template.
3458
3459    If DECLARATOR_P is true, the id-expression is appearing as part of
3460    a declarator, rather than as part of an expression.  */
3461
3462 static tree
3463 cp_parser_id_expression (cp_parser *parser,
3464                          bool template_keyword_p,
3465                          bool check_dependency_p,
3466                          bool *template_p,
3467                          bool declarator_p,
3468                          bool optional_p)
3469 {
3470   bool global_scope_p;
3471   bool nested_name_specifier_p;
3472
3473   /* Assume the `template' keyword was not used.  */
3474   if (template_p)
3475     *template_p = template_keyword_p;
3476
3477   /* Look for the optional `::' operator.  */
3478   global_scope_p
3479     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3480        != NULL_TREE);
3481   /* Look for the optional nested-name-specifier.  */
3482   nested_name_specifier_p
3483     = (cp_parser_nested_name_specifier_opt (parser,
3484                                             /*typename_keyword_p=*/false,
3485                                             check_dependency_p,
3486                                             /*type_p=*/false,
3487                                             declarator_p)
3488        != NULL_TREE);
3489   /* If there is a nested-name-specifier, then we are looking at
3490      the first qualified-id production.  */
3491   if (nested_name_specifier_p)
3492     {
3493       tree saved_scope;
3494       tree saved_object_scope;
3495       tree saved_qualifying_scope;
3496       tree unqualified_id;
3497       bool is_template;
3498
3499       /* See if the next token is the `template' keyword.  */
3500       if (!template_p)
3501         template_p = &is_template;
3502       *template_p = cp_parser_optional_template_keyword (parser);
3503       /* Name lookup we do during the processing of the
3504          unqualified-id might obliterate SCOPE.  */
3505       saved_scope = parser->scope;
3506       saved_object_scope = parser->object_scope;
3507       saved_qualifying_scope = parser->qualifying_scope;
3508       /* Process the final unqualified-id.  */
3509       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3510                                                  check_dependency_p,
3511                                                  declarator_p,
3512                                                  /*optional_p=*/false);
3513       /* Restore the SAVED_SCOPE for our caller.  */
3514       parser->scope = saved_scope;
3515       parser->object_scope = saved_object_scope;
3516       parser->qualifying_scope = saved_qualifying_scope;
3517
3518       return unqualified_id;
3519     }
3520   /* Otherwise, if we are in global scope, then we are looking at one
3521      of the other qualified-id productions.  */
3522   else if (global_scope_p)
3523     {
3524       cp_token *token;
3525       tree id;
3526
3527       /* Peek at the next token.  */
3528       token = cp_lexer_peek_token (parser->lexer);
3529
3530       /* If it's an identifier, and the next token is not a "<", then
3531          we can avoid the template-id case.  This is an optimization
3532          for this common case.  */
3533       if (token->type == CPP_NAME
3534           && !cp_parser_nth_token_starts_template_argument_list_p
3535                (parser, 2))
3536         return cp_parser_identifier (parser);
3537
3538       cp_parser_parse_tentatively (parser);
3539       /* Try a template-id.  */
3540       id = cp_parser_template_id (parser,
3541                                   /*template_keyword_p=*/false,
3542                                   /*check_dependency_p=*/true,
3543                                   declarator_p);
3544       /* If that worked, we're done.  */
3545       if (cp_parser_parse_definitely (parser))
3546         return id;
3547
3548       /* Peek at the next token.  (Changes in the token buffer may
3549          have invalidated the pointer obtained above.)  */
3550       token = cp_lexer_peek_token (parser->lexer);
3551
3552       switch (token->type)
3553         {
3554         case CPP_NAME:
3555           return cp_parser_identifier (parser);
3556
3557         case CPP_KEYWORD:
3558           if (token->keyword == RID_OPERATOR)
3559             return cp_parser_operator_function_id (parser);
3560           /* Fall through.  */
3561
3562         default:
3563           cp_parser_error (parser, "expected id-expression");
3564           return error_mark_node;
3565         }
3566     }
3567   else
3568     return cp_parser_unqualified_id (parser, template_keyword_p,
3569                                      /*check_dependency_p=*/true,
3570                                      declarator_p,
3571                                      optional_p);
3572 }
3573
3574 /* Parse an unqualified-id.
3575
3576    unqualified-id:
3577      identifier
3578      operator-function-id
3579      conversion-function-id
3580      ~ class-name
3581      template-id
3582
3583    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3584    keyword, in a construct like `A::template ...'.
3585
3586    Returns a representation of unqualified-id.  For the `identifier'
3587    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3588    production a BIT_NOT_EXPR is returned; the operand of the
3589    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3590    other productions, see the documentation accompanying the
3591    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3592    names are looked up in uninstantiated templates.  If DECLARATOR_P
3593    is true, the unqualified-id is appearing as part of a declarator,
3594    rather than as part of an expression.  */
3595
3596 static tree
3597 cp_parser_unqualified_id (cp_parser* parser,
3598                           bool template_keyword_p,
3599                           bool check_dependency_p,
3600                           bool declarator_p,
3601                           bool optional_p)
3602 {
3603   cp_token *token;
3604
3605   /* Peek at the next token.  */
3606   token = cp_lexer_peek_token (parser->lexer);
3607
3608   switch (token->type)
3609     {
3610     case CPP_NAME:
3611       {
3612         tree id;
3613
3614         /* We don't know yet whether or not this will be a
3615            template-id.  */
3616         cp_parser_parse_tentatively (parser);
3617         /* Try a template-id.  */
3618         id = cp_parser_template_id (parser, template_keyword_p,
3619                                     check_dependency_p,
3620                                     declarator_p);
3621         /* If it worked, we're done.  */
3622         if (cp_parser_parse_definitely (parser))
3623           return id;
3624         /* Otherwise, it's an ordinary identifier.  */
3625         return cp_parser_identifier (parser);
3626       }
3627
3628     case CPP_TEMPLATE_ID:
3629       return cp_parser_template_id (parser, template_keyword_p,
3630                                     check_dependency_p,
3631                                     declarator_p);
3632
3633     case CPP_COMPL:
3634       {
3635         tree type_decl;
3636         tree qualifying_scope;
3637         tree object_scope;
3638         tree scope;
3639         bool done;
3640
3641         /* Consume the `~' token.  */
3642         cp_lexer_consume_token (parser->lexer);
3643         /* Parse the class-name.  The standard, as written, seems to
3644            say that:
3645
3646              template <typename T> struct S { ~S (); };
3647              template <typename T> S<T>::~S() {}
3648
3649            is invalid, since `~' must be followed by a class-name, but
3650            `S<T>' is dependent, and so not known to be a class.
3651            That's not right; we need to look in uninstantiated
3652            templates.  A further complication arises from:
3653
3654              template <typename T> void f(T t) {
3655                t.T::~T();
3656              }
3657
3658            Here, it is not possible to look up `T' in the scope of `T'
3659            itself.  We must look in both the current scope, and the
3660            scope of the containing complete expression.
3661
3662            Yet another issue is:
3663
3664              struct S {
3665                int S;
3666                ~S();
3667              };
3668
3669              S::~S() {}
3670
3671            The standard does not seem to say that the `S' in `~S'
3672            should refer to the type `S' and not the data member
3673            `S::S'.  */
3674
3675         /* DR 244 says that we look up the name after the "~" in the
3676            same scope as we looked up the qualifying name.  That idea
3677            isn't fully worked out; it's more complicated than that.  */
3678         scope = parser->scope;
3679         object_scope = parser->object_scope;
3680         qualifying_scope = parser->qualifying_scope;
3681
3682         /* Check for invalid scopes.  */
3683         if (scope == error_mark_node)
3684           {
3685             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3686               cp_lexer_consume_token (parser->lexer);
3687             return error_mark_node;
3688           }
3689         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3690           {
3691             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3692               error ("scope %qT before %<~%> is not a class-name", scope);
3693             cp_parser_simulate_error (parser);
3694             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3695               cp_lexer_consume_token (parser->lexer);
3696             return error_mark_node;
3697           }
3698         gcc_assert (!scope || TYPE_P (scope));
3699
3700         /* If the name is of the form "X::~X" it's OK.  */
3701         token = cp_lexer_peek_token (parser->lexer);
3702         if (scope
3703             && token->type == CPP_NAME
3704             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3705                 == CPP_OPEN_PAREN)
3706             && constructor_name_p (token->u.value, scope))
3707           {
3708             cp_lexer_consume_token (parser->lexer);
3709             return build_nt (BIT_NOT_EXPR, scope);
3710           }
3711
3712         /* If there was an explicit qualification (S::~T), first look
3713            in the scope given by the qualification (i.e., S).  */
3714         done = false;
3715         type_decl = NULL_TREE;
3716         if (scope)
3717           {
3718             cp_parser_parse_tentatively (parser);
3719             type_decl = cp_parser_class_name (parser,
3720                                               /*typename_keyword_p=*/false,
3721                                               /*template_keyword_p=*/false,
3722                                               none_type,
3723                                               /*check_dependency=*/false,
3724                                               /*class_head_p=*/false,
3725                                               declarator_p);
3726             if (cp_parser_parse_definitely (parser))
3727               done = true;
3728           }
3729         /* In "N::S::~S", look in "N" as well.  */
3730         if (!done && scope && qualifying_scope)
3731           {
3732             cp_parser_parse_tentatively (parser);
3733             parser->scope = qualifying_scope;
3734             parser->object_scope = NULL_TREE;
3735             parser->qualifying_scope = NULL_TREE;
3736             type_decl
3737               = cp_parser_class_name (parser,
3738                                       /*typename_keyword_p=*/false,
3739                                       /*template_keyword_p=*/false,
3740                                       none_type,
3741                                       /*check_dependency=*/false,
3742                                       /*class_head_p=*/false,
3743                                       declarator_p);
3744             if (cp_parser_parse_definitely (parser))
3745               done = true;
3746           }
3747         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3748         else if (!done && object_scope)
3749           {
3750             cp_parser_parse_tentatively (parser);
3751             parser->scope = object_scope;
3752             parser->object_scope = NULL_TREE;
3753             parser->qualifying_scope = NULL_TREE;
3754             type_decl
3755               = cp_parser_class_name (parser,
3756                                       /*typename_keyword_p=*/false,
3757                                       /*template_keyword_p=*/false,
3758                                       none_type,
3759                                       /*check_dependency=*/false,
3760                                       /*class_head_p=*/false,
3761                                       declarator_p);
3762             if (cp_parser_parse_definitely (parser))
3763               done = true;
3764           }
3765         /* Look in the surrounding context.  */
3766         if (!done)
3767           {
3768             parser->scope = NULL_TREE;
3769             parser->object_scope = NULL_TREE;
3770             parser->qualifying_scope = NULL_TREE;
3771             type_decl
3772               = cp_parser_class_name (parser,
3773                                       /*typename_keyword_p=*/false,
3774                                       /*template_keyword_p=*/false,
3775                                       none_type,
3776                                       /*check_dependency=*/false,
3777                                       /*class_head_p=*/false,
3778                                       declarator_p);
3779           }
3780         /* If an error occurred, assume that the name of the
3781            destructor is the same as the name of the qualifying
3782            class.  That allows us to keep parsing after running
3783            into ill-formed destructor names.  */
3784         if (type_decl == error_mark_node && scope)
3785           return build_nt (BIT_NOT_EXPR, scope);
3786         else if (type_decl == error_mark_node)
3787           return error_mark_node;
3788
3789         /* Check that destructor name and scope match.  */
3790         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3791           {
3792             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3793               error ("declaration of %<~%T%> as member of %qT",
3794                      type_decl, scope);
3795             cp_parser_simulate_error (parser);
3796             return error_mark_node;
3797           }
3798
3799         /* [class.dtor]
3800
3801            A typedef-name that names a class shall not be used as the
3802            identifier in the declarator for a destructor declaration.  */
3803         if (declarator_p
3804             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3805             && !DECL_SELF_REFERENCE_P (type_decl)
3806             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3807           error ("typedef-name %qD used as destructor declarator",
3808                  type_decl);
3809
3810         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3811       }
3812
3813     case CPP_KEYWORD:
3814       if (token->keyword == RID_OPERATOR)
3815         {
3816           tree id;
3817
3818           /* This could be a template-id, so we try that first.  */
3819           cp_parser_parse_tentatively (parser);
3820           /* Try a template-id.  */
3821           id = cp_parser_template_id (parser, template_keyword_p,
3822                                       /*check_dependency_p=*/true,
3823                                       declarator_p);
3824           /* If that worked, we're done.  */
3825           if (cp_parser_parse_definitely (parser))
3826             return id;
3827           /* We still don't know whether we're looking at an
3828              operator-function-id or a conversion-function-id.  */
3829           cp_parser_parse_tentatively (parser);
3830           /* Try an operator-function-id.  */
3831           id = cp_parser_operator_function_id (parser);
3832           /* If that didn't work, try a conversion-function-id.  */
3833           if (!cp_parser_parse_definitely (parser))
3834             id = cp_parser_conversion_function_id (parser);
3835
3836           return id;
3837         }
3838       /* Fall through.  */
3839
3840     default:
3841       if (optional_p)
3842         return NULL_TREE;
3843       cp_parser_error (parser, "expected unqualified-id");
3844       return error_mark_node;
3845     }
3846 }
3847
3848 /* Parse an (optional) nested-name-specifier.
3849
3850    nested-name-specifier:
3851      class-or-namespace-name :: nested-name-specifier [opt]
3852      class-or-namespace-name :: template nested-name-specifier [opt]
3853
3854    PARSER->SCOPE should be set appropriately before this function is
3855    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3856    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3857    in name lookups.
3858
3859    Sets PARSER->SCOPE to the class (TYPE) or namespace
3860    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3861    it unchanged if there is no nested-name-specifier.  Returns the new
3862    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3863
3864    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3865    part of a declaration and/or decl-specifier.  */
3866
3867 static tree
3868 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3869                                      bool typename_keyword_p,
3870                                      bool check_dependency_p,
3871                                      bool type_p,
3872                                      bool is_declaration)
3873 {
3874   bool success = false;
3875   cp_token_position start = 0;
3876   cp_token *token;
3877
3878   /* Remember where the nested-name-specifier starts.  */
3879   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3880     {
3881       start = cp_lexer_token_position (parser->lexer, false);
3882       push_deferring_access_checks (dk_deferred);
3883     }
3884
3885   while (true)
3886     {
3887       tree new_scope;
3888       tree old_scope;
3889       tree saved_qualifying_scope;
3890       bool template_keyword_p;
3891
3892       /* Spot cases that cannot be the beginning of a
3893          nested-name-specifier.  */
3894       token = cp_lexer_peek_token (parser->lexer);
3895
3896       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3897          the already parsed nested-name-specifier.  */
3898       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3899         {
3900           /* Grab the nested-name-specifier and continue the loop.  */
3901           cp_parser_pre_parsed_nested_name_specifier (parser);
3902           /* If we originally encountered this nested-name-specifier
3903              with IS_DECLARATION set to false, we will not have
3904              resolved TYPENAME_TYPEs, so we must do so here.  */
3905           if (is_declaration
3906               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3907             {
3908               new_scope = resolve_typename_type (parser->scope,
3909                                                  /*only_current_p=*/false);
3910               if (new_scope != error_mark_node)
3911                 parser->scope = new_scope;
3912             }
3913           success = true;
3914           continue;
3915         }
3916
3917       /* Spot cases that cannot be the beginning of a
3918          nested-name-specifier.  On the second and subsequent times
3919          through the loop, we look for the `template' keyword.  */
3920       if (success && token->keyword == RID_TEMPLATE)
3921         ;
3922       /* A template-id can start a nested-name-specifier.  */
3923       else if (token->type == CPP_TEMPLATE_ID)
3924         ;
3925       else
3926         {
3927           /* If the next token is not an identifier, then it is
3928              definitely not a class-or-namespace-name.  */
3929           if (token->type != CPP_NAME)
3930             break;
3931           /* If the following token is neither a `<' (to begin a
3932              template-id), nor a `::', then we are not looking at a
3933              nested-name-specifier.  */
3934           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3935           if (token->type != CPP_SCOPE
3936               && !cp_parser_nth_token_starts_template_argument_list_p
3937                   (parser, 2))
3938             break;
3939         }
3940
3941       /* The nested-name-specifier is optional, so we parse
3942          tentatively.  */
3943       cp_parser_parse_tentatively (parser);
3944
3945       /* Look for the optional `template' keyword, if this isn't the
3946          first time through the loop.  */
3947       if (success)
3948         template_keyword_p = cp_parser_optional_template_keyword (parser);
3949       else
3950         template_keyword_p = false;
3951
3952       /* Save the old scope since the name lookup we are about to do
3953          might destroy it.  */
3954       old_scope = parser->scope;
3955       saved_qualifying_scope = parser->qualifying_scope;
3956       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3957          look up names in "X<T>::I" in order to determine that "Y" is
3958          a template.  So, if we have a typename at this point, we make
3959          an effort to look through it.  */
3960       if (is_declaration
3961           && !typename_keyword_p
3962           && parser->scope
3963           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3964         parser->scope = resolve_typename_type (parser->scope,
3965                                                /*only_current_p=*/false);
3966       /* Parse the qualifying entity.  */
3967       new_scope
3968         = cp_parser_class_or_namespace_name (parser,
3969                                              typename_keyword_p,
3970                                              template_keyword_p,
3971                                              check_dependency_p,
3972                                              type_p,
3973                                              is_declaration);
3974       /* Look for the `::' token.  */
3975       cp_parser_require (parser, CPP_SCOPE, "`::'");
3976
3977       /* If we found what we wanted, we keep going; otherwise, we're
3978          done.  */
3979       if (!cp_parser_parse_definitely (parser))
3980         {
3981           bool error_p = false;
3982
3983           /* Restore the OLD_SCOPE since it was valid before the
3984              failed attempt at finding the last
3985              class-or-namespace-name.  */
3986           parser->scope = old_scope;
3987           parser->qualifying_scope = saved_qualifying_scope;
3988           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3989             break;
3990           /* If the next token is an identifier, and the one after
3991              that is a `::', then any valid interpretation would have
3992              found a class-or-namespace-name.  */
3993           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3994                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3995                      == CPP_SCOPE)
3996                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3997                      != CPP_COMPL))
3998             {
3999               token = cp_lexer_consume_token (parser->lexer);
4000               if (!error_p)
4001                 {
4002                   if (!token->ambiguous_p)
4003                     {
4004                       tree decl;
4005                       tree ambiguous_decls;
4006
4007                       decl = cp_parser_lookup_name (parser, token->u.value,
4008                                                     none_type,
4009                                                     /*is_template=*/false,
4010                                                     /*is_namespace=*/false,
4011                                                     /*check_dependency=*/true,
4012                                                     &ambiguous_decls);
4013                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4014                         error ("%qD used without template parameters", decl);
4015                       else if (ambiguous_decls)
4016                         {
4017                           error ("reference to %qD is ambiguous",
4018                                  token->u.value);
4019                           print_candidates (ambiguous_decls);
4020                           decl = error_mark_node;
4021                         }
4022                       else
4023                         cp_parser_name_lookup_error
4024                           (parser, token->u.value, decl,
4025                            "is not a class or namespace");
4026                     }
4027                   parser->scope = error_mark_node;
4028                   error_p = true;
4029                   /* Treat this as a successful nested-name-specifier
4030                      due to:
4031
4032                      [basic.lookup.qual]
4033
4034                      If the name found is not a class-name (clause
4035                      _class_) or namespace-name (_namespace.def_), the
4036                      program is ill-formed.  */
4037                   success = true;
4038                 }
4039               cp_lexer_consume_token (parser->lexer);
4040             }
4041           break;
4042         }
4043       /* We've found one valid nested-name-specifier.  */
4044       success = true;
4045       /* Name lookup always gives us a DECL.  */
4046       if (TREE_CODE (new_scope) == TYPE_DECL)
4047         new_scope = TREE_TYPE (new_scope);
4048       /* Uses of "template" must be followed by actual templates.  */
4049       if (template_keyword_p
4050           && !(CLASS_TYPE_P (new_scope)
4051                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4052                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4053                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4054           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4055                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4056                    == TEMPLATE_ID_EXPR)))
4057         pedwarn (TYPE_P (new_scope)
4058                  ? "%qT is not a template"
4059                  : "%qD is not a template",
4060                  new_scope);
4061       /* If it is a class scope, try to complete it; we are about to
4062          be looking up names inside the class.  */
4063       if (TYPE_P (new_scope)
4064           /* Since checking types for dependency can be expensive,
4065              avoid doing it if the type is already complete.  */
4066           && !COMPLETE_TYPE_P (new_scope)
4067           /* Do not try to complete dependent types.  */
4068           && !dependent_type_p (new_scope))
4069         new_scope = complete_type (new_scope);
4070       /* Make sure we look in the right scope the next time through
4071          the loop.  */
4072       parser->scope = new_scope;
4073     }
4074
4075   /* If parsing tentatively, replace the sequence of tokens that makes
4076      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4077      token.  That way, should we re-parse the token stream, we will
4078      not have to repeat the effort required to do the parse, nor will
4079      we issue duplicate error messages.  */
4080   if (success && start)
4081     {
4082       cp_token *token;
4083
4084       token = cp_lexer_token_at (parser->lexer, start);
4085       /* Reset the contents of the START token.  */
4086       token->type = CPP_NESTED_NAME_SPECIFIER;
4087       /* Retrieve any deferred checks.  Do not pop this access checks yet
4088          so the memory will not be reclaimed during token replacing below.  */
4089       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4090       token->u.tree_check_value->value = parser->scope;
4091       token->u.tree_check_value->checks = get_deferred_access_checks ();
4092       token->u.tree_check_value->qualifying_scope =
4093         parser->qualifying_scope;
4094       token->keyword = RID_MAX;
4095
4096       /* Purge all subsequent tokens.  */
4097       cp_lexer_purge_tokens_after (parser->lexer, start);
4098     }
4099
4100   if (start)
4101     pop_to_parent_deferring_access_checks ();
4102
4103   return success ? parser->scope : NULL_TREE;
4104 }
4105
4106 /* Parse a nested-name-specifier.  See
4107    cp_parser_nested_name_specifier_opt for details.  This function
4108    behaves identically, except that it will an issue an error if no
4109    nested-name-specifier is present.  */
4110
4111 static tree
4112 cp_parser_nested_name_specifier (cp_parser *parser,
4113                                  bool typename_keyword_p,
4114                                  bool check_dependency_p,
4115                                  bool type_p,
4116                                  bool is_declaration)
4117 {
4118   tree scope;
4119
4120   /* Look for the nested-name-specifier.  */
4121   scope = cp_parser_nested_name_specifier_opt (parser,
4122                                                typename_keyword_p,
4123                                                check_dependency_p,
4124                                                type_p,
4125                                                is_declaration);
4126   /* If it was not present, issue an error message.  */
4127   if (!scope)
4128     {
4129       cp_parser_error (parser, "expected nested-name-specifier");
4130       parser->scope = NULL_TREE;
4131     }
4132
4133   return scope;
4134 }
4135
4136 /* Parse a class-or-namespace-name.
4137
4138    class-or-namespace-name:
4139      class-name
4140      namespace-name
4141
4142    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4143    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4144    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4145    TYPE_P is TRUE iff the next name should be taken as a class-name,
4146    even the same name is declared to be another entity in the same
4147    scope.
4148
4149    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4150    specified by the class-or-namespace-name.  If neither is found the
4151    ERROR_MARK_NODE is returned.  */
4152
4153 static tree
4154 cp_parser_class_or_namespace_name (cp_parser *parser,
4155                                    bool typename_keyword_p,
4156                                    bool template_keyword_p,
4157                                    bool check_dependency_p,
4158                                    bool type_p,
4159                                    bool is_declaration)
4160 {
4161   tree saved_scope;
4162   tree saved_qualifying_scope;
4163   tree saved_object_scope;
4164   tree scope;
4165   bool only_class_p;
4166
4167   /* Before we try to parse the class-name, we must save away the
4168      current PARSER->SCOPE since cp_parser_class_name will destroy
4169      it.  */
4170   saved_scope = parser->scope;
4171   saved_qualifying_scope = parser->qualifying_scope;
4172   saved_object_scope = parser->object_scope;
4173   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4174      there is no need to look for a namespace-name.  */
4175   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4176   if (!only_class_p)
4177     cp_parser_parse_tentatively (parser);
4178   scope = cp_parser_class_name (parser,
4179                                 typename_keyword_p,
4180                                 template_keyword_p,
4181                                 type_p ? class_type : none_type,
4182                                 check_dependency_p,
4183                                 /*class_head_p=*/false,
4184                                 is_declaration);
4185   /* If that didn't work, try for a namespace-name.  */
4186   if (!only_class_p && !cp_parser_parse_definitely (parser))
4187     {
4188       /* Restore the saved scope.  */
4189       parser->scope = saved_scope;
4190       parser->qualifying_scope = saved_qualifying_scope;
4191       parser->object_scope = saved_object_scope;
4192       /* If we are not looking at an identifier followed by the scope
4193          resolution operator, then this is not part of a
4194          nested-name-specifier.  (Note that this function is only used
4195          to parse the components of a nested-name-specifier.)  */
4196       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4197           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4198         return error_mark_node;
4199       scope = cp_parser_namespace_name (parser);
4200     }
4201
4202   return scope;
4203 }
4204
4205 /* Parse a postfix-expression.
4206
4207    postfix-expression:
4208      primary-expression
4209      postfix-expression [ expression ]
4210      postfix-expression ( expression-list [opt] )
4211      simple-type-specifier ( expression-list [opt] )
4212      typename :: [opt] nested-name-specifier identifier
4213        ( expression-list [opt] )
4214      typename :: [opt] nested-name-specifier template [opt] template-id
4215        ( expression-list [opt] )
4216      postfix-expression . template [opt] id-expression
4217      postfix-expression -> template [opt] id-expression
4218      postfix-expression . pseudo-destructor-name
4219      postfix-expression -> pseudo-destructor-name
4220      postfix-expression ++
4221      postfix-expression --
4222      dynamic_cast < type-id > ( expression )
4223      static_cast < type-id > ( expression )
4224      reinterpret_cast < type-id > ( expression )
4225      const_cast < type-id > ( expression )
4226      typeid ( expression )
4227      typeid ( type-id )
4228
4229    GNU Extension:
4230
4231    postfix-expression:
4232      ( type-id ) { initializer-list , [opt] }
4233
4234    This extension is a GNU version of the C99 compound-literal
4235    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4236    but they are essentially the same concept.)
4237
4238    If ADDRESS_P is true, the postfix expression is the operand of the
4239    `&' operator.  CAST_P is true if this expression is the target of a
4240    cast.
4241
4242    Returns a representation of the expression.  */
4243
4244 static tree
4245 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4246 {
4247   cp_token *token;
4248   enum rid keyword;
4249   cp_id_kind idk = CP_ID_KIND_NONE;
4250   tree postfix_expression = NULL_TREE;
4251
4252   /* Peek at the next token.  */
4253   token = cp_lexer_peek_token (parser->lexer);
4254   /* Some of the productions are determined by keywords.  */
4255   keyword = token->keyword;
4256   switch (keyword)
4257     {
4258     case RID_DYNCAST:
4259     case RID_STATCAST:
4260     case RID_REINTCAST:
4261     case RID_CONSTCAST:
4262       {
4263         tree type;
4264         tree expression;
4265         const char *saved_message;
4266
4267         /* All of these can be handled in the same way from the point
4268            of view of parsing.  Begin by consuming the token
4269            identifying the cast.  */
4270         cp_lexer_consume_token (parser->lexer);
4271
4272         /* New types cannot be defined in the cast.  */
4273         saved_message = parser->type_definition_forbidden_message;
4274         parser->type_definition_forbidden_message
4275           = "types may not be defined in casts";
4276
4277         /* Look for the opening `<'.  */
4278         cp_parser_require (parser, CPP_LESS, "`<'");
4279         /* Parse the type to which we are casting.  */
4280         type = cp_parser_type_id (parser);
4281         /* Look for the closing `>'.  */
4282         cp_parser_require (parser, CPP_GREATER, "`>'");
4283         /* Restore the old message.  */
4284         parser->type_definition_forbidden_message = saved_message;
4285
4286         /* And the expression which is being cast.  */
4287         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4288         expression = cp_parser_expression (parser, /*cast_p=*/true);
4289         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4290
4291         /* Only type conversions to integral or enumeration types
4292            can be used in constant-expressions.  */
4293         if (!cast_valid_in_integral_constant_expression_p (type)
4294             && (cp_parser_non_integral_constant_expression
4295                 (parser,
4296                  "a cast to a type other than an integral or "
4297                  "enumeration type")))
4298           return error_mark_node;
4299
4300         switch (keyword)
4301           {
4302           case RID_DYNCAST:
4303             postfix_expression
4304               = build_dynamic_cast (type, expression);
4305             break;
4306           case RID_STATCAST:
4307             postfix_expression
4308               = build_static_cast (type, expression);
4309             break;
4310           case RID_REINTCAST:
4311             postfix_expression
4312               = build_reinterpret_cast (type, expression);
4313             break;
4314           case RID_CONSTCAST:
4315             postfix_expression
4316               = build_const_cast (type, expression);
4317             break;
4318           default:
4319             gcc_unreachable ();
4320           }
4321       }
4322       break;
4323
4324     case RID_TYPEID:
4325       {
4326         tree type;
4327         const char *saved_message;
4328         bool saved_in_type_id_in_expr_p;
4329
4330         /* Consume the `typeid' token.  */
4331         cp_lexer_consume_token (parser->lexer);
4332         /* Look for the `(' token.  */
4333         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4334         /* Types cannot be defined in a `typeid' expression.  */
4335         saved_message = parser->type_definition_forbidden_message;
4336         parser->type_definition_forbidden_message
4337           = "types may not be defined in a `typeid\' expression";
4338         /* We can't be sure yet whether we're looking at a type-id or an
4339            expression.  */
4340         cp_parser_parse_tentatively (parser);
4341         /* Try a type-id first.  */
4342         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4343         parser->in_type_id_in_expr_p = true;
4344         type = cp_parser_type_id (parser);
4345         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4346         /* Look for the `)' token.  Otherwise, we can't be sure that
4347            we're not looking at an expression: consider `typeid (int
4348            (3))', for example.  */
4349         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4350         /* If all went well, simply lookup the type-id.  */
4351         if (cp_parser_parse_definitely (parser))
4352           postfix_expression = get_typeid (type);
4353         /* Otherwise, fall back to the expression variant.  */
4354         else
4355           {
4356             tree expression;
4357
4358             /* Look for an expression.  */
4359             expression = cp_parser_expression (parser, /*cast_p=*/false);
4360             /* Compute its typeid.  */
4361             postfix_expression = build_typeid (expression);
4362             /* Look for the `)' token.  */
4363             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4364           }
4365         /* Restore the saved message.  */
4366         parser->type_definition_forbidden_message = saved_message;
4367         /* `typeid' may not appear in an integral constant expression.  */
4368         if (cp_parser_non_integral_constant_expression(parser,
4369                                                        "`typeid' operator"))
4370           return error_mark_node;
4371       }
4372       break;
4373
4374     case RID_TYPENAME:
4375       {
4376         tree type;
4377         /* The syntax permitted here is the same permitted for an
4378            elaborated-type-specifier.  */
4379         type = cp_parser_elaborated_type_specifier (parser,
4380                                                     /*is_friend=*/false,
4381                                                     /*is_declaration=*/false);
4382         postfix_expression = cp_parser_functional_cast (parser, type);
4383       }
4384       break;
4385
4386     default:
4387       {
4388         tree type;
4389
4390         /* If the next thing is a simple-type-specifier, we may be
4391            looking at a functional cast.  We could also be looking at
4392            an id-expression.  So, we try the functional cast, and if
4393            that doesn't work we fall back to the primary-expression.  */
4394         cp_parser_parse_tentatively (parser);
4395         /* Look for the simple-type-specifier.  */
4396         type = cp_parser_simple_type_specifier (parser,
4397                                                 /*decl_specs=*/NULL,
4398                                                 CP_PARSER_FLAGS_NONE);
4399         /* Parse the cast itself.  */
4400         if (!cp_parser_error_occurred (parser))
4401           postfix_expression
4402             = cp_parser_functional_cast (parser, type);
4403         /* If that worked, we're done.  */
4404         if (cp_parser_parse_definitely (parser))
4405           break;
4406
4407         /* If the functional-cast didn't work out, try a
4408            compound-literal.  */
4409         if (cp_parser_allow_gnu_extensions_p (parser)
4410             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4411           {
4412             VEC(constructor_elt,gc) *initializer_list = NULL;
4413             bool saved_in_type_id_in_expr_p;
4414
4415             cp_parser_parse_tentatively (parser);
4416             /* Consume the `('.  */
4417             cp_lexer_consume_token (parser->lexer);
4418             /* Parse the type.  */
4419             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4420             parser->in_type_id_in_expr_p = true;
4421             type = cp_parser_type_id (parser);
4422             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4423             /* Look for the `)'.  */
4424             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4425             /* Look for the `{'.  */
4426             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4427             /* If things aren't going well, there's no need to
4428                keep going.  */
4429             if (!cp_parser_error_occurred (parser))
4430               {
4431                 bool non_constant_p;
4432                 /* Parse the initializer-list.  */
4433                 initializer_list
4434                   = cp_parser_initializer_list (parser, &non_constant_p);
4435                 /* Allow a trailing `,'.  */
4436                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4437                   cp_lexer_consume_token (parser->lexer);
4438                 /* Look for the final `}'.  */
4439                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4440               }
4441             /* If that worked, we're definitely looking at a
4442                compound-literal expression.  */
4443             if (cp_parser_parse_definitely (parser))
4444               {
4445                 /* Warn the user that a compound literal is not
4446                    allowed in standard C++.  */
4447                 if (pedantic)
4448                   pedwarn ("ISO C++ forbids compound-literals");
4449                 /* For simplicity, we disallow compound literals in
4450                    constant-expressions.  We could
4451                    allow compound literals of integer type, whose
4452                    initializer was a constant, in constant
4453                    expressions.  Permitting that usage, as a further
4454                    extension, would not change the meaning of any
4455                    currently accepted programs.  (Of course, as
4456                    compound literals are not part of ISO C++, the
4457                    standard has nothing to say.)  */
4458                 if (cp_parser_non_integral_constant_expression 
4459                     (parser, "non-constant compound literals"))
4460                   {
4461                     postfix_expression = error_mark_node;
4462                     break;
4463                   }
4464                 /* Form the representation of the compound-literal.  */
4465                 postfix_expression
4466                   = finish_compound_literal (type, initializer_list);
4467                 break;
4468               }
4469           }
4470
4471         /* It must be a primary-expression.  */
4472         postfix_expression
4473           = cp_parser_primary_expression (parser, address_p, cast_p,
4474                                           /*template_arg_p=*/false,
4475                                           &idk);
4476       }
4477       break;
4478     }
4479
4480   /* Keep looping until the postfix-expression is complete.  */
4481   while (true)
4482     {
4483       if (idk == CP_ID_KIND_UNQUALIFIED
4484           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4485           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4486         /* It is not a Koenig lookup function call.  */
4487         postfix_expression
4488           = unqualified_name_lookup_error (postfix_expression);
4489
4490       /* Peek at the next token.  */
4491       token = cp_lexer_peek_token (parser->lexer);
4492
4493       switch (token->type)
4494         {
4495         case CPP_OPEN_SQUARE:
4496           postfix_expression
4497             = cp_parser_postfix_open_square_expression (parser,
4498                                                         postfix_expression,
4499                                                         false);
4500           idk = CP_ID_KIND_NONE;
4501           break;
4502
4503         case CPP_OPEN_PAREN:
4504           /* postfix-expression ( expression-list [opt] ) */
4505           {
4506             bool koenig_p;
4507             bool is_builtin_constant_p;
4508             bool saved_integral_constant_expression_p = false;
4509             bool saved_non_integral_constant_expression_p = false;
4510             tree args;
4511
4512             is_builtin_constant_p
4513               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4514             if (is_builtin_constant_p)
4515               {
4516                 /* The whole point of __builtin_constant_p is to allow
4517                    non-constant expressions to appear as arguments.  */
4518                 saved_integral_constant_expression_p
4519                   = parser->integral_constant_expression_p;
4520                 saved_non_integral_constant_expression_p
4521                   = parser->non_integral_constant_expression_p;
4522                 parser->integral_constant_expression_p = false;
4523               }
4524             args = (cp_parser_parenthesized_expression_list
4525                     (parser, /*is_attribute_list=*/false,
4526                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4527                      /*non_constant_p=*/NULL));
4528             if (is_builtin_constant_p)
4529               {
4530                 parser->integral_constant_expression_p
4531                   = saved_integral_constant_expression_p;
4532                 parser->non_integral_constant_expression_p
4533                   = saved_non_integral_constant_expression_p;
4534               }
4535
4536             if (args == error_mark_node)
4537               {
4538                 postfix_expression = error_mark_node;
4539                 break;
4540               }
4541
4542             /* Function calls are not permitted in
4543                constant-expressions.  */
4544             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4545                 && cp_parser_non_integral_constant_expression (parser,
4546                                                                "a function call"))
4547               {
4548                 postfix_expression = error_mark_node;
4549                 break;
4550               }
4551
4552             koenig_p = false;
4553             if (idk == CP_ID_KIND_UNQUALIFIED)
4554               {
4555                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4556                   {
4557                     if (args)
4558                       {
4559                         koenig_p = true;
4560                         postfix_expression
4561                           = perform_koenig_lookup (postfix_expression, args);
4562                       }
4563                     else
4564                       postfix_expression
4565                         = unqualified_fn_lookup_error (postfix_expression);
4566                   }
4567                 /* We do not perform argument-dependent lookup if
4568                    normal lookup finds a non-function, in accordance
4569                    with the expected resolution of DR 218.  */
4570                 else if (args && is_overloaded_fn (postfix_expression))
4571                   {
4572                     tree fn = get_first_fn (postfix_expression);
4573
4574                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4575                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4576
4577                     /* Only do argument dependent lookup if regular
4578                        lookup does not find a set of member functions.
4579                        [basic.lookup.koenig]/2a  */
4580                     if (!DECL_FUNCTION_MEMBER_P (fn))
4581                       {
4582                         koenig_p = true;
4583                         postfix_expression
4584                           = perform_koenig_lookup (postfix_expression, args);
4585                       }
4586                   }
4587               }
4588
4589             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4590               {
4591                 tree instance = TREE_OPERAND (postfix_expression, 0);
4592                 tree fn = TREE_OPERAND (postfix_expression, 1);
4593
4594                 if (processing_template_decl
4595                     && (type_dependent_expression_p (instance)
4596                         || (!BASELINK_P (fn)
4597                             && TREE_CODE (fn) != FIELD_DECL)
4598                         || type_dependent_expression_p (fn)
4599                         || any_type_dependent_arguments_p (args)))
4600                   {
4601                     postfix_expression
4602                       = build_nt_call_list (postfix_expression, args);
4603                     break;
4604                   }
4605
4606                 if (BASELINK_P (fn))
4607                   postfix_expression
4608                     = (build_new_method_call
4609                        (instance, fn, args, NULL_TREE,
4610                         (idk == CP_ID_KIND_QUALIFIED
4611                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4612                         /*fn_p=*/NULL));
4613                 else
4614                   postfix_expression
4615                     = finish_call_expr (postfix_expression, args,
4616                                         /*disallow_virtual=*/false,
4617                                         /*koenig_p=*/false);
4618               }
4619             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4620                      || TREE_CODE (postfix_expression) == MEMBER_REF
4621                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4622               postfix_expression = (build_offset_ref_call_from_tree
4623                                     (postfix_expression, args));
4624             else if (idk == CP_ID_KIND_QUALIFIED)
4625               /* A call to a static class member, or a namespace-scope
4626                  function.  */
4627               postfix_expression
4628                 = finish_call_expr (postfix_expression, args,
4629                                     /*disallow_virtual=*/true,
4630                                     koenig_p);
4631             else
4632               /* All other function calls.  */
4633               postfix_expression
4634                 = finish_call_expr (postfix_expression, args,
4635                                     /*disallow_virtual=*/false,
4636                                     koenig_p);
4637
4638             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4639             idk = CP_ID_KIND_NONE;
4640           }
4641           break;
4642
4643         case CPP_DOT:
4644         case CPP_DEREF:
4645           /* postfix-expression . template [opt] id-expression
4646              postfix-expression . pseudo-destructor-name
4647              postfix-expression -> template [opt] id-expression
4648              postfix-expression -> pseudo-destructor-name */
4649
4650           /* Consume the `.' or `->' operator.  */
4651           cp_lexer_consume_token (parser->lexer);
4652
4653           postfix_expression
4654             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4655                                                       postfix_expression,
4656                                                       false, &idk);
4657           break;
4658
4659         case CPP_PLUS_PLUS:
4660           /* postfix-expression ++  */
4661           /* Consume the `++' token.  */
4662           cp_lexer_consume_token (parser->lexer);
4663           /* Generate a representation for the complete expression.  */
4664           postfix_expression
4665             = finish_increment_expr (postfix_expression,
4666                                      POSTINCREMENT_EXPR);
4667           /* Increments may not appear in constant-expressions.  */
4668           if (cp_parser_non_integral_constant_expression (parser,
4669                                                           "an increment"))
4670             postfix_expression = error_mark_node;
4671           idk = CP_ID_KIND_NONE;
4672           break;
4673
4674         case CPP_MINUS_MINUS:
4675           /* postfix-expression -- */
4676           /* Consume the `--' token.  */
4677           cp_lexer_consume_token (parser->lexer);
4678           /* Generate a representation for the complete expression.  */
4679           postfix_expression
4680             = finish_increment_expr (postfix_expression,
4681                                      POSTDECREMENT_EXPR);
4682           /* Decrements may not appear in constant-expressions.  */
4683           if (cp_parser_non_integral_constant_expression (parser,
4684                                                           "a decrement"))
4685             postfix_expression = error_mark_node;
4686           idk = CP_ID_KIND_NONE;
4687           break;
4688
4689         default:
4690           return postfix_expression;
4691         }
4692     }
4693
4694   /* We should never get here.  */
4695   gcc_unreachable ();
4696   return error_mark_node;
4697 }
4698
4699 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4700    by cp_parser_builtin_offsetof.  We're looking for
4701
4702      postfix-expression [ expression ]
4703
4704    FOR_OFFSETOF is set if we're being called in that context, which
4705    changes how we deal with integer constant expressions.  */
4706
4707 static tree
4708 cp_parser_postfix_open_square_expression (cp_parser *parser,
4709                                           tree postfix_expression,
4710                                           bool for_offsetof)
4711 {
4712   tree index;
4713
4714   /* Consume the `[' token.  */
4715   cp_lexer_consume_token (parser->lexer);
4716
4717   /* Parse the index expression.  */
4718   /* ??? For offsetof, there is a question of what to allow here.  If
4719      offsetof is not being used in an integral constant expression context,
4720      then we *could* get the right answer by computing the value at runtime.
4721      If we are in an integral constant expression context, then we might
4722      could accept any constant expression; hard to say without analysis.
4723      Rather than open the barn door too wide right away, allow only integer
4724      constant expressions here.  */
4725   if (for_offsetof)
4726     index = cp_parser_constant_expression (parser, false, NULL);
4727   else
4728     index = cp_parser_expression (parser, /*cast_p=*/false);
4729
4730   /* Look for the closing `]'.  */
4731   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4732
4733   /* Build the ARRAY_REF.  */
4734   postfix_expression = grok_array_decl (postfix_expression, index);
4735
4736   /* When not doing offsetof, array references are not permitted in
4737      constant-expressions.  */
4738   if (!for_offsetof
4739       && (cp_parser_non_integral_constant_expression
4740           (parser, "an array reference")))
4741     postfix_expression = error_mark_node;
4742
4743   return postfix_expression;
4744 }
4745
4746 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4747    by cp_parser_builtin_offsetof.  We're looking for
4748
4749      postfix-expression . template [opt] id-expression
4750      postfix-expression . pseudo-destructor-name
4751      postfix-expression -> template [opt] id-expression
4752      postfix-expression -> pseudo-destructor-name
4753
4754    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4755    limits what of the above we'll actually accept, but nevermind.
4756    TOKEN_TYPE is the "." or "->" token, which will already have been
4757    removed from the stream.  */
4758
4759 static tree
4760 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4761                                         enum cpp_ttype token_type,
4762                                         tree postfix_expression,
4763                                         bool for_offsetof, cp_id_kind *idk)
4764 {
4765   tree name;
4766   bool dependent_p;
4767   bool pseudo_destructor_p;
4768   tree scope = NULL_TREE;
4769
4770   /* If this is a `->' operator, dereference the pointer.  */
4771   if (token_type == CPP_DEREF)
4772     postfix_expression = build_x_arrow (postfix_expression);
4773   /* Check to see whether or not the expression is type-dependent.  */
4774   dependent_p = type_dependent_expression_p (postfix_expression);
4775   /* The identifier following the `->' or `.' is not qualified.  */
4776   parser->scope = NULL_TREE;
4777   parser->qualifying_scope = NULL_TREE;
4778   parser->object_scope = NULL_TREE;
4779   *idk = CP_ID_KIND_NONE;
4780   /* Enter the scope corresponding to the type of the object
4781      given by the POSTFIX_EXPRESSION.  */
4782   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4783     {
4784       scope = TREE_TYPE (postfix_expression);
4785       /* According to the standard, no expression should ever have
4786          reference type.  Unfortunately, we do not currently match
4787          the standard in this respect in that our internal representation
4788          of an expression may have reference type even when the standard
4789          says it does not.  Therefore, we have to manually obtain the
4790          underlying type here.  */
4791       scope = non_reference (scope);
4792       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4793       if (scope == unknown_type_node)
4794         {
4795           error ("%qE does not have class type", postfix_expression);
4796           scope = NULL_TREE;
4797         }
4798       else
4799         scope = complete_type_or_else (scope, NULL_TREE);
4800       /* Let the name lookup machinery know that we are processing a
4801          class member access expression.  */
4802       parser->context->object_type = scope;
4803       /* If something went wrong, we want to be able to discern that case,
4804          as opposed to the case where there was no SCOPE due to the type
4805          of expression being dependent.  */
4806       if (!scope)
4807         scope = error_mark_node;
4808       /* If the SCOPE was erroneous, make the various semantic analysis
4809          functions exit quickly -- and without issuing additional error
4810          messages.  */
4811       if (scope == error_mark_node)
4812         postfix_expression = error_mark_node;
4813     }
4814
4815   /* Assume this expression is not a pseudo-destructor access.  */
4816   pseudo_destructor_p = false;
4817
4818   /* If the SCOPE is a scalar type, then, if this is a valid program,
4819      we must be looking at a pseudo-destructor-name.  */
4820   if (scope && SCALAR_TYPE_P (scope))
4821     {
4822       tree s;
4823       tree type;
4824
4825       cp_parser_parse_tentatively (parser);
4826       /* Parse the pseudo-destructor-name.  */
4827       s = NULL_TREE;
4828       cp_parser_pseudo_destructor_name (parser, &s, &type);
4829       if (cp_parser_parse_definitely (parser))
4830         {
4831           pseudo_destructor_p = true;
4832           postfix_expression
4833             = finish_pseudo_destructor_expr (postfix_expression,
4834                                              s, TREE_TYPE (type));
4835         }
4836     }
4837
4838   if (!pseudo_destructor_p)
4839     {
4840       /* If the SCOPE is not a scalar type, we are looking at an
4841          ordinary class member access expression, rather than a
4842          pseudo-destructor-name.  */
4843       bool template_p;
4844       /* Parse the id-expression.  */
4845       name = (cp_parser_id_expression
4846               (parser,
4847                cp_parser_optional_template_keyword (parser),
4848                /*check_dependency_p=*/true,
4849                &template_p,
4850                /*declarator_p=*/false,
4851                /*optional_p=*/false));
4852       /* In general, build a SCOPE_REF if the member name is qualified.
4853          However, if the name was not dependent and has already been
4854          resolved; there is no need to build the SCOPE_REF.  For example;
4855
4856              struct X { void f(); };
4857              template <typename T> void f(T* t) { t->X::f(); }
4858
4859          Even though "t" is dependent, "X::f" is not and has been resolved
4860          to a BASELINK; there is no need to include scope information.  */
4861
4862       /* But we do need to remember that there was an explicit scope for
4863          virtual function calls.  */
4864       if (parser->scope)
4865         *idk = CP_ID_KIND_QUALIFIED;
4866
4867       /* If the name is a template-id that names a type, we will get a
4868          TYPE_DECL here.  That is invalid code.  */
4869       if (TREE_CODE (name) == TYPE_DECL)
4870         {
4871           error ("invalid use of %qD", name);
4872           postfix_expression = error_mark_node;
4873         }
4874       else
4875         {
4876           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4877             {
4878               name = build_qualified_name (/*type=*/NULL_TREE,
4879                                            parser->scope,
4880                                            name,
4881                                            template_p);
4882               parser->scope = NULL_TREE;
4883               parser->qualifying_scope = NULL_TREE;
4884               parser->object_scope = NULL_TREE;
4885             }
4886           if (scope && name && BASELINK_P (name))
4887             adjust_result_of_qualified_name_lookup
4888               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4889           postfix_expression
4890             = finish_class_member_access_expr (postfix_expression, name,
4891                                                template_p);
4892         }
4893     }
4894
4895   /* We no longer need to look up names in the scope of the object on
4896      the left-hand side of the `.' or `->' operator.  */
4897   parser->context->object_type = NULL_TREE;
4898
4899   /* Outside of offsetof, these operators may not appear in
4900      constant-expressions.  */
4901   if (!for_offsetof
4902       && (cp_parser_non_integral_constant_expression
4903           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4904     postfix_expression = error_mark_node;
4905
4906   return postfix_expression;
4907 }
4908
4909 /* Parse a parenthesized expression-list.
4910
4911    expression-list:
4912      assignment-expression
4913      expression-list, assignment-expression
4914
4915    attribute-list:
4916      expression-list
4917      identifier
4918      identifier, expression-list
4919
4920    CAST_P is true if this expression is the target of a cast.
4921
4922    ALLOW_EXPANSION_P is true if this expression allows expansion of an
4923    argument pack.
4924
4925    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4926    representation of an assignment-expression.  Note that a TREE_LIST
4927    is returned even if there is only a single expression in the list.
4928    error_mark_node is returned if the ( and or ) are
4929    missing. NULL_TREE is returned on no expressions. The parentheses
4930    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4931    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4932    indicates whether or not all of the expressions in the list were
4933    constant.  */
4934
4935 static tree
4936 cp_parser_parenthesized_expression_list (cp_parser* parser,
4937                                          bool is_attribute_list,
4938                                          bool cast_p,
4939                                          bool allow_expansion_p,
4940                                          bool *non_constant_p)
4941 {
4942   tree expression_list = NULL_TREE;
4943   bool fold_expr_p = is_attribute_list;
4944   tree identifier = NULL_TREE;
4945
4946   /* Assume all the expressions will be constant.  */
4947   if (non_constant_p)
4948     *non_constant_p = false;
4949
4950   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4951     return error_mark_node;
4952
4953   /* Consume expressions until there are no more.  */
4954   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4955     while (true)
4956       {
4957         tree expr;
4958
4959         /* At the beginning of attribute lists, check to see if the
4960            next token is an identifier.  */
4961         if (is_attribute_list
4962             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4963           {
4964             cp_token *token;
4965
4966             /* Consume the identifier.  */
4967             token = cp_lexer_consume_token (parser->lexer);
4968             /* Save the identifier.  */
4969             identifier = token->u.value;
4970           }
4971         else
4972           {
4973             /* Parse the next assignment-expression.  */
4974             if (non_constant_p)
4975               {
4976                 bool expr_non_constant_p;
4977                 expr = (cp_parser_constant_expression
4978                         (parser, /*allow_non_constant_p=*/true,
4979                          &expr_non_constant_p));
4980                 if (expr_non_constant_p)
4981                   *non_constant_p = true;
4982               }
4983             else
4984               expr = cp_parser_assignment_expression (parser, cast_p);
4985
4986             if (fold_expr_p)
4987               expr = fold_non_dependent_expr (expr);
4988
4989             /* If we have an ellipsis, then this is an expression
4990                expansion.  */
4991             if (allow_expansion_p
4992                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4993               {
4994                 /* Consume the `...'.  */
4995                 cp_lexer_consume_token (parser->lexer);
4996
4997                 /* Build the argument pack.  */
4998                 expr = make_pack_expansion (expr);
4999               }
5000
5001              /* Add it to the list.  We add error_mark_node
5002                 expressions to the list, so that we can still tell if
5003                 the correct form for a parenthesized expression-list
5004                 is found. That gives better errors.  */
5005             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5006
5007             if (expr == error_mark_node)
5008               goto skip_comma;
5009           }
5010
5011         /* After the first item, attribute lists look the same as
5012            expression lists.  */
5013         is_attribute_list = false;
5014
5015       get_comma:;
5016         /* If the next token isn't a `,', then we are done.  */
5017         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5018           break;
5019
5020         /* Otherwise, consume the `,' and keep going.  */
5021         cp_lexer_consume_token (parser->lexer);
5022       }
5023
5024   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5025     {
5026       int ending;
5027
5028     skip_comma:;
5029       /* We try and resync to an unnested comma, as that will give the
5030          user better diagnostics.  */
5031       ending = cp_parser_skip_to_closing_parenthesis (parser,
5032                                                       /*recovering=*/true,
5033                                                       /*or_comma=*/true,
5034                                                       /*consume_paren=*/true);
5035       if (ending < 0)
5036         goto get_comma;
5037       if (!ending)
5038         return error_mark_node;
5039     }
5040
5041   /* We built up the list in reverse order so we must reverse it now.  */
5042   expression_list = nreverse (expression_list);
5043   if (identifier)
5044     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5045
5046   return expression_list;
5047 }
5048
5049 /* Parse a pseudo-destructor-name.
5050
5051    pseudo-destructor-name:
5052      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5053      :: [opt] nested-name-specifier template template-id :: ~ type-name
5054      :: [opt] nested-name-specifier [opt] ~ type-name
5055
5056    If either of the first two productions is used, sets *SCOPE to the
5057    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5058    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5059    or ERROR_MARK_NODE if the parse fails.  */
5060
5061 static void
5062 cp_parser_pseudo_destructor_name (cp_parser* parser,
5063                                   tree* scope,
5064                                   tree* type)
5065 {
5066   bool nested_name_specifier_p;
5067
5068   /* Assume that things will not work out.  */
5069   *type = error_mark_node;
5070
5071   /* Look for the optional `::' operator.  */
5072   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5073   /* Look for the optional nested-name-specifier.  */
5074   nested_name_specifier_p
5075     = (cp_parser_nested_name_specifier_opt (parser,
5076                                             /*typename_keyword_p=*/false,
5077                                             /*check_dependency_p=*/true,
5078                                             /*type_p=*/false,
5079                                             /*is_declaration=*/true)
5080        != NULL_TREE);
5081   /* Now, if we saw a nested-name-specifier, we might be doing the
5082      second production.  */
5083   if (nested_name_specifier_p
5084       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5085     {
5086       /* Consume the `template' keyword.  */
5087       cp_lexer_consume_token (parser->lexer);
5088       /* Parse the template-id.  */
5089       cp_parser_template_id (parser,
5090                              /*template_keyword_p=*/true,
5091                              /*check_dependency_p=*/false,
5092                              /*is_declaration=*/true);
5093       /* Look for the `::' token.  */
5094       cp_parser_require (parser, CPP_SCOPE, "`::'");
5095     }
5096   /* If the next token is not a `~', then there might be some
5097      additional qualification.  */
5098   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5099     {
5100       /* Look for the type-name.  */
5101       *scope = TREE_TYPE (cp_parser_type_name (parser));
5102
5103       if (*scope == error_mark_node)
5104         return;
5105
5106       /* If we don't have ::~, then something has gone wrong.  Since
5107          the only caller of this function is looking for something
5108          after `.' or `->' after a scalar type, most likely the
5109          program is trying to get a member of a non-aggregate
5110          type.  */
5111       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5112           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5113         {
5114           cp_parser_error (parser, "request for member of non-aggregate type");
5115           return;
5116         }
5117
5118       /* Look for the `::' token.  */
5119       cp_parser_require (parser, CPP_SCOPE, "`::'");
5120     }
5121   else
5122     *scope = NULL_TREE;
5123
5124   /* Look for the `~'.  */
5125   cp_parser_require (parser, CPP_COMPL, "`~'");
5126   /* Look for the type-name again.  We are not responsible for
5127      checking that it matches the first type-name.  */
5128   *type = cp_parser_type_name (parser);
5129 }
5130
5131 /* Parse a unary-expression.
5132
5133    unary-expression:
5134      postfix-expression
5135      ++ cast-expression
5136      -- cast-expression
5137      unary-operator cast-expression
5138      sizeof unary-expression
5139      sizeof ( type-id )
5140      new-expression
5141      delete-expression
5142
5143    GNU Extensions:
5144
5145    unary-expression:
5146      __extension__ cast-expression
5147      __alignof__ unary-expression
5148      __alignof__ ( type-id )
5149      __real__ cast-expression
5150      __imag__ cast-expression
5151      && identifier
5152
5153    ADDRESS_P is true iff the unary-expression is appearing as the
5154    operand of the `&' operator.   CAST_P is true if this expression is
5155    the target of a cast.
5156
5157    Returns a representation of the expression.  */
5158
5159 static tree
5160 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5161 {
5162   cp_token *token;
5163   enum tree_code unary_operator;
5164
5165   /* Peek at the next token.  */
5166   token = cp_lexer_peek_token (parser->lexer);
5167   /* Some keywords give away the kind of expression.  */
5168   if (token->type == CPP_KEYWORD)
5169     {
5170       enum rid keyword = token->keyword;
5171
5172       switch (keyword)
5173         {
5174         case RID_ALIGNOF:
5175         case RID_SIZEOF:
5176           {
5177             tree operand;
5178             enum tree_code op;
5179
5180             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5181             /* Consume the token.  */
5182             cp_lexer_consume_token (parser->lexer);
5183             /* Parse the operand.  */
5184             operand = cp_parser_sizeof_operand (parser, keyword);
5185
5186             if (TYPE_P (operand))
5187               return cxx_sizeof_or_alignof_type (operand, op, true);
5188             else
5189               return cxx_sizeof_or_alignof_expr (operand, op);
5190           }
5191
5192         case RID_NEW:
5193           return cp_parser_new_expression (parser);
5194
5195         case RID_DELETE:
5196           return cp_parser_delete_expression (parser);
5197
5198         case RID_EXTENSION:
5199           {
5200             /* The saved value of the PEDANTIC flag.  */
5201             int saved_pedantic;
5202             tree expr;
5203
5204             /* Save away the PEDANTIC flag.  */
5205             cp_parser_extension_opt (parser, &saved_pedantic);
5206             /* Parse the cast-expression.  */
5207             expr = cp_parser_simple_cast_expression (parser);
5208             /* Restore the PEDANTIC flag.  */
5209             pedantic = saved_pedantic;
5210
5211             return expr;
5212           }
5213
5214         case RID_REALPART:
5215         case RID_IMAGPART:
5216           {
5217             tree expression;
5218
5219             /* Consume the `__real__' or `__imag__' token.  */
5220             cp_lexer_consume_token (parser->lexer);
5221             /* Parse the cast-expression.  */
5222             expression = cp_parser_simple_cast_expression (parser);
5223             /* Create the complete representation.  */
5224             return build_x_unary_op ((keyword == RID_REALPART
5225                                       ? REALPART_EXPR : IMAGPART_EXPR),
5226                                      expression);
5227           }
5228           break;
5229
5230         default:
5231           break;
5232         }
5233     }
5234
5235   /* Look for the `:: new' and `:: delete', which also signal the
5236      beginning of a new-expression, or delete-expression,
5237      respectively.  If the next token is `::', then it might be one of
5238      these.  */
5239   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5240     {
5241       enum rid keyword;
5242
5243       /* See if the token after the `::' is one of the keywords in
5244          which we're interested.  */
5245       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5246       /* If it's `new', we have a new-expression.  */
5247       if (keyword == RID_NEW)
5248         return cp_parser_new_expression (parser);
5249       /* Similarly, for `delete'.  */
5250       else if (keyword == RID_DELETE)
5251         return cp_parser_delete_expression (parser);
5252     }
5253
5254   /* Look for a unary operator.  */
5255   unary_operator = cp_parser_unary_operator (token);
5256   /* The `++' and `--' operators can be handled similarly, even though
5257      they are not technically unary-operators in the grammar.  */
5258   if (unary_operator == ERROR_MARK)
5259     {
5260       if (token->type == CPP_PLUS_PLUS)
5261         unary_operator = PREINCREMENT_EXPR;
5262       else if (token->type == CPP_MINUS_MINUS)
5263         unary_operator = PREDECREMENT_EXPR;
5264       /* Handle the GNU address-of-label extension.  */
5265       else if (cp_parser_allow_gnu_extensions_p (parser)
5266                && token->type == CPP_AND_AND)
5267         {
5268           tree identifier;
5269
5270           /* Consume the '&&' token.  */
5271           cp_lexer_consume_token (parser->lexer);
5272           /* Look for the identifier.  */
5273           identifier = cp_parser_identifier (parser);
5274           /* Create an expression representing the address.  */
5275           return finish_label_address_expr (identifier);
5276         }
5277     }
5278   if (unary_operator != ERROR_MARK)
5279     {
5280       tree cast_expression;
5281       tree expression = error_mark_node;
5282       const char *non_constant_p = NULL;
5283
5284       /* Consume the operator token.  */
5285       token = cp_lexer_consume_token (parser->lexer);
5286       /* Parse the cast-expression.  */
5287       cast_expression
5288         = cp_parser_cast_expression (parser,
5289                                      unary_operator == ADDR_EXPR,
5290                                      /*cast_p=*/false);
5291       /* Now, build an appropriate representation.  */
5292       switch (unary_operator)
5293         {
5294         case INDIRECT_REF:
5295           non_constant_p = "`*'";
5296           expression = build_x_indirect_ref (cast_expression, "unary *");
5297           break;
5298
5299         case ADDR_EXPR:
5300           non_constant_p = "`&'";
5301           /* Fall through.  */
5302         case BIT_NOT_EXPR:
5303           expression = build_x_unary_op (unary_operator, cast_expression);
5304           break;
5305
5306         case PREINCREMENT_EXPR:
5307         case PREDECREMENT_EXPR:
5308           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5309                             ? "`++'" : "`--'");
5310           /* Fall through.  */
5311         case UNARY_PLUS_EXPR:
5312         case NEGATE_EXPR:
5313         case TRUTH_NOT_EXPR:
5314           expression = finish_unary_op_expr (unary_operator, cast_expression);
5315           break;
5316
5317         default:
5318           gcc_unreachable ();
5319         }
5320
5321       if (non_constant_p
5322           && cp_parser_non_integral_constant_expression (parser,
5323                                                          non_constant_p))
5324         expression = error_mark_node;
5325
5326       return expression;
5327     }
5328
5329   return cp_parser_postfix_expression (parser, address_p, cast_p);
5330 }
5331
5332 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5333    unary-operator, the corresponding tree code is returned.  */
5334
5335 static enum tree_code
5336 cp_parser_unary_operator (cp_token* token)
5337 {
5338   switch (token->type)
5339     {
5340     case CPP_MULT:
5341       return INDIRECT_REF;
5342
5343     case CPP_AND:
5344       return ADDR_EXPR;
5345
5346     case CPP_PLUS:
5347       return UNARY_PLUS_EXPR;
5348
5349     case CPP_MINUS:
5350       return NEGATE_EXPR;
5351
5352     case CPP_NOT:
5353       return TRUTH_NOT_EXPR;
5354
5355     case CPP_COMPL:
5356       return BIT_NOT_EXPR;
5357
5358     default:
5359       return ERROR_MARK;
5360     }
5361 }
5362
5363 /* Parse a new-expression.
5364
5365    new-expression:
5366      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5367      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5368
5369    Returns a representation of the expression.  */
5370
5371 static tree
5372 cp_parser_new_expression (cp_parser* parser)
5373 {
5374   bool global_scope_p;
5375   tree placement;
5376   tree type;
5377   tree initializer;
5378   tree nelts;
5379
5380   /* Look for the optional `::' operator.  */
5381   global_scope_p
5382     = (cp_parser_global_scope_opt (parser,
5383                                    /*current_scope_valid_p=*/false)
5384        != NULL_TREE);
5385   /* Look for the `new' operator.  */
5386   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5387   /* There's no easy way to tell a new-placement from the
5388      `( type-id )' construct.  */
5389   cp_parser_parse_tentatively (parser);
5390   /* Look for a new-placement.  */
5391   placement = cp_parser_new_placement (parser);
5392   /* If that didn't work out, there's no new-placement.  */
5393   if (!cp_parser_parse_definitely (parser))
5394     placement = NULL_TREE;
5395
5396   /* If the next token is a `(', then we have a parenthesized
5397      type-id.  */
5398   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5399     {
5400       /* Consume the `('.  */
5401       cp_lexer_consume_token (parser->lexer);
5402       /* Parse the type-id.  */
5403       type = cp_parser_type_id (parser);
5404       /* Look for the closing `)'.  */
5405       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5406       /* There should not be a direct-new-declarator in this production,
5407          but GCC used to allowed this, so we check and emit a sensible error
5408          message for this case.  */
5409       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5410         {
5411           error ("array bound forbidden after parenthesized type-id");
5412           inform ("try removing the parentheses around the type-id");
5413           cp_parser_direct_new_declarator (parser);
5414         }
5415       nelts = NULL_TREE;
5416     }
5417   /* Otherwise, there must be a new-type-id.  */
5418   else
5419     type = cp_parser_new_type_id (parser, &nelts);
5420
5421   /* If the next token is a `(', then we have a new-initializer.  */
5422   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5423     initializer = cp_parser_new_initializer (parser);
5424   else
5425     initializer = NULL_TREE;
5426
5427   /* A new-expression may not appear in an integral constant
5428      expression.  */
5429   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5430     return error_mark_node;
5431
5432   /* Create a representation of the new-expression.  */
5433   return build_new (placement, type, nelts, initializer, global_scope_p);
5434 }
5435
5436 /* Parse a new-placement.
5437
5438    new-placement:
5439      ( expression-list )
5440
5441    Returns the same representation as for an expression-list.  */
5442
5443 static tree
5444 cp_parser_new_placement (cp_parser* parser)
5445 {
5446   tree expression_list;
5447
5448   /* Parse the expression-list.  */
5449   expression_list = (cp_parser_parenthesized_expression_list
5450                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5451                       /*non_constant_p=*/NULL));
5452
5453   return expression_list;
5454 }
5455
5456 /* Parse a new-type-id.
5457
5458    new-type-id:
5459      type-specifier-seq new-declarator [opt]
5460
5461    Returns the TYPE allocated.  If the new-type-id indicates an array
5462    type, *NELTS is set to the number of elements in the last array
5463    bound; the TYPE will not include the last array bound.  */
5464
5465 static tree
5466 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5467 {
5468   cp_decl_specifier_seq type_specifier_seq;
5469   cp_declarator *new_declarator;
5470   cp_declarator *declarator;
5471   cp_declarator *outer_declarator;
5472   const char *saved_message;
5473   tree type;
5474
5475   /* The type-specifier sequence must not contain type definitions.
5476      (It cannot contain declarations of new types either, but if they
5477      are not definitions we will catch that because they are not
5478      complete.)  */
5479   saved_message = parser->type_definition_forbidden_message;
5480   parser->type_definition_forbidden_message
5481     = "types may not be defined in a new-type-id";
5482   /* Parse the type-specifier-seq.  */
5483   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5484                                 &type_specifier_seq);
5485   /* Restore the old message.  */
5486   parser->type_definition_forbidden_message = saved_message;
5487   /* Parse the new-declarator.  */
5488   new_declarator = cp_parser_new_declarator_opt (parser);
5489
5490   /* Determine the number of elements in the last array dimension, if
5491      any.  */
5492   *nelts = NULL_TREE;
5493   /* Skip down to the last array dimension.  */
5494   declarator = new_declarator;
5495   outer_declarator = NULL;
5496   while (declarator && (declarator->kind == cdk_pointer
5497                         || declarator->kind == cdk_ptrmem))
5498     {
5499       outer_declarator = declarator;
5500       declarator = declarator->declarator;
5501     }
5502   while (declarator
5503          && declarator->kind == cdk_array
5504          && declarator->declarator
5505          && declarator->declarator->kind == cdk_array)
5506     {
5507       outer_declarator = declarator;
5508       declarator = declarator->declarator;
5509     }
5510
5511   if (declarator && declarator->kind == cdk_array)
5512     {
5513       *nelts = declarator->u.array.bounds;
5514       if (*nelts == error_mark_node)
5515         *nelts = integer_one_node;
5516
5517       if (outer_declarator)
5518         outer_declarator->declarator = declarator->declarator;
5519       else
5520         new_declarator = NULL;
5521     }
5522
5523   type = groktypename (&type_specifier_seq, new_declarator);
5524   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5525     {
5526       *nelts = array_type_nelts_top (type);
5527       type = TREE_TYPE (type);
5528     }
5529   return type;
5530 }
5531
5532 /* Parse an (optional) new-declarator.
5533
5534    new-declarator:
5535      ptr-operator new-declarator [opt]
5536      direct-new-declarator
5537
5538    Returns the declarator.  */
5539
5540 static cp_declarator *
5541 cp_parser_new_declarator_opt (cp_parser* parser)
5542 {
5543   enum tree_code code;
5544   tree type;
5545   cp_cv_quals cv_quals;
5546
5547   /* We don't know if there's a ptr-operator next, or not.  */
5548   cp_parser_parse_tentatively (parser);
5549   /* Look for a ptr-operator.  */
5550   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5551   /* If that worked, look for more new-declarators.  */
5552   if (cp_parser_parse_definitely (parser))
5553     {
5554       cp_declarator *declarator;
5555
5556       /* Parse another optional declarator.  */
5557       declarator = cp_parser_new_declarator_opt (parser);
5558
5559       return cp_parser_make_indirect_declarator
5560         (code, type, cv_quals, declarator);
5561     }
5562
5563   /* If the next token is a `[', there is a direct-new-declarator.  */
5564   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5565     return cp_parser_direct_new_declarator (parser);
5566
5567   return NULL;
5568 }
5569
5570 /* Parse a direct-new-declarator.
5571
5572    direct-new-declarator:
5573      [ expression ]
5574      direct-new-declarator [constant-expression]
5575
5576    */
5577
5578 static cp_declarator *
5579 cp_parser_direct_new_declarator (cp_parser* parser)
5580 {
5581   cp_declarator *declarator = NULL;
5582
5583   while (true)
5584     {
5585       tree expression;
5586
5587       /* Look for the opening `['.  */
5588       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5589       /* The first expression is not required to be constant.  */
5590       if (!declarator)
5591         {
5592           expression = cp_parser_expression (parser, /*cast_p=*/false);
5593           /* The standard requires that the expression have integral
5594              type.  DR 74 adds enumeration types.  We believe that the
5595              real intent is that these expressions be handled like the
5596              expression in a `switch' condition, which also allows
5597              classes with a single conversion to integral or
5598              enumeration type.  */
5599           if (!processing_template_decl)
5600             {
5601               expression
5602                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5603                                               expression,
5604                                               /*complain=*/true);
5605               if (!expression)
5606                 {
5607                   error ("expression in new-declarator must have integral "
5608                          "or enumeration type");
5609                   expression = error_mark_node;
5610                 }
5611             }
5612         }
5613       /* But all the other expressions must be.  */
5614       else
5615         expression
5616           = cp_parser_constant_expression (parser,
5617                                            /*allow_non_constant=*/false,
5618                                            NULL);
5619       /* Look for the closing `]'.  */
5620       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5621
5622       /* Add this bound to the declarator.  */
5623       declarator = make_array_declarator (declarator, expression);
5624
5625       /* If the next token is not a `[', then there are no more
5626          bounds.  */
5627       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5628         break;
5629     }
5630
5631   return declarator;
5632 }
5633
5634 /* Parse a new-initializer.
5635
5636    new-initializer:
5637      ( expression-list [opt] )
5638
5639    Returns a representation of the expression-list.  If there is no
5640    expression-list, VOID_ZERO_NODE is returned.  */
5641
5642 static tree
5643 cp_parser_new_initializer (cp_parser* parser)
5644 {
5645   tree expression_list;
5646
5647   expression_list = (cp_parser_parenthesized_expression_list
5648                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5649                       /*non_constant_p=*/NULL));
5650   if (!expression_list)
5651     expression_list = void_zero_node;
5652
5653   return expression_list;
5654 }
5655
5656 /* Parse a delete-expression.
5657
5658    delete-expression:
5659      :: [opt] delete cast-expression
5660      :: [opt] delete [ ] cast-expression
5661
5662    Returns a representation of the expression.  */
5663
5664 static tree
5665 cp_parser_delete_expression (cp_parser* parser)
5666 {
5667   bool global_scope_p;
5668   bool array_p;
5669   tree expression;
5670
5671   /* Look for the optional `::' operator.  */
5672   global_scope_p
5673     = (cp_parser_global_scope_opt (parser,
5674                                    /*current_scope_valid_p=*/false)
5675        != NULL_TREE);
5676   /* Look for the `delete' keyword.  */
5677   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5678   /* See if the array syntax is in use.  */
5679   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5680     {
5681       /* Consume the `[' token.  */
5682       cp_lexer_consume_token (parser->lexer);
5683       /* Look for the `]' token.  */
5684       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5685       /* Remember that this is the `[]' construct.  */
5686       array_p = true;
5687     }
5688   else
5689     array_p = false;
5690
5691   /* Parse the cast-expression.  */
5692   expression = cp_parser_simple_cast_expression (parser);
5693
5694   /* A delete-expression may not appear in an integral constant
5695      expression.  */
5696   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5697     return error_mark_node;
5698
5699   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5700 }
5701
5702 /* Parse a cast-expression.
5703
5704    cast-expression:
5705      unary-expression
5706      ( type-id ) cast-expression
5707
5708    ADDRESS_P is true iff the unary-expression is appearing as the
5709    operand of the `&' operator.   CAST_P is true if this expression is
5710    the target of a cast.
5711
5712    Returns a representation of the expression.  */
5713
5714 static tree
5715 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5716 {
5717   /* If it's a `(', then we might be looking at a cast.  */
5718   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5719     {
5720       tree type = NULL_TREE;
5721       tree expr = NULL_TREE;
5722       bool compound_literal_p;
5723       const char *saved_message;
5724
5725       /* There's no way to know yet whether or not this is a cast.
5726          For example, `(int (3))' is a unary-expression, while `(int)
5727          3' is a cast.  So, we resort to parsing tentatively.  */
5728       cp_parser_parse_tentatively (parser);
5729       /* Types may not be defined in a cast.  */
5730       saved_message = parser->type_definition_forbidden_message;
5731       parser->type_definition_forbidden_message
5732         = "types may not be defined in casts";
5733       /* Consume the `('.  */
5734       cp_lexer_consume_token (parser->lexer);
5735       /* A very tricky bit is that `(struct S) { 3 }' is a
5736          compound-literal (which we permit in C++ as an extension).
5737          But, that construct is not a cast-expression -- it is a
5738          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5739          is legal; if the compound-literal were a cast-expression,
5740          you'd need an extra set of parentheses.)  But, if we parse
5741          the type-id, and it happens to be a class-specifier, then we
5742          will commit to the parse at that point, because we cannot
5743          undo the action that is done when creating a new class.  So,
5744          then we cannot back up and do a postfix-expression.
5745
5746          Therefore, we scan ahead to the closing `)', and check to see
5747          if the token after the `)' is a `{'.  If so, we are not
5748          looking at a cast-expression.
5749
5750          Save tokens so that we can put them back.  */
5751       cp_lexer_save_tokens (parser->lexer);
5752       /* Skip tokens until the next token is a closing parenthesis.
5753          If we find the closing `)', and the next token is a `{', then
5754          we are looking at a compound-literal.  */
5755       compound_literal_p
5756         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5757                                                   /*consume_paren=*/true)
5758            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5759       /* Roll back the tokens we skipped.  */
5760       cp_lexer_rollback_tokens (parser->lexer);
5761       /* If we were looking at a compound-literal, simulate an error
5762          so that the call to cp_parser_parse_definitely below will
5763          fail.  */
5764       if (compound_literal_p)
5765         cp_parser_simulate_error (parser);
5766       else
5767         {
5768           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5769           parser->in_type_id_in_expr_p = true;
5770           /* Look for the type-id.  */
5771           type = cp_parser_type_id (parser);
5772           /* Look for the closing `)'.  */
5773           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5774           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5775         }
5776
5777       /* Restore the saved message.  */
5778       parser->type_definition_forbidden_message = saved_message;
5779
5780       /* If ok so far, parse the dependent expression. We cannot be
5781          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5782          ctor of T, but looks like a cast to function returning T
5783          without a dependent expression.  */
5784       if (!cp_parser_error_occurred (parser))
5785         expr = cp_parser_cast_expression (parser,
5786                                           /*address_p=*/false,
5787                                           /*cast_p=*/true);
5788
5789       if (cp_parser_parse_definitely (parser))
5790         {
5791           /* Warn about old-style casts, if so requested.  */
5792           if (warn_old_style_cast
5793               && !in_system_header
5794               && !VOID_TYPE_P (type)
5795               && current_lang_name != lang_name_c)
5796             warning (OPT_Wold_style_cast, "use of old-style cast");
5797
5798           /* Only type conversions to integral or enumeration types
5799              can be used in constant-expressions.  */
5800           if (!cast_valid_in_integral_constant_expression_p (type)
5801               && (cp_parser_non_integral_constant_expression
5802                   (parser,
5803                    "a cast to a type other than an integral or "
5804                    "enumeration type")))
5805             return error_mark_node;
5806
5807           /* Perform the cast.  */
5808           expr = build_c_cast (type, expr);
5809           return expr;
5810         }
5811     }
5812
5813   /* If we get here, then it's not a cast, so it must be a
5814      unary-expression.  */
5815   return cp_parser_unary_expression (parser, address_p, cast_p);
5816 }
5817
5818 /* Parse a binary expression of the general form:
5819
5820    pm-expression:
5821      cast-expression
5822      pm-expression .* cast-expression
5823      pm-expression ->* cast-expression
5824
5825    multiplicative-expression:
5826      pm-expression
5827      multiplicative-expression * pm-expression
5828      multiplicative-expression / pm-expression
5829      multiplicative-expression % pm-expression
5830
5831    additive-expression:
5832      multiplicative-expression
5833      additive-expression + multiplicative-expression
5834      additive-expression - multiplicative-expression
5835
5836    shift-expression:
5837      additive-expression
5838      shift-expression << additive-expression
5839      shift-expression >> additive-expression
5840
5841    relational-expression:
5842      shift-expression
5843      relational-expression < shift-expression
5844      relational-expression > shift-expression
5845      relational-expression <= shift-expression
5846      relational-expression >= shift-expression
5847
5848   GNU Extension:
5849
5850    relational-expression:
5851      relational-expression <? shift-expression
5852      relational-expression >? shift-expression
5853
5854    equality-expression:
5855      relational-expression
5856      equality-expression == relational-expression
5857      equality-expression != relational-expression
5858
5859    and-expression:
5860      equality-expression
5861      and-expression & equality-expression
5862
5863    exclusive-or-expression:
5864      and-expression
5865      exclusive-or-expression ^ and-expression
5866
5867    inclusive-or-expression:
5868      exclusive-or-expression
5869      inclusive-or-expression | exclusive-or-expression
5870
5871    logical-and-expression:
5872      inclusive-or-expression
5873      logical-and-expression && inclusive-or-expression
5874
5875    logical-or-expression:
5876      logical-and-expression
5877      logical-or-expression || logical-and-expression
5878
5879    All these are implemented with a single function like:
5880
5881    binary-expression:
5882      simple-cast-expression
5883      binary-expression <token> binary-expression
5884
5885    CAST_P is true if this expression is the target of a cast.
5886
5887    The binops_by_token map is used to get the tree codes for each <token> type.
5888    binary-expressions are associated according to a precedence table.  */
5889
5890 #define TOKEN_PRECEDENCE(token)                              \
5891 (((token->type == CPP_GREATER                                \
5892    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5893   && !parser->greater_than_is_operator_p)                    \
5894  ? PREC_NOT_OPERATOR                                         \
5895  : binops_by_token[token->type].prec)
5896
5897 static tree
5898 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5899 {
5900   cp_parser_expression_stack stack;
5901   cp_parser_expression_stack_entry *sp = &stack[0];
5902   tree lhs, rhs;
5903   cp_token *token;
5904   enum tree_code tree_type, lhs_type, rhs_type;
5905   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5906   bool overloaded_p;
5907
5908   /* Parse the first expression.  */
5909   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5910   lhs_type = ERROR_MARK;
5911
5912   for (;;)
5913     {
5914       /* Get an operator token.  */
5915       token = cp_lexer_peek_token (parser->lexer);
5916
5917       if (warn_cxx0x_compat
5918           && token->type == CPP_RSHIFT
5919           && !parser->greater_than_is_operator_p)
5920         {
5921           warning (OPT_Wc__0x_compat, 
5922                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
5923                    &token->location);
5924           warning (OPT_Wc__0x_compat, 
5925                    "suggest parentheses around %<>>%> expression");
5926         }
5927
5928       new_prec = TOKEN_PRECEDENCE (token);
5929
5930       /* Popping an entry off the stack means we completed a subexpression:
5931          - either we found a token which is not an operator (`>' where it is not
5932            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5933            will happen repeatedly;
5934          - or, we found an operator which has lower priority.  This is the case
5935            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5936            parsing `3 * 4'.  */
5937       if (new_prec <= prec)
5938         {
5939           if (sp == stack)
5940             break;
5941           else
5942             goto pop;
5943         }
5944
5945      get_rhs:
5946       tree_type = binops_by_token[token->type].tree_type;
5947
5948       /* We used the operator token.  */
5949       cp_lexer_consume_token (parser->lexer);
5950
5951       /* Extract another operand.  It may be the RHS of this expression
5952          or the LHS of a new, higher priority expression.  */
5953       rhs = cp_parser_simple_cast_expression (parser);
5954       rhs_type = ERROR_MARK;
5955
5956       /* Get another operator token.  Look up its precedence to avoid
5957          building a useless (immediately popped) stack entry for common
5958          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5959       token = cp_lexer_peek_token (parser->lexer);
5960       lookahead_prec = TOKEN_PRECEDENCE (token);
5961       if (lookahead_prec > new_prec)
5962         {
5963           /* ... and prepare to parse the RHS of the new, higher priority
5964              expression.  Since precedence levels on the stack are
5965              monotonically increasing, we do not have to care about
5966              stack overflows.  */
5967           sp->prec = prec;
5968           sp->tree_type = tree_type;
5969           sp->lhs = lhs;
5970           sp->lhs_type = lhs_type;
5971           sp++;
5972           lhs = rhs;
5973           lhs_type = rhs_type;
5974           prec = new_prec;
5975           new_prec = lookahead_prec;
5976           goto get_rhs;
5977
5978          pop:
5979           /* If the stack is not empty, we have parsed into LHS the right side
5980              (`4' in the example above) of an expression we had suspended.
5981              We can use the information on the stack to recover the LHS (`3')
5982              from the stack together with the tree code (`MULT_EXPR'), and
5983              the precedence of the higher level subexpression
5984              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5985              which will be used to actually build the additive expression.  */
5986           --sp;
5987           prec = sp->prec;
5988           tree_type = sp->tree_type;
5989           rhs = lhs;
5990           rhs_type = lhs_type;
5991           lhs = sp->lhs;
5992           lhs_type = sp->lhs_type;
5993         }
5994
5995       overloaded_p = false;
5996       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5997                                &overloaded_p);
5998       lhs_type = tree_type;
5999
6000       /* If the binary operator required the use of an overloaded operator,
6001          then this expression cannot be an integral constant-expression.
6002          An overloaded operator can be used even if both operands are
6003          otherwise permissible in an integral constant-expression if at
6004          least one of the operands is of enumeration type.  */
6005
6006       if (overloaded_p
6007           && (cp_parser_non_integral_constant_expression
6008               (parser, "calls to overloaded operators")))
6009         return error_mark_node;
6010     }
6011
6012   return lhs;
6013 }
6014
6015
6016 /* Parse the `? expression : assignment-expression' part of a
6017    conditional-expression.  The LOGICAL_OR_EXPR is the
6018    logical-or-expression that started the conditional-expression.
6019    Returns a representation of the entire conditional-expression.
6020
6021    This routine is used by cp_parser_assignment_expression.
6022
6023      ? expression : assignment-expression
6024
6025    GNU Extensions:
6026
6027      ? : assignment-expression */
6028
6029 static tree
6030 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6031 {
6032   tree expr;
6033   tree assignment_expr;
6034
6035   /* Consume the `?' token.  */
6036   cp_lexer_consume_token (parser->lexer);
6037   if (cp_parser_allow_gnu_extensions_p (parser)
6038       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6039     /* Implicit true clause.  */
6040     expr = NULL_TREE;
6041   else
6042     /* Parse the expression.  */
6043     expr = cp_parser_expression (parser, /*cast_p=*/false);
6044
6045   /* The next token should be a `:'.  */
6046   cp_parser_require (parser, CPP_COLON, "`:'");
6047   /* Parse the assignment-expression.  */
6048   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6049
6050   /* Build the conditional-expression.  */
6051   return build_x_conditional_expr (logical_or_expr,
6052                                    expr,
6053                                    assignment_expr);
6054 }
6055
6056 /* Parse an assignment-expression.
6057
6058    assignment-expression:
6059      conditional-expression
6060      logical-or-expression assignment-operator assignment_expression
6061      throw-expression
6062
6063    CAST_P is true if this expression is the target of a cast.
6064
6065    Returns a representation for the expression.  */
6066
6067 static tree
6068 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6069 {
6070   tree expr;
6071
6072   /* If the next token is the `throw' keyword, then we're looking at
6073      a throw-expression.  */
6074   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6075     expr = cp_parser_throw_expression (parser);
6076   /* Otherwise, it must be that we are looking at a
6077      logical-or-expression.  */
6078   else
6079     {
6080       /* Parse the binary expressions (logical-or-expression).  */
6081       expr = cp_parser_binary_expression (parser, cast_p);
6082       /* If the next token is a `?' then we're actually looking at a
6083          conditional-expression.  */
6084       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6085         return cp_parser_question_colon_clause (parser, expr);
6086       else
6087         {
6088           enum tree_code assignment_operator;
6089
6090           /* If it's an assignment-operator, we're using the second
6091              production.  */
6092           assignment_operator
6093             = cp_parser_assignment_operator_opt (parser);
6094           if (assignment_operator != ERROR_MARK)
6095             {
6096               tree rhs;
6097
6098               /* Parse the right-hand side of the assignment.  */
6099               rhs = cp_parser_assignment_expression (parser, cast_p);
6100               /* An assignment may not appear in a
6101                  constant-expression.  */
6102               if (cp_parser_non_integral_constant_expression (parser,
6103                                                               "an assignment"))
6104                 return error_mark_node;
6105               /* Build the assignment expression.  */
6106               expr = build_x_modify_expr (expr,
6107                                           assignment_operator,
6108                                           rhs);
6109             }
6110         }
6111     }
6112
6113   return expr;
6114 }
6115
6116 /* Parse an (optional) assignment-operator.
6117
6118    assignment-operator: one of
6119      = *= /= %= += -= >>= <<= &= ^= |=
6120
6121    GNU Extension:
6122
6123    assignment-operator: one of
6124      <?= >?=
6125
6126    If the next token is an assignment operator, the corresponding tree
6127    code is returned, and the token is consumed.  For example, for
6128    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6129    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6130    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6131    operator, ERROR_MARK is returned.  */
6132
6133 static enum tree_code
6134 cp_parser_assignment_operator_opt (cp_parser* parser)
6135 {
6136   enum tree_code op;
6137   cp_token *token;
6138
6139   /* Peek at the next toen.  */
6140   token = cp_lexer_peek_token (parser->lexer);
6141
6142   switch (token->type)
6143     {
6144     case CPP_EQ:
6145       op = NOP_EXPR;
6146       break;
6147
6148     case CPP_MULT_EQ:
6149       op = MULT_EXPR;
6150       break;
6151
6152     case CPP_DIV_EQ:
6153       op = TRUNC_DIV_EXPR;
6154       break;
6155
6156     case CPP_MOD_EQ:
6157       op = TRUNC_MOD_EXPR;
6158       break;
6159
6160     case CPP_PLUS_EQ:
6161       op = PLUS_EXPR;
6162       break;
6163
6164     case CPP_MINUS_EQ:
6165       op = MINUS_EXPR;
6166       break;
6167
6168     case CPP_RSHIFT_EQ:
6169       op = RSHIFT_EXPR;
6170       break;
6171
6172     case CPP_LSHIFT_EQ:
6173       op = LSHIFT_EXPR;
6174       break;
6175
6176     case CPP_AND_EQ:
6177       op = BIT_AND_EXPR;
6178       break;
6179
6180     case CPP_XOR_EQ:
6181       op = BIT_XOR_EXPR;
6182       break;
6183
6184     case CPP_OR_EQ:
6185       op = BIT_IOR_EXPR;
6186       break;
6187
6188     default:
6189       /* Nothing else is an assignment operator.  */
6190       op = ERROR_MARK;
6191     }
6192
6193   /* If it was an assignment operator, consume it.  */
6194   if (op != ERROR_MARK)
6195     cp_lexer_consume_token (parser->lexer);
6196
6197   return op;
6198 }
6199
6200 /* Parse an expression.
6201
6202    expression:
6203      assignment-expression
6204      expression , assignment-expression
6205
6206    CAST_P is true if this expression is the target of a cast.
6207
6208    Returns a representation of the expression.  */
6209
6210 static tree
6211 cp_parser_expression (cp_parser* parser, bool cast_p)
6212 {
6213   tree expression = NULL_TREE;
6214
6215   while (true)
6216     {
6217       tree assignment_expression;
6218
6219       /* Parse the next assignment-expression.  */
6220       assignment_expression
6221         = cp_parser_assignment_expression (parser, cast_p);
6222       /* If this is the first assignment-expression, we can just
6223          save it away.  */
6224       if (!expression)
6225         expression = assignment_expression;
6226       else
6227         expression = build_x_compound_expr (expression,
6228                                             assignment_expression);
6229       /* If the next token is not a comma, then we are done with the
6230          expression.  */
6231       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6232         break;
6233       /* Consume the `,'.  */
6234       cp_lexer_consume_token (parser->lexer);
6235       /* A comma operator cannot appear in a constant-expression.  */
6236       if (cp_parser_non_integral_constant_expression (parser,
6237                                                       "a comma operator"))
6238         expression = error_mark_node;
6239     }
6240
6241   return expression;
6242 }
6243
6244 /* Parse a constant-expression.
6245
6246    constant-expression:
6247      conditional-expression
6248
6249   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6250   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6251   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6252   is false, NON_CONSTANT_P should be NULL.  */
6253
6254 static tree
6255 cp_parser_constant_expression (cp_parser* parser,
6256                                bool allow_non_constant_p,
6257                                bool *non_constant_p)
6258 {
6259   bool saved_integral_constant_expression_p;
6260   bool saved_allow_non_integral_constant_expression_p;
6261   bool saved_non_integral_constant_expression_p;
6262   tree expression;
6263
6264   /* It might seem that we could simply parse the
6265      conditional-expression, and then check to see if it were
6266      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6267      one that the compiler can figure out is constant, possibly after
6268      doing some simplifications or optimizations.  The standard has a
6269      precise definition of constant-expression, and we must honor
6270      that, even though it is somewhat more restrictive.
6271
6272      For example:
6273
6274        int i[(2, 3)];
6275
6276      is not a legal declaration, because `(2, 3)' is not a
6277      constant-expression.  The `,' operator is forbidden in a
6278      constant-expression.  However, GCC's constant-folding machinery
6279      will fold this operation to an INTEGER_CST for `3'.  */
6280
6281   /* Save the old settings.  */
6282   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6283   saved_allow_non_integral_constant_expression_p
6284     = parser->allow_non_integral_constant_expression_p;
6285   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6286   /* We are now parsing a constant-expression.  */
6287   parser->integral_constant_expression_p = true;
6288   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6289   parser->non_integral_constant_expression_p = false;
6290   /* Although the grammar says "conditional-expression", we parse an
6291      "assignment-expression", which also permits "throw-expression"
6292      and the use of assignment operators.  In the case that
6293      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6294      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6295      actually essential that we look for an assignment-expression.
6296      For example, cp_parser_initializer_clauses uses this function to
6297      determine whether a particular assignment-expression is in fact
6298      constant.  */
6299   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6300   /* Restore the old settings.  */
6301   parser->integral_constant_expression_p
6302     = saved_integral_constant_expression_p;
6303   parser->allow_non_integral_constant_expression_p
6304     = saved_allow_non_integral_constant_expression_p;
6305   if (allow_non_constant_p)
6306     *non_constant_p = parser->non_integral_constant_expression_p;
6307   else if (parser->non_integral_constant_expression_p)
6308     expression = error_mark_node;
6309   parser->non_integral_constant_expression_p
6310     = saved_non_integral_constant_expression_p;
6311
6312   return expression;
6313 }
6314
6315 /* Parse __builtin_offsetof.
6316
6317    offsetof-expression:
6318      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6319
6320    offsetof-member-designator:
6321      id-expression
6322      | offsetof-member-designator "." id-expression
6323      | offsetof-member-designator "[" expression "]"  */
6324
6325 static tree
6326 cp_parser_builtin_offsetof (cp_parser *parser)
6327 {
6328   int save_ice_p, save_non_ice_p;
6329   tree type, expr;
6330   cp_id_kind dummy;
6331
6332   /* We're about to accept non-integral-constant things, but will
6333      definitely yield an integral constant expression.  Save and
6334      restore these values around our local parsing.  */
6335   save_ice_p = parser->integral_constant_expression_p;
6336   save_non_ice_p = parser->non_integral_constant_expression_p;
6337
6338   /* Consume the "__builtin_offsetof" token.  */
6339   cp_lexer_consume_token (parser->lexer);
6340   /* Consume the opening `('.  */
6341   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6342   /* Parse the type-id.  */
6343   type = cp_parser_type_id (parser);
6344   /* Look for the `,'.  */
6345   cp_parser_require (parser, CPP_COMMA, "`,'");
6346
6347   /* Build the (type *)null that begins the traditional offsetof macro.  */
6348   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6349
6350   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6351   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6352                                                  true, &dummy);
6353   while (true)
6354     {
6355       cp_token *token = cp_lexer_peek_token (parser->lexer);
6356       switch (token->type)
6357         {
6358         case CPP_OPEN_SQUARE:
6359           /* offsetof-member-designator "[" expression "]" */
6360           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6361           break;
6362
6363         case CPP_DOT:
6364           /* offsetof-member-designator "." identifier */
6365           cp_lexer_consume_token (parser->lexer);
6366           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6367                                                          true, &dummy);
6368           break;
6369
6370         case CPP_CLOSE_PAREN:
6371           /* Consume the ")" token.  */
6372           cp_lexer_consume_token (parser->lexer);
6373           goto success;
6374
6375         default:
6376           /* Error.  We know the following require will fail, but
6377              that gives the proper error message.  */
6378           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6379           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6380           expr = error_mark_node;
6381           goto failure;
6382         }
6383     }
6384
6385  success:
6386   /* If we're processing a template, we can't finish the semantics yet.
6387      Otherwise we can fold the entire expression now.  */
6388   if (processing_template_decl)
6389     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6390   else
6391     expr = finish_offsetof (expr);
6392
6393  failure:
6394   parser->integral_constant_expression_p = save_ice_p;
6395   parser->non_integral_constant_expression_p = save_non_ice_p;
6396
6397   return expr;
6398 }
6399
6400 /* Parse a trait expression.  */
6401
6402 static tree
6403 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6404 {
6405   cp_trait_kind kind;
6406   tree type1, type2 = NULL_TREE;
6407   bool binary = false;
6408   cp_decl_specifier_seq decl_specs;
6409
6410   switch (keyword)
6411     {
6412     case RID_HAS_NOTHROW_ASSIGN:
6413       kind = CPTK_HAS_NOTHROW_ASSIGN;
6414       break;
6415     case RID_HAS_NOTHROW_CONSTRUCTOR:
6416       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6417       break;
6418     case RID_HAS_NOTHROW_COPY:
6419       kind = CPTK_HAS_NOTHROW_COPY;
6420       break;
6421     case RID_HAS_TRIVIAL_ASSIGN:
6422       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6423       break;
6424     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6425       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6426       break;
6427     case RID_HAS_TRIVIAL_COPY:
6428       kind = CPTK_HAS_TRIVIAL_COPY;
6429       break;
6430     case RID_HAS_TRIVIAL_DESTRUCTOR:
6431       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6432       break;
6433     case RID_HAS_VIRTUAL_DESTRUCTOR:
6434       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6435       break;
6436     case RID_IS_ABSTRACT:
6437       kind = CPTK_IS_ABSTRACT;
6438       break;
6439     case RID_IS_BASE_OF:
6440       kind = CPTK_IS_BASE_OF;
6441       binary = true;
6442       break;
6443     case RID_IS_CLASS:
6444       kind = CPTK_IS_CLASS;
6445       break;
6446     case RID_IS_CONVERTIBLE_TO:
6447       kind = CPTK_IS_CONVERTIBLE_TO;
6448       binary = true;
6449       break;
6450     case RID_IS_EMPTY:
6451       kind = CPTK_IS_EMPTY;
6452       break;
6453     case RID_IS_ENUM:
6454       kind = CPTK_IS_ENUM;
6455       break;
6456     case RID_IS_POD:
6457       kind = CPTK_IS_POD;
6458       break;
6459     case RID_IS_POLYMORPHIC:
6460       kind = CPTK_IS_POLYMORPHIC;
6461       break;
6462     case RID_IS_UNION:
6463       kind = CPTK_IS_UNION;
6464       break;
6465     default:
6466       gcc_unreachable ();
6467     }
6468
6469   /* Consume the token.  */
6470   cp_lexer_consume_token (parser->lexer);
6471
6472   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6473
6474   type1 = cp_parser_type_id (parser);
6475
6476   /* Build a trivial decl-specifier-seq.  */
6477   clear_decl_specs (&decl_specs);
6478   decl_specs.type = type1;
6479
6480   /* Call grokdeclarator to figure out what type this is.  */
6481   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6482                           /*initialized=*/0, /*attrlist=*/NULL);
6483
6484   if (binary)
6485     {
6486       cp_parser_require (parser, CPP_COMMA, "`,'");
6487  
6488       type2 = cp_parser_type_id (parser);
6489
6490       /* Build a trivial decl-specifier-seq.  */
6491       clear_decl_specs (&decl_specs);
6492       decl_specs.type = type2;
6493
6494       /* Call grokdeclarator to figure out what type this is.  */
6495       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6496                               /*initialized=*/0, /*attrlist=*/NULL);
6497     }
6498
6499   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6500
6501   /* Complete the trait expr, which may mean either processing the
6502      static assert now or saving it for template instantiation.  */
6503   return finish_trait_expr (kind, type1, type2);
6504 }
6505
6506 /* Statements [gram.stmt.stmt]  */
6507
6508 /* Parse a statement.
6509
6510    statement:
6511      labeled-statement
6512      expression-statement
6513      compound-statement
6514      selection-statement
6515      iteration-statement
6516      jump-statement
6517      declaration-statement
6518      try-block
6519
6520   IN_COMPOUND is true when the statement is nested inside a
6521   cp_parser_compound_statement; this matters for certain pragmas.
6522
6523   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6524   is a (possibly labeled) if statement which is not enclosed in braces
6525   and has an else clause.  This is used to implement -Wparentheses.  */
6526
6527 static void
6528 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6529                      bool in_compound, bool *if_p)
6530 {
6531   tree statement;
6532   cp_token *token;
6533   location_t statement_location;
6534
6535  restart:
6536   if (if_p != NULL)
6537     *if_p = false;
6538   /* There is no statement yet.  */
6539   statement = NULL_TREE;
6540   /* Peek at the next token.  */
6541   token = cp_lexer_peek_token (parser->lexer);
6542   /* Remember the location of the first token in the statement.  */
6543   statement_location = token->location;
6544   /* If this is a keyword, then that will often determine what kind of
6545      statement we have.  */
6546   if (token->type == CPP_KEYWORD)
6547     {
6548       enum rid keyword = token->keyword;
6549
6550       switch (keyword)
6551         {
6552         case RID_CASE:
6553         case RID_DEFAULT:
6554           /* Looks like a labeled-statement with a case label.
6555              Parse the label, and then use tail recursion to parse
6556              the statement.  */
6557           cp_parser_label_for_labeled_statement (parser);
6558           goto restart;
6559
6560         case RID_IF:
6561         case RID_SWITCH:
6562           statement = cp_parser_selection_statement (parser, if_p);
6563           break;
6564
6565         case RID_WHILE:
6566         case RID_DO:
6567         case RID_FOR:
6568           statement = cp_parser_iteration_statement (parser);
6569           break;
6570
6571         case RID_BREAK:
6572         case RID_CONTINUE:
6573         case RID_RETURN:
6574         case RID_GOTO:
6575           statement = cp_parser_jump_statement (parser);
6576           break;
6577
6578           /* Objective-C++ exception-handling constructs.  */
6579         case RID_AT_TRY:
6580         case RID_AT_CATCH:
6581         case RID_AT_FINALLY:
6582         case RID_AT_SYNCHRONIZED:
6583         case RID_AT_THROW:
6584           statement = cp_parser_objc_statement (parser);
6585           break;
6586
6587         case RID_TRY:
6588           statement = cp_parser_try_block (parser);
6589           break;
6590
6591         case RID_NAMESPACE:
6592           /* This must be a namespace alias definition.  */
6593           cp_parser_declaration_statement (parser);
6594           return;
6595           
6596         default:
6597           /* It might be a keyword like `int' that can start a
6598              declaration-statement.  */
6599           break;
6600         }
6601     }
6602   else if (token->type == CPP_NAME)
6603     {
6604       /* If the next token is a `:', then we are looking at a
6605          labeled-statement.  */
6606       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6607       if (token->type == CPP_COLON)
6608         {
6609           /* Looks like a labeled-statement with an ordinary label.
6610              Parse the label, and then use tail recursion to parse
6611              the statement.  */
6612           cp_parser_label_for_labeled_statement (parser);
6613           goto restart;
6614         }
6615     }
6616   /* Anything that starts with a `{' must be a compound-statement.  */
6617   else if (token->type == CPP_OPEN_BRACE)
6618     statement = cp_parser_compound_statement (parser, NULL, false);
6619   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6620      a statement all its own.  */
6621   else if (token->type == CPP_PRAGMA)
6622     {
6623       /* Only certain OpenMP pragmas are attached to statements, and thus
6624          are considered statements themselves.  All others are not.  In
6625          the context of a compound, accept the pragma as a "statement" and
6626          return so that we can check for a close brace.  Otherwise we
6627          require a real statement and must go back and read one.  */
6628       if (in_compound)
6629         cp_parser_pragma (parser, pragma_compound);
6630       else if (!cp_parser_pragma (parser, pragma_stmt))
6631         goto restart;
6632       return;
6633     }
6634   else if (token->type == CPP_EOF)
6635     {
6636       cp_parser_error (parser, "expected statement");
6637       return;
6638     }
6639
6640   /* Everything else must be a declaration-statement or an
6641      expression-statement.  Try for the declaration-statement
6642      first, unless we are looking at a `;', in which case we know that
6643      we have an expression-statement.  */
6644   if (!statement)
6645     {
6646       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6647         {
6648           cp_parser_parse_tentatively (parser);
6649           /* Try to parse the declaration-statement.  */
6650           cp_parser_declaration_statement (parser);
6651           /* If that worked, we're done.  */
6652           if (cp_parser_parse_definitely (parser))
6653             return;
6654         }
6655       /* Look for an expression-statement instead.  */
6656       statement = cp_parser_expression_statement (parser, in_statement_expr);
6657     }
6658
6659   /* Set the line number for the statement.  */
6660   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6661     SET_EXPR_LOCATION (statement, statement_location);
6662 }
6663
6664 /* Parse the label for a labeled-statement, i.e.
6665
6666    identifier :
6667    case constant-expression :
6668    default :
6669
6670    GNU Extension:
6671    case constant-expression ... constant-expression : statement
6672
6673    When a label is parsed without errors, the label is added to the
6674    parse tree by the finish_* functions, so this function doesn't
6675    have to return the label.  */
6676
6677 static void
6678 cp_parser_label_for_labeled_statement (cp_parser* parser)
6679 {
6680   cp_token *token;
6681
6682   /* The next token should be an identifier.  */
6683   token = cp_lexer_peek_token (parser->lexer);
6684   if (token->type != CPP_NAME
6685       && token->type != CPP_KEYWORD)
6686     {
6687       cp_parser_error (parser, "expected labeled-statement");
6688       return;
6689     }
6690
6691   switch (token->keyword)
6692     {
6693     case RID_CASE:
6694       {
6695         tree expr, expr_hi;
6696         cp_token *ellipsis;
6697
6698         /* Consume the `case' token.  */
6699         cp_lexer_consume_token (parser->lexer);
6700         /* Parse the constant-expression.  */
6701         expr = cp_parser_constant_expression (parser,
6702                                               /*allow_non_constant_p=*/false,
6703                                               NULL);
6704
6705         ellipsis = cp_lexer_peek_token (parser->lexer);
6706         if (ellipsis->type == CPP_ELLIPSIS)
6707           {
6708             /* Consume the `...' token.  */
6709             cp_lexer_consume_token (parser->lexer);
6710             expr_hi =
6711               cp_parser_constant_expression (parser,
6712                                              /*allow_non_constant_p=*/false,
6713                                              NULL);
6714             /* We don't need to emit warnings here, as the common code
6715                will do this for us.  */
6716           }
6717         else
6718           expr_hi = NULL_TREE;
6719
6720         if (parser->in_switch_statement_p)
6721           finish_case_label (expr, expr_hi);
6722         else
6723           error ("case label %qE not within a switch statement", expr);
6724       }
6725       break;
6726
6727     case RID_DEFAULT:
6728       /* Consume the `default' token.  */
6729       cp_lexer_consume_token (parser->lexer);
6730
6731       if (parser->in_switch_statement_p)
6732         finish_case_label (NULL_TREE, NULL_TREE);
6733       else
6734         error ("case label not within a switch statement");
6735       break;
6736
6737     default:
6738       /* Anything else must be an ordinary label.  */
6739       finish_label_stmt (cp_parser_identifier (parser));
6740       break;
6741     }
6742
6743   /* Require the `:' token.  */
6744   cp_parser_require (parser, CPP_COLON, "`:'");
6745 }
6746
6747 /* Parse an expression-statement.
6748
6749    expression-statement:
6750      expression [opt] ;
6751
6752    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6753    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6754    indicates whether this expression-statement is part of an
6755    expression statement.  */
6756
6757 static tree
6758 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6759 {
6760   tree statement = NULL_TREE;
6761
6762   /* If the next token is a ';', then there is no expression
6763      statement.  */
6764   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6765     statement = cp_parser_expression (parser, /*cast_p=*/false);
6766
6767   /* Consume the final `;'.  */
6768   cp_parser_consume_semicolon_at_end_of_statement (parser);
6769
6770   if (in_statement_expr
6771       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6772     /* This is the final expression statement of a statement
6773        expression.  */
6774     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6775   else if (statement)
6776     statement = finish_expr_stmt (statement);
6777   else
6778     finish_stmt ();
6779
6780   return statement;
6781 }
6782
6783 /* Parse a compound-statement.
6784
6785    compound-statement:
6786      { statement-seq [opt] }
6787
6788    Returns a tree representing the statement.  */
6789
6790 static tree
6791 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6792                               bool in_try)
6793 {
6794   tree compound_stmt;
6795
6796   /* Consume the `{'.  */
6797   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6798     return error_mark_node;
6799   /* Begin the compound-statement.  */
6800   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6801   /* Parse an (optional) statement-seq.  */
6802   cp_parser_statement_seq_opt (parser, in_statement_expr);
6803   /* Finish the compound-statement.  */
6804   finish_compound_stmt (compound_stmt);
6805   /* Consume the `}'.  */
6806   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6807
6808   return compound_stmt;
6809 }
6810
6811 /* Parse an (optional) statement-seq.
6812
6813    statement-seq:
6814      statement
6815      statement-seq [opt] statement  */
6816
6817 static void
6818 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6819 {
6820   /* Scan statements until there aren't any more.  */
6821   while (true)
6822     {
6823       cp_token *token = cp_lexer_peek_token (parser->lexer);
6824
6825       /* If we're looking at a `}', then we've run out of statements.  */
6826       if (token->type == CPP_CLOSE_BRACE
6827           || token->type == CPP_EOF
6828           || token->type == CPP_PRAGMA_EOL)
6829         break;
6830       
6831       /* If we are in a compound statement and find 'else' then
6832          something went wrong.  */
6833       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6834         {
6835           if (parser->in_statement & IN_IF_STMT) 
6836             break;
6837           else
6838             {
6839               token = cp_lexer_consume_token (parser->lexer);
6840               error ("%<else%> without a previous %<if%>");
6841             }
6842         }
6843
6844       /* Parse the statement.  */
6845       cp_parser_statement (parser, in_statement_expr, true, NULL);
6846     }
6847 }
6848
6849 /* Parse a selection-statement.
6850
6851    selection-statement:
6852      if ( condition ) statement
6853      if ( condition ) statement else statement
6854      switch ( condition ) statement
6855
6856    Returns the new IF_STMT or SWITCH_STMT.
6857
6858    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6859    is a (possibly labeled) if statement which is not enclosed in
6860    braces and has an else clause.  This is used to implement
6861    -Wparentheses.  */
6862
6863 static tree
6864 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6865 {
6866   cp_token *token;
6867   enum rid keyword;
6868
6869   if (if_p != NULL)
6870     *if_p = false;
6871
6872   /* Peek at the next token.  */
6873   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6874
6875   /* See what kind of keyword it is.  */
6876   keyword = token->keyword;
6877   switch (keyword)
6878     {
6879     case RID_IF:
6880     case RID_SWITCH:
6881       {
6882         tree statement;
6883         tree condition;
6884
6885         /* Look for the `('.  */
6886         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6887           {
6888             cp_parser_skip_to_end_of_statement (parser);
6889             return error_mark_node;
6890           }
6891
6892         /* Begin the selection-statement.  */
6893         if (keyword == RID_IF)
6894           statement = begin_if_stmt ();
6895         else
6896           statement = begin_switch_stmt ();
6897
6898         /* Parse the condition.  */
6899         condition = cp_parser_condition (parser);
6900         /* Look for the `)'.  */
6901         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6902           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6903                                                  /*consume_paren=*/true);
6904
6905         if (keyword == RID_IF)
6906           {
6907             bool nested_if;
6908             unsigned char in_statement;
6909
6910             /* Add the condition.  */
6911             finish_if_stmt_cond (condition, statement);
6912
6913             /* Parse the then-clause.  */
6914             in_statement = parser->in_statement;
6915             parser->in_statement |= IN_IF_STMT;
6916             cp_parser_implicitly_scoped_statement (parser, &nested_if);
6917             parser->in_statement = in_statement;
6918
6919             finish_then_clause (statement);
6920
6921             /* If the next token is `else', parse the else-clause.  */
6922             if (cp_lexer_next_token_is_keyword (parser->lexer,
6923                                                 RID_ELSE))
6924               {
6925                 /* Consume the `else' keyword.  */
6926                 cp_lexer_consume_token (parser->lexer);
6927                 begin_else_clause (statement);
6928                 /* Parse the else-clause.  */
6929                 cp_parser_implicitly_scoped_statement (parser, NULL);
6930                 finish_else_clause (statement);
6931
6932                 /* If we are currently parsing a then-clause, then
6933                    IF_P will not be NULL.  We set it to true to
6934                    indicate that this if statement has an else clause.
6935                    This may trigger the Wparentheses warning below
6936                    when we get back up to the parent if statement.  */
6937                 if (if_p != NULL)
6938                   *if_p = true;
6939               }
6940             else
6941               {
6942                 /* This if statement does not have an else clause.  If
6943                    NESTED_IF is true, then the then-clause is an if
6944                    statement which does have an else clause.  We warn
6945                    about the potential ambiguity.  */
6946                 if (nested_if)
6947                   warning (OPT_Wparentheses,
6948                            ("%Hsuggest explicit braces "
6949                             "to avoid ambiguous %<else%>"),
6950                            EXPR_LOCUS (statement));
6951               }
6952
6953             /* Now we're all done with the if-statement.  */
6954             finish_if_stmt (statement);
6955           }
6956         else
6957           {
6958             bool in_switch_statement_p;
6959             unsigned char in_statement;
6960
6961             /* Add the condition.  */
6962             finish_switch_cond (condition, statement);
6963
6964             /* Parse the body of the switch-statement.  */
6965             in_switch_statement_p = parser->in_switch_statement_p;
6966             in_statement = parser->in_statement;
6967             parser->in_switch_statement_p = true;
6968             parser->in_statement |= IN_SWITCH_STMT;
6969             cp_parser_implicitly_scoped_statement (parser, NULL);
6970             parser->in_switch_statement_p = in_switch_statement_p;
6971             parser->in_statement = in_statement;
6972
6973             /* Now we're all done with the switch-statement.  */
6974             finish_switch_stmt (statement);
6975           }
6976
6977         return statement;
6978       }
6979       break;
6980
6981     default:
6982       cp_parser_error (parser, "expected selection-statement");
6983       return error_mark_node;
6984     }
6985 }
6986
6987 /* Parse a condition.
6988
6989    condition:
6990      expression
6991      type-specifier-seq declarator = assignment-expression
6992
6993    GNU Extension:
6994
6995    condition:
6996      type-specifier-seq declarator asm-specification [opt]
6997        attributes [opt] = assignment-expression
6998
6999    Returns the expression that should be tested.  */
7000
7001 static tree
7002 cp_parser_condition (cp_parser* parser)
7003 {
7004   cp_decl_specifier_seq type_specifiers;
7005   const char *saved_message;
7006
7007   /* Try the declaration first.  */
7008   cp_parser_parse_tentatively (parser);
7009   /* New types are not allowed in the type-specifier-seq for a
7010      condition.  */
7011   saved_message = parser->type_definition_forbidden_message;
7012   parser->type_definition_forbidden_message
7013     = "types may not be defined in conditions";
7014   /* Parse the type-specifier-seq.  */
7015   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7016                                 &type_specifiers);
7017   /* Restore the saved message.  */
7018   parser->type_definition_forbidden_message = saved_message;
7019   /* If all is well, we might be looking at a declaration.  */
7020   if (!cp_parser_error_occurred (parser))
7021     {
7022       tree decl;
7023       tree asm_specification;
7024       tree attributes;
7025       cp_declarator *declarator;
7026       tree initializer = NULL_TREE;
7027
7028       /* Parse the declarator.  */
7029       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7030                                          /*ctor_dtor_or_conv_p=*/NULL,
7031                                          /*parenthesized_p=*/NULL,
7032                                          /*member_p=*/false);
7033       /* Parse the attributes.  */
7034       attributes = cp_parser_attributes_opt (parser);
7035       /* Parse the asm-specification.  */
7036       asm_specification = cp_parser_asm_specification_opt (parser);
7037       /* If the next token is not an `=', then we might still be
7038          looking at an expression.  For example:
7039
7040            if (A(a).x)
7041
7042          looks like a decl-specifier-seq and a declarator -- but then
7043          there is no `=', so this is an expression.  */
7044       cp_parser_require (parser, CPP_EQ, "`='");
7045       /* If we did see an `=', then we are looking at a declaration
7046          for sure.  */
7047       if (cp_parser_parse_definitely (parser))
7048         {
7049           tree pushed_scope;
7050           bool non_constant_p;
7051
7052           /* Create the declaration.  */
7053           decl = start_decl (declarator, &type_specifiers,
7054                              /*initialized_p=*/true,
7055                              attributes, /*prefix_attributes=*/NULL_TREE,
7056                              &pushed_scope);
7057           /* Parse the assignment-expression.  */
7058           initializer
7059             = cp_parser_constant_expression (parser,
7060                                              /*allow_non_constant_p=*/true,
7061                                              &non_constant_p);
7062           if (!non_constant_p)
7063             initializer = fold_non_dependent_expr (initializer);
7064
7065           /* Process the initializer.  */
7066           cp_finish_decl (decl,
7067                           initializer, !non_constant_p,
7068                           asm_specification,
7069                           LOOKUP_ONLYCONVERTING);
7070
7071           if (pushed_scope)
7072             pop_scope (pushed_scope);
7073
7074           return convert_from_reference (decl);
7075         }
7076     }
7077   /* If we didn't even get past the declarator successfully, we are
7078      definitely not looking at a declaration.  */
7079   else
7080     cp_parser_abort_tentative_parse (parser);
7081
7082   /* Otherwise, we are looking at an expression.  */
7083   return cp_parser_expression (parser, /*cast_p=*/false);
7084 }
7085
7086 /* We check for a ) immediately followed by ; with no whitespacing
7087    between.  This is used to issue a warning for:
7088
7089      while (...);
7090
7091    and:
7092
7093      for (...);
7094
7095    as the semicolon is probably extraneous.
7096
7097    On parse errors, the next token might not be a ), so do nothing in
7098    that case. */
7099
7100 static void
7101 check_empty_body (cp_parser* parser, const char* type)
7102 {
7103   cp_token *token;
7104   cp_token *close_paren;
7105   expanded_location close_loc;
7106   expanded_location semi_loc;
7107   
7108   close_paren = cp_lexer_peek_token (parser->lexer);
7109   if (close_paren->type != CPP_CLOSE_PAREN)
7110     return;
7111
7112   close_loc = expand_location (close_paren->location);
7113   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7114
7115   if (token->type != CPP_SEMICOLON
7116       || (token->flags & PREV_WHITE))
7117     return;
7118
7119   semi_loc =  expand_location (token->location);
7120   if (close_loc.line == semi_loc.line
7121 #ifdef USE_MAPPED_LOCATION
7122       && close_loc.column+1 == semi_loc.column
7123 #endif
7124       )
7125     warning (OPT_Wempty_body,
7126              "suggest a space before %<;%> or explicit braces around empty "
7127              "body in %<%s%> statement",
7128              type);
7129 }
7130
7131 /* Parse an iteration-statement.
7132
7133    iteration-statement:
7134      while ( condition ) statement
7135      do statement while ( expression ) ;
7136      for ( for-init-statement condition [opt] ; expression [opt] )
7137        statement
7138
7139    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7140
7141 static tree
7142 cp_parser_iteration_statement (cp_parser* parser)
7143 {
7144   cp_token *token;
7145   enum rid keyword;
7146   tree statement;
7147   unsigned char in_statement;
7148
7149   /* Peek at the next token.  */
7150   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7151   if (!token)
7152     return error_mark_node;
7153
7154   /* Remember whether or not we are already within an iteration
7155      statement.  */
7156   in_statement = parser->in_statement;
7157
7158   /* See what kind of keyword it is.  */
7159   keyword = token->keyword;
7160   switch (keyword)
7161     {
7162     case RID_WHILE:
7163       {
7164         tree condition;
7165
7166         /* Begin the while-statement.  */
7167         statement = begin_while_stmt ();
7168         /* Look for the `('.  */
7169         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7170         /* Parse the condition.  */
7171         condition = cp_parser_condition (parser);
7172         finish_while_stmt_cond (condition, statement);
7173         check_empty_body (parser, "while");
7174         /* Look for the `)'.  */
7175         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7176         /* Parse the dependent statement.  */
7177         parser->in_statement = IN_ITERATION_STMT;
7178         cp_parser_already_scoped_statement (parser);
7179         parser->in_statement = in_statement;
7180         /* We're done with the while-statement.  */
7181         finish_while_stmt (statement);
7182       }
7183       break;
7184
7185     case RID_DO:
7186       {
7187         tree expression;
7188
7189         /* Begin the do-statement.  */
7190         statement = begin_do_stmt ();
7191         /* Parse the body of the do-statement.  */
7192         parser->in_statement = IN_ITERATION_STMT;
7193         cp_parser_implicitly_scoped_statement (parser, NULL);
7194         parser->in_statement = in_statement;
7195         finish_do_body (statement);
7196         /* Look for the `while' keyword.  */
7197         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7198         /* Look for the `('.  */
7199         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7200         /* Parse the expression.  */
7201         expression = cp_parser_expression (parser, /*cast_p=*/false);
7202         /* We're done with the do-statement.  */
7203         finish_do_stmt (expression, statement);
7204         /* Look for the `)'.  */
7205         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7206         /* Look for the `;'.  */
7207         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7208       }
7209       break;
7210
7211     case RID_FOR:
7212       {
7213         tree condition = NULL_TREE;
7214         tree expression = NULL_TREE;
7215
7216         /* Begin the for-statement.  */
7217         statement = begin_for_stmt ();
7218         /* Look for the `('.  */
7219         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7220         /* Parse the initialization.  */
7221         cp_parser_for_init_statement (parser);
7222         finish_for_init_stmt (statement);
7223
7224         /* If there's a condition, process it.  */
7225         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7226           condition = cp_parser_condition (parser);
7227         finish_for_cond (condition, statement);
7228         /* Look for the `;'.  */
7229         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7230
7231         /* If there's an expression, process it.  */
7232         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7233           expression = cp_parser_expression (parser, /*cast_p=*/false);
7234         finish_for_expr (expression, statement);
7235         check_empty_body (parser, "for");
7236         /* Look for the `)'.  */
7237         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7238
7239         /* Parse the body of the for-statement.  */
7240         parser->in_statement = IN_ITERATION_STMT;
7241         cp_parser_already_scoped_statement (parser);
7242         parser->in_statement = in_statement;
7243
7244         /* We're done with the for-statement.  */
7245         finish_for_stmt (statement);
7246       }
7247       break;
7248
7249     default:
7250       cp_parser_error (parser, "expected iteration-statement");
7251       statement = error_mark_node;
7252       break;
7253     }
7254
7255   return statement;
7256 }
7257
7258 /* Parse a for-init-statement.
7259
7260    for-init-statement:
7261      expression-statement
7262      simple-declaration  */
7263
7264 static void
7265 cp_parser_for_init_statement (cp_parser* parser)
7266 {
7267   /* If the next token is a `;', then we have an empty
7268      expression-statement.  Grammatically, this is also a
7269      simple-declaration, but an invalid one, because it does not
7270      declare anything.  Therefore, if we did not handle this case
7271      specially, we would issue an error message about an invalid
7272      declaration.  */
7273   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7274     {
7275       /* We're going to speculatively look for a declaration, falling back
7276          to an expression, if necessary.  */
7277       cp_parser_parse_tentatively (parser);
7278       /* Parse the declaration.  */
7279       cp_parser_simple_declaration (parser,
7280                                     /*function_definition_allowed_p=*/false);
7281       /* If the tentative parse failed, then we shall need to look for an
7282          expression-statement.  */
7283       if (cp_parser_parse_definitely (parser))
7284         return;
7285     }
7286
7287   cp_parser_expression_statement (parser, false);
7288 }
7289
7290 /* Parse a jump-statement.
7291
7292    jump-statement:
7293      break ;
7294      continue ;
7295      return expression [opt] ;
7296      goto identifier ;
7297
7298    GNU extension:
7299
7300    jump-statement:
7301      goto * expression ;
7302
7303    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7304
7305 static tree
7306 cp_parser_jump_statement (cp_parser* parser)
7307 {
7308   tree statement = error_mark_node;
7309   cp_token *token;
7310   enum rid keyword;
7311   unsigned char in_statement;
7312
7313   /* Peek at the next token.  */
7314   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7315   if (!token)
7316     return error_mark_node;
7317
7318   /* See what kind of keyword it is.  */
7319   keyword = token->keyword;
7320   switch (keyword)
7321     {
7322     case RID_BREAK:
7323       in_statement = parser->in_statement & ~IN_IF_STMT;      
7324       switch (in_statement)
7325         {
7326         case 0:
7327           error ("break statement not within loop or switch");
7328           break;
7329         default:
7330           gcc_assert ((in_statement & IN_SWITCH_STMT)
7331                       || in_statement == IN_ITERATION_STMT);
7332           statement = finish_break_stmt ();
7333           break;
7334         case IN_OMP_BLOCK:
7335           error ("invalid exit from OpenMP structured block");
7336           break;
7337         case IN_OMP_FOR:
7338           error ("break statement used with OpenMP for loop");
7339           break;
7340         }
7341       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7342       break;
7343
7344     case RID_CONTINUE:
7345       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7346         {
7347         case 0:
7348           error ("continue statement not within a loop");
7349           break;
7350         case IN_ITERATION_STMT:
7351         case IN_OMP_FOR:
7352           statement = finish_continue_stmt ();
7353           break;
7354         case IN_OMP_BLOCK:
7355           error ("invalid exit from OpenMP structured block");
7356           break;
7357         default:
7358           gcc_unreachable ();
7359         }
7360       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7361       break;
7362
7363     case RID_RETURN:
7364       {
7365         tree expr;
7366
7367         /* If the next token is a `;', then there is no
7368            expression.  */
7369         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7370           expr = cp_parser_expression (parser, /*cast_p=*/false);
7371         else
7372           expr = NULL_TREE;
7373         /* Build the return-statement.  */
7374         statement = finish_return_stmt (expr);
7375         /* Look for the final `;'.  */
7376         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7377       }
7378       break;
7379
7380     case RID_GOTO:
7381       /* Create the goto-statement.  */
7382       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7383         {
7384           /* Issue a warning about this use of a GNU extension.  */
7385           if (pedantic)
7386             pedwarn ("ISO C++ forbids computed gotos");
7387           /* Consume the '*' token.  */
7388           cp_lexer_consume_token (parser->lexer);
7389           /* Parse the dependent expression.  */
7390           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7391         }
7392       else
7393         finish_goto_stmt (cp_parser_identifier (parser));
7394       /* Look for the final `;'.  */
7395       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7396       break;
7397
7398     default:
7399       cp_parser_error (parser, "expected jump-statement");
7400       break;
7401     }
7402
7403   return statement;
7404 }
7405
7406 /* Parse a declaration-statement.
7407
7408    declaration-statement:
7409      block-declaration  */
7410
7411 static void
7412 cp_parser_declaration_statement (cp_parser* parser)
7413 {
7414   void *p;
7415
7416   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7417   p = obstack_alloc (&declarator_obstack, 0);
7418
7419  /* Parse the block-declaration.  */
7420   cp_parser_block_declaration (parser, /*statement_p=*/true);
7421
7422   /* Free any declarators allocated.  */
7423   obstack_free (&declarator_obstack, p);
7424
7425   /* Finish off the statement.  */
7426   finish_stmt ();
7427 }
7428
7429 /* Some dependent statements (like `if (cond) statement'), are
7430    implicitly in their own scope.  In other words, if the statement is
7431    a single statement (as opposed to a compound-statement), it is
7432    none-the-less treated as if it were enclosed in braces.  Any
7433    declarations appearing in the dependent statement are out of scope
7434    after control passes that point.  This function parses a statement,
7435    but ensures that is in its own scope, even if it is not a
7436    compound-statement.
7437
7438    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7439    is a (possibly labeled) if statement which is not enclosed in
7440    braces and has an else clause.  This is used to implement
7441    -Wparentheses.
7442
7443    Returns the new statement.  */
7444
7445 static tree
7446 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7447 {
7448   tree statement;
7449
7450   if (if_p != NULL)
7451     *if_p = false;
7452
7453   /* Mark if () ; with a special NOP_EXPR.  */
7454   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7455     {
7456       cp_lexer_consume_token (parser->lexer);
7457       statement = add_stmt (build_empty_stmt ());
7458     }
7459   /* if a compound is opened, we simply parse the statement directly.  */
7460   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7461     statement = cp_parser_compound_statement (parser, NULL, false);
7462   /* If the token is not a `{', then we must take special action.  */
7463   else
7464     {
7465       /* Create a compound-statement.  */
7466       statement = begin_compound_stmt (0);
7467       /* Parse the dependent-statement.  */
7468       cp_parser_statement (parser, NULL_TREE, false, if_p);
7469       /* Finish the dummy compound-statement.  */
7470       finish_compound_stmt (statement);
7471     }
7472
7473   /* Return the statement.  */
7474   return statement;
7475 }
7476
7477 /* For some dependent statements (like `while (cond) statement'), we
7478    have already created a scope.  Therefore, even if the dependent
7479    statement is a compound-statement, we do not want to create another
7480    scope.  */
7481
7482 static void
7483 cp_parser_already_scoped_statement (cp_parser* parser)
7484 {
7485   /* If the token is a `{', then we must take special action.  */
7486   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7487     cp_parser_statement (parser, NULL_TREE, false, NULL);
7488   else
7489     {
7490       /* Avoid calling cp_parser_compound_statement, so that we
7491          don't create a new scope.  Do everything else by hand.  */
7492       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7493       cp_parser_statement_seq_opt (parser, NULL_TREE);
7494       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7495     }
7496 }
7497
7498 /* Declarations [gram.dcl.dcl] */
7499
7500 /* Parse an optional declaration-sequence.
7501
7502    declaration-seq:
7503      declaration
7504      declaration-seq declaration  */
7505
7506 static void
7507 cp_parser_declaration_seq_opt (cp_parser* parser)
7508 {
7509   while (true)
7510     {
7511       cp_token *token;
7512
7513       token = cp_lexer_peek_token (parser->lexer);
7514
7515       if (token->type == CPP_CLOSE_BRACE
7516           || token->type == CPP_EOF
7517           || token->type == CPP_PRAGMA_EOL)
7518         break;
7519
7520       if (token->type == CPP_SEMICOLON)
7521         {
7522           /* A declaration consisting of a single semicolon is
7523              invalid.  Allow it unless we're being pedantic.  */
7524           cp_lexer_consume_token (parser->lexer);
7525           if (pedantic && !in_system_header)
7526             pedwarn ("extra %<;%>");
7527           continue;
7528         }
7529
7530       /* If we're entering or exiting a region that's implicitly
7531          extern "C", modify the lang context appropriately.  */
7532       if (!parser->implicit_extern_c && token->implicit_extern_c)
7533         {
7534           push_lang_context (lang_name_c);
7535           parser->implicit_extern_c = true;
7536         }
7537       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7538         {
7539           pop_lang_context ();
7540           parser->implicit_extern_c = false;
7541         }
7542
7543       if (token->type == CPP_PRAGMA)
7544         {
7545           /* A top-level declaration can consist solely of a #pragma.
7546              A nested declaration cannot, so this is done here and not
7547              in cp_parser_declaration.  (A #pragma at block scope is
7548              handled in cp_parser_statement.)  */
7549           cp_parser_pragma (parser, pragma_external);
7550           continue;
7551         }
7552
7553       /* Parse the declaration itself.  */
7554       cp_parser_declaration (parser);
7555     }
7556 }
7557
7558 /* Parse a declaration.
7559
7560    declaration:
7561      block-declaration
7562      function-definition
7563      template-declaration
7564      explicit-instantiation
7565      explicit-specialization
7566      linkage-specification
7567      namespace-definition
7568
7569    GNU extension:
7570
7571    declaration:
7572       __extension__ declaration */
7573
7574 static void
7575 cp_parser_declaration (cp_parser* parser)
7576 {
7577   cp_token token1;
7578   cp_token token2;
7579   int saved_pedantic;
7580   void *p;
7581
7582   /* Check for the `__extension__' keyword.  */
7583   if (cp_parser_extension_opt (parser, &saved_pedantic))
7584     {
7585       /* Parse the qualified declaration.  */
7586       cp_parser_declaration (parser);
7587       /* Restore the PEDANTIC flag.  */
7588       pedantic = saved_pedantic;
7589
7590       return;
7591     }
7592
7593   /* Try to figure out what kind of declaration is present.  */
7594   token1 = *cp_lexer_peek_token (parser->lexer);
7595
7596   if (token1.type != CPP_EOF)
7597     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7598   else
7599     {
7600       token2.type = CPP_EOF;
7601       token2.keyword = RID_MAX;
7602     }
7603
7604   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7605   p = obstack_alloc (&declarator_obstack, 0);
7606
7607   /* If the next token is `extern' and the following token is a string
7608      literal, then we have a linkage specification.  */
7609   if (token1.keyword == RID_EXTERN
7610       && cp_parser_is_string_literal (&token2))
7611     cp_parser_linkage_specification (parser);
7612   /* If the next token is `template', then we have either a template
7613      declaration, an explicit instantiation, or an explicit
7614      specialization.  */
7615   else if (token1.keyword == RID_TEMPLATE)
7616     {
7617       /* `template <>' indicates a template specialization.  */
7618       if (token2.type == CPP_LESS
7619           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7620         cp_parser_explicit_specialization (parser);
7621       /* `template <' indicates a template declaration.  */
7622       else if (token2.type == CPP_LESS)
7623         cp_parser_template_declaration (parser, /*member_p=*/false);
7624       /* Anything else must be an explicit instantiation.  */
7625       else
7626         cp_parser_explicit_instantiation (parser);
7627     }
7628   /* If the next token is `export', then we have a template
7629      declaration.  */
7630   else if (token1.keyword == RID_EXPORT)
7631     cp_parser_template_declaration (parser, /*member_p=*/false);
7632   /* If the next token is `extern', 'static' or 'inline' and the one
7633      after that is `template', we have a GNU extended explicit
7634      instantiation directive.  */
7635   else if (cp_parser_allow_gnu_extensions_p (parser)
7636            && (token1.keyword == RID_EXTERN
7637                || token1.keyword == RID_STATIC
7638                || token1.keyword == RID_INLINE)
7639            && token2.keyword == RID_TEMPLATE)
7640     cp_parser_explicit_instantiation (parser);
7641   /* If the next token is `namespace', check for a named or unnamed
7642      namespace definition.  */
7643   else if (token1.keyword == RID_NAMESPACE
7644            && (/* A named namespace definition.  */
7645                (token2.type == CPP_NAME
7646                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7647                     != CPP_EQ))
7648                /* An unnamed namespace definition.  */
7649                || token2.type == CPP_OPEN_BRACE
7650                || token2.keyword == RID_ATTRIBUTE))
7651     cp_parser_namespace_definition (parser);
7652   /* Objective-C++ declaration/definition.  */
7653   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7654     cp_parser_objc_declaration (parser);
7655   /* We must have either a block declaration or a function
7656      definition.  */
7657   else
7658     /* Try to parse a block-declaration, or a function-definition.  */
7659     cp_parser_block_declaration (parser, /*statement_p=*/false);
7660
7661   /* Free any declarators allocated.  */
7662   obstack_free (&declarator_obstack, p);
7663 }
7664
7665 /* Parse a block-declaration.
7666
7667    block-declaration:
7668      simple-declaration
7669      asm-definition
7670      namespace-alias-definition
7671      using-declaration
7672      using-directive
7673
7674    GNU Extension:
7675
7676    block-declaration:
7677      __extension__ block-declaration
7678      label-declaration
7679
7680    C++0x Extension:
7681
7682    block-declaration:
7683      static_assert-declaration
7684
7685    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7686    part of a declaration-statement.  */
7687
7688 static void
7689 cp_parser_block_declaration (cp_parser *parser,
7690                              bool      statement_p)
7691 {
7692   cp_token *token1;
7693   int saved_pedantic;
7694
7695   /* Check for the `__extension__' keyword.  */
7696   if (cp_parser_extension_opt (parser, &saved_pedantic))
7697     {
7698       /* Parse the qualified declaration.  */
7699       cp_parser_block_declaration (parser, statement_p);
7700       /* Restore the PEDANTIC flag.  */
7701       pedantic = saved_pedantic;
7702
7703       return;
7704     }
7705
7706   /* Peek at the next token to figure out which kind of declaration is
7707      present.  */
7708   token1 = cp_lexer_peek_token (parser->lexer);
7709
7710   /* If the next keyword is `asm', we have an asm-definition.  */
7711   if (token1->keyword == RID_ASM)
7712     {
7713       if (statement_p)
7714         cp_parser_commit_to_tentative_parse (parser);
7715       cp_parser_asm_definition (parser);
7716     }
7717   /* If the next keyword is `namespace', we have a
7718      namespace-alias-definition.  */
7719   else if (token1->keyword == RID_NAMESPACE)
7720     cp_parser_namespace_alias_definition (parser);
7721   /* If the next keyword is `using', we have either a
7722      using-declaration or a using-directive.  */
7723   else if (token1->keyword == RID_USING)
7724     {
7725       cp_token *token2;
7726
7727       if (statement_p)
7728         cp_parser_commit_to_tentative_parse (parser);
7729       /* If the token after `using' is `namespace', then we have a
7730          using-directive.  */
7731       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7732       if (token2->keyword == RID_NAMESPACE)
7733         cp_parser_using_directive (parser);
7734       /* Otherwise, it's a using-declaration.  */
7735       else
7736         cp_parser_using_declaration (parser,
7737                                      /*access_declaration_p=*/false);
7738     }
7739   /* If the next keyword is `__label__' we have a label declaration.  */
7740   else if (token1->keyword == RID_LABEL)
7741     {
7742       if (statement_p)
7743         cp_parser_commit_to_tentative_parse (parser);
7744       cp_parser_label_declaration (parser);
7745     }
7746   /* If the next token is `static_assert' we have a static assertion.  */
7747   else if (token1->keyword == RID_STATIC_ASSERT)
7748     cp_parser_static_assert (parser, /*member_p=*/false);
7749   /* Anything else must be a simple-declaration.  */
7750   else
7751     cp_parser_simple_declaration (parser, !statement_p);
7752 }
7753
7754 /* Parse a simple-declaration.
7755
7756    simple-declaration:
7757      decl-specifier-seq [opt] init-declarator-list [opt] ;
7758
7759    init-declarator-list:
7760      init-declarator
7761      init-declarator-list , init-declarator
7762
7763    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7764    function-definition as a simple-declaration.  */
7765
7766 static void
7767 cp_parser_simple_declaration (cp_parser* parser,
7768                               bool function_definition_allowed_p)
7769 {
7770   cp_decl_specifier_seq decl_specifiers;
7771   int declares_class_or_enum;
7772   bool saw_declarator;
7773
7774   /* Defer access checks until we know what is being declared; the
7775      checks for names appearing in the decl-specifier-seq should be
7776      done as if we were in the scope of the thing being declared.  */
7777   push_deferring_access_checks (dk_deferred);
7778
7779   /* Parse the decl-specifier-seq.  We have to keep track of whether
7780      or not the decl-specifier-seq declares a named class or
7781      enumeration type, since that is the only case in which the
7782      init-declarator-list is allowed to be empty.
7783
7784      [dcl.dcl]
7785
7786      In a simple-declaration, the optional init-declarator-list can be
7787      omitted only when declaring a class or enumeration, that is when
7788      the decl-specifier-seq contains either a class-specifier, an
7789      elaborated-type-specifier, or an enum-specifier.  */
7790   cp_parser_decl_specifier_seq (parser,
7791                                 CP_PARSER_FLAGS_OPTIONAL,
7792                                 &decl_specifiers,
7793                                 &declares_class_or_enum);
7794   /* We no longer need to defer access checks.  */
7795   stop_deferring_access_checks ();
7796
7797   /* In a block scope, a valid declaration must always have a
7798      decl-specifier-seq.  By not trying to parse declarators, we can
7799      resolve the declaration/expression ambiguity more quickly.  */
7800   if (!function_definition_allowed_p
7801       && !decl_specifiers.any_specifiers_p)
7802     {
7803       cp_parser_error (parser, "expected declaration");
7804       goto done;
7805     }
7806
7807   /* If the next two tokens are both identifiers, the code is
7808      erroneous. The usual cause of this situation is code like:
7809
7810        T t;
7811
7812      where "T" should name a type -- but does not.  */
7813   if (!decl_specifiers.type
7814       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7815     {
7816       /* If parsing tentatively, we should commit; we really are
7817          looking at a declaration.  */
7818       cp_parser_commit_to_tentative_parse (parser);
7819       /* Give up.  */
7820       goto done;
7821     }
7822
7823   /* If we have seen at least one decl-specifier, and the next token
7824      is not a parenthesis, then we must be looking at a declaration.
7825      (After "int (" we might be looking at a functional cast.)  */
7826   if (decl_specifiers.any_specifiers_p
7827       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7828     cp_parser_commit_to_tentative_parse (parser);
7829
7830   /* Keep going until we hit the `;' at the end of the simple
7831      declaration.  */
7832   saw_declarator = false;
7833   while (cp_lexer_next_token_is_not (parser->lexer,
7834                                      CPP_SEMICOLON))
7835     {
7836       cp_token *token;
7837       bool function_definition_p;
7838       tree decl;
7839
7840       if (saw_declarator)
7841         {
7842           /* If we are processing next declarator, coma is expected */
7843           token = cp_lexer_peek_token (parser->lexer);
7844           gcc_assert (token->type == CPP_COMMA);
7845           cp_lexer_consume_token (parser->lexer);
7846         }
7847       else
7848         saw_declarator = true;
7849
7850       /* Parse the init-declarator.  */
7851       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7852                                         /*checks=*/NULL,
7853                                         function_definition_allowed_p,
7854                                         /*member_p=*/false,
7855                                         declares_class_or_enum,
7856                                         &function_definition_p);
7857       /* If an error occurred while parsing tentatively, exit quickly.
7858          (That usually happens when in the body of a function; each
7859          statement is treated as a declaration-statement until proven
7860          otherwise.)  */
7861       if (cp_parser_error_occurred (parser))
7862         goto done;
7863       /* Handle function definitions specially.  */
7864       if (function_definition_p)
7865         {
7866           /* If the next token is a `,', then we are probably
7867              processing something like:
7868
7869                void f() {}, *p;
7870
7871              which is erroneous.  */
7872           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7873             error ("mixing declarations and function-definitions is forbidden");
7874           /* Otherwise, we're done with the list of declarators.  */
7875           else
7876             {
7877               pop_deferring_access_checks ();
7878               return;
7879             }
7880         }
7881       /* The next token should be either a `,' or a `;'.  */
7882       token = cp_lexer_peek_token (parser->lexer);
7883       /* If it's a `,', there are more declarators to come.  */
7884       if (token->type == CPP_COMMA)
7885         /* will be consumed next time around */;
7886       /* If it's a `;', we are done.  */
7887       else if (token->type == CPP_SEMICOLON)
7888         break;
7889       /* Anything else is an error.  */
7890       else
7891         {
7892           /* If we have already issued an error message we don't need
7893              to issue another one.  */
7894           if (decl != error_mark_node
7895               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7896             cp_parser_error (parser, "expected %<,%> or %<;%>");
7897           /* Skip tokens until we reach the end of the statement.  */
7898           cp_parser_skip_to_end_of_statement (parser);
7899           /* If the next token is now a `;', consume it.  */
7900           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7901             cp_lexer_consume_token (parser->lexer);
7902           goto done;
7903         }
7904       /* After the first time around, a function-definition is not
7905          allowed -- even if it was OK at first.  For example:
7906
7907            int i, f() {}
7908
7909          is not valid.  */
7910       function_definition_allowed_p = false;
7911     }
7912
7913   /* Issue an error message if no declarators are present, and the
7914      decl-specifier-seq does not itself declare a class or
7915      enumeration.  */
7916   if (!saw_declarator)
7917     {
7918       if (cp_parser_declares_only_class_p (parser))
7919         shadow_tag (&decl_specifiers);
7920       /* Perform any deferred access checks.  */
7921       perform_deferred_access_checks ();
7922     }
7923
7924   /* Consume the `;'.  */
7925   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7926
7927  done:
7928   pop_deferring_access_checks ();
7929 }
7930
7931 /* Parse a decl-specifier-seq.
7932
7933    decl-specifier-seq:
7934      decl-specifier-seq [opt] decl-specifier
7935
7936    decl-specifier:
7937      storage-class-specifier
7938      type-specifier
7939      function-specifier
7940      friend
7941      typedef
7942
7943    GNU Extension:
7944
7945    decl-specifier:
7946      attributes
7947
7948    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7949
7950    The parser flags FLAGS is used to control type-specifier parsing.
7951
7952    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7953    flags:
7954
7955      1: one of the decl-specifiers is an elaborated-type-specifier
7956         (i.e., a type declaration)
7957      2: one of the decl-specifiers is an enum-specifier or a
7958         class-specifier (i.e., a type definition)
7959
7960    */
7961
7962 static void
7963 cp_parser_decl_specifier_seq (cp_parser* parser,
7964                               cp_parser_flags flags,
7965                               cp_decl_specifier_seq *decl_specs,
7966                               int* declares_class_or_enum)
7967 {
7968   bool constructor_possible_p = !parser->in_declarator_p;
7969
7970   /* Clear DECL_SPECS.  */
7971   clear_decl_specs (decl_specs);
7972
7973   /* Assume no class or enumeration type is declared.  */
7974   *declares_class_or_enum = 0;
7975
7976   /* Keep reading specifiers until there are no more to read.  */
7977   while (true)
7978     {
7979       bool constructor_p;
7980       bool found_decl_spec;
7981       cp_token *token;
7982
7983       /* Peek at the next token.  */
7984       token = cp_lexer_peek_token (parser->lexer);
7985       /* Handle attributes.  */
7986       if (token->keyword == RID_ATTRIBUTE)
7987         {
7988           /* Parse the attributes.  */
7989           decl_specs->attributes
7990             = chainon (decl_specs->attributes,
7991                        cp_parser_attributes_opt (parser));
7992           continue;
7993         }
7994       /* Assume we will find a decl-specifier keyword.  */
7995       found_decl_spec = true;
7996       /* If the next token is an appropriate keyword, we can simply
7997          add it to the list.  */
7998       switch (token->keyword)
7999         {
8000           /* decl-specifier:
8001                friend  */
8002         case RID_FRIEND:
8003           if (!at_class_scope_p ())
8004             {
8005               error ("%<friend%> used outside of class");
8006               cp_lexer_purge_token (parser->lexer);
8007             }
8008           else
8009             {
8010               ++decl_specs->specs[(int) ds_friend];
8011               /* Consume the token.  */
8012               cp_lexer_consume_token (parser->lexer);
8013             }
8014           break;
8015
8016           /* function-specifier:
8017                inline
8018                virtual
8019                explicit  */
8020         case RID_INLINE:
8021         case RID_VIRTUAL:
8022         case RID_EXPLICIT:
8023           cp_parser_function_specifier_opt (parser, decl_specs);
8024           break;
8025
8026           /* decl-specifier:
8027                typedef  */
8028         case RID_TYPEDEF:
8029           ++decl_specs->specs[(int) ds_typedef];
8030           /* Consume the token.  */
8031           cp_lexer_consume_token (parser->lexer);
8032           /* A constructor declarator cannot appear in a typedef.  */
8033           constructor_possible_p = false;
8034           /* The "typedef" keyword can only occur in a declaration; we
8035              may as well commit at this point.  */
8036           cp_parser_commit_to_tentative_parse (parser);
8037
8038           if (decl_specs->storage_class != sc_none)
8039             decl_specs->conflicting_specifiers_p = true;
8040           break;
8041
8042           /* storage-class-specifier:
8043                auto
8044                register
8045                static
8046                extern
8047                mutable
8048
8049              GNU Extension:
8050                thread  */
8051         case RID_AUTO:
8052         case RID_REGISTER:
8053         case RID_STATIC:
8054         case RID_EXTERN:
8055         case RID_MUTABLE:
8056           /* Consume the token.  */
8057           cp_lexer_consume_token (parser->lexer);
8058           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8059           break;
8060         case RID_THREAD:
8061           /* Consume the token.  */
8062           cp_lexer_consume_token (parser->lexer);
8063           ++decl_specs->specs[(int) ds_thread];
8064           break;
8065
8066         default:
8067           /* We did not yet find a decl-specifier yet.  */
8068           found_decl_spec = false;
8069           break;
8070         }
8071
8072       /* Constructors are a special case.  The `S' in `S()' is not a
8073          decl-specifier; it is the beginning of the declarator.  */
8074       constructor_p
8075         = (!found_decl_spec
8076            && constructor_possible_p
8077            && (cp_parser_constructor_declarator_p
8078                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8079
8080       /* If we don't have a DECL_SPEC yet, then we must be looking at
8081          a type-specifier.  */
8082       if (!found_decl_spec && !constructor_p)
8083         {
8084           int decl_spec_declares_class_or_enum;
8085           bool is_cv_qualifier;
8086           tree type_spec;
8087
8088           type_spec
8089             = cp_parser_type_specifier (parser, flags,
8090                                         decl_specs,
8091                                         /*is_declaration=*/true,
8092                                         &decl_spec_declares_class_or_enum,
8093                                         &is_cv_qualifier);
8094
8095           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8096
8097           /* If this type-specifier referenced a user-defined type
8098              (a typedef, class-name, etc.), then we can't allow any
8099              more such type-specifiers henceforth.
8100
8101              [dcl.spec]
8102
8103              The longest sequence of decl-specifiers that could
8104              possibly be a type name is taken as the
8105              decl-specifier-seq of a declaration.  The sequence shall
8106              be self-consistent as described below.
8107
8108              [dcl.type]
8109
8110              As a general rule, at most one type-specifier is allowed
8111              in the complete decl-specifier-seq of a declaration.  The
8112              only exceptions are the following:
8113
8114              -- const or volatile can be combined with any other
8115                 type-specifier.
8116
8117              -- signed or unsigned can be combined with char, long,
8118                 short, or int.
8119
8120              -- ..
8121
8122              Example:
8123
8124                typedef char* Pc;
8125                void g (const int Pc);
8126
8127              Here, Pc is *not* part of the decl-specifier seq; it's
8128              the declarator.  Therefore, once we see a type-specifier
8129              (other than a cv-qualifier), we forbid any additional
8130              user-defined types.  We *do* still allow things like `int
8131              int' to be considered a decl-specifier-seq, and issue the
8132              error message later.  */
8133           if (type_spec && !is_cv_qualifier)
8134             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8135           /* A constructor declarator cannot follow a type-specifier.  */
8136           if (type_spec)
8137             {
8138               constructor_possible_p = false;
8139               found_decl_spec = true;
8140             }
8141         }
8142
8143       /* If we still do not have a DECL_SPEC, then there are no more
8144          decl-specifiers.  */
8145       if (!found_decl_spec)
8146         break;
8147
8148       decl_specs->any_specifiers_p = true;
8149       /* After we see one decl-specifier, further decl-specifiers are
8150          always optional.  */
8151       flags |= CP_PARSER_FLAGS_OPTIONAL;
8152     }
8153
8154   cp_parser_check_decl_spec (decl_specs);
8155
8156   /* Don't allow a friend specifier with a class definition.  */
8157   if (decl_specs->specs[(int) ds_friend] != 0
8158       && (*declares_class_or_enum & 2))
8159     error ("class definition may not be declared a friend");
8160 }
8161
8162 /* Parse an (optional) storage-class-specifier.
8163
8164    storage-class-specifier:
8165      auto
8166      register
8167      static
8168      extern
8169      mutable
8170
8171    GNU Extension:
8172
8173    storage-class-specifier:
8174      thread
8175
8176    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8177
8178 static tree
8179 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8180 {
8181   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8182     {
8183     case RID_AUTO:
8184     case RID_REGISTER:
8185     case RID_STATIC:
8186     case RID_EXTERN:
8187     case RID_MUTABLE:
8188     case RID_THREAD:
8189       /* Consume the token.  */
8190       return cp_lexer_consume_token (parser->lexer)->u.value;
8191
8192     default:
8193       return NULL_TREE;
8194     }
8195 }
8196
8197 /* Parse an (optional) function-specifier.
8198
8199    function-specifier:
8200      inline
8201      virtual
8202      explicit
8203
8204    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8205    Updates DECL_SPECS, if it is non-NULL.  */
8206
8207 static tree
8208 cp_parser_function_specifier_opt (cp_parser* parser,
8209                                   cp_decl_specifier_seq *decl_specs)
8210 {
8211   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8212     {
8213     case RID_INLINE:
8214       if (decl_specs)
8215         ++decl_specs->specs[(int) ds_inline];
8216       break;
8217
8218     case RID_VIRTUAL:
8219       /* 14.5.2.3 [temp.mem]
8220
8221          A member function template shall not be virtual.  */
8222       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8223         error ("templates may not be %<virtual%>");
8224       else if (decl_specs)
8225         ++decl_specs->specs[(int) ds_virtual];
8226       break;
8227
8228     case RID_EXPLICIT:
8229       if (decl_specs)
8230         ++decl_specs->specs[(int) ds_explicit];
8231       break;
8232
8233     default:
8234       return NULL_TREE;
8235     }
8236
8237   /* Consume the token.  */
8238   return cp_lexer_consume_token (parser->lexer)->u.value;
8239 }
8240
8241 /* Parse a linkage-specification.
8242
8243    linkage-specification:
8244      extern string-literal { declaration-seq [opt] }
8245      extern string-literal declaration  */
8246
8247 static void
8248 cp_parser_linkage_specification (cp_parser* parser)
8249 {
8250   tree linkage;
8251
8252   /* Look for the `extern' keyword.  */
8253   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8254
8255   /* Look for the string-literal.  */
8256   linkage = cp_parser_string_literal (parser, false, false);
8257
8258   /* Transform the literal into an identifier.  If the literal is a
8259      wide-character string, or contains embedded NULs, then we can't
8260      handle it as the user wants.  */
8261   if (strlen (TREE_STRING_POINTER (linkage))
8262       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8263     {
8264       cp_parser_error (parser, "invalid linkage-specification");
8265       /* Assume C++ linkage.  */
8266       linkage = lang_name_cplusplus;
8267     }
8268   else
8269     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8270
8271   /* We're now using the new linkage.  */
8272   push_lang_context (linkage);
8273
8274   /* If the next token is a `{', then we're using the first
8275      production.  */
8276   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8277     {
8278       /* Consume the `{' token.  */
8279       cp_lexer_consume_token (parser->lexer);
8280       /* Parse the declarations.  */
8281       cp_parser_declaration_seq_opt (parser);
8282       /* Look for the closing `}'.  */
8283       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8284     }
8285   /* Otherwise, there's just one declaration.  */
8286   else
8287     {
8288       bool saved_in_unbraced_linkage_specification_p;
8289
8290       saved_in_unbraced_linkage_specification_p
8291         = parser->in_unbraced_linkage_specification_p;
8292       parser->in_unbraced_linkage_specification_p = true;
8293       cp_parser_declaration (parser);
8294       parser->in_unbraced_linkage_specification_p
8295         = saved_in_unbraced_linkage_specification_p;
8296     }
8297
8298   /* We're done with the linkage-specification.  */
8299   pop_lang_context ();
8300 }
8301
8302 /* Parse a static_assert-declaration.
8303
8304    static_assert-declaration:
8305      static_assert ( constant-expression , string-literal ) ; 
8306
8307    If MEMBER_P, this static_assert is a class member.  */
8308
8309 static void 
8310 cp_parser_static_assert(cp_parser *parser, bool member_p)
8311 {
8312   tree condition;
8313   tree message;
8314   cp_token *token;
8315   location_t saved_loc;
8316
8317   /* Peek at the `static_assert' token so we can keep track of exactly
8318      where the static assertion started.  */
8319   token = cp_lexer_peek_token (parser->lexer);
8320   saved_loc = token->location;
8321
8322   /* Look for the `static_assert' keyword.  */
8323   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8324                                   "`static_assert'"))
8325     return;
8326
8327   /*  We know we are in a static assertion; commit to any tentative
8328       parse.  */
8329   if (cp_parser_parsing_tentatively (parser))
8330     cp_parser_commit_to_tentative_parse (parser);
8331
8332   /* Parse the `(' starting the static assertion condition.  */
8333   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8334
8335   /* Parse the constant-expression.  */
8336   condition = 
8337     cp_parser_constant_expression (parser,
8338                                    /*allow_non_constant_p=*/false,
8339                                    /*non_constant_p=*/NULL);
8340
8341   /* Parse the separating `,'.  */
8342   cp_parser_require (parser, CPP_COMMA, "`,'");
8343
8344   /* Parse the string-literal message.  */
8345   message = cp_parser_string_literal (parser, 
8346                                       /*translate=*/false,
8347                                       /*wide_ok=*/true);
8348
8349   /* A `)' completes the static assertion.  */
8350   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8351     cp_parser_skip_to_closing_parenthesis (parser, 
8352                                            /*recovering=*/true, 
8353                                            /*or_comma=*/false,
8354                                            /*consume_paren=*/true);
8355
8356   /* A semicolon terminates the declaration.  */
8357   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8358
8359   /* Complete the static assertion, which may mean either processing 
8360      the static assert now or saving it for template instantiation.  */
8361   finish_static_assert (condition, message, saved_loc, member_p);
8362 }
8363
8364 /* Special member functions [gram.special] */
8365
8366 /* Parse a conversion-function-id.
8367
8368    conversion-function-id:
8369      operator conversion-type-id
8370
8371    Returns an IDENTIFIER_NODE representing the operator.  */
8372
8373 static tree
8374 cp_parser_conversion_function_id (cp_parser* parser)
8375 {
8376   tree type;
8377   tree saved_scope;
8378   tree saved_qualifying_scope;
8379   tree saved_object_scope;
8380   tree pushed_scope = NULL_TREE;
8381
8382   /* Look for the `operator' token.  */
8383   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8384     return error_mark_node;
8385   /* When we parse the conversion-type-id, the current scope will be
8386      reset.  However, we need that information in able to look up the
8387      conversion function later, so we save it here.  */
8388   saved_scope = parser->scope;
8389   saved_qualifying_scope = parser->qualifying_scope;
8390   saved_object_scope = parser->object_scope;
8391   /* We must enter the scope of the class so that the names of
8392      entities declared within the class are available in the
8393      conversion-type-id.  For example, consider:
8394
8395        struct S {
8396          typedef int I;
8397          operator I();
8398        };
8399
8400        S::operator I() { ... }
8401
8402      In order to see that `I' is a type-name in the definition, we
8403      must be in the scope of `S'.  */
8404   if (saved_scope)
8405     pushed_scope = push_scope (saved_scope);
8406   /* Parse the conversion-type-id.  */
8407   type = cp_parser_conversion_type_id (parser);
8408   /* Leave the scope of the class, if any.  */
8409   if (pushed_scope)
8410     pop_scope (pushed_scope);
8411   /* Restore the saved scope.  */
8412   parser->scope = saved_scope;
8413   parser->qualifying_scope = saved_qualifying_scope;
8414   parser->object_scope = saved_object_scope;
8415   /* If the TYPE is invalid, indicate failure.  */
8416   if (type == error_mark_node)
8417     return error_mark_node;
8418   return mangle_conv_op_name_for_type (type);
8419 }
8420
8421 /* Parse a conversion-type-id:
8422
8423    conversion-type-id:
8424      type-specifier-seq conversion-declarator [opt]
8425
8426    Returns the TYPE specified.  */
8427
8428 static tree
8429 cp_parser_conversion_type_id (cp_parser* parser)
8430 {
8431   tree attributes;
8432   cp_decl_specifier_seq type_specifiers;
8433   cp_declarator *declarator;
8434   tree type_specified;
8435
8436   /* Parse the attributes.  */
8437   attributes = cp_parser_attributes_opt (parser);
8438   /* Parse the type-specifiers.  */
8439   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8440                                 &type_specifiers);
8441   /* If that didn't work, stop.  */
8442   if (type_specifiers.type == error_mark_node)
8443     return error_mark_node;
8444   /* Parse the conversion-declarator.  */
8445   declarator = cp_parser_conversion_declarator_opt (parser);
8446
8447   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8448                                     /*initialized=*/0, &attributes);
8449   if (attributes)
8450     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8451   return type_specified;
8452 }
8453
8454 /* Parse an (optional) conversion-declarator.
8455
8456    conversion-declarator:
8457      ptr-operator conversion-declarator [opt]
8458
8459    */
8460
8461 static cp_declarator *
8462 cp_parser_conversion_declarator_opt (cp_parser* parser)
8463 {
8464   enum tree_code code;
8465   tree class_type;
8466   cp_cv_quals cv_quals;
8467
8468   /* We don't know if there's a ptr-operator next, or not.  */
8469   cp_parser_parse_tentatively (parser);
8470   /* Try the ptr-operator.  */
8471   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8472   /* If it worked, look for more conversion-declarators.  */
8473   if (cp_parser_parse_definitely (parser))
8474     {
8475       cp_declarator *declarator;
8476
8477       /* Parse another optional declarator.  */
8478       declarator = cp_parser_conversion_declarator_opt (parser);
8479
8480       return cp_parser_make_indirect_declarator
8481         (code, class_type, cv_quals, declarator);
8482    }
8483
8484   return NULL;
8485 }
8486
8487 /* Parse an (optional) ctor-initializer.
8488
8489    ctor-initializer:
8490      : mem-initializer-list
8491
8492    Returns TRUE iff the ctor-initializer was actually present.  */
8493
8494 static bool
8495 cp_parser_ctor_initializer_opt (cp_parser* parser)
8496 {
8497   /* If the next token is not a `:', then there is no
8498      ctor-initializer.  */
8499   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8500     {
8501       /* Do default initialization of any bases and members.  */
8502       if (DECL_CONSTRUCTOR_P (current_function_decl))
8503         finish_mem_initializers (NULL_TREE);
8504
8505       return false;
8506     }
8507
8508   /* Consume the `:' token.  */
8509   cp_lexer_consume_token (parser->lexer);
8510   /* And the mem-initializer-list.  */
8511   cp_parser_mem_initializer_list (parser);
8512
8513   return true;
8514 }
8515
8516 /* Parse a mem-initializer-list.
8517
8518    mem-initializer-list:
8519      mem-initializer ... [opt]
8520      mem-initializer ... [opt] , mem-initializer-list  */
8521
8522 static void
8523 cp_parser_mem_initializer_list (cp_parser* parser)
8524 {
8525   tree mem_initializer_list = NULL_TREE;
8526
8527   /* Let the semantic analysis code know that we are starting the
8528      mem-initializer-list.  */
8529   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8530     error ("only constructors take base initializers");
8531
8532   /* Loop through the list.  */
8533   while (true)
8534     {
8535       tree mem_initializer;
8536
8537       /* Parse the mem-initializer.  */
8538       mem_initializer = cp_parser_mem_initializer (parser);
8539       /* If the next token is a `...', we're expanding member initializers. */
8540       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8541         {
8542           /* Consume the `...'. */
8543           cp_lexer_consume_token (parser->lexer);
8544
8545           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8546              can be expanded but members cannot. */
8547           if (mem_initializer != error_mark_node
8548               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8549             {
8550               error ("cannot expand initializer for member %<%D%>", 
8551                      TREE_PURPOSE (mem_initializer));
8552               mem_initializer = error_mark_node;
8553             }
8554
8555           /* Construct the pack expansion type. */
8556           if (mem_initializer != error_mark_node)
8557             mem_initializer = make_pack_expansion (mem_initializer);
8558         }
8559       /* Add it to the list, unless it was erroneous.  */
8560       if (mem_initializer != error_mark_node)
8561         {
8562           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8563           mem_initializer_list = mem_initializer;
8564         }
8565       /* If the next token is not a `,', we're done.  */
8566       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8567         break;
8568       /* Consume the `,' token.  */
8569       cp_lexer_consume_token (parser->lexer);
8570     }
8571
8572   /* Perform semantic analysis.  */
8573   if (DECL_CONSTRUCTOR_P (current_function_decl))
8574     finish_mem_initializers (mem_initializer_list);
8575 }
8576
8577 /* Parse a mem-initializer.
8578
8579    mem-initializer:
8580      mem-initializer-id ( expression-list [opt] )
8581
8582    GNU extension:
8583
8584    mem-initializer:
8585      ( expression-list [opt] )
8586
8587    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8588    class) or FIELD_DECL (for a non-static data member) to initialize;
8589    the TREE_VALUE is the expression-list.  An empty initialization
8590    list is represented by void_list_node.  */
8591
8592 static tree
8593 cp_parser_mem_initializer (cp_parser* parser)
8594 {
8595   tree mem_initializer_id;
8596   tree expression_list;
8597   tree member;
8598
8599   /* Find out what is being initialized.  */
8600   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8601     {
8602       pedwarn ("anachronistic old-style base class initializer");
8603       mem_initializer_id = NULL_TREE;
8604     }
8605   else
8606     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8607   member = expand_member_init (mem_initializer_id);
8608   if (member && !DECL_P (member))
8609     in_base_initializer = 1;
8610
8611   expression_list
8612     = cp_parser_parenthesized_expression_list (parser, false,
8613                                                /*cast_p=*/false,
8614                                                /*allow_expansion_p=*/true,
8615                                                /*non_constant_p=*/NULL);
8616   if (expression_list == error_mark_node)
8617     return error_mark_node;
8618   if (!expression_list)
8619     expression_list = void_type_node;
8620
8621   in_base_initializer = 0;
8622
8623   return member ? build_tree_list (member, expression_list) : error_mark_node;
8624 }
8625
8626 /* Parse a mem-initializer-id.
8627
8628    mem-initializer-id:
8629      :: [opt] nested-name-specifier [opt] class-name
8630      identifier
8631
8632    Returns a TYPE indicating the class to be initializer for the first
8633    production.  Returns an IDENTIFIER_NODE indicating the data member
8634    to be initialized for the second production.  */
8635
8636 static tree
8637 cp_parser_mem_initializer_id (cp_parser* parser)
8638 {
8639   bool global_scope_p;
8640   bool nested_name_specifier_p;
8641   bool template_p = false;
8642   tree id;
8643
8644   /* `typename' is not allowed in this context ([temp.res]).  */
8645   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8646     {
8647       error ("keyword %<typename%> not allowed in this context (a qualified "
8648              "member initializer is implicitly a type)");
8649       cp_lexer_consume_token (parser->lexer);
8650     }
8651   /* Look for the optional `::' operator.  */
8652   global_scope_p
8653     = (cp_parser_global_scope_opt (parser,
8654                                    /*current_scope_valid_p=*/false)
8655        != NULL_TREE);
8656   /* Look for the optional nested-name-specifier.  The simplest way to
8657      implement:
8658
8659        [temp.res]
8660
8661        The keyword `typename' is not permitted in a base-specifier or
8662        mem-initializer; in these contexts a qualified name that
8663        depends on a template-parameter is implicitly assumed to be a
8664        type name.
8665
8666      is to assume that we have seen the `typename' keyword at this
8667      point.  */
8668   nested_name_specifier_p
8669     = (cp_parser_nested_name_specifier_opt (parser,
8670                                             /*typename_keyword_p=*/true,
8671                                             /*check_dependency_p=*/true,
8672                                             /*type_p=*/true,
8673                                             /*is_declaration=*/true)
8674        != NULL_TREE);
8675   if (nested_name_specifier_p)
8676     template_p = cp_parser_optional_template_keyword (parser);
8677   /* If there is a `::' operator or a nested-name-specifier, then we
8678      are definitely looking for a class-name.  */
8679   if (global_scope_p || nested_name_specifier_p)
8680     return cp_parser_class_name (parser,
8681                                  /*typename_keyword_p=*/true,
8682                                  /*template_keyword_p=*/template_p,
8683                                  none_type,
8684                                  /*check_dependency_p=*/true,
8685                                  /*class_head_p=*/false,
8686                                  /*is_declaration=*/true);
8687   /* Otherwise, we could also be looking for an ordinary identifier.  */
8688   cp_parser_parse_tentatively (parser);
8689   /* Try a class-name.  */
8690   id = cp_parser_class_name (parser,
8691                              /*typename_keyword_p=*/true,
8692                              /*template_keyword_p=*/false,
8693                              none_type,
8694                              /*check_dependency_p=*/true,
8695                              /*class_head_p=*/false,
8696                              /*is_declaration=*/true);
8697   /* If we found one, we're done.  */
8698   if (cp_parser_parse_definitely (parser))
8699     return id;
8700   /* Otherwise, look for an ordinary identifier.  */
8701   return cp_parser_identifier (parser);
8702 }
8703
8704 /* Overloading [gram.over] */
8705
8706 /* Parse an operator-function-id.
8707
8708    operator-function-id:
8709      operator operator
8710
8711    Returns an IDENTIFIER_NODE for the operator which is a
8712    human-readable spelling of the identifier, e.g., `operator +'.  */
8713
8714 static tree
8715 cp_parser_operator_function_id (cp_parser* parser)
8716 {
8717   /* Look for the `operator' keyword.  */
8718   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8719     return error_mark_node;
8720   /* And then the name of the operator itself.  */
8721   return cp_parser_operator (parser);
8722 }
8723
8724 /* Parse an operator.
8725
8726    operator:
8727      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8728      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8729      || ++ -- , ->* -> () []
8730
8731    GNU Extensions:
8732
8733    operator:
8734      <? >? <?= >?=
8735
8736    Returns an IDENTIFIER_NODE for the operator which is a
8737    human-readable spelling of the identifier, e.g., `operator +'.  */
8738
8739 static tree
8740 cp_parser_operator (cp_parser* parser)
8741 {
8742   tree id = NULL_TREE;
8743   cp_token *token;
8744
8745   /* Peek at the next token.  */
8746   token = cp_lexer_peek_token (parser->lexer);
8747   /* Figure out which operator we have.  */
8748   switch (token->type)
8749     {
8750     case CPP_KEYWORD:
8751       {
8752         enum tree_code op;
8753
8754         /* The keyword should be either `new' or `delete'.  */
8755         if (token->keyword == RID_NEW)
8756           op = NEW_EXPR;
8757         else if (token->keyword == RID_DELETE)
8758           op = DELETE_EXPR;
8759         else
8760           break;
8761
8762         /* Consume the `new' or `delete' token.  */
8763         cp_lexer_consume_token (parser->lexer);
8764
8765         /* Peek at the next token.  */
8766         token = cp_lexer_peek_token (parser->lexer);
8767         /* If it's a `[' token then this is the array variant of the
8768            operator.  */
8769         if (token->type == CPP_OPEN_SQUARE)
8770           {
8771             /* Consume the `[' token.  */
8772             cp_lexer_consume_token (parser->lexer);
8773             /* Look for the `]' token.  */
8774             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8775             id = ansi_opname (op == NEW_EXPR
8776                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8777           }
8778         /* Otherwise, we have the non-array variant.  */
8779         else
8780           id = ansi_opname (op);
8781
8782         return id;
8783       }
8784
8785     case CPP_PLUS:
8786       id = ansi_opname (PLUS_EXPR);
8787       break;
8788
8789     case CPP_MINUS:
8790       id = ansi_opname (MINUS_EXPR);
8791       break;
8792
8793     case CPP_MULT:
8794       id = ansi_opname (MULT_EXPR);
8795       break;
8796
8797     case CPP_DIV:
8798       id = ansi_opname (TRUNC_DIV_EXPR);
8799       break;
8800
8801     case CPP_MOD:
8802       id = ansi_opname (TRUNC_MOD_EXPR);
8803       break;
8804
8805     case CPP_XOR:
8806       id = ansi_opname (BIT_XOR_EXPR);
8807       break;
8808
8809     case CPP_AND:
8810       id = ansi_opname (BIT_AND_EXPR);
8811       break;
8812
8813     case CPP_OR:
8814       id = ansi_opname (BIT_IOR_EXPR);
8815       break;
8816
8817     case CPP_COMPL:
8818       id = ansi_opname (BIT_NOT_EXPR);
8819       break;
8820
8821     case CPP_NOT:
8822       id = ansi_opname (TRUTH_NOT_EXPR);
8823       break;
8824
8825     case CPP_EQ:
8826       id = ansi_assopname (NOP_EXPR);
8827       break;
8828
8829     case CPP_LESS:
8830       id = ansi_opname (LT_EXPR);
8831       break;
8832
8833     case CPP_GREATER:
8834       id = ansi_opname (GT_EXPR);
8835       break;
8836
8837     case CPP_PLUS_EQ:
8838       id = ansi_assopname (PLUS_EXPR);
8839       break;
8840
8841     case CPP_MINUS_EQ:
8842       id = ansi_assopname (MINUS_EXPR);
8843       break;
8844
8845     case CPP_MULT_EQ:
8846       id = ansi_assopname (MULT_EXPR);
8847       break;
8848
8849     case CPP_DIV_EQ:
8850       id = ansi_assopname (TRUNC_DIV_EXPR);
8851       break;
8852
8853     case CPP_MOD_EQ:
8854       id = ansi_assopname (TRUNC_MOD_EXPR);
8855       break;
8856
8857     case CPP_XOR_EQ:
8858       id = ansi_assopname (BIT_XOR_EXPR);
8859       break;
8860
8861     case CPP_AND_EQ:
8862       id = ansi_assopname (BIT_AND_EXPR);
8863       break;
8864
8865     case CPP_OR_EQ:
8866       id = ansi_assopname (BIT_IOR_EXPR);
8867       break;
8868
8869     case CPP_LSHIFT:
8870       id = ansi_opname (LSHIFT_EXPR);
8871       break;
8872
8873     case CPP_RSHIFT:
8874       id = ansi_opname (RSHIFT_EXPR);
8875       break;
8876
8877     case CPP_LSHIFT_EQ:
8878       id = ansi_assopname (LSHIFT_EXPR);
8879       break;
8880
8881     case CPP_RSHIFT_EQ:
8882       id = ansi_assopname (RSHIFT_EXPR);
8883       break;
8884
8885     case CPP_EQ_EQ:
8886       id = ansi_opname (EQ_EXPR);
8887       break;
8888
8889     case CPP_NOT_EQ:
8890       id = ansi_opname (NE_EXPR);
8891       break;
8892
8893     case CPP_LESS_EQ:
8894       id = ansi_opname (LE_EXPR);
8895       break;
8896
8897     case CPP_GREATER_EQ:
8898       id = ansi_opname (GE_EXPR);
8899       break;
8900
8901     case CPP_AND_AND:
8902       id = ansi_opname (TRUTH_ANDIF_EXPR);
8903       break;
8904
8905     case CPP_OR_OR:
8906       id = ansi_opname (TRUTH_ORIF_EXPR);
8907       break;
8908
8909     case CPP_PLUS_PLUS:
8910       id = ansi_opname (POSTINCREMENT_EXPR);
8911       break;
8912
8913     case CPP_MINUS_MINUS:
8914       id = ansi_opname (PREDECREMENT_EXPR);
8915       break;
8916
8917     case CPP_COMMA:
8918       id = ansi_opname (COMPOUND_EXPR);
8919       break;
8920
8921     case CPP_DEREF_STAR:
8922       id = ansi_opname (MEMBER_REF);
8923       break;
8924
8925     case CPP_DEREF:
8926       id = ansi_opname (COMPONENT_REF);
8927       break;
8928
8929     case CPP_OPEN_PAREN:
8930       /* Consume the `('.  */
8931       cp_lexer_consume_token (parser->lexer);
8932       /* Look for the matching `)'.  */
8933       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8934       return ansi_opname (CALL_EXPR);
8935
8936     case CPP_OPEN_SQUARE:
8937       /* Consume the `['.  */
8938       cp_lexer_consume_token (parser->lexer);
8939       /* Look for the matching `]'.  */
8940       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8941       return ansi_opname (ARRAY_REF);
8942
8943     default:
8944       /* Anything else is an error.  */
8945       break;
8946     }
8947
8948   /* If we have selected an identifier, we need to consume the
8949      operator token.  */
8950   if (id)
8951     cp_lexer_consume_token (parser->lexer);
8952   /* Otherwise, no valid operator name was present.  */
8953   else
8954     {
8955       cp_parser_error (parser, "expected operator");
8956       id = error_mark_node;
8957     }
8958
8959   return id;
8960 }
8961
8962 /* Parse a template-declaration.
8963
8964    template-declaration:
8965      export [opt] template < template-parameter-list > declaration
8966
8967    If MEMBER_P is TRUE, this template-declaration occurs within a
8968    class-specifier.
8969
8970    The grammar rule given by the standard isn't correct.  What
8971    is really meant is:
8972
8973    template-declaration:
8974      export [opt] template-parameter-list-seq
8975        decl-specifier-seq [opt] init-declarator [opt] ;
8976      export [opt] template-parameter-list-seq
8977        function-definition
8978
8979    template-parameter-list-seq:
8980      template-parameter-list-seq [opt]
8981      template < template-parameter-list >  */
8982
8983 static void
8984 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8985 {
8986   /* Check for `export'.  */
8987   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8988     {
8989       /* Consume the `export' token.  */
8990       cp_lexer_consume_token (parser->lexer);
8991       /* Warn that we do not support `export'.  */
8992       warning (0, "keyword %<export%> not implemented, and will be ignored");
8993     }
8994
8995   cp_parser_template_declaration_after_export (parser, member_p);
8996 }
8997
8998 /* Parse a template-parameter-list.
8999
9000    template-parameter-list:
9001      template-parameter
9002      template-parameter-list , template-parameter
9003
9004    Returns a TREE_LIST.  Each node represents a template parameter.
9005    The nodes are connected via their TREE_CHAINs.  */
9006
9007 static tree
9008 cp_parser_template_parameter_list (cp_parser* parser)
9009 {
9010   tree parameter_list = NULL_TREE;
9011
9012   begin_template_parm_list ();
9013   while (true)
9014     {
9015       tree parameter;
9016       cp_token *token;
9017       bool is_non_type;
9018       bool is_parameter_pack;
9019
9020       /* Parse the template-parameter.  */
9021       parameter = cp_parser_template_parameter (parser, 
9022                                                 &is_non_type,
9023                                                 &is_parameter_pack);
9024       /* Add it to the list.  */
9025       if (parameter != error_mark_node)
9026         parameter_list = process_template_parm (parameter_list,
9027                                                 parameter,
9028                                                 is_non_type,
9029                                                 is_parameter_pack);
9030       else
9031        {
9032          tree err_parm = build_tree_list (parameter, parameter);
9033          TREE_VALUE (err_parm) = error_mark_node;
9034          parameter_list = chainon (parameter_list, err_parm);
9035        }
9036
9037       /* Peek at the next token.  */
9038       token = cp_lexer_peek_token (parser->lexer);
9039       /* If it's not a `,', we're done.  */
9040       if (token->type != CPP_COMMA)
9041         break;
9042       /* Otherwise, consume the `,' token.  */
9043       cp_lexer_consume_token (parser->lexer);
9044     }
9045
9046   return end_template_parm_list (parameter_list);
9047 }
9048
9049 /* Parse a template-parameter.
9050
9051    template-parameter:
9052      type-parameter
9053      parameter-declaration
9054
9055    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9056    the parameter.  The TREE_PURPOSE is the default value, if any.
9057    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9058    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9059    set to true iff this parameter is a parameter pack. */
9060
9061 static tree
9062 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9063                               bool *is_parameter_pack)
9064 {
9065   cp_token *token;
9066   cp_parameter_declarator *parameter_declarator;
9067   tree parm;
9068
9069   /* Assume it is a type parameter or a template parameter.  */
9070   *is_non_type = false;
9071   /* Assume it not a parameter pack. */
9072   *is_parameter_pack = false;
9073   /* Peek at the next token.  */
9074   token = cp_lexer_peek_token (parser->lexer);
9075   /* If it is `class' or `template', we have a type-parameter.  */
9076   if (token->keyword == RID_TEMPLATE)
9077     return cp_parser_type_parameter (parser, is_parameter_pack);
9078   /* If it is `class' or `typename' we do not know yet whether it is a
9079      type parameter or a non-type parameter.  Consider:
9080
9081        template <typename T, typename T::X X> ...
9082
9083      or:
9084
9085        template <class C, class D*> ...
9086
9087      Here, the first parameter is a type parameter, and the second is
9088      a non-type parameter.  We can tell by looking at the token after
9089      the identifier -- if it is a `,', `=', or `>' then we have a type
9090      parameter.  */
9091   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9092     {
9093       /* Peek at the token after `class' or `typename'.  */
9094       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9095       /* If it's an ellipsis, we have a template type parameter
9096          pack. */
9097       if (token->type == CPP_ELLIPSIS)
9098         return cp_parser_type_parameter (parser, is_parameter_pack);
9099       /* If it's an identifier, skip it.  */
9100       if (token->type == CPP_NAME)
9101         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9102       /* Now, see if the token looks like the end of a template
9103          parameter.  */
9104       if (token->type == CPP_COMMA
9105           || token->type == CPP_EQ
9106           || token->type == CPP_GREATER)
9107         return cp_parser_type_parameter (parser, is_parameter_pack);
9108     }
9109
9110   /* Otherwise, it is a non-type parameter.
9111
9112      [temp.param]
9113
9114      When parsing a default template-argument for a non-type
9115      template-parameter, the first non-nested `>' is taken as the end
9116      of the template parameter-list rather than a greater-than
9117      operator.  */
9118   *is_non_type = true;
9119   parameter_declarator
9120      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9121                                         /*parenthesized_p=*/NULL);
9122
9123   /* If the parameter declaration is marked as a parameter pack, set
9124      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9125      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9126      grokdeclarator. */
9127   if (parameter_declarator
9128       && parameter_declarator->declarator
9129       && parameter_declarator->declarator->parameter_pack_p)
9130     {
9131       *is_parameter_pack = true;
9132       parameter_declarator->declarator->parameter_pack_p = false;
9133     }
9134
9135   /* If the next token is an ellipsis, and we don't already have it
9136      marked as a parameter pack, then we have a parameter pack (that
9137      has no declarator); */
9138   if (!*is_parameter_pack
9139       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9140       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9141     {
9142       /* Consume the `...'. */
9143       cp_lexer_consume_token (parser->lexer);
9144       maybe_warn_variadic_templates ();
9145       
9146       *is_parameter_pack = true;
9147     }
9148
9149   parm = grokdeclarator (parameter_declarator->declarator,
9150                          &parameter_declarator->decl_specifiers,
9151                          PARM, /*initialized=*/0,
9152                          /*attrlist=*/NULL);
9153   if (parm == error_mark_node)
9154     return error_mark_node;
9155
9156   return build_tree_list (parameter_declarator->default_argument, parm);
9157 }
9158
9159 /* Parse a type-parameter.
9160
9161    type-parameter:
9162      class identifier [opt]
9163      class identifier [opt] = type-id
9164      typename identifier [opt]
9165      typename identifier [opt] = type-id
9166      template < template-parameter-list > class identifier [opt]
9167      template < template-parameter-list > class identifier [opt]
9168        = id-expression
9169
9170    GNU Extension (variadic templates):
9171
9172    type-parameter:
9173      class ... identifier [opt]
9174      typename ... identifier [opt]
9175
9176    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9177    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9178    the declaration of the parameter.
9179
9180    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9181
9182 static tree
9183 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9184 {
9185   cp_token *token;
9186   tree parameter;
9187
9188   /* Look for a keyword to tell us what kind of parameter this is.  */
9189   token = cp_parser_require (parser, CPP_KEYWORD,
9190                              "`class', `typename', or `template'");
9191   if (!token)
9192     return error_mark_node;
9193
9194   switch (token->keyword)
9195     {
9196     case RID_CLASS:
9197     case RID_TYPENAME:
9198       {
9199         tree identifier;
9200         tree default_argument;
9201
9202         /* If the next token is an ellipsis, we have a template
9203            argument pack. */
9204         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9205           {
9206             /* Consume the `...' token. */
9207             cp_lexer_consume_token (parser->lexer);
9208             maybe_warn_variadic_templates ();
9209
9210             *is_parameter_pack = true;
9211           }
9212
9213         /* If the next token is an identifier, then it names the
9214            parameter.  */
9215         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9216           identifier = cp_parser_identifier (parser);
9217         else
9218           identifier = NULL_TREE;
9219
9220         /* Create the parameter.  */
9221         parameter = finish_template_type_parm (class_type_node, identifier);
9222
9223         /* If the next token is an `=', we have a default argument.  */
9224         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9225           {
9226             /* Consume the `=' token.  */
9227             cp_lexer_consume_token (parser->lexer);
9228             /* Parse the default-argument.  */
9229             push_deferring_access_checks (dk_no_deferred);
9230             default_argument = cp_parser_type_id (parser);
9231
9232             /* Template parameter packs cannot have default
9233                arguments. */
9234             if (*is_parameter_pack)
9235               {
9236                 if (identifier)
9237                   error ("template parameter pack %qD cannot have a default argument", 
9238                          identifier);
9239                 else
9240                   error ("template parameter packs cannot have default arguments");
9241                 default_argument = NULL_TREE;
9242               }
9243             pop_deferring_access_checks ();
9244           }
9245         else
9246           default_argument = NULL_TREE;
9247
9248         /* Create the combined representation of the parameter and the
9249            default argument.  */
9250         parameter = build_tree_list (default_argument, parameter);
9251       }
9252       break;
9253
9254     case RID_TEMPLATE:
9255       {
9256         tree parameter_list;
9257         tree identifier;
9258         tree default_argument;
9259
9260         /* Look for the `<'.  */
9261         cp_parser_require (parser, CPP_LESS, "`<'");
9262         /* Parse the template-parameter-list.  */
9263         parameter_list = cp_parser_template_parameter_list (parser);
9264         /* Look for the `>'.  */
9265         cp_parser_require (parser, CPP_GREATER, "`>'");
9266         /* Look for the `class' keyword.  */
9267         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9268         /* If the next token is an ellipsis, we have a template
9269            argument pack. */
9270         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9271           {
9272             /* Consume the `...' token. */
9273             cp_lexer_consume_token (parser->lexer);
9274             maybe_warn_variadic_templates ();
9275
9276             *is_parameter_pack = true;
9277           }
9278         /* If the next token is an `=', then there is a
9279            default-argument.  If the next token is a `>', we are at
9280            the end of the parameter-list.  If the next token is a `,',
9281            then we are at the end of this parameter.  */
9282         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9283             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9284             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9285           {
9286             identifier = cp_parser_identifier (parser);
9287             /* Treat invalid names as if the parameter were nameless.  */
9288             if (identifier == error_mark_node)
9289               identifier = NULL_TREE;
9290           }
9291         else
9292           identifier = NULL_TREE;
9293
9294         /* Create the template parameter.  */
9295         parameter = finish_template_template_parm (class_type_node,
9296                                                    identifier);
9297
9298         /* If the next token is an `=', then there is a
9299            default-argument.  */
9300         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9301           {
9302             bool is_template;
9303
9304             /* Consume the `='.  */
9305             cp_lexer_consume_token (parser->lexer);
9306             /* Parse the id-expression.  */
9307             push_deferring_access_checks (dk_no_deferred);
9308             default_argument
9309               = cp_parser_id_expression (parser,
9310                                          /*template_keyword_p=*/false,
9311                                          /*check_dependency_p=*/true,
9312                                          /*template_p=*/&is_template,
9313                                          /*declarator_p=*/false,
9314                                          /*optional_p=*/false);
9315             if (TREE_CODE (default_argument) == TYPE_DECL)
9316               /* If the id-expression was a template-id that refers to
9317                  a template-class, we already have the declaration here,
9318                  so no further lookup is needed.  */
9319                  ;
9320             else
9321               /* Look up the name.  */
9322               default_argument
9323                 = cp_parser_lookup_name (parser, default_argument,
9324                                          none_type,
9325                                          /*is_template=*/is_template,
9326                                          /*is_namespace=*/false,
9327                                          /*check_dependency=*/true,
9328                                          /*ambiguous_decls=*/NULL);
9329             /* See if the default argument is valid.  */
9330             default_argument
9331               = check_template_template_default_arg (default_argument);
9332
9333             /* Template parameter packs cannot have default
9334                arguments. */
9335             if (*is_parameter_pack)
9336               {
9337                 if (identifier)
9338                   error ("template parameter pack %qD cannot have a default argument", 
9339                          identifier);
9340                 else
9341                   error ("template parameter packs cannot have default arguments");
9342                 default_argument = NULL_TREE;
9343               }
9344             pop_deferring_access_checks ();
9345           }
9346         else
9347           default_argument = NULL_TREE;
9348
9349         /* Create the combined representation of the parameter and the
9350            default argument.  */
9351         parameter = build_tree_list (default_argument, parameter);
9352       }
9353       break;
9354
9355     default:
9356       gcc_unreachable ();
9357       break;
9358     }
9359
9360   return parameter;
9361 }
9362
9363 /* Parse a template-id.
9364
9365    template-id:
9366      template-name < template-argument-list [opt] >
9367
9368    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9369    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9370    returned.  Otherwise, if the template-name names a function, or set
9371    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9372    names a class, returns a TYPE_DECL for the specialization.
9373
9374    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9375    uninstantiated templates.  */
9376
9377 static tree
9378 cp_parser_template_id (cp_parser *parser,
9379                        bool template_keyword_p,
9380                        bool check_dependency_p,
9381                        bool is_declaration)
9382 {
9383   int i;
9384   tree template;
9385   tree arguments;
9386   tree template_id;
9387   cp_token_position start_of_id = 0;
9388   deferred_access_check *chk;
9389   VEC (deferred_access_check,gc) *access_check;
9390   cp_token *next_token, *next_token_2;
9391   bool is_identifier;
9392
9393   /* If the next token corresponds to a template-id, there is no need
9394      to reparse it.  */
9395   next_token = cp_lexer_peek_token (parser->lexer);
9396   if (next_token->type == CPP_TEMPLATE_ID)
9397     {
9398       struct tree_check *check_value;
9399
9400       /* Get the stored value.  */
9401       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9402       /* Perform any access checks that were deferred.  */
9403       access_check = check_value->checks;
9404       if (access_check)
9405         {
9406           for (i = 0 ;
9407                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9408                ++i)
9409             {
9410               perform_or_defer_access_check (chk->binfo,
9411                                              chk->decl,
9412                                              chk->diag_decl);
9413             }
9414         }
9415       /* Return the stored value.  */
9416       return check_value->value;
9417     }
9418
9419   /* Avoid performing name lookup if there is no possibility of
9420      finding a template-id.  */
9421   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9422       || (next_token->type == CPP_NAME
9423           && !cp_parser_nth_token_starts_template_argument_list_p
9424                (parser, 2)))
9425     {
9426       cp_parser_error (parser, "expected template-id");
9427       return error_mark_node;
9428     }
9429
9430   /* Remember where the template-id starts.  */
9431   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9432     start_of_id = cp_lexer_token_position (parser->lexer, false);
9433
9434   push_deferring_access_checks (dk_deferred);
9435
9436   /* Parse the template-name.  */
9437   is_identifier = false;
9438   template = cp_parser_template_name (parser, template_keyword_p,
9439                                       check_dependency_p,
9440                                       is_declaration,
9441                                       &is_identifier);
9442   if (template == error_mark_node || is_identifier)
9443     {
9444       pop_deferring_access_checks ();
9445       return template;
9446     }
9447
9448   /* If we find the sequence `[:' after a template-name, it's probably
9449      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9450      parse correctly the argument list.  */
9451   next_token = cp_lexer_peek_token (parser->lexer);
9452   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9453   if (next_token->type == CPP_OPEN_SQUARE
9454       && next_token->flags & DIGRAPH
9455       && next_token_2->type == CPP_COLON
9456       && !(next_token_2->flags & PREV_WHITE))
9457     {
9458       cp_parser_parse_tentatively (parser);
9459       /* Change `:' into `::'.  */
9460       next_token_2->type = CPP_SCOPE;
9461       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9462          CPP_LESS.  */
9463       cp_lexer_consume_token (parser->lexer);
9464       /* Parse the arguments.  */
9465       arguments = cp_parser_enclosed_template_argument_list (parser);
9466       if (!cp_parser_parse_definitely (parser))
9467         {
9468           /* If we couldn't parse an argument list, then we revert our changes
9469              and return simply an error. Maybe this is not a template-id
9470              after all.  */
9471           next_token_2->type = CPP_COLON;
9472           cp_parser_error (parser, "expected %<<%>");
9473           pop_deferring_access_checks ();
9474           return error_mark_node;
9475         }
9476       /* Otherwise, emit an error about the invalid digraph, but continue
9477          parsing because we got our argument list.  */
9478       pedwarn ("%<<::%> cannot begin a template-argument list");
9479       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9480               "between %<<%> and %<::%>");
9481       if (!flag_permissive)
9482         {
9483           static bool hint;
9484           if (!hint)
9485             {
9486               inform ("(if you use -fpermissive G++ will accept your code)");
9487               hint = true;
9488             }
9489         }
9490     }
9491   else
9492     {
9493       /* Look for the `<' that starts the template-argument-list.  */
9494       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9495         {
9496           pop_deferring_access_checks ();
9497           return error_mark_node;
9498         }
9499       /* Parse the arguments.  */
9500       arguments = cp_parser_enclosed_template_argument_list (parser);
9501     }
9502
9503   /* Build a representation of the specialization.  */
9504   if (TREE_CODE (template) == IDENTIFIER_NODE)
9505     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9506   else if (DECL_CLASS_TEMPLATE_P (template)
9507            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9508     {
9509       bool entering_scope;
9510       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9511          template (rather than some instantiation thereof) only if
9512          is not nested within some other construct.  For example, in
9513          "template <typename T> void f(T) { A<T>::", A<T> is just an
9514          instantiation of A.  */
9515       entering_scope = (template_parm_scope_p ()
9516                         && cp_lexer_next_token_is (parser->lexer,
9517                                                    CPP_SCOPE));
9518       template_id
9519         = finish_template_type (template, arguments, entering_scope);
9520     }
9521   else
9522     {
9523       /* If it's not a class-template or a template-template, it should be
9524          a function-template.  */
9525       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9526                    || TREE_CODE (template) == OVERLOAD
9527                    || BASELINK_P (template)));
9528
9529       template_id = lookup_template_function (template, arguments);
9530     }
9531
9532   /* If parsing tentatively, replace the sequence of tokens that makes
9533      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9534      should we re-parse the token stream, we will not have to repeat
9535      the effort required to do the parse, nor will we issue duplicate
9536      error messages about problems during instantiation of the
9537      template.  */
9538   if (start_of_id)
9539     {
9540       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9541
9542       /* Reset the contents of the START_OF_ID token.  */
9543       token->type = CPP_TEMPLATE_ID;
9544       /* Retrieve any deferred checks.  Do not pop this access checks yet
9545          so the memory will not be reclaimed during token replacing below.  */
9546       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9547       token->u.tree_check_value->value = template_id;
9548       token->u.tree_check_value->checks = get_deferred_access_checks ();
9549       token->keyword = RID_MAX;
9550
9551       /* Purge all subsequent tokens.  */
9552       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9553
9554       /* ??? Can we actually assume that, if template_id ==
9555          error_mark_node, we will have issued a diagnostic to the
9556          user, as opposed to simply marking the tentative parse as
9557          failed?  */
9558       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9559         error ("parse error in template argument list");
9560     }
9561
9562   pop_deferring_access_checks ();
9563   return template_id;
9564 }
9565
9566 /* Parse a template-name.
9567
9568    template-name:
9569      identifier
9570
9571    The standard should actually say:
9572
9573    template-name:
9574      identifier
9575      operator-function-id
9576
9577    A defect report has been filed about this issue.
9578
9579    A conversion-function-id cannot be a template name because they cannot
9580    be part of a template-id. In fact, looking at this code:
9581
9582    a.operator K<int>()
9583
9584    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9585    It is impossible to call a templated conversion-function-id with an
9586    explicit argument list, since the only allowed template parameter is
9587    the type to which it is converting.
9588
9589    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9590    `template' keyword, in a construction like:
9591
9592      T::template f<3>()
9593
9594    In that case `f' is taken to be a template-name, even though there
9595    is no way of knowing for sure.
9596
9597    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9598    name refers to a set of overloaded functions, at least one of which
9599    is a template, or an IDENTIFIER_NODE with the name of the template,
9600    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9601    names are looked up inside uninstantiated templates.  */
9602
9603 static tree
9604 cp_parser_template_name (cp_parser* parser,
9605                          bool template_keyword_p,
9606                          bool check_dependency_p,
9607                          bool is_declaration,
9608                          bool *is_identifier)
9609 {
9610   tree identifier;
9611   tree decl;
9612   tree fns;
9613
9614   /* If the next token is `operator', then we have either an
9615      operator-function-id or a conversion-function-id.  */
9616   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9617     {
9618       /* We don't know whether we're looking at an
9619          operator-function-id or a conversion-function-id.  */
9620       cp_parser_parse_tentatively (parser);
9621       /* Try an operator-function-id.  */
9622       identifier = cp_parser_operator_function_id (parser);
9623       /* If that didn't work, try a conversion-function-id.  */
9624       if (!cp_parser_parse_definitely (parser))
9625         {
9626           cp_parser_error (parser, "expected template-name");
9627           return error_mark_node;
9628         }
9629     }
9630   /* Look for the identifier.  */
9631   else
9632     identifier = cp_parser_identifier (parser);
9633
9634   /* If we didn't find an identifier, we don't have a template-id.  */
9635   if (identifier == error_mark_node)
9636     return error_mark_node;
9637
9638   /* If the name immediately followed the `template' keyword, then it
9639      is a template-name.  However, if the next token is not `<', then
9640      we do not treat it as a template-name, since it is not being used
9641      as part of a template-id.  This enables us to handle constructs
9642      like:
9643
9644        template <typename T> struct S { S(); };
9645        template <typename T> S<T>::S();
9646
9647      correctly.  We would treat `S' as a template -- if it were `S<T>'
9648      -- but we do not if there is no `<'.  */
9649
9650   if (processing_template_decl
9651       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9652     {
9653       /* In a declaration, in a dependent context, we pretend that the
9654          "template" keyword was present in order to improve error
9655          recovery.  For example, given:
9656
9657            template <typename T> void f(T::X<int>);
9658
9659          we want to treat "X<int>" as a template-id.  */
9660       if (is_declaration
9661           && !template_keyword_p
9662           && parser->scope && TYPE_P (parser->scope)
9663           && check_dependency_p
9664           && dependent_type_p (parser->scope)
9665           /* Do not do this for dtors (or ctors), since they never
9666              need the template keyword before their name.  */
9667           && !constructor_name_p (identifier, parser->scope))
9668         {
9669           cp_token_position start = 0;
9670
9671           /* Explain what went wrong.  */
9672           error ("non-template %qD used as template", identifier);
9673           inform ("use %<%T::template %D%> to indicate that it is a template",
9674                   parser->scope, identifier);
9675           /* If parsing tentatively, find the location of the "<" token.  */
9676           if (cp_parser_simulate_error (parser))
9677             start = cp_lexer_token_position (parser->lexer, true);
9678           /* Parse the template arguments so that we can issue error
9679              messages about them.  */
9680           cp_lexer_consume_token (parser->lexer);
9681           cp_parser_enclosed_template_argument_list (parser);
9682           /* Skip tokens until we find a good place from which to
9683              continue parsing.  */
9684           cp_parser_skip_to_closing_parenthesis (parser,
9685                                                  /*recovering=*/true,
9686                                                  /*or_comma=*/true,
9687                                                  /*consume_paren=*/false);
9688           /* If parsing tentatively, permanently remove the
9689              template argument list.  That will prevent duplicate
9690              error messages from being issued about the missing
9691              "template" keyword.  */
9692           if (start)
9693             cp_lexer_purge_tokens_after (parser->lexer, start);
9694           if (is_identifier)
9695             *is_identifier = true;
9696           return identifier;
9697         }
9698
9699       /* If the "template" keyword is present, then there is generally
9700          no point in doing name-lookup, so we just return IDENTIFIER.
9701          But, if the qualifying scope is non-dependent then we can
9702          (and must) do name-lookup normally.  */
9703       if (template_keyword_p
9704           && (!parser->scope
9705               || (TYPE_P (parser->scope)
9706                   && dependent_type_p (parser->scope))))
9707         return identifier;
9708     }
9709
9710   /* Look up the name.  */
9711   decl = cp_parser_lookup_name (parser, identifier,
9712                                 none_type,
9713                                 /*is_template=*/false,
9714                                 /*is_namespace=*/false,
9715                                 check_dependency_p,
9716                                 /*ambiguous_decls=*/NULL);
9717   decl = maybe_get_template_decl_from_type_decl (decl);
9718
9719   /* If DECL is a template, then the name was a template-name.  */
9720   if (TREE_CODE (decl) == TEMPLATE_DECL)
9721     ;
9722   else
9723     {
9724       tree fn = NULL_TREE;
9725
9726       /* The standard does not explicitly indicate whether a name that
9727          names a set of overloaded declarations, some of which are
9728          templates, is a template-name.  However, such a name should
9729          be a template-name; otherwise, there is no way to form a
9730          template-id for the overloaded templates.  */
9731       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9732       if (TREE_CODE (fns) == OVERLOAD)
9733         for (fn = fns; fn; fn = OVL_NEXT (fn))
9734           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9735             break;
9736
9737       if (!fn)
9738         {
9739           /* The name does not name a template.  */
9740           cp_parser_error (parser, "expected template-name");
9741           return error_mark_node;
9742         }
9743     }
9744
9745   /* If DECL is dependent, and refers to a function, then just return
9746      its name; we will look it up again during template instantiation.  */
9747   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9748     {
9749       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9750       if (TYPE_P (scope) && dependent_type_p (scope))
9751         return identifier;
9752     }
9753
9754   return decl;
9755 }
9756
9757 /* Parse a template-argument-list.
9758
9759    template-argument-list:
9760      template-argument ... [opt]
9761      template-argument-list , template-argument ... [opt]
9762
9763    Returns a TREE_VEC containing the arguments.  */
9764
9765 static tree
9766 cp_parser_template_argument_list (cp_parser* parser)
9767 {
9768   tree fixed_args[10];
9769   unsigned n_args = 0;
9770   unsigned alloced = 10;
9771   tree *arg_ary = fixed_args;
9772   tree vec;
9773   bool saved_in_template_argument_list_p;
9774   bool saved_ice_p;
9775   bool saved_non_ice_p;
9776
9777   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9778   parser->in_template_argument_list_p = true;
9779   /* Even if the template-id appears in an integral
9780      constant-expression, the contents of the argument list do
9781      not.  */
9782   saved_ice_p = parser->integral_constant_expression_p;
9783   parser->integral_constant_expression_p = false;
9784   saved_non_ice_p = parser->non_integral_constant_expression_p;
9785   parser->non_integral_constant_expression_p = false;
9786   /* Parse the arguments.  */
9787   do
9788     {
9789       tree argument;
9790
9791       if (n_args)
9792         /* Consume the comma.  */
9793         cp_lexer_consume_token (parser->lexer);
9794
9795       /* Parse the template-argument.  */
9796       argument = cp_parser_template_argument (parser);
9797
9798       /* If the next token is an ellipsis, we're expanding a template
9799          argument pack. */
9800       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9801         {
9802           /* Consume the `...' token. */
9803           cp_lexer_consume_token (parser->lexer);
9804
9805           /* Make the argument into a TYPE_PACK_EXPANSION or
9806              EXPR_PACK_EXPANSION. */
9807           argument = make_pack_expansion (argument);
9808         }
9809
9810       if (n_args == alloced)
9811         {
9812           alloced *= 2;
9813
9814           if (arg_ary == fixed_args)
9815             {
9816               arg_ary = XNEWVEC (tree, alloced);
9817               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9818             }
9819           else
9820             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9821         }
9822       arg_ary[n_args++] = argument;
9823     }
9824   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9825
9826   vec = make_tree_vec (n_args);
9827
9828   while (n_args--)
9829     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9830
9831   if (arg_ary != fixed_args)
9832     free (arg_ary);
9833   parser->non_integral_constant_expression_p = saved_non_ice_p;
9834   parser->integral_constant_expression_p = saved_ice_p;
9835   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9836   return vec;
9837 }
9838
9839 /* Parse a template-argument.
9840
9841    template-argument:
9842      assignment-expression
9843      type-id
9844      id-expression
9845
9846    The representation is that of an assignment-expression, type-id, or
9847    id-expression -- except that the qualified id-expression is
9848    evaluated, so that the value returned is either a DECL or an
9849    OVERLOAD.
9850
9851    Although the standard says "assignment-expression", it forbids
9852    throw-expressions or assignments in the template argument.
9853    Therefore, we use "conditional-expression" instead.  */
9854
9855 static tree
9856 cp_parser_template_argument (cp_parser* parser)
9857 {
9858   tree argument;
9859   bool template_p;
9860   bool address_p;
9861   bool maybe_type_id = false;
9862   cp_token *token;
9863   cp_id_kind idk;
9864
9865   /* There's really no way to know what we're looking at, so we just
9866      try each alternative in order.
9867
9868        [temp.arg]
9869
9870        In a template-argument, an ambiguity between a type-id and an
9871        expression is resolved to a type-id, regardless of the form of
9872        the corresponding template-parameter.
9873
9874      Therefore, we try a type-id first.  */
9875   cp_parser_parse_tentatively (parser);
9876   argument = cp_parser_type_id (parser);
9877   /* If there was no error parsing the type-id but the next token is a '>>',
9878      we probably found a typo for '> >'. But there are type-id which are
9879      also valid expressions. For instance:
9880
9881      struct X { int operator >> (int); };
9882      template <int V> struct Foo {};
9883      Foo<X () >> 5> r;
9884
9885      Here 'X()' is a valid type-id of a function type, but the user just
9886      wanted to write the expression "X() >> 5". Thus, we remember that we
9887      found a valid type-id, but we still try to parse the argument as an
9888      expression to see what happens.  */
9889   if (!cp_parser_error_occurred (parser)
9890       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9891     {
9892       maybe_type_id = true;
9893       cp_parser_abort_tentative_parse (parser);
9894     }
9895   else
9896     {
9897       /* If the next token isn't a `,' or a `>', then this argument wasn't
9898       really finished. This means that the argument is not a valid
9899       type-id.  */
9900       if (!cp_parser_next_token_ends_template_argument_p (parser))
9901         cp_parser_error (parser, "expected template-argument");
9902       /* If that worked, we're done.  */
9903       if (cp_parser_parse_definitely (parser))
9904         return argument;
9905     }
9906   /* We're still not sure what the argument will be.  */
9907   cp_parser_parse_tentatively (parser);
9908   /* Try a template.  */
9909   argument = cp_parser_id_expression (parser,
9910                                       /*template_keyword_p=*/false,
9911                                       /*check_dependency_p=*/true,
9912                                       &template_p,
9913                                       /*declarator_p=*/false,
9914                                       /*optional_p=*/false);
9915   /* If the next token isn't a `,' or a `>', then this argument wasn't
9916      really finished.  */
9917   if (!cp_parser_next_token_ends_template_argument_p (parser))
9918     cp_parser_error (parser, "expected template-argument");
9919   if (!cp_parser_error_occurred (parser))
9920     {
9921       /* Figure out what is being referred to.  If the id-expression
9922          was for a class template specialization, then we will have a
9923          TYPE_DECL at this point.  There is no need to do name lookup
9924          at this point in that case.  */
9925       if (TREE_CODE (argument) != TYPE_DECL)
9926         argument = cp_parser_lookup_name (parser, argument,
9927                                           none_type,
9928                                           /*is_template=*/template_p,
9929                                           /*is_namespace=*/false,
9930                                           /*check_dependency=*/true,
9931                                           /*ambiguous_decls=*/NULL);
9932       if (TREE_CODE (argument) != TEMPLATE_DECL
9933           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9934         cp_parser_error (parser, "expected template-name");
9935     }
9936   if (cp_parser_parse_definitely (parser))
9937     return argument;
9938   /* It must be a non-type argument.  There permitted cases are given
9939      in [temp.arg.nontype]:
9940
9941      -- an integral constant-expression of integral or enumeration
9942         type; or
9943
9944      -- the name of a non-type template-parameter; or
9945
9946      -- the name of an object or function with external linkage...
9947
9948      -- the address of an object or function with external linkage...
9949
9950      -- a pointer to member...  */
9951   /* Look for a non-type template parameter.  */
9952   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9953     {
9954       cp_parser_parse_tentatively (parser);
9955       argument = cp_parser_primary_expression (parser,
9956                                                /*adress_p=*/false,
9957                                                /*cast_p=*/false,
9958                                                /*template_arg_p=*/true,
9959                                                &idk);
9960       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9961           || !cp_parser_next_token_ends_template_argument_p (parser))
9962         cp_parser_simulate_error (parser);
9963       if (cp_parser_parse_definitely (parser))
9964         return argument;
9965     }
9966
9967   /* If the next token is "&", the argument must be the address of an
9968      object or function with external linkage.  */
9969   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9970   if (address_p)
9971     cp_lexer_consume_token (parser->lexer);
9972   /* See if we might have an id-expression.  */
9973   token = cp_lexer_peek_token (parser->lexer);
9974   if (token->type == CPP_NAME
9975       || token->keyword == RID_OPERATOR
9976       || token->type == CPP_SCOPE
9977       || token->type == CPP_TEMPLATE_ID
9978       || token->type == CPP_NESTED_NAME_SPECIFIER)
9979     {
9980       cp_parser_parse_tentatively (parser);
9981       argument = cp_parser_primary_expression (parser,
9982                                                address_p,
9983                                                /*cast_p=*/false,
9984                                                /*template_arg_p=*/true,
9985                                                &idk);
9986       if (cp_parser_error_occurred (parser)
9987           || !cp_parser_next_token_ends_template_argument_p (parser))
9988         cp_parser_abort_tentative_parse (parser);
9989       else
9990         {
9991           if (TREE_CODE (argument) == INDIRECT_REF)
9992             {
9993               gcc_assert (REFERENCE_REF_P (argument));
9994               argument = TREE_OPERAND (argument, 0);
9995             }
9996
9997           if (TREE_CODE (argument) == VAR_DECL)
9998             {
9999               /* A variable without external linkage might still be a
10000                  valid constant-expression, so no error is issued here
10001                  if the external-linkage check fails.  */
10002               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10003                 cp_parser_simulate_error (parser);
10004             }
10005           else if (is_overloaded_fn (argument))
10006             /* All overloaded functions are allowed; if the external
10007                linkage test does not pass, an error will be issued
10008                later.  */
10009             ;
10010           else if (address_p
10011                    && (TREE_CODE (argument) == OFFSET_REF
10012                        || TREE_CODE (argument) == SCOPE_REF))
10013             /* A pointer-to-member.  */
10014             ;
10015           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10016             ;
10017           else
10018             cp_parser_simulate_error (parser);
10019
10020           if (cp_parser_parse_definitely (parser))
10021             {
10022               if (address_p)
10023                 argument = build_x_unary_op (ADDR_EXPR, argument);
10024               return argument;
10025             }
10026         }
10027     }
10028   /* If the argument started with "&", there are no other valid
10029      alternatives at this point.  */
10030   if (address_p)
10031     {
10032       cp_parser_error (parser, "invalid non-type template argument");
10033       return error_mark_node;
10034     }
10035
10036   /* If the argument wasn't successfully parsed as a type-id followed
10037      by '>>', the argument can only be a constant expression now.
10038      Otherwise, we try parsing the constant-expression tentatively,
10039      because the argument could really be a type-id.  */
10040   if (maybe_type_id)
10041     cp_parser_parse_tentatively (parser);
10042   argument = cp_parser_constant_expression (parser,
10043                                             /*allow_non_constant_p=*/false,
10044                                             /*non_constant_p=*/NULL);
10045   argument = fold_non_dependent_expr (argument);
10046   if (!maybe_type_id)
10047     return argument;
10048   if (!cp_parser_next_token_ends_template_argument_p (parser))
10049     cp_parser_error (parser, "expected template-argument");
10050   if (cp_parser_parse_definitely (parser))
10051     return argument;
10052   /* We did our best to parse the argument as a non type-id, but that
10053      was the only alternative that matched (albeit with a '>' after
10054      it). We can assume it's just a typo from the user, and a
10055      diagnostic will then be issued.  */
10056   return cp_parser_type_id (parser);
10057 }
10058
10059 /* Parse an explicit-instantiation.
10060
10061    explicit-instantiation:
10062      template declaration
10063
10064    Although the standard says `declaration', what it really means is:
10065
10066    explicit-instantiation:
10067      template decl-specifier-seq [opt] declarator [opt] ;
10068
10069    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10070    supposed to be allowed.  A defect report has been filed about this
10071    issue.
10072
10073    GNU Extension:
10074
10075    explicit-instantiation:
10076      storage-class-specifier template
10077        decl-specifier-seq [opt] declarator [opt] ;
10078      function-specifier template
10079        decl-specifier-seq [opt] declarator [opt] ;  */
10080
10081 static void
10082 cp_parser_explicit_instantiation (cp_parser* parser)
10083 {
10084   int declares_class_or_enum;
10085   cp_decl_specifier_seq decl_specifiers;
10086   tree extension_specifier = NULL_TREE;
10087
10088   /* Look for an (optional) storage-class-specifier or
10089      function-specifier.  */
10090   if (cp_parser_allow_gnu_extensions_p (parser))
10091     {
10092       extension_specifier
10093         = cp_parser_storage_class_specifier_opt (parser);
10094       if (!extension_specifier)
10095         extension_specifier
10096           = cp_parser_function_specifier_opt (parser,
10097                                               /*decl_specs=*/NULL);
10098     }
10099
10100   /* Look for the `template' keyword.  */
10101   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10102   /* Let the front end know that we are processing an explicit
10103      instantiation.  */
10104   begin_explicit_instantiation ();
10105   /* [temp.explicit] says that we are supposed to ignore access
10106      control while processing explicit instantiation directives.  */
10107   push_deferring_access_checks (dk_no_check);
10108   /* Parse a decl-specifier-seq.  */
10109   cp_parser_decl_specifier_seq (parser,
10110                                 CP_PARSER_FLAGS_OPTIONAL,
10111                                 &decl_specifiers,
10112                                 &declares_class_or_enum);
10113   /* If there was exactly one decl-specifier, and it declared a class,
10114      and there's no declarator, then we have an explicit type
10115      instantiation.  */
10116   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10117     {
10118       tree type;
10119
10120       type = check_tag_decl (&decl_specifiers);
10121       /* Turn access control back on for names used during
10122          template instantiation.  */
10123       pop_deferring_access_checks ();
10124       if (type)
10125         do_type_instantiation (type, extension_specifier,
10126                                /*complain=*/tf_error);
10127     }
10128   else
10129     {
10130       cp_declarator *declarator;
10131       tree decl;
10132
10133       /* Parse the declarator.  */
10134       declarator
10135         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10136                                 /*ctor_dtor_or_conv_p=*/NULL,
10137                                 /*parenthesized_p=*/NULL,
10138                                 /*member_p=*/false);
10139       if (declares_class_or_enum & 2)
10140         cp_parser_check_for_definition_in_return_type (declarator,
10141                                                        decl_specifiers.type);
10142       if (declarator != cp_error_declarator)
10143         {
10144           decl = grokdeclarator (declarator, &decl_specifiers,
10145                                  NORMAL, 0, &decl_specifiers.attributes);
10146           /* Turn access control back on for names used during
10147              template instantiation.  */
10148           pop_deferring_access_checks ();
10149           /* Do the explicit instantiation.  */
10150           do_decl_instantiation (decl, extension_specifier);
10151         }
10152       else
10153         {
10154           pop_deferring_access_checks ();
10155           /* Skip the body of the explicit instantiation.  */
10156           cp_parser_skip_to_end_of_statement (parser);
10157         }
10158     }
10159   /* We're done with the instantiation.  */
10160   end_explicit_instantiation ();
10161
10162   cp_parser_consume_semicolon_at_end_of_statement (parser);
10163 }
10164
10165 /* Parse an explicit-specialization.
10166
10167    explicit-specialization:
10168      template < > declaration
10169
10170    Although the standard says `declaration', what it really means is:
10171
10172    explicit-specialization:
10173      template <> decl-specifier [opt] init-declarator [opt] ;
10174      template <> function-definition
10175      template <> explicit-specialization
10176      template <> template-declaration  */
10177
10178 static void
10179 cp_parser_explicit_specialization (cp_parser* parser)
10180 {
10181   bool need_lang_pop;
10182   /* Look for the `template' keyword.  */
10183   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10184   /* Look for the `<'.  */
10185   cp_parser_require (parser, CPP_LESS, "`<'");
10186   /* Look for the `>'.  */
10187   cp_parser_require (parser, CPP_GREATER, "`>'");
10188   /* We have processed another parameter list.  */
10189   ++parser->num_template_parameter_lists;
10190   /* [temp]
10191
10192      A template ... explicit specialization ... shall not have C
10193      linkage.  */
10194   if (current_lang_name == lang_name_c)
10195     {
10196       error ("template specialization with C linkage");
10197       /* Give it C++ linkage to avoid confusing other parts of the
10198          front end.  */
10199       push_lang_context (lang_name_cplusplus);
10200       need_lang_pop = true;
10201     }
10202   else
10203     need_lang_pop = false;
10204   /* Let the front end know that we are beginning a specialization.  */
10205   if (!begin_specialization ())
10206     {
10207       end_specialization ();
10208       cp_parser_skip_to_end_of_block_or_statement (parser);
10209       return;
10210     }
10211
10212   /* If the next keyword is `template', we need to figure out whether
10213      or not we're looking a template-declaration.  */
10214   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10215     {
10216       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10217           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10218         cp_parser_template_declaration_after_export (parser,
10219                                                      /*member_p=*/false);
10220       else
10221         cp_parser_explicit_specialization (parser);
10222     }
10223   else
10224     /* Parse the dependent declaration.  */
10225     cp_parser_single_declaration (parser,
10226                                   /*checks=*/NULL,
10227                                   /*member_p=*/false,
10228                                   /*explicit_specialization_p=*/true,
10229                                   /*friend_p=*/NULL);
10230   /* We're done with the specialization.  */
10231   end_specialization ();
10232   /* For the erroneous case of a template with C linkage, we pushed an
10233      implicit C++ linkage scope; exit that scope now.  */
10234   if (need_lang_pop)
10235     pop_lang_context ();
10236   /* We're done with this parameter list.  */
10237   --parser->num_template_parameter_lists;
10238 }
10239
10240 /* Parse a type-specifier.
10241
10242    type-specifier:
10243      simple-type-specifier
10244      class-specifier
10245      enum-specifier
10246      elaborated-type-specifier
10247      cv-qualifier
10248
10249    GNU Extension:
10250
10251    type-specifier:
10252      __complex__
10253
10254    Returns a representation of the type-specifier.  For a
10255    class-specifier, enum-specifier, or elaborated-type-specifier, a
10256    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10257
10258    The parser flags FLAGS is used to control type-specifier parsing.
10259
10260    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10261    in a decl-specifier-seq.
10262
10263    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10264    class-specifier, enum-specifier, or elaborated-type-specifier, then
10265    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10266    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10267    zero.
10268
10269    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10270    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10271    is set to FALSE.  */
10272
10273 static tree
10274 cp_parser_type_specifier (cp_parser* parser,
10275                           cp_parser_flags flags,
10276                           cp_decl_specifier_seq *decl_specs,
10277                           bool is_declaration,
10278                           int* declares_class_or_enum,
10279                           bool* is_cv_qualifier)
10280 {
10281   tree type_spec = NULL_TREE;
10282   cp_token *token;
10283   enum rid keyword;
10284   cp_decl_spec ds = ds_last;
10285
10286   /* Assume this type-specifier does not declare a new type.  */
10287   if (declares_class_or_enum)
10288     *declares_class_or_enum = 0;
10289   /* And that it does not specify a cv-qualifier.  */
10290   if (is_cv_qualifier)
10291     *is_cv_qualifier = false;
10292   /* Peek at the next token.  */
10293   token = cp_lexer_peek_token (parser->lexer);
10294
10295   /* If we're looking at a keyword, we can use that to guide the
10296      production we choose.  */
10297   keyword = token->keyword;
10298   switch (keyword)
10299     {
10300     case RID_ENUM:
10301       /* Look for the enum-specifier.  */
10302       type_spec = cp_parser_enum_specifier (parser);
10303       /* If that worked, we're done.  */
10304       if (type_spec)
10305         {
10306           if (declares_class_or_enum)
10307             *declares_class_or_enum = 2;
10308           if (decl_specs)
10309             cp_parser_set_decl_spec_type (decl_specs,
10310                                           type_spec,
10311                                           /*user_defined_p=*/true);
10312           return type_spec;
10313         }
10314       else
10315         goto elaborated_type_specifier;
10316
10317       /* Any of these indicate either a class-specifier, or an
10318          elaborated-type-specifier.  */
10319     case RID_CLASS:
10320     case RID_STRUCT:
10321     case RID_UNION:
10322       /* Parse tentatively so that we can back up if we don't find a
10323          class-specifier.  */
10324       cp_parser_parse_tentatively (parser);
10325       /* Look for the class-specifier.  */
10326       type_spec = cp_parser_class_specifier (parser);
10327       /* If that worked, we're done.  */
10328       if (cp_parser_parse_definitely (parser))
10329         {
10330           if (declares_class_or_enum)
10331             *declares_class_or_enum = 2;
10332           if (decl_specs)
10333             cp_parser_set_decl_spec_type (decl_specs,
10334                                           type_spec,
10335                                           /*user_defined_p=*/true);
10336           return type_spec;
10337         }
10338
10339       /* Fall through.  */
10340     elaborated_type_specifier:
10341       /* We're declaring (not defining) a class or enum.  */
10342       if (declares_class_or_enum)
10343         *declares_class_or_enum = 1;
10344
10345       /* Fall through.  */
10346     case RID_TYPENAME:
10347       /* Look for an elaborated-type-specifier.  */
10348       type_spec
10349         = (cp_parser_elaborated_type_specifier
10350            (parser,
10351             decl_specs && decl_specs->specs[(int) ds_friend],
10352             is_declaration));
10353       if (decl_specs)
10354         cp_parser_set_decl_spec_type (decl_specs,
10355                                       type_spec,
10356                                       /*user_defined_p=*/true);
10357       return type_spec;
10358
10359     case RID_CONST:
10360       ds = ds_const;
10361       if (is_cv_qualifier)
10362         *is_cv_qualifier = true;
10363       break;
10364
10365     case RID_VOLATILE:
10366       ds = ds_volatile;
10367       if (is_cv_qualifier)
10368         *is_cv_qualifier = true;
10369       break;
10370
10371     case RID_RESTRICT:
10372       ds = ds_restrict;
10373       if (is_cv_qualifier)
10374         *is_cv_qualifier = true;
10375       break;
10376
10377     case RID_COMPLEX:
10378       /* The `__complex__' keyword is a GNU extension.  */
10379       ds = ds_complex;
10380       break;
10381
10382     default:
10383       break;
10384     }
10385
10386   /* Handle simple keywords.  */
10387   if (ds != ds_last)
10388     {
10389       if (decl_specs)
10390         {
10391           ++decl_specs->specs[(int)ds];
10392           decl_specs->any_specifiers_p = true;
10393         }
10394       return cp_lexer_consume_token (parser->lexer)->u.value;
10395     }
10396
10397   /* If we do not already have a type-specifier, assume we are looking
10398      at a simple-type-specifier.  */
10399   type_spec = cp_parser_simple_type_specifier (parser,
10400                                                decl_specs,
10401                                                flags);
10402
10403   /* If we didn't find a type-specifier, and a type-specifier was not
10404      optional in this context, issue an error message.  */
10405   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10406     {
10407       cp_parser_error (parser, "expected type specifier");
10408       return error_mark_node;
10409     }
10410
10411   return type_spec;
10412 }
10413
10414 /* Parse a simple-type-specifier.
10415
10416    simple-type-specifier:
10417      :: [opt] nested-name-specifier [opt] type-name
10418      :: [opt] nested-name-specifier template template-id
10419      char
10420      wchar_t
10421      bool
10422      short
10423      int
10424      long
10425      signed
10426      unsigned
10427      float
10428      double
10429      void
10430
10431    GNU Extension:
10432
10433    simple-type-specifier:
10434      __typeof__ unary-expression
10435      __typeof__ ( type-id )
10436
10437    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10438    appropriately updated.  */
10439
10440 static tree
10441 cp_parser_simple_type_specifier (cp_parser* parser,
10442                                  cp_decl_specifier_seq *decl_specs,
10443                                  cp_parser_flags flags)
10444 {
10445   tree type = NULL_TREE;
10446   cp_token *token;
10447
10448   /* Peek at the next token.  */
10449   token = cp_lexer_peek_token (parser->lexer);
10450
10451   /* If we're looking at a keyword, things are easy.  */
10452   switch (token->keyword)
10453     {
10454     case RID_CHAR:
10455       if (decl_specs)
10456         decl_specs->explicit_char_p = true;
10457       type = char_type_node;
10458       break;
10459     case RID_WCHAR:
10460       type = wchar_type_node;
10461       break;
10462     case RID_BOOL:
10463       type = boolean_type_node;
10464       break;
10465     case RID_SHORT:
10466       if (decl_specs)
10467         ++decl_specs->specs[(int) ds_short];
10468       type = short_integer_type_node;
10469       break;
10470     case RID_INT:
10471       if (decl_specs)
10472         decl_specs->explicit_int_p = true;
10473       type = integer_type_node;
10474       break;
10475     case RID_LONG:
10476       if (decl_specs)
10477         ++decl_specs->specs[(int) ds_long];
10478       type = long_integer_type_node;
10479       break;
10480     case RID_SIGNED:
10481       if (decl_specs)
10482         ++decl_specs->specs[(int) ds_signed];
10483       type = integer_type_node;
10484       break;
10485     case RID_UNSIGNED:
10486       if (decl_specs)
10487         ++decl_specs->specs[(int) ds_unsigned];
10488       type = unsigned_type_node;
10489       break;
10490     case RID_FLOAT:
10491       type = float_type_node;
10492       break;
10493     case RID_DOUBLE:
10494       type = double_type_node;
10495       break;
10496     case RID_VOID:
10497       type = void_type_node;
10498       break;
10499
10500     case RID_TYPEOF:
10501       /* Consume the `typeof' token.  */
10502       cp_lexer_consume_token (parser->lexer);
10503       /* Parse the operand to `typeof'.  */
10504       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10505       /* If it is not already a TYPE, take its type.  */
10506       if (!TYPE_P (type))
10507         type = finish_typeof (type);
10508
10509       if (decl_specs)
10510         cp_parser_set_decl_spec_type (decl_specs, type,
10511                                       /*user_defined_p=*/true);
10512
10513       return type;
10514
10515     default:
10516       break;
10517     }
10518
10519   /* If the type-specifier was for a built-in type, we're done.  */
10520   if (type)
10521     {
10522       tree id;
10523
10524       /* Record the type.  */
10525       if (decl_specs
10526           && (token->keyword != RID_SIGNED
10527               && token->keyword != RID_UNSIGNED
10528               && token->keyword != RID_SHORT
10529               && token->keyword != RID_LONG))
10530         cp_parser_set_decl_spec_type (decl_specs,
10531                                       type,
10532                                       /*user_defined=*/false);
10533       if (decl_specs)
10534         decl_specs->any_specifiers_p = true;
10535
10536       /* Consume the token.  */
10537       id = cp_lexer_consume_token (parser->lexer)->u.value;
10538
10539       /* There is no valid C++ program where a non-template type is
10540          followed by a "<".  That usually indicates that the user thought
10541          that the type was a template.  */
10542       cp_parser_check_for_invalid_template_id (parser, type);
10543
10544       return TYPE_NAME (type);
10545     }
10546
10547   /* The type-specifier must be a user-defined type.  */
10548   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10549     {
10550       bool qualified_p;
10551       bool global_p;
10552
10553       /* Don't gobble tokens or issue error messages if this is an
10554          optional type-specifier.  */
10555       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10556         cp_parser_parse_tentatively (parser);
10557
10558       /* Look for the optional `::' operator.  */
10559       global_p
10560         = (cp_parser_global_scope_opt (parser,
10561                                        /*current_scope_valid_p=*/false)
10562            != NULL_TREE);
10563       /* Look for the nested-name specifier.  */
10564       qualified_p
10565         = (cp_parser_nested_name_specifier_opt (parser,
10566                                                 /*typename_keyword_p=*/false,
10567                                                 /*check_dependency_p=*/true,
10568                                                 /*type_p=*/false,
10569                                                 /*is_declaration=*/false)
10570            != NULL_TREE);
10571       /* If we have seen a nested-name-specifier, and the next token
10572          is `template', then we are using the template-id production.  */
10573       if (parser->scope
10574           && cp_parser_optional_template_keyword (parser))
10575         {
10576           /* Look for the template-id.  */
10577           type = cp_parser_template_id (parser,
10578                                         /*template_keyword_p=*/true,
10579                                         /*check_dependency_p=*/true,
10580                                         /*is_declaration=*/false);
10581           /* If the template-id did not name a type, we are out of
10582              luck.  */
10583           if (TREE_CODE (type) != TYPE_DECL)
10584             {
10585               cp_parser_error (parser, "expected template-id for type");
10586               type = NULL_TREE;
10587             }
10588         }
10589       /* Otherwise, look for a type-name.  */
10590       else
10591         type = cp_parser_type_name (parser);
10592       /* Keep track of all name-lookups performed in class scopes.  */
10593       if (type
10594           && !global_p
10595           && !qualified_p
10596           && TREE_CODE (type) == TYPE_DECL
10597           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10598         maybe_note_name_used_in_class (DECL_NAME (type), type);
10599       /* If it didn't work out, we don't have a TYPE.  */
10600       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10601           && !cp_parser_parse_definitely (parser))
10602         type = NULL_TREE;
10603       if (type && decl_specs)
10604         cp_parser_set_decl_spec_type (decl_specs, type,
10605                                       /*user_defined=*/true);
10606     }
10607
10608   /* If we didn't get a type-name, issue an error message.  */
10609   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10610     {
10611       cp_parser_error (parser, "expected type-name");
10612       return error_mark_node;
10613     }
10614
10615   /* There is no valid C++ program where a non-template type is
10616      followed by a "<".  That usually indicates that the user thought
10617      that the type was a template.  */
10618   if (type && type != error_mark_node)
10619     {
10620       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10621          If it is, then the '<'...'>' enclose protocol names rather than
10622          template arguments, and so everything is fine.  */
10623       if (c_dialect_objc ()
10624           && (objc_is_id (type) || objc_is_class_name (type)))
10625         {
10626           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10627           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10628
10629           /* Clobber the "unqualified" type previously entered into
10630              DECL_SPECS with the new, improved protocol-qualified version.  */
10631           if (decl_specs)
10632             decl_specs->type = qual_type;
10633
10634           return qual_type;
10635         }
10636
10637       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10638     }
10639
10640   return type;
10641 }
10642
10643 /* Parse a type-name.
10644
10645    type-name:
10646      class-name
10647      enum-name
10648      typedef-name
10649
10650    enum-name:
10651      identifier
10652
10653    typedef-name:
10654      identifier
10655
10656    Returns a TYPE_DECL for the type.  */
10657
10658 static tree
10659 cp_parser_type_name (cp_parser* parser)
10660 {
10661   tree type_decl;
10662   tree identifier;
10663
10664   /* We can't know yet whether it is a class-name or not.  */
10665   cp_parser_parse_tentatively (parser);
10666   /* Try a class-name.  */
10667   type_decl = cp_parser_class_name (parser,
10668                                     /*typename_keyword_p=*/false,
10669                                     /*template_keyword_p=*/false,
10670                                     none_type,
10671                                     /*check_dependency_p=*/true,
10672                                     /*class_head_p=*/false,
10673                                     /*is_declaration=*/false);
10674   /* If it's not a class-name, keep looking.  */
10675   if (!cp_parser_parse_definitely (parser))
10676     {
10677       /* It must be a typedef-name or an enum-name.  */
10678       identifier = cp_parser_identifier (parser);
10679       if (identifier == error_mark_node)
10680         return error_mark_node;
10681
10682       /* Look up the type-name.  */
10683       type_decl = cp_parser_lookup_name_simple (parser, identifier);
10684
10685       if (TREE_CODE (type_decl) != TYPE_DECL
10686           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10687         {
10688           /* See if this is an Objective-C type.  */
10689           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10690           tree type = objc_get_protocol_qualified_type (identifier, protos);
10691           if (type)
10692             type_decl = TYPE_NAME (type);
10693         }
10694
10695       /* Issue an error if we did not find a type-name.  */
10696       if (TREE_CODE (type_decl) != TYPE_DECL)
10697         {
10698           if (!cp_parser_simulate_error (parser))
10699             cp_parser_name_lookup_error (parser, identifier, type_decl,
10700                                          "is not a type");
10701           type_decl = error_mark_node;
10702         }
10703       /* Remember that the name was used in the definition of the
10704          current class so that we can check later to see if the
10705          meaning would have been different after the class was
10706          entirely defined.  */
10707       else if (type_decl != error_mark_node
10708                && !parser->scope)
10709         maybe_note_name_used_in_class (identifier, type_decl);
10710     }
10711
10712   return type_decl;
10713 }
10714
10715
10716 /* Parse an elaborated-type-specifier.  Note that the grammar given
10717    here incorporates the resolution to DR68.
10718
10719    elaborated-type-specifier:
10720      class-key :: [opt] nested-name-specifier [opt] identifier
10721      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10722      enum :: [opt] nested-name-specifier [opt] identifier
10723      typename :: [opt] nested-name-specifier identifier
10724      typename :: [opt] nested-name-specifier template [opt]
10725        template-id
10726
10727    GNU extension:
10728
10729    elaborated-type-specifier:
10730      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10731      class-key attributes :: [opt] nested-name-specifier [opt]
10732                template [opt] template-id
10733      enum attributes :: [opt] nested-name-specifier [opt] identifier
10734
10735    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10736    declared `friend'.  If IS_DECLARATION is TRUE, then this
10737    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10738    something is being declared.
10739
10740    Returns the TYPE specified.  */
10741
10742 static tree
10743 cp_parser_elaborated_type_specifier (cp_parser* parser,
10744                                      bool is_friend,
10745                                      bool is_declaration)
10746 {
10747   enum tag_types tag_type;
10748   tree identifier;
10749   tree type = NULL_TREE;
10750   tree attributes = NULL_TREE;
10751
10752   /* See if we're looking at the `enum' keyword.  */
10753   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10754     {
10755       /* Consume the `enum' token.  */
10756       cp_lexer_consume_token (parser->lexer);
10757       /* Remember that it's an enumeration type.  */
10758       tag_type = enum_type;
10759       /* Parse the attributes.  */
10760       attributes = cp_parser_attributes_opt (parser);
10761     }
10762   /* Or, it might be `typename'.  */
10763   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10764                                            RID_TYPENAME))
10765     {
10766       /* Consume the `typename' token.  */
10767       cp_lexer_consume_token (parser->lexer);
10768       /* Remember that it's a `typename' type.  */
10769       tag_type = typename_type;
10770       /* The `typename' keyword is only allowed in templates.  */
10771       if (!processing_template_decl)
10772         pedwarn ("using %<typename%> outside of template");
10773     }
10774   /* Otherwise it must be a class-key.  */
10775   else
10776     {
10777       tag_type = cp_parser_class_key (parser);
10778       if (tag_type == none_type)
10779         return error_mark_node;
10780       /* Parse the attributes.  */
10781       attributes = cp_parser_attributes_opt (parser);
10782     }
10783
10784   /* Look for the `::' operator.  */
10785   cp_parser_global_scope_opt (parser,
10786                               /*current_scope_valid_p=*/false);
10787   /* Look for the nested-name-specifier.  */
10788   if (tag_type == typename_type)
10789     {
10790       if (!cp_parser_nested_name_specifier (parser,
10791                                            /*typename_keyword_p=*/true,
10792                                            /*check_dependency_p=*/true,
10793                                            /*type_p=*/true,
10794                                             is_declaration))
10795         return error_mark_node;
10796     }
10797   else
10798     /* Even though `typename' is not present, the proposed resolution
10799        to Core Issue 180 says that in `class A<T>::B', `B' should be
10800        considered a type-name, even if `A<T>' is dependent.  */
10801     cp_parser_nested_name_specifier_opt (parser,
10802                                          /*typename_keyword_p=*/true,
10803                                          /*check_dependency_p=*/true,
10804                                          /*type_p=*/true,
10805                                          is_declaration);
10806  /* For everything but enumeration types, consider a template-id.
10807     For an enumeration type, consider only a plain identifier.  */
10808   if (tag_type != enum_type)
10809     {
10810       bool template_p = false;
10811       tree decl;
10812
10813       /* Allow the `template' keyword.  */
10814       template_p = cp_parser_optional_template_keyword (parser);
10815       /* If we didn't see `template', we don't know if there's a
10816          template-id or not.  */
10817       if (!template_p)
10818         cp_parser_parse_tentatively (parser);
10819       /* Parse the template-id.  */
10820       decl = cp_parser_template_id (parser, template_p,
10821                                     /*check_dependency_p=*/true,
10822                                     is_declaration);
10823       /* If we didn't find a template-id, look for an ordinary
10824          identifier.  */
10825       if (!template_p && !cp_parser_parse_definitely (parser))
10826         ;
10827       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10828          in effect, then we must assume that, upon instantiation, the
10829          template will correspond to a class.  */
10830       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10831                && tag_type == typename_type)
10832         type = make_typename_type (parser->scope, decl,
10833                                    typename_type,
10834                                    /*complain=*/tf_error);
10835       else
10836         type = TREE_TYPE (decl);
10837     }
10838
10839   if (!type)
10840     {
10841       identifier = cp_parser_identifier (parser);
10842
10843       if (identifier == error_mark_node)
10844         {
10845           parser->scope = NULL_TREE;
10846           return error_mark_node;
10847         }
10848
10849       /* For a `typename', we needn't call xref_tag.  */
10850       if (tag_type == typename_type
10851           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10852         return cp_parser_make_typename_type (parser, parser->scope,
10853                                              identifier);
10854       /* Look up a qualified name in the usual way.  */
10855       if (parser->scope)
10856         {
10857           tree decl;
10858           tree ambiguous_decls;
10859
10860           decl = cp_parser_lookup_name (parser, identifier,
10861                                         tag_type,
10862                                         /*is_template=*/false,
10863                                         /*is_namespace=*/false,
10864                                         /*check_dependency=*/true,
10865                                         &ambiguous_decls);
10866
10867           /* If the lookup was ambiguous, an error will already have been
10868              issued.  */
10869           if (ambiguous_decls)
10870             return error_mark_node;
10871
10872           /* If we are parsing friend declaration, DECL may be a
10873              TEMPLATE_DECL tree node here.  However, we need to check
10874              whether this TEMPLATE_DECL results in valid code.  Consider
10875              the following example:
10876
10877                namespace N {
10878                  template <class T> class C {};
10879                }
10880                class X {
10881                  template <class T> friend class N::C; // #1, valid code
10882                };
10883                template <class T> class Y {
10884                  friend class N::C;                    // #2, invalid code
10885                };
10886
10887              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10888              name lookup of `N::C'.  We see that friend declaration must
10889              be template for the code to be valid.  Note that
10890              processing_template_decl does not work here since it is
10891              always 1 for the above two cases.  */
10892
10893           decl = (cp_parser_maybe_treat_template_as_class
10894                   (decl, /*tag_name_p=*/is_friend
10895                          && parser->num_template_parameter_lists));
10896
10897           if (TREE_CODE (decl) != TYPE_DECL)
10898             {
10899               cp_parser_diagnose_invalid_type_name (parser,
10900                                                     parser->scope,
10901                                                     identifier);
10902               return error_mark_node;
10903             }
10904
10905           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10906             {
10907               bool allow_template = (parser->num_template_parameter_lists
10908                                       || DECL_SELF_REFERENCE_P (decl));
10909               type = check_elaborated_type_specifier (tag_type, decl, 
10910                                                       allow_template);
10911
10912               if (type == error_mark_node)
10913                 return error_mark_node;
10914             }
10915
10916           type = TREE_TYPE (decl);
10917         }
10918       else
10919         {
10920           /* An elaborated-type-specifier sometimes introduces a new type and
10921              sometimes names an existing type.  Normally, the rule is that it
10922              introduces a new type only if there is not an existing type of
10923              the same name already in scope.  For example, given:
10924
10925                struct S {};
10926                void f() { struct S s; }
10927
10928              the `struct S' in the body of `f' is the same `struct S' as in
10929              the global scope; the existing definition is used.  However, if
10930              there were no global declaration, this would introduce a new
10931              local class named `S'.
10932
10933              An exception to this rule applies to the following code:
10934
10935                namespace N { struct S; }
10936
10937              Here, the elaborated-type-specifier names a new type
10938              unconditionally; even if there is already an `S' in the
10939              containing scope this declaration names a new type.
10940              This exception only applies if the elaborated-type-specifier
10941              forms the complete declaration:
10942
10943                [class.name]
10944
10945                A declaration consisting solely of `class-key identifier ;' is
10946                either a redeclaration of the name in the current scope or a
10947                forward declaration of the identifier as a class name.  It
10948                introduces the name into the current scope.
10949
10950              We are in this situation precisely when the next token is a `;'.
10951
10952              An exception to the exception is that a `friend' declaration does
10953              *not* name a new type; i.e., given:
10954
10955                struct S { friend struct T; };
10956
10957              `T' is not a new type in the scope of `S'.
10958
10959              Also, `new struct S' or `sizeof (struct S)' never results in the
10960              definition of a new type; a new type can only be declared in a
10961              declaration context.  */
10962
10963           tag_scope ts;
10964           bool template_p;
10965
10966           if (is_friend)
10967             /* Friends have special name lookup rules.  */
10968             ts = ts_within_enclosing_non_class;
10969           else if (is_declaration
10970                    && cp_lexer_next_token_is (parser->lexer,
10971                                               CPP_SEMICOLON))
10972             /* This is a `class-key identifier ;' */
10973             ts = ts_current;
10974           else
10975             ts = ts_global;
10976
10977           template_p =
10978             (parser->num_template_parameter_lists
10979              && (cp_parser_next_token_starts_class_definition_p (parser)
10980                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10981           /* An unqualified name was used to reference this type, so
10982              there were no qualifying templates.  */
10983           if (!cp_parser_check_template_parameters (parser,
10984                                                     /*num_templates=*/0))
10985             return error_mark_node;
10986           type = xref_tag (tag_type, identifier, ts, template_p);
10987         }
10988     }
10989
10990   if (type == error_mark_node)
10991     return error_mark_node;
10992
10993   /* Allow attributes on forward declarations of classes.  */
10994   if (attributes)
10995     {
10996       if (TREE_CODE (type) == TYPENAME_TYPE)
10997         warning (OPT_Wattributes,
10998                  "attributes ignored on uninstantiated type");
10999       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11000                && ! processing_explicit_instantiation)
11001         warning (OPT_Wattributes,
11002                  "attributes ignored on template instantiation");
11003       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11004         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11005       else
11006         warning (OPT_Wattributes,
11007                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11008     }
11009
11010   if (tag_type != enum_type)
11011     cp_parser_check_class_key (tag_type, type);
11012
11013   /* A "<" cannot follow an elaborated type specifier.  If that
11014      happens, the user was probably trying to form a template-id.  */
11015   cp_parser_check_for_invalid_template_id (parser, type);
11016
11017   return type;
11018 }
11019
11020 /* Parse an enum-specifier.
11021
11022    enum-specifier:
11023      enum identifier [opt] { enumerator-list [opt] }
11024
11025    GNU Extensions:
11026      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11027        attributes[opt]
11028
11029    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11030    if the token stream isn't an enum-specifier after all.  */
11031
11032 static tree
11033 cp_parser_enum_specifier (cp_parser* parser)
11034 {
11035   tree identifier;
11036   tree type;
11037   tree attributes;
11038
11039   /* Parse tentatively so that we can back up if we don't find a
11040      enum-specifier.  */
11041   cp_parser_parse_tentatively (parser);
11042
11043   /* Caller guarantees that the current token is 'enum', an identifier
11044      possibly follows, and the token after that is an opening brace.
11045      If we don't have an identifier, fabricate an anonymous name for
11046      the enumeration being defined.  */
11047   cp_lexer_consume_token (parser->lexer);
11048
11049   attributes = cp_parser_attributes_opt (parser);
11050
11051   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11052     identifier = cp_parser_identifier (parser);
11053   else
11054     identifier = make_anon_name ();
11055
11056   /* Look for the `{' but don't consume it yet.  */
11057   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11058     cp_parser_simulate_error (parser);
11059
11060   if (!cp_parser_parse_definitely (parser))
11061     return NULL_TREE;
11062
11063   /* Issue an error message if type-definitions are forbidden here.  */
11064   if (!cp_parser_check_type_definition (parser))
11065     type = error_mark_node;
11066   else
11067     /* Create the new type.  We do this before consuming the opening
11068        brace so the enum will be recorded as being on the line of its
11069        tag (or the 'enum' keyword, if there is no tag).  */
11070     type = start_enum (identifier);
11071   
11072   /* Consume the opening brace.  */
11073   cp_lexer_consume_token (parser->lexer);
11074
11075   if (type == error_mark_node)
11076     {
11077       cp_parser_skip_to_end_of_block_or_statement (parser);
11078       return error_mark_node;
11079     }
11080
11081   /* If the next token is not '}', then there are some enumerators.  */
11082   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11083     cp_parser_enumerator_list (parser, type);
11084
11085   /* Consume the final '}'.  */
11086   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11087
11088   /* Look for trailing attributes to apply to this enumeration, and
11089      apply them if appropriate.  */
11090   if (cp_parser_allow_gnu_extensions_p (parser))
11091     {
11092       tree trailing_attr = cp_parser_attributes_opt (parser);
11093       cplus_decl_attributes (&type,
11094                              trailing_attr,
11095                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11096     }
11097
11098   /* Finish up the enumeration.  */
11099   finish_enum (type);
11100
11101   return type;
11102 }
11103
11104 /* Parse an enumerator-list.  The enumerators all have the indicated
11105    TYPE.
11106
11107    enumerator-list:
11108      enumerator-definition
11109      enumerator-list , enumerator-definition  */
11110
11111 static void
11112 cp_parser_enumerator_list (cp_parser* parser, tree type)
11113 {
11114   while (true)
11115     {
11116       /* Parse an enumerator-definition.  */
11117       cp_parser_enumerator_definition (parser, type);
11118
11119       /* If the next token is not a ',', we've reached the end of
11120          the list.  */
11121       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11122         break;
11123       /* Otherwise, consume the `,' and keep going.  */
11124       cp_lexer_consume_token (parser->lexer);
11125       /* If the next token is a `}', there is a trailing comma.  */
11126       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11127         {
11128           if (pedantic && !in_system_header)
11129             pedwarn ("comma at end of enumerator list");
11130           break;
11131         }
11132     }
11133 }
11134
11135 /* Parse an enumerator-definition.  The enumerator has the indicated
11136    TYPE.
11137
11138    enumerator-definition:
11139      enumerator
11140      enumerator = constant-expression
11141
11142    enumerator:
11143      identifier  */
11144
11145 static void
11146 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11147 {
11148   tree identifier;
11149   tree value;
11150
11151   /* Look for the identifier.  */
11152   identifier = cp_parser_identifier (parser);
11153   if (identifier == error_mark_node)
11154     return;
11155
11156   /* If the next token is an '=', then there is an explicit value.  */
11157   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11158     {
11159       /* Consume the `=' token.  */
11160       cp_lexer_consume_token (parser->lexer);
11161       /* Parse the value.  */
11162       value = cp_parser_constant_expression (parser,
11163                                              /*allow_non_constant_p=*/false,
11164                                              NULL);
11165     }
11166   else
11167     value = NULL_TREE;
11168
11169   /* Create the enumerator.  */
11170   build_enumerator (identifier, value, type);
11171 }
11172
11173 /* Parse a namespace-name.
11174
11175    namespace-name:
11176      original-namespace-name
11177      namespace-alias
11178
11179    Returns the NAMESPACE_DECL for the namespace.  */
11180
11181 static tree
11182 cp_parser_namespace_name (cp_parser* parser)
11183 {
11184   tree identifier;
11185   tree namespace_decl;
11186
11187   /* Get the name of the namespace.  */
11188   identifier = cp_parser_identifier (parser);
11189   if (identifier == error_mark_node)
11190     return error_mark_node;
11191
11192   /* Look up the identifier in the currently active scope.  Look only
11193      for namespaces, due to:
11194
11195        [basic.lookup.udir]
11196
11197        When looking up a namespace-name in a using-directive or alias
11198        definition, only namespace names are considered.
11199
11200      And:
11201
11202        [basic.lookup.qual]
11203
11204        During the lookup of a name preceding the :: scope resolution
11205        operator, object, function, and enumerator names are ignored.
11206
11207      (Note that cp_parser_class_or_namespace_name only calls this
11208      function if the token after the name is the scope resolution
11209      operator.)  */
11210   namespace_decl = cp_parser_lookup_name (parser, identifier,
11211                                           none_type,
11212                                           /*is_template=*/false,
11213                                           /*is_namespace=*/true,
11214                                           /*check_dependency=*/true,
11215                                           /*ambiguous_decls=*/NULL);
11216   /* If it's not a namespace, issue an error.  */
11217   if (namespace_decl == error_mark_node
11218       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11219     {
11220       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11221         error ("%qD is not a namespace-name", identifier);
11222       cp_parser_error (parser, "expected namespace-name");
11223       namespace_decl = error_mark_node;
11224     }
11225
11226   return namespace_decl;
11227 }
11228
11229 /* Parse a namespace-definition.
11230
11231    namespace-definition:
11232      named-namespace-definition
11233      unnamed-namespace-definition
11234
11235    named-namespace-definition:
11236      original-namespace-definition
11237      extension-namespace-definition
11238
11239    original-namespace-definition:
11240      namespace identifier { namespace-body }
11241
11242    extension-namespace-definition:
11243      namespace original-namespace-name { namespace-body }
11244
11245    unnamed-namespace-definition:
11246      namespace { namespace-body } */
11247
11248 static void
11249 cp_parser_namespace_definition (cp_parser* parser)
11250 {
11251   tree identifier, attribs;
11252
11253   /* Look for the `namespace' keyword.  */
11254   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11255
11256   /* Get the name of the namespace.  We do not attempt to distinguish
11257      between an original-namespace-definition and an
11258      extension-namespace-definition at this point.  The semantic
11259      analysis routines are responsible for that.  */
11260   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11261     identifier = cp_parser_identifier (parser);
11262   else
11263     identifier = NULL_TREE;
11264
11265   /* Parse any specified attributes.  */
11266   attribs = cp_parser_attributes_opt (parser);
11267
11268   /* Look for the `{' to start the namespace.  */
11269   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11270   /* Start the namespace.  */
11271   push_namespace_with_attribs (identifier, attribs);
11272   /* Parse the body of the namespace.  */
11273   cp_parser_namespace_body (parser);
11274   /* Finish the namespace.  */
11275   pop_namespace ();
11276   /* Look for the final `}'.  */
11277   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11278 }
11279
11280 /* Parse a namespace-body.
11281
11282    namespace-body:
11283      declaration-seq [opt]  */
11284
11285 static void
11286 cp_parser_namespace_body (cp_parser* parser)
11287 {
11288   cp_parser_declaration_seq_opt (parser);
11289 }
11290
11291 /* Parse a namespace-alias-definition.
11292
11293    namespace-alias-definition:
11294      namespace identifier = qualified-namespace-specifier ;  */
11295
11296 static void
11297 cp_parser_namespace_alias_definition (cp_parser* parser)
11298 {
11299   tree identifier;
11300   tree namespace_specifier;
11301
11302   /* Look for the `namespace' keyword.  */
11303   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11304   /* Look for the identifier.  */
11305   identifier = cp_parser_identifier (parser);
11306   if (identifier == error_mark_node)
11307     return;
11308   /* Look for the `=' token.  */
11309   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11310       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11311     {
11312       error ("%<namespace%> definition is not allowed here");
11313       /* Skip the definition.  */
11314       cp_lexer_consume_token (parser->lexer);
11315       if (cp_parser_skip_to_closing_brace (parser))
11316         cp_lexer_consume_token (parser->lexer);
11317       return;
11318     }
11319   cp_parser_require (parser, CPP_EQ, "`='");
11320   /* Look for the qualified-namespace-specifier.  */
11321   namespace_specifier
11322     = cp_parser_qualified_namespace_specifier (parser);
11323   /* Look for the `;' token.  */
11324   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11325
11326   /* Register the alias in the symbol table.  */
11327   do_namespace_alias (identifier, namespace_specifier);
11328 }
11329
11330 /* Parse a qualified-namespace-specifier.
11331
11332    qualified-namespace-specifier:
11333      :: [opt] nested-name-specifier [opt] namespace-name
11334
11335    Returns a NAMESPACE_DECL corresponding to the specified
11336    namespace.  */
11337
11338 static tree
11339 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11340 {
11341   /* Look for the optional `::'.  */
11342   cp_parser_global_scope_opt (parser,
11343                               /*current_scope_valid_p=*/false);
11344
11345   /* Look for the optional nested-name-specifier.  */
11346   cp_parser_nested_name_specifier_opt (parser,
11347                                        /*typename_keyword_p=*/false,
11348                                        /*check_dependency_p=*/true,
11349                                        /*type_p=*/false,
11350                                        /*is_declaration=*/true);
11351
11352   return cp_parser_namespace_name (parser);
11353 }
11354
11355 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11356    access declaration.
11357
11358    using-declaration:
11359      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11360      using :: unqualified-id ;  
11361
11362    access-declaration:
11363      qualified-id ;  
11364
11365    */
11366
11367 static bool
11368 cp_parser_using_declaration (cp_parser* parser, 
11369                              bool access_declaration_p)
11370 {
11371   cp_token *token;
11372   bool typename_p = false;
11373   bool global_scope_p;
11374   tree decl;
11375   tree identifier;
11376   tree qscope;
11377
11378   if (access_declaration_p)
11379     cp_parser_parse_tentatively (parser);
11380   else
11381     {
11382       /* Look for the `using' keyword.  */
11383       cp_parser_require_keyword (parser, RID_USING, "`using'");
11384       
11385       /* Peek at the next token.  */
11386       token = cp_lexer_peek_token (parser->lexer);
11387       /* See if it's `typename'.  */
11388       if (token->keyword == RID_TYPENAME)
11389         {
11390           /* Remember that we've seen it.  */
11391           typename_p = true;
11392           /* Consume the `typename' token.  */
11393           cp_lexer_consume_token (parser->lexer);
11394         }
11395     }
11396
11397   /* Look for the optional global scope qualification.  */
11398   global_scope_p
11399     = (cp_parser_global_scope_opt (parser,
11400                                    /*current_scope_valid_p=*/false)
11401        != NULL_TREE);
11402
11403   /* If we saw `typename', or didn't see `::', then there must be a
11404      nested-name-specifier present.  */
11405   if (typename_p || !global_scope_p)
11406     qscope = cp_parser_nested_name_specifier (parser, typename_p,
11407                                               /*check_dependency_p=*/true,
11408                                               /*type_p=*/false,
11409                                               /*is_declaration=*/true);
11410   /* Otherwise, we could be in either of the two productions.  In that
11411      case, treat the nested-name-specifier as optional.  */
11412   else
11413     qscope = cp_parser_nested_name_specifier_opt (parser,
11414                                                   /*typename_keyword_p=*/false,
11415                                                   /*check_dependency_p=*/true,
11416                                                   /*type_p=*/false,
11417                                                   /*is_declaration=*/true);
11418   if (!qscope)
11419     qscope = global_namespace;
11420
11421   if (access_declaration_p && cp_parser_error_occurred (parser))
11422     /* Something has already gone wrong; there's no need to parse
11423        further.  Since an error has occurred, the return value of
11424        cp_parser_parse_definitely will be false, as required.  */
11425     return cp_parser_parse_definitely (parser);
11426
11427   /* Parse the unqualified-id.  */
11428   identifier = cp_parser_unqualified_id (parser,
11429                                          /*template_keyword_p=*/false,
11430                                          /*check_dependency_p=*/true,
11431                                          /*declarator_p=*/true,
11432                                          /*optional_p=*/false);
11433
11434   if (access_declaration_p)
11435     {
11436       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11437         cp_parser_simulate_error (parser);
11438       if (!cp_parser_parse_definitely (parser))
11439         return false;
11440     }
11441
11442   /* The function we call to handle a using-declaration is different
11443      depending on what scope we are in.  */
11444   if (qscope == error_mark_node || identifier == error_mark_node)
11445     ;
11446   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11447            && TREE_CODE (identifier) != BIT_NOT_EXPR)
11448     /* [namespace.udecl]
11449
11450        A using declaration shall not name a template-id.  */
11451     error ("a template-id may not appear in a using-declaration");
11452   else
11453     {
11454       if (at_class_scope_p ())
11455         {
11456           /* Create the USING_DECL.  */
11457           decl = do_class_using_decl (parser->scope, identifier);
11458           /* Add it to the list of members in this class.  */
11459           finish_member_declaration (decl);
11460         }
11461       else
11462         {
11463           decl = cp_parser_lookup_name_simple (parser, identifier);
11464           if (decl == error_mark_node)
11465             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11466           else if (!at_namespace_scope_p ())
11467             do_local_using_decl (decl, qscope, identifier);
11468           else
11469             do_toplevel_using_decl (decl, qscope, identifier);
11470         }
11471     }
11472
11473   /* Look for the final `;'.  */
11474   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11475   
11476   return true;
11477 }
11478
11479 /* Parse a using-directive.
11480
11481    using-directive:
11482      using namespace :: [opt] nested-name-specifier [opt]
11483        namespace-name ;  */
11484
11485 static void
11486 cp_parser_using_directive (cp_parser* parser)
11487 {
11488   tree namespace_decl;
11489   tree attribs;
11490
11491   /* Look for the `using' keyword.  */
11492   cp_parser_require_keyword (parser, RID_USING, "`using'");
11493   /* And the `namespace' keyword.  */
11494   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11495   /* Look for the optional `::' operator.  */
11496   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11497   /* And the optional nested-name-specifier.  */
11498   cp_parser_nested_name_specifier_opt (parser,
11499                                        /*typename_keyword_p=*/false,
11500                                        /*check_dependency_p=*/true,
11501                                        /*type_p=*/false,
11502                                        /*is_declaration=*/true);
11503   /* Get the namespace being used.  */
11504   namespace_decl = cp_parser_namespace_name (parser);
11505   /* And any specified attributes.  */
11506   attribs = cp_parser_attributes_opt (parser);
11507   /* Update the symbol table.  */
11508   parse_using_directive (namespace_decl, attribs);
11509   /* Look for the final `;'.  */
11510   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11511 }
11512
11513 /* Parse an asm-definition.
11514
11515    asm-definition:
11516      asm ( string-literal ) ;
11517
11518    GNU Extension:
11519
11520    asm-definition:
11521      asm volatile [opt] ( string-literal ) ;
11522      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11523      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11524                           : asm-operand-list [opt] ) ;
11525      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11526                           : asm-operand-list [opt]
11527                           : asm-operand-list [opt] ) ;  */
11528
11529 static void
11530 cp_parser_asm_definition (cp_parser* parser)
11531 {
11532   tree string;
11533   tree outputs = NULL_TREE;
11534   tree inputs = NULL_TREE;
11535   tree clobbers = NULL_TREE;
11536   tree asm_stmt;
11537   bool volatile_p = false;
11538   bool extended_p = false;
11539
11540   /* Look for the `asm' keyword.  */
11541   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11542   /* See if the next token is `volatile'.  */
11543   if (cp_parser_allow_gnu_extensions_p (parser)
11544       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11545     {
11546       /* Remember that we saw the `volatile' keyword.  */
11547       volatile_p = true;
11548       /* Consume the token.  */
11549       cp_lexer_consume_token (parser->lexer);
11550     }
11551   /* Look for the opening `('.  */
11552   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11553     return;
11554   /* Look for the string.  */
11555   string = cp_parser_string_literal (parser, false, false);
11556   if (string == error_mark_node)
11557     {
11558       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11559                                              /*consume_paren=*/true);
11560       return;
11561     }
11562
11563   /* If we're allowing GNU extensions, check for the extended assembly
11564      syntax.  Unfortunately, the `:' tokens need not be separated by
11565      a space in C, and so, for compatibility, we tolerate that here
11566      too.  Doing that means that we have to treat the `::' operator as
11567      two `:' tokens.  */
11568   if (cp_parser_allow_gnu_extensions_p (parser)
11569       && parser->in_function_body
11570       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11571           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11572     {
11573       bool inputs_p = false;
11574       bool clobbers_p = false;
11575
11576       /* The extended syntax was used.  */
11577       extended_p = true;
11578
11579       /* Look for outputs.  */
11580       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11581         {
11582           /* Consume the `:'.  */
11583           cp_lexer_consume_token (parser->lexer);
11584           /* Parse the output-operands.  */
11585           if (cp_lexer_next_token_is_not (parser->lexer,
11586                                           CPP_COLON)
11587               && cp_lexer_next_token_is_not (parser->lexer,
11588                                              CPP_SCOPE)
11589               && cp_lexer_next_token_is_not (parser->lexer,
11590                                              CPP_CLOSE_PAREN))
11591             outputs = cp_parser_asm_operand_list (parser);
11592         }
11593       /* If the next token is `::', there are no outputs, and the
11594          next token is the beginning of the inputs.  */
11595       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11596         /* The inputs are coming next.  */
11597         inputs_p = true;
11598
11599       /* Look for inputs.  */
11600       if (inputs_p
11601           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11602         {
11603           /* Consume the `:' or `::'.  */
11604           cp_lexer_consume_token (parser->lexer);
11605           /* Parse the output-operands.  */
11606           if (cp_lexer_next_token_is_not (parser->lexer,
11607                                           CPP_COLON)
11608               && cp_lexer_next_token_is_not (parser->lexer,
11609                                              CPP_CLOSE_PAREN))
11610             inputs = cp_parser_asm_operand_list (parser);
11611         }
11612       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11613         /* The clobbers are coming next.  */
11614         clobbers_p = true;
11615
11616       /* Look for clobbers.  */
11617       if (clobbers_p
11618           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11619         {
11620           /* Consume the `:' or `::'.  */
11621           cp_lexer_consume_token (parser->lexer);
11622           /* Parse the clobbers.  */
11623           if (cp_lexer_next_token_is_not (parser->lexer,
11624                                           CPP_CLOSE_PAREN))
11625             clobbers = cp_parser_asm_clobber_list (parser);
11626         }
11627     }
11628   /* Look for the closing `)'.  */
11629   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11630     cp_parser_skip_to_closing_parenthesis (parser, true, false,
11631                                            /*consume_paren=*/true);
11632   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11633
11634   /* Create the ASM_EXPR.  */
11635   if (parser->in_function_body)
11636     {
11637       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11638                                   inputs, clobbers);
11639       /* If the extended syntax was not used, mark the ASM_EXPR.  */
11640       if (!extended_p)
11641         {
11642           tree temp = asm_stmt;
11643           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11644             temp = TREE_OPERAND (temp, 0);
11645
11646           ASM_INPUT_P (temp) = 1;
11647         }
11648     }
11649   else
11650     cgraph_add_asm_node (string);
11651 }
11652
11653 /* Declarators [gram.dcl.decl] */
11654
11655 /* Parse an init-declarator.
11656
11657    init-declarator:
11658      declarator initializer [opt]
11659
11660    GNU Extension:
11661
11662    init-declarator:
11663      declarator asm-specification [opt] attributes [opt] initializer [opt]
11664
11665    function-definition:
11666      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11667        function-body
11668      decl-specifier-seq [opt] declarator function-try-block
11669
11670    GNU Extension:
11671
11672    function-definition:
11673      __extension__ function-definition
11674
11675    The DECL_SPECIFIERS apply to this declarator.  Returns a
11676    representation of the entity declared.  If MEMBER_P is TRUE, then
11677    this declarator appears in a class scope.  The new DECL created by
11678    this declarator is returned.
11679
11680    The CHECKS are access checks that should be performed once we know
11681    what entity is being declared (and, therefore, what classes have
11682    befriended it).
11683
11684    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11685    for a function-definition here as well.  If the declarator is a
11686    declarator for a function-definition, *FUNCTION_DEFINITION_P will
11687    be TRUE upon return.  By that point, the function-definition will
11688    have been completely parsed.
11689
11690    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11691    is FALSE.  */
11692
11693 static tree
11694 cp_parser_init_declarator (cp_parser* parser,
11695                            cp_decl_specifier_seq *decl_specifiers,
11696                            VEC (deferred_access_check,gc)* checks,
11697                            bool function_definition_allowed_p,
11698                            bool member_p,
11699                            int declares_class_or_enum,
11700                            bool* function_definition_p)
11701 {
11702   cp_token *token;
11703   cp_declarator *declarator;
11704   tree prefix_attributes;
11705   tree attributes;
11706   tree asm_specification;
11707   tree initializer;
11708   tree decl = NULL_TREE;
11709   tree scope;
11710   bool is_initialized;
11711   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
11712      initialized with "= ..", CPP_OPEN_PAREN if initialized with
11713      "(...)".  */
11714   enum cpp_ttype initialization_kind;
11715   bool is_parenthesized_init = false;
11716   bool is_non_constant_init;
11717   int ctor_dtor_or_conv_p;
11718   bool friend_p;
11719   tree pushed_scope = NULL;
11720
11721   /* Gather the attributes that were provided with the
11722      decl-specifiers.  */
11723   prefix_attributes = decl_specifiers->attributes;
11724
11725   /* Assume that this is not the declarator for a function
11726      definition.  */
11727   if (function_definition_p)
11728     *function_definition_p = false;
11729
11730   /* Defer access checks while parsing the declarator; we cannot know
11731      what names are accessible until we know what is being
11732      declared.  */
11733   resume_deferring_access_checks ();
11734
11735   /* Parse the declarator.  */
11736   declarator
11737     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11738                             &ctor_dtor_or_conv_p,
11739                             /*parenthesized_p=*/NULL,
11740                             /*member_p=*/false);
11741   /* Gather up the deferred checks.  */
11742   stop_deferring_access_checks ();
11743
11744   /* If the DECLARATOR was erroneous, there's no need to go
11745      further.  */
11746   if (declarator == cp_error_declarator)
11747     return error_mark_node;
11748
11749   /* Check that the number of template-parameter-lists is OK.  */
11750   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11751     return error_mark_node;
11752
11753   if (declares_class_or_enum & 2)
11754     cp_parser_check_for_definition_in_return_type (declarator,
11755                                                    decl_specifiers->type);
11756
11757   /* Figure out what scope the entity declared by the DECLARATOR is
11758      located in.  `grokdeclarator' sometimes changes the scope, so
11759      we compute it now.  */
11760   scope = get_scope_of_declarator (declarator);
11761
11762   /* If we're allowing GNU extensions, look for an asm-specification
11763      and attributes.  */
11764   if (cp_parser_allow_gnu_extensions_p (parser))
11765     {
11766       /* Look for an asm-specification.  */
11767       asm_specification = cp_parser_asm_specification_opt (parser);
11768       /* And attributes.  */
11769       attributes = cp_parser_attributes_opt (parser);
11770     }
11771   else
11772     {
11773       asm_specification = NULL_TREE;
11774       attributes = NULL_TREE;
11775     }
11776
11777   /* Peek at the next token.  */
11778   token = cp_lexer_peek_token (parser->lexer);
11779   /* Check to see if the token indicates the start of a
11780      function-definition.  */
11781   if (cp_parser_token_starts_function_definition_p (token))
11782     {
11783       if (!function_definition_allowed_p)
11784         {
11785           /* If a function-definition should not appear here, issue an
11786              error message.  */
11787           cp_parser_error (parser,
11788                            "a function-definition is not allowed here");
11789           return error_mark_node;
11790         }
11791       else
11792         {
11793           /* Neither attributes nor an asm-specification are allowed
11794              on a function-definition.  */
11795           if (asm_specification)
11796             error ("an asm-specification is not allowed on a function-definition");
11797           if (attributes)
11798             error ("attributes are not allowed on a function-definition");
11799           /* This is a function-definition.  */
11800           *function_definition_p = true;
11801
11802           /* Parse the function definition.  */
11803           if (member_p)
11804             decl = cp_parser_save_member_function_body (parser,
11805                                                         decl_specifiers,
11806                                                         declarator,
11807                                                         prefix_attributes);
11808           else
11809             decl
11810               = (cp_parser_function_definition_from_specifiers_and_declarator
11811                  (parser, decl_specifiers, prefix_attributes, declarator));
11812
11813           return decl;
11814         }
11815     }
11816
11817   /* [dcl.dcl]
11818
11819      Only in function declarations for constructors, destructors, and
11820      type conversions can the decl-specifier-seq be omitted.
11821
11822      We explicitly postpone this check past the point where we handle
11823      function-definitions because we tolerate function-definitions
11824      that are missing their return types in some modes.  */
11825   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11826     {
11827       cp_parser_error (parser,
11828                        "expected constructor, destructor, or type conversion");
11829       return error_mark_node;
11830     }
11831
11832   /* An `=' or an `(' indicates an initializer.  */
11833   if (token->type == CPP_EQ
11834       || token->type == CPP_OPEN_PAREN)
11835     {
11836       is_initialized = true;
11837       initialization_kind = token->type;
11838     }
11839   else
11840     {
11841       /* If the init-declarator isn't initialized and isn't followed by a
11842          `,' or `;', it's not a valid init-declarator.  */
11843       if (token->type != CPP_COMMA
11844           && token->type != CPP_SEMICOLON)
11845         {
11846           cp_parser_error (parser, "expected initializer");
11847           return error_mark_node;
11848         }
11849       is_initialized = false;
11850       initialization_kind = CPP_EOF;
11851     }
11852
11853   /* Because start_decl has side-effects, we should only call it if we
11854      know we're going ahead.  By this point, we know that we cannot
11855      possibly be looking at any other construct.  */
11856   cp_parser_commit_to_tentative_parse (parser);
11857
11858   /* If the decl specifiers were bad, issue an error now that we're
11859      sure this was intended to be a declarator.  Then continue
11860      declaring the variable(s), as int, to try to cut down on further
11861      errors.  */
11862   if (decl_specifiers->any_specifiers_p
11863       && decl_specifiers->type == error_mark_node)
11864     {
11865       cp_parser_error (parser, "invalid type in declaration");
11866       decl_specifiers->type = integer_type_node;
11867     }
11868
11869   /* Check to see whether or not this declaration is a friend.  */
11870   friend_p = cp_parser_friend_p (decl_specifiers);
11871
11872   /* Enter the newly declared entry in the symbol table.  If we're
11873      processing a declaration in a class-specifier, we wait until
11874      after processing the initializer.  */
11875   if (!member_p)
11876     {
11877       if (parser->in_unbraced_linkage_specification_p)
11878         decl_specifiers->storage_class = sc_extern;
11879       decl = start_decl (declarator, decl_specifiers,
11880                          is_initialized, attributes, prefix_attributes,
11881                          &pushed_scope);
11882     }
11883   else if (scope)
11884     /* Enter the SCOPE.  That way unqualified names appearing in the
11885        initializer will be looked up in SCOPE.  */
11886     pushed_scope = push_scope (scope);
11887
11888   /* Perform deferred access control checks, now that we know in which
11889      SCOPE the declared entity resides.  */
11890   if (!member_p && decl)
11891     {
11892       tree saved_current_function_decl = NULL_TREE;
11893
11894       /* If the entity being declared is a function, pretend that we
11895          are in its scope.  If it is a `friend', it may have access to
11896          things that would not otherwise be accessible.  */
11897       if (TREE_CODE (decl) == FUNCTION_DECL)
11898         {
11899           saved_current_function_decl = current_function_decl;
11900           current_function_decl = decl;
11901         }
11902
11903       /* Perform access checks for template parameters.  */
11904       cp_parser_perform_template_parameter_access_checks (checks);
11905
11906       /* Perform the access control checks for the declarator and the
11907          the decl-specifiers.  */
11908       perform_deferred_access_checks ();
11909
11910       /* Restore the saved value.  */
11911       if (TREE_CODE (decl) == FUNCTION_DECL)
11912         current_function_decl = saved_current_function_decl;
11913     }
11914
11915   /* Parse the initializer.  */
11916   initializer = NULL_TREE;
11917   is_parenthesized_init = false;
11918   is_non_constant_init = true;
11919   if (is_initialized)
11920     {
11921       if (function_declarator_p (declarator))
11922         {
11923            if (initialization_kind == CPP_EQ)
11924              initializer = cp_parser_pure_specifier (parser);
11925            else
11926              {
11927                /* If the declaration was erroneous, we don't really
11928                   know what the user intended, so just silently
11929                   consume the initializer.  */
11930                if (decl != error_mark_node)
11931                  error ("initializer provided for function");
11932                cp_parser_skip_to_closing_parenthesis (parser,
11933                                                       /*recovering=*/true,
11934                                                       /*or_comma=*/false,
11935                                                       /*consume_paren=*/true);
11936              }
11937         }
11938       else
11939         initializer = cp_parser_initializer (parser,
11940                                              &is_parenthesized_init,
11941                                              &is_non_constant_init);
11942     }
11943
11944   /* The old parser allows attributes to appear after a parenthesized
11945      initializer.  Mark Mitchell proposed removing this functionality
11946      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11947      attributes -- but ignores them.  */
11948   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11949     if (cp_parser_attributes_opt (parser))
11950       warning (OPT_Wattributes,
11951                "attributes after parenthesized initializer ignored");
11952
11953   /* For an in-class declaration, use `grokfield' to create the
11954      declaration.  */
11955   if (member_p)
11956     {
11957       if (pushed_scope)
11958         {
11959           pop_scope (pushed_scope);
11960           pushed_scope = false;
11961         }
11962       decl = grokfield (declarator, decl_specifiers,
11963                         initializer, !is_non_constant_init,
11964                         /*asmspec=*/NULL_TREE,
11965                         prefix_attributes);
11966       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11967         cp_parser_save_default_args (parser, decl);
11968     }
11969
11970   /* Finish processing the declaration.  But, skip friend
11971      declarations.  */
11972   if (!friend_p && decl && decl != error_mark_node)
11973     {
11974       cp_finish_decl (decl,
11975                       initializer, !is_non_constant_init,
11976                       asm_specification,
11977                       /* If the initializer is in parentheses, then this is
11978                          a direct-initialization, which means that an
11979                          `explicit' constructor is OK.  Otherwise, an
11980                          `explicit' constructor cannot be used.  */
11981                       ((is_parenthesized_init || !is_initialized)
11982                      ? 0 : LOOKUP_ONLYCONVERTING));
11983     }
11984   else if ((cxx_dialect != cxx98) && friend_p
11985            && decl && TREE_CODE (decl) == FUNCTION_DECL)
11986     /* Core issue #226 (C++0x only): A default template-argument
11987        shall not be specified in a friend class template
11988        declaration. */
11989     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
11990                              /*is_partial=*/0, /*is_friend_decl=*/1);
11991
11992   if (!friend_p && pushed_scope)
11993     pop_scope (pushed_scope);
11994
11995   return decl;
11996 }
11997
11998 /* Parse a declarator.
11999
12000    declarator:
12001      direct-declarator
12002      ptr-operator declarator
12003
12004    abstract-declarator:
12005      ptr-operator abstract-declarator [opt]
12006      direct-abstract-declarator
12007
12008    GNU Extensions:
12009
12010    declarator:
12011      attributes [opt] direct-declarator
12012      attributes [opt] ptr-operator declarator
12013
12014    abstract-declarator:
12015      attributes [opt] ptr-operator abstract-declarator [opt]
12016      attributes [opt] direct-abstract-declarator
12017
12018    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12019    detect constructor, destructor or conversion operators. It is set
12020    to -1 if the declarator is a name, and +1 if it is a
12021    function. Otherwise it is set to zero. Usually you just want to
12022    test for >0, but internally the negative value is used.
12023
12024    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12025    a decl-specifier-seq unless it declares a constructor, destructor,
12026    or conversion.  It might seem that we could check this condition in
12027    semantic analysis, rather than parsing, but that makes it difficult
12028    to handle something like `f()'.  We want to notice that there are
12029    no decl-specifiers, and therefore realize that this is an
12030    expression, not a declaration.)
12031
12032    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12033    the declarator is a direct-declarator of the form "(...)".
12034
12035    MEMBER_P is true iff this declarator is a member-declarator.  */
12036
12037 static cp_declarator *
12038 cp_parser_declarator (cp_parser* parser,
12039                       cp_parser_declarator_kind dcl_kind,
12040                       int* ctor_dtor_or_conv_p,
12041                       bool* parenthesized_p,
12042                       bool member_p)
12043 {
12044   cp_token *token;
12045   cp_declarator *declarator;
12046   enum tree_code code;
12047   cp_cv_quals cv_quals;
12048   tree class_type;
12049   tree attributes = NULL_TREE;
12050
12051   /* Assume this is not a constructor, destructor, or type-conversion
12052      operator.  */
12053   if (ctor_dtor_or_conv_p)
12054     *ctor_dtor_or_conv_p = 0;
12055
12056   if (cp_parser_allow_gnu_extensions_p (parser))
12057     attributes = cp_parser_attributes_opt (parser);
12058
12059   /* Peek at the next token.  */
12060   token = cp_lexer_peek_token (parser->lexer);
12061
12062   /* Check for the ptr-operator production.  */
12063   cp_parser_parse_tentatively (parser);
12064   /* Parse the ptr-operator.  */
12065   code = cp_parser_ptr_operator (parser,
12066                                  &class_type,
12067                                  &cv_quals);
12068   /* If that worked, then we have a ptr-operator.  */
12069   if (cp_parser_parse_definitely (parser))
12070     {
12071       /* If a ptr-operator was found, then this declarator was not
12072          parenthesized.  */
12073       if (parenthesized_p)
12074         *parenthesized_p = true;
12075       /* The dependent declarator is optional if we are parsing an
12076          abstract-declarator.  */
12077       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12078         cp_parser_parse_tentatively (parser);
12079
12080       /* Parse the dependent declarator.  */
12081       declarator = cp_parser_declarator (parser, dcl_kind,
12082                                          /*ctor_dtor_or_conv_p=*/NULL,
12083                                          /*parenthesized_p=*/NULL,
12084                                          /*member_p=*/false);
12085
12086       /* If we are parsing an abstract-declarator, we must handle the
12087          case where the dependent declarator is absent.  */
12088       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12089           && !cp_parser_parse_definitely (parser))
12090         declarator = NULL;
12091
12092       declarator = cp_parser_make_indirect_declarator
12093         (code, class_type, cv_quals, declarator);
12094     }
12095   /* Everything else is a direct-declarator.  */
12096   else
12097     {
12098       if (parenthesized_p)
12099         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12100                                                    CPP_OPEN_PAREN);
12101       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12102                                                 ctor_dtor_or_conv_p,
12103                                                 member_p);
12104     }
12105
12106   if (attributes && declarator && declarator != cp_error_declarator)
12107     declarator->attributes = attributes;
12108
12109   return declarator;
12110 }
12111
12112 /* Parse a direct-declarator or direct-abstract-declarator.
12113
12114    direct-declarator:
12115      declarator-id
12116      direct-declarator ( parameter-declaration-clause )
12117        cv-qualifier-seq [opt]
12118        exception-specification [opt]
12119      direct-declarator [ constant-expression [opt] ]
12120      ( declarator )
12121
12122    direct-abstract-declarator:
12123      direct-abstract-declarator [opt]
12124        ( parameter-declaration-clause )
12125        cv-qualifier-seq [opt]
12126        exception-specification [opt]
12127      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12128      ( abstract-declarator )
12129
12130    Returns a representation of the declarator.  DCL_KIND is
12131    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12132    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12133    we are parsing a direct-declarator.  It is
12134    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12135    of ambiguity we prefer an abstract declarator, as per
12136    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12137    cp_parser_declarator.  */
12138
12139 static cp_declarator *
12140 cp_parser_direct_declarator (cp_parser* parser,
12141                              cp_parser_declarator_kind dcl_kind,
12142                              int* ctor_dtor_or_conv_p,
12143                              bool member_p)
12144 {
12145   cp_token *token;
12146   cp_declarator *declarator = NULL;
12147   tree scope = NULL_TREE;
12148   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12149   bool saved_in_declarator_p = parser->in_declarator_p;
12150   bool first = true;
12151   tree pushed_scope = NULL_TREE;
12152
12153   while (true)
12154     {
12155       /* Peek at the next token.  */
12156       token = cp_lexer_peek_token (parser->lexer);
12157       if (token->type == CPP_OPEN_PAREN)
12158         {
12159           /* This is either a parameter-declaration-clause, or a
12160              parenthesized declarator. When we know we are parsing a
12161              named declarator, it must be a parenthesized declarator
12162              if FIRST is true. For instance, `(int)' is a
12163              parameter-declaration-clause, with an omitted
12164              direct-abstract-declarator. But `((*))', is a
12165              parenthesized abstract declarator. Finally, when T is a
12166              template parameter `(T)' is a
12167              parameter-declaration-clause, and not a parenthesized
12168              named declarator.
12169
12170              We first try and parse a parameter-declaration-clause,
12171              and then try a nested declarator (if FIRST is true).
12172
12173              It is not an error for it not to be a
12174              parameter-declaration-clause, even when FIRST is
12175              false. Consider,
12176
12177                int i (int);
12178                int i (3);
12179
12180              The first is the declaration of a function while the
12181              second is a the definition of a variable, including its
12182              initializer.
12183
12184              Having seen only the parenthesis, we cannot know which of
12185              these two alternatives should be selected.  Even more
12186              complex are examples like:
12187
12188                int i (int (a));
12189                int i (int (3));
12190
12191              The former is a function-declaration; the latter is a
12192              variable initialization.
12193
12194              Thus again, we try a parameter-declaration-clause, and if
12195              that fails, we back out and return.  */
12196
12197           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12198             {
12199               cp_parameter_declarator *params;
12200               unsigned saved_num_template_parameter_lists;
12201
12202               /* In a member-declarator, the only valid interpretation
12203                  of a parenthesis is the start of a
12204                  parameter-declaration-clause.  (It is invalid to
12205                  initialize a static data member with a parenthesized
12206                  initializer; only the "=" form of initialization is
12207                  permitted.)  */
12208               if (!member_p)
12209                 cp_parser_parse_tentatively (parser);
12210
12211               /* Consume the `('.  */
12212               cp_lexer_consume_token (parser->lexer);
12213               if (first)
12214                 {
12215                   /* If this is going to be an abstract declarator, we're
12216                      in a declarator and we can't have default args.  */
12217                   parser->default_arg_ok_p = false;
12218                   parser->in_declarator_p = true;
12219                 }
12220
12221               /* Inside the function parameter list, surrounding
12222                  template-parameter-lists do not apply.  */
12223               saved_num_template_parameter_lists
12224                 = parser->num_template_parameter_lists;
12225               parser->num_template_parameter_lists = 0;
12226
12227               /* Parse the parameter-declaration-clause.  */
12228               params = cp_parser_parameter_declaration_clause (parser);
12229
12230               parser->num_template_parameter_lists
12231                 = saved_num_template_parameter_lists;
12232
12233               /* If all went well, parse the cv-qualifier-seq and the
12234                  exception-specification.  */
12235               if (member_p || cp_parser_parse_definitely (parser))
12236                 {
12237                   cp_cv_quals cv_quals;
12238                   tree exception_specification;
12239
12240                   if (ctor_dtor_or_conv_p)
12241                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12242                   first = false;
12243                   /* Consume the `)'.  */
12244                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12245
12246                   /* Parse the cv-qualifier-seq.  */
12247                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12248                   /* And the exception-specification.  */
12249                   exception_specification
12250                     = cp_parser_exception_specification_opt (parser);
12251
12252                   /* Create the function-declarator.  */
12253                   declarator = make_call_declarator (declarator,
12254                                                      params,
12255                                                      cv_quals,
12256                                                      exception_specification);
12257                   /* Any subsequent parameter lists are to do with
12258                      return type, so are not those of the declared
12259                      function.  */
12260                   parser->default_arg_ok_p = false;
12261
12262                   /* Repeat the main loop.  */
12263                   continue;
12264                 }
12265             }
12266
12267           /* If this is the first, we can try a parenthesized
12268              declarator.  */
12269           if (first)
12270             {
12271               bool saved_in_type_id_in_expr_p;
12272
12273               parser->default_arg_ok_p = saved_default_arg_ok_p;
12274               parser->in_declarator_p = saved_in_declarator_p;
12275
12276               /* Consume the `('.  */
12277               cp_lexer_consume_token (parser->lexer);
12278               /* Parse the nested declarator.  */
12279               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12280               parser->in_type_id_in_expr_p = true;
12281               declarator
12282                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12283                                         /*parenthesized_p=*/NULL,
12284                                         member_p);
12285               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12286               first = false;
12287               /* Expect a `)'.  */
12288               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12289                 declarator = cp_error_declarator;
12290               if (declarator == cp_error_declarator)
12291                 break;
12292
12293               goto handle_declarator;
12294             }
12295           /* Otherwise, we must be done.  */
12296           else
12297             break;
12298         }
12299       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12300                && token->type == CPP_OPEN_SQUARE)
12301         {
12302           /* Parse an array-declarator.  */
12303           tree bounds;
12304
12305           if (ctor_dtor_or_conv_p)
12306             *ctor_dtor_or_conv_p = 0;
12307
12308           first = false;
12309           parser->default_arg_ok_p = false;
12310           parser->in_declarator_p = true;
12311           /* Consume the `['.  */
12312           cp_lexer_consume_token (parser->lexer);
12313           /* Peek at the next token.  */
12314           token = cp_lexer_peek_token (parser->lexer);
12315           /* If the next token is `]', then there is no
12316              constant-expression.  */
12317           if (token->type != CPP_CLOSE_SQUARE)
12318             {
12319               bool non_constant_p;
12320
12321               bounds
12322                 = cp_parser_constant_expression (parser,
12323                                                  /*allow_non_constant=*/true,
12324                                                  &non_constant_p);
12325               if (!non_constant_p)
12326                 bounds = fold_non_dependent_expr (bounds);
12327               /* Normally, the array bound must be an integral constant
12328                  expression.  However, as an extension, we allow VLAs
12329                  in function scopes.  */
12330               else if (!parser->in_function_body)
12331                 {
12332                   error ("array bound is not an integer constant");
12333                   bounds = error_mark_node;
12334                 }
12335             }
12336           else
12337             bounds = NULL_TREE;
12338           /* Look for the closing `]'.  */
12339           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12340             {
12341               declarator = cp_error_declarator;
12342               break;
12343             }
12344
12345           declarator = make_array_declarator (declarator, bounds);
12346         }
12347       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12348         {
12349           tree qualifying_scope;
12350           tree unqualified_name;
12351           special_function_kind sfk;
12352           bool abstract_ok;
12353           bool pack_expansion_p = false;
12354
12355           /* Parse a declarator-id */
12356           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12357           if (abstract_ok)
12358             {
12359               cp_parser_parse_tentatively (parser);
12360
12361               /* If we see an ellipsis, we should be looking at a
12362                  parameter pack. */
12363               if (token->type == CPP_ELLIPSIS)
12364                 {
12365                   /* Consume the `...' */
12366                   cp_lexer_consume_token (parser->lexer);
12367
12368                   pack_expansion_p = true;
12369                 }
12370             }
12371
12372           unqualified_name
12373             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12374           qualifying_scope = parser->scope;
12375           if (abstract_ok)
12376             {
12377               bool okay = false;
12378
12379               if (!unqualified_name && pack_expansion_p)
12380                 {
12381                   /* Check whether an error occurred. */
12382                   okay = !cp_parser_error_occurred (parser);
12383
12384                   /* We already consumed the ellipsis to mark a
12385                      parameter pack, but we have no way to report it,
12386                      so abort the tentative parse. We will be exiting
12387                      immediately anyway. */
12388                   cp_parser_abort_tentative_parse (parser);
12389                 }
12390               else
12391                 okay = cp_parser_parse_definitely (parser);
12392
12393               if (!okay)
12394                 unqualified_name = error_mark_node;
12395               else if (unqualified_name
12396                        && (qualifying_scope
12397                            || (TREE_CODE (unqualified_name)
12398                                != IDENTIFIER_NODE)))
12399                 {
12400                   cp_parser_error (parser, "expected unqualified-id");
12401                   unqualified_name = error_mark_node;
12402                 }
12403             }
12404
12405           if (!unqualified_name)
12406             return NULL;
12407           if (unqualified_name == error_mark_node)
12408             {
12409               declarator = cp_error_declarator;
12410               pack_expansion_p = false;
12411               declarator->parameter_pack_p = false;
12412               break;
12413             }
12414
12415           if (qualifying_scope && at_namespace_scope_p ()
12416               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12417             {
12418               /* In the declaration of a member of a template class
12419                  outside of the class itself, the SCOPE will sometimes
12420                  be a TYPENAME_TYPE.  For example, given:
12421
12422                  template <typename T>
12423                  int S<T>::R::i = 3;
12424
12425                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
12426                  this context, we must resolve S<T>::R to an ordinary
12427                  type, rather than a typename type.
12428
12429                  The reason we normally avoid resolving TYPENAME_TYPEs
12430                  is that a specialization of `S' might render
12431                  `S<T>::R' not a type.  However, if `S' is
12432                  specialized, then this `i' will not be used, so there
12433                  is no harm in resolving the types here.  */
12434               tree type;
12435
12436               /* Resolve the TYPENAME_TYPE.  */
12437               type = resolve_typename_type (qualifying_scope,
12438                                             /*only_current_p=*/false);
12439               /* If that failed, the declarator is invalid.  */
12440               if (type == error_mark_node)
12441                 error ("%<%T::%E%> is not a type",
12442                        TYPE_CONTEXT (qualifying_scope),
12443                        TYPE_IDENTIFIER (qualifying_scope));
12444               qualifying_scope = type;
12445             }
12446
12447           sfk = sfk_none;
12448
12449           if (unqualified_name)
12450             {
12451               tree class_type;
12452
12453               if (qualifying_scope
12454                   && CLASS_TYPE_P (qualifying_scope))
12455                 class_type = qualifying_scope;
12456               else
12457                 class_type = current_class_type;
12458
12459               if (TREE_CODE (unqualified_name) == TYPE_DECL)
12460                 {
12461                   tree name_type = TREE_TYPE (unqualified_name);
12462                   if (class_type && same_type_p (name_type, class_type))
12463                     {
12464                       if (qualifying_scope
12465                           && CLASSTYPE_USE_TEMPLATE (name_type))
12466                         {
12467                           error ("invalid use of constructor as a template");
12468                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12469                                   "name the constructor in a qualified name",
12470                                   class_type,
12471                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12472                                   class_type, name_type);
12473                           declarator = cp_error_declarator;
12474                           break;
12475                         }
12476                       else
12477                         unqualified_name = constructor_name (class_type);
12478                     }
12479                   else
12480                     {
12481                       /* We do not attempt to print the declarator
12482                          here because we do not have enough
12483                          information about its original syntactic
12484                          form.  */
12485                       cp_parser_error (parser, "invalid declarator");
12486                       declarator = cp_error_declarator;
12487                       break;
12488                     }
12489                 }
12490
12491               if (class_type)
12492                 {
12493                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12494                     sfk = sfk_destructor;
12495                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12496                     sfk = sfk_conversion;
12497                   else if (/* There's no way to declare a constructor
12498                               for an anonymous type, even if the type
12499                               got a name for linkage purposes.  */
12500                            !TYPE_WAS_ANONYMOUS (class_type)
12501                            && constructor_name_p (unqualified_name,
12502                                                   class_type))
12503                     {
12504                       unqualified_name = constructor_name (class_type);
12505                       sfk = sfk_constructor;
12506                     }
12507
12508                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
12509                     *ctor_dtor_or_conv_p = -1;
12510                 }
12511             }
12512           declarator = make_id_declarator (qualifying_scope,
12513                                            unqualified_name,
12514                                            sfk);
12515           declarator->id_loc = token->location;
12516           declarator->parameter_pack_p = pack_expansion_p;
12517
12518           if (pack_expansion_p)
12519             maybe_warn_variadic_templates ();
12520
12521         handle_declarator:;
12522           scope = get_scope_of_declarator (declarator);
12523           if (scope)
12524             /* Any names that appear after the declarator-id for a
12525                member are looked up in the containing scope.  */
12526             pushed_scope = push_scope (scope);
12527           parser->in_declarator_p = true;
12528           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12529               || (declarator && declarator->kind == cdk_id))
12530             /* Default args are only allowed on function
12531                declarations.  */
12532             parser->default_arg_ok_p = saved_default_arg_ok_p;
12533           else
12534             parser->default_arg_ok_p = false;
12535
12536           first = false;
12537         }
12538       /* We're done.  */
12539       else
12540         break;
12541     }
12542
12543   /* For an abstract declarator, we might wind up with nothing at this
12544      point.  That's an error; the declarator is not optional.  */
12545   if (!declarator)
12546     cp_parser_error (parser, "expected declarator");
12547
12548   /* If we entered a scope, we must exit it now.  */
12549   if (pushed_scope)
12550     pop_scope (pushed_scope);
12551
12552   parser->default_arg_ok_p = saved_default_arg_ok_p;
12553   parser->in_declarator_p = saved_in_declarator_p;
12554
12555   return declarator;
12556 }
12557
12558 /* Parse a ptr-operator.
12559
12560    ptr-operator:
12561      * cv-qualifier-seq [opt]
12562      &
12563      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12564
12565    GNU Extension:
12566
12567    ptr-operator:
12568      & cv-qualifier-seq [opt]
12569
12570    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12571    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12572    an rvalue reference. In the case of a pointer-to-member, *TYPE is
12573    filled in with the TYPE containing the member.  *CV_QUALS is
12574    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12575    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
12576    Note that the tree codes returned by this function have nothing
12577    to do with the types of trees that will be eventually be created
12578    to represent the pointer or reference type being parsed. They are
12579    just constants with suggestive names. */
12580 static enum tree_code
12581 cp_parser_ptr_operator (cp_parser* parser,
12582                         tree* type,
12583                         cp_cv_quals *cv_quals)
12584 {
12585   enum tree_code code = ERROR_MARK;
12586   cp_token *token;
12587
12588   /* Assume that it's not a pointer-to-member.  */
12589   *type = NULL_TREE;
12590   /* And that there are no cv-qualifiers.  */
12591   *cv_quals = TYPE_UNQUALIFIED;
12592
12593   /* Peek at the next token.  */
12594   token = cp_lexer_peek_token (parser->lexer);
12595
12596   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
12597   if (token->type == CPP_MULT)
12598     code = INDIRECT_REF;
12599   else if (token->type == CPP_AND)
12600     code = ADDR_EXPR;
12601   else if ((cxx_dialect != cxx98) &&
12602            token->type == CPP_AND_AND) /* C++0x only */
12603     code = NON_LVALUE_EXPR;
12604
12605   if (code != ERROR_MARK)
12606     {
12607       /* Consume the `*', `&' or `&&'.  */
12608       cp_lexer_consume_token (parser->lexer);
12609
12610       /* A `*' can be followed by a cv-qualifier-seq, and so can a
12611          `&', if we are allowing GNU extensions.  (The only qualifier
12612          that can legally appear after `&' is `restrict', but that is
12613          enforced during semantic analysis.  */
12614       if (code == INDIRECT_REF
12615           || cp_parser_allow_gnu_extensions_p (parser))
12616         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12617     }
12618   else
12619     {
12620       /* Try the pointer-to-member case.  */
12621       cp_parser_parse_tentatively (parser);
12622       /* Look for the optional `::' operator.  */
12623       cp_parser_global_scope_opt (parser,
12624                                   /*current_scope_valid_p=*/false);
12625       /* Look for the nested-name specifier.  */
12626       cp_parser_nested_name_specifier (parser,
12627                                        /*typename_keyword_p=*/false,
12628                                        /*check_dependency_p=*/true,
12629                                        /*type_p=*/false,
12630                                        /*is_declaration=*/false);
12631       /* If we found it, and the next token is a `*', then we are
12632          indeed looking at a pointer-to-member operator.  */
12633       if (!cp_parser_error_occurred (parser)
12634           && cp_parser_require (parser, CPP_MULT, "`*'"))
12635         {
12636           /* Indicate that the `*' operator was used.  */
12637           code = INDIRECT_REF;
12638
12639           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12640             error ("%qD is a namespace", parser->scope);
12641           else
12642             {
12643               /* The type of which the member is a member is given by the
12644                  current SCOPE.  */
12645               *type = parser->scope;
12646               /* The next name will not be qualified.  */
12647               parser->scope = NULL_TREE;
12648               parser->qualifying_scope = NULL_TREE;
12649               parser->object_scope = NULL_TREE;
12650               /* Look for the optional cv-qualifier-seq.  */
12651               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12652             }
12653         }
12654       /* If that didn't work we don't have a ptr-operator.  */
12655       if (!cp_parser_parse_definitely (parser))
12656         cp_parser_error (parser, "expected ptr-operator");
12657     }
12658
12659   return code;
12660 }
12661
12662 /* Parse an (optional) cv-qualifier-seq.
12663
12664    cv-qualifier-seq:
12665      cv-qualifier cv-qualifier-seq [opt]
12666
12667    cv-qualifier:
12668      const
12669      volatile
12670
12671    GNU Extension:
12672
12673    cv-qualifier:
12674      __restrict__
12675
12676    Returns a bitmask representing the cv-qualifiers.  */
12677
12678 static cp_cv_quals
12679 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12680 {
12681   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12682
12683   while (true)
12684     {
12685       cp_token *token;
12686       cp_cv_quals cv_qualifier;
12687
12688       /* Peek at the next token.  */
12689       token = cp_lexer_peek_token (parser->lexer);
12690       /* See if it's a cv-qualifier.  */
12691       switch (token->keyword)
12692         {
12693         case RID_CONST:
12694           cv_qualifier = TYPE_QUAL_CONST;
12695           break;
12696
12697         case RID_VOLATILE:
12698           cv_qualifier = TYPE_QUAL_VOLATILE;
12699           break;
12700
12701         case RID_RESTRICT:
12702           cv_qualifier = TYPE_QUAL_RESTRICT;
12703           break;
12704
12705         default:
12706           cv_qualifier = TYPE_UNQUALIFIED;
12707           break;
12708         }
12709
12710       if (!cv_qualifier)
12711         break;
12712
12713       if (cv_quals & cv_qualifier)
12714         {
12715           error ("duplicate cv-qualifier");
12716           cp_lexer_purge_token (parser->lexer);
12717         }
12718       else
12719         {
12720           cp_lexer_consume_token (parser->lexer);
12721           cv_quals |= cv_qualifier;
12722         }
12723     }
12724
12725   return cv_quals;
12726 }
12727
12728 /* Parse a declarator-id.
12729
12730    declarator-id:
12731      id-expression
12732      :: [opt] nested-name-specifier [opt] type-name
12733
12734    In the `id-expression' case, the value returned is as for
12735    cp_parser_id_expression if the id-expression was an unqualified-id.
12736    If the id-expression was a qualified-id, then a SCOPE_REF is
12737    returned.  The first operand is the scope (either a NAMESPACE_DECL
12738    or TREE_TYPE), but the second is still just a representation of an
12739    unqualified-id.  */
12740
12741 static tree
12742 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12743 {
12744   tree id;
12745   /* The expression must be an id-expression.  Assume that qualified
12746      names are the names of types so that:
12747
12748        template <class T>
12749        int S<T>::R::i = 3;
12750
12751      will work; we must treat `S<T>::R' as the name of a type.
12752      Similarly, assume that qualified names are templates, where
12753      required, so that:
12754
12755        template <class T>
12756        int S<T>::R<T>::i = 3;
12757
12758      will work, too.  */
12759   id = cp_parser_id_expression (parser,
12760                                 /*template_keyword_p=*/false,
12761                                 /*check_dependency_p=*/false,
12762                                 /*template_p=*/NULL,
12763                                 /*declarator_p=*/true,
12764                                 optional_p);
12765   if (id && BASELINK_P (id))
12766     id = BASELINK_FUNCTIONS (id);
12767   return id;
12768 }
12769
12770 /* Parse a type-id.
12771
12772    type-id:
12773      type-specifier-seq abstract-declarator [opt]
12774
12775    Returns the TYPE specified.  */
12776
12777 static tree
12778 cp_parser_type_id (cp_parser* parser)
12779 {
12780   cp_decl_specifier_seq type_specifier_seq;
12781   cp_declarator *abstract_declarator;
12782
12783   /* Parse the type-specifier-seq.  */
12784   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12785                                 &type_specifier_seq);
12786   if (type_specifier_seq.type == error_mark_node)
12787     return error_mark_node;
12788
12789   /* There might or might not be an abstract declarator.  */
12790   cp_parser_parse_tentatively (parser);
12791   /* Look for the declarator.  */
12792   abstract_declarator
12793     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12794                             /*parenthesized_p=*/NULL,
12795                             /*member_p=*/false);
12796   /* Check to see if there really was a declarator.  */
12797   if (!cp_parser_parse_definitely (parser))
12798     abstract_declarator = NULL;
12799
12800   return groktypename (&type_specifier_seq, abstract_declarator);
12801 }
12802
12803 /* Parse a type-specifier-seq.
12804
12805    type-specifier-seq:
12806      type-specifier type-specifier-seq [opt]
12807
12808    GNU extension:
12809
12810    type-specifier-seq:
12811      attributes type-specifier-seq [opt]
12812
12813    If IS_CONDITION is true, we are at the start of a "condition",
12814    e.g., we've just seen "if (".
12815
12816    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
12817
12818 static void
12819 cp_parser_type_specifier_seq (cp_parser* parser,
12820                               bool is_condition,
12821                               cp_decl_specifier_seq *type_specifier_seq)
12822 {
12823   bool seen_type_specifier = false;
12824   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12825
12826   /* Clear the TYPE_SPECIFIER_SEQ.  */
12827   clear_decl_specs (type_specifier_seq);
12828
12829   /* Parse the type-specifiers and attributes.  */
12830   while (true)
12831     {
12832       tree type_specifier;
12833       bool is_cv_qualifier;
12834
12835       /* Check for attributes first.  */
12836       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12837         {
12838           type_specifier_seq->attributes =
12839             chainon (type_specifier_seq->attributes,
12840                      cp_parser_attributes_opt (parser));
12841           continue;
12842         }
12843
12844       /* Look for the type-specifier.  */
12845       type_specifier = cp_parser_type_specifier (parser,
12846                                                  flags,
12847                                                  type_specifier_seq,
12848                                                  /*is_declaration=*/false,
12849                                                  NULL,
12850                                                  &is_cv_qualifier);
12851       if (!type_specifier)
12852         {
12853           /* If the first type-specifier could not be found, this is not a
12854              type-specifier-seq at all.  */
12855           if (!seen_type_specifier)
12856             {
12857               cp_parser_error (parser, "expected type-specifier");
12858               type_specifier_seq->type = error_mark_node;
12859               return;
12860             }
12861           /* If subsequent type-specifiers could not be found, the
12862              type-specifier-seq is complete.  */
12863           break;
12864         }
12865
12866       seen_type_specifier = true;
12867       /* The standard says that a condition can be:
12868
12869             type-specifier-seq declarator = assignment-expression
12870
12871          However, given:
12872
12873            struct S {};
12874            if (int S = ...)
12875
12876          we should treat the "S" as a declarator, not as a
12877          type-specifier.  The standard doesn't say that explicitly for
12878          type-specifier-seq, but it does say that for
12879          decl-specifier-seq in an ordinary declaration.  Perhaps it
12880          would be clearer just to allow a decl-specifier-seq here, and
12881          then add a semantic restriction that if any decl-specifiers
12882          that are not type-specifiers appear, the program is invalid.  */
12883       if (is_condition && !is_cv_qualifier)
12884         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12885     }
12886
12887   cp_parser_check_decl_spec (type_specifier_seq);
12888 }
12889
12890 /* Parse a parameter-declaration-clause.
12891
12892    parameter-declaration-clause:
12893      parameter-declaration-list [opt] ... [opt]
12894      parameter-declaration-list , ...
12895
12896    Returns a representation for the parameter declarations.  A return
12897    value of NULL indicates a parameter-declaration-clause consisting
12898    only of an ellipsis.  */
12899
12900 static cp_parameter_declarator *
12901 cp_parser_parameter_declaration_clause (cp_parser* parser)
12902 {
12903   cp_parameter_declarator *parameters;
12904   cp_token *token;
12905   bool ellipsis_p;
12906   bool is_error;
12907
12908   /* Peek at the next token.  */
12909   token = cp_lexer_peek_token (parser->lexer);
12910   /* Check for trivial parameter-declaration-clauses.  */
12911   if (token->type == CPP_ELLIPSIS)
12912     {
12913       /* Consume the `...' token.  */
12914       cp_lexer_consume_token (parser->lexer);
12915       return NULL;
12916     }
12917   else if (token->type == CPP_CLOSE_PAREN)
12918     /* There are no parameters.  */
12919     {
12920 #ifndef NO_IMPLICIT_EXTERN_C
12921       if (in_system_header && current_class_type == NULL
12922           && current_lang_name == lang_name_c)
12923         return NULL;
12924       else
12925 #endif
12926         return no_parameters;
12927     }
12928   /* Check for `(void)', too, which is a special case.  */
12929   else if (token->keyword == RID_VOID
12930            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12931                == CPP_CLOSE_PAREN))
12932     {
12933       /* Consume the `void' token.  */
12934       cp_lexer_consume_token (parser->lexer);
12935       /* There are no parameters.  */
12936       return no_parameters;
12937     }
12938
12939   /* Parse the parameter-declaration-list.  */
12940   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12941   /* If a parse error occurred while parsing the
12942      parameter-declaration-list, then the entire
12943      parameter-declaration-clause is erroneous.  */
12944   if (is_error)
12945     return NULL;
12946
12947   /* Peek at the next token.  */
12948   token = cp_lexer_peek_token (parser->lexer);
12949   /* If it's a `,', the clause should terminate with an ellipsis.  */
12950   if (token->type == CPP_COMMA)
12951     {
12952       /* Consume the `,'.  */
12953       cp_lexer_consume_token (parser->lexer);
12954       /* Expect an ellipsis.  */
12955       ellipsis_p
12956         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12957     }
12958   /* It might also be `...' if the optional trailing `,' was
12959      omitted.  */
12960   else if (token->type == CPP_ELLIPSIS)
12961     {
12962       /* Consume the `...' token.  */
12963       cp_lexer_consume_token (parser->lexer);
12964       /* And remember that we saw it.  */
12965       ellipsis_p = true;
12966     }
12967   else
12968     ellipsis_p = false;
12969
12970   /* Finish the parameter list.  */
12971   if (parameters && ellipsis_p)
12972     parameters->ellipsis_p = true;
12973
12974   return parameters;
12975 }
12976
12977 /* Parse a parameter-declaration-list.
12978
12979    parameter-declaration-list:
12980      parameter-declaration
12981      parameter-declaration-list , parameter-declaration
12982
12983    Returns a representation of the parameter-declaration-list, as for
12984    cp_parser_parameter_declaration_clause.  However, the
12985    `void_list_node' is never appended to the list.  Upon return,
12986    *IS_ERROR will be true iff an error occurred.  */
12987
12988 static cp_parameter_declarator *
12989 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12990 {
12991   cp_parameter_declarator *parameters = NULL;
12992   cp_parameter_declarator **tail = &parameters;
12993   bool saved_in_unbraced_linkage_specification_p;
12994
12995   /* Assume all will go well.  */
12996   *is_error = false;
12997   /* The special considerations that apply to a function within an
12998      unbraced linkage specifications do not apply to the parameters
12999      to the function.  */
13000   saved_in_unbraced_linkage_specification_p 
13001     = parser->in_unbraced_linkage_specification_p;
13002   parser->in_unbraced_linkage_specification_p = false;
13003
13004   /* Look for more parameters.  */
13005   while (true)
13006     {
13007       cp_parameter_declarator *parameter;
13008       bool parenthesized_p;
13009       /* Parse the parameter.  */
13010       parameter
13011         = cp_parser_parameter_declaration (parser,
13012                                            /*template_parm_p=*/false,
13013                                            &parenthesized_p);
13014
13015       /* If a parse error occurred parsing the parameter declaration,
13016          then the entire parameter-declaration-list is erroneous.  */
13017       if (!parameter)
13018         {
13019           *is_error = true;
13020           parameters = NULL;
13021           break;
13022         }
13023       /* Add the new parameter to the list.  */
13024       *tail = parameter;
13025       tail = &parameter->next;
13026
13027       /* Peek at the next token.  */
13028       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13029           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13030           /* These are for Objective-C++ */
13031           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13032           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13033         /* The parameter-declaration-list is complete.  */
13034         break;
13035       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13036         {
13037           cp_token *token;
13038
13039           /* Peek at the next token.  */
13040           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13041           /* If it's an ellipsis, then the list is complete.  */
13042           if (token->type == CPP_ELLIPSIS)
13043             break;
13044           /* Otherwise, there must be more parameters.  Consume the
13045              `,'.  */
13046           cp_lexer_consume_token (parser->lexer);
13047           /* When parsing something like:
13048
13049                 int i(float f, double d)
13050
13051              we can tell after seeing the declaration for "f" that we
13052              are not looking at an initialization of a variable "i",
13053              but rather at the declaration of a function "i".
13054
13055              Due to the fact that the parsing of template arguments
13056              (as specified to a template-id) requires backtracking we
13057              cannot use this technique when inside a template argument
13058              list.  */
13059           if (!parser->in_template_argument_list_p
13060               && !parser->in_type_id_in_expr_p
13061               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13062               /* However, a parameter-declaration of the form
13063                  "foat(f)" (which is a valid declaration of a
13064                  parameter "f") can also be interpreted as an
13065                  expression (the conversion of "f" to "float").  */
13066               && !parenthesized_p)
13067             cp_parser_commit_to_tentative_parse (parser);
13068         }
13069       else
13070         {
13071           cp_parser_error (parser, "expected %<,%> or %<...%>");
13072           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13073             cp_parser_skip_to_closing_parenthesis (parser,
13074                                                    /*recovering=*/true,
13075                                                    /*or_comma=*/false,
13076                                                    /*consume_paren=*/false);
13077           break;
13078         }
13079     }
13080
13081   parser->in_unbraced_linkage_specification_p
13082     = saved_in_unbraced_linkage_specification_p;
13083
13084   return parameters;
13085 }
13086
13087 /* Parse a parameter declaration.
13088
13089    parameter-declaration:
13090      decl-specifier-seq ... [opt] declarator
13091      decl-specifier-seq declarator = assignment-expression
13092      decl-specifier-seq ... [opt] abstract-declarator [opt]
13093      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13094
13095    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13096    declares a template parameter.  (In that case, a non-nested `>'
13097    token encountered during the parsing of the assignment-expression
13098    is not interpreted as a greater-than operator.)
13099
13100    Returns a representation of the parameter, or NULL if an error
13101    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13102    true iff the declarator is of the form "(p)".  */
13103
13104 static cp_parameter_declarator *
13105 cp_parser_parameter_declaration (cp_parser *parser,
13106                                  bool template_parm_p,
13107                                  bool *parenthesized_p)
13108 {
13109   int declares_class_or_enum;
13110   bool greater_than_is_operator_p;
13111   cp_decl_specifier_seq decl_specifiers;
13112   cp_declarator *declarator;
13113   tree default_argument;
13114   cp_token *token;
13115   const char *saved_message;
13116
13117   /* In a template parameter, `>' is not an operator.
13118
13119      [temp.param]
13120
13121      When parsing a default template-argument for a non-type
13122      template-parameter, the first non-nested `>' is taken as the end
13123      of the template parameter-list rather than a greater-than
13124      operator.  */
13125   greater_than_is_operator_p = !template_parm_p;
13126
13127   /* Type definitions may not appear in parameter types.  */
13128   saved_message = parser->type_definition_forbidden_message;
13129   parser->type_definition_forbidden_message
13130     = "types may not be defined in parameter types";
13131
13132   /* Parse the declaration-specifiers.  */
13133   cp_parser_decl_specifier_seq (parser,
13134                                 CP_PARSER_FLAGS_NONE,
13135                                 &decl_specifiers,
13136                                 &declares_class_or_enum);
13137   /* If an error occurred, there's no reason to attempt to parse the
13138      rest of the declaration.  */
13139   if (cp_parser_error_occurred (parser))
13140     {
13141       parser->type_definition_forbidden_message = saved_message;
13142       return NULL;
13143     }
13144
13145   /* Peek at the next token.  */
13146   token = cp_lexer_peek_token (parser->lexer);
13147
13148   /* If the next token is a `)', `,', `=', `>', or `...', then there
13149      is no declarator. However, when variadic templates are enabled,
13150      there may be a declarator following `...'.  */
13151   if (token->type == CPP_CLOSE_PAREN
13152       || token->type == CPP_COMMA
13153       || token->type == CPP_EQ
13154       || token->type == CPP_GREATER)
13155     {
13156       declarator = NULL;
13157       if (parenthesized_p)
13158         *parenthesized_p = false;
13159     }
13160   /* Otherwise, there should be a declarator.  */
13161   else
13162     {
13163       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13164       parser->default_arg_ok_p = false;
13165
13166       /* After seeing a decl-specifier-seq, if the next token is not a
13167          "(", there is no possibility that the code is a valid
13168          expression.  Therefore, if parsing tentatively, we commit at
13169          this point.  */
13170       if (!parser->in_template_argument_list_p
13171           /* In an expression context, having seen:
13172
13173                (int((char ...
13174
13175              we cannot be sure whether we are looking at a
13176              function-type (taking a "char" as a parameter) or a cast
13177              of some object of type "char" to "int".  */
13178           && !parser->in_type_id_in_expr_p
13179           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13180           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13181         cp_parser_commit_to_tentative_parse (parser);
13182       /* Parse the declarator.  */
13183       declarator = cp_parser_declarator (parser,
13184                                          CP_PARSER_DECLARATOR_EITHER,
13185                                          /*ctor_dtor_or_conv_p=*/NULL,
13186                                          parenthesized_p,
13187                                          /*member_p=*/false);
13188       parser->default_arg_ok_p = saved_default_arg_ok_p;
13189       /* After the declarator, allow more attributes.  */
13190       decl_specifiers.attributes
13191         = chainon (decl_specifiers.attributes,
13192                    cp_parser_attributes_opt (parser));
13193     }
13194
13195   /* If the next token is an ellipsis, and we have not seen a
13196      declarator name, and the type of the declarator contains parameter
13197      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13198      a parameter pack expansion expression. Otherwise, leave the
13199      ellipsis for a C-style variadic function. */
13200   token = cp_lexer_peek_token (parser->lexer);
13201   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13202     {
13203       tree type = decl_specifiers.type;
13204
13205       if (type && DECL_P (type))
13206         type = TREE_TYPE (type);
13207
13208       if (type
13209           && TREE_CODE (type) != TYPE_PACK_EXPANSION
13210           && declarator_can_be_parameter_pack (declarator)
13211           && (!declarator || !declarator->parameter_pack_p)
13212           && uses_parameter_packs (type))
13213         {
13214           /* Consume the `...'. */
13215           cp_lexer_consume_token (parser->lexer);
13216           maybe_warn_variadic_templates ();
13217           
13218           /* Build a pack expansion type */
13219           if (declarator)
13220             declarator->parameter_pack_p = true;
13221           else
13222             decl_specifiers.type = make_pack_expansion (type);
13223         }
13224     }
13225
13226   /* The restriction on defining new types applies only to the type
13227      of the parameter, not to the default argument.  */
13228   parser->type_definition_forbidden_message = saved_message;
13229
13230   /* If the next token is `=', then process a default argument.  */
13231   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13232     {
13233       bool saved_greater_than_is_operator_p;
13234       /* Consume the `='.  */
13235       cp_lexer_consume_token (parser->lexer);
13236
13237       /* If we are defining a class, then the tokens that make up the
13238          default argument must be saved and processed later.  */
13239       if (!template_parm_p && at_class_scope_p ()
13240           && TYPE_BEING_DEFINED (current_class_type))
13241         {
13242           unsigned depth = 0;
13243           cp_token *first_token;
13244           cp_token *token;
13245
13246           /* Add tokens until we have processed the entire default
13247              argument.  We add the range [first_token, token).  */
13248           first_token = cp_lexer_peek_token (parser->lexer);
13249           while (true)
13250             {
13251               bool done = false;
13252
13253               /* Peek at the next token.  */
13254               token = cp_lexer_peek_token (parser->lexer);
13255               /* What we do depends on what token we have.  */
13256               switch (token->type)
13257                 {
13258                   /* In valid code, a default argument must be
13259                      immediately followed by a `,' `)', or `...'.  */
13260                 case CPP_COMMA:
13261                 case CPP_CLOSE_PAREN:
13262                 case CPP_ELLIPSIS:
13263                   /* If we run into a non-nested `;', `}', or `]',
13264                      then the code is invalid -- but the default
13265                      argument is certainly over.  */
13266                 case CPP_SEMICOLON:
13267                 case CPP_CLOSE_BRACE:
13268                 case CPP_CLOSE_SQUARE:
13269                   if (depth == 0)
13270                     done = true;
13271                   /* Update DEPTH, if necessary.  */
13272                   else if (token->type == CPP_CLOSE_PAREN
13273                            || token->type == CPP_CLOSE_BRACE
13274                            || token->type == CPP_CLOSE_SQUARE)
13275                     --depth;
13276                   break;
13277
13278                 case CPP_OPEN_PAREN:
13279                 case CPP_OPEN_SQUARE:
13280                 case CPP_OPEN_BRACE:
13281                   ++depth;
13282                   break;
13283
13284                 case CPP_RSHIFT:
13285                   if (cxx_dialect == cxx98)
13286                     break;
13287                   /* Fall through for C++0x, which treats the `>>'
13288                      operator like two `>' tokens in certain
13289                      cases.  */
13290
13291                 case CPP_GREATER:
13292                   /* If we see a non-nested `>', and `>' is not an
13293                      operator, then it marks the end of the default
13294                      argument.  */
13295                   if (!depth && !greater_than_is_operator_p)
13296                     done = true;
13297                   break;
13298
13299                   /* If we run out of tokens, issue an error message.  */
13300                 case CPP_EOF:
13301                 case CPP_PRAGMA_EOL:
13302                   error ("file ends in default argument");
13303                   done = true;
13304                   break;
13305
13306                 case CPP_NAME:
13307                 case CPP_SCOPE:
13308                   /* In these cases, we should look for template-ids.
13309                      For example, if the default argument is
13310                      `X<int, double>()', we need to do name lookup to
13311                      figure out whether or not `X' is a template; if
13312                      so, the `,' does not end the default argument.
13313
13314                      That is not yet done.  */
13315                   break;
13316
13317                 default:
13318                   break;
13319                 }
13320
13321               /* If we've reached the end, stop.  */
13322               if (done)
13323                 break;
13324
13325               /* Add the token to the token block.  */
13326               token = cp_lexer_consume_token (parser->lexer);
13327             }
13328
13329           /* Create a DEFAULT_ARG to represented the unparsed default
13330              argument.  */
13331           default_argument = make_node (DEFAULT_ARG);
13332           DEFARG_TOKENS (default_argument)
13333             = cp_token_cache_new (first_token, token);
13334           DEFARG_INSTANTIATIONS (default_argument) = NULL;
13335         }
13336       /* Outside of a class definition, we can just parse the
13337          assignment-expression.  */
13338       else
13339         {
13340           bool saved_local_variables_forbidden_p;
13341
13342           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13343              set correctly.  */
13344           saved_greater_than_is_operator_p
13345             = parser->greater_than_is_operator_p;
13346           parser->greater_than_is_operator_p = greater_than_is_operator_p;
13347           /* Local variable names (and the `this' keyword) may not
13348              appear in a default argument.  */
13349           saved_local_variables_forbidden_p
13350             = parser->local_variables_forbidden_p;
13351           parser->local_variables_forbidden_p = true;
13352           /* The default argument expression may cause implicitly
13353              defined member functions to be synthesized, which will
13354              result in garbage collection.  We must treat this
13355              situation as if we were within the body of function so as
13356              to avoid collecting live data on the stack.  */
13357           ++function_depth;
13358           /* Parse the assignment-expression.  */
13359           if (template_parm_p)
13360             push_deferring_access_checks (dk_no_deferred);
13361           default_argument
13362             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13363           if (template_parm_p)
13364             pop_deferring_access_checks ();
13365           /* Restore saved state.  */
13366           --function_depth;
13367           parser->greater_than_is_operator_p
13368             = saved_greater_than_is_operator_p;
13369           parser->local_variables_forbidden_p
13370             = saved_local_variables_forbidden_p;
13371         }
13372       if (!parser->default_arg_ok_p)
13373         {
13374           if (!flag_pedantic_errors)
13375             warning (0, "deprecated use of default argument for parameter of non-function");
13376           else
13377             {
13378               error ("default arguments are only permitted for function parameters");
13379               default_argument = NULL_TREE;
13380             }
13381         }
13382     }
13383   else
13384     default_argument = NULL_TREE;
13385
13386   return make_parameter_declarator (&decl_specifiers,
13387                                     declarator,
13388                                     default_argument);
13389 }
13390
13391 /* Parse a function-body.
13392
13393    function-body:
13394      compound_statement  */
13395
13396 static void
13397 cp_parser_function_body (cp_parser *parser)
13398 {
13399   cp_parser_compound_statement (parser, NULL, false);
13400 }
13401
13402 /* Parse a ctor-initializer-opt followed by a function-body.  Return
13403    true if a ctor-initializer was present.  */
13404
13405 static bool
13406 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13407 {
13408   tree body;
13409   bool ctor_initializer_p;
13410
13411   /* Begin the function body.  */
13412   body = begin_function_body ();
13413   /* Parse the optional ctor-initializer.  */
13414   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13415   /* Parse the function-body.  */
13416   cp_parser_function_body (parser);
13417   /* Finish the function body.  */
13418   finish_function_body (body);
13419
13420   return ctor_initializer_p;
13421 }
13422
13423 /* Parse an initializer.
13424
13425    initializer:
13426      = initializer-clause
13427      ( expression-list )
13428
13429    Returns an expression representing the initializer.  If no
13430    initializer is present, NULL_TREE is returned.
13431
13432    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13433    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
13434    set to FALSE if there is no initializer present.  If there is an
13435    initializer, and it is not a constant-expression, *NON_CONSTANT_P
13436    is set to true; otherwise it is set to false.  */
13437
13438 static tree
13439 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13440                        bool* non_constant_p)
13441 {
13442   cp_token *token;
13443   tree init;
13444
13445   /* Peek at the next token.  */
13446   token = cp_lexer_peek_token (parser->lexer);
13447
13448   /* Let our caller know whether or not this initializer was
13449      parenthesized.  */
13450   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13451   /* Assume that the initializer is constant.  */
13452   *non_constant_p = false;
13453
13454   if (token->type == CPP_EQ)
13455     {
13456       /* Consume the `='.  */
13457       cp_lexer_consume_token (parser->lexer);
13458       /* Parse the initializer-clause.  */
13459       init = cp_parser_initializer_clause (parser, non_constant_p);
13460     }
13461   else if (token->type == CPP_OPEN_PAREN)
13462     init = cp_parser_parenthesized_expression_list (parser, false,
13463                                                     /*cast_p=*/false,
13464                                                     /*allow_expansion_p=*/true,
13465                                                     non_constant_p);
13466   else
13467     {
13468       /* Anything else is an error.  */
13469       cp_parser_error (parser, "expected initializer");
13470       init = error_mark_node;
13471     }
13472
13473   return init;
13474 }
13475
13476 /* Parse an initializer-clause.
13477
13478    initializer-clause:
13479      assignment-expression
13480      { initializer-list , [opt] }
13481      { }
13482
13483    Returns an expression representing the initializer.
13484
13485    If the `assignment-expression' production is used the value
13486    returned is simply a representation for the expression.
13487
13488    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
13489    the elements of the initializer-list (or NULL, if the last
13490    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
13491    NULL_TREE.  There is no way to detect whether or not the optional
13492    trailing `,' was provided.  NON_CONSTANT_P is as for
13493    cp_parser_initializer.  */
13494
13495 static tree
13496 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13497 {
13498   tree initializer;
13499
13500   /* Assume the expression is constant.  */
13501   *non_constant_p = false;
13502
13503   /* If it is not a `{', then we are looking at an
13504      assignment-expression.  */
13505   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13506     {
13507       initializer
13508         = cp_parser_constant_expression (parser,
13509                                         /*allow_non_constant_p=*/true,
13510                                         non_constant_p);
13511       if (!*non_constant_p)
13512         initializer = fold_non_dependent_expr (initializer);
13513     }
13514   else
13515     {
13516       /* Consume the `{' token.  */
13517       cp_lexer_consume_token (parser->lexer);
13518       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
13519       initializer = make_node (CONSTRUCTOR);
13520       /* If it's not a `}', then there is a non-trivial initializer.  */
13521       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13522         {
13523           /* Parse the initializer list.  */
13524           CONSTRUCTOR_ELTS (initializer)
13525             = cp_parser_initializer_list (parser, non_constant_p);
13526           /* A trailing `,' token is allowed.  */
13527           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13528             cp_lexer_consume_token (parser->lexer);
13529         }
13530       /* Now, there should be a trailing `}'.  */
13531       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13532     }
13533
13534   return initializer;
13535 }
13536
13537 /* Parse an initializer-list.
13538
13539    initializer-list:
13540      initializer-clause ... [opt]
13541      initializer-list , initializer-clause ... [opt]
13542
13543    GNU Extension:
13544
13545    initializer-list:
13546      identifier : initializer-clause
13547      initializer-list, identifier : initializer-clause
13548
13549    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
13550    for the initializer.  If the INDEX of the elt is non-NULL, it is the
13551    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
13552    as for cp_parser_initializer.  */
13553
13554 static VEC(constructor_elt,gc) *
13555 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13556 {
13557   VEC(constructor_elt,gc) *v = NULL;
13558
13559   /* Assume all of the expressions are constant.  */
13560   *non_constant_p = false;
13561
13562   /* Parse the rest of the list.  */
13563   while (true)
13564     {
13565       cp_token *token;
13566       tree identifier;
13567       tree initializer;
13568       bool clause_non_constant_p;
13569
13570       /* If the next token is an identifier and the following one is a
13571          colon, we are looking at the GNU designated-initializer
13572          syntax.  */
13573       if (cp_parser_allow_gnu_extensions_p (parser)
13574           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13575           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13576         {
13577           /* Warn the user that they are using an extension.  */
13578           if (pedantic)
13579             pedwarn ("ISO C++ does not allow designated initializers");
13580           /* Consume the identifier.  */
13581           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13582           /* Consume the `:'.  */
13583           cp_lexer_consume_token (parser->lexer);
13584         }
13585       else
13586         identifier = NULL_TREE;
13587
13588       /* Parse the initializer.  */
13589       initializer = cp_parser_initializer_clause (parser,
13590                                                   &clause_non_constant_p);
13591       /* If any clause is non-constant, so is the entire initializer.  */
13592       if (clause_non_constant_p)
13593         *non_constant_p = true;
13594
13595       /* If we have an ellipsis, this is an initializer pack
13596          expansion.  */
13597       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13598         {
13599           /* Consume the `...'.  */
13600           cp_lexer_consume_token (parser->lexer);
13601
13602           /* Turn the initializer into an initializer expansion.  */
13603           initializer = make_pack_expansion (initializer);
13604         }
13605
13606       /* Add it to the vector.  */
13607       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13608
13609       /* If the next token is not a comma, we have reached the end of
13610          the list.  */
13611       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13612         break;
13613
13614       /* Peek at the next token.  */
13615       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13616       /* If the next token is a `}', then we're still done.  An
13617          initializer-clause can have a trailing `,' after the
13618          initializer-list and before the closing `}'.  */
13619       if (token->type == CPP_CLOSE_BRACE)
13620         break;
13621
13622       /* Consume the `,' token.  */
13623       cp_lexer_consume_token (parser->lexer);
13624     }
13625
13626   return v;
13627 }
13628
13629 /* Classes [gram.class] */
13630
13631 /* Parse a class-name.
13632
13633    class-name:
13634      identifier
13635      template-id
13636
13637    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13638    to indicate that names looked up in dependent types should be
13639    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
13640    keyword has been used to indicate that the name that appears next
13641    is a template.  TAG_TYPE indicates the explicit tag given before
13642    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
13643    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
13644    is the class being defined in a class-head.
13645
13646    Returns the TYPE_DECL representing the class.  */
13647
13648 static tree
13649 cp_parser_class_name (cp_parser *parser,
13650                       bool typename_keyword_p,
13651                       bool template_keyword_p,
13652                       enum tag_types tag_type,
13653                       bool check_dependency_p,
13654                       bool class_head_p,
13655                       bool is_declaration)
13656 {
13657   tree decl;
13658   tree scope;
13659   bool typename_p;
13660   cp_token *token;
13661
13662   /* All class-names start with an identifier.  */
13663   token = cp_lexer_peek_token (parser->lexer);
13664   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13665     {
13666       cp_parser_error (parser, "expected class-name");
13667       return error_mark_node;
13668     }
13669
13670   /* PARSER->SCOPE can be cleared when parsing the template-arguments
13671      to a template-id, so we save it here.  */
13672   scope = parser->scope;
13673   if (scope == error_mark_node)
13674     return error_mark_node;
13675
13676   /* Any name names a type if we're following the `typename' keyword
13677      in a qualified name where the enclosing scope is type-dependent.  */
13678   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13679                 && dependent_type_p (scope));
13680   /* Handle the common case (an identifier, but not a template-id)
13681      efficiently.  */
13682   if (token->type == CPP_NAME
13683       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13684     {
13685       cp_token *identifier_token;
13686       tree identifier;
13687       bool ambiguous_p;
13688
13689       /* Look for the identifier.  */
13690       identifier_token = cp_lexer_peek_token (parser->lexer);
13691       ambiguous_p = identifier_token->ambiguous_p;
13692       identifier = cp_parser_identifier (parser);
13693       /* If the next token isn't an identifier, we are certainly not
13694          looking at a class-name.  */
13695       if (identifier == error_mark_node)
13696         decl = error_mark_node;
13697       /* If we know this is a type-name, there's no need to look it
13698          up.  */
13699       else if (typename_p)
13700         decl = identifier;
13701       else
13702         {
13703           tree ambiguous_decls;
13704           /* If we already know that this lookup is ambiguous, then
13705              we've already issued an error message; there's no reason
13706              to check again.  */
13707           if (ambiguous_p)
13708             {
13709               cp_parser_simulate_error (parser);
13710               return error_mark_node;
13711             }
13712           /* If the next token is a `::', then the name must be a type
13713              name.
13714
13715              [basic.lookup.qual]
13716
13717              During the lookup for a name preceding the :: scope
13718              resolution operator, object, function, and enumerator
13719              names are ignored.  */
13720           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13721             tag_type = typename_type;
13722           /* Look up the name.  */
13723           decl = cp_parser_lookup_name (parser, identifier,
13724                                         tag_type,
13725                                         /*is_template=*/false,
13726                                         /*is_namespace=*/false,
13727                                         check_dependency_p,
13728                                         &ambiguous_decls);
13729           if (ambiguous_decls)
13730             {
13731               error ("reference to %qD is ambiguous", identifier);
13732               print_candidates (ambiguous_decls);
13733               if (cp_parser_parsing_tentatively (parser))
13734                 {
13735                   identifier_token->ambiguous_p = true;
13736                   cp_parser_simulate_error (parser);
13737                 }
13738               return error_mark_node;
13739             }
13740         }
13741     }
13742   else
13743     {
13744       /* Try a template-id.  */
13745       decl = cp_parser_template_id (parser, template_keyword_p,
13746                                     check_dependency_p,
13747                                     is_declaration);
13748       if (decl == error_mark_node)
13749         return error_mark_node;
13750     }
13751
13752   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13753
13754   /* If this is a typename, create a TYPENAME_TYPE.  */
13755   if (typename_p && decl != error_mark_node)
13756     {
13757       decl = make_typename_type (scope, decl, typename_type,
13758                                  /*complain=*/tf_error);
13759       if (decl != error_mark_node)
13760         decl = TYPE_NAME (decl);
13761     }
13762
13763   /* Check to see that it is really the name of a class.  */
13764   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13765       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13766       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13767     /* Situations like this:
13768
13769          template <typename T> struct A {
13770            typename T::template X<int>::I i;
13771          };
13772
13773        are problematic.  Is `T::template X<int>' a class-name?  The
13774        standard does not seem to be definitive, but there is no other
13775        valid interpretation of the following `::'.  Therefore, those
13776        names are considered class-names.  */
13777     {
13778       decl = make_typename_type (scope, decl, tag_type, tf_error);
13779       if (decl != error_mark_node)
13780         decl = TYPE_NAME (decl);
13781     }
13782   else if (TREE_CODE (decl) != TYPE_DECL
13783            || TREE_TYPE (decl) == error_mark_node
13784            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13785     decl = error_mark_node;
13786
13787   if (decl == error_mark_node)
13788     cp_parser_error (parser, "expected class-name");
13789
13790   return decl;
13791 }
13792
13793 /* Parse a class-specifier.
13794
13795    class-specifier:
13796      class-head { member-specification [opt] }
13797
13798    Returns the TREE_TYPE representing the class.  */
13799
13800 static tree
13801 cp_parser_class_specifier (cp_parser* parser)
13802 {
13803   cp_token *token;
13804   tree type;
13805   tree attributes = NULL_TREE;
13806   int has_trailing_semicolon;
13807   bool nested_name_specifier_p;
13808   unsigned saved_num_template_parameter_lists;
13809   bool saved_in_function_body;
13810   tree old_scope = NULL_TREE;
13811   tree scope = NULL_TREE;
13812   tree bases;
13813
13814   push_deferring_access_checks (dk_no_deferred);
13815
13816   /* Parse the class-head.  */
13817   type = cp_parser_class_head (parser,
13818                                &nested_name_specifier_p,
13819                                &attributes,
13820                                &bases);
13821   /* If the class-head was a semantic disaster, skip the entire body
13822      of the class.  */
13823   if (!type)
13824     {
13825       cp_parser_skip_to_end_of_block_or_statement (parser);
13826       pop_deferring_access_checks ();
13827       return error_mark_node;
13828     }
13829
13830   /* Look for the `{'.  */
13831   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13832     {
13833       pop_deferring_access_checks ();
13834       return error_mark_node;
13835     }
13836
13837   /* Process the base classes. If they're invalid, skip the 
13838      entire class body.  */
13839   if (!xref_basetypes (type, bases))
13840     {
13841       /* Consuming the closing brace yields better error messages
13842          later on.  */
13843       if (cp_parser_skip_to_closing_brace (parser))
13844         cp_lexer_consume_token (parser->lexer);
13845       pop_deferring_access_checks ();
13846       return error_mark_node;
13847     }
13848
13849   /* Issue an error message if type-definitions are forbidden here.  */
13850   cp_parser_check_type_definition (parser);
13851   /* Remember that we are defining one more class.  */
13852   ++parser->num_classes_being_defined;
13853   /* Inside the class, surrounding template-parameter-lists do not
13854      apply.  */
13855   saved_num_template_parameter_lists
13856     = parser->num_template_parameter_lists;
13857   parser->num_template_parameter_lists = 0;
13858   /* We are not in a function body.  */
13859   saved_in_function_body = parser->in_function_body;
13860   parser->in_function_body = false;
13861
13862   /* Start the class.  */
13863   if (nested_name_specifier_p)
13864     {
13865       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13866       old_scope = push_inner_scope (scope);
13867     }
13868   type = begin_class_definition (type, attributes);
13869
13870   if (type == error_mark_node)
13871     /* If the type is erroneous, skip the entire body of the class.  */
13872     cp_parser_skip_to_closing_brace (parser);
13873   else
13874     /* Parse the member-specification.  */
13875     cp_parser_member_specification_opt (parser);
13876
13877   /* Look for the trailing `}'.  */
13878   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13879   /* We get better error messages by noticing a common problem: a
13880      missing trailing `;'.  */
13881   token = cp_lexer_peek_token (parser->lexer);
13882   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13883   /* Look for trailing attributes to apply to this class.  */
13884   if (cp_parser_allow_gnu_extensions_p (parser))
13885     attributes = cp_parser_attributes_opt (parser);
13886   if (type != error_mark_node)
13887     type = finish_struct (type, attributes);
13888   if (nested_name_specifier_p)
13889     pop_inner_scope (old_scope, scope);
13890   /* If this class is not itself within the scope of another class,
13891      then we need to parse the bodies of all of the queued function
13892      definitions.  Note that the queued functions defined in a class
13893      are not always processed immediately following the
13894      class-specifier for that class.  Consider:
13895
13896        struct A {
13897          struct B { void f() { sizeof (A); } };
13898        };
13899
13900      If `f' were processed before the processing of `A' were
13901      completed, there would be no way to compute the size of `A'.
13902      Note that the nesting we are interested in here is lexical --
13903      not the semantic nesting given by TYPE_CONTEXT.  In particular,
13904      for:
13905
13906        struct A { struct B; };
13907        struct A::B { void f() { } };
13908
13909      there is no need to delay the parsing of `A::B::f'.  */
13910   if (--parser->num_classes_being_defined == 0)
13911     {
13912       tree queue_entry;
13913       tree fn;
13914       tree class_type = NULL_TREE;
13915       tree pushed_scope = NULL_TREE;
13916
13917       /* In a first pass, parse default arguments to the functions.
13918          Then, in a second pass, parse the bodies of the functions.
13919          This two-phased approach handles cases like:
13920
13921             struct S {
13922               void f() { g(); }
13923               void g(int i = 3);
13924             };
13925
13926          */
13927       for (TREE_PURPOSE (parser->unparsed_functions_queues)
13928              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13929            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13930            TREE_PURPOSE (parser->unparsed_functions_queues)
13931              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13932         {
13933           fn = TREE_VALUE (queue_entry);
13934           /* If there are default arguments that have not yet been processed,
13935              take care of them now.  */
13936           if (class_type != TREE_PURPOSE (queue_entry))
13937             {
13938               if (pushed_scope)
13939                 pop_scope (pushed_scope);
13940               class_type = TREE_PURPOSE (queue_entry);
13941               pushed_scope = push_scope (class_type);
13942             }
13943           /* Make sure that any template parameters are in scope.  */
13944           maybe_begin_member_template_processing (fn);
13945           /* Parse the default argument expressions.  */
13946           cp_parser_late_parsing_default_args (parser, fn);
13947           /* Remove any template parameters from the symbol table.  */
13948           maybe_end_member_template_processing ();
13949         }
13950       if (pushed_scope)
13951         pop_scope (pushed_scope);
13952       /* Now parse the body of the functions.  */
13953       for (TREE_VALUE (parser->unparsed_functions_queues)
13954              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13955            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13956            TREE_VALUE (parser->unparsed_functions_queues)
13957              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13958         {
13959           /* Figure out which function we need to process.  */
13960           fn = TREE_VALUE (queue_entry);
13961           /* Parse the function.  */
13962           cp_parser_late_parsing_for_member (parser, fn);
13963         }
13964     }
13965
13966   /* Put back any saved access checks.  */
13967   pop_deferring_access_checks ();
13968
13969   /* Restore saved state.  */
13970   parser->in_function_body = saved_in_function_body;
13971   parser->num_template_parameter_lists
13972     = saved_num_template_parameter_lists;
13973
13974   return type;
13975 }
13976
13977 /* Parse a class-head.
13978
13979    class-head:
13980      class-key identifier [opt] base-clause [opt]
13981      class-key nested-name-specifier identifier base-clause [opt]
13982      class-key nested-name-specifier [opt] template-id
13983        base-clause [opt]
13984
13985    GNU Extensions:
13986      class-key attributes identifier [opt] base-clause [opt]
13987      class-key attributes nested-name-specifier identifier base-clause [opt]
13988      class-key attributes nested-name-specifier [opt] template-id
13989        base-clause [opt]
13990
13991    Upon return BASES is initialized to the list of base classes (or
13992    NULL, if there are none) in the same form returned by
13993    cp_parser_base_clause.
13994
13995    Returns the TYPE of the indicated class.  Sets
13996    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13997    involving a nested-name-specifier was used, and FALSE otherwise.
13998
13999    Returns error_mark_node if this is not a class-head.
14000
14001    Returns NULL_TREE if the class-head is syntactically valid, but
14002    semantically invalid in a way that means we should skip the entire
14003    body of the class.  */
14004
14005 static tree
14006 cp_parser_class_head (cp_parser* parser,
14007                       bool* nested_name_specifier_p,
14008                       tree *attributes_p,
14009                       tree *bases)
14010 {
14011   tree nested_name_specifier;
14012   enum tag_types class_key;
14013   tree id = NULL_TREE;
14014   tree type = NULL_TREE;
14015   tree attributes;
14016   bool template_id_p = false;
14017   bool qualified_p = false;
14018   bool invalid_nested_name_p = false;
14019   bool invalid_explicit_specialization_p = false;
14020   tree pushed_scope = NULL_TREE;
14021   unsigned num_templates;
14022
14023   /* Assume no nested-name-specifier will be present.  */
14024   *nested_name_specifier_p = false;
14025   /* Assume no template parameter lists will be used in defining the
14026      type.  */
14027   num_templates = 0;
14028
14029   *bases = NULL_TREE;
14030
14031   /* Look for the class-key.  */
14032   class_key = cp_parser_class_key (parser);
14033   if (class_key == none_type)
14034     return error_mark_node;
14035
14036   /* Parse the attributes.  */
14037   attributes = cp_parser_attributes_opt (parser);
14038
14039   /* If the next token is `::', that is invalid -- but sometimes
14040      people do try to write:
14041
14042        struct ::S {};
14043
14044      Handle this gracefully by accepting the extra qualifier, and then
14045      issuing an error about it later if this really is a
14046      class-head.  If it turns out just to be an elaborated type
14047      specifier, remain silent.  */
14048   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14049     qualified_p = true;
14050
14051   push_deferring_access_checks (dk_no_check);
14052
14053   /* Determine the name of the class.  Begin by looking for an
14054      optional nested-name-specifier.  */
14055   nested_name_specifier
14056     = cp_parser_nested_name_specifier_opt (parser,
14057                                            /*typename_keyword_p=*/false,
14058                                            /*check_dependency_p=*/false,
14059                                            /*type_p=*/false,
14060                                            /*is_declaration=*/false);
14061   /* If there was a nested-name-specifier, then there *must* be an
14062      identifier.  */
14063   if (nested_name_specifier)
14064     {
14065       /* Although the grammar says `identifier', it really means
14066          `class-name' or `template-name'.  You are only allowed to
14067          define a class that has already been declared with this
14068          syntax.
14069
14070          The proposed resolution for Core Issue 180 says that wherever
14071          you see `class T::X' you should treat `X' as a type-name.
14072
14073          It is OK to define an inaccessible class; for example:
14074
14075            class A { class B; };
14076            class A::B {};
14077
14078          We do not know if we will see a class-name, or a
14079          template-name.  We look for a class-name first, in case the
14080          class-name is a template-id; if we looked for the
14081          template-name first we would stop after the template-name.  */
14082       cp_parser_parse_tentatively (parser);
14083       type = cp_parser_class_name (parser,
14084                                    /*typename_keyword_p=*/false,
14085                                    /*template_keyword_p=*/false,
14086                                    class_type,
14087                                    /*check_dependency_p=*/false,
14088                                    /*class_head_p=*/true,
14089                                    /*is_declaration=*/false);
14090       /* If that didn't work, ignore the nested-name-specifier.  */
14091       if (!cp_parser_parse_definitely (parser))
14092         {
14093           invalid_nested_name_p = true;
14094           id = cp_parser_identifier (parser);
14095           if (id == error_mark_node)
14096             id = NULL_TREE;
14097         }
14098       /* If we could not find a corresponding TYPE, treat this
14099          declaration like an unqualified declaration.  */
14100       if (type == error_mark_node)
14101         nested_name_specifier = NULL_TREE;
14102       /* Otherwise, count the number of templates used in TYPE and its
14103          containing scopes.  */
14104       else
14105         {
14106           tree scope;
14107
14108           for (scope = TREE_TYPE (type);
14109                scope && TREE_CODE (scope) != NAMESPACE_DECL;
14110                scope = (TYPE_P (scope)
14111                         ? TYPE_CONTEXT (scope)
14112                         : DECL_CONTEXT (scope)))
14113             if (TYPE_P (scope)
14114                 && CLASS_TYPE_P (scope)
14115                 && CLASSTYPE_TEMPLATE_INFO (scope)
14116                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14117                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14118               ++num_templates;
14119         }
14120     }
14121   /* Otherwise, the identifier is optional.  */
14122   else
14123     {
14124       /* We don't know whether what comes next is a template-id,
14125          an identifier, or nothing at all.  */
14126       cp_parser_parse_tentatively (parser);
14127       /* Check for a template-id.  */
14128       id = cp_parser_template_id (parser,
14129                                   /*template_keyword_p=*/false,
14130                                   /*check_dependency_p=*/true,
14131                                   /*is_declaration=*/true);
14132       /* If that didn't work, it could still be an identifier.  */
14133       if (!cp_parser_parse_definitely (parser))
14134         {
14135           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14136             id = cp_parser_identifier (parser);
14137           else
14138             id = NULL_TREE;
14139         }
14140       else
14141         {
14142           template_id_p = true;
14143           ++num_templates;
14144         }
14145     }
14146
14147   pop_deferring_access_checks ();
14148
14149   if (id)
14150     cp_parser_check_for_invalid_template_id (parser, id);
14151
14152   /* If it's not a `:' or a `{' then we can't really be looking at a
14153      class-head, since a class-head only appears as part of a
14154      class-specifier.  We have to detect this situation before calling
14155      xref_tag, since that has irreversible side-effects.  */
14156   if (!cp_parser_next_token_starts_class_definition_p (parser))
14157     {
14158       cp_parser_error (parser, "expected %<{%> or %<:%>");
14159       return error_mark_node;
14160     }
14161
14162   /* At this point, we're going ahead with the class-specifier, even
14163      if some other problem occurs.  */
14164   cp_parser_commit_to_tentative_parse (parser);
14165   /* Issue the error about the overly-qualified name now.  */
14166   if (qualified_p)
14167     cp_parser_error (parser,
14168                      "global qualification of class name is invalid");
14169   else if (invalid_nested_name_p)
14170     cp_parser_error (parser,
14171                      "qualified name does not name a class");
14172   else if (nested_name_specifier)
14173     {
14174       tree scope;
14175
14176       /* Reject typedef-names in class heads.  */
14177       if (!DECL_IMPLICIT_TYPEDEF_P (type))
14178         {
14179           error ("invalid class name in declaration of %qD", type);
14180           type = NULL_TREE;
14181           goto done;
14182         }
14183
14184       /* Figure out in what scope the declaration is being placed.  */
14185       scope = current_scope ();
14186       /* If that scope does not contain the scope in which the
14187          class was originally declared, the program is invalid.  */
14188       if (scope && !is_ancestor (scope, nested_name_specifier))
14189         {
14190           error ("declaration of %qD in %qD which does not enclose %qD",
14191                  type, scope, nested_name_specifier);
14192           type = NULL_TREE;
14193           goto done;
14194         }
14195       /* [dcl.meaning]
14196
14197          A declarator-id shall not be qualified exception of the
14198          definition of a ... nested class outside of its class
14199          ... [or] a the definition or explicit instantiation of a
14200          class member of a namespace outside of its namespace.  */
14201       if (scope == nested_name_specifier)
14202         {
14203           pedwarn ("extra qualification ignored");
14204           nested_name_specifier = NULL_TREE;
14205           num_templates = 0;
14206         }
14207     }
14208   /* An explicit-specialization must be preceded by "template <>".  If
14209      it is not, try to recover gracefully.  */
14210   if (at_namespace_scope_p ()
14211       && parser->num_template_parameter_lists == 0
14212       && template_id_p)
14213     {
14214       error ("an explicit specialization must be preceded by %<template <>%>");
14215       invalid_explicit_specialization_p = true;
14216       /* Take the same action that would have been taken by
14217          cp_parser_explicit_specialization.  */
14218       ++parser->num_template_parameter_lists;
14219       begin_specialization ();
14220     }
14221   /* There must be no "return" statements between this point and the
14222      end of this function; set "type "to the correct return value and
14223      use "goto done;" to return.  */
14224   /* Make sure that the right number of template parameters were
14225      present.  */
14226   if (!cp_parser_check_template_parameters (parser, num_templates))
14227     {
14228       /* If something went wrong, there is no point in even trying to
14229          process the class-definition.  */
14230       type = NULL_TREE;
14231       goto done;
14232     }
14233
14234   /* Look up the type.  */
14235   if (template_id_p)
14236     {
14237       type = TREE_TYPE (id);
14238       type = maybe_process_partial_specialization (type);
14239       if (nested_name_specifier)
14240         pushed_scope = push_scope (nested_name_specifier);
14241     }
14242   else if (nested_name_specifier)
14243     {
14244       tree class_type;
14245
14246       /* Given:
14247
14248             template <typename T> struct S { struct T };
14249             template <typename T> struct S<T>::T { };
14250
14251          we will get a TYPENAME_TYPE when processing the definition of
14252          `S::T'.  We need to resolve it to the actual type before we
14253          try to define it.  */
14254       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14255         {
14256           class_type = resolve_typename_type (TREE_TYPE (type),
14257                                               /*only_current_p=*/false);
14258           if (class_type != error_mark_node)
14259             type = TYPE_NAME (class_type);
14260           else
14261             {
14262               cp_parser_error (parser, "could not resolve typename type");
14263               type = error_mark_node;
14264             }
14265         }
14266
14267       maybe_process_partial_specialization (TREE_TYPE (type));
14268       class_type = current_class_type;
14269       /* Enter the scope indicated by the nested-name-specifier.  */
14270       pushed_scope = push_scope (nested_name_specifier);
14271       /* Get the canonical version of this type.  */
14272       type = TYPE_MAIN_DECL (TREE_TYPE (type));
14273       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14274           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14275         {
14276           type = push_template_decl (type);
14277           if (type == error_mark_node)
14278             {
14279               type = NULL_TREE;
14280               goto done;
14281             }
14282         }
14283
14284       type = TREE_TYPE (type);
14285       *nested_name_specifier_p = true;
14286     }
14287   else      /* The name is not a nested name.  */
14288     {
14289       /* If the class was unnamed, create a dummy name.  */
14290       if (!id)
14291         id = make_anon_name ();
14292       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14293                        parser->num_template_parameter_lists);
14294     }
14295
14296   /* Indicate whether this class was declared as a `class' or as a
14297      `struct'.  */
14298   if (TREE_CODE (type) == RECORD_TYPE)
14299     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14300   cp_parser_check_class_key (class_key, type);
14301
14302   /* If this type was already complete, and we see another definition,
14303      that's an error.  */
14304   if (type != error_mark_node && COMPLETE_TYPE_P (type))
14305     {
14306       error ("redefinition of %q#T", type);
14307       error ("previous definition of %q+#T", type);
14308       type = NULL_TREE;
14309       goto done;
14310     }
14311   else if (type == error_mark_node)
14312     type = NULL_TREE;
14313
14314   /* We will have entered the scope containing the class; the names of
14315      base classes should be looked up in that context.  For example:
14316
14317        struct A { struct B {}; struct C; };
14318        struct A::C : B {};
14319
14320      is valid.  */
14321
14322   /* Get the list of base-classes, if there is one.  */
14323   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14324     *bases = cp_parser_base_clause (parser);
14325
14326  done:
14327   /* Leave the scope given by the nested-name-specifier.  We will
14328      enter the class scope itself while processing the members.  */
14329   if (pushed_scope)
14330     pop_scope (pushed_scope);
14331
14332   if (invalid_explicit_specialization_p)
14333     {
14334       end_specialization ();
14335       --parser->num_template_parameter_lists;
14336     }
14337   *attributes_p = attributes;
14338   return type;
14339 }
14340
14341 /* Parse a class-key.
14342
14343    class-key:
14344      class
14345      struct
14346      union
14347
14348    Returns the kind of class-key specified, or none_type to indicate
14349    error.  */
14350
14351 static enum tag_types
14352 cp_parser_class_key (cp_parser* parser)
14353 {
14354   cp_token *token;
14355   enum tag_types tag_type;
14356
14357   /* Look for the class-key.  */
14358   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14359   if (!token)
14360     return none_type;
14361
14362   /* Check to see if the TOKEN is a class-key.  */
14363   tag_type = cp_parser_token_is_class_key (token);
14364   if (!tag_type)
14365     cp_parser_error (parser, "expected class-key");
14366   return tag_type;
14367 }
14368
14369 /* Parse an (optional) member-specification.
14370
14371    member-specification:
14372      member-declaration member-specification [opt]
14373      access-specifier : member-specification [opt]  */
14374
14375 static void
14376 cp_parser_member_specification_opt (cp_parser* parser)
14377 {
14378   while (true)
14379     {
14380       cp_token *token;
14381       enum rid keyword;
14382
14383       /* Peek at the next token.  */
14384       token = cp_lexer_peek_token (parser->lexer);
14385       /* If it's a `}', or EOF then we've seen all the members.  */
14386       if (token->type == CPP_CLOSE_BRACE
14387           || token->type == CPP_EOF
14388           || token->type == CPP_PRAGMA_EOL)
14389         break;
14390
14391       /* See if this token is a keyword.  */
14392       keyword = token->keyword;
14393       switch (keyword)
14394         {
14395         case RID_PUBLIC:
14396         case RID_PROTECTED:
14397         case RID_PRIVATE:
14398           /* Consume the access-specifier.  */
14399           cp_lexer_consume_token (parser->lexer);
14400           /* Remember which access-specifier is active.  */
14401           current_access_specifier = token->u.value;
14402           /* Look for the `:'.  */
14403           cp_parser_require (parser, CPP_COLON, "`:'");
14404           break;
14405
14406         default:
14407           /* Accept #pragmas at class scope.  */
14408           if (token->type == CPP_PRAGMA)
14409             {
14410               cp_parser_pragma (parser, pragma_external);
14411               break;
14412             }
14413
14414           /* Otherwise, the next construction must be a
14415              member-declaration.  */
14416           cp_parser_member_declaration (parser);
14417         }
14418     }
14419 }
14420
14421 /* Parse a member-declaration.
14422
14423    member-declaration:
14424      decl-specifier-seq [opt] member-declarator-list [opt] ;
14425      function-definition ; [opt]
14426      :: [opt] nested-name-specifier template [opt] unqualified-id ;
14427      using-declaration
14428      template-declaration
14429
14430    member-declarator-list:
14431      member-declarator
14432      member-declarator-list , member-declarator
14433
14434    member-declarator:
14435      declarator pure-specifier [opt]
14436      declarator constant-initializer [opt]
14437      identifier [opt] : constant-expression
14438
14439    GNU Extensions:
14440
14441    member-declaration:
14442      __extension__ member-declaration
14443
14444    member-declarator:
14445      declarator attributes [opt] pure-specifier [opt]
14446      declarator attributes [opt] constant-initializer [opt]
14447      identifier [opt] attributes [opt] : constant-expression  
14448
14449    C++0x Extensions:
14450
14451    member-declaration:
14452      static_assert-declaration  */
14453
14454 static void
14455 cp_parser_member_declaration (cp_parser* parser)
14456 {
14457   cp_decl_specifier_seq decl_specifiers;
14458   tree prefix_attributes;
14459   tree decl;
14460   int declares_class_or_enum;
14461   bool friend_p;
14462   cp_token *token;
14463   int saved_pedantic;
14464
14465   /* Check for the `__extension__' keyword.  */
14466   if (cp_parser_extension_opt (parser, &saved_pedantic))
14467     {
14468       /* Recurse.  */
14469       cp_parser_member_declaration (parser);
14470       /* Restore the old value of the PEDANTIC flag.  */
14471       pedantic = saved_pedantic;
14472
14473       return;
14474     }
14475
14476   /* Check for a template-declaration.  */
14477   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14478     {
14479       /* An explicit specialization here is an error condition, and we
14480          expect the specialization handler to detect and report this.  */
14481       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14482           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14483         cp_parser_explicit_specialization (parser);
14484       else
14485         cp_parser_template_declaration (parser, /*member_p=*/true);
14486
14487       return;
14488     }
14489
14490   /* Check for a using-declaration.  */
14491   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14492     {
14493       /* Parse the using-declaration.  */
14494       cp_parser_using_declaration (parser,
14495                                    /*access_declaration_p=*/false);
14496       return;
14497     }
14498
14499   /* Check for @defs.  */
14500   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14501     {
14502       tree ivar, member;
14503       tree ivar_chains = cp_parser_objc_defs_expression (parser);
14504       ivar = ivar_chains;
14505       while (ivar)
14506         {
14507           member = ivar;
14508           ivar = TREE_CHAIN (member);
14509           TREE_CHAIN (member) = NULL_TREE;
14510           finish_member_declaration (member);
14511         }
14512       return;
14513     }
14514
14515   /* If the next token is `static_assert' we have a static assertion.  */
14516   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14517     {
14518       cp_parser_static_assert (parser, /*member_p=*/true);
14519       return;
14520     }
14521
14522   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14523     return;
14524
14525   /* Parse the decl-specifier-seq.  */
14526   cp_parser_decl_specifier_seq (parser,
14527                                 CP_PARSER_FLAGS_OPTIONAL,
14528                                 &decl_specifiers,
14529                                 &declares_class_or_enum);
14530   prefix_attributes = decl_specifiers.attributes;
14531   decl_specifiers.attributes = NULL_TREE;
14532   /* Check for an invalid type-name.  */
14533   if (!decl_specifiers.type
14534       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14535     return;
14536   /* If there is no declarator, then the decl-specifier-seq should
14537      specify a type.  */
14538   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14539     {
14540       /* If there was no decl-specifier-seq, and the next token is a
14541          `;', then we have something like:
14542
14543            struct S { ; };
14544
14545          [class.mem]
14546
14547          Each member-declaration shall declare at least one member
14548          name of the class.  */
14549       if (!decl_specifiers.any_specifiers_p)
14550         {
14551           cp_token *token = cp_lexer_peek_token (parser->lexer);
14552           if (pedantic && !token->in_system_header)
14553             pedwarn ("%Hextra %<;%>", &token->location);
14554         }
14555       else
14556         {
14557           tree type;
14558
14559           /* See if this declaration is a friend.  */
14560           friend_p = cp_parser_friend_p (&decl_specifiers);
14561           /* If there were decl-specifiers, check to see if there was
14562              a class-declaration.  */
14563           type = check_tag_decl (&decl_specifiers);
14564           /* Nested classes have already been added to the class, but
14565              a `friend' needs to be explicitly registered.  */
14566           if (friend_p)
14567             {
14568               /* If the `friend' keyword was present, the friend must
14569                  be introduced with a class-key.  */
14570                if (!declares_class_or_enum)
14571                  error ("a class-key must be used when declaring a friend");
14572                /* In this case:
14573
14574                     template <typename T> struct A {
14575                       friend struct A<T>::B;
14576                     };
14577
14578                   A<T>::B will be represented by a TYPENAME_TYPE, and
14579                   therefore not recognized by check_tag_decl.  */
14580                if (!type
14581                    && decl_specifiers.type
14582                    && TYPE_P (decl_specifiers.type))
14583                  type = decl_specifiers.type;
14584                if (!type || !TYPE_P (type))
14585                  error ("friend declaration does not name a class or "
14586                         "function");
14587                else
14588                  make_friend_class (current_class_type, type,
14589                                     /*complain=*/true);
14590             }
14591           /* If there is no TYPE, an error message will already have
14592              been issued.  */
14593           else if (!type || type == error_mark_node)
14594             ;
14595           /* An anonymous aggregate has to be handled specially; such
14596              a declaration really declares a data member (with a
14597              particular type), as opposed to a nested class.  */
14598           else if (ANON_AGGR_TYPE_P (type))
14599             {
14600               /* Remove constructors and such from TYPE, now that we
14601                  know it is an anonymous aggregate.  */
14602               fixup_anonymous_aggr (type);
14603               /* And make the corresponding data member.  */
14604               decl = build_decl (FIELD_DECL, NULL_TREE, type);
14605               /* Add it to the class.  */
14606               finish_member_declaration (decl);
14607             }
14608           else
14609             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14610         }
14611     }
14612   else
14613     {
14614       /* See if these declarations will be friends.  */
14615       friend_p = cp_parser_friend_p (&decl_specifiers);
14616
14617       /* Keep going until we hit the `;' at the end of the
14618          declaration.  */
14619       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14620         {
14621           tree attributes = NULL_TREE;
14622           tree first_attribute;
14623
14624           /* Peek at the next token.  */
14625           token = cp_lexer_peek_token (parser->lexer);
14626
14627           /* Check for a bitfield declaration.  */
14628           if (token->type == CPP_COLON
14629               || (token->type == CPP_NAME
14630                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14631                   == CPP_COLON))
14632             {
14633               tree identifier;
14634               tree width;
14635
14636               /* Get the name of the bitfield.  Note that we cannot just
14637                  check TOKEN here because it may have been invalidated by
14638                  the call to cp_lexer_peek_nth_token above.  */
14639               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14640                 identifier = cp_parser_identifier (parser);
14641               else
14642                 identifier = NULL_TREE;
14643
14644               /* Consume the `:' token.  */
14645               cp_lexer_consume_token (parser->lexer);
14646               /* Get the width of the bitfield.  */
14647               width
14648                 = cp_parser_constant_expression (parser,
14649                                                  /*allow_non_constant=*/false,
14650                                                  NULL);
14651
14652               /* Look for attributes that apply to the bitfield.  */
14653               attributes = cp_parser_attributes_opt (parser);
14654               /* Remember which attributes are prefix attributes and
14655                  which are not.  */
14656               first_attribute = attributes;
14657               /* Combine the attributes.  */
14658               attributes = chainon (prefix_attributes, attributes);
14659
14660               /* Create the bitfield declaration.  */
14661               decl = grokbitfield (identifier
14662                                    ? make_id_declarator (NULL_TREE,
14663                                                          identifier,
14664                                                          sfk_none)
14665                                    : NULL,
14666                                    &decl_specifiers,
14667                                    width);
14668               /* Apply the attributes.  */
14669               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14670             }
14671           else
14672             {
14673               cp_declarator *declarator;
14674               tree initializer;
14675               tree asm_specification;
14676               int ctor_dtor_or_conv_p;
14677
14678               /* Parse the declarator.  */
14679               declarator
14680                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14681                                         &ctor_dtor_or_conv_p,
14682                                         /*parenthesized_p=*/NULL,
14683                                         /*member_p=*/true);
14684
14685               /* If something went wrong parsing the declarator, make sure
14686                  that we at least consume some tokens.  */
14687               if (declarator == cp_error_declarator)
14688                 {
14689                   /* Skip to the end of the statement.  */
14690                   cp_parser_skip_to_end_of_statement (parser);
14691                   /* If the next token is not a semicolon, that is
14692                      probably because we just skipped over the body of
14693                      a function.  So, we consume a semicolon if
14694                      present, but do not issue an error message if it
14695                      is not present.  */
14696                   if (cp_lexer_next_token_is (parser->lexer,
14697                                               CPP_SEMICOLON))
14698                     cp_lexer_consume_token (parser->lexer);
14699                   return;
14700                 }
14701
14702               if (declares_class_or_enum & 2)
14703                 cp_parser_check_for_definition_in_return_type
14704                   (declarator, decl_specifiers.type);
14705
14706               /* Look for an asm-specification.  */
14707               asm_specification = cp_parser_asm_specification_opt (parser);
14708               /* Look for attributes that apply to the declaration.  */
14709               attributes = cp_parser_attributes_opt (parser);
14710               /* Remember which attributes are prefix attributes and
14711                  which are not.  */
14712               first_attribute = attributes;
14713               /* Combine the attributes.  */
14714               attributes = chainon (prefix_attributes, attributes);
14715
14716               /* If it's an `=', then we have a constant-initializer or a
14717                  pure-specifier.  It is not correct to parse the
14718                  initializer before registering the member declaration
14719                  since the member declaration should be in scope while
14720                  its initializer is processed.  However, the rest of the
14721                  front end does not yet provide an interface that allows
14722                  us to handle this correctly.  */
14723               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14724                 {
14725                   /* In [class.mem]:
14726
14727                      A pure-specifier shall be used only in the declaration of
14728                      a virtual function.
14729
14730                      A member-declarator can contain a constant-initializer
14731                      only if it declares a static member of integral or
14732                      enumeration type.
14733
14734                      Therefore, if the DECLARATOR is for a function, we look
14735                      for a pure-specifier; otherwise, we look for a
14736                      constant-initializer.  When we call `grokfield', it will
14737                      perform more stringent semantics checks.  */
14738                   if (function_declarator_p (declarator))
14739                     initializer = cp_parser_pure_specifier (parser);
14740                   else
14741                     /* Parse the initializer.  */
14742                     initializer = cp_parser_constant_initializer (parser);
14743                 }
14744               /* Otherwise, there is no initializer.  */
14745               else
14746                 initializer = NULL_TREE;
14747
14748               /* See if we are probably looking at a function
14749                  definition.  We are certainly not looking at a
14750                  member-declarator.  Calling `grokfield' has
14751                  side-effects, so we must not do it unless we are sure
14752                  that we are looking at a member-declarator.  */
14753               if (cp_parser_token_starts_function_definition_p
14754                   (cp_lexer_peek_token (parser->lexer)))
14755                 {
14756                   /* The grammar does not allow a pure-specifier to be
14757                      used when a member function is defined.  (It is
14758                      possible that this fact is an oversight in the
14759                      standard, since a pure function may be defined
14760                      outside of the class-specifier.  */
14761                   if (initializer)
14762                     error ("pure-specifier on function-definition");
14763                   decl = cp_parser_save_member_function_body (parser,
14764                                                               &decl_specifiers,
14765                                                               declarator,
14766                                                               attributes);
14767                   /* If the member was not a friend, declare it here.  */
14768                   if (!friend_p)
14769                     finish_member_declaration (decl);
14770                   /* Peek at the next token.  */
14771                   token = cp_lexer_peek_token (parser->lexer);
14772                   /* If the next token is a semicolon, consume it.  */
14773                   if (token->type == CPP_SEMICOLON)
14774                     {
14775                       if (pedantic && !in_system_header)
14776                         pedwarn ("extra %<;%>");
14777                       cp_lexer_consume_token (parser->lexer);
14778                     }
14779                   return;
14780                 }
14781               else
14782                 /* Create the declaration.  */
14783                 decl = grokfield (declarator, &decl_specifiers,
14784                                   initializer, /*init_const_expr_p=*/true,
14785                                   asm_specification,
14786                                   attributes);
14787             }
14788
14789           /* Reset PREFIX_ATTRIBUTES.  */
14790           while (attributes && TREE_CHAIN (attributes) != first_attribute)
14791             attributes = TREE_CHAIN (attributes);
14792           if (attributes)
14793             TREE_CHAIN (attributes) = NULL_TREE;
14794
14795           /* If there is any qualification still in effect, clear it
14796              now; we will be starting fresh with the next declarator.  */
14797           parser->scope = NULL_TREE;
14798           parser->qualifying_scope = NULL_TREE;
14799           parser->object_scope = NULL_TREE;
14800           /* If it's a `,', then there are more declarators.  */
14801           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14802             cp_lexer_consume_token (parser->lexer);
14803           /* If the next token isn't a `;', then we have a parse error.  */
14804           else if (cp_lexer_next_token_is_not (parser->lexer,
14805                                                CPP_SEMICOLON))
14806             {
14807               cp_parser_error (parser, "expected %<;%>");
14808               /* Skip tokens until we find a `;'.  */
14809               cp_parser_skip_to_end_of_statement (parser);
14810
14811               break;
14812             }
14813
14814           if (decl)
14815             {
14816               /* Add DECL to the list of members.  */
14817               if (!friend_p)
14818                 finish_member_declaration (decl);
14819
14820               if (TREE_CODE (decl) == FUNCTION_DECL)
14821                 cp_parser_save_default_args (parser, decl);
14822             }
14823         }
14824     }
14825
14826   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14827 }
14828
14829 /* Parse a pure-specifier.
14830
14831    pure-specifier:
14832      = 0
14833
14834    Returns INTEGER_ZERO_NODE if a pure specifier is found.
14835    Otherwise, ERROR_MARK_NODE is returned.  */
14836
14837 static tree
14838 cp_parser_pure_specifier (cp_parser* parser)
14839 {
14840   cp_token *token;
14841
14842   /* Look for the `=' token.  */
14843   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14844     return error_mark_node;
14845   /* Look for the `0' token.  */
14846   token = cp_lexer_consume_token (parser->lexer);
14847   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
14848   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14849     {
14850       cp_parser_error (parser,
14851                        "invalid pure specifier (only `= 0' is allowed)");
14852       cp_parser_skip_to_end_of_statement (parser);
14853       return error_mark_node;
14854     }
14855   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14856     {
14857       error ("templates may not be %<virtual%>");
14858       return error_mark_node;
14859     }
14860
14861   return integer_zero_node;
14862 }
14863
14864 /* Parse a constant-initializer.
14865
14866    constant-initializer:
14867      = constant-expression
14868
14869    Returns a representation of the constant-expression.  */
14870
14871 static tree
14872 cp_parser_constant_initializer (cp_parser* parser)
14873 {
14874   /* Look for the `=' token.  */
14875   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14876     return error_mark_node;
14877
14878   /* It is invalid to write:
14879
14880        struct S { static const int i = { 7 }; };
14881
14882      */
14883   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14884     {
14885       cp_parser_error (parser,
14886                        "a brace-enclosed initializer is not allowed here");
14887       /* Consume the opening brace.  */
14888       cp_lexer_consume_token (parser->lexer);
14889       /* Skip the initializer.  */
14890       cp_parser_skip_to_closing_brace (parser);
14891       /* Look for the trailing `}'.  */
14892       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14893
14894       return error_mark_node;
14895     }
14896
14897   return cp_parser_constant_expression (parser,
14898                                         /*allow_non_constant=*/false,
14899                                         NULL);
14900 }
14901
14902 /* Derived classes [gram.class.derived] */
14903
14904 /* Parse a base-clause.
14905
14906    base-clause:
14907      : base-specifier-list
14908
14909    base-specifier-list:
14910      base-specifier ... [opt]
14911      base-specifier-list , base-specifier ... [opt]
14912
14913    Returns a TREE_LIST representing the base-classes, in the order in
14914    which they were declared.  The representation of each node is as
14915    described by cp_parser_base_specifier.
14916
14917    In the case that no bases are specified, this function will return
14918    NULL_TREE, not ERROR_MARK_NODE.  */
14919
14920 static tree
14921 cp_parser_base_clause (cp_parser* parser)
14922 {
14923   tree bases = NULL_TREE;
14924
14925   /* Look for the `:' that begins the list.  */
14926   cp_parser_require (parser, CPP_COLON, "`:'");
14927
14928   /* Scan the base-specifier-list.  */
14929   while (true)
14930     {
14931       cp_token *token;
14932       tree base;
14933       bool pack_expansion_p = false;
14934
14935       /* Look for the base-specifier.  */
14936       base = cp_parser_base_specifier (parser);
14937       /* Look for the (optional) ellipsis. */
14938       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14939         {
14940           /* Consume the `...'. */
14941           cp_lexer_consume_token (parser->lexer);
14942
14943           pack_expansion_p = true;
14944         }
14945
14946       /* Add BASE to the front of the list.  */
14947       if (base != error_mark_node)
14948         {
14949           if (pack_expansion_p)
14950             /* Make this a pack expansion type. */
14951             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
14952           else
14953             check_for_bare_parameter_packs (TREE_VALUE (base));
14954
14955           TREE_CHAIN (base) = bases;
14956           bases = base;
14957         }
14958       /* Peek at the next token.  */
14959       token = cp_lexer_peek_token (parser->lexer);
14960       /* If it's not a comma, then the list is complete.  */
14961       if (token->type != CPP_COMMA)
14962         break;
14963       /* Consume the `,'.  */
14964       cp_lexer_consume_token (parser->lexer);
14965     }
14966
14967   /* PARSER->SCOPE may still be non-NULL at this point, if the last
14968      base class had a qualified name.  However, the next name that
14969      appears is certainly not qualified.  */
14970   parser->scope = NULL_TREE;
14971   parser->qualifying_scope = NULL_TREE;
14972   parser->object_scope = NULL_TREE;
14973
14974   return nreverse (bases);
14975 }
14976
14977 /* Parse a base-specifier.
14978
14979    base-specifier:
14980      :: [opt] nested-name-specifier [opt] class-name
14981      virtual access-specifier [opt] :: [opt] nested-name-specifier
14982        [opt] class-name
14983      access-specifier virtual [opt] :: [opt] nested-name-specifier
14984        [opt] class-name
14985
14986    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14987    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14988    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14989    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14990
14991 static tree
14992 cp_parser_base_specifier (cp_parser* parser)
14993 {
14994   cp_token *token;
14995   bool done = false;
14996   bool virtual_p = false;
14997   bool duplicate_virtual_error_issued_p = false;
14998   bool duplicate_access_error_issued_p = false;
14999   bool class_scope_p, template_p;
15000   tree access = access_default_node;
15001   tree type;
15002
15003   /* Process the optional `virtual' and `access-specifier'.  */
15004   while (!done)
15005     {
15006       /* Peek at the next token.  */
15007       token = cp_lexer_peek_token (parser->lexer);
15008       /* Process `virtual'.  */
15009       switch (token->keyword)
15010         {
15011         case RID_VIRTUAL:
15012           /* If `virtual' appears more than once, issue an error.  */
15013           if (virtual_p && !duplicate_virtual_error_issued_p)
15014             {
15015               cp_parser_error (parser,
15016                                "%<virtual%> specified more than once in base-specified");
15017               duplicate_virtual_error_issued_p = true;
15018             }
15019
15020           virtual_p = true;
15021
15022           /* Consume the `virtual' token.  */
15023           cp_lexer_consume_token (parser->lexer);
15024
15025           break;
15026
15027         case RID_PUBLIC:
15028         case RID_PROTECTED:
15029         case RID_PRIVATE:
15030           /* If more than one access specifier appears, issue an
15031              error.  */
15032           if (access != access_default_node
15033               && !duplicate_access_error_issued_p)
15034             {
15035               cp_parser_error (parser,
15036                                "more than one access specifier in base-specified");
15037               duplicate_access_error_issued_p = true;
15038             }
15039
15040           access = ridpointers[(int) token->keyword];
15041
15042           /* Consume the access-specifier.  */
15043           cp_lexer_consume_token (parser->lexer);
15044
15045           break;
15046
15047         default:
15048           done = true;
15049           break;
15050         }
15051     }
15052   /* It is not uncommon to see programs mechanically, erroneously, use
15053      the 'typename' keyword to denote (dependent) qualified types
15054      as base classes.  */
15055   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15056     {
15057       if (!processing_template_decl)
15058         error ("keyword %<typename%> not allowed outside of templates");
15059       else
15060         error ("keyword %<typename%> not allowed in this context "
15061                "(the base class is implicitly a type)");
15062       cp_lexer_consume_token (parser->lexer);
15063     }
15064
15065   /* Look for the optional `::' operator.  */
15066   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15067   /* Look for the nested-name-specifier.  The simplest way to
15068      implement:
15069
15070        [temp.res]
15071
15072        The keyword `typename' is not permitted in a base-specifier or
15073        mem-initializer; in these contexts a qualified name that
15074        depends on a template-parameter is implicitly assumed to be a
15075        type name.
15076
15077      is to pretend that we have seen the `typename' keyword at this
15078      point.  */
15079   cp_parser_nested_name_specifier_opt (parser,
15080                                        /*typename_keyword_p=*/true,
15081                                        /*check_dependency_p=*/true,
15082                                        typename_type,
15083                                        /*is_declaration=*/true);
15084   /* If the base class is given by a qualified name, assume that names
15085      we see are type names or templates, as appropriate.  */
15086   class_scope_p = (parser->scope && TYPE_P (parser->scope));
15087   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15088
15089   /* Finally, look for the class-name.  */
15090   type = cp_parser_class_name (parser,
15091                                class_scope_p,
15092                                template_p,
15093                                typename_type,
15094                                /*check_dependency_p=*/true,
15095                                /*class_head_p=*/false,
15096                                /*is_declaration=*/true);
15097
15098   if (type == error_mark_node)
15099     return error_mark_node;
15100
15101   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15102 }
15103
15104 /* Exception handling [gram.exception] */
15105
15106 /* Parse an (optional) exception-specification.
15107
15108    exception-specification:
15109      throw ( type-id-list [opt] )
15110
15111    Returns a TREE_LIST representing the exception-specification.  The
15112    TREE_VALUE of each node is a type.  */
15113
15114 static tree
15115 cp_parser_exception_specification_opt (cp_parser* parser)
15116 {
15117   cp_token *token;
15118   tree type_id_list;
15119
15120   /* Peek at the next token.  */
15121   token = cp_lexer_peek_token (parser->lexer);
15122   /* If it's not `throw', then there's no exception-specification.  */
15123   if (!cp_parser_is_keyword (token, RID_THROW))
15124     return NULL_TREE;
15125
15126   /* Consume the `throw'.  */
15127   cp_lexer_consume_token (parser->lexer);
15128
15129   /* Look for the `('.  */
15130   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15131
15132   /* Peek at the next token.  */
15133   token = cp_lexer_peek_token (parser->lexer);
15134   /* If it's not a `)', then there is a type-id-list.  */
15135   if (token->type != CPP_CLOSE_PAREN)
15136     {
15137       const char *saved_message;
15138
15139       /* Types may not be defined in an exception-specification.  */
15140       saved_message = parser->type_definition_forbidden_message;
15141       parser->type_definition_forbidden_message
15142         = "types may not be defined in an exception-specification";
15143       /* Parse the type-id-list.  */
15144       type_id_list = cp_parser_type_id_list (parser);
15145       /* Restore the saved message.  */
15146       parser->type_definition_forbidden_message = saved_message;
15147     }
15148   else
15149     type_id_list = empty_except_spec;
15150
15151   /* Look for the `)'.  */
15152   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15153
15154   return type_id_list;
15155 }
15156
15157 /* Parse an (optional) type-id-list.
15158
15159    type-id-list:
15160      type-id ... [opt]
15161      type-id-list , type-id ... [opt]
15162
15163    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
15164    in the order that the types were presented.  */
15165
15166 static tree
15167 cp_parser_type_id_list (cp_parser* parser)
15168 {
15169   tree types = NULL_TREE;
15170
15171   while (true)
15172     {
15173       cp_token *token;
15174       tree type;
15175
15176       /* Get the next type-id.  */
15177       type = cp_parser_type_id (parser);
15178       /* Parse the optional ellipsis. */
15179       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15180         {
15181           /* Consume the `...'. */
15182           cp_lexer_consume_token (parser->lexer);
15183
15184           /* Turn the type into a pack expansion expression. */
15185           type = make_pack_expansion (type);
15186         }
15187       /* Add it to the list.  */
15188       types = add_exception_specifier (types, type, /*complain=*/1);
15189       /* Peek at the next token.  */
15190       token = cp_lexer_peek_token (parser->lexer);
15191       /* If it is not a `,', we are done.  */
15192       if (token->type != CPP_COMMA)
15193         break;
15194       /* Consume the `,'.  */
15195       cp_lexer_consume_token (parser->lexer);
15196     }
15197
15198   return nreverse (types);
15199 }
15200
15201 /* Parse a try-block.
15202
15203    try-block:
15204      try compound-statement handler-seq  */
15205
15206 static tree
15207 cp_parser_try_block (cp_parser* parser)
15208 {
15209   tree try_block;
15210
15211   cp_parser_require_keyword (parser, RID_TRY, "`try'");
15212   try_block = begin_try_block ();
15213   cp_parser_compound_statement (parser, NULL, true);
15214   finish_try_block (try_block);
15215   cp_parser_handler_seq (parser);
15216   finish_handler_sequence (try_block);
15217
15218   return try_block;
15219 }
15220
15221 /* Parse a function-try-block.
15222
15223    function-try-block:
15224      try ctor-initializer [opt] function-body handler-seq  */
15225
15226 static bool
15227 cp_parser_function_try_block (cp_parser* parser)
15228 {
15229   tree compound_stmt;
15230   tree try_block;
15231   bool ctor_initializer_p;
15232
15233   /* Look for the `try' keyword.  */
15234   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15235     return false;
15236   /* Let the rest of the front end know where we are.  */
15237   try_block = begin_function_try_block (&compound_stmt);
15238   /* Parse the function-body.  */
15239   ctor_initializer_p
15240     = cp_parser_ctor_initializer_opt_and_function_body (parser);
15241   /* We're done with the `try' part.  */
15242   finish_function_try_block (try_block);
15243   /* Parse the handlers.  */
15244   cp_parser_handler_seq (parser);
15245   /* We're done with the handlers.  */
15246   finish_function_handler_sequence (try_block, compound_stmt);
15247
15248   return ctor_initializer_p;
15249 }
15250
15251 /* Parse a handler-seq.
15252
15253    handler-seq:
15254      handler handler-seq [opt]  */
15255
15256 static void
15257 cp_parser_handler_seq (cp_parser* parser)
15258 {
15259   while (true)
15260     {
15261       cp_token *token;
15262
15263       /* Parse the handler.  */
15264       cp_parser_handler (parser);
15265       /* Peek at the next token.  */
15266       token = cp_lexer_peek_token (parser->lexer);
15267       /* If it's not `catch' then there are no more handlers.  */
15268       if (!cp_parser_is_keyword (token, RID_CATCH))
15269         break;
15270     }
15271 }
15272
15273 /* Parse a handler.
15274
15275    handler:
15276      catch ( exception-declaration ) compound-statement  */
15277
15278 static void
15279 cp_parser_handler (cp_parser* parser)
15280 {
15281   tree handler;
15282   tree declaration;
15283
15284   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15285   handler = begin_handler ();
15286   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15287   declaration = cp_parser_exception_declaration (parser);
15288   finish_handler_parms (declaration, handler);
15289   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15290   cp_parser_compound_statement (parser, NULL, false);
15291   finish_handler (handler);
15292 }
15293
15294 /* Parse an exception-declaration.
15295
15296    exception-declaration:
15297      type-specifier-seq declarator
15298      type-specifier-seq abstract-declarator
15299      type-specifier-seq
15300      ...
15301
15302    Returns a VAR_DECL for the declaration, or NULL_TREE if the
15303    ellipsis variant is used.  */
15304
15305 static tree
15306 cp_parser_exception_declaration (cp_parser* parser)
15307 {
15308   cp_decl_specifier_seq type_specifiers;
15309   cp_declarator *declarator;
15310   const char *saved_message;
15311
15312   /* If it's an ellipsis, it's easy to handle.  */
15313   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15314     {
15315       /* Consume the `...' token.  */
15316       cp_lexer_consume_token (parser->lexer);
15317       return NULL_TREE;
15318     }
15319
15320   /* Types may not be defined in exception-declarations.  */
15321   saved_message = parser->type_definition_forbidden_message;
15322   parser->type_definition_forbidden_message
15323     = "types may not be defined in exception-declarations";
15324
15325   /* Parse the type-specifier-seq.  */
15326   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15327                                 &type_specifiers);
15328   /* If it's a `)', then there is no declarator.  */
15329   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15330     declarator = NULL;
15331   else
15332     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15333                                        /*ctor_dtor_or_conv_p=*/NULL,
15334                                        /*parenthesized_p=*/NULL,
15335                                        /*member_p=*/false);
15336
15337   /* Restore the saved message.  */
15338   parser->type_definition_forbidden_message = saved_message;
15339
15340   if (!type_specifiers.any_specifiers_p)
15341     return error_mark_node;
15342
15343   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15344 }
15345
15346 /* Parse a throw-expression.
15347
15348    throw-expression:
15349      throw assignment-expression [opt]
15350
15351    Returns a THROW_EXPR representing the throw-expression.  */
15352
15353 static tree
15354 cp_parser_throw_expression (cp_parser* parser)
15355 {
15356   tree expression;
15357   cp_token* token;
15358
15359   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15360   token = cp_lexer_peek_token (parser->lexer);
15361   /* Figure out whether or not there is an assignment-expression
15362      following the "throw" keyword.  */
15363   if (token->type == CPP_COMMA
15364       || token->type == CPP_SEMICOLON
15365       || token->type == CPP_CLOSE_PAREN
15366       || token->type == CPP_CLOSE_SQUARE
15367       || token->type == CPP_CLOSE_BRACE
15368       || token->type == CPP_COLON)
15369     expression = NULL_TREE;
15370   else
15371     expression = cp_parser_assignment_expression (parser,
15372                                                   /*cast_p=*/false);
15373
15374   return build_throw (expression);
15375 }
15376
15377 /* GNU Extensions */
15378
15379 /* Parse an (optional) asm-specification.
15380
15381    asm-specification:
15382      asm ( string-literal )
15383
15384    If the asm-specification is present, returns a STRING_CST
15385    corresponding to the string-literal.  Otherwise, returns
15386    NULL_TREE.  */
15387
15388 static tree
15389 cp_parser_asm_specification_opt (cp_parser* parser)
15390 {
15391   cp_token *token;
15392   tree asm_specification;
15393
15394   /* Peek at the next token.  */
15395   token = cp_lexer_peek_token (parser->lexer);
15396   /* If the next token isn't the `asm' keyword, then there's no
15397      asm-specification.  */
15398   if (!cp_parser_is_keyword (token, RID_ASM))
15399     return NULL_TREE;
15400
15401   /* Consume the `asm' token.  */
15402   cp_lexer_consume_token (parser->lexer);
15403   /* Look for the `('.  */
15404   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15405
15406   /* Look for the string-literal.  */
15407   asm_specification = cp_parser_string_literal (parser, false, false);
15408
15409   /* Look for the `)'.  */
15410   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15411
15412   return asm_specification;
15413 }
15414
15415 /* Parse an asm-operand-list.
15416
15417    asm-operand-list:
15418      asm-operand
15419      asm-operand-list , asm-operand
15420
15421    asm-operand:
15422      string-literal ( expression )
15423      [ string-literal ] string-literal ( expression )
15424
15425    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
15426    each node is the expression.  The TREE_PURPOSE is itself a
15427    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15428    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15429    is a STRING_CST for the string literal before the parenthesis.  */
15430
15431 static tree
15432 cp_parser_asm_operand_list (cp_parser* parser)
15433 {
15434   tree asm_operands = NULL_TREE;
15435
15436   while (true)
15437     {
15438       tree string_literal;
15439       tree expression;
15440       tree name;
15441
15442       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15443         {
15444           /* Consume the `[' token.  */
15445           cp_lexer_consume_token (parser->lexer);
15446           /* Read the operand name.  */
15447           name = cp_parser_identifier (parser);
15448           if (name != error_mark_node)
15449             name = build_string (IDENTIFIER_LENGTH (name),
15450                                  IDENTIFIER_POINTER (name));
15451           /* Look for the closing `]'.  */
15452           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15453         }
15454       else
15455         name = NULL_TREE;
15456       /* Look for the string-literal.  */
15457       string_literal = cp_parser_string_literal (parser, false, false);
15458
15459       /* Look for the `('.  */
15460       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15461       /* Parse the expression.  */
15462       expression = cp_parser_expression (parser, /*cast_p=*/false);
15463       /* Look for the `)'.  */
15464       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15465
15466       /* Add this operand to the list.  */
15467       asm_operands = tree_cons (build_tree_list (name, string_literal),
15468                                 expression,
15469                                 asm_operands);
15470       /* If the next token is not a `,', there are no more
15471          operands.  */
15472       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15473         break;
15474       /* Consume the `,'.  */
15475       cp_lexer_consume_token (parser->lexer);
15476     }
15477
15478   return nreverse (asm_operands);
15479 }
15480
15481 /* Parse an asm-clobber-list.
15482
15483    asm-clobber-list:
15484      string-literal
15485      asm-clobber-list , string-literal
15486
15487    Returns a TREE_LIST, indicating the clobbers in the order that they
15488    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
15489
15490 static tree
15491 cp_parser_asm_clobber_list (cp_parser* parser)
15492 {
15493   tree clobbers = NULL_TREE;
15494
15495   while (true)
15496     {
15497       tree string_literal;
15498
15499       /* Look for the string literal.  */
15500       string_literal = cp_parser_string_literal (parser, false, false);
15501       /* Add it to the list.  */
15502       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15503       /* If the next token is not a `,', then the list is
15504          complete.  */
15505       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15506         break;
15507       /* Consume the `,' token.  */
15508       cp_lexer_consume_token (parser->lexer);
15509     }
15510
15511   return clobbers;
15512 }
15513
15514 /* Parse an (optional) series of attributes.
15515
15516    attributes:
15517      attributes attribute
15518
15519    attribute:
15520      __attribute__ (( attribute-list [opt] ))
15521
15522    The return value is as for cp_parser_attribute_list.  */
15523
15524 static tree
15525 cp_parser_attributes_opt (cp_parser* parser)
15526 {
15527   tree attributes = NULL_TREE;
15528
15529   while (true)
15530     {
15531       cp_token *token;
15532       tree attribute_list;
15533
15534       /* Peek at the next token.  */
15535       token = cp_lexer_peek_token (parser->lexer);
15536       /* If it's not `__attribute__', then we're done.  */
15537       if (token->keyword != RID_ATTRIBUTE)
15538         break;
15539
15540       /* Consume the `__attribute__' keyword.  */
15541       cp_lexer_consume_token (parser->lexer);
15542       /* Look for the two `(' tokens.  */
15543       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15544       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15545
15546       /* Peek at the next token.  */
15547       token = cp_lexer_peek_token (parser->lexer);
15548       if (token->type != CPP_CLOSE_PAREN)
15549         /* Parse the attribute-list.  */
15550         attribute_list = cp_parser_attribute_list (parser);
15551       else
15552         /* If the next token is a `)', then there is no attribute
15553            list.  */
15554         attribute_list = NULL;
15555
15556       /* Look for the two `)' tokens.  */
15557       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15558       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15559
15560       /* Add these new attributes to the list.  */
15561       attributes = chainon (attributes, attribute_list);
15562     }
15563
15564   return attributes;
15565 }
15566
15567 /* Parse an attribute-list.
15568
15569    attribute-list:
15570      attribute
15571      attribute-list , attribute
15572
15573    attribute:
15574      identifier
15575      identifier ( identifier )
15576      identifier ( identifier , expression-list )
15577      identifier ( expression-list )
15578
15579    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
15580    to an attribute.  The TREE_PURPOSE of each node is the identifier
15581    indicating which attribute is in use.  The TREE_VALUE represents
15582    the arguments, if any.  */
15583
15584 static tree
15585 cp_parser_attribute_list (cp_parser* parser)
15586 {
15587   tree attribute_list = NULL_TREE;
15588   bool save_translate_strings_p = parser->translate_strings_p;
15589
15590   parser->translate_strings_p = false;
15591   while (true)
15592     {
15593       cp_token *token;
15594       tree identifier;
15595       tree attribute;
15596
15597       /* Look for the identifier.  We also allow keywords here; for
15598          example `__attribute__ ((const))' is legal.  */
15599       token = cp_lexer_peek_token (parser->lexer);
15600       if (token->type == CPP_NAME
15601           || token->type == CPP_KEYWORD)
15602         {
15603           tree arguments = NULL_TREE;
15604
15605           /* Consume the token.  */
15606           token = cp_lexer_consume_token (parser->lexer);
15607
15608           /* Save away the identifier that indicates which attribute
15609              this is.  */
15610           identifier = token->u.value;
15611           attribute = build_tree_list (identifier, NULL_TREE);
15612
15613           /* Peek at the next token.  */
15614           token = cp_lexer_peek_token (parser->lexer);
15615           /* If it's an `(', then parse the attribute arguments.  */
15616           if (token->type == CPP_OPEN_PAREN)
15617             {
15618               arguments = cp_parser_parenthesized_expression_list
15619                           (parser, true, /*cast_p=*/false,
15620                            /*allow_expansion_p=*/false,
15621                            /*non_constant_p=*/NULL);
15622               /* Save the arguments away.  */
15623               TREE_VALUE (attribute) = arguments;
15624             }
15625
15626           if (arguments != error_mark_node)
15627             {
15628               /* Add this attribute to the list.  */
15629               TREE_CHAIN (attribute) = attribute_list;
15630               attribute_list = attribute;
15631             }
15632
15633           token = cp_lexer_peek_token (parser->lexer);
15634         }
15635       /* Now, look for more attributes.  If the next token isn't a
15636          `,', we're done.  */
15637       if (token->type != CPP_COMMA)
15638         break;
15639
15640       /* Consume the comma and keep going.  */
15641       cp_lexer_consume_token (parser->lexer);
15642     }
15643   parser->translate_strings_p = save_translate_strings_p;
15644
15645   /* We built up the list in reverse order.  */
15646   return nreverse (attribute_list);
15647 }
15648
15649 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
15650    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
15651    current value of the PEDANTIC flag, regardless of whether or not
15652    the `__extension__' keyword is present.  The caller is responsible
15653    for restoring the value of the PEDANTIC flag.  */
15654
15655 static bool
15656 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15657 {
15658   /* Save the old value of the PEDANTIC flag.  */
15659   *saved_pedantic = pedantic;
15660
15661   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15662     {
15663       /* Consume the `__extension__' token.  */
15664       cp_lexer_consume_token (parser->lexer);
15665       /* We're not being pedantic while the `__extension__' keyword is
15666          in effect.  */
15667       pedantic = 0;
15668
15669       return true;
15670     }
15671
15672   return false;
15673 }
15674
15675 /* Parse a label declaration.
15676
15677    label-declaration:
15678      __label__ label-declarator-seq ;
15679
15680    label-declarator-seq:
15681      identifier , label-declarator-seq
15682      identifier  */
15683
15684 static void
15685 cp_parser_label_declaration (cp_parser* parser)
15686 {
15687   /* Look for the `__label__' keyword.  */
15688   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15689
15690   while (true)
15691     {
15692       tree identifier;
15693
15694       /* Look for an identifier.  */
15695       identifier = cp_parser_identifier (parser);
15696       /* If we failed, stop.  */
15697       if (identifier == error_mark_node)
15698         break;
15699       /* Declare it as a label.  */
15700       finish_label_decl (identifier);
15701       /* If the next token is a `;', stop.  */
15702       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15703         break;
15704       /* Look for the `,' separating the label declarations.  */
15705       cp_parser_require (parser, CPP_COMMA, "`,'");
15706     }
15707
15708   /* Look for the final `;'.  */
15709   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15710 }
15711
15712 /* Support Functions */
15713
15714 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15715    NAME should have one of the representations used for an
15716    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15717    is returned.  If PARSER->SCOPE is a dependent type, then a
15718    SCOPE_REF is returned.
15719
15720    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15721    returned; the name was already resolved when the TEMPLATE_ID_EXPR
15722    was formed.  Abstractly, such entities should not be passed to this
15723    function, because they do not need to be looked up, but it is
15724    simpler to check for this special case here, rather than at the
15725    call-sites.
15726
15727    In cases not explicitly covered above, this function returns a
15728    DECL, OVERLOAD, or baselink representing the result of the lookup.
15729    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15730    is returned.
15731
15732    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15733    (e.g., "struct") that was used.  In that case bindings that do not
15734    refer to types are ignored.
15735
15736    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15737    ignored.
15738
15739    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15740    are ignored.
15741
15742    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15743    types.
15744
15745    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15746    TREE_LIST of candidates if name-lookup results in an ambiguity, and
15747    NULL_TREE otherwise.  */
15748
15749 static tree
15750 cp_parser_lookup_name (cp_parser *parser, tree name,
15751                        enum tag_types tag_type,
15752                        bool is_template,
15753                        bool is_namespace,
15754                        bool check_dependency,
15755                        tree *ambiguous_decls)
15756 {
15757   int flags = 0;
15758   tree decl;
15759   tree object_type = parser->context->object_type;
15760
15761   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15762     flags |= LOOKUP_COMPLAIN;
15763
15764   /* Assume that the lookup will be unambiguous.  */
15765   if (ambiguous_decls)
15766     *ambiguous_decls = NULL_TREE;
15767
15768   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15769      no longer valid.  Note that if we are parsing tentatively, and
15770      the parse fails, OBJECT_TYPE will be automatically restored.  */
15771   parser->context->object_type = NULL_TREE;
15772
15773   if (name == error_mark_node)
15774     return error_mark_node;
15775
15776   /* A template-id has already been resolved; there is no lookup to
15777      do.  */
15778   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15779     return name;
15780   if (BASELINK_P (name))
15781     {
15782       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15783                   == TEMPLATE_ID_EXPR);
15784       return name;
15785     }
15786
15787   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
15788      it should already have been checked to make sure that the name
15789      used matches the type being destroyed.  */
15790   if (TREE_CODE (name) == BIT_NOT_EXPR)
15791     {
15792       tree type;
15793
15794       /* Figure out to which type this destructor applies.  */
15795       if (parser->scope)
15796         type = parser->scope;
15797       else if (object_type)
15798         type = object_type;
15799       else
15800         type = current_class_type;
15801       /* If that's not a class type, there is no destructor.  */
15802       if (!type || !CLASS_TYPE_P (type))
15803         return error_mark_node;
15804       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15805         lazily_declare_fn (sfk_destructor, type);
15806       if (!CLASSTYPE_DESTRUCTORS (type))
15807           return error_mark_node;
15808       /* If it was a class type, return the destructor.  */
15809       return CLASSTYPE_DESTRUCTORS (type);
15810     }
15811
15812   /* By this point, the NAME should be an ordinary identifier.  If
15813      the id-expression was a qualified name, the qualifying scope is
15814      stored in PARSER->SCOPE at this point.  */
15815   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15816
15817   /* Perform the lookup.  */
15818   if (parser->scope)
15819     {
15820       bool dependent_p;
15821
15822       if (parser->scope == error_mark_node)
15823         return error_mark_node;
15824
15825       /* If the SCOPE is dependent, the lookup must be deferred until
15826          the template is instantiated -- unless we are explicitly
15827          looking up names in uninstantiated templates.  Even then, we
15828          cannot look up the name if the scope is not a class type; it
15829          might, for example, be a template type parameter.  */
15830       dependent_p = (TYPE_P (parser->scope)
15831                      && !(parser->in_declarator_p
15832                           && currently_open_class (parser->scope))
15833                      && dependent_type_p (parser->scope));
15834       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15835            && dependent_p)
15836         {
15837           if (tag_type)
15838             {
15839               tree type;
15840
15841               /* The resolution to Core Issue 180 says that `struct
15842                  A::B' should be considered a type-name, even if `A'
15843                  is dependent.  */
15844               type = make_typename_type (parser->scope, name, tag_type,
15845                                          /*complain=*/tf_error);
15846               decl = TYPE_NAME (type);
15847             }
15848           else if (is_template
15849                    && (cp_parser_next_token_ends_template_argument_p (parser)
15850                        || cp_lexer_next_token_is (parser->lexer,
15851                                                   CPP_CLOSE_PAREN)))
15852             decl = make_unbound_class_template (parser->scope,
15853                                                 name, NULL_TREE,
15854                                                 /*complain=*/tf_error);
15855           else
15856             decl = build_qualified_name (/*type=*/NULL_TREE,
15857                                          parser->scope, name,
15858                                          is_template);
15859         }
15860       else
15861         {
15862           tree pushed_scope = NULL_TREE;
15863
15864           /* If PARSER->SCOPE is a dependent type, then it must be a
15865              class type, and we must not be checking dependencies;
15866              otherwise, we would have processed this lookup above.  So
15867              that PARSER->SCOPE is not considered a dependent base by
15868              lookup_member, we must enter the scope here.  */
15869           if (dependent_p)
15870             pushed_scope = push_scope (parser->scope);
15871           /* If the PARSER->SCOPE is a template specialization, it
15872              may be instantiated during name lookup.  In that case,
15873              errors may be issued.  Even if we rollback the current
15874              tentative parse, those errors are valid.  */
15875           decl = lookup_qualified_name (parser->scope, name,
15876                                         tag_type != none_type,
15877                                         /*complain=*/true);
15878           if (pushed_scope)
15879             pop_scope (pushed_scope);
15880         }
15881       parser->qualifying_scope = parser->scope;
15882       parser->object_scope = NULL_TREE;
15883     }
15884   else if (object_type)
15885     {
15886       tree object_decl = NULL_TREE;
15887       /* Look up the name in the scope of the OBJECT_TYPE, unless the
15888          OBJECT_TYPE is not a class.  */
15889       if (CLASS_TYPE_P (object_type))
15890         /* If the OBJECT_TYPE is a template specialization, it may
15891            be instantiated during name lookup.  In that case, errors
15892            may be issued.  Even if we rollback the current tentative
15893            parse, those errors are valid.  */
15894         object_decl = lookup_member (object_type,
15895                                      name,
15896                                      /*protect=*/0,
15897                                      tag_type != none_type);
15898       /* Look it up in the enclosing context, too.  */
15899       decl = lookup_name_real (name, tag_type != none_type,
15900                                /*nonclass=*/0,
15901                                /*block_p=*/true, is_namespace, flags);
15902       parser->object_scope = object_type;
15903       parser->qualifying_scope = NULL_TREE;
15904       if (object_decl)
15905         decl = object_decl;
15906     }
15907   else
15908     {
15909       decl = lookup_name_real (name, tag_type != none_type,
15910                                /*nonclass=*/0,
15911                                /*block_p=*/true, is_namespace, flags);
15912       parser->qualifying_scope = NULL_TREE;
15913       parser->object_scope = NULL_TREE;
15914     }
15915
15916   /* If the lookup failed, let our caller know.  */
15917   if (!decl || decl == error_mark_node)
15918     return error_mark_node;
15919
15920   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
15921   if (TREE_CODE (decl) == TREE_LIST)
15922     {
15923       if (ambiguous_decls)
15924         *ambiguous_decls = decl;
15925       /* The error message we have to print is too complicated for
15926          cp_parser_error, so we incorporate its actions directly.  */
15927       if (!cp_parser_simulate_error (parser))
15928         {
15929           error ("reference to %qD is ambiguous", name);
15930           print_candidates (decl);
15931         }
15932       return error_mark_node;
15933     }
15934
15935   gcc_assert (DECL_P (decl)
15936               || TREE_CODE (decl) == OVERLOAD
15937               || TREE_CODE (decl) == SCOPE_REF
15938               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15939               || BASELINK_P (decl));
15940
15941   /* If we have resolved the name of a member declaration, check to
15942      see if the declaration is accessible.  When the name resolves to
15943      set of overloaded functions, accessibility is checked when
15944      overload resolution is done.
15945
15946      During an explicit instantiation, access is not checked at all,
15947      as per [temp.explicit].  */
15948   if (DECL_P (decl))
15949     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15950
15951   return decl;
15952 }
15953
15954 /* Like cp_parser_lookup_name, but for use in the typical case where
15955    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15956    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
15957
15958 static tree
15959 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15960 {
15961   return cp_parser_lookup_name (parser, name,
15962                                 none_type,
15963                                 /*is_template=*/false,
15964                                 /*is_namespace=*/false,
15965                                 /*check_dependency=*/true,
15966                                 /*ambiguous_decls=*/NULL);
15967 }
15968
15969 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15970    the current context, return the TYPE_DECL.  If TAG_NAME_P is
15971    true, the DECL indicates the class being defined in a class-head,
15972    or declared in an elaborated-type-specifier.
15973
15974    Otherwise, return DECL.  */
15975
15976 static tree
15977 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15978 {
15979   /* If the TEMPLATE_DECL is being declared as part of a class-head,
15980      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15981
15982        struct A {
15983          template <typename T> struct B;
15984        };
15985
15986        template <typename T> struct A::B {};
15987
15988      Similarly, in an elaborated-type-specifier:
15989
15990        namespace N { struct X{}; }
15991
15992        struct A {
15993          template <typename T> friend struct N::X;
15994        };
15995
15996      However, if the DECL refers to a class type, and we are in
15997      the scope of the class, then the name lookup automatically
15998      finds the TYPE_DECL created by build_self_reference rather
15999      than a TEMPLATE_DECL.  For example, in:
16000
16001        template <class T> struct S {
16002          S s;
16003        };
16004
16005      there is no need to handle such case.  */
16006
16007   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16008     return DECL_TEMPLATE_RESULT (decl);
16009
16010   return decl;
16011 }
16012
16013 /* If too many, or too few, template-parameter lists apply to the
16014    declarator, issue an error message.  Returns TRUE if all went well,
16015    and FALSE otherwise.  */
16016
16017 static bool
16018 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16019                                                 cp_declarator *declarator)
16020 {
16021   unsigned num_templates;
16022
16023   /* We haven't seen any classes that involve template parameters yet.  */
16024   num_templates = 0;
16025
16026   switch (declarator->kind)
16027     {
16028     case cdk_id:
16029       if (declarator->u.id.qualifying_scope)
16030         {
16031           tree scope;
16032           tree member;
16033
16034           scope = declarator->u.id.qualifying_scope;
16035           member = declarator->u.id.unqualified_name;
16036
16037           while (scope && CLASS_TYPE_P (scope))
16038             {
16039               /* You're supposed to have one `template <...>'
16040                  for every template class, but you don't need one
16041                  for a full specialization.  For example:
16042
16043                  template <class T> struct S{};
16044                  template <> struct S<int> { void f(); };
16045                  void S<int>::f () {}
16046
16047                  is correct; there shouldn't be a `template <>' for
16048                  the definition of `S<int>::f'.  */
16049               if (!CLASSTYPE_TEMPLATE_INFO (scope))
16050                 /* If SCOPE does not have template information of any
16051                    kind, then it is not a template, nor is it nested
16052                    within a template.  */
16053                 break;
16054               if (explicit_class_specialization_p (scope))
16055                 break;
16056               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16057                 ++num_templates;
16058
16059               scope = TYPE_CONTEXT (scope);
16060             }
16061         }
16062       else if (TREE_CODE (declarator->u.id.unqualified_name)
16063                == TEMPLATE_ID_EXPR)
16064         /* If the DECLARATOR has the form `X<y>' then it uses one
16065            additional level of template parameters.  */
16066         ++num_templates;
16067
16068       return cp_parser_check_template_parameters (parser,
16069                                                   num_templates);
16070
16071     case cdk_function:
16072     case cdk_array:
16073     case cdk_pointer:
16074     case cdk_reference:
16075     case cdk_ptrmem:
16076       return (cp_parser_check_declarator_template_parameters
16077               (parser, declarator->declarator));
16078
16079     case cdk_error:
16080       return true;
16081
16082     default:
16083       gcc_unreachable ();
16084     }
16085   return false;
16086 }
16087
16088 /* NUM_TEMPLATES were used in the current declaration.  If that is
16089    invalid, return FALSE and issue an error messages.  Otherwise,
16090    return TRUE.  */
16091
16092 static bool
16093 cp_parser_check_template_parameters (cp_parser* parser,
16094                                      unsigned num_templates)
16095 {
16096   /* If there are more template classes than parameter lists, we have
16097      something like:
16098
16099        template <class T> void S<T>::R<T>::f ();  */
16100   if (parser->num_template_parameter_lists < num_templates)
16101     {
16102       error ("too few template-parameter-lists");
16103       return false;
16104     }
16105   /* If there are the same number of template classes and parameter
16106      lists, that's OK.  */
16107   if (parser->num_template_parameter_lists == num_templates)
16108     return true;
16109   /* If there are more, but only one more, then we are referring to a
16110      member template.  That's OK too.  */
16111   if (parser->num_template_parameter_lists == num_templates + 1)
16112       return true;
16113   /* Otherwise, there are too many template parameter lists.  We have
16114      something like:
16115
16116      template <class T> template <class U> void S::f();  */
16117   error ("too many template-parameter-lists");
16118   return false;
16119 }
16120
16121 /* Parse an optional `::' token indicating that the following name is
16122    from the global namespace.  If so, PARSER->SCOPE is set to the
16123    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16124    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16125    Returns the new value of PARSER->SCOPE, if the `::' token is
16126    present, and NULL_TREE otherwise.  */
16127
16128 static tree
16129 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16130 {
16131   cp_token *token;
16132
16133   /* Peek at the next token.  */
16134   token = cp_lexer_peek_token (parser->lexer);
16135   /* If we're looking at a `::' token then we're starting from the
16136      global namespace, not our current location.  */
16137   if (token->type == CPP_SCOPE)
16138     {
16139       /* Consume the `::' token.  */
16140       cp_lexer_consume_token (parser->lexer);
16141       /* Set the SCOPE so that we know where to start the lookup.  */
16142       parser->scope = global_namespace;
16143       parser->qualifying_scope = global_namespace;
16144       parser->object_scope = NULL_TREE;
16145
16146       return parser->scope;
16147     }
16148   else if (!current_scope_valid_p)
16149     {
16150       parser->scope = NULL_TREE;
16151       parser->qualifying_scope = NULL_TREE;
16152       parser->object_scope = NULL_TREE;
16153     }
16154
16155   return NULL_TREE;
16156 }
16157
16158 /* Returns TRUE if the upcoming token sequence is the start of a
16159    constructor declarator.  If FRIEND_P is true, the declarator is
16160    preceded by the `friend' specifier.  */
16161
16162 static bool
16163 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16164 {
16165   bool constructor_p;
16166   tree type_decl = NULL_TREE;
16167   bool nested_name_p;
16168   cp_token *next_token;
16169
16170   /* The common case is that this is not a constructor declarator, so
16171      try to avoid doing lots of work if at all possible.  It's not
16172      valid declare a constructor at function scope.  */
16173   if (parser->in_function_body)
16174     return false;
16175   /* And only certain tokens can begin a constructor declarator.  */
16176   next_token = cp_lexer_peek_token (parser->lexer);
16177   if (next_token->type != CPP_NAME
16178       && next_token->type != CPP_SCOPE
16179       && next_token->type != CPP_NESTED_NAME_SPECIFIER
16180       && next_token->type != CPP_TEMPLATE_ID)
16181     return false;
16182
16183   /* Parse tentatively; we are going to roll back all of the tokens
16184      consumed here.  */
16185   cp_parser_parse_tentatively (parser);
16186   /* Assume that we are looking at a constructor declarator.  */
16187   constructor_p = true;
16188
16189   /* Look for the optional `::' operator.  */
16190   cp_parser_global_scope_opt (parser,
16191                               /*current_scope_valid_p=*/false);
16192   /* Look for the nested-name-specifier.  */
16193   nested_name_p
16194     = (cp_parser_nested_name_specifier_opt (parser,
16195                                             /*typename_keyword_p=*/false,
16196                                             /*check_dependency_p=*/false,
16197                                             /*type_p=*/false,
16198                                             /*is_declaration=*/false)
16199        != NULL_TREE);
16200   /* Outside of a class-specifier, there must be a
16201      nested-name-specifier.  */
16202   if (!nested_name_p &&
16203       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16204        || friend_p))
16205     constructor_p = false;
16206   /* If we still think that this might be a constructor-declarator,
16207      look for a class-name.  */
16208   if (constructor_p)
16209     {
16210       /* If we have:
16211
16212            template <typename T> struct S { S(); };
16213            template <typename T> S<T>::S ();
16214
16215          we must recognize that the nested `S' names a class.
16216          Similarly, for:
16217
16218            template <typename T> S<T>::S<T> ();
16219
16220          we must recognize that the nested `S' names a template.  */
16221       type_decl = cp_parser_class_name (parser,
16222                                         /*typename_keyword_p=*/false,
16223                                         /*template_keyword_p=*/false,
16224                                         none_type,
16225                                         /*check_dependency_p=*/false,
16226                                         /*class_head_p=*/false,
16227                                         /*is_declaration=*/false);
16228       /* If there was no class-name, then this is not a constructor.  */
16229       constructor_p = !cp_parser_error_occurred (parser);
16230     }
16231
16232   /* If we're still considering a constructor, we have to see a `(',
16233      to begin the parameter-declaration-clause, followed by either a
16234      `)', an `...', or a decl-specifier.  We need to check for a
16235      type-specifier to avoid being fooled into thinking that:
16236
16237        S::S (f) (int);
16238
16239      is a constructor.  (It is actually a function named `f' that
16240      takes one parameter (of type `int') and returns a value of type
16241      `S::S'.  */
16242   if (constructor_p
16243       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16244     {
16245       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16246           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16247           /* A parameter declaration begins with a decl-specifier,
16248              which is either the "attribute" keyword, a storage class
16249              specifier, or (usually) a type-specifier.  */
16250           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16251         {
16252           tree type;
16253           tree pushed_scope = NULL_TREE;
16254           unsigned saved_num_template_parameter_lists;
16255
16256           /* Names appearing in the type-specifier should be looked up
16257              in the scope of the class.  */
16258           if (current_class_type)
16259             type = NULL_TREE;
16260           else
16261             {
16262               type = TREE_TYPE (type_decl);
16263               if (TREE_CODE (type) == TYPENAME_TYPE)
16264                 {
16265                   type = resolve_typename_type (type,
16266                                                 /*only_current_p=*/false);
16267                   if (type == error_mark_node)
16268                     {
16269                       cp_parser_abort_tentative_parse (parser);
16270                       return false;
16271                     }
16272                 }
16273               pushed_scope = push_scope (type);
16274             }
16275
16276           /* Inside the constructor parameter list, surrounding
16277              template-parameter-lists do not apply.  */
16278           saved_num_template_parameter_lists
16279             = parser->num_template_parameter_lists;
16280           parser->num_template_parameter_lists = 0;
16281
16282           /* Look for the type-specifier.  */
16283           cp_parser_type_specifier (parser,
16284                                     CP_PARSER_FLAGS_NONE,
16285                                     /*decl_specs=*/NULL,
16286                                     /*is_declarator=*/true,
16287                                     /*declares_class_or_enum=*/NULL,
16288                                     /*is_cv_qualifier=*/NULL);
16289
16290           parser->num_template_parameter_lists
16291             = saved_num_template_parameter_lists;
16292
16293           /* Leave the scope of the class.  */
16294           if (pushed_scope)
16295             pop_scope (pushed_scope);
16296
16297           constructor_p = !cp_parser_error_occurred (parser);
16298         }
16299     }
16300   else
16301     constructor_p = false;
16302   /* We did not really want to consume any tokens.  */
16303   cp_parser_abort_tentative_parse (parser);
16304
16305   return constructor_p;
16306 }
16307
16308 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16309    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
16310    they must be performed once we are in the scope of the function.
16311
16312    Returns the function defined.  */
16313
16314 static tree
16315 cp_parser_function_definition_from_specifiers_and_declarator
16316   (cp_parser* parser,
16317    cp_decl_specifier_seq *decl_specifiers,
16318    tree attributes,
16319    const cp_declarator *declarator)
16320 {
16321   tree fn;
16322   bool success_p;
16323
16324   /* Begin the function-definition.  */
16325   success_p = start_function (decl_specifiers, declarator, attributes);
16326
16327   /* The things we're about to see are not directly qualified by any
16328      template headers we've seen thus far.  */
16329   reset_specialization ();
16330
16331   /* If there were names looked up in the decl-specifier-seq that we
16332      did not check, check them now.  We must wait until we are in the
16333      scope of the function to perform the checks, since the function
16334      might be a friend.  */
16335   perform_deferred_access_checks ();
16336
16337   if (!success_p)
16338     {
16339       /* Skip the entire function.  */
16340       cp_parser_skip_to_end_of_block_or_statement (parser);
16341       fn = error_mark_node;
16342     }
16343   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16344     {
16345       /* Seen already, skip it.  An error message has already been output.  */
16346       cp_parser_skip_to_end_of_block_or_statement (parser);
16347       fn = current_function_decl;
16348       current_function_decl = NULL_TREE;
16349       /* If this is a function from a class, pop the nested class.  */
16350       if (current_class_name)
16351         pop_nested_class ();
16352     }
16353   else
16354     fn = cp_parser_function_definition_after_declarator (parser,
16355                                                          /*inline_p=*/false);
16356
16357   return fn;
16358 }
16359
16360 /* Parse the part of a function-definition that follows the
16361    declarator.  INLINE_P is TRUE iff this function is an inline
16362    function defined with a class-specifier.
16363
16364    Returns the function defined.  */
16365
16366 static tree
16367 cp_parser_function_definition_after_declarator (cp_parser* parser,
16368                                                 bool inline_p)
16369 {
16370   tree fn;
16371   bool ctor_initializer_p = false;
16372   bool saved_in_unbraced_linkage_specification_p;
16373   bool saved_in_function_body;
16374   unsigned saved_num_template_parameter_lists;
16375
16376   saved_in_function_body = parser->in_function_body;
16377   parser->in_function_body = true;
16378   /* If the next token is `return', then the code may be trying to
16379      make use of the "named return value" extension that G++ used to
16380      support.  */
16381   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16382     {
16383       /* Consume the `return' keyword.  */
16384       cp_lexer_consume_token (parser->lexer);
16385       /* Look for the identifier that indicates what value is to be
16386          returned.  */
16387       cp_parser_identifier (parser);
16388       /* Issue an error message.  */
16389       error ("named return values are no longer supported");
16390       /* Skip tokens until we reach the start of the function body.  */
16391       while (true)
16392         {
16393           cp_token *token = cp_lexer_peek_token (parser->lexer);
16394           if (token->type == CPP_OPEN_BRACE
16395               || token->type == CPP_EOF
16396               || token->type == CPP_PRAGMA_EOL)
16397             break;
16398           cp_lexer_consume_token (parser->lexer);
16399         }
16400     }
16401   /* The `extern' in `extern "C" void f () { ... }' does not apply to
16402      anything declared inside `f'.  */
16403   saved_in_unbraced_linkage_specification_p
16404     = parser->in_unbraced_linkage_specification_p;
16405   parser->in_unbraced_linkage_specification_p = false;
16406   /* Inside the function, surrounding template-parameter-lists do not
16407      apply.  */
16408   saved_num_template_parameter_lists
16409     = parser->num_template_parameter_lists;
16410   parser->num_template_parameter_lists = 0;
16411   /* If the next token is `try', then we are looking at a
16412      function-try-block.  */
16413   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16414     ctor_initializer_p = cp_parser_function_try_block (parser);
16415   /* A function-try-block includes the function-body, so we only do
16416      this next part if we're not processing a function-try-block.  */
16417   else
16418     ctor_initializer_p
16419       = cp_parser_ctor_initializer_opt_and_function_body (parser);
16420
16421   /* Finish the function.  */
16422   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16423                         (inline_p ? 2 : 0));
16424   /* Generate code for it, if necessary.  */
16425   expand_or_defer_fn (fn);
16426   /* Restore the saved values.  */
16427   parser->in_unbraced_linkage_specification_p
16428     = saved_in_unbraced_linkage_specification_p;
16429   parser->num_template_parameter_lists
16430     = saved_num_template_parameter_lists;
16431   parser->in_function_body = saved_in_function_body;
16432
16433   return fn;
16434 }
16435
16436 /* Parse a template-declaration, assuming that the `export' (and
16437    `extern') keywords, if present, has already been scanned.  MEMBER_P
16438    is as for cp_parser_template_declaration.  */
16439
16440 static void
16441 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16442 {
16443   tree decl = NULL_TREE;
16444   VEC (deferred_access_check,gc) *checks;
16445   tree parameter_list;
16446   bool friend_p = false;
16447   bool need_lang_pop;
16448
16449   /* Look for the `template' keyword.  */
16450   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16451     return;
16452
16453   /* And the `<'.  */
16454   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16455     return;
16456   if (at_class_scope_p () && current_function_decl)
16457     {
16458       /* 14.5.2.2 [temp.mem]
16459
16460          A local class shall not have member templates.  */
16461       error ("invalid declaration of member template in local class");
16462       cp_parser_skip_to_end_of_block_or_statement (parser);
16463       return;
16464     }
16465   /* [temp]
16466
16467      A template ... shall not have C linkage.  */
16468   if (current_lang_name == lang_name_c)
16469     {
16470       error ("template with C linkage");
16471       /* Give it C++ linkage to avoid confusing other parts of the
16472          front end.  */
16473       push_lang_context (lang_name_cplusplus);
16474       need_lang_pop = true;
16475     }
16476   else
16477     need_lang_pop = false;
16478
16479   /* We cannot perform access checks on the template parameter
16480      declarations until we know what is being declared, just as we
16481      cannot check the decl-specifier list.  */
16482   push_deferring_access_checks (dk_deferred);
16483
16484   /* If the next token is `>', then we have an invalid
16485      specialization.  Rather than complain about an invalid template
16486      parameter, issue an error message here.  */
16487   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16488     {
16489       cp_parser_error (parser, "invalid explicit specialization");
16490       begin_specialization ();
16491       parameter_list = NULL_TREE;
16492     }
16493   else
16494     /* Parse the template parameters.  */
16495     parameter_list = cp_parser_template_parameter_list (parser);
16496
16497   /* Get the deferred access checks from the parameter list.  These
16498      will be checked once we know what is being declared, as for a
16499      member template the checks must be performed in the scope of the
16500      class containing the member.  */
16501   checks = get_deferred_access_checks ();
16502
16503   /* Look for the `>'.  */
16504   cp_parser_skip_to_end_of_template_parameter_list (parser);
16505   /* We just processed one more parameter list.  */
16506   ++parser->num_template_parameter_lists;
16507   /* If the next token is `template', there are more template
16508      parameters.  */
16509   if (cp_lexer_next_token_is_keyword (parser->lexer,
16510                                       RID_TEMPLATE))
16511     cp_parser_template_declaration_after_export (parser, member_p);
16512   else
16513     {
16514       /* There are no access checks when parsing a template, as we do not
16515          know if a specialization will be a friend.  */
16516       push_deferring_access_checks (dk_no_check);
16517       decl = cp_parser_single_declaration (parser,
16518                                            checks,
16519                                            member_p,
16520                                            /*explicit_specialization_p=*/false,
16521                                            &friend_p);
16522       pop_deferring_access_checks ();
16523
16524       /* If this is a member template declaration, let the front
16525          end know.  */
16526       if (member_p && !friend_p && decl)
16527         {
16528           if (TREE_CODE (decl) == TYPE_DECL)
16529             cp_parser_check_access_in_redeclaration (decl);
16530
16531           decl = finish_member_template_decl (decl);
16532         }
16533       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16534         make_friend_class (current_class_type, TREE_TYPE (decl),
16535                            /*complain=*/true);
16536     }
16537   /* We are done with the current parameter list.  */
16538   --parser->num_template_parameter_lists;
16539
16540   pop_deferring_access_checks ();
16541
16542   /* Finish up.  */
16543   finish_template_decl (parameter_list);
16544
16545   /* Register member declarations.  */
16546   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16547     finish_member_declaration (decl);
16548   /* For the erroneous case of a template with C linkage, we pushed an
16549      implicit C++ linkage scope; exit that scope now.  */
16550   if (need_lang_pop)
16551     pop_lang_context ();
16552   /* If DECL is a function template, we must return to parse it later.
16553      (Even though there is no definition, there might be default
16554      arguments that need handling.)  */
16555   if (member_p && decl
16556       && (TREE_CODE (decl) == FUNCTION_DECL
16557           || DECL_FUNCTION_TEMPLATE_P (decl)))
16558     TREE_VALUE (parser->unparsed_functions_queues)
16559       = tree_cons (NULL_TREE, decl,
16560                    TREE_VALUE (parser->unparsed_functions_queues));
16561 }
16562
16563 /* Perform the deferred access checks from a template-parameter-list.
16564    CHECKS is a TREE_LIST of access checks, as returned by
16565    get_deferred_access_checks.  */
16566
16567 static void
16568 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16569 {
16570   ++processing_template_parmlist;
16571   perform_access_checks (checks);
16572   --processing_template_parmlist;
16573 }
16574
16575 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16576    `function-definition' sequence.  MEMBER_P is true, this declaration
16577    appears in a class scope.
16578
16579    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
16580    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
16581
16582 static tree
16583 cp_parser_single_declaration (cp_parser* parser,
16584                               VEC (deferred_access_check,gc)* checks,
16585                               bool member_p,
16586                               bool explicit_specialization_p,
16587                               bool* friend_p)
16588 {
16589   int declares_class_or_enum;
16590   tree decl = NULL_TREE;
16591   cp_decl_specifier_seq decl_specifiers;
16592   bool function_definition_p = false;
16593
16594   /* This function is only used when processing a template
16595      declaration.  */
16596   gcc_assert (innermost_scope_kind () == sk_template_parms
16597               || innermost_scope_kind () == sk_template_spec);
16598
16599   /* Defer access checks until we know what is being declared.  */
16600   push_deferring_access_checks (dk_deferred);
16601
16602   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16603      alternative.  */
16604   cp_parser_decl_specifier_seq (parser,
16605                                 CP_PARSER_FLAGS_OPTIONAL,
16606                                 &decl_specifiers,
16607                                 &declares_class_or_enum);
16608   if (friend_p)
16609     *friend_p = cp_parser_friend_p (&decl_specifiers);
16610
16611   /* There are no template typedefs.  */
16612   if (decl_specifiers.specs[(int) ds_typedef])
16613     {
16614       error ("template declaration of %qs", "typedef");
16615       decl = error_mark_node;
16616     }
16617
16618   /* Gather up the access checks that occurred the
16619      decl-specifier-seq.  */
16620   stop_deferring_access_checks ();
16621
16622   /* Check for the declaration of a template class.  */
16623   if (declares_class_or_enum)
16624     {
16625       if (cp_parser_declares_only_class_p (parser))
16626         {
16627           decl = shadow_tag (&decl_specifiers);
16628
16629           /* In this case:
16630
16631                struct C {
16632                  friend template <typename T> struct A<T>::B;
16633                };
16634
16635              A<T>::B will be represented by a TYPENAME_TYPE, and
16636              therefore not recognized by shadow_tag.  */
16637           if (friend_p && *friend_p
16638               && !decl
16639               && decl_specifiers.type
16640               && TYPE_P (decl_specifiers.type))
16641             decl = decl_specifiers.type;
16642
16643           if (decl && decl != error_mark_node)
16644             decl = TYPE_NAME (decl);
16645           else
16646             decl = error_mark_node;
16647
16648           /* Perform access checks for template parameters.  */
16649           cp_parser_perform_template_parameter_access_checks (checks);
16650         }
16651     }
16652   /* If it's not a template class, try for a template function.  If
16653      the next token is a `;', then this declaration does not declare
16654      anything.  But, if there were errors in the decl-specifiers, then
16655      the error might well have come from an attempted class-specifier.
16656      In that case, there's no need to warn about a missing declarator.  */
16657   if (!decl
16658       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16659           || decl_specifiers.type != error_mark_node))
16660     {
16661       decl = cp_parser_init_declarator (parser,
16662                                         &decl_specifiers,
16663                                         checks,
16664                                         /*function_definition_allowed_p=*/true,
16665                                         member_p,
16666                                         declares_class_or_enum,
16667                                         &function_definition_p);
16668
16669     /* 7.1.1-1 [dcl.stc]
16670
16671        A storage-class-specifier shall not be specified in an explicit
16672        specialization...  */
16673     if (decl
16674         && explicit_specialization_p
16675         && decl_specifiers.storage_class != sc_none)
16676       {
16677         error ("explicit template specialization cannot have a storage class");
16678         decl = error_mark_node;
16679       }
16680     }
16681
16682   pop_deferring_access_checks ();
16683
16684   /* Clear any current qualification; whatever comes next is the start
16685      of something new.  */
16686   parser->scope = NULL_TREE;
16687   parser->qualifying_scope = NULL_TREE;
16688   parser->object_scope = NULL_TREE;
16689   /* Look for a trailing `;' after the declaration.  */
16690   if (!function_definition_p
16691       && (decl == error_mark_node
16692           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16693     cp_parser_skip_to_end_of_block_or_statement (parser);
16694
16695   return decl;
16696 }
16697
16698 /* Parse a cast-expression that is not the operand of a unary "&".  */
16699
16700 static tree
16701 cp_parser_simple_cast_expression (cp_parser *parser)
16702 {
16703   return cp_parser_cast_expression (parser, /*address_p=*/false,
16704                                     /*cast_p=*/false);
16705 }
16706
16707 /* Parse a functional cast to TYPE.  Returns an expression
16708    representing the cast.  */
16709
16710 static tree
16711 cp_parser_functional_cast (cp_parser* parser, tree type)
16712 {
16713   tree expression_list;
16714   tree cast;
16715
16716   expression_list
16717     = cp_parser_parenthesized_expression_list (parser, false,
16718                                                /*cast_p=*/true,
16719                                                /*allow_expansion_p=*/true,
16720                                                /*non_constant_p=*/NULL);
16721
16722   cast = build_functional_cast (type, expression_list);
16723   /* [expr.const]/1: In an integral constant expression "only type
16724      conversions to integral or enumeration type can be used".  */
16725   if (TREE_CODE (type) == TYPE_DECL)
16726     type = TREE_TYPE (type);
16727   if (cast != error_mark_node
16728       && !cast_valid_in_integral_constant_expression_p (type)
16729       && (cp_parser_non_integral_constant_expression
16730           (parser, "a call to a constructor")))
16731     return error_mark_node;
16732   return cast;
16733 }
16734
16735 /* Save the tokens that make up the body of a member function defined
16736    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
16737    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
16738    specifiers applied to the declaration.  Returns the FUNCTION_DECL
16739    for the member function.  */
16740
16741 static tree
16742 cp_parser_save_member_function_body (cp_parser* parser,
16743                                      cp_decl_specifier_seq *decl_specifiers,
16744                                      cp_declarator *declarator,
16745                                      tree attributes)
16746 {
16747   cp_token *first;
16748   cp_token *last;
16749   tree fn;
16750
16751   /* Create the function-declaration.  */
16752   fn = start_method (decl_specifiers, declarator, attributes);
16753   /* If something went badly wrong, bail out now.  */
16754   if (fn == error_mark_node)
16755     {
16756       /* If there's a function-body, skip it.  */
16757       if (cp_parser_token_starts_function_definition_p
16758           (cp_lexer_peek_token (parser->lexer)))
16759         cp_parser_skip_to_end_of_block_or_statement (parser);
16760       return error_mark_node;
16761     }
16762
16763   /* Remember it, if there default args to post process.  */
16764   cp_parser_save_default_args (parser, fn);
16765
16766   /* Save away the tokens that make up the body of the
16767      function.  */
16768   first = parser->lexer->next_token;
16769   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16770   /* Handle function try blocks.  */
16771   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16772     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16773   last = parser->lexer->next_token;
16774
16775   /* Save away the inline definition; we will process it when the
16776      class is complete.  */
16777   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16778   DECL_PENDING_INLINE_P (fn) = 1;
16779
16780   /* We need to know that this was defined in the class, so that
16781      friend templates are handled correctly.  */
16782   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16783
16784   /* We're done with the inline definition.  */
16785   finish_method (fn);
16786
16787   /* Add FN to the queue of functions to be parsed later.  */
16788   TREE_VALUE (parser->unparsed_functions_queues)
16789     = tree_cons (NULL_TREE, fn,
16790                  TREE_VALUE (parser->unparsed_functions_queues));
16791
16792   return fn;
16793 }
16794
16795 /* Parse a template-argument-list, as well as the trailing ">" (but
16796    not the opening ">").  See cp_parser_template_argument_list for the
16797    return value.  */
16798
16799 static tree
16800 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16801 {
16802   tree arguments;
16803   tree saved_scope;
16804   tree saved_qualifying_scope;
16805   tree saved_object_scope;
16806   bool saved_greater_than_is_operator_p;
16807   bool saved_skip_evaluation;
16808
16809   /* [temp.names]
16810
16811      When parsing a template-id, the first non-nested `>' is taken as
16812      the end of the template-argument-list rather than a greater-than
16813      operator.  */
16814   saved_greater_than_is_operator_p
16815     = parser->greater_than_is_operator_p;
16816   parser->greater_than_is_operator_p = false;
16817   /* Parsing the argument list may modify SCOPE, so we save it
16818      here.  */
16819   saved_scope = parser->scope;
16820   saved_qualifying_scope = parser->qualifying_scope;
16821   saved_object_scope = parser->object_scope;
16822   /* We need to evaluate the template arguments, even though this
16823      template-id may be nested within a "sizeof".  */
16824   saved_skip_evaluation = skip_evaluation;
16825   skip_evaluation = false;
16826   /* Parse the template-argument-list itself.  */
16827   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
16828       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16829     arguments = NULL_TREE;
16830   else
16831     arguments = cp_parser_template_argument_list (parser);
16832   /* Look for the `>' that ends the template-argument-list. If we find
16833      a '>>' instead, it's probably just a typo.  */
16834   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16835     {
16836       if (cxx_dialect != cxx98)
16837         {
16838           /* In C++0x, a `>>' in a template argument list or cast
16839              expression is considered to be two separate `>'
16840              tokens. So, change the current token to a `>', but don't
16841              consume it: it will be consumed later when the outer
16842              template argument list (or cast expression) is parsed.
16843              Note that this replacement of `>' for `>>' is necessary
16844              even if we are parsing tentatively: in the tentative
16845              case, after calling
16846              cp_parser_enclosed_template_argument_list we will always
16847              throw away all of the template arguments and the first
16848              closing `>', either because the template argument list
16849              was erroneous or because we are replacing those tokens
16850              with a CPP_TEMPLATE_ID token.  The second `>' (which will
16851              not have been thrown away) is needed either to close an
16852              outer template argument list or to complete a new-style
16853              cast.  */
16854           cp_token *token = cp_lexer_peek_token (parser->lexer);
16855           token->type = CPP_GREATER;
16856         }
16857       else if (!saved_greater_than_is_operator_p)
16858         {
16859           /* If we're in a nested template argument list, the '>>' has
16860             to be a typo for '> >'. We emit the error message, but we
16861             continue parsing and we push a '>' as next token, so that
16862             the argument list will be parsed correctly.  Note that the
16863             global source location is still on the token before the
16864             '>>', so we need to say explicitly where we want it.  */
16865           cp_token *token = cp_lexer_peek_token (parser->lexer);
16866           error ("%H%<>>%> should be %<> >%> "
16867                  "within a nested template argument list",
16868                  &token->location);
16869
16870           token->type = CPP_GREATER;
16871         }
16872       else
16873         {
16874           /* If this is not a nested template argument list, the '>>'
16875             is a typo for '>'. Emit an error message and continue.
16876             Same deal about the token location, but here we can get it
16877             right by consuming the '>>' before issuing the diagnostic.  */
16878           cp_lexer_consume_token (parser->lexer);
16879           error ("spurious %<>>%>, use %<>%> to terminate "
16880                  "a template argument list");
16881         }
16882     }
16883   else
16884     cp_parser_skip_to_end_of_template_parameter_list (parser);
16885   /* The `>' token might be a greater-than operator again now.  */
16886   parser->greater_than_is_operator_p
16887     = saved_greater_than_is_operator_p;
16888   /* Restore the SAVED_SCOPE.  */
16889   parser->scope = saved_scope;
16890   parser->qualifying_scope = saved_qualifying_scope;
16891   parser->object_scope = saved_object_scope;
16892   skip_evaluation = saved_skip_evaluation;
16893
16894   return arguments;
16895 }
16896
16897 /* MEMBER_FUNCTION is a member function, or a friend.  If default
16898    arguments, or the body of the function have not yet been parsed,
16899    parse them now.  */
16900
16901 static void
16902 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16903 {
16904   /* If this member is a template, get the underlying
16905      FUNCTION_DECL.  */
16906   if (DECL_FUNCTION_TEMPLATE_P (member_function))
16907     member_function = DECL_TEMPLATE_RESULT (member_function);
16908
16909   /* There should not be any class definitions in progress at this
16910      point; the bodies of members are only parsed outside of all class
16911      definitions.  */
16912   gcc_assert (parser->num_classes_being_defined == 0);
16913   /* While we're parsing the member functions we might encounter more
16914      classes.  We want to handle them right away, but we don't want
16915      them getting mixed up with functions that are currently in the
16916      queue.  */
16917   parser->unparsed_functions_queues
16918     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16919
16920   /* Make sure that any template parameters are in scope.  */
16921   maybe_begin_member_template_processing (member_function);
16922
16923   /* If the body of the function has not yet been parsed, parse it
16924      now.  */
16925   if (DECL_PENDING_INLINE_P (member_function))
16926     {
16927       tree function_scope;
16928       cp_token_cache *tokens;
16929
16930       /* The function is no longer pending; we are processing it.  */
16931       tokens = DECL_PENDING_INLINE_INFO (member_function);
16932       DECL_PENDING_INLINE_INFO (member_function) = NULL;
16933       DECL_PENDING_INLINE_P (member_function) = 0;
16934
16935       /* If this is a local class, enter the scope of the containing
16936          function.  */
16937       function_scope = current_function_decl;
16938       if (function_scope)
16939         push_function_context_to (function_scope);
16940
16941
16942       /* Push the body of the function onto the lexer stack.  */
16943       cp_parser_push_lexer_for_tokens (parser, tokens);
16944
16945       /* Let the front end know that we going to be defining this
16946          function.  */
16947       start_preparsed_function (member_function, NULL_TREE,
16948                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
16949
16950       /* Don't do access checking if it is a templated function.  */
16951       if (processing_template_decl)
16952         push_deferring_access_checks (dk_no_check);
16953
16954       /* Now, parse the body of the function.  */
16955       cp_parser_function_definition_after_declarator (parser,
16956                                                       /*inline_p=*/true);
16957
16958       if (processing_template_decl)
16959         pop_deferring_access_checks ();
16960
16961       /* Leave the scope of the containing function.  */
16962       if (function_scope)
16963         pop_function_context_from (function_scope);
16964       cp_parser_pop_lexer (parser);
16965     }
16966
16967   /* Remove any template parameters from the symbol table.  */
16968   maybe_end_member_template_processing ();
16969
16970   /* Restore the queue.  */
16971   parser->unparsed_functions_queues
16972     = TREE_CHAIN (parser->unparsed_functions_queues);
16973 }
16974
16975 /* If DECL contains any default args, remember it on the unparsed
16976    functions queue.  */
16977
16978 static void
16979 cp_parser_save_default_args (cp_parser* parser, tree decl)
16980 {
16981   tree probe;
16982
16983   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16984        probe;
16985        probe = TREE_CHAIN (probe))
16986     if (TREE_PURPOSE (probe))
16987       {
16988         TREE_PURPOSE (parser->unparsed_functions_queues)
16989           = tree_cons (current_class_type, decl,
16990                        TREE_PURPOSE (parser->unparsed_functions_queues));
16991         break;
16992       }
16993 }
16994
16995 /* FN is a FUNCTION_DECL which may contains a parameter with an
16996    unparsed DEFAULT_ARG.  Parse the default args now.  This function
16997    assumes that the current scope is the scope in which the default
16998    argument should be processed.  */
16999
17000 static void
17001 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17002 {
17003   bool saved_local_variables_forbidden_p;
17004   tree parm;
17005
17006   /* While we're parsing the default args, we might (due to the
17007      statement expression extension) encounter more classes.  We want
17008      to handle them right away, but we don't want them getting mixed
17009      up with default args that are currently in the queue.  */
17010   parser->unparsed_functions_queues
17011     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17012
17013   /* Local variable names (and the `this' keyword) may not appear
17014      in a default argument.  */
17015   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17016   parser->local_variables_forbidden_p = true;
17017
17018   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17019        parm;
17020        parm = TREE_CHAIN (parm))
17021     {
17022       cp_token_cache *tokens;
17023       tree default_arg = TREE_PURPOSE (parm);
17024       tree parsed_arg;
17025       VEC(tree,gc) *insts;
17026       tree copy;
17027       unsigned ix;
17028
17029       if (!default_arg)
17030         continue;
17031
17032       if (TREE_CODE (default_arg) != DEFAULT_ARG)
17033         /* This can happen for a friend declaration for a function
17034            already declared with default arguments.  */
17035         continue;
17036
17037        /* Push the saved tokens for the default argument onto the parser's
17038           lexer stack.  */
17039       tokens = DEFARG_TOKENS (default_arg);
17040       cp_parser_push_lexer_for_tokens (parser, tokens);
17041
17042       /* Parse the assignment-expression.  */
17043       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17044
17045       if (!processing_template_decl)
17046         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17047
17048       TREE_PURPOSE (parm) = parsed_arg;
17049
17050       /* Update any instantiations we've already created.  */
17051       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17052            VEC_iterate (tree, insts, ix, copy); ix++)
17053         TREE_PURPOSE (copy) = parsed_arg;
17054
17055       /* If the token stream has not been completely used up, then
17056          there was extra junk after the end of the default
17057          argument.  */
17058       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17059         cp_parser_error (parser, "expected %<,%>");
17060
17061       /* Revert to the main lexer.  */
17062       cp_parser_pop_lexer (parser);
17063     }
17064
17065   /* Make sure no default arg is missing.  */
17066   check_default_args (fn);
17067
17068   /* Restore the state of local_variables_forbidden_p.  */
17069   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17070
17071   /* Restore the queue.  */
17072   parser->unparsed_functions_queues
17073     = TREE_CHAIN (parser->unparsed_functions_queues);
17074 }
17075
17076 /* Parse the operand of `sizeof' (or a similar operator).  Returns
17077    either a TYPE or an expression, depending on the form of the
17078    input.  The KEYWORD indicates which kind of expression we have
17079    encountered.  */
17080
17081 static tree
17082 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17083 {
17084   static const char *format;
17085   tree expr = NULL_TREE;
17086   const char *saved_message;
17087   bool saved_integral_constant_expression_p;
17088   bool saved_non_integral_constant_expression_p;
17089   bool pack_expansion_p = false;
17090
17091   /* Initialize FORMAT the first time we get here.  */
17092   if (!format)
17093     format = "types may not be defined in '%s' expressions";
17094
17095   /* Types cannot be defined in a `sizeof' expression.  Save away the
17096      old message.  */
17097   saved_message = parser->type_definition_forbidden_message;
17098   /* And create the new one.  */
17099   parser->type_definition_forbidden_message
17100     = XNEWVEC (const char, strlen (format)
17101                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17102                + 1 /* `\0' */);
17103   sprintf ((char *) parser->type_definition_forbidden_message,
17104            format, IDENTIFIER_POINTER (ridpointers[keyword]));
17105
17106   /* The restrictions on constant-expressions do not apply inside
17107      sizeof expressions.  */
17108   saved_integral_constant_expression_p
17109     = parser->integral_constant_expression_p;
17110   saved_non_integral_constant_expression_p
17111     = parser->non_integral_constant_expression_p;
17112   parser->integral_constant_expression_p = false;
17113
17114   /* If it's a `...', then we are computing the length of a parameter
17115      pack.  */
17116   if (keyword == RID_SIZEOF
17117       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17118     {
17119       /* Consume the `...'.  */
17120       cp_lexer_consume_token (parser->lexer);
17121       maybe_warn_variadic_templates ();
17122
17123       /* Note that this is an expansion.  */
17124       pack_expansion_p = true;
17125     }
17126
17127   /* Do not actually evaluate the expression.  */
17128   ++skip_evaluation;
17129   /* If it's a `(', then we might be looking at the type-id
17130      construction.  */
17131   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17132     {
17133       tree type;
17134       bool saved_in_type_id_in_expr_p;
17135
17136       /* We can't be sure yet whether we're looking at a type-id or an
17137          expression.  */
17138       cp_parser_parse_tentatively (parser);
17139       /* Consume the `('.  */
17140       cp_lexer_consume_token (parser->lexer);
17141       /* Parse the type-id.  */
17142       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17143       parser->in_type_id_in_expr_p = true;
17144       type = cp_parser_type_id (parser);
17145       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17146       /* Now, look for the trailing `)'.  */
17147       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17148       /* If all went well, then we're done.  */
17149       if (cp_parser_parse_definitely (parser))
17150         {
17151           cp_decl_specifier_seq decl_specs;
17152
17153           /* Build a trivial decl-specifier-seq.  */
17154           clear_decl_specs (&decl_specs);
17155           decl_specs.type = type;
17156
17157           /* Call grokdeclarator to figure out what type this is.  */
17158           expr = grokdeclarator (NULL,
17159                                  &decl_specs,
17160                                  TYPENAME,
17161                                  /*initialized=*/0,
17162                                  /*attrlist=*/NULL);
17163         }
17164     }
17165
17166   /* If the type-id production did not work out, then we must be
17167      looking at the unary-expression production.  */
17168   if (!expr)
17169     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17170                                        /*cast_p=*/false);
17171
17172   if (pack_expansion_p)
17173     /* Build a pack expansion. */
17174     expr = make_pack_expansion (expr);
17175
17176   /* Go back to evaluating expressions.  */
17177   --skip_evaluation;
17178
17179   /* Free the message we created.  */
17180   free ((char *) parser->type_definition_forbidden_message);
17181   /* And restore the old one.  */
17182   parser->type_definition_forbidden_message = saved_message;
17183   parser->integral_constant_expression_p
17184     = saved_integral_constant_expression_p;
17185   parser->non_integral_constant_expression_p
17186     = saved_non_integral_constant_expression_p;
17187
17188   return expr;
17189 }
17190
17191 /* If the current declaration has no declarator, return true.  */
17192
17193 static bool
17194 cp_parser_declares_only_class_p (cp_parser *parser)
17195 {
17196   /* If the next token is a `;' or a `,' then there is no
17197      declarator.  */
17198   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17199           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17200 }
17201
17202 /* Update the DECL_SPECS to reflect the storage class indicated by
17203    KEYWORD.  */
17204
17205 static void
17206 cp_parser_set_storage_class (cp_parser *parser,
17207                              cp_decl_specifier_seq *decl_specs,
17208                              enum rid keyword)
17209 {
17210   cp_storage_class storage_class;
17211
17212   if (parser->in_unbraced_linkage_specification_p)
17213     {
17214       error ("invalid use of %qD in linkage specification",
17215              ridpointers[keyword]);
17216       return;
17217     }
17218   else if (decl_specs->storage_class != sc_none)
17219     {
17220       decl_specs->conflicting_specifiers_p = true;
17221       return;
17222     }
17223
17224   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17225       && decl_specs->specs[(int) ds_thread])
17226     {
17227       error ("%<__thread%> before %qD", ridpointers[keyword]);
17228       decl_specs->specs[(int) ds_thread] = 0;
17229     }
17230
17231   switch (keyword)
17232     {
17233     case RID_AUTO:
17234       storage_class = sc_auto;
17235       break;
17236     case RID_REGISTER:
17237       storage_class = sc_register;
17238       break;
17239     case RID_STATIC:
17240       storage_class = sc_static;
17241       break;
17242     case RID_EXTERN:
17243       storage_class = sc_extern;
17244       break;
17245     case RID_MUTABLE:
17246       storage_class = sc_mutable;
17247       break;
17248     default:
17249       gcc_unreachable ();
17250     }
17251   decl_specs->storage_class = storage_class;
17252
17253   /* A storage class specifier cannot be applied alongside a typedef 
17254      specifier. If there is a typedef specifier present then set 
17255      conflicting_specifiers_p which will trigger an error later
17256      on in grokdeclarator. */
17257   if (decl_specs->specs[(int)ds_typedef])
17258     decl_specs->conflicting_specifiers_p = true;
17259 }
17260
17261 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
17262    is true, the type is a user-defined type; otherwise it is a
17263    built-in type specified by a keyword.  */
17264
17265 static void
17266 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17267                               tree type_spec,
17268                               bool user_defined_p)
17269 {
17270   decl_specs->any_specifiers_p = true;
17271
17272   /* If the user tries to redeclare bool or wchar_t (with, for
17273      example, in "typedef int wchar_t;") we remember that this is what
17274      happened.  In system headers, we ignore these declarations so
17275      that G++ can work with system headers that are not C++-safe.  */
17276   if (decl_specs->specs[(int) ds_typedef]
17277       && !user_defined_p
17278       && (type_spec == boolean_type_node
17279           || type_spec == wchar_type_node)
17280       && (decl_specs->type
17281           || decl_specs->specs[(int) ds_long]
17282           || decl_specs->specs[(int) ds_short]
17283           || decl_specs->specs[(int) ds_unsigned]
17284           || decl_specs->specs[(int) ds_signed]))
17285     {
17286       decl_specs->redefined_builtin_type = type_spec;
17287       if (!decl_specs->type)
17288         {
17289           decl_specs->type = type_spec;
17290           decl_specs->user_defined_type_p = false;
17291         }
17292     }
17293   else if (decl_specs->type)
17294     decl_specs->multiple_types_p = true;
17295   else
17296     {
17297       decl_specs->type = type_spec;
17298       decl_specs->user_defined_type_p = user_defined_p;
17299       decl_specs->redefined_builtin_type = NULL_TREE;
17300     }
17301 }
17302
17303 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17304    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
17305
17306 static bool
17307 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17308 {
17309   return decl_specifiers->specs[(int) ds_friend] != 0;
17310 }
17311
17312 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
17313    issue an error message indicating that TOKEN_DESC was expected.
17314
17315    Returns the token consumed, if the token had the appropriate type.
17316    Otherwise, returns NULL.  */
17317
17318 static cp_token *
17319 cp_parser_require (cp_parser* parser,
17320                    enum cpp_ttype type,
17321                    const char* token_desc)
17322 {
17323   if (cp_lexer_next_token_is (parser->lexer, type))
17324     return cp_lexer_consume_token (parser->lexer);
17325   else
17326     {
17327       /* Output the MESSAGE -- unless we're parsing tentatively.  */
17328       if (!cp_parser_simulate_error (parser))
17329         {
17330           char *message = concat ("expected ", token_desc, NULL);
17331           cp_parser_error (parser, message);
17332           free (message);
17333         }
17334       return NULL;
17335     }
17336 }
17337
17338 /* An error message is produced if the next token is not '>'.
17339    All further tokens are skipped until the desired token is
17340    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
17341
17342 static void
17343 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17344 {
17345   /* Current level of '< ... >'.  */
17346   unsigned level = 0;
17347   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
17348   unsigned nesting_depth = 0;
17349
17350   /* Are we ready, yet?  If not, issue error message.  */
17351   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17352     return;
17353
17354   /* Skip tokens until the desired token is found.  */
17355   while (true)
17356     {
17357       /* Peek at the next token.  */
17358       switch (cp_lexer_peek_token (parser->lexer)->type)
17359         {
17360         case CPP_LESS:
17361           if (!nesting_depth)
17362             ++level;
17363           break;
17364
17365         case CPP_RSHIFT:
17366           if (cxx_dialect == cxx98)
17367             /* C++0x views the `>>' operator as two `>' tokens, but
17368                C++98 does not. */
17369             break;
17370           else if (!nesting_depth && level-- == 0)
17371             {
17372               /* We've hit a `>>' where the first `>' closes the
17373                  template argument list, and the second `>' is
17374                  spurious.  Just consume the `>>' and stop; we've
17375                  already produced at least one error.  */
17376               cp_lexer_consume_token (parser->lexer);
17377               return;
17378             }
17379           /* Fall through for C++0x, so we handle the second `>' in
17380              the `>>'.  */
17381
17382         case CPP_GREATER:
17383           if (!nesting_depth && level-- == 0)
17384             {
17385               /* We've reached the token we want, consume it and stop.  */
17386               cp_lexer_consume_token (parser->lexer);
17387               return;
17388             }
17389           break;
17390
17391         case CPP_OPEN_PAREN:
17392         case CPP_OPEN_SQUARE:
17393           ++nesting_depth;
17394           break;
17395
17396         case CPP_CLOSE_PAREN:
17397         case CPP_CLOSE_SQUARE:
17398           if (nesting_depth-- == 0)
17399             return;
17400           break;
17401
17402         case CPP_EOF:
17403         case CPP_PRAGMA_EOL:
17404         case CPP_SEMICOLON:
17405         case CPP_OPEN_BRACE:
17406         case CPP_CLOSE_BRACE:
17407           /* The '>' was probably forgotten, don't look further.  */
17408           return;
17409
17410         default:
17411           break;
17412         }
17413
17414       /* Consume this token.  */
17415       cp_lexer_consume_token (parser->lexer);
17416     }
17417 }
17418
17419 /* If the next token is the indicated keyword, consume it.  Otherwise,
17420    issue an error message indicating that TOKEN_DESC was expected.
17421
17422    Returns the token consumed, if the token had the appropriate type.
17423    Otherwise, returns NULL.  */
17424
17425 static cp_token *
17426 cp_parser_require_keyword (cp_parser* parser,
17427                            enum rid keyword,
17428                            const char* token_desc)
17429 {
17430   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17431
17432   if (token && token->keyword != keyword)
17433     {
17434       dyn_string_t error_msg;
17435
17436       /* Format the error message.  */
17437       error_msg = dyn_string_new (0);
17438       dyn_string_append_cstr (error_msg, "expected ");
17439       dyn_string_append_cstr (error_msg, token_desc);
17440       cp_parser_error (parser, error_msg->s);
17441       dyn_string_delete (error_msg);
17442       return NULL;
17443     }
17444
17445   return token;
17446 }
17447
17448 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17449    function-definition.  */
17450
17451 static bool
17452 cp_parser_token_starts_function_definition_p (cp_token* token)
17453 {
17454   return (/* An ordinary function-body begins with an `{'.  */
17455           token->type == CPP_OPEN_BRACE
17456           /* A ctor-initializer begins with a `:'.  */
17457           || token->type == CPP_COLON
17458           /* A function-try-block begins with `try'.  */
17459           || token->keyword == RID_TRY
17460           /* The named return value extension begins with `return'.  */
17461           || token->keyword == RID_RETURN);
17462 }
17463
17464 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17465    definition.  */
17466
17467 static bool
17468 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17469 {
17470   cp_token *token;
17471
17472   token = cp_lexer_peek_token (parser->lexer);
17473   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17474 }
17475
17476 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17477    C++0x) ending a template-argument.  */
17478
17479 static bool
17480 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17481 {
17482   cp_token *token;
17483
17484   token = cp_lexer_peek_token (parser->lexer);
17485   return (token->type == CPP_COMMA 
17486           || token->type == CPP_GREATER
17487           || token->type == CPP_ELLIPSIS
17488           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17489 }
17490
17491 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17492    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
17493
17494 static bool
17495 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17496                                                      size_t n)
17497 {
17498   cp_token *token;
17499
17500   token = cp_lexer_peek_nth_token (parser->lexer, n);
17501   if (token->type == CPP_LESS)
17502     return true;
17503   /* Check for the sequence `<::' in the original code. It would be lexed as
17504      `[:', where `[' is a digraph, and there is no whitespace before
17505      `:'.  */
17506   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17507     {
17508       cp_token *token2;
17509       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17510       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17511         return true;
17512     }
17513   return false;
17514 }
17515
17516 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17517    or none_type otherwise.  */
17518
17519 static enum tag_types
17520 cp_parser_token_is_class_key (cp_token* token)
17521 {
17522   switch (token->keyword)
17523     {
17524     case RID_CLASS:
17525       return class_type;
17526     case RID_STRUCT:
17527       return record_type;
17528     case RID_UNION:
17529       return union_type;
17530
17531     default:
17532       return none_type;
17533     }
17534 }
17535
17536 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
17537
17538 static void
17539 cp_parser_check_class_key (enum tag_types class_key, tree type)
17540 {
17541   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17542     pedwarn ("%qs tag used in naming %q#T",
17543             class_key == union_type ? "union"
17544              : class_key == record_type ? "struct" : "class",
17545              type);
17546 }
17547
17548 /* Issue an error message if DECL is redeclared with different
17549    access than its original declaration [class.access.spec/3].
17550    This applies to nested classes and nested class templates.
17551    [class.mem/1].  */
17552
17553 static void
17554 cp_parser_check_access_in_redeclaration (tree decl)
17555 {
17556   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17557     return;
17558
17559   if ((TREE_PRIVATE (decl)
17560        != (current_access_specifier == access_private_node))
17561       || (TREE_PROTECTED (decl)
17562           != (current_access_specifier == access_protected_node)))
17563     error ("%qD redeclared with different access", decl);
17564 }
17565
17566 /* Look for the `template' keyword, as a syntactic disambiguator.
17567    Return TRUE iff it is present, in which case it will be
17568    consumed.  */
17569
17570 static bool
17571 cp_parser_optional_template_keyword (cp_parser *parser)
17572 {
17573   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17574     {
17575       /* The `template' keyword can only be used within templates;
17576          outside templates the parser can always figure out what is a
17577          template and what is not.  */
17578       if (!processing_template_decl)
17579         {
17580           error ("%<template%> (as a disambiguator) is only allowed "
17581                  "within templates");
17582           /* If this part of the token stream is rescanned, the same
17583              error message would be generated.  So, we purge the token
17584              from the stream.  */
17585           cp_lexer_purge_token (parser->lexer);
17586           return false;
17587         }
17588       else
17589         {
17590           /* Consume the `template' keyword.  */
17591           cp_lexer_consume_token (parser->lexer);
17592           return true;
17593         }
17594     }
17595
17596   return false;
17597 }
17598
17599 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
17600    set PARSER->SCOPE, and perform other related actions.  */
17601
17602 static void
17603 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17604 {
17605   int i;
17606   struct tree_check *check_value;
17607   deferred_access_check *chk;
17608   VEC (deferred_access_check,gc) *checks;
17609
17610   /* Get the stored value.  */
17611   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17612   /* Perform any access checks that were deferred.  */
17613   checks = check_value->checks;
17614   if (checks)
17615     {
17616       for (i = 0 ;
17617            VEC_iterate (deferred_access_check, checks, i, chk) ;
17618            ++i)
17619         {
17620           perform_or_defer_access_check (chk->binfo,
17621                                          chk->decl,
17622                                          chk->diag_decl);
17623         }
17624     }
17625   /* Set the scope from the stored value.  */
17626   parser->scope = check_value->value;
17627   parser->qualifying_scope = check_value->qualifying_scope;
17628   parser->object_scope = NULL_TREE;
17629 }
17630
17631 /* Consume tokens up through a non-nested END token.  */
17632
17633 static void
17634 cp_parser_cache_group (cp_parser *parser,
17635                        enum cpp_ttype end,
17636                        unsigned depth)
17637 {
17638   while (true)
17639     {
17640       cp_token *token;
17641
17642       /* Abort a parenthesized expression if we encounter a brace.  */
17643       if ((end == CPP_CLOSE_PAREN || depth == 0)
17644           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17645         return;
17646       /* If we've reached the end of the file, stop.  */
17647       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17648           || (end != CPP_PRAGMA_EOL
17649               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17650         return;
17651       /* Consume the next token.  */
17652       token = cp_lexer_consume_token (parser->lexer);
17653       /* See if it starts a new group.  */
17654       if (token->type == CPP_OPEN_BRACE)
17655         {
17656           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17657           if (depth == 0)
17658             return;
17659         }
17660       else if (token->type == CPP_OPEN_PAREN)
17661         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17662       else if (token->type == CPP_PRAGMA)
17663         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17664       else if (token->type == end)
17665         return;
17666     }
17667 }
17668
17669 /* Begin parsing tentatively.  We always save tokens while parsing
17670    tentatively so that if the tentative parsing fails we can restore the
17671    tokens.  */
17672
17673 static void
17674 cp_parser_parse_tentatively (cp_parser* parser)
17675 {
17676   /* Enter a new parsing context.  */
17677   parser->context = cp_parser_context_new (parser->context);
17678   /* Begin saving tokens.  */
17679   cp_lexer_save_tokens (parser->lexer);
17680   /* In order to avoid repetitive access control error messages,
17681      access checks are queued up until we are no longer parsing
17682      tentatively.  */
17683   push_deferring_access_checks (dk_deferred);
17684 }
17685
17686 /* Commit to the currently active tentative parse.  */
17687
17688 static void
17689 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17690 {
17691   cp_parser_context *context;
17692   cp_lexer *lexer;
17693
17694   /* Mark all of the levels as committed.  */
17695   lexer = parser->lexer;
17696   for (context = parser->context; context->next; context = context->next)
17697     {
17698       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17699         break;
17700       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17701       while (!cp_lexer_saving_tokens (lexer))
17702         lexer = lexer->next;
17703       cp_lexer_commit_tokens (lexer);
17704     }
17705 }
17706
17707 /* Abort the currently active tentative parse.  All consumed tokens
17708    will be rolled back, and no diagnostics will be issued.  */
17709
17710 static void
17711 cp_parser_abort_tentative_parse (cp_parser* parser)
17712 {
17713   cp_parser_simulate_error (parser);
17714   /* Now, pretend that we want to see if the construct was
17715      successfully parsed.  */
17716   cp_parser_parse_definitely (parser);
17717 }
17718
17719 /* Stop parsing tentatively.  If a parse error has occurred, restore the
17720    token stream.  Otherwise, commit to the tokens we have consumed.
17721    Returns true if no error occurred; false otherwise.  */
17722
17723 static bool
17724 cp_parser_parse_definitely (cp_parser* parser)
17725 {
17726   bool error_occurred;
17727   cp_parser_context *context;
17728
17729   /* Remember whether or not an error occurred, since we are about to
17730      destroy that information.  */
17731   error_occurred = cp_parser_error_occurred (parser);
17732   /* Remove the topmost context from the stack.  */
17733   context = parser->context;
17734   parser->context = context->next;
17735   /* If no parse errors occurred, commit to the tentative parse.  */
17736   if (!error_occurred)
17737     {
17738       /* Commit to the tokens read tentatively, unless that was
17739          already done.  */
17740       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17741         cp_lexer_commit_tokens (parser->lexer);
17742
17743       pop_to_parent_deferring_access_checks ();
17744     }
17745   /* Otherwise, if errors occurred, roll back our state so that things
17746      are just as they were before we began the tentative parse.  */
17747   else
17748     {
17749       cp_lexer_rollback_tokens (parser->lexer);
17750       pop_deferring_access_checks ();
17751     }
17752   /* Add the context to the front of the free list.  */
17753   context->next = cp_parser_context_free_list;
17754   cp_parser_context_free_list = context;
17755
17756   return !error_occurred;
17757 }
17758
17759 /* Returns true if we are parsing tentatively and are not committed to
17760    this tentative parse.  */
17761
17762 static bool
17763 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17764 {
17765   return (cp_parser_parsing_tentatively (parser)
17766           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17767 }
17768
17769 /* Returns nonzero iff an error has occurred during the most recent
17770    tentative parse.  */
17771
17772 static bool
17773 cp_parser_error_occurred (cp_parser* parser)
17774 {
17775   return (cp_parser_parsing_tentatively (parser)
17776           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17777 }
17778
17779 /* Returns nonzero if GNU extensions are allowed.  */
17780
17781 static bool
17782 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17783 {
17784   return parser->allow_gnu_extensions_p;
17785 }
17786 \f
17787 /* Objective-C++ Productions */
17788
17789
17790 /* Parse an Objective-C expression, which feeds into a primary-expression
17791    above.
17792
17793    objc-expression:
17794      objc-message-expression
17795      objc-string-literal
17796      objc-encode-expression
17797      objc-protocol-expression
17798      objc-selector-expression
17799
17800   Returns a tree representation of the expression.  */
17801
17802 static tree
17803 cp_parser_objc_expression (cp_parser* parser)
17804 {
17805   /* Try to figure out what kind of declaration is present.  */
17806   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17807
17808   switch (kwd->type)
17809     {
17810     case CPP_OPEN_SQUARE:
17811       return cp_parser_objc_message_expression (parser);
17812
17813     case CPP_OBJC_STRING:
17814       kwd = cp_lexer_consume_token (parser->lexer);
17815       return objc_build_string_object (kwd->u.value);
17816
17817     case CPP_KEYWORD:
17818       switch (kwd->keyword)
17819         {
17820         case RID_AT_ENCODE:
17821           return cp_parser_objc_encode_expression (parser);
17822
17823         case RID_AT_PROTOCOL:
17824           return cp_parser_objc_protocol_expression (parser);
17825
17826         case RID_AT_SELECTOR:
17827           return cp_parser_objc_selector_expression (parser);
17828
17829         default:
17830           break;
17831         }
17832     default:
17833       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17834       cp_parser_skip_to_end_of_block_or_statement (parser);
17835     }
17836
17837   return error_mark_node;
17838 }
17839
17840 /* Parse an Objective-C message expression.
17841
17842    objc-message-expression:
17843      [ objc-message-receiver objc-message-args ]
17844
17845    Returns a representation of an Objective-C message.  */
17846
17847 static tree
17848 cp_parser_objc_message_expression (cp_parser* parser)
17849 {
17850   tree receiver, messageargs;
17851
17852   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
17853   receiver = cp_parser_objc_message_receiver (parser);
17854   messageargs = cp_parser_objc_message_args (parser);
17855   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17856
17857   return objc_build_message_expr (build_tree_list (receiver, messageargs));
17858 }
17859
17860 /* Parse an objc-message-receiver.
17861
17862    objc-message-receiver:
17863      expression
17864      simple-type-specifier
17865
17866   Returns a representation of the type or expression.  */
17867
17868 static tree
17869 cp_parser_objc_message_receiver (cp_parser* parser)
17870 {
17871   tree rcv;
17872
17873   /* An Objective-C message receiver may be either (1) a type
17874      or (2) an expression.  */
17875   cp_parser_parse_tentatively (parser);
17876   rcv = cp_parser_expression (parser, false);
17877
17878   if (cp_parser_parse_definitely (parser))
17879     return rcv;
17880
17881   rcv = cp_parser_simple_type_specifier (parser,
17882                                          /*decl_specs=*/NULL,
17883                                          CP_PARSER_FLAGS_NONE);
17884
17885   return objc_get_class_reference (rcv);
17886 }
17887
17888 /* Parse the arguments and selectors comprising an Objective-C message.
17889
17890    objc-message-args:
17891      objc-selector
17892      objc-selector-args
17893      objc-selector-args , objc-comma-args
17894
17895    objc-selector-args:
17896      objc-selector [opt] : assignment-expression
17897      objc-selector-args objc-selector [opt] : assignment-expression
17898
17899    objc-comma-args:
17900      assignment-expression
17901      objc-comma-args , assignment-expression
17902
17903    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17904    selector arguments and TREE_VALUE containing a list of comma
17905    arguments.  */
17906
17907 static tree
17908 cp_parser_objc_message_args (cp_parser* parser)
17909 {
17910   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17911   bool maybe_unary_selector_p = true;
17912   cp_token *token = cp_lexer_peek_token (parser->lexer);
17913
17914   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17915     {
17916       tree selector = NULL_TREE, arg;
17917
17918       if (token->type != CPP_COLON)
17919         selector = cp_parser_objc_selector (parser);
17920
17921       /* Detect if we have a unary selector.  */
17922       if (maybe_unary_selector_p
17923           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17924         return build_tree_list (selector, NULL_TREE);
17925
17926       maybe_unary_selector_p = false;
17927       cp_parser_require (parser, CPP_COLON, "`:'");
17928       arg = cp_parser_assignment_expression (parser, false);
17929
17930       sel_args
17931         = chainon (sel_args,
17932                    build_tree_list (selector, arg));
17933
17934       token = cp_lexer_peek_token (parser->lexer);
17935     }
17936
17937   /* Handle non-selector arguments, if any. */
17938   while (token->type == CPP_COMMA)
17939     {
17940       tree arg;
17941
17942       cp_lexer_consume_token (parser->lexer);
17943       arg = cp_parser_assignment_expression (parser, false);
17944
17945       addl_args
17946         = chainon (addl_args,
17947                    build_tree_list (NULL_TREE, arg));
17948
17949       token = cp_lexer_peek_token (parser->lexer);
17950     }
17951
17952   return build_tree_list (sel_args, addl_args);
17953 }
17954
17955 /* Parse an Objective-C encode expression.
17956
17957    objc-encode-expression:
17958      @encode objc-typename
17959
17960    Returns an encoded representation of the type argument.  */
17961
17962 static tree
17963 cp_parser_objc_encode_expression (cp_parser* parser)
17964 {
17965   tree type;
17966
17967   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
17968   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17969   type = complete_type (cp_parser_type_id (parser));
17970   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17971
17972   if (!type)
17973     {
17974       error ("%<@encode%> must specify a type as an argument");
17975       return error_mark_node;
17976     }
17977
17978   return objc_build_encode_expr (type);
17979 }
17980
17981 /* Parse an Objective-C @defs expression.  */
17982
17983 static tree
17984 cp_parser_objc_defs_expression (cp_parser *parser)
17985 {
17986   tree name;
17987
17988   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
17989   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17990   name = cp_parser_identifier (parser);
17991   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17992
17993   return objc_get_class_ivars (name);
17994 }
17995
17996 /* Parse an Objective-C protocol expression.
17997
17998   objc-protocol-expression:
17999     @protocol ( identifier )
18000
18001   Returns a representation of the protocol expression.  */
18002
18003 static tree
18004 cp_parser_objc_protocol_expression (cp_parser* parser)
18005 {
18006   tree proto;
18007
18008   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18009   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18010   proto = cp_parser_identifier (parser);
18011   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18012
18013   return objc_build_protocol_expr (proto);
18014 }
18015
18016 /* Parse an Objective-C selector expression.
18017
18018    objc-selector-expression:
18019      @selector ( objc-method-signature )
18020
18021    objc-method-signature:
18022      objc-selector
18023      objc-selector-seq
18024
18025    objc-selector-seq:
18026      objc-selector :
18027      objc-selector-seq objc-selector :
18028
18029   Returns a representation of the method selector.  */
18030
18031 static tree
18032 cp_parser_objc_selector_expression (cp_parser* parser)
18033 {
18034   tree sel_seq = NULL_TREE;
18035   bool maybe_unary_selector_p = true;
18036   cp_token *token;
18037
18038   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
18039   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18040   token = cp_lexer_peek_token (parser->lexer);
18041
18042   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18043          || token->type == CPP_SCOPE)
18044     {
18045       tree selector = NULL_TREE;
18046
18047       if (token->type != CPP_COLON
18048           || token->type == CPP_SCOPE)
18049         selector = cp_parser_objc_selector (parser);
18050
18051       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18052           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18053         {
18054           /* Detect if we have a unary selector.  */
18055           if (maybe_unary_selector_p)
18056             {
18057               sel_seq = selector;
18058               goto finish_selector;
18059             }
18060           else
18061             {
18062               cp_parser_error (parser, "expected %<:%>");
18063             }
18064         }
18065       maybe_unary_selector_p = false;
18066       token = cp_lexer_consume_token (parser->lexer);
18067
18068       if (token->type == CPP_SCOPE)
18069         {
18070           sel_seq
18071             = chainon (sel_seq,
18072                        build_tree_list (selector, NULL_TREE));
18073           sel_seq
18074             = chainon (sel_seq,
18075                        build_tree_list (NULL_TREE, NULL_TREE));
18076         }
18077       else
18078         sel_seq
18079           = chainon (sel_seq,
18080                      build_tree_list (selector, NULL_TREE));
18081
18082       token = cp_lexer_peek_token (parser->lexer);
18083     }
18084
18085  finish_selector:
18086   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18087
18088   return objc_build_selector_expr (sel_seq);
18089 }
18090
18091 /* Parse a list of identifiers.
18092
18093    objc-identifier-list:
18094      identifier
18095      objc-identifier-list , identifier
18096
18097    Returns a TREE_LIST of identifier nodes.  */
18098
18099 static tree
18100 cp_parser_objc_identifier_list (cp_parser* parser)
18101 {
18102   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18103   cp_token *sep = cp_lexer_peek_token (parser->lexer);
18104
18105   while (sep->type == CPP_COMMA)
18106     {
18107       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18108       list = chainon (list,
18109                       build_tree_list (NULL_TREE,
18110                                        cp_parser_identifier (parser)));
18111       sep = cp_lexer_peek_token (parser->lexer);
18112     }
18113
18114   return list;
18115 }
18116
18117 /* Parse an Objective-C alias declaration.
18118
18119    objc-alias-declaration:
18120      @compatibility_alias identifier identifier ;
18121
18122    This function registers the alias mapping with the Objective-C front end.
18123    It returns nothing.  */
18124
18125 static void
18126 cp_parser_objc_alias_declaration (cp_parser* parser)
18127 {
18128   tree alias, orig;
18129
18130   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
18131   alias = cp_parser_identifier (parser);
18132   orig = cp_parser_identifier (parser);
18133   objc_declare_alias (alias, orig);
18134   cp_parser_consume_semicolon_at_end_of_statement (parser);
18135 }
18136
18137 /* Parse an Objective-C class forward-declaration.
18138
18139    objc-class-declaration:
18140      @class objc-identifier-list ;
18141
18142    The function registers the forward declarations with the Objective-C
18143    front end.  It returns nothing.  */
18144
18145 static void
18146 cp_parser_objc_class_declaration (cp_parser* parser)
18147 {
18148   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
18149   objc_declare_class (cp_parser_objc_identifier_list (parser));
18150   cp_parser_consume_semicolon_at_end_of_statement (parser);
18151 }
18152
18153 /* Parse a list of Objective-C protocol references.
18154
18155    objc-protocol-refs-opt:
18156      objc-protocol-refs [opt]
18157
18158    objc-protocol-refs:
18159      < objc-identifier-list >
18160
18161    Returns a TREE_LIST of identifiers, if any.  */
18162
18163 static tree
18164 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18165 {
18166   tree protorefs = NULL_TREE;
18167
18168   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18169     {
18170       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
18171       protorefs = cp_parser_objc_identifier_list (parser);
18172       cp_parser_require (parser, CPP_GREATER, "`>'");
18173     }
18174
18175   return protorefs;
18176 }
18177
18178 /* Parse a Objective-C visibility specification.  */
18179
18180 static void
18181 cp_parser_objc_visibility_spec (cp_parser* parser)
18182 {
18183   cp_token *vis = cp_lexer_peek_token (parser->lexer);
18184
18185   switch (vis->keyword)
18186     {
18187     case RID_AT_PRIVATE:
18188       objc_set_visibility (2);
18189       break;
18190     case RID_AT_PROTECTED:
18191       objc_set_visibility (0);
18192       break;
18193     case RID_AT_PUBLIC:
18194       objc_set_visibility (1);
18195       break;
18196     default:
18197       return;
18198     }
18199
18200   /* Eat '@private'/'@protected'/'@public'.  */
18201   cp_lexer_consume_token (parser->lexer);
18202 }
18203
18204 /* Parse an Objective-C method type.  */
18205
18206 static void
18207 cp_parser_objc_method_type (cp_parser* parser)
18208 {
18209   objc_set_method_type
18210    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18211     ? PLUS_EXPR
18212     : MINUS_EXPR);
18213 }
18214
18215 /* Parse an Objective-C protocol qualifier.  */
18216
18217 static tree
18218 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18219 {
18220   tree quals = NULL_TREE, node;
18221   cp_token *token = cp_lexer_peek_token (parser->lexer);
18222
18223   node = token->u.value;
18224
18225   while (node && TREE_CODE (node) == IDENTIFIER_NODE
18226          && (node == ridpointers [(int) RID_IN]
18227              || node == ridpointers [(int) RID_OUT]
18228              || node == ridpointers [(int) RID_INOUT]
18229              || node == ridpointers [(int) RID_BYCOPY]
18230              || node == ridpointers [(int) RID_BYREF]
18231              || node == ridpointers [(int) RID_ONEWAY]))
18232     {
18233       quals = tree_cons (NULL_TREE, node, quals);
18234       cp_lexer_consume_token (parser->lexer);
18235       token = cp_lexer_peek_token (parser->lexer);
18236       node = token->u.value;
18237     }
18238
18239   return quals;
18240 }
18241
18242 /* Parse an Objective-C typename.  */
18243
18244 static tree
18245 cp_parser_objc_typename (cp_parser* parser)
18246 {
18247   tree typename = NULL_TREE;
18248
18249   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18250     {
18251       tree proto_quals, cp_type = NULL_TREE;
18252
18253       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18254       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18255
18256       /* An ObjC type name may consist of just protocol qualifiers, in which
18257          case the type shall default to 'id'.  */
18258       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18259         cp_type = cp_parser_type_id (parser);
18260
18261       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18262       typename = build_tree_list (proto_quals, cp_type);
18263     }
18264
18265   return typename;
18266 }
18267
18268 /* Check to see if TYPE refers to an Objective-C selector name.  */
18269
18270 static bool
18271 cp_parser_objc_selector_p (enum cpp_ttype type)
18272 {
18273   return (type == CPP_NAME || type == CPP_KEYWORD
18274           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18275           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18276           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18277           || type == CPP_XOR || type == CPP_XOR_EQ);
18278 }
18279
18280 /* Parse an Objective-C selector.  */
18281
18282 static tree
18283 cp_parser_objc_selector (cp_parser* parser)
18284 {
18285   cp_token *token = cp_lexer_consume_token (parser->lexer);
18286
18287   if (!cp_parser_objc_selector_p (token->type))
18288     {
18289       error ("invalid Objective-C++ selector name");
18290       return error_mark_node;
18291     }
18292
18293   /* C++ operator names are allowed to appear in ObjC selectors.  */
18294   switch (token->type)
18295     {
18296     case CPP_AND_AND: return get_identifier ("and");
18297     case CPP_AND_EQ: return get_identifier ("and_eq");
18298     case CPP_AND: return get_identifier ("bitand");
18299     case CPP_OR: return get_identifier ("bitor");
18300     case CPP_COMPL: return get_identifier ("compl");
18301     case CPP_NOT: return get_identifier ("not");
18302     case CPP_NOT_EQ: return get_identifier ("not_eq");
18303     case CPP_OR_OR: return get_identifier ("or");
18304     case CPP_OR_EQ: return get_identifier ("or_eq");
18305     case CPP_XOR: return get_identifier ("xor");
18306     case CPP_XOR_EQ: return get_identifier ("xor_eq");
18307     default: return token->u.value;
18308     }
18309 }
18310
18311 /* Parse an Objective-C params list.  */
18312
18313 static tree
18314 cp_parser_objc_method_keyword_params (cp_parser* parser)
18315 {
18316   tree params = NULL_TREE;
18317   bool maybe_unary_selector_p = true;
18318   cp_token *token = cp_lexer_peek_token (parser->lexer);
18319
18320   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18321     {
18322       tree selector = NULL_TREE, typename, identifier;
18323
18324       if (token->type != CPP_COLON)
18325         selector = cp_parser_objc_selector (parser);
18326
18327       /* Detect if we have a unary selector.  */
18328       if (maybe_unary_selector_p
18329           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18330         return selector;
18331
18332       maybe_unary_selector_p = false;
18333       cp_parser_require (parser, CPP_COLON, "`:'");
18334       typename = cp_parser_objc_typename (parser);
18335       identifier = cp_parser_identifier (parser);
18336
18337       params
18338         = chainon (params,
18339                    objc_build_keyword_decl (selector,
18340                                             typename,
18341                                             identifier));
18342
18343       token = cp_lexer_peek_token (parser->lexer);
18344     }
18345
18346   return params;
18347 }
18348
18349 /* Parse the non-keyword Objective-C params.  */
18350
18351 static tree
18352 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18353 {
18354   tree params = make_node (TREE_LIST);
18355   cp_token *token = cp_lexer_peek_token (parser->lexer);
18356   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
18357
18358   while (token->type == CPP_COMMA)
18359     {
18360       cp_parameter_declarator *parmdecl;
18361       tree parm;
18362
18363       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18364       token = cp_lexer_peek_token (parser->lexer);
18365
18366       if (token->type == CPP_ELLIPSIS)
18367         {
18368           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
18369           *ellipsisp = true;
18370           break;
18371         }
18372
18373       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18374       parm = grokdeclarator (parmdecl->declarator,
18375                              &parmdecl->decl_specifiers,
18376                              PARM, /*initialized=*/0,
18377                              /*attrlist=*/NULL);
18378
18379       chainon (params, build_tree_list (NULL_TREE, parm));
18380       token = cp_lexer_peek_token (parser->lexer);
18381     }
18382
18383   return params;
18384 }
18385
18386 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
18387
18388 static void
18389 cp_parser_objc_interstitial_code (cp_parser* parser)
18390 {
18391   cp_token *token = cp_lexer_peek_token (parser->lexer);
18392
18393   /* If the next token is `extern' and the following token is a string
18394      literal, then we have a linkage specification.  */
18395   if (token->keyword == RID_EXTERN
18396       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18397     cp_parser_linkage_specification (parser);
18398   /* Handle #pragma, if any.  */
18399   else if (token->type == CPP_PRAGMA)
18400     cp_parser_pragma (parser, pragma_external);
18401   /* Allow stray semicolons.  */
18402   else if (token->type == CPP_SEMICOLON)
18403     cp_lexer_consume_token (parser->lexer);
18404   /* Finally, try to parse a block-declaration, or a function-definition.  */
18405   else
18406     cp_parser_block_declaration (parser, /*statement_p=*/false);
18407 }
18408
18409 /* Parse a method signature.  */
18410
18411 static tree
18412 cp_parser_objc_method_signature (cp_parser* parser)
18413 {
18414   tree rettype, kwdparms, optparms;
18415   bool ellipsis = false;
18416
18417   cp_parser_objc_method_type (parser);
18418   rettype = cp_parser_objc_typename (parser);
18419   kwdparms = cp_parser_objc_method_keyword_params (parser);
18420   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18421
18422   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18423 }
18424
18425 /* Pars an Objective-C method prototype list.  */
18426
18427 static void
18428 cp_parser_objc_method_prototype_list (cp_parser* parser)
18429 {
18430   cp_token *token = cp_lexer_peek_token (parser->lexer);
18431
18432   while (token->keyword != RID_AT_END)
18433     {
18434       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18435         {
18436           objc_add_method_declaration
18437            (cp_parser_objc_method_signature (parser));
18438           cp_parser_consume_semicolon_at_end_of_statement (parser);
18439         }
18440       else
18441         /* Allow for interspersed non-ObjC++ code.  */
18442         cp_parser_objc_interstitial_code (parser);
18443
18444       token = cp_lexer_peek_token (parser->lexer);
18445     }
18446
18447   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18448   objc_finish_interface ();
18449 }
18450
18451 /* Parse an Objective-C method definition list.  */
18452
18453 static void
18454 cp_parser_objc_method_definition_list (cp_parser* parser)
18455 {
18456   cp_token *token = cp_lexer_peek_token (parser->lexer);
18457
18458   while (token->keyword != RID_AT_END)
18459     {
18460       tree meth;
18461
18462       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18463         {
18464           push_deferring_access_checks (dk_deferred);
18465           objc_start_method_definition
18466            (cp_parser_objc_method_signature (parser));
18467
18468           /* For historical reasons, we accept an optional semicolon.  */
18469           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18470             cp_lexer_consume_token (parser->lexer);
18471
18472           perform_deferred_access_checks ();
18473           stop_deferring_access_checks ();
18474           meth = cp_parser_function_definition_after_declarator (parser,
18475                                                                  false);
18476           pop_deferring_access_checks ();
18477           objc_finish_method_definition (meth);
18478         }
18479       else
18480         /* Allow for interspersed non-ObjC++ code.  */
18481         cp_parser_objc_interstitial_code (parser);
18482
18483       token = cp_lexer_peek_token (parser->lexer);
18484     }
18485
18486   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18487   objc_finish_implementation ();
18488 }
18489
18490 /* Parse Objective-C ivars.  */
18491
18492 static void
18493 cp_parser_objc_class_ivars (cp_parser* parser)
18494 {
18495   cp_token *token = cp_lexer_peek_token (parser->lexer);
18496
18497   if (token->type != CPP_OPEN_BRACE)
18498     return;     /* No ivars specified.  */
18499
18500   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
18501   token = cp_lexer_peek_token (parser->lexer);
18502
18503   while (token->type != CPP_CLOSE_BRACE)
18504     {
18505       cp_decl_specifier_seq declspecs;
18506       int decl_class_or_enum_p;
18507       tree prefix_attributes;
18508
18509       cp_parser_objc_visibility_spec (parser);
18510
18511       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18512         break;
18513
18514       cp_parser_decl_specifier_seq (parser,
18515                                     CP_PARSER_FLAGS_OPTIONAL,
18516                                     &declspecs,
18517                                     &decl_class_or_enum_p);
18518       prefix_attributes = declspecs.attributes;
18519       declspecs.attributes = NULL_TREE;
18520
18521       /* Keep going until we hit the `;' at the end of the
18522          declaration.  */
18523       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18524         {
18525           tree width = NULL_TREE, attributes, first_attribute, decl;
18526           cp_declarator *declarator = NULL;
18527           int ctor_dtor_or_conv_p;
18528
18529           /* Check for a (possibly unnamed) bitfield declaration.  */
18530           token = cp_lexer_peek_token (parser->lexer);
18531           if (token->type == CPP_COLON)
18532             goto eat_colon;
18533
18534           if (token->type == CPP_NAME
18535               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18536                   == CPP_COLON))
18537             {
18538               /* Get the name of the bitfield.  */
18539               declarator = make_id_declarator (NULL_TREE,
18540                                                cp_parser_identifier (parser),
18541                                                sfk_none);
18542
18543              eat_colon:
18544               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18545               /* Get the width of the bitfield.  */
18546               width
18547                 = cp_parser_constant_expression (parser,
18548                                                  /*allow_non_constant=*/false,
18549                                                  NULL);
18550             }
18551           else
18552             {
18553               /* Parse the declarator.  */
18554               declarator
18555                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18556                                         &ctor_dtor_or_conv_p,
18557                                         /*parenthesized_p=*/NULL,
18558                                         /*member_p=*/false);
18559             }
18560
18561           /* Look for attributes that apply to the ivar.  */
18562           attributes = cp_parser_attributes_opt (parser);
18563           /* Remember which attributes are prefix attributes and
18564              which are not.  */
18565           first_attribute = attributes;
18566           /* Combine the attributes.  */
18567           attributes = chainon (prefix_attributes, attributes);
18568
18569           if (width)
18570             {
18571               /* Create the bitfield declaration.  */
18572               decl = grokbitfield (declarator, &declspecs, width);
18573               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18574             }
18575           else
18576             decl = grokfield (declarator, &declspecs,
18577                               NULL_TREE, /*init_const_expr_p=*/false,
18578                               NULL_TREE, attributes);
18579
18580           /* Add the instance variable.  */
18581           objc_add_instance_variable (decl);
18582
18583           /* Reset PREFIX_ATTRIBUTES.  */
18584           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18585             attributes = TREE_CHAIN (attributes);
18586           if (attributes)
18587             TREE_CHAIN (attributes) = NULL_TREE;
18588
18589           token = cp_lexer_peek_token (parser->lexer);
18590
18591           if (token->type == CPP_COMMA)
18592             {
18593               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18594               continue;
18595             }
18596           break;
18597         }
18598
18599       cp_parser_consume_semicolon_at_end_of_statement (parser);
18600       token = cp_lexer_peek_token (parser->lexer);
18601     }
18602
18603   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
18604   /* For historical reasons, we accept an optional semicolon.  */
18605   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18606     cp_lexer_consume_token (parser->lexer);
18607 }
18608
18609 /* Parse an Objective-C protocol declaration.  */
18610
18611 static void
18612 cp_parser_objc_protocol_declaration (cp_parser* parser)
18613 {
18614   tree proto, protorefs;
18615   cp_token *tok;
18616
18617   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18618   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18619     {
18620       error ("identifier expected after %<@protocol%>");
18621       goto finish;
18622     }
18623
18624   /* See if we have a forward declaration or a definition.  */
18625   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18626
18627   /* Try a forward declaration first.  */
18628   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18629     {
18630       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18631      finish:
18632       cp_parser_consume_semicolon_at_end_of_statement (parser);
18633     }
18634
18635   /* Ok, we got a full-fledged definition (or at least should).  */
18636   else
18637     {
18638       proto = cp_parser_identifier (parser);
18639       protorefs = cp_parser_objc_protocol_refs_opt (parser);
18640       objc_start_protocol (proto, protorefs);
18641       cp_parser_objc_method_prototype_list (parser);
18642     }
18643 }
18644
18645 /* Parse an Objective-C superclass or category.  */
18646
18647 static void
18648 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18649                                                           tree *categ)
18650 {
18651   cp_token *next = cp_lexer_peek_token (parser->lexer);
18652
18653   *super = *categ = NULL_TREE;
18654   if (next->type == CPP_COLON)
18655     {
18656       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18657       *super = cp_parser_identifier (parser);
18658     }
18659   else if (next->type == CPP_OPEN_PAREN)
18660     {
18661       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18662       *categ = cp_parser_identifier (parser);
18663       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18664     }
18665 }
18666
18667 /* Parse an Objective-C class interface.  */
18668
18669 static void
18670 cp_parser_objc_class_interface (cp_parser* parser)
18671 {
18672   tree name, super, categ, protos;
18673
18674   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
18675   name = cp_parser_identifier (parser);
18676   cp_parser_objc_superclass_or_category (parser, &super, &categ);
18677   protos = cp_parser_objc_protocol_refs_opt (parser);
18678
18679   /* We have either a class or a category on our hands.  */
18680   if (categ)
18681     objc_start_category_interface (name, categ, protos);
18682   else
18683     {
18684       objc_start_class_interface (name, super, protos);
18685       /* Handle instance variable declarations, if any.  */
18686       cp_parser_objc_class_ivars (parser);
18687       objc_continue_interface ();
18688     }
18689
18690   cp_parser_objc_method_prototype_list (parser);
18691 }
18692
18693 /* Parse an Objective-C class implementation.  */
18694
18695 static void
18696 cp_parser_objc_class_implementation (cp_parser* parser)
18697 {
18698   tree name, super, categ;
18699
18700   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
18701   name = cp_parser_identifier (parser);
18702   cp_parser_objc_superclass_or_category (parser, &super, &categ);
18703
18704   /* We have either a class or a category on our hands.  */
18705   if (categ)
18706     objc_start_category_implementation (name, categ);
18707   else
18708     {
18709       objc_start_class_implementation (name, super);
18710       /* Handle instance variable declarations, if any.  */
18711       cp_parser_objc_class_ivars (parser);
18712       objc_continue_implementation ();
18713     }
18714
18715   cp_parser_objc_method_definition_list (parser);
18716 }
18717
18718 /* Consume the @end token and finish off the implementation.  */
18719
18720 static void
18721 cp_parser_objc_end_implementation (cp_parser* parser)
18722 {
18723   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18724   objc_finish_implementation ();
18725 }
18726
18727 /* Parse an Objective-C declaration.  */
18728
18729 static void
18730 cp_parser_objc_declaration (cp_parser* parser)
18731 {
18732   /* Try to figure out what kind of declaration is present.  */
18733   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18734
18735   switch (kwd->keyword)
18736     {
18737     case RID_AT_ALIAS:
18738       cp_parser_objc_alias_declaration (parser);
18739       break;
18740     case RID_AT_CLASS:
18741       cp_parser_objc_class_declaration (parser);
18742       break;
18743     case RID_AT_PROTOCOL:
18744       cp_parser_objc_protocol_declaration (parser);
18745       break;
18746     case RID_AT_INTERFACE:
18747       cp_parser_objc_class_interface (parser);
18748       break;
18749     case RID_AT_IMPLEMENTATION:
18750       cp_parser_objc_class_implementation (parser);
18751       break;
18752     case RID_AT_END:
18753       cp_parser_objc_end_implementation (parser);
18754       break;
18755     default:
18756       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18757       cp_parser_skip_to_end_of_block_or_statement (parser);
18758     }
18759 }
18760
18761 /* Parse an Objective-C try-catch-finally statement.
18762
18763    objc-try-catch-finally-stmt:
18764      @try compound-statement objc-catch-clause-seq [opt]
18765        objc-finally-clause [opt]
18766
18767    objc-catch-clause-seq:
18768      objc-catch-clause objc-catch-clause-seq [opt]
18769
18770    objc-catch-clause:
18771      @catch ( exception-declaration ) compound-statement
18772
18773    objc-finally-clause
18774      @finally compound-statement
18775
18776    Returns NULL_TREE.  */
18777
18778 static tree
18779 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18780   location_t location;
18781   tree stmt;
18782
18783   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18784   location = cp_lexer_peek_token (parser->lexer)->location;
18785   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18786      node, lest it get absorbed into the surrounding block.  */
18787   stmt = push_stmt_list ();
18788   cp_parser_compound_statement (parser, NULL, false);
18789   objc_begin_try_stmt (location, pop_stmt_list (stmt));
18790
18791   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18792     {
18793       cp_parameter_declarator *parmdecl;
18794       tree parm;
18795
18796       cp_lexer_consume_token (parser->lexer);
18797       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18798       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18799       parm = grokdeclarator (parmdecl->declarator,
18800                              &parmdecl->decl_specifiers,
18801                              PARM, /*initialized=*/0,
18802                              /*attrlist=*/NULL);
18803       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18804       objc_begin_catch_clause (parm);
18805       cp_parser_compound_statement (parser, NULL, false);
18806       objc_finish_catch_clause ();
18807     }
18808
18809   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18810     {
18811       cp_lexer_consume_token (parser->lexer);
18812       location = cp_lexer_peek_token (parser->lexer)->location;
18813       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18814          node, lest it get absorbed into the surrounding block.  */
18815       stmt = push_stmt_list ();
18816       cp_parser_compound_statement (parser, NULL, false);
18817       objc_build_finally_clause (location, pop_stmt_list (stmt));
18818     }
18819
18820   return objc_finish_try_stmt ();
18821 }
18822
18823 /* Parse an Objective-C synchronized statement.
18824
18825    objc-synchronized-stmt:
18826      @synchronized ( expression ) compound-statement
18827
18828    Returns NULL_TREE.  */
18829
18830 static tree
18831 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18832   location_t location;
18833   tree lock, stmt;
18834
18835   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18836
18837   location = cp_lexer_peek_token (parser->lexer)->location;
18838   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18839   lock = cp_parser_expression (parser, false);
18840   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18841
18842   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18843      node, lest it get absorbed into the surrounding block.  */
18844   stmt = push_stmt_list ();
18845   cp_parser_compound_statement (parser, NULL, false);
18846
18847   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18848 }
18849
18850 /* Parse an Objective-C throw statement.
18851
18852    objc-throw-stmt:
18853      @throw assignment-expression [opt] ;
18854
18855    Returns a constructed '@throw' statement.  */
18856
18857 static tree
18858 cp_parser_objc_throw_statement (cp_parser *parser) {
18859   tree expr = NULL_TREE;
18860
18861   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18862
18863   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18864     expr = cp_parser_assignment_expression (parser, false);
18865
18866   cp_parser_consume_semicolon_at_end_of_statement (parser);
18867
18868   return objc_build_throw_stmt (expr);
18869 }
18870
18871 /* Parse an Objective-C statement.  */
18872
18873 static tree
18874 cp_parser_objc_statement (cp_parser * parser) {
18875   /* Try to figure out what kind of declaration is present.  */
18876   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18877
18878   switch (kwd->keyword)
18879     {
18880     case RID_AT_TRY:
18881       return cp_parser_objc_try_catch_finally_statement (parser);
18882     case RID_AT_SYNCHRONIZED:
18883       return cp_parser_objc_synchronized_statement (parser);
18884     case RID_AT_THROW:
18885       return cp_parser_objc_throw_statement (parser);
18886     default:
18887       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18888       cp_parser_skip_to_end_of_block_or_statement (parser);
18889     }
18890
18891   return error_mark_node;
18892 }
18893 \f
18894 /* OpenMP 2.5 parsing routines.  */
18895
18896 /* Returns name of the next clause.
18897    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18898    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
18899    returned and the token is consumed.  */
18900
18901 static pragma_omp_clause
18902 cp_parser_omp_clause_name (cp_parser *parser)
18903 {
18904   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18905
18906   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18907     result = PRAGMA_OMP_CLAUSE_IF;
18908   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18909     result = PRAGMA_OMP_CLAUSE_DEFAULT;
18910   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18911     result = PRAGMA_OMP_CLAUSE_PRIVATE;
18912   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18913     {
18914       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18915       const char *p = IDENTIFIER_POINTER (id);
18916
18917       switch (p[0])
18918         {
18919         case 'c':
18920           if (!strcmp ("copyin", p))
18921             result = PRAGMA_OMP_CLAUSE_COPYIN;
18922           else if (!strcmp ("copyprivate", p))
18923             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18924           break;
18925         case 'f':
18926           if (!strcmp ("firstprivate", p))
18927             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18928           break;
18929         case 'l':
18930           if (!strcmp ("lastprivate", p))
18931             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18932           break;
18933         case 'n':
18934           if (!strcmp ("nowait", p))
18935             result = PRAGMA_OMP_CLAUSE_NOWAIT;
18936           else if (!strcmp ("num_threads", p))
18937             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18938           break;
18939         case 'o':
18940           if (!strcmp ("ordered", p))
18941             result = PRAGMA_OMP_CLAUSE_ORDERED;
18942           break;
18943         case 'r':
18944           if (!strcmp ("reduction", p))
18945             result = PRAGMA_OMP_CLAUSE_REDUCTION;
18946           break;
18947         case 's':
18948           if (!strcmp ("schedule", p))
18949             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18950           else if (!strcmp ("shared", p))
18951             result = PRAGMA_OMP_CLAUSE_SHARED;
18952           break;
18953         }
18954     }
18955
18956   if (result != PRAGMA_OMP_CLAUSE_NONE)
18957     cp_lexer_consume_token (parser->lexer);
18958
18959   return result;
18960 }
18961
18962 /* Validate that a clause of the given type does not already exist.  */
18963
18964 static void
18965 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18966 {
18967   tree c;
18968
18969   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18970     if (OMP_CLAUSE_CODE (c) == code)
18971       {
18972         error ("too many %qs clauses", name);
18973         break;
18974       }
18975 }
18976
18977 /* OpenMP 2.5:
18978    variable-list:
18979      identifier
18980      variable-list , identifier
18981
18982    In addition, we match a closing parenthesis.  An opening parenthesis
18983    will have been consumed by the caller.
18984
18985    If KIND is nonzero, create the appropriate node and install the decl
18986    in OMP_CLAUSE_DECL and add the node to the head of the list.
18987
18988    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18989    return the list created.  */
18990
18991 static tree
18992 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18993                                 tree list)
18994 {
18995   while (1)
18996     {
18997       tree name, decl;
18998
18999       name = cp_parser_id_expression (parser, /*template_p=*/false,
19000                                       /*check_dependency_p=*/true,
19001                                       /*template_p=*/NULL,
19002                                       /*declarator_p=*/false,
19003                                       /*optional_p=*/false);
19004       if (name == error_mark_node)
19005         goto skip_comma;
19006
19007       decl = cp_parser_lookup_name_simple (parser, name);
19008       if (decl == error_mark_node)
19009         cp_parser_name_lookup_error (parser, name, decl, NULL);
19010       else if (kind != 0)
19011         {
19012           tree u = build_omp_clause (kind);
19013           OMP_CLAUSE_DECL (u) = decl;
19014           OMP_CLAUSE_CHAIN (u) = list;
19015           list = u;
19016         }
19017       else
19018         list = tree_cons (decl, NULL_TREE, list);
19019
19020     get_comma:
19021       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19022         break;
19023       cp_lexer_consume_token (parser->lexer);
19024     }
19025
19026   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19027     {
19028       int ending;
19029
19030       /* Try to resync to an unnested comma.  Copied from
19031          cp_parser_parenthesized_expression_list.  */
19032     skip_comma:
19033       ending = cp_parser_skip_to_closing_parenthesis (parser,
19034                                                       /*recovering=*/true,
19035                                                       /*or_comma=*/true,
19036                                                       /*consume_paren=*/true);
19037       if (ending < 0)
19038         goto get_comma;
19039     }
19040
19041   return list;
19042 }
19043
19044 /* Similarly, but expect leading and trailing parenthesis.  This is a very
19045    common case for omp clauses.  */
19046
19047 static tree
19048 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19049 {
19050   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19051     return cp_parser_omp_var_list_no_open (parser, kind, list);
19052   return list;
19053 }
19054
19055 /* OpenMP 2.5:
19056    default ( shared | none ) */
19057
19058 static tree
19059 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19060 {
19061   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19062   tree c;
19063
19064   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19065     return list;
19066   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19067     {
19068       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19069       const char *p = IDENTIFIER_POINTER (id);
19070
19071       switch (p[0])
19072         {
19073         case 'n':
19074           if (strcmp ("none", p) != 0)
19075             goto invalid_kind;
19076           kind = OMP_CLAUSE_DEFAULT_NONE;
19077           break;
19078
19079         case 's':
19080           if (strcmp ("shared", p) != 0)
19081             goto invalid_kind;
19082           kind = OMP_CLAUSE_DEFAULT_SHARED;
19083           break;
19084
19085         default:
19086           goto invalid_kind;
19087         }
19088
19089       cp_lexer_consume_token (parser->lexer);
19090     }
19091   else
19092     {
19093     invalid_kind:
19094       cp_parser_error (parser, "expected %<none%> or %<shared%>");
19095     }
19096
19097   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19098     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19099                                            /*or_comma=*/false,
19100                                            /*consume_paren=*/true);
19101
19102   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19103     return list;
19104
19105   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19106   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19107   OMP_CLAUSE_CHAIN (c) = list;
19108   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19109
19110   return c;
19111 }
19112
19113 /* OpenMP 2.5:
19114    if ( expression ) */
19115
19116 static tree
19117 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19118 {
19119   tree t, c;
19120
19121   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19122     return list;
19123
19124   t = cp_parser_condition (parser);
19125
19126   if (t == error_mark_node
19127       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19128     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19129                                            /*or_comma=*/false,
19130                                            /*consume_paren=*/true);
19131
19132   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19133
19134   c = build_omp_clause (OMP_CLAUSE_IF);
19135   OMP_CLAUSE_IF_EXPR (c) = t;
19136   OMP_CLAUSE_CHAIN (c) = list;
19137
19138   return c;
19139 }
19140
19141 /* OpenMP 2.5:
19142    nowait */
19143
19144 static tree
19145 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19146 {
19147   tree c;
19148
19149   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19150
19151   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19152   OMP_CLAUSE_CHAIN (c) = list;
19153   return c;
19154 }
19155
19156 /* OpenMP 2.5:
19157    num_threads ( expression ) */
19158
19159 static tree
19160 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19161 {
19162   tree t, c;
19163
19164   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19165     return list;
19166
19167   t = cp_parser_expression (parser, false);
19168
19169   if (t == error_mark_node
19170       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19171     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19172                                            /*or_comma=*/false,
19173                                            /*consume_paren=*/true);
19174
19175   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19176
19177   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19178   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19179   OMP_CLAUSE_CHAIN (c) = list;
19180
19181   return c;
19182 }
19183
19184 /* OpenMP 2.5:
19185    ordered */
19186
19187 static tree
19188 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19189 {
19190   tree c;
19191
19192   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19193
19194   c = build_omp_clause (OMP_CLAUSE_ORDERED);
19195   OMP_CLAUSE_CHAIN (c) = list;
19196   return c;
19197 }
19198
19199 /* OpenMP 2.5:
19200    reduction ( reduction-operator : variable-list )
19201
19202    reduction-operator:
19203      One of: + * - & ^ | && || */
19204
19205 static tree
19206 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19207 {
19208   enum tree_code code;
19209   tree nlist, c;
19210
19211   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19212     return list;
19213
19214   switch (cp_lexer_peek_token (parser->lexer)->type)
19215     {
19216     case CPP_PLUS:
19217       code = PLUS_EXPR;
19218       break;
19219     case CPP_MULT:
19220       code = MULT_EXPR;
19221       break;
19222     case CPP_MINUS:
19223       code = MINUS_EXPR;
19224       break;
19225     case CPP_AND:
19226       code = BIT_AND_EXPR;
19227       break;
19228     case CPP_XOR:
19229       code = BIT_XOR_EXPR;
19230       break;
19231     case CPP_OR:
19232       code = BIT_IOR_EXPR;
19233       break;
19234     case CPP_AND_AND:
19235       code = TRUTH_ANDIF_EXPR;
19236       break;
19237     case CPP_OR_OR:
19238       code = TRUTH_ORIF_EXPR;
19239       break;
19240     default:
19241       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19242     resync_fail:
19243       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19244                                              /*or_comma=*/false,
19245                                              /*consume_paren=*/true);
19246       return list;
19247     }
19248   cp_lexer_consume_token (parser->lexer);
19249
19250   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19251     goto resync_fail;
19252
19253   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19254   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19255     OMP_CLAUSE_REDUCTION_CODE (c) = code;
19256
19257   return nlist;
19258 }
19259
19260 /* OpenMP 2.5:
19261    schedule ( schedule-kind )
19262    schedule ( schedule-kind , expression )
19263
19264    schedule-kind:
19265      static | dynamic | guided | runtime  */
19266
19267 static tree
19268 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19269 {
19270   tree c, t;
19271
19272   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19273     return list;
19274
19275   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19276
19277   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19278     {
19279       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19280       const char *p = IDENTIFIER_POINTER (id);
19281
19282       switch (p[0])
19283         {
19284         case 'd':
19285           if (strcmp ("dynamic", p) != 0)
19286             goto invalid_kind;
19287           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19288           break;
19289
19290         case 'g':
19291           if (strcmp ("guided", p) != 0)
19292             goto invalid_kind;
19293           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19294           break;
19295
19296         case 'r':
19297           if (strcmp ("runtime", p) != 0)
19298             goto invalid_kind;
19299           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19300           break;
19301
19302         default:
19303           goto invalid_kind;
19304         }
19305     }
19306   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19307     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19308   else
19309     goto invalid_kind;
19310   cp_lexer_consume_token (parser->lexer);
19311
19312   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19313     {
19314       cp_lexer_consume_token (parser->lexer);
19315
19316       t = cp_parser_assignment_expression (parser, false);
19317
19318       if (t == error_mark_node)
19319         goto resync_fail;
19320       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19321         error ("schedule %<runtime%> does not take "
19322                "a %<chunk_size%> parameter");
19323       else
19324         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19325
19326       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19327         goto resync_fail;
19328     }
19329   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19330     goto resync_fail;
19331
19332   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19333   OMP_CLAUSE_CHAIN (c) = list;
19334   return c;
19335
19336  invalid_kind:
19337   cp_parser_error (parser, "invalid schedule kind");
19338  resync_fail:
19339   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19340                                          /*or_comma=*/false,
19341                                          /*consume_paren=*/true);
19342   return list;
19343 }
19344
19345 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
19346    is a bitmask in MASK.  Return the list of clauses found; the result
19347    of clause default goes in *pdefault.  */
19348
19349 static tree
19350 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19351                            const char *where, cp_token *pragma_tok)
19352 {
19353   tree clauses = NULL;
19354
19355   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19356     {
19357       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19358       const char *c_name;
19359       tree prev = clauses;
19360
19361       switch (c_kind)
19362         {
19363         case PRAGMA_OMP_CLAUSE_COPYIN:
19364           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19365           c_name = "copyin";
19366           break;
19367         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19368           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19369                                             clauses);
19370           c_name = "copyprivate";
19371           break;
19372         case PRAGMA_OMP_CLAUSE_DEFAULT:
19373           clauses = cp_parser_omp_clause_default (parser, clauses);
19374           c_name = "default";
19375           break;
19376         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19377           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19378                                             clauses);
19379           c_name = "firstprivate";
19380           break;
19381         case PRAGMA_OMP_CLAUSE_IF:
19382           clauses = cp_parser_omp_clause_if (parser, clauses);
19383           c_name = "if";
19384           break;
19385         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19386           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19387                                             clauses);
19388           c_name = "lastprivate";
19389           break;
19390         case PRAGMA_OMP_CLAUSE_NOWAIT:
19391           clauses = cp_parser_omp_clause_nowait (parser, clauses);
19392           c_name = "nowait";
19393           break;
19394         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19395           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19396           c_name = "num_threads";
19397           break;
19398         case PRAGMA_OMP_CLAUSE_ORDERED:
19399           clauses = cp_parser_omp_clause_ordered (parser, clauses);
19400           c_name = "ordered";
19401           break;
19402         case PRAGMA_OMP_CLAUSE_PRIVATE:
19403           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19404                                             clauses);
19405           c_name = "private";
19406           break;
19407         case PRAGMA_OMP_CLAUSE_REDUCTION:
19408           clauses = cp_parser_omp_clause_reduction (parser, clauses);
19409           c_name = "reduction";
19410           break;
19411         case PRAGMA_OMP_CLAUSE_SCHEDULE:
19412           clauses = cp_parser_omp_clause_schedule (parser, clauses);
19413           c_name = "schedule";
19414           break;
19415         case PRAGMA_OMP_CLAUSE_SHARED:
19416           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19417                                             clauses);
19418           c_name = "shared";
19419           break;
19420         default:
19421           cp_parser_error (parser, "expected %<#pragma omp%> clause");
19422           goto saw_error;
19423         }
19424
19425       if (((mask >> c_kind) & 1) == 0)
19426         {
19427           /* Remove the invalid clause(s) from the list to avoid
19428              confusing the rest of the compiler.  */
19429           clauses = prev;
19430           error ("%qs is not valid for %qs", c_name, where);
19431         }
19432     }
19433  saw_error:
19434   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19435   return finish_omp_clauses (clauses);
19436 }
19437
19438 /* OpenMP 2.5:
19439    structured-block:
19440      statement
19441
19442    In practice, we're also interested in adding the statement to an
19443    outer node.  So it is convenient if we work around the fact that
19444    cp_parser_statement calls add_stmt.  */
19445
19446 static unsigned
19447 cp_parser_begin_omp_structured_block (cp_parser *parser)
19448 {
19449   unsigned save = parser->in_statement;
19450
19451   /* Only move the values to IN_OMP_BLOCK if they weren't false.
19452      This preserves the "not within loop or switch" style error messages
19453      for nonsense cases like
19454         void foo() {
19455         #pragma omp single
19456           break;
19457         }
19458   */
19459   if (parser->in_statement)
19460     parser->in_statement = IN_OMP_BLOCK;
19461
19462   return save;
19463 }
19464
19465 static void
19466 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19467 {
19468   parser->in_statement = save;
19469 }
19470
19471 static tree
19472 cp_parser_omp_structured_block (cp_parser *parser)
19473 {
19474   tree stmt = begin_omp_structured_block ();
19475   unsigned int save = cp_parser_begin_omp_structured_block (parser);
19476
19477   cp_parser_statement (parser, NULL_TREE, false, NULL);
19478
19479   cp_parser_end_omp_structured_block (parser, save);
19480   return finish_omp_structured_block (stmt);
19481 }
19482
19483 /* OpenMP 2.5:
19484    # pragma omp atomic new-line
19485      expression-stmt
19486
19487    expression-stmt:
19488      x binop= expr | x++ | ++x | x-- | --x
19489    binop:
19490      +, *, -, /, &, ^, |, <<, >>
19491
19492   where x is an lvalue expression with scalar type.  */
19493
19494 static void
19495 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19496 {
19497   tree lhs, rhs;
19498   enum tree_code code;
19499
19500   cp_parser_require_pragma_eol (parser, pragma_tok);
19501
19502   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19503                                     /*cast_p=*/false);
19504   switch (TREE_CODE (lhs))
19505     {
19506     case ERROR_MARK:
19507       goto saw_error;
19508
19509     case PREINCREMENT_EXPR:
19510     case POSTINCREMENT_EXPR:
19511       lhs = TREE_OPERAND (lhs, 0);
19512       code = PLUS_EXPR;
19513       rhs = integer_one_node;
19514       break;
19515
19516     case PREDECREMENT_EXPR:
19517     case POSTDECREMENT_EXPR:
19518       lhs = TREE_OPERAND (lhs, 0);
19519       code = MINUS_EXPR;
19520       rhs = integer_one_node;
19521       break;
19522
19523     default:
19524       switch (cp_lexer_peek_token (parser->lexer)->type)
19525         {
19526         case CPP_MULT_EQ:
19527           code = MULT_EXPR;
19528           break;
19529         case CPP_DIV_EQ:
19530           code = TRUNC_DIV_EXPR;
19531           break;
19532         case CPP_PLUS_EQ:
19533           code = PLUS_EXPR;
19534           break;
19535         case CPP_MINUS_EQ:
19536           code = MINUS_EXPR;
19537           break;
19538         case CPP_LSHIFT_EQ:
19539           code = LSHIFT_EXPR;
19540           break;
19541         case CPP_RSHIFT_EQ:
19542           code = RSHIFT_EXPR;
19543           break;
19544         case CPP_AND_EQ:
19545           code = BIT_AND_EXPR;
19546           break;
19547         case CPP_OR_EQ:
19548           code = BIT_IOR_EXPR;
19549           break;
19550         case CPP_XOR_EQ:
19551           code = BIT_XOR_EXPR;
19552           break;
19553         default:
19554           cp_parser_error (parser,
19555                            "invalid operator for %<#pragma omp atomic%>");
19556           goto saw_error;
19557         }
19558       cp_lexer_consume_token (parser->lexer);
19559
19560       rhs = cp_parser_expression (parser, false);
19561       if (rhs == error_mark_node)
19562         goto saw_error;
19563       break;
19564     }
19565   finish_omp_atomic (code, lhs, rhs);
19566   cp_parser_consume_semicolon_at_end_of_statement (parser);
19567   return;
19568
19569  saw_error:
19570   cp_parser_skip_to_end_of_block_or_statement (parser);
19571 }
19572
19573
19574 /* OpenMP 2.5:
19575    # pragma omp barrier new-line  */
19576
19577 static void
19578 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19579 {
19580   cp_parser_require_pragma_eol (parser, pragma_tok);
19581   finish_omp_barrier ();
19582 }
19583
19584 /* OpenMP 2.5:
19585    # pragma omp critical [(name)] new-line
19586      structured-block  */
19587
19588 static tree
19589 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19590 {
19591   tree stmt, name = NULL;
19592
19593   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19594     {
19595       cp_lexer_consume_token (parser->lexer);
19596
19597       name = cp_parser_identifier (parser);
19598
19599       if (name == error_mark_node
19600           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19601         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19602                                                /*or_comma=*/false,
19603                                                /*consume_paren=*/true);
19604       if (name == error_mark_node)
19605         name = NULL;
19606     }
19607   cp_parser_require_pragma_eol (parser, pragma_tok);
19608
19609   stmt = cp_parser_omp_structured_block (parser);
19610   return c_finish_omp_critical (stmt, name);
19611 }
19612
19613 /* OpenMP 2.5:
19614    # pragma omp flush flush-vars[opt] new-line
19615
19616    flush-vars:
19617      ( variable-list ) */
19618
19619 static void
19620 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19621 {
19622   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19623     (void) cp_parser_omp_var_list (parser, 0, NULL);
19624   cp_parser_require_pragma_eol (parser, pragma_tok);
19625
19626   finish_omp_flush ();
19627 }
19628
19629 /* Parse the restricted form of the for statment allowed by OpenMP.  */
19630
19631 static tree
19632 cp_parser_omp_for_loop (cp_parser *parser)
19633 {
19634   tree init, cond, incr, body, decl, pre_body;
19635   location_t loc;
19636
19637   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19638     {
19639       cp_parser_error (parser, "for statement expected");
19640       return NULL;
19641     }
19642   loc = cp_lexer_consume_token (parser->lexer)->location;
19643   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19644     return NULL;
19645
19646   init = decl = NULL;
19647   pre_body = push_stmt_list ();
19648   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19649     {
19650       cp_decl_specifier_seq type_specifiers;
19651
19652       /* First, try to parse as an initialized declaration.  See
19653          cp_parser_condition, from whence the bulk of this is copied.  */
19654
19655       cp_parser_parse_tentatively (parser);
19656       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19657                                     &type_specifiers);
19658       if (!cp_parser_error_occurred (parser))
19659         {
19660           tree asm_specification, attributes;
19661           cp_declarator *declarator;
19662
19663           declarator = cp_parser_declarator (parser,
19664                                              CP_PARSER_DECLARATOR_NAMED,
19665                                              /*ctor_dtor_or_conv_p=*/NULL,
19666                                              /*parenthesized_p=*/NULL,
19667                                              /*member_p=*/false);
19668           attributes = cp_parser_attributes_opt (parser);
19669           asm_specification = cp_parser_asm_specification_opt (parser);
19670
19671           cp_parser_require (parser, CPP_EQ, "`='");
19672           if (cp_parser_parse_definitely (parser))
19673             {
19674               tree pushed_scope;
19675
19676               decl = start_decl (declarator, &type_specifiers,
19677                                  /*initialized_p=*/false, attributes,
19678                                  /*prefix_attributes=*/NULL_TREE,
19679                                  &pushed_scope);
19680
19681               init = cp_parser_assignment_expression (parser, false);
19682
19683               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19684                               asm_specification, LOOKUP_ONLYCONVERTING);
19685
19686               if (pushed_scope)
19687                 pop_scope (pushed_scope);
19688             }
19689         }
19690       else
19691         cp_parser_abort_tentative_parse (parser);
19692
19693       /* If parsing as an initialized declaration failed, try again as
19694          a simple expression.  */
19695       if (decl == NULL)
19696         init = cp_parser_expression (parser, false);
19697     }
19698   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19699   pre_body = pop_stmt_list (pre_body);
19700
19701   cond = NULL;
19702   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19703     cond = cp_parser_condition (parser);
19704   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19705
19706   incr = NULL;
19707   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19708     incr = cp_parser_expression (parser, false);
19709
19710   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19711     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19712                                            /*or_comma=*/false,
19713                                            /*consume_paren=*/true);
19714
19715   /* Note that we saved the original contents of this flag when we entered
19716      the structured block, and so we don't need to re-save it here.  */
19717   parser->in_statement = IN_OMP_FOR;
19718
19719   /* Note that the grammar doesn't call for a structured block here,
19720      though the loop as a whole is a structured block.  */
19721   body = push_stmt_list ();
19722   cp_parser_statement (parser, NULL_TREE, false, NULL);
19723   body = pop_stmt_list (body);
19724
19725   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19726 }
19727
19728 /* OpenMP 2.5:
19729    #pragma omp for for-clause[optseq] new-line
19730      for-loop  */
19731
19732 #define OMP_FOR_CLAUSE_MASK                             \
19733         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19734         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19735         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19736         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19737         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
19738         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
19739         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19740
19741 static tree
19742 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19743 {
19744   tree clauses, sb, ret;
19745   unsigned int save;
19746
19747   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19748                                        "#pragma omp for", pragma_tok);
19749
19750   sb = begin_omp_structured_block ();
19751   save = cp_parser_begin_omp_structured_block (parser);
19752
19753   ret = cp_parser_omp_for_loop (parser);
19754   if (ret)
19755     OMP_FOR_CLAUSES (ret) = clauses;
19756
19757   cp_parser_end_omp_structured_block (parser, save);
19758   add_stmt (finish_omp_structured_block (sb));
19759
19760   return ret;
19761 }
19762
19763 /* OpenMP 2.5:
19764    # pragma omp master new-line
19765      structured-block  */
19766
19767 static tree
19768 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19769 {
19770   cp_parser_require_pragma_eol (parser, pragma_tok);
19771   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19772 }
19773
19774 /* OpenMP 2.5:
19775    # pragma omp ordered new-line
19776      structured-block  */
19777
19778 static tree
19779 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19780 {
19781   cp_parser_require_pragma_eol (parser, pragma_tok);
19782   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19783 }
19784
19785 /* OpenMP 2.5:
19786
19787    section-scope:
19788      { section-sequence }
19789
19790    section-sequence:
19791      section-directive[opt] structured-block
19792      section-sequence section-directive structured-block  */
19793
19794 static tree
19795 cp_parser_omp_sections_scope (cp_parser *parser)
19796 {
19797   tree stmt, substmt;
19798   bool error_suppress = false;
19799   cp_token *tok;
19800
19801   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19802     return NULL_TREE;
19803
19804   stmt = push_stmt_list ();
19805
19806   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19807     {
19808       unsigned save;
19809
19810       substmt = begin_omp_structured_block ();
19811       save = cp_parser_begin_omp_structured_block (parser);
19812
19813       while (1)
19814         {
19815           cp_parser_statement (parser, NULL_TREE, false, NULL);
19816
19817           tok = cp_lexer_peek_token (parser->lexer);
19818           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19819             break;
19820           if (tok->type == CPP_CLOSE_BRACE)
19821             break;
19822           if (tok->type == CPP_EOF)
19823             break;
19824         }
19825
19826       cp_parser_end_omp_structured_block (parser, save);
19827       substmt = finish_omp_structured_block (substmt);
19828       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19829       add_stmt (substmt);
19830     }
19831
19832   while (1)
19833     {
19834       tok = cp_lexer_peek_token (parser->lexer);
19835       if (tok->type == CPP_CLOSE_BRACE)
19836         break;
19837       if (tok->type == CPP_EOF)
19838         break;
19839
19840       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19841         {
19842           cp_lexer_consume_token (parser->lexer);
19843           cp_parser_require_pragma_eol (parser, tok);
19844           error_suppress = false;
19845         }
19846       else if (!error_suppress)
19847         {
19848           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19849           error_suppress = true;
19850         }
19851
19852       substmt = cp_parser_omp_structured_block (parser);
19853       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19854       add_stmt (substmt);
19855     }
19856   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19857
19858   substmt = pop_stmt_list (stmt);
19859
19860   stmt = make_node (OMP_SECTIONS);
19861   TREE_TYPE (stmt) = void_type_node;
19862   OMP_SECTIONS_BODY (stmt) = substmt;
19863
19864   add_stmt (stmt);
19865   return stmt;
19866 }
19867
19868 /* OpenMP 2.5:
19869    # pragma omp sections sections-clause[optseq] newline
19870      sections-scope  */
19871
19872 #define OMP_SECTIONS_CLAUSE_MASK                        \
19873         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19874         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19875         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19876         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19877         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19878
19879 static tree
19880 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19881 {
19882   tree clauses, ret;
19883
19884   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19885                                        "#pragma omp sections", pragma_tok);
19886
19887   ret = cp_parser_omp_sections_scope (parser);
19888   if (ret)
19889     OMP_SECTIONS_CLAUSES (ret) = clauses;
19890
19891   return ret;
19892 }
19893
19894 /* OpenMP 2.5:
19895    # pragma parallel parallel-clause new-line
19896    # pragma parallel for parallel-for-clause new-line
19897    # pragma parallel sections parallel-sections-clause new-line  */
19898
19899 #define OMP_PARALLEL_CLAUSE_MASK                        \
19900         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
19901         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19902         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19903         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
19904         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
19905         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
19906         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19907         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19908
19909 static tree
19910 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19911 {
19912   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19913   const char *p_name = "#pragma omp parallel";
19914   tree stmt, clauses, par_clause, ws_clause, block;
19915   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19916   unsigned int save;
19917
19918   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19919     {
19920       cp_lexer_consume_token (parser->lexer);
19921       p_kind = PRAGMA_OMP_PARALLEL_FOR;
19922       p_name = "#pragma omp parallel for";
19923       mask |= OMP_FOR_CLAUSE_MASK;
19924       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19925     }
19926   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19927     {
19928       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19929       const char *p = IDENTIFIER_POINTER (id);
19930       if (strcmp (p, "sections") == 0)
19931         {
19932           cp_lexer_consume_token (parser->lexer);
19933           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19934           p_name = "#pragma omp parallel sections";
19935           mask |= OMP_SECTIONS_CLAUSE_MASK;
19936           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19937         }
19938     }
19939
19940   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19941   block = begin_omp_parallel ();
19942   save = cp_parser_begin_omp_structured_block (parser);
19943
19944   switch (p_kind)
19945     {
19946     case PRAGMA_OMP_PARALLEL:
19947       cp_parser_already_scoped_statement (parser);
19948       par_clause = clauses;
19949       break;
19950
19951     case PRAGMA_OMP_PARALLEL_FOR:
19952       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19953       stmt = cp_parser_omp_for_loop (parser);
19954       if (stmt)
19955         OMP_FOR_CLAUSES (stmt) = ws_clause;
19956       break;
19957
19958     case PRAGMA_OMP_PARALLEL_SECTIONS:
19959       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19960       stmt = cp_parser_omp_sections_scope (parser);
19961       if (stmt)
19962         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19963       break;
19964
19965     default:
19966       gcc_unreachable ();
19967     }
19968
19969   cp_parser_end_omp_structured_block (parser, save);
19970   stmt = finish_omp_parallel (par_clause, block);
19971   if (p_kind != PRAGMA_OMP_PARALLEL)
19972     OMP_PARALLEL_COMBINED (stmt) = 1;
19973   return stmt;
19974 }
19975
19976 /* OpenMP 2.5:
19977    # pragma omp single single-clause[optseq] new-line
19978      structured-block  */
19979
19980 #define OMP_SINGLE_CLAUSE_MASK                          \
19981         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19982         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19983         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
19984         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19985
19986 static tree
19987 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19988 {
19989   tree stmt = make_node (OMP_SINGLE);
19990   TREE_TYPE (stmt) = void_type_node;
19991
19992   OMP_SINGLE_CLAUSES (stmt)
19993     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19994                                  "#pragma omp single", pragma_tok);
19995   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19996
19997   return add_stmt (stmt);
19998 }
19999
20000 /* OpenMP 2.5:
20001    # pragma omp threadprivate (variable-list) */
20002
20003 static void
20004 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20005 {
20006   tree vars;
20007
20008   vars = cp_parser_omp_var_list (parser, 0, NULL);
20009   cp_parser_require_pragma_eol (parser, pragma_tok);
20010
20011   finish_omp_threadprivate (vars);
20012 }
20013
20014 /* Main entry point to OpenMP statement pragmas.  */
20015
20016 static void
20017 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20018 {
20019   tree stmt;
20020
20021   switch (pragma_tok->pragma_kind)
20022     {
20023     case PRAGMA_OMP_ATOMIC:
20024       cp_parser_omp_atomic (parser, pragma_tok);
20025       return;
20026     case PRAGMA_OMP_CRITICAL:
20027       stmt = cp_parser_omp_critical (parser, pragma_tok);
20028       break;
20029     case PRAGMA_OMP_FOR:
20030       stmt = cp_parser_omp_for (parser, pragma_tok);
20031       break;
20032     case PRAGMA_OMP_MASTER:
20033       stmt = cp_parser_omp_master (parser, pragma_tok);
20034       break;
20035     case PRAGMA_OMP_ORDERED:
20036       stmt = cp_parser_omp_ordered (parser, pragma_tok);
20037       break;
20038     case PRAGMA_OMP_PARALLEL:
20039       stmt = cp_parser_omp_parallel (parser, pragma_tok);
20040       break;
20041     case PRAGMA_OMP_SECTIONS:
20042       stmt = cp_parser_omp_sections (parser, pragma_tok);
20043       break;
20044     case PRAGMA_OMP_SINGLE:
20045       stmt = cp_parser_omp_single (parser, pragma_tok);
20046       break;
20047     default:
20048       gcc_unreachable ();
20049     }
20050
20051   if (stmt)
20052     SET_EXPR_LOCATION (stmt, pragma_tok->location);
20053 }
20054 \f
20055 /* The parser.  */
20056
20057 static GTY (()) cp_parser *the_parser;
20058
20059 \f
20060 /* Special handling for the first token or line in the file.  The first
20061    thing in the file might be #pragma GCC pch_preprocess, which loads a
20062    PCH file, which is a GC collection point.  So we need to handle this
20063    first pragma without benefit of an existing lexer structure.
20064
20065    Always returns one token to the caller in *FIRST_TOKEN.  This is
20066    either the true first token of the file, or the first token after
20067    the initial pragma.  */
20068
20069 static void
20070 cp_parser_initial_pragma (cp_token *first_token)
20071 {
20072   tree name = NULL;
20073
20074   cp_lexer_get_preprocessor_token (NULL, first_token);
20075   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20076     return;
20077
20078   cp_lexer_get_preprocessor_token (NULL, first_token);
20079   if (first_token->type == CPP_STRING)
20080     {
20081       name = first_token->u.value;
20082
20083       cp_lexer_get_preprocessor_token (NULL, first_token);
20084       if (first_token->type != CPP_PRAGMA_EOL)
20085         error ("junk at end of %<#pragma GCC pch_preprocess%>");
20086     }
20087   else
20088     error ("expected string literal");
20089
20090   /* Skip to the end of the pragma.  */
20091   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20092     cp_lexer_get_preprocessor_token (NULL, first_token);
20093
20094   /* Now actually load the PCH file.  */
20095   if (name)
20096     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20097
20098   /* Read one more token to return to our caller.  We have to do this
20099      after reading the PCH file in, since its pointers have to be
20100      live.  */
20101   cp_lexer_get_preprocessor_token (NULL, first_token);
20102 }
20103
20104 /* Normal parsing of a pragma token.  Here we can (and must) use the
20105    regular lexer.  */
20106
20107 static bool
20108 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20109 {
20110   cp_token *pragma_tok;
20111   unsigned int id;
20112
20113   pragma_tok = cp_lexer_consume_token (parser->lexer);
20114   gcc_assert (pragma_tok->type == CPP_PRAGMA);
20115   parser->lexer->in_pragma = true;
20116
20117   id = pragma_tok->pragma_kind;
20118   switch (id)
20119     {
20120     case PRAGMA_GCC_PCH_PREPROCESS:
20121       error ("%<#pragma GCC pch_preprocess%> must be first");
20122       break;
20123
20124     case PRAGMA_OMP_BARRIER:
20125       switch (context)
20126         {
20127         case pragma_compound:
20128           cp_parser_omp_barrier (parser, pragma_tok);
20129           return false;
20130         case pragma_stmt:
20131           error ("%<#pragma omp barrier%> may only be "
20132                  "used in compound statements");
20133           break;
20134         default:
20135           goto bad_stmt;
20136         }
20137       break;
20138
20139     case PRAGMA_OMP_FLUSH:
20140       switch (context)
20141         {
20142         case pragma_compound:
20143           cp_parser_omp_flush (parser, pragma_tok);
20144           return false;
20145         case pragma_stmt:
20146           error ("%<#pragma omp flush%> may only be "
20147                  "used in compound statements");
20148           break;
20149         default:
20150           goto bad_stmt;
20151         }
20152       break;
20153
20154     case PRAGMA_OMP_THREADPRIVATE:
20155       cp_parser_omp_threadprivate (parser, pragma_tok);
20156       return false;
20157
20158     case PRAGMA_OMP_ATOMIC:
20159     case PRAGMA_OMP_CRITICAL:
20160     case PRAGMA_OMP_FOR:
20161     case PRAGMA_OMP_MASTER:
20162     case PRAGMA_OMP_ORDERED:
20163     case PRAGMA_OMP_PARALLEL:
20164     case PRAGMA_OMP_SECTIONS:
20165     case PRAGMA_OMP_SINGLE:
20166       if (context == pragma_external)
20167         goto bad_stmt;
20168       cp_parser_omp_construct (parser, pragma_tok);
20169       return true;
20170
20171     case PRAGMA_OMP_SECTION:
20172       error ("%<#pragma omp section%> may only be used in "
20173              "%<#pragma omp sections%> construct");
20174       break;
20175
20176     default:
20177       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20178       c_invoke_pragma_handler (id);
20179       break;
20180
20181     bad_stmt:
20182       cp_parser_error (parser, "expected declaration specifiers");
20183       break;
20184     }
20185
20186   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20187   return false;
20188 }
20189
20190 /* The interface the pragma parsers have to the lexer.  */
20191
20192 enum cpp_ttype
20193 pragma_lex (tree *value)
20194 {
20195   cp_token *tok;
20196   enum cpp_ttype ret;
20197
20198   tok = cp_lexer_peek_token (the_parser->lexer);
20199
20200   ret = tok->type;
20201   *value = tok->u.value;
20202
20203   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20204     ret = CPP_EOF;
20205   else if (ret == CPP_STRING)
20206     *value = cp_parser_string_literal (the_parser, false, false);
20207   else
20208     {
20209       cp_lexer_consume_token (the_parser->lexer);
20210       if (ret == CPP_KEYWORD)
20211         ret = CPP_NAME;
20212     }
20213
20214   return ret;
20215 }
20216
20217 \f
20218 /* External interface.  */
20219
20220 /* Parse one entire translation unit.  */
20221
20222 void
20223 c_parse_file (void)
20224 {
20225   bool error_occurred;
20226   static bool already_called = false;
20227
20228   if (already_called)
20229     {
20230       sorry ("inter-module optimizations not implemented for C++");
20231       return;
20232     }
20233   already_called = true;
20234
20235   the_parser = cp_parser_new ();
20236   push_deferring_access_checks (flag_access_control
20237                                 ? dk_no_deferred : dk_no_check);
20238   error_occurred = cp_parser_translation_unit (the_parser);
20239   the_parser = NULL;
20240 }
20241
20242 #include "gt-cp-parser.h"