OSDN Git Service

cp/
[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 == ERROR_MARK)
2706     return cp_error_declarator;
2707
2708   if (code == INDIRECT_REF)
2709     if (class_type == NULL_TREE)
2710       return make_pointer_declarator (cv_qualifiers, target);
2711     else
2712       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2713   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2714     return make_reference_declarator (cv_qualifiers, target, false);
2715   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2716     return make_reference_declarator (cv_qualifiers, target, true);
2717   gcc_unreachable ();
2718 }
2719
2720 /* Create a new C++ parser.  */
2721
2722 static cp_parser *
2723 cp_parser_new (void)
2724 {
2725   cp_parser *parser;
2726   cp_lexer *lexer;
2727   unsigned i;
2728
2729   /* cp_lexer_new_main is called before calling ggc_alloc because
2730      cp_lexer_new_main might load a PCH file.  */
2731   lexer = cp_lexer_new_main ();
2732
2733   /* Initialize the binops_by_token so that we can get the tree
2734      directly from the token.  */
2735   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2736     binops_by_token[binops[i].token_type] = binops[i];
2737
2738   parser = GGC_CNEW (cp_parser);
2739   parser->lexer = lexer;
2740   parser->context = cp_parser_context_new (NULL);
2741
2742   /* For now, we always accept GNU extensions.  */
2743   parser->allow_gnu_extensions_p = 1;
2744
2745   /* The `>' token is a greater-than operator, not the end of a
2746      template-id.  */
2747   parser->greater_than_is_operator_p = true;
2748
2749   parser->default_arg_ok_p = true;
2750
2751   /* We are not parsing a constant-expression.  */
2752   parser->integral_constant_expression_p = false;
2753   parser->allow_non_integral_constant_expression_p = false;
2754   parser->non_integral_constant_expression_p = false;
2755
2756   /* Local variable names are not forbidden.  */
2757   parser->local_variables_forbidden_p = false;
2758
2759   /* We are not processing an `extern "C"' declaration.  */
2760   parser->in_unbraced_linkage_specification_p = false;
2761
2762   /* We are not processing a declarator.  */
2763   parser->in_declarator_p = false;
2764
2765   /* We are not processing a template-argument-list.  */
2766   parser->in_template_argument_list_p = false;
2767
2768   /* We are not in an iteration statement.  */
2769   parser->in_statement = 0;
2770
2771   /* We are not in a switch statement.  */
2772   parser->in_switch_statement_p = false;
2773
2774   /* We are not parsing a type-id inside an expression.  */
2775   parser->in_type_id_in_expr_p = false;
2776
2777   /* Declarations aren't implicitly extern "C".  */
2778   parser->implicit_extern_c = false;
2779
2780   /* String literals should be translated to the execution character set.  */
2781   parser->translate_strings_p = true;
2782
2783   /* We are not parsing a function body.  */
2784   parser->in_function_body = false;
2785
2786   /* The unparsed function queue is empty.  */
2787   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2788
2789   /* There are no classes being defined.  */
2790   parser->num_classes_being_defined = 0;
2791
2792   /* No template parameters apply.  */
2793   parser->num_template_parameter_lists = 0;
2794
2795   return parser;
2796 }
2797
2798 /* Create a cp_lexer structure which will emit the tokens in CACHE
2799    and push it onto the parser's lexer stack.  This is used for delayed
2800    parsing of in-class method bodies and default arguments, and should
2801    not be confused with tentative parsing.  */
2802 static void
2803 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2804 {
2805   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2806   lexer->next = parser->lexer;
2807   parser->lexer = lexer;
2808
2809   /* Move the current source position to that of the first token in the
2810      new lexer.  */
2811   cp_lexer_set_source_position_from_token (lexer->next_token);
2812 }
2813
2814 /* Pop the top lexer off the parser stack.  This is never used for the
2815    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2816 static void
2817 cp_parser_pop_lexer (cp_parser *parser)
2818 {
2819   cp_lexer *lexer = parser->lexer;
2820   parser->lexer = lexer->next;
2821   cp_lexer_destroy (lexer);
2822
2823   /* Put the current source position back where it was before this
2824      lexer was pushed.  */
2825   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2826 }
2827
2828 /* Lexical conventions [gram.lex]  */
2829
2830 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2831    identifier.  */
2832
2833 static tree
2834 cp_parser_identifier (cp_parser* parser)
2835 {
2836   cp_token *token;
2837
2838   /* Look for the identifier.  */
2839   token = cp_parser_require (parser, CPP_NAME, "identifier");
2840   /* Return the value.  */
2841   return token ? token->u.value : error_mark_node;
2842 }
2843
2844 /* Parse a sequence of adjacent string constants.  Returns a
2845    TREE_STRING representing the combined, nul-terminated string
2846    constant.  If TRANSLATE is true, translate the string to the
2847    execution character set.  If WIDE_OK is true, a wide string is
2848    invalid here.
2849
2850    C++98 [lex.string] says that if a narrow string literal token is
2851    adjacent to a wide string literal token, the behavior is undefined.
2852    However, C99 6.4.5p4 says that this results in a wide string literal.
2853    We follow C99 here, for consistency with the C front end.
2854
2855    This code is largely lifted from lex_string() in c-lex.c.
2856
2857    FUTURE: ObjC++ will need to handle @-strings here.  */
2858 static tree
2859 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2860 {
2861   tree value;
2862   bool wide = false;
2863   size_t count;
2864   struct obstack str_ob;
2865   cpp_string str, istr, *strs;
2866   cp_token *tok;
2867
2868   tok = cp_lexer_peek_token (parser->lexer);
2869   if (!cp_parser_is_string_literal (tok))
2870     {
2871       cp_parser_error (parser, "expected string-literal");
2872       return error_mark_node;
2873     }
2874
2875   /* Try to avoid the overhead of creating and destroying an obstack
2876      for the common case of just one string.  */
2877   if (!cp_parser_is_string_literal
2878       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2879     {
2880       cp_lexer_consume_token (parser->lexer);
2881
2882       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2883       str.len = TREE_STRING_LENGTH (tok->u.value);
2884       count = 1;
2885       if (tok->type == CPP_WSTRING)
2886         wide = true;
2887
2888       strs = &str;
2889     }
2890   else
2891     {
2892       gcc_obstack_init (&str_ob);
2893       count = 0;
2894
2895       do
2896         {
2897           cp_lexer_consume_token (parser->lexer);
2898           count++;
2899           str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2900           str.len = TREE_STRING_LENGTH (tok->u.value);
2901           if (tok->type == CPP_WSTRING)
2902             wide = true;
2903
2904           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2905
2906           tok = cp_lexer_peek_token (parser->lexer);
2907         }
2908       while (cp_parser_is_string_literal (tok));
2909
2910       strs = (cpp_string *) obstack_finish (&str_ob);
2911     }
2912
2913   if (wide && !wide_ok)
2914     {
2915       cp_parser_error (parser, "a wide string is invalid in this context");
2916       wide = false;
2917     }
2918
2919   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2920       (parse_in, strs, count, &istr, wide))
2921     {
2922       value = build_string (istr.len, (char *)istr.text);
2923       free ((void *)istr.text);
2924
2925       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2926       value = fix_string_type (value);
2927     }
2928   else
2929     /* cpp_interpret_string has issued an error.  */
2930     value = error_mark_node;
2931
2932   if (count > 1)
2933     obstack_free (&str_ob, 0);
2934
2935   return value;
2936 }
2937
2938
2939 /* Basic concepts [gram.basic]  */
2940
2941 /* Parse a translation-unit.
2942
2943    translation-unit:
2944      declaration-seq [opt]
2945
2946    Returns TRUE if all went well.  */
2947
2948 static bool
2949 cp_parser_translation_unit (cp_parser* parser)
2950 {
2951   /* The address of the first non-permanent object on the declarator
2952      obstack.  */
2953   static void *declarator_obstack_base;
2954
2955   bool success;
2956
2957   /* Create the declarator obstack, if necessary.  */
2958   if (!cp_error_declarator)
2959     {
2960       gcc_obstack_init (&declarator_obstack);
2961       /* Create the error declarator.  */
2962       cp_error_declarator = make_declarator (cdk_error);
2963       /* Create the empty parameter list.  */
2964       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2965       /* Remember where the base of the declarator obstack lies.  */
2966       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2967     }
2968
2969   cp_parser_declaration_seq_opt (parser);
2970
2971   /* If there are no tokens left then all went well.  */
2972   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2973     {
2974       /* Get rid of the token array; we don't need it any more.  */
2975       cp_lexer_destroy (parser->lexer);
2976       parser->lexer = NULL;
2977
2978       /* This file might have been a context that's implicitly extern
2979          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2980       if (parser->implicit_extern_c)
2981         {
2982           pop_lang_context ();
2983           parser->implicit_extern_c = false;
2984         }
2985
2986       /* Finish up.  */
2987       finish_translation_unit ();
2988
2989       success = true;
2990     }
2991   else
2992     {
2993       cp_parser_error (parser, "expected declaration");
2994       success = false;
2995     }
2996
2997   /* Make sure the declarator obstack was fully cleaned up.  */
2998   gcc_assert (obstack_next_free (&declarator_obstack)
2999               == declarator_obstack_base);
3000
3001   /* All went well.  */
3002   return success;
3003 }
3004
3005 /* Expressions [gram.expr] */
3006
3007 /* Parse a primary-expression.
3008
3009    primary-expression:
3010      literal
3011      this
3012      ( expression )
3013      id-expression
3014
3015    GNU Extensions:
3016
3017    primary-expression:
3018      ( compound-statement )
3019      __builtin_va_arg ( assignment-expression , type-id )
3020      __builtin_offsetof ( type-id , offsetof-expression )
3021
3022    C++ Extensions:
3023      __has_nothrow_assign ( type-id )   
3024      __has_nothrow_constructor ( type-id )
3025      __has_nothrow_copy ( type-id )
3026      __has_trivial_assign ( type-id )   
3027      __has_trivial_constructor ( type-id )
3028      __has_trivial_copy ( type-id )
3029      __has_trivial_destructor ( type-id )
3030      __has_virtual_destructor ( type-id )     
3031      __is_abstract ( type-id )
3032      __is_base_of ( type-id , type-id )
3033      __is_class ( type-id )
3034      __is_convertible_to ( type-id , type-id )     
3035      __is_empty ( type-id )
3036      __is_enum ( type-id )
3037      __is_pod ( type-id )
3038      __is_polymorphic ( type-id )
3039      __is_union ( type-id )
3040
3041    Objective-C++ Extension:
3042
3043    primary-expression:
3044      objc-expression
3045
3046    literal:
3047      __null
3048
3049    ADDRESS_P is true iff this expression was immediately preceded by
3050    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3051    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3052    true iff this expression is a template argument.
3053
3054    Returns a representation of the expression.  Upon return, *IDK
3055    indicates what kind of id-expression (if any) was present.  */
3056
3057 static tree
3058 cp_parser_primary_expression (cp_parser *parser,
3059                               bool address_p,
3060                               bool cast_p,
3061                               bool template_arg_p,
3062                               cp_id_kind *idk)
3063 {
3064   cp_token *token;
3065
3066   /* Assume the primary expression is not an id-expression.  */
3067   *idk = CP_ID_KIND_NONE;
3068
3069   /* Peek at the next token.  */
3070   token = cp_lexer_peek_token (parser->lexer);
3071   switch (token->type)
3072     {
3073       /* literal:
3074            integer-literal
3075            character-literal
3076            floating-literal
3077            string-literal
3078            boolean-literal  */
3079     case CPP_CHAR:
3080     case CPP_WCHAR:
3081     case CPP_NUMBER:
3082       token = cp_lexer_consume_token (parser->lexer);
3083       /* Floating-point literals are only allowed in an integral
3084          constant expression if they are cast to an integral or
3085          enumeration type.  */
3086       if (TREE_CODE (token->u.value) == REAL_CST
3087           && parser->integral_constant_expression_p
3088           && pedantic)
3089         {
3090           /* CAST_P will be set even in invalid code like "int(2.7 +
3091              ...)".   Therefore, we have to check that the next token
3092              is sure to end the cast.  */
3093           if (cast_p)
3094             {
3095               cp_token *next_token;
3096
3097               next_token = cp_lexer_peek_token (parser->lexer);
3098               if (/* The comma at the end of an
3099                      enumerator-definition.  */
3100                   next_token->type != CPP_COMMA
3101                   /* The curly brace at the end of an enum-specifier.  */
3102                   && next_token->type != CPP_CLOSE_BRACE
3103                   /* The end of a statement.  */
3104                   && next_token->type != CPP_SEMICOLON
3105                   /* The end of the cast-expression.  */
3106                   && next_token->type != CPP_CLOSE_PAREN
3107                   /* The end of an array bound.  */
3108                   && next_token->type != CPP_CLOSE_SQUARE
3109                   /* The closing ">" in a template-argument-list.  */
3110                   && (next_token->type != CPP_GREATER
3111                       || parser->greater_than_is_operator_p)
3112                   /* C++0x only: A ">>" treated like two ">" tokens,
3113                      in a template-argument-list.  */
3114                   && (next_token->type != CPP_RSHIFT
3115                       || (cxx_dialect == cxx98)
3116                       || parser->greater_than_is_operator_p))
3117                 cast_p = false;
3118             }
3119
3120           /* If we are within a cast, then the constraint that the
3121              cast is to an integral or enumeration type will be
3122              checked at that point.  If we are not within a cast, then
3123              this code is invalid.  */
3124           if (!cast_p)
3125             cp_parser_non_integral_constant_expression
3126               (parser, "floating-point literal");
3127         }
3128       return token->u.value;
3129
3130     case CPP_STRING:
3131     case CPP_WSTRING:
3132       /* ??? Should wide strings be allowed when parser->translate_strings_p
3133          is false (i.e. in attributes)?  If not, we can kill the third
3134          argument to cp_parser_string_literal.  */
3135       return cp_parser_string_literal (parser,
3136                                        parser->translate_strings_p,
3137                                        true);
3138
3139     case CPP_OPEN_PAREN:
3140       {
3141         tree expr;
3142         bool saved_greater_than_is_operator_p;
3143
3144         /* Consume the `('.  */
3145         cp_lexer_consume_token (parser->lexer);
3146         /* Within a parenthesized expression, a `>' token is always
3147            the greater-than operator.  */
3148         saved_greater_than_is_operator_p
3149           = parser->greater_than_is_operator_p;
3150         parser->greater_than_is_operator_p = true;
3151         /* If we see `( { ' then we are looking at the beginning of
3152            a GNU statement-expression.  */
3153         if (cp_parser_allow_gnu_extensions_p (parser)
3154             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3155           {
3156             /* Statement-expressions are not allowed by the standard.  */
3157             if (pedantic)
3158               pedwarn ("ISO C++ forbids braced-groups within expressions");
3159
3160             /* And they're not allowed outside of a function-body; you
3161                cannot, for example, write:
3162
3163                  int i = ({ int j = 3; j + 1; });
3164
3165                at class or namespace scope.  */
3166             if (!parser->in_function_body)
3167               {
3168                 error ("statement-expressions are allowed only inside functions");
3169                 cp_parser_skip_to_end_of_block_or_statement (parser);
3170                 expr = error_mark_node;
3171               }
3172             else
3173               {
3174                 /* Start the statement-expression.  */
3175                 expr = begin_stmt_expr ();
3176                 /* Parse the compound-statement.  */
3177                 cp_parser_compound_statement (parser, expr, false);
3178                 /* Finish up.  */
3179                 expr = finish_stmt_expr (expr, false);
3180               }
3181           }
3182         else
3183           {
3184             /* Parse the parenthesized expression.  */
3185             expr = cp_parser_expression (parser, cast_p);
3186             /* Let the front end know that this expression was
3187                enclosed in parentheses. This matters in case, for
3188                example, the expression is of the form `A::B', since
3189                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3190                not.  */
3191             finish_parenthesized_expr (expr);
3192           }
3193         /* The `>' token might be the end of a template-id or
3194            template-parameter-list now.  */
3195         parser->greater_than_is_operator_p
3196           = saved_greater_than_is_operator_p;
3197         /* Consume the `)'.  */
3198         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3199           cp_parser_skip_to_end_of_statement (parser);
3200
3201         return expr;
3202       }
3203
3204     case CPP_KEYWORD:
3205       switch (token->keyword)
3206         {
3207           /* These two are the boolean literals.  */
3208         case RID_TRUE:
3209           cp_lexer_consume_token (parser->lexer);
3210           return boolean_true_node;
3211         case RID_FALSE:
3212           cp_lexer_consume_token (parser->lexer);
3213           return boolean_false_node;
3214
3215           /* The `__null' literal.  */
3216         case RID_NULL:
3217           cp_lexer_consume_token (parser->lexer);
3218           return null_node;
3219
3220           /* Recognize the `this' keyword.  */
3221         case RID_THIS:
3222           cp_lexer_consume_token (parser->lexer);
3223           if (parser->local_variables_forbidden_p)
3224             {
3225               error ("%<this%> may not be used in this context");
3226               return error_mark_node;
3227             }
3228           /* Pointers cannot appear in constant-expressions.  */
3229           if (cp_parser_non_integral_constant_expression (parser,
3230                                                           "`this'"))
3231             return error_mark_node;
3232           return finish_this_expr ();
3233
3234           /* The `operator' keyword can be the beginning of an
3235              id-expression.  */
3236         case RID_OPERATOR:
3237           goto id_expression;
3238
3239         case RID_FUNCTION_NAME:
3240         case RID_PRETTY_FUNCTION_NAME:
3241         case RID_C99_FUNCTION_NAME:
3242           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3243              __func__ are the names of variables -- but they are
3244              treated specially.  Therefore, they are handled here,
3245              rather than relying on the generic id-expression logic
3246              below.  Grammatically, these names are id-expressions.
3247
3248              Consume the token.  */
3249           token = cp_lexer_consume_token (parser->lexer);
3250           /* Look up the name.  */
3251           return finish_fname (token->u.value);
3252
3253         case RID_VA_ARG:
3254           {
3255             tree expression;
3256             tree type;
3257
3258             /* The `__builtin_va_arg' construct is used to handle
3259                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3260             cp_lexer_consume_token (parser->lexer);
3261             /* Look for the opening `('.  */
3262             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3263             /* Now, parse the assignment-expression.  */
3264             expression = cp_parser_assignment_expression (parser,
3265                                                           /*cast_p=*/false);
3266             /* Look for the `,'.  */
3267             cp_parser_require (parser, CPP_COMMA, "`,'");
3268             /* Parse the type-id.  */
3269             type = cp_parser_type_id (parser);
3270             /* Look for the closing `)'.  */
3271             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3272             /* Using `va_arg' in a constant-expression is not
3273                allowed.  */
3274             if (cp_parser_non_integral_constant_expression (parser,
3275                                                             "`va_arg'"))
3276               return error_mark_node;
3277             return build_x_va_arg (expression, type);
3278           }
3279
3280         case RID_OFFSETOF:
3281           return cp_parser_builtin_offsetof (parser);
3282
3283         case RID_HAS_NOTHROW_ASSIGN:
3284         case RID_HAS_NOTHROW_CONSTRUCTOR:
3285         case RID_HAS_NOTHROW_COPY:        
3286         case RID_HAS_TRIVIAL_ASSIGN:
3287         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3288         case RID_HAS_TRIVIAL_COPY:        
3289         case RID_HAS_TRIVIAL_DESTRUCTOR:
3290         case RID_HAS_VIRTUAL_DESTRUCTOR:
3291         case RID_IS_ABSTRACT:
3292         case RID_IS_BASE_OF:
3293         case RID_IS_CLASS:
3294         case RID_IS_CONVERTIBLE_TO:
3295         case RID_IS_EMPTY:
3296         case RID_IS_ENUM:
3297         case RID_IS_POD:
3298         case RID_IS_POLYMORPHIC:
3299         case RID_IS_UNION:
3300           return cp_parser_trait_expr (parser, token->keyword);
3301
3302         /* Objective-C++ expressions.  */
3303         case RID_AT_ENCODE:
3304         case RID_AT_PROTOCOL:
3305         case RID_AT_SELECTOR:
3306           return cp_parser_objc_expression (parser);
3307
3308         default:
3309           cp_parser_error (parser, "expected primary-expression");
3310           return error_mark_node;
3311         }
3312
3313       /* An id-expression can start with either an identifier, a
3314          `::' as the beginning of a qualified-id, or the "operator"
3315          keyword.  */
3316     case CPP_NAME:
3317     case CPP_SCOPE:
3318     case CPP_TEMPLATE_ID:
3319     case CPP_NESTED_NAME_SPECIFIER:
3320       {
3321         tree id_expression;
3322         tree decl;
3323         const char *error_msg;
3324         bool template_p;
3325         bool done;
3326
3327       id_expression:
3328         /* Parse the id-expression.  */
3329         id_expression
3330           = cp_parser_id_expression (parser,
3331                                      /*template_keyword_p=*/false,
3332                                      /*check_dependency_p=*/true,
3333                                      &template_p,
3334                                      /*declarator_p=*/false,
3335                                      /*optional_p=*/false);
3336         if (id_expression == error_mark_node)
3337           return error_mark_node;
3338         token = cp_lexer_peek_token (parser->lexer);
3339         done = (token->type != CPP_OPEN_SQUARE
3340                 && token->type != CPP_OPEN_PAREN
3341                 && token->type != CPP_DOT
3342                 && token->type != CPP_DEREF
3343                 && token->type != CPP_PLUS_PLUS
3344                 && token->type != CPP_MINUS_MINUS);
3345         /* If we have a template-id, then no further lookup is
3346            required.  If the template-id was for a template-class, we
3347            will sometimes have a TYPE_DECL at this point.  */
3348         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3349                  || TREE_CODE (id_expression) == TYPE_DECL)
3350           decl = id_expression;
3351         /* Look up the name.  */
3352         else
3353           {
3354             tree ambiguous_decls;
3355
3356             decl = cp_parser_lookup_name (parser, id_expression,
3357                                           none_type,
3358                                           template_p,
3359                                           /*is_namespace=*/false,
3360                                           /*check_dependency=*/true,
3361                                           &ambiguous_decls);
3362             /* If the lookup was ambiguous, an error will already have
3363                been issued.  */
3364             if (ambiguous_decls)
3365               return error_mark_node;
3366
3367             /* In Objective-C++, an instance variable (ivar) may be preferred
3368                to whatever cp_parser_lookup_name() found.  */
3369             decl = objc_lookup_ivar (decl, id_expression);
3370
3371             /* If name lookup gives us a SCOPE_REF, then the
3372                qualifying scope was dependent.  */
3373             if (TREE_CODE (decl) == SCOPE_REF)
3374               {
3375                 /* At this point, we do not know if DECL is a valid
3376                    integral constant expression.  We assume that it is
3377                    in fact such an expression, so that code like:
3378
3379                       template <int N> struct A {
3380                         int a[B<N>::i];
3381                       };
3382                      
3383                    is accepted.  At template-instantiation time, we
3384                    will check that B<N>::i is actually a constant.  */
3385                 return decl;
3386               }
3387             /* Check to see if DECL is a local variable in a context
3388                where that is forbidden.  */
3389             if (parser->local_variables_forbidden_p
3390                 && local_variable_p (decl))
3391               {
3392                 /* It might be that we only found DECL because we are
3393                    trying to be generous with pre-ISO scoping rules.
3394                    For example, consider:
3395
3396                      int i;
3397                      void g() {
3398                        for (int i = 0; i < 10; ++i) {}
3399                        extern void f(int j = i);
3400                      }
3401
3402                    Here, name look up will originally find the out
3403                    of scope `i'.  We need to issue a warning message,
3404                    but then use the global `i'.  */
3405                 decl = check_for_out_of_scope_variable (decl);
3406                 if (local_variable_p (decl))
3407                   {
3408                     error ("local variable %qD may not appear in this context",
3409                            decl);
3410                     return error_mark_node;
3411                   }
3412               }
3413           }
3414
3415         decl = (finish_id_expression
3416                 (id_expression, decl, parser->scope,
3417                  idk,
3418                  parser->integral_constant_expression_p,
3419                  parser->allow_non_integral_constant_expression_p,
3420                  &parser->non_integral_constant_expression_p,
3421                  template_p, done, address_p,
3422                  template_arg_p,
3423                  &error_msg));
3424         if (error_msg)
3425           cp_parser_error (parser, error_msg);
3426         return decl;
3427       }
3428
3429       /* Anything else is an error.  */
3430     default:
3431       /* ...unless we have an Objective-C++ message or string literal,
3432          that is.  */
3433       if (c_dialect_objc ()
3434           && (token->type == CPP_OPEN_SQUARE
3435               || token->type == CPP_OBJC_STRING))
3436         return cp_parser_objc_expression (parser);
3437
3438       cp_parser_error (parser, "expected primary-expression");
3439       return error_mark_node;
3440     }
3441 }
3442
3443 /* Parse an id-expression.
3444
3445    id-expression:
3446      unqualified-id
3447      qualified-id
3448
3449    qualified-id:
3450      :: [opt] nested-name-specifier template [opt] unqualified-id
3451      :: identifier
3452      :: operator-function-id
3453      :: template-id
3454
3455    Return a representation of the unqualified portion of the
3456    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3457    a `::' or nested-name-specifier.
3458
3459    Often, if the id-expression was a qualified-id, the caller will
3460    want to make a SCOPE_REF to represent the qualified-id.  This
3461    function does not do this in order to avoid wastefully creating
3462    SCOPE_REFs when they are not required.
3463
3464    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3465    `template' keyword.
3466
3467    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3468    uninstantiated templates.
3469
3470    If *TEMPLATE_P is non-NULL, it is set to true iff the
3471    `template' keyword is used to explicitly indicate that the entity
3472    named is a template.
3473
3474    If DECLARATOR_P is true, the id-expression is appearing as part of
3475    a declarator, rather than as part of an expression.  */
3476
3477 static tree
3478 cp_parser_id_expression (cp_parser *parser,
3479                          bool template_keyword_p,
3480                          bool check_dependency_p,
3481                          bool *template_p,
3482                          bool declarator_p,
3483                          bool optional_p)
3484 {
3485   bool global_scope_p;
3486   bool nested_name_specifier_p;
3487
3488   /* Assume the `template' keyword was not used.  */
3489   if (template_p)
3490     *template_p = template_keyword_p;
3491
3492   /* Look for the optional `::' operator.  */
3493   global_scope_p
3494     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3495        != NULL_TREE);
3496   /* Look for the optional nested-name-specifier.  */
3497   nested_name_specifier_p
3498     = (cp_parser_nested_name_specifier_opt (parser,
3499                                             /*typename_keyword_p=*/false,
3500                                             check_dependency_p,
3501                                             /*type_p=*/false,
3502                                             declarator_p)
3503        != NULL_TREE);
3504   /* If there is a nested-name-specifier, then we are looking at
3505      the first qualified-id production.  */
3506   if (nested_name_specifier_p)
3507     {
3508       tree saved_scope;
3509       tree saved_object_scope;
3510       tree saved_qualifying_scope;
3511       tree unqualified_id;
3512       bool is_template;
3513
3514       /* See if the next token is the `template' keyword.  */
3515       if (!template_p)
3516         template_p = &is_template;
3517       *template_p = cp_parser_optional_template_keyword (parser);
3518       /* Name lookup we do during the processing of the
3519          unqualified-id might obliterate SCOPE.  */
3520       saved_scope = parser->scope;
3521       saved_object_scope = parser->object_scope;
3522       saved_qualifying_scope = parser->qualifying_scope;
3523       /* Process the final unqualified-id.  */
3524       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3525                                                  check_dependency_p,
3526                                                  declarator_p,
3527                                                  /*optional_p=*/false);
3528       /* Restore the SAVED_SCOPE for our caller.  */
3529       parser->scope = saved_scope;
3530       parser->object_scope = saved_object_scope;
3531       parser->qualifying_scope = saved_qualifying_scope;
3532
3533       return unqualified_id;
3534     }
3535   /* Otherwise, if we are in global scope, then we are looking at one
3536      of the other qualified-id productions.  */
3537   else if (global_scope_p)
3538     {
3539       cp_token *token;
3540       tree id;
3541
3542       /* Peek at the next token.  */
3543       token = cp_lexer_peek_token (parser->lexer);
3544
3545       /* If it's an identifier, and the next token is not a "<", then
3546          we can avoid the template-id case.  This is an optimization
3547          for this common case.  */
3548       if (token->type == CPP_NAME
3549           && !cp_parser_nth_token_starts_template_argument_list_p
3550                (parser, 2))
3551         return cp_parser_identifier (parser);
3552
3553       cp_parser_parse_tentatively (parser);
3554       /* Try a template-id.  */
3555       id = cp_parser_template_id (parser,
3556                                   /*template_keyword_p=*/false,
3557                                   /*check_dependency_p=*/true,
3558                                   declarator_p);
3559       /* If that worked, we're done.  */
3560       if (cp_parser_parse_definitely (parser))
3561         return id;
3562
3563       /* Peek at the next token.  (Changes in the token buffer may
3564          have invalidated the pointer obtained above.)  */
3565       token = cp_lexer_peek_token (parser->lexer);
3566
3567       switch (token->type)
3568         {
3569         case CPP_NAME:
3570           return cp_parser_identifier (parser);
3571
3572         case CPP_KEYWORD:
3573           if (token->keyword == RID_OPERATOR)
3574             return cp_parser_operator_function_id (parser);
3575           /* Fall through.  */
3576
3577         default:
3578           cp_parser_error (parser, "expected id-expression");
3579           return error_mark_node;
3580         }
3581     }
3582   else
3583     return cp_parser_unqualified_id (parser, template_keyword_p,
3584                                      /*check_dependency_p=*/true,
3585                                      declarator_p,
3586                                      optional_p);
3587 }
3588
3589 /* Parse an unqualified-id.
3590
3591    unqualified-id:
3592      identifier
3593      operator-function-id
3594      conversion-function-id
3595      ~ class-name
3596      template-id
3597
3598    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3599    keyword, in a construct like `A::template ...'.
3600
3601    Returns a representation of unqualified-id.  For the `identifier'
3602    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3603    production a BIT_NOT_EXPR is returned; the operand of the
3604    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3605    other productions, see the documentation accompanying the
3606    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3607    names are looked up in uninstantiated templates.  If DECLARATOR_P
3608    is true, the unqualified-id is appearing as part of a declarator,
3609    rather than as part of an expression.  */
3610
3611 static tree
3612 cp_parser_unqualified_id (cp_parser* parser,
3613                           bool template_keyword_p,
3614                           bool check_dependency_p,
3615                           bool declarator_p,
3616                           bool optional_p)
3617 {
3618   cp_token *token;
3619
3620   /* Peek at the next token.  */
3621   token = cp_lexer_peek_token (parser->lexer);
3622
3623   switch (token->type)
3624     {
3625     case CPP_NAME:
3626       {
3627         tree id;
3628
3629         /* We don't know yet whether or not this will be a
3630            template-id.  */
3631         cp_parser_parse_tentatively (parser);
3632         /* Try a template-id.  */
3633         id = cp_parser_template_id (parser, template_keyword_p,
3634                                     check_dependency_p,
3635                                     declarator_p);
3636         /* If it worked, we're done.  */
3637         if (cp_parser_parse_definitely (parser))
3638           return id;
3639         /* Otherwise, it's an ordinary identifier.  */
3640         return cp_parser_identifier (parser);
3641       }
3642
3643     case CPP_TEMPLATE_ID:
3644       return cp_parser_template_id (parser, template_keyword_p,
3645                                     check_dependency_p,
3646                                     declarator_p);
3647
3648     case CPP_COMPL:
3649       {
3650         tree type_decl;
3651         tree qualifying_scope;
3652         tree object_scope;
3653         tree scope;
3654         bool done;
3655
3656         /* Consume the `~' token.  */
3657         cp_lexer_consume_token (parser->lexer);
3658         /* Parse the class-name.  The standard, as written, seems to
3659            say that:
3660
3661              template <typename T> struct S { ~S (); };
3662              template <typename T> S<T>::~S() {}
3663
3664            is invalid, since `~' must be followed by a class-name, but
3665            `S<T>' is dependent, and so not known to be a class.
3666            That's not right; we need to look in uninstantiated
3667            templates.  A further complication arises from:
3668
3669              template <typename T> void f(T t) {
3670                t.T::~T();
3671              }
3672
3673            Here, it is not possible to look up `T' in the scope of `T'
3674            itself.  We must look in both the current scope, and the
3675            scope of the containing complete expression.
3676
3677            Yet another issue is:
3678
3679              struct S {
3680                int S;
3681                ~S();
3682              };
3683
3684              S::~S() {}
3685
3686            The standard does not seem to say that the `S' in `~S'
3687            should refer to the type `S' and not the data member
3688            `S::S'.  */
3689
3690         /* DR 244 says that we look up the name after the "~" in the
3691            same scope as we looked up the qualifying name.  That idea
3692            isn't fully worked out; it's more complicated than that.  */
3693         scope = parser->scope;
3694         object_scope = parser->object_scope;
3695         qualifying_scope = parser->qualifying_scope;
3696
3697         /* Check for invalid scopes.  */
3698         if (scope == error_mark_node)
3699           {
3700             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3701               cp_lexer_consume_token (parser->lexer);
3702             return error_mark_node;
3703           }
3704         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3705           {
3706             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3707               error ("scope %qT before %<~%> is not a class-name", scope);
3708             cp_parser_simulate_error (parser);
3709             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3710               cp_lexer_consume_token (parser->lexer);
3711             return error_mark_node;
3712           }
3713         gcc_assert (!scope || TYPE_P (scope));
3714
3715         /* If the name is of the form "X::~X" it's OK.  */
3716         token = cp_lexer_peek_token (parser->lexer);
3717         if (scope
3718             && token->type == CPP_NAME
3719             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3720                 == CPP_OPEN_PAREN)
3721             && constructor_name_p (token->u.value, scope))
3722           {
3723             cp_lexer_consume_token (parser->lexer);
3724             return build_nt (BIT_NOT_EXPR, scope);
3725           }
3726
3727         /* If there was an explicit qualification (S::~T), first look
3728            in the scope given by the qualification (i.e., S).  */
3729         done = false;
3730         type_decl = NULL_TREE;
3731         if (scope)
3732           {
3733             cp_parser_parse_tentatively (parser);
3734             type_decl = cp_parser_class_name (parser,
3735                                               /*typename_keyword_p=*/false,
3736                                               /*template_keyword_p=*/false,
3737                                               none_type,
3738                                               /*check_dependency=*/false,
3739                                               /*class_head_p=*/false,
3740                                               declarator_p);
3741             if (cp_parser_parse_definitely (parser))
3742               done = true;
3743           }
3744         /* In "N::S::~S", look in "N" as well.  */
3745         if (!done && scope && qualifying_scope)
3746           {
3747             cp_parser_parse_tentatively (parser);
3748             parser->scope = qualifying_scope;
3749             parser->object_scope = NULL_TREE;
3750             parser->qualifying_scope = NULL_TREE;
3751             type_decl
3752               = cp_parser_class_name (parser,
3753                                       /*typename_keyword_p=*/false,
3754                                       /*template_keyword_p=*/false,
3755                                       none_type,
3756                                       /*check_dependency=*/false,
3757                                       /*class_head_p=*/false,
3758                                       declarator_p);
3759             if (cp_parser_parse_definitely (parser))
3760               done = true;
3761           }
3762         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3763         else if (!done && object_scope)
3764           {
3765             cp_parser_parse_tentatively (parser);
3766             parser->scope = object_scope;
3767             parser->object_scope = NULL_TREE;
3768             parser->qualifying_scope = NULL_TREE;
3769             type_decl
3770               = cp_parser_class_name (parser,
3771                                       /*typename_keyword_p=*/false,
3772                                       /*template_keyword_p=*/false,
3773                                       none_type,
3774                                       /*check_dependency=*/false,
3775                                       /*class_head_p=*/false,
3776                                       declarator_p);
3777             if (cp_parser_parse_definitely (parser))
3778               done = true;
3779           }
3780         /* Look in the surrounding context.  */
3781         if (!done)
3782           {
3783             parser->scope = NULL_TREE;
3784             parser->object_scope = NULL_TREE;
3785             parser->qualifying_scope = NULL_TREE;
3786             type_decl
3787               = cp_parser_class_name (parser,
3788                                       /*typename_keyword_p=*/false,
3789                                       /*template_keyword_p=*/false,
3790                                       none_type,
3791                                       /*check_dependency=*/false,
3792                                       /*class_head_p=*/false,
3793                                       declarator_p);
3794           }
3795         /* If an error occurred, assume that the name of the
3796            destructor is the same as the name of the qualifying
3797            class.  That allows us to keep parsing after running
3798            into ill-formed destructor names.  */
3799         if (type_decl == error_mark_node && scope)
3800           return build_nt (BIT_NOT_EXPR, scope);
3801         else if (type_decl == error_mark_node)
3802           return error_mark_node;
3803
3804         /* Check that destructor name and scope match.  */
3805         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3806           {
3807             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3808               error ("declaration of %<~%T%> as member of %qT",
3809                      type_decl, scope);
3810             cp_parser_simulate_error (parser);
3811             return error_mark_node;
3812           }
3813
3814         /* [class.dtor]
3815
3816            A typedef-name that names a class shall not be used as the
3817            identifier in the declarator for a destructor declaration.  */
3818         if (declarator_p
3819             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3820             && !DECL_SELF_REFERENCE_P (type_decl)
3821             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3822           error ("typedef-name %qD used as destructor declarator",
3823                  type_decl);
3824
3825         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3826       }
3827
3828     case CPP_KEYWORD:
3829       if (token->keyword == RID_OPERATOR)
3830         {
3831           tree id;
3832
3833           /* This could be a template-id, so we try that first.  */
3834           cp_parser_parse_tentatively (parser);
3835           /* Try a template-id.  */
3836           id = cp_parser_template_id (parser, template_keyword_p,
3837                                       /*check_dependency_p=*/true,
3838                                       declarator_p);
3839           /* If that worked, we're done.  */
3840           if (cp_parser_parse_definitely (parser))
3841             return id;
3842           /* We still don't know whether we're looking at an
3843              operator-function-id or a conversion-function-id.  */
3844           cp_parser_parse_tentatively (parser);
3845           /* Try an operator-function-id.  */
3846           id = cp_parser_operator_function_id (parser);
3847           /* If that didn't work, try a conversion-function-id.  */
3848           if (!cp_parser_parse_definitely (parser))
3849             id = cp_parser_conversion_function_id (parser);
3850
3851           return id;
3852         }
3853       /* Fall through.  */
3854
3855     default:
3856       if (optional_p)
3857         return NULL_TREE;
3858       cp_parser_error (parser, "expected unqualified-id");
3859       return error_mark_node;
3860     }
3861 }
3862
3863 /* Parse an (optional) nested-name-specifier.
3864
3865    nested-name-specifier:
3866      class-or-namespace-name :: nested-name-specifier [opt]
3867      class-or-namespace-name :: template nested-name-specifier [opt]
3868
3869    PARSER->SCOPE should be set appropriately before this function is
3870    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3871    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3872    in name lookups.
3873
3874    Sets PARSER->SCOPE to the class (TYPE) or namespace
3875    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3876    it unchanged if there is no nested-name-specifier.  Returns the new
3877    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3878
3879    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3880    part of a declaration and/or decl-specifier.  */
3881
3882 static tree
3883 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3884                                      bool typename_keyword_p,
3885                                      bool check_dependency_p,
3886                                      bool type_p,
3887                                      bool is_declaration)
3888 {
3889   bool success = false;
3890   cp_token_position start = 0;
3891   cp_token *token;
3892
3893   /* Remember where the nested-name-specifier starts.  */
3894   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3895     {
3896       start = cp_lexer_token_position (parser->lexer, false);
3897       push_deferring_access_checks (dk_deferred);
3898     }
3899
3900   while (true)
3901     {
3902       tree new_scope;
3903       tree old_scope;
3904       tree saved_qualifying_scope;
3905       bool template_keyword_p;
3906
3907       /* Spot cases that cannot be the beginning of a
3908          nested-name-specifier.  */
3909       token = cp_lexer_peek_token (parser->lexer);
3910
3911       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3912          the already parsed nested-name-specifier.  */
3913       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3914         {
3915           /* Grab the nested-name-specifier and continue the loop.  */
3916           cp_parser_pre_parsed_nested_name_specifier (parser);
3917           /* If we originally encountered this nested-name-specifier
3918              with IS_DECLARATION set to false, we will not have
3919              resolved TYPENAME_TYPEs, so we must do so here.  */
3920           if (is_declaration
3921               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3922             {
3923               new_scope = resolve_typename_type (parser->scope,
3924                                                  /*only_current_p=*/false);
3925               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3926                 parser->scope = new_scope;
3927             }
3928           success = true;
3929           continue;
3930         }
3931
3932       /* Spot cases that cannot be the beginning of a
3933          nested-name-specifier.  On the second and subsequent times
3934          through the loop, we look for the `template' keyword.  */
3935       if (success && token->keyword == RID_TEMPLATE)
3936         ;
3937       /* A template-id can start a nested-name-specifier.  */
3938       else if (token->type == CPP_TEMPLATE_ID)
3939         ;
3940       else
3941         {
3942           /* If the next token is not an identifier, then it is
3943              definitely not a class-or-namespace-name.  */
3944           if (token->type != CPP_NAME)
3945             break;
3946           /* If the following token is neither a `<' (to begin a
3947              template-id), nor a `::', then we are not looking at a
3948              nested-name-specifier.  */
3949           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3950           if (token->type != CPP_SCOPE
3951               && !cp_parser_nth_token_starts_template_argument_list_p
3952                   (parser, 2))
3953             break;
3954         }
3955
3956       /* The nested-name-specifier is optional, so we parse
3957          tentatively.  */
3958       cp_parser_parse_tentatively (parser);
3959
3960       /* Look for the optional `template' keyword, if this isn't the
3961          first time through the loop.  */
3962       if (success)
3963         template_keyword_p = cp_parser_optional_template_keyword (parser);
3964       else
3965         template_keyword_p = false;
3966
3967       /* Save the old scope since the name lookup we are about to do
3968          might destroy it.  */
3969       old_scope = parser->scope;
3970       saved_qualifying_scope = parser->qualifying_scope;
3971       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3972          look up names in "X<T>::I" in order to determine that "Y" is
3973          a template.  So, if we have a typename at this point, we make
3974          an effort to look through it.  */
3975       if (is_declaration
3976           && !typename_keyword_p
3977           && parser->scope
3978           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3979         parser->scope = resolve_typename_type (parser->scope,
3980                                                /*only_current_p=*/false);
3981       /* Parse the qualifying entity.  */
3982       new_scope
3983         = cp_parser_class_or_namespace_name (parser,
3984                                              typename_keyword_p,
3985                                              template_keyword_p,
3986                                              check_dependency_p,
3987                                              type_p,
3988                                              is_declaration);
3989       /* Look for the `::' token.  */
3990       cp_parser_require (parser, CPP_SCOPE, "`::'");
3991
3992       /* If we found what we wanted, we keep going; otherwise, we're
3993          done.  */
3994       if (!cp_parser_parse_definitely (parser))
3995         {
3996           bool error_p = false;
3997
3998           /* Restore the OLD_SCOPE since it was valid before the
3999              failed attempt at finding the last
4000              class-or-namespace-name.  */
4001           parser->scope = old_scope;
4002           parser->qualifying_scope = saved_qualifying_scope;
4003           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4004             break;
4005           /* If the next token is an identifier, and the one after
4006              that is a `::', then any valid interpretation would have
4007              found a class-or-namespace-name.  */
4008           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4009                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4010                      == CPP_SCOPE)
4011                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4012                      != CPP_COMPL))
4013             {
4014               token = cp_lexer_consume_token (parser->lexer);
4015               if (!error_p)
4016                 {
4017                   if (!token->ambiguous_p)
4018                     {
4019                       tree decl;
4020                       tree ambiguous_decls;
4021
4022                       decl = cp_parser_lookup_name (parser, token->u.value,
4023                                                     none_type,
4024                                                     /*is_template=*/false,
4025                                                     /*is_namespace=*/false,
4026                                                     /*check_dependency=*/true,
4027                                                     &ambiguous_decls);
4028                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4029                         error ("%qD used without template parameters", decl);
4030                       else if (ambiguous_decls)
4031                         {
4032                           error ("reference to %qD is ambiguous",
4033                                  token->u.value);
4034                           print_candidates (ambiguous_decls);
4035                           decl = error_mark_node;
4036                         }
4037                       else
4038                         cp_parser_name_lookup_error
4039                           (parser, token->u.value, decl,
4040                            "is not a class or namespace");
4041                     }
4042                   parser->scope = error_mark_node;
4043                   error_p = true;
4044                   /* Treat this as a successful nested-name-specifier
4045                      due to:
4046
4047                      [basic.lookup.qual]
4048
4049                      If the name found is not a class-name (clause
4050                      _class_) or namespace-name (_namespace.def_), the
4051                      program is ill-formed.  */
4052                   success = true;
4053                 }
4054               cp_lexer_consume_token (parser->lexer);
4055             }
4056           break;
4057         }
4058       /* We've found one valid nested-name-specifier.  */
4059       success = true;
4060       /* Name lookup always gives us a DECL.  */
4061       if (TREE_CODE (new_scope) == TYPE_DECL)
4062         new_scope = TREE_TYPE (new_scope);
4063       /* Uses of "template" must be followed by actual templates.  */
4064       if (template_keyword_p
4065           && !(CLASS_TYPE_P (new_scope)
4066                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4067                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4068                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4069           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4070                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4071                    == TEMPLATE_ID_EXPR)))
4072         pedwarn (TYPE_P (new_scope)
4073                  ? "%qT is not a template"
4074                  : "%qD is not a template",
4075                  new_scope);
4076       /* If it is a class scope, try to complete it; we are about to
4077          be looking up names inside the class.  */
4078       if (TYPE_P (new_scope)
4079           /* Since checking types for dependency can be expensive,
4080              avoid doing it if the type is already complete.  */
4081           && !COMPLETE_TYPE_P (new_scope)
4082           /* Do not try to complete dependent types.  */
4083           && !dependent_type_p (new_scope))
4084         new_scope = complete_type (new_scope);
4085       /* Make sure we look in the right scope the next time through
4086          the loop.  */
4087       parser->scope = new_scope;
4088     }
4089
4090   /* If parsing tentatively, replace the sequence of tokens that makes
4091      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4092      token.  That way, should we re-parse the token stream, we will
4093      not have to repeat the effort required to do the parse, nor will
4094      we issue duplicate error messages.  */
4095   if (success && start)
4096     {
4097       cp_token *token;
4098
4099       token = cp_lexer_token_at (parser->lexer, start);
4100       /* Reset the contents of the START token.  */
4101       token->type = CPP_NESTED_NAME_SPECIFIER;
4102       /* Retrieve any deferred checks.  Do not pop this access checks yet
4103          so the memory will not be reclaimed during token replacing below.  */
4104       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4105       token->u.tree_check_value->value = parser->scope;
4106       token->u.tree_check_value->checks = get_deferred_access_checks ();
4107       token->u.tree_check_value->qualifying_scope =
4108         parser->qualifying_scope;
4109       token->keyword = RID_MAX;
4110
4111       /* Purge all subsequent tokens.  */
4112       cp_lexer_purge_tokens_after (parser->lexer, start);
4113     }
4114
4115   if (start)
4116     pop_to_parent_deferring_access_checks ();
4117
4118   return success ? parser->scope : NULL_TREE;
4119 }
4120
4121 /* Parse a nested-name-specifier.  See
4122    cp_parser_nested_name_specifier_opt for details.  This function
4123    behaves identically, except that it will an issue an error if no
4124    nested-name-specifier is present.  */
4125
4126 static tree
4127 cp_parser_nested_name_specifier (cp_parser *parser,
4128                                  bool typename_keyword_p,
4129                                  bool check_dependency_p,
4130                                  bool type_p,
4131                                  bool is_declaration)
4132 {
4133   tree scope;
4134
4135   /* Look for the nested-name-specifier.  */
4136   scope = cp_parser_nested_name_specifier_opt (parser,
4137                                                typename_keyword_p,
4138                                                check_dependency_p,
4139                                                type_p,
4140                                                is_declaration);
4141   /* If it was not present, issue an error message.  */
4142   if (!scope)
4143     {
4144       cp_parser_error (parser, "expected nested-name-specifier");
4145       parser->scope = NULL_TREE;
4146     }
4147
4148   return scope;
4149 }
4150
4151 /* Parse a class-or-namespace-name.
4152
4153    class-or-namespace-name:
4154      class-name
4155      namespace-name
4156
4157    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4158    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4159    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4160    TYPE_P is TRUE iff the next name should be taken as a class-name,
4161    even the same name is declared to be another entity in the same
4162    scope.
4163
4164    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4165    specified by the class-or-namespace-name.  If neither is found the
4166    ERROR_MARK_NODE is returned.  */
4167
4168 static tree
4169 cp_parser_class_or_namespace_name (cp_parser *parser,
4170                                    bool typename_keyword_p,
4171                                    bool template_keyword_p,
4172                                    bool check_dependency_p,
4173                                    bool type_p,
4174                                    bool is_declaration)
4175 {
4176   tree saved_scope;
4177   tree saved_qualifying_scope;
4178   tree saved_object_scope;
4179   tree scope;
4180   bool only_class_p;
4181
4182   /* Before we try to parse the class-name, we must save away the
4183      current PARSER->SCOPE since cp_parser_class_name will destroy
4184      it.  */
4185   saved_scope = parser->scope;
4186   saved_qualifying_scope = parser->qualifying_scope;
4187   saved_object_scope = parser->object_scope;
4188   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4189      there is no need to look for a namespace-name.  */
4190   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4191   if (!only_class_p)
4192     cp_parser_parse_tentatively (parser);
4193   scope = cp_parser_class_name (parser,
4194                                 typename_keyword_p,
4195                                 template_keyword_p,
4196                                 type_p ? class_type : none_type,
4197                                 check_dependency_p,
4198                                 /*class_head_p=*/false,
4199                                 is_declaration);
4200   /* If that didn't work, try for a namespace-name.  */
4201   if (!only_class_p && !cp_parser_parse_definitely (parser))
4202     {
4203       /* Restore the saved scope.  */
4204       parser->scope = saved_scope;
4205       parser->qualifying_scope = saved_qualifying_scope;
4206       parser->object_scope = saved_object_scope;
4207       /* If we are not looking at an identifier followed by the scope
4208          resolution operator, then this is not part of a
4209          nested-name-specifier.  (Note that this function is only used
4210          to parse the components of a nested-name-specifier.)  */
4211       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4212           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4213         return error_mark_node;
4214       scope = cp_parser_namespace_name (parser);
4215     }
4216
4217   return scope;
4218 }
4219
4220 /* Parse a postfix-expression.
4221
4222    postfix-expression:
4223      primary-expression
4224      postfix-expression [ expression ]
4225      postfix-expression ( expression-list [opt] )
4226      simple-type-specifier ( expression-list [opt] )
4227      typename :: [opt] nested-name-specifier identifier
4228        ( expression-list [opt] )
4229      typename :: [opt] nested-name-specifier template [opt] template-id
4230        ( expression-list [opt] )
4231      postfix-expression . template [opt] id-expression
4232      postfix-expression -> template [opt] id-expression
4233      postfix-expression . pseudo-destructor-name
4234      postfix-expression -> pseudo-destructor-name
4235      postfix-expression ++
4236      postfix-expression --
4237      dynamic_cast < type-id > ( expression )
4238      static_cast < type-id > ( expression )
4239      reinterpret_cast < type-id > ( expression )
4240      const_cast < type-id > ( expression )
4241      typeid ( expression )
4242      typeid ( type-id )
4243
4244    GNU Extension:
4245
4246    postfix-expression:
4247      ( type-id ) { initializer-list , [opt] }
4248
4249    This extension is a GNU version of the C99 compound-literal
4250    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4251    but they are essentially the same concept.)
4252
4253    If ADDRESS_P is true, the postfix expression is the operand of the
4254    `&' operator.  CAST_P is true if this expression is the target of a
4255    cast.
4256
4257    Returns a representation of the expression.  */
4258
4259 static tree
4260 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4261 {
4262   cp_token *token;
4263   enum rid keyword;
4264   cp_id_kind idk = CP_ID_KIND_NONE;
4265   tree postfix_expression = NULL_TREE;
4266
4267   /* Peek at the next token.  */
4268   token = cp_lexer_peek_token (parser->lexer);
4269   /* Some of the productions are determined by keywords.  */
4270   keyword = token->keyword;
4271   switch (keyword)
4272     {
4273     case RID_DYNCAST:
4274     case RID_STATCAST:
4275     case RID_REINTCAST:
4276     case RID_CONSTCAST:
4277       {
4278         tree type;
4279         tree expression;
4280         const char *saved_message;
4281
4282         /* All of these can be handled in the same way from the point
4283            of view of parsing.  Begin by consuming the token
4284            identifying the cast.  */
4285         cp_lexer_consume_token (parser->lexer);
4286
4287         /* New types cannot be defined in the cast.  */
4288         saved_message = parser->type_definition_forbidden_message;
4289         parser->type_definition_forbidden_message
4290           = "types may not be defined in casts";
4291
4292         /* Look for the opening `<'.  */
4293         cp_parser_require (parser, CPP_LESS, "`<'");
4294         /* Parse the type to which we are casting.  */
4295         type = cp_parser_type_id (parser);
4296         /* Look for the closing `>'.  */
4297         cp_parser_require (parser, CPP_GREATER, "`>'");
4298         /* Restore the old message.  */
4299         parser->type_definition_forbidden_message = saved_message;
4300
4301         /* And the expression which is being cast.  */
4302         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4303         expression = cp_parser_expression (parser, /*cast_p=*/true);
4304         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4305
4306         /* Only type conversions to integral or enumeration types
4307            can be used in constant-expressions.  */
4308         if (!cast_valid_in_integral_constant_expression_p (type)
4309             && (cp_parser_non_integral_constant_expression
4310                 (parser,
4311                  "a cast to a type other than an integral or "
4312                  "enumeration type")))
4313           return error_mark_node;
4314
4315         switch (keyword)
4316           {
4317           case RID_DYNCAST:
4318             postfix_expression
4319               = build_dynamic_cast (type, expression);
4320             break;
4321           case RID_STATCAST:
4322             postfix_expression
4323               = build_static_cast (type, expression);
4324             break;
4325           case RID_REINTCAST:
4326             postfix_expression
4327               = build_reinterpret_cast (type, expression);
4328             break;
4329           case RID_CONSTCAST:
4330             postfix_expression
4331               = build_const_cast (type, expression);
4332             break;
4333           default:
4334             gcc_unreachable ();
4335           }
4336       }
4337       break;
4338
4339     case RID_TYPEID:
4340       {
4341         tree type;
4342         const char *saved_message;
4343         bool saved_in_type_id_in_expr_p;
4344
4345         /* Consume the `typeid' token.  */
4346         cp_lexer_consume_token (parser->lexer);
4347         /* Look for the `(' token.  */
4348         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4349         /* Types cannot be defined in a `typeid' expression.  */
4350         saved_message = parser->type_definition_forbidden_message;
4351         parser->type_definition_forbidden_message
4352           = "types may not be defined in a `typeid\' expression";
4353         /* We can't be sure yet whether we're looking at a type-id or an
4354            expression.  */
4355         cp_parser_parse_tentatively (parser);
4356         /* Try a type-id first.  */
4357         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4358         parser->in_type_id_in_expr_p = true;
4359         type = cp_parser_type_id (parser);
4360         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4361         /* Look for the `)' token.  Otherwise, we can't be sure that
4362            we're not looking at an expression: consider `typeid (int
4363            (3))', for example.  */
4364         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4365         /* If all went well, simply lookup the type-id.  */
4366         if (cp_parser_parse_definitely (parser))
4367           postfix_expression = get_typeid (type);
4368         /* Otherwise, fall back to the expression variant.  */
4369         else
4370           {
4371             tree expression;
4372
4373             /* Look for an expression.  */
4374             expression = cp_parser_expression (parser, /*cast_p=*/false);
4375             /* Compute its typeid.  */
4376             postfix_expression = build_typeid (expression);
4377             /* Look for the `)' token.  */
4378             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4379           }
4380         /* Restore the saved message.  */
4381         parser->type_definition_forbidden_message = saved_message;
4382         /* `typeid' may not appear in an integral constant expression.  */
4383         if (cp_parser_non_integral_constant_expression(parser,
4384                                                        "`typeid' operator"))
4385           return error_mark_node;
4386       }
4387       break;
4388
4389     case RID_TYPENAME:
4390       {
4391         tree type;
4392         /* The syntax permitted here is the same permitted for an
4393            elaborated-type-specifier.  */
4394         type = cp_parser_elaborated_type_specifier (parser,
4395                                                     /*is_friend=*/false,
4396                                                     /*is_declaration=*/false);
4397         postfix_expression = cp_parser_functional_cast (parser, type);
4398       }
4399       break;
4400
4401     default:
4402       {
4403         tree type;
4404
4405         /* If the next thing is a simple-type-specifier, we may be
4406            looking at a functional cast.  We could also be looking at
4407            an id-expression.  So, we try the functional cast, and if
4408            that doesn't work we fall back to the primary-expression.  */
4409         cp_parser_parse_tentatively (parser);
4410         /* Look for the simple-type-specifier.  */
4411         type = cp_parser_simple_type_specifier (parser,
4412                                                 /*decl_specs=*/NULL,
4413                                                 CP_PARSER_FLAGS_NONE);
4414         /* Parse the cast itself.  */
4415         if (!cp_parser_error_occurred (parser))
4416           postfix_expression
4417             = cp_parser_functional_cast (parser, type);
4418         /* If that worked, we're done.  */
4419         if (cp_parser_parse_definitely (parser))
4420           break;
4421
4422         /* If the functional-cast didn't work out, try a
4423            compound-literal.  */
4424         if (cp_parser_allow_gnu_extensions_p (parser)
4425             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4426           {
4427             VEC(constructor_elt,gc) *initializer_list = NULL;
4428             bool saved_in_type_id_in_expr_p;
4429
4430             cp_parser_parse_tentatively (parser);
4431             /* Consume the `('.  */
4432             cp_lexer_consume_token (parser->lexer);
4433             /* Parse the type.  */
4434             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4435             parser->in_type_id_in_expr_p = true;
4436             type = cp_parser_type_id (parser);
4437             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4438             /* Look for the `)'.  */
4439             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4440             /* Look for the `{'.  */
4441             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4442             /* If things aren't going well, there's no need to
4443                keep going.  */
4444             if (!cp_parser_error_occurred (parser))
4445               {
4446                 bool non_constant_p;
4447                 /* Parse the initializer-list.  */
4448                 initializer_list
4449                   = cp_parser_initializer_list (parser, &non_constant_p);
4450                 /* Allow a trailing `,'.  */
4451                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4452                   cp_lexer_consume_token (parser->lexer);
4453                 /* Look for the final `}'.  */
4454                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4455               }
4456             /* If that worked, we're definitely looking at a
4457                compound-literal expression.  */
4458             if (cp_parser_parse_definitely (parser))
4459               {
4460                 /* Warn the user that a compound literal is not
4461                    allowed in standard C++.  */
4462                 if (pedantic)
4463                   pedwarn ("ISO C++ forbids compound-literals");
4464                 /* For simplicity, we disallow compound literals in
4465                    constant-expressions.  We could
4466                    allow compound literals of integer type, whose
4467                    initializer was a constant, in constant
4468                    expressions.  Permitting that usage, as a further
4469                    extension, would not change the meaning of any
4470                    currently accepted programs.  (Of course, as
4471                    compound literals are not part of ISO C++, the
4472                    standard has nothing to say.)  */
4473                 if (cp_parser_non_integral_constant_expression 
4474                     (parser, "non-constant compound literals"))
4475                   {
4476                     postfix_expression = error_mark_node;
4477                     break;
4478                   }
4479                 /* Form the representation of the compound-literal.  */
4480                 postfix_expression
4481                   = finish_compound_literal (type, initializer_list);
4482                 break;
4483               }
4484           }
4485
4486         /* It must be a primary-expression.  */
4487         postfix_expression
4488           = cp_parser_primary_expression (parser, address_p, cast_p,
4489                                           /*template_arg_p=*/false,
4490                                           &idk);
4491       }
4492       break;
4493     }
4494
4495   /* Keep looping until the postfix-expression is complete.  */
4496   while (true)
4497     {
4498       if (idk == CP_ID_KIND_UNQUALIFIED
4499           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4500           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4501         /* It is not a Koenig lookup function call.  */
4502         postfix_expression
4503           = unqualified_name_lookup_error (postfix_expression);
4504
4505       /* Peek at the next token.  */
4506       token = cp_lexer_peek_token (parser->lexer);
4507
4508       switch (token->type)
4509         {
4510         case CPP_OPEN_SQUARE:
4511           postfix_expression
4512             = cp_parser_postfix_open_square_expression (parser,
4513                                                         postfix_expression,
4514                                                         false);
4515           idk = CP_ID_KIND_NONE;
4516           break;
4517
4518         case CPP_OPEN_PAREN:
4519           /* postfix-expression ( expression-list [opt] ) */
4520           {
4521             bool koenig_p;
4522             bool is_builtin_constant_p;
4523             bool saved_integral_constant_expression_p = false;
4524             bool saved_non_integral_constant_expression_p = false;
4525             tree args;
4526
4527             is_builtin_constant_p
4528               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4529             if (is_builtin_constant_p)
4530               {
4531                 /* The whole point of __builtin_constant_p is to allow
4532                    non-constant expressions to appear as arguments.  */
4533                 saved_integral_constant_expression_p
4534                   = parser->integral_constant_expression_p;
4535                 saved_non_integral_constant_expression_p
4536                   = parser->non_integral_constant_expression_p;
4537                 parser->integral_constant_expression_p = false;
4538               }
4539             args = (cp_parser_parenthesized_expression_list
4540                     (parser, /*is_attribute_list=*/false,
4541                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4542                      /*non_constant_p=*/NULL));
4543             if (is_builtin_constant_p)
4544               {
4545                 parser->integral_constant_expression_p
4546                   = saved_integral_constant_expression_p;
4547                 parser->non_integral_constant_expression_p
4548                   = saved_non_integral_constant_expression_p;
4549               }
4550
4551             if (args == error_mark_node)
4552               {
4553                 postfix_expression = error_mark_node;
4554                 break;
4555               }
4556
4557             /* Function calls are not permitted in
4558                constant-expressions.  */
4559             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4560                 && cp_parser_non_integral_constant_expression (parser,
4561                                                                "a function call"))
4562               {
4563                 postfix_expression = error_mark_node;
4564                 break;
4565               }
4566
4567             koenig_p = false;
4568             if (idk == CP_ID_KIND_UNQUALIFIED)
4569               {
4570                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4571                   {
4572                     if (args)
4573                       {
4574                         koenig_p = true;
4575                         postfix_expression
4576                           = perform_koenig_lookup (postfix_expression, args);
4577                       }
4578                     else
4579                       postfix_expression
4580                         = unqualified_fn_lookup_error (postfix_expression);
4581                   }
4582                 /* We do not perform argument-dependent lookup if
4583                    normal lookup finds a non-function, in accordance
4584                    with the expected resolution of DR 218.  */
4585                 else if (args && is_overloaded_fn (postfix_expression))
4586                   {
4587                     tree fn = get_first_fn (postfix_expression);
4588
4589                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4590                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4591
4592                     /* Only do argument dependent lookup if regular
4593                        lookup does not find a set of member functions.
4594                        [basic.lookup.koenig]/2a  */
4595                     if (!DECL_FUNCTION_MEMBER_P (fn))
4596                       {
4597                         koenig_p = true;
4598                         postfix_expression
4599                           = perform_koenig_lookup (postfix_expression, args);
4600                       }
4601                   }
4602               }
4603
4604             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4605               {
4606                 tree instance = TREE_OPERAND (postfix_expression, 0);
4607                 tree fn = TREE_OPERAND (postfix_expression, 1);
4608
4609                 if (processing_template_decl
4610                     && (type_dependent_expression_p (instance)
4611                         || (!BASELINK_P (fn)
4612                             && TREE_CODE (fn) != FIELD_DECL)
4613                         || type_dependent_expression_p (fn)
4614                         || any_type_dependent_arguments_p (args)))
4615                   {
4616                     postfix_expression
4617                       = build_nt_call_list (postfix_expression, args);
4618                     break;
4619                   }
4620
4621                 if (BASELINK_P (fn))
4622                   postfix_expression
4623                     = (build_new_method_call
4624                        (instance, fn, args, NULL_TREE,
4625                         (idk == CP_ID_KIND_QUALIFIED
4626                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4627                         /*fn_p=*/NULL));
4628                 else
4629                   postfix_expression
4630                     = finish_call_expr (postfix_expression, args,
4631                                         /*disallow_virtual=*/false,
4632                                         /*koenig_p=*/false);
4633               }
4634             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4635                      || TREE_CODE (postfix_expression) == MEMBER_REF
4636                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4637               postfix_expression = (build_offset_ref_call_from_tree
4638                                     (postfix_expression, args));
4639             else if (idk == CP_ID_KIND_QUALIFIED)
4640               /* A call to a static class member, or a namespace-scope
4641                  function.  */
4642               postfix_expression
4643                 = finish_call_expr (postfix_expression, args,
4644                                     /*disallow_virtual=*/true,
4645                                     koenig_p);
4646             else
4647               /* All other function calls.  */
4648               postfix_expression
4649                 = finish_call_expr (postfix_expression, args,
4650                                     /*disallow_virtual=*/false,
4651                                     koenig_p);
4652
4653             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4654             idk = CP_ID_KIND_NONE;
4655           }
4656           break;
4657
4658         case CPP_DOT:
4659         case CPP_DEREF:
4660           /* postfix-expression . template [opt] id-expression
4661              postfix-expression . pseudo-destructor-name
4662              postfix-expression -> template [opt] id-expression
4663              postfix-expression -> pseudo-destructor-name */
4664
4665           /* Consume the `.' or `->' operator.  */
4666           cp_lexer_consume_token (parser->lexer);
4667
4668           postfix_expression
4669             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4670                                                       postfix_expression,
4671                                                       false, &idk);
4672           break;
4673
4674         case CPP_PLUS_PLUS:
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                                      POSTINCREMENT_EXPR);
4682           /* Increments may not appear in constant-expressions.  */
4683           if (cp_parser_non_integral_constant_expression (parser,
4684                                                           "an increment"))
4685             postfix_expression = error_mark_node;
4686           idk = CP_ID_KIND_NONE;
4687           break;
4688
4689         case CPP_MINUS_MINUS:
4690           /* postfix-expression -- */
4691           /* Consume the `--' token.  */
4692           cp_lexer_consume_token (parser->lexer);
4693           /* Generate a representation for the complete expression.  */
4694           postfix_expression
4695             = finish_increment_expr (postfix_expression,
4696                                      POSTDECREMENT_EXPR);
4697           /* Decrements may not appear in constant-expressions.  */
4698           if (cp_parser_non_integral_constant_expression (parser,
4699                                                           "a decrement"))
4700             postfix_expression = error_mark_node;
4701           idk = CP_ID_KIND_NONE;
4702           break;
4703
4704         default:
4705           return postfix_expression;
4706         }
4707     }
4708
4709   /* We should never get here.  */
4710   gcc_unreachable ();
4711   return error_mark_node;
4712 }
4713
4714 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4715    by cp_parser_builtin_offsetof.  We're looking for
4716
4717      postfix-expression [ expression ]
4718
4719    FOR_OFFSETOF is set if we're being called in that context, which
4720    changes how we deal with integer constant expressions.  */
4721
4722 static tree
4723 cp_parser_postfix_open_square_expression (cp_parser *parser,
4724                                           tree postfix_expression,
4725                                           bool for_offsetof)
4726 {
4727   tree index;
4728
4729   /* Consume the `[' token.  */
4730   cp_lexer_consume_token (parser->lexer);
4731
4732   /* Parse the index expression.  */
4733   /* ??? For offsetof, there is a question of what to allow here.  If
4734      offsetof is not being used in an integral constant expression context,
4735      then we *could* get the right answer by computing the value at runtime.
4736      If we are in an integral constant expression context, then we might
4737      could accept any constant expression; hard to say without analysis.
4738      Rather than open the barn door too wide right away, allow only integer
4739      constant expressions here.  */
4740   if (for_offsetof)
4741     index = cp_parser_constant_expression (parser, false, NULL);
4742   else
4743     index = cp_parser_expression (parser, /*cast_p=*/false);
4744
4745   /* Look for the closing `]'.  */
4746   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4747
4748   /* Build the ARRAY_REF.  */
4749   postfix_expression = grok_array_decl (postfix_expression, index);
4750
4751   /* When not doing offsetof, array references are not permitted in
4752      constant-expressions.  */
4753   if (!for_offsetof
4754       && (cp_parser_non_integral_constant_expression
4755           (parser, "an array reference")))
4756     postfix_expression = error_mark_node;
4757
4758   return postfix_expression;
4759 }
4760
4761 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4762    by cp_parser_builtin_offsetof.  We're looking for
4763
4764      postfix-expression . template [opt] id-expression
4765      postfix-expression . pseudo-destructor-name
4766      postfix-expression -> template [opt] id-expression
4767      postfix-expression -> pseudo-destructor-name
4768
4769    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4770    limits what of the above we'll actually accept, but nevermind.
4771    TOKEN_TYPE is the "." or "->" token, which will already have been
4772    removed from the stream.  */
4773
4774 static tree
4775 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4776                                         enum cpp_ttype token_type,
4777                                         tree postfix_expression,
4778                                         bool for_offsetof, cp_id_kind *idk)
4779 {
4780   tree name;
4781   bool dependent_p;
4782   bool pseudo_destructor_p;
4783   tree scope = NULL_TREE;
4784
4785   /* If this is a `->' operator, dereference the pointer.  */
4786   if (token_type == CPP_DEREF)
4787     postfix_expression = build_x_arrow (postfix_expression);
4788   /* Check to see whether or not the expression is type-dependent.  */
4789   dependent_p = type_dependent_expression_p (postfix_expression);
4790   /* The identifier following the `->' or `.' is not qualified.  */
4791   parser->scope = NULL_TREE;
4792   parser->qualifying_scope = NULL_TREE;
4793   parser->object_scope = NULL_TREE;
4794   *idk = CP_ID_KIND_NONE;
4795   /* Enter the scope corresponding to the type of the object
4796      given by the POSTFIX_EXPRESSION.  */
4797   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4798     {
4799       scope = TREE_TYPE (postfix_expression);
4800       /* According to the standard, no expression should ever have
4801          reference type.  Unfortunately, we do not currently match
4802          the standard in this respect in that our internal representation
4803          of an expression may have reference type even when the standard
4804          says it does not.  Therefore, we have to manually obtain the
4805          underlying type here.  */
4806       scope = non_reference (scope);
4807       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4808       if (scope == unknown_type_node)
4809         {
4810           error ("%qE does not have class type", postfix_expression);
4811           scope = NULL_TREE;
4812         }
4813       else
4814         scope = complete_type_or_else (scope, NULL_TREE);
4815       /* Let the name lookup machinery know that we are processing a
4816          class member access expression.  */
4817       parser->context->object_type = scope;
4818       /* If something went wrong, we want to be able to discern that case,
4819          as opposed to the case where there was no SCOPE due to the type
4820          of expression being dependent.  */
4821       if (!scope)
4822         scope = error_mark_node;
4823       /* If the SCOPE was erroneous, make the various semantic analysis
4824          functions exit quickly -- and without issuing additional error
4825          messages.  */
4826       if (scope == error_mark_node)
4827         postfix_expression = error_mark_node;
4828     }
4829
4830   /* Assume this expression is not a pseudo-destructor access.  */
4831   pseudo_destructor_p = false;
4832
4833   /* If the SCOPE is a scalar type, then, if this is a valid program,
4834      we must be looking at a pseudo-destructor-name.  */
4835   if (scope && SCALAR_TYPE_P (scope))
4836     {
4837       tree s;
4838       tree type;
4839
4840       cp_parser_parse_tentatively (parser);
4841       /* Parse the pseudo-destructor-name.  */
4842       s = NULL_TREE;
4843       cp_parser_pseudo_destructor_name (parser, &s, &type);
4844       if (cp_parser_parse_definitely (parser))
4845         {
4846           pseudo_destructor_p = true;
4847           postfix_expression
4848             = finish_pseudo_destructor_expr (postfix_expression,
4849                                              s, TREE_TYPE (type));
4850         }
4851     }
4852
4853   if (!pseudo_destructor_p)
4854     {
4855       /* If the SCOPE is not a scalar type, we are looking at an
4856          ordinary class member access expression, rather than a
4857          pseudo-destructor-name.  */
4858       bool template_p;
4859       /* Parse the id-expression.  */
4860       name = (cp_parser_id_expression
4861               (parser,
4862                cp_parser_optional_template_keyword (parser),
4863                /*check_dependency_p=*/true,
4864                &template_p,
4865                /*declarator_p=*/false,
4866                /*optional_p=*/false));
4867       /* In general, build a SCOPE_REF if the member name is qualified.
4868          However, if the name was not dependent and has already been
4869          resolved; there is no need to build the SCOPE_REF.  For example;
4870
4871              struct X { void f(); };
4872              template <typename T> void f(T* t) { t->X::f(); }
4873
4874          Even though "t" is dependent, "X::f" is not and has been resolved
4875          to a BASELINK; there is no need to include scope information.  */
4876
4877       /* But we do need to remember that there was an explicit scope for
4878          virtual function calls.  */
4879       if (parser->scope)
4880         *idk = CP_ID_KIND_QUALIFIED;
4881
4882       /* If the name is a template-id that names a type, we will get a
4883          TYPE_DECL here.  That is invalid code.  */
4884       if (TREE_CODE (name) == TYPE_DECL)
4885         {
4886           error ("invalid use of %qD", name);
4887           postfix_expression = error_mark_node;
4888         }
4889       else
4890         {
4891           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4892             {
4893               name = build_qualified_name (/*type=*/NULL_TREE,
4894                                            parser->scope,
4895                                            name,
4896                                            template_p);
4897               parser->scope = NULL_TREE;
4898               parser->qualifying_scope = NULL_TREE;
4899               parser->object_scope = NULL_TREE;
4900             }
4901           if (scope && name && BASELINK_P (name))
4902             adjust_result_of_qualified_name_lookup
4903               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4904           postfix_expression
4905             = finish_class_member_access_expr (postfix_expression, name,
4906                                                template_p);
4907         }
4908     }
4909
4910   /* We no longer need to look up names in the scope of the object on
4911      the left-hand side of the `.' or `->' operator.  */
4912   parser->context->object_type = NULL_TREE;
4913
4914   /* Outside of offsetof, these operators may not appear in
4915      constant-expressions.  */
4916   if (!for_offsetof
4917       && (cp_parser_non_integral_constant_expression
4918           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4919     postfix_expression = error_mark_node;
4920
4921   return postfix_expression;
4922 }
4923
4924 /* Parse a parenthesized expression-list.
4925
4926    expression-list:
4927      assignment-expression
4928      expression-list, assignment-expression
4929
4930    attribute-list:
4931      expression-list
4932      identifier
4933      identifier, expression-list
4934
4935    CAST_P is true if this expression is the target of a cast.
4936
4937    ALLOW_EXPANSION_P is true if this expression allows expansion of an
4938    argument pack.
4939
4940    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4941    representation of an assignment-expression.  Note that a TREE_LIST
4942    is returned even if there is only a single expression in the list.
4943    error_mark_node is returned if the ( and or ) are
4944    missing. NULL_TREE is returned on no expressions. The parentheses
4945    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4946    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4947    indicates whether or not all of the expressions in the list were
4948    constant.  */
4949
4950 static tree
4951 cp_parser_parenthesized_expression_list (cp_parser* parser,
4952                                          bool is_attribute_list,
4953                                          bool cast_p,
4954                                          bool allow_expansion_p,
4955                                          bool *non_constant_p)
4956 {
4957   tree expression_list = NULL_TREE;
4958   bool fold_expr_p = is_attribute_list;
4959   tree identifier = NULL_TREE;
4960
4961   /* Assume all the expressions will be constant.  */
4962   if (non_constant_p)
4963     *non_constant_p = false;
4964
4965   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4966     return error_mark_node;
4967
4968   /* Consume expressions until there are no more.  */
4969   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4970     while (true)
4971       {
4972         tree expr;
4973
4974         /* At the beginning of attribute lists, check to see if the
4975            next token is an identifier.  */
4976         if (is_attribute_list
4977             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4978           {
4979             cp_token *token;
4980
4981             /* Consume the identifier.  */
4982             token = cp_lexer_consume_token (parser->lexer);
4983             /* Save the identifier.  */
4984             identifier = token->u.value;
4985           }
4986         else
4987           {
4988             /* Parse the next assignment-expression.  */
4989             if (non_constant_p)
4990               {
4991                 bool expr_non_constant_p;
4992                 expr = (cp_parser_constant_expression
4993                         (parser, /*allow_non_constant_p=*/true,
4994                          &expr_non_constant_p));
4995                 if (expr_non_constant_p)
4996                   *non_constant_p = true;
4997               }
4998             else
4999               expr = cp_parser_assignment_expression (parser, cast_p);
5000
5001             if (fold_expr_p)
5002               expr = fold_non_dependent_expr (expr);
5003
5004             /* If we have an ellipsis, then this is an expression
5005                expansion.  */
5006             if (allow_expansion_p
5007                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5008               {
5009                 /* Consume the `...'.  */
5010                 cp_lexer_consume_token (parser->lexer);
5011
5012                 /* Build the argument pack.  */
5013                 expr = make_pack_expansion (expr);
5014               }
5015
5016              /* Add it to the list.  We add error_mark_node
5017                 expressions to the list, so that we can still tell if
5018                 the correct form for a parenthesized expression-list
5019                 is found. That gives better errors.  */
5020             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5021
5022             if (expr == error_mark_node)
5023               goto skip_comma;
5024           }
5025
5026         /* After the first item, attribute lists look the same as
5027            expression lists.  */
5028         is_attribute_list = false;
5029
5030       get_comma:;
5031         /* If the next token isn't a `,', then we are done.  */
5032         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5033           break;
5034
5035         /* Otherwise, consume the `,' and keep going.  */
5036         cp_lexer_consume_token (parser->lexer);
5037       }
5038
5039   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5040     {
5041       int ending;
5042
5043     skip_comma:;
5044       /* We try and resync to an unnested comma, as that will give the
5045          user better diagnostics.  */
5046       ending = cp_parser_skip_to_closing_parenthesis (parser,
5047                                                       /*recovering=*/true,
5048                                                       /*or_comma=*/true,
5049                                                       /*consume_paren=*/true);
5050       if (ending < 0)
5051         goto get_comma;
5052       if (!ending)
5053         return error_mark_node;
5054     }
5055
5056   /* We built up the list in reverse order so we must reverse it now.  */
5057   expression_list = nreverse (expression_list);
5058   if (identifier)
5059     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5060
5061   return expression_list;
5062 }
5063
5064 /* Parse a pseudo-destructor-name.
5065
5066    pseudo-destructor-name:
5067      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5068      :: [opt] nested-name-specifier template template-id :: ~ type-name
5069      :: [opt] nested-name-specifier [opt] ~ type-name
5070
5071    If either of the first two productions is used, sets *SCOPE to the
5072    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5073    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5074    or ERROR_MARK_NODE if the parse fails.  */
5075
5076 static void
5077 cp_parser_pseudo_destructor_name (cp_parser* parser,
5078                                   tree* scope,
5079                                   tree* type)
5080 {
5081   bool nested_name_specifier_p;
5082
5083   /* Assume that things will not work out.  */
5084   *type = error_mark_node;
5085
5086   /* Look for the optional `::' operator.  */
5087   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5088   /* Look for the optional nested-name-specifier.  */
5089   nested_name_specifier_p
5090     = (cp_parser_nested_name_specifier_opt (parser,
5091                                             /*typename_keyword_p=*/false,
5092                                             /*check_dependency_p=*/true,
5093                                             /*type_p=*/false,
5094                                             /*is_declaration=*/true)
5095        != NULL_TREE);
5096   /* Now, if we saw a nested-name-specifier, we might be doing the
5097      second production.  */
5098   if (nested_name_specifier_p
5099       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5100     {
5101       /* Consume the `template' keyword.  */
5102       cp_lexer_consume_token (parser->lexer);
5103       /* Parse the template-id.  */
5104       cp_parser_template_id (parser,
5105                              /*template_keyword_p=*/true,
5106                              /*check_dependency_p=*/false,
5107                              /*is_declaration=*/true);
5108       /* Look for the `::' token.  */
5109       cp_parser_require (parser, CPP_SCOPE, "`::'");
5110     }
5111   /* If the next token is not a `~', then there might be some
5112      additional qualification.  */
5113   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5114     {
5115       /* Look for the type-name.  */
5116       *scope = TREE_TYPE (cp_parser_type_name (parser));
5117
5118       if (*scope == error_mark_node)
5119         return;
5120
5121       /* If we don't have ::~, then something has gone wrong.  Since
5122          the only caller of this function is looking for something
5123          after `.' or `->' after a scalar type, most likely the
5124          program is trying to get a member of a non-aggregate
5125          type.  */
5126       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5127           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5128         {
5129           cp_parser_error (parser, "request for member of non-aggregate type");
5130           return;
5131         }
5132
5133       /* Look for the `::' token.  */
5134       cp_parser_require (parser, CPP_SCOPE, "`::'");
5135     }
5136   else
5137     *scope = NULL_TREE;
5138
5139   /* Look for the `~'.  */
5140   cp_parser_require (parser, CPP_COMPL, "`~'");
5141   /* Look for the type-name again.  We are not responsible for
5142      checking that it matches the first type-name.  */
5143   *type = cp_parser_type_name (parser);
5144 }
5145
5146 /* Parse a unary-expression.
5147
5148    unary-expression:
5149      postfix-expression
5150      ++ cast-expression
5151      -- cast-expression
5152      unary-operator cast-expression
5153      sizeof unary-expression
5154      sizeof ( type-id )
5155      new-expression
5156      delete-expression
5157
5158    GNU Extensions:
5159
5160    unary-expression:
5161      __extension__ cast-expression
5162      __alignof__ unary-expression
5163      __alignof__ ( type-id )
5164      __real__ cast-expression
5165      __imag__ cast-expression
5166      && identifier
5167
5168    ADDRESS_P is true iff the unary-expression is appearing as the
5169    operand of the `&' operator.   CAST_P is true if this expression is
5170    the target of a cast.
5171
5172    Returns a representation of the expression.  */
5173
5174 static tree
5175 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5176 {
5177   cp_token *token;
5178   enum tree_code unary_operator;
5179
5180   /* Peek at the next token.  */
5181   token = cp_lexer_peek_token (parser->lexer);
5182   /* Some keywords give away the kind of expression.  */
5183   if (token->type == CPP_KEYWORD)
5184     {
5185       enum rid keyword = token->keyword;
5186
5187       switch (keyword)
5188         {
5189         case RID_ALIGNOF:
5190         case RID_SIZEOF:
5191           {
5192             tree operand;
5193             enum tree_code op;
5194
5195             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5196             /* Consume the token.  */
5197             cp_lexer_consume_token (parser->lexer);
5198             /* Parse the operand.  */
5199             operand = cp_parser_sizeof_operand (parser, keyword);
5200
5201             if (TYPE_P (operand))
5202               return cxx_sizeof_or_alignof_type (operand, op, true);
5203             else
5204               return cxx_sizeof_or_alignof_expr (operand, op);
5205           }
5206
5207         case RID_NEW:
5208           return cp_parser_new_expression (parser);
5209
5210         case RID_DELETE:
5211           return cp_parser_delete_expression (parser);
5212
5213         case RID_EXTENSION:
5214           {
5215             /* The saved value of the PEDANTIC flag.  */
5216             int saved_pedantic;
5217             tree expr;
5218
5219             /* Save away the PEDANTIC flag.  */
5220             cp_parser_extension_opt (parser, &saved_pedantic);
5221             /* Parse the cast-expression.  */
5222             expr = cp_parser_simple_cast_expression (parser);
5223             /* Restore the PEDANTIC flag.  */
5224             pedantic = saved_pedantic;
5225
5226             return expr;
5227           }
5228
5229         case RID_REALPART:
5230         case RID_IMAGPART:
5231           {
5232             tree expression;
5233
5234             /* Consume the `__real__' or `__imag__' token.  */
5235             cp_lexer_consume_token (parser->lexer);
5236             /* Parse the cast-expression.  */
5237             expression = cp_parser_simple_cast_expression (parser);
5238             /* Create the complete representation.  */
5239             return build_x_unary_op ((keyword == RID_REALPART
5240                                       ? REALPART_EXPR : IMAGPART_EXPR),
5241                                      expression);
5242           }
5243           break;
5244
5245         default:
5246           break;
5247         }
5248     }
5249
5250   /* Look for the `:: new' and `:: delete', which also signal the
5251      beginning of a new-expression, or delete-expression,
5252      respectively.  If the next token is `::', then it might be one of
5253      these.  */
5254   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5255     {
5256       enum rid keyword;
5257
5258       /* See if the token after the `::' is one of the keywords in
5259          which we're interested.  */
5260       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5261       /* If it's `new', we have a new-expression.  */
5262       if (keyword == RID_NEW)
5263         return cp_parser_new_expression (parser);
5264       /* Similarly, for `delete'.  */
5265       else if (keyword == RID_DELETE)
5266         return cp_parser_delete_expression (parser);
5267     }
5268
5269   /* Look for a unary operator.  */
5270   unary_operator = cp_parser_unary_operator (token);
5271   /* The `++' and `--' operators can be handled similarly, even though
5272      they are not technically unary-operators in the grammar.  */
5273   if (unary_operator == ERROR_MARK)
5274     {
5275       if (token->type == CPP_PLUS_PLUS)
5276         unary_operator = PREINCREMENT_EXPR;
5277       else if (token->type == CPP_MINUS_MINUS)
5278         unary_operator = PREDECREMENT_EXPR;
5279       /* Handle the GNU address-of-label extension.  */
5280       else if (cp_parser_allow_gnu_extensions_p (parser)
5281                && token->type == CPP_AND_AND)
5282         {
5283           tree identifier;
5284
5285           /* Consume the '&&' token.  */
5286           cp_lexer_consume_token (parser->lexer);
5287           /* Look for the identifier.  */
5288           identifier = cp_parser_identifier (parser);
5289           /* Create an expression representing the address.  */
5290           return finish_label_address_expr (identifier);
5291         }
5292     }
5293   if (unary_operator != ERROR_MARK)
5294     {
5295       tree cast_expression;
5296       tree expression = error_mark_node;
5297       const char *non_constant_p = NULL;
5298
5299       /* Consume the operator token.  */
5300       token = cp_lexer_consume_token (parser->lexer);
5301       /* Parse the cast-expression.  */
5302       cast_expression
5303         = cp_parser_cast_expression (parser,
5304                                      unary_operator == ADDR_EXPR,
5305                                      /*cast_p=*/false);
5306       /* Now, build an appropriate representation.  */
5307       switch (unary_operator)
5308         {
5309         case INDIRECT_REF:
5310           non_constant_p = "`*'";
5311           expression = build_x_indirect_ref (cast_expression, "unary *");
5312           break;
5313
5314         case ADDR_EXPR:
5315           non_constant_p = "`&'";
5316           /* Fall through.  */
5317         case BIT_NOT_EXPR:
5318           expression = build_x_unary_op (unary_operator, cast_expression);
5319           break;
5320
5321         case PREINCREMENT_EXPR:
5322         case PREDECREMENT_EXPR:
5323           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5324                             ? "`++'" : "`--'");
5325           /* Fall through.  */
5326         case UNARY_PLUS_EXPR:
5327         case NEGATE_EXPR:
5328         case TRUTH_NOT_EXPR:
5329           expression = finish_unary_op_expr (unary_operator, cast_expression);
5330           break;
5331
5332         default:
5333           gcc_unreachable ();
5334         }
5335
5336       if (non_constant_p
5337           && cp_parser_non_integral_constant_expression (parser,
5338                                                          non_constant_p))
5339         expression = error_mark_node;
5340
5341       return expression;
5342     }
5343
5344   return cp_parser_postfix_expression (parser, address_p, cast_p);
5345 }
5346
5347 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5348    unary-operator, the corresponding tree code is returned.  */
5349
5350 static enum tree_code
5351 cp_parser_unary_operator (cp_token* token)
5352 {
5353   switch (token->type)
5354     {
5355     case CPP_MULT:
5356       return INDIRECT_REF;
5357
5358     case CPP_AND:
5359       return ADDR_EXPR;
5360
5361     case CPP_PLUS:
5362       return UNARY_PLUS_EXPR;
5363
5364     case CPP_MINUS:
5365       return NEGATE_EXPR;
5366
5367     case CPP_NOT:
5368       return TRUTH_NOT_EXPR;
5369
5370     case CPP_COMPL:
5371       return BIT_NOT_EXPR;
5372
5373     default:
5374       return ERROR_MARK;
5375     }
5376 }
5377
5378 /* Parse a new-expression.
5379
5380    new-expression:
5381      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5382      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5383
5384    Returns a representation of the expression.  */
5385
5386 static tree
5387 cp_parser_new_expression (cp_parser* parser)
5388 {
5389   bool global_scope_p;
5390   tree placement;
5391   tree type;
5392   tree initializer;
5393   tree nelts;
5394
5395   /* Look for the optional `::' operator.  */
5396   global_scope_p
5397     = (cp_parser_global_scope_opt (parser,
5398                                    /*current_scope_valid_p=*/false)
5399        != NULL_TREE);
5400   /* Look for the `new' operator.  */
5401   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5402   /* There's no easy way to tell a new-placement from the
5403      `( type-id )' construct.  */
5404   cp_parser_parse_tentatively (parser);
5405   /* Look for a new-placement.  */
5406   placement = cp_parser_new_placement (parser);
5407   /* If that didn't work out, there's no new-placement.  */
5408   if (!cp_parser_parse_definitely (parser))
5409     placement = NULL_TREE;
5410
5411   /* If the next token is a `(', then we have a parenthesized
5412      type-id.  */
5413   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5414     {
5415       /* Consume the `('.  */
5416       cp_lexer_consume_token (parser->lexer);
5417       /* Parse the type-id.  */
5418       type = cp_parser_type_id (parser);
5419       /* Look for the closing `)'.  */
5420       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5421       /* There should not be a direct-new-declarator in this production,
5422          but GCC used to allowed this, so we check and emit a sensible error
5423          message for this case.  */
5424       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5425         {
5426           error ("array bound forbidden after parenthesized type-id");
5427           inform ("try removing the parentheses around the type-id");
5428           cp_parser_direct_new_declarator (parser);
5429         }
5430       nelts = NULL_TREE;
5431     }
5432   /* Otherwise, there must be a new-type-id.  */
5433   else
5434     type = cp_parser_new_type_id (parser, &nelts);
5435
5436   /* If the next token is a `(', then we have a new-initializer.  */
5437   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5438     initializer = cp_parser_new_initializer (parser);
5439   else
5440     initializer = NULL_TREE;
5441
5442   /* A new-expression may not appear in an integral constant
5443      expression.  */
5444   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5445     return error_mark_node;
5446
5447   /* Create a representation of the new-expression.  */
5448   return build_new (placement, type, nelts, initializer, global_scope_p);
5449 }
5450
5451 /* Parse a new-placement.
5452
5453    new-placement:
5454      ( expression-list )
5455
5456    Returns the same representation as for an expression-list.  */
5457
5458 static tree
5459 cp_parser_new_placement (cp_parser* parser)
5460 {
5461   tree expression_list;
5462
5463   /* Parse the expression-list.  */
5464   expression_list = (cp_parser_parenthesized_expression_list
5465                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5466                       /*non_constant_p=*/NULL));
5467
5468   return expression_list;
5469 }
5470
5471 /* Parse a new-type-id.
5472
5473    new-type-id:
5474      type-specifier-seq new-declarator [opt]
5475
5476    Returns the TYPE allocated.  If the new-type-id indicates an array
5477    type, *NELTS is set to the number of elements in the last array
5478    bound; the TYPE will not include the last array bound.  */
5479
5480 static tree
5481 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5482 {
5483   cp_decl_specifier_seq type_specifier_seq;
5484   cp_declarator *new_declarator;
5485   cp_declarator *declarator;
5486   cp_declarator *outer_declarator;
5487   const char *saved_message;
5488   tree type;
5489
5490   /* The type-specifier sequence must not contain type definitions.
5491      (It cannot contain declarations of new types either, but if they
5492      are not definitions we will catch that because they are not
5493      complete.)  */
5494   saved_message = parser->type_definition_forbidden_message;
5495   parser->type_definition_forbidden_message
5496     = "types may not be defined in a new-type-id";
5497   /* Parse the type-specifier-seq.  */
5498   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5499                                 &type_specifier_seq);
5500   /* Restore the old message.  */
5501   parser->type_definition_forbidden_message = saved_message;
5502   /* Parse the new-declarator.  */
5503   new_declarator = cp_parser_new_declarator_opt (parser);
5504
5505   /* Determine the number of elements in the last array dimension, if
5506      any.  */
5507   *nelts = NULL_TREE;
5508   /* Skip down to the last array dimension.  */
5509   declarator = new_declarator;
5510   outer_declarator = NULL;
5511   while (declarator && (declarator->kind == cdk_pointer
5512                         || declarator->kind == cdk_ptrmem))
5513     {
5514       outer_declarator = declarator;
5515       declarator = declarator->declarator;
5516     }
5517   while (declarator
5518          && declarator->kind == cdk_array
5519          && declarator->declarator
5520          && declarator->declarator->kind == cdk_array)
5521     {
5522       outer_declarator = declarator;
5523       declarator = declarator->declarator;
5524     }
5525
5526   if (declarator && declarator->kind == cdk_array)
5527     {
5528       *nelts = declarator->u.array.bounds;
5529       if (*nelts == error_mark_node)
5530         *nelts = integer_one_node;
5531
5532       if (outer_declarator)
5533         outer_declarator->declarator = declarator->declarator;
5534       else
5535         new_declarator = NULL;
5536     }
5537
5538   type = groktypename (&type_specifier_seq, new_declarator);
5539   return type;
5540 }
5541
5542 /* Parse an (optional) new-declarator.
5543
5544    new-declarator:
5545      ptr-operator new-declarator [opt]
5546      direct-new-declarator
5547
5548    Returns the declarator.  */
5549
5550 static cp_declarator *
5551 cp_parser_new_declarator_opt (cp_parser* parser)
5552 {
5553   enum tree_code code;
5554   tree type;
5555   cp_cv_quals cv_quals;
5556
5557   /* We don't know if there's a ptr-operator next, or not.  */
5558   cp_parser_parse_tentatively (parser);
5559   /* Look for a ptr-operator.  */
5560   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5561   /* If that worked, look for more new-declarators.  */
5562   if (cp_parser_parse_definitely (parser))
5563     {
5564       cp_declarator *declarator;
5565
5566       /* Parse another optional declarator.  */
5567       declarator = cp_parser_new_declarator_opt (parser);
5568
5569       return cp_parser_make_indirect_declarator
5570         (code, type, cv_quals, declarator);
5571     }
5572
5573   /* If the next token is a `[', there is a direct-new-declarator.  */
5574   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5575     return cp_parser_direct_new_declarator (parser);
5576
5577   return NULL;
5578 }
5579
5580 /* Parse a direct-new-declarator.
5581
5582    direct-new-declarator:
5583      [ expression ]
5584      direct-new-declarator [constant-expression]
5585
5586    */
5587
5588 static cp_declarator *
5589 cp_parser_direct_new_declarator (cp_parser* parser)
5590 {
5591   cp_declarator *declarator = NULL;
5592
5593   while (true)
5594     {
5595       tree expression;
5596
5597       /* Look for the opening `['.  */
5598       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5599       /* The first expression is not required to be constant.  */
5600       if (!declarator)
5601         {
5602           expression = cp_parser_expression (parser, /*cast_p=*/false);
5603           /* The standard requires that the expression have integral
5604              type.  DR 74 adds enumeration types.  We believe that the
5605              real intent is that these expressions be handled like the
5606              expression in a `switch' condition, which also allows
5607              classes with a single conversion to integral or
5608              enumeration type.  */
5609           if (!processing_template_decl)
5610             {
5611               expression
5612                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5613                                               expression,
5614                                               /*complain=*/true);
5615               if (!expression)
5616                 {
5617                   error ("expression in new-declarator must have integral "
5618                          "or enumeration type");
5619                   expression = error_mark_node;
5620                 }
5621             }
5622         }
5623       /* But all the other expressions must be.  */
5624       else
5625         expression
5626           = cp_parser_constant_expression (parser,
5627                                            /*allow_non_constant=*/false,
5628                                            NULL);
5629       /* Look for the closing `]'.  */
5630       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5631
5632       /* Add this bound to the declarator.  */
5633       declarator = make_array_declarator (declarator, expression);
5634
5635       /* If the next token is not a `[', then there are no more
5636          bounds.  */
5637       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5638         break;
5639     }
5640
5641   return declarator;
5642 }
5643
5644 /* Parse a new-initializer.
5645
5646    new-initializer:
5647      ( expression-list [opt] )
5648
5649    Returns a representation of the expression-list.  If there is no
5650    expression-list, VOID_ZERO_NODE is returned.  */
5651
5652 static tree
5653 cp_parser_new_initializer (cp_parser* parser)
5654 {
5655   tree expression_list;
5656
5657   expression_list = (cp_parser_parenthesized_expression_list
5658                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5659                       /*non_constant_p=*/NULL));
5660   if (!expression_list)
5661     expression_list = void_zero_node;
5662
5663   return expression_list;
5664 }
5665
5666 /* Parse a delete-expression.
5667
5668    delete-expression:
5669      :: [opt] delete cast-expression
5670      :: [opt] delete [ ] cast-expression
5671
5672    Returns a representation of the expression.  */
5673
5674 static tree
5675 cp_parser_delete_expression (cp_parser* parser)
5676 {
5677   bool global_scope_p;
5678   bool array_p;
5679   tree expression;
5680
5681   /* Look for the optional `::' operator.  */
5682   global_scope_p
5683     = (cp_parser_global_scope_opt (parser,
5684                                    /*current_scope_valid_p=*/false)
5685        != NULL_TREE);
5686   /* Look for the `delete' keyword.  */
5687   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5688   /* See if the array syntax is in use.  */
5689   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5690     {
5691       /* Consume the `[' token.  */
5692       cp_lexer_consume_token (parser->lexer);
5693       /* Look for the `]' token.  */
5694       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5695       /* Remember that this is the `[]' construct.  */
5696       array_p = true;
5697     }
5698   else
5699     array_p = false;
5700
5701   /* Parse the cast-expression.  */
5702   expression = cp_parser_simple_cast_expression (parser);
5703
5704   /* A delete-expression may not appear in an integral constant
5705      expression.  */
5706   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5707     return error_mark_node;
5708
5709   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5710 }
5711
5712 /* Parse a cast-expression.
5713
5714    cast-expression:
5715      unary-expression
5716      ( type-id ) cast-expression
5717
5718    ADDRESS_P is true iff the unary-expression is appearing as the
5719    operand of the `&' operator.   CAST_P is true if this expression is
5720    the target of a cast.
5721
5722    Returns a representation of the expression.  */
5723
5724 static tree
5725 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5726 {
5727   /* If it's a `(', then we might be looking at a cast.  */
5728   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5729     {
5730       tree type = NULL_TREE;
5731       tree expr = NULL_TREE;
5732       bool compound_literal_p;
5733       const char *saved_message;
5734
5735       /* There's no way to know yet whether or not this is a cast.
5736          For example, `(int (3))' is a unary-expression, while `(int)
5737          3' is a cast.  So, we resort to parsing tentatively.  */
5738       cp_parser_parse_tentatively (parser);
5739       /* Types may not be defined in a cast.  */
5740       saved_message = parser->type_definition_forbidden_message;
5741       parser->type_definition_forbidden_message
5742         = "types may not be defined in casts";
5743       /* Consume the `('.  */
5744       cp_lexer_consume_token (parser->lexer);
5745       /* A very tricky bit is that `(struct S) { 3 }' is a
5746          compound-literal (which we permit in C++ as an extension).
5747          But, that construct is not a cast-expression -- it is a
5748          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5749          is legal; if the compound-literal were a cast-expression,
5750          you'd need an extra set of parentheses.)  But, if we parse
5751          the type-id, and it happens to be a class-specifier, then we
5752          will commit to the parse at that point, because we cannot
5753          undo the action that is done when creating a new class.  So,
5754          then we cannot back up and do a postfix-expression.
5755
5756          Therefore, we scan ahead to the closing `)', and check to see
5757          if the token after the `)' is a `{'.  If so, we are not
5758          looking at a cast-expression.
5759
5760          Save tokens so that we can put them back.  */
5761       cp_lexer_save_tokens (parser->lexer);
5762       /* Skip tokens until the next token is a closing parenthesis.
5763          If we find the closing `)', and the next token is a `{', then
5764          we are looking at a compound-literal.  */
5765       compound_literal_p
5766         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5767                                                   /*consume_paren=*/true)
5768            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5769       /* Roll back the tokens we skipped.  */
5770       cp_lexer_rollback_tokens (parser->lexer);
5771       /* If we were looking at a compound-literal, simulate an error
5772          so that the call to cp_parser_parse_definitely below will
5773          fail.  */
5774       if (compound_literal_p)
5775         cp_parser_simulate_error (parser);
5776       else
5777         {
5778           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5779           parser->in_type_id_in_expr_p = true;
5780           /* Look for the type-id.  */
5781           type = cp_parser_type_id (parser);
5782           /* Look for the closing `)'.  */
5783           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5784           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5785         }
5786
5787       /* Restore the saved message.  */
5788       parser->type_definition_forbidden_message = saved_message;
5789
5790       /* If ok so far, parse the dependent expression. We cannot be
5791          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5792          ctor of T, but looks like a cast to function returning T
5793          without a dependent expression.  */
5794       if (!cp_parser_error_occurred (parser))
5795         expr = cp_parser_cast_expression (parser,
5796                                           /*address_p=*/false,
5797                                           /*cast_p=*/true);
5798
5799       if (cp_parser_parse_definitely (parser))
5800         {
5801           /* Warn about old-style casts, if so requested.  */
5802           if (warn_old_style_cast
5803               && !in_system_header
5804               && !VOID_TYPE_P (type)
5805               && current_lang_name != lang_name_c)
5806             warning (OPT_Wold_style_cast, "use of old-style cast");
5807
5808           /* Only type conversions to integral or enumeration types
5809              can be used in constant-expressions.  */
5810           if (!cast_valid_in_integral_constant_expression_p (type)
5811               && (cp_parser_non_integral_constant_expression
5812                   (parser,
5813                    "a cast to a type other than an integral or "
5814                    "enumeration type")))
5815             return error_mark_node;
5816
5817           /* Perform the cast.  */
5818           expr = build_c_cast (type, expr);
5819           return expr;
5820         }
5821     }
5822
5823   /* If we get here, then it's not a cast, so it must be a
5824      unary-expression.  */
5825   return cp_parser_unary_expression (parser, address_p, cast_p);
5826 }
5827
5828 /* Parse a binary expression of the general form:
5829
5830    pm-expression:
5831      cast-expression
5832      pm-expression .* cast-expression
5833      pm-expression ->* cast-expression
5834
5835    multiplicative-expression:
5836      pm-expression
5837      multiplicative-expression * pm-expression
5838      multiplicative-expression / pm-expression
5839      multiplicative-expression % pm-expression
5840
5841    additive-expression:
5842      multiplicative-expression
5843      additive-expression + multiplicative-expression
5844      additive-expression - multiplicative-expression
5845
5846    shift-expression:
5847      additive-expression
5848      shift-expression << additive-expression
5849      shift-expression >> additive-expression
5850
5851    relational-expression:
5852      shift-expression
5853      relational-expression < shift-expression
5854      relational-expression > shift-expression
5855      relational-expression <= shift-expression
5856      relational-expression >= shift-expression
5857
5858   GNU Extension:
5859
5860    relational-expression:
5861      relational-expression <? shift-expression
5862      relational-expression >? shift-expression
5863
5864    equality-expression:
5865      relational-expression
5866      equality-expression == relational-expression
5867      equality-expression != relational-expression
5868
5869    and-expression:
5870      equality-expression
5871      and-expression & equality-expression
5872
5873    exclusive-or-expression:
5874      and-expression
5875      exclusive-or-expression ^ and-expression
5876
5877    inclusive-or-expression:
5878      exclusive-or-expression
5879      inclusive-or-expression | exclusive-or-expression
5880
5881    logical-and-expression:
5882      inclusive-or-expression
5883      logical-and-expression && inclusive-or-expression
5884
5885    logical-or-expression:
5886      logical-and-expression
5887      logical-or-expression || logical-and-expression
5888
5889    All these are implemented with a single function like:
5890
5891    binary-expression:
5892      simple-cast-expression
5893      binary-expression <token> binary-expression
5894
5895    CAST_P is true if this expression is the target of a cast.
5896
5897    The binops_by_token map is used to get the tree codes for each <token> type.
5898    binary-expressions are associated according to a precedence table.  */
5899
5900 #define TOKEN_PRECEDENCE(token)                              \
5901 (((token->type == CPP_GREATER                                \
5902    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5903   && !parser->greater_than_is_operator_p)                    \
5904  ? PREC_NOT_OPERATOR                                         \
5905  : binops_by_token[token->type].prec)
5906
5907 static tree
5908 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5909 {
5910   cp_parser_expression_stack stack;
5911   cp_parser_expression_stack_entry *sp = &stack[0];
5912   tree lhs, rhs;
5913   cp_token *token;
5914   enum tree_code tree_type, lhs_type, rhs_type;
5915   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5916   bool overloaded_p;
5917
5918   /* Parse the first expression.  */
5919   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5920   lhs_type = ERROR_MARK;
5921
5922   for (;;)
5923     {
5924       /* Get an operator token.  */
5925       token = cp_lexer_peek_token (parser->lexer);
5926
5927       if (warn_cxx0x_compat
5928           && token->type == CPP_RSHIFT
5929           && !parser->greater_than_is_operator_p)
5930         {
5931           warning (OPT_Wc__0x_compat, 
5932                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
5933                    &token->location);
5934           warning (OPT_Wc__0x_compat, 
5935                    "suggest parentheses around %<>>%> expression");
5936         }
5937
5938       new_prec = TOKEN_PRECEDENCE (token);
5939
5940       /* Popping an entry off the stack means we completed a subexpression:
5941          - either we found a token which is not an operator (`>' where it is not
5942            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5943            will happen repeatedly;
5944          - or, we found an operator which has lower priority.  This is the case
5945            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5946            parsing `3 * 4'.  */
5947       if (new_prec <= prec)
5948         {
5949           if (sp == stack)
5950             break;
5951           else
5952             goto pop;
5953         }
5954
5955      get_rhs:
5956       tree_type = binops_by_token[token->type].tree_type;
5957
5958       /* We used the operator token.  */
5959       cp_lexer_consume_token (parser->lexer);
5960
5961       /* Extract another operand.  It may be the RHS of this expression
5962          or the LHS of a new, higher priority expression.  */
5963       rhs = cp_parser_simple_cast_expression (parser);
5964       rhs_type = ERROR_MARK;
5965
5966       /* Get another operator token.  Look up its precedence to avoid
5967          building a useless (immediately popped) stack entry for common
5968          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5969       token = cp_lexer_peek_token (parser->lexer);
5970       lookahead_prec = TOKEN_PRECEDENCE (token);
5971       if (lookahead_prec > new_prec)
5972         {
5973           /* ... and prepare to parse the RHS of the new, higher priority
5974              expression.  Since precedence levels on the stack are
5975              monotonically increasing, we do not have to care about
5976              stack overflows.  */
5977           sp->prec = prec;
5978           sp->tree_type = tree_type;
5979           sp->lhs = lhs;
5980           sp->lhs_type = lhs_type;
5981           sp++;
5982           lhs = rhs;
5983           lhs_type = rhs_type;
5984           prec = new_prec;
5985           new_prec = lookahead_prec;
5986           goto get_rhs;
5987
5988          pop:
5989           /* If the stack is not empty, we have parsed into LHS the right side
5990              (`4' in the example above) of an expression we had suspended.
5991              We can use the information on the stack to recover the LHS (`3')
5992              from the stack together with the tree code (`MULT_EXPR'), and
5993              the precedence of the higher level subexpression
5994              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5995              which will be used to actually build the additive expression.  */
5996           --sp;
5997           prec = sp->prec;
5998           tree_type = sp->tree_type;
5999           rhs = lhs;
6000           rhs_type = lhs_type;
6001           lhs = sp->lhs;
6002           lhs_type = sp->lhs_type;
6003         }
6004
6005       overloaded_p = false;
6006       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6007                                &overloaded_p);
6008       lhs_type = tree_type;
6009
6010       /* If the binary operator required the use of an overloaded operator,
6011          then this expression cannot be an integral constant-expression.
6012          An overloaded operator can be used even if both operands are
6013          otherwise permissible in an integral constant-expression if at
6014          least one of the operands is of enumeration type.  */
6015
6016       if (overloaded_p
6017           && (cp_parser_non_integral_constant_expression
6018               (parser, "calls to overloaded operators")))
6019         return error_mark_node;
6020     }
6021
6022   return lhs;
6023 }
6024
6025
6026 /* Parse the `? expression : assignment-expression' part of a
6027    conditional-expression.  The LOGICAL_OR_EXPR is the
6028    logical-or-expression that started the conditional-expression.
6029    Returns a representation of the entire conditional-expression.
6030
6031    This routine is used by cp_parser_assignment_expression.
6032
6033      ? expression : assignment-expression
6034
6035    GNU Extensions:
6036
6037      ? : assignment-expression */
6038
6039 static tree
6040 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6041 {
6042   tree expr;
6043   tree assignment_expr;
6044
6045   /* Consume the `?' token.  */
6046   cp_lexer_consume_token (parser->lexer);
6047   if (cp_parser_allow_gnu_extensions_p (parser)
6048       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6049     /* Implicit true clause.  */
6050     expr = NULL_TREE;
6051   else
6052     /* Parse the expression.  */
6053     expr = cp_parser_expression (parser, /*cast_p=*/false);
6054
6055   /* The next token should be a `:'.  */
6056   cp_parser_require (parser, CPP_COLON, "`:'");
6057   /* Parse the assignment-expression.  */
6058   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6059
6060   /* Build the conditional-expression.  */
6061   return build_x_conditional_expr (logical_or_expr,
6062                                    expr,
6063                                    assignment_expr);
6064 }
6065
6066 /* Parse an assignment-expression.
6067
6068    assignment-expression:
6069      conditional-expression
6070      logical-or-expression assignment-operator assignment_expression
6071      throw-expression
6072
6073    CAST_P is true if this expression is the target of a cast.
6074
6075    Returns a representation for the expression.  */
6076
6077 static tree
6078 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6079 {
6080   tree expr;
6081
6082   /* If the next token is the `throw' keyword, then we're looking at
6083      a throw-expression.  */
6084   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6085     expr = cp_parser_throw_expression (parser);
6086   /* Otherwise, it must be that we are looking at a
6087      logical-or-expression.  */
6088   else
6089     {
6090       /* Parse the binary expressions (logical-or-expression).  */
6091       expr = cp_parser_binary_expression (parser, cast_p);
6092       /* If the next token is a `?' then we're actually looking at a
6093          conditional-expression.  */
6094       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6095         return cp_parser_question_colon_clause (parser, expr);
6096       else
6097         {
6098           enum tree_code assignment_operator;
6099
6100           /* If it's an assignment-operator, we're using the second
6101              production.  */
6102           assignment_operator
6103             = cp_parser_assignment_operator_opt (parser);
6104           if (assignment_operator != ERROR_MARK)
6105             {
6106               tree rhs;
6107
6108               /* Parse the right-hand side of the assignment.  */
6109               rhs = cp_parser_assignment_expression (parser, cast_p);
6110               /* An assignment may not appear in a
6111                  constant-expression.  */
6112               if (cp_parser_non_integral_constant_expression (parser,
6113                                                               "an assignment"))
6114                 return error_mark_node;
6115               /* Build the assignment expression.  */
6116               expr = build_x_modify_expr (expr,
6117                                           assignment_operator,
6118                                           rhs);
6119             }
6120         }
6121     }
6122
6123   return expr;
6124 }
6125
6126 /* Parse an (optional) assignment-operator.
6127
6128    assignment-operator: one of
6129      = *= /= %= += -= >>= <<= &= ^= |=
6130
6131    GNU Extension:
6132
6133    assignment-operator: one of
6134      <?= >?=
6135
6136    If the next token is an assignment operator, the corresponding tree
6137    code is returned, and the token is consumed.  For example, for
6138    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6139    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6140    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6141    operator, ERROR_MARK is returned.  */
6142
6143 static enum tree_code
6144 cp_parser_assignment_operator_opt (cp_parser* parser)
6145 {
6146   enum tree_code op;
6147   cp_token *token;
6148
6149   /* Peek at the next toen.  */
6150   token = cp_lexer_peek_token (parser->lexer);
6151
6152   switch (token->type)
6153     {
6154     case CPP_EQ:
6155       op = NOP_EXPR;
6156       break;
6157
6158     case CPP_MULT_EQ:
6159       op = MULT_EXPR;
6160       break;
6161
6162     case CPP_DIV_EQ:
6163       op = TRUNC_DIV_EXPR;
6164       break;
6165
6166     case CPP_MOD_EQ:
6167       op = TRUNC_MOD_EXPR;
6168       break;
6169
6170     case CPP_PLUS_EQ:
6171       op = PLUS_EXPR;
6172       break;
6173
6174     case CPP_MINUS_EQ:
6175       op = MINUS_EXPR;
6176       break;
6177
6178     case CPP_RSHIFT_EQ:
6179       op = RSHIFT_EXPR;
6180       break;
6181
6182     case CPP_LSHIFT_EQ:
6183       op = LSHIFT_EXPR;
6184       break;
6185
6186     case CPP_AND_EQ:
6187       op = BIT_AND_EXPR;
6188       break;
6189
6190     case CPP_XOR_EQ:
6191       op = BIT_XOR_EXPR;
6192       break;
6193
6194     case CPP_OR_EQ:
6195       op = BIT_IOR_EXPR;
6196       break;
6197
6198     default:
6199       /* Nothing else is an assignment operator.  */
6200       op = ERROR_MARK;
6201     }
6202
6203   /* If it was an assignment operator, consume it.  */
6204   if (op != ERROR_MARK)
6205     cp_lexer_consume_token (parser->lexer);
6206
6207   return op;
6208 }
6209
6210 /* Parse an expression.
6211
6212    expression:
6213      assignment-expression
6214      expression , assignment-expression
6215
6216    CAST_P is true if this expression is the target of a cast.
6217
6218    Returns a representation of the expression.  */
6219
6220 static tree
6221 cp_parser_expression (cp_parser* parser, bool cast_p)
6222 {
6223   tree expression = NULL_TREE;
6224
6225   while (true)
6226     {
6227       tree assignment_expression;
6228
6229       /* Parse the next assignment-expression.  */
6230       assignment_expression
6231         = cp_parser_assignment_expression (parser, cast_p);
6232       /* If this is the first assignment-expression, we can just
6233          save it away.  */
6234       if (!expression)
6235         expression = assignment_expression;
6236       else
6237         expression = build_x_compound_expr (expression,
6238                                             assignment_expression);
6239       /* If the next token is not a comma, then we are done with the
6240          expression.  */
6241       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6242         break;
6243       /* Consume the `,'.  */
6244       cp_lexer_consume_token (parser->lexer);
6245       /* A comma operator cannot appear in a constant-expression.  */
6246       if (cp_parser_non_integral_constant_expression (parser,
6247                                                       "a comma operator"))
6248         expression = error_mark_node;
6249     }
6250
6251   return expression;
6252 }
6253
6254 /* Parse a constant-expression.
6255
6256    constant-expression:
6257      conditional-expression
6258
6259   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6260   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6261   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6262   is false, NON_CONSTANT_P should be NULL.  */
6263
6264 static tree
6265 cp_parser_constant_expression (cp_parser* parser,
6266                                bool allow_non_constant_p,
6267                                bool *non_constant_p)
6268 {
6269   bool saved_integral_constant_expression_p;
6270   bool saved_allow_non_integral_constant_expression_p;
6271   bool saved_non_integral_constant_expression_p;
6272   tree expression;
6273
6274   /* It might seem that we could simply parse the
6275      conditional-expression, and then check to see if it were
6276      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6277      one that the compiler can figure out is constant, possibly after
6278      doing some simplifications or optimizations.  The standard has a
6279      precise definition of constant-expression, and we must honor
6280      that, even though it is somewhat more restrictive.
6281
6282      For example:
6283
6284        int i[(2, 3)];
6285
6286      is not a legal declaration, because `(2, 3)' is not a
6287      constant-expression.  The `,' operator is forbidden in a
6288      constant-expression.  However, GCC's constant-folding machinery
6289      will fold this operation to an INTEGER_CST for `3'.  */
6290
6291   /* Save the old settings.  */
6292   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6293   saved_allow_non_integral_constant_expression_p
6294     = parser->allow_non_integral_constant_expression_p;
6295   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6296   /* We are now parsing a constant-expression.  */
6297   parser->integral_constant_expression_p = true;
6298   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6299   parser->non_integral_constant_expression_p = false;
6300   /* Although the grammar says "conditional-expression", we parse an
6301      "assignment-expression", which also permits "throw-expression"
6302      and the use of assignment operators.  In the case that
6303      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6304      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6305      actually essential that we look for an assignment-expression.
6306      For example, cp_parser_initializer_clauses uses this function to
6307      determine whether a particular assignment-expression is in fact
6308      constant.  */
6309   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6310   /* Restore the old settings.  */
6311   parser->integral_constant_expression_p
6312     = saved_integral_constant_expression_p;
6313   parser->allow_non_integral_constant_expression_p
6314     = saved_allow_non_integral_constant_expression_p;
6315   if (allow_non_constant_p)
6316     *non_constant_p = parser->non_integral_constant_expression_p;
6317   else if (parser->non_integral_constant_expression_p)
6318     expression = error_mark_node;
6319   parser->non_integral_constant_expression_p
6320     = saved_non_integral_constant_expression_p;
6321
6322   return expression;
6323 }
6324
6325 /* Parse __builtin_offsetof.
6326
6327    offsetof-expression:
6328      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6329
6330    offsetof-member-designator:
6331      id-expression
6332      | offsetof-member-designator "." id-expression
6333      | offsetof-member-designator "[" expression "]"  */
6334
6335 static tree
6336 cp_parser_builtin_offsetof (cp_parser *parser)
6337 {
6338   int save_ice_p, save_non_ice_p;
6339   tree type, expr;
6340   cp_id_kind dummy;
6341
6342   /* We're about to accept non-integral-constant things, but will
6343      definitely yield an integral constant expression.  Save and
6344      restore these values around our local parsing.  */
6345   save_ice_p = parser->integral_constant_expression_p;
6346   save_non_ice_p = parser->non_integral_constant_expression_p;
6347
6348   /* Consume the "__builtin_offsetof" token.  */
6349   cp_lexer_consume_token (parser->lexer);
6350   /* Consume the opening `('.  */
6351   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6352   /* Parse the type-id.  */
6353   type = cp_parser_type_id (parser);
6354   /* Look for the `,'.  */
6355   cp_parser_require (parser, CPP_COMMA, "`,'");
6356
6357   /* Build the (type *)null that begins the traditional offsetof macro.  */
6358   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6359
6360   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6361   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6362                                                  true, &dummy);
6363   while (true)
6364     {
6365       cp_token *token = cp_lexer_peek_token (parser->lexer);
6366       switch (token->type)
6367         {
6368         case CPP_OPEN_SQUARE:
6369           /* offsetof-member-designator "[" expression "]" */
6370           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6371           break;
6372
6373         case CPP_DOT:
6374           /* offsetof-member-designator "." identifier */
6375           cp_lexer_consume_token (parser->lexer);
6376           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6377                                                          true, &dummy);
6378           break;
6379
6380         case CPP_CLOSE_PAREN:
6381           /* Consume the ")" token.  */
6382           cp_lexer_consume_token (parser->lexer);
6383           goto success;
6384
6385         default:
6386           /* Error.  We know the following require will fail, but
6387              that gives the proper error message.  */
6388           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6389           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6390           expr = error_mark_node;
6391           goto failure;
6392         }
6393     }
6394
6395  success:
6396   /* If we're processing a template, we can't finish the semantics yet.
6397      Otherwise we can fold the entire expression now.  */
6398   if (processing_template_decl)
6399     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6400   else
6401     expr = finish_offsetof (expr);
6402
6403  failure:
6404   parser->integral_constant_expression_p = save_ice_p;
6405   parser->non_integral_constant_expression_p = save_non_ice_p;
6406
6407   return expr;
6408 }
6409
6410 /* Parse a trait expression.  */
6411
6412 static tree
6413 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6414 {
6415   cp_trait_kind kind;
6416   tree type1, type2 = NULL_TREE;
6417   bool binary = false;
6418   cp_decl_specifier_seq decl_specs;
6419
6420   switch (keyword)
6421     {
6422     case RID_HAS_NOTHROW_ASSIGN:
6423       kind = CPTK_HAS_NOTHROW_ASSIGN;
6424       break;
6425     case RID_HAS_NOTHROW_CONSTRUCTOR:
6426       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6427       break;
6428     case RID_HAS_NOTHROW_COPY:
6429       kind = CPTK_HAS_NOTHROW_COPY;
6430       break;
6431     case RID_HAS_TRIVIAL_ASSIGN:
6432       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6433       break;
6434     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6435       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6436       break;
6437     case RID_HAS_TRIVIAL_COPY:
6438       kind = CPTK_HAS_TRIVIAL_COPY;
6439       break;
6440     case RID_HAS_TRIVIAL_DESTRUCTOR:
6441       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6442       break;
6443     case RID_HAS_VIRTUAL_DESTRUCTOR:
6444       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6445       break;
6446     case RID_IS_ABSTRACT:
6447       kind = CPTK_IS_ABSTRACT;
6448       break;
6449     case RID_IS_BASE_OF:
6450       kind = CPTK_IS_BASE_OF;
6451       binary = true;
6452       break;
6453     case RID_IS_CLASS:
6454       kind = CPTK_IS_CLASS;
6455       break;
6456     case RID_IS_CONVERTIBLE_TO:
6457       kind = CPTK_IS_CONVERTIBLE_TO;
6458       binary = true;
6459       break;
6460     case RID_IS_EMPTY:
6461       kind = CPTK_IS_EMPTY;
6462       break;
6463     case RID_IS_ENUM:
6464       kind = CPTK_IS_ENUM;
6465       break;
6466     case RID_IS_POD:
6467       kind = CPTK_IS_POD;
6468       break;
6469     case RID_IS_POLYMORPHIC:
6470       kind = CPTK_IS_POLYMORPHIC;
6471       break;
6472     case RID_IS_UNION:
6473       kind = CPTK_IS_UNION;
6474       break;
6475     default:
6476       gcc_unreachable ();
6477     }
6478
6479   /* Consume the token.  */
6480   cp_lexer_consume_token (parser->lexer);
6481
6482   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6483
6484   type1 = cp_parser_type_id (parser);
6485
6486   /* Build a trivial decl-specifier-seq.  */
6487   clear_decl_specs (&decl_specs);
6488   decl_specs.type = type1;
6489
6490   /* Call grokdeclarator to figure out what type this is.  */
6491   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6492                           /*initialized=*/0, /*attrlist=*/NULL);
6493
6494   if (binary)
6495     {
6496       cp_parser_require (parser, CPP_COMMA, "`,'");
6497  
6498       type2 = cp_parser_type_id (parser);
6499
6500       /* Build a trivial decl-specifier-seq.  */
6501       clear_decl_specs (&decl_specs);
6502       decl_specs.type = type2;
6503
6504       /* Call grokdeclarator to figure out what type this is.  */
6505       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6506                               /*initialized=*/0, /*attrlist=*/NULL);
6507     }
6508
6509   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6510
6511   /* Complete the trait expr, which may mean either processing the
6512      static assert now or saving it for template instantiation.  */
6513   return finish_trait_expr (kind, type1, type2);
6514 }
6515
6516 /* Statements [gram.stmt.stmt]  */
6517
6518 /* Parse a statement.
6519
6520    statement:
6521      labeled-statement
6522      expression-statement
6523      compound-statement
6524      selection-statement
6525      iteration-statement
6526      jump-statement
6527      declaration-statement
6528      try-block
6529
6530   IN_COMPOUND is true when the statement is nested inside a
6531   cp_parser_compound_statement; this matters for certain pragmas.
6532
6533   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6534   is a (possibly labeled) if statement which is not enclosed in braces
6535   and has an else clause.  This is used to implement -Wparentheses.  */
6536
6537 static void
6538 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6539                      bool in_compound, bool *if_p)
6540 {
6541   tree statement;
6542   cp_token *token;
6543   location_t statement_location;
6544
6545  restart:
6546   if (if_p != NULL)
6547     *if_p = false;
6548   /* There is no statement yet.  */
6549   statement = NULL_TREE;
6550   /* Peek at the next token.  */
6551   token = cp_lexer_peek_token (parser->lexer);
6552   /* Remember the location of the first token in the statement.  */
6553   statement_location = token->location;
6554   /* If this is a keyword, then that will often determine what kind of
6555      statement we have.  */
6556   if (token->type == CPP_KEYWORD)
6557     {
6558       enum rid keyword = token->keyword;
6559
6560       switch (keyword)
6561         {
6562         case RID_CASE:
6563         case RID_DEFAULT:
6564           /* Looks like a labeled-statement with a case label.
6565              Parse the label, and then use tail recursion to parse
6566              the statement.  */
6567           cp_parser_label_for_labeled_statement (parser);
6568           goto restart;
6569
6570         case RID_IF:
6571         case RID_SWITCH:
6572           statement = cp_parser_selection_statement (parser, if_p);
6573           break;
6574
6575         case RID_WHILE:
6576         case RID_DO:
6577         case RID_FOR:
6578           statement = cp_parser_iteration_statement (parser);
6579           break;
6580
6581         case RID_BREAK:
6582         case RID_CONTINUE:
6583         case RID_RETURN:
6584         case RID_GOTO:
6585           statement = cp_parser_jump_statement (parser);
6586           break;
6587
6588           /* Objective-C++ exception-handling constructs.  */
6589         case RID_AT_TRY:
6590         case RID_AT_CATCH:
6591         case RID_AT_FINALLY:
6592         case RID_AT_SYNCHRONIZED:
6593         case RID_AT_THROW:
6594           statement = cp_parser_objc_statement (parser);
6595           break;
6596
6597         case RID_TRY:
6598           statement = cp_parser_try_block (parser);
6599           break;
6600
6601         case RID_NAMESPACE:
6602           /* This must be a namespace alias definition.  */
6603           cp_parser_declaration_statement (parser);
6604           return;
6605           
6606         default:
6607           /* It might be a keyword like `int' that can start a
6608              declaration-statement.  */
6609           break;
6610         }
6611     }
6612   else if (token->type == CPP_NAME)
6613     {
6614       /* If the next token is a `:', then we are looking at a
6615          labeled-statement.  */
6616       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6617       if (token->type == CPP_COLON)
6618         {
6619           /* Looks like a labeled-statement with an ordinary label.
6620              Parse the label, and then use tail recursion to parse
6621              the statement.  */
6622           cp_parser_label_for_labeled_statement (parser);
6623           goto restart;
6624         }
6625     }
6626   /* Anything that starts with a `{' must be a compound-statement.  */
6627   else if (token->type == CPP_OPEN_BRACE)
6628     statement = cp_parser_compound_statement (parser, NULL, false);
6629   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6630      a statement all its own.  */
6631   else if (token->type == CPP_PRAGMA)
6632     {
6633       /* Only certain OpenMP pragmas are attached to statements, and thus
6634          are considered statements themselves.  All others are not.  In
6635          the context of a compound, accept the pragma as a "statement" and
6636          return so that we can check for a close brace.  Otherwise we
6637          require a real statement and must go back and read one.  */
6638       if (in_compound)
6639         cp_parser_pragma (parser, pragma_compound);
6640       else if (!cp_parser_pragma (parser, pragma_stmt))
6641         goto restart;
6642       return;
6643     }
6644   else if (token->type == CPP_EOF)
6645     {
6646       cp_parser_error (parser, "expected statement");
6647       return;
6648     }
6649
6650   /* Everything else must be a declaration-statement or an
6651      expression-statement.  Try for the declaration-statement
6652      first, unless we are looking at a `;', in which case we know that
6653      we have an expression-statement.  */
6654   if (!statement)
6655     {
6656       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6657         {
6658           cp_parser_parse_tentatively (parser);
6659           /* Try to parse the declaration-statement.  */
6660           cp_parser_declaration_statement (parser);
6661           /* If that worked, we're done.  */
6662           if (cp_parser_parse_definitely (parser))
6663             return;
6664         }
6665       /* Look for an expression-statement instead.  */
6666       statement = cp_parser_expression_statement (parser, in_statement_expr);
6667     }
6668
6669   /* Set the line number for the statement.  */
6670   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6671     SET_EXPR_LOCATION (statement, statement_location);
6672 }
6673
6674 /* Parse the label for a labeled-statement, i.e.
6675
6676    identifier :
6677    case constant-expression :
6678    default :
6679
6680    GNU Extension:
6681    case constant-expression ... constant-expression : statement
6682
6683    When a label is parsed without errors, the label is added to the
6684    parse tree by the finish_* functions, so this function doesn't
6685    have to return the label.  */
6686
6687 static void
6688 cp_parser_label_for_labeled_statement (cp_parser* parser)
6689 {
6690   cp_token *token;
6691
6692   /* The next token should be an identifier.  */
6693   token = cp_lexer_peek_token (parser->lexer);
6694   if (token->type != CPP_NAME
6695       && token->type != CPP_KEYWORD)
6696     {
6697       cp_parser_error (parser, "expected labeled-statement");
6698       return;
6699     }
6700
6701   switch (token->keyword)
6702     {
6703     case RID_CASE:
6704       {
6705         tree expr, expr_hi;
6706         cp_token *ellipsis;
6707
6708         /* Consume the `case' token.  */
6709         cp_lexer_consume_token (parser->lexer);
6710         /* Parse the constant-expression.  */
6711         expr = cp_parser_constant_expression (parser,
6712                                               /*allow_non_constant_p=*/false,
6713                                               NULL);
6714
6715         ellipsis = cp_lexer_peek_token (parser->lexer);
6716         if (ellipsis->type == CPP_ELLIPSIS)
6717           {
6718             /* Consume the `...' token.  */
6719             cp_lexer_consume_token (parser->lexer);
6720             expr_hi =
6721               cp_parser_constant_expression (parser,
6722                                              /*allow_non_constant_p=*/false,
6723                                              NULL);
6724             /* We don't need to emit warnings here, as the common code
6725                will do this for us.  */
6726           }
6727         else
6728           expr_hi = NULL_TREE;
6729
6730         if (parser->in_switch_statement_p)
6731           finish_case_label (expr, expr_hi);
6732         else
6733           error ("case label %qE not within a switch statement", expr);
6734       }
6735       break;
6736
6737     case RID_DEFAULT:
6738       /* Consume the `default' token.  */
6739       cp_lexer_consume_token (parser->lexer);
6740
6741       if (parser->in_switch_statement_p)
6742         finish_case_label (NULL_TREE, NULL_TREE);
6743       else
6744         error ("case label not within a switch statement");
6745       break;
6746
6747     default:
6748       /* Anything else must be an ordinary label.  */
6749       finish_label_stmt (cp_parser_identifier (parser));
6750       break;
6751     }
6752
6753   /* Require the `:' token.  */
6754   cp_parser_require (parser, CPP_COLON, "`:'");
6755 }
6756
6757 /* Parse an expression-statement.
6758
6759    expression-statement:
6760      expression [opt] ;
6761
6762    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6763    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6764    indicates whether this expression-statement is part of an
6765    expression statement.  */
6766
6767 static tree
6768 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6769 {
6770   tree statement = NULL_TREE;
6771
6772   /* If the next token is a ';', then there is no expression
6773      statement.  */
6774   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6775     statement = cp_parser_expression (parser, /*cast_p=*/false);
6776
6777   /* Consume the final `;'.  */
6778   cp_parser_consume_semicolon_at_end_of_statement (parser);
6779
6780   if (in_statement_expr
6781       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6782     /* This is the final expression statement of a statement
6783        expression.  */
6784     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6785   else if (statement)
6786     statement = finish_expr_stmt (statement);
6787   else
6788     finish_stmt ();
6789
6790   return statement;
6791 }
6792
6793 /* Parse a compound-statement.
6794
6795    compound-statement:
6796      { statement-seq [opt] }
6797
6798    Returns a tree representing the statement.  */
6799
6800 static tree
6801 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6802                               bool in_try)
6803 {
6804   tree compound_stmt;
6805
6806   /* Consume the `{'.  */
6807   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6808     return error_mark_node;
6809   /* Begin the compound-statement.  */
6810   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6811   /* Parse an (optional) statement-seq.  */
6812   cp_parser_statement_seq_opt (parser, in_statement_expr);
6813   /* Finish the compound-statement.  */
6814   finish_compound_stmt (compound_stmt);
6815   /* Consume the `}'.  */
6816   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6817
6818   return compound_stmt;
6819 }
6820
6821 /* Parse an (optional) statement-seq.
6822
6823    statement-seq:
6824      statement
6825      statement-seq [opt] statement  */
6826
6827 static void
6828 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6829 {
6830   /* Scan statements until there aren't any more.  */
6831   while (true)
6832     {
6833       cp_token *token = cp_lexer_peek_token (parser->lexer);
6834
6835       /* If we're looking at a `}', then we've run out of statements.  */
6836       if (token->type == CPP_CLOSE_BRACE
6837           || token->type == CPP_EOF
6838           || token->type == CPP_PRAGMA_EOL)
6839         break;
6840       
6841       /* If we are in a compound statement and find 'else' then
6842          something went wrong.  */
6843       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6844         {
6845           if (parser->in_statement & IN_IF_STMT) 
6846             break;
6847           else
6848             {
6849               token = cp_lexer_consume_token (parser->lexer);
6850               error ("%<else%> without a previous %<if%>");
6851             }
6852         }
6853
6854       /* Parse the statement.  */
6855       cp_parser_statement (parser, in_statement_expr, true, NULL);
6856     }
6857 }
6858
6859 /* Parse a selection-statement.
6860
6861    selection-statement:
6862      if ( condition ) statement
6863      if ( condition ) statement else statement
6864      switch ( condition ) statement
6865
6866    Returns the new IF_STMT or SWITCH_STMT.
6867
6868    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6869    is a (possibly labeled) if statement which is not enclosed in
6870    braces and has an else clause.  This is used to implement
6871    -Wparentheses.  */
6872
6873 static tree
6874 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6875 {
6876   cp_token *token;
6877   enum rid keyword;
6878
6879   if (if_p != NULL)
6880     *if_p = false;
6881
6882   /* Peek at the next token.  */
6883   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6884
6885   /* See what kind of keyword it is.  */
6886   keyword = token->keyword;
6887   switch (keyword)
6888     {
6889     case RID_IF:
6890     case RID_SWITCH:
6891       {
6892         tree statement;
6893         tree condition;
6894
6895         /* Look for the `('.  */
6896         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6897           {
6898             cp_parser_skip_to_end_of_statement (parser);
6899             return error_mark_node;
6900           }
6901
6902         /* Begin the selection-statement.  */
6903         if (keyword == RID_IF)
6904           statement = begin_if_stmt ();
6905         else
6906           statement = begin_switch_stmt ();
6907
6908         /* Parse the condition.  */
6909         condition = cp_parser_condition (parser);
6910         /* Look for the `)'.  */
6911         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6912           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6913                                                  /*consume_paren=*/true);
6914
6915         if (keyword == RID_IF)
6916           {
6917             bool nested_if;
6918             unsigned char in_statement;
6919
6920             /* Add the condition.  */
6921             finish_if_stmt_cond (condition, statement);
6922
6923             /* Parse the then-clause.  */
6924             in_statement = parser->in_statement;
6925             parser->in_statement |= IN_IF_STMT;
6926             cp_parser_implicitly_scoped_statement (parser, &nested_if);
6927             parser->in_statement = in_statement;
6928
6929             finish_then_clause (statement);
6930
6931             /* If the next token is `else', parse the else-clause.  */
6932             if (cp_lexer_next_token_is_keyword (parser->lexer,
6933                                                 RID_ELSE))
6934               {
6935                 /* Consume the `else' keyword.  */
6936                 cp_lexer_consume_token (parser->lexer);
6937                 begin_else_clause (statement);
6938                 /* Parse the else-clause.  */
6939                 cp_parser_implicitly_scoped_statement (parser, NULL);
6940                 finish_else_clause (statement);
6941
6942                 /* If we are currently parsing a then-clause, then
6943                    IF_P will not be NULL.  We set it to true to
6944                    indicate that this if statement has an else clause.
6945                    This may trigger the Wparentheses warning below
6946                    when we get back up to the parent if statement.  */
6947                 if (if_p != NULL)
6948                   *if_p = true;
6949               }
6950             else
6951               {
6952                 /* This if statement does not have an else clause.  If
6953                    NESTED_IF is true, then the then-clause is an if
6954                    statement which does have an else clause.  We warn
6955                    about the potential ambiguity.  */
6956                 if (nested_if)
6957                   warning (OPT_Wparentheses,
6958                            ("%Hsuggest explicit braces "
6959                             "to avoid ambiguous %<else%>"),
6960                            EXPR_LOCUS (statement));
6961               }
6962
6963             /* Now we're all done with the if-statement.  */
6964             finish_if_stmt (statement);
6965           }
6966         else
6967           {
6968             bool in_switch_statement_p;
6969             unsigned char in_statement;
6970
6971             /* Add the condition.  */
6972             finish_switch_cond (condition, statement);
6973
6974             /* Parse the body of the switch-statement.  */
6975             in_switch_statement_p = parser->in_switch_statement_p;
6976             in_statement = parser->in_statement;
6977             parser->in_switch_statement_p = true;
6978             parser->in_statement |= IN_SWITCH_STMT;
6979             cp_parser_implicitly_scoped_statement (parser, NULL);
6980             parser->in_switch_statement_p = in_switch_statement_p;
6981             parser->in_statement = in_statement;
6982
6983             /* Now we're all done with the switch-statement.  */
6984             finish_switch_stmt (statement);
6985           }
6986
6987         return statement;
6988       }
6989       break;
6990
6991     default:
6992       cp_parser_error (parser, "expected selection-statement");
6993       return error_mark_node;
6994     }
6995 }
6996
6997 /* Parse a condition.
6998
6999    condition:
7000      expression
7001      type-specifier-seq declarator = assignment-expression
7002
7003    GNU Extension:
7004
7005    condition:
7006      type-specifier-seq declarator asm-specification [opt]
7007        attributes [opt] = assignment-expression
7008
7009    Returns the expression that should be tested.  */
7010
7011 static tree
7012 cp_parser_condition (cp_parser* parser)
7013 {
7014   cp_decl_specifier_seq type_specifiers;
7015   const char *saved_message;
7016
7017   /* Try the declaration first.  */
7018   cp_parser_parse_tentatively (parser);
7019   /* New types are not allowed in the type-specifier-seq for a
7020      condition.  */
7021   saved_message = parser->type_definition_forbidden_message;
7022   parser->type_definition_forbidden_message
7023     = "types may not be defined in conditions";
7024   /* Parse the type-specifier-seq.  */
7025   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7026                                 &type_specifiers);
7027   /* Restore the saved message.  */
7028   parser->type_definition_forbidden_message = saved_message;
7029   /* If all is well, we might be looking at a declaration.  */
7030   if (!cp_parser_error_occurred (parser))
7031     {
7032       tree decl;
7033       tree asm_specification;
7034       tree attributes;
7035       cp_declarator *declarator;
7036       tree initializer = NULL_TREE;
7037
7038       /* Parse the declarator.  */
7039       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7040                                          /*ctor_dtor_or_conv_p=*/NULL,
7041                                          /*parenthesized_p=*/NULL,
7042                                          /*member_p=*/false);
7043       /* Parse the attributes.  */
7044       attributes = cp_parser_attributes_opt (parser);
7045       /* Parse the asm-specification.  */
7046       asm_specification = cp_parser_asm_specification_opt (parser);
7047       /* If the next token is not an `=', then we might still be
7048          looking at an expression.  For example:
7049
7050            if (A(a).x)
7051
7052          looks like a decl-specifier-seq and a declarator -- but then
7053          there is no `=', so this is an expression.  */
7054       cp_parser_require (parser, CPP_EQ, "`='");
7055       /* If we did see an `=', then we are looking at a declaration
7056          for sure.  */
7057       if (cp_parser_parse_definitely (parser))
7058         {
7059           tree pushed_scope;
7060           bool non_constant_p;
7061
7062           /* Create the declaration.  */
7063           decl = start_decl (declarator, &type_specifiers,
7064                              /*initialized_p=*/true,
7065                              attributes, /*prefix_attributes=*/NULL_TREE,
7066                              &pushed_scope);
7067           /* Parse the assignment-expression.  */
7068           initializer
7069             = cp_parser_constant_expression (parser,
7070                                              /*allow_non_constant_p=*/true,
7071                                              &non_constant_p);
7072           if (!non_constant_p)
7073             initializer = fold_non_dependent_expr (initializer);
7074
7075           /* Process the initializer.  */
7076           cp_finish_decl (decl,
7077                           initializer, !non_constant_p,
7078                           asm_specification,
7079                           LOOKUP_ONLYCONVERTING);
7080
7081           if (pushed_scope)
7082             pop_scope (pushed_scope);
7083
7084           return convert_from_reference (decl);
7085         }
7086     }
7087   /* If we didn't even get past the declarator successfully, we are
7088      definitely not looking at a declaration.  */
7089   else
7090     cp_parser_abort_tentative_parse (parser);
7091
7092   /* Otherwise, we are looking at an expression.  */
7093   return cp_parser_expression (parser, /*cast_p=*/false);
7094 }
7095
7096 /* We check for a ) immediately followed by ; with no whitespacing
7097    between.  This is used to issue a warning for:
7098
7099      while (...);
7100
7101    and:
7102
7103      for (...);
7104
7105    as the semicolon is probably extraneous.
7106
7107    On parse errors, the next token might not be a ), so do nothing in
7108    that case. */
7109
7110 static void
7111 check_empty_body (cp_parser* parser, const char* type)
7112 {
7113   cp_token *token;
7114   cp_token *close_paren;
7115   expanded_location close_loc;
7116   expanded_location semi_loc;
7117   
7118   close_paren = cp_lexer_peek_token (parser->lexer);
7119   if (close_paren->type != CPP_CLOSE_PAREN)
7120     return;
7121
7122   close_loc = expand_location (close_paren->location);
7123   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7124
7125   if (token->type != CPP_SEMICOLON
7126       || (token->flags & PREV_WHITE))
7127     return;
7128
7129   semi_loc =  expand_location (token->location);
7130   if (close_loc.line == semi_loc.line
7131 #ifdef USE_MAPPED_LOCATION
7132       && close_loc.column+1 == semi_loc.column
7133 #endif
7134       )
7135     warning (OPT_Wempty_body,
7136              "suggest a space before %<;%> or explicit braces around empty "
7137              "body in %<%s%> statement",
7138              type);
7139 }
7140
7141 /* Parse an iteration-statement.
7142
7143    iteration-statement:
7144      while ( condition ) statement
7145      do statement while ( expression ) ;
7146      for ( for-init-statement condition [opt] ; expression [opt] )
7147        statement
7148
7149    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7150
7151 static tree
7152 cp_parser_iteration_statement (cp_parser* parser)
7153 {
7154   cp_token *token;
7155   enum rid keyword;
7156   tree statement;
7157   unsigned char in_statement;
7158
7159   /* Peek at the next token.  */
7160   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7161   if (!token)
7162     return error_mark_node;
7163
7164   /* Remember whether or not we are already within an iteration
7165      statement.  */
7166   in_statement = parser->in_statement;
7167
7168   /* See what kind of keyword it is.  */
7169   keyword = token->keyword;
7170   switch (keyword)
7171     {
7172     case RID_WHILE:
7173       {
7174         tree condition;
7175
7176         /* Begin the while-statement.  */
7177         statement = begin_while_stmt ();
7178         /* Look for the `('.  */
7179         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7180         /* Parse the condition.  */
7181         condition = cp_parser_condition (parser);
7182         finish_while_stmt_cond (condition, statement);
7183         check_empty_body (parser, "while");
7184         /* Look for the `)'.  */
7185         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7186         /* Parse the dependent statement.  */
7187         parser->in_statement = IN_ITERATION_STMT;
7188         cp_parser_already_scoped_statement (parser);
7189         parser->in_statement = in_statement;
7190         /* We're done with the while-statement.  */
7191         finish_while_stmt (statement);
7192       }
7193       break;
7194
7195     case RID_DO:
7196       {
7197         tree expression;
7198
7199         /* Begin the do-statement.  */
7200         statement = begin_do_stmt ();
7201         /* Parse the body of the do-statement.  */
7202         parser->in_statement = IN_ITERATION_STMT;
7203         cp_parser_implicitly_scoped_statement (parser, NULL);
7204         parser->in_statement = in_statement;
7205         finish_do_body (statement);
7206         /* Look for the `while' keyword.  */
7207         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7208         /* Look for the `('.  */
7209         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7210         /* Parse the expression.  */
7211         expression = cp_parser_expression (parser, /*cast_p=*/false);
7212         /* We're done with the do-statement.  */
7213         finish_do_stmt (expression, statement);
7214         /* Look for the `)'.  */
7215         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7216         /* Look for the `;'.  */
7217         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7218       }
7219       break;
7220
7221     case RID_FOR:
7222       {
7223         tree condition = NULL_TREE;
7224         tree expression = NULL_TREE;
7225
7226         /* Begin the for-statement.  */
7227         statement = begin_for_stmt ();
7228         /* Look for the `('.  */
7229         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7230         /* Parse the initialization.  */
7231         cp_parser_for_init_statement (parser);
7232         finish_for_init_stmt (statement);
7233
7234         /* If there's a condition, process it.  */
7235         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7236           condition = cp_parser_condition (parser);
7237         finish_for_cond (condition, statement);
7238         /* Look for the `;'.  */
7239         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7240
7241         /* If there's an expression, process it.  */
7242         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7243           expression = cp_parser_expression (parser, /*cast_p=*/false);
7244         finish_for_expr (expression, statement);
7245         check_empty_body (parser, "for");
7246         /* Look for the `)'.  */
7247         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7248
7249         /* Parse the body of the for-statement.  */
7250         parser->in_statement = IN_ITERATION_STMT;
7251         cp_parser_already_scoped_statement (parser);
7252         parser->in_statement = in_statement;
7253
7254         /* We're done with the for-statement.  */
7255         finish_for_stmt (statement);
7256       }
7257       break;
7258
7259     default:
7260       cp_parser_error (parser, "expected iteration-statement");
7261       statement = error_mark_node;
7262       break;
7263     }
7264
7265   return statement;
7266 }
7267
7268 /* Parse a for-init-statement.
7269
7270    for-init-statement:
7271      expression-statement
7272      simple-declaration  */
7273
7274 static void
7275 cp_parser_for_init_statement (cp_parser* parser)
7276 {
7277   /* If the next token is a `;', then we have an empty
7278      expression-statement.  Grammatically, this is also a
7279      simple-declaration, but an invalid one, because it does not
7280      declare anything.  Therefore, if we did not handle this case
7281      specially, we would issue an error message about an invalid
7282      declaration.  */
7283   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7284     {
7285       /* We're going to speculatively look for a declaration, falling back
7286          to an expression, if necessary.  */
7287       cp_parser_parse_tentatively (parser);
7288       /* Parse the declaration.  */
7289       cp_parser_simple_declaration (parser,
7290                                     /*function_definition_allowed_p=*/false);
7291       /* If the tentative parse failed, then we shall need to look for an
7292          expression-statement.  */
7293       if (cp_parser_parse_definitely (parser))
7294         return;
7295     }
7296
7297   cp_parser_expression_statement (parser, false);
7298 }
7299
7300 /* Parse a jump-statement.
7301
7302    jump-statement:
7303      break ;
7304      continue ;
7305      return expression [opt] ;
7306      goto identifier ;
7307
7308    GNU extension:
7309
7310    jump-statement:
7311      goto * expression ;
7312
7313    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7314
7315 static tree
7316 cp_parser_jump_statement (cp_parser* parser)
7317 {
7318   tree statement = error_mark_node;
7319   cp_token *token;
7320   enum rid keyword;
7321   unsigned char in_statement;
7322
7323   /* Peek at the next token.  */
7324   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7325   if (!token)
7326     return error_mark_node;
7327
7328   /* See what kind of keyword it is.  */
7329   keyword = token->keyword;
7330   switch (keyword)
7331     {
7332     case RID_BREAK:
7333       in_statement = parser->in_statement & ~IN_IF_STMT;      
7334       switch (in_statement)
7335         {
7336         case 0:
7337           error ("break statement not within loop or switch");
7338           break;
7339         default:
7340           gcc_assert ((in_statement & IN_SWITCH_STMT)
7341                       || in_statement == IN_ITERATION_STMT);
7342           statement = finish_break_stmt ();
7343           break;
7344         case IN_OMP_BLOCK:
7345           error ("invalid exit from OpenMP structured block");
7346           break;
7347         case IN_OMP_FOR:
7348           error ("break statement used with OpenMP for loop");
7349           break;
7350         }
7351       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7352       break;
7353
7354     case RID_CONTINUE:
7355       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7356         {
7357         case 0:
7358           error ("continue statement not within a loop");
7359           break;
7360         case IN_ITERATION_STMT:
7361         case IN_OMP_FOR:
7362           statement = finish_continue_stmt ();
7363           break;
7364         case IN_OMP_BLOCK:
7365           error ("invalid exit from OpenMP structured block");
7366           break;
7367         default:
7368           gcc_unreachable ();
7369         }
7370       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7371       break;
7372
7373     case RID_RETURN:
7374       {
7375         tree expr;
7376
7377         /* If the next token is a `;', then there is no
7378            expression.  */
7379         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7380           expr = cp_parser_expression (parser, /*cast_p=*/false);
7381         else
7382           expr = NULL_TREE;
7383         /* Build the return-statement.  */
7384         statement = finish_return_stmt (expr);
7385         /* Look for the final `;'.  */
7386         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7387       }
7388       break;
7389
7390     case RID_GOTO:
7391       /* Create the goto-statement.  */
7392       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7393         {
7394           /* Issue a warning about this use of a GNU extension.  */
7395           if (pedantic)
7396             pedwarn ("ISO C++ forbids computed gotos");
7397           /* Consume the '*' token.  */
7398           cp_lexer_consume_token (parser->lexer);
7399           /* Parse the dependent expression.  */
7400           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7401         }
7402       else
7403         finish_goto_stmt (cp_parser_identifier (parser));
7404       /* Look for the final `;'.  */
7405       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7406       break;
7407
7408     default:
7409       cp_parser_error (parser, "expected jump-statement");
7410       break;
7411     }
7412
7413   return statement;
7414 }
7415
7416 /* Parse a declaration-statement.
7417
7418    declaration-statement:
7419      block-declaration  */
7420
7421 static void
7422 cp_parser_declaration_statement (cp_parser* parser)
7423 {
7424   void *p;
7425
7426   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7427   p = obstack_alloc (&declarator_obstack, 0);
7428
7429  /* Parse the block-declaration.  */
7430   cp_parser_block_declaration (parser, /*statement_p=*/true);
7431
7432   /* Free any declarators allocated.  */
7433   obstack_free (&declarator_obstack, p);
7434
7435   /* Finish off the statement.  */
7436   finish_stmt ();
7437 }
7438
7439 /* Some dependent statements (like `if (cond) statement'), are
7440    implicitly in their own scope.  In other words, if the statement is
7441    a single statement (as opposed to a compound-statement), it is
7442    none-the-less treated as if it were enclosed in braces.  Any
7443    declarations appearing in the dependent statement are out of scope
7444    after control passes that point.  This function parses a statement,
7445    but ensures that is in its own scope, even if it is not a
7446    compound-statement.
7447
7448    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7449    is a (possibly labeled) if statement which is not enclosed in
7450    braces and has an else clause.  This is used to implement
7451    -Wparentheses.
7452
7453    Returns the new statement.  */
7454
7455 static tree
7456 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7457 {
7458   tree statement;
7459
7460   if (if_p != NULL)
7461     *if_p = false;
7462
7463   /* Mark if () ; with a special NOP_EXPR.  */
7464   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7465     {
7466       cp_lexer_consume_token (parser->lexer);
7467       statement = add_stmt (build_empty_stmt ());
7468     }
7469   /* if a compound is opened, we simply parse the statement directly.  */
7470   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7471     statement = cp_parser_compound_statement (parser, NULL, false);
7472   /* If the token is not a `{', then we must take special action.  */
7473   else
7474     {
7475       /* Create a compound-statement.  */
7476       statement = begin_compound_stmt (0);
7477       /* Parse the dependent-statement.  */
7478       cp_parser_statement (parser, NULL_TREE, false, if_p);
7479       /* Finish the dummy compound-statement.  */
7480       finish_compound_stmt (statement);
7481     }
7482
7483   /* Return the statement.  */
7484   return statement;
7485 }
7486
7487 /* For some dependent statements (like `while (cond) statement'), we
7488    have already created a scope.  Therefore, even if the dependent
7489    statement is a compound-statement, we do not want to create another
7490    scope.  */
7491
7492 static void
7493 cp_parser_already_scoped_statement (cp_parser* parser)
7494 {
7495   /* If the token is a `{', then we must take special action.  */
7496   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7497     cp_parser_statement (parser, NULL_TREE, false, NULL);
7498   else
7499     {
7500       /* Avoid calling cp_parser_compound_statement, so that we
7501          don't create a new scope.  Do everything else by hand.  */
7502       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7503       cp_parser_statement_seq_opt (parser, NULL_TREE);
7504       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7505     }
7506 }
7507
7508 /* Declarations [gram.dcl.dcl] */
7509
7510 /* Parse an optional declaration-sequence.
7511
7512    declaration-seq:
7513      declaration
7514      declaration-seq declaration  */
7515
7516 static void
7517 cp_parser_declaration_seq_opt (cp_parser* parser)
7518 {
7519   while (true)
7520     {
7521       cp_token *token;
7522
7523       token = cp_lexer_peek_token (parser->lexer);
7524
7525       if (token->type == CPP_CLOSE_BRACE
7526           || token->type == CPP_EOF
7527           || token->type == CPP_PRAGMA_EOL)
7528         break;
7529
7530       if (token->type == CPP_SEMICOLON)
7531         {
7532           /* A declaration consisting of a single semicolon is
7533              invalid.  Allow it unless we're being pedantic.  */
7534           cp_lexer_consume_token (parser->lexer);
7535           if (pedantic && !in_system_header)
7536             pedwarn ("extra %<;%>");
7537           continue;
7538         }
7539
7540       /* If we're entering or exiting a region that's implicitly
7541          extern "C", modify the lang context appropriately.  */
7542       if (!parser->implicit_extern_c && token->implicit_extern_c)
7543         {
7544           push_lang_context (lang_name_c);
7545           parser->implicit_extern_c = true;
7546         }
7547       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7548         {
7549           pop_lang_context ();
7550           parser->implicit_extern_c = false;
7551         }
7552
7553       if (token->type == CPP_PRAGMA)
7554         {
7555           /* A top-level declaration can consist solely of a #pragma.
7556              A nested declaration cannot, so this is done here and not
7557              in cp_parser_declaration.  (A #pragma at block scope is
7558              handled in cp_parser_statement.)  */
7559           cp_parser_pragma (parser, pragma_external);
7560           continue;
7561         }
7562
7563       /* Parse the declaration itself.  */
7564       cp_parser_declaration (parser);
7565     }
7566 }
7567
7568 /* Parse a declaration.
7569
7570    declaration:
7571      block-declaration
7572      function-definition
7573      template-declaration
7574      explicit-instantiation
7575      explicit-specialization
7576      linkage-specification
7577      namespace-definition
7578
7579    GNU extension:
7580
7581    declaration:
7582       __extension__ declaration */
7583
7584 static void
7585 cp_parser_declaration (cp_parser* parser)
7586 {
7587   cp_token token1;
7588   cp_token token2;
7589   int saved_pedantic;
7590   void *p;
7591
7592   /* Check for the `__extension__' keyword.  */
7593   if (cp_parser_extension_opt (parser, &saved_pedantic))
7594     {
7595       /* Parse the qualified declaration.  */
7596       cp_parser_declaration (parser);
7597       /* Restore the PEDANTIC flag.  */
7598       pedantic = saved_pedantic;
7599
7600       return;
7601     }
7602
7603   /* Try to figure out what kind of declaration is present.  */
7604   token1 = *cp_lexer_peek_token (parser->lexer);
7605
7606   if (token1.type != CPP_EOF)
7607     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7608   else
7609     {
7610       token2.type = CPP_EOF;
7611       token2.keyword = RID_MAX;
7612     }
7613
7614   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7615   p = obstack_alloc (&declarator_obstack, 0);
7616
7617   /* If the next token is `extern' and the following token is a string
7618      literal, then we have a linkage specification.  */
7619   if (token1.keyword == RID_EXTERN
7620       && cp_parser_is_string_literal (&token2))
7621     cp_parser_linkage_specification (parser);
7622   /* If the next token is `template', then we have either a template
7623      declaration, an explicit instantiation, or an explicit
7624      specialization.  */
7625   else if (token1.keyword == RID_TEMPLATE)
7626     {
7627       /* `template <>' indicates a template specialization.  */
7628       if (token2.type == CPP_LESS
7629           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7630         cp_parser_explicit_specialization (parser);
7631       /* `template <' indicates a template declaration.  */
7632       else if (token2.type == CPP_LESS)
7633         cp_parser_template_declaration (parser, /*member_p=*/false);
7634       /* Anything else must be an explicit instantiation.  */
7635       else
7636         cp_parser_explicit_instantiation (parser);
7637     }
7638   /* If the next token is `export', then we have a template
7639      declaration.  */
7640   else if (token1.keyword == RID_EXPORT)
7641     cp_parser_template_declaration (parser, /*member_p=*/false);
7642   /* If the next token is `extern', 'static' or 'inline' and the one
7643      after that is `template', we have a GNU extended explicit
7644      instantiation directive.  */
7645   else if (cp_parser_allow_gnu_extensions_p (parser)
7646            && (token1.keyword == RID_EXTERN
7647                || token1.keyword == RID_STATIC
7648                || token1.keyword == RID_INLINE)
7649            && token2.keyword == RID_TEMPLATE)
7650     cp_parser_explicit_instantiation (parser);
7651   /* If the next token is `namespace', check for a named or unnamed
7652      namespace definition.  */
7653   else if (token1.keyword == RID_NAMESPACE
7654            && (/* A named namespace definition.  */
7655                (token2.type == CPP_NAME
7656                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7657                     != CPP_EQ))
7658                /* An unnamed namespace definition.  */
7659                || token2.type == CPP_OPEN_BRACE
7660                || token2.keyword == RID_ATTRIBUTE))
7661     cp_parser_namespace_definition (parser);
7662   /* Objective-C++ declaration/definition.  */
7663   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7664     cp_parser_objc_declaration (parser);
7665   /* We must have either a block declaration or a function
7666      definition.  */
7667   else
7668     /* Try to parse a block-declaration, or a function-definition.  */
7669     cp_parser_block_declaration (parser, /*statement_p=*/false);
7670
7671   /* Free any declarators allocated.  */
7672   obstack_free (&declarator_obstack, p);
7673 }
7674
7675 /* Parse a block-declaration.
7676
7677    block-declaration:
7678      simple-declaration
7679      asm-definition
7680      namespace-alias-definition
7681      using-declaration
7682      using-directive
7683
7684    GNU Extension:
7685
7686    block-declaration:
7687      __extension__ block-declaration
7688      label-declaration
7689
7690    C++0x Extension:
7691
7692    block-declaration:
7693      static_assert-declaration
7694
7695    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7696    part of a declaration-statement.  */
7697
7698 static void
7699 cp_parser_block_declaration (cp_parser *parser,
7700                              bool      statement_p)
7701 {
7702   cp_token *token1;
7703   int saved_pedantic;
7704
7705   /* Check for the `__extension__' keyword.  */
7706   if (cp_parser_extension_opt (parser, &saved_pedantic))
7707     {
7708       /* Parse the qualified declaration.  */
7709       cp_parser_block_declaration (parser, statement_p);
7710       /* Restore the PEDANTIC flag.  */
7711       pedantic = saved_pedantic;
7712
7713       return;
7714     }
7715
7716   /* Peek at the next token to figure out which kind of declaration is
7717      present.  */
7718   token1 = cp_lexer_peek_token (parser->lexer);
7719
7720   /* If the next keyword is `asm', we have an asm-definition.  */
7721   if (token1->keyword == RID_ASM)
7722     {
7723       if (statement_p)
7724         cp_parser_commit_to_tentative_parse (parser);
7725       cp_parser_asm_definition (parser);
7726     }
7727   /* If the next keyword is `namespace', we have a
7728      namespace-alias-definition.  */
7729   else if (token1->keyword == RID_NAMESPACE)
7730     cp_parser_namespace_alias_definition (parser);
7731   /* If the next keyword is `using', we have either a
7732      using-declaration or a using-directive.  */
7733   else if (token1->keyword == RID_USING)
7734     {
7735       cp_token *token2;
7736
7737       if (statement_p)
7738         cp_parser_commit_to_tentative_parse (parser);
7739       /* If the token after `using' is `namespace', then we have a
7740          using-directive.  */
7741       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7742       if (token2->keyword == RID_NAMESPACE)
7743         cp_parser_using_directive (parser);
7744       /* Otherwise, it's a using-declaration.  */
7745       else
7746         cp_parser_using_declaration (parser,
7747                                      /*access_declaration_p=*/false);
7748     }
7749   /* If the next keyword is `__label__' we have a label declaration.  */
7750   else if (token1->keyword == RID_LABEL)
7751     {
7752       if (statement_p)
7753         cp_parser_commit_to_tentative_parse (parser);
7754       cp_parser_label_declaration (parser);
7755     }
7756   /* If the next token is `static_assert' we have a static assertion.  */
7757   else if (token1->keyword == RID_STATIC_ASSERT)
7758     cp_parser_static_assert (parser, /*member_p=*/false);
7759   /* Anything else must be a simple-declaration.  */
7760   else
7761     cp_parser_simple_declaration (parser, !statement_p);
7762 }
7763
7764 /* Parse a simple-declaration.
7765
7766    simple-declaration:
7767      decl-specifier-seq [opt] init-declarator-list [opt] ;
7768
7769    init-declarator-list:
7770      init-declarator
7771      init-declarator-list , init-declarator
7772
7773    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7774    function-definition as a simple-declaration.  */
7775
7776 static void
7777 cp_parser_simple_declaration (cp_parser* parser,
7778                               bool function_definition_allowed_p)
7779 {
7780   cp_decl_specifier_seq decl_specifiers;
7781   int declares_class_or_enum;
7782   bool saw_declarator;
7783
7784   /* Defer access checks until we know what is being declared; the
7785      checks for names appearing in the decl-specifier-seq should be
7786      done as if we were in the scope of the thing being declared.  */
7787   push_deferring_access_checks (dk_deferred);
7788
7789   /* Parse the decl-specifier-seq.  We have to keep track of whether
7790      or not the decl-specifier-seq declares a named class or
7791      enumeration type, since that is the only case in which the
7792      init-declarator-list is allowed to be empty.
7793
7794      [dcl.dcl]
7795
7796      In a simple-declaration, the optional init-declarator-list can be
7797      omitted only when declaring a class or enumeration, that is when
7798      the decl-specifier-seq contains either a class-specifier, an
7799      elaborated-type-specifier, or an enum-specifier.  */
7800   cp_parser_decl_specifier_seq (parser,
7801                                 CP_PARSER_FLAGS_OPTIONAL,
7802                                 &decl_specifiers,
7803                                 &declares_class_or_enum);
7804   /* We no longer need to defer access checks.  */
7805   stop_deferring_access_checks ();
7806
7807   /* In a block scope, a valid declaration must always have a
7808      decl-specifier-seq.  By not trying to parse declarators, we can
7809      resolve the declaration/expression ambiguity more quickly.  */
7810   if (!function_definition_allowed_p
7811       && !decl_specifiers.any_specifiers_p)
7812     {
7813       cp_parser_error (parser, "expected declaration");
7814       goto done;
7815     }
7816
7817   /* If the next two tokens are both identifiers, the code is
7818      erroneous. The usual cause of this situation is code like:
7819
7820        T t;
7821
7822      where "T" should name a type -- but does not.  */
7823   if (!decl_specifiers.type
7824       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7825     {
7826       /* If parsing tentatively, we should commit; we really are
7827          looking at a declaration.  */
7828       cp_parser_commit_to_tentative_parse (parser);
7829       /* Give up.  */
7830       goto done;
7831     }
7832
7833   /* If we have seen at least one decl-specifier, and the next token
7834      is not a parenthesis, then we must be looking at a declaration.
7835      (After "int (" we might be looking at a functional cast.)  */
7836   if (decl_specifiers.any_specifiers_p
7837       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7838     cp_parser_commit_to_tentative_parse (parser);
7839
7840   /* Keep going until we hit the `;' at the end of the simple
7841      declaration.  */
7842   saw_declarator = false;
7843   while (cp_lexer_next_token_is_not (parser->lexer,
7844                                      CPP_SEMICOLON))
7845     {
7846       cp_token *token;
7847       bool function_definition_p;
7848       tree decl;
7849
7850       if (saw_declarator)
7851         {
7852           /* If we are processing next declarator, coma is expected */
7853           token = cp_lexer_peek_token (parser->lexer);
7854           gcc_assert (token->type == CPP_COMMA);
7855           cp_lexer_consume_token (parser->lexer);
7856         }
7857       else
7858         saw_declarator = true;
7859
7860       /* Parse the init-declarator.  */
7861       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7862                                         /*checks=*/NULL,
7863                                         function_definition_allowed_p,
7864                                         /*member_p=*/false,
7865                                         declares_class_or_enum,
7866                                         &function_definition_p);
7867       /* If an error occurred while parsing tentatively, exit quickly.
7868          (That usually happens when in the body of a function; each
7869          statement is treated as a declaration-statement until proven
7870          otherwise.)  */
7871       if (cp_parser_error_occurred (parser))
7872         goto done;
7873       /* Handle function definitions specially.  */
7874       if (function_definition_p)
7875         {
7876           /* If the next token is a `,', then we are probably
7877              processing something like:
7878
7879                void f() {}, *p;
7880
7881              which is erroneous.  */
7882           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7883             error ("mixing declarations and function-definitions is forbidden");
7884           /* Otherwise, we're done with the list of declarators.  */
7885           else
7886             {
7887               pop_deferring_access_checks ();
7888               return;
7889             }
7890         }
7891       /* The next token should be either a `,' or a `;'.  */
7892       token = cp_lexer_peek_token (parser->lexer);
7893       /* If it's a `,', there are more declarators to come.  */
7894       if (token->type == CPP_COMMA)
7895         /* will be consumed next time around */;
7896       /* If it's a `;', we are done.  */
7897       else if (token->type == CPP_SEMICOLON)
7898         break;
7899       /* Anything else is an error.  */
7900       else
7901         {
7902           /* If we have already issued an error message we don't need
7903              to issue another one.  */
7904           if (decl != error_mark_node
7905               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7906             cp_parser_error (parser, "expected %<,%> or %<;%>");
7907           /* Skip tokens until we reach the end of the statement.  */
7908           cp_parser_skip_to_end_of_statement (parser);
7909           /* If the next token is now a `;', consume it.  */
7910           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7911             cp_lexer_consume_token (parser->lexer);
7912           goto done;
7913         }
7914       /* After the first time around, a function-definition is not
7915          allowed -- even if it was OK at first.  For example:
7916
7917            int i, f() {}
7918
7919          is not valid.  */
7920       function_definition_allowed_p = false;
7921     }
7922
7923   /* Issue an error message if no declarators are present, and the
7924      decl-specifier-seq does not itself declare a class or
7925      enumeration.  */
7926   if (!saw_declarator)
7927     {
7928       if (cp_parser_declares_only_class_p (parser))
7929         shadow_tag (&decl_specifiers);
7930       /* Perform any deferred access checks.  */
7931       perform_deferred_access_checks ();
7932     }
7933
7934   /* Consume the `;'.  */
7935   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7936
7937  done:
7938   pop_deferring_access_checks ();
7939 }
7940
7941 /* Parse a decl-specifier-seq.
7942
7943    decl-specifier-seq:
7944      decl-specifier-seq [opt] decl-specifier
7945
7946    decl-specifier:
7947      storage-class-specifier
7948      type-specifier
7949      function-specifier
7950      friend
7951      typedef
7952
7953    GNU Extension:
7954
7955    decl-specifier:
7956      attributes
7957
7958    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7959
7960    The parser flags FLAGS is used to control type-specifier parsing.
7961
7962    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7963    flags:
7964
7965      1: one of the decl-specifiers is an elaborated-type-specifier
7966         (i.e., a type declaration)
7967      2: one of the decl-specifiers is an enum-specifier or a
7968         class-specifier (i.e., a type definition)
7969
7970    */
7971
7972 static void
7973 cp_parser_decl_specifier_seq (cp_parser* parser,
7974                               cp_parser_flags flags,
7975                               cp_decl_specifier_seq *decl_specs,
7976                               int* declares_class_or_enum)
7977 {
7978   bool constructor_possible_p = !parser->in_declarator_p;
7979
7980   /* Clear DECL_SPECS.  */
7981   clear_decl_specs (decl_specs);
7982
7983   /* Assume no class or enumeration type is declared.  */
7984   *declares_class_or_enum = 0;
7985
7986   /* Keep reading specifiers until there are no more to read.  */
7987   while (true)
7988     {
7989       bool constructor_p;
7990       bool found_decl_spec;
7991       cp_token *token;
7992
7993       /* Peek at the next token.  */
7994       token = cp_lexer_peek_token (parser->lexer);
7995       /* Handle attributes.  */
7996       if (token->keyword == RID_ATTRIBUTE)
7997         {
7998           /* Parse the attributes.  */
7999           decl_specs->attributes
8000             = chainon (decl_specs->attributes,
8001                        cp_parser_attributes_opt (parser));
8002           continue;
8003         }
8004       /* Assume we will find a decl-specifier keyword.  */
8005       found_decl_spec = true;
8006       /* If the next token is an appropriate keyword, we can simply
8007          add it to the list.  */
8008       switch (token->keyword)
8009         {
8010           /* decl-specifier:
8011                friend  */
8012         case RID_FRIEND:
8013           if (!at_class_scope_p ())
8014             {
8015               error ("%<friend%> used outside of class");
8016               cp_lexer_purge_token (parser->lexer);
8017             }
8018           else
8019             {
8020               ++decl_specs->specs[(int) ds_friend];
8021               /* Consume the token.  */
8022               cp_lexer_consume_token (parser->lexer);
8023             }
8024           break;
8025
8026           /* function-specifier:
8027                inline
8028                virtual
8029                explicit  */
8030         case RID_INLINE:
8031         case RID_VIRTUAL:
8032         case RID_EXPLICIT:
8033           cp_parser_function_specifier_opt (parser, decl_specs);
8034           break;
8035
8036           /* decl-specifier:
8037                typedef  */
8038         case RID_TYPEDEF:
8039           ++decl_specs->specs[(int) ds_typedef];
8040           /* Consume the token.  */
8041           cp_lexer_consume_token (parser->lexer);
8042           /* A constructor declarator cannot appear in a typedef.  */
8043           constructor_possible_p = false;
8044           /* The "typedef" keyword can only occur in a declaration; we
8045              may as well commit at this point.  */
8046           cp_parser_commit_to_tentative_parse (parser);
8047
8048           if (decl_specs->storage_class != sc_none)
8049             decl_specs->conflicting_specifiers_p = true;
8050           break;
8051
8052           /* storage-class-specifier:
8053                auto
8054                register
8055                static
8056                extern
8057                mutable
8058
8059              GNU Extension:
8060                thread  */
8061         case RID_AUTO:
8062         case RID_REGISTER:
8063         case RID_STATIC:
8064         case RID_EXTERN:
8065         case RID_MUTABLE:
8066           /* Consume the token.  */
8067           cp_lexer_consume_token (parser->lexer);
8068           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8069           break;
8070         case RID_THREAD:
8071           /* Consume the token.  */
8072           cp_lexer_consume_token (parser->lexer);
8073           ++decl_specs->specs[(int) ds_thread];
8074           break;
8075
8076         default:
8077           /* We did not yet find a decl-specifier yet.  */
8078           found_decl_spec = false;
8079           break;
8080         }
8081
8082       /* Constructors are a special case.  The `S' in `S()' is not a
8083          decl-specifier; it is the beginning of the declarator.  */
8084       constructor_p
8085         = (!found_decl_spec
8086            && constructor_possible_p
8087            && (cp_parser_constructor_declarator_p
8088                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8089
8090       /* If we don't have a DECL_SPEC yet, then we must be looking at
8091          a type-specifier.  */
8092       if (!found_decl_spec && !constructor_p)
8093         {
8094           int decl_spec_declares_class_or_enum;
8095           bool is_cv_qualifier;
8096           tree type_spec;
8097
8098           type_spec
8099             = cp_parser_type_specifier (parser, flags,
8100                                         decl_specs,
8101                                         /*is_declaration=*/true,
8102                                         &decl_spec_declares_class_or_enum,
8103                                         &is_cv_qualifier);
8104
8105           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8106
8107           /* If this type-specifier referenced a user-defined type
8108              (a typedef, class-name, etc.), then we can't allow any
8109              more such type-specifiers henceforth.
8110
8111              [dcl.spec]
8112
8113              The longest sequence of decl-specifiers that could
8114              possibly be a type name is taken as the
8115              decl-specifier-seq of a declaration.  The sequence shall
8116              be self-consistent as described below.
8117
8118              [dcl.type]
8119
8120              As a general rule, at most one type-specifier is allowed
8121              in the complete decl-specifier-seq of a declaration.  The
8122              only exceptions are the following:
8123
8124              -- const or volatile can be combined with any other
8125                 type-specifier.
8126
8127              -- signed or unsigned can be combined with char, long,
8128                 short, or int.
8129
8130              -- ..
8131
8132              Example:
8133
8134                typedef char* Pc;
8135                void g (const int Pc);
8136
8137              Here, Pc is *not* part of the decl-specifier seq; it's
8138              the declarator.  Therefore, once we see a type-specifier
8139              (other than a cv-qualifier), we forbid any additional
8140              user-defined types.  We *do* still allow things like `int
8141              int' to be considered a decl-specifier-seq, and issue the
8142              error message later.  */
8143           if (type_spec && !is_cv_qualifier)
8144             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8145           /* A constructor declarator cannot follow a type-specifier.  */
8146           if (type_spec)
8147             {
8148               constructor_possible_p = false;
8149               found_decl_spec = true;
8150             }
8151         }
8152
8153       /* If we still do not have a DECL_SPEC, then there are no more
8154          decl-specifiers.  */
8155       if (!found_decl_spec)
8156         break;
8157
8158       decl_specs->any_specifiers_p = true;
8159       /* After we see one decl-specifier, further decl-specifiers are
8160          always optional.  */
8161       flags |= CP_PARSER_FLAGS_OPTIONAL;
8162     }
8163
8164   cp_parser_check_decl_spec (decl_specs);
8165
8166   /* Don't allow a friend specifier with a class definition.  */
8167   if (decl_specs->specs[(int) ds_friend] != 0
8168       && (*declares_class_or_enum & 2))
8169     error ("class definition may not be declared a friend");
8170 }
8171
8172 /* Parse an (optional) storage-class-specifier.
8173
8174    storage-class-specifier:
8175      auto
8176      register
8177      static
8178      extern
8179      mutable
8180
8181    GNU Extension:
8182
8183    storage-class-specifier:
8184      thread
8185
8186    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8187
8188 static tree
8189 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8190 {
8191   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8192     {
8193     case RID_AUTO:
8194     case RID_REGISTER:
8195     case RID_STATIC:
8196     case RID_EXTERN:
8197     case RID_MUTABLE:
8198     case RID_THREAD:
8199       /* Consume the token.  */
8200       return cp_lexer_consume_token (parser->lexer)->u.value;
8201
8202     default:
8203       return NULL_TREE;
8204     }
8205 }
8206
8207 /* Parse an (optional) function-specifier.
8208
8209    function-specifier:
8210      inline
8211      virtual
8212      explicit
8213
8214    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8215    Updates DECL_SPECS, if it is non-NULL.  */
8216
8217 static tree
8218 cp_parser_function_specifier_opt (cp_parser* parser,
8219                                   cp_decl_specifier_seq *decl_specs)
8220 {
8221   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8222     {
8223     case RID_INLINE:
8224       if (decl_specs)
8225         ++decl_specs->specs[(int) ds_inline];
8226       break;
8227
8228     case RID_VIRTUAL:
8229       /* 14.5.2.3 [temp.mem]
8230
8231          A member function template shall not be virtual.  */
8232       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8233         error ("templates may not be %<virtual%>");
8234       else if (decl_specs)
8235         ++decl_specs->specs[(int) ds_virtual];
8236       break;
8237
8238     case RID_EXPLICIT:
8239       if (decl_specs)
8240         ++decl_specs->specs[(int) ds_explicit];
8241       break;
8242
8243     default:
8244       return NULL_TREE;
8245     }
8246
8247   /* Consume the token.  */
8248   return cp_lexer_consume_token (parser->lexer)->u.value;
8249 }
8250
8251 /* Parse a linkage-specification.
8252
8253    linkage-specification:
8254      extern string-literal { declaration-seq [opt] }
8255      extern string-literal declaration  */
8256
8257 static void
8258 cp_parser_linkage_specification (cp_parser* parser)
8259 {
8260   tree linkage;
8261
8262   /* Look for the `extern' keyword.  */
8263   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8264
8265   /* Look for the string-literal.  */
8266   linkage = cp_parser_string_literal (parser, false, false);
8267
8268   /* Transform the literal into an identifier.  If the literal is a
8269      wide-character string, or contains embedded NULs, then we can't
8270      handle it as the user wants.  */
8271   if (strlen (TREE_STRING_POINTER (linkage))
8272       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8273     {
8274       cp_parser_error (parser, "invalid linkage-specification");
8275       /* Assume C++ linkage.  */
8276       linkage = lang_name_cplusplus;
8277     }
8278   else
8279     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8280
8281   /* We're now using the new linkage.  */
8282   push_lang_context (linkage);
8283
8284   /* If the next token is a `{', then we're using the first
8285      production.  */
8286   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8287     {
8288       /* Consume the `{' token.  */
8289       cp_lexer_consume_token (parser->lexer);
8290       /* Parse the declarations.  */
8291       cp_parser_declaration_seq_opt (parser);
8292       /* Look for the closing `}'.  */
8293       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8294     }
8295   /* Otherwise, there's just one declaration.  */
8296   else
8297     {
8298       bool saved_in_unbraced_linkage_specification_p;
8299
8300       saved_in_unbraced_linkage_specification_p
8301         = parser->in_unbraced_linkage_specification_p;
8302       parser->in_unbraced_linkage_specification_p = true;
8303       cp_parser_declaration (parser);
8304       parser->in_unbraced_linkage_specification_p
8305         = saved_in_unbraced_linkage_specification_p;
8306     }
8307
8308   /* We're done with the linkage-specification.  */
8309   pop_lang_context ();
8310 }
8311
8312 /* Parse a static_assert-declaration.
8313
8314    static_assert-declaration:
8315      static_assert ( constant-expression , string-literal ) ; 
8316
8317    If MEMBER_P, this static_assert is a class member.  */
8318
8319 static void 
8320 cp_parser_static_assert(cp_parser *parser, bool member_p)
8321 {
8322   tree condition;
8323   tree message;
8324   cp_token *token;
8325   location_t saved_loc;
8326
8327   /* Peek at the `static_assert' token so we can keep track of exactly
8328      where the static assertion started.  */
8329   token = cp_lexer_peek_token (parser->lexer);
8330   saved_loc = token->location;
8331
8332   /* Look for the `static_assert' keyword.  */
8333   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8334                                   "`static_assert'"))
8335     return;
8336
8337   /*  We know we are in a static assertion; commit to any tentative
8338       parse.  */
8339   if (cp_parser_parsing_tentatively (parser))
8340     cp_parser_commit_to_tentative_parse (parser);
8341
8342   /* Parse the `(' starting the static assertion condition.  */
8343   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8344
8345   /* Parse the constant-expression.  */
8346   condition = 
8347     cp_parser_constant_expression (parser,
8348                                    /*allow_non_constant_p=*/false,
8349                                    /*non_constant_p=*/NULL);
8350
8351   /* Parse the separating `,'.  */
8352   cp_parser_require (parser, CPP_COMMA, "`,'");
8353
8354   /* Parse the string-literal message.  */
8355   message = cp_parser_string_literal (parser, 
8356                                       /*translate=*/false,
8357                                       /*wide_ok=*/true);
8358
8359   /* A `)' completes the static assertion.  */
8360   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8361     cp_parser_skip_to_closing_parenthesis (parser, 
8362                                            /*recovering=*/true, 
8363                                            /*or_comma=*/false,
8364                                            /*consume_paren=*/true);
8365
8366   /* A semicolon terminates the declaration.  */
8367   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8368
8369   /* Complete the static assertion, which may mean either processing 
8370      the static assert now or saving it for template instantiation.  */
8371   finish_static_assert (condition, message, saved_loc, member_p);
8372 }
8373
8374 /* Special member functions [gram.special] */
8375
8376 /* Parse a conversion-function-id.
8377
8378    conversion-function-id:
8379      operator conversion-type-id
8380
8381    Returns an IDENTIFIER_NODE representing the operator.  */
8382
8383 static tree
8384 cp_parser_conversion_function_id (cp_parser* parser)
8385 {
8386   tree type;
8387   tree saved_scope;
8388   tree saved_qualifying_scope;
8389   tree saved_object_scope;
8390   tree pushed_scope = NULL_TREE;
8391
8392   /* Look for the `operator' token.  */
8393   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8394     return error_mark_node;
8395   /* When we parse the conversion-type-id, the current scope will be
8396      reset.  However, we need that information in able to look up the
8397      conversion function later, so we save it here.  */
8398   saved_scope = parser->scope;
8399   saved_qualifying_scope = parser->qualifying_scope;
8400   saved_object_scope = parser->object_scope;
8401   /* We must enter the scope of the class so that the names of
8402      entities declared within the class are available in the
8403      conversion-type-id.  For example, consider:
8404
8405        struct S {
8406          typedef int I;
8407          operator I();
8408        };
8409
8410        S::operator I() { ... }
8411
8412      In order to see that `I' is a type-name in the definition, we
8413      must be in the scope of `S'.  */
8414   if (saved_scope)
8415     pushed_scope = push_scope (saved_scope);
8416   /* Parse the conversion-type-id.  */
8417   type = cp_parser_conversion_type_id (parser);
8418   /* Leave the scope of the class, if any.  */
8419   if (pushed_scope)
8420     pop_scope (pushed_scope);
8421   /* Restore the saved scope.  */
8422   parser->scope = saved_scope;
8423   parser->qualifying_scope = saved_qualifying_scope;
8424   parser->object_scope = saved_object_scope;
8425   /* If the TYPE is invalid, indicate failure.  */
8426   if (type == error_mark_node)
8427     return error_mark_node;
8428   return mangle_conv_op_name_for_type (type);
8429 }
8430
8431 /* Parse a conversion-type-id:
8432
8433    conversion-type-id:
8434      type-specifier-seq conversion-declarator [opt]
8435
8436    Returns the TYPE specified.  */
8437
8438 static tree
8439 cp_parser_conversion_type_id (cp_parser* parser)
8440 {
8441   tree attributes;
8442   cp_decl_specifier_seq type_specifiers;
8443   cp_declarator *declarator;
8444   tree type_specified;
8445
8446   /* Parse the attributes.  */
8447   attributes = cp_parser_attributes_opt (parser);
8448   /* Parse the type-specifiers.  */
8449   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8450                                 &type_specifiers);
8451   /* If that didn't work, stop.  */
8452   if (type_specifiers.type == error_mark_node)
8453     return error_mark_node;
8454   /* Parse the conversion-declarator.  */
8455   declarator = cp_parser_conversion_declarator_opt (parser);
8456
8457   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8458                                     /*initialized=*/0, &attributes);
8459   if (attributes)
8460     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8461   return type_specified;
8462 }
8463
8464 /* Parse an (optional) conversion-declarator.
8465
8466    conversion-declarator:
8467      ptr-operator conversion-declarator [opt]
8468
8469    */
8470
8471 static cp_declarator *
8472 cp_parser_conversion_declarator_opt (cp_parser* parser)
8473 {
8474   enum tree_code code;
8475   tree class_type;
8476   cp_cv_quals cv_quals;
8477
8478   /* We don't know if there's a ptr-operator next, or not.  */
8479   cp_parser_parse_tentatively (parser);
8480   /* Try the ptr-operator.  */
8481   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8482   /* If it worked, look for more conversion-declarators.  */
8483   if (cp_parser_parse_definitely (parser))
8484     {
8485       cp_declarator *declarator;
8486
8487       /* Parse another optional declarator.  */
8488       declarator = cp_parser_conversion_declarator_opt (parser);
8489
8490       return cp_parser_make_indirect_declarator
8491         (code, class_type, cv_quals, declarator);
8492    }
8493
8494   return NULL;
8495 }
8496
8497 /* Parse an (optional) ctor-initializer.
8498
8499    ctor-initializer:
8500      : mem-initializer-list
8501
8502    Returns TRUE iff the ctor-initializer was actually present.  */
8503
8504 static bool
8505 cp_parser_ctor_initializer_opt (cp_parser* parser)
8506 {
8507   /* If the next token is not a `:', then there is no
8508      ctor-initializer.  */
8509   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8510     {
8511       /* Do default initialization of any bases and members.  */
8512       if (DECL_CONSTRUCTOR_P (current_function_decl))
8513         finish_mem_initializers (NULL_TREE);
8514
8515       return false;
8516     }
8517
8518   /* Consume the `:' token.  */
8519   cp_lexer_consume_token (parser->lexer);
8520   /* And the mem-initializer-list.  */
8521   cp_parser_mem_initializer_list (parser);
8522
8523   return true;
8524 }
8525
8526 /* Parse a mem-initializer-list.
8527
8528    mem-initializer-list:
8529      mem-initializer ... [opt]
8530      mem-initializer ... [opt] , mem-initializer-list  */
8531
8532 static void
8533 cp_parser_mem_initializer_list (cp_parser* parser)
8534 {
8535   tree mem_initializer_list = NULL_TREE;
8536
8537   /* Let the semantic analysis code know that we are starting the
8538      mem-initializer-list.  */
8539   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8540     error ("only constructors take base initializers");
8541
8542   /* Loop through the list.  */
8543   while (true)
8544     {
8545       tree mem_initializer;
8546
8547       /* Parse the mem-initializer.  */
8548       mem_initializer = cp_parser_mem_initializer (parser);
8549       /* If the next token is a `...', we're expanding member initializers. */
8550       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8551         {
8552           /* Consume the `...'. */
8553           cp_lexer_consume_token (parser->lexer);
8554
8555           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8556              can be expanded but members cannot. */
8557           if (mem_initializer != error_mark_node
8558               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8559             {
8560               error ("cannot expand initializer for member %<%D%>", 
8561                      TREE_PURPOSE (mem_initializer));
8562               mem_initializer = error_mark_node;
8563             }
8564
8565           /* Construct the pack expansion type. */
8566           if (mem_initializer != error_mark_node)
8567             mem_initializer = make_pack_expansion (mem_initializer);
8568         }
8569       /* Add it to the list, unless it was erroneous.  */
8570       if (mem_initializer != error_mark_node)
8571         {
8572           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8573           mem_initializer_list = mem_initializer;
8574         }
8575       /* If the next token is not a `,', we're done.  */
8576       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8577         break;
8578       /* Consume the `,' token.  */
8579       cp_lexer_consume_token (parser->lexer);
8580     }
8581
8582   /* Perform semantic analysis.  */
8583   if (DECL_CONSTRUCTOR_P (current_function_decl))
8584     finish_mem_initializers (mem_initializer_list);
8585 }
8586
8587 /* Parse a mem-initializer.
8588
8589    mem-initializer:
8590      mem-initializer-id ( expression-list [opt] )
8591
8592    GNU extension:
8593
8594    mem-initializer:
8595      ( expression-list [opt] )
8596
8597    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8598    class) or FIELD_DECL (for a non-static data member) to initialize;
8599    the TREE_VALUE is the expression-list.  An empty initialization
8600    list is represented by void_list_node.  */
8601
8602 static tree
8603 cp_parser_mem_initializer (cp_parser* parser)
8604 {
8605   tree mem_initializer_id;
8606   tree expression_list;
8607   tree member;
8608
8609   /* Find out what is being initialized.  */
8610   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8611     {
8612       pedwarn ("anachronistic old-style base class initializer");
8613       mem_initializer_id = NULL_TREE;
8614     }
8615   else
8616     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8617   member = expand_member_init (mem_initializer_id);
8618   if (member && !DECL_P (member))
8619     in_base_initializer = 1;
8620
8621   expression_list
8622     = cp_parser_parenthesized_expression_list (parser, false,
8623                                                /*cast_p=*/false,
8624                                                /*allow_expansion_p=*/true,
8625                                                /*non_constant_p=*/NULL);
8626   if (expression_list == error_mark_node)
8627     return error_mark_node;
8628   if (!expression_list)
8629     expression_list = void_type_node;
8630
8631   in_base_initializer = 0;
8632
8633   return member ? build_tree_list (member, expression_list) : error_mark_node;
8634 }
8635
8636 /* Parse a mem-initializer-id.
8637
8638    mem-initializer-id:
8639      :: [opt] nested-name-specifier [opt] class-name
8640      identifier
8641
8642    Returns a TYPE indicating the class to be initializer for the first
8643    production.  Returns an IDENTIFIER_NODE indicating the data member
8644    to be initialized for the second production.  */
8645
8646 static tree
8647 cp_parser_mem_initializer_id (cp_parser* parser)
8648 {
8649   bool global_scope_p;
8650   bool nested_name_specifier_p;
8651   bool template_p = false;
8652   tree id;
8653
8654   /* `typename' is not allowed in this context ([temp.res]).  */
8655   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8656     {
8657       error ("keyword %<typename%> not allowed in this context (a qualified "
8658              "member initializer is implicitly a type)");
8659       cp_lexer_consume_token (parser->lexer);
8660     }
8661   /* Look for the optional `::' operator.  */
8662   global_scope_p
8663     = (cp_parser_global_scope_opt (parser,
8664                                    /*current_scope_valid_p=*/false)
8665        != NULL_TREE);
8666   /* Look for the optional nested-name-specifier.  The simplest way to
8667      implement:
8668
8669        [temp.res]
8670
8671        The keyword `typename' is not permitted in a base-specifier or
8672        mem-initializer; in these contexts a qualified name that
8673        depends on a template-parameter is implicitly assumed to be a
8674        type name.
8675
8676      is to assume that we have seen the `typename' keyword at this
8677      point.  */
8678   nested_name_specifier_p
8679     = (cp_parser_nested_name_specifier_opt (parser,
8680                                             /*typename_keyword_p=*/true,
8681                                             /*check_dependency_p=*/true,
8682                                             /*type_p=*/true,
8683                                             /*is_declaration=*/true)
8684        != NULL_TREE);
8685   if (nested_name_specifier_p)
8686     template_p = cp_parser_optional_template_keyword (parser);
8687   /* If there is a `::' operator or a nested-name-specifier, then we
8688      are definitely looking for a class-name.  */
8689   if (global_scope_p || nested_name_specifier_p)
8690     return cp_parser_class_name (parser,
8691                                  /*typename_keyword_p=*/true,
8692                                  /*template_keyword_p=*/template_p,
8693                                  none_type,
8694                                  /*check_dependency_p=*/true,
8695                                  /*class_head_p=*/false,
8696                                  /*is_declaration=*/true);
8697   /* Otherwise, we could also be looking for an ordinary identifier.  */
8698   cp_parser_parse_tentatively (parser);
8699   /* Try a class-name.  */
8700   id = cp_parser_class_name (parser,
8701                              /*typename_keyword_p=*/true,
8702                              /*template_keyword_p=*/false,
8703                              none_type,
8704                              /*check_dependency_p=*/true,
8705                              /*class_head_p=*/false,
8706                              /*is_declaration=*/true);
8707   /* If we found one, we're done.  */
8708   if (cp_parser_parse_definitely (parser))
8709     return id;
8710   /* Otherwise, look for an ordinary identifier.  */
8711   return cp_parser_identifier (parser);
8712 }
8713
8714 /* Overloading [gram.over] */
8715
8716 /* Parse an operator-function-id.
8717
8718    operator-function-id:
8719      operator operator
8720
8721    Returns an IDENTIFIER_NODE for the operator which is a
8722    human-readable spelling of the identifier, e.g., `operator +'.  */
8723
8724 static tree
8725 cp_parser_operator_function_id (cp_parser* parser)
8726 {
8727   /* Look for the `operator' keyword.  */
8728   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8729     return error_mark_node;
8730   /* And then the name of the operator itself.  */
8731   return cp_parser_operator (parser);
8732 }
8733
8734 /* Parse an operator.
8735
8736    operator:
8737      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8738      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8739      || ++ -- , ->* -> () []
8740
8741    GNU Extensions:
8742
8743    operator:
8744      <? >? <?= >?=
8745
8746    Returns an IDENTIFIER_NODE for the operator which is a
8747    human-readable spelling of the identifier, e.g., `operator +'.  */
8748
8749 static tree
8750 cp_parser_operator (cp_parser* parser)
8751 {
8752   tree id = NULL_TREE;
8753   cp_token *token;
8754
8755   /* Peek at the next token.  */
8756   token = cp_lexer_peek_token (parser->lexer);
8757   /* Figure out which operator we have.  */
8758   switch (token->type)
8759     {
8760     case CPP_KEYWORD:
8761       {
8762         enum tree_code op;
8763
8764         /* The keyword should be either `new' or `delete'.  */
8765         if (token->keyword == RID_NEW)
8766           op = NEW_EXPR;
8767         else if (token->keyword == RID_DELETE)
8768           op = DELETE_EXPR;
8769         else
8770           break;
8771
8772         /* Consume the `new' or `delete' token.  */
8773         cp_lexer_consume_token (parser->lexer);
8774
8775         /* Peek at the next token.  */
8776         token = cp_lexer_peek_token (parser->lexer);
8777         /* If it's a `[' token then this is the array variant of the
8778            operator.  */
8779         if (token->type == CPP_OPEN_SQUARE)
8780           {
8781             /* Consume the `[' token.  */
8782             cp_lexer_consume_token (parser->lexer);
8783             /* Look for the `]' token.  */
8784             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8785             id = ansi_opname (op == NEW_EXPR
8786                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8787           }
8788         /* Otherwise, we have the non-array variant.  */
8789         else
8790           id = ansi_opname (op);
8791
8792         return id;
8793       }
8794
8795     case CPP_PLUS:
8796       id = ansi_opname (PLUS_EXPR);
8797       break;
8798
8799     case CPP_MINUS:
8800       id = ansi_opname (MINUS_EXPR);
8801       break;
8802
8803     case CPP_MULT:
8804       id = ansi_opname (MULT_EXPR);
8805       break;
8806
8807     case CPP_DIV:
8808       id = ansi_opname (TRUNC_DIV_EXPR);
8809       break;
8810
8811     case CPP_MOD:
8812       id = ansi_opname (TRUNC_MOD_EXPR);
8813       break;
8814
8815     case CPP_XOR:
8816       id = ansi_opname (BIT_XOR_EXPR);
8817       break;
8818
8819     case CPP_AND:
8820       id = ansi_opname (BIT_AND_EXPR);
8821       break;
8822
8823     case CPP_OR:
8824       id = ansi_opname (BIT_IOR_EXPR);
8825       break;
8826
8827     case CPP_COMPL:
8828       id = ansi_opname (BIT_NOT_EXPR);
8829       break;
8830
8831     case CPP_NOT:
8832       id = ansi_opname (TRUTH_NOT_EXPR);
8833       break;
8834
8835     case CPP_EQ:
8836       id = ansi_assopname (NOP_EXPR);
8837       break;
8838
8839     case CPP_LESS:
8840       id = ansi_opname (LT_EXPR);
8841       break;
8842
8843     case CPP_GREATER:
8844       id = ansi_opname (GT_EXPR);
8845       break;
8846
8847     case CPP_PLUS_EQ:
8848       id = ansi_assopname (PLUS_EXPR);
8849       break;
8850
8851     case CPP_MINUS_EQ:
8852       id = ansi_assopname (MINUS_EXPR);
8853       break;
8854
8855     case CPP_MULT_EQ:
8856       id = ansi_assopname (MULT_EXPR);
8857       break;
8858
8859     case CPP_DIV_EQ:
8860       id = ansi_assopname (TRUNC_DIV_EXPR);
8861       break;
8862
8863     case CPP_MOD_EQ:
8864       id = ansi_assopname (TRUNC_MOD_EXPR);
8865       break;
8866
8867     case CPP_XOR_EQ:
8868       id = ansi_assopname (BIT_XOR_EXPR);
8869       break;
8870
8871     case CPP_AND_EQ:
8872       id = ansi_assopname (BIT_AND_EXPR);
8873       break;
8874
8875     case CPP_OR_EQ:
8876       id = ansi_assopname (BIT_IOR_EXPR);
8877       break;
8878
8879     case CPP_LSHIFT:
8880       id = ansi_opname (LSHIFT_EXPR);
8881       break;
8882
8883     case CPP_RSHIFT:
8884       id = ansi_opname (RSHIFT_EXPR);
8885       break;
8886
8887     case CPP_LSHIFT_EQ:
8888       id = ansi_assopname (LSHIFT_EXPR);
8889       break;
8890
8891     case CPP_RSHIFT_EQ:
8892       id = ansi_assopname (RSHIFT_EXPR);
8893       break;
8894
8895     case CPP_EQ_EQ:
8896       id = ansi_opname (EQ_EXPR);
8897       break;
8898
8899     case CPP_NOT_EQ:
8900       id = ansi_opname (NE_EXPR);
8901       break;
8902
8903     case CPP_LESS_EQ:
8904       id = ansi_opname (LE_EXPR);
8905       break;
8906
8907     case CPP_GREATER_EQ:
8908       id = ansi_opname (GE_EXPR);
8909       break;
8910
8911     case CPP_AND_AND:
8912       id = ansi_opname (TRUTH_ANDIF_EXPR);
8913       break;
8914
8915     case CPP_OR_OR:
8916       id = ansi_opname (TRUTH_ORIF_EXPR);
8917       break;
8918
8919     case CPP_PLUS_PLUS:
8920       id = ansi_opname (POSTINCREMENT_EXPR);
8921       break;
8922
8923     case CPP_MINUS_MINUS:
8924       id = ansi_opname (PREDECREMENT_EXPR);
8925       break;
8926
8927     case CPP_COMMA:
8928       id = ansi_opname (COMPOUND_EXPR);
8929       break;
8930
8931     case CPP_DEREF_STAR:
8932       id = ansi_opname (MEMBER_REF);
8933       break;
8934
8935     case CPP_DEREF:
8936       id = ansi_opname (COMPONENT_REF);
8937       break;
8938
8939     case CPP_OPEN_PAREN:
8940       /* Consume the `('.  */
8941       cp_lexer_consume_token (parser->lexer);
8942       /* Look for the matching `)'.  */
8943       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8944       return ansi_opname (CALL_EXPR);
8945
8946     case CPP_OPEN_SQUARE:
8947       /* Consume the `['.  */
8948       cp_lexer_consume_token (parser->lexer);
8949       /* Look for the matching `]'.  */
8950       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8951       return ansi_opname (ARRAY_REF);
8952
8953     default:
8954       /* Anything else is an error.  */
8955       break;
8956     }
8957
8958   /* If we have selected an identifier, we need to consume the
8959      operator token.  */
8960   if (id)
8961     cp_lexer_consume_token (parser->lexer);
8962   /* Otherwise, no valid operator name was present.  */
8963   else
8964     {
8965       cp_parser_error (parser, "expected operator");
8966       id = error_mark_node;
8967     }
8968
8969   return id;
8970 }
8971
8972 /* Parse a template-declaration.
8973
8974    template-declaration:
8975      export [opt] template < template-parameter-list > declaration
8976
8977    If MEMBER_P is TRUE, this template-declaration occurs within a
8978    class-specifier.
8979
8980    The grammar rule given by the standard isn't correct.  What
8981    is really meant is:
8982
8983    template-declaration:
8984      export [opt] template-parameter-list-seq
8985        decl-specifier-seq [opt] init-declarator [opt] ;
8986      export [opt] template-parameter-list-seq
8987        function-definition
8988
8989    template-parameter-list-seq:
8990      template-parameter-list-seq [opt]
8991      template < template-parameter-list >  */
8992
8993 static void
8994 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8995 {
8996   /* Check for `export'.  */
8997   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8998     {
8999       /* Consume the `export' token.  */
9000       cp_lexer_consume_token (parser->lexer);
9001       /* Warn that we do not support `export'.  */
9002       warning (0, "keyword %<export%> not implemented, and will be ignored");
9003     }
9004
9005   cp_parser_template_declaration_after_export (parser, member_p);
9006 }
9007
9008 /* Parse a template-parameter-list.
9009
9010    template-parameter-list:
9011      template-parameter
9012      template-parameter-list , template-parameter
9013
9014    Returns a TREE_LIST.  Each node represents a template parameter.
9015    The nodes are connected via their TREE_CHAINs.  */
9016
9017 static tree
9018 cp_parser_template_parameter_list (cp_parser* parser)
9019 {
9020   tree parameter_list = NULL_TREE;
9021
9022   begin_template_parm_list ();
9023   while (true)
9024     {
9025       tree parameter;
9026       cp_token *token;
9027       bool is_non_type;
9028       bool is_parameter_pack;
9029
9030       /* Parse the template-parameter.  */
9031       parameter = cp_parser_template_parameter (parser, 
9032                                                 &is_non_type,
9033                                                 &is_parameter_pack);
9034       /* Add it to the list.  */
9035       if (parameter != error_mark_node)
9036         parameter_list = process_template_parm (parameter_list,
9037                                                 parameter,
9038                                                 is_non_type,
9039                                                 is_parameter_pack);
9040       else
9041        {
9042          tree err_parm = build_tree_list (parameter, parameter);
9043          TREE_VALUE (err_parm) = error_mark_node;
9044          parameter_list = chainon (parameter_list, err_parm);
9045        }
9046
9047       /* Peek at the next token.  */
9048       token = cp_lexer_peek_token (parser->lexer);
9049       /* If it's not a `,', we're done.  */
9050       if (token->type != CPP_COMMA)
9051         break;
9052       /* Otherwise, consume the `,' token.  */
9053       cp_lexer_consume_token (parser->lexer);
9054     }
9055
9056   return end_template_parm_list (parameter_list);
9057 }
9058
9059 /* Parse a template-parameter.
9060
9061    template-parameter:
9062      type-parameter
9063      parameter-declaration
9064
9065    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9066    the parameter.  The TREE_PURPOSE is the default value, if any.
9067    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9068    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9069    set to true iff this parameter is a parameter pack. */
9070
9071 static tree
9072 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9073                               bool *is_parameter_pack)
9074 {
9075   cp_token *token;
9076   cp_parameter_declarator *parameter_declarator;
9077   tree parm;
9078
9079   /* Assume it is a type parameter or a template parameter.  */
9080   *is_non_type = false;
9081   /* Assume it not a parameter pack. */
9082   *is_parameter_pack = false;
9083   /* Peek at the next token.  */
9084   token = cp_lexer_peek_token (parser->lexer);
9085   /* If it is `class' or `template', we have a type-parameter.  */
9086   if (token->keyword == RID_TEMPLATE)
9087     return cp_parser_type_parameter (parser, is_parameter_pack);
9088   /* If it is `class' or `typename' we do not know yet whether it is a
9089      type parameter or a non-type parameter.  Consider:
9090
9091        template <typename T, typename T::X X> ...
9092
9093      or:
9094
9095        template <class C, class D*> ...
9096
9097      Here, the first parameter is a type parameter, and the second is
9098      a non-type parameter.  We can tell by looking at the token after
9099      the identifier -- if it is a `,', `=', or `>' then we have a type
9100      parameter.  */
9101   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9102     {
9103       /* Peek at the token after `class' or `typename'.  */
9104       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9105       /* If it's an ellipsis, we have a template type parameter
9106          pack. */
9107       if (token->type == CPP_ELLIPSIS)
9108         return cp_parser_type_parameter (parser, is_parameter_pack);
9109       /* If it's an identifier, skip it.  */
9110       if (token->type == CPP_NAME)
9111         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9112       /* Now, see if the token looks like the end of a template
9113          parameter.  */
9114       if (token->type == CPP_COMMA
9115           || token->type == CPP_EQ
9116           || token->type == CPP_GREATER)
9117         return cp_parser_type_parameter (parser, is_parameter_pack);
9118     }
9119
9120   /* Otherwise, it is a non-type parameter.
9121
9122      [temp.param]
9123
9124      When parsing a default template-argument for a non-type
9125      template-parameter, the first non-nested `>' is taken as the end
9126      of the template parameter-list rather than a greater-than
9127      operator.  */
9128   *is_non_type = true;
9129   parameter_declarator
9130      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9131                                         /*parenthesized_p=*/NULL);
9132
9133   /* If the parameter declaration is marked as a parameter pack, set
9134      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9135      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9136      grokdeclarator. */
9137   if (parameter_declarator
9138       && parameter_declarator->declarator
9139       && parameter_declarator->declarator->parameter_pack_p)
9140     {
9141       *is_parameter_pack = true;
9142       parameter_declarator->declarator->parameter_pack_p = false;
9143     }
9144
9145   /* If the next token is an ellipsis, and we don't already have it
9146      marked as a parameter pack, then we have a parameter pack (that
9147      has no declarator); */
9148   if (!*is_parameter_pack
9149       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9150       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9151     {
9152       /* Consume the `...'. */
9153       cp_lexer_consume_token (parser->lexer);
9154       maybe_warn_variadic_templates ();
9155       
9156       *is_parameter_pack = true;
9157     }
9158
9159   parm = grokdeclarator (parameter_declarator->declarator,
9160                          &parameter_declarator->decl_specifiers,
9161                          PARM, /*initialized=*/0,
9162                          /*attrlist=*/NULL);
9163   if (parm == error_mark_node)
9164     return error_mark_node;
9165
9166   return build_tree_list (parameter_declarator->default_argument, parm);
9167 }
9168
9169 /* Parse a type-parameter.
9170
9171    type-parameter:
9172      class identifier [opt]
9173      class identifier [opt] = type-id
9174      typename identifier [opt]
9175      typename identifier [opt] = type-id
9176      template < template-parameter-list > class identifier [opt]
9177      template < template-parameter-list > class identifier [opt]
9178        = id-expression
9179
9180    GNU Extension (variadic templates):
9181
9182    type-parameter:
9183      class ... identifier [opt]
9184      typename ... identifier [opt]
9185
9186    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9187    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9188    the declaration of the parameter.
9189
9190    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9191
9192 static tree
9193 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9194 {
9195   cp_token *token;
9196   tree parameter;
9197
9198   /* Look for a keyword to tell us what kind of parameter this is.  */
9199   token = cp_parser_require (parser, CPP_KEYWORD,
9200                              "`class', `typename', or `template'");
9201   if (!token)
9202     return error_mark_node;
9203
9204   switch (token->keyword)
9205     {
9206     case RID_CLASS:
9207     case RID_TYPENAME:
9208       {
9209         tree identifier;
9210         tree default_argument;
9211
9212         /* If the next token is an ellipsis, we have a template
9213            argument pack. */
9214         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9215           {
9216             /* Consume the `...' token. */
9217             cp_lexer_consume_token (parser->lexer);
9218             maybe_warn_variadic_templates ();
9219
9220             *is_parameter_pack = true;
9221           }
9222
9223         /* If the next token is an identifier, then it names the
9224            parameter.  */
9225         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9226           identifier = cp_parser_identifier (parser);
9227         else
9228           identifier = NULL_TREE;
9229
9230         /* Create the parameter.  */
9231         parameter = finish_template_type_parm (class_type_node, identifier);
9232
9233         /* If the next token is an `=', we have a default argument.  */
9234         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9235           {
9236             /* Consume the `=' token.  */
9237             cp_lexer_consume_token (parser->lexer);
9238             /* Parse the default-argument.  */
9239             push_deferring_access_checks (dk_no_deferred);
9240             default_argument = cp_parser_type_id (parser);
9241
9242             /* Template parameter packs cannot have default
9243                arguments. */
9244             if (*is_parameter_pack)
9245               {
9246                 if (identifier)
9247                   error ("template parameter pack %qD cannot have a default argument", 
9248                          identifier);
9249                 else
9250                   error ("template parameter packs cannot have default arguments");
9251                 default_argument = NULL_TREE;
9252               }
9253             pop_deferring_access_checks ();
9254           }
9255         else
9256           default_argument = NULL_TREE;
9257
9258         /* Create the combined representation of the parameter and the
9259            default argument.  */
9260         parameter = build_tree_list (default_argument, parameter);
9261       }
9262       break;
9263
9264     case RID_TEMPLATE:
9265       {
9266         tree parameter_list;
9267         tree identifier;
9268         tree default_argument;
9269
9270         /* Look for the `<'.  */
9271         cp_parser_require (parser, CPP_LESS, "`<'");
9272         /* Parse the template-parameter-list.  */
9273         parameter_list = cp_parser_template_parameter_list (parser);
9274         /* Look for the `>'.  */
9275         cp_parser_require (parser, CPP_GREATER, "`>'");
9276         /* Look for the `class' keyword.  */
9277         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9278         /* If the next token is an ellipsis, we have a template
9279            argument pack. */
9280         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9281           {
9282             /* Consume the `...' token. */
9283             cp_lexer_consume_token (parser->lexer);
9284             maybe_warn_variadic_templates ();
9285
9286             *is_parameter_pack = true;
9287           }
9288         /* If the next token is an `=', then there is a
9289            default-argument.  If the next token is a `>', we are at
9290            the end of the parameter-list.  If the next token is a `,',
9291            then we are at the end of this parameter.  */
9292         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9293             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9294             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9295           {
9296             identifier = cp_parser_identifier (parser);
9297             /* Treat invalid names as if the parameter were nameless.  */
9298             if (identifier == error_mark_node)
9299               identifier = NULL_TREE;
9300           }
9301         else
9302           identifier = NULL_TREE;
9303
9304         /* Create the template parameter.  */
9305         parameter = finish_template_template_parm (class_type_node,
9306                                                    identifier);
9307
9308         /* If the next token is an `=', then there is a
9309            default-argument.  */
9310         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9311           {
9312             bool is_template;
9313
9314             /* Consume the `='.  */
9315             cp_lexer_consume_token (parser->lexer);
9316             /* Parse the id-expression.  */
9317             push_deferring_access_checks (dk_no_deferred);
9318             default_argument
9319               = cp_parser_id_expression (parser,
9320                                          /*template_keyword_p=*/false,
9321                                          /*check_dependency_p=*/true,
9322                                          /*template_p=*/&is_template,
9323                                          /*declarator_p=*/false,
9324                                          /*optional_p=*/false);
9325             if (TREE_CODE (default_argument) == TYPE_DECL)
9326               /* If the id-expression was a template-id that refers to
9327                  a template-class, we already have the declaration here,
9328                  so no further lookup is needed.  */
9329                  ;
9330             else
9331               /* Look up the name.  */
9332               default_argument
9333                 = cp_parser_lookup_name (parser, default_argument,
9334                                          none_type,
9335                                          /*is_template=*/is_template,
9336                                          /*is_namespace=*/false,
9337                                          /*check_dependency=*/true,
9338                                          /*ambiguous_decls=*/NULL);
9339             /* See if the default argument is valid.  */
9340             default_argument
9341               = check_template_template_default_arg (default_argument);
9342
9343             /* Template parameter packs cannot have default
9344                arguments. */
9345             if (*is_parameter_pack)
9346               {
9347                 if (identifier)
9348                   error ("template parameter pack %qD cannot have a default argument", 
9349                          identifier);
9350                 else
9351                   error ("template parameter packs cannot have default arguments");
9352                 default_argument = NULL_TREE;
9353               }
9354             pop_deferring_access_checks ();
9355           }
9356         else
9357           default_argument = NULL_TREE;
9358
9359         /* Create the combined representation of the parameter and the
9360            default argument.  */
9361         parameter = build_tree_list (default_argument, parameter);
9362       }
9363       break;
9364
9365     default:
9366       gcc_unreachable ();
9367       break;
9368     }
9369
9370   return parameter;
9371 }
9372
9373 /* Parse a template-id.
9374
9375    template-id:
9376      template-name < template-argument-list [opt] >
9377
9378    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9379    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9380    returned.  Otherwise, if the template-name names a function, or set
9381    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9382    names a class, returns a TYPE_DECL for the specialization.
9383
9384    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9385    uninstantiated templates.  */
9386
9387 static tree
9388 cp_parser_template_id (cp_parser *parser,
9389                        bool template_keyword_p,
9390                        bool check_dependency_p,
9391                        bool is_declaration)
9392 {
9393   int i;
9394   tree template;
9395   tree arguments;
9396   tree template_id;
9397   cp_token_position start_of_id = 0;
9398   deferred_access_check *chk;
9399   VEC (deferred_access_check,gc) *access_check;
9400   cp_token *next_token, *next_token_2;
9401   bool is_identifier;
9402
9403   /* If the next token corresponds to a template-id, there is no need
9404      to reparse it.  */
9405   next_token = cp_lexer_peek_token (parser->lexer);
9406   if (next_token->type == CPP_TEMPLATE_ID)
9407     {
9408       struct tree_check *check_value;
9409
9410       /* Get the stored value.  */
9411       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9412       /* Perform any access checks that were deferred.  */
9413       access_check = check_value->checks;
9414       if (access_check)
9415         {
9416           for (i = 0 ;
9417                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9418                ++i)
9419             {
9420               perform_or_defer_access_check (chk->binfo,
9421                                              chk->decl,
9422                                              chk->diag_decl);
9423             }
9424         }
9425       /* Return the stored value.  */
9426       return check_value->value;
9427     }
9428
9429   /* Avoid performing name lookup if there is no possibility of
9430      finding a template-id.  */
9431   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9432       || (next_token->type == CPP_NAME
9433           && !cp_parser_nth_token_starts_template_argument_list_p
9434                (parser, 2)))
9435     {
9436       cp_parser_error (parser, "expected template-id");
9437       return error_mark_node;
9438     }
9439
9440   /* Remember where the template-id starts.  */
9441   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9442     start_of_id = cp_lexer_token_position (parser->lexer, false);
9443
9444   push_deferring_access_checks (dk_deferred);
9445
9446   /* Parse the template-name.  */
9447   is_identifier = false;
9448   template = cp_parser_template_name (parser, template_keyword_p,
9449                                       check_dependency_p,
9450                                       is_declaration,
9451                                       &is_identifier);
9452   if (template == error_mark_node || is_identifier)
9453     {
9454       pop_deferring_access_checks ();
9455       return template;
9456     }
9457
9458   /* If we find the sequence `[:' after a template-name, it's probably
9459      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9460      parse correctly the argument list.  */
9461   next_token = cp_lexer_peek_token (parser->lexer);
9462   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9463   if (next_token->type == CPP_OPEN_SQUARE
9464       && next_token->flags & DIGRAPH
9465       && next_token_2->type == CPP_COLON
9466       && !(next_token_2->flags & PREV_WHITE))
9467     {
9468       cp_parser_parse_tentatively (parser);
9469       /* Change `:' into `::'.  */
9470       next_token_2->type = CPP_SCOPE;
9471       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9472          CPP_LESS.  */
9473       cp_lexer_consume_token (parser->lexer);
9474       /* Parse the arguments.  */
9475       arguments = cp_parser_enclosed_template_argument_list (parser);
9476       if (!cp_parser_parse_definitely (parser))
9477         {
9478           /* If we couldn't parse an argument list, then we revert our changes
9479              and return simply an error. Maybe this is not a template-id
9480              after all.  */
9481           next_token_2->type = CPP_COLON;
9482           cp_parser_error (parser, "expected %<<%>");
9483           pop_deferring_access_checks ();
9484           return error_mark_node;
9485         }
9486       /* Otherwise, emit an error about the invalid digraph, but continue
9487          parsing because we got our argument list.  */
9488       pedwarn ("%<<::%> cannot begin a template-argument list");
9489       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9490               "between %<<%> and %<::%>");
9491       if (!flag_permissive)
9492         {
9493           static bool hint;
9494           if (!hint)
9495             {
9496               inform ("(if you use -fpermissive G++ will accept your code)");
9497               hint = true;
9498             }
9499         }
9500     }
9501   else
9502     {
9503       /* Look for the `<' that starts the template-argument-list.  */
9504       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9505         {
9506           pop_deferring_access_checks ();
9507           return error_mark_node;
9508         }
9509       /* Parse the arguments.  */
9510       arguments = cp_parser_enclosed_template_argument_list (parser);
9511     }
9512
9513   /* Build a representation of the specialization.  */
9514   if (TREE_CODE (template) == IDENTIFIER_NODE)
9515     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9516   else if (DECL_CLASS_TEMPLATE_P (template)
9517            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9518     {
9519       bool entering_scope;
9520       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9521          template (rather than some instantiation thereof) only if
9522          is not nested within some other construct.  For example, in
9523          "template <typename T> void f(T) { A<T>::", A<T> is just an
9524          instantiation of A.  */
9525       entering_scope = (template_parm_scope_p ()
9526                         && cp_lexer_next_token_is (parser->lexer,
9527                                                    CPP_SCOPE));
9528       template_id
9529         = finish_template_type (template, arguments, entering_scope);
9530     }
9531   else
9532     {
9533       /* If it's not a class-template or a template-template, it should be
9534          a function-template.  */
9535       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9536                    || TREE_CODE (template) == OVERLOAD
9537                    || BASELINK_P (template)));
9538
9539       template_id = lookup_template_function (template, arguments);
9540     }
9541
9542   /* If parsing tentatively, replace the sequence of tokens that makes
9543      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9544      should we re-parse the token stream, we will not have to repeat
9545      the effort required to do the parse, nor will we issue duplicate
9546      error messages about problems during instantiation of the
9547      template.  */
9548   if (start_of_id)
9549     {
9550       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9551
9552       /* Reset the contents of the START_OF_ID token.  */
9553       token->type = CPP_TEMPLATE_ID;
9554       /* Retrieve any deferred checks.  Do not pop this access checks yet
9555          so the memory will not be reclaimed during token replacing below.  */
9556       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9557       token->u.tree_check_value->value = template_id;
9558       token->u.tree_check_value->checks = get_deferred_access_checks ();
9559       token->keyword = RID_MAX;
9560
9561       /* Purge all subsequent tokens.  */
9562       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9563
9564       /* ??? Can we actually assume that, if template_id ==
9565          error_mark_node, we will have issued a diagnostic to the
9566          user, as opposed to simply marking the tentative parse as
9567          failed?  */
9568       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9569         error ("parse error in template argument list");
9570     }
9571
9572   pop_deferring_access_checks ();
9573   return template_id;
9574 }
9575
9576 /* Parse a template-name.
9577
9578    template-name:
9579      identifier
9580
9581    The standard should actually say:
9582
9583    template-name:
9584      identifier
9585      operator-function-id
9586
9587    A defect report has been filed about this issue.
9588
9589    A conversion-function-id cannot be a template name because they cannot
9590    be part of a template-id. In fact, looking at this code:
9591
9592    a.operator K<int>()
9593
9594    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9595    It is impossible to call a templated conversion-function-id with an
9596    explicit argument list, since the only allowed template parameter is
9597    the type to which it is converting.
9598
9599    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9600    `template' keyword, in a construction like:
9601
9602      T::template f<3>()
9603
9604    In that case `f' is taken to be a template-name, even though there
9605    is no way of knowing for sure.
9606
9607    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9608    name refers to a set of overloaded functions, at least one of which
9609    is a template, or an IDENTIFIER_NODE with the name of the template,
9610    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9611    names are looked up inside uninstantiated templates.  */
9612
9613 static tree
9614 cp_parser_template_name (cp_parser* parser,
9615                          bool template_keyword_p,
9616                          bool check_dependency_p,
9617                          bool is_declaration,
9618                          bool *is_identifier)
9619 {
9620   tree identifier;
9621   tree decl;
9622   tree fns;
9623
9624   /* If the next token is `operator', then we have either an
9625      operator-function-id or a conversion-function-id.  */
9626   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9627     {
9628       /* We don't know whether we're looking at an
9629          operator-function-id or a conversion-function-id.  */
9630       cp_parser_parse_tentatively (parser);
9631       /* Try an operator-function-id.  */
9632       identifier = cp_parser_operator_function_id (parser);
9633       /* If that didn't work, try a conversion-function-id.  */
9634       if (!cp_parser_parse_definitely (parser))
9635         {
9636           cp_parser_error (parser, "expected template-name");
9637           return error_mark_node;
9638         }
9639     }
9640   /* Look for the identifier.  */
9641   else
9642     identifier = cp_parser_identifier (parser);
9643
9644   /* If we didn't find an identifier, we don't have a template-id.  */
9645   if (identifier == error_mark_node)
9646     return error_mark_node;
9647
9648   /* If the name immediately followed the `template' keyword, then it
9649      is a template-name.  However, if the next token is not `<', then
9650      we do not treat it as a template-name, since it is not being used
9651      as part of a template-id.  This enables us to handle constructs
9652      like:
9653
9654        template <typename T> struct S { S(); };
9655        template <typename T> S<T>::S();
9656
9657      correctly.  We would treat `S' as a template -- if it were `S<T>'
9658      -- but we do not if there is no `<'.  */
9659
9660   if (processing_template_decl
9661       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9662     {
9663       /* In a declaration, in a dependent context, we pretend that the
9664          "template" keyword was present in order to improve error
9665          recovery.  For example, given:
9666
9667            template <typename T> void f(T::X<int>);
9668
9669          we want to treat "X<int>" as a template-id.  */
9670       if (is_declaration
9671           && !template_keyword_p
9672           && parser->scope && TYPE_P (parser->scope)
9673           && check_dependency_p
9674           && dependent_type_p (parser->scope)
9675           /* Do not do this for dtors (or ctors), since they never
9676              need the template keyword before their name.  */
9677           && !constructor_name_p (identifier, parser->scope))
9678         {
9679           cp_token_position start = 0;
9680
9681           /* Explain what went wrong.  */
9682           error ("non-template %qD used as template", identifier);
9683           inform ("use %<%T::template %D%> to indicate that it is a template",
9684                   parser->scope, identifier);
9685           /* If parsing tentatively, find the location of the "<" token.  */
9686           if (cp_parser_simulate_error (parser))
9687             start = cp_lexer_token_position (parser->lexer, true);
9688           /* Parse the template arguments so that we can issue error
9689              messages about them.  */
9690           cp_lexer_consume_token (parser->lexer);
9691           cp_parser_enclosed_template_argument_list (parser);
9692           /* Skip tokens until we find a good place from which to
9693              continue parsing.  */
9694           cp_parser_skip_to_closing_parenthesis (parser,
9695                                                  /*recovering=*/true,
9696                                                  /*or_comma=*/true,
9697                                                  /*consume_paren=*/false);
9698           /* If parsing tentatively, permanently remove the
9699              template argument list.  That will prevent duplicate
9700              error messages from being issued about the missing
9701              "template" keyword.  */
9702           if (start)
9703             cp_lexer_purge_tokens_after (parser->lexer, start);
9704           if (is_identifier)
9705             *is_identifier = true;
9706           return identifier;
9707         }
9708
9709       /* If the "template" keyword is present, then there is generally
9710          no point in doing name-lookup, so we just return IDENTIFIER.
9711          But, if the qualifying scope is non-dependent then we can
9712          (and must) do name-lookup normally.  */
9713       if (template_keyword_p
9714           && (!parser->scope
9715               || (TYPE_P (parser->scope)
9716                   && dependent_type_p (parser->scope))))
9717         return identifier;
9718     }
9719
9720   /* Look up the name.  */
9721   decl = cp_parser_lookup_name (parser, identifier,
9722                                 none_type,
9723                                 /*is_template=*/false,
9724                                 /*is_namespace=*/false,
9725                                 check_dependency_p,
9726                                 /*ambiguous_decls=*/NULL);
9727   decl = maybe_get_template_decl_from_type_decl (decl);
9728
9729   /* If DECL is a template, then the name was a template-name.  */
9730   if (TREE_CODE (decl) == TEMPLATE_DECL)
9731     ;
9732   else
9733     {
9734       tree fn = NULL_TREE;
9735
9736       /* The standard does not explicitly indicate whether a name that
9737          names a set of overloaded declarations, some of which are
9738          templates, is a template-name.  However, such a name should
9739          be a template-name; otherwise, there is no way to form a
9740          template-id for the overloaded templates.  */
9741       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9742       if (TREE_CODE (fns) == OVERLOAD)
9743         for (fn = fns; fn; fn = OVL_NEXT (fn))
9744           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9745             break;
9746
9747       if (!fn)
9748         {
9749           /* The name does not name a template.  */
9750           cp_parser_error (parser, "expected template-name");
9751           return error_mark_node;
9752         }
9753     }
9754
9755   /* If DECL is dependent, and refers to a function, then just return
9756      its name; we will look it up again during template instantiation.  */
9757   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9758     {
9759       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9760       if (TYPE_P (scope) && dependent_type_p (scope))
9761         return identifier;
9762     }
9763
9764   return decl;
9765 }
9766
9767 /* Parse a template-argument-list.
9768
9769    template-argument-list:
9770      template-argument ... [opt]
9771      template-argument-list , template-argument ... [opt]
9772
9773    Returns a TREE_VEC containing the arguments.  */
9774
9775 static tree
9776 cp_parser_template_argument_list (cp_parser* parser)
9777 {
9778   tree fixed_args[10];
9779   unsigned n_args = 0;
9780   unsigned alloced = 10;
9781   tree *arg_ary = fixed_args;
9782   tree vec;
9783   bool saved_in_template_argument_list_p;
9784   bool saved_ice_p;
9785   bool saved_non_ice_p;
9786
9787   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9788   parser->in_template_argument_list_p = true;
9789   /* Even if the template-id appears in an integral
9790      constant-expression, the contents of the argument list do
9791      not.  */
9792   saved_ice_p = parser->integral_constant_expression_p;
9793   parser->integral_constant_expression_p = false;
9794   saved_non_ice_p = parser->non_integral_constant_expression_p;
9795   parser->non_integral_constant_expression_p = false;
9796   /* Parse the arguments.  */
9797   do
9798     {
9799       tree argument;
9800
9801       if (n_args)
9802         /* Consume the comma.  */
9803         cp_lexer_consume_token (parser->lexer);
9804
9805       /* Parse the template-argument.  */
9806       argument = cp_parser_template_argument (parser);
9807
9808       /* If the next token is an ellipsis, we're expanding a template
9809          argument pack. */
9810       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9811         {
9812           /* Consume the `...' token. */
9813           cp_lexer_consume_token (parser->lexer);
9814
9815           /* Make the argument into a TYPE_PACK_EXPANSION or
9816              EXPR_PACK_EXPANSION. */
9817           argument = make_pack_expansion (argument);
9818         }
9819
9820       if (n_args == alloced)
9821         {
9822           alloced *= 2;
9823
9824           if (arg_ary == fixed_args)
9825             {
9826               arg_ary = XNEWVEC (tree, alloced);
9827               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9828             }
9829           else
9830             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9831         }
9832       arg_ary[n_args++] = argument;
9833     }
9834   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9835
9836   vec = make_tree_vec (n_args);
9837
9838   while (n_args--)
9839     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9840
9841   if (arg_ary != fixed_args)
9842     free (arg_ary);
9843   parser->non_integral_constant_expression_p = saved_non_ice_p;
9844   parser->integral_constant_expression_p = saved_ice_p;
9845   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9846   return vec;
9847 }
9848
9849 /* Parse a template-argument.
9850
9851    template-argument:
9852      assignment-expression
9853      type-id
9854      id-expression
9855
9856    The representation is that of an assignment-expression, type-id, or
9857    id-expression -- except that the qualified id-expression is
9858    evaluated, so that the value returned is either a DECL or an
9859    OVERLOAD.
9860
9861    Although the standard says "assignment-expression", it forbids
9862    throw-expressions or assignments in the template argument.
9863    Therefore, we use "conditional-expression" instead.  */
9864
9865 static tree
9866 cp_parser_template_argument (cp_parser* parser)
9867 {
9868   tree argument;
9869   bool template_p;
9870   bool address_p;
9871   bool maybe_type_id = false;
9872   cp_token *token;
9873   cp_id_kind idk;
9874
9875   /* There's really no way to know what we're looking at, so we just
9876      try each alternative in order.
9877
9878        [temp.arg]
9879
9880        In a template-argument, an ambiguity between a type-id and an
9881        expression is resolved to a type-id, regardless of the form of
9882        the corresponding template-parameter.
9883
9884      Therefore, we try a type-id first.  */
9885   cp_parser_parse_tentatively (parser);
9886   argument = cp_parser_type_id (parser);
9887   /* If there was no error parsing the type-id but the next token is a '>>',
9888      we probably found a typo for '> >'. But there are type-id which are
9889      also valid expressions. For instance:
9890
9891      struct X { int operator >> (int); };
9892      template <int V> struct Foo {};
9893      Foo<X () >> 5> r;
9894
9895      Here 'X()' is a valid type-id of a function type, but the user just
9896      wanted to write the expression "X() >> 5". Thus, we remember that we
9897      found a valid type-id, but we still try to parse the argument as an
9898      expression to see what happens.  */
9899   if (!cp_parser_error_occurred (parser)
9900       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9901     {
9902       maybe_type_id = true;
9903       cp_parser_abort_tentative_parse (parser);
9904     }
9905   else
9906     {
9907       /* If the next token isn't a `,' or a `>', then this argument wasn't
9908       really finished. This means that the argument is not a valid
9909       type-id.  */
9910       if (!cp_parser_next_token_ends_template_argument_p (parser))
9911         cp_parser_error (parser, "expected template-argument");
9912       /* If that worked, we're done.  */
9913       if (cp_parser_parse_definitely (parser))
9914         return argument;
9915     }
9916   /* We're still not sure what the argument will be.  */
9917   cp_parser_parse_tentatively (parser);
9918   /* Try a template.  */
9919   argument = cp_parser_id_expression (parser,
9920                                       /*template_keyword_p=*/false,
9921                                       /*check_dependency_p=*/true,
9922                                       &template_p,
9923                                       /*declarator_p=*/false,
9924                                       /*optional_p=*/false);
9925   /* If the next token isn't a `,' or a `>', then this argument wasn't
9926      really finished.  */
9927   if (!cp_parser_next_token_ends_template_argument_p (parser))
9928     cp_parser_error (parser, "expected template-argument");
9929   if (!cp_parser_error_occurred (parser))
9930     {
9931       /* Figure out what is being referred to.  If the id-expression
9932          was for a class template specialization, then we will have a
9933          TYPE_DECL at this point.  There is no need to do name lookup
9934          at this point in that case.  */
9935       if (TREE_CODE (argument) != TYPE_DECL)
9936         argument = cp_parser_lookup_name (parser, argument,
9937                                           none_type,
9938                                           /*is_template=*/template_p,
9939                                           /*is_namespace=*/false,
9940                                           /*check_dependency=*/true,
9941                                           /*ambiguous_decls=*/NULL);
9942       if (TREE_CODE (argument) != TEMPLATE_DECL
9943           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9944         cp_parser_error (parser, "expected template-name");
9945     }
9946   if (cp_parser_parse_definitely (parser))
9947     return argument;
9948   /* It must be a non-type argument.  There permitted cases are given
9949      in [temp.arg.nontype]:
9950
9951      -- an integral constant-expression of integral or enumeration
9952         type; or
9953
9954      -- the name of a non-type template-parameter; or
9955
9956      -- the name of an object or function with external linkage...
9957
9958      -- the address of an object or function with external linkage...
9959
9960      -- a pointer to member...  */
9961   /* Look for a non-type template parameter.  */
9962   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9963     {
9964       cp_parser_parse_tentatively (parser);
9965       argument = cp_parser_primary_expression (parser,
9966                                                /*adress_p=*/false,
9967                                                /*cast_p=*/false,
9968                                                /*template_arg_p=*/true,
9969                                                &idk);
9970       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9971           || !cp_parser_next_token_ends_template_argument_p (parser))
9972         cp_parser_simulate_error (parser);
9973       if (cp_parser_parse_definitely (parser))
9974         return argument;
9975     }
9976
9977   /* If the next token is "&", the argument must be the address of an
9978      object or function with external linkage.  */
9979   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9980   if (address_p)
9981     cp_lexer_consume_token (parser->lexer);
9982   /* See if we might have an id-expression.  */
9983   token = cp_lexer_peek_token (parser->lexer);
9984   if (token->type == CPP_NAME
9985       || token->keyword == RID_OPERATOR
9986       || token->type == CPP_SCOPE
9987       || token->type == CPP_TEMPLATE_ID
9988       || token->type == CPP_NESTED_NAME_SPECIFIER)
9989     {
9990       cp_parser_parse_tentatively (parser);
9991       argument = cp_parser_primary_expression (parser,
9992                                                address_p,
9993                                                /*cast_p=*/false,
9994                                                /*template_arg_p=*/true,
9995                                                &idk);
9996       if (cp_parser_error_occurred (parser)
9997           || !cp_parser_next_token_ends_template_argument_p (parser))
9998         cp_parser_abort_tentative_parse (parser);
9999       else
10000         {
10001           if (TREE_CODE (argument) == INDIRECT_REF)
10002             {
10003               gcc_assert (REFERENCE_REF_P (argument));
10004               argument = TREE_OPERAND (argument, 0);
10005             }
10006
10007           if (TREE_CODE (argument) == VAR_DECL)
10008             {
10009               /* A variable without external linkage might still be a
10010                  valid constant-expression, so no error is issued here
10011                  if the external-linkage check fails.  */
10012               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10013                 cp_parser_simulate_error (parser);
10014             }
10015           else if (is_overloaded_fn (argument))
10016             /* All overloaded functions are allowed; if the external
10017                linkage test does not pass, an error will be issued
10018                later.  */
10019             ;
10020           else if (address_p
10021                    && (TREE_CODE (argument) == OFFSET_REF
10022                        || TREE_CODE (argument) == SCOPE_REF))
10023             /* A pointer-to-member.  */
10024             ;
10025           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10026             ;
10027           else
10028             cp_parser_simulate_error (parser);
10029
10030           if (cp_parser_parse_definitely (parser))
10031             {
10032               if (address_p)
10033                 argument = build_x_unary_op (ADDR_EXPR, argument);
10034               return argument;
10035             }
10036         }
10037     }
10038   /* If the argument started with "&", there are no other valid
10039      alternatives at this point.  */
10040   if (address_p)
10041     {
10042       cp_parser_error (parser, "invalid non-type template argument");
10043       return error_mark_node;
10044     }
10045
10046   /* If the argument wasn't successfully parsed as a type-id followed
10047      by '>>', the argument can only be a constant expression now.
10048      Otherwise, we try parsing the constant-expression tentatively,
10049      because the argument could really be a type-id.  */
10050   if (maybe_type_id)
10051     cp_parser_parse_tentatively (parser);
10052   argument = cp_parser_constant_expression (parser,
10053                                             /*allow_non_constant_p=*/false,
10054                                             /*non_constant_p=*/NULL);
10055   argument = fold_non_dependent_expr (argument);
10056   if (!maybe_type_id)
10057     return argument;
10058   if (!cp_parser_next_token_ends_template_argument_p (parser))
10059     cp_parser_error (parser, "expected template-argument");
10060   if (cp_parser_parse_definitely (parser))
10061     return argument;
10062   /* We did our best to parse the argument as a non type-id, but that
10063      was the only alternative that matched (albeit with a '>' after
10064      it). We can assume it's just a typo from the user, and a
10065      diagnostic will then be issued.  */
10066   return cp_parser_type_id (parser);
10067 }
10068
10069 /* Parse an explicit-instantiation.
10070
10071    explicit-instantiation:
10072      template declaration
10073
10074    Although the standard says `declaration', what it really means is:
10075
10076    explicit-instantiation:
10077      template decl-specifier-seq [opt] declarator [opt] ;
10078
10079    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10080    supposed to be allowed.  A defect report has been filed about this
10081    issue.
10082
10083    GNU Extension:
10084
10085    explicit-instantiation:
10086      storage-class-specifier template
10087        decl-specifier-seq [opt] declarator [opt] ;
10088      function-specifier template
10089        decl-specifier-seq [opt] declarator [opt] ;  */
10090
10091 static void
10092 cp_parser_explicit_instantiation (cp_parser* parser)
10093 {
10094   int declares_class_or_enum;
10095   cp_decl_specifier_seq decl_specifiers;
10096   tree extension_specifier = NULL_TREE;
10097
10098   /* Look for an (optional) storage-class-specifier or
10099      function-specifier.  */
10100   if (cp_parser_allow_gnu_extensions_p (parser))
10101     {
10102       extension_specifier
10103         = cp_parser_storage_class_specifier_opt (parser);
10104       if (!extension_specifier)
10105         extension_specifier
10106           = cp_parser_function_specifier_opt (parser,
10107                                               /*decl_specs=*/NULL);
10108     }
10109
10110   /* Look for the `template' keyword.  */
10111   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10112   /* Let the front end know that we are processing an explicit
10113      instantiation.  */
10114   begin_explicit_instantiation ();
10115   /* [temp.explicit] says that we are supposed to ignore access
10116      control while processing explicit instantiation directives.  */
10117   push_deferring_access_checks (dk_no_check);
10118   /* Parse a decl-specifier-seq.  */
10119   cp_parser_decl_specifier_seq (parser,
10120                                 CP_PARSER_FLAGS_OPTIONAL,
10121                                 &decl_specifiers,
10122                                 &declares_class_or_enum);
10123   /* If there was exactly one decl-specifier, and it declared a class,
10124      and there's no declarator, then we have an explicit type
10125      instantiation.  */
10126   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10127     {
10128       tree type;
10129
10130       type = check_tag_decl (&decl_specifiers);
10131       /* Turn access control back on for names used during
10132          template instantiation.  */
10133       pop_deferring_access_checks ();
10134       if (type)
10135         do_type_instantiation (type, extension_specifier,
10136                                /*complain=*/tf_error);
10137     }
10138   else
10139     {
10140       cp_declarator *declarator;
10141       tree decl;
10142
10143       /* Parse the declarator.  */
10144       declarator
10145         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10146                                 /*ctor_dtor_or_conv_p=*/NULL,
10147                                 /*parenthesized_p=*/NULL,
10148                                 /*member_p=*/false);
10149       if (declares_class_or_enum & 2)
10150         cp_parser_check_for_definition_in_return_type (declarator,
10151                                                        decl_specifiers.type);
10152       if (declarator != cp_error_declarator)
10153         {
10154           decl = grokdeclarator (declarator, &decl_specifiers,
10155                                  NORMAL, 0, &decl_specifiers.attributes);
10156           /* Turn access control back on for names used during
10157              template instantiation.  */
10158           pop_deferring_access_checks ();
10159           /* Do the explicit instantiation.  */
10160           do_decl_instantiation (decl, extension_specifier);
10161         }
10162       else
10163         {
10164           pop_deferring_access_checks ();
10165           /* Skip the body of the explicit instantiation.  */
10166           cp_parser_skip_to_end_of_statement (parser);
10167         }
10168     }
10169   /* We're done with the instantiation.  */
10170   end_explicit_instantiation ();
10171
10172   cp_parser_consume_semicolon_at_end_of_statement (parser);
10173 }
10174
10175 /* Parse an explicit-specialization.
10176
10177    explicit-specialization:
10178      template < > declaration
10179
10180    Although the standard says `declaration', what it really means is:
10181
10182    explicit-specialization:
10183      template <> decl-specifier [opt] init-declarator [opt] ;
10184      template <> function-definition
10185      template <> explicit-specialization
10186      template <> template-declaration  */
10187
10188 static void
10189 cp_parser_explicit_specialization (cp_parser* parser)
10190 {
10191   bool need_lang_pop;
10192   /* Look for the `template' keyword.  */
10193   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10194   /* Look for the `<'.  */
10195   cp_parser_require (parser, CPP_LESS, "`<'");
10196   /* Look for the `>'.  */
10197   cp_parser_require (parser, CPP_GREATER, "`>'");
10198   /* We have processed another parameter list.  */
10199   ++parser->num_template_parameter_lists;
10200   /* [temp]
10201
10202      A template ... explicit specialization ... shall not have C
10203      linkage.  */
10204   if (current_lang_name == lang_name_c)
10205     {
10206       error ("template specialization with C linkage");
10207       /* Give it C++ linkage to avoid confusing other parts of the
10208          front end.  */
10209       push_lang_context (lang_name_cplusplus);
10210       need_lang_pop = true;
10211     }
10212   else
10213     need_lang_pop = false;
10214   /* Let the front end know that we are beginning a specialization.  */
10215   if (!begin_specialization ())
10216     {
10217       end_specialization ();
10218       cp_parser_skip_to_end_of_block_or_statement (parser);
10219       return;
10220     }
10221
10222   /* If the next keyword is `template', we need to figure out whether
10223      or not we're looking a template-declaration.  */
10224   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10225     {
10226       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10227           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10228         cp_parser_template_declaration_after_export (parser,
10229                                                      /*member_p=*/false);
10230       else
10231         cp_parser_explicit_specialization (parser);
10232     }
10233   else
10234     /* Parse the dependent declaration.  */
10235     cp_parser_single_declaration (parser,
10236                                   /*checks=*/NULL,
10237                                   /*member_p=*/false,
10238                                   /*explicit_specialization_p=*/true,
10239                                   /*friend_p=*/NULL);
10240   /* We're done with the specialization.  */
10241   end_specialization ();
10242   /* For the erroneous case of a template with C linkage, we pushed an
10243      implicit C++ linkage scope; exit that scope now.  */
10244   if (need_lang_pop)
10245     pop_lang_context ();
10246   /* We're done with this parameter list.  */
10247   --parser->num_template_parameter_lists;
10248 }
10249
10250 /* Parse a type-specifier.
10251
10252    type-specifier:
10253      simple-type-specifier
10254      class-specifier
10255      enum-specifier
10256      elaborated-type-specifier
10257      cv-qualifier
10258
10259    GNU Extension:
10260
10261    type-specifier:
10262      __complex__
10263
10264    Returns a representation of the type-specifier.  For a
10265    class-specifier, enum-specifier, or elaborated-type-specifier, a
10266    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10267
10268    The parser flags FLAGS is used to control type-specifier parsing.
10269
10270    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10271    in a decl-specifier-seq.
10272
10273    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10274    class-specifier, enum-specifier, or elaborated-type-specifier, then
10275    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10276    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10277    zero.
10278
10279    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10280    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10281    is set to FALSE.  */
10282
10283 static tree
10284 cp_parser_type_specifier (cp_parser* parser,
10285                           cp_parser_flags flags,
10286                           cp_decl_specifier_seq *decl_specs,
10287                           bool is_declaration,
10288                           int* declares_class_or_enum,
10289                           bool* is_cv_qualifier)
10290 {
10291   tree type_spec = NULL_TREE;
10292   cp_token *token;
10293   enum rid keyword;
10294   cp_decl_spec ds = ds_last;
10295
10296   /* Assume this type-specifier does not declare a new type.  */
10297   if (declares_class_or_enum)
10298     *declares_class_or_enum = 0;
10299   /* And that it does not specify a cv-qualifier.  */
10300   if (is_cv_qualifier)
10301     *is_cv_qualifier = false;
10302   /* Peek at the next token.  */
10303   token = cp_lexer_peek_token (parser->lexer);
10304
10305   /* If we're looking at a keyword, we can use that to guide the
10306      production we choose.  */
10307   keyword = token->keyword;
10308   switch (keyword)
10309     {
10310     case RID_ENUM:
10311       /* Look for the enum-specifier.  */
10312       type_spec = cp_parser_enum_specifier (parser);
10313       /* If that worked, we're done.  */
10314       if (type_spec)
10315         {
10316           if (declares_class_or_enum)
10317             *declares_class_or_enum = 2;
10318           if (decl_specs)
10319             cp_parser_set_decl_spec_type (decl_specs,
10320                                           type_spec,
10321                                           /*user_defined_p=*/true);
10322           return type_spec;
10323         }
10324       else
10325         goto elaborated_type_specifier;
10326
10327       /* Any of these indicate either a class-specifier, or an
10328          elaborated-type-specifier.  */
10329     case RID_CLASS:
10330     case RID_STRUCT:
10331     case RID_UNION:
10332       /* Parse tentatively so that we can back up if we don't find a
10333          class-specifier.  */
10334       cp_parser_parse_tentatively (parser);
10335       /* Look for the class-specifier.  */
10336       type_spec = cp_parser_class_specifier (parser);
10337       /* If that worked, we're done.  */
10338       if (cp_parser_parse_definitely (parser))
10339         {
10340           if (declares_class_or_enum)
10341             *declares_class_or_enum = 2;
10342           if (decl_specs)
10343             cp_parser_set_decl_spec_type (decl_specs,
10344                                           type_spec,
10345                                           /*user_defined_p=*/true);
10346           return type_spec;
10347         }
10348
10349       /* Fall through.  */
10350     elaborated_type_specifier:
10351       /* We're declaring (not defining) a class or enum.  */
10352       if (declares_class_or_enum)
10353         *declares_class_or_enum = 1;
10354
10355       /* Fall through.  */
10356     case RID_TYPENAME:
10357       /* Look for an elaborated-type-specifier.  */
10358       type_spec
10359         = (cp_parser_elaborated_type_specifier
10360            (parser,
10361             decl_specs && decl_specs->specs[(int) ds_friend],
10362             is_declaration));
10363       if (decl_specs)
10364         cp_parser_set_decl_spec_type (decl_specs,
10365                                       type_spec,
10366                                       /*user_defined_p=*/true);
10367       return type_spec;
10368
10369     case RID_CONST:
10370       ds = ds_const;
10371       if (is_cv_qualifier)
10372         *is_cv_qualifier = true;
10373       break;
10374
10375     case RID_VOLATILE:
10376       ds = ds_volatile;
10377       if (is_cv_qualifier)
10378         *is_cv_qualifier = true;
10379       break;
10380
10381     case RID_RESTRICT:
10382       ds = ds_restrict;
10383       if (is_cv_qualifier)
10384         *is_cv_qualifier = true;
10385       break;
10386
10387     case RID_COMPLEX:
10388       /* The `__complex__' keyword is a GNU extension.  */
10389       ds = ds_complex;
10390       break;
10391
10392     default:
10393       break;
10394     }
10395
10396   /* Handle simple keywords.  */
10397   if (ds != ds_last)
10398     {
10399       if (decl_specs)
10400         {
10401           ++decl_specs->specs[(int)ds];
10402           decl_specs->any_specifiers_p = true;
10403         }
10404       return cp_lexer_consume_token (parser->lexer)->u.value;
10405     }
10406
10407   /* If we do not already have a type-specifier, assume we are looking
10408      at a simple-type-specifier.  */
10409   type_spec = cp_parser_simple_type_specifier (parser,
10410                                                decl_specs,
10411                                                flags);
10412
10413   /* If we didn't find a type-specifier, and a type-specifier was not
10414      optional in this context, issue an error message.  */
10415   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10416     {
10417       cp_parser_error (parser, "expected type specifier");
10418       return error_mark_node;
10419     }
10420
10421   return type_spec;
10422 }
10423
10424 /* Parse a simple-type-specifier.
10425
10426    simple-type-specifier:
10427      :: [opt] nested-name-specifier [opt] type-name
10428      :: [opt] nested-name-specifier template template-id
10429      char
10430      wchar_t
10431      bool
10432      short
10433      int
10434      long
10435      signed
10436      unsigned
10437      float
10438      double
10439      void
10440
10441    GNU Extension:
10442
10443    simple-type-specifier:
10444      __typeof__ unary-expression
10445      __typeof__ ( type-id )
10446
10447    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10448    appropriately updated.  */
10449
10450 static tree
10451 cp_parser_simple_type_specifier (cp_parser* parser,
10452                                  cp_decl_specifier_seq *decl_specs,
10453                                  cp_parser_flags flags)
10454 {
10455   tree type = NULL_TREE;
10456   cp_token *token;
10457
10458   /* Peek at the next token.  */
10459   token = cp_lexer_peek_token (parser->lexer);
10460
10461   /* If we're looking at a keyword, things are easy.  */
10462   switch (token->keyword)
10463     {
10464     case RID_CHAR:
10465       if (decl_specs)
10466         decl_specs->explicit_char_p = true;
10467       type = char_type_node;
10468       break;
10469     case RID_WCHAR:
10470       type = wchar_type_node;
10471       break;
10472     case RID_BOOL:
10473       type = boolean_type_node;
10474       break;
10475     case RID_SHORT:
10476       if (decl_specs)
10477         ++decl_specs->specs[(int) ds_short];
10478       type = short_integer_type_node;
10479       break;
10480     case RID_INT:
10481       if (decl_specs)
10482         decl_specs->explicit_int_p = true;
10483       type = integer_type_node;
10484       break;
10485     case RID_LONG:
10486       if (decl_specs)
10487         ++decl_specs->specs[(int) ds_long];
10488       type = long_integer_type_node;
10489       break;
10490     case RID_SIGNED:
10491       if (decl_specs)
10492         ++decl_specs->specs[(int) ds_signed];
10493       type = integer_type_node;
10494       break;
10495     case RID_UNSIGNED:
10496       if (decl_specs)
10497         ++decl_specs->specs[(int) ds_unsigned];
10498       type = unsigned_type_node;
10499       break;
10500     case RID_FLOAT:
10501       type = float_type_node;
10502       break;
10503     case RID_DOUBLE:
10504       type = double_type_node;
10505       break;
10506     case RID_VOID:
10507       type = void_type_node;
10508       break;
10509
10510     case RID_TYPEOF:
10511       /* Consume the `typeof' token.  */
10512       cp_lexer_consume_token (parser->lexer);
10513       /* Parse the operand to `typeof'.  */
10514       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10515       /* If it is not already a TYPE, take its type.  */
10516       if (!TYPE_P (type))
10517         type = finish_typeof (type);
10518
10519       if (decl_specs)
10520         cp_parser_set_decl_spec_type (decl_specs, type,
10521                                       /*user_defined_p=*/true);
10522
10523       return type;
10524
10525     default:
10526       break;
10527     }
10528
10529   /* If the type-specifier was for a built-in type, we're done.  */
10530   if (type)
10531     {
10532       tree id;
10533
10534       /* Record the type.  */
10535       if (decl_specs
10536           && (token->keyword != RID_SIGNED
10537               && token->keyword != RID_UNSIGNED
10538               && token->keyword != RID_SHORT
10539               && token->keyword != RID_LONG))
10540         cp_parser_set_decl_spec_type (decl_specs,
10541                                       type,
10542                                       /*user_defined=*/false);
10543       if (decl_specs)
10544         decl_specs->any_specifiers_p = true;
10545
10546       /* Consume the token.  */
10547       id = cp_lexer_consume_token (parser->lexer)->u.value;
10548
10549       /* There is no valid C++ program where a non-template type is
10550          followed by a "<".  That usually indicates that the user thought
10551          that the type was a template.  */
10552       cp_parser_check_for_invalid_template_id (parser, type);
10553
10554       return TYPE_NAME (type);
10555     }
10556
10557   /* The type-specifier must be a user-defined type.  */
10558   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10559     {
10560       bool qualified_p;
10561       bool global_p;
10562
10563       /* Don't gobble tokens or issue error messages if this is an
10564          optional type-specifier.  */
10565       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10566         cp_parser_parse_tentatively (parser);
10567
10568       /* Look for the optional `::' operator.  */
10569       global_p
10570         = (cp_parser_global_scope_opt (parser,
10571                                        /*current_scope_valid_p=*/false)
10572            != NULL_TREE);
10573       /* Look for the nested-name specifier.  */
10574       qualified_p
10575         = (cp_parser_nested_name_specifier_opt (parser,
10576                                                 /*typename_keyword_p=*/false,
10577                                                 /*check_dependency_p=*/true,
10578                                                 /*type_p=*/false,
10579                                                 /*is_declaration=*/false)
10580            != NULL_TREE);
10581       /* If we have seen a nested-name-specifier, and the next token
10582          is `template', then we are using the template-id production.  */
10583       if (parser->scope
10584           && cp_parser_optional_template_keyword (parser))
10585         {
10586           /* Look for the template-id.  */
10587           type = cp_parser_template_id (parser,
10588                                         /*template_keyword_p=*/true,
10589                                         /*check_dependency_p=*/true,
10590                                         /*is_declaration=*/false);
10591           /* If the template-id did not name a type, we are out of
10592              luck.  */
10593           if (TREE_CODE (type) != TYPE_DECL)
10594             {
10595               cp_parser_error (parser, "expected template-id for type");
10596               type = NULL_TREE;
10597             }
10598         }
10599       /* Otherwise, look for a type-name.  */
10600       else
10601         type = cp_parser_type_name (parser);
10602       /* Keep track of all name-lookups performed in class scopes.  */
10603       if (type
10604           && !global_p
10605           && !qualified_p
10606           && TREE_CODE (type) == TYPE_DECL
10607           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10608         maybe_note_name_used_in_class (DECL_NAME (type), type);
10609       /* If it didn't work out, we don't have a TYPE.  */
10610       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10611           && !cp_parser_parse_definitely (parser))
10612         type = NULL_TREE;
10613       if (type && decl_specs)
10614         cp_parser_set_decl_spec_type (decl_specs, type,
10615                                       /*user_defined=*/true);
10616     }
10617
10618   /* If we didn't get a type-name, issue an error message.  */
10619   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10620     {
10621       cp_parser_error (parser, "expected type-name");
10622       return error_mark_node;
10623     }
10624
10625   /* There is no valid C++ program where a non-template type is
10626      followed by a "<".  That usually indicates that the user thought
10627      that the type was a template.  */
10628   if (type && type != error_mark_node)
10629     {
10630       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10631          If it is, then the '<'...'>' enclose protocol names rather than
10632          template arguments, and so everything is fine.  */
10633       if (c_dialect_objc ()
10634           && (objc_is_id (type) || objc_is_class_name (type)))
10635         {
10636           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10637           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10638
10639           /* Clobber the "unqualified" type previously entered into
10640              DECL_SPECS with the new, improved protocol-qualified version.  */
10641           if (decl_specs)
10642             decl_specs->type = qual_type;
10643
10644           return qual_type;
10645         }
10646
10647       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10648     }
10649
10650   return type;
10651 }
10652
10653 /* Parse a type-name.
10654
10655    type-name:
10656      class-name
10657      enum-name
10658      typedef-name
10659
10660    enum-name:
10661      identifier
10662
10663    typedef-name:
10664      identifier
10665
10666    Returns a TYPE_DECL for the type.  */
10667
10668 static tree
10669 cp_parser_type_name (cp_parser* parser)
10670 {
10671   tree type_decl;
10672   tree identifier;
10673
10674   /* We can't know yet whether it is a class-name or not.  */
10675   cp_parser_parse_tentatively (parser);
10676   /* Try a class-name.  */
10677   type_decl = cp_parser_class_name (parser,
10678                                     /*typename_keyword_p=*/false,
10679                                     /*template_keyword_p=*/false,
10680                                     none_type,
10681                                     /*check_dependency_p=*/true,
10682                                     /*class_head_p=*/false,
10683                                     /*is_declaration=*/false);
10684   /* If it's not a class-name, keep looking.  */
10685   if (!cp_parser_parse_definitely (parser))
10686     {
10687       /* It must be a typedef-name or an enum-name.  */
10688       identifier = cp_parser_identifier (parser);
10689       if (identifier == error_mark_node)
10690         return error_mark_node;
10691
10692       /* Look up the type-name.  */
10693       type_decl = cp_parser_lookup_name_simple (parser, identifier);
10694
10695       if (TREE_CODE (type_decl) != TYPE_DECL
10696           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10697         {
10698           /* See if this is an Objective-C type.  */
10699           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10700           tree type = objc_get_protocol_qualified_type (identifier, protos);
10701           if (type)
10702             type_decl = TYPE_NAME (type);
10703         }
10704
10705       /* Issue an error if we did not find a type-name.  */
10706       if (TREE_CODE (type_decl) != TYPE_DECL)
10707         {
10708           if (!cp_parser_simulate_error (parser))
10709             cp_parser_name_lookup_error (parser, identifier, type_decl,
10710                                          "is not a type");
10711           type_decl = error_mark_node;
10712         }
10713       /* Remember that the name was used in the definition of the
10714          current class so that we can check later to see if the
10715          meaning would have been different after the class was
10716          entirely defined.  */
10717       else if (type_decl != error_mark_node
10718                && !parser->scope)
10719         maybe_note_name_used_in_class (identifier, type_decl);
10720     }
10721
10722   return type_decl;
10723 }
10724
10725
10726 /* Parse an elaborated-type-specifier.  Note that the grammar given
10727    here incorporates the resolution to DR68.
10728
10729    elaborated-type-specifier:
10730      class-key :: [opt] nested-name-specifier [opt] identifier
10731      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10732      enum :: [opt] nested-name-specifier [opt] identifier
10733      typename :: [opt] nested-name-specifier identifier
10734      typename :: [opt] nested-name-specifier template [opt]
10735        template-id
10736
10737    GNU extension:
10738
10739    elaborated-type-specifier:
10740      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10741      class-key attributes :: [opt] nested-name-specifier [opt]
10742                template [opt] template-id
10743      enum attributes :: [opt] nested-name-specifier [opt] identifier
10744
10745    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10746    declared `friend'.  If IS_DECLARATION is TRUE, then this
10747    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10748    something is being declared.
10749
10750    Returns the TYPE specified.  */
10751
10752 static tree
10753 cp_parser_elaborated_type_specifier (cp_parser* parser,
10754                                      bool is_friend,
10755                                      bool is_declaration)
10756 {
10757   enum tag_types tag_type;
10758   tree identifier;
10759   tree type = NULL_TREE;
10760   tree attributes = NULL_TREE;
10761
10762   /* See if we're looking at the `enum' keyword.  */
10763   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10764     {
10765       /* Consume the `enum' token.  */
10766       cp_lexer_consume_token (parser->lexer);
10767       /* Remember that it's an enumeration type.  */
10768       tag_type = enum_type;
10769       /* Parse the attributes.  */
10770       attributes = cp_parser_attributes_opt (parser);
10771     }
10772   /* Or, it might be `typename'.  */
10773   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10774                                            RID_TYPENAME))
10775     {
10776       /* Consume the `typename' token.  */
10777       cp_lexer_consume_token (parser->lexer);
10778       /* Remember that it's a `typename' type.  */
10779       tag_type = typename_type;
10780       /* The `typename' keyword is only allowed in templates.  */
10781       if (!processing_template_decl)
10782         pedwarn ("using %<typename%> outside of template");
10783     }
10784   /* Otherwise it must be a class-key.  */
10785   else
10786     {
10787       tag_type = cp_parser_class_key (parser);
10788       if (tag_type == none_type)
10789         return error_mark_node;
10790       /* Parse the attributes.  */
10791       attributes = cp_parser_attributes_opt (parser);
10792     }
10793
10794   /* Look for the `::' operator.  */
10795   cp_parser_global_scope_opt (parser,
10796                               /*current_scope_valid_p=*/false);
10797   /* Look for the nested-name-specifier.  */
10798   if (tag_type == typename_type)
10799     {
10800       if (!cp_parser_nested_name_specifier (parser,
10801                                            /*typename_keyword_p=*/true,
10802                                            /*check_dependency_p=*/true,
10803                                            /*type_p=*/true,
10804                                             is_declaration))
10805         return error_mark_node;
10806     }
10807   else
10808     /* Even though `typename' is not present, the proposed resolution
10809        to Core Issue 180 says that in `class A<T>::B', `B' should be
10810        considered a type-name, even if `A<T>' is dependent.  */
10811     cp_parser_nested_name_specifier_opt (parser,
10812                                          /*typename_keyword_p=*/true,
10813                                          /*check_dependency_p=*/true,
10814                                          /*type_p=*/true,
10815                                          is_declaration);
10816  /* For everything but enumeration types, consider a template-id.
10817     For an enumeration type, consider only a plain identifier.  */
10818   if (tag_type != enum_type)
10819     {
10820       bool template_p = false;
10821       tree decl;
10822
10823       /* Allow the `template' keyword.  */
10824       template_p = cp_parser_optional_template_keyword (parser);
10825       /* If we didn't see `template', we don't know if there's a
10826          template-id or not.  */
10827       if (!template_p)
10828         cp_parser_parse_tentatively (parser);
10829       /* Parse the template-id.  */
10830       decl = cp_parser_template_id (parser, template_p,
10831                                     /*check_dependency_p=*/true,
10832                                     is_declaration);
10833       /* If we didn't find a template-id, look for an ordinary
10834          identifier.  */
10835       if (!template_p && !cp_parser_parse_definitely (parser))
10836         ;
10837       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10838          in effect, then we must assume that, upon instantiation, the
10839          template will correspond to a class.  */
10840       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10841                && tag_type == typename_type)
10842         type = make_typename_type (parser->scope, decl,
10843                                    typename_type,
10844                                    /*complain=*/tf_error);
10845       else
10846         type = TREE_TYPE (decl);
10847     }
10848
10849   if (!type)
10850     {
10851       identifier = cp_parser_identifier (parser);
10852
10853       if (identifier == error_mark_node)
10854         {
10855           parser->scope = NULL_TREE;
10856           return error_mark_node;
10857         }
10858
10859       /* For a `typename', we needn't call xref_tag.  */
10860       if (tag_type == typename_type
10861           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10862         return cp_parser_make_typename_type (parser, parser->scope,
10863                                              identifier);
10864       /* Look up a qualified name in the usual way.  */
10865       if (parser->scope)
10866         {
10867           tree decl;
10868           tree ambiguous_decls;
10869
10870           decl = cp_parser_lookup_name (parser, identifier,
10871                                         tag_type,
10872                                         /*is_template=*/false,
10873                                         /*is_namespace=*/false,
10874                                         /*check_dependency=*/true,
10875                                         &ambiguous_decls);
10876
10877           /* If the lookup was ambiguous, an error will already have been
10878              issued.  */
10879           if (ambiguous_decls)
10880             return error_mark_node;
10881
10882           /* If we are parsing friend declaration, DECL may be a
10883              TEMPLATE_DECL tree node here.  However, we need to check
10884              whether this TEMPLATE_DECL results in valid code.  Consider
10885              the following example:
10886
10887                namespace N {
10888                  template <class T> class C {};
10889                }
10890                class X {
10891                  template <class T> friend class N::C; // #1, valid code
10892                };
10893                template <class T> class Y {
10894                  friend class N::C;                    // #2, invalid code
10895                };
10896
10897              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10898              name lookup of `N::C'.  We see that friend declaration must
10899              be template for the code to be valid.  Note that
10900              processing_template_decl does not work here since it is
10901              always 1 for the above two cases.  */
10902
10903           decl = (cp_parser_maybe_treat_template_as_class
10904                   (decl, /*tag_name_p=*/is_friend
10905                          && parser->num_template_parameter_lists));
10906
10907           if (TREE_CODE (decl) != TYPE_DECL)
10908             {
10909               cp_parser_diagnose_invalid_type_name (parser,
10910                                                     parser->scope,
10911                                                     identifier);
10912               return error_mark_node;
10913             }
10914
10915           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10916             {
10917               bool allow_template = (parser->num_template_parameter_lists
10918                                       || DECL_SELF_REFERENCE_P (decl));
10919               type = check_elaborated_type_specifier (tag_type, decl, 
10920                                                       allow_template);
10921
10922               if (type == error_mark_node)
10923                 return error_mark_node;
10924             }
10925
10926           /* Forward declarations of nested types, such as
10927
10928                class C1::C2;
10929                class C1::C2::C3;
10930
10931              are invalid unless all components preceding the final '::'
10932              are complete.  If all enclosing types are complete, these
10933              declarations become merely pointless.
10934
10935              Invalid forward declarations of nested types are errors
10936              caught elsewhere in parsing.  Those that are pointless arrive
10937              here.  */
10938
10939           if (cp_parser_declares_only_class_p (parser)
10940               && !is_friend && !processing_explicit_instantiation)
10941             warning (0, "declaration %qD does not declare anything", decl);
10942
10943           type = TREE_TYPE (decl);
10944         }
10945       else
10946         {
10947           /* An elaborated-type-specifier sometimes introduces a new type and
10948              sometimes names an existing type.  Normally, the rule is that it
10949              introduces a new type only if there is not an existing type of
10950              the same name already in scope.  For example, given:
10951
10952                struct S {};
10953                void f() { struct S s; }
10954
10955              the `struct S' in the body of `f' is the same `struct S' as in
10956              the global scope; the existing definition is used.  However, if
10957              there were no global declaration, this would introduce a new
10958              local class named `S'.
10959
10960              An exception to this rule applies to the following code:
10961
10962                namespace N { struct S; }
10963
10964              Here, the elaborated-type-specifier names a new type
10965              unconditionally; even if there is already an `S' in the
10966              containing scope this declaration names a new type.
10967              This exception only applies if the elaborated-type-specifier
10968              forms the complete declaration:
10969
10970                [class.name]
10971
10972                A declaration consisting solely of `class-key identifier ;' is
10973                either a redeclaration of the name in the current scope or a
10974                forward declaration of the identifier as a class name.  It
10975                introduces the name into the current scope.
10976
10977              We are in this situation precisely when the next token is a `;'.
10978
10979              An exception to the exception is that a `friend' declaration does
10980              *not* name a new type; i.e., given:
10981
10982                struct S { friend struct T; };
10983
10984              `T' is not a new type in the scope of `S'.
10985
10986              Also, `new struct S' or `sizeof (struct S)' never results in the
10987              definition of a new type; a new type can only be declared in a
10988              declaration context.  */
10989
10990           tag_scope ts;
10991           bool template_p;
10992
10993           if (is_friend)
10994             /* Friends have special name lookup rules.  */
10995             ts = ts_within_enclosing_non_class;
10996           else if (is_declaration
10997                    && cp_lexer_next_token_is (parser->lexer,
10998                                               CPP_SEMICOLON))
10999             /* This is a `class-key identifier ;' */
11000             ts = ts_current;
11001           else
11002             ts = ts_global;
11003
11004           template_p =
11005             (parser->num_template_parameter_lists
11006              && (cp_parser_next_token_starts_class_definition_p (parser)
11007                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11008           /* An unqualified name was used to reference this type, so
11009              there were no qualifying templates.  */
11010           if (!cp_parser_check_template_parameters (parser,
11011                                                     /*num_templates=*/0))
11012             return error_mark_node;
11013           type = xref_tag (tag_type, identifier, ts, template_p);
11014         }
11015     }
11016
11017   if (type == error_mark_node)
11018     return error_mark_node;
11019
11020   /* Allow attributes on forward declarations of classes.  */
11021   if (attributes)
11022     {
11023       if (TREE_CODE (type) == TYPENAME_TYPE)
11024         warning (OPT_Wattributes,
11025                  "attributes ignored on uninstantiated type");
11026       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11027                && ! processing_explicit_instantiation)
11028         warning (OPT_Wattributes,
11029                  "attributes ignored on template instantiation");
11030       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11031         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11032       else
11033         warning (OPT_Wattributes,
11034                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11035     }
11036
11037   if (tag_type != enum_type)
11038     cp_parser_check_class_key (tag_type, type);
11039
11040   /* A "<" cannot follow an elaborated type specifier.  If that
11041      happens, the user was probably trying to form a template-id.  */
11042   cp_parser_check_for_invalid_template_id (parser, type);
11043
11044   return type;
11045 }
11046
11047 /* Parse an enum-specifier.
11048
11049    enum-specifier:
11050      enum identifier [opt] { enumerator-list [opt] }
11051
11052    GNU Extensions:
11053      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11054        attributes[opt]
11055
11056    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11057    if the token stream isn't an enum-specifier after all.  */
11058
11059 static tree
11060 cp_parser_enum_specifier (cp_parser* parser)
11061 {
11062   tree identifier;
11063   tree type;
11064   tree attributes;
11065
11066   /* Parse tentatively so that we can back up if we don't find a
11067      enum-specifier.  */
11068   cp_parser_parse_tentatively (parser);
11069
11070   /* Caller guarantees that the current token is 'enum', an identifier
11071      possibly follows, and the token after that is an opening brace.
11072      If we don't have an identifier, fabricate an anonymous name for
11073      the enumeration being defined.  */
11074   cp_lexer_consume_token (parser->lexer);
11075
11076   attributes = cp_parser_attributes_opt (parser);
11077
11078   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11079     identifier = cp_parser_identifier (parser);
11080   else
11081     identifier = make_anon_name ();
11082
11083   /* Look for the `{' but don't consume it yet.  */
11084   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11085     cp_parser_simulate_error (parser);
11086
11087   if (!cp_parser_parse_definitely (parser))
11088     return NULL_TREE;
11089
11090   /* Issue an error message if type-definitions are forbidden here.  */
11091   if (!cp_parser_check_type_definition (parser))
11092     type = error_mark_node;
11093   else
11094     /* Create the new type.  We do this before consuming the opening
11095        brace so the enum will be recorded as being on the line of its
11096        tag (or the 'enum' keyword, if there is no tag).  */
11097     type = start_enum (identifier);
11098   
11099   /* Consume the opening brace.  */
11100   cp_lexer_consume_token (parser->lexer);
11101
11102   if (type == error_mark_node)
11103     {
11104       cp_parser_skip_to_end_of_block_or_statement (parser);
11105       return error_mark_node;
11106     }
11107
11108   /* If the next token is not '}', then there are some enumerators.  */
11109   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11110     cp_parser_enumerator_list (parser, type);
11111
11112   /* Consume the final '}'.  */
11113   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11114
11115   /* Look for trailing attributes to apply to this enumeration, and
11116      apply them if appropriate.  */
11117   if (cp_parser_allow_gnu_extensions_p (parser))
11118     {
11119       tree trailing_attr = cp_parser_attributes_opt (parser);
11120       cplus_decl_attributes (&type,
11121                              trailing_attr,
11122                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11123     }
11124
11125   /* Finish up the enumeration.  */
11126   finish_enum (type);
11127
11128   return type;
11129 }
11130
11131 /* Parse an enumerator-list.  The enumerators all have the indicated
11132    TYPE.
11133
11134    enumerator-list:
11135      enumerator-definition
11136      enumerator-list , enumerator-definition  */
11137
11138 static void
11139 cp_parser_enumerator_list (cp_parser* parser, tree type)
11140 {
11141   while (true)
11142     {
11143       /* Parse an enumerator-definition.  */
11144       cp_parser_enumerator_definition (parser, type);
11145
11146       /* If the next token is not a ',', we've reached the end of
11147          the list.  */
11148       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11149         break;
11150       /* Otherwise, consume the `,' and keep going.  */
11151       cp_lexer_consume_token (parser->lexer);
11152       /* If the next token is a `}', there is a trailing comma.  */
11153       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11154         {
11155           if (pedantic && !in_system_header)
11156             pedwarn ("comma at end of enumerator list");
11157           break;
11158         }
11159     }
11160 }
11161
11162 /* Parse an enumerator-definition.  The enumerator has the indicated
11163    TYPE.
11164
11165    enumerator-definition:
11166      enumerator
11167      enumerator = constant-expression
11168
11169    enumerator:
11170      identifier  */
11171
11172 static void
11173 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11174 {
11175   tree identifier;
11176   tree value;
11177
11178   /* Look for the identifier.  */
11179   identifier = cp_parser_identifier (parser);
11180   if (identifier == error_mark_node)
11181     return;
11182
11183   /* If the next token is an '=', then there is an explicit value.  */
11184   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11185     {
11186       /* Consume the `=' token.  */
11187       cp_lexer_consume_token (parser->lexer);
11188       /* Parse the value.  */
11189       value = cp_parser_constant_expression (parser,
11190                                              /*allow_non_constant_p=*/false,
11191                                              NULL);
11192     }
11193   else
11194     value = NULL_TREE;
11195
11196   /* Create the enumerator.  */
11197   build_enumerator (identifier, value, type);
11198 }
11199
11200 /* Parse a namespace-name.
11201
11202    namespace-name:
11203      original-namespace-name
11204      namespace-alias
11205
11206    Returns the NAMESPACE_DECL for the namespace.  */
11207
11208 static tree
11209 cp_parser_namespace_name (cp_parser* parser)
11210 {
11211   tree identifier;
11212   tree namespace_decl;
11213
11214   /* Get the name of the namespace.  */
11215   identifier = cp_parser_identifier (parser);
11216   if (identifier == error_mark_node)
11217     return error_mark_node;
11218
11219   /* Look up the identifier in the currently active scope.  Look only
11220      for namespaces, due to:
11221
11222        [basic.lookup.udir]
11223
11224        When looking up a namespace-name in a using-directive or alias
11225        definition, only namespace names are considered.
11226
11227      And:
11228
11229        [basic.lookup.qual]
11230
11231        During the lookup of a name preceding the :: scope resolution
11232        operator, object, function, and enumerator names are ignored.
11233
11234      (Note that cp_parser_class_or_namespace_name only calls this
11235      function if the token after the name is the scope resolution
11236      operator.)  */
11237   namespace_decl = cp_parser_lookup_name (parser, identifier,
11238                                           none_type,
11239                                           /*is_template=*/false,
11240                                           /*is_namespace=*/true,
11241                                           /*check_dependency=*/true,
11242                                           /*ambiguous_decls=*/NULL);
11243   /* If it's not a namespace, issue an error.  */
11244   if (namespace_decl == error_mark_node
11245       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11246     {
11247       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11248         error ("%qD is not a namespace-name", identifier);
11249       cp_parser_error (parser, "expected namespace-name");
11250       namespace_decl = error_mark_node;
11251     }
11252
11253   return namespace_decl;
11254 }
11255
11256 /* Parse a namespace-definition.
11257
11258    namespace-definition:
11259      named-namespace-definition
11260      unnamed-namespace-definition
11261
11262    named-namespace-definition:
11263      original-namespace-definition
11264      extension-namespace-definition
11265
11266    original-namespace-definition:
11267      namespace identifier { namespace-body }
11268
11269    extension-namespace-definition:
11270      namespace original-namespace-name { namespace-body }
11271
11272    unnamed-namespace-definition:
11273      namespace { namespace-body } */
11274
11275 static void
11276 cp_parser_namespace_definition (cp_parser* parser)
11277 {
11278   tree identifier, attribs;
11279
11280   /* Look for the `namespace' keyword.  */
11281   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11282
11283   /* Get the name of the namespace.  We do not attempt to distinguish
11284      between an original-namespace-definition and an
11285      extension-namespace-definition at this point.  The semantic
11286      analysis routines are responsible for that.  */
11287   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11288     identifier = cp_parser_identifier (parser);
11289   else
11290     identifier = NULL_TREE;
11291
11292   /* Parse any specified attributes.  */
11293   attribs = cp_parser_attributes_opt (parser);
11294
11295   /* Look for the `{' to start the namespace.  */
11296   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11297   /* Start the namespace.  */
11298   push_namespace_with_attribs (identifier, attribs);
11299   /* Parse the body of the namespace.  */
11300   cp_parser_namespace_body (parser);
11301   /* Finish the namespace.  */
11302   pop_namespace ();
11303   /* Look for the final `}'.  */
11304   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11305 }
11306
11307 /* Parse a namespace-body.
11308
11309    namespace-body:
11310      declaration-seq [opt]  */
11311
11312 static void
11313 cp_parser_namespace_body (cp_parser* parser)
11314 {
11315   cp_parser_declaration_seq_opt (parser);
11316 }
11317
11318 /* Parse a namespace-alias-definition.
11319
11320    namespace-alias-definition:
11321      namespace identifier = qualified-namespace-specifier ;  */
11322
11323 static void
11324 cp_parser_namespace_alias_definition (cp_parser* parser)
11325 {
11326   tree identifier;
11327   tree namespace_specifier;
11328
11329   /* Look for the `namespace' keyword.  */
11330   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11331   /* Look for the identifier.  */
11332   identifier = cp_parser_identifier (parser);
11333   if (identifier == error_mark_node)
11334     return;
11335   /* Look for the `=' token.  */
11336   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11337       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11338     {
11339       error ("%<namespace%> definition is not allowed here");
11340       /* Skip the definition.  */
11341       cp_lexer_consume_token (parser->lexer);
11342       if (cp_parser_skip_to_closing_brace (parser))
11343         cp_lexer_consume_token (parser->lexer);
11344       return;
11345     }
11346   cp_parser_require (parser, CPP_EQ, "`='");
11347   /* Look for the qualified-namespace-specifier.  */
11348   namespace_specifier
11349     = cp_parser_qualified_namespace_specifier (parser);
11350   /* Look for the `;' token.  */
11351   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11352
11353   /* Register the alias in the symbol table.  */
11354   do_namespace_alias (identifier, namespace_specifier);
11355 }
11356
11357 /* Parse a qualified-namespace-specifier.
11358
11359    qualified-namespace-specifier:
11360      :: [opt] nested-name-specifier [opt] namespace-name
11361
11362    Returns a NAMESPACE_DECL corresponding to the specified
11363    namespace.  */
11364
11365 static tree
11366 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11367 {
11368   /* Look for the optional `::'.  */
11369   cp_parser_global_scope_opt (parser,
11370                               /*current_scope_valid_p=*/false);
11371
11372   /* Look for the optional nested-name-specifier.  */
11373   cp_parser_nested_name_specifier_opt (parser,
11374                                        /*typename_keyword_p=*/false,
11375                                        /*check_dependency_p=*/true,
11376                                        /*type_p=*/false,
11377                                        /*is_declaration=*/true);
11378
11379   return cp_parser_namespace_name (parser);
11380 }
11381
11382 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11383    access declaration.
11384
11385    using-declaration:
11386      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11387      using :: unqualified-id ;  
11388
11389    access-declaration:
11390      qualified-id ;  
11391
11392    */
11393
11394 static bool
11395 cp_parser_using_declaration (cp_parser* parser, 
11396                              bool access_declaration_p)
11397 {
11398   cp_token *token;
11399   bool typename_p = false;
11400   bool global_scope_p;
11401   tree decl;
11402   tree identifier;
11403   tree qscope;
11404
11405   if (access_declaration_p)
11406     cp_parser_parse_tentatively (parser);
11407   else
11408     {
11409       /* Look for the `using' keyword.  */
11410       cp_parser_require_keyword (parser, RID_USING, "`using'");
11411       
11412       /* Peek at the next token.  */
11413       token = cp_lexer_peek_token (parser->lexer);
11414       /* See if it's `typename'.  */
11415       if (token->keyword == RID_TYPENAME)
11416         {
11417           /* Remember that we've seen it.  */
11418           typename_p = true;
11419           /* Consume the `typename' token.  */
11420           cp_lexer_consume_token (parser->lexer);
11421         }
11422     }
11423
11424   /* Look for the optional global scope qualification.  */
11425   global_scope_p
11426     = (cp_parser_global_scope_opt (parser,
11427                                    /*current_scope_valid_p=*/false)
11428        != NULL_TREE);
11429
11430   /* If we saw `typename', or didn't see `::', then there must be a
11431      nested-name-specifier present.  */
11432   if (typename_p || !global_scope_p)
11433     qscope = cp_parser_nested_name_specifier (parser, typename_p,
11434                                               /*check_dependency_p=*/true,
11435                                               /*type_p=*/false,
11436                                               /*is_declaration=*/true);
11437   /* Otherwise, we could be in either of the two productions.  In that
11438      case, treat the nested-name-specifier as optional.  */
11439   else
11440     qscope = cp_parser_nested_name_specifier_opt (parser,
11441                                                   /*typename_keyword_p=*/false,
11442                                                   /*check_dependency_p=*/true,
11443                                                   /*type_p=*/false,
11444                                                   /*is_declaration=*/true);
11445   if (!qscope)
11446     qscope = global_namespace;
11447
11448   if (access_declaration_p && cp_parser_error_occurred (parser))
11449     /* Something has already gone wrong; there's no need to parse
11450        further.  Since an error has occurred, the return value of
11451        cp_parser_parse_definitely will be false, as required.  */
11452     return cp_parser_parse_definitely (parser);
11453
11454   /* Parse the unqualified-id.  */
11455   identifier = cp_parser_unqualified_id (parser,
11456                                          /*template_keyword_p=*/false,
11457                                          /*check_dependency_p=*/true,
11458                                          /*declarator_p=*/true,
11459                                          /*optional_p=*/false);
11460
11461   if (access_declaration_p)
11462     {
11463       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11464         cp_parser_simulate_error (parser);
11465       if (!cp_parser_parse_definitely (parser))
11466         return false;
11467     }
11468
11469   /* The function we call to handle a using-declaration is different
11470      depending on what scope we are in.  */
11471   if (qscope == error_mark_node || identifier == error_mark_node)
11472     ;
11473   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11474            && TREE_CODE (identifier) != BIT_NOT_EXPR)
11475     /* [namespace.udecl]
11476
11477        A using declaration shall not name a template-id.  */
11478     error ("a template-id may not appear in a using-declaration");
11479   else
11480     {
11481       if (at_class_scope_p ())
11482         {
11483           /* Create the USING_DECL.  */
11484           decl = do_class_using_decl (parser->scope, identifier);
11485           /* Add it to the list of members in this class.  */
11486           finish_member_declaration (decl);
11487         }
11488       else
11489         {
11490           decl = cp_parser_lookup_name_simple (parser, identifier);
11491           if (decl == error_mark_node)
11492             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11493           else if (!at_namespace_scope_p ())
11494             do_local_using_decl (decl, qscope, identifier);
11495           else
11496             do_toplevel_using_decl (decl, qscope, identifier);
11497         }
11498     }
11499
11500   /* Look for the final `;'.  */
11501   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11502   
11503   return true;
11504 }
11505
11506 /* Parse a using-directive.
11507
11508    using-directive:
11509      using namespace :: [opt] nested-name-specifier [opt]
11510        namespace-name ;  */
11511
11512 static void
11513 cp_parser_using_directive (cp_parser* parser)
11514 {
11515   tree namespace_decl;
11516   tree attribs;
11517
11518   /* Look for the `using' keyword.  */
11519   cp_parser_require_keyword (parser, RID_USING, "`using'");
11520   /* And the `namespace' keyword.  */
11521   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11522   /* Look for the optional `::' operator.  */
11523   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11524   /* And the optional nested-name-specifier.  */
11525   cp_parser_nested_name_specifier_opt (parser,
11526                                        /*typename_keyword_p=*/false,
11527                                        /*check_dependency_p=*/true,
11528                                        /*type_p=*/false,
11529                                        /*is_declaration=*/true);
11530   /* Get the namespace being used.  */
11531   namespace_decl = cp_parser_namespace_name (parser);
11532   /* And any specified attributes.  */
11533   attribs = cp_parser_attributes_opt (parser);
11534   /* Update the symbol table.  */
11535   parse_using_directive (namespace_decl, attribs);
11536   /* Look for the final `;'.  */
11537   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11538 }
11539
11540 /* Parse an asm-definition.
11541
11542    asm-definition:
11543      asm ( string-literal ) ;
11544
11545    GNU Extension:
11546
11547    asm-definition:
11548      asm volatile [opt] ( string-literal ) ;
11549      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11550      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11551                           : asm-operand-list [opt] ) ;
11552      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11553                           : asm-operand-list [opt]
11554                           : asm-operand-list [opt] ) ;  */
11555
11556 static void
11557 cp_parser_asm_definition (cp_parser* parser)
11558 {
11559   tree string;
11560   tree outputs = NULL_TREE;
11561   tree inputs = NULL_TREE;
11562   tree clobbers = NULL_TREE;
11563   tree asm_stmt;
11564   bool volatile_p = false;
11565   bool extended_p = false;
11566
11567   /* Look for the `asm' keyword.  */
11568   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11569   /* See if the next token is `volatile'.  */
11570   if (cp_parser_allow_gnu_extensions_p (parser)
11571       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11572     {
11573       /* Remember that we saw the `volatile' keyword.  */
11574       volatile_p = true;
11575       /* Consume the token.  */
11576       cp_lexer_consume_token (parser->lexer);
11577     }
11578   /* Look for the opening `('.  */
11579   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11580     return;
11581   /* Look for the string.  */
11582   string = cp_parser_string_literal (parser, false, false);
11583   if (string == error_mark_node)
11584     {
11585       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11586                                              /*consume_paren=*/true);
11587       return;
11588     }
11589
11590   /* If we're allowing GNU extensions, check for the extended assembly
11591      syntax.  Unfortunately, the `:' tokens need not be separated by
11592      a space in C, and so, for compatibility, we tolerate that here
11593      too.  Doing that means that we have to treat the `::' operator as
11594      two `:' tokens.  */
11595   if (cp_parser_allow_gnu_extensions_p (parser)
11596       && parser->in_function_body
11597       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11598           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11599     {
11600       bool inputs_p = false;
11601       bool clobbers_p = false;
11602
11603       /* The extended syntax was used.  */
11604       extended_p = true;
11605
11606       /* Look for outputs.  */
11607       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11608         {
11609           /* Consume the `:'.  */
11610           cp_lexer_consume_token (parser->lexer);
11611           /* Parse the output-operands.  */
11612           if (cp_lexer_next_token_is_not (parser->lexer,
11613                                           CPP_COLON)
11614               && cp_lexer_next_token_is_not (parser->lexer,
11615                                              CPP_SCOPE)
11616               && cp_lexer_next_token_is_not (parser->lexer,
11617                                              CPP_CLOSE_PAREN))
11618             outputs = cp_parser_asm_operand_list (parser);
11619         }
11620       /* If the next token is `::', there are no outputs, and the
11621          next token is the beginning of the inputs.  */
11622       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11623         /* The inputs are coming next.  */
11624         inputs_p = true;
11625
11626       /* Look for inputs.  */
11627       if (inputs_p
11628           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11629         {
11630           /* Consume the `:' or `::'.  */
11631           cp_lexer_consume_token (parser->lexer);
11632           /* Parse the output-operands.  */
11633           if (cp_lexer_next_token_is_not (parser->lexer,
11634                                           CPP_COLON)
11635               && cp_lexer_next_token_is_not (parser->lexer,
11636                                              CPP_CLOSE_PAREN))
11637             inputs = cp_parser_asm_operand_list (parser);
11638         }
11639       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11640         /* The clobbers are coming next.  */
11641         clobbers_p = true;
11642
11643       /* Look for clobbers.  */
11644       if (clobbers_p
11645           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11646         {
11647           /* Consume the `:' or `::'.  */
11648           cp_lexer_consume_token (parser->lexer);
11649           /* Parse the clobbers.  */
11650           if (cp_lexer_next_token_is_not (parser->lexer,
11651                                           CPP_CLOSE_PAREN))
11652             clobbers = cp_parser_asm_clobber_list (parser);
11653         }
11654     }
11655   /* Look for the closing `)'.  */
11656   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11657     cp_parser_skip_to_closing_parenthesis (parser, true, false,
11658                                            /*consume_paren=*/true);
11659   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11660
11661   /* Create the ASM_EXPR.  */
11662   if (parser->in_function_body)
11663     {
11664       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11665                                   inputs, clobbers);
11666       /* If the extended syntax was not used, mark the ASM_EXPR.  */
11667       if (!extended_p)
11668         {
11669           tree temp = asm_stmt;
11670           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11671             temp = TREE_OPERAND (temp, 0);
11672
11673           ASM_INPUT_P (temp) = 1;
11674         }
11675     }
11676   else
11677     cgraph_add_asm_node (string);
11678 }
11679
11680 /* Declarators [gram.dcl.decl] */
11681
11682 /* Parse an init-declarator.
11683
11684    init-declarator:
11685      declarator initializer [opt]
11686
11687    GNU Extension:
11688
11689    init-declarator:
11690      declarator asm-specification [opt] attributes [opt] initializer [opt]
11691
11692    function-definition:
11693      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11694        function-body
11695      decl-specifier-seq [opt] declarator function-try-block
11696
11697    GNU Extension:
11698
11699    function-definition:
11700      __extension__ function-definition
11701
11702    The DECL_SPECIFIERS apply to this declarator.  Returns a
11703    representation of the entity declared.  If MEMBER_P is TRUE, then
11704    this declarator appears in a class scope.  The new DECL created by
11705    this declarator is returned.
11706
11707    The CHECKS are access checks that should be performed once we know
11708    what entity is being declared (and, therefore, what classes have
11709    befriended it).
11710
11711    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11712    for a function-definition here as well.  If the declarator is a
11713    declarator for a function-definition, *FUNCTION_DEFINITION_P will
11714    be TRUE upon return.  By that point, the function-definition will
11715    have been completely parsed.
11716
11717    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11718    is FALSE.  */
11719
11720 static tree
11721 cp_parser_init_declarator (cp_parser* parser,
11722                            cp_decl_specifier_seq *decl_specifiers,
11723                            VEC (deferred_access_check,gc)* checks,
11724                            bool function_definition_allowed_p,
11725                            bool member_p,
11726                            int declares_class_or_enum,
11727                            bool* function_definition_p)
11728 {
11729   cp_token *token;
11730   cp_declarator *declarator;
11731   tree prefix_attributes;
11732   tree attributes;
11733   tree asm_specification;
11734   tree initializer;
11735   tree decl = NULL_TREE;
11736   tree scope;
11737   bool is_initialized;
11738   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
11739      initialized with "= ..", CPP_OPEN_PAREN if initialized with
11740      "(...)".  */
11741   enum cpp_ttype initialization_kind;
11742   bool is_parenthesized_init = false;
11743   bool is_non_constant_init;
11744   int ctor_dtor_or_conv_p;
11745   bool friend_p;
11746   tree pushed_scope = NULL;
11747
11748   /* Gather the attributes that were provided with the
11749      decl-specifiers.  */
11750   prefix_attributes = decl_specifiers->attributes;
11751
11752   /* Assume that this is not the declarator for a function
11753      definition.  */
11754   if (function_definition_p)
11755     *function_definition_p = false;
11756
11757   /* Defer access checks while parsing the declarator; we cannot know
11758      what names are accessible until we know what is being
11759      declared.  */
11760   resume_deferring_access_checks ();
11761
11762   /* Parse the declarator.  */
11763   declarator
11764     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11765                             &ctor_dtor_or_conv_p,
11766                             /*parenthesized_p=*/NULL,
11767                             /*member_p=*/false);
11768   /* Gather up the deferred checks.  */
11769   stop_deferring_access_checks ();
11770
11771   /* If the DECLARATOR was erroneous, there's no need to go
11772      further.  */
11773   if (declarator == cp_error_declarator)
11774     return error_mark_node;
11775
11776   /* Check that the number of template-parameter-lists is OK.  */
11777   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11778     return error_mark_node;
11779
11780   if (declares_class_or_enum & 2)
11781     cp_parser_check_for_definition_in_return_type (declarator,
11782                                                    decl_specifiers->type);
11783
11784   /* Figure out what scope the entity declared by the DECLARATOR is
11785      located in.  `grokdeclarator' sometimes changes the scope, so
11786      we compute it now.  */
11787   scope = get_scope_of_declarator (declarator);
11788
11789   /* If we're allowing GNU extensions, look for an asm-specification
11790      and attributes.  */
11791   if (cp_parser_allow_gnu_extensions_p (parser))
11792     {
11793       /* Look for an asm-specification.  */
11794       asm_specification = cp_parser_asm_specification_opt (parser);
11795       /* And attributes.  */
11796       attributes = cp_parser_attributes_opt (parser);
11797     }
11798   else
11799     {
11800       asm_specification = NULL_TREE;
11801       attributes = NULL_TREE;
11802     }
11803
11804   /* Peek at the next token.  */
11805   token = cp_lexer_peek_token (parser->lexer);
11806   /* Check to see if the token indicates the start of a
11807      function-definition.  */
11808   if (cp_parser_token_starts_function_definition_p (token))
11809     {
11810       if (!function_definition_allowed_p)
11811         {
11812           /* If a function-definition should not appear here, issue an
11813              error message.  */
11814           cp_parser_error (parser,
11815                            "a function-definition is not allowed here");
11816           return error_mark_node;
11817         }
11818       else
11819         {
11820           /* Neither attributes nor an asm-specification are allowed
11821              on a function-definition.  */
11822           if (asm_specification)
11823             error ("an asm-specification is not allowed on a function-definition");
11824           if (attributes)
11825             error ("attributes are not allowed on a function-definition");
11826           /* This is a function-definition.  */
11827           *function_definition_p = true;
11828
11829           /* Parse the function definition.  */
11830           if (member_p)
11831             decl = cp_parser_save_member_function_body (parser,
11832                                                         decl_specifiers,
11833                                                         declarator,
11834                                                         prefix_attributes);
11835           else
11836             decl
11837               = (cp_parser_function_definition_from_specifiers_and_declarator
11838                  (parser, decl_specifiers, prefix_attributes, declarator));
11839
11840           return decl;
11841         }
11842     }
11843
11844   /* [dcl.dcl]
11845
11846      Only in function declarations for constructors, destructors, and
11847      type conversions can the decl-specifier-seq be omitted.
11848
11849      We explicitly postpone this check past the point where we handle
11850      function-definitions because we tolerate function-definitions
11851      that are missing their return types in some modes.  */
11852   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11853     {
11854       cp_parser_error (parser,
11855                        "expected constructor, destructor, or type conversion");
11856       return error_mark_node;
11857     }
11858
11859   /* An `=' or an `(' indicates an initializer.  */
11860   if (token->type == CPP_EQ
11861       || token->type == CPP_OPEN_PAREN)
11862     {
11863       is_initialized = true;
11864       initialization_kind = token->type;
11865     }
11866   else
11867     {
11868       /* If the init-declarator isn't initialized and isn't followed by a
11869          `,' or `;', it's not a valid init-declarator.  */
11870       if (token->type != CPP_COMMA
11871           && token->type != CPP_SEMICOLON)
11872         {
11873           cp_parser_error (parser, "expected initializer");
11874           return error_mark_node;
11875         }
11876       is_initialized = false;
11877       initialization_kind = CPP_EOF;
11878     }
11879
11880   /* Because start_decl has side-effects, we should only call it if we
11881      know we're going ahead.  By this point, we know that we cannot
11882      possibly be looking at any other construct.  */
11883   cp_parser_commit_to_tentative_parse (parser);
11884
11885   /* If the decl specifiers were bad, issue an error now that we're
11886      sure this was intended to be a declarator.  Then continue
11887      declaring the variable(s), as int, to try to cut down on further
11888      errors.  */
11889   if (decl_specifiers->any_specifiers_p
11890       && decl_specifiers->type == error_mark_node)
11891     {
11892       cp_parser_error (parser, "invalid type in declaration");
11893       decl_specifiers->type = integer_type_node;
11894     }
11895
11896   /* Check to see whether or not this declaration is a friend.  */
11897   friend_p = cp_parser_friend_p (decl_specifiers);
11898
11899   /* Enter the newly declared entry in the symbol table.  If we're
11900      processing a declaration in a class-specifier, we wait until
11901      after processing the initializer.  */
11902   if (!member_p)
11903     {
11904       if (parser->in_unbraced_linkage_specification_p)
11905         decl_specifiers->storage_class = sc_extern;
11906       decl = start_decl (declarator, decl_specifiers,
11907                          is_initialized, attributes, prefix_attributes,
11908                          &pushed_scope);
11909     }
11910   else if (scope)
11911     /* Enter the SCOPE.  That way unqualified names appearing in the
11912        initializer will be looked up in SCOPE.  */
11913     pushed_scope = push_scope (scope);
11914
11915   /* Perform deferred access control checks, now that we know in which
11916      SCOPE the declared entity resides.  */
11917   if (!member_p && decl)
11918     {
11919       tree saved_current_function_decl = NULL_TREE;
11920
11921       /* If the entity being declared is a function, pretend that we
11922          are in its scope.  If it is a `friend', it may have access to
11923          things that would not otherwise be accessible.  */
11924       if (TREE_CODE (decl) == FUNCTION_DECL)
11925         {
11926           saved_current_function_decl = current_function_decl;
11927           current_function_decl = decl;
11928         }
11929
11930       /* Perform access checks for template parameters.  */
11931       cp_parser_perform_template_parameter_access_checks (checks);
11932
11933       /* Perform the access control checks for the declarator and the
11934          the decl-specifiers.  */
11935       perform_deferred_access_checks ();
11936
11937       /* Restore the saved value.  */
11938       if (TREE_CODE (decl) == FUNCTION_DECL)
11939         current_function_decl = saved_current_function_decl;
11940     }
11941
11942   /* Parse the initializer.  */
11943   initializer = NULL_TREE;
11944   is_parenthesized_init = false;
11945   is_non_constant_init = true;
11946   if (is_initialized)
11947     {
11948       if (function_declarator_p (declarator))
11949         {
11950            if (initialization_kind == CPP_EQ)
11951              initializer = cp_parser_pure_specifier (parser);
11952            else
11953              {
11954                /* If the declaration was erroneous, we don't really
11955                   know what the user intended, so just silently
11956                   consume the initializer.  */
11957                if (decl != error_mark_node)
11958                  error ("initializer provided for function");
11959                cp_parser_skip_to_closing_parenthesis (parser,
11960                                                       /*recovering=*/true,
11961                                                       /*or_comma=*/false,
11962                                                       /*consume_paren=*/true);
11963              }
11964         }
11965       else
11966         initializer = cp_parser_initializer (parser,
11967                                              &is_parenthesized_init,
11968                                              &is_non_constant_init);
11969     }
11970
11971   /* The old parser allows attributes to appear after a parenthesized
11972      initializer.  Mark Mitchell proposed removing this functionality
11973      on the GCC mailing lists on 2002-08-13.  This parser accepts the
11974      attributes -- but ignores them.  */
11975   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11976     if (cp_parser_attributes_opt (parser))
11977       warning (OPT_Wattributes,
11978                "attributes after parenthesized initializer ignored");
11979
11980   /* For an in-class declaration, use `grokfield' to create the
11981      declaration.  */
11982   if (member_p)
11983     {
11984       if (pushed_scope)
11985         {
11986           pop_scope (pushed_scope);
11987           pushed_scope = false;
11988         }
11989       decl = grokfield (declarator, decl_specifiers,
11990                         initializer, !is_non_constant_init,
11991                         /*asmspec=*/NULL_TREE,
11992                         prefix_attributes);
11993       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11994         cp_parser_save_default_args (parser, decl);
11995     }
11996
11997   /* Finish processing the declaration.  But, skip friend
11998      declarations.  */
11999   if (!friend_p && decl && decl != error_mark_node)
12000     {
12001       cp_finish_decl (decl,
12002                       initializer, !is_non_constant_init,
12003                       asm_specification,
12004                       /* If the initializer is in parentheses, then this is
12005                          a direct-initialization, which means that an
12006                          `explicit' constructor is OK.  Otherwise, an
12007                          `explicit' constructor cannot be used.  */
12008                       ((is_parenthesized_init || !is_initialized)
12009                      ? 0 : LOOKUP_ONLYCONVERTING));
12010     }
12011   else if ((cxx_dialect != cxx98) && friend_p
12012            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12013     /* Core issue #226 (C++0x only): A default template-argument
12014        shall not be specified in a friend class template
12015        declaration. */
12016     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12017                              /*is_partial=*/0, /*is_friend_decl=*/1);
12018
12019   if (!friend_p && pushed_scope)
12020     pop_scope (pushed_scope);
12021
12022   return decl;
12023 }
12024
12025 /* Parse a declarator.
12026
12027    declarator:
12028      direct-declarator
12029      ptr-operator declarator
12030
12031    abstract-declarator:
12032      ptr-operator abstract-declarator [opt]
12033      direct-abstract-declarator
12034
12035    GNU Extensions:
12036
12037    declarator:
12038      attributes [opt] direct-declarator
12039      attributes [opt] ptr-operator declarator
12040
12041    abstract-declarator:
12042      attributes [opt] ptr-operator abstract-declarator [opt]
12043      attributes [opt] direct-abstract-declarator
12044
12045    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12046    detect constructor, destructor or conversion operators. It is set
12047    to -1 if the declarator is a name, and +1 if it is a
12048    function. Otherwise it is set to zero. Usually you just want to
12049    test for >0, but internally the negative value is used.
12050
12051    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12052    a decl-specifier-seq unless it declares a constructor, destructor,
12053    or conversion.  It might seem that we could check this condition in
12054    semantic analysis, rather than parsing, but that makes it difficult
12055    to handle something like `f()'.  We want to notice that there are
12056    no decl-specifiers, and therefore realize that this is an
12057    expression, not a declaration.)
12058
12059    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12060    the declarator is a direct-declarator of the form "(...)".
12061
12062    MEMBER_P is true iff this declarator is a member-declarator.  */
12063
12064 static cp_declarator *
12065 cp_parser_declarator (cp_parser* parser,
12066                       cp_parser_declarator_kind dcl_kind,
12067                       int* ctor_dtor_or_conv_p,
12068                       bool* parenthesized_p,
12069                       bool member_p)
12070 {
12071   cp_token *token;
12072   cp_declarator *declarator;
12073   enum tree_code code;
12074   cp_cv_quals cv_quals;
12075   tree class_type;
12076   tree attributes = NULL_TREE;
12077
12078   /* Assume this is not a constructor, destructor, or type-conversion
12079      operator.  */
12080   if (ctor_dtor_or_conv_p)
12081     *ctor_dtor_or_conv_p = 0;
12082
12083   if (cp_parser_allow_gnu_extensions_p (parser))
12084     attributes = cp_parser_attributes_opt (parser);
12085
12086   /* Peek at the next token.  */
12087   token = cp_lexer_peek_token (parser->lexer);
12088
12089   /* Check for the ptr-operator production.  */
12090   cp_parser_parse_tentatively (parser);
12091   /* Parse the ptr-operator.  */
12092   code = cp_parser_ptr_operator (parser,
12093                                  &class_type,
12094                                  &cv_quals);
12095   /* If that worked, then we have a ptr-operator.  */
12096   if (cp_parser_parse_definitely (parser))
12097     {
12098       /* If a ptr-operator was found, then this declarator was not
12099          parenthesized.  */
12100       if (parenthesized_p)
12101         *parenthesized_p = true;
12102       /* The dependent declarator is optional if we are parsing an
12103          abstract-declarator.  */
12104       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12105         cp_parser_parse_tentatively (parser);
12106
12107       /* Parse the dependent declarator.  */
12108       declarator = cp_parser_declarator (parser, dcl_kind,
12109                                          /*ctor_dtor_or_conv_p=*/NULL,
12110                                          /*parenthesized_p=*/NULL,
12111                                          /*member_p=*/false);
12112
12113       /* If we are parsing an abstract-declarator, we must handle the
12114          case where the dependent declarator is absent.  */
12115       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12116           && !cp_parser_parse_definitely (parser))
12117         declarator = NULL;
12118
12119       declarator = cp_parser_make_indirect_declarator
12120         (code, class_type, cv_quals, declarator);
12121     }
12122   /* Everything else is a direct-declarator.  */
12123   else
12124     {
12125       if (parenthesized_p)
12126         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12127                                                    CPP_OPEN_PAREN);
12128       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12129                                                 ctor_dtor_or_conv_p,
12130                                                 member_p);
12131     }
12132
12133   if (attributes && declarator && declarator != cp_error_declarator)
12134     declarator->attributes = attributes;
12135
12136   return declarator;
12137 }
12138
12139 /* Parse a direct-declarator or direct-abstract-declarator.
12140
12141    direct-declarator:
12142      declarator-id
12143      direct-declarator ( parameter-declaration-clause )
12144        cv-qualifier-seq [opt]
12145        exception-specification [opt]
12146      direct-declarator [ constant-expression [opt] ]
12147      ( declarator )
12148
12149    direct-abstract-declarator:
12150      direct-abstract-declarator [opt]
12151        ( parameter-declaration-clause )
12152        cv-qualifier-seq [opt]
12153        exception-specification [opt]
12154      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12155      ( abstract-declarator )
12156
12157    Returns a representation of the declarator.  DCL_KIND is
12158    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12159    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12160    we are parsing a direct-declarator.  It is
12161    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12162    of ambiguity we prefer an abstract declarator, as per
12163    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12164    cp_parser_declarator.  */
12165
12166 static cp_declarator *
12167 cp_parser_direct_declarator (cp_parser* parser,
12168                              cp_parser_declarator_kind dcl_kind,
12169                              int* ctor_dtor_or_conv_p,
12170                              bool member_p)
12171 {
12172   cp_token *token;
12173   cp_declarator *declarator = NULL;
12174   tree scope = NULL_TREE;
12175   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12176   bool saved_in_declarator_p = parser->in_declarator_p;
12177   bool first = true;
12178   tree pushed_scope = NULL_TREE;
12179
12180   while (true)
12181     {
12182       /* Peek at the next token.  */
12183       token = cp_lexer_peek_token (parser->lexer);
12184       if (token->type == CPP_OPEN_PAREN)
12185         {
12186           /* This is either a parameter-declaration-clause, or a
12187              parenthesized declarator. When we know we are parsing a
12188              named declarator, it must be a parenthesized declarator
12189              if FIRST is true. For instance, `(int)' is a
12190              parameter-declaration-clause, with an omitted
12191              direct-abstract-declarator. But `((*))', is a
12192              parenthesized abstract declarator. Finally, when T is a
12193              template parameter `(T)' is a
12194              parameter-declaration-clause, and not a parenthesized
12195              named declarator.
12196
12197              We first try and parse a parameter-declaration-clause,
12198              and then try a nested declarator (if FIRST is true).
12199
12200              It is not an error for it not to be a
12201              parameter-declaration-clause, even when FIRST is
12202              false. Consider,
12203
12204                int i (int);
12205                int i (3);
12206
12207              The first is the declaration of a function while the
12208              second is a the definition of a variable, including its
12209              initializer.
12210
12211              Having seen only the parenthesis, we cannot know which of
12212              these two alternatives should be selected.  Even more
12213              complex are examples like:
12214
12215                int i (int (a));
12216                int i (int (3));
12217
12218              The former is a function-declaration; the latter is a
12219              variable initialization.
12220
12221              Thus again, we try a parameter-declaration-clause, and if
12222              that fails, we back out and return.  */
12223
12224           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12225             {
12226               cp_parameter_declarator *params;
12227               unsigned saved_num_template_parameter_lists;
12228
12229               /* In a member-declarator, the only valid interpretation
12230                  of a parenthesis is the start of a
12231                  parameter-declaration-clause.  (It is invalid to
12232                  initialize a static data member with a parenthesized
12233                  initializer; only the "=" form of initialization is
12234                  permitted.)  */
12235               if (!member_p)
12236                 cp_parser_parse_tentatively (parser);
12237
12238               /* Consume the `('.  */
12239               cp_lexer_consume_token (parser->lexer);
12240               if (first)
12241                 {
12242                   /* If this is going to be an abstract declarator, we're
12243                      in a declarator and we can't have default args.  */
12244                   parser->default_arg_ok_p = false;
12245                   parser->in_declarator_p = true;
12246                 }
12247
12248               /* Inside the function parameter list, surrounding
12249                  template-parameter-lists do not apply.  */
12250               saved_num_template_parameter_lists
12251                 = parser->num_template_parameter_lists;
12252               parser->num_template_parameter_lists = 0;
12253
12254               /* Parse the parameter-declaration-clause.  */
12255               params = cp_parser_parameter_declaration_clause (parser);
12256
12257               parser->num_template_parameter_lists
12258                 = saved_num_template_parameter_lists;
12259
12260               /* If all went well, parse the cv-qualifier-seq and the
12261                  exception-specification.  */
12262               if (member_p || cp_parser_parse_definitely (parser))
12263                 {
12264                   cp_cv_quals cv_quals;
12265                   tree exception_specification;
12266
12267                   if (ctor_dtor_or_conv_p)
12268                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12269                   first = false;
12270                   /* Consume the `)'.  */
12271                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12272
12273                   /* Parse the cv-qualifier-seq.  */
12274                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12275                   /* And the exception-specification.  */
12276                   exception_specification
12277                     = cp_parser_exception_specification_opt (parser);
12278
12279                   /* Create the function-declarator.  */
12280                   declarator = make_call_declarator (declarator,
12281                                                      params,
12282                                                      cv_quals,
12283                                                      exception_specification);
12284                   /* Any subsequent parameter lists are to do with
12285                      return type, so are not those of the declared
12286                      function.  */
12287                   parser->default_arg_ok_p = false;
12288
12289                   /* Repeat the main loop.  */
12290                   continue;
12291                 }
12292             }
12293
12294           /* If this is the first, we can try a parenthesized
12295              declarator.  */
12296           if (first)
12297             {
12298               bool saved_in_type_id_in_expr_p;
12299
12300               parser->default_arg_ok_p = saved_default_arg_ok_p;
12301               parser->in_declarator_p = saved_in_declarator_p;
12302
12303               /* Consume the `('.  */
12304               cp_lexer_consume_token (parser->lexer);
12305               /* Parse the nested declarator.  */
12306               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12307               parser->in_type_id_in_expr_p = true;
12308               declarator
12309                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12310                                         /*parenthesized_p=*/NULL,
12311                                         member_p);
12312               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12313               first = false;
12314               /* Expect a `)'.  */
12315               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12316                 declarator = cp_error_declarator;
12317               if (declarator == cp_error_declarator)
12318                 break;
12319
12320               goto handle_declarator;
12321             }
12322           /* Otherwise, we must be done.  */
12323           else
12324             break;
12325         }
12326       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12327                && token->type == CPP_OPEN_SQUARE)
12328         {
12329           /* Parse an array-declarator.  */
12330           tree bounds;
12331
12332           if (ctor_dtor_or_conv_p)
12333             *ctor_dtor_or_conv_p = 0;
12334
12335           first = false;
12336           parser->default_arg_ok_p = false;
12337           parser->in_declarator_p = true;
12338           /* Consume the `['.  */
12339           cp_lexer_consume_token (parser->lexer);
12340           /* Peek at the next token.  */
12341           token = cp_lexer_peek_token (parser->lexer);
12342           /* If the next token is `]', then there is no
12343              constant-expression.  */
12344           if (token->type != CPP_CLOSE_SQUARE)
12345             {
12346               bool non_constant_p;
12347
12348               bounds
12349                 = cp_parser_constant_expression (parser,
12350                                                  /*allow_non_constant=*/true,
12351                                                  &non_constant_p);
12352               if (!non_constant_p)
12353                 bounds = fold_non_dependent_expr (bounds);
12354               /* Normally, the array bound must be an integral constant
12355                  expression.  However, as an extension, we allow VLAs
12356                  in function scopes.  */
12357               else if (!parser->in_function_body)
12358                 {
12359                   error ("array bound is not an integer constant");
12360                   bounds = error_mark_node;
12361                 }
12362             }
12363           else
12364             bounds = NULL_TREE;
12365           /* Look for the closing `]'.  */
12366           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12367             {
12368               declarator = cp_error_declarator;
12369               break;
12370             }
12371
12372           declarator = make_array_declarator (declarator, bounds);
12373         }
12374       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12375         {
12376           tree qualifying_scope;
12377           tree unqualified_name;
12378           special_function_kind sfk;
12379           bool abstract_ok;
12380           bool pack_expansion_p = false;
12381
12382           /* Parse a declarator-id */
12383           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12384           if (abstract_ok)
12385             {
12386               cp_parser_parse_tentatively (parser);
12387
12388               /* If we see an ellipsis, we should be looking at a
12389                  parameter pack. */
12390               if (token->type == CPP_ELLIPSIS)
12391                 {
12392                   /* Consume the `...' */
12393                   cp_lexer_consume_token (parser->lexer);
12394
12395                   pack_expansion_p = true;
12396                 }
12397             }
12398
12399           unqualified_name
12400             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12401           qualifying_scope = parser->scope;
12402           if (abstract_ok)
12403             {
12404               bool okay = false;
12405
12406               if (!unqualified_name && pack_expansion_p)
12407                 {
12408                   /* Check whether an error occurred. */
12409                   okay = !cp_parser_error_occurred (parser);
12410
12411                   /* We already consumed the ellipsis to mark a
12412                      parameter pack, but we have no way to report it,
12413                      so abort the tentative parse. We will be exiting
12414                      immediately anyway. */
12415                   cp_parser_abort_tentative_parse (parser);
12416                 }
12417               else
12418                 okay = cp_parser_parse_definitely (parser);
12419
12420               if (!okay)
12421                 unqualified_name = error_mark_node;
12422               else if (unqualified_name
12423                        && (qualifying_scope
12424                            || (TREE_CODE (unqualified_name)
12425                                != IDENTIFIER_NODE)))
12426                 {
12427                   cp_parser_error (parser, "expected unqualified-id");
12428                   unqualified_name = error_mark_node;
12429                 }
12430             }
12431
12432           if (!unqualified_name)
12433             return NULL;
12434           if (unqualified_name == error_mark_node)
12435             {
12436               declarator = cp_error_declarator;
12437               pack_expansion_p = false;
12438               declarator->parameter_pack_p = false;
12439               break;
12440             }
12441
12442           if (qualifying_scope && at_namespace_scope_p ()
12443               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12444             {
12445               /* In the declaration of a member of a template class
12446                  outside of the class itself, the SCOPE will sometimes
12447                  be a TYPENAME_TYPE.  For example, given:
12448
12449                  template <typename T>
12450                  int S<T>::R::i = 3;
12451
12452                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
12453                  this context, we must resolve S<T>::R to an ordinary
12454                  type, rather than a typename type.
12455
12456                  The reason we normally avoid resolving TYPENAME_TYPEs
12457                  is that a specialization of `S' might render
12458                  `S<T>::R' not a type.  However, if `S' is
12459                  specialized, then this `i' will not be used, so there
12460                  is no harm in resolving the types here.  */
12461               tree type;
12462
12463               /* Resolve the TYPENAME_TYPE.  */
12464               type = resolve_typename_type (qualifying_scope,
12465                                             /*only_current_p=*/false);
12466               /* If that failed, the declarator is invalid.  */
12467               if (TREE_CODE (type) == TYPENAME_TYPE)
12468                 error ("%<%T::%E%> is not a type",
12469                        TYPE_CONTEXT (qualifying_scope),
12470                        TYPE_IDENTIFIER (qualifying_scope));
12471               qualifying_scope = type;
12472             }
12473
12474           sfk = sfk_none;
12475
12476           if (unqualified_name)
12477             {
12478               tree class_type;
12479
12480               if (qualifying_scope
12481                   && CLASS_TYPE_P (qualifying_scope))
12482                 class_type = qualifying_scope;
12483               else
12484                 class_type = current_class_type;
12485
12486               if (TREE_CODE (unqualified_name) == TYPE_DECL)
12487                 {
12488                   tree name_type = TREE_TYPE (unqualified_name);
12489                   if (class_type && same_type_p (name_type, class_type))
12490                     {
12491                       if (qualifying_scope
12492                           && CLASSTYPE_USE_TEMPLATE (name_type))
12493                         {
12494                           error ("invalid use of constructor as a template");
12495                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12496                                   "name the constructor in a qualified name",
12497                                   class_type,
12498                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12499                                   class_type, name_type);
12500                           declarator = cp_error_declarator;
12501                           break;
12502                         }
12503                       else
12504                         unqualified_name = constructor_name (class_type);
12505                     }
12506                   else
12507                     {
12508                       /* We do not attempt to print the declarator
12509                          here because we do not have enough
12510                          information about its original syntactic
12511                          form.  */
12512                       cp_parser_error (parser, "invalid declarator");
12513                       declarator = cp_error_declarator;
12514                       break;
12515                     }
12516                 }
12517
12518               if (class_type)
12519                 {
12520                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12521                     sfk = sfk_destructor;
12522                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12523                     sfk = sfk_conversion;
12524                   else if (/* There's no way to declare a constructor
12525                               for an anonymous type, even if the type
12526                               got a name for linkage purposes.  */
12527                            !TYPE_WAS_ANONYMOUS (class_type)
12528                            && constructor_name_p (unqualified_name,
12529                                                   class_type))
12530                     {
12531                       unqualified_name = constructor_name (class_type);
12532                       sfk = sfk_constructor;
12533                     }
12534
12535                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
12536                     *ctor_dtor_or_conv_p = -1;
12537                 }
12538             }
12539           declarator = make_id_declarator (qualifying_scope,
12540                                            unqualified_name,
12541                                            sfk);
12542           declarator->id_loc = token->location;
12543           declarator->parameter_pack_p = pack_expansion_p;
12544
12545           if (pack_expansion_p)
12546             maybe_warn_variadic_templates ();
12547
12548         handle_declarator:;
12549           scope = get_scope_of_declarator (declarator);
12550           if (scope)
12551             /* Any names that appear after the declarator-id for a
12552                member are looked up in the containing scope.  */
12553             pushed_scope = push_scope (scope);
12554           parser->in_declarator_p = true;
12555           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12556               || (declarator && declarator->kind == cdk_id))
12557             /* Default args are only allowed on function
12558                declarations.  */
12559             parser->default_arg_ok_p = saved_default_arg_ok_p;
12560           else
12561             parser->default_arg_ok_p = false;
12562
12563           first = false;
12564         }
12565       /* We're done.  */
12566       else
12567         break;
12568     }
12569
12570   /* For an abstract declarator, we might wind up with nothing at this
12571      point.  That's an error; the declarator is not optional.  */
12572   if (!declarator)
12573     cp_parser_error (parser, "expected declarator");
12574
12575   /* If we entered a scope, we must exit it now.  */
12576   if (pushed_scope)
12577     pop_scope (pushed_scope);
12578
12579   parser->default_arg_ok_p = saved_default_arg_ok_p;
12580   parser->in_declarator_p = saved_in_declarator_p;
12581
12582   return declarator;
12583 }
12584
12585 /* Parse a ptr-operator.
12586
12587    ptr-operator:
12588      * cv-qualifier-seq [opt]
12589      &
12590      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12591
12592    GNU Extension:
12593
12594    ptr-operator:
12595      & cv-qualifier-seq [opt]
12596
12597    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12598    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12599    an rvalue reference. In the case of a pointer-to-member, *TYPE is
12600    filled in with the TYPE containing the member.  *CV_QUALS is
12601    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12602    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
12603    Note that the tree codes returned by this function have nothing
12604    to do with the types of trees that will be eventually be created
12605    to represent the pointer or reference type being parsed. They are
12606    just constants with suggestive names. */
12607 static enum tree_code
12608 cp_parser_ptr_operator (cp_parser* parser,
12609                         tree* type,
12610                         cp_cv_quals *cv_quals)
12611 {
12612   enum tree_code code = ERROR_MARK;
12613   cp_token *token;
12614
12615   /* Assume that it's not a pointer-to-member.  */
12616   *type = NULL_TREE;
12617   /* And that there are no cv-qualifiers.  */
12618   *cv_quals = TYPE_UNQUALIFIED;
12619
12620   /* Peek at the next token.  */
12621   token = cp_lexer_peek_token (parser->lexer);
12622
12623   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
12624   if (token->type == CPP_MULT)
12625     code = INDIRECT_REF;
12626   else if (token->type == CPP_AND)
12627     code = ADDR_EXPR;
12628   else if ((cxx_dialect != cxx98) &&
12629            token->type == CPP_AND_AND) /* C++0x only */
12630     code = NON_LVALUE_EXPR;
12631
12632   if (code != ERROR_MARK)
12633     {
12634       /* Consume the `*', `&' or `&&'.  */
12635       cp_lexer_consume_token (parser->lexer);
12636
12637       /* A `*' can be followed by a cv-qualifier-seq, and so can a
12638          `&', if we are allowing GNU extensions.  (The only qualifier
12639          that can legally appear after `&' is `restrict', but that is
12640          enforced during semantic analysis.  */
12641       if (code == INDIRECT_REF
12642           || cp_parser_allow_gnu_extensions_p (parser))
12643         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12644     }
12645   else
12646     {
12647       /* Try the pointer-to-member case.  */
12648       cp_parser_parse_tentatively (parser);
12649       /* Look for the optional `::' operator.  */
12650       cp_parser_global_scope_opt (parser,
12651                                   /*current_scope_valid_p=*/false);
12652       /* Look for the nested-name specifier.  */
12653       cp_parser_nested_name_specifier (parser,
12654                                        /*typename_keyword_p=*/false,
12655                                        /*check_dependency_p=*/true,
12656                                        /*type_p=*/false,
12657                                        /*is_declaration=*/false);
12658       /* If we found it, and the next token is a `*', then we are
12659          indeed looking at a pointer-to-member operator.  */
12660       if (!cp_parser_error_occurred (parser)
12661           && cp_parser_require (parser, CPP_MULT, "`*'"))
12662         {
12663           /* Indicate that the `*' operator was used.  */
12664           code = INDIRECT_REF;
12665
12666           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12667             error ("%qD is a namespace", parser->scope);
12668           else
12669             {
12670               /* The type of which the member is a member is given by the
12671                  current SCOPE.  */
12672               *type = parser->scope;
12673               /* The next name will not be qualified.  */
12674               parser->scope = NULL_TREE;
12675               parser->qualifying_scope = NULL_TREE;
12676               parser->object_scope = NULL_TREE;
12677               /* Look for the optional cv-qualifier-seq.  */
12678               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12679             }
12680         }
12681       /* If that didn't work we don't have a ptr-operator.  */
12682       if (!cp_parser_parse_definitely (parser))
12683         cp_parser_error (parser, "expected ptr-operator");
12684     }
12685
12686   return code;
12687 }
12688
12689 /* Parse an (optional) cv-qualifier-seq.
12690
12691    cv-qualifier-seq:
12692      cv-qualifier cv-qualifier-seq [opt]
12693
12694    cv-qualifier:
12695      const
12696      volatile
12697
12698    GNU Extension:
12699
12700    cv-qualifier:
12701      __restrict__
12702
12703    Returns a bitmask representing the cv-qualifiers.  */
12704
12705 static cp_cv_quals
12706 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12707 {
12708   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12709
12710   while (true)
12711     {
12712       cp_token *token;
12713       cp_cv_quals cv_qualifier;
12714
12715       /* Peek at the next token.  */
12716       token = cp_lexer_peek_token (parser->lexer);
12717       /* See if it's a cv-qualifier.  */
12718       switch (token->keyword)
12719         {
12720         case RID_CONST:
12721           cv_qualifier = TYPE_QUAL_CONST;
12722           break;
12723
12724         case RID_VOLATILE:
12725           cv_qualifier = TYPE_QUAL_VOLATILE;
12726           break;
12727
12728         case RID_RESTRICT:
12729           cv_qualifier = TYPE_QUAL_RESTRICT;
12730           break;
12731
12732         default:
12733           cv_qualifier = TYPE_UNQUALIFIED;
12734           break;
12735         }
12736
12737       if (!cv_qualifier)
12738         break;
12739
12740       if (cv_quals & cv_qualifier)
12741         {
12742           error ("duplicate cv-qualifier");
12743           cp_lexer_purge_token (parser->lexer);
12744         }
12745       else
12746         {
12747           cp_lexer_consume_token (parser->lexer);
12748           cv_quals |= cv_qualifier;
12749         }
12750     }
12751
12752   return cv_quals;
12753 }
12754
12755 /* Parse a declarator-id.
12756
12757    declarator-id:
12758      id-expression
12759      :: [opt] nested-name-specifier [opt] type-name
12760
12761    In the `id-expression' case, the value returned is as for
12762    cp_parser_id_expression if the id-expression was an unqualified-id.
12763    If the id-expression was a qualified-id, then a SCOPE_REF is
12764    returned.  The first operand is the scope (either a NAMESPACE_DECL
12765    or TREE_TYPE), but the second is still just a representation of an
12766    unqualified-id.  */
12767
12768 static tree
12769 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12770 {
12771   tree id;
12772   /* The expression must be an id-expression.  Assume that qualified
12773      names are the names of types so that:
12774
12775        template <class T>
12776        int S<T>::R::i = 3;
12777
12778      will work; we must treat `S<T>::R' as the name of a type.
12779      Similarly, assume that qualified names are templates, where
12780      required, so that:
12781
12782        template <class T>
12783        int S<T>::R<T>::i = 3;
12784
12785      will work, too.  */
12786   id = cp_parser_id_expression (parser,
12787                                 /*template_keyword_p=*/false,
12788                                 /*check_dependency_p=*/false,
12789                                 /*template_p=*/NULL,
12790                                 /*declarator_p=*/true,
12791                                 optional_p);
12792   if (id && BASELINK_P (id))
12793     id = BASELINK_FUNCTIONS (id);
12794   return id;
12795 }
12796
12797 /* Parse a type-id.
12798
12799    type-id:
12800      type-specifier-seq abstract-declarator [opt]
12801
12802    Returns the TYPE specified.  */
12803
12804 static tree
12805 cp_parser_type_id (cp_parser* parser)
12806 {
12807   cp_decl_specifier_seq type_specifier_seq;
12808   cp_declarator *abstract_declarator;
12809
12810   /* Parse the type-specifier-seq.  */
12811   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12812                                 &type_specifier_seq);
12813   if (type_specifier_seq.type == error_mark_node)
12814     return error_mark_node;
12815
12816   /* There might or might not be an abstract declarator.  */
12817   cp_parser_parse_tentatively (parser);
12818   /* Look for the declarator.  */
12819   abstract_declarator
12820     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12821                             /*parenthesized_p=*/NULL,
12822                             /*member_p=*/false);
12823   /* Check to see if there really was a declarator.  */
12824   if (!cp_parser_parse_definitely (parser))
12825     abstract_declarator = NULL;
12826
12827   return groktypename (&type_specifier_seq, abstract_declarator);
12828 }
12829
12830 /* Parse a type-specifier-seq.
12831
12832    type-specifier-seq:
12833      type-specifier type-specifier-seq [opt]
12834
12835    GNU extension:
12836
12837    type-specifier-seq:
12838      attributes type-specifier-seq [opt]
12839
12840    If IS_CONDITION is true, we are at the start of a "condition",
12841    e.g., we've just seen "if (".
12842
12843    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
12844
12845 static void
12846 cp_parser_type_specifier_seq (cp_parser* parser,
12847                               bool is_condition,
12848                               cp_decl_specifier_seq *type_specifier_seq)
12849 {
12850   bool seen_type_specifier = false;
12851   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12852
12853   /* Clear the TYPE_SPECIFIER_SEQ.  */
12854   clear_decl_specs (type_specifier_seq);
12855
12856   /* Parse the type-specifiers and attributes.  */
12857   while (true)
12858     {
12859       tree type_specifier;
12860       bool is_cv_qualifier;
12861
12862       /* Check for attributes first.  */
12863       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12864         {
12865           type_specifier_seq->attributes =
12866             chainon (type_specifier_seq->attributes,
12867                      cp_parser_attributes_opt (parser));
12868           continue;
12869         }
12870
12871       /* Look for the type-specifier.  */
12872       type_specifier = cp_parser_type_specifier (parser,
12873                                                  flags,
12874                                                  type_specifier_seq,
12875                                                  /*is_declaration=*/false,
12876                                                  NULL,
12877                                                  &is_cv_qualifier);
12878       if (!type_specifier)
12879         {
12880           /* If the first type-specifier could not be found, this is not a
12881              type-specifier-seq at all.  */
12882           if (!seen_type_specifier)
12883             {
12884               cp_parser_error (parser, "expected type-specifier");
12885               type_specifier_seq->type = error_mark_node;
12886               return;
12887             }
12888           /* If subsequent type-specifiers could not be found, the
12889              type-specifier-seq is complete.  */
12890           break;
12891         }
12892
12893       seen_type_specifier = true;
12894       /* The standard says that a condition can be:
12895
12896             type-specifier-seq declarator = assignment-expression
12897
12898          However, given:
12899
12900            struct S {};
12901            if (int S = ...)
12902
12903          we should treat the "S" as a declarator, not as a
12904          type-specifier.  The standard doesn't say that explicitly for
12905          type-specifier-seq, but it does say that for
12906          decl-specifier-seq in an ordinary declaration.  Perhaps it
12907          would be clearer just to allow a decl-specifier-seq here, and
12908          then add a semantic restriction that if any decl-specifiers
12909          that are not type-specifiers appear, the program is invalid.  */
12910       if (is_condition && !is_cv_qualifier)
12911         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12912     }
12913
12914   cp_parser_check_decl_spec (type_specifier_seq);
12915 }
12916
12917 /* Parse a parameter-declaration-clause.
12918
12919    parameter-declaration-clause:
12920      parameter-declaration-list [opt] ... [opt]
12921      parameter-declaration-list , ...
12922
12923    Returns a representation for the parameter declarations.  A return
12924    value of NULL indicates a parameter-declaration-clause consisting
12925    only of an ellipsis.  */
12926
12927 static cp_parameter_declarator *
12928 cp_parser_parameter_declaration_clause (cp_parser* parser)
12929 {
12930   cp_parameter_declarator *parameters;
12931   cp_token *token;
12932   bool ellipsis_p;
12933   bool is_error;
12934
12935   /* Peek at the next token.  */
12936   token = cp_lexer_peek_token (parser->lexer);
12937   /* Check for trivial parameter-declaration-clauses.  */
12938   if (token->type == CPP_ELLIPSIS)
12939     {
12940       /* Consume the `...' token.  */
12941       cp_lexer_consume_token (parser->lexer);
12942       return NULL;
12943     }
12944   else if (token->type == CPP_CLOSE_PAREN)
12945     /* There are no parameters.  */
12946     {
12947 #ifndef NO_IMPLICIT_EXTERN_C
12948       if (in_system_header && current_class_type == NULL
12949           && current_lang_name == lang_name_c)
12950         return NULL;
12951       else
12952 #endif
12953         return no_parameters;
12954     }
12955   /* Check for `(void)', too, which is a special case.  */
12956   else if (token->keyword == RID_VOID
12957            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12958                == CPP_CLOSE_PAREN))
12959     {
12960       /* Consume the `void' token.  */
12961       cp_lexer_consume_token (parser->lexer);
12962       /* There are no parameters.  */
12963       return no_parameters;
12964     }
12965
12966   /* Parse the parameter-declaration-list.  */
12967   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12968   /* If a parse error occurred while parsing the
12969      parameter-declaration-list, then the entire
12970      parameter-declaration-clause is erroneous.  */
12971   if (is_error)
12972     return NULL;
12973
12974   /* Peek at the next token.  */
12975   token = cp_lexer_peek_token (parser->lexer);
12976   /* If it's a `,', the clause should terminate with an ellipsis.  */
12977   if (token->type == CPP_COMMA)
12978     {
12979       /* Consume the `,'.  */
12980       cp_lexer_consume_token (parser->lexer);
12981       /* Expect an ellipsis.  */
12982       ellipsis_p
12983         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12984     }
12985   /* It might also be `...' if the optional trailing `,' was
12986      omitted.  */
12987   else if (token->type == CPP_ELLIPSIS)
12988     {
12989       /* Consume the `...' token.  */
12990       cp_lexer_consume_token (parser->lexer);
12991       /* And remember that we saw it.  */
12992       ellipsis_p = true;
12993     }
12994   else
12995     ellipsis_p = false;
12996
12997   /* Finish the parameter list.  */
12998   if (parameters && ellipsis_p)
12999     parameters->ellipsis_p = true;
13000
13001   return parameters;
13002 }
13003
13004 /* Parse a parameter-declaration-list.
13005
13006    parameter-declaration-list:
13007      parameter-declaration
13008      parameter-declaration-list , parameter-declaration
13009
13010    Returns a representation of the parameter-declaration-list, as for
13011    cp_parser_parameter_declaration_clause.  However, the
13012    `void_list_node' is never appended to the list.  Upon return,
13013    *IS_ERROR will be true iff an error occurred.  */
13014
13015 static cp_parameter_declarator *
13016 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13017 {
13018   cp_parameter_declarator *parameters = NULL;
13019   cp_parameter_declarator **tail = &parameters;
13020   bool saved_in_unbraced_linkage_specification_p;
13021
13022   /* Assume all will go well.  */
13023   *is_error = false;
13024   /* The special considerations that apply to a function within an
13025      unbraced linkage specifications do not apply to the parameters
13026      to the function.  */
13027   saved_in_unbraced_linkage_specification_p 
13028     = parser->in_unbraced_linkage_specification_p;
13029   parser->in_unbraced_linkage_specification_p = false;
13030
13031   /* Look for more parameters.  */
13032   while (true)
13033     {
13034       cp_parameter_declarator *parameter;
13035       bool parenthesized_p;
13036       /* Parse the parameter.  */
13037       parameter
13038         = cp_parser_parameter_declaration (parser,
13039                                            /*template_parm_p=*/false,
13040                                            &parenthesized_p);
13041
13042       /* If a parse error occurred parsing the parameter declaration,
13043          then the entire parameter-declaration-list is erroneous.  */
13044       if (!parameter)
13045         {
13046           *is_error = true;
13047           parameters = NULL;
13048           break;
13049         }
13050       /* Add the new parameter to the list.  */
13051       *tail = parameter;
13052       tail = &parameter->next;
13053
13054       /* Peek at the next token.  */
13055       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13056           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13057           /* These are for Objective-C++ */
13058           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13059           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13060         /* The parameter-declaration-list is complete.  */
13061         break;
13062       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13063         {
13064           cp_token *token;
13065
13066           /* Peek at the next token.  */
13067           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13068           /* If it's an ellipsis, then the list is complete.  */
13069           if (token->type == CPP_ELLIPSIS)
13070             break;
13071           /* Otherwise, there must be more parameters.  Consume the
13072              `,'.  */
13073           cp_lexer_consume_token (parser->lexer);
13074           /* When parsing something like:
13075
13076                 int i(float f, double d)
13077
13078              we can tell after seeing the declaration for "f" that we
13079              are not looking at an initialization of a variable "i",
13080              but rather at the declaration of a function "i".
13081
13082              Due to the fact that the parsing of template arguments
13083              (as specified to a template-id) requires backtracking we
13084              cannot use this technique when inside a template argument
13085              list.  */
13086           if (!parser->in_template_argument_list_p
13087               && !parser->in_type_id_in_expr_p
13088               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13089               /* However, a parameter-declaration of the form
13090                  "foat(f)" (which is a valid declaration of a
13091                  parameter "f") can also be interpreted as an
13092                  expression (the conversion of "f" to "float").  */
13093               && !parenthesized_p)
13094             cp_parser_commit_to_tentative_parse (parser);
13095         }
13096       else
13097         {
13098           cp_parser_error (parser, "expected %<,%> or %<...%>");
13099           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13100             cp_parser_skip_to_closing_parenthesis (parser,
13101                                                    /*recovering=*/true,
13102                                                    /*or_comma=*/false,
13103                                                    /*consume_paren=*/false);
13104           break;
13105         }
13106     }
13107
13108   parser->in_unbraced_linkage_specification_p
13109     = saved_in_unbraced_linkage_specification_p;
13110
13111   return parameters;
13112 }
13113
13114 /* Parse a parameter declaration.
13115
13116    parameter-declaration:
13117      decl-specifier-seq ... [opt] declarator
13118      decl-specifier-seq declarator = assignment-expression
13119      decl-specifier-seq ... [opt] abstract-declarator [opt]
13120      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13121
13122    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13123    declares a template parameter.  (In that case, a non-nested `>'
13124    token encountered during the parsing of the assignment-expression
13125    is not interpreted as a greater-than operator.)
13126
13127    Returns a representation of the parameter, or NULL if an error
13128    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13129    true iff the declarator is of the form "(p)".  */
13130
13131 static cp_parameter_declarator *
13132 cp_parser_parameter_declaration (cp_parser *parser,
13133                                  bool template_parm_p,
13134                                  bool *parenthesized_p)
13135 {
13136   int declares_class_or_enum;
13137   bool greater_than_is_operator_p;
13138   cp_decl_specifier_seq decl_specifiers;
13139   cp_declarator *declarator;
13140   tree default_argument;
13141   cp_token *token;
13142   const char *saved_message;
13143
13144   /* In a template parameter, `>' is not an operator.
13145
13146      [temp.param]
13147
13148      When parsing a default template-argument for a non-type
13149      template-parameter, the first non-nested `>' is taken as the end
13150      of the template parameter-list rather than a greater-than
13151      operator.  */
13152   greater_than_is_operator_p = !template_parm_p;
13153
13154   /* Type definitions may not appear in parameter types.  */
13155   saved_message = parser->type_definition_forbidden_message;
13156   parser->type_definition_forbidden_message
13157     = "types may not be defined in parameter types";
13158
13159   /* Parse the declaration-specifiers.  */
13160   cp_parser_decl_specifier_seq (parser,
13161                                 CP_PARSER_FLAGS_NONE,
13162                                 &decl_specifiers,
13163                                 &declares_class_or_enum);
13164   /* If an error occurred, there's no reason to attempt to parse the
13165      rest of the declaration.  */
13166   if (cp_parser_error_occurred (parser))
13167     {
13168       parser->type_definition_forbidden_message = saved_message;
13169       return NULL;
13170     }
13171
13172   /* Peek at the next token.  */
13173   token = cp_lexer_peek_token (parser->lexer);
13174
13175   /* If the next token is a `)', `,', `=', `>', or `...', then there
13176      is no declarator. However, when variadic templates are enabled,
13177      there may be a declarator following `...'.  */
13178   if (token->type == CPP_CLOSE_PAREN
13179       || token->type == CPP_COMMA
13180       || token->type == CPP_EQ
13181       || token->type == CPP_GREATER)
13182     {
13183       declarator = NULL;
13184       if (parenthesized_p)
13185         *parenthesized_p = false;
13186     }
13187   /* Otherwise, there should be a declarator.  */
13188   else
13189     {
13190       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13191       parser->default_arg_ok_p = false;
13192
13193       /* After seeing a decl-specifier-seq, if the next token is not a
13194          "(", there is no possibility that the code is a valid
13195          expression.  Therefore, if parsing tentatively, we commit at
13196          this point.  */
13197       if (!parser->in_template_argument_list_p
13198           /* In an expression context, having seen:
13199
13200                (int((char ...
13201
13202              we cannot be sure whether we are looking at a
13203              function-type (taking a "char" as a parameter) or a cast
13204              of some object of type "char" to "int".  */
13205           && !parser->in_type_id_in_expr_p
13206           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13207           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13208         cp_parser_commit_to_tentative_parse (parser);
13209       /* Parse the declarator.  */
13210       declarator = cp_parser_declarator (parser,
13211                                          CP_PARSER_DECLARATOR_EITHER,
13212                                          /*ctor_dtor_or_conv_p=*/NULL,
13213                                          parenthesized_p,
13214                                          /*member_p=*/false);
13215       parser->default_arg_ok_p = saved_default_arg_ok_p;
13216       /* After the declarator, allow more attributes.  */
13217       decl_specifiers.attributes
13218         = chainon (decl_specifiers.attributes,
13219                    cp_parser_attributes_opt (parser));
13220     }
13221
13222   /* If the next token is an ellipsis, and we have not seen a
13223      declarator name, and the type of the declarator contains parameter
13224      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13225      a parameter pack expansion expression. Otherwise, leave the
13226      ellipsis for a C-style variadic function. */
13227   token = cp_lexer_peek_token (parser->lexer);
13228   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13229     {
13230       tree type = decl_specifiers.type;
13231
13232       if (type && DECL_P (type))
13233         type = TREE_TYPE (type);
13234
13235       if (type
13236           && TREE_CODE (type) != TYPE_PACK_EXPANSION
13237           && declarator_can_be_parameter_pack (declarator)
13238           && (!declarator || !declarator->parameter_pack_p)
13239           && uses_parameter_packs (type))
13240         {
13241           /* Consume the `...'. */
13242           cp_lexer_consume_token (parser->lexer);
13243           maybe_warn_variadic_templates ();
13244           
13245           /* Build a pack expansion type */
13246           if (declarator)
13247             declarator->parameter_pack_p = true;
13248           else
13249             decl_specifiers.type = make_pack_expansion (type);
13250         }
13251     }
13252
13253   /* The restriction on defining new types applies only to the type
13254      of the parameter, not to the default argument.  */
13255   parser->type_definition_forbidden_message = saved_message;
13256
13257   /* If the next token is `=', then process a default argument.  */
13258   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13259     {
13260       bool saved_greater_than_is_operator_p;
13261       /* Consume the `='.  */
13262       cp_lexer_consume_token (parser->lexer);
13263
13264       /* If we are defining a class, then the tokens that make up the
13265          default argument must be saved and processed later.  */
13266       if (!template_parm_p && at_class_scope_p ()
13267           && TYPE_BEING_DEFINED (current_class_type))
13268         {
13269           unsigned depth = 0;
13270           cp_token *first_token;
13271           cp_token *token;
13272
13273           /* Add tokens until we have processed the entire default
13274              argument.  We add the range [first_token, token).  */
13275           first_token = cp_lexer_peek_token (parser->lexer);
13276           while (true)
13277             {
13278               bool done = false;
13279
13280               /* Peek at the next token.  */
13281               token = cp_lexer_peek_token (parser->lexer);
13282               /* What we do depends on what token we have.  */
13283               switch (token->type)
13284                 {
13285                   /* In valid code, a default argument must be
13286                      immediately followed by a `,' `)', or `...'.  */
13287                 case CPP_COMMA:
13288                 case CPP_CLOSE_PAREN:
13289                 case CPP_ELLIPSIS:
13290                   /* If we run into a non-nested `;', `}', or `]',
13291                      then the code is invalid -- but the default
13292                      argument is certainly over.  */
13293                 case CPP_SEMICOLON:
13294                 case CPP_CLOSE_BRACE:
13295                 case CPP_CLOSE_SQUARE:
13296                   if (depth == 0)
13297                     done = true;
13298                   /* Update DEPTH, if necessary.  */
13299                   else if (token->type == CPP_CLOSE_PAREN
13300                            || token->type == CPP_CLOSE_BRACE
13301                            || token->type == CPP_CLOSE_SQUARE)
13302                     --depth;
13303                   break;
13304
13305                 case CPP_OPEN_PAREN:
13306                 case CPP_OPEN_SQUARE:
13307                 case CPP_OPEN_BRACE:
13308                   ++depth;
13309                   break;
13310
13311                 case CPP_RSHIFT:
13312                   if (cxx_dialect == cxx98)
13313                     break;
13314                   /* Fall through for C++0x, which treats the `>>'
13315                      operator like two `>' tokens in certain
13316                      cases.  */
13317
13318                 case CPP_GREATER:
13319                   /* If we see a non-nested `>', and `>' is not an
13320                      operator, then it marks the end of the default
13321                      argument.  */
13322                   if (!depth && !greater_than_is_operator_p)
13323                     done = true;
13324                   break;
13325
13326                   /* If we run out of tokens, issue an error message.  */
13327                 case CPP_EOF:
13328                 case CPP_PRAGMA_EOL:
13329                   error ("file ends in default argument");
13330                   done = true;
13331                   break;
13332
13333                 case CPP_NAME:
13334                 case CPP_SCOPE:
13335                   /* In these cases, we should look for template-ids.
13336                      For example, if the default argument is
13337                      `X<int, double>()', we need to do name lookup to
13338                      figure out whether or not `X' is a template; if
13339                      so, the `,' does not end the default argument.
13340
13341                      That is not yet done.  */
13342                   break;
13343
13344                 default:
13345                   break;
13346                 }
13347
13348               /* If we've reached the end, stop.  */
13349               if (done)
13350                 break;
13351
13352               /* Add the token to the token block.  */
13353               token = cp_lexer_consume_token (parser->lexer);
13354             }
13355
13356           /* Create a DEFAULT_ARG to represented the unparsed default
13357              argument.  */
13358           default_argument = make_node (DEFAULT_ARG);
13359           DEFARG_TOKENS (default_argument)
13360             = cp_token_cache_new (first_token, token);
13361           DEFARG_INSTANTIATIONS (default_argument) = NULL;
13362         }
13363       /* Outside of a class definition, we can just parse the
13364          assignment-expression.  */
13365       else
13366         {
13367           bool saved_local_variables_forbidden_p;
13368
13369           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13370              set correctly.  */
13371           saved_greater_than_is_operator_p
13372             = parser->greater_than_is_operator_p;
13373           parser->greater_than_is_operator_p = greater_than_is_operator_p;
13374           /* Local variable names (and the `this' keyword) may not
13375              appear in a default argument.  */
13376           saved_local_variables_forbidden_p
13377             = parser->local_variables_forbidden_p;
13378           parser->local_variables_forbidden_p = true;
13379           /* The default argument expression may cause implicitly
13380              defined member functions to be synthesized, which will
13381              result in garbage collection.  We must treat this
13382              situation as if we were within the body of function so as
13383              to avoid collecting live data on the stack.  */
13384           ++function_depth;
13385           /* Parse the assignment-expression.  */
13386           if (template_parm_p)
13387             push_deferring_access_checks (dk_no_deferred);
13388           default_argument
13389             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13390           if (template_parm_p)
13391             pop_deferring_access_checks ();
13392           /* Restore saved state.  */
13393           --function_depth;
13394           parser->greater_than_is_operator_p
13395             = saved_greater_than_is_operator_p;
13396           parser->local_variables_forbidden_p
13397             = saved_local_variables_forbidden_p;
13398         }
13399       if (!parser->default_arg_ok_p)
13400         {
13401           if (!flag_pedantic_errors)
13402             warning (0, "deprecated use of default argument for parameter of non-function");
13403           else
13404             {
13405               error ("default arguments are only permitted for function parameters");
13406               default_argument = NULL_TREE;
13407             }
13408         }
13409     }
13410   else
13411     default_argument = NULL_TREE;
13412
13413   return make_parameter_declarator (&decl_specifiers,
13414                                     declarator,
13415                                     default_argument);
13416 }
13417
13418 /* Parse a function-body.
13419
13420    function-body:
13421      compound_statement  */
13422
13423 static void
13424 cp_parser_function_body (cp_parser *parser)
13425 {
13426   cp_parser_compound_statement (parser, NULL, false);
13427 }
13428
13429 /* Parse a ctor-initializer-opt followed by a function-body.  Return
13430    true if a ctor-initializer was present.  */
13431
13432 static bool
13433 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13434 {
13435   tree body;
13436   bool ctor_initializer_p;
13437
13438   /* Begin the function body.  */
13439   body = begin_function_body ();
13440   /* Parse the optional ctor-initializer.  */
13441   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13442   /* Parse the function-body.  */
13443   cp_parser_function_body (parser);
13444   /* Finish the function body.  */
13445   finish_function_body (body);
13446
13447   return ctor_initializer_p;
13448 }
13449
13450 /* Parse an initializer.
13451
13452    initializer:
13453      = initializer-clause
13454      ( expression-list )
13455
13456    Returns an expression representing the initializer.  If no
13457    initializer is present, NULL_TREE is returned.
13458
13459    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13460    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
13461    set to FALSE if there is no initializer present.  If there is an
13462    initializer, and it is not a constant-expression, *NON_CONSTANT_P
13463    is set to true; otherwise it is set to false.  */
13464
13465 static tree
13466 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13467                        bool* non_constant_p)
13468 {
13469   cp_token *token;
13470   tree init;
13471
13472   /* Peek at the next token.  */
13473   token = cp_lexer_peek_token (parser->lexer);
13474
13475   /* Let our caller know whether or not this initializer was
13476      parenthesized.  */
13477   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13478   /* Assume that the initializer is constant.  */
13479   *non_constant_p = false;
13480
13481   if (token->type == CPP_EQ)
13482     {
13483       /* Consume the `='.  */
13484       cp_lexer_consume_token (parser->lexer);
13485       /* Parse the initializer-clause.  */
13486       init = cp_parser_initializer_clause (parser, non_constant_p);
13487     }
13488   else if (token->type == CPP_OPEN_PAREN)
13489     init = cp_parser_parenthesized_expression_list (parser, false,
13490                                                     /*cast_p=*/false,
13491                                                     /*allow_expansion_p=*/true,
13492                                                     non_constant_p);
13493   else
13494     {
13495       /* Anything else is an error.  */
13496       cp_parser_error (parser, "expected initializer");
13497       init = error_mark_node;
13498     }
13499
13500   return init;
13501 }
13502
13503 /* Parse an initializer-clause.
13504
13505    initializer-clause:
13506      assignment-expression
13507      { initializer-list , [opt] }
13508      { }
13509
13510    Returns an expression representing the initializer.
13511
13512    If the `assignment-expression' production is used the value
13513    returned is simply a representation for the expression.
13514
13515    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
13516    the elements of the initializer-list (or NULL, if the last
13517    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
13518    NULL_TREE.  There is no way to detect whether or not the optional
13519    trailing `,' was provided.  NON_CONSTANT_P is as for
13520    cp_parser_initializer.  */
13521
13522 static tree
13523 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13524 {
13525   tree initializer;
13526
13527   /* Assume the expression is constant.  */
13528   *non_constant_p = false;
13529
13530   /* If it is not a `{', then we are looking at an
13531      assignment-expression.  */
13532   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13533     {
13534       initializer
13535         = cp_parser_constant_expression (parser,
13536                                         /*allow_non_constant_p=*/true,
13537                                         non_constant_p);
13538       if (!*non_constant_p)
13539         initializer = fold_non_dependent_expr (initializer);
13540     }
13541   else
13542     {
13543       /* Consume the `{' token.  */
13544       cp_lexer_consume_token (parser->lexer);
13545       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
13546       initializer = make_node (CONSTRUCTOR);
13547       /* If it's not a `}', then there is a non-trivial initializer.  */
13548       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13549         {
13550           /* Parse the initializer list.  */
13551           CONSTRUCTOR_ELTS (initializer)
13552             = cp_parser_initializer_list (parser, non_constant_p);
13553           /* A trailing `,' token is allowed.  */
13554           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13555             cp_lexer_consume_token (parser->lexer);
13556         }
13557       /* Now, there should be a trailing `}'.  */
13558       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13559     }
13560
13561   return initializer;
13562 }
13563
13564 /* Parse an initializer-list.
13565
13566    initializer-list:
13567      initializer-clause ... [opt]
13568      initializer-list , initializer-clause ... [opt]
13569
13570    GNU Extension:
13571
13572    initializer-list:
13573      identifier : initializer-clause
13574      initializer-list, identifier : initializer-clause
13575
13576    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
13577    for the initializer.  If the INDEX of the elt is non-NULL, it is the
13578    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
13579    as for cp_parser_initializer.  */
13580
13581 static VEC(constructor_elt,gc) *
13582 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13583 {
13584   VEC(constructor_elt,gc) *v = NULL;
13585
13586   /* Assume all of the expressions are constant.  */
13587   *non_constant_p = false;
13588
13589   /* Parse the rest of the list.  */
13590   while (true)
13591     {
13592       cp_token *token;
13593       tree identifier;
13594       tree initializer;
13595       bool clause_non_constant_p;
13596
13597       /* If the next token is an identifier and the following one is a
13598          colon, we are looking at the GNU designated-initializer
13599          syntax.  */
13600       if (cp_parser_allow_gnu_extensions_p (parser)
13601           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13602           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13603         {
13604           /* Warn the user that they are using an extension.  */
13605           if (pedantic)
13606             pedwarn ("ISO C++ does not allow designated initializers");
13607           /* Consume the identifier.  */
13608           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13609           /* Consume the `:'.  */
13610           cp_lexer_consume_token (parser->lexer);
13611         }
13612       else
13613         identifier = NULL_TREE;
13614
13615       /* Parse the initializer.  */
13616       initializer = cp_parser_initializer_clause (parser,
13617                                                   &clause_non_constant_p);
13618       /* If any clause is non-constant, so is the entire initializer.  */
13619       if (clause_non_constant_p)
13620         *non_constant_p = true;
13621
13622       /* If we have an ellipsis, this is an initializer pack
13623          expansion.  */
13624       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13625         {
13626           /* Consume the `...'.  */
13627           cp_lexer_consume_token (parser->lexer);
13628
13629           /* Turn the initializer into an initializer expansion.  */
13630           initializer = make_pack_expansion (initializer);
13631         }
13632
13633       /* Add it to the vector.  */
13634       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13635
13636       /* If the next token is not a comma, we have reached the end of
13637          the list.  */
13638       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13639         break;
13640
13641       /* Peek at the next token.  */
13642       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13643       /* If the next token is a `}', then we're still done.  An
13644          initializer-clause can have a trailing `,' after the
13645          initializer-list and before the closing `}'.  */
13646       if (token->type == CPP_CLOSE_BRACE)
13647         break;
13648
13649       /* Consume the `,' token.  */
13650       cp_lexer_consume_token (parser->lexer);
13651     }
13652
13653   return v;
13654 }
13655
13656 /* Classes [gram.class] */
13657
13658 /* Parse a class-name.
13659
13660    class-name:
13661      identifier
13662      template-id
13663
13664    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13665    to indicate that names looked up in dependent types should be
13666    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
13667    keyword has been used to indicate that the name that appears next
13668    is a template.  TAG_TYPE indicates the explicit tag given before
13669    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
13670    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
13671    is the class being defined in a class-head.
13672
13673    Returns the TYPE_DECL representing the class.  */
13674
13675 static tree
13676 cp_parser_class_name (cp_parser *parser,
13677                       bool typename_keyword_p,
13678                       bool template_keyword_p,
13679                       enum tag_types tag_type,
13680                       bool check_dependency_p,
13681                       bool class_head_p,
13682                       bool is_declaration)
13683 {
13684   tree decl;
13685   tree scope;
13686   bool typename_p;
13687   cp_token *token;
13688
13689   /* All class-names start with an identifier.  */
13690   token = cp_lexer_peek_token (parser->lexer);
13691   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13692     {
13693       cp_parser_error (parser, "expected class-name");
13694       return error_mark_node;
13695     }
13696
13697   /* PARSER->SCOPE can be cleared when parsing the template-arguments
13698      to a template-id, so we save it here.  */
13699   scope = parser->scope;
13700   if (scope == error_mark_node)
13701     return error_mark_node;
13702
13703   /* Any name names a type if we're following the `typename' keyword
13704      in a qualified name where the enclosing scope is type-dependent.  */
13705   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13706                 && dependent_type_p (scope));
13707   /* Handle the common case (an identifier, but not a template-id)
13708      efficiently.  */
13709   if (token->type == CPP_NAME
13710       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13711     {
13712       cp_token *identifier_token;
13713       tree identifier;
13714       bool ambiguous_p;
13715
13716       /* Look for the identifier.  */
13717       identifier_token = cp_lexer_peek_token (parser->lexer);
13718       ambiguous_p = identifier_token->ambiguous_p;
13719       identifier = cp_parser_identifier (parser);
13720       /* If the next token isn't an identifier, we are certainly not
13721          looking at a class-name.  */
13722       if (identifier == error_mark_node)
13723         decl = error_mark_node;
13724       /* If we know this is a type-name, there's no need to look it
13725          up.  */
13726       else if (typename_p)
13727         decl = identifier;
13728       else
13729         {
13730           tree ambiguous_decls;
13731           /* If we already know that this lookup is ambiguous, then
13732              we've already issued an error message; there's no reason
13733              to check again.  */
13734           if (ambiguous_p)
13735             {
13736               cp_parser_simulate_error (parser);
13737               return error_mark_node;
13738             }
13739           /* If the next token is a `::', then the name must be a type
13740              name.
13741
13742              [basic.lookup.qual]
13743
13744              During the lookup for a name preceding the :: scope
13745              resolution operator, object, function, and enumerator
13746              names are ignored.  */
13747           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13748             tag_type = typename_type;
13749           /* Look up the name.  */
13750           decl = cp_parser_lookup_name (parser, identifier,
13751                                         tag_type,
13752                                         /*is_template=*/false,
13753                                         /*is_namespace=*/false,
13754                                         check_dependency_p,
13755                                         &ambiguous_decls);
13756           if (ambiguous_decls)
13757             {
13758               error ("reference to %qD is ambiguous", identifier);
13759               print_candidates (ambiguous_decls);
13760               if (cp_parser_parsing_tentatively (parser))
13761                 {
13762                   identifier_token->ambiguous_p = true;
13763                   cp_parser_simulate_error (parser);
13764                 }
13765               return error_mark_node;
13766             }
13767         }
13768     }
13769   else
13770     {
13771       /* Try a template-id.  */
13772       decl = cp_parser_template_id (parser, template_keyword_p,
13773                                     check_dependency_p,
13774                                     is_declaration);
13775       if (decl == error_mark_node)
13776         return error_mark_node;
13777     }
13778
13779   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13780
13781   /* If this is a typename, create a TYPENAME_TYPE.  */
13782   if (typename_p && decl != error_mark_node)
13783     {
13784       decl = make_typename_type (scope, decl, typename_type,
13785                                  /*complain=*/tf_error);
13786       if (decl != error_mark_node)
13787         decl = TYPE_NAME (decl);
13788     }
13789
13790   /* Check to see that it is really the name of a class.  */
13791   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13792       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13793       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13794     /* Situations like this:
13795
13796          template <typename T> struct A {
13797            typename T::template X<int>::I i;
13798          };
13799
13800        are problematic.  Is `T::template X<int>' a class-name?  The
13801        standard does not seem to be definitive, but there is no other
13802        valid interpretation of the following `::'.  Therefore, those
13803        names are considered class-names.  */
13804     {
13805       decl = make_typename_type (scope, decl, tag_type, tf_error);
13806       if (decl != error_mark_node)
13807         decl = TYPE_NAME (decl);
13808     }
13809   else if (TREE_CODE (decl) != TYPE_DECL
13810            || TREE_TYPE (decl) == error_mark_node
13811            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13812     decl = error_mark_node;
13813
13814   if (decl == error_mark_node)
13815     cp_parser_error (parser, "expected class-name");
13816
13817   return decl;
13818 }
13819
13820 /* Parse a class-specifier.
13821
13822    class-specifier:
13823      class-head { member-specification [opt] }
13824
13825    Returns the TREE_TYPE representing the class.  */
13826
13827 static tree
13828 cp_parser_class_specifier (cp_parser* parser)
13829 {
13830   cp_token *token;
13831   tree type;
13832   tree attributes = NULL_TREE;
13833   int has_trailing_semicolon;
13834   bool nested_name_specifier_p;
13835   unsigned saved_num_template_parameter_lists;
13836   bool saved_in_function_body;
13837   tree old_scope = NULL_TREE;
13838   tree scope = NULL_TREE;
13839   tree bases;
13840
13841   push_deferring_access_checks (dk_no_deferred);
13842
13843   /* Parse the class-head.  */
13844   type = cp_parser_class_head (parser,
13845                                &nested_name_specifier_p,
13846                                &attributes,
13847                                &bases);
13848   /* If the class-head was a semantic disaster, skip the entire body
13849      of the class.  */
13850   if (!type)
13851     {
13852       cp_parser_skip_to_end_of_block_or_statement (parser);
13853       pop_deferring_access_checks ();
13854       return error_mark_node;
13855     }
13856
13857   /* Look for the `{'.  */
13858   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13859     {
13860       pop_deferring_access_checks ();
13861       return error_mark_node;
13862     }
13863
13864   /* Process the base classes. If they're invalid, skip the 
13865      entire class body.  */
13866   if (!xref_basetypes (type, bases))
13867     {
13868       /* Consuming the closing brace yields better error messages
13869          later on.  */
13870       if (cp_parser_skip_to_closing_brace (parser))
13871         cp_lexer_consume_token (parser->lexer);
13872       pop_deferring_access_checks ();
13873       return error_mark_node;
13874     }
13875
13876   /* Issue an error message if type-definitions are forbidden here.  */
13877   cp_parser_check_type_definition (parser);
13878   /* Remember that we are defining one more class.  */
13879   ++parser->num_classes_being_defined;
13880   /* Inside the class, surrounding template-parameter-lists do not
13881      apply.  */
13882   saved_num_template_parameter_lists
13883     = parser->num_template_parameter_lists;
13884   parser->num_template_parameter_lists = 0;
13885   /* We are not in a function body.  */
13886   saved_in_function_body = parser->in_function_body;
13887   parser->in_function_body = false;
13888
13889   /* Start the class.  */
13890   if (nested_name_specifier_p)
13891     {
13892       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13893       old_scope = push_inner_scope (scope);
13894     }
13895   type = begin_class_definition (type, attributes);
13896
13897   if (type == error_mark_node)
13898     /* If the type is erroneous, skip the entire body of the class.  */
13899     cp_parser_skip_to_closing_brace (parser);
13900   else
13901     /* Parse the member-specification.  */
13902     cp_parser_member_specification_opt (parser);
13903
13904   /* Look for the trailing `}'.  */
13905   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13906   /* We get better error messages by noticing a common problem: a
13907      missing trailing `;'.  */
13908   token = cp_lexer_peek_token (parser->lexer);
13909   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13910   /* Look for trailing attributes to apply to this class.  */
13911   if (cp_parser_allow_gnu_extensions_p (parser))
13912     attributes = cp_parser_attributes_opt (parser);
13913   if (type != error_mark_node)
13914     type = finish_struct (type, attributes);
13915   if (nested_name_specifier_p)
13916     pop_inner_scope (old_scope, scope);
13917   /* If this class is not itself within the scope of another class,
13918      then we need to parse the bodies of all of the queued function
13919      definitions.  Note that the queued functions defined in a class
13920      are not always processed immediately following the
13921      class-specifier for that class.  Consider:
13922
13923        struct A {
13924          struct B { void f() { sizeof (A); } };
13925        };
13926
13927      If `f' were processed before the processing of `A' were
13928      completed, there would be no way to compute the size of `A'.
13929      Note that the nesting we are interested in here is lexical --
13930      not the semantic nesting given by TYPE_CONTEXT.  In particular,
13931      for:
13932
13933        struct A { struct B; };
13934        struct A::B { void f() { } };
13935
13936      there is no need to delay the parsing of `A::B::f'.  */
13937   if (--parser->num_classes_being_defined == 0)
13938     {
13939       tree queue_entry;
13940       tree fn;
13941       tree class_type = NULL_TREE;
13942       tree pushed_scope = NULL_TREE;
13943
13944       /* In a first pass, parse default arguments to the functions.
13945          Then, in a second pass, parse the bodies of the functions.
13946          This two-phased approach handles cases like:
13947
13948             struct S {
13949               void f() { g(); }
13950               void g(int i = 3);
13951             };
13952
13953          */
13954       for (TREE_PURPOSE (parser->unparsed_functions_queues)
13955              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13956            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13957            TREE_PURPOSE (parser->unparsed_functions_queues)
13958              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13959         {
13960           fn = TREE_VALUE (queue_entry);
13961           /* If there are default arguments that have not yet been processed,
13962              take care of them now.  */
13963           if (class_type != TREE_PURPOSE (queue_entry))
13964             {
13965               if (pushed_scope)
13966                 pop_scope (pushed_scope);
13967               class_type = TREE_PURPOSE (queue_entry);
13968               pushed_scope = push_scope (class_type);
13969             }
13970           /* Make sure that any template parameters are in scope.  */
13971           maybe_begin_member_template_processing (fn);
13972           /* Parse the default argument expressions.  */
13973           cp_parser_late_parsing_default_args (parser, fn);
13974           /* Remove any template parameters from the symbol table.  */
13975           maybe_end_member_template_processing ();
13976         }
13977       if (pushed_scope)
13978         pop_scope (pushed_scope);
13979       /* Now parse the body of the functions.  */
13980       for (TREE_VALUE (parser->unparsed_functions_queues)
13981              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13982            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13983            TREE_VALUE (parser->unparsed_functions_queues)
13984              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13985         {
13986           /* Figure out which function we need to process.  */
13987           fn = TREE_VALUE (queue_entry);
13988           /* Parse the function.  */
13989           cp_parser_late_parsing_for_member (parser, fn);
13990         }
13991     }
13992
13993   /* Put back any saved access checks.  */
13994   pop_deferring_access_checks ();
13995
13996   /* Restore saved state.  */
13997   parser->in_function_body = saved_in_function_body;
13998   parser->num_template_parameter_lists
13999     = saved_num_template_parameter_lists;
14000
14001   return type;
14002 }
14003
14004 /* Parse a class-head.
14005
14006    class-head:
14007      class-key identifier [opt] base-clause [opt]
14008      class-key nested-name-specifier identifier base-clause [opt]
14009      class-key nested-name-specifier [opt] template-id
14010        base-clause [opt]
14011
14012    GNU Extensions:
14013      class-key attributes identifier [opt] base-clause [opt]
14014      class-key attributes nested-name-specifier identifier base-clause [opt]
14015      class-key attributes nested-name-specifier [opt] template-id
14016        base-clause [opt]
14017
14018    Upon return BASES is initialized to the list of base classes (or
14019    NULL, if there are none) in the same form returned by
14020    cp_parser_base_clause.
14021
14022    Returns the TYPE of the indicated class.  Sets
14023    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14024    involving a nested-name-specifier was used, and FALSE otherwise.
14025
14026    Returns error_mark_node if this is not a class-head.
14027
14028    Returns NULL_TREE if the class-head is syntactically valid, but
14029    semantically invalid in a way that means we should skip the entire
14030    body of the class.  */
14031
14032 static tree
14033 cp_parser_class_head (cp_parser* parser,
14034                       bool* nested_name_specifier_p,
14035                       tree *attributes_p,
14036                       tree *bases)
14037 {
14038   tree nested_name_specifier;
14039   enum tag_types class_key;
14040   tree id = NULL_TREE;
14041   tree type = NULL_TREE;
14042   tree attributes;
14043   bool template_id_p = false;
14044   bool qualified_p = false;
14045   bool invalid_nested_name_p = false;
14046   bool invalid_explicit_specialization_p = false;
14047   tree pushed_scope = NULL_TREE;
14048   unsigned num_templates;
14049
14050   /* Assume no nested-name-specifier will be present.  */
14051   *nested_name_specifier_p = false;
14052   /* Assume no template parameter lists will be used in defining the
14053      type.  */
14054   num_templates = 0;
14055
14056   *bases = NULL_TREE;
14057
14058   /* Look for the class-key.  */
14059   class_key = cp_parser_class_key (parser);
14060   if (class_key == none_type)
14061     return error_mark_node;
14062
14063   /* Parse the attributes.  */
14064   attributes = cp_parser_attributes_opt (parser);
14065
14066   /* If the next token is `::', that is invalid -- but sometimes
14067      people do try to write:
14068
14069        struct ::S {};
14070
14071      Handle this gracefully by accepting the extra qualifier, and then
14072      issuing an error about it later if this really is a
14073      class-head.  If it turns out just to be an elaborated type
14074      specifier, remain silent.  */
14075   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14076     qualified_p = true;
14077
14078   push_deferring_access_checks (dk_no_check);
14079
14080   /* Determine the name of the class.  Begin by looking for an
14081      optional nested-name-specifier.  */
14082   nested_name_specifier
14083     = cp_parser_nested_name_specifier_opt (parser,
14084                                            /*typename_keyword_p=*/false,
14085                                            /*check_dependency_p=*/false,
14086                                            /*type_p=*/false,
14087                                            /*is_declaration=*/false);
14088   /* If there was a nested-name-specifier, then there *must* be an
14089      identifier.  */
14090   if (nested_name_specifier)
14091     {
14092       /* Although the grammar says `identifier', it really means
14093          `class-name' or `template-name'.  You are only allowed to
14094          define a class that has already been declared with this
14095          syntax.
14096
14097          The proposed resolution for Core Issue 180 says that wherever
14098          you see `class T::X' you should treat `X' as a type-name.
14099
14100          It is OK to define an inaccessible class; for example:
14101
14102            class A { class B; };
14103            class A::B {};
14104
14105          We do not know if we will see a class-name, or a
14106          template-name.  We look for a class-name first, in case the
14107          class-name is a template-id; if we looked for the
14108          template-name first we would stop after the template-name.  */
14109       cp_parser_parse_tentatively (parser);
14110       type = cp_parser_class_name (parser,
14111                                    /*typename_keyword_p=*/false,
14112                                    /*template_keyword_p=*/false,
14113                                    class_type,
14114                                    /*check_dependency_p=*/false,
14115                                    /*class_head_p=*/true,
14116                                    /*is_declaration=*/false);
14117       /* If that didn't work, ignore the nested-name-specifier.  */
14118       if (!cp_parser_parse_definitely (parser))
14119         {
14120           invalid_nested_name_p = true;
14121           id = cp_parser_identifier (parser);
14122           if (id == error_mark_node)
14123             id = NULL_TREE;
14124         }
14125       /* If we could not find a corresponding TYPE, treat this
14126          declaration like an unqualified declaration.  */
14127       if (type == error_mark_node)
14128         nested_name_specifier = NULL_TREE;
14129       /* Otherwise, count the number of templates used in TYPE and its
14130          containing scopes.  */
14131       else
14132         {
14133           tree scope;
14134
14135           for (scope = TREE_TYPE (type);
14136                scope && TREE_CODE (scope) != NAMESPACE_DECL;
14137                scope = (TYPE_P (scope)
14138                         ? TYPE_CONTEXT (scope)
14139                         : DECL_CONTEXT (scope)))
14140             if (TYPE_P (scope)
14141                 && CLASS_TYPE_P (scope)
14142                 && CLASSTYPE_TEMPLATE_INFO (scope)
14143                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14144                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14145               ++num_templates;
14146         }
14147     }
14148   /* Otherwise, the identifier is optional.  */
14149   else
14150     {
14151       /* We don't know whether what comes next is a template-id,
14152          an identifier, or nothing at all.  */
14153       cp_parser_parse_tentatively (parser);
14154       /* Check for a template-id.  */
14155       id = cp_parser_template_id (parser,
14156                                   /*template_keyword_p=*/false,
14157                                   /*check_dependency_p=*/true,
14158                                   /*is_declaration=*/true);
14159       /* If that didn't work, it could still be an identifier.  */
14160       if (!cp_parser_parse_definitely (parser))
14161         {
14162           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14163             id = cp_parser_identifier (parser);
14164           else
14165             id = NULL_TREE;
14166         }
14167       else
14168         {
14169           template_id_p = true;
14170           ++num_templates;
14171         }
14172     }
14173
14174   pop_deferring_access_checks ();
14175
14176   if (id)
14177     cp_parser_check_for_invalid_template_id (parser, id);
14178
14179   /* If it's not a `:' or a `{' then we can't really be looking at a
14180      class-head, since a class-head only appears as part of a
14181      class-specifier.  We have to detect this situation before calling
14182      xref_tag, since that has irreversible side-effects.  */
14183   if (!cp_parser_next_token_starts_class_definition_p (parser))
14184     {
14185       cp_parser_error (parser, "expected %<{%> or %<:%>");
14186       return error_mark_node;
14187     }
14188
14189   /* At this point, we're going ahead with the class-specifier, even
14190      if some other problem occurs.  */
14191   cp_parser_commit_to_tentative_parse (parser);
14192   /* Issue the error about the overly-qualified name now.  */
14193   if (qualified_p)
14194     cp_parser_error (parser,
14195                      "global qualification of class name is invalid");
14196   else if (invalid_nested_name_p)
14197     cp_parser_error (parser,
14198                      "qualified name does not name a class");
14199   else if (nested_name_specifier)
14200     {
14201       tree scope;
14202
14203       /* Reject typedef-names in class heads.  */
14204       if (!DECL_IMPLICIT_TYPEDEF_P (type))
14205         {
14206           error ("invalid class name in declaration of %qD", type);
14207           type = NULL_TREE;
14208           goto done;
14209         }
14210
14211       /* Figure out in what scope the declaration is being placed.  */
14212       scope = current_scope ();
14213       /* If that scope does not contain the scope in which the
14214          class was originally declared, the program is invalid.  */
14215       if (scope && !is_ancestor (scope, nested_name_specifier))
14216         {
14217           error ("declaration of %qD in %qD which does not enclose %qD",
14218                  type, scope, nested_name_specifier);
14219           type = NULL_TREE;
14220           goto done;
14221         }
14222       /* [dcl.meaning]
14223
14224          A declarator-id shall not be qualified exception of the
14225          definition of a ... nested class outside of its class
14226          ... [or] a the definition or explicit instantiation of a
14227          class member of a namespace outside of its namespace.  */
14228       if (scope == nested_name_specifier)
14229         {
14230           pedwarn ("extra qualification ignored");
14231           nested_name_specifier = NULL_TREE;
14232           num_templates = 0;
14233         }
14234     }
14235   /* An explicit-specialization must be preceded by "template <>".  If
14236      it is not, try to recover gracefully.  */
14237   if (at_namespace_scope_p ()
14238       && parser->num_template_parameter_lists == 0
14239       && template_id_p)
14240     {
14241       error ("an explicit specialization must be preceded by %<template <>%>");
14242       invalid_explicit_specialization_p = true;
14243       /* Take the same action that would have been taken by
14244          cp_parser_explicit_specialization.  */
14245       ++parser->num_template_parameter_lists;
14246       begin_specialization ();
14247     }
14248   /* There must be no "return" statements between this point and the
14249      end of this function; set "type "to the correct return value and
14250      use "goto done;" to return.  */
14251   /* Make sure that the right number of template parameters were
14252      present.  */
14253   if (!cp_parser_check_template_parameters (parser, num_templates))
14254     {
14255       /* If something went wrong, there is no point in even trying to
14256          process the class-definition.  */
14257       type = NULL_TREE;
14258       goto done;
14259     }
14260
14261   /* Look up the type.  */
14262   if (template_id_p)
14263     {
14264       type = TREE_TYPE (id);
14265       type = maybe_process_partial_specialization (type);
14266       if (nested_name_specifier)
14267         pushed_scope = push_scope (nested_name_specifier);
14268     }
14269   else if (nested_name_specifier)
14270     {
14271       tree class_type;
14272
14273       /* Given:
14274
14275             template <typename T> struct S { struct T };
14276             template <typename T> struct S<T>::T { };
14277
14278          we will get a TYPENAME_TYPE when processing the definition of
14279          `S::T'.  We need to resolve it to the actual type before we
14280          try to define it.  */
14281       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14282         {
14283           class_type = resolve_typename_type (TREE_TYPE (type),
14284                                               /*only_current_p=*/false);
14285           if (TREE_CODE (class_type) != TYPENAME_TYPE)
14286             type = TYPE_NAME (class_type);
14287           else
14288             {
14289               cp_parser_error (parser, "could not resolve typename type");
14290               type = error_mark_node;
14291             }
14292         }
14293
14294       maybe_process_partial_specialization (TREE_TYPE (type));
14295       class_type = current_class_type;
14296       /* Enter the scope indicated by the nested-name-specifier.  */
14297       pushed_scope = push_scope (nested_name_specifier);
14298       /* Get the canonical version of this type.  */
14299       type = TYPE_MAIN_DECL (TREE_TYPE (type));
14300       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14301           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14302         {
14303           type = push_template_decl (type);
14304           if (type == error_mark_node)
14305             {
14306               type = NULL_TREE;
14307               goto done;
14308             }
14309         }
14310
14311       type = TREE_TYPE (type);
14312       *nested_name_specifier_p = true;
14313     }
14314   else      /* The name is not a nested name.  */
14315     {
14316       /* If the class was unnamed, create a dummy name.  */
14317       if (!id)
14318         id = make_anon_name ();
14319       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14320                        parser->num_template_parameter_lists);
14321     }
14322
14323   /* Indicate whether this class was declared as a `class' or as a
14324      `struct'.  */
14325   if (TREE_CODE (type) == RECORD_TYPE)
14326     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14327   cp_parser_check_class_key (class_key, type);
14328
14329   /* If this type was already complete, and we see another definition,
14330      that's an error.  */
14331   if (type != error_mark_node && COMPLETE_TYPE_P (type))
14332     {
14333       error ("redefinition of %q#T", type);
14334       error ("previous definition of %q+#T", type);
14335       type = NULL_TREE;
14336       goto done;
14337     }
14338   else if (type == error_mark_node)
14339     type = NULL_TREE;
14340
14341   /* We will have entered the scope containing the class; the names of
14342      base classes should be looked up in that context.  For example:
14343
14344        struct A { struct B {}; struct C; };
14345        struct A::C : B {};
14346
14347      is valid.  */
14348
14349   /* Get the list of base-classes, if there is one.  */
14350   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14351     *bases = cp_parser_base_clause (parser);
14352
14353  done:
14354   /* Leave the scope given by the nested-name-specifier.  We will
14355      enter the class scope itself while processing the members.  */
14356   if (pushed_scope)
14357     pop_scope (pushed_scope);
14358
14359   if (invalid_explicit_specialization_p)
14360     {
14361       end_specialization ();
14362       --parser->num_template_parameter_lists;
14363     }
14364   *attributes_p = attributes;
14365   return type;
14366 }
14367
14368 /* Parse a class-key.
14369
14370    class-key:
14371      class
14372      struct
14373      union
14374
14375    Returns the kind of class-key specified, or none_type to indicate
14376    error.  */
14377
14378 static enum tag_types
14379 cp_parser_class_key (cp_parser* parser)
14380 {
14381   cp_token *token;
14382   enum tag_types tag_type;
14383
14384   /* Look for the class-key.  */
14385   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14386   if (!token)
14387     return none_type;
14388
14389   /* Check to see if the TOKEN is a class-key.  */
14390   tag_type = cp_parser_token_is_class_key (token);
14391   if (!tag_type)
14392     cp_parser_error (parser, "expected class-key");
14393   return tag_type;
14394 }
14395
14396 /* Parse an (optional) member-specification.
14397
14398    member-specification:
14399      member-declaration member-specification [opt]
14400      access-specifier : member-specification [opt]  */
14401
14402 static void
14403 cp_parser_member_specification_opt (cp_parser* parser)
14404 {
14405   while (true)
14406     {
14407       cp_token *token;
14408       enum rid keyword;
14409
14410       /* Peek at the next token.  */
14411       token = cp_lexer_peek_token (parser->lexer);
14412       /* If it's a `}', or EOF then we've seen all the members.  */
14413       if (token->type == CPP_CLOSE_BRACE
14414           || token->type == CPP_EOF
14415           || token->type == CPP_PRAGMA_EOL)
14416         break;
14417
14418       /* See if this token is a keyword.  */
14419       keyword = token->keyword;
14420       switch (keyword)
14421         {
14422         case RID_PUBLIC:
14423         case RID_PROTECTED:
14424         case RID_PRIVATE:
14425           /* Consume the access-specifier.  */
14426           cp_lexer_consume_token (parser->lexer);
14427           /* Remember which access-specifier is active.  */
14428           current_access_specifier = token->u.value;
14429           /* Look for the `:'.  */
14430           cp_parser_require (parser, CPP_COLON, "`:'");
14431           break;
14432
14433         default:
14434           /* Accept #pragmas at class scope.  */
14435           if (token->type == CPP_PRAGMA)
14436             {
14437               cp_parser_pragma (parser, pragma_external);
14438               break;
14439             }
14440
14441           /* Otherwise, the next construction must be a
14442              member-declaration.  */
14443           cp_parser_member_declaration (parser);
14444         }
14445     }
14446 }
14447
14448 /* Parse a member-declaration.
14449
14450    member-declaration:
14451      decl-specifier-seq [opt] member-declarator-list [opt] ;
14452      function-definition ; [opt]
14453      :: [opt] nested-name-specifier template [opt] unqualified-id ;
14454      using-declaration
14455      template-declaration
14456
14457    member-declarator-list:
14458      member-declarator
14459      member-declarator-list , member-declarator
14460
14461    member-declarator:
14462      declarator pure-specifier [opt]
14463      declarator constant-initializer [opt]
14464      identifier [opt] : constant-expression
14465
14466    GNU Extensions:
14467
14468    member-declaration:
14469      __extension__ member-declaration
14470
14471    member-declarator:
14472      declarator attributes [opt] pure-specifier [opt]
14473      declarator attributes [opt] constant-initializer [opt]
14474      identifier [opt] attributes [opt] : constant-expression  
14475
14476    C++0x Extensions:
14477
14478    member-declaration:
14479      static_assert-declaration  */
14480
14481 static void
14482 cp_parser_member_declaration (cp_parser* parser)
14483 {
14484   cp_decl_specifier_seq decl_specifiers;
14485   tree prefix_attributes;
14486   tree decl;
14487   int declares_class_or_enum;
14488   bool friend_p;
14489   cp_token *token;
14490   int saved_pedantic;
14491
14492   /* Check for the `__extension__' keyword.  */
14493   if (cp_parser_extension_opt (parser, &saved_pedantic))
14494     {
14495       /* Recurse.  */
14496       cp_parser_member_declaration (parser);
14497       /* Restore the old value of the PEDANTIC flag.  */
14498       pedantic = saved_pedantic;
14499
14500       return;
14501     }
14502
14503   /* Check for a template-declaration.  */
14504   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14505     {
14506       /* An explicit specialization here is an error condition, and we
14507          expect the specialization handler to detect and report this.  */
14508       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14509           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14510         cp_parser_explicit_specialization (parser);
14511       else
14512         cp_parser_template_declaration (parser, /*member_p=*/true);
14513
14514       return;
14515     }
14516
14517   /* Check for a using-declaration.  */
14518   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14519     {
14520       /* Parse the using-declaration.  */
14521       cp_parser_using_declaration (parser,
14522                                    /*access_declaration_p=*/false);
14523       return;
14524     }
14525
14526   /* Check for @defs.  */
14527   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14528     {
14529       tree ivar, member;
14530       tree ivar_chains = cp_parser_objc_defs_expression (parser);
14531       ivar = ivar_chains;
14532       while (ivar)
14533         {
14534           member = ivar;
14535           ivar = TREE_CHAIN (member);
14536           TREE_CHAIN (member) = NULL_TREE;
14537           finish_member_declaration (member);
14538         }
14539       return;
14540     }
14541
14542   /* If the next token is `static_assert' we have a static assertion.  */
14543   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14544     {
14545       cp_parser_static_assert (parser, /*member_p=*/true);
14546       return;
14547     }
14548
14549   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14550     return;
14551
14552   /* Parse the decl-specifier-seq.  */
14553   cp_parser_decl_specifier_seq (parser,
14554                                 CP_PARSER_FLAGS_OPTIONAL,
14555                                 &decl_specifiers,
14556                                 &declares_class_or_enum);
14557   prefix_attributes = decl_specifiers.attributes;
14558   decl_specifiers.attributes = NULL_TREE;
14559   /* Check for an invalid type-name.  */
14560   if (!decl_specifiers.type
14561       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14562     return;
14563   /* If there is no declarator, then the decl-specifier-seq should
14564      specify a type.  */
14565   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14566     {
14567       /* If there was no decl-specifier-seq, and the next token is a
14568          `;', then we have something like:
14569
14570            struct S { ; };
14571
14572          [class.mem]
14573
14574          Each member-declaration shall declare at least one member
14575          name of the class.  */
14576       if (!decl_specifiers.any_specifiers_p)
14577         {
14578           cp_token *token = cp_lexer_peek_token (parser->lexer);
14579           if (pedantic && !token->in_system_header)
14580             pedwarn ("%Hextra %<;%>", &token->location);
14581         }
14582       else
14583         {
14584           tree type;
14585
14586           /* See if this declaration is a friend.  */
14587           friend_p = cp_parser_friend_p (&decl_specifiers);
14588           /* If there were decl-specifiers, check to see if there was
14589              a class-declaration.  */
14590           type = check_tag_decl (&decl_specifiers);
14591           /* Nested classes have already been added to the class, but
14592              a `friend' needs to be explicitly registered.  */
14593           if (friend_p)
14594             {
14595               /* If the `friend' keyword was present, the friend must
14596                  be introduced with a class-key.  */
14597                if (!declares_class_or_enum)
14598                  error ("a class-key must be used when declaring a friend");
14599                /* In this case:
14600
14601                     template <typename T> struct A {
14602                       friend struct A<T>::B;
14603                     };
14604
14605                   A<T>::B will be represented by a TYPENAME_TYPE, and
14606                   therefore not recognized by check_tag_decl.  */
14607                if (!type
14608                    && decl_specifiers.type
14609                    && TYPE_P (decl_specifiers.type))
14610                  type = decl_specifiers.type;
14611                if (!type || !TYPE_P (type))
14612                  error ("friend declaration does not name a class or "
14613                         "function");
14614                else
14615                  make_friend_class (current_class_type, type,
14616                                     /*complain=*/true);
14617             }
14618           /* If there is no TYPE, an error message will already have
14619              been issued.  */
14620           else if (!type || type == error_mark_node)
14621             ;
14622           /* An anonymous aggregate has to be handled specially; such
14623              a declaration really declares a data member (with a
14624              particular type), as opposed to a nested class.  */
14625           else if (ANON_AGGR_TYPE_P (type))
14626             {
14627               /* Remove constructors and such from TYPE, now that we
14628                  know it is an anonymous aggregate.  */
14629               fixup_anonymous_aggr (type);
14630               /* And make the corresponding data member.  */
14631               decl = build_decl (FIELD_DECL, NULL_TREE, type);
14632               /* Add it to the class.  */
14633               finish_member_declaration (decl);
14634             }
14635           else
14636             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14637         }
14638     }
14639   else
14640     {
14641       /* See if these declarations will be friends.  */
14642       friend_p = cp_parser_friend_p (&decl_specifiers);
14643
14644       /* Keep going until we hit the `;' at the end of the
14645          declaration.  */
14646       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14647         {
14648           tree attributes = NULL_TREE;
14649           tree first_attribute;
14650
14651           /* Peek at the next token.  */
14652           token = cp_lexer_peek_token (parser->lexer);
14653
14654           /* Check for a bitfield declaration.  */
14655           if (token->type == CPP_COLON
14656               || (token->type == CPP_NAME
14657                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14658                   == CPP_COLON))
14659             {
14660               tree identifier;
14661               tree width;
14662
14663               /* Get the name of the bitfield.  Note that we cannot just
14664                  check TOKEN here because it may have been invalidated by
14665                  the call to cp_lexer_peek_nth_token above.  */
14666               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14667                 identifier = cp_parser_identifier (parser);
14668               else
14669                 identifier = NULL_TREE;
14670
14671               /* Consume the `:' token.  */
14672               cp_lexer_consume_token (parser->lexer);
14673               /* Get the width of the bitfield.  */
14674               width
14675                 = cp_parser_constant_expression (parser,
14676                                                  /*allow_non_constant=*/false,
14677                                                  NULL);
14678
14679               /* Look for attributes that apply to the bitfield.  */
14680               attributes = cp_parser_attributes_opt (parser);
14681               /* Remember which attributes are prefix attributes and
14682                  which are not.  */
14683               first_attribute = attributes;
14684               /* Combine the attributes.  */
14685               attributes = chainon (prefix_attributes, attributes);
14686
14687               /* Create the bitfield declaration.  */
14688               decl = grokbitfield (identifier
14689                                    ? make_id_declarator (NULL_TREE,
14690                                                          identifier,
14691                                                          sfk_none)
14692                                    : NULL,
14693                                    &decl_specifiers,
14694                                    width);
14695               /* Apply the attributes.  */
14696               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14697             }
14698           else
14699             {
14700               cp_declarator *declarator;
14701               tree initializer;
14702               tree asm_specification;
14703               int ctor_dtor_or_conv_p;
14704
14705               /* Parse the declarator.  */
14706               declarator
14707                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14708                                         &ctor_dtor_or_conv_p,
14709                                         /*parenthesized_p=*/NULL,
14710                                         /*member_p=*/true);
14711
14712               /* If something went wrong parsing the declarator, make sure
14713                  that we at least consume some tokens.  */
14714               if (declarator == cp_error_declarator)
14715                 {
14716                   /* Skip to the end of the statement.  */
14717                   cp_parser_skip_to_end_of_statement (parser);
14718                   /* If the next token is not a semicolon, that is
14719                      probably because we just skipped over the body of
14720                      a function.  So, we consume a semicolon if
14721                      present, but do not issue an error message if it
14722                      is not present.  */
14723                   if (cp_lexer_next_token_is (parser->lexer,
14724                                               CPP_SEMICOLON))
14725                     cp_lexer_consume_token (parser->lexer);
14726                   return;
14727                 }
14728
14729               if (declares_class_or_enum & 2)
14730                 cp_parser_check_for_definition_in_return_type
14731                   (declarator, decl_specifiers.type);
14732
14733               /* Look for an asm-specification.  */
14734               asm_specification = cp_parser_asm_specification_opt (parser);
14735               /* Look for attributes that apply to the declaration.  */
14736               attributes = cp_parser_attributes_opt (parser);
14737               /* Remember which attributes are prefix attributes and
14738                  which are not.  */
14739               first_attribute = attributes;
14740               /* Combine the attributes.  */
14741               attributes = chainon (prefix_attributes, attributes);
14742
14743               /* If it's an `=', then we have a constant-initializer or a
14744                  pure-specifier.  It is not correct to parse the
14745                  initializer before registering the member declaration
14746                  since the member declaration should be in scope while
14747                  its initializer is processed.  However, the rest of the
14748                  front end does not yet provide an interface that allows
14749                  us to handle this correctly.  */
14750               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14751                 {
14752                   /* In [class.mem]:
14753
14754                      A pure-specifier shall be used only in the declaration of
14755                      a virtual function.
14756
14757                      A member-declarator can contain a constant-initializer
14758                      only if it declares a static member of integral or
14759                      enumeration type.
14760
14761                      Therefore, if the DECLARATOR is for a function, we look
14762                      for a pure-specifier; otherwise, we look for a
14763                      constant-initializer.  When we call `grokfield', it will
14764                      perform more stringent semantics checks.  */
14765                   if (function_declarator_p (declarator))
14766                     initializer = cp_parser_pure_specifier (parser);
14767                   else
14768                     /* Parse the initializer.  */
14769                     initializer = cp_parser_constant_initializer (parser);
14770                 }
14771               /* Otherwise, there is no initializer.  */
14772               else
14773                 initializer = NULL_TREE;
14774
14775               /* See if we are probably looking at a function
14776                  definition.  We are certainly not looking at a
14777                  member-declarator.  Calling `grokfield' has
14778                  side-effects, so we must not do it unless we are sure
14779                  that we are looking at a member-declarator.  */
14780               if (cp_parser_token_starts_function_definition_p
14781                   (cp_lexer_peek_token (parser->lexer)))
14782                 {
14783                   /* The grammar does not allow a pure-specifier to be
14784                      used when a member function is defined.  (It is
14785                      possible that this fact is an oversight in the
14786                      standard, since a pure function may be defined
14787                      outside of the class-specifier.  */
14788                   if (initializer)
14789                     error ("pure-specifier on function-definition");
14790                   decl = cp_parser_save_member_function_body (parser,
14791                                                               &decl_specifiers,
14792                                                               declarator,
14793                                                               attributes);
14794                   /* If the member was not a friend, declare it here.  */
14795                   if (!friend_p)
14796                     finish_member_declaration (decl);
14797                   /* Peek at the next token.  */
14798                   token = cp_lexer_peek_token (parser->lexer);
14799                   /* If the next token is a semicolon, consume it.  */
14800                   if (token->type == CPP_SEMICOLON)
14801                     {
14802                       if (pedantic && !in_system_header)
14803                         pedwarn ("extra %<;%>");
14804                       cp_lexer_consume_token (parser->lexer);
14805                     }
14806                   return;
14807                 }
14808               else
14809                 /* Create the declaration.  */
14810                 decl = grokfield (declarator, &decl_specifiers,
14811                                   initializer, /*init_const_expr_p=*/true,
14812                                   asm_specification,
14813                                   attributes);
14814             }
14815
14816           /* Reset PREFIX_ATTRIBUTES.  */
14817           while (attributes && TREE_CHAIN (attributes) != first_attribute)
14818             attributes = TREE_CHAIN (attributes);
14819           if (attributes)
14820             TREE_CHAIN (attributes) = NULL_TREE;
14821
14822           /* If there is any qualification still in effect, clear it
14823              now; we will be starting fresh with the next declarator.  */
14824           parser->scope = NULL_TREE;
14825           parser->qualifying_scope = NULL_TREE;
14826           parser->object_scope = NULL_TREE;
14827           /* If it's a `,', then there are more declarators.  */
14828           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14829             cp_lexer_consume_token (parser->lexer);
14830           /* If the next token isn't a `;', then we have a parse error.  */
14831           else if (cp_lexer_next_token_is_not (parser->lexer,
14832                                                CPP_SEMICOLON))
14833             {
14834               cp_parser_error (parser, "expected %<;%>");
14835               /* Skip tokens until we find a `;'.  */
14836               cp_parser_skip_to_end_of_statement (parser);
14837
14838               break;
14839             }
14840
14841           if (decl)
14842             {
14843               /* Add DECL to the list of members.  */
14844               if (!friend_p)
14845                 finish_member_declaration (decl);
14846
14847               if (TREE_CODE (decl) == FUNCTION_DECL)
14848                 cp_parser_save_default_args (parser, decl);
14849             }
14850         }
14851     }
14852
14853   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14854 }
14855
14856 /* Parse a pure-specifier.
14857
14858    pure-specifier:
14859      = 0
14860
14861    Returns INTEGER_ZERO_NODE if a pure specifier is found.
14862    Otherwise, ERROR_MARK_NODE is returned.  */
14863
14864 static tree
14865 cp_parser_pure_specifier (cp_parser* parser)
14866 {
14867   cp_token *token;
14868
14869   /* Look for the `=' token.  */
14870   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14871     return error_mark_node;
14872   /* Look for the `0' token.  */
14873   token = cp_lexer_consume_token (parser->lexer);
14874   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
14875   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14876     {
14877       cp_parser_error (parser,
14878                        "invalid pure specifier (only `= 0' is allowed)");
14879       cp_parser_skip_to_end_of_statement (parser);
14880       return error_mark_node;
14881     }
14882   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14883     {
14884       error ("templates may not be %<virtual%>");
14885       return error_mark_node;
14886     }
14887
14888   return integer_zero_node;
14889 }
14890
14891 /* Parse a constant-initializer.
14892
14893    constant-initializer:
14894      = constant-expression
14895
14896    Returns a representation of the constant-expression.  */
14897
14898 static tree
14899 cp_parser_constant_initializer (cp_parser* parser)
14900 {
14901   /* Look for the `=' token.  */
14902   if (!cp_parser_require (parser, CPP_EQ, "`='"))
14903     return error_mark_node;
14904
14905   /* It is invalid to write:
14906
14907        struct S { static const int i = { 7 }; };
14908
14909      */
14910   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14911     {
14912       cp_parser_error (parser,
14913                        "a brace-enclosed initializer is not allowed here");
14914       /* Consume the opening brace.  */
14915       cp_lexer_consume_token (parser->lexer);
14916       /* Skip the initializer.  */
14917       cp_parser_skip_to_closing_brace (parser);
14918       /* Look for the trailing `}'.  */
14919       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14920
14921       return error_mark_node;
14922     }
14923
14924   return cp_parser_constant_expression (parser,
14925                                         /*allow_non_constant=*/false,
14926                                         NULL);
14927 }
14928
14929 /* Derived classes [gram.class.derived] */
14930
14931 /* Parse a base-clause.
14932
14933    base-clause:
14934      : base-specifier-list
14935
14936    base-specifier-list:
14937      base-specifier ... [opt]
14938      base-specifier-list , base-specifier ... [opt]
14939
14940    Returns a TREE_LIST representing the base-classes, in the order in
14941    which they were declared.  The representation of each node is as
14942    described by cp_parser_base_specifier.
14943
14944    In the case that no bases are specified, this function will return
14945    NULL_TREE, not ERROR_MARK_NODE.  */
14946
14947 static tree
14948 cp_parser_base_clause (cp_parser* parser)
14949 {
14950   tree bases = NULL_TREE;
14951
14952   /* Look for the `:' that begins the list.  */
14953   cp_parser_require (parser, CPP_COLON, "`:'");
14954
14955   /* Scan the base-specifier-list.  */
14956   while (true)
14957     {
14958       cp_token *token;
14959       tree base;
14960       bool pack_expansion_p = false;
14961
14962       /* Look for the base-specifier.  */
14963       base = cp_parser_base_specifier (parser);
14964       /* Look for the (optional) ellipsis. */
14965       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14966         {
14967           /* Consume the `...'. */
14968           cp_lexer_consume_token (parser->lexer);
14969
14970           pack_expansion_p = true;
14971         }
14972
14973       /* Add BASE to the front of the list.  */
14974       if (base != error_mark_node)
14975         {
14976           if (pack_expansion_p)
14977             /* Make this a pack expansion type. */
14978             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
14979           else
14980             check_for_bare_parameter_packs (TREE_VALUE (base));
14981
14982           TREE_CHAIN (base) = bases;
14983           bases = base;
14984         }
14985       /* Peek at the next token.  */
14986       token = cp_lexer_peek_token (parser->lexer);
14987       /* If it's not a comma, then the list is complete.  */
14988       if (token->type != CPP_COMMA)
14989         break;
14990       /* Consume the `,'.  */
14991       cp_lexer_consume_token (parser->lexer);
14992     }
14993
14994   /* PARSER->SCOPE may still be non-NULL at this point, if the last
14995      base class had a qualified name.  However, the next name that
14996      appears is certainly not qualified.  */
14997   parser->scope = NULL_TREE;
14998   parser->qualifying_scope = NULL_TREE;
14999   parser->object_scope = NULL_TREE;
15000
15001   return nreverse (bases);
15002 }
15003
15004 /* Parse a base-specifier.
15005
15006    base-specifier:
15007      :: [opt] nested-name-specifier [opt] class-name
15008      virtual access-specifier [opt] :: [opt] nested-name-specifier
15009        [opt] class-name
15010      access-specifier virtual [opt] :: [opt] nested-name-specifier
15011        [opt] class-name
15012
15013    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15014    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15015    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15016    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15017
15018 static tree
15019 cp_parser_base_specifier (cp_parser* parser)
15020 {
15021   cp_token *token;
15022   bool done = false;
15023   bool virtual_p = false;
15024   bool duplicate_virtual_error_issued_p = false;
15025   bool duplicate_access_error_issued_p = false;
15026   bool class_scope_p, template_p;
15027   tree access = access_default_node;
15028   tree type;
15029
15030   /* Process the optional `virtual' and `access-specifier'.  */
15031   while (!done)
15032     {
15033       /* Peek at the next token.  */
15034       token = cp_lexer_peek_token (parser->lexer);
15035       /* Process `virtual'.  */
15036       switch (token->keyword)
15037         {
15038         case RID_VIRTUAL:
15039           /* If `virtual' appears more than once, issue an error.  */
15040           if (virtual_p && !duplicate_virtual_error_issued_p)
15041             {
15042               cp_parser_error (parser,
15043                                "%<virtual%> specified more than once in base-specified");
15044               duplicate_virtual_error_issued_p = true;
15045             }
15046
15047           virtual_p = true;
15048
15049           /* Consume the `virtual' token.  */
15050           cp_lexer_consume_token (parser->lexer);
15051
15052           break;
15053
15054         case RID_PUBLIC:
15055         case RID_PROTECTED:
15056         case RID_PRIVATE:
15057           /* If more than one access specifier appears, issue an
15058              error.  */
15059           if (access != access_default_node
15060               && !duplicate_access_error_issued_p)
15061             {
15062               cp_parser_error (parser,
15063                                "more than one access specifier in base-specified");
15064               duplicate_access_error_issued_p = true;
15065             }
15066
15067           access = ridpointers[(int) token->keyword];
15068
15069           /* Consume the access-specifier.  */
15070           cp_lexer_consume_token (parser->lexer);
15071
15072           break;
15073
15074         default:
15075           done = true;
15076           break;
15077         }
15078     }
15079   /* It is not uncommon to see programs mechanically, erroneously, use
15080      the 'typename' keyword to denote (dependent) qualified types
15081      as base classes.  */
15082   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15083     {
15084       if (!processing_template_decl)
15085         error ("keyword %<typename%> not allowed outside of templates");
15086       else
15087         error ("keyword %<typename%> not allowed in this context "
15088                "(the base class is implicitly a type)");
15089       cp_lexer_consume_token (parser->lexer);
15090     }
15091
15092   /* Look for the optional `::' operator.  */
15093   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15094   /* Look for the nested-name-specifier.  The simplest way to
15095      implement:
15096
15097        [temp.res]
15098
15099        The keyword `typename' is not permitted in a base-specifier or
15100        mem-initializer; in these contexts a qualified name that
15101        depends on a template-parameter is implicitly assumed to be a
15102        type name.
15103
15104      is to pretend that we have seen the `typename' keyword at this
15105      point.  */
15106   cp_parser_nested_name_specifier_opt (parser,
15107                                        /*typename_keyword_p=*/true,
15108                                        /*check_dependency_p=*/true,
15109                                        typename_type,
15110                                        /*is_declaration=*/true);
15111   /* If the base class is given by a qualified name, assume that names
15112      we see are type names or templates, as appropriate.  */
15113   class_scope_p = (parser->scope && TYPE_P (parser->scope));
15114   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15115
15116   /* Finally, look for the class-name.  */
15117   type = cp_parser_class_name (parser,
15118                                class_scope_p,
15119                                template_p,
15120                                typename_type,
15121                                /*check_dependency_p=*/true,
15122                                /*class_head_p=*/false,
15123                                /*is_declaration=*/true);
15124
15125   if (type == error_mark_node)
15126     return error_mark_node;
15127
15128   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15129 }
15130
15131 /* Exception handling [gram.exception] */
15132
15133 /* Parse an (optional) exception-specification.
15134
15135    exception-specification:
15136      throw ( type-id-list [opt] )
15137
15138    Returns a TREE_LIST representing the exception-specification.  The
15139    TREE_VALUE of each node is a type.  */
15140
15141 static tree
15142 cp_parser_exception_specification_opt (cp_parser* parser)
15143 {
15144   cp_token *token;
15145   tree type_id_list;
15146
15147   /* Peek at the next token.  */
15148   token = cp_lexer_peek_token (parser->lexer);
15149   /* If it's not `throw', then there's no exception-specification.  */
15150   if (!cp_parser_is_keyword (token, RID_THROW))
15151     return NULL_TREE;
15152
15153   /* Consume the `throw'.  */
15154   cp_lexer_consume_token (parser->lexer);
15155
15156   /* Look for the `('.  */
15157   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15158
15159   /* Peek at the next token.  */
15160   token = cp_lexer_peek_token (parser->lexer);
15161   /* If it's not a `)', then there is a type-id-list.  */
15162   if (token->type != CPP_CLOSE_PAREN)
15163     {
15164       const char *saved_message;
15165
15166       /* Types may not be defined in an exception-specification.  */
15167       saved_message = parser->type_definition_forbidden_message;
15168       parser->type_definition_forbidden_message
15169         = "types may not be defined in an exception-specification";
15170       /* Parse the type-id-list.  */
15171       type_id_list = cp_parser_type_id_list (parser);
15172       /* Restore the saved message.  */
15173       parser->type_definition_forbidden_message = saved_message;
15174     }
15175   else
15176     type_id_list = empty_except_spec;
15177
15178   /* Look for the `)'.  */
15179   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15180
15181   return type_id_list;
15182 }
15183
15184 /* Parse an (optional) type-id-list.
15185
15186    type-id-list:
15187      type-id ... [opt]
15188      type-id-list , type-id ... [opt]
15189
15190    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
15191    in the order that the types were presented.  */
15192
15193 static tree
15194 cp_parser_type_id_list (cp_parser* parser)
15195 {
15196   tree types = NULL_TREE;
15197
15198   while (true)
15199     {
15200       cp_token *token;
15201       tree type;
15202
15203       /* Get the next type-id.  */
15204       type = cp_parser_type_id (parser);
15205       /* Parse the optional ellipsis. */
15206       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15207         {
15208           /* Consume the `...'. */
15209           cp_lexer_consume_token (parser->lexer);
15210
15211           /* Turn the type into a pack expansion expression. */
15212           type = make_pack_expansion (type);
15213         }
15214       /* Add it to the list.  */
15215       types = add_exception_specifier (types, type, /*complain=*/1);
15216       /* Peek at the next token.  */
15217       token = cp_lexer_peek_token (parser->lexer);
15218       /* If it is not a `,', we are done.  */
15219       if (token->type != CPP_COMMA)
15220         break;
15221       /* Consume the `,'.  */
15222       cp_lexer_consume_token (parser->lexer);
15223     }
15224
15225   return nreverse (types);
15226 }
15227
15228 /* Parse a try-block.
15229
15230    try-block:
15231      try compound-statement handler-seq  */
15232
15233 static tree
15234 cp_parser_try_block (cp_parser* parser)
15235 {
15236   tree try_block;
15237
15238   cp_parser_require_keyword (parser, RID_TRY, "`try'");
15239   try_block = begin_try_block ();
15240   cp_parser_compound_statement (parser, NULL, true);
15241   finish_try_block (try_block);
15242   cp_parser_handler_seq (parser);
15243   finish_handler_sequence (try_block);
15244
15245   return try_block;
15246 }
15247
15248 /* Parse a function-try-block.
15249
15250    function-try-block:
15251      try ctor-initializer [opt] function-body handler-seq  */
15252
15253 static bool
15254 cp_parser_function_try_block (cp_parser* parser)
15255 {
15256   tree compound_stmt;
15257   tree try_block;
15258   bool ctor_initializer_p;
15259
15260   /* Look for the `try' keyword.  */
15261   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15262     return false;
15263   /* Let the rest of the front end know where we are.  */
15264   try_block = begin_function_try_block (&compound_stmt);
15265   /* Parse the function-body.  */
15266   ctor_initializer_p
15267     = cp_parser_ctor_initializer_opt_and_function_body (parser);
15268   /* We're done with the `try' part.  */
15269   finish_function_try_block (try_block);
15270   /* Parse the handlers.  */
15271   cp_parser_handler_seq (parser);
15272   /* We're done with the handlers.  */
15273   finish_function_handler_sequence (try_block, compound_stmt);
15274
15275   return ctor_initializer_p;
15276 }
15277
15278 /* Parse a handler-seq.
15279
15280    handler-seq:
15281      handler handler-seq [opt]  */
15282
15283 static void
15284 cp_parser_handler_seq (cp_parser* parser)
15285 {
15286   while (true)
15287     {
15288       cp_token *token;
15289
15290       /* Parse the handler.  */
15291       cp_parser_handler (parser);
15292       /* Peek at the next token.  */
15293       token = cp_lexer_peek_token (parser->lexer);
15294       /* If it's not `catch' then there are no more handlers.  */
15295       if (!cp_parser_is_keyword (token, RID_CATCH))
15296         break;
15297     }
15298 }
15299
15300 /* Parse a handler.
15301
15302    handler:
15303      catch ( exception-declaration ) compound-statement  */
15304
15305 static void
15306 cp_parser_handler (cp_parser* parser)
15307 {
15308   tree handler;
15309   tree declaration;
15310
15311   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15312   handler = begin_handler ();
15313   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15314   declaration = cp_parser_exception_declaration (parser);
15315   finish_handler_parms (declaration, handler);
15316   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15317   cp_parser_compound_statement (parser, NULL, false);
15318   finish_handler (handler);
15319 }
15320
15321 /* Parse an exception-declaration.
15322
15323    exception-declaration:
15324      type-specifier-seq declarator
15325      type-specifier-seq abstract-declarator
15326      type-specifier-seq
15327      ...
15328
15329    Returns a VAR_DECL for the declaration, or NULL_TREE if the
15330    ellipsis variant is used.  */
15331
15332 static tree
15333 cp_parser_exception_declaration (cp_parser* parser)
15334 {
15335   cp_decl_specifier_seq type_specifiers;
15336   cp_declarator *declarator;
15337   const char *saved_message;
15338
15339   /* If it's an ellipsis, it's easy to handle.  */
15340   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15341     {
15342       /* Consume the `...' token.  */
15343       cp_lexer_consume_token (parser->lexer);
15344       return NULL_TREE;
15345     }
15346
15347   /* Types may not be defined in exception-declarations.  */
15348   saved_message = parser->type_definition_forbidden_message;
15349   parser->type_definition_forbidden_message
15350     = "types may not be defined in exception-declarations";
15351
15352   /* Parse the type-specifier-seq.  */
15353   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15354                                 &type_specifiers);
15355   /* If it's a `)', then there is no declarator.  */
15356   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15357     declarator = NULL;
15358   else
15359     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15360                                        /*ctor_dtor_or_conv_p=*/NULL,
15361                                        /*parenthesized_p=*/NULL,
15362                                        /*member_p=*/false);
15363
15364   /* Restore the saved message.  */
15365   parser->type_definition_forbidden_message = saved_message;
15366
15367   if (!type_specifiers.any_specifiers_p)
15368     return error_mark_node;
15369
15370   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15371 }
15372
15373 /* Parse a throw-expression.
15374
15375    throw-expression:
15376      throw assignment-expression [opt]
15377
15378    Returns a THROW_EXPR representing the throw-expression.  */
15379
15380 static tree
15381 cp_parser_throw_expression (cp_parser* parser)
15382 {
15383   tree expression;
15384   cp_token* token;
15385
15386   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15387   token = cp_lexer_peek_token (parser->lexer);
15388   /* Figure out whether or not there is an assignment-expression
15389      following the "throw" keyword.  */
15390   if (token->type == CPP_COMMA
15391       || token->type == CPP_SEMICOLON
15392       || token->type == CPP_CLOSE_PAREN
15393       || token->type == CPP_CLOSE_SQUARE
15394       || token->type == CPP_CLOSE_BRACE
15395       || token->type == CPP_COLON)
15396     expression = NULL_TREE;
15397   else
15398     expression = cp_parser_assignment_expression (parser,
15399                                                   /*cast_p=*/false);
15400
15401   return build_throw (expression);
15402 }
15403
15404 /* GNU Extensions */
15405
15406 /* Parse an (optional) asm-specification.
15407
15408    asm-specification:
15409      asm ( string-literal )
15410
15411    If the asm-specification is present, returns a STRING_CST
15412    corresponding to the string-literal.  Otherwise, returns
15413    NULL_TREE.  */
15414
15415 static tree
15416 cp_parser_asm_specification_opt (cp_parser* parser)
15417 {
15418   cp_token *token;
15419   tree asm_specification;
15420
15421   /* Peek at the next token.  */
15422   token = cp_lexer_peek_token (parser->lexer);
15423   /* If the next token isn't the `asm' keyword, then there's no
15424      asm-specification.  */
15425   if (!cp_parser_is_keyword (token, RID_ASM))
15426     return NULL_TREE;
15427
15428   /* Consume the `asm' token.  */
15429   cp_lexer_consume_token (parser->lexer);
15430   /* Look for the `('.  */
15431   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15432
15433   /* Look for the string-literal.  */
15434   asm_specification = cp_parser_string_literal (parser, false, false);
15435
15436   /* Look for the `)'.  */
15437   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15438
15439   return asm_specification;
15440 }
15441
15442 /* Parse an asm-operand-list.
15443
15444    asm-operand-list:
15445      asm-operand
15446      asm-operand-list , asm-operand
15447
15448    asm-operand:
15449      string-literal ( expression )
15450      [ string-literal ] string-literal ( expression )
15451
15452    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
15453    each node is the expression.  The TREE_PURPOSE is itself a
15454    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15455    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15456    is a STRING_CST for the string literal before the parenthesis.  */
15457
15458 static tree
15459 cp_parser_asm_operand_list (cp_parser* parser)
15460 {
15461   tree asm_operands = NULL_TREE;
15462
15463   while (true)
15464     {
15465       tree string_literal;
15466       tree expression;
15467       tree name;
15468
15469       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15470         {
15471           /* Consume the `[' token.  */
15472           cp_lexer_consume_token (parser->lexer);
15473           /* Read the operand name.  */
15474           name = cp_parser_identifier (parser);
15475           if (name != error_mark_node)
15476             name = build_string (IDENTIFIER_LENGTH (name),
15477                                  IDENTIFIER_POINTER (name));
15478           /* Look for the closing `]'.  */
15479           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15480         }
15481       else
15482         name = NULL_TREE;
15483       /* Look for the string-literal.  */
15484       string_literal = cp_parser_string_literal (parser, false, false);
15485
15486       /* Look for the `('.  */
15487       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15488       /* Parse the expression.  */
15489       expression = cp_parser_expression (parser, /*cast_p=*/false);
15490       /* Look for the `)'.  */
15491       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15492
15493       /* Add this operand to the list.  */
15494       asm_operands = tree_cons (build_tree_list (name, string_literal),
15495                                 expression,
15496                                 asm_operands);
15497       /* If the next token is not a `,', there are no more
15498          operands.  */
15499       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15500         break;
15501       /* Consume the `,'.  */
15502       cp_lexer_consume_token (parser->lexer);
15503     }
15504
15505   return nreverse (asm_operands);
15506 }
15507
15508 /* Parse an asm-clobber-list.
15509
15510    asm-clobber-list:
15511      string-literal
15512      asm-clobber-list , string-literal
15513
15514    Returns a TREE_LIST, indicating the clobbers in the order that they
15515    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
15516
15517 static tree
15518 cp_parser_asm_clobber_list (cp_parser* parser)
15519 {
15520   tree clobbers = NULL_TREE;
15521
15522   while (true)
15523     {
15524       tree string_literal;
15525
15526       /* Look for the string literal.  */
15527       string_literal = cp_parser_string_literal (parser, false, false);
15528       /* Add it to the list.  */
15529       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15530       /* If the next token is not a `,', then the list is
15531          complete.  */
15532       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15533         break;
15534       /* Consume the `,' token.  */
15535       cp_lexer_consume_token (parser->lexer);
15536     }
15537
15538   return clobbers;
15539 }
15540
15541 /* Parse an (optional) series of attributes.
15542
15543    attributes:
15544      attributes attribute
15545
15546    attribute:
15547      __attribute__ (( attribute-list [opt] ))
15548
15549    The return value is as for cp_parser_attribute_list.  */
15550
15551 static tree
15552 cp_parser_attributes_opt (cp_parser* parser)
15553 {
15554   tree attributes = NULL_TREE;
15555
15556   while (true)
15557     {
15558       cp_token *token;
15559       tree attribute_list;
15560
15561       /* Peek at the next token.  */
15562       token = cp_lexer_peek_token (parser->lexer);
15563       /* If it's not `__attribute__', then we're done.  */
15564       if (token->keyword != RID_ATTRIBUTE)
15565         break;
15566
15567       /* Consume the `__attribute__' keyword.  */
15568       cp_lexer_consume_token (parser->lexer);
15569       /* Look for the two `(' tokens.  */
15570       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15571       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15572
15573       /* Peek at the next token.  */
15574       token = cp_lexer_peek_token (parser->lexer);
15575       if (token->type != CPP_CLOSE_PAREN)
15576         /* Parse the attribute-list.  */
15577         attribute_list = cp_parser_attribute_list (parser);
15578       else
15579         /* If the next token is a `)', then there is no attribute
15580            list.  */
15581         attribute_list = NULL;
15582
15583       /* Look for the two `)' tokens.  */
15584       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15585       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15586
15587       /* Add these new attributes to the list.  */
15588       attributes = chainon (attributes, attribute_list);
15589     }
15590
15591   return attributes;
15592 }
15593
15594 /* Parse an attribute-list.
15595
15596    attribute-list:
15597      attribute
15598      attribute-list , attribute
15599
15600    attribute:
15601      identifier
15602      identifier ( identifier )
15603      identifier ( identifier , expression-list )
15604      identifier ( expression-list )
15605
15606    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
15607    to an attribute.  The TREE_PURPOSE of each node is the identifier
15608    indicating which attribute is in use.  The TREE_VALUE represents
15609    the arguments, if any.  */
15610
15611 static tree
15612 cp_parser_attribute_list (cp_parser* parser)
15613 {
15614   tree attribute_list = NULL_TREE;
15615   bool save_translate_strings_p = parser->translate_strings_p;
15616
15617   parser->translate_strings_p = false;
15618   while (true)
15619     {
15620       cp_token *token;
15621       tree identifier;
15622       tree attribute;
15623
15624       /* Look for the identifier.  We also allow keywords here; for
15625          example `__attribute__ ((const))' is legal.  */
15626       token = cp_lexer_peek_token (parser->lexer);
15627       if (token->type == CPP_NAME
15628           || token->type == CPP_KEYWORD)
15629         {
15630           tree arguments = NULL_TREE;
15631
15632           /* Consume the token.  */
15633           token = cp_lexer_consume_token (parser->lexer);
15634
15635           /* Save away the identifier that indicates which attribute
15636              this is.  */
15637           identifier = token->u.value;
15638           attribute = build_tree_list (identifier, NULL_TREE);
15639
15640           /* Peek at the next token.  */
15641           token = cp_lexer_peek_token (parser->lexer);
15642           /* If it's an `(', then parse the attribute arguments.  */
15643           if (token->type == CPP_OPEN_PAREN)
15644             {
15645               arguments = cp_parser_parenthesized_expression_list
15646                           (parser, true, /*cast_p=*/false,
15647                            /*allow_expansion_p=*/false,
15648                            /*non_constant_p=*/NULL);
15649               /* Save the arguments away.  */
15650               TREE_VALUE (attribute) = arguments;
15651             }
15652
15653           if (arguments != error_mark_node)
15654             {
15655               /* Add this attribute to the list.  */
15656               TREE_CHAIN (attribute) = attribute_list;
15657               attribute_list = attribute;
15658             }
15659
15660           token = cp_lexer_peek_token (parser->lexer);
15661         }
15662       /* Now, look for more attributes.  If the next token isn't a
15663          `,', we're done.  */
15664       if (token->type != CPP_COMMA)
15665         break;
15666
15667       /* Consume the comma and keep going.  */
15668       cp_lexer_consume_token (parser->lexer);
15669     }
15670   parser->translate_strings_p = save_translate_strings_p;
15671
15672   /* We built up the list in reverse order.  */
15673   return nreverse (attribute_list);
15674 }
15675
15676 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
15677    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
15678    current value of the PEDANTIC flag, regardless of whether or not
15679    the `__extension__' keyword is present.  The caller is responsible
15680    for restoring the value of the PEDANTIC flag.  */
15681
15682 static bool
15683 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15684 {
15685   /* Save the old value of the PEDANTIC flag.  */
15686   *saved_pedantic = pedantic;
15687
15688   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15689     {
15690       /* Consume the `__extension__' token.  */
15691       cp_lexer_consume_token (parser->lexer);
15692       /* We're not being pedantic while the `__extension__' keyword is
15693          in effect.  */
15694       pedantic = 0;
15695
15696       return true;
15697     }
15698
15699   return false;
15700 }
15701
15702 /* Parse a label declaration.
15703
15704    label-declaration:
15705      __label__ label-declarator-seq ;
15706
15707    label-declarator-seq:
15708      identifier , label-declarator-seq
15709      identifier  */
15710
15711 static void
15712 cp_parser_label_declaration (cp_parser* parser)
15713 {
15714   /* Look for the `__label__' keyword.  */
15715   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15716
15717   while (true)
15718     {
15719       tree identifier;
15720
15721       /* Look for an identifier.  */
15722       identifier = cp_parser_identifier (parser);
15723       /* If we failed, stop.  */
15724       if (identifier == error_mark_node)
15725         break;
15726       /* Declare it as a label.  */
15727       finish_label_decl (identifier);
15728       /* If the next token is a `;', stop.  */
15729       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15730         break;
15731       /* Look for the `,' separating the label declarations.  */
15732       cp_parser_require (parser, CPP_COMMA, "`,'");
15733     }
15734
15735   /* Look for the final `;'.  */
15736   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15737 }
15738
15739 /* Support Functions */
15740
15741 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15742    NAME should have one of the representations used for an
15743    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15744    is returned.  If PARSER->SCOPE is a dependent type, then a
15745    SCOPE_REF is returned.
15746
15747    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15748    returned; the name was already resolved when the TEMPLATE_ID_EXPR
15749    was formed.  Abstractly, such entities should not be passed to this
15750    function, because they do not need to be looked up, but it is
15751    simpler to check for this special case here, rather than at the
15752    call-sites.
15753
15754    In cases not explicitly covered above, this function returns a
15755    DECL, OVERLOAD, or baselink representing the result of the lookup.
15756    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15757    is returned.
15758
15759    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15760    (e.g., "struct") that was used.  In that case bindings that do not
15761    refer to types are ignored.
15762
15763    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15764    ignored.
15765
15766    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15767    are ignored.
15768
15769    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15770    types.
15771
15772    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15773    TREE_LIST of candidates if name-lookup results in an ambiguity, and
15774    NULL_TREE otherwise.  */
15775
15776 static tree
15777 cp_parser_lookup_name (cp_parser *parser, tree name,
15778                        enum tag_types tag_type,
15779                        bool is_template,
15780                        bool is_namespace,
15781                        bool check_dependency,
15782                        tree *ambiguous_decls)
15783 {
15784   int flags = 0;
15785   tree decl;
15786   tree object_type = parser->context->object_type;
15787
15788   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15789     flags |= LOOKUP_COMPLAIN;
15790
15791   /* Assume that the lookup will be unambiguous.  */
15792   if (ambiguous_decls)
15793     *ambiguous_decls = NULL_TREE;
15794
15795   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15796      no longer valid.  Note that if we are parsing tentatively, and
15797      the parse fails, OBJECT_TYPE will be automatically restored.  */
15798   parser->context->object_type = NULL_TREE;
15799
15800   if (name == error_mark_node)
15801     return error_mark_node;
15802
15803   /* A template-id has already been resolved; there is no lookup to
15804      do.  */
15805   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15806     return name;
15807   if (BASELINK_P (name))
15808     {
15809       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15810                   == TEMPLATE_ID_EXPR);
15811       return name;
15812     }
15813
15814   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
15815      it should already have been checked to make sure that the name
15816      used matches the type being destroyed.  */
15817   if (TREE_CODE (name) == BIT_NOT_EXPR)
15818     {
15819       tree type;
15820
15821       /* Figure out to which type this destructor applies.  */
15822       if (parser->scope)
15823         type = parser->scope;
15824       else if (object_type)
15825         type = object_type;
15826       else
15827         type = current_class_type;
15828       /* If that's not a class type, there is no destructor.  */
15829       if (!type || !CLASS_TYPE_P (type))
15830         return error_mark_node;
15831       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15832         lazily_declare_fn (sfk_destructor, type);
15833       if (!CLASSTYPE_DESTRUCTORS (type))
15834           return error_mark_node;
15835       /* If it was a class type, return the destructor.  */
15836       return CLASSTYPE_DESTRUCTORS (type);
15837     }
15838
15839   /* By this point, the NAME should be an ordinary identifier.  If
15840      the id-expression was a qualified name, the qualifying scope is
15841      stored in PARSER->SCOPE at this point.  */
15842   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15843
15844   /* Perform the lookup.  */
15845   if (parser->scope)
15846     {
15847       bool dependent_p;
15848
15849       if (parser->scope == error_mark_node)
15850         return error_mark_node;
15851
15852       /* If the SCOPE is dependent, the lookup must be deferred until
15853          the template is instantiated -- unless we are explicitly
15854          looking up names in uninstantiated templates.  Even then, we
15855          cannot look up the name if the scope is not a class type; it
15856          might, for example, be a template type parameter.  */
15857       dependent_p = (TYPE_P (parser->scope)
15858                      && !(parser->in_declarator_p
15859                           && currently_open_class (parser->scope))
15860                      && dependent_type_p (parser->scope));
15861       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15862            && dependent_p)
15863         {
15864           if (tag_type)
15865             {
15866               tree type;
15867
15868               /* The resolution to Core Issue 180 says that `struct
15869                  A::B' should be considered a type-name, even if `A'
15870                  is dependent.  */
15871               type = make_typename_type (parser->scope, name, tag_type,
15872                                          /*complain=*/tf_error);
15873               decl = TYPE_NAME (type);
15874             }
15875           else if (is_template
15876                    && (cp_parser_next_token_ends_template_argument_p (parser)
15877                        || cp_lexer_next_token_is (parser->lexer,
15878                                                   CPP_CLOSE_PAREN)))
15879             decl = make_unbound_class_template (parser->scope,
15880                                                 name, NULL_TREE,
15881                                                 /*complain=*/tf_error);
15882           else
15883             decl = build_qualified_name (/*type=*/NULL_TREE,
15884                                          parser->scope, name,
15885                                          is_template);
15886         }
15887       else
15888         {
15889           tree pushed_scope = NULL_TREE;
15890
15891           /* If PARSER->SCOPE is a dependent type, then it must be a
15892              class type, and we must not be checking dependencies;
15893              otherwise, we would have processed this lookup above.  So
15894              that PARSER->SCOPE is not considered a dependent base by
15895              lookup_member, we must enter the scope here.  */
15896           if (dependent_p)
15897             pushed_scope = push_scope (parser->scope);
15898           /* If the PARSER->SCOPE is a template specialization, it
15899              may be instantiated during name lookup.  In that case,
15900              errors may be issued.  Even if we rollback the current
15901              tentative parse, those errors are valid.  */
15902           decl = lookup_qualified_name (parser->scope, name,
15903                                         tag_type != none_type,
15904                                         /*complain=*/true);
15905           if (pushed_scope)
15906             pop_scope (pushed_scope);
15907         }
15908       parser->qualifying_scope = parser->scope;
15909       parser->object_scope = NULL_TREE;
15910     }
15911   else if (object_type)
15912     {
15913       tree object_decl = NULL_TREE;
15914       /* Look up the name in the scope of the OBJECT_TYPE, unless the
15915          OBJECT_TYPE is not a class.  */
15916       if (CLASS_TYPE_P (object_type))
15917         /* If the OBJECT_TYPE is a template specialization, it may
15918            be instantiated during name lookup.  In that case, errors
15919            may be issued.  Even if we rollback the current tentative
15920            parse, those errors are valid.  */
15921         object_decl = lookup_member (object_type,
15922                                      name,
15923                                      /*protect=*/0,
15924                                      tag_type != none_type);
15925       /* Look it up in the enclosing context, too.  */
15926       decl = lookup_name_real (name, tag_type != none_type,
15927                                /*nonclass=*/0,
15928                                /*block_p=*/true, is_namespace, flags);
15929       parser->object_scope = object_type;
15930       parser->qualifying_scope = NULL_TREE;
15931       if (object_decl)
15932         decl = object_decl;
15933     }
15934   else
15935     {
15936       decl = lookup_name_real (name, tag_type != none_type,
15937                                /*nonclass=*/0,
15938                                /*block_p=*/true, is_namespace, flags);
15939       parser->qualifying_scope = NULL_TREE;
15940       parser->object_scope = NULL_TREE;
15941     }
15942
15943   /* If the lookup failed, let our caller know.  */
15944   if (!decl || decl == error_mark_node)
15945     return error_mark_node;
15946
15947   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
15948   if (TREE_CODE (decl) == TREE_LIST)
15949     {
15950       if (ambiguous_decls)
15951         *ambiguous_decls = decl;
15952       /* The error message we have to print is too complicated for
15953          cp_parser_error, so we incorporate its actions directly.  */
15954       if (!cp_parser_simulate_error (parser))
15955         {
15956           error ("reference to %qD is ambiguous", name);
15957           print_candidates (decl);
15958         }
15959       return error_mark_node;
15960     }
15961
15962   gcc_assert (DECL_P (decl)
15963               || TREE_CODE (decl) == OVERLOAD
15964               || TREE_CODE (decl) == SCOPE_REF
15965               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15966               || BASELINK_P (decl));
15967
15968   /* If we have resolved the name of a member declaration, check to
15969      see if the declaration is accessible.  When the name resolves to
15970      set of overloaded functions, accessibility is checked when
15971      overload resolution is done.
15972
15973      During an explicit instantiation, access is not checked at all,
15974      as per [temp.explicit].  */
15975   if (DECL_P (decl))
15976     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15977
15978   return decl;
15979 }
15980
15981 /* Like cp_parser_lookup_name, but for use in the typical case where
15982    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15983    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
15984
15985 static tree
15986 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15987 {
15988   return cp_parser_lookup_name (parser, name,
15989                                 none_type,
15990                                 /*is_template=*/false,
15991                                 /*is_namespace=*/false,
15992                                 /*check_dependency=*/true,
15993                                 /*ambiguous_decls=*/NULL);
15994 }
15995
15996 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15997    the current context, return the TYPE_DECL.  If TAG_NAME_P is
15998    true, the DECL indicates the class being defined in a class-head,
15999    or declared in an elaborated-type-specifier.
16000
16001    Otherwise, return DECL.  */
16002
16003 static tree
16004 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16005 {
16006   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16007      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16008
16009        struct A {
16010          template <typename T> struct B;
16011        };
16012
16013        template <typename T> struct A::B {};
16014
16015      Similarly, in an elaborated-type-specifier:
16016
16017        namespace N { struct X{}; }
16018
16019        struct A {
16020          template <typename T> friend struct N::X;
16021        };
16022
16023      However, if the DECL refers to a class type, and we are in
16024      the scope of the class, then the name lookup automatically
16025      finds the TYPE_DECL created by build_self_reference rather
16026      than a TEMPLATE_DECL.  For example, in:
16027
16028        template <class T> struct S {
16029          S s;
16030        };
16031
16032      there is no need to handle such case.  */
16033
16034   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16035     return DECL_TEMPLATE_RESULT (decl);
16036
16037   return decl;
16038 }
16039
16040 /* If too many, or too few, template-parameter lists apply to the
16041    declarator, issue an error message.  Returns TRUE if all went well,
16042    and FALSE otherwise.  */
16043
16044 static bool
16045 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16046                                                 cp_declarator *declarator)
16047 {
16048   unsigned num_templates;
16049
16050   /* We haven't seen any classes that involve template parameters yet.  */
16051   num_templates = 0;
16052
16053   switch (declarator->kind)
16054     {
16055     case cdk_id:
16056       if (declarator->u.id.qualifying_scope)
16057         {
16058           tree scope;
16059           tree member;
16060
16061           scope = declarator->u.id.qualifying_scope;
16062           member = declarator->u.id.unqualified_name;
16063
16064           while (scope && CLASS_TYPE_P (scope))
16065             {
16066               /* You're supposed to have one `template <...>'
16067                  for every template class, but you don't need one
16068                  for a full specialization.  For example:
16069
16070                  template <class T> struct S{};
16071                  template <> struct S<int> { void f(); };
16072                  void S<int>::f () {}
16073
16074                  is correct; there shouldn't be a `template <>' for
16075                  the definition of `S<int>::f'.  */
16076               if (!CLASSTYPE_TEMPLATE_INFO (scope))
16077                 /* If SCOPE does not have template information of any
16078                    kind, then it is not a template, nor is it nested
16079                    within a template.  */
16080                 break;
16081               if (explicit_class_specialization_p (scope))
16082                 break;
16083               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16084                 ++num_templates;
16085
16086               scope = TYPE_CONTEXT (scope);
16087             }
16088         }
16089       else if (TREE_CODE (declarator->u.id.unqualified_name)
16090                == TEMPLATE_ID_EXPR)
16091         /* If the DECLARATOR has the form `X<y>' then it uses one
16092            additional level of template parameters.  */
16093         ++num_templates;
16094
16095       return cp_parser_check_template_parameters (parser,
16096                                                   num_templates);
16097
16098     case cdk_function:
16099     case cdk_array:
16100     case cdk_pointer:
16101     case cdk_reference:
16102     case cdk_ptrmem:
16103       return (cp_parser_check_declarator_template_parameters
16104               (parser, declarator->declarator));
16105
16106     case cdk_error:
16107       return true;
16108
16109     default:
16110       gcc_unreachable ();
16111     }
16112   return false;
16113 }
16114
16115 /* NUM_TEMPLATES were used in the current declaration.  If that is
16116    invalid, return FALSE and issue an error messages.  Otherwise,
16117    return TRUE.  */
16118
16119 static bool
16120 cp_parser_check_template_parameters (cp_parser* parser,
16121                                      unsigned num_templates)
16122 {
16123   /* If there are more template classes than parameter lists, we have
16124      something like:
16125
16126        template <class T> void S<T>::R<T>::f ();  */
16127   if (parser->num_template_parameter_lists < num_templates)
16128     {
16129       error ("too few template-parameter-lists");
16130       return false;
16131     }
16132   /* If there are the same number of template classes and parameter
16133      lists, that's OK.  */
16134   if (parser->num_template_parameter_lists == num_templates)
16135     return true;
16136   /* If there are more, but only one more, then we are referring to a
16137      member template.  That's OK too.  */
16138   if (parser->num_template_parameter_lists == num_templates + 1)
16139       return true;
16140   /* Otherwise, there are too many template parameter lists.  We have
16141      something like:
16142
16143      template <class T> template <class U> void S::f();  */
16144   error ("too many template-parameter-lists");
16145   return false;
16146 }
16147
16148 /* Parse an optional `::' token indicating that the following name is
16149    from the global namespace.  If so, PARSER->SCOPE is set to the
16150    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16151    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16152    Returns the new value of PARSER->SCOPE, if the `::' token is
16153    present, and NULL_TREE otherwise.  */
16154
16155 static tree
16156 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16157 {
16158   cp_token *token;
16159
16160   /* Peek at the next token.  */
16161   token = cp_lexer_peek_token (parser->lexer);
16162   /* If we're looking at a `::' token then we're starting from the
16163      global namespace, not our current location.  */
16164   if (token->type == CPP_SCOPE)
16165     {
16166       /* Consume the `::' token.  */
16167       cp_lexer_consume_token (parser->lexer);
16168       /* Set the SCOPE so that we know where to start the lookup.  */
16169       parser->scope = global_namespace;
16170       parser->qualifying_scope = global_namespace;
16171       parser->object_scope = NULL_TREE;
16172
16173       return parser->scope;
16174     }
16175   else if (!current_scope_valid_p)
16176     {
16177       parser->scope = NULL_TREE;
16178       parser->qualifying_scope = NULL_TREE;
16179       parser->object_scope = NULL_TREE;
16180     }
16181
16182   return NULL_TREE;
16183 }
16184
16185 /* Returns TRUE if the upcoming token sequence is the start of a
16186    constructor declarator.  If FRIEND_P is true, the declarator is
16187    preceded by the `friend' specifier.  */
16188
16189 static bool
16190 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16191 {
16192   bool constructor_p;
16193   tree type_decl = NULL_TREE;
16194   bool nested_name_p;
16195   cp_token *next_token;
16196
16197   /* The common case is that this is not a constructor declarator, so
16198      try to avoid doing lots of work if at all possible.  It's not
16199      valid declare a constructor at function scope.  */
16200   if (parser->in_function_body)
16201     return false;
16202   /* And only certain tokens can begin a constructor declarator.  */
16203   next_token = cp_lexer_peek_token (parser->lexer);
16204   if (next_token->type != CPP_NAME
16205       && next_token->type != CPP_SCOPE
16206       && next_token->type != CPP_NESTED_NAME_SPECIFIER
16207       && next_token->type != CPP_TEMPLATE_ID)
16208     return false;
16209
16210   /* Parse tentatively; we are going to roll back all of the tokens
16211      consumed here.  */
16212   cp_parser_parse_tentatively (parser);
16213   /* Assume that we are looking at a constructor declarator.  */
16214   constructor_p = true;
16215
16216   /* Look for the optional `::' operator.  */
16217   cp_parser_global_scope_opt (parser,
16218                               /*current_scope_valid_p=*/false);
16219   /* Look for the nested-name-specifier.  */
16220   nested_name_p
16221     = (cp_parser_nested_name_specifier_opt (parser,
16222                                             /*typename_keyword_p=*/false,
16223                                             /*check_dependency_p=*/false,
16224                                             /*type_p=*/false,
16225                                             /*is_declaration=*/false)
16226        != NULL_TREE);
16227   /* Outside of a class-specifier, there must be a
16228      nested-name-specifier.  */
16229   if (!nested_name_p &&
16230       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16231        || friend_p))
16232     constructor_p = false;
16233   /* If we still think that this might be a constructor-declarator,
16234      look for a class-name.  */
16235   if (constructor_p)
16236     {
16237       /* If we have:
16238
16239            template <typename T> struct S { S(); };
16240            template <typename T> S<T>::S ();
16241
16242          we must recognize that the nested `S' names a class.
16243          Similarly, for:
16244
16245            template <typename T> S<T>::S<T> ();
16246
16247          we must recognize that the nested `S' names a template.  */
16248       type_decl = cp_parser_class_name (parser,
16249                                         /*typename_keyword_p=*/false,
16250                                         /*template_keyword_p=*/false,
16251                                         none_type,
16252                                         /*check_dependency_p=*/false,
16253                                         /*class_head_p=*/false,
16254                                         /*is_declaration=*/false);
16255       /* If there was no class-name, then this is not a constructor.  */
16256       constructor_p = !cp_parser_error_occurred (parser);
16257     }
16258
16259   /* If we're still considering a constructor, we have to see a `(',
16260      to begin the parameter-declaration-clause, followed by either a
16261      `)', an `...', or a decl-specifier.  We need to check for a
16262      type-specifier to avoid being fooled into thinking that:
16263
16264        S::S (f) (int);
16265
16266      is a constructor.  (It is actually a function named `f' that
16267      takes one parameter (of type `int') and returns a value of type
16268      `S::S'.  */
16269   if (constructor_p
16270       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16271     {
16272       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16273           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16274           /* A parameter declaration begins with a decl-specifier,
16275              which is either the "attribute" keyword, a storage class
16276              specifier, or (usually) a type-specifier.  */
16277           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16278         {
16279           tree type;
16280           tree pushed_scope = NULL_TREE;
16281           unsigned saved_num_template_parameter_lists;
16282
16283           /* Names appearing in the type-specifier should be looked up
16284              in the scope of the class.  */
16285           if (current_class_type)
16286             type = NULL_TREE;
16287           else
16288             {
16289               type = TREE_TYPE (type_decl);
16290               if (TREE_CODE (type) == TYPENAME_TYPE)
16291                 {
16292                   type = resolve_typename_type (type,
16293                                                 /*only_current_p=*/false);
16294                   if (TREE_CODE (type) == TYPENAME_TYPE)
16295                     {
16296                       cp_parser_abort_tentative_parse (parser);
16297                       return false;
16298                     }
16299                 }
16300               pushed_scope = push_scope (type);
16301             }
16302
16303           /* Inside the constructor parameter list, surrounding
16304              template-parameter-lists do not apply.  */
16305           saved_num_template_parameter_lists
16306             = parser->num_template_parameter_lists;
16307           parser->num_template_parameter_lists = 0;
16308
16309           /* Look for the type-specifier.  */
16310           cp_parser_type_specifier (parser,
16311                                     CP_PARSER_FLAGS_NONE,
16312                                     /*decl_specs=*/NULL,
16313                                     /*is_declarator=*/true,
16314                                     /*declares_class_or_enum=*/NULL,
16315                                     /*is_cv_qualifier=*/NULL);
16316
16317           parser->num_template_parameter_lists
16318             = saved_num_template_parameter_lists;
16319
16320           /* Leave the scope of the class.  */
16321           if (pushed_scope)
16322             pop_scope (pushed_scope);
16323
16324           constructor_p = !cp_parser_error_occurred (parser);
16325         }
16326     }
16327   else
16328     constructor_p = false;
16329   /* We did not really want to consume any tokens.  */
16330   cp_parser_abort_tentative_parse (parser);
16331
16332   return constructor_p;
16333 }
16334
16335 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16336    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
16337    they must be performed once we are in the scope of the function.
16338
16339    Returns the function defined.  */
16340
16341 static tree
16342 cp_parser_function_definition_from_specifiers_and_declarator
16343   (cp_parser* parser,
16344    cp_decl_specifier_seq *decl_specifiers,
16345    tree attributes,
16346    const cp_declarator *declarator)
16347 {
16348   tree fn;
16349   bool success_p;
16350
16351   /* Begin the function-definition.  */
16352   success_p = start_function (decl_specifiers, declarator, attributes);
16353
16354   /* The things we're about to see are not directly qualified by any
16355      template headers we've seen thus far.  */
16356   reset_specialization ();
16357
16358   /* If there were names looked up in the decl-specifier-seq that we
16359      did not check, check them now.  We must wait until we are in the
16360      scope of the function to perform the checks, since the function
16361      might be a friend.  */
16362   perform_deferred_access_checks ();
16363
16364   if (!success_p)
16365     {
16366       /* Skip the entire function.  */
16367       cp_parser_skip_to_end_of_block_or_statement (parser);
16368       fn = error_mark_node;
16369     }
16370   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16371     {
16372       /* Seen already, skip it.  An error message has already been output.  */
16373       cp_parser_skip_to_end_of_block_or_statement (parser);
16374       fn = current_function_decl;
16375       current_function_decl = NULL_TREE;
16376       /* If this is a function from a class, pop the nested class.  */
16377       if (current_class_name)
16378         pop_nested_class ();
16379     }
16380   else
16381     fn = cp_parser_function_definition_after_declarator (parser,
16382                                                          /*inline_p=*/false);
16383
16384   return fn;
16385 }
16386
16387 /* Parse the part of a function-definition that follows the
16388    declarator.  INLINE_P is TRUE iff this function is an inline
16389    function defined with a class-specifier.
16390
16391    Returns the function defined.  */
16392
16393 static tree
16394 cp_parser_function_definition_after_declarator (cp_parser* parser,
16395                                                 bool inline_p)
16396 {
16397   tree fn;
16398   bool ctor_initializer_p = false;
16399   bool saved_in_unbraced_linkage_specification_p;
16400   bool saved_in_function_body;
16401   unsigned saved_num_template_parameter_lists;
16402
16403   saved_in_function_body = parser->in_function_body;
16404   parser->in_function_body = true;
16405   /* If the next token is `return', then the code may be trying to
16406      make use of the "named return value" extension that G++ used to
16407      support.  */
16408   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16409     {
16410       /* Consume the `return' keyword.  */
16411       cp_lexer_consume_token (parser->lexer);
16412       /* Look for the identifier that indicates what value is to be
16413          returned.  */
16414       cp_parser_identifier (parser);
16415       /* Issue an error message.  */
16416       error ("named return values are no longer supported");
16417       /* Skip tokens until we reach the start of the function body.  */
16418       while (true)
16419         {
16420           cp_token *token = cp_lexer_peek_token (parser->lexer);
16421           if (token->type == CPP_OPEN_BRACE
16422               || token->type == CPP_EOF
16423               || token->type == CPP_PRAGMA_EOL)
16424             break;
16425           cp_lexer_consume_token (parser->lexer);
16426         }
16427     }
16428   /* The `extern' in `extern "C" void f () { ... }' does not apply to
16429      anything declared inside `f'.  */
16430   saved_in_unbraced_linkage_specification_p
16431     = parser->in_unbraced_linkage_specification_p;
16432   parser->in_unbraced_linkage_specification_p = false;
16433   /* Inside the function, surrounding template-parameter-lists do not
16434      apply.  */
16435   saved_num_template_parameter_lists
16436     = parser->num_template_parameter_lists;
16437   parser->num_template_parameter_lists = 0;
16438   /* If the next token is `try', then we are looking at a
16439      function-try-block.  */
16440   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16441     ctor_initializer_p = cp_parser_function_try_block (parser);
16442   /* A function-try-block includes the function-body, so we only do
16443      this next part if we're not processing a function-try-block.  */
16444   else
16445     ctor_initializer_p
16446       = cp_parser_ctor_initializer_opt_and_function_body (parser);
16447
16448   /* Finish the function.  */
16449   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16450                         (inline_p ? 2 : 0));
16451   /* Generate code for it, if necessary.  */
16452   expand_or_defer_fn (fn);
16453   /* Restore the saved values.  */
16454   parser->in_unbraced_linkage_specification_p
16455     = saved_in_unbraced_linkage_specification_p;
16456   parser->num_template_parameter_lists
16457     = saved_num_template_parameter_lists;
16458   parser->in_function_body = saved_in_function_body;
16459
16460   return fn;
16461 }
16462
16463 /* Parse a template-declaration, assuming that the `export' (and
16464    `extern') keywords, if present, has already been scanned.  MEMBER_P
16465    is as for cp_parser_template_declaration.  */
16466
16467 static void
16468 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16469 {
16470   tree decl = NULL_TREE;
16471   VEC (deferred_access_check,gc) *checks;
16472   tree parameter_list;
16473   bool friend_p = false;
16474   bool need_lang_pop;
16475
16476   /* Look for the `template' keyword.  */
16477   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16478     return;
16479
16480   /* And the `<'.  */
16481   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16482     return;
16483   if (at_class_scope_p () && current_function_decl)
16484     {
16485       /* 14.5.2.2 [temp.mem]
16486
16487          A local class shall not have member templates.  */
16488       error ("invalid declaration of member template in local class");
16489       cp_parser_skip_to_end_of_block_or_statement (parser);
16490       return;
16491     }
16492   /* [temp]
16493
16494      A template ... shall not have C linkage.  */
16495   if (current_lang_name == lang_name_c)
16496     {
16497       error ("template with C linkage");
16498       /* Give it C++ linkage to avoid confusing other parts of the
16499          front end.  */
16500       push_lang_context (lang_name_cplusplus);
16501       need_lang_pop = true;
16502     }
16503   else
16504     need_lang_pop = false;
16505
16506   /* We cannot perform access checks on the template parameter
16507      declarations until we know what is being declared, just as we
16508      cannot check the decl-specifier list.  */
16509   push_deferring_access_checks (dk_deferred);
16510
16511   /* If the next token is `>', then we have an invalid
16512      specialization.  Rather than complain about an invalid template
16513      parameter, issue an error message here.  */
16514   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16515     {
16516       cp_parser_error (parser, "invalid explicit specialization");
16517       begin_specialization ();
16518       parameter_list = NULL_TREE;
16519     }
16520   else
16521     /* Parse the template parameters.  */
16522     parameter_list = cp_parser_template_parameter_list (parser);
16523
16524   /* Get the deferred access checks from the parameter list.  These
16525      will be checked once we know what is being declared, as for a
16526      member template the checks must be performed in the scope of the
16527      class containing the member.  */
16528   checks = get_deferred_access_checks ();
16529
16530   /* Look for the `>'.  */
16531   cp_parser_skip_to_end_of_template_parameter_list (parser);
16532   /* We just processed one more parameter list.  */
16533   ++parser->num_template_parameter_lists;
16534   /* If the next token is `template', there are more template
16535      parameters.  */
16536   if (cp_lexer_next_token_is_keyword (parser->lexer,
16537                                       RID_TEMPLATE))
16538     cp_parser_template_declaration_after_export (parser, member_p);
16539   else
16540     {
16541       /* There are no access checks when parsing a template, as we do not
16542          know if a specialization will be a friend.  */
16543       push_deferring_access_checks (dk_no_check);
16544       decl = cp_parser_single_declaration (parser,
16545                                            checks,
16546                                            member_p,
16547                                            /*explicit_specialization_p=*/false,
16548                                            &friend_p);
16549       pop_deferring_access_checks ();
16550
16551       /* If this is a member template declaration, let the front
16552          end know.  */
16553       if (member_p && !friend_p && decl)
16554         {
16555           if (TREE_CODE (decl) == TYPE_DECL)
16556             cp_parser_check_access_in_redeclaration (decl);
16557
16558           decl = finish_member_template_decl (decl);
16559         }
16560       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16561         make_friend_class (current_class_type, TREE_TYPE (decl),
16562                            /*complain=*/true);
16563     }
16564   /* We are done with the current parameter list.  */
16565   --parser->num_template_parameter_lists;
16566
16567   pop_deferring_access_checks ();
16568
16569   /* Finish up.  */
16570   finish_template_decl (parameter_list);
16571
16572   /* Register member declarations.  */
16573   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16574     finish_member_declaration (decl);
16575   /* For the erroneous case of a template with C linkage, we pushed an
16576      implicit C++ linkage scope; exit that scope now.  */
16577   if (need_lang_pop)
16578     pop_lang_context ();
16579   /* If DECL is a function template, we must return to parse it later.
16580      (Even though there is no definition, there might be default
16581      arguments that need handling.)  */
16582   if (member_p && decl
16583       && (TREE_CODE (decl) == FUNCTION_DECL
16584           || DECL_FUNCTION_TEMPLATE_P (decl)))
16585     TREE_VALUE (parser->unparsed_functions_queues)
16586       = tree_cons (NULL_TREE, decl,
16587                    TREE_VALUE (parser->unparsed_functions_queues));
16588 }
16589
16590 /* Perform the deferred access checks from a template-parameter-list.
16591    CHECKS is a TREE_LIST of access checks, as returned by
16592    get_deferred_access_checks.  */
16593
16594 static void
16595 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16596 {
16597   ++processing_template_parmlist;
16598   perform_access_checks (checks);
16599   --processing_template_parmlist;
16600 }
16601
16602 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16603    `function-definition' sequence.  MEMBER_P is true, this declaration
16604    appears in a class scope.
16605
16606    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
16607    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
16608
16609 static tree
16610 cp_parser_single_declaration (cp_parser* parser,
16611                               VEC (deferred_access_check,gc)* checks,
16612                               bool member_p,
16613                               bool explicit_specialization_p,
16614                               bool* friend_p)
16615 {
16616   int declares_class_or_enum;
16617   tree decl = NULL_TREE;
16618   cp_decl_specifier_seq decl_specifiers;
16619   bool function_definition_p = false;
16620
16621   /* This function is only used when processing a template
16622      declaration.  */
16623   gcc_assert (innermost_scope_kind () == sk_template_parms
16624               || innermost_scope_kind () == sk_template_spec);
16625
16626   /* Defer access checks until we know what is being declared.  */
16627   push_deferring_access_checks (dk_deferred);
16628
16629   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16630      alternative.  */
16631   cp_parser_decl_specifier_seq (parser,
16632                                 CP_PARSER_FLAGS_OPTIONAL,
16633                                 &decl_specifiers,
16634                                 &declares_class_or_enum);
16635   if (friend_p)
16636     *friend_p = cp_parser_friend_p (&decl_specifiers);
16637
16638   /* There are no template typedefs.  */
16639   if (decl_specifiers.specs[(int) ds_typedef])
16640     {
16641       error ("template declaration of %qs", "typedef");
16642       decl = error_mark_node;
16643     }
16644
16645   /* Gather up the access checks that occurred the
16646      decl-specifier-seq.  */
16647   stop_deferring_access_checks ();
16648
16649   /* Check for the declaration of a template class.  */
16650   if (declares_class_or_enum)
16651     {
16652       if (cp_parser_declares_only_class_p (parser))
16653         {
16654           decl = shadow_tag (&decl_specifiers);
16655
16656           /* In this case:
16657
16658                struct C {
16659                  friend template <typename T> struct A<T>::B;
16660                };
16661
16662              A<T>::B will be represented by a TYPENAME_TYPE, and
16663              therefore not recognized by shadow_tag.  */
16664           if (friend_p && *friend_p
16665               && !decl
16666               && decl_specifiers.type
16667               && TYPE_P (decl_specifiers.type))
16668             decl = decl_specifiers.type;
16669
16670           if (decl && decl != error_mark_node)
16671             decl = TYPE_NAME (decl);
16672           else
16673             decl = error_mark_node;
16674
16675           /* Perform access checks for template parameters.  */
16676           cp_parser_perform_template_parameter_access_checks (checks);
16677         }
16678     }
16679   /* If it's not a template class, try for a template function.  If
16680      the next token is a `;', then this declaration does not declare
16681      anything.  But, if there were errors in the decl-specifiers, then
16682      the error might well have come from an attempted class-specifier.
16683      In that case, there's no need to warn about a missing declarator.  */
16684   if (!decl
16685       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16686           || decl_specifiers.type != error_mark_node))
16687     {
16688       decl = cp_parser_init_declarator (parser,
16689                                         &decl_specifiers,
16690                                         checks,
16691                                         /*function_definition_allowed_p=*/true,
16692                                         member_p,
16693                                         declares_class_or_enum,
16694                                         &function_definition_p);
16695
16696     /* 7.1.1-1 [dcl.stc]
16697
16698        A storage-class-specifier shall not be specified in an explicit
16699        specialization...  */
16700     if (decl
16701         && explicit_specialization_p
16702         && decl_specifiers.storage_class != sc_none)
16703       {
16704         error ("explicit template specialization cannot have a storage class");
16705         decl = error_mark_node;
16706       }
16707     }
16708
16709   pop_deferring_access_checks ();
16710
16711   /* Clear any current qualification; whatever comes next is the start
16712      of something new.  */
16713   parser->scope = NULL_TREE;
16714   parser->qualifying_scope = NULL_TREE;
16715   parser->object_scope = NULL_TREE;
16716   /* Look for a trailing `;' after the declaration.  */
16717   if (!function_definition_p
16718       && (decl == error_mark_node
16719           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16720     cp_parser_skip_to_end_of_block_or_statement (parser);
16721
16722   return decl;
16723 }
16724
16725 /* Parse a cast-expression that is not the operand of a unary "&".  */
16726
16727 static tree
16728 cp_parser_simple_cast_expression (cp_parser *parser)
16729 {
16730   return cp_parser_cast_expression (parser, /*address_p=*/false,
16731                                     /*cast_p=*/false);
16732 }
16733
16734 /* Parse a functional cast to TYPE.  Returns an expression
16735    representing the cast.  */
16736
16737 static tree
16738 cp_parser_functional_cast (cp_parser* parser, tree type)
16739 {
16740   tree expression_list;
16741   tree cast;
16742
16743   expression_list
16744     = cp_parser_parenthesized_expression_list (parser, false,
16745                                                /*cast_p=*/true,
16746                                                /*allow_expansion_p=*/true,
16747                                                /*non_constant_p=*/NULL);
16748
16749   cast = build_functional_cast (type, expression_list);
16750   /* [expr.const]/1: In an integral constant expression "only type
16751      conversions to integral or enumeration type can be used".  */
16752   if (TREE_CODE (type) == TYPE_DECL)
16753     type = TREE_TYPE (type);
16754   if (cast != error_mark_node
16755       && !cast_valid_in_integral_constant_expression_p (type)
16756       && (cp_parser_non_integral_constant_expression
16757           (parser, "a call to a constructor")))
16758     return error_mark_node;
16759   return cast;
16760 }
16761
16762 /* Save the tokens that make up the body of a member function defined
16763    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
16764    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
16765    specifiers applied to the declaration.  Returns the FUNCTION_DECL
16766    for the member function.  */
16767
16768 static tree
16769 cp_parser_save_member_function_body (cp_parser* parser,
16770                                      cp_decl_specifier_seq *decl_specifiers,
16771                                      cp_declarator *declarator,
16772                                      tree attributes)
16773 {
16774   cp_token *first;
16775   cp_token *last;
16776   tree fn;
16777
16778   /* Create the function-declaration.  */
16779   fn = start_method (decl_specifiers, declarator, attributes);
16780   /* If something went badly wrong, bail out now.  */
16781   if (fn == error_mark_node)
16782     {
16783       /* If there's a function-body, skip it.  */
16784       if (cp_parser_token_starts_function_definition_p
16785           (cp_lexer_peek_token (parser->lexer)))
16786         cp_parser_skip_to_end_of_block_or_statement (parser);
16787       return error_mark_node;
16788     }
16789
16790   /* Remember it, if there default args to post process.  */
16791   cp_parser_save_default_args (parser, fn);
16792
16793   /* Save away the tokens that make up the body of the
16794      function.  */
16795   first = parser->lexer->next_token;
16796   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16797   /* Handle function try blocks.  */
16798   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16799     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16800   last = parser->lexer->next_token;
16801
16802   /* Save away the inline definition; we will process it when the
16803      class is complete.  */
16804   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16805   DECL_PENDING_INLINE_P (fn) = 1;
16806
16807   /* We need to know that this was defined in the class, so that
16808      friend templates are handled correctly.  */
16809   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16810
16811   /* We're done with the inline definition.  */
16812   finish_method (fn);
16813
16814   /* Add FN to the queue of functions to be parsed later.  */
16815   TREE_VALUE (parser->unparsed_functions_queues)
16816     = tree_cons (NULL_TREE, fn,
16817                  TREE_VALUE (parser->unparsed_functions_queues));
16818
16819   return fn;
16820 }
16821
16822 /* Parse a template-argument-list, as well as the trailing ">" (but
16823    not the opening ">").  See cp_parser_template_argument_list for the
16824    return value.  */
16825
16826 static tree
16827 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16828 {
16829   tree arguments;
16830   tree saved_scope;
16831   tree saved_qualifying_scope;
16832   tree saved_object_scope;
16833   bool saved_greater_than_is_operator_p;
16834   bool saved_skip_evaluation;
16835
16836   /* [temp.names]
16837
16838      When parsing a template-id, the first non-nested `>' is taken as
16839      the end of the template-argument-list rather than a greater-than
16840      operator.  */
16841   saved_greater_than_is_operator_p
16842     = parser->greater_than_is_operator_p;
16843   parser->greater_than_is_operator_p = false;
16844   /* Parsing the argument list may modify SCOPE, so we save it
16845      here.  */
16846   saved_scope = parser->scope;
16847   saved_qualifying_scope = parser->qualifying_scope;
16848   saved_object_scope = parser->object_scope;
16849   /* We need to evaluate the template arguments, even though this
16850      template-id may be nested within a "sizeof".  */
16851   saved_skip_evaluation = skip_evaluation;
16852   skip_evaluation = false;
16853   /* Parse the template-argument-list itself.  */
16854   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
16855       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16856     arguments = NULL_TREE;
16857   else
16858     arguments = cp_parser_template_argument_list (parser);
16859   /* Look for the `>' that ends the template-argument-list. If we find
16860      a '>>' instead, it's probably just a typo.  */
16861   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16862     {
16863       if (cxx_dialect != cxx98)
16864         {
16865           /* In C++0x, a `>>' in a template argument list or cast
16866              expression is considered to be two separate `>'
16867              tokens. So, change the current token to a `>', but don't
16868              consume it: it will be consumed later when the outer
16869              template argument list (or cast expression) is parsed.
16870              Note that this replacement of `>' for `>>' is necessary
16871              even if we are parsing tentatively: in the tentative
16872              case, after calling
16873              cp_parser_enclosed_template_argument_list we will always
16874              throw away all of the template arguments and the first
16875              closing `>', either because the template argument list
16876              was erroneous or because we are replacing those tokens
16877              with a CPP_TEMPLATE_ID token.  The second `>' (which will
16878              not have been thrown away) is needed either to close an
16879              outer template argument list or to complete a new-style
16880              cast.  */
16881           cp_token *token = cp_lexer_peek_token (parser->lexer);
16882           token->type = CPP_GREATER;
16883         }
16884       else if (!saved_greater_than_is_operator_p)
16885         {
16886           /* If we're in a nested template argument list, the '>>' has
16887             to be a typo for '> >'. We emit the error message, but we
16888             continue parsing and we push a '>' as next token, so that
16889             the argument list will be parsed correctly.  Note that the
16890             global source location is still on the token before the
16891             '>>', so we need to say explicitly where we want it.  */
16892           cp_token *token = cp_lexer_peek_token (parser->lexer);
16893           error ("%H%<>>%> should be %<> >%> "
16894                  "within a nested template argument list",
16895                  &token->location);
16896
16897           token->type = CPP_GREATER;
16898         }
16899       else
16900         {
16901           /* If this is not a nested template argument list, the '>>'
16902             is a typo for '>'. Emit an error message and continue.
16903             Same deal about the token location, but here we can get it
16904             right by consuming the '>>' before issuing the diagnostic.  */
16905           cp_lexer_consume_token (parser->lexer);
16906           error ("spurious %<>>%>, use %<>%> to terminate "
16907                  "a template argument list");
16908         }
16909     }
16910   else
16911     cp_parser_skip_to_end_of_template_parameter_list (parser);
16912   /* The `>' token might be a greater-than operator again now.  */
16913   parser->greater_than_is_operator_p
16914     = saved_greater_than_is_operator_p;
16915   /* Restore the SAVED_SCOPE.  */
16916   parser->scope = saved_scope;
16917   parser->qualifying_scope = saved_qualifying_scope;
16918   parser->object_scope = saved_object_scope;
16919   skip_evaluation = saved_skip_evaluation;
16920
16921   return arguments;
16922 }
16923
16924 /* MEMBER_FUNCTION is a member function, or a friend.  If default
16925    arguments, or the body of the function have not yet been parsed,
16926    parse them now.  */
16927
16928 static void
16929 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16930 {
16931   /* If this member is a template, get the underlying
16932      FUNCTION_DECL.  */
16933   if (DECL_FUNCTION_TEMPLATE_P (member_function))
16934     member_function = DECL_TEMPLATE_RESULT (member_function);
16935
16936   /* There should not be any class definitions in progress at this
16937      point; the bodies of members are only parsed outside of all class
16938      definitions.  */
16939   gcc_assert (parser->num_classes_being_defined == 0);
16940   /* While we're parsing the member functions we might encounter more
16941      classes.  We want to handle them right away, but we don't want
16942      them getting mixed up with functions that are currently in the
16943      queue.  */
16944   parser->unparsed_functions_queues
16945     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16946
16947   /* Make sure that any template parameters are in scope.  */
16948   maybe_begin_member_template_processing (member_function);
16949
16950   /* If the body of the function has not yet been parsed, parse it
16951      now.  */
16952   if (DECL_PENDING_INLINE_P (member_function))
16953     {
16954       tree function_scope;
16955       cp_token_cache *tokens;
16956
16957       /* The function is no longer pending; we are processing it.  */
16958       tokens = DECL_PENDING_INLINE_INFO (member_function);
16959       DECL_PENDING_INLINE_INFO (member_function) = NULL;
16960       DECL_PENDING_INLINE_P (member_function) = 0;
16961
16962       /* If this is a local class, enter the scope of the containing
16963          function.  */
16964       function_scope = current_function_decl;
16965       if (function_scope)
16966         push_function_context_to (function_scope);
16967
16968
16969       /* Push the body of the function onto the lexer stack.  */
16970       cp_parser_push_lexer_for_tokens (parser, tokens);
16971
16972       /* Let the front end know that we going to be defining this
16973          function.  */
16974       start_preparsed_function (member_function, NULL_TREE,
16975                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
16976
16977       /* Don't do access checking if it is a templated function.  */
16978       if (processing_template_decl)
16979         push_deferring_access_checks (dk_no_check);
16980
16981       /* Now, parse the body of the function.  */
16982       cp_parser_function_definition_after_declarator (parser,
16983                                                       /*inline_p=*/true);
16984
16985       if (processing_template_decl)
16986         pop_deferring_access_checks ();
16987
16988       /* Leave the scope of the containing function.  */
16989       if (function_scope)
16990         pop_function_context_from (function_scope);
16991       cp_parser_pop_lexer (parser);
16992     }
16993
16994   /* Remove any template parameters from the symbol table.  */
16995   maybe_end_member_template_processing ();
16996
16997   /* Restore the queue.  */
16998   parser->unparsed_functions_queues
16999     = TREE_CHAIN (parser->unparsed_functions_queues);
17000 }
17001
17002 /* If DECL contains any default args, remember it on the unparsed
17003    functions queue.  */
17004
17005 static void
17006 cp_parser_save_default_args (cp_parser* parser, tree decl)
17007 {
17008   tree probe;
17009
17010   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17011        probe;
17012        probe = TREE_CHAIN (probe))
17013     if (TREE_PURPOSE (probe))
17014       {
17015         TREE_PURPOSE (parser->unparsed_functions_queues)
17016           = tree_cons (current_class_type, decl,
17017                        TREE_PURPOSE (parser->unparsed_functions_queues));
17018         break;
17019       }
17020 }
17021
17022 /* FN is a FUNCTION_DECL which may contains a parameter with an
17023    unparsed DEFAULT_ARG.  Parse the default args now.  This function
17024    assumes that the current scope is the scope in which the default
17025    argument should be processed.  */
17026
17027 static void
17028 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17029 {
17030   bool saved_local_variables_forbidden_p;
17031   tree parm;
17032
17033   /* While we're parsing the default args, we might (due to the
17034      statement expression extension) encounter more classes.  We want
17035      to handle them right away, but we don't want them getting mixed
17036      up with default args that are currently in the queue.  */
17037   parser->unparsed_functions_queues
17038     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17039
17040   /* Local variable names (and the `this' keyword) may not appear
17041      in a default argument.  */
17042   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17043   parser->local_variables_forbidden_p = true;
17044
17045   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17046        parm;
17047        parm = TREE_CHAIN (parm))
17048     {
17049       cp_token_cache *tokens;
17050       tree default_arg = TREE_PURPOSE (parm);
17051       tree parsed_arg;
17052       VEC(tree,gc) *insts;
17053       tree copy;
17054       unsigned ix;
17055
17056       if (!default_arg)
17057         continue;
17058
17059       if (TREE_CODE (default_arg) != DEFAULT_ARG)
17060         /* This can happen for a friend declaration for a function
17061            already declared with default arguments.  */
17062         continue;
17063
17064        /* Push the saved tokens for the default argument onto the parser's
17065           lexer stack.  */
17066       tokens = DEFARG_TOKENS (default_arg);
17067       cp_parser_push_lexer_for_tokens (parser, tokens);
17068
17069       /* Parse the assignment-expression.  */
17070       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17071
17072       if (!processing_template_decl)
17073         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17074
17075       TREE_PURPOSE (parm) = parsed_arg;
17076
17077       /* Update any instantiations we've already created.  */
17078       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17079            VEC_iterate (tree, insts, ix, copy); ix++)
17080         TREE_PURPOSE (copy) = parsed_arg;
17081
17082       /* If the token stream has not been completely used up, then
17083          there was extra junk after the end of the default
17084          argument.  */
17085       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17086         cp_parser_error (parser, "expected %<,%>");
17087
17088       /* Revert to the main lexer.  */
17089       cp_parser_pop_lexer (parser);
17090     }
17091
17092   /* Make sure no default arg is missing.  */
17093   check_default_args (fn);
17094
17095   /* Restore the state of local_variables_forbidden_p.  */
17096   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17097
17098   /* Restore the queue.  */
17099   parser->unparsed_functions_queues
17100     = TREE_CHAIN (parser->unparsed_functions_queues);
17101 }
17102
17103 /* Parse the operand of `sizeof' (or a similar operator).  Returns
17104    either a TYPE or an expression, depending on the form of the
17105    input.  The KEYWORD indicates which kind of expression we have
17106    encountered.  */
17107
17108 static tree
17109 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17110 {
17111   static const char *format;
17112   tree expr = NULL_TREE;
17113   const char *saved_message;
17114   bool saved_integral_constant_expression_p;
17115   bool saved_non_integral_constant_expression_p;
17116   bool pack_expansion_p = false;
17117
17118   /* Initialize FORMAT the first time we get here.  */
17119   if (!format)
17120     format = "types may not be defined in '%s' expressions";
17121
17122   /* Types cannot be defined in a `sizeof' expression.  Save away the
17123      old message.  */
17124   saved_message = parser->type_definition_forbidden_message;
17125   /* And create the new one.  */
17126   parser->type_definition_forbidden_message
17127     = XNEWVEC (const char, strlen (format)
17128                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17129                + 1 /* `\0' */);
17130   sprintf ((char *) parser->type_definition_forbidden_message,
17131            format, IDENTIFIER_POINTER (ridpointers[keyword]));
17132
17133   /* The restrictions on constant-expressions do not apply inside
17134      sizeof expressions.  */
17135   saved_integral_constant_expression_p
17136     = parser->integral_constant_expression_p;
17137   saved_non_integral_constant_expression_p
17138     = parser->non_integral_constant_expression_p;
17139   parser->integral_constant_expression_p = false;
17140
17141   /* If it's a `...', then we are computing the length of a parameter
17142      pack.  */
17143   if (keyword == RID_SIZEOF
17144       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17145     {
17146       /* Consume the `...'.  */
17147       cp_lexer_consume_token (parser->lexer);
17148       maybe_warn_variadic_templates ();
17149
17150       /* Note that this is an expansion.  */
17151       pack_expansion_p = true;
17152     }
17153
17154   /* Do not actually evaluate the expression.  */
17155   ++skip_evaluation;
17156   /* If it's a `(', then we might be looking at the type-id
17157      construction.  */
17158   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17159     {
17160       tree type;
17161       bool saved_in_type_id_in_expr_p;
17162
17163       /* We can't be sure yet whether we're looking at a type-id or an
17164          expression.  */
17165       cp_parser_parse_tentatively (parser);
17166       /* Consume the `('.  */
17167       cp_lexer_consume_token (parser->lexer);
17168       /* Parse the type-id.  */
17169       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17170       parser->in_type_id_in_expr_p = true;
17171       type = cp_parser_type_id (parser);
17172       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17173       /* Now, look for the trailing `)'.  */
17174       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17175       /* If all went well, then we're done.  */
17176       if (cp_parser_parse_definitely (parser))
17177         {
17178           cp_decl_specifier_seq decl_specs;
17179
17180           /* Build a trivial decl-specifier-seq.  */
17181           clear_decl_specs (&decl_specs);
17182           decl_specs.type = type;
17183
17184           /* Call grokdeclarator to figure out what type this is.  */
17185           expr = grokdeclarator (NULL,
17186                                  &decl_specs,
17187                                  TYPENAME,
17188                                  /*initialized=*/0,
17189                                  /*attrlist=*/NULL);
17190         }
17191     }
17192
17193   /* If the type-id production did not work out, then we must be
17194      looking at the unary-expression production.  */
17195   if (!expr)
17196     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17197                                        /*cast_p=*/false);
17198
17199   if (pack_expansion_p)
17200     /* Build a pack expansion. */
17201     expr = make_pack_expansion (expr);
17202
17203   /* Go back to evaluating expressions.  */
17204   --skip_evaluation;
17205
17206   /* Free the message we created.  */
17207   free ((char *) parser->type_definition_forbidden_message);
17208   /* And restore the old one.  */
17209   parser->type_definition_forbidden_message = saved_message;
17210   parser->integral_constant_expression_p
17211     = saved_integral_constant_expression_p;
17212   parser->non_integral_constant_expression_p
17213     = saved_non_integral_constant_expression_p;
17214
17215   return expr;
17216 }
17217
17218 /* If the current declaration has no declarator, return true.  */
17219
17220 static bool
17221 cp_parser_declares_only_class_p (cp_parser *parser)
17222 {
17223   /* If the next token is a `;' or a `,' then there is no
17224      declarator.  */
17225   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17226           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17227 }
17228
17229 /* Update the DECL_SPECS to reflect the storage class indicated by
17230    KEYWORD.  */
17231
17232 static void
17233 cp_parser_set_storage_class (cp_parser *parser,
17234                              cp_decl_specifier_seq *decl_specs,
17235                              enum rid keyword)
17236 {
17237   cp_storage_class storage_class;
17238
17239   if (parser->in_unbraced_linkage_specification_p)
17240     {
17241       error ("invalid use of %qD in linkage specification",
17242              ridpointers[keyword]);
17243       return;
17244     }
17245   else if (decl_specs->storage_class != sc_none)
17246     {
17247       decl_specs->conflicting_specifiers_p = true;
17248       return;
17249     }
17250
17251   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17252       && decl_specs->specs[(int) ds_thread])
17253     {
17254       error ("%<__thread%> before %qD", ridpointers[keyword]);
17255       decl_specs->specs[(int) ds_thread] = 0;
17256     }
17257
17258   switch (keyword)
17259     {
17260     case RID_AUTO:
17261       storage_class = sc_auto;
17262       break;
17263     case RID_REGISTER:
17264       storage_class = sc_register;
17265       break;
17266     case RID_STATIC:
17267       storage_class = sc_static;
17268       break;
17269     case RID_EXTERN:
17270       storage_class = sc_extern;
17271       break;
17272     case RID_MUTABLE:
17273       storage_class = sc_mutable;
17274       break;
17275     default:
17276       gcc_unreachable ();
17277     }
17278   decl_specs->storage_class = storage_class;
17279
17280   /* A storage class specifier cannot be applied alongside a typedef 
17281      specifier. If there is a typedef specifier present then set 
17282      conflicting_specifiers_p which will trigger an error later
17283      on in grokdeclarator. */
17284   if (decl_specs->specs[(int)ds_typedef])
17285     decl_specs->conflicting_specifiers_p = true;
17286 }
17287
17288 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
17289    is true, the type is a user-defined type; otherwise it is a
17290    built-in type specified by a keyword.  */
17291
17292 static void
17293 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17294                               tree type_spec,
17295                               bool user_defined_p)
17296 {
17297   decl_specs->any_specifiers_p = true;
17298
17299   /* If the user tries to redeclare bool or wchar_t (with, for
17300      example, in "typedef int wchar_t;") we remember that this is what
17301      happened.  In system headers, we ignore these declarations so
17302      that G++ can work with system headers that are not C++-safe.  */
17303   if (decl_specs->specs[(int) ds_typedef]
17304       && !user_defined_p
17305       && (type_spec == boolean_type_node
17306           || type_spec == wchar_type_node)
17307       && (decl_specs->type
17308           || decl_specs->specs[(int) ds_long]
17309           || decl_specs->specs[(int) ds_short]
17310           || decl_specs->specs[(int) ds_unsigned]
17311           || decl_specs->specs[(int) ds_signed]))
17312     {
17313       decl_specs->redefined_builtin_type = type_spec;
17314       if (!decl_specs->type)
17315         {
17316           decl_specs->type = type_spec;
17317           decl_specs->user_defined_type_p = false;
17318         }
17319     }
17320   else if (decl_specs->type)
17321     decl_specs->multiple_types_p = true;
17322   else
17323     {
17324       decl_specs->type = type_spec;
17325       decl_specs->user_defined_type_p = user_defined_p;
17326       decl_specs->redefined_builtin_type = NULL_TREE;
17327     }
17328 }
17329
17330 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17331    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
17332
17333 static bool
17334 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17335 {
17336   return decl_specifiers->specs[(int) ds_friend] != 0;
17337 }
17338
17339 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
17340    issue an error message indicating that TOKEN_DESC was expected.
17341
17342    Returns the token consumed, if the token had the appropriate type.
17343    Otherwise, returns NULL.  */
17344
17345 static cp_token *
17346 cp_parser_require (cp_parser* parser,
17347                    enum cpp_ttype type,
17348                    const char* token_desc)
17349 {
17350   if (cp_lexer_next_token_is (parser->lexer, type))
17351     return cp_lexer_consume_token (parser->lexer);
17352   else
17353     {
17354       /* Output the MESSAGE -- unless we're parsing tentatively.  */
17355       if (!cp_parser_simulate_error (parser))
17356         {
17357           char *message = concat ("expected ", token_desc, NULL);
17358           cp_parser_error (parser, message);
17359           free (message);
17360         }
17361       return NULL;
17362     }
17363 }
17364
17365 /* An error message is produced if the next token is not '>'.
17366    All further tokens are skipped until the desired token is
17367    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
17368
17369 static void
17370 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17371 {
17372   /* Current level of '< ... >'.  */
17373   unsigned level = 0;
17374   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
17375   unsigned nesting_depth = 0;
17376
17377   /* Are we ready, yet?  If not, issue error message.  */
17378   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17379     return;
17380
17381   /* Skip tokens until the desired token is found.  */
17382   while (true)
17383     {
17384       /* Peek at the next token.  */
17385       switch (cp_lexer_peek_token (parser->lexer)->type)
17386         {
17387         case CPP_LESS:
17388           if (!nesting_depth)
17389             ++level;
17390           break;
17391
17392         case CPP_RSHIFT:
17393           if (cxx_dialect == cxx98)
17394             /* C++0x views the `>>' operator as two `>' tokens, but
17395                C++98 does not. */
17396             break;
17397           else if (!nesting_depth && level-- == 0)
17398             {
17399               /* We've hit a `>>' where the first `>' closes the
17400                  template argument list, and the second `>' is
17401                  spurious.  Just consume the `>>' and stop; we've
17402                  already produced at least one error.  */
17403               cp_lexer_consume_token (parser->lexer);
17404               return;
17405             }
17406           /* Fall through for C++0x, so we handle the second `>' in
17407              the `>>'.  */
17408
17409         case CPP_GREATER:
17410           if (!nesting_depth && level-- == 0)
17411             {
17412               /* We've reached the token we want, consume it and stop.  */
17413               cp_lexer_consume_token (parser->lexer);
17414               return;
17415             }
17416           break;
17417
17418         case CPP_OPEN_PAREN:
17419         case CPP_OPEN_SQUARE:
17420           ++nesting_depth;
17421           break;
17422
17423         case CPP_CLOSE_PAREN:
17424         case CPP_CLOSE_SQUARE:
17425           if (nesting_depth-- == 0)
17426             return;
17427           break;
17428
17429         case CPP_EOF:
17430         case CPP_PRAGMA_EOL:
17431         case CPP_SEMICOLON:
17432         case CPP_OPEN_BRACE:
17433         case CPP_CLOSE_BRACE:
17434           /* The '>' was probably forgotten, don't look further.  */
17435           return;
17436
17437         default:
17438           break;
17439         }
17440
17441       /* Consume this token.  */
17442       cp_lexer_consume_token (parser->lexer);
17443     }
17444 }
17445
17446 /* If the next token is the indicated keyword, consume it.  Otherwise,
17447    issue an error message indicating that TOKEN_DESC was expected.
17448
17449    Returns the token consumed, if the token had the appropriate type.
17450    Otherwise, returns NULL.  */
17451
17452 static cp_token *
17453 cp_parser_require_keyword (cp_parser* parser,
17454                            enum rid keyword,
17455                            const char* token_desc)
17456 {
17457   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17458
17459   if (token && token->keyword != keyword)
17460     {
17461       dyn_string_t error_msg;
17462
17463       /* Format the error message.  */
17464       error_msg = dyn_string_new (0);
17465       dyn_string_append_cstr (error_msg, "expected ");
17466       dyn_string_append_cstr (error_msg, token_desc);
17467       cp_parser_error (parser, error_msg->s);
17468       dyn_string_delete (error_msg);
17469       return NULL;
17470     }
17471
17472   return token;
17473 }
17474
17475 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17476    function-definition.  */
17477
17478 static bool
17479 cp_parser_token_starts_function_definition_p (cp_token* token)
17480 {
17481   return (/* An ordinary function-body begins with an `{'.  */
17482           token->type == CPP_OPEN_BRACE
17483           /* A ctor-initializer begins with a `:'.  */
17484           || token->type == CPP_COLON
17485           /* A function-try-block begins with `try'.  */
17486           || token->keyword == RID_TRY
17487           /* The named return value extension begins with `return'.  */
17488           || token->keyword == RID_RETURN);
17489 }
17490
17491 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17492    definition.  */
17493
17494 static bool
17495 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17496 {
17497   cp_token *token;
17498
17499   token = cp_lexer_peek_token (parser->lexer);
17500   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17501 }
17502
17503 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17504    C++0x) ending a template-argument.  */
17505
17506 static bool
17507 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17508 {
17509   cp_token *token;
17510
17511   token = cp_lexer_peek_token (parser->lexer);
17512   return (token->type == CPP_COMMA 
17513           || token->type == CPP_GREATER
17514           || token->type == CPP_ELLIPSIS
17515           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17516 }
17517
17518 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17519    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
17520
17521 static bool
17522 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17523                                                      size_t n)
17524 {
17525   cp_token *token;
17526
17527   token = cp_lexer_peek_nth_token (parser->lexer, n);
17528   if (token->type == CPP_LESS)
17529     return true;
17530   /* Check for the sequence `<::' in the original code. It would be lexed as
17531      `[:', where `[' is a digraph, and there is no whitespace before
17532      `:'.  */
17533   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17534     {
17535       cp_token *token2;
17536       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17537       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17538         return true;
17539     }
17540   return false;
17541 }
17542
17543 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17544    or none_type otherwise.  */
17545
17546 static enum tag_types
17547 cp_parser_token_is_class_key (cp_token* token)
17548 {
17549   switch (token->keyword)
17550     {
17551     case RID_CLASS:
17552       return class_type;
17553     case RID_STRUCT:
17554       return record_type;
17555     case RID_UNION:
17556       return union_type;
17557
17558     default:
17559       return none_type;
17560     }
17561 }
17562
17563 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
17564
17565 static void
17566 cp_parser_check_class_key (enum tag_types class_key, tree type)
17567 {
17568   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17569     pedwarn ("%qs tag used in naming %q#T",
17570             class_key == union_type ? "union"
17571              : class_key == record_type ? "struct" : "class",
17572              type);
17573 }
17574
17575 /* Issue an error message if DECL is redeclared with different
17576    access than its original declaration [class.access.spec/3].
17577    This applies to nested classes and nested class templates.
17578    [class.mem/1].  */
17579
17580 static void
17581 cp_parser_check_access_in_redeclaration (tree decl)
17582 {
17583   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17584     return;
17585
17586   if ((TREE_PRIVATE (decl)
17587        != (current_access_specifier == access_private_node))
17588       || (TREE_PROTECTED (decl)
17589           != (current_access_specifier == access_protected_node)))
17590     error ("%qD redeclared with different access", decl);
17591 }
17592
17593 /* Look for the `template' keyword, as a syntactic disambiguator.
17594    Return TRUE iff it is present, in which case it will be
17595    consumed.  */
17596
17597 static bool
17598 cp_parser_optional_template_keyword (cp_parser *parser)
17599 {
17600   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17601     {
17602       /* The `template' keyword can only be used within templates;
17603          outside templates the parser can always figure out what is a
17604          template and what is not.  */
17605       if (!processing_template_decl)
17606         {
17607           error ("%<template%> (as a disambiguator) is only allowed "
17608                  "within templates");
17609           /* If this part of the token stream is rescanned, the same
17610              error message would be generated.  So, we purge the token
17611              from the stream.  */
17612           cp_lexer_purge_token (parser->lexer);
17613           return false;
17614         }
17615       else
17616         {
17617           /* Consume the `template' keyword.  */
17618           cp_lexer_consume_token (parser->lexer);
17619           return true;
17620         }
17621     }
17622
17623   return false;
17624 }
17625
17626 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
17627    set PARSER->SCOPE, and perform other related actions.  */
17628
17629 static void
17630 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17631 {
17632   int i;
17633   struct tree_check *check_value;
17634   deferred_access_check *chk;
17635   VEC (deferred_access_check,gc) *checks;
17636
17637   /* Get the stored value.  */
17638   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17639   /* Perform any access checks that were deferred.  */
17640   checks = check_value->checks;
17641   if (checks)
17642     {
17643       for (i = 0 ;
17644            VEC_iterate (deferred_access_check, checks, i, chk) ;
17645            ++i)
17646         {
17647           perform_or_defer_access_check (chk->binfo,
17648                                          chk->decl,
17649                                          chk->diag_decl);
17650         }
17651     }
17652   /* Set the scope from the stored value.  */
17653   parser->scope = check_value->value;
17654   parser->qualifying_scope = check_value->qualifying_scope;
17655   parser->object_scope = NULL_TREE;
17656 }
17657
17658 /* Consume tokens up through a non-nested END token.  */
17659
17660 static void
17661 cp_parser_cache_group (cp_parser *parser,
17662                        enum cpp_ttype end,
17663                        unsigned depth)
17664 {
17665   while (true)
17666     {
17667       cp_token *token;
17668
17669       /* Abort a parenthesized expression if we encounter a brace.  */
17670       if ((end == CPP_CLOSE_PAREN || depth == 0)
17671           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17672         return;
17673       /* If we've reached the end of the file, stop.  */
17674       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17675           || (end != CPP_PRAGMA_EOL
17676               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17677         return;
17678       /* Consume the next token.  */
17679       token = cp_lexer_consume_token (parser->lexer);
17680       /* See if it starts a new group.  */
17681       if (token->type == CPP_OPEN_BRACE)
17682         {
17683           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17684           if (depth == 0)
17685             return;
17686         }
17687       else if (token->type == CPP_OPEN_PAREN)
17688         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17689       else if (token->type == CPP_PRAGMA)
17690         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17691       else if (token->type == end)
17692         return;
17693     }
17694 }
17695
17696 /* Begin parsing tentatively.  We always save tokens while parsing
17697    tentatively so that if the tentative parsing fails we can restore the
17698    tokens.  */
17699
17700 static void
17701 cp_parser_parse_tentatively (cp_parser* parser)
17702 {
17703   /* Enter a new parsing context.  */
17704   parser->context = cp_parser_context_new (parser->context);
17705   /* Begin saving tokens.  */
17706   cp_lexer_save_tokens (parser->lexer);
17707   /* In order to avoid repetitive access control error messages,
17708      access checks are queued up until we are no longer parsing
17709      tentatively.  */
17710   push_deferring_access_checks (dk_deferred);
17711 }
17712
17713 /* Commit to the currently active tentative parse.  */
17714
17715 static void
17716 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17717 {
17718   cp_parser_context *context;
17719   cp_lexer *lexer;
17720
17721   /* Mark all of the levels as committed.  */
17722   lexer = parser->lexer;
17723   for (context = parser->context; context->next; context = context->next)
17724     {
17725       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17726         break;
17727       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17728       while (!cp_lexer_saving_tokens (lexer))
17729         lexer = lexer->next;
17730       cp_lexer_commit_tokens (lexer);
17731     }
17732 }
17733
17734 /* Abort the currently active tentative parse.  All consumed tokens
17735    will be rolled back, and no diagnostics will be issued.  */
17736
17737 static void
17738 cp_parser_abort_tentative_parse (cp_parser* parser)
17739 {
17740   cp_parser_simulate_error (parser);
17741   /* Now, pretend that we want to see if the construct was
17742      successfully parsed.  */
17743   cp_parser_parse_definitely (parser);
17744 }
17745
17746 /* Stop parsing tentatively.  If a parse error has occurred, restore the
17747    token stream.  Otherwise, commit to the tokens we have consumed.
17748    Returns true if no error occurred; false otherwise.  */
17749
17750 static bool
17751 cp_parser_parse_definitely (cp_parser* parser)
17752 {
17753   bool error_occurred;
17754   cp_parser_context *context;
17755
17756   /* Remember whether or not an error occurred, since we are about to
17757      destroy that information.  */
17758   error_occurred = cp_parser_error_occurred (parser);
17759   /* Remove the topmost context from the stack.  */
17760   context = parser->context;
17761   parser->context = context->next;
17762   /* If no parse errors occurred, commit to the tentative parse.  */
17763   if (!error_occurred)
17764     {
17765       /* Commit to the tokens read tentatively, unless that was
17766          already done.  */
17767       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17768         cp_lexer_commit_tokens (parser->lexer);
17769
17770       pop_to_parent_deferring_access_checks ();
17771     }
17772   /* Otherwise, if errors occurred, roll back our state so that things
17773      are just as they were before we began the tentative parse.  */
17774   else
17775     {
17776       cp_lexer_rollback_tokens (parser->lexer);
17777       pop_deferring_access_checks ();
17778     }
17779   /* Add the context to the front of the free list.  */
17780   context->next = cp_parser_context_free_list;
17781   cp_parser_context_free_list = context;
17782
17783   return !error_occurred;
17784 }
17785
17786 /* Returns true if we are parsing tentatively and are not committed to
17787    this tentative parse.  */
17788
17789 static bool
17790 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17791 {
17792   return (cp_parser_parsing_tentatively (parser)
17793           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17794 }
17795
17796 /* Returns nonzero iff an error has occurred during the most recent
17797    tentative parse.  */
17798
17799 static bool
17800 cp_parser_error_occurred (cp_parser* parser)
17801 {
17802   return (cp_parser_parsing_tentatively (parser)
17803           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17804 }
17805
17806 /* Returns nonzero if GNU extensions are allowed.  */
17807
17808 static bool
17809 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17810 {
17811   return parser->allow_gnu_extensions_p;
17812 }
17813 \f
17814 /* Objective-C++ Productions */
17815
17816
17817 /* Parse an Objective-C expression, which feeds into a primary-expression
17818    above.
17819
17820    objc-expression:
17821      objc-message-expression
17822      objc-string-literal
17823      objc-encode-expression
17824      objc-protocol-expression
17825      objc-selector-expression
17826
17827   Returns a tree representation of the expression.  */
17828
17829 static tree
17830 cp_parser_objc_expression (cp_parser* parser)
17831 {
17832   /* Try to figure out what kind of declaration is present.  */
17833   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17834
17835   switch (kwd->type)
17836     {
17837     case CPP_OPEN_SQUARE:
17838       return cp_parser_objc_message_expression (parser);
17839
17840     case CPP_OBJC_STRING:
17841       kwd = cp_lexer_consume_token (parser->lexer);
17842       return objc_build_string_object (kwd->u.value);
17843
17844     case CPP_KEYWORD:
17845       switch (kwd->keyword)
17846         {
17847         case RID_AT_ENCODE:
17848           return cp_parser_objc_encode_expression (parser);
17849
17850         case RID_AT_PROTOCOL:
17851           return cp_parser_objc_protocol_expression (parser);
17852
17853         case RID_AT_SELECTOR:
17854           return cp_parser_objc_selector_expression (parser);
17855
17856         default:
17857           break;
17858         }
17859     default:
17860       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17861       cp_parser_skip_to_end_of_block_or_statement (parser);
17862     }
17863
17864   return error_mark_node;
17865 }
17866
17867 /* Parse an Objective-C message expression.
17868
17869    objc-message-expression:
17870      [ objc-message-receiver objc-message-args ]
17871
17872    Returns a representation of an Objective-C message.  */
17873
17874 static tree
17875 cp_parser_objc_message_expression (cp_parser* parser)
17876 {
17877   tree receiver, messageargs;
17878
17879   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
17880   receiver = cp_parser_objc_message_receiver (parser);
17881   messageargs = cp_parser_objc_message_args (parser);
17882   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17883
17884   return objc_build_message_expr (build_tree_list (receiver, messageargs));
17885 }
17886
17887 /* Parse an objc-message-receiver.
17888
17889    objc-message-receiver:
17890      expression
17891      simple-type-specifier
17892
17893   Returns a representation of the type or expression.  */
17894
17895 static tree
17896 cp_parser_objc_message_receiver (cp_parser* parser)
17897 {
17898   tree rcv;
17899
17900   /* An Objective-C message receiver may be either (1) a type
17901      or (2) an expression.  */
17902   cp_parser_parse_tentatively (parser);
17903   rcv = cp_parser_expression (parser, false);
17904
17905   if (cp_parser_parse_definitely (parser))
17906     return rcv;
17907
17908   rcv = cp_parser_simple_type_specifier (parser,
17909                                          /*decl_specs=*/NULL,
17910                                          CP_PARSER_FLAGS_NONE);
17911
17912   return objc_get_class_reference (rcv);
17913 }
17914
17915 /* Parse the arguments and selectors comprising an Objective-C message.
17916
17917    objc-message-args:
17918      objc-selector
17919      objc-selector-args
17920      objc-selector-args , objc-comma-args
17921
17922    objc-selector-args:
17923      objc-selector [opt] : assignment-expression
17924      objc-selector-args objc-selector [opt] : assignment-expression
17925
17926    objc-comma-args:
17927      assignment-expression
17928      objc-comma-args , assignment-expression
17929
17930    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17931    selector arguments and TREE_VALUE containing a list of comma
17932    arguments.  */
17933
17934 static tree
17935 cp_parser_objc_message_args (cp_parser* parser)
17936 {
17937   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17938   bool maybe_unary_selector_p = true;
17939   cp_token *token = cp_lexer_peek_token (parser->lexer);
17940
17941   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17942     {
17943       tree selector = NULL_TREE, arg;
17944
17945       if (token->type != CPP_COLON)
17946         selector = cp_parser_objc_selector (parser);
17947
17948       /* Detect if we have a unary selector.  */
17949       if (maybe_unary_selector_p
17950           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17951         return build_tree_list (selector, NULL_TREE);
17952
17953       maybe_unary_selector_p = false;
17954       cp_parser_require (parser, CPP_COLON, "`:'");
17955       arg = cp_parser_assignment_expression (parser, false);
17956
17957       sel_args
17958         = chainon (sel_args,
17959                    build_tree_list (selector, arg));
17960
17961       token = cp_lexer_peek_token (parser->lexer);
17962     }
17963
17964   /* Handle non-selector arguments, if any. */
17965   while (token->type == CPP_COMMA)
17966     {
17967       tree arg;
17968
17969       cp_lexer_consume_token (parser->lexer);
17970       arg = cp_parser_assignment_expression (parser, false);
17971
17972       addl_args
17973         = chainon (addl_args,
17974                    build_tree_list (NULL_TREE, arg));
17975
17976       token = cp_lexer_peek_token (parser->lexer);
17977     }
17978
17979   return build_tree_list (sel_args, addl_args);
17980 }
17981
17982 /* Parse an Objective-C encode expression.
17983
17984    objc-encode-expression:
17985      @encode objc-typename
17986
17987    Returns an encoded representation of the type argument.  */
17988
17989 static tree
17990 cp_parser_objc_encode_expression (cp_parser* parser)
17991 {
17992   tree type;
17993
17994   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
17995   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17996   type = complete_type (cp_parser_type_id (parser));
17997   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17998
17999   if (!type)
18000     {
18001       error ("%<@encode%> must specify a type as an argument");
18002       return error_mark_node;
18003     }
18004
18005   return objc_build_encode_expr (type);
18006 }
18007
18008 /* Parse an Objective-C @defs expression.  */
18009
18010 static tree
18011 cp_parser_objc_defs_expression (cp_parser *parser)
18012 {
18013   tree name;
18014
18015   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
18016   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18017   name = cp_parser_identifier (parser);
18018   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18019
18020   return objc_get_class_ivars (name);
18021 }
18022
18023 /* Parse an Objective-C protocol expression.
18024
18025   objc-protocol-expression:
18026     @protocol ( identifier )
18027
18028   Returns a representation of the protocol expression.  */
18029
18030 static tree
18031 cp_parser_objc_protocol_expression (cp_parser* parser)
18032 {
18033   tree proto;
18034
18035   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18036   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18037   proto = cp_parser_identifier (parser);
18038   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18039
18040   return objc_build_protocol_expr (proto);
18041 }
18042
18043 /* Parse an Objective-C selector expression.
18044
18045    objc-selector-expression:
18046      @selector ( objc-method-signature )
18047
18048    objc-method-signature:
18049      objc-selector
18050      objc-selector-seq
18051
18052    objc-selector-seq:
18053      objc-selector :
18054      objc-selector-seq objc-selector :
18055
18056   Returns a representation of the method selector.  */
18057
18058 static tree
18059 cp_parser_objc_selector_expression (cp_parser* parser)
18060 {
18061   tree sel_seq = NULL_TREE;
18062   bool maybe_unary_selector_p = true;
18063   cp_token *token;
18064
18065   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
18066   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18067   token = cp_lexer_peek_token (parser->lexer);
18068
18069   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18070          || token->type == CPP_SCOPE)
18071     {
18072       tree selector = NULL_TREE;
18073
18074       if (token->type != CPP_COLON
18075           || token->type == CPP_SCOPE)
18076         selector = cp_parser_objc_selector (parser);
18077
18078       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18079           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18080         {
18081           /* Detect if we have a unary selector.  */
18082           if (maybe_unary_selector_p)
18083             {
18084               sel_seq = selector;
18085               goto finish_selector;
18086             }
18087           else
18088             {
18089               cp_parser_error (parser, "expected %<:%>");
18090             }
18091         }
18092       maybe_unary_selector_p = false;
18093       token = cp_lexer_consume_token (parser->lexer);
18094
18095       if (token->type == CPP_SCOPE)
18096         {
18097           sel_seq
18098             = chainon (sel_seq,
18099                        build_tree_list (selector, NULL_TREE));
18100           sel_seq
18101             = chainon (sel_seq,
18102                        build_tree_list (NULL_TREE, NULL_TREE));
18103         }
18104       else
18105         sel_seq
18106           = chainon (sel_seq,
18107                      build_tree_list (selector, NULL_TREE));
18108
18109       token = cp_lexer_peek_token (parser->lexer);
18110     }
18111
18112  finish_selector:
18113   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18114
18115   return objc_build_selector_expr (sel_seq);
18116 }
18117
18118 /* Parse a list of identifiers.
18119
18120    objc-identifier-list:
18121      identifier
18122      objc-identifier-list , identifier
18123
18124    Returns a TREE_LIST of identifier nodes.  */
18125
18126 static tree
18127 cp_parser_objc_identifier_list (cp_parser* parser)
18128 {
18129   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18130   cp_token *sep = cp_lexer_peek_token (parser->lexer);
18131
18132   while (sep->type == CPP_COMMA)
18133     {
18134       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18135       list = chainon (list,
18136                       build_tree_list (NULL_TREE,
18137                                        cp_parser_identifier (parser)));
18138       sep = cp_lexer_peek_token (parser->lexer);
18139     }
18140
18141   return list;
18142 }
18143
18144 /* Parse an Objective-C alias declaration.
18145
18146    objc-alias-declaration:
18147      @compatibility_alias identifier identifier ;
18148
18149    This function registers the alias mapping with the Objective-C front end.
18150    It returns nothing.  */
18151
18152 static void
18153 cp_parser_objc_alias_declaration (cp_parser* parser)
18154 {
18155   tree alias, orig;
18156
18157   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
18158   alias = cp_parser_identifier (parser);
18159   orig = cp_parser_identifier (parser);
18160   objc_declare_alias (alias, orig);
18161   cp_parser_consume_semicolon_at_end_of_statement (parser);
18162 }
18163
18164 /* Parse an Objective-C class forward-declaration.
18165
18166    objc-class-declaration:
18167      @class objc-identifier-list ;
18168
18169    The function registers the forward declarations with the Objective-C
18170    front end.  It returns nothing.  */
18171
18172 static void
18173 cp_parser_objc_class_declaration (cp_parser* parser)
18174 {
18175   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
18176   objc_declare_class (cp_parser_objc_identifier_list (parser));
18177   cp_parser_consume_semicolon_at_end_of_statement (parser);
18178 }
18179
18180 /* Parse a list of Objective-C protocol references.
18181
18182    objc-protocol-refs-opt:
18183      objc-protocol-refs [opt]
18184
18185    objc-protocol-refs:
18186      < objc-identifier-list >
18187
18188    Returns a TREE_LIST of identifiers, if any.  */
18189
18190 static tree
18191 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18192 {
18193   tree protorefs = NULL_TREE;
18194
18195   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18196     {
18197       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
18198       protorefs = cp_parser_objc_identifier_list (parser);
18199       cp_parser_require (parser, CPP_GREATER, "`>'");
18200     }
18201
18202   return protorefs;
18203 }
18204
18205 /* Parse a Objective-C visibility specification.  */
18206
18207 static void
18208 cp_parser_objc_visibility_spec (cp_parser* parser)
18209 {
18210   cp_token *vis = cp_lexer_peek_token (parser->lexer);
18211
18212   switch (vis->keyword)
18213     {
18214     case RID_AT_PRIVATE:
18215       objc_set_visibility (2);
18216       break;
18217     case RID_AT_PROTECTED:
18218       objc_set_visibility (0);
18219       break;
18220     case RID_AT_PUBLIC:
18221       objc_set_visibility (1);
18222       break;
18223     default:
18224       return;
18225     }
18226
18227   /* Eat '@private'/'@protected'/'@public'.  */
18228   cp_lexer_consume_token (parser->lexer);
18229 }
18230
18231 /* Parse an Objective-C method type.  */
18232
18233 static void
18234 cp_parser_objc_method_type (cp_parser* parser)
18235 {
18236   objc_set_method_type
18237    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18238     ? PLUS_EXPR
18239     : MINUS_EXPR);
18240 }
18241
18242 /* Parse an Objective-C protocol qualifier.  */
18243
18244 static tree
18245 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18246 {
18247   tree quals = NULL_TREE, node;
18248   cp_token *token = cp_lexer_peek_token (parser->lexer);
18249
18250   node = token->u.value;
18251
18252   while (node && TREE_CODE (node) == IDENTIFIER_NODE
18253          && (node == ridpointers [(int) RID_IN]
18254              || node == ridpointers [(int) RID_OUT]
18255              || node == ridpointers [(int) RID_INOUT]
18256              || node == ridpointers [(int) RID_BYCOPY]
18257              || node == ridpointers [(int) RID_BYREF]
18258              || node == ridpointers [(int) RID_ONEWAY]))
18259     {
18260       quals = tree_cons (NULL_TREE, node, quals);
18261       cp_lexer_consume_token (parser->lexer);
18262       token = cp_lexer_peek_token (parser->lexer);
18263       node = token->u.value;
18264     }
18265
18266   return quals;
18267 }
18268
18269 /* Parse an Objective-C typename.  */
18270
18271 static tree
18272 cp_parser_objc_typename (cp_parser* parser)
18273 {
18274   tree typename = NULL_TREE;
18275
18276   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18277     {
18278       tree proto_quals, cp_type = NULL_TREE;
18279
18280       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18281       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18282
18283       /* An ObjC type name may consist of just protocol qualifiers, in which
18284          case the type shall default to 'id'.  */
18285       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18286         cp_type = cp_parser_type_id (parser);
18287
18288       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18289       typename = build_tree_list (proto_quals, cp_type);
18290     }
18291
18292   return typename;
18293 }
18294
18295 /* Check to see if TYPE refers to an Objective-C selector name.  */
18296
18297 static bool
18298 cp_parser_objc_selector_p (enum cpp_ttype type)
18299 {
18300   return (type == CPP_NAME || type == CPP_KEYWORD
18301           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18302           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18303           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18304           || type == CPP_XOR || type == CPP_XOR_EQ);
18305 }
18306
18307 /* Parse an Objective-C selector.  */
18308
18309 static tree
18310 cp_parser_objc_selector (cp_parser* parser)
18311 {
18312   cp_token *token = cp_lexer_consume_token (parser->lexer);
18313
18314   if (!cp_parser_objc_selector_p (token->type))
18315     {
18316       error ("invalid Objective-C++ selector name");
18317       return error_mark_node;
18318     }
18319
18320   /* C++ operator names are allowed to appear in ObjC selectors.  */
18321   switch (token->type)
18322     {
18323     case CPP_AND_AND: return get_identifier ("and");
18324     case CPP_AND_EQ: return get_identifier ("and_eq");
18325     case CPP_AND: return get_identifier ("bitand");
18326     case CPP_OR: return get_identifier ("bitor");
18327     case CPP_COMPL: return get_identifier ("compl");
18328     case CPP_NOT: return get_identifier ("not");
18329     case CPP_NOT_EQ: return get_identifier ("not_eq");
18330     case CPP_OR_OR: return get_identifier ("or");
18331     case CPP_OR_EQ: return get_identifier ("or_eq");
18332     case CPP_XOR: return get_identifier ("xor");
18333     case CPP_XOR_EQ: return get_identifier ("xor_eq");
18334     default: return token->u.value;
18335     }
18336 }
18337
18338 /* Parse an Objective-C params list.  */
18339
18340 static tree
18341 cp_parser_objc_method_keyword_params (cp_parser* parser)
18342 {
18343   tree params = NULL_TREE;
18344   bool maybe_unary_selector_p = true;
18345   cp_token *token = cp_lexer_peek_token (parser->lexer);
18346
18347   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18348     {
18349       tree selector = NULL_TREE, typename, identifier;
18350
18351       if (token->type != CPP_COLON)
18352         selector = cp_parser_objc_selector (parser);
18353
18354       /* Detect if we have a unary selector.  */
18355       if (maybe_unary_selector_p
18356           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18357         return selector;
18358
18359       maybe_unary_selector_p = false;
18360       cp_parser_require (parser, CPP_COLON, "`:'");
18361       typename = cp_parser_objc_typename (parser);
18362       identifier = cp_parser_identifier (parser);
18363
18364       params
18365         = chainon (params,
18366                    objc_build_keyword_decl (selector,
18367                                             typename,
18368                                             identifier));
18369
18370       token = cp_lexer_peek_token (parser->lexer);
18371     }
18372
18373   return params;
18374 }
18375
18376 /* Parse the non-keyword Objective-C params.  */
18377
18378 static tree
18379 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18380 {
18381   tree params = make_node (TREE_LIST);
18382   cp_token *token = cp_lexer_peek_token (parser->lexer);
18383   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
18384
18385   while (token->type == CPP_COMMA)
18386     {
18387       cp_parameter_declarator *parmdecl;
18388       tree parm;
18389
18390       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18391       token = cp_lexer_peek_token (parser->lexer);
18392
18393       if (token->type == CPP_ELLIPSIS)
18394         {
18395           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
18396           *ellipsisp = true;
18397           break;
18398         }
18399
18400       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18401       parm = grokdeclarator (parmdecl->declarator,
18402                              &parmdecl->decl_specifiers,
18403                              PARM, /*initialized=*/0,
18404                              /*attrlist=*/NULL);
18405
18406       chainon (params, build_tree_list (NULL_TREE, parm));
18407       token = cp_lexer_peek_token (parser->lexer);
18408     }
18409
18410   return params;
18411 }
18412
18413 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
18414
18415 static void
18416 cp_parser_objc_interstitial_code (cp_parser* parser)
18417 {
18418   cp_token *token = cp_lexer_peek_token (parser->lexer);
18419
18420   /* If the next token is `extern' and the following token is a string
18421      literal, then we have a linkage specification.  */
18422   if (token->keyword == RID_EXTERN
18423       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18424     cp_parser_linkage_specification (parser);
18425   /* Handle #pragma, if any.  */
18426   else if (token->type == CPP_PRAGMA)
18427     cp_parser_pragma (parser, pragma_external);
18428   /* Allow stray semicolons.  */
18429   else if (token->type == CPP_SEMICOLON)
18430     cp_lexer_consume_token (parser->lexer);
18431   /* Finally, try to parse a block-declaration, or a function-definition.  */
18432   else
18433     cp_parser_block_declaration (parser, /*statement_p=*/false);
18434 }
18435
18436 /* Parse a method signature.  */
18437
18438 static tree
18439 cp_parser_objc_method_signature (cp_parser* parser)
18440 {
18441   tree rettype, kwdparms, optparms;
18442   bool ellipsis = false;
18443
18444   cp_parser_objc_method_type (parser);
18445   rettype = cp_parser_objc_typename (parser);
18446   kwdparms = cp_parser_objc_method_keyword_params (parser);
18447   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18448
18449   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18450 }
18451
18452 /* Pars an Objective-C method prototype list.  */
18453
18454 static void
18455 cp_parser_objc_method_prototype_list (cp_parser* parser)
18456 {
18457   cp_token *token = cp_lexer_peek_token (parser->lexer);
18458
18459   while (token->keyword != RID_AT_END)
18460     {
18461       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18462         {
18463           objc_add_method_declaration
18464            (cp_parser_objc_method_signature (parser));
18465           cp_parser_consume_semicolon_at_end_of_statement (parser);
18466         }
18467       else
18468         /* Allow for interspersed non-ObjC++ code.  */
18469         cp_parser_objc_interstitial_code (parser);
18470
18471       token = cp_lexer_peek_token (parser->lexer);
18472     }
18473
18474   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18475   objc_finish_interface ();
18476 }
18477
18478 /* Parse an Objective-C method definition list.  */
18479
18480 static void
18481 cp_parser_objc_method_definition_list (cp_parser* parser)
18482 {
18483   cp_token *token = cp_lexer_peek_token (parser->lexer);
18484
18485   while (token->keyword != RID_AT_END)
18486     {
18487       tree meth;
18488
18489       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18490         {
18491           push_deferring_access_checks (dk_deferred);
18492           objc_start_method_definition
18493            (cp_parser_objc_method_signature (parser));
18494
18495           /* For historical reasons, we accept an optional semicolon.  */
18496           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18497             cp_lexer_consume_token (parser->lexer);
18498
18499           perform_deferred_access_checks ();
18500           stop_deferring_access_checks ();
18501           meth = cp_parser_function_definition_after_declarator (parser,
18502                                                                  false);
18503           pop_deferring_access_checks ();
18504           objc_finish_method_definition (meth);
18505         }
18506       else
18507         /* Allow for interspersed non-ObjC++ code.  */
18508         cp_parser_objc_interstitial_code (parser);
18509
18510       token = cp_lexer_peek_token (parser->lexer);
18511     }
18512
18513   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18514   objc_finish_implementation ();
18515 }
18516
18517 /* Parse Objective-C ivars.  */
18518
18519 static void
18520 cp_parser_objc_class_ivars (cp_parser* parser)
18521 {
18522   cp_token *token = cp_lexer_peek_token (parser->lexer);
18523
18524   if (token->type != CPP_OPEN_BRACE)
18525     return;     /* No ivars specified.  */
18526
18527   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
18528   token = cp_lexer_peek_token (parser->lexer);
18529
18530   while (token->type != CPP_CLOSE_BRACE)
18531     {
18532       cp_decl_specifier_seq declspecs;
18533       int decl_class_or_enum_p;
18534       tree prefix_attributes;
18535
18536       cp_parser_objc_visibility_spec (parser);
18537
18538       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18539         break;
18540
18541       cp_parser_decl_specifier_seq (parser,
18542                                     CP_PARSER_FLAGS_OPTIONAL,
18543                                     &declspecs,
18544                                     &decl_class_or_enum_p);
18545       prefix_attributes = declspecs.attributes;
18546       declspecs.attributes = NULL_TREE;
18547
18548       /* Keep going until we hit the `;' at the end of the
18549          declaration.  */
18550       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18551         {
18552           tree width = NULL_TREE, attributes, first_attribute, decl;
18553           cp_declarator *declarator = NULL;
18554           int ctor_dtor_or_conv_p;
18555
18556           /* Check for a (possibly unnamed) bitfield declaration.  */
18557           token = cp_lexer_peek_token (parser->lexer);
18558           if (token->type == CPP_COLON)
18559             goto eat_colon;
18560
18561           if (token->type == CPP_NAME
18562               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18563                   == CPP_COLON))
18564             {
18565               /* Get the name of the bitfield.  */
18566               declarator = make_id_declarator (NULL_TREE,
18567                                                cp_parser_identifier (parser),
18568                                                sfk_none);
18569
18570              eat_colon:
18571               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18572               /* Get the width of the bitfield.  */
18573               width
18574                 = cp_parser_constant_expression (parser,
18575                                                  /*allow_non_constant=*/false,
18576                                                  NULL);
18577             }
18578           else
18579             {
18580               /* Parse the declarator.  */
18581               declarator
18582                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18583                                         &ctor_dtor_or_conv_p,
18584                                         /*parenthesized_p=*/NULL,
18585                                         /*member_p=*/false);
18586             }
18587
18588           /* Look for attributes that apply to the ivar.  */
18589           attributes = cp_parser_attributes_opt (parser);
18590           /* Remember which attributes are prefix attributes and
18591              which are not.  */
18592           first_attribute = attributes;
18593           /* Combine the attributes.  */
18594           attributes = chainon (prefix_attributes, attributes);
18595
18596           if (width)
18597             {
18598               /* Create the bitfield declaration.  */
18599               decl = grokbitfield (declarator, &declspecs, width);
18600               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18601             }
18602           else
18603             decl = grokfield (declarator, &declspecs,
18604                               NULL_TREE, /*init_const_expr_p=*/false,
18605                               NULL_TREE, attributes);
18606
18607           /* Add the instance variable.  */
18608           objc_add_instance_variable (decl);
18609
18610           /* Reset PREFIX_ATTRIBUTES.  */
18611           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18612             attributes = TREE_CHAIN (attributes);
18613           if (attributes)
18614             TREE_CHAIN (attributes) = NULL_TREE;
18615
18616           token = cp_lexer_peek_token (parser->lexer);
18617
18618           if (token->type == CPP_COMMA)
18619             {
18620               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18621               continue;
18622             }
18623           break;
18624         }
18625
18626       cp_parser_consume_semicolon_at_end_of_statement (parser);
18627       token = cp_lexer_peek_token (parser->lexer);
18628     }
18629
18630   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
18631   /* For historical reasons, we accept an optional semicolon.  */
18632   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18633     cp_lexer_consume_token (parser->lexer);
18634 }
18635
18636 /* Parse an Objective-C protocol declaration.  */
18637
18638 static void
18639 cp_parser_objc_protocol_declaration (cp_parser* parser)
18640 {
18641   tree proto, protorefs;
18642   cp_token *tok;
18643
18644   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18645   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18646     {
18647       error ("identifier expected after %<@protocol%>");
18648       goto finish;
18649     }
18650
18651   /* See if we have a forward declaration or a definition.  */
18652   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18653
18654   /* Try a forward declaration first.  */
18655   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18656     {
18657       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18658      finish:
18659       cp_parser_consume_semicolon_at_end_of_statement (parser);
18660     }
18661
18662   /* Ok, we got a full-fledged definition (or at least should).  */
18663   else
18664     {
18665       proto = cp_parser_identifier (parser);
18666       protorefs = cp_parser_objc_protocol_refs_opt (parser);
18667       objc_start_protocol (proto, protorefs);
18668       cp_parser_objc_method_prototype_list (parser);
18669     }
18670 }
18671
18672 /* Parse an Objective-C superclass or category.  */
18673
18674 static void
18675 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18676                                                           tree *categ)
18677 {
18678   cp_token *next = cp_lexer_peek_token (parser->lexer);
18679
18680   *super = *categ = NULL_TREE;
18681   if (next->type == CPP_COLON)
18682     {
18683       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18684       *super = cp_parser_identifier (parser);
18685     }
18686   else if (next->type == CPP_OPEN_PAREN)
18687     {
18688       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18689       *categ = cp_parser_identifier (parser);
18690       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18691     }
18692 }
18693
18694 /* Parse an Objective-C class interface.  */
18695
18696 static void
18697 cp_parser_objc_class_interface (cp_parser* parser)
18698 {
18699   tree name, super, categ, protos;
18700
18701   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
18702   name = cp_parser_identifier (parser);
18703   cp_parser_objc_superclass_or_category (parser, &super, &categ);
18704   protos = cp_parser_objc_protocol_refs_opt (parser);
18705
18706   /* We have either a class or a category on our hands.  */
18707   if (categ)
18708     objc_start_category_interface (name, categ, protos);
18709   else
18710     {
18711       objc_start_class_interface (name, super, protos);
18712       /* Handle instance variable declarations, if any.  */
18713       cp_parser_objc_class_ivars (parser);
18714       objc_continue_interface ();
18715     }
18716
18717   cp_parser_objc_method_prototype_list (parser);
18718 }
18719
18720 /* Parse an Objective-C class implementation.  */
18721
18722 static void
18723 cp_parser_objc_class_implementation (cp_parser* parser)
18724 {
18725   tree name, super, categ;
18726
18727   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
18728   name = cp_parser_identifier (parser);
18729   cp_parser_objc_superclass_or_category (parser, &super, &categ);
18730
18731   /* We have either a class or a category on our hands.  */
18732   if (categ)
18733     objc_start_category_implementation (name, categ);
18734   else
18735     {
18736       objc_start_class_implementation (name, super);
18737       /* Handle instance variable declarations, if any.  */
18738       cp_parser_objc_class_ivars (parser);
18739       objc_continue_implementation ();
18740     }
18741
18742   cp_parser_objc_method_definition_list (parser);
18743 }
18744
18745 /* Consume the @end token and finish off the implementation.  */
18746
18747 static void
18748 cp_parser_objc_end_implementation (cp_parser* parser)
18749 {
18750   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18751   objc_finish_implementation ();
18752 }
18753
18754 /* Parse an Objective-C declaration.  */
18755
18756 static void
18757 cp_parser_objc_declaration (cp_parser* parser)
18758 {
18759   /* Try to figure out what kind of declaration is present.  */
18760   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18761
18762   switch (kwd->keyword)
18763     {
18764     case RID_AT_ALIAS:
18765       cp_parser_objc_alias_declaration (parser);
18766       break;
18767     case RID_AT_CLASS:
18768       cp_parser_objc_class_declaration (parser);
18769       break;
18770     case RID_AT_PROTOCOL:
18771       cp_parser_objc_protocol_declaration (parser);
18772       break;
18773     case RID_AT_INTERFACE:
18774       cp_parser_objc_class_interface (parser);
18775       break;
18776     case RID_AT_IMPLEMENTATION:
18777       cp_parser_objc_class_implementation (parser);
18778       break;
18779     case RID_AT_END:
18780       cp_parser_objc_end_implementation (parser);
18781       break;
18782     default:
18783       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18784       cp_parser_skip_to_end_of_block_or_statement (parser);
18785     }
18786 }
18787
18788 /* Parse an Objective-C try-catch-finally statement.
18789
18790    objc-try-catch-finally-stmt:
18791      @try compound-statement objc-catch-clause-seq [opt]
18792        objc-finally-clause [opt]
18793
18794    objc-catch-clause-seq:
18795      objc-catch-clause objc-catch-clause-seq [opt]
18796
18797    objc-catch-clause:
18798      @catch ( exception-declaration ) compound-statement
18799
18800    objc-finally-clause
18801      @finally compound-statement
18802
18803    Returns NULL_TREE.  */
18804
18805 static tree
18806 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18807   location_t location;
18808   tree stmt;
18809
18810   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18811   location = cp_lexer_peek_token (parser->lexer)->location;
18812   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18813      node, lest it get absorbed into the surrounding block.  */
18814   stmt = push_stmt_list ();
18815   cp_parser_compound_statement (parser, NULL, false);
18816   objc_begin_try_stmt (location, pop_stmt_list (stmt));
18817
18818   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18819     {
18820       cp_parameter_declarator *parmdecl;
18821       tree parm;
18822
18823       cp_lexer_consume_token (parser->lexer);
18824       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18825       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18826       parm = grokdeclarator (parmdecl->declarator,
18827                              &parmdecl->decl_specifiers,
18828                              PARM, /*initialized=*/0,
18829                              /*attrlist=*/NULL);
18830       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18831       objc_begin_catch_clause (parm);
18832       cp_parser_compound_statement (parser, NULL, false);
18833       objc_finish_catch_clause ();
18834     }
18835
18836   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18837     {
18838       cp_lexer_consume_token (parser->lexer);
18839       location = cp_lexer_peek_token (parser->lexer)->location;
18840       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18841          node, lest it get absorbed into the surrounding block.  */
18842       stmt = push_stmt_list ();
18843       cp_parser_compound_statement (parser, NULL, false);
18844       objc_build_finally_clause (location, pop_stmt_list (stmt));
18845     }
18846
18847   return objc_finish_try_stmt ();
18848 }
18849
18850 /* Parse an Objective-C synchronized statement.
18851
18852    objc-synchronized-stmt:
18853      @synchronized ( expression ) compound-statement
18854
18855    Returns NULL_TREE.  */
18856
18857 static tree
18858 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18859   location_t location;
18860   tree lock, stmt;
18861
18862   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18863
18864   location = cp_lexer_peek_token (parser->lexer)->location;
18865   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18866   lock = cp_parser_expression (parser, false);
18867   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18868
18869   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18870      node, lest it get absorbed into the surrounding block.  */
18871   stmt = push_stmt_list ();
18872   cp_parser_compound_statement (parser, NULL, false);
18873
18874   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18875 }
18876
18877 /* Parse an Objective-C throw statement.
18878
18879    objc-throw-stmt:
18880      @throw assignment-expression [opt] ;
18881
18882    Returns a constructed '@throw' statement.  */
18883
18884 static tree
18885 cp_parser_objc_throw_statement (cp_parser *parser) {
18886   tree expr = NULL_TREE;
18887
18888   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18889
18890   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18891     expr = cp_parser_assignment_expression (parser, false);
18892
18893   cp_parser_consume_semicolon_at_end_of_statement (parser);
18894
18895   return objc_build_throw_stmt (expr);
18896 }
18897
18898 /* Parse an Objective-C statement.  */
18899
18900 static tree
18901 cp_parser_objc_statement (cp_parser * parser) {
18902   /* Try to figure out what kind of declaration is present.  */
18903   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18904
18905   switch (kwd->keyword)
18906     {
18907     case RID_AT_TRY:
18908       return cp_parser_objc_try_catch_finally_statement (parser);
18909     case RID_AT_SYNCHRONIZED:
18910       return cp_parser_objc_synchronized_statement (parser);
18911     case RID_AT_THROW:
18912       return cp_parser_objc_throw_statement (parser);
18913     default:
18914       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18915       cp_parser_skip_to_end_of_block_or_statement (parser);
18916     }
18917
18918   return error_mark_node;
18919 }
18920 \f
18921 /* OpenMP 2.5 parsing routines.  */
18922
18923 /* Returns name of the next clause.
18924    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18925    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
18926    returned and the token is consumed.  */
18927
18928 static pragma_omp_clause
18929 cp_parser_omp_clause_name (cp_parser *parser)
18930 {
18931   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18932
18933   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18934     result = PRAGMA_OMP_CLAUSE_IF;
18935   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18936     result = PRAGMA_OMP_CLAUSE_DEFAULT;
18937   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18938     result = PRAGMA_OMP_CLAUSE_PRIVATE;
18939   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18940     {
18941       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18942       const char *p = IDENTIFIER_POINTER (id);
18943
18944       switch (p[0])
18945         {
18946         case 'c':
18947           if (!strcmp ("copyin", p))
18948             result = PRAGMA_OMP_CLAUSE_COPYIN;
18949           else if (!strcmp ("copyprivate", p))
18950             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18951           break;
18952         case 'f':
18953           if (!strcmp ("firstprivate", p))
18954             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18955           break;
18956         case 'l':
18957           if (!strcmp ("lastprivate", p))
18958             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18959           break;
18960         case 'n':
18961           if (!strcmp ("nowait", p))
18962             result = PRAGMA_OMP_CLAUSE_NOWAIT;
18963           else if (!strcmp ("num_threads", p))
18964             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18965           break;
18966         case 'o':
18967           if (!strcmp ("ordered", p))
18968             result = PRAGMA_OMP_CLAUSE_ORDERED;
18969           break;
18970         case 'r':
18971           if (!strcmp ("reduction", p))
18972             result = PRAGMA_OMP_CLAUSE_REDUCTION;
18973           break;
18974         case 's':
18975           if (!strcmp ("schedule", p))
18976             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18977           else if (!strcmp ("shared", p))
18978             result = PRAGMA_OMP_CLAUSE_SHARED;
18979           break;
18980         }
18981     }
18982
18983   if (result != PRAGMA_OMP_CLAUSE_NONE)
18984     cp_lexer_consume_token (parser->lexer);
18985
18986   return result;
18987 }
18988
18989 /* Validate that a clause of the given type does not already exist.  */
18990
18991 static void
18992 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18993 {
18994   tree c;
18995
18996   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18997     if (OMP_CLAUSE_CODE (c) == code)
18998       {
18999         error ("too many %qs clauses", name);
19000         break;
19001       }
19002 }
19003
19004 /* OpenMP 2.5:
19005    variable-list:
19006      identifier
19007      variable-list , identifier
19008
19009    In addition, we match a closing parenthesis.  An opening parenthesis
19010    will have been consumed by the caller.
19011
19012    If KIND is nonzero, create the appropriate node and install the decl
19013    in OMP_CLAUSE_DECL and add the node to the head of the list.
19014
19015    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19016    return the list created.  */
19017
19018 static tree
19019 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19020                                 tree list)
19021 {
19022   while (1)
19023     {
19024       tree name, decl;
19025
19026       name = cp_parser_id_expression (parser, /*template_p=*/false,
19027                                       /*check_dependency_p=*/true,
19028                                       /*template_p=*/NULL,
19029                                       /*declarator_p=*/false,
19030                                       /*optional_p=*/false);
19031       if (name == error_mark_node)
19032         goto skip_comma;
19033
19034       decl = cp_parser_lookup_name_simple (parser, name);
19035       if (decl == error_mark_node)
19036         cp_parser_name_lookup_error (parser, name, decl, NULL);
19037       else if (kind != 0)
19038         {
19039           tree u = build_omp_clause (kind);
19040           OMP_CLAUSE_DECL (u) = decl;
19041           OMP_CLAUSE_CHAIN (u) = list;
19042           list = u;
19043         }
19044       else
19045         list = tree_cons (decl, NULL_TREE, list);
19046
19047     get_comma:
19048       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19049         break;
19050       cp_lexer_consume_token (parser->lexer);
19051     }
19052
19053   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19054     {
19055       int ending;
19056
19057       /* Try to resync to an unnested comma.  Copied from
19058          cp_parser_parenthesized_expression_list.  */
19059     skip_comma:
19060       ending = cp_parser_skip_to_closing_parenthesis (parser,
19061                                                       /*recovering=*/true,
19062                                                       /*or_comma=*/true,
19063                                                       /*consume_paren=*/true);
19064       if (ending < 0)
19065         goto get_comma;
19066     }
19067
19068   return list;
19069 }
19070
19071 /* Similarly, but expect leading and trailing parenthesis.  This is a very
19072    common case for omp clauses.  */
19073
19074 static tree
19075 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19076 {
19077   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19078     return cp_parser_omp_var_list_no_open (parser, kind, list);
19079   return list;
19080 }
19081
19082 /* OpenMP 2.5:
19083    default ( shared | none ) */
19084
19085 static tree
19086 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19087 {
19088   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19089   tree c;
19090
19091   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19092     return list;
19093   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19094     {
19095       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19096       const char *p = IDENTIFIER_POINTER (id);
19097
19098       switch (p[0])
19099         {
19100         case 'n':
19101           if (strcmp ("none", p) != 0)
19102             goto invalid_kind;
19103           kind = OMP_CLAUSE_DEFAULT_NONE;
19104           break;
19105
19106         case 's':
19107           if (strcmp ("shared", p) != 0)
19108             goto invalid_kind;
19109           kind = OMP_CLAUSE_DEFAULT_SHARED;
19110           break;
19111
19112         default:
19113           goto invalid_kind;
19114         }
19115
19116       cp_lexer_consume_token (parser->lexer);
19117     }
19118   else
19119     {
19120     invalid_kind:
19121       cp_parser_error (parser, "expected %<none%> or %<shared%>");
19122     }
19123
19124   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19125     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19126                                            /*or_comma=*/false,
19127                                            /*consume_paren=*/true);
19128
19129   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19130     return list;
19131
19132   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19133   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19134   OMP_CLAUSE_CHAIN (c) = list;
19135   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19136
19137   return c;
19138 }
19139
19140 /* OpenMP 2.5:
19141    if ( expression ) */
19142
19143 static tree
19144 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19145 {
19146   tree t, c;
19147
19148   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19149     return list;
19150
19151   t = cp_parser_condition (parser);
19152
19153   if (t == error_mark_node
19154       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19155     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19156                                            /*or_comma=*/false,
19157                                            /*consume_paren=*/true);
19158
19159   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19160
19161   c = build_omp_clause (OMP_CLAUSE_IF);
19162   OMP_CLAUSE_IF_EXPR (c) = t;
19163   OMP_CLAUSE_CHAIN (c) = list;
19164
19165   return c;
19166 }
19167
19168 /* OpenMP 2.5:
19169    nowait */
19170
19171 static tree
19172 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19173 {
19174   tree c;
19175
19176   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19177
19178   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19179   OMP_CLAUSE_CHAIN (c) = list;
19180   return c;
19181 }
19182
19183 /* OpenMP 2.5:
19184    num_threads ( expression ) */
19185
19186 static tree
19187 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19188 {
19189   tree t, c;
19190
19191   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19192     return list;
19193
19194   t = cp_parser_expression (parser, false);
19195
19196   if (t == error_mark_node
19197       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19198     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19199                                            /*or_comma=*/false,
19200                                            /*consume_paren=*/true);
19201
19202   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19203
19204   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19205   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19206   OMP_CLAUSE_CHAIN (c) = list;
19207
19208   return c;
19209 }
19210
19211 /* OpenMP 2.5:
19212    ordered */
19213
19214 static tree
19215 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19216 {
19217   tree c;
19218
19219   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19220
19221   c = build_omp_clause (OMP_CLAUSE_ORDERED);
19222   OMP_CLAUSE_CHAIN (c) = list;
19223   return c;
19224 }
19225
19226 /* OpenMP 2.5:
19227    reduction ( reduction-operator : variable-list )
19228
19229    reduction-operator:
19230      One of: + * - & ^ | && || */
19231
19232 static tree
19233 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19234 {
19235   enum tree_code code;
19236   tree nlist, c;
19237
19238   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19239     return list;
19240
19241   switch (cp_lexer_peek_token (parser->lexer)->type)
19242     {
19243     case CPP_PLUS:
19244       code = PLUS_EXPR;
19245       break;
19246     case CPP_MULT:
19247       code = MULT_EXPR;
19248       break;
19249     case CPP_MINUS:
19250       code = MINUS_EXPR;
19251       break;
19252     case CPP_AND:
19253       code = BIT_AND_EXPR;
19254       break;
19255     case CPP_XOR:
19256       code = BIT_XOR_EXPR;
19257       break;
19258     case CPP_OR:
19259       code = BIT_IOR_EXPR;
19260       break;
19261     case CPP_AND_AND:
19262       code = TRUTH_ANDIF_EXPR;
19263       break;
19264     case CPP_OR_OR:
19265       code = TRUTH_ORIF_EXPR;
19266       break;
19267     default:
19268       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19269     resync_fail:
19270       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19271                                              /*or_comma=*/false,
19272                                              /*consume_paren=*/true);
19273       return list;
19274     }
19275   cp_lexer_consume_token (parser->lexer);
19276
19277   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19278     goto resync_fail;
19279
19280   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19281   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19282     OMP_CLAUSE_REDUCTION_CODE (c) = code;
19283
19284   return nlist;
19285 }
19286
19287 /* OpenMP 2.5:
19288    schedule ( schedule-kind )
19289    schedule ( schedule-kind , expression )
19290
19291    schedule-kind:
19292      static | dynamic | guided | runtime  */
19293
19294 static tree
19295 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19296 {
19297   tree c, t;
19298
19299   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19300     return list;
19301
19302   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19303
19304   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19305     {
19306       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19307       const char *p = IDENTIFIER_POINTER (id);
19308
19309       switch (p[0])
19310         {
19311         case 'd':
19312           if (strcmp ("dynamic", p) != 0)
19313             goto invalid_kind;
19314           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19315           break;
19316
19317         case 'g':
19318           if (strcmp ("guided", p) != 0)
19319             goto invalid_kind;
19320           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19321           break;
19322
19323         case 'r':
19324           if (strcmp ("runtime", p) != 0)
19325             goto invalid_kind;
19326           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19327           break;
19328
19329         default:
19330           goto invalid_kind;
19331         }
19332     }
19333   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19334     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19335   else
19336     goto invalid_kind;
19337   cp_lexer_consume_token (parser->lexer);
19338
19339   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19340     {
19341       cp_lexer_consume_token (parser->lexer);
19342
19343       t = cp_parser_assignment_expression (parser, false);
19344
19345       if (t == error_mark_node)
19346         goto resync_fail;
19347       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19348         error ("schedule %<runtime%> does not take "
19349                "a %<chunk_size%> parameter");
19350       else
19351         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19352
19353       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19354         goto resync_fail;
19355     }
19356   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19357     goto resync_fail;
19358
19359   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19360   OMP_CLAUSE_CHAIN (c) = list;
19361   return c;
19362
19363  invalid_kind:
19364   cp_parser_error (parser, "invalid schedule kind");
19365  resync_fail:
19366   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19367                                          /*or_comma=*/false,
19368                                          /*consume_paren=*/true);
19369   return list;
19370 }
19371
19372 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
19373    is a bitmask in MASK.  Return the list of clauses found; the result
19374    of clause default goes in *pdefault.  */
19375
19376 static tree
19377 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19378                            const char *where, cp_token *pragma_tok)
19379 {
19380   tree clauses = NULL;
19381
19382   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19383     {
19384       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19385       const char *c_name;
19386       tree prev = clauses;
19387
19388       switch (c_kind)
19389         {
19390         case PRAGMA_OMP_CLAUSE_COPYIN:
19391           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19392           c_name = "copyin";
19393           break;
19394         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19395           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19396                                             clauses);
19397           c_name = "copyprivate";
19398           break;
19399         case PRAGMA_OMP_CLAUSE_DEFAULT:
19400           clauses = cp_parser_omp_clause_default (parser, clauses);
19401           c_name = "default";
19402           break;
19403         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19404           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19405                                             clauses);
19406           c_name = "firstprivate";
19407           break;
19408         case PRAGMA_OMP_CLAUSE_IF:
19409           clauses = cp_parser_omp_clause_if (parser, clauses);
19410           c_name = "if";
19411           break;
19412         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19413           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19414                                             clauses);
19415           c_name = "lastprivate";
19416           break;
19417         case PRAGMA_OMP_CLAUSE_NOWAIT:
19418           clauses = cp_parser_omp_clause_nowait (parser, clauses);
19419           c_name = "nowait";
19420           break;
19421         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19422           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19423           c_name = "num_threads";
19424           break;
19425         case PRAGMA_OMP_CLAUSE_ORDERED:
19426           clauses = cp_parser_omp_clause_ordered (parser, clauses);
19427           c_name = "ordered";
19428           break;
19429         case PRAGMA_OMP_CLAUSE_PRIVATE:
19430           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19431                                             clauses);
19432           c_name = "private";
19433           break;
19434         case PRAGMA_OMP_CLAUSE_REDUCTION:
19435           clauses = cp_parser_omp_clause_reduction (parser, clauses);
19436           c_name = "reduction";
19437           break;
19438         case PRAGMA_OMP_CLAUSE_SCHEDULE:
19439           clauses = cp_parser_omp_clause_schedule (parser, clauses);
19440           c_name = "schedule";
19441           break;
19442         case PRAGMA_OMP_CLAUSE_SHARED:
19443           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19444                                             clauses);
19445           c_name = "shared";
19446           break;
19447         default:
19448           cp_parser_error (parser, "expected %<#pragma omp%> clause");
19449           goto saw_error;
19450         }
19451
19452       if (((mask >> c_kind) & 1) == 0)
19453         {
19454           /* Remove the invalid clause(s) from the list to avoid
19455              confusing the rest of the compiler.  */
19456           clauses = prev;
19457           error ("%qs is not valid for %qs", c_name, where);
19458         }
19459     }
19460  saw_error:
19461   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19462   return finish_omp_clauses (clauses);
19463 }
19464
19465 /* OpenMP 2.5:
19466    structured-block:
19467      statement
19468
19469    In practice, we're also interested in adding the statement to an
19470    outer node.  So it is convenient if we work around the fact that
19471    cp_parser_statement calls add_stmt.  */
19472
19473 static unsigned
19474 cp_parser_begin_omp_structured_block (cp_parser *parser)
19475 {
19476   unsigned save = parser->in_statement;
19477
19478   /* Only move the values to IN_OMP_BLOCK if they weren't false.
19479      This preserves the "not within loop or switch" style error messages
19480      for nonsense cases like
19481         void foo() {
19482         #pragma omp single
19483           break;
19484         }
19485   */
19486   if (parser->in_statement)
19487     parser->in_statement = IN_OMP_BLOCK;
19488
19489   return save;
19490 }
19491
19492 static void
19493 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19494 {
19495   parser->in_statement = save;
19496 }
19497
19498 static tree
19499 cp_parser_omp_structured_block (cp_parser *parser)
19500 {
19501   tree stmt = begin_omp_structured_block ();
19502   unsigned int save = cp_parser_begin_omp_structured_block (parser);
19503
19504   cp_parser_statement (parser, NULL_TREE, false, NULL);
19505
19506   cp_parser_end_omp_structured_block (parser, save);
19507   return finish_omp_structured_block (stmt);
19508 }
19509
19510 /* OpenMP 2.5:
19511    # pragma omp atomic new-line
19512      expression-stmt
19513
19514    expression-stmt:
19515      x binop= expr | x++ | ++x | x-- | --x
19516    binop:
19517      +, *, -, /, &, ^, |, <<, >>
19518
19519   where x is an lvalue expression with scalar type.  */
19520
19521 static void
19522 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19523 {
19524   tree lhs, rhs;
19525   enum tree_code code;
19526
19527   cp_parser_require_pragma_eol (parser, pragma_tok);
19528
19529   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19530                                     /*cast_p=*/false);
19531   switch (TREE_CODE (lhs))
19532     {
19533     case ERROR_MARK:
19534       goto saw_error;
19535
19536     case PREINCREMENT_EXPR:
19537     case POSTINCREMENT_EXPR:
19538       lhs = TREE_OPERAND (lhs, 0);
19539       code = PLUS_EXPR;
19540       rhs = integer_one_node;
19541       break;
19542
19543     case PREDECREMENT_EXPR:
19544     case POSTDECREMENT_EXPR:
19545       lhs = TREE_OPERAND (lhs, 0);
19546       code = MINUS_EXPR;
19547       rhs = integer_one_node;
19548       break;
19549
19550     default:
19551       switch (cp_lexer_peek_token (parser->lexer)->type)
19552         {
19553         case CPP_MULT_EQ:
19554           code = MULT_EXPR;
19555           break;
19556         case CPP_DIV_EQ:
19557           code = TRUNC_DIV_EXPR;
19558           break;
19559         case CPP_PLUS_EQ:
19560           code = PLUS_EXPR;
19561           break;
19562         case CPP_MINUS_EQ:
19563           code = MINUS_EXPR;
19564           break;
19565         case CPP_LSHIFT_EQ:
19566           code = LSHIFT_EXPR;
19567           break;
19568         case CPP_RSHIFT_EQ:
19569           code = RSHIFT_EXPR;
19570           break;
19571         case CPP_AND_EQ:
19572           code = BIT_AND_EXPR;
19573           break;
19574         case CPP_OR_EQ:
19575           code = BIT_IOR_EXPR;
19576           break;
19577         case CPP_XOR_EQ:
19578           code = BIT_XOR_EXPR;
19579           break;
19580         default:
19581           cp_parser_error (parser,
19582                            "invalid operator for %<#pragma omp atomic%>");
19583           goto saw_error;
19584         }
19585       cp_lexer_consume_token (parser->lexer);
19586
19587       rhs = cp_parser_expression (parser, false);
19588       if (rhs == error_mark_node)
19589         goto saw_error;
19590       break;
19591     }
19592   finish_omp_atomic (code, lhs, rhs);
19593   cp_parser_consume_semicolon_at_end_of_statement (parser);
19594   return;
19595
19596  saw_error:
19597   cp_parser_skip_to_end_of_block_or_statement (parser);
19598 }
19599
19600
19601 /* OpenMP 2.5:
19602    # pragma omp barrier new-line  */
19603
19604 static void
19605 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19606 {
19607   cp_parser_require_pragma_eol (parser, pragma_tok);
19608   finish_omp_barrier ();
19609 }
19610
19611 /* OpenMP 2.5:
19612    # pragma omp critical [(name)] new-line
19613      structured-block  */
19614
19615 static tree
19616 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19617 {
19618   tree stmt, name = NULL;
19619
19620   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19621     {
19622       cp_lexer_consume_token (parser->lexer);
19623
19624       name = cp_parser_identifier (parser);
19625
19626       if (name == error_mark_node
19627           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19628         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19629                                                /*or_comma=*/false,
19630                                                /*consume_paren=*/true);
19631       if (name == error_mark_node)
19632         name = NULL;
19633     }
19634   cp_parser_require_pragma_eol (parser, pragma_tok);
19635
19636   stmt = cp_parser_omp_structured_block (parser);
19637   return c_finish_omp_critical (stmt, name);
19638 }
19639
19640 /* OpenMP 2.5:
19641    # pragma omp flush flush-vars[opt] new-line
19642
19643    flush-vars:
19644      ( variable-list ) */
19645
19646 static void
19647 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19648 {
19649   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19650     (void) cp_parser_omp_var_list (parser, 0, NULL);
19651   cp_parser_require_pragma_eol (parser, pragma_tok);
19652
19653   finish_omp_flush ();
19654 }
19655
19656 /* Parse the restricted form of the for statment allowed by OpenMP.  */
19657
19658 static tree
19659 cp_parser_omp_for_loop (cp_parser *parser)
19660 {
19661   tree init, cond, incr, body, decl, pre_body;
19662   location_t loc;
19663
19664   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19665     {
19666       cp_parser_error (parser, "for statement expected");
19667       return NULL;
19668     }
19669   loc = cp_lexer_consume_token (parser->lexer)->location;
19670   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19671     return NULL;
19672
19673   init = decl = NULL;
19674   pre_body = push_stmt_list ();
19675   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19676     {
19677       cp_decl_specifier_seq type_specifiers;
19678
19679       /* First, try to parse as an initialized declaration.  See
19680          cp_parser_condition, from whence the bulk of this is copied.  */
19681
19682       cp_parser_parse_tentatively (parser);
19683       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19684                                     &type_specifiers);
19685       if (!cp_parser_error_occurred (parser))
19686         {
19687           tree asm_specification, attributes;
19688           cp_declarator *declarator;
19689
19690           declarator = cp_parser_declarator (parser,
19691                                              CP_PARSER_DECLARATOR_NAMED,
19692                                              /*ctor_dtor_or_conv_p=*/NULL,
19693                                              /*parenthesized_p=*/NULL,
19694                                              /*member_p=*/false);
19695           attributes = cp_parser_attributes_opt (parser);
19696           asm_specification = cp_parser_asm_specification_opt (parser);
19697
19698           cp_parser_require (parser, CPP_EQ, "`='");
19699           if (cp_parser_parse_definitely (parser))
19700             {
19701               tree pushed_scope;
19702
19703               decl = start_decl (declarator, &type_specifiers,
19704                                  /*initialized_p=*/false, attributes,
19705                                  /*prefix_attributes=*/NULL_TREE,
19706                                  &pushed_scope);
19707
19708               init = cp_parser_assignment_expression (parser, false);
19709
19710               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19711                               asm_specification, LOOKUP_ONLYCONVERTING);
19712
19713               if (pushed_scope)
19714                 pop_scope (pushed_scope);
19715             }
19716         }
19717       else
19718         cp_parser_abort_tentative_parse (parser);
19719
19720       /* If parsing as an initialized declaration failed, try again as
19721          a simple expression.  */
19722       if (decl == NULL)
19723         init = cp_parser_expression (parser, false);
19724     }
19725   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19726   pre_body = pop_stmt_list (pre_body);
19727
19728   cond = NULL;
19729   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19730     cond = cp_parser_condition (parser);
19731   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19732
19733   incr = NULL;
19734   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19735     incr = cp_parser_expression (parser, false);
19736
19737   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19738     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19739                                            /*or_comma=*/false,
19740                                            /*consume_paren=*/true);
19741
19742   /* Note that we saved the original contents of this flag when we entered
19743      the structured block, and so we don't need to re-save it here.  */
19744   parser->in_statement = IN_OMP_FOR;
19745
19746   /* Note that the grammar doesn't call for a structured block here,
19747      though the loop as a whole is a structured block.  */
19748   body = push_stmt_list ();
19749   cp_parser_statement (parser, NULL_TREE, false, NULL);
19750   body = pop_stmt_list (body);
19751
19752   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19753 }
19754
19755 /* OpenMP 2.5:
19756    #pragma omp for for-clause[optseq] new-line
19757      for-loop  */
19758
19759 #define OMP_FOR_CLAUSE_MASK                             \
19760         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19761         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19762         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19763         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19764         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
19765         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
19766         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19767
19768 static tree
19769 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19770 {
19771   tree clauses, sb, ret;
19772   unsigned int save;
19773
19774   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19775                                        "#pragma omp for", pragma_tok);
19776
19777   sb = begin_omp_structured_block ();
19778   save = cp_parser_begin_omp_structured_block (parser);
19779
19780   ret = cp_parser_omp_for_loop (parser);
19781   if (ret)
19782     OMP_FOR_CLAUSES (ret) = clauses;
19783
19784   cp_parser_end_omp_structured_block (parser, save);
19785   add_stmt (finish_omp_structured_block (sb));
19786
19787   return ret;
19788 }
19789
19790 /* OpenMP 2.5:
19791    # pragma omp master new-line
19792      structured-block  */
19793
19794 static tree
19795 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19796 {
19797   cp_parser_require_pragma_eol (parser, pragma_tok);
19798   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19799 }
19800
19801 /* OpenMP 2.5:
19802    # pragma omp ordered new-line
19803      structured-block  */
19804
19805 static tree
19806 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19807 {
19808   cp_parser_require_pragma_eol (parser, pragma_tok);
19809   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19810 }
19811
19812 /* OpenMP 2.5:
19813
19814    section-scope:
19815      { section-sequence }
19816
19817    section-sequence:
19818      section-directive[opt] structured-block
19819      section-sequence section-directive structured-block  */
19820
19821 static tree
19822 cp_parser_omp_sections_scope (cp_parser *parser)
19823 {
19824   tree stmt, substmt;
19825   bool error_suppress = false;
19826   cp_token *tok;
19827
19828   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19829     return NULL_TREE;
19830
19831   stmt = push_stmt_list ();
19832
19833   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19834     {
19835       unsigned save;
19836
19837       substmt = begin_omp_structured_block ();
19838       save = cp_parser_begin_omp_structured_block (parser);
19839
19840       while (1)
19841         {
19842           cp_parser_statement (parser, NULL_TREE, false, NULL);
19843
19844           tok = cp_lexer_peek_token (parser->lexer);
19845           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19846             break;
19847           if (tok->type == CPP_CLOSE_BRACE)
19848             break;
19849           if (tok->type == CPP_EOF)
19850             break;
19851         }
19852
19853       cp_parser_end_omp_structured_block (parser, save);
19854       substmt = finish_omp_structured_block (substmt);
19855       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19856       add_stmt (substmt);
19857     }
19858
19859   while (1)
19860     {
19861       tok = cp_lexer_peek_token (parser->lexer);
19862       if (tok->type == CPP_CLOSE_BRACE)
19863         break;
19864       if (tok->type == CPP_EOF)
19865         break;
19866
19867       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19868         {
19869           cp_lexer_consume_token (parser->lexer);
19870           cp_parser_require_pragma_eol (parser, tok);
19871           error_suppress = false;
19872         }
19873       else if (!error_suppress)
19874         {
19875           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19876           error_suppress = true;
19877         }
19878
19879       substmt = cp_parser_omp_structured_block (parser);
19880       substmt = build1 (OMP_SECTION, void_type_node, substmt);
19881       add_stmt (substmt);
19882     }
19883   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19884
19885   substmt = pop_stmt_list (stmt);
19886
19887   stmt = make_node (OMP_SECTIONS);
19888   TREE_TYPE (stmt) = void_type_node;
19889   OMP_SECTIONS_BODY (stmt) = substmt;
19890
19891   add_stmt (stmt);
19892   return stmt;
19893 }
19894
19895 /* OpenMP 2.5:
19896    # pragma omp sections sections-clause[optseq] newline
19897      sections-scope  */
19898
19899 #define OMP_SECTIONS_CLAUSE_MASK                        \
19900         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19901         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19902         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19903         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19904         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19905
19906 static tree
19907 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19908 {
19909   tree clauses, ret;
19910
19911   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19912                                        "#pragma omp sections", pragma_tok);
19913
19914   ret = cp_parser_omp_sections_scope (parser);
19915   if (ret)
19916     OMP_SECTIONS_CLAUSES (ret) = clauses;
19917
19918   return ret;
19919 }
19920
19921 /* OpenMP 2.5:
19922    # pragma parallel parallel-clause new-line
19923    # pragma parallel for parallel-for-clause new-line
19924    # pragma parallel sections parallel-sections-clause new-line  */
19925
19926 #define OMP_PARALLEL_CLAUSE_MASK                        \
19927         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
19928         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19929         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19930         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
19931         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
19932         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
19933         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19934         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19935
19936 static tree
19937 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19938 {
19939   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19940   const char *p_name = "#pragma omp parallel";
19941   tree stmt, clauses, par_clause, ws_clause, block;
19942   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19943   unsigned int save;
19944
19945   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19946     {
19947       cp_lexer_consume_token (parser->lexer);
19948       p_kind = PRAGMA_OMP_PARALLEL_FOR;
19949       p_name = "#pragma omp parallel for";
19950       mask |= OMP_FOR_CLAUSE_MASK;
19951       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19952     }
19953   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19954     {
19955       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19956       const char *p = IDENTIFIER_POINTER (id);
19957       if (strcmp (p, "sections") == 0)
19958         {
19959           cp_lexer_consume_token (parser->lexer);
19960           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19961           p_name = "#pragma omp parallel sections";
19962           mask |= OMP_SECTIONS_CLAUSE_MASK;
19963           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19964         }
19965     }
19966
19967   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19968   block = begin_omp_parallel ();
19969   save = cp_parser_begin_omp_structured_block (parser);
19970
19971   switch (p_kind)
19972     {
19973     case PRAGMA_OMP_PARALLEL:
19974       cp_parser_already_scoped_statement (parser);
19975       par_clause = clauses;
19976       break;
19977
19978     case PRAGMA_OMP_PARALLEL_FOR:
19979       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19980       stmt = cp_parser_omp_for_loop (parser);
19981       if (stmt)
19982         OMP_FOR_CLAUSES (stmt) = ws_clause;
19983       break;
19984
19985     case PRAGMA_OMP_PARALLEL_SECTIONS:
19986       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19987       stmt = cp_parser_omp_sections_scope (parser);
19988       if (stmt)
19989         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19990       break;
19991
19992     default:
19993       gcc_unreachable ();
19994     }
19995
19996   cp_parser_end_omp_structured_block (parser, save);
19997   stmt = finish_omp_parallel (par_clause, block);
19998   if (p_kind != PRAGMA_OMP_PARALLEL)
19999     OMP_PARALLEL_COMBINED (stmt) = 1;
20000   return stmt;
20001 }
20002
20003 /* OpenMP 2.5:
20004    # pragma omp single single-clause[optseq] new-line
20005      structured-block  */
20006
20007 #define OMP_SINGLE_CLAUSE_MASK                          \
20008         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20009         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20010         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
20011         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20012
20013 static tree
20014 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20015 {
20016   tree stmt = make_node (OMP_SINGLE);
20017   TREE_TYPE (stmt) = void_type_node;
20018
20019   OMP_SINGLE_CLAUSES (stmt)
20020     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20021                                  "#pragma omp single", pragma_tok);
20022   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20023
20024   return add_stmt (stmt);
20025 }
20026
20027 /* OpenMP 2.5:
20028    # pragma omp threadprivate (variable-list) */
20029
20030 static void
20031 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20032 {
20033   tree vars;
20034
20035   vars = cp_parser_omp_var_list (parser, 0, NULL);
20036   cp_parser_require_pragma_eol (parser, pragma_tok);
20037
20038   finish_omp_threadprivate (vars);
20039 }
20040
20041 /* Main entry point to OpenMP statement pragmas.  */
20042
20043 static void
20044 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20045 {
20046   tree stmt;
20047
20048   switch (pragma_tok->pragma_kind)
20049     {
20050     case PRAGMA_OMP_ATOMIC:
20051       cp_parser_omp_atomic (parser, pragma_tok);
20052       return;
20053     case PRAGMA_OMP_CRITICAL:
20054       stmt = cp_parser_omp_critical (parser, pragma_tok);
20055       break;
20056     case PRAGMA_OMP_FOR:
20057       stmt = cp_parser_omp_for (parser, pragma_tok);
20058       break;
20059     case PRAGMA_OMP_MASTER:
20060       stmt = cp_parser_omp_master (parser, pragma_tok);
20061       break;
20062     case PRAGMA_OMP_ORDERED:
20063       stmt = cp_parser_omp_ordered (parser, pragma_tok);
20064       break;
20065     case PRAGMA_OMP_PARALLEL:
20066       stmt = cp_parser_omp_parallel (parser, pragma_tok);
20067       break;
20068     case PRAGMA_OMP_SECTIONS:
20069       stmt = cp_parser_omp_sections (parser, pragma_tok);
20070       break;
20071     case PRAGMA_OMP_SINGLE:
20072       stmt = cp_parser_omp_single (parser, pragma_tok);
20073       break;
20074     default:
20075       gcc_unreachable ();
20076     }
20077
20078   if (stmt)
20079     SET_EXPR_LOCATION (stmt, pragma_tok->location);
20080 }
20081 \f
20082 /* The parser.  */
20083
20084 static GTY (()) cp_parser *the_parser;
20085
20086 \f
20087 /* Special handling for the first token or line in the file.  The first
20088    thing in the file might be #pragma GCC pch_preprocess, which loads a
20089    PCH file, which is a GC collection point.  So we need to handle this
20090    first pragma without benefit of an existing lexer structure.
20091
20092    Always returns one token to the caller in *FIRST_TOKEN.  This is
20093    either the true first token of the file, or the first token after
20094    the initial pragma.  */
20095
20096 static void
20097 cp_parser_initial_pragma (cp_token *first_token)
20098 {
20099   tree name = NULL;
20100
20101   cp_lexer_get_preprocessor_token (NULL, first_token);
20102   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20103     return;
20104
20105   cp_lexer_get_preprocessor_token (NULL, first_token);
20106   if (first_token->type == CPP_STRING)
20107     {
20108       name = first_token->u.value;
20109
20110       cp_lexer_get_preprocessor_token (NULL, first_token);
20111       if (first_token->type != CPP_PRAGMA_EOL)
20112         error ("junk at end of %<#pragma GCC pch_preprocess%>");
20113     }
20114   else
20115     error ("expected string literal");
20116
20117   /* Skip to the end of the pragma.  */
20118   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20119     cp_lexer_get_preprocessor_token (NULL, first_token);
20120
20121   /* Now actually load the PCH file.  */
20122   if (name)
20123     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20124
20125   /* Read one more token to return to our caller.  We have to do this
20126      after reading the PCH file in, since its pointers have to be
20127      live.  */
20128   cp_lexer_get_preprocessor_token (NULL, first_token);
20129 }
20130
20131 /* Normal parsing of a pragma token.  Here we can (and must) use the
20132    regular lexer.  */
20133
20134 static bool
20135 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20136 {
20137   cp_token *pragma_tok;
20138   unsigned int id;
20139
20140   pragma_tok = cp_lexer_consume_token (parser->lexer);
20141   gcc_assert (pragma_tok->type == CPP_PRAGMA);
20142   parser->lexer->in_pragma = true;
20143
20144   id = pragma_tok->pragma_kind;
20145   switch (id)
20146     {
20147     case PRAGMA_GCC_PCH_PREPROCESS:
20148       error ("%<#pragma GCC pch_preprocess%> must be first");
20149       break;
20150
20151     case PRAGMA_OMP_BARRIER:
20152       switch (context)
20153         {
20154         case pragma_compound:
20155           cp_parser_omp_barrier (parser, pragma_tok);
20156           return false;
20157         case pragma_stmt:
20158           error ("%<#pragma omp barrier%> may only be "
20159                  "used in compound statements");
20160           break;
20161         default:
20162           goto bad_stmt;
20163         }
20164       break;
20165
20166     case PRAGMA_OMP_FLUSH:
20167       switch (context)
20168         {
20169         case pragma_compound:
20170           cp_parser_omp_flush (parser, pragma_tok);
20171           return false;
20172         case pragma_stmt:
20173           error ("%<#pragma omp flush%> may only be "
20174                  "used in compound statements");
20175           break;
20176         default:
20177           goto bad_stmt;
20178         }
20179       break;
20180
20181     case PRAGMA_OMP_THREADPRIVATE:
20182       cp_parser_omp_threadprivate (parser, pragma_tok);
20183       return false;
20184
20185     case PRAGMA_OMP_ATOMIC:
20186     case PRAGMA_OMP_CRITICAL:
20187     case PRAGMA_OMP_FOR:
20188     case PRAGMA_OMP_MASTER:
20189     case PRAGMA_OMP_ORDERED:
20190     case PRAGMA_OMP_PARALLEL:
20191     case PRAGMA_OMP_SECTIONS:
20192     case PRAGMA_OMP_SINGLE:
20193       if (context == pragma_external)
20194         goto bad_stmt;
20195       cp_parser_omp_construct (parser, pragma_tok);
20196       return true;
20197
20198     case PRAGMA_OMP_SECTION:
20199       error ("%<#pragma omp section%> may only be used in "
20200              "%<#pragma omp sections%> construct");
20201       break;
20202
20203     default:
20204       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20205       c_invoke_pragma_handler (id);
20206       break;
20207
20208     bad_stmt:
20209       cp_parser_error (parser, "expected declaration specifiers");
20210       break;
20211     }
20212
20213   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20214   return false;
20215 }
20216
20217 /* The interface the pragma parsers have to the lexer.  */
20218
20219 enum cpp_ttype
20220 pragma_lex (tree *value)
20221 {
20222   cp_token *tok;
20223   enum cpp_ttype ret;
20224
20225   tok = cp_lexer_peek_token (the_parser->lexer);
20226
20227   ret = tok->type;
20228   *value = tok->u.value;
20229
20230   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20231     ret = CPP_EOF;
20232   else if (ret == CPP_STRING)
20233     *value = cp_parser_string_literal (the_parser, false, false);
20234   else
20235     {
20236       cp_lexer_consume_token (the_parser->lexer);
20237       if (ret == CPP_KEYWORD)
20238         ret = CPP_NAME;
20239     }
20240
20241   return ret;
20242 }
20243
20244 \f
20245 /* External interface.  */
20246
20247 /* Parse one entire translation unit.  */
20248
20249 void
20250 c_parse_file (void)
20251 {
20252   bool error_occurred;
20253   static bool already_called = false;
20254
20255   if (already_called)
20256     {
20257       sorry ("inter-module optimizations not implemented for C++");
20258       return;
20259     }
20260   already_called = true;
20261
20262   the_parser = cp_parser_new ();
20263   push_deferring_access_checks (flag_access_control
20264                                 ? dk_no_deferred : dk_no_check);
20265   error_occurred = cp_parser_translation_unit (the_parser);
20266   the_parser = NULL;
20267 }
20268
20269 #include "gt-cp-parser.h"