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, 2007  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 3, 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 COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a system header.  */
75   BOOL_BITFIELD in_system_header : 1;
76   /* True if this token is from a context where it is implicitly extern "C" */
77   BOOL_BITFIELD implicit_extern_c : 1;
78   /* True for a CPP_NAME token that is not a keyword (i.e., for which
79      KEYWORD is RID_MAX) iff this name was looked up and found to be
80      ambiguous.  An error has already been reported.  */
81   BOOL_BITFIELD ambiguous_p : 1;
82   /* The input file stack index at which this token was found.  */
83   unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
84   /* The value associated with this token, if any.  */
85   union cp_token_value {
86     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
87     struct tree_check* GTY((tag ("1"))) tree_check_value;
88     /* Use for all other tokens.  */
89     tree GTY((tag ("0"))) value;
90   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
91   /* The location at which this token was found.  */
92   location_t location;
93 } cp_token;
94
95 /* We use a stack of token pointer for saving token sets.  */
96 typedef struct cp_token *cp_token_position;
97 DEF_VEC_P (cp_token_position);
98 DEF_VEC_ALLOC_P (cp_token_position,heap);
99
100 static cp_token eof_token =
101 {
102   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
103 #if USE_MAPPED_LOCATION
104   0
105 #else
106   {0, 0}
107 #endif
108 };
109
110 /* The cp_lexer structure represents the C++ lexer.  It is responsible
111    for managing the token stream from the preprocessor and supplying
112    it to the parser.  Tokens are never added to the cp_lexer after
113    it is created.  */
114
115 typedef struct cp_lexer GTY (())
116 {
117   /* The memory allocated for the buffer.  NULL if this lexer does not
118      own the token buffer.  */
119   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
120   /* If the lexer owns the buffer, this is the number of tokens in the
121      buffer.  */
122   size_t buffer_length;
123
124   /* A pointer just past the last available token.  The tokens
125      in this lexer are [buffer, last_token).  */
126   cp_token_position GTY ((skip)) last_token;
127
128   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
129      no more available tokens.  */
130   cp_token_position GTY ((skip)) next_token;
131
132   /* A stack indicating positions at which cp_lexer_save_tokens was
133      called.  The top entry is the most recent position at which we
134      began saving tokens.  If the stack is non-empty, we are saving
135      tokens.  */
136   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
137
138   /* The next lexer in a linked list of lexers.  */
139   struct cp_lexer *next;
140
141   /* True if we should output debugging information.  */
142   bool debugging_p;
143
144   /* True if we're in the context of parsing a pragma, and should not
145      increment past the end-of-line marker.  */
146   bool in_pragma;
147 } cp_lexer;
148
149 /* cp_token_cache is a range of tokens.  There is no need to represent
150    allocate heap memory for it, since tokens are never removed from the
151    lexer's array.  There is also no need for the GC to walk through
152    a cp_token_cache, since everything in here is referenced through
153    a lexer.  */
154
155 typedef struct cp_token_cache GTY(())
156 {
157   /* The beginning of the token range.  */
158   cp_token * GTY((skip)) first;
159
160   /* Points immediately after the last token in the range.  */
161   cp_token * GTY ((skip)) last;
162 } cp_token_cache;
163
164 /* Prototypes.  */
165
166 static cp_lexer *cp_lexer_new_main
167   (void);
168 static cp_lexer *cp_lexer_new_from_tokens
169   (cp_token_cache *tokens);
170 static void cp_lexer_destroy
171   (cp_lexer *);
172 static int cp_lexer_saving_tokens
173   (const cp_lexer *);
174 static cp_token_position cp_lexer_token_position
175   (cp_lexer *, bool);
176 static cp_token *cp_lexer_token_at
177   (cp_lexer *, cp_token_position);
178 static void cp_lexer_get_preprocessor_token
179   (cp_lexer *, cp_token *);
180 static inline cp_token *cp_lexer_peek_token
181   (cp_lexer *);
182 static cp_token *cp_lexer_peek_nth_token
183   (cp_lexer *, size_t);
184 static inline bool cp_lexer_next_token_is
185   (cp_lexer *, enum cpp_ttype);
186 static bool cp_lexer_next_token_is_not
187   (cp_lexer *, enum cpp_ttype);
188 static bool cp_lexer_next_token_is_keyword
189   (cp_lexer *, enum rid);
190 static cp_token *cp_lexer_consume_token
191   (cp_lexer *);
192 static void cp_lexer_purge_token
193   (cp_lexer *);
194 static void cp_lexer_purge_tokens_after
195   (cp_lexer *, cp_token_position);
196 static void cp_lexer_save_tokens
197   (cp_lexer *);
198 static void cp_lexer_commit_tokens
199   (cp_lexer *);
200 static void cp_lexer_rollback_tokens
201   (cp_lexer *);
202 #ifdef ENABLE_CHECKING
203 static void cp_lexer_print_token
204   (FILE *, cp_token *);
205 static inline bool cp_lexer_debugging_p
206   (cp_lexer *);
207 static void cp_lexer_start_debugging
208   (cp_lexer *) ATTRIBUTE_UNUSED;
209 static void cp_lexer_stop_debugging
210   (cp_lexer *) ATTRIBUTE_UNUSED;
211 #else
212 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
213    about passing NULL to functions that require non-NULL arguments
214    (fputs, fprintf).  It will never be used, so all we need is a value
215    of the right type that's guaranteed not to be NULL.  */
216 #define cp_lexer_debug_stream stdout
217 #define cp_lexer_print_token(str, tok) (void) 0
218 #define cp_lexer_debugging_p(lexer) 0
219 #endif /* ENABLE_CHECKING */
220
221 static cp_token_cache *cp_token_cache_new
222   (cp_token *, cp_token *);
223
224 static void cp_parser_initial_pragma
225   (cp_token *);
226
227 /* Manifest constants.  */
228 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
229 #define CP_SAVED_TOKEN_STACK 5
230
231 /* A token type for keywords, as opposed to ordinary identifiers.  */
232 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
233
234 /* A token type for template-ids.  If a template-id is processed while
235    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
236    the value of the CPP_TEMPLATE_ID is whatever was returned by
237    cp_parser_template_id.  */
238 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
239
240 /* A token type for nested-name-specifiers.  If a
241    nested-name-specifier is processed while parsing tentatively, it is
242    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
243    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
244    cp_parser_nested_name_specifier_opt.  */
245 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
246
247 /* A token type for tokens that are not tokens at all; these are used
248    to represent slots in the array where there used to be a token
249    that has now been deleted.  */
250 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
251
252 /* The number of token types, including C++-specific ones.  */
253 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
254
255 /* Variables.  */
256
257 #ifdef ENABLE_CHECKING
258 /* The stream to which debugging output should be written.  */
259 static FILE *cp_lexer_debug_stream;
260 #endif /* ENABLE_CHECKING */
261
262 /* Create a new main C++ lexer, the lexer that gets tokens from the
263    preprocessor.  */
264
265 static cp_lexer *
266 cp_lexer_new_main (void)
267 {
268   cp_token first_token;
269   cp_lexer *lexer;
270   cp_token *pos;
271   size_t alloc;
272   size_t space;
273   cp_token *buffer;
274
275   /* It's possible that parsing the first pragma will load a PCH file,
276      which is a GC collection point.  So we have to do that before
277      allocating any memory.  */
278   cp_parser_initial_pragma (&first_token);
279
280   /* Tell c_lex_with_flags not to merge string constants.  */
281   c_lex_return_raw_strings = true;
282
283   c_common_no_more_pch ();
284
285   /* Allocate the memory.  */
286   lexer = GGC_CNEW (cp_lexer);
287
288 #ifdef ENABLE_CHECKING
289   /* Initially we are not debugging.  */
290   lexer->debugging_p = false;
291 #endif /* ENABLE_CHECKING */
292   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
293                                    CP_SAVED_TOKEN_STACK);
294
295   /* Create the buffer.  */
296   alloc = CP_LEXER_BUFFER_SIZE;
297   buffer = GGC_NEWVEC (cp_token, alloc);
298
299   /* Put the first token in the buffer.  */
300   space = alloc;
301   pos = buffer;
302   *pos = first_token;
303
304   /* Get the remaining tokens from the preprocessor.  */
305   while (pos->type != CPP_EOF)
306     {
307       pos++;
308       if (!--space)
309         {
310           space = alloc;
311           alloc *= 2;
312           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
313           pos = buffer + space;
314         }
315       cp_lexer_get_preprocessor_token (lexer, pos);
316     }
317   lexer->buffer = buffer;
318   lexer->buffer_length = alloc - space;
319   lexer->last_token = pos;
320   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
321
322   /* Subsequent preprocessor diagnostics should use compiler
323      diagnostic functions to get the compiler source location.  */
324   cpp_get_options (parse_in)->client_diagnostic = true;
325   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
326
327   gcc_assert (lexer->next_token->type != CPP_PURGED);
328   return lexer;
329 }
330
331 /* Create a new lexer whose token stream is primed with the tokens in
332    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
333
334 static cp_lexer *
335 cp_lexer_new_from_tokens (cp_token_cache *cache)
336 {
337   cp_token *first = cache->first;
338   cp_token *last = cache->last;
339   cp_lexer *lexer = GGC_CNEW (cp_lexer);
340
341   /* We do not own the buffer.  */
342   lexer->buffer = NULL;
343   lexer->buffer_length = 0;
344   lexer->next_token = first == last ? &eof_token : first;
345   lexer->last_token = last;
346
347   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
348                                    CP_SAVED_TOKEN_STACK);
349
350 #ifdef ENABLE_CHECKING
351   /* Initially we are not debugging.  */
352   lexer->debugging_p = false;
353 #endif
354
355   gcc_assert (lexer->next_token->type != CPP_PURGED);
356   return lexer;
357 }
358
359 /* Frees all resources associated with LEXER.  */
360
361 static void
362 cp_lexer_destroy (cp_lexer *lexer)
363 {
364   if (lexer->buffer)
365     ggc_free (lexer->buffer);
366   VEC_free (cp_token_position, heap, lexer->saved_tokens);
367   ggc_free (lexer);
368 }
369
370 /* Returns nonzero if debugging information should be output.  */
371
372 #ifdef ENABLE_CHECKING
373
374 static inline bool
375 cp_lexer_debugging_p (cp_lexer *lexer)
376 {
377   return lexer->debugging_p;
378 }
379
380 #endif /* ENABLE_CHECKING */
381
382 static inline cp_token_position
383 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
384 {
385   gcc_assert (!previous_p || lexer->next_token != &eof_token);
386
387   return lexer->next_token - previous_p;
388 }
389
390 static inline cp_token *
391 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
392 {
393   return pos;
394 }
395
396 /* nonzero if we are presently saving tokens.  */
397
398 static inline int
399 cp_lexer_saving_tokens (const cp_lexer* lexer)
400 {
401   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
402 }
403
404 /* Store the next token from the preprocessor in *TOKEN.  Return true
405    if we reach EOF.  */
406
407 static void
408 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
409                                  cp_token *token)
410 {
411   static int is_extern_c = 0;
412
413    /* Get a new token from the preprocessor.  */
414   token->type
415     = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
416   token->input_file_stack_index = input_file_stack_tick;
417   token->keyword = RID_MAX;
418   token->pragma_kind = PRAGMA_NONE;
419   token->in_system_header = in_system_header;
420
421   /* On some systems, some header files are surrounded by an
422      implicit extern "C" block.  Set a flag in the token if it
423      comes from such a header.  */
424   is_extern_c += pending_lang_change;
425   pending_lang_change = 0;
426   token->implicit_extern_c = is_extern_c > 0;
427
428   /* Check to see if this token is a keyword.  */
429   if (token->type == CPP_NAME)
430     {
431       if (C_IS_RESERVED_WORD (token->u.value))
432         {
433           /* Mark this token as a keyword.  */
434           token->type = CPP_KEYWORD;
435           /* Record which keyword.  */
436           token->keyword = C_RID_CODE (token->u.value);
437           /* Update the value.  Some keywords are mapped to particular
438              entities, rather than simply having the value of the
439              corresponding IDENTIFIER_NODE.  For example, `__const' is
440              mapped to `const'.  */
441           token->u.value = ridpointers[token->keyword];
442         }
443       else
444         {
445           if (warn_cxx0x_compat
446               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
447               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
448             {
449               /* Warn about the C++0x keyword (but still treat it as
450                  an identifier).  */
451               warning (OPT_Wc__0x_compat, 
452                        "identifier %<%s%> will become a keyword in C++0x",
453                        IDENTIFIER_POINTER (token->u.value));
454
455               /* Clear out the C_RID_CODE so we don't warn about this
456                  particular identifier-turned-keyword again.  */
457               C_RID_CODE (token->u.value) = RID_MAX;
458             }
459
460           token->ambiguous_p = false;
461           token->keyword = RID_MAX;
462         }
463     }
464   /* Handle Objective-C++ keywords.  */
465   else if (token->type == CPP_AT_NAME)
466     {
467       token->type = CPP_KEYWORD;
468       switch (C_RID_CODE (token->u.value))
469         {
470         /* Map 'class' to '@class', 'private' to '@private', etc.  */
471         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
472         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
473         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
474         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
475         case RID_THROW: token->keyword = RID_AT_THROW; break;
476         case RID_TRY: token->keyword = RID_AT_TRY; break;
477         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
478         default: token->keyword = C_RID_CODE (token->u.value);
479         }
480     }
481   else if (token->type == CPP_PRAGMA)
482     {
483       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
484       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
485       token->u.value = NULL_TREE;
486     }
487 }
488
489 /* Update the globals input_location and in_system_header and the
490    input file stack from TOKEN.  */
491 static inline void
492 cp_lexer_set_source_position_from_token (cp_token *token)
493 {
494   if (token->type != CPP_EOF)
495     {
496       input_location = token->location;
497       in_system_header = token->in_system_header;
498       restore_input_file_stack (token->input_file_stack_index);
499     }
500 }
501
502 /* Return a pointer to the next token in the token stream, but do not
503    consume it.  */
504
505 static inline cp_token *
506 cp_lexer_peek_token (cp_lexer *lexer)
507 {
508   if (cp_lexer_debugging_p (lexer))
509     {
510       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
511       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
512       putc ('\n', cp_lexer_debug_stream);
513     }
514   return lexer->next_token;
515 }
516
517 /* Return true if the next token has the indicated TYPE.  */
518
519 static inline bool
520 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
521 {
522   return cp_lexer_peek_token (lexer)->type == type;
523 }
524
525 /* Return true if the next token does not have the indicated TYPE.  */
526
527 static inline bool
528 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
529 {
530   return !cp_lexer_next_token_is (lexer, type);
531 }
532
533 /* Return true if the next token is the indicated KEYWORD.  */
534
535 static inline bool
536 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
537 {
538   return cp_lexer_peek_token (lexer)->keyword == keyword;
539 }
540
541 /* Return true if the next token is a keyword for a decl-specifier.  */
542
543 static bool
544 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
545 {
546   cp_token *token;
547
548   token = cp_lexer_peek_token (lexer);
549   switch (token->keyword) 
550     {
551       /* Storage classes.  */
552     case RID_AUTO:
553     case RID_REGISTER:
554     case RID_STATIC:
555     case RID_EXTERN:
556     case RID_MUTABLE:
557     case RID_THREAD:
558       /* Elaborated type specifiers.  */
559     case RID_ENUM:
560     case RID_CLASS:
561     case RID_STRUCT:
562     case RID_UNION:
563     case RID_TYPENAME:
564       /* Simple type specifiers.  */
565     case RID_CHAR:
566     case RID_WCHAR:
567     case RID_BOOL:
568     case RID_SHORT:
569     case RID_INT:
570     case RID_LONG:
571     case RID_SIGNED:
572     case RID_UNSIGNED:
573     case RID_FLOAT:
574     case RID_DOUBLE:
575     case RID_VOID:
576       /* GNU extensions.  */ 
577     case RID_ATTRIBUTE:
578     case RID_TYPEOF:
579       /* C++0x extensions.  */
580     case RID_DECLTYPE:
581       return true;
582
583     default:
584       return false;
585     }
586 }
587
588 /* Return a pointer to the Nth token in the token stream.  If N is 1,
589    then this is precisely equivalent to cp_lexer_peek_token (except
590    that it is not inline).  One would like to disallow that case, but
591    there is one case (cp_parser_nth_token_starts_template_id) where
592    the caller passes a variable for N and it might be 1.  */
593
594 static cp_token *
595 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
596 {
597   cp_token *token;
598
599   /* N is 1-based, not zero-based.  */
600   gcc_assert (n > 0);
601
602   if (cp_lexer_debugging_p (lexer))
603     fprintf (cp_lexer_debug_stream,
604              "cp_lexer: peeking ahead %ld at token: ", (long)n);
605
606   --n;
607   token = lexer->next_token;
608   gcc_assert (!n || token != &eof_token);
609   while (n != 0)
610     {
611       ++token;
612       if (token == lexer->last_token)
613         {
614           token = &eof_token;
615           break;
616         }
617
618       if (token->type != CPP_PURGED)
619         --n;
620     }
621
622   if (cp_lexer_debugging_p (lexer))
623     {
624       cp_lexer_print_token (cp_lexer_debug_stream, token);
625       putc ('\n', cp_lexer_debug_stream);
626     }
627
628   return token;
629 }
630
631 /* Return the next token, and advance the lexer's next_token pointer
632    to point to the next non-purged token.  */
633
634 static cp_token *
635 cp_lexer_consume_token (cp_lexer* lexer)
636 {
637   cp_token *token = lexer->next_token;
638
639   gcc_assert (token != &eof_token);
640   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
641
642   do
643     {
644       lexer->next_token++;
645       if (lexer->next_token == lexer->last_token)
646         {
647           lexer->next_token = &eof_token;
648           break;
649         }
650
651     }
652   while (lexer->next_token->type == CPP_PURGED);
653
654   cp_lexer_set_source_position_from_token (token);
655
656   /* Provide debugging output.  */
657   if (cp_lexer_debugging_p (lexer))
658     {
659       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
660       cp_lexer_print_token (cp_lexer_debug_stream, token);
661       putc ('\n', cp_lexer_debug_stream);
662     }
663
664   return token;
665 }
666
667 /* Permanently remove the next token from the token stream, and
668    advance the next_token pointer to refer to the next non-purged
669    token.  */
670
671 static void
672 cp_lexer_purge_token (cp_lexer *lexer)
673 {
674   cp_token *tok = lexer->next_token;
675
676   gcc_assert (tok != &eof_token);
677   tok->type = CPP_PURGED;
678   tok->location = UNKNOWN_LOCATION;
679   tok->u.value = NULL_TREE;
680   tok->keyword = RID_MAX;
681
682   do
683     {
684       tok++;
685       if (tok == lexer->last_token)
686         {
687           tok = &eof_token;
688           break;
689         }
690     }
691   while (tok->type == CPP_PURGED);
692   lexer->next_token = tok;
693 }
694
695 /* Permanently remove all tokens after TOK, up to, but not
696    including, the token that will be returned next by
697    cp_lexer_peek_token.  */
698
699 static void
700 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
701 {
702   cp_token *peek = lexer->next_token;
703
704   if (peek == &eof_token)
705     peek = lexer->last_token;
706
707   gcc_assert (tok < peek);
708
709   for ( tok += 1; tok != peek; tok += 1)
710     {
711       tok->type = CPP_PURGED;
712       tok->location = UNKNOWN_LOCATION;
713       tok->u.value = NULL_TREE;
714       tok->keyword = RID_MAX;
715     }
716 }
717
718 /* Begin saving tokens.  All tokens consumed after this point will be
719    preserved.  */
720
721 static void
722 cp_lexer_save_tokens (cp_lexer* lexer)
723 {
724   /* Provide debugging output.  */
725   if (cp_lexer_debugging_p (lexer))
726     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
727
728   VEC_safe_push (cp_token_position, heap,
729                  lexer->saved_tokens, lexer->next_token);
730 }
731
732 /* Commit to the portion of the token stream most recently saved.  */
733
734 static void
735 cp_lexer_commit_tokens (cp_lexer* lexer)
736 {
737   /* Provide debugging output.  */
738   if (cp_lexer_debugging_p (lexer))
739     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
740
741   VEC_pop (cp_token_position, lexer->saved_tokens);
742 }
743
744 /* Return all tokens saved since the last call to cp_lexer_save_tokens
745    to the token stream.  Stop saving tokens.  */
746
747 static void
748 cp_lexer_rollback_tokens (cp_lexer* lexer)
749 {
750   /* Provide debugging output.  */
751   if (cp_lexer_debugging_p (lexer))
752     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
753
754   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
755 }
756
757 /* Print a representation of the TOKEN on the STREAM.  */
758
759 #ifdef ENABLE_CHECKING
760
761 static void
762 cp_lexer_print_token (FILE * stream, cp_token *token)
763 {
764   /* We don't use cpp_type2name here because the parser defines
765      a few tokens of its own.  */
766   static const char *const token_names[] = {
767     /* cpplib-defined token types */
768 #define OP(e, s) #e,
769 #define TK(e, s) #e,
770     TTYPE_TABLE
771 #undef OP
772 #undef TK
773     /* C++ parser token types - see "Manifest constants", above.  */
774     "KEYWORD",
775     "TEMPLATE_ID",
776     "NESTED_NAME_SPECIFIER",
777     "PURGED"
778   };
779
780   /* If we have a name for the token, print it out.  Otherwise, we
781      simply give the numeric code.  */
782   gcc_assert (token->type < ARRAY_SIZE(token_names));
783   fputs (token_names[token->type], stream);
784
785   /* For some tokens, print the associated data.  */
786   switch (token->type)
787     {
788     case CPP_KEYWORD:
789       /* Some keywords have a value that is not an IDENTIFIER_NODE.
790          For example, `struct' is mapped to an INTEGER_CST.  */
791       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
792         break;
793       /* else fall through */
794     case CPP_NAME:
795       fputs (IDENTIFIER_POINTER (token->u.value), stream);
796       break;
797
798     case CPP_STRING:
799     case CPP_WSTRING:
800       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
801       break;
802
803     default:
804       break;
805     }
806 }
807
808 /* Start emitting debugging information.  */
809
810 static void
811 cp_lexer_start_debugging (cp_lexer* lexer)
812 {
813   lexer->debugging_p = true;
814 }
815
816 /* Stop emitting debugging information.  */
817
818 static void
819 cp_lexer_stop_debugging (cp_lexer* lexer)
820 {
821   lexer->debugging_p = false;
822 }
823
824 #endif /* ENABLE_CHECKING */
825
826 /* Create a new cp_token_cache, representing a range of tokens.  */
827
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
830 {
831   cp_token_cache *cache = GGC_NEW (cp_token_cache);
832   cache->first = first;
833   cache->last = last;
834   return cache;
835 }
836
837 \f
838 /* Decl-specifiers.  */
839
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
841
842 static void
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 {
845   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
846 }
847
848 /* Declarators.  */
849
850 /* Nothing other than the parser should be creating declarators;
851    declarators are a semi-syntactic representation of C++ entities.
852    Other parts of the front end that need to create entities (like
853    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
854
855 static cp_declarator *make_call_declarator
856   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
857 static cp_declarator *make_array_declarator
858   (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860   (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862   (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864   (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866   (cp_cv_quals, tree, cp_declarator *);
867
868 /* An erroneous declarator.  */
869 static cp_declarator *cp_error_declarator;
870
871 /* The obstack on which declarators and related data structures are
872    allocated.  */
873 static struct obstack declarator_obstack;
874
875 /* Alloc BYTES from the declarator memory pool.  */
876
877 static inline void *
878 alloc_declarator (size_t bytes)
879 {
880   return obstack_alloc (&declarator_obstack, bytes);
881 }
882
883 /* Allocate a declarator of the indicated KIND.  Clear fields that are
884    common to all declarators.  */
885
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
888 {
889   cp_declarator *declarator;
890
891   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892   declarator->kind = kind;
893   declarator->attributes = NULL_TREE;
894   declarator->declarator = NULL;
895   declarator->parameter_pack_p = false;
896
897   return declarator;
898 }
899
900 /* Make a declarator for a generalized identifier.  If
901    QUALIFYING_SCOPE is non-NULL, the identifier is
902    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
904    is, if any.   */
905
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908                     special_function_kind sfk)
909 {
910   cp_declarator *declarator;
911
912   /* It is valid to write:
913
914        class C { void f(); };
915        typedef C D;
916        void D::f();
917
918      The standard is not clear about whether `typedef const C D' is
919      legal; as of 2002-09-15 the committee is considering that
920      question.  EDG 3.0 allows that syntax.  Therefore, we do as
921      well.  */
922   if (qualifying_scope && TYPE_P (qualifying_scope))
923     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924
925   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928
929   declarator = make_declarator (cdk_id);
930   declarator->u.id.qualifying_scope = qualifying_scope;
931   declarator->u.id.unqualified_name = unqualified_name;
932   declarator->u.id.sfk = sfk;
933   
934   return declarator;
935 }
936
937 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
938    of modifiers such as const or volatile to apply to the pointer
939    type, represented as identifiers.  */
940
941 cp_declarator *
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 {
944   cp_declarator *declarator;
945
946   declarator = make_declarator (cdk_pointer);
947   declarator->declarator = target;
948   declarator->u.pointer.qualifiers = cv_qualifiers;
949   declarator->u.pointer.class_type = NULL_TREE;
950   if (target)
951     {
952       declarator->parameter_pack_p = target->parameter_pack_p;
953       target->parameter_pack_p = false;
954     }
955   else
956     declarator->parameter_pack_p = false;
957
958   return declarator;
959 }
960
961 /* Like make_pointer_declarator -- but for references.  */
962
963 cp_declarator *
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
965                            bool rvalue_ref)
966 {
967   cp_declarator *declarator;
968
969   declarator = make_declarator (cdk_reference);
970   declarator->declarator = target;
971   declarator->u.reference.qualifiers = cv_qualifiers;
972   declarator->u.reference.rvalue_ref = rvalue_ref;
973   if (target)
974     {
975       declarator->parameter_pack_p = target->parameter_pack_p;
976       target->parameter_pack_p = false;
977     }
978   else
979     declarator->parameter_pack_p = false;
980
981   return declarator;
982 }
983
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985    member of CLASS_TYPE.  */
986
987 cp_declarator *
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989                         cp_declarator *pointee)
990 {
991   cp_declarator *declarator;
992
993   declarator = make_declarator (cdk_ptrmem);
994   declarator->declarator = pointee;
995   declarator->u.pointer.qualifiers = cv_qualifiers;
996   declarator->u.pointer.class_type = class_type;
997
998   if (pointee)
999     {
1000       declarator->parameter_pack_p = pointee->parameter_pack_p;
1001       pointee->parameter_pack_p = false;
1002     }
1003   else
1004     declarator->parameter_pack_p = false;
1005
1006   return declarator;
1007 }
1008
1009 /* Make a declarator for the function given by TARGET, with the
1010    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1011    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1012    indicates what exceptions can be thrown.  */
1013
1014 cp_declarator *
1015 make_call_declarator (cp_declarator *target,
1016                       cp_parameter_declarator *parms,
1017                       cp_cv_quals cv_qualifiers,
1018                       tree exception_specification)
1019 {
1020   cp_declarator *declarator;
1021
1022   declarator = make_declarator (cdk_function);
1023   declarator->declarator = target;
1024   declarator->u.function.parameters = parms;
1025   declarator->u.function.qualifiers = cv_qualifiers;
1026   declarator->u.function.exception_specification = exception_specification;
1027   if (target)
1028     {
1029       declarator->parameter_pack_p = target->parameter_pack_p;
1030       target->parameter_pack_p = false;
1031     }
1032   else
1033     declarator->parameter_pack_p = false;
1034
1035   return declarator;
1036 }
1037
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039    defined by ELEMENT.  */
1040
1041 cp_declarator *
1042 make_array_declarator (cp_declarator *element, tree bounds)
1043 {
1044   cp_declarator *declarator;
1045
1046   declarator = make_declarator (cdk_array);
1047   declarator->declarator = element;
1048   declarator->u.array.bounds = bounds;
1049   if (element)
1050     {
1051       declarator->parameter_pack_p = element->parameter_pack_p;
1052       element->parameter_pack_p = false;
1053     }
1054   else
1055     declarator->parameter_pack_p = false;
1056
1057   return declarator;
1058 }
1059
1060 /* Determine whether the declarator we've seen so far can be a
1061    parameter pack, when followed by an ellipsis.  */
1062 static bool 
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 {
1065   /* Search for a declarator name, or any other declarator that goes
1066      after the point where the ellipsis could appear in a parameter
1067      pack. If we find any of these, then this declarator can not be
1068      made into a parameter pack.  */
1069   bool found = false;
1070   while (declarator && !found)
1071     {
1072       switch ((int)declarator->kind)
1073         {
1074         case cdk_id:
1075         case cdk_error:
1076         case cdk_array:
1077         case cdk_ptrmem:
1078           found = true;
1079           break;
1080           
1081         default:
1082           declarator = declarator->declarator;
1083           break;
1084         }
1085     }
1086
1087   return !found;
1088 }
1089
1090 cp_parameter_declarator *no_parameters;
1091
1092 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1093    DECLARATOR and DEFAULT_ARGUMENT.  */
1094
1095 cp_parameter_declarator *
1096 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1097                            cp_declarator *declarator,
1098                            tree default_argument)
1099 {
1100   cp_parameter_declarator *parameter;
1101
1102   parameter = ((cp_parameter_declarator *)
1103                alloc_declarator (sizeof (cp_parameter_declarator)));
1104   parameter->next = NULL;
1105   if (decl_specifiers)
1106     parameter->decl_specifiers = *decl_specifiers;
1107   else
1108     clear_decl_specs (&parameter->decl_specifiers);
1109   parameter->declarator = declarator;
1110   parameter->default_argument = default_argument;
1111   parameter->ellipsis_p = false;
1112
1113   return parameter;
1114 }
1115
1116 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1117
1118 static bool
1119 function_declarator_p (const cp_declarator *declarator)
1120 {
1121   while (declarator)
1122     {
1123       if (declarator->kind == cdk_function
1124           && declarator->declarator->kind == cdk_id)
1125         return true;
1126       if (declarator->kind == cdk_id
1127           || declarator->kind == cdk_error)
1128         return false;
1129       declarator = declarator->declarator;
1130     }
1131   return false;
1132 }
1133  
1134 /* The parser.  */
1135
1136 /* Overview
1137    --------
1138
1139    A cp_parser parses the token stream as specified by the C++
1140    grammar.  Its job is purely parsing, not semantic analysis.  For
1141    example, the parser breaks the token stream into declarators,
1142    expressions, statements, and other similar syntactic constructs.
1143    It does not check that the types of the expressions on either side
1144    of an assignment-statement are compatible, or that a function is
1145    not declared with a parameter of type `void'.
1146
1147    The parser invokes routines elsewhere in the compiler to perform
1148    semantic analysis and to build up the abstract syntax tree for the
1149    code processed.
1150
1151    The parser (and the template instantiation code, which is, in a
1152    way, a close relative of parsing) are the only parts of the
1153    compiler that should be calling push_scope and pop_scope, or
1154    related functions.  The parser (and template instantiation code)
1155    keeps track of what scope is presently active; everything else
1156    should simply honor that.  (The code that generates static
1157    initializers may also need to set the scope, in order to check
1158    access control correctly when emitting the initializers.)
1159
1160    Methodology
1161    -----------
1162
1163    The parser is of the standard recursive-descent variety.  Upcoming
1164    tokens in the token stream are examined in order to determine which
1165    production to use when parsing a non-terminal.  Some C++ constructs
1166    require arbitrary look ahead to disambiguate.  For example, it is
1167    impossible, in the general case, to tell whether a statement is an
1168    expression or declaration without scanning the entire statement.
1169    Therefore, the parser is capable of "parsing tentatively."  When the
1170    parser is not sure what construct comes next, it enters this mode.
1171    Then, while we attempt to parse the construct, the parser queues up
1172    error messages, rather than issuing them immediately, and saves the
1173    tokens it consumes.  If the construct is parsed successfully, the
1174    parser "commits", i.e., it issues any queued error messages and
1175    the tokens that were being preserved are permanently discarded.
1176    If, however, the construct is not parsed successfully, the parser
1177    rolls back its state completely so that it can resume parsing using
1178    a different alternative.
1179
1180    Future Improvements
1181    -------------------
1182
1183    The performance of the parser could probably be improved substantially.
1184    We could often eliminate the need to parse tentatively by looking ahead
1185    a little bit.  In some places, this approach might not entirely eliminate
1186    the need to parse tentatively, but it might still speed up the average
1187    case.  */
1188
1189 /* Flags that are passed to some parsing functions.  These values can
1190    be bitwise-ored together.  */
1191
1192 typedef enum cp_parser_flags
1193 {
1194   /* No flags.  */
1195   CP_PARSER_FLAGS_NONE = 0x0,
1196   /* The construct is optional.  If it is not present, then no error
1197      should be issued.  */
1198   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1199   /* When parsing a type-specifier, do not allow user-defined types.  */
1200   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1201 } cp_parser_flags;
1202
1203 /* The different kinds of declarators we want to parse.  */
1204
1205 typedef enum cp_parser_declarator_kind
1206 {
1207   /* We want an abstract declarator.  */
1208   CP_PARSER_DECLARATOR_ABSTRACT,
1209   /* We want a named declarator.  */
1210   CP_PARSER_DECLARATOR_NAMED,
1211   /* We don't mind, but the name must be an unqualified-id.  */
1212   CP_PARSER_DECLARATOR_EITHER
1213 } cp_parser_declarator_kind;
1214
1215 /* The precedence values used to parse binary expressions.  The minimum value
1216    of PREC must be 1, because zero is reserved to quickly discriminate
1217    binary operators from other tokens.  */
1218
1219 enum cp_parser_prec
1220 {
1221   PREC_NOT_OPERATOR,
1222   PREC_LOGICAL_OR_EXPRESSION,
1223   PREC_LOGICAL_AND_EXPRESSION,
1224   PREC_INCLUSIVE_OR_EXPRESSION,
1225   PREC_EXCLUSIVE_OR_EXPRESSION,
1226   PREC_AND_EXPRESSION,
1227   PREC_EQUALITY_EXPRESSION,
1228   PREC_RELATIONAL_EXPRESSION,
1229   PREC_SHIFT_EXPRESSION,
1230   PREC_ADDITIVE_EXPRESSION,
1231   PREC_MULTIPLICATIVE_EXPRESSION,
1232   PREC_PM_EXPRESSION,
1233   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1234 };
1235
1236 /* A mapping from a token type to a corresponding tree node type, with a
1237    precedence value.  */
1238
1239 typedef struct cp_parser_binary_operations_map_node
1240 {
1241   /* The token type.  */
1242   enum cpp_ttype token_type;
1243   /* The corresponding tree code.  */
1244   enum tree_code tree_type;
1245   /* The precedence of this operator.  */
1246   enum cp_parser_prec prec;
1247 } cp_parser_binary_operations_map_node;
1248
1249 /* The status of a tentative parse.  */
1250
1251 typedef enum cp_parser_status_kind
1252 {
1253   /* No errors have occurred.  */
1254   CP_PARSER_STATUS_KIND_NO_ERROR,
1255   /* An error has occurred.  */
1256   CP_PARSER_STATUS_KIND_ERROR,
1257   /* We are committed to this tentative parse, whether or not an error
1258      has occurred.  */
1259   CP_PARSER_STATUS_KIND_COMMITTED
1260 } cp_parser_status_kind;
1261
1262 typedef struct cp_parser_expression_stack_entry
1263 {
1264   /* Left hand side of the binary operation we are currently
1265      parsing.  */
1266   tree lhs;
1267   /* Original tree code for left hand side, if it was a binary
1268      expression itself (used for -Wparentheses).  */
1269   enum tree_code lhs_type;
1270   /* Tree code for the binary operation we are parsing.  */
1271   enum tree_code tree_type;
1272   /* Precedence of the binary operation we are parsing.  */
1273   int prec;
1274 } cp_parser_expression_stack_entry;
1275
1276 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1277    entries because precedence levels on the stack are monotonically
1278    increasing.  */
1279 typedef struct cp_parser_expression_stack_entry
1280   cp_parser_expression_stack[NUM_PREC_VALUES];
1281
1282 /* Context that is saved and restored when parsing tentatively.  */
1283 typedef struct cp_parser_context GTY (())
1284 {
1285   /* If this is a tentative parsing context, the status of the
1286      tentative parse.  */
1287   enum cp_parser_status_kind status;
1288   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1289      that are looked up in this context must be looked up both in the
1290      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1291      the context of the containing expression.  */
1292   tree object_type;
1293
1294   /* The next parsing context in the stack.  */
1295   struct cp_parser_context *next;
1296 } cp_parser_context;
1297
1298 /* Prototypes.  */
1299
1300 /* Constructors and destructors.  */
1301
1302 static cp_parser_context *cp_parser_context_new
1303   (cp_parser_context *);
1304
1305 /* Class variables.  */
1306
1307 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1308
1309 /* The operator-precedence table used by cp_parser_binary_expression.
1310    Transformed into an associative array (binops_by_token) by
1311    cp_parser_new.  */
1312
1313 static const cp_parser_binary_operations_map_node binops[] = {
1314   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1315   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1316
1317   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1318   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320
1321   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1322   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323
1324   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1325   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326
1327   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1328   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1330   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331
1332   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1333   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1334
1335   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1336
1337   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1338
1339   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1340
1341   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1342
1343   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1344 };
1345
1346 /* The same as binops, but initialized by cp_parser_new so that
1347    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1348    for speed.  */
1349 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1350
1351 /* Constructors and destructors.  */
1352
1353 /* Construct a new context.  The context below this one on the stack
1354    is given by NEXT.  */
1355
1356 static cp_parser_context *
1357 cp_parser_context_new (cp_parser_context* next)
1358 {
1359   cp_parser_context *context;
1360
1361   /* Allocate the storage.  */
1362   if (cp_parser_context_free_list != NULL)
1363     {
1364       /* Pull the first entry from the free list.  */
1365       context = cp_parser_context_free_list;
1366       cp_parser_context_free_list = context->next;
1367       memset (context, 0, sizeof (*context));
1368     }
1369   else
1370     context = GGC_CNEW (cp_parser_context);
1371
1372   /* No errors have occurred yet in this context.  */
1373   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1374   /* If this is not the bottomost context, copy information that we
1375      need from the previous context.  */
1376   if (next)
1377     {
1378       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1379          expression, then we are parsing one in this context, too.  */
1380       context->object_type = next->object_type;
1381       /* Thread the stack.  */
1382       context->next = next;
1383     }
1384
1385   return context;
1386 }
1387
1388 /* The cp_parser structure represents the C++ parser.  */
1389
1390 typedef struct cp_parser GTY(())
1391 {
1392   /* The lexer from which we are obtaining tokens.  */
1393   cp_lexer *lexer;
1394
1395   /* The scope in which names should be looked up.  If NULL_TREE, then
1396      we look up names in the scope that is currently open in the
1397      source program.  If non-NULL, this is either a TYPE or
1398      NAMESPACE_DECL for the scope in which we should look.  It can
1399      also be ERROR_MARK, when we've parsed a bogus scope.
1400
1401      This value is not cleared automatically after a name is looked
1402      up, so we must be careful to clear it before starting a new look
1403      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1404      will look up `Z' in the scope of `X', rather than the current
1405      scope.)  Unfortunately, it is difficult to tell when name lookup
1406      is complete, because we sometimes peek at a token, look it up,
1407      and then decide not to consume it.   */
1408   tree scope;
1409
1410   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1411      last lookup took place.  OBJECT_SCOPE is used if an expression
1412      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1413      respectively.  QUALIFYING_SCOPE is used for an expression of the
1414      form "X::Y"; it refers to X.  */
1415   tree object_scope;
1416   tree qualifying_scope;
1417
1418   /* A stack of parsing contexts.  All but the bottom entry on the
1419      stack will be tentative contexts.
1420
1421      We parse tentatively in order to determine which construct is in
1422      use in some situations.  For example, in order to determine
1423      whether a statement is an expression-statement or a
1424      declaration-statement we parse it tentatively as a
1425      declaration-statement.  If that fails, we then reparse the same
1426      token stream as an expression-statement.  */
1427   cp_parser_context *context;
1428
1429   /* True if we are parsing GNU C++.  If this flag is not set, then
1430      GNU extensions are not recognized.  */
1431   bool allow_gnu_extensions_p;
1432
1433   /* TRUE if the `>' token should be interpreted as the greater-than
1434      operator.  FALSE if it is the end of a template-id or
1435      template-parameter-list. In C++0x mode, this flag also applies to
1436      `>>' tokens, which are viewed as two consecutive `>' tokens when
1437      this flag is FALSE.  */
1438   bool greater_than_is_operator_p;
1439
1440   /* TRUE if default arguments are allowed within a parameter list
1441      that starts at this point. FALSE if only a gnu extension makes
1442      them permissible.  */
1443   bool default_arg_ok_p;
1444
1445   /* TRUE if we are parsing an integral constant-expression.  See
1446      [expr.const] for a precise definition.  */
1447   bool integral_constant_expression_p;
1448
1449   /* TRUE if we are parsing an integral constant-expression -- but a
1450      non-constant expression should be permitted as well.  This flag
1451      is used when parsing an array bound so that GNU variable-length
1452      arrays are tolerated.  */
1453   bool allow_non_integral_constant_expression_p;
1454
1455   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1456      been seen that makes the expression non-constant.  */
1457   bool non_integral_constant_expression_p;
1458
1459   /* TRUE if local variable names and `this' are forbidden in the
1460      current context.  */
1461   bool local_variables_forbidden_p;
1462
1463   /* TRUE if the declaration we are parsing is part of a
1464      linkage-specification of the form `extern string-literal
1465      declaration'.  */
1466   bool in_unbraced_linkage_specification_p;
1467
1468   /* TRUE if we are presently parsing a declarator, after the
1469      direct-declarator.  */
1470   bool in_declarator_p;
1471
1472   /* TRUE if we are presently parsing a template-argument-list.  */
1473   bool in_template_argument_list_p;
1474
1475   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1476      to IN_OMP_BLOCK if parsing OpenMP structured block and
1477      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1478      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1479      iteration-statement, OpenMP block or loop within that switch.  */
1480 #define IN_SWITCH_STMT          1
1481 #define IN_ITERATION_STMT       2
1482 #define IN_OMP_BLOCK            4
1483 #define IN_OMP_FOR              8
1484 #define IN_IF_STMT             16
1485   unsigned char in_statement;
1486
1487   /* TRUE if we are presently parsing the body of a switch statement.
1488      Note that this doesn't quite overlap with in_statement above.
1489      The difference relates to giving the right sets of error messages:
1490      "case not in switch" vs "break statement used with OpenMP...".  */
1491   bool in_switch_statement_p;
1492
1493   /* TRUE if we are parsing a type-id in an expression context.  In
1494      such a situation, both "type (expr)" and "type (type)" are valid
1495      alternatives.  */
1496   bool in_type_id_in_expr_p;
1497
1498   /* TRUE if we are currently in a header file where declarations are
1499      implicitly extern "C".  */
1500   bool implicit_extern_c;
1501
1502   /* TRUE if strings in expressions should be translated to the execution
1503      character set.  */
1504   bool translate_strings_p;
1505
1506   /* TRUE if we are presently parsing the body of a function, but not
1507      a local class.  */
1508   bool in_function_body;
1509
1510   /* If non-NULL, then we are parsing a construct where new type
1511      definitions are not permitted.  The string stored here will be
1512      issued as an error message if a type is defined.  */
1513   const char *type_definition_forbidden_message;
1514
1515   /* A list of lists. The outer list is a stack, used for member
1516      functions of local classes. At each level there are two sub-list,
1517      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1518      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1519      TREE_VALUE's. The functions are chained in reverse declaration
1520      order.
1521
1522      The TREE_PURPOSE sublist contains those functions with default
1523      arguments that need post processing, and the TREE_VALUE sublist
1524      contains those functions with definitions that need post
1525      processing.
1526
1527      These lists can only be processed once the outermost class being
1528      defined is complete.  */
1529   tree unparsed_functions_queues;
1530
1531   /* The number of classes whose definitions are currently in
1532      progress.  */
1533   unsigned num_classes_being_defined;
1534
1535   /* The number of template parameter lists that apply directly to the
1536      current declaration.  */
1537   unsigned num_template_parameter_lists;
1538 } cp_parser;
1539
1540 /* Prototypes.  */
1541
1542 /* Constructors and destructors.  */
1543
1544 static cp_parser *cp_parser_new
1545   (void);
1546
1547 /* Routines to parse various constructs.
1548
1549    Those that return `tree' will return the error_mark_node (rather
1550    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1551    Sometimes, they will return an ordinary node if error-recovery was
1552    attempted, even though a parse error occurred.  So, to check
1553    whether or not a parse error occurred, you should always use
1554    cp_parser_error_occurred.  If the construct is optional (indicated
1555    either by an `_opt' in the name of the function that does the
1556    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1557    the construct is not present.  */
1558
1559 /* Lexical conventions [gram.lex]  */
1560
1561 static tree cp_parser_identifier
1562   (cp_parser *);
1563 static tree cp_parser_string_literal
1564   (cp_parser *, bool, bool);
1565
1566 /* Basic concepts [gram.basic]  */
1567
1568 static bool cp_parser_translation_unit
1569   (cp_parser *);
1570
1571 /* Expressions [gram.expr]  */
1572
1573 static tree cp_parser_primary_expression
1574   (cp_parser *, bool, bool, bool, cp_id_kind *);
1575 static tree cp_parser_id_expression
1576   (cp_parser *, bool, bool, bool *, bool, bool);
1577 static tree cp_parser_unqualified_id
1578   (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier_opt
1580   (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier
1582   (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_class_or_namespace_name
1584   (cp_parser *, bool, bool, bool, bool, bool);
1585 static tree cp_parser_postfix_expression
1586   (cp_parser *, bool, bool, bool);
1587 static tree cp_parser_postfix_open_square_expression
1588   (cp_parser *, tree, bool);
1589 static tree cp_parser_postfix_dot_deref_expression
1590   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1591 static tree cp_parser_parenthesized_expression_list
1592   (cp_parser *, bool, bool, bool, bool *);
1593 static void cp_parser_pseudo_destructor_name
1594   (cp_parser *, tree *, tree *);
1595 static tree cp_parser_unary_expression
1596   (cp_parser *, bool, bool);
1597 static enum tree_code cp_parser_unary_operator
1598   (cp_token *);
1599 static tree cp_parser_new_expression
1600   (cp_parser *);
1601 static tree cp_parser_new_placement
1602   (cp_parser *);
1603 static tree cp_parser_new_type_id
1604   (cp_parser *, tree *);
1605 static cp_declarator *cp_parser_new_declarator_opt
1606   (cp_parser *);
1607 static cp_declarator *cp_parser_direct_new_declarator
1608   (cp_parser *);
1609 static tree cp_parser_new_initializer
1610   (cp_parser *);
1611 static tree cp_parser_delete_expression
1612   (cp_parser *);
1613 static tree cp_parser_cast_expression
1614   (cp_parser *, bool, bool);
1615 static tree cp_parser_binary_expression
1616   (cp_parser *, bool);
1617 static tree cp_parser_question_colon_clause
1618   (cp_parser *, tree);
1619 static tree cp_parser_assignment_expression
1620   (cp_parser *, bool);
1621 static enum tree_code cp_parser_assignment_operator_opt
1622   (cp_parser *);
1623 static tree cp_parser_expression
1624   (cp_parser *, bool);
1625 static tree cp_parser_constant_expression
1626   (cp_parser *, bool, bool *);
1627 static tree cp_parser_builtin_offsetof
1628   (cp_parser *);
1629
1630 /* Statements [gram.stmt.stmt]  */
1631
1632 static void cp_parser_statement
1633   (cp_parser *, tree, bool, bool *);
1634 static void cp_parser_label_for_labeled_statement
1635   (cp_parser *);
1636 static tree cp_parser_expression_statement
1637   (cp_parser *, tree);
1638 static tree cp_parser_compound_statement
1639   (cp_parser *, tree, bool);
1640 static void cp_parser_statement_seq_opt
1641   (cp_parser *, tree);
1642 static tree cp_parser_selection_statement
1643   (cp_parser *, bool *);
1644 static tree cp_parser_condition
1645   (cp_parser *);
1646 static tree cp_parser_iteration_statement
1647   (cp_parser *);
1648 static void cp_parser_for_init_statement
1649   (cp_parser *);
1650 static tree cp_parser_jump_statement
1651   (cp_parser *);
1652 static void cp_parser_declaration_statement
1653   (cp_parser *);
1654
1655 static tree cp_parser_implicitly_scoped_statement
1656   (cp_parser *, bool *);
1657 static void cp_parser_already_scoped_statement
1658   (cp_parser *);
1659
1660 /* Declarations [gram.dcl.dcl] */
1661
1662 static void cp_parser_declaration_seq_opt
1663   (cp_parser *);
1664 static void cp_parser_declaration
1665   (cp_parser *);
1666 static void cp_parser_block_declaration
1667   (cp_parser *, bool);
1668 static void cp_parser_simple_declaration
1669   (cp_parser *, bool);
1670 static void cp_parser_decl_specifier_seq
1671   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1672 static tree cp_parser_storage_class_specifier_opt
1673   (cp_parser *);
1674 static tree cp_parser_function_specifier_opt
1675   (cp_parser *, cp_decl_specifier_seq *);
1676 static tree cp_parser_type_specifier
1677   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1678    int *, bool *);
1679 static tree cp_parser_simple_type_specifier
1680   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1681 static tree cp_parser_type_name
1682   (cp_parser *);
1683 static tree cp_parser_elaborated_type_specifier
1684   (cp_parser *, bool, bool);
1685 static tree cp_parser_enum_specifier
1686   (cp_parser *);
1687 static void cp_parser_enumerator_list
1688   (cp_parser *, tree);
1689 static void cp_parser_enumerator_definition
1690   (cp_parser *, tree);
1691 static tree cp_parser_namespace_name
1692   (cp_parser *);
1693 static void cp_parser_namespace_definition
1694   (cp_parser *);
1695 static void cp_parser_namespace_body
1696   (cp_parser *);
1697 static tree cp_parser_qualified_namespace_specifier
1698   (cp_parser *);
1699 static void cp_parser_namespace_alias_definition
1700   (cp_parser *);
1701 static bool cp_parser_using_declaration
1702   (cp_parser *, bool);
1703 static void cp_parser_using_directive
1704   (cp_parser *);
1705 static void cp_parser_asm_definition
1706   (cp_parser *);
1707 static void cp_parser_linkage_specification
1708   (cp_parser *);
1709 static void cp_parser_static_assert
1710   (cp_parser *, bool);
1711 static tree cp_parser_decltype
1712   (cp_parser *);
1713
1714 /* Declarators [gram.dcl.decl] */
1715
1716 static tree cp_parser_init_declarator
1717   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1718 static cp_declarator *cp_parser_declarator
1719   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1720 static cp_declarator *cp_parser_direct_declarator
1721   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1722 static enum tree_code cp_parser_ptr_operator
1723   (cp_parser *, tree *, cp_cv_quals *);
1724 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1725   (cp_parser *);
1726 static tree cp_parser_declarator_id
1727   (cp_parser *, bool);
1728 static tree cp_parser_type_id
1729   (cp_parser *);
1730 static void cp_parser_type_specifier_seq
1731   (cp_parser *, bool, cp_decl_specifier_seq *);
1732 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1733   (cp_parser *);
1734 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1735   (cp_parser *, bool *);
1736 static cp_parameter_declarator *cp_parser_parameter_declaration
1737   (cp_parser *, bool, bool *);
1738 static void cp_parser_function_body
1739   (cp_parser *);
1740 static tree cp_parser_initializer
1741   (cp_parser *, bool *, bool *);
1742 static tree cp_parser_initializer_clause
1743   (cp_parser *, bool *);
1744 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1745   (cp_parser *, bool *);
1746
1747 static bool cp_parser_ctor_initializer_opt_and_function_body
1748   (cp_parser *);
1749
1750 /* Classes [gram.class] */
1751
1752 static tree cp_parser_class_name
1753   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1754 static tree cp_parser_class_specifier
1755   (cp_parser *);
1756 static tree cp_parser_class_head
1757   (cp_parser *, bool *, tree *, tree *);
1758 static enum tag_types cp_parser_class_key
1759   (cp_parser *);
1760 static void cp_parser_member_specification_opt
1761   (cp_parser *);
1762 static void cp_parser_member_declaration
1763   (cp_parser *);
1764 static tree cp_parser_pure_specifier
1765   (cp_parser *);
1766 static tree cp_parser_constant_initializer
1767   (cp_parser *);
1768
1769 /* Derived classes [gram.class.derived] */
1770
1771 static tree cp_parser_base_clause
1772   (cp_parser *);
1773 static tree cp_parser_base_specifier
1774   (cp_parser *);
1775
1776 /* Special member functions [gram.special] */
1777
1778 static tree cp_parser_conversion_function_id
1779   (cp_parser *);
1780 static tree cp_parser_conversion_type_id
1781   (cp_parser *);
1782 static cp_declarator *cp_parser_conversion_declarator_opt
1783   (cp_parser *);
1784 static bool cp_parser_ctor_initializer_opt
1785   (cp_parser *);
1786 static void cp_parser_mem_initializer_list
1787   (cp_parser *);
1788 static tree cp_parser_mem_initializer
1789   (cp_parser *);
1790 static tree cp_parser_mem_initializer_id
1791   (cp_parser *);
1792
1793 /* Overloading [gram.over] */
1794
1795 static tree cp_parser_operator_function_id
1796   (cp_parser *);
1797 static tree cp_parser_operator
1798   (cp_parser *);
1799
1800 /* Templates [gram.temp] */
1801
1802 static void cp_parser_template_declaration
1803   (cp_parser *, bool);
1804 static tree cp_parser_template_parameter_list
1805   (cp_parser *);
1806 static tree cp_parser_template_parameter
1807   (cp_parser *, bool *, bool *);
1808 static tree cp_parser_type_parameter
1809   (cp_parser *, bool *);
1810 static tree cp_parser_template_id
1811   (cp_parser *, bool, bool, bool);
1812 static tree cp_parser_template_name
1813   (cp_parser *, bool, bool, bool, bool *);
1814 static tree cp_parser_template_argument_list
1815   (cp_parser *);
1816 static tree cp_parser_template_argument
1817   (cp_parser *);
1818 static void cp_parser_explicit_instantiation
1819   (cp_parser *);
1820 static void cp_parser_explicit_specialization
1821   (cp_parser *);
1822
1823 /* Exception handling [gram.exception] */
1824
1825 static tree cp_parser_try_block
1826   (cp_parser *);
1827 static bool cp_parser_function_try_block
1828   (cp_parser *);
1829 static void cp_parser_handler_seq
1830   (cp_parser *);
1831 static void cp_parser_handler
1832   (cp_parser *);
1833 static tree cp_parser_exception_declaration
1834   (cp_parser *);
1835 static tree cp_parser_throw_expression
1836   (cp_parser *);
1837 static tree cp_parser_exception_specification_opt
1838   (cp_parser *);
1839 static tree cp_parser_type_id_list
1840   (cp_parser *);
1841
1842 /* GNU Extensions */
1843
1844 static tree cp_parser_asm_specification_opt
1845   (cp_parser *);
1846 static tree cp_parser_asm_operand_list
1847   (cp_parser *);
1848 static tree cp_parser_asm_clobber_list
1849   (cp_parser *);
1850 static tree cp_parser_attributes_opt
1851   (cp_parser *);
1852 static tree cp_parser_attribute_list
1853   (cp_parser *);
1854 static bool cp_parser_extension_opt
1855   (cp_parser *, int *);
1856 static void cp_parser_label_declaration
1857   (cp_parser *);
1858
1859 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1860 static bool cp_parser_pragma
1861   (cp_parser *, enum pragma_context);
1862
1863 /* Objective-C++ Productions */
1864
1865 static tree cp_parser_objc_message_receiver
1866   (cp_parser *);
1867 static tree cp_parser_objc_message_args
1868   (cp_parser *);
1869 static tree cp_parser_objc_message_expression
1870   (cp_parser *);
1871 static tree cp_parser_objc_encode_expression
1872   (cp_parser *);
1873 static tree cp_parser_objc_defs_expression
1874   (cp_parser *);
1875 static tree cp_parser_objc_protocol_expression
1876   (cp_parser *);
1877 static tree cp_parser_objc_selector_expression
1878   (cp_parser *);
1879 static tree cp_parser_objc_expression
1880   (cp_parser *);
1881 static bool cp_parser_objc_selector_p
1882   (enum cpp_ttype);
1883 static tree cp_parser_objc_selector
1884   (cp_parser *);
1885 static tree cp_parser_objc_protocol_refs_opt
1886   (cp_parser *);
1887 static void cp_parser_objc_declaration
1888   (cp_parser *);
1889 static tree cp_parser_objc_statement
1890   (cp_parser *);
1891
1892 /* Utility Routines */
1893
1894 static tree cp_parser_lookup_name
1895   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1896 static tree cp_parser_lookup_name_simple
1897   (cp_parser *, tree);
1898 static tree cp_parser_maybe_treat_template_as_class
1899   (tree, bool);
1900 static bool cp_parser_check_declarator_template_parameters
1901   (cp_parser *, cp_declarator *);
1902 static bool cp_parser_check_template_parameters
1903   (cp_parser *, unsigned);
1904 static tree cp_parser_simple_cast_expression
1905   (cp_parser *);
1906 static tree cp_parser_global_scope_opt
1907   (cp_parser *, bool);
1908 static bool cp_parser_constructor_declarator_p
1909   (cp_parser *, bool);
1910 static tree cp_parser_function_definition_from_specifiers_and_declarator
1911   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1912 static tree cp_parser_function_definition_after_declarator
1913   (cp_parser *, bool);
1914 static void cp_parser_template_declaration_after_export
1915   (cp_parser *, bool);
1916 static void cp_parser_perform_template_parameter_access_checks
1917   (VEC (deferred_access_check,gc)*);
1918 static tree cp_parser_single_declaration
1919   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1920 static tree cp_parser_functional_cast
1921   (cp_parser *, tree);
1922 static tree cp_parser_save_member_function_body
1923   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1924 static tree cp_parser_enclosed_template_argument_list
1925   (cp_parser *);
1926 static void cp_parser_save_default_args
1927   (cp_parser *, tree);
1928 static void cp_parser_late_parsing_for_member
1929   (cp_parser *, tree);
1930 static void cp_parser_late_parsing_default_args
1931   (cp_parser *, tree);
1932 static tree cp_parser_sizeof_operand
1933   (cp_parser *, enum rid);
1934 static tree cp_parser_trait_expr
1935   (cp_parser *, enum rid);
1936 static bool cp_parser_declares_only_class_p
1937   (cp_parser *);
1938 static void cp_parser_set_storage_class
1939   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1940 static void cp_parser_set_decl_spec_type
1941   (cp_decl_specifier_seq *, tree, bool);
1942 static bool cp_parser_friend_p
1943   (const cp_decl_specifier_seq *);
1944 static cp_token *cp_parser_require
1945   (cp_parser *, enum cpp_ttype, const char *);
1946 static cp_token *cp_parser_require_keyword
1947   (cp_parser *, enum rid, const char *);
1948 static bool cp_parser_token_starts_function_definition_p
1949   (cp_token *);
1950 static bool cp_parser_next_token_starts_class_definition_p
1951   (cp_parser *);
1952 static bool cp_parser_next_token_ends_template_argument_p
1953   (cp_parser *);
1954 static bool cp_parser_nth_token_starts_template_argument_list_p
1955   (cp_parser *, size_t);
1956 static enum tag_types cp_parser_token_is_class_key
1957   (cp_token *);
1958 static void cp_parser_check_class_key
1959   (enum tag_types, tree type);
1960 static void cp_parser_check_access_in_redeclaration
1961   (tree type);
1962 static bool cp_parser_optional_template_keyword
1963   (cp_parser *);
1964 static void cp_parser_pre_parsed_nested_name_specifier
1965   (cp_parser *);
1966 static void cp_parser_cache_group
1967   (cp_parser *, enum cpp_ttype, unsigned);
1968 static void cp_parser_parse_tentatively
1969   (cp_parser *);
1970 static void cp_parser_commit_to_tentative_parse
1971   (cp_parser *);
1972 static void cp_parser_abort_tentative_parse
1973   (cp_parser *);
1974 static bool cp_parser_parse_definitely
1975   (cp_parser *);
1976 static inline bool cp_parser_parsing_tentatively
1977   (cp_parser *);
1978 static bool cp_parser_uncommitted_to_tentative_parse_p
1979   (cp_parser *);
1980 static void cp_parser_error
1981   (cp_parser *, const char *);
1982 static void cp_parser_name_lookup_error
1983   (cp_parser *, tree, tree, const char *);
1984 static bool cp_parser_simulate_error
1985   (cp_parser *);
1986 static bool cp_parser_check_type_definition
1987   (cp_parser *);
1988 static void cp_parser_check_for_definition_in_return_type
1989   (cp_declarator *, tree);
1990 static void cp_parser_check_for_invalid_template_id
1991   (cp_parser *, tree);
1992 static bool cp_parser_non_integral_constant_expression
1993   (cp_parser *, const char *);
1994 static void cp_parser_diagnose_invalid_type_name
1995   (cp_parser *, tree, tree);
1996 static bool cp_parser_parse_and_diagnose_invalid_type_name
1997   (cp_parser *);
1998 static int cp_parser_skip_to_closing_parenthesis
1999   (cp_parser *, bool, bool, bool);
2000 static void cp_parser_skip_to_end_of_statement
2001   (cp_parser *);
2002 static void cp_parser_consume_semicolon_at_end_of_statement
2003   (cp_parser *);
2004 static void cp_parser_skip_to_end_of_block_or_statement
2005   (cp_parser *);
2006 static bool cp_parser_skip_to_closing_brace
2007   (cp_parser *);
2008 static void cp_parser_skip_to_end_of_template_parameter_list
2009   (cp_parser *);
2010 static void cp_parser_skip_to_pragma_eol
2011   (cp_parser*, cp_token *);
2012 static bool cp_parser_error_occurred
2013   (cp_parser *);
2014 static bool cp_parser_allow_gnu_extensions_p
2015   (cp_parser *);
2016 static bool cp_parser_is_string_literal
2017   (cp_token *);
2018 static bool cp_parser_is_keyword
2019   (cp_token *, enum rid);
2020 static tree cp_parser_make_typename_type
2021   (cp_parser *, tree, tree);
2022 static cp_declarator * cp_parser_make_indirect_declarator
2023   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2024
2025 /* Returns nonzero if we are parsing tentatively.  */
2026
2027 static inline bool
2028 cp_parser_parsing_tentatively (cp_parser* parser)
2029 {
2030   return parser->context->next != NULL;
2031 }
2032
2033 /* Returns nonzero if TOKEN is a string literal.  */
2034
2035 static bool
2036 cp_parser_is_string_literal (cp_token* token)
2037 {
2038   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2039 }
2040
2041 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2042
2043 static bool
2044 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2045 {
2046   return token->keyword == keyword;
2047 }
2048
2049 /* If not parsing tentatively, issue a diagnostic of the form
2050       FILE:LINE: MESSAGE before TOKEN
2051    where TOKEN is the next token in the input stream.  MESSAGE
2052    (specified by the caller) is usually of the form "expected
2053    OTHER-TOKEN".  */
2054
2055 static void
2056 cp_parser_error (cp_parser* parser, const char* message)
2057 {
2058   if (!cp_parser_simulate_error (parser))
2059     {
2060       cp_token *token = cp_lexer_peek_token (parser->lexer);
2061       /* This diagnostic makes more sense if it is tagged to the line
2062          of the token we just peeked at.  */
2063       cp_lexer_set_source_position_from_token (token);
2064
2065       if (token->type == CPP_PRAGMA)
2066         {
2067           error ("%<#pragma%> is not allowed here");
2068           cp_parser_skip_to_pragma_eol (parser, token);
2069           return;
2070         }
2071
2072       c_parse_error (message,
2073                      /* Because c_parser_error does not understand
2074                         CPP_KEYWORD, keywords are treated like
2075                         identifiers.  */
2076                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2077                      token->u.value);
2078     }
2079 }
2080
2081 /* Issue an error about name-lookup failing.  NAME is the
2082    IDENTIFIER_NODE DECL is the result of
2083    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2084    the thing that we hoped to find.  */
2085
2086 static void
2087 cp_parser_name_lookup_error (cp_parser* parser,
2088                              tree name,
2089                              tree decl,
2090                              const char* desired)
2091 {
2092   /* If name lookup completely failed, tell the user that NAME was not
2093      declared.  */
2094   if (decl == error_mark_node)
2095     {
2096       if (parser->scope && parser->scope != global_namespace)
2097         error ("%<%E::%E%> has not been declared",
2098                parser->scope, name);
2099       else if (parser->scope == global_namespace)
2100         error ("%<::%E%> has not been declared", name);
2101       else if (parser->object_scope
2102                && !CLASS_TYPE_P (parser->object_scope))
2103         error ("request for member %qE in non-class type %qT",
2104                name, parser->object_scope);
2105       else if (parser->object_scope)
2106         error ("%<%T::%E%> has not been declared",
2107                parser->object_scope, name);
2108       else
2109         error ("%qE has not been declared", name);
2110     }
2111   else if (parser->scope && parser->scope != global_namespace)
2112     error ("%<%E::%E%> %s", parser->scope, name, desired);
2113   else if (parser->scope == global_namespace)
2114     error ("%<::%E%> %s", name, desired);
2115   else
2116     error ("%qE %s", name, desired);
2117 }
2118
2119 /* If we are parsing tentatively, remember that an error has occurred
2120    during this tentative parse.  Returns true if the error was
2121    simulated; false if a message should be issued by the caller.  */
2122
2123 static bool
2124 cp_parser_simulate_error (cp_parser* parser)
2125 {
2126   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2127     {
2128       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2129       return true;
2130     }
2131   return false;
2132 }
2133
2134 /* Check for repeated decl-specifiers.  */
2135
2136 static void
2137 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2138 {
2139   cp_decl_spec ds;
2140
2141   for (ds = ds_first; ds != ds_last; ++ds)
2142     {
2143       unsigned count = decl_specs->specs[(int)ds];
2144       if (count < 2)
2145         continue;
2146       /* The "long" specifier is a special case because of "long long".  */
2147       if (ds == ds_long)
2148         {
2149           if (count > 2)
2150             error ("%<long long long%> is too long for GCC");
2151           else if (pedantic && !in_system_header && warn_long_long)
2152             pedwarn ("ISO C++ does not support %<long long%>");
2153         }
2154       else if (count > 1)
2155         {
2156           static const char *const decl_spec_names[] = {
2157             "signed",
2158             "unsigned",
2159             "short",
2160             "long",
2161             "const",
2162             "volatile",
2163             "restrict",
2164             "inline",
2165             "virtual",
2166             "explicit",
2167             "friend",
2168             "typedef",
2169             "__complex",
2170             "__thread"
2171           };
2172           error ("duplicate %qs", decl_spec_names[(int)ds]);
2173         }
2174     }
2175 }
2176
2177 /* This function is called when a type is defined.  If type
2178    definitions are forbidden at this point, an error message is
2179    issued.  */
2180
2181 static bool
2182 cp_parser_check_type_definition (cp_parser* parser)
2183 {
2184   /* If types are forbidden here, issue a message.  */
2185   if (parser->type_definition_forbidden_message)
2186     {
2187       /* Use `%s' to print the string in case there are any escape
2188          characters in the message.  */
2189       error ("%s", parser->type_definition_forbidden_message);
2190       return false;
2191     }
2192   return true;
2193 }
2194
2195 /* This function is called when the DECLARATOR is processed.  The TYPE
2196    was a type defined in the decl-specifiers.  If it is invalid to
2197    define a type in the decl-specifiers for DECLARATOR, an error is
2198    issued.  */
2199
2200 static void
2201 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2202                                                tree type)
2203 {
2204   /* [dcl.fct] forbids type definitions in return types.
2205      Unfortunately, it's not easy to know whether or not we are
2206      processing a return type until after the fact.  */
2207   while (declarator
2208          && (declarator->kind == cdk_pointer
2209              || declarator->kind == cdk_reference
2210              || declarator->kind == cdk_ptrmem))
2211     declarator = declarator->declarator;
2212   if (declarator
2213       && declarator->kind == cdk_function)
2214     {
2215       error ("new types may not be defined in a return type");
2216       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2217               type);
2218     }
2219 }
2220
2221 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2222    "<" in any valid C++ program.  If the next token is indeed "<",
2223    issue a message warning the user about what appears to be an
2224    invalid attempt to form a template-id.  */
2225
2226 static void
2227 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2228                                          tree type)
2229 {
2230   cp_token_position start = 0;
2231
2232   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2233     {
2234       if (TYPE_P (type))
2235         error ("%qT is not a template", type);
2236       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2237         error ("%qE is not a template", type);
2238       else
2239         error ("invalid template-id");
2240       /* Remember the location of the invalid "<".  */
2241       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2242         start = cp_lexer_token_position (parser->lexer, true);
2243       /* Consume the "<".  */
2244       cp_lexer_consume_token (parser->lexer);
2245       /* Parse the template arguments.  */
2246       cp_parser_enclosed_template_argument_list (parser);
2247       /* Permanently remove the invalid template arguments so that
2248          this error message is not issued again.  */
2249       if (start)
2250         cp_lexer_purge_tokens_after (parser->lexer, start);
2251     }
2252 }
2253
2254 /* If parsing an integral constant-expression, issue an error message
2255    about the fact that THING appeared and return true.  Otherwise,
2256    return false.  In either case, set
2257    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2258
2259 static bool
2260 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2261                                             const char *thing)
2262 {
2263   parser->non_integral_constant_expression_p = true;
2264   if (parser->integral_constant_expression_p)
2265     {
2266       if (!parser->allow_non_integral_constant_expression_p)
2267         {
2268           error ("%s cannot appear in a constant-expression", thing);
2269           return true;
2270         }
2271     }
2272   return false;
2273 }
2274
2275 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2276    qualifying scope (or NULL, if none) for ID.  This function commits
2277    to the current active tentative parse, if any.  (Otherwise, the
2278    problematic construct might be encountered again later, resulting
2279    in duplicate error messages.)  */
2280
2281 static void
2282 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2283 {
2284   tree decl, old_scope;
2285   /* Try to lookup the identifier.  */
2286   old_scope = parser->scope;
2287   parser->scope = scope;
2288   decl = cp_parser_lookup_name_simple (parser, id);
2289   parser->scope = old_scope;
2290   /* If the lookup found a template-name, it means that the user forgot
2291   to specify an argument list. Emit a useful error message.  */
2292   if (TREE_CODE (decl) == TEMPLATE_DECL)
2293     error ("invalid use of template-name %qE without an argument list", decl);
2294   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2295     error ("invalid use of destructor %qD as a type", id);
2296   else if (TREE_CODE (decl) == TYPE_DECL)
2297     /* Something like 'unsigned A a;'  */
2298     error ("invalid combination of multiple type-specifiers");
2299   else if (!parser->scope)
2300     {
2301       /* Issue an error message.  */
2302       error ("%qE does not name a type", id);
2303       /* If we're in a template class, it's possible that the user was
2304          referring to a type from a base class.  For example:
2305
2306            template <typename T> struct A { typedef T X; };
2307            template <typename T> struct B : public A<T> { X x; };
2308
2309          The user should have said "typename A<T>::X".  */
2310       if (processing_template_decl && current_class_type
2311           && TYPE_BINFO (current_class_type))
2312         {
2313           tree b;
2314
2315           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2316                b;
2317                b = TREE_CHAIN (b))
2318             {
2319               tree base_type = BINFO_TYPE (b);
2320               if (CLASS_TYPE_P (base_type)
2321                   && dependent_type_p (base_type))
2322                 {
2323                   tree field;
2324                   /* Go from a particular instantiation of the
2325                      template (which will have an empty TYPE_FIELDs),
2326                      to the main version.  */
2327                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2328                   for (field = TYPE_FIELDS (base_type);
2329                        field;
2330                        field = TREE_CHAIN (field))
2331                     if (TREE_CODE (field) == TYPE_DECL
2332                         && DECL_NAME (field) == id)
2333                       {
2334                         inform ("(perhaps %<typename %T::%E%> was intended)",
2335                                 BINFO_TYPE (b), id);
2336                         break;
2337                       }
2338                   if (field)
2339                     break;
2340                 }
2341             }
2342         }
2343     }
2344   /* Here we diagnose qualified-ids where the scope is actually correct,
2345      but the identifier does not resolve to a valid type name.  */
2346   else if (parser->scope != error_mark_node)
2347     {
2348       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2349         error ("%qE in namespace %qE does not name a type",
2350                id, parser->scope);
2351       else if (TYPE_P (parser->scope))
2352         error ("%qE in class %qT does not name a type", id, parser->scope);
2353       else
2354         gcc_unreachable ();
2355     }
2356   cp_parser_commit_to_tentative_parse (parser);
2357 }
2358
2359 /* Check for a common situation where a type-name should be present,
2360    but is not, and issue a sensible error message.  Returns true if an
2361    invalid type-name was detected.
2362
2363    The situation handled by this function are variable declarations of the
2364    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2365    Usually, `ID' should name a type, but if we got here it means that it
2366    does not. We try to emit the best possible error message depending on
2367    how exactly the id-expression looks like.  */
2368
2369 static bool
2370 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2371 {
2372   tree id;
2373
2374   cp_parser_parse_tentatively (parser);
2375   id = cp_parser_id_expression (parser,
2376                                 /*template_keyword_p=*/false,
2377                                 /*check_dependency_p=*/true,
2378                                 /*template_p=*/NULL,
2379                                 /*declarator_p=*/true,
2380                                 /*optional_p=*/false);
2381   /* After the id-expression, there should be a plain identifier,
2382      otherwise this is not a simple variable declaration. Also, if
2383      the scope is dependent, we cannot do much.  */
2384   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2385       || (parser->scope && TYPE_P (parser->scope)
2386           && dependent_type_p (parser->scope))
2387       || TREE_CODE (id) == TYPE_DECL)
2388     {
2389       cp_parser_abort_tentative_parse (parser);
2390       return false;
2391     }
2392   if (!cp_parser_parse_definitely (parser))
2393     return false;
2394
2395   /* Emit a diagnostic for the invalid type.  */
2396   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2397   /* Skip to the end of the declaration; there's no point in
2398      trying to process it.  */
2399   cp_parser_skip_to_end_of_block_or_statement (parser);
2400   return true;
2401 }
2402
2403 /* Consume tokens up to, and including, the next non-nested closing `)'.
2404    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2405    are doing error recovery. Returns -1 if OR_COMMA is true and we
2406    found an unnested comma.  */
2407
2408 static int
2409 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2410                                        bool recovering,
2411                                        bool or_comma,
2412                                        bool consume_paren)
2413 {
2414   unsigned paren_depth = 0;
2415   unsigned brace_depth = 0;
2416
2417   if (recovering && !or_comma
2418       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2419     return 0;
2420
2421   while (true)
2422     {
2423       cp_token * token = cp_lexer_peek_token (parser->lexer);
2424
2425       switch (token->type)
2426         {
2427         case CPP_EOF:
2428         case CPP_PRAGMA_EOL:
2429           /* If we've run out of tokens, then there is no closing `)'.  */
2430           return 0;
2431
2432         case CPP_SEMICOLON:
2433           /* This matches the processing in skip_to_end_of_statement.  */
2434           if (!brace_depth)
2435             return 0;
2436           break;
2437
2438         case CPP_OPEN_BRACE:
2439           ++brace_depth;
2440           break;
2441         case CPP_CLOSE_BRACE:
2442           if (!brace_depth--)
2443             return 0;
2444           break;
2445
2446         case CPP_COMMA:
2447           if (recovering && or_comma && !brace_depth && !paren_depth)
2448             return -1;
2449           break;
2450
2451         case CPP_OPEN_PAREN:
2452           if (!brace_depth)
2453             ++paren_depth;
2454           break;
2455
2456         case CPP_CLOSE_PAREN:
2457           if (!brace_depth && !paren_depth--)
2458             {
2459               if (consume_paren)
2460                 cp_lexer_consume_token (parser->lexer);
2461               return 1;
2462             }
2463           break;
2464
2465         default:
2466           break;
2467         }
2468
2469       /* Consume the token.  */
2470       cp_lexer_consume_token (parser->lexer);
2471     }
2472 }
2473
2474 /* Consume tokens until we reach the end of the current statement.
2475    Normally, that will be just before consuming a `;'.  However, if a
2476    non-nested `}' comes first, then we stop before consuming that.  */
2477
2478 static void
2479 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2480 {
2481   unsigned nesting_depth = 0;
2482
2483   while (true)
2484     {
2485       cp_token *token = cp_lexer_peek_token (parser->lexer);
2486
2487       switch (token->type)
2488         {
2489         case CPP_EOF:
2490         case CPP_PRAGMA_EOL:
2491           /* If we've run out of tokens, stop.  */
2492           return;
2493
2494         case CPP_SEMICOLON:
2495           /* If the next token is a `;', we have reached the end of the
2496              statement.  */
2497           if (!nesting_depth)
2498             return;
2499           break;
2500
2501         case CPP_CLOSE_BRACE:
2502           /* If this is a non-nested '}', stop before consuming it.
2503              That way, when confronted with something like:
2504
2505                { 3 + }
2506
2507              we stop before consuming the closing '}', even though we
2508              have not yet reached a `;'.  */
2509           if (nesting_depth == 0)
2510             return;
2511
2512           /* If it is the closing '}' for a block that we have
2513              scanned, stop -- but only after consuming the token.
2514              That way given:
2515
2516                 void f g () { ... }
2517                 typedef int I;
2518
2519              we will stop after the body of the erroneously declared
2520              function, but before consuming the following `typedef'
2521              declaration.  */
2522           if (--nesting_depth == 0)
2523             {
2524               cp_lexer_consume_token (parser->lexer);
2525               return;
2526             }
2527
2528         case CPP_OPEN_BRACE:
2529           ++nesting_depth;
2530           break;
2531
2532         default:
2533           break;
2534         }
2535
2536       /* Consume the token.  */
2537       cp_lexer_consume_token (parser->lexer);
2538     }
2539 }
2540
2541 /* This function is called at the end of a statement or declaration.
2542    If the next token is a semicolon, it is consumed; otherwise, error
2543    recovery is attempted.  */
2544
2545 static void
2546 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2547 {
2548   /* Look for the trailing `;'.  */
2549   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2550     {
2551       /* If there is additional (erroneous) input, skip to the end of
2552          the statement.  */
2553       cp_parser_skip_to_end_of_statement (parser);
2554       /* If the next token is now a `;', consume it.  */
2555       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2556         cp_lexer_consume_token (parser->lexer);
2557     }
2558 }
2559
2560 /* Skip tokens until we have consumed an entire block, or until we
2561    have consumed a non-nested `;'.  */
2562
2563 static void
2564 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2565 {
2566   int nesting_depth = 0;
2567
2568   while (nesting_depth >= 0)
2569     {
2570       cp_token *token = cp_lexer_peek_token (parser->lexer);
2571
2572       switch (token->type)
2573         {
2574         case CPP_EOF:
2575         case CPP_PRAGMA_EOL:
2576           /* If we've run out of tokens, stop.  */
2577           return;
2578
2579         case CPP_SEMICOLON:
2580           /* Stop if this is an unnested ';'. */
2581           if (!nesting_depth)
2582             nesting_depth = -1;
2583           break;
2584
2585         case CPP_CLOSE_BRACE:
2586           /* Stop if this is an unnested '}', or closes the outermost
2587              nesting level.  */
2588           nesting_depth--;
2589           if (!nesting_depth)
2590             nesting_depth = -1;
2591           break;
2592
2593         case CPP_OPEN_BRACE:
2594           /* Nest. */
2595           nesting_depth++;
2596           break;
2597
2598         default:
2599           break;
2600         }
2601
2602       /* Consume the token.  */
2603       cp_lexer_consume_token (parser->lexer);
2604     }
2605 }
2606
2607 /* Skip tokens until a non-nested closing curly brace is the next
2608    token, or there are no more tokens. Return true in the first case,
2609    false otherwise.  */
2610
2611 static bool
2612 cp_parser_skip_to_closing_brace (cp_parser *parser)
2613 {
2614   unsigned nesting_depth = 0;
2615
2616   while (true)
2617     {
2618       cp_token *token = cp_lexer_peek_token (parser->lexer);
2619
2620       switch (token->type)
2621         {
2622         case CPP_EOF:
2623         case CPP_PRAGMA_EOL:
2624           /* If we've run out of tokens, stop.  */
2625           return false;
2626
2627         case CPP_CLOSE_BRACE:
2628           /* If the next token is a non-nested `}', then we have reached
2629              the end of the current block.  */
2630           if (nesting_depth-- == 0)
2631             return true;
2632           break;
2633
2634         case CPP_OPEN_BRACE:
2635           /* If it the next token is a `{', then we are entering a new
2636              block.  Consume the entire block.  */
2637           ++nesting_depth;
2638           break;
2639
2640         default:
2641           break;
2642         }
2643
2644       /* Consume the token.  */
2645       cp_lexer_consume_token (parser->lexer);
2646     }
2647 }
2648
2649 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2650    parameter is the PRAGMA token, allowing us to purge the entire pragma
2651    sequence.  */
2652
2653 static void
2654 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2655 {
2656   cp_token *token;
2657
2658   parser->lexer->in_pragma = false;
2659
2660   do
2661     token = cp_lexer_consume_token (parser->lexer);
2662   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2663
2664   /* Ensure that the pragma is not parsed again.  */
2665   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2666 }
2667
2668 /* Require pragma end of line, resyncing with it as necessary.  The
2669    arguments are as for cp_parser_skip_to_pragma_eol.  */
2670
2671 static void
2672 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2673 {
2674   parser->lexer->in_pragma = false;
2675   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2676     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2677 }
2678
2679 /* This is a simple wrapper around make_typename_type. When the id is
2680    an unresolved identifier node, we can provide a superior diagnostic
2681    using cp_parser_diagnose_invalid_type_name.  */
2682
2683 static tree
2684 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2685 {
2686   tree result;
2687   if (TREE_CODE (id) == IDENTIFIER_NODE)
2688     {
2689       result = make_typename_type (scope, id, typename_type,
2690                                    /*complain=*/tf_none);
2691       if (result == error_mark_node)
2692         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2693       return result;
2694     }
2695   return make_typename_type (scope, id, typename_type, tf_error);
2696 }
2697
2698 /* This is a wrapper around the
2699    make_{pointer,ptrmem,reference}_declarator functions that decides
2700    which one to call based on the CODE and CLASS_TYPE arguments. The
2701    CODE argument should be one of the values returned by
2702    cp_parser_ptr_operator. */
2703 static cp_declarator *
2704 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2705                                     cp_cv_quals cv_qualifiers,
2706                                     cp_declarator *target)
2707 {
2708   if (code == ERROR_MARK)
2709     return cp_error_declarator;
2710
2711   if (code == INDIRECT_REF)
2712     if (class_type == NULL_TREE)
2713       return make_pointer_declarator (cv_qualifiers, target);
2714     else
2715       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2716   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2717     return make_reference_declarator (cv_qualifiers, target, false);
2718   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2719     return make_reference_declarator (cv_qualifiers, target, true);
2720   gcc_unreachable ();
2721 }
2722
2723 /* Create a new C++ parser.  */
2724
2725 static cp_parser *
2726 cp_parser_new (void)
2727 {
2728   cp_parser *parser;
2729   cp_lexer *lexer;
2730   unsigned i;
2731
2732   /* cp_lexer_new_main is called before calling ggc_alloc because
2733      cp_lexer_new_main might load a PCH file.  */
2734   lexer = cp_lexer_new_main ();
2735
2736   /* Initialize the binops_by_token so that we can get the tree
2737      directly from the token.  */
2738   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2739     binops_by_token[binops[i].token_type] = binops[i];
2740
2741   parser = GGC_CNEW (cp_parser);
2742   parser->lexer = lexer;
2743   parser->context = cp_parser_context_new (NULL);
2744
2745   /* For now, we always accept GNU extensions.  */
2746   parser->allow_gnu_extensions_p = 1;
2747
2748   /* The `>' token is a greater-than operator, not the end of a
2749      template-id.  */
2750   parser->greater_than_is_operator_p = true;
2751
2752   parser->default_arg_ok_p = true;
2753
2754   /* We are not parsing a constant-expression.  */
2755   parser->integral_constant_expression_p = false;
2756   parser->allow_non_integral_constant_expression_p = false;
2757   parser->non_integral_constant_expression_p = false;
2758
2759   /* Local variable names are not forbidden.  */
2760   parser->local_variables_forbidden_p = false;
2761
2762   /* We are not processing an `extern "C"' declaration.  */
2763   parser->in_unbraced_linkage_specification_p = false;
2764
2765   /* We are not processing a declarator.  */
2766   parser->in_declarator_p = false;
2767
2768   /* We are not processing a template-argument-list.  */
2769   parser->in_template_argument_list_p = false;
2770
2771   /* We are not in an iteration statement.  */
2772   parser->in_statement = 0;
2773
2774   /* We are not in a switch statement.  */
2775   parser->in_switch_statement_p = false;
2776
2777   /* We are not parsing a type-id inside an expression.  */
2778   parser->in_type_id_in_expr_p = false;
2779
2780   /* Declarations aren't implicitly extern "C".  */
2781   parser->implicit_extern_c = false;
2782
2783   /* String literals should be translated to the execution character set.  */
2784   parser->translate_strings_p = true;
2785
2786   /* We are not parsing a function body.  */
2787   parser->in_function_body = false;
2788
2789   /* The unparsed function queue is empty.  */
2790   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2791
2792   /* There are no classes being defined.  */
2793   parser->num_classes_being_defined = 0;
2794
2795   /* No template parameters apply.  */
2796   parser->num_template_parameter_lists = 0;
2797
2798   return parser;
2799 }
2800
2801 /* Create a cp_lexer structure which will emit the tokens in CACHE
2802    and push it onto the parser's lexer stack.  This is used for delayed
2803    parsing of in-class method bodies and default arguments, and should
2804    not be confused with tentative parsing.  */
2805 static void
2806 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2807 {
2808   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2809   lexer->next = parser->lexer;
2810   parser->lexer = lexer;
2811
2812   /* Move the current source position to that of the first token in the
2813      new lexer.  */
2814   cp_lexer_set_source_position_from_token (lexer->next_token);
2815 }
2816
2817 /* Pop the top lexer off the parser stack.  This is never used for the
2818    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2819 static void
2820 cp_parser_pop_lexer (cp_parser *parser)
2821 {
2822   cp_lexer *lexer = parser->lexer;
2823   parser->lexer = lexer->next;
2824   cp_lexer_destroy (lexer);
2825
2826   /* Put the current source position back where it was before this
2827      lexer was pushed.  */
2828   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2829 }
2830
2831 /* Lexical conventions [gram.lex]  */
2832
2833 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2834    identifier.  */
2835
2836 static tree
2837 cp_parser_identifier (cp_parser* parser)
2838 {
2839   cp_token *token;
2840
2841   /* Look for the identifier.  */
2842   token = cp_parser_require (parser, CPP_NAME, "identifier");
2843   /* Return the value.  */
2844   return token ? token->u.value : error_mark_node;
2845 }
2846
2847 /* Parse a sequence of adjacent string constants.  Returns a
2848    TREE_STRING representing the combined, nul-terminated string
2849    constant.  If TRANSLATE is true, translate the string to the
2850    execution character set.  If WIDE_OK is true, a wide string is
2851    invalid here.
2852
2853    C++98 [lex.string] says that if a narrow string literal token is
2854    adjacent to a wide string literal token, the behavior is undefined.
2855    However, C99 6.4.5p4 says that this results in a wide string literal.
2856    We follow C99 here, for consistency with the C front end.
2857
2858    This code is largely lifted from lex_string() in c-lex.c.
2859
2860    FUTURE: ObjC++ will need to handle @-strings here.  */
2861 static tree
2862 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2863 {
2864   tree value;
2865   bool wide = false;
2866   size_t count;
2867   struct obstack str_ob;
2868   cpp_string str, istr, *strs;
2869   cp_token *tok;
2870
2871   tok = cp_lexer_peek_token (parser->lexer);
2872   if (!cp_parser_is_string_literal (tok))
2873     {
2874       cp_parser_error (parser, "expected string-literal");
2875       return error_mark_node;
2876     }
2877
2878   /* Try to avoid the overhead of creating and destroying an obstack
2879      for the common case of just one string.  */
2880   if (!cp_parser_is_string_literal
2881       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2882     {
2883       cp_lexer_consume_token (parser->lexer);
2884
2885       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2886       str.len = TREE_STRING_LENGTH (tok->u.value);
2887       count = 1;
2888       if (tok->type == CPP_WSTRING)
2889         wide = true;
2890
2891       strs = &str;
2892     }
2893   else
2894     {
2895       gcc_obstack_init (&str_ob);
2896       count = 0;
2897
2898       do
2899         {
2900           cp_lexer_consume_token (parser->lexer);
2901           count++;
2902           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2903           str.len = TREE_STRING_LENGTH (tok->u.value);
2904           if (tok->type == CPP_WSTRING)
2905             wide = true;
2906
2907           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2908
2909           tok = cp_lexer_peek_token (parser->lexer);
2910         }
2911       while (cp_parser_is_string_literal (tok));
2912
2913       strs = (cpp_string *) obstack_finish (&str_ob);
2914     }
2915
2916   if (wide && !wide_ok)
2917     {
2918       cp_parser_error (parser, "a wide string is invalid in this context");
2919       wide = false;
2920     }
2921
2922   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2923       (parse_in, strs, count, &istr, wide))
2924     {
2925       value = build_string (istr.len, (const char *)istr.text);
2926       free (CONST_CAST (istr.text));
2927
2928       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2929       value = fix_string_type (value);
2930     }
2931   else
2932     /* cpp_interpret_string has issued an error.  */
2933     value = error_mark_node;
2934
2935   if (count > 1)
2936     obstack_free (&str_ob, 0);
2937
2938   return value;
2939 }
2940
2941
2942 /* Basic concepts [gram.basic]  */
2943
2944 /* Parse a translation-unit.
2945
2946    translation-unit:
2947      declaration-seq [opt]
2948
2949    Returns TRUE if all went well.  */
2950
2951 static bool
2952 cp_parser_translation_unit (cp_parser* parser)
2953 {
2954   /* The address of the first non-permanent object on the declarator
2955      obstack.  */
2956   static void *declarator_obstack_base;
2957
2958   bool success;
2959
2960   /* Create the declarator obstack, if necessary.  */
2961   if (!cp_error_declarator)
2962     {
2963       gcc_obstack_init (&declarator_obstack);
2964       /* Create the error declarator.  */
2965       cp_error_declarator = make_declarator (cdk_error);
2966       /* Create the empty parameter list.  */
2967       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2968       /* Remember where the base of the declarator obstack lies.  */
2969       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2970     }
2971
2972   cp_parser_declaration_seq_opt (parser);
2973
2974   /* If there are no tokens left then all went well.  */
2975   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2976     {
2977       /* Get rid of the token array; we don't need it any more.  */
2978       cp_lexer_destroy (parser->lexer);
2979       parser->lexer = NULL;
2980
2981       /* This file might have been a context that's implicitly extern
2982          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2983       if (parser->implicit_extern_c)
2984         {
2985           pop_lang_context ();
2986           parser->implicit_extern_c = false;
2987         }
2988
2989       /* Finish up.  */
2990       finish_translation_unit ();
2991
2992       success = true;
2993     }
2994   else
2995     {
2996       cp_parser_error (parser, "expected declaration");
2997       success = false;
2998     }
2999
3000   /* Make sure the declarator obstack was fully cleaned up.  */
3001   gcc_assert (obstack_next_free (&declarator_obstack)
3002               == declarator_obstack_base);
3003
3004   /* All went well.  */
3005   return success;
3006 }
3007
3008 /* Expressions [gram.expr] */
3009
3010 /* Parse a primary-expression.
3011
3012    primary-expression:
3013      literal
3014      this
3015      ( expression )
3016      id-expression
3017
3018    GNU Extensions:
3019
3020    primary-expression:
3021      ( compound-statement )
3022      __builtin_va_arg ( assignment-expression , type-id )
3023      __builtin_offsetof ( type-id , offsetof-expression )
3024
3025    C++ Extensions:
3026      __has_nothrow_assign ( type-id )   
3027      __has_nothrow_constructor ( type-id )
3028      __has_nothrow_copy ( type-id )
3029      __has_trivial_assign ( type-id )   
3030      __has_trivial_constructor ( type-id )
3031      __has_trivial_copy ( type-id )
3032      __has_trivial_destructor ( type-id )
3033      __has_virtual_destructor ( type-id )     
3034      __is_abstract ( type-id )
3035      __is_base_of ( type-id , type-id )
3036      __is_class ( type-id )
3037      __is_convertible_to ( type-id , type-id )     
3038      __is_empty ( type-id )
3039      __is_enum ( type-id )
3040      __is_pod ( type-id )
3041      __is_polymorphic ( type-id )
3042      __is_union ( type-id )
3043
3044    Objective-C++ Extension:
3045
3046    primary-expression:
3047      objc-expression
3048
3049    literal:
3050      __null
3051
3052    ADDRESS_P is true iff this expression was immediately preceded by
3053    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3054    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3055    true iff this expression is a template argument.
3056
3057    Returns a representation of the expression.  Upon return, *IDK
3058    indicates what kind of id-expression (if any) was present.  */
3059
3060 static tree
3061 cp_parser_primary_expression (cp_parser *parser,
3062                               bool address_p,
3063                               bool cast_p,
3064                               bool template_arg_p,
3065                               cp_id_kind *idk)
3066 {
3067   cp_token *token;
3068
3069   /* Assume the primary expression is not an id-expression.  */
3070   *idk = CP_ID_KIND_NONE;
3071
3072   /* Peek at the next token.  */
3073   token = cp_lexer_peek_token (parser->lexer);
3074   switch (token->type)
3075     {
3076       /* literal:
3077            integer-literal
3078            character-literal
3079            floating-literal
3080            string-literal
3081            boolean-literal  */
3082     case CPP_CHAR:
3083     case CPP_WCHAR:
3084     case CPP_NUMBER:
3085       token = cp_lexer_consume_token (parser->lexer);
3086       /* Floating-point literals are only allowed in an integral
3087          constant expression if they are cast to an integral or
3088          enumeration type.  */
3089       if (TREE_CODE (token->u.value) == REAL_CST
3090           && parser->integral_constant_expression_p
3091           && pedantic)
3092         {
3093           /* CAST_P will be set even in invalid code like "int(2.7 +
3094              ...)".   Therefore, we have to check that the next token
3095              is sure to end the cast.  */
3096           if (cast_p)
3097             {
3098               cp_token *next_token;
3099
3100               next_token = cp_lexer_peek_token (parser->lexer);
3101               if (/* The comma at the end of an
3102                      enumerator-definition.  */
3103                   next_token->type != CPP_COMMA
3104                   /* The curly brace at the end of an enum-specifier.  */
3105                   && next_token->type != CPP_CLOSE_BRACE
3106                   /* The end of a statement.  */
3107                   && next_token->type != CPP_SEMICOLON
3108                   /* The end of the cast-expression.  */
3109                   && next_token->type != CPP_CLOSE_PAREN
3110                   /* The end of an array bound.  */
3111                   && next_token->type != CPP_CLOSE_SQUARE
3112                   /* The closing ">" in a template-argument-list.  */
3113                   && (next_token->type != CPP_GREATER
3114                       || parser->greater_than_is_operator_p)
3115                   /* C++0x only: A ">>" treated like two ">" tokens,
3116                      in a template-argument-list.  */
3117                   && (next_token->type != CPP_RSHIFT
3118                       || (cxx_dialect == cxx98)
3119                       || parser->greater_than_is_operator_p))
3120                 cast_p = false;
3121             }
3122
3123           /* If we are within a cast, then the constraint that the
3124              cast is to an integral or enumeration type will be
3125              checked at that point.  If we are not within a cast, then
3126              this code is invalid.  */
3127           if (!cast_p)
3128             cp_parser_non_integral_constant_expression
3129               (parser, "floating-point literal");
3130         }
3131       return token->u.value;
3132
3133     case CPP_STRING:
3134     case CPP_WSTRING:
3135       /* ??? Should wide strings be allowed when parser->translate_strings_p
3136          is false (i.e. in attributes)?  If not, we can kill the third
3137          argument to cp_parser_string_literal.  */
3138       return cp_parser_string_literal (parser,
3139                                        parser->translate_strings_p,
3140                                        true);
3141
3142     case CPP_OPEN_PAREN:
3143       {
3144         tree expr;
3145         bool saved_greater_than_is_operator_p;
3146
3147         /* Consume the `('.  */
3148         cp_lexer_consume_token (parser->lexer);
3149         /* Within a parenthesized expression, a `>' token is always
3150            the greater-than operator.  */
3151         saved_greater_than_is_operator_p
3152           = parser->greater_than_is_operator_p;
3153         parser->greater_than_is_operator_p = true;
3154         /* If we see `( { ' then we are looking at the beginning of
3155            a GNU statement-expression.  */
3156         if (cp_parser_allow_gnu_extensions_p (parser)
3157             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3158           {
3159             /* Statement-expressions are not allowed by the standard.  */
3160             if (pedantic)
3161               pedwarn ("ISO C++ forbids braced-groups within expressions");
3162
3163             /* And they're not allowed outside of a function-body; you
3164                cannot, for example, write:
3165
3166                  int i = ({ int j = 3; j + 1; });
3167
3168                at class or namespace scope.  */
3169             if (!parser->in_function_body)
3170               {
3171                 error ("statement-expressions are allowed only inside functions");
3172                 cp_parser_skip_to_end_of_block_or_statement (parser);
3173                 expr = error_mark_node;
3174               }
3175             else
3176               {
3177                 /* Start the statement-expression.  */
3178                 expr = begin_stmt_expr ();
3179                 /* Parse the compound-statement.  */
3180                 cp_parser_compound_statement (parser, expr, false);
3181                 /* Finish up.  */
3182                 expr = finish_stmt_expr (expr, false);
3183               }
3184           }
3185         else
3186           {
3187             /* Parse the parenthesized expression.  */
3188             expr = cp_parser_expression (parser, cast_p);
3189             /* Let the front end know that this expression was
3190                enclosed in parentheses. This matters in case, for
3191                example, the expression is of the form `A::B', since
3192                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3193                not.  */
3194             finish_parenthesized_expr (expr);
3195           }
3196         /* The `>' token might be the end of a template-id or
3197            template-parameter-list now.  */
3198         parser->greater_than_is_operator_p
3199           = saved_greater_than_is_operator_p;
3200         /* Consume the `)'.  */
3201         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3202           cp_parser_skip_to_end_of_statement (parser);
3203
3204         return expr;
3205       }
3206
3207     case CPP_KEYWORD:
3208       switch (token->keyword)
3209         {
3210           /* These two are the boolean literals.  */
3211         case RID_TRUE:
3212           cp_lexer_consume_token (parser->lexer);
3213           return boolean_true_node;
3214         case RID_FALSE:
3215           cp_lexer_consume_token (parser->lexer);
3216           return boolean_false_node;
3217
3218           /* The `__null' literal.  */
3219         case RID_NULL:
3220           cp_lexer_consume_token (parser->lexer);
3221           return null_node;
3222
3223           /* Recognize the `this' keyword.  */
3224         case RID_THIS:
3225           cp_lexer_consume_token (parser->lexer);
3226           if (parser->local_variables_forbidden_p)
3227             {
3228               error ("%<this%> may not be used in this context");
3229               return error_mark_node;
3230             }
3231           /* Pointers cannot appear in constant-expressions.  */
3232           if (cp_parser_non_integral_constant_expression (parser,
3233                                                           "`this'"))
3234             return error_mark_node;
3235           return finish_this_expr ();
3236
3237           /* The `operator' keyword can be the beginning of an
3238              id-expression.  */
3239         case RID_OPERATOR:
3240           goto id_expression;
3241
3242         case RID_FUNCTION_NAME:
3243         case RID_PRETTY_FUNCTION_NAME:
3244         case RID_C99_FUNCTION_NAME:
3245           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3246              __func__ are the names of variables -- but they are
3247              treated specially.  Therefore, they are handled here,
3248              rather than relying on the generic id-expression logic
3249              below.  Grammatically, these names are id-expressions.
3250
3251              Consume the token.  */
3252           token = cp_lexer_consume_token (parser->lexer);
3253           /* Look up the name.  */
3254           return finish_fname (token->u.value);
3255
3256         case RID_VA_ARG:
3257           {
3258             tree expression;
3259             tree type;
3260
3261             /* The `__builtin_va_arg' construct is used to handle
3262                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3263             cp_lexer_consume_token (parser->lexer);
3264             /* Look for the opening `('.  */
3265             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3266             /* Now, parse the assignment-expression.  */
3267             expression = cp_parser_assignment_expression (parser,
3268                                                           /*cast_p=*/false);
3269             /* Look for the `,'.  */
3270             cp_parser_require (parser, CPP_COMMA, "`,'");
3271             /* Parse the type-id.  */
3272             type = cp_parser_type_id (parser);
3273             /* Look for the closing `)'.  */
3274             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3275             /* Using `va_arg' in a constant-expression is not
3276                allowed.  */
3277             if (cp_parser_non_integral_constant_expression (parser,
3278                                                             "`va_arg'"))
3279               return error_mark_node;
3280             return build_x_va_arg (expression, type);
3281           }
3282
3283         case RID_OFFSETOF:
3284           return cp_parser_builtin_offsetof (parser);
3285
3286         case RID_HAS_NOTHROW_ASSIGN:
3287         case RID_HAS_NOTHROW_CONSTRUCTOR:
3288         case RID_HAS_NOTHROW_COPY:        
3289         case RID_HAS_TRIVIAL_ASSIGN:
3290         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3291         case RID_HAS_TRIVIAL_COPY:        
3292         case RID_HAS_TRIVIAL_DESTRUCTOR:
3293         case RID_HAS_VIRTUAL_DESTRUCTOR:
3294         case RID_IS_ABSTRACT:
3295         case RID_IS_BASE_OF:
3296         case RID_IS_CLASS:
3297         case RID_IS_CONVERTIBLE_TO:
3298         case RID_IS_EMPTY:
3299         case RID_IS_ENUM:
3300         case RID_IS_POD:
3301         case RID_IS_POLYMORPHIC:
3302         case RID_IS_UNION:
3303           return cp_parser_trait_expr (parser, token->keyword);
3304
3305         /* Objective-C++ expressions.  */
3306         case RID_AT_ENCODE:
3307         case RID_AT_PROTOCOL:
3308         case RID_AT_SELECTOR:
3309           return cp_parser_objc_expression (parser);
3310
3311         default:
3312           cp_parser_error (parser, "expected primary-expression");
3313           return error_mark_node;
3314         }
3315
3316       /* An id-expression can start with either an identifier, a
3317          `::' as the beginning of a qualified-id, or the "operator"
3318          keyword.  */
3319     case CPP_NAME:
3320     case CPP_SCOPE:
3321     case CPP_TEMPLATE_ID:
3322     case CPP_NESTED_NAME_SPECIFIER:
3323       {
3324         tree id_expression;
3325         tree decl;
3326         const char *error_msg;
3327         bool template_p;
3328         bool done;
3329
3330       id_expression:
3331         /* Parse the id-expression.  */
3332         id_expression
3333           = cp_parser_id_expression (parser,
3334                                      /*template_keyword_p=*/false,
3335                                      /*check_dependency_p=*/true,
3336                                      &template_p,
3337                                      /*declarator_p=*/false,
3338                                      /*optional_p=*/false);
3339         if (id_expression == error_mark_node)
3340           return error_mark_node;
3341         token = cp_lexer_peek_token (parser->lexer);
3342         done = (token->type != CPP_OPEN_SQUARE
3343                 && token->type != CPP_OPEN_PAREN
3344                 && token->type != CPP_DOT
3345                 && token->type != CPP_DEREF
3346                 && token->type != CPP_PLUS_PLUS
3347                 && token->type != CPP_MINUS_MINUS);
3348         /* If we have a template-id, then no further lookup is
3349            required.  If the template-id was for a template-class, we
3350            will sometimes have a TYPE_DECL at this point.  */
3351         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3352                  || TREE_CODE (id_expression) == TYPE_DECL)
3353           decl = id_expression;
3354         /* Look up the name.  */
3355         else
3356           {
3357             tree ambiguous_decls;
3358
3359             decl = cp_parser_lookup_name (parser, id_expression,
3360                                           none_type,
3361                                           template_p,
3362                                           /*is_namespace=*/false,
3363                                           /*check_dependency=*/true,
3364                                           &ambiguous_decls);
3365             /* If the lookup was ambiguous, an error will already have
3366                been issued.  */
3367             if (ambiguous_decls)
3368               return error_mark_node;
3369
3370             /* In Objective-C++, an instance variable (ivar) may be preferred
3371                to whatever cp_parser_lookup_name() found.  */
3372             decl = objc_lookup_ivar (decl, id_expression);
3373
3374             /* If name lookup gives us a SCOPE_REF, then the
3375                qualifying scope was dependent.  */
3376             if (TREE_CODE (decl) == SCOPE_REF)
3377               {
3378                 /* At this point, we do not know if DECL is a valid
3379                    integral constant expression.  We assume that it is
3380                    in fact such an expression, so that code like:
3381
3382                       template <int N> struct A {
3383                         int a[B<N>::i];
3384                       };
3385                      
3386                    is accepted.  At template-instantiation time, we
3387                    will check that B<N>::i is actually a constant.  */
3388                 return decl;
3389               }
3390             /* Check to see if DECL is a local variable in a context
3391                where that is forbidden.  */
3392             if (parser->local_variables_forbidden_p
3393                 && local_variable_p (decl))
3394               {
3395                 /* It might be that we only found DECL because we are
3396                    trying to be generous with pre-ISO scoping rules.
3397                    For example, consider:
3398
3399                      int i;
3400                      void g() {
3401                        for (int i = 0; i < 10; ++i) {}
3402                        extern void f(int j = i);
3403                      }
3404
3405                    Here, name look up will originally find the out
3406                    of scope `i'.  We need to issue a warning message,
3407                    but then use the global `i'.  */
3408                 decl = check_for_out_of_scope_variable (decl);
3409                 if (local_variable_p (decl))
3410                   {
3411                     error ("local variable %qD may not appear in this context",
3412                            decl);
3413                     return error_mark_node;
3414                   }
3415               }
3416           }
3417
3418         decl = (finish_id_expression
3419                 (id_expression, decl, parser->scope,
3420                  idk,
3421                  parser->integral_constant_expression_p,
3422                  parser->allow_non_integral_constant_expression_p,
3423                  &parser->non_integral_constant_expression_p,
3424                  template_p, done, address_p,
3425                  template_arg_p,
3426                  &error_msg));
3427         if (error_msg)
3428           cp_parser_error (parser, error_msg);
3429         return decl;
3430       }
3431
3432       /* Anything else is an error.  */
3433     default:
3434       /* ...unless we have an Objective-C++ message or string literal,
3435          that is.  */
3436       if (c_dialect_objc ()
3437           && (token->type == CPP_OPEN_SQUARE
3438               || token->type == CPP_OBJC_STRING))
3439         return cp_parser_objc_expression (parser);
3440
3441       cp_parser_error (parser, "expected primary-expression");
3442       return error_mark_node;
3443     }
3444 }
3445
3446 /* Parse an id-expression.
3447
3448    id-expression:
3449      unqualified-id
3450      qualified-id
3451
3452    qualified-id:
3453      :: [opt] nested-name-specifier template [opt] unqualified-id
3454      :: identifier
3455      :: operator-function-id
3456      :: template-id
3457
3458    Return a representation of the unqualified portion of the
3459    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3460    a `::' or nested-name-specifier.
3461
3462    Often, if the id-expression was a qualified-id, the caller will
3463    want to make a SCOPE_REF to represent the qualified-id.  This
3464    function does not do this in order to avoid wastefully creating
3465    SCOPE_REFs when they are not required.
3466
3467    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3468    `template' keyword.
3469
3470    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3471    uninstantiated templates.
3472
3473    If *TEMPLATE_P is non-NULL, it is set to true iff the
3474    `template' keyword is used to explicitly indicate that the entity
3475    named is a template.
3476
3477    If DECLARATOR_P is true, the id-expression is appearing as part of
3478    a declarator, rather than as part of an expression.  */
3479
3480 static tree
3481 cp_parser_id_expression (cp_parser *parser,
3482                          bool template_keyword_p,
3483                          bool check_dependency_p,
3484                          bool *template_p,
3485                          bool declarator_p,
3486                          bool optional_p)
3487 {
3488   bool global_scope_p;
3489   bool nested_name_specifier_p;
3490
3491   /* Assume the `template' keyword was not used.  */
3492   if (template_p)
3493     *template_p = template_keyword_p;
3494
3495   /* Look for the optional `::' operator.  */
3496   global_scope_p
3497     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3498        != NULL_TREE);
3499   /* Look for the optional nested-name-specifier.  */
3500   nested_name_specifier_p
3501     = (cp_parser_nested_name_specifier_opt (parser,
3502                                             /*typename_keyword_p=*/false,
3503                                             check_dependency_p,
3504                                             /*type_p=*/false,
3505                                             declarator_p)
3506        != NULL_TREE);
3507   /* If there is a nested-name-specifier, then we are looking at
3508      the first qualified-id production.  */
3509   if (nested_name_specifier_p)
3510     {
3511       tree saved_scope;
3512       tree saved_object_scope;
3513       tree saved_qualifying_scope;
3514       tree unqualified_id;
3515       bool is_template;
3516
3517       /* See if the next token is the `template' keyword.  */
3518       if (!template_p)
3519         template_p = &is_template;
3520       *template_p = cp_parser_optional_template_keyword (parser);
3521       /* Name lookup we do during the processing of the
3522          unqualified-id might obliterate SCOPE.  */
3523       saved_scope = parser->scope;
3524       saved_object_scope = parser->object_scope;
3525       saved_qualifying_scope = parser->qualifying_scope;
3526       /* Process the final unqualified-id.  */
3527       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3528                                                  check_dependency_p,
3529                                                  declarator_p,
3530                                                  /*optional_p=*/false);
3531       /* Restore the SAVED_SCOPE for our caller.  */
3532       parser->scope = saved_scope;
3533       parser->object_scope = saved_object_scope;
3534       parser->qualifying_scope = saved_qualifying_scope;
3535
3536       return unqualified_id;
3537     }
3538   /* Otherwise, if we are in global scope, then we are looking at one
3539      of the other qualified-id productions.  */
3540   else if (global_scope_p)
3541     {
3542       cp_token *token;
3543       tree id;
3544
3545       /* Peek at the next token.  */
3546       token = cp_lexer_peek_token (parser->lexer);
3547
3548       /* If it's an identifier, and the next token is not a "<", then
3549          we can avoid the template-id case.  This is an optimization
3550          for this common case.  */
3551       if (token->type == CPP_NAME
3552           && !cp_parser_nth_token_starts_template_argument_list_p
3553                (parser, 2))
3554         return cp_parser_identifier (parser);
3555
3556       cp_parser_parse_tentatively (parser);
3557       /* Try a template-id.  */
3558       id = cp_parser_template_id (parser,
3559                                   /*template_keyword_p=*/false,
3560                                   /*check_dependency_p=*/true,
3561                                   declarator_p);
3562       /* If that worked, we're done.  */
3563       if (cp_parser_parse_definitely (parser))
3564         return id;
3565
3566       /* Peek at the next token.  (Changes in the token buffer may
3567          have invalidated the pointer obtained above.)  */
3568       token = cp_lexer_peek_token (parser->lexer);
3569
3570       switch (token->type)
3571         {
3572         case CPP_NAME:
3573           return cp_parser_identifier (parser);
3574
3575         case CPP_KEYWORD:
3576           if (token->keyword == RID_OPERATOR)
3577             return cp_parser_operator_function_id (parser);
3578           /* Fall through.  */
3579
3580         default:
3581           cp_parser_error (parser, "expected id-expression");
3582           return error_mark_node;
3583         }
3584     }
3585   else
3586     return cp_parser_unqualified_id (parser, template_keyword_p,
3587                                      /*check_dependency_p=*/true,
3588                                      declarator_p,
3589                                      optional_p);
3590 }
3591
3592 /* Parse an unqualified-id.
3593
3594    unqualified-id:
3595      identifier
3596      operator-function-id
3597      conversion-function-id
3598      ~ class-name
3599      template-id
3600
3601    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3602    keyword, in a construct like `A::template ...'.
3603
3604    Returns a representation of unqualified-id.  For the `identifier'
3605    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3606    production a BIT_NOT_EXPR is returned; the operand of the
3607    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3608    other productions, see the documentation accompanying the
3609    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3610    names are looked up in uninstantiated templates.  If DECLARATOR_P
3611    is true, the unqualified-id is appearing as part of a declarator,
3612    rather than as part of an expression.  */
3613
3614 static tree
3615 cp_parser_unqualified_id (cp_parser* parser,
3616                           bool template_keyword_p,
3617                           bool check_dependency_p,
3618                           bool declarator_p,
3619                           bool optional_p)
3620 {
3621   cp_token *token;
3622
3623   /* Peek at the next token.  */
3624   token = cp_lexer_peek_token (parser->lexer);
3625
3626   switch (token->type)
3627     {
3628     case CPP_NAME:
3629       {
3630         tree id;
3631
3632         /* We don't know yet whether or not this will be a
3633            template-id.  */
3634         cp_parser_parse_tentatively (parser);
3635         /* Try a template-id.  */
3636         id = cp_parser_template_id (parser, template_keyword_p,
3637                                     check_dependency_p,
3638                                     declarator_p);
3639         /* If it worked, we're done.  */
3640         if (cp_parser_parse_definitely (parser))
3641           return id;
3642         /* Otherwise, it's an ordinary identifier.  */
3643         return cp_parser_identifier (parser);
3644       }
3645
3646     case CPP_TEMPLATE_ID:
3647       return cp_parser_template_id (parser, template_keyword_p,
3648                                     check_dependency_p,
3649                                     declarator_p);
3650
3651     case CPP_COMPL:
3652       {
3653         tree type_decl;
3654         tree qualifying_scope;
3655         tree object_scope;
3656         tree scope;
3657         bool done;
3658
3659         /* Consume the `~' token.  */
3660         cp_lexer_consume_token (parser->lexer);
3661         /* Parse the class-name.  The standard, as written, seems to
3662            say that:
3663
3664              template <typename T> struct S { ~S (); };
3665              template <typename T> S<T>::~S() {}
3666
3667            is invalid, since `~' must be followed by a class-name, but
3668            `S<T>' is dependent, and so not known to be a class.
3669            That's not right; we need to look in uninstantiated
3670            templates.  A further complication arises from:
3671
3672              template <typename T> void f(T t) {
3673                t.T::~T();
3674              }
3675
3676            Here, it is not possible to look up `T' in the scope of `T'
3677            itself.  We must look in both the current scope, and the
3678            scope of the containing complete expression.
3679
3680            Yet another issue is:
3681
3682              struct S {
3683                int S;
3684                ~S();
3685              };
3686
3687              S::~S() {}
3688
3689            The standard does not seem to say that the `S' in `~S'
3690            should refer to the type `S' and not the data member
3691            `S::S'.  */
3692
3693         /* DR 244 says that we look up the name after the "~" in the
3694            same scope as we looked up the qualifying name.  That idea
3695            isn't fully worked out; it's more complicated than that.  */
3696         scope = parser->scope;
3697         object_scope = parser->object_scope;
3698         qualifying_scope = parser->qualifying_scope;
3699
3700         /* Check for invalid scopes.  */
3701         if (scope == error_mark_node)
3702           {
3703             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3704               cp_lexer_consume_token (parser->lexer);
3705             return error_mark_node;
3706           }
3707         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3708           {
3709             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3710               error ("scope %qT before %<~%> is not a class-name", scope);
3711             cp_parser_simulate_error (parser);
3712             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3713               cp_lexer_consume_token (parser->lexer);
3714             return error_mark_node;
3715           }
3716         gcc_assert (!scope || TYPE_P (scope));
3717
3718         /* If the name is of the form "X::~X" it's OK.  */
3719         token = cp_lexer_peek_token (parser->lexer);
3720         if (scope
3721             && token->type == CPP_NAME
3722             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3723                 == CPP_OPEN_PAREN)
3724             && constructor_name_p (token->u.value, scope))
3725           {
3726             cp_lexer_consume_token (parser->lexer);
3727             return build_nt (BIT_NOT_EXPR, scope);
3728           }
3729
3730         /* If there was an explicit qualification (S::~T), first look
3731            in the scope given by the qualification (i.e., S).  */
3732         done = false;
3733         type_decl = NULL_TREE;
3734         if (scope)
3735           {
3736             cp_parser_parse_tentatively (parser);
3737             type_decl = cp_parser_class_name (parser,
3738                                               /*typename_keyword_p=*/false,
3739                                               /*template_keyword_p=*/false,
3740                                               none_type,
3741                                               /*check_dependency=*/false,
3742                                               /*class_head_p=*/false,
3743                                               declarator_p);
3744             if (cp_parser_parse_definitely (parser))
3745               done = true;
3746           }
3747         /* In "N::S::~S", look in "N" as well.  */
3748         if (!done && scope && qualifying_scope)
3749           {
3750             cp_parser_parse_tentatively (parser);
3751             parser->scope = qualifying_scope;
3752             parser->object_scope = NULL_TREE;
3753             parser->qualifying_scope = NULL_TREE;
3754             type_decl
3755               = cp_parser_class_name (parser,
3756                                       /*typename_keyword_p=*/false,
3757                                       /*template_keyword_p=*/false,
3758                                       none_type,
3759                                       /*check_dependency=*/false,
3760                                       /*class_head_p=*/false,
3761                                       declarator_p);
3762             if (cp_parser_parse_definitely (parser))
3763               done = true;
3764           }
3765         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3766         else if (!done && object_scope)
3767           {
3768             cp_parser_parse_tentatively (parser);
3769             parser->scope = object_scope;
3770             parser->object_scope = NULL_TREE;
3771             parser->qualifying_scope = NULL_TREE;
3772             type_decl
3773               = cp_parser_class_name (parser,
3774                                       /*typename_keyword_p=*/false,
3775                                       /*template_keyword_p=*/false,
3776                                       none_type,
3777                                       /*check_dependency=*/false,
3778                                       /*class_head_p=*/false,
3779                                       declarator_p);
3780             if (cp_parser_parse_definitely (parser))
3781               done = true;
3782           }
3783         /* Look in the surrounding context.  */
3784         if (!done)
3785           {
3786             parser->scope = NULL_TREE;
3787             parser->object_scope = NULL_TREE;
3788             parser->qualifying_scope = NULL_TREE;
3789             type_decl
3790               = cp_parser_class_name (parser,
3791                                       /*typename_keyword_p=*/false,
3792                                       /*template_keyword_p=*/false,
3793                                       none_type,
3794                                       /*check_dependency=*/false,
3795                                       /*class_head_p=*/false,
3796                                       declarator_p);
3797           }
3798         /* If an error occurred, assume that the name of the
3799            destructor is the same as the name of the qualifying
3800            class.  That allows us to keep parsing after running
3801            into ill-formed destructor names.  */
3802         if (type_decl == error_mark_node && scope)
3803           return build_nt (BIT_NOT_EXPR, scope);
3804         else if (type_decl == error_mark_node)
3805           return error_mark_node;
3806
3807         /* Check that destructor name and scope match.  */
3808         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3809           {
3810             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3811               error ("declaration of %<~%T%> as member of %qT",
3812                      type_decl, scope);
3813             cp_parser_simulate_error (parser);
3814             return error_mark_node;
3815           }
3816
3817         /* [class.dtor]
3818
3819            A typedef-name that names a class shall not be used as the
3820            identifier in the declarator for a destructor declaration.  */
3821         if (declarator_p
3822             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3823             && !DECL_SELF_REFERENCE_P (type_decl)
3824             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3825           error ("typedef-name %qD used as destructor declarator",
3826                  type_decl);
3827
3828         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3829       }
3830
3831     case CPP_KEYWORD:
3832       if (token->keyword == RID_OPERATOR)
3833         {
3834           tree id;
3835
3836           /* This could be a template-id, so we try that first.  */
3837           cp_parser_parse_tentatively (parser);
3838           /* Try a template-id.  */
3839           id = cp_parser_template_id (parser, template_keyword_p,
3840                                       /*check_dependency_p=*/true,
3841                                       declarator_p);
3842           /* If that worked, we're done.  */
3843           if (cp_parser_parse_definitely (parser))
3844             return id;
3845           /* We still don't know whether we're looking at an
3846              operator-function-id or a conversion-function-id.  */
3847           cp_parser_parse_tentatively (parser);
3848           /* Try an operator-function-id.  */
3849           id = cp_parser_operator_function_id (parser);
3850           /* If that didn't work, try a conversion-function-id.  */
3851           if (!cp_parser_parse_definitely (parser))
3852             id = cp_parser_conversion_function_id (parser);
3853
3854           return id;
3855         }
3856       /* Fall through.  */
3857
3858     default:
3859       if (optional_p)
3860         return NULL_TREE;
3861       cp_parser_error (parser, "expected unqualified-id");
3862       return error_mark_node;
3863     }
3864 }
3865
3866 /* Parse an (optional) nested-name-specifier.
3867
3868    nested-name-specifier:
3869      class-or-namespace-name :: nested-name-specifier [opt]
3870      class-or-namespace-name :: template nested-name-specifier [opt]
3871
3872    PARSER->SCOPE should be set appropriately before this function is
3873    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3874    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3875    in name lookups.
3876
3877    Sets PARSER->SCOPE to the class (TYPE) or namespace
3878    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3879    it unchanged if there is no nested-name-specifier.  Returns the new
3880    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3881
3882    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3883    part of a declaration and/or decl-specifier.  */
3884
3885 static tree
3886 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3887                                      bool typename_keyword_p,
3888                                      bool check_dependency_p,
3889                                      bool type_p,
3890                                      bool is_declaration)
3891 {
3892   bool success = false;
3893   cp_token_position start = 0;
3894   cp_token *token;
3895
3896   /* Remember where the nested-name-specifier starts.  */
3897   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3898     {
3899       start = cp_lexer_token_position (parser->lexer, false);
3900       push_deferring_access_checks (dk_deferred);
3901     }
3902
3903   while (true)
3904     {
3905       tree new_scope;
3906       tree old_scope;
3907       tree saved_qualifying_scope;
3908       bool template_keyword_p;
3909
3910       /* Spot cases that cannot be the beginning of a
3911          nested-name-specifier.  */
3912       token = cp_lexer_peek_token (parser->lexer);
3913
3914       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3915          the already parsed nested-name-specifier.  */
3916       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3917         {
3918           /* Grab the nested-name-specifier and continue the loop.  */
3919           cp_parser_pre_parsed_nested_name_specifier (parser);
3920           /* If we originally encountered this nested-name-specifier
3921              with IS_DECLARATION set to false, we will not have
3922              resolved TYPENAME_TYPEs, so we must do so here.  */
3923           if (is_declaration
3924               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3925             {
3926               new_scope = resolve_typename_type (parser->scope,
3927                                                  /*only_current_p=*/false);
3928               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3929                 parser->scope = new_scope;
3930             }
3931           success = true;
3932           continue;
3933         }
3934
3935       /* Spot cases that cannot be the beginning of a
3936          nested-name-specifier.  On the second and subsequent times
3937          through the loop, we look for the `template' keyword.  */
3938       if (success && token->keyword == RID_TEMPLATE)
3939         ;
3940       /* A template-id can start a nested-name-specifier.  */
3941       else if (token->type == CPP_TEMPLATE_ID)
3942         ;
3943       else
3944         {
3945           /* If the next token is not an identifier, then it is
3946              definitely not a class-or-namespace-name.  */
3947           if (token->type != CPP_NAME)
3948             break;
3949           /* If the following token is neither a `<' (to begin a
3950              template-id), nor a `::', then we are not looking at a
3951              nested-name-specifier.  */
3952           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3953           if (token->type != CPP_SCOPE
3954               && !cp_parser_nth_token_starts_template_argument_list_p
3955                   (parser, 2))
3956             break;
3957         }
3958
3959       /* The nested-name-specifier is optional, so we parse
3960          tentatively.  */
3961       cp_parser_parse_tentatively (parser);
3962
3963       /* Look for the optional `template' keyword, if this isn't the
3964          first time through the loop.  */
3965       if (success)
3966         template_keyword_p = cp_parser_optional_template_keyword (parser);
3967       else
3968         template_keyword_p = false;
3969
3970       /* Save the old scope since the name lookup we are about to do
3971          might destroy it.  */
3972       old_scope = parser->scope;
3973       saved_qualifying_scope = parser->qualifying_scope;
3974       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3975          look up names in "X<T>::I" in order to determine that "Y" is
3976          a template.  So, if we have a typename at this point, we make
3977          an effort to look through it.  */
3978       if (is_declaration
3979           && !typename_keyword_p
3980           && parser->scope
3981           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3982         parser->scope = resolve_typename_type (parser->scope,
3983                                                /*only_current_p=*/false);
3984       /* Parse the qualifying entity.  */
3985       new_scope
3986         = cp_parser_class_or_namespace_name (parser,
3987                                              typename_keyword_p,
3988                                              template_keyword_p,
3989                                              check_dependency_p,
3990                                              type_p,
3991                                              is_declaration);
3992       /* Look for the `::' token.  */
3993       cp_parser_require (parser, CPP_SCOPE, "`::'");
3994
3995       /* If we found what we wanted, we keep going; otherwise, we're
3996          done.  */
3997       if (!cp_parser_parse_definitely (parser))
3998         {
3999           bool error_p = false;
4000
4001           /* Restore the OLD_SCOPE since it was valid before the
4002              failed attempt at finding the last
4003              class-or-namespace-name.  */
4004           parser->scope = old_scope;
4005           parser->qualifying_scope = saved_qualifying_scope;
4006           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4007             break;
4008           /* If the next token is an identifier, and the one after
4009              that is a `::', then any valid interpretation would have
4010              found a class-or-namespace-name.  */
4011           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4012                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4013                      == CPP_SCOPE)
4014                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4015                      != CPP_COMPL))
4016             {
4017               token = cp_lexer_consume_token (parser->lexer);
4018               if (!error_p)
4019                 {
4020                   if (!token->ambiguous_p)
4021                     {
4022                       tree decl;
4023                       tree ambiguous_decls;
4024
4025                       decl = cp_parser_lookup_name (parser, token->u.value,
4026                                                     none_type,
4027                                                     /*is_template=*/false,
4028                                                     /*is_namespace=*/false,
4029                                                     /*check_dependency=*/true,
4030                                                     &ambiguous_decls);
4031                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4032                         error ("%qD used without template parameters", decl);
4033                       else if (ambiguous_decls)
4034                         {
4035                           error ("reference to %qD is ambiguous",
4036                                  token->u.value);
4037                           print_candidates (ambiguous_decls);
4038                           decl = error_mark_node;
4039                         }
4040                       else
4041                         cp_parser_name_lookup_error
4042                           (parser, token->u.value, decl,
4043                            "is not a class or namespace");
4044                     }
4045                   parser->scope = error_mark_node;
4046                   error_p = true;
4047                   /* Treat this as a successful nested-name-specifier
4048                      due to:
4049
4050                      [basic.lookup.qual]
4051
4052                      If the name found is not a class-name (clause
4053                      _class_) or namespace-name (_namespace.def_), the
4054                      program is ill-formed.  */
4055                   success = true;
4056                 }
4057               cp_lexer_consume_token (parser->lexer);
4058             }
4059           break;
4060         }
4061       /* We've found one valid nested-name-specifier.  */
4062       success = true;
4063       /* Name lookup always gives us a DECL.  */
4064       if (TREE_CODE (new_scope) == TYPE_DECL)
4065         new_scope = TREE_TYPE (new_scope);
4066       /* Uses of "template" must be followed by actual templates.  */
4067       if (template_keyword_p
4068           && !(CLASS_TYPE_P (new_scope)
4069                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4070                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4071                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4072           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4073                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4074                    == TEMPLATE_ID_EXPR)))
4075         pedwarn (TYPE_P (new_scope)
4076                  ? "%qT is not a template"
4077                  : "%qD is not a template",
4078                  new_scope);
4079       /* If it is a class scope, try to complete it; we are about to
4080          be looking up names inside the class.  */
4081       if (TYPE_P (new_scope)
4082           /* Since checking types for dependency can be expensive,
4083              avoid doing it if the type is already complete.  */
4084           && !COMPLETE_TYPE_P (new_scope)
4085           /* Do not try to complete dependent types.  */
4086           && !dependent_type_p (new_scope))
4087         new_scope = complete_type (new_scope);
4088       /* Make sure we look in the right scope the next time through
4089          the loop.  */
4090       parser->scope = new_scope;
4091     }
4092
4093   /* If parsing tentatively, replace the sequence of tokens that makes
4094      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4095      token.  That way, should we re-parse the token stream, we will
4096      not have to repeat the effort required to do the parse, nor will
4097      we issue duplicate error messages.  */
4098   if (success && start)
4099     {
4100       cp_token *token;
4101
4102       token = cp_lexer_token_at (parser->lexer, start);
4103       /* Reset the contents of the START token.  */
4104       token->type = CPP_NESTED_NAME_SPECIFIER;
4105       /* Retrieve any deferred checks.  Do not pop this access checks yet
4106          so the memory will not be reclaimed during token replacing below.  */
4107       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4108       token->u.tree_check_value->value = parser->scope;
4109       token->u.tree_check_value->checks = get_deferred_access_checks ();
4110       token->u.tree_check_value->qualifying_scope =
4111         parser->qualifying_scope;
4112       token->keyword = RID_MAX;
4113
4114       /* Purge all subsequent tokens.  */
4115       cp_lexer_purge_tokens_after (parser->lexer, start);
4116     }
4117
4118   if (start)
4119     pop_to_parent_deferring_access_checks ();
4120
4121   return success ? parser->scope : NULL_TREE;
4122 }
4123
4124 /* Parse a nested-name-specifier.  See
4125    cp_parser_nested_name_specifier_opt for details.  This function
4126    behaves identically, except that it will an issue an error if no
4127    nested-name-specifier is present.  */
4128
4129 static tree
4130 cp_parser_nested_name_specifier (cp_parser *parser,
4131                                  bool typename_keyword_p,
4132                                  bool check_dependency_p,
4133                                  bool type_p,
4134                                  bool is_declaration)
4135 {
4136   tree scope;
4137
4138   /* Look for the nested-name-specifier.  */
4139   scope = cp_parser_nested_name_specifier_opt (parser,
4140                                                typename_keyword_p,
4141                                                check_dependency_p,
4142                                                type_p,
4143                                                is_declaration);
4144   /* If it was not present, issue an error message.  */
4145   if (!scope)
4146     {
4147       cp_parser_error (parser, "expected nested-name-specifier");
4148       parser->scope = NULL_TREE;
4149     }
4150
4151   return scope;
4152 }
4153
4154 /* Parse a class-or-namespace-name.
4155
4156    class-or-namespace-name:
4157      class-name
4158      namespace-name
4159
4160    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4161    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4162    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4163    TYPE_P is TRUE iff the next name should be taken as a class-name,
4164    even the same name is declared to be another entity in the same
4165    scope.
4166
4167    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4168    specified by the class-or-namespace-name.  If neither is found the
4169    ERROR_MARK_NODE is returned.  */
4170
4171 static tree
4172 cp_parser_class_or_namespace_name (cp_parser *parser,
4173                                    bool typename_keyword_p,
4174                                    bool template_keyword_p,
4175                                    bool check_dependency_p,
4176                                    bool type_p,
4177                                    bool is_declaration)
4178 {
4179   tree saved_scope;
4180   tree saved_qualifying_scope;
4181   tree saved_object_scope;
4182   tree scope;
4183   bool only_class_p;
4184
4185   /* Before we try to parse the class-name, we must save away the
4186      current PARSER->SCOPE since cp_parser_class_name will destroy
4187      it.  */
4188   saved_scope = parser->scope;
4189   saved_qualifying_scope = parser->qualifying_scope;
4190   saved_object_scope = parser->object_scope;
4191   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4192      there is no need to look for a namespace-name.  */
4193   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4194   if (!only_class_p)
4195     cp_parser_parse_tentatively (parser);
4196   scope = cp_parser_class_name (parser,
4197                                 typename_keyword_p,
4198                                 template_keyword_p,
4199                                 type_p ? class_type : none_type,
4200                                 check_dependency_p,
4201                                 /*class_head_p=*/false,
4202                                 is_declaration);
4203   /* If that didn't work, try for a namespace-name.  */
4204   if (!only_class_p && !cp_parser_parse_definitely (parser))
4205     {
4206       /* Restore the saved scope.  */
4207       parser->scope = saved_scope;
4208       parser->qualifying_scope = saved_qualifying_scope;
4209       parser->object_scope = saved_object_scope;
4210       /* If we are not looking at an identifier followed by the scope
4211          resolution operator, then this is not part of a
4212          nested-name-specifier.  (Note that this function is only used
4213          to parse the components of a nested-name-specifier.)  */
4214       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4215           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4216         return error_mark_node;
4217       scope = cp_parser_namespace_name (parser);
4218     }
4219
4220   return scope;
4221 }
4222
4223 /* Parse a postfix-expression.
4224
4225    postfix-expression:
4226      primary-expression
4227      postfix-expression [ expression ]
4228      postfix-expression ( expression-list [opt] )
4229      simple-type-specifier ( expression-list [opt] )
4230      typename :: [opt] nested-name-specifier identifier
4231        ( expression-list [opt] )
4232      typename :: [opt] nested-name-specifier template [opt] template-id
4233        ( expression-list [opt] )
4234      postfix-expression . template [opt] id-expression
4235      postfix-expression -> template [opt] id-expression
4236      postfix-expression . pseudo-destructor-name
4237      postfix-expression -> pseudo-destructor-name
4238      postfix-expression ++
4239      postfix-expression --
4240      dynamic_cast < type-id > ( expression )
4241      static_cast < type-id > ( expression )
4242      reinterpret_cast < type-id > ( expression )
4243      const_cast < type-id > ( expression )
4244      typeid ( expression )
4245      typeid ( type-id )
4246
4247    GNU Extension:
4248
4249    postfix-expression:
4250      ( type-id ) { initializer-list , [opt] }
4251
4252    This extension is a GNU version of the C99 compound-literal
4253    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4254    but they are essentially the same concept.)
4255
4256    If ADDRESS_P is true, the postfix expression is the operand of the
4257    `&' operator.  CAST_P is true if this expression is the target of a
4258    cast.
4259
4260    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4261    class member access expressions [expr.ref].
4262
4263    Returns a representation of the expression.  */
4264
4265 static tree
4266 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4267                               bool member_access_only_p)
4268 {
4269   cp_token *token;
4270   enum rid keyword;
4271   cp_id_kind idk = CP_ID_KIND_NONE;
4272   tree postfix_expression = NULL_TREE;
4273   bool is_member_access = false;
4274
4275   /* Peek at the next token.  */
4276   token = cp_lexer_peek_token (parser->lexer);
4277   /* Some of the productions are determined by keywords.  */
4278   keyword = token->keyword;
4279   switch (keyword)
4280     {
4281     case RID_DYNCAST:
4282     case RID_STATCAST:
4283     case RID_REINTCAST:
4284     case RID_CONSTCAST:
4285       {
4286         tree type;
4287         tree expression;
4288         const char *saved_message;
4289
4290         /* All of these can be handled in the same way from the point
4291            of view of parsing.  Begin by consuming the token
4292            identifying the cast.  */
4293         cp_lexer_consume_token (parser->lexer);
4294
4295         /* New types cannot be defined in the cast.  */
4296         saved_message = parser->type_definition_forbidden_message;
4297         parser->type_definition_forbidden_message
4298           = "types may not be defined in casts";
4299
4300         /* Look for the opening `<'.  */
4301         cp_parser_require (parser, CPP_LESS, "`<'");
4302         /* Parse the type to which we are casting.  */
4303         type = cp_parser_type_id (parser);
4304         /* Look for the closing `>'.  */
4305         cp_parser_require (parser, CPP_GREATER, "`>'");
4306         /* Restore the old message.  */
4307         parser->type_definition_forbidden_message = saved_message;
4308
4309         /* And the expression which is being cast.  */
4310         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4311         expression = cp_parser_expression (parser, /*cast_p=*/true);
4312         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4313
4314         /* Only type conversions to integral or enumeration types
4315            can be used in constant-expressions.  */
4316         if (!cast_valid_in_integral_constant_expression_p (type)
4317             && (cp_parser_non_integral_constant_expression
4318                 (parser,
4319                  "a cast to a type other than an integral or "
4320                  "enumeration type")))
4321           return error_mark_node;
4322
4323         switch (keyword)
4324           {
4325           case RID_DYNCAST:
4326             postfix_expression
4327               = build_dynamic_cast (type, expression);
4328             break;
4329           case RID_STATCAST:
4330             postfix_expression
4331               = build_static_cast (type, expression);
4332             break;
4333           case RID_REINTCAST:
4334             postfix_expression
4335               = build_reinterpret_cast (type, expression);
4336             break;
4337           case RID_CONSTCAST:
4338             postfix_expression
4339               = build_const_cast (type, expression);
4340             break;
4341           default:
4342             gcc_unreachable ();
4343           }
4344       }
4345       break;
4346
4347     case RID_TYPEID:
4348       {
4349         tree type;
4350         const char *saved_message;
4351         bool saved_in_type_id_in_expr_p;
4352
4353         /* Consume the `typeid' token.  */
4354         cp_lexer_consume_token (parser->lexer);
4355         /* Look for the `(' token.  */
4356         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4357         /* Types cannot be defined in a `typeid' expression.  */
4358         saved_message = parser->type_definition_forbidden_message;
4359         parser->type_definition_forbidden_message
4360           = "types may not be defined in a `typeid\' expression";
4361         /* We can't be sure yet whether we're looking at a type-id or an
4362            expression.  */
4363         cp_parser_parse_tentatively (parser);
4364         /* Try a type-id first.  */
4365         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4366         parser->in_type_id_in_expr_p = true;
4367         type = cp_parser_type_id (parser);
4368         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4369         /* Look for the `)' token.  Otherwise, we can't be sure that
4370            we're not looking at an expression: consider `typeid (int
4371            (3))', for example.  */
4372         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4373         /* If all went well, simply lookup the type-id.  */
4374         if (cp_parser_parse_definitely (parser))
4375           postfix_expression = get_typeid (type);
4376         /* Otherwise, fall back to the expression variant.  */
4377         else
4378           {
4379             tree expression;
4380
4381             /* Look for an expression.  */
4382             expression = cp_parser_expression (parser, /*cast_p=*/false);
4383             /* Compute its typeid.  */
4384             postfix_expression = build_typeid (expression);
4385             /* Look for the `)' token.  */
4386             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4387           }
4388         /* Restore the saved message.  */
4389         parser->type_definition_forbidden_message = saved_message;
4390         /* `typeid' may not appear in an integral constant expression.  */
4391         if (cp_parser_non_integral_constant_expression(parser,
4392                                                        "`typeid' operator"))
4393           return error_mark_node;
4394       }
4395       break;
4396
4397     case RID_TYPENAME:
4398       {
4399         tree type;
4400         /* The syntax permitted here is the same permitted for an
4401            elaborated-type-specifier.  */
4402         type = cp_parser_elaborated_type_specifier (parser,
4403                                                     /*is_friend=*/false,
4404                                                     /*is_declaration=*/false);
4405         postfix_expression = cp_parser_functional_cast (parser, type);
4406       }
4407       break;
4408
4409     default:
4410       {
4411         tree type;
4412
4413         /* If the next thing is a simple-type-specifier, we may be
4414            looking at a functional cast.  We could also be looking at
4415            an id-expression.  So, we try the functional cast, and if
4416            that doesn't work we fall back to the primary-expression.  */
4417         cp_parser_parse_tentatively (parser);
4418         /* Look for the simple-type-specifier.  */
4419         type = cp_parser_simple_type_specifier (parser,
4420                                                 /*decl_specs=*/NULL,
4421                                                 CP_PARSER_FLAGS_NONE);
4422         /* Parse the cast itself.  */
4423         if (!cp_parser_error_occurred (parser))
4424           postfix_expression
4425             = cp_parser_functional_cast (parser, type);
4426         /* If that worked, we're done.  */
4427         if (cp_parser_parse_definitely (parser))
4428           break;
4429
4430         /* If the functional-cast didn't work out, try a
4431            compound-literal.  */
4432         if (cp_parser_allow_gnu_extensions_p (parser)
4433             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4434           {
4435             VEC(constructor_elt,gc) *initializer_list = NULL;
4436             bool saved_in_type_id_in_expr_p;
4437
4438             cp_parser_parse_tentatively (parser);
4439             /* Consume the `('.  */
4440             cp_lexer_consume_token (parser->lexer);
4441             /* Parse the type.  */
4442             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4443             parser->in_type_id_in_expr_p = true;
4444             type = cp_parser_type_id (parser);
4445             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4446             /* Look for the `)'.  */
4447             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4448             /* Look for the `{'.  */
4449             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4450             /* If things aren't going well, there's no need to
4451                keep going.  */
4452             if (!cp_parser_error_occurred (parser))
4453               {
4454                 bool non_constant_p;
4455                 /* Parse the initializer-list.  */
4456                 initializer_list
4457                   = cp_parser_initializer_list (parser, &non_constant_p);
4458                 /* Allow a trailing `,'.  */
4459                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4460                   cp_lexer_consume_token (parser->lexer);
4461                 /* Look for the final `}'.  */
4462                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4463               }
4464             /* If that worked, we're definitely looking at a
4465                compound-literal expression.  */
4466             if (cp_parser_parse_definitely (parser))
4467               {
4468                 /* Warn the user that a compound literal is not
4469                    allowed in standard C++.  */
4470                 if (pedantic)
4471                   pedwarn ("ISO C++ forbids compound-literals");
4472                 /* For simplicity, we disallow compound literals in
4473                    constant-expressions.  We could
4474                    allow compound literals of integer type, whose
4475                    initializer was a constant, in constant
4476                    expressions.  Permitting that usage, as a further
4477                    extension, would not change the meaning of any
4478                    currently accepted programs.  (Of course, as
4479                    compound literals are not part of ISO C++, the
4480                    standard has nothing to say.)  */
4481                 if (cp_parser_non_integral_constant_expression 
4482                     (parser, "non-constant compound literals"))
4483                   {
4484                     postfix_expression = error_mark_node;
4485                     break;
4486                   }
4487                 /* Form the representation of the compound-literal.  */
4488                 postfix_expression
4489                   = finish_compound_literal (type, initializer_list);
4490                 break;
4491               }
4492           }
4493
4494         /* It must be a primary-expression.  */
4495         postfix_expression
4496           = cp_parser_primary_expression (parser, address_p, cast_p,
4497                                           /*template_arg_p=*/false,
4498                                           &idk);
4499       }
4500       break;
4501     }
4502
4503   /* Keep looping until the postfix-expression is complete.  */
4504   while (true)
4505     {
4506       if (idk == CP_ID_KIND_UNQUALIFIED
4507           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4508           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4509         /* It is not a Koenig lookup function call.  */
4510         postfix_expression
4511           = unqualified_name_lookup_error (postfix_expression);
4512
4513       /* Peek at the next token.  */
4514       token = cp_lexer_peek_token (parser->lexer);
4515
4516       switch (token->type)
4517         {
4518         case CPP_OPEN_SQUARE:
4519           postfix_expression
4520             = cp_parser_postfix_open_square_expression (parser,
4521                                                         postfix_expression,
4522                                                         false);
4523           idk = CP_ID_KIND_NONE;
4524           is_member_access = false;
4525           break;
4526
4527         case CPP_OPEN_PAREN:
4528           /* postfix-expression ( expression-list [opt] ) */
4529           {
4530             bool koenig_p;
4531             bool is_builtin_constant_p;
4532             bool saved_integral_constant_expression_p = false;
4533             bool saved_non_integral_constant_expression_p = false;
4534             tree args;
4535
4536             is_member_access = false;
4537
4538             is_builtin_constant_p
4539               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4540             if (is_builtin_constant_p)
4541               {
4542                 /* The whole point of __builtin_constant_p is to allow
4543                    non-constant expressions to appear as arguments.  */
4544                 saved_integral_constant_expression_p
4545                   = parser->integral_constant_expression_p;
4546                 saved_non_integral_constant_expression_p
4547                   = parser->non_integral_constant_expression_p;
4548                 parser->integral_constant_expression_p = false;
4549               }
4550             args = (cp_parser_parenthesized_expression_list
4551                     (parser, /*is_attribute_list=*/false,
4552                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4553                      /*non_constant_p=*/NULL));
4554             if (is_builtin_constant_p)
4555               {
4556                 parser->integral_constant_expression_p
4557                   = saved_integral_constant_expression_p;
4558                 parser->non_integral_constant_expression_p
4559                   = saved_non_integral_constant_expression_p;
4560               }
4561
4562             if (args == error_mark_node)
4563               {
4564                 postfix_expression = error_mark_node;
4565                 break;
4566               }
4567
4568             /* Function calls are not permitted in
4569                constant-expressions.  */
4570             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4571                 && cp_parser_non_integral_constant_expression (parser,
4572                                                                "a function call"))
4573               {
4574                 postfix_expression = error_mark_node;
4575                 break;
4576               }
4577
4578             koenig_p = false;
4579             if (idk == CP_ID_KIND_UNQUALIFIED)
4580               {
4581                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4582                   {
4583                     if (args)
4584                       {
4585                         koenig_p = true;
4586                         postfix_expression
4587                           = perform_koenig_lookup (postfix_expression, args);
4588                       }
4589                     else
4590                       postfix_expression
4591                         = unqualified_fn_lookup_error (postfix_expression);
4592                   }
4593                 /* We do not perform argument-dependent lookup if
4594                    normal lookup finds a non-function, in accordance
4595                    with the expected resolution of DR 218.  */
4596                 else if (args && is_overloaded_fn (postfix_expression))
4597                   {
4598                     tree fn = get_first_fn (postfix_expression);
4599
4600                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4601                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4602
4603                     /* Only do argument dependent lookup if regular
4604                        lookup does not find a set of member functions.
4605                        [basic.lookup.koenig]/2a  */
4606                     if (!DECL_FUNCTION_MEMBER_P (fn))
4607                       {
4608                         koenig_p = true;
4609                         postfix_expression
4610                           = perform_koenig_lookup (postfix_expression, args);
4611                       }
4612                   }
4613               }
4614
4615             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4616               {
4617                 tree instance = TREE_OPERAND (postfix_expression, 0);
4618                 tree fn = TREE_OPERAND (postfix_expression, 1);
4619
4620                 if (processing_template_decl
4621                     && (type_dependent_expression_p (instance)
4622                         || (!BASELINK_P (fn)
4623                             && TREE_CODE (fn) != FIELD_DECL)
4624                         || type_dependent_expression_p (fn)
4625                         || any_type_dependent_arguments_p (args)))
4626                   {
4627                     postfix_expression
4628                       = build_nt_call_list (postfix_expression, args);
4629                     break;
4630                   }
4631
4632                 if (BASELINK_P (fn))
4633                   postfix_expression
4634                     = (build_new_method_call
4635                        (instance, fn, args, NULL_TREE,
4636                         (idk == CP_ID_KIND_QUALIFIED
4637                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4638                         /*fn_p=*/NULL));
4639                 else
4640                   postfix_expression
4641                     = finish_call_expr (postfix_expression, args,
4642                                         /*disallow_virtual=*/false,
4643                                         /*koenig_p=*/false);
4644               }
4645             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4646                      || TREE_CODE (postfix_expression) == MEMBER_REF
4647                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4648               postfix_expression = (build_offset_ref_call_from_tree
4649                                     (postfix_expression, args));
4650             else if (idk == CP_ID_KIND_QUALIFIED)
4651               /* A call to a static class member, or a namespace-scope
4652                  function.  */
4653               postfix_expression
4654                 = finish_call_expr (postfix_expression, args,
4655                                     /*disallow_virtual=*/true,
4656                                     koenig_p);
4657             else
4658               /* All other function calls.  */
4659               postfix_expression
4660                 = finish_call_expr (postfix_expression, args,
4661                                     /*disallow_virtual=*/false,
4662                                     koenig_p);
4663
4664             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4665             idk = CP_ID_KIND_NONE;
4666           }
4667           break;
4668
4669         case CPP_DOT:
4670         case CPP_DEREF:
4671           /* postfix-expression . template [opt] id-expression
4672              postfix-expression . pseudo-destructor-name
4673              postfix-expression -> template [opt] id-expression
4674              postfix-expression -> pseudo-destructor-name */
4675
4676           /* Consume the `.' or `->' operator.  */
4677           cp_lexer_consume_token (parser->lexer);
4678
4679           postfix_expression
4680             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4681                                                       postfix_expression,
4682                                                       false, &idk);
4683
4684           is_member_access = true;
4685           break;
4686
4687         case CPP_PLUS_PLUS:
4688           /* postfix-expression ++  */
4689           /* Consume the `++' token.  */
4690           cp_lexer_consume_token (parser->lexer);
4691           /* Generate a representation for the complete expression.  */
4692           postfix_expression
4693             = finish_increment_expr (postfix_expression,
4694                                      POSTINCREMENT_EXPR);
4695           /* Increments may not appear in constant-expressions.  */
4696           if (cp_parser_non_integral_constant_expression (parser,
4697                                                           "an increment"))
4698             postfix_expression = error_mark_node;
4699           idk = CP_ID_KIND_NONE;
4700           is_member_access = false;
4701           break;
4702
4703         case CPP_MINUS_MINUS:
4704           /* postfix-expression -- */
4705           /* Consume the `--' token.  */
4706           cp_lexer_consume_token (parser->lexer);
4707           /* Generate a representation for the complete expression.  */
4708           postfix_expression
4709             = finish_increment_expr (postfix_expression,
4710                                      POSTDECREMENT_EXPR);
4711           /* Decrements may not appear in constant-expressions.  */
4712           if (cp_parser_non_integral_constant_expression (parser,
4713                                                           "a decrement"))
4714             postfix_expression = error_mark_node;
4715           idk = CP_ID_KIND_NONE;
4716           is_member_access = false;
4717           break;
4718
4719         default:
4720           if (member_access_only_p)
4721             return is_member_access? postfix_expression : error_mark_node;
4722           else
4723             return postfix_expression;
4724         }
4725     }
4726
4727   /* We should never get here.  */
4728   gcc_unreachable ();
4729   return error_mark_node;
4730 }
4731
4732 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4733    by cp_parser_builtin_offsetof.  We're looking for
4734
4735      postfix-expression [ expression ]
4736
4737    FOR_OFFSETOF is set if we're being called in that context, which
4738    changes how we deal with integer constant expressions.  */
4739
4740 static tree
4741 cp_parser_postfix_open_square_expression (cp_parser *parser,
4742                                           tree postfix_expression,
4743                                           bool for_offsetof)
4744 {
4745   tree index;
4746
4747   /* Consume the `[' token.  */
4748   cp_lexer_consume_token (parser->lexer);
4749
4750   /* Parse the index expression.  */
4751   /* ??? For offsetof, there is a question of what to allow here.  If
4752      offsetof is not being used in an integral constant expression context,
4753      then we *could* get the right answer by computing the value at runtime.
4754      If we are in an integral constant expression context, then we might
4755      could accept any constant expression; hard to say without analysis.
4756      Rather than open the barn door too wide right away, allow only integer
4757      constant expressions here.  */
4758   if (for_offsetof)
4759     index = cp_parser_constant_expression (parser, false, NULL);
4760   else
4761     index = cp_parser_expression (parser, /*cast_p=*/false);
4762
4763   /* Look for the closing `]'.  */
4764   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4765
4766   /* Build the ARRAY_REF.  */
4767   postfix_expression = grok_array_decl (postfix_expression, index);
4768
4769   /* When not doing offsetof, array references are not permitted in
4770      constant-expressions.  */
4771   if (!for_offsetof
4772       && (cp_parser_non_integral_constant_expression
4773           (parser, "an array reference")))
4774     postfix_expression = error_mark_node;
4775
4776   return postfix_expression;
4777 }
4778
4779 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4780    by cp_parser_builtin_offsetof.  We're looking for
4781
4782      postfix-expression . template [opt] id-expression
4783      postfix-expression . pseudo-destructor-name
4784      postfix-expression -> template [opt] id-expression
4785      postfix-expression -> pseudo-destructor-name
4786
4787    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4788    limits what of the above we'll actually accept, but nevermind.
4789    TOKEN_TYPE is the "." or "->" token, which will already have been
4790    removed from the stream.  */
4791
4792 static tree
4793 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4794                                         enum cpp_ttype token_type,
4795                                         tree postfix_expression,
4796                                         bool for_offsetof, cp_id_kind *idk)
4797 {
4798   tree name;
4799   bool dependent_p;
4800   bool pseudo_destructor_p;
4801   tree scope = NULL_TREE;
4802
4803   /* If this is a `->' operator, dereference the pointer.  */
4804   if (token_type == CPP_DEREF)
4805     postfix_expression = build_x_arrow (postfix_expression);
4806   /* Check to see whether or not the expression is type-dependent.  */
4807   dependent_p = type_dependent_expression_p (postfix_expression);
4808   /* The identifier following the `->' or `.' is not qualified.  */
4809   parser->scope = NULL_TREE;
4810   parser->qualifying_scope = NULL_TREE;
4811   parser->object_scope = NULL_TREE;
4812   *idk = CP_ID_KIND_NONE;
4813   /* Enter the scope corresponding to the type of the object
4814      given by the POSTFIX_EXPRESSION.  */
4815   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4816     {
4817       scope = TREE_TYPE (postfix_expression);
4818       /* According to the standard, no expression should ever have
4819          reference type.  Unfortunately, we do not currently match
4820          the standard in this respect in that our internal representation
4821          of an expression may have reference type even when the standard
4822          says it does not.  Therefore, we have to manually obtain the
4823          underlying type here.  */
4824       scope = non_reference (scope);
4825       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4826       if (scope == unknown_type_node)
4827         {
4828           error ("%qE does not have class type", postfix_expression);
4829           scope = NULL_TREE;
4830         }
4831       else
4832         scope = complete_type_or_else (scope, NULL_TREE);
4833       /* Let the name lookup machinery know that we are processing a
4834          class member access expression.  */
4835       parser->context->object_type = scope;
4836       /* If something went wrong, we want to be able to discern that case,
4837          as opposed to the case where there was no SCOPE due to the type
4838          of expression being dependent.  */
4839       if (!scope)
4840         scope = error_mark_node;
4841       /* If the SCOPE was erroneous, make the various semantic analysis
4842          functions exit quickly -- and without issuing additional error
4843          messages.  */
4844       if (scope == error_mark_node)
4845         postfix_expression = error_mark_node;
4846     }
4847
4848   /* Assume this expression is not a pseudo-destructor access.  */
4849   pseudo_destructor_p = false;
4850
4851   /* If the SCOPE is a scalar type, then, if this is a valid program,
4852      we must be looking at a pseudo-destructor-name.  */
4853   if (scope && SCALAR_TYPE_P (scope))
4854     {
4855       tree s;
4856       tree type;
4857
4858       cp_parser_parse_tentatively (parser);
4859       /* Parse the pseudo-destructor-name.  */
4860       s = NULL_TREE;
4861       cp_parser_pseudo_destructor_name (parser, &s, &type);
4862       if (cp_parser_parse_definitely (parser))
4863         {
4864           pseudo_destructor_p = true;
4865           postfix_expression
4866             = finish_pseudo_destructor_expr (postfix_expression,
4867                                              s, TREE_TYPE (type));
4868         }
4869     }
4870
4871   if (!pseudo_destructor_p)
4872     {
4873       /* If the SCOPE is not a scalar type, we are looking at an
4874          ordinary class member access expression, rather than a
4875          pseudo-destructor-name.  */
4876       bool template_p;
4877       /* Parse the id-expression.  */
4878       name = (cp_parser_id_expression
4879               (parser,
4880                cp_parser_optional_template_keyword (parser),
4881                /*check_dependency_p=*/true,
4882                &template_p,
4883                /*declarator_p=*/false,
4884                /*optional_p=*/false));
4885       /* In general, build a SCOPE_REF if the member name is qualified.
4886          However, if the name was not dependent and has already been
4887          resolved; there is no need to build the SCOPE_REF.  For example;
4888
4889              struct X { void f(); };
4890              template <typename T> void f(T* t) { t->X::f(); }
4891
4892          Even though "t" is dependent, "X::f" is not and has been resolved
4893          to a BASELINK; there is no need to include scope information.  */
4894
4895       /* But we do need to remember that there was an explicit scope for
4896          virtual function calls.  */
4897       if (parser->scope)
4898         *idk = CP_ID_KIND_QUALIFIED;
4899
4900       /* If the name is a template-id that names a type, we will get a
4901          TYPE_DECL here.  That is invalid code.  */
4902       if (TREE_CODE (name) == TYPE_DECL)
4903         {
4904           error ("invalid use of %qD", name);
4905           postfix_expression = error_mark_node;
4906         }
4907       else
4908         {
4909           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4910             {
4911               name = build_qualified_name (/*type=*/NULL_TREE,
4912                                            parser->scope,
4913                                            name,
4914                                            template_p);
4915               parser->scope = NULL_TREE;
4916               parser->qualifying_scope = NULL_TREE;
4917               parser->object_scope = NULL_TREE;
4918             }
4919           if (scope && name && BASELINK_P (name))
4920             adjust_result_of_qualified_name_lookup
4921               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4922           postfix_expression
4923             = finish_class_member_access_expr (postfix_expression, name,
4924                                                template_p);
4925         }
4926     }
4927
4928   /* We no longer need to look up names in the scope of the object on
4929      the left-hand side of the `.' or `->' operator.  */
4930   parser->context->object_type = NULL_TREE;
4931
4932   /* Outside of offsetof, these operators may not appear in
4933      constant-expressions.  */
4934   if (!for_offsetof
4935       && (cp_parser_non_integral_constant_expression
4936           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4937     postfix_expression = error_mark_node;
4938
4939   return postfix_expression;
4940 }
4941
4942 /* Parse a parenthesized expression-list.
4943
4944    expression-list:
4945      assignment-expression
4946      expression-list, assignment-expression
4947
4948    attribute-list:
4949      expression-list
4950      identifier
4951      identifier, expression-list
4952
4953    CAST_P is true if this expression is the target of a cast.
4954
4955    ALLOW_EXPANSION_P is true if this expression allows expansion of an
4956    argument pack.
4957
4958    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4959    representation of an assignment-expression.  Note that a TREE_LIST
4960    is returned even if there is only a single expression in the list.
4961    error_mark_node is returned if the ( and or ) are
4962    missing. NULL_TREE is returned on no expressions. The parentheses
4963    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4964    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4965    indicates whether or not all of the expressions in the list were
4966    constant.  */
4967
4968 static tree
4969 cp_parser_parenthesized_expression_list (cp_parser* parser,
4970                                          bool is_attribute_list,
4971                                          bool cast_p,
4972                                          bool allow_expansion_p,
4973                                          bool *non_constant_p)
4974 {
4975   tree expression_list = NULL_TREE;
4976   bool fold_expr_p = is_attribute_list;
4977   tree identifier = NULL_TREE;
4978
4979   /* Assume all the expressions will be constant.  */
4980   if (non_constant_p)
4981     *non_constant_p = false;
4982
4983   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4984     return error_mark_node;
4985
4986   /* Consume expressions until there are no more.  */
4987   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4988     while (true)
4989       {
4990         tree expr;
4991
4992         /* At the beginning of attribute lists, check to see if the
4993            next token is an identifier.  */
4994         if (is_attribute_list
4995             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4996           {
4997             cp_token *token;
4998
4999             /* Consume the identifier.  */
5000             token = cp_lexer_consume_token (parser->lexer);
5001             /* Save the identifier.  */
5002             identifier = token->u.value;
5003           }
5004         else
5005           {
5006             /* Parse the next assignment-expression.  */
5007             if (non_constant_p)
5008               {
5009                 bool expr_non_constant_p;
5010                 expr = (cp_parser_constant_expression
5011                         (parser, /*allow_non_constant_p=*/true,
5012                          &expr_non_constant_p));
5013                 if (expr_non_constant_p)
5014                   *non_constant_p = true;
5015               }
5016             else
5017               expr = cp_parser_assignment_expression (parser, cast_p);
5018
5019             if (fold_expr_p)
5020               expr = fold_non_dependent_expr (expr);
5021
5022             /* If we have an ellipsis, then this is an expression
5023                expansion.  */
5024             if (allow_expansion_p
5025                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5026               {
5027                 /* Consume the `...'.  */
5028                 cp_lexer_consume_token (parser->lexer);
5029
5030                 /* Build the argument pack.  */
5031                 expr = make_pack_expansion (expr);
5032               }
5033
5034              /* Add it to the list.  We add error_mark_node
5035                 expressions to the list, so that we can still tell if
5036                 the correct form for a parenthesized expression-list
5037                 is found. That gives better errors.  */
5038             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5039
5040             if (expr == error_mark_node)
5041               goto skip_comma;
5042           }
5043
5044         /* After the first item, attribute lists look the same as
5045            expression lists.  */
5046         is_attribute_list = false;
5047
5048       get_comma:;
5049         /* If the next token isn't a `,', then we are done.  */
5050         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5051           break;
5052
5053         /* Otherwise, consume the `,' and keep going.  */
5054         cp_lexer_consume_token (parser->lexer);
5055       }
5056
5057   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5058     {
5059       int ending;
5060
5061     skip_comma:;
5062       /* We try and resync to an unnested comma, as that will give the
5063          user better diagnostics.  */
5064       ending = cp_parser_skip_to_closing_parenthesis (parser,
5065                                                       /*recovering=*/true,
5066                                                       /*or_comma=*/true,
5067                                                       /*consume_paren=*/true);
5068       if (ending < 0)
5069         goto get_comma;
5070       if (!ending)
5071         return error_mark_node;
5072     }
5073
5074   /* We built up the list in reverse order so we must reverse it now.  */
5075   expression_list = nreverse (expression_list);
5076   if (identifier)
5077     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5078
5079   return expression_list;
5080 }
5081
5082 /* Parse a pseudo-destructor-name.
5083
5084    pseudo-destructor-name:
5085      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5086      :: [opt] nested-name-specifier template template-id :: ~ type-name
5087      :: [opt] nested-name-specifier [opt] ~ type-name
5088
5089    If either of the first two productions is used, sets *SCOPE to the
5090    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5091    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5092    or ERROR_MARK_NODE if the parse fails.  */
5093
5094 static void
5095 cp_parser_pseudo_destructor_name (cp_parser* parser,
5096                                   tree* scope,
5097                                   tree* type)
5098 {
5099   bool nested_name_specifier_p;
5100
5101   /* Assume that things will not work out.  */
5102   *type = error_mark_node;
5103
5104   /* Look for the optional `::' operator.  */
5105   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5106   /* Look for the optional nested-name-specifier.  */
5107   nested_name_specifier_p
5108     = (cp_parser_nested_name_specifier_opt (parser,
5109                                             /*typename_keyword_p=*/false,
5110                                             /*check_dependency_p=*/true,
5111                                             /*type_p=*/false,
5112                                             /*is_declaration=*/true)
5113        != NULL_TREE);
5114   /* Now, if we saw a nested-name-specifier, we might be doing the
5115      second production.  */
5116   if (nested_name_specifier_p
5117       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5118     {
5119       /* Consume the `template' keyword.  */
5120       cp_lexer_consume_token (parser->lexer);
5121       /* Parse the template-id.  */
5122       cp_parser_template_id (parser,
5123                              /*template_keyword_p=*/true,
5124                              /*check_dependency_p=*/false,
5125                              /*is_declaration=*/true);
5126       /* Look for the `::' token.  */
5127       cp_parser_require (parser, CPP_SCOPE, "`::'");
5128     }
5129   /* If the next token is not a `~', then there might be some
5130      additional qualification.  */
5131   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5132     {
5133       /* Look for the type-name.  */
5134       *scope = TREE_TYPE (cp_parser_type_name (parser));
5135
5136       if (*scope == error_mark_node)
5137         return;
5138
5139       /* If we don't have ::~, then something has gone wrong.  Since
5140          the only caller of this function is looking for something
5141          after `.' or `->' after a scalar type, most likely the
5142          program is trying to get a member of a non-aggregate
5143          type.  */
5144       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5145           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5146         {
5147           cp_parser_error (parser, "request for member of non-aggregate type");
5148           return;
5149         }
5150
5151       /* Look for the `::' token.  */
5152       cp_parser_require (parser, CPP_SCOPE, "`::'");
5153     }
5154   else
5155     *scope = NULL_TREE;
5156
5157   /* Look for the `~'.  */
5158   cp_parser_require (parser, CPP_COMPL, "`~'");
5159   /* Look for the type-name again.  We are not responsible for
5160      checking that it matches the first type-name.  */
5161   *type = cp_parser_type_name (parser);
5162 }
5163
5164 /* Parse a unary-expression.
5165
5166    unary-expression:
5167      postfix-expression
5168      ++ cast-expression
5169      -- cast-expression
5170      unary-operator cast-expression
5171      sizeof unary-expression
5172      sizeof ( type-id )
5173      new-expression
5174      delete-expression
5175
5176    GNU Extensions:
5177
5178    unary-expression:
5179      __extension__ cast-expression
5180      __alignof__ unary-expression
5181      __alignof__ ( type-id )
5182      __real__ cast-expression
5183      __imag__ cast-expression
5184      && identifier
5185
5186    ADDRESS_P is true iff the unary-expression is appearing as the
5187    operand of the `&' operator.   CAST_P is true if this expression is
5188    the target of a cast.
5189
5190    Returns a representation of the expression.  */
5191
5192 static tree
5193 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5194 {
5195   cp_token *token;
5196   enum tree_code unary_operator;
5197
5198   /* Peek at the next token.  */
5199   token = cp_lexer_peek_token (parser->lexer);
5200   /* Some keywords give away the kind of expression.  */
5201   if (token->type == CPP_KEYWORD)
5202     {
5203       enum rid keyword = token->keyword;
5204
5205       switch (keyword)
5206         {
5207         case RID_ALIGNOF:
5208         case RID_SIZEOF:
5209           {
5210             tree operand;
5211             enum tree_code op;
5212
5213             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5214             /* Consume the token.  */
5215             cp_lexer_consume_token (parser->lexer);
5216             /* Parse the operand.  */
5217             operand = cp_parser_sizeof_operand (parser, keyword);
5218
5219             if (TYPE_P (operand))
5220               return cxx_sizeof_or_alignof_type (operand, op, true);
5221             else
5222               return cxx_sizeof_or_alignof_expr (operand, op);
5223           }
5224
5225         case RID_NEW:
5226           return cp_parser_new_expression (parser);
5227
5228         case RID_DELETE:
5229           return cp_parser_delete_expression (parser);
5230
5231         case RID_EXTENSION:
5232           {
5233             /* The saved value of the PEDANTIC flag.  */
5234             int saved_pedantic;
5235             tree expr;
5236
5237             /* Save away the PEDANTIC flag.  */
5238             cp_parser_extension_opt (parser, &saved_pedantic);
5239             /* Parse the cast-expression.  */
5240             expr = cp_parser_simple_cast_expression (parser);
5241             /* Restore the PEDANTIC flag.  */
5242             pedantic = saved_pedantic;
5243
5244             return expr;
5245           }
5246
5247         case RID_REALPART:
5248         case RID_IMAGPART:
5249           {
5250             tree expression;
5251
5252             /* Consume the `__real__' or `__imag__' token.  */
5253             cp_lexer_consume_token (parser->lexer);
5254             /* Parse the cast-expression.  */
5255             expression = cp_parser_simple_cast_expression (parser);
5256             /* Create the complete representation.  */
5257             return build_x_unary_op ((keyword == RID_REALPART
5258                                       ? REALPART_EXPR : IMAGPART_EXPR),
5259                                      expression);
5260           }
5261           break;
5262
5263         default:
5264           break;
5265         }
5266     }
5267
5268   /* Look for the `:: new' and `:: delete', which also signal the
5269      beginning of a new-expression, or delete-expression,
5270      respectively.  If the next token is `::', then it might be one of
5271      these.  */
5272   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5273     {
5274       enum rid keyword;
5275
5276       /* See if the token after the `::' is one of the keywords in
5277          which we're interested.  */
5278       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5279       /* If it's `new', we have a new-expression.  */
5280       if (keyword == RID_NEW)
5281         return cp_parser_new_expression (parser);
5282       /* Similarly, for `delete'.  */
5283       else if (keyword == RID_DELETE)
5284         return cp_parser_delete_expression (parser);
5285     }
5286
5287   /* Look for a unary operator.  */
5288   unary_operator = cp_parser_unary_operator (token);
5289   /* The `++' and `--' operators can be handled similarly, even though
5290      they are not technically unary-operators in the grammar.  */
5291   if (unary_operator == ERROR_MARK)
5292     {
5293       if (token->type == CPP_PLUS_PLUS)
5294         unary_operator = PREINCREMENT_EXPR;
5295       else if (token->type == CPP_MINUS_MINUS)
5296         unary_operator = PREDECREMENT_EXPR;
5297       /* Handle the GNU address-of-label extension.  */
5298       else if (cp_parser_allow_gnu_extensions_p (parser)
5299                && token->type == CPP_AND_AND)
5300         {
5301           tree identifier;
5302
5303           /* Consume the '&&' token.  */
5304           cp_lexer_consume_token (parser->lexer);
5305           /* Look for the identifier.  */
5306           identifier = cp_parser_identifier (parser);
5307           /* Create an expression representing the address.  */
5308           return finish_label_address_expr (identifier);
5309         }
5310     }
5311   if (unary_operator != ERROR_MARK)
5312     {
5313       tree cast_expression;
5314       tree expression = error_mark_node;
5315       const char *non_constant_p = NULL;
5316
5317       /* Consume the operator token.  */
5318       token = cp_lexer_consume_token (parser->lexer);
5319       /* Parse the cast-expression.  */
5320       cast_expression
5321         = cp_parser_cast_expression (parser,
5322                                      unary_operator == ADDR_EXPR,
5323                                      /*cast_p=*/false);
5324       /* Now, build an appropriate representation.  */
5325       switch (unary_operator)
5326         {
5327         case INDIRECT_REF:
5328           non_constant_p = "`*'";
5329           expression = build_x_indirect_ref (cast_expression, "unary *");
5330           break;
5331
5332         case ADDR_EXPR:
5333           non_constant_p = "`&'";
5334           /* Fall through.  */
5335         case BIT_NOT_EXPR:
5336           expression = build_x_unary_op (unary_operator, cast_expression);
5337           break;
5338
5339         case PREINCREMENT_EXPR:
5340         case PREDECREMENT_EXPR:
5341           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5342                             ? "`++'" : "`--'");
5343           /* Fall through.  */
5344         case UNARY_PLUS_EXPR:
5345         case NEGATE_EXPR:
5346         case TRUTH_NOT_EXPR:
5347           expression = finish_unary_op_expr (unary_operator, cast_expression);
5348           break;
5349
5350         default:
5351           gcc_unreachable ();
5352         }
5353
5354       if (non_constant_p
5355           && cp_parser_non_integral_constant_expression (parser,
5356                                                          non_constant_p))
5357         expression = error_mark_node;
5358
5359       return expression;
5360     }
5361
5362   return cp_parser_postfix_expression (parser, address_p, cast_p,
5363                                        /*member_access_only_p=*/false);
5364 }
5365
5366 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5367    unary-operator, the corresponding tree code is returned.  */
5368
5369 static enum tree_code
5370 cp_parser_unary_operator (cp_token* token)
5371 {
5372   switch (token->type)
5373     {
5374     case CPP_MULT:
5375       return INDIRECT_REF;
5376
5377     case CPP_AND:
5378       return ADDR_EXPR;
5379
5380     case CPP_PLUS:
5381       return UNARY_PLUS_EXPR;
5382
5383     case CPP_MINUS:
5384       return NEGATE_EXPR;
5385
5386     case CPP_NOT:
5387       return TRUTH_NOT_EXPR;
5388
5389     case CPP_COMPL:
5390       return BIT_NOT_EXPR;
5391
5392     default:
5393       return ERROR_MARK;
5394     }
5395 }
5396
5397 /* Parse a new-expression.
5398
5399    new-expression:
5400      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5401      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5402
5403    Returns a representation of the expression.  */
5404
5405 static tree
5406 cp_parser_new_expression (cp_parser* parser)
5407 {
5408   bool global_scope_p;
5409   tree placement;
5410   tree type;
5411   tree initializer;
5412   tree nelts;
5413
5414   /* Look for the optional `::' operator.  */
5415   global_scope_p
5416     = (cp_parser_global_scope_opt (parser,
5417                                    /*current_scope_valid_p=*/false)
5418        != NULL_TREE);
5419   /* Look for the `new' operator.  */
5420   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5421   /* There's no easy way to tell a new-placement from the
5422      `( type-id )' construct.  */
5423   cp_parser_parse_tentatively (parser);
5424   /* Look for a new-placement.  */
5425   placement = cp_parser_new_placement (parser);
5426   /* If that didn't work out, there's no new-placement.  */
5427   if (!cp_parser_parse_definitely (parser))
5428     placement = NULL_TREE;
5429
5430   /* If the next token is a `(', then we have a parenthesized
5431      type-id.  */
5432   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5433     {
5434       /* Consume the `('.  */
5435       cp_lexer_consume_token (parser->lexer);
5436       /* Parse the type-id.  */
5437       type = cp_parser_type_id (parser);
5438       /* Look for the closing `)'.  */
5439       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5440       /* There should not be a direct-new-declarator in this production,
5441          but GCC used to allowed this, so we check and emit a sensible error
5442          message for this case.  */
5443       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5444         {
5445           error ("array bound forbidden after parenthesized type-id");
5446           inform ("try removing the parentheses around the type-id");
5447           cp_parser_direct_new_declarator (parser);
5448         }
5449       nelts = NULL_TREE;
5450     }
5451   /* Otherwise, there must be a new-type-id.  */
5452   else
5453     type = cp_parser_new_type_id (parser, &nelts);
5454
5455   /* If the next token is a `(', then we have a new-initializer.  */
5456   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5457     initializer = cp_parser_new_initializer (parser);
5458   else
5459     initializer = NULL_TREE;
5460
5461   /* A new-expression may not appear in an integral constant
5462      expression.  */
5463   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5464     return error_mark_node;
5465
5466   /* Create a representation of the new-expression.  */
5467   return build_new (placement, type, nelts, initializer, global_scope_p);
5468 }
5469
5470 /* Parse a new-placement.
5471
5472    new-placement:
5473      ( expression-list )
5474
5475    Returns the same representation as for an expression-list.  */
5476
5477 static tree
5478 cp_parser_new_placement (cp_parser* parser)
5479 {
5480   tree expression_list;
5481
5482   /* Parse the expression-list.  */
5483   expression_list = (cp_parser_parenthesized_expression_list
5484                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5485                       /*non_constant_p=*/NULL));
5486
5487   return expression_list;
5488 }
5489
5490 /* Parse a new-type-id.
5491
5492    new-type-id:
5493      type-specifier-seq new-declarator [opt]
5494
5495    Returns the TYPE allocated.  If the new-type-id indicates an array
5496    type, *NELTS is set to the number of elements in the last array
5497    bound; the TYPE will not include the last array bound.  */
5498
5499 static tree
5500 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5501 {
5502   cp_decl_specifier_seq type_specifier_seq;
5503   cp_declarator *new_declarator;
5504   cp_declarator *declarator;
5505   cp_declarator *outer_declarator;
5506   const char *saved_message;
5507   tree type;
5508
5509   /* The type-specifier sequence must not contain type definitions.
5510      (It cannot contain declarations of new types either, but if they
5511      are not definitions we will catch that because they are not
5512      complete.)  */
5513   saved_message = parser->type_definition_forbidden_message;
5514   parser->type_definition_forbidden_message
5515     = "types may not be defined in a new-type-id";
5516   /* Parse the type-specifier-seq.  */
5517   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5518                                 &type_specifier_seq);
5519   /* Restore the old message.  */
5520   parser->type_definition_forbidden_message = saved_message;
5521   /* Parse the new-declarator.  */
5522   new_declarator = cp_parser_new_declarator_opt (parser);
5523
5524   /* Determine the number of elements in the last array dimension, if
5525      any.  */
5526   *nelts = NULL_TREE;
5527   /* Skip down to the last array dimension.  */
5528   declarator = new_declarator;
5529   outer_declarator = NULL;
5530   while (declarator && (declarator->kind == cdk_pointer
5531                         || declarator->kind == cdk_ptrmem))
5532     {
5533       outer_declarator = declarator;
5534       declarator = declarator->declarator;
5535     }
5536   while (declarator
5537          && declarator->kind == cdk_array
5538          && declarator->declarator
5539          && declarator->declarator->kind == cdk_array)
5540     {
5541       outer_declarator = declarator;
5542       declarator = declarator->declarator;
5543     }
5544
5545   if (declarator && declarator->kind == cdk_array)
5546     {
5547       *nelts = declarator->u.array.bounds;
5548       if (*nelts == error_mark_node)
5549         *nelts = integer_one_node;
5550
5551       if (outer_declarator)
5552         outer_declarator->declarator = declarator->declarator;
5553       else
5554         new_declarator = NULL;
5555     }
5556
5557   type = groktypename (&type_specifier_seq, new_declarator);
5558   return type;
5559 }
5560
5561 /* Parse an (optional) new-declarator.
5562
5563    new-declarator:
5564      ptr-operator new-declarator [opt]
5565      direct-new-declarator
5566
5567    Returns the declarator.  */
5568
5569 static cp_declarator *
5570 cp_parser_new_declarator_opt (cp_parser* parser)
5571 {
5572   enum tree_code code;
5573   tree type;
5574   cp_cv_quals cv_quals;
5575
5576   /* We don't know if there's a ptr-operator next, or not.  */
5577   cp_parser_parse_tentatively (parser);
5578   /* Look for a ptr-operator.  */
5579   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5580   /* If that worked, look for more new-declarators.  */
5581   if (cp_parser_parse_definitely (parser))
5582     {
5583       cp_declarator *declarator;
5584
5585       /* Parse another optional declarator.  */
5586       declarator = cp_parser_new_declarator_opt (parser);
5587
5588       return cp_parser_make_indirect_declarator
5589         (code, type, cv_quals, declarator);
5590     }
5591
5592   /* If the next token is a `[', there is a direct-new-declarator.  */
5593   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5594     return cp_parser_direct_new_declarator (parser);
5595
5596   return NULL;
5597 }
5598
5599 /* Parse a direct-new-declarator.
5600
5601    direct-new-declarator:
5602      [ expression ]
5603      direct-new-declarator [constant-expression]
5604
5605    */
5606
5607 static cp_declarator *
5608 cp_parser_direct_new_declarator (cp_parser* parser)
5609 {
5610   cp_declarator *declarator = NULL;
5611
5612   while (true)
5613     {
5614       tree expression;
5615
5616       /* Look for the opening `['.  */
5617       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5618       /* The first expression is not required to be constant.  */
5619       if (!declarator)
5620         {
5621           expression = cp_parser_expression (parser, /*cast_p=*/false);
5622           /* The standard requires that the expression have integral
5623              type.  DR 74 adds enumeration types.  We believe that the
5624              real intent is that these expressions be handled like the
5625              expression in a `switch' condition, which also allows
5626              classes with a single conversion to integral or
5627              enumeration type.  */
5628           if (!processing_template_decl)
5629             {
5630               expression
5631                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5632                                               expression,
5633                                               /*complain=*/true);
5634               if (!expression)
5635                 {
5636                   error ("expression in new-declarator must have integral "
5637                          "or enumeration type");
5638                   expression = error_mark_node;
5639                 }
5640             }
5641         }
5642       /* But all the other expressions must be.  */
5643       else
5644         expression
5645           = cp_parser_constant_expression (parser,
5646                                            /*allow_non_constant=*/false,
5647                                            NULL);
5648       /* Look for the closing `]'.  */
5649       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5650
5651       /* Add this bound to the declarator.  */
5652       declarator = make_array_declarator (declarator, expression);
5653
5654       /* If the next token is not a `[', then there are no more
5655          bounds.  */
5656       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5657         break;
5658     }
5659
5660   return declarator;
5661 }
5662
5663 /* Parse a new-initializer.
5664
5665    new-initializer:
5666      ( expression-list [opt] )
5667
5668    Returns a representation of the expression-list.  If there is no
5669    expression-list, VOID_ZERO_NODE is returned.  */
5670
5671 static tree
5672 cp_parser_new_initializer (cp_parser* parser)
5673 {
5674   tree expression_list;
5675
5676   expression_list = (cp_parser_parenthesized_expression_list
5677                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5678                       /*non_constant_p=*/NULL));
5679   if (!expression_list)
5680     expression_list = void_zero_node;
5681
5682   return expression_list;
5683 }
5684
5685 /* Parse a delete-expression.
5686
5687    delete-expression:
5688      :: [opt] delete cast-expression
5689      :: [opt] delete [ ] cast-expression
5690
5691    Returns a representation of the expression.  */
5692
5693 static tree
5694 cp_parser_delete_expression (cp_parser* parser)
5695 {
5696   bool global_scope_p;
5697   bool array_p;
5698   tree expression;
5699
5700   /* Look for the optional `::' operator.  */
5701   global_scope_p
5702     = (cp_parser_global_scope_opt (parser,
5703                                    /*current_scope_valid_p=*/false)
5704        != NULL_TREE);
5705   /* Look for the `delete' keyword.  */
5706   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5707   /* See if the array syntax is in use.  */
5708   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5709     {
5710       /* Consume the `[' token.  */
5711       cp_lexer_consume_token (parser->lexer);
5712       /* Look for the `]' token.  */
5713       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5714       /* Remember that this is the `[]' construct.  */
5715       array_p = true;
5716     }
5717   else
5718     array_p = false;
5719
5720   /* Parse the cast-expression.  */
5721   expression = cp_parser_simple_cast_expression (parser);
5722
5723   /* A delete-expression may not appear in an integral constant
5724      expression.  */
5725   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5726     return error_mark_node;
5727
5728   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5729 }
5730
5731 /* Parse a cast-expression.
5732
5733    cast-expression:
5734      unary-expression
5735      ( type-id ) cast-expression
5736
5737    ADDRESS_P is true iff the unary-expression is appearing as the
5738    operand of the `&' operator.   CAST_P is true if this expression is
5739    the target of a cast.
5740
5741    Returns a representation of the expression.  */
5742
5743 static tree
5744 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5745 {
5746   /* If it's a `(', then we might be looking at a cast.  */
5747   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5748     {
5749       tree type = NULL_TREE;
5750       tree expr = NULL_TREE;
5751       bool compound_literal_p;
5752       const char *saved_message;
5753
5754       /* There's no way to know yet whether or not this is a cast.
5755          For example, `(int (3))' is a unary-expression, while `(int)
5756          3' is a cast.  So, we resort to parsing tentatively.  */
5757       cp_parser_parse_tentatively (parser);
5758       /* Types may not be defined in a cast.  */
5759       saved_message = parser->type_definition_forbidden_message;
5760       parser->type_definition_forbidden_message
5761         = "types may not be defined in casts";
5762       /* Consume the `('.  */
5763       cp_lexer_consume_token (parser->lexer);
5764       /* A very tricky bit is that `(struct S) { 3 }' is a
5765          compound-literal (which we permit in C++ as an extension).
5766          But, that construct is not a cast-expression -- it is a
5767          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5768          is legal; if the compound-literal were a cast-expression,
5769          you'd need an extra set of parentheses.)  But, if we parse
5770          the type-id, and it happens to be a class-specifier, then we
5771          will commit to the parse at that point, because we cannot
5772          undo the action that is done when creating a new class.  So,
5773          then we cannot back up and do a postfix-expression.
5774
5775          Therefore, we scan ahead to the closing `)', and check to see
5776          if the token after the `)' is a `{'.  If so, we are not
5777          looking at a cast-expression.
5778
5779          Save tokens so that we can put them back.  */
5780       cp_lexer_save_tokens (parser->lexer);
5781       /* Skip tokens until the next token is a closing parenthesis.
5782          If we find the closing `)', and the next token is a `{', then
5783          we are looking at a compound-literal.  */
5784       compound_literal_p
5785         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5786                                                   /*consume_paren=*/true)
5787            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5788       /* Roll back the tokens we skipped.  */
5789       cp_lexer_rollback_tokens (parser->lexer);
5790       /* If we were looking at a compound-literal, simulate an error
5791          so that the call to cp_parser_parse_definitely below will
5792          fail.  */
5793       if (compound_literal_p)
5794         cp_parser_simulate_error (parser);
5795       else
5796         {
5797           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5798           parser->in_type_id_in_expr_p = true;
5799           /* Look for the type-id.  */
5800           type = cp_parser_type_id (parser);
5801           /* Look for the closing `)'.  */
5802           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5803           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5804         }
5805
5806       /* Restore the saved message.  */
5807       parser->type_definition_forbidden_message = saved_message;
5808
5809       /* If ok so far, parse the dependent expression. We cannot be
5810          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5811          ctor of T, but looks like a cast to function returning T
5812          without a dependent expression.  */
5813       if (!cp_parser_error_occurred (parser))
5814         expr = cp_parser_cast_expression (parser,
5815                                           /*address_p=*/false,
5816                                           /*cast_p=*/true);
5817
5818       if (cp_parser_parse_definitely (parser))
5819         {
5820           /* Warn about old-style casts, if so requested.  */
5821           if (warn_old_style_cast
5822               && !in_system_header
5823               && !VOID_TYPE_P (type)
5824               && current_lang_name != lang_name_c)
5825             warning (OPT_Wold_style_cast, "use of old-style cast");
5826
5827           /* Only type conversions to integral or enumeration types
5828              can be used in constant-expressions.  */
5829           if (!cast_valid_in_integral_constant_expression_p (type)
5830               && (cp_parser_non_integral_constant_expression
5831                   (parser,
5832                    "a cast to a type other than an integral or "
5833                    "enumeration type")))
5834             return error_mark_node;
5835
5836           /* Perform the cast.  */
5837           expr = build_c_cast (type, expr);
5838           return expr;
5839         }
5840     }
5841
5842   /* If we get here, then it's not a cast, so it must be a
5843      unary-expression.  */
5844   return cp_parser_unary_expression (parser, address_p, cast_p);
5845 }
5846
5847 /* Parse a binary expression of the general form:
5848
5849    pm-expression:
5850      cast-expression
5851      pm-expression .* cast-expression
5852      pm-expression ->* cast-expression
5853
5854    multiplicative-expression:
5855      pm-expression
5856      multiplicative-expression * pm-expression
5857      multiplicative-expression / pm-expression
5858      multiplicative-expression % pm-expression
5859
5860    additive-expression:
5861      multiplicative-expression
5862      additive-expression + multiplicative-expression
5863      additive-expression - multiplicative-expression
5864
5865    shift-expression:
5866      additive-expression
5867      shift-expression << additive-expression
5868      shift-expression >> additive-expression
5869
5870    relational-expression:
5871      shift-expression
5872      relational-expression < shift-expression
5873      relational-expression > shift-expression
5874      relational-expression <= shift-expression
5875      relational-expression >= shift-expression
5876
5877   GNU Extension:
5878
5879    relational-expression:
5880      relational-expression <? shift-expression
5881      relational-expression >? shift-expression
5882
5883    equality-expression:
5884      relational-expression
5885      equality-expression == relational-expression
5886      equality-expression != relational-expression
5887
5888    and-expression:
5889      equality-expression
5890      and-expression & equality-expression
5891
5892    exclusive-or-expression:
5893      and-expression
5894      exclusive-or-expression ^ and-expression
5895
5896    inclusive-or-expression:
5897      exclusive-or-expression
5898      inclusive-or-expression | exclusive-or-expression
5899
5900    logical-and-expression:
5901      inclusive-or-expression
5902      logical-and-expression && inclusive-or-expression
5903
5904    logical-or-expression:
5905      logical-and-expression
5906      logical-or-expression || logical-and-expression
5907
5908    All these are implemented with a single function like:
5909
5910    binary-expression:
5911      simple-cast-expression
5912      binary-expression <token> binary-expression
5913
5914    CAST_P is true if this expression is the target of a cast.
5915
5916    The binops_by_token map is used to get the tree codes for each <token> type.
5917    binary-expressions are associated according to a precedence table.  */
5918
5919 #define TOKEN_PRECEDENCE(token)                              \
5920 (((token->type == CPP_GREATER                                \
5921    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5922   && !parser->greater_than_is_operator_p)                    \
5923  ? PREC_NOT_OPERATOR                                         \
5924  : binops_by_token[token->type].prec)
5925
5926 static tree
5927 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5928 {
5929   cp_parser_expression_stack stack;
5930   cp_parser_expression_stack_entry *sp = &stack[0];
5931   tree lhs, rhs;
5932   cp_token *token;
5933   enum tree_code tree_type, lhs_type, rhs_type;
5934   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5935   bool overloaded_p;
5936
5937   /* Parse the first expression.  */
5938   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5939   lhs_type = ERROR_MARK;
5940
5941   for (;;)
5942     {
5943       /* Get an operator token.  */
5944       token = cp_lexer_peek_token (parser->lexer);
5945
5946       if (warn_cxx0x_compat
5947           && token->type == CPP_RSHIFT
5948           && !parser->greater_than_is_operator_p)
5949         {
5950           warning (OPT_Wc__0x_compat, 
5951                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
5952                    &token->location);
5953           warning (OPT_Wc__0x_compat, 
5954                    "suggest parentheses around %<>>%> expression");
5955         }
5956
5957       new_prec = TOKEN_PRECEDENCE (token);
5958
5959       /* Popping an entry off the stack means we completed a subexpression:
5960          - either we found a token which is not an operator (`>' where it is not
5961            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5962            will happen repeatedly;
5963          - or, we found an operator which has lower priority.  This is the case
5964            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5965            parsing `3 * 4'.  */
5966       if (new_prec <= prec)
5967         {
5968           if (sp == stack)
5969             break;
5970           else
5971             goto pop;
5972         }
5973
5974      get_rhs:
5975       tree_type = binops_by_token[token->type].tree_type;
5976
5977       /* We used the operator token.  */
5978       cp_lexer_consume_token (parser->lexer);
5979
5980       /* Extract another operand.  It may be the RHS of this expression
5981          or the LHS of a new, higher priority expression.  */
5982       rhs = cp_parser_simple_cast_expression (parser);
5983       rhs_type = ERROR_MARK;
5984
5985       /* Get another operator token.  Look up its precedence to avoid
5986          building a useless (immediately popped) stack entry for common
5987          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5988       token = cp_lexer_peek_token (parser->lexer);
5989       lookahead_prec = TOKEN_PRECEDENCE (token);
5990       if (lookahead_prec > new_prec)
5991         {
5992           /* ... and prepare to parse the RHS of the new, higher priority
5993              expression.  Since precedence levels on the stack are
5994              monotonically increasing, we do not have to care about
5995              stack overflows.  */
5996           sp->prec = prec;
5997           sp->tree_type = tree_type;
5998           sp->lhs = lhs;
5999           sp->lhs_type = lhs_type;
6000           sp++;
6001           lhs = rhs;
6002           lhs_type = rhs_type;
6003           prec = new_prec;
6004           new_prec = lookahead_prec;
6005           goto get_rhs;
6006
6007          pop:
6008           /* If the stack is not empty, we have parsed into LHS the right side
6009              (`4' in the example above) of an expression we had suspended.
6010              We can use the information on the stack to recover the LHS (`3')
6011              from the stack together with the tree code (`MULT_EXPR'), and
6012              the precedence of the higher level subexpression
6013              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6014              which will be used to actually build the additive expression.  */
6015           --sp;
6016           prec = sp->prec;
6017           tree_type = sp->tree_type;
6018           rhs = lhs;
6019           rhs_type = lhs_type;
6020           lhs = sp->lhs;
6021           lhs_type = sp->lhs_type;
6022         }
6023
6024       overloaded_p = false;
6025       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6026                                &overloaded_p);
6027       lhs_type = tree_type;
6028
6029       /* If the binary operator required the use of an overloaded operator,
6030          then this expression cannot be an integral constant-expression.
6031          An overloaded operator can be used even if both operands are
6032          otherwise permissible in an integral constant-expression if at
6033          least one of the operands is of enumeration type.  */
6034
6035       if (overloaded_p
6036           && (cp_parser_non_integral_constant_expression
6037               (parser, "calls to overloaded operators")))
6038         return error_mark_node;
6039     }
6040
6041   return lhs;
6042 }
6043
6044
6045 /* Parse the `? expression : assignment-expression' part of a
6046    conditional-expression.  The LOGICAL_OR_EXPR is the
6047    logical-or-expression that started the conditional-expression.
6048    Returns a representation of the entire conditional-expression.
6049
6050    This routine is used by cp_parser_assignment_expression.
6051
6052      ? expression : assignment-expression
6053
6054    GNU Extensions:
6055
6056      ? : assignment-expression */
6057
6058 static tree
6059 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6060 {
6061   tree expr;
6062   tree assignment_expr;
6063
6064   /* Consume the `?' token.  */
6065   cp_lexer_consume_token (parser->lexer);
6066   if (cp_parser_allow_gnu_extensions_p (parser)
6067       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6068     /* Implicit true clause.  */
6069     expr = NULL_TREE;
6070   else
6071     /* Parse the expression.  */
6072     expr = cp_parser_expression (parser, /*cast_p=*/false);
6073
6074   /* The next token should be a `:'.  */
6075   cp_parser_require (parser, CPP_COLON, "`:'");
6076   /* Parse the assignment-expression.  */
6077   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6078
6079   /* Build the conditional-expression.  */
6080   return build_x_conditional_expr (logical_or_expr,
6081                                    expr,
6082                                    assignment_expr);
6083 }
6084
6085 /* Parse an assignment-expression.
6086
6087    assignment-expression:
6088      conditional-expression
6089      logical-or-expression assignment-operator assignment_expression
6090      throw-expression
6091
6092    CAST_P is true if this expression is the target of a cast.
6093
6094    Returns a representation for the expression.  */
6095
6096 static tree
6097 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6098 {
6099   tree expr;
6100
6101   /* If the next token is the `throw' keyword, then we're looking at
6102      a throw-expression.  */
6103   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6104     expr = cp_parser_throw_expression (parser);
6105   /* Otherwise, it must be that we are looking at a
6106      logical-or-expression.  */
6107   else
6108     {
6109       /* Parse the binary expressions (logical-or-expression).  */
6110       expr = cp_parser_binary_expression (parser, cast_p);
6111       /* If the next token is a `?' then we're actually looking at a
6112          conditional-expression.  */
6113       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6114         return cp_parser_question_colon_clause (parser, expr);
6115       else
6116         {
6117           enum tree_code assignment_operator;
6118
6119           /* If it's an assignment-operator, we're using the second
6120              production.  */
6121           assignment_operator
6122             = cp_parser_assignment_operator_opt (parser);
6123           if (assignment_operator != ERROR_MARK)
6124             {
6125               tree rhs;
6126
6127               /* Parse the right-hand side of the assignment.  */
6128               rhs = cp_parser_assignment_expression (parser, cast_p);
6129               /* An assignment may not appear in a
6130                  constant-expression.  */
6131               if (cp_parser_non_integral_constant_expression (parser,
6132                                                               "an assignment"))
6133                 return error_mark_node;
6134               /* Build the assignment expression.  */
6135               expr = build_x_modify_expr (expr,
6136                                           assignment_operator,
6137                                           rhs);
6138             }
6139         }
6140     }
6141
6142   return expr;
6143 }
6144
6145 /* Parse an (optional) assignment-operator.
6146
6147    assignment-operator: one of
6148      = *= /= %= += -= >>= <<= &= ^= |=
6149
6150    GNU Extension:
6151
6152    assignment-operator: one of
6153      <?= >?=
6154
6155    If the next token is an assignment operator, the corresponding tree
6156    code is returned, and the token is consumed.  For example, for
6157    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6158    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6159    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6160    operator, ERROR_MARK is returned.  */
6161
6162 static enum tree_code
6163 cp_parser_assignment_operator_opt (cp_parser* parser)
6164 {
6165   enum tree_code op;
6166   cp_token *token;
6167
6168   /* Peek at the next toen.  */
6169   token = cp_lexer_peek_token (parser->lexer);
6170
6171   switch (token->type)
6172     {
6173     case CPP_EQ:
6174       op = NOP_EXPR;
6175       break;
6176
6177     case CPP_MULT_EQ:
6178       op = MULT_EXPR;
6179       break;
6180
6181     case CPP_DIV_EQ:
6182       op = TRUNC_DIV_EXPR;
6183       break;
6184
6185     case CPP_MOD_EQ:
6186       op = TRUNC_MOD_EXPR;
6187       break;
6188
6189     case CPP_PLUS_EQ:
6190       op = PLUS_EXPR;
6191       break;
6192
6193     case CPP_MINUS_EQ:
6194       op = MINUS_EXPR;
6195       break;
6196
6197     case CPP_RSHIFT_EQ:
6198       op = RSHIFT_EXPR;
6199       break;
6200
6201     case CPP_LSHIFT_EQ:
6202       op = LSHIFT_EXPR;
6203       break;
6204
6205     case CPP_AND_EQ:
6206       op = BIT_AND_EXPR;
6207       break;
6208
6209     case CPP_XOR_EQ:
6210       op = BIT_XOR_EXPR;
6211       break;
6212
6213     case CPP_OR_EQ:
6214       op = BIT_IOR_EXPR;
6215       break;
6216
6217     default:
6218       /* Nothing else is an assignment operator.  */
6219       op = ERROR_MARK;
6220     }
6221
6222   /* If it was an assignment operator, consume it.  */
6223   if (op != ERROR_MARK)
6224     cp_lexer_consume_token (parser->lexer);
6225
6226   return op;
6227 }
6228
6229 /* Parse an expression.
6230
6231    expression:
6232      assignment-expression
6233      expression , assignment-expression
6234
6235    CAST_P is true if this expression is the target of a cast.
6236
6237    Returns a representation of the expression.  */
6238
6239 static tree
6240 cp_parser_expression (cp_parser* parser, bool cast_p)
6241 {
6242   tree expression = NULL_TREE;
6243
6244   while (true)
6245     {
6246       tree assignment_expression;
6247
6248       /* Parse the next assignment-expression.  */
6249       assignment_expression
6250         = cp_parser_assignment_expression (parser, cast_p);
6251       /* If this is the first assignment-expression, we can just
6252          save it away.  */
6253       if (!expression)
6254         expression = assignment_expression;
6255       else
6256         expression = build_x_compound_expr (expression,
6257                                             assignment_expression);
6258       /* If the next token is not a comma, then we are done with the
6259          expression.  */
6260       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6261         break;
6262       /* Consume the `,'.  */
6263       cp_lexer_consume_token (parser->lexer);
6264       /* A comma operator cannot appear in a constant-expression.  */
6265       if (cp_parser_non_integral_constant_expression (parser,
6266                                                       "a comma operator"))
6267         expression = error_mark_node;
6268     }
6269
6270   return expression;
6271 }
6272
6273 /* Parse a constant-expression.
6274
6275    constant-expression:
6276      conditional-expression
6277
6278   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6279   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6280   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6281   is false, NON_CONSTANT_P should be NULL.  */
6282
6283 static tree
6284 cp_parser_constant_expression (cp_parser* parser,
6285                                bool allow_non_constant_p,
6286                                bool *non_constant_p)
6287 {
6288   bool saved_integral_constant_expression_p;
6289   bool saved_allow_non_integral_constant_expression_p;
6290   bool saved_non_integral_constant_expression_p;
6291   tree expression;
6292
6293   /* It might seem that we could simply parse the
6294      conditional-expression, and then check to see if it were
6295      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6296      one that the compiler can figure out is constant, possibly after
6297      doing some simplifications or optimizations.  The standard has a
6298      precise definition of constant-expression, and we must honor
6299      that, even though it is somewhat more restrictive.
6300
6301      For example:
6302
6303        int i[(2, 3)];
6304
6305      is not a legal declaration, because `(2, 3)' is not a
6306      constant-expression.  The `,' operator is forbidden in a
6307      constant-expression.  However, GCC's constant-folding machinery
6308      will fold this operation to an INTEGER_CST for `3'.  */
6309
6310   /* Save the old settings.  */
6311   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6312   saved_allow_non_integral_constant_expression_p
6313     = parser->allow_non_integral_constant_expression_p;
6314   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6315   /* We are now parsing a constant-expression.  */
6316   parser->integral_constant_expression_p = true;
6317   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6318   parser->non_integral_constant_expression_p = false;
6319   /* Although the grammar says "conditional-expression", we parse an
6320      "assignment-expression", which also permits "throw-expression"
6321      and the use of assignment operators.  In the case that
6322      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6323      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6324      actually essential that we look for an assignment-expression.
6325      For example, cp_parser_initializer_clauses uses this function to
6326      determine whether a particular assignment-expression is in fact
6327      constant.  */
6328   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6329   /* Restore the old settings.  */
6330   parser->integral_constant_expression_p
6331     = saved_integral_constant_expression_p;
6332   parser->allow_non_integral_constant_expression_p
6333     = saved_allow_non_integral_constant_expression_p;
6334   if (allow_non_constant_p)
6335     *non_constant_p = parser->non_integral_constant_expression_p;
6336   else if (parser->non_integral_constant_expression_p)
6337     expression = error_mark_node;
6338   parser->non_integral_constant_expression_p
6339     = saved_non_integral_constant_expression_p;
6340
6341   return expression;
6342 }
6343
6344 /* Parse __builtin_offsetof.
6345
6346    offsetof-expression:
6347      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6348
6349    offsetof-member-designator:
6350      id-expression
6351      | offsetof-member-designator "." id-expression
6352      | offsetof-member-designator "[" expression "]"  */
6353
6354 static tree
6355 cp_parser_builtin_offsetof (cp_parser *parser)
6356 {
6357   int save_ice_p, save_non_ice_p;
6358   tree type, expr;
6359   cp_id_kind dummy;
6360
6361   /* We're about to accept non-integral-constant things, but will
6362      definitely yield an integral constant expression.  Save and
6363      restore these values around our local parsing.  */
6364   save_ice_p = parser->integral_constant_expression_p;
6365   save_non_ice_p = parser->non_integral_constant_expression_p;
6366
6367   /* Consume the "__builtin_offsetof" token.  */
6368   cp_lexer_consume_token (parser->lexer);
6369   /* Consume the opening `('.  */
6370   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6371   /* Parse the type-id.  */
6372   type = cp_parser_type_id (parser);
6373   /* Look for the `,'.  */
6374   cp_parser_require (parser, CPP_COMMA, "`,'");
6375
6376   /* Build the (type *)null that begins the traditional offsetof macro.  */
6377   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6378
6379   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6380   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6381                                                  true, &dummy);
6382   while (true)
6383     {
6384       cp_token *token = cp_lexer_peek_token (parser->lexer);
6385       switch (token->type)
6386         {
6387         case CPP_OPEN_SQUARE:
6388           /* offsetof-member-designator "[" expression "]" */
6389           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6390           break;
6391
6392         case CPP_DOT:
6393           /* offsetof-member-designator "." identifier */
6394           cp_lexer_consume_token (parser->lexer);
6395           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6396                                                          true, &dummy);
6397           break;
6398
6399         case CPP_CLOSE_PAREN:
6400           /* Consume the ")" token.  */
6401           cp_lexer_consume_token (parser->lexer);
6402           goto success;
6403
6404         default:
6405           /* Error.  We know the following require will fail, but
6406              that gives the proper error message.  */
6407           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6408           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6409           expr = error_mark_node;
6410           goto failure;
6411         }
6412     }
6413
6414  success:
6415   /* If we're processing a template, we can't finish the semantics yet.
6416      Otherwise we can fold the entire expression now.  */
6417   if (processing_template_decl)
6418     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6419   else
6420     expr = finish_offsetof (expr);
6421
6422  failure:
6423   parser->integral_constant_expression_p = save_ice_p;
6424   parser->non_integral_constant_expression_p = save_non_ice_p;
6425
6426   return expr;
6427 }
6428
6429 /* Parse a trait expression.  */
6430
6431 static tree
6432 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6433 {
6434   cp_trait_kind kind;
6435   tree type1, type2 = NULL_TREE;
6436   bool binary = false;
6437   cp_decl_specifier_seq decl_specs;
6438
6439   switch (keyword)
6440     {
6441     case RID_HAS_NOTHROW_ASSIGN:
6442       kind = CPTK_HAS_NOTHROW_ASSIGN;
6443       break;
6444     case RID_HAS_NOTHROW_CONSTRUCTOR:
6445       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6446       break;
6447     case RID_HAS_NOTHROW_COPY:
6448       kind = CPTK_HAS_NOTHROW_COPY;
6449       break;
6450     case RID_HAS_TRIVIAL_ASSIGN:
6451       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6452       break;
6453     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6454       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6455       break;
6456     case RID_HAS_TRIVIAL_COPY:
6457       kind = CPTK_HAS_TRIVIAL_COPY;
6458       break;
6459     case RID_HAS_TRIVIAL_DESTRUCTOR:
6460       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6461       break;
6462     case RID_HAS_VIRTUAL_DESTRUCTOR:
6463       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6464       break;
6465     case RID_IS_ABSTRACT:
6466       kind = CPTK_IS_ABSTRACT;
6467       break;
6468     case RID_IS_BASE_OF:
6469       kind = CPTK_IS_BASE_OF;
6470       binary = true;
6471       break;
6472     case RID_IS_CLASS:
6473       kind = CPTK_IS_CLASS;
6474       break;
6475     case RID_IS_CONVERTIBLE_TO:
6476       kind = CPTK_IS_CONVERTIBLE_TO;
6477       binary = true;
6478       break;
6479     case RID_IS_EMPTY:
6480       kind = CPTK_IS_EMPTY;
6481       break;
6482     case RID_IS_ENUM:
6483       kind = CPTK_IS_ENUM;
6484       break;
6485     case RID_IS_POD:
6486       kind = CPTK_IS_POD;
6487       break;
6488     case RID_IS_POLYMORPHIC:
6489       kind = CPTK_IS_POLYMORPHIC;
6490       break;
6491     case RID_IS_UNION:
6492       kind = CPTK_IS_UNION;
6493       break;
6494     default:
6495       gcc_unreachable ();
6496     }
6497
6498   /* Consume the token.  */
6499   cp_lexer_consume_token (parser->lexer);
6500
6501   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6502
6503   type1 = cp_parser_type_id (parser);
6504
6505   /* Build a trivial decl-specifier-seq.  */
6506   clear_decl_specs (&decl_specs);
6507   decl_specs.type = type1;
6508
6509   /* Call grokdeclarator to figure out what type this is.  */
6510   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6511                           /*initialized=*/0, /*attrlist=*/NULL);
6512
6513   if (binary)
6514     {
6515       cp_parser_require (parser, CPP_COMMA, "`,'");
6516  
6517       type2 = cp_parser_type_id (parser);
6518
6519       /* Build a trivial decl-specifier-seq.  */
6520       clear_decl_specs (&decl_specs);
6521       decl_specs.type = type2;
6522
6523       /* Call grokdeclarator to figure out what type this is.  */
6524       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6525                               /*initialized=*/0, /*attrlist=*/NULL);
6526     }
6527
6528   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6529
6530   /* Complete the trait expr, which may mean either processing the
6531      static assert now or saving it for template instantiation.  */
6532   return finish_trait_expr (kind, type1, type2);
6533 }
6534
6535 /* Statements [gram.stmt.stmt]  */
6536
6537 /* Parse a statement.
6538
6539    statement:
6540      labeled-statement
6541      expression-statement
6542      compound-statement
6543      selection-statement
6544      iteration-statement
6545      jump-statement
6546      declaration-statement
6547      try-block
6548
6549   IN_COMPOUND is true when the statement is nested inside a
6550   cp_parser_compound_statement; this matters for certain pragmas.
6551
6552   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6553   is a (possibly labeled) if statement which is not enclosed in braces
6554   and has an else clause.  This is used to implement -Wparentheses.  */
6555
6556 static void
6557 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6558                      bool in_compound, bool *if_p)
6559 {
6560   tree statement;
6561   cp_token *token;
6562   location_t statement_location;
6563
6564  restart:
6565   if (if_p != NULL)
6566     *if_p = false;
6567   /* There is no statement yet.  */
6568   statement = NULL_TREE;
6569   /* Peek at the next token.  */
6570   token = cp_lexer_peek_token (parser->lexer);
6571   /* Remember the location of the first token in the statement.  */
6572   statement_location = token->location;
6573   /* If this is a keyword, then that will often determine what kind of
6574      statement we have.  */
6575   if (token->type == CPP_KEYWORD)
6576     {
6577       enum rid keyword = token->keyword;
6578
6579       switch (keyword)
6580         {
6581         case RID_CASE:
6582         case RID_DEFAULT:
6583           /* Looks like a labeled-statement with a case label.
6584              Parse the label, and then use tail recursion to parse
6585              the statement.  */
6586           cp_parser_label_for_labeled_statement (parser);
6587           goto restart;
6588
6589         case RID_IF:
6590         case RID_SWITCH:
6591           statement = cp_parser_selection_statement (parser, if_p);
6592           break;
6593
6594         case RID_WHILE:
6595         case RID_DO:
6596         case RID_FOR:
6597           statement = cp_parser_iteration_statement (parser);
6598           break;
6599
6600         case RID_BREAK:
6601         case RID_CONTINUE:
6602         case RID_RETURN:
6603         case RID_GOTO:
6604           statement = cp_parser_jump_statement (parser);
6605           break;
6606
6607           /* Objective-C++ exception-handling constructs.  */
6608         case RID_AT_TRY:
6609         case RID_AT_CATCH:
6610         case RID_AT_FINALLY:
6611         case RID_AT_SYNCHRONIZED:
6612         case RID_AT_THROW:
6613           statement = cp_parser_objc_statement (parser);
6614           break;
6615
6616         case RID_TRY:
6617           statement = cp_parser_try_block (parser);
6618           break;
6619
6620         case RID_NAMESPACE:
6621           /* This must be a namespace alias definition.  */
6622           cp_parser_declaration_statement (parser);
6623           return;
6624           
6625         default:
6626           /* It might be a keyword like `int' that can start a
6627              declaration-statement.  */
6628           break;
6629         }
6630     }
6631   else if (token->type == CPP_NAME)
6632     {
6633       /* If the next token is a `:', then we are looking at a
6634          labeled-statement.  */
6635       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6636       if (token->type == CPP_COLON)
6637         {
6638           /* Looks like a labeled-statement with an ordinary label.
6639              Parse the label, and then use tail recursion to parse
6640              the statement.  */
6641           cp_parser_label_for_labeled_statement (parser);
6642           goto restart;
6643         }
6644     }
6645   /* Anything that starts with a `{' must be a compound-statement.  */
6646   else if (token->type == CPP_OPEN_BRACE)
6647     statement = cp_parser_compound_statement (parser, NULL, false);
6648   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6649      a statement all its own.  */
6650   else if (token->type == CPP_PRAGMA)
6651     {
6652       /* Only certain OpenMP pragmas are attached to statements, and thus
6653          are considered statements themselves.  All others are not.  In
6654          the context of a compound, accept the pragma as a "statement" and
6655          return so that we can check for a close brace.  Otherwise we
6656          require a real statement and must go back and read one.  */
6657       if (in_compound)
6658         cp_parser_pragma (parser, pragma_compound);
6659       else if (!cp_parser_pragma (parser, pragma_stmt))
6660         goto restart;
6661       return;
6662     }
6663   else if (token->type == CPP_EOF)
6664     {
6665       cp_parser_error (parser, "expected statement");
6666       return;
6667     }
6668
6669   /* Everything else must be a declaration-statement or an
6670      expression-statement.  Try for the declaration-statement
6671      first, unless we are looking at a `;', in which case we know that
6672      we have an expression-statement.  */
6673   if (!statement)
6674     {
6675       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6676         {
6677           cp_parser_parse_tentatively (parser);
6678           /* Try to parse the declaration-statement.  */
6679           cp_parser_declaration_statement (parser);
6680           /* If that worked, we're done.  */
6681           if (cp_parser_parse_definitely (parser))
6682             return;
6683         }
6684       /* Look for an expression-statement instead.  */
6685       statement = cp_parser_expression_statement (parser, in_statement_expr);
6686     }
6687
6688   /* Set the line number for the statement.  */
6689   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6690     SET_EXPR_LOCATION (statement, statement_location);
6691 }
6692
6693 /* Parse the label for a labeled-statement, i.e.
6694
6695    identifier :
6696    case constant-expression :
6697    default :
6698
6699    GNU Extension:
6700    case constant-expression ... constant-expression : statement
6701
6702    When a label is parsed without errors, the label is added to the
6703    parse tree by the finish_* functions, so this function doesn't
6704    have to return the label.  */
6705
6706 static void
6707 cp_parser_label_for_labeled_statement (cp_parser* parser)
6708 {
6709   cp_token *token;
6710
6711   /* The next token should be an identifier.  */
6712   token = cp_lexer_peek_token (parser->lexer);
6713   if (token->type != CPP_NAME
6714       && token->type != CPP_KEYWORD)
6715     {
6716       cp_parser_error (parser, "expected labeled-statement");
6717       return;
6718     }
6719
6720   switch (token->keyword)
6721     {
6722     case RID_CASE:
6723       {
6724         tree expr, expr_hi;
6725         cp_token *ellipsis;
6726
6727         /* Consume the `case' token.  */
6728         cp_lexer_consume_token (parser->lexer);
6729         /* Parse the constant-expression.  */
6730         expr = cp_parser_constant_expression (parser,
6731                                               /*allow_non_constant_p=*/false,
6732                                               NULL);
6733
6734         ellipsis = cp_lexer_peek_token (parser->lexer);
6735         if (ellipsis->type == CPP_ELLIPSIS)
6736           {
6737             /* Consume the `...' token.  */
6738             cp_lexer_consume_token (parser->lexer);
6739             expr_hi =
6740               cp_parser_constant_expression (parser,
6741                                              /*allow_non_constant_p=*/false,
6742                                              NULL);
6743             /* We don't need to emit warnings here, as the common code
6744                will do this for us.  */
6745           }
6746         else
6747           expr_hi = NULL_TREE;
6748
6749         if (parser->in_switch_statement_p)
6750           finish_case_label (expr, expr_hi);
6751         else
6752           error ("case label %qE not within a switch statement", expr);
6753       }
6754       break;
6755
6756     case RID_DEFAULT:
6757       /* Consume the `default' token.  */
6758       cp_lexer_consume_token (parser->lexer);
6759
6760       if (parser->in_switch_statement_p)
6761         finish_case_label (NULL_TREE, NULL_TREE);
6762       else
6763         error ("case label not within a switch statement");
6764       break;
6765
6766     default:
6767       /* Anything else must be an ordinary label.  */
6768       finish_label_stmt (cp_parser_identifier (parser));
6769       break;
6770     }
6771
6772   /* Require the `:' token.  */
6773   cp_parser_require (parser, CPP_COLON, "`:'");
6774 }
6775
6776 /* Parse an expression-statement.
6777
6778    expression-statement:
6779      expression [opt] ;
6780
6781    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6782    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6783    indicates whether this expression-statement is part of an
6784    expression statement.  */
6785
6786 static tree
6787 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6788 {
6789   tree statement = NULL_TREE;
6790
6791   /* If the next token is a ';', then there is no expression
6792      statement.  */
6793   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6794     statement = cp_parser_expression (parser, /*cast_p=*/false);
6795
6796   /* Consume the final `;'.  */
6797   cp_parser_consume_semicolon_at_end_of_statement (parser);
6798
6799   if (in_statement_expr
6800       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6801     /* This is the final expression statement of a statement
6802        expression.  */
6803     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6804   else if (statement)
6805     statement = finish_expr_stmt (statement);
6806   else
6807     finish_stmt ();
6808
6809   return statement;
6810 }
6811
6812 /* Parse a compound-statement.
6813
6814    compound-statement:
6815      { statement-seq [opt] }
6816
6817    Returns a tree representing the statement.  */
6818
6819 static tree
6820 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6821                               bool in_try)
6822 {
6823   tree compound_stmt;
6824
6825   /* Consume the `{'.  */
6826   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6827     return error_mark_node;
6828   /* Begin the compound-statement.  */
6829   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6830   /* Parse an (optional) statement-seq.  */
6831   cp_parser_statement_seq_opt (parser, in_statement_expr);
6832   /* Finish the compound-statement.  */
6833   finish_compound_stmt (compound_stmt);
6834   /* Consume the `}'.  */
6835   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6836
6837   return compound_stmt;
6838 }
6839
6840 /* Parse an (optional) statement-seq.
6841
6842    statement-seq:
6843      statement
6844      statement-seq [opt] statement  */
6845
6846 static void
6847 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6848 {
6849   /* Scan statements until there aren't any more.  */
6850   while (true)
6851     {
6852       cp_token *token = cp_lexer_peek_token (parser->lexer);
6853
6854       /* If we're looking at a `}', then we've run out of statements.  */
6855       if (token->type == CPP_CLOSE_BRACE
6856           || token->type == CPP_EOF
6857           || token->type == CPP_PRAGMA_EOL)
6858         break;
6859       
6860       /* If we are in a compound statement and find 'else' then
6861          something went wrong.  */
6862       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6863         {
6864           if (parser->in_statement & IN_IF_STMT) 
6865             break;
6866           else
6867             {
6868               token = cp_lexer_consume_token (parser->lexer);
6869               error ("%<else%> without a previous %<if%>");
6870             }
6871         }
6872
6873       /* Parse the statement.  */
6874       cp_parser_statement (parser, in_statement_expr, true, NULL);
6875     }
6876 }
6877
6878 /* Parse a selection-statement.
6879
6880    selection-statement:
6881      if ( condition ) statement
6882      if ( condition ) statement else statement
6883      switch ( condition ) statement
6884
6885    Returns the new IF_STMT or SWITCH_STMT.
6886
6887    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6888    is a (possibly labeled) if statement which is not enclosed in
6889    braces and has an else clause.  This is used to implement
6890    -Wparentheses.  */
6891
6892 static tree
6893 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6894 {
6895   cp_token *token;
6896   enum rid keyword;
6897
6898   if (if_p != NULL)
6899     *if_p = false;
6900
6901   /* Peek at the next token.  */
6902   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6903
6904   /* See what kind of keyword it is.  */
6905   keyword = token->keyword;
6906   switch (keyword)
6907     {
6908     case RID_IF:
6909     case RID_SWITCH:
6910       {
6911         tree statement;
6912         tree condition;
6913
6914         /* Look for the `('.  */
6915         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6916           {
6917             cp_parser_skip_to_end_of_statement (parser);
6918             return error_mark_node;
6919           }
6920
6921         /* Begin the selection-statement.  */
6922         if (keyword == RID_IF)
6923           statement = begin_if_stmt ();
6924         else
6925           statement = begin_switch_stmt ();
6926
6927         /* Parse the condition.  */
6928         condition = cp_parser_condition (parser);
6929         /* Look for the `)'.  */
6930         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6931           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6932                                                  /*consume_paren=*/true);
6933
6934         if (keyword == RID_IF)
6935           {
6936             bool nested_if;
6937             unsigned char in_statement;
6938
6939             /* Add the condition.  */
6940             finish_if_stmt_cond (condition, statement);
6941
6942             /* Parse the then-clause.  */
6943             in_statement = parser->in_statement;
6944             parser->in_statement |= IN_IF_STMT;
6945             cp_parser_implicitly_scoped_statement (parser, &nested_if);
6946             parser->in_statement = in_statement;
6947
6948             finish_then_clause (statement);
6949
6950             /* If the next token is `else', parse the else-clause.  */
6951             if (cp_lexer_next_token_is_keyword (parser->lexer,
6952                                                 RID_ELSE))
6953               {
6954                 /* Consume the `else' keyword.  */
6955                 cp_lexer_consume_token (parser->lexer);
6956                 begin_else_clause (statement);
6957                 /* Parse the else-clause.  */
6958                 cp_parser_implicitly_scoped_statement (parser, NULL);
6959                 finish_else_clause (statement);
6960
6961                 /* If we are currently parsing a then-clause, then
6962                    IF_P will not be NULL.  We set it to true to
6963                    indicate that this if statement has an else clause.
6964                    This may trigger the Wparentheses warning below
6965                    when we get back up to the parent if statement.  */
6966                 if (if_p != NULL)
6967                   *if_p = true;
6968               }
6969             else
6970               {
6971                 /* This if statement does not have an else clause.  If
6972                    NESTED_IF is true, then the then-clause is an if
6973                    statement which does have an else clause.  We warn
6974                    about the potential ambiguity.  */
6975                 if (nested_if)
6976                   warning (OPT_Wparentheses,
6977                            ("%Hsuggest explicit braces "
6978                             "to avoid ambiguous %<else%>"),
6979                            EXPR_LOCUS (statement));
6980               }
6981
6982             /* Now we're all done with the if-statement.  */
6983             finish_if_stmt (statement);
6984           }
6985         else
6986           {
6987             bool in_switch_statement_p;
6988             unsigned char in_statement;
6989
6990             /* Add the condition.  */
6991             finish_switch_cond (condition, statement);
6992
6993             /* Parse the body of the switch-statement.  */
6994             in_switch_statement_p = parser->in_switch_statement_p;
6995             in_statement = parser->in_statement;
6996             parser->in_switch_statement_p = true;
6997             parser->in_statement |= IN_SWITCH_STMT;
6998             cp_parser_implicitly_scoped_statement (parser, NULL);
6999             parser->in_switch_statement_p = in_switch_statement_p;
7000             parser->in_statement = in_statement;
7001
7002             /* Now we're all done with the switch-statement.  */
7003             finish_switch_stmt (statement);
7004           }
7005
7006         return statement;
7007       }
7008       break;
7009
7010     default:
7011       cp_parser_error (parser, "expected selection-statement");
7012       return error_mark_node;
7013     }
7014 }
7015
7016 /* Parse a condition.
7017
7018    condition:
7019      expression
7020      type-specifier-seq declarator = assignment-expression
7021
7022    GNU Extension:
7023
7024    condition:
7025      type-specifier-seq declarator asm-specification [opt]
7026        attributes [opt] = assignment-expression
7027
7028    Returns the expression that should be tested.  */
7029
7030 static tree
7031 cp_parser_condition (cp_parser* parser)
7032 {
7033   cp_decl_specifier_seq type_specifiers;
7034   const char *saved_message;
7035
7036   /* Try the declaration first.  */
7037   cp_parser_parse_tentatively (parser);
7038   /* New types are not allowed in the type-specifier-seq for a
7039      condition.  */
7040   saved_message = parser->type_definition_forbidden_message;
7041   parser->type_definition_forbidden_message
7042     = "types may not be defined in conditions";
7043   /* Parse the type-specifier-seq.  */
7044   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7045                                 &type_specifiers);
7046   /* Restore the saved message.  */
7047   parser->type_definition_forbidden_message = saved_message;
7048   /* If all is well, we might be looking at a declaration.  */
7049   if (!cp_parser_error_occurred (parser))
7050     {
7051       tree decl;
7052       tree asm_specification;
7053       tree attributes;
7054       cp_declarator *declarator;
7055       tree initializer = NULL_TREE;
7056
7057       /* Parse the declarator.  */
7058       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7059                                          /*ctor_dtor_or_conv_p=*/NULL,
7060                                          /*parenthesized_p=*/NULL,
7061                                          /*member_p=*/false);
7062       /* Parse the attributes.  */
7063       attributes = cp_parser_attributes_opt (parser);
7064       /* Parse the asm-specification.  */
7065       asm_specification = cp_parser_asm_specification_opt (parser);
7066       /* If the next token is not an `=', then we might still be
7067          looking at an expression.  For example:
7068
7069            if (A(a).x)
7070
7071          looks like a decl-specifier-seq and a declarator -- but then
7072          there is no `=', so this is an expression.  */
7073       cp_parser_require (parser, CPP_EQ, "`='");
7074       /* If we did see an `=', then we are looking at a declaration
7075          for sure.  */
7076       if (cp_parser_parse_definitely (parser))
7077         {
7078           tree pushed_scope;
7079           bool non_constant_p;
7080
7081           /* Create the declaration.  */
7082           decl = start_decl (declarator, &type_specifiers,
7083                              /*initialized_p=*/true,
7084                              attributes, /*prefix_attributes=*/NULL_TREE,
7085                              &pushed_scope);
7086           /* Parse the assignment-expression.  */
7087           initializer
7088             = cp_parser_constant_expression (parser,
7089                                              /*allow_non_constant_p=*/true,
7090                                              &non_constant_p);
7091           if (!non_constant_p)
7092             initializer = fold_non_dependent_expr (initializer);
7093
7094           /* Process the initializer.  */
7095           cp_finish_decl (decl,
7096                           initializer, !non_constant_p,
7097                           asm_specification,
7098                           LOOKUP_ONLYCONVERTING);
7099
7100           if (pushed_scope)
7101             pop_scope (pushed_scope);
7102
7103           return convert_from_reference (decl);
7104         }
7105     }
7106   /* If we didn't even get past the declarator successfully, we are
7107      definitely not looking at a declaration.  */
7108   else
7109     cp_parser_abort_tentative_parse (parser);
7110
7111   /* Otherwise, we are looking at an expression.  */
7112   return cp_parser_expression (parser, /*cast_p=*/false);
7113 }
7114
7115 /* We check for a ) immediately followed by ; with no whitespacing
7116    between.  This is used to issue a warning for:
7117
7118      while (...);
7119
7120    and:
7121
7122      for (...);
7123
7124    as the semicolon is probably extraneous.
7125
7126    On parse errors, the next token might not be a ), so do nothing in
7127    that case. */
7128
7129 static void
7130 check_empty_body (cp_parser* parser, const char* type)
7131 {
7132   cp_token *token;
7133   cp_token *close_paren;
7134   expanded_location close_loc;
7135   expanded_location semi_loc;
7136   
7137   close_paren = cp_lexer_peek_token (parser->lexer);
7138   if (close_paren->type != CPP_CLOSE_PAREN)
7139     return;
7140
7141   close_loc = expand_location (close_paren->location);
7142   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7143
7144   if (token->type != CPP_SEMICOLON
7145       || (token->flags & PREV_WHITE))
7146     return;
7147
7148   semi_loc =  expand_location (token->location);
7149   if (close_loc.line == semi_loc.line
7150 #ifdef USE_MAPPED_LOCATION
7151       && close_loc.column+1 == semi_loc.column
7152 #endif
7153       )
7154     warning (OPT_Wempty_body,
7155              "suggest a space before %<;%> or explicit braces around empty "
7156              "body in %<%s%> statement",
7157              type);
7158 }
7159
7160 /* Parse an iteration-statement.
7161
7162    iteration-statement:
7163      while ( condition ) statement
7164      do statement while ( expression ) ;
7165      for ( for-init-statement condition [opt] ; expression [opt] )
7166        statement
7167
7168    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7169
7170 static tree
7171 cp_parser_iteration_statement (cp_parser* parser)
7172 {
7173   cp_token *token;
7174   enum rid keyword;
7175   tree statement;
7176   unsigned char in_statement;
7177
7178   /* Peek at the next token.  */
7179   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7180   if (!token)
7181     return error_mark_node;
7182
7183   /* Remember whether or not we are already within an iteration
7184      statement.  */
7185   in_statement = parser->in_statement;
7186
7187   /* See what kind of keyword it is.  */
7188   keyword = token->keyword;
7189   switch (keyword)
7190     {
7191     case RID_WHILE:
7192       {
7193         tree condition;
7194
7195         /* Begin the while-statement.  */
7196         statement = begin_while_stmt ();
7197         /* Look for the `('.  */
7198         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7199         /* Parse the condition.  */
7200         condition = cp_parser_condition (parser);
7201         finish_while_stmt_cond (condition, statement);
7202         check_empty_body (parser, "while");
7203         /* Look for the `)'.  */
7204         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7205         /* Parse the dependent statement.  */
7206         parser->in_statement = IN_ITERATION_STMT;
7207         cp_parser_already_scoped_statement (parser);
7208         parser->in_statement = in_statement;
7209         /* We're done with the while-statement.  */
7210         finish_while_stmt (statement);
7211       }
7212       break;
7213
7214     case RID_DO:
7215       {
7216         tree expression;
7217
7218         /* Begin the do-statement.  */
7219         statement = begin_do_stmt ();
7220         /* Parse the body of the do-statement.  */
7221         parser->in_statement = IN_ITERATION_STMT;
7222         cp_parser_implicitly_scoped_statement (parser, NULL);
7223         parser->in_statement = in_statement;
7224         finish_do_body (statement);
7225         /* Look for the `while' keyword.  */
7226         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7227         /* Look for the `('.  */
7228         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7229         /* Parse the expression.  */
7230         expression = cp_parser_expression (parser, /*cast_p=*/false);
7231         /* We're done with the do-statement.  */
7232         finish_do_stmt (expression, statement);
7233         /* Look for the `)'.  */
7234         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7235         /* Look for the `;'.  */
7236         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7237       }
7238       break;
7239
7240     case RID_FOR:
7241       {
7242         tree condition = NULL_TREE;
7243         tree expression = NULL_TREE;
7244
7245         /* Begin the for-statement.  */
7246         statement = begin_for_stmt ();
7247         /* Look for the `('.  */
7248         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7249         /* Parse the initialization.  */
7250         cp_parser_for_init_statement (parser);
7251         finish_for_init_stmt (statement);
7252
7253         /* If there's a condition, process it.  */
7254         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7255           condition = cp_parser_condition (parser);
7256         finish_for_cond (condition, statement);
7257         /* Look for the `;'.  */
7258         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7259
7260         /* If there's an expression, process it.  */
7261         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7262           expression = cp_parser_expression (parser, /*cast_p=*/false);
7263         finish_for_expr (expression, statement);
7264         check_empty_body (parser, "for");
7265         /* Look for the `)'.  */
7266         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7267
7268         /* Parse the body of the for-statement.  */
7269         parser->in_statement = IN_ITERATION_STMT;
7270         cp_parser_already_scoped_statement (parser);
7271         parser->in_statement = in_statement;
7272
7273         /* We're done with the for-statement.  */
7274         finish_for_stmt (statement);
7275       }
7276       break;
7277
7278     default:
7279       cp_parser_error (parser, "expected iteration-statement");
7280       statement = error_mark_node;
7281       break;
7282     }
7283
7284   return statement;
7285 }
7286
7287 /* Parse a for-init-statement.
7288
7289    for-init-statement:
7290      expression-statement
7291      simple-declaration  */
7292
7293 static void
7294 cp_parser_for_init_statement (cp_parser* parser)
7295 {
7296   /* If the next token is a `;', then we have an empty
7297      expression-statement.  Grammatically, this is also a
7298      simple-declaration, but an invalid one, because it does not
7299      declare anything.  Therefore, if we did not handle this case
7300      specially, we would issue an error message about an invalid
7301      declaration.  */
7302   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7303     {
7304       /* We're going to speculatively look for a declaration, falling back
7305          to an expression, if necessary.  */
7306       cp_parser_parse_tentatively (parser);
7307       /* Parse the declaration.  */
7308       cp_parser_simple_declaration (parser,
7309                                     /*function_definition_allowed_p=*/false);
7310       /* If the tentative parse failed, then we shall need to look for an
7311          expression-statement.  */
7312       if (cp_parser_parse_definitely (parser))
7313         return;
7314     }
7315
7316   cp_parser_expression_statement (parser, false);
7317 }
7318
7319 /* Parse a jump-statement.
7320
7321    jump-statement:
7322      break ;
7323      continue ;
7324      return expression [opt] ;
7325      goto identifier ;
7326
7327    GNU extension:
7328
7329    jump-statement:
7330      goto * expression ;
7331
7332    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7333
7334 static tree
7335 cp_parser_jump_statement (cp_parser* parser)
7336 {
7337   tree statement = error_mark_node;
7338   cp_token *token;
7339   enum rid keyword;
7340   unsigned char in_statement;
7341
7342   /* Peek at the next token.  */
7343   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7344   if (!token)
7345     return error_mark_node;
7346
7347   /* See what kind of keyword it is.  */
7348   keyword = token->keyword;
7349   switch (keyword)
7350     {
7351     case RID_BREAK:
7352       in_statement = parser->in_statement & ~IN_IF_STMT;      
7353       switch (in_statement)
7354         {
7355         case 0:
7356           error ("break statement not within loop or switch");
7357           break;
7358         default:
7359           gcc_assert ((in_statement & IN_SWITCH_STMT)
7360                       || in_statement == IN_ITERATION_STMT);
7361           statement = finish_break_stmt ();
7362           break;
7363         case IN_OMP_BLOCK:
7364           error ("invalid exit from OpenMP structured block");
7365           break;
7366         case IN_OMP_FOR:
7367           error ("break statement used with OpenMP for loop");
7368           break;
7369         }
7370       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7371       break;
7372
7373     case RID_CONTINUE:
7374       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7375         {
7376         case 0:
7377           error ("continue statement not within a loop");
7378           break;
7379         case IN_ITERATION_STMT:
7380         case IN_OMP_FOR:
7381           statement = finish_continue_stmt ();
7382           break;
7383         case IN_OMP_BLOCK:
7384           error ("invalid exit from OpenMP structured block");
7385           break;
7386         default:
7387           gcc_unreachable ();
7388         }
7389       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7390       break;
7391
7392     case RID_RETURN:
7393       {
7394         tree expr;
7395
7396         /* If the next token is a `;', then there is no
7397            expression.  */
7398         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7399           expr = cp_parser_expression (parser, /*cast_p=*/false);
7400         else
7401           expr = NULL_TREE;
7402         /* Build the return-statement.  */
7403         statement = finish_return_stmt (expr);
7404         /* Look for the final `;'.  */
7405         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7406       }
7407       break;
7408
7409     case RID_GOTO:
7410       /* Create the goto-statement.  */
7411       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7412         {
7413           /* Issue a warning about this use of a GNU extension.  */
7414           if (pedantic)
7415             pedwarn ("ISO C++ forbids computed gotos");
7416           /* Consume the '*' token.  */
7417           cp_lexer_consume_token (parser->lexer);
7418           /* Parse the dependent expression.  */
7419           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7420         }
7421       else
7422         finish_goto_stmt (cp_parser_identifier (parser));
7423       /* Look for the final `;'.  */
7424       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7425       break;
7426
7427     default:
7428       cp_parser_error (parser, "expected jump-statement");
7429       break;
7430     }
7431
7432   return statement;
7433 }
7434
7435 /* Parse a declaration-statement.
7436
7437    declaration-statement:
7438      block-declaration  */
7439
7440 static void
7441 cp_parser_declaration_statement (cp_parser* parser)
7442 {
7443   void *p;
7444
7445   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7446   p = obstack_alloc (&declarator_obstack, 0);
7447
7448  /* Parse the block-declaration.  */
7449   cp_parser_block_declaration (parser, /*statement_p=*/true);
7450
7451   /* Free any declarators allocated.  */
7452   obstack_free (&declarator_obstack, p);
7453
7454   /* Finish off the statement.  */
7455   finish_stmt ();
7456 }
7457
7458 /* Some dependent statements (like `if (cond) statement'), are
7459    implicitly in their own scope.  In other words, if the statement is
7460    a single statement (as opposed to a compound-statement), it is
7461    none-the-less treated as if it were enclosed in braces.  Any
7462    declarations appearing in the dependent statement are out of scope
7463    after control passes that point.  This function parses a statement,
7464    but ensures that is in its own scope, even if it is not a
7465    compound-statement.
7466
7467    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7468    is a (possibly labeled) if statement which is not enclosed in
7469    braces and has an else clause.  This is used to implement
7470    -Wparentheses.
7471
7472    Returns the new statement.  */
7473
7474 static tree
7475 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7476 {
7477   tree statement;
7478
7479   if (if_p != NULL)
7480     *if_p = false;
7481
7482   /* Mark if () ; with a special NOP_EXPR.  */
7483   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7484     {
7485       cp_lexer_consume_token (parser->lexer);
7486       statement = add_stmt (build_empty_stmt ());
7487     }
7488   /* if a compound is opened, we simply parse the statement directly.  */
7489   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7490     statement = cp_parser_compound_statement (parser, NULL, false);
7491   /* If the token is not a `{', then we must take special action.  */
7492   else
7493     {
7494       /* Create a compound-statement.  */
7495       statement = begin_compound_stmt (0);
7496       /* Parse the dependent-statement.  */
7497       cp_parser_statement (parser, NULL_TREE, false, if_p);
7498       /* Finish the dummy compound-statement.  */
7499       finish_compound_stmt (statement);
7500     }
7501
7502   /* Return the statement.  */
7503   return statement;
7504 }
7505
7506 /* For some dependent statements (like `while (cond) statement'), we
7507    have already created a scope.  Therefore, even if the dependent
7508    statement is a compound-statement, we do not want to create another
7509    scope.  */
7510
7511 static void
7512 cp_parser_already_scoped_statement (cp_parser* parser)
7513 {
7514   /* If the token is a `{', then we must take special action.  */
7515   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7516     cp_parser_statement (parser, NULL_TREE, false, NULL);
7517   else
7518     {
7519       /* Avoid calling cp_parser_compound_statement, so that we
7520          don't create a new scope.  Do everything else by hand.  */
7521       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7522       cp_parser_statement_seq_opt (parser, NULL_TREE);
7523       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7524     }
7525 }
7526
7527 /* Declarations [gram.dcl.dcl] */
7528
7529 /* Parse an optional declaration-sequence.
7530
7531    declaration-seq:
7532      declaration
7533      declaration-seq declaration  */
7534
7535 static void
7536 cp_parser_declaration_seq_opt (cp_parser* parser)
7537 {
7538   while (true)
7539     {
7540       cp_token *token;
7541
7542       token = cp_lexer_peek_token (parser->lexer);
7543
7544       if (token->type == CPP_CLOSE_BRACE
7545           || token->type == CPP_EOF
7546           || token->type == CPP_PRAGMA_EOL)
7547         break;
7548
7549       if (token->type == CPP_SEMICOLON)
7550         {
7551           /* A declaration consisting of a single semicolon is
7552              invalid.  Allow it unless we're being pedantic.  */
7553           cp_lexer_consume_token (parser->lexer);
7554           if (pedantic && !in_system_header)
7555             pedwarn ("extra %<;%>");
7556           continue;
7557         }
7558
7559       /* If we're entering or exiting a region that's implicitly
7560          extern "C", modify the lang context appropriately.  */
7561       if (!parser->implicit_extern_c && token->implicit_extern_c)
7562         {
7563           push_lang_context (lang_name_c);
7564           parser->implicit_extern_c = true;
7565         }
7566       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7567         {
7568           pop_lang_context ();
7569           parser->implicit_extern_c = false;
7570         }
7571
7572       if (token->type == CPP_PRAGMA)
7573         {
7574           /* A top-level declaration can consist solely of a #pragma.
7575              A nested declaration cannot, so this is done here and not
7576              in cp_parser_declaration.  (A #pragma at block scope is
7577              handled in cp_parser_statement.)  */
7578           cp_parser_pragma (parser, pragma_external);
7579           continue;
7580         }
7581
7582       /* Parse the declaration itself.  */
7583       cp_parser_declaration (parser);
7584     }
7585 }
7586
7587 /* Parse a declaration.
7588
7589    declaration:
7590      block-declaration
7591      function-definition
7592      template-declaration
7593      explicit-instantiation
7594      explicit-specialization
7595      linkage-specification
7596      namespace-definition
7597
7598    GNU extension:
7599
7600    declaration:
7601       __extension__ declaration */
7602
7603 static void
7604 cp_parser_declaration (cp_parser* parser)
7605 {
7606   cp_token token1;
7607   cp_token token2;
7608   int saved_pedantic;
7609   void *p;
7610
7611   /* Check for the `__extension__' keyword.  */
7612   if (cp_parser_extension_opt (parser, &saved_pedantic))
7613     {
7614       /* Parse the qualified declaration.  */
7615       cp_parser_declaration (parser);
7616       /* Restore the PEDANTIC flag.  */
7617       pedantic = saved_pedantic;
7618
7619       return;
7620     }
7621
7622   /* Try to figure out what kind of declaration is present.  */
7623   token1 = *cp_lexer_peek_token (parser->lexer);
7624
7625   if (token1.type != CPP_EOF)
7626     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7627   else
7628     {
7629       token2.type = CPP_EOF;
7630       token2.keyword = RID_MAX;
7631     }
7632
7633   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7634   p = obstack_alloc (&declarator_obstack, 0);
7635
7636   /* If the next token is `extern' and the following token is a string
7637      literal, then we have a linkage specification.  */
7638   if (token1.keyword == RID_EXTERN
7639       && cp_parser_is_string_literal (&token2))
7640     cp_parser_linkage_specification (parser);
7641   /* If the next token is `template', then we have either a template
7642      declaration, an explicit instantiation, or an explicit
7643      specialization.  */
7644   else if (token1.keyword == RID_TEMPLATE)
7645     {
7646       /* `template <>' indicates a template specialization.  */
7647       if (token2.type == CPP_LESS
7648           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7649         cp_parser_explicit_specialization (parser);
7650       /* `template <' indicates a template declaration.  */
7651       else if (token2.type == CPP_LESS)
7652         cp_parser_template_declaration (parser, /*member_p=*/false);
7653       /* Anything else must be an explicit instantiation.  */
7654       else
7655         cp_parser_explicit_instantiation (parser);
7656     }
7657   /* If the next token is `export', then we have a template
7658      declaration.  */
7659   else if (token1.keyword == RID_EXPORT)
7660     cp_parser_template_declaration (parser, /*member_p=*/false);
7661   /* If the next token is `extern', 'static' or 'inline' and the one
7662      after that is `template', we have a GNU extended explicit
7663      instantiation directive.  */
7664   else if (cp_parser_allow_gnu_extensions_p (parser)
7665            && (token1.keyword == RID_EXTERN
7666                || token1.keyword == RID_STATIC
7667                || token1.keyword == RID_INLINE)
7668            && token2.keyword == RID_TEMPLATE)
7669     cp_parser_explicit_instantiation (parser);
7670   /* If the next token is `namespace', check for a named or unnamed
7671      namespace definition.  */
7672   else if (token1.keyword == RID_NAMESPACE
7673            && (/* A named namespace definition.  */
7674                (token2.type == CPP_NAME
7675                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7676                     != CPP_EQ))
7677                /* An unnamed namespace definition.  */
7678                || token2.type == CPP_OPEN_BRACE
7679                || token2.keyword == RID_ATTRIBUTE))
7680     cp_parser_namespace_definition (parser);
7681   /* Objective-C++ declaration/definition.  */
7682   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7683     cp_parser_objc_declaration (parser);
7684   /* We must have either a block declaration or a function
7685      definition.  */
7686   else
7687     /* Try to parse a block-declaration, or a function-definition.  */
7688     cp_parser_block_declaration (parser, /*statement_p=*/false);
7689
7690   /* Free any declarators allocated.  */
7691   obstack_free (&declarator_obstack, p);
7692 }
7693
7694 /* Parse a block-declaration.
7695
7696    block-declaration:
7697      simple-declaration
7698      asm-definition
7699      namespace-alias-definition
7700      using-declaration
7701      using-directive
7702
7703    GNU Extension:
7704
7705    block-declaration:
7706      __extension__ block-declaration
7707      label-declaration
7708
7709    C++0x Extension:
7710
7711    block-declaration:
7712      static_assert-declaration
7713
7714    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7715    part of a declaration-statement.  */
7716
7717 static void
7718 cp_parser_block_declaration (cp_parser *parser,
7719                              bool      statement_p)
7720 {
7721   cp_token *token1;
7722   int saved_pedantic;
7723
7724   /* Check for the `__extension__' keyword.  */
7725   if (cp_parser_extension_opt (parser, &saved_pedantic))
7726     {
7727       /* Parse the qualified declaration.  */
7728       cp_parser_block_declaration (parser, statement_p);
7729       /* Restore the PEDANTIC flag.  */
7730       pedantic = saved_pedantic;
7731
7732       return;
7733     }
7734
7735   /* Peek at the next token to figure out which kind of declaration is
7736      present.  */
7737   token1 = cp_lexer_peek_token (parser->lexer);
7738
7739   /* If the next keyword is `asm', we have an asm-definition.  */
7740   if (token1->keyword == RID_ASM)
7741     {
7742       if (statement_p)
7743         cp_parser_commit_to_tentative_parse (parser);
7744       cp_parser_asm_definition (parser);
7745     }
7746   /* If the next keyword is `namespace', we have a
7747      namespace-alias-definition.  */
7748   else if (token1->keyword == RID_NAMESPACE)
7749     cp_parser_namespace_alias_definition (parser);
7750   /* If the next keyword is `using', we have either a
7751      using-declaration or a using-directive.  */
7752   else if (token1->keyword == RID_USING)
7753     {
7754       cp_token *token2;
7755
7756       if (statement_p)
7757         cp_parser_commit_to_tentative_parse (parser);
7758       /* If the token after `using' is `namespace', then we have a
7759          using-directive.  */
7760       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7761       if (token2->keyword == RID_NAMESPACE)
7762         cp_parser_using_directive (parser);
7763       /* Otherwise, it's a using-declaration.  */
7764       else
7765         cp_parser_using_declaration (parser,
7766                                      /*access_declaration_p=*/false);
7767     }
7768   /* If the next keyword is `__label__' we have a label declaration.  */
7769   else if (token1->keyword == RID_LABEL)
7770     {
7771       if (statement_p)
7772         cp_parser_commit_to_tentative_parse (parser);
7773       cp_parser_label_declaration (parser);
7774     }
7775   /* If the next token is `static_assert' we have a static assertion.  */
7776   else if (token1->keyword == RID_STATIC_ASSERT)
7777     cp_parser_static_assert (parser, /*member_p=*/false);
7778   /* Anything else must be a simple-declaration.  */
7779   else
7780     cp_parser_simple_declaration (parser, !statement_p);
7781 }
7782
7783 /* Parse a simple-declaration.
7784
7785    simple-declaration:
7786      decl-specifier-seq [opt] init-declarator-list [opt] ;
7787
7788    init-declarator-list:
7789      init-declarator
7790      init-declarator-list , init-declarator
7791
7792    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7793    function-definition as a simple-declaration.  */
7794
7795 static void
7796 cp_parser_simple_declaration (cp_parser* parser,
7797                               bool function_definition_allowed_p)
7798 {
7799   cp_decl_specifier_seq decl_specifiers;
7800   int declares_class_or_enum;
7801   bool saw_declarator;
7802
7803   /* Defer access checks until we know what is being declared; the
7804      checks for names appearing in the decl-specifier-seq should be
7805      done as if we were in the scope of the thing being declared.  */
7806   push_deferring_access_checks (dk_deferred);
7807
7808   /* Parse the decl-specifier-seq.  We have to keep track of whether
7809      or not the decl-specifier-seq declares a named class or
7810      enumeration type, since that is the only case in which the
7811      init-declarator-list is allowed to be empty.
7812
7813      [dcl.dcl]
7814
7815      In a simple-declaration, the optional init-declarator-list can be
7816      omitted only when declaring a class or enumeration, that is when
7817      the decl-specifier-seq contains either a class-specifier, an
7818      elaborated-type-specifier, or an enum-specifier.  */
7819   cp_parser_decl_specifier_seq (parser,
7820                                 CP_PARSER_FLAGS_OPTIONAL,
7821                                 &decl_specifiers,
7822                                 &declares_class_or_enum);
7823   /* We no longer need to defer access checks.  */
7824   stop_deferring_access_checks ();
7825
7826   /* In a block scope, a valid declaration must always have a
7827      decl-specifier-seq.  By not trying to parse declarators, we can
7828      resolve the declaration/expression ambiguity more quickly.  */
7829   if (!function_definition_allowed_p
7830       && !decl_specifiers.any_specifiers_p)
7831     {
7832       cp_parser_error (parser, "expected declaration");
7833       goto done;
7834     }
7835
7836   /* If the next two tokens are both identifiers, the code is
7837      erroneous. The usual cause of this situation is code like:
7838
7839        T t;
7840
7841      where "T" should name a type -- but does not.  */
7842   if (!decl_specifiers.type
7843       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7844     {
7845       /* If parsing tentatively, we should commit; we really are
7846          looking at a declaration.  */
7847       cp_parser_commit_to_tentative_parse (parser);
7848       /* Give up.  */
7849       goto done;
7850     }
7851
7852   /* If we have seen at least one decl-specifier, and the next token
7853      is not a parenthesis, then we must be looking at a declaration.
7854      (After "int (" we might be looking at a functional cast.)  */
7855   if (decl_specifiers.any_specifiers_p
7856       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7857     cp_parser_commit_to_tentative_parse (parser);
7858
7859   /* Keep going until we hit the `;' at the end of the simple
7860      declaration.  */
7861   saw_declarator = false;
7862   while (cp_lexer_next_token_is_not (parser->lexer,
7863                                      CPP_SEMICOLON))
7864     {
7865       cp_token *token;
7866       bool function_definition_p;
7867       tree decl;
7868
7869       if (saw_declarator)
7870         {
7871           /* If we are processing next declarator, coma is expected */
7872           token = cp_lexer_peek_token (parser->lexer);
7873           gcc_assert (token->type == CPP_COMMA);
7874           cp_lexer_consume_token (parser->lexer);
7875         }
7876       else
7877         saw_declarator = true;
7878
7879       /* Parse the init-declarator.  */
7880       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7881                                         /*checks=*/NULL,
7882                                         function_definition_allowed_p,
7883                                         /*member_p=*/false,
7884                                         declares_class_or_enum,
7885                                         &function_definition_p);
7886       /* If an error occurred while parsing tentatively, exit quickly.
7887          (That usually happens when in the body of a function; each
7888          statement is treated as a declaration-statement until proven
7889          otherwise.)  */
7890       if (cp_parser_error_occurred (parser))
7891         goto done;
7892       /* Handle function definitions specially.  */
7893       if (function_definition_p)
7894         {
7895           /* If the next token is a `,', then we are probably
7896              processing something like:
7897
7898                void f() {}, *p;
7899
7900              which is erroneous.  */
7901           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7902             error ("mixing declarations and function-definitions is forbidden");
7903           /* Otherwise, we're done with the list of declarators.  */
7904           else
7905             {
7906               pop_deferring_access_checks ();
7907               return;
7908             }
7909         }
7910       /* The next token should be either a `,' or a `;'.  */
7911       token = cp_lexer_peek_token (parser->lexer);
7912       /* If it's a `,', there are more declarators to come.  */
7913       if (token->type == CPP_COMMA)
7914         /* will be consumed next time around */;
7915       /* If it's a `;', we are done.  */
7916       else if (token->type == CPP_SEMICOLON)
7917         break;
7918       /* Anything else is an error.  */
7919       else
7920         {
7921           /* If we have already issued an error message we don't need
7922              to issue another one.  */
7923           if (decl != error_mark_node
7924               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7925             cp_parser_error (parser, "expected %<,%> or %<;%>");
7926           /* Skip tokens until we reach the end of the statement.  */
7927           cp_parser_skip_to_end_of_statement (parser);
7928           /* If the next token is now a `;', consume it.  */
7929           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7930             cp_lexer_consume_token (parser->lexer);
7931           goto done;
7932         }
7933       /* After the first time around, a function-definition is not
7934          allowed -- even if it was OK at first.  For example:
7935
7936            int i, f() {}
7937
7938          is not valid.  */
7939       function_definition_allowed_p = false;
7940     }
7941
7942   /* Issue an error message if no declarators are present, and the
7943      decl-specifier-seq does not itself declare a class or
7944      enumeration.  */
7945   if (!saw_declarator)
7946     {
7947       if (cp_parser_declares_only_class_p (parser))
7948         shadow_tag (&decl_specifiers);
7949       /* Perform any deferred access checks.  */
7950       perform_deferred_access_checks ();
7951     }
7952
7953   /* Consume the `;'.  */
7954   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7955
7956  done:
7957   pop_deferring_access_checks ();
7958 }
7959
7960 /* Parse a decl-specifier-seq.
7961
7962    decl-specifier-seq:
7963      decl-specifier-seq [opt] decl-specifier
7964
7965    decl-specifier:
7966      storage-class-specifier
7967      type-specifier
7968      function-specifier
7969      friend
7970      typedef
7971
7972    GNU Extension:
7973
7974    decl-specifier:
7975      attributes
7976
7977    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7978
7979    The parser flags FLAGS is used to control type-specifier parsing.
7980
7981    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7982    flags:
7983
7984      1: one of the decl-specifiers is an elaborated-type-specifier
7985         (i.e., a type declaration)
7986      2: one of the decl-specifiers is an enum-specifier or a
7987         class-specifier (i.e., a type definition)
7988
7989    */
7990
7991 static void
7992 cp_parser_decl_specifier_seq (cp_parser* parser,
7993                               cp_parser_flags flags,
7994                               cp_decl_specifier_seq *decl_specs,
7995                               int* declares_class_or_enum)
7996 {
7997   bool constructor_possible_p = !parser->in_declarator_p;
7998
7999   /* Clear DECL_SPECS.  */
8000   clear_decl_specs (decl_specs);
8001
8002   /* Assume no class or enumeration type is declared.  */
8003   *declares_class_or_enum = 0;
8004
8005   /* Keep reading specifiers until there are no more to read.  */
8006   while (true)
8007     {
8008       bool constructor_p;
8009       bool found_decl_spec;
8010       cp_token *token;
8011
8012       /* Peek at the next token.  */
8013       token = cp_lexer_peek_token (parser->lexer);
8014       /* Handle attributes.  */
8015       if (token->keyword == RID_ATTRIBUTE)
8016         {
8017           /* Parse the attributes.  */
8018           decl_specs->attributes
8019             = chainon (decl_specs->attributes,
8020                        cp_parser_attributes_opt (parser));
8021           continue;
8022         }
8023       /* Assume we will find a decl-specifier keyword.  */
8024       found_decl_spec = true;
8025       /* If the next token is an appropriate keyword, we can simply
8026          add it to the list.  */
8027       switch (token->keyword)
8028         {
8029           /* decl-specifier:
8030                friend  */
8031         case RID_FRIEND:
8032           if (!at_class_scope_p ())
8033             {
8034               error ("%<friend%> used outside of class");
8035               cp_lexer_purge_token (parser->lexer);
8036             }
8037           else
8038             {
8039               ++decl_specs->specs[(int) ds_friend];
8040               /* Consume the token.  */
8041               cp_lexer_consume_token (parser->lexer);
8042             }
8043           break;
8044
8045           /* function-specifier:
8046                inline
8047                virtual
8048                explicit  */
8049         case RID_INLINE:
8050         case RID_VIRTUAL:
8051         case RID_EXPLICIT:
8052           cp_parser_function_specifier_opt (parser, decl_specs);
8053           break;
8054
8055           /* decl-specifier:
8056                typedef  */
8057         case RID_TYPEDEF:
8058           ++decl_specs->specs[(int) ds_typedef];
8059           /* Consume the token.  */
8060           cp_lexer_consume_token (parser->lexer);
8061           /* A constructor declarator cannot appear in a typedef.  */
8062           constructor_possible_p = false;
8063           /* The "typedef" keyword can only occur in a declaration; we
8064              may as well commit at this point.  */
8065           cp_parser_commit_to_tentative_parse (parser);
8066
8067           if (decl_specs->storage_class != sc_none)
8068             decl_specs->conflicting_specifiers_p = true;
8069           break;
8070
8071           /* storage-class-specifier:
8072                auto
8073                register
8074                static
8075                extern
8076                mutable
8077
8078              GNU Extension:
8079                thread  */
8080         case RID_AUTO:
8081         case RID_REGISTER:
8082         case RID_STATIC:
8083         case RID_EXTERN:
8084         case RID_MUTABLE:
8085           /* Consume the token.  */
8086           cp_lexer_consume_token (parser->lexer);
8087           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8088           break;
8089         case RID_THREAD:
8090           /* Consume the token.  */
8091           cp_lexer_consume_token (parser->lexer);
8092           ++decl_specs->specs[(int) ds_thread];
8093           break;
8094
8095         default:
8096           /* We did not yet find a decl-specifier yet.  */
8097           found_decl_spec = false;
8098           break;
8099         }
8100
8101       /* Constructors are a special case.  The `S' in `S()' is not a
8102          decl-specifier; it is the beginning of the declarator.  */
8103       constructor_p
8104         = (!found_decl_spec
8105            && constructor_possible_p
8106            && (cp_parser_constructor_declarator_p
8107                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8108
8109       /* If we don't have a DECL_SPEC yet, then we must be looking at
8110          a type-specifier.  */
8111       if (!found_decl_spec && !constructor_p)
8112         {
8113           int decl_spec_declares_class_or_enum;
8114           bool is_cv_qualifier;
8115           tree type_spec;
8116
8117           type_spec
8118             = cp_parser_type_specifier (parser, flags,
8119                                         decl_specs,
8120                                         /*is_declaration=*/true,
8121                                         &decl_spec_declares_class_or_enum,
8122                                         &is_cv_qualifier);
8123
8124           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8125
8126           /* If this type-specifier referenced a user-defined type
8127              (a typedef, class-name, etc.), then we can't allow any
8128              more such type-specifiers henceforth.
8129
8130              [dcl.spec]
8131
8132              The longest sequence of decl-specifiers that could
8133              possibly be a type name is taken as the
8134              decl-specifier-seq of a declaration.  The sequence shall
8135              be self-consistent as described below.
8136
8137              [dcl.type]
8138
8139              As a general rule, at most one type-specifier is allowed
8140              in the complete decl-specifier-seq of a declaration.  The
8141              only exceptions are the following:
8142
8143              -- const or volatile can be combined with any other
8144                 type-specifier.
8145
8146              -- signed or unsigned can be combined with char, long,
8147                 short, or int.
8148
8149              -- ..
8150
8151              Example:
8152
8153                typedef char* Pc;
8154                void g (const int Pc);
8155
8156              Here, Pc is *not* part of the decl-specifier seq; it's
8157              the declarator.  Therefore, once we see a type-specifier
8158              (other than a cv-qualifier), we forbid any additional
8159              user-defined types.  We *do* still allow things like `int
8160              int' to be considered a decl-specifier-seq, and issue the
8161              error message later.  */
8162           if (type_spec && !is_cv_qualifier)
8163             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8164           /* A constructor declarator cannot follow a type-specifier.  */
8165           if (type_spec)
8166             {
8167               constructor_possible_p = false;
8168               found_decl_spec = true;
8169             }
8170         }
8171
8172       /* If we still do not have a DECL_SPEC, then there are no more
8173          decl-specifiers.  */
8174       if (!found_decl_spec)
8175         break;
8176
8177       decl_specs->any_specifiers_p = true;
8178       /* After we see one decl-specifier, further decl-specifiers are
8179          always optional.  */
8180       flags |= CP_PARSER_FLAGS_OPTIONAL;
8181     }
8182
8183   cp_parser_check_decl_spec (decl_specs);
8184
8185   /* Don't allow a friend specifier with a class definition.  */
8186   if (decl_specs->specs[(int) ds_friend] != 0
8187       && (*declares_class_or_enum & 2))
8188     error ("class definition may not be declared a friend");
8189 }
8190
8191 /* Parse an (optional) storage-class-specifier.
8192
8193    storage-class-specifier:
8194      auto
8195      register
8196      static
8197      extern
8198      mutable
8199
8200    GNU Extension:
8201
8202    storage-class-specifier:
8203      thread
8204
8205    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8206
8207 static tree
8208 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8209 {
8210   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8211     {
8212     case RID_AUTO:
8213     case RID_REGISTER:
8214     case RID_STATIC:
8215     case RID_EXTERN:
8216     case RID_MUTABLE:
8217     case RID_THREAD:
8218       /* Consume the token.  */
8219       return cp_lexer_consume_token (parser->lexer)->u.value;
8220
8221     default:
8222       return NULL_TREE;
8223     }
8224 }
8225
8226 /* Parse an (optional) function-specifier.
8227
8228    function-specifier:
8229      inline
8230      virtual
8231      explicit
8232
8233    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8234    Updates DECL_SPECS, if it is non-NULL.  */
8235
8236 static tree
8237 cp_parser_function_specifier_opt (cp_parser* parser,
8238                                   cp_decl_specifier_seq *decl_specs)
8239 {
8240   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8241     {
8242     case RID_INLINE:
8243       if (decl_specs)
8244         ++decl_specs->specs[(int) ds_inline];
8245       break;
8246
8247     case RID_VIRTUAL:
8248       /* 14.5.2.3 [temp.mem]
8249
8250          A member function template shall not be virtual.  */
8251       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8252         error ("templates may not be %<virtual%>");
8253       else if (decl_specs)
8254         ++decl_specs->specs[(int) ds_virtual];
8255       break;
8256
8257     case RID_EXPLICIT:
8258       if (decl_specs)
8259         ++decl_specs->specs[(int) ds_explicit];
8260       break;
8261
8262     default:
8263       return NULL_TREE;
8264     }
8265
8266   /* Consume the token.  */
8267   return cp_lexer_consume_token (parser->lexer)->u.value;
8268 }
8269
8270 /* Parse a linkage-specification.
8271
8272    linkage-specification:
8273      extern string-literal { declaration-seq [opt] }
8274      extern string-literal declaration  */
8275
8276 static void
8277 cp_parser_linkage_specification (cp_parser* parser)
8278 {
8279   tree linkage;
8280
8281   /* Look for the `extern' keyword.  */
8282   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8283
8284   /* Look for the string-literal.  */
8285   linkage = cp_parser_string_literal (parser, false, false);
8286
8287   /* Transform the literal into an identifier.  If the literal is a
8288      wide-character string, or contains embedded NULs, then we can't
8289      handle it as the user wants.  */
8290   if (strlen (TREE_STRING_POINTER (linkage))
8291       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8292     {
8293       cp_parser_error (parser, "invalid linkage-specification");
8294       /* Assume C++ linkage.  */
8295       linkage = lang_name_cplusplus;
8296     }
8297   else
8298     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8299
8300   /* We're now using the new linkage.  */
8301   push_lang_context (linkage);
8302
8303   /* If the next token is a `{', then we're using the first
8304      production.  */
8305   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8306     {
8307       /* Consume the `{' token.  */
8308       cp_lexer_consume_token (parser->lexer);
8309       /* Parse the declarations.  */
8310       cp_parser_declaration_seq_opt (parser);
8311       /* Look for the closing `}'.  */
8312       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8313     }
8314   /* Otherwise, there's just one declaration.  */
8315   else
8316     {
8317       bool saved_in_unbraced_linkage_specification_p;
8318
8319       saved_in_unbraced_linkage_specification_p
8320         = parser->in_unbraced_linkage_specification_p;
8321       parser->in_unbraced_linkage_specification_p = true;
8322       cp_parser_declaration (parser);
8323       parser->in_unbraced_linkage_specification_p
8324         = saved_in_unbraced_linkage_specification_p;
8325     }
8326
8327   /* We're done with the linkage-specification.  */
8328   pop_lang_context ();
8329 }
8330
8331 /* Parse a static_assert-declaration.
8332
8333    static_assert-declaration:
8334      static_assert ( constant-expression , string-literal ) ; 
8335
8336    If MEMBER_P, this static_assert is a class member.  */
8337
8338 static void 
8339 cp_parser_static_assert(cp_parser *parser, bool member_p)
8340 {
8341   tree condition;
8342   tree message;
8343   cp_token *token;
8344   location_t saved_loc;
8345
8346   /* Peek at the `static_assert' token so we can keep track of exactly
8347      where the static assertion started.  */
8348   token = cp_lexer_peek_token (parser->lexer);
8349   saved_loc = token->location;
8350
8351   /* Look for the `static_assert' keyword.  */
8352   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8353                                   "`static_assert'"))
8354     return;
8355
8356   /*  We know we are in a static assertion; commit to any tentative
8357       parse.  */
8358   if (cp_parser_parsing_tentatively (parser))
8359     cp_parser_commit_to_tentative_parse (parser);
8360
8361   /* Parse the `(' starting the static assertion condition.  */
8362   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8363
8364   /* Parse the constant-expression.  */
8365   condition = 
8366     cp_parser_constant_expression (parser,
8367                                    /*allow_non_constant_p=*/false,
8368                                    /*non_constant_p=*/NULL);
8369
8370   /* Parse the separating `,'.  */
8371   cp_parser_require (parser, CPP_COMMA, "`,'");
8372
8373   /* Parse the string-literal message.  */
8374   message = cp_parser_string_literal (parser, 
8375                                       /*translate=*/false,
8376                                       /*wide_ok=*/true);
8377
8378   /* A `)' completes the static assertion.  */
8379   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8380     cp_parser_skip_to_closing_parenthesis (parser, 
8381                                            /*recovering=*/true, 
8382                                            /*or_comma=*/false,
8383                                            /*consume_paren=*/true);
8384
8385   /* A semicolon terminates the declaration.  */
8386   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8387
8388   /* Complete the static assertion, which may mean either processing 
8389      the static assert now or saving it for template instantiation.  */
8390   finish_static_assert (condition, message, saved_loc, member_p);
8391 }
8392
8393 /* Parse a `decltype' type. Returns the type. 
8394
8395    simple-type-specifier:
8396      decltype ( expression )  */
8397
8398 static tree
8399 cp_parser_decltype (cp_parser *parser)
8400 {
8401   tree expr;
8402   bool id_expression_or_member_access_p = false;
8403   const char *saved_message;
8404   bool saved_integral_constant_expression_p;
8405   bool saved_non_integral_constant_expression_p;
8406
8407   /* Look for the `decltype' token.  */
8408   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8409     return error_mark_node;
8410
8411   /* Types cannot be defined in a `decltype' expression.  Save away the
8412      old message.  */
8413   saved_message = parser->type_definition_forbidden_message;
8414
8415   /* And create the new one.  */
8416   parser->type_definition_forbidden_message
8417     = "types may not be defined in `decltype' expressions";
8418
8419   /* The restrictions on constant-expressions do not apply inside
8420      decltype expressions.  */
8421   saved_integral_constant_expression_p
8422     = parser->integral_constant_expression_p;
8423   saved_non_integral_constant_expression_p
8424     = parser->non_integral_constant_expression_p;
8425   parser->integral_constant_expression_p = false;
8426
8427   /* Do not actually evaluate the expression.  */
8428   ++skip_evaluation;
8429
8430   /* Parse the opening `('.  */
8431   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8432   
8433   /* First, try parsing an id-expression.  */
8434   cp_parser_parse_tentatively (parser);
8435   expr = cp_parser_id_expression (parser,
8436                                   /*template_keyword_p=*/false,
8437                                   /*check_dependency_p=*/true,
8438                                   /*template_p=*/NULL,
8439                                   /*declarator_p=*/false,
8440                                   /*optional_p=*/false);
8441
8442   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8443     {
8444       bool non_integral_constant_expression_p = false;
8445       tree id_expression = expr;
8446       cp_id_kind idk;
8447       const char *error_msg;
8448
8449       /* Lookup the name we got back from the id-expression.  */
8450       expr = cp_parser_lookup_name (parser, expr,
8451                                     none_type,
8452                                     /*is_template=*/false,
8453                                     /*is_namespace=*/false,
8454                                     /*check_dependency=*/true,
8455                                     /*ambiguous_decls=*/NULL);
8456       
8457       if (expr 
8458           && expr != error_mark_node
8459           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8460           && TREE_CODE (expr) != TYPE_DECL
8461           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8462         {
8463           /* Complete lookup of the id-expression.  */
8464           expr = (finish_id_expression
8465                   (id_expression, expr, parser->scope, &idk,
8466                    /*integral_constant_expression_p=*/false,
8467                    /*allow_non_integral_constant_expression_p=*/true,
8468                    &non_integral_constant_expression_p,
8469                    /*template_p=*/false,
8470                    /*done=*/true,
8471                    /*address_p=*/false,
8472                    /*template_arg_p=*/false,
8473                    &error_msg));
8474
8475           if (expr == error_mark_node)
8476             /* We found an id-expression, but it was something that we
8477                should not have found. This is an error, not something
8478                we can recover from, so note that we found an
8479                id-expression and we'll recover as gracefully as
8480                possible.  */
8481             id_expression_or_member_access_p = true;
8482         }
8483
8484       if (expr 
8485           && expr != error_mark_node
8486           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8487         /* We have an id-expression.  */
8488         id_expression_or_member_access_p = true;
8489     }
8490
8491   if (!id_expression_or_member_access_p)
8492     {
8493       /* Abort the id-expression parse.  */
8494       cp_parser_abort_tentative_parse (parser);
8495
8496       /* Parsing tentatively, again.  */
8497       cp_parser_parse_tentatively (parser);
8498
8499       /* Parse a class member access.  */
8500       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8501                                            /*cast_p=*/false,
8502                                            /*member_access_only_p=*/true);
8503
8504       if (expr 
8505           && expr != error_mark_node
8506           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8507         /* We have an id-expression.  */
8508         id_expression_or_member_access_p = true;
8509     }
8510
8511   if (id_expression_or_member_access_p)
8512     /* We have parsed the complete id-expression or member access.  */
8513     cp_parser_parse_definitely (parser);
8514   else
8515     {
8516       /* Abort our attempt to parse an id-expression or member access
8517          expression.  */
8518       cp_parser_abort_tentative_parse (parser);
8519
8520       /* Parse a full expression.  */
8521       expr = cp_parser_expression (parser, /*cast_p=*/false);
8522     }
8523
8524   /* Go back to evaluating expressions.  */
8525   --skip_evaluation;
8526
8527   /* Restore the old message and the integral constant expression
8528      flags.  */
8529   parser->type_definition_forbidden_message = saved_message;
8530   parser->integral_constant_expression_p
8531     = saved_integral_constant_expression_p;
8532   parser->non_integral_constant_expression_p
8533     = saved_non_integral_constant_expression_p;
8534
8535   if (expr == error_mark_node)
8536     {
8537       /* Skip everything up to the closing `)'.  */
8538       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8539                                              /*consume_paren=*/true);
8540       return error_mark_node;
8541     }
8542   
8543   /* Parse to the closing `)'.  */
8544   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8545     cp_parser_skip_to_closing_parenthesis (parser, true, false,
8546                                            /*consume_paren=*/true);
8547
8548   return finish_decltype_type (expr, id_expression_or_member_access_p);
8549 }
8550
8551 /* Special member functions [gram.special] */
8552
8553 /* Parse a conversion-function-id.
8554
8555    conversion-function-id:
8556      operator conversion-type-id
8557
8558    Returns an IDENTIFIER_NODE representing the operator.  */
8559
8560 static tree
8561 cp_parser_conversion_function_id (cp_parser* parser)
8562 {
8563   tree type;
8564   tree saved_scope;
8565   tree saved_qualifying_scope;
8566   tree saved_object_scope;
8567   tree pushed_scope = NULL_TREE;
8568
8569   /* Look for the `operator' token.  */
8570   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8571     return error_mark_node;
8572   /* When we parse the conversion-type-id, the current scope will be
8573      reset.  However, we need that information in able to look up the
8574      conversion function later, so we save it here.  */
8575   saved_scope = parser->scope;
8576   saved_qualifying_scope = parser->qualifying_scope;
8577   saved_object_scope = parser->object_scope;
8578   /* We must enter the scope of the class so that the names of
8579      entities declared within the class are available in the
8580      conversion-type-id.  For example, consider:
8581
8582        struct S {
8583          typedef int I;
8584          operator I();
8585        };
8586
8587        S::operator I() { ... }
8588
8589      In order to see that `I' is a type-name in the definition, we
8590      must be in the scope of `S'.  */
8591   if (saved_scope)
8592     pushed_scope = push_scope (saved_scope);
8593   /* Parse the conversion-type-id.  */
8594   type = cp_parser_conversion_type_id (parser);
8595   /* Leave the scope of the class, if any.  */
8596   if (pushed_scope)
8597     pop_scope (pushed_scope);
8598   /* Restore the saved scope.  */
8599   parser->scope = saved_scope;
8600   parser->qualifying_scope = saved_qualifying_scope;
8601   parser->object_scope = saved_object_scope;
8602   /* If the TYPE is invalid, indicate failure.  */
8603   if (type == error_mark_node)
8604     return error_mark_node;
8605   return mangle_conv_op_name_for_type (type);
8606 }
8607
8608 /* Parse a conversion-type-id:
8609
8610    conversion-type-id:
8611      type-specifier-seq conversion-declarator [opt]
8612
8613    Returns the TYPE specified.  */
8614
8615 static tree
8616 cp_parser_conversion_type_id (cp_parser* parser)
8617 {
8618   tree attributes;
8619   cp_decl_specifier_seq type_specifiers;
8620   cp_declarator *declarator;
8621   tree type_specified;
8622
8623   /* Parse the attributes.  */
8624   attributes = cp_parser_attributes_opt (parser);
8625   /* Parse the type-specifiers.  */
8626   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8627                                 &type_specifiers);
8628   /* If that didn't work, stop.  */
8629   if (type_specifiers.type == error_mark_node)
8630     return error_mark_node;
8631   /* Parse the conversion-declarator.  */
8632   declarator = cp_parser_conversion_declarator_opt (parser);
8633
8634   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8635                                     /*initialized=*/0, &attributes);
8636   if (attributes)
8637     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8638   return type_specified;
8639 }
8640
8641 /* Parse an (optional) conversion-declarator.
8642
8643    conversion-declarator:
8644      ptr-operator conversion-declarator [opt]
8645
8646    */
8647
8648 static cp_declarator *
8649 cp_parser_conversion_declarator_opt (cp_parser* parser)
8650 {
8651   enum tree_code code;
8652   tree class_type;
8653   cp_cv_quals cv_quals;
8654
8655   /* We don't know if there's a ptr-operator next, or not.  */
8656   cp_parser_parse_tentatively (parser);
8657   /* Try the ptr-operator.  */
8658   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8659   /* If it worked, look for more conversion-declarators.  */
8660   if (cp_parser_parse_definitely (parser))
8661     {
8662       cp_declarator *declarator;
8663
8664       /* Parse another optional declarator.  */
8665       declarator = cp_parser_conversion_declarator_opt (parser);
8666
8667       return cp_parser_make_indirect_declarator
8668         (code, class_type, cv_quals, declarator);
8669    }
8670
8671   return NULL;
8672 }
8673
8674 /* Parse an (optional) ctor-initializer.
8675
8676    ctor-initializer:
8677      : mem-initializer-list
8678
8679    Returns TRUE iff the ctor-initializer was actually present.  */
8680
8681 static bool
8682 cp_parser_ctor_initializer_opt (cp_parser* parser)
8683 {
8684   /* If the next token is not a `:', then there is no
8685      ctor-initializer.  */
8686   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8687     {
8688       /* Do default initialization of any bases and members.  */
8689       if (DECL_CONSTRUCTOR_P (current_function_decl))
8690         finish_mem_initializers (NULL_TREE);
8691
8692       return false;
8693     }
8694
8695   /* Consume the `:' token.  */
8696   cp_lexer_consume_token (parser->lexer);
8697   /* And the mem-initializer-list.  */
8698   cp_parser_mem_initializer_list (parser);
8699
8700   return true;
8701 }
8702
8703 /* Parse a mem-initializer-list.
8704
8705    mem-initializer-list:
8706      mem-initializer ... [opt]
8707      mem-initializer ... [opt] , mem-initializer-list  */
8708
8709 static void
8710 cp_parser_mem_initializer_list (cp_parser* parser)
8711 {
8712   tree mem_initializer_list = NULL_TREE;
8713
8714   /* Let the semantic analysis code know that we are starting the
8715      mem-initializer-list.  */
8716   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8717     error ("only constructors take base initializers");
8718
8719   /* Loop through the list.  */
8720   while (true)
8721     {
8722       tree mem_initializer;
8723
8724       /* Parse the mem-initializer.  */
8725       mem_initializer = cp_parser_mem_initializer (parser);
8726       /* If the next token is a `...', we're expanding member initializers. */
8727       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8728         {
8729           /* Consume the `...'. */
8730           cp_lexer_consume_token (parser->lexer);
8731
8732           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8733              can be expanded but members cannot. */
8734           if (mem_initializer != error_mark_node
8735               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8736             {
8737               error ("cannot expand initializer for member %<%D%>", 
8738                      TREE_PURPOSE (mem_initializer));
8739               mem_initializer = error_mark_node;
8740             }
8741
8742           /* Construct the pack expansion type. */
8743           if (mem_initializer != error_mark_node)
8744             mem_initializer = make_pack_expansion (mem_initializer);
8745         }
8746       /* Add it to the list, unless it was erroneous.  */
8747       if (mem_initializer != error_mark_node)
8748         {
8749           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8750           mem_initializer_list = mem_initializer;
8751         }
8752       /* If the next token is not a `,', we're done.  */
8753       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8754         break;
8755       /* Consume the `,' token.  */
8756       cp_lexer_consume_token (parser->lexer);
8757     }
8758
8759   /* Perform semantic analysis.  */
8760   if (DECL_CONSTRUCTOR_P (current_function_decl))
8761     finish_mem_initializers (mem_initializer_list);
8762 }
8763
8764 /* Parse a mem-initializer.
8765
8766    mem-initializer:
8767      mem-initializer-id ( expression-list [opt] )
8768
8769    GNU extension:
8770
8771    mem-initializer:
8772      ( expression-list [opt] )
8773
8774    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8775    class) or FIELD_DECL (for a non-static data member) to initialize;
8776    the TREE_VALUE is the expression-list.  An empty initialization
8777    list is represented by void_list_node.  */
8778
8779 static tree
8780 cp_parser_mem_initializer (cp_parser* parser)
8781 {
8782   tree mem_initializer_id;
8783   tree expression_list;
8784   tree member;
8785
8786   /* Find out what is being initialized.  */
8787   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8788     {
8789       pedwarn ("anachronistic old-style base class initializer");
8790       mem_initializer_id = NULL_TREE;
8791     }
8792   else
8793     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8794   member = expand_member_init (mem_initializer_id);
8795   if (member && !DECL_P (member))
8796     in_base_initializer = 1;
8797
8798   expression_list
8799     = cp_parser_parenthesized_expression_list (parser, false,
8800                                                /*cast_p=*/false,
8801                                                /*allow_expansion_p=*/true,
8802                                                /*non_constant_p=*/NULL);
8803   if (expression_list == error_mark_node)
8804     return error_mark_node;
8805   if (!expression_list)
8806     expression_list = void_type_node;
8807
8808   in_base_initializer = 0;
8809
8810   return member ? build_tree_list (member, expression_list) : error_mark_node;
8811 }
8812
8813 /* Parse a mem-initializer-id.
8814
8815    mem-initializer-id:
8816      :: [opt] nested-name-specifier [opt] class-name
8817      identifier
8818
8819    Returns a TYPE indicating the class to be initializer for the first
8820    production.  Returns an IDENTIFIER_NODE indicating the data member
8821    to be initialized for the second production.  */
8822
8823 static tree
8824 cp_parser_mem_initializer_id (cp_parser* parser)
8825 {
8826   bool global_scope_p;
8827   bool nested_name_specifier_p;
8828   bool template_p = false;
8829   tree id;
8830
8831   /* `typename' is not allowed in this context ([temp.res]).  */
8832   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8833     {
8834       error ("keyword %<typename%> not allowed in this context (a qualified "
8835              "member initializer is implicitly a type)");
8836       cp_lexer_consume_token (parser->lexer);
8837     }
8838   /* Look for the optional `::' operator.  */
8839   global_scope_p
8840     = (cp_parser_global_scope_opt (parser,
8841                                    /*current_scope_valid_p=*/false)
8842        != NULL_TREE);
8843   /* Look for the optional nested-name-specifier.  The simplest way to
8844      implement:
8845
8846        [temp.res]
8847
8848        The keyword `typename' is not permitted in a base-specifier or
8849        mem-initializer; in these contexts a qualified name that
8850        depends on a template-parameter is implicitly assumed to be a
8851        type name.
8852
8853      is to assume that we have seen the `typename' keyword at this
8854      point.  */
8855   nested_name_specifier_p
8856     = (cp_parser_nested_name_specifier_opt (parser,
8857                                             /*typename_keyword_p=*/true,
8858                                             /*check_dependency_p=*/true,
8859                                             /*type_p=*/true,
8860                                             /*is_declaration=*/true)
8861        != NULL_TREE);
8862   if (nested_name_specifier_p)
8863     template_p = cp_parser_optional_template_keyword (parser);
8864   /* If there is a `::' operator or a nested-name-specifier, then we
8865      are definitely looking for a class-name.  */
8866   if (global_scope_p || nested_name_specifier_p)
8867     return cp_parser_class_name (parser,
8868                                  /*typename_keyword_p=*/true,
8869                                  /*template_keyword_p=*/template_p,
8870                                  none_type,
8871                                  /*check_dependency_p=*/true,
8872                                  /*class_head_p=*/false,
8873                                  /*is_declaration=*/true);
8874   /* Otherwise, we could also be looking for an ordinary identifier.  */
8875   cp_parser_parse_tentatively (parser);
8876   /* Try a class-name.  */
8877   id = cp_parser_class_name (parser,
8878                              /*typename_keyword_p=*/true,
8879                              /*template_keyword_p=*/false,
8880                              none_type,
8881                              /*check_dependency_p=*/true,
8882                              /*class_head_p=*/false,
8883                              /*is_declaration=*/true);
8884   /* If we found one, we're done.  */
8885   if (cp_parser_parse_definitely (parser))
8886     return id;
8887   /* Otherwise, look for an ordinary identifier.  */
8888   return cp_parser_identifier (parser);
8889 }
8890
8891 /* Overloading [gram.over] */
8892
8893 /* Parse an operator-function-id.
8894
8895    operator-function-id:
8896      operator operator
8897
8898    Returns an IDENTIFIER_NODE for the operator which is a
8899    human-readable spelling of the identifier, e.g., `operator +'.  */
8900
8901 static tree
8902 cp_parser_operator_function_id (cp_parser* parser)
8903 {
8904   /* Look for the `operator' keyword.  */
8905   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8906     return error_mark_node;
8907   /* And then the name of the operator itself.  */
8908   return cp_parser_operator (parser);
8909 }
8910
8911 /* Parse an operator.
8912
8913    operator:
8914      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8915      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8916      || ++ -- , ->* -> () []
8917
8918    GNU Extensions:
8919
8920    operator:
8921      <? >? <?= >?=
8922
8923    Returns an IDENTIFIER_NODE for the operator which is a
8924    human-readable spelling of the identifier, e.g., `operator +'.  */
8925
8926 static tree
8927 cp_parser_operator (cp_parser* parser)
8928 {
8929   tree id = NULL_TREE;
8930   cp_token *token;
8931
8932   /* Peek at the next token.  */
8933   token = cp_lexer_peek_token (parser->lexer);
8934   /* Figure out which operator we have.  */
8935   switch (token->type)
8936     {
8937     case CPP_KEYWORD:
8938       {
8939         enum tree_code op;
8940
8941         /* The keyword should be either `new' or `delete'.  */
8942         if (token->keyword == RID_NEW)
8943           op = NEW_EXPR;
8944         else if (token->keyword == RID_DELETE)
8945           op = DELETE_EXPR;
8946         else
8947           break;
8948
8949         /* Consume the `new' or `delete' token.  */
8950         cp_lexer_consume_token (parser->lexer);
8951
8952         /* Peek at the next token.  */
8953         token = cp_lexer_peek_token (parser->lexer);
8954         /* If it's a `[' token then this is the array variant of the
8955            operator.  */
8956         if (token->type == CPP_OPEN_SQUARE)
8957           {
8958             /* Consume the `[' token.  */
8959             cp_lexer_consume_token (parser->lexer);
8960             /* Look for the `]' token.  */
8961             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8962             id = ansi_opname (op == NEW_EXPR
8963                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8964           }
8965         /* Otherwise, we have the non-array variant.  */
8966         else
8967           id = ansi_opname (op);
8968
8969         return id;
8970       }
8971
8972     case CPP_PLUS:
8973       id = ansi_opname (PLUS_EXPR);
8974       break;
8975
8976     case CPP_MINUS:
8977       id = ansi_opname (MINUS_EXPR);
8978       break;
8979
8980     case CPP_MULT:
8981       id = ansi_opname (MULT_EXPR);
8982       break;
8983
8984     case CPP_DIV:
8985       id = ansi_opname (TRUNC_DIV_EXPR);
8986       break;
8987
8988     case CPP_MOD:
8989       id = ansi_opname (TRUNC_MOD_EXPR);
8990       break;
8991
8992     case CPP_XOR:
8993       id = ansi_opname (BIT_XOR_EXPR);
8994       break;
8995
8996     case CPP_AND:
8997       id = ansi_opname (BIT_AND_EXPR);
8998       break;
8999
9000     case CPP_OR:
9001       id = ansi_opname (BIT_IOR_EXPR);
9002       break;
9003
9004     case CPP_COMPL:
9005       id = ansi_opname (BIT_NOT_EXPR);
9006       break;
9007
9008     case CPP_NOT:
9009       id = ansi_opname (TRUTH_NOT_EXPR);
9010       break;
9011
9012     case CPP_EQ:
9013       id = ansi_assopname (NOP_EXPR);
9014       break;
9015
9016     case CPP_LESS:
9017       id = ansi_opname (LT_EXPR);
9018       break;
9019
9020     case CPP_GREATER:
9021       id = ansi_opname (GT_EXPR);
9022       break;
9023
9024     case CPP_PLUS_EQ:
9025       id = ansi_assopname (PLUS_EXPR);
9026       break;
9027
9028     case CPP_MINUS_EQ:
9029       id = ansi_assopname (MINUS_EXPR);
9030       break;
9031
9032     case CPP_MULT_EQ:
9033       id = ansi_assopname (MULT_EXPR);
9034       break;
9035
9036     case CPP_DIV_EQ:
9037       id = ansi_assopname (TRUNC_DIV_EXPR);
9038       break;
9039
9040     case CPP_MOD_EQ:
9041       id = ansi_assopname (TRUNC_MOD_EXPR);
9042       break;
9043
9044     case CPP_XOR_EQ:
9045       id = ansi_assopname (BIT_XOR_EXPR);
9046       break;
9047
9048     case CPP_AND_EQ:
9049       id = ansi_assopname (BIT_AND_EXPR);
9050       break;
9051
9052     case CPP_OR_EQ:
9053       id = ansi_assopname (BIT_IOR_EXPR);
9054       break;
9055
9056     case CPP_LSHIFT:
9057       id = ansi_opname (LSHIFT_EXPR);
9058       break;
9059
9060     case CPP_RSHIFT:
9061       id = ansi_opname (RSHIFT_EXPR);
9062       break;
9063
9064     case CPP_LSHIFT_EQ:
9065       id = ansi_assopname (LSHIFT_EXPR);
9066       break;
9067
9068     case CPP_RSHIFT_EQ:
9069       id = ansi_assopname (RSHIFT_EXPR);
9070       break;
9071
9072     case CPP_EQ_EQ:
9073       id = ansi_opname (EQ_EXPR);
9074       break;
9075
9076     case CPP_NOT_EQ:
9077       id = ansi_opname (NE_EXPR);
9078       break;
9079
9080     case CPP_LESS_EQ:
9081       id = ansi_opname (LE_EXPR);
9082       break;
9083
9084     case CPP_GREATER_EQ:
9085       id = ansi_opname (GE_EXPR);
9086       break;
9087
9088     case CPP_AND_AND:
9089       id = ansi_opname (TRUTH_ANDIF_EXPR);
9090       break;
9091
9092     case CPP_OR_OR:
9093       id = ansi_opname (TRUTH_ORIF_EXPR);
9094       break;
9095
9096     case CPP_PLUS_PLUS:
9097       id = ansi_opname (POSTINCREMENT_EXPR);
9098       break;
9099
9100     case CPP_MINUS_MINUS:
9101       id = ansi_opname (PREDECREMENT_EXPR);
9102       break;
9103
9104     case CPP_COMMA:
9105       id = ansi_opname (COMPOUND_EXPR);
9106       break;
9107
9108     case CPP_DEREF_STAR:
9109       id = ansi_opname (MEMBER_REF);
9110       break;
9111
9112     case CPP_DEREF:
9113       id = ansi_opname (COMPONENT_REF);
9114       break;
9115
9116     case CPP_OPEN_PAREN:
9117       /* Consume the `('.  */
9118       cp_lexer_consume_token (parser->lexer);
9119       /* Look for the matching `)'.  */
9120       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9121       return ansi_opname (CALL_EXPR);
9122
9123     case CPP_OPEN_SQUARE:
9124       /* Consume the `['.  */
9125       cp_lexer_consume_token (parser->lexer);
9126       /* Look for the matching `]'.  */
9127       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9128       return ansi_opname (ARRAY_REF);
9129
9130     default:
9131       /* Anything else is an error.  */
9132       break;
9133     }
9134
9135   /* If we have selected an identifier, we need to consume the
9136      operator token.  */
9137   if (id)
9138     cp_lexer_consume_token (parser->lexer);
9139   /* Otherwise, no valid operator name was present.  */
9140   else
9141     {
9142       cp_parser_error (parser, "expected operator");
9143       id = error_mark_node;
9144     }
9145
9146   return id;
9147 }
9148
9149 /* Parse a template-declaration.
9150
9151    template-declaration:
9152      export [opt] template < template-parameter-list > declaration
9153
9154    If MEMBER_P is TRUE, this template-declaration occurs within a
9155    class-specifier.
9156
9157    The grammar rule given by the standard isn't correct.  What
9158    is really meant is:
9159
9160    template-declaration:
9161      export [opt] template-parameter-list-seq
9162        decl-specifier-seq [opt] init-declarator [opt] ;
9163      export [opt] template-parameter-list-seq
9164        function-definition
9165
9166    template-parameter-list-seq:
9167      template-parameter-list-seq [opt]
9168      template < template-parameter-list >  */
9169
9170 static void
9171 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9172 {
9173   /* Check for `export'.  */
9174   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9175     {
9176       /* Consume the `export' token.  */
9177       cp_lexer_consume_token (parser->lexer);
9178       /* Warn that we do not support `export'.  */
9179       warning (0, "keyword %<export%> not implemented, and will be ignored");
9180     }
9181
9182   cp_parser_template_declaration_after_export (parser, member_p);
9183 }
9184
9185 /* Parse a template-parameter-list.
9186
9187    template-parameter-list:
9188      template-parameter
9189      template-parameter-list , template-parameter
9190
9191    Returns a TREE_LIST.  Each node represents a template parameter.
9192    The nodes are connected via their TREE_CHAINs.  */
9193
9194 static tree
9195 cp_parser_template_parameter_list (cp_parser* parser)
9196 {
9197   tree parameter_list = NULL_TREE;
9198
9199   begin_template_parm_list ();
9200   while (true)
9201     {
9202       tree parameter;
9203       cp_token *token;
9204       bool is_non_type;
9205       bool is_parameter_pack;
9206
9207       /* Parse the template-parameter.  */
9208       parameter = cp_parser_template_parameter (parser, 
9209                                                 &is_non_type,
9210                                                 &is_parameter_pack);
9211       /* Add it to the list.  */
9212       if (parameter != error_mark_node)
9213         parameter_list = process_template_parm (parameter_list,
9214                                                 parameter,
9215                                                 is_non_type,
9216                                                 is_parameter_pack);
9217       else
9218        {
9219          tree err_parm = build_tree_list (parameter, parameter);
9220          TREE_VALUE (err_parm) = error_mark_node;
9221          parameter_list = chainon (parameter_list, err_parm);
9222        }
9223
9224       /* Peek at the next token.  */
9225       token = cp_lexer_peek_token (parser->lexer);
9226       /* If it's not a `,', we're done.  */
9227       if (token->type != CPP_COMMA)
9228         break;
9229       /* Otherwise, consume the `,' token.  */
9230       cp_lexer_consume_token (parser->lexer);
9231     }
9232
9233   return end_template_parm_list (parameter_list);
9234 }
9235
9236 /* Parse a template-parameter.
9237
9238    template-parameter:
9239      type-parameter
9240      parameter-declaration
9241
9242    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9243    the parameter.  The TREE_PURPOSE is the default value, if any.
9244    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9245    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9246    set to true iff this parameter is a parameter pack. */
9247
9248 static tree
9249 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9250                               bool *is_parameter_pack)
9251 {
9252   cp_token *token;
9253   cp_parameter_declarator *parameter_declarator;
9254   tree parm;
9255
9256   /* Assume it is a type parameter or a template parameter.  */
9257   *is_non_type = false;
9258   /* Assume it not a parameter pack. */
9259   *is_parameter_pack = false;
9260   /* Peek at the next token.  */
9261   token = cp_lexer_peek_token (parser->lexer);
9262   /* If it is `class' or `template', we have a type-parameter.  */
9263   if (token->keyword == RID_TEMPLATE)
9264     return cp_parser_type_parameter (parser, is_parameter_pack);
9265   /* If it is `class' or `typename' we do not know yet whether it is a
9266      type parameter or a non-type parameter.  Consider:
9267
9268        template <typename T, typename T::X X> ...
9269
9270      or:
9271
9272        template <class C, class D*> ...
9273
9274      Here, the first parameter is a type parameter, and the second is
9275      a non-type parameter.  We can tell by looking at the token after
9276      the identifier -- if it is a `,', `=', or `>' then we have a type
9277      parameter.  */
9278   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9279     {
9280       /* Peek at the token after `class' or `typename'.  */
9281       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9282       /* If it's an ellipsis, we have a template type parameter
9283          pack. */
9284       if (token->type == CPP_ELLIPSIS)
9285         return cp_parser_type_parameter (parser, is_parameter_pack);
9286       /* If it's an identifier, skip it.  */
9287       if (token->type == CPP_NAME)
9288         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9289       /* Now, see if the token looks like the end of a template
9290          parameter.  */
9291       if (token->type == CPP_COMMA
9292           || token->type == CPP_EQ
9293           || token->type == CPP_GREATER)
9294         return cp_parser_type_parameter (parser, is_parameter_pack);
9295     }
9296
9297   /* Otherwise, it is a non-type parameter.
9298
9299      [temp.param]
9300
9301      When parsing a default template-argument for a non-type
9302      template-parameter, the first non-nested `>' is taken as the end
9303      of the template parameter-list rather than a greater-than
9304      operator.  */
9305   *is_non_type = true;
9306   parameter_declarator
9307      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9308                                         /*parenthesized_p=*/NULL);
9309
9310   /* If the parameter declaration is marked as a parameter pack, set
9311      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9312      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9313      grokdeclarator. */
9314   if (parameter_declarator
9315       && parameter_declarator->declarator
9316       && parameter_declarator->declarator->parameter_pack_p)
9317     {
9318       *is_parameter_pack = true;
9319       parameter_declarator->declarator->parameter_pack_p = false;
9320     }
9321
9322   /* If the next token is an ellipsis, and we don't already have it
9323      marked as a parameter pack, then we have a parameter pack (that
9324      has no declarator); */
9325   if (!*is_parameter_pack
9326       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9327       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9328     {
9329       /* Consume the `...'. */
9330       cp_lexer_consume_token (parser->lexer);
9331       maybe_warn_variadic_templates ();
9332       
9333       *is_parameter_pack = true;
9334     }
9335
9336   parm = grokdeclarator (parameter_declarator->declarator,
9337                          &parameter_declarator->decl_specifiers,
9338                          PARM, /*initialized=*/0,
9339                          /*attrlist=*/NULL);
9340   if (parm == error_mark_node)
9341     return error_mark_node;
9342
9343   return build_tree_list (parameter_declarator->default_argument, parm);
9344 }
9345
9346 /* Parse a type-parameter.
9347
9348    type-parameter:
9349      class identifier [opt]
9350      class identifier [opt] = type-id
9351      typename identifier [opt]
9352      typename identifier [opt] = type-id
9353      template < template-parameter-list > class identifier [opt]
9354      template < template-parameter-list > class identifier [opt]
9355        = id-expression
9356
9357    GNU Extension (variadic templates):
9358
9359    type-parameter:
9360      class ... identifier [opt]
9361      typename ... identifier [opt]
9362
9363    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9364    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9365    the declaration of the parameter.
9366
9367    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9368
9369 static tree
9370 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9371 {
9372   cp_token *token;
9373   tree parameter;
9374
9375   /* Look for a keyword to tell us what kind of parameter this is.  */
9376   token = cp_parser_require (parser, CPP_KEYWORD,
9377                              "`class', `typename', or `template'");
9378   if (!token)
9379     return error_mark_node;
9380
9381   switch (token->keyword)
9382     {
9383     case RID_CLASS:
9384     case RID_TYPENAME:
9385       {
9386         tree identifier;
9387         tree default_argument;
9388
9389         /* If the next token is an ellipsis, we have a template
9390            argument pack. */
9391         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9392           {
9393             /* Consume the `...' token. */
9394             cp_lexer_consume_token (parser->lexer);
9395             maybe_warn_variadic_templates ();
9396
9397             *is_parameter_pack = true;
9398           }
9399
9400         /* If the next token is an identifier, then it names the
9401            parameter.  */
9402         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9403           identifier = cp_parser_identifier (parser);
9404         else
9405           identifier = NULL_TREE;
9406
9407         /* Create the parameter.  */
9408         parameter = finish_template_type_parm (class_type_node, identifier);
9409
9410         /* If the next token is an `=', we have a default argument.  */
9411         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9412           {
9413             /* Consume the `=' token.  */
9414             cp_lexer_consume_token (parser->lexer);
9415             /* Parse the default-argument.  */
9416             push_deferring_access_checks (dk_no_deferred);
9417             default_argument = cp_parser_type_id (parser);
9418
9419             /* Template parameter packs cannot have default
9420                arguments. */
9421             if (*is_parameter_pack)
9422               {
9423                 if (identifier)
9424                   error ("template parameter pack %qD cannot have a default argument", 
9425                          identifier);
9426                 else
9427                   error ("template parameter packs cannot have default arguments");
9428                 default_argument = NULL_TREE;
9429               }
9430             pop_deferring_access_checks ();
9431           }
9432         else
9433           default_argument = NULL_TREE;
9434
9435         /* Create the combined representation of the parameter and the
9436            default argument.  */
9437         parameter = build_tree_list (default_argument, parameter);
9438       }
9439       break;
9440
9441     case RID_TEMPLATE:
9442       {
9443         tree parameter_list;
9444         tree identifier;
9445         tree default_argument;
9446
9447         /* Look for the `<'.  */
9448         cp_parser_require (parser, CPP_LESS, "`<'");
9449         /* Parse the template-parameter-list.  */
9450         parameter_list = cp_parser_template_parameter_list (parser);
9451         /* Look for the `>'.  */
9452         cp_parser_require (parser, CPP_GREATER, "`>'");
9453         /* Look for the `class' keyword.  */
9454         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9455         /* If the next token is an ellipsis, we have a template
9456            argument pack. */
9457         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9458           {
9459             /* Consume the `...' token. */
9460             cp_lexer_consume_token (parser->lexer);
9461             maybe_warn_variadic_templates ();
9462
9463             *is_parameter_pack = true;
9464           }
9465         /* If the next token is an `=', then there is a
9466            default-argument.  If the next token is a `>', we are at
9467            the end of the parameter-list.  If the next token is a `,',
9468            then we are at the end of this parameter.  */
9469         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9470             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9471             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9472           {
9473             identifier = cp_parser_identifier (parser);
9474             /* Treat invalid names as if the parameter were nameless.  */
9475             if (identifier == error_mark_node)
9476               identifier = NULL_TREE;
9477           }
9478         else
9479           identifier = NULL_TREE;
9480
9481         /* Create the template parameter.  */
9482         parameter = finish_template_template_parm (class_type_node,
9483                                                    identifier);
9484
9485         /* If the next token is an `=', then there is a
9486            default-argument.  */
9487         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9488           {
9489             bool is_template;
9490
9491             /* Consume the `='.  */
9492             cp_lexer_consume_token (parser->lexer);
9493             /* Parse the id-expression.  */
9494             push_deferring_access_checks (dk_no_deferred);
9495             default_argument
9496               = cp_parser_id_expression (parser,
9497                                          /*template_keyword_p=*/false,
9498                                          /*check_dependency_p=*/true,
9499                                          /*template_p=*/&is_template,
9500                                          /*declarator_p=*/false,
9501                                          /*optional_p=*/false);
9502             if (TREE_CODE (default_argument) == TYPE_DECL)
9503               /* If the id-expression was a template-id that refers to
9504                  a template-class, we already have the declaration here,
9505                  so no further lookup is needed.  */
9506                  ;
9507             else
9508               /* Look up the name.  */
9509               default_argument
9510                 = cp_parser_lookup_name (parser, default_argument,
9511                                          none_type,
9512                                          /*is_template=*/is_template,
9513                                          /*is_namespace=*/false,
9514                                          /*check_dependency=*/true,
9515                                          /*ambiguous_decls=*/NULL);
9516             /* See if the default argument is valid.  */
9517             default_argument
9518               = check_template_template_default_arg (default_argument);
9519
9520             /* Template parameter packs cannot have default
9521                arguments. */
9522             if (*is_parameter_pack)
9523               {
9524                 if (identifier)
9525                   error ("template parameter pack %qD cannot have a default argument", 
9526                          identifier);
9527                 else
9528                   error ("template parameter packs cannot have default arguments");
9529                 default_argument = NULL_TREE;
9530               }
9531             pop_deferring_access_checks ();
9532           }
9533         else
9534           default_argument = NULL_TREE;
9535
9536         /* Create the combined representation of the parameter and the
9537            default argument.  */
9538         parameter = build_tree_list (default_argument, parameter);
9539       }
9540       break;
9541
9542     default:
9543       gcc_unreachable ();
9544       break;
9545     }
9546
9547   return parameter;
9548 }
9549
9550 /* Parse a template-id.
9551
9552    template-id:
9553      template-name < template-argument-list [opt] >
9554
9555    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9556    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9557    returned.  Otherwise, if the template-name names a function, or set
9558    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9559    names a class, returns a TYPE_DECL for the specialization.
9560
9561    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9562    uninstantiated templates.  */
9563
9564 static tree
9565 cp_parser_template_id (cp_parser *parser,
9566                        bool template_keyword_p,
9567                        bool check_dependency_p,
9568                        bool is_declaration)
9569 {
9570   int i;
9571   tree template;
9572   tree arguments;
9573   tree template_id;
9574   cp_token_position start_of_id = 0;
9575   deferred_access_check *chk;
9576   VEC (deferred_access_check,gc) *access_check;
9577   cp_token *next_token, *next_token_2;
9578   bool is_identifier;
9579
9580   /* If the next token corresponds to a template-id, there is no need
9581      to reparse it.  */
9582   next_token = cp_lexer_peek_token (parser->lexer);
9583   if (next_token->type == CPP_TEMPLATE_ID)
9584     {
9585       struct tree_check *check_value;
9586
9587       /* Get the stored value.  */
9588       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9589       /* Perform any access checks that were deferred.  */
9590       access_check = check_value->checks;
9591       if (access_check)
9592         {
9593           for (i = 0 ;
9594                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9595                ++i)
9596             {
9597               perform_or_defer_access_check (chk->binfo,
9598                                              chk->decl,
9599                                              chk->diag_decl);
9600             }
9601         }
9602       /* Return the stored value.  */
9603       return check_value->value;
9604     }
9605
9606   /* Avoid performing name lookup if there is no possibility of
9607      finding a template-id.  */
9608   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9609       || (next_token->type == CPP_NAME
9610           && !cp_parser_nth_token_starts_template_argument_list_p
9611                (parser, 2)))
9612     {
9613       cp_parser_error (parser, "expected template-id");
9614       return error_mark_node;
9615     }
9616
9617   /* Remember where the template-id starts.  */
9618   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9619     start_of_id = cp_lexer_token_position (parser->lexer, false);
9620
9621   push_deferring_access_checks (dk_deferred);
9622
9623   /* Parse the template-name.  */
9624   is_identifier = false;
9625   template = cp_parser_template_name (parser, template_keyword_p,
9626                                       check_dependency_p,
9627                                       is_declaration,
9628                                       &is_identifier);
9629   if (template == error_mark_node || is_identifier)
9630     {
9631       pop_deferring_access_checks ();
9632       return template;
9633     }
9634
9635   /* If we find the sequence `[:' after a template-name, it's probably
9636      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9637      parse correctly the argument list.  */
9638   next_token = cp_lexer_peek_token (parser->lexer);
9639   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9640   if (next_token->type == CPP_OPEN_SQUARE
9641       && next_token->flags & DIGRAPH
9642       && next_token_2->type == CPP_COLON
9643       && !(next_token_2->flags & PREV_WHITE))
9644     {
9645       cp_parser_parse_tentatively (parser);
9646       /* Change `:' into `::'.  */
9647       next_token_2->type = CPP_SCOPE;
9648       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9649          CPP_LESS.  */
9650       cp_lexer_consume_token (parser->lexer);
9651       /* Parse the arguments.  */
9652       arguments = cp_parser_enclosed_template_argument_list (parser);
9653       if (!cp_parser_parse_definitely (parser))
9654         {
9655           /* If we couldn't parse an argument list, then we revert our changes
9656              and return simply an error. Maybe this is not a template-id
9657              after all.  */
9658           next_token_2->type = CPP_COLON;
9659           cp_parser_error (parser, "expected %<<%>");
9660           pop_deferring_access_checks ();
9661           return error_mark_node;
9662         }
9663       /* Otherwise, emit an error about the invalid digraph, but continue
9664          parsing because we got our argument list.  */
9665       pedwarn ("%<<::%> cannot begin a template-argument list");
9666       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9667               "between %<<%> and %<::%>");
9668       if (!flag_permissive)
9669         {
9670           static bool hint;
9671           if (!hint)
9672             {
9673               inform ("(if you use -fpermissive G++ will accept your code)");
9674               hint = true;
9675             }
9676         }
9677     }
9678   else
9679     {
9680       /* Look for the `<' that starts the template-argument-list.  */
9681       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9682         {
9683           pop_deferring_access_checks ();
9684           return error_mark_node;
9685         }
9686       /* Parse the arguments.  */
9687       arguments = cp_parser_enclosed_template_argument_list (parser);
9688     }
9689
9690   /* Build a representation of the specialization.  */
9691   if (TREE_CODE (template) == IDENTIFIER_NODE)
9692     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9693   else if (DECL_CLASS_TEMPLATE_P (template)
9694            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9695     {
9696       bool entering_scope;
9697       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9698          template (rather than some instantiation thereof) only if
9699          is not nested within some other construct.  For example, in
9700          "template <typename T> void f(T) { A<T>::", A<T> is just an
9701          instantiation of A.  */
9702       entering_scope = (template_parm_scope_p ()
9703                         && cp_lexer_next_token_is (parser->lexer,
9704                                                    CPP_SCOPE));
9705       template_id
9706         = finish_template_type (template, arguments, entering_scope);
9707     }
9708   else
9709     {
9710       /* If it's not a class-template or a template-template, it should be
9711          a function-template.  */
9712       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9713                    || TREE_CODE (template) == OVERLOAD
9714                    || BASELINK_P (template)));
9715
9716       template_id = lookup_template_function (template, arguments);
9717     }
9718
9719   /* If parsing tentatively, replace the sequence of tokens that makes
9720      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9721      should we re-parse the token stream, we will not have to repeat
9722      the effort required to do the parse, nor will we issue duplicate
9723      error messages about problems during instantiation of the
9724      template.  */
9725   if (start_of_id)
9726     {
9727       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9728
9729       /* Reset the contents of the START_OF_ID token.  */
9730       token->type = CPP_TEMPLATE_ID;
9731       /* Retrieve any deferred checks.  Do not pop this access checks yet
9732          so the memory will not be reclaimed during token replacing below.  */
9733       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9734       token->u.tree_check_value->value = template_id;
9735       token->u.tree_check_value->checks = get_deferred_access_checks ();
9736       token->keyword = RID_MAX;
9737
9738       /* Purge all subsequent tokens.  */
9739       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9740
9741       /* ??? Can we actually assume that, if template_id ==
9742          error_mark_node, we will have issued a diagnostic to the
9743          user, as opposed to simply marking the tentative parse as
9744          failed?  */
9745       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9746         error ("parse error in template argument list");
9747     }
9748
9749   pop_deferring_access_checks ();
9750   return template_id;
9751 }
9752
9753 /* Parse a template-name.
9754
9755    template-name:
9756      identifier
9757
9758    The standard should actually say:
9759
9760    template-name:
9761      identifier
9762      operator-function-id
9763
9764    A defect report has been filed about this issue.
9765
9766    A conversion-function-id cannot be a template name because they cannot
9767    be part of a template-id. In fact, looking at this code:
9768
9769    a.operator K<int>()
9770
9771    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9772    It is impossible to call a templated conversion-function-id with an
9773    explicit argument list, since the only allowed template parameter is
9774    the type to which it is converting.
9775
9776    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9777    `template' keyword, in a construction like:
9778
9779      T::template f<3>()
9780
9781    In that case `f' is taken to be a template-name, even though there
9782    is no way of knowing for sure.
9783
9784    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9785    name refers to a set of overloaded functions, at least one of which
9786    is a template, or an IDENTIFIER_NODE with the name of the template,
9787    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9788    names are looked up inside uninstantiated templates.  */
9789
9790 static tree
9791 cp_parser_template_name (cp_parser* parser,
9792                          bool template_keyword_p,
9793                          bool check_dependency_p,
9794                          bool is_declaration,
9795                          bool *is_identifier)
9796 {
9797   tree identifier;
9798   tree decl;
9799   tree fns;
9800
9801   /* If the next token is `operator', then we have either an
9802      operator-function-id or a conversion-function-id.  */
9803   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9804     {
9805       /* We don't know whether we're looking at an
9806          operator-function-id or a conversion-function-id.  */
9807       cp_parser_parse_tentatively (parser);
9808       /* Try an operator-function-id.  */
9809       identifier = cp_parser_operator_function_id (parser);
9810       /* If that didn't work, try a conversion-function-id.  */
9811       if (!cp_parser_parse_definitely (parser))
9812         {
9813           cp_parser_error (parser, "expected template-name");
9814           return error_mark_node;
9815         }
9816     }
9817   /* Look for the identifier.  */
9818   else
9819     identifier = cp_parser_identifier (parser);
9820
9821   /* If we didn't find an identifier, we don't have a template-id.  */
9822   if (identifier == error_mark_node)
9823     return error_mark_node;
9824
9825   /* If the name immediately followed the `template' keyword, then it
9826      is a template-name.  However, if the next token is not `<', then
9827      we do not treat it as a template-name, since it is not being used
9828      as part of a template-id.  This enables us to handle constructs
9829      like:
9830
9831        template <typename T> struct S { S(); };
9832        template <typename T> S<T>::S();
9833
9834      correctly.  We would treat `S' as a template -- if it were `S<T>'
9835      -- but we do not if there is no `<'.  */
9836
9837   if (processing_template_decl
9838       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9839     {
9840       /* In a declaration, in a dependent context, we pretend that the
9841          "template" keyword was present in order to improve error
9842          recovery.  For example, given:
9843
9844            template <typename T> void f(T::X<int>);
9845
9846          we want to treat "X<int>" as a template-id.  */
9847       if (is_declaration
9848           && !template_keyword_p
9849           && parser->scope && TYPE_P (parser->scope)
9850           && check_dependency_p
9851           && dependent_type_p (parser->scope)
9852           /* Do not do this for dtors (or ctors), since they never
9853              need the template keyword before their name.  */
9854           && !constructor_name_p (identifier, parser->scope))
9855         {
9856           cp_token_position start = 0;
9857
9858           /* Explain what went wrong.  */
9859           error ("non-template %qD used as template", identifier);
9860           inform ("use %<%T::template %D%> to indicate that it is a template",
9861                   parser->scope, identifier);
9862           /* If parsing tentatively, find the location of the "<" token.  */
9863           if (cp_parser_simulate_error (parser))
9864             start = cp_lexer_token_position (parser->lexer, true);
9865           /* Parse the template arguments so that we can issue error
9866              messages about them.  */
9867           cp_lexer_consume_token (parser->lexer);
9868           cp_parser_enclosed_template_argument_list (parser);
9869           /* Skip tokens until we find a good place from which to
9870              continue parsing.  */
9871           cp_parser_skip_to_closing_parenthesis (parser,
9872                                                  /*recovering=*/true,
9873                                                  /*or_comma=*/true,
9874                                                  /*consume_paren=*/false);
9875           /* If parsing tentatively, permanently remove the
9876              template argument list.  That will prevent duplicate
9877              error messages from being issued about the missing
9878              "template" keyword.  */
9879           if (start)
9880             cp_lexer_purge_tokens_after (parser->lexer, start);
9881           if (is_identifier)
9882             *is_identifier = true;
9883           return identifier;
9884         }
9885
9886       /* If the "template" keyword is present, then there is generally
9887          no point in doing name-lookup, so we just return IDENTIFIER.
9888          But, if the qualifying scope is non-dependent then we can
9889          (and must) do name-lookup normally.  */
9890       if (template_keyword_p
9891           && (!parser->scope
9892               || (TYPE_P (parser->scope)
9893                   && dependent_type_p (parser->scope))))
9894         return identifier;
9895     }
9896
9897   /* Look up the name.  */
9898   decl = cp_parser_lookup_name (parser, identifier,
9899                                 none_type,
9900                                 /*is_template=*/false,
9901                                 /*is_namespace=*/false,
9902                                 check_dependency_p,
9903                                 /*ambiguous_decls=*/NULL);
9904   decl = maybe_get_template_decl_from_type_decl (decl);
9905
9906   /* If DECL is a template, then the name was a template-name.  */
9907   if (TREE_CODE (decl) == TEMPLATE_DECL)
9908     ;
9909   else
9910     {
9911       tree fn = NULL_TREE;
9912
9913       /* The standard does not explicitly indicate whether a name that
9914          names a set of overloaded declarations, some of which are
9915          templates, is a template-name.  However, such a name should
9916          be a template-name; otherwise, there is no way to form a
9917          template-id for the overloaded templates.  */
9918       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9919       if (TREE_CODE (fns) == OVERLOAD)
9920         for (fn = fns; fn; fn = OVL_NEXT (fn))
9921           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9922             break;
9923
9924       if (!fn)
9925         {
9926           /* The name does not name a template.  */
9927           cp_parser_error (parser, "expected template-name");
9928           return error_mark_node;
9929         }
9930     }
9931
9932   /* If DECL is dependent, and refers to a function, then just return
9933      its name; we will look it up again during template instantiation.  */
9934   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9935     {
9936       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9937       if (TYPE_P (scope) && dependent_type_p (scope))
9938         return identifier;
9939     }
9940
9941   return decl;
9942 }
9943
9944 /* Parse a template-argument-list.
9945
9946    template-argument-list:
9947      template-argument ... [opt]
9948      template-argument-list , template-argument ... [opt]
9949
9950    Returns a TREE_VEC containing the arguments.  */
9951
9952 static tree
9953 cp_parser_template_argument_list (cp_parser* parser)
9954 {
9955   tree fixed_args[10];
9956   unsigned n_args = 0;
9957   unsigned alloced = 10;
9958   tree *arg_ary = fixed_args;
9959   tree vec;
9960   bool saved_in_template_argument_list_p;
9961   bool saved_ice_p;
9962   bool saved_non_ice_p;
9963
9964   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9965   parser->in_template_argument_list_p = true;
9966   /* Even if the template-id appears in an integral
9967      constant-expression, the contents of the argument list do
9968      not.  */
9969   saved_ice_p = parser->integral_constant_expression_p;
9970   parser->integral_constant_expression_p = false;
9971   saved_non_ice_p = parser->non_integral_constant_expression_p;
9972   parser->non_integral_constant_expression_p = false;
9973   /* Parse the arguments.  */
9974   do
9975     {
9976       tree argument;
9977
9978       if (n_args)
9979         /* Consume the comma.  */
9980         cp_lexer_consume_token (parser->lexer);
9981
9982       /* Parse the template-argument.  */
9983       argument = cp_parser_template_argument (parser);
9984
9985       /* If the next token is an ellipsis, we're expanding a template
9986          argument pack. */
9987       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9988         {
9989           /* Consume the `...' token. */
9990           cp_lexer_consume_token (parser->lexer);
9991
9992           /* Make the argument into a TYPE_PACK_EXPANSION or
9993              EXPR_PACK_EXPANSION. */
9994           argument = make_pack_expansion (argument);
9995         }
9996
9997       if (n_args == alloced)
9998         {
9999           alloced *= 2;
10000
10001           if (arg_ary == fixed_args)
10002             {
10003               arg_ary = XNEWVEC (tree, alloced);
10004               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10005             }
10006           else
10007             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10008         }
10009       arg_ary[n_args++] = argument;
10010     }
10011   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10012
10013   vec = make_tree_vec (n_args);
10014
10015   while (n_args--)
10016     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10017
10018   if (arg_ary != fixed_args)
10019     free (arg_ary);
10020   parser->non_integral_constant_expression_p = saved_non_ice_p;
10021   parser->integral_constant_expression_p = saved_ice_p;
10022   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10023   return vec;
10024 }
10025
10026 /* Parse a template-argument.
10027
10028    template-argument:
10029      assignment-expression
10030      type-id
10031      id-expression
10032
10033    The representation is that of an assignment-expression, type-id, or
10034    id-expression -- except that the qualified id-expression is
10035    evaluated, so that the value returned is either a DECL or an
10036    OVERLOAD.
10037
10038    Although the standard says "assignment-expression", it forbids
10039    throw-expressions or assignments in the template argument.
10040    Therefore, we use "conditional-expression" instead.  */
10041
10042 static tree
10043 cp_parser_template_argument (cp_parser* parser)
10044 {
10045   tree argument;
10046   bool template_p;
10047   bool address_p;
10048   bool maybe_type_id = false;
10049   cp_token *token;
10050   cp_id_kind idk;
10051
10052   /* There's really no way to know what we're looking at, so we just
10053      try each alternative in order.
10054
10055        [temp.arg]
10056
10057        In a template-argument, an ambiguity between a type-id and an
10058        expression is resolved to a type-id, regardless of the form of
10059        the corresponding template-parameter.
10060
10061      Therefore, we try a type-id first.  */
10062   cp_parser_parse_tentatively (parser);
10063   argument = cp_parser_type_id (parser);
10064   /* If there was no error parsing the type-id but the next token is a '>>',
10065      we probably found a typo for '> >'. But there are type-id which are
10066      also valid expressions. For instance:
10067
10068      struct X { int operator >> (int); };
10069      template <int V> struct Foo {};
10070      Foo<X () >> 5> r;
10071
10072      Here 'X()' is a valid type-id of a function type, but the user just
10073      wanted to write the expression "X() >> 5". Thus, we remember that we
10074      found a valid type-id, but we still try to parse the argument as an
10075      expression to see what happens.  */
10076   if (!cp_parser_error_occurred (parser)
10077       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10078     {
10079       maybe_type_id = true;
10080       cp_parser_abort_tentative_parse (parser);
10081     }
10082   else
10083     {
10084       /* If the next token isn't a `,' or a `>', then this argument wasn't
10085       really finished. This means that the argument is not a valid
10086       type-id.  */
10087       if (!cp_parser_next_token_ends_template_argument_p (parser))
10088         cp_parser_error (parser, "expected template-argument");
10089       /* If that worked, we're done.  */
10090       if (cp_parser_parse_definitely (parser))
10091         return argument;
10092     }
10093   /* We're still not sure what the argument will be.  */
10094   cp_parser_parse_tentatively (parser);
10095   /* Try a template.  */
10096   argument = cp_parser_id_expression (parser,
10097                                       /*template_keyword_p=*/false,
10098                                       /*check_dependency_p=*/true,
10099                                       &template_p,
10100                                       /*declarator_p=*/false,
10101                                       /*optional_p=*/false);
10102   /* If the next token isn't a `,' or a `>', then this argument wasn't
10103      really finished.  */
10104   if (!cp_parser_next_token_ends_template_argument_p (parser))
10105     cp_parser_error (parser, "expected template-argument");
10106   if (!cp_parser_error_occurred (parser))
10107     {
10108       /* Figure out what is being referred to.  If the id-expression
10109          was for a class template specialization, then we will have a
10110          TYPE_DECL at this point.  There is no need to do name lookup
10111          at this point in that case.  */
10112       if (TREE_CODE (argument) != TYPE_DECL)
10113         argument = cp_parser_lookup_name (parser, argument,
10114                                           none_type,
10115                                           /*is_template=*/template_p,
10116                                           /*is_namespace=*/false,
10117                                           /*check_dependency=*/true,
10118                                           /*ambiguous_decls=*/NULL);
10119       if (TREE_CODE (argument) != TEMPLATE_DECL
10120           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10121         cp_parser_error (parser, "expected template-name");
10122     }
10123   if (cp_parser_parse_definitely (parser))
10124     return argument;
10125   /* It must be a non-type argument.  There permitted cases are given
10126      in [temp.arg.nontype]:
10127
10128      -- an integral constant-expression of integral or enumeration
10129         type; or
10130
10131      -- the name of a non-type template-parameter; or
10132
10133      -- the name of an object or function with external linkage...
10134
10135      -- the address of an object or function with external linkage...
10136
10137      -- a pointer to member...  */
10138   /* Look for a non-type template parameter.  */
10139   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10140     {
10141       cp_parser_parse_tentatively (parser);
10142       argument = cp_parser_primary_expression (parser,
10143                                                /*adress_p=*/false,
10144                                                /*cast_p=*/false,
10145                                                /*template_arg_p=*/true,
10146                                                &idk);
10147       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10148           || !cp_parser_next_token_ends_template_argument_p (parser))
10149         cp_parser_simulate_error (parser);
10150       if (cp_parser_parse_definitely (parser))
10151         return argument;
10152     }
10153
10154   /* If the next token is "&", the argument must be the address of an
10155      object or function with external linkage.  */
10156   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10157   if (address_p)
10158     cp_lexer_consume_token (parser->lexer);
10159   /* See if we might have an id-expression.  */
10160   token = cp_lexer_peek_token (parser->lexer);
10161   if (token->type == CPP_NAME
10162       || token->keyword == RID_OPERATOR
10163       || token->type == CPP_SCOPE
10164       || token->type == CPP_TEMPLATE_ID
10165       || token->type == CPP_NESTED_NAME_SPECIFIER)
10166     {
10167       cp_parser_parse_tentatively (parser);
10168       argument = cp_parser_primary_expression (parser,
10169                                                address_p,
10170                                                /*cast_p=*/false,
10171                                                /*template_arg_p=*/true,
10172                                                &idk);
10173       if (cp_parser_error_occurred (parser)
10174           || !cp_parser_next_token_ends_template_argument_p (parser))
10175         cp_parser_abort_tentative_parse (parser);
10176       else
10177         {
10178           if (TREE_CODE (argument) == INDIRECT_REF)
10179             {
10180               gcc_assert (REFERENCE_REF_P (argument));
10181               argument = TREE_OPERAND (argument, 0);
10182             }
10183
10184           if (TREE_CODE (argument) == VAR_DECL)
10185             {
10186               /* A variable without external linkage might still be a
10187                  valid constant-expression, so no error is issued here
10188                  if the external-linkage check fails.  */
10189               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10190                 cp_parser_simulate_error (parser);
10191             }
10192           else if (is_overloaded_fn (argument))
10193             /* All overloaded functions are allowed; if the external
10194                linkage test does not pass, an error will be issued
10195                later.  */
10196             ;
10197           else if (address_p
10198                    && (TREE_CODE (argument) == OFFSET_REF
10199                        || TREE_CODE (argument) == SCOPE_REF))
10200             /* A pointer-to-member.  */
10201             ;
10202           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10203             ;
10204           else
10205             cp_parser_simulate_error (parser);
10206
10207           if (cp_parser_parse_definitely (parser))
10208             {
10209               if (address_p)
10210                 argument = build_x_unary_op (ADDR_EXPR, argument);
10211               return argument;
10212             }
10213         }
10214     }
10215   /* If the argument started with "&", there are no other valid
10216      alternatives at this point.  */
10217   if (address_p)
10218     {
10219       cp_parser_error (parser, "invalid non-type template argument");
10220       return error_mark_node;
10221     }
10222
10223   /* If the argument wasn't successfully parsed as a type-id followed
10224      by '>>', the argument can only be a constant expression now.
10225      Otherwise, we try parsing the constant-expression tentatively,
10226      because the argument could really be a type-id.  */
10227   if (maybe_type_id)
10228     cp_parser_parse_tentatively (parser);
10229   argument = cp_parser_constant_expression (parser,
10230                                             /*allow_non_constant_p=*/false,
10231                                             /*non_constant_p=*/NULL);
10232   argument = fold_non_dependent_expr (argument);
10233   if (!maybe_type_id)
10234     return argument;
10235   if (!cp_parser_next_token_ends_template_argument_p (parser))
10236     cp_parser_error (parser, "expected template-argument");
10237   if (cp_parser_parse_definitely (parser))
10238     return argument;
10239   /* We did our best to parse the argument as a non type-id, but that
10240      was the only alternative that matched (albeit with a '>' after
10241      it). We can assume it's just a typo from the user, and a
10242      diagnostic will then be issued.  */
10243   return cp_parser_type_id (parser);
10244 }
10245
10246 /* Parse an explicit-instantiation.
10247
10248    explicit-instantiation:
10249      template declaration
10250
10251    Although the standard says `declaration', what it really means is:
10252
10253    explicit-instantiation:
10254      template decl-specifier-seq [opt] declarator [opt] ;
10255
10256    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10257    supposed to be allowed.  A defect report has been filed about this
10258    issue.
10259
10260    GNU Extension:
10261
10262    explicit-instantiation:
10263      storage-class-specifier template
10264        decl-specifier-seq [opt] declarator [opt] ;
10265      function-specifier template
10266        decl-specifier-seq [opt] declarator [opt] ;  */
10267
10268 static void
10269 cp_parser_explicit_instantiation (cp_parser* parser)
10270 {
10271   int declares_class_or_enum;
10272   cp_decl_specifier_seq decl_specifiers;
10273   tree extension_specifier = NULL_TREE;
10274
10275   /* Look for an (optional) storage-class-specifier or
10276      function-specifier.  */
10277   if (cp_parser_allow_gnu_extensions_p (parser))
10278     {
10279       extension_specifier
10280         = cp_parser_storage_class_specifier_opt (parser);
10281       if (!extension_specifier)
10282         extension_specifier
10283           = cp_parser_function_specifier_opt (parser,
10284                                               /*decl_specs=*/NULL);
10285     }
10286
10287   /* Look for the `template' keyword.  */
10288   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10289   /* Let the front end know that we are processing an explicit
10290      instantiation.  */
10291   begin_explicit_instantiation ();
10292   /* [temp.explicit] says that we are supposed to ignore access
10293      control while processing explicit instantiation directives.  */
10294   push_deferring_access_checks (dk_no_check);
10295   /* Parse a decl-specifier-seq.  */
10296   cp_parser_decl_specifier_seq (parser,
10297                                 CP_PARSER_FLAGS_OPTIONAL,
10298                                 &decl_specifiers,
10299                                 &declares_class_or_enum);
10300   /* If there was exactly one decl-specifier, and it declared a class,
10301      and there's no declarator, then we have an explicit type
10302      instantiation.  */
10303   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10304     {
10305       tree type;
10306
10307       type = check_tag_decl (&decl_specifiers);
10308       /* Turn access control back on for names used during
10309          template instantiation.  */
10310       pop_deferring_access_checks ();
10311       if (type)
10312         do_type_instantiation (type, extension_specifier,
10313                                /*complain=*/tf_error);
10314     }
10315   else
10316     {
10317       cp_declarator *declarator;
10318       tree decl;
10319
10320       /* Parse the declarator.  */
10321       declarator
10322         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10323                                 /*ctor_dtor_or_conv_p=*/NULL,
10324                                 /*parenthesized_p=*/NULL,
10325                                 /*member_p=*/false);
10326       if (declares_class_or_enum & 2)
10327         cp_parser_check_for_definition_in_return_type (declarator,
10328                                                        decl_specifiers.type);
10329       if (declarator != cp_error_declarator)
10330         {
10331           decl = grokdeclarator (declarator, &decl_specifiers,
10332                                  NORMAL, 0, &decl_specifiers.attributes);
10333           /* Turn access control back on for names used during
10334              template instantiation.  */
10335           pop_deferring_access_checks ();
10336           /* Do the explicit instantiation.  */
10337           do_decl_instantiation (decl, extension_specifier);
10338         }
10339       else
10340         {
10341           pop_deferring_access_checks ();
10342           /* Skip the body of the explicit instantiation.  */
10343           cp_parser_skip_to_end_of_statement (parser);
10344         }
10345     }
10346   /* We're done with the instantiation.  */
10347   end_explicit_instantiation ();
10348
10349   cp_parser_consume_semicolon_at_end_of_statement (parser);
10350 }
10351
10352 /* Parse an explicit-specialization.
10353
10354    explicit-specialization:
10355      template < > declaration
10356
10357    Although the standard says `declaration', what it really means is:
10358
10359    explicit-specialization:
10360      template <> decl-specifier [opt] init-declarator [opt] ;
10361      template <> function-definition
10362      template <> explicit-specialization
10363      template <> template-declaration  */
10364
10365 static void
10366 cp_parser_explicit_specialization (cp_parser* parser)
10367 {
10368   bool need_lang_pop;
10369   /* Look for the `template' keyword.  */
10370   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10371   /* Look for the `<'.  */
10372   cp_parser_require (parser, CPP_LESS, "`<'");
10373   /* Look for the `>'.  */
10374   cp_parser_require (parser, CPP_GREATER, "`>'");
10375   /* We have processed another parameter list.  */
10376   ++parser->num_template_parameter_lists;
10377   /* [temp]
10378
10379      A template ... explicit specialization ... shall not have C
10380      linkage.  */
10381   if (current_lang_name == lang_name_c)
10382     {
10383       error ("template specialization with C linkage");
10384       /* Give it C++ linkage to avoid confusing other parts of the
10385          front end.  */
10386       push_lang_context (lang_name_cplusplus);
10387       need_lang_pop = true;
10388     }
10389   else
10390     need_lang_pop = false;
10391   /* Let the front end know that we are beginning a specialization.  */
10392   if (!begin_specialization ())
10393     {
10394       end_specialization ();
10395       cp_parser_skip_to_end_of_block_or_statement (parser);
10396       return;
10397     }
10398
10399   /* If the next keyword is `template', we need to figure out whether
10400      or not we're looking a template-declaration.  */
10401   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10402     {
10403       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10404           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10405         cp_parser_template_declaration_after_export (parser,
10406                                                      /*member_p=*/false);
10407       else
10408         cp_parser_explicit_specialization (parser);
10409     }
10410   else
10411     /* Parse the dependent declaration.  */
10412     cp_parser_single_declaration (parser,
10413                                   /*checks=*/NULL,
10414                                   /*member_p=*/false,
10415                                   /*explicit_specialization_p=*/true,
10416                                   /*friend_p=*/NULL);
10417   /* We're done with the specialization.  */
10418   end_specialization ();
10419   /* For the erroneous case of a template with C linkage, we pushed an
10420      implicit C++ linkage scope; exit that scope now.  */
10421   if (need_lang_pop)
10422     pop_lang_context ();
10423   /* We're done with this parameter list.  */
10424   --parser->num_template_parameter_lists;
10425 }
10426
10427 /* Parse a type-specifier.
10428
10429    type-specifier:
10430      simple-type-specifier
10431      class-specifier
10432      enum-specifier
10433      elaborated-type-specifier
10434      cv-qualifier
10435
10436    GNU Extension:
10437
10438    type-specifier:
10439      __complex__
10440
10441    Returns a representation of the type-specifier.  For a
10442    class-specifier, enum-specifier, or elaborated-type-specifier, a
10443    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10444
10445    The parser flags FLAGS is used to control type-specifier parsing.
10446
10447    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10448    in a decl-specifier-seq.
10449
10450    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10451    class-specifier, enum-specifier, or elaborated-type-specifier, then
10452    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10453    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10454    zero.
10455
10456    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10457    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10458    is set to FALSE.  */
10459
10460 static tree
10461 cp_parser_type_specifier (cp_parser* parser,
10462                           cp_parser_flags flags,
10463                           cp_decl_specifier_seq *decl_specs,
10464                           bool is_declaration,
10465                           int* declares_class_or_enum,
10466                           bool* is_cv_qualifier)
10467 {
10468   tree type_spec = NULL_TREE;
10469   cp_token *token;
10470   enum rid keyword;
10471   cp_decl_spec ds = ds_last;
10472
10473   /* Assume this type-specifier does not declare a new type.  */
10474   if (declares_class_or_enum)
10475     *declares_class_or_enum = 0;
10476   /* And that it does not specify a cv-qualifier.  */
10477   if (is_cv_qualifier)
10478     *is_cv_qualifier = false;
10479   /* Peek at the next token.  */
10480   token = cp_lexer_peek_token (parser->lexer);
10481
10482   /* If we're looking at a keyword, we can use that to guide the
10483      production we choose.  */
10484   keyword = token->keyword;
10485   switch (keyword)
10486     {
10487     case RID_ENUM:
10488       /* Look for the enum-specifier.  */
10489       type_spec = cp_parser_enum_specifier (parser);
10490       /* If that worked, we're done.  */
10491       if (type_spec)
10492         {
10493           if (declares_class_or_enum)
10494             *declares_class_or_enum = 2;
10495           if (decl_specs)
10496             cp_parser_set_decl_spec_type (decl_specs,
10497                                           type_spec,
10498                                           /*user_defined_p=*/true);
10499           return type_spec;
10500         }
10501       else
10502         goto elaborated_type_specifier;
10503
10504       /* Any of these indicate either a class-specifier, or an
10505          elaborated-type-specifier.  */
10506     case RID_CLASS:
10507     case RID_STRUCT:
10508     case RID_UNION:
10509       /* Parse tentatively so that we can back up if we don't find a
10510          class-specifier.  */
10511       cp_parser_parse_tentatively (parser);
10512       /* Look for the class-specifier.  */
10513       type_spec = cp_parser_class_specifier (parser);
10514       /* If that worked, we're done.  */
10515       if (cp_parser_parse_definitely (parser))
10516         {
10517           if (declares_class_or_enum)
10518             *declares_class_or_enum = 2;
10519           if (decl_specs)
10520             cp_parser_set_decl_spec_type (decl_specs,
10521                                           type_spec,
10522                                           /*user_defined_p=*/true);
10523           return type_spec;
10524         }
10525
10526       /* Fall through.  */
10527     elaborated_type_specifier:
10528       /* We're declaring (not defining) a class or enum.  */
10529       if (declares_class_or_enum)
10530         *declares_class_or_enum = 1;
10531
10532       /* Fall through.  */
10533     case RID_TYPENAME:
10534       /* Look for an elaborated-type-specifier.  */
10535       type_spec
10536         = (cp_parser_elaborated_type_specifier
10537            (parser,
10538             decl_specs && decl_specs->specs[(int) ds_friend],
10539             is_declaration));
10540       if (decl_specs)
10541         cp_parser_set_decl_spec_type (decl_specs,
10542                                       type_spec,
10543                                       /*user_defined_p=*/true);
10544       return type_spec;
10545
10546     case RID_CONST:
10547       ds = ds_const;
10548       if (is_cv_qualifier)
10549         *is_cv_qualifier = true;
10550       break;
10551
10552     case RID_VOLATILE:
10553       ds = ds_volatile;
10554       if (is_cv_qualifier)
10555         *is_cv_qualifier = true;
10556       break;
10557
10558     case RID_RESTRICT:
10559       ds = ds_restrict;
10560       if (is_cv_qualifier)
10561         *is_cv_qualifier = true;
10562       break;
10563
10564     case RID_COMPLEX:
10565       /* The `__complex__' keyword is a GNU extension.  */
10566       ds = ds_complex;
10567       break;
10568
10569     default:
10570       break;
10571     }
10572
10573   /* Handle simple keywords.  */
10574   if (ds != ds_last)
10575     {
10576       if (decl_specs)
10577         {
10578           ++decl_specs->specs[(int)ds];
10579           decl_specs->any_specifiers_p = true;
10580         }
10581       return cp_lexer_consume_token (parser->lexer)->u.value;
10582     }
10583
10584   /* If we do not already have a type-specifier, assume we are looking
10585      at a simple-type-specifier.  */
10586   type_spec = cp_parser_simple_type_specifier (parser,
10587                                                decl_specs,
10588                                                flags);
10589
10590   /* If we didn't find a type-specifier, and a type-specifier was not
10591      optional in this context, issue an error message.  */
10592   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10593     {
10594       cp_parser_error (parser, "expected type specifier");
10595       return error_mark_node;
10596     }
10597
10598   return type_spec;
10599 }
10600
10601 /* Parse a simple-type-specifier.
10602
10603    simple-type-specifier:
10604      :: [opt] nested-name-specifier [opt] type-name
10605      :: [opt] nested-name-specifier template template-id
10606      char
10607      wchar_t
10608      bool
10609      short
10610      int
10611      long
10612      signed
10613      unsigned
10614      float
10615      double
10616      void
10617
10618    C++0x Extension:
10619
10620    simple-type-specifier:
10621      decltype ( expression )   
10622
10623    GNU Extension:
10624
10625    simple-type-specifier:
10626      __typeof__ unary-expression
10627      __typeof__ ( type-id )
10628
10629    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10630    appropriately updated.  */
10631
10632 static tree
10633 cp_parser_simple_type_specifier (cp_parser* parser,
10634                                  cp_decl_specifier_seq *decl_specs,
10635                                  cp_parser_flags flags)
10636 {
10637   tree type = NULL_TREE;
10638   cp_token *token;
10639
10640   /* Peek at the next token.  */
10641   token = cp_lexer_peek_token (parser->lexer);
10642
10643   /* If we're looking at a keyword, things are easy.  */
10644   switch (token->keyword)
10645     {
10646     case RID_CHAR:
10647       if (decl_specs)
10648         decl_specs->explicit_char_p = true;
10649       type = char_type_node;
10650       break;
10651     case RID_WCHAR:
10652       type = wchar_type_node;
10653       break;
10654     case RID_BOOL:
10655       type = boolean_type_node;
10656       break;
10657     case RID_SHORT:
10658       if (decl_specs)
10659         ++decl_specs->specs[(int) ds_short];
10660       type = short_integer_type_node;
10661       break;
10662     case RID_INT:
10663       if (decl_specs)
10664         decl_specs->explicit_int_p = true;
10665       type = integer_type_node;
10666       break;
10667     case RID_LONG:
10668       if (decl_specs)
10669         ++decl_specs->specs[(int) ds_long];
10670       type = long_integer_type_node;
10671       break;
10672     case RID_SIGNED:
10673       if (decl_specs)
10674         ++decl_specs->specs[(int) ds_signed];
10675       type = integer_type_node;
10676       break;
10677     case RID_UNSIGNED:
10678       if (decl_specs)
10679         ++decl_specs->specs[(int) ds_unsigned];
10680       type = unsigned_type_node;
10681       break;
10682     case RID_FLOAT:
10683       type = float_type_node;
10684       break;
10685     case RID_DOUBLE:
10686       type = double_type_node;
10687       break;
10688     case RID_VOID:
10689       type = void_type_node;
10690       break;
10691
10692     case RID_DECLTYPE:
10693       /* Parse the `decltype' type.  */
10694       type = cp_parser_decltype (parser);
10695
10696       if (decl_specs)
10697         cp_parser_set_decl_spec_type (decl_specs, type,
10698                                       /*user_defined_p=*/true);
10699
10700       return type;
10701
10702     case RID_TYPEOF:
10703       /* Consume the `typeof' token.  */
10704       cp_lexer_consume_token (parser->lexer);
10705       /* Parse the operand to `typeof'.  */
10706       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10707       /* If it is not already a TYPE, take its type.  */
10708       if (!TYPE_P (type))
10709         type = finish_typeof (type);
10710
10711       if (decl_specs)
10712         cp_parser_set_decl_spec_type (decl_specs, type,
10713                                       /*user_defined_p=*/true);
10714
10715       return type;
10716
10717     default:
10718       break;
10719     }
10720
10721   /* If the type-specifier was for a built-in type, we're done.  */
10722   if (type)
10723     {
10724       tree id;
10725
10726       /* Record the type.  */
10727       if (decl_specs
10728           && (token->keyword != RID_SIGNED
10729               && token->keyword != RID_UNSIGNED
10730               && token->keyword != RID_SHORT
10731               && token->keyword != RID_LONG))
10732         cp_parser_set_decl_spec_type (decl_specs,
10733                                       type,
10734                                       /*user_defined=*/false);
10735       if (decl_specs)
10736         decl_specs->any_specifiers_p = true;
10737
10738       /* Consume the token.  */
10739       id = cp_lexer_consume_token (parser->lexer)->u.value;
10740
10741       /* There is no valid C++ program where a non-template type is
10742          followed by a "<".  That usually indicates that the user thought
10743          that the type was a template.  */
10744       cp_parser_check_for_invalid_template_id (parser, type);
10745
10746       return TYPE_NAME (type);
10747     }
10748
10749   /* The type-specifier must be a user-defined type.  */
10750   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10751     {
10752       bool qualified_p;
10753       bool global_p;
10754
10755       /* Don't gobble tokens or issue error messages if this is an
10756          optional type-specifier.  */
10757       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10758         cp_parser_parse_tentatively (parser);
10759
10760       /* Look for the optional `::' operator.  */
10761       global_p
10762         = (cp_parser_global_scope_opt (parser,
10763                                        /*current_scope_valid_p=*/false)
10764            != NULL_TREE);
10765       /* Look for the nested-name specifier.  */
10766       qualified_p
10767         = (cp_parser_nested_name_specifier_opt (parser,
10768                                                 /*typename_keyword_p=*/false,
10769                                                 /*check_dependency_p=*/true,
10770                                                 /*type_p=*/false,
10771                                                 /*is_declaration=*/false)
10772            != NULL_TREE);
10773       /* If we have seen a nested-name-specifier, and the next token
10774          is `template', then we are using the template-id production.  */
10775       if (parser->scope
10776           && cp_parser_optional_template_keyword (parser))
10777         {
10778           /* Look for the template-id.  */
10779           type = cp_parser_template_id (parser,
10780                                         /*template_keyword_p=*/true,
10781                                         /*check_dependency_p=*/true,
10782                                         /*is_declaration=*/false);
10783           /* If the template-id did not name a type, we are out of
10784              luck.  */
10785           if (TREE_CODE (type) != TYPE_DECL)
10786             {
10787               cp_parser_error (parser, "expected template-id for type");
10788               type = NULL_TREE;
10789             }
10790         }
10791       /* Otherwise, look for a type-name.  */
10792       else
10793         type = cp_parser_type_name (parser);
10794       /* Keep track of all name-lookups performed in class scopes.  */
10795       if (type
10796           && !global_p
10797           && !qualified_p
10798           && TREE_CODE (type) == TYPE_DECL
10799           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10800         maybe_note_name_used_in_class (DECL_NAME (type), type);
10801       /* If it didn't work out, we don't have a TYPE.  */
10802       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10803           && !cp_parser_parse_definitely (parser))
10804         type = NULL_TREE;
10805       if (type && decl_specs)
10806         cp_parser_set_decl_spec_type (decl_specs, type,
10807                                       /*user_defined=*/true);
10808     }
10809
10810   /* If we didn't get a type-name, issue an error message.  */
10811   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10812     {
10813       cp_parser_error (parser, "expected type-name");
10814       return error_mark_node;
10815     }
10816
10817   /* There is no valid C++ program where a non-template type is
10818      followed by a "<".  That usually indicates that the user thought
10819      that the type was a template.  */
10820   if (type && type != error_mark_node)
10821     {
10822       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10823          If it is, then the '<'...'>' enclose protocol names rather than
10824          template arguments, and so everything is fine.  */
10825       if (c_dialect_objc ()
10826           && (objc_is_id (type) || objc_is_class_name (type)))
10827         {
10828           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10829           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10830
10831           /* Clobber the "unqualified" type previously entered into
10832              DECL_SPECS with the new, improved protocol-qualified version.  */
10833           if (decl_specs)
10834             decl_specs->type = qual_type;
10835
10836           return qual_type;
10837         }
10838
10839       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10840     }
10841
10842   return type;
10843 }
10844
10845 /* Parse a type-name.
10846
10847    type-name:
10848      class-name
10849      enum-name
10850      typedef-name
10851
10852    enum-name:
10853      identifier
10854
10855    typedef-name:
10856      identifier
10857
10858    Returns a TYPE_DECL for the type.  */
10859
10860 static tree
10861 cp_parser_type_name (cp_parser* parser)
10862 {
10863   tree type_decl;
10864   tree identifier;
10865
10866   /* We can't know yet whether it is a class-name or not.  */
10867   cp_parser_parse_tentatively (parser);
10868   /* Try a class-name.  */
10869   type_decl = cp_parser_class_name (parser,
10870                                     /*typename_keyword_p=*/false,
10871                                     /*template_keyword_p=*/false,
10872                                     none_type,
10873                                     /*check_dependency_p=*/true,
10874                                     /*class_head_p=*/false,
10875                                     /*is_declaration=*/false);
10876   /* If it's not a class-name, keep looking.  */
10877   if (!cp_parser_parse_definitely (parser))
10878     {
10879       /* It must be a typedef-name or an enum-name.  */
10880       identifier = cp_parser_identifier (parser);
10881       if (identifier == error_mark_node)
10882         return error_mark_node;
10883
10884       /* Look up the type-name.  */
10885       type_decl = cp_parser_lookup_name_simple (parser, identifier);
10886
10887       if (TREE_CODE (type_decl) != TYPE_DECL
10888           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10889         {
10890           /* See if this is an Objective-C type.  */
10891           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10892           tree type = objc_get_protocol_qualified_type (identifier, protos);
10893           if (type)
10894             type_decl = TYPE_NAME (type);
10895         }
10896
10897       /* Issue an error if we did not find a type-name.  */
10898       if (TREE_CODE (type_decl) != TYPE_DECL)
10899         {
10900           if (!cp_parser_simulate_error (parser))
10901             cp_parser_name_lookup_error (parser, identifier, type_decl,
10902                                          "is not a type");
10903           type_decl = error_mark_node;
10904         }
10905       /* Remember that the name was used in the definition of the
10906          current class so that we can check later to see if the
10907          meaning would have been different after the class was
10908          entirely defined.  */
10909       else if (type_decl != error_mark_node
10910                && !parser->scope)
10911         maybe_note_name_used_in_class (identifier, type_decl);
10912     }
10913
10914   return type_decl;
10915 }
10916
10917
10918 /* Parse an elaborated-type-specifier.  Note that the grammar given
10919    here incorporates the resolution to DR68.
10920
10921    elaborated-type-specifier:
10922      class-key :: [opt] nested-name-specifier [opt] identifier
10923      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10924      enum :: [opt] nested-name-specifier [opt] identifier
10925      typename :: [opt] nested-name-specifier identifier
10926      typename :: [opt] nested-name-specifier template [opt]
10927        template-id
10928
10929    GNU extension:
10930
10931    elaborated-type-specifier:
10932      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10933      class-key attributes :: [opt] nested-name-specifier [opt]
10934                template [opt] template-id
10935      enum attributes :: [opt] nested-name-specifier [opt] identifier
10936
10937    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10938    declared `friend'.  If IS_DECLARATION is TRUE, then this
10939    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10940    something is being declared.
10941
10942    Returns the TYPE specified.  */
10943
10944 static tree
10945 cp_parser_elaborated_type_specifier (cp_parser* parser,
10946                                      bool is_friend,
10947                                      bool is_declaration)
10948 {
10949   enum tag_types tag_type;
10950   tree identifier;
10951   tree type = NULL_TREE;
10952   tree attributes = NULL_TREE;
10953
10954   /* See if we're looking at the `enum' keyword.  */
10955   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10956     {
10957       /* Consume the `enum' token.  */
10958       cp_lexer_consume_token (parser->lexer);
10959       /* Remember that it's an enumeration type.  */
10960       tag_type = enum_type;
10961       /* Parse the attributes.  */
10962       attributes = cp_parser_attributes_opt (parser);
10963     }
10964   /* Or, it might be `typename'.  */
10965   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10966                                            RID_TYPENAME))
10967     {
10968       /* Consume the `typename' token.  */
10969       cp_lexer_consume_token (parser->lexer);
10970       /* Remember that it's a `typename' type.  */
10971       tag_type = typename_type;
10972       /* The `typename' keyword is only allowed in templates.  */
10973       if (!processing_template_decl)
10974         pedwarn ("using %<typename%> outside of template");
10975     }
10976   /* Otherwise it must be a class-key.  */
10977   else
10978     {
10979       tag_type = cp_parser_class_key (parser);
10980       if (tag_type == none_type)
10981         return error_mark_node;
10982       /* Parse the attributes.  */
10983       attributes = cp_parser_attributes_opt (parser);
10984     }
10985
10986   /* Look for the `::' operator.  */
10987   cp_parser_global_scope_opt (parser,
10988                               /*current_scope_valid_p=*/false);
10989   /* Look for the nested-name-specifier.  */
10990   if (tag_type == typename_type)
10991     {
10992       if (!cp_parser_nested_name_specifier (parser,
10993                                            /*typename_keyword_p=*/true,
10994                                            /*check_dependency_p=*/true,
10995                                            /*type_p=*/true,
10996                                             is_declaration))
10997         return error_mark_node;
10998     }
10999   else
11000     /* Even though `typename' is not present, the proposed resolution
11001        to Core Issue 180 says that in `class A<T>::B', `B' should be
11002        considered a type-name, even if `A<T>' is dependent.  */
11003     cp_parser_nested_name_specifier_opt (parser,
11004                                          /*typename_keyword_p=*/true,
11005                                          /*check_dependency_p=*/true,
11006                                          /*type_p=*/true,
11007                                          is_declaration);
11008  /* For everything but enumeration types, consider a template-id.
11009     For an enumeration type, consider only a plain identifier.  */
11010   if (tag_type != enum_type)
11011     {
11012       bool template_p = false;
11013       tree decl;
11014
11015       /* Allow the `template' keyword.  */
11016       template_p = cp_parser_optional_template_keyword (parser);
11017       /* If we didn't see `template', we don't know if there's a
11018          template-id or not.  */
11019       if (!template_p)
11020         cp_parser_parse_tentatively (parser);
11021       /* Parse the template-id.  */
11022       decl = cp_parser_template_id (parser, template_p,
11023                                     /*check_dependency_p=*/true,
11024                                     is_declaration);
11025       /* If we didn't find a template-id, look for an ordinary
11026          identifier.  */
11027       if (!template_p && !cp_parser_parse_definitely (parser))
11028         ;
11029       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11030          in effect, then we must assume that, upon instantiation, the
11031          template will correspond to a class.  */
11032       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11033                && tag_type == typename_type)
11034         type = make_typename_type (parser->scope, decl,
11035                                    typename_type,
11036                                    /*complain=*/tf_error);
11037       else
11038         type = TREE_TYPE (decl);
11039     }
11040
11041   if (!type)
11042     {
11043       identifier = cp_parser_identifier (parser);
11044
11045       if (identifier == error_mark_node)
11046         {
11047           parser->scope = NULL_TREE;
11048           return error_mark_node;
11049         }
11050
11051       /* For a `typename', we needn't call xref_tag.  */
11052       if (tag_type == typename_type
11053           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11054         return cp_parser_make_typename_type (parser, parser->scope,
11055                                              identifier);
11056       /* Look up a qualified name in the usual way.  */
11057       if (parser->scope)
11058         {
11059           tree decl;
11060           tree ambiguous_decls;
11061
11062           decl = cp_parser_lookup_name (parser, identifier,
11063                                         tag_type,
11064                                         /*is_template=*/false,
11065                                         /*is_namespace=*/false,
11066                                         /*check_dependency=*/true,
11067                                         &ambiguous_decls);
11068
11069           /* If the lookup was ambiguous, an error will already have been
11070              issued.  */
11071           if (ambiguous_decls)
11072             return error_mark_node;
11073
11074           /* If we are parsing friend declaration, DECL may be a
11075              TEMPLATE_DECL tree node here.  However, we need to check
11076              whether this TEMPLATE_DECL results in valid code.  Consider
11077              the following example:
11078
11079                namespace N {
11080                  template <class T> class C {};
11081                }
11082                class X {
11083                  template <class T> friend class N::C; // #1, valid code
11084                };
11085                template <class T> class Y {
11086                  friend class N::C;                    // #2, invalid code
11087                };
11088
11089              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11090              name lookup of `N::C'.  We see that friend declaration must
11091              be template for the code to be valid.  Note that
11092              processing_template_decl does not work here since it is
11093              always 1 for the above two cases.  */
11094
11095           decl = (cp_parser_maybe_treat_template_as_class
11096                   (decl, /*tag_name_p=*/is_friend
11097                          && parser->num_template_parameter_lists));
11098
11099           if (TREE_CODE (decl) != TYPE_DECL)
11100             {
11101               cp_parser_diagnose_invalid_type_name (parser,
11102                                                     parser->scope,
11103                                                     identifier);
11104               return error_mark_node;
11105             }
11106
11107           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11108             {
11109               bool allow_template = (parser->num_template_parameter_lists
11110                                       || DECL_SELF_REFERENCE_P (decl));
11111               type = check_elaborated_type_specifier (tag_type, decl, 
11112                                                       allow_template);
11113
11114               if (type == error_mark_node)
11115                 return error_mark_node;
11116             }
11117
11118           /* Forward declarations of nested types, such as
11119
11120                class C1::C2;
11121                class C1::C2::C3;
11122
11123              are invalid unless all components preceding the final '::'
11124              are complete.  If all enclosing types are complete, these
11125              declarations become merely pointless.
11126
11127              Invalid forward declarations of nested types are errors
11128              caught elsewhere in parsing.  Those that are pointless arrive
11129              here.  */
11130
11131           if (cp_parser_declares_only_class_p (parser)
11132               && !is_friend && !processing_explicit_instantiation)
11133             warning (0, "declaration %qD does not declare anything", decl);
11134
11135           type = TREE_TYPE (decl);
11136         }
11137       else
11138         {
11139           /* An elaborated-type-specifier sometimes introduces a new type and
11140              sometimes names an existing type.  Normally, the rule is that it
11141              introduces a new type only if there is not an existing type of
11142              the same name already in scope.  For example, given:
11143
11144                struct S {};
11145                void f() { struct S s; }
11146
11147              the `struct S' in the body of `f' is the same `struct S' as in
11148              the global scope; the existing definition is used.  However, if
11149              there were no global declaration, this would introduce a new
11150              local class named `S'.
11151
11152              An exception to this rule applies to the following code:
11153
11154                namespace N { struct S; }
11155
11156              Here, the elaborated-type-specifier names a new type
11157              unconditionally; even if there is already an `S' in the
11158              containing scope this declaration names a new type.
11159              This exception only applies if the elaborated-type-specifier
11160              forms the complete declaration:
11161
11162                [class.name]
11163
11164                A declaration consisting solely of `class-key identifier ;' is
11165                either a redeclaration of the name in the current scope or a
11166                forward declaration of the identifier as a class name.  It
11167                introduces the name into the current scope.
11168
11169              We are in this situation precisely when the next token is a `;'.
11170
11171              An exception to the exception is that a `friend' declaration does
11172              *not* name a new type; i.e., given:
11173
11174                struct S { friend struct T; };
11175
11176              `T' is not a new type in the scope of `S'.
11177
11178              Also, `new struct S' or `sizeof (struct S)' never results in the
11179              definition of a new type; a new type can only be declared in a
11180              declaration context.  */
11181
11182           tag_scope ts;
11183           bool template_p;
11184
11185           if (is_friend)
11186             /* Friends have special name lookup rules.  */
11187             ts = ts_within_enclosing_non_class;
11188           else if (is_declaration
11189                    && cp_lexer_next_token_is (parser->lexer,
11190                                               CPP_SEMICOLON))
11191             /* This is a `class-key identifier ;' */
11192             ts = ts_current;
11193           else
11194             ts = ts_global;
11195
11196           template_p =
11197             (parser->num_template_parameter_lists
11198              && (cp_parser_next_token_starts_class_definition_p (parser)
11199                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11200           /* An unqualified name was used to reference this type, so
11201              there were no qualifying templates.  */
11202           if (!cp_parser_check_template_parameters (parser,
11203                                                     /*num_templates=*/0))
11204             return error_mark_node;
11205           type = xref_tag (tag_type, identifier, ts, template_p);
11206         }
11207     }
11208
11209   if (type == error_mark_node)
11210     return error_mark_node;
11211
11212   /* Allow attributes on forward declarations of classes.  */
11213   if (attributes)
11214     {
11215       if (TREE_CODE (type) == TYPENAME_TYPE)
11216         warning (OPT_Wattributes,
11217                  "attributes ignored on uninstantiated type");
11218       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11219                && ! processing_explicit_instantiation)
11220         warning (OPT_Wattributes,
11221                  "attributes ignored on template instantiation");
11222       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11223         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11224       else
11225         warning (OPT_Wattributes,
11226                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11227     }
11228
11229   if (tag_type != enum_type)
11230     cp_parser_check_class_key (tag_type, type);
11231
11232   /* A "<" cannot follow an elaborated type specifier.  If that
11233      happens, the user was probably trying to form a template-id.  */
11234   cp_parser_check_for_invalid_template_id (parser, type);
11235
11236   return type;
11237 }
11238
11239 /* Parse an enum-specifier.
11240
11241    enum-specifier:
11242      enum identifier [opt] { enumerator-list [opt] }
11243
11244    GNU Extensions:
11245      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11246        attributes[opt]
11247
11248    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11249    if the token stream isn't an enum-specifier after all.  */
11250
11251 static tree
11252 cp_parser_enum_specifier (cp_parser* parser)
11253 {
11254   tree identifier;
11255   tree type;
11256   tree attributes;
11257
11258   /* Parse tentatively so that we can back up if we don't find a
11259      enum-specifier.  */
11260   cp_parser_parse_tentatively (parser);
11261
11262   /* Caller guarantees that the current token is 'enum', an identifier
11263      possibly follows, and the token after that is an opening brace.
11264      If we don't have an identifier, fabricate an anonymous name for
11265      the enumeration being defined.  */
11266   cp_lexer_consume_token (parser->lexer);
11267
11268   attributes = cp_parser_attributes_opt (parser);
11269
11270   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11271     identifier = cp_parser_identifier (parser);
11272   else
11273     identifier = make_anon_name ();
11274
11275   /* Look for the `{' but don't consume it yet.  */
11276   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11277     cp_parser_simulate_error (parser);
11278
11279   if (!cp_parser_parse_definitely (parser))
11280     return NULL_TREE;
11281
11282   /* Issue an error message if type-definitions are forbidden here.  */
11283   if (!cp_parser_check_type_definition (parser))
11284     type = error_mark_node;
11285   else
11286     /* Create the new type.  We do this before consuming the opening
11287        brace so the enum will be recorded as being on the line of its
11288        tag (or the 'enum' keyword, if there is no tag).  */
11289     type = start_enum (identifier);
11290   
11291   /* Consume the opening brace.  */
11292   cp_lexer_consume_token (parser->lexer);
11293
11294   if (type == error_mark_node)
11295     {
11296       cp_parser_skip_to_end_of_block_or_statement (parser);
11297       return error_mark_node;
11298     }
11299
11300   /* If the next token is not '}', then there are some enumerators.  */
11301   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11302     cp_parser_enumerator_list (parser, type);
11303
11304   /* Consume the final '}'.  */
11305   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11306
11307   /* Look for trailing attributes to apply to this enumeration, and
11308      apply them if appropriate.  */
11309   if (cp_parser_allow_gnu_extensions_p (parser))
11310     {
11311       tree trailing_attr = cp_parser_attributes_opt (parser);
11312       cplus_decl_attributes (&type,
11313                              trailing_attr,
11314                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11315     }
11316
11317   /* Finish up the enumeration.  */
11318   finish_enum (type);
11319
11320   return type;
11321 }
11322
11323 /* Parse an enumerator-list.  The enumerators all have the indicated
11324    TYPE.
11325
11326    enumerator-list:
11327      enumerator-definition
11328      enumerator-list , enumerator-definition  */
11329
11330 static void
11331 cp_parser_enumerator_list (cp_parser* parser, tree type)
11332 {
11333   while (true)
11334     {
11335       /* Parse an enumerator-definition.  */
11336       cp_parser_enumerator_definition (parser, type);
11337
11338       /* If the next token is not a ',', we've reached the end of
11339          the list.  */
11340       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11341         break;
11342       /* Otherwise, consume the `,' and keep going.  */
11343       cp_lexer_consume_token (parser->lexer);
11344       /* If the next token is a `}', there is a trailing comma.  */
11345       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11346         {
11347           if (pedantic && !in_system_header)
11348             pedwarn ("comma at end of enumerator list");
11349           break;
11350         }
11351     }
11352 }
11353
11354 /* Parse an enumerator-definition.  The enumerator has the indicated
11355    TYPE.
11356
11357    enumerator-definition:
11358      enumerator
11359      enumerator = constant-expression
11360
11361    enumerator:
11362      identifier  */
11363
11364 static void
11365 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11366 {
11367   tree identifier;
11368   tree value;
11369
11370   /* Look for the identifier.  */
11371   identifier = cp_parser_identifier (parser);
11372   if (identifier == error_mark_node)
11373     return;
11374
11375   /* If the next token is an '=', then there is an explicit value.  */
11376   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11377     {
11378       /* Consume the `=' token.  */
11379       cp_lexer_consume_token (parser->lexer);
11380       /* Parse the value.  */
11381       value = cp_parser_constant_expression (parser,
11382                                              /*allow_non_constant_p=*/false,
11383                                              NULL);
11384     }
11385   else
11386     value = NULL_TREE;
11387
11388   /* Create the enumerator.  */
11389   build_enumerator (identifier, value, type);
11390 }
11391
11392 /* Parse a namespace-name.
11393
11394    namespace-name:
11395      original-namespace-name
11396      namespace-alias
11397
11398    Returns the NAMESPACE_DECL for the namespace.  */
11399
11400 static tree
11401 cp_parser_namespace_name (cp_parser* parser)
11402 {
11403   tree identifier;
11404   tree namespace_decl;
11405
11406   /* Get the name of the namespace.  */
11407   identifier = cp_parser_identifier (parser);
11408   if (identifier == error_mark_node)
11409     return error_mark_node;
11410
11411   /* Look up the identifier in the currently active scope.  Look only
11412      for namespaces, due to:
11413
11414        [basic.lookup.udir]
11415
11416        When looking up a namespace-name in a using-directive or alias
11417        definition, only namespace names are considered.
11418
11419      And:
11420
11421        [basic.lookup.qual]
11422
11423        During the lookup of a name preceding the :: scope resolution
11424        operator, object, function, and enumerator names are ignored.
11425
11426      (Note that cp_parser_class_or_namespace_name only calls this
11427      function if the token after the name is the scope resolution
11428      operator.)  */
11429   namespace_decl = cp_parser_lookup_name (parser, identifier,
11430                                           none_type,
11431                                           /*is_template=*/false,
11432                                           /*is_namespace=*/true,
11433                                           /*check_dependency=*/true,
11434                                           /*ambiguous_decls=*/NULL);
11435   /* If it's not a namespace, issue an error.  */
11436   if (namespace_decl == error_mark_node
11437       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11438     {
11439       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11440         error ("%qD is not a namespace-name", identifier);
11441       cp_parser_error (parser, "expected namespace-name");
11442       namespace_decl = error_mark_node;
11443     }
11444
11445   return namespace_decl;
11446 }
11447
11448 /* Parse a namespace-definition.
11449
11450    namespace-definition:
11451      named-namespace-definition
11452      unnamed-namespace-definition
11453
11454    named-namespace-definition:
11455      original-namespace-definition
11456      extension-namespace-definition
11457
11458    original-namespace-definition:
11459      namespace identifier { namespace-body }
11460
11461    extension-namespace-definition:
11462      namespace original-namespace-name { namespace-body }
11463
11464    unnamed-namespace-definition:
11465      namespace { namespace-body } */
11466
11467 static void
11468 cp_parser_namespace_definition (cp_parser* parser)
11469 {
11470   tree identifier, attribs;
11471
11472   /* Look for the `namespace' keyword.  */
11473   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11474
11475   /* Get the name of the namespace.  We do not attempt to distinguish
11476      between an original-namespace-definition and an
11477      extension-namespace-definition at this point.  The semantic
11478      analysis routines are responsible for that.  */
11479   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11480     identifier = cp_parser_identifier (parser);
11481   else
11482     identifier = NULL_TREE;
11483
11484   /* Parse any specified attributes.  */
11485   attribs = cp_parser_attributes_opt (parser);
11486
11487   /* Look for the `{' to start the namespace.  */
11488   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11489   /* Start the namespace.  */
11490   push_namespace_with_attribs (identifier, attribs);
11491   /* Parse the body of the namespace.  */
11492   cp_parser_namespace_body (parser);
11493   /* Finish the namespace.  */
11494   pop_namespace ();
11495   /* Look for the final `}'.  */
11496   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11497 }
11498
11499 /* Parse a namespace-body.
11500
11501    namespace-body:
11502      declaration-seq [opt]  */
11503
11504 static void
11505 cp_parser_namespace_body (cp_parser* parser)
11506 {
11507   cp_parser_declaration_seq_opt (parser);
11508 }
11509
11510 /* Parse a namespace-alias-definition.
11511
11512    namespace-alias-definition:
11513      namespace identifier = qualified-namespace-specifier ;  */
11514
11515 static void
11516 cp_parser_namespace_alias_definition (cp_parser* parser)
11517 {
11518   tree identifier;
11519   tree namespace_specifier;
11520
11521   /* Look for the `namespace' keyword.  */
11522   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11523   /* Look for the identifier.  */
11524   identifier = cp_parser_identifier (parser);
11525   if (identifier == error_mark_node)
11526     return;
11527   /* Look for the `=' token.  */
11528   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11529       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11530     {
11531       error ("%<namespace%> definition is not allowed here");
11532       /* Skip the definition.  */
11533       cp_lexer_consume_token (parser->lexer);
11534       if (cp_parser_skip_to_closing_brace (parser))
11535         cp_lexer_consume_token (parser->lexer);
11536       return;
11537     }
11538   cp_parser_require (parser, CPP_EQ, "`='");
11539   /* Look for the qualified-namespace-specifier.  */
11540   namespace_specifier
11541     = cp_parser_qualified_namespace_specifier (parser);
11542   /* Look for the `;' token.  */
11543   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11544
11545   /* Register the alias in the symbol table.  */
11546   do_namespace_alias (identifier, namespace_specifier);
11547 }
11548
11549 /* Parse a qualified-namespace-specifier.
11550
11551    qualified-namespace-specifier:
11552      :: [opt] nested-name-specifier [opt] namespace-name
11553
11554    Returns a NAMESPACE_DECL corresponding to the specified
11555    namespace.  */
11556
11557 static tree
11558 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11559 {
11560   /* Look for the optional `::'.  */
11561   cp_parser_global_scope_opt (parser,
11562                               /*current_scope_valid_p=*/false);
11563
11564   /* Look for the optional nested-name-specifier.  */
11565   cp_parser_nested_name_specifier_opt (parser,
11566                                        /*typename_keyword_p=*/false,
11567                                        /*check_dependency_p=*/true,
11568                                        /*type_p=*/false,
11569                                        /*is_declaration=*/true);
11570
11571   return cp_parser_namespace_name (parser);
11572 }
11573
11574 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11575    access declaration.
11576
11577    using-declaration:
11578      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11579      using :: unqualified-id ;  
11580
11581    access-declaration:
11582      qualified-id ;  
11583
11584    */
11585
11586 static bool
11587 cp_parser_using_declaration (cp_parser* parser, 
11588                              bool access_declaration_p)
11589 {
11590   cp_token *token;
11591   bool typename_p = false;
11592   bool global_scope_p;
11593   tree decl;
11594   tree identifier;
11595   tree qscope;
11596
11597   if (access_declaration_p)
11598     cp_parser_parse_tentatively (parser);
11599   else
11600     {
11601       /* Look for the `using' keyword.  */
11602       cp_parser_require_keyword (parser, RID_USING, "`using'");
11603       
11604       /* Peek at the next token.  */
11605       token = cp_lexer_peek_token (parser->lexer);
11606       /* See if it's `typename'.  */
11607       if (token->keyword == RID_TYPENAME)
11608         {
11609           /* Remember that we've seen it.  */
11610           typename_p = true;
11611           /* Consume the `typename' token.  */
11612           cp_lexer_consume_token (parser->lexer);
11613         }
11614     }
11615
11616   /* Look for the optional global scope qualification.  */
11617   global_scope_p
11618     = (cp_parser_global_scope_opt (parser,
11619                                    /*current_scope_valid_p=*/false)
11620        != NULL_TREE);
11621
11622   /* If we saw `typename', or didn't see `::', then there must be a
11623      nested-name-specifier present.  */
11624   if (typename_p || !global_scope_p)
11625     qscope = cp_parser_nested_name_specifier (parser, typename_p,
11626                                               /*check_dependency_p=*/true,
11627                                               /*type_p=*/false,
11628                                               /*is_declaration=*/true);
11629   /* Otherwise, we could be in either of the two productions.  In that
11630      case, treat the nested-name-specifier as optional.  */
11631   else
11632     qscope = cp_parser_nested_name_specifier_opt (parser,
11633                                                   /*typename_keyword_p=*/false,
11634                                                   /*check_dependency_p=*/true,
11635                                                   /*type_p=*/false,
11636                                                   /*is_declaration=*/true);
11637   if (!qscope)
11638     qscope = global_namespace;
11639
11640   if (access_declaration_p && cp_parser_error_occurred (parser))
11641     /* Something has already gone wrong; there's no need to parse
11642        further.  Since an error has occurred, the return value of
11643        cp_parser_parse_definitely will be false, as required.  */
11644     return cp_parser_parse_definitely (parser);
11645
11646   /* Parse the unqualified-id.  */
11647   identifier = cp_parser_unqualified_id (parser,
11648                                          /*template_keyword_p=*/false,
11649                                          /*check_dependency_p=*/true,
11650                                          /*declarator_p=*/true,
11651                                          /*optional_p=*/false);
11652
11653   if (access_declaration_p)
11654     {
11655       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11656         cp_parser_simulate_error (parser);
11657       if (!cp_parser_parse_definitely (parser))
11658         return false;
11659     }
11660
11661   /* The function we call to handle a using-declaration is different
11662      depending on what scope we are in.  */
11663   if (qscope == error_mark_node || identifier == error_mark_node)
11664     ;
11665   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11666            && TREE_CODE (identifier) != BIT_NOT_EXPR)
11667     /* [namespace.udecl]
11668
11669        A using declaration shall not name a template-id.  */
11670     error ("a template-id may not appear in a using-declaration");
11671   else
11672     {
11673       if (at_class_scope_p ())
11674         {
11675           /* Create the USING_DECL.  */
11676           decl = do_class_using_decl (parser->scope, identifier);
11677           /* Add it to the list of members in this class.  */
11678           finish_member_declaration (decl);
11679         }
11680       else
11681         {
11682           decl = cp_parser_lookup_name_simple (parser, identifier);
11683           if (decl == error_mark_node)
11684             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11685           else if (!at_namespace_scope_p ())
11686             do_local_using_decl (decl, qscope, identifier);
11687           else
11688             do_toplevel_using_decl (decl, qscope, identifier);
11689         }
11690     }
11691
11692   /* Look for the final `;'.  */
11693   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11694   
11695   return true;
11696 }
11697
11698 /* Parse a using-directive.
11699
11700    using-directive:
11701      using namespace :: [opt] nested-name-specifier [opt]
11702        namespace-name ;  */
11703
11704 static void
11705 cp_parser_using_directive (cp_parser* parser)
11706 {
11707   tree namespace_decl;
11708   tree attribs;
11709
11710   /* Look for the `using' keyword.  */
11711   cp_parser_require_keyword (parser, RID_USING, "`using'");
11712   /* And the `namespace' keyword.  */
11713   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11714   /* Look for the optional `::' operator.  */
11715   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11716   /* And the optional nested-name-specifier.  */
11717   cp_parser_nested_name_specifier_opt (parser,
11718                                        /*typename_keyword_p=*/false,
11719                                        /*check_dependency_p=*/true,
11720                                        /*type_p=*/false,
11721                                        /*is_declaration=*/true);
11722   /* Get the namespace being used.  */
11723   namespace_decl = cp_parser_namespace_name (parser);
11724   /* And any specified attributes.  */
11725   attribs = cp_parser_attributes_opt (parser);
11726   /* Update the symbol table.  */
11727   parse_using_directive (namespace_decl, attribs);
11728   /* Look for the final `;'.  */
11729   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11730 }
11731
11732 /* Parse an asm-definition.
11733
11734    asm-definition:
11735      asm ( string-literal ) ;
11736
11737    GNU Extension:
11738
11739    asm-definition:
11740      asm volatile [opt] ( string-literal ) ;
11741      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11742      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11743                           : asm-operand-list [opt] ) ;
11744      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11745                           : asm-operand-list [opt]
11746                           : asm-operand-list [opt] ) ;  */
11747
11748 static void
11749 cp_parser_asm_definition (cp_parser* parser)
11750 {
11751   tree string;
11752   tree outputs = NULL_TREE;
11753   tree inputs = NULL_TREE;
11754   tree clobbers = NULL_TREE;
11755   tree asm_stmt;
11756   bool volatile_p = false;
11757   bool extended_p = false;
11758   bool invalid_inputs_p = false;
11759   bool invalid_outputs_p = false;
11760
11761   /* Look for the `asm' keyword.  */
11762   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11763   /* See if the next token is `volatile'.  */
11764   if (cp_parser_allow_gnu_extensions_p (parser)
11765       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11766     {
11767       /* Remember that we saw the `volatile' keyword.  */
11768       volatile_p = true;
11769       /* Consume the token.  */
11770       cp_lexer_consume_token (parser->lexer);
11771     }
11772   /* Look for the opening `('.  */
11773   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11774     return;
11775   /* Look for the string.  */
11776   string = cp_parser_string_literal (parser, false, false);
11777   if (string == error_mark_node)
11778     {
11779       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11780                                              /*consume_paren=*/true);
11781       return;
11782     }
11783
11784   /* If we're allowing GNU extensions, check for the extended assembly
11785      syntax.  Unfortunately, the `:' tokens need not be separated by
11786      a space in C, and so, for compatibility, we tolerate that here
11787      too.  Doing that means that we have to treat the `::' operator as
11788      two `:' tokens.  */
11789   if (cp_parser_allow_gnu_extensions_p (parser)
11790       && parser->in_function_body
11791       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11792           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11793     {
11794       bool inputs_p = false;
11795       bool clobbers_p = false;
11796
11797       /* The extended syntax was used.  */
11798       extended_p = true;
11799
11800       /* Look for outputs.  */
11801       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11802         {
11803           /* Consume the `:'.  */
11804           cp_lexer_consume_token (parser->lexer);
11805           /* Parse the output-operands.  */
11806           if (cp_lexer_next_token_is_not (parser->lexer,
11807                                           CPP_COLON)
11808               && cp_lexer_next_token_is_not (parser->lexer,
11809                                              CPP_SCOPE)
11810               && cp_lexer_next_token_is_not (parser->lexer,
11811                                              CPP_CLOSE_PAREN))
11812             outputs = cp_parser_asm_operand_list (parser);
11813
11814             if (outputs == error_mark_node)
11815               invalid_outputs_p = true;
11816         }
11817       /* If the next token is `::', there are no outputs, and the
11818          next token is the beginning of the inputs.  */
11819       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11820         /* The inputs are coming next.  */
11821         inputs_p = true;
11822
11823       /* Look for inputs.  */
11824       if (inputs_p
11825           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11826         {
11827           /* Consume the `:' or `::'.  */
11828           cp_lexer_consume_token (parser->lexer);
11829           /* Parse the output-operands.  */
11830           if (cp_lexer_next_token_is_not (parser->lexer,
11831                                           CPP_COLON)
11832               && cp_lexer_next_token_is_not (parser->lexer,
11833                                              CPP_CLOSE_PAREN))
11834             inputs = cp_parser_asm_operand_list (parser);
11835
11836             if (inputs == error_mark_node)
11837               invalid_inputs_p = true;
11838         }
11839       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11840         /* The clobbers are coming next.  */
11841         clobbers_p = true;
11842
11843       /* Look for clobbers.  */
11844       if (clobbers_p
11845           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11846         {
11847           /* Consume the `:' or `::'.  */
11848           cp_lexer_consume_token (parser->lexer);
11849           /* Parse the clobbers.  */
11850           if (cp_lexer_next_token_is_not (parser->lexer,
11851                                           CPP_CLOSE_PAREN))
11852             clobbers = cp_parser_asm_clobber_list (parser);
11853         }
11854     }
11855   /* Look for the closing `)'.  */
11856   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11857     cp_parser_skip_to_closing_parenthesis (parser, true, false,
11858                                            /*consume_paren=*/true);
11859   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11860
11861   if (!invalid_inputs_p && !invalid_outputs_p)
11862     {
11863       /* Create the ASM_EXPR.  */
11864       if (parser->in_function_body)
11865         {
11866           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11867                                       inputs, clobbers);
11868           /* If the extended syntax was not used, mark the ASM_EXPR.  */
11869           if (!extended_p)
11870             {
11871               tree temp = asm_stmt;
11872               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11873                 temp = TREE_OPERAND (temp, 0);
11874
11875               ASM_INPUT_P (temp) = 1;
11876             }
11877         }
11878       else
11879         cgraph_add_asm_node (string);
11880     }
11881 }
11882
11883 /* Declarators [gram.dcl.decl] */
11884
11885 /* Parse an init-declarator.
11886
11887    init-declarator:
11888      declarator initializer [opt]
11889
11890    GNU Extension:
11891
11892    init-declarator:
11893      declarator asm-specification [opt] attributes [opt] initializer [opt]
11894
11895    function-definition:
11896      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11897        function-body
11898      decl-specifier-seq [opt] declarator function-try-block
11899
11900    GNU Extension:
11901
11902    function-definition:
11903      __extension__ function-definition
11904
11905    The DECL_SPECIFIERS apply to this declarator.  Returns a
11906    representation of the entity declared.  If MEMBER_P is TRUE, then
11907    this declarator appears in a class scope.  The new DECL created by
11908    this declarator is returned.
11909
11910    The CHECKS are access checks that should be performed once we know
11911    what entity is being declared (and, therefore, what classes have
11912    befriended it).
11913
11914    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11915    for a function-definition here as well.  If the declarator is a
11916    declarator for a function-definition, *FUNCTION_DEFINITION_P will
11917    be TRUE upon return.  By that point, the function-definition will
11918    have been completely parsed.
11919
11920    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11921    is FALSE.  */
11922
11923 static tree
11924 cp_parser_init_declarator (cp_parser* parser,
11925                            cp_decl_specifier_seq *decl_specifiers,
11926                            VEC (deferred_access_check,gc)* checks,
11927                            bool function_definition_allowed_p,
11928                            bool member_p,
11929                            int declares_class_or_enum,
11930                            bool* function_definition_p)
11931 {
11932   cp_token *token;
11933   cp_declarator *declarator;
11934   tree prefix_attributes;
11935   tree attributes;
11936   tree asm_specification;
11937   tree initializer;
11938   tree decl = NULL_TREE;
11939   tree scope;
11940   bool is_initialized;
11941   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
11942      initialized with "= ..", CPP_OPEN_PAREN if initialized with
11943      "(...)".  */
11944   enum cpp_ttype initialization_kind;
11945   bool is_parenthesized_init = false;
11946   bool is_non_constant_init;
11947   int ctor_dtor_or_conv_p;
11948   bool friend_p;
11949   tree pushed_scope = NULL;
11950
11951   /* Gather the attributes that were provided with the
11952      decl-specifiers.  */
11953   prefix_attributes = decl_specifiers->attributes;
11954
11955   /* Assume that this is not the declarator for a function
11956      definition.  */
11957   if (function_definition_p)
11958     *function_definition_p = false;
11959
11960   /* Defer access checks while parsing the declarator; we cannot know
11961      what names are accessible until we know what is being
11962      declared.  */
11963   resume_deferring_access_checks ();
11964
11965   /* Parse the declarator.  */
11966   declarator
11967     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11968                             &ctor_dtor_or_conv_p,
11969                             /*parenthesized_p=*/NULL,
11970                             /*member_p=*/false);
11971   /* Gather up the deferred checks.  */
11972   stop_deferring_access_checks ();
11973
11974   /* If the DECLARATOR was erroneous, there's no need to go
11975      further.  */
11976   if (declarator == cp_error_declarator)
11977     return error_mark_node;
11978
11979   /* Check that the number of template-parameter-lists is OK.  */
11980   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11981     return error_mark_node;
11982
11983   if (declares_class_or_enum & 2)
11984     cp_parser_check_for_definition_in_return_type (declarator,
11985                                                    decl_specifiers->type);
11986
11987   /* Figure out what scope the entity declared by the DECLARATOR is
11988      located in.  `grokdeclarator' sometimes changes the scope, so
11989      we compute it now.  */
11990   scope = get_scope_of_declarator (declarator);
11991
11992   /* If we're allowing GNU extensions, look for an asm-specification
11993      and attributes.  */
11994   if (cp_parser_allow_gnu_extensions_p (parser))
11995     {
11996       /* Look for an asm-specification.  */
11997       asm_specification = cp_parser_asm_specification_opt (parser);
11998       /* And attributes.  */
11999       attributes = cp_parser_attributes_opt (parser);
12000     }
12001   else
12002     {
12003       asm_specification = NULL_TREE;
12004       attributes = NULL_TREE;
12005     }
12006
12007   /* Peek at the next token.  */
12008   token = cp_lexer_peek_token (parser->lexer);
12009   /* Check to see if the token indicates the start of a
12010      function-definition.  */
12011   if (cp_parser_token_starts_function_definition_p (token))
12012     {
12013       if (!function_definition_allowed_p)
12014         {
12015           /* If a function-definition should not appear here, issue an
12016              error message.  */
12017           cp_parser_error (parser,
12018                            "a function-definition is not allowed here");
12019           return error_mark_node;
12020         }
12021       else
12022         {
12023           /* Neither attributes nor an asm-specification are allowed
12024              on a function-definition.  */
12025           if (asm_specification)
12026             error ("an asm-specification is not allowed on a function-definition");
12027           if (attributes)
12028             error ("attributes are not allowed on a function-definition");
12029           /* This is a function-definition.  */
12030           *function_definition_p = true;
12031
12032           /* Parse the function definition.  */
12033           if (member_p)
12034             decl = cp_parser_save_member_function_body (parser,
12035                                                         decl_specifiers,
12036                                                         declarator,
12037                                                         prefix_attributes);
12038           else
12039             decl
12040               = (cp_parser_function_definition_from_specifiers_and_declarator
12041                  (parser, decl_specifiers, prefix_attributes, declarator));
12042
12043           return decl;
12044         }
12045     }
12046
12047   /* [dcl.dcl]
12048
12049      Only in function declarations for constructors, destructors, and
12050      type conversions can the decl-specifier-seq be omitted.
12051
12052      We explicitly postpone this check past the point where we handle
12053      function-definitions because we tolerate function-definitions
12054      that are missing their return types in some modes.  */
12055   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12056     {
12057       cp_parser_error (parser,
12058                        "expected constructor, destructor, or type conversion");
12059       return error_mark_node;
12060     }
12061
12062   /* An `=' or an `(' indicates an initializer.  */
12063   if (token->type == CPP_EQ
12064       || token->type == CPP_OPEN_PAREN)
12065     {
12066       is_initialized = true;
12067       initialization_kind = token->type;
12068     }
12069   else
12070     {
12071       /* If the init-declarator isn't initialized and isn't followed by a
12072          `,' or `;', it's not a valid init-declarator.  */
12073       if (token->type != CPP_COMMA
12074           && token->type != CPP_SEMICOLON)
12075         {
12076           cp_parser_error (parser, "expected initializer");
12077           return error_mark_node;
12078         }
12079       is_initialized = false;
12080       initialization_kind = CPP_EOF;
12081     }
12082
12083   /* Because start_decl has side-effects, we should only call it if we
12084      know we're going ahead.  By this point, we know that we cannot
12085      possibly be looking at any other construct.  */
12086   cp_parser_commit_to_tentative_parse (parser);
12087
12088   /* If the decl specifiers were bad, issue an error now that we're
12089      sure this was intended to be a declarator.  Then continue
12090      declaring the variable(s), as int, to try to cut down on further
12091      errors.  */
12092   if (decl_specifiers->any_specifiers_p
12093       && decl_specifiers->type == error_mark_node)
12094     {
12095       cp_parser_error (parser, "invalid type in declaration");
12096       decl_specifiers->type = integer_type_node;
12097     }
12098
12099   /* Check to see whether or not this declaration is a friend.  */
12100   friend_p = cp_parser_friend_p (decl_specifiers);
12101
12102   /* Enter the newly declared entry in the symbol table.  If we're
12103      processing a declaration in a class-specifier, we wait until
12104      after processing the initializer.  */
12105   if (!member_p)
12106     {
12107       if (parser->in_unbraced_linkage_specification_p)
12108         decl_specifiers->storage_class = sc_extern;
12109       decl = start_decl (declarator, decl_specifiers,
12110                          is_initialized, attributes, prefix_attributes,
12111                          &pushed_scope);
12112     }
12113   else if (scope)
12114     /* Enter the SCOPE.  That way unqualified names appearing in the
12115        initializer will be looked up in SCOPE.  */
12116     pushed_scope = push_scope (scope);
12117
12118   /* Perform deferred access control checks, now that we know in which
12119      SCOPE the declared entity resides.  */
12120   if (!member_p && decl)
12121     {
12122       tree saved_current_function_decl = NULL_TREE;
12123
12124       /* If the entity being declared is a function, pretend that we
12125          are in its scope.  If it is a `friend', it may have access to
12126          things that would not otherwise be accessible.  */
12127       if (TREE_CODE (decl) == FUNCTION_DECL)
12128         {
12129           saved_current_function_decl = current_function_decl;
12130           current_function_decl = decl;
12131         }
12132
12133       /* Perform access checks for template parameters.  */
12134       cp_parser_perform_template_parameter_access_checks (checks);
12135
12136       /* Perform the access control checks for the declarator and the
12137          the decl-specifiers.  */
12138       perform_deferred_access_checks ();
12139
12140       /* Restore the saved value.  */
12141       if (TREE_CODE (decl) == FUNCTION_DECL)
12142         current_function_decl = saved_current_function_decl;
12143     }
12144
12145   /* Parse the initializer.  */
12146   initializer = NULL_TREE;
12147   is_parenthesized_init = false;
12148   is_non_constant_init = true;
12149   if (is_initialized)
12150     {
12151       if (function_declarator_p (declarator))
12152         {
12153            if (initialization_kind == CPP_EQ)
12154              initializer = cp_parser_pure_specifier (parser);
12155            else
12156              {
12157                /* If the declaration was erroneous, we don't really
12158                   know what the user intended, so just silently
12159                   consume the initializer.  */
12160                if (decl != error_mark_node)
12161                  error ("initializer provided for function");
12162                cp_parser_skip_to_closing_parenthesis (parser,
12163                                                       /*recovering=*/true,
12164                                                       /*or_comma=*/false,
12165                                                       /*consume_paren=*/true);
12166              }
12167         }
12168       else
12169         initializer = cp_parser_initializer (parser,
12170                                              &is_parenthesized_init,
12171                                              &is_non_constant_init);
12172     }
12173
12174   /* The old parser allows attributes to appear after a parenthesized
12175      initializer.  Mark Mitchell proposed removing this functionality
12176      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12177      attributes -- but ignores them.  */
12178   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12179     if (cp_parser_attributes_opt (parser))
12180       warning (OPT_Wattributes,
12181                "attributes after parenthesized initializer ignored");
12182
12183   /* For an in-class declaration, use `grokfield' to create the
12184      declaration.  */
12185   if (member_p)
12186     {
12187       if (pushed_scope)
12188         {
12189           pop_scope (pushed_scope);
12190           pushed_scope = false;
12191         }
12192       decl = grokfield (declarator, decl_specifiers,
12193                         initializer, !is_non_constant_init,
12194                         /*asmspec=*/NULL_TREE,
12195                         prefix_attributes);
12196       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12197         cp_parser_save_default_args (parser, decl);
12198     }
12199
12200   /* Finish processing the declaration.  But, skip friend
12201      declarations.  */
12202   if (!friend_p && decl && decl != error_mark_node)
12203     {
12204       cp_finish_decl (decl,
12205                       initializer, !is_non_constant_init,
12206                       asm_specification,
12207                       /* If the initializer is in parentheses, then this is
12208                          a direct-initialization, which means that an
12209                          `explicit' constructor is OK.  Otherwise, an
12210                          `explicit' constructor cannot be used.  */
12211                       ((is_parenthesized_init || !is_initialized)
12212                      ? 0 : LOOKUP_ONLYCONVERTING));
12213     }
12214   else if ((cxx_dialect != cxx98) && friend_p
12215            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12216     /* Core issue #226 (C++0x only): A default template-argument
12217        shall not be specified in a friend class template
12218        declaration. */
12219     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12220                              /*is_partial=*/0, /*is_friend_decl=*/1);
12221
12222   if (!friend_p && pushed_scope)
12223     pop_scope (pushed_scope);
12224
12225   return decl;
12226 }
12227
12228 /* Parse a declarator.
12229
12230    declarator:
12231      direct-declarator
12232      ptr-operator declarator
12233
12234    abstract-declarator:
12235      ptr-operator abstract-declarator [opt]
12236      direct-abstract-declarator
12237
12238    GNU Extensions:
12239
12240    declarator:
12241      attributes [opt] direct-declarator
12242      attributes [opt] ptr-operator declarator
12243
12244    abstract-declarator:
12245      attributes [opt] ptr-operator abstract-declarator [opt]
12246      attributes [opt] direct-abstract-declarator
12247
12248    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12249    detect constructor, destructor or conversion operators. It is set
12250    to -1 if the declarator is a name, and +1 if it is a
12251    function. Otherwise it is set to zero. Usually you just want to
12252    test for >0, but internally the negative value is used.
12253
12254    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12255    a decl-specifier-seq unless it declares a constructor, destructor,
12256    or conversion.  It might seem that we could check this condition in
12257    semantic analysis, rather than parsing, but that makes it difficult
12258    to handle something like `f()'.  We want to notice that there are
12259    no decl-specifiers, and therefore realize that this is an
12260    expression, not a declaration.)
12261
12262    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12263    the declarator is a direct-declarator of the form "(...)".
12264
12265    MEMBER_P is true iff this declarator is a member-declarator.  */
12266
12267 static cp_declarator *
12268 cp_parser_declarator (cp_parser* parser,
12269                       cp_parser_declarator_kind dcl_kind,
12270                       int* ctor_dtor_or_conv_p,
12271                       bool* parenthesized_p,
12272                       bool member_p)
12273 {
12274   cp_token *token;
12275   cp_declarator *declarator;
12276   enum tree_code code;
12277   cp_cv_quals cv_quals;
12278   tree class_type;
12279   tree attributes = NULL_TREE;
12280
12281   /* Assume this is not a constructor, destructor, or type-conversion
12282      operator.  */
12283   if (ctor_dtor_or_conv_p)
12284     *ctor_dtor_or_conv_p = 0;
12285
12286   if (cp_parser_allow_gnu_extensions_p (parser))
12287     attributes = cp_parser_attributes_opt (parser);
12288
12289   /* Peek at the next token.  */
12290   token = cp_lexer_peek_token (parser->lexer);
12291
12292   /* Check for the ptr-operator production.  */
12293   cp_parser_parse_tentatively (parser);
12294   /* Parse the ptr-operator.  */
12295   code = cp_parser_ptr_operator (parser,
12296                                  &class_type,
12297                                  &cv_quals);
12298   /* If that worked, then we have a ptr-operator.  */
12299   if (cp_parser_parse_definitely (parser))
12300     {
12301       /* If a ptr-operator was found, then this declarator was not
12302          parenthesized.  */
12303       if (parenthesized_p)
12304         *parenthesized_p = true;
12305       /* The dependent declarator is optional if we are parsing an
12306          abstract-declarator.  */
12307       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12308         cp_parser_parse_tentatively (parser);
12309
12310       /* Parse the dependent declarator.  */
12311       declarator = cp_parser_declarator (parser, dcl_kind,
12312                                          /*ctor_dtor_or_conv_p=*/NULL,
12313                                          /*parenthesized_p=*/NULL,
12314                                          /*member_p=*/false);
12315
12316       /* If we are parsing an abstract-declarator, we must handle the
12317          case where the dependent declarator is absent.  */
12318       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12319           && !cp_parser_parse_definitely (parser))
12320         declarator = NULL;
12321
12322       declarator = cp_parser_make_indirect_declarator
12323         (code, class_type, cv_quals, declarator);
12324     }
12325   /* Everything else is a direct-declarator.  */
12326   else
12327     {
12328       if (parenthesized_p)
12329         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12330                                                    CPP_OPEN_PAREN);
12331       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12332                                                 ctor_dtor_or_conv_p,
12333                                                 member_p);
12334     }
12335
12336   if (attributes && declarator && declarator != cp_error_declarator)
12337     declarator->attributes = attributes;
12338
12339   return declarator;
12340 }
12341
12342 /* Parse a direct-declarator or direct-abstract-declarator.
12343
12344    direct-declarator:
12345      declarator-id
12346      direct-declarator ( parameter-declaration-clause )
12347        cv-qualifier-seq [opt]
12348        exception-specification [opt]
12349      direct-declarator [ constant-expression [opt] ]
12350      ( declarator )
12351
12352    direct-abstract-declarator:
12353      direct-abstract-declarator [opt]
12354        ( parameter-declaration-clause )
12355        cv-qualifier-seq [opt]
12356        exception-specification [opt]
12357      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12358      ( abstract-declarator )
12359
12360    Returns a representation of the declarator.  DCL_KIND is
12361    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12362    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12363    we are parsing a direct-declarator.  It is
12364    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12365    of ambiguity we prefer an abstract declarator, as per
12366    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12367    cp_parser_declarator.  */
12368
12369 static cp_declarator *
12370 cp_parser_direct_declarator (cp_parser* parser,
12371                              cp_parser_declarator_kind dcl_kind,
12372                              int* ctor_dtor_or_conv_p,
12373                              bool member_p)
12374 {
12375   cp_token *token;
12376   cp_declarator *declarator = NULL;
12377   tree scope = NULL_TREE;
12378   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12379   bool saved_in_declarator_p = parser->in_declarator_p;
12380   bool first = true;
12381   tree pushed_scope = NULL_TREE;
12382
12383   while (true)
12384     {
12385       /* Peek at the next token.  */
12386       token = cp_lexer_peek_token (parser->lexer);
12387       if (token->type == CPP_OPEN_PAREN)
12388         {
12389           /* This is either a parameter-declaration-clause, or a
12390              parenthesized declarator. When we know we are parsing a
12391              named declarator, it must be a parenthesized declarator
12392              if FIRST is true. For instance, `(int)' is a
12393              parameter-declaration-clause, with an omitted
12394              direct-abstract-declarator. But `((*))', is a
12395              parenthesized abstract declarator. Finally, when T is a
12396              template parameter `(T)' is a
12397              parameter-declaration-clause, and not a parenthesized
12398              named declarator.
12399
12400              We first try and parse a parameter-declaration-clause,
12401              and then try a nested declarator (if FIRST is true).
12402
12403              It is not an error for it not to be a
12404              parameter-declaration-clause, even when FIRST is
12405              false. Consider,
12406
12407                int i (int);
12408                int i (3);
12409
12410              The first is the declaration of a function while the
12411              second is a the definition of a variable, including its
12412              initializer.
12413
12414              Having seen only the parenthesis, we cannot know which of
12415              these two alternatives should be selected.  Even more
12416              complex are examples like:
12417
12418                int i (int (a));
12419                int i (int (3));
12420
12421              The former is a function-declaration; the latter is a
12422              variable initialization.
12423
12424              Thus again, we try a parameter-declaration-clause, and if
12425              that fails, we back out and return.  */
12426
12427           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12428             {
12429               cp_parameter_declarator *params;
12430               unsigned saved_num_template_parameter_lists;
12431
12432               /* In a member-declarator, the only valid interpretation
12433                  of a parenthesis is the start of a
12434                  parameter-declaration-clause.  (It is invalid to
12435                  initialize a static data member with a parenthesized
12436                  initializer; only the "=" form of initialization is
12437                  permitted.)  */
12438               if (!member_p)
12439                 cp_parser_parse_tentatively (parser);
12440
12441               /* Consume the `('.  */
12442               cp_lexer_consume_token (parser->lexer);
12443               if (first)
12444                 {
12445                   /* If this is going to be an abstract declarator, we're
12446                      in a declarator and we can't have default args.  */
12447                   parser->default_arg_ok_p = false;
12448                   parser->in_declarator_p = true;
12449                 }
12450
12451               /* Inside the function parameter list, surrounding
12452                  template-parameter-lists do not apply.  */
12453               saved_num_template_parameter_lists
12454                 = parser->num_template_parameter_lists;
12455               parser->num_template_parameter_lists = 0;
12456
12457               /* Parse the parameter-declaration-clause.  */
12458               params = cp_parser_parameter_declaration_clause (parser);
12459
12460               parser->num_template_parameter_lists
12461                 = saved_num_template_parameter_lists;
12462
12463               /* If all went well, parse the cv-qualifier-seq and the
12464                  exception-specification.  */
12465               if (member_p || cp_parser_parse_definitely (parser))
12466                 {
12467                   cp_cv_quals cv_quals;
12468                   tree exception_specification;
12469
12470                   if (ctor_dtor_or_conv_p)
12471                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12472                   first = false;
12473                   /* Consume the `)'.  */
12474                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12475
12476                   /* Parse the cv-qualifier-seq.  */
12477                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12478                   /* And the exception-specification.  */
12479                   exception_specification
12480                     = cp_parser_exception_specification_opt (parser);
12481
12482                   /* Create the function-declarator.  */
12483                   declarator = make_call_declarator (declarator,
12484                                                      params,
12485                                                      cv_quals,
12486                                                      exception_specification);
12487                   /* Any subsequent parameter lists are to do with
12488                      return type, so are not those of the declared
12489                      function.  */
12490                   parser->default_arg_ok_p = false;
12491
12492                   /* Repeat the main loop.  */
12493                   continue;
12494                 }
12495             }
12496
12497           /* If this is the first, we can try a parenthesized
12498              declarator.  */
12499           if (first)
12500             {
12501               bool saved_in_type_id_in_expr_p;
12502
12503               parser->default_arg_ok_p = saved_default_arg_ok_p;
12504               parser->in_declarator_p = saved_in_declarator_p;
12505
12506               /* Consume the `('.  */
12507               cp_lexer_consume_token (parser->lexer);
12508               /* Parse the nested declarator.  */
12509               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12510               parser->in_type_id_in_expr_p = true;
12511               declarator
12512                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12513                                         /*parenthesized_p=*/NULL,
12514                                         member_p);
12515               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12516               first = false;
12517               /* Expect a `)'.  */
12518               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12519                 declarator = cp_error_declarator;
12520               if (declarator == cp_error_declarator)
12521                 break;
12522
12523               goto handle_declarator;
12524             }
12525           /* Otherwise, we must be done.  */
12526           else
12527             break;
12528         }
12529       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12530                && token->type == CPP_OPEN_SQUARE)
12531         {
12532           /* Parse an array-declarator.  */
12533           tree bounds;
12534
12535           if (ctor_dtor_or_conv_p)
12536             *ctor_dtor_or_conv_p = 0;
12537
12538           first = false;
12539           parser->default_arg_ok_p = false;
12540           parser->in_declarator_p = true;
12541           /* Consume the `['.  */
12542           cp_lexer_consume_token (parser->lexer);
12543           /* Peek at the next token.  */
12544           token = cp_lexer_peek_token (parser->lexer);
12545           /* If the next token is `]', then there is no
12546              constant-expression.  */
12547           if (token->type != CPP_CLOSE_SQUARE)
12548             {
12549               bool non_constant_p;
12550
12551               bounds
12552                 = cp_parser_constant_expression (parser,
12553                                                  /*allow_non_constant=*/true,
12554                                                  &non_constant_p);
12555               if (!non_constant_p)
12556                 bounds = fold_non_dependent_expr (bounds);
12557               /* Normally, the array bound must be an integral constant
12558                  expression.  However, as an extension, we allow VLAs
12559                  in function scopes.  */
12560               else if (!parser->in_function_body)
12561                 {
12562                   error ("array bound is not an integer constant");
12563                   bounds = error_mark_node;
12564                 }
12565             }
12566           else
12567             bounds = NULL_TREE;
12568           /* Look for the closing `]'.  */
12569           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12570             {
12571               declarator = cp_error_declarator;
12572               break;
12573             }
12574
12575           declarator = make_array_declarator (declarator, bounds);
12576         }
12577       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12578         {
12579           tree qualifying_scope;
12580           tree unqualified_name;
12581           special_function_kind sfk;
12582           bool abstract_ok;
12583           bool pack_expansion_p = false;
12584
12585           /* Parse a declarator-id */
12586           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12587           if (abstract_ok)
12588             {
12589               cp_parser_parse_tentatively (parser);
12590
12591               /* If we see an ellipsis, we should be looking at a
12592                  parameter pack. */
12593               if (token->type == CPP_ELLIPSIS)
12594                 {
12595                   /* Consume the `...' */
12596                   cp_lexer_consume_token (parser->lexer);
12597
12598                   pack_expansion_p = true;
12599                 }
12600             }
12601
12602           unqualified_name
12603             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12604           qualifying_scope = parser->scope;
12605           if (abstract_ok)
12606             {
12607               bool okay = false;
12608
12609               if (!unqualified_name && pack_expansion_p)
12610                 {
12611                   /* Check whether an error occurred. */
12612                   okay = !cp_parser_error_occurred (parser);
12613
12614                   /* We already consumed the ellipsis to mark a
12615                      parameter pack, but we have no way to report it,
12616                      so abort the tentative parse. We will be exiting
12617                      immediately anyway. */
12618                   cp_parser_abort_tentative_parse (parser);
12619                 }
12620               else
12621                 okay = cp_parser_parse_definitely (parser);
12622
12623               if (!okay)
12624                 unqualified_name = error_mark_node;
12625               else if (unqualified_name
12626                        && (qualifying_scope
12627                            || (TREE_CODE (unqualified_name)
12628                                != IDENTIFIER_NODE)))
12629                 {
12630                   cp_parser_error (parser, "expected unqualified-id");
12631                   unqualified_name = error_mark_node;
12632                 }
12633             }
12634
12635           if (!unqualified_name)
12636             return NULL;
12637           if (unqualified_name == error_mark_node)
12638             {
12639               declarator = cp_error_declarator;
12640               pack_expansion_p = false;
12641               declarator->parameter_pack_p = false;
12642               break;
12643             }
12644
12645           if (qualifying_scope && at_namespace_scope_p ()
12646               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12647             {
12648               /* In the declaration of a member of a template class
12649                  outside of the class itself, the SCOPE will sometimes
12650                  be a TYPENAME_TYPE.  For example, given:
12651
12652                  template <typename T>
12653                  int S<T>::R::i = 3;
12654
12655                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
12656                  this context, we must resolve S<T>::R to an ordinary
12657                  type, rather than a typename type.
12658
12659                  The reason we normally avoid resolving TYPENAME_TYPEs
12660                  is that a specialization of `S' might render
12661                  `S<T>::R' not a type.  However, if `S' is
12662                  specialized, then this `i' will not be used, so there
12663                  is no harm in resolving the types here.  */
12664               tree type;
12665
12666               /* Resolve the TYPENAME_TYPE.  */
12667               type = resolve_typename_type (qualifying_scope,
12668                                             /*only_current_p=*/false);
12669               /* If that failed, the declarator is invalid.  */
12670               if (TREE_CODE (type) == TYPENAME_TYPE)
12671                 error ("%<%T::%E%> is not a type",
12672                        TYPE_CONTEXT (qualifying_scope),
12673                        TYPE_IDENTIFIER (qualifying_scope));
12674               qualifying_scope = type;
12675             }
12676
12677           sfk = sfk_none;
12678
12679           if (unqualified_name)
12680             {
12681               tree class_type;
12682
12683               if (qualifying_scope
12684                   && CLASS_TYPE_P (qualifying_scope))
12685                 class_type = qualifying_scope;
12686               else
12687                 class_type = current_class_type;
12688
12689               if (TREE_CODE (unqualified_name) == TYPE_DECL)
12690                 {
12691                   tree name_type = TREE_TYPE (unqualified_name);
12692                   if (class_type && same_type_p (name_type, class_type))
12693                     {
12694                       if (qualifying_scope
12695                           && CLASSTYPE_USE_TEMPLATE (name_type))
12696                         {
12697                           error ("invalid use of constructor as a template");
12698                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12699                                   "name the constructor in a qualified name",
12700                                   class_type,
12701                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12702                                   class_type, name_type);
12703                           declarator = cp_error_declarator;
12704                           break;
12705                         }
12706                       else
12707                         unqualified_name = constructor_name (class_type);
12708                     }
12709                   else
12710                     {
12711                       /* We do not attempt to print the declarator
12712                          here because we do not have enough
12713                          information about its original syntactic
12714                          form.  */
12715                       cp_parser_error (parser, "invalid declarator");
12716                       declarator = cp_error_declarator;
12717                       break;
12718                     }
12719                 }
12720
12721               if (class_type)
12722                 {
12723                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12724                     sfk = sfk_destructor;
12725                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12726                     sfk = sfk_conversion;
12727                   else if (/* There's no way to declare a constructor
12728                               for an anonymous type, even if the type
12729                               got a name for linkage purposes.  */
12730                            !TYPE_WAS_ANONYMOUS (class_type)
12731                            && constructor_name_p (unqualified_name,
12732                                                   class_type))
12733                     {
12734                       unqualified_name = constructor_name (class_type);
12735                       sfk = sfk_constructor;
12736                     }
12737
12738                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
12739                     *ctor_dtor_or_conv_p = -1;
12740                 }
12741             }
12742           declarator = make_id_declarator (qualifying_scope,
12743                                            unqualified_name,
12744                                            sfk);
12745           declarator->id_loc = token->location;
12746           declarator->parameter_pack_p = pack_expansion_p;
12747
12748           if (pack_expansion_p)
12749             maybe_warn_variadic_templates ();
12750
12751         handle_declarator:;
12752           scope = get_scope_of_declarator (declarator);
12753           if (scope)
12754             /* Any names that appear after the declarator-id for a
12755                member are looked up in the containing scope.  */
12756             pushed_scope = push_scope (scope);
12757           parser->in_declarator_p = true;
12758           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12759               || (declarator && declarator->kind == cdk_id))
12760             /* Default args are only allowed on function
12761                declarations.  */
12762             parser->default_arg_ok_p = saved_default_arg_ok_p;
12763           else
12764             parser->default_arg_ok_p = false;
12765
12766           first = false;
12767         }
12768       /* We're done.  */
12769       else
12770         break;
12771     }
12772
12773   /* For an abstract declarator, we might wind up with nothing at this
12774      point.  That's an error; the declarator is not optional.  */
12775   if (!declarator)
12776     cp_parser_error (parser, "expected declarator");
12777
12778   /* If we entered a scope, we must exit it now.  */
12779   if (pushed_scope)
12780     pop_scope (pushed_scope);
12781
12782   parser->default_arg_ok_p = saved_default_arg_ok_p;
12783   parser->in_declarator_p = saved_in_declarator_p;
12784
12785   return declarator;
12786 }
12787
12788 /* Parse a ptr-operator.
12789
12790    ptr-operator:
12791      * cv-qualifier-seq [opt]
12792      &
12793      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12794
12795    GNU Extension:
12796
12797    ptr-operator:
12798      & cv-qualifier-seq [opt]
12799
12800    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12801    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12802    an rvalue reference. In the case of a pointer-to-member, *TYPE is
12803    filled in with the TYPE containing the member.  *CV_QUALS is
12804    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12805    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
12806    Note that the tree codes returned by this function have nothing
12807    to do with the types of trees that will be eventually be created
12808    to represent the pointer or reference type being parsed. They are
12809    just constants with suggestive names. */
12810 static enum tree_code
12811 cp_parser_ptr_operator (cp_parser* parser,
12812                         tree* type,
12813                         cp_cv_quals *cv_quals)
12814 {
12815   enum tree_code code = ERROR_MARK;
12816   cp_token *token;
12817
12818   /* Assume that it's not a pointer-to-member.  */
12819   *type = NULL_TREE;
12820   /* And that there are no cv-qualifiers.  */
12821   *cv_quals = TYPE_UNQUALIFIED;
12822
12823   /* Peek at the next token.  */
12824   token = cp_lexer_peek_token (parser->lexer);
12825
12826   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
12827   if (token->type == CPP_MULT)
12828     code = INDIRECT_REF;
12829   else if (token->type == CPP_AND)
12830     code = ADDR_EXPR;
12831   else if ((cxx_dialect != cxx98) &&
12832            token->type == CPP_AND_AND) /* C++0x only */
12833     code = NON_LVALUE_EXPR;
12834
12835   if (code != ERROR_MARK)
12836     {
12837       /* Consume the `*', `&' or `&&'.  */
12838       cp_lexer_consume_token (parser->lexer);
12839
12840       /* A `*' can be followed by a cv-qualifier-seq, and so can a
12841          `&', if we are allowing GNU extensions.  (The only qualifier
12842          that can legally appear after `&' is `restrict', but that is
12843          enforced during semantic analysis.  */
12844       if (code == INDIRECT_REF
12845           || cp_parser_allow_gnu_extensions_p (parser))
12846         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12847     }
12848   else
12849     {
12850       /* Try the pointer-to-member case.  */
12851       cp_parser_parse_tentatively (parser);
12852       /* Look for the optional `::' operator.  */
12853       cp_parser_global_scope_opt (parser,
12854                                   /*current_scope_valid_p=*/false);
12855       /* Look for the nested-name specifier.  */
12856       cp_parser_nested_name_specifier (parser,
12857                                        /*typename_keyword_p=*/false,
12858                                        /*check_dependency_p=*/true,
12859                                        /*type_p=*/false,
12860                                        /*is_declaration=*/false);
12861       /* If we found it, and the next token is a `*', then we are
12862          indeed looking at a pointer-to-member operator.  */
12863       if (!cp_parser_error_occurred (parser)
12864           && cp_parser_require (parser, CPP_MULT, "`*'"))
12865         {
12866           /* Indicate that the `*' operator was used.  */
12867           code = INDIRECT_REF;
12868
12869           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12870             error ("%qD is a namespace", parser->scope);
12871           else
12872             {
12873               /* The type of which the member is a member is given by the
12874                  current SCOPE.  */
12875               *type = parser->scope;
12876               /* The next name will not be qualified.  */
12877               parser->scope = NULL_TREE;
12878               parser->qualifying_scope = NULL_TREE;
12879               parser->object_scope = NULL_TREE;
12880               /* Look for the optional cv-qualifier-seq.  */
12881               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12882             }
12883         }
12884       /* If that didn't work we don't have a ptr-operator.  */
12885       if (!cp_parser_parse_definitely (parser))
12886         cp_parser_error (parser, "expected ptr-operator");
12887     }
12888
12889   return code;
12890 }
12891
12892 /* Parse an (optional) cv-qualifier-seq.
12893
12894    cv-qualifier-seq:
12895      cv-qualifier cv-qualifier-seq [opt]
12896
12897    cv-qualifier:
12898      const
12899      volatile
12900
12901    GNU Extension:
12902
12903    cv-qualifier:
12904      __restrict__
12905
12906    Returns a bitmask representing the cv-qualifiers.  */
12907
12908 static cp_cv_quals
12909 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12910 {
12911   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12912
12913   while (true)
12914     {
12915       cp_token *token;
12916       cp_cv_quals cv_qualifier;
12917
12918       /* Peek at the next token.  */
12919       token = cp_lexer_peek_token (parser->lexer);
12920       /* See if it's a cv-qualifier.  */
12921       switch (token->keyword)
12922         {
12923         case RID_CONST:
12924           cv_qualifier = TYPE_QUAL_CONST;
12925           break;
12926
12927         case RID_VOLATILE:
12928           cv_qualifier = TYPE_QUAL_VOLATILE;
12929           break;
12930
12931         case RID_RESTRICT:
12932           cv_qualifier = TYPE_QUAL_RESTRICT;
12933           break;
12934
12935         default:
12936           cv_qualifier = TYPE_UNQUALIFIED;
12937           break;
12938         }
12939
12940       if (!cv_qualifier)
12941         break;
12942
12943       if (cv_quals & cv_qualifier)
12944         {
12945           error ("duplicate cv-qualifier");
12946           cp_lexer_purge_token (parser->lexer);
12947         }
12948       else
12949         {
12950           cp_lexer_consume_token (parser->lexer);
12951           cv_quals |= cv_qualifier;
12952         }
12953     }
12954
12955   return cv_quals;
12956 }
12957
12958 /* Parse a declarator-id.
12959
12960    declarator-id:
12961      id-expression
12962      :: [opt] nested-name-specifier [opt] type-name
12963
12964    In the `id-expression' case, the value returned is as for
12965    cp_parser_id_expression if the id-expression was an unqualified-id.
12966    If the id-expression was a qualified-id, then a SCOPE_REF is
12967    returned.  The first operand is the scope (either a NAMESPACE_DECL
12968    or TREE_TYPE), but the second is still just a representation of an
12969    unqualified-id.  */
12970
12971 static tree
12972 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12973 {
12974   tree id;
12975   /* The expression must be an id-expression.  Assume that qualified
12976      names are the names of types so that:
12977
12978        template <class T>
12979        int S<T>::R::i = 3;
12980
12981      will work; we must treat `S<T>::R' as the name of a type.
12982      Similarly, assume that qualified names are templates, where
12983      required, so that:
12984
12985        template <class T>
12986        int S<T>::R<T>::i = 3;
12987
12988      will work, too.  */
12989   id = cp_parser_id_expression (parser,
12990                                 /*template_keyword_p=*/false,
12991                                 /*check_dependency_p=*/false,
12992                                 /*template_p=*/NULL,
12993                                 /*declarator_p=*/true,
12994                                 optional_p);
12995   if (id && BASELINK_P (id))
12996     id = BASELINK_FUNCTIONS (id);
12997   return id;
12998 }
12999
13000 /* Parse a type-id.
13001
13002    type-id:
13003      type-specifier-seq abstract-declarator [opt]
13004
13005    Returns the TYPE specified.  */
13006
13007 static tree
13008 cp_parser_type_id (cp_parser* parser)
13009 {
13010   cp_decl_specifier_seq type_specifier_seq;
13011   cp_declarator *abstract_declarator;
13012
13013   /* Parse the type-specifier-seq.  */
13014   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13015                                 &type_specifier_seq);
13016   if (type_specifier_seq.type == error_mark_node)
13017     return error_mark_node;
13018
13019   /* There might or might not be an abstract declarator.  */
13020   cp_parser_parse_tentatively (parser);
13021   /* Look for the declarator.  */
13022   abstract_declarator
13023     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13024                             /*parenthesized_p=*/NULL,
13025                             /*member_p=*/false);
13026   /* Check to see if there really was a declarator.  */
13027   if (!cp_parser_parse_definitely (parser))
13028     abstract_declarator = NULL;
13029
13030   return groktypename (&type_specifier_seq, abstract_declarator);
13031 }
13032
13033 /* Parse a type-specifier-seq.
13034
13035    type-specifier-seq:
13036      type-specifier type-specifier-seq [opt]
13037
13038    GNU extension:
13039
13040    type-specifier-seq:
13041      attributes type-specifier-seq [opt]
13042
13043    If IS_CONDITION is true, we are at the start of a "condition",
13044    e.g., we've just seen "if (".
13045
13046    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13047
13048 static void
13049 cp_parser_type_specifier_seq (cp_parser* parser,
13050                               bool is_condition,
13051                               cp_decl_specifier_seq *type_specifier_seq)
13052 {
13053   bool seen_type_specifier = false;
13054   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13055
13056   /* Clear the TYPE_SPECIFIER_SEQ.  */
13057   clear_decl_specs (type_specifier_seq);
13058
13059   /* Parse the type-specifiers and attributes.  */
13060   while (true)
13061     {
13062       tree type_specifier;
13063       bool is_cv_qualifier;
13064
13065       /* Check for attributes first.  */
13066       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13067         {
13068           type_specifier_seq->attributes =
13069             chainon (type_specifier_seq->attributes,
13070                      cp_parser_attributes_opt (parser));
13071           continue;
13072         }
13073
13074       /* Look for the type-specifier.  */
13075       type_specifier = cp_parser_type_specifier (parser,
13076                                                  flags,
13077                                                  type_specifier_seq,
13078                                                  /*is_declaration=*/false,
13079                                                  NULL,
13080                                                  &is_cv_qualifier);
13081       if (!type_specifier)
13082         {
13083           /* If the first type-specifier could not be found, this is not a
13084              type-specifier-seq at all.  */
13085           if (!seen_type_specifier)
13086             {
13087               cp_parser_error (parser, "expected type-specifier");
13088               type_specifier_seq->type = error_mark_node;
13089               return;
13090             }
13091           /* If subsequent type-specifiers could not be found, the
13092              type-specifier-seq is complete.  */
13093           break;
13094         }
13095
13096       seen_type_specifier = true;
13097       /* The standard says that a condition can be:
13098
13099             type-specifier-seq declarator = assignment-expression
13100
13101          However, given:
13102
13103            struct S {};
13104            if (int S = ...)
13105
13106          we should treat the "S" as a declarator, not as a
13107          type-specifier.  The standard doesn't say that explicitly for
13108          type-specifier-seq, but it does say that for
13109          decl-specifier-seq in an ordinary declaration.  Perhaps it
13110          would be clearer just to allow a decl-specifier-seq here, and
13111          then add a semantic restriction that if any decl-specifiers
13112          that are not type-specifiers appear, the program is invalid.  */
13113       if (is_condition && !is_cv_qualifier)
13114         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13115     }
13116
13117   cp_parser_check_decl_spec (type_specifier_seq);
13118 }
13119
13120 /* Parse a parameter-declaration-clause.
13121
13122    parameter-declaration-clause:
13123      parameter-declaration-list [opt] ... [opt]
13124      parameter-declaration-list , ...
13125
13126    Returns a representation for the parameter declarations.  A return
13127    value of NULL indicates a parameter-declaration-clause consisting
13128    only of an ellipsis.  */
13129
13130 static cp_parameter_declarator *
13131 cp_parser_parameter_declaration_clause (cp_parser* parser)
13132 {
13133   cp_parameter_declarator *parameters;
13134   cp_token *token;
13135   bool ellipsis_p;
13136   bool is_error;
13137
13138   /* Peek at the next token.  */
13139   token = cp_lexer_peek_token (parser->lexer);
13140   /* Check for trivial parameter-declaration-clauses.  */
13141   if (token->type == CPP_ELLIPSIS)
13142     {
13143       /* Consume the `...' token.  */
13144       cp_lexer_consume_token (parser->lexer);
13145       return NULL;
13146     }
13147   else if (token->type == CPP_CLOSE_PAREN)
13148     /* There are no parameters.  */
13149     {
13150 #ifndef NO_IMPLICIT_EXTERN_C
13151       if (in_system_header && current_class_type == NULL
13152           && current_lang_name == lang_name_c)
13153         return NULL;
13154       else
13155 #endif
13156         return no_parameters;
13157     }
13158   /* Check for `(void)', too, which is a special case.  */
13159   else if (token->keyword == RID_VOID
13160            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13161                == CPP_CLOSE_PAREN))
13162     {
13163       /* Consume the `void' token.  */
13164       cp_lexer_consume_token (parser->lexer);
13165       /* There are no parameters.  */
13166       return no_parameters;
13167     }
13168
13169   /* Parse the parameter-declaration-list.  */
13170   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13171   /* If a parse error occurred while parsing the
13172      parameter-declaration-list, then the entire
13173      parameter-declaration-clause is erroneous.  */
13174   if (is_error)
13175     return NULL;
13176
13177   /* Peek at the next token.  */
13178   token = cp_lexer_peek_token (parser->lexer);
13179   /* If it's a `,', the clause should terminate with an ellipsis.  */
13180   if (token->type == CPP_COMMA)
13181     {
13182       /* Consume the `,'.  */
13183       cp_lexer_consume_token (parser->lexer);
13184       /* Expect an ellipsis.  */
13185       ellipsis_p
13186         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13187     }
13188   /* It might also be `...' if the optional trailing `,' was
13189      omitted.  */
13190   else if (token->type == CPP_ELLIPSIS)
13191     {
13192       /* Consume the `...' token.  */
13193       cp_lexer_consume_token (parser->lexer);
13194       /* And remember that we saw it.  */
13195       ellipsis_p = true;
13196     }
13197   else
13198     ellipsis_p = false;
13199
13200   /* Finish the parameter list.  */
13201   if (parameters && ellipsis_p)
13202     parameters->ellipsis_p = true;
13203
13204   return parameters;
13205 }
13206
13207 /* Parse a parameter-declaration-list.
13208
13209    parameter-declaration-list:
13210      parameter-declaration
13211      parameter-declaration-list , parameter-declaration
13212
13213    Returns a representation of the parameter-declaration-list, as for
13214    cp_parser_parameter_declaration_clause.  However, the
13215    `void_list_node' is never appended to the list.  Upon return,
13216    *IS_ERROR will be true iff an error occurred.  */
13217
13218 static cp_parameter_declarator *
13219 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13220 {
13221   cp_parameter_declarator *parameters = NULL;
13222   cp_parameter_declarator **tail = &parameters;
13223   bool saved_in_unbraced_linkage_specification_p;
13224
13225   /* Assume all will go well.  */
13226   *is_error = false;
13227   /* The special considerations that apply to a function within an
13228      unbraced linkage specifications do not apply to the parameters
13229      to the function.  */
13230   saved_in_unbraced_linkage_specification_p 
13231     = parser->in_unbraced_linkage_specification_p;
13232   parser->in_unbraced_linkage_specification_p = false;
13233
13234   /* Look for more parameters.  */
13235   while (true)
13236     {
13237       cp_parameter_declarator *parameter;
13238       bool parenthesized_p;
13239       /* Parse the parameter.  */
13240       parameter
13241         = cp_parser_parameter_declaration (parser,
13242                                            /*template_parm_p=*/false,
13243                                            &parenthesized_p);
13244
13245       /* If a parse error occurred parsing the parameter declaration,
13246          then the entire parameter-declaration-list is erroneous.  */
13247       if (!parameter)
13248         {
13249           *is_error = true;
13250           parameters = NULL;
13251           break;
13252         }
13253       /* Add the new parameter to the list.  */
13254       *tail = parameter;
13255       tail = &parameter->next;
13256
13257       /* Peek at the next token.  */
13258       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13259           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13260           /* These are for Objective-C++ */
13261           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13262           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13263         /* The parameter-declaration-list is complete.  */
13264         break;
13265       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13266         {
13267           cp_token *token;
13268
13269           /* Peek at the next token.  */
13270           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13271           /* If it's an ellipsis, then the list is complete.  */
13272           if (token->type == CPP_ELLIPSIS)
13273             break;
13274           /* Otherwise, there must be more parameters.  Consume the
13275              `,'.  */
13276           cp_lexer_consume_token (parser->lexer);
13277           /* When parsing something like:
13278
13279                 int i(float f, double d)
13280
13281              we can tell after seeing the declaration for "f" that we
13282              are not looking at an initialization of a variable "i",
13283              but rather at the declaration of a function "i".
13284
13285              Due to the fact that the parsing of template arguments
13286              (as specified to a template-id) requires backtracking we
13287              cannot use this technique when inside a template argument
13288              list.  */
13289           if (!parser->in_template_argument_list_p
13290               && !parser->in_type_id_in_expr_p
13291               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13292               /* However, a parameter-declaration of the form
13293                  "foat(f)" (which is a valid declaration of a
13294                  parameter "f") can also be interpreted as an
13295                  expression (the conversion of "f" to "float").  */
13296               && !parenthesized_p)
13297             cp_parser_commit_to_tentative_parse (parser);
13298         }
13299       else
13300         {
13301           cp_parser_error (parser, "expected %<,%> or %<...%>");
13302           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13303             cp_parser_skip_to_closing_parenthesis (parser,
13304                                                    /*recovering=*/true,
13305                                                    /*or_comma=*/false,
13306                                                    /*consume_paren=*/false);
13307           break;
13308         }
13309     }
13310
13311   parser->in_unbraced_linkage_specification_p
13312     = saved_in_unbraced_linkage_specification_p;
13313
13314   return parameters;
13315 }
13316
13317 /* Parse a parameter declaration.
13318
13319    parameter-declaration:
13320      decl-specifier-seq ... [opt] declarator
13321      decl-specifier-seq declarator = assignment-expression
13322      decl-specifier-seq ... [opt] abstract-declarator [opt]
13323      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13324
13325    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13326    declares a template parameter.  (In that case, a non-nested `>'
13327    token encountered during the parsing of the assignment-expression
13328    is not interpreted as a greater-than operator.)
13329
13330    Returns a representation of the parameter, or NULL if an error
13331    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13332    true iff the declarator is of the form "(p)".  */
13333
13334 static cp_parameter_declarator *
13335 cp_parser_parameter_declaration (cp_parser *parser,
13336                                  bool template_parm_p,
13337                                  bool *parenthesized_p)
13338 {
13339   int declares_class_or_enum;
13340   bool greater_than_is_operator_p;
13341   cp_decl_specifier_seq decl_specifiers;
13342   cp_declarator *declarator;
13343   tree default_argument;
13344   cp_token *token;
13345   const char *saved_message;
13346
13347   /* In a template parameter, `>' is not an operator.
13348
13349      [temp.param]
13350
13351      When parsing a default template-argument for a non-type
13352      template-parameter, the first non-nested `>' is taken as the end
13353      of the template parameter-list rather than a greater-than
13354      operator.  */
13355   greater_than_is_operator_p = !template_parm_p;
13356
13357   /* Type definitions may not appear in parameter types.  */
13358   saved_message = parser->type_definition_forbidden_message;
13359   parser->type_definition_forbidden_message
13360     = "types may not be defined in parameter types";
13361
13362   /* Parse the declaration-specifiers.  */
13363   cp_parser_decl_specifier_seq (parser,
13364                                 CP_PARSER_FLAGS_NONE,
13365                                 &decl_specifiers,
13366                                 &declares_class_or_enum);
13367   /* If an error occurred, there's no reason to attempt to parse the
13368      rest of the declaration.  */
13369   if (cp_parser_error_occurred (parser))
13370     {
13371       parser->type_definition_forbidden_message = saved_message;
13372       return NULL;
13373     }
13374
13375   /* Peek at the next token.  */
13376   token = cp_lexer_peek_token (parser->lexer);
13377
13378   /* If the next token is a `)', `,', `=', `>', or `...', then there
13379      is no declarator. However, when variadic templates are enabled,
13380      there may be a declarator following `...'.  */
13381   if (token->type == CPP_CLOSE_PAREN
13382       || token->type == CPP_COMMA
13383       || token->type == CPP_EQ
13384       || token->type == CPP_GREATER)
13385     {
13386       declarator = NULL;
13387       if (parenthesized_p)
13388         *parenthesized_p = false;
13389     }
13390   /* Otherwise, there should be a declarator.  */
13391   else
13392     {
13393       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13394       parser->default_arg_ok_p = false;
13395
13396       /* After seeing a decl-specifier-seq, if the next token is not a
13397          "(", there is no possibility that the code is a valid
13398          expression.  Therefore, if parsing tentatively, we commit at
13399          this point.  */
13400       if (!parser->in_template_argument_list_p
13401           /* In an expression context, having seen:
13402
13403                (int((char ...
13404
13405              we cannot be sure whether we are looking at a
13406              function-type (taking a "char" as a parameter) or a cast
13407              of some object of type "char" to "int".  */
13408           && !parser->in_type_id_in_expr_p
13409           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13410           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13411         cp_parser_commit_to_tentative_parse (parser);
13412       /* Parse the declarator.  */
13413       declarator = cp_parser_declarator (parser,
13414                                          CP_PARSER_DECLARATOR_EITHER,
13415                                          /*ctor_dtor_or_conv_p=*/NULL,
13416                                          parenthesized_p,
13417                                          /*member_p=*/false);
13418       parser->default_arg_ok_p = saved_default_arg_ok_p;
13419       /* After the declarator, allow more attributes.  */
13420       decl_specifiers.attributes
13421         = chainon (decl_specifiers.attributes,
13422                    cp_parser_attributes_opt (parser));
13423     }
13424
13425   /* If the next token is an ellipsis, and we have not seen a
13426      declarator name, and the type of the declarator contains parameter
13427      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13428      a parameter pack expansion expression. Otherwise, leave the
13429      ellipsis for a C-style variadic function. */
13430   token = cp_lexer_peek_token (parser->lexer);
13431   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13432     {
13433       tree type = decl_specifiers.type;
13434
13435       if (type && DECL_P (type))
13436         type = TREE_TYPE (type);
13437
13438       if (type
13439           && TREE_CODE (type) != TYPE_PACK_EXPANSION
13440           && declarator_can_be_parameter_pack (declarator)
13441           && (!declarator || !declarator->parameter_pack_p)
13442           && uses_parameter_packs (type))
13443         {
13444           /* Consume the `...'. */
13445           cp_lexer_consume_token (parser->lexer);
13446           maybe_warn_variadic_templates ();
13447           
13448           /* Build a pack expansion type */
13449           if (declarator)
13450             declarator->parameter_pack_p = true;
13451           else
13452             decl_specifiers.type = make_pack_expansion (type);
13453         }
13454     }
13455
13456   /* The restriction on defining new types applies only to the type
13457      of the parameter, not to the default argument.  */
13458   parser->type_definition_forbidden_message = saved_message;
13459
13460   /* If the next token is `=', then process a default argument.  */
13461   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13462     {
13463       bool saved_greater_than_is_operator_p;
13464       /* Consume the `='.  */
13465       cp_lexer_consume_token (parser->lexer);
13466
13467       /* If we are defining a class, then the tokens that make up the
13468          default argument must be saved and processed later.  */
13469       if (!template_parm_p && at_class_scope_p ()
13470           && TYPE_BEING_DEFINED (current_class_type))
13471         {
13472           unsigned depth = 0;
13473           cp_token *first_token;
13474           cp_token *token;
13475
13476           /* Add tokens until we have processed the entire default
13477              argument.  We add the range [first_token, token).  */
13478           first_token = cp_lexer_peek_token (parser->lexer);
13479           while (true)
13480             {
13481               bool done = false;
13482
13483               /* Peek at the next token.  */
13484               token = cp_lexer_peek_token (parser->lexer);
13485               /* What we do depends on what token we have.  */
13486               switch (token->type)
13487                 {
13488                   /* In valid code, a default argument must be
13489                      immediately followed by a `,' `)', or `...'.  */
13490                 case CPP_COMMA:
13491                 case CPP_CLOSE_PAREN:
13492                 case CPP_ELLIPSIS:
13493                   /* If we run into a non-nested `;', `}', or `]',
13494                      then the code is invalid -- but the default
13495                      argument is certainly over.  */
13496                 case CPP_SEMICOLON:
13497                 case CPP_CLOSE_BRACE:
13498                 case CPP_CLOSE_SQUARE:
13499                   if (depth == 0)
13500                     done = true;
13501                   /* Update DEPTH, if necessary.  */
13502                   else if (token->type == CPP_CLOSE_PAREN
13503                            || token->type == CPP_CLOSE_BRACE
13504                            || token->type == CPP_CLOSE_SQUARE)
13505                     --depth;
13506                   break;
13507
13508                 case CPP_OPEN_PAREN:
13509                 case CPP_OPEN_SQUARE:
13510                 case CPP_OPEN_BRACE:
13511                   ++depth;
13512                   break;
13513
13514                 case CPP_RSHIFT:
13515                   if (cxx_dialect == cxx98)
13516                     break;
13517                   /* Fall through for C++0x, which treats the `>>'
13518                      operator like two `>' tokens in certain
13519                      cases.  */
13520
13521                 case CPP_GREATER:
13522                   /* If we see a non-nested `>', and `>' is not an
13523                      operator, then it marks the end of the default
13524                      argument.  */
13525                   if (!depth && !greater_than_is_operator_p)
13526                     done = true;
13527                   break;
13528
13529                   /* If we run out of tokens, issue an error message.  */
13530                 case CPP_EOF:
13531                 case CPP_PRAGMA_EOL:
13532                   error ("file ends in default argument");
13533                   done = true;
13534                   break;
13535
13536                 case CPP_NAME:
13537                 case CPP_SCOPE:
13538                   /* In these cases, we should look for template-ids.
13539                      For example, if the default argument is
13540                      `X<int, double>()', we need to do name lookup to
13541                      figure out whether or not `X' is a template; if
13542                      so, the `,' does not end the default argument.
13543
13544                      That is not yet done.  */
13545                   break;
13546
13547                 default:
13548                   break;
13549                 }
13550
13551               /* If we've reached the end, stop.  */
13552               if (done)
13553                 break;
13554
13555               /* Add the token to the token block.  */
13556               token = cp_lexer_consume_token (parser->lexer);
13557             }
13558
13559           /* Create a DEFAULT_ARG to represented the unparsed default
13560              argument.  */
13561           default_argument = make_node (DEFAULT_ARG);
13562           DEFARG_TOKENS (default_argument)
13563             = cp_token_cache_new (first_token, token);
13564           DEFARG_INSTANTIATIONS (default_argument) = NULL;
13565         }
13566       /* Outside of a class definition, we can just parse the
13567          assignment-expression.  */
13568       else
13569         {
13570           bool saved_local_variables_forbidden_p;
13571
13572           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13573              set correctly.  */
13574           saved_greater_than_is_operator_p
13575             = parser->greater_than_is_operator_p;
13576           parser->greater_than_is_operator_p = greater_than_is_operator_p;
13577           /* Local variable names (and the `this' keyword) may not
13578              appear in a default argument.  */
13579           saved_local_variables_forbidden_p
13580             = parser->local_variables_forbidden_p;
13581           parser->local_variables_forbidden_p = true;
13582           /* The default argument expression may cause implicitly
13583              defined member functions to be synthesized, which will
13584              result in garbage collection.  We must treat this
13585              situation as if we were within the body of function so as
13586              to avoid collecting live data on the stack.  */
13587           ++function_depth;
13588           /* Parse the assignment-expression.  */
13589           if (template_parm_p)
13590             push_deferring_access_checks (dk_no_deferred);
13591           default_argument
13592             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13593           if (template_parm_p)
13594             pop_deferring_access_checks ();
13595           /* Restore saved state.  */
13596           --function_depth;
13597           parser->greater_than_is_operator_p
13598             = saved_greater_than_is_operator_p;
13599           parser->local_variables_forbidden_p
13600             = saved_local_variables_forbidden_p;
13601         }
13602       if (!parser->default_arg_ok_p)
13603         {
13604           if (!flag_pedantic_errors)
13605             warning (0, "deprecated use of default argument for parameter of non-function");
13606           else
13607             {
13608               error ("default arguments are only permitted for function parameters");
13609               default_argument = NULL_TREE;
13610             }
13611         }
13612     }
13613   else
13614     default_argument = NULL_TREE;
13615
13616   return make_parameter_declarator (&decl_specifiers,
13617                                     declarator,
13618                                     default_argument);
13619 }
13620
13621 /* Parse a function-body.
13622
13623    function-body:
13624      compound_statement  */
13625
13626 static void
13627 cp_parser_function_body (cp_parser *parser)
13628 {
13629   cp_parser_compound_statement (parser, NULL, false);
13630 }
13631
13632 /* Parse a ctor-initializer-opt followed by a function-body.  Return
13633    true if a ctor-initializer was present.  */
13634
13635 static bool
13636 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13637 {
13638   tree body;
13639   bool ctor_initializer_p;
13640
13641   /* Begin the function body.  */
13642   body = begin_function_body ();
13643   /* Parse the optional ctor-initializer.  */
13644   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13645   /* Parse the function-body.  */
13646   cp_parser_function_body (parser);
13647   /* Finish the function body.  */
13648   finish_function_body (body);
13649
13650   return ctor_initializer_p;
13651 }
13652
13653 /* Parse an initializer.
13654
13655    initializer:
13656      = initializer-clause
13657      ( expression-list )
13658
13659    Returns an expression representing the initializer.  If no
13660    initializer is present, NULL_TREE is returned.
13661
13662    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13663    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
13664    set to FALSE if there is no initializer present.  If there is an
13665    initializer, and it is not a constant-expression, *NON_CONSTANT_P
13666    is set to true; otherwise it is set to false.  */
13667
13668 static tree
13669 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13670                        bool* non_constant_p)
13671 {
13672   cp_token *token;
13673   tree init;
13674
13675   /* Peek at the next token.  */
13676   token = cp_lexer_peek_token (parser->lexer);
13677
13678   /* Let our caller know whether or not this initializer was
13679      parenthesized.  */
13680   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13681   /* Assume that the initializer is constant.  */
13682   *non_constant_p = false;
13683
13684   if (token->type == CPP_EQ)
13685     {
13686       /* Consume the `='.  */
13687       cp_lexer_consume_token (parser->lexer);
13688       /* Parse the initializer-clause.  */
13689       init = cp_parser_initializer_clause (parser, non_constant_p);
13690     }
13691   else if (token->type == CPP_OPEN_PAREN)
13692     init = cp_parser_parenthesized_expression_list (parser, false,
13693                                                     /*cast_p=*/false,
13694                                                     /*allow_expansion_p=*/true,
13695                                                     non_constant_p);
13696   else
13697     {
13698       /* Anything else is an error.  */
13699       cp_parser_error (parser, "expected initializer");
13700       init = error_mark_node;
13701     }
13702
13703   return init;
13704 }
13705
13706 /* Parse an initializer-clause.
13707
13708    initializer-clause:
13709      assignment-expression
13710      { initializer-list , [opt] }
13711      { }
13712
13713    Returns an expression representing the initializer.
13714
13715    If the `assignment-expression' production is used the value
13716    returned is simply a representation for the expression.
13717
13718    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
13719    the elements of the initializer-list (or NULL, if the last
13720    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
13721    NULL_TREE.  There is no way to detect whether or not the optional
13722    trailing `,' was provided.  NON_CONSTANT_P is as for
13723    cp_parser_initializer.  */
13724
13725 static tree
13726 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13727 {
13728   tree initializer;
13729
13730   /* Assume the expression is constant.  */
13731   *non_constant_p = false;
13732
13733   /* If it is not a `{', then we are looking at an
13734      assignment-expression.  */
13735   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13736     {
13737       initializer
13738         = cp_parser_constant_expression (parser,
13739                                         /*allow_non_constant_p=*/true,
13740                                         non_constant_p);
13741       if (!*non_constant_p)
13742         initializer = fold_non_dependent_expr (initializer);
13743     }
13744   else
13745     {
13746       /* Consume the `{' token.  */
13747       cp_lexer_consume_token (parser->lexer);
13748       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
13749       initializer = make_node (CONSTRUCTOR);
13750       /* If it's not a `}', then there is a non-trivial initializer.  */
13751       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13752         {
13753           /* Parse the initializer list.  */
13754           CONSTRUCTOR_ELTS (initializer)
13755             = cp_parser_initializer_list (parser, non_constant_p);
13756           /* A trailing `,' token is allowed.  */
13757           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13758             cp_lexer_consume_token (parser->lexer);
13759         }
13760       /* Now, there should be a trailing `}'.  */
13761       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13762     }
13763
13764   return initializer;
13765 }
13766
13767 /* Parse an initializer-list.
13768
13769    initializer-list:
13770      initializer-clause ... [opt]
13771      initializer-list , initializer-clause ... [opt]
13772
13773    GNU Extension:
13774
13775    initializer-list:
13776      identifier : initializer-clause
13777      initializer-list, identifier : initializer-clause
13778
13779    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
13780    for the initializer.  If the INDEX of the elt is non-NULL, it is the
13781    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
13782    as for cp_parser_initializer.  */
13783
13784 static VEC(constructor_elt,gc) *
13785 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13786 {
13787   VEC(constructor_elt,gc) *v = NULL;
13788
13789   /* Assume all of the expressions are constant.  */
13790   *non_constant_p = false;
13791
13792   /* Parse the rest of the list.  */
13793   while (true)
13794     {
13795       cp_token *token;
13796       tree identifier;
13797       tree initializer;
13798       bool clause_non_constant_p;
13799
13800       /* If the next token is an identifier and the following one is a
13801          colon, we are looking at the GNU designated-initializer
13802          syntax.  */
13803       if (cp_parser_allow_gnu_extensions_p (parser)
13804           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13805           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13806         {
13807           /* Warn the user that they are using an extension.  */
13808           if (pedantic)
13809             pedwarn ("ISO C++ does not allow designated initializers");
13810           /* Consume the identifier.  */
13811           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13812           /* Consume the `:'.  */
13813           cp_lexer_consume_token (parser->lexer);
13814         }
13815       else
13816         identifier = NULL_TREE;
13817
13818       /* Parse the initializer.  */
13819       initializer = cp_parser_initializer_clause (parser,
13820                                                   &clause_non_constant_p);
13821       /* If any clause is non-constant, so is the entire initializer.  */
13822       if (clause_non_constant_p)
13823         *non_constant_p = true;
13824
13825       /* If we have an ellipsis, this is an initializer pack
13826          expansion.  */
13827       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13828         {
13829           /* Consume the `...'.  */
13830           cp_lexer_consume_token (parser->lexer);
13831
13832           /* Turn the initializer into an initializer expansion.  */
13833           initializer = make_pack_expansion (initializer);
13834         }
13835
13836       /* Add it to the vector.  */
13837       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13838
13839       /* If the next token is not a comma, we have reached the end of
13840          the list.  */
13841       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13842         break;
13843
13844       /* Peek at the next token.  */
13845       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13846       /* If the next token is a `}', then we're still done.  An
13847          initializer-clause can have a trailing `,' after the
13848          initializer-list and before the closing `}'.  */
13849       if (token->type == CPP_CLOSE_BRACE)
13850         break;
13851
13852       /* Consume the `,' token.  */
13853       cp_lexer_consume_token (parser->lexer);
13854     }
13855
13856   return v;
13857 }
13858
13859 /* Classes [gram.class] */
13860
13861 /* Parse a class-name.
13862
13863    class-name:
13864      identifier
13865      template-id
13866
13867    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13868    to indicate that names looked up in dependent types should be
13869    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
13870    keyword has been used to indicate that the name that appears next
13871    is a template.  TAG_TYPE indicates the explicit tag given before
13872    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
13873    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
13874    is the class being defined in a class-head.
13875
13876    Returns the TYPE_DECL representing the class.  */
13877
13878 static tree
13879 cp_parser_class_name (cp_parser *parser,
13880                       bool typename_keyword_p,
13881                       bool template_keyword_p,
13882                       enum tag_types tag_type,
13883                       bool check_dependency_p,
13884                       bool class_head_p,
13885                       bool is_declaration)
13886 {
13887   tree decl;
13888   tree scope;
13889   bool typename_p;
13890   cp_token *token;
13891
13892   /* All class-names start with an identifier.  */
13893   token = cp_lexer_peek_token (parser->lexer);
13894   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13895     {
13896       cp_parser_error (parser, "expected class-name");
13897       return error_mark_node;
13898     }
13899
13900   /* PARSER->SCOPE can be cleared when parsing the template-arguments
13901      to a template-id, so we save it here.  */
13902   scope = parser->scope;
13903   if (scope == error_mark_node)
13904     return error_mark_node;
13905
13906   /* Any name names a type if we're following the `typename' keyword
13907      in a qualified name where the enclosing scope is type-dependent.  */
13908   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13909                 && dependent_type_p (scope));
13910   /* Handle the common case (an identifier, but not a template-id)
13911      efficiently.  */
13912   if (token->type == CPP_NAME
13913       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13914     {
13915       cp_token *identifier_token;
13916       tree identifier;
13917       bool ambiguous_p;
13918
13919       /* Look for the identifier.  */
13920       identifier_token = cp_lexer_peek_token (parser->lexer);
13921       ambiguous_p = identifier_token->ambiguous_p;
13922       identifier = cp_parser_identifier (parser);
13923       /* If the next token isn't an identifier, we are certainly not
13924          looking at a class-name.  */
13925       if (identifier == error_mark_node)
13926         decl = error_mark_node;
13927       /* If we know this is a type-name, there's no need to look it
13928          up.  */
13929       else if (typename_p)
13930         decl = identifier;
13931       else
13932         {
13933           tree ambiguous_decls;
13934           /* If we already know that this lookup is ambiguous, then
13935              we've already issued an error message; there's no reason
13936              to check again.  */
13937           if (ambiguous_p)
13938             {
13939               cp_parser_simulate_error (parser);
13940               return error_mark_node;
13941             }
13942           /* If the next token is a `::', then the name must be a type
13943              name.
13944
13945              [basic.lookup.qual]
13946
13947              During the lookup for a name preceding the :: scope
13948              resolution operator, object, function, and enumerator
13949              names are ignored.  */
13950           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13951             tag_type = typename_type;
13952           /* Look up the name.  */
13953           decl = cp_parser_lookup_name (parser, identifier,
13954                                         tag_type,
13955                                         /*is_template=*/false,
13956                                         /*is_namespace=*/false,
13957                                         check_dependency_p,
13958                                         &ambiguous_decls);
13959           if (ambiguous_decls)
13960             {
13961               error ("reference to %qD is ambiguous", identifier);
13962               print_candidates (ambiguous_decls);
13963               if (cp_parser_parsing_tentatively (parser))
13964                 {
13965                   identifier_token->ambiguous_p = true;
13966                   cp_parser_simulate_error (parser);
13967                 }
13968               return error_mark_node;
13969             }
13970         }
13971     }
13972   else
13973     {
13974       /* Try a template-id.  */
13975       decl = cp_parser_template_id (parser, template_keyword_p,
13976                                     check_dependency_p,
13977                                     is_declaration);
13978       if (decl == error_mark_node)
13979         return error_mark_node;
13980     }
13981
13982   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13983
13984   /* If this is a typename, create a TYPENAME_TYPE.  */
13985   if (typename_p && decl != error_mark_node)
13986     {
13987       decl = make_typename_type (scope, decl, typename_type,
13988                                  /*complain=*/tf_error);
13989       if (decl != error_mark_node)
13990         decl = TYPE_NAME (decl);
13991     }
13992
13993   /* Check to see that it is really the name of a class.  */
13994   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13995       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13996       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13997     /* Situations like this:
13998
13999          template <typename T> struct A {
14000            typename T::template X<int>::I i;
14001          };
14002
14003        are problematic.  Is `T::template X<int>' a class-name?  The
14004        standard does not seem to be definitive, but there is no other
14005        valid interpretation of the following `::'.  Therefore, those
14006        names are considered class-names.  */
14007     {
14008       decl = make_typename_type (scope, decl, tag_type, tf_error);
14009       if (decl != error_mark_node)
14010         decl = TYPE_NAME (decl);
14011     }
14012   else if (TREE_CODE (decl) != TYPE_DECL
14013            || TREE_TYPE (decl) == error_mark_node
14014            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14015     decl = error_mark_node;
14016
14017   if (decl == error_mark_node)
14018     cp_parser_error (parser, "expected class-name");
14019
14020   return decl;
14021 }
14022
14023 /* Parse a class-specifier.
14024
14025    class-specifier:
14026      class-head { member-specification [opt] }
14027
14028    Returns the TREE_TYPE representing the class.  */
14029
14030 static tree
14031 cp_parser_class_specifier (cp_parser* parser)
14032 {
14033   cp_token *token;
14034   tree type;
14035   tree attributes = NULL_TREE;
14036   int has_trailing_semicolon;
14037   bool nested_name_specifier_p;
14038   unsigned saved_num_template_parameter_lists;
14039   bool saved_in_function_body;
14040   tree old_scope = NULL_TREE;
14041   tree scope = NULL_TREE;
14042   tree bases;
14043
14044   push_deferring_access_checks (dk_no_deferred);
14045
14046   /* Parse the class-head.  */
14047   type = cp_parser_class_head (parser,
14048                                &nested_name_specifier_p,
14049                                &attributes,
14050                                &bases);
14051   /* If the class-head was a semantic disaster, skip the entire body
14052      of the class.  */
14053   if (!type)
14054     {
14055       cp_parser_skip_to_end_of_block_or_statement (parser);
14056       pop_deferring_access_checks ();
14057       return error_mark_node;
14058     }
14059
14060   /* Look for the `{'.  */
14061   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14062     {
14063       pop_deferring_access_checks ();
14064       return error_mark_node;
14065     }
14066
14067   /* Process the base classes. If they're invalid, skip the 
14068      entire class body.  */
14069   if (!xref_basetypes (type, bases))
14070     {
14071       /* Consuming the closing brace yields better error messages
14072          later on.  */
14073       if (cp_parser_skip_to_closing_brace (parser))
14074         cp_lexer_consume_token (parser->lexer);
14075       pop_deferring_access_checks ();
14076       return error_mark_node;
14077     }
14078
14079   /* Issue an error message if type-definitions are forbidden here.  */
14080   cp_parser_check_type_definition (parser);
14081   /* Remember that we are defining one more class.  */
14082   ++parser->num_classes_being_defined;
14083   /* Inside the class, surrounding template-parameter-lists do not
14084      apply.  */
14085   saved_num_template_parameter_lists
14086     = parser->num_template_parameter_lists;
14087   parser->num_template_parameter_lists = 0;
14088   /* We are not in a function body.  */
14089   saved_in_function_body = parser->in_function_body;
14090   parser->in_function_body = false;
14091
14092   /* Start the class.  */
14093   if (nested_name_specifier_p)
14094     {
14095       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14096       old_scope = push_inner_scope (scope);
14097     }
14098   type = begin_class_definition (type, attributes);
14099
14100   if (type == error_mark_node)
14101     /* If the type is erroneous, skip the entire body of the class.  */
14102     cp_parser_skip_to_closing_brace (parser);
14103   else
14104     /* Parse the member-specification.  */
14105     cp_parser_member_specification_opt (parser);
14106
14107   /* Look for the trailing `}'.  */
14108   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14109   /* We get better error messages by noticing a common problem: a
14110      missing trailing `;'.  */
14111   token = cp_lexer_peek_token (parser->lexer);
14112   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14113   /* Look for trailing attributes to apply to this class.  */
14114   if (cp_parser_allow_gnu_extensions_p (parser))
14115     attributes = cp_parser_attributes_opt (parser);
14116   if (type != error_mark_node)
14117     type = finish_struct (type, attributes);
14118   if (nested_name_specifier_p)
14119     pop_inner_scope (old_scope, scope);
14120   /* If this class is not itself within the scope of another class,
14121      then we need to parse the bodies of all of the queued function
14122      definitions.  Note that the queued functions defined in a class
14123      are not always processed immediately following the
14124      class-specifier for that class.  Consider:
14125
14126        struct A {
14127          struct B { void f() { sizeof (A); } };
14128        };
14129
14130      If `f' were processed before the processing of `A' were
14131      completed, there would be no way to compute the size of `A'.
14132      Note that the nesting we are interested in here is lexical --
14133      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14134      for:
14135
14136        struct A { struct B; };
14137        struct A::B { void f() { } };
14138
14139      there is no need to delay the parsing of `A::B::f'.  */
14140   if (--parser->num_classes_being_defined == 0)
14141     {
14142       tree queue_entry;
14143       tree fn;
14144       tree class_type = NULL_TREE;
14145       tree pushed_scope = NULL_TREE;
14146
14147       /* In a first pass, parse default arguments to the functions.
14148          Then, in a second pass, parse the bodies of the functions.
14149          This two-phased approach handles cases like:
14150
14151             struct S {
14152               void f() { g(); }
14153               void g(int i = 3);
14154             };
14155
14156          */
14157       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14158              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14159            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14160            TREE_PURPOSE (parser->unparsed_functions_queues)
14161              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14162         {
14163           fn = TREE_VALUE (queue_entry);
14164           /* If there are default arguments that have not yet been processed,
14165              take care of them now.  */
14166           if (class_type != TREE_PURPOSE (queue_entry))
14167             {
14168               if (pushed_scope)
14169                 pop_scope (pushed_scope);
14170               class_type = TREE_PURPOSE (queue_entry);
14171               pushed_scope = push_scope (class_type);
14172             }
14173           /* Make sure that any template parameters are in scope.  */
14174           maybe_begin_member_template_processing (fn);
14175           /* Parse the default argument expressions.  */
14176           cp_parser_late_parsing_default_args (parser, fn);
14177           /* Remove any template parameters from the symbol table.  */
14178           maybe_end_member_template_processing ();
14179         }
14180       if (pushed_scope)
14181         pop_scope (pushed_scope);
14182       /* Now parse the body of the functions.  */
14183       for (TREE_VALUE (parser->unparsed_functions_queues)
14184              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14185            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14186            TREE_VALUE (parser->unparsed_functions_queues)
14187              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14188         {
14189           /* Figure out which function we need to process.  */
14190           fn = TREE_VALUE (queue_entry);
14191           /* Parse the function.  */
14192           cp_parser_late_parsing_for_member (parser, fn);
14193         }
14194     }
14195
14196   /* Put back any saved access checks.  */
14197   pop_deferring_access_checks ();
14198
14199   /* Restore saved state.  */
14200   parser->in_function_body = saved_in_function_body;
14201   parser->num_template_parameter_lists
14202     = saved_num_template_parameter_lists;
14203
14204   return type;
14205 }
14206
14207 /* Parse a class-head.
14208
14209    class-head:
14210      class-key identifier [opt] base-clause [opt]
14211      class-key nested-name-specifier identifier base-clause [opt]
14212      class-key nested-name-specifier [opt] template-id
14213        base-clause [opt]
14214
14215    GNU Extensions:
14216      class-key attributes identifier [opt] base-clause [opt]
14217      class-key attributes nested-name-specifier identifier base-clause [opt]
14218      class-key attributes nested-name-specifier [opt] template-id
14219        base-clause [opt]
14220
14221    Upon return BASES is initialized to the list of base classes (or
14222    NULL, if there are none) in the same form returned by
14223    cp_parser_base_clause.
14224
14225    Returns the TYPE of the indicated class.  Sets
14226    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14227    involving a nested-name-specifier was used, and FALSE otherwise.
14228
14229    Returns error_mark_node if this is not a class-head.
14230
14231    Returns NULL_TREE if the class-head is syntactically valid, but
14232    semantically invalid in a way that means we should skip the entire
14233    body of the class.  */
14234
14235 static tree
14236 cp_parser_class_head (cp_parser* parser,
14237                       bool* nested_name_specifier_p,
14238                       tree *attributes_p,
14239                       tree *bases)
14240 {
14241   tree nested_name_specifier;
14242   enum tag_types class_key;
14243   tree id = NULL_TREE;
14244   tree type = NULL_TREE;
14245   tree attributes;
14246   bool template_id_p = false;
14247   bool qualified_p = false;
14248   bool invalid_nested_name_p = false;
14249   bool invalid_explicit_specialization_p = false;
14250   tree pushed_scope = NULL_TREE;
14251   unsigned num_templates;
14252
14253   /* Assume no nested-name-specifier will be present.  */
14254   *nested_name_specifier_p = false;
14255   /* Assume no template parameter lists will be used in defining the
14256      type.  */
14257   num_templates = 0;
14258
14259   *bases = NULL_TREE;
14260
14261   /* Look for the class-key.  */
14262   class_key = cp_parser_class_key (parser);
14263   if (class_key == none_type)
14264     return error_mark_node;
14265
14266   /* Parse the attributes.  */
14267   attributes = cp_parser_attributes_opt (parser);
14268
14269   /* If the next token is `::', that is invalid -- but sometimes
14270      people do try to write:
14271
14272        struct ::S {};
14273
14274      Handle this gracefully by accepting the extra qualifier, and then
14275      issuing an error about it later if this really is a
14276      class-head.  If it turns out just to be an elaborated type
14277      specifier, remain silent.  */
14278   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14279     qualified_p = true;
14280
14281   push_deferring_access_checks (dk_no_check);
14282
14283   /* Determine the name of the class.  Begin by looking for an
14284      optional nested-name-specifier.  */
14285   nested_name_specifier
14286     = cp_parser_nested_name_specifier_opt (parser,
14287                                            /*typename_keyword_p=*/false,
14288                                            /*check_dependency_p=*/false,
14289                                            /*type_p=*/false,
14290                                            /*is_declaration=*/false);
14291   /* If there was a nested-name-specifier, then there *must* be an
14292      identifier.  */
14293   if (nested_name_specifier)
14294     {
14295       /* Although the grammar says `identifier', it really means
14296          `class-name' or `template-name'.  You are only allowed to
14297          define a class that has already been declared with this
14298          syntax.
14299
14300          The proposed resolution for Core Issue 180 says that wherever
14301          you see `class T::X' you should treat `X' as a type-name.
14302
14303          It is OK to define an inaccessible class; for example:
14304
14305            class A { class B; };
14306            class A::B {};
14307
14308          We do not know if we will see a class-name, or a
14309          template-name.  We look for a class-name first, in case the
14310          class-name is a template-id; if we looked for the
14311          template-name first we would stop after the template-name.  */
14312       cp_parser_parse_tentatively (parser);
14313       type = cp_parser_class_name (parser,
14314                                    /*typename_keyword_p=*/false,
14315                                    /*template_keyword_p=*/false,
14316                                    class_type,
14317                                    /*check_dependency_p=*/false,
14318                                    /*class_head_p=*/true,
14319                                    /*is_declaration=*/false);
14320       /* If that didn't work, ignore the nested-name-specifier.  */
14321       if (!cp_parser_parse_definitely (parser))
14322         {
14323           invalid_nested_name_p = true;
14324           id = cp_parser_identifier (parser);
14325           if (id == error_mark_node)
14326             id = NULL_TREE;
14327         }
14328       /* If we could not find a corresponding TYPE, treat this
14329          declaration like an unqualified declaration.  */
14330       if (type == error_mark_node)
14331         nested_name_specifier = NULL_TREE;
14332       /* Otherwise, count the number of templates used in TYPE and its
14333          containing scopes.  */
14334       else
14335         {
14336           tree scope;
14337
14338           for (scope = TREE_TYPE (type);
14339                scope && TREE_CODE (scope) != NAMESPACE_DECL;
14340                scope = (TYPE_P (scope)
14341                         ? TYPE_CONTEXT (scope)
14342                         : DECL_CONTEXT (scope)))
14343             if (TYPE_P (scope)
14344                 && CLASS_TYPE_P (scope)
14345                 && CLASSTYPE_TEMPLATE_INFO (scope)
14346                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14347                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14348               ++num_templates;
14349         }
14350     }
14351   /* Otherwise, the identifier is optional.  */
14352   else
14353     {
14354       /* We don't know whether what comes next is a template-id,
14355          an identifier, or nothing at all.  */
14356       cp_parser_parse_tentatively (parser);
14357       /* Check for a template-id.  */
14358       id = cp_parser_template_id (parser,
14359                                   /*template_keyword_p=*/false,
14360                                   /*check_dependency_p=*/true,
14361                                   /*is_declaration=*/true);
14362       /* If that didn't work, it could still be an identifier.  */
14363       if (!cp_parser_parse_definitely (parser))
14364         {
14365           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14366             id = cp_parser_identifier (parser);
14367           else
14368             id = NULL_TREE;
14369         }
14370       else
14371         {
14372           template_id_p = true;
14373           ++num_templates;
14374         }
14375     }
14376
14377   pop_deferring_access_checks ();
14378
14379   if (id)
14380     cp_parser_check_for_invalid_template_id (parser, id);
14381
14382   /* If it's not a `:' or a `{' then we can't really be looking at a
14383      class-head, since a class-head only appears as part of a
14384      class-specifier.  We have to detect this situation before calling
14385      xref_tag, since that has irreversible side-effects.  */
14386   if (!cp_parser_next_token_starts_class_definition_p (parser))
14387     {
14388       cp_parser_error (parser, "expected %<{%> or %<:%>");
14389       return error_mark_node;
14390     }
14391
14392   /* At this point, we're going ahead with the class-specifier, even
14393      if some other problem occurs.  */
14394   cp_parser_commit_to_tentative_parse (parser);
14395   /* Issue the error about the overly-qualified name now.  */
14396   if (qualified_p)
14397     cp_parser_error (parser,
14398                      "global qualification of class name is invalid");
14399   else if (invalid_nested_name_p)
14400     cp_parser_error (parser,
14401                      "qualified name does not name a class");
14402   else if (nested_name_specifier)
14403     {
14404       tree scope;
14405
14406       /* Reject typedef-names in class heads.  */
14407       if (!DECL_IMPLICIT_TYPEDEF_P (type))
14408         {
14409           error ("invalid class name in declaration of %qD", type);
14410           type = NULL_TREE;
14411           goto done;
14412         }
14413
14414       /* Figure out in what scope the declaration is being placed.  */
14415       scope = current_scope ();
14416       /* If that scope does not contain the scope in which the
14417          class was originally declared, the program is invalid.  */
14418       if (scope && !is_ancestor (scope, nested_name_specifier))
14419         {
14420           if (at_namespace_scope_p ())
14421             error ("declaration of %qD in namespace %qD which does not "
14422                    "enclose %qD", type, scope, nested_name_specifier);
14423           else
14424             error ("declaration of %qD in %qD which does not enclose %qD",
14425                    type, scope, nested_name_specifier);
14426           type = NULL_TREE;
14427           goto done;
14428         }
14429       /* [dcl.meaning]
14430
14431          A declarator-id shall not be qualified exception of the
14432          definition of a ... nested class outside of its class
14433          ... [or] a the definition or explicit instantiation of a
14434          class member of a namespace outside of its namespace.  */
14435       if (scope == nested_name_specifier)
14436         {
14437           pedwarn ("extra qualification ignored");
14438           nested_name_specifier = NULL_TREE;
14439           num_templates = 0;
14440         }
14441     }
14442   /* An explicit-specialization must be preceded by "template <>".  If
14443      it is not, try to recover gracefully.  */
14444   if (at_namespace_scope_p ()
14445       && parser->num_template_parameter_lists == 0
14446       && template_id_p)
14447     {
14448       error ("an explicit specialization must be preceded by %<template <>%>");
14449       invalid_explicit_specialization_p = true;
14450       /* Take the same action that would have been taken by
14451          cp_parser_explicit_specialization.  */
14452       ++parser->num_template_parameter_lists;
14453       begin_specialization ();
14454     }
14455   /* There must be no "return" statements between this point and the
14456      end of this function; set "type "to the correct return value and
14457      use "goto done;" to return.  */
14458   /* Make sure that the right number of template parameters were
14459      present.  */
14460   if (!cp_parser_check_template_parameters (parser, num_templates))
14461     {
14462       /* If something went wrong, there is no point in even trying to
14463          process the class-definition.  */
14464       type = NULL_TREE;
14465       goto done;
14466     }
14467
14468   /* Look up the type.  */
14469   if (template_id_p)
14470     {
14471       type = TREE_TYPE (id);
14472       type = maybe_process_partial_specialization (type);
14473       if (nested_name_specifier)
14474         pushed_scope = push_scope (nested_name_specifier);
14475     }
14476   else if (nested_name_specifier)
14477     {
14478       tree class_type;
14479
14480       /* Given:
14481
14482             template <typename T> struct S { struct T };
14483             template <typename T> struct S<T>::T { };
14484
14485          we will get a TYPENAME_TYPE when processing the definition of
14486          `S::T'.  We need to resolve it to the actual type before we
14487          try to define it.  */
14488       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14489         {
14490           class_type = resolve_typename_type (TREE_TYPE (type),
14491                                               /*only_current_p=*/false);
14492           if (TREE_CODE (class_type) != TYPENAME_TYPE)
14493             type = TYPE_NAME (class_type);
14494           else
14495             {
14496               cp_parser_error (parser, "could not resolve typename type");
14497               type = error_mark_node;
14498             }
14499         }
14500
14501       maybe_process_partial_specialization (TREE_TYPE (type));
14502       class_type = current_class_type;
14503       /* Enter the scope indicated by the nested-name-specifier.  */
14504       pushed_scope = push_scope (nested_name_specifier);
14505       /* Get the canonical version of this type.  */
14506       type = TYPE_MAIN_DECL (TREE_TYPE (type));
14507       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14508           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14509         {
14510           type = push_template_decl (type);
14511           if (type == error_mark_node)
14512             {
14513               type = NULL_TREE;
14514               goto done;
14515             }
14516         }
14517
14518       type = TREE_TYPE (type);
14519       *nested_name_specifier_p = true;
14520     }
14521   else      /* The name is not a nested name.  */
14522     {
14523       /* If the class was unnamed, create a dummy name.  */
14524       if (!id)
14525         id = make_anon_name ();
14526       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14527                        parser->num_template_parameter_lists);
14528     }
14529
14530   /* Indicate whether this class was declared as a `class' or as a
14531      `struct'.  */
14532   if (TREE_CODE (type) == RECORD_TYPE)
14533     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14534   cp_parser_check_class_key (class_key, type);
14535
14536   /* If this type was already complete, and we see another definition,
14537      that's an error.  */
14538   if (type != error_mark_node && COMPLETE_TYPE_P (type))
14539     {
14540       error ("redefinition of %q#T", type);
14541       error ("previous definition of %q+#T", type);
14542       type = NULL_TREE;
14543       goto done;
14544     }
14545   else if (type == error_mark_node)
14546     type = NULL_TREE;
14547
14548   /* We will have entered the scope containing the class; the names of
14549      base classes should be looked up in that context.  For example:
14550
14551        struct A { struct B {}; struct C; };
14552        struct A::C : B {};
14553
14554      is valid.  */
14555
14556   /* Get the list of base-classes, if there is one.  */
14557   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14558     *bases = cp_parser_base_clause (parser);
14559
14560  done:
14561   /* Leave the scope given by the nested-name-specifier.  We will
14562      enter the class scope itself while processing the members.  */
14563   if (pushed_scope)
14564     pop_scope (pushed_scope);
14565
14566   if (invalid_explicit_specialization_p)
14567     {
14568       end_specialization ();
14569       --parser->num_template_parameter_lists;
14570     }
14571   *attributes_p = attributes;
14572   return type;
14573 }
14574
14575 /* Parse a class-key.
14576
14577    class-key:
14578      class
14579      struct
14580      union
14581
14582    Returns the kind of class-key specified, or none_type to indicate
14583    error.  */
14584
14585 static enum tag_types
14586 cp_parser_class_key (cp_parser* parser)
14587 {
14588   cp_token *token;
14589   enum tag_types tag_type;
14590
14591   /* Look for the class-key.  */
14592   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14593   if (!token)
14594     return none_type;
14595
14596   /* Check to see if the TOKEN is a class-key.  */
14597   tag_type = cp_parser_token_is_class_key (token);
14598   if (!tag_type)
14599     cp_parser_error (parser, "expected class-key");
14600   return tag_type;
14601 }
14602
14603 /* Parse an (optional) member-specification.
14604
14605    member-specification:
14606      member-declaration member-specification [opt]
14607      access-specifier : member-specification [opt]  */
14608
14609 static void
14610 cp_parser_member_specification_opt (cp_parser* parser)
14611 {
14612   while (true)
14613     {
14614       cp_token *token;
14615       enum rid keyword;
14616
14617       /* Peek at the next token.  */
14618       token = cp_lexer_peek_token (parser->lexer);
14619       /* If it's a `}', or EOF then we've seen all the members.  */
14620       if (token->type == CPP_CLOSE_BRACE
14621           || token->type == CPP_EOF
14622           || token->type == CPP_PRAGMA_EOL)
14623         break;
14624
14625       /* See if this token is a keyword.  */
14626       keyword = token->keyword;
14627       switch (keyword)
14628         {
14629         case RID_PUBLIC:
14630         case RID_PROTECTED:
14631         case RID_PRIVATE:
14632           /* Consume the access-specifier.  */
14633           cp_lexer_consume_token (parser->lexer);
14634           /* Remember which access-specifier is active.  */
14635           current_access_specifier = token->u.value;
14636           /* Look for the `:'.  */
14637           cp_parser_require (parser, CPP_COLON, "`:'");
14638           break;
14639
14640         default:
14641           /* Accept #pragmas at class scope.  */
14642           if (token->type == CPP_PRAGMA)
14643             {
14644               cp_parser_pragma (parser, pragma_external);
14645               break;
14646             }
14647
14648           /* Otherwise, the next construction must be a
14649              member-declaration.  */
14650           cp_parser_member_declaration (parser);
14651         }
14652     }
14653 }
14654
14655 /* Parse a member-declaration.
14656
14657    member-declaration:
14658      decl-specifier-seq [opt] member-declarator-list [opt] ;
14659      function-definition ; [opt]
14660      :: [opt] nested-name-specifier template [opt] unqualified-id ;
14661      using-declaration
14662      template-declaration
14663
14664    member-declarator-list:
14665      member-declarator
14666      member-declarator-list , member-declarator
14667
14668    member-declarator:
14669      declarator pure-specifier [opt]
14670      declarator constant-initializer [opt]
14671      identifier [opt] : constant-expression
14672
14673    GNU Extensions:
14674
14675    member-declaration:
14676      __extension__ member-declaration
14677
14678    member-declarator:
14679      declarator attributes [opt] pure-specifier [opt]
14680      declarator attributes [opt] constant-initializer [opt]
14681      identifier [opt] attributes [opt] : constant-expression  
14682
14683    C++0x Extensions:
14684
14685    member-declaration:
14686      static_assert-declaration  */
14687
14688 static void
14689 cp_parser_member_declaration (cp_parser* parser)
14690 {
14691   cp_decl_specifier_seq decl_specifiers;
14692   tree prefix_attributes;
14693   tree decl;
14694   int declares_class_or_enum;
14695   bool friend_p;
14696   cp_token *token;
14697   int saved_pedantic;
14698
14699   /* Check for the `__extension__' keyword.  */
14700   if (cp_parser_extension_opt (parser, &saved_pedantic))
14701     {
14702       /* Recurse.  */
14703       cp_parser_member_declaration (parser);
14704       /* Restore the old value of the PEDANTIC flag.  */
14705       pedantic = saved_pedantic;
14706
14707       return;
14708     }
14709
14710   /* Check for a template-declaration.  */
14711   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14712     {
14713       /* An explicit specialization here is an error condition, and we
14714          expect the specialization handler to detect and report this.  */
14715       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14716           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14717         cp_parser_explicit_specialization (parser);
14718       else
14719         cp_parser_template_declaration (parser, /*member_p=*/true);
14720
14721       return;
14722     }
14723
14724   /* Check for a using-declaration.  */
14725   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14726     {
14727       /* Parse the using-declaration.  */
14728       cp_parser_using_declaration (parser,
14729                                    /*access_declaration_p=*/false);
14730       return;
14731     }
14732
14733   /* Check for @defs.  */
14734   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14735     {
14736       tree ivar, member;
14737       tree ivar_chains = cp_parser_objc_defs_expression (parser);
14738       ivar = ivar_chains;
14739       while (ivar)
14740         {
14741           member = ivar;
14742           ivar = TREE_CHAIN (member);
14743           TREE_CHAIN (member) = NULL_TREE;
14744           finish_member_declaration (member);
14745         }
14746       return;
14747     }
14748
14749   /* If the next token is `static_assert' we have a static assertion.  */
14750   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14751     {
14752       cp_parser_static_assert (parser, /*member_p=*/true);
14753       return;
14754     }
14755
14756   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14757     return;
14758
14759   /* Parse the decl-specifier-seq.  */
14760   cp_parser_decl_specifier_seq (parser,
14761                                 CP_PARSER_FLAGS_OPTIONAL,
14762                                 &decl_specifiers,
14763                                 &declares_class_or_enum);
14764   prefix_attributes = decl_specifiers.attributes;
14765   decl_specifiers.attributes = NULL_TREE;
14766   /* Check for an invalid type-name.  */
14767   if (!decl_specifiers.type
14768       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14769     return;
14770   /* If there is no declarator, then the decl-specifier-seq should
14771      specify a type.  */
14772   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14773     {
14774       /* If there was no decl-specifier-seq, and the next token is a
14775          `;', then we have something like:
14776
14777            struct S { ; };
14778
14779          [class.mem]
14780
14781          Each member-declaration shall declare at least one member
14782          name of the class.  */
14783       if (!decl_specifiers.any_specifiers_p)
14784         {
14785           cp_token *token = cp_lexer_peek_token (parser->lexer);
14786           if (pedantic && !token->in_system_header)
14787             pedwarn ("%Hextra %<;%>", &token->location);
14788         }
14789       else
14790         {
14791           tree type;
14792
14793           /* See if this declaration is a friend.  */
14794           friend_p = cp_parser_friend_p (&decl_specifiers);
14795           /* If there were decl-specifiers, check to see if there was
14796              a class-declaration.  */
14797           type = check_tag_decl (&decl_specifiers);
14798           /* Nested classes have already been added to the class, but
14799              a `friend' needs to be explicitly registered.  */
14800           if (friend_p)
14801             {
14802               /* If the `friend' keyword was present, the friend must
14803                  be introduced with a class-key.  */
14804                if (!declares_class_or_enum)
14805                  error ("a class-key must be used when declaring a friend");
14806                /* In this case:
14807
14808                     template <typename T> struct A {
14809                       friend struct A<T>::B;
14810                     };
14811
14812                   A<T>::B will be represented by a TYPENAME_TYPE, and
14813                   therefore not recognized by check_tag_decl.  */
14814                if (!type
14815                    && decl_specifiers.type
14816                    && TYPE_P (decl_specifiers.type))
14817                  type = decl_specifiers.type;
14818                if (!type || !TYPE_P (type))
14819                  error ("friend declaration does not name a class or "
14820                         "function");
14821                else
14822                  make_friend_class (current_class_type, type,
14823                                     /*complain=*/true);
14824             }
14825           /* If there is no TYPE, an error message will already have
14826              been issued.  */
14827           else if (!type || type == error_mark_node)
14828             ;
14829           /* An anonymous aggregate has to be handled specially; such
14830              a declaration really declares a data member (with a
14831              particular type), as opposed to a nested class.  */
14832           else if (ANON_AGGR_TYPE_P (type))
14833             {
14834               /* Remove constructors and such from TYPE, now that we
14835                  know it is an anonymous aggregate.  */
14836               fixup_anonymous_aggr (type);
14837               /* And make the corresponding data member.  */
14838               decl = build_decl (FIELD_DECL, NULL_TREE, type);
14839               /* Add it to the class.  */
14840               finish_member_declaration (decl);
14841             }
14842           else
14843             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14844         }
14845     }
14846   else
14847     {
14848       /* See if these declarations will be friends.  */
14849       friend_p = cp_parser_friend_p (&decl_specifiers);
14850
14851       /* Keep going until we hit the `;' at the end of the
14852          declaration.  */
14853       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14854         {
14855           tree attributes = NULL_TREE;
14856           tree first_attribute;
14857
14858           /* Peek at the next token.  */
14859           token = cp_lexer_peek_token (parser->lexer);
14860
14861           /* Check for a bitfield declaration.  */
14862           if (token->type == CPP_COLON
14863               || (token->type == CPP_NAME
14864                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14865                   == CPP_COLON))
14866             {
14867               tree identifier;
14868               tree width;
14869
14870               /* Get the name of the bitfield.  Note that we cannot just
14871                  check TOKEN here because it may have been invalidated by
14872                  the call to cp_lexer_peek_nth_token above.  */
14873               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14874                 identifier = cp_parser_identifier (parser);
14875               else
14876                 identifier = NULL_TREE;
14877
14878               /* Consume the `:' token.  */
14879               cp_lexer_consume_token (parser->lexer);
14880               /* Get the width of the bitfield.  */
14881               width
14882                 = cp_parser_constant_expression (parser,
14883                                                  /*allow_non_constant=*/false,
14884                                                  NULL);
14885
14886               /* Look for attributes that apply to the bitfield.  */
14887               attributes = cp_parser_attributes_opt (parser);
14888               /* Remember which attributes are prefix attributes and
14889                  which are not.  */
14890               first_attribute = attributes;
14891               /* Combine the attributes.  */
14892               attributes = chainon (prefix_attributes, attributes);
14893
14894               /* Create the bitfield declaration.  */
14895               decl = grokbitfield (identifier
14896                                    ? make_id_declarator (NULL_TREE,
14897                                                          identifier,
14898                                                          sfk_none)
14899                                    : NULL,
14900                                    &decl_specifiers,
14901                                    width);
14902               /* Apply the attributes.  */
14903               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14904             }
14905           else
14906             {
14907               cp_declarator *declarator;
14908               tree initializer;
14909               tree asm_specification;
14910               int ctor_dtor_or_conv_p;
14911
14912               /* Parse the declarator.  */
14913               declarator
14914                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14915                                         &ctor_dtor_or_conv_p,
14916                                         /*parenthesized_p=*/NULL,
14917                                         /*member_p=*/true);
14918
14919               /* If something went wrong parsing the declarator, make sure
14920                  that we at least consume some tokens.  */
14921               if (declarator == cp_error_declarator)
14922                 {
14923                   /* Skip to the end of the statement.  */
14924                   cp_parser_skip_to_end_of_statement (parser);
14925                   /* If the next token is not a semicolon, that is
14926                      probably because we just skipped over the body of
14927                      a function.  So, we consume a semicolon if
14928                      present, but do not issue an error message if it
14929                      is not present.  */
14930                   if (cp_lexer_next_token_is (parser->lexer,
14931                                               CPP_SEMICOLON))
14932                     cp_lexer_consume_token (parser->lexer);
14933                   return;
14934                 }
14935
14936               if (declares_class_or_enum & 2)
14937                 cp_parser_check_for_definition_in_return_type
14938                   (declarator, decl_specifiers.type);
14939
14940               /* Look for an asm-specification.  */
14941               asm_specification = cp_parser_asm_specification_opt (parser);
14942               /* Look for attributes that apply to the declaration.  */
14943               attributes = cp_parser_attributes_opt (parser);
14944               /* Remember which attributes are prefix attributes and
14945                  which are not.  */
14946               first_attribute = attributes;
14947               /* Combine the attributes.  */
14948               attributes = chainon (prefix_attributes, attributes);
14949
14950               /* If it's an `=', then we have a constant-initializer or a
14951                  pure-specifier.  It is not correct to parse the
14952                  initializer before registering the member declaration
14953                  since the member declaration should be in scope while
14954                  its initializer is processed.  However, the rest of the
14955                  front end does not yet provide an interface that allows
14956                  us to handle this correctly.  */
14957               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14958                 {
14959                   /* In [class.mem]:
14960
14961                      A pure-specifier shall be used only in the declaration of
14962                      a virtual function.
14963
14964                      A member-declarator can contain a constant-initializer
14965                      only if it declares a static member of integral or
14966                      enumeration type.
14967
14968                      Therefore, if the DECLARATOR is for a function, we look
14969                      for a pure-specifier; otherwise, we look for a
14970                      constant-initializer.  When we call `grokfield', it will
14971                      perform more stringent semantics checks.  */
14972                   if (function_declarator_p (declarator))
14973                     initializer = cp_parser_pure_specifier (parser);
14974                   else
14975                     /* Parse the initializer.  */
14976                     initializer = cp_parser_constant_initializer (parser);
14977                 }
14978               /* Otherwise, there is no initializer.  */
14979               else
14980                 initializer = NULL_TREE;
14981
14982               /* See if we are probably looking at a function
14983                  definition.  We are certainly not looking at a
14984                  member-declarator.  Calling `grokfield' has
14985                  side-effects, so we must not do it unless we are sure
14986                  that we are looking at a member-declarator.  */
14987               if (cp_parser_token_starts_function_definition_p
14988                   (cp_lexer_peek_token (parser->lexer)))
14989                 {
14990                   /* The grammar does not allow a pure-specifier to be
14991                      used when a member function is defined.  (It is
14992                      possible that this fact is an oversight in the
14993                      standard, since a pure function may be defined
14994                      outside of the class-specifier.  */
14995                   if (initializer)
14996                     error ("pure-specifier on function-definition");
14997                   decl = cp_parser_save_member_function_body (parser,
14998                                                               &decl_specifiers,
14999                                                               declarator,
15000                                                               attributes);
15001                   /* If the member was not a friend, declare it here.  */
15002                   if (!friend_p)
15003                     finish_member_declaration (decl);
15004                   /* Peek at the next token.  */
15005                   token = cp_lexer_peek_token (parser->lexer);
15006                   /* If the next token is a semicolon, consume it.  */
15007                   if (token->type == CPP_SEMICOLON)
15008                     cp_lexer_consume_token (parser->lexer);
15009                   return;
15010                 }
15011               else
15012                 /* Create the declaration.  */
15013                 decl = grokfield (declarator, &decl_specifiers,
15014                                   initializer, /*init_const_expr_p=*/true,
15015                                   asm_specification,
15016                                   attributes);
15017             }
15018
15019           /* Reset PREFIX_ATTRIBUTES.  */
15020           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15021             attributes = TREE_CHAIN (attributes);
15022           if (attributes)
15023             TREE_CHAIN (attributes) = NULL_TREE;
15024
15025           /* If there is any qualification still in effect, clear it
15026              now; we will be starting fresh with the next declarator.  */
15027           parser->scope = NULL_TREE;
15028           parser->qualifying_scope = NULL_TREE;
15029           parser->object_scope = NULL_TREE;
15030           /* If it's a `,', then there are more declarators.  */
15031           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15032             cp_lexer_consume_token (parser->lexer);
15033           /* If the next token isn't a `;', then we have a parse error.  */
15034           else if (cp_lexer_next_token_is_not (parser->lexer,
15035                                                CPP_SEMICOLON))
15036             {
15037               cp_parser_error (parser, "expected %<;%>");
15038               /* Skip tokens until we find a `;'.  */
15039               cp_parser_skip_to_end_of_statement (parser);
15040
15041               break;
15042             }
15043
15044           if (decl)
15045             {
15046               /* Add DECL to the list of members.  */
15047               if (!friend_p)
15048                 finish_member_declaration (decl);
15049
15050               if (TREE_CODE (decl) == FUNCTION_DECL)
15051                 cp_parser_save_default_args (parser, decl);
15052             }
15053         }
15054     }
15055
15056   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15057 }
15058
15059 /* Parse a pure-specifier.
15060
15061    pure-specifier:
15062      = 0
15063
15064    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15065    Otherwise, ERROR_MARK_NODE is returned.  */
15066
15067 static tree
15068 cp_parser_pure_specifier (cp_parser* parser)
15069 {
15070   cp_token *token;
15071
15072   /* Look for the `=' token.  */
15073   if (!cp_parser_require (parser, CPP_EQ, "`='"))
15074     return error_mark_node;
15075   /* Look for the `0' token.  */
15076   token = cp_lexer_consume_token (parser->lexer);
15077   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15078   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15079     {
15080       cp_parser_error (parser,
15081                        "invalid pure specifier (only `= 0' is allowed)");
15082       cp_parser_skip_to_end_of_statement (parser);
15083       return error_mark_node;
15084     }
15085   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15086     {
15087       error ("templates may not be %<virtual%>");
15088       return error_mark_node;
15089     }
15090
15091   return integer_zero_node;
15092 }
15093
15094 /* Parse a constant-initializer.
15095
15096    constant-initializer:
15097      = constant-expression
15098
15099    Returns a representation of the constant-expression.  */
15100
15101 static tree
15102 cp_parser_constant_initializer (cp_parser* parser)
15103 {
15104   /* Look for the `=' token.  */
15105   if (!cp_parser_require (parser, CPP_EQ, "`='"))
15106     return error_mark_node;
15107
15108   /* It is invalid to write:
15109
15110        struct S { static const int i = { 7 }; };
15111
15112      */
15113   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15114     {
15115       cp_parser_error (parser,
15116                        "a brace-enclosed initializer is not allowed here");
15117       /* Consume the opening brace.  */
15118       cp_lexer_consume_token (parser->lexer);
15119       /* Skip the initializer.  */
15120       cp_parser_skip_to_closing_brace (parser);
15121       /* Look for the trailing `}'.  */
15122       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15123
15124       return error_mark_node;
15125     }
15126
15127   return cp_parser_constant_expression (parser,
15128                                         /*allow_non_constant=*/false,
15129                                         NULL);
15130 }
15131
15132 /* Derived classes [gram.class.derived] */
15133
15134 /* Parse a base-clause.
15135
15136    base-clause:
15137      : base-specifier-list
15138
15139    base-specifier-list:
15140      base-specifier ... [opt]
15141      base-specifier-list , base-specifier ... [opt]
15142
15143    Returns a TREE_LIST representing the base-classes, in the order in
15144    which they were declared.  The representation of each node is as
15145    described by cp_parser_base_specifier.
15146
15147    In the case that no bases are specified, this function will return
15148    NULL_TREE, not ERROR_MARK_NODE.  */
15149
15150 static tree
15151 cp_parser_base_clause (cp_parser* parser)
15152 {
15153   tree bases = NULL_TREE;
15154
15155   /* Look for the `:' that begins the list.  */
15156   cp_parser_require (parser, CPP_COLON, "`:'");
15157
15158   /* Scan the base-specifier-list.  */
15159   while (true)
15160     {
15161       cp_token *token;
15162       tree base;
15163       bool pack_expansion_p = false;
15164
15165       /* Look for the base-specifier.  */
15166       base = cp_parser_base_specifier (parser);
15167       /* Look for the (optional) ellipsis. */
15168       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15169         {
15170           /* Consume the `...'. */
15171           cp_lexer_consume_token (parser->lexer);
15172
15173           pack_expansion_p = true;
15174         }
15175
15176       /* Add BASE to the front of the list.  */
15177       if (base != error_mark_node)
15178         {
15179           if (pack_expansion_p)
15180             /* Make this a pack expansion type. */
15181             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15182           else
15183             check_for_bare_parameter_packs (TREE_VALUE (base));
15184
15185           TREE_CHAIN (base) = bases;
15186           bases = base;
15187         }
15188       /* Peek at the next token.  */
15189       token = cp_lexer_peek_token (parser->lexer);
15190       /* If it's not a comma, then the list is complete.  */
15191       if (token->type != CPP_COMMA)
15192         break;
15193       /* Consume the `,'.  */
15194       cp_lexer_consume_token (parser->lexer);
15195     }
15196
15197   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15198      base class had a qualified name.  However, the next name that
15199      appears is certainly not qualified.  */
15200   parser->scope = NULL_TREE;
15201   parser->qualifying_scope = NULL_TREE;
15202   parser->object_scope = NULL_TREE;
15203
15204   return nreverse (bases);
15205 }
15206
15207 /* Parse a base-specifier.
15208
15209    base-specifier:
15210      :: [opt] nested-name-specifier [opt] class-name
15211      virtual access-specifier [opt] :: [opt] nested-name-specifier
15212        [opt] class-name
15213      access-specifier virtual [opt] :: [opt] nested-name-specifier
15214        [opt] class-name
15215
15216    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15217    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15218    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15219    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15220
15221 static tree
15222 cp_parser_base_specifier (cp_parser* parser)
15223 {
15224   cp_token *token;
15225   bool done = false;
15226   bool virtual_p = false;
15227   bool duplicate_virtual_error_issued_p = false;
15228   bool duplicate_access_error_issued_p = false;
15229   bool class_scope_p, template_p;
15230   tree access = access_default_node;
15231   tree type;
15232
15233   /* Process the optional `virtual' and `access-specifier'.  */
15234   while (!done)
15235     {
15236       /* Peek at the next token.  */
15237       token = cp_lexer_peek_token (parser->lexer);
15238       /* Process `virtual'.  */
15239       switch (token->keyword)
15240         {
15241         case RID_VIRTUAL:
15242           /* If `virtual' appears more than once, issue an error.  */
15243           if (virtual_p && !duplicate_virtual_error_issued_p)
15244             {
15245               cp_parser_error (parser,
15246                                "%<virtual%> specified more than once in base-specified");
15247               duplicate_virtual_error_issued_p = true;
15248             }
15249
15250           virtual_p = true;
15251
15252           /* Consume the `virtual' token.  */
15253           cp_lexer_consume_token (parser->lexer);
15254
15255           break;
15256
15257         case RID_PUBLIC:
15258         case RID_PROTECTED:
15259         case RID_PRIVATE:
15260           /* If more than one access specifier appears, issue an
15261              error.  */
15262           if (access != access_default_node
15263               && !duplicate_access_error_issued_p)
15264             {
15265               cp_parser_error (parser,
15266                                "more than one access specifier in base-specified");
15267               duplicate_access_error_issued_p = true;
15268             }
15269
15270           access = ridpointers[(int) token->keyword];
15271
15272           /* Consume the access-specifier.  */
15273           cp_lexer_consume_token (parser->lexer);
15274
15275           break;
15276
15277         default:
15278           done = true;
15279           break;
15280         }
15281     }
15282   /* It is not uncommon to see programs mechanically, erroneously, use
15283      the 'typename' keyword to denote (dependent) qualified types
15284      as base classes.  */
15285   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15286     {
15287       if (!processing_template_decl)
15288         error ("keyword %<typename%> not allowed outside of templates");
15289       else
15290         error ("keyword %<typename%> not allowed in this context "
15291                "(the base class is implicitly a type)");
15292       cp_lexer_consume_token (parser->lexer);
15293     }
15294
15295   /* Look for the optional `::' operator.  */
15296   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15297   /* Look for the nested-name-specifier.  The simplest way to
15298      implement:
15299
15300        [temp.res]
15301
15302        The keyword `typename' is not permitted in a base-specifier or
15303        mem-initializer; in these contexts a qualified name that
15304        depends on a template-parameter is implicitly assumed to be a
15305        type name.
15306
15307      is to pretend that we have seen the `typename' keyword at this
15308      point.  */
15309   cp_parser_nested_name_specifier_opt (parser,
15310                                        /*typename_keyword_p=*/true,
15311                                        /*check_dependency_p=*/true,
15312                                        typename_type,
15313                                        /*is_declaration=*/true);
15314   /* If the base class is given by a qualified name, assume that names
15315      we see are type names or templates, as appropriate.  */
15316   class_scope_p = (parser->scope && TYPE_P (parser->scope));
15317   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15318
15319   /* Finally, look for the class-name.  */
15320   type = cp_parser_class_name (parser,
15321                                class_scope_p,
15322                                template_p,
15323                                typename_type,
15324                                /*check_dependency_p=*/true,
15325                                /*class_head_p=*/false,
15326                                /*is_declaration=*/true);
15327
15328   if (type == error_mark_node)
15329     return error_mark_node;
15330
15331   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15332 }
15333
15334 /* Exception handling [gram.exception] */
15335
15336 /* Parse an (optional) exception-specification.
15337
15338    exception-specification:
15339      throw ( type-id-list [opt] )
15340
15341    Returns a TREE_LIST representing the exception-specification.  The
15342    TREE_VALUE of each node is a type.  */
15343
15344 static tree
15345 cp_parser_exception_specification_opt (cp_parser* parser)
15346 {
15347   cp_token *token;
15348   tree type_id_list;
15349
15350   /* Peek at the next token.  */
15351   token = cp_lexer_peek_token (parser->lexer);
15352   /* If it's not `throw', then there's no exception-specification.  */
15353   if (!cp_parser_is_keyword (token, RID_THROW))
15354     return NULL_TREE;
15355
15356   /* Consume the `throw'.  */
15357   cp_lexer_consume_token (parser->lexer);
15358
15359   /* Look for the `('.  */
15360   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15361
15362   /* Peek at the next token.  */
15363   token = cp_lexer_peek_token (parser->lexer);
15364   /* If it's not a `)', then there is a type-id-list.  */
15365   if (token->type != CPP_CLOSE_PAREN)
15366     {
15367       const char *saved_message;
15368
15369       /* Types may not be defined in an exception-specification.  */
15370       saved_message = parser->type_definition_forbidden_message;
15371       parser->type_definition_forbidden_message
15372         = "types may not be defined in an exception-specification";
15373       /* Parse the type-id-list.  */
15374       type_id_list = cp_parser_type_id_list (parser);
15375       /* Restore the saved message.  */
15376       parser->type_definition_forbidden_message = saved_message;
15377     }
15378   else
15379     type_id_list = empty_except_spec;
15380
15381   /* Look for the `)'.  */
15382   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15383
15384   return type_id_list;
15385 }
15386
15387 /* Parse an (optional) type-id-list.
15388
15389    type-id-list:
15390      type-id ... [opt]
15391      type-id-list , type-id ... [opt]
15392
15393    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
15394    in the order that the types were presented.  */
15395
15396 static tree
15397 cp_parser_type_id_list (cp_parser* parser)
15398 {
15399   tree types = NULL_TREE;
15400
15401   while (true)
15402     {
15403       cp_token *token;
15404       tree type;
15405
15406       /* Get the next type-id.  */
15407       type = cp_parser_type_id (parser);
15408       /* Parse the optional ellipsis. */
15409       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15410         {
15411           /* Consume the `...'. */
15412           cp_lexer_consume_token (parser->lexer);
15413
15414           /* Turn the type into a pack expansion expression. */
15415           type = make_pack_expansion (type);
15416         }
15417       /* Add it to the list.  */
15418       types = add_exception_specifier (types, type, /*complain=*/1);
15419       /* Peek at the next token.  */
15420       token = cp_lexer_peek_token (parser->lexer);
15421       /* If it is not a `,', we are done.  */
15422       if (token->type != CPP_COMMA)
15423         break;
15424       /* Consume the `,'.  */
15425       cp_lexer_consume_token (parser->lexer);
15426     }
15427
15428   return nreverse (types);
15429 }
15430
15431 /* Parse a try-block.
15432
15433    try-block:
15434      try compound-statement handler-seq  */
15435
15436 static tree
15437 cp_parser_try_block (cp_parser* parser)
15438 {
15439   tree try_block;
15440
15441   cp_parser_require_keyword (parser, RID_TRY, "`try'");
15442   try_block = begin_try_block ();
15443   cp_parser_compound_statement (parser, NULL, true);
15444   finish_try_block (try_block);
15445   cp_parser_handler_seq (parser);
15446   finish_handler_sequence (try_block);
15447
15448   return try_block;
15449 }
15450
15451 /* Parse a function-try-block.
15452
15453    function-try-block:
15454      try ctor-initializer [opt] function-body handler-seq  */
15455
15456 static bool
15457 cp_parser_function_try_block (cp_parser* parser)
15458 {
15459   tree compound_stmt;
15460   tree try_block;
15461   bool ctor_initializer_p;
15462
15463   /* Look for the `try' keyword.  */
15464   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15465     return false;
15466   /* Let the rest of the front end know where we are.  */
15467   try_block = begin_function_try_block (&compound_stmt);
15468   /* Parse the function-body.  */
15469   ctor_initializer_p
15470     = cp_parser_ctor_initializer_opt_and_function_body (parser);
15471   /* We're done with the `try' part.  */
15472   finish_function_try_block (try_block);
15473   /* Parse the handlers.  */
15474   cp_parser_handler_seq (parser);
15475   /* We're done with the handlers.  */
15476   finish_function_handler_sequence (try_block, compound_stmt);
15477
15478   return ctor_initializer_p;
15479 }
15480
15481 /* Parse a handler-seq.
15482
15483    handler-seq:
15484      handler handler-seq [opt]  */
15485
15486 static void
15487 cp_parser_handler_seq (cp_parser* parser)
15488 {
15489   while (true)
15490     {
15491       cp_token *token;
15492
15493       /* Parse the handler.  */
15494       cp_parser_handler (parser);
15495       /* Peek at the next token.  */
15496       token = cp_lexer_peek_token (parser->lexer);
15497       /* If it's not `catch' then there are no more handlers.  */
15498       if (!cp_parser_is_keyword (token, RID_CATCH))
15499         break;
15500     }
15501 }
15502
15503 /* Parse a handler.
15504
15505    handler:
15506      catch ( exception-declaration ) compound-statement  */
15507
15508 static void
15509 cp_parser_handler (cp_parser* parser)
15510 {
15511   tree handler;
15512   tree declaration;
15513
15514   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15515   handler = begin_handler ();
15516   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15517   declaration = cp_parser_exception_declaration (parser);
15518   finish_handler_parms (declaration, handler);
15519   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15520   cp_parser_compound_statement (parser, NULL, false);
15521   finish_handler (handler);
15522 }
15523
15524 /* Parse an exception-declaration.
15525
15526    exception-declaration:
15527      type-specifier-seq declarator
15528      type-specifier-seq abstract-declarator
15529      type-specifier-seq
15530      ...
15531
15532    Returns a VAR_DECL for the declaration, or NULL_TREE if the
15533    ellipsis variant is used.  */
15534
15535 static tree
15536 cp_parser_exception_declaration (cp_parser* parser)
15537 {
15538   cp_decl_specifier_seq type_specifiers;
15539   cp_declarator *declarator;
15540   const char *saved_message;
15541
15542   /* If it's an ellipsis, it's easy to handle.  */
15543   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15544     {
15545       /* Consume the `...' token.  */
15546       cp_lexer_consume_token (parser->lexer);
15547       return NULL_TREE;
15548     }
15549
15550   /* Types may not be defined in exception-declarations.  */
15551   saved_message = parser->type_definition_forbidden_message;
15552   parser->type_definition_forbidden_message
15553     = "types may not be defined in exception-declarations";
15554
15555   /* Parse the type-specifier-seq.  */
15556   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15557                                 &type_specifiers);
15558   /* If it's a `)', then there is no declarator.  */
15559   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15560     declarator = NULL;
15561   else
15562     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15563                                        /*ctor_dtor_or_conv_p=*/NULL,
15564                                        /*parenthesized_p=*/NULL,
15565                                        /*member_p=*/false);
15566
15567   /* Restore the saved message.  */
15568   parser->type_definition_forbidden_message = saved_message;
15569
15570   if (!type_specifiers.any_specifiers_p)
15571     return error_mark_node;
15572
15573   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15574 }
15575
15576 /* Parse a throw-expression.
15577
15578    throw-expression:
15579      throw assignment-expression [opt]
15580
15581    Returns a THROW_EXPR representing the throw-expression.  */
15582
15583 static tree
15584 cp_parser_throw_expression (cp_parser* parser)
15585 {
15586   tree expression;
15587   cp_token* token;
15588
15589   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15590   token = cp_lexer_peek_token (parser->lexer);
15591   /* Figure out whether or not there is an assignment-expression
15592      following the "throw" keyword.  */
15593   if (token->type == CPP_COMMA
15594       || token->type == CPP_SEMICOLON
15595       || token->type == CPP_CLOSE_PAREN
15596       || token->type == CPP_CLOSE_SQUARE
15597       || token->type == CPP_CLOSE_BRACE
15598       || token->type == CPP_COLON)
15599     expression = NULL_TREE;
15600   else
15601     expression = cp_parser_assignment_expression (parser,
15602                                                   /*cast_p=*/false);
15603
15604   return build_throw (expression);
15605 }
15606
15607 /* GNU Extensions */
15608
15609 /* Parse an (optional) asm-specification.
15610
15611    asm-specification:
15612      asm ( string-literal )
15613
15614    If the asm-specification is present, returns a STRING_CST
15615    corresponding to the string-literal.  Otherwise, returns
15616    NULL_TREE.  */
15617
15618 static tree
15619 cp_parser_asm_specification_opt (cp_parser* parser)
15620 {
15621   cp_token *token;
15622   tree asm_specification;
15623
15624   /* Peek at the next token.  */
15625   token = cp_lexer_peek_token (parser->lexer);
15626   /* If the next token isn't the `asm' keyword, then there's no
15627      asm-specification.  */
15628   if (!cp_parser_is_keyword (token, RID_ASM))
15629     return NULL_TREE;
15630
15631   /* Consume the `asm' token.  */
15632   cp_lexer_consume_token (parser->lexer);
15633   /* Look for the `('.  */
15634   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15635
15636   /* Look for the string-literal.  */
15637   asm_specification = cp_parser_string_literal (parser, false, false);
15638
15639   /* Look for the `)'.  */
15640   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15641
15642   return asm_specification;
15643 }
15644
15645 /* Parse an asm-operand-list.
15646
15647    asm-operand-list:
15648      asm-operand
15649      asm-operand-list , asm-operand
15650
15651    asm-operand:
15652      string-literal ( expression )
15653      [ string-literal ] string-literal ( expression )
15654
15655    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
15656    each node is the expression.  The TREE_PURPOSE is itself a
15657    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15658    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15659    is a STRING_CST for the string literal before the parenthesis. Returns
15660    ERROR_MARK_NODE if any of the operands are invalid.  */
15661
15662 static tree
15663 cp_parser_asm_operand_list (cp_parser* parser)
15664 {
15665   tree asm_operands = NULL_TREE;
15666   bool invalid_operands = false;
15667
15668   while (true)
15669     {
15670       tree string_literal;
15671       tree expression;
15672       tree name;
15673
15674       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15675         {
15676           /* Consume the `[' token.  */
15677           cp_lexer_consume_token (parser->lexer);
15678           /* Read the operand name.  */
15679           name = cp_parser_identifier (parser);
15680           if (name != error_mark_node)
15681             name = build_string (IDENTIFIER_LENGTH (name),
15682                                  IDENTIFIER_POINTER (name));
15683           /* Look for the closing `]'.  */
15684           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15685         }
15686       else
15687         name = NULL_TREE;
15688       /* Look for the string-literal.  */
15689       string_literal = cp_parser_string_literal (parser, false, false);
15690
15691       /* Look for the `('.  */
15692       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15693       /* Parse the expression.  */
15694       expression = cp_parser_expression (parser, /*cast_p=*/false);
15695       /* Look for the `)'.  */
15696       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15697
15698       if (name == error_mark_node 
15699           || string_literal == error_mark_node 
15700           || expression == error_mark_node)
15701         invalid_operands = true;
15702
15703       /* Add this operand to the list.  */
15704       asm_operands = tree_cons (build_tree_list (name, string_literal),
15705                                 expression,
15706                                 asm_operands);
15707       /* If the next token is not a `,', there are no more
15708          operands.  */
15709       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15710         break;
15711       /* Consume the `,'.  */
15712       cp_lexer_consume_token (parser->lexer);
15713     }
15714
15715   return invalid_operands ? error_mark_node : nreverse (asm_operands);
15716 }
15717
15718 /* Parse an asm-clobber-list.
15719
15720    asm-clobber-list:
15721      string-literal
15722      asm-clobber-list , string-literal
15723
15724    Returns a TREE_LIST, indicating the clobbers in the order that they
15725    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
15726
15727 static tree
15728 cp_parser_asm_clobber_list (cp_parser* parser)
15729 {
15730   tree clobbers = NULL_TREE;
15731
15732   while (true)
15733     {
15734       tree string_literal;
15735
15736       /* Look for the string literal.  */
15737       string_literal = cp_parser_string_literal (parser, false, false);
15738       /* Add it to the list.  */
15739       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15740       /* If the next token is not a `,', then the list is
15741          complete.  */
15742       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15743         break;
15744       /* Consume the `,' token.  */
15745       cp_lexer_consume_token (parser->lexer);
15746     }
15747
15748   return clobbers;
15749 }
15750
15751 /* Parse an (optional) series of attributes.
15752
15753    attributes:
15754      attributes attribute
15755
15756    attribute:
15757      __attribute__ (( attribute-list [opt] ))
15758
15759    The return value is as for cp_parser_attribute_list.  */
15760
15761 static tree
15762 cp_parser_attributes_opt (cp_parser* parser)
15763 {
15764   tree attributes = NULL_TREE;
15765
15766   while (true)
15767     {
15768       cp_token *token;
15769       tree attribute_list;
15770
15771       /* Peek at the next token.  */
15772       token = cp_lexer_peek_token (parser->lexer);
15773       /* If it's not `__attribute__', then we're done.  */
15774       if (token->keyword != RID_ATTRIBUTE)
15775         break;
15776
15777       /* Consume the `__attribute__' keyword.  */
15778       cp_lexer_consume_token (parser->lexer);
15779       /* Look for the two `(' tokens.  */
15780       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15781       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15782
15783       /* Peek at the next token.  */
15784       token = cp_lexer_peek_token (parser->lexer);
15785       if (token->type != CPP_CLOSE_PAREN)
15786         /* Parse the attribute-list.  */
15787         attribute_list = cp_parser_attribute_list (parser);
15788       else
15789         /* If the next token is a `)', then there is no attribute
15790            list.  */
15791         attribute_list = NULL;
15792
15793       /* Look for the two `)' tokens.  */
15794       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15795       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15796
15797       /* Add these new attributes to the list.  */
15798       attributes = chainon (attributes, attribute_list);
15799     }
15800
15801   return attributes;
15802 }
15803
15804 /* Parse an attribute-list.
15805
15806    attribute-list:
15807      attribute
15808      attribute-list , attribute
15809
15810    attribute:
15811      identifier
15812      identifier ( identifier )
15813      identifier ( identifier , expression-list )
15814      identifier ( expression-list )
15815
15816    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
15817    to an attribute.  The TREE_PURPOSE of each node is the identifier
15818    indicating which attribute is in use.  The TREE_VALUE represents
15819    the arguments, if any.  */
15820
15821 static tree
15822 cp_parser_attribute_list (cp_parser* parser)
15823 {
15824   tree attribute_list = NULL_TREE;
15825   bool save_translate_strings_p = parser->translate_strings_p;
15826
15827   parser->translate_strings_p = false;
15828   while (true)
15829     {
15830       cp_token *token;
15831       tree identifier;
15832       tree attribute;
15833
15834       /* Look for the identifier.  We also allow keywords here; for
15835          example `__attribute__ ((const))' is legal.  */
15836       token = cp_lexer_peek_token (parser->lexer);
15837       if (token->type == CPP_NAME
15838           || token->type == CPP_KEYWORD)
15839         {
15840           tree arguments = NULL_TREE;
15841
15842           /* Consume the token.  */
15843           token = cp_lexer_consume_token (parser->lexer);
15844
15845           /* Save away the identifier that indicates which attribute
15846              this is.  */
15847           identifier = token->u.value;
15848           attribute = build_tree_list (identifier, NULL_TREE);
15849
15850           /* Peek at the next token.  */
15851           token = cp_lexer_peek_token (parser->lexer);
15852           /* If it's an `(', then parse the attribute arguments.  */
15853           if (token->type == CPP_OPEN_PAREN)
15854             {
15855               arguments = cp_parser_parenthesized_expression_list
15856                           (parser, true, /*cast_p=*/false,
15857                            /*allow_expansion_p=*/false,
15858                            /*non_constant_p=*/NULL);
15859               /* Save the arguments away.  */
15860               TREE_VALUE (attribute) = arguments;
15861             }
15862
15863           if (arguments != error_mark_node)
15864             {
15865               /* Add this attribute to the list.  */
15866               TREE_CHAIN (attribute) = attribute_list;
15867               attribute_list = attribute;
15868             }
15869
15870           token = cp_lexer_peek_token (parser->lexer);
15871         }
15872       /* Now, look for more attributes.  If the next token isn't a
15873          `,', we're done.  */
15874       if (token->type != CPP_COMMA)
15875         break;
15876
15877       /* Consume the comma and keep going.  */
15878       cp_lexer_consume_token (parser->lexer);
15879     }
15880   parser->translate_strings_p = save_translate_strings_p;
15881
15882   /* We built up the list in reverse order.  */
15883   return nreverse (attribute_list);
15884 }
15885
15886 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
15887    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
15888    current value of the PEDANTIC flag, regardless of whether or not
15889    the `__extension__' keyword is present.  The caller is responsible
15890    for restoring the value of the PEDANTIC flag.  */
15891
15892 static bool
15893 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15894 {
15895   /* Save the old value of the PEDANTIC flag.  */
15896   *saved_pedantic = pedantic;
15897
15898   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15899     {
15900       /* Consume the `__extension__' token.  */
15901       cp_lexer_consume_token (parser->lexer);
15902       /* We're not being pedantic while the `__extension__' keyword is
15903          in effect.  */
15904       pedantic = 0;
15905
15906       return true;
15907     }
15908
15909   return false;
15910 }
15911
15912 /* Parse a label declaration.
15913
15914    label-declaration:
15915      __label__ label-declarator-seq ;
15916
15917    label-declarator-seq:
15918      identifier , label-declarator-seq
15919      identifier  */
15920
15921 static void
15922 cp_parser_label_declaration (cp_parser* parser)
15923 {
15924   /* Look for the `__label__' keyword.  */
15925   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15926
15927   while (true)
15928     {
15929       tree identifier;
15930
15931       /* Look for an identifier.  */
15932       identifier = cp_parser_identifier (parser);
15933       /* If we failed, stop.  */
15934       if (identifier == error_mark_node)
15935         break;
15936       /* Declare it as a label.  */
15937       finish_label_decl (identifier);
15938       /* If the next token is a `;', stop.  */
15939       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15940         break;
15941       /* Look for the `,' separating the label declarations.  */
15942       cp_parser_require (parser, CPP_COMMA, "`,'");
15943     }
15944
15945   /* Look for the final `;'.  */
15946   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15947 }
15948
15949 /* Support Functions */
15950
15951 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15952    NAME should have one of the representations used for an
15953    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15954    is returned.  If PARSER->SCOPE is a dependent type, then a
15955    SCOPE_REF is returned.
15956
15957    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15958    returned; the name was already resolved when the TEMPLATE_ID_EXPR
15959    was formed.  Abstractly, such entities should not be passed to this
15960    function, because they do not need to be looked up, but it is
15961    simpler to check for this special case here, rather than at the
15962    call-sites.
15963
15964    In cases not explicitly covered above, this function returns a
15965    DECL, OVERLOAD, or baselink representing the result of the lookup.
15966    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15967    is returned.
15968
15969    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15970    (e.g., "struct") that was used.  In that case bindings that do not
15971    refer to types are ignored.
15972
15973    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15974    ignored.
15975
15976    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15977    are ignored.
15978
15979    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15980    types.
15981
15982    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15983    TREE_LIST of candidates if name-lookup results in an ambiguity, and
15984    NULL_TREE otherwise.  */
15985
15986 static tree
15987 cp_parser_lookup_name (cp_parser *parser, tree name,
15988                        enum tag_types tag_type,
15989                        bool is_template,
15990                        bool is_namespace,
15991                        bool check_dependency,
15992                        tree *ambiguous_decls)
15993 {
15994   int flags = 0;
15995   tree decl;
15996   tree object_type = parser->context->object_type;
15997
15998   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15999     flags |= LOOKUP_COMPLAIN;
16000
16001   /* Assume that the lookup will be unambiguous.  */
16002   if (ambiguous_decls)
16003     *ambiguous_decls = NULL_TREE;
16004
16005   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16006      no longer valid.  Note that if we are parsing tentatively, and
16007      the parse fails, OBJECT_TYPE will be automatically restored.  */
16008   parser->context->object_type = NULL_TREE;
16009
16010   if (name == error_mark_node)
16011     return error_mark_node;
16012
16013   /* A template-id has already been resolved; there is no lookup to
16014      do.  */
16015   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16016     return name;
16017   if (BASELINK_P (name))
16018     {
16019       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16020                   == TEMPLATE_ID_EXPR);
16021       return name;
16022     }
16023
16024   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16025      it should already have been checked to make sure that the name
16026      used matches the type being destroyed.  */
16027   if (TREE_CODE (name) == BIT_NOT_EXPR)
16028     {
16029       tree type;
16030
16031       /* Figure out to which type this destructor applies.  */
16032       if (parser->scope)
16033         type = parser->scope;
16034       else if (object_type)
16035         type = object_type;
16036       else
16037         type = current_class_type;
16038       /* If that's not a class type, there is no destructor.  */
16039       if (!type || !CLASS_TYPE_P (type))
16040         return error_mark_node;
16041       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16042         lazily_declare_fn (sfk_destructor, type);
16043       if (!CLASSTYPE_DESTRUCTORS (type))
16044           return error_mark_node;
16045       /* If it was a class type, return the destructor.  */
16046       return CLASSTYPE_DESTRUCTORS (type);
16047     }
16048
16049   /* By this point, the NAME should be an ordinary identifier.  If
16050      the id-expression was a qualified name, the qualifying scope is
16051      stored in PARSER->SCOPE at this point.  */
16052   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16053
16054   /* Perform the lookup.  */
16055   if (parser->scope)
16056     {
16057       bool dependent_p;
16058
16059       if (parser->scope == error_mark_node)
16060         return error_mark_node;
16061
16062       /* If the SCOPE is dependent, the lookup must be deferred until
16063          the template is instantiated -- unless we are explicitly
16064          looking up names in uninstantiated templates.  Even then, we
16065          cannot look up the name if the scope is not a class type; it
16066          might, for example, be a template type parameter.  */
16067       dependent_p = (TYPE_P (parser->scope)
16068                      && !(parser->in_declarator_p
16069                           && currently_open_class (parser->scope))
16070                      && dependent_type_p (parser->scope));
16071       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16072            && dependent_p)
16073         {
16074           if (tag_type)
16075             {
16076               tree type;
16077
16078               /* The resolution to Core Issue 180 says that `struct
16079                  A::B' should be considered a type-name, even if `A'
16080                  is dependent.  */
16081               type = make_typename_type (parser->scope, name, tag_type,
16082                                          /*complain=*/tf_error);
16083               decl = TYPE_NAME (type);
16084             }
16085           else if (is_template
16086                    && (cp_parser_next_token_ends_template_argument_p (parser)
16087                        || cp_lexer_next_token_is (parser->lexer,
16088                                                   CPP_CLOSE_PAREN)))
16089             decl = make_unbound_class_template (parser->scope,
16090                                                 name, NULL_TREE,
16091                                                 /*complain=*/tf_error);
16092           else
16093             decl = build_qualified_name (/*type=*/NULL_TREE,
16094                                          parser->scope, name,
16095                                          is_template);
16096         }
16097       else
16098         {
16099           tree pushed_scope = NULL_TREE;
16100
16101           /* If PARSER->SCOPE is a dependent type, then it must be a
16102              class type, and we must not be checking dependencies;
16103              otherwise, we would have processed this lookup above.  So
16104              that PARSER->SCOPE is not considered a dependent base by
16105              lookup_member, we must enter the scope here.  */
16106           if (dependent_p)
16107             pushed_scope = push_scope (parser->scope);
16108           /* If the PARSER->SCOPE is a template specialization, it
16109              may be instantiated during name lookup.  In that case,
16110              errors may be issued.  Even if we rollback the current
16111              tentative parse, those errors are valid.  */
16112           decl = lookup_qualified_name (parser->scope, name,
16113                                         tag_type != none_type,
16114                                         /*complain=*/true);
16115           if (pushed_scope)
16116             pop_scope (pushed_scope);
16117         }
16118       parser->qualifying_scope = parser->scope;
16119       parser->object_scope = NULL_TREE;
16120     }
16121   else if (object_type)
16122     {
16123       tree object_decl = NULL_TREE;
16124       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16125          OBJECT_TYPE is not a class.  */
16126       if (CLASS_TYPE_P (object_type))
16127         /* If the OBJECT_TYPE is a template specialization, it may
16128            be instantiated during name lookup.  In that case, errors
16129            may be issued.  Even if we rollback the current tentative
16130            parse, those errors are valid.  */
16131         object_decl = lookup_member (object_type,
16132                                      name,
16133                                      /*protect=*/0,
16134                                      tag_type != none_type);
16135       /* Look it up in the enclosing context, too.  */
16136       decl = lookup_name_real (name, tag_type != none_type,
16137                                /*nonclass=*/0,
16138                                /*block_p=*/true, is_namespace, flags);
16139       parser->object_scope = object_type;
16140       parser->qualifying_scope = NULL_TREE;
16141       if (object_decl)
16142         decl = object_decl;
16143     }
16144   else
16145     {
16146       decl = lookup_name_real (name, tag_type != none_type,
16147                                /*nonclass=*/0,
16148                                /*block_p=*/true, is_namespace, flags);
16149       parser->qualifying_scope = NULL_TREE;
16150       parser->object_scope = NULL_TREE;
16151     }
16152
16153   /* If the lookup failed, let our caller know.  */
16154   if (!decl || decl == error_mark_node)
16155     return error_mark_node;
16156
16157   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16158   if (TREE_CODE (decl) == TREE_LIST)
16159     {
16160       if (ambiguous_decls)
16161         *ambiguous_decls = decl;
16162       /* The error message we have to print is too complicated for
16163          cp_parser_error, so we incorporate its actions directly.  */
16164       if (!cp_parser_simulate_error (parser))
16165         {
16166           error ("reference to %qD is ambiguous", name);
16167           print_candidates (decl);
16168         }
16169       return error_mark_node;
16170     }
16171
16172   gcc_assert (DECL_P (decl)
16173               || TREE_CODE (decl) == OVERLOAD
16174               || TREE_CODE (decl) == SCOPE_REF
16175               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16176               || BASELINK_P (decl));
16177
16178   /* If we have resolved the name of a member declaration, check to
16179      see if the declaration is accessible.  When the name resolves to
16180      set of overloaded functions, accessibility is checked when
16181      overload resolution is done.
16182
16183      During an explicit instantiation, access is not checked at all,
16184      as per [temp.explicit].  */
16185   if (DECL_P (decl))
16186     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16187
16188   return decl;
16189 }
16190
16191 /* Like cp_parser_lookup_name, but for use in the typical case where
16192    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16193    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16194
16195 static tree
16196 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16197 {
16198   return cp_parser_lookup_name (parser, name,
16199                                 none_type,
16200                                 /*is_template=*/false,
16201                                 /*is_namespace=*/false,
16202                                 /*check_dependency=*/true,
16203                                 /*ambiguous_decls=*/NULL);
16204 }
16205
16206 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16207    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16208    true, the DECL indicates the class being defined in a class-head,
16209    or declared in an elaborated-type-specifier.
16210
16211    Otherwise, return DECL.  */
16212
16213 static tree
16214 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16215 {
16216   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16217      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16218
16219        struct A {
16220          template <typename T> struct B;
16221        };
16222
16223        template <typename T> struct A::B {};
16224
16225      Similarly, in an elaborated-type-specifier:
16226
16227        namespace N { struct X{}; }
16228
16229        struct A {
16230          template <typename T> friend struct N::X;
16231        };
16232
16233      However, if the DECL refers to a class type, and we are in
16234      the scope of the class, then the name lookup automatically
16235      finds the TYPE_DECL created by build_self_reference rather
16236      than a TEMPLATE_DECL.  For example, in:
16237
16238        template <class T> struct S {
16239          S s;
16240        };
16241
16242      there is no need to handle such case.  */
16243
16244   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16245     return DECL_TEMPLATE_RESULT (decl);
16246
16247   return decl;
16248 }
16249
16250 /* If too many, or too few, template-parameter lists apply to the
16251    declarator, issue an error message.  Returns TRUE if all went well,
16252    and FALSE otherwise.  */
16253
16254 static bool
16255 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16256                                                 cp_declarator *declarator)
16257 {
16258   unsigned num_templates;
16259
16260   /* We haven't seen any classes that involve template parameters yet.  */
16261   num_templates = 0;
16262
16263   switch (declarator->kind)
16264     {
16265     case cdk_id:
16266       if (declarator->u.id.qualifying_scope)
16267         {
16268           tree scope;
16269           tree member;
16270
16271           scope = declarator->u.id.qualifying_scope;
16272           member = declarator->u.id.unqualified_name;
16273
16274           while (scope && CLASS_TYPE_P (scope))
16275             {
16276               /* You're supposed to have one `template <...>'
16277                  for every template class, but you don't need one
16278                  for a full specialization.  For example:
16279
16280                  template <class T> struct S{};
16281                  template <> struct S<int> { void f(); };
16282                  void S<int>::f () {}
16283
16284                  is correct; there shouldn't be a `template <>' for
16285                  the definition of `S<int>::f'.  */
16286               if (!CLASSTYPE_TEMPLATE_INFO (scope))
16287                 /* If SCOPE does not have template information of any
16288                    kind, then it is not a template, nor is it nested
16289                    within a template.  */
16290                 break;
16291               if (explicit_class_specialization_p (scope))
16292                 break;
16293               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16294                 ++num_templates;
16295
16296               scope = TYPE_CONTEXT (scope);
16297             }
16298         }
16299       else if (TREE_CODE (declarator->u.id.unqualified_name)
16300                == TEMPLATE_ID_EXPR)
16301         /* If the DECLARATOR has the form `X<y>' then it uses one
16302            additional level of template parameters.  */
16303         ++num_templates;
16304
16305       return cp_parser_check_template_parameters (parser,
16306                                                   num_templates);
16307
16308     case cdk_function:
16309     case cdk_array:
16310     case cdk_pointer:
16311     case cdk_reference:
16312     case cdk_ptrmem:
16313       return (cp_parser_check_declarator_template_parameters
16314               (parser, declarator->declarator));
16315
16316     case cdk_error:
16317       return true;
16318
16319     default:
16320       gcc_unreachable ();
16321     }
16322   return false;
16323 }
16324
16325 /* NUM_TEMPLATES were used in the current declaration.  If that is
16326    invalid, return FALSE and issue an error messages.  Otherwise,
16327    return TRUE.  */
16328
16329 static bool
16330 cp_parser_check_template_parameters (cp_parser* parser,
16331                                      unsigned num_templates)
16332 {
16333   /* If there are more template classes than parameter lists, we have
16334      something like:
16335
16336        template <class T> void S<T>::R<T>::f ();  */
16337   if (parser->num_template_parameter_lists < num_templates)
16338     {
16339       error ("too few template-parameter-lists");
16340       return false;
16341     }
16342   /* If there are the same number of template classes and parameter
16343      lists, that's OK.  */
16344   if (parser->num_template_parameter_lists == num_templates)
16345     return true;
16346   /* If there are more, but only one more, then we are referring to a
16347      member template.  That's OK too.  */
16348   if (parser->num_template_parameter_lists == num_templates + 1)
16349       return true;
16350   /* Otherwise, there are too many template parameter lists.  We have
16351      something like:
16352
16353      template <class T> template <class U> void S::f();  */
16354   error ("too many template-parameter-lists");
16355   return false;
16356 }
16357
16358 /* Parse an optional `::' token indicating that the following name is
16359    from the global namespace.  If so, PARSER->SCOPE is set to the
16360    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16361    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16362    Returns the new value of PARSER->SCOPE, if the `::' token is
16363    present, and NULL_TREE otherwise.  */
16364
16365 static tree
16366 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16367 {
16368   cp_token *token;
16369
16370   /* Peek at the next token.  */
16371   token = cp_lexer_peek_token (parser->lexer);
16372   /* If we're looking at a `::' token then we're starting from the
16373      global namespace, not our current location.  */
16374   if (token->type == CPP_SCOPE)
16375     {
16376       /* Consume the `::' token.  */
16377       cp_lexer_consume_token (parser->lexer);
16378       /* Set the SCOPE so that we know where to start the lookup.  */
16379       parser->scope = global_namespace;
16380       parser->qualifying_scope = global_namespace;
16381       parser->object_scope = NULL_TREE;
16382
16383       return parser->scope;
16384     }
16385   else if (!current_scope_valid_p)
16386     {
16387       parser->scope = NULL_TREE;
16388       parser->qualifying_scope = NULL_TREE;
16389       parser->object_scope = NULL_TREE;
16390     }
16391
16392   return NULL_TREE;
16393 }
16394
16395 /* Returns TRUE if the upcoming token sequence is the start of a
16396    constructor declarator.  If FRIEND_P is true, the declarator is
16397    preceded by the `friend' specifier.  */
16398
16399 static bool
16400 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16401 {
16402   bool constructor_p;
16403   tree type_decl = NULL_TREE;
16404   bool nested_name_p;
16405   cp_token *next_token;
16406
16407   /* The common case is that this is not a constructor declarator, so
16408      try to avoid doing lots of work if at all possible.  It's not
16409      valid declare a constructor at function scope.  */
16410   if (parser->in_function_body)
16411     return false;
16412   /* And only certain tokens can begin a constructor declarator.  */
16413   next_token = cp_lexer_peek_token (parser->lexer);
16414   if (next_token->type != CPP_NAME
16415       && next_token->type != CPP_SCOPE
16416       && next_token->type != CPP_NESTED_NAME_SPECIFIER
16417       && next_token->type != CPP_TEMPLATE_ID)
16418     return false;
16419
16420   /* Parse tentatively; we are going to roll back all of the tokens
16421      consumed here.  */
16422   cp_parser_parse_tentatively (parser);
16423   /* Assume that we are looking at a constructor declarator.  */
16424   constructor_p = true;
16425
16426   /* Look for the optional `::' operator.  */
16427   cp_parser_global_scope_opt (parser,
16428                               /*current_scope_valid_p=*/false);
16429   /* Look for the nested-name-specifier.  */
16430   nested_name_p
16431     = (cp_parser_nested_name_specifier_opt (parser,
16432                                             /*typename_keyword_p=*/false,
16433                                             /*check_dependency_p=*/false,
16434                                             /*type_p=*/false,
16435                                             /*is_declaration=*/false)
16436        != NULL_TREE);
16437   /* Outside of a class-specifier, there must be a
16438      nested-name-specifier.  */
16439   if (!nested_name_p &&
16440       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16441        || friend_p))
16442     constructor_p = false;
16443   /* If we still think that this might be a constructor-declarator,
16444      look for a class-name.  */
16445   if (constructor_p)
16446     {
16447       /* If we have:
16448
16449            template <typename T> struct S { S(); };
16450            template <typename T> S<T>::S ();
16451
16452          we must recognize that the nested `S' names a class.
16453          Similarly, for:
16454
16455            template <typename T> S<T>::S<T> ();
16456
16457          we must recognize that the nested `S' names a template.  */
16458       type_decl = cp_parser_class_name (parser,
16459                                         /*typename_keyword_p=*/false,
16460                                         /*template_keyword_p=*/false,
16461                                         none_type,
16462                                         /*check_dependency_p=*/false,
16463                                         /*class_head_p=*/false,
16464                                         /*is_declaration=*/false);
16465       /* If there was no class-name, then this is not a constructor.  */
16466       constructor_p = !cp_parser_error_occurred (parser);
16467     }
16468
16469   /* If we're still considering a constructor, we have to see a `(',
16470      to begin the parameter-declaration-clause, followed by either a
16471      `)', an `...', or a decl-specifier.  We need to check for a
16472      type-specifier to avoid being fooled into thinking that:
16473
16474        S::S (f) (int);
16475
16476      is a constructor.  (It is actually a function named `f' that
16477      takes one parameter (of type `int') and returns a value of type
16478      `S::S'.  */
16479   if (constructor_p
16480       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16481     {
16482       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16483           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16484           /* A parameter declaration begins with a decl-specifier,
16485              which is either the "attribute" keyword, a storage class
16486              specifier, or (usually) a type-specifier.  */
16487           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16488         {
16489           tree type;
16490           tree pushed_scope = NULL_TREE;
16491           unsigned saved_num_template_parameter_lists;
16492
16493           /* Names appearing in the type-specifier should be looked up
16494              in the scope of the class.  */
16495           if (current_class_type)
16496             type = NULL_TREE;
16497           else
16498             {
16499               type = TREE_TYPE (type_decl);
16500               if (TREE_CODE (type) == TYPENAME_TYPE)
16501                 {
16502                   type = resolve_typename_type (type,
16503                                                 /*only_current_p=*/false);
16504                   if (TREE_CODE (type) == TYPENAME_TYPE)
16505                     {
16506                       cp_parser_abort_tentative_parse (parser);
16507                       return false;
16508                     }
16509                 }
16510               pushed_scope = push_scope (type);
16511             }
16512
16513           /* Inside the constructor parameter list, surrounding
16514              template-parameter-lists do not apply.  */
16515           saved_num_template_parameter_lists
16516             = parser->num_template_parameter_lists;
16517           parser->num_template_parameter_lists = 0;
16518
16519           /* Look for the type-specifier.  */
16520           cp_parser_type_specifier (parser,
16521                                     CP_PARSER_FLAGS_NONE,
16522                                     /*decl_specs=*/NULL,
16523                                     /*is_declarator=*/true,
16524                                     /*declares_class_or_enum=*/NULL,
16525                                     /*is_cv_qualifier=*/NULL);
16526
16527           parser->num_template_parameter_lists
16528             = saved_num_template_parameter_lists;
16529
16530           /* Leave the scope of the class.  */
16531           if (pushed_scope)
16532             pop_scope (pushed_scope);
16533
16534           constructor_p = !cp_parser_error_occurred (parser);
16535         }
16536     }
16537   else
16538     constructor_p = false;
16539   /* We did not really want to consume any tokens.  */
16540   cp_parser_abort_tentative_parse (parser);
16541
16542   return constructor_p;
16543 }
16544
16545 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16546    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
16547    they must be performed once we are in the scope of the function.
16548
16549    Returns the function defined.  */
16550
16551 static tree
16552 cp_parser_function_definition_from_specifiers_and_declarator
16553   (cp_parser* parser,
16554    cp_decl_specifier_seq *decl_specifiers,
16555    tree attributes,
16556    const cp_declarator *declarator)
16557 {
16558   tree fn;
16559   bool success_p;
16560
16561   /* Begin the function-definition.  */
16562   success_p = start_function (decl_specifiers, declarator, attributes);
16563
16564   /* The things we're about to see are not directly qualified by any
16565      template headers we've seen thus far.  */
16566   reset_specialization ();
16567
16568   /* If there were names looked up in the decl-specifier-seq that we
16569      did not check, check them now.  We must wait until we are in the
16570      scope of the function to perform the checks, since the function
16571      might be a friend.  */
16572   perform_deferred_access_checks ();
16573
16574   if (!success_p)
16575     {
16576       /* Skip the entire function.  */
16577       cp_parser_skip_to_end_of_block_or_statement (parser);
16578       fn = error_mark_node;
16579     }
16580   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16581     {
16582       /* Seen already, skip it.  An error message has already been output.  */
16583       cp_parser_skip_to_end_of_block_or_statement (parser);
16584       fn = current_function_decl;
16585       current_function_decl = NULL_TREE;
16586       /* If this is a function from a class, pop the nested class.  */
16587       if (current_class_name)
16588         pop_nested_class ();
16589     }
16590   else
16591     fn = cp_parser_function_definition_after_declarator (parser,
16592                                                          /*inline_p=*/false);
16593
16594   return fn;
16595 }
16596
16597 /* Parse the part of a function-definition that follows the
16598    declarator.  INLINE_P is TRUE iff this function is an inline
16599    function defined with a class-specifier.
16600
16601    Returns the function defined.  */
16602
16603 static tree
16604 cp_parser_function_definition_after_declarator (cp_parser* parser,
16605                                                 bool inline_p)
16606 {
16607   tree fn;
16608   bool ctor_initializer_p = false;
16609   bool saved_in_unbraced_linkage_specification_p;
16610   bool saved_in_function_body;
16611   unsigned saved_num_template_parameter_lists;
16612
16613   saved_in_function_body = parser->in_function_body;
16614   parser->in_function_body = true;
16615   /* If the next token is `return', then the code may be trying to
16616      make use of the "named return value" extension that G++ used to
16617      support.  */
16618   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16619     {
16620       /* Consume the `return' keyword.  */
16621       cp_lexer_consume_token (parser->lexer);
16622       /* Look for the identifier that indicates what value is to be
16623          returned.  */
16624       cp_parser_identifier (parser);
16625       /* Issue an error message.  */
16626       error ("named return values are no longer supported");
16627       /* Skip tokens until we reach the start of the function body.  */
16628       while (true)
16629         {
16630           cp_token *token = cp_lexer_peek_token (parser->lexer);
16631           if (token->type == CPP_OPEN_BRACE
16632               || token->type == CPP_EOF
16633               || token->type == CPP_PRAGMA_EOL)
16634             break;
16635           cp_lexer_consume_token (parser->lexer);
16636         }
16637     }
16638   /* The `extern' in `extern "C" void f () { ... }' does not apply to
16639      anything declared inside `f'.  */
16640   saved_in_unbraced_linkage_specification_p
16641     = parser->in_unbraced_linkage_specification_p;
16642   parser->in_unbraced_linkage_specification_p = false;
16643   /* Inside the function, surrounding template-parameter-lists do not
16644      apply.  */
16645   saved_num_template_parameter_lists
16646     = parser->num_template_parameter_lists;
16647   parser->num_template_parameter_lists = 0;
16648   /* If the next token is `try', then we are looking at a
16649      function-try-block.  */
16650   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16651     ctor_initializer_p = cp_parser_function_try_block (parser);
16652   /* A function-try-block includes the function-body, so we only do
16653      this next part if we're not processing a function-try-block.  */
16654   else
16655     ctor_initializer_p
16656       = cp_parser_ctor_initializer_opt_and_function_body (parser);
16657
16658   /* Finish the function.  */
16659   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16660                         (inline_p ? 2 : 0));
16661   /* Generate code for it, if necessary.  */
16662   expand_or_defer_fn (fn);
16663   /* Restore the saved values.  */
16664   parser->in_unbraced_linkage_specification_p
16665     = saved_in_unbraced_linkage_specification_p;
16666   parser->num_template_parameter_lists
16667     = saved_num_template_parameter_lists;
16668   parser->in_function_body = saved_in_function_body;
16669
16670   return fn;
16671 }
16672
16673 /* Parse a template-declaration, assuming that the `export' (and
16674    `extern') keywords, if present, has already been scanned.  MEMBER_P
16675    is as for cp_parser_template_declaration.  */
16676
16677 static void
16678 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16679 {
16680   tree decl = NULL_TREE;
16681   VEC (deferred_access_check,gc) *checks;
16682   tree parameter_list;
16683   bool friend_p = false;
16684   bool need_lang_pop;
16685
16686   /* Look for the `template' keyword.  */
16687   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16688     return;
16689
16690   /* And the `<'.  */
16691   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16692     return;
16693   if (at_class_scope_p () && current_function_decl)
16694     {
16695       /* 14.5.2.2 [temp.mem]
16696
16697          A local class shall not have member templates.  */
16698       error ("invalid declaration of member template in local class");
16699       cp_parser_skip_to_end_of_block_or_statement (parser);
16700       return;
16701     }
16702   /* [temp]
16703
16704      A template ... shall not have C linkage.  */
16705   if (current_lang_name == lang_name_c)
16706     {
16707       error ("template with C linkage");
16708       /* Give it C++ linkage to avoid confusing other parts of the
16709          front end.  */
16710       push_lang_context (lang_name_cplusplus);
16711       need_lang_pop = true;
16712     }
16713   else
16714     need_lang_pop = false;
16715
16716   /* We cannot perform access checks on the template parameter
16717      declarations until we know what is being declared, just as we
16718      cannot check the decl-specifier list.  */
16719   push_deferring_access_checks (dk_deferred);
16720
16721   /* If the next token is `>', then we have an invalid
16722      specialization.  Rather than complain about an invalid template
16723      parameter, issue an error message here.  */
16724   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16725     {
16726       cp_parser_error (parser, "invalid explicit specialization");
16727       begin_specialization ();
16728       parameter_list = NULL_TREE;
16729     }
16730   else
16731     /* Parse the template parameters.  */
16732     parameter_list = cp_parser_template_parameter_list (parser);
16733
16734   /* Get the deferred access checks from the parameter list.  These
16735      will be checked once we know what is being declared, as for a
16736      member template the checks must be performed in the scope of the
16737      class containing the member.  */
16738   checks = get_deferred_access_checks ();
16739
16740   /* Look for the `>'.  */
16741   cp_parser_skip_to_end_of_template_parameter_list (parser);
16742   /* We just processed one more parameter list.  */
16743   ++parser->num_template_parameter_lists;
16744   /* If the next token is `template', there are more template
16745      parameters.  */
16746   if (cp_lexer_next_token_is_keyword (parser->lexer,
16747                                       RID_TEMPLATE))
16748     cp_parser_template_declaration_after_export (parser, member_p);
16749   else
16750     {
16751       /* There are no access checks when parsing a template, as we do not
16752          know if a specialization will be a friend.  */
16753       push_deferring_access_checks (dk_no_check);
16754       decl = cp_parser_single_declaration (parser,
16755                                            checks,
16756                                            member_p,
16757                                            /*explicit_specialization_p=*/false,
16758                                            &friend_p);
16759       pop_deferring_access_checks ();
16760
16761       /* If this is a member template declaration, let the front
16762          end know.  */
16763       if (member_p && !friend_p && decl)
16764         {
16765           if (TREE_CODE (decl) == TYPE_DECL)
16766             cp_parser_check_access_in_redeclaration (decl);
16767
16768           decl = finish_member_template_decl (decl);
16769         }
16770       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16771         make_friend_class (current_class_type, TREE_TYPE (decl),
16772                            /*complain=*/true);
16773     }
16774   /* We are done with the current parameter list.  */
16775   --parser->num_template_parameter_lists;
16776
16777   pop_deferring_access_checks ();
16778
16779   /* Finish up.  */
16780   finish_template_decl (parameter_list);
16781
16782   /* Register member declarations.  */
16783   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16784     finish_member_declaration (decl);
16785   /* For the erroneous case of a template with C linkage, we pushed an
16786      implicit C++ linkage scope; exit that scope now.  */
16787   if (need_lang_pop)
16788     pop_lang_context ();
16789   /* If DECL is a function template, we must return to parse it later.
16790      (Even though there is no definition, there might be default
16791      arguments that need handling.)  */
16792   if (member_p && decl
16793       && (TREE_CODE (decl) == FUNCTION_DECL
16794           || DECL_FUNCTION_TEMPLATE_P (decl)))
16795     TREE_VALUE (parser->unparsed_functions_queues)
16796       = tree_cons (NULL_TREE, decl,
16797                    TREE_VALUE (parser->unparsed_functions_queues));
16798 }
16799
16800 /* Perform the deferred access checks from a template-parameter-list.
16801    CHECKS is a TREE_LIST of access checks, as returned by
16802    get_deferred_access_checks.  */
16803
16804 static void
16805 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16806 {
16807   ++processing_template_parmlist;
16808   perform_access_checks (checks);
16809   --processing_template_parmlist;
16810 }
16811
16812 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16813    `function-definition' sequence.  MEMBER_P is true, this declaration
16814    appears in a class scope.
16815
16816    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
16817    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
16818
16819 static tree
16820 cp_parser_single_declaration (cp_parser* parser,
16821                               VEC (deferred_access_check,gc)* checks,
16822                               bool member_p,
16823                               bool explicit_specialization_p,
16824                               bool* friend_p)
16825 {
16826   int declares_class_or_enum;
16827   tree decl = NULL_TREE;
16828   cp_decl_specifier_seq decl_specifiers;
16829   bool function_definition_p = false;
16830
16831   /* This function is only used when processing a template
16832      declaration.  */
16833   gcc_assert (innermost_scope_kind () == sk_template_parms
16834               || innermost_scope_kind () == sk_template_spec);
16835
16836   /* Defer access checks until we know what is being declared.  */
16837   push_deferring_access_checks (dk_deferred);
16838
16839   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16840      alternative.  */
16841   cp_parser_decl_specifier_seq (parser,
16842                                 CP_PARSER_FLAGS_OPTIONAL,
16843                                 &decl_specifiers,
16844                                 &declares_class_or_enum);
16845   if (friend_p)
16846     *friend_p = cp_parser_friend_p (&decl_specifiers);
16847
16848   /* There are no template typedefs.  */
16849   if (decl_specifiers.specs[(int) ds_typedef])
16850     {
16851       error ("template declaration of %qs", "typedef");
16852       decl = error_mark_node;
16853     }
16854
16855   /* Gather up the access checks that occurred the
16856      decl-specifier-seq.  */
16857   stop_deferring_access_checks ();
16858
16859   /* Check for the declaration of a template class.  */
16860   if (declares_class_or_enum)
16861     {
16862       if (cp_parser_declares_only_class_p (parser))
16863         {
16864           decl = shadow_tag (&decl_specifiers);
16865
16866           /* In this case:
16867
16868                struct C {
16869                  friend template <typename T> struct A<T>::B;
16870                };
16871
16872              A<T>::B will be represented by a TYPENAME_TYPE, and
16873              therefore not recognized by shadow_tag.  */
16874           if (friend_p && *friend_p
16875               && !decl
16876               && decl_specifiers.type
16877               && TYPE_P (decl_specifiers.type))
16878             decl = decl_specifiers.type;
16879
16880           if (decl && decl != error_mark_node)
16881             decl = TYPE_NAME (decl);
16882           else
16883             decl = error_mark_node;
16884
16885           /* Perform access checks for template parameters.  */
16886           cp_parser_perform_template_parameter_access_checks (checks);
16887         }
16888     }
16889   /* If it's not a template class, try for a template function.  If
16890      the next token is a `;', then this declaration does not declare
16891      anything.  But, if there were errors in the decl-specifiers, then
16892      the error might well have come from an attempted class-specifier.
16893      In that case, there's no need to warn about a missing declarator.  */
16894   if (!decl
16895       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16896           || decl_specifiers.type != error_mark_node))
16897     {
16898       decl = cp_parser_init_declarator (parser,
16899                                         &decl_specifiers,
16900                                         checks,
16901                                         /*function_definition_allowed_p=*/true,
16902                                         member_p,
16903                                         declares_class_or_enum,
16904                                         &function_definition_p);
16905
16906     /* 7.1.1-1 [dcl.stc]
16907
16908        A storage-class-specifier shall not be specified in an explicit
16909        specialization...  */
16910     if (decl
16911         && explicit_specialization_p
16912         && decl_specifiers.storage_class != sc_none)
16913       {
16914         error ("explicit template specialization cannot have a storage class");
16915         decl = error_mark_node;
16916       }
16917     }
16918
16919   pop_deferring_access_checks ();
16920
16921   /* Clear any current qualification; whatever comes next is the start
16922      of something new.  */
16923   parser->scope = NULL_TREE;
16924   parser->qualifying_scope = NULL_TREE;
16925   parser->object_scope = NULL_TREE;
16926   /* Look for a trailing `;' after the declaration.  */
16927   if (!function_definition_p
16928       && (decl == error_mark_node
16929           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16930     cp_parser_skip_to_end_of_block_or_statement (parser);
16931
16932   return decl;
16933 }
16934
16935 /* Parse a cast-expression that is not the operand of a unary "&".  */
16936
16937 static tree
16938 cp_parser_simple_cast_expression (cp_parser *parser)
16939 {
16940   return cp_parser_cast_expression (parser, /*address_p=*/false,
16941                                     /*cast_p=*/false);
16942 }
16943
16944 /* Parse a functional cast to TYPE.  Returns an expression
16945    representing the cast.  */
16946
16947 static tree
16948 cp_parser_functional_cast (cp_parser* parser, tree type)
16949 {
16950   tree expression_list;
16951   tree cast;
16952
16953   expression_list
16954     = cp_parser_parenthesized_expression_list (parser, false,
16955                                                /*cast_p=*/true,
16956                                                /*allow_expansion_p=*/true,
16957                                                /*non_constant_p=*/NULL);
16958
16959   cast = build_functional_cast (type, expression_list);
16960   /* [expr.const]/1: In an integral constant expression "only type
16961      conversions to integral or enumeration type can be used".  */
16962   if (TREE_CODE (type) == TYPE_DECL)
16963     type = TREE_TYPE (type);
16964   if (cast != error_mark_node
16965       && !cast_valid_in_integral_constant_expression_p (type)
16966       && (cp_parser_non_integral_constant_expression
16967           (parser, "a call to a constructor")))
16968     return error_mark_node;
16969   return cast;
16970 }
16971
16972 /* Save the tokens that make up the body of a member function defined
16973    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
16974    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
16975    specifiers applied to the declaration.  Returns the FUNCTION_DECL
16976    for the member function.  */
16977
16978 static tree
16979 cp_parser_save_member_function_body (cp_parser* parser,
16980                                      cp_decl_specifier_seq *decl_specifiers,
16981                                      cp_declarator *declarator,
16982                                      tree attributes)
16983 {
16984   cp_token *first;
16985   cp_token *last;
16986   tree fn;
16987
16988   /* Create the function-declaration.  */
16989   fn = start_method (decl_specifiers, declarator, attributes);
16990   /* If something went badly wrong, bail out now.  */
16991   if (fn == error_mark_node)
16992     {
16993       /* If there's a function-body, skip it.  */
16994       if (cp_parser_token_starts_function_definition_p
16995           (cp_lexer_peek_token (parser->lexer)))
16996         cp_parser_skip_to_end_of_block_or_statement (parser);
16997       return error_mark_node;
16998     }
16999
17000   /* Remember it, if there default args to post process.  */
17001   cp_parser_save_default_args (parser, fn);
17002
17003   /* Save away the tokens that make up the body of the
17004      function.  */
17005   first = parser->lexer->next_token;
17006   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17007   /* Handle function try blocks.  */
17008   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17009     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17010   last = parser->lexer->next_token;
17011
17012   /* Save away the inline definition; we will process it when the
17013      class is complete.  */
17014   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17015   DECL_PENDING_INLINE_P (fn) = 1;
17016
17017   /* We need to know that this was defined in the class, so that
17018      friend templates are handled correctly.  */
17019   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17020
17021   /* We're done with the inline definition.  */
17022   finish_method (fn);
17023
17024   /* Add FN to the queue of functions to be parsed later.  */
17025   TREE_VALUE (parser->unparsed_functions_queues)
17026     = tree_cons (NULL_TREE, fn,
17027                  TREE_VALUE (parser->unparsed_functions_queues));
17028
17029   return fn;
17030 }
17031
17032 /* Parse a template-argument-list, as well as the trailing ">" (but
17033    not the opening ">").  See cp_parser_template_argument_list for the
17034    return value.  */
17035
17036 static tree
17037 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17038 {
17039   tree arguments;
17040   tree saved_scope;
17041   tree saved_qualifying_scope;
17042   tree saved_object_scope;
17043   bool saved_greater_than_is_operator_p;
17044   bool saved_skip_evaluation;
17045
17046   /* [temp.names]
17047
17048      When parsing a template-id, the first non-nested `>' is taken as
17049      the end of the template-argument-list rather than a greater-than
17050      operator.  */
17051   saved_greater_than_is_operator_p
17052     = parser->greater_than_is_operator_p;
17053   parser->greater_than_is_operator_p = false;
17054   /* Parsing the argument list may modify SCOPE, so we save it
17055      here.  */
17056   saved_scope = parser->scope;
17057   saved_qualifying_scope = parser->qualifying_scope;
17058   saved_object_scope = parser->object_scope;
17059   /* We need to evaluate the template arguments, even though this
17060      template-id may be nested within a "sizeof".  */
17061   saved_skip_evaluation = skip_evaluation;
17062   skip_evaluation = false;
17063   /* Parse the template-argument-list itself.  */
17064   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17065       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17066     arguments = NULL_TREE;
17067   else
17068     arguments = cp_parser_template_argument_list (parser);
17069   /* Look for the `>' that ends the template-argument-list. If we find
17070      a '>>' instead, it's probably just a typo.  */
17071   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17072     {
17073       if (cxx_dialect != cxx98)
17074         {
17075           /* In C++0x, a `>>' in a template argument list or cast
17076              expression is considered to be two separate `>'
17077              tokens. So, change the current token to a `>', but don't
17078              consume it: it will be consumed later when the outer
17079              template argument list (or cast expression) is parsed.
17080              Note that this replacement of `>' for `>>' is necessary
17081              even if we are parsing tentatively: in the tentative
17082              case, after calling
17083              cp_parser_enclosed_template_argument_list we will always
17084              throw away all of the template arguments and the first
17085              closing `>', either because the template argument list
17086              was erroneous or because we are replacing those tokens
17087              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17088              not have been thrown away) is needed either to close an
17089              outer template argument list or to complete a new-style
17090              cast.  */
17091           cp_token *token = cp_lexer_peek_token (parser->lexer);
17092           token->type = CPP_GREATER;
17093         }
17094       else if (!saved_greater_than_is_operator_p)
17095         {
17096           /* If we're in a nested template argument list, the '>>' has
17097             to be a typo for '> >'. We emit the error message, but we
17098             continue parsing and we push a '>' as next token, so that
17099             the argument list will be parsed correctly.  Note that the
17100             global source location is still on the token before the
17101             '>>', so we need to say explicitly where we want it.  */
17102           cp_token *token = cp_lexer_peek_token (parser->lexer);
17103           error ("%H%<>>%> should be %<> >%> "
17104                  "within a nested template argument list",
17105                  &token->location);
17106
17107           token->type = CPP_GREATER;
17108         }
17109       else
17110         {
17111           /* If this is not a nested template argument list, the '>>'
17112             is a typo for '>'. Emit an error message and continue.
17113             Same deal about the token location, but here we can get it
17114             right by consuming the '>>' before issuing the diagnostic.  */
17115           cp_lexer_consume_token (parser->lexer);
17116           error ("spurious %<>>%>, use %<>%> to terminate "
17117                  "a template argument list");
17118         }
17119     }
17120   else
17121     cp_parser_skip_to_end_of_template_parameter_list (parser);
17122   /* The `>' token might be a greater-than operator again now.  */
17123   parser->greater_than_is_operator_p
17124     = saved_greater_than_is_operator_p;
17125   /* Restore the SAVED_SCOPE.  */
17126   parser->scope = saved_scope;
17127   parser->qualifying_scope = saved_qualifying_scope;
17128   parser->object_scope = saved_object_scope;
17129   skip_evaluation = saved_skip_evaluation;
17130
17131   return arguments;
17132 }
17133
17134 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17135    arguments, or the body of the function have not yet been parsed,
17136    parse them now.  */
17137
17138 static void
17139 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17140 {
17141   /* If this member is a template, get the underlying
17142      FUNCTION_DECL.  */
17143   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17144     member_function = DECL_TEMPLATE_RESULT (member_function);
17145
17146   /* There should not be any class definitions in progress at this
17147      point; the bodies of members are only parsed outside of all class
17148      definitions.  */
17149   gcc_assert (parser->num_classes_being_defined == 0);
17150   /* While we're parsing the member functions we might encounter more
17151      classes.  We want to handle them right away, but we don't want
17152      them getting mixed up with functions that are currently in the
17153      queue.  */
17154   parser->unparsed_functions_queues
17155     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17156
17157   /* Make sure that any template parameters are in scope.  */
17158   maybe_begin_member_template_processing (member_function);
17159
17160   /* If the body of the function has not yet been parsed, parse it
17161      now.  */
17162   if (DECL_PENDING_INLINE_P (member_function))
17163     {
17164       tree function_scope;
17165       cp_token_cache *tokens;
17166
17167       /* The function is no longer pending; we are processing it.  */
17168       tokens = DECL_PENDING_INLINE_INFO (member_function);
17169       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17170       DECL_PENDING_INLINE_P (member_function) = 0;
17171
17172       /* If this is a local class, enter the scope of the containing
17173          function.  */
17174       function_scope = current_function_decl;
17175       if (function_scope)
17176         push_function_context_to (function_scope);
17177
17178
17179       /* Push the body of the function onto the lexer stack.  */
17180       cp_parser_push_lexer_for_tokens (parser, tokens);
17181
17182       /* Let the front end know that we going to be defining this
17183          function.  */
17184       start_preparsed_function (member_function, NULL_TREE,
17185                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17186
17187       /* Don't do access checking if it is a templated function.  */
17188       if (processing_template_decl)
17189         push_deferring_access_checks (dk_no_check);
17190
17191       /* Now, parse the body of the function.  */
17192       cp_parser_function_definition_after_declarator (parser,
17193                                                       /*inline_p=*/true);
17194
17195       if (processing_template_decl)
17196         pop_deferring_access_checks ();
17197
17198       /* Leave the scope of the containing function.  */
17199       if (function_scope)
17200         pop_function_context_from (function_scope);
17201       cp_parser_pop_lexer (parser);
17202     }
17203
17204   /* Remove any template parameters from the symbol table.  */
17205   maybe_end_member_template_processing ();
17206
17207   /* Restore the queue.  */
17208   parser->unparsed_functions_queues
17209     = TREE_CHAIN (parser->unparsed_functions_queues);
17210 }
17211
17212 /* If DECL contains any default args, remember it on the unparsed
17213    functions queue.  */
17214
17215 static void
17216 cp_parser_save_default_args (cp_parser* parser, tree decl)
17217 {
17218   tree probe;
17219
17220   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17221        probe;
17222        probe = TREE_CHAIN (probe))
17223     if (TREE_PURPOSE (probe))
17224       {
17225         TREE_PURPOSE (parser->unparsed_functions_queues)
17226           = tree_cons (current_class_type, decl,
17227                        TREE_PURPOSE (parser->unparsed_functions_queues));
17228         break;
17229       }
17230 }
17231
17232 /* FN is a FUNCTION_DECL which may contains a parameter with an
17233    unparsed DEFAULT_ARG.  Parse the default args now.  This function
17234    assumes that the current scope is the scope in which the default
17235    argument should be processed.  */
17236
17237 static void
17238 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17239 {
17240   bool saved_local_variables_forbidden_p;
17241   tree parm;
17242
17243   /* While we're parsing the default args, we might (due to the
17244      statement expression extension) encounter more classes.  We want
17245      to handle them right away, but we don't want them getting mixed
17246      up with default args that are currently in the queue.  */
17247   parser->unparsed_functions_queues
17248     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17249
17250   /* Local variable names (and the `this' keyword) may not appear
17251      in a default argument.  */
17252   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17253   parser->local_variables_forbidden_p = true;
17254
17255   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17256        parm;
17257        parm = TREE_CHAIN (parm))
17258     {
17259       cp_token_cache *tokens;
17260       tree default_arg = TREE_PURPOSE (parm);
17261       tree parsed_arg;
17262       VEC(tree,gc) *insts;
17263       tree copy;
17264       unsigned ix;
17265
17266       if (!default_arg)
17267         continue;
17268
17269       if (TREE_CODE (default_arg) != DEFAULT_ARG)
17270         /* This can happen for a friend declaration for a function
17271            already declared with default arguments.  */
17272         continue;
17273
17274        /* Push the saved tokens for the default argument onto the parser's
17275           lexer stack.  */
17276       tokens = DEFARG_TOKENS (default_arg);
17277       cp_parser_push_lexer_for_tokens (parser, tokens);
17278
17279       /* Parse the assignment-expression.  */
17280       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17281
17282       if (!processing_template_decl)
17283         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17284
17285       TREE_PURPOSE (parm) = parsed_arg;
17286
17287       /* Update any instantiations we've already created.  */
17288       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17289            VEC_iterate (tree, insts, ix, copy); ix++)
17290         TREE_PURPOSE (copy) = parsed_arg;
17291
17292       /* If the token stream has not been completely used up, then
17293          there was extra junk after the end of the default
17294          argument.  */
17295       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17296         cp_parser_error (parser, "expected %<,%>");
17297
17298       /* Revert to the main lexer.  */
17299       cp_parser_pop_lexer (parser);
17300     }
17301
17302   /* Make sure no default arg is missing.  */
17303   check_default_args (fn);
17304
17305   /* Restore the state of local_variables_forbidden_p.  */
17306   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17307
17308   /* Restore the queue.  */
17309   parser->unparsed_functions_queues
17310     = TREE_CHAIN (parser->unparsed_functions_queues);
17311 }
17312
17313 /* Parse the operand of `sizeof' (or a similar operator).  Returns
17314    either a TYPE or an expression, depending on the form of the
17315    input.  The KEYWORD indicates which kind of expression we have
17316    encountered.  */
17317
17318 static tree
17319 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17320 {
17321   static const char *format;
17322   tree expr = NULL_TREE;
17323   const char *saved_message;
17324   char *tmp;
17325   bool saved_integral_constant_expression_p;
17326   bool saved_non_integral_constant_expression_p;
17327   bool pack_expansion_p = false;
17328
17329   /* Initialize FORMAT the first time we get here.  */
17330   if (!format)
17331     format = "types may not be defined in '%s' expressions";
17332
17333   /* Types cannot be defined in a `sizeof' expression.  Save away the
17334      old message.  */
17335   saved_message = parser->type_definition_forbidden_message;
17336   /* And create the new one.  */
17337   parser->type_definition_forbidden_message = tmp
17338     = XNEWVEC (char, strlen (format)
17339                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17340                + 1 /* `\0' */);
17341   sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17342
17343   /* The restrictions on constant-expressions do not apply inside
17344      sizeof expressions.  */
17345   saved_integral_constant_expression_p
17346     = parser->integral_constant_expression_p;
17347   saved_non_integral_constant_expression_p
17348     = parser->non_integral_constant_expression_p;
17349   parser->integral_constant_expression_p = false;
17350
17351   /* If it's a `...', then we are computing the length of a parameter
17352      pack.  */
17353   if (keyword == RID_SIZEOF
17354       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17355     {
17356       /* Consume the `...'.  */
17357       cp_lexer_consume_token (parser->lexer);
17358       maybe_warn_variadic_templates ();
17359
17360       /* Note that this is an expansion.  */
17361       pack_expansion_p = true;
17362     }
17363
17364   /* Do not actually evaluate the expression.  */
17365   ++skip_evaluation;
17366   /* If it's a `(', then we might be looking at the type-id
17367      construction.  */
17368   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17369     {
17370       tree type;
17371       bool saved_in_type_id_in_expr_p;
17372
17373       /* We can't be sure yet whether we're looking at a type-id or an
17374          expression.  */
17375       cp_parser_parse_tentatively (parser);
17376       /* Consume the `('.  */
17377       cp_lexer_consume_token (parser->lexer);
17378       /* Parse the type-id.  */
17379       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17380       parser->in_type_id_in_expr_p = true;
17381       type = cp_parser_type_id (parser);
17382       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17383       /* Now, look for the trailing `)'.  */
17384       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17385       /* If all went well, then we're done.  */
17386       if (cp_parser_parse_definitely (parser))
17387         {
17388           cp_decl_specifier_seq decl_specs;
17389
17390           /* Build a trivial decl-specifier-seq.  */
17391           clear_decl_specs (&decl_specs);
17392           decl_specs.type = type;
17393
17394           /* Call grokdeclarator to figure out what type this is.  */
17395           expr = grokdeclarator (NULL,
17396                                  &decl_specs,
17397                                  TYPENAME,
17398                                  /*initialized=*/0,
17399                                  /*attrlist=*/NULL);
17400         }
17401     }
17402
17403   /* If the type-id production did not work out, then we must be
17404      looking at the unary-expression production.  */
17405   if (!expr)
17406     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17407                                        /*cast_p=*/false);
17408
17409   if (pack_expansion_p)
17410     /* Build a pack expansion. */
17411     expr = make_pack_expansion (expr);
17412
17413   /* Go back to evaluating expressions.  */
17414   --skip_evaluation;
17415
17416   /* Free the message we created.  */
17417   free (tmp);
17418   /* And restore the old one.  */
17419   parser->type_definition_forbidden_message = saved_message;
17420   parser->integral_constant_expression_p
17421     = saved_integral_constant_expression_p;
17422   parser->non_integral_constant_expression_p
17423     = saved_non_integral_constant_expression_p;
17424
17425   return expr;
17426 }
17427
17428 /* If the current declaration has no declarator, return true.  */
17429
17430 static bool
17431 cp_parser_declares_only_class_p (cp_parser *parser)
17432 {
17433   /* If the next token is a `;' or a `,' then there is no
17434      declarator.  */
17435   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17436           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17437 }
17438
17439 /* Update the DECL_SPECS to reflect the storage class indicated by
17440    KEYWORD.  */
17441
17442 static void
17443 cp_parser_set_storage_class (cp_parser *parser,
17444                              cp_decl_specifier_seq *decl_specs,
17445                              enum rid keyword)
17446 {
17447   cp_storage_class storage_class;
17448
17449   if (parser->in_unbraced_linkage_specification_p)
17450     {
17451       error ("invalid use of %qD in linkage specification",
17452              ridpointers[keyword]);
17453       return;
17454     }
17455   else if (decl_specs->storage_class != sc_none)
17456     {
17457       decl_specs->conflicting_specifiers_p = true;
17458       return;
17459     }
17460
17461   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17462       && decl_specs->specs[(int) ds_thread])
17463     {
17464       error ("%<__thread%> before %qD", ridpointers[keyword]);
17465       decl_specs->specs[(int) ds_thread] = 0;
17466     }
17467
17468   switch (keyword)
17469     {
17470     case RID_AUTO:
17471       storage_class = sc_auto;
17472       break;
17473     case RID_REGISTER:
17474       storage_class = sc_register;
17475       break;
17476     case RID_STATIC:
17477       storage_class = sc_static;
17478       break;
17479     case RID_EXTERN:
17480       storage_class = sc_extern;
17481       break;
17482     case RID_MUTABLE:
17483       storage_class = sc_mutable;
17484       break;
17485     default:
17486       gcc_unreachable ();
17487     }
17488   decl_specs->storage_class = storage_class;
17489
17490   /* A storage class specifier cannot be applied alongside a typedef 
17491      specifier. If there is a typedef specifier present then set 
17492      conflicting_specifiers_p which will trigger an error later
17493      on in grokdeclarator. */
17494   if (decl_specs->specs[(int)ds_typedef])
17495     decl_specs->conflicting_specifiers_p = true;
17496 }
17497
17498 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
17499    is true, the type is a user-defined type; otherwise it is a
17500    built-in type specified by a keyword.  */
17501
17502 static void
17503 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17504                               tree type_spec,
17505                               bool user_defined_p)
17506 {
17507   decl_specs->any_specifiers_p = true;
17508
17509   /* If the user tries to redeclare bool or wchar_t (with, for
17510      example, in "typedef int wchar_t;") we remember that this is what
17511      happened.  In system headers, we ignore these declarations so
17512      that G++ can work with system headers that are not C++-safe.  */
17513   if (decl_specs->specs[(int) ds_typedef]
17514       && !user_defined_p
17515       && (type_spec == boolean_type_node
17516           || type_spec == wchar_type_node)
17517       && (decl_specs->type
17518           || decl_specs->specs[(int) ds_long]
17519           || decl_specs->specs[(int) ds_short]
17520           || decl_specs->specs[(int) ds_unsigned]
17521           || decl_specs->specs[(int) ds_signed]))
17522     {
17523       decl_specs->redefined_builtin_type = type_spec;
17524       if (!decl_specs->type)
17525         {
17526           decl_specs->type = type_spec;
17527           decl_specs->user_defined_type_p = false;
17528         }
17529     }
17530   else if (decl_specs->type)
17531     decl_specs->multiple_types_p = true;
17532   else
17533     {
17534       decl_specs->type = type_spec;
17535       decl_specs->user_defined_type_p = user_defined_p;
17536       decl_specs->redefined_builtin_type = NULL_TREE;
17537     }
17538 }
17539
17540 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17541    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
17542
17543 static bool
17544 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17545 {
17546   return decl_specifiers->specs[(int) ds_friend] != 0;
17547 }
17548
17549 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
17550    issue an error message indicating that TOKEN_DESC was expected.
17551
17552    Returns the token consumed, if the token had the appropriate type.
17553    Otherwise, returns NULL.  */
17554
17555 static cp_token *
17556 cp_parser_require (cp_parser* parser,
17557                    enum cpp_ttype type,
17558                    const char* token_desc)
17559 {
17560   if (cp_lexer_next_token_is (parser->lexer, type))
17561     return cp_lexer_consume_token (parser->lexer);
17562   else
17563     {
17564       /* Output the MESSAGE -- unless we're parsing tentatively.  */
17565       if (!cp_parser_simulate_error (parser))
17566         {
17567           char *message = concat ("expected ", token_desc, NULL);
17568           cp_parser_error (parser, message);
17569           free (message);
17570         }
17571       return NULL;
17572     }
17573 }
17574
17575 /* An error message is produced if the next token is not '>'.
17576    All further tokens are skipped until the desired token is
17577    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
17578
17579 static void
17580 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17581 {
17582   /* Current level of '< ... >'.  */
17583   unsigned level = 0;
17584   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
17585   unsigned nesting_depth = 0;
17586
17587   /* Are we ready, yet?  If not, issue error message.  */
17588   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17589     return;
17590
17591   /* Skip tokens until the desired token is found.  */
17592   while (true)
17593     {
17594       /* Peek at the next token.  */
17595       switch (cp_lexer_peek_token (parser->lexer)->type)
17596         {
17597         case CPP_LESS:
17598           if (!nesting_depth)
17599             ++level;
17600           break;
17601
17602         case CPP_RSHIFT:
17603           if (cxx_dialect == cxx98)
17604             /* C++0x views the `>>' operator as two `>' tokens, but
17605                C++98 does not. */
17606             break;
17607           else if (!nesting_depth && level-- == 0)
17608             {
17609               /* We've hit a `>>' where the first `>' closes the
17610                  template argument list, and the second `>' is
17611                  spurious.  Just consume the `>>' and stop; we've
17612                  already produced at least one error.  */
17613               cp_lexer_consume_token (parser->lexer);
17614               return;
17615             }
17616           /* Fall through for C++0x, so we handle the second `>' in
17617              the `>>'.  */
17618
17619         case CPP_GREATER:
17620           if (!nesting_depth && level-- == 0)
17621             {
17622               /* We've reached the token we want, consume it and stop.  */
17623               cp_lexer_consume_token (parser->lexer);
17624               return;
17625             }
17626           break;
17627
17628         case CPP_OPEN_PAREN:
17629         case CPP_OPEN_SQUARE:
17630           ++nesting_depth;
17631           break;
17632
17633         case CPP_CLOSE_PAREN:
17634         case CPP_CLOSE_SQUARE:
17635           if (nesting_depth-- == 0)
17636             return;
17637           break;
17638
17639         case CPP_EOF:
17640         case CPP_PRAGMA_EOL:
17641         case CPP_SEMICOLON:
17642         case CPP_OPEN_BRACE:
17643         case CPP_CLOSE_BRACE:
17644           /* The '>' was probably forgotten, don't look further.  */
17645           return;
17646
17647         default:
17648           break;
17649         }
17650
17651       /* Consume this token.  */
17652       cp_lexer_consume_token (parser->lexer);
17653     }
17654 }
17655
17656 /* If the next token is the indicated keyword, consume it.  Otherwise,
17657    issue an error message indicating that TOKEN_DESC was expected.
17658
17659    Returns the token consumed, if the token had the appropriate type.
17660    Otherwise, returns NULL.  */
17661
17662 static cp_token *
17663 cp_parser_require_keyword (cp_parser* parser,
17664                            enum rid keyword,
17665                            const char* token_desc)
17666 {
17667   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17668
17669   if (token && token->keyword != keyword)
17670     {
17671       dyn_string_t error_msg;
17672
17673       /* Format the error message.  */
17674       error_msg = dyn_string_new (0);
17675       dyn_string_append_cstr (error_msg, "expected ");
17676       dyn_string_append_cstr (error_msg, token_desc);
17677       cp_parser_error (parser, error_msg->s);
17678       dyn_string_delete (error_msg);
17679       return NULL;
17680     }
17681
17682   return token;
17683 }
17684
17685 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17686    function-definition.  */
17687
17688 static bool
17689 cp_parser_token_starts_function_definition_p (cp_token* token)
17690 {
17691   return (/* An ordinary function-body begins with an `{'.  */
17692           token->type == CPP_OPEN_BRACE
17693           /* A ctor-initializer begins with a `:'.  */
17694           || token->type == CPP_COLON
17695           /* A function-try-block begins with `try'.  */
17696           || token->keyword == RID_TRY
17697           /* The named return value extension begins with `return'.  */
17698           || token->keyword == RID_RETURN);
17699 }
17700
17701 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17702    definition.  */
17703
17704 static bool
17705 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17706 {
17707   cp_token *token;
17708
17709   token = cp_lexer_peek_token (parser->lexer);
17710   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17711 }
17712
17713 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17714    C++0x) ending a template-argument.  */
17715
17716 static bool
17717 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17718 {
17719   cp_token *token;
17720
17721   token = cp_lexer_peek_token (parser->lexer);
17722   return (token->type == CPP_COMMA 
17723           || token->type == CPP_GREATER
17724           || token->type == CPP_ELLIPSIS
17725           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17726 }
17727
17728 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17729    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
17730
17731 static bool
17732 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17733                                                      size_t n)
17734 {
17735   cp_token *token;
17736
17737   token = cp_lexer_peek_nth_token (parser->lexer, n);
17738   if (token->type == CPP_LESS)
17739     return true;
17740   /* Check for the sequence `<::' in the original code. It would be lexed as
17741      `[:', where `[' is a digraph, and there is no whitespace before
17742      `:'.  */
17743   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17744     {
17745       cp_token *token2;
17746       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17747       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17748         return true;
17749     }
17750   return false;
17751 }
17752
17753 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17754    or none_type otherwise.  */
17755
17756 static enum tag_types
17757 cp_parser_token_is_class_key (cp_token* token)
17758 {
17759   switch (token->keyword)
17760     {
17761     case RID_CLASS:
17762       return class_type;
17763     case RID_STRUCT:
17764       return record_type;
17765     case RID_UNION:
17766       return union_type;
17767
17768     default:
17769       return none_type;
17770     }
17771 }
17772
17773 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
17774
17775 static void
17776 cp_parser_check_class_key (enum tag_types class_key, tree type)
17777 {
17778   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17779     pedwarn ("%qs tag used in naming %q#T",
17780             class_key == union_type ? "union"
17781              : class_key == record_type ? "struct" : "class",
17782              type);
17783 }
17784
17785 /* Issue an error message if DECL is redeclared with different
17786    access than its original declaration [class.access.spec/3].
17787    This applies to nested classes and nested class templates.
17788    [class.mem/1].  */
17789
17790 static void
17791 cp_parser_check_access_in_redeclaration (tree decl)
17792 {
17793   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17794     return;
17795
17796   if ((TREE_PRIVATE (decl)
17797        != (current_access_specifier == access_private_node))
17798       || (TREE_PROTECTED (decl)
17799           != (current_access_specifier == access_protected_node)))
17800     error ("%qD redeclared with different access", decl);
17801 }
17802
17803 /* Look for the `template' keyword, as a syntactic disambiguator.
17804    Return TRUE iff it is present, in which case it will be
17805    consumed.  */
17806
17807 static bool
17808 cp_parser_optional_template_keyword (cp_parser *parser)
17809 {
17810   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17811     {
17812       /* The `template' keyword can only be used within templates;
17813          outside templates the parser can always figure out what is a
17814          template and what is not.  */
17815       if (!processing_template_decl)
17816         {
17817           error ("%<template%> (as a disambiguator) is only allowed "
17818                  "within templates");
17819           /* If this part of the token stream is rescanned, the same
17820              error message would be generated.  So, we purge the token
17821              from the stream.  */
17822           cp_lexer_purge_token (parser->lexer);
17823           return false;
17824         }
17825       else
17826         {
17827           /* Consume the `template' keyword.  */
17828           cp_lexer_consume_token (parser->lexer);
17829           return true;
17830         }
17831     }
17832
17833   return false;
17834 }
17835
17836 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
17837    set PARSER->SCOPE, and perform other related actions.  */
17838
17839 static void
17840 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17841 {
17842   int i;
17843   struct tree_check *check_value;
17844   deferred_access_check *chk;
17845   VEC (deferred_access_check,gc) *checks;
17846
17847   /* Get the stored value.  */
17848   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17849   /* Perform any access checks that were deferred.  */
17850   checks = check_value->checks;
17851   if (checks)
17852     {
17853       for (i = 0 ;
17854            VEC_iterate (deferred_access_check, checks, i, chk) ;
17855            ++i)
17856         {
17857           perform_or_defer_access_check (chk->binfo,
17858                                          chk->decl,
17859                                          chk->diag_decl);
17860         }
17861     }
17862   /* Set the scope from the stored value.  */
17863   parser->scope = check_value->value;
17864   parser->qualifying_scope = check_value->qualifying_scope;
17865   parser->object_scope = NULL_TREE;
17866 }
17867
17868 /* Consume tokens up through a non-nested END token.  */
17869
17870 static void
17871 cp_parser_cache_group (cp_parser *parser,
17872                        enum cpp_ttype end,
17873                        unsigned depth)
17874 {
17875   while (true)
17876     {
17877       cp_token *token;
17878
17879       /* Abort a parenthesized expression if we encounter a brace.  */
17880       if ((end == CPP_CLOSE_PAREN || depth == 0)
17881           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17882         return;
17883       /* If we've reached the end of the file, stop.  */
17884       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17885           || (end != CPP_PRAGMA_EOL
17886               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17887         return;
17888       /* Consume the next token.  */
17889       token = cp_lexer_consume_token (parser->lexer);
17890       /* See if it starts a new group.  */
17891       if (token->type == CPP_OPEN_BRACE)
17892         {
17893           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17894           if (depth == 0)
17895             return;
17896         }
17897       else if (token->type == CPP_OPEN_PAREN)
17898         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17899       else if (token->type == CPP_PRAGMA)
17900         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17901       else if (token->type == end)
17902         return;
17903     }
17904 }
17905
17906 /* Begin parsing tentatively.  We always save tokens while parsing
17907    tentatively so that if the tentative parsing fails we can restore the
17908    tokens.  */
17909
17910 static void
17911 cp_parser_parse_tentatively (cp_parser* parser)
17912 {
17913   /* Enter a new parsing context.  */
17914   parser->context = cp_parser_context_new (parser->context);
17915   /* Begin saving tokens.  */
17916   cp_lexer_save_tokens (parser->lexer);
17917   /* In order to avoid repetitive access control error messages,
17918      access checks are queued up until we are no longer parsing
17919      tentatively.  */
17920   push_deferring_access_checks (dk_deferred);
17921 }
17922
17923 /* Commit to the currently active tentative parse.  */
17924
17925 static void
17926 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17927 {
17928   cp_parser_context *context;
17929   cp_lexer *lexer;
17930
17931   /* Mark all of the levels as committed.  */
17932   lexer = parser->lexer;
17933   for (context = parser->context; context->next; context = context->next)
17934     {
17935       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17936         break;
17937       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17938       while (!cp_lexer_saving_tokens (lexer))
17939         lexer = lexer->next;
17940       cp_lexer_commit_tokens (lexer);
17941     }
17942 }
17943
17944 /* Abort the currently active tentative parse.  All consumed tokens
17945    will be rolled back, and no diagnostics will be issued.  */
17946
17947 static void
17948 cp_parser_abort_tentative_parse (cp_parser* parser)
17949 {
17950   cp_parser_simulate_error (parser);
17951   /* Now, pretend that we want to see if the construct was
17952      successfully parsed.  */
17953   cp_parser_parse_definitely (parser);
17954 }
17955
17956 /* Stop parsing tentatively.  If a parse error has occurred, restore the
17957    token stream.  Otherwise, commit to the tokens we have consumed.
17958    Returns true if no error occurred; false otherwise.  */
17959
17960 static bool
17961 cp_parser_parse_definitely (cp_parser* parser)
17962 {
17963   bool error_occurred;
17964   cp_parser_context *context;
17965
17966   /* Remember whether or not an error occurred, since we are about to
17967      destroy that information.  */
17968   error_occurred = cp_parser_error_occurred (parser);
17969   /* Remove the topmost context from the stack.  */
17970   context = parser->context;
17971   parser->context = context->next;
17972   /* If no parse errors occurred, commit to the tentative parse.  */
17973   if (!error_occurred)
17974     {
17975       /* Commit to the tokens read tentatively, unless that was
17976          already done.  */
17977       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17978         cp_lexer_commit_tokens (parser->lexer);
17979
17980       pop_to_parent_deferring_access_checks ();
17981     }
17982   /* Otherwise, if errors occurred, roll back our state so that things
17983      are just as they were before we began the tentative parse.  */
17984   else
17985     {
17986       cp_lexer_rollback_tokens (parser->lexer);
17987       pop_deferring_access_checks ();
17988     }
17989   /* Add the context to the front of the free list.  */
17990   context->next = cp_parser_context_free_list;
17991   cp_parser_context_free_list = context;
17992
17993   return !error_occurred;
17994 }
17995
17996 /* Returns true if we are parsing tentatively and are not committed to
17997    this tentative parse.  */
17998
17999 static bool
18000 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18001 {
18002   return (cp_parser_parsing_tentatively (parser)
18003           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18004 }
18005
18006 /* Returns nonzero iff an error has occurred during the most recent
18007    tentative parse.  */
18008
18009 static bool
18010 cp_parser_error_occurred (cp_parser* parser)
18011 {
18012   return (cp_parser_parsing_tentatively (parser)
18013           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18014 }
18015
18016 /* Returns nonzero if GNU extensions are allowed.  */
18017
18018 static bool
18019 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18020 {
18021   return parser->allow_gnu_extensions_p;
18022 }
18023 \f
18024 /* Objective-C++ Productions */
18025
18026
18027 /* Parse an Objective-C expression, which feeds into a primary-expression
18028    above.
18029
18030    objc-expression:
18031      objc-message-expression
18032      objc-string-literal
18033      objc-encode-expression
18034      objc-protocol-expression
18035      objc-selector-expression
18036
18037   Returns a tree representation of the expression.  */
18038
18039 static tree
18040 cp_parser_objc_expression (cp_parser* parser)
18041 {
18042   /* Try to figure out what kind of declaration is present.  */
18043   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18044
18045   switch (kwd->type)
18046     {
18047     case CPP_OPEN_SQUARE:
18048       return cp_parser_objc_message_expression (parser);
18049
18050     case CPP_OBJC_STRING:
18051       kwd = cp_lexer_consume_token (parser->lexer);
18052       return objc_build_string_object (kwd->u.value);
18053
18054     case CPP_KEYWORD:
18055       switch (kwd->keyword)
18056         {
18057         case RID_AT_ENCODE:
18058           return cp_parser_objc_encode_expression (parser);
18059
18060         case RID_AT_PROTOCOL:
18061           return cp_parser_objc_protocol_expression (parser);
18062
18063         case RID_AT_SELECTOR:
18064           return cp_parser_objc_selector_expression (parser);
18065
18066         default:
18067           break;
18068         }
18069     default:
18070       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18071       cp_parser_skip_to_end_of_block_or_statement (parser);
18072     }
18073
18074   return error_mark_node;
18075 }
18076
18077 /* Parse an Objective-C message expression.
18078
18079    objc-message-expression:
18080      [ objc-message-receiver objc-message-args ]
18081
18082    Returns a representation of an Objective-C message.  */
18083
18084 static tree
18085 cp_parser_objc_message_expression (cp_parser* parser)
18086 {
18087   tree receiver, messageargs;
18088
18089   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18090   receiver = cp_parser_objc_message_receiver (parser);
18091   messageargs = cp_parser_objc_message_args (parser);
18092   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18093
18094   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18095 }
18096
18097 /* Parse an objc-message-receiver.
18098
18099    objc-message-receiver:
18100      expression
18101      simple-type-specifier
18102
18103   Returns a representation of the type or expression.  */
18104
18105 static tree
18106 cp_parser_objc_message_receiver (cp_parser* parser)
18107 {
18108   tree rcv;
18109
18110   /* An Objective-C message receiver may be either (1) a type
18111      or (2) an expression.  */
18112   cp_parser_parse_tentatively (parser);
18113   rcv = cp_parser_expression (parser, false);
18114
18115   if (cp_parser_parse_definitely (parser))
18116     return rcv;
18117
18118   rcv = cp_parser_simple_type_specifier (parser,
18119                                          /*decl_specs=*/NULL,
18120                                          CP_PARSER_FLAGS_NONE);
18121
18122   return objc_get_class_reference (rcv);
18123 }
18124
18125 /* Parse the arguments and selectors comprising an Objective-C message.
18126
18127    objc-message-args:
18128      objc-selector
18129      objc-selector-args
18130      objc-selector-args , objc-comma-args
18131
18132    objc-selector-args:
18133      objc-selector [opt] : assignment-expression
18134      objc-selector-args objc-selector [opt] : assignment-expression
18135
18136    objc-comma-args:
18137      assignment-expression
18138      objc-comma-args , assignment-expression
18139
18140    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18141    selector arguments and TREE_VALUE containing a list of comma
18142    arguments.  */
18143
18144 static tree
18145 cp_parser_objc_message_args (cp_parser* parser)
18146 {
18147   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18148   bool maybe_unary_selector_p = true;
18149   cp_token *token = cp_lexer_peek_token (parser->lexer);
18150
18151   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18152     {
18153       tree selector = NULL_TREE, arg;
18154
18155       if (token->type != CPP_COLON)
18156         selector = cp_parser_objc_selector (parser);
18157
18158       /* Detect if we have a unary selector.  */
18159       if (maybe_unary_selector_p
18160           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18161         return build_tree_list (selector, NULL_TREE);
18162
18163       maybe_unary_selector_p = false;
18164       cp_parser_require (parser, CPP_COLON, "`:'");
18165       arg = cp_parser_assignment_expression (parser, false);
18166
18167       sel_args
18168         = chainon (sel_args,
18169                    build_tree_list (selector, arg));
18170
18171       token = cp_lexer_peek_token (parser->lexer);
18172     }
18173
18174   /* Handle non-selector arguments, if any. */
18175   while (token->type == CPP_COMMA)
18176     {
18177       tree arg;
18178
18179       cp_lexer_consume_token (parser->lexer);
18180       arg = cp_parser_assignment_expression (parser, false);
18181
18182       addl_args
18183         = chainon (addl_args,
18184                    build_tree_list (NULL_TREE, arg));
18185
18186       token = cp_lexer_peek_token (parser->lexer);
18187     }
18188
18189   return build_tree_list (sel_args, addl_args);
18190 }
18191
18192 /* Parse an Objective-C encode expression.
18193
18194    objc-encode-expression:
18195      @encode objc-typename
18196
18197    Returns an encoded representation of the type argument.  */
18198
18199 static tree
18200 cp_parser_objc_encode_expression (cp_parser* parser)
18201 {
18202   tree type;
18203
18204   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
18205   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18206   type = complete_type (cp_parser_type_id (parser));
18207   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18208
18209   if (!type)
18210     {
18211       error ("%<@encode%> must specify a type as an argument");
18212       return error_mark_node;
18213     }
18214
18215   return objc_build_encode_expr (type);
18216 }
18217
18218 /* Parse an Objective-C @defs expression.  */
18219
18220 static tree
18221 cp_parser_objc_defs_expression (cp_parser *parser)
18222 {
18223   tree name;
18224
18225   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
18226   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18227   name = cp_parser_identifier (parser);
18228   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18229
18230   return objc_get_class_ivars (name);
18231 }
18232
18233 /* Parse an Objective-C protocol expression.
18234
18235   objc-protocol-expression:
18236     @protocol ( identifier )
18237
18238   Returns a representation of the protocol expression.  */
18239
18240 static tree
18241 cp_parser_objc_protocol_expression (cp_parser* parser)
18242 {
18243   tree proto;
18244
18245   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18246   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18247   proto = cp_parser_identifier (parser);
18248   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18249
18250   return objc_build_protocol_expr (proto);
18251 }
18252
18253 /* Parse an Objective-C selector expression.
18254
18255    objc-selector-expression:
18256      @selector ( objc-method-signature )
18257
18258    objc-method-signature:
18259      objc-selector
18260      objc-selector-seq
18261
18262    objc-selector-seq:
18263      objc-selector :
18264      objc-selector-seq objc-selector :
18265
18266   Returns a representation of the method selector.  */
18267
18268 static tree
18269 cp_parser_objc_selector_expression (cp_parser* parser)
18270 {
18271   tree sel_seq = NULL_TREE;
18272   bool maybe_unary_selector_p = true;
18273   cp_token *token;
18274
18275   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
18276   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18277   token = cp_lexer_peek_token (parser->lexer);
18278
18279   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18280          || token->type == CPP_SCOPE)
18281     {
18282       tree selector = NULL_TREE;
18283
18284       if (token->type != CPP_COLON
18285           || token->type == CPP_SCOPE)
18286         selector = cp_parser_objc_selector (parser);
18287
18288       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18289           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18290         {
18291           /* Detect if we have a unary selector.  */
18292           if (maybe_unary_selector_p)
18293             {
18294               sel_seq = selector;
18295               goto finish_selector;
18296             }
18297           else
18298             {
18299               cp_parser_error (parser, "expected %<:%>");
18300             }
18301         }
18302       maybe_unary_selector_p = false;
18303       token = cp_lexer_consume_token (parser->lexer);
18304
18305       if (token->type == CPP_SCOPE)
18306         {
18307           sel_seq
18308             = chainon (sel_seq,
18309                        build_tree_list (selector, NULL_TREE));
18310           sel_seq
18311             = chainon (sel_seq,
18312                        build_tree_list (NULL_TREE, NULL_TREE));
18313         }
18314       else
18315         sel_seq
18316           = chainon (sel_seq,
18317                      build_tree_list (selector, NULL_TREE));
18318
18319       token = cp_lexer_peek_token (parser->lexer);
18320     }
18321
18322  finish_selector:
18323   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18324
18325   return objc_build_selector_expr (sel_seq);
18326 }
18327
18328 /* Parse a list of identifiers.
18329
18330    objc-identifier-list:
18331      identifier
18332      objc-identifier-list , identifier
18333
18334    Returns a TREE_LIST of identifier nodes.  */
18335
18336 static tree
18337 cp_parser_objc_identifier_list (cp_parser* parser)
18338 {
18339   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18340   cp_token *sep = cp_lexer_peek_token (parser->lexer);
18341
18342   while (sep->type == CPP_COMMA)
18343     {
18344       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18345       list = chainon (list,
18346                       build_tree_list (NULL_TREE,
18347                                        cp_parser_identifier (parser)));
18348       sep = cp_lexer_peek_token (parser->lexer);
18349     }
18350
18351   return list;
18352 }
18353
18354 /* Parse an Objective-C alias declaration.
18355
18356    objc-alias-declaration:
18357      @compatibility_alias identifier identifier ;
18358
18359    This function registers the alias mapping with the Objective-C front end.
18360    It returns nothing.  */
18361
18362 static void
18363 cp_parser_objc_alias_declaration (cp_parser* parser)
18364 {
18365   tree alias, orig;
18366
18367   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
18368   alias = cp_parser_identifier (parser);
18369   orig = cp_parser_identifier (parser);
18370   objc_declare_alias (alias, orig);
18371   cp_parser_consume_semicolon_at_end_of_statement (parser);
18372 }
18373
18374 /* Parse an Objective-C class forward-declaration.
18375
18376    objc-class-declaration:
18377      @class objc-identifier-list ;
18378
18379    The function registers the forward declarations with the Objective-C
18380    front end.  It returns nothing.  */
18381
18382 static void
18383 cp_parser_objc_class_declaration (cp_parser* parser)
18384 {
18385   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
18386   objc_declare_class (cp_parser_objc_identifier_list (parser));
18387   cp_parser_consume_semicolon_at_end_of_statement (parser);
18388 }
18389
18390 /* Parse a list of Objective-C protocol references.
18391
18392    objc-protocol-refs-opt:
18393      objc-protocol-refs [opt]
18394
18395    objc-protocol-refs:
18396      < objc-identifier-list >
18397
18398    Returns a TREE_LIST of identifiers, if any.  */
18399
18400 static tree
18401 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18402 {
18403   tree protorefs = NULL_TREE;
18404
18405   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18406     {
18407       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
18408       protorefs = cp_parser_objc_identifier_list (parser);
18409       cp_parser_require (parser, CPP_GREATER, "`>'");
18410     }
18411
18412   return protorefs;
18413 }
18414
18415 /* Parse a Objective-C visibility specification.  */
18416
18417 static void
18418 cp_parser_objc_visibility_spec (cp_parser* parser)
18419 {
18420   cp_token *vis = cp_lexer_peek_token (parser->lexer);
18421
18422   switch (vis->keyword)
18423     {
18424     case RID_AT_PRIVATE:
18425       objc_set_visibility (2);
18426       break;
18427     case RID_AT_PROTECTED:
18428       objc_set_visibility (0);
18429       break;
18430     case RID_AT_PUBLIC:
18431       objc_set_visibility (1);
18432       break;
18433     default:
18434       return;
18435     }
18436
18437   /* Eat '@private'/'@protected'/'@public'.  */
18438   cp_lexer_consume_token (parser->lexer);
18439 }
18440
18441 /* Parse an Objective-C method type.  */
18442
18443 static void
18444 cp_parser_objc_method_type (cp_parser* parser)
18445 {
18446   objc_set_method_type
18447    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18448     ? PLUS_EXPR
18449     : MINUS_EXPR);
18450 }
18451
18452 /* Parse an Objective-C protocol qualifier.  */
18453
18454 static tree
18455 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18456 {
18457   tree quals = NULL_TREE, node;
18458   cp_token *token = cp_lexer_peek_token (parser->lexer);
18459
18460   node = token->u.value;
18461
18462   while (node && TREE_CODE (node) == IDENTIFIER_NODE
18463          && (node == ridpointers [(int) RID_IN]
18464              || node == ridpointers [(int) RID_OUT]
18465              || node == ridpointers [(int) RID_INOUT]
18466              || node == ridpointers [(int) RID_BYCOPY]
18467              || node == ridpointers [(int) RID_BYREF]
18468              || node == ridpointers [(int) RID_ONEWAY]))
18469     {
18470       quals = tree_cons (NULL_TREE, node, quals);
18471       cp_lexer_consume_token (parser->lexer);
18472       token = cp_lexer_peek_token (parser->lexer);
18473       node = token->u.value;
18474     }
18475
18476   return quals;
18477 }
18478
18479 /* Parse an Objective-C typename.  */
18480
18481 static tree
18482 cp_parser_objc_typename (cp_parser* parser)
18483 {
18484   tree typename = NULL_TREE;
18485
18486   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18487     {
18488       tree proto_quals, cp_type = NULL_TREE;
18489
18490       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18491       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18492
18493       /* An ObjC type name may consist of just protocol qualifiers, in which
18494          case the type shall default to 'id'.  */
18495       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18496         cp_type = cp_parser_type_id (parser);
18497
18498       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18499       typename = build_tree_list (proto_quals, cp_type);
18500     }
18501
18502   return typename;
18503 }
18504
18505 /* Check to see if TYPE refers to an Objective-C selector name.  */
18506
18507 static bool
18508 cp_parser_objc_selector_p (enum cpp_ttype type)
18509 {
18510   return (type == CPP_NAME || type == CPP_KEYWORD
18511           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18512           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18513           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18514           || type == CPP_XOR || type == CPP_XOR_EQ);
18515 }
18516
18517 /* Parse an Objective-C selector.  */
18518
18519 static tree
18520 cp_parser_objc_selector (cp_parser* parser)
18521 {
18522   cp_token *token = cp_lexer_consume_token (parser->lexer);
18523
18524   if (!cp_parser_objc_selector_p (token->type))
18525     {
18526       error ("invalid Objective-C++ selector name");
18527       return error_mark_node;
18528     }
18529
18530   /* C++ operator names are allowed to appear in ObjC selectors.  */
18531   switch (token->type)
18532     {
18533     case CPP_AND_AND: return get_identifier ("and");
18534     case CPP_AND_EQ: return get_identifier ("and_eq");
18535     case CPP_AND: return get_identifier ("bitand");
18536     case CPP_OR: return get_identifier ("bitor");
18537     case CPP_COMPL: return get_identifier ("compl");
18538     case CPP_NOT: return get_identifier ("not");
18539     case CPP_NOT_EQ: return get_identifier ("not_eq");
18540     case CPP_OR_OR: return get_identifier ("or");
18541     case CPP_OR_EQ: return get_identifier ("or_eq");
18542     case CPP_XOR: return get_identifier ("xor");
18543     case CPP_XOR_EQ: return get_identifier ("xor_eq");
18544     default: return token->u.value;
18545     }
18546 }
18547
18548 /* Parse an Objective-C params list.  */
18549
18550 static tree
18551 cp_parser_objc_method_keyword_params (cp_parser* parser)
18552 {
18553   tree params = NULL_TREE;
18554   bool maybe_unary_selector_p = true;
18555   cp_token *token = cp_lexer_peek_token (parser->lexer);
18556
18557   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18558     {
18559       tree selector = NULL_TREE, typename, identifier;
18560
18561       if (token->type != CPP_COLON)
18562         selector = cp_parser_objc_selector (parser);
18563
18564       /* Detect if we have a unary selector.  */
18565       if (maybe_unary_selector_p
18566           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18567         return selector;
18568
18569       maybe_unary_selector_p = false;
18570       cp_parser_require (parser, CPP_COLON, "`:'");
18571       typename = cp_parser_objc_typename (parser);
18572       identifier = cp_parser_identifier (parser);
18573
18574       params
18575         = chainon (params,
18576                    objc_build_keyword_decl (selector,
18577                                             typename,
18578                                             identifier));
18579
18580       token = cp_lexer_peek_token (parser->lexer);
18581     }
18582
18583   return params;
18584 }
18585
18586 /* Parse the non-keyword Objective-C params.  */
18587
18588 static tree
18589 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18590 {
18591   tree params = make_node (TREE_LIST);
18592   cp_token *token = cp_lexer_peek_token (parser->lexer);
18593   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
18594
18595   while (token->type == CPP_COMMA)
18596     {
18597       cp_parameter_declarator *parmdecl;
18598       tree parm;
18599
18600       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18601       token = cp_lexer_peek_token (parser->lexer);
18602
18603       if (token->type == CPP_ELLIPSIS)
18604         {
18605           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
18606           *ellipsisp = true;
18607           break;
18608         }
18609
18610       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18611       parm = grokdeclarator (parmdecl->declarator,
18612                              &parmdecl->decl_specifiers,
18613                              PARM, /*initialized=*/0,
18614                              /*attrlist=*/NULL);
18615
18616       chainon (params, build_tree_list (NULL_TREE, parm));
18617       token = cp_lexer_peek_token (parser->lexer);
18618     }
18619
18620   return params;
18621 }
18622
18623 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
18624
18625 static void
18626 cp_parser_objc_interstitial_code (cp_parser* parser)
18627 {
18628   cp_token *token = cp_lexer_peek_token (parser->lexer);
18629
18630   /* If the next token is `extern' and the following token is a string
18631      literal, then we have a linkage specification.  */
18632   if (token->keyword == RID_EXTERN
18633       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18634     cp_parser_linkage_specification (parser);
18635   /* Handle #pragma, if any.  */
18636   else if (token->type == CPP_PRAGMA)
18637     cp_parser_pragma (parser, pragma_external);
18638   /* Allow stray semicolons.  */
18639   else if (token->type == CPP_SEMICOLON)
18640     cp_lexer_consume_token (parser->lexer);
18641   /* Finally, try to parse a block-declaration, or a function-definition.  */
18642   else
18643     cp_parser_block_declaration (parser, /*statement_p=*/false);
18644 }
18645
18646 /* Parse a method signature.  */
18647
18648 static tree
18649 cp_parser_objc_method_signature (cp_parser* parser)
18650 {
18651   tree rettype, kwdparms, optparms;
18652   bool ellipsis = false;
18653
18654   cp_parser_objc_method_type (parser);
18655   rettype = cp_parser_objc_typename (parser);
18656   kwdparms = cp_parser_objc_method_keyword_params (parser);
18657   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18658
18659   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18660 }
18661
18662 /* Pars an Objective-C method prototype list.  */
18663
18664 static void
18665 cp_parser_objc_method_prototype_list (cp_parser* parser)
18666 {
18667   cp_token *token = cp_lexer_peek_token (parser->lexer);
18668
18669   while (token->keyword != RID_AT_END)
18670     {
18671       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18672         {
18673           objc_add_method_declaration
18674            (cp_parser_objc_method_signature (parser));
18675           cp_parser_consume_semicolon_at_end_of_statement (parser);
18676         }
18677       else
18678         /* Allow for interspersed non-ObjC++ code.  */
18679         cp_parser_objc_interstitial_code (parser);
18680
18681       token = cp_lexer_peek_token (parser->lexer);
18682     }
18683
18684   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18685   objc_finish_interface ();
18686 }
18687
18688 /* Parse an Objective-C method definition list.  */
18689
18690 static void
18691 cp_parser_objc_method_definition_list (cp_parser* parser)
18692 {
18693   cp_token *token = cp_lexer_peek_token (parser->lexer);
18694
18695   while (token->keyword != RID_AT_END)
18696     {
18697       tree meth;
18698
18699       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18700         {
18701           push_deferring_access_checks (dk_deferred);
18702           objc_start_method_definition
18703            (cp_parser_objc_method_signature (parser));
18704
18705           /* For historical reasons, we accept an optional semicolon.  */
18706           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18707             cp_lexer_consume_token (parser->lexer);
18708
18709           perform_deferred_access_checks ();
18710           stop_deferring_access_checks ();
18711           meth = cp_parser_function_definition_after_declarator (parser,
18712                                                                  false);
18713           pop_deferring_access_checks ();
18714           objc_finish_method_definition (meth);
18715         }
18716       else
18717         /* Allow for interspersed non-ObjC++ code.  */
18718         cp_parser_objc_interstitial_code (parser);
18719
18720       token = cp_lexer_peek_token (parser->lexer);
18721     }
18722
18723   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18724   objc_finish_implementation ();
18725 }
18726
18727 /* Parse Objective-C ivars.  */
18728
18729 static void
18730 cp_parser_objc_class_ivars (cp_parser* parser)
18731 {
18732   cp_token *token = cp_lexer_peek_token (parser->lexer);
18733
18734   if (token->type != CPP_OPEN_BRACE)
18735     return;     /* No ivars specified.  */
18736
18737   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
18738   token = cp_lexer_peek_token (parser->lexer);
18739
18740   while (token->type != CPP_CLOSE_BRACE)
18741     {
18742       cp_decl_specifier_seq declspecs;
18743       int decl_class_or_enum_p;
18744       tree prefix_attributes;
18745
18746       cp_parser_objc_visibility_spec (parser);
18747
18748       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18749         break;
18750
18751       cp_parser_decl_specifier_seq (parser,
18752                                     CP_PARSER_FLAGS_OPTIONAL,
18753                                     &declspecs,
18754                                     &decl_class_or_enum_p);
18755       prefix_attributes = declspecs.attributes;
18756       declspecs.attributes = NULL_TREE;
18757
18758       /* Keep going until we hit the `;' at the end of the
18759          declaration.  */
18760       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18761         {
18762           tree width = NULL_TREE, attributes, first_attribute, decl;
18763           cp_declarator *declarator = NULL;
18764           int ctor_dtor_or_conv_p;
18765
18766           /* Check for a (possibly unnamed) bitfield declaration.  */
18767           token = cp_lexer_peek_token (parser->lexer);
18768           if (token->type == CPP_COLON)
18769             goto eat_colon;
18770
18771           if (token->type == CPP_NAME
18772               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18773                   == CPP_COLON))
18774             {
18775               /* Get the name of the bitfield.  */
18776               declarator = make_id_declarator (NULL_TREE,
18777                                                cp_parser_identifier (parser),
18778                                                sfk_none);
18779
18780              eat_colon:
18781               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18782               /* Get the width of the bitfield.  */
18783               width
18784                 = cp_parser_constant_expression (parser,
18785                                                  /*allow_non_constant=*/false,
18786                                                  NULL);
18787             }
18788           else
18789             {
18790               /* Parse the declarator.  */
18791               declarator
18792                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18793                                         &ctor_dtor_or_conv_p,
18794                                         /*parenthesized_p=*/NULL,
18795                                         /*member_p=*/false);
18796             }
18797
18798           /* Look for attributes that apply to the ivar.  */
18799           attributes = cp_parser_attributes_opt (parser);
18800           /* Remember which attributes are prefix attributes and
18801              which are not.  */
18802           first_attribute = attributes;
18803           /* Combine the attributes.  */
18804           attributes = chainon (prefix_attributes, attributes);
18805
18806           if (width)
18807             {
18808               /* Create the bitfield declaration.  */
18809               decl = grokbitfield (declarator, &declspecs, width);
18810               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18811             }
18812           else
18813             decl = grokfield (declarator, &declspecs,
18814                               NULL_TREE, /*init_const_expr_p=*/false,
18815                               NULL_TREE, attributes);
18816
18817           /* Add the instance variable.  */
18818           objc_add_instance_variable (decl);
18819
18820           /* Reset PREFIX_ATTRIBUTES.  */
18821           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18822             attributes = TREE_CHAIN (attributes);
18823           if (attributes)
18824             TREE_CHAIN (attributes) = NULL_TREE;
18825
18826           token = cp_lexer_peek_token (parser->lexer);
18827
18828           if (token->type == CPP_COMMA)
18829             {
18830               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18831               continue;
18832             }
18833           break;
18834         }
18835
18836       cp_parser_consume_semicolon_at_end_of_statement (parser);
18837       token = cp_lexer_peek_token (parser->lexer);
18838     }
18839
18840   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
18841   /* For historical reasons, we accept an optional semicolon.  */
18842   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18843     cp_lexer_consume_token (parser->lexer);
18844 }
18845
18846 /* Parse an Objective-C protocol declaration.  */
18847
18848 static void
18849 cp_parser_objc_protocol_declaration (cp_parser* parser)
18850 {
18851   tree proto, protorefs;
18852   cp_token *tok;
18853
18854   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18855   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18856     {
18857       error ("identifier expected after %<@protocol%>");
18858       goto finish;
18859     }
18860
18861   /* See if we have a forward declaration or a definition.  */
18862   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18863
18864   /* Try a forward declaration first.  */
18865   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18866     {
18867       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18868      finish:
18869       cp_parser_consume_semicolon_at_end_of_statement (parser);
18870     }
18871
18872   /* Ok, we got a full-fledged definition (or at least should).  */
18873   else
18874     {
18875       proto = cp_parser_identifier (parser);
18876       protorefs = cp_parser_objc_protocol_refs_opt (parser);
18877       objc_start_protocol (proto, protorefs);
18878       cp_parser_objc_method_prototype_list (parser);
18879     }
18880 }
18881
18882 /* Parse an Objective-C superclass or category.  */
18883
18884 static void
18885 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18886                                                           tree *categ)
18887 {
18888   cp_token *next = cp_lexer_peek_token (parser->lexer);
18889
18890   *super = *categ = NULL_TREE;
18891   if (next->type == CPP_COLON)
18892     {
18893       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18894       *super = cp_parser_identifier (parser);
18895     }
18896   else if (next->type == CPP_OPEN_PAREN)
18897     {
18898       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18899       *categ = cp_parser_identifier (parser);
18900       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18901     }
18902 }
18903
18904 /* Parse an Objective-C class interface.  */
18905
18906 static void
18907 cp_parser_objc_class_interface (cp_parser* parser)
18908 {
18909   tree name, super, categ, protos;
18910
18911   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
18912   name = cp_parser_identifier (parser);
18913   cp_parser_objc_superclass_or_category (parser, &super, &categ);
18914   protos = cp_parser_objc_protocol_refs_opt (parser);
18915
18916   /* We have either a class or a category on our hands.  */
18917   if (categ)
18918     objc_start_category_interface (name, categ, protos);
18919   else
18920     {
18921       objc_start_class_interface (name, super, protos);
18922       /* Handle instance variable declarations, if any.  */
18923       cp_parser_objc_class_ivars (parser);
18924       objc_continue_interface ();
18925     }
18926
18927   cp_parser_objc_method_prototype_list (parser);
18928 }
18929
18930 /* Parse an Objective-C class implementation.  */
18931
18932 static void
18933 cp_parser_objc_class_implementation (cp_parser* parser)
18934 {
18935   tree name, super, categ;
18936
18937   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
18938   name = cp_parser_identifier (parser);
18939   cp_parser_objc_superclass_or_category (parser, &super, &categ);
18940
18941   /* We have either a class or a category on our hands.  */
18942   if (categ)
18943     objc_start_category_implementation (name, categ);
18944   else
18945     {
18946       objc_start_class_implementation (name, super);
18947       /* Handle instance variable declarations, if any.  */
18948       cp_parser_objc_class_ivars (parser);
18949       objc_continue_implementation ();
18950     }
18951
18952   cp_parser_objc_method_definition_list (parser);
18953 }
18954
18955 /* Consume the @end token and finish off the implementation.  */
18956
18957 static void
18958 cp_parser_objc_end_implementation (cp_parser* parser)
18959 {
18960   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18961   objc_finish_implementation ();
18962 }
18963
18964 /* Parse an Objective-C declaration.  */
18965
18966 static void
18967 cp_parser_objc_declaration (cp_parser* parser)
18968 {
18969   /* Try to figure out what kind of declaration is present.  */
18970   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18971
18972   switch (kwd->keyword)
18973     {
18974     case RID_AT_ALIAS:
18975       cp_parser_objc_alias_declaration (parser);
18976       break;
18977     case RID_AT_CLASS:
18978       cp_parser_objc_class_declaration (parser);
18979       break;
18980     case RID_AT_PROTOCOL:
18981       cp_parser_objc_protocol_declaration (parser);
18982       break;
18983     case RID_AT_INTERFACE:
18984       cp_parser_objc_class_interface (parser);
18985       break;
18986     case RID_AT_IMPLEMENTATION:
18987       cp_parser_objc_class_implementation (parser);
18988       break;
18989     case RID_AT_END:
18990       cp_parser_objc_end_implementation (parser);
18991       break;
18992     default:
18993       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18994       cp_parser_skip_to_end_of_block_or_statement (parser);
18995     }
18996 }
18997
18998 /* Parse an Objective-C try-catch-finally statement.
18999
19000    objc-try-catch-finally-stmt:
19001      @try compound-statement objc-catch-clause-seq [opt]
19002        objc-finally-clause [opt]
19003
19004    objc-catch-clause-seq:
19005      objc-catch-clause objc-catch-clause-seq [opt]
19006
19007    objc-catch-clause:
19008      @catch ( exception-declaration ) compound-statement
19009
19010    objc-finally-clause
19011      @finally compound-statement
19012
19013    Returns NULL_TREE.  */
19014
19015 static tree
19016 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19017   location_t location;
19018   tree stmt;
19019
19020   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19021   location = cp_lexer_peek_token (parser->lexer)->location;
19022   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19023      node, lest it get absorbed into the surrounding block.  */
19024   stmt = push_stmt_list ();
19025   cp_parser_compound_statement (parser, NULL, false);
19026   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19027
19028   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19029     {
19030       cp_parameter_declarator *parmdecl;
19031       tree parm;
19032
19033       cp_lexer_consume_token (parser->lexer);
19034       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19035       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19036       parm = grokdeclarator (parmdecl->declarator,
19037                              &parmdecl->decl_specifiers,
19038                              PARM, /*initialized=*/0,
19039                              /*attrlist=*/NULL);
19040       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19041       objc_begin_catch_clause (parm);
19042       cp_parser_compound_statement (parser, NULL, false);
19043       objc_finish_catch_clause ();
19044     }
19045
19046   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19047     {
19048       cp_lexer_consume_token (parser->lexer);
19049       location = cp_lexer_peek_token (parser->lexer)->location;
19050       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19051          node, lest it get absorbed into the surrounding block.  */
19052       stmt = push_stmt_list ();
19053       cp_parser_compound_statement (parser, NULL, false);
19054       objc_build_finally_clause (location, pop_stmt_list (stmt));
19055     }
19056
19057   return objc_finish_try_stmt ();
19058 }
19059
19060 /* Parse an Objective-C synchronized statement.
19061
19062    objc-synchronized-stmt:
19063      @synchronized ( expression ) compound-statement
19064
19065    Returns NULL_TREE.  */
19066
19067 static tree
19068 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19069   location_t location;
19070   tree lock, stmt;
19071
19072   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19073
19074   location = cp_lexer_peek_token (parser->lexer)->location;
19075   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19076   lock = cp_parser_expression (parser, false);
19077   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19078
19079   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19080      node, lest it get absorbed into the surrounding block.  */
19081   stmt = push_stmt_list ();
19082   cp_parser_compound_statement (parser, NULL, false);
19083
19084   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19085 }
19086
19087 /* Parse an Objective-C throw statement.
19088
19089    objc-throw-stmt:
19090      @throw assignment-expression [opt] ;
19091
19092    Returns a constructed '@throw' statement.  */
19093
19094 static tree
19095 cp_parser_objc_throw_statement (cp_parser *parser) {
19096   tree expr = NULL_TREE;
19097
19098   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19099
19100   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19101     expr = cp_parser_assignment_expression (parser, false);
19102
19103   cp_parser_consume_semicolon_at_end_of_statement (parser);
19104
19105   return objc_build_throw_stmt (expr);
19106 }
19107
19108 /* Parse an Objective-C statement.  */
19109
19110 static tree
19111 cp_parser_objc_statement (cp_parser * parser) {
19112   /* Try to figure out what kind of declaration is present.  */
19113   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19114
19115   switch (kwd->keyword)
19116     {
19117     case RID_AT_TRY:
19118       return cp_parser_objc_try_catch_finally_statement (parser);
19119     case RID_AT_SYNCHRONIZED:
19120       return cp_parser_objc_synchronized_statement (parser);
19121     case RID_AT_THROW:
19122       return cp_parser_objc_throw_statement (parser);
19123     default:
19124       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19125       cp_parser_skip_to_end_of_block_or_statement (parser);
19126     }
19127
19128   return error_mark_node;
19129 }
19130 \f
19131 /* OpenMP 2.5 parsing routines.  */
19132
19133 /* Returns name of the next clause.
19134    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19135    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19136    returned and the token is consumed.  */
19137
19138 static pragma_omp_clause
19139 cp_parser_omp_clause_name (cp_parser *parser)
19140 {
19141   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19142
19143   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19144     result = PRAGMA_OMP_CLAUSE_IF;
19145   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19146     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19147   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19148     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19149   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19150     {
19151       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19152       const char *p = IDENTIFIER_POINTER (id);
19153
19154       switch (p[0])
19155         {
19156         case 'c':
19157           if (!strcmp ("copyin", p))
19158             result = PRAGMA_OMP_CLAUSE_COPYIN;
19159           else if (!strcmp ("copyprivate", p))
19160             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19161           break;
19162         case 'f':
19163           if (!strcmp ("firstprivate", p))
19164             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19165           break;
19166         case 'l':
19167           if (!strcmp ("lastprivate", p))
19168             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19169           break;
19170         case 'n':
19171           if (!strcmp ("nowait", p))
19172             result = PRAGMA_OMP_CLAUSE_NOWAIT;
19173           else if (!strcmp ("num_threads", p))
19174             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19175           break;
19176         case 'o':
19177           if (!strcmp ("ordered", p))
19178             result = PRAGMA_OMP_CLAUSE_ORDERED;
19179           break;
19180         case 'r':
19181           if (!strcmp ("reduction", p))
19182             result = PRAGMA_OMP_CLAUSE_REDUCTION;
19183           break;
19184         case 's':
19185           if (!strcmp ("schedule", p))
19186             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19187           else if (!strcmp ("shared", p))
19188             result = PRAGMA_OMP_CLAUSE_SHARED;
19189           break;
19190         }
19191     }
19192
19193   if (result != PRAGMA_OMP_CLAUSE_NONE)
19194     cp_lexer_consume_token (parser->lexer);
19195
19196   return result;
19197 }
19198
19199 /* Validate that a clause of the given type does not already exist.  */
19200
19201 static void
19202 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19203 {
19204   tree c;
19205
19206   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19207     if (OMP_CLAUSE_CODE (c) == code)
19208       {
19209         error ("too many %qs clauses", name);
19210         break;
19211       }
19212 }
19213
19214 /* OpenMP 2.5:
19215    variable-list:
19216      identifier
19217      variable-list , identifier
19218
19219    In addition, we match a closing parenthesis.  An opening parenthesis
19220    will have been consumed by the caller.
19221
19222    If KIND is nonzero, create the appropriate node and install the decl
19223    in OMP_CLAUSE_DECL and add the node to the head of the list.
19224
19225    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19226    return the list created.  */
19227
19228 static tree
19229 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19230                                 tree list)
19231 {
19232   while (1)
19233     {
19234       tree name, decl;
19235
19236       name = cp_parser_id_expression (parser, /*template_p=*/false,
19237                                       /*check_dependency_p=*/true,
19238                                       /*template_p=*/NULL,
19239                                       /*declarator_p=*/false,
19240                                       /*optional_p=*/false);
19241       if (name == error_mark_node)
19242         goto skip_comma;
19243
19244       decl = cp_parser_lookup_name_simple (parser, name);
19245       if (decl == error_mark_node)
19246         cp_parser_name_lookup_error (parser, name, decl, NULL);
19247       else if (kind != 0)
19248         {
19249           tree u = build_omp_clause (kind);
19250           OMP_CLAUSE_DECL (u) = decl;
19251           OMP_CLAUSE_CHAIN (u) = list;
19252           list = u;
19253         }
19254       else
19255         list = tree_cons (decl, NULL_TREE, list);
19256
19257     get_comma:
19258       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19259         break;
19260       cp_lexer_consume_token (parser->lexer);
19261     }
19262
19263   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19264     {
19265       int ending;
19266
19267       /* Try to resync to an unnested comma.  Copied from
19268          cp_parser_parenthesized_expression_list.  */
19269     skip_comma:
19270       ending = cp_parser_skip_to_closing_parenthesis (parser,
19271                                                       /*recovering=*/true,
19272                                                       /*or_comma=*/true,
19273                                                       /*consume_paren=*/true);
19274       if (ending < 0)
19275         goto get_comma;
19276     }
19277
19278   return list;
19279 }
19280
19281 /* Similarly, but expect leading and trailing parenthesis.  This is a very
19282    common case for omp clauses.  */
19283
19284 static tree
19285 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19286 {
19287   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19288     return cp_parser_omp_var_list_no_open (parser, kind, list);
19289   return list;
19290 }
19291
19292 /* OpenMP 2.5:
19293    default ( shared | none ) */
19294
19295 static tree
19296 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19297 {
19298   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19299   tree c;
19300
19301   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19302     return list;
19303   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19304     {
19305       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19306       const char *p = IDENTIFIER_POINTER (id);
19307
19308       switch (p[0])
19309         {
19310         case 'n':
19311           if (strcmp ("none", p) != 0)
19312             goto invalid_kind;
19313           kind = OMP_CLAUSE_DEFAULT_NONE;
19314           break;
19315
19316         case 's':
19317           if (strcmp ("shared", p) != 0)
19318             goto invalid_kind;
19319           kind = OMP_CLAUSE_DEFAULT_SHARED;
19320           break;
19321
19322         default:
19323           goto invalid_kind;
19324         }
19325
19326       cp_lexer_consume_token (parser->lexer);
19327     }
19328   else
19329     {
19330     invalid_kind:
19331       cp_parser_error (parser, "expected %<none%> or %<shared%>");
19332     }
19333
19334   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19335     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19336                                            /*or_comma=*/false,
19337                                            /*consume_paren=*/true);
19338
19339   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19340     return list;
19341
19342   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19343   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19344   OMP_CLAUSE_CHAIN (c) = list;
19345   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19346
19347   return c;
19348 }
19349
19350 /* OpenMP 2.5:
19351    if ( expression ) */
19352
19353 static tree
19354 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19355 {
19356   tree t, c;
19357
19358   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19359     return list;
19360
19361   t = cp_parser_condition (parser);
19362
19363   if (t == error_mark_node
19364       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19365     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19366                                            /*or_comma=*/false,
19367                                            /*consume_paren=*/true);
19368
19369   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19370
19371   c = build_omp_clause (OMP_CLAUSE_IF);
19372   OMP_CLAUSE_IF_EXPR (c) = t;
19373   OMP_CLAUSE_CHAIN (c) = list;
19374
19375   return c;
19376 }
19377
19378 /* OpenMP 2.5:
19379    nowait */
19380
19381 static tree
19382 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19383 {
19384   tree c;
19385
19386   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19387
19388   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19389   OMP_CLAUSE_CHAIN (c) = list;
19390   return c;
19391 }
19392
19393 /* OpenMP 2.5:
19394    num_threads ( expression ) */
19395
19396 static tree
19397 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19398 {
19399   tree t, c;
19400
19401   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19402     return list;
19403
19404   t = cp_parser_expression (parser, false);
19405
19406   if (t == error_mark_node
19407       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19408     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19409                                            /*or_comma=*/false,
19410                                            /*consume_paren=*/true);
19411
19412   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19413
19414   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19415   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19416   OMP_CLAUSE_CHAIN (c) = list;
19417
19418   return c;
19419 }
19420
19421 /* OpenMP 2.5:
19422    ordered */
19423
19424 static tree
19425 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19426 {
19427   tree c;
19428
19429   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19430
19431   c = build_omp_clause (OMP_CLAUSE_ORDERED);
19432   OMP_CLAUSE_CHAIN (c) = list;
19433   return c;
19434 }
19435
19436 /* OpenMP 2.5:
19437    reduction ( reduction-operator : variable-list )
19438
19439    reduction-operator:
19440      One of: + * - & ^ | && || */
19441
19442 static tree
19443 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19444 {
19445   enum tree_code code;
19446   tree nlist, c;
19447
19448   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19449     return list;
19450
19451   switch (cp_lexer_peek_token (parser->lexer)->type)
19452     {
19453     case CPP_PLUS:
19454       code = PLUS_EXPR;
19455       break;
19456     case CPP_MULT:
19457       code = MULT_EXPR;
19458       break;
19459     case CPP_MINUS:
19460       code = MINUS_EXPR;
19461       break;
19462     case CPP_AND:
19463       code = BIT_AND_EXPR;
19464       break;
19465     case CPP_XOR:
19466       code = BIT_XOR_EXPR;
19467       break;
19468     case CPP_OR:
19469       code = BIT_IOR_EXPR;
19470       break;
19471     case CPP_AND_AND:
19472       code = TRUTH_ANDIF_EXPR;
19473       break;
19474     case CPP_OR_OR:
19475       code = TRUTH_ORIF_EXPR;
19476       break;
19477     default:
19478       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19479     resync_fail:
19480       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19481                                              /*or_comma=*/false,
19482                                              /*consume_paren=*/true);
19483       return list;
19484     }
19485   cp_lexer_consume_token (parser->lexer);
19486
19487   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19488     goto resync_fail;
19489
19490   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19491   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19492     OMP_CLAUSE_REDUCTION_CODE (c) = code;
19493
19494   return nlist;
19495 }
19496
19497 /* OpenMP 2.5:
19498    schedule ( schedule-kind )
19499    schedule ( schedule-kind , expression )
19500
19501    schedule-kind:
19502      static | dynamic | guided | runtime  */
19503
19504 static tree
19505 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19506 {
19507   tree c, t;
19508
19509   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19510     return list;
19511
19512   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19513
19514   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19515     {
19516       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19517       const char *p = IDENTIFIER_POINTER (id);
19518
19519       switch (p[0])
19520         {
19521         case 'd':
19522           if (strcmp ("dynamic", p) != 0)
19523             goto invalid_kind;
19524           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19525           break;
19526
19527         case 'g':
19528           if (strcmp ("guided", p) != 0)
19529             goto invalid_kind;
19530           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19531           break;
19532
19533         case 'r':
19534           if (strcmp ("runtime", p) != 0)
19535             goto invalid_kind;
19536           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19537           break;
19538
19539         default:
19540           goto invalid_kind;
19541         }
19542     }
19543   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19544     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19545   else
19546     goto invalid_kind;
19547   cp_lexer_consume_token (parser->lexer);
19548
19549   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19550     {
19551       cp_lexer_consume_token (parser->lexer);
19552
19553       t = cp_parser_assignment_expression (parser, false);
19554
19555       if (t == error_mark_node)
19556         goto resync_fail;
19557       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19558         error ("schedule %<runtime%> does not take "
19559                "a %<chunk_size%> parameter");
19560       else
19561         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19562
19563       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19564         goto resync_fail;
19565     }
19566   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19567     goto resync_fail;
19568
19569   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19570   OMP_CLAUSE_CHAIN (c) = list;
19571   return c;
19572
19573  invalid_kind:
19574   cp_parser_error (parser, "invalid schedule kind");
19575  resync_fail:
19576   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19577                                          /*or_comma=*/false,
19578                                          /*consume_paren=*/true);
19579   return list;
19580 }
19581
19582 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
19583    is a bitmask in MASK.  Return the list of clauses found; the result
19584    of clause default goes in *pdefault.  */
19585
19586 static tree
19587 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19588                            const char *where, cp_token *pragma_tok)
19589 {
19590   tree clauses = NULL;
19591
19592   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19593     {
19594       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19595       const char *c_name;
19596       tree prev = clauses;
19597
19598       switch (c_kind)
19599         {
19600         case PRAGMA_OMP_CLAUSE_COPYIN:
19601           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19602           c_name = "copyin";
19603           break;
19604         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19605           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19606                                             clauses);
19607           c_name = "copyprivate";
19608           break;
19609         case PRAGMA_OMP_CLAUSE_DEFAULT:
19610           clauses = cp_parser_omp_clause_default (parser, clauses);
19611           c_name = "default";
19612           break;
19613         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19614           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19615                                             clauses);
19616           c_name = "firstprivate";
19617           break;
19618         case PRAGMA_OMP_CLAUSE_IF:
19619           clauses = cp_parser_omp_clause_if (parser, clauses);
19620           c_name = "if";
19621           break;
19622         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19623           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19624                                             clauses);
19625           c_name = "lastprivate";
19626           break;
19627         case PRAGMA_OMP_CLAUSE_NOWAIT:
19628           clauses = cp_parser_omp_clause_nowait (parser, clauses);
19629           c_name = "nowait";
19630           break;
19631         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19632           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19633           c_name = "num_threads";
19634           break;
19635         case PRAGMA_OMP_CLAUSE_ORDERED:
19636           clauses = cp_parser_omp_clause_ordered (parser, clauses);
19637           c_name = "ordered";
19638           break;
19639         case PRAGMA_OMP_CLAUSE_PRIVATE:
19640           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19641                                             clauses);
19642           c_name = "private";
19643           break;
19644         case PRAGMA_OMP_CLAUSE_REDUCTION:
19645           clauses = cp_parser_omp_clause_reduction (parser, clauses);
19646           c_name = "reduction";
19647           break;
19648         case PRAGMA_OMP_CLAUSE_SCHEDULE:
19649           clauses = cp_parser_omp_clause_schedule (parser, clauses);
19650           c_name = "schedule";
19651           break;
19652         case PRAGMA_OMP_CLAUSE_SHARED:
19653           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19654                                             clauses);
19655           c_name = "shared";
19656           break;
19657         default:
19658           cp_parser_error (parser, "expected %<#pragma omp%> clause");
19659           goto saw_error;
19660         }
19661
19662       if (((mask >> c_kind) & 1) == 0)
19663         {
19664           /* Remove the invalid clause(s) from the list to avoid
19665              confusing the rest of the compiler.  */
19666           clauses = prev;
19667           error ("%qs is not valid for %qs", c_name, where);
19668         }
19669     }
19670  saw_error:
19671   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19672   return finish_omp_clauses (clauses);
19673 }
19674
19675 /* OpenMP 2.5:
19676    structured-block:
19677      statement
19678
19679    In practice, we're also interested in adding the statement to an
19680    outer node.  So it is convenient if we work around the fact that
19681    cp_parser_statement calls add_stmt.  */
19682
19683 static unsigned
19684 cp_parser_begin_omp_structured_block (cp_parser *parser)
19685 {
19686   unsigned save = parser->in_statement;
19687
19688   /* Only move the values to IN_OMP_BLOCK if they weren't false.
19689      This preserves the "not within loop or switch" style error messages
19690      for nonsense cases like
19691         void foo() {
19692         #pragma omp single
19693           break;
19694         }
19695   */
19696   if (parser->in_statement)
19697     parser->in_statement = IN_OMP_BLOCK;
19698
19699   return save;
19700 }
19701
19702 static void
19703 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19704 {
19705   parser->in_statement = save;
19706 }
19707
19708 static tree
19709 cp_parser_omp_structured_block (cp_parser *parser)
19710 {
19711   tree stmt = begin_omp_structured_block ();
19712   unsigned int save = cp_parser_begin_omp_structured_block (parser);
19713
19714   cp_parser_statement (parser, NULL_TREE, false, NULL);
19715
19716   cp_parser_end_omp_structured_block (parser, save);
19717   return finish_omp_structured_block (stmt);
19718 }
19719
19720 /* OpenMP 2.5:
19721    # pragma omp atomic new-line
19722      expression-stmt
19723
19724    expression-stmt:
19725      x binop= expr | x++ | ++x | x-- | --x
19726    binop:
19727      +, *, -, /, &, ^, |, <<, >>
19728
19729   where x is an lvalue expression with scalar type.  */
19730
19731 static void
19732 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19733 {
19734   tree lhs, rhs;
19735   enum tree_code code;
19736
19737   cp_parser_require_pragma_eol (parser, pragma_tok);
19738
19739   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19740                                     /*cast_p=*/false);
19741   switch (TREE_CODE (lhs))
19742     {
19743     case ERROR_MARK:
19744       goto saw_error;
19745
19746     case PREINCREMENT_EXPR:
19747     case POSTINCREMENT_EXPR:
19748       lhs = TREE_OPERAND (lhs, 0);
19749       code = PLUS_EXPR;
19750       rhs = integer_one_node;
19751       break;
19752
19753     case PREDECREMENT_EXPR:
19754     case POSTDECREMENT_EXPR:
19755       lhs = TREE_OPERAND (lhs, 0);
19756       code = MINUS_EXPR;
19757       rhs = integer_one_node;
19758       break;
19759
19760     default:
19761       switch (cp_lexer_peek_token (parser->lexer)->type)
19762         {
19763         case CPP_MULT_EQ:
19764           code = MULT_EXPR;
19765           break;
19766         case CPP_DIV_EQ:
19767           code = TRUNC_DIV_EXPR;
19768           break;
19769         case CPP_PLUS_EQ:
19770           code = PLUS_EXPR;
19771           break;
19772         case CPP_MINUS_EQ:
19773           code = MINUS_EXPR;
19774           break;
19775         case CPP_LSHIFT_EQ:
19776           code = LSHIFT_EXPR;
19777           break;
19778         case CPP_RSHIFT_EQ:
19779           code = RSHIFT_EXPR;
19780           break;
19781         case CPP_AND_EQ:
19782           code = BIT_AND_EXPR;
19783           break;
19784         case CPP_OR_EQ:
19785           code = BIT_IOR_EXPR;
19786           break;
19787         case CPP_XOR_EQ:
19788           code = BIT_XOR_EXPR;
19789           break;
19790         default:
19791           cp_parser_error (parser,
19792                            "invalid operator for %<#pragma omp atomic%>");
19793           goto saw_error;
19794         }
19795       cp_lexer_consume_token (parser->lexer);
19796
19797       rhs = cp_parser_expression (parser, false);
19798       if (rhs == error_mark_node)
19799         goto saw_error;
19800       break;
19801     }
19802   finish_omp_atomic (code, lhs, rhs);
19803   cp_parser_consume_semicolon_at_end_of_statement (parser);
19804   return;
19805
19806  saw_error:
19807   cp_parser_skip_to_end_of_block_or_statement (parser);
19808 }
19809
19810
19811 /* OpenMP 2.5:
19812    # pragma omp barrier new-line  */
19813
19814 static void
19815 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19816 {
19817   cp_parser_require_pragma_eol (parser, pragma_tok);
19818   finish_omp_barrier ();
19819 }
19820
19821 /* OpenMP 2.5:
19822    # pragma omp critical [(name)] new-line
19823      structured-block  */
19824
19825 static tree
19826 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19827 {
19828   tree stmt, name = NULL;
19829
19830   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19831     {
19832       cp_lexer_consume_token (parser->lexer);
19833
19834       name = cp_parser_identifier (parser);
19835
19836       if (name == error_mark_node
19837           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19838         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19839                                                /*or_comma=*/false,
19840                                                /*consume_paren=*/true);
19841       if (name == error_mark_node)
19842         name = NULL;
19843     }
19844   cp_parser_require_pragma_eol (parser, pragma_tok);
19845
19846   stmt = cp_parser_omp_structured_block (parser);
19847   return c_finish_omp_critical (stmt, name);
19848 }
19849
19850 /* OpenMP 2.5:
19851    # pragma omp flush flush-vars[opt] new-line
19852
19853    flush-vars:
19854      ( variable-list ) */
19855
19856 static void
19857 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19858 {
19859   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19860     (void) cp_parser_omp_var_list (parser, 0, NULL);
19861   cp_parser_require_pragma_eol (parser, pragma_tok);
19862
19863   finish_omp_flush ();
19864 }
19865
19866 /* Parse the restricted form of the for statment allowed by OpenMP.  */
19867
19868 static tree
19869 cp_parser_omp_for_loop (cp_parser *parser)
19870 {
19871   tree init, cond, incr, body, decl, pre_body;
19872   location_t loc;
19873
19874   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19875     {
19876       cp_parser_error (parser, "for statement expected");
19877       return NULL;
19878     }
19879   loc = cp_lexer_consume_token (parser->lexer)->location;
19880   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19881     return NULL;
19882
19883   init = decl = NULL;
19884   pre_body = push_stmt_list ();
19885   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19886     {
19887       cp_decl_specifier_seq type_specifiers;
19888
19889       /* First, try to parse as an initialized declaration.  See
19890          cp_parser_condition, from whence the bulk of this is copied.  */
19891
19892       cp_parser_parse_tentatively (parser);
19893       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19894                                     &type_specifiers);
19895       if (!cp_parser_error_occurred (parser))
19896         {
19897           tree asm_specification, attributes;
19898           cp_declarator *declarator;
19899
19900           declarator = cp_parser_declarator (parser,
19901                                              CP_PARSER_DECLARATOR_NAMED,
19902                                              /*ctor_dtor_or_conv_p=*/NULL,
19903                                              /*parenthesized_p=*/NULL,
19904                                              /*member_p=*/false);
19905           attributes = cp_parser_attributes_opt (parser);
19906           asm_specification = cp_parser_asm_specification_opt (parser);
19907
19908           cp_parser_require (parser, CPP_EQ, "`='");
19909           if (cp_parser_parse_definitely (parser))
19910             {
19911               tree pushed_scope;
19912
19913               decl = start_decl (declarator, &type_specifiers,
19914                                  /*initialized_p=*/false, attributes,
19915                                  /*prefix_attributes=*/NULL_TREE,
19916                                  &pushed_scope);
19917
19918               init = cp_parser_assignment_expression (parser, false);
19919
19920               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19921                               asm_specification, LOOKUP_ONLYCONVERTING);
19922
19923               if (pushed_scope)
19924                 pop_scope (pushed_scope);
19925             }
19926         }
19927       else
19928         cp_parser_abort_tentative_parse (parser);
19929
19930       /* If parsing as an initialized declaration failed, try again as
19931          a simple expression.  */
19932       if (decl == NULL)
19933         init = cp_parser_expression (parser, false);
19934     }
19935   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19936   pre_body = pop_stmt_list (pre_body);
19937
19938   cond = NULL;
19939   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19940     cond = cp_parser_condition (parser);
19941   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19942
19943   incr = NULL;
19944   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19945     incr = cp_parser_expression (parser, false);
19946
19947   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19948     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19949                                            /*or_comma=*/false,
19950                                            /*consume_paren=*/true);
19951
19952   /* Note that we saved the original contents of this flag when we entered
19953      the structured block, and so we don't need to re-save it here.  */
19954   parser->in_statement = IN_OMP_FOR;
19955
19956   /* Note that the grammar doesn't call for a structured block here,
19957      though the loop as a whole is a structured block.  */
19958   body = push_stmt_list ();
19959   cp_parser_statement (parser, NULL_TREE, false, NULL);
19960   body = pop_stmt_list (body);
19961
19962   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19963 }
19964
19965 /* OpenMP 2.5:
19966    #pragma omp for for-clause[optseq] new-line
19967      for-loop  */
19968
19969 #define OMP_FOR_CLAUSE_MASK                             \
19970         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19971         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19972         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19973         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19974         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
19975         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
19976         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19977
19978 static tree
19979 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19980 {
19981   tree clauses, sb, ret;
19982   unsigned int save;
19983
19984   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19985                                        "#pragma omp for", pragma_tok);
19986
19987   sb = begin_omp_structured_block ();
19988   save = cp_parser_begin_omp_structured_block (parser);
19989
19990   ret = cp_parser_omp_for_loop (parser);
19991   if (ret)
19992     OMP_FOR_CLAUSES (ret) = clauses;
19993
19994   cp_parser_end_omp_structured_block (parser, save);
19995   add_stmt (finish_omp_structured_block (sb));
19996
19997   return ret;
19998 }
19999
20000 /* OpenMP 2.5:
20001    # pragma omp master new-line
20002      structured-block  */
20003
20004 static tree
20005 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20006 {
20007   cp_parser_require_pragma_eol (parser, pragma_tok);
20008   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20009 }
20010
20011 /* OpenMP 2.5:
20012    # pragma omp ordered new-line
20013      structured-block  */
20014
20015 static tree
20016 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20017 {
20018   cp_parser_require_pragma_eol (parser, pragma_tok);
20019   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20020 }
20021
20022 /* OpenMP 2.5:
20023
20024    section-scope:
20025      { section-sequence }
20026
20027    section-sequence:
20028      section-directive[opt] structured-block
20029      section-sequence section-directive structured-block  */
20030
20031 static tree
20032 cp_parser_omp_sections_scope (cp_parser *parser)
20033 {
20034   tree stmt, substmt;
20035   bool error_suppress = false;
20036   cp_token *tok;
20037
20038   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20039     return NULL_TREE;
20040
20041   stmt = push_stmt_list ();
20042
20043   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20044     {
20045       unsigned save;
20046
20047       substmt = begin_omp_structured_block ();
20048       save = cp_parser_begin_omp_structured_block (parser);
20049
20050       while (1)
20051         {
20052           cp_parser_statement (parser, NULL_TREE, false, NULL);
20053
20054           tok = cp_lexer_peek_token (parser->lexer);
20055           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20056             break;
20057           if (tok->type == CPP_CLOSE_BRACE)
20058             break;
20059           if (tok->type == CPP_EOF)
20060             break;
20061         }
20062
20063       cp_parser_end_omp_structured_block (parser, save);
20064       substmt = finish_omp_structured_block (substmt);
20065       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20066       add_stmt (substmt);
20067     }
20068
20069   while (1)
20070     {
20071       tok = cp_lexer_peek_token (parser->lexer);
20072       if (tok->type == CPP_CLOSE_BRACE)
20073         break;
20074       if (tok->type == CPP_EOF)
20075         break;
20076
20077       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20078         {
20079           cp_lexer_consume_token (parser->lexer);
20080           cp_parser_require_pragma_eol (parser, tok);
20081           error_suppress = false;
20082         }
20083       else if (!error_suppress)
20084         {
20085           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20086           error_suppress = true;
20087         }
20088
20089       substmt = cp_parser_omp_structured_block (parser);
20090       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20091       add_stmt (substmt);
20092     }
20093   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20094
20095   substmt = pop_stmt_list (stmt);
20096
20097   stmt = make_node (OMP_SECTIONS);
20098   TREE_TYPE (stmt) = void_type_node;
20099   OMP_SECTIONS_BODY (stmt) = substmt;
20100
20101   add_stmt (stmt);
20102   return stmt;
20103 }
20104
20105 /* OpenMP 2.5:
20106    # pragma omp sections sections-clause[optseq] newline
20107      sections-scope  */
20108
20109 #define OMP_SECTIONS_CLAUSE_MASK                        \
20110         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20111         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20112         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
20113         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20114         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20115
20116 static tree
20117 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20118 {
20119   tree clauses, ret;
20120
20121   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20122                                        "#pragma omp sections", pragma_tok);
20123
20124   ret = cp_parser_omp_sections_scope (parser);
20125   if (ret)
20126     OMP_SECTIONS_CLAUSES (ret) = clauses;
20127
20128   return ret;
20129 }
20130
20131 /* OpenMP 2.5:
20132    # pragma parallel parallel-clause new-line
20133    # pragma parallel for parallel-for-clause new-line
20134    # pragma parallel sections parallel-sections-clause new-line  */
20135
20136 #define OMP_PARALLEL_CLAUSE_MASK                        \
20137         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
20138         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20139         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20140         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
20141         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
20142         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
20143         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20144         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20145
20146 static tree
20147 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20148 {
20149   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20150   const char *p_name = "#pragma omp parallel";
20151   tree stmt, clauses, par_clause, ws_clause, block;
20152   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20153   unsigned int save;
20154
20155   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20156     {
20157       cp_lexer_consume_token (parser->lexer);
20158       p_kind = PRAGMA_OMP_PARALLEL_FOR;
20159       p_name = "#pragma omp parallel for";
20160       mask |= OMP_FOR_CLAUSE_MASK;
20161       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20162     }
20163   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20164     {
20165       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20166       const char *p = IDENTIFIER_POINTER (id);
20167       if (strcmp (p, "sections") == 0)
20168         {
20169           cp_lexer_consume_token (parser->lexer);
20170           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20171           p_name = "#pragma omp parallel sections";
20172           mask |= OMP_SECTIONS_CLAUSE_MASK;
20173           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20174         }
20175     }
20176
20177   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20178   block = begin_omp_parallel ();
20179   save = cp_parser_begin_omp_structured_block (parser);
20180
20181   switch (p_kind)
20182     {
20183     case PRAGMA_OMP_PARALLEL:
20184       cp_parser_already_scoped_statement (parser);
20185       par_clause = clauses;
20186       break;
20187
20188     case PRAGMA_OMP_PARALLEL_FOR:
20189       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20190       stmt = cp_parser_omp_for_loop (parser);
20191       if (stmt)
20192         OMP_FOR_CLAUSES (stmt) = ws_clause;
20193       break;
20194
20195     case PRAGMA_OMP_PARALLEL_SECTIONS:
20196       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20197       stmt = cp_parser_omp_sections_scope (parser);
20198       if (stmt)
20199         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20200       break;
20201
20202     default:
20203       gcc_unreachable ();
20204     }
20205
20206   cp_parser_end_omp_structured_block (parser, save);
20207   stmt = finish_omp_parallel (par_clause, block);
20208   if (p_kind != PRAGMA_OMP_PARALLEL)
20209     OMP_PARALLEL_COMBINED (stmt) = 1;
20210   return stmt;
20211 }
20212
20213 /* OpenMP 2.5:
20214    # pragma omp single single-clause[optseq] new-line
20215      structured-block  */
20216
20217 #define OMP_SINGLE_CLAUSE_MASK                          \
20218         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20219         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20220         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
20221         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20222
20223 static tree
20224 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20225 {
20226   tree stmt = make_node (OMP_SINGLE);
20227   TREE_TYPE (stmt) = void_type_node;
20228
20229   OMP_SINGLE_CLAUSES (stmt)
20230     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20231                                  "#pragma omp single", pragma_tok);
20232   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20233
20234   return add_stmt (stmt);
20235 }
20236
20237 /* OpenMP 2.5:
20238    # pragma omp threadprivate (variable-list) */
20239
20240 static void
20241 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20242 {
20243   tree vars;
20244
20245   vars = cp_parser_omp_var_list (parser, 0, NULL);
20246   cp_parser_require_pragma_eol (parser, pragma_tok);
20247
20248   finish_omp_threadprivate (vars);
20249 }
20250
20251 /* Main entry point to OpenMP statement pragmas.  */
20252
20253 static void
20254 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20255 {
20256   tree stmt;
20257
20258   switch (pragma_tok->pragma_kind)
20259     {
20260     case PRAGMA_OMP_ATOMIC:
20261       cp_parser_omp_atomic (parser, pragma_tok);
20262       return;
20263     case PRAGMA_OMP_CRITICAL:
20264       stmt = cp_parser_omp_critical (parser, pragma_tok);
20265       break;
20266     case PRAGMA_OMP_FOR:
20267       stmt = cp_parser_omp_for (parser, pragma_tok);
20268       break;
20269     case PRAGMA_OMP_MASTER:
20270       stmt = cp_parser_omp_master (parser, pragma_tok);
20271       break;
20272     case PRAGMA_OMP_ORDERED:
20273       stmt = cp_parser_omp_ordered (parser, pragma_tok);
20274       break;
20275     case PRAGMA_OMP_PARALLEL:
20276       stmt = cp_parser_omp_parallel (parser, pragma_tok);
20277       break;
20278     case PRAGMA_OMP_SECTIONS:
20279       stmt = cp_parser_omp_sections (parser, pragma_tok);
20280       break;
20281     case PRAGMA_OMP_SINGLE:
20282       stmt = cp_parser_omp_single (parser, pragma_tok);
20283       break;
20284     default:
20285       gcc_unreachable ();
20286     }
20287
20288   if (stmt)
20289     SET_EXPR_LOCATION (stmt, pragma_tok->location);
20290 }
20291 \f
20292 /* The parser.  */
20293
20294 static GTY (()) cp_parser *the_parser;
20295
20296 \f
20297 /* Special handling for the first token or line in the file.  The first
20298    thing in the file might be #pragma GCC pch_preprocess, which loads a
20299    PCH file, which is a GC collection point.  So we need to handle this
20300    first pragma without benefit of an existing lexer structure.
20301
20302    Always returns one token to the caller in *FIRST_TOKEN.  This is
20303    either the true first token of the file, or the first token after
20304    the initial pragma.  */
20305
20306 static void
20307 cp_parser_initial_pragma (cp_token *first_token)
20308 {
20309   tree name = NULL;
20310
20311   cp_lexer_get_preprocessor_token (NULL, first_token);
20312   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20313     return;
20314
20315   cp_lexer_get_preprocessor_token (NULL, first_token);
20316   if (first_token->type == CPP_STRING)
20317     {
20318       name = first_token->u.value;
20319
20320       cp_lexer_get_preprocessor_token (NULL, first_token);
20321       if (first_token->type != CPP_PRAGMA_EOL)
20322         error ("junk at end of %<#pragma GCC pch_preprocess%>");
20323     }
20324   else
20325     error ("expected string literal");
20326
20327   /* Skip to the end of the pragma.  */
20328   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20329     cp_lexer_get_preprocessor_token (NULL, first_token);
20330
20331   /* Now actually load the PCH file.  */
20332   if (name)
20333     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20334
20335   /* Read one more token to return to our caller.  We have to do this
20336      after reading the PCH file in, since its pointers have to be
20337      live.  */
20338   cp_lexer_get_preprocessor_token (NULL, first_token);
20339 }
20340
20341 /* Normal parsing of a pragma token.  Here we can (and must) use the
20342    regular lexer.  */
20343
20344 static bool
20345 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20346 {
20347   cp_token *pragma_tok;
20348   unsigned int id;
20349
20350   pragma_tok = cp_lexer_consume_token (parser->lexer);
20351   gcc_assert (pragma_tok->type == CPP_PRAGMA);
20352   parser->lexer->in_pragma = true;
20353
20354   id = pragma_tok->pragma_kind;
20355   switch (id)
20356     {
20357     case PRAGMA_GCC_PCH_PREPROCESS:
20358       error ("%<#pragma GCC pch_preprocess%> must be first");
20359       break;
20360
20361     case PRAGMA_OMP_BARRIER:
20362       switch (context)
20363         {
20364         case pragma_compound:
20365           cp_parser_omp_barrier (parser, pragma_tok);
20366           return false;
20367         case pragma_stmt:
20368           error ("%<#pragma omp barrier%> may only be "
20369                  "used in compound statements");
20370           break;
20371         default:
20372           goto bad_stmt;
20373         }
20374       break;
20375
20376     case PRAGMA_OMP_FLUSH:
20377       switch (context)
20378         {
20379         case pragma_compound:
20380           cp_parser_omp_flush (parser, pragma_tok);
20381           return false;
20382         case pragma_stmt:
20383           error ("%<#pragma omp flush%> may only be "
20384                  "used in compound statements");
20385           break;
20386         default:
20387           goto bad_stmt;
20388         }
20389       break;
20390
20391     case PRAGMA_OMP_THREADPRIVATE:
20392       cp_parser_omp_threadprivate (parser, pragma_tok);
20393       return false;
20394
20395     case PRAGMA_OMP_ATOMIC:
20396     case PRAGMA_OMP_CRITICAL:
20397     case PRAGMA_OMP_FOR:
20398     case PRAGMA_OMP_MASTER:
20399     case PRAGMA_OMP_ORDERED:
20400     case PRAGMA_OMP_PARALLEL:
20401     case PRAGMA_OMP_SECTIONS:
20402     case PRAGMA_OMP_SINGLE:
20403       if (context == pragma_external)
20404         goto bad_stmt;
20405       cp_parser_omp_construct (parser, pragma_tok);
20406       return true;
20407
20408     case PRAGMA_OMP_SECTION:
20409       error ("%<#pragma omp section%> may only be used in "
20410              "%<#pragma omp sections%> construct");
20411       break;
20412
20413     default:
20414       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20415       c_invoke_pragma_handler (id);
20416       break;
20417
20418     bad_stmt:
20419       cp_parser_error (parser, "expected declaration specifiers");
20420       break;
20421     }
20422
20423   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20424   return false;
20425 }
20426
20427 /* The interface the pragma parsers have to the lexer.  */
20428
20429 enum cpp_ttype
20430 pragma_lex (tree *value)
20431 {
20432   cp_token *tok;
20433   enum cpp_ttype ret;
20434
20435   tok = cp_lexer_peek_token (the_parser->lexer);
20436
20437   ret = tok->type;
20438   *value = tok->u.value;
20439
20440   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20441     ret = CPP_EOF;
20442   else if (ret == CPP_STRING)
20443     *value = cp_parser_string_literal (the_parser, false, false);
20444   else
20445     {
20446       cp_lexer_consume_token (the_parser->lexer);
20447       if (ret == CPP_KEYWORD)
20448         ret = CPP_NAME;
20449     }
20450
20451   return ret;
20452 }
20453
20454 \f
20455 /* External interface.  */
20456
20457 /* Parse one entire translation unit.  */
20458
20459 void
20460 c_parse_file (void)
20461 {
20462   bool error_occurred;
20463   static bool already_called = false;
20464
20465   if (already_called)
20466     {
20467       sorry ("inter-module optimizations not implemented for C++");
20468       return;
20469     }
20470   already_called = true;
20471
20472   the_parser = cp_parser_new ();
20473   push_deferring_access_checks (flag_access_control
20474                                 ? dk_no_deferred : dk_no_check);
20475   error_occurred = cp_parser_translation_unit (the_parser);
20476   the_parser = NULL;
20477 }
20478
20479 #include "gt-cp-parser.h"