OSDN Git Service

gcc/cp
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009  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 context where it is implicitly extern "C" */
75   BOOL_BITFIELD implicit_extern_c : 1;
76   /* True for a CPP_NAME token that is not a keyword (i.e., for which
77      KEYWORD is RID_MAX) iff this name was looked up and found to be
78      ambiguous.  An error has already been reported.  */
79   BOOL_BITFIELD ambiguous_p : 1;
80   /* The value associated with this token, if any.  */
81   union cp_token_value {
82     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
83     struct tree_check* GTY((tag ("1"))) tree_check_value;
84     /* Use for all other tokens.  */
85     tree GTY((tag ("0"))) value;
86   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87   /* The location at which this token was found.  */
88   location_t location;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets.  */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
99   0
100 };
101
102 /* The cp_lexer structure represents the C++ lexer.  It is responsible
103    for managing the token stream from the preprocessor and supplying
104    it to the parser.  Tokens are never added to the cp_lexer after
105    it is created.  */
106
107 typedef struct cp_lexer GTY (())
108 {
109   /* The memory allocated for the buffer.  NULL if this lexer does not
110      own the token buffer.  */
111   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112   /* If the lexer owns the buffer, this is the number of tokens in the
113      buffer.  */
114   size_t buffer_length;
115
116   /* A pointer just past the last available token.  The tokens
117      in this lexer are [buffer, last_token).  */
118   cp_token_position GTY ((skip)) last_token;
119
120   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
121      no more available tokens.  */
122   cp_token_position GTY ((skip)) next_token;
123
124   /* A stack indicating positions at which cp_lexer_save_tokens was
125      called.  The top entry is the most recent position at which we
126      began saving tokens.  If the stack is non-empty, we are saving
127      tokens.  */
128   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
129
130   /* The next lexer in a linked list of lexers.  */
131   struct cp_lexer *next;
132
133   /* True if we should output debugging information.  */
134   bool debugging_p;
135
136   /* True if we're in the context of parsing a pragma, and should not
137      increment past the end-of-line marker.  */
138   bool in_pragma;
139 } cp_lexer;
140
141 /* cp_token_cache is a range of tokens.  There is no need to represent
142    allocate heap memory for it, since tokens are never removed from the
143    lexer's array.  There is also no need for the GC to walk through
144    a cp_token_cache, since everything in here is referenced through
145    a lexer.  */
146
147 typedef struct cp_token_cache GTY(())
148 {
149   /* The beginning of the token range.  */
150   cp_token * GTY((skip)) first;
151
152   /* Points immediately after the last token in the range.  */
153   cp_token * GTY ((skip)) last;
154 } cp_token_cache;
155
156 /* Prototypes.  */
157
158 static cp_lexer *cp_lexer_new_main
159   (void);
160 static cp_lexer *cp_lexer_new_from_tokens
161   (cp_token_cache *tokens);
162 static void cp_lexer_destroy
163   (cp_lexer *);
164 static int cp_lexer_saving_tokens
165   (const cp_lexer *);
166 static cp_token_position cp_lexer_token_position
167   (cp_lexer *, bool);
168 static cp_token *cp_lexer_token_at
169   (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171   (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
173   (cp_lexer *);
174 static cp_token *cp_lexer_peek_nth_token
175   (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177   (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179   (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181   (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
183   (cp_lexer *);
184 static void cp_lexer_purge_token
185   (cp_lexer *);
186 static void cp_lexer_purge_tokens_after
187   (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
189   (cp_lexer *);
190 static void cp_lexer_commit_tokens
191   (cp_lexer *);
192 static void cp_lexer_rollback_tokens
193   (cp_lexer *);
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196   (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
198   (cp_lexer *);
199 static void cp_lexer_start_debugging
200   (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202   (cp_lexer *) ATTRIBUTE_UNUSED;
203 #else
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205    about passing NULL to functions that require non-NULL arguments
206    (fputs, fprintf).  It will never be used, so all we need is a value
207    of the right type that's guaranteed not to be NULL.  */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
212
213 static cp_token_cache *cp_token_cache_new
214   (cp_token *, cp_token *);
215
216 static void cp_parser_initial_pragma
217   (cp_token *);
218
219 /* Manifest constants.  */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
222
223 /* A token type for keywords, as opposed to ordinary identifiers.  */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
225
226 /* A token type for template-ids.  If a template-id is processed while
227    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228    the value of the CPP_TEMPLATE_ID is whatever was returned by
229    cp_parser_template_id.  */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
231
232 /* A token type for nested-name-specifiers.  If a
233    nested-name-specifier is processed while parsing tentatively, it is
234    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236    cp_parser_nested_name_specifier_opt.  */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
238
239 /* A token type for tokens that are not tokens at all; these are used
240    to represent slots in the array where there used to be a token
241    that has now been deleted.  */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
243
244 /* The number of token types, including C++-specific ones.  */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
246
247 /* Variables.  */
248
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written.  */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
253
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255    preprocessor.  */
256
257 static cp_lexer *
258 cp_lexer_new_main (void)
259 {
260   cp_token first_token;
261   cp_lexer *lexer;
262   cp_token *pos;
263   size_t alloc;
264   size_t space;
265   cp_token *buffer;
266
267   /* It's possible that parsing the first pragma will load a PCH file,
268      which is a GC collection point.  So we have to do that before
269      allocating any memory.  */
270   cp_parser_initial_pragma (&first_token);
271
272   c_common_no_more_pch ();
273
274   /* Allocate the memory.  */
275   lexer = GGC_CNEW (cp_lexer);
276
277 #ifdef ENABLE_CHECKING
278   /* Initially we are not debugging.  */
279   lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282                                    CP_SAVED_TOKEN_STACK);
283
284   /* Create the buffer.  */
285   alloc = CP_LEXER_BUFFER_SIZE;
286   buffer = GGC_NEWVEC (cp_token, alloc);
287
288   /* Put the first token in the buffer.  */
289   space = alloc;
290   pos = buffer;
291   *pos = first_token;
292
293   /* Get the remaining tokens from the preprocessor.  */
294   while (pos->type != CPP_EOF)
295     {
296       pos++;
297       if (!--space)
298         {
299           space = alloc;
300           alloc *= 2;
301           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302           pos = buffer + space;
303         }
304       cp_lexer_get_preprocessor_token (lexer, pos);
305     }
306   lexer->buffer = buffer;
307   lexer->buffer_length = alloc - space;
308   lexer->last_token = pos;
309   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310
311   /* Subsequent preprocessor diagnostics should use compiler
312      diagnostic functions to get the compiler source location.  */
313   cpp_get_options (parse_in)->client_diagnostic = true;
314   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
315
316   gcc_assert (lexer->next_token->type != CPP_PURGED);
317   return lexer;
318 }
319
320 /* Create a new lexer whose token stream is primed with the tokens in
321    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
322
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 {
326   cp_token *first = cache->first;
327   cp_token *last = cache->last;
328   cp_lexer *lexer = GGC_CNEW (cp_lexer);
329
330   /* We do not own the buffer.  */
331   lexer->buffer = NULL;
332   lexer->buffer_length = 0;
333   lexer->next_token = first == last ? &eof_token : first;
334   lexer->last_token = last;
335
336   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337                                    CP_SAVED_TOKEN_STACK);
338
339 #ifdef ENABLE_CHECKING
340   /* Initially we are not debugging.  */
341   lexer->debugging_p = false;
342 #endif
343
344   gcc_assert (lexer->next_token->type != CPP_PURGED);
345   return lexer;
346 }
347
348 /* Frees all resources associated with LEXER.  */
349
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
352 {
353   if (lexer->buffer)
354     ggc_free (lexer->buffer);
355   VEC_free (cp_token_position, heap, lexer->saved_tokens);
356   ggc_free (lexer);
357 }
358
359 /* Returns nonzero if debugging information should be output.  */
360
361 #ifdef ENABLE_CHECKING
362
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
365 {
366   return lexer->debugging_p;
367 }
368
369 #endif /* ENABLE_CHECKING */
370
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 {
374   gcc_assert (!previous_p || lexer->next_token != &eof_token);
375
376   return lexer->next_token - previous_p;
377 }
378
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381 {
382   return pos;
383 }
384
385 /* nonzero if we are presently saving tokens.  */
386
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 {
390   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391 }
392
393 /* Store the next token from the preprocessor in *TOKEN.  Return true
394    if we reach EOF.  If LEXER is NULL, assume we are handling an
395    initial #pragma pch_preprocess, and thus want the lexer to return
396    processed strings.  */
397
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 {
401   static int is_extern_c = 0;
402
403    /* Get a new token from the preprocessor.  */
404   token->type
405     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407   token->keyword = RID_MAX;
408   token->pragma_kind = PRAGMA_NONE;
409
410   /* On some systems, some header files are surrounded by an
411      implicit extern "C" block.  Set a flag in the token if it
412      comes from such a header.  */
413   is_extern_c += pending_lang_change;
414   pending_lang_change = 0;
415   token->implicit_extern_c = is_extern_c > 0;
416
417   /* Check to see if this token is a keyword.  */
418   if (token->type == CPP_NAME)
419     {
420       if (C_IS_RESERVED_WORD (token->u.value))
421         {
422           /* Mark this token as a keyword.  */
423           token->type = CPP_KEYWORD;
424           /* Record which keyword.  */
425           token->keyword = C_RID_CODE (token->u.value);
426           /* Update the value.  Some keywords are mapped to particular
427              entities, rather than simply having the value of the
428              corresponding IDENTIFIER_NODE.  For example, `__const' is
429              mapped to `const'.  */
430           token->u.value = ridpointers[token->keyword];
431         }
432       else
433         {
434           if (warn_cxx0x_compat
435               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
437             {
438               /* Warn about the C++0x keyword (but still treat it as
439                  an identifier).  */
440               warning (OPT_Wc__0x_compat, 
441                        "identifier %<%s%> will become a keyword in C++0x",
442                        IDENTIFIER_POINTER (token->u.value));
443
444               /* Clear out the C_RID_CODE so we don't warn about this
445                  particular identifier-turned-keyword again.  */
446               C_SET_RID_CODE (token->u.value, RID_MAX);
447             }
448
449           token->ambiguous_p = false;
450           token->keyword = RID_MAX;
451         }
452     }
453   /* Handle Objective-C++ keywords.  */
454   else if (token->type == CPP_AT_NAME)
455     {
456       token->type = CPP_KEYWORD;
457       switch (C_RID_CODE (token->u.value))
458         {
459         /* Map 'class' to '@class', 'private' to '@private', etc.  */
460         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464         case RID_THROW: token->keyword = RID_AT_THROW; break;
465         case RID_TRY: token->keyword = RID_AT_TRY; break;
466         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467         default: token->keyword = C_RID_CODE (token->u.value);
468         }
469     }
470   else if (token->type == CPP_PRAGMA)
471     {
472       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
473       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474       token->u.value = NULL_TREE;
475     }
476 }
477
478 /* Update the globals input_location and the input file stack from TOKEN.  */
479 static inline void
480 cp_lexer_set_source_position_from_token (cp_token *token)
481 {
482   if (token->type != CPP_EOF)
483     {
484       input_location = token->location;
485     }
486 }
487
488 /* Return a pointer to the next token in the token stream, but do not
489    consume it.  */
490
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
493 {
494   if (cp_lexer_debugging_p (lexer))
495     {
496       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498       putc ('\n', cp_lexer_debug_stream);
499     }
500   return lexer->next_token;
501 }
502
503 /* Return true if the next token has the indicated TYPE.  */
504
505 static inline bool
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 {
508   return cp_lexer_peek_token (lexer)->type == type;
509 }
510
511 /* Return true if the next token does not have the indicated TYPE.  */
512
513 static inline bool
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 {
516   return !cp_lexer_next_token_is (lexer, type);
517 }
518
519 /* Return true if the next token is the indicated KEYWORD.  */
520
521 static inline bool
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 {
524   return cp_lexer_peek_token (lexer)->keyword == keyword;
525 }
526
527 /* Return true if the next token is not the indicated KEYWORD.  */
528
529 static inline bool
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
531 {
532   return cp_lexer_peek_token (lexer)->keyword != keyword;
533 }
534
535 /* Return true if the next token is a keyword for a decl-specifier.  */
536
537 static bool
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
539 {
540   cp_token *token;
541
542   token = cp_lexer_peek_token (lexer);
543   switch (token->keyword) 
544     {
545       /* auto specifier: storage-class-specifier in C++,
546          simple-type-specifier in C++0x.  */
547     case RID_AUTO:
548       /* Storage classes.  */
549     case RID_REGISTER:
550     case RID_STATIC:
551     case RID_EXTERN:
552     case RID_MUTABLE:
553     case RID_THREAD:
554       /* Elaborated type specifiers.  */
555     case RID_ENUM:
556     case RID_CLASS:
557     case RID_STRUCT:
558     case RID_UNION:
559     case RID_TYPENAME:
560       /* Simple type specifiers.  */
561     case RID_CHAR:
562     case RID_CHAR16:
563     case RID_CHAR32:
564     case RID_WCHAR:
565     case RID_BOOL:
566     case RID_SHORT:
567     case RID_INT:
568     case RID_LONG:
569     case RID_SIGNED:
570     case RID_UNSIGNED:
571     case RID_FLOAT:
572     case RID_DOUBLE:
573     case RID_VOID:
574       /* GNU extensions.  */ 
575     case RID_ATTRIBUTE:
576     case RID_TYPEOF:
577       /* C++0x extensions.  */
578     case RID_DECLTYPE:
579       return true;
580
581     default:
582       return false;
583     }
584 }
585
586 /* Return a pointer to the Nth token in the token stream.  If N is 1,
587    then this is precisely equivalent to cp_lexer_peek_token (except
588    that it is not inline).  One would like to disallow that case, but
589    there is one case (cp_parser_nth_token_starts_template_id) where
590    the caller passes a variable for N and it might be 1.  */
591
592 static cp_token *
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
594 {
595   cp_token *token;
596
597   /* N is 1-based, not zero-based.  */
598   gcc_assert (n > 0);
599
600   if (cp_lexer_debugging_p (lexer))
601     fprintf (cp_lexer_debug_stream,
602              "cp_lexer: peeking ahead %ld at token: ", (long)n);
603
604   --n;
605   token = lexer->next_token;
606   gcc_assert (!n || token != &eof_token);
607   while (n != 0)
608     {
609       ++token;
610       if (token == lexer->last_token)
611         {
612           token = &eof_token;
613           break;
614         }
615
616       if (token->type != CPP_PURGED)
617         --n;
618     }
619
620   if (cp_lexer_debugging_p (lexer))
621     {
622       cp_lexer_print_token (cp_lexer_debug_stream, token);
623       putc ('\n', cp_lexer_debug_stream);
624     }
625
626   return token;
627 }
628
629 /* Return the next token, and advance the lexer's next_token pointer
630    to point to the next non-purged token.  */
631
632 static cp_token *
633 cp_lexer_consume_token (cp_lexer* lexer)
634 {
635   cp_token *token = lexer->next_token;
636
637   gcc_assert (token != &eof_token);
638   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
639
640   do
641     {
642       lexer->next_token++;
643       if (lexer->next_token == lexer->last_token)
644         {
645           lexer->next_token = &eof_token;
646           break;
647         }
648
649     }
650   while (lexer->next_token->type == CPP_PURGED);
651
652   cp_lexer_set_source_position_from_token (token);
653
654   /* Provide debugging output.  */
655   if (cp_lexer_debugging_p (lexer))
656     {
657       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658       cp_lexer_print_token (cp_lexer_debug_stream, token);
659       putc ('\n', cp_lexer_debug_stream);
660     }
661
662   return token;
663 }
664
665 /* Permanently remove the next token from the token stream, and
666    advance the next_token pointer to refer to the next non-purged
667    token.  */
668
669 static void
670 cp_lexer_purge_token (cp_lexer *lexer)
671 {
672   cp_token *tok = lexer->next_token;
673
674   gcc_assert (tok != &eof_token);
675   tok->type = CPP_PURGED;
676   tok->location = UNKNOWN_LOCATION;
677   tok->u.value = NULL_TREE;
678   tok->keyword = RID_MAX;
679
680   do
681     {
682       tok++;
683       if (tok == lexer->last_token)
684         {
685           tok = &eof_token;
686           break;
687         }
688     }
689   while (tok->type == CPP_PURGED);
690   lexer->next_token = tok;
691 }
692
693 /* Permanently remove all tokens after TOK, up to, but not
694    including, the token that will be returned next by
695    cp_lexer_peek_token.  */
696
697 static void
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
699 {
700   cp_token *peek = lexer->next_token;
701
702   if (peek == &eof_token)
703     peek = lexer->last_token;
704
705   gcc_assert (tok < peek);
706
707   for ( tok += 1; tok != peek; tok += 1)
708     {
709       tok->type = CPP_PURGED;
710       tok->location = UNKNOWN_LOCATION;
711       tok->u.value = NULL_TREE;
712       tok->keyword = RID_MAX;
713     }
714 }
715
716 /* Begin saving tokens.  All tokens consumed after this point will be
717    preserved.  */
718
719 static void
720 cp_lexer_save_tokens (cp_lexer* lexer)
721 {
722   /* Provide debugging output.  */
723   if (cp_lexer_debugging_p (lexer))
724     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
725
726   VEC_safe_push (cp_token_position, heap,
727                  lexer->saved_tokens, lexer->next_token);
728 }
729
730 /* Commit to the portion of the token stream most recently saved.  */
731
732 static void
733 cp_lexer_commit_tokens (cp_lexer* lexer)
734 {
735   /* Provide debugging output.  */
736   if (cp_lexer_debugging_p (lexer))
737     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
738
739   VEC_pop (cp_token_position, lexer->saved_tokens);
740 }
741
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743    to the token stream.  Stop saving tokens.  */
744
745 static void
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
747 {
748   /* Provide debugging output.  */
749   if (cp_lexer_debugging_p (lexer))
750     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
751
752   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
753 }
754
755 /* Print a representation of the TOKEN on the STREAM.  */
756
757 #ifdef ENABLE_CHECKING
758
759 static void
760 cp_lexer_print_token (FILE * stream, cp_token *token)
761 {
762   /* We don't use cpp_type2name here because the parser defines
763      a few tokens of its own.  */
764   static const char *const token_names[] = {
765     /* cpplib-defined token types */
766 #define OP(e, s) #e,
767 #define TK(e, s) #e,
768     TTYPE_TABLE
769 #undef OP
770 #undef TK
771     /* C++ parser token types - see "Manifest constants", above.  */
772     "KEYWORD",
773     "TEMPLATE_ID",
774     "NESTED_NAME_SPECIFIER",
775     "PURGED"
776   };
777
778   /* If we have a name for the token, print it out.  Otherwise, we
779      simply give the numeric code.  */
780   gcc_assert (token->type < ARRAY_SIZE(token_names));
781   fputs (token_names[token->type], stream);
782
783   /* For some tokens, print the associated data.  */
784   switch (token->type)
785     {
786     case CPP_KEYWORD:
787       /* Some keywords have a value that is not an IDENTIFIER_NODE.
788          For example, `struct' is mapped to an INTEGER_CST.  */
789       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
790         break;
791       /* else fall through */
792     case CPP_NAME:
793       fputs (IDENTIFIER_POINTER (token->u.value), stream);
794       break;
795
796     case CPP_STRING:
797     case CPP_STRING16:
798     case CPP_STRING32:
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 *, tree, cp_cv_quals, tree, 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                       tree parms,
1017                       cp_cv_quals cv_qualifiers,
1018                       tree exception_specification,
1019                       tree late_return_type)
1020 {
1021   cp_declarator *declarator;
1022
1023   declarator = make_declarator (cdk_function);
1024   declarator->declarator = target;
1025   declarator->u.function.parameters = parms;
1026   declarator->u.function.qualifiers = cv_qualifiers;
1027   declarator->u.function.exception_specification = exception_specification;
1028   declarator->u.function.late_return_type = late_return_type;
1029   if (target)
1030     {
1031       declarator->parameter_pack_p = target->parameter_pack_p;
1032       target->parameter_pack_p = false;
1033     }
1034   else
1035     declarator->parameter_pack_p = false;
1036
1037   return declarator;
1038 }
1039
1040 /* Make a declarator for an array of BOUNDS elements, each of which is
1041    defined by ELEMENT.  */
1042
1043 cp_declarator *
1044 make_array_declarator (cp_declarator *element, tree bounds)
1045 {
1046   cp_declarator *declarator;
1047
1048   declarator = make_declarator (cdk_array);
1049   declarator->declarator = element;
1050   declarator->u.array.bounds = bounds;
1051   if (element)
1052     {
1053       declarator->parameter_pack_p = element->parameter_pack_p;
1054       element->parameter_pack_p = false;
1055     }
1056   else
1057     declarator->parameter_pack_p = false;
1058
1059   return declarator;
1060 }
1061
1062 /* Determine whether the declarator we've seen so far can be a
1063    parameter pack, when followed by an ellipsis.  */
1064 static bool 
1065 declarator_can_be_parameter_pack (cp_declarator *declarator)
1066 {
1067   /* Search for a declarator name, or any other declarator that goes
1068      after the point where the ellipsis could appear in a parameter
1069      pack. If we find any of these, then this declarator can not be
1070      made into a parameter pack.  */
1071   bool found = false;
1072   while (declarator && !found)
1073     {
1074       switch ((int)declarator->kind)
1075         {
1076         case cdk_id:
1077         case cdk_array:
1078           found = true;
1079           break;
1080
1081         case cdk_error:
1082           return true;
1083
1084         default:
1085           declarator = declarator->declarator;
1086           break;
1087         }
1088     }
1089
1090   return !found;
1091 }
1092
1093 cp_parameter_declarator *no_parameters;
1094
1095 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1096    DECLARATOR and DEFAULT_ARGUMENT.  */
1097
1098 cp_parameter_declarator *
1099 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1100                            cp_declarator *declarator,
1101                            tree default_argument)
1102 {
1103   cp_parameter_declarator *parameter;
1104
1105   parameter = ((cp_parameter_declarator *)
1106                alloc_declarator (sizeof (cp_parameter_declarator)));
1107   parameter->next = NULL;
1108   if (decl_specifiers)
1109     parameter->decl_specifiers = *decl_specifiers;
1110   else
1111     clear_decl_specs (&parameter->decl_specifiers);
1112   parameter->declarator = declarator;
1113   parameter->default_argument = default_argument;
1114   parameter->ellipsis_p = false;
1115
1116   return parameter;
1117 }
1118
1119 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1120
1121 static bool
1122 function_declarator_p (const cp_declarator *declarator)
1123 {
1124   while (declarator)
1125     {
1126       if (declarator->kind == cdk_function
1127           && declarator->declarator->kind == cdk_id)
1128         return true;
1129       if (declarator->kind == cdk_id
1130           || declarator->kind == cdk_error)
1131         return false;
1132       declarator = declarator->declarator;
1133     }
1134   return false;
1135 }
1136  
1137 /* The parser.  */
1138
1139 /* Overview
1140    --------
1141
1142    A cp_parser parses the token stream as specified by the C++
1143    grammar.  Its job is purely parsing, not semantic analysis.  For
1144    example, the parser breaks the token stream into declarators,
1145    expressions, statements, and other similar syntactic constructs.
1146    It does not check that the types of the expressions on either side
1147    of an assignment-statement are compatible, or that a function is
1148    not declared with a parameter of type `void'.
1149
1150    The parser invokes routines elsewhere in the compiler to perform
1151    semantic analysis and to build up the abstract syntax tree for the
1152    code processed.
1153
1154    The parser (and the template instantiation code, which is, in a
1155    way, a close relative of parsing) are the only parts of the
1156    compiler that should be calling push_scope and pop_scope, or
1157    related functions.  The parser (and template instantiation code)
1158    keeps track of what scope is presently active; everything else
1159    should simply honor that.  (The code that generates static
1160    initializers may also need to set the scope, in order to check
1161    access control correctly when emitting the initializers.)
1162
1163    Methodology
1164    -----------
1165
1166    The parser is of the standard recursive-descent variety.  Upcoming
1167    tokens in the token stream are examined in order to determine which
1168    production to use when parsing a non-terminal.  Some C++ constructs
1169    require arbitrary look ahead to disambiguate.  For example, it is
1170    impossible, in the general case, to tell whether a statement is an
1171    expression or declaration without scanning the entire statement.
1172    Therefore, the parser is capable of "parsing tentatively."  When the
1173    parser is not sure what construct comes next, it enters this mode.
1174    Then, while we attempt to parse the construct, the parser queues up
1175    error messages, rather than issuing them immediately, and saves the
1176    tokens it consumes.  If the construct is parsed successfully, the
1177    parser "commits", i.e., it issues any queued error messages and
1178    the tokens that were being preserved are permanently discarded.
1179    If, however, the construct is not parsed successfully, the parser
1180    rolls back its state completely so that it can resume parsing using
1181    a different alternative.
1182
1183    Future Improvements
1184    -------------------
1185
1186    The performance of the parser could probably be improved substantially.
1187    We could often eliminate the need to parse tentatively by looking ahead
1188    a little bit.  In some places, this approach might not entirely eliminate
1189    the need to parse tentatively, but it might still speed up the average
1190    case.  */
1191
1192 /* Flags that are passed to some parsing functions.  These values can
1193    be bitwise-ored together.  */
1194
1195 typedef enum cp_parser_flags
1196 {
1197   /* No flags.  */
1198   CP_PARSER_FLAGS_NONE = 0x0,
1199   /* The construct is optional.  If it is not present, then no error
1200      should be issued.  */
1201   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1202   /* When parsing a type-specifier, do not allow user-defined types.  */
1203   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1204 } cp_parser_flags;
1205
1206 /* The different kinds of declarators we want to parse.  */
1207
1208 typedef enum cp_parser_declarator_kind
1209 {
1210   /* We want an abstract declarator.  */
1211   CP_PARSER_DECLARATOR_ABSTRACT,
1212   /* We want a named declarator.  */
1213   CP_PARSER_DECLARATOR_NAMED,
1214   /* We don't mind, but the name must be an unqualified-id.  */
1215   CP_PARSER_DECLARATOR_EITHER
1216 } cp_parser_declarator_kind;
1217
1218 /* The precedence values used to parse binary expressions.  The minimum value
1219    of PREC must be 1, because zero is reserved to quickly discriminate
1220    binary operators from other tokens.  */
1221
1222 enum cp_parser_prec
1223 {
1224   PREC_NOT_OPERATOR,
1225   PREC_LOGICAL_OR_EXPRESSION,
1226   PREC_LOGICAL_AND_EXPRESSION,
1227   PREC_INCLUSIVE_OR_EXPRESSION,
1228   PREC_EXCLUSIVE_OR_EXPRESSION,
1229   PREC_AND_EXPRESSION,
1230   PREC_EQUALITY_EXPRESSION,
1231   PREC_RELATIONAL_EXPRESSION,
1232   PREC_SHIFT_EXPRESSION,
1233   PREC_ADDITIVE_EXPRESSION,
1234   PREC_MULTIPLICATIVE_EXPRESSION,
1235   PREC_PM_EXPRESSION,
1236   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1237 };
1238
1239 /* A mapping from a token type to a corresponding tree node type, with a
1240    precedence value.  */
1241
1242 typedef struct cp_parser_binary_operations_map_node
1243 {
1244   /* The token type.  */
1245   enum cpp_ttype token_type;
1246   /* The corresponding tree code.  */
1247   enum tree_code tree_type;
1248   /* The precedence of this operator.  */
1249   enum cp_parser_prec prec;
1250 } cp_parser_binary_operations_map_node;
1251
1252 /* The status of a tentative parse.  */
1253
1254 typedef enum cp_parser_status_kind
1255 {
1256   /* No errors have occurred.  */
1257   CP_PARSER_STATUS_KIND_NO_ERROR,
1258   /* An error has occurred.  */
1259   CP_PARSER_STATUS_KIND_ERROR,
1260   /* We are committed to this tentative parse, whether or not an error
1261      has occurred.  */
1262   CP_PARSER_STATUS_KIND_COMMITTED
1263 } cp_parser_status_kind;
1264
1265 typedef struct cp_parser_expression_stack_entry
1266 {
1267   /* Left hand side of the binary operation we are currently
1268      parsing.  */
1269   tree lhs;
1270   /* Original tree code for left hand side, if it was a binary
1271      expression itself (used for -Wparentheses).  */
1272   enum tree_code lhs_type;
1273   /* Tree code for the binary operation we are parsing.  */
1274   enum tree_code tree_type;
1275   /* Precedence of the binary operation we are parsing.  */
1276   int prec;
1277 } cp_parser_expression_stack_entry;
1278
1279 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1280    entries because precedence levels on the stack are monotonically
1281    increasing.  */
1282 typedef struct cp_parser_expression_stack_entry
1283   cp_parser_expression_stack[NUM_PREC_VALUES];
1284
1285 /* Context that is saved and restored when parsing tentatively.  */
1286 typedef struct cp_parser_context GTY (())
1287 {
1288   /* If this is a tentative parsing context, the status of the
1289      tentative parse.  */
1290   enum cp_parser_status_kind status;
1291   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1292      that are looked up in this context must be looked up both in the
1293      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1294      the context of the containing expression.  */
1295   tree object_type;
1296
1297   /* The next parsing context in the stack.  */
1298   struct cp_parser_context *next;
1299 } cp_parser_context;
1300
1301 /* Prototypes.  */
1302
1303 /* Constructors and destructors.  */
1304
1305 static cp_parser_context *cp_parser_context_new
1306   (cp_parser_context *);
1307
1308 /* Class variables.  */
1309
1310 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1311
1312 /* The operator-precedence table used by cp_parser_binary_expression.
1313    Transformed into an associative array (binops_by_token) by
1314    cp_parser_new.  */
1315
1316 static const cp_parser_binary_operations_map_node binops[] = {
1317   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1318   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1319
1320   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1323
1324   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1326
1327   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1329
1330   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1332   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1334
1335   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1336   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1337
1338   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1339
1340   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1343
1344   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1345
1346   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 };
1348
1349 /* The same as binops, but initialized by cp_parser_new so that
1350    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1351    for speed.  */
1352 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1353
1354 /* Constructors and destructors.  */
1355
1356 /* Construct a new context.  The context below this one on the stack
1357    is given by NEXT.  */
1358
1359 static cp_parser_context *
1360 cp_parser_context_new (cp_parser_context* next)
1361 {
1362   cp_parser_context *context;
1363
1364   /* Allocate the storage.  */
1365   if (cp_parser_context_free_list != NULL)
1366     {
1367       /* Pull the first entry from the free list.  */
1368       context = cp_parser_context_free_list;
1369       cp_parser_context_free_list = context->next;
1370       memset (context, 0, sizeof (*context));
1371     }
1372   else
1373     context = GGC_CNEW (cp_parser_context);
1374
1375   /* No errors have occurred yet in this context.  */
1376   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1377   /* If this is not the bottommost context, copy information that we
1378      need from the previous context.  */
1379   if (next)
1380     {
1381       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1382          expression, then we are parsing one in this context, too.  */
1383       context->object_type = next->object_type;
1384       /* Thread the stack.  */
1385       context->next = next;
1386     }
1387
1388   return context;
1389 }
1390
1391 /* The cp_parser structure represents the C++ parser.  */
1392
1393 typedef struct cp_parser GTY(())
1394 {
1395   /* The lexer from which we are obtaining tokens.  */
1396   cp_lexer *lexer;
1397
1398   /* The scope in which names should be looked up.  If NULL_TREE, then
1399      we look up names in the scope that is currently open in the
1400      source program.  If non-NULL, this is either a TYPE or
1401      NAMESPACE_DECL for the scope in which we should look.  It can
1402      also be ERROR_MARK, when we've parsed a bogus scope.
1403
1404      This value is not cleared automatically after a name is looked
1405      up, so we must be careful to clear it before starting a new look
1406      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1407      will look up `Z' in the scope of `X', rather than the current
1408      scope.)  Unfortunately, it is difficult to tell when name lookup
1409      is complete, because we sometimes peek at a token, look it up,
1410      and then decide not to consume it.   */
1411   tree scope;
1412
1413   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1414      last lookup took place.  OBJECT_SCOPE is used if an expression
1415      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1416      respectively.  QUALIFYING_SCOPE is used for an expression of the
1417      form "X::Y"; it refers to X.  */
1418   tree object_scope;
1419   tree qualifying_scope;
1420
1421   /* A stack of parsing contexts.  All but the bottom entry on the
1422      stack will be tentative contexts.
1423
1424      We parse tentatively in order to determine which construct is in
1425      use in some situations.  For example, in order to determine
1426      whether a statement is an expression-statement or a
1427      declaration-statement we parse it tentatively as a
1428      declaration-statement.  If that fails, we then reparse the same
1429      token stream as an expression-statement.  */
1430   cp_parser_context *context;
1431
1432   /* True if we are parsing GNU C++.  If this flag is not set, then
1433      GNU extensions are not recognized.  */
1434   bool allow_gnu_extensions_p;
1435
1436   /* TRUE if the `>' token should be interpreted as the greater-than
1437      operator.  FALSE if it is the end of a template-id or
1438      template-parameter-list. In C++0x mode, this flag also applies to
1439      `>>' tokens, which are viewed as two consecutive `>' tokens when
1440      this flag is FALSE.  */
1441   bool greater_than_is_operator_p;
1442
1443   /* TRUE if default arguments are allowed within a parameter list
1444      that starts at this point. FALSE if only a gnu extension makes
1445      them permissible.  */
1446   bool default_arg_ok_p;
1447
1448   /* TRUE if we are parsing an integral constant-expression.  See
1449      [expr.const] for a precise definition.  */
1450   bool integral_constant_expression_p;
1451
1452   /* TRUE if we are parsing an integral constant-expression -- but a
1453      non-constant expression should be permitted as well.  This flag
1454      is used when parsing an array bound so that GNU variable-length
1455      arrays are tolerated.  */
1456   bool allow_non_integral_constant_expression_p;
1457
1458   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1459      been seen that makes the expression non-constant.  */
1460   bool non_integral_constant_expression_p;
1461
1462   /* TRUE if local variable names and `this' are forbidden in the
1463      current context.  */
1464   bool local_variables_forbidden_p;
1465
1466   /* TRUE if the declaration we are parsing is part of a
1467      linkage-specification of the form `extern string-literal
1468      declaration'.  */
1469   bool in_unbraced_linkage_specification_p;
1470
1471   /* TRUE if we are presently parsing a declarator, after the
1472      direct-declarator.  */
1473   bool in_declarator_p;
1474
1475   /* TRUE if we are presently parsing a template-argument-list.  */
1476   bool in_template_argument_list_p;
1477
1478   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1479      to IN_OMP_BLOCK if parsing OpenMP structured block and
1480      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1481      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1482      iteration-statement, OpenMP block or loop within that switch.  */
1483 #define IN_SWITCH_STMT          1
1484 #define IN_ITERATION_STMT       2
1485 #define IN_OMP_BLOCK            4
1486 #define IN_OMP_FOR              8
1487 #define IN_IF_STMT             16
1488   unsigned char in_statement;
1489
1490   /* TRUE if we are presently parsing the body of a switch statement.
1491      Note that this doesn't quite overlap with in_statement above.
1492      The difference relates to giving the right sets of error messages:
1493      "case not in switch" vs "break statement used with OpenMP...".  */
1494   bool in_switch_statement_p;
1495
1496   /* TRUE if we are parsing a type-id in an expression context.  In
1497      such a situation, both "type (expr)" and "type (type)" are valid
1498      alternatives.  */
1499   bool in_type_id_in_expr_p;
1500
1501   /* TRUE if we are currently in a header file where declarations are
1502      implicitly extern "C".  */
1503   bool implicit_extern_c;
1504
1505   /* TRUE if strings in expressions should be translated to the execution
1506      character set.  */
1507   bool translate_strings_p;
1508
1509   /* TRUE if we are presently parsing the body of a function, but not
1510      a local class.  */
1511   bool in_function_body;
1512
1513   /* If non-NULL, then we are parsing a construct where new type
1514      definitions are not permitted.  The string stored here will be
1515      issued as an error message if a type is defined.  */
1516   const char *type_definition_forbidden_message;
1517
1518   /* A list of lists. The outer list is a stack, used for member
1519      functions of local classes. At each level there are two sub-list,
1520      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1521      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1522      TREE_VALUE's. The functions are chained in reverse declaration
1523      order.
1524
1525      The TREE_PURPOSE sublist contains those functions with default
1526      arguments that need post processing, and the TREE_VALUE sublist
1527      contains those functions with definitions that need post
1528      processing.
1529
1530      These lists can only be processed once the outermost class being
1531      defined is complete.  */
1532   tree unparsed_functions_queues;
1533
1534   /* The number of classes whose definitions are currently in
1535      progress.  */
1536   unsigned num_classes_being_defined;
1537
1538   /* The number of template parameter lists that apply directly to the
1539      current declaration.  */
1540   unsigned num_template_parameter_lists;
1541 } cp_parser;
1542
1543 /* Prototypes.  */
1544
1545 /* Constructors and destructors.  */
1546
1547 static cp_parser *cp_parser_new
1548   (void);
1549
1550 /* Routines to parse various constructs.
1551
1552    Those that return `tree' will return the error_mark_node (rather
1553    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1554    Sometimes, they will return an ordinary node if error-recovery was
1555    attempted, even though a parse error occurred.  So, to check
1556    whether or not a parse error occurred, you should always use
1557    cp_parser_error_occurred.  If the construct is optional (indicated
1558    either by an `_opt' in the name of the function that does the
1559    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1560    the construct is not present.  */
1561
1562 /* Lexical conventions [gram.lex]  */
1563
1564 static tree cp_parser_identifier
1565   (cp_parser *);
1566 static tree cp_parser_string_literal
1567   (cp_parser *, bool, bool);
1568
1569 /* Basic concepts [gram.basic]  */
1570
1571 static bool cp_parser_translation_unit
1572   (cp_parser *);
1573
1574 /* Expressions [gram.expr]  */
1575
1576 static tree cp_parser_primary_expression
1577   (cp_parser *, bool, bool, bool, cp_id_kind *);
1578 static tree cp_parser_id_expression
1579   (cp_parser *, bool, bool, bool *, bool, bool);
1580 static tree cp_parser_unqualified_id
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier_opt
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier
1585   (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_qualifying_entity
1587   (cp_parser *, bool, bool, bool, bool, bool);
1588 static tree cp_parser_postfix_expression
1589   (cp_parser *, bool, bool, bool, cp_id_kind *);
1590 static tree cp_parser_postfix_open_square_expression
1591   (cp_parser *, tree, bool);
1592 static tree cp_parser_postfix_dot_deref_expression
1593   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1594 static tree cp_parser_parenthesized_expression_list
1595   (cp_parser *, bool, bool, bool, bool *);
1596 static void cp_parser_pseudo_destructor_name
1597   (cp_parser *, tree *, tree *);
1598 static tree cp_parser_unary_expression
1599   (cp_parser *, bool, bool, cp_id_kind *);
1600 static enum tree_code cp_parser_unary_operator
1601   (cp_token *);
1602 static tree cp_parser_new_expression
1603   (cp_parser *);
1604 static tree cp_parser_new_placement
1605   (cp_parser *);
1606 static tree cp_parser_new_type_id
1607   (cp_parser *, tree *);
1608 static cp_declarator *cp_parser_new_declarator_opt
1609   (cp_parser *);
1610 static cp_declarator *cp_parser_direct_new_declarator
1611   (cp_parser *);
1612 static tree cp_parser_new_initializer
1613   (cp_parser *);
1614 static tree cp_parser_delete_expression
1615   (cp_parser *);
1616 static tree cp_parser_cast_expression
1617   (cp_parser *, bool, bool, cp_id_kind *);
1618 static tree cp_parser_binary_expression
1619   (cp_parser *, bool, enum cp_parser_prec, cp_id_kind *);
1620 static tree cp_parser_question_colon_clause
1621   (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623   (cp_parser *, bool, cp_id_kind *);
1624 static enum tree_code cp_parser_assignment_operator_opt
1625   (cp_parser *);
1626 static tree cp_parser_expression
1627   (cp_parser *, bool, cp_id_kind *);
1628 static tree cp_parser_constant_expression
1629   (cp_parser *, bool, bool *);
1630 static tree cp_parser_builtin_offsetof
1631   (cp_parser *);
1632
1633 /* Statements [gram.stmt.stmt]  */
1634
1635 static void cp_parser_statement
1636   (cp_parser *, tree, bool, bool *);
1637 static void cp_parser_label_for_labeled_statement
1638   (cp_parser *);
1639 static tree cp_parser_expression_statement
1640   (cp_parser *, tree);
1641 static tree cp_parser_compound_statement
1642   (cp_parser *, tree, bool);
1643 static void cp_parser_statement_seq_opt
1644   (cp_parser *, tree);
1645 static tree cp_parser_selection_statement
1646   (cp_parser *, bool *);
1647 static tree cp_parser_condition
1648   (cp_parser *);
1649 static tree cp_parser_iteration_statement
1650   (cp_parser *);
1651 static void cp_parser_for_init_statement
1652   (cp_parser *);
1653 static tree cp_parser_jump_statement
1654   (cp_parser *);
1655 static void cp_parser_declaration_statement
1656   (cp_parser *);
1657
1658 static tree cp_parser_implicitly_scoped_statement
1659   (cp_parser *, bool *);
1660 static void cp_parser_already_scoped_statement
1661   (cp_parser *);
1662
1663 /* Declarations [gram.dcl.dcl] */
1664
1665 static void cp_parser_declaration_seq_opt
1666   (cp_parser *);
1667 static void cp_parser_declaration
1668   (cp_parser *);
1669 static void cp_parser_block_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_simple_declaration
1672   (cp_parser *, bool);
1673 static void cp_parser_decl_specifier_seq
1674   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1675 static tree cp_parser_storage_class_specifier_opt
1676   (cp_parser *);
1677 static tree cp_parser_function_specifier_opt
1678   (cp_parser *, cp_decl_specifier_seq *);
1679 static tree cp_parser_type_specifier
1680   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1681    int *, bool *);
1682 static tree cp_parser_simple_type_specifier
1683   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1684 static tree cp_parser_type_name
1685   (cp_parser *);
1686 static tree cp_parser_nonclass_name 
1687   (cp_parser* parser);
1688 static tree cp_parser_elaborated_type_specifier
1689   (cp_parser *, bool, bool);
1690 static tree cp_parser_enum_specifier
1691   (cp_parser *);
1692 static void cp_parser_enumerator_list
1693   (cp_parser *, tree);
1694 static void cp_parser_enumerator_definition
1695   (cp_parser *, tree);
1696 static tree cp_parser_namespace_name
1697   (cp_parser *);
1698 static void cp_parser_namespace_definition
1699   (cp_parser *);
1700 static void cp_parser_namespace_body
1701   (cp_parser *);
1702 static tree cp_parser_qualified_namespace_specifier
1703   (cp_parser *);
1704 static void cp_parser_namespace_alias_definition
1705   (cp_parser *);
1706 static bool cp_parser_using_declaration
1707   (cp_parser *, bool);
1708 static void cp_parser_using_directive
1709   (cp_parser *);
1710 static void cp_parser_asm_definition
1711   (cp_parser *);
1712 static void cp_parser_linkage_specification
1713   (cp_parser *);
1714 static void cp_parser_static_assert
1715   (cp_parser *, bool);
1716 static tree cp_parser_decltype
1717   (cp_parser *);
1718
1719 /* Declarators [gram.dcl.decl] */
1720
1721 static tree cp_parser_init_declarator
1722   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1723 static cp_declarator *cp_parser_declarator
1724   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1725 static cp_declarator *cp_parser_direct_declarator
1726   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1727 static enum tree_code cp_parser_ptr_operator
1728   (cp_parser *, tree *, cp_cv_quals *);
1729 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1730   (cp_parser *);
1731 static tree cp_parser_late_return_type_opt
1732   (cp_parser *);
1733 static tree cp_parser_declarator_id
1734   (cp_parser *, bool);
1735 static tree cp_parser_type_id
1736   (cp_parser *);
1737 static void cp_parser_type_specifier_seq
1738   (cp_parser *, bool, cp_decl_specifier_seq *);
1739 static tree cp_parser_parameter_declaration_clause
1740   (cp_parser *);
1741 static tree cp_parser_parameter_declaration_list
1742   (cp_parser *, bool *);
1743 static cp_parameter_declarator *cp_parser_parameter_declaration
1744   (cp_parser *, bool, bool *);
1745 static tree cp_parser_default_argument 
1746   (cp_parser *, bool);
1747 static void cp_parser_function_body
1748   (cp_parser *);
1749 static tree cp_parser_initializer
1750   (cp_parser *, bool *, bool *);
1751 static tree cp_parser_initializer_clause
1752   (cp_parser *, bool *);
1753 static tree cp_parser_braced_list
1754   (cp_parser*, bool*);
1755 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1756   (cp_parser *, bool *);
1757
1758 static bool cp_parser_ctor_initializer_opt_and_function_body
1759   (cp_parser *);
1760
1761 /* Classes [gram.class] */
1762
1763 static tree cp_parser_class_name
1764   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1765 static tree cp_parser_class_specifier
1766   (cp_parser *);
1767 static tree cp_parser_class_head
1768   (cp_parser *, bool *, tree *, tree *);
1769 static enum tag_types cp_parser_class_key
1770   (cp_parser *);
1771 static void cp_parser_member_specification_opt
1772   (cp_parser *);
1773 static void cp_parser_member_declaration
1774   (cp_parser *);
1775 static tree cp_parser_pure_specifier
1776   (cp_parser *);
1777 static tree cp_parser_constant_initializer
1778   (cp_parser *);
1779
1780 /* Derived classes [gram.class.derived] */
1781
1782 static tree cp_parser_base_clause
1783   (cp_parser *);
1784 static tree cp_parser_base_specifier
1785   (cp_parser *);
1786
1787 /* Special member functions [gram.special] */
1788
1789 static tree cp_parser_conversion_function_id
1790   (cp_parser *);
1791 static tree cp_parser_conversion_type_id
1792   (cp_parser *);
1793 static cp_declarator *cp_parser_conversion_declarator_opt
1794   (cp_parser *);
1795 static bool cp_parser_ctor_initializer_opt
1796   (cp_parser *);
1797 static void cp_parser_mem_initializer_list
1798   (cp_parser *);
1799 static tree cp_parser_mem_initializer
1800   (cp_parser *);
1801 static tree cp_parser_mem_initializer_id
1802   (cp_parser *);
1803
1804 /* Overloading [gram.over] */
1805
1806 static tree cp_parser_operator_function_id
1807   (cp_parser *);
1808 static tree cp_parser_operator
1809   (cp_parser *);
1810
1811 /* Templates [gram.temp] */
1812
1813 static void cp_parser_template_declaration
1814   (cp_parser *, bool);
1815 static tree cp_parser_template_parameter_list
1816   (cp_parser *);
1817 static tree cp_parser_template_parameter
1818   (cp_parser *, bool *, bool *);
1819 static tree cp_parser_type_parameter
1820   (cp_parser *, bool *);
1821 static tree cp_parser_template_id
1822   (cp_parser *, bool, bool, bool);
1823 static tree cp_parser_template_name
1824   (cp_parser *, bool, bool, bool, bool *);
1825 static tree cp_parser_template_argument_list
1826   (cp_parser *);
1827 static tree cp_parser_template_argument
1828   (cp_parser *);
1829 static void cp_parser_explicit_instantiation
1830   (cp_parser *);
1831 static void cp_parser_explicit_specialization
1832   (cp_parser *);
1833
1834 /* Exception handling [gram.exception] */
1835
1836 static tree cp_parser_try_block
1837   (cp_parser *);
1838 static bool cp_parser_function_try_block
1839   (cp_parser *);
1840 static void cp_parser_handler_seq
1841   (cp_parser *);
1842 static void cp_parser_handler
1843   (cp_parser *);
1844 static tree cp_parser_exception_declaration
1845   (cp_parser *);
1846 static tree cp_parser_throw_expression
1847   (cp_parser *);
1848 static tree cp_parser_exception_specification_opt
1849   (cp_parser *);
1850 static tree cp_parser_type_id_list
1851   (cp_parser *);
1852
1853 /* GNU Extensions */
1854
1855 static tree cp_parser_asm_specification_opt
1856   (cp_parser *);
1857 static tree cp_parser_asm_operand_list
1858   (cp_parser *);
1859 static tree cp_parser_asm_clobber_list
1860   (cp_parser *);
1861 static tree cp_parser_attributes_opt
1862   (cp_parser *);
1863 static tree cp_parser_attribute_list
1864   (cp_parser *);
1865 static bool cp_parser_extension_opt
1866   (cp_parser *, int *);
1867 static void cp_parser_label_declaration
1868   (cp_parser *);
1869
1870 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1871 static bool cp_parser_pragma
1872   (cp_parser *, enum pragma_context);
1873
1874 /* Objective-C++ Productions */
1875
1876 static tree cp_parser_objc_message_receiver
1877   (cp_parser *);
1878 static tree cp_parser_objc_message_args
1879   (cp_parser *);
1880 static tree cp_parser_objc_message_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_encode_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_defs_expression
1885   (cp_parser *);
1886 static tree cp_parser_objc_protocol_expression
1887   (cp_parser *);
1888 static tree cp_parser_objc_selector_expression
1889   (cp_parser *);
1890 static tree cp_parser_objc_expression
1891   (cp_parser *);
1892 static bool cp_parser_objc_selector_p
1893   (enum cpp_ttype);
1894 static tree cp_parser_objc_selector
1895   (cp_parser *);
1896 static tree cp_parser_objc_protocol_refs_opt
1897   (cp_parser *);
1898 static void cp_parser_objc_declaration
1899   (cp_parser *);
1900 static tree cp_parser_objc_statement
1901   (cp_parser *);
1902
1903 /* Utility Routines */
1904
1905 static tree cp_parser_lookup_name
1906   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1907 static tree cp_parser_lookup_name_simple
1908   (cp_parser *, tree, location_t);
1909 static tree cp_parser_maybe_treat_template_as_class
1910   (tree, bool);
1911 static bool cp_parser_check_declarator_template_parameters
1912   (cp_parser *, cp_declarator *, location_t);
1913 static bool cp_parser_check_template_parameters
1914   (cp_parser *, unsigned, location_t);
1915 static tree cp_parser_simple_cast_expression
1916   (cp_parser *);
1917 static tree cp_parser_global_scope_opt
1918   (cp_parser *, bool);
1919 static bool cp_parser_constructor_declarator_p
1920   (cp_parser *, bool);
1921 static tree cp_parser_function_definition_from_specifiers_and_declarator
1922   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1923 static tree cp_parser_function_definition_after_declarator
1924   (cp_parser *, bool);
1925 static void cp_parser_template_declaration_after_export
1926   (cp_parser *, bool);
1927 static void cp_parser_perform_template_parameter_access_checks
1928   (VEC (deferred_access_check,gc)*);
1929 static tree cp_parser_single_declaration
1930   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1931 static tree cp_parser_functional_cast
1932   (cp_parser *, tree);
1933 static tree cp_parser_save_member_function_body
1934   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1935 static tree cp_parser_enclosed_template_argument_list
1936   (cp_parser *);
1937 static void cp_parser_save_default_args
1938   (cp_parser *, tree);
1939 static void cp_parser_late_parsing_for_member
1940   (cp_parser *, tree);
1941 static void cp_parser_late_parsing_default_args
1942   (cp_parser *, tree);
1943 static tree cp_parser_sizeof_operand
1944   (cp_parser *, enum rid);
1945 static tree cp_parser_trait_expr
1946   (cp_parser *, enum rid);
1947 static bool cp_parser_declares_only_class_p
1948   (cp_parser *);
1949 static void cp_parser_set_storage_class
1950   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1951 static void cp_parser_set_decl_spec_type
1952   (cp_decl_specifier_seq *, tree, location_t, bool);
1953 static bool cp_parser_friend_p
1954   (const cp_decl_specifier_seq *);
1955 static cp_token *cp_parser_require
1956   (cp_parser *, enum cpp_ttype, const char *);
1957 static cp_token *cp_parser_require_keyword
1958   (cp_parser *, enum rid, const char *);
1959 static bool cp_parser_token_starts_function_definition_p
1960   (cp_token *);
1961 static bool cp_parser_next_token_starts_class_definition_p
1962   (cp_parser *);
1963 static bool cp_parser_next_token_ends_template_argument_p
1964   (cp_parser *);
1965 static bool cp_parser_nth_token_starts_template_argument_list_p
1966   (cp_parser *, size_t);
1967 static enum tag_types cp_parser_token_is_class_key
1968   (cp_token *);
1969 static void cp_parser_check_class_key
1970   (enum tag_types, tree type);
1971 static void cp_parser_check_access_in_redeclaration
1972   (tree type, location_t location);
1973 static bool cp_parser_optional_template_keyword
1974   (cp_parser *);
1975 static void cp_parser_pre_parsed_nested_name_specifier
1976   (cp_parser *);
1977 static bool cp_parser_cache_group
1978   (cp_parser *, enum cpp_ttype, unsigned);
1979 static void cp_parser_parse_tentatively
1980   (cp_parser *);
1981 static void cp_parser_commit_to_tentative_parse
1982   (cp_parser *);
1983 static void cp_parser_abort_tentative_parse
1984   (cp_parser *);
1985 static bool cp_parser_parse_definitely
1986   (cp_parser *);
1987 static inline bool cp_parser_parsing_tentatively
1988   (cp_parser *);
1989 static bool cp_parser_uncommitted_to_tentative_parse_p
1990   (cp_parser *);
1991 static void cp_parser_error
1992   (cp_parser *, const char *);
1993 static void cp_parser_name_lookup_error
1994   (cp_parser *, tree, tree, const char *, location_t);
1995 static bool cp_parser_simulate_error
1996   (cp_parser *);
1997 static bool cp_parser_check_type_definition
1998   (cp_parser *);
1999 static void cp_parser_check_for_definition_in_return_type
2000   (cp_declarator *, tree, location_t type_location);
2001 static void cp_parser_check_for_invalid_template_id
2002   (cp_parser *, tree, location_t location);
2003 static bool cp_parser_non_integral_constant_expression
2004   (cp_parser *, const char *);
2005 static void cp_parser_diagnose_invalid_type_name
2006   (cp_parser *, tree, tree, location_t);
2007 static bool cp_parser_parse_and_diagnose_invalid_type_name
2008   (cp_parser *);
2009 static int cp_parser_skip_to_closing_parenthesis
2010   (cp_parser *, bool, bool, bool);
2011 static void cp_parser_skip_to_end_of_statement
2012   (cp_parser *);
2013 static void cp_parser_consume_semicolon_at_end_of_statement
2014   (cp_parser *);
2015 static void cp_parser_skip_to_end_of_block_or_statement
2016   (cp_parser *);
2017 static bool cp_parser_skip_to_closing_brace
2018   (cp_parser *);
2019 static void cp_parser_skip_to_end_of_template_parameter_list
2020   (cp_parser *);
2021 static void cp_parser_skip_to_pragma_eol
2022   (cp_parser*, cp_token *);
2023 static bool cp_parser_error_occurred
2024   (cp_parser *);
2025 static bool cp_parser_allow_gnu_extensions_p
2026   (cp_parser *);
2027 static bool cp_parser_is_string_literal
2028   (cp_token *);
2029 static bool cp_parser_is_keyword
2030   (cp_token *, enum rid);
2031 static tree cp_parser_make_typename_type
2032   (cp_parser *, tree, tree, location_t location);
2033 static cp_declarator * cp_parser_make_indirect_declarator
2034   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2035
2036 /* Returns nonzero if we are parsing tentatively.  */
2037
2038 static inline bool
2039 cp_parser_parsing_tentatively (cp_parser* parser)
2040 {
2041   return parser->context->next != NULL;
2042 }
2043
2044 /* Returns nonzero if TOKEN is a string literal.  */
2045
2046 static bool
2047 cp_parser_is_string_literal (cp_token* token)
2048 {
2049   return (token->type == CPP_STRING ||
2050           token->type == CPP_STRING16 ||
2051           token->type == CPP_STRING32 ||
2052           token->type == CPP_WSTRING);
2053 }
2054
2055 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2056
2057 static bool
2058 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2059 {
2060   return token->keyword == keyword;
2061 }
2062
2063 /* If not parsing tentatively, issue a diagnostic of the form
2064       FILE:LINE: MESSAGE before TOKEN
2065    where TOKEN is the next token in the input stream.  MESSAGE
2066    (specified by the caller) is usually of the form "expected
2067    OTHER-TOKEN".  */
2068
2069 static void
2070 cp_parser_error (cp_parser* parser, const char* message)
2071 {
2072   if (!cp_parser_simulate_error (parser))
2073     {
2074       cp_token *token = cp_lexer_peek_token (parser->lexer);
2075       /* This diagnostic makes more sense if it is tagged to the line
2076          of the token we just peeked at.  */
2077       cp_lexer_set_source_position_from_token (token);
2078
2079       if (token->type == CPP_PRAGMA)
2080         {
2081           error ("%H%<#pragma%> is not allowed here", &token->location);
2082           cp_parser_skip_to_pragma_eol (parser, token);
2083           return;
2084         }
2085
2086       c_parse_error (message,
2087                      /* Because c_parser_error does not understand
2088                         CPP_KEYWORD, keywords are treated like
2089                         identifiers.  */
2090                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2091                      token->u.value);
2092     }
2093 }
2094
2095 /* Issue an error about name-lookup failing.  NAME is the
2096    IDENTIFIER_NODE DECL is the result of
2097    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2098    the thing that we hoped to find.  */
2099
2100 static void
2101 cp_parser_name_lookup_error (cp_parser* parser,
2102                              tree name,
2103                              tree decl,
2104                              const char* desired,
2105                              location_t location)
2106 {
2107   /* If name lookup completely failed, tell the user that NAME was not
2108      declared.  */
2109   if (decl == error_mark_node)
2110     {
2111       if (parser->scope && parser->scope != global_namespace)
2112         error ("%H%<%E::%E%> has not been declared",
2113                &location, parser->scope, name);
2114       else if (parser->scope == global_namespace)
2115         error ("%H%<::%E%> has not been declared", &location, name);
2116       else if (parser->object_scope
2117                && !CLASS_TYPE_P (parser->object_scope))
2118         error ("%Hrequest for member %qE in non-class type %qT",
2119                &location, name, parser->object_scope);
2120       else if (parser->object_scope)
2121         error ("%H%<%T::%E%> has not been declared",
2122                &location, parser->object_scope, name);
2123       else
2124         error ("%H%qE has not been declared", &location, name);
2125     }
2126   else if (parser->scope && parser->scope != global_namespace)
2127     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2128   else if (parser->scope == global_namespace)
2129     error ("%H%<::%E%> %s", &location, name, desired);
2130   else
2131     error ("%H%qE %s", &location, name, desired);
2132 }
2133
2134 /* If we are parsing tentatively, remember that an error has occurred
2135    during this tentative parse.  Returns true if the error was
2136    simulated; false if a message should be issued by the caller.  */
2137
2138 static bool
2139 cp_parser_simulate_error (cp_parser* parser)
2140 {
2141   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2142     {
2143       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2144       return true;
2145     }
2146   return false;
2147 }
2148
2149 /* Check for repeated decl-specifiers.  */
2150
2151 static void
2152 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2153                            location_t location)
2154 {
2155   cp_decl_spec ds;
2156
2157   for (ds = ds_first; ds != ds_last; ++ds)
2158     {
2159       unsigned count = decl_specs->specs[(int)ds];
2160       if (count < 2)
2161         continue;
2162       /* The "long" specifier is a special case because of "long long".  */
2163       if (ds == ds_long)
2164         {
2165           if (count > 2)
2166             error ("%H%<long long long%> is too long for GCC", &location);
2167           else if (pedantic && !in_system_header && warn_long_long
2168                    && cxx_dialect == cxx98)
2169             pedwarn (location, OPT_Wlong_long, 
2170                      "ISO C++ 1998 does not support %<long long%>");
2171         }
2172       else if (count > 1)
2173         {
2174           static const char *const decl_spec_names[] = {
2175             "signed",
2176             "unsigned",
2177             "short",
2178             "long",
2179             "const",
2180             "volatile",
2181             "restrict",
2182             "inline",
2183             "virtual",
2184             "explicit",
2185             "friend",
2186             "typedef",
2187             "__complex",
2188             "__thread"
2189           };
2190           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2191         }
2192     }
2193 }
2194
2195 /* This function is called when a type is defined.  If type
2196    definitions are forbidden at this point, an error message is
2197    issued.  */
2198
2199 static bool
2200 cp_parser_check_type_definition (cp_parser* parser)
2201 {
2202   /* If types are forbidden here, issue a message.  */
2203   if (parser->type_definition_forbidden_message)
2204     {
2205       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206          in the message need to be interpreted.  */
2207       error (parser->type_definition_forbidden_message);
2208       return false;
2209     }
2210   return true;
2211 }
2212
2213 /* This function is called when the DECLARATOR is processed.  The TYPE
2214    was a type defined in the decl-specifiers.  If it is invalid to
2215    define a type in the decl-specifiers for DECLARATOR, an error is
2216    issued. TYPE_LOCATION is the location of TYPE and is used
2217    for error reporting.  */
2218
2219 static void
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221                                                tree type, location_t type_location)
2222 {
2223   /* [dcl.fct] forbids type definitions in return types.
2224      Unfortunately, it's not easy to know whether or not we are
2225      processing a return type until after the fact.  */
2226   while (declarator
2227          && (declarator->kind == cdk_pointer
2228              || declarator->kind == cdk_reference
2229              || declarator->kind == cdk_ptrmem))
2230     declarator = declarator->declarator;
2231   if (declarator
2232       && declarator->kind == cdk_function)
2233     {
2234       error ("%Hnew types may not be defined in a return type", &type_location);
2235       inform (type_location, 
2236               "(perhaps a semicolon is missing after the definition of %qT)",
2237               type);
2238     }
2239 }
2240
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242    "<" in any valid C++ program.  If the next token is indeed "<",
2243    issue a message warning the user about what appears to be an
2244    invalid attempt to form a template-id. LOCATION is the location
2245    of the type-specifier (TYPE) */
2246
2247 static void
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249                                          tree type, location_t location)
2250 {
2251   cp_token_position start = 0;
2252
2253   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2254     {
2255       if (TYPE_P (type))
2256         error ("%H%qT is not a template", &location, type);
2257       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258         error ("%H%qE is not a template", &location, type);
2259       else
2260         error ("%Hinvalid template-id", &location);
2261       /* Remember the location of the invalid "<".  */
2262       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263         start = cp_lexer_token_position (parser->lexer, true);
2264       /* Consume the "<".  */
2265       cp_lexer_consume_token (parser->lexer);
2266       /* Parse the template arguments.  */
2267       cp_parser_enclosed_template_argument_list (parser);
2268       /* Permanently remove the invalid template arguments so that
2269          this error message is not issued again.  */
2270       if (start)
2271         cp_lexer_purge_tokens_after (parser->lexer, start);
2272     }
2273 }
2274
2275 /* If parsing an integral constant-expression, issue an error message
2276    about the fact that THING appeared and return true.  Otherwise,
2277    return false.  In either case, set
2278    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2279
2280 static bool
2281 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2282                                             const char *thing)
2283 {
2284   parser->non_integral_constant_expression_p = true;
2285   if (parser->integral_constant_expression_p)
2286     {
2287       if (!parser->allow_non_integral_constant_expression_p)
2288         {
2289           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290              in the message need to be interpreted.  */
2291           char *message = concat (thing,
2292                                   " cannot appear in a constant-expression",
2293                                   NULL);
2294           error (message);
2295           free (message);
2296           return true;
2297         }
2298     }
2299   return false;
2300 }
2301
2302 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2303    qualifying scope (or NULL, if none) for ID.  This function commits
2304    to the current active tentative parse, if any.  (Otherwise, the
2305    problematic construct might be encountered again later, resulting
2306    in duplicate error messages.) LOCATION is the location of ID.  */
2307
2308 static void
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310                                       tree scope, tree id,
2311                                       location_t location)
2312 {
2313   tree decl, old_scope;
2314   /* Try to lookup the identifier.  */
2315   old_scope = parser->scope;
2316   parser->scope = scope;
2317   decl = cp_parser_lookup_name_simple (parser, id, location);
2318   parser->scope = old_scope;
2319   /* If the lookup found a template-name, it means that the user forgot
2320   to specify an argument list. Emit a useful error message.  */
2321   if (TREE_CODE (decl) == TEMPLATE_DECL)
2322     error ("%Hinvalid use of template-name %qE without an argument list",
2323            &location, decl);
2324   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326   else if (TREE_CODE (decl) == TYPE_DECL)
2327     /* Something like 'unsigned A a;'  */
2328     error ("%Hinvalid combination of multiple type-specifiers",
2329            &location);
2330   else if (!parser->scope)
2331     {
2332       /* Issue an error message.  */
2333       error ("%H%qE does not name a type", &location, id);
2334       /* If we're in a template class, it's possible that the user was
2335          referring to a type from a base class.  For example:
2336
2337            template <typename T> struct A { typedef T X; };
2338            template <typename T> struct B : public A<T> { X x; };
2339
2340          The user should have said "typename A<T>::X".  */
2341       if (processing_template_decl && current_class_type
2342           && TYPE_BINFO (current_class_type))
2343         {
2344           tree b;
2345
2346           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2347                b;
2348                b = TREE_CHAIN (b))
2349             {
2350               tree base_type = BINFO_TYPE (b);
2351               if (CLASS_TYPE_P (base_type)
2352                   && dependent_type_p (base_type))
2353                 {
2354                   tree field;
2355                   /* Go from a particular instantiation of the
2356                      template (which will have an empty TYPE_FIELDs),
2357                      to the main version.  */
2358                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359                   for (field = TYPE_FIELDS (base_type);
2360                        field;
2361                        field = TREE_CHAIN (field))
2362                     if (TREE_CODE (field) == TYPE_DECL
2363                         && DECL_NAME (field) == id)
2364                       {
2365                         inform (location, 
2366                                 "(perhaps %<typename %T::%E%> was intended)",
2367                                 BINFO_TYPE (b), id);
2368                         break;
2369                       }
2370                   if (field)
2371                     break;
2372                 }
2373             }
2374         }
2375     }
2376   /* Here we diagnose qualified-ids where the scope is actually correct,
2377      but the identifier does not resolve to a valid type name.  */
2378   else if (parser->scope != error_mark_node)
2379     {
2380       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381         error ("%H%qE in namespace %qE does not name a type",
2382                &location, id, parser->scope);
2383       else if (TYPE_P (parser->scope))
2384         error ("%H%qE in class %qT does not name a type",
2385                &location, id, parser->scope);
2386       else
2387         gcc_unreachable ();
2388     }
2389   cp_parser_commit_to_tentative_parse (parser);
2390 }
2391
2392 /* Check for a common situation where a type-name should be present,
2393    but is not, and issue a sensible error message.  Returns true if an
2394    invalid type-name was detected.
2395
2396    The situation handled by this function are variable declarations of the
2397    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398    Usually, `ID' should name a type, but if we got here it means that it
2399    does not. We try to emit the best possible error message depending on
2400    how exactly the id-expression looks like.  */
2401
2402 static bool
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2404 {
2405   tree id;
2406   cp_token *token = cp_lexer_peek_token (parser->lexer);
2407
2408   cp_parser_parse_tentatively (parser);
2409   id = cp_parser_id_expression (parser,
2410                                 /*template_keyword_p=*/false,
2411                                 /*check_dependency_p=*/true,
2412                                 /*template_p=*/NULL,
2413                                 /*declarator_p=*/true,
2414                                 /*optional_p=*/false);
2415   /* After the id-expression, there should be a plain identifier,
2416      otherwise this is not a simple variable declaration. Also, if
2417      the scope is dependent, we cannot do much.  */
2418   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419       || (parser->scope && TYPE_P (parser->scope)
2420           && dependent_type_p (parser->scope))
2421       || TREE_CODE (id) == TYPE_DECL)
2422     {
2423       cp_parser_abort_tentative_parse (parser);
2424       return false;
2425     }
2426   if (!cp_parser_parse_definitely (parser))
2427     return false;
2428
2429   /* Emit a diagnostic for the invalid type.  */
2430   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431                                         id, token->location);
2432   /* Skip to the end of the declaration; there's no point in
2433      trying to process it.  */
2434   cp_parser_skip_to_end_of_block_or_statement (parser);
2435   return true;
2436 }
2437
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2440    are doing error recovery. Returns -1 if OR_COMMA is true and we
2441    found an unnested comma.  */
2442
2443 static int
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2445                                        bool recovering,
2446                                        bool or_comma,
2447                                        bool consume_paren)
2448 {
2449   unsigned paren_depth = 0;
2450   unsigned brace_depth = 0;
2451
2452   if (recovering && !or_comma
2453       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2454     return 0;
2455
2456   while (true)
2457     {
2458       cp_token * token = cp_lexer_peek_token (parser->lexer);
2459
2460       switch (token->type)
2461         {
2462         case CPP_EOF:
2463         case CPP_PRAGMA_EOL:
2464           /* If we've run out of tokens, then there is no closing `)'.  */
2465           return 0;
2466
2467         case CPP_SEMICOLON:
2468           /* This matches the processing in skip_to_end_of_statement.  */
2469           if (!brace_depth)
2470             return 0;
2471           break;
2472
2473         case CPP_OPEN_BRACE:
2474           ++brace_depth;
2475           break;
2476         case CPP_CLOSE_BRACE:
2477           if (!brace_depth--)
2478             return 0;
2479           break;
2480
2481         case CPP_COMMA:
2482           if (recovering && or_comma && !brace_depth && !paren_depth)
2483             return -1;
2484           break;
2485
2486         case CPP_OPEN_PAREN:
2487           if (!brace_depth)
2488             ++paren_depth;
2489           break;
2490
2491         case CPP_CLOSE_PAREN:
2492           if (!brace_depth && !paren_depth--)
2493             {
2494               if (consume_paren)
2495                 cp_lexer_consume_token (parser->lexer);
2496               return 1;
2497             }
2498           break;
2499
2500         default:
2501           break;
2502         }
2503
2504       /* Consume the token.  */
2505       cp_lexer_consume_token (parser->lexer);
2506     }
2507 }
2508
2509 /* Consume tokens until we reach the end of the current statement.
2510    Normally, that will be just before consuming a `;'.  However, if a
2511    non-nested `}' comes first, then we stop before consuming that.  */
2512
2513 static void
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2515 {
2516   unsigned nesting_depth = 0;
2517
2518   while (true)
2519     {
2520       cp_token *token = cp_lexer_peek_token (parser->lexer);
2521
2522       switch (token->type)
2523         {
2524         case CPP_EOF:
2525         case CPP_PRAGMA_EOL:
2526           /* If we've run out of tokens, stop.  */
2527           return;
2528
2529         case CPP_SEMICOLON:
2530           /* If the next token is a `;', we have reached the end of the
2531              statement.  */
2532           if (!nesting_depth)
2533             return;
2534           break;
2535
2536         case CPP_CLOSE_BRACE:
2537           /* If this is a non-nested '}', stop before consuming it.
2538              That way, when confronted with something like:
2539
2540                { 3 + }
2541
2542              we stop before consuming the closing '}', even though we
2543              have not yet reached a `;'.  */
2544           if (nesting_depth == 0)
2545             return;
2546
2547           /* If it is the closing '}' for a block that we have
2548              scanned, stop -- but only after consuming the token.
2549              That way given:
2550
2551                 void f g () { ... }
2552                 typedef int I;
2553
2554              we will stop after the body of the erroneously declared
2555              function, but before consuming the following `typedef'
2556              declaration.  */
2557           if (--nesting_depth == 0)
2558             {
2559               cp_lexer_consume_token (parser->lexer);
2560               return;
2561             }
2562
2563         case CPP_OPEN_BRACE:
2564           ++nesting_depth;
2565           break;
2566
2567         default:
2568           break;
2569         }
2570
2571       /* Consume the token.  */
2572       cp_lexer_consume_token (parser->lexer);
2573     }
2574 }
2575
2576 /* This function is called at the end of a statement or declaration.
2577    If the next token is a semicolon, it is consumed; otherwise, error
2578    recovery is attempted.  */
2579
2580 static void
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2582 {
2583   /* Look for the trailing `;'.  */
2584   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2585     {
2586       /* If there is additional (erroneous) input, skip to the end of
2587          the statement.  */
2588       cp_parser_skip_to_end_of_statement (parser);
2589       /* If the next token is now a `;', consume it.  */
2590       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591         cp_lexer_consume_token (parser->lexer);
2592     }
2593 }
2594
2595 /* Skip tokens until we have consumed an entire block, or until we
2596    have consumed a non-nested `;'.  */
2597
2598 static void
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2600 {
2601   int nesting_depth = 0;
2602
2603   while (nesting_depth >= 0)
2604     {
2605       cp_token *token = cp_lexer_peek_token (parser->lexer);
2606
2607       switch (token->type)
2608         {
2609         case CPP_EOF:
2610         case CPP_PRAGMA_EOL:
2611           /* If we've run out of tokens, stop.  */
2612           return;
2613
2614         case CPP_SEMICOLON:
2615           /* Stop if this is an unnested ';'. */
2616           if (!nesting_depth)
2617             nesting_depth = -1;
2618           break;
2619
2620         case CPP_CLOSE_BRACE:
2621           /* Stop if this is an unnested '}', or closes the outermost
2622              nesting level.  */
2623           nesting_depth--;
2624           if (!nesting_depth)
2625             nesting_depth = -1;
2626           break;
2627
2628         case CPP_OPEN_BRACE:
2629           /* Nest. */
2630           nesting_depth++;
2631           break;
2632
2633         default:
2634           break;
2635         }
2636
2637       /* Consume the token.  */
2638       cp_lexer_consume_token (parser->lexer);
2639     }
2640 }
2641
2642 /* Skip tokens until a non-nested closing curly brace is the next
2643    token, or there are no more tokens. Return true in the first case,
2644    false otherwise.  */
2645
2646 static bool
2647 cp_parser_skip_to_closing_brace (cp_parser *parser)
2648 {
2649   unsigned nesting_depth = 0;
2650
2651   while (true)
2652     {
2653       cp_token *token = cp_lexer_peek_token (parser->lexer);
2654
2655       switch (token->type)
2656         {
2657         case CPP_EOF:
2658         case CPP_PRAGMA_EOL:
2659           /* If we've run out of tokens, stop.  */
2660           return false;
2661
2662         case CPP_CLOSE_BRACE:
2663           /* If the next token is a non-nested `}', then we have reached
2664              the end of the current block.  */
2665           if (nesting_depth-- == 0)
2666             return true;
2667           break;
2668
2669         case CPP_OPEN_BRACE:
2670           /* If it the next token is a `{', then we are entering a new
2671              block.  Consume the entire block.  */
2672           ++nesting_depth;
2673           break;
2674
2675         default:
2676           break;
2677         }
2678
2679       /* Consume the token.  */
2680       cp_lexer_consume_token (parser->lexer);
2681     }
2682 }
2683
2684 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2685    parameter is the PRAGMA token, allowing us to purge the entire pragma
2686    sequence.  */
2687
2688 static void
2689 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2690 {
2691   cp_token *token;
2692
2693   parser->lexer->in_pragma = false;
2694
2695   do
2696     token = cp_lexer_consume_token (parser->lexer);
2697   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2698
2699   /* Ensure that the pragma is not parsed again.  */
2700   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2701 }
2702
2703 /* Require pragma end of line, resyncing with it as necessary.  The
2704    arguments are as for cp_parser_skip_to_pragma_eol.  */
2705
2706 static void
2707 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2708 {
2709   parser->lexer->in_pragma = false;
2710   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2711     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2712 }
2713
2714 /* This is a simple wrapper around make_typename_type. When the id is
2715    an unresolved identifier node, we can provide a superior diagnostic
2716    using cp_parser_diagnose_invalid_type_name.  */
2717
2718 static tree
2719 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2720                               tree id, location_t id_location)
2721 {
2722   tree result;
2723   if (TREE_CODE (id) == IDENTIFIER_NODE)
2724     {
2725       result = make_typename_type (scope, id, typename_type,
2726                                    /*complain=*/tf_none);
2727       if (result == error_mark_node)
2728         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2729       return result;
2730     }
2731   return make_typename_type (scope, id, typename_type, tf_error);
2732 }
2733
2734 /* This is a wrapper around the
2735    make_{pointer,ptrmem,reference}_declarator functions that decides
2736    which one to call based on the CODE and CLASS_TYPE arguments. The
2737    CODE argument should be one of the values returned by
2738    cp_parser_ptr_operator. */
2739 static cp_declarator *
2740 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2741                                     cp_cv_quals cv_qualifiers,
2742                                     cp_declarator *target)
2743 {
2744   if (code == ERROR_MARK)
2745     return cp_error_declarator;
2746
2747   if (code == INDIRECT_REF)
2748     if (class_type == NULL_TREE)
2749       return make_pointer_declarator (cv_qualifiers, target);
2750     else
2751       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2752   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2753     return make_reference_declarator (cv_qualifiers, target, false);
2754   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2755     return make_reference_declarator (cv_qualifiers, target, true);
2756   gcc_unreachable ();
2757 }
2758
2759 /* Create a new C++ parser.  */
2760
2761 static cp_parser *
2762 cp_parser_new (void)
2763 {
2764   cp_parser *parser;
2765   cp_lexer *lexer;
2766   unsigned i;
2767
2768   /* cp_lexer_new_main is called before calling ggc_alloc because
2769      cp_lexer_new_main might load a PCH file.  */
2770   lexer = cp_lexer_new_main ();
2771
2772   /* Initialize the binops_by_token so that we can get the tree
2773      directly from the token.  */
2774   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2775     binops_by_token[binops[i].token_type] = binops[i];
2776
2777   parser = GGC_CNEW (cp_parser);
2778   parser->lexer = lexer;
2779   parser->context = cp_parser_context_new (NULL);
2780
2781   /* For now, we always accept GNU extensions.  */
2782   parser->allow_gnu_extensions_p = 1;
2783
2784   /* The `>' token is a greater-than operator, not the end of a
2785      template-id.  */
2786   parser->greater_than_is_operator_p = true;
2787
2788   parser->default_arg_ok_p = true;
2789
2790   /* We are not parsing a constant-expression.  */
2791   parser->integral_constant_expression_p = false;
2792   parser->allow_non_integral_constant_expression_p = false;
2793   parser->non_integral_constant_expression_p = false;
2794
2795   /* Local variable names are not forbidden.  */
2796   parser->local_variables_forbidden_p = false;
2797
2798   /* We are not processing an `extern "C"' declaration.  */
2799   parser->in_unbraced_linkage_specification_p = false;
2800
2801   /* We are not processing a declarator.  */
2802   parser->in_declarator_p = false;
2803
2804   /* We are not processing a template-argument-list.  */
2805   parser->in_template_argument_list_p = false;
2806
2807   /* We are not in an iteration statement.  */
2808   parser->in_statement = 0;
2809
2810   /* We are not in a switch statement.  */
2811   parser->in_switch_statement_p = false;
2812
2813   /* We are not parsing a type-id inside an expression.  */
2814   parser->in_type_id_in_expr_p = false;
2815
2816   /* Declarations aren't implicitly extern "C".  */
2817   parser->implicit_extern_c = false;
2818
2819   /* String literals should be translated to the execution character set.  */
2820   parser->translate_strings_p = true;
2821
2822   /* We are not parsing a function body.  */
2823   parser->in_function_body = false;
2824
2825   /* The unparsed function queue is empty.  */
2826   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2827
2828   /* There are no classes being defined.  */
2829   parser->num_classes_being_defined = 0;
2830
2831   /* No template parameters apply.  */
2832   parser->num_template_parameter_lists = 0;
2833
2834   return parser;
2835 }
2836
2837 /* Create a cp_lexer structure which will emit the tokens in CACHE
2838    and push it onto the parser's lexer stack.  This is used for delayed
2839    parsing of in-class method bodies and default arguments, and should
2840    not be confused with tentative parsing.  */
2841 static void
2842 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2843 {
2844   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2845   lexer->next = parser->lexer;
2846   parser->lexer = lexer;
2847
2848   /* Move the current source position to that of the first token in the
2849      new lexer.  */
2850   cp_lexer_set_source_position_from_token (lexer->next_token);
2851 }
2852
2853 /* Pop the top lexer off the parser stack.  This is never used for the
2854    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2855 static void
2856 cp_parser_pop_lexer (cp_parser *parser)
2857 {
2858   cp_lexer *lexer = parser->lexer;
2859   parser->lexer = lexer->next;
2860   cp_lexer_destroy (lexer);
2861
2862   /* Put the current source position back where it was before this
2863      lexer was pushed.  */
2864   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2865 }
2866
2867 /* Lexical conventions [gram.lex]  */
2868
2869 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2870    identifier.  */
2871
2872 static tree
2873 cp_parser_identifier (cp_parser* parser)
2874 {
2875   cp_token *token;
2876
2877   /* Look for the identifier.  */
2878   token = cp_parser_require (parser, CPP_NAME, "identifier");
2879   /* Return the value.  */
2880   return token ? token->u.value : error_mark_node;
2881 }
2882
2883 /* Parse a sequence of adjacent string constants.  Returns a
2884    TREE_STRING representing the combined, nul-terminated string
2885    constant.  If TRANSLATE is true, translate the string to the
2886    execution character set.  If WIDE_OK is true, a wide string is
2887    invalid here.
2888
2889    C++98 [lex.string] says that if a narrow string literal token is
2890    adjacent to a wide string literal token, the behavior is undefined.
2891    However, C99 6.4.5p4 says that this results in a wide string literal.
2892    We follow C99 here, for consistency with the C front end.
2893
2894    This code is largely lifted from lex_string() in c-lex.c.
2895
2896    FUTURE: ObjC++ will need to handle @-strings here.  */
2897 static tree
2898 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2899 {
2900   tree value;
2901   size_t count;
2902   struct obstack str_ob;
2903   cpp_string str, istr, *strs;
2904   cp_token *tok;
2905   enum cpp_ttype type;
2906
2907   tok = cp_lexer_peek_token (parser->lexer);
2908   if (!cp_parser_is_string_literal (tok))
2909     {
2910       cp_parser_error (parser, "expected string-literal");
2911       return error_mark_node;
2912     }
2913
2914   type = tok->type;
2915
2916   /* Try to avoid the overhead of creating and destroying an obstack
2917      for the common case of just one string.  */
2918   if (!cp_parser_is_string_literal
2919       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2920     {
2921       cp_lexer_consume_token (parser->lexer);
2922
2923       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2924       str.len = TREE_STRING_LENGTH (tok->u.value);
2925       count = 1;
2926
2927       strs = &str;
2928     }
2929   else
2930     {
2931       gcc_obstack_init (&str_ob);
2932       count = 0;
2933
2934       do
2935         {
2936           cp_lexer_consume_token (parser->lexer);
2937           count++;
2938           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2939           str.len = TREE_STRING_LENGTH (tok->u.value);
2940
2941           if (type != tok->type)
2942             {
2943               if (type == CPP_STRING)
2944                 type = tok->type;
2945               else if (tok->type != CPP_STRING)
2946                 error ("%Hunsupported non-standard concatenation "
2947                        "of string literals", &tok->location);
2948             }
2949
2950           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2951
2952           tok = cp_lexer_peek_token (parser->lexer);
2953         }
2954       while (cp_parser_is_string_literal (tok));
2955
2956       strs = (cpp_string *) obstack_finish (&str_ob);
2957     }
2958
2959   if (type != CPP_STRING && !wide_ok)
2960     {
2961       cp_parser_error (parser, "a wide string is invalid in this context");
2962       type = CPP_STRING;
2963     }
2964
2965   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2966       (parse_in, strs, count, &istr, type))
2967     {
2968       value = build_string (istr.len, (const char *)istr.text);
2969       free (CONST_CAST (unsigned char *, istr.text));
2970
2971       switch (type)
2972         {
2973         default:
2974         case CPP_STRING:
2975           TREE_TYPE (value) = char_array_type_node;
2976           break;
2977         case CPP_STRING16:
2978           TREE_TYPE (value) = char16_array_type_node;
2979           break;
2980         case CPP_STRING32:
2981           TREE_TYPE (value) = char32_array_type_node;
2982           break;
2983         case CPP_WSTRING:
2984           TREE_TYPE (value) = wchar_array_type_node;
2985           break;
2986         }
2987
2988       value = fix_string_type (value);
2989     }
2990   else
2991     /* cpp_interpret_string has issued an error.  */
2992     value = error_mark_node;
2993
2994   if (count > 1)
2995     obstack_free (&str_ob, 0);
2996
2997   return value;
2998 }
2999
3000
3001 /* Basic concepts [gram.basic]  */
3002
3003 /* Parse a translation-unit.
3004
3005    translation-unit:
3006      declaration-seq [opt]
3007
3008    Returns TRUE if all went well.  */
3009
3010 static bool
3011 cp_parser_translation_unit (cp_parser* parser)
3012 {
3013   /* The address of the first non-permanent object on the declarator
3014      obstack.  */
3015   static void *declarator_obstack_base;
3016
3017   bool success;
3018
3019   /* Create the declarator obstack, if necessary.  */
3020   if (!cp_error_declarator)
3021     {
3022       gcc_obstack_init (&declarator_obstack);
3023       /* Create the error declarator.  */
3024       cp_error_declarator = make_declarator (cdk_error);
3025       /* Create the empty parameter list.  */
3026       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3027       /* Remember where the base of the declarator obstack lies.  */
3028       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3029     }
3030
3031   cp_parser_declaration_seq_opt (parser);
3032
3033   /* If there are no tokens left then all went well.  */
3034   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3035     {
3036       /* Get rid of the token array; we don't need it any more.  */
3037       cp_lexer_destroy (parser->lexer);
3038       parser->lexer = NULL;
3039
3040       /* This file might have been a context that's implicitly extern
3041          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3042       if (parser->implicit_extern_c)
3043         {
3044           pop_lang_context ();
3045           parser->implicit_extern_c = false;
3046         }
3047
3048       /* Finish up.  */
3049       finish_translation_unit ();
3050
3051       success = true;
3052     }
3053   else
3054     {
3055       cp_parser_error (parser, "expected declaration");
3056       success = false;
3057     }
3058
3059   /* Make sure the declarator obstack was fully cleaned up.  */
3060   gcc_assert (obstack_next_free (&declarator_obstack)
3061               == declarator_obstack_base);
3062
3063   /* All went well.  */
3064   return success;
3065 }
3066
3067 /* Expressions [gram.expr] */
3068
3069 /* Parse a primary-expression.
3070
3071    primary-expression:
3072      literal
3073      this
3074      ( expression )
3075      id-expression
3076
3077    GNU Extensions:
3078
3079    primary-expression:
3080      ( compound-statement )
3081      __builtin_va_arg ( assignment-expression , type-id )
3082      __builtin_offsetof ( type-id , offsetof-expression )
3083
3084    C++ Extensions:
3085      __has_nothrow_assign ( type-id )   
3086      __has_nothrow_constructor ( type-id )
3087      __has_nothrow_copy ( type-id )
3088      __has_trivial_assign ( type-id )   
3089      __has_trivial_constructor ( type-id )
3090      __has_trivial_copy ( type-id )
3091      __has_trivial_destructor ( type-id )
3092      __has_virtual_destructor ( type-id )     
3093      __is_abstract ( type-id )
3094      __is_base_of ( type-id , type-id )
3095      __is_class ( type-id )
3096      __is_convertible_to ( type-id , type-id )     
3097      __is_empty ( type-id )
3098      __is_enum ( type-id )
3099      __is_pod ( type-id )
3100      __is_polymorphic ( type-id )
3101      __is_union ( type-id )
3102
3103    Objective-C++ Extension:
3104
3105    primary-expression:
3106      objc-expression
3107
3108    literal:
3109      __null
3110
3111    ADDRESS_P is true iff this expression was immediately preceded by
3112    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3113    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3114    true iff this expression is a template argument.
3115
3116    Returns a representation of the expression.  Upon return, *IDK
3117    indicates what kind of id-expression (if any) was present.  */
3118
3119 static tree
3120 cp_parser_primary_expression (cp_parser *parser,
3121                               bool address_p,
3122                               bool cast_p,
3123                               bool template_arg_p,
3124                               cp_id_kind *idk)
3125 {
3126   cp_token *token = NULL;
3127
3128   /* Assume the primary expression is not an id-expression.  */
3129   *idk = CP_ID_KIND_NONE;
3130
3131   /* Peek at the next token.  */
3132   token = cp_lexer_peek_token (parser->lexer);
3133   switch (token->type)
3134     {
3135       /* literal:
3136            integer-literal
3137            character-literal
3138            floating-literal
3139            string-literal
3140            boolean-literal  */
3141     case CPP_CHAR:
3142     case CPP_CHAR16:
3143     case CPP_CHAR32:
3144     case CPP_WCHAR:
3145     case CPP_NUMBER:
3146       token = cp_lexer_consume_token (parser->lexer);
3147       if (TREE_CODE (token->u.value) == FIXED_CST)
3148         {
3149           error ("%Hfixed-point types not supported in C++",
3150                  &token->location);
3151           return error_mark_node;
3152         }
3153       /* Floating-point literals are only allowed in an integral
3154          constant expression if they are cast to an integral or
3155          enumeration type.  */
3156       if (TREE_CODE (token->u.value) == REAL_CST
3157           && parser->integral_constant_expression_p
3158           && pedantic)
3159         {
3160           /* CAST_P will be set even in invalid code like "int(2.7 +
3161              ...)".   Therefore, we have to check that the next token
3162              is sure to end the cast.  */
3163           if (cast_p)
3164             {
3165               cp_token *next_token;
3166
3167               next_token = cp_lexer_peek_token (parser->lexer);
3168               if (/* The comma at the end of an
3169                      enumerator-definition.  */
3170                   next_token->type != CPP_COMMA
3171                   /* The curly brace at the end of an enum-specifier.  */
3172                   && next_token->type != CPP_CLOSE_BRACE
3173                   /* The end of a statement.  */
3174                   && next_token->type != CPP_SEMICOLON
3175                   /* The end of the cast-expression.  */
3176                   && next_token->type != CPP_CLOSE_PAREN
3177                   /* The end of an array bound.  */
3178                   && next_token->type != CPP_CLOSE_SQUARE
3179                   /* The closing ">" in a template-argument-list.  */
3180                   && (next_token->type != CPP_GREATER
3181                       || parser->greater_than_is_operator_p)
3182                   /* C++0x only: A ">>" treated like two ">" tokens,
3183                      in a template-argument-list.  */
3184                   && (next_token->type != CPP_RSHIFT
3185                       || (cxx_dialect == cxx98)
3186                       || parser->greater_than_is_operator_p))
3187                 cast_p = false;
3188             }
3189
3190           /* If we are within a cast, then the constraint that the
3191              cast is to an integral or enumeration type will be
3192              checked at that point.  If we are not within a cast, then
3193              this code is invalid.  */
3194           if (!cast_p)
3195             cp_parser_non_integral_constant_expression
3196               (parser, "floating-point literal");
3197         }
3198       return token->u.value;
3199
3200     case CPP_STRING:
3201     case CPP_STRING16:
3202     case CPP_STRING32:
3203     case CPP_WSTRING:
3204       /* ??? Should wide strings be allowed when parser->translate_strings_p
3205          is false (i.e. in attributes)?  If not, we can kill the third
3206          argument to cp_parser_string_literal.  */
3207       return cp_parser_string_literal (parser,
3208                                        parser->translate_strings_p,
3209                                        true);
3210
3211     case CPP_OPEN_PAREN:
3212       {
3213         tree expr;
3214         bool saved_greater_than_is_operator_p;
3215
3216         /* Consume the `('.  */
3217         cp_lexer_consume_token (parser->lexer);
3218         /* Within a parenthesized expression, a `>' token is always
3219            the greater-than operator.  */
3220         saved_greater_than_is_operator_p
3221           = parser->greater_than_is_operator_p;
3222         parser->greater_than_is_operator_p = true;
3223         /* If we see `( { ' then we are looking at the beginning of
3224            a GNU statement-expression.  */
3225         if (cp_parser_allow_gnu_extensions_p (parser)
3226             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3227           {
3228             /* Statement-expressions are not allowed by the standard.  */
3229             pedwarn (token->location, OPT_pedantic, 
3230                      "ISO C++ forbids braced-groups within expressions");
3231
3232             /* And they're not allowed outside of a function-body; you
3233                cannot, for example, write:
3234
3235                  int i = ({ int j = 3; j + 1; });
3236
3237                at class or namespace scope.  */
3238             if (!parser->in_function_body
3239                 || parser->in_template_argument_list_p)
3240               {
3241                 error ("%Hstatement-expressions are not allowed outside "
3242                        "functions nor in template-argument lists",
3243                        &token->location);
3244                 cp_parser_skip_to_end_of_block_or_statement (parser);
3245                 expr = error_mark_node;
3246               }
3247             else
3248               {
3249                 /* Start the statement-expression.  */
3250                 expr = begin_stmt_expr ();
3251                 /* Parse the compound-statement.  */
3252                 cp_parser_compound_statement (parser, expr, false);
3253                 /* Finish up.  */
3254                 expr = finish_stmt_expr (expr, false);
3255               }
3256           }
3257         else
3258           {
3259             /* Parse the parenthesized expression.  */
3260             expr = cp_parser_expression (parser, cast_p, idk);
3261             /* Let the front end know that this expression was
3262                enclosed in parentheses. This matters in case, for
3263                example, the expression is of the form `A::B', since
3264                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3265                not.  */
3266             finish_parenthesized_expr (expr);
3267           }
3268         /* The `>' token might be the end of a template-id or
3269            template-parameter-list now.  */
3270         parser->greater_than_is_operator_p
3271           = saved_greater_than_is_operator_p;
3272         /* Consume the `)'.  */
3273         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3274           cp_parser_skip_to_end_of_statement (parser);
3275
3276         return expr;
3277       }
3278
3279     case CPP_KEYWORD:
3280       switch (token->keyword)
3281         {
3282           /* These two are the boolean literals.  */
3283         case RID_TRUE:
3284           cp_lexer_consume_token (parser->lexer);
3285           return boolean_true_node;
3286         case RID_FALSE:
3287           cp_lexer_consume_token (parser->lexer);
3288           return boolean_false_node;
3289
3290           /* The `__null' literal.  */
3291         case RID_NULL:
3292           cp_lexer_consume_token (parser->lexer);
3293           return null_node;
3294
3295           /* Recognize the `this' keyword.  */
3296         case RID_THIS:
3297           cp_lexer_consume_token (parser->lexer);
3298           if (parser->local_variables_forbidden_p)
3299             {
3300               error ("%H%<this%> may not be used in this context",
3301                      &token->location);
3302               return error_mark_node;
3303             }
3304           /* Pointers cannot appear in constant-expressions.  */
3305           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3306             return error_mark_node;
3307           return finish_this_expr ();
3308
3309           /* The `operator' keyword can be the beginning of an
3310              id-expression.  */
3311         case RID_OPERATOR:
3312           goto id_expression;
3313
3314         case RID_FUNCTION_NAME:
3315         case RID_PRETTY_FUNCTION_NAME:
3316         case RID_C99_FUNCTION_NAME:
3317           {
3318             const char *name;
3319
3320             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3321                __func__ are the names of variables -- but they are
3322                treated specially.  Therefore, they are handled here,
3323                rather than relying on the generic id-expression logic
3324                below.  Grammatically, these names are id-expressions.
3325
3326                Consume the token.  */
3327             token = cp_lexer_consume_token (parser->lexer);
3328
3329             switch (token->keyword)
3330               {
3331               case RID_FUNCTION_NAME:
3332                 name = "%<__FUNCTION__%>";
3333                 break;
3334               case RID_PRETTY_FUNCTION_NAME:
3335                 name = "%<__PRETTY_FUNCTION__%>";
3336                 break;
3337               case RID_C99_FUNCTION_NAME:
3338                 name = "%<__func__%>";
3339                 break;
3340               default:
3341                 gcc_unreachable ();
3342               }
3343
3344             if (cp_parser_non_integral_constant_expression (parser, name))
3345               return error_mark_node;
3346
3347             /* Look up the name.  */
3348             return finish_fname (token->u.value);
3349           }
3350
3351         case RID_VA_ARG:
3352           {
3353             tree expression;
3354             tree type;
3355
3356             /* The `__builtin_va_arg' construct is used to handle
3357                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3358             cp_lexer_consume_token (parser->lexer);
3359             /* Look for the opening `('.  */
3360             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3361             /* Now, parse the assignment-expression.  */
3362             expression = cp_parser_assignment_expression (parser,
3363                                                           /*cast_p=*/false, NULL);
3364             /* Look for the `,'.  */
3365             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3366             /* Parse the type-id.  */
3367             type = cp_parser_type_id (parser);
3368             /* Look for the closing `)'.  */
3369             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3370             /* Using `va_arg' in a constant-expression is not
3371                allowed.  */
3372             if (cp_parser_non_integral_constant_expression (parser,
3373                                                             "%<va_arg%>"))
3374               return error_mark_node;
3375             return build_x_va_arg (expression, type);
3376           }
3377
3378         case RID_OFFSETOF:
3379           return cp_parser_builtin_offsetof (parser);
3380
3381         case RID_HAS_NOTHROW_ASSIGN:
3382         case RID_HAS_NOTHROW_CONSTRUCTOR:
3383         case RID_HAS_NOTHROW_COPY:        
3384         case RID_HAS_TRIVIAL_ASSIGN:
3385         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3386         case RID_HAS_TRIVIAL_COPY:        
3387         case RID_HAS_TRIVIAL_DESTRUCTOR:
3388         case RID_HAS_VIRTUAL_DESTRUCTOR:
3389         case RID_IS_ABSTRACT:
3390         case RID_IS_BASE_OF:
3391         case RID_IS_CLASS:
3392         case RID_IS_CONVERTIBLE_TO:
3393         case RID_IS_EMPTY:
3394         case RID_IS_ENUM:
3395         case RID_IS_POD:
3396         case RID_IS_POLYMORPHIC:
3397         case RID_IS_UNION:
3398           return cp_parser_trait_expr (parser, token->keyword);
3399
3400         /* Objective-C++ expressions.  */
3401         case RID_AT_ENCODE:
3402         case RID_AT_PROTOCOL:
3403         case RID_AT_SELECTOR:
3404           return cp_parser_objc_expression (parser);
3405
3406         default:
3407           cp_parser_error (parser, "expected primary-expression");
3408           return error_mark_node;
3409         }
3410
3411       /* An id-expression can start with either an identifier, a
3412          `::' as the beginning of a qualified-id, or the "operator"
3413          keyword.  */
3414     case CPP_NAME:
3415     case CPP_SCOPE:
3416     case CPP_TEMPLATE_ID:
3417     case CPP_NESTED_NAME_SPECIFIER:
3418       {
3419         tree id_expression;
3420         tree decl;
3421         const char *error_msg;
3422         bool template_p;
3423         bool done;
3424         cp_token *id_expr_token;
3425
3426       id_expression:
3427         /* Parse the id-expression.  */
3428         id_expression
3429           = cp_parser_id_expression (parser,
3430                                      /*template_keyword_p=*/false,
3431                                      /*check_dependency_p=*/true,
3432                                      &template_p,
3433                                      /*declarator_p=*/false,
3434                                      /*optional_p=*/false);
3435         if (id_expression == error_mark_node)
3436           return error_mark_node;
3437         id_expr_token = token;
3438         token = cp_lexer_peek_token (parser->lexer);
3439         done = (token->type != CPP_OPEN_SQUARE
3440                 && token->type != CPP_OPEN_PAREN
3441                 && token->type != CPP_DOT
3442                 && token->type != CPP_DEREF
3443                 && token->type != CPP_PLUS_PLUS
3444                 && token->type != CPP_MINUS_MINUS);
3445         /* If we have a template-id, then no further lookup is
3446            required.  If the template-id was for a template-class, we
3447            will sometimes have a TYPE_DECL at this point.  */
3448         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3449                  || TREE_CODE (id_expression) == TYPE_DECL)
3450           decl = id_expression;
3451         /* Look up the name.  */
3452         else
3453           {
3454             tree ambiguous_decls;
3455
3456             decl = cp_parser_lookup_name (parser, id_expression,
3457                                           none_type,
3458                                           template_p,
3459                                           /*is_namespace=*/false,
3460                                           /*check_dependency=*/true,
3461                                           &ambiguous_decls,
3462                                           id_expr_token->location);
3463             /* If the lookup was ambiguous, an error will already have
3464                been issued.  */
3465             if (ambiguous_decls)
3466               return error_mark_node;
3467
3468             /* In Objective-C++, an instance variable (ivar) may be preferred
3469                to whatever cp_parser_lookup_name() found.  */
3470             decl = objc_lookup_ivar (decl, id_expression);
3471
3472             /* If name lookup gives us a SCOPE_REF, then the
3473                qualifying scope was dependent.  */
3474             if (TREE_CODE (decl) == SCOPE_REF)
3475               {
3476                 /* At this point, we do not know if DECL is a valid
3477                    integral constant expression.  We assume that it is
3478                    in fact such an expression, so that code like:
3479
3480                       template <int N> struct A {
3481                         int a[B<N>::i];
3482                       };
3483                      
3484                    is accepted.  At template-instantiation time, we
3485                    will check that B<N>::i is actually a constant.  */
3486                 return decl;
3487               }
3488             /* Check to see if DECL is a local variable in a context
3489                where that is forbidden.  */
3490             if (parser->local_variables_forbidden_p
3491                 && local_variable_p (decl))
3492               {
3493                 /* It might be that we only found DECL because we are
3494                    trying to be generous with pre-ISO scoping rules.
3495                    For example, consider:
3496
3497                      int i;
3498                      void g() {
3499                        for (int i = 0; i < 10; ++i) {}
3500                        extern void f(int j = i);
3501                      }
3502
3503                    Here, name look up will originally find the out
3504                    of scope `i'.  We need to issue a warning message,
3505                    but then use the global `i'.  */
3506                 decl = check_for_out_of_scope_variable (decl);
3507                 if (local_variable_p (decl))
3508                   {
3509                     error ("%Hlocal variable %qD may not appear in this context",
3510                            &id_expr_token->location, decl);
3511                     return error_mark_node;
3512                   }
3513               }
3514           }
3515
3516         decl = (finish_id_expression
3517                 (id_expression, decl, parser->scope,
3518                  idk,
3519                  parser->integral_constant_expression_p,
3520                  parser->allow_non_integral_constant_expression_p,
3521                  &parser->non_integral_constant_expression_p,
3522                  template_p, done, address_p,
3523                  template_arg_p,
3524                  &error_msg,
3525                  id_expr_token->location));
3526         if (error_msg)
3527           cp_parser_error (parser, error_msg);
3528         return decl;
3529       }
3530
3531       /* Anything else is an error.  */
3532     default:
3533       /* ...unless we have an Objective-C++ message or string literal,
3534          that is.  */
3535       if (c_dialect_objc ()
3536           && (token->type == CPP_OPEN_SQUARE
3537               || token->type == CPP_OBJC_STRING))
3538         return cp_parser_objc_expression (parser);
3539
3540       cp_parser_error (parser, "expected primary-expression");
3541       return error_mark_node;
3542     }
3543 }
3544
3545 /* Parse an id-expression.
3546
3547    id-expression:
3548      unqualified-id
3549      qualified-id
3550
3551    qualified-id:
3552      :: [opt] nested-name-specifier template [opt] unqualified-id
3553      :: identifier
3554      :: operator-function-id
3555      :: template-id
3556
3557    Return a representation of the unqualified portion of the
3558    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3559    a `::' or nested-name-specifier.
3560
3561    Often, if the id-expression was a qualified-id, the caller will
3562    want to make a SCOPE_REF to represent the qualified-id.  This
3563    function does not do this in order to avoid wastefully creating
3564    SCOPE_REFs when they are not required.
3565
3566    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3567    `template' keyword.
3568
3569    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3570    uninstantiated templates.
3571
3572    If *TEMPLATE_P is non-NULL, it is set to true iff the
3573    `template' keyword is used to explicitly indicate that the entity
3574    named is a template.
3575
3576    If DECLARATOR_P is true, the id-expression is appearing as part of
3577    a declarator, rather than as part of an expression.  */
3578
3579 static tree
3580 cp_parser_id_expression (cp_parser *parser,
3581                          bool template_keyword_p,
3582                          bool check_dependency_p,
3583                          bool *template_p,
3584                          bool declarator_p,
3585                          bool optional_p)
3586 {
3587   bool global_scope_p;
3588   bool nested_name_specifier_p;
3589
3590   /* Assume the `template' keyword was not used.  */
3591   if (template_p)
3592     *template_p = template_keyword_p;
3593
3594   /* Look for the optional `::' operator.  */
3595   global_scope_p
3596     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3597        != NULL_TREE);
3598   /* Look for the optional nested-name-specifier.  */
3599   nested_name_specifier_p
3600     = (cp_parser_nested_name_specifier_opt (parser,
3601                                             /*typename_keyword_p=*/false,
3602                                             check_dependency_p,
3603                                             /*type_p=*/false,
3604                                             declarator_p)
3605        != NULL_TREE);
3606   /* If there is a nested-name-specifier, then we are looking at
3607      the first qualified-id production.  */
3608   if (nested_name_specifier_p)
3609     {
3610       tree saved_scope;
3611       tree saved_object_scope;
3612       tree saved_qualifying_scope;
3613       tree unqualified_id;
3614       bool is_template;
3615
3616       /* See if the next token is the `template' keyword.  */
3617       if (!template_p)
3618         template_p = &is_template;
3619       *template_p = cp_parser_optional_template_keyword (parser);
3620       /* Name lookup we do during the processing of the
3621          unqualified-id might obliterate SCOPE.  */
3622       saved_scope = parser->scope;
3623       saved_object_scope = parser->object_scope;
3624       saved_qualifying_scope = parser->qualifying_scope;
3625       /* Process the final unqualified-id.  */
3626       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3627                                                  check_dependency_p,
3628                                                  declarator_p,
3629                                                  /*optional_p=*/false);
3630       /* Restore the SAVED_SCOPE for our caller.  */
3631       parser->scope = saved_scope;
3632       parser->object_scope = saved_object_scope;
3633       parser->qualifying_scope = saved_qualifying_scope;
3634
3635       return unqualified_id;
3636     }
3637   /* Otherwise, if we are in global scope, then we are looking at one
3638      of the other qualified-id productions.  */
3639   else if (global_scope_p)
3640     {
3641       cp_token *token;
3642       tree id;
3643
3644       /* Peek at the next token.  */
3645       token = cp_lexer_peek_token (parser->lexer);
3646
3647       /* If it's an identifier, and the next token is not a "<", then
3648          we can avoid the template-id case.  This is an optimization
3649          for this common case.  */
3650       if (token->type == CPP_NAME
3651           && !cp_parser_nth_token_starts_template_argument_list_p
3652                (parser, 2))
3653         return cp_parser_identifier (parser);
3654
3655       cp_parser_parse_tentatively (parser);
3656       /* Try a template-id.  */
3657       id = cp_parser_template_id (parser,
3658                                   /*template_keyword_p=*/false,
3659                                   /*check_dependency_p=*/true,
3660                                   declarator_p);
3661       /* If that worked, we're done.  */
3662       if (cp_parser_parse_definitely (parser))
3663         return id;
3664
3665       /* Peek at the next token.  (Changes in the token buffer may
3666          have invalidated the pointer obtained above.)  */
3667       token = cp_lexer_peek_token (parser->lexer);
3668
3669       switch (token->type)
3670         {
3671         case CPP_NAME:
3672           return cp_parser_identifier (parser);
3673
3674         case CPP_KEYWORD:
3675           if (token->keyword == RID_OPERATOR)
3676             return cp_parser_operator_function_id (parser);
3677           /* Fall through.  */
3678
3679         default:
3680           cp_parser_error (parser, "expected id-expression");
3681           return error_mark_node;
3682         }
3683     }
3684   else
3685     return cp_parser_unqualified_id (parser, template_keyword_p,
3686                                      /*check_dependency_p=*/true,
3687                                      declarator_p,
3688                                      optional_p);
3689 }
3690
3691 /* Parse an unqualified-id.
3692
3693    unqualified-id:
3694      identifier
3695      operator-function-id
3696      conversion-function-id
3697      ~ class-name
3698      template-id
3699
3700    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3701    keyword, in a construct like `A::template ...'.
3702
3703    Returns a representation of unqualified-id.  For the `identifier'
3704    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3705    production a BIT_NOT_EXPR is returned; the operand of the
3706    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3707    other productions, see the documentation accompanying the
3708    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3709    names are looked up in uninstantiated templates.  If DECLARATOR_P
3710    is true, the unqualified-id is appearing as part of a declarator,
3711    rather than as part of an expression.  */
3712
3713 static tree
3714 cp_parser_unqualified_id (cp_parser* parser,
3715                           bool template_keyword_p,
3716                           bool check_dependency_p,
3717                           bool declarator_p,
3718                           bool optional_p)
3719 {
3720   cp_token *token;
3721
3722   /* Peek at the next token.  */
3723   token = cp_lexer_peek_token (parser->lexer);
3724
3725   switch (token->type)
3726     {
3727     case CPP_NAME:
3728       {
3729         tree id;
3730
3731         /* We don't know yet whether or not this will be a
3732            template-id.  */
3733         cp_parser_parse_tentatively (parser);
3734         /* Try a template-id.  */
3735         id = cp_parser_template_id (parser, template_keyword_p,
3736                                     check_dependency_p,
3737                                     declarator_p);
3738         /* If it worked, we're done.  */
3739         if (cp_parser_parse_definitely (parser))
3740           return id;
3741         /* Otherwise, it's an ordinary identifier.  */
3742         return cp_parser_identifier (parser);
3743       }
3744
3745     case CPP_TEMPLATE_ID:
3746       return cp_parser_template_id (parser, template_keyword_p,
3747                                     check_dependency_p,
3748                                     declarator_p);
3749
3750     case CPP_COMPL:
3751       {
3752         tree type_decl;
3753         tree qualifying_scope;
3754         tree object_scope;
3755         tree scope;
3756         bool done;
3757
3758         /* Consume the `~' token.  */
3759         cp_lexer_consume_token (parser->lexer);
3760         /* Parse the class-name.  The standard, as written, seems to
3761            say that:
3762
3763              template <typename T> struct S { ~S (); };
3764              template <typename T> S<T>::~S() {}
3765
3766            is invalid, since `~' must be followed by a class-name, but
3767            `S<T>' is dependent, and so not known to be a class.
3768            That's not right; we need to look in uninstantiated
3769            templates.  A further complication arises from:
3770
3771              template <typename T> void f(T t) {
3772                t.T::~T();
3773              }
3774
3775            Here, it is not possible to look up `T' in the scope of `T'
3776            itself.  We must look in both the current scope, and the
3777            scope of the containing complete expression.
3778
3779            Yet another issue is:
3780
3781              struct S {
3782                int S;
3783                ~S();
3784              };
3785
3786              S::~S() {}
3787
3788            The standard does not seem to say that the `S' in `~S'
3789            should refer to the type `S' and not the data member
3790            `S::S'.  */
3791
3792         /* DR 244 says that we look up the name after the "~" in the
3793            same scope as we looked up the qualifying name.  That idea
3794            isn't fully worked out; it's more complicated than that.  */
3795         scope = parser->scope;
3796         object_scope = parser->object_scope;
3797         qualifying_scope = parser->qualifying_scope;
3798
3799         /* Check for invalid scopes.  */
3800         if (scope == error_mark_node)
3801           {
3802             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3803               cp_lexer_consume_token (parser->lexer);
3804             return error_mark_node;
3805           }
3806         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3807           {
3808             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3809               error ("%Hscope %qT before %<~%> is not a class-name",
3810                      &token->location, scope);
3811             cp_parser_simulate_error (parser);
3812             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3813               cp_lexer_consume_token (parser->lexer);
3814             return error_mark_node;
3815           }
3816         gcc_assert (!scope || TYPE_P (scope));
3817
3818         /* If the name is of the form "X::~X" it's OK.  */
3819         token = cp_lexer_peek_token (parser->lexer);
3820         if (scope
3821             && token->type == CPP_NAME
3822             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3823                 == CPP_OPEN_PAREN)
3824             && constructor_name_p (token->u.value, scope))
3825           {
3826             cp_lexer_consume_token (parser->lexer);
3827             return build_nt (BIT_NOT_EXPR, scope);
3828           }
3829
3830         /* If there was an explicit qualification (S::~T), first look
3831            in the scope given by the qualification (i.e., S).  */
3832         done = false;
3833         type_decl = NULL_TREE;
3834         if (scope)
3835           {
3836             cp_parser_parse_tentatively (parser);
3837             type_decl = cp_parser_class_name (parser,
3838                                               /*typename_keyword_p=*/false,
3839                                               /*template_keyword_p=*/false,
3840                                               none_type,
3841                                               /*check_dependency=*/false,
3842                                               /*class_head_p=*/false,
3843                                               declarator_p);
3844             if (cp_parser_parse_definitely (parser))
3845               done = true;
3846           }
3847         /* In "N::S::~S", look in "N" as well.  */
3848         if (!done && scope && qualifying_scope)
3849           {
3850             cp_parser_parse_tentatively (parser);
3851             parser->scope = qualifying_scope;
3852             parser->object_scope = NULL_TREE;
3853             parser->qualifying_scope = NULL_TREE;
3854             type_decl
3855               = cp_parser_class_name (parser,
3856                                       /*typename_keyword_p=*/false,
3857                                       /*template_keyword_p=*/false,
3858                                       none_type,
3859                                       /*check_dependency=*/false,
3860                                       /*class_head_p=*/false,
3861                                       declarator_p);
3862             if (cp_parser_parse_definitely (parser))
3863               done = true;
3864           }
3865         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3866         else if (!done && object_scope)
3867           {
3868             cp_parser_parse_tentatively (parser);
3869             parser->scope = object_scope;
3870             parser->object_scope = NULL_TREE;
3871             parser->qualifying_scope = NULL_TREE;
3872             type_decl
3873               = cp_parser_class_name (parser,
3874                                       /*typename_keyword_p=*/false,
3875                                       /*template_keyword_p=*/false,
3876                                       none_type,
3877                                       /*check_dependency=*/false,
3878                                       /*class_head_p=*/false,
3879                                       declarator_p);
3880             if (cp_parser_parse_definitely (parser))
3881               done = true;
3882           }
3883         /* Look in the surrounding context.  */
3884         if (!done)
3885           {
3886             parser->scope = NULL_TREE;
3887             parser->object_scope = NULL_TREE;
3888             parser->qualifying_scope = NULL_TREE;
3889             if (processing_template_decl)
3890               cp_parser_parse_tentatively (parser);
3891             type_decl
3892               = cp_parser_class_name (parser,
3893                                       /*typename_keyword_p=*/false,
3894                                       /*template_keyword_p=*/false,
3895                                       none_type,
3896                                       /*check_dependency=*/false,
3897                                       /*class_head_p=*/false,
3898                                       declarator_p);
3899             if (processing_template_decl
3900                 && ! cp_parser_parse_definitely (parser))
3901               {
3902                 /* We couldn't find a type with this name, so just accept
3903                    it and check for a match at instantiation time.  */
3904                 type_decl = cp_parser_identifier (parser);
3905                 if (type_decl != error_mark_node)
3906                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3907                 return type_decl;
3908               }
3909           }
3910         /* If an error occurred, assume that the name of the
3911            destructor is the same as the name of the qualifying
3912            class.  That allows us to keep parsing after running
3913            into ill-formed destructor names.  */
3914         if (type_decl == error_mark_node && scope)
3915           return build_nt (BIT_NOT_EXPR, scope);
3916         else if (type_decl == error_mark_node)
3917           return error_mark_node;
3918
3919         /* Check that destructor name and scope match.  */
3920         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3921           {
3922             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3923               error ("%Hdeclaration of %<~%T%> as member of %qT",
3924                      &token->location, type_decl, scope);
3925             cp_parser_simulate_error (parser);
3926             return error_mark_node;
3927           }
3928
3929         /* [class.dtor]
3930
3931            A typedef-name that names a class shall not be used as the
3932            identifier in the declarator for a destructor declaration.  */
3933         if (declarator_p
3934             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3935             && !DECL_SELF_REFERENCE_P (type_decl)
3936             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3937           error ("%Htypedef-name %qD used as destructor declarator",
3938                  &token->location, type_decl);
3939
3940         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3941       }
3942
3943     case CPP_KEYWORD:
3944       if (token->keyword == RID_OPERATOR)
3945         {
3946           tree id;
3947
3948           /* This could be a template-id, so we try that first.  */
3949           cp_parser_parse_tentatively (parser);
3950           /* Try a template-id.  */
3951           id = cp_parser_template_id (parser, template_keyword_p,
3952                                       /*check_dependency_p=*/true,
3953                                       declarator_p);
3954           /* If that worked, we're done.  */
3955           if (cp_parser_parse_definitely (parser))
3956             return id;
3957           /* We still don't know whether we're looking at an
3958              operator-function-id or a conversion-function-id.  */
3959           cp_parser_parse_tentatively (parser);
3960           /* Try an operator-function-id.  */
3961           id = cp_parser_operator_function_id (parser);
3962           /* If that didn't work, try a conversion-function-id.  */
3963           if (!cp_parser_parse_definitely (parser))
3964             id = cp_parser_conversion_function_id (parser);
3965
3966           return id;
3967         }
3968       /* Fall through.  */
3969
3970     default:
3971       if (optional_p)
3972         return NULL_TREE;
3973       cp_parser_error (parser, "expected unqualified-id");
3974       return error_mark_node;
3975     }
3976 }
3977
3978 /* Parse an (optional) nested-name-specifier.
3979
3980    nested-name-specifier: [C++98]
3981      class-or-namespace-name :: nested-name-specifier [opt]
3982      class-or-namespace-name :: template nested-name-specifier [opt]
3983
3984    nested-name-specifier: [C++0x]
3985      type-name ::
3986      namespace-name ::
3987      nested-name-specifier identifier ::
3988      nested-name-specifier template [opt] simple-template-id ::
3989
3990    PARSER->SCOPE should be set appropriately before this function is
3991    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3992    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3993    in name lookups.
3994
3995    Sets PARSER->SCOPE to the class (TYPE) or namespace
3996    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3997    it unchanged if there is no nested-name-specifier.  Returns the new
3998    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3999
4000    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4001    part of a declaration and/or decl-specifier.  */
4002
4003 static tree
4004 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4005                                      bool typename_keyword_p,
4006                                      bool check_dependency_p,
4007                                      bool type_p,
4008                                      bool is_declaration)
4009 {
4010   bool success = false;
4011   cp_token_position start = 0;
4012   cp_token *token;
4013
4014   /* Remember where the nested-name-specifier starts.  */
4015   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4016     {
4017       start = cp_lexer_token_position (parser->lexer, false);
4018       push_deferring_access_checks (dk_deferred);
4019     }
4020
4021   while (true)
4022     {
4023       tree new_scope;
4024       tree old_scope;
4025       tree saved_qualifying_scope;
4026       bool template_keyword_p;
4027
4028       /* Spot cases that cannot be the beginning of a
4029          nested-name-specifier.  */
4030       token = cp_lexer_peek_token (parser->lexer);
4031
4032       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4033          the already parsed nested-name-specifier.  */
4034       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4035         {
4036           /* Grab the nested-name-specifier and continue the loop.  */
4037           cp_parser_pre_parsed_nested_name_specifier (parser);
4038           /* If we originally encountered this nested-name-specifier
4039              with IS_DECLARATION set to false, we will not have
4040              resolved TYPENAME_TYPEs, so we must do so here.  */
4041           if (is_declaration
4042               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4043             {
4044               new_scope = resolve_typename_type (parser->scope,
4045                                                  /*only_current_p=*/false);
4046               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4047                 parser->scope = new_scope;
4048             }
4049           success = true;
4050           continue;
4051         }
4052
4053       /* Spot cases that cannot be the beginning of a
4054          nested-name-specifier.  On the second and subsequent times
4055          through the loop, we look for the `template' keyword.  */
4056       if (success && token->keyword == RID_TEMPLATE)
4057         ;
4058       /* A template-id can start a nested-name-specifier.  */
4059       else if (token->type == CPP_TEMPLATE_ID)
4060         ;
4061       else
4062         {
4063           /* If the next token is not an identifier, then it is
4064              definitely not a type-name or namespace-name.  */
4065           if (token->type != CPP_NAME)
4066             break;
4067           /* If the following token is neither a `<' (to begin a
4068              template-id), nor a `::', then we are not looking at a
4069              nested-name-specifier.  */
4070           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4071           if (token->type != CPP_SCOPE
4072               && !cp_parser_nth_token_starts_template_argument_list_p
4073                   (parser, 2))
4074             break;
4075         }
4076
4077       /* The nested-name-specifier is optional, so we parse
4078          tentatively.  */
4079       cp_parser_parse_tentatively (parser);
4080
4081       /* Look for the optional `template' keyword, if this isn't the
4082          first time through the loop.  */
4083       if (success)
4084         template_keyword_p = cp_parser_optional_template_keyword (parser);
4085       else
4086         template_keyword_p = false;
4087
4088       /* Save the old scope since the name lookup we are about to do
4089          might destroy it.  */
4090       old_scope = parser->scope;
4091       saved_qualifying_scope = parser->qualifying_scope;
4092       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4093          look up names in "X<T>::I" in order to determine that "Y" is
4094          a template.  So, if we have a typename at this point, we make
4095          an effort to look through it.  */
4096       if (is_declaration
4097           && !typename_keyword_p
4098           && parser->scope
4099           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4100         parser->scope = resolve_typename_type (parser->scope,
4101                                                /*only_current_p=*/false);
4102       /* Parse the qualifying entity.  */
4103       new_scope
4104         = cp_parser_qualifying_entity (parser,
4105                                        typename_keyword_p,
4106                                        template_keyword_p,
4107                                        check_dependency_p,
4108                                        type_p,
4109                                        is_declaration);
4110       /* Look for the `::' token.  */
4111       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4112
4113       /* If we found what we wanted, we keep going; otherwise, we're
4114          done.  */
4115       if (!cp_parser_parse_definitely (parser))
4116         {
4117           bool error_p = false;
4118
4119           /* Restore the OLD_SCOPE since it was valid before the
4120              failed attempt at finding the last
4121              class-or-namespace-name.  */
4122           parser->scope = old_scope;
4123           parser->qualifying_scope = saved_qualifying_scope;
4124           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4125             break;
4126           /* If the next token is an identifier, and the one after
4127              that is a `::', then any valid interpretation would have
4128              found a class-or-namespace-name.  */
4129           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4130                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4131                      == CPP_SCOPE)
4132                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4133                      != CPP_COMPL))
4134             {
4135               token = cp_lexer_consume_token (parser->lexer);
4136               if (!error_p)
4137                 {
4138                   if (!token->ambiguous_p)
4139                     {
4140                       tree decl;
4141                       tree ambiguous_decls;
4142
4143                       decl = cp_parser_lookup_name (parser, token->u.value,
4144                                                     none_type,
4145                                                     /*is_template=*/false,
4146                                                     /*is_namespace=*/false,
4147                                                     /*check_dependency=*/true,
4148                                                     &ambiguous_decls,
4149                                                     token->location);
4150                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4151                         error ("%H%qD used without template parameters",
4152                                &token->location, decl);
4153                       else if (ambiguous_decls)
4154                         {
4155                           error ("%Hreference to %qD is ambiguous",
4156                                  &token->location, token->u.value);
4157                           print_candidates (ambiguous_decls);
4158                           decl = error_mark_node;
4159                         }
4160                       else
4161                         {
4162                           const char* msg = "is not a class or namespace";
4163                           if (cxx_dialect != cxx98)
4164                             msg = "is not a class, namespace, or enumeration";
4165                           cp_parser_name_lookup_error
4166                             (parser, token->u.value, decl, msg,
4167                              token->location);
4168                         }
4169                     }
4170                   parser->scope = error_mark_node;
4171                   error_p = true;
4172                   /* Treat this as a successful nested-name-specifier
4173                      due to:
4174
4175                      [basic.lookup.qual]
4176
4177                      If the name found is not a class-name (clause
4178                      _class_) or namespace-name (_namespace.def_), the
4179                      program is ill-formed.  */
4180                   success = true;
4181                 }
4182               cp_lexer_consume_token (parser->lexer);
4183             }
4184           break;
4185         }
4186       /* We've found one valid nested-name-specifier.  */
4187       success = true;
4188       /* Name lookup always gives us a DECL.  */
4189       if (TREE_CODE (new_scope) == TYPE_DECL)
4190         new_scope = TREE_TYPE (new_scope);
4191       /* Uses of "template" must be followed by actual templates.  */
4192       if (template_keyword_p
4193           && !(CLASS_TYPE_P (new_scope)
4194                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4195                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4196                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4197           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4198                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4199                    == TEMPLATE_ID_EXPR)))
4200         permerror (input_location, TYPE_P (new_scope)
4201                    ? "%qT is not a template"
4202                    : "%qD is not a template",
4203                    new_scope);
4204       /* If it is a class scope, try to complete it; we are about to
4205          be looking up names inside the class.  */
4206       if (TYPE_P (new_scope)
4207           /* Since checking types for dependency can be expensive,
4208              avoid doing it if the type is already complete.  */
4209           && !COMPLETE_TYPE_P (new_scope)
4210           /* Do not try to complete dependent types.  */
4211           && !dependent_type_p (new_scope))
4212         {
4213           new_scope = complete_type (new_scope);
4214           /* If it is a typedef to current class, use the current
4215              class instead, as the typedef won't have any names inside
4216              it yet.  */
4217           if (!COMPLETE_TYPE_P (new_scope)
4218               && currently_open_class (new_scope))
4219             new_scope = TYPE_MAIN_VARIANT (new_scope);
4220         }
4221       /* Make sure we look in the right scope the next time through
4222          the loop.  */
4223       parser->scope = new_scope;
4224     }
4225
4226   /* If parsing tentatively, replace the sequence of tokens that makes
4227      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4228      token.  That way, should we re-parse the token stream, we will
4229      not have to repeat the effort required to do the parse, nor will
4230      we issue duplicate error messages.  */
4231   if (success && start)
4232     {
4233       cp_token *token;
4234
4235       token = cp_lexer_token_at (parser->lexer, start);
4236       /* Reset the contents of the START token.  */
4237       token->type = CPP_NESTED_NAME_SPECIFIER;
4238       /* Retrieve any deferred checks.  Do not pop this access checks yet
4239          so the memory will not be reclaimed during token replacing below.  */
4240       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4241       token->u.tree_check_value->value = parser->scope;
4242       token->u.tree_check_value->checks = get_deferred_access_checks ();
4243       token->u.tree_check_value->qualifying_scope =
4244         parser->qualifying_scope;
4245       token->keyword = RID_MAX;
4246
4247       /* Purge all subsequent tokens.  */
4248       cp_lexer_purge_tokens_after (parser->lexer, start);
4249     }
4250
4251   if (start)
4252     pop_to_parent_deferring_access_checks ();
4253
4254   return success ? parser->scope : NULL_TREE;
4255 }
4256
4257 /* Parse a nested-name-specifier.  See
4258    cp_parser_nested_name_specifier_opt for details.  This function
4259    behaves identically, except that it will an issue an error if no
4260    nested-name-specifier is present.  */
4261
4262 static tree
4263 cp_parser_nested_name_specifier (cp_parser *parser,
4264                                  bool typename_keyword_p,
4265                                  bool check_dependency_p,
4266                                  bool type_p,
4267                                  bool is_declaration)
4268 {
4269   tree scope;
4270
4271   /* Look for the nested-name-specifier.  */
4272   scope = cp_parser_nested_name_specifier_opt (parser,
4273                                                typename_keyword_p,
4274                                                check_dependency_p,
4275                                                type_p,
4276                                                is_declaration);
4277   /* If it was not present, issue an error message.  */
4278   if (!scope)
4279     {
4280       cp_parser_error (parser, "expected nested-name-specifier");
4281       parser->scope = NULL_TREE;
4282     }
4283
4284   return scope;
4285 }
4286
4287 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4288    this is either a class-name or a namespace-name (which corresponds
4289    to the class-or-namespace-name production in the grammar). For
4290    C++0x, it can also be a type-name that refers to an enumeration
4291    type.
4292
4293    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4294    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4295    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4296    TYPE_P is TRUE iff the next name should be taken as a class-name,
4297    even the same name is declared to be another entity in the same
4298    scope.
4299
4300    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4301    specified by the class-or-namespace-name.  If neither is found the
4302    ERROR_MARK_NODE is returned.  */
4303
4304 static tree
4305 cp_parser_qualifying_entity (cp_parser *parser,
4306                              bool typename_keyword_p,
4307                              bool template_keyword_p,
4308                              bool check_dependency_p,
4309                              bool type_p,
4310                              bool is_declaration)
4311 {
4312   tree saved_scope;
4313   tree saved_qualifying_scope;
4314   tree saved_object_scope;
4315   tree scope;
4316   bool only_class_p;
4317   bool successful_parse_p;
4318
4319   /* Before we try to parse the class-name, we must save away the
4320      current PARSER->SCOPE since cp_parser_class_name will destroy
4321      it.  */
4322   saved_scope = parser->scope;
4323   saved_qualifying_scope = parser->qualifying_scope;
4324   saved_object_scope = parser->object_scope;
4325   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4326      there is no need to look for a namespace-name.  */
4327   only_class_p = template_keyword_p 
4328     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4329   if (!only_class_p)
4330     cp_parser_parse_tentatively (parser);
4331   scope = cp_parser_class_name (parser,
4332                                 typename_keyword_p,
4333                                 template_keyword_p,
4334                                 type_p ? class_type : none_type,
4335                                 check_dependency_p,
4336                                 /*class_head_p=*/false,
4337                                 is_declaration);
4338   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4339   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4340   if (!only_class_p 
4341       && cxx_dialect != cxx98
4342       && !successful_parse_p)
4343     {
4344       /* Restore the saved scope.  */
4345       parser->scope = saved_scope;
4346       parser->qualifying_scope = saved_qualifying_scope;
4347       parser->object_scope = saved_object_scope;
4348
4349       /* Parse tentatively.  */
4350       cp_parser_parse_tentatively (parser);
4351      
4352       /* Parse a typedef-name or enum-name.  */
4353       scope = cp_parser_nonclass_name (parser);
4354       successful_parse_p = cp_parser_parse_definitely (parser);
4355     }
4356   /* If that didn't work, try for a namespace-name.  */
4357   if (!only_class_p && !successful_parse_p)
4358     {
4359       /* Restore the saved scope.  */
4360       parser->scope = saved_scope;
4361       parser->qualifying_scope = saved_qualifying_scope;
4362       parser->object_scope = saved_object_scope;
4363       /* If we are not looking at an identifier followed by the scope
4364          resolution operator, then this is not part of a
4365          nested-name-specifier.  (Note that this function is only used
4366          to parse the components of a nested-name-specifier.)  */
4367       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4368           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4369         return error_mark_node;
4370       scope = cp_parser_namespace_name (parser);
4371     }
4372
4373   return scope;
4374 }
4375
4376 /* Parse a postfix-expression.
4377
4378    postfix-expression:
4379      primary-expression
4380      postfix-expression [ expression ]
4381      postfix-expression ( expression-list [opt] )
4382      simple-type-specifier ( expression-list [opt] )
4383      typename :: [opt] nested-name-specifier identifier
4384        ( expression-list [opt] )
4385      typename :: [opt] nested-name-specifier template [opt] template-id
4386        ( expression-list [opt] )
4387      postfix-expression . template [opt] id-expression
4388      postfix-expression -> template [opt] id-expression
4389      postfix-expression . pseudo-destructor-name
4390      postfix-expression -> pseudo-destructor-name
4391      postfix-expression ++
4392      postfix-expression --
4393      dynamic_cast < type-id > ( expression )
4394      static_cast < type-id > ( expression )
4395      reinterpret_cast < type-id > ( expression )
4396      const_cast < type-id > ( expression )
4397      typeid ( expression )
4398      typeid ( type-id )
4399
4400    GNU Extension:
4401
4402    postfix-expression:
4403      ( type-id ) { initializer-list , [opt] }
4404
4405    This extension is a GNU version of the C99 compound-literal
4406    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4407    but they are essentially the same concept.)
4408
4409    If ADDRESS_P is true, the postfix expression is the operand of the
4410    `&' operator.  CAST_P is true if this expression is the target of a
4411    cast.
4412
4413    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4414    class member access expressions [expr.ref].
4415
4416    Returns a representation of the expression.  */
4417
4418 static tree
4419 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4420                               bool member_access_only_p,
4421                               cp_id_kind * pidk_return)
4422 {
4423   cp_token *token;
4424   enum rid keyword;
4425   cp_id_kind idk = CP_ID_KIND_NONE;
4426   tree postfix_expression = NULL_TREE;
4427   bool is_member_access = false;
4428
4429   /* Peek at the next token.  */
4430   token = cp_lexer_peek_token (parser->lexer);
4431   /* Some of the productions are determined by keywords.  */
4432   keyword = token->keyword;
4433   switch (keyword)
4434     {
4435     case RID_DYNCAST:
4436     case RID_STATCAST:
4437     case RID_REINTCAST:
4438     case RID_CONSTCAST:
4439       {
4440         tree type;
4441         tree expression;
4442         const char *saved_message;
4443
4444         /* All of these can be handled in the same way from the point
4445            of view of parsing.  Begin by consuming the token
4446            identifying the cast.  */
4447         cp_lexer_consume_token (parser->lexer);
4448
4449         /* New types cannot be defined in the cast.  */
4450         saved_message = parser->type_definition_forbidden_message;
4451         parser->type_definition_forbidden_message
4452           = "types may not be defined in casts";
4453
4454         /* Look for the opening `<'.  */
4455         cp_parser_require (parser, CPP_LESS, "%<<%>");
4456         /* Parse the type to which we are casting.  */
4457         type = cp_parser_type_id (parser);
4458         /* Look for the closing `>'.  */
4459         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4460         /* Restore the old message.  */
4461         parser->type_definition_forbidden_message = saved_message;
4462
4463         /* And the expression which is being cast.  */
4464         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4465         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4466         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4467
4468         /* Only type conversions to integral or enumeration types
4469            can be used in constant-expressions.  */
4470         if (!cast_valid_in_integral_constant_expression_p (type)
4471             && (cp_parser_non_integral_constant_expression
4472                 (parser,
4473                  "a cast to a type other than an integral or "
4474                  "enumeration type")))
4475           return error_mark_node;
4476
4477         switch (keyword)
4478           {
4479           case RID_DYNCAST:
4480             postfix_expression
4481               = build_dynamic_cast (type, expression, tf_warning_or_error);
4482             break;
4483           case RID_STATCAST:
4484             postfix_expression
4485               = build_static_cast (type, expression, tf_warning_or_error);
4486             break;
4487           case RID_REINTCAST:
4488             postfix_expression
4489               = build_reinterpret_cast (type, expression, 
4490                                         tf_warning_or_error);
4491             break;
4492           case RID_CONSTCAST:
4493             postfix_expression
4494               = build_const_cast (type, expression, tf_warning_or_error);
4495             break;
4496           default:
4497             gcc_unreachable ();
4498           }
4499       }
4500       break;
4501
4502     case RID_TYPEID:
4503       {
4504         tree type;
4505         const char *saved_message;
4506         bool saved_in_type_id_in_expr_p;
4507
4508         /* Consume the `typeid' token.  */
4509         cp_lexer_consume_token (parser->lexer);
4510         /* Look for the `(' token.  */
4511         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4512         /* Types cannot be defined in a `typeid' expression.  */
4513         saved_message = parser->type_definition_forbidden_message;
4514         parser->type_definition_forbidden_message
4515           = "types may not be defined in a %<typeid%> expression";
4516         /* We can't be sure yet whether we're looking at a type-id or an
4517            expression.  */
4518         cp_parser_parse_tentatively (parser);
4519         /* Try a type-id first.  */
4520         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4521         parser->in_type_id_in_expr_p = true;
4522         type = cp_parser_type_id (parser);
4523         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4524         /* Look for the `)' token.  Otherwise, we can't be sure that
4525            we're not looking at an expression: consider `typeid (int
4526            (3))', for example.  */
4527         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4528         /* If all went well, simply lookup the type-id.  */
4529         if (cp_parser_parse_definitely (parser))
4530           postfix_expression = get_typeid (type);
4531         /* Otherwise, fall back to the expression variant.  */
4532         else
4533           {
4534             tree expression;
4535
4536             /* Look for an expression.  */
4537             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4538             /* Compute its typeid.  */
4539             postfix_expression = build_typeid (expression);
4540             /* Look for the `)' token.  */
4541             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4542           }
4543         /* Restore the saved message.  */
4544         parser->type_definition_forbidden_message = saved_message;
4545         /* `typeid' may not appear in an integral constant expression.  */
4546         if (cp_parser_non_integral_constant_expression(parser,
4547                                                        "%<typeid%> operator"))
4548           return error_mark_node;
4549       }
4550       break;
4551
4552     case RID_TYPENAME:
4553       {
4554         tree type;
4555         /* The syntax permitted here is the same permitted for an
4556            elaborated-type-specifier.  */
4557         type = cp_parser_elaborated_type_specifier (parser,
4558                                                     /*is_friend=*/false,
4559                                                     /*is_declaration=*/false);
4560         postfix_expression = cp_parser_functional_cast (parser, type);
4561       }
4562       break;
4563
4564     default:
4565       {
4566         tree type;
4567
4568         /* If the next thing is a simple-type-specifier, we may be
4569            looking at a functional cast.  We could also be looking at
4570            an id-expression.  So, we try the functional cast, and if
4571            that doesn't work we fall back to the primary-expression.  */
4572         cp_parser_parse_tentatively (parser);
4573         /* Look for the simple-type-specifier.  */
4574         type = cp_parser_simple_type_specifier (parser,
4575                                                 /*decl_specs=*/NULL,
4576                                                 CP_PARSER_FLAGS_NONE);
4577         /* Parse the cast itself.  */
4578         if (!cp_parser_error_occurred (parser))
4579           postfix_expression
4580             = cp_parser_functional_cast (parser, type);
4581         /* If that worked, we're done.  */
4582         if (cp_parser_parse_definitely (parser))
4583           break;
4584
4585         /* If the functional-cast didn't work out, try a
4586            compound-literal.  */
4587         if (cp_parser_allow_gnu_extensions_p (parser)
4588             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4589           {
4590             VEC(constructor_elt,gc) *initializer_list = NULL;
4591             bool saved_in_type_id_in_expr_p;
4592
4593             cp_parser_parse_tentatively (parser);
4594             /* Consume the `('.  */
4595             cp_lexer_consume_token (parser->lexer);
4596             /* Parse the type.  */
4597             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4598             parser->in_type_id_in_expr_p = true;
4599             type = cp_parser_type_id (parser);
4600             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4601             /* Look for the `)'.  */
4602             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4603             /* Look for the `{'.  */
4604             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4605             /* If things aren't going well, there's no need to
4606                keep going.  */
4607             if (!cp_parser_error_occurred (parser))
4608               {
4609                 bool non_constant_p;
4610                 /* Parse the initializer-list.  */
4611                 initializer_list
4612                   = cp_parser_initializer_list (parser, &non_constant_p);
4613                 /* Allow a trailing `,'.  */
4614                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4615                   cp_lexer_consume_token (parser->lexer);
4616                 /* Look for the final `}'.  */
4617                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4618               }
4619             /* If that worked, we're definitely looking at a
4620                compound-literal expression.  */
4621             if (cp_parser_parse_definitely (parser))
4622               {
4623                 /* Warn the user that a compound literal is not
4624                    allowed in standard C++.  */
4625                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4626                 /* For simplicity, we disallow compound literals in
4627                    constant-expressions.  We could
4628                    allow compound literals of integer type, whose
4629                    initializer was a constant, in constant
4630                    expressions.  Permitting that usage, as a further
4631                    extension, would not change the meaning of any
4632                    currently accepted programs.  (Of course, as
4633                    compound literals are not part of ISO C++, the
4634                    standard has nothing to say.)  */
4635                 if (cp_parser_non_integral_constant_expression 
4636                     (parser, "non-constant compound literals"))
4637                   {
4638                     postfix_expression = error_mark_node;
4639                     break;
4640                   }
4641                 /* Form the representation of the compound-literal.  */
4642                 postfix_expression
4643                   = (finish_compound_literal
4644                      (type, build_constructor (init_list_type_node,
4645                                                initializer_list)));
4646                 break;
4647               }
4648           }
4649
4650         /* It must be a primary-expression.  */
4651         postfix_expression
4652           = cp_parser_primary_expression (parser, address_p, cast_p,
4653                                           /*template_arg_p=*/false,
4654                                           &idk);
4655       }
4656       break;
4657     }
4658
4659   /* Keep looping until the postfix-expression is complete.  */
4660   while (true)
4661     {
4662       if (idk == CP_ID_KIND_UNQUALIFIED
4663           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4664           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4665         /* It is not a Koenig lookup function call.  */
4666         postfix_expression
4667           = unqualified_name_lookup_error (postfix_expression);
4668
4669       /* Peek at the next token.  */
4670       token = cp_lexer_peek_token (parser->lexer);
4671
4672       switch (token->type)
4673         {
4674         case CPP_OPEN_SQUARE:
4675           postfix_expression
4676             = cp_parser_postfix_open_square_expression (parser,
4677                                                         postfix_expression,
4678                                                         false);
4679           idk = CP_ID_KIND_NONE;
4680           is_member_access = false;
4681           break;
4682
4683         case CPP_OPEN_PAREN:
4684           /* postfix-expression ( expression-list [opt] ) */
4685           {
4686             bool koenig_p;
4687             bool is_builtin_constant_p;
4688             bool saved_integral_constant_expression_p = false;
4689             bool saved_non_integral_constant_expression_p = false;
4690             tree args;
4691
4692             is_member_access = false;
4693
4694             is_builtin_constant_p
4695               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4696             if (is_builtin_constant_p)
4697               {
4698                 /* The whole point of __builtin_constant_p is to allow
4699                    non-constant expressions to appear as arguments.  */
4700                 saved_integral_constant_expression_p
4701                   = parser->integral_constant_expression_p;
4702                 saved_non_integral_constant_expression_p
4703                   = parser->non_integral_constant_expression_p;
4704                 parser->integral_constant_expression_p = false;
4705               }
4706             args = (cp_parser_parenthesized_expression_list
4707                     (parser, /*is_attribute_list=*/false,
4708                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4709                      /*non_constant_p=*/NULL));
4710             if (is_builtin_constant_p)
4711               {
4712                 parser->integral_constant_expression_p
4713                   = saved_integral_constant_expression_p;
4714                 parser->non_integral_constant_expression_p
4715                   = saved_non_integral_constant_expression_p;
4716               }
4717
4718             if (args == error_mark_node)
4719               {
4720                 postfix_expression = error_mark_node;
4721                 break;
4722               }
4723
4724             /* Function calls are not permitted in
4725                constant-expressions.  */
4726             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4727                 && cp_parser_non_integral_constant_expression (parser,
4728                                                                "a function call"))
4729               {
4730                 postfix_expression = error_mark_node;
4731                 break;
4732               }
4733
4734             koenig_p = false;
4735             if (idk == CP_ID_KIND_UNQUALIFIED)
4736               {
4737                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4738                   {
4739                     if (args)
4740                       {
4741                         koenig_p = true;
4742                         postfix_expression
4743                           = perform_koenig_lookup (postfix_expression, args);
4744                       }
4745                     else
4746                       postfix_expression
4747                         = unqualified_fn_lookup_error (postfix_expression);
4748                   }
4749                 /* We do not perform argument-dependent lookup if
4750                    normal lookup finds a non-function, in accordance
4751                    with the expected resolution of DR 218.  */
4752                 else if (args && is_overloaded_fn (postfix_expression))
4753                   {
4754                     tree fn = get_first_fn (postfix_expression);
4755
4756                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4757                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4758
4759                     /* Only do argument dependent lookup if regular
4760                        lookup does not find a set of member functions.
4761                        [basic.lookup.koenig]/2a  */
4762                     if (!DECL_FUNCTION_MEMBER_P (fn))
4763                       {
4764                         koenig_p = true;
4765                         postfix_expression
4766                           = perform_koenig_lookup (postfix_expression, args);
4767                       }
4768                   }
4769               }
4770
4771             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4772               {
4773                 tree instance = TREE_OPERAND (postfix_expression, 0);
4774                 tree fn = TREE_OPERAND (postfix_expression, 1);
4775
4776                 if (processing_template_decl
4777                     && (type_dependent_expression_p (instance)
4778                         || (!BASELINK_P (fn)
4779                             && TREE_CODE (fn) != FIELD_DECL)
4780                         || type_dependent_expression_p (fn)
4781                         || any_type_dependent_arguments_p (args)))
4782                   {
4783                     postfix_expression
4784                       = build_nt_call_list (postfix_expression, args);
4785                     break;
4786                   }
4787
4788                 if (BASELINK_P (fn))
4789                   {
4790                   postfix_expression
4791                     = (build_new_method_call
4792                        (instance, fn, args, NULL_TREE,
4793                         (idk == CP_ID_KIND_QUALIFIED
4794                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4795                         /*fn_p=*/NULL,
4796                         tf_warning_or_error));
4797                   }
4798                 else
4799                   postfix_expression
4800                     = finish_call_expr (postfix_expression, args,
4801                                         /*disallow_virtual=*/false,
4802                                         /*koenig_p=*/false,
4803                                         tf_warning_or_error);
4804               }
4805             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4806                      || TREE_CODE (postfix_expression) == MEMBER_REF
4807                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4808               postfix_expression = (build_offset_ref_call_from_tree
4809                                     (postfix_expression, args));
4810             else if (idk == CP_ID_KIND_QUALIFIED)
4811               /* A call to a static class member, or a namespace-scope
4812                  function.  */
4813               postfix_expression
4814                 = finish_call_expr (postfix_expression, args,
4815                                     /*disallow_virtual=*/true,
4816                                     koenig_p,
4817                                     tf_warning_or_error);
4818             else
4819               /* All other function calls.  */
4820               postfix_expression
4821                 = finish_call_expr (postfix_expression, args,
4822                                     /*disallow_virtual=*/false,
4823                                     koenig_p,
4824                                     tf_warning_or_error);
4825
4826             if (warn_disallowed_functions)
4827               warn_if_disallowed_function_p (postfix_expression);
4828
4829             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4830             idk = CP_ID_KIND_NONE;
4831           }
4832           break;
4833
4834         case CPP_DOT:
4835         case CPP_DEREF:
4836           /* postfix-expression . template [opt] id-expression
4837              postfix-expression . pseudo-destructor-name
4838              postfix-expression -> template [opt] id-expression
4839              postfix-expression -> pseudo-destructor-name */
4840
4841           /* Consume the `.' or `->' operator.  */
4842           cp_lexer_consume_token (parser->lexer);
4843
4844           postfix_expression
4845             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4846                                                       postfix_expression,
4847                                                       false, &idk,
4848                                                       token->location);
4849
4850           is_member_access = true;
4851           break;
4852
4853         case CPP_PLUS_PLUS:
4854           /* postfix-expression ++  */
4855           /* Consume the `++' token.  */
4856           cp_lexer_consume_token (parser->lexer);
4857           /* Generate a representation for the complete expression.  */
4858           postfix_expression
4859             = finish_increment_expr (postfix_expression,
4860                                      POSTINCREMENT_EXPR);
4861           /* Increments may not appear in constant-expressions.  */
4862           if (cp_parser_non_integral_constant_expression (parser,
4863                                                           "an increment"))
4864             postfix_expression = error_mark_node;
4865           idk = CP_ID_KIND_NONE;
4866           is_member_access = false;
4867           break;
4868
4869         case CPP_MINUS_MINUS:
4870           /* postfix-expression -- */
4871           /* Consume the `--' token.  */
4872           cp_lexer_consume_token (parser->lexer);
4873           /* Generate a representation for the complete expression.  */
4874           postfix_expression
4875             = finish_increment_expr (postfix_expression,
4876                                      POSTDECREMENT_EXPR);
4877           /* Decrements may not appear in constant-expressions.  */
4878           if (cp_parser_non_integral_constant_expression (parser,
4879                                                           "a decrement"))
4880             postfix_expression = error_mark_node;
4881           idk = CP_ID_KIND_NONE;
4882           is_member_access = false;
4883           break;
4884
4885         default:
4886           if (pidk_return != NULL)
4887             * pidk_return = idk;
4888           if (member_access_only_p)
4889             return is_member_access? postfix_expression : error_mark_node;
4890           else
4891             return postfix_expression;
4892         }
4893     }
4894
4895   /* We should never get here.  */
4896   gcc_unreachable ();
4897   return error_mark_node;
4898 }
4899
4900 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4901    by cp_parser_builtin_offsetof.  We're looking for
4902
4903      postfix-expression [ expression ]
4904
4905    FOR_OFFSETOF is set if we're being called in that context, which
4906    changes how we deal with integer constant expressions.  */
4907
4908 static tree
4909 cp_parser_postfix_open_square_expression (cp_parser *parser,
4910                                           tree postfix_expression,
4911                                           bool for_offsetof)
4912 {
4913   tree index;
4914
4915   /* Consume the `[' token.  */
4916   cp_lexer_consume_token (parser->lexer);
4917
4918   /* Parse the index expression.  */
4919   /* ??? For offsetof, there is a question of what to allow here.  If
4920      offsetof is not being used in an integral constant expression context,
4921      then we *could* get the right answer by computing the value at runtime.
4922      If we are in an integral constant expression context, then we might
4923      could accept any constant expression; hard to say without analysis.
4924      Rather than open the barn door too wide right away, allow only integer
4925      constant expressions here.  */
4926   if (for_offsetof)
4927     index = cp_parser_constant_expression (parser, false, NULL);
4928   else
4929     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4930
4931   /* Look for the closing `]'.  */
4932   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4933
4934   /* Build the ARRAY_REF.  */
4935   postfix_expression = grok_array_decl (postfix_expression, index);
4936
4937   /* When not doing offsetof, array references are not permitted in
4938      constant-expressions.  */
4939   if (!for_offsetof
4940       && (cp_parser_non_integral_constant_expression
4941           (parser, "an array reference")))
4942     postfix_expression = error_mark_node;
4943
4944   return postfix_expression;
4945 }
4946
4947 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4948    by cp_parser_builtin_offsetof.  We're looking for
4949
4950      postfix-expression . template [opt] id-expression
4951      postfix-expression . pseudo-destructor-name
4952      postfix-expression -> template [opt] id-expression
4953      postfix-expression -> pseudo-destructor-name
4954
4955    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4956    limits what of the above we'll actually accept, but nevermind.
4957    TOKEN_TYPE is the "." or "->" token, which will already have been
4958    removed from the stream.  */
4959
4960 static tree
4961 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4962                                         enum cpp_ttype token_type,
4963                                         tree postfix_expression,
4964                                         bool for_offsetof, cp_id_kind *idk,
4965                                         location_t location)
4966 {
4967   tree name;
4968   bool dependent_p;
4969   bool pseudo_destructor_p;
4970   tree scope = NULL_TREE;
4971
4972   /* If this is a `->' operator, dereference the pointer.  */
4973   if (token_type == CPP_DEREF)
4974     postfix_expression = build_x_arrow (postfix_expression);
4975   /* Check to see whether or not the expression is type-dependent.  */
4976   dependent_p = type_dependent_expression_p (postfix_expression);
4977   /* The identifier following the `->' or `.' is not qualified.  */
4978   parser->scope = NULL_TREE;
4979   parser->qualifying_scope = NULL_TREE;
4980   parser->object_scope = NULL_TREE;
4981   *idk = CP_ID_KIND_NONE;
4982
4983   /* Enter the scope corresponding to the type of the object
4984      given by the POSTFIX_EXPRESSION.  */
4985   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4986     {
4987       scope = TREE_TYPE (postfix_expression);
4988       /* According to the standard, no expression should ever have
4989          reference type.  Unfortunately, we do not currently match
4990          the standard in this respect in that our internal representation
4991          of an expression may have reference type even when the standard
4992          says it does not.  Therefore, we have to manually obtain the
4993          underlying type here.  */
4994       scope = non_reference (scope);
4995       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4996       if (scope == unknown_type_node)
4997         {
4998           error ("%H%qE does not have class type", &location, postfix_expression);
4999           scope = NULL_TREE;
5000         }
5001       else
5002         scope = complete_type_or_else (scope, NULL_TREE);
5003       /* Let the name lookup machinery know that we are processing a
5004          class member access expression.  */
5005       parser->context->object_type = scope;
5006       /* If something went wrong, we want to be able to discern that case,
5007          as opposed to the case where there was no SCOPE due to the type
5008          of expression being dependent.  */
5009       if (!scope)
5010         scope = error_mark_node;
5011       /* If the SCOPE was erroneous, make the various semantic analysis
5012          functions exit quickly -- and without issuing additional error
5013          messages.  */
5014       if (scope == error_mark_node)
5015         postfix_expression = error_mark_node;
5016     }
5017
5018   /* Assume this expression is not a pseudo-destructor access.  */
5019   pseudo_destructor_p = false;
5020
5021   /* If the SCOPE is a scalar type, then, if this is a valid program,
5022      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5023      is type dependent, it can be pseudo-destructor-name or something else.
5024      Try to parse it as pseudo-destructor-name first.  */
5025   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5026     {
5027       tree s;
5028       tree type;
5029
5030       cp_parser_parse_tentatively (parser);
5031       /* Parse the pseudo-destructor-name.  */
5032       s = NULL_TREE;
5033       cp_parser_pseudo_destructor_name (parser, &s, &type);
5034       if (dependent_p
5035           && (cp_parser_error_occurred (parser)
5036               || TREE_CODE (type) != TYPE_DECL
5037               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5038         cp_parser_abort_tentative_parse (parser);
5039       else if (cp_parser_parse_definitely (parser))
5040         {
5041           pseudo_destructor_p = true;
5042           postfix_expression
5043             = finish_pseudo_destructor_expr (postfix_expression,
5044                                              s, TREE_TYPE (type));
5045         }
5046     }
5047
5048   if (!pseudo_destructor_p)
5049     {
5050       /* If the SCOPE is not a scalar type, we are looking at an
5051          ordinary class member access expression, rather than a
5052          pseudo-destructor-name.  */
5053       bool template_p;
5054       cp_token *token = cp_lexer_peek_token (parser->lexer);
5055       /* Parse the id-expression.  */
5056       name = (cp_parser_id_expression
5057               (parser,
5058                cp_parser_optional_template_keyword (parser),
5059                /*check_dependency_p=*/true,
5060                &template_p,
5061                /*declarator_p=*/false,
5062                /*optional_p=*/false));
5063       /* In general, build a SCOPE_REF if the member name is qualified.
5064          However, if the name was not dependent and has already been
5065          resolved; there is no need to build the SCOPE_REF.  For example;
5066
5067              struct X { void f(); };
5068              template <typename T> void f(T* t) { t->X::f(); }
5069
5070          Even though "t" is dependent, "X::f" is not and has been resolved
5071          to a BASELINK; there is no need to include scope information.  */
5072
5073       /* But we do need to remember that there was an explicit scope for
5074          virtual function calls.  */
5075       if (parser->scope)
5076         *idk = CP_ID_KIND_QUALIFIED;
5077
5078       /* If the name is a template-id that names a type, we will get a
5079          TYPE_DECL here.  That is invalid code.  */
5080       if (TREE_CODE (name) == TYPE_DECL)
5081         {
5082           error ("%Hinvalid use of %qD", &token->location, name);
5083           postfix_expression = error_mark_node;
5084         }
5085       else
5086         {
5087           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5088             {
5089               name = build_qualified_name (/*type=*/NULL_TREE,
5090                                            parser->scope,
5091                                            name,
5092                                            template_p);
5093               parser->scope = NULL_TREE;
5094               parser->qualifying_scope = NULL_TREE;
5095               parser->object_scope = NULL_TREE;
5096             }
5097           if (scope && name && BASELINK_P (name))
5098             adjust_result_of_qualified_name_lookup
5099               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5100           postfix_expression
5101             = finish_class_member_access_expr (postfix_expression, name,
5102                                                template_p, 
5103                                                tf_warning_or_error);
5104         }
5105     }
5106
5107   /* We no longer need to look up names in the scope of the object on
5108      the left-hand side of the `.' or `->' operator.  */
5109   parser->context->object_type = NULL_TREE;
5110
5111   /* Outside of offsetof, these operators may not appear in
5112      constant-expressions.  */
5113   if (!for_offsetof
5114       && (cp_parser_non_integral_constant_expression
5115           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5116     postfix_expression = error_mark_node;
5117
5118   return postfix_expression;
5119 }
5120
5121 /* Parse a parenthesized expression-list.
5122
5123    expression-list:
5124      assignment-expression
5125      expression-list, assignment-expression
5126
5127    attribute-list:
5128      expression-list
5129      identifier
5130      identifier, expression-list
5131
5132    CAST_P is true if this expression is the target of a cast.
5133
5134    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5135    argument pack.
5136
5137    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5138    representation of an assignment-expression.  Note that a TREE_LIST
5139    is returned even if there is only a single expression in the list.
5140    error_mark_node is returned if the ( and or ) are
5141    missing. NULL_TREE is returned on no expressions. The parentheses
5142    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5143    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5144    indicates whether or not all of the expressions in the list were
5145    constant.  */
5146
5147 static tree
5148 cp_parser_parenthesized_expression_list (cp_parser* parser,
5149                                          bool is_attribute_list,
5150                                          bool cast_p,
5151                                          bool allow_expansion_p,
5152                                          bool *non_constant_p)
5153 {
5154   tree expression_list = NULL_TREE;
5155   bool fold_expr_p = is_attribute_list;
5156   tree identifier = NULL_TREE;
5157   bool saved_greater_than_is_operator_p;
5158
5159   /* Assume all the expressions will be constant.  */
5160   if (non_constant_p)
5161     *non_constant_p = false;
5162
5163   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5164     return error_mark_node;
5165
5166   /* Within a parenthesized expression, a `>' token is always
5167      the greater-than operator.  */
5168   saved_greater_than_is_operator_p
5169     = parser->greater_than_is_operator_p;
5170   parser->greater_than_is_operator_p = true;
5171
5172   /* Consume expressions until there are no more.  */
5173   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5174     while (true)
5175       {
5176         tree expr;
5177
5178         /* At the beginning of attribute lists, check to see if the
5179            next token is an identifier.  */
5180         if (is_attribute_list
5181             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5182           {
5183             cp_token *token;
5184
5185             /* Consume the identifier.  */
5186             token = cp_lexer_consume_token (parser->lexer);
5187             /* Save the identifier.  */
5188             identifier = token->u.value;
5189           }
5190         else
5191           {
5192             bool expr_non_constant_p;
5193
5194             /* Parse the next assignment-expression.  */
5195             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5196               {
5197                 /* A braced-init-list.  */
5198                 maybe_warn_cpp0x ("extended initializer lists");
5199                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5200                 if (non_constant_p && expr_non_constant_p)
5201                   *non_constant_p = true;
5202               }
5203             else if (non_constant_p)
5204               {
5205                 expr = (cp_parser_constant_expression
5206                         (parser, /*allow_non_constant_p=*/true,
5207                          &expr_non_constant_p));
5208                 if (expr_non_constant_p)
5209                   *non_constant_p = true;
5210               }
5211             else
5212               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5213
5214             if (fold_expr_p)
5215               expr = fold_non_dependent_expr (expr);
5216
5217             /* If we have an ellipsis, then this is an expression
5218                expansion.  */
5219             if (allow_expansion_p
5220                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5221               {
5222                 /* Consume the `...'.  */
5223                 cp_lexer_consume_token (parser->lexer);
5224
5225                 /* Build the argument pack.  */
5226                 expr = make_pack_expansion (expr);
5227               }
5228
5229              /* Add it to the list.  We add error_mark_node
5230                 expressions to the list, so that we can still tell if
5231                 the correct form for a parenthesized expression-list
5232                 is found. That gives better errors.  */
5233             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5234
5235             if (expr == error_mark_node)
5236               goto skip_comma;
5237           }
5238
5239         /* After the first item, attribute lists look the same as
5240            expression lists.  */
5241         is_attribute_list = false;
5242
5243       get_comma:;
5244         /* If the next token isn't a `,', then we are done.  */
5245         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5246           break;
5247
5248         /* Otherwise, consume the `,' and keep going.  */
5249         cp_lexer_consume_token (parser->lexer);
5250       }
5251
5252   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5253     {
5254       int ending;
5255
5256     skip_comma:;
5257       /* We try and resync to an unnested comma, as that will give the
5258          user better diagnostics.  */
5259       ending = cp_parser_skip_to_closing_parenthesis (parser,
5260                                                       /*recovering=*/true,
5261                                                       /*or_comma=*/true,
5262                                                       /*consume_paren=*/true);
5263       if (ending < 0)
5264         goto get_comma;
5265       if (!ending)
5266         {
5267           parser->greater_than_is_operator_p
5268             = saved_greater_than_is_operator_p;
5269           return error_mark_node;
5270         }
5271     }
5272
5273   parser->greater_than_is_operator_p
5274     = saved_greater_than_is_operator_p;
5275
5276   /* We built up the list in reverse order so we must reverse it now.  */
5277   expression_list = nreverse (expression_list);
5278   if (identifier)
5279     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5280
5281   return expression_list;
5282 }
5283
5284 /* Parse a pseudo-destructor-name.
5285
5286    pseudo-destructor-name:
5287      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5288      :: [opt] nested-name-specifier template template-id :: ~ type-name
5289      :: [opt] nested-name-specifier [opt] ~ type-name
5290
5291    If either of the first two productions is used, sets *SCOPE to the
5292    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5293    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5294    or ERROR_MARK_NODE if the parse fails.  */
5295
5296 static void
5297 cp_parser_pseudo_destructor_name (cp_parser* parser,
5298                                   tree* scope,
5299                                   tree* type)
5300 {
5301   bool nested_name_specifier_p;
5302
5303   /* Assume that things will not work out.  */
5304   *type = error_mark_node;
5305
5306   /* Look for the optional `::' operator.  */
5307   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5308   /* Look for the optional nested-name-specifier.  */
5309   nested_name_specifier_p
5310     = (cp_parser_nested_name_specifier_opt (parser,
5311                                             /*typename_keyword_p=*/false,
5312                                             /*check_dependency_p=*/true,
5313                                             /*type_p=*/false,
5314                                             /*is_declaration=*/false)
5315        != NULL_TREE);
5316   /* Now, if we saw a nested-name-specifier, we might be doing the
5317      second production.  */
5318   if (nested_name_specifier_p
5319       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5320     {
5321       /* Consume the `template' keyword.  */
5322       cp_lexer_consume_token (parser->lexer);
5323       /* Parse the template-id.  */
5324       cp_parser_template_id (parser,
5325                              /*template_keyword_p=*/true,
5326                              /*check_dependency_p=*/false,
5327                              /*is_declaration=*/true);
5328       /* Look for the `::' token.  */
5329       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5330     }
5331   /* If the next token is not a `~', then there might be some
5332      additional qualification.  */
5333   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5334     {
5335       /* At this point, we're looking for "type-name :: ~".  The type-name
5336          must not be a class-name, since this is a pseudo-destructor.  So,
5337          it must be either an enum-name, or a typedef-name -- both of which
5338          are just identifiers.  So, we peek ahead to check that the "::"
5339          and "~" tokens are present; if they are not, then we can avoid
5340          calling type_name.  */
5341       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5342           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5343           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5344         {
5345           cp_parser_error (parser, "non-scalar type");
5346           return;
5347         }
5348
5349       /* Look for the type-name.  */
5350       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5351       if (*scope == error_mark_node)
5352         return;
5353
5354       /* Look for the `::' token.  */
5355       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5356     }
5357   else
5358     *scope = NULL_TREE;
5359
5360   /* Look for the `~'.  */
5361   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5362   /* Look for the type-name again.  We are not responsible for
5363      checking that it matches the first type-name.  */
5364   *type = cp_parser_nonclass_name (parser);
5365 }
5366
5367 /* Parse a unary-expression.
5368
5369    unary-expression:
5370      postfix-expression
5371      ++ cast-expression
5372      -- cast-expression
5373      unary-operator cast-expression
5374      sizeof unary-expression
5375      sizeof ( type-id )
5376      new-expression
5377      delete-expression
5378
5379    GNU Extensions:
5380
5381    unary-expression:
5382      __extension__ cast-expression
5383      __alignof__ unary-expression
5384      __alignof__ ( type-id )
5385      __real__ cast-expression
5386      __imag__ cast-expression
5387      && identifier
5388
5389    ADDRESS_P is true iff the unary-expression is appearing as the
5390    operand of the `&' operator.   CAST_P is true if this expression is
5391    the target of a cast.
5392
5393    Returns a representation of the expression.  */
5394
5395 static tree
5396 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5397                             cp_id_kind * pidk)
5398 {
5399   cp_token *token;
5400   enum tree_code unary_operator;
5401
5402   /* Peek at the next token.  */
5403   token = cp_lexer_peek_token (parser->lexer);
5404   /* Some keywords give away the kind of expression.  */
5405   if (token->type == CPP_KEYWORD)
5406     {
5407       enum rid keyword = token->keyword;
5408
5409       switch (keyword)
5410         {
5411         case RID_ALIGNOF:
5412         case RID_SIZEOF:
5413           {
5414             tree operand;
5415             enum tree_code op;
5416
5417             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5418             /* Consume the token.  */
5419             cp_lexer_consume_token (parser->lexer);
5420             /* Parse the operand.  */
5421             operand = cp_parser_sizeof_operand (parser, keyword);
5422
5423             if (TYPE_P (operand))
5424               return cxx_sizeof_or_alignof_type (operand, op, true);
5425             else
5426               return cxx_sizeof_or_alignof_expr (operand, op, true);
5427           }
5428
5429         case RID_NEW:
5430           return cp_parser_new_expression (parser);
5431
5432         case RID_DELETE:
5433           return cp_parser_delete_expression (parser);
5434
5435         case RID_EXTENSION:
5436           {
5437             /* The saved value of the PEDANTIC flag.  */
5438             int saved_pedantic;
5439             tree expr;
5440
5441             /* Save away the PEDANTIC flag.  */
5442             cp_parser_extension_opt (parser, &saved_pedantic);
5443             /* Parse the cast-expression.  */
5444             expr = cp_parser_simple_cast_expression (parser);
5445             /* Restore the PEDANTIC flag.  */
5446             pedantic = saved_pedantic;
5447
5448             return expr;
5449           }
5450
5451         case RID_REALPART:
5452         case RID_IMAGPART:
5453           {
5454             tree expression;
5455
5456             /* Consume the `__real__' or `__imag__' token.  */
5457             cp_lexer_consume_token (parser->lexer);
5458             /* Parse the cast-expression.  */
5459             expression = cp_parser_simple_cast_expression (parser);
5460             /* Create the complete representation.  */
5461             return build_x_unary_op ((keyword == RID_REALPART
5462                                       ? REALPART_EXPR : IMAGPART_EXPR),
5463                                      expression,
5464                                      tf_warning_or_error);
5465           }
5466           break;
5467
5468         default:
5469           break;
5470         }
5471     }
5472
5473   /* Look for the `:: new' and `:: delete', which also signal the
5474      beginning of a new-expression, or delete-expression,
5475      respectively.  If the next token is `::', then it might be one of
5476      these.  */
5477   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5478     {
5479       enum rid keyword;
5480
5481       /* See if the token after the `::' is one of the keywords in
5482          which we're interested.  */
5483       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5484       /* If it's `new', we have a new-expression.  */
5485       if (keyword == RID_NEW)
5486         return cp_parser_new_expression (parser);
5487       /* Similarly, for `delete'.  */
5488       else if (keyword == RID_DELETE)
5489         return cp_parser_delete_expression (parser);
5490     }
5491
5492   /* Look for a unary operator.  */
5493   unary_operator = cp_parser_unary_operator (token);
5494   /* The `++' and `--' operators can be handled similarly, even though
5495      they are not technically unary-operators in the grammar.  */
5496   if (unary_operator == ERROR_MARK)
5497     {
5498       if (token->type == CPP_PLUS_PLUS)
5499         unary_operator = PREINCREMENT_EXPR;
5500       else if (token->type == CPP_MINUS_MINUS)
5501         unary_operator = PREDECREMENT_EXPR;
5502       /* Handle the GNU address-of-label extension.  */
5503       else if (cp_parser_allow_gnu_extensions_p (parser)
5504                && token->type == CPP_AND_AND)
5505         {
5506           tree identifier;
5507           tree expression;
5508           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5509
5510           /* Consume the '&&' token.  */
5511           cp_lexer_consume_token (parser->lexer);
5512           /* Look for the identifier.  */
5513           identifier = cp_parser_identifier (parser);
5514           /* Create an expression representing the address.  */
5515           expression = finish_label_address_expr (identifier, loc);
5516           if (cp_parser_non_integral_constant_expression (parser,
5517                                                 "the address of a label"))
5518             expression = error_mark_node;
5519           return expression;
5520         }
5521     }
5522   if (unary_operator != ERROR_MARK)
5523     {
5524       tree cast_expression;
5525       tree expression = error_mark_node;
5526       const char *non_constant_p = NULL;
5527
5528       /* Consume the operator token.  */
5529       token = cp_lexer_consume_token (parser->lexer);
5530       /* Parse the cast-expression.  */
5531       cast_expression
5532         = cp_parser_cast_expression (parser,
5533                                      unary_operator == ADDR_EXPR,
5534                                      /*cast_p=*/false, pidk);
5535       /* Now, build an appropriate representation.  */
5536       switch (unary_operator)
5537         {
5538         case INDIRECT_REF:
5539           non_constant_p = "%<*%>";
5540           expression = build_x_indirect_ref (cast_expression, "unary *",
5541                                              tf_warning_or_error);
5542           break;
5543
5544         case ADDR_EXPR:
5545           non_constant_p = "%<&%>";
5546           /* Fall through.  */
5547         case BIT_NOT_EXPR:
5548           expression = build_x_unary_op (unary_operator, cast_expression,
5549                                          tf_warning_or_error);
5550           break;
5551
5552         case PREINCREMENT_EXPR:
5553         case PREDECREMENT_EXPR:
5554           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5555                             ? "%<++%>" : "%<--%>");
5556           /* Fall through.  */
5557         case UNARY_PLUS_EXPR:
5558         case NEGATE_EXPR:
5559         case TRUTH_NOT_EXPR:
5560           expression = finish_unary_op_expr (unary_operator, cast_expression);
5561           break;
5562
5563         default:
5564           gcc_unreachable ();
5565         }
5566
5567       if (non_constant_p
5568           && cp_parser_non_integral_constant_expression (parser,
5569                                                          non_constant_p))
5570         expression = error_mark_node;
5571
5572       return expression;
5573     }
5574
5575   return cp_parser_postfix_expression (parser, address_p, cast_p,
5576                                        /*member_access_only_p=*/false,
5577                                        pidk);
5578 }
5579
5580 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5581    unary-operator, the corresponding tree code is returned.  */
5582
5583 static enum tree_code
5584 cp_parser_unary_operator (cp_token* token)
5585 {
5586   switch (token->type)
5587     {
5588     case CPP_MULT:
5589       return INDIRECT_REF;
5590
5591     case CPP_AND:
5592       return ADDR_EXPR;
5593
5594     case CPP_PLUS:
5595       return UNARY_PLUS_EXPR;
5596
5597     case CPP_MINUS:
5598       return NEGATE_EXPR;
5599
5600     case CPP_NOT:
5601       return TRUTH_NOT_EXPR;
5602
5603     case CPP_COMPL:
5604       return BIT_NOT_EXPR;
5605
5606     default:
5607       return ERROR_MARK;
5608     }
5609 }
5610
5611 /* Parse a new-expression.
5612
5613    new-expression:
5614      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5615      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5616
5617    Returns a representation of the expression.  */
5618
5619 static tree
5620 cp_parser_new_expression (cp_parser* parser)
5621 {
5622   bool global_scope_p;
5623   tree placement;
5624   tree type;
5625   tree initializer;
5626   tree nelts;
5627
5628   /* Look for the optional `::' operator.  */
5629   global_scope_p
5630     = (cp_parser_global_scope_opt (parser,
5631                                    /*current_scope_valid_p=*/false)
5632        != NULL_TREE);
5633   /* Look for the `new' operator.  */
5634   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5635   /* There's no easy way to tell a new-placement from the
5636      `( type-id )' construct.  */
5637   cp_parser_parse_tentatively (parser);
5638   /* Look for a new-placement.  */
5639   placement = cp_parser_new_placement (parser);
5640   /* If that didn't work out, there's no new-placement.  */
5641   if (!cp_parser_parse_definitely (parser))
5642     placement = NULL_TREE;
5643
5644   /* If the next token is a `(', then we have a parenthesized
5645      type-id.  */
5646   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5647     {
5648       cp_token *token;
5649       /* Consume the `('.  */
5650       cp_lexer_consume_token (parser->lexer);
5651       /* Parse the type-id.  */
5652       type = cp_parser_type_id (parser);
5653       /* Look for the closing `)'.  */
5654       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5655       token = cp_lexer_peek_token (parser->lexer);
5656       /* There should not be a direct-new-declarator in this production,
5657          but GCC used to allowed this, so we check and emit a sensible error
5658          message for this case.  */
5659       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5660         {
5661           error ("%Harray bound forbidden after parenthesized type-id",
5662                  &token->location);
5663           inform (token->location, 
5664                   "try removing the parentheses around the type-id");
5665           cp_parser_direct_new_declarator (parser);
5666         }
5667       nelts = NULL_TREE;
5668     }
5669   /* Otherwise, there must be a new-type-id.  */
5670   else
5671     type = cp_parser_new_type_id (parser, &nelts);
5672
5673   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5674   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5675       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5676     initializer = cp_parser_new_initializer (parser);
5677   else
5678     initializer = NULL_TREE;
5679
5680   /* A new-expression may not appear in an integral constant
5681      expression.  */
5682   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5683     return error_mark_node;
5684
5685   /* Create a representation of the new-expression.  */
5686   return build_new (placement, type, nelts, initializer, global_scope_p,
5687                     tf_warning_or_error);
5688 }
5689
5690 /* Parse a new-placement.
5691
5692    new-placement:
5693      ( expression-list )
5694
5695    Returns the same representation as for an expression-list.  */
5696
5697 static tree
5698 cp_parser_new_placement (cp_parser* parser)
5699 {
5700   tree expression_list;
5701
5702   /* Parse the expression-list.  */
5703   expression_list = (cp_parser_parenthesized_expression_list
5704                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5705                       /*non_constant_p=*/NULL));
5706
5707   return expression_list;
5708 }
5709
5710 /* Parse a new-type-id.
5711
5712    new-type-id:
5713      type-specifier-seq new-declarator [opt]
5714
5715    Returns the TYPE allocated.  If the new-type-id indicates an array
5716    type, *NELTS is set to the number of elements in the last array
5717    bound; the TYPE will not include the last array bound.  */
5718
5719 static tree
5720 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5721 {
5722   cp_decl_specifier_seq type_specifier_seq;
5723   cp_declarator *new_declarator;
5724   cp_declarator *declarator;
5725   cp_declarator *outer_declarator;
5726   const char *saved_message;
5727   tree type;
5728
5729   /* The type-specifier sequence must not contain type definitions.
5730      (It cannot contain declarations of new types either, but if they
5731      are not definitions we will catch that because they are not
5732      complete.)  */
5733   saved_message = parser->type_definition_forbidden_message;
5734   parser->type_definition_forbidden_message
5735     = "types may not be defined in a new-type-id";
5736   /* Parse the type-specifier-seq.  */
5737   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5738                                 &type_specifier_seq);
5739   /* Restore the old message.  */
5740   parser->type_definition_forbidden_message = saved_message;
5741   /* Parse the new-declarator.  */
5742   new_declarator = cp_parser_new_declarator_opt (parser);
5743
5744   /* Determine the number of elements in the last array dimension, if
5745      any.  */
5746   *nelts = NULL_TREE;
5747   /* Skip down to the last array dimension.  */
5748   declarator = new_declarator;
5749   outer_declarator = NULL;
5750   while (declarator && (declarator->kind == cdk_pointer
5751                         || declarator->kind == cdk_ptrmem))
5752     {
5753       outer_declarator = declarator;
5754       declarator = declarator->declarator;
5755     }
5756   while (declarator
5757          && declarator->kind == cdk_array
5758          && declarator->declarator
5759          && declarator->declarator->kind == cdk_array)
5760     {
5761       outer_declarator = declarator;
5762       declarator = declarator->declarator;
5763     }
5764
5765   if (declarator && declarator->kind == cdk_array)
5766     {
5767       *nelts = declarator->u.array.bounds;
5768       if (*nelts == error_mark_node)
5769         *nelts = integer_one_node;
5770
5771       if (outer_declarator)
5772         outer_declarator->declarator = declarator->declarator;
5773       else
5774         new_declarator = NULL;
5775     }
5776
5777   type = groktypename (&type_specifier_seq, new_declarator);
5778   return type;
5779 }
5780
5781 /* Parse an (optional) new-declarator.
5782
5783    new-declarator:
5784      ptr-operator new-declarator [opt]
5785      direct-new-declarator
5786
5787    Returns the declarator.  */
5788
5789 static cp_declarator *
5790 cp_parser_new_declarator_opt (cp_parser* parser)
5791 {
5792   enum tree_code code;
5793   tree type;
5794   cp_cv_quals cv_quals;
5795
5796   /* We don't know if there's a ptr-operator next, or not.  */
5797   cp_parser_parse_tentatively (parser);
5798   /* Look for a ptr-operator.  */
5799   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5800   /* If that worked, look for more new-declarators.  */
5801   if (cp_parser_parse_definitely (parser))
5802     {
5803       cp_declarator *declarator;
5804
5805       /* Parse another optional declarator.  */
5806       declarator = cp_parser_new_declarator_opt (parser);
5807
5808       return cp_parser_make_indirect_declarator
5809         (code, type, cv_quals, declarator);
5810     }
5811
5812   /* If the next token is a `[', there is a direct-new-declarator.  */
5813   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5814     return cp_parser_direct_new_declarator (parser);
5815
5816   return NULL;
5817 }
5818
5819 /* Parse a direct-new-declarator.
5820
5821    direct-new-declarator:
5822      [ expression ]
5823      direct-new-declarator [constant-expression]
5824
5825    */
5826
5827 static cp_declarator *
5828 cp_parser_direct_new_declarator (cp_parser* parser)
5829 {
5830   cp_declarator *declarator = NULL;
5831
5832   while (true)
5833     {
5834       tree expression;
5835
5836       /* Look for the opening `['.  */
5837       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5838       /* The first expression is not required to be constant.  */
5839       if (!declarator)
5840         {
5841           cp_token *token = cp_lexer_peek_token (parser->lexer);
5842           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5843           /* The standard requires that the expression have integral
5844              type.  DR 74 adds enumeration types.  We believe that the
5845              real intent is that these expressions be handled like the
5846              expression in a `switch' condition, which also allows
5847              classes with a single conversion to integral or
5848              enumeration type.  */
5849           if (!processing_template_decl)
5850             {
5851               expression
5852                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5853                                               expression,
5854                                               /*complain=*/true);
5855               if (!expression)
5856                 {
5857                   error ("%Hexpression in new-declarator must have integral "
5858                          "or enumeration type", &token->location);
5859                   expression = error_mark_node;
5860                 }
5861             }
5862         }
5863       /* But all the other expressions must be.  */
5864       else
5865         expression
5866           = cp_parser_constant_expression (parser,
5867                                            /*allow_non_constant=*/false,
5868                                            NULL);
5869       /* Look for the closing `]'.  */
5870       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5871
5872       /* Add this bound to the declarator.  */
5873       declarator = make_array_declarator (declarator, expression);
5874
5875       /* If the next token is not a `[', then there are no more
5876          bounds.  */
5877       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5878         break;
5879     }
5880
5881   return declarator;
5882 }
5883
5884 /* Parse a new-initializer.
5885
5886    new-initializer:
5887      ( expression-list [opt] )
5888      braced-init-list
5889
5890    Returns a representation of the expression-list.  If there is no
5891    expression-list, VOID_ZERO_NODE is returned.  */
5892
5893 static tree
5894 cp_parser_new_initializer (cp_parser* parser)
5895 {
5896   tree expression_list;
5897
5898   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5899     {
5900       bool expr_non_constant_p;
5901       maybe_warn_cpp0x ("extended initializer lists");
5902       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5903       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5904       expression_list = build_tree_list (NULL_TREE, expression_list);
5905     }
5906   else
5907     expression_list = (cp_parser_parenthesized_expression_list
5908                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5909                         /*non_constant_p=*/NULL));
5910   if (!expression_list)
5911     expression_list = void_zero_node;
5912
5913   return expression_list;
5914 }
5915
5916 /* Parse a delete-expression.
5917
5918    delete-expression:
5919      :: [opt] delete cast-expression
5920      :: [opt] delete [ ] cast-expression
5921
5922    Returns a representation of the expression.  */
5923
5924 static tree
5925 cp_parser_delete_expression (cp_parser* parser)
5926 {
5927   bool global_scope_p;
5928   bool array_p;
5929   tree expression;
5930
5931   /* Look for the optional `::' operator.  */
5932   global_scope_p
5933     = (cp_parser_global_scope_opt (parser,
5934                                    /*current_scope_valid_p=*/false)
5935        != NULL_TREE);
5936   /* Look for the `delete' keyword.  */
5937   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5938   /* See if the array syntax is in use.  */
5939   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5940     {
5941       /* Consume the `[' token.  */
5942       cp_lexer_consume_token (parser->lexer);
5943       /* Look for the `]' token.  */
5944       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5945       /* Remember that this is the `[]' construct.  */
5946       array_p = true;
5947     }
5948   else
5949     array_p = false;
5950
5951   /* Parse the cast-expression.  */
5952   expression = cp_parser_simple_cast_expression (parser);
5953
5954   /* A delete-expression may not appear in an integral constant
5955      expression.  */
5956   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5957     return error_mark_node;
5958
5959   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5960 }
5961
5962 /* Returns true if TOKEN may start a cast-expression and false
5963    otherwise.  */
5964
5965 static bool
5966 cp_parser_token_starts_cast_expression (cp_token *token)
5967 {
5968   switch (token->type)
5969     {
5970     case CPP_COMMA:
5971     case CPP_SEMICOLON:
5972     case CPP_QUERY:
5973     case CPP_COLON:
5974     case CPP_CLOSE_SQUARE:
5975     case CPP_CLOSE_PAREN:
5976     case CPP_CLOSE_BRACE:
5977     case CPP_DOT:
5978     case CPP_DOT_STAR:
5979     case CPP_DEREF:
5980     case CPP_DEREF_STAR:
5981     case CPP_DIV:
5982     case CPP_MOD:
5983     case CPP_LSHIFT:
5984     case CPP_RSHIFT:
5985     case CPP_LESS:
5986     case CPP_GREATER:
5987     case CPP_LESS_EQ:
5988     case CPP_GREATER_EQ:
5989     case CPP_EQ_EQ:
5990     case CPP_NOT_EQ:
5991     case CPP_EQ:
5992     case CPP_MULT_EQ:
5993     case CPP_DIV_EQ:
5994     case CPP_MOD_EQ:
5995     case CPP_PLUS_EQ:
5996     case CPP_MINUS_EQ:
5997     case CPP_RSHIFT_EQ:
5998     case CPP_LSHIFT_EQ:
5999     case CPP_AND_EQ:
6000     case CPP_XOR_EQ:
6001     case CPP_OR_EQ:
6002     case CPP_XOR:
6003     case CPP_OR:
6004     case CPP_OR_OR:
6005     case CPP_EOF:
6006       return false;
6007
6008       /* '[' may start a primary-expression in obj-c++.  */
6009     case CPP_OPEN_SQUARE:
6010       return c_dialect_objc ();
6011
6012     default:
6013       return true;
6014     }
6015 }
6016
6017 /* Parse a cast-expression.
6018
6019    cast-expression:
6020      unary-expression
6021      ( type-id ) cast-expression
6022
6023    ADDRESS_P is true iff the unary-expression is appearing as the
6024    operand of the `&' operator.   CAST_P is true if this expression is
6025    the target of a cast.
6026
6027    Returns a representation of the expression.  */
6028
6029 static tree
6030 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6031                            cp_id_kind * pidk)
6032 {
6033   /* If it's a `(', then we might be looking at a cast.  */
6034   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6035     {
6036       tree type = NULL_TREE;
6037       tree expr = NULL_TREE;
6038       bool compound_literal_p;
6039       const char *saved_message;
6040
6041       /* There's no way to know yet whether or not this is a cast.
6042          For example, `(int (3))' is a unary-expression, while `(int)
6043          3' is a cast.  So, we resort to parsing tentatively.  */
6044       cp_parser_parse_tentatively (parser);
6045       /* Types may not be defined in a cast.  */
6046       saved_message = parser->type_definition_forbidden_message;
6047       parser->type_definition_forbidden_message
6048         = "types may not be defined in casts";
6049       /* Consume the `('.  */
6050       cp_lexer_consume_token (parser->lexer);
6051       /* A very tricky bit is that `(struct S) { 3 }' is a
6052          compound-literal (which we permit in C++ as an extension).
6053          But, that construct is not a cast-expression -- it is a
6054          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6055          is legal; if the compound-literal were a cast-expression,
6056          you'd need an extra set of parentheses.)  But, if we parse
6057          the type-id, and it happens to be a class-specifier, then we
6058          will commit to the parse at that point, because we cannot
6059          undo the action that is done when creating a new class.  So,
6060          then we cannot back up and do a postfix-expression.
6061
6062          Therefore, we scan ahead to the closing `)', and check to see
6063          if the token after the `)' is a `{'.  If so, we are not
6064          looking at a cast-expression.
6065
6066          Save tokens so that we can put them back.  */
6067       cp_lexer_save_tokens (parser->lexer);
6068       /* Skip tokens until the next token is a closing parenthesis.
6069          If we find the closing `)', and the next token is a `{', then
6070          we are looking at a compound-literal.  */
6071       compound_literal_p
6072         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6073                                                   /*consume_paren=*/true)
6074            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6075       /* Roll back the tokens we skipped.  */
6076       cp_lexer_rollback_tokens (parser->lexer);
6077       /* If we were looking at a compound-literal, simulate an error
6078          so that the call to cp_parser_parse_definitely below will
6079          fail.  */
6080       if (compound_literal_p)
6081         cp_parser_simulate_error (parser);
6082       else
6083         {
6084           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6085           parser->in_type_id_in_expr_p = true;
6086           /* Look for the type-id.  */
6087           type = cp_parser_type_id (parser);
6088           /* Look for the closing `)'.  */
6089           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6090           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6091         }
6092
6093       /* Restore the saved message.  */
6094       parser->type_definition_forbidden_message = saved_message;
6095
6096       /* At this point this can only be either a cast or a
6097          parenthesized ctor such as `(T ())' that looks like a cast to
6098          function returning T.  */
6099       if (!cp_parser_error_occurred (parser)
6100           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6101                                                      (parser->lexer)))
6102         {
6103           cp_parser_parse_definitely (parser);
6104           expr = cp_parser_cast_expression (parser,
6105                                             /*address_p=*/false,
6106                                             /*cast_p=*/true, pidk);
6107
6108           /* Warn about old-style casts, if so requested.  */
6109           if (warn_old_style_cast
6110               && !in_system_header
6111               && !VOID_TYPE_P (type)
6112               && current_lang_name != lang_name_c)
6113             warning (OPT_Wold_style_cast, "use of old-style cast");
6114
6115           /* Only type conversions to integral or enumeration types
6116              can be used in constant-expressions.  */
6117           if (!cast_valid_in_integral_constant_expression_p (type)
6118               && (cp_parser_non_integral_constant_expression
6119                   (parser,
6120                    "a cast to a type other than an integral or "
6121                    "enumeration type")))
6122             return error_mark_node;
6123
6124           /* Perform the cast.  */
6125           expr = build_c_cast (type, expr);
6126           return expr;
6127         }
6128       else 
6129         cp_parser_abort_tentative_parse (parser);
6130     }
6131
6132   /* If we get here, then it's not a cast, so it must be a
6133      unary-expression.  */
6134   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6135 }
6136
6137 /* Parse a binary expression of the general form:
6138
6139    pm-expression:
6140      cast-expression
6141      pm-expression .* cast-expression
6142      pm-expression ->* cast-expression
6143
6144    multiplicative-expression:
6145      pm-expression
6146      multiplicative-expression * pm-expression
6147      multiplicative-expression / pm-expression
6148      multiplicative-expression % pm-expression
6149
6150    additive-expression:
6151      multiplicative-expression
6152      additive-expression + multiplicative-expression
6153      additive-expression - multiplicative-expression
6154
6155    shift-expression:
6156      additive-expression
6157      shift-expression << additive-expression
6158      shift-expression >> additive-expression
6159
6160    relational-expression:
6161      shift-expression
6162      relational-expression < shift-expression
6163      relational-expression > shift-expression
6164      relational-expression <= shift-expression
6165      relational-expression >= shift-expression
6166
6167   GNU Extension:
6168
6169    relational-expression:
6170      relational-expression <? shift-expression
6171      relational-expression >? shift-expression
6172
6173    equality-expression:
6174      relational-expression
6175      equality-expression == relational-expression
6176      equality-expression != relational-expression
6177
6178    and-expression:
6179      equality-expression
6180      and-expression & equality-expression
6181
6182    exclusive-or-expression:
6183      and-expression
6184      exclusive-or-expression ^ and-expression
6185
6186    inclusive-or-expression:
6187      exclusive-or-expression
6188      inclusive-or-expression | exclusive-or-expression
6189
6190    logical-and-expression:
6191      inclusive-or-expression
6192      logical-and-expression && inclusive-or-expression
6193
6194    logical-or-expression:
6195      logical-and-expression
6196      logical-or-expression || logical-and-expression
6197
6198    All these are implemented with a single function like:
6199
6200    binary-expression:
6201      simple-cast-expression
6202      binary-expression <token> binary-expression
6203
6204    CAST_P is true if this expression is the target of a cast.
6205
6206    The binops_by_token map is used to get the tree codes for each <token> type.
6207    binary-expressions are associated according to a precedence table.  */
6208
6209 #define TOKEN_PRECEDENCE(token)                              \
6210 (((token->type == CPP_GREATER                                \
6211    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6212   && !parser->greater_than_is_operator_p)                    \
6213  ? PREC_NOT_OPERATOR                                         \
6214  : binops_by_token[token->type].prec)
6215
6216 static tree
6217 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6218                              enum cp_parser_prec prec,
6219                              cp_id_kind * pidk)
6220 {
6221   cp_parser_expression_stack stack;
6222   cp_parser_expression_stack_entry *sp = &stack[0];
6223   tree lhs, rhs;
6224   cp_token *token;
6225   enum tree_code tree_type, lhs_type, rhs_type;
6226   enum cp_parser_prec new_prec, lookahead_prec;
6227   bool overloaded_p;
6228
6229   /* Parse the first expression.  */
6230   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6231   lhs_type = ERROR_MARK;
6232
6233   for (;;)
6234     {
6235       /* Get an operator token.  */
6236       token = cp_lexer_peek_token (parser->lexer);
6237
6238       if (warn_cxx0x_compat
6239           && token->type == CPP_RSHIFT
6240           && !parser->greater_than_is_operator_p)
6241         {
6242           warning (OPT_Wc__0x_compat, 
6243                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6244                    &token->location);
6245           warning (OPT_Wc__0x_compat, 
6246                    "suggest parentheses around %<>>%> expression");
6247         }
6248
6249       new_prec = TOKEN_PRECEDENCE (token);
6250
6251       /* Popping an entry off the stack means we completed a subexpression:
6252          - either we found a token which is not an operator (`>' where it is not
6253            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6254            will happen repeatedly;
6255          - or, we found an operator which has lower priority.  This is the case
6256            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6257            parsing `3 * 4'.  */
6258       if (new_prec <= prec)
6259         {
6260           if (sp == stack)
6261             break;
6262           else
6263             goto pop;
6264         }
6265
6266      get_rhs:
6267       tree_type = binops_by_token[token->type].tree_type;
6268
6269       /* We used the operator token.  */
6270       cp_lexer_consume_token (parser->lexer);
6271
6272       /* Extract another operand.  It may be the RHS of this expression
6273          or the LHS of a new, higher priority expression.  */
6274       rhs = cp_parser_simple_cast_expression (parser);
6275       rhs_type = ERROR_MARK;
6276
6277       /* Get another operator token.  Look up its precedence to avoid
6278          building a useless (immediately popped) stack entry for common
6279          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6280       token = cp_lexer_peek_token (parser->lexer);
6281       lookahead_prec = TOKEN_PRECEDENCE (token);
6282       if (lookahead_prec > new_prec)
6283         {
6284           /* ... and prepare to parse the RHS of the new, higher priority
6285              expression.  Since precedence levels on the stack are
6286              monotonically increasing, we do not have to care about
6287              stack overflows.  */
6288           sp->prec = prec;
6289           sp->tree_type = tree_type;
6290           sp->lhs = lhs;
6291           sp->lhs_type = lhs_type;
6292           sp++;
6293           lhs = rhs;
6294           lhs_type = rhs_type;
6295           prec = new_prec;
6296           new_prec = lookahead_prec;
6297           goto get_rhs;
6298
6299          pop:
6300           /* If the stack is not empty, we have parsed into LHS the right side
6301              (`4' in the example above) of an expression we had suspended.
6302              We can use the information on the stack to recover the LHS (`3')
6303              from the stack together with the tree code (`MULT_EXPR'), and
6304              the precedence of the higher level subexpression
6305              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6306              which will be used to actually build the additive expression.  */
6307           --sp;
6308           prec = sp->prec;
6309           tree_type = sp->tree_type;
6310           rhs = lhs;
6311           rhs_type = lhs_type;
6312           lhs = sp->lhs;
6313           lhs_type = sp->lhs_type;
6314         }
6315
6316       overloaded_p = false;
6317       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6318          ERROR_MARK for everything that is not a binary expression.
6319          This makes warn_about_parentheses miss some warnings that
6320          involve unary operators.  For unary expressions we should
6321          pass the correct tree_code unless the unary expression was
6322          surrounded by parentheses.
6323       */
6324       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6325                                &overloaded_p, tf_warning_or_error);
6326       lhs_type = tree_type;
6327
6328       /* If the binary operator required the use of an overloaded operator,
6329          then this expression cannot be an integral constant-expression.
6330          An overloaded operator can be used even if both operands are
6331          otherwise permissible in an integral constant-expression if at
6332          least one of the operands is of enumeration type.  */
6333
6334       if (overloaded_p
6335           && (cp_parser_non_integral_constant_expression
6336               (parser, "calls to overloaded operators")))
6337         return error_mark_node;
6338     }
6339
6340   return lhs;
6341 }
6342
6343
6344 /* Parse the `? expression : assignment-expression' part of a
6345    conditional-expression.  The LOGICAL_OR_EXPR is the
6346    logical-or-expression that started the conditional-expression.
6347    Returns a representation of the entire conditional-expression.
6348
6349    This routine is used by cp_parser_assignment_expression.
6350
6351      ? expression : assignment-expression
6352
6353    GNU Extensions:
6354
6355      ? : assignment-expression */
6356
6357 static tree
6358 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6359 {
6360   tree expr;
6361   tree assignment_expr;
6362
6363   /* Consume the `?' token.  */
6364   cp_lexer_consume_token (parser->lexer);
6365   if (cp_parser_allow_gnu_extensions_p (parser)
6366       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6367     /* Implicit true clause.  */
6368     expr = NULL_TREE;
6369   else
6370     /* Parse the expression.  */
6371     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6372
6373   /* The next token should be a `:'.  */
6374   cp_parser_require (parser, CPP_COLON, "%<:%>");
6375   /* Parse the assignment-expression.  */
6376   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6377
6378   /* Build the conditional-expression.  */
6379   return build_x_conditional_expr (logical_or_expr,
6380                                    expr,
6381                                    assignment_expr,
6382                                    tf_warning_or_error);
6383 }
6384
6385 /* Parse an assignment-expression.
6386
6387    assignment-expression:
6388      conditional-expression
6389      logical-or-expression assignment-operator assignment_expression
6390      throw-expression
6391
6392    CAST_P is true if this expression is the target of a cast.
6393
6394    Returns a representation for the expression.  */
6395
6396 static tree
6397 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6398                                  cp_id_kind * pidk)
6399 {
6400   tree expr;
6401
6402   /* If the next token is the `throw' keyword, then we're looking at
6403      a throw-expression.  */
6404   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6405     expr = cp_parser_throw_expression (parser);
6406   /* Otherwise, it must be that we are looking at a
6407      logical-or-expression.  */
6408   else
6409     {
6410       /* Parse the binary expressions (logical-or-expression).  */
6411       expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR, pidk);
6412       /* If the next token is a `?' then we're actually looking at a
6413          conditional-expression.  */
6414       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6415         return cp_parser_question_colon_clause (parser, expr);
6416       else
6417         {
6418           enum tree_code assignment_operator;
6419
6420           /* If it's an assignment-operator, we're using the second
6421              production.  */
6422           assignment_operator
6423             = cp_parser_assignment_operator_opt (parser);
6424           if (assignment_operator != ERROR_MARK)
6425             {
6426               bool non_constant_p;
6427
6428               /* Parse the right-hand side of the assignment.  */
6429               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6430
6431               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6432                 maybe_warn_cpp0x ("extended initializer lists");
6433
6434               /* An assignment may not appear in a
6435                  constant-expression.  */
6436               if (cp_parser_non_integral_constant_expression (parser,
6437                                                               "an assignment"))
6438                 return error_mark_node;
6439               /* Build the assignment expression.  */
6440               expr = build_x_modify_expr (expr,
6441                                           assignment_operator,
6442                                           rhs,
6443                                           tf_warning_or_error);
6444             }
6445         }
6446     }
6447
6448   return expr;
6449 }
6450
6451 /* Parse an (optional) assignment-operator.
6452
6453    assignment-operator: one of
6454      = *= /= %= += -= >>= <<= &= ^= |=
6455
6456    GNU Extension:
6457
6458    assignment-operator: one of
6459      <?= >?=
6460
6461    If the next token is an assignment operator, the corresponding tree
6462    code is returned, and the token is consumed.  For example, for
6463    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6464    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6465    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6466    operator, ERROR_MARK is returned.  */
6467
6468 static enum tree_code
6469 cp_parser_assignment_operator_opt (cp_parser* parser)
6470 {
6471   enum tree_code op;
6472   cp_token *token;
6473
6474   /* Peek at the next token.  */
6475   token = cp_lexer_peek_token (parser->lexer);
6476
6477   switch (token->type)
6478     {
6479     case CPP_EQ:
6480       op = NOP_EXPR;
6481       break;
6482
6483     case CPP_MULT_EQ:
6484       op = MULT_EXPR;
6485       break;
6486
6487     case CPP_DIV_EQ:
6488       op = TRUNC_DIV_EXPR;
6489       break;
6490
6491     case CPP_MOD_EQ:
6492       op = TRUNC_MOD_EXPR;
6493       break;
6494
6495     case CPP_PLUS_EQ:
6496       op = PLUS_EXPR;
6497       break;
6498
6499     case CPP_MINUS_EQ:
6500       op = MINUS_EXPR;
6501       break;
6502
6503     case CPP_RSHIFT_EQ:
6504       op = RSHIFT_EXPR;
6505       break;
6506
6507     case CPP_LSHIFT_EQ:
6508       op = LSHIFT_EXPR;
6509       break;
6510
6511     case CPP_AND_EQ:
6512       op = BIT_AND_EXPR;
6513       break;
6514
6515     case CPP_XOR_EQ:
6516       op = BIT_XOR_EXPR;
6517       break;
6518
6519     case CPP_OR_EQ:
6520       op = BIT_IOR_EXPR;
6521       break;
6522
6523     default:
6524       /* Nothing else is an assignment operator.  */
6525       op = ERROR_MARK;
6526     }
6527
6528   /* If it was an assignment operator, consume it.  */
6529   if (op != ERROR_MARK)
6530     cp_lexer_consume_token (parser->lexer);
6531
6532   return op;
6533 }
6534
6535 /* Parse an expression.
6536
6537    expression:
6538      assignment-expression
6539      expression , assignment-expression
6540
6541    CAST_P is true if this expression is the target of a cast.
6542
6543    Returns a representation of the expression.  */
6544
6545 static tree
6546 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6547 {
6548   tree expression = NULL_TREE;
6549
6550   while (true)
6551     {
6552       tree assignment_expression;
6553
6554       /* Parse the next assignment-expression.  */
6555       assignment_expression
6556         = cp_parser_assignment_expression (parser, cast_p, pidk);
6557       /* If this is the first assignment-expression, we can just
6558          save it away.  */
6559       if (!expression)
6560         expression = assignment_expression;
6561       else
6562         expression = build_x_compound_expr (expression,
6563                                             assignment_expression,
6564                                             tf_warning_or_error);
6565       /* If the next token is not a comma, then we are done with the
6566          expression.  */
6567       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6568         break;
6569       /* Consume the `,'.  */
6570       cp_lexer_consume_token (parser->lexer);
6571       /* A comma operator cannot appear in a constant-expression.  */
6572       if (cp_parser_non_integral_constant_expression (parser,
6573                                                       "a comma operator"))
6574         expression = error_mark_node;
6575     }
6576
6577   return expression;
6578 }
6579
6580 /* Parse a constant-expression.
6581
6582    constant-expression:
6583      conditional-expression
6584
6585   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6586   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6587   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6588   is false, NON_CONSTANT_P should be NULL.  */
6589
6590 static tree
6591 cp_parser_constant_expression (cp_parser* parser,
6592                                bool allow_non_constant_p,
6593                                bool *non_constant_p)
6594 {
6595   bool saved_integral_constant_expression_p;
6596   bool saved_allow_non_integral_constant_expression_p;
6597   bool saved_non_integral_constant_expression_p;
6598   tree expression;
6599
6600   /* It might seem that we could simply parse the
6601      conditional-expression, and then check to see if it were
6602      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6603      one that the compiler can figure out is constant, possibly after
6604      doing some simplifications or optimizations.  The standard has a
6605      precise definition of constant-expression, and we must honor
6606      that, even though it is somewhat more restrictive.
6607
6608      For example:
6609
6610        int i[(2, 3)];
6611
6612      is not a legal declaration, because `(2, 3)' is not a
6613      constant-expression.  The `,' operator is forbidden in a
6614      constant-expression.  However, GCC's constant-folding machinery
6615      will fold this operation to an INTEGER_CST for `3'.  */
6616
6617   /* Save the old settings.  */
6618   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6619   saved_allow_non_integral_constant_expression_p
6620     = parser->allow_non_integral_constant_expression_p;
6621   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6622   /* We are now parsing a constant-expression.  */
6623   parser->integral_constant_expression_p = true;
6624   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6625   parser->non_integral_constant_expression_p = false;
6626   /* Although the grammar says "conditional-expression", we parse an
6627      "assignment-expression", which also permits "throw-expression"
6628      and the use of assignment operators.  In the case that
6629      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6630      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6631      actually essential that we look for an assignment-expression.
6632      For example, cp_parser_initializer_clauses uses this function to
6633      determine whether a particular assignment-expression is in fact
6634      constant.  */
6635   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6636   /* Restore the old settings.  */
6637   parser->integral_constant_expression_p
6638     = saved_integral_constant_expression_p;
6639   parser->allow_non_integral_constant_expression_p
6640     = saved_allow_non_integral_constant_expression_p;
6641   if (allow_non_constant_p)
6642     *non_constant_p = parser->non_integral_constant_expression_p;
6643   else if (parser->non_integral_constant_expression_p)
6644     expression = error_mark_node;
6645   parser->non_integral_constant_expression_p
6646     = saved_non_integral_constant_expression_p;
6647
6648   return expression;
6649 }
6650
6651 /* Parse __builtin_offsetof.
6652
6653    offsetof-expression:
6654      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6655
6656    offsetof-member-designator:
6657      id-expression
6658      | offsetof-member-designator "." id-expression
6659      | offsetof-member-designator "[" expression "]"
6660      | offsetof-member-designator "->" id-expression  */
6661
6662 static tree
6663 cp_parser_builtin_offsetof (cp_parser *parser)
6664 {
6665   int save_ice_p, save_non_ice_p;
6666   tree type, expr;
6667   cp_id_kind dummy;
6668   cp_token *token;
6669
6670   /* We're about to accept non-integral-constant things, but will
6671      definitely yield an integral constant expression.  Save and
6672      restore these values around our local parsing.  */
6673   save_ice_p = parser->integral_constant_expression_p;
6674   save_non_ice_p = parser->non_integral_constant_expression_p;
6675
6676   /* Consume the "__builtin_offsetof" token.  */
6677   cp_lexer_consume_token (parser->lexer);
6678   /* Consume the opening `('.  */
6679   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6680   /* Parse the type-id.  */
6681   type = cp_parser_type_id (parser);
6682   /* Look for the `,'.  */
6683   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6684   token = cp_lexer_peek_token (parser->lexer);
6685
6686   /* Build the (type *)null that begins the traditional offsetof macro.  */
6687   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6688                             tf_warning_or_error);
6689
6690   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6691   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6692                                                  true, &dummy, token->location);
6693   while (true)
6694     {
6695       token = cp_lexer_peek_token (parser->lexer);
6696       switch (token->type)
6697         {
6698         case CPP_OPEN_SQUARE:
6699           /* offsetof-member-designator "[" expression "]" */
6700           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6701           break;
6702
6703         case CPP_DEREF:
6704           /* offsetof-member-designator "->" identifier */
6705           expr = grok_array_decl (expr, integer_zero_node);
6706           /* FALLTHRU */
6707
6708         case CPP_DOT:
6709           /* offsetof-member-designator "." identifier */
6710           cp_lexer_consume_token (parser->lexer);
6711           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6712                                                          expr, true, &dummy,
6713                                                          token->location);
6714           break;
6715
6716         case CPP_CLOSE_PAREN:
6717           /* Consume the ")" token.  */
6718           cp_lexer_consume_token (parser->lexer);
6719           goto success;
6720
6721         default:
6722           /* Error.  We know the following require will fail, but
6723              that gives the proper error message.  */
6724           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6725           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6726           expr = error_mark_node;
6727           goto failure;
6728         }
6729     }
6730
6731  success:
6732   /* If we're processing a template, we can't finish the semantics yet.
6733      Otherwise we can fold the entire expression now.  */
6734   if (processing_template_decl)
6735     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6736   else
6737     expr = finish_offsetof (expr);
6738
6739  failure:
6740   parser->integral_constant_expression_p = save_ice_p;
6741   parser->non_integral_constant_expression_p = save_non_ice_p;
6742
6743   return expr;
6744 }
6745
6746 /* Parse a trait expression.  */
6747
6748 static tree
6749 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6750 {
6751   cp_trait_kind kind;
6752   tree type1, type2 = NULL_TREE;
6753   bool binary = false;
6754   cp_decl_specifier_seq decl_specs;
6755
6756   switch (keyword)
6757     {
6758     case RID_HAS_NOTHROW_ASSIGN:
6759       kind = CPTK_HAS_NOTHROW_ASSIGN;
6760       break;
6761     case RID_HAS_NOTHROW_CONSTRUCTOR:
6762       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6763       break;
6764     case RID_HAS_NOTHROW_COPY:
6765       kind = CPTK_HAS_NOTHROW_COPY;
6766       break;
6767     case RID_HAS_TRIVIAL_ASSIGN:
6768       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6769       break;
6770     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6771       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6772       break;
6773     case RID_HAS_TRIVIAL_COPY:
6774       kind = CPTK_HAS_TRIVIAL_COPY;
6775       break;
6776     case RID_HAS_TRIVIAL_DESTRUCTOR:
6777       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6778       break;
6779     case RID_HAS_VIRTUAL_DESTRUCTOR:
6780       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6781       break;
6782     case RID_IS_ABSTRACT:
6783       kind = CPTK_IS_ABSTRACT;
6784       break;
6785     case RID_IS_BASE_OF:
6786       kind = CPTK_IS_BASE_OF;
6787       binary = true;
6788       break;
6789     case RID_IS_CLASS:
6790       kind = CPTK_IS_CLASS;
6791       break;
6792     case RID_IS_CONVERTIBLE_TO:
6793       kind = CPTK_IS_CONVERTIBLE_TO;
6794       binary = true;
6795       break;
6796     case RID_IS_EMPTY:
6797       kind = CPTK_IS_EMPTY;
6798       break;
6799     case RID_IS_ENUM:
6800       kind = CPTK_IS_ENUM;
6801       break;
6802     case RID_IS_POD:
6803       kind = CPTK_IS_POD;
6804       break;
6805     case RID_IS_POLYMORPHIC:
6806       kind = CPTK_IS_POLYMORPHIC;
6807       break;
6808     case RID_IS_UNION:
6809       kind = CPTK_IS_UNION;
6810       break;
6811     default:
6812       gcc_unreachable ();
6813     }
6814
6815   /* Consume the token.  */
6816   cp_lexer_consume_token (parser->lexer);
6817
6818   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6819
6820   type1 = cp_parser_type_id (parser);
6821
6822   if (type1 == error_mark_node)
6823     return error_mark_node;
6824
6825   /* Build a trivial decl-specifier-seq.  */
6826   clear_decl_specs (&decl_specs);
6827   decl_specs.type = type1;
6828
6829   /* Call grokdeclarator to figure out what type this is.  */
6830   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6831                           /*initialized=*/0, /*attrlist=*/NULL);
6832
6833   if (binary)
6834     {
6835       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6836  
6837       type2 = cp_parser_type_id (parser);
6838
6839       if (type2 == error_mark_node)
6840         return error_mark_node;
6841
6842       /* Build a trivial decl-specifier-seq.  */
6843       clear_decl_specs (&decl_specs);
6844       decl_specs.type = type2;
6845
6846       /* Call grokdeclarator to figure out what type this is.  */
6847       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6848                               /*initialized=*/0, /*attrlist=*/NULL);
6849     }
6850
6851   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6852
6853   /* Complete the trait expression, which may mean either processing
6854      the trait expr now or saving it for template instantiation.  */
6855   return finish_trait_expr (kind, type1, type2);
6856 }
6857
6858 /* Statements [gram.stmt.stmt]  */
6859
6860 /* Parse a statement.
6861
6862    statement:
6863      labeled-statement
6864      expression-statement
6865      compound-statement
6866      selection-statement
6867      iteration-statement
6868      jump-statement
6869      declaration-statement
6870      try-block
6871
6872   IN_COMPOUND is true when the statement is nested inside a
6873   cp_parser_compound_statement; this matters for certain pragmas.
6874
6875   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6876   is a (possibly labeled) if statement which is not enclosed in braces
6877   and has an else clause.  This is used to implement -Wparentheses.  */
6878
6879 static void
6880 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6881                      bool in_compound, bool *if_p)
6882 {
6883   tree statement;
6884   cp_token *token;
6885   location_t statement_location;
6886
6887  restart:
6888   if (if_p != NULL)
6889     *if_p = false;
6890   /* There is no statement yet.  */
6891   statement = NULL_TREE;
6892   /* Peek at the next token.  */
6893   token = cp_lexer_peek_token (parser->lexer);
6894   /* Remember the location of the first token in the statement.  */
6895   statement_location = token->location;
6896   /* If this is a keyword, then that will often determine what kind of
6897      statement we have.  */
6898   if (token->type == CPP_KEYWORD)
6899     {
6900       enum rid keyword = token->keyword;
6901
6902       switch (keyword)
6903         {
6904         case RID_CASE:
6905         case RID_DEFAULT:
6906           /* Looks like a labeled-statement with a case label.
6907              Parse the label, and then use tail recursion to parse
6908              the statement.  */
6909           cp_parser_label_for_labeled_statement (parser);
6910           goto restart;
6911
6912         case RID_IF:
6913         case RID_SWITCH:
6914           statement = cp_parser_selection_statement (parser, if_p);
6915           break;
6916
6917         case RID_WHILE:
6918         case RID_DO:
6919         case RID_FOR:
6920           statement = cp_parser_iteration_statement (parser);
6921           break;
6922
6923         case RID_BREAK:
6924         case RID_CONTINUE:
6925         case RID_RETURN:
6926         case RID_GOTO:
6927           statement = cp_parser_jump_statement (parser);
6928           break;
6929
6930           /* Objective-C++ exception-handling constructs.  */
6931         case RID_AT_TRY:
6932         case RID_AT_CATCH:
6933         case RID_AT_FINALLY:
6934         case RID_AT_SYNCHRONIZED:
6935         case RID_AT_THROW:
6936           statement = cp_parser_objc_statement (parser);
6937           break;
6938
6939         case RID_TRY:
6940           statement = cp_parser_try_block (parser);
6941           break;
6942
6943         case RID_NAMESPACE:
6944           /* This must be a namespace alias definition.  */
6945           cp_parser_declaration_statement (parser);
6946           return;
6947           
6948         default:
6949           /* It might be a keyword like `int' that can start a
6950              declaration-statement.  */
6951           break;
6952         }
6953     }
6954   else if (token->type == CPP_NAME)
6955     {
6956       /* If the next token is a `:', then we are looking at a
6957          labeled-statement.  */
6958       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6959       if (token->type == CPP_COLON)
6960         {
6961           /* Looks like a labeled-statement with an ordinary label.
6962              Parse the label, and then use tail recursion to parse
6963              the statement.  */
6964           cp_parser_label_for_labeled_statement (parser);
6965           goto restart;
6966         }
6967     }
6968   /* Anything that starts with a `{' must be a compound-statement.  */
6969   else if (token->type == CPP_OPEN_BRACE)
6970     statement = cp_parser_compound_statement (parser, NULL, false);
6971   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6972      a statement all its own.  */
6973   else if (token->type == CPP_PRAGMA)
6974     {
6975       /* Only certain OpenMP pragmas are attached to statements, and thus
6976          are considered statements themselves.  All others are not.  In
6977          the context of a compound, accept the pragma as a "statement" and
6978          return so that we can check for a close brace.  Otherwise we
6979          require a real statement and must go back and read one.  */
6980       if (in_compound)
6981         cp_parser_pragma (parser, pragma_compound);
6982       else if (!cp_parser_pragma (parser, pragma_stmt))
6983         goto restart;
6984       return;
6985     }
6986   else if (token->type == CPP_EOF)
6987     {
6988       cp_parser_error (parser, "expected statement");
6989       return;
6990     }
6991
6992   /* Everything else must be a declaration-statement or an
6993      expression-statement.  Try for the declaration-statement
6994      first, unless we are looking at a `;', in which case we know that
6995      we have an expression-statement.  */
6996   if (!statement)
6997     {
6998       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6999         {
7000           cp_parser_parse_tentatively (parser);
7001           /* Try to parse the declaration-statement.  */
7002           cp_parser_declaration_statement (parser);
7003           /* If that worked, we're done.  */
7004           if (cp_parser_parse_definitely (parser))
7005             return;
7006         }
7007       /* Look for an expression-statement instead.  */
7008       statement = cp_parser_expression_statement (parser, in_statement_expr);
7009     }
7010
7011   /* Set the line number for the statement.  */
7012   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7013     SET_EXPR_LOCATION (statement, statement_location);
7014 }
7015
7016 /* Parse the label for a labeled-statement, i.e.
7017
7018    identifier :
7019    case constant-expression :
7020    default :
7021
7022    GNU Extension:
7023    case constant-expression ... constant-expression : statement
7024
7025    When a label is parsed without errors, the label is added to the
7026    parse tree by the finish_* functions, so this function doesn't
7027    have to return the label.  */
7028
7029 static void
7030 cp_parser_label_for_labeled_statement (cp_parser* parser)
7031 {
7032   cp_token *token;
7033
7034   /* The next token should be an identifier.  */
7035   token = cp_lexer_peek_token (parser->lexer);
7036   if (token->type != CPP_NAME
7037       && token->type != CPP_KEYWORD)
7038     {
7039       cp_parser_error (parser, "expected labeled-statement");
7040       return;
7041     }
7042
7043   switch (token->keyword)
7044     {
7045     case RID_CASE:
7046       {
7047         tree expr, expr_hi;
7048         cp_token *ellipsis;
7049
7050         /* Consume the `case' token.  */
7051         cp_lexer_consume_token (parser->lexer);
7052         /* Parse the constant-expression.  */
7053         expr = cp_parser_constant_expression (parser,
7054                                               /*allow_non_constant_p=*/false,
7055                                               NULL);
7056
7057         ellipsis = cp_lexer_peek_token (parser->lexer);
7058         if (ellipsis->type == CPP_ELLIPSIS)
7059           {
7060             /* Consume the `...' token.  */
7061             cp_lexer_consume_token (parser->lexer);
7062             expr_hi =
7063               cp_parser_constant_expression (parser,
7064                                              /*allow_non_constant_p=*/false,
7065                                              NULL);
7066             /* We don't need to emit warnings here, as the common code
7067                will do this for us.  */
7068           }
7069         else
7070           expr_hi = NULL_TREE;
7071
7072         if (parser->in_switch_statement_p)
7073           finish_case_label (expr, expr_hi);
7074         else
7075           error ("%Hcase label %qE not within a switch statement",
7076                  &token->location, expr);
7077       }
7078       break;
7079
7080     case RID_DEFAULT:
7081       /* Consume the `default' token.  */
7082       cp_lexer_consume_token (parser->lexer);
7083
7084       if (parser->in_switch_statement_p)
7085         finish_case_label (NULL_TREE, NULL_TREE);
7086       else
7087         error ("%Hcase label not within a switch statement", &token->location);
7088       break;
7089
7090     default:
7091       /* Anything else must be an ordinary label.  */
7092       finish_label_stmt (cp_parser_identifier (parser));
7093       break;
7094     }
7095
7096   /* Require the `:' token.  */
7097   cp_parser_require (parser, CPP_COLON, "%<:%>");
7098 }
7099
7100 /* Parse an expression-statement.
7101
7102    expression-statement:
7103      expression [opt] ;
7104
7105    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7106    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7107    indicates whether this expression-statement is part of an
7108    expression statement.  */
7109
7110 static tree
7111 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7112 {
7113   tree statement = NULL_TREE;
7114
7115   /* If the next token is a ';', then there is no expression
7116      statement.  */
7117   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7118     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7119
7120   /* Consume the final `;'.  */
7121   cp_parser_consume_semicolon_at_end_of_statement (parser);
7122
7123   if (in_statement_expr
7124       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7125     /* This is the final expression statement of a statement
7126        expression.  */
7127     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7128   else if (statement)
7129     statement = finish_expr_stmt (statement);
7130   else
7131     finish_stmt ();
7132
7133   return statement;
7134 }
7135
7136 /* Parse a compound-statement.
7137
7138    compound-statement:
7139      { statement-seq [opt] }
7140
7141    GNU extension:
7142
7143    compound-statement:
7144      { label-declaration-seq [opt] statement-seq [opt] }
7145
7146    label-declaration-seq:
7147      label-declaration
7148      label-declaration-seq label-declaration
7149
7150    Returns a tree representing the statement.  */
7151
7152 static tree
7153 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7154                               bool in_try)
7155 {
7156   tree compound_stmt;
7157
7158   /* Consume the `{'.  */
7159   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7160     return error_mark_node;
7161   /* Begin the compound-statement.  */
7162   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7163   /* If the next keyword is `__label__' we have a label declaration.  */
7164   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7165     cp_parser_label_declaration (parser);
7166   /* Parse an (optional) statement-seq.  */
7167   cp_parser_statement_seq_opt (parser, in_statement_expr);
7168   /* Finish the compound-statement.  */
7169   finish_compound_stmt (compound_stmt);
7170   /* Consume the `}'.  */
7171   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7172
7173   return compound_stmt;
7174 }
7175
7176 /* Parse an (optional) statement-seq.
7177
7178    statement-seq:
7179      statement
7180      statement-seq [opt] statement  */
7181
7182 static void
7183 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7184 {
7185   /* Scan statements until there aren't any more.  */
7186   while (true)
7187     {
7188       cp_token *token = cp_lexer_peek_token (parser->lexer);
7189
7190       /* If we're looking at a `}', then we've run out of statements.  */
7191       if (token->type == CPP_CLOSE_BRACE
7192           || token->type == CPP_EOF
7193           || token->type == CPP_PRAGMA_EOL)
7194         break;
7195       
7196       /* If we are in a compound statement and find 'else' then
7197          something went wrong.  */
7198       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7199         {
7200           if (parser->in_statement & IN_IF_STMT) 
7201             break;
7202           else
7203             {
7204               token = cp_lexer_consume_token (parser->lexer);
7205               error ("%H%<else%> without a previous %<if%>", &token->location);
7206             }
7207         }
7208
7209       /* Parse the statement.  */
7210       cp_parser_statement (parser, in_statement_expr, true, NULL);
7211     }
7212 }
7213
7214 /* Parse a selection-statement.
7215
7216    selection-statement:
7217      if ( condition ) statement
7218      if ( condition ) statement else statement
7219      switch ( condition ) statement
7220
7221    Returns the new IF_STMT or SWITCH_STMT.
7222
7223    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7224    is a (possibly labeled) if statement which is not enclosed in
7225    braces and has an else clause.  This is used to implement
7226    -Wparentheses.  */
7227
7228 static tree
7229 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7230 {
7231   cp_token *token;
7232   enum rid keyword;
7233
7234   if (if_p != NULL)
7235     *if_p = false;
7236
7237   /* Peek at the next token.  */
7238   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7239
7240   /* See what kind of keyword it is.  */
7241   keyword = token->keyword;
7242   switch (keyword)
7243     {
7244     case RID_IF:
7245     case RID_SWITCH:
7246       {
7247         tree statement;
7248         tree condition;
7249
7250         /* Look for the `('.  */
7251         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7252           {
7253             cp_parser_skip_to_end_of_statement (parser);
7254             return error_mark_node;
7255           }
7256
7257         /* Begin the selection-statement.  */
7258         if (keyword == RID_IF)
7259           statement = begin_if_stmt ();
7260         else
7261           statement = begin_switch_stmt ();
7262
7263         /* Parse the condition.  */
7264         condition = cp_parser_condition (parser);
7265         /* Look for the `)'.  */
7266         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7267           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7268                                                  /*consume_paren=*/true);
7269
7270         if (keyword == RID_IF)
7271           {
7272             bool nested_if;
7273             unsigned char in_statement;
7274
7275             /* Add the condition.  */
7276             finish_if_stmt_cond (condition, statement);
7277
7278             /* Parse the then-clause.  */
7279             in_statement = parser->in_statement;
7280             parser->in_statement |= IN_IF_STMT;
7281             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7282               {
7283                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7284                 add_stmt (build_empty_stmt ());
7285                 cp_lexer_consume_token (parser->lexer);
7286                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7287                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7288                               "empty body in an %<if%> statement");
7289                 nested_if = false;
7290               }
7291             else
7292               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7293             parser->in_statement = in_statement;
7294
7295             finish_then_clause (statement);
7296
7297             /* If the next token is `else', parse the else-clause.  */
7298             if (cp_lexer_next_token_is_keyword (parser->lexer,
7299                                                 RID_ELSE))
7300               {
7301                 /* Consume the `else' keyword.  */
7302                 cp_lexer_consume_token (parser->lexer);
7303                 begin_else_clause (statement);
7304                 /* Parse the else-clause.  */
7305                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7306                   {
7307                     warning_at (cp_lexer_peek_token (parser->lexer)->location,
7308                                 OPT_Wempty_body, "suggest braces around "
7309                                 "empty body in an %<else%> statement");
7310                     add_stmt (build_empty_stmt ());
7311                     cp_lexer_consume_token (parser->lexer);
7312                   }
7313                 else
7314                   cp_parser_implicitly_scoped_statement (parser, NULL);
7315
7316                 finish_else_clause (statement);
7317
7318                 /* If we are currently parsing a then-clause, then
7319                    IF_P will not be NULL.  We set it to true to
7320                    indicate that this if statement has an else clause.
7321                    This may trigger the Wparentheses warning below
7322                    when we get back up to the parent if statement.  */
7323                 if (if_p != NULL)
7324                   *if_p = true;
7325               }
7326             else
7327               {
7328                 /* This if statement does not have an else clause.  If
7329                    NESTED_IF is true, then the then-clause is an if
7330                    statement which does have an else clause.  We warn
7331                    about the potential ambiguity.  */
7332                 if (nested_if)
7333                   warning (OPT_Wparentheses,
7334                            ("%Hsuggest explicit braces "
7335                             "to avoid ambiguous %<else%>"),
7336                            EXPR_LOCUS (statement));
7337               }
7338
7339             /* Now we're all done with the if-statement.  */
7340             finish_if_stmt (statement);
7341           }
7342         else
7343           {
7344             bool in_switch_statement_p;
7345             unsigned char in_statement;
7346
7347             /* Add the condition.  */
7348             finish_switch_cond (condition, statement);
7349
7350             /* Parse the body of the switch-statement.  */
7351             in_switch_statement_p = parser->in_switch_statement_p;
7352             in_statement = parser->in_statement;
7353             parser->in_switch_statement_p = true;
7354             parser->in_statement |= IN_SWITCH_STMT;
7355             cp_parser_implicitly_scoped_statement (parser, NULL);
7356             parser->in_switch_statement_p = in_switch_statement_p;
7357             parser->in_statement = in_statement;
7358
7359             /* Now we're all done with the switch-statement.  */
7360             finish_switch_stmt (statement);
7361           }
7362
7363         return statement;
7364       }
7365       break;
7366
7367     default:
7368       cp_parser_error (parser, "expected selection-statement");
7369       return error_mark_node;
7370     }
7371 }
7372
7373 /* Parse a condition.
7374
7375    condition:
7376      expression
7377      type-specifier-seq declarator = initializer-clause
7378      type-specifier-seq declarator braced-init-list
7379
7380    GNU Extension:
7381
7382    condition:
7383      type-specifier-seq declarator asm-specification [opt]
7384        attributes [opt] = assignment-expression
7385
7386    Returns the expression that should be tested.  */
7387
7388 static tree
7389 cp_parser_condition (cp_parser* parser)
7390 {
7391   cp_decl_specifier_seq type_specifiers;
7392   const char *saved_message;
7393
7394   /* Try the declaration first.  */
7395   cp_parser_parse_tentatively (parser);
7396   /* New types are not allowed in the type-specifier-seq for a
7397      condition.  */
7398   saved_message = parser->type_definition_forbidden_message;
7399   parser->type_definition_forbidden_message
7400     = "types may not be defined in conditions";
7401   /* Parse the type-specifier-seq.  */
7402   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7403                                 &type_specifiers);
7404   /* Restore the saved message.  */
7405   parser->type_definition_forbidden_message = saved_message;
7406   /* If all is well, we might be looking at a declaration.  */
7407   if (!cp_parser_error_occurred (parser))
7408     {
7409       tree decl;
7410       tree asm_specification;
7411       tree attributes;
7412       cp_declarator *declarator;
7413       tree initializer = NULL_TREE;
7414
7415       /* Parse the declarator.  */
7416       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7417                                          /*ctor_dtor_or_conv_p=*/NULL,
7418                                          /*parenthesized_p=*/NULL,
7419                                          /*member_p=*/false);
7420       /* Parse the attributes.  */
7421       attributes = cp_parser_attributes_opt (parser);
7422       /* Parse the asm-specification.  */
7423       asm_specification = cp_parser_asm_specification_opt (parser);
7424       /* If the next token is not an `=' or '{', then we might still be
7425          looking at an expression.  For example:
7426
7427            if (A(a).x)
7428
7429          looks like a decl-specifier-seq and a declarator -- but then
7430          there is no `=', so this is an expression.  */
7431       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7432           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7433         cp_parser_simulate_error (parser);
7434         
7435       /* If we did see an `=' or '{', then we are looking at a declaration
7436          for sure.  */
7437       if (cp_parser_parse_definitely (parser))
7438         {
7439           tree pushed_scope;
7440           bool non_constant_p;
7441           bool flags = LOOKUP_ONLYCONVERTING;
7442
7443           /* Create the declaration.  */
7444           decl = start_decl (declarator, &type_specifiers,
7445                              /*initialized_p=*/true,
7446                              attributes, /*prefix_attributes=*/NULL_TREE,
7447                              &pushed_scope);
7448
7449           /* Parse the initializer.  */
7450           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7451             {
7452               initializer = cp_parser_braced_list (parser, &non_constant_p);
7453               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7454               flags = 0;
7455             }
7456           else
7457             {
7458               /* Consume the `='.  */
7459               cp_parser_require (parser, CPP_EQ, "%<=%>");
7460               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7461             }
7462           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7463             maybe_warn_cpp0x ("extended initializer lists");
7464
7465           if (!non_constant_p)
7466             initializer = fold_non_dependent_expr (initializer);
7467
7468           /* Process the initializer.  */
7469           cp_finish_decl (decl,
7470                           initializer, !non_constant_p,
7471                           asm_specification,
7472                           flags);
7473
7474           if (pushed_scope)
7475             pop_scope (pushed_scope);
7476
7477           return convert_from_reference (decl);
7478         }
7479     }
7480   /* If we didn't even get past the declarator successfully, we are
7481      definitely not looking at a declaration.  */
7482   else
7483     cp_parser_abort_tentative_parse (parser);
7484
7485   /* Otherwise, we are looking at an expression.  */
7486   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7487 }
7488
7489 /* Parse an iteration-statement.
7490
7491    iteration-statement:
7492      while ( condition ) statement
7493      do statement while ( expression ) ;
7494      for ( for-init-statement condition [opt] ; expression [opt] )
7495        statement
7496
7497    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7498
7499 static tree
7500 cp_parser_iteration_statement (cp_parser* parser)
7501 {
7502   cp_token *token;
7503   enum rid keyword;
7504   tree statement;
7505   unsigned char in_statement;
7506
7507   /* Peek at the next token.  */
7508   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7509   if (!token)
7510     return error_mark_node;
7511
7512   /* Remember whether or not we are already within an iteration
7513      statement.  */
7514   in_statement = parser->in_statement;
7515
7516   /* See what kind of keyword it is.  */
7517   keyword = token->keyword;
7518   switch (keyword)
7519     {
7520     case RID_WHILE:
7521       {
7522         tree condition;
7523
7524         /* Begin the while-statement.  */
7525         statement = begin_while_stmt ();
7526         /* Look for the `('.  */
7527         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7528         /* Parse the condition.  */
7529         condition = cp_parser_condition (parser);
7530         finish_while_stmt_cond (condition, statement);
7531         /* Look for the `)'.  */
7532         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7533         /* Parse the dependent statement.  */
7534         parser->in_statement = IN_ITERATION_STMT;
7535         cp_parser_already_scoped_statement (parser);
7536         parser->in_statement = in_statement;
7537         /* We're done with the while-statement.  */
7538         finish_while_stmt (statement);
7539       }
7540       break;
7541
7542     case RID_DO:
7543       {
7544         tree expression;
7545
7546         /* Begin the do-statement.  */
7547         statement = begin_do_stmt ();
7548         /* Parse the body of the do-statement.  */
7549         parser->in_statement = IN_ITERATION_STMT;
7550         cp_parser_implicitly_scoped_statement (parser, NULL);
7551         parser->in_statement = in_statement;
7552         finish_do_body (statement);
7553         /* Look for the `while' keyword.  */
7554         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7555         /* Look for the `('.  */
7556         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7557         /* Parse the expression.  */
7558         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7559         /* We're done with the do-statement.  */
7560         finish_do_stmt (expression, statement);
7561         /* Look for the `)'.  */
7562         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7563         /* Look for the `;'.  */
7564         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7565       }
7566       break;
7567
7568     case RID_FOR:
7569       {
7570         tree condition = NULL_TREE;
7571         tree expression = NULL_TREE;
7572
7573         /* Begin the for-statement.  */
7574         statement = begin_for_stmt ();
7575         /* Look for the `('.  */
7576         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7577         /* Parse the initialization.  */
7578         cp_parser_for_init_statement (parser);
7579         finish_for_init_stmt (statement);
7580
7581         /* If there's a condition, process it.  */
7582         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7583           condition = cp_parser_condition (parser);
7584         finish_for_cond (condition, statement);
7585         /* Look for the `;'.  */
7586         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7587
7588         /* If there's an expression, process it.  */
7589         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7590           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7591         finish_for_expr (expression, statement);
7592         /* Look for the `)'.  */
7593         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7594
7595         /* Parse the body of the for-statement.  */
7596         parser->in_statement = IN_ITERATION_STMT;
7597         cp_parser_already_scoped_statement (parser);
7598         parser->in_statement = in_statement;
7599
7600         /* We're done with the for-statement.  */
7601         finish_for_stmt (statement);
7602       }
7603       break;
7604
7605     default:
7606       cp_parser_error (parser, "expected iteration-statement");
7607       statement = error_mark_node;
7608       break;
7609     }
7610
7611   return statement;
7612 }
7613
7614 /* Parse a for-init-statement.
7615
7616    for-init-statement:
7617      expression-statement
7618      simple-declaration  */
7619
7620 static void
7621 cp_parser_for_init_statement (cp_parser* parser)
7622 {
7623   /* If the next token is a `;', then we have an empty
7624      expression-statement.  Grammatically, this is also a
7625      simple-declaration, but an invalid one, because it does not
7626      declare anything.  Therefore, if we did not handle this case
7627      specially, we would issue an error message about an invalid
7628      declaration.  */
7629   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7630     {
7631       /* We're going to speculatively look for a declaration, falling back
7632          to an expression, if necessary.  */
7633       cp_parser_parse_tentatively (parser);
7634       /* Parse the declaration.  */
7635       cp_parser_simple_declaration (parser,
7636                                     /*function_definition_allowed_p=*/false);
7637       /* If the tentative parse failed, then we shall need to look for an
7638          expression-statement.  */
7639       if (cp_parser_parse_definitely (parser))
7640         return;
7641     }
7642
7643   cp_parser_expression_statement (parser, false);
7644 }
7645
7646 /* Parse a jump-statement.
7647
7648    jump-statement:
7649      break ;
7650      continue ;
7651      return expression [opt] ;
7652      return braced-init-list ;
7653      goto identifier ;
7654
7655    GNU extension:
7656
7657    jump-statement:
7658      goto * expression ;
7659
7660    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7661
7662 static tree
7663 cp_parser_jump_statement (cp_parser* parser)
7664 {
7665   tree statement = error_mark_node;
7666   cp_token *token;
7667   enum rid keyword;
7668   unsigned char in_statement;
7669
7670   /* Peek at the next token.  */
7671   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7672   if (!token)
7673     return error_mark_node;
7674
7675   /* See what kind of keyword it is.  */
7676   keyword = token->keyword;
7677   switch (keyword)
7678     {
7679     case RID_BREAK:
7680       in_statement = parser->in_statement & ~IN_IF_STMT;      
7681       switch (in_statement)
7682         {
7683         case 0:
7684           error ("%Hbreak statement not within loop or switch", &token->location);
7685           break;
7686         default:
7687           gcc_assert ((in_statement & IN_SWITCH_STMT)
7688                       || in_statement == IN_ITERATION_STMT);
7689           statement = finish_break_stmt ();
7690           break;
7691         case IN_OMP_BLOCK:
7692           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7693           break;
7694         case IN_OMP_FOR:
7695           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7696           break;
7697         }
7698       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7699       break;
7700
7701     case RID_CONTINUE:
7702       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7703         {
7704         case 0:
7705           error ("%Hcontinue statement not within a loop", &token->location);
7706           break;
7707         case IN_ITERATION_STMT:
7708         case IN_OMP_FOR:
7709           statement = finish_continue_stmt ();
7710           break;
7711         case IN_OMP_BLOCK:
7712           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7713           break;
7714         default:
7715           gcc_unreachable ();
7716         }
7717       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7718       break;
7719
7720     case RID_RETURN:
7721       {
7722         tree expr;
7723         bool expr_non_constant_p;
7724
7725         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7726           {
7727             maybe_warn_cpp0x ("extended initializer lists");
7728             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7729           }
7730         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7731           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7732         else
7733           /* If the next token is a `;', then there is no
7734              expression.  */
7735           expr = NULL_TREE;
7736         /* Build the return-statement.  */
7737         statement = finish_return_stmt (expr);
7738         /* Look for the final `;'.  */
7739         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7740       }
7741       break;
7742
7743     case RID_GOTO:
7744       /* Create the goto-statement.  */
7745       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7746         {
7747           /* Issue a warning about this use of a GNU extension.  */
7748           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7749           /* Consume the '*' token.  */
7750           cp_lexer_consume_token (parser->lexer);
7751           /* Parse the dependent expression.  */
7752           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7753         }
7754       else
7755         finish_goto_stmt (cp_parser_identifier (parser));
7756       /* Look for the final `;'.  */
7757       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7758       break;
7759
7760     default:
7761       cp_parser_error (parser, "expected jump-statement");
7762       break;
7763     }
7764
7765   return statement;
7766 }
7767
7768 /* Parse a declaration-statement.
7769
7770    declaration-statement:
7771      block-declaration  */
7772
7773 static void
7774 cp_parser_declaration_statement (cp_parser* parser)
7775 {
7776   void *p;
7777
7778   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7779   p = obstack_alloc (&declarator_obstack, 0);
7780
7781  /* Parse the block-declaration.  */
7782   cp_parser_block_declaration (parser, /*statement_p=*/true);
7783
7784   /* Free any declarators allocated.  */
7785   obstack_free (&declarator_obstack, p);
7786
7787   /* Finish off the statement.  */
7788   finish_stmt ();
7789 }
7790
7791 /* Some dependent statements (like `if (cond) statement'), are
7792    implicitly in their own scope.  In other words, if the statement is
7793    a single statement (as opposed to a compound-statement), it is
7794    none-the-less treated as if it were enclosed in braces.  Any
7795    declarations appearing in the dependent statement are out of scope
7796    after control passes that point.  This function parses a statement,
7797    but ensures that is in its own scope, even if it is not a
7798    compound-statement.
7799
7800    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7801    is a (possibly labeled) if statement which is not enclosed in
7802    braces and has an else clause.  This is used to implement
7803    -Wparentheses.
7804
7805    Returns the new statement.  */
7806
7807 static tree
7808 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7809 {
7810   tree statement;
7811
7812   if (if_p != NULL)
7813     *if_p = false;
7814
7815   /* Mark if () ; with a special NOP_EXPR.  */
7816   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7817     {
7818       cp_lexer_consume_token (parser->lexer);
7819       statement = add_stmt (build_empty_stmt ());
7820     }
7821   /* if a compound is opened, we simply parse the statement directly.  */
7822   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7823     statement = cp_parser_compound_statement (parser, NULL, false);
7824   /* If the token is not a `{', then we must take special action.  */
7825   else
7826     {
7827       /* Create a compound-statement.  */
7828       statement = begin_compound_stmt (0);
7829       /* Parse the dependent-statement.  */
7830       cp_parser_statement (parser, NULL_TREE, false, if_p);
7831       /* Finish the dummy compound-statement.  */
7832       finish_compound_stmt (statement);
7833     }
7834
7835   /* Return the statement.  */
7836   return statement;
7837 }
7838
7839 /* For some dependent statements (like `while (cond) statement'), we
7840    have already created a scope.  Therefore, even if the dependent
7841    statement is a compound-statement, we do not want to create another
7842    scope.  */
7843
7844 static void
7845 cp_parser_already_scoped_statement (cp_parser* parser)
7846 {
7847   /* If the token is a `{', then we must take special action.  */
7848   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7849     cp_parser_statement (parser, NULL_TREE, false, NULL);
7850   else
7851     {
7852       /* Avoid calling cp_parser_compound_statement, so that we
7853          don't create a new scope.  Do everything else by hand.  */
7854       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7855       /* If the next keyword is `__label__' we have a label declaration.  */
7856       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7857         cp_parser_label_declaration (parser);
7858       /* Parse an (optional) statement-seq.  */
7859       cp_parser_statement_seq_opt (parser, NULL_TREE);
7860       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7861     }
7862 }
7863
7864 /* Declarations [gram.dcl.dcl] */
7865
7866 /* Parse an optional declaration-sequence.
7867
7868    declaration-seq:
7869      declaration
7870      declaration-seq declaration  */
7871
7872 static void
7873 cp_parser_declaration_seq_opt (cp_parser* parser)
7874 {
7875   while (true)
7876     {
7877       cp_token *token;
7878
7879       token = cp_lexer_peek_token (parser->lexer);
7880
7881       if (token->type == CPP_CLOSE_BRACE
7882           || token->type == CPP_EOF
7883           || token->type == CPP_PRAGMA_EOL)
7884         break;
7885
7886       if (token->type == CPP_SEMICOLON)
7887         {
7888           /* A declaration consisting of a single semicolon is
7889              invalid.  Allow it unless we're being pedantic.  */
7890           cp_lexer_consume_token (parser->lexer);
7891           if (!in_system_header)
7892             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7893           continue;
7894         }
7895
7896       /* If we're entering or exiting a region that's implicitly
7897          extern "C", modify the lang context appropriately.  */
7898       if (!parser->implicit_extern_c && token->implicit_extern_c)
7899         {
7900           push_lang_context (lang_name_c);
7901           parser->implicit_extern_c = true;
7902         }
7903       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7904         {
7905           pop_lang_context ();
7906           parser->implicit_extern_c = false;
7907         }
7908
7909       if (token->type == CPP_PRAGMA)
7910         {
7911           /* A top-level declaration can consist solely of a #pragma.
7912              A nested declaration cannot, so this is done here and not
7913              in cp_parser_declaration.  (A #pragma at block scope is
7914              handled in cp_parser_statement.)  */
7915           cp_parser_pragma (parser, pragma_external);
7916           continue;
7917         }
7918
7919       /* Parse the declaration itself.  */
7920       cp_parser_declaration (parser);
7921     }
7922 }
7923
7924 /* Parse a declaration.
7925
7926    declaration:
7927      block-declaration
7928      function-definition
7929      template-declaration
7930      explicit-instantiation
7931      explicit-specialization
7932      linkage-specification
7933      namespace-definition
7934
7935    GNU extension:
7936
7937    declaration:
7938       __extension__ declaration */
7939
7940 static void
7941 cp_parser_declaration (cp_parser* parser)
7942 {
7943   cp_token token1;
7944   cp_token token2;
7945   int saved_pedantic;
7946   void *p;
7947
7948   /* Check for the `__extension__' keyword.  */
7949   if (cp_parser_extension_opt (parser, &saved_pedantic))
7950     {
7951       /* Parse the qualified declaration.  */
7952       cp_parser_declaration (parser);
7953       /* Restore the PEDANTIC flag.  */
7954       pedantic = saved_pedantic;
7955
7956       return;
7957     }
7958
7959   /* Try to figure out what kind of declaration is present.  */
7960   token1 = *cp_lexer_peek_token (parser->lexer);
7961
7962   if (token1.type != CPP_EOF)
7963     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7964   else
7965     {
7966       token2.type = CPP_EOF;
7967       token2.keyword = RID_MAX;
7968     }
7969
7970   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7971   p = obstack_alloc (&declarator_obstack, 0);
7972
7973   /* If the next token is `extern' and the following token is a string
7974      literal, then we have a linkage specification.  */
7975   if (token1.keyword == RID_EXTERN
7976       && cp_parser_is_string_literal (&token2))
7977     cp_parser_linkage_specification (parser);
7978   /* If the next token is `template', then we have either a template
7979      declaration, an explicit instantiation, or an explicit
7980      specialization.  */
7981   else if (token1.keyword == RID_TEMPLATE)
7982     {
7983       /* `template <>' indicates a template specialization.  */
7984       if (token2.type == CPP_LESS
7985           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7986         cp_parser_explicit_specialization (parser);
7987       /* `template <' indicates a template declaration.  */
7988       else if (token2.type == CPP_LESS)
7989         cp_parser_template_declaration (parser, /*member_p=*/false);
7990       /* Anything else must be an explicit instantiation.  */
7991       else
7992         cp_parser_explicit_instantiation (parser);
7993     }
7994   /* If the next token is `export', then we have a template
7995      declaration.  */
7996   else if (token1.keyword == RID_EXPORT)
7997     cp_parser_template_declaration (parser, /*member_p=*/false);
7998   /* If the next token is `extern', 'static' or 'inline' and the one
7999      after that is `template', we have a GNU extended explicit
8000      instantiation directive.  */
8001   else if (cp_parser_allow_gnu_extensions_p (parser)
8002            && (token1.keyword == RID_EXTERN
8003                || token1.keyword == RID_STATIC
8004                || token1.keyword == RID_INLINE)
8005            && token2.keyword == RID_TEMPLATE)
8006     cp_parser_explicit_instantiation (parser);
8007   /* If the next token is `namespace', check for a named or unnamed
8008      namespace definition.  */
8009   else if (token1.keyword == RID_NAMESPACE
8010            && (/* A named namespace definition.  */
8011                (token2.type == CPP_NAME
8012                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8013                     != CPP_EQ))
8014                /* An unnamed namespace definition.  */
8015                || token2.type == CPP_OPEN_BRACE
8016                || token2.keyword == RID_ATTRIBUTE))
8017     cp_parser_namespace_definition (parser);
8018   /* An inline (associated) namespace definition.  */
8019   else if (token1.keyword == RID_INLINE
8020            && token2.keyword == RID_NAMESPACE)
8021     cp_parser_namespace_definition (parser);
8022   /* Objective-C++ declaration/definition.  */
8023   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8024     cp_parser_objc_declaration (parser);
8025   /* We must have either a block declaration or a function
8026      definition.  */
8027   else
8028     /* Try to parse a block-declaration, or a function-definition.  */
8029     cp_parser_block_declaration (parser, /*statement_p=*/false);
8030
8031   /* Free any declarators allocated.  */
8032   obstack_free (&declarator_obstack, p);
8033 }
8034
8035 /* Parse a block-declaration.
8036
8037    block-declaration:
8038      simple-declaration
8039      asm-definition
8040      namespace-alias-definition
8041      using-declaration
8042      using-directive
8043
8044    GNU Extension:
8045
8046    block-declaration:
8047      __extension__ block-declaration
8048
8049    C++0x Extension:
8050
8051    block-declaration:
8052      static_assert-declaration
8053
8054    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8055    part of a declaration-statement.  */
8056
8057 static void
8058 cp_parser_block_declaration (cp_parser *parser,
8059                              bool      statement_p)
8060 {
8061   cp_token *token1;
8062   int saved_pedantic;
8063
8064   /* Check for the `__extension__' keyword.  */
8065   if (cp_parser_extension_opt (parser, &saved_pedantic))
8066     {
8067       /* Parse the qualified declaration.  */
8068       cp_parser_block_declaration (parser, statement_p);
8069       /* Restore the PEDANTIC flag.  */
8070       pedantic = saved_pedantic;
8071
8072       return;
8073     }
8074
8075   /* Peek at the next token to figure out which kind of declaration is
8076      present.  */
8077   token1 = cp_lexer_peek_token (parser->lexer);
8078
8079   /* If the next keyword is `asm', we have an asm-definition.  */
8080   if (token1->keyword == RID_ASM)
8081     {
8082       if (statement_p)
8083         cp_parser_commit_to_tentative_parse (parser);
8084       cp_parser_asm_definition (parser);
8085     }
8086   /* If the next keyword is `namespace', we have a
8087      namespace-alias-definition.  */
8088   else if (token1->keyword == RID_NAMESPACE)
8089     cp_parser_namespace_alias_definition (parser);
8090   /* If the next keyword is `using', we have either a
8091      using-declaration or a using-directive.  */
8092   else if (token1->keyword == RID_USING)
8093     {
8094       cp_token *token2;
8095
8096       if (statement_p)
8097         cp_parser_commit_to_tentative_parse (parser);
8098       /* If the token after `using' is `namespace', then we have a
8099          using-directive.  */
8100       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8101       if (token2->keyword == RID_NAMESPACE)
8102         cp_parser_using_directive (parser);
8103       /* Otherwise, it's a using-declaration.  */
8104       else
8105         cp_parser_using_declaration (parser,
8106                                      /*access_declaration_p=*/false);
8107     }
8108   /* If the next keyword is `__label__' we have a misplaced label
8109      declaration.  */
8110   else if (token1->keyword == RID_LABEL)
8111     {
8112       cp_lexer_consume_token (parser->lexer);
8113       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8114       cp_parser_skip_to_end_of_statement (parser);
8115       /* If the next token is now a `;', consume it.  */
8116       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8117         cp_lexer_consume_token (parser->lexer);
8118     }
8119   /* If the next token is `static_assert' we have a static assertion.  */
8120   else if (token1->keyword == RID_STATIC_ASSERT)
8121     cp_parser_static_assert (parser, /*member_p=*/false);
8122   /* Anything else must be a simple-declaration.  */
8123   else
8124     cp_parser_simple_declaration (parser, !statement_p);
8125 }
8126
8127 /* Parse a simple-declaration.
8128
8129    simple-declaration:
8130      decl-specifier-seq [opt] init-declarator-list [opt] ;
8131
8132    init-declarator-list:
8133      init-declarator
8134      init-declarator-list , init-declarator
8135
8136    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8137    function-definition as a simple-declaration.  */
8138
8139 static void
8140 cp_parser_simple_declaration (cp_parser* parser,
8141                               bool function_definition_allowed_p)
8142 {
8143   cp_decl_specifier_seq decl_specifiers;
8144   int declares_class_or_enum;
8145   bool saw_declarator;
8146
8147   /* Defer access checks until we know what is being declared; the
8148      checks for names appearing in the decl-specifier-seq should be
8149      done as if we were in the scope of the thing being declared.  */
8150   push_deferring_access_checks (dk_deferred);
8151
8152   /* Parse the decl-specifier-seq.  We have to keep track of whether
8153      or not the decl-specifier-seq declares a named class or
8154      enumeration type, since that is the only case in which the
8155      init-declarator-list is allowed to be empty.
8156
8157      [dcl.dcl]
8158
8159      In a simple-declaration, the optional init-declarator-list can be
8160      omitted only when declaring a class or enumeration, that is when
8161      the decl-specifier-seq contains either a class-specifier, an
8162      elaborated-type-specifier, or an enum-specifier.  */
8163   cp_parser_decl_specifier_seq (parser,
8164                                 CP_PARSER_FLAGS_OPTIONAL,
8165                                 &decl_specifiers,
8166                                 &declares_class_or_enum);
8167   /* We no longer need to defer access checks.  */
8168   stop_deferring_access_checks ();
8169
8170   /* In a block scope, a valid declaration must always have a
8171      decl-specifier-seq.  By not trying to parse declarators, we can
8172      resolve the declaration/expression ambiguity more quickly.  */
8173   if (!function_definition_allowed_p
8174       && !decl_specifiers.any_specifiers_p)
8175     {
8176       cp_parser_error (parser, "expected declaration");
8177       goto done;
8178     }
8179
8180   /* If the next two tokens are both identifiers, the code is
8181      erroneous. The usual cause of this situation is code like:
8182
8183        T t;
8184
8185      where "T" should name a type -- but does not.  */
8186   if (!decl_specifiers.type
8187       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8188     {
8189       /* If parsing tentatively, we should commit; we really are
8190          looking at a declaration.  */
8191       cp_parser_commit_to_tentative_parse (parser);
8192       /* Give up.  */
8193       goto done;
8194     }
8195
8196   /* If we have seen at least one decl-specifier, and the next token
8197      is not a parenthesis, then we must be looking at a declaration.
8198      (After "int (" we might be looking at a functional cast.)  */
8199   if (decl_specifiers.any_specifiers_p
8200       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8201       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8202       && !cp_parser_error_occurred (parser))
8203     cp_parser_commit_to_tentative_parse (parser);
8204
8205   /* Keep going until we hit the `;' at the end of the simple
8206      declaration.  */
8207   saw_declarator = false;
8208   while (cp_lexer_next_token_is_not (parser->lexer,
8209                                      CPP_SEMICOLON))
8210     {
8211       cp_token *token;
8212       bool function_definition_p;
8213       tree decl;
8214
8215       if (saw_declarator)
8216         {
8217           /* If we are processing next declarator, coma is expected */
8218           token = cp_lexer_peek_token (parser->lexer);
8219           gcc_assert (token->type == CPP_COMMA);
8220           cp_lexer_consume_token (parser->lexer);
8221         }
8222       else
8223         saw_declarator = true;
8224
8225       /* Parse the init-declarator.  */
8226       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8227                                         /*checks=*/NULL,
8228                                         function_definition_allowed_p,
8229                                         /*member_p=*/false,
8230                                         declares_class_or_enum,
8231                                         &function_definition_p);
8232       /* If an error occurred while parsing tentatively, exit quickly.
8233          (That usually happens when in the body of a function; each
8234          statement is treated as a declaration-statement until proven
8235          otherwise.)  */
8236       if (cp_parser_error_occurred (parser))
8237         goto done;
8238       /* Handle function definitions specially.  */
8239       if (function_definition_p)
8240         {
8241           /* If the next token is a `,', then we are probably
8242              processing something like:
8243
8244                void f() {}, *p;
8245
8246              which is erroneous.  */
8247           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8248             {
8249               cp_token *token = cp_lexer_peek_token (parser->lexer);
8250               error ("%Hmixing declarations and function-definitions is forbidden",
8251                      &token->location);
8252             }
8253           /* Otherwise, we're done with the list of declarators.  */
8254           else
8255             {
8256               pop_deferring_access_checks ();
8257               return;
8258             }
8259         }
8260       /* The next token should be either a `,' or a `;'.  */
8261       token = cp_lexer_peek_token (parser->lexer);
8262       /* If it's a `,', there are more declarators to come.  */
8263       if (token->type == CPP_COMMA)
8264         /* will be consumed next time around */;
8265       /* If it's a `;', we are done.  */
8266       else if (token->type == CPP_SEMICOLON)
8267         break;
8268       /* Anything else is an error.  */
8269       else
8270         {
8271           /* If we have already issued an error message we don't need
8272              to issue another one.  */
8273           if (decl != error_mark_node
8274               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8275             cp_parser_error (parser, "expected %<,%> or %<;%>");
8276           /* Skip tokens until we reach the end of the statement.  */
8277           cp_parser_skip_to_end_of_statement (parser);
8278           /* If the next token is now a `;', consume it.  */
8279           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8280             cp_lexer_consume_token (parser->lexer);
8281           goto done;
8282         }
8283       /* After the first time around, a function-definition is not
8284          allowed -- even if it was OK at first.  For example:
8285
8286            int i, f() {}
8287
8288          is not valid.  */
8289       function_definition_allowed_p = false;
8290     }
8291
8292   /* Issue an error message if no declarators are present, and the
8293      decl-specifier-seq does not itself declare a class or
8294      enumeration.  */
8295   if (!saw_declarator)
8296     {
8297       if (cp_parser_declares_only_class_p (parser))
8298         shadow_tag (&decl_specifiers);
8299       /* Perform any deferred access checks.  */
8300       perform_deferred_access_checks ();
8301     }
8302
8303   /* Consume the `;'.  */
8304   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8305
8306  done:
8307   pop_deferring_access_checks ();
8308 }
8309
8310 /* Parse a decl-specifier-seq.
8311
8312    decl-specifier-seq:
8313      decl-specifier-seq [opt] decl-specifier
8314
8315    decl-specifier:
8316      storage-class-specifier
8317      type-specifier
8318      function-specifier
8319      friend
8320      typedef
8321
8322    GNU Extension:
8323
8324    decl-specifier:
8325      attributes
8326
8327    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8328
8329    The parser flags FLAGS is used to control type-specifier parsing.
8330
8331    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8332    flags:
8333
8334      1: one of the decl-specifiers is an elaborated-type-specifier
8335         (i.e., a type declaration)
8336      2: one of the decl-specifiers is an enum-specifier or a
8337         class-specifier (i.e., a type definition)
8338
8339    */
8340
8341 static void
8342 cp_parser_decl_specifier_seq (cp_parser* parser,
8343                               cp_parser_flags flags,
8344                               cp_decl_specifier_seq *decl_specs,
8345                               int* declares_class_or_enum)
8346 {
8347   bool constructor_possible_p = !parser->in_declarator_p;
8348   cp_token *start_token = NULL;
8349
8350   /* Clear DECL_SPECS.  */
8351   clear_decl_specs (decl_specs);
8352
8353   /* Assume no class or enumeration type is declared.  */
8354   *declares_class_or_enum = 0;
8355
8356   /* Keep reading specifiers until there are no more to read.  */
8357   while (true)
8358     {
8359       bool constructor_p;
8360       bool found_decl_spec;
8361       cp_token *token;
8362
8363       /* Peek at the next token.  */
8364       token = cp_lexer_peek_token (parser->lexer);
8365
8366       /* Save the first token of the decl spec list for error
8367          reporting.  */
8368       if (!start_token)
8369         start_token = token;
8370       /* Handle attributes.  */
8371       if (token->keyword == RID_ATTRIBUTE)
8372         {
8373           /* Parse the attributes.  */
8374           decl_specs->attributes
8375             = chainon (decl_specs->attributes,
8376                        cp_parser_attributes_opt (parser));
8377           continue;
8378         }
8379       /* Assume we will find a decl-specifier keyword.  */
8380       found_decl_spec = true;
8381       /* If the next token is an appropriate keyword, we can simply
8382          add it to the list.  */
8383       switch (token->keyword)
8384         {
8385           /* decl-specifier:
8386                friend  */
8387         case RID_FRIEND:
8388           if (!at_class_scope_p ())
8389             {
8390               error ("%H%<friend%> used outside of class", &token->location);
8391               cp_lexer_purge_token (parser->lexer);
8392             }
8393           else
8394             {
8395               ++decl_specs->specs[(int) ds_friend];
8396               /* Consume the token.  */
8397               cp_lexer_consume_token (parser->lexer);
8398             }
8399           break;
8400
8401           /* function-specifier:
8402                inline
8403                virtual
8404                explicit  */
8405         case RID_INLINE:
8406         case RID_VIRTUAL:
8407         case RID_EXPLICIT:
8408           cp_parser_function_specifier_opt (parser, decl_specs);
8409           break;
8410
8411           /* decl-specifier:
8412                typedef  */
8413         case RID_TYPEDEF:
8414           ++decl_specs->specs[(int) ds_typedef];
8415           /* Consume the token.  */
8416           cp_lexer_consume_token (parser->lexer);
8417           /* A constructor declarator cannot appear in a typedef.  */
8418           constructor_possible_p = false;
8419           /* The "typedef" keyword can only occur in a declaration; we
8420              may as well commit at this point.  */
8421           cp_parser_commit_to_tentative_parse (parser);
8422
8423           if (decl_specs->storage_class != sc_none)
8424             decl_specs->conflicting_specifiers_p = true;
8425           break;
8426
8427           /* storage-class-specifier:
8428                auto
8429                register
8430                static
8431                extern
8432                mutable
8433
8434              GNU Extension:
8435                thread  */
8436         case RID_AUTO:
8437           if (cxx_dialect == cxx98) 
8438             {
8439               /* Consume the token.  */
8440               cp_lexer_consume_token (parser->lexer);
8441
8442               /* Complain about `auto' as a storage specifier, if
8443                  we're complaining about C++0x compatibility.  */
8444               warning 
8445                 (OPT_Wc__0x_compat, 
8446                  "%H%<auto%> will change meaning in C++0x; please remove it",
8447                  &token->location);
8448
8449               /* Set the storage class anyway.  */
8450               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8451                                            token->location);
8452             }
8453           else
8454             /* C++0x auto type-specifier.  */
8455             found_decl_spec = false;
8456           break;
8457
8458         case RID_REGISTER:
8459         case RID_STATIC:
8460         case RID_EXTERN:
8461         case RID_MUTABLE:
8462           /* Consume the token.  */
8463           cp_lexer_consume_token (parser->lexer);
8464           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8465                                        token->location);
8466           break;
8467         case RID_THREAD:
8468           /* Consume the token.  */
8469           cp_lexer_consume_token (parser->lexer);
8470           ++decl_specs->specs[(int) ds_thread];
8471           break;
8472
8473         default:
8474           /* We did not yet find a decl-specifier yet.  */
8475           found_decl_spec = false;
8476           break;
8477         }
8478
8479       /* Constructors are a special case.  The `S' in `S()' is not a
8480          decl-specifier; it is the beginning of the declarator.  */
8481       constructor_p
8482         = (!found_decl_spec
8483            && constructor_possible_p
8484            && (cp_parser_constructor_declarator_p
8485                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8486
8487       /* If we don't have a DECL_SPEC yet, then we must be looking at
8488          a type-specifier.  */
8489       if (!found_decl_spec && !constructor_p)
8490         {
8491           int decl_spec_declares_class_or_enum;
8492           bool is_cv_qualifier;
8493           tree type_spec;
8494
8495           type_spec
8496             = cp_parser_type_specifier (parser, flags,
8497                                         decl_specs,
8498                                         /*is_declaration=*/true,
8499                                         &decl_spec_declares_class_or_enum,
8500                                         &is_cv_qualifier);
8501           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8502
8503           /* If this type-specifier referenced a user-defined type
8504              (a typedef, class-name, etc.), then we can't allow any
8505              more such type-specifiers henceforth.
8506
8507              [dcl.spec]
8508
8509              The longest sequence of decl-specifiers that could
8510              possibly be a type name is taken as the
8511              decl-specifier-seq of a declaration.  The sequence shall
8512              be self-consistent as described below.
8513
8514              [dcl.type]
8515
8516              As a general rule, at most one type-specifier is allowed
8517              in the complete decl-specifier-seq of a declaration.  The
8518              only exceptions are the following:
8519
8520              -- const or volatile can be combined with any other
8521                 type-specifier.
8522
8523              -- signed or unsigned can be combined with char, long,
8524                 short, or int.
8525
8526              -- ..
8527
8528              Example:
8529
8530                typedef char* Pc;
8531                void g (const int Pc);
8532
8533              Here, Pc is *not* part of the decl-specifier seq; it's
8534              the declarator.  Therefore, once we see a type-specifier
8535              (other than a cv-qualifier), we forbid any additional
8536              user-defined types.  We *do* still allow things like `int
8537              int' to be considered a decl-specifier-seq, and issue the
8538              error message later.  */
8539           if (type_spec && !is_cv_qualifier)
8540             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8541           /* A constructor declarator cannot follow a type-specifier.  */
8542           if (type_spec)
8543             {
8544               constructor_possible_p = false;
8545               found_decl_spec = true;
8546             }
8547         }
8548
8549       /* If we still do not have a DECL_SPEC, then there are no more
8550          decl-specifiers.  */
8551       if (!found_decl_spec)
8552         break;
8553
8554       decl_specs->any_specifiers_p = true;
8555       /* After we see one decl-specifier, further decl-specifiers are
8556          always optional.  */
8557       flags |= CP_PARSER_FLAGS_OPTIONAL;
8558     }
8559
8560   cp_parser_check_decl_spec (decl_specs, start_token->location);
8561
8562   /* Don't allow a friend specifier with a class definition.  */
8563   if (decl_specs->specs[(int) ds_friend] != 0
8564       && (*declares_class_or_enum & 2))
8565     error ("%Hclass definition may not be declared a friend",
8566             &start_token->location);
8567 }
8568
8569 /* Parse an (optional) storage-class-specifier.
8570
8571    storage-class-specifier:
8572      auto
8573      register
8574      static
8575      extern
8576      mutable
8577
8578    GNU Extension:
8579
8580    storage-class-specifier:
8581      thread
8582
8583    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8584
8585 static tree
8586 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8587 {
8588   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8589     {
8590     case RID_AUTO:
8591       if (cxx_dialect != cxx98)
8592         return NULL_TREE;
8593       /* Fall through for C++98.  */
8594
8595     case RID_REGISTER:
8596     case RID_STATIC:
8597     case RID_EXTERN:
8598     case RID_MUTABLE:
8599     case RID_THREAD:
8600       /* Consume the token.  */
8601       return cp_lexer_consume_token (parser->lexer)->u.value;
8602
8603     default:
8604       return NULL_TREE;
8605     }
8606 }
8607
8608 /* Parse an (optional) function-specifier.
8609
8610    function-specifier:
8611      inline
8612      virtual
8613      explicit
8614
8615    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8616    Updates DECL_SPECS, if it is non-NULL.  */
8617
8618 static tree
8619 cp_parser_function_specifier_opt (cp_parser* parser,
8620                                   cp_decl_specifier_seq *decl_specs)
8621 {
8622   cp_token *token = cp_lexer_peek_token (parser->lexer);
8623   switch (token->keyword)
8624     {
8625     case RID_INLINE:
8626       if (decl_specs)
8627         ++decl_specs->specs[(int) ds_inline];
8628       break;
8629
8630     case RID_VIRTUAL:
8631       /* 14.5.2.3 [temp.mem]
8632
8633          A member function template shall not be virtual.  */
8634       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8635         error ("%Htemplates may not be %<virtual%>", &token->location);
8636       else if (decl_specs)
8637         ++decl_specs->specs[(int) ds_virtual];
8638       break;
8639
8640     case RID_EXPLICIT:
8641       if (decl_specs)
8642         ++decl_specs->specs[(int) ds_explicit];
8643       break;
8644
8645     default:
8646       return NULL_TREE;
8647     }
8648
8649   /* Consume the token.  */
8650   return cp_lexer_consume_token (parser->lexer)->u.value;
8651 }
8652
8653 /* Parse a linkage-specification.
8654
8655    linkage-specification:
8656      extern string-literal { declaration-seq [opt] }
8657      extern string-literal declaration  */
8658
8659 static void
8660 cp_parser_linkage_specification (cp_parser* parser)
8661 {
8662   tree linkage;
8663
8664   /* Look for the `extern' keyword.  */
8665   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8666
8667   /* Look for the string-literal.  */
8668   linkage = cp_parser_string_literal (parser, false, false);
8669
8670   /* Transform the literal into an identifier.  If the literal is a
8671      wide-character string, or contains embedded NULs, then we can't
8672      handle it as the user wants.  */
8673   if (strlen (TREE_STRING_POINTER (linkage))
8674       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8675     {
8676       cp_parser_error (parser, "invalid linkage-specification");
8677       /* Assume C++ linkage.  */
8678       linkage = lang_name_cplusplus;
8679     }
8680   else
8681     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8682
8683   /* We're now using the new linkage.  */
8684   push_lang_context (linkage);
8685
8686   /* If the next token is a `{', then we're using the first
8687      production.  */
8688   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8689     {
8690       /* Consume the `{' token.  */
8691       cp_lexer_consume_token (parser->lexer);
8692       /* Parse the declarations.  */
8693       cp_parser_declaration_seq_opt (parser);
8694       /* Look for the closing `}'.  */
8695       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8696     }
8697   /* Otherwise, there's just one declaration.  */
8698   else
8699     {
8700       bool saved_in_unbraced_linkage_specification_p;
8701
8702       saved_in_unbraced_linkage_specification_p
8703         = parser->in_unbraced_linkage_specification_p;
8704       parser->in_unbraced_linkage_specification_p = true;
8705       cp_parser_declaration (parser);
8706       parser->in_unbraced_linkage_specification_p
8707         = saved_in_unbraced_linkage_specification_p;
8708     }
8709
8710   /* We're done with the linkage-specification.  */
8711   pop_lang_context ();
8712 }
8713
8714 /* Parse a static_assert-declaration.
8715
8716    static_assert-declaration:
8717      static_assert ( constant-expression , string-literal ) ; 
8718
8719    If MEMBER_P, this static_assert is a class member.  */
8720
8721 static void 
8722 cp_parser_static_assert(cp_parser *parser, bool member_p)
8723 {
8724   tree condition;
8725   tree message;
8726   cp_token *token;
8727   location_t saved_loc;
8728
8729   /* Peek at the `static_assert' token so we can keep track of exactly
8730      where the static assertion started.  */
8731   token = cp_lexer_peek_token (parser->lexer);
8732   saved_loc = token->location;
8733
8734   /* Look for the `static_assert' keyword.  */
8735   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8736                                   "%<static_assert%>"))
8737     return;
8738
8739   /*  We know we are in a static assertion; commit to any tentative
8740       parse.  */
8741   if (cp_parser_parsing_tentatively (parser))
8742     cp_parser_commit_to_tentative_parse (parser);
8743
8744   /* Parse the `(' starting the static assertion condition.  */
8745   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8746
8747   /* Parse the constant-expression.  */
8748   condition = 
8749     cp_parser_constant_expression (parser,
8750                                    /*allow_non_constant_p=*/false,
8751                                    /*non_constant_p=*/NULL);
8752
8753   /* Parse the separating `,'.  */
8754   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8755
8756   /* Parse the string-literal message.  */
8757   message = cp_parser_string_literal (parser, 
8758                                       /*translate=*/false,
8759                                       /*wide_ok=*/true);
8760
8761   /* A `)' completes the static assertion.  */
8762   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8763     cp_parser_skip_to_closing_parenthesis (parser, 
8764                                            /*recovering=*/true, 
8765                                            /*or_comma=*/false,
8766                                            /*consume_paren=*/true);
8767
8768   /* A semicolon terminates the declaration.  */
8769   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8770
8771   /* Complete the static assertion, which may mean either processing 
8772      the static assert now or saving it for template instantiation.  */
8773   finish_static_assert (condition, message, saved_loc, member_p);
8774 }
8775
8776 /* Parse a `decltype' type. Returns the type. 
8777
8778    simple-type-specifier:
8779      decltype ( expression )  */
8780
8781 static tree
8782 cp_parser_decltype (cp_parser *parser)
8783 {
8784   tree expr;
8785   bool id_expression_or_member_access_p = false;
8786   const char *saved_message;
8787   bool saved_integral_constant_expression_p;
8788   bool saved_non_integral_constant_expression_p;
8789   cp_token *id_expr_start_token;
8790
8791   /* Look for the `decltype' token.  */
8792   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8793     return error_mark_node;
8794
8795   /* Types cannot be defined in a `decltype' expression.  Save away the
8796      old message.  */
8797   saved_message = parser->type_definition_forbidden_message;
8798
8799   /* And create the new one.  */
8800   parser->type_definition_forbidden_message
8801     = "types may not be defined in %<decltype%> expressions";
8802
8803   /* The restrictions on constant-expressions do not apply inside
8804      decltype expressions.  */
8805   saved_integral_constant_expression_p
8806     = parser->integral_constant_expression_p;
8807   saved_non_integral_constant_expression_p
8808     = parser->non_integral_constant_expression_p;
8809   parser->integral_constant_expression_p = false;
8810
8811   /* Do not actually evaluate the expression.  */
8812   ++skip_evaluation;
8813
8814   /* Parse the opening `('.  */
8815   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8816     return error_mark_node;
8817   
8818   /* First, try parsing an id-expression.  */
8819   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8820   cp_parser_parse_tentatively (parser);
8821   expr = cp_parser_id_expression (parser,
8822                                   /*template_keyword_p=*/false,
8823                                   /*check_dependency_p=*/true,
8824                                   /*template_p=*/NULL,
8825                                   /*declarator_p=*/false,
8826                                   /*optional_p=*/false);
8827
8828   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8829     {
8830       bool non_integral_constant_expression_p = false;
8831       tree id_expression = expr;
8832       cp_id_kind idk;
8833       const char *error_msg;
8834
8835       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8836         /* Lookup the name we got back from the id-expression.  */
8837         expr = cp_parser_lookup_name (parser, expr,
8838                                       none_type,
8839                                       /*is_template=*/false,
8840                                       /*is_namespace=*/false,
8841                                       /*check_dependency=*/true,
8842                                       /*ambiguous_decls=*/NULL,
8843                                       id_expr_start_token->location);
8844
8845       if (expr
8846           && expr != error_mark_node
8847           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8848           && TREE_CODE (expr) != TYPE_DECL
8849           && (TREE_CODE (expr) != BIT_NOT_EXPR
8850               || !TYPE_P (TREE_OPERAND (expr, 0)))
8851           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8852         {
8853           /* Complete lookup of the id-expression.  */
8854           expr = (finish_id_expression
8855                   (id_expression, expr, parser->scope, &idk,
8856                    /*integral_constant_expression_p=*/false,
8857                    /*allow_non_integral_constant_expression_p=*/true,
8858                    &non_integral_constant_expression_p,
8859                    /*template_p=*/false,
8860                    /*done=*/true,
8861                    /*address_p=*/false,
8862                    /*template_arg_p=*/false,
8863                    &error_msg,
8864                    id_expr_start_token->location));
8865
8866           if (expr == error_mark_node)
8867             /* We found an id-expression, but it was something that we
8868                should not have found. This is an error, not something
8869                we can recover from, so note that we found an
8870                id-expression and we'll recover as gracefully as
8871                possible.  */
8872             id_expression_or_member_access_p = true;
8873         }
8874
8875       if (expr 
8876           && expr != error_mark_node
8877           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8878         /* We have an id-expression.  */
8879         id_expression_or_member_access_p = true;
8880     }
8881
8882   if (!id_expression_or_member_access_p)
8883     {
8884       /* Abort the id-expression parse.  */
8885       cp_parser_abort_tentative_parse (parser);
8886
8887       /* Parsing tentatively, again.  */
8888       cp_parser_parse_tentatively (parser);
8889
8890       /* Parse a class member access.  */
8891       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8892                                            /*cast_p=*/false,
8893                                            /*member_access_only_p=*/true, NULL);
8894
8895       if (expr 
8896           && expr != error_mark_node
8897           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8898         /* We have an id-expression.  */
8899         id_expression_or_member_access_p = true;
8900     }
8901
8902   if (id_expression_or_member_access_p)
8903     /* We have parsed the complete id-expression or member access.  */
8904     cp_parser_parse_definitely (parser);
8905   else
8906     {
8907       /* Abort our attempt to parse an id-expression or member access
8908          expression.  */
8909       cp_parser_abort_tentative_parse (parser);
8910
8911       /* Parse a full expression.  */
8912       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8913     }
8914
8915   /* Go back to evaluating expressions.  */
8916   --skip_evaluation;
8917
8918   /* Restore the old message and the integral constant expression
8919      flags.  */
8920   parser->type_definition_forbidden_message = saved_message;
8921   parser->integral_constant_expression_p
8922     = saved_integral_constant_expression_p;
8923   parser->non_integral_constant_expression_p
8924     = saved_non_integral_constant_expression_p;
8925
8926   if (expr == error_mark_node)
8927     {
8928       /* Skip everything up to the closing `)'.  */
8929       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8930                                              /*consume_paren=*/true);
8931       return error_mark_node;
8932     }
8933   
8934   /* Parse to the closing `)'.  */
8935   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8936     {
8937       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8938                                              /*consume_paren=*/true);
8939       return error_mark_node;
8940     }
8941
8942   return finish_decltype_type (expr, id_expression_or_member_access_p);
8943 }
8944
8945 /* Special member functions [gram.special] */
8946
8947 /* Parse a conversion-function-id.
8948
8949    conversion-function-id:
8950      operator conversion-type-id
8951
8952    Returns an IDENTIFIER_NODE representing the operator.  */
8953
8954 static tree
8955 cp_parser_conversion_function_id (cp_parser* parser)
8956 {
8957   tree type;
8958   tree saved_scope;
8959   tree saved_qualifying_scope;
8960   tree saved_object_scope;
8961   tree pushed_scope = NULL_TREE;
8962
8963   /* Look for the `operator' token.  */
8964   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8965     return error_mark_node;
8966   /* When we parse the conversion-type-id, the current scope will be
8967      reset.  However, we need that information in able to look up the
8968      conversion function later, so we save it here.  */
8969   saved_scope = parser->scope;
8970   saved_qualifying_scope = parser->qualifying_scope;
8971   saved_object_scope = parser->object_scope;
8972   /* We must enter the scope of the class so that the names of
8973      entities declared within the class are available in the
8974      conversion-type-id.  For example, consider:
8975
8976        struct S {
8977          typedef int I;
8978          operator I();
8979        };
8980
8981        S::operator I() { ... }
8982
8983      In order to see that `I' is a type-name in the definition, we
8984      must be in the scope of `S'.  */
8985   if (saved_scope)
8986     pushed_scope = push_scope (saved_scope);
8987   /* Parse the conversion-type-id.  */
8988   type = cp_parser_conversion_type_id (parser);
8989   /* Leave the scope of the class, if any.  */
8990   if (pushed_scope)
8991     pop_scope (pushed_scope);
8992   /* Restore the saved scope.  */
8993   parser->scope = saved_scope;
8994   parser->qualifying_scope = saved_qualifying_scope;
8995   parser->object_scope = saved_object_scope;
8996   /* If the TYPE is invalid, indicate failure.  */
8997   if (type == error_mark_node)
8998     return error_mark_node;
8999   return mangle_conv_op_name_for_type (type);
9000 }
9001
9002 /* Parse a conversion-type-id:
9003
9004    conversion-type-id:
9005      type-specifier-seq conversion-declarator [opt]
9006
9007    Returns the TYPE specified.  */
9008
9009 static tree
9010 cp_parser_conversion_type_id (cp_parser* parser)
9011 {
9012   tree attributes;
9013   cp_decl_specifier_seq type_specifiers;
9014   cp_declarator *declarator;
9015   tree type_specified;
9016
9017   /* Parse the attributes.  */
9018   attributes = cp_parser_attributes_opt (parser);
9019   /* Parse the type-specifiers.  */
9020   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9021                                 &type_specifiers);
9022   /* If that didn't work, stop.  */
9023   if (type_specifiers.type == error_mark_node)
9024     return error_mark_node;
9025   /* Parse the conversion-declarator.  */
9026   declarator = cp_parser_conversion_declarator_opt (parser);
9027
9028   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9029                                     /*initialized=*/0, &attributes);
9030   if (attributes)
9031     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9032
9033   /* Don't give this error when parsing tentatively.  This happens to
9034      work because we always parse this definitively once.  */
9035   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9036       && type_uses_auto (type_specified))
9037     {
9038       error ("invalid use of %<auto%> in conversion operator");
9039       return error_mark_node;
9040     }
9041
9042   return type_specified;
9043 }
9044
9045 /* Parse an (optional) conversion-declarator.
9046
9047    conversion-declarator:
9048      ptr-operator conversion-declarator [opt]
9049
9050    */
9051
9052 static cp_declarator *
9053 cp_parser_conversion_declarator_opt (cp_parser* parser)
9054 {
9055   enum tree_code code;
9056   tree class_type;
9057   cp_cv_quals cv_quals;
9058
9059   /* We don't know if there's a ptr-operator next, or not.  */
9060   cp_parser_parse_tentatively (parser);
9061   /* Try the ptr-operator.  */
9062   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9063   /* If it worked, look for more conversion-declarators.  */
9064   if (cp_parser_parse_definitely (parser))
9065     {
9066       cp_declarator *declarator;
9067
9068       /* Parse another optional declarator.  */
9069       declarator = cp_parser_conversion_declarator_opt (parser);
9070
9071       return cp_parser_make_indirect_declarator
9072         (code, class_type, cv_quals, declarator);
9073    }
9074
9075   return NULL;
9076 }
9077
9078 /* Parse an (optional) ctor-initializer.
9079
9080    ctor-initializer:
9081      : mem-initializer-list
9082
9083    Returns TRUE iff the ctor-initializer was actually present.  */
9084
9085 static bool
9086 cp_parser_ctor_initializer_opt (cp_parser* parser)
9087 {
9088   /* If the next token is not a `:', then there is no
9089      ctor-initializer.  */
9090   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9091     {
9092       /* Do default initialization of any bases and members.  */
9093       if (DECL_CONSTRUCTOR_P (current_function_decl))
9094         finish_mem_initializers (NULL_TREE);
9095
9096       return false;
9097     }
9098
9099   /* Consume the `:' token.  */
9100   cp_lexer_consume_token (parser->lexer);
9101   /* And the mem-initializer-list.  */
9102   cp_parser_mem_initializer_list (parser);
9103
9104   return true;
9105 }
9106
9107 /* Parse a mem-initializer-list.
9108
9109    mem-initializer-list:
9110      mem-initializer ... [opt]
9111      mem-initializer ... [opt] , mem-initializer-list  */
9112
9113 static void
9114 cp_parser_mem_initializer_list (cp_parser* parser)
9115 {
9116   tree mem_initializer_list = NULL_TREE;
9117   cp_token *token = cp_lexer_peek_token (parser->lexer);
9118
9119   /* Let the semantic analysis code know that we are starting the
9120      mem-initializer-list.  */
9121   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9122     error ("%Honly constructors take base initializers",
9123            &token->location);
9124
9125   /* Loop through the list.  */
9126   while (true)
9127     {
9128       tree mem_initializer;
9129
9130       token = cp_lexer_peek_token (parser->lexer);
9131       /* Parse the mem-initializer.  */
9132       mem_initializer = cp_parser_mem_initializer (parser);
9133       /* If the next token is a `...', we're expanding member initializers. */
9134       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9135         {
9136           /* Consume the `...'. */
9137           cp_lexer_consume_token (parser->lexer);
9138
9139           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9140              can be expanded but members cannot. */
9141           if (mem_initializer != error_mark_node
9142               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9143             {
9144               error ("%Hcannot expand initializer for member %<%D%>",
9145                      &token->location, TREE_PURPOSE (mem_initializer));
9146               mem_initializer = error_mark_node;
9147             }
9148
9149           /* Construct the pack expansion type. */
9150           if (mem_initializer != error_mark_node)
9151             mem_initializer = make_pack_expansion (mem_initializer);
9152         }
9153       /* Add it to the list, unless it was erroneous.  */
9154       if (mem_initializer != error_mark_node)
9155         {
9156           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9157           mem_initializer_list = mem_initializer;
9158         }
9159       /* If the next token is not a `,', we're done.  */
9160       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9161         break;
9162       /* Consume the `,' token.  */
9163       cp_lexer_consume_token (parser->lexer);
9164     }
9165
9166   /* Perform semantic analysis.  */
9167   if (DECL_CONSTRUCTOR_P (current_function_decl))
9168     finish_mem_initializers (mem_initializer_list);
9169 }
9170
9171 /* Parse a mem-initializer.
9172
9173    mem-initializer:
9174      mem-initializer-id ( expression-list [opt] )
9175      mem-initializer-id braced-init-list
9176
9177    GNU extension:
9178
9179    mem-initializer:
9180      ( expression-list [opt] )
9181
9182    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9183    class) or FIELD_DECL (for a non-static data member) to initialize;
9184    the TREE_VALUE is the expression-list.  An empty initialization
9185    list is represented by void_list_node.  */
9186
9187 static tree
9188 cp_parser_mem_initializer (cp_parser* parser)
9189 {
9190   tree mem_initializer_id;
9191   tree expression_list;
9192   tree member;
9193   cp_token *token = cp_lexer_peek_token (parser->lexer);
9194
9195   /* Find out what is being initialized.  */
9196   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9197     {
9198       permerror (token->location,
9199                  "anachronistic old-style base class initializer");
9200       mem_initializer_id = NULL_TREE;
9201     }
9202   else
9203     {
9204       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9205       if (mem_initializer_id == error_mark_node)
9206         return mem_initializer_id;
9207     }
9208   member = expand_member_init (mem_initializer_id);
9209   if (member && !DECL_P (member))
9210     in_base_initializer = 1;
9211
9212   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9213     {
9214       bool expr_non_constant_p;
9215       maybe_warn_cpp0x ("extended initializer lists");
9216       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9217       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9218       expression_list = build_tree_list (NULL_TREE, expression_list);
9219     }
9220   else
9221     expression_list
9222       = cp_parser_parenthesized_expression_list (parser, false,
9223                                                  /*cast_p=*/false,
9224                                                  /*allow_expansion_p=*/true,
9225                                                  /*non_constant_p=*/NULL);
9226   if (expression_list == error_mark_node)
9227     return error_mark_node;
9228   if (!expression_list)
9229     expression_list = void_type_node;
9230
9231   in_base_initializer = 0;
9232
9233   return member ? build_tree_list (member, expression_list) : error_mark_node;
9234 }
9235
9236 /* Parse a mem-initializer-id.
9237
9238    mem-initializer-id:
9239      :: [opt] nested-name-specifier [opt] class-name
9240      identifier
9241
9242    Returns a TYPE indicating the class to be initializer for the first
9243    production.  Returns an IDENTIFIER_NODE indicating the data member
9244    to be initialized for the second production.  */
9245
9246 static tree
9247 cp_parser_mem_initializer_id (cp_parser* parser)
9248 {
9249   bool global_scope_p;
9250   bool nested_name_specifier_p;
9251   bool template_p = false;
9252   tree id;
9253
9254   cp_token *token = cp_lexer_peek_token (parser->lexer);
9255
9256   /* `typename' is not allowed in this context ([temp.res]).  */
9257   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9258     {
9259       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9260              "member initializer is implicitly a type)",
9261              &token->location);
9262       cp_lexer_consume_token (parser->lexer);
9263     }
9264   /* Look for the optional `::' operator.  */
9265   global_scope_p
9266     = (cp_parser_global_scope_opt (parser,
9267                                    /*current_scope_valid_p=*/false)
9268        != NULL_TREE);
9269   /* Look for the optional nested-name-specifier.  The simplest way to
9270      implement:
9271
9272        [temp.res]
9273
9274        The keyword `typename' is not permitted in a base-specifier or
9275        mem-initializer; in these contexts a qualified name that
9276        depends on a template-parameter is implicitly assumed to be a
9277        type name.
9278
9279      is to assume that we have seen the `typename' keyword at this
9280      point.  */
9281   nested_name_specifier_p
9282     = (cp_parser_nested_name_specifier_opt (parser,
9283                                             /*typename_keyword_p=*/true,
9284                                             /*check_dependency_p=*/true,
9285                                             /*type_p=*/true,
9286                                             /*is_declaration=*/true)
9287        != NULL_TREE);
9288   if (nested_name_specifier_p)
9289     template_p = cp_parser_optional_template_keyword (parser);
9290   /* If there is a `::' operator or a nested-name-specifier, then we
9291      are definitely looking for a class-name.  */
9292   if (global_scope_p || nested_name_specifier_p)
9293     return cp_parser_class_name (parser,
9294                                  /*typename_keyword_p=*/true,
9295                                  /*template_keyword_p=*/template_p,
9296                                  none_type,
9297                                  /*check_dependency_p=*/true,
9298                                  /*class_head_p=*/false,
9299                                  /*is_declaration=*/true);
9300   /* Otherwise, we could also be looking for an ordinary identifier.  */
9301   cp_parser_parse_tentatively (parser);
9302   /* Try a class-name.  */
9303   id = cp_parser_class_name (parser,
9304                              /*typename_keyword_p=*/true,
9305                              /*template_keyword_p=*/false,
9306                              none_type,
9307                              /*check_dependency_p=*/true,
9308                              /*class_head_p=*/false,
9309                              /*is_declaration=*/true);
9310   /* If we found one, we're done.  */
9311   if (cp_parser_parse_definitely (parser))
9312     return id;
9313   /* Otherwise, look for an ordinary identifier.  */
9314   return cp_parser_identifier (parser);
9315 }
9316
9317 /* Overloading [gram.over] */
9318
9319 /* Parse an operator-function-id.
9320
9321    operator-function-id:
9322      operator operator
9323
9324    Returns an IDENTIFIER_NODE for the operator which is a
9325    human-readable spelling of the identifier, e.g., `operator +'.  */
9326
9327 static tree
9328 cp_parser_operator_function_id (cp_parser* parser)
9329 {
9330   /* Look for the `operator' keyword.  */
9331   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9332     return error_mark_node;
9333   /* And then the name of the operator itself.  */
9334   return cp_parser_operator (parser);
9335 }
9336
9337 /* Parse an operator.
9338
9339    operator:
9340      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9341      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9342      || ++ -- , ->* -> () []
9343
9344    GNU Extensions:
9345
9346    operator:
9347      <? >? <?= >?=
9348
9349    Returns an IDENTIFIER_NODE for the operator which is a
9350    human-readable spelling of the identifier, e.g., `operator +'.  */
9351
9352 static tree
9353 cp_parser_operator (cp_parser* parser)
9354 {
9355   tree id = NULL_TREE;
9356   cp_token *token;
9357
9358   /* Peek at the next token.  */
9359   token = cp_lexer_peek_token (parser->lexer);
9360   /* Figure out which operator we have.  */
9361   switch (token->type)
9362     {
9363     case CPP_KEYWORD:
9364       {
9365         enum tree_code op;
9366
9367         /* The keyword should be either `new' or `delete'.  */
9368         if (token->keyword == RID_NEW)
9369           op = NEW_EXPR;
9370         else if (token->keyword == RID_DELETE)
9371           op = DELETE_EXPR;
9372         else
9373           break;
9374
9375         /* Consume the `new' or `delete' token.  */
9376         cp_lexer_consume_token (parser->lexer);
9377
9378         /* Peek at the next token.  */
9379         token = cp_lexer_peek_token (parser->lexer);
9380         /* If it's a `[' token then this is the array variant of the
9381            operator.  */
9382         if (token->type == CPP_OPEN_SQUARE)
9383           {
9384             /* Consume the `[' token.  */
9385             cp_lexer_consume_token (parser->lexer);
9386             /* Look for the `]' token.  */
9387             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9388             id = ansi_opname (op == NEW_EXPR
9389                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9390           }
9391         /* Otherwise, we have the non-array variant.  */
9392         else
9393           id = ansi_opname (op);
9394
9395         return id;
9396       }
9397
9398     case CPP_PLUS:
9399       id = ansi_opname (PLUS_EXPR);
9400       break;
9401
9402     case CPP_MINUS:
9403       id = ansi_opname (MINUS_EXPR);
9404       break;
9405
9406     case CPP_MULT:
9407       id = ansi_opname (MULT_EXPR);
9408       break;
9409
9410     case CPP_DIV:
9411       id = ansi_opname (TRUNC_DIV_EXPR);
9412       break;
9413
9414     case CPP_MOD:
9415       id = ansi_opname (TRUNC_MOD_EXPR);
9416       break;
9417
9418     case CPP_XOR:
9419       id = ansi_opname (BIT_XOR_EXPR);
9420       break;
9421
9422     case CPP_AND:
9423       id = ansi_opname (BIT_AND_EXPR);
9424       break;
9425
9426     case CPP_OR:
9427       id = ansi_opname (BIT_IOR_EXPR);
9428       break;
9429
9430     case CPP_COMPL:
9431       id = ansi_opname (BIT_NOT_EXPR);
9432       break;
9433
9434     case CPP_NOT:
9435       id = ansi_opname (TRUTH_NOT_EXPR);
9436       break;
9437
9438     case CPP_EQ:
9439       id = ansi_assopname (NOP_EXPR);
9440       break;
9441
9442     case CPP_LESS:
9443       id = ansi_opname (LT_EXPR);
9444       break;
9445
9446     case CPP_GREATER:
9447       id = ansi_opname (GT_EXPR);
9448       break;
9449
9450     case CPP_PLUS_EQ:
9451       id = ansi_assopname (PLUS_EXPR);
9452       break;
9453
9454     case CPP_MINUS_EQ:
9455       id = ansi_assopname (MINUS_EXPR);
9456       break;
9457
9458     case CPP_MULT_EQ:
9459       id = ansi_assopname (MULT_EXPR);
9460       break;
9461
9462     case CPP_DIV_EQ:
9463       id = ansi_assopname (TRUNC_DIV_EXPR);
9464       break;
9465
9466     case CPP_MOD_EQ:
9467       id = ansi_assopname (TRUNC_MOD_EXPR);
9468       break;
9469
9470     case CPP_XOR_EQ:
9471       id = ansi_assopname (BIT_XOR_EXPR);
9472       break;
9473
9474     case CPP_AND_EQ:
9475       id = ansi_assopname (BIT_AND_EXPR);
9476       break;
9477
9478     case CPP_OR_EQ:
9479       id = ansi_assopname (BIT_IOR_EXPR);
9480       break;
9481
9482     case CPP_LSHIFT:
9483       id = ansi_opname (LSHIFT_EXPR);
9484       break;
9485
9486     case CPP_RSHIFT:
9487       id = ansi_opname (RSHIFT_EXPR);
9488       break;
9489
9490     case CPP_LSHIFT_EQ:
9491       id = ansi_assopname (LSHIFT_EXPR);
9492       break;
9493
9494     case CPP_RSHIFT_EQ:
9495       id = ansi_assopname (RSHIFT_EXPR);
9496       break;
9497
9498     case CPP_EQ_EQ:
9499       id = ansi_opname (EQ_EXPR);
9500       break;
9501
9502     case CPP_NOT_EQ:
9503       id = ansi_opname (NE_EXPR);
9504       break;
9505
9506     case CPP_LESS_EQ:
9507       id = ansi_opname (LE_EXPR);
9508       break;
9509
9510     case CPP_GREATER_EQ:
9511       id = ansi_opname (GE_EXPR);
9512       break;
9513
9514     case CPP_AND_AND:
9515       id = ansi_opname (TRUTH_ANDIF_EXPR);
9516       break;
9517
9518     case CPP_OR_OR:
9519       id = ansi_opname (TRUTH_ORIF_EXPR);
9520       break;
9521
9522     case CPP_PLUS_PLUS:
9523       id = ansi_opname (POSTINCREMENT_EXPR);
9524       break;
9525
9526     case CPP_MINUS_MINUS:
9527       id = ansi_opname (PREDECREMENT_EXPR);
9528       break;
9529
9530     case CPP_COMMA:
9531       id = ansi_opname (COMPOUND_EXPR);
9532       break;
9533
9534     case CPP_DEREF_STAR:
9535       id = ansi_opname (MEMBER_REF);
9536       break;
9537
9538     case CPP_DEREF:
9539       id = ansi_opname (COMPONENT_REF);
9540       break;
9541
9542     case CPP_OPEN_PAREN:
9543       /* Consume the `('.  */
9544       cp_lexer_consume_token (parser->lexer);
9545       /* Look for the matching `)'.  */
9546       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9547       return ansi_opname (CALL_EXPR);
9548
9549     case CPP_OPEN_SQUARE:
9550       /* Consume the `['.  */
9551       cp_lexer_consume_token (parser->lexer);
9552       /* Look for the matching `]'.  */
9553       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9554       return ansi_opname (ARRAY_REF);
9555
9556     default:
9557       /* Anything else is an error.  */
9558       break;
9559     }
9560
9561   /* If we have selected an identifier, we need to consume the
9562      operator token.  */
9563   if (id)
9564     cp_lexer_consume_token (parser->lexer);
9565   /* Otherwise, no valid operator name was present.  */
9566   else
9567     {
9568       cp_parser_error (parser, "expected operator");
9569       id = error_mark_node;
9570     }
9571
9572   return id;
9573 }
9574
9575 /* Parse a template-declaration.
9576
9577    template-declaration:
9578      export [opt] template < template-parameter-list > declaration
9579
9580    If MEMBER_P is TRUE, this template-declaration occurs within a
9581    class-specifier.
9582
9583    The grammar rule given by the standard isn't correct.  What
9584    is really meant is:
9585
9586    template-declaration:
9587      export [opt] template-parameter-list-seq
9588        decl-specifier-seq [opt] init-declarator [opt] ;
9589      export [opt] template-parameter-list-seq
9590        function-definition
9591
9592    template-parameter-list-seq:
9593      template-parameter-list-seq [opt]
9594      template < template-parameter-list >  */
9595
9596 static void
9597 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9598 {
9599   /* Check for `export'.  */
9600   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9601     {
9602       /* Consume the `export' token.  */
9603       cp_lexer_consume_token (parser->lexer);
9604       /* Warn that we do not support `export'.  */
9605       warning (0, "keyword %<export%> not implemented, and will be ignored");
9606     }
9607
9608   cp_parser_template_declaration_after_export (parser, member_p);
9609 }
9610
9611 /* Parse a template-parameter-list.
9612
9613    template-parameter-list:
9614      template-parameter
9615      template-parameter-list , template-parameter
9616
9617    Returns a TREE_LIST.  Each node represents a template parameter.
9618    The nodes are connected via their TREE_CHAINs.  */
9619
9620 static tree
9621 cp_parser_template_parameter_list (cp_parser* parser)
9622 {
9623   tree parameter_list = NULL_TREE;
9624
9625   begin_template_parm_list ();
9626   while (true)
9627     {
9628       tree parameter;
9629       bool is_non_type;
9630       bool is_parameter_pack;
9631
9632       /* Parse the template-parameter.  */
9633       parameter = cp_parser_template_parameter (parser, 
9634                                                 &is_non_type,
9635                                                 &is_parameter_pack);
9636       /* Add it to the list.  */
9637       if (parameter != error_mark_node)
9638         parameter_list = process_template_parm (parameter_list,
9639                                                 parameter,
9640                                                 is_non_type,
9641                                                 is_parameter_pack);
9642       else
9643        {
9644          tree err_parm = build_tree_list (parameter, parameter);
9645          TREE_VALUE (err_parm) = error_mark_node;
9646          parameter_list = chainon (parameter_list, err_parm);
9647        }
9648
9649       /* If the next token is not a `,', we're done.  */
9650       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9651         break;
9652       /* Otherwise, consume the `,' token.  */
9653       cp_lexer_consume_token (parser->lexer);
9654     }
9655
9656   return end_template_parm_list (parameter_list);
9657 }
9658
9659 /* Parse a template-parameter.
9660
9661    template-parameter:
9662      type-parameter
9663      parameter-declaration
9664
9665    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9666    the parameter.  The TREE_PURPOSE is the default value, if any.
9667    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9668    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9669    set to true iff this parameter is a parameter pack. */
9670
9671 static tree
9672 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9673                               bool *is_parameter_pack)
9674 {
9675   cp_token *token;
9676   cp_parameter_declarator *parameter_declarator;
9677   cp_declarator *id_declarator;
9678   tree parm;
9679
9680   /* Assume it is a type parameter or a template parameter.  */
9681   *is_non_type = false;
9682   /* Assume it not a parameter pack. */
9683   *is_parameter_pack = false;
9684   /* Peek at the next token.  */
9685   token = cp_lexer_peek_token (parser->lexer);
9686   /* If it is `class' or `template', we have a type-parameter.  */
9687   if (token->keyword == RID_TEMPLATE)
9688     return cp_parser_type_parameter (parser, is_parameter_pack);
9689   /* If it is `class' or `typename' we do not know yet whether it is a
9690      type parameter or a non-type parameter.  Consider:
9691
9692        template <typename T, typename T::X X> ...
9693
9694      or:
9695
9696        template <class C, class D*> ...
9697
9698      Here, the first parameter is a type parameter, and the second is
9699      a non-type parameter.  We can tell by looking at the token after
9700      the identifier -- if it is a `,', `=', or `>' then we have a type
9701      parameter.  */
9702   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9703     {
9704       /* Peek at the token after `class' or `typename'.  */
9705       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9706       /* If it's an ellipsis, we have a template type parameter
9707          pack. */
9708       if (token->type == CPP_ELLIPSIS)
9709         return cp_parser_type_parameter (parser, is_parameter_pack);
9710       /* If it's an identifier, skip it.  */
9711       if (token->type == CPP_NAME)
9712         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9713       /* Now, see if the token looks like the end of a template
9714          parameter.  */
9715       if (token->type == CPP_COMMA
9716           || token->type == CPP_EQ
9717           || token->type == CPP_GREATER)
9718         return cp_parser_type_parameter (parser, is_parameter_pack);
9719     }
9720
9721   /* Otherwise, it is a non-type parameter.
9722
9723      [temp.param]
9724
9725      When parsing a default template-argument for a non-type
9726      template-parameter, the first non-nested `>' is taken as the end
9727      of the template parameter-list rather than a greater-than
9728      operator.  */
9729   *is_non_type = true;
9730   parameter_declarator
9731      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9732                                         /*parenthesized_p=*/NULL);
9733
9734   /* If the parameter declaration is marked as a parameter pack, set
9735      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9736      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9737      grokdeclarator. */
9738   if (parameter_declarator
9739       && parameter_declarator->declarator
9740       && parameter_declarator->declarator->parameter_pack_p)
9741     {
9742       *is_parameter_pack = true;
9743       parameter_declarator->declarator->parameter_pack_p = false;
9744     }
9745
9746   /* If the next token is an ellipsis, and we don't already have it
9747      marked as a parameter pack, then we have a parameter pack (that
9748      has no declarator).  */
9749   if (!*is_parameter_pack
9750       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9751       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9752     {
9753       /* Consume the `...'.  */
9754       cp_lexer_consume_token (parser->lexer);
9755       maybe_warn_variadic_templates ();
9756       
9757       *is_parameter_pack = true;
9758     }
9759   /* We might end up with a pack expansion as the type of the non-type
9760      template parameter, in which case this is a non-type template
9761      parameter pack.  */
9762   else if (parameter_declarator
9763            && parameter_declarator->decl_specifiers.type
9764            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9765     {
9766       *is_parameter_pack = true;
9767       parameter_declarator->decl_specifiers.type = 
9768         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9769     }
9770
9771   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9772     {
9773       /* Parameter packs cannot have default arguments.  However, a
9774          user may try to do so, so we'll parse them and give an
9775          appropriate diagnostic here.  */
9776
9777       /* Consume the `='.  */
9778       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9779       cp_lexer_consume_token (parser->lexer);
9780       
9781       /* Find the name of the parameter pack.  */     
9782       id_declarator = parameter_declarator->declarator;
9783       while (id_declarator && id_declarator->kind != cdk_id)
9784         id_declarator = id_declarator->declarator;
9785       
9786       if (id_declarator && id_declarator->kind == cdk_id)
9787         error ("%Htemplate parameter pack %qD cannot have a default argument",
9788                &start_token->location, id_declarator->u.id.unqualified_name);
9789       else
9790         error ("%Htemplate parameter pack cannot have a default argument",
9791                &start_token->location);
9792       
9793       /* Parse the default argument, but throw away the result.  */
9794       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9795     }
9796
9797   parm = grokdeclarator (parameter_declarator->declarator,
9798                          &parameter_declarator->decl_specifiers,
9799                          PARM, /*initialized=*/0,
9800                          /*attrlist=*/NULL);
9801   if (parm == error_mark_node)
9802     return error_mark_node;
9803
9804   return build_tree_list (parameter_declarator->default_argument, parm);
9805 }
9806
9807 /* Parse a type-parameter.
9808
9809    type-parameter:
9810      class identifier [opt]
9811      class identifier [opt] = type-id
9812      typename identifier [opt]
9813      typename identifier [opt] = type-id
9814      template < template-parameter-list > class identifier [opt]
9815      template < template-parameter-list > class identifier [opt]
9816        = id-expression
9817
9818    GNU Extension (variadic templates):
9819
9820    type-parameter:
9821      class ... identifier [opt]
9822      typename ... identifier [opt]
9823
9824    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9825    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9826    the declaration of the parameter.
9827
9828    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9829
9830 static tree
9831 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9832 {
9833   cp_token *token;
9834   tree parameter;
9835
9836   /* Look for a keyword to tell us what kind of parameter this is.  */
9837   token = cp_parser_require (parser, CPP_KEYWORD,
9838                              "%<class%>, %<typename%>, or %<template%>");
9839   if (!token)
9840     return error_mark_node;
9841
9842   switch (token->keyword)
9843     {
9844     case RID_CLASS:
9845     case RID_TYPENAME:
9846       {
9847         tree identifier;
9848         tree default_argument;
9849
9850         /* If the next token is an ellipsis, we have a template
9851            argument pack. */
9852         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9853           {
9854             /* Consume the `...' token. */
9855             cp_lexer_consume_token (parser->lexer);
9856             maybe_warn_variadic_templates ();
9857
9858             *is_parameter_pack = true;
9859           }
9860
9861         /* If the next token is an identifier, then it names the
9862            parameter.  */
9863         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9864           identifier = cp_parser_identifier (parser);
9865         else
9866           identifier = NULL_TREE;
9867
9868         /* Create the parameter.  */
9869         parameter = finish_template_type_parm (class_type_node, identifier);
9870
9871         /* If the next token is an `=', we have a default argument.  */
9872         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9873           {
9874             /* Consume the `=' token.  */
9875             cp_lexer_consume_token (parser->lexer);
9876             /* Parse the default-argument.  */
9877             push_deferring_access_checks (dk_no_deferred);
9878             default_argument = cp_parser_type_id (parser);
9879
9880             /* Template parameter packs cannot have default
9881                arguments. */
9882             if (*is_parameter_pack)
9883               {
9884                 if (identifier)
9885                   error ("%Htemplate parameter pack %qD cannot have a "
9886                          "default argument", &token->location, identifier);
9887                 else
9888                   error ("%Htemplate parameter packs cannot have "
9889                          "default arguments", &token->location);
9890                 default_argument = NULL_TREE;
9891               }
9892             pop_deferring_access_checks ();
9893           }
9894         else
9895           default_argument = NULL_TREE;
9896
9897         /* Create the combined representation of the parameter and the
9898            default argument.  */
9899         parameter = build_tree_list (default_argument, parameter);
9900       }
9901       break;
9902
9903     case RID_TEMPLATE:
9904       {
9905         tree parameter_list;
9906         tree identifier;
9907         tree default_argument;
9908
9909         /* Look for the `<'.  */
9910         cp_parser_require (parser, CPP_LESS, "%<<%>");
9911         /* Parse the template-parameter-list.  */
9912         parameter_list = cp_parser_template_parameter_list (parser);
9913         /* Look for the `>'.  */
9914         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9915         /* Look for the `class' keyword.  */
9916         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9917         /* If the next token is an ellipsis, we have a template
9918            argument pack. */
9919         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9920           {
9921             /* Consume the `...' token. */
9922             cp_lexer_consume_token (parser->lexer);
9923             maybe_warn_variadic_templates ();
9924
9925             *is_parameter_pack = true;
9926           }
9927         /* If the next token is an `=', then there is a
9928            default-argument.  If the next token is a `>', we are at
9929            the end of the parameter-list.  If the next token is a `,',
9930            then we are at the end of this parameter.  */
9931         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9932             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9933             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9934           {
9935             identifier = cp_parser_identifier (parser);
9936             /* Treat invalid names as if the parameter were nameless.  */
9937             if (identifier == error_mark_node)
9938               identifier = NULL_TREE;
9939           }
9940         else
9941           identifier = NULL_TREE;
9942
9943         /* Create the template parameter.  */
9944         parameter = finish_template_template_parm (class_type_node,
9945                                                    identifier);
9946
9947         /* If the next token is an `=', then there is a
9948            default-argument.  */
9949         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9950           {
9951             bool is_template;
9952
9953             /* Consume the `='.  */
9954             cp_lexer_consume_token (parser->lexer);
9955             /* Parse the id-expression.  */
9956             push_deferring_access_checks (dk_no_deferred);
9957             /* save token before parsing the id-expression, for error
9958                reporting */
9959             token = cp_lexer_peek_token (parser->lexer);
9960             default_argument
9961               = cp_parser_id_expression (parser,
9962                                          /*template_keyword_p=*/false,
9963                                          /*check_dependency_p=*/true,
9964                                          /*template_p=*/&is_template,
9965                                          /*declarator_p=*/false,
9966                                          /*optional_p=*/false);
9967             if (TREE_CODE (default_argument) == TYPE_DECL)
9968               /* If the id-expression was a template-id that refers to
9969                  a template-class, we already have the declaration here,
9970                  so no further lookup is needed.  */
9971                  ;
9972             else
9973               /* Look up the name.  */
9974               default_argument
9975                 = cp_parser_lookup_name (parser, default_argument,
9976                                          none_type,
9977                                          /*is_template=*/is_template,
9978                                          /*is_namespace=*/false,
9979                                          /*check_dependency=*/true,
9980                                          /*ambiguous_decls=*/NULL,
9981                                          token->location);
9982             /* See if the default argument is valid.  */
9983             default_argument
9984               = check_template_template_default_arg (default_argument);
9985
9986             /* Template parameter packs cannot have default
9987                arguments. */
9988             if (*is_parameter_pack)
9989               {
9990                 if (identifier)
9991                   error ("%Htemplate parameter pack %qD cannot "
9992                          "have a default argument",
9993                          &token->location, identifier);
9994                 else
9995                   error ("%Htemplate parameter packs cannot "
9996                          "have default arguments",
9997                          &token->location);
9998                 default_argument = NULL_TREE;
9999               }
10000             pop_deferring_access_checks ();
10001           }
10002         else
10003           default_argument = NULL_TREE;
10004
10005         /* Create the combined representation of the parameter and the
10006            default argument.  */
10007         parameter = build_tree_list (default_argument, parameter);
10008       }
10009       break;
10010
10011     default:
10012       gcc_unreachable ();
10013       break;
10014     }
10015
10016   return parameter;
10017 }
10018
10019 /* Parse a template-id.
10020
10021    template-id:
10022      template-name < template-argument-list [opt] >
10023
10024    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10025    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10026    returned.  Otherwise, if the template-name names a function, or set
10027    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10028    names a class, returns a TYPE_DECL for the specialization.
10029
10030    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10031    uninstantiated templates.  */
10032
10033 static tree
10034 cp_parser_template_id (cp_parser *parser,
10035                        bool template_keyword_p,
10036                        bool check_dependency_p,
10037                        bool is_declaration)
10038 {
10039   int i;
10040   tree templ;
10041   tree arguments;
10042   tree template_id;
10043   cp_token_position start_of_id = 0;
10044   deferred_access_check *chk;
10045   VEC (deferred_access_check,gc) *access_check;
10046   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10047   bool is_identifier;
10048
10049   /* If the next token corresponds to a template-id, there is no need
10050      to reparse it.  */
10051   next_token = cp_lexer_peek_token (parser->lexer);
10052   if (next_token->type == CPP_TEMPLATE_ID)
10053     {
10054       struct tree_check *check_value;
10055
10056       /* Get the stored value.  */
10057       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10058       /* Perform any access checks that were deferred.  */
10059       access_check = check_value->checks;
10060       if (access_check)
10061         {
10062           for (i = 0 ;
10063                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10064                ++i)
10065             {
10066               perform_or_defer_access_check (chk->binfo,
10067                                              chk->decl,
10068                                              chk->diag_decl);
10069             }
10070         }
10071       /* Return the stored value.  */
10072       return check_value->value;
10073     }
10074
10075   /* Avoid performing name lookup if there is no possibility of
10076      finding a template-id.  */
10077   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10078       || (next_token->type == CPP_NAME
10079           && !cp_parser_nth_token_starts_template_argument_list_p
10080                (parser, 2)))
10081     {
10082       cp_parser_error (parser, "expected template-id");
10083       return error_mark_node;
10084     }
10085
10086   /* Remember where the template-id starts.  */
10087   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10088     start_of_id = cp_lexer_token_position (parser->lexer, false);
10089
10090   push_deferring_access_checks (dk_deferred);
10091
10092   /* Parse the template-name.  */
10093   is_identifier = false;
10094   token = cp_lexer_peek_token (parser->lexer);
10095   templ = cp_parser_template_name (parser, template_keyword_p,
10096                                    check_dependency_p,
10097                                    is_declaration,
10098                                    &is_identifier);
10099   if (templ == error_mark_node || is_identifier)
10100     {
10101       pop_deferring_access_checks ();
10102       return templ;
10103     }
10104
10105   /* If we find the sequence `[:' after a template-name, it's probably
10106      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10107      parse correctly the argument list.  */
10108   next_token = cp_lexer_peek_token (parser->lexer);
10109   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10110   if (next_token->type == CPP_OPEN_SQUARE
10111       && next_token->flags & DIGRAPH
10112       && next_token_2->type == CPP_COLON
10113       && !(next_token_2->flags & PREV_WHITE))
10114     {
10115       cp_parser_parse_tentatively (parser);
10116       /* Change `:' into `::'.  */
10117       next_token_2->type = CPP_SCOPE;
10118       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10119          CPP_LESS.  */
10120       cp_lexer_consume_token (parser->lexer);
10121
10122       /* Parse the arguments.  */
10123       arguments = cp_parser_enclosed_template_argument_list (parser);
10124       if (!cp_parser_parse_definitely (parser))
10125         {
10126           /* If we couldn't parse an argument list, then we revert our changes
10127              and return simply an error. Maybe this is not a template-id
10128              after all.  */
10129           next_token_2->type = CPP_COLON;
10130           cp_parser_error (parser, "expected %<<%>");
10131           pop_deferring_access_checks ();
10132           return error_mark_node;
10133         }
10134       /* Otherwise, emit an error about the invalid digraph, but continue
10135          parsing because we got our argument list.  */
10136       if (permerror (next_token->location,
10137                      "%<<::%> cannot begin a template-argument list"))
10138         {
10139           static bool hint = false;
10140           inform (next_token->location,
10141                   "%<<:%> is an alternate spelling for %<[%>."
10142                   " Insert whitespace between %<<%> and %<::%>");
10143           if (!hint && !flag_permissive)
10144             {
10145               inform (next_token->location, "(if you use %<-fpermissive%>"
10146                       " G++ will accept your code)");
10147               hint = true;
10148             }
10149         }
10150     }
10151   else
10152     {
10153       /* Look for the `<' that starts the template-argument-list.  */
10154       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10155         {
10156           pop_deferring_access_checks ();
10157           return error_mark_node;
10158         }
10159       /* Parse the arguments.  */
10160       arguments = cp_parser_enclosed_template_argument_list (parser);
10161     }
10162
10163   /* Build a representation of the specialization.  */
10164   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10165     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10166   else if (DECL_CLASS_TEMPLATE_P (templ)
10167            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10168     {
10169       bool entering_scope;
10170       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10171          template (rather than some instantiation thereof) only if
10172          is not nested within some other construct.  For example, in
10173          "template <typename T> void f(T) { A<T>::", A<T> is just an
10174          instantiation of A.  */
10175       entering_scope = (template_parm_scope_p ()
10176                         && cp_lexer_next_token_is (parser->lexer,
10177                                                    CPP_SCOPE));
10178       template_id
10179         = finish_template_type (templ, arguments, entering_scope);
10180     }
10181   else
10182     {
10183       /* If it's not a class-template or a template-template, it should be
10184          a function-template.  */
10185       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10186                    || TREE_CODE (templ) == OVERLOAD
10187                    || BASELINK_P (templ)));
10188
10189       template_id = lookup_template_function (templ, arguments);
10190     }
10191
10192   /* If parsing tentatively, replace the sequence of tokens that makes
10193      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10194      should we re-parse the token stream, we will not have to repeat
10195      the effort required to do the parse, nor will we issue duplicate
10196      error messages about problems during instantiation of the
10197      template.  */
10198   if (start_of_id)
10199     {
10200       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10201
10202       /* Reset the contents of the START_OF_ID token.  */
10203       token->type = CPP_TEMPLATE_ID;
10204       /* Retrieve any deferred checks.  Do not pop this access checks yet
10205          so the memory will not be reclaimed during token replacing below.  */
10206       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10207       token->u.tree_check_value->value = template_id;
10208       token->u.tree_check_value->checks = get_deferred_access_checks ();
10209       token->keyword = RID_MAX;
10210
10211       /* Purge all subsequent tokens.  */
10212       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10213
10214       /* ??? Can we actually assume that, if template_id ==
10215          error_mark_node, we will have issued a diagnostic to the
10216          user, as opposed to simply marking the tentative parse as
10217          failed?  */
10218       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10219         error ("%Hparse error in template argument list",
10220                &token->location);
10221     }
10222
10223   pop_deferring_access_checks ();
10224   return template_id;
10225 }
10226
10227 /* Parse a template-name.
10228
10229    template-name:
10230      identifier
10231
10232    The standard should actually say:
10233
10234    template-name:
10235      identifier
10236      operator-function-id
10237
10238    A defect report has been filed about this issue.
10239
10240    A conversion-function-id cannot be a template name because they cannot
10241    be part of a template-id. In fact, looking at this code:
10242
10243    a.operator K<int>()
10244
10245    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10246    It is impossible to call a templated conversion-function-id with an
10247    explicit argument list, since the only allowed template parameter is
10248    the type to which it is converting.
10249
10250    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10251    `template' keyword, in a construction like:
10252
10253      T::template f<3>()
10254
10255    In that case `f' is taken to be a template-name, even though there
10256    is no way of knowing for sure.
10257
10258    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10259    name refers to a set of overloaded functions, at least one of which
10260    is a template, or an IDENTIFIER_NODE with the name of the template,
10261    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10262    names are looked up inside uninstantiated templates.  */
10263
10264 static tree
10265 cp_parser_template_name (cp_parser* parser,
10266                          bool template_keyword_p,
10267                          bool check_dependency_p,
10268                          bool is_declaration,
10269                          bool *is_identifier)
10270 {
10271   tree identifier;
10272   tree decl;
10273   tree fns;
10274   cp_token *token = cp_lexer_peek_token (parser->lexer);
10275
10276   /* If the next token is `operator', then we have either an
10277      operator-function-id or a conversion-function-id.  */
10278   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10279     {
10280       /* We don't know whether we're looking at an
10281          operator-function-id or a conversion-function-id.  */
10282       cp_parser_parse_tentatively (parser);
10283       /* Try an operator-function-id.  */
10284       identifier = cp_parser_operator_function_id (parser);
10285       /* If that didn't work, try a conversion-function-id.  */
10286       if (!cp_parser_parse_definitely (parser))
10287         {
10288           cp_parser_error (parser, "expected template-name");
10289           return error_mark_node;
10290         }
10291     }
10292   /* Look for the identifier.  */
10293   else
10294     identifier = cp_parser_identifier (parser);
10295
10296   /* If we didn't find an identifier, we don't have a template-id.  */
10297   if (identifier == error_mark_node)
10298     return error_mark_node;
10299
10300   /* If the name immediately followed the `template' keyword, then it
10301      is a template-name.  However, if the next token is not `<', then
10302      we do not treat it as a template-name, since it is not being used
10303      as part of a template-id.  This enables us to handle constructs
10304      like:
10305
10306        template <typename T> struct S { S(); };
10307        template <typename T> S<T>::S();
10308
10309      correctly.  We would treat `S' as a template -- if it were `S<T>'
10310      -- but we do not if there is no `<'.  */
10311
10312   if (processing_template_decl
10313       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10314     {
10315       /* In a declaration, in a dependent context, we pretend that the
10316          "template" keyword was present in order to improve error
10317          recovery.  For example, given:
10318
10319            template <typename T> void f(T::X<int>);
10320
10321          we want to treat "X<int>" as a template-id.  */
10322       if (is_declaration
10323           && !template_keyword_p
10324           && parser->scope && TYPE_P (parser->scope)
10325           && check_dependency_p
10326           && dependent_type_p (parser->scope)
10327           /* Do not do this for dtors (or ctors), since they never
10328              need the template keyword before their name.  */
10329           && !constructor_name_p (identifier, parser->scope))
10330         {
10331           cp_token_position start = 0;
10332
10333           /* Explain what went wrong.  */
10334           error ("%Hnon-template %qD used as template",
10335                  &token->location, identifier);
10336           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10337                   parser->scope, identifier);
10338           /* If parsing tentatively, find the location of the "<" token.  */
10339           if (cp_parser_simulate_error (parser))
10340             start = cp_lexer_token_position (parser->lexer, true);
10341           /* Parse the template arguments so that we can issue error
10342              messages about them.  */
10343           cp_lexer_consume_token (parser->lexer);
10344           cp_parser_enclosed_template_argument_list (parser);
10345           /* Skip tokens until we find a good place from which to
10346              continue parsing.  */
10347           cp_parser_skip_to_closing_parenthesis (parser,
10348                                                  /*recovering=*/true,
10349                                                  /*or_comma=*/true,
10350                                                  /*consume_paren=*/false);
10351           /* If parsing tentatively, permanently remove the
10352              template argument list.  That will prevent duplicate
10353              error messages from being issued about the missing
10354              "template" keyword.  */
10355           if (start)
10356             cp_lexer_purge_tokens_after (parser->lexer, start);
10357           if (is_identifier)
10358             *is_identifier = true;
10359           return identifier;
10360         }
10361
10362       /* If the "template" keyword is present, then there is generally
10363          no point in doing name-lookup, so we just return IDENTIFIER.
10364          But, if the qualifying scope is non-dependent then we can
10365          (and must) do name-lookup normally.  */
10366       if (template_keyword_p
10367           && (!parser->scope
10368               || (TYPE_P (parser->scope)
10369                   && dependent_type_p (parser->scope))))
10370         return identifier;
10371     }
10372
10373   /* Look up the name.  */
10374   decl = cp_parser_lookup_name (parser, identifier,
10375                                 none_type,
10376                                 /*is_template=*/false,
10377                                 /*is_namespace=*/false,
10378                                 check_dependency_p,
10379                                 /*ambiguous_decls=*/NULL,
10380                                 token->location);
10381   decl = maybe_get_template_decl_from_type_decl (decl);
10382
10383   /* If DECL is a template, then the name was a template-name.  */
10384   if (TREE_CODE (decl) == TEMPLATE_DECL)
10385     ;
10386   else
10387     {
10388       tree fn = NULL_TREE;
10389
10390       /* The standard does not explicitly indicate whether a name that
10391          names a set of overloaded declarations, some of which are
10392          templates, is a template-name.  However, such a name should
10393          be a template-name; otherwise, there is no way to form a
10394          template-id for the overloaded templates.  */
10395       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10396       if (TREE_CODE (fns) == OVERLOAD)
10397         for (fn = fns; fn; fn = OVL_NEXT (fn))
10398           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10399             break;
10400
10401       if (!fn)
10402         {
10403           /* The name does not name a template.  */
10404           cp_parser_error (parser, "expected template-name");
10405           return error_mark_node;
10406         }
10407     }
10408
10409   /* If DECL is dependent, and refers to a function, then just return
10410      its name; we will look it up again during template instantiation.  */
10411   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10412     {
10413       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10414       if (TYPE_P (scope) && dependent_type_p (scope))
10415         return identifier;
10416     }
10417
10418   return decl;
10419 }
10420
10421 /* Parse a template-argument-list.
10422
10423    template-argument-list:
10424      template-argument ... [opt]
10425      template-argument-list , template-argument ... [opt]
10426
10427    Returns a TREE_VEC containing the arguments.  */
10428
10429 static tree
10430 cp_parser_template_argument_list (cp_parser* parser)
10431 {
10432   tree fixed_args[10];
10433   unsigned n_args = 0;
10434   unsigned alloced = 10;
10435   tree *arg_ary = fixed_args;
10436   tree vec;
10437   bool saved_in_template_argument_list_p;
10438   bool saved_ice_p;
10439   bool saved_non_ice_p;
10440
10441   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10442   parser->in_template_argument_list_p = true;
10443   /* Even if the template-id appears in an integral
10444      constant-expression, the contents of the argument list do
10445      not.  */
10446   saved_ice_p = parser->integral_constant_expression_p;
10447   parser->integral_constant_expression_p = false;
10448   saved_non_ice_p = parser->non_integral_constant_expression_p;
10449   parser->non_integral_constant_expression_p = false;
10450   /* Parse the arguments.  */
10451   do
10452     {
10453       tree argument;
10454
10455       if (n_args)
10456         /* Consume the comma.  */
10457         cp_lexer_consume_token (parser->lexer);
10458
10459       /* Parse the template-argument.  */
10460       argument = cp_parser_template_argument (parser);
10461
10462       /* If the next token is an ellipsis, we're expanding a template
10463          argument pack. */
10464       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10465         {
10466           /* Consume the `...' token. */
10467           cp_lexer_consume_token (parser->lexer);
10468
10469           /* Make the argument into a TYPE_PACK_EXPANSION or
10470              EXPR_PACK_EXPANSION. */
10471           argument = make_pack_expansion (argument);
10472         }
10473
10474       if (n_args == alloced)
10475         {
10476           alloced *= 2;
10477
10478           if (arg_ary == fixed_args)
10479             {
10480               arg_ary = XNEWVEC (tree, alloced);
10481               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10482             }
10483           else
10484             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10485         }
10486       arg_ary[n_args++] = argument;
10487     }
10488   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10489
10490   vec = make_tree_vec (n_args);
10491
10492   while (n_args--)
10493     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10494
10495   if (arg_ary != fixed_args)
10496     free (arg_ary);
10497   parser->non_integral_constant_expression_p = saved_non_ice_p;
10498   parser->integral_constant_expression_p = saved_ice_p;
10499   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10500   return vec;
10501 }
10502
10503 /* Parse a template-argument.
10504
10505    template-argument:
10506      assignment-expression
10507      type-id
10508      id-expression
10509
10510    The representation is that of an assignment-expression, type-id, or
10511    id-expression -- except that the qualified id-expression is
10512    evaluated, so that the value returned is either a DECL or an
10513    OVERLOAD.
10514
10515    Although the standard says "assignment-expression", it forbids
10516    throw-expressions or assignments in the template argument.
10517    Therefore, we use "conditional-expression" instead.  */
10518
10519 static tree
10520 cp_parser_template_argument (cp_parser* parser)
10521 {
10522   tree argument;
10523   bool template_p;
10524   bool address_p;
10525   bool maybe_type_id = false;
10526   cp_token *token = NULL, *argument_start_token = NULL;
10527   cp_id_kind idk;
10528
10529   /* There's really no way to know what we're looking at, so we just
10530      try each alternative in order.
10531
10532        [temp.arg]
10533
10534        In a template-argument, an ambiguity between a type-id and an
10535        expression is resolved to a type-id, regardless of the form of
10536        the corresponding template-parameter.
10537
10538      Therefore, we try a type-id first.  */
10539   cp_parser_parse_tentatively (parser);
10540   argument = cp_parser_type_id (parser);
10541   /* If there was no error parsing the type-id but the next token is a
10542      '>>', our behavior depends on which dialect of C++ we're
10543      parsing. In C++98, we probably found a typo for '> >'. But there
10544      are type-id which are also valid expressions. For instance:
10545
10546      struct X { int operator >> (int); };
10547      template <int V> struct Foo {};
10548      Foo<X () >> 5> r;
10549
10550      Here 'X()' is a valid type-id of a function type, but the user just
10551      wanted to write the expression "X() >> 5". Thus, we remember that we
10552      found a valid type-id, but we still try to parse the argument as an
10553      expression to see what happens. 
10554
10555      In C++0x, the '>>' will be considered two separate '>'
10556      tokens.  */
10557   if (!cp_parser_error_occurred (parser)
10558       && cxx_dialect == cxx98
10559       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10560     {
10561       maybe_type_id = true;
10562       cp_parser_abort_tentative_parse (parser);
10563     }
10564   else
10565     {
10566       /* If the next token isn't a `,' or a `>', then this argument wasn't
10567       really finished. This means that the argument is not a valid
10568       type-id.  */
10569       if (!cp_parser_next_token_ends_template_argument_p (parser))
10570         cp_parser_error (parser, "expected template-argument");
10571       /* If that worked, we're done.  */
10572       if (cp_parser_parse_definitely (parser))
10573         return argument;
10574     }
10575   /* We're still not sure what the argument will be.  */
10576   cp_parser_parse_tentatively (parser);
10577   /* Try a template.  */
10578   argument_start_token = cp_lexer_peek_token (parser->lexer);
10579   argument = cp_parser_id_expression (parser,
10580                                       /*template_keyword_p=*/false,
10581                                       /*check_dependency_p=*/true,
10582                                       &template_p,
10583                                       /*declarator_p=*/false,
10584                                       /*optional_p=*/false);
10585   /* If the next token isn't a `,' or a `>', then this argument wasn't
10586      really finished.  */
10587   if (!cp_parser_next_token_ends_template_argument_p (parser))
10588     cp_parser_error (parser, "expected template-argument");
10589   if (!cp_parser_error_occurred (parser))
10590     {
10591       /* Figure out what is being referred to.  If the id-expression
10592          was for a class template specialization, then we will have a
10593          TYPE_DECL at this point.  There is no need to do name lookup
10594          at this point in that case.  */
10595       if (TREE_CODE (argument) != TYPE_DECL)
10596         argument = cp_parser_lookup_name (parser, argument,
10597                                           none_type,
10598                                           /*is_template=*/template_p,
10599                                           /*is_namespace=*/false,
10600                                           /*check_dependency=*/true,
10601                                           /*ambiguous_decls=*/NULL,
10602                                           argument_start_token->location);
10603       if (TREE_CODE (argument) != TEMPLATE_DECL
10604           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10605         cp_parser_error (parser, "expected template-name");
10606     }
10607   if (cp_parser_parse_definitely (parser))
10608     return argument;
10609   /* It must be a non-type argument.  There permitted cases are given
10610      in [temp.arg.nontype]:
10611
10612      -- an integral constant-expression of integral or enumeration
10613         type; or
10614
10615      -- the name of a non-type template-parameter; or
10616
10617      -- the name of an object or function with external linkage...
10618
10619      -- the address of an object or function with external linkage...
10620
10621      -- a pointer to member...  */
10622   /* Look for a non-type template parameter.  */
10623   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10624     {
10625       cp_parser_parse_tentatively (parser);
10626       argument = cp_parser_primary_expression (parser,
10627                                                /*address_p=*/false,
10628                                                /*cast_p=*/false,
10629                                                /*template_arg_p=*/true,
10630                                                &idk);
10631       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10632           || !cp_parser_next_token_ends_template_argument_p (parser))
10633         cp_parser_simulate_error (parser);
10634       if (cp_parser_parse_definitely (parser))
10635         return argument;
10636     }
10637
10638   /* If the next token is "&", the argument must be the address of an
10639      object or function with external linkage.  */
10640   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10641   if (address_p)
10642     cp_lexer_consume_token (parser->lexer);
10643   /* See if we might have an id-expression.  */
10644   token = cp_lexer_peek_token (parser->lexer);
10645   if (token->type == CPP_NAME
10646       || token->keyword == RID_OPERATOR
10647       || token->type == CPP_SCOPE
10648       || token->type == CPP_TEMPLATE_ID
10649       || token->type == CPP_NESTED_NAME_SPECIFIER)
10650     {
10651       cp_parser_parse_tentatively (parser);
10652       argument = cp_parser_primary_expression (parser,
10653                                                address_p,
10654                                                /*cast_p=*/false,
10655                                                /*template_arg_p=*/true,
10656                                                &idk);
10657       if (cp_parser_error_occurred (parser)
10658           || !cp_parser_next_token_ends_template_argument_p (parser))
10659         cp_parser_abort_tentative_parse (parser);
10660       else
10661         {
10662           if (TREE_CODE (argument) == INDIRECT_REF)
10663             {
10664               gcc_assert (REFERENCE_REF_P (argument));
10665               argument = TREE_OPERAND (argument, 0);
10666             }
10667
10668           if (TREE_CODE (argument) == VAR_DECL)
10669             {
10670               /* A variable without external linkage might still be a
10671                  valid constant-expression, so no error is issued here
10672                  if the external-linkage check fails.  */
10673               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10674                 cp_parser_simulate_error (parser);
10675             }
10676           else if (is_overloaded_fn (argument))
10677             /* All overloaded functions are allowed; if the external
10678                linkage test does not pass, an error will be issued
10679                later.  */
10680             ;
10681           else if (address_p
10682                    && (TREE_CODE (argument) == OFFSET_REF
10683                        || TREE_CODE (argument) == SCOPE_REF))
10684             /* A pointer-to-member.  */
10685             ;
10686           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10687             ;
10688           else
10689             cp_parser_simulate_error (parser);
10690
10691           if (cp_parser_parse_definitely (parser))
10692             {
10693               if (address_p)
10694                 argument = build_x_unary_op (ADDR_EXPR, argument,
10695                                              tf_warning_or_error);
10696               return argument;
10697             }
10698         }
10699     }
10700   /* If the argument started with "&", there are no other valid
10701      alternatives at this point.  */
10702   if (address_p)
10703     {
10704       cp_parser_error (parser, "invalid non-type template argument");
10705       return error_mark_node;
10706     }
10707
10708   /* If the argument wasn't successfully parsed as a type-id followed
10709      by '>>', the argument can only be a constant expression now.
10710      Otherwise, we try parsing the constant-expression tentatively,
10711      because the argument could really be a type-id.  */
10712   if (maybe_type_id)
10713     cp_parser_parse_tentatively (parser);
10714   argument = cp_parser_constant_expression (parser,
10715                                             /*allow_non_constant_p=*/false,
10716                                             /*non_constant_p=*/NULL);
10717   argument = fold_non_dependent_expr (argument);
10718   if (!maybe_type_id)
10719     return argument;
10720   if (!cp_parser_next_token_ends_template_argument_p (parser))
10721     cp_parser_error (parser, "expected template-argument");
10722   if (cp_parser_parse_definitely (parser))
10723     return argument;
10724   /* We did our best to parse the argument as a non type-id, but that
10725      was the only alternative that matched (albeit with a '>' after
10726      it). We can assume it's just a typo from the user, and a
10727      diagnostic will then be issued.  */
10728   return cp_parser_type_id (parser);
10729 }
10730
10731 /* Parse an explicit-instantiation.
10732
10733    explicit-instantiation:
10734      template declaration
10735
10736    Although the standard says `declaration', what it really means is:
10737
10738    explicit-instantiation:
10739      template decl-specifier-seq [opt] declarator [opt] ;
10740
10741    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10742    supposed to be allowed.  A defect report has been filed about this
10743    issue.
10744
10745    GNU Extension:
10746
10747    explicit-instantiation:
10748      storage-class-specifier template
10749        decl-specifier-seq [opt] declarator [opt] ;
10750      function-specifier template
10751        decl-specifier-seq [opt] declarator [opt] ;  */
10752
10753 static void
10754 cp_parser_explicit_instantiation (cp_parser* parser)
10755 {
10756   int declares_class_or_enum;
10757   cp_decl_specifier_seq decl_specifiers;
10758   tree extension_specifier = NULL_TREE;
10759   cp_token *token;
10760
10761   /* Look for an (optional) storage-class-specifier or
10762      function-specifier.  */
10763   if (cp_parser_allow_gnu_extensions_p (parser))
10764     {
10765       extension_specifier
10766         = cp_parser_storage_class_specifier_opt (parser);
10767       if (!extension_specifier)
10768         extension_specifier
10769           = cp_parser_function_specifier_opt (parser,
10770                                               /*decl_specs=*/NULL);
10771     }
10772
10773   /* Look for the `template' keyword.  */
10774   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10775   /* Let the front end know that we are processing an explicit
10776      instantiation.  */
10777   begin_explicit_instantiation ();
10778   /* [temp.explicit] says that we are supposed to ignore access
10779      control while processing explicit instantiation directives.  */
10780   push_deferring_access_checks (dk_no_check);
10781   /* Parse a decl-specifier-seq.  */
10782   token = cp_lexer_peek_token (parser->lexer);
10783   cp_parser_decl_specifier_seq (parser,
10784                                 CP_PARSER_FLAGS_OPTIONAL,
10785                                 &decl_specifiers,
10786                                 &declares_class_or_enum);
10787   /* If there was exactly one decl-specifier, and it declared a class,
10788      and there's no declarator, then we have an explicit type
10789      instantiation.  */
10790   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10791     {
10792       tree type;
10793
10794       type = check_tag_decl (&decl_specifiers);
10795       /* Turn access control back on for names used during
10796          template instantiation.  */
10797       pop_deferring_access_checks ();
10798       if (type)
10799         do_type_instantiation (type, extension_specifier,
10800                                /*complain=*/tf_error);
10801     }
10802   else
10803     {
10804       cp_declarator *declarator;
10805       tree decl;
10806
10807       /* Parse the declarator.  */
10808       declarator
10809         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10810                                 /*ctor_dtor_or_conv_p=*/NULL,
10811                                 /*parenthesized_p=*/NULL,
10812                                 /*member_p=*/false);
10813       if (declares_class_or_enum & 2)
10814         cp_parser_check_for_definition_in_return_type (declarator,
10815                                                        decl_specifiers.type,
10816                                                        decl_specifiers.type_location);
10817       if (declarator != cp_error_declarator)
10818         {
10819           decl = grokdeclarator (declarator, &decl_specifiers,
10820                                  NORMAL, 0, &decl_specifiers.attributes);
10821           /* Turn access control back on for names used during
10822              template instantiation.  */
10823           pop_deferring_access_checks ();
10824           /* Do the explicit instantiation.  */
10825           do_decl_instantiation (decl, extension_specifier);
10826         }
10827       else
10828         {
10829           pop_deferring_access_checks ();
10830           /* Skip the body of the explicit instantiation.  */
10831           cp_parser_skip_to_end_of_statement (parser);
10832         }
10833     }
10834   /* We're done with the instantiation.  */
10835   end_explicit_instantiation ();
10836
10837   cp_parser_consume_semicolon_at_end_of_statement (parser);
10838 }
10839
10840 /* Parse an explicit-specialization.
10841
10842    explicit-specialization:
10843      template < > declaration
10844
10845    Although the standard says `declaration', what it really means is:
10846
10847    explicit-specialization:
10848      template <> decl-specifier [opt] init-declarator [opt] ;
10849      template <> function-definition
10850      template <> explicit-specialization
10851      template <> template-declaration  */
10852
10853 static void
10854 cp_parser_explicit_specialization (cp_parser* parser)
10855 {
10856   bool need_lang_pop;
10857   cp_token *token = cp_lexer_peek_token (parser->lexer);
10858
10859   /* Look for the `template' keyword.  */
10860   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10861   /* Look for the `<'.  */
10862   cp_parser_require (parser, CPP_LESS, "%<<%>");
10863   /* Look for the `>'.  */
10864   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10865   /* We have processed another parameter list.  */
10866   ++parser->num_template_parameter_lists;
10867   /* [temp]
10868
10869      A template ... explicit specialization ... shall not have C
10870      linkage.  */
10871   if (current_lang_name == lang_name_c)
10872     {
10873       error ("%Htemplate specialization with C linkage", &token->location);
10874       /* Give it C++ linkage to avoid confusing other parts of the
10875          front end.  */
10876       push_lang_context (lang_name_cplusplus);
10877       need_lang_pop = true;
10878     }
10879   else
10880     need_lang_pop = false;
10881   /* Let the front end know that we are beginning a specialization.  */
10882   if (!begin_specialization ())
10883     {
10884       end_specialization ();
10885       cp_parser_skip_to_end_of_block_or_statement (parser);
10886       return;
10887     }
10888
10889   /* If the next keyword is `template', we need to figure out whether
10890      or not we're looking a template-declaration.  */
10891   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10892     {
10893       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10894           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10895         cp_parser_template_declaration_after_export (parser,
10896                                                      /*member_p=*/false);
10897       else
10898         cp_parser_explicit_specialization (parser);
10899     }
10900   else
10901     /* Parse the dependent declaration.  */
10902     cp_parser_single_declaration (parser,
10903                                   /*checks=*/NULL,
10904                                   /*member_p=*/false,
10905                                   /*explicit_specialization_p=*/true,
10906                                   /*friend_p=*/NULL);
10907   /* We're done with the specialization.  */
10908   end_specialization ();
10909   /* For the erroneous case of a template with C linkage, we pushed an
10910      implicit C++ linkage scope; exit that scope now.  */
10911   if (need_lang_pop)
10912     pop_lang_context ();
10913   /* We're done with this parameter list.  */
10914   --parser->num_template_parameter_lists;
10915 }
10916
10917 /* Parse a type-specifier.
10918
10919    type-specifier:
10920      simple-type-specifier
10921      class-specifier
10922      enum-specifier
10923      elaborated-type-specifier
10924      cv-qualifier
10925
10926    GNU Extension:
10927
10928    type-specifier:
10929      __complex__
10930
10931    Returns a representation of the type-specifier.  For a
10932    class-specifier, enum-specifier, or elaborated-type-specifier, a
10933    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10934
10935    The parser flags FLAGS is used to control type-specifier parsing.
10936
10937    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10938    in a decl-specifier-seq.
10939
10940    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10941    class-specifier, enum-specifier, or elaborated-type-specifier, then
10942    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10943    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10944    zero.
10945
10946    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10947    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10948    is set to FALSE.  */
10949
10950 static tree
10951 cp_parser_type_specifier (cp_parser* parser,
10952                           cp_parser_flags flags,
10953                           cp_decl_specifier_seq *decl_specs,
10954                           bool is_declaration,
10955                           int* declares_class_or_enum,
10956                           bool* is_cv_qualifier)
10957 {
10958   tree type_spec = NULL_TREE;
10959   cp_token *token;
10960   enum rid keyword;
10961   cp_decl_spec ds = ds_last;
10962
10963   /* Assume this type-specifier does not declare a new type.  */
10964   if (declares_class_or_enum)
10965     *declares_class_or_enum = 0;
10966   /* And that it does not specify a cv-qualifier.  */
10967   if (is_cv_qualifier)
10968     *is_cv_qualifier = false;
10969   /* Peek at the next token.  */
10970   token = cp_lexer_peek_token (parser->lexer);
10971
10972   /* If we're looking at a keyword, we can use that to guide the
10973      production we choose.  */
10974   keyword = token->keyword;
10975   switch (keyword)
10976     {
10977     case RID_ENUM:
10978       /* Look for the enum-specifier.  */
10979       type_spec = cp_parser_enum_specifier (parser);
10980       /* If that worked, we're done.  */
10981       if (type_spec)
10982         {
10983           if (declares_class_or_enum)
10984             *declares_class_or_enum = 2;
10985           if (decl_specs)
10986             cp_parser_set_decl_spec_type (decl_specs,
10987                                           type_spec,
10988                                           token->location,
10989                                           /*user_defined_p=*/true);
10990           return type_spec;
10991         }
10992       else
10993         goto elaborated_type_specifier;
10994
10995       /* Any of these indicate either a class-specifier, or an
10996          elaborated-type-specifier.  */
10997     case RID_CLASS:
10998     case RID_STRUCT:
10999     case RID_UNION:
11000       /* Parse tentatively so that we can back up if we don't find a
11001          class-specifier.  */
11002       cp_parser_parse_tentatively (parser);
11003       /* Look for the class-specifier.  */
11004       type_spec = cp_parser_class_specifier (parser);
11005       /* If that worked, we're done.  */
11006       if (cp_parser_parse_definitely (parser))
11007         {
11008           if (declares_class_or_enum)
11009             *declares_class_or_enum = 2;
11010           if (decl_specs)
11011             cp_parser_set_decl_spec_type (decl_specs,
11012                                           type_spec,
11013                                           token->location,
11014                                           /*user_defined_p=*/true);
11015           return type_spec;
11016         }
11017
11018       /* Fall through.  */
11019     elaborated_type_specifier:
11020       /* We're declaring (not defining) a class or enum.  */
11021       if (declares_class_or_enum)
11022         *declares_class_or_enum = 1;
11023
11024       /* Fall through.  */
11025     case RID_TYPENAME:
11026       /* Look for an elaborated-type-specifier.  */
11027       type_spec
11028         = (cp_parser_elaborated_type_specifier
11029            (parser,
11030             decl_specs && decl_specs->specs[(int) ds_friend],
11031             is_declaration));
11032       if (decl_specs)
11033         cp_parser_set_decl_spec_type (decl_specs,
11034                                       type_spec,
11035                                       token->location,
11036                                       /*user_defined_p=*/true);
11037       return type_spec;
11038
11039     case RID_CONST:
11040       ds = ds_const;
11041       if (is_cv_qualifier)
11042         *is_cv_qualifier = true;
11043       break;
11044
11045     case RID_VOLATILE:
11046       ds = ds_volatile;
11047       if (is_cv_qualifier)
11048         *is_cv_qualifier = true;
11049       break;
11050
11051     case RID_RESTRICT:
11052       ds = ds_restrict;
11053       if (is_cv_qualifier)
11054         *is_cv_qualifier = true;
11055       break;
11056
11057     case RID_COMPLEX:
11058       /* The `__complex__' keyword is a GNU extension.  */
11059       ds = ds_complex;
11060       break;
11061
11062     default:
11063       break;
11064     }
11065
11066   /* Handle simple keywords.  */
11067   if (ds != ds_last)
11068     {
11069       if (decl_specs)
11070         {
11071           ++decl_specs->specs[(int)ds];
11072           decl_specs->any_specifiers_p = true;
11073         }
11074       return cp_lexer_consume_token (parser->lexer)->u.value;
11075     }
11076
11077   /* If we do not already have a type-specifier, assume we are looking
11078      at a simple-type-specifier.  */
11079   type_spec = cp_parser_simple_type_specifier (parser,
11080                                                decl_specs,
11081                                                flags);
11082
11083   /* If we didn't find a type-specifier, and a type-specifier was not
11084      optional in this context, issue an error message.  */
11085   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11086     {
11087       cp_parser_error (parser, "expected type specifier");
11088       return error_mark_node;
11089     }
11090
11091   return type_spec;
11092 }
11093
11094 /* Parse a simple-type-specifier.
11095
11096    simple-type-specifier:
11097      :: [opt] nested-name-specifier [opt] type-name
11098      :: [opt] nested-name-specifier template template-id
11099      char
11100      wchar_t
11101      bool
11102      short
11103      int
11104      long
11105      signed
11106      unsigned
11107      float
11108      double
11109      void
11110
11111    C++0x Extension:
11112
11113    simple-type-specifier:
11114      auto
11115      decltype ( expression )   
11116      char16_t
11117      char32_t
11118
11119    GNU Extension:
11120
11121    simple-type-specifier:
11122      __typeof__ unary-expression
11123      __typeof__ ( type-id )
11124
11125    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11126    appropriately updated.  */
11127
11128 static tree
11129 cp_parser_simple_type_specifier (cp_parser* parser,
11130                                  cp_decl_specifier_seq *decl_specs,
11131                                  cp_parser_flags flags)
11132 {
11133   tree type = NULL_TREE;
11134   cp_token *token;
11135
11136   /* Peek at the next token.  */
11137   token = cp_lexer_peek_token (parser->lexer);
11138
11139   /* If we're looking at a keyword, things are easy.  */
11140   switch (token->keyword)
11141     {
11142     case RID_CHAR:
11143       if (decl_specs)
11144         decl_specs->explicit_char_p = true;
11145       type = char_type_node;
11146       break;
11147     case RID_CHAR16:
11148       type = char16_type_node;
11149       break;
11150     case RID_CHAR32:
11151       type = char32_type_node;
11152       break;
11153     case RID_WCHAR:
11154       type = wchar_type_node;
11155       break;
11156     case RID_BOOL:
11157       type = boolean_type_node;
11158       break;
11159     case RID_SHORT:
11160       if (decl_specs)
11161         ++decl_specs->specs[(int) ds_short];
11162       type = short_integer_type_node;
11163       break;
11164     case RID_INT:
11165       if (decl_specs)
11166         decl_specs->explicit_int_p = true;
11167       type = integer_type_node;
11168       break;
11169     case RID_LONG:
11170       if (decl_specs)
11171         ++decl_specs->specs[(int) ds_long];
11172       type = long_integer_type_node;
11173       break;
11174     case RID_SIGNED:
11175       if (decl_specs)
11176         ++decl_specs->specs[(int) ds_signed];
11177       type = integer_type_node;
11178       break;
11179     case RID_UNSIGNED:
11180       if (decl_specs)
11181         ++decl_specs->specs[(int) ds_unsigned];
11182       type = unsigned_type_node;
11183       break;
11184     case RID_FLOAT:
11185       type = float_type_node;
11186       break;
11187     case RID_DOUBLE:
11188       type = double_type_node;
11189       break;
11190     case RID_VOID:
11191       type = void_type_node;
11192       break;
11193       
11194     case RID_AUTO:
11195       maybe_warn_cpp0x ("C++0x auto");
11196       type = make_auto ();
11197       break;
11198
11199     case RID_DECLTYPE:
11200       /* Parse the `decltype' type.  */
11201       type = cp_parser_decltype (parser);
11202
11203       if (decl_specs)
11204         cp_parser_set_decl_spec_type (decl_specs, type,
11205                                       token->location,
11206                                       /*user_defined_p=*/true);
11207
11208       return type;
11209
11210     case RID_TYPEOF:
11211       /* Consume the `typeof' token.  */
11212       cp_lexer_consume_token (parser->lexer);
11213       /* Parse the operand to `typeof'.  */
11214       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11215       /* If it is not already a TYPE, take its type.  */
11216       if (!TYPE_P (type))
11217         type = finish_typeof (type);
11218
11219       if (decl_specs)
11220         cp_parser_set_decl_spec_type (decl_specs, type,
11221                                       token->location,
11222                                       /*user_defined_p=*/true);
11223
11224       return type;
11225
11226     default:
11227       break;
11228     }
11229
11230   /* If the type-specifier was for a built-in type, we're done.  */
11231   if (type)
11232     {
11233       tree id;
11234
11235       /* Record the type.  */
11236       if (decl_specs
11237           && (token->keyword != RID_SIGNED
11238               && token->keyword != RID_UNSIGNED
11239               && token->keyword != RID_SHORT
11240               && token->keyword != RID_LONG))
11241         cp_parser_set_decl_spec_type (decl_specs,
11242                                       type,
11243                                       token->location,
11244                                       /*user_defined=*/false);
11245       if (decl_specs)
11246         decl_specs->any_specifiers_p = true;
11247
11248       /* Consume the token.  */
11249       id = cp_lexer_consume_token (parser->lexer)->u.value;
11250
11251       /* There is no valid C++ program where a non-template type is
11252          followed by a "<".  That usually indicates that the user thought
11253          that the type was a template.  */
11254       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11255
11256       return TYPE_NAME (type);
11257     }
11258
11259   /* The type-specifier must be a user-defined type.  */
11260   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11261     {
11262       bool qualified_p;
11263       bool global_p;
11264
11265       /* Don't gobble tokens or issue error messages if this is an
11266          optional type-specifier.  */
11267       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11268         cp_parser_parse_tentatively (parser);
11269
11270       /* Look for the optional `::' operator.  */
11271       global_p
11272         = (cp_parser_global_scope_opt (parser,
11273                                        /*current_scope_valid_p=*/false)
11274            != NULL_TREE);
11275       /* Look for the nested-name specifier.  */
11276       qualified_p
11277         = (cp_parser_nested_name_specifier_opt (parser,
11278                                                 /*typename_keyword_p=*/false,
11279                                                 /*check_dependency_p=*/true,
11280                                                 /*type_p=*/false,
11281                                                 /*is_declaration=*/false)
11282            != NULL_TREE);
11283       token = cp_lexer_peek_token (parser->lexer);
11284       /* If we have seen a nested-name-specifier, and the next token
11285          is `template', then we are using the template-id production.  */
11286       if (parser->scope
11287           && cp_parser_optional_template_keyword (parser))
11288         {
11289           /* Look for the template-id.  */
11290           type = cp_parser_template_id (parser,
11291                                         /*template_keyword_p=*/true,
11292                                         /*check_dependency_p=*/true,
11293                                         /*is_declaration=*/false);
11294           /* If the template-id did not name a type, we are out of
11295              luck.  */
11296           if (TREE_CODE (type) != TYPE_DECL)
11297             {
11298               cp_parser_error (parser, "expected template-id for type");
11299               type = NULL_TREE;
11300             }
11301         }
11302       /* Otherwise, look for a type-name.  */
11303       else
11304         type = cp_parser_type_name (parser);
11305       /* Keep track of all name-lookups performed in class scopes.  */
11306       if (type
11307           && !global_p
11308           && !qualified_p
11309           && TREE_CODE (type) == TYPE_DECL
11310           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11311         maybe_note_name_used_in_class (DECL_NAME (type), type);
11312       /* If it didn't work out, we don't have a TYPE.  */
11313       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11314           && !cp_parser_parse_definitely (parser))
11315         type = NULL_TREE;
11316       if (type && decl_specs)
11317         cp_parser_set_decl_spec_type (decl_specs, type,
11318                                       token->location,
11319                                       /*user_defined=*/true);
11320     }
11321
11322   /* If we didn't get a type-name, issue an error message.  */
11323   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11324     {
11325       cp_parser_error (parser, "expected type-name");
11326       return error_mark_node;
11327     }
11328
11329   /* There is no valid C++ program where a non-template type is
11330      followed by a "<".  That usually indicates that the user thought
11331      that the type was a template.  */
11332   if (type && type != error_mark_node)
11333     {
11334       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11335          If it is, then the '<'...'>' enclose protocol names rather than
11336          template arguments, and so everything is fine.  */
11337       if (c_dialect_objc ()
11338           && (objc_is_id (type) || objc_is_class_name (type)))
11339         {
11340           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11341           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11342
11343           /* Clobber the "unqualified" type previously entered into
11344              DECL_SPECS with the new, improved protocol-qualified version.  */
11345           if (decl_specs)
11346             decl_specs->type = qual_type;
11347
11348           return qual_type;
11349         }
11350
11351       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11352                                                token->location);
11353     }
11354
11355   return type;
11356 }
11357
11358 /* Parse a type-name.
11359
11360    type-name:
11361      class-name
11362      enum-name
11363      typedef-name
11364
11365    enum-name:
11366      identifier
11367
11368    typedef-name:
11369      identifier
11370
11371    Returns a TYPE_DECL for the type.  */
11372
11373 static tree
11374 cp_parser_type_name (cp_parser* parser)
11375 {
11376   tree type_decl;
11377
11378   /* We can't know yet whether it is a class-name or not.  */
11379   cp_parser_parse_tentatively (parser);
11380   /* Try a class-name.  */
11381   type_decl = cp_parser_class_name (parser,
11382                                     /*typename_keyword_p=*/false,
11383                                     /*template_keyword_p=*/false,
11384                                     none_type,
11385                                     /*check_dependency_p=*/true,
11386                                     /*class_head_p=*/false,
11387                                     /*is_declaration=*/false);
11388   /* If it's not a class-name, keep looking.  */
11389   if (!cp_parser_parse_definitely (parser))
11390     {
11391       /* It must be a typedef-name or an enum-name.  */
11392       return cp_parser_nonclass_name (parser);
11393     }
11394
11395   return type_decl;
11396 }
11397
11398 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11399
11400    enum-name:
11401      identifier
11402
11403    typedef-name:
11404      identifier
11405
11406    Returns a TYPE_DECL for the type.  */
11407
11408 static tree
11409 cp_parser_nonclass_name (cp_parser* parser)
11410 {
11411   tree type_decl;
11412   tree identifier;
11413
11414   cp_token *token = cp_lexer_peek_token (parser->lexer);
11415   identifier = cp_parser_identifier (parser);
11416   if (identifier == error_mark_node)
11417     return error_mark_node;
11418
11419   /* Look up the type-name.  */
11420   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11421
11422   if (TREE_CODE (type_decl) != TYPE_DECL
11423       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11424     {
11425       /* See if this is an Objective-C type.  */
11426       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11427       tree type = objc_get_protocol_qualified_type (identifier, protos);
11428       if (type)
11429         type_decl = TYPE_NAME (type);
11430     }
11431   
11432   /* Issue an error if we did not find a type-name.  */
11433   if (TREE_CODE (type_decl) != TYPE_DECL)
11434     {
11435       if (!cp_parser_simulate_error (parser))
11436         cp_parser_name_lookup_error (parser, identifier, type_decl,
11437                                      "is not a type", token->location);
11438       return error_mark_node;
11439     }
11440   /* Remember that the name was used in the definition of the
11441      current class so that we can check later to see if the
11442      meaning would have been different after the class was
11443      entirely defined.  */
11444   else if (type_decl != error_mark_node
11445            && !parser->scope)
11446     maybe_note_name_used_in_class (identifier, type_decl);
11447   
11448   return type_decl;
11449 }
11450
11451 /* Parse an elaborated-type-specifier.  Note that the grammar given
11452    here incorporates the resolution to DR68.
11453
11454    elaborated-type-specifier:
11455      class-key :: [opt] nested-name-specifier [opt] identifier
11456      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11457      enum-key :: [opt] nested-name-specifier [opt] identifier
11458      typename :: [opt] nested-name-specifier identifier
11459      typename :: [opt] nested-name-specifier template [opt]
11460        template-id
11461
11462    GNU extension:
11463
11464    elaborated-type-specifier:
11465      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11466      class-key attributes :: [opt] nested-name-specifier [opt]
11467                template [opt] template-id
11468      enum attributes :: [opt] nested-name-specifier [opt] identifier
11469
11470    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11471    declared `friend'.  If IS_DECLARATION is TRUE, then this
11472    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11473    something is being declared.
11474
11475    Returns the TYPE specified.  */
11476
11477 static tree
11478 cp_parser_elaborated_type_specifier (cp_parser* parser,
11479                                      bool is_friend,
11480                                      bool is_declaration)
11481 {
11482   enum tag_types tag_type;
11483   tree identifier;
11484   tree type = NULL_TREE;
11485   tree attributes = NULL_TREE;
11486   cp_token *token = NULL;
11487
11488   /* See if we're looking at the `enum' keyword.  */
11489   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11490     {
11491       /* Consume the `enum' token.  */
11492       cp_lexer_consume_token (parser->lexer);
11493       /* Remember that it's an enumeration type.  */
11494       tag_type = enum_type;
11495       /* Parse the optional `struct' or `class' key (for C++0x scoped
11496          enums).  */
11497       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11498           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11499         {
11500           if (cxx_dialect == cxx98)
11501             maybe_warn_cpp0x ("scoped enums");
11502
11503           /* Consume the `struct' or `class'.  */
11504           cp_lexer_consume_token (parser->lexer);
11505         }
11506       /* Parse the attributes.  */
11507       attributes = cp_parser_attributes_opt (parser);
11508     }
11509   /* Or, it might be `typename'.  */
11510   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11511                                            RID_TYPENAME))
11512     {
11513       /* Consume the `typename' token.  */
11514       cp_lexer_consume_token (parser->lexer);
11515       /* Remember that it's a `typename' type.  */
11516       tag_type = typename_type;
11517       /* The `typename' keyword is only allowed in templates.  */
11518       if (!processing_template_decl)
11519         permerror (input_location, "using %<typename%> outside of template");
11520     }
11521   /* Otherwise it must be a class-key.  */
11522   else
11523     {
11524       tag_type = cp_parser_class_key (parser);
11525       if (tag_type == none_type)
11526         return error_mark_node;
11527       /* Parse the attributes.  */
11528       attributes = cp_parser_attributes_opt (parser);
11529     }
11530
11531   /* Look for the `::' operator.  */
11532   cp_parser_global_scope_opt (parser,
11533                               /*current_scope_valid_p=*/false);
11534   /* Look for the nested-name-specifier.  */
11535   if (tag_type == typename_type)
11536     {
11537       if (!cp_parser_nested_name_specifier (parser,
11538                                            /*typename_keyword_p=*/true,
11539                                            /*check_dependency_p=*/true,
11540                                            /*type_p=*/true,
11541                                             is_declaration))
11542         return error_mark_node;
11543     }
11544   else
11545     /* Even though `typename' is not present, the proposed resolution
11546        to Core Issue 180 says that in `class A<T>::B', `B' should be
11547        considered a type-name, even if `A<T>' is dependent.  */
11548     cp_parser_nested_name_specifier_opt (parser,
11549                                          /*typename_keyword_p=*/true,
11550                                          /*check_dependency_p=*/true,
11551                                          /*type_p=*/true,
11552                                          is_declaration);
11553  /* For everything but enumeration types, consider a template-id.
11554     For an enumeration type, consider only a plain identifier.  */
11555   if (tag_type != enum_type)
11556     {
11557       bool template_p = false;
11558       tree decl;
11559
11560       /* Allow the `template' keyword.  */
11561       template_p = cp_parser_optional_template_keyword (parser);
11562       /* If we didn't see `template', we don't know if there's a
11563          template-id or not.  */
11564       if (!template_p)
11565         cp_parser_parse_tentatively (parser);
11566       /* Parse the template-id.  */
11567       token = cp_lexer_peek_token (parser->lexer);
11568       decl = cp_parser_template_id (parser, template_p,
11569                                     /*check_dependency_p=*/true,
11570                                     is_declaration);
11571       /* If we didn't find a template-id, look for an ordinary
11572          identifier.  */
11573       if (!template_p && !cp_parser_parse_definitely (parser))
11574         ;
11575       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11576          in effect, then we must assume that, upon instantiation, the
11577          template will correspond to a class.  */
11578       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11579                && tag_type == typename_type)
11580         type = make_typename_type (parser->scope, decl,
11581                                    typename_type,
11582                                    /*complain=*/tf_error);
11583       else
11584         type = TREE_TYPE (decl);
11585     }
11586
11587   if (!type)
11588     {
11589       token = cp_lexer_peek_token (parser->lexer);
11590       identifier = cp_parser_identifier (parser);
11591
11592       if (identifier == error_mark_node)
11593         {
11594           parser->scope = NULL_TREE;
11595           return error_mark_node;
11596         }
11597
11598       /* For a `typename', we needn't call xref_tag.  */
11599       if (tag_type == typename_type
11600           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11601         return cp_parser_make_typename_type (parser, parser->scope,
11602                                              identifier,
11603                                              token->location);
11604       /* Look up a qualified name in the usual way.  */
11605       if (parser->scope)
11606         {
11607           tree decl;
11608           tree ambiguous_decls;
11609
11610           decl = cp_parser_lookup_name (parser, identifier,
11611                                         tag_type,
11612                                         /*is_template=*/false,
11613                                         /*is_namespace=*/false,
11614                                         /*check_dependency=*/true,
11615                                         &ambiguous_decls,
11616                                         token->location);
11617
11618           /* If the lookup was ambiguous, an error will already have been
11619              issued.  */
11620           if (ambiguous_decls)
11621             return error_mark_node;
11622
11623           /* If we are parsing friend declaration, DECL may be a
11624              TEMPLATE_DECL tree node here.  However, we need to check
11625              whether this TEMPLATE_DECL results in valid code.  Consider
11626              the following example:
11627
11628                namespace N {
11629                  template <class T> class C {};
11630                }
11631                class X {
11632                  template <class T> friend class N::C; // #1, valid code
11633                };
11634                template <class T> class Y {
11635                  friend class N::C;                    // #2, invalid code
11636                };
11637
11638              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11639              name lookup of `N::C'.  We see that friend declaration must
11640              be template for the code to be valid.  Note that
11641              processing_template_decl does not work here since it is
11642              always 1 for the above two cases.  */
11643
11644           decl = (cp_parser_maybe_treat_template_as_class
11645                   (decl, /*tag_name_p=*/is_friend
11646                          && parser->num_template_parameter_lists));
11647
11648           if (TREE_CODE (decl) != TYPE_DECL)
11649             {
11650               cp_parser_diagnose_invalid_type_name (parser,
11651                                                     parser->scope,
11652                                                     identifier,
11653                                                     token->location);
11654               return error_mark_node;
11655             }
11656
11657           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11658             {
11659               bool allow_template = (parser->num_template_parameter_lists
11660                                       || DECL_SELF_REFERENCE_P (decl));
11661               type = check_elaborated_type_specifier (tag_type, decl, 
11662                                                       allow_template);
11663
11664               if (type == error_mark_node)
11665                 return error_mark_node;
11666             }
11667
11668           /* Forward declarations of nested types, such as
11669
11670                class C1::C2;
11671                class C1::C2::C3;
11672
11673              are invalid unless all components preceding the final '::'
11674              are complete.  If all enclosing types are complete, these
11675              declarations become merely pointless.
11676
11677              Invalid forward declarations of nested types are errors
11678              caught elsewhere in parsing.  Those that are pointless arrive
11679              here.  */
11680
11681           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11682               && !is_friend && !processing_explicit_instantiation)
11683             warning (0, "declaration %qD does not declare anything", decl);
11684
11685           type = TREE_TYPE (decl);
11686         }
11687       else
11688         {
11689           /* An elaborated-type-specifier sometimes introduces a new type and
11690              sometimes names an existing type.  Normally, the rule is that it
11691              introduces a new type only if there is not an existing type of
11692              the same name already in scope.  For example, given:
11693
11694                struct S {};
11695                void f() { struct S s; }
11696
11697              the `struct S' in the body of `f' is the same `struct S' as in
11698              the global scope; the existing definition is used.  However, if
11699              there were no global declaration, this would introduce a new
11700              local class named `S'.
11701
11702              An exception to this rule applies to the following code:
11703
11704                namespace N { struct S; }
11705
11706              Here, the elaborated-type-specifier names a new type
11707              unconditionally; even if there is already an `S' in the
11708              containing scope this declaration names a new type.
11709              This exception only applies if the elaborated-type-specifier
11710              forms the complete declaration:
11711
11712                [class.name]
11713
11714                A declaration consisting solely of `class-key identifier ;' is
11715                either a redeclaration of the name in the current scope or a
11716                forward declaration of the identifier as a class name.  It
11717                introduces the name into the current scope.
11718
11719              We are in this situation precisely when the next token is a `;'.
11720
11721              An exception to the exception is that a `friend' declaration does
11722              *not* name a new type; i.e., given:
11723
11724                struct S { friend struct T; };
11725
11726              `T' is not a new type in the scope of `S'.
11727
11728              Also, `new struct S' or `sizeof (struct S)' never results in the
11729              definition of a new type; a new type can only be declared in a
11730              declaration context.  */
11731
11732           tag_scope ts;
11733           bool template_p;
11734
11735           if (is_friend)
11736             /* Friends have special name lookup rules.  */
11737             ts = ts_within_enclosing_non_class;
11738           else if (is_declaration
11739                    && cp_lexer_next_token_is (parser->lexer,
11740                                               CPP_SEMICOLON))
11741             /* This is a `class-key identifier ;' */
11742             ts = ts_current;
11743           else
11744             ts = ts_global;
11745
11746           template_p =
11747             (parser->num_template_parameter_lists
11748              && (cp_parser_next_token_starts_class_definition_p (parser)
11749                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11750           /* An unqualified name was used to reference this type, so
11751              there were no qualifying templates.  */
11752           if (!cp_parser_check_template_parameters (parser,
11753                                                     /*num_templates=*/0,
11754                                                     token->location))
11755             return error_mark_node;
11756           type = xref_tag (tag_type, identifier, ts, template_p);
11757         }
11758     }
11759
11760   if (type == error_mark_node)
11761     return error_mark_node;
11762
11763   /* Allow attributes on forward declarations of classes.  */
11764   if (attributes)
11765     {
11766       if (TREE_CODE (type) == TYPENAME_TYPE)
11767         warning (OPT_Wattributes,
11768                  "attributes ignored on uninstantiated type");
11769       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11770                && ! processing_explicit_instantiation)
11771         warning (OPT_Wattributes,
11772                  "attributes ignored on template instantiation");
11773       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11774         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11775       else
11776         warning (OPT_Wattributes,
11777                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11778     }
11779
11780   if (tag_type != enum_type)
11781     cp_parser_check_class_key (tag_type, type);
11782
11783   /* A "<" cannot follow an elaborated type specifier.  If that
11784      happens, the user was probably trying to form a template-id.  */
11785   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11786
11787   return type;
11788 }
11789
11790 /* Parse an enum-specifier.
11791
11792    enum-specifier:
11793      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11794
11795    enum-key:
11796      enum
11797      enum class   [C++0x]
11798      enum struct  [C++0x]
11799
11800    enum-base:   [C++0x]
11801      : type-specifier-seq
11802
11803    GNU Extensions:
11804      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11805        { enumerator-list [opt] }attributes[opt]
11806
11807    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11808    if the token stream isn't an enum-specifier after all.  */
11809
11810 static tree
11811 cp_parser_enum_specifier (cp_parser* parser)
11812 {
11813   tree identifier;
11814   tree type;
11815   tree attributes;
11816   bool scoped_enum_p = false;
11817   bool has_underlying_type = false;
11818   tree underlying_type = NULL_TREE;
11819
11820   /* Parse tentatively so that we can back up if we don't find a
11821      enum-specifier.  */
11822   cp_parser_parse_tentatively (parser);
11823
11824   /* Caller guarantees that the current token is 'enum', an identifier
11825      possibly follows, and the token after that is an opening brace.
11826      If we don't have an identifier, fabricate an anonymous name for
11827      the enumeration being defined.  */
11828   cp_lexer_consume_token (parser->lexer);
11829
11830   /* Parse the "class" or "struct", which indicates a scoped
11831      enumeration type in C++0x.  */
11832   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11833       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11834     {
11835       if (cxx_dialect == cxx98)
11836         maybe_warn_cpp0x ("scoped enums");
11837
11838       /* Consume the `struct' or `class' token.  */
11839       cp_lexer_consume_token (parser->lexer);
11840
11841       scoped_enum_p = true;
11842     }
11843
11844   attributes = cp_parser_attributes_opt (parser);
11845
11846   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11847     identifier = cp_parser_identifier (parser);
11848   else
11849     identifier = make_anon_name ();
11850
11851   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11852   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11853     {
11854       cp_decl_specifier_seq type_specifiers;
11855
11856       /* At this point this is surely not elaborated type specifier.  */
11857       if (!cp_parser_parse_definitely (parser))
11858         return NULL_TREE;
11859
11860       if (cxx_dialect == cxx98)
11861         maybe_warn_cpp0x ("scoped enums");
11862
11863       /* Consume the `:'.  */
11864       cp_lexer_consume_token (parser->lexer);
11865
11866       has_underlying_type = true;
11867
11868       /* Parse the type-specifier-seq.  */
11869       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11870                                     &type_specifiers);
11871
11872       /* If that didn't work, stop.  */
11873       if (type_specifiers.type != error_mark_node)
11874         {
11875           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11876                                             /*initialized=*/0, NULL);
11877           if (underlying_type == error_mark_node)
11878             underlying_type = NULL_TREE;
11879         }
11880     }
11881
11882   /* Look for the `{' but don't consume it yet.  */
11883   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11884     {
11885       cp_parser_error (parser, "expected %<{%>");
11886       if (has_underlying_type)
11887         return NULL_TREE;
11888     }
11889
11890   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11891     return NULL_TREE;
11892
11893   /* Issue an error message if type-definitions are forbidden here.  */
11894   if (!cp_parser_check_type_definition (parser))
11895     type = error_mark_node;
11896   else
11897     /* Create the new type.  We do this before consuming the opening
11898        brace so the enum will be recorded as being on the line of its
11899        tag (or the 'enum' keyword, if there is no tag).  */
11900     type = start_enum (identifier, underlying_type, scoped_enum_p);
11901   
11902   /* Consume the opening brace.  */
11903   cp_lexer_consume_token (parser->lexer);
11904
11905   if (type == error_mark_node)
11906     {
11907       cp_parser_skip_to_end_of_block_or_statement (parser);
11908       return error_mark_node;
11909     }
11910
11911   /* If the next token is not '}', then there are some enumerators.  */
11912   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11913     cp_parser_enumerator_list (parser, type);
11914
11915   /* Consume the final '}'.  */
11916   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11917
11918   /* Look for trailing attributes to apply to this enumeration, and
11919      apply them if appropriate.  */
11920   if (cp_parser_allow_gnu_extensions_p (parser))
11921     {
11922       tree trailing_attr = cp_parser_attributes_opt (parser);
11923       trailing_attr = chainon (trailing_attr, attributes);
11924       cplus_decl_attributes (&type,
11925                              trailing_attr,
11926                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11927     }
11928
11929   /* Finish up the enumeration.  */
11930   finish_enum (type);
11931
11932   return type;
11933 }
11934
11935 /* Parse an enumerator-list.  The enumerators all have the indicated
11936    TYPE.
11937
11938    enumerator-list:
11939      enumerator-definition
11940      enumerator-list , enumerator-definition  */
11941
11942 static void
11943 cp_parser_enumerator_list (cp_parser* parser, tree type)
11944 {
11945   while (true)
11946     {
11947       /* Parse an enumerator-definition.  */
11948       cp_parser_enumerator_definition (parser, type);
11949
11950       /* If the next token is not a ',', we've reached the end of
11951          the list.  */
11952       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11953         break;
11954       /* Otherwise, consume the `,' and keep going.  */
11955       cp_lexer_consume_token (parser->lexer);
11956       /* If the next token is a `}', there is a trailing comma.  */
11957       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11958         {
11959           if (!in_system_header)
11960             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11961           break;
11962         }
11963     }
11964 }
11965
11966 /* Parse an enumerator-definition.  The enumerator has the indicated
11967    TYPE.
11968
11969    enumerator-definition:
11970      enumerator
11971      enumerator = constant-expression
11972
11973    enumerator:
11974      identifier  */
11975
11976 static void
11977 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11978 {
11979   tree identifier;
11980   tree value;
11981
11982   /* Look for the identifier.  */
11983   identifier = cp_parser_identifier (parser);
11984   if (identifier == error_mark_node)
11985     return;
11986
11987   /* If the next token is an '=', then there is an explicit value.  */
11988   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11989     {
11990       /* Consume the `=' token.  */
11991       cp_lexer_consume_token (parser->lexer);
11992       /* Parse the value.  */
11993       value = cp_parser_constant_expression (parser,
11994                                              /*allow_non_constant_p=*/false,
11995                                              NULL);
11996     }
11997   else
11998     value = NULL_TREE;
11999
12000   /* Create the enumerator.  */
12001   build_enumerator (identifier, value, type);
12002 }
12003
12004 /* Parse a namespace-name.
12005
12006    namespace-name:
12007      original-namespace-name
12008      namespace-alias
12009
12010    Returns the NAMESPACE_DECL for the namespace.  */
12011
12012 static tree
12013 cp_parser_namespace_name (cp_parser* parser)
12014 {
12015   tree identifier;
12016   tree namespace_decl;
12017
12018   cp_token *token = cp_lexer_peek_token (parser->lexer);
12019
12020   /* Get the name of the namespace.  */
12021   identifier = cp_parser_identifier (parser);
12022   if (identifier == error_mark_node)
12023     return error_mark_node;
12024
12025   /* Look up the identifier in the currently active scope.  Look only
12026      for namespaces, due to:
12027
12028        [basic.lookup.udir]
12029
12030        When looking up a namespace-name in a using-directive or alias
12031        definition, only namespace names are considered.
12032
12033      And:
12034
12035        [basic.lookup.qual]
12036
12037        During the lookup of a name preceding the :: scope resolution
12038        operator, object, function, and enumerator names are ignored.
12039
12040      (Note that cp_parser_qualifying_entity only calls this
12041      function if the token after the name is the scope resolution
12042      operator.)  */
12043   namespace_decl = cp_parser_lookup_name (parser, identifier,
12044                                           none_type,
12045                                           /*is_template=*/false,
12046                                           /*is_namespace=*/true,
12047                                           /*check_dependency=*/true,
12048                                           /*ambiguous_decls=*/NULL,
12049                                           token->location);
12050   /* If it's not a namespace, issue an error.  */
12051   if (namespace_decl == error_mark_node
12052       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12053     {
12054       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12055         error ("%H%qD is not a namespace-name", &token->location, identifier);
12056       cp_parser_error (parser, "expected namespace-name");
12057       namespace_decl = error_mark_node;
12058     }
12059
12060   return namespace_decl;
12061 }
12062
12063 /* Parse a namespace-definition.
12064
12065    namespace-definition:
12066      named-namespace-definition
12067      unnamed-namespace-definition
12068
12069    named-namespace-definition:
12070      original-namespace-definition
12071      extension-namespace-definition
12072
12073    original-namespace-definition:
12074      namespace identifier { namespace-body }
12075
12076    extension-namespace-definition:
12077      namespace original-namespace-name { namespace-body }
12078
12079    unnamed-namespace-definition:
12080      namespace { namespace-body } */
12081
12082 static void
12083 cp_parser_namespace_definition (cp_parser* parser)
12084 {
12085   tree identifier, attribs;
12086   bool has_visibility;
12087   bool is_inline;
12088
12089   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12090     {
12091       is_inline = true;
12092       cp_lexer_consume_token (parser->lexer);
12093     }
12094   else
12095     is_inline = false;
12096
12097   /* Look for the `namespace' keyword.  */
12098   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12099
12100   /* Get the name of the namespace.  We do not attempt to distinguish
12101      between an original-namespace-definition and an
12102      extension-namespace-definition at this point.  The semantic
12103      analysis routines are responsible for that.  */
12104   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12105     identifier = cp_parser_identifier (parser);
12106   else
12107     identifier = NULL_TREE;
12108
12109   /* Parse any specified attributes.  */
12110   attribs = cp_parser_attributes_opt (parser);
12111
12112   /* Look for the `{' to start the namespace.  */
12113   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12114   /* Start the namespace.  */
12115   push_namespace (identifier);
12116
12117   /* "inline namespace" is equivalent to a stub namespace definition
12118      followed by a strong using directive.  */
12119   if (is_inline)
12120     {
12121       tree name_space = current_namespace;
12122       /* Set up namespace association.  */
12123       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12124         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12125                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12126       /* Import the contents of the inline namespace.  */
12127       pop_namespace ();
12128       do_using_directive (name_space);
12129       push_namespace (identifier);
12130     }
12131
12132   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12133
12134   /* Parse the body of the namespace.  */
12135   cp_parser_namespace_body (parser);
12136
12137 #ifdef HANDLE_PRAGMA_VISIBILITY
12138   if (has_visibility)
12139     pop_visibility ();
12140 #endif
12141
12142   /* Finish the namespace.  */
12143   pop_namespace ();
12144   /* Look for the final `}'.  */
12145   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12146 }
12147
12148 /* Parse a namespace-body.
12149
12150    namespace-body:
12151      declaration-seq [opt]  */
12152
12153 static void
12154 cp_parser_namespace_body (cp_parser* parser)
12155 {
12156   cp_parser_declaration_seq_opt (parser);
12157 }
12158
12159 /* Parse a namespace-alias-definition.
12160
12161    namespace-alias-definition:
12162      namespace identifier = qualified-namespace-specifier ;  */
12163
12164 static void
12165 cp_parser_namespace_alias_definition (cp_parser* parser)
12166 {
12167   tree identifier;
12168   tree namespace_specifier;
12169
12170   cp_token *token = cp_lexer_peek_token (parser->lexer);
12171
12172   /* Look for the `namespace' keyword.  */
12173   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12174   /* Look for the identifier.  */
12175   identifier = cp_parser_identifier (parser);
12176   if (identifier == error_mark_node)
12177     return;
12178   /* Look for the `=' token.  */
12179   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12180       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12181     {
12182       error ("%H%<namespace%> definition is not allowed here", &token->location);
12183       /* Skip the definition.  */
12184       cp_lexer_consume_token (parser->lexer);
12185       if (cp_parser_skip_to_closing_brace (parser))
12186         cp_lexer_consume_token (parser->lexer);
12187       return;
12188     }
12189   cp_parser_require (parser, CPP_EQ, "%<=%>");
12190   /* Look for the qualified-namespace-specifier.  */
12191   namespace_specifier
12192     = cp_parser_qualified_namespace_specifier (parser);
12193   /* Look for the `;' token.  */
12194   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12195
12196   /* Register the alias in the symbol table.  */
12197   do_namespace_alias (identifier, namespace_specifier);
12198 }
12199
12200 /* Parse a qualified-namespace-specifier.
12201
12202    qualified-namespace-specifier:
12203      :: [opt] nested-name-specifier [opt] namespace-name
12204
12205    Returns a NAMESPACE_DECL corresponding to the specified
12206    namespace.  */
12207
12208 static tree
12209 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12210 {
12211   /* Look for the optional `::'.  */
12212   cp_parser_global_scope_opt (parser,
12213                               /*current_scope_valid_p=*/false);
12214
12215   /* Look for the optional nested-name-specifier.  */
12216   cp_parser_nested_name_specifier_opt (parser,
12217                                        /*typename_keyword_p=*/false,
12218                                        /*check_dependency_p=*/true,
12219                                        /*type_p=*/false,
12220                                        /*is_declaration=*/true);
12221
12222   return cp_parser_namespace_name (parser);
12223 }
12224
12225 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12226    access declaration.
12227
12228    using-declaration:
12229      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12230      using :: unqualified-id ;  
12231
12232    access-declaration:
12233      qualified-id ;  
12234
12235    */
12236
12237 static bool
12238 cp_parser_using_declaration (cp_parser* parser, 
12239                              bool access_declaration_p)
12240 {
12241   cp_token *token;
12242   bool typename_p = false;
12243   bool global_scope_p;
12244   tree decl;
12245   tree identifier;
12246   tree qscope;
12247
12248   if (access_declaration_p)
12249     cp_parser_parse_tentatively (parser);
12250   else
12251     {
12252       /* Look for the `using' keyword.  */
12253       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12254       
12255       /* Peek at the next token.  */
12256       token = cp_lexer_peek_token (parser->lexer);
12257       /* See if it's `typename'.  */
12258       if (token->keyword == RID_TYPENAME)
12259         {
12260           /* Remember that we've seen it.  */
12261           typename_p = true;
12262           /* Consume the `typename' token.  */
12263           cp_lexer_consume_token (parser->lexer);
12264         }
12265     }
12266
12267   /* Look for the optional global scope qualification.  */
12268   global_scope_p
12269     = (cp_parser_global_scope_opt (parser,
12270                                    /*current_scope_valid_p=*/false)
12271        != NULL_TREE);
12272
12273   /* If we saw `typename', or didn't see `::', then there must be a
12274      nested-name-specifier present.  */
12275   if (typename_p || !global_scope_p)
12276     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12277                                               /*check_dependency_p=*/true,
12278                                               /*type_p=*/false,
12279                                               /*is_declaration=*/true);
12280   /* Otherwise, we could be in either of the two productions.  In that
12281      case, treat the nested-name-specifier as optional.  */
12282   else
12283     qscope = cp_parser_nested_name_specifier_opt (parser,
12284                                                   /*typename_keyword_p=*/false,
12285                                                   /*check_dependency_p=*/true,
12286                                                   /*type_p=*/false,
12287                                                   /*is_declaration=*/true);
12288   if (!qscope)
12289     qscope = global_namespace;
12290
12291   if (access_declaration_p && cp_parser_error_occurred (parser))
12292     /* Something has already gone wrong; there's no need to parse
12293        further.  Since an error has occurred, the return value of
12294        cp_parser_parse_definitely will be false, as required.  */
12295     return cp_parser_parse_definitely (parser);
12296
12297   token = cp_lexer_peek_token (parser->lexer);
12298   /* Parse the unqualified-id.  */
12299   identifier = cp_parser_unqualified_id (parser,
12300                                          /*template_keyword_p=*/false,
12301                                          /*check_dependency_p=*/true,
12302                                          /*declarator_p=*/true,
12303                                          /*optional_p=*/false);
12304
12305   if (access_declaration_p)
12306     {
12307       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12308         cp_parser_simulate_error (parser);
12309       if (!cp_parser_parse_definitely (parser))
12310         return false;
12311     }
12312
12313   /* The function we call to handle a using-declaration is different
12314      depending on what scope we are in.  */
12315   if (qscope == error_mark_node || identifier == error_mark_node)
12316     ;
12317   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12318            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12319     /* [namespace.udecl]
12320
12321        A using declaration shall not name a template-id.  */
12322     error ("%Ha template-id may not appear in a using-declaration",
12323             &token->location);
12324   else
12325     {
12326       if (at_class_scope_p ())
12327         {
12328           /* Create the USING_DECL.  */
12329           decl = do_class_using_decl (parser->scope, identifier);
12330
12331           if (check_for_bare_parameter_packs (decl))
12332             return false;
12333           else
12334             /* Add it to the list of members in this class.  */
12335             finish_member_declaration (decl);
12336         }
12337       else
12338         {
12339           decl = cp_parser_lookup_name_simple (parser,
12340                                                identifier,
12341                                                token->location);
12342           if (decl == error_mark_node)
12343             cp_parser_name_lookup_error (parser, identifier,
12344                                          decl, NULL,
12345                                          token->location);
12346           else if (check_for_bare_parameter_packs (decl))
12347             return false;
12348           else if (!at_namespace_scope_p ())
12349             do_local_using_decl (decl, qscope, identifier);
12350           else
12351             do_toplevel_using_decl (decl, qscope, identifier);
12352         }
12353     }
12354
12355   /* Look for the final `;'.  */
12356   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12357   
12358   return true;
12359 }
12360
12361 /* Parse a using-directive.
12362
12363    using-directive:
12364      using namespace :: [opt] nested-name-specifier [opt]
12365        namespace-name ;  */
12366
12367 static void
12368 cp_parser_using_directive (cp_parser* parser)
12369 {
12370   tree namespace_decl;
12371   tree attribs;
12372
12373   /* Look for the `using' keyword.  */
12374   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12375   /* And the `namespace' keyword.  */
12376   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12377   /* Look for the optional `::' operator.  */
12378   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12379   /* And the optional nested-name-specifier.  */
12380   cp_parser_nested_name_specifier_opt (parser,
12381                                        /*typename_keyword_p=*/false,
12382                                        /*check_dependency_p=*/true,
12383                                        /*type_p=*/false,
12384                                        /*is_declaration=*/true);
12385   /* Get the namespace being used.  */
12386   namespace_decl = cp_parser_namespace_name (parser);
12387   /* And any specified attributes.  */
12388   attribs = cp_parser_attributes_opt (parser);
12389   /* Update the symbol table.  */
12390   parse_using_directive (namespace_decl, attribs);
12391   /* Look for the final `;'.  */
12392   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12393 }
12394
12395 /* Parse an asm-definition.
12396
12397    asm-definition:
12398      asm ( string-literal ) ;
12399
12400    GNU Extension:
12401
12402    asm-definition:
12403      asm volatile [opt] ( string-literal ) ;
12404      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12405      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12406                           : asm-operand-list [opt] ) ;
12407      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12408                           : asm-operand-list [opt]
12409                           : asm-operand-list [opt] ) ;  */
12410
12411 static void
12412 cp_parser_asm_definition (cp_parser* parser)
12413 {
12414   tree string;
12415   tree outputs = NULL_TREE;
12416   tree inputs = NULL_TREE;
12417   tree clobbers = NULL_TREE;
12418   tree asm_stmt;
12419   bool volatile_p = false;
12420   bool extended_p = false;
12421   bool invalid_inputs_p = false;
12422   bool invalid_outputs_p = false;
12423
12424   /* Look for the `asm' keyword.  */
12425   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12426   /* See if the next token is `volatile'.  */
12427   if (cp_parser_allow_gnu_extensions_p (parser)
12428       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12429     {
12430       /* Remember that we saw the `volatile' keyword.  */
12431       volatile_p = true;
12432       /* Consume the token.  */
12433       cp_lexer_consume_token (parser->lexer);
12434     }
12435   /* Look for the opening `('.  */
12436   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12437     return;
12438   /* Look for the string.  */
12439   string = cp_parser_string_literal (parser, false, false);
12440   if (string == error_mark_node)
12441     {
12442       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12443                                              /*consume_paren=*/true);
12444       return;
12445     }
12446
12447   /* If we're allowing GNU extensions, check for the extended assembly
12448      syntax.  Unfortunately, the `:' tokens need not be separated by
12449      a space in C, and so, for compatibility, we tolerate that here
12450      too.  Doing that means that we have to treat the `::' operator as
12451      two `:' tokens.  */
12452   if (cp_parser_allow_gnu_extensions_p (parser)
12453       && parser->in_function_body
12454       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12455           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12456     {
12457       bool inputs_p = false;
12458       bool clobbers_p = false;
12459
12460       /* The extended syntax was used.  */
12461       extended_p = true;
12462
12463       /* Look for outputs.  */
12464       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12465         {
12466           /* Consume the `:'.  */
12467           cp_lexer_consume_token (parser->lexer);
12468           /* Parse the output-operands.  */
12469           if (cp_lexer_next_token_is_not (parser->lexer,
12470                                           CPP_COLON)
12471               && cp_lexer_next_token_is_not (parser->lexer,
12472                                              CPP_SCOPE)
12473               && cp_lexer_next_token_is_not (parser->lexer,
12474                                              CPP_CLOSE_PAREN))
12475             outputs = cp_parser_asm_operand_list (parser);
12476
12477             if (outputs == error_mark_node)
12478               invalid_outputs_p = true;
12479         }
12480       /* If the next token is `::', there are no outputs, and the
12481          next token is the beginning of the inputs.  */
12482       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12483         /* The inputs are coming next.  */
12484         inputs_p = true;
12485
12486       /* Look for inputs.  */
12487       if (inputs_p
12488           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12489         {
12490           /* Consume the `:' or `::'.  */
12491           cp_lexer_consume_token (parser->lexer);
12492           /* Parse the output-operands.  */
12493           if (cp_lexer_next_token_is_not (parser->lexer,
12494                                           CPP_COLON)
12495               && cp_lexer_next_token_is_not (parser->lexer,
12496                                              CPP_CLOSE_PAREN))
12497             inputs = cp_parser_asm_operand_list (parser);
12498
12499             if (inputs == error_mark_node)
12500               invalid_inputs_p = true;
12501         }
12502       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12503         /* The clobbers are coming next.  */
12504         clobbers_p = true;
12505
12506       /* Look for clobbers.  */
12507       if (clobbers_p
12508           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12509         {
12510           /* Consume the `:' or `::'.  */
12511           cp_lexer_consume_token (parser->lexer);
12512           /* Parse the clobbers.  */
12513           if (cp_lexer_next_token_is_not (parser->lexer,
12514                                           CPP_CLOSE_PAREN))
12515             clobbers = cp_parser_asm_clobber_list (parser);
12516         }
12517     }
12518   /* Look for the closing `)'.  */
12519   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12520     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12521                                            /*consume_paren=*/true);
12522   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12523
12524   if (!invalid_inputs_p && !invalid_outputs_p)
12525     {
12526       /* Create the ASM_EXPR.  */
12527       if (parser->in_function_body)
12528         {
12529           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12530                                       inputs, clobbers);
12531           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12532           if (!extended_p)
12533             {
12534               tree temp = asm_stmt;
12535               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12536                 temp = TREE_OPERAND (temp, 0);
12537
12538               ASM_INPUT_P (temp) = 1;
12539             }
12540         }
12541       else
12542         cgraph_add_asm_node (string);
12543     }
12544 }
12545
12546 /* Declarators [gram.dcl.decl] */
12547
12548 /* Parse an init-declarator.
12549
12550    init-declarator:
12551      declarator initializer [opt]
12552
12553    GNU Extension:
12554
12555    init-declarator:
12556      declarator asm-specification [opt] attributes [opt] initializer [opt]
12557
12558    function-definition:
12559      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12560        function-body
12561      decl-specifier-seq [opt] declarator function-try-block
12562
12563    GNU Extension:
12564
12565    function-definition:
12566      __extension__ function-definition
12567
12568    The DECL_SPECIFIERS apply to this declarator.  Returns a
12569    representation of the entity declared.  If MEMBER_P is TRUE, then
12570    this declarator appears in a class scope.  The new DECL created by
12571    this declarator is returned.
12572
12573    The CHECKS are access checks that should be performed once we know
12574    what entity is being declared (and, therefore, what classes have
12575    befriended it).
12576
12577    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12578    for a function-definition here as well.  If the declarator is a
12579    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12580    be TRUE upon return.  By that point, the function-definition will
12581    have been completely parsed.
12582
12583    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12584    is FALSE.  */
12585
12586 static tree
12587 cp_parser_init_declarator (cp_parser* parser,
12588                            cp_decl_specifier_seq *decl_specifiers,
12589                            VEC (deferred_access_check,gc)* checks,
12590                            bool function_definition_allowed_p,
12591                            bool member_p,
12592                            int declares_class_or_enum,
12593                            bool* function_definition_p)
12594 {
12595   cp_token *token = NULL, *asm_spec_start_token = NULL,
12596            *attributes_start_token = NULL;
12597   cp_declarator *declarator;
12598   tree prefix_attributes;
12599   tree attributes;
12600   tree asm_specification;
12601   tree initializer;
12602   tree decl = NULL_TREE;
12603   tree scope;
12604   int is_initialized;
12605   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12606      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12607      "(...)".  */
12608   enum cpp_ttype initialization_kind;
12609   bool is_direct_init = false;
12610   bool is_non_constant_init;
12611   int ctor_dtor_or_conv_p;
12612   bool friend_p;
12613   tree pushed_scope = NULL;
12614
12615   /* Gather the attributes that were provided with the
12616      decl-specifiers.  */
12617   prefix_attributes = decl_specifiers->attributes;
12618
12619   /* Assume that this is not the declarator for a function
12620      definition.  */
12621   if (function_definition_p)
12622     *function_definition_p = false;
12623
12624   /* Defer access checks while parsing the declarator; we cannot know
12625      what names are accessible until we know what is being
12626      declared.  */
12627   resume_deferring_access_checks ();
12628
12629   /* Parse the declarator.  */
12630   token = cp_lexer_peek_token (parser->lexer);
12631   declarator
12632     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12633                             &ctor_dtor_or_conv_p,
12634                             /*parenthesized_p=*/NULL,
12635                             /*member_p=*/false);
12636   /* Gather up the deferred checks.  */
12637   stop_deferring_access_checks ();
12638
12639   /* If the DECLARATOR was erroneous, there's no need to go
12640      further.  */
12641   if (declarator == cp_error_declarator)
12642     return error_mark_node;
12643
12644   /* Check that the number of template-parameter-lists is OK.  */
12645   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12646                                                        token->location))
12647     return error_mark_node;
12648
12649   if (declares_class_or_enum & 2)
12650     cp_parser_check_for_definition_in_return_type (declarator,
12651                                                    decl_specifiers->type,
12652                                                    decl_specifiers->type_location);
12653
12654   /* Figure out what scope the entity declared by the DECLARATOR is
12655      located in.  `grokdeclarator' sometimes changes the scope, so
12656      we compute it now.  */
12657   scope = get_scope_of_declarator (declarator);
12658
12659   /* If we're allowing GNU extensions, look for an asm-specification
12660      and attributes.  */
12661   if (cp_parser_allow_gnu_extensions_p (parser))
12662     {
12663       /* Look for an asm-specification.  */
12664       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12665       asm_specification = cp_parser_asm_specification_opt (parser);
12666       /* And attributes.  */
12667       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12668       attributes = cp_parser_attributes_opt (parser);
12669     }
12670   else
12671     {
12672       asm_specification = NULL_TREE;
12673       attributes = NULL_TREE;
12674     }
12675
12676   /* Peek at the next token.  */
12677   token = cp_lexer_peek_token (parser->lexer);
12678   /* Check to see if the token indicates the start of a
12679      function-definition.  */
12680   if (function_declarator_p (declarator)
12681       && cp_parser_token_starts_function_definition_p (token))
12682     {
12683       if (!function_definition_allowed_p)
12684         {
12685           /* If a function-definition should not appear here, issue an
12686              error message.  */
12687           cp_parser_error (parser,
12688                            "a function-definition is not allowed here");
12689           return error_mark_node;
12690         }
12691       else
12692         {
12693           location_t func_brace_location
12694             = cp_lexer_peek_token (parser->lexer)->location;
12695
12696           /* Neither attributes nor an asm-specification are allowed
12697              on a function-definition.  */
12698           if (asm_specification)
12699             error ("%Han asm-specification is not allowed "
12700                    "on a function-definition",
12701                    &asm_spec_start_token->location);
12702           if (attributes)
12703             error ("%Hattributes are not allowed on a function-definition",
12704                    &attributes_start_token->location);
12705           /* This is a function-definition.  */
12706           *function_definition_p = true;
12707
12708           /* Parse the function definition.  */
12709           if (member_p)
12710             decl = cp_parser_save_member_function_body (parser,
12711                                                         decl_specifiers,
12712                                                         declarator,
12713                                                         prefix_attributes);
12714           else
12715             decl
12716               = (cp_parser_function_definition_from_specifiers_and_declarator
12717                  (parser, decl_specifiers, prefix_attributes, declarator));
12718
12719           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12720             {
12721               /* This is where the prologue starts...  */
12722               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12723                 = func_brace_location;
12724             }
12725
12726           return decl;
12727         }
12728     }
12729
12730   /* [dcl.dcl]
12731
12732      Only in function declarations for constructors, destructors, and
12733      type conversions can the decl-specifier-seq be omitted.
12734
12735      We explicitly postpone this check past the point where we handle
12736      function-definitions because we tolerate function-definitions
12737      that are missing their return types in some modes.  */
12738   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12739     {
12740       cp_parser_error (parser,
12741                        "expected constructor, destructor, or type conversion");
12742       return error_mark_node;
12743     }
12744
12745   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12746   if (token->type == CPP_EQ
12747       || token->type == CPP_OPEN_PAREN
12748       || token->type == CPP_OPEN_BRACE)
12749     {
12750       is_initialized = SD_INITIALIZED;
12751       initialization_kind = token->type;
12752
12753       if (token->type == CPP_EQ
12754           && function_declarator_p (declarator))
12755         {
12756           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12757           if (t2->keyword == RID_DEFAULT)
12758             is_initialized = SD_DEFAULTED;
12759           else if (t2->keyword == RID_DELETE)
12760             is_initialized = SD_DELETED;
12761         }
12762     }
12763   else
12764     {
12765       /* If the init-declarator isn't initialized and isn't followed by a
12766          `,' or `;', it's not a valid init-declarator.  */
12767       if (token->type != CPP_COMMA
12768           && token->type != CPP_SEMICOLON)
12769         {
12770           cp_parser_error (parser, "expected initializer");
12771           return error_mark_node;
12772         }
12773       is_initialized = SD_UNINITIALIZED;
12774       initialization_kind = CPP_EOF;
12775     }
12776
12777   /* Because start_decl has side-effects, we should only call it if we
12778      know we're going ahead.  By this point, we know that we cannot
12779      possibly be looking at any other construct.  */
12780   cp_parser_commit_to_tentative_parse (parser);
12781
12782   /* If the decl specifiers were bad, issue an error now that we're
12783      sure this was intended to be a declarator.  Then continue
12784      declaring the variable(s), as int, to try to cut down on further
12785      errors.  */
12786   if (decl_specifiers->any_specifiers_p
12787       && decl_specifiers->type == error_mark_node)
12788     {
12789       cp_parser_error (parser, "invalid type in declaration");
12790       decl_specifiers->type = integer_type_node;
12791     }
12792
12793   /* Check to see whether or not this declaration is a friend.  */
12794   friend_p = cp_parser_friend_p (decl_specifiers);
12795
12796   /* Enter the newly declared entry in the symbol table.  If we're
12797      processing a declaration in a class-specifier, we wait until
12798      after processing the initializer.  */
12799   if (!member_p)
12800     {
12801       if (parser->in_unbraced_linkage_specification_p)
12802         decl_specifiers->storage_class = sc_extern;
12803       decl = start_decl (declarator, decl_specifiers,
12804                          is_initialized, attributes, prefix_attributes,
12805                          &pushed_scope);
12806     }
12807   else if (scope)
12808     /* Enter the SCOPE.  That way unqualified names appearing in the
12809        initializer will be looked up in SCOPE.  */
12810     pushed_scope = push_scope (scope);
12811
12812   /* Perform deferred access control checks, now that we know in which
12813      SCOPE the declared entity resides.  */
12814   if (!member_p && decl)
12815     {
12816       tree saved_current_function_decl = NULL_TREE;
12817
12818       /* If the entity being declared is a function, pretend that we
12819          are in its scope.  If it is a `friend', it may have access to
12820          things that would not otherwise be accessible.  */
12821       if (TREE_CODE (decl) == FUNCTION_DECL)
12822         {
12823           saved_current_function_decl = current_function_decl;
12824           current_function_decl = decl;
12825         }
12826
12827       /* Perform access checks for template parameters.  */
12828       cp_parser_perform_template_parameter_access_checks (checks);
12829
12830       /* Perform the access control checks for the declarator and the
12831          decl-specifiers.  */
12832       perform_deferred_access_checks ();
12833
12834       /* Restore the saved value.  */
12835       if (TREE_CODE (decl) == FUNCTION_DECL)
12836         current_function_decl = saved_current_function_decl;
12837     }
12838
12839   /* Parse the initializer.  */
12840   initializer = NULL_TREE;
12841   is_direct_init = false;
12842   is_non_constant_init = true;
12843   if (is_initialized)
12844     {
12845       if (function_declarator_p (declarator))
12846         {
12847           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12848            if (initialization_kind == CPP_EQ)
12849              initializer = cp_parser_pure_specifier (parser);
12850            else
12851              {
12852                /* If the declaration was erroneous, we don't really
12853                   know what the user intended, so just silently
12854                   consume the initializer.  */
12855                if (decl != error_mark_node)
12856                  error ("%Hinitializer provided for function",
12857                         &initializer_start_token->location);
12858                cp_parser_skip_to_closing_parenthesis (parser,
12859                                                       /*recovering=*/true,
12860                                                       /*or_comma=*/false,
12861                                                       /*consume_paren=*/true);
12862              }
12863         }
12864       else
12865         initializer = cp_parser_initializer (parser,
12866                                              &is_direct_init,
12867                                              &is_non_constant_init);
12868     }
12869
12870   /* The old parser allows attributes to appear after a parenthesized
12871      initializer.  Mark Mitchell proposed removing this functionality
12872      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12873      attributes -- but ignores them.  */
12874   if (cp_parser_allow_gnu_extensions_p (parser)
12875       && initialization_kind == CPP_OPEN_PAREN)
12876     if (cp_parser_attributes_opt (parser))
12877       warning (OPT_Wattributes,
12878                "attributes after parenthesized initializer ignored");
12879
12880   /* For an in-class declaration, use `grokfield' to create the
12881      declaration.  */
12882   if (member_p)
12883     {
12884       if (pushed_scope)
12885         {
12886           pop_scope (pushed_scope);
12887           pushed_scope = false;
12888         }
12889       decl = grokfield (declarator, decl_specifiers,
12890                         initializer, !is_non_constant_init,
12891                         /*asmspec=*/NULL_TREE,
12892                         prefix_attributes);
12893       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12894         cp_parser_save_default_args (parser, decl);
12895     }
12896
12897   /* Finish processing the declaration.  But, skip friend
12898      declarations.  */
12899   if (!friend_p && decl && decl != error_mark_node)
12900     {
12901       cp_finish_decl (decl,
12902                       initializer, !is_non_constant_init,
12903                       asm_specification,
12904                       /* If the initializer is in parentheses, then this is
12905                          a direct-initialization, which means that an
12906                          `explicit' constructor is OK.  Otherwise, an
12907                          `explicit' constructor cannot be used.  */
12908                       ((is_direct_init || !is_initialized)
12909                        ? 0 : LOOKUP_ONLYCONVERTING));
12910     }
12911   else if ((cxx_dialect != cxx98) && friend_p
12912            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12913     /* Core issue #226 (C++0x only): A default template-argument
12914        shall not be specified in a friend class template
12915        declaration. */
12916     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12917                              /*is_partial=*/0, /*is_friend_decl=*/1);
12918
12919   if (!friend_p && pushed_scope)
12920     pop_scope (pushed_scope);
12921
12922   return decl;
12923 }
12924
12925 /* Parse a declarator.
12926
12927    declarator:
12928      direct-declarator
12929      ptr-operator declarator
12930
12931    abstract-declarator:
12932      ptr-operator abstract-declarator [opt]
12933      direct-abstract-declarator
12934
12935    GNU Extensions:
12936
12937    declarator:
12938      attributes [opt] direct-declarator
12939      attributes [opt] ptr-operator declarator
12940
12941    abstract-declarator:
12942      attributes [opt] ptr-operator abstract-declarator [opt]
12943      attributes [opt] direct-abstract-declarator
12944
12945    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12946    detect constructor, destructor or conversion operators. It is set
12947    to -1 if the declarator is a name, and +1 if it is a
12948    function. Otherwise it is set to zero. Usually you just want to
12949    test for >0, but internally the negative value is used.
12950
12951    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12952    a decl-specifier-seq unless it declares a constructor, destructor,
12953    or conversion.  It might seem that we could check this condition in
12954    semantic analysis, rather than parsing, but that makes it difficult
12955    to handle something like `f()'.  We want to notice that there are
12956    no decl-specifiers, and therefore realize that this is an
12957    expression, not a declaration.)
12958
12959    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12960    the declarator is a direct-declarator of the form "(...)".
12961
12962    MEMBER_P is true iff this declarator is a member-declarator.  */
12963
12964 static cp_declarator *
12965 cp_parser_declarator (cp_parser* parser,
12966                       cp_parser_declarator_kind dcl_kind,
12967                       int* ctor_dtor_or_conv_p,
12968                       bool* parenthesized_p,
12969                       bool member_p)
12970 {
12971   cp_token *token;
12972   cp_declarator *declarator;
12973   enum tree_code code;
12974   cp_cv_quals cv_quals;
12975   tree class_type;
12976   tree attributes = NULL_TREE;
12977
12978   /* Assume this is not a constructor, destructor, or type-conversion
12979      operator.  */
12980   if (ctor_dtor_or_conv_p)
12981     *ctor_dtor_or_conv_p = 0;
12982
12983   if (cp_parser_allow_gnu_extensions_p (parser))
12984     attributes = cp_parser_attributes_opt (parser);
12985
12986   /* Peek at the next token.  */
12987   token = cp_lexer_peek_token (parser->lexer);
12988
12989   /* Check for the ptr-operator production.  */
12990   cp_parser_parse_tentatively (parser);
12991   /* Parse the ptr-operator.  */
12992   code = cp_parser_ptr_operator (parser,
12993                                  &class_type,
12994                                  &cv_quals);
12995   /* If that worked, then we have a ptr-operator.  */
12996   if (cp_parser_parse_definitely (parser))
12997     {
12998       /* If a ptr-operator was found, then this declarator was not
12999          parenthesized.  */
13000       if (parenthesized_p)
13001         *parenthesized_p = true;
13002       /* The dependent declarator is optional if we are parsing an
13003          abstract-declarator.  */
13004       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13005         cp_parser_parse_tentatively (parser);
13006
13007       /* Parse the dependent declarator.  */
13008       declarator = cp_parser_declarator (parser, dcl_kind,
13009                                          /*ctor_dtor_or_conv_p=*/NULL,
13010                                          /*parenthesized_p=*/NULL,
13011                                          /*member_p=*/false);
13012
13013       /* If we are parsing an abstract-declarator, we must handle the
13014          case where the dependent declarator is absent.  */
13015       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13016           && !cp_parser_parse_definitely (parser))
13017         declarator = NULL;
13018
13019       declarator = cp_parser_make_indirect_declarator
13020         (code, class_type, cv_quals, declarator);
13021     }
13022   /* Everything else is a direct-declarator.  */
13023   else
13024     {
13025       if (parenthesized_p)
13026         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13027                                                    CPP_OPEN_PAREN);
13028       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13029                                                 ctor_dtor_or_conv_p,
13030                                                 member_p);
13031     }
13032
13033   if (attributes && declarator && declarator != cp_error_declarator)
13034     declarator->attributes = attributes;
13035
13036   return declarator;
13037 }
13038
13039 /* Parse a direct-declarator or direct-abstract-declarator.
13040
13041    direct-declarator:
13042      declarator-id
13043      direct-declarator ( parameter-declaration-clause )
13044        cv-qualifier-seq [opt]
13045        exception-specification [opt]
13046      direct-declarator [ constant-expression [opt] ]
13047      ( declarator )
13048
13049    direct-abstract-declarator:
13050      direct-abstract-declarator [opt]
13051        ( parameter-declaration-clause )
13052        cv-qualifier-seq [opt]
13053        exception-specification [opt]
13054      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13055      ( abstract-declarator )
13056
13057    Returns a representation of the declarator.  DCL_KIND is
13058    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13059    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13060    we are parsing a direct-declarator.  It is
13061    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13062    of ambiguity we prefer an abstract declarator, as per
13063    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13064    cp_parser_declarator.  */
13065
13066 static cp_declarator *
13067 cp_parser_direct_declarator (cp_parser* parser,
13068                              cp_parser_declarator_kind dcl_kind,
13069                              int* ctor_dtor_or_conv_p,
13070                              bool member_p)
13071 {
13072   cp_token *token;
13073   cp_declarator *declarator = NULL;
13074   tree scope = NULL_TREE;
13075   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13076   bool saved_in_declarator_p = parser->in_declarator_p;
13077   bool first = true;
13078   tree pushed_scope = NULL_TREE;
13079
13080   while (true)
13081     {
13082       /* Peek at the next token.  */
13083       token = cp_lexer_peek_token (parser->lexer);
13084       if (token->type == CPP_OPEN_PAREN)
13085         {
13086           /* This is either a parameter-declaration-clause, or a
13087              parenthesized declarator. When we know we are parsing a
13088              named declarator, it must be a parenthesized declarator
13089              if FIRST is true. For instance, `(int)' is a
13090              parameter-declaration-clause, with an omitted
13091              direct-abstract-declarator. But `((*))', is a
13092              parenthesized abstract declarator. Finally, when T is a
13093              template parameter `(T)' is a
13094              parameter-declaration-clause, and not a parenthesized
13095              named declarator.
13096
13097              We first try and parse a parameter-declaration-clause,
13098              and then try a nested declarator (if FIRST is true).
13099
13100              It is not an error for it not to be a
13101              parameter-declaration-clause, even when FIRST is
13102              false. Consider,
13103
13104                int i (int);
13105                int i (3);
13106
13107              The first is the declaration of a function while the
13108              second is the definition of a variable, including its
13109              initializer.
13110
13111              Having seen only the parenthesis, we cannot know which of
13112              these two alternatives should be selected.  Even more
13113              complex are examples like:
13114
13115                int i (int (a));
13116                int i (int (3));
13117
13118              The former is a function-declaration; the latter is a
13119              variable initialization.
13120
13121              Thus again, we try a parameter-declaration-clause, and if
13122              that fails, we back out and return.  */
13123
13124           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13125             {
13126               tree params;
13127               unsigned saved_num_template_parameter_lists;
13128               bool is_declarator = false;
13129               tree t;
13130
13131               /* In a member-declarator, the only valid interpretation
13132                  of a parenthesis is the start of a
13133                  parameter-declaration-clause.  (It is invalid to
13134                  initialize a static data member with a parenthesized
13135                  initializer; only the "=" form of initialization is
13136                  permitted.)  */
13137               if (!member_p)
13138                 cp_parser_parse_tentatively (parser);
13139
13140               /* Consume the `('.  */
13141               cp_lexer_consume_token (parser->lexer);
13142               if (first)
13143                 {
13144                   /* If this is going to be an abstract declarator, we're
13145                      in a declarator and we can't have default args.  */
13146                   parser->default_arg_ok_p = false;
13147                   parser->in_declarator_p = true;
13148                 }
13149
13150               /* Inside the function parameter list, surrounding
13151                  template-parameter-lists do not apply.  */
13152               saved_num_template_parameter_lists
13153                 = parser->num_template_parameter_lists;
13154               parser->num_template_parameter_lists = 0;
13155
13156               begin_scope (sk_function_parms, NULL_TREE);
13157
13158               /* Parse the parameter-declaration-clause.  */
13159               params = cp_parser_parameter_declaration_clause (parser);
13160
13161               parser->num_template_parameter_lists
13162                 = saved_num_template_parameter_lists;
13163
13164               /* If all went well, parse the cv-qualifier-seq and the
13165                  exception-specification.  */
13166               if (member_p || cp_parser_parse_definitely (parser))
13167                 {
13168                   cp_cv_quals cv_quals;
13169                   tree exception_specification;
13170                   tree late_return;
13171
13172                   is_declarator = true;
13173
13174                   if (ctor_dtor_or_conv_p)
13175                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13176                   first = false;
13177                   /* Consume the `)'.  */
13178                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13179
13180                   /* Parse the cv-qualifier-seq.  */
13181                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13182                   /* And the exception-specification.  */
13183                   exception_specification
13184                     = cp_parser_exception_specification_opt (parser);
13185
13186                   late_return
13187                     = cp_parser_late_return_type_opt (parser);
13188
13189                   /* Create the function-declarator.  */
13190                   declarator = make_call_declarator (declarator,
13191                                                      params,
13192                                                      cv_quals,
13193                                                      exception_specification,
13194                                                      late_return);
13195                   /* Any subsequent parameter lists are to do with
13196                      return type, so are not those of the declared
13197                      function.  */
13198                   parser->default_arg_ok_p = false;
13199                 }
13200
13201               /* Remove the function parms from scope.  */
13202               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13203                 pop_binding (DECL_NAME (t), t);
13204               leave_scope();
13205
13206               if (is_declarator)
13207                 /* Repeat the main loop.  */
13208                 continue;
13209             }
13210
13211           /* If this is the first, we can try a parenthesized
13212              declarator.  */
13213           if (first)
13214             {
13215               bool saved_in_type_id_in_expr_p;
13216
13217               parser->default_arg_ok_p = saved_default_arg_ok_p;
13218               parser->in_declarator_p = saved_in_declarator_p;
13219
13220               /* Consume the `('.  */
13221               cp_lexer_consume_token (parser->lexer);
13222               /* Parse the nested declarator.  */
13223               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13224               parser->in_type_id_in_expr_p = true;
13225               declarator
13226                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13227                                         /*parenthesized_p=*/NULL,
13228                                         member_p);
13229               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13230               first = false;
13231               /* Expect a `)'.  */
13232               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13233                 declarator = cp_error_declarator;
13234               if (declarator == cp_error_declarator)
13235                 break;
13236
13237               goto handle_declarator;
13238             }
13239           /* Otherwise, we must be done.  */
13240           else
13241             break;
13242         }
13243       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13244                && token->type == CPP_OPEN_SQUARE)
13245         {
13246           /* Parse an array-declarator.  */
13247           tree bounds;
13248
13249           if (ctor_dtor_or_conv_p)
13250             *ctor_dtor_or_conv_p = 0;
13251
13252           first = false;
13253           parser->default_arg_ok_p = false;
13254           parser->in_declarator_p = true;
13255           /* Consume the `['.  */
13256           cp_lexer_consume_token (parser->lexer);
13257           /* Peek at the next token.  */
13258           token = cp_lexer_peek_token (parser->lexer);
13259           /* If the next token is `]', then there is no
13260              constant-expression.  */
13261           if (token->type != CPP_CLOSE_SQUARE)
13262             {
13263               bool non_constant_p;
13264
13265               bounds
13266                 = cp_parser_constant_expression (parser,
13267                                                  /*allow_non_constant=*/true,
13268                                                  &non_constant_p);
13269               if (!non_constant_p)
13270                 bounds = fold_non_dependent_expr (bounds);
13271               /* Normally, the array bound must be an integral constant
13272                  expression.  However, as an extension, we allow VLAs
13273                  in function scopes.  */
13274               else if (!parser->in_function_body)
13275                 {
13276                   error ("%Harray bound is not an integer constant",
13277                          &token->location);
13278                   bounds = error_mark_node;
13279                 }
13280             }
13281           else
13282             bounds = NULL_TREE;
13283           /* Look for the closing `]'.  */
13284           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13285             {
13286               declarator = cp_error_declarator;
13287               break;
13288             }
13289
13290           declarator = make_array_declarator (declarator, bounds);
13291         }
13292       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13293         {
13294           tree qualifying_scope;
13295           tree unqualified_name;
13296           special_function_kind sfk;
13297           bool abstract_ok;
13298           bool pack_expansion_p = false;
13299           cp_token *declarator_id_start_token;
13300
13301           /* Parse a declarator-id */
13302           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13303           if (abstract_ok)
13304             {
13305               cp_parser_parse_tentatively (parser);
13306
13307               /* If we see an ellipsis, we should be looking at a
13308                  parameter pack. */
13309               if (token->type == CPP_ELLIPSIS)
13310                 {
13311                   /* Consume the `...' */
13312                   cp_lexer_consume_token (parser->lexer);
13313
13314                   pack_expansion_p = true;
13315                 }
13316             }
13317
13318           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13319           unqualified_name
13320             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13321           qualifying_scope = parser->scope;
13322           if (abstract_ok)
13323             {
13324               bool okay = false;
13325
13326               if (!unqualified_name && pack_expansion_p)
13327                 {
13328                   /* Check whether an error occurred. */
13329                   okay = !cp_parser_error_occurred (parser);
13330
13331                   /* We already consumed the ellipsis to mark a
13332                      parameter pack, but we have no way to report it,
13333                      so abort the tentative parse. We will be exiting
13334                      immediately anyway. */
13335                   cp_parser_abort_tentative_parse (parser);
13336                 }
13337               else
13338                 okay = cp_parser_parse_definitely (parser);
13339
13340               if (!okay)
13341                 unqualified_name = error_mark_node;
13342               else if (unqualified_name
13343                        && (qualifying_scope
13344                            || (TREE_CODE (unqualified_name)
13345                                != IDENTIFIER_NODE)))
13346                 {
13347                   cp_parser_error (parser, "expected unqualified-id");
13348                   unqualified_name = error_mark_node;
13349                 }
13350             }
13351
13352           if (!unqualified_name)
13353             return NULL;
13354           if (unqualified_name == error_mark_node)
13355             {
13356               declarator = cp_error_declarator;
13357               pack_expansion_p = false;
13358               declarator->parameter_pack_p = false;
13359               break;
13360             }
13361
13362           if (qualifying_scope && at_namespace_scope_p ()
13363               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13364             {
13365               /* In the declaration of a member of a template class
13366                  outside of the class itself, the SCOPE will sometimes
13367                  be a TYPENAME_TYPE.  For example, given:
13368
13369                  template <typename T>
13370                  int S<T>::R::i = 3;
13371
13372                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13373                  this context, we must resolve S<T>::R to an ordinary
13374                  type, rather than a typename type.
13375
13376                  The reason we normally avoid resolving TYPENAME_TYPEs
13377                  is that a specialization of `S' might render
13378                  `S<T>::R' not a type.  However, if `S' is
13379                  specialized, then this `i' will not be used, so there
13380                  is no harm in resolving the types here.  */
13381               tree type;
13382
13383               /* Resolve the TYPENAME_TYPE.  */
13384               type = resolve_typename_type (qualifying_scope,
13385                                             /*only_current_p=*/false);
13386               /* If that failed, the declarator is invalid.  */
13387               if (TREE_CODE (type) == TYPENAME_TYPE)
13388                 error ("%H%<%T::%E%> is not a type",
13389                        &declarator_id_start_token->location,
13390                        TYPE_CONTEXT (qualifying_scope),
13391                        TYPE_IDENTIFIER (qualifying_scope));
13392               qualifying_scope = type;
13393             }
13394
13395           sfk = sfk_none;
13396
13397           if (unqualified_name)
13398             {
13399               tree class_type;
13400
13401               if (qualifying_scope
13402                   && CLASS_TYPE_P (qualifying_scope))
13403                 class_type = qualifying_scope;
13404               else
13405                 class_type = current_class_type;
13406
13407               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13408                 {
13409                   tree name_type = TREE_TYPE (unqualified_name);
13410                   if (class_type && same_type_p (name_type, class_type))
13411                     {
13412                       if (qualifying_scope
13413                           && CLASSTYPE_USE_TEMPLATE (name_type))
13414                         {
13415                           error ("%Hinvalid use of constructor as a template",
13416                                  &declarator_id_start_token->location);
13417                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13418                                   "name the constructor in a qualified name",
13419                                   class_type,
13420                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13421                                   class_type, name_type);
13422                           declarator = cp_error_declarator;
13423                           break;
13424                         }
13425                       else
13426                         unqualified_name = constructor_name (class_type);
13427                     }
13428                   else
13429                     {
13430                       /* We do not attempt to print the declarator
13431                          here because we do not have enough
13432                          information about its original syntactic
13433                          form.  */
13434                       cp_parser_error (parser, "invalid declarator");
13435                       declarator = cp_error_declarator;
13436                       break;
13437                     }
13438                 }
13439
13440               if (class_type)
13441                 {
13442                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13443                     sfk = sfk_destructor;
13444                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13445                     sfk = sfk_conversion;
13446                   else if (/* There's no way to declare a constructor
13447                               for an anonymous type, even if the type
13448                               got a name for linkage purposes.  */
13449                            !TYPE_WAS_ANONYMOUS (class_type)
13450                            && constructor_name_p (unqualified_name,
13451                                                   class_type))
13452                     {
13453                       unqualified_name = constructor_name (class_type);
13454                       sfk = sfk_constructor;
13455                     }
13456
13457                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13458                     *ctor_dtor_or_conv_p = -1;
13459                 }
13460             }
13461           declarator = make_id_declarator (qualifying_scope,
13462                                            unqualified_name,
13463                                            sfk);
13464           declarator->id_loc = token->location;
13465           declarator->parameter_pack_p = pack_expansion_p;
13466
13467           if (pack_expansion_p)
13468             maybe_warn_variadic_templates ();
13469
13470         handle_declarator:;
13471           scope = get_scope_of_declarator (declarator);
13472           if (scope)
13473             /* Any names that appear after the declarator-id for a
13474                member are looked up in the containing scope.  */
13475             pushed_scope = push_scope (scope);
13476           parser->in_declarator_p = true;
13477           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13478               || (declarator && declarator->kind == cdk_id))
13479             /* Default args are only allowed on function
13480                declarations.  */
13481             parser->default_arg_ok_p = saved_default_arg_ok_p;
13482           else
13483             parser->default_arg_ok_p = false;
13484
13485           first = false;
13486         }
13487       /* We're done.  */
13488       else
13489         break;
13490     }
13491
13492   /* For an abstract declarator, we might wind up with nothing at this
13493      point.  That's an error; the declarator is not optional.  */
13494   if (!declarator)
13495     cp_parser_error (parser, "expected declarator");
13496
13497   /* If we entered a scope, we must exit it now.  */
13498   if (pushed_scope)
13499     pop_scope (pushed_scope);
13500
13501   parser->default_arg_ok_p = saved_default_arg_ok_p;
13502   parser->in_declarator_p = saved_in_declarator_p;
13503
13504   return declarator;
13505 }
13506
13507 /* Parse a ptr-operator.
13508
13509    ptr-operator:
13510      * cv-qualifier-seq [opt]
13511      &
13512      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13513
13514    GNU Extension:
13515
13516    ptr-operator:
13517      & cv-qualifier-seq [opt]
13518
13519    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13520    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13521    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13522    filled in with the TYPE containing the member.  *CV_QUALS is
13523    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13524    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13525    Note that the tree codes returned by this function have nothing
13526    to do with the types of trees that will be eventually be created
13527    to represent the pointer or reference type being parsed. They are
13528    just constants with suggestive names. */
13529 static enum tree_code
13530 cp_parser_ptr_operator (cp_parser* parser,
13531                         tree* type,
13532                         cp_cv_quals *cv_quals)
13533 {
13534   enum tree_code code = ERROR_MARK;
13535   cp_token *token;
13536
13537   /* Assume that it's not a pointer-to-member.  */
13538   *type = NULL_TREE;
13539   /* And that there are no cv-qualifiers.  */
13540   *cv_quals = TYPE_UNQUALIFIED;
13541
13542   /* Peek at the next token.  */
13543   token = cp_lexer_peek_token (parser->lexer);
13544
13545   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13546   if (token->type == CPP_MULT)
13547     code = INDIRECT_REF;
13548   else if (token->type == CPP_AND)
13549     code = ADDR_EXPR;
13550   else if ((cxx_dialect != cxx98) &&
13551            token->type == CPP_AND_AND) /* C++0x only */
13552     code = NON_LVALUE_EXPR;
13553
13554   if (code != ERROR_MARK)
13555     {
13556       /* Consume the `*', `&' or `&&'.  */
13557       cp_lexer_consume_token (parser->lexer);
13558
13559       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13560          `&', if we are allowing GNU extensions.  (The only qualifier
13561          that can legally appear after `&' is `restrict', but that is
13562          enforced during semantic analysis.  */
13563       if (code == INDIRECT_REF
13564           || cp_parser_allow_gnu_extensions_p (parser))
13565         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13566     }
13567   else
13568     {
13569       /* Try the pointer-to-member case.  */
13570       cp_parser_parse_tentatively (parser);
13571       /* Look for the optional `::' operator.  */
13572       cp_parser_global_scope_opt (parser,
13573                                   /*current_scope_valid_p=*/false);
13574       /* Look for the nested-name specifier.  */
13575       token = cp_lexer_peek_token (parser->lexer);
13576       cp_parser_nested_name_specifier (parser,
13577                                        /*typename_keyword_p=*/false,
13578                                        /*check_dependency_p=*/true,
13579                                        /*type_p=*/false,
13580                                        /*is_declaration=*/false);
13581       /* If we found it, and the next token is a `*', then we are
13582          indeed looking at a pointer-to-member operator.  */
13583       if (!cp_parser_error_occurred (parser)
13584           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13585         {
13586           /* Indicate that the `*' operator was used.  */
13587           code = INDIRECT_REF;
13588
13589           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13590             error ("%H%qD is a namespace", &token->location, parser->scope);
13591           else
13592             {
13593               /* The type of which the member is a member is given by the
13594                  current SCOPE.  */
13595               *type = parser->scope;
13596               /* The next name will not be qualified.  */
13597               parser->scope = NULL_TREE;
13598               parser->qualifying_scope = NULL_TREE;
13599               parser->object_scope = NULL_TREE;
13600               /* Look for the optional cv-qualifier-seq.  */
13601               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13602             }
13603         }
13604       /* If that didn't work we don't have a ptr-operator.  */
13605       if (!cp_parser_parse_definitely (parser))
13606         cp_parser_error (parser, "expected ptr-operator");
13607     }
13608
13609   return code;
13610 }
13611
13612 /* Parse an (optional) cv-qualifier-seq.
13613
13614    cv-qualifier-seq:
13615      cv-qualifier cv-qualifier-seq [opt]
13616
13617    cv-qualifier:
13618      const
13619      volatile
13620
13621    GNU Extension:
13622
13623    cv-qualifier:
13624      __restrict__
13625
13626    Returns a bitmask representing the cv-qualifiers.  */
13627
13628 static cp_cv_quals
13629 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13630 {
13631   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13632
13633   while (true)
13634     {
13635       cp_token *token;
13636       cp_cv_quals cv_qualifier;
13637
13638       /* Peek at the next token.  */
13639       token = cp_lexer_peek_token (parser->lexer);
13640       /* See if it's a cv-qualifier.  */
13641       switch (token->keyword)
13642         {
13643         case RID_CONST:
13644           cv_qualifier = TYPE_QUAL_CONST;
13645           break;
13646
13647         case RID_VOLATILE:
13648           cv_qualifier = TYPE_QUAL_VOLATILE;
13649           break;
13650
13651         case RID_RESTRICT:
13652           cv_qualifier = TYPE_QUAL_RESTRICT;
13653           break;
13654
13655         default:
13656           cv_qualifier = TYPE_UNQUALIFIED;
13657           break;
13658         }
13659
13660       if (!cv_qualifier)
13661         break;
13662
13663       if (cv_quals & cv_qualifier)
13664         {
13665           error ("%Hduplicate cv-qualifier", &token->location);
13666           cp_lexer_purge_token (parser->lexer);
13667         }
13668       else
13669         {
13670           cp_lexer_consume_token (parser->lexer);
13671           cv_quals |= cv_qualifier;
13672         }
13673     }
13674
13675   return cv_quals;
13676 }
13677
13678 /* Parse a late-specified return type, if any.  This is not a separate
13679    non-terminal, but part of a function declarator, which looks like
13680
13681    -> type-id
13682
13683    Returns the type indicated by the type-id.  */
13684
13685 static tree
13686 cp_parser_late_return_type_opt (cp_parser* parser)
13687 {
13688   cp_token *token;
13689
13690   /* Peek at the next token.  */
13691   token = cp_lexer_peek_token (parser->lexer);
13692   /* A late-specified return type is indicated by an initial '->'. */
13693   if (token->type != CPP_DEREF)
13694     return NULL_TREE;
13695
13696   /* Consume the ->.  */
13697   cp_lexer_consume_token (parser->lexer);
13698
13699   return cp_parser_type_id (parser);
13700 }
13701
13702 /* Parse a declarator-id.
13703
13704    declarator-id:
13705      id-expression
13706      :: [opt] nested-name-specifier [opt] type-name
13707
13708    In the `id-expression' case, the value returned is as for
13709    cp_parser_id_expression if the id-expression was an unqualified-id.
13710    If the id-expression was a qualified-id, then a SCOPE_REF is
13711    returned.  The first operand is the scope (either a NAMESPACE_DECL
13712    or TREE_TYPE), but the second is still just a representation of an
13713    unqualified-id.  */
13714
13715 static tree
13716 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13717 {
13718   tree id;
13719   /* The expression must be an id-expression.  Assume that qualified
13720      names are the names of types so that:
13721
13722        template <class T>
13723        int S<T>::R::i = 3;
13724
13725      will work; we must treat `S<T>::R' as the name of a type.
13726      Similarly, assume that qualified names are templates, where
13727      required, so that:
13728
13729        template <class T>
13730        int S<T>::R<T>::i = 3;
13731
13732      will work, too.  */
13733   id = cp_parser_id_expression (parser,
13734                                 /*template_keyword_p=*/false,
13735                                 /*check_dependency_p=*/false,
13736                                 /*template_p=*/NULL,
13737                                 /*declarator_p=*/true,
13738                                 optional_p);
13739   if (id && BASELINK_P (id))
13740     id = BASELINK_FUNCTIONS (id);
13741   return id;
13742 }
13743
13744 /* Parse a type-id.
13745
13746    type-id:
13747      type-specifier-seq abstract-declarator [opt]
13748
13749    Returns the TYPE specified.  */
13750
13751 static tree
13752 cp_parser_type_id (cp_parser* parser)
13753 {
13754   cp_decl_specifier_seq type_specifier_seq;
13755   cp_declarator *abstract_declarator;
13756
13757   /* Parse the type-specifier-seq.  */
13758   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13759                                 &type_specifier_seq);
13760   if (type_specifier_seq.type == error_mark_node)
13761     return error_mark_node;
13762
13763   /* There might or might not be an abstract declarator.  */
13764   cp_parser_parse_tentatively (parser);
13765   /* Look for the declarator.  */
13766   abstract_declarator
13767     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13768                             /*parenthesized_p=*/NULL,
13769                             /*member_p=*/false);
13770   /* Check to see if there really was a declarator.  */
13771   if (!cp_parser_parse_definitely (parser))
13772     abstract_declarator = NULL;
13773
13774   if (type_specifier_seq.type
13775       && type_uses_auto (type_specifier_seq.type))
13776     {
13777       error ("invalid use of %<auto%>");
13778       return error_mark_node;
13779     }
13780   
13781   return groktypename (&type_specifier_seq, abstract_declarator);
13782 }
13783
13784 /* Parse a type-specifier-seq.
13785
13786    type-specifier-seq:
13787      type-specifier type-specifier-seq [opt]
13788
13789    GNU extension:
13790
13791    type-specifier-seq:
13792      attributes type-specifier-seq [opt]
13793
13794    If IS_CONDITION is true, we are at the start of a "condition",
13795    e.g., we've just seen "if (".
13796
13797    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13798
13799 static void
13800 cp_parser_type_specifier_seq (cp_parser* parser,
13801                               bool is_condition,
13802                               cp_decl_specifier_seq *type_specifier_seq)
13803 {
13804   bool seen_type_specifier = false;
13805   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13806   cp_token *start_token = NULL;
13807
13808   /* Clear the TYPE_SPECIFIER_SEQ.  */
13809   clear_decl_specs (type_specifier_seq);
13810
13811   /* Parse the type-specifiers and attributes.  */
13812   while (true)
13813     {
13814       tree type_specifier;
13815       bool is_cv_qualifier;
13816
13817       /* Check for attributes first.  */
13818       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13819         {
13820           type_specifier_seq->attributes =
13821             chainon (type_specifier_seq->attributes,
13822                      cp_parser_attributes_opt (parser));
13823           continue;
13824         }
13825
13826       /* record the token of the beginning of the type specifier seq,
13827          for error reporting purposes*/
13828      if (!start_token)
13829        start_token = cp_lexer_peek_token (parser->lexer);
13830
13831       /* Look for the type-specifier.  */
13832       type_specifier = cp_parser_type_specifier (parser,
13833                                                  flags,
13834                                                  type_specifier_seq,
13835                                                  /*is_declaration=*/false,
13836                                                  NULL,
13837                                                  &is_cv_qualifier);
13838       if (!type_specifier)
13839         {
13840           /* If the first type-specifier could not be found, this is not a
13841              type-specifier-seq at all.  */
13842           if (!seen_type_specifier)
13843             {
13844               cp_parser_error (parser, "expected type-specifier");
13845               type_specifier_seq->type = error_mark_node;
13846               return;
13847             }
13848           /* If subsequent type-specifiers could not be found, the
13849              type-specifier-seq is complete.  */
13850           break;
13851         }
13852
13853       seen_type_specifier = true;
13854       /* The standard says that a condition can be:
13855
13856             type-specifier-seq declarator = assignment-expression
13857
13858          However, given:
13859
13860            struct S {};
13861            if (int S = ...)
13862
13863          we should treat the "S" as a declarator, not as a
13864          type-specifier.  The standard doesn't say that explicitly for
13865          type-specifier-seq, but it does say that for
13866          decl-specifier-seq in an ordinary declaration.  Perhaps it
13867          would be clearer just to allow a decl-specifier-seq here, and
13868          then add a semantic restriction that if any decl-specifiers
13869          that are not type-specifiers appear, the program is invalid.  */
13870       if (is_condition && !is_cv_qualifier)
13871         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13872     }
13873
13874   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13875 }
13876
13877 /* Parse a parameter-declaration-clause.
13878
13879    parameter-declaration-clause:
13880      parameter-declaration-list [opt] ... [opt]
13881      parameter-declaration-list , ...
13882
13883    Returns a representation for the parameter declarations.  A return
13884    value of NULL indicates a parameter-declaration-clause consisting
13885    only of an ellipsis.  */
13886
13887 static tree
13888 cp_parser_parameter_declaration_clause (cp_parser* parser)
13889 {
13890   tree parameters;
13891   cp_token *token;
13892   bool ellipsis_p;
13893   bool is_error;
13894
13895   /* Peek at the next token.  */
13896   token = cp_lexer_peek_token (parser->lexer);
13897   /* Check for trivial parameter-declaration-clauses.  */
13898   if (token->type == CPP_ELLIPSIS)
13899     {
13900       /* Consume the `...' token.  */
13901       cp_lexer_consume_token (parser->lexer);
13902       return NULL_TREE;
13903     }
13904   else if (token->type == CPP_CLOSE_PAREN)
13905     /* There are no parameters.  */
13906     {
13907 #ifndef NO_IMPLICIT_EXTERN_C
13908       if (in_system_header && current_class_type == NULL
13909           && current_lang_name == lang_name_c)
13910         return NULL_TREE;
13911       else
13912 #endif
13913         return void_list_node;
13914     }
13915   /* Check for `(void)', too, which is a special case.  */
13916   else if (token->keyword == RID_VOID
13917            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13918                == CPP_CLOSE_PAREN))
13919     {
13920       /* Consume the `void' token.  */
13921       cp_lexer_consume_token (parser->lexer);
13922       /* There are no parameters.  */
13923       return void_list_node;
13924     }
13925
13926   /* Parse the parameter-declaration-list.  */
13927   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13928   /* If a parse error occurred while parsing the
13929      parameter-declaration-list, then the entire
13930      parameter-declaration-clause is erroneous.  */
13931   if (is_error)
13932     return NULL;
13933
13934   /* Peek at the next token.  */
13935   token = cp_lexer_peek_token (parser->lexer);
13936   /* If it's a `,', the clause should terminate with an ellipsis.  */
13937   if (token->type == CPP_COMMA)
13938     {
13939       /* Consume the `,'.  */
13940       cp_lexer_consume_token (parser->lexer);
13941       /* Expect an ellipsis.  */
13942       ellipsis_p
13943         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13944     }
13945   /* It might also be `...' if the optional trailing `,' was
13946      omitted.  */
13947   else if (token->type == CPP_ELLIPSIS)
13948     {
13949       /* Consume the `...' token.  */
13950       cp_lexer_consume_token (parser->lexer);
13951       /* And remember that we saw it.  */
13952       ellipsis_p = true;
13953     }
13954   else
13955     ellipsis_p = false;
13956
13957   /* Finish the parameter list.  */
13958   if (!ellipsis_p)
13959     parameters = chainon (parameters, void_list_node);
13960
13961   return parameters;
13962 }
13963
13964 /* Parse a parameter-declaration-list.
13965
13966    parameter-declaration-list:
13967      parameter-declaration
13968      parameter-declaration-list , parameter-declaration
13969
13970    Returns a representation of the parameter-declaration-list, as for
13971    cp_parser_parameter_declaration_clause.  However, the
13972    `void_list_node' is never appended to the list.  Upon return,
13973    *IS_ERROR will be true iff an error occurred.  */
13974
13975 static tree
13976 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13977 {
13978   tree parameters = NULL_TREE;
13979   tree *tail = &parameters; 
13980   bool saved_in_unbraced_linkage_specification_p;
13981
13982   /* Assume all will go well.  */
13983   *is_error = false;
13984   /* The special considerations that apply to a function within an
13985      unbraced linkage specifications do not apply to the parameters
13986      to the function.  */
13987   saved_in_unbraced_linkage_specification_p 
13988     = parser->in_unbraced_linkage_specification_p;
13989   parser->in_unbraced_linkage_specification_p = false;
13990
13991   /* Look for more parameters.  */
13992   while (true)
13993     {
13994       cp_parameter_declarator *parameter;
13995       tree decl = error_mark_node;
13996       bool parenthesized_p;
13997       /* Parse the parameter.  */
13998       parameter
13999         = cp_parser_parameter_declaration (parser,
14000                                            /*template_parm_p=*/false,
14001                                            &parenthesized_p);
14002
14003       /* We don't know yet if the enclosing context is deprecated, so wait
14004          and warn in grokparms if appropriate.  */
14005       deprecated_state = DEPRECATED_SUPPRESS;
14006
14007       if (parameter)
14008         decl = grokdeclarator (parameter->declarator,
14009                                &parameter->decl_specifiers,
14010                                PARM,
14011                                parameter->default_argument != NULL_TREE,
14012                                &parameter->decl_specifiers.attributes);
14013
14014       deprecated_state = DEPRECATED_NORMAL;
14015
14016       /* If a parse error occurred parsing the parameter declaration,
14017          then the entire parameter-declaration-list is erroneous.  */
14018       if (decl == error_mark_node)
14019         {
14020           *is_error = true;
14021           parameters = error_mark_node;
14022           break;
14023         }
14024
14025       if (parameter->decl_specifiers.attributes)
14026         cplus_decl_attributes (&decl,
14027                                parameter->decl_specifiers.attributes,
14028                                0);
14029       if (DECL_NAME (decl))
14030         decl = pushdecl (decl);
14031
14032       /* Add the new parameter to the list.  */
14033       *tail = build_tree_list (parameter->default_argument, decl);
14034       tail = &TREE_CHAIN (*tail);
14035
14036       /* Peek at the next token.  */
14037       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14038           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14039           /* These are for Objective-C++ */
14040           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14041           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14042         /* The parameter-declaration-list is complete.  */
14043         break;
14044       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14045         {
14046           cp_token *token;
14047
14048           /* Peek at the next token.  */
14049           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14050           /* If it's an ellipsis, then the list is complete.  */
14051           if (token->type == CPP_ELLIPSIS)
14052             break;
14053           /* Otherwise, there must be more parameters.  Consume the
14054              `,'.  */
14055           cp_lexer_consume_token (parser->lexer);
14056           /* When parsing something like:
14057
14058                 int i(float f, double d)
14059
14060              we can tell after seeing the declaration for "f" that we
14061              are not looking at an initialization of a variable "i",
14062              but rather at the declaration of a function "i".
14063
14064              Due to the fact that the parsing of template arguments
14065              (as specified to a template-id) requires backtracking we
14066              cannot use this technique when inside a template argument
14067              list.  */
14068           if (!parser->in_template_argument_list_p
14069               && !parser->in_type_id_in_expr_p
14070               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14071               /* However, a parameter-declaration of the form
14072                  "foat(f)" (which is a valid declaration of a
14073                  parameter "f") can also be interpreted as an
14074                  expression (the conversion of "f" to "float").  */
14075               && !parenthesized_p)
14076             cp_parser_commit_to_tentative_parse (parser);
14077         }
14078       else
14079         {
14080           cp_parser_error (parser, "expected %<,%> or %<...%>");
14081           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14082             cp_parser_skip_to_closing_parenthesis (parser,
14083                                                    /*recovering=*/true,
14084                                                    /*or_comma=*/false,
14085                                                    /*consume_paren=*/false);
14086           break;
14087         }
14088     }
14089
14090   parser->in_unbraced_linkage_specification_p
14091     = saved_in_unbraced_linkage_specification_p;
14092
14093   return parameters;
14094 }
14095
14096 /* Parse a parameter declaration.
14097
14098    parameter-declaration:
14099      decl-specifier-seq ... [opt] declarator
14100      decl-specifier-seq declarator = assignment-expression
14101      decl-specifier-seq ... [opt] abstract-declarator [opt]
14102      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14103
14104    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14105    declares a template parameter.  (In that case, a non-nested `>'
14106    token encountered during the parsing of the assignment-expression
14107    is not interpreted as a greater-than operator.)
14108
14109    Returns a representation of the parameter, or NULL if an error
14110    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14111    true iff the declarator is of the form "(p)".  */
14112
14113 static cp_parameter_declarator *
14114 cp_parser_parameter_declaration (cp_parser *parser,
14115                                  bool template_parm_p,
14116                                  bool *parenthesized_p)
14117 {
14118   int declares_class_or_enum;
14119   bool greater_than_is_operator_p;
14120   cp_decl_specifier_seq decl_specifiers;
14121   cp_declarator *declarator;
14122   tree default_argument;
14123   cp_token *token = NULL, *declarator_token_start = NULL;
14124   const char *saved_message;
14125
14126   /* In a template parameter, `>' is not an operator.
14127
14128      [temp.param]
14129
14130      When parsing a default template-argument for a non-type
14131      template-parameter, the first non-nested `>' is taken as the end
14132      of the template parameter-list rather than a greater-than
14133      operator.  */
14134   greater_than_is_operator_p = !template_parm_p;
14135
14136   /* Type definitions may not appear in parameter types.  */
14137   saved_message = parser->type_definition_forbidden_message;
14138   parser->type_definition_forbidden_message
14139     = "types may not be defined in parameter types";
14140
14141   /* Parse the declaration-specifiers.  */
14142   cp_parser_decl_specifier_seq (parser,
14143                                 CP_PARSER_FLAGS_NONE,
14144                                 &decl_specifiers,
14145                                 &declares_class_or_enum);
14146   /* If an error occurred, there's no reason to attempt to parse the
14147      rest of the declaration.  */
14148   if (cp_parser_error_occurred (parser))
14149     {
14150       parser->type_definition_forbidden_message = saved_message;
14151       return NULL;
14152     }
14153
14154   /* Peek at the next token.  */
14155   token = cp_lexer_peek_token (parser->lexer);
14156
14157   /* If the next token is a `)', `,', `=', `>', or `...', then there
14158      is no declarator. However, when variadic templates are enabled,
14159      there may be a declarator following `...'.  */
14160   if (token->type == CPP_CLOSE_PAREN
14161       || token->type == CPP_COMMA
14162       || token->type == CPP_EQ
14163       || token->type == CPP_GREATER)
14164     {
14165       declarator = NULL;
14166       if (parenthesized_p)
14167         *parenthesized_p = false;
14168     }
14169   /* Otherwise, there should be a declarator.  */
14170   else
14171     {
14172       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14173       parser->default_arg_ok_p = false;
14174
14175       /* After seeing a decl-specifier-seq, if the next token is not a
14176          "(", there is no possibility that the code is a valid
14177          expression.  Therefore, if parsing tentatively, we commit at
14178          this point.  */
14179       if (!parser->in_template_argument_list_p
14180           /* In an expression context, having seen:
14181
14182                (int((char ...
14183
14184              we cannot be sure whether we are looking at a
14185              function-type (taking a "char" as a parameter) or a cast
14186              of some object of type "char" to "int".  */
14187           && !parser->in_type_id_in_expr_p
14188           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14189           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14190         cp_parser_commit_to_tentative_parse (parser);
14191       /* Parse the declarator.  */
14192       declarator_token_start = token;
14193       declarator = cp_parser_declarator (parser,
14194                                          CP_PARSER_DECLARATOR_EITHER,
14195                                          /*ctor_dtor_or_conv_p=*/NULL,
14196                                          parenthesized_p,
14197                                          /*member_p=*/false);
14198       parser->default_arg_ok_p = saved_default_arg_ok_p;
14199       /* After the declarator, allow more attributes.  */
14200       decl_specifiers.attributes
14201         = chainon (decl_specifiers.attributes,
14202                    cp_parser_attributes_opt (parser));
14203     }
14204
14205   /* If the next token is an ellipsis, and we have not seen a
14206      declarator name, and the type of the declarator contains parameter
14207      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14208      a parameter pack expansion expression. Otherwise, leave the
14209      ellipsis for a C-style variadic function. */
14210   token = cp_lexer_peek_token (parser->lexer);
14211   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14212     {
14213       tree type = decl_specifiers.type;
14214
14215       if (type && DECL_P (type))
14216         type = TREE_TYPE (type);
14217
14218       if (type
14219           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14220           && declarator_can_be_parameter_pack (declarator)
14221           && (!declarator || !declarator->parameter_pack_p)
14222           && uses_parameter_packs (type))
14223         {
14224           /* Consume the `...'. */
14225           cp_lexer_consume_token (parser->lexer);
14226           maybe_warn_variadic_templates ();
14227           
14228           /* Build a pack expansion type */
14229           if (declarator)
14230             declarator->parameter_pack_p = true;
14231           else
14232             decl_specifiers.type = make_pack_expansion (type);
14233         }
14234     }
14235
14236   /* The restriction on defining new types applies only to the type
14237      of the parameter, not to the default argument.  */
14238   parser->type_definition_forbidden_message = saved_message;
14239
14240   /* If the next token is `=', then process a default argument.  */
14241   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14242     {
14243       /* Consume the `='.  */
14244       cp_lexer_consume_token (parser->lexer);
14245
14246       /* If we are defining a class, then the tokens that make up the
14247          default argument must be saved and processed later.  */
14248       if (!template_parm_p && at_class_scope_p ()
14249           && TYPE_BEING_DEFINED (current_class_type))
14250         {
14251           unsigned depth = 0;
14252           int maybe_template_id = 0;
14253           cp_token *first_token;
14254           cp_token *token;
14255
14256           /* Add tokens until we have processed the entire default
14257              argument.  We add the range [first_token, token).  */
14258           first_token = cp_lexer_peek_token (parser->lexer);
14259           while (true)
14260             {
14261               bool done = false;
14262
14263               /* Peek at the next token.  */
14264               token = cp_lexer_peek_token (parser->lexer);
14265               /* What we do depends on what token we have.  */
14266               switch (token->type)
14267                 {
14268                   /* In valid code, a default argument must be
14269                      immediately followed by a `,' `)', or `...'.  */
14270                 case CPP_COMMA:
14271                   if (depth == 0 && maybe_template_id)
14272                     {
14273                       /* If we've seen a '<', we might be in a
14274                          template-argument-list.  Until Core issue 325 is
14275                          resolved, we don't know how this situation ought
14276                          to be handled, so try to DTRT.  We check whether
14277                          what comes after the comma is a valid parameter
14278                          declaration list.  If it is, then the comma ends
14279                          the default argument; otherwise the default
14280                          argument continues.  */
14281                       bool error = false;
14282
14283                       /* Set ITALP so cp_parser_parameter_declaration_list
14284                          doesn't decide to commit to this parse.  */
14285                       bool saved_italp = parser->in_template_argument_list_p;
14286                       parser->in_template_argument_list_p = true;
14287
14288                       cp_parser_parse_tentatively (parser);
14289                       cp_lexer_consume_token (parser->lexer);
14290                       cp_parser_parameter_declaration_list (parser, &error);
14291                       if (!cp_parser_error_occurred (parser) && !error)
14292                         done = true;
14293                       cp_parser_abort_tentative_parse (parser);
14294
14295                       parser->in_template_argument_list_p = saved_italp;
14296                       break;
14297                     }
14298                 case CPP_CLOSE_PAREN:
14299                 case CPP_ELLIPSIS:
14300                   /* If we run into a non-nested `;', `}', or `]',
14301                      then the code is invalid -- but the default
14302                      argument is certainly over.  */
14303                 case CPP_SEMICOLON:
14304                 case CPP_CLOSE_BRACE:
14305                 case CPP_CLOSE_SQUARE:
14306                   if (depth == 0)
14307                     done = true;
14308                   /* Update DEPTH, if necessary.  */
14309                   else if (token->type == CPP_CLOSE_PAREN
14310                            || token->type == CPP_CLOSE_BRACE
14311                            || token->type == CPP_CLOSE_SQUARE)
14312                     --depth;
14313                   break;
14314
14315                 case CPP_OPEN_PAREN:
14316                 case CPP_OPEN_SQUARE:
14317                 case CPP_OPEN_BRACE:
14318                   ++depth;
14319                   break;
14320
14321                 case CPP_LESS:
14322                   if (depth == 0)
14323                     /* This might be the comparison operator, or it might
14324                        start a template argument list.  */
14325                     ++maybe_template_id;
14326                   break;
14327
14328                 case CPP_RSHIFT:
14329                   if (cxx_dialect == cxx98)
14330                     break;
14331                   /* Fall through for C++0x, which treats the `>>'
14332                      operator like two `>' tokens in certain
14333                      cases.  */
14334
14335                 case CPP_GREATER:
14336                   if (depth == 0)
14337                     {
14338                       /* This might be an operator, or it might close a
14339                          template argument list.  But if a previous '<'
14340                          started a template argument list, this will have
14341                          closed it, so we can't be in one anymore.  */
14342                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14343                       if (maybe_template_id < 0)
14344                         maybe_template_id = 0;
14345                     }
14346                   break;
14347
14348                   /* If we run out of tokens, issue an error message.  */
14349                 case CPP_EOF:
14350                 case CPP_PRAGMA_EOL:
14351                   error ("%Hfile ends in default argument", &token->location);
14352                   done = true;
14353                   break;
14354
14355                 case CPP_NAME:
14356                 case CPP_SCOPE:
14357                   /* In these cases, we should look for template-ids.
14358                      For example, if the default argument is
14359                      `X<int, double>()', we need to do name lookup to
14360                      figure out whether or not `X' is a template; if
14361                      so, the `,' does not end the default argument.
14362
14363                      That is not yet done.  */
14364                   break;
14365
14366                 default:
14367                   break;
14368                 }
14369
14370               /* If we've reached the end, stop.  */
14371               if (done)
14372                 break;
14373
14374               /* Add the token to the token block.  */
14375               token = cp_lexer_consume_token (parser->lexer);
14376             }
14377
14378           /* Create a DEFAULT_ARG to represent the unparsed default
14379              argument.  */
14380           default_argument = make_node (DEFAULT_ARG);
14381           DEFARG_TOKENS (default_argument)
14382             = cp_token_cache_new (first_token, token);
14383           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14384         }
14385       /* Outside of a class definition, we can just parse the
14386          assignment-expression.  */
14387       else
14388         {
14389           token = cp_lexer_peek_token (parser->lexer);
14390           default_argument 
14391             = cp_parser_default_argument (parser, template_parm_p);
14392         }
14393
14394       if (!parser->default_arg_ok_p)
14395         {
14396           if (flag_permissive)
14397             warning (0, "deprecated use of default argument for parameter of non-function");
14398           else
14399             {
14400               error ("%Hdefault arguments are only "
14401                      "permitted for function parameters",
14402                      &token->location);
14403               default_argument = NULL_TREE;
14404             }
14405         }
14406       else if ((declarator && declarator->parameter_pack_p)
14407                || (decl_specifiers.type
14408                    && PACK_EXPANSION_P (decl_specifiers.type)))
14409         {
14410           const char* kind = template_parm_p? "template " : "";
14411           
14412           /* Find the name of the parameter pack.  */     
14413           cp_declarator *id_declarator = declarator;
14414           while (id_declarator && id_declarator->kind != cdk_id)
14415             id_declarator = id_declarator->declarator;
14416           
14417           if (id_declarator && id_declarator->kind == cdk_id)
14418             error ("%H%sparameter pack %qD cannot have a default argument",
14419                    &declarator_token_start->location,
14420                    kind, id_declarator->u.id.unqualified_name);
14421           else
14422             error ("%H%sparameter pack cannot have a default argument",
14423                    &declarator_token_start->location, kind);
14424           
14425           default_argument = NULL_TREE;
14426         }
14427     }
14428   else
14429     default_argument = NULL_TREE;
14430
14431   return make_parameter_declarator (&decl_specifiers,
14432                                     declarator,
14433                                     default_argument);
14434 }
14435
14436 /* Parse a default argument and return it.
14437
14438    TEMPLATE_PARM_P is true if this is a default argument for a
14439    non-type template parameter.  */
14440 static tree
14441 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14442 {
14443   tree default_argument = NULL_TREE;
14444   bool saved_greater_than_is_operator_p;
14445   bool saved_local_variables_forbidden_p;
14446
14447   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14448      set correctly.  */
14449   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14450   parser->greater_than_is_operator_p = !template_parm_p;
14451   /* Local variable names (and the `this' keyword) may not
14452      appear in a default argument.  */
14453   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14454   parser->local_variables_forbidden_p = true;
14455   /* The default argument expression may cause implicitly
14456      defined member functions to be synthesized, which will
14457      result in garbage collection.  We must treat this
14458      situation as if we were within the body of function so as
14459      to avoid collecting live data on the stack.  */
14460   ++function_depth;
14461   /* Parse the assignment-expression.  */
14462   if (template_parm_p)
14463     push_deferring_access_checks (dk_no_deferred);
14464   default_argument
14465     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14466   if (template_parm_p)
14467     pop_deferring_access_checks ();
14468   /* Restore saved state.  */
14469   --function_depth;
14470   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14471   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14472
14473   return default_argument;
14474 }
14475
14476 /* Parse a function-body.
14477
14478    function-body:
14479      compound_statement  */
14480
14481 static void
14482 cp_parser_function_body (cp_parser *parser)
14483 {
14484   cp_parser_compound_statement (parser, NULL, false);
14485 }
14486
14487 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14488    true if a ctor-initializer was present.  */
14489
14490 static bool
14491 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14492 {
14493   tree body;
14494   bool ctor_initializer_p;
14495
14496   /* Begin the function body.  */
14497   body = begin_function_body ();
14498   /* Parse the optional ctor-initializer.  */
14499   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14500   /* Parse the function-body.  */
14501   cp_parser_function_body (parser);
14502   /* Finish the function body.  */
14503   finish_function_body (body);
14504
14505   return ctor_initializer_p;
14506 }
14507
14508 /* Parse an initializer.
14509
14510    initializer:
14511      = initializer-clause
14512      ( expression-list )
14513
14514    Returns an expression representing the initializer.  If no
14515    initializer is present, NULL_TREE is returned.
14516
14517    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14518    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14519    set to TRUE if there is no initializer present.  If there is an
14520    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14521    is set to true; otherwise it is set to false.  */
14522
14523 static tree
14524 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14525                        bool* non_constant_p)
14526 {
14527   cp_token *token;
14528   tree init;
14529
14530   /* Peek at the next token.  */
14531   token = cp_lexer_peek_token (parser->lexer);
14532
14533   /* Let our caller know whether or not this initializer was
14534      parenthesized.  */
14535   *is_direct_init = (token->type != CPP_EQ);
14536   /* Assume that the initializer is constant.  */
14537   *non_constant_p = false;
14538
14539   if (token->type == CPP_EQ)
14540     {
14541       /* Consume the `='.  */
14542       cp_lexer_consume_token (parser->lexer);
14543       /* Parse the initializer-clause.  */
14544       init = cp_parser_initializer_clause (parser, non_constant_p);
14545     }
14546   else if (token->type == CPP_OPEN_PAREN)
14547     init = cp_parser_parenthesized_expression_list (parser, false,
14548                                                     /*cast_p=*/false,
14549                                                     /*allow_expansion_p=*/true,
14550                                                     non_constant_p);
14551   else if (token->type == CPP_OPEN_BRACE)
14552     {
14553       maybe_warn_cpp0x ("extended initializer lists");
14554       init = cp_parser_braced_list (parser, non_constant_p);
14555       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14556     }
14557   else
14558     {
14559       /* Anything else is an error.  */
14560       cp_parser_error (parser, "expected initializer");
14561       init = error_mark_node;
14562     }
14563
14564   return init;
14565 }
14566
14567 /* Parse an initializer-clause.
14568
14569    initializer-clause:
14570      assignment-expression
14571      braced-init-list
14572
14573    Returns an expression representing the initializer.
14574
14575    If the `assignment-expression' production is used the value
14576    returned is simply a representation for the expression.
14577
14578    Otherwise, calls cp_parser_braced_list.  */
14579
14580 static tree
14581 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14582 {
14583   tree initializer;
14584
14585   /* Assume the expression is constant.  */
14586   *non_constant_p = false;
14587
14588   /* If it is not a `{', then we are looking at an
14589      assignment-expression.  */
14590   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14591     {
14592       initializer
14593         = cp_parser_constant_expression (parser,
14594                                         /*allow_non_constant_p=*/true,
14595                                         non_constant_p);
14596       if (!*non_constant_p)
14597         initializer = fold_non_dependent_expr (initializer);
14598     }
14599   else
14600     initializer = cp_parser_braced_list (parser, non_constant_p);
14601
14602   return initializer;
14603 }
14604
14605 /* Parse a brace-enclosed initializer list.
14606
14607    braced-init-list:
14608      { initializer-list , [opt] }
14609      { }
14610
14611    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14612    the elements of the initializer-list (or NULL, if the last
14613    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14614    NULL_TREE.  There is no way to detect whether or not the optional
14615    trailing `,' was provided.  NON_CONSTANT_P is as for
14616    cp_parser_initializer.  */     
14617
14618 static tree
14619 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14620 {
14621   tree initializer;
14622
14623   /* Consume the `{' token.  */
14624   cp_lexer_consume_token (parser->lexer);
14625   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14626   initializer = make_node (CONSTRUCTOR);
14627   /* If it's not a `}', then there is a non-trivial initializer.  */
14628   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14629     {
14630       /* Parse the initializer list.  */
14631       CONSTRUCTOR_ELTS (initializer)
14632         = cp_parser_initializer_list (parser, non_constant_p);
14633       /* A trailing `,' token is allowed.  */
14634       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14635         cp_lexer_consume_token (parser->lexer);
14636     }
14637   /* Now, there should be a trailing `}'.  */
14638   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14639   TREE_TYPE (initializer) = init_list_type_node;
14640   return initializer;
14641 }
14642
14643 /* Parse an initializer-list.
14644
14645    initializer-list:
14646      initializer-clause ... [opt]
14647      initializer-list , initializer-clause ... [opt]
14648
14649    GNU Extension:
14650
14651    initializer-list:
14652      identifier : initializer-clause
14653      initializer-list, identifier : initializer-clause
14654
14655    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14656    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14657    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14658    as for cp_parser_initializer.  */
14659
14660 static VEC(constructor_elt,gc) *
14661 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14662 {
14663   VEC(constructor_elt,gc) *v = NULL;
14664
14665   /* Assume all of the expressions are constant.  */
14666   *non_constant_p = false;
14667
14668   /* Parse the rest of the list.  */
14669   while (true)
14670     {
14671       cp_token *token;
14672       tree identifier;
14673       tree initializer;
14674       bool clause_non_constant_p;
14675
14676       /* If the next token is an identifier and the following one is a
14677          colon, we are looking at the GNU designated-initializer
14678          syntax.  */
14679       if (cp_parser_allow_gnu_extensions_p (parser)
14680           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14681           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14682         {
14683           /* Warn the user that they are using an extension.  */
14684           pedwarn (input_location, OPT_pedantic, 
14685                    "ISO C++ does not allow designated initializers");
14686           /* Consume the identifier.  */
14687           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14688           /* Consume the `:'.  */
14689           cp_lexer_consume_token (parser->lexer);
14690         }
14691       else
14692         identifier = NULL_TREE;
14693
14694       /* Parse the initializer.  */
14695       initializer = cp_parser_initializer_clause (parser,
14696                                                   &clause_non_constant_p);
14697       /* If any clause is non-constant, so is the entire initializer.  */
14698       if (clause_non_constant_p)
14699         *non_constant_p = true;
14700
14701       /* If we have an ellipsis, this is an initializer pack
14702          expansion.  */
14703       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14704         {
14705           /* Consume the `...'.  */
14706           cp_lexer_consume_token (parser->lexer);
14707
14708           /* Turn the initializer into an initializer expansion.  */
14709           initializer = make_pack_expansion (initializer);
14710         }
14711
14712       /* Add it to the vector.  */
14713       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14714
14715       /* If the next token is not a comma, we have reached the end of
14716          the list.  */
14717       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14718         break;
14719
14720       /* Peek at the next token.  */
14721       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14722       /* If the next token is a `}', then we're still done.  An
14723          initializer-clause can have a trailing `,' after the
14724          initializer-list and before the closing `}'.  */
14725       if (token->type == CPP_CLOSE_BRACE)
14726         break;
14727
14728       /* Consume the `,' token.  */
14729       cp_lexer_consume_token (parser->lexer);
14730     }
14731
14732   return v;
14733 }
14734
14735 /* Classes [gram.class] */
14736
14737 /* Parse a class-name.
14738
14739    class-name:
14740      identifier
14741      template-id
14742
14743    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14744    to indicate that names looked up in dependent types should be
14745    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14746    keyword has been used to indicate that the name that appears next
14747    is a template.  TAG_TYPE indicates the explicit tag given before
14748    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14749    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14750    is the class being defined in a class-head.
14751
14752    Returns the TYPE_DECL representing the class.  */
14753
14754 static tree
14755 cp_parser_class_name (cp_parser *parser,
14756                       bool typename_keyword_p,
14757                       bool template_keyword_p,
14758                       enum tag_types tag_type,
14759                       bool check_dependency_p,
14760                       bool class_head_p,
14761                       bool is_declaration)
14762 {
14763   tree decl;
14764   tree scope;
14765   bool typename_p;
14766   cp_token *token;
14767   tree identifier = NULL_TREE;
14768
14769   /* All class-names start with an identifier.  */
14770   token = cp_lexer_peek_token (parser->lexer);
14771   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14772     {
14773       cp_parser_error (parser, "expected class-name");
14774       return error_mark_node;
14775     }
14776
14777   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14778      to a template-id, so we save it here.  */
14779   scope = parser->scope;
14780   if (scope == error_mark_node)
14781     return error_mark_node;
14782
14783   /* Any name names a type if we're following the `typename' keyword
14784      in a qualified name where the enclosing scope is type-dependent.  */
14785   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14786                 && dependent_type_p (scope));
14787   /* Handle the common case (an identifier, but not a template-id)
14788      efficiently.  */
14789   if (token->type == CPP_NAME
14790       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14791     {
14792       cp_token *identifier_token;
14793       bool ambiguous_p;
14794
14795       /* Look for the identifier.  */
14796       identifier_token = cp_lexer_peek_token (parser->lexer);
14797       ambiguous_p = identifier_token->ambiguous_p;
14798       identifier = cp_parser_identifier (parser);
14799       /* If the next token isn't an identifier, we are certainly not
14800          looking at a class-name.  */
14801       if (identifier == error_mark_node)
14802         decl = error_mark_node;
14803       /* If we know this is a type-name, there's no need to look it
14804          up.  */
14805       else if (typename_p)
14806         decl = identifier;
14807       else
14808         {
14809           tree ambiguous_decls;
14810           /* If we already know that this lookup is ambiguous, then
14811              we've already issued an error message; there's no reason
14812              to check again.  */
14813           if (ambiguous_p)
14814             {
14815               cp_parser_simulate_error (parser);
14816               return error_mark_node;
14817             }
14818           /* If the next token is a `::', then the name must be a type
14819              name.
14820
14821              [basic.lookup.qual]
14822
14823              During the lookup for a name preceding the :: scope
14824              resolution operator, object, function, and enumerator
14825              names are ignored.  */
14826           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14827             tag_type = typename_type;
14828           /* Look up the name.  */
14829           decl = cp_parser_lookup_name (parser, identifier,
14830                                         tag_type,
14831                                         /*is_template=*/false,
14832                                         /*is_namespace=*/false,
14833                                         check_dependency_p,
14834                                         &ambiguous_decls,
14835                                         identifier_token->location);
14836           if (ambiguous_decls)
14837             {
14838               error ("%Hreference to %qD is ambiguous",
14839                      &identifier_token->location, identifier);
14840               print_candidates (ambiguous_decls);
14841               if (cp_parser_parsing_tentatively (parser))
14842                 {
14843                   identifier_token->ambiguous_p = true;
14844                   cp_parser_simulate_error (parser);
14845                 }
14846               return error_mark_node;
14847             }
14848         }
14849     }
14850   else
14851     {
14852       /* Try a template-id.  */
14853       decl = cp_parser_template_id (parser, template_keyword_p,
14854                                     check_dependency_p,
14855                                     is_declaration);
14856       if (decl == error_mark_node)
14857         return error_mark_node;
14858     }
14859
14860   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14861
14862   /* If this is a typename, create a TYPENAME_TYPE.  */
14863   if (typename_p && decl != error_mark_node)
14864     {
14865       decl = make_typename_type (scope, decl, typename_type,
14866                                  /*complain=*/tf_error);
14867       if (decl != error_mark_node)
14868         decl = TYPE_NAME (decl);
14869     }
14870
14871   /* Check to see that it is really the name of a class.  */
14872   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14873       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14874       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14875     /* Situations like this:
14876
14877          template <typename T> struct A {
14878            typename T::template X<int>::I i;
14879          };
14880
14881        are problematic.  Is `T::template X<int>' a class-name?  The
14882        standard does not seem to be definitive, but there is no other
14883        valid interpretation of the following `::'.  Therefore, those
14884        names are considered class-names.  */
14885     {
14886       decl = make_typename_type (scope, decl, tag_type, tf_error);
14887       if (decl != error_mark_node)
14888         decl = TYPE_NAME (decl);
14889     }
14890   else if (TREE_CODE (decl) != TYPE_DECL
14891            || TREE_TYPE (decl) == error_mark_node
14892            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14893     decl = error_mark_node;
14894
14895   if (decl == error_mark_node)
14896     cp_parser_error (parser, "expected class-name");
14897   else if (identifier && !parser->scope)
14898     maybe_note_name_used_in_class (identifier, decl);
14899
14900   return decl;
14901 }
14902
14903 /* Parse a class-specifier.
14904
14905    class-specifier:
14906      class-head { member-specification [opt] }
14907
14908    Returns the TREE_TYPE representing the class.  */
14909
14910 static tree
14911 cp_parser_class_specifier (cp_parser* parser)
14912 {
14913   cp_token *token;
14914   tree type;
14915   tree attributes = NULL_TREE;
14916   int has_trailing_semicolon;
14917   bool nested_name_specifier_p;
14918   unsigned saved_num_template_parameter_lists;
14919   bool saved_in_function_body;
14920   bool saved_in_unbraced_linkage_specification_p;
14921   tree old_scope = NULL_TREE;
14922   tree scope = NULL_TREE;
14923   tree bases;
14924
14925   push_deferring_access_checks (dk_no_deferred);
14926
14927   /* Parse the class-head.  */
14928   type = cp_parser_class_head (parser,
14929                                &nested_name_specifier_p,
14930                                &attributes,
14931                                &bases);
14932   /* If the class-head was a semantic disaster, skip the entire body
14933      of the class.  */
14934   if (!type)
14935     {
14936       cp_parser_skip_to_end_of_block_or_statement (parser);
14937       pop_deferring_access_checks ();
14938       return error_mark_node;
14939     }
14940
14941   /* Look for the `{'.  */
14942   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14943     {
14944       pop_deferring_access_checks ();
14945       return error_mark_node;
14946     }
14947
14948   /* Process the base classes. If they're invalid, skip the 
14949      entire class body.  */
14950   if (!xref_basetypes (type, bases))
14951     {
14952       /* Consuming the closing brace yields better error messages
14953          later on.  */
14954       if (cp_parser_skip_to_closing_brace (parser))
14955         cp_lexer_consume_token (parser->lexer);
14956       pop_deferring_access_checks ();
14957       return error_mark_node;
14958     }
14959
14960   /* Issue an error message if type-definitions are forbidden here.  */
14961   cp_parser_check_type_definition (parser);
14962   /* Remember that we are defining one more class.  */
14963   ++parser->num_classes_being_defined;
14964   /* Inside the class, surrounding template-parameter-lists do not
14965      apply.  */
14966   saved_num_template_parameter_lists
14967     = parser->num_template_parameter_lists;
14968   parser->num_template_parameter_lists = 0;
14969   /* We are not in a function body.  */
14970   saved_in_function_body = parser->in_function_body;
14971   parser->in_function_body = false;
14972   /* We are not immediately inside an extern "lang" block.  */
14973   saved_in_unbraced_linkage_specification_p
14974     = parser->in_unbraced_linkage_specification_p;
14975   parser->in_unbraced_linkage_specification_p = false;
14976
14977   /* Start the class.  */
14978   if (nested_name_specifier_p)
14979     {
14980       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14981       old_scope = push_inner_scope (scope);
14982     }
14983   type = begin_class_definition (type, attributes);
14984
14985   if (type == error_mark_node)
14986     /* If the type is erroneous, skip the entire body of the class.  */
14987     cp_parser_skip_to_closing_brace (parser);
14988   else
14989     /* Parse the member-specification.  */
14990     cp_parser_member_specification_opt (parser);
14991
14992   /* Look for the trailing `}'.  */
14993   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14994   /* We get better error messages by noticing a common problem: a
14995      missing trailing `;'.  */
14996   token = cp_lexer_peek_token (parser->lexer);
14997   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14998   /* Look for trailing attributes to apply to this class.  */
14999   if (cp_parser_allow_gnu_extensions_p (parser))
15000     attributes = cp_parser_attributes_opt (parser);
15001   if (type != error_mark_node)
15002     type = finish_struct (type, attributes);
15003   if (nested_name_specifier_p)
15004     pop_inner_scope (old_scope, scope);
15005   /* If this class is not itself within the scope of another class,
15006      then we need to parse the bodies of all of the queued function
15007      definitions.  Note that the queued functions defined in a class
15008      are not always processed immediately following the
15009      class-specifier for that class.  Consider:
15010
15011        struct A {
15012          struct B { void f() { sizeof (A); } };
15013        };
15014
15015      If `f' were processed before the processing of `A' were
15016      completed, there would be no way to compute the size of `A'.
15017      Note that the nesting we are interested in here is lexical --
15018      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15019      for:
15020
15021        struct A { struct B; };
15022        struct A::B { void f() { } };
15023
15024      there is no need to delay the parsing of `A::B::f'.  */
15025   if (--parser->num_classes_being_defined == 0)
15026     {
15027       tree queue_entry;
15028       tree fn;
15029       tree class_type = NULL_TREE;
15030       tree pushed_scope = NULL_TREE;
15031
15032       /* In a first pass, parse default arguments to the functions.
15033          Then, in a second pass, parse the bodies of the functions.
15034          This two-phased approach handles cases like:
15035
15036             struct S {
15037               void f() { g(); }
15038               void g(int i = 3);
15039             };
15040
15041          */
15042       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15043              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15044            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15045            TREE_PURPOSE (parser->unparsed_functions_queues)
15046              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15047         {
15048           fn = TREE_VALUE (queue_entry);
15049           /* If there are default arguments that have not yet been processed,
15050              take care of them now.  */
15051           if (class_type != TREE_PURPOSE (queue_entry))
15052             {
15053               if (pushed_scope)
15054                 pop_scope (pushed_scope);
15055               class_type = TREE_PURPOSE (queue_entry);
15056               pushed_scope = push_scope (class_type);
15057             }
15058           /* Make sure that any template parameters are in scope.  */
15059           maybe_begin_member_template_processing (fn);
15060           /* Parse the default argument expressions.  */
15061           cp_parser_late_parsing_default_args (parser, fn);
15062           /* Remove any template parameters from the symbol table.  */
15063           maybe_end_member_template_processing ();
15064         }
15065       if (pushed_scope)
15066         pop_scope (pushed_scope);
15067       /* Now parse the body of the functions.  */
15068       for (TREE_VALUE (parser->unparsed_functions_queues)
15069              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15070            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15071            TREE_VALUE (parser->unparsed_functions_queues)
15072              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15073         {
15074           /* Figure out which function we need to process.  */
15075           fn = TREE_VALUE (queue_entry);
15076           /* Parse the function.  */
15077           cp_parser_late_parsing_for_member (parser, fn);
15078         }
15079     }
15080
15081   /* Put back any saved access checks.  */
15082   pop_deferring_access_checks ();
15083
15084   /* Restore saved state.  */
15085   parser->in_function_body = saved_in_function_body;
15086   parser->num_template_parameter_lists
15087     = saved_num_template_parameter_lists;
15088   parser->in_unbraced_linkage_specification_p
15089     = saved_in_unbraced_linkage_specification_p;
15090
15091   return type;
15092 }
15093
15094 /* Parse a class-head.
15095
15096    class-head:
15097      class-key identifier [opt] base-clause [opt]
15098      class-key nested-name-specifier identifier base-clause [opt]
15099      class-key nested-name-specifier [opt] template-id
15100        base-clause [opt]
15101
15102    GNU Extensions:
15103      class-key attributes identifier [opt] base-clause [opt]
15104      class-key attributes nested-name-specifier identifier base-clause [opt]
15105      class-key attributes nested-name-specifier [opt] template-id
15106        base-clause [opt]
15107
15108    Upon return BASES is initialized to the list of base classes (or
15109    NULL, if there are none) in the same form returned by
15110    cp_parser_base_clause.
15111
15112    Returns the TYPE of the indicated class.  Sets
15113    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15114    involving a nested-name-specifier was used, and FALSE otherwise.
15115
15116    Returns error_mark_node if this is not a class-head.
15117
15118    Returns NULL_TREE if the class-head is syntactically valid, but
15119    semantically invalid in a way that means we should skip the entire
15120    body of the class.  */
15121
15122 static tree
15123 cp_parser_class_head (cp_parser* parser,
15124                       bool* nested_name_specifier_p,
15125                       tree *attributes_p,
15126                       tree *bases)
15127 {
15128   tree nested_name_specifier;
15129   enum tag_types class_key;
15130   tree id = NULL_TREE;
15131   tree type = NULL_TREE;
15132   tree attributes;
15133   bool template_id_p = false;
15134   bool qualified_p = false;
15135   bool invalid_nested_name_p = false;
15136   bool invalid_explicit_specialization_p = false;
15137   tree pushed_scope = NULL_TREE;
15138   unsigned num_templates;
15139   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15140   /* Assume no nested-name-specifier will be present.  */
15141   *nested_name_specifier_p = false;
15142   /* Assume no template parameter lists will be used in defining the
15143      type.  */
15144   num_templates = 0;
15145
15146   *bases = NULL_TREE;
15147
15148   /* Look for the class-key.  */
15149   class_key = cp_parser_class_key (parser);
15150   if (class_key == none_type)
15151     return error_mark_node;
15152
15153   /* Parse the attributes.  */
15154   attributes = cp_parser_attributes_opt (parser);
15155
15156   /* If the next token is `::', that is invalid -- but sometimes
15157      people do try to write:
15158
15159        struct ::S {};
15160
15161      Handle this gracefully by accepting the extra qualifier, and then
15162      issuing an error about it later if this really is a
15163      class-head.  If it turns out just to be an elaborated type
15164      specifier, remain silent.  */
15165   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15166     qualified_p = true;
15167
15168   push_deferring_access_checks (dk_no_check);
15169
15170   /* Determine the name of the class.  Begin by looking for an
15171      optional nested-name-specifier.  */
15172   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15173   nested_name_specifier
15174     = cp_parser_nested_name_specifier_opt (parser,
15175                                            /*typename_keyword_p=*/false,
15176                                            /*check_dependency_p=*/false,
15177                                            /*type_p=*/false,
15178                                            /*is_declaration=*/false);
15179   /* If there was a nested-name-specifier, then there *must* be an
15180      identifier.  */
15181   if (nested_name_specifier)
15182     {
15183       type_start_token = cp_lexer_peek_token (parser->lexer);
15184       /* Although the grammar says `identifier', it really means
15185          `class-name' or `template-name'.  You are only allowed to
15186          define a class that has already been declared with this
15187          syntax.
15188
15189          The proposed resolution for Core Issue 180 says that wherever
15190          you see `class T::X' you should treat `X' as a type-name.
15191
15192          It is OK to define an inaccessible class; for example:
15193
15194            class A { class B; };
15195            class A::B {};
15196
15197          We do not know if we will see a class-name, or a
15198          template-name.  We look for a class-name first, in case the
15199          class-name is a template-id; if we looked for the
15200          template-name first we would stop after the template-name.  */
15201       cp_parser_parse_tentatively (parser);
15202       type = cp_parser_class_name (parser,
15203                                    /*typename_keyword_p=*/false,
15204                                    /*template_keyword_p=*/false,
15205                                    class_type,
15206                                    /*check_dependency_p=*/false,
15207                                    /*class_head_p=*/true,
15208                                    /*is_declaration=*/false);
15209       /* If that didn't work, ignore the nested-name-specifier.  */
15210       if (!cp_parser_parse_definitely (parser))
15211         {
15212           invalid_nested_name_p = true;
15213           type_start_token = cp_lexer_peek_token (parser->lexer);
15214           id = cp_parser_identifier (parser);
15215           if (id == error_mark_node)
15216             id = NULL_TREE;
15217         }
15218       /* If we could not find a corresponding TYPE, treat this
15219          declaration like an unqualified declaration.  */
15220       if (type == error_mark_node)
15221         nested_name_specifier = NULL_TREE;
15222       /* Otherwise, count the number of templates used in TYPE and its
15223          containing scopes.  */
15224       else
15225         {
15226           tree scope;
15227
15228           for (scope = TREE_TYPE (type);
15229                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15230                scope = (TYPE_P (scope)
15231                         ? TYPE_CONTEXT (scope)
15232                         : DECL_CONTEXT (scope)))
15233             if (TYPE_P (scope)
15234                 && CLASS_TYPE_P (scope)
15235                 && CLASSTYPE_TEMPLATE_INFO (scope)
15236                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15237                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15238               ++num_templates;
15239         }
15240     }
15241   /* Otherwise, the identifier is optional.  */
15242   else
15243     {
15244       /* We don't know whether what comes next is a template-id,
15245          an identifier, or nothing at all.  */
15246       cp_parser_parse_tentatively (parser);
15247       /* Check for a template-id.  */
15248       type_start_token = cp_lexer_peek_token (parser->lexer);
15249       id = cp_parser_template_id (parser,
15250                                   /*template_keyword_p=*/false,
15251                                   /*check_dependency_p=*/true,
15252                                   /*is_declaration=*/true);
15253       /* If that didn't work, it could still be an identifier.  */
15254       if (!cp_parser_parse_definitely (parser))
15255         {
15256           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15257             {
15258               type_start_token = cp_lexer_peek_token (parser->lexer);
15259               id = cp_parser_identifier (parser);
15260             }
15261           else
15262             id = NULL_TREE;
15263         }
15264       else
15265         {
15266           template_id_p = true;
15267           ++num_templates;
15268         }
15269     }
15270
15271   pop_deferring_access_checks ();
15272
15273   if (id)
15274     cp_parser_check_for_invalid_template_id (parser, id,
15275                                              type_start_token->location);
15276
15277   /* If it's not a `:' or a `{' then we can't really be looking at a
15278      class-head, since a class-head only appears as part of a
15279      class-specifier.  We have to detect this situation before calling
15280      xref_tag, since that has irreversible side-effects.  */
15281   if (!cp_parser_next_token_starts_class_definition_p (parser))
15282     {
15283       cp_parser_error (parser, "expected %<{%> or %<:%>");
15284       return error_mark_node;
15285     }
15286
15287   /* At this point, we're going ahead with the class-specifier, even
15288      if some other problem occurs.  */
15289   cp_parser_commit_to_tentative_parse (parser);
15290   /* Issue the error about the overly-qualified name now.  */
15291   if (qualified_p)
15292     {
15293       cp_parser_error (parser,
15294                        "global qualification of class name is invalid");
15295       return error_mark_node;
15296     }
15297   else if (invalid_nested_name_p)
15298     {
15299       cp_parser_error (parser,
15300                        "qualified name does not name a class");
15301       return error_mark_node;
15302     }
15303   else if (nested_name_specifier)
15304     {
15305       tree scope;
15306
15307       /* Reject typedef-names in class heads.  */
15308       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15309         {
15310           error ("%Hinvalid class name in declaration of %qD",
15311                  &type_start_token->location, type);
15312           type = NULL_TREE;
15313           goto done;
15314         }
15315
15316       /* Figure out in what scope the declaration is being placed.  */
15317       scope = current_scope ();
15318       /* If that scope does not contain the scope in which the
15319          class was originally declared, the program is invalid.  */
15320       if (scope && !is_ancestor (scope, nested_name_specifier))
15321         {
15322           if (at_namespace_scope_p ())
15323             error ("%Hdeclaration of %qD in namespace %qD which does not "
15324                    "enclose %qD",
15325                    &type_start_token->location,
15326                    type, scope, nested_name_specifier);
15327           else
15328             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15329                    &type_start_token->location,
15330                    type, scope, nested_name_specifier);
15331           type = NULL_TREE;
15332           goto done;
15333         }
15334       /* [dcl.meaning]
15335
15336          A declarator-id shall not be qualified except for the
15337          definition of a ... nested class outside of its class
15338          ... [or] the definition or explicit instantiation of a
15339          class member of a namespace outside of its namespace.  */
15340       if (scope == nested_name_specifier)
15341         {
15342           permerror (input_location, "%Hextra qualification not allowed",
15343                      &nested_name_specifier_token_start->location);
15344           nested_name_specifier = NULL_TREE;
15345           num_templates = 0;
15346         }
15347     }
15348   /* An explicit-specialization must be preceded by "template <>".  If
15349      it is not, try to recover gracefully.  */
15350   if (at_namespace_scope_p ()
15351       && parser->num_template_parameter_lists == 0
15352       && template_id_p)
15353     {
15354       error ("%Han explicit specialization must be preceded by %<template <>%>",
15355              &type_start_token->location);
15356       invalid_explicit_specialization_p = true;
15357       /* Take the same action that would have been taken by
15358          cp_parser_explicit_specialization.  */
15359       ++parser->num_template_parameter_lists;
15360       begin_specialization ();
15361     }
15362   /* There must be no "return" statements between this point and the
15363      end of this function; set "type "to the correct return value and
15364      use "goto done;" to return.  */
15365   /* Make sure that the right number of template parameters were
15366      present.  */
15367   if (!cp_parser_check_template_parameters (parser, num_templates,
15368                                             type_start_token->location))
15369     {
15370       /* If something went wrong, there is no point in even trying to
15371          process the class-definition.  */
15372       type = NULL_TREE;
15373       goto done;
15374     }
15375
15376   /* Look up the type.  */
15377   if (template_id_p)
15378     {
15379       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15380           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15381               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15382         {
15383           error ("%Hfunction template %qD redeclared as a class template",
15384                  &type_start_token->location, id);
15385           type = error_mark_node;
15386         }
15387       else
15388         {
15389           type = TREE_TYPE (id);
15390           type = maybe_process_partial_specialization (type);
15391         }
15392       if (nested_name_specifier)
15393         pushed_scope = push_scope (nested_name_specifier);
15394     }
15395   else if (nested_name_specifier)
15396     {
15397       tree class_type;
15398
15399       /* Given:
15400
15401             template <typename T> struct S { struct T };
15402             template <typename T> struct S<T>::T { };
15403
15404          we will get a TYPENAME_TYPE when processing the definition of
15405          `S::T'.  We need to resolve it to the actual type before we
15406          try to define it.  */
15407       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15408         {
15409           class_type = resolve_typename_type (TREE_TYPE (type),
15410                                               /*only_current_p=*/false);
15411           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15412             type = TYPE_NAME (class_type);
15413           else
15414             {
15415               cp_parser_error (parser, "could not resolve typename type");
15416               type = error_mark_node;
15417             }
15418         }
15419
15420       if (maybe_process_partial_specialization (TREE_TYPE (type))
15421           == error_mark_node)
15422         {
15423           type = NULL_TREE;
15424           goto done;
15425         }
15426
15427       class_type = current_class_type;
15428       /* Enter the scope indicated by the nested-name-specifier.  */
15429       pushed_scope = push_scope (nested_name_specifier);
15430       /* Get the canonical version of this type.  */
15431       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15432       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15433           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15434         {
15435           type = push_template_decl (type);
15436           if (type == error_mark_node)
15437             {
15438               type = NULL_TREE;
15439               goto done;
15440             }
15441         }
15442
15443       type = TREE_TYPE (type);
15444       *nested_name_specifier_p = true;
15445     }
15446   else      /* The name is not a nested name.  */
15447     {
15448       /* If the class was unnamed, create a dummy name.  */
15449       if (!id)
15450         id = make_anon_name ();
15451       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15452                        parser->num_template_parameter_lists);
15453     }
15454
15455   /* Indicate whether this class was declared as a `class' or as a
15456      `struct'.  */
15457   if (TREE_CODE (type) == RECORD_TYPE)
15458     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15459   cp_parser_check_class_key (class_key, type);
15460
15461   /* If this type was already complete, and we see another definition,
15462      that's an error.  */
15463   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15464     {
15465       error ("%Hredefinition of %q#T",
15466              &type_start_token->location, type);
15467       error ("%Hprevious definition of %q+#T",
15468              &type_start_token->location, type);
15469       type = NULL_TREE;
15470       goto done;
15471     }
15472   else if (type == error_mark_node)
15473     type = NULL_TREE;
15474
15475   /* We will have entered the scope containing the class; the names of
15476      base classes should be looked up in that context.  For example:
15477
15478        struct A { struct B {}; struct C; };
15479        struct A::C : B {};
15480
15481      is valid.  */
15482
15483   /* Get the list of base-classes, if there is one.  */
15484   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15485     *bases = cp_parser_base_clause (parser);
15486
15487  done:
15488   /* Leave the scope given by the nested-name-specifier.  We will
15489      enter the class scope itself while processing the members.  */
15490   if (pushed_scope)
15491     pop_scope (pushed_scope);
15492
15493   if (invalid_explicit_specialization_p)
15494     {
15495       end_specialization ();
15496       --parser->num_template_parameter_lists;
15497     }
15498   *attributes_p = attributes;
15499   return type;
15500 }
15501
15502 /* Parse a class-key.
15503
15504    class-key:
15505      class
15506      struct
15507      union
15508
15509    Returns the kind of class-key specified, or none_type to indicate
15510    error.  */
15511
15512 static enum tag_types
15513 cp_parser_class_key (cp_parser* parser)
15514 {
15515   cp_token *token;
15516   enum tag_types tag_type;
15517
15518   /* Look for the class-key.  */
15519   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15520   if (!token)
15521     return none_type;
15522
15523   /* Check to see if the TOKEN is a class-key.  */
15524   tag_type = cp_parser_token_is_class_key (token);
15525   if (!tag_type)
15526     cp_parser_error (parser, "expected class-key");
15527   return tag_type;
15528 }
15529
15530 /* Parse an (optional) member-specification.
15531
15532    member-specification:
15533      member-declaration member-specification [opt]
15534      access-specifier : member-specification [opt]  */
15535
15536 static void
15537 cp_parser_member_specification_opt (cp_parser* parser)
15538 {
15539   while (true)
15540     {
15541       cp_token *token;
15542       enum rid keyword;
15543
15544       /* Peek at the next token.  */
15545       token = cp_lexer_peek_token (parser->lexer);
15546       /* If it's a `}', or EOF then we've seen all the members.  */
15547       if (token->type == CPP_CLOSE_BRACE
15548           || token->type == CPP_EOF
15549           || token->type == CPP_PRAGMA_EOL)
15550         break;
15551
15552       /* See if this token is a keyword.  */
15553       keyword = token->keyword;
15554       switch (keyword)
15555         {
15556         case RID_PUBLIC:
15557         case RID_PROTECTED:
15558         case RID_PRIVATE:
15559           /* Consume the access-specifier.  */
15560           cp_lexer_consume_token (parser->lexer);
15561           /* Remember which access-specifier is active.  */
15562           current_access_specifier = token->u.value;
15563           /* Look for the `:'.  */
15564           cp_parser_require (parser, CPP_COLON, "%<:%>");
15565           break;
15566
15567         default:
15568           /* Accept #pragmas at class scope.  */
15569           if (token->type == CPP_PRAGMA)
15570             {
15571               cp_parser_pragma (parser, pragma_external);
15572               break;
15573             }
15574
15575           /* Otherwise, the next construction must be a
15576              member-declaration.  */
15577           cp_parser_member_declaration (parser);
15578         }
15579     }
15580 }
15581
15582 /* Parse a member-declaration.
15583
15584    member-declaration:
15585      decl-specifier-seq [opt] member-declarator-list [opt] ;
15586      function-definition ; [opt]
15587      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15588      using-declaration
15589      template-declaration
15590
15591    member-declarator-list:
15592      member-declarator
15593      member-declarator-list , member-declarator
15594
15595    member-declarator:
15596      declarator pure-specifier [opt]
15597      declarator constant-initializer [opt]
15598      identifier [opt] : constant-expression
15599
15600    GNU Extensions:
15601
15602    member-declaration:
15603      __extension__ member-declaration
15604
15605    member-declarator:
15606      declarator attributes [opt] pure-specifier [opt]
15607      declarator attributes [opt] constant-initializer [opt]
15608      identifier [opt] attributes [opt] : constant-expression  
15609
15610    C++0x Extensions:
15611
15612    member-declaration:
15613      static_assert-declaration  */
15614
15615 static void
15616 cp_parser_member_declaration (cp_parser* parser)
15617 {
15618   cp_decl_specifier_seq decl_specifiers;
15619   tree prefix_attributes;
15620   tree decl;
15621   int declares_class_or_enum;
15622   bool friend_p;
15623   cp_token *token = NULL;
15624   cp_token *decl_spec_token_start = NULL;
15625   cp_token *initializer_token_start = NULL;
15626   int saved_pedantic;
15627
15628   /* Check for the `__extension__' keyword.  */
15629   if (cp_parser_extension_opt (parser, &saved_pedantic))
15630     {
15631       /* Recurse.  */
15632       cp_parser_member_declaration (parser);
15633       /* Restore the old value of the PEDANTIC flag.  */
15634       pedantic = saved_pedantic;
15635
15636       return;
15637     }
15638
15639   /* Check for a template-declaration.  */
15640   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15641     {
15642       /* An explicit specialization here is an error condition, and we
15643          expect the specialization handler to detect and report this.  */
15644       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15645           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15646         cp_parser_explicit_specialization (parser);
15647       else
15648         cp_parser_template_declaration (parser, /*member_p=*/true);
15649
15650       return;
15651     }
15652
15653   /* Check for a using-declaration.  */
15654   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15655     {
15656       /* Parse the using-declaration.  */
15657       cp_parser_using_declaration (parser,
15658                                    /*access_declaration_p=*/false);
15659       return;
15660     }
15661
15662   /* Check for @defs.  */
15663   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15664     {
15665       tree ivar, member;
15666       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15667       ivar = ivar_chains;
15668       while (ivar)
15669         {
15670           member = ivar;
15671           ivar = TREE_CHAIN (member);
15672           TREE_CHAIN (member) = NULL_TREE;
15673           finish_member_declaration (member);
15674         }
15675       return;
15676     }
15677
15678   /* If the next token is `static_assert' we have a static assertion.  */
15679   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15680     {
15681       cp_parser_static_assert (parser, /*member_p=*/true);
15682       return;
15683     }
15684
15685   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15686     return;
15687
15688   /* Parse the decl-specifier-seq.  */
15689   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15690   cp_parser_decl_specifier_seq (parser,
15691                                 CP_PARSER_FLAGS_OPTIONAL,
15692                                 &decl_specifiers,
15693                                 &declares_class_or_enum);
15694   prefix_attributes = decl_specifiers.attributes;
15695   decl_specifiers.attributes = NULL_TREE;
15696   /* Check for an invalid type-name.  */
15697   if (!decl_specifiers.type
15698       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15699     return;
15700   /* If there is no declarator, then the decl-specifier-seq should
15701      specify a type.  */
15702   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15703     {
15704       /* If there was no decl-specifier-seq, and the next token is a
15705          `;', then we have something like:
15706
15707            struct S { ; };
15708
15709          [class.mem]
15710
15711          Each member-declaration shall declare at least one member
15712          name of the class.  */
15713       if (!decl_specifiers.any_specifiers_p)
15714         {
15715           cp_token *token = cp_lexer_peek_token (parser->lexer);
15716           if (!in_system_header_at (token->location))
15717             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15718         }
15719       else
15720         {
15721           tree type;
15722
15723           /* See if this declaration is a friend.  */
15724           friend_p = cp_parser_friend_p (&decl_specifiers);
15725           /* If there were decl-specifiers, check to see if there was
15726              a class-declaration.  */
15727           type = check_tag_decl (&decl_specifiers);
15728           /* Nested classes have already been added to the class, but
15729              a `friend' needs to be explicitly registered.  */
15730           if (friend_p)
15731             {
15732               /* If the `friend' keyword was present, the friend must
15733                  be introduced with a class-key.  */
15734                if (!declares_class_or_enum)
15735                  error ("%Ha class-key must be used when declaring a friend",
15736                         &decl_spec_token_start->location);
15737                /* In this case:
15738
15739                     template <typename T> struct A {
15740                       friend struct A<T>::B;
15741                     };
15742
15743                   A<T>::B will be represented by a TYPENAME_TYPE, and
15744                   therefore not recognized by check_tag_decl.  */
15745                if (!type
15746                    && decl_specifiers.type
15747                    && TYPE_P (decl_specifiers.type))
15748                  type = decl_specifiers.type;
15749                if (!type || !TYPE_P (type))
15750                  error ("%Hfriend declaration does not name a class or "
15751                         "function", &decl_spec_token_start->location);
15752                else
15753                  make_friend_class (current_class_type, type,
15754                                     /*complain=*/true);
15755             }
15756           /* If there is no TYPE, an error message will already have
15757              been issued.  */
15758           else if (!type || type == error_mark_node)
15759             ;
15760           /* An anonymous aggregate has to be handled specially; such
15761              a declaration really declares a data member (with a
15762              particular type), as opposed to a nested class.  */
15763           else if (ANON_AGGR_TYPE_P (type))
15764             {
15765               /* Remove constructors and such from TYPE, now that we
15766                  know it is an anonymous aggregate.  */
15767               fixup_anonymous_aggr (type);
15768               /* And make the corresponding data member.  */
15769               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15770               /* Add it to the class.  */
15771               finish_member_declaration (decl);
15772             }
15773           else
15774             cp_parser_check_access_in_redeclaration
15775                                               (TYPE_NAME (type),
15776                                                decl_spec_token_start->location);
15777         }
15778     }
15779   else
15780     {
15781       /* See if these declarations will be friends.  */
15782       friend_p = cp_parser_friend_p (&decl_specifiers);
15783
15784       /* Keep going until we hit the `;' at the end of the
15785          declaration.  */
15786       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15787         {
15788           tree attributes = NULL_TREE;
15789           tree first_attribute;
15790
15791           /* Peek at the next token.  */
15792           token = cp_lexer_peek_token (parser->lexer);
15793
15794           /* Check for a bitfield declaration.  */
15795           if (token->type == CPP_COLON
15796               || (token->type == CPP_NAME
15797                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15798                   == CPP_COLON))
15799             {
15800               tree identifier;
15801               tree width;
15802
15803               /* Get the name of the bitfield.  Note that we cannot just
15804                  check TOKEN here because it may have been invalidated by
15805                  the call to cp_lexer_peek_nth_token above.  */
15806               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15807                 identifier = cp_parser_identifier (parser);
15808               else
15809                 identifier = NULL_TREE;
15810
15811               /* Consume the `:' token.  */
15812               cp_lexer_consume_token (parser->lexer);
15813               /* Get the width of the bitfield.  */
15814               width
15815                 = cp_parser_constant_expression (parser,
15816                                                  /*allow_non_constant=*/false,
15817                                                  NULL);
15818
15819               /* Look for attributes that apply to the bitfield.  */
15820               attributes = cp_parser_attributes_opt (parser);
15821               /* Remember which attributes are prefix attributes and
15822                  which are not.  */
15823               first_attribute = attributes;
15824               /* Combine the attributes.  */
15825               attributes = chainon (prefix_attributes, attributes);
15826
15827               /* Create the bitfield declaration.  */
15828               decl = grokbitfield (identifier
15829                                    ? make_id_declarator (NULL_TREE,
15830                                                          identifier,
15831                                                          sfk_none)
15832                                    : NULL,
15833                                    &decl_specifiers,
15834                                    width,
15835                                    attributes);
15836             }
15837           else
15838             {
15839               cp_declarator *declarator;
15840               tree initializer;
15841               tree asm_specification;
15842               int ctor_dtor_or_conv_p;
15843
15844               /* Parse the declarator.  */
15845               declarator
15846                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15847                                         &ctor_dtor_or_conv_p,
15848                                         /*parenthesized_p=*/NULL,
15849                                         /*member_p=*/true);
15850
15851               /* If something went wrong parsing the declarator, make sure
15852                  that we at least consume some tokens.  */
15853               if (declarator == cp_error_declarator)
15854                 {
15855                   /* Skip to the end of the statement.  */
15856                   cp_parser_skip_to_end_of_statement (parser);
15857                   /* If the next token is not a semicolon, that is
15858                      probably because we just skipped over the body of
15859                      a function.  So, we consume a semicolon if
15860                      present, but do not issue an error message if it
15861                      is not present.  */
15862                   if (cp_lexer_next_token_is (parser->lexer,
15863                                               CPP_SEMICOLON))
15864                     cp_lexer_consume_token (parser->lexer);
15865                   return;
15866                 }
15867
15868               if (declares_class_or_enum & 2)
15869                 cp_parser_check_for_definition_in_return_type
15870                                             (declarator, decl_specifiers.type,
15871                                              decl_specifiers.type_location);
15872
15873               /* Look for an asm-specification.  */
15874               asm_specification = cp_parser_asm_specification_opt (parser);
15875               /* Look for attributes that apply to the declaration.  */
15876               attributes = cp_parser_attributes_opt (parser);
15877               /* Remember which attributes are prefix attributes and
15878                  which are not.  */
15879               first_attribute = attributes;
15880               /* Combine the attributes.  */
15881               attributes = chainon (prefix_attributes, attributes);
15882
15883               /* If it's an `=', then we have a constant-initializer or a
15884                  pure-specifier.  It is not correct to parse the
15885                  initializer before registering the member declaration
15886                  since the member declaration should be in scope while
15887                  its initializer is processed.  However, the rest of the
15888                  front end does not yet provide an interface that allows
15889                  us to handle this correctly.  */
15890               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15891                 {
15892                   /* In [class.mem]:
15893
15894                      A pure-specifier shall be used only in the declaration of
15895                      a virtual function.
15896
15897                      A member-declarator can contain a constant-initializer
15898                      only if it declares a static member of integral or
15899                      enumeration type.
15900
15901                      Therefore, if the DECLARATOR is for a function, we look
15902                      for a pure-specifier; otherwise, we look for a
15903                      constant-initializer.  When we call `grokfield', it will
15904                      perform more stringent semantics checks.  */
15905                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15906                   if (function_declarator_p (declarator))
15907                     initializer = cp_parser_pure_specifier (parser);
15908                   else
15909                     /* Parse the initializer.  */
15910                     initializer = cp_parser_constant_initializer (parser);
15911                 }
15912               /* Otherwise, there is no initializer.  */
15913               else
15914                 initializer = NULL_TREE;
15915
15916               /* See if we are probably looking at a function
15917                  definition.  We are certainly not looking at a
15918                  member-declarator.  Calling `grokfield' has
15919                  side-effects, so we must not do it unless we are sure
15920                  that we are looking at a member-declarator.  */
15921               if (cp_parser_token_starts_function_definition_p
15922                   (cp_lexer_peek_token (parser->lexer)))
15923                 {
15924                   /* The grammar does not allow a pure-specifier to be
15925                      used when a member function is defined.  (It is
15926                      possible that this fact is an oversight in the
15927                      standard, since a pure function may be defined
15928                      outside of the class-specifier.  */
15929                   if (initializer)
15930                     error ("%Hpure-specifier on function-definition",
15931                            &initializer_token_start->location);
15932                   decl = cp_parser_save_member_function_body (parser,
15933                                                               &decl_specifiers,
15934                                                               declarator,
15935                                                               attributes);
15936                   /* If the member was not a friend, declare it here.  */
15937                   if (!friend_p)
15938                     finish_member_declaration (decl);
15939                   /* Peek at the next token.  */
15940                   token = cp_lexer_peek_token (parser->lexer);
15941                   /* If the next token is a semicolon, consume it.  */
15942                   if (token->type == CPP_SEMICOLON)
15943                     cp_lexer_consume_token (parser->lexer);
15944                   return;
15945                 }
15946               else
15947                 if (declarator->kind == cdk_function)
15948                   declarator->id_loc = token->location;
15949                 /* Create the declaration.  */
15950                 decl = grokfield (declarator, &decl_specifiers,
15951                                   initializer, /*init_const_expr_p=*/true,
15952                                   asm_specification,
15953                                   attributes);
15954             }
15955
15956           /* Reset PREFIX_ATTRIBUTES.  */
15957           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15958             attributes = TREE_CHAIN (attributes);
15959           if (attributes)
15960             TREE_CHAIN (attributes) = NULL_TREE;
15961
15962           /* If there is any qualification still in effect, clear it
15963              now; we will be starting fresh with the next declarator.  */
15964           parser->scope = NULL_TREE;
15965           parser->qualifying_scope = NULL_TREE;
15966           parser->object_scope = NULL_TREE;
15967           /* If it's a `,', then there are more declarators.  */
15968           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15969             cp_lexer_consume_token (parser->lexer);
15970           /* If the next token isn't a `;', then we have a parse error.  */
15971           else if (cp_lexer_next_token_is_not (parser->lexer,
15972                                                CPP_SEMICOLON))
15973             {
15974               cp_parser_error (parser, "expected %<;%>");
15975               /* Skip tokens until we find a `;'.  */
15976               cp_parser_skip_to_end_of_statement (parser);
15977
15978               break;
15979             }
15980
15981           if (decl)
15982             {
15983               /* Add DECL to the list of members.  */
15984               if (!friend_p)
15985                 finish_member_declaration (decl);
15986
15987               if (TREE_CODE (decl) == FUNCTION_DECL)
15988                 cp_parser_save_default_args (parser, decl);
15989             }
15990         }
15991     }
15992
15993   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15994 }
15995
15996 /* Parse a pure-specifier.
15997
15998    pure-specifier:
15999      = 0
16000
16001    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16002    Otherwise, ERROR_MARK_NODE is returned.  */
16003
16004 static tree
16005 cp_parser_pure_specifier (cp_parser* parser)
16006 {
16007   cp_token *token;
16008
16009   /* Look for the `=' token.  */
16010   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16011     return error_mark_node;
16012   /* Look for the `0' token.  */
16013   token = cp_lexer_peek_token (parser->lexer);
16014
16015   if (token->type == CPP_EOF
16016       || token->type == CPP_PRAGMA_EOL)
16017     return error_mark_node;
16018
16019   cp_lexer_consume_token (parser->lexer);
16020
16021   /* Accept = default or = delete in c++0x mode.  */
16022   if (token->keyword == RID_DEFAULT
16023       || token->keyword == RID_DELETE)
16024     {
16025       maybe_warn_cpp0x ("defaulted and deleted functions");
16026       return token->u.value;
16027     }
16028
16029   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16030   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16031     {
16032       cp_parser_error (parser,
16033                        "invalid pure specifier (only %<= 0%> is allowed)");
16034       cp_parser_skip_to_end_of_statement (parser);
16035       return error_mark_node;
16036     }
16037   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16038     {
16039       error ("%Htemplates may not be %<virtual%>", &token->location);
16040       return error_mark_node;
16041     }
16042
16043   return integer_zero_node;
16044 }
16045
16046 /* Parse a constant-initializer.
16047
16048    constant-initializer:
16049      = constant-expression
16050
16051    Returns a representation of the constant-expression.  */
16052
16053 static tree
16054 cp_parser_constant_initializer (cp_parser* parser)
16055 {
16056   /* Look for the `=' token.  */
16057   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16058     return error_mark_node;
16059
16060   /* It is invalid to write:
16061
16062        struct S { static const int i = { 7 }; };
16063
16064      */
16065   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16066     {
16067       cp_parser_error (parser,
16068                        "a brace-enclosed initializer is not allowed here");
16069       /* Consume the opening brace.  */
16070       cp_lexer_consume_token (parser->lexer);
16071       /* Skip the initializer.  */
16072       cp_parser_skip_to_closing_brace (parser);
16073       /* Look for the trailing `}'.  */
16074       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16075
16076       return error_mark_node;
16077     }
16078
16079   return cp_parser_constant_expression (parser,
16080                                         /*allow_non_constant=*/false,
16081                                         NULL);
16082 }
16083
16084 /* Derived classes [gram.class.derived] */
16085
16086 /* Parse a base-clause.
16087
16088    base-clause:
16089      : base-specifier-list
16090
16091    base-specifier-list:
16092      base-specifier ... [opt]
16093      base-specifier-list , base-specifier ... [opt]
16094
16095    Returns a TREE_LIST representing the base-classes, in the order in
16096    which they were declared.  The representation of each node is as
16097    described by cp_parser_base_specifier.
16098
16099    In the case that no bases are specified, this function will return
16100    NULL_TREE, not ERROR_MARK_NODE.  */
16101
16102 static tree
16103 cp_parser_base_clause (cp_parser* parser)
16104 {
16105   tree bases = NULL_TREE;
16106
16107   /* Look for the `:' that begins the list.  */
16108   cp_parser_require (parser, CPP_COLON, "%<:%>");
16109
16110   /* Scan the base-specifier-list.  */
16111   while (true)
16112     {
16113       cp_token *token;
16114       tree base;
16115       bool pack_expansion_p = false;
16116
16117       /* Look for the base-specifier.  */
16118       base = cp_parser_base_specifier (parser);
16119       /* Look for the (optional) ellipsis. */
16120       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16121         {
16122           /* Consume the `...'. */
16123           cp_lexer_consume_token (parser->lexer);
16124
16125           pack_expansion_p = true;
16126         }
16127
16128       /* Add BASE to the front of the list.  */
16129       if (base != error_mark_node)
16130         {
16131           if (pack_expansion_p)
16132             /* Make this a pack expansion type. */
16133             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16134           
16135
16136           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16137             {
16138               TREE_CHAIN (base) = bases;
16139               bases = base;
16140             }
16141         }
16142       /* Peek at the next token.  */
16143       token = cp_lexer_peek_token (parser->lexer);
16144       /* If it's not a comma, then the list is complete.  */
16145       if (token->type != CPP_COMMA)
16146         break;
16147       /* Consume the `,'.  */
16148       cp_lexer_consume_token (parser->lexer);
16149     }
16150
16151   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16152      base class had a qualified name.  However, the next name that
16153      appears is certainly not qualified.  */
16154   parser->scope = NULL_TREE;
16155   parser->qualifying_scope = NULL_TREE;
16156   parser->object_scope = NULL_TREE;
16157
16158   return nreverse (bases);
16159 }
16160
16161 /* Parse a base-specifier.
16162
16163    base-specifier:
16164      :: [opt] nested-name-specifier [opt] class-name
16165      virtual access-specifier [opt] :: [opt] nested-name-specifier
16166        [opt] class-name
16167      access-specifier virtual [opt] :: [opt] nested-name-specifier
16168        [opt] class-name
16169
16170    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16171    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16172    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16173    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16174
16175 static tree
16176 cp_parser_base_specifier (cp_parser* parser)
16177 {
16178   cp_token *token;
16179   bool done = false;
16180   bool virtual_p = false;
16181   bool duplicate_virtual_error_issued_p = false;
16182   bool duplicate_access_error_issued_p = false;
16183   bool class_scope_p, template_p;
16184   tree access = access_default_node;
16185   tree type;
16186
16187   /* Process the optional `virtual' and `access-specifier'.  */
16188   while (!done)
16189     {
16190       /* Peek at the next token.  */
16191       token = cp_lexer_peek_token (parser->lexer);
16192       /* Process `virtual'.  */
16193       switch (token->keyword)
16194         {
16195         case RID_VIRTUAL:
16196           /* If `virtual' appears more than once, issue an error.  */
16197           if (virtual_p && !duplicate_virtual_error_issued_p)
16198             {
16199               cp_parser_error (parser,
16200                                "%<virtual%> specified more than once in base-specified");
16201               duplicate_virtual_error_issued_p = true;
16202             }
16203
16204           virtual_p = true;
16205
16206           /* Consume the `virtual' token.  */
16207           cp_lexer_consume_token (parser->lexer);
16208
16209           break;
16210
16211         case RID_PUBLIC:
16212         case RID_PROTECTED:
16213         case RID_PRIVATE:
16214           /* If more than one access specifier appears, issue an
16215              error.  */
16216           if (access != access_default_node
16217               && !duplicate_access_error_issued_p)
16218             {
16219               cp_parser_error (parser,
16220                                "more than one access specifier in base-specified");
16221               duplicate_access_error_issued_p = true;
16222             }
16223
16224           access = ridpointers[(int) token->keyword];
16225
16226           /* Consume the access-specifier.  */
16227           cp_lexer_consume_token (parser->lexer);
16228
16229           break;
16230
16231         default:
16232           done = true;
16233           break;
16234         }
16235     }
16236   /* It is not uncommon to see programs mechanically, erroneously, use
16237      the 'typename' keyword to denote (dependent) qualified types
16238      as base classes.  */
16239   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16240     {
16241       token = cp_lexer_peek_token (parser->lexer);
16242       if (!processing_template_decl)
16243         error ("%Hkeyword %<typename%> not allowed outside of templates",
16244                &token->location);
16245       else
16246         error ("%Hkeyword %<typename%> not allowed in this context "
16247                "(the base class is implicitly a type)",
16248                &token->location);
16249       cp_lexer_consume_token (parser->lexer);
16250     }
16251
16252   /* Look for the optional `::' operator.  */
16253   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16254   /* Look for the nested-name-specifier.  The simplest way to
16255      implement:
16256
16257        [temp.res]
16258
16259        The keyword `typename' is not permitted in a base-specifier or
16260        mem-initializer; in these contexts a qualified name that
16261        depends on a template-parameter is implicitly assumed to be a
16262        type name.
16263
16264      is to pretend that we have seen the `typename' keyword at this
16265      point.  */
16266   cp_parser_nested_name_specifier_opt (parser,
16267                                        /*typename_keyword_p=*/true,
16268                                        /*check_dependency_p=*/true,
16269                                        typename_type,
16270                                        /*is_declaration=*/true);
16271   /* If the base class is given by a qualified name, assume that names
16272      we see are type names or templates, as appropriate.  */
16273   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16274   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16275
16276   /* Finally, look for the class-name.  */
16277   type = cp_parser_class_name (parser,
16278                                class_scope_p,
16279                                template_p,
16280                                typename_type,
16281                                /*check_dependency_p=*/true,
16282                                /*class_head_p=*/false,
16283                                /*is_declaration=*/true);
16284
16285   if (type == error_mark_node)
16286     return error_mark_node;
16287
16288   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16289 }
16290
16291 /* Exception handling [gram.exception] */
16292
16293 /* Parse an (optional) exception-specification.
16294
16295    exception-specification:
16296      throw ( type-id-list [opt] )
16297
16298    Returns a TREE_LIST representing the exception-specification.  The
16299    TREE_VALUE of each node is a type.  */
16300
16301 static tree
16302 cp_parser_exception_specification_opt (cp_parser* parser)
16303 {
16304   cp_token *token;
16305   tree type_id_list;
16306
16307   /* Peek at the next token.  */
16308   token = cp_lexer_peek_token (parser->lexer);
16309   /* If it's not `throw', then there's no exception-specification.  */
16310   if (!cp_parser_is_keyword (token, RID_THROW))
16311     return NULL_TREE;
16312
16313   /* Consume the `throw'.  */
16314   cp_lexer_consume_token (parser->lexer);
16315
16316   /* Look for the `('.  */
16317   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16318
16319   /* Peek at the next token.  */
16320   token = cp_lexer_peek_token (parser->lexer);
16321   /* If it's not a `)', then there is a type-id-list.  */
16322   if (token->type != CPP_CLOSE_PAREN)
16323     {
16324       const char *saved_message;
16325
16326       /* Types may not be defined in an exception-specification.  */
16327       saved_message = parser->type_definition_forbidden_message;
16328       parser->type_definition_forbidden_message
16329         = "types may not be defined in an exception-specification";
16330       /* Parse the type-id-list.  */
16331       type_id_list = cp_parser_type_id_list (parser);
16332       /* Restore the saved message.  */
16333       parser->type_definition_forbidden_message = saved_message;
16334     }
16335   else
16336     type_id_list = empty_except_spec;
16337
16338   /* Look for the `)'.  */
16339   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16340
16341   return type_id_list;
16342 }
16343
16344 /* Parse an (optional) type-id-list.
16345
16346    type-id-list:
16347      type-id ... [opt]
16348      type-id-list , type-id ... [opt]
16349
16350    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16351    in the order that the types were presented.  */
16352
16353 static tree
16354 cp_parser_type_id_list (cp_parser* parser)
16355 {
16356   tree types = NULL_TREE;
16357
16358   while (true)
16359     {
16360       cp_token *token;
16361       tree type;
16362
16363       /* Get the next type-id.  */
16364       type = cp_parser_type_id (parser);
16365       /* Parse the optional ellipsis. */
16366       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16367         {
16368           /* Consume the `...'. */
16369           cp_lexer_consume_token (parser->lexer);
16370
16371           /* Turn the type into a pack expansion expression. */
16372           type = make_pack_expansion (type);
16373         }
16374       /* Add it to the list.  */
16375       types = add_exception_specifier (types, type, /*complain=*/1);
16376       /* Peek at the next token.  */
16377       token = cp_lexer_peek_token (parser->lexer);
16378       /* If it is not a `,', we are done.  */
16379       if (token->type != CPP_COMMA)
16380         break;
16381       /* Consume the `,'.  */
16382       cp_lexer_consume_token (parser->lexer);
16383     }
16384
16385   return nreverse (types);
16386 }
16387
16388 /* Parse a try-block.
16389
16390    try-block:
16391      try compound-statement handler-seq  */
16392
16393 static tree
16394 cp_parser_try_block (cp_parser* parser)
16395 {
16396   tree try_block;
16397
16398   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16399   try_block = begin_try_block ();
16400   cp_parser_compound_statement (parser, NULL, true);
16401   finish_try_block (try_block);
16402   cp_parser_handler_seq (parser);
16403   finish_handler_sequence (try_block);
16404
16405   return try_block;
16406 }
16407
16408 /* Parse a function-try-block.
16409
16410    function-try-block:
16411      try ctor-initializer [opt] function-body handler-seq  */
16412
16413 static bool
16414 cp_parser_function_try_block (cp_parser* parser)
16415 {
16416   tree compound_stmt;
16417   tree try_block;
16418   bool ctor_initializer_p;
16419
16420   /* Look for the `try' keyword.  */
16421   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16422     return false;
16423   /* Let the rest of the front end know where we are.  */
16424   try_block = begin_function_try_block (&compound_stmt);
16425   /* Parse the function-body.  */
16426   ctor_initializer_p
16427     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16428   /* We're done with the `try' part.  */
16429   finish_function_try_block (try_block);
16430   /* Parse the handlers.  */
16431   cp_parser_handler_seq (parser);
16432   /* We're done with the handlers.  */
16433   finish_function_handler_sequence (try_block, compound_stmt);
16434
16435   return ctor_initializer_p;
16436 }
16437
16438 /* Parse a handler-seq.
16439
16440    handler-seq:
16441      handler handler-seq [opt]  */
16442
16443 static void
16444 cp_parser_handler_seq (cp_parser* parser)
16445 {
16446   while (true)
16447     {
16448       cp_token *token;
16449
16450       /* Parse the handler.  */
16451       cp_parser_handler (parser);
16452       /* Peek at the next token.  */
16453       token = cp_lexer_peek_token (parser->lexer);
16454       /* If it's not `catch' then there are no more handlers.  */
16455       if (!cp_parser_is_keyword (token, RID_CATCH))
16456         break;
16457     }
16458 }
16459
16460 /* Parse a handler.
16461
16462    handler:
16463      catch ( exception-declaration ) compound-statement  */
16464
16465 static void
16466 cp_parser_handler (cp_parser* parser)
16467 {
16468   tree handler;
16469   tree declaration;
16470
16471   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16472   handler = begin_handler ();
16473   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16474   declaration = cp_parser_exception_declaration (parser);
16475   finish_handler_parms (declaration, handler);
16476   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16477   cp_parser_compound_statement (parser, NULL, false);
16478   finish_handler (handler);
16479 }
16480
16481 /* Parse an exception-declaration.
16482
16483    exception-declaration:
16484      type-specifier-seq declarator
16485      type-specifier-seq abstract-declarator
16486      type-specifier-seq
16487      ...
16488
16489    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16490    ellipsis variant is used.  */
16491
16492 static tree
16493 cp_parser_exception_declaration (cp_parser* parser)
16494 {
16495   cp_decl_specifier_seq type_specifiers;
16496   cp_declarator *declarator;
16497   const char *saved_message;
16498
16499   /* If it's an ellipsis, it's easy to handle.  */
16500   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16501     {
16502       /* Consume the `...' token.  */
16503       cp_lexer_consume_token (parser->lexer);
16504       return NULL_TREE;
16505     }
16506
16507   /* Types may not be defined in exception-declarations.  */
16508   saved_message = parser->type_definition_forbidden_message;
16509   parser->type_definition_forbidden_message
16510     = "types may not be defined in exception-declarations";
16511
16512   /* Parse the type-specifier-seq.  */
16513   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16514                                 &type_specifiers);
16515   /* If it's a `)', then there is no declarator.  */
16516   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16517     declarator = NULL;
16518   else
16519     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16520                                        /*ctor_dtor_or_conv_p=*/NULL,
16521                                        /*parenthesized_p=*/NULL,
16522                                        /*member_p=*/false);
16523
16524   /* Restore the saved message.  */
16525   parser->type_definition_forbidden_message = saved_message;
16526
16527   if (!type_specifiers.any_specifiers_p)
16528     return error_mark_node;
16529
16530   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16531 }
16532
16533 /* Parse a throw-expression.
16534
16535    throw-expression:
16536      throw assignment-expression [opt]
16537
16538    Returns a THROW_EXPR representing the throw-expression.  */
16539
16540 static tree
16541 cp_parser_throw_expression (cp_parser* parser)
16542 {
16543   tree expression;
16544   cp_token* token;
16545
16546   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16547   token = cp_lexer_peek_token (parser->lexer);
16548   /* Figure out whether or not there is an assignment-expression
16549      following the "throw" keyword.  */
16550   if (token->type == CPP_COMMA
16551       || token->type == CPP_SEMICOLON
16552       || token->type == CPP_CLOSE_PAREN
16553       || token->type == CPP_CLOSE_SQUARE
16554       || token->type == CPP_CLOSE_BRACE
16555       || token->type == CPP_COLON)
16556     expression = NULL_TREE;
16557   else
16558     expression = cp_parser_assignment_expression (parser,
16559                                                   /*cast_p=*/false, NULL);
16560
16561   return build_throw (expression);
16562 }
16563
16564 /* GNU Extensions */
16565
16566 /* Parse an (optional) asm-specification.
16567
16568    asm-specification:
16569      asm ( string-literal )
16570
16571    If the asm-specification is present, returns a STRING_CST
16572    corresponding to the string-literal.  Otherwise, returns
16573    NULL_TREE.  */
16574
16575 static tree
16576 cp_parser_asm_specification_opt (cp_parser* parser)
16577 {
16578   cp_token *token;
16579   tree asm_specification;
16580
16581   /* Peek at the next token.  */
16582   token = cp_lexer_peek_token (parser->lexer);
16583   /* If the next token isn't the `asm' keyword, then there's no
16584      asm-specification.  */
16585   if (!cp_parser_is_keyword (token, RID_ASM))
16586     return NULL_TREE;
16587
16588   /* Consume the `asm' token.  */
16589   cp_lexer_consume_token (parser->lexer);
16590   /* Look for the `('.  */
16591   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16592
16593   /* Look for the string-literal.  */
16594   asm_specification = cp_parser_string_literal (parser, false, false);
16595
16596   /* Look for the `)'.  */
16597   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16598
16599   return asm_specification;
16600 }
16601
16602 /* Parse an asm-operand-list.
16603
16604    asm-operand-list:
16605      asm-operand
16606      asm-operand-list , asm-operand
16607
16608    asm-operand:
16609      string-literal ( expression )
16610      [ string-literal ] string-literal ( expression )
16611
16612    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16613    each node is the expression.  The TREE_PURPOSE is itself a
16614    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16615    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16616    is a STRING_CST for the string literal before the parenthesis. Returns
16617    ERROR_MARK_NODE if any of the operands are invalid.  */
16618
16619 static tree
16620 cp_parser_asm_operand_list (cp_parser* parser)
16621 {
16622   tree asm_operands = NULL_TREE;
16623   bool invalid_operands = false;
16624
16625   while (true)
16626     {
16627       tree string_literal;
16628       tree expression;
16629       tree name;
16630
16631       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16632         {
16633           /* Consume the `[' token.  */
16634           cp_lexer_consume_token (parser->lexer);
16635           /* Read the operand name.  */
16636           name = cp_parser_identifier (parser);
16637           if (name != error_mark_node)
16638             name = build_string (IDENTIFIER_LENGTH (name),
16639                                  IDENTIFIER_POINTER (name));
16640           /* Look for the closing `]'.  */
16641           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16642         }
16643       else
16644         name = NULL_TREE;
16645       /* Look for the string-literal.  */
16646       string_literal = cp_parser_string_literal (parser, false, false);
16647
16648       /* Look for the `('.  */
16649       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16650       /* Parse the expression.  */
16651       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16652       /* Look for the `)'.  */
16653       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16654
16655       if (name == error_mark_node 
16656           || string_literal == error_mark_node 
16657           || expression == error_mark_node)
16658         invalid_operands = true;
16659
16660       /* Add this operand to the list.  */
16661       asm_operands = tree_cons (build_tree_list (name, string_literal),
16662                                 expression,
16663                                 asm_operands);
16664       /* If the next token is not a `,', there are no more
16665          operands.  */
16666       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16667         break;
16668       /* Consume the `,'.  */
16669       cp_lexer_consume_token (parser->lexer);
16670     }
16671
16672   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16673 }
16674
16675 /* Parse an asm-clobber-list.
16676
16677    asm-clobber-list:
16678      string-literal
16679      asm-clobber-list , string-literal
16680
16681    Returns a TREE_LIST, indicating the clobbers in the order that they
16682    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16683
16684 static tree
16685 cp_parser_asm_clobber_list (cp_parser* parser)
16686 {
16687   tree clobbers = NULL_TREE;
16688
16689   while (true)
16690     {
16691       tree string_literal;
16692
16693       /* Look for the string literal.  */
16694       string_literal = cp_parser_string_literal (parser, false, false);
16695       /* Add it to the list.  */
16696       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16697       /* If the next token is not a `,', then the list is
16698          complete.  */
16699       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16700         break;
16701       /* Consume the `,' token.  */
16702       cp_lexer_consume_token (parser->lexer);
16703     }
16704
16705   return clobbers;
16706 }
16707
16708 /* Parse an (optional) series of attributes.
16709
16710    attributes:
16711      attributes attribute
16712
16713    attribute:
16714      __attribute__ (( attribute-list [opt] ))
16715
16716    The return value is as for cp_parser_attribute_list.  */
16717
16718 static tree
16719 cp_parser_attributes_opt (cp_parser* parser)
16720 {
16721   tree attributes = NULL_TREE;
16722
16723   while (true)
16724     {
16725       cp_token *token;
16726       tree attribute_list;
16727
16728       /* Peek at the next token.  */
16729       token = cp_lexer_peek_token (parser->lexer);
16730       /* If it's not `__attribute__', then we're done.  */
16731       if (token->keyword != RID_ATTRIBUTE)
16732         break;
16733
16734       /* Consume the `__attribute__' keyword.  */
16735       cp_lexer_consume_token (parser->lexer);
16736       /* Look for the two `(' tokens.  */
16737       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16738       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16739
16740       /* Peek at the next token.  */
16741       token = cp_lexer_peek_token (parser->lexer);
16742       if (token->type != CPP_CLOSE_PAREN)
16743         /* Parse the attribute-list.  */
16744         attribute_list = cp_parser_attribute_list (parser);
16745       else
16746         /* If the next token is a `)', then there is no attribute
16747            list.  */
16748         attribute_list = NULL;
16749
16750       /* Look for the two `)' tokens.  */
16751       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16752       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16753
16754       /* Add these new attributes to the list.  */
16755       attributes = chainon (attributes, attribute_list);
16756     }
16757
16758   return attributes;
16759 }
16760
16761 /* Parse an attribute-list.
16762
16763    attribute-list:
16764      attribute
16765      attribute-list , attribute
16766
16767    attribute:
16768      identifier
16769      identifier ( identifier )
16770      identifier ( identifier , expression-list )
16771      identifier ( expression-list )
16772
16773    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16774    to an attribute.  The TREE_PURPOSE of each node is the identifier
16775    indicating which attribute is in use.  The TREE_VALUE represents
16776    the arguments, if any.  */
16777
16778 static tree
16779 cp_parser_attribute_list (cp_parser* parser)
16780 {
16781   tree attribute_list = NULL_TREE;
16782   bool save_translate_strings_p = parser->translate_strings_p;
16783
16784   parser->translate_strings_p = false;
16785   while (true)
16786     {
16787       cp_token *token;
16788       tree identifier;
16789       tree attribute;
16790
16791       /* Look for the identifier.  We also allow keywords here; for
16792          example `__attribute__ ((const))' is legal.  */
16793       token = cp_lexer_peek_token (parser->lexer);
16794       if (token->type == CPP_NAME
16795           || token->type == CPP_KEYWORD)
16796         {
16797           tree arguments = NULL_TREE;
16798
16799           /* Consume the token.  */
16800           token = cp_lexer_consume_token (parser->lexer);
16801
16802           /* Save away the identifier that indicates which attribute
16803              this is.  */
16804           identifier = token->u.value;
16805           attribute = build_tree_list (identifier, NULL_TREE);
16806
16807           /* Peek at the next token.  */
16808           token = cp_lexer_peek_token (parser->lexer);
16809           /* If it's an `(', then parse the attribute arguments.  */
16810           if (token->type == CPP_OPEN_PAREN)
16811             {
16812               arguments = cp_parser_parenthesized_expression_list
16813                           (parser, true, /*cast_p=*/false,
16814                            /*allow_expansion_p=*/false,
16815                            /*non_constant_p=*/NULL);
16816               /* Save the arguments away.  */
16817               TREE_VALUE (attribute) = arguments;
16818             }
16819
16820           if (arguments != error_mark_node)
16821             {
16822               /* Add this attribute to the list.  */
16823               TREE_CHAIN (attribute) = attribute_list;
16824               attribute_list = attribute;
16825             }
16826
16827           token = cp_lexer_peek_token (parser->lexer);
16828         }
16829       /* Now, look for more attributes.  If the next token isn't a
16830          `,', we're done.  */
16831       if (token->type != CPP_COMMA)
16832         break;
16833
16834       /* Consume the comma and keep going.  */
16835       cp_lexer_consume_token (parser->lexer);
16836     }
16837   parser->translate_strings_p = save_translate_strings_p;
16838
16839   /* We built up the list in reverse order.  */
16840   return nreverse (attribute_list);
16841 }
16842
16843 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16844    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16845    current value of the PEDANTIC flag, regardless of whether or not
16846    the `__extension__' keyword is present.  The caller is responsible
16847    for restoring the value of the PEDANTIC flag.  */
16848
16849 static bool
16850 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16851 {
16852   /* Save the old value of the PEDANTIC flag.  */
16853   *saved_pedantic = pedantic;
16854
16855   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16856     {
16857       /* Consume the `__extension__' token.  */
16858       cp_lexer_consume_token (parser->lexer);
16859       /* We're not being pedantic while the `__extension__' keyword is
16860          in effect.  */
16861       pedantic = 0;
16862
16863       return true;
16864     }
16865
16866   return false;
16867 }
16868
16869 /* Parse a label declaration.
16870
16871    label-declaration:
16872      __label__ label-declarator-seq ;
16873
16874    label-declarator-seq:
16875      identifier , label-declarator-seq
16876      identifier  */
16877
16878 static void
16879 cp_parser_label_declaration (cp_parser* parser)
16880 {
16881   /* Look for the `__label__' keyword.  */
16882   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16883
16884   while (true)
16885     {
16886       tree identifier;
16887
16888       /* Look for an identifier.  */
16889       identifier = cp_parser_identifier (parser);
16890       /* If we failed, stop.  */
16891       if (identifier == error_mark_node)
16892         break;
16893       /* Declare it as a label.  */
16894       finish_label_decl (identifier);
16895       /* If the next token is a `;', stop.  */
16896       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16897         break;
16898       /* Look for the `,' separating the label declarations.  */
16899       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16900     }
16901
16902   /* Look for the final `;'.  */
16903   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16904 }
16905
16906 /* Support Functions */
16907
16908 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16909    NAME should have one of the representations used for an
16910    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16911    is returned.  If PARSER->SCOPE is a dependent type, then a
16912    SCOPE_REF is returned.
16913
16914    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16915    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16916    was formed.  Abstractly, such entities should not be passed to this
16917    function, because they do not need to be looked up, but it is
16918    simpler to check for this special case here, rather than at the
16919    call-sites.
16920
16921    In cases not explicitly covered above, this function returns a
16922    DECL, OVERLOAD, or baselink representing the result of the lookup.
16923    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16924    is returned.
16925
16926    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16927    (e.g., "struct") that was used.  In that case bindings that do not
16928    refer to types are ignored.
16929
16930    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16931    ignored.
16932
16933    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16934    are ignored.
16935
16936    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16937    types.
16938
16939    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16940    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16941    NULL_TREE otherwise.  */
16942
16943 static tree
16944 cp_parser_lookup_name (cp_parser *parser, tree name,
16945                        enum tag_types tag_type,
16946                        bool is_template,
16947                        bool is_namespace,
16948                        bool check_dependency,
16949                        tree *ambiguous_decls,
16950                        location_t name_location)
16951 {
16952   int flags = 0;
16953   tree decl;
16954   tree object_type = parser->context->object_type;
16955
16956   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16957     flags |= LOOKUP_COMPLAIN;
16958
16959   /* Assume that the lookup will be unambiguous.  */
16960   if (ambiguous_decls)
16961     *ambiguous_decls = NULL_TREE;
16962
16963   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16964      no longer valid.  Note that if we are parsing tentatively, and
16965      the parse fails, OBJECT_TYPE will be automatically restored.  */
16966   parser->context->object_type = NULL_TREE;
16967
16968   if (name == error_mark_node)
16969     return error_mark_node;
16970
16971   /* A template-id has already been resolved; there is no lookup to
16972      do.  */
16973   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16974     return name;
16975   if (BASELINK_P (name))
16976     {
16977       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16978                   == TEMPLATE_ID_EXPR);
16979       return name;
16980     }
16981
16982   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16983      it should already have been checked to make sure that the name
16984      used matches the type being destroyed.  */
16985   if (TREE_CODE (name) == BIT_NOT_EXPR)
16986     {
16987       tree type;
16988
16989       /* Figure out to which type this destructor applies.  */
16990       if (parser->scope)
16991         type = parser->scope;
16992       else if (object_type)
16993         type = object_type;
16994       else
16995         type = current_class_type;
16996       /* If that's not a class type, there is no destructor.  */
16997       if (!type || !CLASS_TYPE_P (type))
16998         return error_mark_node;
16999       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17000         lazily_declare_fn (sfk_destructor, type);
17001       if (!CLASSTYPE_DESTRUCTORS (type))
17002           return error_mark_node;
17003       /* If it was a class type, return the destructor.  */
17004       return CLASSTYPE_DESTRUCTORS (type);
17005     }
17006
17007   /* By this point, the NAME should be an ordinary identifier.  If
17008      the id-expression was a qualified name, the qualifying scope is
17009      stored in PARSER->SCOPE at this point.  */
17010   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17011
17012   /* Perform the lookup.  */
17013   if (parser->scope)
17014     {
17015       bool dependent_p;
17016
17017       if (parser->scope == error_mark_node)
17018         return error_mark_node;
17019
17020       /* If the SCOPE is dependent, the lookup must be deferred until
17021          the template is instantiated -- unless we are explicitly
17022          looking up names in uninstantiated templates.  Even then, we
17023          cannot look up the name if the scope is not a class type; it
17024          might, for example, be a template type parameter.  */
17025       dependent_p = (TYPE_P (parser->scope)
17026                      && !(parser->in_declarator_p
17027                           && currently_open_class (parser->scope))
17028                      && dependent_type_p (parser->scope));
17029       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17030            && dependent_p)
17031         {
17032           if (tag_type)
17033             {
17034               tree type;
17035
17036               /* The resolution to Core Issue 180 says that `struct
17037                  A::B' should be considered a type-name, even if `A'
17038                  is dependent.  */
17039               type = make_typename_type (parser->scope, name, tag_type,
17040                                          /*complain=*/tf_error);
17041               decl = TYPE_NAME (type);
17042             }
17043           else if (is_template
17044                    && (cp_parser_next_token_ends_template_argument_p (parser)
17045                        || cp_lexer_next_token_is (parser->lexer,
17046                                                   CPP_CLOSE_PAREN)))
17047             decl = make_unbound_class_template (parser->scope,
17048                                                 name, NULL_TREE,
17049                                                 /*complain=*/tf_error);
17050           else
17051             decl = build_qualified_name (/*type=*/NULL_TREE,
17052                                          parser->scope, name,
17053                                          is_template);
17054         }
17055       else
17056         {
17057           tree pushed_scope = NULL_TREE;
17058
17059           /* If PARSER->SCOPE is a dependent type, then it must be a
17060              class type, and we must not be checking dependencies;
17061              otherwise, we would have processed this lookup above.  So
17062              that PARSER->SCOPE is not considered a dependent base by
17063              lookup_member, we must enter the scope here.  */
17064           if (dependent_p)
17065             pushed_scope = push_scope (parser->scope);
17066           /* If the PARSER->SCOPE is a template specialization, it
17067              may be instantiated during name lookup.  In that case,
17068              errors may be issued.  Even if we rollback the current
17069              tentative parse, those errors are valid.  */
17070           decl = lookup_qualified_name (parser->scope, name,
17071                                         tag_type != none_type,
17072                                         /*complain=*/true);
17073
17074           /* If we have a single function from a using decl, pull it out.  */
17075           if (decl
17076               && TREE_CODE (decl) == OVERLOAD
17077               && !really_overloaded_fn (decl))
17078             decl = OVL_FUNCTION (decl);
17079
17080           if (pushed_scope)
17081             pop_scope (pushed_scope);
17082         }
17083       parser->qualifying_scope = parser->scope;
17084       parser->object_scope = NULL_TREE;
17085     }
17086   else if (object_type)
17087     {
17088       tree object_decl = NULL_TREE;
17089       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17090          OBJECT_TYPE is not a class.  */
17091       if (CLASS_TYPE_P (object_type))
17092         /* If the OBJECT_TYPE is a template specialization, it may
17093            be instantiated during name lookup.  In that case, errors
17094            may be issued.  Even if we rollback the current tentative
17095            parse, those errors are valid.  */
17096         object_decl = lookup_member (object_type,
17097                                      name,
17098                                      /*protect=*/0,
17099                                      tag_type != none_type);
17100       /* Look it up in the enclosing context, too.  */
17101       decl = lookup_name_real (name, tag_type != none_type,
17102                                /*nonclass=*/0,
17103                                /*block_p=*/true, is_namespace, flags);
17104       parser->object_scope = object_type;
17105       parser->qualifying_scope = NULL_TREE;
17106       if (object_decl)
17107         decl = object_decl;
17108     }
17109   else
17110     {
17111       decl = lookup_name_real (name, tag_type != none_type,
17112                                /*nonclass=*/0,
17113                                /*block_p=*/true, is_namespace, flags);
17114       parser->qualifying_scope = NULL_TREE;
17115       parser->object_scope = NULL_TREE;
17116     }
17117
17118   /* If the lookup failed, let our caller know.  */
17119   if (!decl || decl == error_mark_node)
17120     return error_mark_node;
17121
17122   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17123   if (TREE_CODE (decl) == TREE_LIST)
17124     {
17125       if (ambiguous_decls)
17126         *ambiguous_decls = decl;
17127       /* The error message we have to print is too complicated for
17128          cp_parser_error, so we incorporate its actions directly.  */
17129       if (!cp_parser_simulate_error (parser))
17130         {
17131           error ("%Hreference to %qD is ambiguous",
17132                  &name_location, name);
17133           print_candidates (decl);
17134         }
17135       return error_mark_node;
17136     }
17137
17138   gcc_assert (DECL_P (decl)
17139               || TREE_CODE (decl) == OVERLOAD
17140               || TREE_CODE (decl) == SCOPE_REF
17141               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17142               || BASELINK_P (decl));
17143
17144   /* If we have resolved the name of a member declaration, check to
17145      see if the declaration is accessible.  When the name resolves to
17146      set of overloaded functions, accessibility is checked when
17147      overload resolution is done.
17148
17149      During an explicit instantiation, access is not checked at all,
17150      as per [temp.explicit].  */
17151   if (DECL_P (decl))
17152     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17153
17154   return decl;
17155 }
17156
17157 /* Like cp_parser_lookup_name, but for use in the typical case where
17158    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17159    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17160
17161 static tree
17162 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17163 {
17164   return cp_parser_lookup_name (parser, name,
17165                                 none_type,
17166                                 /*is_template=*/false,
17167                                 /*is_namespace=*/false,
17168                                 /*check_dependency=*/true,
17169                                 /*ambiguous_decls=*/NULL,
17170                                 location);
17171 }
17172
17173 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17174    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17175    true, the DECL indicates the class being defined in a class-head,
17176    or declared in an elaborated-type-specifier.
17177
17178    Otherwise, return DECL.  */
17179
17180 static tree
17181 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17182 {
17183   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17184      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17185
17186        struct A {
17187          template <typename T> struct B;
17188        };
17189
17190        template <typename T> struct A::B {};
17191
17192      Similarly, in an elaborated-type-specifier:
17193
17194        namespace N { struct X{}; }
17195
17196        struct A {
17197          template <typename T> friend struct N::X;
17198        };
17199
17200      However, if the DECL refers to a class type, and we are in
17201      the scope of the class, then the name lookup automatically
17202      finds the TYPE_DECL created by build_self_reference rather
17203      than a TEMPLATE_DECL.  For example, in:
17204
17205        template <class T> struct S {
17206          S s;
17207        };
17208
17209      there is no need to handle such case.  */
17210
17211   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17212     return DECL_TEMPLATE_RESULT (decl);
17213
17214   return decl;
17215 }
17216
17217 /* If too many, or too few, template-parameter lists apply to the
17218    declarator, issue an error message.  Returns TRUE if all went well,
17219    and FALSE otherwise.  */
17220
17221 static bool
17222 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17223                                                 cp_declarator *declarator,
17224                                                 location_t declarator_location)
17225 {
17226   unsigned num_templates;
17227
17228   /* We haven't seen any classes that involve template parameters yet.  */
17229   num_templates = 0;
17230
17231   switch (declarator->kind)
17232     {
17233     case cdk_id:
17234       if (declarator->u.id.qualifying_scope)
17235         {
17236           tree scope;
17237           tree member;
17238
17239           scope = declarator->u.id.qualifying_scope;
17240           member = declarator->u.id.unqualified_name;
17241
17242           while (scope && CLASS_TYPE_P (scope))
17243             {
17244               /* You're supposed to have one `template <...>'
17245                  for every template class, but you don't need one
17246                  for a full specialization.  For example:
17247
17248                  template <class T> struct S{};
17249                  template <> struct S<int> { void f(); };
17250                  void S<int>::f () {}
17251
17252                  is correct; there shouldn't be a `template <>' for
17253                  the definition of `S<int>::f'.  */
17254               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17255                 /* If SCOPE does not have template information of any
17256                    kind, then it is not a template, nor is it nested
17257                    within a template.  */
17258                 break;
17259               if (explicit_class_specialization_p (scope))
17260                 break;
17261               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17262                 ++num_templates;
17263
17264               scope = TYPE_CONTEXT (scope);
17265             }
17266         }
17267       else if (TREE_CODE (declarator->u.id.unqualified_name)
17268                == TEMPLATE_ID_EXPR)
17269         /* If the DECLARATOR has the form `X<y>' then it uses one
17270            additional level of template parameters.  */
17271         ++num_templates;
17272
17273       return cp_parser_check_template_parameters (parser,
17274                                                   num_templates,
17275                                                   declarator_location);
17276
17277     case cdk_function:
17278     case cdk_array:
17279     case cdk_pointer:
17280     case cdk_reference:
17281     case cdk_ptrmem:
17282       return (cp_parser_check_declarator_template_parameters
17283               (parser, declarator->declarator, declarator_location));
17284
17285     case cdk_error:
17286       return true;
17287
17288     default:
17289       gcc_unreachable ();
17290     }
17291   return false;
17292 }
17293
17294 /* NUM_TEMPLATES were used in the current declaration.  If that is
17295    invalid, return FALSE and issue an error messages.  Otherwise,
17296    return TRUE.  */
17297
17298 static bool
17299 cp_parser_check_template_parameters (cp_parser* parser,
17300                                      unsigned num_templates,
17301                                      location_t location)
17302 {
17303   /* If there are more template classes than parameter lists, we have
17304      something like:
17305
17306        template <class T> void S<T>::R<T>::f ();  */
17307   if (parser->num_template_parameter_lists < num_templates)
17308     {
17309       error ("%Htoo few template-parameter-lists", &location);
17310       return false;
17311     }
17312   /* If there are the same number of template classes and parameter
17313      lists, that's OK.  */
17314   if (parser->num_template_parameter_lists == num_templates)
17315     return true;
17316   /* If there are more, but only one more, then we are referring to a
17317      member template.  That's OK too.  */
17318   if (parser->num_template_parameter_lists == num_templates + 1)
17319       return true;
17320   /* Otherwise, there are too many template parameter lists.  We have
17321      something like:
17322
17323      template <class T> template <class U> void S::f();  */
17324   error ("%Htoo many template-parameter-lists", &location);
17325   return false;
17326 }
17327
17328 /* Parse an optional `::' token indicating that the following name is
17329    from the global namespace.  If so, PARSER->SCOPE is set to the
17330    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17331    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17332    Returns the new value of PARSER->SCOPE, if the `::' token is
17333    present, and NULL_TREE otherwise.  */
17334
17335 static tree
17336 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17337 {
17338   cp_token *token;
17339
17340   /* Peek at the next token.  */
17341   token = cp_lexer_peek_token (parser->lexer);
17342   /* If we're looking at a `::' token then we're starting from the
17343      global namespace, not our current location.  */
17344   if (token->type == CPP_SCOPE)
17345     {
17346       /* Consume the `::' token.  */
17347       cp_lexer_consume_token (parser->lexer);
17348       /* Set the SCOPE so that we know where to start the lookup.  */
17349       parser->scope = global_namespace;
17350       parser->qualifying_scope = global_namespace;
17351       parser->object_scope = NULL_TREE;
17352
17353       return parser->scope;
17354     }
17355   else if (!current_scope_valid_p)
17356     {
17357       parser->scope = NULL_TREE;
17358       parser->qualifying_scope = NULL_TREE;
17359       parser->object_scope = NULL_TREE;
17360     }
17361
17362   return NULL_TREE;
17363 }
17364
17365 /* Returns TRUE if the upcoming token sequence is the start of a
17366    constructor declarator.  If FRIEND_P is true, the declarator is
17367    preceded by the `friend' specifier.  */
17368
17369 static bool
17370 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17371 {
17372   bool constructor_p;
17373   tree type_decl = NULL_TREE;
17374   bool nested_name_p;
17375   cp_token *next_token;
17376
17377   /* The common case is that this is not a constructor declarator, so
17378      try to avoid doing lots of work if at all possible.  It's not
17379      valid declare a constructor at function scope.  */
17380   if (parser->in_function_body)
17381     return false;
17382   /* And only certain tokens can begin a constructor declarator.  */
17383   next_token = cp_lexer_peek_token (parser->lexer);
17384   if (next_token->type != CPP_NAME
17385       && next_token->type != CPP_SCOPE
17386       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17387       && next_token->type != CPP_TEMPLATE_ID)
17388     return false;
17389
17390   /* Parse tentatively; we are going to roll back all of the tokens
17391      consumed here.  */
17392   cp_parser_parse_tentatively (parser);
17393   /* Assume that we are looking at a constructor declarator.  */
17394   constructor_p = true;
17395
17396   /* Look for the optional `::' operator.  */
17397   cp_parser_global_scope_opt (parser,
17398                               /*current_scope_valid_p=*/false);
17399   /* Look for the nested-name-specifier.  */
17400   nested_name_p
17401     = (cp_parser_nested_name_specifier_opt (parser,
17402                                             /*typename_keyword_p=*/false,
17403                                             /*check_dependency_p=*/false,
17404                                             /*type_p=*/false,
17405                                             /*is_declaration=*/false)
17406        != NULL_TREE);
17407   /* Outside of a class-specifier, there must be a
17408      nested-name-specifier.  */
17409   if (!nested_name_p &&
17410       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17411        || friend_p))
17412     constructor_p = false;
17413   /* If we still think that this might be a constructor-declarator,
17414      look for a class-name.  */
17415   if (constructor_p)
17416     {
17417       /* If we have:
17418
17419            template <typename T> struct S { S(); };
17420            template <typename T> S<T>::S ();
17421
17422          we must recognize that the nested `S' names a class.
17423          Similarly, for:
17424
17425            template <typename T> S<T>::S<T> ();
17426
17427          we must recognize that the nested `S' names a template.  */
17428       type_decl = cp_parser_class_name (parser,
17429                                         /*typename_keyword_p=*/false,
17430                                         /*template_keyword_p=*/false,
17431                                         none_type,
17432                                         /*check_dependency_p=*/false,
17433                                         /*class_head_p=*/false,
17434                                         /*is_declaration=*/false);
17435       /* If there was no class-name, then this is not a constructor.  */
17436       constructor_p = !cp_parser_error_occurred (parser);
17437     }
17438
17439   /* If we're still considering a constructor, we have to see a `(',
17440      to begin the parameter-declaration-clause, followed by either a
17441      `)', an `...', or a decl-specifier.  We need to check for a
17442      type-specifier to avoid being fooled into thinking that:
17443
17444        S::S (f) (int);
17445
17446      is a constructor.  (It is actually a function named `f' that
17447      takes one parameter (of type `int') and returns a value of type
17448      `S::S'.  */
17449   if (constructor_p
17450       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17451     {
17452       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17453           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17454           /* A parameter declaration begins with a decl-specifier,
17455              which is either the "attribute" keyword, a storage class
17456              specifier, or (usually) a type-specifier.  */
17457           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17458         {
17459           tree type;
17460           tree pushed_scope = NULL_TREE;
17461           unsigned saved_num_template_parameter_lists;
17462
17463           /* Names appearing in the type-specifier should be looked up
17464              in the scope of the class.  */
17465           if (current_class_type)
17466             type = NULL_TREE;
17467           else
17468             {
17469               type = TREE_TYPE (type_decl);
17470               if (TREE_CODE (type) == TYPENAME_TYPE)
17471                 {
17472                   type = resolve_typename_type (type,
17473                                                 /*only_current_p=*/false);
17474                   if (TREE_CODE (type) == TYPENAME_TYPE)
17475                     {
17476                       cp_parser_abort_tentative_parse (parser);
17477                       return false;
17478                     }
17479                 }
17480               pushed_scope = push_scope (type);
17481             }
17482
17483           /* Inside the constructor parameter list, surrounding
17484              template-parameter-lists do not apply.  */
17485           saved_num_template_parameter_lists
17486             = parser->num_template_parameter_lists;
17487           parser->num_template_parameter_lists = 0;
17488
17489           /* Look for the type-specifier.  */
17490           cp_parser_type_specifier (parser,
17491                                     CP_PARSER_FLAGS_NONE,
17492                                     /*decl_specs=*/NULL,
17493                                     /*is_declarator=*/true,
17494                                     /*declares_class_or_enum=*/NULL,
17495                                     /*is_cv_qualifier=*/NULL);
17496
17497           parser->num_template_parameter_lists
17498             = saved_num_template_parameter_lists;
17499
17500           /* Leave the scope of the class.  */
17501           if (pushed_scope)
17502             pop_scope (pushed_scope);
17503
17504           constructor_p = !cp_parser_error_occurred (parser);
17505         }
17506     }
17507   else
17508     constructor_p = false;
17509   /* We did not really want to consume any tokens.  */
17510   cp_parser_abort_tentative_parse (parser);
17511
17512   return constructor_p;
17513 }
17514
17515 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17516    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17517    they must be performed once we are in the scope of the function.
17518
17519    Returns the function defined.  */
17520
17521 static tree
17522 cp_parser_function_definition_from_specifiers_and_declarator
17523   (cp_parser* parser,
17524    cp_decl_specifier_seq *decl_specifiers,
17525    tree attributes,
17526    const cp_declarator *declarator)
17527 {
17528   tree fn;
17529   bool success_p;
17530
17531   /* Begin the function-definition.  */
17532   success_p = start_function (decl_specifiers, declarator, attributes);
17533
17534   /* The things we're about to see are not directly qualified by any
17535      template headers we've seen thus far.  */
17536   reset_specialization ();
17537
17538   /* If there were names looked up in the decl-specifier-seq that we
17539      did not check, check them now.  We must wait until we are in the
17540      scope of the function to perform the checks, since the function
17541      might be a friend.  */
17542   perform_deferred_access_checks ();
17543
17544   if (!success_p)
17545     {
17546       /* Skip the entire function.  */
17547       cp_parser_skip_to_end_of_block_or_statement (parser);
17548       fn = error_mark_node;
17549     }
17550   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17551     {
17552       /* Seen already, skip it.  An error message has already been output.  */
17553       cp_parser_skip_to_end_of_block_or_statement (parser);
17554       fn = current_function_decl;
17555       current_function_decl = NULL_TREE;
17556       /* If this is a function from a class, pop the nested class.  */
17557       if (current_class_name)
17558         pop_nested_class ();
17559     }
17560   else
17561     fn = cp_parser_function_definition_after_declarator (parser,
17562                                                          /*inline_p=*/false);
17563
17564   return fn;
17565 }
17566
17567 /* Parse the part of a function-definition that follows the
17568    declarator.  INLINE_P is TRUE iff this function is an inline
17569    function defined with a class-specifier.
17570
17571    Returns the function defined.  */
17572
17573 static tree
17574 cp_parser_function_definition_after_declarator (cp_parser* parser,
17575                                                 bool inline_p)
17576 {
17577   tree fn;
17578   bool ctor_initializer_p = false;
17579   bool saved_in_unbraced_linkage_specification_p;
17580   bool saved_in_function_body;
17581   unsigned saved_num_template_parameter_lists;
17582   cp_token *token;
17583
17584   saved_in_function_body = parser->in_function_body;
17585   parser->in_function_body = true;
17586   /* If the next token is `return', then the code may be trying to
17587      make use of the "named return value" extension that G++ used to
17588      support.  */
17589   token = cp_lexer_peek_token (parser->lexer);
17590   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17591     {
17592       /* Consume the `return' keyword.  */
17593       cp_lexer_consume_token (parser->lexer);
17594       /* Look for the identifier that indicates what value is to be
17595          returned.  */
17596       cp_parser_identifier (parser);
17597       /* Issue an error message.  */
17598       error ("%Hnamed return values are no longer supported",
17599              &token->location);
17600       /* Skip tokens until we reach the start of the function body.  */
17601       while (true)
17602         {
17603           cp_token *token = cp_lexer_peek_token (parser->lexer);
17604           if (token->type == CPP_OPEN_BRACE
17605               || token->type == CPP_EOF
17606               || token->type == CPP_PRAGMA_EOL)
17607             break;
17608           cp_lexer_consume_token (parser->lexer);
17609         }
17610     }
17611   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17612      anything declared inside `f'.  */
17613   saved_in_unbraced_linkage_specification_p
17614     = parser->in_unbraced_linkage_specification_p;
17615   parser->in_unbraced_linkage_specification_p = false;
17616   /* Inside the function, surrounding template-parameter-lists do not
17617      apply.  */
17618   saved_num_template_parameter_lists
17619     = parser->num_template_parameter_lists;
17620   parser->num_template_parameter_lists = 0;
17621   /* If the next token is `try', then we are looking at a
17622      function-try-block.  */
17623   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17624     ctor_initializer_p = cp_parser_function_try_block (parser);
17625   /* A function-try-block includes the function-body, so we only do
17626      this next part if we're not processing a function-try-block.  */
17627   else
17628     ctor_initializer_p
17629       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17630
17631   /* Finish the function.  */
17632   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17633                         (inline_p ? 2 : 0));
17634   /* Generate code for it, if necessary.  */
17635   expand_or_defer_fn (fn);
17636   /* Restore the saved values.  */
17637   parser->in_unbraced_linkage_specification_p
17638     = saved_in_unbraced_linkage_specification_p;
17639   parser->num_template_parameter_lists
17640     = saved_num_template_parameter_lists;
17641   parser->in_function_body = saved_in_function_body;
17642
17643   return fn;
17644 }
17645
17646 /* Parse a template-declaration, assuming that the `export' (and
17647    `extern') keywords, if present, has already been scanned.  MEMBER_P
17648    is as for cp_parser_template_declaration.  */
17649
17650 static void
17651 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17652 {
17653   tree decl = NULL_TREE;
17654   VEC (deferred_access_check,gc) *checks;
17655   tree parameter_list;
17656   bool friend_p = false;
17657   bool need_lang_pop;
17658   cp_token *token;
17659
17660   /* Look for the `template' keyword.  */
17661   token = cp_lexer_peek_token (parser->lexer);
17662   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17663     return;
17664
17665   /* And the `<'.  */
17666   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17667     return;
17668   if (at_class_scope_p () && current_function_decl)
17669     {
17670       /* 14.5.2.2 [temp.mem]
17671
17672          A local class shall not have member templates.  */
17673       error ("%Hinvalid declaration of member template in local class",
17674              &token->location);
17675       cp_parser_skip_to_end_of_block_or_statement (parser);
17676       return;
17677     }
17678   /* [temp]
17679
17680      A template ... shall not have C linkage.  */
17681   if (current_lang_name == lang_name_c)
17682     {
17683       error ("%Htemplate with C linkage", &token->location);
17684       /* Give it C++ linkage to avoid confusing other parts of the
17685          front end.  */
17686       push_lang_context (lang_name_cplusplus);
17687       need_lang_pop = true;
17688     }
17689   else
17690     need_lang_pop = false;
17691
17692   /* We cannot perform access checks on the template parameter
17693      declarations until we know what is being declared, just as we
17694      cannot check the decl-specifier list.  */
17695   push_deferring_access_checks (dk_deferred);
17696
17697   /* If the next token is `>', then we have an invalid
17698      specialization.  Rather than complain about an invalid template
17699      parameter, issue an error message here.  */
17700   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17701     {
17702       cp_parser_error (parser, "invalid explicit specialization");
17703       begin_specialization ();
17704       parameter_list = NULL_TREE;
17705     }
17706   else
17707     /* Parse the template parameters.  */
17708     parameter_list = cp_parser_template_parameter_list (parser);
17709
17710   /* Get the deferred access checks from the parameter list.  These
17711      will be checked once we know what is being declared, as for a
17712      member template the checks must be performed in the scope of the
17713      class containing the member.  */
17714   checks = get_deferred_access_checks ();
17715
17716   /* Look for the `>'.  */
17717   cp_parser_skip_to_end_of_template_parameter_list (parser);
17718   /* We just processed one more parameter list.  */
17719   ++parser->num_template_parameter_lists;
17720   /* If the next token is `template', there are more template
17721      parameters.  */
17722   if (cp_lexer_next_token_is_keyword (parser->lexer,
17723                                       RID_TEMPLATE))
17724     cp_parser_template_declaration_after_export (parser, member_p);
17725   else
17726     {
17727       /* There are no access checks when parsing a template, as we do not
17728          know if a specialization will be a friend.  */
17729       push_deferring_access_checks (dk_no_check);
17730       token = cp_lexer_peek_token (parser->lexer);
17731       decl = cp_parser_single_declaration (parser,
17732                                            checks,
17733                                            member_p,
17734                                            /*explicit_specialization_p=*/false,
17735                                            &friend_p);
17736       pop_deferring_access_checks ();
17737
17738       /* If this is a member template declaration, let the front
17739          end know.  */
17740       if (member_p && !friend_p && decl)
17741         {
17742           if (TREE_CODE (decl) == TYPE_DECL)
17743             cp_parser_check_access_in_redeclaration (decl, token->location);
17744
17745           decl = finish_member_template_decl (decl);
17746         }
17747       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17748         make_friend_class (current_class_type, TREE_TYPE (decl),
17749                            /*complain=*/true);
17750     }
17751   /* We are done with the current parameter list.  */
17752   --parser->num_template_parameter_lists;
17753
17754   pop_deferring_access_checks ();
17755
17756   /* Finish up.  */
17757   finish_template_decl (parameter_list);
17758
17759   /* Register member declarations.  */
17760   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17761     finish_member_declaration (decl);
17762   /* For the erroneous case of a template with C linkage, we pushed an
17763      implicit C++ linkage scope; exit that scope now.  */
17764   if (need_lang_pop)
17765     pop_lang_context ();
17766   /* If DECL is a function template, we must return to parse it later.
17767      (Even though there is no definition, there might be default
17768      arguments that need handling.)  */
17769   if (member_p && decl
17770       && (TREE_CODE (decl) == FUNCTION_DECL
17771           || DECL_FUNCTION_TEMPLATE_P (decl)))
17772     TREE_VALUE (parser->unparsed_functions_queues)
17773       = tree_cons (NULL_TREE, decl,
17774                    TREE_VALUE (parser->unparsed_functions_queues));
17775 }
17776
17777 /* Perform the deferred access checks from a template-parameter-list.
17778    CHECKS is a TREE_LIST of access checks, as returned by
17779    get_deferred_access_checks.  */
17780
17781 static void
17782 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17783 {
17784   ++processing_template_parmlist;
17785   perform_access_checks (checks);
17786   --processing_template_parmlist;
17787 }
17788
17789 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17790    `function-definition' sequence.  MEMBER_P is true, this declaration
17791    appears in a class scope.
17792
17793    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17794    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17795
17796 static tree
17797 cp_parser_single_declaration (cp_parser* parser,
17798                               VEC (deferred_access_check,gc)* checks,
17799                               bool member_p,
17800                               bool explicit_specialization_p,
17801                               bool* friend_p)
17802 {
17803   int declares_class_or_enum;
17804   tree decl = NULL_TREE;
17805   cp_decl_specifier_seq decl_specifiers;
17806   bool function_definition_p = false;
17807   cp_token *decl_spec_token_start;
17808
17809   /* This function is only used when processing a template
17810      declaration.  */
17811   gcc_assert (innermost_scope_kind () == sk_template_parms
17812               || innermost_scope_kind () == sk_template_spec);
17813
17814   /* Defer access checks until we know what is being declared.  */
17815   push_deferring_access_checks (dk_deferred);
17816
17817   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17818      alternative.  */
17819   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17820   cp_parser_decl_specifier_seq (parser,
17821                                 CP_PARSER_FLAGS_OPTIONAL,
17822                                 &decl_specifiers,
17823                                 &declares_class_or_enum);
17824   if (friend_p)
17825     *friend_p = cp_parser_friend_p (&decl_specifiers);
17826
17827   /* There are no template typedefs.  */
17828   if (decl_specifiers.specs[(int) ds_typedef])
17829     {
17830       error ("%Htemplate declaration of %qs",
17831              &decl_spec_token_start->location, "typedef");
17832       decl = error_mark_node;
17833     }
17834
17835   /* Gather up the access checks that occurred the
17836      decl-specifier-seq.  */
17837   stop_deferring_access_checks ();
17838
17839   /* Check for the declaration of a template class.  */
17840   if (declares_class_or_enum)
17841     {
17842       if (cp_parser_declares_only_class_p (parser))
17843         {
17844           decl = shadow_tag (&decl_specifiers);
17845
17846           /* In this case:
17847
17848                struct C {
17849                  friend template <typename T> struct A<T>::B;
17850                };
17851
17852              A<T>::B will be represented by a TYPENAME_TYPE, and
17853              therefore not recognized by shadow_tag.  */
17854           if (friend_p && *friend_p
17855               && !decl
17856               && decl_specifiers.type
17857               && TYPE_P (decl_specifiers.type))
17858             decl = decl_specifiers.type;
17859
17860           if (decl && decl != error_mark_node)
17861             decl = TYPE_NAME (decl);
17862           else
17863             decl = error_mark_node;
17864
17865           /* Perform access checks for template parameters.  */
17866           cp_parser_perform_template_parameter_access_checks (checks);
17867         }
17868     }
17869   /* If it's not a template class, try for a template function.  If
17870      the next token is a `;', then this declaration does not declare
17871      anything.  But, if there were errors in the decl-specifiers, then
17872      the error might well have come from an attempted class-specifier.
17873      In that case, there's no need to warn about a missing declarator.  */
17874   if (!decl
17875       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17876           || decl_specifiers.type != error_mark_node))
17877     {
17878       decl = cp_parser_init_declarator (parser,
17879                                         &decl_specifiers,
17880                                         checks,
17881                                         /*function_definition_allowed_p=*/true,
17882                                         member_p,
17883                                         declares_class_or_enum,
17884                                         &function_definition_p);
17885
17886     /* 7.1.1-1 [dcl.stc]
17887
17888        A storage-class-specifier shall not be specified in an explicit
17889        specialization...  */
17890     if (decl
17891         && explicit_specialization_p
17892         && decl_specifiers.storage_class != sc_none)
17893       {
17894         error ("%Hexplicit template specialization cannot have a storage class",
17895                &decl_spec_token_start->location);
17896         decl = error_mark_node;
17897       }
17898     }
17899
17900   pop_deferring_access_checks ();
17901
17902   /* Clear any current qualification; whatever comes next is the start
17903      of something new.  */
17904   parser->scope = NULL_TREE;
17905   parser->qualifying_scope = NULL_TREE;
17906   parser->object_scope = NULL_TREE;
17907   /* Look for a trailing `;' after the declaration.  */
17908   if (!function_definition_p
17909       && (decl == error_mark_node
17910           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17911     cp_parser_skip_to_end_of_block_or_statement (parser);
17912
17913   return decl;
17914 }
17915
17916 /* Parse a cast-expression that is not the operand of a unary "&".  */
17917
17918 static tree
17919 cp_parser_simple_cast_expression (cp_parser *parser)
17920 {
17921   return cp_parser_cast_expression (parser, /*address_p=*/false,
17922                                     /*cast_p=*/false, NULL);
17923 }
17924
17925 /* Parse a functional cast to TYPE.  Returns an expression
17926    representing the cast.  */
17927
17928 static tree
17929 cp_parser_functional_cast (cp_parser* parser, tree type)
17930 {
17931   tree expression_list;
17932   tree cast;
17933   bool nonconst_p;
17934
17935   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17936     {
17937       maybe_warn_cpp0x ("extended initializer lists");
17938       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17939       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17940       if (TREE_CODE (type) == TYPE_DECL)
17941         type = TREE_TYPE (type);
17942       return finish_compound_literal (type, expression_list);
17943     }
17944
17945   expression_list
17946     = cp_parser_parenthesized_expression_list (parser, false,
17947                                                /*cast_p=*/true,
17948                                                /*allow_expansion_p=*/true,
17949                                                /*non_constant_p=*/NULL);
17950
17951   cast = build_functional_cast (type, expression_list,
17952                                 tf_warning_or_error);
17953   /* [expr.const]/1: In an integral constant expression "only type
17954      conversions to integral or enumeration type can be used".  */
17955   if (TREE_CODE (type) == TYPE_DECL)
17956     type = TREE_TYPE (type);
17957   if (cast != error_mark_node
17958       && !cast_valid_in_integral_constant_expression_p (type)
17959       && (cp_parser_non_integral_constant_expression
17960           (parser, "a call to a constructor")))
17961     return error_mark_node;
17962   return cast;
17963 }
17964
17965 /* Save the tokens that make up the body of a member function defined
17966    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17967    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17968    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17969    for the member function.  */
17970
17971 static tree
17972 cp_parser_save_member_function_body (cp_parser* parser,
17973                                      cp_decl_specifier_seq *decl_specifiers,
17974                                      cp_declarator *declarator,
17975                                      tree attributes)
17976 {
17977   cp_token *first;
17978   cp_token *last;
17979   tree fn;
17980
17981   /* Create the function-declaration.  */
17982   fn = start_method (decl_specifiers, declarator, attributes);
17983   /* If something went badly wrong, bail out now.  */
17984   if (fn == error_mark_node)
17985     {
17986       /* If there's a function-body, skip it.  */
17987       if (cp_parser_token_starts_function_definition_p
17988           (cp_lexer_peek_token (parser->lexer)))
17989         cp_parser_skip_to_end_of_block_or_statement (parser);
17990       return error_mark_node;
17991     }
17992
17993   /* Remember it, if there default args to post process.  */
17994   cp_parser_save_default_args (parser, fn);
17995
17996   /* Save away the tokens that make up the body of the
17997      function.  */
17998   first = parser->lexer->next_token;
17999   /* We can have braced-init-list mem-initializers before the fn body.  */
18000   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18001     {
18002       cp_lexer_consume_token (parser->lexer);
18003       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18004              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18005         {
18006           /* cache_group will stop after an un-nested { } pair, too.  */
18007           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18008             break;
18009
18010           /* variadic mem-inits have ... after the ')'.  */
18011           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18012             cp_lexer_consume_token (parser->lexer);
18013         }
18014     }
18015   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18016   /* Handle function try blocks.  */
18017   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18018     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18019   last = parser->lexer->next_token;
18020
18021   /* Save away the inline definition; we will process it when the
18022      class is complete.  */
18023   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18024   DECL_PENDING_INLINE_P (fn) = 1;
18025
18026   /* We need to know that this was defined in the class, so that
18027      friend templates are handled correctly.  */
18028   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18029
18030   /* We're done with the inline definition.  */
18031   finish_method (fn);
18032
18033   /* Add FN to the queue of functions to be parsed later.  */
18034   TREE_VALUE (parser->unparsed_functions_queues)
18035     = tree_cons (NULL_TREE, fn,
18036                  TREE_VALUE (parser->unparsed_functions_queues));
18037
18038   return fn;
18039 }
18040
18041 /* Parse a template-argument-list, as well as the trailing ">" (but
18042    not the opening ">").  See cp_parser_template_argument_list for the
18043    return value.  */
18044
18045 static tree
18046 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18047 {
18048   tree arguments;
18049   tree saved_scope;
18050   tree saved_qualifying_scope;
18051   tree saved_object_scope;
18052   bool saved_greater_than_is_operator_p;
18053   bool saved_skip_evaluation;
18054
18055   /* [temp.names]
18056
18057      When parsing a template-id, the first non-nested `>' is taken as
18058      the end of the template-argument-list rather than a greater-than
18059      operator.  */
18060   saved_greater_than_is_operator_p
18061     = parser->greater_than_is_operator_p;
18062   parser->greater_than_is_operator_p = false;
18063   /* Parsing the argument list may modify SCOPE, so we save it
18064      here.  */
18065   saved_scope = parser->scope;
18066   saved_qualifying_scope = parser->qualifying_scope;
18067   saved_object_scope = parser->object_scope;
18068   /* We need to evaluate the template arguments, even though this
18069      template-id may be nested within a "sizeof".  */
18070   saved_skip_evaluation = skip_evaluation;
18071   skip_evaluation = false;
18072   /* Parse the template-argument-list itself.  */
18073   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18074       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18075     arguments = NULL_TREE;
18076   else
18077     arguments = cp_parser_template_argument_list (parser);
18078   /* Look for the `>' that ends the template-argument-list. If we find
18079      a '>>' instead, it's probably just a typo.  */
18080   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18081     {
18082       if (cxx_dialect != cxx98)
18083         {
18084           /* In C++0x, a `>>' in a template argument list or cast
18085              expression is considered to be two separate `>'
18086              tokens. So, change the current token to a `>', but don't
18087              consume it: it will be consumed later when the outer
18088              template argument list (or cast expression) is parsed.
18089              Note that this replacement of `>' for `>>' is necessary
18090              even if we are parsing tentatively: in the tentative
18091              case, after calling
18092              cp_parser_enclosed_template_argument_list we will always
18093              throw away all of the template arguments and the first
18094              closing `>', either because the template argument list
18095              was erroneous or because we are replacing those tokens
18096              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18097              not have been thrown away) is needed either to close an
18098              outer template argument list or to complete a new-style
18099              cast.  */
18100           cp_token *token = cp_lexer_peek_token (parser->lexer);
18101           token->type = CPP_GREATER;
18102         }
18103       else if (!saved_greater_than_is_operator_p)
18104         {
18105           /* If we're in a nested template argument list, the '>>' has
18106             to be a typo for '> >'. We emit the error message, but we
18107             continue parsing and we push a '>' as next token, so that
18108             the argument list will be parsed correctly.  Note that the
18109             global source location is still on the token before the
18110             '>>', so we need to say explicitly where we want it.  */
18111           cp_token *token = cp_lexer_peek_token (parser->lexer);
18112           error ("%H%<>>%> should be %<> >%> "
18113                  "within a nested template argument list",
18114                  &token->location);
18115
18116           token->type = CPP_GREATER;
18117         }
18118       else
18119         {
18120           /* If this is not a nested template argument list, the '>>'
18121             is a typo for '>'. Emit an error message and continue.
18122             Same deal about the token location, but here we can get it
18123             right by consuming the '>>' before issuing the diagnostic.  */
18124           cp_token *token = cp_lexer_consume_token (parser->lexer);
18125           error ("%Hspurious %<>>%>, use %<>%> to terminate "
18126                  "a template argument list", &token->location);
18127         }
18128     }
18129   else
18130     cp_parser_skip_to_end_of_template_parameter_list (parser);
18131   /* The `>' token might be a greater-than operator again now.  */
18132   parser->greater_than_is_operator_p
18133     = saved_greater_than_is_operator_p;
18134   /* Restore the SAVED_SCOPE.  */
18135   parser->scope = saved_scope;
18136   parser->qualifying_scope = saved_qualifying_scope;
18137   parser->object_scope = saved_object_scope;
18138   skip_evaluation = saved_skip_evaluation;
18139
18140   return arguments;
18141 }
18142
18143 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18144    arguments, or the body of the function have not yet been parsed,
18145    parse them now.  */
18146
18147 static void
18148 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18149 {
18150   /* If this member is a template, get the underlying
18151      FUNCTION_DECL.  */
18152   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18153     member_function = DECL_TEMPLATE_RESULT (member_function);
18154
18155   /* There should not be any class definitions in progress at this
18156      point; the bodies of members are only parsed outside of all class
18157      definitions.  */
18158   gcc_assert (parser->num_classes_being_defined == 0);
18159   /* While we're parsing the member functions we might encounter more
18160      classes.  We want to handle them right away, but we don't want
18161      them getting mixed up with functions that are currently in the
18162      queue.  */
18163   parser->unparsed_functions_queues
18164     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18165
18166   /* Make sure that any template parameters are in scope.  */
18167   maybe_begin_member_template_processing (member_function);
18168
18169   /* If the body of the function has not yet been parsed, parse it
18170      now.  */
18171   if (DECL_PENDING_INLINE_P (member_function))
18172     {
18173       tree function_scope;
18174       cp_token_cache *tokens;
18175
18176       /* The function is no longer pending; we are processing it.  */
18177       tokens = DECL_PENDING_INLINE_INFO (member_function);
18178       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18179       DECL_PENDING_INLINE_P (member_function) = 0;
18180
18181       /* If this is a local class, enter the scope of the containing
18182          function.  */
18183       function_scope = current_function_decl;
18184       if (function_scope)
18185         push_function_context ();
18186
18187       /* Push the body of the function onto the lexer stack.  */
18188       cp_parser_push_lexer_for_tokens (parser, tokens);
18189
18190       /* Let the front end know that we going to be defining this
18191          function.  */
18192       start_preparsed_function (member_function, NULL_TREE,
18193                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18194
18195       /* Don't do access checking if it is a templated function.  */
18196       if (processing_template_decl)
18197         push_deferring_access_checks (dk_no_check);
18198
18199       /* Now, parse the body of the function.  */
18200       cp_parser_function_definition_after_declarator (parser,
18201                                                       /*inline_p=*/true);
18202
18203       if (processing_template_decl)
18204         pop_deferring_access_checks ();
18205
18206       /* Leave the scope of the containing function.  */
18207       if (function_scope)
18208         pop_function_context ();
18209       cp_parser_pop_lexer (parser);
18210     }
18211
18212   /* Remove any template parameters from the symbol table.  */
18213   maybe_end_member_template_processing ();
18214
18215   /* Restore the queue.  */
18216   parser->unparsed_functions_queues
18217     = TREE_CHAIN (parser->unparsed_functions_queues);
18218 }
18219
18220 /* If DECL contains any default args, remember it on the unparsed
18221    functions queue.  */
18222
18223 static void
18224 cp_parser_save_default_args (cp_parser* parser, tree decl)
18225 {
18226   tree probe;
18227
18228   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18229        probe;
18230        probe = TREE_CHAIN (probe))
18231     if (TREE_PURPOSE (probe))
18232       {
18233         TREE_PURPOSE (parser->unparsed_functions_queues)
18234           = tree_cons (current_class_type, decl,
18235                        TREE_PURPOSE (parser->unparsed_functions_queues));
18236         break;
18237       }
18238 }
18239
18240 /* FN is a FUNCTION_DECL which may contains a parameter with an
18241    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18242    assumes that the current scope is the scope in which the default
18243    argument should be processed.  */
18244
18245 static void
18246 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18247 {
18248   bool saved_local_variables_forbidden_p;
18249   tree parm;
18250
18251   /* While we're parsing the default args, we might (due to the
18252      statement expression extension) encounter more classes.  We want
18253      to handle them right away, but we don't want them getting mixed
18254      up with default args that are currently in the queue.  */
18255   parser->unparsed_functions_queues
18256     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18257
18258   /* Local variable names (and the `this' keyword) may not appear
18259      in a default argument.  */
18260   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18261   parser->local_variables_forbidden_p = true;
18262
18263   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18264        parm;
18265        parm = TREE_CHAIN (parm))
18266     {
18267       cp_token_cache *tokens;
18268       tree default_arg = TREE_PURPOSE (parm);
18269       tree parsed_arg;
18270       VEC(tree,gc) *insts;
18271       tree copy;
18272       unsigned ix;
18273
18274       if (!default_arg)
18275         continue;
18276
18277       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18278         /* This can happen for a friend declaration for a function
18279            already declared with default arguments.  */
18280         continue;
18281
18282        /* Push the saved tokens for the default argument onto the parser's
18283           lexer stack.  */
18284       tokens = DEFARG_TOKENS (default_arg);
18285       cp_parser_push_lexer_for_tokens (parser, tokens);
18286
18287       /* Parse the assignment-expression.  */
18288       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18289
18290       if (!processing_template_decl)
18291         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18292
18293       TREE_PURPOSE (parm) = parsed_arg;
18294
18295       /* Update any instantiations we've already created.  */
18296       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18297            VEC_iterate (tree, insts, ix, copy); ix++)
18298         TREE_PURPOSE (copy) = parsed_arg;
18299
18300       /* If the token stream has not been completely used up, then
18301          there was extra junk after the end of the default
18302          argument.  */
18303       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18304         cp_parser_error (parser, "expected %<,%>");
18305
18306       /* Revert to the main lexer.  */
18307       cp_parser_pop_lexer (parser);
18308     }
18309
18310   /* Make sure no default arg is missing.  */
18311   check_default_args (fn);
18312
18313   /* Restore the state of local_variables_forbidden_p.  */
18314   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18315
18316   /* Restore the queue.  */
18317   parser->unparsed_functions_queues
18318     = TREE_CHAIN (parser->unparsed_functions_queues);
18319 }
18320
18321 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18322    either a TYPE or an expression, depending on the form of the
18323    input.  The KEYWORD indicates which kind of expression we have
18324    encountered.  */
18325
18326 static tree
18327 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18328 {
18329   tree expr = NULL_TREE;
18330   const char *saved_message;
18331   char *tmp;
18332   bool saved_integral_constant_expression_p;
18333   bool saved_non_integral_constant_expression_p;
18334   bool pack_expansion_p = false;
18335
18336   /* Types cannot be defined in a `sizeof' expression.  Save away the
18337      old message.  */
18338   saved_message = parser->type_definition_forbidden_message;
18339   /* And create the new one.  */
18340   tmp = concat ("types may not be defined in %<",
18341                 IDENTIFIER_POINTER (ridpointers[keyword]),
18342                 "%> expressions", NULL);
18343   parser->type_definition_forbidden_message = tmp;
18344
18345   /* The restrictions on constant-expressions do not apply inside
18346      sizeof expressions.  */
18347   saved_integral_constant_expression_p
18348     = parser->integral_constant_expression_p;
18349   saved_non_integral_constant_expression_p
18350     = parser->non_integral_constant_expression_p;
18351   parser->integral_constant_expression_p = false;
18352
18353   /* If it's a `...', then we are computing the length of a parameter
18354      pack.  */
18355   if (keyword == RID_SIZEOF
18356       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18357     {
18358       /* Consume the `...'.  */
18359       cp_lexer_consume_token (parser->lexer);
18360       maybe_warn_variadic_templates ();
18361
18362       /* Note that this is an expansion.  */
18363       pack_expansion_p = true;
18364     }
18365
18366   /* Do not actually evaluate the expression.  */
18367   ++skip_evaluation;
18368   /* If it's a `(', then we might be looking at the type-id
18369      construction.  */
18370   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18371     {
18372       tree type;
18373       bool saved_in_type_id_in_expr_p;
18374
18375       /* We can't be sure yet whether we're looking at a type-id or an
18376          expression.  */
18377       cp_parser_parse_tentatively (parser);
18378       /* Consume the `('.  */
18379       cp_lexer_consume_token (parser->lexer);
18380       /* Parse the type-id.  */
18381       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18382       parser->in_type_id_in_expr_p = true;
18383       type = cp_parser_type_id (parser);
18384       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18385       /* Now, look for the trailing `)'.  */
18386       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18387       /* If all went well, then we're done.  */
18388       if (cp_parser_parse_definitely (parser))
18389         {
18390           cp_decl_specifier_seq decl_specs;
18391
18392           /* Build a trivial decl-specifier-seq.  */
18393           clear_decl_specs (&decl_specs);
18394           decl_specs.type = type;
18395
18396           /* Call grokdeclarator to figure out what type this is.  */
18397           expr = grokdeclarator (NULL,
18398                                  &decl_specs,
18399                                  TYPENAME,
18400                                  /*initialized=*/0,
18401                                  /*attrlist=*/NULL);
18402         }
18403     }
18404
18405   /* If the type-id production did not work out, then we must be
18406      looking at the unary-expression production.  */
18407   if (!expr)
18408     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18409                                        /*cast_p=*/false, NULL);
18410
18411   if (pack_expansion_p)
18412     /* Build a pack expansion. */
18413     expr = make_pack_expansion (expr);
18414
18415   /* Go back to evaluating expressions.  */
18416   --skip_evaluation;
18417
18418   /* Free the message we created.  */
18419   free (tmp);
18420   /* And restore the old one.  */
18421   parser->type_definition_forbidden_message = saved_message;
18422   parser->integral_constant_expression_p
18423     = saved_integral_constant_expression_p;
18424   parser->non_integral_constant_expression_p
18425     = saved_non_integral_constant_expression_p;
18426
18427   return expr;
18428 }
18429
18430 /* If the current declaration has no declarator, return true.  */
18431
18432 static bool
18433 cp_parser_declares_only_class_p (cp_parser *parser)
18434 {
18435   /* If the next token is a `;' or a `,' then there is no
18436      declarator.  */
18437   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18438           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18439 }
18440
18441 /* Update the DECL_SPECS to reflect the storage class indicated by
18442    KEYWORD.  */
18443
18444 static void
18445 cp_parser_set_storage_class (cp_parser *parser,
18446                              cp_decl_specifier_seq *decl_specs,
18447                              enum rid keyword,
18448                              location_t location)
18449 {
18450   cp_storage_class storage_class;
18451
18452   if (parser->in_unbraced_linkage_specification_p)
18453     {
18454       error ("%Hinvalid use of %qD in linkage specification",
18455              &location, ridpointers[keyword]);
18456       return;
18457     }
18458   else if (decl_specs->storage_class != sc_none)
18459     {
18460       decl_specs->conflicting_specifiers_p = true;
18461       return;
18462     }
18463
18464   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18465       && decl_specs->specs[(int) ds_thread])
18466     {
18467       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18468       decl_specs->specs[(int) ds_thread] = 0;
18469     }
18470
18471   switch (keyword)
18472     {
18473     case RID_AUTO:
18474       storage_class = sc_auto;
18475       break;
18476     case RID_REGISTER:
18477       storage_class = sc_register;
18478       break;
18479     case RID_STATIC:
18480       storage_class = sc_static;
18481       break;
18482     case RID_EXTERN:
18483       storage_class = sc_extern;
18484       break;
18485     case RID_MUTABLE:
18486       storage_class = sc_mutable;
18487       break;
18488     default:
18489       gcc_unreachable ();
18490     }
18491   decl_specs->storage_class = storage_class;
18492
18493   /* A storage class specifier cannot be applied alongside a typedef 
18494      specifier. If there is a typedef specifier present then set 
18495      conflicting_specifiers_p which will trigger an error later
18496      on in grokdeclarator. */
18497   if (decl_specs->specs[(int)ds_typedef])
18498     decl_specs->conflicting_specifiers_p = true;
18499 }
18500
18501 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18502    is true, the type is a user-defined type; otherwise it is a
18503    built-in type specified by a keyword.  */
18504
18505 static void
18506 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18507                               tree type_spec,
18508                               location_t location,
18509                               bool user_defined_p)
18510 {
18511   decl_specs->any_specifiers_p = true;
18512
18513   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18514      (with, for example, in "typedef int wchar_t;") we remember that
18515      this is what happened.  In system headers, we ignore these
18516      declarations so that G++ can work with system headers that are not
18517      C++-safe.  */
18518   if (decl_specs->specs[(int) ds_typedef]
18519       && !user_defined_p
18520       && (type_spec == boolean_type_node
18521           || type_spec == char16_type_node
18522           || type_spec == char32_type_node
18523           || type_spec == wchar_type_node)
18524       && (decl_specs->type
18525           || decl_specs->specs[(int) ds_long]
18526           || decl_specs->specs[(int) ds_short]
18527           || decl_specs->specs[(int) ds_unsigned]
18528           || decl_specs->specs[(int) ds_signed]))
18529     {
18530       decl_specs->redefined_builtin_type = type_spec;
18531       if (!decl_specs->type)
18532         {
18533           decl_specs->type = type_spec;
18534           decl_specs->user_defined_type_p = false;
18535           decl_specs->type_location = location;
18536         }
18537     }
18538   else if (decl_specs->type)
18539     decl_specs->multiple_types_p = true;
18540   else
18541     {
18542       decl_specs->type = type_spec;
18543       decl_specs->user_defined_type_p = user_defined_p;
18544       decl_specs->redefined_builtin_type = NULL_TREE;
18545       decl_specs->type_location = location;
18546     }
18547 }
18548
18549 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18550    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18551
18552 static bool
18553 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18554 {
18555   return decl_specifiers->specs[(int) ds_friend] != 0;
18556 }
18557
18558 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18559    issue an error message indicating that TOKEN_DESC was expected.
18560
18561    Returns the token consumed, if the token had the appropriate type.
18562    Otherwise, returns NULL.  */
18563
18564 static cp_token *
18565 cp_parser_require (cp_parser* parser,
18566                    enum cpp_ttype type,
18567                    const char* token_desc)
18568 {
18569   if (cp_lexer_next_token_is (parser->lexer, type))
18570     return cp_lexer_consume_token (parser->lexer);
18571   else
18572     {
18573       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18574       if (!cp_parser_simulate_error (parser))
18575         {
18576           char *message = concat ("expected ", token_desc, NULL);
18577           cp_parser_error (parser, message);
18578           free (message);
18579         }
18580       return NULL;
18581     }
18582 }
18583
18584 /* An error message is produced if the next token is not '>'.
18585    All further tokens are skipped until the desired token is
18586    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18587
18588 static void
18589 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18590 {
18591   /* Current level of '< ... >'.  */
18592   unsigned level = 0;
18593   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18594   unsigned nesting_depth = 0;
18595
18596   /* Are we ready, yet?  If not, issue error message.  */
18597   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18598     return;
18599
18600   /* Skip tokens until the desired token is found.  */
18601   while (true)
18602     {
18603       /* Peek at the next token.  */
18604       switch (cp_lexer_peek_token (parser->lexer)->type)
18605         {
18606         case CPP_LESS:
18607           if (!nesting_depth)
18608             ++level;
18609           break;
18610
18611         case CPP_RSHIFT:
18612           if (cxx_dialect == cxx98)
18613             /* C++0x views the `>>' operator as two `>' tokens, but
18614                C++98 does not. */
18615             break;
18616           else if (!nesting_depth && level-- == 0)
18617             {
18618               /* We've hit a `>>' where the first `>' closes the
18619                  template argument list, and the second `>' is
18620                  spurious.  Just consume the `>>' and stop; we've
18621                  already produced at least one error.  */
18622               cp_lexer_consume_token (parser->lexer);
18623               return;
18624             }
18625           /* Fall through for C++0x, so we handle the second `>' in
18626              the `>>'.  */
18627
18628         case CPP_GREATER:
18629           if (!nesting_depth && level-- == 0)
18630             {
18631               /* We've reached the token we want, consume it and stop.  */
18632               cp_lexer_consume_token (parser->lexer);
18633               return;
18634             }
18635           break;
18636
18637         case CPP_OPEN_PAREN:
18638         case CPP_OPEN_SQUARE:
18639           ++nesting_depth;
18640           break;
18641
18642         case CPP_CLOSE_PAREN:
18643         case CPP_CLOSE_SQUARE:
18644           if (nesting_depth-- == 0)
18645             return;
18646           break;
18647
18648         case CPP_EOF:
18649         case CPP_PRAGMA_EOL:
18650         case CPP_SEMICOLON:
18651         case CPP_OPEN_BRACE:
18652         case CPP_CLOSE_BRACE:
18653           /* The '>' was probably forgotten, don't look further.  */
18654           return;
18655
18656         default:
18657           break;
18658         }
18659
18660       /* Consume this token.  */
18661       cp_lexer_consume_token (parser->lexer);
18662     }
18663 }
18664
18665 /* If the next token is the indicated keyword, consume it.  Otherwise,
18666    issue an error message indicating that TOKEN_DESC was expected.
18667
18668    Returns the token consumed, if the token had the appropriate type.
18669    Otherwise, returns NULL.  */
18670
18671 static cp_token *
18672 cp_parser_require_keyword (cp_parser* parser,
18673                            enum rid keyword,
18674                            const char* token_desc)
18675 {
18676   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18677
18678   if (token && token->keyword != keyword)
18679     {
18680       dyn_string_t error_msg;
18681
18682       /* Format the error message.  */
18683       error_msg = dyn_string_new (0);
18684       dyn_string_append_cstr (error_msg, "expected ");
18685       dyn_string_append_cstr (error_msg, token_desc);
18686       cp_parser_error (parser, error_msg->s);
18687       dyn_string_delete (error_msg);
18688       return NULL;
18689     }
18690
18691   return token;
18692 }
18693
18694 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18695    function-definition.  */
18696
18697 static bool
18698 cp_parser_token_starts_function_definition_p (cp_token* token)
18699 {
18700   return (/* An ordinary function-body begins with an `{'.  */
18701           token->type == CPP_OPEN_BRACE
18702           /* A ctor-initializer begins with a `:'.  */
18703           || token->type == CPP_COLON
18704           /* A function-try-block begins with `try'.  */
18705           || token->keyword == RID_TRY
18706           /* The named return value extension begins with `return'.  */
18707           || token->keyword == RID_RETURN);
18708 }
18709
18710 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18711    definition.  */
18712
18713 static bool
18714 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18715 {
18716   cp_token *token;
18717
18718   token = cp_lexer_peek_token (parser->lexer);
18719   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18720 }
18721
18722 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18723    C++0x) ending a template-argument.  */
18724
18725 static bool
18726 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18727 {
18728   cp_token *token;
18729
18730   token = cp_lexer_peek_token (parser->lexer);
18731   return (token->type == CPP_COMMA 
18732           || token->type == CPP_GREATER
18733           || token->type == CPP_ELLIPSIS
18734           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18735 }
18736
18737 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18738    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18739
18740 static bool
18741 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18742                                                      size_t n)
18743 {
18744   cp_token *token;
18745
18746   token = cp_lexer_peek_nth_token (parser->lexer, n);
18747   if (token->type == CPP_LESS)
18748     return true;
18749   /* Check for the sequence `<::' in the original code. It would be lexed as
18750      `[:', where `[' is a digraph, and there is no whitespace before
18751      `:'.  */
18752   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18753     {
18754       cp_token *token2;
18755       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18756       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18757         return true;
18758     }
18759   return false;
18760 }
18761
18762 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18763    or none_type otherwise.  */
18764
18765 static enum tag_types
18766 cp_parser_token_is_class_key (cp_token* token)
18767 {
18768   switch (token->keyword)
18769     {
18770     case RID_CLASS:
18771       return class_type;
18772     case RID_STRUCT:
18773       return record_type;
18774     case RID_UNION:
18775       return union_type;
18776
18777     default:
18778       return none_type;
18779     }
18780 }
18781
18782 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18783
18784 static void
18785 cp_parser_check_class_key (enum tag_types class_key, tree type)
18786 {
18787   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18788     permerror (input_location, "%qs tag used in naming %q#T",
18789             class_key == union_type ? "union"
18790              : class_key == record_type ? "struct" : "class",
18791              type);
18792 }
18793
18794 /* Issue an error message if DECL is redeclared with different
18795    access than its original declaration [class.access.spec/3].
18796    This applies to nested classes and nested class templates.
18797    [class.mem/1].  */
18798
18799 static void
18800 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18801 {
18802   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18803     return;
18804
18805   if ((TREE_PRIVATE (decl)
18806        != (current_access_specifier == access_private_node))
18807       || (TREE_PROTECTED (decl)
18808           != (current_access_specifier == access_protected_node)))
18809     error ("%H%qD redeclared with different access", &location, decl);
18810 }
18811
18812 /* Look for the `template' keyword, as a syntactic disambiguator.
18813    Return TRUE iff it is present, in which case it will be
18814    consumed.  */
18815
18816 static bool
18817 cp_parser_optional_template_keyword (cp_parser *parser)
18818 {
18819   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18820     {
18821       /* The `template' keyword can only be used within templates;
18822          outside templates the parser can always figure out what is a
18823          template and what is not.  */
18824       if (!processing_template_decl)
18825         {
18826           cp_token *token = cp_lexer_peek_token (parser->lexer);
18827           error ("%H%<template%> (as a disambiguator) is only allowed "
18828                  "within templates", &token->location);
18829           /* If this part of the token stream is rescanned, the same
18830              error message would be generated.  So, we purge the token
18831              from the stream.  */
18832           cp_lexer_purge_token (parser->lexer);
18833           return false;
18834         }
18835       else
18836         {
18837           /* Consume the `template' keyword.  */
18838           cp_lexer_consume_token (parser->lexer);
18839           return true;
18840         }
18841     }
18842
18843   return false;
18844 }
18845
18846 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18847    set PARSER->SCOPE, and perform other related actions.  */
18848
18849 static void
18850 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18851 {
18852   int i;
18853   struct tree_check *check_value;
18854   deferred_access_check *chk;
18855   VEC (deferred_access_check,gc) *checks;
18856
18857   /* Get the stored value.  */
18858   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18859   /* Perform any access checks that were deferred.  */
18860   checks = check_value->checks;
18861   if (checks)
18862     {
18863       for (i = 0 ;
18864            VEC_iterate (deferred_access_check, checks, i, chk) ;
18865            ++i)
18866         {
18867           perform_or_defer_access_check (chk->binfo,
18868                                          chk->decl,
18869                                          chk->diag_decl);
18870         }
18871     }
18872   /* Set the scope from the stored value.  */
18873   parser->scope = check_value->value;
18874   parser->qualifying_scope = check_value->qualifying_scope;
18875   parser->object_scope = NULL_TREE;
18876 }
18877
18878 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18879    encounter the end of a block before what we were looking for.  */
18880
18881 static bool
18882 cp_parser_cache_group (cp_parser *parser,
18883                        enum cpp_ttype end,
18884                        unsigned depth)
18885 {
18886   while (true)
18887     {
18888       cp_token *token = cp_lexer_peek_token (parser->lexer);
18889
18890       /* Abort a parenthesized expression if we encounter a semicolon.  */
18891       if ((end == CPP_CLOSE_PAREN || depth == 0)
18892           && token->type == CPP_SEMICOLON)
18893         return true;
18894       /* If we've reached the end of the file, stop.  */
18895       if (token->type == CPP_EOF
18896           || (end != CPP_PRAGMA_EOL
18897               && token->type == CPP_PRAGMA_EOL))
18898         return true;
18899       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18900         /* We've hit the end of an enclosing block, so there's been some
18901            kind of syntax error.  */
18902         return true;
18903
18904       /* Consume the token.  */
18905       cp_lexer_consume_token (parser->lexer);
18906       /* See if it starts a new group.  */
18907       if (token->type == CPP_OPEN_BRACE)
18908         {
18909           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18910           /* In theory this should probably check end == '}', but
18911              cp_parser_save_member_function_body needs it to exit
18912              after either '}' or ')' when called with ')'.  */
18913           if (depth == 0)
18914             return false;
18915         }
18916       else if (token->type == CPP_OPEN_PAREN)
18917         {
18918           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18919           if (depth == 0 && end == CPP_CLOSE_PAREN)
18920             return false;
18921         }
18922       else if (token->type == CPP_PRAGMA)
18923         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18924       else if (token->type == end)
18925         return false;
18926     }
18927 }
18928
18929 /* Begin parsing tentatively.  We always save tokens while parsing
18930    tentatively so that if the tentative parsing fails we can restore the
18931    tokens.  */
18932
18933 static void
18934 cp_parser_parse_tentatively (cp_parser* parser)
18935 {
18936   /* Enter a new parsing context.  */
18937   parser->context = cp_parser_context_new (parser->context);
18938   /* Begin saving tokens.  */
18939   cp_lexer_save_tokens (parser->lexer);
18940   /* In order to avoid repetitive access control error messages,
18941      access checks are queued up until we are no longer parsing
18942      tentatively.  */
18943   push_deferring_access_checks (dk_deferred);
18944 }
18945
18946 /* Commit to the currently active tentative parse.  */
18947
18948 static void
18949 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18950 {
18951   cp_parser_context *context;
18952   cp_lexer *lexer;
18953
18954   /* Mark all of the levels as committed.  */
18955   lexer = parser->lexer;
18956   for (context = parser->context; context->next; context = context->next)
18957     {
18958       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18959         break;
18960       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18961       while (!cp_lexer_saving_tokens (lexer))
18962         lexer = lexer->next;
18963       cp_lexer_commit_tokens (lexer);
18964     }
18965 }
18966
18967 /* Abort the currently active tentative parse.  All consumed tokens
18968    will be rolled back, and no diagnostics will be issued.  */
18969
18970 static void
18971 cp_parser_abort_tentative_parse (cp_parser* parser)
18972 {
18973   cp_parser_simulate_error (parser);
18974   /* Now, pretend that we want to see if the construct was
18975      successfully parsed.  */
18976   cp_parser_parse_definitely (parser);
18977 }
18978
18979 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18980    token stream.  Otherwise, commit to the tokens we have consumed.
18981    Returns true if no error occurred; false otherwise.  */
18982
18983 static bool
18984 cp_parser_parse_definitely (cp_parser* parser)
18985 {
18986   bool error_occurred;
18987   cp_parser_context *context;
18988
18989   /* Remember whether or not an error occurred, since we are about to
18990      destroy that information.  */
18991   error_occurred = cp_parser_error_occurred (parser);
18992   /* Remove the topmost context from the stack.  */
18993   context = parser->context;
18994   parser->context = context->next;
18995   /* If no parse errors occurred, commit to the tentative parse.  */
18996   if (!error_occurred)
18997     {
18998       /* Commit to the tokens read tentatively, unless that was
18999          already done.  */
19000       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19001         cp_lexer_commit_tokens (parser->lexer);
19002
19003       pop_to_parent_deferring_access_checks ();
19004     }
19005   /* Otherwise, if errors occurred, roll back our state so that things
19006      are just as they were before we began the tentative parse.  */
19007   else
19008     {
19009       cp_lexer_rollback_tokens (parser->lexer);
19010       pop_deferring_access_checks ();
19011     }
19012   /* Add the context to the front of the free list.  */
19013   context->next = cp_parser_context_free_list;
19014   cp_parser_context_free_list = context;
19015
19016   return !error_occurred;
19017 }
19018
19019 /* Returns true if we are parsing tentatively and are not committed to
19020    this tentative parse.  */
19021
19022 static bool
19023 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19024 {
19025   return (cp_parser_parsing_tentatively (parser)
19026           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19027 }
19028
19029 /* Returns nonzero iff an error has occurred during the most recent
19030    tentative parse.  */
19031
19032 static bool
19033 cp_parser_error_occurred (cp_parser* parser)
19034 {
19035   return (cp_parser_parsing_tentatively (parser)
19036           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19037 }
19038
19039 /* Returns nonzero if GNU extensions are allowed.  */
19040
19041 static bool
19042 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19043 {
19044   return parser->allow_gnu_extensions_p;
19045 }
19046 \f
19047 /* Objective-C++ Productions */
19048
19049
19050 /* Parse an Objective-C expression, which feeds into a primary-expression
19051    above.
19052
19053    objc-expression:
19054      objc-message-expression
19055      objc-string-literal
19056      objc-encode-expression
19057      objc-protocol-expression
19058      objc-selector-expression
19059
19060   Returns a tree representation of the expression.  */
19061
19062 static tree
19063 cp_parser_objc_expression (cp_parser* parser)
19064 {
19065   /* Try to figure out what kind of declaration is present.  */
19066   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19067
19068   switch (kwd->type)
19069     {
19070     case CPP_OPEN_SQUARE:
19071       return cp_parser_objc_message_expression (parser);
19072
19073     case CPP_OBJC_STRING:
19074       kwd = cp_lexer_consume_token (parser->lexer);
19075       return objc_build_string_object (kwd->u.value);
19076
19077     case CPP_KEYWORD:
19078       switch (kwd->keyword)
19079         {
19080         case RID_AT_ENCODE:
19081           return cp_parser_objc_encode_expression (parser);
19082
19083         case RID_AT_PROTOCOL:
19084           return cp_parser_objc_protocol_expression (parser);
19085
19086         case RID_AT_SELECTOR:
19087           return cp_parser_objc_selector_expression (parser);
19088
19089         default:
19090           break;
19091         }
19092     default:
19093       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19094              &kwd->location, kwd->u.value);
19095       cp_parser_skip_to_end_of_block_or_statement (parser);
19096     }
19097
19098   return error_mark_node;
19099 }
19100
19101 /* Parse an Objective-C message expression.
19102
19103    objc-message-expression:
19104      [ objc-message-receiver objc-message-args ]
19105
19106    Returns a representation of an Objective-C message.  */
19107
19108 static tree
19109 cp_parser_objc_message_expression (cp_parser* parser)
19110 {
19111   tree receiver, messageargs;
19112
19113   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19114   receiver = cp_parser_objc_message_receiver (parser);
19115   messageargs = cp_parser_objc_message_args (parser);
19116   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19117
19118   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19119 }
19120
19121 /* Parse an objc-message-receiver.
19122
19123    objc-message-receiver:
19124      expression
19125      simple-type-specifier
19126
19127   Returns a representation of the type or expression.  */
19128
19129 static tree
19130 cp_parser_objc_message_receiver (cp_parser* parser)
19131 {
19132   tree rcv;
19133
19134   /* An Objective-C message receiver may be either (1) a type
19135      or (2) an expression.  */
19136   cp_parser_parse_tentatively (parser);
19137   rcv = cp_parser_expression (parser, false, NULL);
19138
19139   if (cp_parser_parse_definitely (parser))
19140     return rcv;
19141
19142   rcv = cp_parser_simple_type_specifier (parser,
19143                                          /*decl_specs=*/NULL,
19144                                          CP_PARSER_FLAGS_NONE);
19145
19146   return objc_get_class_reference (rcv);
19147 }
19148
19149 /* Parse the arguments and selectors comprising an Objective-C message.
19150
19151    objc-message-args:
19152      objc-selector
19153      objc-selector-args
19154      objc-selector-args , objc-comma-args
19155
19156    objc-selector-args:
19157      objc-selector [opt] : assignment-expression
19158      objc-selector-args objc-selector [opt] : assignment-expression
19159
19160    objc-comma-args:
19161      assignment-expression
19162      objc-comma-args , assignment-expression
19163
19164    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19165    selector arguments and TREE_VALUE containing a list of comma
19166    arguments.  */
19167
19168 static tree
19169 cp_parser_objc_message_args (cp_parser* parser)
19170 {
19171   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19172   bool maybe_unary_selector_p = true;
19173   cp_token *token = cp_lexer_peek_token (parser->lexer);
19174
19175   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19176     {
19177       tree selector = NULL_TREE, arg;
19178
19179       if (token->type != CPP_COLON)
19180         selector = cp_parser_objc_selector (parser);
19181
19182       /* Detect if we have a unary selector.  */
19183       if (maybe_unary_selector_p
19184           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19185         return build_tree_list (selector, NULL_TREE);
19186
19187       maybe_unary_selector_p = false;
19188       cp_parser_require (parser, CPP_COLON, "%<:%>");
19189       arg = cp_parser_assignment_expression (parser, false, NULL);
19190
19191       sel_args
19192         = chainon (sel_args,
19193                    build_tree_list (selector, arg));
19194
19195       token = cp_lexer_peek_token (parser->lexer);
19196     }
19197
19198   /* Handle non-selector arguments, if any. */
19199   while (token->type == CPP_COMMA)
19200     {
19201       tree arg;
19202
19203       cp_lexer_consume_token (parser->lexer);
19204       arg = cp_parser_assignment_expression (parser, false, NULL);
19205
19206       addl_args
19207         = chainon (addl_args,
19208                    build_tree_list (NULL_TREE, arg));
19209
19210       token = cp_lexer_peek_token (parser->lexer);
19211     }
19212
19213   return build_tree_list (sel_args, addl_args);
19214 }
19215
19216 /* Parse an Objective-C encode expression.
19217
19218    objc-encode-expression:
19219      @encode objc-typename
19220
19221    Returns an encoded representation of the type argument.  */
19222
19223 static tree
19224 cp_parser_objc_encode_expression (cp_parser* parser)
19225 {
19226   tree type;
19227   cp_token *token;
19228
19229   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19230   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19231   token = cp_lexer_peek_token (parser->lexer);
19232   type = complete_type (cp_parser_type_id (parser));
19233   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19234
19235   if (!type)
19236     {
19237       error ("%H%<@encode%> must specify a type as an argument",
19238              &token->location);
19239       return error_mark_node;
19240     }
19241
19242   return objc_build_encode_expr (type);
19243 }
19244
19245 /* Parse an Objective-C @defs expression.  */
19246
19247 static tree
19248 cp_parser_objc_defs_expression (cp_parser *parser)
19249 {
19250   tree name;
19251
19252   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19253   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19254   name = cp_parser_identifier (parser);
19255   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19256
19257   return objc_get_class_ivars (name);
19258 }
19259
19260 /* Parse an Objective-C protocol expression.
19261
19262   objc-protocol-expression:
19263     @protocol ( identifier )
19264
19265   Returns a representation of the protocol expression.  */
19266
19267 static tree
19268 cp_parser_objc_protocol_expression (cp_parser* parser)
19269 {
19270   tree proto;
19271
19272   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19273   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19274   proto = cp_parser_identifier (parser);
19275   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19276
19277   return objc_build_protocol_expr (proto);
19278 }
19279
19280 /* Parse an Objective-C selector expression.
19281
19282    objc-selector-expression:
19283      @selector ( objc-method-signature )
19284
19285    objc-method-signature:
19286      objc-selector
19287      objc-selector-seq
19288
19289    objc-selector-seq:
19290      objc-selector :
19291      objc-selector-seq objc-selector :
19292
19293   Returns a representation of the method selector.  */
19294
19295 static tree
19296 cp_parser_objc_selector_expression (cp_parser* parser)
19297 {
19298   tree sel_seq = NULL_TREE;
19299   bool maybe_unary_selector_p = true;
19300   cp_token *token;
19301
19302   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19303   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19304   token = cp_lexer_peek_token (parser->lexer);
19305
19306   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19307          || token->type == CPP_SCOPE)
19308     {
19309       tree selector = NULL_TREE;
19310
19311       if (token->type != CPP_COLON
19312           || token->type == CPP_SCOPE)
19313         selector = cp_parser_objc_selector (parser);
19314
19315       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19316           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19317         {
19318           /* Detect if we have a unary selector.  */
19319           if (maybe_unary_selector_p)
19320             {
19321               sel_seq = selector;
19322               goto finish_selector;
19323             }
19324           else
19325             {
19326               cp_parser_error (parser, "expected %<:%>");
19327             }
19328         }
19329       maybe_unary_selector_p = false;
19330       token = cp_lexer_consume_token (parser->lexer);
19331
19332       if (token->type == CPP_SCOPE)
19333         {
19334           sel_seq
19335             = chainon (sel_seq,
19336                        build_tree_list (selector, NULL_TREE));
19337           sel_seq
19338             = chainon (sel_seq,
19339                        build_tree_list (NULL_TREE, NULL_TREE));
19340         }
19341       else
19342         sel_seq
19343           = chainon (sel_seq,
19344                      build_tree_list (selector, NULL_TREE));
19345
19346       token = cp_lexer_peek_token (parser->lexer);
19347     }
19348
19349  finish_selector:
19350   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19351
19352   return objc_build_selector_expr (sel_seq);
19353 }
19354
19355 /* Parse a list of identifiers.
19356
19357    objc-identifier-list:
19358      identifier
19359      objc-identifier-list , identifier
19360
19361    Returns a TREE_LIST of identifier nodes.  */
19362
19363 static tree
19364 cp_parser_objc_identifier_list (cp_parser* parser)
19365 {
19366   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19367   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19368
19369   while (sep->type == CPP_COMMA)
19370     {
19371       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19372       list = chainon (list,
19373                       build_tree_list (NULL_TREE,
19374                                        cp_parser_identifier (parser)));
19375       sep = cp_lexer_peek_token (parser->lexer);
19376     }
19377
19378   return list;
19379 }
19380
19381 /* Parse an Objective-C alias declaration.
19382
19383    objc-alias-declaration:
19384      @compatibility_alias identifier identifier ;
19385
19386    This function registers the alias mapping with the Objective-C front end.
19387    It returns nothing.  */
19388
19389 static void
19390 cp_parser_objc_alias_declaration (cp_parser* parser)
19391 {
19392   tree alias, orig;
19393
19394   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19395   alias = cp_parser_identifier (parser);
19396   orig = cp_parser_identifier (parser);
19397   objc_declare_alias (alias, orig);
19398   cp_parser_consume_semicolon_at_end_of_statement (parser);
19399 }
19400
19401 /* Parse an Objective-C class forward-declaration.
19402
19403    objc-class-declaration:
19404      @class objc-identifier-list ;
19405
19406    The function registers the forward declarations with the Objective-C
19407    front end.  It returns nothing.  */
19408
19409 static void
19410 cp_parser_objc_class_declaration (cp_parser* parser)
19411 {
19412   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19413   objc_declare_class (cp_parser_objc_identifier_list (parser));
19414   cp_parser_consume_semicolon_at_end_of_statement (parser);
19415 }
19416
19417 /* Parse a list of Objective-C protocol references.
19418
19419    objc-protocol-refs-opt:
19420      objc-protocol-refs [opt]
19421
19422    objc-protocol-refs:
19423      < objc-identifier-list >
19424
19425    Returns a TREE_LIST of identifiers, if any.  */
19426
19427 static tree
19428 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19429 {
19430   tree protorefs = NULL_TREE;
19431
19432   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19433     {
19434       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19435       protorefs = cp_parser_objc_identifier_list (parser);
19436       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19437     }
19438
19439   return protorefs;
19440 }
19441
19442 /* Parse a Objective-C visibility specification.  */
19443
19444 static void
19445 cp_parser_objc_visibility_spec (cp_parser* parser)
19446 {
19447   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19448
19449   switch (vis->keyword)
19450     {
19451     case RID_AT_PRIVATE:
19452       objc_set_visibility (2);
19453       break;
19454     case RID_AT_PROTECTED:
19455       objc_set_visibility (0);
19456       break;
19457     case RID_AT_PUBLIC:
19458       objc_set_visibility (1);
19459       break;
19460     default:
19461       return;
19462     }
19463
19464   /* Eat '@private'/'@protected'/'@public'.  */
19465   cp_lexer_consume_token (parser->lexer);
19466 }
19467
19468 /* Parse an Objective-C method type.  */
19469
19470 static void
19471 cp_parser_objc_method_type (cp_parser* parser)
19472 {
19473   objc_set_method_type
19474    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19475     ? PLUS_EXPR
19476     : MINUS_EXPR);
19477 }
19478
19479 /* Parse an Objective-C protocol qualifier.  */
19480
19481 static tree
19482 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19483 {
19484   tree quals = NULL_TREE, node;
19485   cp_token *token = cp_lexer_peek_token (parser->lexer);
19486
19487   node = token->u.value;
19488
19489   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19490          && (node == ridpointers [(int) RID_IN]
19491              || node == ridpointers [(int) RID_OUT]
19492              || node == ridpointers [(int) RID_INOUT]
19493              || node == ridpointers [(int) RID_BYCOPY]
19494              || node == ridpointers [(int) RID_BYREF]
19495              || node == ridpointers [(int) RID_ONEWAY]))
19496     {
19497       quals = tree_cons (NULL_TREE, node, quals);
19498       cp_lexer_consume_token (parser->lexer);
19499       token = cp_lexer_peek_token (parser->lexer);
19500       node = token->u.value;
19501     }
19502
19503   return quals;
19504 }
19505
19506 /* Parse an Objective-C typename.  */
19507
19508 static tree
19509 cp_parser_objc_typename (cp_parser* parser)
19510 {
19511   tree type_name = NULL_TREE;
19512
19513   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19514     {
19515       tree proto_quals, cp_type = NULL_TREE;
19516
19517       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19518       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19519
19520       /* An ObjC type name may consist of just protocol qualifiers, in which
19521          case the type shall default to 'id'.  */
19522       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19523         cp_type = cp_parser_type_id (parser);
19524
19525       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19526       type_name = build_tree_list (proto_quals, cp_type);
19527     }
19528
19529   return type_name;
19530 }
19531
19532 /* Check to see if TYPE refers to an Objective-C selector name.  */
19533
19534 static bool
19535 cp_parser_objc_selector_p (enum cpp_ttype type)
19536 {
19537   return (type == CPP_NAME || type == CPP_KEYWORD
19538           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19539           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19540           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19541           || type == CPP_XOR || type == CPP_XOR_EQ);
19542 }
19543
19544 /* Parse an Objective-C selector.  */
19545
19546 static tree
19547 cp_parser_objc_selector (cp_parser* parser)
19548 {
19549   cp_token *token = cp_lexer_consume_token (parser->lexer);
19550
19551   if (!cp_parser_objc_selector_p (token->type))
19552     {
19553       error ("%Hinvalid Objective-C++ selector name", &token->location);
19554       return error_mark_node;
19555     }
19556
19557   /* C++ operator names are allowed to appear in ObjC selectors.  */
19558   switch (token->type)
19559     {
19560     case CPP_AND_AND: return get_identifier ("and");
19561     case CPP_AND_EQ: return get_identifier ("and_eq");
19562     case CPP_AND: return get_identifier ("bitand");
19563     case CPP_OR: return get_identifier ("bitor");
19564     case CPP_COMPL: return get_identifier ("compl");
19565     case CPP_NOT: return get_identifier ("not");
19566     case CPP_NOT_EQ: return get_identifier ("not_eq");
19567     case CPP_OR_OR: return get_identifier ("or");
19568     case CPP_OR_EQ: return get_identifier ("or_eq");
19569     case CPP_XOR: return get_identifier ("xor");
19570     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19571     default: return token->u.value;
19572     }
19573 }
19574
19575 /* Parse an Objective-C params list.  */
19576
19577 static tree
19578 cp_parser_objc_method_keyword_params (cp_parser* parser)
19579 {
19580   tree params = NULL_TREE;
19581   bool maybe_unary_selector_p = true;
19582   cp_token *token = cp_lexer_peek_token (parser->lexer);
19583
19584   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19585     {
19586       tree selector = NULL_TREE, type_name, identifier;
19587
19588       if (token->type != CPP_COLON)
19589         selector = cp_parser_objc_selector (parser);
19590
19591       /* Detect if we have a unary selector.  */
19592       if (maybe_unary_selector_p
19593           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19594         return selector;
19595
19596       maybe_unary_selector_p = false;
19597       cp_parser_require (parser, CPP_COLON, "%<:%>");
19598       type_name = cp_parser_objc_typename (parser);
19599       identifier = cp_parser_identifier (parser);
19600
19601       params
19602         = chainon (params,
19603                    objc_build_keyword_decl (selector,
19604                                             type_name,
19605                                             identifier));
19606
19607       token = cp_lexer_peek_token (parser->lexer);
19608     }
19609
19610   return params;
19611 }
19612
19613 /* Parse the non-keyword Objective-C params.  */
19614
19615 static tree
19616 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19617 {
19618   tree params = make_node (TREE_LIST);
19619   cp_token *token = cp_lexer_peek_token (parser->lexer);
19620   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19621
19622   while (token->type == CPP_COMMA)
19623     {
19624       cp_parameter_declarator *parmdecl;
19625       tree parm;
19626
19627       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19628       token = cp_lexer_peek_token (parser->lexer);
19629
19630       if (token->type == CPP_ELLIPSIS)
19631         {
19632           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19633           *ellipsisp = true;
19634           break;
19635         }
19636
19637       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19638       parm = grokdeclarator (parmdecl->declarator,
19639                              &parmdecl->decl_specifiers,
19640                              PARM, /*initialized=*/0,
19641                              /*attrlist=*/NULL);
19642
19643       chainon (params, build_tree_list (NULL_TREE, parm));
19644       token = cp_lexer_peek_token (parser->lexer);
19645     }
19646
19647   return params;
19648 }
19649
19650 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19651
19652 static void
19653 cp_parser_objc_interstitial_code (cp_parser* parser)
19654 {
19655   cp_token *token = cp_lexer_peek_token (parser->lexer);
19656
19657   /* If the next token is `extern' and the following token is a string
19658      literal, then we have a linkage specification.  */
19659   if (token->keyword == RID_EXTERN
19660       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19661     cp_parser_linkage_specification (parser);
19662   /* Handle #pragma, if any.  */
19663   else if (token->type == CPP_PRAGMA)
19664     cp_parser_pragma (parser, pragma_external);
19665   /* Allow stray semicolons.  */
19666   else if (token->type == CPP_SEMICOLON)
19667     cp_lexer_consume_token (parser->lexer);
19668   /* Finally, try to parse a block-declaration, or a function-definition.  */
19669   else
19670     cp_parser_block_declaration (parser, /*statement_p=*/false);
19671 }
19672
19673 /* Parse a method signature.  */
19674
19675 static tree
19676 cp_parser_objc_method_signature (cp_parser* parser)
19677 {
19678   tree rettype, kwdparms, optparms;
19679   bool ellipsis = false;
19680
19681   cp_parser_objc_method_type (parser);
19682   rettype = cp_parser_objc_typename (parser);
19683   kwdparms = cp_parser_objc_method_keyword_params (parser);
19684   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19685
19686   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19687 }
19688
19689 /* Pars an Objective-C method prototype list.  */
19690
19691 static void
19692 cp_parser_objc_method_prototype_list (cp_parser* parser)
19693 {
19694   cp_token *token = cp_lexer_peek_token (parser->lexer);
19695
19696   while (token->keyword != RID_AT_END)
19697     {
19698       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19699         {
19700           objc_add_method_declaration
19701            (cp_parser_objc_method_signature (parser));
19702           cp_parser_consume_semicolon_at_end_of_statement (parser);
19703         }
19704       else
19705         /* Allow for interspersed non-ObjC++ code.  */
19706         cp_parser_objc_interstitial_code (parser);
19707
19708       token = cp_lexer_peek_token (parser->lexer);
19709     }
19710
19711   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19712   objc_finish_interface ();
19713 }
19714
19715 /* Parse an Objective-C method definition list.  */
19716
19717 static void
19718 cp_parser_objc_method_definition_list (cp_parser* parser)
19719 {
19720   cp_token *token = cp_lexer_peek_token (parser->lexer);
19721
19722   while (token->keyword != RID_AT_END)
19723     {
19724       tree meth;
19725
19726       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19727         {
19728           push_deferring_access_checks (dk_deferred);
19729           objc_start_method_definition
19730            (cp_parser_objc_method_signature (parser));
19731
19732           /* For historical reasons, we accept an optional semicolon.  */
19733           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19734             cp_lexer_consume_token (parser->lexer);
19735
19736           perform_deferred_access_checks ();
19737           stop_deferring_access_checks ();
19738           meth = cp_parser_function_definition_after_declarator (parser,
19739                                                                  false);
19740           pop_deferring_access_checks ();
19741           objc_finish_method_definition (meth);
19742         }
19743       else
19744         /* Allow for interspersed non-ObjC++ code.  */
19745         cp_parser_objc_interstitial_code (parser);
19746
19747       token = cp_lexer_peek_token (parser->lexer);
19748     }
19749
19750   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19751   objc_finish_implementation ();
19752 }
19753
19754 /* Parse Objective-C ivars.  */
19755
19756 static void
19757 cp_parser_objc_class_ivars (cp_parser* parser)
19758 {
19759   cp_token *token = cp_lexer_peek_token (parser->lexer);
19760
19761   if (token->type != CPP_OPEN_BRACE)
19762     return;     /* No ivars specified.  */
19763
19764   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19765   token = cp_lexer_peek_token (parser->lexer);
19766
19767   while (token->type != CPP_CLOSE_BRACE)
19768     {
19769       cp_decl_specifier_seq declspecs;
19770       int decl_class_or_enum_p;
19771       tree prefix_attributes;
19772
19773       cp_parser_objc_visibility_spec (parser);
19774
19775       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19776         break;
19777
19778       cp_parser_decl_specifier_seq (parser,
19779                                     CP_PARSER_FLAGS_OPTIONAL,
19780                                     &declspecs,
19781                                     &decl_class_or_enum_p);
19782       prefix_attributes = declspecs.attributes;
19783       declspecs.attributes = NULL_TREE;
19784
19785       /* Keep going until we hit the `;' at the end of the
19786          declaration.  */
19787       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19788         {
19789           tree width = NULL_TREE, attributes, first_attribute, decl;
19790           cp_declarator *declarator = NULL;
19791           int ctor_dtor_or_conv_p;
19792
19793           /* Check for a (possibly unnamed) bitfield declaration.  */
19794           token = cp_lexer_peek_token (parser->lexer);
19795           if (token->type == CPP_COLON)
19796             goto eat_colon;
19797
19798           if (token->type == CPP_NAME
19799               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19800                   == CPP_COLON))
19801             {
19802               /* Get the name of the bitfield.  */
19803               declarator = make_id_declarator (NULL_TREE,
19804                                                cp_parser_identifier (parser),
19805                                                sfk_none);
19806
19807              eat_colon:
19808               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19809               /* Get the width of the bitfield.  */
19810               width
19811                 = cp_parser_constant_expression (parser,
19812                                                  /*allow_non_constant=*/false,
19813                                                  NULL);
19814             }
19815           else
19816             {
19817               /* Parse the declarator.  */
19818               declarator
19819                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19820                                         &ctor_dtor_or_conv_p,
19821                                         /*parenthesized_p=*/NULL,
19822                                         /*member_p=*/false);
19823             }
19824
19825           /* Look for attributes that apply to the ivar.  */
19826           attributes = cp_parser_attributes_opt (parser);
19827           /* Remember which attributes are prefix attributes and
19828              which are not.  */
19829           first_attribute = attributes;
19830           /* Combine the attributes.  */
19831           attributes = chainon (prefix_attributes, attributes);
19832
19833           if (width)
19834               /* Create the bitfield declaration.  */
19835               decl = grokbitfield (declarator, &declspecs,
19836                                    width,
19837                                    attributes);
19838           else
19839             decl = grokfield (declarator, &declspecs,
19840                               NULL_TREE, /*init_const_expr_p=*/false,
19841                               NULL_TREE, attributes);
19842
19843           /* Add the instance variable.  */
19844           objc_add_instance_variable (decl);
19845
19846           /* Reset PREFIX_ATTRIBUTES.  */
19847           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19848             attributes = TREE_CHAIN (attributes);
19849           if (attributes)
19850             TREE_CHAIN (attributes) = NULL_TREE;
19851
19852           token = cp_lexer_peek_token (parser->lexer);
19853
19854           if (token->type == CPP_COMMA)
19855             {
19856               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19857               continue;
19858             }
19859           break;
19860         }
19861
19862       cp_parser_consume_semicolon_at_end_of_statement (parser);
19863       token = cp_lexer_peek_token (parser->lexer);
19864     }
19865
19866   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19867   /* For historical reasons, we accept an optional semicolon.  */
19868   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19869     cp_lexer_consume_token (parser->lexer);
19870 }
19871
19872 /* Parse an Objective-C protocol declaration.  */
19873
19874 static void
19875 cp_parser_objc_protocol_declaration (cp_parser* parser)
19876 {
19877   tree proto, protorefs;
19878   cp_token *tok;
19879
19880   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19881   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19882     {
19883       tok = cp_lexer_peek_token (parser->lexer);
19884       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19885       goto finish;
19886     }
19887
19888   /* See if we have a forward declaration or a definition.  */
19889   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19890
19891   /* Try a forward declaration first.  */
19892   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19893     {
19894       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19895      finish:
19896       cp_parser_consume_semicolon_at_end_of_statement (parser);
19897     }
19898
19899   /* Ok, we got a full-fledged definition (or at least should).  */
19900   else
19901     {
19902       proto = cp_parser_identifier (parser);
19903       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19904       objc_start_protocol (proto, protorefs);
19905       cp_parser_objc_method_prototype_list (parser);
19906     }
19907 }
19908
19909 /* Parse an Objective-C superclass or category.  */
19910
19911 static void
19912 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19913                                                           tree *categ)
19914 {
19915   cp_token *next = cp_lexer_peek_token (parser->lexer);
19916
19917   *super = *categ = NULL_TREE;
19918   if (next->type == CPP_COLON)
19919     {
19920       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19921       *super = cp_parser_identifier (parser);
19922     }
19923   else if (next->type == CPP_OPEN_PAREN)
19924     {
19925       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19926       *categ = cp_parser_identifier (parser);
19927       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19928     }
19929 }
19930
19931 /* Parse an Objective-C class interface.  */
19932
19933 static void
19934 cp_parser_objc_class_interface (cp_parser* parser)
19935 {
19936   tree name, super, categ, protos;
19937
19938   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19939   name = cp_parser_identifier (parser);
19940   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19941   protos = cp_parser_objc_protocol_refs_opt (parser);
19942
19943   /* We have either a class or a category on our hands.  */
19944   if (categ)
19945     objc_start_category_interface (name, categ, protos);
19946   else
19947     {
19948       objc_start_class_interface (name, super, protos);
19949       /* Handle instance variable declarations, if any.  */
19950       cp_parser_objc_class_ivars (parser);
19951       objc_continue_interface ();
19952     }
19953
19954   cp_parser_objc_method_prototype_list (parser);
19955 }
19956
19957 /* Parse an Objective-C class implementation.  */
19958
19959 static void
19960 cp_parser_objc_class_implementation (cp_parser* parser)
19961 {
19962   tree name, super, categ;
19963
19964   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19965   name = cp_parser_identifier (parser);
19966   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19967
19968   /* We have either a class or a category on our hands.  */
19969   if (categ)
19970     objc_start_category_implementation (name, categ);
19971   else
19972     {
19973       objc_start_class_implementation (name, super);
19974       /* Handle instance variable declarations, if any.  */
19975       cp_parser_objc_class_ivars (parser);
19976       objc_continue_implementation ();
19977     }
19978
19979   cp_parser_objc_method_definition_list (parser);
19980 }
19981
19982 /* Consume the @end token and finish off the implementation.  */
19983
19984 static void
19985 cp_parser_objc_end_implementation (cp_parser* parser)
19986 {
19987   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19988   objc_finish_implementation ();
19989 }
19990
19991 /* Parse an Objective-C declaration.  */
19992
19993 static void
19994 cp_parser_objc_declaration (cp_parser* parser)
19995 {
19996   /* Try to figure out what kind of declaration is present.  */
19997   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19998
19999   switch (kwd->keyword)
20000     {
20001     case RID_AT_ALIAS:
20002       cp_parser_objc_alias_declaration (parser);
20003       break;
20004     case RID_AT_CLASS:
20005       cp_parser_objc_class_declaration (parser);
20006       break;
20007     case RID_AT_PROTOCOL:
20008       cp_parser_objc_protocol_declaration (parser);
20009       break;
20010     case RID_AT_INTERFACE:
20011       cp_parser_objc_class_interface (parser);
20012       break;
20013     case RID_AT_IMPLEMENTATION:
20014       cp_parser_objc_class_implementation (parser);
20015       break;
20016     case RID_AT_END:
20017       cp_parser_objc_end_implementation (parser);
20018       break;
20019     default:
20020       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20021              &kwd->location, kwd->u.value);
20022       cp_parser_skip_to_end_of_block_or_statement (parser);
20023     }
20024 }
20025
20026 /* Parse an Objective-C try-catch-finally statement.
20027
20028    objc-try-catch-finally-stmt:
20029      @try compound-statement objc-catch-clause-seq [opt]
20030        objc-finally-clause [opt]
20031
20032    objc-catch-clause-seq:
20033      objc-catch-clause objc-catch-clause-seq [opt]
20034
20035    objc-catch-clause:
20036      @catch ( exception-declaration ) compound-statement
20037
20038    objc-finally-clause
20039      @finally compound-statement
20040
20041    Returns NULL_TREE.  */
20042
20043 static tree
20044 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20045   location_t location;
20046   tree stmt;
20047
20048   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20049   location = cp_lexer_peek_token (parser->lexer)->location;
20050   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20051      node, lest it get absorbed into the surrounding block.  */
20052   stmt = push_stmt_list ();
20053   cp_parser_compound_statement (parser, NULL, false);
20054   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20055
20056   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20057     {
20058       cp_parameter_declarator *parmdecl;
20059       tree parm;
20060
20061       cp_lexer_consume_token (parser->lexer);
20062       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20063       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20064       parm = grokdeclarator (parmdecl->declarator,
20065                              &parmdecl->decl_specifiers,
20066                              PARM, /*initialized=*/0,
20067                              /*attrlist=*/NULL);
20068       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20069       objc_begin_catch_clause (parm);
20070       cp_parser_compound_statement (parser, NULL, false);
20071       objc_finish_catch_clause ();
20072     }
20073
20074   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20075     {
20076       cp_lexer_consume_token (parser->lexer);
20077       location = cp_lexer_peek_token (parser->lexer)->location;
20078       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20079          node, lest it get absorbed into the surrounding block.  */
20080       stmt = push_stmt_list ();
20081       cp_parser_compound_statement (parser, NULL, false);
20082       objc_build_finally_clause (location, pop_stmt_list (stmt));
20083     }
20084
20085   return objc_finish_try_stmt ();
20086 }
20087
20088 /* Parse an Objective-C synchronized statement.
20089
20090    objc-synchronized-stmt:
20091      @synchronized ( expression ) compound-statement
20092
20093    Returns NULL_TREE.  */
20094
20095 static tree
20096 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20097   location_t location;
20098   tree lock, stmt;
20099
20100   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20101
20102   location = cp_lexer_peek_token (parser->lexer)->location;
20103   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20104   lock = cp_parser_expression (parser, false, NULL);
20105   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20106
20107   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20108      node, lest it get absorbed into the surrounding block.  */
20109   stmt = push_stmt_list ();
20110   cp_parser_compound_statement (parser, NULL, false);
20111
20112   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20113 }
20114
20115 /* Parse an Objective-C throw statement.
20116
20117    objc-throw-stmt:
20118      @throw assignment-expression [opt] ;
20119
20120    Returns a constructed '@throw' statement.  */
20121
20122 static tree
20123 cp_parser_objc_throw_statement (cp_parser *parser) {
20124   tree expr = NULL_TREE;
20125
20126   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20127
20128   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20129     expr = cp_parser_assignment_expression (parser, false, NULL);
20130
20131   cp_parser_consume_semicolon_at_end_of_statement (parser);
20132
20133   return objc_build_throw_stmt (expr);
20134 }
20135
20136 /* Parse an Objective-C statement.  */
20137
20138 static tree
20139 cp_parser_objc_statement (cp_parser * parser) {
20140   /* Try to figure out what kind of declaration is present.  */
20141   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20142
20143   switch (kwd->keyword)
20144     {
20145     case RID_AT_TRY:
20146       return cp_parser_objc_try_catch_finally_statement (parser);
20147     case RID_AT_SYNCHRONIZED:
20148       return cp_parser_objc_synchronized_statement (parser);
20149     case RID_AT_THROW:
20150       return cp_parser_objc_throw_statement (parser);
20151     default:
20152       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20153              &kwd->location, kwd->u.value);
20154       cp_parser_skip_to_end_of_block_or_statement (parser);
20155     }
20156
20157   return error_mark_node;
20158 }
20159 \f
20160 /* OpenMP 2.5 parsing routines.  */
20161
20162 /* Returns name of the next clause.
20163    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20164    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20165    returned and the token is consumed.  */
20166
20167 static pragma_omp_clause
20168 cp_parser_omp_clause_name (cp_parser *parser)
20169 {
20170   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20171
20172   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20173     result = PRAGMA_OMP_CLAUSE_IF;
20174   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20175     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20176   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20177     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20178   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20179     {
20180       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20181       const char *p = IDENTIFIER_POINTER (id);
20182
20183       switch (p[0])
20184         {
20185         case 'c':
20186           if (!strcmp ("collapse", p))
20187             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20188           else if (!strcmp ("copyin", p))
20189             result = PRAGMA_OMP_CLAUSE_COPYIN;
20190           else if (!strcmp ("copyprivate", p))
20191             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20192           break;
20193         case 'f':
20194           if (!strcmp ("firstprivate", p))
20195             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20196           break;
20197         case 'l':
20198           if (!strcmp ("lastprivate", p))
20199             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20200           break;
20201         case 'n':
20202           if (!strcmp ("nowait", p))
20203             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20204           else if (!strcmp ("num_threads", p))
20205             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20206           break;
20207         case 'o':
20208           if (!strcmp ("ordered", p))
20209             result = PRAGMA_OMP_CLAUSE_ORDERED;
20210           break;
20211         case 'r':
20212           if (!strcmp ("reduction", p))
20213             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20214           break;
20215         case 's':
20216           if (!strcmp ("schedule", p))
20217             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20218           else if (!strcmp ("shared", p))
20219             result = PRAGMA_OMP_CLAUSE_SHARED;
20220           break;
20221         case 'u':
20222           if (!strcmp ("untied", p))
20223             result = PRAGMA_OMP_CLAUSE_UNTIED;
20224           break;
20225         }
20226     }
20227
20228   if (result != PRAGMA_OMP_CLAUSE_NONE)
20229     cp_lexer_consume_token (parser->lexer);
20230
20231   return result;
20232 }
20233
20234 /* Validate that a clause of the given type does not already exist.  */
20235
20236 static void
20237 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20238                            const char *name, location_t location)
20239 {
20240   tree c;
20241
20242   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20243     if (OMP_CLAUSE_CODE (c) == code)
20244       {
20245         error ("%Htoo many %qs clauses", &location, name);
20246         break;
20247       }
20248 }
20249
20250 /* OpenMP 2.5:
20251    variable-list:
20252      identifier
20253      variable-list , identifier
20254
20255    In addition, we match a closing parenthesis.  An opening parenthesis
20256    will have been consumed by the caller.
20257
20258    If KIND is nonzero, create the appropriate node and install the decl
20259    in OMP_CLAUSE_DECL and add the node to the head of the list.
20260
20261    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20262    return the list created.  */
20263
20264 static tree
20265 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20266                                 tree list)
20267 {
20268   cp_token *token;
20269   while (1)
20270     {
20271       tree name, decl;
20272
20273       token = cp_lexer_peek_token (parser->lexer);
20274       name = cp_parser_id_expression (parser, /*template_p=*/false,
20275                                       /*check_dependency_p=*/true,
20276                                       /*template_p=*/NULL,
20277                                       /*declarator_p=*/false,
20278                                       /*optional_p=*/false);
20279       if (name == error_mark_node)
20280         goto skip_comma;
20281
20282       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20283       if (decl == error_mark_node)
20284         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20285       else if (kind != 0)
20286         {
20287           tree u = build_omp_clause (kind);
20288           OMP_CLAUSE_DECL (u) = decl;
20289           OMP_CLAUSE_CHAIN (u) = list;
20290           list = u;
20291         }
20292       else
20293         list = tree_cons (decl, NULL_TREE, list);
20294
20295     get_comma:
20296       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20297         break;
20298       cp_lexer_consume_token (parser->lexer);
20299     }
20300
20301   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20302     {
20303       int ending;
20304
20305       /* Try to resync to an unnested comma.  Copied from
20306          cp_parser_parenthesized_expression_list.  */
20307     skip_comma:
20308       ending = cp_parser_skip_to_closing_parenthesis (parser,
20309                                                       /*recovering=*/true,
20310                                                       /*or_comma=*/true,
20311                                                       /*consume_paren=*/true);
20312       if (ending < 0)
20313         goto get_comma;
20314     }
20315
20316   return list;
20317 }
20318
20319 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20320    common case for omp clauses.  */
20321
20322 static tree
20323 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20324 {
20325   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20326     return cp_parser_omp_var_list_no_open (parser, kind, list);
20327   return list;
20328 }
20329
20330 /* OpenMP 3.0:
20331    collapse ( constant-expression ) */
20332
20333 static tree
20334 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20335 {
20336   tree c, num;
20337   location_t loc;
20338   HOST_WIDE_INT n;
20339
20340   loc = cp_lexer_peek_token (parser->lexer)->location;
20341   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20342     return list;
20343
20344   num = cp_parser_constant_expression (parser, false, NULL);
20345
20346   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20347     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20348                                            /*or_comma=*/false,
20349                                            /*consume_paren=*/true);
20350
20351   if (num == error_mark_node)
20352     return list;
20353   num = fold_non_dependent_expr (num);
20354   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20355       || !host_integerp (num, 0)
20356       || (n = tree_low_cst (num, 0)) <= 0
20357       || (int) n != n)
20358     {
20359       error ("%Hcollapse argument needs positive constant integer expression",
20360              &loc);
20361       return list;
20362     }
20363
20364   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20365   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20366   OMP_CLAUSE_CHAIN (c) = list;
20367   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20368
20369   return c;
20370 }
20371
20372 /* OpenMP 2.5:
20373    default ( shared | none ) */
20374
20375 static tree
20376 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20377 {
20378   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20379   tree c;
20380
20381   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20382     return list;
20383   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20384     {
20385       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20386       const char *p = IDENTIFIER_POINTER (id);
20387
20388       switch (p[0])
20389         {
20390         case 'n':
20391           if (strcmp ("none", p) != 0)
20392             goto invalid_kind;
20393           kind = OMP_CLAUSE_DEFAULT_NONE;
20394           break;
20395
20396         case 's':
20397           if (strcmp ("shared", p) != 0)
20398             goto invalid_kind;
20399           kind = OMP_CLAUSE_DEFAULT_SHARED;
20400           break;
20401
20402         default:
20403           goto invalid_kind;
20404         }
20405
20406       cp_lexer_consume_token (parser->lexer);
20407     }
20408   else
20409     {
20410     invalid_kind:
20411       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20412     }
20413
20414   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20415     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20416                                            /*or_comma=*/false,
20417                                            /*consume_paren=*/true);
20418
20419   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20420     return list;
20421
20422   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20423   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20424   OMP_CLAUSE_CHAIN (c) = list;
20425   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20426
20427   return c;
20428 }
20429
20430 /* OpenMP 2.5:
20431    if ( expression ) */
20432
20433 static tree
20434 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20435 {
20436   tree t, c;
20437
20438   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20439     return list;
20440
20441   t = cp_parser_condition (parser);
20442
20443   if (t == error_mark_node
20444       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20445     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20446                                            /*or_comma=*/false,
20447                                            /*consume_paren=*/true);
20448
20449   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20450
20451   c = build_omp_clause (OMP_CLAUSE_IF);
20452   OMP_CLAUSE_IF_EXPR (c) = t;
20453   OMP_CLAUSE_CHAIN (c) = list;
20454
20455   return c;
20456 }
20457
20458 /* OpenMP 2.5:
20459    nowait */
20460
20461 static tree
20462 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20463                              tree list, location_t location)
20464 {
20465   tree c;
20466
20467   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20468
20469   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20470   OMP_CLAUSE_CHAIN (c) = list;
20471   return c;
20472 }
20473
20474 /* OpenMP 2.5:
20475    num_threads ( expression ) */
20476
20477 static tree
20478 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20479                                   location_t location)
20480 {
20481   tree t, c;
20482
20483   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20484     return list;
20485
20486   t = cp_parser_expression (parser, false, NULL);
20487
20488   if (t == error_mark_node
20489       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20490     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20491                                            /*or_comma=*/false,
20492                                            /*consume_paren=*/true);
20493
20494   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20495                              "num_threads", location);
20496
20497   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20498   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20499   OMP_CLAUSE_CHAIN (c) = list;
20500
20501   return c;
20502 }
20503
20504 /* OpenMP 2.5:
20505    ordered */
20506
20507 static tree
20508 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20509                               tree list, location_t location)
20510 {
20511   tree c;
20512
20513   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20514                              "ordered", location);
20515
20516   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20517   OMP_CLAUSE_CHAIN (c) = list;
20518   return c;
20519 }
20520
20521 /* OpenMP 2.5:
20522    reduction ( reduction-operator : variable-list )
20523
20524    reduction-operator:
20525      One of: + * - & ^ | && || */
20526
20527 static tree
20528 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20529 {
20530   enum tree_code code;
20531   tree nlist, c;
20532
20533   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20534     return list;
20535
20536   switch (cp_lexer_peek_token (parser->lexer)->type)
20537     {
20538     case CPP_PLUS:
20539       code = PLUS_EXPR;
20540       break;
20541     case CPP_MULT:
20542       code = MULT_EXPR;
20543       break;
20544     case CPP_MINUS:
20545       code = MINUS_EXPR;
20546       break;
20547     case CPP_AND:
20548       code = BIT_AND_EXPR;
20549       break;
20550     case CPP_XOR:
20551       code = BIT_XOR_EXPR;
20552       break;
20553     case CPP_OR:
20554       code = BIT_IOR_EXPR;
20555       break;
20556     case CPP_AND_AND:
20557       code = TRUTH_ANDIF_EXPR;
20558       break;
20559     case CPP_OR_OR:
20560       code = TRUTH_ORIF_EXPR;
20561       break;
20562     default:
20563       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20564                                "%<|%>, %<&&%>, or %<||%>");
20565     resync_fail:
20566       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20567                                              /*or_comma=*/false,
20568                                              /*consume_paren=*/true);
20569       return list;
20570     }
20571   cp_lexer_consume_token (parser->lexer);
20572
20573   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20574     goto resync_fail;
20575
20576   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20577   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20578     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20579
20580   return nlist;
20581 }
20582
20583 /* OpenMP 2.5:
20584    schedule ( schedule-kind )
20585    schedule ( schedule-kind , expression )
20586
20587    schedule-kind:
20588      static | dynamic | guided | runtime | auto  */
20589
20590 static tree
20591 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20592 {
20593   tree c, t;
20594
20595   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20596     return list;
20597
20598   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20599
20600   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20601     {
20602       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20603       const char *p = IDENTIFIER_POINTER (id);
20604
20605       switch (p[0])
20606         {
20607         case 'd':
20608           if (strcmp ("dynamic", p) != 0)
20609             goto invalid_kind;
20610           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20611           break;
20612
20613         case 'g':
20614           if (strcmp ("guided", p) != 0)
20615             goto invalid_kind;
20616           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20617           break;
20618
20619         case 'r':
20620           if (strcmp ("runtime", p) != 0)
20621             goto invalid_kind;
20622           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20623           break;
20624
20625         default:
20626           goto invalid_kind;
20627         }
20628     }
20629   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20630     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20631   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20632     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20633   else
20634     goto invalid_kind;
20635   cp_lexer_consume_token (parser->lexer);
20636
20637   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20638     {
20639       cp_token *token;
20640       cp_lexer_consume_token (parser->lexer);
20641
20642       token = cp_lexer_peek_token (parser->lexer);
20643       t = cp_parser_assignment_expression (parser, false, NULL);
20644
20645       if (t == error_mark_node)
20646         goto resync_fail;
20647       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20648         error ("%Hschedule %<runtime%> does not take "
20649                "a %<chunk_size%> parameter", &token->location);
20650       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20651         error ("%Hschedule %<auto%> does not take "
20652                "a %<chunk_size%> parameter", &token->location);
20653       else
20654         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20655
20656       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20657         goto resync_fail;
20658     }
20659   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20660     goto resync_fail;
20661
20662   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20663   OMP_CLAUSE_CHAIN (c) = list;
20664   return c;
20665
20666  invalid_kind:
20667   cp_parser_error (parser, "invalid schedule kind");
20668  resync_fail:
20669   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20670                                          /*or_comma=*/false,
20671                                          /*consume_paren=*/true);
20672   return list;
20673 }
20674
20675 /* OpenMP 3.0:
20676    untied */
20677
20678 static tree
20679 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20680                              tree list, location_t location)
20681 {
20682   tree c;
20683
20684   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20685
20686   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20687   OMP_CLAUSE_CHAIN (c) = list;
20688   return c;
20689 }
20690
20691 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20692    is a bitmask in MASK.  Return the list of clauses found; the result
20693    of clause default goes in *pdefault.  */
20694
20695 static tree
20696 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20697                            const char *where, cp_token *pragma_tok)
20698 {
20699   tree clauses = NULL;
20700   bool first = true;
20701   cp_token *token = NULL;
20702
20703   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20704     {
20705       pragma_omp_clause c_kind;
20706       const char *c_name;
20707       tree prev = clauses;
20708
20709       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20710         cp_lexer_consume_token (parser->lexer);
20711
20712       token = cp_lexer_peek_token (parser->lexer);
20713       c_kind = cp_parser_omp_clause_name (parser);
20714       first = false;
20715
20716       switch (c_kind)
20717         {
20718         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20719           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20720                                                    token->location);
20721           c_name = "collapse";
20722           break;
20723         case PRAGMA_OMP_CLAUSE_COPYIN:
20724           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20725           c_name = "copyin";
20726           break;
20727         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20728           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20729                                             clauses);
20730           c_name = "copyprivate";
20731           break;
20732         case PRAGMA_OMP_CLAUSE_DEFAULT:
20733           clauses = cp_parser_omp_clause_default (parser, clauses,
20734                                                   token->location);
20735           c_name = "default";
20736           break;
20737         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20738           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20739                                             clauses);
20740           c_name = "firstprivate";
20741           break;
20742         case PRAGMA_OMP_CLAUSE_IF:
20743           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20744           c_name = "if";
20745           break;
20746         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20747           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20748                                             clauses);
20749           c_name = "lastprivate";
20750           break;
20751         case PRAGMA_OMP_CLAUSE_NOWAIT:
20752           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20753           c_name = "nowait";
20754           break;
20755         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20756           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20757                                                       token->location);
20758           c_name = "num_threads";
20759           break;
20760         case PRAGMA_OMP_CLAUSE_ORDERED:
20761           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20762                                                   token->location);
20763           c_name = "ordered";
20764           break;
20765         case PRAGMA_OMP_CLAUSE_PRIVATE:
20766           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20767                                             clauses);
20768           c_name = "private";
20769           break;
20770         case PRAGMA_OMP_CLAUSE_REDUCTION:
20771           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20772           c_name = "reduction";
20773           break;
20774         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20775           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20776                                                    token->location);
20777           c_name = "schedule";
20778           break;
20779         case PRAGMA_OMP_CLAUSE_SHARED:
20780           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20781                                             clauses);
20782           c_name = "shared";
20783           break;
20784         case PRAGMA_OMP_CLAUSE_UNTIED:
20785           clauses = cp_parser_omp_clause_untied (parser, clauses,
20786                                                  token->location);
20787           c_name = "nowait";
20788           break;
20789         default:
20790           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20791           goto saw_error;
20792         }
20793
20794       if (((mask >> c_kind) & 1) == 0)
20795         {
20796           /* Remove the invalid clause(s) from the list to avoid
20797              confusing the rest of the compiler.  */
20798           clauses = prev;
20799           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20800         }
20801     }
20802  saw_error:
20803   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20804   return finish_omp_clauses (clauses);
20805 }
20806
20807 /* OpenMP 2.5:
20808    structured-block:
20809      statement
20810
20811    In practice, we're also interested in adding the statement to an
20812    outer node.  So it is convenient if we work around the fact that
20813    cp_parser_statement calls add_stmt.  */
20814
20815 static unsigned
20816 cp_parser_begin_omp_structured_block (cp_parser *parser)
20817 {
20818   unsigned save = parser->in_statement;
20819
20820   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20821      This preserves the "not within loop or switch" style error messages
20822      for nonsense cases like
20823         void foo() {
20824         #pragma omp single
20825           break;
20826         }
20827   */
20828   if (parser->in_statement)
20829     parser->in_statement = IN_OMP_BLOCK;
20830
20831   return save;
20832 }
20833
20834 static void
20835 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20836 {
20837   parser->in_statement = save;
20838 }
20839
20840 static tree
20841 cp_parser_omp_structured_block (cp_parser *parser)
20842 {
20843   tree stmt = begin_omp_structured_block ();
20844   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20845
20846   cp_parser_statement (parser, NULL_TREE, false, NULL);
20847
20848   cp_parser_end_omp_structured_block (parser, save);
20849   return finish_omp_structured_block (stmt);
20850 }
20851
20852 /* OpenMP 2.5:
20853    # pragma omp atomic new-line
20854      expression-stmt
20855
20856    expression-stmt:
20857      x binop= expr | x++ | ++x | x-- | --x
20858    binop:
20859      +, *, -, /, &, ^, |, <<, >>
20860
20861   where x is an lvalue expression with scalar type.  */
20862
20863 static void
20864 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20865 {
20866   tree lhs, rhs;
20867   enum tree_code code;
20868
20869   cp_parser_require_pragma_eol (parser, pragma_tok);
20870
20871   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20872                                     /*cast_p=*/false, NULL);
20873   switch (TREE_CODE (lhs))
20874     {
20875     case ERROR_MARK:
20876       goto saw_error;
20877
20878     case PREINCREMENT_EXPR:
20879     case POSTINCREMENT_EXPR:
20880       lhs = TREE_OPERAND (lhs, 0);
20881       code = PLUS_EXPR;
20882       rhs = integer_one_node;
20883       break;
20884
20885     case PREDECREMENT_EXPR:
20886     case POSTDECREMENT_EXPR:
20887       lhs = TREE_OPERAND (lhs, 0);
20888       code = MINUS_EXPR;
20889       rhs = integer_one_node;
20890       break;
20891
20892     default:
20893       switch (cp_lexer_peek_token (parser->lexer)->type)
20894         {
20895         case CPP_MULT_EQ:
20896           code = MULT_EXPR;
20897           break;
20898         case CPP_DIV_EQ:
20899           code = TRUNC_DIV_EXPR;
20900           break;
20901         case CPP_PLUS_EQ:
20902           code = PLUS_EXPR;
20903           break;
20904         case CPP_MINUS_EQ:
20905           code = MINUS_EXPR;
20906           break;
20907         case CPP_LSHIFT_EQ:
20908           code = LSHIFT_EXPR;
20909           break;
20910         case CPP_RSHIFT_EQ:
20911           code = RSHIFT_EXPR;
20912           break;
20913         case CPP_AND_EQ:
20914           code = BIT_AND_EXPR;
20915           break;
20916         case CPP_OR_EQ:
20917           code = BIT_IOR_EXPR;
20918           break;
20919         case CPP_XOR_EQ:
20920           code = BIT_XOR_EXPR;
20921           break;
20922         default:
20923           cp_parser_error (parser,
20924                            "invalid operator for %<#pragma omp atomic%>");
20925           goto saw_error;
20926         }
20927       cp_lexer_consume_token (parser->lexer);
20928
20929       rhs = cp_parser_expression (parser, false, NULL);
20930       if (rhs == error_mark_node)
20931         goto saw_error;
20932       break;
20933     }
20934   finish_omp_atomic (code, lhs, rhs);
20935   cp_parser_consume_semicolon_at_end_of_statement (parser);
20936   return;
20937
20938  saw_error:
20939   cp_parser_skip_to_end_of_block_or_statement (parser);
20940 }
20941
20942
20943 /* OpenMP 2.5:
20944    # pragma omp barrier new-line  */
20945
20946 static void
20947 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20948 {
20949   cp_parser_require_pragma_eol (parser, pragma_tok);
20950   finish_omp_barrier ();
20951 }
20952
20953 /* OpenMP 2.5:
20954    # pragma omp critical [(name)] new-line
20955      structured-block  */
20956
20957 static tree
20958 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20959 {
20960   tree stmt, name = NULL;
20961
20962   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20963     {
20964       cp_lexer_consume_token (parser->lexer);
20965
20966       name = cp_parser_identifier (parser);
20967
20968       if (name == error_mark_node
20969           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20970         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20971                                                /*or_comma=*/false,
20972                                                /*consume_paren=*/true);
20973       if (name == error_mark_node)
20974         name = NULL;
20975     }
20976   cp_parser_require_pragma_eol (parser, pragma_tok);
20977
20978   stmt = cp_parser_omp_structured_block (parser);
20979   return c_finish_omp_critical (stmt, name);
20980 }
20981
20982 /* OpenMP 2.5:
20983    # pragma omp flush flush-vars[opt] new-line
20984
20985    flush-vars:
20986      ( variable-list ) */
20987
20988 static void
20989 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20990 {
20991   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20992     (void) cp_parser_omp_var_list (parser, 0, NULL);
20993   cp_parser_require_pragma_eol (parser, pragma_tok);
20994
20995   finish_omp_flush ();
20996 }
20997
20998 /* Helper function, to parse omp for increment expression.  */
20999
21000 static tree
21001 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21002 {
21003   tree lhs = cp_parser_cast_expression (parser, false, false, NULL), rhs;
21004   enum tree_code op;
21005   cp_token *token;
21006
21007   if (lhs != decl)
21008     {
21009       cp_parser_skip_to_end_of_statement (parser);
21010       return error_mark_node;
21011     }
21012
21013   token = cp_lexer_peek_token (parser->lexer);
21014   op = binops_by_token [token->type].tree_type;
21015   switch (op)
21016     {
21017     case LT_EXPR:
21018     case LE_EXPR:
21019     case GT_EXPR:
21020     case GE_EXPR:
21021       break;
21022     default:
21023       cp_parser_skip_to_end_of_statement (parser);
21024       return error_mark_node;
21025     }
21026
21027   cp_lexer_consume_token (parser->lexer);
21028   rhs = cp_parser_binary_expression (parser, false,
21029                                      PREC_RELATIONAL_EXPRESSION, NULL);
21030   if (rhs == error_mark_node
21031       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21032     {
21033       cp_parser_skip_to_end_of_statement (parser);
21034       return error_mark_node;
21035     }
21036
21037   return build2 (op, boolean_type_node, lhs, rhs);
21038 }
21039
21040 /* Helper function, to parse omp for increment expression.  */
21041
21042 static tree
21043 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21044 {
21045   cp_token *token = cp_lexer_peek_token (parser->lexer);
21046   enum tree_code op;
21047   tree lhs, rhs;
21048   cp_id_kind idk;
21049   bool decl_first;
21050
21051   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21052     {
21053       op = (token->type == CPP_PLUS_PLUS
21054             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21055       cp_lexer_consume_token (parser->lexer);
21056       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21057       if (lhs != decl)
21058         return error_mark_node;
21059       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21060     }
21061
21062   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21063   if (lhs != decl)
21064     return error_mark_node;
21065
21066   token = cp_lexer_peek_token (parser->lexer);
21067   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21068     {
21069       op = (token->type == CPP_PLUS_PLUS
21070             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21071       cp_lexer_consume_token (parser->lexer);
21072       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21073     }
21074
21075   op = cp_parser_assignment_operator_opt (parser);
21076   if (op == ERROR_MARK)
21077     return error_mark_node;
21078
21079   if (op != NOP_EXPR)
21080     {
21081       rhs = cp_parser_assignment_expression (parser, false, NULL);
21082       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21083       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21084     }
21085
21086   lhs = cp_parser_binary_expression (parser, false,
21087                                      PREC_ADDITIVE_EXPRESSION, NULL);
21088   token = cp_lexer_peek_token (parser->lexer);
21089   decl_first = lhs == decl;
21090   if (decl_first)
21091     lhs = NULL_TREE;
21092   if (token->type != CPP_PLUS
21093       && token->type != CPP_MINUS)
21094     return error_mark_node;
21095
21096   do
21097     {
21098       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21099       cp_lexer_consume_token (parser->lexer);
21100       rhs = cp_parser_binary_expression (parser, false,
21101                                          PREC_ADDITIVE_EXPRESSION, NULL);
21102       token = cp_lexer_peek_token (parser->lexer);
21103       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21104         {
21105           if (lhs == NULL_TREE)
21106             {
21107               if (op == PLUS_EXPR)
21108                 lhs = rhs;
21109               else
21110                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21111             }
21112           else
21113             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21114                                      NULL, tf_warning_or_error);
21115         }
21116     }
21117   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21118
21119   if (!decl_first)
21120     {
21121       if (rhs != decl || op == MINUS_EXPR)
21122         return error_mark_node;
21123       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21124     }
21125   else
21126     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21127
21128   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21129 }
21130
21131 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21132
21133 static tree
21134 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21135 {
21136   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21137   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21138   tree this_pre_body, cl;
21139   location_t loc_first;
21140   bool collapse_err = false;
21141   int i, collapse = 1, nbraces = 0;
21142
21143   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21144     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21145       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21146
21147   gcc_assert (collapse >= 1);
21148
21149   declv = make_tree_vec (collapse);
21150   initv = make_tree_vec (collapse);
21151   condv = make_tree_vec (collapse);
21152   incrv = make_tree_vec (collapse);
21153
21154   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21155
21156   for (i = 0; i < collapse; i++)
21157     {
21158       int bracecount = 0;
21159       bool add_private_clause = false;
21160       location_t loc;
21161
21162       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21163         {
21164           cp_parser_error (parser, "for statement expected");
21165           return NULL;
21166         }
21167       loc = cp_lexer_consume_token (parser->lexer)->location;
21168
21169       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21170         return NULL;
21171
21172       init = decl = real_decl = NULL;
21173       this_pre_body = push_stmt_list ();
21174       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21175         {
21176           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21177
21178              init-expr:
21179                        var = lb
21180                        integer-type var = lb
21181                        random-access-iterator-type var = lb
21182                        pointer-type var = lb
21183           */
21184           cp_decl_specifier_seq type_specifiers;
21185
21186           /* First, try to parse as an initialized declaration.  See
21187              cp_parser_condition, from whence the bulk of this is copied.  */
21188
21189           cp_parser_parse_tentatively (parser);
21190           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21191                                         &type_specifiers);
21192           if (cp_parser_parse_definitely (parser))
21193             {
21194               /* If parsing a type specifier seq succeeded, then this
21195                  MUST be a initialized declaration.  */
21196               tree asm_specification, attributes;
21197               cp_declarator *declarator;
21198
21199               declarator = cp_parser_declarator (parser,
21200                                                  CP_PARSER_DECLARATOR_NAMED,
21201                                                  /*ctor_dtor_or_conv_p=*/NULL,
21202                                                  /*parenthesized_p=*/NULL,
21203                                                  /*member_p=*/false);
21204               attributes = cp_parser_attributes_opt (parser);
21205               asm_specification = cp_parser_asm_specification_opt (parser);
21206
21207               if (declarator == cp_error_declarator) 
21208                 cp_parser_skip_to_end_of_statement (parser);
21209
21210               else 
21211                 {
21212                   tree pushed_scope, auto_node;
21213
21214                   decl = start_decl (declarator, &type_specifiers,
21215                                      SD_INITIALIZED, attributes,
21216                                      /*prefix_attributes=*/NULL_TREE,
21217                                      &pushed_scope);
21218
21219                   auto_node = type_uses_auto (TREE_TYPE (decl));
21220                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21221                     {
21222                       if (cp_lexer_next_token_is (parser->lexer, 
21223                                                   CPP_OPEN_PAREN))
21224                         error ("parenthesized initialization is not allowed in "
21225                                "OpenMP %<for%> loop");
21226                       else
21227                         /* Trigger an error.  */
21228                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21229
21230                       init = error_mark_node;
21231                       cp_parser_skip_to_end_of_statement (parser);
21232                     }
21233                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21234                            || type_dependent_expression_p (decl)
21235                            || auto_node)
21236                     {
21237                       bool is_direct_init, is_non_constant_init;
21238
21239                       init = cp_parser_initializer (parser,
21240                                                     &is_direct_init,
21241                                                     &is_non_constant_init);
21242
21243                       if (auto_node && describable_type (init))
21244                         {
21245                           TREE_TYPE (decl)
21246                             = do_auto_deduction (TREE_TYPE (decl), init,
21247                                                  auto_node);
21248
21249                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21250                               && !type_dependent_expression_p (decl))
21251                             goto non_class;
21252                         }
21253                       
21254                       cp_finish_decl (decl, init, !is_non_constant_init,
21255                                       asm_specification,
21256                                       LOOKUP_ONLYCONVERTING);
21257                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21258                         {
21259                           for_block
21260                             = tree_cons (NULL, this_pre_body, for_block);
21261                           init = NULL_TREE;
21262                         }
21263                       else
21264                         init = pop_stmt_list (this_pre_body);
21265                       this_pre_body = NULL_TREE;
21266                     }
21267                   else
21268                     {
21269                       /* Consume '='.  */
21270                       cp_lexer_consume_token (parser->lexer);
21271                       init = cp_parser_assignment_expression (parser, false, NULL);
21272
21273                     non_class:
21274                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21275                         init = error_mark_node;
21276                       else
21277                         cp_finish_decl (decl, NULL_TREE,
21278                                         /*init_const_expr_p=*/false,
21279                                         asm_specification,
21280                                         LOOKUP_ONLYCONVERTING);
21281                     }
21282
21283                   if (pushed_scope)
21284                     pop_scope (pushed_scope);
21285                 }
21286             }
21287           else 
21288             {
21289               cp_id_kind idk;
21290               /* If parsing a type specifier sequence failed, then
21291                  this MUST be a simple expression.  */
21292               cp_parser_parse_tentatively (parser);
21293               decl = cp_parser_primary_expression (parser, false, false,
21294                                                    false, &idk);
21295               if (!cp_parser_error_occurred (parser)
21296                   && decl
21297                   && DECL_P (decl)
21298                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21299                 {
21300                   tree rhs;
21301
21302                   cp_parser_parse_definitely (parser);
21303                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21304                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21305                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21306                                                          rhs,
21307                                                          tf_warning_or_error));
21308                   add_private_clause = true;
21309                 }
21310               else
21311                 {
21312                   decl = NULL;
21313                   cp_parser_abort_tentative_parse (parser);
21314                   init = cp_parser_expression (parser, false, NULL);
21315                   if (init)
21316                     {
21317                       if (TREE_CODE (init) == MODIFY_EXPR
21318                           || TREE_CODE (init) == MODOP_EXPR)
21319                         real_decl = TREE_OPERAND (init, 0);
21320                     }
21321                 }
21322             }
21323         }
21324       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21325       if (this_pre_body)
21326         {
21327           this_pre_body = pop_stmt_list (this_pre_body);
21328           if (pre_body)
21329             {
21330               tree t = pre_body;
21331               pre_body = push_stmt_list ();
21332               add_stmt (t);
21333               add_stmt (this_pre_body);
21334               pre_body = pop_stmt_list (pre_body);
21335             }
21336           else
21337             pre_body = this_pre_body;
21338         }
21339
21340       if (decl)
21341         real_decl = decl;
21342       if (par_clauses != NULL && real_decl != NULL_TREE)
21343         {
21344           tree *c;
21345           for (c = par_clauses; *c ; )
21346             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21347                 && OMP_CLAUSE_DECL (*c) == real_decl)
21348               {
21349                 error ("%Hiteration variable %qD should not be firstprivate",
21350                        &loc, real_decl);
21351                 *c = OMP_CLAUSE_CHAIN (*c);
21352               }
21353             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21354                      && OMP_CLAUSE_DECL (*c) == real_decl)
21355               {
21356                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21357                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21358                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21359                 OMP_CLAUSE_DECL (l) = real_decl;
21360                 OMP_CLAUSE_CHAIN (l) = clauses;
21361                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21362                 clauses = l;
21363                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21364                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21365                 add_private_clause = false;
21366               }
21367             else
21368               {
21369                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21370                     && OMP_CLAUSE_DECL (*c) == real_decl)
21371                   add_private_clause = false;
21372                 c = &OMP_CLAUSE_CHAIN (*c);
21373               }
21374         }
21375
21376       if (add_private_clause)
21377         {
21378           tree c;
21379           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21380             {
21381               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21382                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21383                   && OMP_CLAUSE_DECL (c) == decl)
21384                 break;
21385               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21386                        && OMP_CLAUSE_DECL (c) == decl)
21387                 error ("%Hiteration variable %qD should not be firstprivate",
21388                        &loc, decl);
21389               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21390                        && OMP_CLAUSE_DECL (c) == decl)
21391                 error ("%Hiteration variable %qD should not be reduction",
21392                        &loc, decl);
21393             }
21394           if (c == NULL)
21395             {
21396               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21397               OMP_CLAUSE_DECL (c) = decl;
21398               c = finish_omp_clauses (c);
21399               if (c)
21400                 {
21401                   OMP_CLAUSE_CHAIN (c) = clauses;
21402                   clauses = c;
21403                 }
21404             }
21405         }
21406
21407       cond = NULL;
21408       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21409         {
21410           /* If decl is an iterator, preserve LHS and RHS of the relational
21411              expr until finish_omp_for.  */
21412           if (decl
21413               && (type_dependent_expression_p (decl)
21414                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21415             cond = cp_parser_omp_for_cond (parser, decl);
21416           else
21417             cond = cp_parser_condition (parser);
21418         }
21419       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21420
21421       incr = NULL;
21422       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21423         {
21424           /* If decl is an iterator, preserve the operator on decl
21425              until finish_omp_for.  */
21426           if (decl
21427               && (type_dependent_expression_p (decl)
21428                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21429             incr = cp_parser_omp_for_incr (parser, decl);
21430           else
21431             incr = cp_parser_expression (parser, false, NULL);
21432         }
21433
21434       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21435         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21436                                                /*or_comma=*/false,
21437                                                /*consume_paren=*/true);
21438
21439       TREE_VEC_ELT (declv, i) = decl;
21440       TREE_VEC_ELT (initv, i) = init;
21441       TREE_VEC_ELT (condv, i) = cond;
21442       TREE_VEC_ELT (incrv, i) = incr;
21443
21444       if (i == collapse - 1)
21445         break;
21446
21447       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21448          in between the collapsed for loops to be still considered perfectly
21449          nested.  Hopefully the final version clarifies this.
21450          For now handle (multiple) {'s and empty statements.  */
21451       cp_parser_parse_tentatively (parser);
21452       do
21453         {
21454           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21455             break;
21456           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21457             {
21458               cp_lexer_consume_token (parser->lexer);
21459               bracecount++;
21460             }
21461           else if (bracecount
21462                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21463             cp_lexer_consume_token (parser->lexer);
21464           else
21465             {
21466               loc = cp_lexer_peek_token (parser->lexer)->location;
21467               error ("%Hnot enough collapsed for loops", &loc);
21468               collapse_err = true;
21469               cp_parser_abort_tentative_parse (parser);
21470               declv = NULL_TREE;
21471               break;
21472             }
21473         }
21474       while (1);
21475
21476       if (declv)
21477         {
21478           cp_parser_parse_definitely (parser);
21479           nbraces += bracecount;
21480         }
21481     }
21482
21483   /* Note that we saved the original contents of this flag when we entered
21484      the structured block, and so we don't need to re-save it here.  */
21485   parser->in_statement = IN_OMP_FOR;
21486
21487   /* Note that the grammar doesn't call for a structured block here,
21488      though the loop as a whole is a structured block.  */
21489   body = push_stmt_list ();
21490   cp_parser_statement (parser, NULL_TREE, false, NULL);
21491   body = pop_stmt_list (body);
21492
21493   if (declv == NULL_TREE)
21494     ret = NULL_TREE;
21495   else
21496     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21497                           pre_body, clauses);
21498
21499   while (nbraces)
21500     {
21501       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21502         {
21503           cp_lexer_consume_token (parser->lexer);
21504           nbraces--;
21505         }
21506       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21507         cp_lexer_consume_token (parser->lexer);
21508       else
21509         {
21510           if (!collapse_err)
21511             {
21512               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21513               error ("%Hcollapsed loops not perfectly nested", &loc);
21514             }
21515           collapse_err = true;
21516           cp_parser_statement_seq_opt (parser, NULL);
21517           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21518         }
21519     }
21520
21521   while (for_block)
21522     {
21523       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21524       for_block = TREE_CHAIN (for_block);
21525     }
21526
21527   return ret;
21528 }
21529
21530 /* OpenMP 2.5:
21531    #pragma omp for for-clause[optseq] new-line
21532      for-loop  */
21533
21534 #define OMP_FOR_CLAUSE_MASK                             \
21535         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21536         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21537         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21538         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21539         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21540         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21541         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21542         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21543
21544 static tree
21545 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21546 {
21547   tree clauses, sb, ret;
21548   unsigned int save;
21549
21550   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21551                                        "#pragma omp for", pragma_tok);
21552
21553   sb = begin_omp_structured_block ();
21554   save = cp_parser_begin_omp_structured_block (parser);
21555
21556   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21557
21558   cp_parser_end_omp_structured_block (parser, save);
21559   add_stmt (finish_omp_structured_block (sb));
21560
21561   return ret;
21562 }
21563
21564 /* OpenMP 2.5:
21565    # pragma omp master new-line
21566      structured-block  */
21567
21568 static tree
21569 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21570 {
21571   cp_parser_require_pragma_eol (parser, pragma_tok);
21572   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21573 }
21574
21575 /* OpenMP 2.5:
21576    # pragma omp ordered new-line
21577      structured-block  */
21578
21579 static tree
21580 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21581 {
21582   cp_parser_require_pragma_eol (parser, pragma_tok);
21583   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21584 }
21585
21586 /* OpenMP 2.5:
21587
21588    section-scope:
21589      { section-sequence }
21590
21591    section-sequence:
21592      section-directive[opt] structured-block
21593      section-sequence section-directive structured-block  */
21594
21595 static tree
21596 cp_parser_omp_sections_scope (cp_parser *parser)
21597 {
21598   tree stmt, substmt;
21599   bool error_suppress = false;
21600   cp_token *tok;
21601
21602   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21603     return NULL_TREE;
21604
21605   stmt = push_stmt_list ();
21606
21607   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21608     {
21609       unsigned save;
21610
21611       substmt = begin_omp_structured_block ();
21612       save = cp_parser_begin_omp_structured_block (parser);
21613
21614       while (1)
21615         {
21616           cp_parser_statement (parser, NULL_TREE, false, NULL);
21617
21618           tok = cp_lexer_peek_token (parser->lexer);
21619           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21620             break;
21621           if (tok->type == CPP_CLOSE_BRACE)
21622             break;
21623           if (tok->type == CPP_EOF)
21624             break;
21625         }
21626
21627       cp_parser_end_omp_structured_block (parser, save);
21628       substmt = finish_omp_structured_block (substmt);
21629       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21630       add_stmt (substmt);
21631     }
21632
21633   while (1)
21634     {
21635       tok = cp_lexer_peek_token (parser->lexer);
21636       if (tok->type == CPP_CLOSE_BRACE)
21637         break;
21638       if (tok->type == CPP_EOF)
21639         break;
21640
21641       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21642         {
21643           cp_lexer_consume_token (parser->lexer);
21644           cp_parser_require_pragma_eol (parser, tok);
21645           error_suppress = false;
21646         }
21647       else if (!error_suppress)
21648         {
21649           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21650           error_suppress = true;
21651         }
21652
21653       substmt = cp_parser_omp_structured_block (parser);
21654       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21655       add_stmt (substmt);
21656     }
21657   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21658
21659   substmt = pop_stmt_list (stmt);
21660
21661   stmt = make_node (OMP_SECTIONS);
21662   TREE_TYPE (stmt) = void_type_node;
21663   OMP_SECTIONS_BODY (stmt) = substmt;
21664
21665   add_stmt (stmt);
21666   return stmt;
21667 }
21668
21669 /* OpenMP 2.5:
21670    # pragma omp sections sections-clause[optseq] newline
21671      sections-scope  */
21672
21673 #define OMP_SECTIONS_CLAUSE_MASK                        \
21674         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21675         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21676         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21677         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21678         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21679
21680 static tree
21681 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21682 {
21683   tree clauses, ret;
21684
21685   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21686                                        "#pragma omp sections", pragma_tok);
21687
21688   ret = cp_parser_omp_sections_scope (parser);
21689   if (ret)
21690     OMP_SECTIONS_CLAUSES (ret) = clauses;
21691
21692   return ret;
21693 }
21694
21695 /* OpenMP 2.5:
21696    # pragma parallel parallel-clause new-line
21697    # pragma parallel for parallel-for-clause new-line
21698    # pragma parallel sections parallel-sections-clause new-line  */
21699
21700 #define OMP_PARALLEL_CLAUSE_MASK                        \
21701         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21702         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21703         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21704         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21705         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21706         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21707         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21708         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21709
21710 static tree
21711 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21712 {
21713   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21714   const char *p_name = "#pragma omp parallel";
21715   tree stmt, clauses, par_clause, ws_clause, block;
21716   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21717   unsigned int save;
21718
21719   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21720     {
21721       cp_lexer_consume_token (parser->lexer);
21722       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21723       p_name = "#pragma omp parallel for";
21724       mask |= OMP_FOR_CLAUSE_MASK;
21725       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21726     }
21727   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21728     {
21729       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21730       const char *p = IDENTIFIER_POINTER (id);
21731       if (strcmp (p, "sections") == 0)
21732         {
21733           cp_lexer_consume_token (parser->lexer);
21734           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21735           p_name = "#pragma omp parallel sections";
21736           mask |= OMP_SECTIONS_CLAUSE_MASK;
21737           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21738         }
21739     }
21740
21741   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21742   block = begin_omp_parallel ();
21743   save = cp_parser_begin_omp_structured_block (parser);
21744
21745   switch (p_kind)
21746     {
21747     case PRAGMA_OMP_PARALLEL:
21748       cp_parser_statement (parser, NULL_TREE, false, NULL);
21749       par_clause = clauses;
21750       break;
21751
21752     case PRAGMA_OMP_PARALLEL_FOR:
21753       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21754       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21755       break;
21756
21757     case PRAGMA_OMP_PARALLEL_SECTIONS:
21758       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21759       stmt = cp_parser_omp_sections_scope (parser);
21760       if (stmt)
21761         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21762       break;
21763
21764     default:
21765       gcc_unreachable ();
21766     }
21767
21768   cp_parser_end_omp_structured_block (parser, save);
21769   stmt = finish_omp_parallel (par_clause, block);
21770   if (p_kind != PRAGMA_OMP_PARALLEL)
21771     OMP_PARALLEL_COMBINED (stmt) = 1;
21772   return stmt;
21773 }
21774
21775 /* OpenMP 2.5:
21776    # pragma omp single single-clause[optseq] new-line
21777      structured-block  */
21778
21779 #define OMP_SINGLE_CLAUSE_MASK                          \
21780         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21781         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21782         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21783         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21784
21785 static tree
21786 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21787 {
21788   tree stmt = make_node (OMP_SINGLE);
21789   TREE_TYPE (stmt) = void_type_node;
21790
21791   OMP_SINGLE_CLAUSES (stmt)
21792     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21793                                  "#pragma omp single", pragma_tok);
21794   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21795
21796   return add_stmt (stmt);
21797 }
21798
21799 /* OpenMP 3.0:
21800    # pragma omp task task-clause[optseq] new-line
21801      structured-block  */
21802
21803 #define OMP_TASK_CLAUSE_MASK                            \
21804         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21805         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21806         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21807         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21808         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21809         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21810
21811 static tree
21812 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21813 {
21814   tree clauses, block;
21815   unsigned int save;
21816
21817   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21818                                        "#pragma omp task", pragma_tok);
21819   block = begin_omp_task ();
21820   save = cp_parser_begin_omp_structured_block (parser);
21821   cp_parser_statement (parser, NULL_TREE, false, NULL);
21822   cp_parser_end_omp_structured_block (parser, save);
21823   return finish_omp_task (clauses, block);
21824 }
21825
21826 /* OpenMP 3.0:
21827    # pragma omp taskwait new-line  */
21828
21829 static void
21830 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21831 {
21832   cp_parser_require_pragma_eol (parser, pragma_tok);
21833   finish_omp_taskwait ();
21834 }
21835
21836 /* OpenMP 2.5:
21837    # pragma omp threadprivate (variable-list) */
21838
21839 static void
21840 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21841 {
21842   tree vars;
21843
21844   vars = cp_parser_omp_var_list (parser, 0, NULL);
21845   cp_parser_require_pragma_eol (parser, pragma_tok);
21846
21847   finish_omp_threadprivate (vars);
21848 }
21849
21850 /* Main entry point to OpenMP statement pragmas.  */
21851
21852 static void
21853 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21854 {
21855   tree stmt;
21856
21857   switch (pragma_tok->pragma_kind)
21858     {
21859     case PRAGMA_OMP_ATOMIC:
21860       cp_parser_omp_atomic (parser, pragma_tok);
21861       return;
21862     case PRAGMA_OMP_CRITICAL:
21863       stmt = cp_parser_omp_critical (parser, pragma_tok);
21864       break;
21865     case PRAGMA_OMP_FOR:
21866       stmt = cp_parser_omp_for (parser, pragma_tok);
21867       break;
21868     case PRAGMA_OMP_MASTER:
21869       stmt = cp_parser_omp_master (parser, pragma_tok);
21870       break;
21871     case PRAGMA_OMP_ORDERED:
21872       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21873       break;
21874     case PRAGMA_OMP_PARALLEL:
21875       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21876       break;
21877     case PRAGMA_OMP_SECTIONS:
21878       stmt = cp_parser_omp_sections (parser, pragma_tok);
21879       break;
21880     case PRAGMA_OMP_SINGLE:
21881       stmt = cp_parser_omp_single (parser, pragma_tok);
21882       break;
21883     case PRAGMA_OMP_TASK:
21884       stmt = cp_parser_omp_task (parser, pragma_tok);
21885       break;
21886     default:
21887       gcc_unreachable ();
21888     }
21889
21890   if (stmt)
21891     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21892 }
21893 \f
21894 /* The parser.  */
21895
21896 static GTY (()) cp_parser *the_parser;
21897
21898 \f
21899 /* Special handling for the first token or line in the file.  The first
21900    thing in the file might be #pragma GCC pch_preprocess, which loads a
21901    PCH file, which is a GC collection point.  So we need to handle this
21902    first pragma without benefit of an existing lexer structure.
21903
21904    Always returns one token to the caller in *FIRST_TOKEN.  This is
21905    either the true first token of the file, or the first token after
21906    the initial pragma.  */
21907
21908 static void
21909 cp_parser_initial_pragma (cp_token *first_token)
21910 {
21911   tree name = NULL;
21912
21913   cp_lexer_get_preprocessor_token (NULL, first_token);
21914   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21915     return;
21916
21917   cp_lexer_get_preprocessor_token (NULL, first_token);
21918   if (first_token->type == CPP_STRING)
21919     {
21920       name = first_token->u.value;
21921
21922       cp_lexer_get_preprocessor_token (NULL, first_token);
21923       if (first_token->type != CPP_PRAGMA_EOL)
21924         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21925                &first_token->location);
21926     }
21927   else
21928     error ("%Hexpected string literal", &first_token->location);
21929
21930   /* Skip to the end of the pragma.  */
21931   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21932     cp_lexer_get_preprocessor_token (NULL, first_token);
21933
21934   /* Now actually load the PCH file.  */
21935   if (name)
21936     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21937
21938   /* Read one more token to return to our caller.  We have to do this
21939      after reading the PCH file in, since its pointers have to be
21940      live.  */
21941   cp_lexer_get_preprocessor_token (NULL, first_token);
21942 }
21943
21944 /* Normal parsing of a pragma token.  Here we can (and must) use the
21945    regular lexer.  */
21946
21947 static bool
21948 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21949 {
21950   cp_token *pragma_tok;
21951   unsigned int id;
21952
21953   pragma_tok = cp_lexer_consume_token (parser->lexer);
21954   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21955   parser->lexer->in_pragma = true;
21956
21957   id = pragma_tok->pragma_kind;
21958   switch (id)
21959     {
21960     case PRAGMA_GCC_PCH_PREPROCESS:
21961       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21962              &pragma_tok->location);
21963       break;
21964
21965     case PRAGMA_OMP_BARRIER:
21966       switch (context)
21967         {
21968         case pragma_compound:
21969           cp_parser_omp_barrier (parser, pragma_tok);
21970           return false;
21971         case pragma_stmt:
21972           error ("%H%<#pragma omp barrier%> may only be "
21973                  "used in compound statements", &pragma_tok->location);
21974           break;
21975         default:
21976           goto bad_stmt;
21977         }
21978       break;
21979
21980     case PRAGMA_OMP_FLUSH:
21981       switch (context)
21982         {
21983         case pragma_compound:
21984           cp_parser_omp_flush (parser, pragma_tok);
21985           return false;
21986         case pragma_stmt:
21987           error ("%H%<#pragma omp flush%> may only be "
21988                  "used in compound statements", &pragma_tok->location);
21989           break;
21990         default:
21991           goto bad_stmt;
21992         }
21993       break;
21994
21995     case PRAGMA_OMP_TASKWAIT:
21996       switch (context)
21997         {
21998         case pragma_compound:
21999           cp_parser_omp_taskwait (parser, pragma_tok);
22000           return false;
22001         case pragma_stmt:
22002           error ("%H%<#pragma omp taskwait%> may only be "
22003                  "used in compound statements",
22004                  &pragma_tok->location);
22005           break;
22006         default:
22007           goto bad_stmt;
22008         }
22009       break;
22010
22011     case PRAGMA_OMP_THREADPRIVATE:
22012       cp_parser_omp_threadprivate (parser, pragma_tok);
22013       return false;
22014
22015     case PRAGMA_OMP_ATOMIC:
22016     case PRAGMA_OMP_CRITICAL:
22017     case PRAGMA_OMP_FOR:
22018     case PRAGMA_OMP_MASTER:
22019     case PRAGMA_OMP_ORDERED:
22020     case PRAGMA_OMP_PARALLEL:
22021     case PRAGMA_OMP_SECTIONS:
22022     case PRAGMA_OMP_SINGLE:
22023     case PRAGMA_OMP_TASK:
22024       if (context == pragma_external)
22025         goto bad_stmt;
22026       cp_parser_omp_construct (parser, pragma_tok);
22027       return true;
22028
22029     case PRAGMA_OMP_SECTION:
22030       error ("%H%<#pragma omp section%> may only be used in "
22031              "%<#pragma omp sections%> construct", &pragma_tok->location);
22032       break;
22033
22034     default:
22035       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22036       c_invoke_pragma_handler (id);
22037       break;
22038
22039     bad_stmt:
22040       cp_parser_error (parser, "expected declaration specifiers");
22041       break;
22042     }
22043
22044   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22045   return false;
22046 }
22047
22048 /* The interface the pragma parsers have to the lexer.  */
22049
22050 enum cpp_ttype
22051 pragma_lex (tree *value)
22052 {
22053   cp_token *tok;
22054   enum cpp_ttype ret;
22055
22056   tok = cp_lexer_peek_token (the_parser->lexer);
22057
22058   ret = tok->type;
22059   *value = tok->u.value;
22060
22061   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22062     ret = CPP_EOF;
22063   else if (ret == CPP_STRING)
22064     *value = cp_parser_string_literal (the_parser, false, false);
22065   else
22066     {
22067       cp_lexer_consume_token (the_parser->lexer);
22068       if (ret == CPP_KEYWORD)
22069         ret = CPP_NAME;
22070     }
22071
22072   return ret;
22073 }
22074
22075 \f
22076 /* External interface.  */
22077
22078 /* Parse one entire translation unit.  */
22079
22080 void
22081 c_parse_file (void)
22082 {
22083   bool error_occurred;
22084   static bool already_called = false;
22085
22086   if (already_called)
22087     {
22088       sorry ("inter-module optimizations not implemented for C++");
22089       return;
22090     }
22091   already_called = true;
22092
22093   the_parser = cp_parser_new ();
22094   push_deferring_access_checks (flag_access_control
22095                                 ? dk_no_deferred : dk_no_check);
22096   error_occurred = cp_parser_translation_unit (the_parser);
22097   the_parser = NULL;
22098 }
22099
22100 #include "gt-cp-parser.h"