OSDN Git Service

* cvt.c (convert_to_void): Avoid C++ keywords.
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a system header.  */
75   BOOL_BITFIELD in_system_header : 1;
76   /* True if this token is from a context where it is implicitly extern "C" */
77   BOOL_BITFIELD implicit_extern_c : 1;
78   /* True for a CPP_NAME token that is not a keyword (i.e., for which
79      KEYWORD is RID_MAX) iff this name was looked up and found to be
80      ambiguous.  An error has already been reported.  */
81   BOOL_BITFIELD ambiguous_p : 1;
82   /* The value associated with this token, if any.  */
83   union cp_token_value {
84     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
85     struct tree_check* GTY((tag ("1"))) tree_check_value;
86     /* Use for all other tokens.  */
87     tree GTY((tag ("0"))) value;
88   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89   /* The location at which this token was found.  */
90   location_t location;
91 } cp_token;
92
93 /* We use a stack of token pointer for saving token sets.  */
94 typedef struct cp_token *cp_token_position;
95 DEF_VEC_P (cp_token_position);
96 DEF_VEC_ALLOC_P (cp_token_position,heap);
97
98 static cp_token eof_token =
99 {
100   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, false, 0, { NULL },
101   0
102 };
103
104 /* The cp_lexer structure represents the C++ lexer.  It is responsible
105    for managing the token stream from the preprocessor and supplying
106    it to the parser.  Tokens are never added to the cp_lexer after
107    it is created.  */
108
109 typedef struct cp_lexer GTY (())
110 {
111   /* The memory allocated for the buffer.  NULL if this lexer does not
112      own the token buffer.  */
113   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
114   /* If the lexer owns the buffer, this is the number of tokens in the
115      buffer.  */
116   size_t buffer_length;
117
118   /* A pointer just past the last available token.  The tokens
119      in this lexer are [buffer, last_token).  */
120   cp_token_position GTY ((skip)) last_token;
121
122   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
123      no more available tokens.  */
124   cp_token_position GTY ((skip)) next_token;
125
126   /* A stack indicating positions at which cp_lexer_save_tokens was
127      called.  The top entry is the most recent position at which we
128      began saving tokens.  If the stack is non-empty, we are saving
129      tokens.  */
130   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
131
132   /* The next lexer in a linked list of lexers.  */
133   struct cp_lexer *next;
134
135   /* True if we should output debugging information.  */
136   bool debugging_p;
137
138   /* True if we're in the context of parsing a pragma, and should not
139      increment past the end-of-line marker.  */
140   bool in_pragma;
141 } cp_lexer;
142
143 /* cp_token_cache is a range of tokens.  There is no need to represent
144    allocate heap memory for it, since tokens are never removed from the
145    lexer's array.  There is also no need for the GC to walk through
146    a cp_token_cache, since everything in here is referenced through
147    a lexer.  */
148
149 typedef struct cp_token_cache GTY(())
150 {
151   /* The beginning of the token range.  */
152   cp_token * GTY((skip)) first;
153
154   /* Points immediately after the last token in the range.  */
155   cp_token * GTY ((skip)) last;
156 } cp_token_cache;
157
158 /* Prototypes.  */
159
160 static cp_lexer *cp_lexer_new_main
161   (void);
162 static cp_lexer *cp_lexer_new_from_tokens
163   (cp_token_cache *tokens);
164 static void cp_lexer_destroy
165   (cp_lexer *);
166 static int cp_lexer_saving_tokens
167   (const cp_lexer *);
168 static cp_token_position cp_lexer_token_position
169   (cp_lexer *, bool);
170 static cp_token *cp_lexer_token_at
171   (cp_lexer *, cp_token_position);
172 static void cp_lexer_get_preprocessor_token
173   (cp_lexer *, cp_token *);
174 static inline cp_token *cp_lexer_peek_token
175   (cp_lexer *);
176 static cp_token *cp_lexer_peek_nth_token
177   (cp_lexer *, size_t);
178 static inline bool cp_lexer_next_token_is
179   (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_not
181   (cp_lexer *, enum cpp_ttype);
182 static bool cp_lexer_next_token_is_keyword
183   (cp_lexer *, enum rid);
184 static cp_token *cp_lexer_consume_token
185   (cp_lexer *);
186 static void cp_lexer_purge_token
187   (cp_lexer *);
188 static void cp_lexer_purge_tokens_after
189   (cp_lexer *, cp_token_position);
190 static void cp_lexer_save_tokens
191   (cp_lexer *);
192 static void cp_lexer_commit_tokens
193   (cp_lexer *);
194 static void cp_lexer_rollback_tokens
195   (cp_lexer *);
196 #ifdef ENABLE_CHECKING
197 static void cp_lexer_print_token
198   (FILE *, cp_token *);
199 static inline bool cp_lexer_debugging_p
200   (cp_lexer *);
201 static void cp_lexer_start_debugging
202   (cp_lexer *) ATTRIBUTE_UNUSED;
203 static void cp_lexer_stop_debugging
204   (cp_lexer *) ATTRIBUTE_UNUSED;
205 #else
206 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
207    about passing NULL to functions that require non-NULL arguments
208    (fputs, fprintf).  It will never be used, so all we need is a value
209    of the right type that's guaranteed not to be NULL.  */
210 #define cp_lexer_debug_stream stdout
211 #define cp_lexer_print_token(str, tok) (void) 0
212 #define cp_lexer_debugging_p(lexer) 0
213 #endif /* ENABLE_CHECKING */
214
215 static cp_token_cache *cp_token_cache_new
216   (cp_token *, cp_token *);
217
218 static void cp_parser_initial_pragma
219   (cp_token *);
220
221 /* Manifest constants.  */
222 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
223 #define CP_SAVED_TOKEN_STACK 5
224
225 /* A token type for keywords, as opposed to ordinary identifiers.  */
226 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
227
228 /* A token type for template-ids.  If a template-id is processed while
229    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
230    the value of the CPP_TEMPLATE_ID is whatever was returned by
231    cp_parser_template_id.  */
232 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
233
234 /* A token type for nested-name-specifiers.  If a
235    nested-name-specifier is processed while parsing tentatively, it is
236    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
237    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
238    cp_parser_nested_name_specifier_opt.  */
239 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
240
241 /* A token type for tokens that are not tokens at all; these are used
242    to represent slots in the array where there used to be a token
243    that has now been deleted.  */
244 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
245
246 /* The number of token types, including C++-specific ones.  */
247 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
248
249 /* Variables.  */
250
251 #ifdef ENABLE_CHECKING
252 /* The stream to which debugging output should be written.  */
253 static FILE *cp_lexer_debug_stream;
254 #endif /* ENABLE_CHECKING */
255
256 /* Create a new main C++ lexer, the lexer that gets tokens from the
257    preprocessor.  */
258
259 static cp_lexer *
260 cp_lexer_new_main (void)
261 {
262   cp_token first_token;
263   cp_lexer *lexer;
264   cp_token *pos;
265   size_t alloc;
266   size_t space;
267   cp_token *buffer;
268
269   /* It's possible that parsing the first pragma will load a PCH file,
270      which is a GC collection point.  So we have to do that before
271      allocating any memory.  */
272   cp_parser_initial_pragma (&first_token);
273
274   c_common_no_more_pch ();
275
276   /* Allocate the memory.  */
277   lexer = GGC_CNEW (cp_lexer);
278
279 #ifdef ENABLE_CHECKING
280   /* Initially we are not debugging.  */
281   lexer->debugging_p = false;
282 #endif /* ENABLE_CHECKING */
283   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
284                                    CP_SAVED_TOKEN_STACK);
285
286   /* Create the buffer.  */
287   alloc = CP_LEXER_BUFFER_SIZE;
288   buffer = GGC_NEWVEC (cp_token, alloc);
289
290   /* Put the first token in the buffer.  */
291   space = alloc;
292   pos = buffer;
293   *pos = first_token;
294
295   /* Get the remaining tokens from the preprocessor.  */
296   while (pos->type != CPP_EOF)
297     {
298       pos++;
299       if (!--space)
300         {
301           space = alloc;
302           alloc *= 2;
303           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
304           pos = buffer + space;
305         }
306       cp_lexer_get_preprocessor_token (lexer, pos);
307     }
308   lexer->buffer = buffer;
309   lexer->buffer_length = alloc - space;
310   lexer->last_token = pos;
311   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
312
313   /* Subsequent preprocessor diagnostics should use compiler
314      diagnostic functions to get the compiler source location.  */
315   cpp_get_options (parse_in)->client_diagnostic = true;
316   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
317
318   gcc_assert (lexer->next_token->type != CPP_PURGED);
319   return lexer;
320 }
321
322 /* Create a new lexer whose token stream is primed with the tokens in
323    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
324
325 static cp_lexer *
326 cp_lexer_new_from_tokens (cp_token_cache *cache)
327 {
328   cp_token *first = cache->first;
329   cp_token *last = cache->last;
330   cp_lexer *lexer = GGC_CNEW (cp_lexer);
331
332   /* We do not own the buffer.  */
333   lexer->buffer = NULL;
334   lexer->buffer_length = 0;
335   lexer->next_token = first == last ? &eof_token : first;
336   lexer->last_token = last;
337
338   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
339                                    CP_SAVED_TOKEN_STACK);
340
341 #ifdef ENABLE_CHECKING
342   /* Initially we are not debugging.  */
343   lexer->debugging_p = false;
344 #endif
345
346   gcc_assert (lexer->next_token->type != CPP_PURGED);
347   return lexer;
348 }
349
350 /* Frees all resources associated with LEXER.  */
351
352 static void
353 cp_lexer_destroy (cp_lexer *lexer)
354 {
355   if (lexer->buffer)
356     ggc_free (lexer->buffer);
357   VEC_free (cp_token_position, heap, lexer->saved_tokens);
358   ggc_free (lexer);
359 }
360
361 /* Returns nonzero if debugging information should be output.  */
362
363 #ifdef ENABLE_CHECKING
364
365 static inline bool
366 cp_lexer_debugging_p (cp_lexer *lexer)
367 {
368   return lexer->debugging_p;
369 }
370
371 #endif /* ENABLE_CHECKING */
372
373 static inline cp_token_position
374 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
375 {
376   gcc_assert (!previous_p || lexer->next_token != &eof_token);
377
378   return lexer->next_token - previous_p;
379 }
380
381 static inline cp_token *
382 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
383 {
384   return pos;
385 }
386
387 /* nonzero if we are presently saving tokens.  */
388
389 static inline int
390 cp_lexer_saving_tokens (const cp_lexer* lexer)
391 {
392   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 }
394
395 /* Store the next token from the preprocessor in *TOKEN.  Return true
396    if we reach EOF.  If LEXER is NULL, assume we are handling an
397    initial #pragma pch_preprocess, and thus want the lexer to return
398    processed strings.  */
399
400 static void
401 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
402 {
403   static int is_extern_c = 0;
404
405    /* Get a new token from the preprocessor.  */
406   token->type
407     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
408                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
409   token->keyword = RID_MAX;
410   token->pragma_kind = PRAGMA_NONE;
411   token->in_system_header = in_system_header;
412
413   /* On some systems, some header files are surrounded by an
414      implicit extern "C" block.  Set a flag in the token if it
415      comes from such a header.  */
416   is_extern_c += pending_lang_change;
417   pending_lang_change = 0;
418   token->implicit_extern_c = is_extern_c > 0;
419
420   /* Check to see if this token is a keyword.  */
421   if (token->type == CPP_NAME)
422     {
423       if (C_IS_RESERVED_WORD (token->u.value))
424         {
425           /* Mark this token as a keyword.  */
426           token->type = CPP_KEYWORD;
427           /* Record which keyword.  */
428           token->keyword = C_RID_CODE (token->u.value);
429           /* Update the value.  Some keywords are mapped to particular
430              entities, rather than simply having the value of the
431              corresponding IDENTIFIER_NODE.  For example, `__const' is
432              mapped to `const'.  */
433           token->u.value = ridpointers[token->keyword];
434         }
435       else
436         {
437           if (warn_cxx0x_compat
438               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
439               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
440             {
441               /* Warn about the C++0x keyword (but still treat it as
442                  an identifier).  */
443               warning (OPT_Wc__0x_compat, 
444                        "identifier %<%s%> will become a keyword in C++0x",
445                        IDENTIFIER_POINTER (token->u.value));
446
447               /* Clear out the C_RID_CODE so we don't warn about this
448                  particular identifier-turned-keyword again.  */
449               C_SET_RID_CODE (token->u.value, RID_MAX);
450             }
451
452           token->ambiguous_p = false;
453           token->keyword = RID_MAX;
454         }
455     }
456   /* Handle Objective-C++ keywords.  */
457   else if (token->type == CPP_AT_NAME)
458     {
459       token->type = CPP_KEYWORD;
460       switch (C_RID_CODE (token->u.value))
461         {
462         /* Map 'class' to '@class', 'private' to '@private', etc.  */
463         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
464         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
465         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
466         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
467         case RID_THROW: token->keyword = RID_AT_THROW; break;
468         case RID_TRY: token->keyword = RID_AT_TRY; break;
469         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
470         default: token->keyword = C_RID_CODE (token->u.value);
471         }
472     }
473   else if (token->type == CPP_PRAGMA)
474     {
475       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
476       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
477       token->u.value = NULL_TREE;
478     }
479 }
480
481 /* Update the globals input_location and in_system_header and the
482    input file stack from TOKEN.  */
483 static inline void
484 cp_lexer_set_source_position_from_token (cp_token *token)
485 {
486   if (token->type != CPP_EOF)
487     {
488       input_location = token->location;
489       in_system_header = token->in_system_header;
490     }
491 }
492
493 /* Return a pointer to the next token in the token stream, but do not
494    consume it.  */
495
496 static inline cp_token *
497 cp_lexer_peek_token (cp_lexer *lexer)
498 {
499   if (cp_lexer_debugging_p (lexer))
500     {
501       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
502       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
503       putc ('\n', cp_lexer_debug_stream);
504     }
505   return lexer->next_token;
506 }
507
508 /* Return true if the next token has the indicated TYPE.  */
509
510 static inline bool
511 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
512 {
513   return cp_lexer_peek_token (lexer)->type == type;
514 }
515
516 /* Return true if the next token does not have the indicated TYPE.  */
517
518 static inline bool
519 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
520 {
521   return !cp_lexer_next_token_is (lexer, type);
522 }
523
524 /* Return true if the next token is the indicated KEYWORD.  */
525
526 static inline bool
527 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
528 {
529   return cp_lexer_peek_token (lexer)->keyword == keyword;
530 }
531
532 /* Return true if the next token is not the indicated KEYWORD.  */
533
534 static inline bool
535 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
536 {
537   return cp_lexer_peek_token (lexer)->keyword != keyword;
538 }
539
540 /* Return true if the next token is a keyword for a decl-specifier.  */
541
542 static bool
543 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
544 {
545   cp_token *token;
546
547   token = cp_lexer_peek_token (lexer);
548   switch (token->keyword) 
549     {
550       /* auto specifier: storage-class-specifier in C++,
551          simple-type-specifier in C++0x.  */
552     case RID_AUTO:
553       /* Storage classes.  */
554     case RID_REGISTER:
555     case RID_STATIC:
556     case RID_EXTERN:
557     case RID_MUTABLE:
558     case RID_THREAD:
559       /* Elaborated type specifiers.  */
560     case RID_ENUM:
561     case RID_CLASS:
562     case RID_STRUCT:
563     case RID_UNION:
564     case RID_TYPENAME:
565       /* Simple type specifiers.  */
566     case RID_CHAR:
567     case RID_CHAR16:
568     case RID_CHAR32:
569     case RID_WCHAR:
570     case RID_BOOL:
571     case RID_SHORT:
572     case RID_INT:
573     case RID_LONG:
574     case RID_SIGNED:
575     case RID_UNSIGNED:
576     case RID_FLOAT:
577     case RID_DOUBLE:
578     case RID_VOID:
579       /* GNU extensions.  */ 
580     case RID_ATTRIBUTE:
581     case RID_TYPEOF:
582       /* C++0x extensions.  */
583     case RID_DECLTYPE:
584       return true;
585
586     default:
587       return false;
588     }
589 }
590
591 /* Return a pointer to the Nth token in the token stream.  If N is 1,
592    then this is precisely equivalent to cp_lexer_peek_token (except
593    that it is not inline).  One would like to disallow that case, but
594    there is one case (cp_parser_nth_token_starts_template_id) where
595    the caller passes a variable for N and it might be 1.  */
596
597 static cp_token *
598 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
599 {
600   cp_token *token;
601
602   /* N is 1-based, not zero-based.  */
603   gcc_assert (n > 0);
604
605   if (cp_lexer_debugging_p (lexer))
606     fprintf (cp_lexer_debug_stream,
607              "cp_lexer: peeking ahead %ld at token: ", (long)n);
608
609   --n;
610   token = lexer->next_token;
611   gcc_assert (!n || token != &eof_token);
612   while (n != 0)
613     {
614       ++token;
615       if (token == lexer->last_token)
616         {
617           token = &eof_token;
618           break;
619         }
620
621       if (token->type != CPP_PURGED)
622         --n;
623     }
624
625   if (cp_lexer_debugging_p (lexer))
626     {
627       cp_lexer_print_token (cp_lexer_debug_stream, token);
628       putc ('\n', cp_lexer_debug_stream);
629     }
630
631   return token;
632 }
633
634 /* Return the next token, and advance the lexer's next_token pointer
635    to point to the next non-purged token.  */
636
637 static cp_token *
638 cp_lexer_consume_token (cp_lexer* lexer)
639 {
640   cp_token *token = lexer->next_token;
641
642   gcc_assert (token != &eof_token);
643   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
644
645   do
646     {
647       lexer->next_token++;
648       if (lexer->next_token == lexer->last_token)
649         {
650           lexer->next_token = &eof_token;
651           break;
652         }
653
654     }
655   while (lexer->next_token->type == CPP_PURGED);
656
657   cp_lexer_set_source_position_from_token (token);
658
659   /* Provide debugging output.  */
660   if (cp_lexer_debugging_p (lexer))
661     {
662       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
663       cp_lexer_print_token (cp_lexer_debug_stream, token);
664       putc ('\n', cp_lexer_debug_stream);
665     }
666
667   return token;
668 }
669
670 /* Permanently remove the next token from the token stream, and
671    advance the next_token pointer to refer to the next non-purged
672    token.  */
673
674 static void
675 cp_lexer_purge_token (cp_lexer *lexer)
676 {
677   cp_token *tok = lexer->next_token;
678
679   gcc_assert (tok != &eof_token);
680   tok->type = CPP_PURGED;
681   tok->location = UNKNOWN_LOCATION;
682   tok->u.value = NULL_TREE;
683   tok->keyword = RID_MAX;
684
685   do
686     {
687       tok++;
688       if (tok == lexer->last_token)
689         {
690           tok = &eof_token;
691           break;
692         }
693     }
694   while (tok->type == CPP_PURGED);
695   lexer->next_token = tok;
696 }
697
698 /* Permanently remove all tokens after TOK, up to, but not
699    including, the token that will be returned next by
700    cp_lexer_peek_token.  */
701
702 static void
703 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
704 {
705   cp_token *peek = lexer->next_token;
706
707   if (peek == &eof_token)
708     peek = lexer->last_token;
709
710   gcc_assert (tok < peek);
711
712   for ( tok += 1; tok != peek; tok += 1)
713     {
714       tok->type = CPP_PURGED;
715       tok->location = UNKNOWN_LOCATION;
716       tok->u.value = NULL_TREE;
717       tok->keyword = RID_MAX;
718     }
719 }
720
721 /* Begin saving tokens.  All tokens consumed after this point will be
722    preserved.  */
723
724 static void
725 cp_lexer_save_tokens (cp_lexer* lexer)
726 {
727   /* Provide debugging output.  */
728   if (cp_lexer_debugging_p (lexer))
729     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
730
731   VEC_safe_push (cp_token_position, heap,
732                  lexer->saved_tokens, lexer->next_token);
733 }
734
735 /* Commit to the portion of the token stream most recently saved.  */
736
737 static void
738 cp_lexer_commit_tokens (cp_lexer* lexer)
739 {
740   /* Provide debugging output.  */
741   if (cp_lexer_debugging_p (lexer))
742     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
743
744   VEC_pop (cp_token_position, lexer->saved_tokens);
745 }
746
747 /* Return all tokens saved since the last call to cp_lexer_save_tokens
748    to the token stream.  Stop saving tokens.  */
749
750 static void
751 cp_lexer_rollback_tokens (cp_lexer* lexer)
752 {
753   /* Provide debugging output.  */
754   if (cp_lexer_debugging_p (lexer))
755     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
756
757   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
758 }
759
760 /* Print a representation of the TOKEN on the STREAM.  */
761
762 #ifdef ENABLE_CHECKING
763
764 static void
765 cp_lexer_print_token (FILE * stream, cp_token *token)
766 {
767   /* We don't use cpp_type2name here because the parser defines
768      a few tokens of its own.  */
769   static const char *const token_names[] = {
770     /* cpplib-defined token types */
771 #define OP(e, s) #e,
772 #define TK(e, s) #e,
773     TTYPE_TABLE
774 #undef OP
775 #undef TK
776     /* C++ parser token types - see "Manifest constants", above.  */
777     "KEYWORD",
778     "TEMPLATE_ID",
779     "NESTED_NAME_SPECIFIER",
780     "PURGED"
781   };
782
783   /* If we have a name for the token, print it out.  Otherwise, we
784      simply give the numeric code.  */
785   gcc_assert (token->type < ARRAY_SIZE(token_names));
786   fputs (token_names[token->type], stream);
787
788   /* For some tokens, print the associated data.  */
789   switch (token->type)
790     {
791     case CPP_KEYWORD:
792       /* Some keywords have a value that is not an IDENTIFIER_NODE.
793          For example, `struct' is mapped to an INTEGER_CST.  */
794       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
795         break;
796       /* else fall through */
797     case CPP_NAME:
798       fputs (IDENTIFIER_POINTER (token->u.value), stream);
799       break;
800
801     case CPP_STRING:
802     case CPP_STRING16:
803     case CPP_STRING32:
804     case CPP_WSTRING:
805       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
806       break;
807
808     default:
809       break;
810     }
811 }
812
813 /* Start emitting debugging information.  */
814
815 static void
816 cp_lexer_start_debugging (cp_lexer* lexer)
817 {
818   lexer->debugging_p = true;
819 }
820
821 /* Stop emitting debugging information.  */
822
823 static void
824 cp_lexer_stop_debugging (cp_lexer* lexer)
825 {
826   lexer->debugging_p = false;
827 }
828
829 #endif /* ENABLE_CHECKING */
830
831 /* Create a new cp_token_cache, representing a range of tokens.  */
832
833 static cp_token_cache *
834 cp_token_cache_new (cp_token *first, cp_token *last)
835 {
836   cp_token_cache *cache = GGC_NEW (cp_token_cache);
837   cache->first = first;
838   cache->last = last;
839   return cache;
840 }
841
842 \f
843 /* Decl-specifiers.  */
844
845 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
846
847 static void
848 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
849 {
850   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
851 }
852
853 /* Declarators.  */
854
855 /* Nothing other than the parser should be creating declarators;
856    declarators are a semi-syntactic representation of C++ entities.
857    Other parts of the front end that need to create entities (like
858    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
859
860 static cp_declarator *make_call_declarator
861   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
862 static cp_declarator *make_array_declarator
863   (cp_declarator *, tree);
864 static cp_declarator *make_pointer_declarator
865   (cp_cv_quals, cp_declarator *);
866 static cp_declarator *make_reference_declarator
867   (cp_cv_quals, cp_declarator *, bool);
868 static cp_parameter_declarator *make_parameter_declarator
869   (cp_decl_specifier_seq *, cp_declarator *, tree);
870 static cp_declarator *make_ptrmem_declarator
871   (cp_cv_quals, tree, cp_declarator *);
872
873 /* An erroneous declarator.  */
874 static cp_declarator *cp_error_declarator;
875
876 /* The obstack on which declarators and related data structures are
877    allocated.  */
878 static struct obstack declarator_obstack;
879
880 /* Alloc BYTES from the declarator memory pool.  */
881
882 static inline void *
883 alloc_declarator (size_t bytes)
884 {
885   return obstack_alloc (&declarator_obstack, bytes);
886 }
887
888 /* Allocate a declarator of the indicated KIND.  Clear fields that are
889    common to all declarators.  */
890
891 static cp_declarator *
892 make_declarator (cp_declarator_kind kind)
893 {
894   cp_declarator *declarator;
895
896   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
897   declarator->kind = kind;
898   declarator->attributes = NULL_TREE;
899   declarator->declarator = NULL;
900   declarator->parameter_pack_p = false;
901
902   return declarator;
903 }
904
905 /* Make a declarator for a generalized identifier.  If
906    QUALIFYING_SCOPE is non-NULL, the identifier is
907    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
908    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
909    is, if any.   */
910
911 static cp_declarator *
912 make_id_declarator (tree qualifying_scope, tree unqualified_name,
913                     special_function_kind sfk)
914 {
915   cp_declarator *declarator;
916
917   /* It is valid to write:
918
919        class C { void f(); };
920        typedef C D;
921        void D::f();
922
923      The standard is not clear about whether `typedef const C D' is
924      legal; as of 2002-09-15 the committee is considering that
925      question.  EDG 3.0 allows that syntax.  Therefore, we do as
926      well.  */
927   if (qualifying_scope && TYPE_P (qualifying_scope))
928     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
929
930   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
931               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
932               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
933
934   declarator = make_declarator (cdk_id);
935   declarator->u.id.qualifying_scope = qualifying_scope;
936   declarator->u.id.unqualified_name = unqualified_name;
937   declarator->u.id.sfk = sfk;
938   
939   return declarator;
940 }
941
942 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
943    of modifiers such as const or volatile to apply to the pointer
944    type, represented as identifiers.  */
945
946 cp_declarator *
947 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
948 {
949   cp_declarator *declarator;
950
951   declarator = make_declarator (cdk_pointer);
952   declarator->declarator = target;
953   declarator->u.pointer.qualifiers = cv_qualifiers;
954   declarator->u.pointer.class_type = NULL_TREE;
955   if (target)
956     {
957       declarator->parameter_pack_p = target->parameter_pack_p;
958       target->parameter_pack_p = false;
959     }
960   else
961     declarator->parameter_pack_p = false;
962
963   return declarator;
964 }
965
966 /* Like make_pointer_declarator -- but for references.  */
967
968 cp_declarator *
969 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
970                            bool rvalue_ref)
971 {
972   cp_declarator *declarator;
973
974   declarator = make_declarator (cdk_reference);
975   declarator->declarator = target;
976   declarator->u.reference.qualifiers = cv_qualifiers;
977   declarator->u.reference.rvalue_ref = rvalue_ref;
978   if (target)
979     {
980       declarator->parameter_pack_p = target->parameter_pack_p;
981       target->parameter_pack_p = false;
982     }
983   else
984     declarator->parameter_pack_p = false;
985
986   return declarator;
987 }
988
989 /* Like make_pointer_declarator -- but for a pointer to a non-static
990    member of CLASS_TYPE.  */
991
992 cp_declarator *
993 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
994                         cp_declarator *pointee)
995 {
996   cp_declarator *declarator;
997
998   declarator = make_declarator (cdk_ptrmem);
999   declarator->declarator = pointee;
1000   declarator->u.pointer.qualifiers = cv_qualifiers;
1001   declarator->u.pointer.class_type = class_type;
1002
1003   if (pointee)
1004     {
1005       declarator->parameter_pack_p = pointee->parameter_pack_p;
1006       pointee->parameter_pack_p = false;
1007     }
1008   else
1009     declarator->parameter_pack_p = false;
1010
1011   return declarator;
1012 }
1013
1014 /* Make a declarator for the function given by TARGET, with the
1015    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1016    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1017    indicates what exceptions can be thrown.  */
1018
1019 cp_declarator *
1020 make_call_declarator (cp_declarator *target,
1021                       cp_parameter_declarator *parms,
1022                       cp_cv_quals cv_qualifiers,
1023                       tree exception_specification)
1024 {
1025   cp_declarator *declarator;
1026
1027   declarator = make_declarator (cdk_function);
1028   declarator->declarator = target;
1029   declarator->u.function.parameters = parms;
1030   declarator->u.function.qualifiers = cv_qualifiers;
1031   declarator->u.function.exception_specification = exception_specification;
1032   if (target)
1033     {
1034       declarator->parameter_pack_p = target->parameter_pack_p;
1035       target->parameter_pack_p = false;
1036     }
1037   else
1038     declarator->parameter_pack_p = false;
1039
1040   return declarator;
1041 }
1042
1043 /* Make a declarator for an array of BOUNDS elements, each of which is
1044    defined by ELEMENT.  */
1045
1046 cp_declarator *
1047 make_array_declarator (cp_declarator *element, tree bounds)
1048 {
1049   cp_declarator *declarator;
1050
1051   declarator = make_declarator (cdk_array);
1052   declarator->declarator = element;
1053   declarator->u.array.bounds = bounds;
1054   if (element)
1055     {
1056       declarator->parameter_pack_p = element->parameter_pack_p;
1057       element->parameter_pack_p = false;
1058     }
1059   else
1060     declarator->parameter_pack_p = false;
1061
1062   return declarator;
1063 }
1064
1065 /* Determine whether the declarator we've seen so far can be a
1066    parameter pack, when followed by an ellipsis.  */
1067 static bool 
1068 declarator_can_be_parameter_pack (cp_declarator *declarator)
1069 {
1070   /* Search for a declarator name, or any other declarator that goes
1071      after the point where the ellipsis could appear in a parameter
1072      pack. If we find any of these, then this declarator can not be
1073      made into a parameter pack.  */
1074   bool found = false;
1075   while (declarator && !found)
1076     {
1077       switch ((int)declarator->kind)
1078         {
1079         case cdk_id:
1080         case cdk_array:
1081           found = true;
1082           break;
1083
1084         case cdk_error:
1085           return true;
1086
1087         default:
1088           declarator = declarator->declarator;
1089           break;
1090         }
1091     }
1092
1093   return !found;
1094 }
1095
1096 cp_parameter_declarator *no_parameters;
1097
1098 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1099    DECLARATOR and DEFAULT_ARGUMENT.  */
1100
1101 cp_parameter_declarator *
1102 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1103                            cp_declarator *declarator,
1104                            tree default_argument)
1105 {
1106   cp_parameter_declarator *parameter;
1107
1108   parameter = ((cp_parameter_declarator *)
1109                alloc_declarator (sizeof (cp_parameter_declarator)));
1110   parameter->next = NULL;
1111   if (decl_specifiers)
1112     parameter->decl_specifiers = *decl_specifiers;
1113   else
1114     clear_decl_specs (&parameter->decl_specifiers);
1115   parameter->declarator = declarator;
1116   parameter->default_argument = default_argument;
1117   parameter->ellipsis_p = false;
1118
1119   return parameter;
1120 }
1121
1122 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1123
1124 static bool
1125 function_declarator_p (const cp_declarator *declarator)
1126 {
1127   while (declarator)
1128     {
1129       if (declarator->kind == cdk_function
1130           && declarator->declarator->kind == cdk_id)
1131         return true;
1132       if (declarator->kind == cdk_id
1133           || declarator->kind == cdk_error)
1134         return false;
1135       declarator = declarator->declarator;
1136     }
1137   return false;
1138 }
1139  
1140 /* The parser.  */
1141
1142 /* Overview
1143    --------
1144
1145    A cp_parser parses the token stream as specified by the C++
1146    grammar.  Its job is purely parsing, not semantic analysis.  For
1147    example, the parser breaks the token stream into declarators,
1148    expressions, statements, and other similar syntactic constructs.
1149    It does not check that the types of the expressions on either side
1150    of an assignment-statement are compatible, or that a function is
1151    not declared with a parameter of type `void'.
1152
1153    The parser invokes routines elsewhere in the compiler to perform
1154    semantic analysis and to build up the abstract syntax tree for the
1155    code processed.
1156
1157    The parser (and the template instantiation code, which is, in a
1158    way, a close relative of parsing) are the only parts of the
1159    compiler that should be calling push_scope and pop_scope, or
1160    related functions.  The parser (and template instantiation code)
1161    keeps track of what scope is presently active; everything else
1162    should simply honor that.  (The code that generates static
1163    initializers may also need to set the scope, in order to check
1164    access control correctly when emitting the initializers.)
1165
1166    Methodology
1167    -----------
1168
1169    The parser is of the standard recursive-descent variety.  Upcoming
1170    tokens in the token stream are examined in order to determine which
1171    production to use when parsing a non-terminal.  Some C++ constructs
1172    require arbitrary look ahead to disambiguate.  For example, it is
1173    impossible, in the general case, to tell whether a statement is an
1174    expression or declaration without scanning the entire statement.
1175    Therefore, the parser is capable of "parsing tentatively."  When the
1176    parser is not sure what construct comes next, it enters this mode.
1177    Then, while we attempt to parse the construct, the parser queues up
1178    error messages, rather than issuing them immediately, and saves the
1179    tokens it consumes.  If the construct is parsed successfully, the
1180    parser "commits", i.e., it issues any queued error messages and
1181    the tokens that were being preserved are permanently discarded.
1182    If, however, the construct is not parsed successfully, the parser
1183    rolls back its state completely so that it can resume parsing using
1184    a different alternative.
1185
1186    Future Improvements
1187    -------------------
1188
1189    The performance of the parser could probably be improved substantially.
1190    We could often eliminate the need to parse tentatively by looking ahead
1191    a little bit.  In some places, this approach might not entirely eliminate
1192    the need to parse tentatively, but it might still speed up the average
1193    case.  */
1194
1195 /* Flags that are passed to some parsing functions.  These values can
1196    be bitwise-ored together.  */
1197
1198 typedef enum cp_parser_flags
1199 {
1200   /* No flags.  */
1201   CP_PARSER_FLAGS_NONE = 0x0,
1202   /* The construct is optional.  If it is not present, then no error
1203      should be issued.  */
1204   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1205   /* When parsing a type-specifier, do not allow user-defined types.  */
1206   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1207 } cp_parser_flags;
1208
1209 /* The different kinds of declarators we want to parse.  */
1210
1211 typedef enum cp_parser_declarator_kind
1212 {
1213   /* We want an abstract declarator.  */
1214   CP_PARSER_DECLARATOR_ABSTRACT,
1215   /* We want a named declarator.  */
1216   CP_PARSER_DECLARATOR_NAMED,
1217   /* We don't mind, but the name must be an unqualified-id.  */
1218   CP_PARSER_DECLARATOR_EITHER
1219 } cp_parser_declarator_kind;
1220
1221 /* The precedence values used to parse binary expressions.  The minimum value
1222    of PREC must be 1, because zero is reserved to quickly discriminate
1223    binary operators from other tokens.  */
1224
1225 enum cp_parser_prec
1226 {
1227   PREC_NOT_OPERATOR,
1228   PREC_LOGICAL_OR_EXPRESSION,
1229   PREC_LOGICAL_AND_EXPRESSION,
1230   PREC_INCLUSIVE_OR_EXPRESSION,
1231   PREC_EXCLUSIVE_OR_EXPRESSION,
1232   PREC_AND_EXPRESSION,
1233   PREC_EQUALITY_EXPRESSION,
1234   PREC_RELATIONAL_EXPRESSION,
1235   PREC_SHIFT_EXPRESSION,
1236   PREC_ADDITIVE_EXPRESSION,
1237   PREC_MULTIPLICATIVE_EXPRESSION,
1238   PREC_PM_EXPRESSION,
1239   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1240 };
1241
1242 /* A mapping from a token type to a corresponding tree node type, with a
1243    precedence value.  */
1244
1245 typedef struct cp_parser_binary_operations_map_node
1246 {
1247   /* The token type.  */
1248   enum cpp_ttype token_type;
1249   /* The corresponding tree code.  */
1250   enum tree_code tree_type;
1251   /* The precedence of this operator.  */
1252   enum cp_parser_prec prec;
1253 } cp_parser_binary_operations_map_node;
1254
1255 /* The status of a tentative parse.  */
1256
1257 typedef enum cp_parser_status_kind
1258 {
1259   /* No errors have occurred.  */
1260   CP_PARSER_STATUS_KIND_NO_ERROR,
1261   /* An error has occurred.  */
1262   CP_PARSER_STATUS_KIND_ERROR,
1263   /* We are committed to this tentative parse, whether or not an error
1264      has occurred.  */
1265   CP_PARSER_STATUS_KIND_COMMITTED
1266 } cp_parser_status_kind;
1267
1268 typedef struct cp_parser_expression_stack_entry
1269 {
1270   /* Left hand side of the binary operation we are currently
1271      parsing.  */
1272   tree lhs;
1273   /* Original tree code for left hand side, if it was a binary
1274      expression itself (used for -Wparentheses).  */
1275   enum tree_code lhs_type;
1276   /* Tree code for the binary operation we are parsing.  */
1277   enum tree_code tree_type;
1278   /* Precedence of the binary operation we are parsing.  */
1279   int prec;
1280 } cp_parser_expression_stack_entry;
1281
1282 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1283    entries because precedence levels on the stack are monotonically
1284    increasing.  */
1285 typedef struct cp_parser_expression_stack_entry
1286   cp_parser_expression_stack[NUM_PREC_VALUES];
1287
1288 /* Context that is saved and restored when parsing tentatively.  */
1289 typedef struct cp_parser_context GTY (())
1290 {
1291   /* If this is a tentative parsing context, the status of the
1292      tentative parse.  */
1293   enum cp_parser_status_kind status;
1294   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1295      that are looked up in this context must be looked up both in the
1296      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1297      the context of the containing expression.  */
1298   tree object_type;
1299
1300   /* The next parsing context in the stack.  */
1301   struct cp_parser_context *next;
1302 } cp_parser_context;
1303
1304 /* Prototypes.  */
1305
1306 /* Constructors and destructors.  */
1307
1308 static cp_parser_context *cp_parser_context_new
1309   (cp_parser_context *);
1310
1311 /* Class variables.  */
1312
1313 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1314
1315 /* The operator-precedence table used by cp_parser_binary_expression.
1316    Transformed into an associative array (binops_by_token) by
1317    cp_parser_new.  */
1318
1319 static const cp_parser_binary_operations_map_node binops[] = {
1320   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1321   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1322
1323   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326
1327   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1328   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1329
1330   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1331   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1332
1333   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1334   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1336   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1337
1338   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1339   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1340
1341   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1342
1343   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1344
1345   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1346
1347   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1348
1349   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1350 };
1351
1352 /* The same as binops, but initialized by cp_parser_new so that
1353    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1354    for speed.  */
1355 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1356
1357 /* Constructors and destructors.  */
1358
1359 /* Construct a new context.  The context below this one on the stack
1360    is given by NEXT.  */
1361
1362 static cp_parser_context *
1363 cp_parser_context_new (cp_parser_context* next)
1364 {
1365   cp_parser_context *context;
1366
1367   /* Allocate the storage.  */
1368   if (cp_parser_context_free_list != NULL)
1369     {
1370       /* Pull the first entry from the free list.  */
1371       context = cp_parser_context_free_list;
1372       cp_parser_context_free_list = context->next;
1373       memset (context, 0, sizeof (*context));
1374     }
1375   else
1376     context = GGC_CNEW (cp_parser_context);
1377
1378   /* No errors have occurred yet in this context.  */
1379   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1380   /* If this is not the bottomost context, copy information that we
1381      need from the previous context.  */
1382   if (next)
1383     {
1384       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1385          expression, then we are parsing one in this context, too.  */
1386       context->object_type = next->object_type;
1387       /* Thread the stack.  */
1388       context->next = next;
1389     }
1390
1391   return context;
1392 }
1393
1394 /* The cp_parser structure represents the C++ parser.  */
1395
1396 typedef struct cp_parser GTY(())
1397 {
1398   /* The lexer from which we are obtaining tokens.  */
1399   cp_lexer *lexer;
1400
1401   /* The scope in which names should be looked up.  If NULL_TREE, then
1402      we look up names in the scope that is currently open in the
1403      source program.  If non-NULL, this is either a TYPE or
1404      NAMESPACE_DECL for the scope in which we should look.  It can
1405      also be ERROR_MARK, when we've parsed a bogus scope.
1406
1407      This value is not cleared automatically after a name is looked
1408      up, so we must be careful to clear it before starting a new look
1409      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1410      will look up `Z' in the scope of `X', rather than the current
1411      scope.)  Unfortunately, it is difficult to tell when name lookup
1412      is complete, because we sometimes peek at a token, look it up,
1413      and then decide not to consume it.   */
1414   tree scope;
1415
1416   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1417      last lookup took place.  OBJECT_SCOPE is used if an expression
1418      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1419      respectively.  QUALIFYING_SCOPE is used for an expression of the
1420      form "X::Y"; it refers to X.  */
1421   tree object_scope;
1422   tree qualifying_scope;
1423
1424   /* A stack of parsing contexts.  All but the bottom entry on the
1425      stack will be tentative contexts.
1426
1427      We parse tentatively in order to determine which construct is in
1428      use in some situations.  For example, in order to determine
1429      whether a statement is an expression-statement or a
1430      declaration-statement we parse it tentatively as a
1431      declaration-statement.  If that fails, we then reparse the same
1432      token stream as an expression-statement.  */
1433   cp_parser_context *context;
1434
1435   /* True if we are parsing GNU C++.  If this flag is not set, then
1436      GNU extensions are not recognized.  */
1437   bool allow_gnu_extensions_p;
1438
1439   /* TRUE if the `>' token should be interpreted as the greater-than
1440      operator.  FALSE if it is the end of a template-id or
1441      template-parameter-list. In C++0x mode, this flag also applies to
1442      `>>' tokens, which are viewed as two consecutive `>' tokens when
1443      this flag is FALSE.  */
1444   bool greater_than_is_operator_p;
1445
1446   /* TRUE if default arguments are allowed within a parameter list
1447      that starts at this point. FALSE if only a gnu extension makes
1448      them permissible.  */
1449   bool default_arg_ok_p;
1450
1451   /* TRUE if we are parsing an integral constant-expression.  See
1452      [expr.const] for a precise definition.  */
1453   bool integral_constant_expression_p;
1454
1455   /* TRUE if we are parsing an integral constant-expression -- but a
1456      non-constant expression should be permitted as well.  This flag
1457      is used when parsing an array bound so that GNU variable-length
1458      arrays are tolerated.  */
1459   bool allow_non_integral_constant_expression_p;
1460
1461   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1462      been seen that makes the expression non-constant.  */
1463   bool non_integral_constant_expression_p;
1464
1465   /* TRUE if local variable names and `this' are forbidden in the
1466      current context.  */
1467   bool local_variables_forbidden_p;
1468
1469   /* TRUE if the declaration we are parsing is part of a
1470      linkage-specification of the form `extern string-literal
1471      declaration'.  */
1472   bool in_unbraced_linkage_specification_p;
1473
1474   /* TRUE if we are presently parsing a declarator, after the
1475      direct-declarator.  */
1476   bool in_declarator_p;
1477
1478   /* TRUE if we are presently parsing a template-argument-list.  */
1479   bool in_template_argument_list_p;
1480
1481   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1482      to IN_OMP_BLOCK if parsing OpenMP structured block and
1483      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1484      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1485      iteration-statement, OpenMP block or loop within that switch.  */
1486 #define IN_SWITCH_STMT          1
1487 #define IN_ITERATION_STMT       2
1488 #define IN_OMP_BLOCK            4
1489 #define IN_OMP_FOR              8
1490 #define IN_IF_STMT             16
1491   unsigned char in_statement;
1492
1493   /* TRUE if we are presently parsing the body of a switch statement.
1494      Note that this doesn't quite overlap with in_statement above.
1495      The difference relates to giving the right sets of error messages:
1496      "case not in switch" vs "break statement used with OpenMP...".  */
1497   bool in_switch_statement_p;
1498
1499   /* TRUE if we are parsing a type-id in an expression context.  In
1500      such a situation, both "type (expr)" and "type (type)" are valid
1501      alternatives.  */
1502   bool in_type_id_in_expr_p;
1503
1504   /* TRUE if we are currently in a header file where declarations are
1505      implicitly extern "C".  */
1506   bool implicit_extern_c;
1507
1508   /* TRUE if strings in expressions should be translated to the execution
1509      character set.  */
1510   bool translate_strings_p;
1511
1512   /* TRUE if we are presently parsing the body of a function, but not
1513      a local class.  */
1514   bool in_function_body;
1515
1516   /* If non-NULL, then we are parsing a construct where new type
1517      definitions are not permitted.  The string stored here will be
1518      issued as an error message if a type is defined.  */
1519   const char *type_definition_forbidden_message;
1520
1521   /* A list of lists. The outer list is a stack, used for member
1522      functions of local classes. At each level there are two sub-list,
1523      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1524      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1525      TREE_VALUE's. The functions are chained in reverse declaration
1526      order.
1527
1528      The TREE_PURPOSE sublist contains those functions with default
1529      arguments that need post processing, and the TREE_VALUE sublist
1530      contains those functions with definitions that need post
1531      processing.
1532
1533      These lists can only be processed once the outermost class being
1534      defined is complete.  */
1535   tree unparsed_functions_queues;
1536
1537   /* The number of classes whose definitions are currently in
1538      progress.  */
1539   unsigned num_classes_being_defined;
1540
1541   /* The number of template parameter lists that apply directly to the
1542      current declaration.  */
1543   unsigned num_template_parameter_lists;
1544 } cp_parser;
1545
1546 /* Prototypes.  */
1547
1548 /* Constructors and destructors.  */
1549
1550 static cp_parser *cp_parser_new
1551   (void);
1552
1553 /* Routines to parse various constructs.
1554
1555    Those that return `tree' will return the error_mark_node (rather
1556    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1557    Sometimes, they will return an ordinary node if error-recovery was
1558    attempted, even though a parse error occurred.  So, to check
1559    whether or not a parse error occurred, you should always use
1560    cp_parser_error_occurred.  If the construct is optional (indicated
1561    either by an `_opt' in the name of the function that does the
1562    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1563    the construct is not present.  */
1564
1565 /* Lexical conventions [gram.lex]  */
1566
1567 static tree cp_parser_identifier
1568   (cp_parser *);
1569 static tree cp_parser_string_literal
1570   (cp_parser *, bool, bool);
1571
1572 /* Basic concepts [gram.basic]  */
1573
1574 static bool cp_parser_translation_unit
1575   (cp_parser *);
1576
1577 /* Expressions [gram.expr]  */
1578
1579 static tree cp_parser_primary_expression
1580   (cp_parser *, bool, bool, bool, cp_id_kind *);
1581 static tree cp_parser_id_expression
1582   (cp_parser *, bool, bool, bool *, bool, bool);
1583 static tree cp_parser_unqualified_id
1584   (cp_parser *, bool, bool, bool, bool);
1585 static tree cp_parser_nested_name_specifier_opt
1586   (cp_parser *, bool, bool, bool, bool);
1587 static tree cp_parser_nested_name_specifier
1588   (cp_parser *, bool, bool, bool, bool);
1589 static tree cp_parser_class_or_namespace_name
1590   (cp_parser *, bool, bool, bool, bool, bool);
1591 static tree cp_parser_postfix_expression
1592   (cp_parser *, bool, bool, bool);
1593 static tree cp_parser_postfix_open_square_expression
1594   (cp_parser *, tree, bool);
1595 static tree cp_parser_postfix_dot_deref_expression
1596   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1597 static tree cp_parser_parenthesized_expression_list
1598   (cp_parser *, bool, bool, bool, bool *);
1599 static void cp_parser_pseudo_destructor_name
1600   (cp_parser *, tree *, tree *);
1601 static tree cp_parser_unary_expression
1602   (cp_parser *, bool, bool);
1603 static enum tree_code cp_parser_unary_operator
1604   (cp_token *);
1605 static tree cp_parser_new_expression
1606   (cp_parser *);
1607 static tree cp_parser_new_placement
1608   (cp_parser *);
1609 static tree cp_parser_new_type_id
1610   (cp_parser *, tree *);
1611 static cp_declarator *cp_parser_new_declarator_opt
1612   (cp_parser *);
1613 static cp_declarator *cp_parser_direct_new_declarator
1614   (cp_parser *);
1615 static tree cp_parser_new_initializer
1616   (cp_parser *);
1617 static tree cp_parser_delete_expression
1618   (cp_parser *);
1619 static tree cp_parser_cast_expression
1620   (cp_parser *, bool, bool);
1621 static tree cp_parser_binary_expression
1622   (cp_parser *, bool, enum cp_parser_prec);
1623 static tree cp_parser_question_colon_clause
1624   (cp_parser *, tree);
1625 static tree cp_parser_assignment_expression
1626   (cp_parser *, bool);
1627 static enum tree_code cp_parser_assignment_operator_opt
1628   (cp_parser *);
1629 static tree cp_parser_expression
1630   (cp_parser *, bool);
1631 static tree cp_parser_constant_expression
1632   (cp_parser *, bool, bool *);
1633 static tree cp_parser_builtin_offsetof
1634   (cp_parser *);
1635
1636 /* Statements [gram.stmt.stmt]  */
1637
1638 static void cp_parser_statement
1639   (cp_parser *, tree, bool, bool *);
1640 static void cp_parser_label_for_labeled_statement
1641   (cp_parser *);
1642 static tree cp_parser_expression_statement
1643   (cp_parser *, tree);
1644 static tree cp_parser_compound_statement
1645   (cp_parser *, tree, bool);
1646 static void cp_parser_statement_seq_opt
1647   (cp_parser *, tree);
1648 static tree cp_parser_selection_statement
1649   (cp_parser *, bool *);
1650 static tree cp_parser_condition
1651   (cp_parser *);
1652 static tree cp_parser_iteration_statement
1653   (cp_parser *);
1654 static void cp_parser_for_init_statement
1655   (cp_parser *);
1656 static tree cp_parser_jump_statement
1657   (cp_parser *);
1658 static void cp_parser_declaration_statement
1659   (cp_parser *);
1660
1661 static tree cp_parser_implicitly_scoped_statement
1662   (cp_parser *, bool *);
1663 static void cp_parser_already_scoped_statement
1664   (cp_parser *);
1665
1666 /* Declarations [gram.dcl.dcl] */
1667
1668 static void cp_parser_declaration_seq_opt
1669   (cp_parser *);
1670 static void cp_parser_declaration
1671   (cp_parser *);
1672 static void cp_parser_block_declaration
1673   (cp_parser *, bool);
1674 static void cp_parser_simple_declaration
1675   (cp_parser *, bool);
1676 static void cp_parser_decl_specifier_seq
1677   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1678 static tree cp_parser_storage_class_specifier_opt
1679   (cp_parser *);
1680 static tree cp_parser_function_specifier_opt
1681   (cp_parser *, cp_decl_specifier_seq *);
1682 static tree cp_parser_type_specifier
1683   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1684    int *, bool *);
1685 static tree cp_parser_simple_type_specifier
1686   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1687 static tree cp_parser_type_name
1688   (cp_parser *);
1689 static tree cp_parser_nonclass_name 
1690   (cp_parser* parser);
1691 static tree cp_parser_elaborated_type_specifier
1692   (cp_parser *, bool, bool);
1693 static tree cp_parser_enum_specifier
1694   (cp_parser *);
1695 static void cp_parser_enumerator_list
1696   (cp_parser *, tree);
1697 static void cp_parser_enumerator_definition
1698   (cp_parser *, tree);
1699 static tree cp_parser_namespace_name
1700   (cp_parser *);
1701 static void cp_parser_namespace_definition
1702   (cp_parser *);
1703 static void cp_parser_namespace_body
1704   (cp_parser *);
1705 static tree cp_parser_qualified_namespace_specifier
1706   (cp_parser *);
1707 static void cp_parser_namespace_alias_definition
1708   (cp_parser *);
1709 static bool cp_parser_using_declaration
1710   (cp_parser *, bool);
1711 static void cp_parser_using_directive
1712   (cp_parser *);
1713 static void cp_parser_asm_definition
1714   (cp_parser *);
1715 static void cp_parser_linkage_specification
1716   (cp_parser *);
1717 static void cp_parser_static_assert
1718   (cp_parser *, bool);
1719 static tree cp_parser_decltype
1720   (cp_parser *);
1721
1722 /* Declarators [gram.dcl.decl] */
1723
1724 static tree cp_parser_init_declarator
1725   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1726 static cp_declarator *cp_parser_declarator
1727   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1728 static cp_declarator *cp_parser_direct_declarator
1729   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1730 static enum tree_code cp_parser_ptr_operator
1731   (cp_parser *, tree *, cp_cv_quals *);
1732 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1733   (cp_parser *);
1734 static tree cp_parser_declarator_id
1735   (cp_parser *, bool);
1736 static tree cp_parser_type_id
1737   (cp_parser *);
1738 static void cp_parser_type_specifier_seq
1739   (cp_parser *, bool, cp_decl_specifier_seq *);
1740 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1741   (cp_parser *);
1742 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1743   (cp_parser *, bool *);
1744 static cp_parameter_declarator *cp_parser_parameter_declaration
1745   (cp_parser *, bool, bool *);
1746 static tree cp_parser_default_argument 
1747   (cp_parser *, bool);
1748 static void cp_parser_function_body
1749   (cp_parser *);
1750 static tree cp_parser_initializer
1751   (cp_parser *, bool *, bool *);
1752 static tree cp_parser_initializer_clause
1753   (cp_parser *, bool *);
1754 static tree cp_parser_braced_list
1755   (cp_parser*, bool*);
1756 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1757   (cp_parser *, bool *);
1758
1759 static bool cp_parser_ctor_initializer_opt_and_function_body
1760   (cp_parser *);
1761
1762 /* Classes [gram.class] */
1763
1764 static tree cp_parser_class_name
1765   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1766 static tree cp_parser_class_specifier
1767   (cp_parser *);
1768 static tree cp_parser_class_head
1769   (cp_parser *, bool *, tree *, tree *);
1770 static enum tag_types cp_parser_class_key
1771   (cp_parser *);
1772 static void cp_parser_member_specification_opt
1773   (cp_parser *);
1774 static void cp_parser_member_declaration
1775   (cp_parser *);
1776 static tree cp_parser_pure_specifier
1777   (cp_parser *);
1778 static tree cp_parser_constant_initializer
1779   (cp_parser *);
1780
1781 /* Derived classes [gram.class.derived] */
1782
1783 static tree cp_parser_base_clause
1784   (cp_parser *);
1785 static tree cp_parser_base_specifier
1786   (cp_parser *);
1787
1788 /* Special member functions [gram.special] */
1789
1790 static tree cp_parser_conversion_function_id
1791   (cp_parser *);
1792 static tree cp_parser_conversion_type_id
1793   (cp_parser *);
1794 static cp_declarator *cp_parser_conversion_declarator_opt
1795   (cp_parser *);
1796 static bool cp_parser_ctor_initializer_opt
1797   (cp_parser *);
1798 static void cp_parser_mem_initializer_list
1799   (cp_parser *);
1800 static tree cp_parser_mem_initializer
1801   (cp_parser *);
1802 static tree cp_parser_mem_initializer_id
1803   (cp_parser *);
1804
1805 /* Overloading [gram.over] */
1806
1807 static tree cp_parser_operator_function_id
1808   (cp_parser *);
1809 static tree cp_parser_operator
1810   (cp_parser *);
1811
1812 /* Templates [gram.temp] */
1813
1814 static void cp_parser_template_declaration
1815   (cp_parser *, bool);
1816 static tree cp_parser_template_parameter_list
1817   (cp_parser *);
1818 static tree cp_parser_template_parameter
1819   (cp_parser *, bool *, bool *);
1820 static tree cp_parser_type_parameter
1821   (cp_parser *, bool *);
1822 static tree cp_parser_template_id
1823   (cp_parser *, bool, bool, bool);
1824 static tree cp_parser_template_name
1825   (cp_parser *, bool, bool, bool, bool *);
1826 static tree cp_parser_template_argument_list
1827   (cp_parser *);
1828 static tree cp_parser_template_argument
1829   (cp_parser *);
1830 static void cp_parser_explicit_instantiation
1831   (cp_parser *);
1832 static void cp_parser_explicit_specialization
1833   (cp_parser *);
1834
1835 /* Exception handling [gram.exception] */
1836
1837 static tree cp_parser_try_block
1838   (cp_parser *);
1839 static bool cp_parser_function_try_block
1840   (cp_parser *);
1841 static void cp_parser_handler_seq
1842   (cp_parser *);
1843 static void cp_parser_handler
1844   (cp_parser *);
1845 static tree cp_parser_exception_declaration
1846   (cp_parser *);
1847 static tree cp_parser_throw_expression
1848   (cp_parser *);
1849 static tree cp_parser_exception_specification_opt
1850   (cp_parser *);
1851 static tree cp_parser_type_id_list
1852   (cp_parser *);
1853
1854 /* GNU Extensions */
1855
1856 static tree cp_parser_asm_specification_opt
1857   (cp_parser *);
1858 static tree cp_parser_asm_operand_list
1859   (cp_parser *);
1860 static tree cp_parser_asm_clobber_list
1861   (cp_parser *);
1862 static tree cp_parser_attributes_opt
1863   (cp_parser *);
1864 static tree cp_parser_attribute_list
1865   (cp_parser *);
1866 static bool cp_parser_extension_opt
1867   (cp_parser *, int *);
1868 static void cp_parser_label_declaration
1869   (cp_parser *);
1870
1871 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1872 static bool cp_parser_pragma
1873   (cp_parser *, enum pragma_context);
1874
1875 /* Objective-C++ Productions */
1876
1877 static tree cp_parser_objc_message_receiver
1878   (cp_parser *);
1879 static tree cp_parser_objc_message_args
1880   (cp_parser *);
1881 static tree cp_parser_objc_message_expression
1882   (cp_parser *);
1883 static tree cp_parser_objc_encode_expression
1884   (cp_parser *);
1885 static tree cp_parser_objc_defs_expression
1886   (cp_parser *);
1887 static tree cp_parser_objc_protocol_expression
1888   (cp_parser *);
1889 static tree cp_parser_objc_selector_expression
1890   (cp_parser *);
1891 static tree cp_parser_objc_expression
1892   (cp_parser *);
1893 static bool cp_parser_objc_selector_p
1894   (enum cpp_ttype);
1895 static tree cp_parser_objc_selector
1896   (cp_parser *);
1897 static tree cp_parser_objc_protocol_refs_opt
1898   (cp_parser *);
1899 static void cp_parser_objc_declaration
1900   (cp_parser *);
1901 static tree cp_parser_objc_statement
1902   (cp_parser *);
1903
1904 /* Utility Routines */
1905
1906 static tree cp_parser_lookup_name
1907   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1908 static tree cp_parser_lookup_name_simple
1909   (cp_parser *, tree, location_t);
1910 static tree cp_parser_maybe_treat_template_as_class
1911   (tree, bool);
1912 static bool cp_parser_check_declarator_template_parameters
1913   (cp_parser *, cp_declarator *, location_t);
1914 static bool cp_parser_check_template_parameters
1915   (cp_parser *, unsigned, location_t);
1916 static tree cp_parser_simple_cast_expression
1917   (cp_parser *);
1918 static tree cp_parser_global_scope_opt
1919   (cp_parser *, bool);
1920 static bool cp_parser_constructor_declarator_p
1921   (cp_parser *, bool);
1922 static tree cp_parser_function_definition_from_specifiers_and_declarator
1923   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1924 static tree cp_parser_function_definition_after_declarator
1925   (cp_parser *, bool);
1926 static void cp_parser_template_declaration_after_export
1927   (cp_parser *, bool);
1928 static void cp_parser_perform_template_parameter_access_checks
1929   (VEC (deferred_access_check,gc)*);
1930 static tree cp_parser_single_declaration
1931   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1932 static tree cp_parser_functional_cast
1933   (cp_parser *, tree);
1934 static tree cp_parser_save_member_function_body
1935   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1936 static tree cp_parser_enclosed_template_argument_list
1937   (cp_parser *);
1938 static void cp_parser_save_default_args
1939   (cp_parser *, tree);
1940 static void cp_parser_late_parsing_for_member
1941   (cp_parser *, tree);
1942 static void cp_parser_late_parsing_default_args
1943   (cp_parser *, tree);
1944 static tree cp_parser_sizeof_operand
1945   (cp_parser *, enum rid);
1946 static tree cp_parser_trait_expr
1947   (cp_parser *, enum rid);
1948 static bool cp_parser_declares_only_class_p
1949   (cp_parser *);
1950 static void cp_parser_set_storage_class
1951   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1952 static void cp_parser_set_decl_spec_type
1953   (cp_decl_specifier_seq *, tree, location_t, bool);
1954 static bool cp_parser_friend_p
1955   (const cp_decl_specifier_seq *);
1956 static cp_token *cp_parser_require
1957   (cp_parser *, enum cpp_ttype, const char *);
1958 static cp_token *cp_parser_require_keyword
1959   (cp_parser *, enum rid, const char *);
1960 static bool cp_parser_token_starts_function_definition_p
1961   (cp_token *);
1962 static bool cp_parser_next_token_starts_class_definition_p
1963   (cp_parser *);
1964 static bool cp_parser_next_token_ends_template_argument_p
1965   (cp_parser *);
1966 static bool cp_parser_nth_token_starts_template_argument_list_p
1967   (cp_parser *, size_t);
1968 static enum tag_types cp_parser_token_is_class_key
1969   (cp_token *);
1970 static void cp_parser_check_class_key
1971   (enum tag_types, tree type);
1972 static void cp_parser_check_access_in_redeclaration
1973   (tree type, location_t location);
1974 static bool cp_parser_optional_template_keyword
1975   (cp_parser *);
1976 static void cp_parser_pre_parsed_nested_name_specifier
1977   (cp_parser *);
1978 static bool cp_parser_cache_group
1979   (cp_parser *, enum cpp_ttype, unsigned);
1980 static void cp_parser_parse_tentatively
1981   (cp_parser *);
1982 static void cp_parser_commit_to_tentative_parse
1983   (cp_parser *);
1984 static void cp_parser_abort_tentative_parse
1985   (cp_parser *);
1986 static bool cp_parser_parse_definitely
1987   (cp_parser *);
1988 static inline bool cp_parser_parsing_tentatively
1989   (cp_parser *);
1990 static bool cp_parser_uncommitted_to_tentative_parse_p
1991   (cp_parser *);
1992 static void cp_parser_error
1993   (cp_parser *, const char *);
1994 static void cp_parser_name_lookup_error
1995   (cp_parser *, tree, tree, const char *, location_t);
1996 static bool cp_parser_simulate_error
1997   (cp_parser *);
1998 static bool cp_parser_check_type_definition
1999   (cp_parser *);
2000 static void cp_parser_check_for_definition_in_return_type
2001   (cp_declarator *, tree, location_t type_location);
2002 static void cp_parser_check_for_invalid_template_id
2003   (cp_parser *, tree, location_t location);
2004 static bool cp_parser_non_integral_constant_expression
2005   (cp_parser *, const char *);
2006 static void cp_parser_diagnose_invalid_type_name
2007   (cp_parser *, tree, tree, location_t);
2008 static bool cp_parser_parse_and_diagnose_invalid_type_name
2009   (cp_parser *);
2010 static int cp_parser_skip_to_closing_parenthesis
2011   (cp_parser *, bool, bool, bool);
2012 static void cp_parser_skip_to_end_of_statement
2013   (cp_parser *);
2014 static void cp_parser_consume_semicolon_at_end_of_statement
2015   (cp_parser *);
2016 static void cp_parser_skip_to_end_of_block_or_statement
2017   (cp_parser *);
2018 static bool cp_parser_skip_to_closing_brace
2019   (cp_parser *);
2020 static void cp_parser_skip_to_end_of_template_parameter_list
2021   (cp_parser *);
2022 static void cp_parser_skip_to_pragma_eol
2023   (cp_parser*, cp_token *);
2024 static bool cp_parser_error_occurred
2025   (cp_parser *);
2026 static bool cp_parser_allow_gnu_extensions_p
2027   (cp_parser *);
2028 static bool cp_parser_is_string_literal
2029   (cp_token *);
2030 static bool cp_parser_is_keyword
2031   (cp_token *, enum rid);
2032 static tree cp_parser_make_typename_type
2033   (cp_parser *, tree, tree, location_t location);
2034 static cp_declarator * cp_parser_make_indirect_declarator
2035   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2036
2037 /* Returns nonzero if we are parsing tentatively.  */
2038
2039 static inline bool
2040 cp_parser_parsing_tentatively (cp_parser* parser)
2041 {
2042   return parser->context->next != NULL;
2043 }
2044
2045 /* Returns nonzero if TOKEN is a string literal.  */
2046
2047 static bool
2048 cp_parser_is_string_literal (cp_token* token)
2049 {
2050   return (token->type == CPP_STRING ||
2051           token->type == CPP_STRING16 ||
2052           token->type == CPP_STRING32 ||
2053           token->type == CPP_WSTRING);
2054 }
2055
2056 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2057
2058 static bool
2059 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2060 {
2061   return token->keyword == keyword;
2062 }
2063
2064 /* If not parsing tentatively, issue a diagnostic of the form
2065       FILE:LINE: MESSAGE before TOKEN
2066    where TOKEN is the next token in the input stream.  MESSAGE
2067    (specified by the caller) is usually of the form "expected
2068    OTHER-TOKEN".  */
2069
2070 static void
2071 cp_parser_error (cp_parser* parser, const char* message)
2072 {
2073   if (!cp_parser_simulate_error (parser))
2074     {
2075       cp_token *token = cp_lexer_peek_token (parser->lexer);
2076       /* This diagnostic makes more sense if it is tagged to the line
2077          of the token we just peeked at.  */
2078       cp_lexer_set_source_position_from_token (token);
2079
2080       if (token->type == CPP_PRAGMA)
2081         {
2082           error ("%H%<#pragma%> is not allowed here", &token->location);
2083           cp_parser_skip_to_pragma_eol (parser, token);
2084           return;
2085         }
2086
2087       c_parse_error (message,
2088                      /* Because c_parser_error does not understand
2089                         CPP_KEYWORD, keywords are treated like
2090                         identifiers.  */
2091                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2092                      token->u.value);
2093     }
2094 }
2095
2096 /* Issue an error about name-lookup failing.  NAME is the
2097    IDENTIFIER_NODE DECL is the result of
2098    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2099    the thing that we hoped to find.  */
2100
2101 static void
2102 cp_parser_name_lookup_error (cp_parser* parser,
2103                              tree name,
2104                              tree decl,
2105                              const char* desired,
2106                              location_t location)
2107 {
2108   /* If name lookup completely failed, tell the user that NAME was not
2109      declared.  */
2110   if (decl == error_mark_node)
2111     {
2112       if (parser->scope && parser->scope != global_namespace)
2113         error ("%H%<%E::%E%> has not been declared",
2114                &location, parser->scope, name);
2115       else if (parser->scope == global_namespace)
2116         error ("%H%<::%E%> has not been declared", &location, name);
2117       else if (parser->object_scope
2118                && !CLASS_TYPE_P (parser->object_scope))
2119         error ("%Hrequest for member %qE in non-class type %qT",
2120                &location, name, parser->object_scope);
2121       else if (parser->object_scope)
2122         error ("%H%<%T::%E%> has not been declared",
2123                &location, parser->object_scope, name);
2124       else
2125         error ("%H%qE has not been declared", &location, name);
2126     }
2127   else if (parser->scope && parser->scope != global_namespace)
2128     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2129   else if (parser->scope == global_namespace)
2130     error ("%H%<::%E%> %s", &location, name, desired);
2131   else
2132     error ("%H%qE %s", &location, name, desired);
2133 }
2134
2135 /* If we are parsing tentatively, remember that an error has occurred
2136    during this tentative parse.  Returns true if the error was
2137    simulated; false if a message should be issued by the caller.  */
2138
2139 static bool
2140 cp_parser_simulate_error (cp_parser* parser)
2141 {
2142   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2143     {
2144       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2145       return true;
2146     }
2147   return false;
2148 }
2149
2150 /* Check for repeated decl-specifiers.  */
2151
2152 static void
2153 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2154                            location_t location)
2155 {
2156   cp_decl_spec ds;
2157
2158   for (ds = ds_first; ds != ds_last; ++ds)
2159     {
2160       unsigned count = decl_specs->specs[(int)ds];
2161       if (count < 2)
2162         continue;
2163       /* The "long" specifier is a special case because of "long long".  */
2164       if (ds == ds_long)
2165         {
2166           if (count > 2)
2167             error ("%H%<long long long%> is too long for GCC", &location);
2168           else if (pedantic && !in_system_header && warn_long_long
2169                    && cxx_dialect == cxx98)
2170             pedwarn ("%HISO C++ 1998 does not support %<long long%>",
2171                      &location);
2172         }
2173       else if (count > 1)
2174         {
2175           static const char *const decl_spec_names[] = {
2176             "signed",
2177             "unsigned",
2178             "short",
2179             "long",
2180             "const",
2181             "volatile",
2182             "restrict",
2183             "inline",
2184             "virtual",
2185             "explicit",
2186             "friend",
2187             "typedef",
2188             "__complex",
2189             "__thread"
2190           };
2191           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2192         }
2193     }
2194 }
2195
2196 /* This function is called when a type is defined.  If type
2197    definitions are forbidden at this point, an error message is
2198    issued.  */
2199
2200 static bool
2201 cp_parser_check_type_definition (cp_parser* parser)
2202 {
2203   /* If types are forbidden here, issue a message.  */
2204   if (parser->type_definition_forbidden_message)
2205     {
2206       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2207          in the message need to be interpreted.  */
2208       error (parser->type_definition_forbidden_message);
2209       return false;
2210     }
2211   return true;
2212 }
2213
2214 /* This function is called when the DECLARATOR is processed.  The TYPE
2215    was a type defined in the decl-specifiers.  If it is invalid to
2216    define a type in the decl-specifiers for DECLARATOR, an error is
2217    issued. TYPE_LOCATION is the location of TYPE and is used
2218    for error reporting.  */
2219
2220 static void
2221 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2222                                                tree type, location_t type_location)
2223 {
2224   /* [dcl.fct] forbids type definitions in return types.
2225      Unfortunately, it's not easy to know whether or not we are
2226      processing a return type until after the fact.  */
2227   while (declarator
2228          && (declarator->kind == cdk_pointer
2229              || declarator->kind == cdk_reference
2230              || declarator->kind == cdk_ptrmem))
2231     declarator = declarator->declarator;
2232   if (declarator
2233       && declarator->kind == cdk_function)
2234     {
2235       error ("%Hnew types may not be defined in a return type", &type_location);
2236       inform ("%H(perhaps a semicolon is missing after the definition of %qT)",
2237               &type_location, 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 ("%H(perhaps %<typename %T::%E%> was intended)",
2366                                 &location, BINFO_TYPE (b), id);
2367                         break;
2368                       }
2369                   if (field)
2370                     break;
2371                 }
2372             }
2373         }
2374     }
2375   /* Here we diagnose qualified-ids where the scope is actually correct,
2376      but the identifier does not resolve to a valid type name.  */
2377   else if (parser->scope != error_mark_node)
2378     {
2379       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2380         error ("%H%qE in namespace %qE does not name a type",
2381                &location, id, parser->scope);
2382       else if (TYPE_P (parser->scope))
2383         error ("%H%qE in class %qT does not name a type",
2384                &location, id, parser->scope);
2385       else
2386         gcc_unreachable ();
2387     }
2388   cp_parser_commit_to_tentative_parse (parser);
2389 }
2390
2391 /* Check for a common situation where a type-name should be present,
2392    but is not, and issue a sensible error message.  Returns true if an
2393    invalid type-name was detected.
2394
2395    The situation handled by this function are variable declarations of the
2396    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2397    Usually, `ID' should name a type, but if we got here it means that it
2398    does not. We try to emit the best possible error message depending on
2399    how exactly the id-expression looks like.  */
2400
2401 static bool
2402 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2403 {
2404   tree id;
2405   cp_token *token = cp_lexer_peek_token (parser->lexer);
2406
2407   cp_parser_parse_tentatively (parser);
2408   id = cp_parser_id_expression (parser,
2409                                 /*template_keyword_p=*/false,
2410                                 /*check_dependency_p=*/true,
2411                                 /*template_p=*/NULL,
2412                                 /*declarator_p=*/true,
2413                                 /*optional_p=*/false);
2414   /* After the id-expression, there should be a plain identifier,
2415      otherwise this is not a simple variable declaration. Also, if
2416      the scope is dependent, we cannot do much.  */
2417   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2418       || (parser->scope && TYPE_P (parser->scope)
2419           && dependent_type_p (parser->scope))
2420       || TREE_CODE (id) == TYPE_DECL)
2421     {
2422       cp_parser_abort_tentative_parse (parser);
2423       return false;
2424     }
2425   if (!cp_parser_parse_definitely (parser))
2426     return false;
2427
2428   /* Emit a diagnostic for the invalid type.  */
2429   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2430                                         id, token->location);
2431   /* Skip to the end of the declaration; there's no point in
2432      trying to process it.  */
2433   cp_parser_skip_to_end_of_block_or_statement (parser);
2434   return true;
2435 }
2436
2437 /* Consume tokens up to, and including, the next non-nested closing `)'.
2438    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2439    are doing error recovery. Returns -1 if OR_COMMA is true and we
2440    found an unnested comma.  */
2441
2442 static int
2443 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2444                                        bool recovering,
2445                                        bool or_comma,
2446                                        bool consume_paren)
2447 {
2448   unsigned paren_depth = 0;
2449   unsigned brace_depth = 0;
2450
2451   if (recovering && !or_comma
2452       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2453     return 0;
2454
2455   while (true)
2456     {
2457       cp_token * token = cp_lexer_peek_token (parser->lexer);
2458
2459       switch (token->type)
2460         {
2461         case CPP_EOF:
2462         case CPP_PRAGMA_EOL:
2463           /* If we've run out of tokens, then there is no closing `)'.  */
2464           return 0;
2465
2466         case CPP_SEMICOLON:
2467           /* This matches the processing in skip_to_end_of_statement.  */
2468           if (!brace_depth)
2469             return 0;
2470           break;
2471
2472         case CPP_OPEN_BRACE:
2473           ++brace_depth;
2474           break;
2475         case CPP_CLOSE_BRACE:
2476           if (!brace_depth--)
2477             return 0;
2478           break;
2479
2480         case CPP_COMMA:
2481           if (recovering && or_comma && !brace_depth && !paren_depth)
2482             return -1;
2483           break;
2484
2485         case CPP_OPEN_PAREN:
2486           if (!brace_depth)
2487             ++paren_depth;
2488           break;
2489
2490         case CPP_CLOSE_PAREN:
2491           if (!brace_depth && !paren_depth--)
2492             {
2493               if (consume_paren)
2494                 cp_lexer_consume_token (parser->lexer);
2495               return 1;
2496             }
2497           break;
2498
2499         default:
2500           break;
2501         }
2502
2503       /* Consume the token.  */
2504       cp_lexer_consume_token (parser->lexer);
2505     }
2506 }
2507
2508 /* Consume tokens until we reach the end of the current statement.
2509    Normally, that will be just before consuming a `;'.  However, if a
2510    non-nested `}' comes first, then we stop before consuming that.  */
2511
2512 static void
2513 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2514 {
2515   unsigned nesting_depth = 0;
2516
2517   while (true)
2518     {
2519       cp_token *token = cp_lexer_peek_token (parser->lexer);
2520
2521       switch (token->type)
2522         {
2523         case CPP_EOF:
2524         case CPP_PRAGMA_EOL:
2525           /* If we've run out of tokens, stop.  */
2526           return;
2527
2528         case CPP_SEMICOLON:
2529           /* If the next token is a `;', we have reached the end of the
2530              statement.  */
2531           if (!nesting_depth)
2532             return;
2533           break;
2534
2535         case CPP_CLOSE_BRACE:
2536           /* If this is a non-nested '}', stop before consuming it.
2537              That way, when confronted with something like:
2538
2539                { 3 + }
2540
2541              we stop before consuming the closing '}', even though we
2542              have not yet reached a `;'.  */
2543           if (nesting_depth == 0)
2544             return;
2545
2546           /* If it is the closing '}' for a block that we have
2547              scanned, stop -- but only after consuming the token.
2548              That way given:
2549
2550                 void f g () { ... }
2551                 typedef int I;
2552
2553              we will stop after the body of the erroneously declared
2554              function, but before consuming the following `typedef'
2555              declaration.  */
2556           if (--nesting_depth == 0)
2557             {
2558               cp_lexer_consume_token (parser->lexer);
2559               return;
2560             }
2561
2562         case CPP_OPEN_BRACE:
2563           ++nesting_depth;
2564           break;
2565
2566         default:
2567           break;
2568         }
2569
2570       /* Consume the token.  */
2571       cp_lexer_consume_token (parser->lexer);
2572     }
2573 }
2574
2575 /* This function is called at the end of a statement or declaration.
2576    If the next token is a semicolon, it is consumed; otherwise, error
2577    recovery is attempted.  */
2578
2579 static void
2580 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2581 {
2582   /* Look for the trailing `;'.  */
2583   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2584     {
2585       /* If there is additional (erroneous) input, skip to the end of
2586          the statement.  */
2587       cp_parser_skip_to_end_of_statement (parser);
2588       /* If the next token is now a `;', consume it.  */
2589       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2590         cp_lexer_consume_token (parser->lexer);
2591     }
2592 }
2593
2594 /* Skip tokens until we have consumed an entire block, or until we
2595    have consumed a non-nested `;'.  */
2596
2597 static void
2598 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2599 {
2600   int nesting_depth = 0;
2601
2602   while (nesting_depth >= 0)
2603     {
2604       cp_token *token = cp_lexer_peek_token (parser->lexer);
2605
2606       switch (token->type)
2607         {
2608         case CPP_EOF:
2609         case CPP_PRAGMA_EOL:
2610           /* If we've run out of tokens, stop.  */
2611           return;
2612
2613         case CPP_SEMICOLON:
2614           /* Stop if this is an unnested ';'. */
2615           if (!nesting_depth)
2616             nesting_depth = -1;
2617           break;
2618
2619         case CPP_CLOSE_BRACE:
2620           /* Stop if this is an unnested '}', or closes the outermost
2621              nesting level.  */
2622           nesting_depth--;
2623           if (!nesting_depth)
2624             nesting_depth = -1;
2625           break;
2626
2627         case CPP_OPEN_BRACE:
2628           /* Nest. */
2629           nesting_depth++;
2630           break;
2631
2632         default:
2633           break;
2634         }
2635
2636       /* Consume the token.  */
2637       cp_lexer_consume_token (parser->lexer);
2638     }
2639 }
2640
2641 /* Skip tokens until a non-nested closing curly brace is the next
2642    token, or there are no more tokens. Return true in the first case,
2643    false otherwise.  */
2644
2645 static bool
2646 cp_parser_skip_to_closing_brace (cp_parser *parser)
2647 {
2648   unsigned nesting_depth = 0;
2649
2650   while (true)
2651     {
2652       cp_token *token = cp_lexer_peek_token (parser->lexer);
2653
2654       switch (token->type)
2655         {
2656         case CPP_EOF:
2657         case CPP_PRAGMA_EOL:
2658           /* If we've run out of tokens, stop.  */
2659           return false;
2660
2661         case CPP_CLOSE_BRACE:
2662           /* If the next token is a non-nested `}', then we have reached
2663              the end of the current block.  */
2664           if (nesting_depth-- == 0)
2665             return true;
2666           break;
2667
2668         case CPP_OPEN_BRACE:
2669           /* If it the next token is a `{', then we are entering a new
2670              block.  Consume the entire block.  */
2671           ++nesting_depth;
2672           break;
2673
2674         default:
2675           break;
2676         }
2677
2678       /* Consume the token.  */
2679       cp_lexer_consume_token (parser->lexer);
2680     }
2681 }
2682
2683 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2684    parameter is the PRAGMA token, allowing us to purge the entire pragma
2685    sequence.  */
2686
2687 static void
2688 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2689 {
2690   cp_token *token;
2691
2692   parser->lexer->in_pragma = false;
2693
2694   do
2695     token = cp_lexer_consume_token (parser->lexer);
2696   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2697
2698   /* Ensure that the pragma is not parsed again.  */
2699   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2700 }
2701
2702 /* Require pragma end of line, resyncing with it as necessary.  The
2703    arguments are as for cp_parser_skip_to_pragma_eol.  */
2704
2705 static void
2706 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2707 {
2708   parser->lexer->in_pragma = false;
2709   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2710     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2711 }
2712
2713 /* This is a simple wrapper around make_typename_type. When the id is
2714    an unresolved identifier node, we can provide a superior diagnostic
2715    using cp_parser_diagnose_invalid_type_name.  */
2716
2717 static tree
2718 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2719                               tree id, location_t id_location)
2720 {
2721   tree result;
2722   if (TREE_CODE (id) == IDENTIFIER_NODE)
2723     {
2724       result = make_typename_type (scope, id, typename_type,
2725                                    /*complain=*/tf_none);
2726       if (result == error_mark_node)
2727         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2728       return result;
2729     }
2730   return make_typename_type (scope, id, typename_type, tf_error);
2731 }
2732
2733 /* This is a wrapper around the
2734    make_{pointer,ptrmem,reference}_declarator functions that decides
2735    which one to call based on the CODE and CLASS_TYPE arguments. The
2736    CODE argument should be one of the values returned by
2737    cp_parser_ptr_operator. */
2738 static cp_declarator *
2739 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2740                                     cp_cv_quals cv_qualifiers,
2741                                     cp_declarator *target)
2742 {
2743   if (code == ERROR_MARK)
2744     return cp_error_declarator;
2745
2746   if (code == INDIRECT_REF)
2747     if (class_type == NULL_TREE)
2748       return make_pointer_declarator (cv_qualifiers, target);
2749     else
2750       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2751   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2752     return make_reference_declarator (cv_qualifiers, target, false);
2753   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2754     return make_reference_declarator (cv_qualifiers, target, true);
2755   gcc_unreachable ();
2756 }
2757
2758 /* Create a new C++ parser.  */
2759
2760 static cp_parser *
2761 cp_parser_new (void)
2762 {
2763   cp_parser *parser;
2764   cp_lexer *lexer;
2765   unsigned i;
2766
2767   /* cp_lexer_new_main is called before calling ggc_alloc because
2768      cp_lexer_new_main might load a PCH file.  */
2769   lexer = cp_lexer_new_main ();
2770
2771   /* Initialize the binops_by_token so that we can get the tree
2772      directly from the token.  */
2773   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2774     binops_by_token[binops[i].token_type] = binops[i];
2775
2776   parser = GGC_CNEW (cp_parser);
2777   parser->lexer = lexer;
2778   parser->context = cp_parser_context_new (NULL);
2779
2780   /* For now, we always accept GNU extensions.  */
2781   parser->allow_gnu_extensions_p = 1;
2782
2783   /* The `>' token is a greater-than operator, not the end of a
2784      template-id.  */
2785   parser->greater_than_is_operator_p = true;
2786
2787   parser->default_arg_ok_p = true;
2788
2789   /* We are not parsing a constant-expression.  */
2790   parser->integral_constant_expression_p = false;
2791   parser->allow_non_integral_constant_expression_p = false;
2792   parser->non_integral_constant_expression_p = false;
2793
2794   /* Local variable names are not forbidden.  */
2795   parser->local_variables_forbidden_p = false;
2796
2797   /* We are not processing an `extern "C"' declaration.  */
2798   parser->in_unbraced_linkage_specification_p = false;
2799
2800   /* We are not processing a declarator.  */
2801   parser->in_declarator_p = false;
2802
2803   /* We are not processing a template-argument-list.  */
2804   parser->in_template_argument_list_p = false;
2805
2806   /* We are not in an iteration statement.  */
2807   parser->in_statement = 0;
2808
2809   /* We are not in a switch statement.  */
2810   parser->in_switch_statement_p = false;
2811
2812   /* We are not parsing a type-id inside an expression.  */
2813   parser->in_type_id_in_expr_p = false;
2814
2815   /* Declarations aren't implicitly extern "C".  */
2816   parser->implicit_extern_c = false;
2817
2818   /* String literals should be translated to the execution character set.  */
2819   parser->translate_strings_p = true;
2820
2821   /* We are not parsing a function body.  */
2822   parser->in_function_body = false;
2823
2824   /* The unparsed function queue is empty.  */
2825   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2826
2827   /* There are no classes being defined.  */
2828   parser->num_classes_being_defined = 0;
2829
2830   /* No template parameters apply.  */
2831   parser->num_template_parameter_lists = 0;
2832
2833   return parser;
2834 }
2835
2836 /* Create a cp_lexer structure which will emit the tokens in CACHE
2837    and push it onto the parser's lexer stack.  This is used for delayed
2838    parsing of in-class method bodies and default arguments, and should
2839    not be confused with tentative parsing.  */
2840 static void
2841 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2842 {
2843   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2844   lexer->next = parser->lexer;
2845   parser->lexer = lexer;
2846
2847   /* Move the current source position to that of the first token in the
2848      new lexer.  */
2849   cp_lexer_set_source_position_from_token (lexer->next_token);
2850 }
2851
2852 /* Pop the top lexer off the parser stack.  This is never used for the
2853    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2854 static void
2855 cp_parser_pop_lexer (cp_parser *parser)
2856 {
2857   cp_lexer *lexer = parser->lexer;
2858   parser->lexer = lexer->next;
2859   cp_lexer_destroy (lexer);
2860
2861   /* Put the current source position back where it was before this
2862      lexer was pushed.  */
2863   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2864 }
2865
2866 /* Lexical conventions [gram.lex]  */
2867
2868 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2869    identifier.  */
2870
2871 static tree
2872 cp_parser_identifier (cp_parser* parser)
2873 {
2874   cp_token *token;
2875
2876   /* Look for the identifier.  */
2877   token = cp_parser_require (parser, CPP_NAME, "identifier");
2878   /* Return the value.  */
2879   return token ? token->u.value : error_mark_node;
2880 }
2881
2882 /* Parse a sequence of adjacent string constants.  Returns a
2883    TREE_STRING representing the combined, nul-terminated string
2884    constant.  If TRANSLATE is true, translate the string to the
2885    execution character set.  If WIDE_OK is true, a wide string is
2886    invalid here.
2887
2888    C++98 [lex.string] says that if a narrow string literal token is
2889    adjacent to a wide string literal token, the behavior is undefined.
2890    However, C99 6.4.5p4 says that this results in a wide string literal.
2891    We follow C99 here, for consistency with the C front end.
2892
2893    This code is largely lifted from lex_string() in c-lex.c.
2894
2895    FUTURE: ObjC++ will need to handle @-strings here.  */
2896 static tree
2897 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2898 {
2899   tree value;
2900   size_t count;
2901   struct obstack str_ob;
2902   cpp_string str, istr, *strs;
2903   cp_token *tok;
2904   enum cpp_ttype type;
2905
2906   tok = cp_lexer_peek_token (parser->lexer);
2907   if (!cp_parser_is_string_literal (tok))
2908     {
2909       cp_parser_error (parser, "expected string-literal");
2910       return error_mark_node;
2911     }
2912
2913   type = tok->type;
2914
2915   /* Try to avoid the overhead of creating and destroying an obstack
2916      for the common case of just one string.  */
2917   if (!cp_parser_is_string_literal
2918       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2919     {
2920       cp_lexer_consume_token (parser->lexer);
2921
2922       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2923       str.len = TREE_STRING_LENGTH (tok->u.value);
2924       count = 1;
2925
2926       strs = &str;
2927     }
2928   else
2929     {
2930       gcc_obstack_init (&str_ob);
2931       count = 0;
2932
2933       do
2934         {
2935           cp_lexer_consume_token (parser->lexer);
2936           count++;
2937           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2938           str.len = TREE_STRING_LENGTH (tok->u.value);
2939
2940           if (type != tok->type)
2941             {
2942               if (type == CPP_STRING)
2943                 type = tok->type;
2944               else if (tok->type != CPP_STRING)
2945                 error ("%Hunsupported non-standard concatenation "
2946                        "of string literals", &tok->location);
2947             }
2948
2949           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2950
2951           tok = cp_lexer_peek_token (parser->lexer);
2952         }
2953       while (cp_parser_is_string_literal (tok));
2954
2955       strs = (cpp_string *) obstack_finish (&str_ob);
2956     }
2957
2958   if (type != CPP_STRING && !wide_ok)
2959     {
2960       cp_parser_error (parser, "a wide string is invalid in this context");
2961       type = CPP_STRING;
2962     }
2963
2964   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2965       (parse_in, strs, count, &istr, type))
2966     {
2967       value = build_string (istr.len, (const char *)istr.text);
2968       free (CONST_CAST (unsigned char *, istr.text));
2969
2970       switch (type)
2971         {
2972         default:
2973         case CPP_STRING:
2974           TREE_TYPE (value) = char_array_type_node;
2975           break;
2976         case CPP_STRING16:
2977           TREE_TYPE (value) = char16_array_type_node;
2978           break;
2979         case CPP_STRING32:
2980           TREE_TYPE (value) = char32_array_type_node;
2981           break;
2982         case CPP_WSTRING:
2983           TREE_TYPE (value) = wchar_array_type_node;
2984           break;
2985         }
2986
2987       value = fix_string_type (value);
2988     }
2989   else
2990     /* cpp_interpret_string has issued an error.  */
2991     value = error_mark_node;
2992
2993   if (count > 1)
2994     obstack_free (&str_ob, 0);
2995
2996   return value;
2997 }
2998
2999
3000 /* Basic concepts [gram.basic]  */
3001
3002 /* Parse a translation-unit.
3003
3004    translation-unit:
3005      declaration-seq [opt]
3006
3007    Returns TRUE if all went well.  */
3008
3009 static bool
3010 cp_parser_translation_unit (cp_parser* parser)
3011 {
3012   /* The address of the first non-permanent object on the declarator
3013      obstack.  */
3014   static void *declarator_obstack_base;
3015
3016   bool success;
3017
3018   /* Create the declarator obstack, if necessary.  */
3019   if (!cp_error_declarator)
3020     {
3021       gcc_obstack_init (&declarator_obstack);
3022       /* Create the error declarator.  */
3023       cp_error_declarator = make_declarator (cdk_error);
3024       /* Create the empty parameter list.  */
3025       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3026       /* Remember where the base of the declarator obstack lies.  */
3027       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3028     }
3029
3030   cp_parser_declaration_seq_opt (parser);
3031
3032   /* If there are no tokens left then all went well.  */
3033   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3034     {
3035       /* Get rid of the token array; we don't need it any more.  */
3036       cp_lexer_destroy (parser->lexer);
3037       parser->lexer = NULL;
3038
3039       /* This file might have been a context that's implicitly extern
3040          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3041       if (parser->implicit_extern_c)
3042         {
3043           pop_lang_context ();
3044           parser->implicit_extern_c = false;
3045         }
3046
3047       /* Finish up.  */
3048       finish_translation_unit ();
3049
3050       success = true;
3051     }
3052   else
3053     {
3054       cp_parser_error (parser, "expected declaration");
3055       success = false;
3056     }
3057
3058   /* Make sure the declarator obstack was fully cleaned up.  */
3059   gcc_assert (obstack_next_free (&declarator_obstack)
3060               == declarator_obstack_base);
3061
3062   /* All went well.  */
3063   return success;
3064 }
3065
3066 /* Expressions [gram.expr] */
3067
3068 /* Parse a primary-expression.
3069
3070    primary-expression:
3071      literal
3072      this
3073      ( expression )
3074      id-expression
3075
3076    GNU Extensions:
3077
3078    primary-expression:
3079      ( compound-statement )
3080      __builtin_va_arg ( assignment-expression , type-id )
3081      __builtin_offsetof ( type-id , offsetof-expression )
3082
3083    C++ Extensions:
3084      __has_nothrow_assign ( type-id )   
3085      __has_nothrow_constructor ( type-id )
3086      __has_nothrow_copy ( type-id )
3087      __has_trivial_assign ( type-id )   
3088      __has_trivial_constructor ( type-id )
3089      __has_trivial_copy ( type-id )
3090      __has_trivial_destructor ( type-id )
3091      __has_virtual_destructor ( type-id )     
3092      __is_abstract ( type-id )
3093      __is_base_of ( type-id , type-id )
3094      __is_class ( type-id )
3095      __is_convertible_to ( type-id , type-id )     
3096      __is_empty ( type-id )
3097      __is_enum ( type-id )
3098      __is_pod ( type-id )
3099      __is_polymorphic ( type-id )
3100      __is_union ( type-id )
3101
3102    Objective-C++ Extension:
3103
3104    primary-expression:
3105      objc-expression
3106
3107    literal:
3108      __null
3109
3110    ADDRESS_P is true iff this expression was immediately preceded by
3111    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3112    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3113    true iff this expression is a template argument.
3114
3115    Returns a representation of the expression.  Upon return, *IDK
3116    indicates what kind of id-expression (if any) was present.  */
3117
3118 static tree
3119 cp_parser_primary_expression (cp_parser *parser,
3120                               bool address_p,
3121                               bool cast_p,
3122                               bool template_arg_p,
3123                               cp_id_kind *idk)
3124 {
3125   cp_token *token = NULL;
3126
3127   /* Assume the primary expression is not an id-expression.  */
3128   *idk = CP_ID_KIND_NONE;
3129
3130   /* Peek at the next token.  */
3131   token = cp_lexer_peek_token (parser->lexer);
3132   switch (token->type)
3133     {
3134       /* literal:
3135            integer-literal
3136            character-literal
3137            floating-literal
3138            string-literal
3139            boolean-literal  */
3140     case CPP_CHAR:
3141     case CPP_CHAR16:
3142     case CPP_CHAR32:
3143     case CPP_WCHAR:
3144     case CPP_NUMBER:
3145       token = cp_lexer_consume_token (parser->lexer);
3146       /* Floating-point literals are only allowed in an integral
3147          constant expression if they are cast to an integral or
3148          enumeration type.  */
3149       if (TREE_CODE (token->u.value) == REAL_CST
3150           && parser->integral_constant_expression_p
3151           && pedantic)
3152         {
3153           /* CAST_P will be set even in invalid code like "int(2.7 +
3154              ...)".   Therefore, we have to check that the next token
3155              is sure to end the cast.  */
3156           if (cast_p)
3157             {
3158               cp_token *next_token;
3159
3160               next_token = cp_lexer_peek_token (parser->lexer);
3161               if (/* The comma at the end of an
3162                      enumerator-definition.  */
3163                   next_token->type != CPP_COMMA
3164                   /* The curly brace at the end of an enum-specifier.  */
3165                   && next_token->type != CPP_CLOSE_BRACE
3166                   /* The end of a statement.  */
3167                   && next_token->type != CPP_SEMICOLON
3168                   /* The end of the cast-expression.  */
3169                   && next_token->type != CPP_CLOSE_PAREN
3170                   /* The end of an array bound.  */
3171                   && next_token->type != CPP_CLOSE_SQUARE
3172                   /* The closing ">" in a template-argument-list.  */
3173                   && (next_token->type != CPP_GREATER
3174                       || parser->greater_than_is_operator_p)
3175                   /* C++0x only: A ">>" treated like two ">" tokens,
3176                      in a template-argument-list.  */
3177                   && (next_token->type != CPP_RSHIFT
3178                       || (cxx_dialect == cxx98)
3179                       || parser->greater_than_is_operator_p))
3180                 cast_p = false;
3181             }
3182
3183           /* If we are within a cast, then the constraint that the
3184              cast is to an integral or enumeration type will be
3185              checked at that point.  If we are not within a cast, then
3186              this code is invalid.  */
3187           if (!cast_p)
3188             cp_parser_non_integral_constant_expression
3189               (parser, "floating-point literal");
3190         }
3191       return token->u.value;
3192
3193     case CPP_STRING:
3194     case CPP_STRING16:
3195     case CPP_STRING32:
3196     case CPP_WSTRING:
3197       /* ??? Should wide strings be allowed when parser->translate_strings_p
3198          is false (i.e. in attributes)?  If not, we can kill the third
3199          argument to cp_parser_string_literal.  */
3200       return cp_parser_string_literal (parser,
3201                                        parser->translate_strings_p,
3202                                        true);
3203
3204     case CPP_OPEN_PAREN:
3205       {
3206         tree expr;
3207         bool saved_greater_than_is_operator_p;
3208
3209         /* Consume the `('.  */
3210         cp_lexer_consume_token (parser->lexer);
3211         /* Within a parenthesized expression, a `>' token is always
3212            the greater-than operator.  */
3213         saved_greater_than_is_operator_p
3214           = parser->greater_than_is_operator_p;
3215         parser->greater_than_is_operator_p = true;
3216         /* If we see `( { ' then we are looking at the beginning of
3217            a GNU statement-expression.  */
3218         if (cp_parser_allow_gnu_extensions_p (parser)
3219             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3220           {
3221             /* Statement-expressions are not allowed by the standard.  */
3222             if (pedantic)
3223               pedwarn ("%HISO C++ forbids braced-groups within expressions",
3224                        &token->location);
3225
3226             /* And they're not allowed outside of a function-body; you
3227                cannot, for example, write:
3228
3229                  int i = ({ int j = 3; j + 1; });
3230
3231                at class or namespace scope.  */
3232             if (!parser->in_function_body
3233                 || parser->in_template_argument_list_p)
3234               {
3235                 error ("%Hstatement-expressions are not allowed outside "
3236                        "functions nor in template-argument lists",
3237                        &token->location);
3238                 cp_parser_skip_to_end_of_block_or_statement (parser);
3239                 expr = error_mark_node;
3240               }
3241             else
3242               {
3243                 /* Start the statement-expression.  */
3244                 expr = begin_stmt_expr ();
3245                 /* Parse the compound-statement.  */
3246                 cp_parser_compound_statement (parser, expr, false);
3247                 /* Finish up.  */
3248                 expr = finish_stmt_expr (expr, false);
3249               }
3250           }
3251         else
3252           {
3253             /* Parse the parenthesized expression.  */
3254             expr = cp_parser_expression (parser, cast_p);
3255             /* Let the front end know that this expression was
3256                enclosed in parentheses. This matters in case, for
3257                example, the expression is of the form `A::B', since
3258                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3259                not.  */
3260             finish_parenthesized_expr (expr);
3261           }
3262         /* The `>' token might be the end of a template-id or
3263            template-parameter-list now.  */
3264         parser->greater_than_is_operator_p
3265           = saved_greater_than_is_operator_p;
3266         /* Consume the `)'.  */
3267         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3268           cp_parser_skip_to_end_of_statement (parser);
3269
3270         return expr;
3271       }
3272
3273     case CPP_KEYWORD:
3274       switch (token->keyword)
3275         {
3276           /* These two are the boolean literals.  */
3277         case RID_TRUE:
3278           cp_lexer_consume_token (parser->lexer);
3279           return boolean_true_node;
3280         case RID_FALSE:
3281           cp_lexer_consume_token (parser->lexer);
3282           return boolean_false_node;
3283
3284           /* The `__null' literal.  */
3285         case RID_NULL:
3286           cp_lexer_consume_token (parser->lexer);
3287           return null_node;
3288
3289           /* Recognize the `this' keyword.  */
3290         case RID_THIS:
3291           cp_lexer_consume_token (parser->lexer);
3292           if (parser->local_variables_forbidden_p)
3293             {
3294               error ("%H%<this%> may not be used in this context",
3295                      &token->location);
3296               return error_mark_node;
3297             }
3298           /* Pointers cannot appear in constant-expressions.  */
3299           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3300             return error_mark_node;
3301           return finish_this_expr ();
3302
3303           /* The `operator' keyword can be the beginning of an
3304              id-expression.  */
3305         case RID_OPERATOR:
3306           goto id_expression;
3307
3308         case RID_FUNCTION_NAME:
3309         case RID_PRETTY_FUNCTION_NAME:
3310         case RID_C99_FUNCTION_NAME:
3311           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3312              __func__ are the names of variables -- but they are
3313              treated specially.  Therefore, they are handled here,
3314              rather than relying on the generic id-expression logic
3315              below.  Grammatically, these names are id-expressions.
3316
3317              Consume the token.  */
3318           token = cp_lexer_consume_token (parser->lexer);
3319           /* Look up the name.  */
3320           return finish_fname (token->u.value);
3321
3322         case RID_VA_ARG:
3323           {
3324             tree expression;
3325             tree type;
3326
3327             /* The `__builtin_va_arg' construct is used to handle
3328                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3329             cp_lexer_consume_token (parser->lexer);
3330             /* Look for the opening `('.  */
3331             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3332             /* Now, parse the assignment-expression.  */
3333             expression = cp_parser_assignment_expression (parser,
3334                                                           /*cast_p=*/false);
3335             /* Look for the `,'.  */
3336             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3337             /* Parse the type-id.  */
3338             type = cp_parser_type_id (parser);
3339             /* Look for the closing `)'.  */
3340             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3341             /* Using `va_arg' in a constant-expression is not
3342                allowed.  */
3343             if (cp_parser_non_integral_constant_expression (parser,
3344                                                             "%<va_arg%>"))
3345               return error_mark_node;
3346             return build_x_va_arg (expression, type);
3347           }
3348
3349         case RID_OFFSETOF:
3350           return cp_parser_builtin_offsetof (parser);
3351
3352         case RID_HAS_NOTHROW_ASSIGN:
3353         case RID_HAS_NOTHROW_CONSTRUCTOR:
3354         case RID_HAS_NOTHROW_COPY:        
3355         case RID_HAS_TRIVIAL_ASSIGN:
3356         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3357         case RID_HAS_TRIVIAL_COPY:        
3358         case RID_HAS_TRIVIAL_DESTRUCTOR:
3359         case RID_HAS_VIRTUAL_DESTRUCTOR:
3360         case RID_IS_ABSTRACT:
3361         case RID_IS_BASE_OF:
3362         case RID_IS_CLASS:
3363         case RID_IS_CONVERTIBLE_TO:
3364         case RID_IS_EMPTY:
3365         case RID_IS_ENUM:
3366         case RID_IS_POD:
3367         case RID_IS_POLYMORPHIC:
3368         case RID_IS_UNION:
3369           return cp_parser_trait_expr (parser, token->keyword);
3370
3371         /* Objective-C++ expressions.  */
3372         case RID_AT_ENCODE:
3373         case RID_AT_PROTOCOL:
3374         case RID_AT_SELECTOR:
3375           return cp_parser_objc_expression (parser);
3376
3377         default:
3378           cp_parser_error (parser, "expected primary-expression");
3379           return error_mark_node;
3380         }
3381
3382       /* An id-expression can start with either an identifier, a
3383          `::' as the beginning of a qualified-id, or the "operator"
3384          keyword.  */
3385     case CPP_NAME:
3386     case CPP_SCOPE:
3387     case CPP_TEMPLATE_ID:
3388     case CPP_NESTED_NAME_SPECIFIER:
3389       {
3390         tree id_expression;
3391         tree decl;
3392         const char *error_msg;
3393         bool template_p;
3394         bool done;
3395         cp_token *id_expr_token;
3396
3397       id_expression:
3398         /* Parse the id-expression.  */
3399         id_expression
3400           = cp_parser_id_expression (parser,
3401                                      /*template_keyword_p=*/false,
3402                                      /*check_dependency_p=*/true,
3403                                      &template_p,
3404                                      /*declarator_p=*/false,
3405                                      /*optional_p=*/false);
3406         if (id_expression == error_mark_node)
3407           return error_mark_node;
3408         id_expr_token = token;
3409         token = cp_lexer_peek_token (parser->lexer);
3410         done = (token->type != CPP_OPEN_SQUARE
3411                 && token->type != CPP_OPEN_PAREN
3412                 && token->type != CPP_DOT
3413                 && token->type != CPP_DEREF
3414                 && token->type != CPP_PLUS_PLUS
3415                 && token->type != CPP_MINUS_MINUS);
3416         /* If we have a template-id, then no further lookup is
3417            required.  If the template-id was for a template-class, we
3418            will sometimes have a TYPE_DECL at this point.  */
3419         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3420                  || TREE_CODE (id_expression) == TYPE_DECL)
3421           decl = id_expression;
3422         /* Look up the name.  */
3423         else
3424           {
3425             tree ambiguous_decls;
3426
3427             decl = cp_parser_lookup_name (parser, id_expression,
3428                                           none_type,
3429                                           template_p,
3430                                           /*is_namespace=*/false,
3431                                           /*check_dependency=*/true,
3432                                           &ambiguous_decls,
3433                                           id_expr_token->location);
3434             /* If the lookup was ambiguous, an error will already have
3435                been issued.  */
3436             if (ambiguous_decls)
3437               return error_mark_node;
3438
3439             /* In Objective-C++, an instance variable (ivar) may be preferred
3440                to whatever cp_parser_lookup_name() found.  */
3441             decl = objc_lookup_ivar (decl, id_expression);
3442
3443             /* If name lookup gives us a SCOPE_REF, then the
3444                qualifying scope was dependent.  */
3445             if (TREE_CODE (decl) == SCOPE_REF)
3446               {
3447                 /* At this point, we do not know if DECL is a valid
3448                    integral constant expression.  We assume that it is
3449                    in fact such an expression, so that code like:
3450
3451                       template <int N> struct A {
3452                         int a[B<N>::i];
3453                       };
3454                      
3455                    is accepted.  At template-instantiation time, we
3456                    will check that B<N>::i is actually a constant.  */
3457                 return decl;
3458               }
3459             /* Check to see if DECL is a local variable in a context
3460                where that is forbidden.  */
3461             if (parser->local_variables_forbidden_p
3462                 && local_variable_p (decl))
3463               {
3464                 /* It might be that we only found DECL because we are
3465                    trying to be generous with pre-ISO scoping rules.
3466                    For example, consider:
3467
3468                      int i;
3469                      void g() {
3470                        for (int i = 0; i < 10; ++i) {}
3471                        extern void f(int j = i);
3472                      }
3473
3474                    Here, name look up will originally find the out
3475                    of scope `i'.  We need to issue a warning message,
3476                    but then use the global `i'.  */
3477                 decl = check_for_out_of_scope_variable (decl);
3478                 if (local_variable_p (decl))
3479                   {
3480                     error ("%Hlocal variable %qD may not appear in this context",
3481                            &id_expr_token->location, decl);
3482                     return error_mark_node;
3483                   }
3484               }
3485           }
3486
3487         decl = (finish_id_expression
3488                 (id_expression, decl, parser->scope,
3489                  idk,
3490                  parser->integral_constant_expression_p,
3491                  parser->allow_non_integral_constant_expression_p,
3492                  &parser->non_integral_constant_expression_p,
3493                  template_p, done, address_p,
3494                  template_arg_p,
3495                  &error_msg,
3496                  id_expr_token->location));
3497         if (error_msg)
3498           cp_parser_error (parser, error_msg);
3499         return decl;
3500       }
3501
3502       /* Anything else is an error.  */
3503     default:
3504       /* ...unless we have an Objective-C++ message or string literal,
3505          that is.  */
3506       if (c_dialect_objc ()
3507           && (token->type == CPP_OPEN_SQUARE
3508               || token->type == CPP_OBJC_STRING))
3509         return cp_parser_objc_expression (parser);
3510
3511       cp_parser_error (parser, "expected primary-expression");
3512       return error_mark_node;
3513     }
3514 }
3515
3516 /* Parse an id-expression.
3517
3518    id-expression:
3519      unqualified-id
3520      qualified-id
3521
3522    qualified-id:
3523      :: [opt] nested-name-specifier template [opt] unqualified-id
3524      :: identifier
3525      :: operator-function-id
3526      :: template-id
3527
3528    Return a representation of the unqualified portion of the
3529    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3530    a `::' or nested-name-specifier.
3531
3532    Often, if the id-expression was a qualified-id, the caller will
3533    want to make a SCOPE_REF to represent the qualified-id.  This
3534    function does not do this in order to avoid wastefully creating
3535    SCOPE_REFs when they are not required.
3536
3537    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3538    `template' keyword.
3539
3540    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3541    uninstantiated templates.
3542
3543    If *TEMPLATE_P is non-NULL, it is set to true iff the
3544    `template' keyword is used to explicitly indicate that the entity
3545    named is a template.
3546
3547    If DECLARATOR_P is true, the id-expression is appearing as part of
3548    a declarator, rather than as part of an expression.  */
3549
3550 static tree
3551 cp_parser_id_expression (cp_parser *parser,
3552                          bool template_keyword_p,
3553                          bool check_dependency_p,
3554                          bool *template_p,
3555                          bool declarator_p,
3556                          bool optional_p)
3557 {
3558   bool global_scope_p;
3559   bool nested_name_specifier_p;
3560
3561   /* Assume the `template' keyword was not used.  */
3562   if (template_p)
3563     *template_p = template_keyword_p;
3564
3565   /* Look for the optional `::' operator.  */
3566   global_scope_p
3567     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3568        != NULL_TREE);
3569   /* Look for the optional nested-name-specifier.  */
3570   nested_name_specifier_p
3571     = (cp_parser_nested_name_specifier_opt (parser,
3572                                             /*typename_keyword_p=*/false,
3573                                             check_dependency_p,
3574                                             /*type_p=*/false,
3575                                             declarator_p)
3576        != NULL_TREE);
3577   /* If there is a nested-name-specifier, then we are looking at
3578      the first qualified-id production.  */
3579   if (nested_name_specifier_p)
3580     {
3581       tree saved_scope;
3582       tree saved_object_scope;
3583       tree saved_qualifying_scope;
3584       tree unqualified_id;
3585       bool is_template;
3586
3587       /* See if the next token is the `template' keyword.  */
3588       if (!template_p)
3589         template_p = &is_template;
3590       *template_p = cp_parser_optional_template_keyword (parser);
3591       /* Name lookup we do during the processing of the
3592          unqualified-id might obliterate SCOPE.  */
3593       saved_scope = parser->scope;
3594       saved_object_scope = parser->object_scope;
3595       saved_qualifying_scope = parser->qualifying_scope;
3596       /* Process the final unqualified-id.  */
3597       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3598                                                  check_dependency_p,
3599                                                  declarator_p,
3600                                                  /*optional_p=*/false);
3601       /* Restore the SAVED_SCOPE for our caller.  */
3602       parser->scope = saved_scope;
3603       parser->object_scope = saved_object_scope;
3604       parser->qualifying_scope = saved_qualifying_scope;
3605
3606       return unqualified_id;
3607     }
3608   /* Otherwise, if we are in global scope, then we are looking at one
3609      of the other qualified-id productions.  */
3610   else if (global_scope_p)
3611     {
3612       cp_token *token;
3613       tree id;
3614
3615       /* Peek at the next token.  */
3616       token = cp_lexer_peek_token (parser->lexer);
3617
3618       /* If it's an identifier, and the next token is not a "<", then
3619          we can avoid the template-id case.  This is an optimization
3620          for this common case.  */
3621       if (token->type == CPP_NAME
3622           && !cp_parser_nth_token_starts_template_argument_list_p
3623                (parser, 2))
3624         return cp_parser_identifier (parser);
3625
3626       cp_parser_parse_tentatively (parser);
3627       /* Try a template-id.  */
3628       id = cp_parser_template_id (parser,
3629                                   /*template_keyword_p=*/false,
3630                                   /*check_dependency_p=*/true,
3631                                   declarator_p);
3632       /* If that worked, we're done.  */
3633       if (cp_parser_parse_definitely (parser))
3634         return id;
3635
3636       /* Peek at the next token.  (Changes in the token buffer may
3637          have invalidated the pointer obtained above.)  */
3638       token = cp_lexer_peek_token (parser->lexer);
3639
3640       switch (token->type)
3641         {
3642         case CPP_NAME:
3643           return cp_parser_identifier (parser);
3644
3645         case CPP_KEYWORD:
3646           if (token->keyword == RID_OPERATOR)
3647             return cp_parser_operator_function_id (parser);
3648           /* Fall through.  */
3649
3650         default:
3651           cp_parser_error (parser, "expected id-expression");
3652           return error_mark_node;
3653         }
3654     }
3655   else
3656     return cp_parser_unqualified_id (parser, template_keyword_p,
3657                                      /*check_dependency_p=*/true,
3658                                      declarator_p,
3659                                      optional_p);
3660 }
3661
3662 /* Parse an unqualified-id.
3663
3664    unqualified-id:
3665      identifier
3666      operator-function-id
3667      conversion-function-id
3668      ~ class-name
3669      template-id
3670
3671    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3672    keyword, in a construct like `A::template ...'.
3673
3674    Returns a representation of unqualified-id.  For the `identifier'
3675    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3676    production a BIT_NOT_EXPR is returned; the operand of the
3677    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3678    other productions, see the documentation accompanying the
3679    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3680    names are looked up in uninstantiated templates.  If DECLARATOR_P
3681    is true, the unqualified-id is appearing as part of a declarator,
3682    rather than as part of an expression.  */
3683
3684 static tree
3685 cp_parser_unqualified_id (cp_parser* parser,
3686                           bool template_keyword_p,
3687                           bool check_dependency_p,
3688                           bool declarator_p,
3689                           bool optional_p)
3690 {
3691   cp_token *token;
3692
3693   /* Peek at the next token.  */
3694   token = cp_lexer_peek_token (parser->lexer);
3695
3696   switch (token->type)
3697     {
3698     case CPP_NAME:
3699       {
3700         tree id;
3701
3702         /* We don't know yet whether or not this will be a
3703            template-id.  */
3704         cp_parser_parse_tentatively (parser);
3705         /* Try a template-id.  */
3706         id = cp_parser_template_id (parser, template_keyword_p,
3707                                     check_dependency_p,
3708                                     declarator_p);
3709         /* If it worked, we're done.  */
3710         if (cp_parser_parse_definitely (parser))
3711           return id;
3712         /* Otherwise, it's an ordinary identifier.  */
3713         return cp_parser_identifier (parser);
3714       }
3715
3716     case CPP_TEMPLATE_ID:
3717       return cp_parser_template_id (parser, template_keyword_p,
3718                                     check_dependency_p,
3719                                     declarator_p);
3720
3721     case CPP_COMPL:
3722       {
3723         tree type_decl;
3724         tree qualifying_scope;
3725         tree object_scope;
3726         tree scope;
3727         bool done;
3728
3729         /* Consume the `~' token.  */
3730         cp_lexer_consume_token (parser->lexer);
3731         /* Parse the class-name.  The standard, as written, seems to
3732            say that:
3733
3734              template <typename T> struct S { ~S (); };
3735              template <typename T> S<T>::~S() {}
3736
3737            is invalid, since `~' must be followed by a class-name, but
3738            `S<T>' is dependent, and so not known to be a class.
3739            That's not right; we need to look in uninstantiated
3740            templates.  A further complication arises from:
3741
3742              template <typename T> void f(T t) {
3743                t.T::~T();
3744              }
3745
3746            Here, it is not possible to look up `T' in the scope of `T'
3747            itself.  We must look in both the current scope, and the
3748            scope of the containing complete expression.
3749
3750            Yet another issue is:
3751
3752              struct S {
3753                int S;
3754                ~S();
3755              };
3756
3757              S::~S() {}
3758
3759            The standard does not seem to say that the `S' in `~S'
3760            should refer to the type `S' and not the data member
3761            `S::S'.  */
3762
3763         /* DR 244 says that we look up the name after the "~" in the
3764            same scope as we looked up the qualifying name.  That idea
3765            isn't fully worked out; it's more complicated than that.  */
3766         scope = parser->scope;
3767         object_scope = parser->object_scope;
3768         qualifying_scope = parser->qualifying_scope;
3769
3770         /* Check for invalid scopes.  */
3771         if (scope == error_mark_node)
3772           {
3773             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3774               cp_lexer_consume_token (parser->lexer);
3775             return error_mark_node;
3776           }
3777         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3778           {
3779             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3780               error ("%Hscope %qT before %<~%> is not a class-name",
3781                      &token->location, scope);
3782             cp_parser_simulate_error (parser);
3783             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3784               cp_lexer_consume_token (parser->lexer);
3785             return error_mark_node;
3786           }
3787         gcc_assert (!scope || TYPE_P (scope));
3788
3789         /* If the name is of the form "X::~X" it's OK.  */
3790         token = cp_lexer_peek_token (parser->lexer);
3791         if (scope
3792             && token->type == CPP_NAME
3793             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3794                 == CPP_OPEN_PAREN)
3795             && constructor_name_p (token->u.value, scope))
3796           {
3797             cp_lexer_consume_token (parser->lexer);
3798             return build_nt (BIT_NOT_EXPR, scope);
3799           }
3800
3801         /* If there was an explicit qualification (S::~T), first look
3802            in the scope given by the qualification (i.e., S).  */
3803         done = false;
3804         type_decl = NULL_TREE;
3805         if (scope)
3806           {
3807             cp_parser_parse_tentatively (parser);
3808             type_decl = cp_parser_class_name (parser,
3809                                               /*typename_keyword_p=*/false,
3810                                               /*template_keyword_p=*/false,
3811                                               none_type,
3812                                               /*check_dependency=*/false,
3813                                               /*class_head_p=*/false,
3814                                               declarator_p);
3815             if (cp_parser_parse_definitely (parser))
3816               done = true;
3817           }
3818         /* In "N::S::~S", look in "N" as well.  */
3819         if (!done && scope && qualifying_scope)
3820           {
3821             cp_parser_parse_tentatively (parser);
3822             parser->scope = qualifying_scope;
3823             parser->object_scope = NULL_TREE;
3824             parser->qualifying_scope = NULL_TREE;
3825             type_decl
3826               = cp_parser_class_name (parser,
3827                                       /*typename_keyword_p=*/false,
3828                                       /*template_keyword_p=*/false,
3829                                       none_type,
3830                                       /*check_dependency=*/false,
3831                                       /*class_head_p=*/false,
3832                                       declarator_p);
3833             if (cp_parser_parse_definitely (parser))
3834               done = true;
3835           }
3836         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3837         else if (!done && object_scope)
3838           {
3839             cp_parser_parse_tentatively (parser);
3840             parser->scope = object_scope;
3841             parser->object_scope = NULL_TREE;
3842             parser->qualifying_scope = NULL_TREE;
3843             type_decl
3844               = cp_parser_class_name (parser,
3845                                       /*typename_keyword_p=*/false,
3846                                       /*template_keyword_p=*/false,
3847                                       none_type,
3848                                       /*check_dependency=*/false,
3849                                       /*class_head_p=*/false,
3850                                       declarator_p);
3851             if (cp_parser_parse_definitely (parser))
3852               done = true;
3853           }
3854         /* Look in the surrounding context.  */
3855         if (!done)
3856           {
3857             parser->scope = NULL_TREE;
3858             parser->object_scope = NULL_TREE;
3859             parser->qualifying_scope = NULL_TREE;
3860             type_decl
3861               = cp_parser_class_name (parser,
3862                                       /*typename_keyword_p=*/false,
3863                                       /*template_keyword_p=*/false,
3864                                       none_type,
3865                                       /*check_dependency=*/false,
3866                                       /*class_head_p=*/false,
3867                                       declarator_p);
3868           }
3869         /* If an error occurred, assume that the name of the
3870            destructor is the same as the name of the qualifying
3871            class.  That allows us to keep parsing after running
3872            into ill-formed destructor names.  */
3873         if (type_decl == error_mark_node && scope)
3874           return build_nt (BIT_NOT_EXPR, scope);
3875         else if (type_decl == error_mark_node)
3876           return error_mark_node;
3877
3878         /* Check that destructor name and scope match.  */
3879         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3880           {
3881             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3882               error ("%Hdeclaration of %<~%T%> as member of %qT",
3883                      &token->location, type_decl, scope);
3884             cp_parser_simulate_error (parser);
3885             return error_mark_node;
3886           }
3887
3888         /* [class.dtor]
3889
3890            A typedef-name that names a class shall not be used as the
3891            identifier in the declarator for a destructor declaration.  */
3892         if (declarator_p
3893             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3894             && !DECL_SELF_REFERENCE_P (type_decl)
3895             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3896           error ("%Htypedef-name %qD used as destructor declarator",
3897                  &token->location, type_decl);
3898
3899         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3900       }
3901
3902     case CPP_KEYWORD:
3903       if (token->keyword == RID_OPERATOR)
3904         {
3905           tree id;
3906
3907           /* This could be a template-id, so we try that first.  */
3908           cp_parser_parse_tentatively (parser);
3909           /* Try a template-id.  */
3910           id = cp_parser_template_id (parser, template_keyword_p,
3911                                       /*check_dependency_p=*/true,
3912                                       declarator_p);
3913           /* If that worked, we're done.  */
3914           if (cp_parser_parse_definitely (parser))
3915             return id;
3916           /* We still don't know whether we're looking at an
3917              operator-function-id or a conversion-function-id.  */
3918           cp_parser_parse_tentatively (parser);
3919           /* Try an operator-function-id.  */
3920           id = cp_parser_operator_function_id (parser);
3921           /* If that didn't work, try a conversion-function-id.  */
3922           if (!cp_parser_parse_definitely (parser))
3923             id = cp_parser_conversion_function_id (parser);
3924
3925           return id;
3926         }
3927       /* Fall through.  */
3928
3929     default:
3930       if (optional_p)
3931         return NULL_TREE;
3932       cp_parser_error (parser, "expected unqualified-id");
3933       return error_mark_node;
3934     }
3935 }
3936
3937 /* Parse an (optional) nested-name-specifier.
3938
3939    nested-name-specifier:
3940      class-or-namespace-name :: nested-name-specifier [opt]
3941      class-or-namespace-name :: template nested-name-specifier [opt]
3942
3943    PARSER->SCOPE should be set appropriately before this function is
3944    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3945    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3946    in name lookups.
3947
3948    Sets PARSER->SCOPE to the class (TYPE) or namespace
3949    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3950    it unchanged if there is no nested-name-specifier.  Returns the new
3951    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3952
3953    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3954    part of a declaration and/or decl-specifier.  */
3955
3956 static tree
3957 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3958                                      bool typename_keyword_p,
3959                                      bool check_dependency_p,
3960                                      bool type_p,
3961                                      bool is_declaration)
3962 {
3963   bool success = false;
3964   cp_token_position start = 0;
3965   cp_token *token;
3966
3967   /* Remember where the nested-name-specifier starts.  */
3968   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3969     {
3970       start = cp_lexer_token_position (parser->lexer, false);
3971       push_deferring_access_checks (dk_deferred);
3972     }
3973
3974   while (true)
3975     {
3976       tree new_scope;
3977       tree old_scope;
3978       tree saved_qualifying_scope;
3979       bool template_keyword_p;
3980
3981       /* Spot cases that cannot be the beginning of a
3982          nested-name-specifier.  */
3983       token = cp_lexer_peek_token (parser->lexer);
3984
3985       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3986          the already parsed nested-name-specifier.  */
3987       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3988         {
3989           /* Grab the nested-name-specifier and continue the loop.  */
3990           cp_parser_pre_parsed_nested_name_specifier (parser);
3991           /* If we originally encountered this nested-name-specifier
3992              with IS_DECLARATION set to false, we will not have
3993              resolved TYPENAME_TYPEs, so we must do so here.  */
3994           if (is_declaration
3995               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3996             {
3997               new_scope = resolve_typename_type (parser->scope,
3998                                                  /*only_current_p=*/false);
3999               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4000                 parser->scope = new_scope;
4001             }
4002           success = true;
4003           continue;
4004         }
4005
4006       /* Spot cases that cannot be the beginning of a
4007          nested-name-specifier.  On the second and subsequent times
4008          through the loop, we look for the `template' keyword.  */
4009       if (success && token->keyword == RID_TEMPLATE)
4010         ;
4011       /* A template-id can start a nested-name-specifier.  */
4012       else if (token->type == CPP_TEMPLATE_ID)
4013         ;
4014       else
4015         {
4016           /* If the next token is not an identifier, then it is
4017              definitely not a class-or-namespace-name.  */
4018           if (token->type != CPP_NAME)
4019             break;
4020           /* If the following token is neither a `<' (to begin a
4021              template-id), nor a `::', then we are not looking at a
4022              nested-name-specifier.  */
4023           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4024           if (token->type != CPP_SCOPE
4025               && !cp_parser_nth_token_starts_template_argument_list_p
4026                   (parser, 2))
4027             break;
4028         }
4029
4030       /* The nested-name-specifier is optional, so we parse
4031          tentatively.  */
4032       cp_parser_parse_tentatively (parser);
4033
4034       /* Look for the optional `template' keyword, if this isn't the
4035          first time through the loop.  */
4036       if (success)
4037         template_keyword_p = cp_parser_optional_template_keyword (parser);
4038       else
4039         template_keyword_p = false;
4040
4041       /* Save the old scope since the name lookup we are about to do
4042          might destroy it.  */
4043       old_scope = parser->scope;
4044       saved_qualifying_scope = parser->qualifying_scope;
4045       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4046          look up names in "X<T>::I" in order to determine that "Y" is
4047          a template.  So, if we have a typename at this point, we make
4048          an effort to look through it.  */
4049       if (is_declaration
4050           && !typename_keyword_p
4051           && parser->scope
4052           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4053         parser->scope = resolve_typename_type (parser->scope,
4054                                                /*only_current_p=*/false);
4055       /* Parse the qualifying entity.  */
4056       new_scope
4057         = cp_parser_class_or_namespace_name (parser,
4058                                              typename_keyword_p,
4059                                              template_keyword_p,
4060                                              check_dependency_p,
4061                                              type_p,
4062                                              is_declaration);
4063       /* Look for the `::' token.  */
4064       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4065
4066       /* If we found what we wanted, we keep going; otherwise, we're
4067          done.  */
4068       if (!cp_parser_parse_definitely (parser))
4069         {
4070           bool error_p = false;
4071
4072           /* Restore the OLD_SCOPE since it was valid before the
4073              failed attempt at finding the last
4074              class-or-namespace-name.  */
4075           parser->scope = old_scope;
4076           parser->qualifying_scope = saved_qualifying_scope;
4077           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4078             break;
4079           /* If the next token is an identifier, and the one after
4080              that is a `::', then any valid interpretation would have
4081              found a class-or-namespace-name.  */
4082           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4083                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4084                      == CPP_SCOPE)
4085                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4086                      != CPP_COMPL))
4087             {
4088               token = cp_lexer_consume_token (parser->lexer);
4089               if (!error_p)
4090                 {
4091                   if (!token->ambiguous_p)
4092                     {
4093                       tree decl;
4094                       tree ambiguous_decls;
4095
4096                       decl = cp_parser_lookup_name (parser, token->u.value,
4097                                                     none_type,
4098                                                     /*is_template=*/false,
4099                                                     /*is_namespace=*/false,
4100                                                     /*check_dependency=*/true,
4101                                                     &ambiguous_decls,
4102                                                     token->location);
4103                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4104                         error ("%H%qD used without template parameters",
4105                                &token->location, decl);
4106                       else if (ambiguous_decls)
4107                         {
4108                           error ("%Hreference to %qD is ambiguous",
4109                                  &token->location, token->u.value);
4110                           print_candidates (ambiguous_decls);
4111                           decl = error_mark_node;
4112                         }
4113                       else
4114                         cp_parser_name_lookup_error
4115                           (parser, token->u.value, decl,
4116                            "is not a class or namespace",
4117                            token->location);
4118                     }
4119                   parser->scope = error_mark_node;
4120                   error_p = true;
4121                   /* Treat this as a successful nested-name-specifier
4122                      due to:
4123
4124                      [basic.lookup.qual]
4125
4126                      If the name found is not a class-name (clause
4127                      _class_) or namespace-name (_namespace.def_), the
4128                      program is ill-formed.  */
4129                   success = true;
4130                 }
4131               cp_lexer_consume_token (parser->lexer);
4132             }
4133           break;
4134         }
4135       /* We've found one valid nested-name-specifier.  */
4136       success = true;
4137       /* Name lookup always gives us a DECL.  */
4138       if (TREE_CODE (new_scope) == TYPE_DECL)
4139         new_scope = TREE_TYPE (new_scope);
4140       /* Uses of "template" must be followed by actual templates.  */
4141       if (template_keyword_p
4142           && !(CLASS_TYPE_P (new_scope)
4143                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4144                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4145                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4146           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4147                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4148                    == TEMPLATE_ID_EXPR)))
4149         permerror (TYPE_P (new_scope)
4150                    ? "%qT is not a template"
4151                    : "%qD is not a template",
4152                    new_scope);
4153       /* If it is a class scope, try to complete it; we are about to
4154          be looking up names inside the class.  */
4155       if (TYPE_P (new_scope)
4156           /* Since checking types for dependency can be expensive,
4157              avoid doing it if the type is already complete.  */
4158           && !COMPLETE_TYPE_P (new_scope)
4159           /* Do not try to complete dependent types.  */
4160           && !dependent_type_p (new_scope))
4161         {
4162           new_scope = complete_type (new_scope);
4163           /* If it is a typedef to current class, use the current
4164              class instead, as the typedef won't have any names inside
4165              it yet.  */
4166           if (!COMPLETE_TYPE_P (new_scope)
4167               && currently_open_class (new_scope))
4168             new_scope = TYPE_MAIN_VARIANT (new_scope);
4169         }
4170       /* Make sure we look in the right scope the next time through
4171          the loop.  */
4172       parser->scope = new_scope;
4173     }
4174
4175   /* If parsing tentatively, replace the sequence of tokens that makes
4176      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4177      token.  That way, should we re-parse the token stream, we will
4178      not have to repeat the effort required to do the parse, nor will
4179      we issue duplicate error messages.  */
4180   if (success && start)
4181     {
4182       cp_token *token;
4183
4184       token = cp_lexer_token_at (parser->lexer, start);
4185       /* Reset the contents of the START token.  */
4186       token->type = CPP_NESTED_NAME_SPECIFIER;
4187       /* Retrieve any deferred checks.  Do not pop this access checks yet
4188          so the memory will not be reclaimed during token replacing below.  */
4189       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4190       token->u.tree_check_value->value = parser->scope;
4191       token->u.tree_check_value->checks = get_deferred_access_checks ();
4192       token->u.tree_check_value->qualifying_scope =
4193         parser->qualifying_scope;
4194       token->keyword = RID_MAX;
4195
4196       /* Purge all subsequent tokens.  */
4197       cp_lexer_purge_tokens_after (parser->lexer, start);
4198     }
4199
4200   if (start)
4201     pop_to_parent_deferring_access_checks ();
4202
4203   return success ? parser->scope : NULL_TREE;
4204 }
4205
4206 /* Parse a nested-name-specifier.  See
4207    cp_parser_nested_name_specifier_opt for details.  This function
4208    behaves identically, except that it will an issue an error if no
4209    nested-name-specifier is present.  */
4210
4211 static tree
4212 cp_parser_nested_name_specifier (cp_parser *parser,
4213                                  bool typename_keyword_p,
4214                                  bool check_dependency_p,
4215                                  bool type_p,
4216                                  bool is_declaration)
4217 {
4218   tree scope;
4219
4220   /* Look for the nested-name-specifier.  */
4221   scope = cp_parser_nested_name_specifier_opt (parser,
4222                                                typename_keyword_p,
4223                                                check_dependency_p,
4224                                                type_p,
4225                                                is_declaration);
4226   /* If it was not present, issue an error message.  */
4227   if (!scope)
4228     {
4229       cp_parser_error (parser, "expected nested-name-specifier");
4230       parser->scope = NULL_TREE;
4231     }
4232
4233   return scope;
4234 }
4235
4236 /* Parse a class-or-namespace-name.
4237
4238    class-or-namespace-name:
4239      class-name
4240      namespace-name
4241
4242    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4243    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4244    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4245    TYPE_P is TRUE iff the next name should be taken as a class-name,
4246    even the same name is declared to be another entity in the same
4247    scope.
4248
4249    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4250    specified by the class-or-namespace-name.  If neither is found the
4251    ERROR_MARK_NODE is returned.  */
4252
4253 static tree
4254 cp_parser_class_or_namespace_name (cp_parser *parser,
4255                                    bool typename_keyword_p,
4256                                    bool template_keyword_p,
4257                                    bool check_dependency_p,
4258                                    bool type_p,
4259                                    bool is_declaration)
4260 {
4261   tree saved_scope;
4262   tree saved_qualifying_scope;
4263   tree saved_object_scope;
4264   tree scope;
4265   bool only_class_p;
4266
4267   /* Before we try to parse the class-name, we must save away the
4268      current PARSER->SCOPE since cp_parser_class_name will destroy
4269      it.  */
4270   saved_scope = parser->scope;
4271   saved_qualifying_scope = parser->qualifying_scope;
4272   saved_object_scope = parser->object_scope;
4273   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4274      there is no need to look for a namespace-name.  */
4275   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4276   if (!only_class_p)
4277     cp_parser_parse_tentatively (parser);
4278   scope = cp_parser_class_name (parser,
4279                                 typename_keyword_p,
4280                                 template_keyword_p,
4281                                 type_p ? class_type : none_type,
4282                                 check_dependency_p,
4283                                 /*class_head_p=*/false,
4284                                 is_declaration);
4285   /* If that didn't work, try for a namespace-name.  */
4286   if (!only_class_p && !cp_parser_parse_definitely (parser))
4287     {
4288       /* Restore the saved scope.  */
4289       parser->scope = saved_scope;
4290       parser->qualifying_scope = saved_qualifying_scope;
4291       parser->object_scope = saved_object_scope;
4292       /* If we are not looking at an identifier followed by the scope
4293          resolution operator, then this is not part of a
4294          nested-name-specifier.  (Note that this function is only used
4295          to parse the components of a nested-name-specifier.)  */
4296       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4297           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4298         return error_mark_node;
4299       scope = cp_parser_namespace_name (parser);
4300     }
4301
4302   return scope;
4303 }
4304
4305 /* Parse a postfix-expression.
4306
4307    postfix-expression:
4308      primary-expression
4309      postfix-expression [ expression ]
4310      postfix-expression ( expression-list [opt] )
4311      simple-type-specifier ( expression-list [opt] )
4312      typename :: [opt] nested-name-specifier identifier
4313        ( expression-list [opt] )
4314      typename :: [opt] nested-name-specifier template [opt] template-id
4315        ( expression-list [opt] )
4316      postfix-expression . template [opt] id-expression
4317      postfix-expression -> template [opt] id-expression
4318      postfix-expression . pseudo-destructor-name
4319      postfix-expression -> pseudo-destructor-name
4320      postfix-expression ++
4321      postfix-expression --
4322      dynamic_cast < type-id > ( expression )
4323      static_cast < type-id > ( expression )
4324      reinterpret_cast < type-id > ( expression )
4325      const_cast < type-id > ( expression )
4326      typeid ( expression )
4327      typeid ( type-id )
4328
4329    GNU Extension:
4330
4331    postfix-expression:
4332      ( type-id ) { initializer-list , [opt] }
4333
4334    This extension is a GNU version of the C99 compound-literal
4335    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4336    but they are essentially the same concept.)
4337
4338    If ADDRESS_P is true, the postfix expression is the operand of the
4339    `&' operator.  CAST_P is true if this expression is the target of a
4340    cast.
4341
4342    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4343    class member access expressions [expr.ref].
4344
4345    Returns a representation of the expression.  */
4346
4347 static tree
4348 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4349                               bool member_access_only_p)
4350 {
4351   cp_token *token;
4352   enum rid keyword;
4353   cp_id_kind idk = CP_ID_KIND_NONE;
4354   tree postfix_expression = NULL_TREE;
4355   bool is_member_access = false;
4356
4357   /* Peek at the next token.  */
4358   token = cp_lexer_peek_token (parser->lexer);
4359   /* Some of the productions are determined by keywords.  */
4360   keyword = token->keyword;
4361   switch (keyword)
4362     {
4363     case RID_DYNCAST:
4364     case RID_STATCAST:
4365     case RID_REINTCAST:
4366     case RID_CONSTCAST:
4367       {
4368         tree type;
4369         tree expression;
4370         const char *saved_message;
4371
4372         /* All of these can be handled in the same way from the point
4373            of view of parsing.  Begin by consuming the token
4374            identifying the cast.  */
4375         cp_lexer_consume_token (parser->lexer);
4376
4377         /* New types cannot be defined in the cast.  */
4378         saved_message = parser->type_definition_forbidden_message;
4379         parser->type_definition_forbidden_message
4380           = "types may not be defined in casts";
4381
4382         /* Look for the opening `<'.  */
4383         cp_parser_require (parser, CPP_LESS, "%<<%>");
4384         /* Parse the type to which we are casting.  */
4385         type = cp_parser_type_id (parser);
4386         /* Look for the closing `>'.  */
4387         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4388         /* Restore the old message.  */
4389         parser->type_definition_forbidden_message = saved_message;
4390
4391         /* And the expression which is being cast.  */
4392         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4393         expression = cp_parser_expression (parser, /*cast_p=*/true);
4394         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4395
4396         /* Only type conversions to integral or enumeration types
4397            can be used in constant-expressions.  */
4398         if (!cast_valid_in_integral_constant_expression_p (type)
4399             && (cp_parser_non_integral_constant_expression
4400                 (parser,
4401                  "a cast to a type other than an integral or "
4402                  "enumeration type")))
4403           return error_mark_node;
4404
4405         switch (keyword)
4406           {
4407           case RID_DYNCAST:
4408             postfix_expression
4409               = build_dynamic_cast (type, expression, tf_warning_or_error);
4410             break;
4411           case RID_STATCAST:
4412             postfix_expression
4413               = build_static_cast (type, expression, tf_warning_or_error);
4414             break;
4415           case RID_REINTCAST:
4416             postfix_expression
4417               = build_reinterpret_cast (type, expression, 
4418                                         tf_warning_or_error);
4419             break;
4420           case RID_CONSTCAST:
4421             postfix_expression
4422               = build_const_cast (type, expression, tf_warning_or_error);
4423             break;
4424           default:
4425             gcc_unreachable ();
4426           }
4427       }
4428       break;
4429
4430     case RID_TYPEID:
4431       {
4432         tree type;
4433         const char *saved_message;
4434         bool saved_in_type_id_in_expr_p;
4435
4436         /* Consume the `typeid' token.  */
4437         cp_lexer_consume_token (parser->lexer);
4438         /* Look for the `(' token.  */
4439         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4440         /* Types cannot be defined in a `typeid' expression.  */
4441         saved_message = parser->type_definition_forbidden_message;
4442         parser->type_definition_forbidden_message
4443           = "types may not be defined in a %<typeid%> expression";
4444         /* We can't be sure yet whether we're looking at a type-id or an
4445            expression.  */
4446         cp_parser_parse_tentatively (parser);
4447         /* Try a type-id first.  */
4448         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4449         parser->in_type_id_in_expr_p = true;
4450         type = cp_parser_type_id (parser);
4451         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4452         /* Look for the `)' token.  Otherwise, we can't be sure that
4453            we're not looking at an expression: consider `typeid (int
4454            (3))', for example.  */
4455         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4456         /* If all went well, simply lookup the type-id.  */
4457         if (cp_parser_parse_definitely (parser))
4458           postfix_expression = get_typeid (type);
4459         /* Otherwise, fall back to the expression variant.  */
4460         else
4461           {
4462             tree expression;
4463
4464             /* Look for an expression.  */
4465             expression = cp_parser_expression (parser, /*cast_p=*/false);
4466             /* Compute its typeid.  */
4467             postfix_expression = build_typeid (expression);
4468             /* Look for the `)' token.  */
4469             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4470           }
4471         /* Restore the saved message.  */
4472         parser->type_definition_forbidden_message = saved_message;
4473         /* `typeid' may not appear in an integral constant expression.  */
4474         if (cp_parser_non_integral_constant_expression(parser,
4475                                                        "%<typeid%> operator"))
4476           return error_mark_node;
4477       }
4478       break;
4479
4480     case RID_TYPENAME:
4481       {
4482         tree type;
4483         /* The syntax permitted here is the same permitted for an
4484            elaborated-type-specifier.  */
4485         type = cp_parser_elaborated_type_specifier (parser,
4486                                                     /*is_friend=*/false,
4487                                                     /*is_declaration=*/false);
4488         postfix_expression = cp_parser_functional_cast (parser, type);
4489       }
4490       break;
4491
4492     default:
4493       {
4494         tree type;
4495
4496         /* If the next thing is a simple-type-specifier, we may be
4497            looking at a functional cast.  We could also be looking at
4498            an id-expression.  So, we try the functional cast, and if
4499            that doesn't work we fall back to the primary-expression.  */
4500         cp_parser_parse_tentatively (parser);
4501         /* Look for the simple-type-specifier.  */
4502         type = cp_parser_simple_type_specifier (parser,
4503                                                 /*decl_specs=*/NULL,
4504                                                 CP_PARSER_FLAGS_NONE);
4505         /* Parse the cast itself.  */
4506         if (!cp_parser_error_occurred (parser))
4507           postfix_expression
4508             = cp_parser_functional_cast (parser, type);
4509         /* If that worked, we're done.  */
4510         if (cp_parser_parse_definitely (parser))
4511           break;
4512
4513         /* If the functional-cast didn't work out, try a
4514            compound-literal.  */
4515         if (cp_parser_allow_gnu_extensions_p (parser)
4516             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4517           {
4518             VEC(constructor_elt,gc) *initializer_list = NULL;
4519             bool saved_in_type_id_in_expr_p;
4520
4521             cp_parser_parse_tentatively (parser);
4522             /* Consume the `('.  */
4523             cp_lexer_consume_token (parser->lexer);
4524             /* Parse the type.  */
4525             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4526             parser->in_type_id_in_expr_p = true;
4527             type = cp_parser_type_id (parser);
4528             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4529             /* Look for the `)'.  */
4530             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4531             /* Look for the `{'.  */
4532             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4533             /* If things aren't going well, there's no need to
4534                keep going.  */
4535             if (!cp_parser_error_occurred (parser))
4536               {
4537                 bool non_constant_p;
4538                 /* Parse the initializer-list.  */
4539                 initializer_list
4540                   = cp_parser_initializer_list (parser, &non_constant_p);
4541                 /* Allow a trailing `,'.  */
4542                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4543                   cp_lexer_consume_token (parser->lexer);
4544                 /* Look for the final `}'.  */
4545                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4546               }
4547             /* If that worked, we're definitely looking at a
4548                compound-literal expression.  */
4549             if (cp_parser_parse_definitely (parser))
4550               {
4551                 /* Warn the user that a compound literal is not
4552                    allowed in standard C++.  */
4553                 if (pedantic)
4554                   pedwarn ("ISO C++ forbids compound-literals");
4555                 /* For simplicity, we disallow compound literals in
4556                    constant-expressions.  We could
4557                    allow compound literals of integer type, whose
4558                    initializer was a constant, in constant
4559                    expressions.  Permitting that usage, as a further
4560                    extension, would not change the meaning of any
4561                    currently accepted programs.  (Of course, as
4562                    compound literals are not part of ISO C++, the
4563                    standard has nothing to say.)  */
4564                 if (cp_parser_non_integral_constant_expression 
4565                     (parser, "non-constant compound literals"))
4566                   {
4567                     postfix_expression = error_mark_node;
4568                     break;
4569                   }
4570                 /* Form the representation of the compound-literal.  */
4571                 postfix_expression
4572                   = (finish_compound_literal
4573                      (type, build_constructor (init_list_type_node,
4574                                                initializer_list)));
4575                 break;
4576               }
4577           }
4578
4579         /* It must be a primary-expression.  */
4580         postfix_expression
4581           = cp_parser_primary_expression (parser, address_p, cast_p,
4582                                           /*template_arg_p=*/false,
4583                                           &idk);
4584       }
4585       break;
4586     }
4587
4588   /* Keep looping until the postfix-expression is complete.  */
4589   while (true)
4590     {
4591       if (idk == CP_ID_KIND_UNQUALIFIED
4592           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4593           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4594         /* It is not a Koenig lookup function call.  */
4595         postfix_expression
4596           = unqualified_name_lookup_error (postfix_expression);
4597
4598       /* Peek at the next token.  */
4599       token = cp_lexer_peek_token (parser->lexer);
4600
4601       switch (token->type)
4602         {
4603         case CPP_OPEN_SQUARE:
4604           postfix_expression
4605             = cp_parser_postfix_open_square_expression (parser,
4606                                                         postfix_expression,
4607                                                         false);
4608           idk = CP_ID_KIND_NONE;
4609           is_member_access = false;
4610           break;
4611
4612         case CPP_OPEN_PAREN:
4613           /* postfix-expression ( expression-list [opt] ) */
4614           {
4615             bool koenig_p;
4616             bool is_builtin_constant_p;
4617             bool saved_integral_constant_expression_p = false;
4618             bool saved_non_integral_constant_expression_p = false;
4619             tree args;
4620
4621             is_member_access = false;
4622
4623             is_builtin_constant_p
4624               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4625             if (is_builtin_constant_p)
4626               {
4627                 /* The whole point of __builtin_constant_p is to allow
4628                    non-constant expressions to appear as arguments.  */
4629                 saved_integral_constant_expression_p
4630                   = parser->integral_constant_expression_p;
4631                 saved_non_integral_constant_expression_p
4632                   = parser->non_integral_constant_expression_p;
4633                 parser->integral_constant_expression_p = false;
4634               }
4635             args = (cp_parser_parenthesized_expression_list
4636                     (parser, /*is_attribute_list=*/false,
4637                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4638                      /*non_constant_p=*/NULL));
4639             if (is_builtin_constant_p)
4640               {
4641                 parser->integral_constant_expression_p
4642                   = saved_integral_constant_expression_p;
4643                 parser->non_integral_constant_expression_p
4644                   = saved_non_integral_constant_expression_p;
4645               }
4646
4647             if (args == error_mark_node)
4648               {
4649                 postfix_expression = error_mark_node;
4650                 break;
4651               }
4652
4653             /* Function calls are not permitted in
4654                constant-expressions.  */
4655             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4656                 && cp_parser_non_integral_constant_expression (parser,
4657                                                                "a function call"))
4658               {
4659                 postfix_expression = error_mark_node;
4660                 break;
4661               }
4662
4663             koenig_p = false;
4664             if (idk == CP_ID_KIND_UNQUALIFIED)
4665               {
4666                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4667                   {
4668                     if (args)
4669                       {
4670                         koenig_p = true;
4671                         postfix_expression
4672                           = perform_koenig_lookup (postfix_expression, args);
4673                       }
4674                     else
4675                       postfix_expression
4676                         = unqualified_fn_lookup_error (postfix_expression);
4677                   }
4678                 /* We do not perform argument-dependent lookup if
4679                    normal lookup finds a non-function, in accordance
4680                    with the expected resolution of DR 218.  */
4681                 else if (args && is_overloaded_fn (postfix_expression))
4682                   {
4683                     tree fn = get_first_fn (postfix_expression);
4684
4685                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4686                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4687
4688                     /* Only do argument dependent lookup if regular
4689                        lookup does not find a set of member functions.
4690                        [basic.lookup.koenig]/2a  */
4691                     if (!DECL_FUNCTION_MEMBER_P (fn))
4692                       {
4693                         koenig_p = true;
4694                         postfix_expression
4695                           = perform_koenig_lookup (postfix_expression, args);
4696                       }
4697                   }
4698               }
4699
4700             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4701               {
4702                 tree instance = TREE_OPERAND (postfix_expression, 0);
4703                 tree fn = TREE_OPERAND (postfix_expression, 1);
4704
4705                 if (processing_template_decl
4706                     && (type_dependent_expression_p (instance)
4707                         || (!BASELINK_P (fn)
4708                             && TREE_CODE (fn) != FIELD_DECL)
4709                         || type_dependent_expression_p (fn)
4710                         || any_type_dependent_arguments_p (args)))
4711                   {
4712                     postfix_expression
4713                       = build_nt_call_list (postfix_expression, args);
4714                     break;
4715                   }
4716
4717                 if (BASELINK_P (fn))
4718                   postfix_expression
4719                     = (build_new_method_call
4720                        (instance, fn, args, NULL_TREE,
4721                         (idk == CP_ID_KIND_QUALIFIED
4722                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4723                         /*fn_p=*/NULL,
4724                         tf_warning_or_error));
4725                 else
4726                   postfix_expression
4727                     = finish_call_expr (postfix_expression, args,
4728                                         /*disallow_virtual=*/false,
4729                                         /*koenig_p=*/false,
4730                                         tf_warning_or_error);
4731               }
4732             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4733                      || TREE_CODE (postfix_expression) == MEMBER_REF
4734                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4735               postfix_expression = (build_offset_ref_call_from_tree
4736                                     (postfix_expression, args));
4737             else if (idk == CP_ID_KIND_QUALIFIED)
4738               /* A call to a static class member, or a namespace-scope
4739                  function.  */
4740               postfix_expression
4741                 = finish_call_expr (postfix_expression, args,
4742                                     /*disallow_virtual=*/true,
4743                                     koenig_p,
4744                                     tf_warning_or_error);
4745             else
4746               /* All other function calls.  */
4747               postfix_expression
4748                 = finish_call_expr (postfix_expression, args,
4749                                     /*disallow_virtual=*/false,
4750                                     koenig_p,
4751                                     tf_warning_or_error);
4752
4753             if (warn_disallowed_functions)
4754               warn_if_disallowed_function_p (postfix_expression);
4755
4756             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4757             idk = CP_ID_KIND_NONE;
4758           }
4759           break;
4760
4761         case CPP_DOT:
4762         case CPP_DEREF:
4763           /* postfix-expression . template [opt] id-expression
4764              postfix-expression . pseudo-destructor-name
4765              postfix-expression -> template [opt] id-expression
4766              postfix-expression -> pseudo-destructor-name */
4767
4768           /* Consume the `.' or `->' operator.  */
4769           cp_lexer_consume_token (parser->lexer);
4770
4771           postfix_expression
4772             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4773                                                       postfix_expression,
4774                                                       false, &idk,
4775                                                       token->location);
4776
4777           is_member_access = true;
4778           break;
4779
4780         case CPP_PLUS_PLUS:
4781           /* postfix-expression ++  */
4782           /* Consume the `++' token.  */
4783           cp_lexer_consume_token (parser->lexer);
4784           /* Generate a representation for the complete expression.  */
4785           postfix_expression
4786             = finish_increment_expr (postfix_expression,
4787                                      POSTINCREMENT_EXPR);
4788           /* Increments may not appear in constant-expressions.  */
4789           if (cp_parser_non_integral_constant_expression (parser,
4790                                                           "an increment"))
4791             postfix_expression = error_mark_node;
4792           idk = CP_ID_KIND_NONE;
4793           is_member_access = false;
4794           break;
4795
4796         case CPP_MINUS_MINUS:
4797           /* postfix-expression -- */
4798           /* Consume the `--' token.  */
4799           cp_lexer_consume_token (parser->lexer);
4800           /* Generate a representation for the complete expression.  */
4801           postfix_expression
4802             = finish_increment_expr (postfix_expression,
4803                                      POSTDECREMENT_EXPR);
4804           /* Decrements may not appear in constant-expressions.  */
4805           if (cp_parser_non_integral_constant_expression (parser,
4806                                                           "a decrement"))
4807             postfix_expression = error_mark_node;
4808           idk = CP_ID_KIND_NONE;
4809           is_member_access = false;
4810           break;
4811
4812         default:
4813           if (member_access_only_p)
4814             return is_member_access? postfix_expression : error_mark_node;
4815           else
4816             return postfix_expression;
4817         }
4818     }
4819
4820   /* We should never get here.  */
4821   gcc_unreachable ();
4822   return error_mark_node;
4823 }
4824
4825 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4826    by cp_parser_builtin_offsetof.  We're looking for
4827
4828      postfix-expression [ expression ]
4829
4830    FOR_OFFSETOF is set if we're being called in that context, which
4831    changes how we deal with integer constant expressions.  */
4832
4833 static tree
4834 cp_parser_postfix_open_square_expression (cp_parser *parser,
4835                                           tree postfix_expression,
4836                                           bool for_offsetof)
4837 {
4838   tree index;
4839
4840   /* Consume the `[' token.  */
4841   cp_lexer_consume_token (parser->lexer);
4842
4843   /* Parse the index expression.  */
4844   /* ??? For offsetof, there is a question of what to allow here.  If
4845      offsetof is not being used in an integral constant expression context,
4846      then we *could* get the right answer by computing the value at runtime.
4847      If we are in an integral constant expression context, then we might
4848      could accept any constant expression; hard to say without analysis.
4849      Rather than open the barn door too wide right away, allow only integer
4850      constant expressions here.  */
4851   if (for_offsetof)
4852     index = cp_parser_constant_expression (parser, false, NULL);
4853   else
4854     index = cp_parser_expression (parser, /*cast_p=*/false);
4855
4856   /* Look for the closing `]'.  */
4857   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4858
4859   /* Build the ARRAY_REF.  */
4860   postfix_expression = grok_array_decl (postfix_expression, index);
4861
4862   /* When not doing offsetof, array references are not permitted in
4863      constant-expressions.  */
4864   if (!for_offsetof
4865       && (cp_parser_non_integral_constant_expression
4866           (parser, "an array reference")))
4867     postfix_expression = error_mark_node;
4868
4869   return postfix_expression;
4870 }
4871
4872 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4873    by cp_parser_builtin_offsetof.  We're looking for
4874
4875      postfix-expression . template [opt] id-expression
4876      postfix-expression . pseudo-destructor-name
4877      postfix-expression -> template [opt] id-expression
4878      postfix-expression -> pseudo-destructor-name
4879
4880    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4881    limits what of the above we'll actually accept, but nevermind.
4882    TOKEN_TYPE is the "." or "->" token, which will already have been
4883    removed from the stream.  */
4884
4885 static tree
4886 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4887                                         enum cpp_ttype token_type,
4888                                         tree postfix_expression,
4889                                         bool for_offsetof, cp_id_kind *idk,
4890                                         location_t location)
4891 {
4892   tree name;
4893   bool dependent_p;
4894   bool pseudo_destructor_p;
4895   tree scope = NULL_TREE;
4896
4897   /* If this is a `->' operator, dereference the pointer.  */
4898   if (token_type == CPP_DEREF)
4899     postfix_expression = build_x_arrow (postfix_expression);
4900   /* Check to see whether or not the expression is type-dependent.  */
4901   dependent_p = type_dependent_expression_p (postfix_expression);
4902   /* The identifier following the `->' or `.' is not qualified.  */
4903   parser->scope = NULL_TREE;
4904   parser->qualifying_scope = NULL_TREE;
4905   parser->object_scope = NULL_TREE;
4906   *idk = CP_ID_KIND_NONE;
4907   /* Enter the scope corresponding to the type of the object
4908      given by the POSTFIX_EXPRESSION.  */
4909   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4910     {
4911       scope = TREE_TYPE (postfix_expression);
4912       /* According to the standard, no expression should ever have
4913          reference type.  Unfortunately, we do not currently match
4914          the standard in this respect in that our internal representation
4915          of an expression may have reference type even when the standard
4916          says it does not.  Therefore, we have to manually obtain the
4917          underlying type here.  */
4918       scope = non_reference (scope);
4919       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4920       if (scope == unknown_type_node)
4921         {
4922           error ("%H%qE does not have class type", &location, postfix_expression);
4923           scope = NULL_TREE;
4924         }
4925       else
4926         scope = complete_type_or_else (scope, NULL_TREE);
4927       /* Let the name lookup machinery know that we are processing a
4928          class member access expression.  */
4929       parser->context->object_type = scope;
4930       /* If something went wrong, we want to be able to discern that case,
4931          as opposed to the case where there was no SCOPE due to the type
4932          of expression being dependent.  */
4933       if (!scope)
4934         scope = error_mark_node;
4935       /* If the SCOPE was erroneous, make the various semantic analysis
4936          functions exit quickly -- and without issuing additional error
4937          messages.  */
4938       if (scope == error_mark_node)
4939         postfix_expression = error_mark_node;
4940     }
4941
4942   /* Assume this expression is not a pseudo-destructor access.  */
4943   pseudo_destructor_p = false;
4944
4945   /* If the SCOPE is a scalar type, then, if this is a valid program,
4946      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4947      is type dependent, it can be pseudo-destructor-name or something else.
4948      Try to parse it as pseudo-destructor-name first.  */
4949   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4950     {
4951       tree s;
4952       tree type;
4953
4954       cp_parser_parse_tentatively (parser);
4955       /* Parse the pseudo-destructor-name.  */
4956       s = NULL_TREE;
4957       cp_parser_pseudo_destructor_name (parser, &s, &type);
4958       if (dependent_p
4959           && (cp_parser_error_occurred (parser)
4960               || TREE_CODE (type) != TYPE_DECL
4961               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4962         cp_parser_abort_tentative_parse (parser);
4963       else if (cp_parser_parse_definitely (parser))
4964         {
4965           pseudo_destructor_p = true;
4966           postfix_expression
4967             = finish_pseudo_destructor_expr (postfix_expression,
4968                                              s, TREE_TYPE (type));
4969         }
4970     }
4971
4972   if (!pseudo_destructor_p)
4973     {
4974       /* If the SCOPE is not a scalar type, we are looking at an
4975          ordinary class member access expression, rather than a
4976          pseudo-destructor-name.  */
4977       bool template_p;
4978       cp_token *token = cp_lexer_peek_token (parser->lexer);
4979       /* Parse the id-expression.  */
4980       name = (cp_parser_id_expression
4981               (parser,
4982                cp_parser_optional_template_keyword (parser),
4983                /*check_dependency_p=*/true,
4984                &template_p,
4985                /*declarator_p=*/false,
4986                /*optional_p=*/false));
4987       /* In general, build a SCOPE_REF if the member name is qualified.
4988          However, if the name was not dependent and has already been
4989          resolved; there is no need to build the SCOPE_REF.  For example;
4990
4991              struct X { void f(); };
4992              template <typename T> void f(T* t) { t->X::f(); }
4993
4994          Even though "t" is dependent, "X::f" is not and has been resolved
4995          to a BASELINK; there is no need to include scope information.  */
4996
4997       /* But we do need to remember that there was an explicit scope for
4998          virtual function calls.  */
4999       if (parser->scope)
5000         *idk = CP_ID_KIND_QUALIFIED;
5001
5002       /* If the name is a template-id that names a type, we will get a
5003          TYPE_DECL here.  That is invalid code.  */
5004       if (TREE_CODE (name) == TYPE_DECL)
5005         {
5006           error ("%Hinvalid use of %qD", &token->location, name);
5007           postfix_expression = error_mark_node;
5008         }
5009       else
5010         {
5011           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5012             {
5013               name = build_qualified_name (/*type=*/NULL_TREE,
5014                                            parser->scope,
5015                                            name,
5016                                            template_p);
5017               parser->scope = NULL_TREE;
5018               parser->qualifying_scope = NULL_TREE;
5019               parser->object_scope = NULL_TREE;
5020             }
5021           if (scope && name && BASELINK_P (name))
5022             adjust_result_of_qualified_name_lookup
5023               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5024           postfix_expression
5025             = finish_class_member_access_expr (postfix_expression, name,
5026                                                template_p, 
5027                                                tf_warning_or_error);
5028         }
5029     }
5030
5031   /* We no longer need to look up names in the scope of the object on
5032      the left-hand side of the `.' or `->' operator.  */
5033   parser->context->object_type = NULL_TREE;
5034
5035   /* Outside of offsetof, these operators may not appear in
5036      constant-expressions.  */
5037   if (!for_offsetof
5038       && (cp_parser_non_integral_constant_expression
5039           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5040     postfix_expression = error_mark_node;
5041
5042   return postfix_expression;
5043 }
5044
5045 /* Parse a parenthesized expression-list.
5046
5047    expression-list:
5048      assignment-expression
5049      expression-list, assignment-expression
5050
5051    attribute-list:
5052      expression-list
5053      identifier
5054      identifier, expression-list
5055
5056    CAST_P is true if this expression is the target of a cast.
5057
5058    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5059    argument pack.
5060
5061    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5062    representation of an assignment-expression.  Note that a TREE_LIST
5063    is returned even if there is only a single expression in the list.
5064    error_mark_node is returned if the ( and or ) are
5065    missing. NULL_TREE is returned on no expressions. The parentheses
5066    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5067    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5068    indicates whether or not all of the expressions in the list were
5069    constant.  */
5070
5071 static tree
5072 cp_parser_parenthesized_expression_list (cp_parser* parser,
5073                                          bool is_attribute_list,
5074                                          bool cast_p,
5075                                          bool allow_expansion_p,
5076                                          bool *non_constant_p)
5077 {
5078   tree expression_list = NULL_TREE;
5079   bool fold_expr_p = is_attribute_list;
5080   tree identifier = NULL_TREE;
5081   bool saved_greater_than_is_operator_p;
5082
5083   /* Assume all the expressions will be constant.  */
5084   if (non_constant_p)
5085     *non_constant_p = false;
5086
5087   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5088     return error_mark_node;
5089
5090   /* Within a parenthesized expression, a `>' token is always
5091      the greater-than operator.  */
5092   saved_greater_than_is_operator_p
5093     = parser->greater_than_is_operator_p;
5094   parser->greater_than_is_operator_p = true;
5095
5096   /* Consume expressions until there are no more.  */
5097   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5098     while (true)
5099       {
5100         tree expr;
5101
5102         /* At the beginning of attribute lists, check to see if the
5103            next token is an identifier.  */
5104         if (is_attribute_list
5105             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5106           {
5107             cp_token *token;
5108
5109             /* Consume the identifier.  */
5110             token = cp_lexer_consume_token (parser->lexer);
5111             /* Save the identifier.  */
5112             identifier = token->u.value;
5113           }
5114         else
5115           {
5116             bool expr_non_constant_p;
5117
5118             /* Parse the next assignment-expression.  */
5119             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5120               {
5121                 /* A braced-init-list.  */
5122                 maybe_warn_cpp0x ("extended initializer lists");
5123                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5124                 if (non_constant_p && expr_non_constant_p)
5125                   *non_constant_p = true;
5126               }
5127             else if (non_constant_p)
5128               {
5129                 expr = (cp_parser_constant_expression
5130                         (parser, /*allow_non_constant_p=*/true,
5131                          &expr_non_constant_p));
5132                 if (expr_non_constant_p)
5133                   *non_constant_p = true;
5134               }
5135             else
5136               expr = cp_parser_assignment_expression (parser, cast_p);
5137
5138             if (fold_expr_p)
5139               expr = fold_non_dependent_expr (expr);
5140
5141             /* If we have an ellipsis, then this is an expression
5142                expansion.  */
5143             if (allow_expansion_p
5144                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5145               {
5146                 /* Consume the `...'.  */
5147                 cp_lexer_consume_token (parser->lexer);
5148
5149                 /* Build the argument pack.  */
5150                 expr = make_pack_expansion (expr);
5151               }
5152
5153              /* Add it to the list.  We add error_mark_node
5154                 expressions to the list, so that we can still tell if
5155                 the correct form for a parenthesized expression-list
5156                 is found. That gives better errors.  */
5157             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5158
5159             if (expr == error_mark_node)
5160               goto skip_comma;
5161           }
5162
5163         /* After the first item, attribute lists look the same as
5164            expression lists.  */
5165         is_attribute_list = false;
5166
5167       get_comma:;
5168         /* If the next token isn't a `,', then we are done.  */
5169         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5170           break;
5171
5172         /* Otherwise, consume the `,' and keep going.  */
5173         cp_lexer_consume_token (parser->lexer);
5174       }
5175
5176   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5177     {
5178       int ending;
5179
5180     skip_comma:;
5181       /* We try and resync to an unnested comma, as that will give the
5182          user better diagnostics.  */
5183       ending = cp_parser_skip_to_closing_parenthesis (parser,
5184                                                       /*recovering=*/true,
5185                                                       /*or_comma=*/true,
5186                                                       /*consume_paren=*/true);
5187       if (ending < 0)
5188         goto get_comma;
5189       if (!ending)
5190         {
5191           parser->greater_than_is_operator_p
5192             = saved_greater_than_is_operator_p;
5193           return error_mark_node;
5194         }
5195     }
5196
5197   parser->greater_than_is_operator_p
5198     = saved_greater_than_is_operator_p;
5199
5200   /* We built up the list in reverse order so we must reverse it now.  */
5201   expression_list = nreverse (expression_list);
5202   if (identifier)
5203     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5204
5205   return expression_list;
5206 }
5207
5208 /* Parse a pseudo-destructor-name.
5209
5210    pseudo-destructor-name:
5211      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5212      :: [opt] nested-name-specifier template template-id :: ~ type-name
5213      :: [opt] nested-name-specifier [opt] ~ type-name
5214
5215    If either of the first two productions is used, sets *SCOPE to the
5216    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5217    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5218    or ERROR_MARK_NODE if the parse fails.  */
5219
5220 static void
5221 cp_parser_pseudo_destructor_name (cp_parser* parser,
5222                                   tree* scope,
5223                                   tree* type)
5224 {
5225   bool nested_name_specifier_p;
5226
5227   /* Assume that things will not work out.  */
5228   *type = error_mark_node;
5229
5230   /* Look for the optional `::' operator.  */
5231   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5232   /* Look for the optional nested-name-specifier.  */
5233   nested_name_specifier_p
5234     = (cp_parser_nested_name_specifier_opt (parser,
5235                                             /*typename_keyword_p=*/false,
5236                                             /*check_dependency_p=*/true,
5237                                             /*type_p=*/false,
5238                                             /*is_declaration=*/true)
5239        != NULL_TREE);
5240   /* Now, if we saw a nested-name-specifier, we might be doing the
5241      second production.  */
5242   if (nested_name_specifier_p
5243       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5244     {
5245       /* Consume the `template' keyword.  */
5246       cp_lexer_consume_token (parser->lexer);
5247       /* Parse the template-id.  */
5248       cp_parser_template_id (parser,
5249                              /*template_keyword_p=*/true,
5250                              /*check_dependency_p=*/false,
5251                              /*is_declaration=*/true);
5252       /* Look for the `::' token.  */
5253       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5254     }
5255   /* If the next token is not a `~', then there might be some
5256      additional qualification.  */
5257   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5258     {
5259       /* At this point, we're looking for "type-name :: ~".  The type-name
5260          must not be a class-name, since this is a pseudo-destructor.  So,
5261          it must be either an enum-name, or a typedef-name -- both of which
5262          are just identifiers.  So, we peek ahead to check that the "::"
5263          and "~" tokens are present; if they are not, then we can avoid
5264          calling type_name.  */
5265       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5266           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5267           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5268         {
5269           cp_parser_error (parser, "non-scalar type");
5270           return;
5271         }
5272
5273       /* Look for the type-name.  */
5274       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5275       if (*scope == error_mark_node)
5276         return;
5277
5278       /* Look for the `::' token.  */
5279       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5280     }
5281   else
5282     *scope = NULL_TREE;
5283
5284   /* Look for the `~'.  */
5285   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5286   /* Look for the type-name again.  We are not responsible for
5287      checking that it matches the first type-name.  */
5288   *type = cp_parser_nonclass_name (parser);
5289 }
5290
5291 /* Parse a unary-expression.
5292
5293    unary-expression:
5294      postfix-expression
5295      ++ cast-expression
5296      -- cast-expression
5297      unary-operator cast-expression
5298      sizeof unary-expression
5299      sizeof ( type-id )
5300      new-expression
5301      delete-expression
5302
5303    GNU Extensions:
5304
5305    unary-expression:
5306      __extension__ cast-expression
5307      __alignof__ unary-expression
5308      __alignof__ ( type-id )
5309      __real__ cast-expression
5310      __imag__ cast-expression
5311      && identifier
5312
5313    ADDRESS_P is true iff the unary-expression is appearing as the
5314    operand of the `&' operator.   CAST_P is true if this expression is
5315    the target of a cast.
5316
5317    Returns a representation of the expression.  */
5318
5319 static tree
5320 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5321 {
5322   cp_token *token;
5323   enum tree_code unary_operator;
5324
5325   /* Peek at the next token.  */
5326   token = cp_lexer_peek_token (parser->lexer);
5327   /* Some keywords give away the kind of expression.  */
5328   if (token->type == CPP_KEYWORD)
5329     {
5330       enum rid keyword = token->keyword;
5331
5332       switch (keyword)
5333         {
5334         case RID_ALIGNOF:
5335         case RID_SIZEOF:
5336           {
5337             tree operand;
5338             enum tree_code op;
5339
5340             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5341             /* Consume the token.  */
5342             cp_lexer_consume_token (parser->lexer);
5343             /* Parse the operand.  */
5344             operand = cp_parser_sizeof_operand (parser, keyword);
5345
5346             if (TYPE_P (operand))
5347               return cxx_sizeof_or_alignof_type (operand, op, true);
5348             else
5349               return cxx_sizeof_or_alignof_expr (operand, op, true);
5350           }
5351
5352         case RID_NEW:
5353           return cp_parser_new_expression (parser);
5354
5355         case RID_DELETE:
5356           return cp_parser_delete_expression (parser);
5357
5358         case RID_EXTENSION:
5359           {
5360             /* The saved value of the PEDANTIC flag.  */
5361             int saved_pedantic;
5362             tree expr;
5363
5364             /* Save away the PEDANTIC flag.  */
5365             cp_parser_extension_opt (parser, &saved_pedantic);
5366             /* Parse the cast-expression.  */
5367             expr = cp_parser_simple_cast_expression (parser);
5368             /* Restore the PEDANTIC flag.  */
5369             pedantic = saved_pedantic;
5370
5371             return expr;
5372           }
5373
5374         case RID_REALPART:
5375         case RID_IMAGPART:
5376           {
5377             tree expression;
5378
5379             /* Consume the `__real__' or `__imag__' token.  */
5380             cp_lexer_consume_token (parser->lexer);
5381             /* Parse the cast-expression.  */
5382             expression = cp_parser_simple_cast_expression (parser);
5383             /* Create the complete representation.  */
5384             return build_x_unary_op ((keyword == RID_REALPART
5385                                       ? REALPART_EXPR : IMAGPART_EXPR),
5386                                      expression,
5387                                      tf_warning_or_error);
5388           }
5389           break;
5390
5391         default:
5392           break;
5393         }
5394     }
5395
5396   /* Look for the `:: new' and `:: delete', which also signal the
5397      beginning of a new-expression, or delete-expression,
5398      respectively.  If the next token is `::', then it might be one of
5399      these.  */
5400   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5401     {
5402       enum rid keyword;
5403
5404       /* See if the token after the `::' is one of the keywords in
5405          which we're interested.  */
5406       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5407       /* If it's `new', we have a new-expression.  */
5408       if (keyword == RID_NEW)
5409         return cp_parser_new_expression (parser);
5410       /* Similarly, for `delete'.  */
5411       else if (keyword == RID_DELETE)
5412         return cp_parser_delete_expression (parser);
5413     }
5414
5415   /* Look for a unary operator.  */
5416   unary_operator = cp_parser_unary_operator (token);
5417   /* The `++' and `--' operators can be handled similarly, even though
5418      they are not technically unary-operators in the grammar.  */
5419   if (unary_operator == ERROR_MARK)
5420     {
5421       if (token->type == CPP_PLUS_PLUS)
5422         unary_operator = PREINCREMENT_EXPR;
5423       else if (token->type == CPP_MINUS_MINUS)
5424         unary_operator = PREDECREMENT_EXPR;
5425       /* Handle the GNU address-of-label extension.  */
5426       else if (cp_parser_allow_gnu_extensions_p (parser)
5427                && token->type == CPP_AND_AND)
5428         {
5429           tree identifier;
5430           tree expression;
5431
5432           /* Consume the '&&' token.  */
5433           cp_lexer_consume_token (parser->lexer);
5434           /* Look for the identifier.  */
5435           identifier = cp_parser_identifier (parser);
5436           /* Create an expression representing the address.  */
5437           expression = finish_label_address_expr (identifier);
5438           if (cp_parser_non_integral_constant_expression (parser,
5439                                                 "the address of a label"))
5440             expression = error_mark_node;
5441           return expression;
5442         }
5443     }
5444   if (unary_operator != ERROR_MARK)
5445     {
5446       tree cast_expression;
5447       tree expression = error_mark_node;
5448       const char *non_constant_p = NULL;
5449
5450       /* Consume the operator token.  */
5451       token = cp_lexer_consume_token (parser->lexer);
5452       /* Parse the cast-expression.  */
5453       cast_expression
5454         = cp_parser_cast_expression (parser,
5455                                      unary_operator == ADDR_EXPR,
5456                                      /*cast_p=*/false);
5457       /* Now, build an appropriate representation.  */
5458       switch (unary_operator)
5459         {
5460         case INDIRECT_REF:
5461           non_constant_p = "%<*%>";
5462           expression = build_x_indirect_ref (cast_expression, "unary *",
5463                                              tf_warning_or_error);
5464           break;
5465
5466         case ADDR_EXPR:
5467           non_constant_p = "%<&%>";
5468           /* Fall through.  */
5469         case BIT_NOT_EXPR:
5470           expression = build_x_unary_op (unary_operator, cast_expression,
5471                                          tf_warning_or_error);
5472           break;
5473
5474         case PREINCREMENT_EXPR:
5475         case PREDECREMENT_EXPR:
5476           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5477                             ? "%<++%>" : "%<--%>");
5478           /* Fall through.  */
5479         case UNARY_PLUS_EXPR:
5480         case NEGATE_EXPR:
5481         case TRUTH_NOT_EXPR:
5482           expression = finish_unary_op_expr (unary_operator, cast_expression);
5483           break;
5484
5485         default:
5486           gcc_unreachable ();
5487         }
5488
5489       if (non_constant_p
5490           && cp_parser_non_integral_constant_expression (parser,
5491                                                          non_constant_p))
5492         expression = error_mark_node;
5493
5494       return expression;
5495     }
5496
5497   return cp_parser_postfix_expression (parser, address_p, cast_p,
5498                                        /*member_access_only_p=*/false);
5499 }
5500
5501 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5502    unary-operator, the corresponding tree code is returned.  */
5503
5504 static enum tree_code
5505 cp_parser_unary_operator (cp_token* token)
5506 {
5507   switch (token->type)
5508     {
5509     case CPP_MULT:
5510       return INDIRECT_REF;
5511
5512     case CPP_AND:
5513       return ADDR_EXPR;
5514
5515     case CPP_PLUS:
5516       return UNARY_PLUS_EXPR;
5517
5518     case CPP_MINUS:
5519       return NEGATE_EXPR;
5520
5521     case CPP_NOT:
5522       return TRUTH_NOT_EXPR;
5523
5524     case CPP_COMPL:
5525       return BIT_NOT_EXPR;
5526
5527     default:
5528       return ERROR_MARK;
5529     }
5530 }
5531
5532 /* Parse a new-expression.
5533
5534    new-expression:
5535      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5536      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5537
5538    Returns a representation of the expression.  */
5539
5540 static tree
5541 cp_parser_new_expression (cp_parser* parser)
5542 {
5543   bool global_scope_p;
5544   tree placement;
5545   tree type;
5546   tree initializer;
5547   tree nelts;
5548
5549   /* Look for the optional `::' operator.  */
5550   global_scope_p
5551     = (cp_parser_global_scope_opt (parser,
5552                                    /*current_scope_valid_p=*/false)
5553        != NULL_TREE);
5554   /* Look for the `new' operator.  */
5555   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5556   /* There's no easy way to tell a new-placement from the
5557      `( type-id )' construct.  */
5558   cp_parser_parse_tentatively (parser);
5559   /* Look for a new-placement.  */
5560   placement = cp_parser_new_placement (parser);
5561   /* If that didn't work out, there's no new-placement.  */
5562   if (!cp_parser_parse_definitely (parser))
5563     placement = NULL_TREE;
5564
5565   /* If the next token is a `(', then we have a parenthesized
5566      type-id.  */
5567   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5568     {
5569       cp_token *token;
5570       /* Consume the `('.  */
5571       cp_lexer_consume_token (parser->lexer);
5572       /* Parse the type-id.  */
5573       type = cp_parser_type_id (parser);
5574       /* Look for the closing `)'.  */
5575       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5576       token = cp_lexer_peek_token (parser->lexer);
5577       /* There should not be a direct-new-declarator in this production,
5578          but GCC used to allowed this, so we check and emit a sensible error
5579          message for this case.  */
5580       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5581         {
5582           error ("%Harray bound forbidden after parenthesized type-id",
5583                  &token->location);
5584           inform ("%Htry removing the parentheses around the type-id",
5585                  &token->location);
5586           cp_parser_direct_new_declarator (parser);
5587         }
5588       nelts = NULL_TREE;
5589     }
5590   /* Otherwise, there must be a new-type-id.  */
5591   else
5592     type = cp_parser_new_type_id (parser, &nelts);
5593
5594   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5595   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5596       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5597     initializer = cp_parser_new_initializer (parser);
5598   else
5599     initializer = NULL_TREE;
5600
5601   /* A new-expression may not appear in an integral constant
5602      expression.  */
5603   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5604     return error_mark_node;
5605
5606   /* Create a representation of the new-expression.  */
5607   return build_new (placement, type, nelts, initializer, global_scope_p,
5608                     tf_warning_or_error);
5609 }
5610
5611 /* Parse a new-placement.
5612
5613    new-placement:
5614      ( expression-list )
5615
5616    Returns the same representation as for an expression-list.  */
5617
5618 static tree
5619 cp_parser_new_placement (cp_parser* parser)
5620 {
5621   tree expression_list;
5622
5623   /* Parse the expression-list.  */
5624   expression_list = (cp_parser_parenthesized_expression_list
5625                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5626                       /*non_constant_p=*/NULL));
5627
5628   return expression_list;
5629 }
5630
5631 /* Parse a new-type-id.
5632
5633    new-type-id:
5634      type-specifier-seq new-declarator [opt]
5635
5636    Returns the TYPE allocated.  If the new-type-id indicates an array
5637    type, *NELTS is set to the number of elements in the last array
5638    bound; the TYPE will not include the last array bound.  */
5639
5640 static tree
5641 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5642 {
5643   cp_decl_specifier_seq type_specifier_seq;
5644   cp_declarator *new_declarator;
5645   cp_declarator *declarator;
5646   cp_declarator *outer_declarator;
5647   const char *saved_message;
5648   tree type;
5649
5650   /* The type-specifier sequence must not contain type definitions.
5651      (It cannot contain declarations of new types either, but if they
5652      are not definitions we will catch that because they are not
5653      complete.)  */
5654   saved_message = parser->type_definition_forbidden_message;
5655   parser->type_definition_forbidden_message
5656     = "types may not be defined in a new-type-id";
5657   /* Parse the type-specifier-seq.  */
5658   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5659                                 &type_specifier_seq);
5660   /* Restore the old message.  */
5661   parser->type_definition_forbidden_message = saved_message;
5662   /* Parse the new-declarator.  */
5663   new_declarator = cp_parser_new_declarator_opt (parser);
5664
5665   /* Determine the number of elements in the last array dimension, if
5666      any.  */
5667   *nelts = NULL_TREE;
5668   /* Skip down to the last array dimension.  */
5669   declarator = new_declarator;
5670   outer_declarator = NULL;
5671   while (declarator && (declarator->kind == cdk_pointer
5672                         || declarator->kind == cdk_ptrmem))
5673     {
5674       outer_declarator = declarator;
5675       declarator = declarator->declarator;
5676     }
5677   while (declarator
5678          && declarator->kind == cdk_array
5679          && declarator->declarator
5680          && declarator->declarator->kind == cdk_array)
5681     {
5682       outer_declarator = declarator;
5683       declarator = declarator->declarator;
5684     }
5685
5686   if (declarator && declarator->kind == cdk_array)
5687     {
5688       *nelts = declarator->u.array.bounds;
5689       if (*nelts == error_mark_node)
5690         *nelts = integer_one_node;
5691
5692       if (outer_declarator)
5693         outer_declarator->declarator = declarator->declarator;
5694       else
5695         new_declarator = NULL;
5696     }
5697
5698   type = groktypename (&type_specifier_seq, new_declarator);
5699   return type;
5700 }
5701
5702 /* Parse an (optional) new-declarator.
5703
5704    new-declarator:
5705      ptr-operator new-declarator [opt]
5706      direct-new-declarator
5707
5708    Returns the declarator.  */
5709
5710 static cp_declarator *
5711 cp_parser_new_declarator_opt (cp_parser* parser)
5712 {
5713   enum tree_code code;
5714   tree type;
5715   cp_cv_quals cv_quals;
5716
5717   /* We don't know if there's a ptr-operator next, or not.  */
5718   cp_parser_parse_tentatively (parser);
5719   /* Look for a ptr-operator.  */
5720   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5721   /* If that worked, look for more new-declarators.  */
5722   if (cp_parser_parse_definitely (parser))
5723     {
5724       cp_declarator *declarator;
5725
5726       /* Parse another optional declarator.  */
5727       declarator = cp_parser_new_declarator_opt (parser);
5728
5729       return cp_parser_make_indirect_declarator
5730         (code, type, cv_quals, declarator);
5731     }
5732
5733   /* If the next token is a `[', there is a direct-new-declarator.  */
5734   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5735     return cp_parser_direct_new_declarator (parser);
5736
5737   return NULL;
5738 }
5739
5740 /* Parse a direct-new-declarator.
5741
5742    direct-new-declarator:
5743      [ expression ]
5744      direct-new-declarator [constant-expression]
5745
5746    */
5747
5748 static cp_declarator *
5749 cp_parser_direct_new_declarator (cp_parser* parser)
5750 {
5751   cp_declarator *declarator = NULL;
5752
5753   while (true)
5754     {
5755       tree expression;
5756
5757       /* Look for the opening `['.  */
5758       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5759       /* The first expression is not required to be constant.  */
5760       if (!declarator)
5761         {
5762           cp_token *token = cp_lexer_peek_token (parser->lexer);
5763           expression = cp_parser_expression (parser, /*cast_p=*/false);
5764           /* The standard requires that the expression have integral
5765              type.  DR 74 adds enumeration types.  We believe that the
5766              real intent is that these expressions be handled like the
5767              expression in a `switch' condition, which also allows
5768              classes with a single conversion to integral or
5769              enumeration type.  */
5770           if (!processing_template_decl)
5771             {
5772               expression
5773                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5774                                               expression,
5775                                               /*complain=*/true);
5776               if (!expression)
5777                 {
5778                   error ("%Hexpression in new-declarator must have integral "
5779                          "or enumeration type", &token->location);
5780                   expression = error_mark_node;
5781                 }
5782             }
5783         }
5784       /* But all the other expressions must be.  */
5785       else
5786         expression
5787           = cp_parser_constant_expression (parser,
5788                                            /*allow_non_constant=*/false,
5789                                            NULL);
5790       /* Look for the closing `]'.  */
5791       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5792
5793       /* Add this bound to the declarator.  */
5794       declarator = make_array_declarator (declarator, expression);
5795
5796       /* If the next token is not a `[', then there are no more
5797          bounds.  */
5798       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5799         break;
5800     }
5801
5802   return declarator;
5803 }
5804
5805 /* Parse a new-initializer.
5806
5807    new-initializer:
5808      ( expression-list [opt] )
5809      braced-init-list
5810
5811    Returns a representation of the expression-list.  If there is no
5812    expression-list, VOID_ZERO_NODE is returned.  */
5813
5814 static tree
5815 cp_parser_new_initializer (cp_parser* parser)
5816 {
5817   tree expression_list;
5818
5819   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5820     {
5821       bool expr_non_constant_p;
5822       maybe_warn_cpp0x ("extended initializer lists");
5823       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5824       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5825       expression_list = build_tree_list (NULL_TREE, expression_list);
5826     }
5827   else
5828     expression_list = (cp_parser_parenthesized_expression_list
5829                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5830                         /*non_constant_p=*/NULL));
5831   if (!expression_list)
5832     expression_list = void_zero_node;
5833
5834   return expression_list;
5835 }
5836
5837 /* Parse a delete-expression.
5838
5839    delete-expression:
5840      :: [opt] delete cast-expression
5841      :: [opt] delete [ ] cast-expression
5842
5843    Returns a representation of the expression.  */
5844
5845 static tree
5846 cp_parser_delete_expression (cp_parser* parser)
5847 {
5848   bool global_scope_p;
5849   bool array_p;
5850   tree expression;
5851
5852   /* Look for the optional `::' operator.  */
5853   global_scope_p
5854     = (cp_parser_global_scope_opt (parser,
5855                                    /*current_scope_valid_p=*/false)
5856        != NULL_TREE);
5857   /* Look for the `delete' keyword.  */
5858   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5859   /* See if the array syntax is in use.  */
5860   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5861     {
5862       /* Consume the `[' token.  */
5863       cp_lexer_consume_token (parser->lexer);
5864       /* Look for the `]' token.  */
5865       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5866       /* Remember that this is the `[]' construct.  */
5867       array_p = true;
5868     }
5869   else
5870     array_p = false;
5871
5872   /* Parse the cast-expression.  */
5873   expression = cp_parser_simple_cast_expression (parser);
5874
5875   /* A delete-expression may not appear in an integral constant
5876      expression.  */
5877   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5878     return error_mark_node;
5879
5880   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5881 }
5882
5883 /* Parse a cast-expression.
5884
5885    cast-expression:
5886      unary-expression
5887      ( type-id ) cast-expression
5888
5889    ADDRESS_P is true iff the unary-expression is appearing as the
5890    operand of the `&' operator.   CAST_P is true if this expression is
5891    the target of a cast.
5892
5893    Returns a representation of the expression.  */
5894
5895 static tree
5896 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5897 {
5898   /* If it's a `(', then we might be looking at a cast.  */
5899   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5900     {
5901       tree type = NULL_TREE;
5902       tree expr = NULL_TREE;
5903       bool compound_literal_p;
5904       const char *saved_message;
5905
5906       /* There's no way to know yet whether or not this is a cast.
5907          For example, `(int (3))' is a unary-expression, while `(int)
5908          3' is a cast.  So, we resort to parsing tentatively.  */
5909       cp_parser_parse_tentatively (parser);
5910       /* Types may not be defined in a cast.  */
5911       saved_message = parser->type_definition_forbidden_message;
5912       parser->type_definition_forbidden_message
5913         = "types may not be defined in casts";
5914       /* Consume the `('.  */
5915       cp_lexer_consume_token (parser->lexer);
5916       /* A very tricky bit is that `(struct S) { 3 }' is a
5917          compound-literal (which we permit in C++ as an extension).
5918          But, that construct is not a cast-expression -- it is a
5919          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5920          is legal; if the compound-literal were a cast-expression,
5921          you'd need an extra set of parentheses.)  But, if we parse
5922          the type-id, and it happens to be a class-specifier, then we
5923          will commit to the parse at that point, because we cannot
5924          undo the action that is done when creating a new class.  So,
5925          then we cannot back up and do a postfix-expression.
5926
5927          Therefore, we scan ahead to the closing `)', and check to see
5928          if the token after the `)' is a `{'.  If so, we are not
5929          looking at a cast-expression.
5930
5931          Save tokens so that we can put them back.  */
5932       cp_lexer_save_tokens (parser->lexer);
5933       /* Skip tokens until the next token is a closing parenthesis.
5934          If we find the closing `)', and the next token is a `{', then
5935          we are looking at a compound-literal.  */
5936       compound_literal_p
5937         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5938                                                   /*consume_paren=*/true)
5939            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5940       /* Roll back the tokens we skipped.  */
5941       cp_lexer_rollback_tokens (parser->lexer);
5942       /* If we were looking at a compound-literal, simulate an error
5943          so that the call to cp_parser_parse_definitely below will
5944          fail.  */
5945       if (compound_literal_p)
5946         cp_parser_simulate_error (parser);
5947       else
5948         {
5949           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5950           parser->in_type_id_in_expr_p = true;
5951           /* Look for the type-id.  */
5952           type = cp_parser_type_id (parser);
5953           /* Look for the closing `)'.  */
5954           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5955           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5956         }
5957
5958       /* Restore the saved message.  */
5959       parser->type_definition_forbidden_message = saved_message;
5960
5961       /* If ok so far, parse the dependent expression. We cannot be
5962          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5963          ctor of T, but looks like a cast to function returning T
5964          without a dependent expression.  */
5965       if (!cp_parser_error_occurred (parser))
5966         expr = cp_parser_cast_expression (parser,
5967                                           /*address_p=*/false,
5968                                           /*cast_p=*/true);
5969
5970       if (cp_parser_parse_definitely (parser))
5971         {
5972           /* Warn about old-style casts, if so requested.  */
5973           if (warn_old_style_cast
5974               && !in_system_header
5975               && !VOID_TYPE_P (type)
5976               && current_lang_name != lang_name_c)
5977             warning (OPT_Wold_style_cast, "use of old-style cast");
5978
5979           /* Only type conversions to integral or enumeration types
5980              can be used in constant-expressions.  */
5981           if (!cast_valid_in_integral_constant_expression_p (type)
5982               && (cp_parser_non_integral_constant_expression
5983                   (parser,
5984                    "a cast to a type other than an integral or "
5985                    "enumeration type")))
5986             return error_mark_node;
5987
5988           /* Perform the cast.  */
5989           expr = build_c_cast (type, expr);
5990           return expr;
5991         }
5992     }
5993
5994   /* If we get here, then it's not a cast, so it must be a
5995      unary-expression.  */
5996   return cp_parser_unary_expression (parser, address_p, cast_p);
5997 }
5998
5999 /* Parse a binary expression of the general form:
6000
6001    pm-expression:
6002      cast-expression
6003      pm-expression .* cast-expression
6004      pm-expression ->* cast-expression
6005
6006    multiplicative-expression:
6007      pm-expression
6008      multiplicative-expression * pm-expression
6009      multiplicative-expression / pm-expression
6010      multiplicative-expression % pm-expression
6011
6012    additive-expression:
6013      multiplicative-expression
6014      additive-expression + multiplicative-expression
6015      additive-expression - multiplicative-expression
6016
6017    shift-expression:
6018      additive-expression
6019      shift-expression << additive-expression
6020      shift-expression >> additive-expression
6021
6022    relational-expression:
6023      shift-expression
6024      relational-expression < shift-expression
6025      relational-expression > shift-expression
6026      relational-expression <= shift-expression
6027      relational-expression >= shift-expression
6028
6029   GNU Extension:
6030
6031    relational-expression:
6032      relational-expression <? shift-expression
6033      relational-expression >? shift-expression
6034
6035    equality-expression:
6036      relational-expression
6037      equality-expression == relational-expression
6038      equality-expression != relational-expression
6039
6040    and-expression:
6041      equality-expression
6042      and-expression & equality-expression
6043
6044    exclusive-or-expression:
6045      and-expression
6046      exclusive-or-expression ^ and-expression
6047
6048    inclusive-or-expression:
6049      exclusive-or-expression
6050      inclusive-or-expression | exclusive-or-expression
6051
6052    logical-and-expression:
6053      inclusive-or-expression
6054      logical-and-expression && inclusive-or-expression
6055
6056    logical-or-expression:
6057      logical-and-expression
6058      logical-or-expression || logical-and-expression
6059
6060    All these are implemented with a single function like:
6061
6062    binary-expression:
6063      simple-cast-expression
6064      binary-expression <token> binary-expression
6065
6066    CAST_P is true if this expression is the target of a cast.
6067
6068    The binops_by_token map is used to get the tree codes for each <token> type.
6069    binary-expressions are associated according to a precedence table.  */
6070
6071 #define TOKEN_PRECEDENCE(token)                              \
6072 (((token->type == CPP_GREATER                                \
6073    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6074   && !parser->greater_than_is_operator_p)                    \
6075  ? PREC_NOT_OPERATOR                                         \
6076  : binops_by_token[token->type].prec)
6077
6078 static tree
6079 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6080                              enum cp_parser_prec prec)
6081 {
6082   cp_parser_expression_stack stack;
6083   cp_parser_expression_stack_entry *sp = &stack[0];
6084   tree lhs, rhs;
6085   cp_token *token;
6086   enum tree_code tree_type, lhs_type, rhs_type;
6087   enum cp_parser_prec new_prec, lookahead_prec;
6088   bool overloaded_p;
6089
6090   /* Parse the first expression.  */
6091   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6092   lhs_type = ERROR_MARK;
6093
6094   for (;;)
6095     {
6096       /* Get an operator token.  */
6097       token = cp_lexer_peek_token (parser->lexer);
6098
6099       if (warn_cxx0x_compat
6100           && token->type == CPP_RSHIFT
6101           && !parser->greater_than_is_operator_p)
6102         {
6103           warning (OPT_Wc__0x_compat, 
6104                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6105                    &token->location);
6106           warning (OPT_Wc__0x_compat, 
6107                    "suggest parentheses around %<>>%> expression");
6108         }
6109
6110       new_prec = TOKEN_PRECEDENCE (token);
6111
6112       /* Popping an entry off the stack means we completed a subexpression:
6113          - either we found a token which is not an operator (`>' where it is not
6114            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6115            will happen repeatedly;
6116          - or, we found an operator which has lower priority.  This is the case
6117            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6118            parsing `3 * 4'.  */
6119       if (new_prec <= prec)
6120         {
6121           if (sp == stack)
6122             break;
6123           else
6124             goto pop;
6125         }
6126
6127      get_rhs:
6128       tree_type = binops_by_token[token->type].tree_type;
6129
6130       /* We used the operator token.  */
6131       cp_lexer_consume_token (parser->lexer);
6132
6133       /* Extract another operand.  It may be the RHS of this expression
6134          or the LHS of a new, higher priority expression.  */
6135       rhs = cp_parser_simple_cast_expression (parser);
6136       rhs_type = ERROR_MARK;
6137
6138       /* Get another operator token.  Look up its precedence to avoid
6139          building a useless (immediately popped) stack entry for common
6140          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6141       token = cp_lexer_peek_token (parser->lexer);
6142       lookahead_prec = TOKEN_PRECEDENCE (token);
6143       if (lookahead_prec > new_prec)
6144         {
6145           /* ... and prepare to parse the RHS of the new, higher priority
6146              expression.  Since precedence levels on the stack are
6147              monotonically increasing, we do not have to care about
6148              stack overflows.  */
6149           sp->prec = prec;
6150           sp->tree_type = tree_type;
6151           sp->lhs = lhs;
6152           sp->lhs_type = lhs_type;
6153           sp++;
6154           lhs = rhs;
6155           lhs_type = rhs_type;
6156           prec = new_prec;
6157           new_prec = lookahead_prec;
6158           goto get_rhs;
6159
6160          pop:
6161           /* If the stack is not empty, we have parsed into LHS the right side
6162              (`4' in the example above) of an expression we had suspended.
6163              We can use the information on the stack to recover the LHS (`3')
6164              from the stack together with the tree code (`MULT_EXPR'), and
6165              the precedence of the higher level subexpression
6166              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6167              which will be used to actually build the additive expression.  */
6168           --sp;
6169           prec = sp->prec;
6170           tree_type = sp->tree_type;
6171           rhs = lhs;
6172           rhs_type = lhs_type;
6173           lhs = sp->lhs;
6174           lhs_type = sp->lhs_type;
6175         }
6176
6177       overloaded_p = false;
6178       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6179                                &overloaded_p, tf_warning_or_error);
6180       lhs_type = tree_type;
6181
6182       /* If the binary operator required the use of an overloaded operator,
6183          then this expression cannot be an integral constant-expression.
6184          An overloaded operator can be used even if both operands are
6185          otherwise permissible in an integral constant-expression if at
6186          least one of the operands is of enumeration type.  */
6187
6188       if (overloaded_p
6189           && (cp_parser_non_integral_constant_expression
6190               (parser, "calls to overloaded operators")))
6191         return error_mark_node;
6192     }
6193
6194   return lhs;
6195 }
6196
6197
6198 /* Parse the `? expression : assignment-expression' part of a
6199    conditional-expression.  The LOGICAL_OR_EXPR is the
6200    logical-or-expression that started the conditional-expression.
6201    Returns a representation of the entire conditional-expression.
6202
6203    This routine is used by cp_parser_assignment_expression.
6204
6205      ? expression : assignment-expression
6206
6207    GNU Extensions:
6208
6209      ? : assignment-expression */
6210
6211 static tree
6212 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6213 {
6214   tree expr;
6215   tree assignment_expr;
6216
6217   /* Consume the `?' token.  */
6218   cp_lexer_consume_token (parser->lexer);
6219   if (cp_parser_allow_gnu_extensions_p (parser)
6220       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6221     /* Implicit true clause.  */
6222     expr = NULL_TREE;
6223   else
6224     /* Parse the expression.  */
6225     expr = cp_parser_expression (parser, /*cast_p=*/false);
6226
6227   /* The next token should be a `:'.  */
6228   cp_parser_require (parser, CPP_COLON, "%<:%>");
6229   /* Parse the assignment-expression.  */
6230   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6231
6232   /* Build the conditional-expression.  */
6233   return build_x_conditional_expr (logical_or_expr,
6234                                    expr,
6235                                    assignment_expr,
6236                                    tf_warning_or_error);
6237 }
6238
6239 /* Parse an assignment-expression.
6240
6241    assignment-expression:
6242      conditional-expression
6243      logical-or-expression assignment-operator assignment_expression
6244      throw-expression
6245
6246    CAST_P is true if this expression is the target of a cast.
6247
6248    Returns a representation for the expression.  */
6249
6250 static tree
6251 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6252 {
6253   tree expr;
6254
6255   /* If the next token is the `throw' keyword, then we're looking at
6256      a throw-expression.  */
6257   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6258     expr = cp_parser_throw_expression (parser);
6259   /* Otherwise, it must be that we are looking at a
6260      logical-or-expression.  */
6261   else
6262     {
6263       /* Parse the binary expressions (logical-or-expression).  */
6264       expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6265       /* If the next token is a `?' then we're actually looking at a
6266          conditional-expression.  */
6267       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6268         return cp_parser_question_colon_clause (parser, expr);
6269       else
6270         {
6271           enum tree_code assignment_operator;
6272
6273           /* If it's an assignment-operator, we're using the second
6274              production.  */
6275           assignment_operator
6276             = cp_parser_assignment_operator_opt (parser);
6277           if (assignment_operator != ERROR_MARK)
6278             {
6279               bool non_constant_p;
6280
6281               /* Parse the right-hand side of the assignment.  */
6282               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6283
6284               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6285                 maybe_warn_cpp0x ("extended initializer lists");
6286
6287               /* An assignment may not appear in a
6288                  constant-expression.  */
6289               if (cp_parser_non_integral_constant_expression (parser,
6290                                                               "an assignment"))
6291                 return error_mark_node;
6292               /* Build the assignment expression.  */
6293               expr = build_x_modify_expr (expr,
6294                                           assignment_operator,
6295                                           rhs,
6296                                           tf_warning_or_error);
6297             }
6298         }
6299     }
6300
6301   return expr;
6302 }
6303
6304 /* Parse an (optional) assignment-operator.
6305
6306    assignment-operator: one of
6307      = *= /= %= += -= >>= <<= &= ^= |=
6308
6309    GNU Extension:
6310
6311    assignment-operator: one of
6312      <?= >?=
6313
6314    If the next token is an assignment operator, the corresponding tree
6315    code is returned, and the token is consumed.  For example, for
6316    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6317    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6318    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6319    operator, ERROR_MARK is returned.  */
6320
6321 static enum tree_code
6322 cp_parser_assignment_operator_opt (cp_parser* parser)
6323 {
6324   enum tree_code op;
6325   cp_token *token;
6326
6327   /* Peek at the next toen.  */
6328   token = cp_lexer_peek_token (parser->lexer);
6329
6330   switch (token->type)
6331     {
6332     case CPP_EQ:
6333       op = NOP_EXPR;
6334       break;
6335
6336     case CPP_MULT_EQ:
6337       op = MULT_EXPR;
6338       break;
6339
6340     case CPP_DIV_EQ:
6341       op = TRUNC_DIV_EXPR;
6342       break;
6343
6344     case CPP_MOD_EQ:
6345       op = TRUNC_MOD_EXPR;
6346       break;
6347
6348     case CPP_PLUS_EQ:
6349       op = PLUS_EXPR;
6350       break;
6351
6352     case CPP_MINUS_EQ:
6353       op = MINUS_EXPR;
6354       break;
6355
6356     case CPP_RSHIFT_EQ:
6357       op = RSHIFT_EXPR;
6358       break;
6359
6360     case CPP_LSHIFT_EQ:
6361       op = LSHIFT_EXPR;
6362       break;
6363
6364     case CPP_AND_EQ:
6365       op = BIT_AND_EXPR;
6366       break;
6367
6368     case CPP_XOR_EQ:
6369       op = BIT_XOR_EXPR;
6370       break;
6371
6372     case CPP_OR_EQ:
6373       op = BIT_IOR_EXPR;
6374       break;
6375
6376     default:
6377       /* Nothing else is an assignment operator.  */
6378       op = ERROR_MARK;
6379     }
6380
6381   /* If it was an assignment operator, consume it.  */
6382   if (op != ERROR_MARK)
6383     cp_lexer_consume_token (parser->lexer);
6384
6385   return op;
6386 }
6387
6388 /* Parse an expression.
6389
6390    expression:
6391      assignment-expression
6392      expression , assignment-expression
6393
6394    CAST_P is true if this expression is the target of a cast.
6395
6396    Returns a representation of the expression.  */
6397
6398 static tree
6399 cp_parser_expression (cp_parser* parser, bool cast_p)
6400 {
6401   tree expression = NULL_TREE;
6402
6403   while (true)
6404     {
6405       tree assignment_expression;
6406
6407       /* Parse the next assignment-expression.  */
6408       assignment_expression
6409         = cp_parser_assignment_expression (parser, cast_p);
6410       /* If this is the first assignment-expression, we can just
6411          save it away.  */
6412       if (!expression)
6413         expression = assignment_expression;
6414       else
6415         expression = build_x_compound_expr (expression,
6416                                             assignment_expression,
6417                                             tf_warning_or_error);
6418       /* If the next token is not a comma, then we are done with the
6419          expression.  */
6420       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6421         break;
6422       /* Consume the `,'.  */
6423       cp_lexer_consume_token (parser->lexer);
6424       /* A comma operator cannot appear in a constant-expression.  */
6425       if (cp_parser_non_integral_constant_expression (parser,
6426                                                       "a comma operator"))
6427         expression = error_mark_node;
6428     }
6429
6430   return expression;
6431 }
6432
6433 /* Parse a constant-expression.
6434
6435    constant-expression:
6436      conditional-expression
6437
6438   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6439   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6440   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6441   is false, NON_CONSTANT_P should be NULL.  */
6442
6443 static tree
6444 cp_parser_constant_expression (cp_parser* parser,
6445                                bool allow_non_constant_p,
6446                                bool *non_constant_p)
6447 {
6448   bool saved_integral_constant_expression_p;
6449   bool saved_allow_non_integral_constant_expression_p;
6450   bool saved_non_integral_constant_expression_p;
6451   tree expression;
6452
6453   /* It might seem that we could simply parse the
6454      conditional-expression, and then check to see if it were
6455      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6456      one that the compiler can figure out is constant, possibly after
6457      doing some simplifications or optimizations.  The standard has a
6458      precise definition of constant-expression, and we must honor
6459      that, even though it is somewhat more restrictive.
6460
6461      For example:
6462
6463        int i[(2, 3)];
6464
6465      is not a legal declaration, because `(2, 3)' is not a
6466      constant-expression.  The `,' operator is forbidden in a
6467      constant-expression.  However, GCC's constant-folding machinery
6468      will fold this operation to an INTEGER_CST for `3'.  */
6469
6470   /* Save the old settings.  */
6471   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6472   saved_allow_non_integral_constant_expression_p
6473     = parser->allow_non_integral_constant_expression_p;
6474   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6475   /* We are now parsing a constant-expression.  */
6476   parser->integral_constant_expression_p = true;
6477   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6478   parser->non_integral_constant_expression_p = false;
6479   /* Although the grammar says "conditional-expression", we parse an
6480      "assignment-expression", which also permits "throw-expression"
6481      and the use of assignment operators.  In the case that
6482      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6483      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6484      actually essential that we look for an assignment-expression.
6485      For example, cp_parser_initializer_clauses uses this function to
6486      determine whether a particular assignment-expression is in fact
6487      constant.  */
6488   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6489   /* Restore the old settings.  */
6490   parser->integral_constant_expression_p
6491     = saved_integral_constant_expression_p;
6492   parser->allow_non_integral_constant_expression_p
6493     = saved_allow_non_integral_constant_expression_p;
6494   if (allow_non_constant_p)
6495     *non_constant_p = parser->non_integral_constant_expression_p;
6496   else if (parser->non_integral_constant_expression_p)
6497     expression = error_mark_node;
6498   parser->non_integral_constant_expression_p
6499     = saved_non_integral_constant_expression_p;
6500
6501   return expression;
6502 }
6503
6504 /* Parse __builtin_offsetof.
6505
6506    offsetof-expression:
6507      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6508
6509    offsetof-member-designator:
6510      id-expression
6511      | offsetof-member-designator "." id-expression
6512      | offsetof-member-designator "[" expression "]"  */
6513
6514 static tree
6515 cp_parser_builtin_offsetof (cp_parser *parser)
6516 {
6517   int save_ice_p, save_non_ice_p;
6518   tree type, expr;
6519   cp_id_kind dummy;
6520   cp_token *token;
6521
6522   /* We're about to accept non-integral-constant things, but will
6523      definitely yield an integral constant expression.  Save and
6524      restore these values around our local parsing.  */
6525   save_ice_p = parser->integral_constant_expression_p;
6526   save_non_ice_p = parser->non_integral_constant_expression_p;
6527
6528   /* Consume the "__builtin_offsetof" token.  */
6529   cp_lexer_consume_token (parser->lexer);
6530   /* Consume the opening `('.  */
6531   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6532   /* Parse the type-id.  */
6533   type = cp_parser_type_id (parser);
6534   /* Look for the `,'.  */
6535   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6536   token = cp_lexer_peek_token (parser->lexer);
6537
6538   /* Build the (type *)null that begins the traditional offsetof macro.  */
6539   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6540                             tf_warning_or_error);
6541
6542   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6543   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6544                                                  true, &dummy, token->location);
6545   while (true)
6546     {
6547       token = cp_lexer_peek_token (parser->lexer);
6548       switch (token->type)
6549         {
6550         case CPP_OPEN_SQUARE:
6551           /* offsetof-member-designator "[" expression "]" */
6552           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6553           break;
6554
6555         case CPP_DOT:
6556           /* offsetof-member-designator "." identifier */
6557           cp_lexer_consume_token (parser->lexer);
6558           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6559                                                          true, &dummy,
6560                                                          token->location);
6561           break;
6562
6563         case CPP_CLOSE_PAREN:
6564           /* Consume the ")" token.  */
6565           cp_lexer_consume_token (parser->lexer);
6566           goto success;
6567
6568         default:
6569           /* Error.  We know the following require will fail, but
6570              that gives the proper error message.  */
6571           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6572           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6573           expr = error_mark_node;
6574           goto failure;
6575         }
6576     }
6577
6578  success:
6579   /* If we're processing a template, we can't finish the semantics yet.
6580      Otherwise we can fold the entire expression now.  */
6581   if (processing_template_decl)
6582     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6583   else
6584     expr = finish_offsetof (expr);
6585
6586  failure:
6587   parser->integral_constant_expression_p = save_ice_p;
6588   parser->non_integral_constant_expression_p = save_non_ice_p;
6589
6590   return expr;
6591 }
6592
6593 /* Parse a trait expression.  */
6594
6595 static tree
6596 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6597 {
6598   cp_trait_kind kind;
6599   tree type1, type2 = NULL_TREE;
6600   bool binary = false;
6601   cp_decl_specifier_seq decl_specs;
6602
6603   switch (keyword)
6604     {
6605     case RID_HAS_NOTHROW_ASSIGN:
6606       kind = CPTK_HAS_NOTHROW_ASSIGN;
6607       break;
6608     case RID_HAS_NOTHROW_CONSTRUCTOR:
6609       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6610       break;
6611     case RID_HAS_NOTHROW_COPY:
6612       kind = CPTK_HAS_NOTHROW_COPY;
6613       break;
6614     case RID_HAS_TRIVIAL_ASSIGN:
6615       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6616       break;
6617     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6618       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6619       break;
6620     case RID_HAS_TRIVIAL_COPY:
6621       kind = CPTK_HAS_TRIVIAL_COPY;
6622       break;
6623     case RID_HAS_TRIVIAL_DESTRUCTOR:
6624       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6625       break;
6626     case RID_HAS_VIRTUAL_DESTRUCTOR:
6627       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6628       break;
6629     case RID_IS_ABSTRACT:
6630       kind = CPTK_IS_ABSTRACT;
6631       break;
6632     case RID_IS_BASE_OF:
6633       kind = CPTK_IS_BASE_OF;
6634       binary = true;
6635       break;
6636     case RID_IS_CLASS:
6637       kind = CPTK_IS_CLASS;
6638       break;
6639     case RID_IS_CONVERTIBLE_TO:
6640       kind = CPTK_IS_CONVERTIBLE_TO;
6641       binary = true;
6642       break;
6643     case RID_IS_EMPTY:
6644       kind = CPTK_IS_EMPTY;
6645       break;
6646     case RID_IS_ENUM:
6647       kind = CPTK_IS_ENUM;
6648       break;
6649     case RID_IS_POD:
6650       kind = CPTK_IS_POD;
6651       break;
6652     case RID_IS_POLYMORPHIC:
6653       kind = CPTK_IS_POLYMORPHIC;
6654       break;
6655     case RID_IS_UNION:
6656       kind = CPTK_IS_UNION;
6657       break;
6658     default:
6659       gcc_unreachable ();
6660     }
6661
6662   /* Consume the token.  */
6663   cp_lexer_consume_token (parser->lexer);
6664
6665   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6666
6667   type1 = cp_parser_type_id (parser);
6668
6669   if (type1 == error_mark_node)
6670     return error_mark_node;
6671
6672   /* Build a trivial decl-specifier-seq.  */
6673   clear_decl_specs (&decl_specs);
6674   decl_specs.type = type1;
6675
6676   /* Call grokdeclarator to figure out what type this is.  */
6677   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6678                           /*initialized=*/0, /*attrlist=*/NULL);
6679
6680   if (binary)
6681     {
6682       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6683  
6684       type2 = cp_parser_type_id (parser);
6685
6686       if (type2 == error_mark_node)
6687         return error_mark_node;
6688
6689       /* Build a trivial decl-specifier-seq.  */
6690       clear_decl_specs (&decl_specs);
6691       decl_specs.type = type2;
6692
6693       /* Call grokdeclarator to figure out what type this is.  */
6694       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6695                               /*initialized=*/0, /*attrlist=*/NULL);
6696     }
6697
6698   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6699
6700   /* Complete the trait expression, which may mean either processing
6701      the trait expr now or saving it for template instantiation.  */
6702   return finish_trait_expr (kind, type1, type2);
6703 }
6704
6705 /* Statements [gram.stmt.stmt]  */
6706
6707 /* Parse a statement.
6708
6709    statement:
6710      labeled-statement
6711      expression-statement
6712      compound-statement
6713      selection-statement
6714      iteration-statement
6715      jump-statement
6716      declaration-statement
6717      try-block
6718
6719   IN_COMPOUND is true when the statement is nested inside a
6720   cp_parser_compound_statement; this matters for certain pragmas.
6721
6722   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6723   is a (possibly labeled) if statement which is not enclosed in braces
6724   and has an else clause.  This is used to implement -Wparentheses.  */
6725
6726 static void
6727 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6728                      bool in_compound, bool *if_p)
6729 {
6730   tree statement;
6731   cp_token *token;
6732   location_t statement_location;
6733
6734  restart:
6735   if (if_p != NULL)
6736     *if_p = false;
6737   /* There is no statement yet.  */
6738   statement = NULL_TREE;
6739   /* Peek at the next token.  */
6740   token = cp_lexer_peek_token (parser->lexer);
6741   /* Remember the location of the first token in the statement.  */
6742   statement_location = token->location;
6743   /* If this is a keyword, then that will often determine what kind of
6744      statement we have.  */
6745   if (token->type == CPP_KEYWORD)
6746     {
6747       enum rid keyword = token->keyword;
6748
6749       switch (keyword)
6750         {
6751         case RID_CASE:
6752         case RID_DEFAULT:
6753           /* Looks like a labeled-statement with a case label.
6754              Parse the label, and then use tail recursion to parse
6755              the statement.  */
6756           cp_parser_label_for_labeled_statement (parser);
6757           goto restart;
6758
6759         case RID_IF:
6760         case RID_SWITCH:
6761           statement = cp_parser_selection_statement (parser, if_p);
6762           break;
6763
6764         case RID_WHILE:
6765         case RID_DO:
6766         case RID_FOR:
6767           statement = cp_parser_iteration_statement (parser);
6768           break;
6769
6770         case RID_BREAK:
6771         case RID_CONTINUE:
6772         case RID_RETURN:
6773         case RID_GOTO:
6774           statement = cp_parser_jump_statement (parser);
6775           break;
6776
6777           /* Objective-C++ exception-handling constructs.  */
6778         case RID_AT_TRY:
6779         case RID_AT_CATCH:
6780         case RID_AT_FINALLY:
6781         case RID_AT_SYNCHRONIZED:
6782         case RID_AT_THROW:
6783           statement = cp_parser_objc_statement (parser);
6784           break;
6785
6786         case RID_TRY:
6787           statement = cp_parser_try_block (parser);
6788           break;
6789
6790         case RID_NAMESPACE:
6791           /* This must be a namespace alias definition.  */
6792           cp_parser_declaration_statement (parser);
6793           return;
6794           
6795         default:
6796           /* It might be a keyword like `int' that can start a
6797              declaration-statement.  */
6798           break;
6799         }
6800     }
6801   else if (token->type == CPP_NAME)
6802     {
6803       /* If the next token is a `:', then we are looking at a
6804          labeled-statement.  */
6805       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6806       if (token->type == CPP_COLON)
6807         {
6808           /* Looks like a labeled-statement with an ordinary label.
6809              Parse the label, and then use tail recursion to parse
6810              the statement.  */
6811           cp_parser_label_for_labeled_statement (parser);
6812           goto restart;
6813         }
6814     }
6815   /* Anything that starts with a `{' must be a compound-statement.  */
6816   else if (token->type == CPP_OPEN_BRACE)
6817     statement = cp_parser_compound_statement (parser, NULL, false);
6818   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6819      a statement all its own.  */
6820   else if (token->type == CPP_PRAGMA)
6821     {
6822       /* Only certain OpenMP pragmas are attached to statements, and thus
6823          are considered statements themselves.  All others are not.  In
6824          the context of a compound, accept the pragma as a "statement" and
6825          return so that we can check for a close brace.  Otherwise we
6826          require a real statement and must go back and read one.  */
6827       if (in_compound)
6828         cp_parser_pragma (parser, pragma_compound);
6829       else if (!cp_parser_pragma (parser, pragma_stmt))
6830         goto restart;
6831       return;
6832     }
6833   else if (token->type == CPP_EOF)
6834     {
6835       cp_parser_error (parser, "expected statement");
6836       return;
6837     }
6838
6839   /* Everything else must be a declaration-statement or an
6840      expression-statement.  Try for the declaration-statement
6841      first, unless we are looking at a `;', in which case we know that
6842      we have an expression-statement.  */
6843   if (!statement)
6844     {
6845       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6846         {
6847           cp_parser_parse_tentatively (parser);
6848           /* Try to parse the declaration-statement.  */
6849           cp_parser_declaration_statement (parser);
6850           /* If that worked, we're done.  */
6851           if (cp_parser_parse_definitely (parser))
6852             return;
6853         }
6854       /* Look for an expression-statement instead.  */
6855       statement = cp_parser_expression_statement (parser, in_statement_expr);
6856     }
6857
6858   /* Set the line number for the statement.  */
6859   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6860     SET_EXPR_LOCATION (statement, statement_location);
6861 }
6862
6863 /* Parse the label for a labeled-statement, i.e.
6864
6865    identifier :
6866    case constant-expression :
6867    default :
6868
6869    GNU Extension:
6870    case constant-expression ... constant-expression : statement
6871
6872    When a label is parsed without errors, the label is added to the
6873    parse tree by the finish_* functions, so this function doesn't
6874    have to return the label.  */
6875
6876 static void
6877 cp_parser_label_for_labeled_statement (cp_parser* parser)
6878 {
6879   cp_token *token;
6880
6881   /* The next token should be an identifier.  */
6882   token = cp_lexer_peek_token (parser->lexer);
6883   if (token->type != CPP_NAME
6884       && token->type != CPP_KEYWORD)
6885     {
6886       cp_parser_error (parser, "expected labeled-statement");
6887       return;
6888     }
6889
6890   switch (token->keyword)
6891     {
6892     case RID_CASE:
6893       {
6894         tree expr, expr_hi;
6895         cp_token *ellipsis;
6896
6897         /* Consume the `case' token.  */
6898         cp_lexer_consume_token (parser->lexer);
6899         /* Parse the constant-expression.  */
6900         expr = cp_parser_constant_expression (parser,
6901                                               /*allow_non_constant_p=*/false,
6902                                               NULL);
6903
6904         ellipsis = cp_lexer_peek_token (parser->lexer);
6905         if (ellipsis->type == CPP_ELLIPSIS)
6906           {
6907             /* Consume the `...' token.  */
6908             cp_lexer_consume_token (parser->lexer);
6909             expr_hi =
6910               cp_parser_constant_expression (parser,
6911                                              /*allow_non_constant_p=*/false,
6912                                              NULL);
6913             /* We don't need to emit warnings here, as the common code
6914                will do this for us.  */
6915           }
6916         else
6917           expr_hi = NULL_TREE;
6918
6919         if (parser->in_switch_statement_p)
6920           finish_case_label (expr, expr_hi);
6921         else
6922           error ("%Hcase label %qE not within a switch statement",
6923                  &token->location, expr);
6924       }
6925       break;
6926
6927     case RID_DEFAULT:
6928       /* Consume the `default' token.  */
6929       cp_lexer_consume_token (parser->lexer);
6930
6931       if (parser->in_switch_statement_p)
6932         finish_case_label (NULL_TREE, NULL_TREE);
6933       else
6934         error ("%Hcase label not within a switch statement", &token->location);
6935       break;
6936
6937     default:
6938       /* Anything else must be an ordinary label.  */
6939       finish_label_stmt (cp_parser_identifier (parser));
6940       break;
6941     }
6942
6943   /* Require the `:' token.  */
6944   cp_parser_require (parser, CPP_COLON, "%<:%>");
6945 }
6946
6947 /* Parse an expression-statement.
6948
6949    expression-statement:
6950      expression [opt] ;
6951
6952    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6953    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6954    indicates whether this expression-statement is part of an
6955    expression statement.  */
6956
6957 static tree
6958 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6959 {
6960   tree statement = NULL_TREE;
6961
6962   /* If the next token is a ';', then there is no expression
6963      statement.  */
6964   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6965     statement = cp_parser_expression (parser, /*cast_p=*/false);
6966
6967   /* Consume the final `;'.  */
6968   cp_parser_consume_semicolon_at_end_of_statement (parser);
6969
6970   if (in_statement_expr
6971       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6972     /* This is the final expression statement of a statement
6973        expression.  */
6974     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6975   else if (statement)
6976     statement = finish_expr_stmt (statement);
6977   else
6978     finish_stmt ();
6979
6980   return statement;
6981 }
6982
6983 /* Parse a compound-statement.
6984
6985    compound-statement:
6986      { statement-seq [opt] }
6987
6988    GNU extension:
6989
6990    compound-statement:
6991      { label-declaration-seq [opt] statement-seq [opt] }
6992
6993    label-declaration-seq:
6994      label-declaration
6995      label-declaration-seq label-declaration
6996
6997    Returns a tree representing the statement.  */
6998
6999 static tree
7000 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7001                               bool in_try)
7002 {
7003   tree compound_stmt;
7004
7005   /* Consume the `{'.  */
7006   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7007     return error_mark_node;
7008   /* Begin the compound-statement.  */
7009   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7010   /* If the next keyword is `__label__' we have a label declaration.  */
7011   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7012     cp_parser_label_declaration (parser);
7013   /* Parse an (optional) statement-seq.  */
7014   cp_parser_statement_seq_opt (parser, in_statement_expr);
7015   /* Finish the compound-statement.  */
7016   finish_compound_stmt (compound_stmt);
7017   /* Consume the `}'.  */
7018   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7019
7020   return compound_stmt;
7021 }
7022
7023 /* Parse an (optional) statement-seq.
7024
7025    statement-seq:
7026      statement
7027      statement-seq [opt] statement  */
7028
7029 static void
7030 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7031 {
7032   /* Scan statements until there aren't any more.  */
7033   while (true)
7034     {
7035       cp_token *token = cp_lexer_peek_token (parser->lexer);
7036
7037       /* If we're looking at a `}', then we've run out of statements.  */
7038       if (token->type == CPP_CLOSE_BRACE
7039           || token->type == CPP_EOF
7040           || token->type == CPP_PRAGMA_EOL)
7041         break;
7042       
7043       /* If we are in a compound statement and find 'else' then
7044          something went wrong.  */
7045       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7046         {
7047           if (parser->in_statement & IN_IF_STMT) 
7048             break;
7049           else
7050             {
7051               token = cp_lexer_consume_token (parser->lexer);
7052               error ("%H%<else%> without a previous %<if%>", &token->location);
7053             }
7054         }
7055
7056       /* Parse the statement.  */
7057       cp_parser_statement (parser, in_statement_expr, true, NULL);
7058     }
7059 }
7060
7061 /* Parse a selection-statement.
7062
7063    selection-statement:
7064      if ( condition ) statement
7065      if ( condition ) statement else statement
7066      switch ( condition ) statement
7067
7068    Returns the new IF_STMT or SWITCH_STMT.
7069
7070    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7071    is a (possibly labeled) if statement which is not enclosed in
7072    braces and has an else clause.  This is used to implement
7073    -Wparentheses.  */
7074
7075 static tree
7076 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7077 {
7078   cp_token *token;
7079   enum rid keyword;
7080
7081   if (if_p != NULL)
7082     *if_p = false;
7083
7084   /* Peek at the next token.  */
7085   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7086
7087   /* See what kind of keyword it is.  */
7088   keyword = token->keyword;
7089   switch (keyword)
7090     {
7091     case RID_IF:
7092     case RID_SWITCH:
7093       {
7094         tree statement;
7095         tree condition;
7096
7097         /* Look for the `('.  */
7098         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7099           {
7100             cp_parser_skip_to_end_of_statement (parser);
7101             return error_mark_node;
7102           }
7103
7104         /* Begin the selection-statement.  */
7105         if (keyword == RID_IF)
7106           statement = begin_if_stmt ();
7107         else
7108           statement = begin_switch_stmt ();
7109
7110         /* Parse the condition.  */
7111         condition = cp_parser_condition (parser);
7112         /* Look for the `)'.  */
7113         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7114           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7115                                                  /*consume_paren=*/true);
7116
7117         if (keyword == RID_IF)
7118           {
7119             bool nested_if;
7120             unsigned char in_statement;
7121
7122             /* Add the condition.  */
7123             finish_if_stmt_cond (condition, statement);
7124
7125             /* Parse the then-clause.  */
7126             in_statement = parser->in_statement;
7127             parser->in_statement |= IN_IF_STMT;
7128             cp_parser_implicitly_scoped_statement (parser, &nested_if);
7129             parser->in_statement = in_statement;
7130
7131             finish_then_clause (statement);
7132
7133             /* If the next token is `else', parse the else-clause.  */
7134             if (cp_lexer_next_token_is_keyword (parser->lexer,
7135                                                 RID_ELSE))
7136               {
7137                 /* Consume the `else' keyword.  */
7138                 cp_lexer_consume_token (parser->lexer);
7139                 begin_else_clause (statement);
7140                 /* Parse the else-clause.  */
7141                 cp_parser_implicitly_scoped_statement (parser, NULL);
7142                 finish_else_clause (statement);
7143
7144                 /* If we are currently parsing a then-clause, then
7145                    IF_P will not be NULL.  We set it to true to
7146                    indicate that this if statement has an else clause.
7147                    This may trigger the Wparentheses warning below
7148                    when we get back up to the parent if statement.  */
7149                 if (if_p != NULL)
7150                   *if_p = true;
7151               }
7152             else
7153               {
7154                 /* This if statement does not have an else clause.  If
7155                    NESTED_IF is true, then the then-clause is an if
7156                    statement which does have an else clause.  We warn
7157                    about the potential ambiguity.  */
7158                 if (nested_if)
7159                   warning (OPT_Wparentheses,
7160                            ("%Hsuggest explicit braces "
7161                             "to avoid ambiguous %<else%>"),
7162                            EXPR_LOCUS (statement));
7163               }
7164
7165             /* Now we're all done with the if-statement.  */
7166             finish_if_stmt (statement);
7167           }
7168         else
7169           {
7170             bool in_switch_statement_p;
7171             unsigned char in_statement;
7172
7173             /* Add the condition.  */
7174             finish_switch_cond (condition, statement);
7175
7176             /* Parse the body of the switch-statement.  */
7177             in_switch_statement_p = parser->in_switch_statement_p;
7178             in_statement = parser->in_statement;
7179             parser->in_switch_statement_p = true;
7180             parser->in_statement |= IN_SWITCH_STMT;
7181             cp_parser_implicitly_scoped_statement (parser, NULL);
7182             parser->in_switch_statement_p = in_switch_statement_p;
7183             parser->in_statement = in_statement;
7184
7185             /* Now we're all done with the switch-statement.  */
7186             finish_switch_stmt (statement);
7187           }
7188
7189         return statement;
7190       }
7191       break;
7192
7193     default:
7194       cp_parser_error (parser, "expected selection-statement");
7195       return error_mark_node;
7196     }
7197 }
7198
7199 /* Parse a condition.
7200
7201    condition:
7202      expression
7203      type-specifier-seq declarator = initializer-clause
7204      type-specifier-seq declarator braced-init-list
7205
7206    GNU Extension:
7207
7208    condition:
7209      type-specifier-seq declarator asm-specification [opt]
7210        attributes [opt] = assignment-expression
7211
7212    Returns the expression that should be tested.  */
7213
7214 static tree
7215 cp_parser_condition (cp_parser* parser)
7216 {
7217   cp_decl_specifier_seq type_specifiers;
7218   const char *saved_message;
7219
7220   /* Try the declaration first.  */
7221   cp_parser_parse_tentatively (parser);
7222   /* New types are not allowed in the type-specifier-seq for a
7223      condition.  */
7224   saved_message = parser->type_definition_forbidden_message;
7225   parser->type_definition_forbidden_message
7226     = "types may not be defined in conditions";
7227   /* Parse the type-specifier-seq.  */
7228   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7229                                 &type_specifiers);
7230   /* Restore the saved message.  */
7231   parser->type_definition_forbidden_message = saved_message;
7232   /* If all is well, we might be looking at a declaration.  */
7233   if (!cp_parser_error_occurred (parser))
7234     {
7235       tree decl;
7236       tree asm_specification;
7237       tree attributes;
7238       cp_declarator *declarator;
7239       tree initializer = NULL_TREE;
7240
7241       /* Parse the declarator.  */
7242       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7243                                          /*ctor_dtor_or_conv_p=*/NULL,
7244                                          /*parenthesized_p=*/NULL,
7245                                          /*member_p=*/false);
7246       /* Parse the attributes.  */
7247       attributes = cp_parser_attributes_opt (parser);
7248       /* Parse the asm-specification.  */
7249       asm_specification = cp_parser_asm_specification_opt (parser);
7250       /* If the next token is not an `=' or '{', then we might still be
7251          looking at an expression.  For example:
7252
7253            if (A(a).x)
7254
7255          looks like a decl-specifier-seq and a declarator -- but then
7256          there is no `=', so this is an expression.  */
7257       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7258           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7259         cp_parser_simulate_error (parser);
7260         
7261       /* If we did see an `=' or '{', then we are looking at a declaration
7262          for sure.  */
7263       if (cp_parser_parse_definitely (parser))
7264         {
7265           tree pushed_scope;
7266           bool non_constant_p;
7267           bool flags = LOOKUP_ONLYCONVERTING;
7268
7269           /* Create the declaration.  */
7270           decl = start_decl (declarator, &type_specifiers,
7271                              /*initialized_p=*/true,
7272                              attributes, /*prefix_attributes=*/NULL_TREE,
7273                              &pushed_scope);
7274
7275           /* Parse the initializer.  */
7276           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7277             {
7278               initializer = cp_parser_braced_list (parser, &non_constant_p);
7279               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7280               flags = 0;
7281             }
7282           else
7283             {
7284               /* Consume the `='.  */
7285               cp_lexer_consume_token (parser->lexer);
7286               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7287             }
7288           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7289             maybe_warn_cpp0x ("extended initializer lists");
7290
7291           if (!non_constant_p)
7292             initializer = fold_non_dependent_expr (initializer);
7293
7294           /* Process the initializer.  */
7295           cp_finish_decl (decl,
7296                           initializer, !non_constant_p,
7297                           asm_specification,
7298                           flags);
7299
7300           if (pushed_scope)
7301             pop_scope (pushed_scope);
7302
7303           return convert_from_reference (decl);
7304         }
7305     }
7306   /* If we didn't even get past the declarator successfully, we are
7307      definitely not looking at a declaration.  */
7308   else
7309     cp_parser_abort_tentative_parse (parser);
7310
7311   /* Otherwise, we are looking at an expression.  */
7312   return cp_parser_expression (parser, /*cast_p=*/false);
7313 }
7314
7315 /* We check for a ) immediately followed by ; with no whitespacing
7316    between.  This is used to issue a warning for:
7317
7318      while (...);
7319
7320    and:
7321
7322      for (...);
7323
7324    as the semicolon is probably extraneous.
7325
7326    On parse errors, the next token might not be a ), so do nothing in
7327    that case. */
7328
7329 static void
7330 check_empty_body (cp_parser* parser, const char* type)
7331 {
7332   cp_token *token;
7333   cp_token *close_paren;
7334   expanded_location close_loc;
7335   expanded_location semi_loc;
7336   
7337   close_paren = cp_lexer_peek_token (parser->lexer);
7338   if (close_paren->type != CPP_CLOSE_PAREN)
7339     return;
7340
7341   close_loc = expand_location (close_paren->location);
7342   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7343
7344   if (token->type != CPP_SEMICOLON
7345       || (token->flags & PREV_WHITE))
7346     return;
7347
7348   semi_loc =  expand_location (token->location);
7349   if (close_loc.line == semi_loc.line
7350       && close_loc.column+1 == semi_loc.column)
7351     warning (OPT_Wempty_body,
7352              "suggest a space before %<;%> or explicit braces around empty "
7353              "body in %<%s%> statement",
7354              type);
7355 }
7356
7357 /* Parse an iteration-statement.
7358
7359    iteration-statement:
7360      while ( condition ) statement
7361      do statement while ( expression ) ;
7362      for ( for-init-statement condition [opt] ; expression [opt] )
7363        statement
7364
7365    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7366
7367 static tree
7368 cp_parser_iteration_statement (cp_parser* parser)
7369 {
7370   cp_token *token;
7371   enum rid keyword;
7372   tree statement;
7373   unsigned char in_statement;
7374
7375   /* Peek at the next token.  */
7376   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7377   if (!token)
7378     return error_mark_node;
7379
7380   /* Remember whether or not we are already within an iteration
7381      statement.  */
7382   in_statement = parser->in_statement;
7383
7384   /* See what kind of keyword it is.  */
7385   keyword = token->keyword;
7386   switch (keyword)
7387     {
7388     case RID_WHILE:
7389       {
7390         tree condition;
7391
7392         /* Begin the while-statement.  */
7393         statement = begin_while_stmt ();
7394         /* Look for the `('.  */
7395         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7396         /* Parse the condition.  */
7397         condition = cp_parser_condition (parser);
7398         finish_while_stmt_cond (condition, statement);
7399         check_empty_body (parser, "while");
7400         /* Look for the `)'.  */
7401         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7402         /* Parse the dependent statement.  */
7403         parser->in_statement = IN_ITERATION_STMT;
7404         cp_parser_already_scoped_statement (parser);
7405         parser->in_statement = in_statement;
7406         /* We're done with the while-statement.  */
7407         finish_while_stmt (statement);
7408       }
7409       break;
7410
7411     case RID_DO:
7412       {
7413         tree expression;
7414
7415         /* Begin the do-statement.  */
7416         statement = begin_do_stmt ();
7417         /* Parse the body of the do-statement.  */
7418         parser->in_statement = IN_ITERATION_STMT;
7419         cp_parser_implicitly_scoped_statement (parser, NULL);
7420         parser->in_statement = in_statement;
7421         finish_do_body (statement);
7422         /* Look for the `while' keyword.  */
7423         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7424         /* Look for the `('.  */
7425         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7426         /* Parse the expression.  */
7427         expression = cp_parser_expression (parser, /*cast_p=*/false);
7428         /* We're done with the do-statement.  */
7429         finish_do_stmt (expression, statement);
7430         /* Look for the `)'.  */
7431         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7432         /* Look for the `;'.  */
7433         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7434       }
7435       break;
7436
7437     case RID_FOR:
7438       {
7439         tree condition = NULL_TREE;
7440         tree expression = NULL_TREE;
7441
7442         /* Begin the for-statement.  */
7443         statement = begin_for_stmt ();
7444         /* Look for the `('.  */
7445         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7446         /* Parse the initialization.  */
7447         cp_parser_for_init_statement (parser);
7448         finish_for_init_stmt (statement);
7449
7450         /* If there's a condition, process it.  */
7451         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7452           condition = cp_parser_condition (parser);
7453         finish_for_cond (condition, statement);
7454         /* Look for the `;'.  */
7455         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7456
7457         /* If there's an expression, process it.  */
7458         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7459           expression = cp_parser_expression (parser, /*cast_p=*/false);
7460         finish_for_expr (expression, statement);
7461         check_empty_body (parser, "for");
7462         /* Look for the `)'.  */
7463         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7464
7465         /* Parse the body of the for-statement.  */
7466         parser->in_statement = IN_ITERATION_STMT;
7467         cp_parser_already_scoped_statement (parser);
7468         parser->in_statement = in_statement;
7469
7470         /* We're done with the for-statement.  */
7471         finish_for_stmt (statement);
7472       }
7473       break;
7474
7475     default:
7476       cp_parser_error (parser, "expected iteration-statement");
7477       statement = error_mark_node;
7478       break;
7479     }
7480
7481   return statement;
7482 }
7483
7484 /* Parse a for-init-statement.
7485
7486    for-init-statement:
7487      expression-statement
7488      simple-declaration  */
7489
7490 static void
7491 cp_parser_for_init_statement (cp_parser* parser)
7492 {
7493   /* If the next token is a `;', then we have an empty
7494      expression-statement.  Grammatically, this is also a
7495      simple-declaration, but an invalid one, because it does not
7496      declare anything.  Therefore, if we did not handle this case
7497      specially, we would issue an error message about an invalid
7498      declaration.  */
7499   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7500     {
7501       /* We're going to speculatively look for a declaration, falling back
7502          to an expression, if necessary.  */
7503       cp_parser_parse_tentatively (parser);
7504       /* Parse the declaration.  */
7505       cp_parser_simple_declaration (parser,
7506                                     /*function_definition_allowed_p=*/false);
7507       /* If the tentative parse failed, then we shall need to look for an
7508          expression-statement.  */
7509       if (cp_parser_parse_definitely (parser))
7510         return;
7511     }
7512
7513   cp_parser_expression_statement (parser, false);
7514 }
7515
7516 /* Parse a jump-statement.
7517
7518    jump-statement:
7519      break ;
7520      continue ;
7521      return expression [opt] ;
7522      return braced-init-list ;
7523      goto identifier ;
7524
7525    GNU extension:
7526
7527    jump-statement:
7528      goto * expression ;
7529
7530    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7531
7532 static tree
7533 cp_parser_jump_statement (cp_parser* parser)
7534 {
7535   tree statement = error_mark_node;
7536   cp_token *token;
7537   enum rid keyword;
7538   unsigned char in_statement;
7539
7540   /* Peek at the next token.  */
7541   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7542   if (!token)
7543     return error_mark_node;
7544
7545   /* See what kind of keyword it is.  */
7546   keyword = token->keyword;
7547   switch (keyword)
7548     {
7549     case RID_BREAK:
7550       in_statement = parser->in_statement & ~IN_IF_STMT;      
7551       switch (in_statement)
7552         {
7553         case 0:
7554           error ("%Hbreak statement not within loop or switch", &token->location);
7555           break;
7556         default:
7557           gcc_assert ((in_statement & IN_SWITCH_STMT)
7558                       || in_statement == IN_ITERATION_STMT);
7559           statement = finish_break_stmt ();
7560           break;
7561         case IN_OMP_BLOCK:
7562           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7563           break;
7564         case IN_OMP_FOR:
7565           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7566           break;
7567         }
7568       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7569       break;
7570
7571     case RID_CONTINUE:
7572       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7573         {
7574         case 0:
7575           error ("%Hcontinue statement not within a loop", &token->location);
7576           break;
7577         case IN_ITERATION_STMT:
7578         case IN_OMP_FOR:
7579           statement = finish_continue_stmt ();
7580           break;
7581         case IN_OMP_BLOCK:
7582           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7583           break;
7584         default:
7585           gcc_unreachable ();
7586         }
7587       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7588       break;
7589
7590     case RID_RETURN:
7591       {
7592         tree expr;
7593         bool expr_non_constant_p;
7594
7595         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7596           {
7597             maybe_warn_cpp0x ("extended initializer lists");
7598             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7599           }
7600         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7601           expr = cp_parser_expression (parser, /*cast_p=*/false);
7602         else
7603           /* If the next token is a `;', then there is no
7604              expression.  */
7605           expr = NULL_TREE;
7606         /* Build the return-statement.  */
7607         statement = finish_return_stmt (expr);
7608         /* Look for the final `;'.  */
7609         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7610       }
7611       break;
7612
7613     case RID_GOTO:
7614       /* Create the goto-statement.  */
7615       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7616         {
7617           /* Issue a warning about this use of a GNU extension.  */
7618           if (pedantic)
7619             pedwarn ("%HISO C++ forbids computed gotos", &token->location);
7620           /* Consume the '*' token.  */
7621           cp_lexer_consume_token (parser->lexer);
7622           /* Parse the dependent expression.  */
7623           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7624         }
7625       else
7626         finish_goto_stmt (cp_parser_identifier (parser));
7627       /* Look for the final `;'.  */
7628       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7629       break;
7630
7631     default:
7632       cp_parser_error (parser, "expected jump-statement");
7633       break;
7634     }
7635
7636   return statement;
7637 }
7638
7639 /* Parse a declaration-statement.
7640
7641    declaration-statement:
7642      block-declaration  */
7643
7644 static void
7645 cp_parser_declaration_statement (cp_parser* parser)
7646 {
7647   void *p;
7648
7649   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7650   p = obstack_alloc (&declarator_obstack, 0);
7651
7652  /* Parse the block-declaration.  */
7653   cp_parser_block_declaration (parser, /*statement_p=*/true);
7654
7655   /* Free any declarators allocated.  */
7656   obstack_free (&declarator_obstack, p);
7657
7658   /* Finish off the statement.  */
7659   finish_stmt ();
7660 }
7661
7662 /* Some dependent statements (like `if (cond) statement'), are
7663    implicitly in their own scope.  In other words, if the statement is
7664    a single statement (as opposed to a compound-statement), it is
7665    none-the-less treated as if it were enclosed in braces.  Any
7666    declarations appearing in the dependent statement are out of scope
7667    after control passes that point.  This function parses a statement,
7668    but ensures that is in its own scope, even if it is not a
7669    compound-statement.
7670
7671    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7672    is a (possibly labeled) if statement which is not enclosed in
7673    braces and has an else clause.  This is used to implement
7674    -Wparentheses.
7675
7676    Returns the new statement.  */
7677
7678 static tree
7679 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7680 {
7681   tree statement;
7682
7683   if (if_p != NULL)
7684     *if_p = false;
7685
7686   /* Mark if () ; with a special NOP_EXPR.  */
7687   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7688     {
7689       cp_lexer_consume_token (parser->lexer);
7690       statement = add_stmt (build_empty_stmt ());
7691     }
7692   /* if a compound is opened, we simply parse the statement directly.  */
7693   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7694     statement = cp_parser_compound_statement (parser, NULL, false);
7695   /* If the token is not a `{', then we must take special action.  */
7696   else
7697     {
7698       /* Create a compound-statement.  */
7699       statement = begin_compound_stmt (0);
7700       /* Parse the dependent-statement.  */
7701       cp_parser_statement (parser, NULL_TREE, false, if_p);
7702       /* Finish the dummy compound-statement.  */
7703       finish_compound_stmt (statement);
7704     }
7705
7706   /* Return the statement.  */
7707   return statement;
7708 }
7709
7710 /* For some dependent statements (like `while (cond) statement'), we
7711    have already created a scope.  Therefore, even if the dependent
7712    statement is a compound-statement, we do not want to create another
7713    scope.  */
7714
7715 static void
7716 cp_parser_already_scoped_statement (cp_parser* parser)
7717 {
7718   /* If the token is a `{', then we must take special action.  */
7719   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7720     cp_parser_statement (parser, NULL_TREE, false, NULL);
7721   else
7722     {
7723       /* Avoid calling cp_parser_compound_statement, so that we
7724          don't create a new scope.  Do everything else by hand.  */
7725       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7726       cp_parser_statement_seq_opt (parser, NULL_TREE);
7727       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7728     }
7729 }
7730
7731 /* Declarations [gram.dcl.dcl] */
7732
7733 /* Parse an optional declaration-sequence.
7734
7735    declaration-seq:
7736      declaration
7737      declaration-seq declaration  */
7738
7739 static void
7740 cp_parser_declaration_seq_opt (cp_parser* parser)
7741 {
7742   while (true)
7743     {
7744       cp_token *token;
7745
7746       token = cp_lexer_peek_token (parser->lexer);
7747
7748       if (token->type == CPP_CLOSE_BRACE
7749           || token->type == CPP_EOF
7750           || token->type == CPP_PRAGMA_EOL)
7751         break;
7752
7753       if (token->type == CPP_SEMICOLON)
7754         {
7755           /* A declaration consisting of a single semicolon is
7756              invalid.  Allow it unless we're being pedantic.  */
7757           cp_lexer_consume_token (parser->lexer);
7758           if (pedantic && !in_system_header)
7759             pedwarn ("extra %<;%>");
7760           continue;
7761         }
7762
7763       /* If we're entering or exiting a region that's implicitly
7764          extern "C", modify the lang context appropriately.  */
7765       if (!parser->implicit_extern_c && token->implicit_extern_c)
7766         {
7767           push_lang_context (lang_name_c);
7768           parser->implicit_extern_c = true;
7769         }
7770       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7771         {
7772           pop_lang_context ();
7773           parser->implicit_extern_c = false;
7774         }
7775
7776       if (token->type == CPP_PRAGMA)
7777         {
7778           /* A top-level declaration can consist solely of a #pragma.
7779              A nested declaration cannot, so this is done here and not
7780              in cp_parser_declaration.  (A #pragma at block scope is
7781              handled in cp_parser_statement.)  */
7782           cp_parser_pragma (parser, pragma_external);
7783           continue;
7784         }
7785
7786       /* Parse the declaration itself.  */
7787       cp_parser_declaration (parser);
7788     }
7789 }
7790
7791 /* Parse a declaration.
7792
7793    declaration:
7794      block-declaration
7795      function-definition
7796      template-declaration
7797      explicit-instantiation
7798      explicit-specialization
7799      linkage-specification
7800      namespace-definition
7801
7802    GNU extension:
7803
7804    declaration:
7805       __extension__ declaration */
7806
7807 static void
7808 cp_parser_declaration (cp_parser* parser)
7809 {
7810   cp_token token1;
7811   cp_token token2;
7812   int saved_pedantic;
7813   void *p;
7814
7815   /* Check for the `__extension__' keyword.  */
7816   if (cp_parser_extension_opt (parser, &saved_pedantic))
7817     {
7818       /* Parse the qualified declaration.  */
7819       cp_parser_declaration (parser);
7820       /* Restore the PEDANTIC flag.  */
7821       pedantic = saved_pedantic;
7822
7823       return;
7824     }
7825
7826   /* Try to figure out what kind of declaration is present.  */
7827   token1 = *cp_lexer_peek_token (parser->lexer);
7828
7829   if (token1.type != CPP_EOF)
7830     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7831   else
7832     {
7833       token2.type = CPP_EOF;
7834       token2.keyword = RID_MAX;
7835     }
7836
7837   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7838   p = obstack_alloc (&declarator_obstack, 0);
7839
7840   /* If the next token is `extern' and the following token is a string
7841      literal, then we have a linkage specification.  */
7842   if (token1.keyword == RID_EXTERN
7843       && cp_parser_is_string_literal (&token2))
7844     cp_parser_linkage_specification (parser);
7845   /* If the next token is `template', then we have either a template
7846      declaration, an explicit instantiation, or an explicit
7847      specialization.  */
7848   else if (token1.keyword == RID_TEMPLATE)
7849     {
7850       /* `template <>' indicates a template specialization.  */
7851       if (token2.type == CPP_LESS
7852           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7853         cp_parser_explicit_specialization (parser);
7854       /* `template <' indicates a template declaration.  */
7855       else if (token2.type == CPP_LESS)
7856         cp_parser_template_declaration (parser, /*member_p=*/false);
7857       /* Anything else must be an explicit instantiation.  */
7858       else
7859         cp_parser_explicit_instantiation (parser);
7860     }
7861   /* If the next token is `export', then we have a template
7862      declaration.  */
7863   else if (token1.keyword == RID_EXPORT)
7864     cp_parser_template_declaration (parser, /*member_p=*/false);
7865   /* If the next token is `extern', 'static' or 'inline' and the one
7866      after that is `template', we have a GNU extended explicit
7867      instantiation directive.  */
7868   else if (cp_parser_allow_gnu_extensions_p (parser)
7869            && (token1.keyword == RID_EXTERN
7870                || token1.keyword == RID_STATIC
7871                || token1.keyword == RID_INLINE)
7872            && token2.keyword == RID_TEMPLATE)
7873     cp_parser_explicit_instantiation (parser);
7874   /* If the next token is `namespace', check for a named or unnamed
7875      namespace definition.  */
7876   else if (token1.keyword == RID_NAMESPACE
7877            && (/* A named namespace definition.  */
7878                (token2.type == CPP_NAME
7879                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7880                     != CPP_EQ))
7881                /* An unnamed namespace definition.  */
7882                || token2.type == CPP_OPEN_BRACE
7883                || token2.keyword == RID_ATTRIBUTE))
7884     cp_parser_namespace_definition (parser);
7885   /* An inline (associated) namespace definition.  */
7886   else if (token1.keyword == RID_INLINE
7887            && token2.keyword == RID_NAMESPACE)
7888     cp_parser_namespace_definition (parser);
7889   /* Objective-C++ declaration/definition.  */
7890   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7891     cp_parser_objc_declaration (parser);
7892   /* We must have either a block declaration or a function
7893      definition.  */
7894   else
7895     /* Try to parse a block-declaration, or a function-definition.  */
7896     cp_parser_block_declaration (parser, /*statement_p=*/false);
7897
7898   /* Free any declarators allocated.  */
7899   obstack_free (&declarator_obstack, p);
7900 }
7901
7902 /* Parse a block-declaration.
7903
7904    block-declaration:
7905      simple-declaration
7906      asm-definition
7907      namespace-alias-definition
7908      using-declaration
7909      using-directive
7910
7911    GNU Extension:
7912
7913    block-declaration:
7914      __extension__ block-declaration
7915
7916    C++0x Extension:
7917
7918    block-declaration:
7919      static_assert-declaration
7920
7921    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7922    part of a declaration-statement.  */
7923
7924 static void
7925 cp_parser_block_declaration (cp_parser *parser,
7926                              bool      statement_p)
7927 {
7928   cp_token *token1;
7929   int saved_pedantic;
7930
7931   /* Check for the `__extension__' keyword.  */
7932   if (cp_parser_extension_opt (parser, &saved_pedantic))
7933     {
7934       /* Parse the qualified declaration.  */
7935       cp_parser_block_declaration (parser, statement_p);
7936       /* Restore the PEDANTIC flag.  */
7937       pedantic = saved_pedantic;
7938
7939       return;
7940     }
7941
7942   /* Peek at the next token to figure out which kind of declaration is
7943      present.  */
7944   token1 = cp_lexer_peek_token (parser->lexer);
7945
7946   /* If the next keyword is `asm', we have an asm-definition.  */
7947   if (token1->keyword == RID_ASM)
7948     {
7949       if (statement_p)
7950         cp_parser_commit_to_tentative_parse (parser);
7951       cp_parser_asm_definition (parser);
7952     }
7953   /* If the next keyword is `namespace', we have a
7954      namespace-alias-definition.  */
7955   else if (token1->keyword == RID_NAMESPACE)
7956     cp_parser_namespace_alias_definition (parser);
7957   /* If the next keyword is `using', we have either a
7958      using-declaration or a using-directive.  */
7959   else if (token1->keyword == RID_USING)
7960     {
7961       cp_token *token2;
7962
7963       if (statement_p)
7964         cp_parser_commit_to_tentative_parse (parser);
7965       /* If the token after `using' is `namespace', then we have a
7966          using-directive.  */
7967       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7968       if (token2->keyword == RID_NAMESPACE)
7969         cp_parser_using_directive (parser);
7970       /* Otherwise, it's a using-declaration.  */
7971       else
7972         cp_parser_using_declaration (parser,
7973                                      /*access_declaration_p=*/false);
7974     }
7975   /* If the next keyword is `__label__' we have a misplaced label
7976      declaration.  */
7977   else if (token1->keyword == RID_LABEL)
7978     {
7979       cp_lexer_consume_token (parser->lexer);
7980       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
7981       cp_parser_skip_to_end_of_statement (parser);
7982       /* If the next token is now a `;', consume it.  */
7983       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7984         cp_lexer_consume_token (parser->lexer);
7985     }
7986   /* If the next token is `static_assert' we have a static assertion.  */
7987   else if (token1->keyword == RID_STATIC_ASSERT)
7988     cp_parser_static_assert (parser, /*member_p=*/false);
7989   /* Anything else must be a simple-declaration.  */
7990   else
7991     cp_parser_simple_declaration (parser, !statement_p);
7992 }
7993
7994 /* Parse a simple-declaration.
7995
7996    simple-declaration:
7997      decl-specifier-seq [opt] init-declarator-list [opt] ;
7998
7999    init-declarator-list:
8000      init-declarator
8001      init-declarator-list , init-declarator
8002
8003    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8004    function-definition as a simple-declaration.  */
8005
8006 static void
8007 cp_parser_simple_declaration (cp_parser* parser,
8008                               bool function_definition_allowed_p)
8009 {
8010   cp_decl_specifier_seq decl_specifiers;
8011   int declares_class_or_enum;
8012   bool saw_declarator;
8013
8014   /* Defer access checks until we know what is being declared; the
8015      checks for names appearing in the decl-specifier-seq should be
8016      done as if we were in the scope of the thing being declared.  */
8017   push_deferring_access_checks (dk_deferred);
8018
8019   /* Parse the decl-specifier-seq.  We have to keep track of whether
8020      or not the decl-specifier-seq declares a named class or
8021      enumeration type, since that is the only case in which the
8022      init-declarator-list is allowed to be empty.
8023
8024      [dcl.dcl]
8025
8026      In a simple-declaration, the optional init-declarator-list can be
8027      omitted only when declaring a class or enumeration, that is when
8028      the decl-specifier-seq contains either a class-specifier, an
8029      elaborated-type-specifier, or an enum-specifier.  */
8030   cp_parser_decl_specifier_seq (parser,
8031                                 CP_PARSER_FLAGS_OPTIONAL,
8032                                 &decl_specifiers,
8033                                 &declares_class_or_enum);
8034   /* We no longer need to defer access checks.  */
8035   stop_deferring_access_checks ();
8036
8037   /* In a block scope, a valid declaration must always have a
8038      decl-specifier-seq.  By not trying to parse declarators, we can
8039      resolve the declaration/expression ambiguity more quickly.  */
8040   if (!function_definition_allowed_p
8041       && !decl_specifiers.any_specifiers_p)
8042     {
8043       cp_parser_error (parser, "expected declaration");
8044       goto done;
8045     }
8046
8047   /* If the next two tokens are both identifiers, the code is
8048      erroneous. The usual cause of this situation is code like:
8049
8050        T t;
8051
8052      where "T" should name a type -- but does not.  */
8053   if (!decl_specifiers.type
8054       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8055     {
8056       /* If parsing tentatively, we should commit; we really are
8057          looking at a declaration.  */
8058       cp_parser_commit_to_tentative_parse (parser);
8059       /* Give up.  */
8060       goto done;
8061     }
8062
8063   /* If we have seen at least one decl-specifier, and the next token
8064      is not a parenthesis, then we must be looking at a declaration.
8065      (After "int (" we might be looking at a functional cast.)  */
8066   if (decl_specifiers.any_specifiers_p
8067       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8068       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8069     cp_parser_commit_to_tentative_parse (parser);
8070
8071   /* Keep going until we hit the `;' at the end of the simple
8072      declaration.  */
8073   saw_declarator = false;
8074   while (cp_lexer_next_token_is_not (parser->lexer,
8075                                      CPP_SEMICOLON))
8076     {
8077       cp_token *token;
8078       bool function_definition_p;
8079       tree decl;
8080
8081       if (saw_declarator)
8082         {
8083           /* If we are processing next declarator, coma is expected */
8084           token = cp_lexer_peek_token (parser->lexer);
8085           gcc_assert (token->type == CPP_COMMA);
8086           cp_lexer_consume_token (parser->lexer);
8087         }
8088       else
8089         saw_declarator = true;
8090
8091       /* Parse the init-declarator.  */
8092       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8093                                         /*checks=*/NULL,
8094                                         function_definition_allowed_p,
8095                                         /*member_p=*/false,
8096                                         declares_class_or_enum,
8097                                         &function_definition_p);
8098       /* If an error occurred while parsing tentatively, exit quickly.
8099          (That usually happens when in the body of a function; each
8100          statement is treated as a declaration-statement until proven
8101          otherwise.)  */
8102       if (cp_parser_error_occurred (parser))
8103         goto done;
8104       /* Handle function definitions specially.  */
8105       if (function_definition_p)
8106         {
8107           /* If the next token is a `,', then we are probably
8108              processing something like:
8109
8110                void f() {}, *p;
8111
8112              which is erroneous.  */
8113           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8114             {
8115               cp_token *token = cp_lexer_peek_token (parser->lexer);
8116               error ("%Hmixing declarations and function-definitions is forbidden",
8117                      &token->location);
8118             }
8119           /* Otherwise, we're done with the list of declarators.  */
8120           else
8121             {
8122               pop_deferring_access_checks ();
8123               return;
8124             }
8125         }
8126       /* The next token should be either a `,' or a `;'.  */
8127       token = cp_lexer_peek_token (parser->lexer);
8128       /* If it's a `,', there are more declarators to come.  */
8129       if (token->type == CPP_COMMA)
8130         /* will be consumed next time around */;
8131       /* If it's a `;', we are done.  */
8132       else if (token->type == CPP_SEMICOLON)
8133         break;
8134       /* Anything else is an error.  */
8135       else
8136         {
8137           /* If we have already issued an error message we don't need
8138              to issue another one.  */
8139           if (decl != error_mark_node
8140               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8141             cp_parser_error (parser, "expected %<,%> or %<;%>");
8142           /* Skip tokens until we reach the end of the statement.  */
8143           cp_parser_skip_to_end_of_statement (parser);
8144           /* If the next token is now a `;', consume it.  */
8145           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8146             cp_lexer_consume_token (parser->lexer);
8147           goto done;
8148         }
8149       /* After the first time around, a function-definition is not
8150          allowed -- even if it was OK at first.  For example:
8151
8152            int i, f() {}
8153
8154          is not valid.  */
8155       function_definition_allowed_p = false;
8156     }
8157
8158   /* Issue an error message if no declarators are present, and the
8159      decl-specifier-seq does not itself declare a class or
8160      enumeration.  */
8161   if (!saw_declarator)
8162     {
8163       if (cp_parser_declares_only_class_p (parser))
8164         shadow_tag (&decl_specifiers);
8165       /* Perform any deferred access checks.  */
8166       perform_deferred_access_checks ();
8167     }
8168
8169   /* Consume the `;'.  */
8170   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8171
8172  done:
8173   pop_deferring_access_checks ();
8174 }
8175
8176 /* Parse a decl-specifier-seq.
8177
8178    decl-specifier-seq:
8179      decl-specifier-seq [opt] decl-specifier
8180
8181    decl-specifier:
8182      storage-class-specifier
8183      type-specifier
8184      function-specifier
8185      friend
8186      typedef
8187
8188    GNU Extension:
8189
8190    decl-specifier:
8191      attributes
8192
8193    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8194
8195    The parser flags FLAGS is used to control type-specifier parsing.
8196
8197    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8198    flags:
8199
8200      1: one of the decl-specifiers is an elaborated-type-specifier
8201         (i.e., a type declaration)
8202      2: one of the decl-specifiers is an enum-specifier or a
8203         class-specifier (i.e., a type definition)
8204
8205    */
8206
8207 static void
8208 cp_parser_decl_specifier_seq (cp_parser* parser,
8209                               cp_parser_flags flags,
8210                               cp_decl_specifier_seq *decl_specs,
8211                               int* declares_class_or_enum)
8212 {
8213   bool constructor_possible_p = !parser->in_declarator_p;
8214   cp_token *start_token = NULL;
8215
8216   /* Clear DECL_SPECS.  */
8217   clear_decl_specs (decl_specs);
8218
8219   /* Assume no class or enumeration type is declared.  */
8220   *declares_class_or_enum = 0;
8221
8222   /* Keep reading specifiers until there are no more to read.  */
8223   while (true)
8224     {
8225       bool constructor_p;
8226       bool found_decl_spec;
8227       cp_token *token;
8228
8229       /* Peek at the next token.  */
8230       token = cp_lexer_peek_token (parser->lexer);
8231
8232       /* Save the first token of the decl spec list for error
8233          reporting.  */
8234       if (!start_token)
8235         start_token = token;
8236       /* Handle attributes.  */
8237       if (token->keyword == RID_ATTRIBUTE)
8238         {
8239           /* Parse the attributes.  */
8240           decl_specs->attributes
8241             = chainon (decl_specs->attributes,
8242                        cp_parser_attributes_opt (parser));
8243           continue;
8244         }
8245       /* Assume we will find a decl-specifier keyword.  */
8246       found_decl_spec = true;
8247       /* If the next token is an appropriate keyword, we can simply
8248          add it to the list.  */
8249       switch (token->keyword)
8250         {
8251           /* decl-specifier:
8252                friend  */
8253         case RID_FRIEND:
8254           if (!at_class_scope_p ())
8255             {
8256               error ("%H%<friend%> used outside of class", &token->location);
8257               cp_lexer_purge_token (parser->lexer);
8258             }
8259           else
8260             {
8261               ++decl_specs->specs[(int) ds_friend];
8262               /* Consume the token.  */
8263               cp_lexer_consume_token (parser->lexer);
8264             }
8265           break;
8266
8267           /* function-specifier:
8268                inline
8269                virtual
8270                explicit  */
8271         case RID_INLINE:
8272         case RID_VIRTUAL:
8273         case RID_EXPLICIT:
8274           cp_parser_function_specifier_opt (parser, decl_specs);
8275           break;
8276
8277           /* decl-specifier:
8278                typedef  */
8279         case RID_TYPEDEF:
8280           ++decl_specs->specs[(int) ds_typedef];
8281           /* Consume the token.  */
8282           cp_lexer_consume_token (parser->lexer);
8283           /* A constructor declarator cannot appear in a typedef.  */
8284           constructor_possible_p = false;
8285           /* The "typedef" keyword can only occur in a declaration; we
8286              may as well commit at this point.  */
8287           cp_parser_commit_to_tentative_parse (parser);
8288
8289           if (decl_specs->storage_class != sc_none)
8290             decl_specs->conflicting_specifiers_p = true;
8291           break;
8292
8293           /* storage-class-specifier:
8294                auto
8295                register
8296                static
8297                extern
8298                mutable
8299
8300              GNU Extension:
8301                thread  */
8302         case RID_AUTO:
8303           /* Consume the token.  */
8304           cp_lexer_consume_token (parser->lexer);
8305
8306           if (cxx_dialect == cxx98) 
8307             {
8308               /* Complain about `auto' as a storage specifier, if
8309                  we're complaining about C++0x compatibility.  */
8310               warning 
8311                 (OPT_Wc__0x_compat, 
8312                  "%H%<auto%> will change meaning in C++0x; please remove it",
8313                  &token->location);
8314
8315               /* Set the storage class anyway.  */
8316               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8317                                            token->location);
8318             }
8319           else 
8320             /* We do not yet support the use of `auto' as a
8321                type-specifier.  */
8322             error ("%HC++0x %<auto%> specifier not supported", &token->location);
8323           break;
8324
8325         case RID_REGISTER:
8326         case RID_STATIC:
8327         case RID_EXTERN:
8328         case RID_MUTABLE:
8329           /* Consume the token.  */
8330           cp_lexer_consume_token (parser->lexer);
8331           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8332                                        token->location);
8333           break;
8334         case RID_THREAD:
8335           /* Consume the token.  */
8336           cp_lexer_consume_token (parser->lexer);
8337           ++decl_specs->specs[(int) ds_thread];
8338           break;
8339
8340         default:
8341           /* We did not yet find a decl-specifier yet.  */
8342           found_decl_spec = false;
8343           break;
8344         }
8345
8346       /* Constructors are a special case.  The `S' in `S()' is not a
8347          decl-specifier; it is the beginning of the declarator.  */
8348       constructor_p
8349         = (!found_decl_spec
8350            && constructor_possible_p
8351            && (cp_parser_constructor_declarator_p
8352                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8353
8354       /* If we don't have a DECL_SPEC yet, then we must be looking at
8355          a type-specifier.  */
8356       if (!found_decl_spec && !constructor_p)
8357         {
8358           int decl_spec_declares_class_or_enum;
8359           bool is_cv_qualifier;
8360           tree type_spec;
8361
8362           type_spec
8363             = cp_parser_type_specifier (parser, flags,
8364                                         decl_specs,
8365                                         /*is_declaration=*/true,
8366                                         &decl_spec_declares_class_or_enum,
8367                                         &is_cv_qualifier);
8368           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8369
8370           /* If this type-specifier referenced a user-defined type
8371              (a typedef, class-name, etc.), then we can't allow any
8372              more such type-specifiers henceforth.
8373
8374              [dcl.spec]
8375
8376              The longest sequence of decl-specifiers that could
8377              possibly be a type name is taken as the
8378              decl-specifier-seq of a declaration.  The sequence shall
8379              be self-consistent as described below.
8380
8381              [dcl.type]
8382
8383              As a general rule, at most one type-specifier is allowed
8384              in the complete decl-specifier-seq of a declaration.  The
8385              only exceptions are the following:
8386
8387              -- const or volatile can be combined with any other
8388                 type-specifier.
8389
8390              -- signed or unsigned can be combined with char, long,
8391                 short, or int.
8392
8393              -- ..
8394
8395              Example:
8396
8397                typedef char* Pc;
8398                void g (const int Pc);
8399
8400              Here, Pc is *not* part of the decl-specifier seq; it's
8401              the declarator.  Therefore, once we see a type-specifier
8402              (other than a cv-qualifier), we forbid any additional
8403              user-defined types.  We *do* still allow things like `int
8404              int' to be considered a decl-specifier-seq, and issue the
8405              error message later.  */
8406           if (type_spec && !is_cv_qualifier)
8407             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8408           /* A constructor declarator cannot follow a type-specifier.  */
8409           if (type_spec)
8410             {
8411               constructor_possible_p = false;
8412               found_decl_spec = true;
8413             }
8414         }
8415
8416       /* If we still do not have a DECL_SPEC, then there are no more
8417          decl-specifiers.  */
8418       if (!found_decl_spec)
8419         break;
8420
8421       decl_specs->any_specifiers_p = true;
8422       /* After we see one decl-specifier, further decl-specifiers are
8423          always optional.  */
8424       flags |= CP_PARSER_FLAGS_OPTIONAL;
8425     }
8426
8427   cp_parser_check_decl_spec (decl_specs, start_token->location);
8428
8429   /* Don't allow a friend specifier with a class definition.  */
8430   if (decl_specs->specs[(int) ds_friend] != 0
8431       && (*declares_class_or_enum & 2))
8432     error ("%Hclass definition may not be declared a friend",
8433             &start_token->location);
8434 }
8435
8436 /* Parse an (optional) storage-class-specifier.
8437
8438    storage-class-specifier:
8439      auto
8440      register
8441      static
8442      extern
8443      mutable
8444
8445    GNU Extension:
8446
8447    storage-class-specifier:
8448      thread
8449
8450    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8451
8452 static tree
8453 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8454 {
8455   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8456     {
8457     case RID_AUTO:
8458       if (cxx_dialect != cxx98)
8459         return NULL_TREE;
8460       /* Fall through for C++98.  */
8461
8462     case RID_REGISTER:
8463     case RID_STATIC:
8464     case RID_EXTERN:
8465     case RID_MUTABLE:
8466     case RID_THREAD:
8467       /* Consume the token.  */
8468       return cp_lexer_consume_token (parser->lexer)->u.value;
8469
8470     default:
8471       return NULL_TREE;
8472     }
8473 }
8474
8475 /* Parse an (optional) function-specifier.
8476
8477    function-specifier:
8478      inline
8479      virtual
8480      explicit
8481
8482    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8483    Updates DECL_SPECS, if it is non-NULL.  */
8484
8485 static tree
8486 cp_parser_function_specifier_opt (cp_parser* parser,
8487                                   cp_decl_specifier_seq *decl_specs)
8488 {
8489   cp_token *token = cp_lexer_peek_token (parser->lexer);
8490   switch (token->keyword)
8491     {
8492     case RID_INLINE:
8493       if (decl_specs)
8494         ++decl_specs->specs[(int) ds_inline];
8495       break;
8496
8497     case RID_VIRTUAL:
8498       /* 14.5.2.3 [temp.mem]
8499
8500          A member function template shall not be virtual.  */
8501       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8502         error ("%Htemplates may not be %<virtual%>", &token->location);
8503       else if (decl_specs)
8504         ++decl_specs->specs[(int) ds_virtual];
8505       break;
8506
8507     case RID_EXPLICIT:
8508       if (decl_specs)
8509         ++decl_specs->specs[(int) ds_explicit];
8510       break;
8511
8512     default:
8513       return NULL_TREE;
8514     }
8515
8516   /* Consume the token.  */
8517   return cp_lexer_consume_token (parser->lexer)->u.value;
8518 }
8519
8520 /* Parse a linkage-specification.
8521
8522    linkage-specification:
8523      extern string-literal { declaration-seq [opt] }
8524      extern string-literal declaration  */
8525
8526 static void
8527 cp_parser_linkage_specification (cp_parser* parser)
8528 {
8529   tree linkage;
8530
8531   /* Look for the `extern' keyword.  */
8532   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8533
8534   /* Look for the string-literal.  */
8535   linkage = cp_parser_string_literal (parser, false, false);
8536
8537   /* Transform the literal into an identifier.  If the literal is a
8538      wide-character string, or contains embedded NULs, then we can't
8539      handle it as the user wants.  */
8540   if (strlen (TREE_STRING_POINTER (linkage))
8541       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8542     {
8543       cp_parser_error (parser, "invalid linkage-specification");
8544       /* Assume C++ linkage.  */
8545       linkage = lang_name_cplusplus;
8546     }
8547   else
8548     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8549
8550   /* We're now using the new linkage.  */
8551   push_lang_context (linkage);
8552
8553   /* If the next token is a `{', then we're using the first
8554      production.  */
8555   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8556     {
8557       /* Consume the `{' token.  */
8558       cp_lexer_consume_token (parser->lexer);
8559       /* Parse the declarations.  */
8560       cp_parser_declaration_seq_opt (parser);
8561       /* Look for the closing `}'.  */
8562       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8563     }
8564   /* Otherwise, there's just one declaration.  */
8565   else
8566     {
8567       bool saved_in_unbraced_linkage_specification_p;
8568
8569       saved_in_unbraced_linkage_specification_p
8570         = parser->in_unbraced_linkage_specification_p;
8571       parser->in_unbraced_linkage_specification_p = true;
8572       cp_parser_declaration (parser);
8573       parser->in_unbraced_linkage_specification_p
8574         = saved_in_unbraced_linkage_specification_p;
8575     }
8576
8577   /* We're done with the linkage-specification.  */
8578   pop_lang_context ();
8579 }
8580
8581 /* Parse a static_assert-declaration.
8582
8583    static_assert-declaration:
8584      static_assert ( constant-expression , string-literal ) ; 
8585
8586    If MEMBER_P, this static_assert is a class member.  */
8587
8588 static void 
8589 cp_parser_static_assert(cp_parser *parser, bool member_p)
8590 {
8591   tree condition;
8592   tree message;
8593   cp_token *token;
8594   location_t saved_loc;
8595
8596   /* Peek at the `static_assert' token so we can keep track of exactly
8597      where the static assertion started.  */
8598   token = cp_lexer_peek_token (parser->lexer);
8599   saved_loc = token->location;
8600
8601   /* Look for the `static_assert' keyword.  */
8602   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8603                                   "%<static_assert%>"))
8604     return;
8605
8606   /*  We know we are in a static assertion; commit to any tentative
8607       parse.  */
8608   if (cp_parser_parsing_tentatively (parser))
8609     cp_parser_commit_to_tentative_parse (parser);
8610
8611   /* Parse the `(' starting the static assertion condition.  */
8612   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8613
8614   /* Parse the constant-expression.  */
8615   condition = 
8616     cp_parser_constant_expression (parser,
8617                                    /*allow_non_constant_p=*/false,
8618                                    /*non_constant_p=*/NULL);
8619
8620   /* Parse the separating `,'.  */
8621   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8622
8623   /* Parse the string-literal message.  */
8624   message = cp_parser_string_literal (parser, 
8625                                       /*translate=*/false,
8626                                       /*wide_ok=*/true);
8627
8628   /* A `)' completes the static assertion.  */
8629   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8630     cp_parser_skip_to_closing_parenthesis (parser, 
8631                                            /*recovering=*/true, 
8632                                            /*or_comma=*/false,
8633                                            /*consume_paren=*/true);
8634
8635   /* A semicolon terminates the declaration.  */
8636   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8637
8638   /* Complete the static assertion, which may mean either processing 
8639      the static assert now or saving it for template instantiation.  */
8640   finish_static_assert (condition, message, saved_loc, member_p);
8641 }
8642
8643 /* Parse a `decltype' type. Returns the type. 
8644
8645    simple-type-specifier:
8646      decltype ( expression )  */
8647
8648 static tree
8649 cp_parser_decltype (cp_parser *parser)
8650 {
8651   tree expr;
8652   bool id_expression_or_member_access_p = false;
8653   const char *saved_message;
8654   bool saved_integral_constant_expression_p;
8655   bool saved_non_integral_constant_expression_p;
8656   cp_token *id_expr_start_token;
8657
8658   /* Look for the `decltype' token.  */
8659   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8660     return error_mark_node;
8661
8662   /* Types cannot be defined in a `decltype' expression.  Save away the
8663      old message.  */
8664   saved_message = parser->type_definition_forbidden_message;
8665
8666   /* And create the new one.  */
8667   parser->type_definition_forbidden_message
8668     = "types may not be defined in %<decltype%> expressions";
8669
8670   /* The restrictions on constant-expressions do not apply inside
8671      decltype expressions.  */
8672   saved_integral_constant_expression_p
8673     = parser->integral_constant_expression_p;
8674   saved_non_integral_constant_expression_p
8675     = parser->non_integral_constant_expression_p;
8676   parser->integral_constant_expression_p = false;
8677
8678   /* Do not actually evaluate the expression.  */
8679   ++skip_evaluation;
8680
8681   /* Parse the opening `('.  */
8682   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8683     return error_mark_node;
8684   
8685   /* First, try parsing an id-expression.  */
8686   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8687   cp_parser_parse_tentatively (parser);
8688   expr = cp_parser_id_expression (parser,
8689                                   /*template_keyword_p=*/false,
8690                                   /*check_dependency_p=*/true,
8691                                   /*template_p=*/NULL,
8692                                   /*declarator_p=*/false,
8693                                   /*optional_p=*/false);
8694
8695   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8696     {
8697       bool non_integral_constant_expression_p = false;
8698       tree id_expression = expr;
8699       cp_id_kind idk;
8700       const char *error_msg;
8701
8702       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8703         /* Lookup the name we got back from the id-expression.  */
8704         expr = cp_parser_lookup_name (parser, expr,
8705                                       none_type,
8706                                       /*is_template=*/false,
8707                                       /*is_namespace=*/false,
8708                                       /*check_dependency=*/true,
8709                                       /*ambiguous_decls=*/NULL,
8710                                       id_expr_start_token->location);
8711
8712       if (expr
8713           && expr != error_mark_node
8714           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8715           && TREE_CODE (expr) != TYPE_DECL
8716           && (TREE_CODE (expr) != BIT_NOT_EXPR
8717               || !TYPE_P (TREE_OPERAND (expr, 0)))
8718           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8719         {
8720           /* Complete lookup of the id-expression.  */
8721           expr = (finish_id_expression
8722                   (id_expression, expr, parser->scope, &idk,
8723                    /*integral_constant_expression_p=*/false,
8724                    /*allow_non_integral_constant_expression_p=*/true,
8725                    &non_integral_constant_expression_p,
8726                    /*template_p=*/false,
8727                    /*done=*/true,
8728                    /*address_p=*/false,
8729                    /*template_arg_p=*/false,
8730                    &error_msg,
8731                    id_expr_start_token->location));
8732
8733           if (expr == error_mark_node)
8734             /* We found an id-expression, but it was something that we
8735                should not have found. This is an error, not something
8736                we can recover from, so note that we found an
8737                id-expression and we'll recover as gracefully as
8738                possible.  */
8739             id_expression_or_member_access_p = true;
8740         }
8741
8742       if (expr 
8743           && expr != error_mark_node
8744           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8745         /* We have an id-expression.  */
8746         id_expression_or_member_access_p = true;
8747     }
8748
8749   if (!id_expression_or_member_access_p)
8750     {
8751       /* Abort the id-expression parse.  */
8752       cp_parser_abort_tentative_parse (parser);
8753
8754       /* Parsing tentatively, again.  */
8755       cp_parser_parse_tentatively (parser);
8756
8757       /* Parse a class member access.  */
8758       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8759                                            /*cast_p=*/false,
8760                                            /*member_access_only_p=*/true);
8761
8762       if (expr 
8763           && expr != error_mark_node
8764           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8765         /* We have an id-expression.  */
8766         id_expression_or_member_access_p = true;
8767     }
8768
8769   if (id_expression_or_member_access_p)
8770     /* We have parsed the complete id-expression or member access.  */
8771     cp_parser_parse_definitely (parser);
8772   else
8773     {
8774       /* Abort our attempt to parse an id-expression or member access
8775          expression.  */
8776       cp_parser_abort_tentative_parse (parser);
8777
8778       /* Parse a full expression.  */
8779       expr = cp_parser_expression (parser, /*cast_p=*/false);
8780     }
8781
8782   /* Go back to evaluating expressions.  */
8783   --skip_evaluation;
8784
8785   /* Restore the old message and the integral constant expression
8786      flags.  */
8787   parser->type_definition_forbidden_message = saved_message;
8788   parser->integral_constant_expression_p
8789     = saved_integral_constant_expression_p;
8790   parser->non_integral_constant_expression_p
8791     = saved_non_integral_constant_expression_p;
8792
8793   if (expr == error_mark_node)
8794     {
8795       /* Skip everything up to the closing `)'.  */
8796       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8797                                              /*consume_paren=*/true);
8798       return error_mark_node;
8799     }
8800   
8801   /* Parse to the closing `)'.  */
8802   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8803     {
8804       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8805                                              /*consume_paren=*/true);
8806       return error_mark_node;
8807     }
8808
8809   return finish_decltype_type (expr, id_expression_or_member_access_p);
8810 }
8811
8812 /* Special member functions [gram.special] */
8813
8814 /* Parse a conversion-function-id.
8815
8816    conversion-function-id:
8817      operator conversion-type-id
8818
8819    Returns an IDENTIFIER_NODE representing the operator.  */
8820
8821 static tree
8822 cp_parser_conversion_function_id (cp_parser* parser)
8823 {
8824   tree type;
8825   tree saved_scope;
8826   tree saved_qualifying_scope;
8827   tree saved_object_scope;
8828   tree pushed_scope = NULL_TREE;
8829
8830   /* Look for the `operator' token.  */
8831   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8832     return error_mark_node;
8833   /* When we parse the conversion-type-id, the current scope will be
8834      reset.  However, we need that information in able to look up the
8835      conversion function later, so we save it here.  */
8836   saved_scope = parser->scope;
8837   saved_qualifying_scope = parser->qualifying_scope;
8838   saved_object_scope = parser->object_scope;
8839   /* We must enter the scope of the class so that the names of
8840      entities declared within the class are available in the
8841      conversion-type-id.  For example, consider:
8842
8843        struct S {
8844          typedef int I;
8845          operator I();
8846        };
8847
8848        S::operator I() { ... }
8849
8850      In order to see that `I' is a type-name in the definition, we
8851      must be in the scope of `S'.  */
8852   if (saved_scope)
8853     pushed_scope = push_scope (saved_scope);
8854   /* Parse the conversion-type-id.  */
8855   type = cp_parser_conversion_type_id (parser);
8856   /* Leave the scope of the class, if any.  */
8857   if (pushed_scope)
8858     pop_scope (pushed_scope);
8859   /* Restore the saved scope.  */
8860   parser->scope = saved_scope;
8861   parser->qualifying_scope = saved_qualifying_scope;
8862   parser->object_scope = saved_object_scope;
8863   /* If the TYPE is invalid, indicate failure.  */
8864   if (type == error_mark_node)
8865     return error_mark_node;
8866   return mangle_conv_op_name_for_type (type);
8867 }
8868
8869 /* Parse a conversion-type-id:
8870
8871    conversion-type-id:
8872      type-specifier-seq conversion-declarator [opt]
8873
8874    Returns the TYPE specified.  */
8875
8876 static tree
8877 cp_parser_conversion_type_id (cp_parser* parser)
8878 {
8879   tree attributes;
8880   cp_decl_specifier_seq type_specifiers;
8881   cp_declarator *declarator;
8882   tree type_specified;
8883
8884   /* Parse the attributes.  */
8885   attributes = cp_parser_attributes_opt (parser);
8886   /* Parse the type-specifiers.  */
8887   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8888                                 &type_specifiers);
8889   /* If that didn't work, stop.  */
8890   if (type_specifiers.type == error_mark_node)
8891     return error_mark_node;
8892   /* Parse the conversion-declarator.  */
8893   declarator = cp_parser_conversion_declarator_opt (parser);
8894
8895   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8896                                     /*initialized=*/0, &attributes);
8897   if (attributes)
8898     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8899   return type_specified;
8900 }
8901
8902 /* Parse an (optional) conversion-declarator.
8903
8904    conversion-declarator:
8905      ptr-operator conversion-declarator [opt]
8906
8907    */
8908
8909 static cp_declarator *
8910 cp_parser_conversion_declarator_opt (cp_parser* parser)
8911 {
8912   enum tree_code code;
8913   tree class_type;
8914   cp_cv_quals cv_quals;
8915
8916   /* We don't know if there's a ptr-operator next, or not.  */
8917   cp_parser_parse_tentatively (parser);
8918   /* Try the ptr-operator.  */
8919   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8920   /* If it worked, look for more conversion-declarators.  */
8921   if (cp_parser_parse_definitely (parser))
8922     {
8923       cp_declarator *declarator;
8924
8925       /* Parse another optional declarator.  */
8926       declarator = cp_parser_conversion_declarator_opt (parser);
8927
8928       return cp_parser_make_indirect_declarator
8929         (code, class_type, cv_quals, declarator);
8930    }
8931
8932   return NULL;
8933 }
8934
8935 /* Parse an (optional) ctor-initializer.
8936
8937    ctor-initializer:
8938      : mem-initializer-list
8939
8940    Returns TRUE iff the ctor-initializer was actually present.  */
8941
8942 static bool
8943 cp_parser_ctor_initializer_opt (cp_parser* parser)
8944 {
8945   /* If the next token is not a `:', then there is no
8946      ctor-initializer.  */
8947   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8948     {
8949       /* Do default initialization of any bases and members.  */
8950       if (DECL_CONSTRUCTOR_P (current_function_decl))
8951         finish_mem_initializers (NULL_TREE);
8952
8953       return false;
8954     }
8955
8956   /* Consume the `:' token.  */
8957   cp_lexer_consume_token (parser->lexer);
8958   /* And the mem-initializer-list.  */
8959   cp_parser_mem_initializer_list (parser);
8960
8961   return true;
8962 }
8963
8964 /* Parse a mem-initializer-list.
8965
8966    mem-initializer-list:
8967      mem-initializer ... [opt]
8968      mem-initializer ... [opt] , mem-initializer-list  */
8969
8970 static void
8971 cp_parser_mem_initializer_list (cp_parser* parser)
8972 {
8973   tree mem_initializer_list = NULL_TREE;
8974   cp_token *token = cp_lexer_peek_token (parser->lexer);
8975
8976   /* Let the semantic analysis code know that we are starting the
8977      mem-initializer-list.  */
8978   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8979     error ("%Honly constructors take base initializers",
8980            &token->location);
8981
8982   /* Loop through the list.  */
8983   while (true)
8984     {
8985       tree mem_initializer;
8986
8987       token = cp_lexer_peek_token (parser->lexer);
8988       /* Parse the mem-initializer.  */
8989       mem_initializer = cp_parser_mem_initializer (parser);
8990       /* If the next token is a `...', we're expanding member initializers. */
8991       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8992         {
8993           /* Consume the `...'. */
8994           cp_lexer_consume_token (parser->lexer);
8995
8996           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8997              can be expanded but members cannot. */
8998           if (mem_initializer != error_mark_node
8999               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9000             {
9001               error ("%Hcannot expand initializer for member %<%D%>",
9002                      &token->location, TREE_PURPOSE (mem_initializer));
9003               mem_initializer = error_mark_node;
9004             }
9005
9006           /* Construct the pack expansion type. */
9007           if (mem_initializer != error_mark_node)
9008             mem_initializer = make_pack_expansion (mem_initializer);
9009         }
9010       /* Add it to the list, unless it was erroneous.  */
9011       if (mem_initializer != error_mark_node)
9012         {
9013           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9014           mem_initializer_list = mem_initializer;
9015         }
9016       /* If the next token is not a `,', we're done.  */
9017       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9018         break;
9019       /* Consume the `,' token.  */
9020       cp_lexer_consume_token (parser->lexer);
9021     }
9022
9023   /* Perform semantic analysis.  */
9024   if (DECL_CONSTRUCTOR_P (current_function_decl))
9025     finish_mem_initializers (mem_initializer_list);
9026 }
9027
9028 /* Parse a mem-initializer.
9029
9030    mem-initializer:
9031      mem-initializer-id ( expression-list [opt] )
9032      mem-initializer-id braced-init-list
9033
9034    GNU extension:
9035
9036    mem-initializer:
9037      ( expression-list [opt] )
9038
9039    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9040    class) or FIELD_DECL (for a non-static data member) to initialize;
9041    the TREE_VALUE is the expression-list.  An empty initialization
9042    list is represented by void_list_node.  */
9043
9044 static tree
9045 cp_parser_mem_initializer (cp_parser* parser)
9046 {
9047   tree mem_initializer_id;
9048   tree expression_list;
9049   tree member;
9050   cp_token *token = cp_lexer_peek_token (parser->lexer);
9051
9052   /* Find out what is being initialized.  */
9053   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9054     {
9055       permerror ("%Hanachronistic old-style base class initializer",
9056                  &token->location);
9057       mem_initializer_id = NULL_TREE;
9058     }
9059   else
9060     mem_initializer_id = cp_parser_mem_initializer_id (parser);
9061   member = expand_member_init (mem_initializer_id);
9062   if (member && !DECL_P (member))
9063     in_base_initializer = 1;
9064
9065   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9066     {
9067       bool expr_non_constant_p;
9068       maybe_warn_cpp0x ("extended initializer lists");
9069       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9070       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9071       expression_list = build_tree_list (NULL_TREE, expression_list);
9072     }
9073   else
9074     expression_list
9075       = cp_parser_parenthesized_expression_list (parser, false,
9076                                                  /*cast_p=*/false,
9077                                                  /*allow_expansion_p=*/true,
9078                                                  /*non_constant_p=*/NULL);
9079   if (expression_list == error_mark_node)
9080     return error_mark_node;
9081   if (!expression_list)
9082     expression_list = void_type_node;
9083
9084   in_base_initializer = 0;
9085
9086   return member ? build_tree_list (member, expression_list) : error_mark_node;
9087 }
9088
9089 /* Parse a mem-initializer-id.
9090
9091    mem-initializer-id:
9092      :: [opt] nested-name-specifier [opt] class-name
9093      identifier
9094
9095    Returns a TYPE indicating the class to be initializer for the first
9096    production.  Returns an IDENTIFIER_NODE indicating the data member
9097    to be initialized for the second production.  */
9098
9099 static tree
9100 cp_parser_mem_initializer_id (cp_parser* parser)
9101 {
9102   bool global_scope_p;
9103   bool nested_name_specifier_p;
9104   bool template_p = false;
9105   tree id;
9106
9107   cp_token *token = cp_lexer_peek_token (parser->lexer);
9108
9109   /* `typename' is not allowed in this context ([temp.res]).  */
9110   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9111     {
9112       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9113              "member initializer is implicitly a type)",
9114              &token->location);
9115       cp_lexer_consume_token (parser->lexer);
9116     }
9117   /* Look for the optional `::' operator.  */
9118   global_scope_p
9119     = (cp_parser_global_scope_opt (parser,
9120                                    /*current_scope_valid_p=*/false)
9121        != NULL_TREE);
9122   /* Look for the optional nested-name-specifier.  The simplest way to
9123      implement:
9124
9125        [temp.res]
9126
9127        The keyword `typename' is not permitted in a base-specifier or
9128        mem-initializer; in these contexts a qualified name that
9129        depends on a template-parameter is implicitly assumed to be a
9130        type name.
9131
9132      is to assume that we have seen the `typename' keyword at this
9133      point.  */
9134   nested_name_specifier_p
9135     = (cp_parser_nested_name_specifier_opt (parser,
9136                                             /*typename_keyword_p=*/true,
9137                                             /*check_dependency_p=*/true,
9138                                             /*type_p=*/true,
9139                                             /*is_declaration=*/true)
9140        != NULL_TREE);
9141   if (nested_name_specifier_p)
9142     template_p = cp_parser_optional_template_keyword (parser);
9143   /* If there is a `::' operator or a nested-name-specifier, then we
9144      are definitely looking for a class-name.  */
9145   if (global_scope_p || nested_name_specifier_p)
9146     return cp_parser_class_name (parser,
9147                                  /*typename_keyword_p=*/true,
9148                                  /*template_keyword_p=*/template_p,
9149                                  none_type,
9150                                  /*check_dependency_p=*/true,
9151                                  /*class_head_p=*/false,
9152                                  /*is_declaration=*/true);
9153   /* Otherwise, we could also be looking for an ordinary identifier.  */
9154   cp_parser_parse_tentatively (parser);
9155   /* Try a class-name.  */
9156   id = cp_parser_class_name (parser,
9157                              /*typename_keyword_p=*/true,
9158                              /*template_keyword_p=*/false,
9159                              none_type,
9160                              /*check_dependency_p=*/true,
9161                              /*class_head_p=*/false,
9162                              /*is_declaration=*/true);
9163   /* If we found one, we're done.  */
9164   if (cp_parser_parse_definitely (parser))
9165     return id;
9166   /* Otherwise, look for an ordinary identifier.  */
9167   return cp_parser_identifier (parser);
9168 }
9169
9170 /* Overloading [gram.over] */
9171
9172 /* Parse an operator-function-id.
9173
9174    operator-function-id:
9175      operator operator
9176
9177    Returns an IDENTIFIER_NODE for the operator which is a
9178    human-readable spelling of the identifier, e.g., `operator +'.  */
9179
9180 static tree
9181 cp_parser_operator_function_id (cp_parser* parser)
9182 {
9183   /* Look for the `operator' keyword.  */
9184   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9185     return error_mark_node;
9186   /* And then the name of the operator itself.  */
9187   return cp_parser_operator (parser);
9188 }
9189
9190 /* Parse an operator.
9191
9192    operator:
9193      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9194      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9195      || ++ -- , ->* -> () []
9196
9197    GNU Extensions:
9198
9199    operator:
9200      <? >? <?= >?=
9201
9202    Returns an IDENTIFIER_NODE for the operator which is a
9203    human-readable spelling of the identifier, e.g., `operator +'.  */
9204
9205 static tree
9206 cp_parser_operator (cp_parser* parser)
9207 {
9208   tree id = NULL_TREE;
9209   cp_token *token;
9210
9211   /* Peek at the next token.  */
9212   token = cp_lexer_peek_token (parser->lexer);
9213   /* Figure out which operator we have.  */
9214   switch (token->type)
9215     {
9216     case CPP_KEYWORD:
9217       {
9218         enum tree_code op;
9219
9220         /* The keyword should be either `new' or `delete'.  */
9221         if (token->keyword == RID_NEW)
9222           op = NEW_EXPR;
9223         else if (token->keyword == RID_DELETE)
9224           op = DELETE_EXPR;
9225         else
9226           break;
9227
9228         /* Consume the `new' or `delete' token.  */
9229         cp_lexer_consume_token (parser->lexer);
9230
9231         /* Peek at the next token.  */
9232         token = cp_lexer_peek_token (parser->lexer);
9233         /* If it's a `[' token then this is the array variant of the
9234            operator.  */
9235         if (token->type == CPP_OPEN_SQUARE)
9236           {
9237             /* Consume the `[' token.  */
9238             cp_lexer_consume_token (parser->lexer);
9239             /* Look for the `]' token.  */
9240             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9241             id = ansi_opname (op == NEW_EXPR
9242                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9243           }
9244         /* Otherwise, we have the non-array variant.  */
9245         else
9246           id = ansi_opname (op);
9247
9248         return id;
9249       }
9250
9251     case CPP_PLUS:
9252       id = ansi_opname (PLUS_EXPR);
9253       break;
9254
9255     case CPP_MINUS:
9256       id = ansi_opname (MINUS_EXPR);
9257       break;
9258
9259     case CPP_MULT:
9260       id = ansi_opname (MULT_EXPR);
9261       break;
9262
9263     case CPP_DIV:
9264       id = ansi_opname (TRUNC_DIV_EXPR);
9265       break;
9266
9267     case CPP_MOD:
9268       id = ansi_opname (TRUNC_MOD_EXPR);
9269       break;
9270
9271     case CPP_XOR:
9272       id = ansi_opname (BIT_XOR_EXPR);
9273       break;
9274
9275     case CPP_AND:
9276       id = ansi_opname (BIT_AND_EXPR);
9277       break;
9278
9279     case CPP_OR:
9280       id = ansi_opname (BIT_IOR_EXPR);
9281       break;
9282
9283     case CPP_COMPL:
9284       id = ansi_opname (BIT_NOT_EXPR);
9285       break;
9286
9287     case CPP_NOT:
9288       id = ansi_opname (TRUTH_NOT_EXPR);
9289       break;
9290
9291     case CPP_EQ:
9292       id = ansi_assopname (NOP_EXPR);
9293       break;
9294
9295     case CPP_LESS:
9296       id = ansi_opname (LT_EXPR);
9297       break;
9298
9299     case CPP_GREATER:
9300       id = ansi_opname (GT_EXPR);
9301       break;
9302
9303     case CPP_PLUS_EQ:
9304       id = ansi_assopname (PLUS_EXPR);
9305       break;
9306
9307     case CPP_MINUS_EQ:
9308       id = ansi_assopname (MINUS_EXPR);
9309       break;
9310
9311     case CPP_MULT_EQ:
9312       id = ansi_assopname (MULT_EXPR);
9313       break;
9314
9315     case CPP_DIV_EQ:
9316       id = ansi_assopname (TRUNC_DIV_EXPR);
9317       break;
9318
9319     case CPP_MOD_EQ:
9320       id = ansi_assopname (TRUNC_MOD_EXPR);
9321       break;
9322
9323     case CPP_XOR_EQ:
9324       id = ansi_assopname (BIT_XOR_EXPR);
9325       break;
9326
9327     case CPP_AND_EQ:
9328       id = ansi_assopname (BIT_AND_EXPR);
9329       break;
9330
9331     case CPP_OR_EQ:
9332       id = ansi_assopname (BIT_IOR_EXPR);
9333       break;
9334
9335     case CPP_LSHIFT:
9336       id = ansi_opname (LSHIFT_EXPR);
9337       break;
9338
9339     case CPP_RSHIFT:
9340       id = ansi_opname (RSHIFT_EXPR);
9341       break;
9342
9343     case CPP_LSHIFT_EQ:
9344       id = ansi_assopname (LSHIFT_EXPR);
9345       break;
9346
9347     case CPP_RSHIFT_EQ:
9348       id = ansi_assopname (RSHIFT_EXPR);
9349       break;
9350
9351     case CPP_EQ_EQ:
9352       id = ansi_opname (EQ_EXPR);
9353       break;
9354
9355     case CPP_NOT_EQ:
9356       id = ansi_opname (NE_EXPR);
9357       break;
9358
9359     case CPP_LESS_EQ:
9360       id = ansi_opname (LE_EXPR);
9361       break;
9362
9363     case CPP_GREATER_EQ:
9364       id = ansi_opname (GE_EXPR);
9365       break;
9366
9367     case CPP_AND_AND:
9368       id = ansi_opname (TRUTH_ANDIF_EXPR);
9369       break;
9370
9371     case CPP_OR_OR:
9372       id = ansi_opname (TRUTH_ORIF_EXPR);
9373       break;
9374
9375     case CPP_PLUS_PLUS:
9376       id = ansi_opname (POSTINCREMENT_EXPR);
9377       break;
9378
9379     case CPP_MINUS_MINUS:
9380       id = ansi_opname (PREDECREMENT_EXPR);
9381       break;
9382
9383     case CPP_COMMA:
9384       id = ansi_opname (COMPOUND_EXPR);
9385       break;
9386
9387     case CPP_DEREF_STAR:
9388       id = ansi_opname (MEMBER_REF);
9389       break;
9390
9391     case CPP_DEREF:
9392       id = ansi_opname (COMPONENT_REF);
9393       break;
9394
9395     case CPP_OPEN_PAREN:
9396       /* Consume the `('.  */
9397       cp_lexer_consume_token (parser->lexer);
9398       /* Look for the matching `)'.  */
9399       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9400       return ansi_opname (CALL_EXPR);
9401
9402     case CPP_OPEN_SQUARE:
9403       /* Consume the `['.  */
9404       cp_lexer_consume_token (parser->lexer);
9405       /* Look for the matching `]'.  */
9406       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9407       return ansi_opname (ARRAY_REF);
9408
9409     default:
9410       /* Anything else is an error.  */
9411       break;
9412     }
9413
9414   /* If we have selected an identifier, we need to consume the
9415      operator token.  */
9416   if (id)
9417     cp_lexer_consume_token (parser->lexer);
9418   /* Otherwise, no valid operator name was present.  */
9419   else
9420     {
9421       cp_parser_error (parser, "expected operator");
9422       id = error_mark_node;
9423     }
9424
9425   return id;
9426 }
9427
9428 /* Parse a template-declaration.
9429
9430    template-declaration:
9431      export [opt] template < template-parameter-list > declaration
9432
9433    If MEMBER_P is TRUE, this template-declaration occurs within a
9434    class-specifier.
9435
9436    The grammar rule given by the standard isn't correct.  What
9437    is really meant is:
9438
9439    template-declaration:
9440      export [opt] template-parameter-list-seq
9441        decl-specifier-seq [opt] init-declarator [opt] ;
9442      export [opt] template-parameter-list-seq
9443        function-definition
9444
9445    template-parameter-list-seq:
9446      template-parameter-list-seq [opt]
9447      template < template-parameter-list >  */
9448
9449 static void
9450 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9451 {
9452   /* Check for `export'.  */
9453   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9454     {
9455       /* Consume the `export' token.  */
9456       cp_lexer_consume_token (parser->lexer);
9457       /* Warn that we do not support `export'.  */
9458       warning (0, "keyword %<export%> not implemented, and will be ignored");
9459     }
9460
9461   cp_parser_template_declaration_after_export (parser, member_p);
9462 }
9463
9464 /* Parse a template-parameter-list.
9465
9466    template-parameter-list:
9467      template-parameter
9468      template-parameter-list , template-parameter
9469
9470    Returns a TREE_LIST.  Each node represents a template parameter.
9471    The nodes are connected via their TREE_CHAINs.  */
9472
9473 static tree
9474 cp_parser_template_parameter_list (cp_parser* parser)
9475 {
9476   tree parameter_list = NULL_TREE;
9477
9478   begin_template_parm_list ();
9479   while (true)
9480     {
9481       tree parameter;
9482       bool is_non_type;
9483       bool is_parameter_pack;
9484
9485       /* Parse the template-parameter.  */
9486       parameter = cp_parser_template_parameter (parser, 
9487                                                 &is_non_type,
9488                                                 &is_parameter_pack);
9489       /* Add it to the list.  */
9490       if (parameter != error_mark_node)
9491         parameter_list = process_template_parm (parameter_list,
9492                                                 parameter,
9493                                                 is_non_type,
9494                                                 is_parameter_pack);
9495       else
9496        {
9497          tree err_parm = build_tree_list (parameter, parameter);
9498          TREE_VALUE (err_parm) = error_mark_node;
9499          parameter_list = chainon (parameter_list, err_parm);
9500        }
9501
9502       /* If the next token is not a `,', we're done.  */
9503       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9504         break;
9505       /* Otherwise, consume the `,' token.  */
9506       cp_lexer_consume_token (parser->lexer);
9507     }
9508
9509   return end_template_parm_list (parameter_list);
9510 }
9511
9512 /* Parse a template-parameter.
9513
9514    template-parameter:
9515      type-parameter
9516      parameter-declaration
9517
9518    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9519    the parameter.  The TREE_PURPOSE is the default value, if any.
9520    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9521    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9522    set to true iff this parameter is a parameter pack. */
9523
9524 static tree
9525 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9526                               bool *is_parameter_pack)
9527 {
9528   cp_token *token;
9529   cp_parameter_declarator *parameter_declarator;
9530   cp_declarator *id_declarator;
9531   tree parm;
9532
9533   /* Assume it is a type parameter or a template parameter.  */
9534   *is_non_type = false;
9535   /* Assume it not a parameter pack. */
9536   *is_parameter_pack = false;
9537   /* Peek at the next token.  */
9538   token = cp_lexer_peek_token (parser->lexer);
9539   /* If it is `class' or `template', we have a type-parameter.  */
9540   if (token->keyword == RID_TEMPLATE)
9541     return cp_parser_type_parameter (parser, is_parameter_pack);
9542   /* If it is `class' or `typename' we do not know yet whether it is a
9543      type parameter or a non-type parameter.  Consider:
9544
9545        template <typename T, typename T::X X> ...
9546
9547      or:
9548
9549        template <class C, class D*> ...
9550
9551      Here, the first parameter is a type parameter, and the second is
9552      a non-type parameter.  We can tell by looking at the token after
9553      the identifier -- if it is a `,', `=', or `>' then we have a type
9554      parameter.  */
9555   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9556     {
9557       /* Peek at the token after `class' or `typename'.  */
9558       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9559       /* If it's an ellipsis, we have a template type parameter
9560          pack. */
9561       if (token->type == CPP_ELLIPSIS)
9562         return cp_parser_type_parameter (parser, is_parameter_pack);
9563       /* If it's an identifier, skip it.  */
9564       if (token->type == CPP_NAME)
9565         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9566       /* Now, see if the token looks like the end of a template
9567          parameter.  */
9568       if (token->type == CPP_COMMA
9569           || token->type == CPP_EQ
9570           || token->type == CPP_GREATER)
9571         return cp_parser_type_parameter (parser, is_parameter_pack);
9572     }
9573
9574   /* Otherwise, it is a non-type parameter.
9575
9576      [temp.param]
9577
9578      When parsing a default template-argument for a non-type
9579      template-parameter, the first non-nested `>' is taken as the end
9580      of the template parameter-list rather than a greater-than
9581      operator.  */
9582   *is_non_type = true;
9583   parameter_declarator
9584      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9585                                         /*parenthesized_p=*/NULL);
9586
9587   /* If the parameter declaration is marked as a parameter pack, set
9588      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9589      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9590      grokdeclarator. */
9591   if (parameter_declarator
9592       && parameter_declarator->declarator
9593       && parameter_declarator->declarator->parameter_pack_p)
9594     {
9595       *is_parameter_pack = true;
9596       parameter_declarator->declarator->parameter_pack_p = false;
9597     }
9598
9599   /* If the next token is an ellipsis, and we don't already have it
9600      marked as a parameter pack, then we have a parameter pack (that
9601      has no declarator).  */
9602   if (!*is_parameter_pack
9603       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9604       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9605     {
9606       /* Consume the `...'.  */
9607       cp_lexer_consume_token (parser->lexer);
9608       maybe_warn_variadic_templates ();
9609       
9610       *is_parameter_pack = true;
9611     }
9612   /* We might end up with a pack expansion as the type of the non-type
9613      template parameter, in which case this is a non-type template
9614      parameter pack.  */
9615   else if (parameter_declarator
9616            && parameter_declarator->decl_specifiers.type
9617            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9618     {
9619       *is_parameter_pack = true;
9620       parameter_declarator->decl_specifiers.type = 
9621         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9622     }
9623
9624   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9625     {
9626       /* Parameter packs cannot have default arguments.  However, a
9627          user may try to do so, so we'll parse them and give an
9628          appropriate diagnostic here.  */
9629
9630       /* Consume the `='.  */
9631       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9632       cp_lexer_consume_token (parser->lexer);
9633       
9634       /* Find the name of the parameter pack.  */     
9635       id_declarator = parameter_declarator->declarator;
9636       while (id_declarator && id_declarator->kind != cdk_id)
9637         id_declarator = id_declarator->declarator;
9638       
9639       if (id_declarator && id_declarator->kind == cdk_id)
9640         error ("%Htemplate parameter pack %qD cannot have a default argument",
9641                &start_token->location, id_declarator->u.id.unqualified_name);
9642       else
9643         error ("%Htemplate parameter pack cannot have a default argument",
9644                &start_token->location);
9645       
9646       /* Parse the default argument, but throw away the result.  */
9647       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9648     }
9649
9650   parm = grokdeclarator (parameter_declarator->declarator,
9651                          &parameter_declarator->decl_specifiers,
9652                          PARM, /*initialized=*/0,
9653                          /*attrlist=*/NULL);
9654   if (parm == error_mark_node)
9655     return error_mark_node;
9656
9657   return build_tree_list (parameter_declarator->default_argument, parm);
9658 }
9659
9660 /* Parse a type-parameter.
9661
9662    type-parameter:
9663      class identifier [opt]
9664      class identifier [opt] = type-id
9665      typename identifier [opt]
9666      typename identifier [opt] = type-id
9667      template < template-parameter-list > class identifier [opt]
9668      template < template-parameter-list > class identifier [opt]
9669        = id-expression
9670
9671    GNU Extension (variadic templates):
9672
9673    type-parameter:
9674      class ... identifier [opt]
9675      typename ... identifier [opt]
9676
9677    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9678    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9679    the declaration of the parameter.
9680
9681    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9682
9683 static tree
9684 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9685 {
9686   cp_token *token;
9687   tree parameter;
9688
9689   /* Look for a keyword to tell us what kind of parameter this is.  */
9690   token = cp_parser_require (parser, CPP_KEYWORD,
9691                              "%<class%>, %<typename%>, or %<template%>");
9692   if (!token)
9693     return error_mark_node;
9694
9695   switch (token->keyword)
9696     {
9697     case RID_CLASS:
9698     case RID_TYPENAME:
9699       {
9700         tree identifier;
9701         tree default_argument;
9702
9703         /* If the next token is an ellipsis, we have a template
9704            argument pack. */
9705         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9706           {
9707             /* Consume the `...' token. */
9708             cp_lexer_consume_token (parser->lexer);
9709             maybe_warn_variadic_templates ();
9710
9711             *is_parameter_pack = true;
9712           }
9713
9714         /* If the next token is an identifier, then it names the
9715            parameter.  */
9716         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9717           identifier = cp_parser_identifier (parser);
9718         else
9719           identifier = NULL_TREE;
9720
9721         /* Create the parameter.  */
9722         parameter = finish_template_type_parm (class_type_node, identifier);
9723
9724         /* If the next token is an `=', we have a default argument.  */
9725         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9726           {
9727             /* Consume the `=' token.  */
9728             cp_lexer_consume_token (parser->lexer);
9729             /* Parse the default-argument.  */
9730             push_deferring_access_checks (dk_no_deferred);
9731             default_argument = cp_parser_type_id (parser);
9732
9733             /* Template parameter packs cannot have default
9734                arguments. */
9735             if (*is_parameter_pack)
9736               {
9737                 if (identifier)
9738                   error ("%Htemplate parameter pack %qD cannot have a "
9739                          "default argument", &token->location, identifier);
9740                 else
9741                   error ("%Htemplate parameter packs cannot have "
9742                          "default arguments", &token->location);
9743                 default_argument = NULL_TREE;
9744               }
9745             pop_deferring_access_checks ();
9746           }
9747         else
9748           default_argument = NULL_TREE;
9749
9750         /* Create the combined representation of the parameter and the
9751            default argument.  */
9752         parameter = build_tree_list (default_argument, parameter);
9753       }
9754       break;
9755
9756     case RID_TEMPLATE:
9757       {
9758         tree parameter_list;
9759         tree identifier;
9760         tree default_argument;
9761
9762         /* Look for the `<'.  */
9763         cp_parser_require (parser, CPP_LESS, "%<<%>");
9764         /* Parse the template-parameter-list.  */
9765         parameter_list = cp_parser_template_parameter_list (parser);
9766         /* Look for the `>'.  */
9767         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9768         /* Look for the `class' keyword.  */
9769         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9770         /* If the next token is an ellipsis, we have a template
9771            argument pack. */
9772         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9773           {
9774             /* Consume the `...' token. */
9775             cp_lexer_consume_token (parser->lexer);
9776             maybe_warn_variadic_templates ();
9777
9778             *is_parameter_pack = true;
9779           }
9780         /* If the next token is an `=', then there is a
9781            default-argument.  If the next token is a `>', we are at
9782            the end of the parameter-list.  If the next token is a `,',
9783            then we are at the end of this parameter.  */
9784         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9785             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9786             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9787           {
9788             identifier = cp_parser_identifier (parser);
9789             /* Treat invalid names as if the parameter were nameless.  */
9790             if (identifier == error_mark_node)
9791               identifier = NULL_TREE;
9792           }
9793         else
9794           identifier = NULL_TREE;
9795
9796         /* Create the template parameter.  */
9797         parameter = finish_template_template_parm (class_type_node,
9798                                                    identifier);
9799
9800         /* If the next token is an `=', then there is a
9801            default-argument.  */
9802         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9803           {
9804             bool is_template;
9805
9806             /* Consume the `='.  */
9807             cp_lexer_consume_token (parser->lexer);
9808             /* Parse the id-expression.  */
9809             push_deferring_access_checks (dk_no_deferred);
9810             /* save token before parsing the id-expression, for error
9811                reporting */
9812             token = cp_lexer_peek_token (parser->lexer);
9813             default_argument
9814               = cp_parser_id_expression (parser,
9815                                          /*template_keyword_p=*/false,
9816                                          /*check_dependency_p=*/true,
9817                                          /*template_p=*/&is_template,
9818                                          /*declarator_p=*/false,
9819                                          /*optional_p=*/false);
9820             if (TREE_CODE (default_argument) == TYPE_DECL)
9821               /* If the id-expression was a template-id that refers to
9822                  a template-class, we already have the declaration here,
9823                  so no further lookup is needed.  */
9824                  ;
9825             else
9826               /* Look up the name.  */
9827               default_argument
9828                 = cp_parser_lookup_name (parser, default_argument,
9829                                          none_type,
9830                                          /*is_template=*/is_template,
9831                                          /*is_namespace=*/false,
9832                                          /*check_dependency=*/true,
9833                                          /*ambiguous_decls=*/NULL,
9834                                          token->location);
9835             /* See if the default argument is valid.  */
9836             default_argument
9837               = check_template_template_default_arg (default_argument);
9838
9839             /* Template parameter packs cannot have default
9840                arguments. */
9841             if (*is_parameter_pack)
9842               {
9843                 if (identifier)
9844                   error ("%Htemplate parameter pack %qD cannot "
9845                          "have a default argument",
9846                          &token->location, identifier);
9847                 else
9848                   error ("%Htemplate parameter packs cannot "
9849                          "have default arguments",
9850                          &token->location);
9851                 default_argument = NULL_TREE;
9852               }
9853             pop_deferring_access_checks ();
9854           }
9855         else
9856           default_argument = NULL_TREE;
9857
9858         /* Create the combined representation of the parameter and the
9859            default argument.  */
9860         parameter = build_tree_list (default_argument, parameter);
9861       }
9862       break;
9863
9864     default:
9865       gcc_unreachable ();
9866       break;
9867     }
9868
9869   return parameter;
9870 }
9871
9872 /* Parse a template-id.
9873
9874    template-id:
9875      template-name < template-argument-list [opt] >
9876
9877    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9878    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9879    returned.  Otherwise, if the template-name names a function, or set
9880    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9881    names a class, returns a TYPE_DECL for the specialization.
9882
9883    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9884    uninstantiated templates.  */
9885
9886 static tree
9887 cp_parser_template_id (cp_parser *parser,
9888                        bool template_keyword_p,
9889                        bool check_dependency_p,
9890                        bool is_declaration)
9891 {
9892   int i;
9893   tree templ;
9894   tree arguments;
9895   tree template_id;
9896   cp_token_position start_of_id = 0;
9897   deferred_access_check *chk;
9898   VEC (deferred_access_check,gc) *access_check;
9899   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9900   bool is_identifier;
9901
9902   /* If the next token corresponds to a template-id, there is no need
9903      to reparse it.  */
9904   next_token = cp_lexer_peek_token (parser->lexer);
9905   if (next_token->type == CPP_TEMPLATE_ID)
9906     {
9907       struct tree_check *check_value;
9908
9909       /* Get the stored value.  */
9910       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9911       /* Perform any access checks that were deferred.  */
9912       access_check = check_value->checks;
9913       if (access_check)
9914         {
9915           for (i = 0 ;
9916                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9917                ++i)
9918             {
9919               perform_or_defer_access_check (chk->binfo,
9920                                              chk->decl,
9921                                              chk->diag_decl);
9922             }
9923         }
9924       /* Return the stored value.  */
9925       return check_value->value;
9926     }
9927
9928   /* Avoid performing name lookup if there is no possibility of
9929      finding a template-id.  */
9930   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9931       || (next_token->type == CPP_NAME
9932           && !cp_parser_nth_token_starts_template_argument_list_p
9933                (parser, 2)))
9934     {
9935       cp_parser_error (parser, "expected template-id");
9936       return error_mark_node;
9937     }
9938
9939   /* Remember where the template-id starts.  */
9940   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9941     start_of_id = cp_lexer_token_position (parser->lexer, false);
9942
9943   push_deferring_access_checks (dk_deferred);
9944
9945   /* Parse the template-name.  */
9946   is_identifier = false;
9947   token = cp_lexer_peek_token (parser->lexer);
9948   templ = cp_parser_template_name (parser, template_keyword_p,
9949                                    check_dependency_p,
9950                                    is_declaration,
9951                                    &is_identifier);
9952   if (templ == error_mark_node || is_identifier)
9953     {
9954       pop_deferring_access_checks ();
9955       return templ;
9956     }
9957
9958   /* If we find the sequence `[:' after a template-name, it's probably
9959      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9960      parse correctly the argument list.  */
9961   next_token = cp_lexer_peek_token (parser->lexer);
9962   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9963   if (next_token->type == CPP_OPEN_SQUARE
9964       && next_token->flags & DIGRAPH
9965       && next_token_2->type == CPP_COLON
9966       && !(next_token_2->flags & PREV_WHITE))
9967     {
9968       cp_parser_parse_tentatively (parser);
9969       /* Change `:' into `::'.  */
9970       next_token_2->type = CPP_SCOPE;
9971       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9972          CPP_LESS.  */
9973       cp_lexer_consume_token (parser->lexer);
9974
9975       /* Parse the arguments.  */
9976       arguments = cp_parser_enclosed_template_argument_list (parser);
9977       if (!cp_parser_parse_definitely (parser))
9978         {
9979           /* If we couldn't parse an argument list, then we revert our changes
9980              and return simply an error. Maybe this is not a template-id
9981              after all.  */
9982           next_token_2->type = CPP_COLON;
9983           cp_parser_error (parser, "expected %<<%>");
9984           pop_deferring_access_checks ();
9985           return error_mark_node;
9986         }
9987       /* Otherwise, emit an error about the invalid digraph, but continue
9988          parsing because we got our argument list.  */
9989       permerror ("%H%<<::%> cannot begin a template-argument list",
9990                  &next_token->location);
9991       inform ("%H%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9992               "between %<<%> and %<::%>",
9993               &next_token->location);
9994       if (!flag_permissive)
9995         {
9996           static bool hint;
9997           if (!hint)
9998             {
9999               inform ("%H(if you use %<-fpermissive%> G++ will accept your code)",
10000                       &next_token->location);
10001               hint = true;
10002             }
10003         }
10004     }
10005   else
10006     {
10007       /* Look for the `<' that starts the template-argument-list.  */
10008       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10009         {
10010           pop_deferring_access_checks ();
10011           return error_mark_node;
10012         }
10013       /* Parse the arguments.  */
10014       arguments = cp_parser_enclosed_template_argument_list (parser);
10015     }
10016
10017   /* Build a representation of the specialization.  */
10018   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10019     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10020   else if (DECL_CLASS_TEMPLATE_P (templ)
10021            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10022     {
10023       bool entering_scope;
10024       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10025          template (rather than some instantiation thereof) only if
10026          is not nested within some other construct.  For example, in
10027          "template <typename T> void f(T) { A<T>::", A<T> is just an
10028          instantiation of A.  */
10029       entering_scope = (template_parm_scope_p ()
10030                         && cp_lexer_next_token_is (parser->lexer,
10031                                                    CPP_SCOPE));
10032       template_id
10033         = finish_template_type (templ, arguments, entering_scope);
10034     }
10035   else
10036     {
10037       /* If it's not a class-template or a template-template, it should be
10038          a function-template.  */
10039       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10040                    || TREE_CODE (templ) == OVERLOAD
10041                    || BASELINK_P (templ)));
10042
10043       template_id = lookup_template_function (templ, arguments);
10044     }
10045
10046   /* If parsing tentatively, replace the sequence of tokens that makes
10047      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10048      should we re-parse the token stream, we will not have to repeat
10049      the effort required to do the parse, nor will we issue duplicate
10050      error messages about problems during instantiation of the
10051      template.  */
10052   if (start_of_id)
10053     {
10054       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10055
10056       /* Reset the contents of the START_OF_ID token.  */
10057       token->type = CPP_TEMPLATE_ID;
10058       /* Retrieve any deferred checks.  Do not pop this access checks yet
10059          so the memory will not be reclaimed during token replacing below.  */
10060       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10061       token->u.tree_check_value->value = template_id;
10062       token->u.tree_check_value->checks = get_deferred_access_checks ();
10063       token->keyword = RID_MAX;
10064
10065       /* Purge all subsequent tokens.  */
10066       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10067
10068       /* ??? Can we actually assume that, if template_id ==
10069          error_mark_node, we will have issued a diagnostic to the
10070          user, as opposed to simply marking the tentative parse as
10071          failed?  */
10072       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10073         error ("%Hparse error in template argument list",
10074                &token->location);
10075     }
10076
10077   pop_deferring_access_checks ();
10078   return template_id;
10079 }
10080
10081 /* Parse a template-name.
10082
10083    template-name:
10084      identifier
10085
10086    The standard should actually say:
10087
10088    template-name:
10089      identifier
10090      operator-function-id
10091
10092    A defect report has been filed about this issue.
10093
10094    A conversion-function-id cannot be a template name because they cannot
10095    be part of a template-id. In fact, looking at this code:
10096
10097    a.operator K<int>()
10098
10099    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10100    It is impossible to call a templated conversion-function-id with an
10101    explicit argument list, since the only allowed template parameter is
10102    the type to which it is converting.
10103
10104    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10105    `template' keyword, in a construction like:
10106
10107      T::template f<3>()
10108
10109    In that case `f' is taken to be a template-name, even though there
10110    is no way of knowing for sure.
10111
10112    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10113    name refers to a set of overloaded functions, at least one of which
10114    is a template, or an IDENTIFIER_NODE with the name of the template,
10115    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10116    names are looked up inside uninstantiated templates.  */
10117
10118 static tree
10119 cp_parser_template_name (cp_parser* parser,
10120                          bool template_keyword_p,
10121                          bool check_dependency_p,
10122                          bool is_declaration,
10123                          bool *is_identifier)
10124 {
10125   tree identifier;
10126   tree decl;
10127   tree fns;
10128   cp_token *token = cp_lexer_peek_token (parser->lexer);
10129
10130   /* If the next token is `operator', then we have either an
10131      operator-function-id or a conversion-function-id.  */
10132   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10133     {
10134       /* We don't know whether we're looking at an
10135          operator-function-id or a conversion-function-id.  */
10136       cp_parser_parse_tentatively (parser);
10137       /* Try an operator-function-id.  */
10138       identifier = cp_parser_operator_function_id (parser);
10139       /* If that didn't work, try a conversion-function-id.  */
10140       if (!cp_parser_parse_definitely (parser))
10141         {
10142           cp_parser_error (parser, "expected template-name");
10143           return error_mark_node;
10144         }
10145     }
10146   /* Look for the identifier.  */
10147   else
10148     identifier = cp_parser_identifier (parser);
10149
10150   /* If we didn't find an identifier, we don't have a template-id.  */
10151   if (identifier == error_mark_node)
10152     return error_mark_node;
10153
10154   /* If the name immediately followed the `template' keyword, then it
10155      is a template-name.  However, if the next token is not `<', then
10156      we do not treat it as a template-name, since it is not being used
10157      as part of a template-id.  This enables us to handle constructs
10158      like:
10159
10160        template <typename T> struct S { S(); };
10161        template <typename T> S<T>::S();
10162
10163      correctly.  We would treat `S' as a template -- if it were `S<T>'
10164      -- but we do not if there is no `<'.  */
10165
10166   if (processing_template_decl
10167       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10168     {
10169       /* In a declaration, in a dependent context, we pretend that the
10170          "template" keyword was present in order to improve error
10171          recovery.  For example, given:
10172
10173            template <typename T> void f(T::X<int>);
10174
10175          we want to treat "X<int>" as a template-id.  */
10176       if (is_declaration
10177           && !template_keyword_p
10178           && parser->scope && TYPE_P (parser->scope)
10179           && check_dependency_p
10180           && dependent_type_p (parser->scope)
10181           /* Do not do this for dtors (or ctors), since they never
10182              need the template keyword before their name.  */
10183           && !constructor_name_p (identifier, parser->scope))
10184         {
10185           cp_token_position start = 0;
10186
10187           /* Explain what went wrong.  */
10188           error ("%Hnon-template %qD used as template",
10189                  &token->location, identifier);
10190           inform ("use %<%T::template %D%> to indicate that it is a template",
10191                   parser->scope, identifier);
10192           /* If parsing tentatively, find the location of the "<" token.  */
10193           if (cp_parser_simulate_error (parser))
10194             start = cp_lexer_token_position (parser->lexer, true);
10195           /* Parse the template arguments so that we can issue error
10196              messages about them.  */
10197           cp_lexer_consume_token (parser->lexer);
10198           cp_parser_enclosed_template_argument_list (parser);
10199           /* Skip tokens until we find a good place from which to
10200              continue parsing.  */
10201           cp_parser_skip_to_closing_parenthesis (parser,
10202                                                  /*recovering=*/true,
10203                                                  /*or_comma=*/true,
10204                                                  /*consume_paren=*/false);
10205           /* If parsing tentatively, permanently remove the
10206              template argument list.  That will prevent duplicate
10207              error messages from being issued about the missing
10208              "template" keyword.  */
10209           if (start)
10210             cp_lexer_purge_tokens_after (parser->lexer, start);
10211           if (is_identifier)
10212             *is_identifier = true;
10213           return identifier;
10214         }
10215
10216       /* If the "template" keyword is present, then there is generally
10217          no point in doing name-lookup, so we just return IDENTIFIER.
10218          But, if the qualifying scope is non-dependent then we can
10219          (and must) do name-lookup normally.  */
10220       if (template_keyword_p
10221           && (!parser->scope
10222               || (TYPE_P (parser->scope)
10223                   && dependent_type_p (parser->scope))))
10224         return identifier;
10225     }
10226
10227   /* Look up the name.  */
10228   decl = cp_parser_lookup_name (parser, identifier,
10229                                 none_type,
10230                                 /*is_template=*/false,
10231                                 /*is_namespace=*/false,
10232                                 check_dependency_p,
10233                                 /*ambiguous_decls=*/NULL,
10234                                 token->location);
10235   decl = maybe_get_template_decl_from_type_decl (decl);
10236
10237   /* If DECL is a template, then the name was a template-name.  */
10238   if (TREE_CODE (decl) == TEMPLATE_DECL)
10239     ;
10240   else
10241     {
10242       tree fn = NULL_TREE;
10243
10244       /* The standard does not explicitly indicate whether a name that
10245          names a set of overloaded declarations, some of which are
10246          templates, is a template-name.  However, such a name should
10247          be a template-name; otherwise, there is no way to form a
10248          template-id for the overloaded templates.  */
10249       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10250       if (TREE_CODE (fns) == OVERLOAD)
10251         for (fn = fns; fn; fn = OVL_NEXT (fn))
10252           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10253             break;
10254
10255       if (!fn)
10256         {
10257           /* The name does not name a template.  */
10258           cp_parser_error (parser, "expected template-name");
10259           return error_mark_node;
10260         }
10261     }
10262
10263   /* If DECL is dependent, and refers to a function, then just return
10264      its name; we will look it up again during template instantiation.  */
10265   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10266     {
10267       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10268       if (TYPE_P (scope) && dependent_type_p (scope))
10269         return identifier;
10270     }
10271
10272   return decl;
10273 }
10274
10275 /* Parse a template-argument-list.
10276
10277    template-argument-list:
10278      template-argument ... [opt]
10279      template-argument-list , template-argument ... [opt]
10280
10281    Returns a TREE_VEC containing the arguments.  */
10282
10283 static tree
10284 cp_parser_template_argument_list (cp_parser* parser)
10285 {
10286   tree fixed_args[10];
10287   unsigned n_args = 0;
10288   unsigned alloced = 10;
10289   tree *arg_ary = fixed_args;
10290   tree vec;
10291   bool saved_in_template_argument_list_p;
10292   bool saved_ice_p;
10293   bool saved_non_ice_p;
10294
10295   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10296   parser->in_template_argument_list_p = true;
10297   /* Even if the template-id appears in an integral
10298      constant-expression, the contents of the argument list do
10299      not.  */
10300   saved_ice_p = parser->integral_constant_expression_p;
10301   parser->integral_constant_expression_p = false;
10302   saved_non_ice_p = parser->non_integral_constant_expression_p;
10303   parser->non_integral_constant_expression_p = false;
10304   /* Parse the arguments.  */
10305   do
10306     {
10307       tree argument;
10308
10309       if (n_args)
10310         /* Consume the comma.  */
10311         cp_lexer_consume_token (parser->lexer);
10312
10313       /* Parse the template-argument.  */
10314       argument = cp_parser_template_argument (parser);
10315
10316       /* If the next token is an ellipsis, we're expanding a template
10317          argument pack. */
10318       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10319         {
10320           /* Consume the `...' token. */
10321           cp_lexer_consume_token (parser->lexer);
10322
10323           /* Make the argument into a TYPE_PACK_EXPANSION or
10324              EXPR_PACK_EXPANSION. */
10325           argument = make_pack_expansion (argument);
10326         }
10327
10328       if (n_args == alloced)
10329         {
10330           alloced *= 2;
10331
10332           if (arg_ary == fixed_args)
10333             {
10334               arg_ary = XNEWVEC (tree, alloced);
10335               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10336             }
10337           else
10338             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10339         }
10340       arg_ary[n_args++] = argument;
10341     }
10342   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10343
10344   vec = make_tree_vec (n_args);
10345
10346   while (n_args--)
10347     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10348
10349   if (arg_ary != fixed_args)
10350     free (arg_ary);
10351   parser->non_integral_constant_expression_p = saved_non_ice_p;
10352   parser->integral_constant_expression_p = saved_ice_p;
10353   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10354   return vec;
10355 }
10356
10357 /* Parse a template-argument.
10358
10359    template-argument:
10360      assignment-expression
10361      type-id
10362      id-expression
10363
10364    The representation is that of an assignment-expression, type-id, or
10365    id-expression -- except that the qualified id-expression is
10366    evaluated, so that the value returned is either a DECL or an
10367    OVERLOAD.
10368
10369    Although the standard says "assignment-expression", it forbids
10370    throw-expressions or assignments in the template argument.
10371    Therefore, we use "conditional-expression" instead.  */
10372
10373 static tree
10374 cp_parser_template_argument (cp_parser* parser)
10375 {
10376   tree argument;
10377   bool template_p;
10378   bool address_p;
10379   bool maybe_type_id = false;
10380   cp_token *token = NULL, *argument_start_token = NULL;
10381   cp_id_kind idk;
10382
10383   /* There's really no way to know what we're looking at, so we just
10384      try each alternative in order.
10385
10386        [temp.arg]
10387
10388        In a template-argument, an ambiguity between a type-id and an
10389        expression is resolved to a type-id, regardless of the form of
10390        the corresponding template-parameter.
10391
10392      Therefore, we try a type-id first.  */
10393   cp_parser_parse_tentatively (parser);
10394   argument = cp_parser_type_id (parser);
10395   /* If there was no error parsing the type-id but the next token is a '>>',
10396      we probably found a typo for '> >'. But there are type-id which are
10397      also valid expressions. For instance:
10398
10399      struct X { int operator >> (int); };
10400      template <int V> struct Foo {};
10401      Foo<X () >> 5> r;
10402
10403      Here 'X()' is a valid type-id of a function type, but the user just
10404      wanted to write the expression "X() >> 5". Thus, we remember that we
10405      found a valid type-id, but we still try to parse the argument as an
10406      expression to see what happens.  */
10407   if (!cp_parser_error_occurred (parser)
10408       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10409     {
10410       maybe_type_id = true;
10411       cp_parser_abort_tentative_parse (parser);
10412     }
10413   else
10414     {
10415       /* If the next token isn't a `,' or a `>', then this argument wasn't
10416       really finished. This means that the argument is not a valid
10417       type-id.  */
10418       if (!cp_parser_next_token_ends_template_argument_p (parser))
10419         cp_parser_error (parser, "expected template-argument");
10420       /* If that worked, we're done.  */
10421       if (cp_parser_parse_definitely (parser))
10422         return argument;
10423     }
10424   /* We're still not sure what the argument will be.  */
10425   cp_parser_parse_tentatively (parser);
10426   /* Try a template.  */
10427   argument_start_token = cp_lexer_peek_token (parser->lexer);
10428   argument = cp_parser_id_expression (parser,
10429                                       /*template_keyword_p=*/false,
10430                                       /*check_dependency_p=*/true,
10431                                       &template_p,
10432                                       /*declarator_p=*/false,
10433                                       /*optional_p=*/false);
10434   /* If the next token isn't a `,' or a `>', then this argument wasn't
10435      really finished.  */
10436   if (!cp_parser_next_token_ends_template_argument_p (parser))
10437     cp_parser_error (parser, "expected template-argument");
10438   if (!cp_parser_error_occurred (parser))
10439     {
10440       /* Figure out what is being referred to.  If the id-expression
10441          was for a class template specialization, then we will have a
10442          TYPE_DECL at this point.  There is no need to do name lookup
10443          at this point in that case.  */
10444       if (TREE_CODE (argument) != TYPE_DECL)
10445         argument = cp_parser_lookup_name (parser, argument,
10446                                           none_type,
10447                                           /*is_template=*/template_p,
10448                                           /*is_namespace=*/false,
10449                                           /*check_dependency=*/true,
10450                                           /*ambiguous_decls=*/NULL,
10451                                           argument_start_token->location);
10452       if (TREE_CODE (argument) != TEMPLATE_DECL
10453           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10454         cp_parser_error (parser, "expected template-name");
10455     }
10456   if (cp_parser_parse_definitely (parser))
10457     return argument;
10458   /* It must be a non-type argument.  There permitted cases are given
10459      in [temp.arg.nontype]:
10460
10461      -- an integral constant-expression of integral or enumeration
10462         type; or
10463
10464      -- the name of a non-type template-parameter; or
10465
10466      -- the name of an object or function with external linkage...
10467
10468      -- the address of an object or function with external linkage...
10469
10470      -- a pointer to member...  */
10471   /* Look for a non-type template parameter.  */
10472   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10473     {
10474       cp_parser_parse_tentatively (parser);
10475       argument = cp_parser_primary_expression (parser,
10476                                                /*adress_p=*/false,
10477                                                /*cast_p=*/false,
10478                                                /*template_arg_p=*/true,
10479                                                &idk);
10480       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10481           || !cp_parser_next_token_ends_template_argument_p (parser))
10482         cp_parser_simulate_error (parser);
10483       if (cp_parser_parse_definitely (parser))
10484         return argument;
10485     }
10486
10487   /* If the next token is "&", the argument must be the address of an
10488      object or function with external linkage.  */
10489   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10490   if (address_p)
10491     cp_lexer_consume_token (parser->lexer);
10492   /* See if we might have an id-expression.  */
10493   token = cp_lexer_peek_token (parser->lexer);
10494   if (token->type == CPP_NAME
10495       || token->keyword == RID_OPERATOR
10496       || token->type == CPP_SCOPE
10497       || token->type == CPP_TEMPLATE_ID
10498       || token->type == CPP_NESTED_NAME_SPECIFIER)
10499     {
10500       cp_parser_parse_tentatively (parser);
10501       argument = cp_parser_primary_expression (parser,
10502                                                address_p,
10503                                                /*cast_p=*/false,
10504                                                /*template_arg_p=*/true,
10505                                                &idk);
10506       if (cp_parser_error_occurred (parser)
10507           || !cp_parser_next_token_ends_template_argument_p (parser))
10508         cp_parser_abort_tentative_parse (parser);
10509       else
10510         {
10511           if (TREE_CODE (argument) == INDIRECT_REF)
10512             {
10513               gcc_assert (REFERENCE_REF_P (argument));
10514               argument = TREE_OPERAND (argument, 0);
10515             }
10516
10517           if (TREE_CODE (argument) == VAR_DECL)
10518             {
10519               /* A variable without external linkage might still be a
10520                  valid constant-expression, so no error is issued here
10521                  if the external-linkage check fails.  */
10522               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10523                 cp_parser_simulate_error (parser);
10524             }
10525           else if (is_overloaded_fn (argument))
10526             /* All overloaded functions are allowed; if the external
10527                linkage test does not pass, an error will be issued
10528                later.  */
10529             ;
10530           else if (address_p
10531                    && (TREE_CODE (argument) == OFFSET_REF
10532                        || TREE_CODE (argument) == SCOPE_REF))
10533             /* A pointer-to-member.  */
10534             ;
10535           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10536             ;
10537           else
10538             cp_parser_simulate_error (parser);
10539
10540           if (cp_parser_parse_definitely (parser))
10541             {
10542               if (address_p)
10543                 argument = build_x_unary_op (ADDR_EXPR, argument,
10544                                              tf_warning_or_error);
10545               return argument;
10546             }
10547         }
10548     }
10549   /* If the argument started with "&", there are no other valid
10550      alternatives at this point.  */
10551   if (address_p)
10552     {
10553       cp_parser_error (parser, "invalid non-type template argument");
10554       return error_mark_node;
10555     }
10556
10557   /* If the argument wasn't successfully parsed as a type-id followed
10558      by '>>', the argument can only be a constant expression now.
10559      Otherwise, we try parsing the constant-expression tentatively,
10560      because the argument could really be a type-id.  */
10561   if (maybe_type_id)
10562     cp_parser_parse_tentatively (parser);
10563   argument = cp_parser_constant_expression (parser,
10564                                             /*allow_non_constant_p=*/false,
10565                                             /*non_constant_p=*/NULL);
10566   argument = fold_non_dependent_expr (argument);
10567   if (!maybe_type_id)
10568     return argument;
10569   if (!cp_parser_next_token_ends_template_argument_p (parser))
10570     cp_parser_error (parser, "expected template-argument");
10571   if (cp_parser_parse_definitely (parser))
10572     return argument;
10573   /* We did our best to parse the argument as a non type-id, but that
10574      was the only alternative that matched (albeit with a '>' after
10575      it). We can assume it's just a typo from the user, and a
10576      diagnostic will then be issued.  */
10577   return cp_parser_type_id (parser);
10578 }
10579
10580 /* Parse an explicit-instantiation.
10581
10582    explicit-instantiation:
10583      template declaration
10584
10585    Although the standard says `declaration', what it really means is:
10586
10587    explicit-instantiation:
10588      template decl-specifier-seq [opt] declarator [opt] ;
10589
10590    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10591    supposed to be allowed.  A defect report has been filed about this
10592    issue.
10593
10594    GNU Extension:
10595
10596    explicit-instantiation:
10597      storage-class-specifier template
10598        decl-specifier-seq [opt] declarator [opt] ;
10599      function-specifier template
10600        decl-specifier-seq [opt] declarator [opt] ;  */
10601
10602 static void
10603 cp_parser_explicit_instantiation (cp_parser* parser)
10604 {
10605   int declares_class_or_enum;
10606   cp_decl_specifier_seq decl_specifiers;
10607   tree extension_specifier = NULL_TREE;
10608   cp_token *token;
10609
10610   /* Look for an (optional) storage-class-specifier or
10611      function-specifier.  */
10612   if (cp_parser_allow_gnu_extensions_p (parser))
10613     {
10614       extension_specifier
10615         = cp_parser_storage_class_specifier_opt (parser);
10616       if (!extension_specifier)
10617         extension_specifier
10618           = cp_parser_function_specifier_opt (parser,
10619                                               /*decl_specs=*/NULL);
10620     }
10621
10622   /* Look for the `template' keyword.  */
10623   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10624   /* Let the front end know that we are processing an explicit
10625      instantiation.  */
10626   begin_explicit_instantiation ();
10627   /* [temp.explicit] says that we are supposed to ignore access
10628      control while processing explicit instantiation directives.  */
10629   push_deferring_access_checks (dk_no_check);
10630   /* Parse a decl-specifier-seq.  */
10631   token = cp_lexer_peek_token (parser->lexer);
10632   cp_parser_decl_specifier_seq (parser,
10633                                 CP_PARSER_FLAGS_OPTIONAL,
10634                                 &decl_specifiers,
10635                                 &declares_class_or_enum);
10636   /* If there was exactly one decl-specifier, and it declared a class,
10637      and there's no declarator, then we have an explicit type
10638      instantiation.  */
10639   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10640     {
10641       tree type;
10642
10643       type = check_tag_decl (&decl_specifiers);
10644       /* Turn access control back on for names used during
10645          template instantiation.  */
10646       pop_deferring_access_checks ();
10647       if (type)
10648         do_type_instantiation (type, extension_specifier,
10649                                /*complain=*/tf_error);
10650     }
10651   else
10652     {
10653       cp_declarator *declarator;
10654       tree decl;
10655
10656       /* Parse the declarator.  */
10657       declarator
10658         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10659                                 /*ctor_dtor_or_conv_p=*/NULL,
10660                                 /*parenthesized_p=*/NULL,
10661                                 /*member_p=*/false);
10662       if (declares_class_or_enum & 2)
10663         cp_parser_check_for_definition_in_return_type (declarator,
10664                                                        decl_specifiers.type,
10665                                                        decl_specifiers.type_location);
10666       if (declarator != cp_error_declarator)
10667         {
10668           decl = grokdeclarator (declarator, &decl_specifiers,
10669                                  NORMAL, 0, &decl_specifiers.attributes);
10670           /* Turn access control back on for names used during
10671              template instantiation.  */
10672           pop_deferring_access_checks ();
10673           /* Do the explicit instantiation.  */
10674           do_decl_instantiation (decl, extension_specifier);
10675         }
10676       else
10677         {
10678           pop_deferring_access_checks ();
10679           /* Skip the body of the explicit instantiation.  */
10680           cp_parser_skip_to_end_of_statement (parser);
10681         }
10682     }
10683   /* We're done with the instantiation.  */
10684   end_explicit_instantiation ();
10685
10686   cp_parser_consume_semicolon_at_end_of_statement (parser);
10687 }
10688
10689 /* Parse an explicit-specialization.
10690
10691    explicit-specialization:
10692      template < > declaration
10693
10694    Although the standard says `declaration', what it really means is:
10695
10696    explicit-specialization:
10697      template <> decl-specifier [opt] init-declarator [opt] ;
10698      template <> function-definition
10699      template <> explicit-specialization
10700      template <> template-declaration  */
10701
10702 static void
10703 cp_parser_explicit_specialization (cp_parser* parser)
10704 {
10705   bool need_lang_pop;
10706   cp_token *token = cp_lexer_peek_token (parser->lexer);
10707
10708   /* Look for the `template' keyword.  */
10709   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10710   /* Look for the `<'.  */
10711   cp_parser_require (parser, CPP_LESS, "%<<%>");
10712   /* Look for the `>'.  */
10713   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10714   /* We have processed another parameter list.  */
10715   ++parser->num_template_parameter_lists;
10716   /* [temp]
10717
10718      A template ... explicit specialization ... shall not have C
10719      linkage.  */
10720   if (current_lang_name == lang_name_c)
10721     {
10722       error ("%Htemplate specialization with C linkage", &token->location);
10723       /* Give it C++ linkage to avoid confusing other parts of the
10724          front end.  */
10725       push_lang_context (lang_name_cplusplus);
10726       need_lang_pop = true;
10727     }
10728   else
10729     need_lang_pop = false;
10730   /* Let the front end know that we are beginning a specialization.  */
10731   if (!begin_specialization ())
10732     {
10733       end_specialization ();
10734       cp_parser_skip_to_end_of_block_or_statement (parser);
10735       return;
10736     }
10737
10738   /* If the next keyword is `template', we need to figure out whether
10739      or not we're looking a template-declaration.  */
10740   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10741     {
10742       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10743           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10744         cp_parser_template_declaration_after_export (parser,
10745                                                      /*member_p=*/false);
10746       else
10747         cp_parser_explicit_specialization (parser);
10748     }
10749   else
10750     /* Parse the dependent declaration.  */
10751     cp_parser_single_declaration (parser,
10752                                   /*checks=*/NULL,
10753                                   /*member_p=*/false,
10754                                   /*explicit_specialization_p=*/true,
10755                                   /*friend_p=*/NULL);
10756   /* We're done with the specialization.  */
10757   end_specialization ();
10758   /* For the erroneous case of a template with C linkage, we pushed an
10759      implicit C++ linkage scope; exit that scope now.  */
10760   if (need_lang_pop)
10761     pop_lang_context ();
10762   /* We're done with this parameter list.  */
10763   --parser->num_template_parameter_lists;
10764 }
10765
10766 /* Parse a type-specifier.
10767
10768    type-specifier:
10769      simple-type-specifier
10770      class-specifier
10771      enum-specifier
10772      elaborated-type-specifier
10773      cv-qualifier
10774
10775    GNU Extension:
10776
10777    type-specifier:
10778      __complex__
10779
10780    Returns a representation of the type-specifier.  For a
10781    class-specifier, enum-specifier, or elaborated-type-specifier, a
10782    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10783
10784    The parser flags FLAGS is used to control type-specifier parsing.
10785
10786    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10787    in a decl-specifier-seq.
10788
10789    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10790    class-specifier, enum-specifier, or elaborated-type-specifier, then
10791    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10792    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10793    zero.
10794
10795    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10796    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10797    is set to FALSE.  */
10798
10799 static tree
10800 cp_parser_type_specifier (cp_parser* parser,
10801                           cp_parser_flags flags,
10802                           cp_decl_specifier_seq *decl_specs,
10803                           bool is_declaration,
10804                           int* declares_class_or_enum,
10805                           bool* is_cv_qualifier)
10806 {
10807   tree type_spec = NULL_TREE;
10808   cp_token *token;
10809   enum rid keyword;
10810   cp_decl_spec ds = ds_last;
10811
10812   /* Assume this type-specifier does not declare a new type.  */
10813   if (declares_class_or_enum)
10814     *declares_class_or_enum = 0;
10815   /* And that it does not specify a cv-qualifier.  */
10816   if (is_cv_qualifier)
10817     *is_cv_qualifier = false;
10818   /* Peek at the next token.  */
10819   token = cp_lexer_peek_token (parser->lexer);
10820
10821   /* If we're looking at a keyword, we can use that to guide the
10822      production we choose.  */
10823   keyword = token->keyword;
10824   switch (keyword)
10825     {
10826     case RID_ENUM:
10827       /* Look for the enum-specifier.  */
10828       type_spec = cp_parser_enum_specifier (parser);
10829       /* If that worked, we're done.  */
10830       if (type_spec)
10831         {
10832           if (declares_class_or_enum)
10833             *declares_class_or_enum = 2;
10834           if (decl_specs)
10835             cp_parser_set_decl_spec_type (decl_specs,
10836                                           type_spec,
10837                                           token->location,
10838                                           /*user_defined_p=*/true);
10839           return type_spec;
10840         }
10841       else
10842         goto elaborated_type_specifier;
10843
10844       /* Any of these indicate either a class-specifier, or an
10845          elaborated-type-specifier.  */
10846     case RID_CLASS:
10847     case RID_STRUCT:
10848     case RID_UNION:
10849       /* Parse tentatively so that we can back up if we don't find a
10850          class-specifier.  */
10851       cp_parser_parse_tentatively (parser);
10852       /* Look for the class-specifier.  */
10853       type_spec = cp_parser_class_specifier (parser);
10854       /* If that worked, we're done.  */
10855       if (cp_parser_parse_definitely (parser))
10856         {
10857           if (declares_class_or_enum)
10858             *declares_class_or_enum = 2;
10859           if (decl_specs)
10860             cp_parser_set_decl_spec_type (decl_specs,
10861                                           type_spec,
10862                                           token->location,
10863                                           /*user_defined_p=*/true);
10864           return type_spec;
10865         }
10866
10867       /* Fall through.  */
10868     elaborated_type_specifier:
10869       /* We're declaring (not defining) a class or enum.  */
10870       if (declares_class_or_enum)
10871         *declares_class_or_enum = 1;
10872
10873       /* Fall through.  */
10874     case RID_TYPENAME:
10875       /* Look for an elaborated-type-specifier.  */
10876       type_spec
10877         = (cp_parser_elaborated_type_specifier
10878            (parser,
10879             decl_specs && decl_specs->specs[(int) ds_friend],
10880             is_declaration));
10881       if (decl_specs)
10882         cp_parser_set_decl_spec_type (decl_specs,
10883                                       type_spec,
10884                                       token->location,
10885                                       /*user_defined_p=*/true);
10886       return type_spec;
10887
10888     case RID_CONST:
10889       ds = ds_const;
10890       if (is_cv_qualifier)
10891         *is_cv_qualifier = true;
10892       break;
10893
10894     case RID_VOLATILE:
10895       ds = ds_volatile;
10896       if (is_cv_qualifier)
10897         *is_cv_qualifier = true;
10898       break;
10899
10900     case RID_RESTRICT:
10901       ds = ds_restrict;
10902       if (is_cv_qualifier)
10903         *is_cv_qualifier = true;
10904       break;
10905
10906     case RID_COMPLEX:
10907       /* The `__complex__' keyword is a GNU extension.  */
10908       ds = ds_complex;
10909       break;
10910
10911     default:
10912       break;
10913     }
10914
10915   /* Handle simple keywords.  */
10916   if (ds != ds_last)
10917     {
10918       if (decl_specs)
10919         {
10920           ++decl_specs->specs[(int)ds];
10921           decl_specs->any_specifiers_p = true;
10922         }
10923       return cp_lexer_consume_token (parser->lexer)->u.value;
10924     }
10925
10926   /* If we do not already have a type-specifier, assume we are looking
10927      at a simple-type-specifier.  */
10928   type_spec = cp_parser_simple_type_specifier (parser,
10929                                                decl_specs,
10930                                                flags);
10931
10932   /* If we didn't find a type-specifier, and a type-specifier was not
10933      optional in this context, issue an error message.  */
10934   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10935     {
10936       cp_parser_error (parser, "expected type specifier");
10937       return error_mark_node;
10938     }
10939
10940   return type_spec;
10941 }
10942
10943 /* Parse a simple-type-specifier.
10944
10945    simple-type-specifier:
10946      :: [opt] nested-name-specifier [opt] type-name
10947      :: [opt] nested-name-specifier template template-id
10948      char
10949      wchar_t
10950      bool
10951      short
10952      int
10953      long
10954      signed
10955      unsigned
10956      float
10957      double
10958      void
10959
10960    C++0x Extension:
10961
10962    simple-type-specifier:
10963      auto
10964      decltype ( expression )   
10965      char16_t
10966      char32_t
10967
10968    GNU Extension:
10969
10970    simple-type-specifier:
10971      __typeof__ unary-expression
10972      __typeof__ ( type-id )
10973
10974    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10975    appropriately updated.  */
10976
10977 static tree
10978 cp_parser_simple_type_specifier (cp_parser* parser,
10979                                  cp_decl_specifier_seq *decl_specs,
10980                                  cp_parser_flags flags)
10981 {
10982   tree type = NULL_TREE;
10983   cp_token *token;
10984
10985   /* Peek at the next token.  */
10986   token = cp_lexer_peek_token (parser->lexer);
10987
10988   /* If we're looking at a keyword, things are easy.  */
10989   switch (token->keyword)
10990     {
10991     case RID_CHAR:
10992       if (decl_specs)
10993         decl_specs->explicit_char_p = true;
10994       type = char_type_node;
10995       break;
10996     case RID_CHAR16:
10997       type = char16_type_node;
10998       break;
10999     case RID_CHAR32:
11000       type = char32_type_node;
11001       break;
11002     case RID_WCHAR:
11003       type = wchar_type_node;
11004       break;
11005     case RID_BOOL:
11006       type = boolean_type_node;
11007       break;
11008     case RID_SHORT:
11009       if (decl_specs)
11010         ++decl_specs->specs[(int) ds_short];
11011       type = short_integer_type_node;
11012       break;
11013     case RID_INT:
11014       if (decl_specs)
11015         decl_specs->explicit_int_p = true;
11016       type = integer_type_node;
11017       break;
11018     case RID_LONG:
11019       if (decl_specs)
11020         ++decl_specs->specs[(int) ds_long];
11021       type = long_integer_type_node;
11022       break;
11023     case RID_SIGNED:
11024       if (decl_specs)
11025         ++decl_specs->specs[(int) ds_signed];
11026       type = integer_type_node;
11027       break;
11028     case RID_UNSIGNED:
11029       if (decl_specs)
11030         ++decl_specs->specs[(int) ds_unsigned];
11031       type = unsigned_type_node;
11032       break;
11033     case RID_FLOAT:
11034       type = float_type_node;
11035       break;
11036     case RID_DOUBLE:
11037       type = double_type_node;
11038       break;
11039     case RID_VOID:
11040       type = void_type_node;
11041       break;
11042       
11043     case RID_AUTO:
11044       if (cxx_dialect != cxx98)
11045         {
11046           /* Consume the token.  */
11047           cp_lexer_consume_token (parser->lexer);
11048           /* We do not yet support the use of `auto' as a
11049              type-specifier.  */
11050           error ("%HC++0x %<auto%> specifier not supported", &token->location);
11051         }
11052       break;
11053
11054     case RID_DECLTYPE:
11055       /* Parse the `decltype' type.  */
11056       type = cp_parser_decltype (parser);
11057
11058       if (decl_specs)
11059         cp_parser_set_decl_spec_type (decl_specs, type,
11060                                       token->location,
11061                                       /*user_defined_p=*/true);
11062
11063       return type;
11064
11065     case RID_TYPEOF:
11066       /* Consume the `typeof' token.  */
11067       cp_lexer_consume_token (parser->lexer);
11068       /* Parse the operand to `typeof'.  */
11069       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11070       /* If it is not already a TYPE, take its type.  */
11071       if (!TYPE_P (type))
11072         type = finish_typeof (type);
11073
11074       if (decl_specs)
11075         cp_parser_set_decl_spec_type (decl_specs, type,
11076                                       token->location,
11077                                       /*user_defined_p=*/true);
11078
11079       return type;
11080
11081     default:
11082       break;
11083     }
11084
11085   /* If the type-specifier was for a built-in type, we're done.  */
11086   if (type)
11087     {
11088       tree id;
11089
11090       /* Record the type.  */
11091       if (decl_specs
11092           && (token->keyword != RID_SIGNED
11093               && token->keyword != RID_UNSIGNED
11094               && token->keyword != RID_SHORT
11095               && token->keyword != RID_LONG))
11096         cp_parser_set_decl_spec_type (decl_specs,
11097                                       type,
11098                                       token->location,
11099                                       /*user_defined=*/false);
11100       if (decl_specs)
11101         decl_specs->any_specifiers_p = true;
11102
11103       /* Consume the token.  */
11104       id = cp_lexer_consume_token (parser->lexer)->u.value;
11105
11106       /* There is no valid C++ program where a non-template type is
11107          followed by a "<".  That usually indicates that the user thought
11108          that the type was a template.  */
11109       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11110
11111       return TYPE_NAME (type);
11112     }
11113
11114   /* The type-specifier must be a user-defined type.  */
11115   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11116     {
11117       bool qualified_p;
11118       bool global_p;
11119
11120       /* Don't gobble tokens or issue error messages if this is an
11121          optional type-specifier.  */
11122       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11123         cp_parser_parse_tentatively (parser);
11124
11125       /* Look for the optional `::' operator.  */
11126       global_p
11127         = (cp_parser_global_scope_opt (parser,
11128                                        /*current_scope_valid_p=*/false)
11129            != NULL_TREE);
11130       /* Look for the nested-name specifier.  */
11131       qualified_p
11132         = (cp_parser_nested_name_specifier_opt (parser,
11133                                                 /*typename_keyword_p=*/false,
11134                                                 /*check_dependency_p=*/true,
11135                                                 /*type_p=*/false,
11136                                                 /*is_declaration=*/false)
11137            != NULL_TREE);
11138       token = cp_lexer_peek_token (parser->lexer);
11139       /* If we have seen a nested-name-specifier, and the next token
11140          is `template', then we are using the template-id production.  */
11141       if (parser->scope
11142           && cp_parser_optional_template_keyword (parser))
11143         {
11144           /* Look for the template-id.  */
11145           type = cp_parser_template_id (parser,
11146                                         /*template_keyword_p=*/true,
11147                                         /*check_dependency_p=*/true,
11148                                         /*is_declaration=*/false);
11149           /* If the template-id did not name a type, we are out of
11150              luck.  */
11151           if (TREE_CODE (type) != TYPE_DECL)
11152             {
11153               cp_parser_error (parser, "expected template-id for type");
11154               type = NULL_TREE;
11155             }
11156         }
11157       /* Otherwise, look for a type-name.  */
11158       else
11159         type = cp_parser_type_name (parser);
11160       /* Keep track of all name-lookups performed in class scopes.  */
11161       if (type
11162           && !global_p
11163           && !qualified_p
11164           && TREE_CODE (type) == TYPE_DECL
11165           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11166         maybe_note_name_used_in_class (DECL_NAME (type), type);
11167       /* If it didn't work out, we don't have a TYPE.  */
11168       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11169           && !cp_parser_parse_definitely (parser))
11170         type = NULL_TREE;
11171       if (type && decl_specs)
11172         cp_parser_set_decl_spec_type (decl_specs, type,
11173                                       token->location,
11174                                       /*user_defined=*/true);
11175     }
11176
11177   /* If we didn't get a type-name, issue an error message.  */
11178   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11179     {
11180       cp_parser_error (parser, "expected type-name");
11181       return error_mark_node;
11182     }
11183
11184   /* There is no valid C++ program where a non-template type is
11185      followed by a "<".  That usually indicates that the user thought
11186      that the type was a template.  */
11187   if (type && type != error_mark_node)
11188     {
11189       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11190          If it is, then the '<'...'>' enclose protocol names rather than
11191          template arguments, and so everything is fine.  */
11192       if (c_dialect_objc ()
11193           && (objc_is_id (type) || objc_is_class_name (type)))
11194         {
11195           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11196           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11197
11198           /* Clobber the "unqualified" type previously entered into
11199              DECL_SPECS with the new, improved protocol-qualified version.  */
11200           if (decl_specs)
11201             decl_specs->type = qual_type;
11202
11203           return qual_type;
11204         }
11205
11206       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11207                                                token->location);
11208     }
11209
11210   return type;
11211 }
11212
11213 /* Parse a type-name.
11214
11215    type-name:
11216      class-name
11217      enum-name
11218      typedef-name
11219
11220    enum-name:
11221      identifier
11222
11223    typedef-name:
11224      identifier
11225
11226    Returns a TYPE_DECL for the type.  */
11227
11228 static tree
11229 cp_parser_type_name (cp_parser* parser)
11230 {
11231   tree type_decl;
11232
11233   /* We can't know yet whether it is a class-name or not.  */
11234   cp_parser_parse_tentatively (parser);
11235   /* Try a class-name.  */
11236   type_decl = cp_parser_class_name (parser,
11237                                     /*typename_keyword_p=*/false,
11238                                     /*template_keyword_p=*/false,
11239                                     none_type,
11240                                     /*check_dependency_p=*/true,
11241                                     /*class_head_p=*/false,
11242                                     /*is_declaration=*/false);
11243   /* If it's not a class-name, keep looking.  */
11244   if (!cp_parser_parse_definitely (parser))
11245     {
11246       /* It must be a typedef-name or an enum-name.  */
11247       return cp_parser_nonclass_name (parser);
11248     }
11249
11250   return type_decl;
11251 }
11252
11253 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11254
11255    enum-name:
11256      identifier
11257
11258    typedef-name:
11259      identifier
11260
11261    Returns a TYPE_DECL for the type.  */
11262
11263 static tree
11264 cp_parser_nonclass_name (cp_parser* parser)
11265 {
11266   tree type_decl;
11267   tree identifier;
11268
11269   cp_token *token = cp_lexer_peek_token (parser->lexer);
11270   identifier = cp_parser_identifier (parser);
11271   if (identifier == error_mark_node)
11272     return error_mark_node;
11273
11274   /* Look up the type-name.  */
11275   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11276
11277   if (TREE_CODE (type_decl) != TYPE_DECL
11278       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11279     {
11280       /* See if this is an Objective-C type.  */
11281       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11282       tree type = objc_get_protocol_qualified_type (identifier, protos);
11283       if (type)
11284         type_decl = TYPE_NAME (type);
11285     }
11286   
11287   /* Issue an error if we did not find a type-name.  */
11288   if (TREE_CODE (type_decl) != TYPE_DECL)
11289     {
11290       if (!cp_parser_simulate_error (parser))
11291         cp_parser_name_lookup_error (parser, identifier, type_decl,
11292                                      "is not a type", token->location);
11293       return error_mark_node;
11294     }
11295   /* Remember that the name was used in the definition of the
11296      current class so that we can check later to see if the
11297      meaning would have been different after the class was
11298      entirely defined.  */
11299   else if (type_decl != error_mark_node
11300            && !parser->scope)
11301     maybe_note_name_used_in_class (identifier, type_decl);
11302   
11303   return type_decl;
11304 }
11305
11306 /* Parse an elaborated-type-specifier.  Note that the grammar given
11307    here incorporates the resolution to DR68.
11308
11309    elaborated-type-specifier:
11310      class-key :: [opt] nested-name-specifier [opt] identifier
11311      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11312      enum :: [opt] nested-name-specifier [opt] identifier
11313      typename :: [opt] nested-name-specifier identifier
11314      typename :: [opt] nested-name-specifier template [opt]
11315        template-id
11316
11317    GNU extension:
11318
11319    elaborated-type-specifier:
11320      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11321      class-key attributes :: [opt] nested-name-specifier [opt]
11322                template [opt] template-id
11323      enum attributes :: [opt] nested-name-specifier [opt] identifier
11324
11325    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11326    declared `friend'.  If IS_DECLARATION is TRUE, then this
11327    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11328    something is being declared.
11329
11330    Returns the TYPE specified.  */
11331
11332 static tree
11333 cp_parser_elaborated_type_specifier (cp_parser* parser,
11334                                      bool is_friend,
11335                                      bool is_declaration)
11336 {
11337   enum tag_types tag_type;
11338   tree identifier;
11339   tree type = NULL_TREE;
11340   tree attributes = NULL_TREE;
11341   cp_token *token = NULL;
11342
11343   /* See if we're looking at the `enum' keyword.  */
11344   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11345     {
11346       /* Consume the `enum' token.  */
11347       cp_lexer_consume_token (parser->lexer);
11348       /* Remember that it's an enumeration type.  */
11349       tag_type = enum_type;
11350       /* Parse the attributes.  */
11351       attributes = cp_parser_attributes_opt (parser);
11352     }
11353   /* Or, it might be `typename'.  */
11354   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11355                                            RID_TYPENAME))
11356     {
11357       /* Consume the `typename' token.  */
11358       cp_lexer_consume_token (parser->lexer);
11359       /* Remember that it's a `typename' type.  */
11360       tag_type = typename_type;
11361       /* The `typename' keyword is only allowed in templates.  */
11362       if (!processing_template_decl)
11363         permerror ("using %<typename%> outside of template");
11364     }
11365   /* Otherwise it must be a class-key.  */
11366   else
11367     {
11368       tag_type = cp_parser_class_key (parser);
11369       if (tag_type == none_type)
11370         return error_mark_node;
11371       /* Parse the attributes.  */
11372       attributes = cp_parser_attributes_opt (parser);
11373     }
11374
11375   /* Look for the `::' operator.  */
11376   cp_parser_global_scope_opt (parser,
11377                               /*current_scope_valid_p=*/false);
11378   /* Look for the nested-name-specifier.  */
11379   if (tag_type == typename_type)
11380     {
11381       if (!cp_parser_nested_name_specifier (parser,
11382                                            /*typename_keyword_p=*/true,
11383                                            /*check_dependency_p=*/true,
11384                                            /*type_p=*/true,
11385                                             is_declaration))
11386         return error_mark_node;
11387     }
11388   else
11389     /* Even though `typename' is not present, the proposed resolution
11390        to Core Issue 180 says that in `class A<T>::B', `B' should be
11391        considered a type-name, even if `A<T>' is dependent.  */
11392     cp_parser_nested_name_specifier_opt (parser,
11393                                          /*typename_keyword_p=*/true,
11394                                          /*check_dependency_p=*/true,
11395                                          /*type_p=*/true,
11396                                          is_declaration);
11397  /* For everything but enumeration types, consider a template-id.
11398     For an enumeration type, consider only a plain identifier.  */
11399   if (tag_type != enum_type)
11400     {
11401       bool template_p = false;
11402       tree decl;
11403
11404       /* Allow the `template' keyword.  */
11405       template_p = cp_parser_optional_template_keyword (parser);
11406       /* If we didn't see `template', we don't know if there's a
11407          template-id or not.  */
11408       if (!template_p)
11409         cp_parser_parse_tentatively (parser);
11410       /* Parse the template-id.  */
11411       token = cp_lexer_peek_token (parser->lexer);
11412       decl = cp_parser_template_id (parser, template_p,
11413                                     /*check_dependency_p=*/true,
11414                                     is_declaration);
11415       /* If we didn't find a template-id, look for an ordinary
11416          identifier.  */
11417       if (!template_p && !cp_parser_parse_definitely (parser))
11418         ;
11419       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11420          in effect, then we must assume that, upon instantiation, the
11421          template will correspond to a class.  */
11422       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11423                && tag_type == typename_type)
11424         type = make_typename_type (parser->scope, decl,
11425                                    typename_type,
11426                                    /*complain=*/tf_error);
11427       else
11428         type = TREE_TYPE (decl);
11429     }
11430
11431   if (!type)
11432     {
11433       token = cp_lexer_peek_token (parser->lexer);
11434       identifier = cp_parser_identifier (parser);
11435
11436       if (identifier == error_mark_node)
11437         {
11438           parser->scope = NULL_TREE;
11439           return error_mark_node;
11440         }
11441
11442       /* For a `typename', we needn't call xref_tag.  */
11443       if (tag_type == typename_type
11444           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11445         return cp_parser_make_typename_type (parser, parser->scope,
11446                                              identifier,
11447                                              token->location);
11448       /* Look up a qualified name in the usual way.  */
11449       if (parser->scope)
11450         {
11451           tree decl;
11452           tree ambiguous_decls;
11453
11454           decl = cp_parser_lookup_name (parser, identifier,
11455                                         tag_type,
11456                                         /*is_template=*/false,
11457                                         /*is_namespace=*/false,
11458                                         /*check_dependency=*/true,
11459                                         &ambiguous_decls,
11460                                         token->location);
11461
11462           /* If the lookup was ambiguous, an error will already have been
11463              issued.  */
11464           if (ambiguous_decls)
11465             return error_mark_node;
11466
11467           /* If we are parsing friend declaration, DECL may be a
11468              TEMPLATE_DECL tree node here.  However, we need to check
11469              whether this TEMPLATE_DECL results in valid code.  Consider
11470              the following example:
11471
11472                namespace N {
11473                  template <class T> class C {};
11474                }
11475                class X {
11476                  template <class T> friend class N::C; // #1, valid code
11477                };
11478                template <class T> class Y {
11479                  friend class N::C;                    // #2, invalid code
11480                };
11481
11482              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11483              name lookup of `N::C'.  We see that friend declaration must
11484              be template for the code to be valid.  Note that
11485              processing_template_decl does not work here since it is
11486              always 1 for the above two cases.  */
11487
11488           decl = (cp_parser_maybe_treat_template_as_class
11489                   (decl, /*tag_name_p=*/is_friend
11490                          && parser->num_template_parameter_lists));
11491
11492           if (TREE_CODE (decl) != TYPE_DECL)
11493             {
11494               cp_parser_diagnose_invalid_type_name (parser,
11495                                                     parser->scope,
11496                                                     identifier,
11497                                                     token->location);
11498               return error_mark_node;
11499             }
11500
11501           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11502             {
11503               bool allow_template = (parser->num_template_parameter_lists
11504                                       || DECL_SELF_REFERENCE_P (decl));
11505               type = check_elaborated_type_specifier (tag_type, decl, 
11506                                                       allow_template);
11507
11508               if (type == error_mark_node)
11509                 return error_mark_node;
11510             }
11511
11512           /* Forward declarations of nested types, such as
11513
11514                class C1::C2;
11515                class C1::C2::C3;
11516
11517              are invalid unless all components preceding the final '::'
11518              are complete.  If all enclosing types are complete, these
11519              declarations become merely pointless.
11520
11521              Invalid forward declarations of nested types are errors
11522              caught elsewhere in parsing.  Those that are pointless arrive
11523              here.  */
11524
11525           if (cp_parser_declares_only_class_p (parser)
11526               && !is_friend && !processing_explicit_instantiation)
11527             warning (0, "declaration %qD does not declare anything", decl);
11528
11529           type = TREE_TYPE (decl);
11530         }
11531       else
11532         {
11533           /* An elaborated-type-specifier sometimes introduces a new type and
11534              sometimes names an existing type.  Normally, the rule is that it
11535              introduces a new type only if there is not an existing type of
11536              the same name already in scope.  For example, given:
11537
11538                struct S {};
11539                void f() { struct S s; }
11540
11541              the `struct S' in the body of `f' is the same `struct S' as in
11542              the global scope; the existing definition is used.  However, if
11543              there were no global declaration, this would introduce a new
11544              local class named `S'.
11545
11546              An exception to this rule applies to the following code:
11547
11548                namespace N { struct S; }
11549
11550              Here, the elaborated-type-specifier names a new type
11551              unconditionally; even if there is already an `S' in the
11552              containing scope this declaration names a new type.
11553              This exception only applies if the elaborated-type-specifier
11554              forms the complete declaration:
11555
11556                [class.name]
11557
11558                A declaration consisting solely of `class-key identifier ;' is
11559                either a redeclaration of the name in the current scope or a
11560                forward declaration of the identifier as a class name.  It
11561                introduces the name into the current scope.
11562
11563              We are in this situation precisely when the next token is a `;'.
11564
11565              An exception to the exception is that a `friend' declaration does
11566              *not* name a new type; i.e., given:
11567
11568                struct S { friend struct T; };
11569
11570              `T' is not a new type in the scope of `S'.
11571
11572              Also, `new struct S' or `sizeof (struct S)' never results in the
11573              definition of a new type; a new type can only be declared in a
11574              declaration context.  */
11575
11576           tag_scope ts;
11577           bool template_p;
11578
11579           if (is_friend)
11580             /* Friends have special name lookup rules.  */
11581             ts = ts_within_enclosing_non_class;
11582           else if (is_declaration
11583                    && cp_lexer_next_token_is (parser->lexer,
11584                                               CPP_SEMICOLON))
11585             /* This is a `class-key identifier ;' */
11586             ts = ts_current;
11587           else
11588             ts = ts_global;
11589
11590           template_p =
11591             (parser->num_template_parameter_lists
11592              && (cp_parser_next_token_starts_class_definition_p (parser)
11593                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11594           /* An unqualified name was used to reference this type, so
11595              there were no qualifying templates.  */
11596           if (!cp_parser_check_template_parameters (parser,
11597                                                     /*num_templates=*/0,
11598                                                     token->location))
11599             return error_mark_node;
11600           type = xref_tag (tag_type, identifier, ts, template_p);
11601         }
11602     }
11603
11604   if (type == error_mark_node)
11605     return error_mark_node;
11606
11607   /* Allow attributes on forward declarations of classes.  */
11608   if (attributes)
11609     {
11610       if (TREE_CODE (type) == TYPENAME_TYPE)
11611         warning (OPT_Wattributes,
11612                  "attributes ignored on uninstantiated type");
11613       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11614                && ! processing_explicit_instantiation)
11615         warning (OPT_Wattributes,
11616                  "attributes ignored on template instantiation");
11617       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11618         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11619       else
11620         warning (OPT_Wattributes,
11621                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11622     }
11623
11624   if (tag_type != enum_type)
11625     cp_parser_check_class_key (tag_type, type);
11626
11627   /* A "<" cannot follow an elaborated type specifier.  If that
11628      happens, the user was probably trying to form a template-id.  */
11629   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11630
11631   return type;
11632 }
11633
11634 /* Parse an enum-specifier.
11635
11636    enum-specifier:
11637      enum identifier [opt] { enumerator-list [opt] }
11638
11639    GNU Extensions:
11640      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11641        attributes[opt]
11642
11643    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11644    if the token stream isn't an enum-specifier after all.  */
11645
11646 static tree
11647 cp_parser_enum_specifier (cp_parser* parser)
11648 {
11649   tree identifier;
11650   tree type;
11651   tree attributes;
11652
11653   /* Parse tentatively so that we can back up if we don't find a
11654      enum-specifier.  */
11655   cp_parser_parse_tentatively (parser);
11656
11657   /* Caller guarantees that the current token is 'enum', an identifier
11658      possibly follows, and the token after that is an opening brace.
11659      If we don't have an identifier, fabricate an anonymous name for
11660      the enumeration being defined.  */
11661   cp_lexer_consume_token (parser->lexer);
11662
11663   attributes = cp_parser_attributes_opt (parser);
11664
11665   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11666     identifier = cp_parser_identifier (parser);
11667   else
11668     identifier = make_anon_name ();
11669
11670   /* Look for the `{' but don't consume it yet.  */
11671   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11672     cp_parser_simulate_error (parser);
11673
11674   if (!cp_parser_parse_definitely (parser))
11675     return NULL_TREE;
11676
11677   /* Issue an error message if type-definitions are forbidden here.  */
11678   if (!cp_parser_check_type_definition (parser))
11679     type = error_mark_node;
11680   else
11681     /* Create the new type.  We do this before consuming the opening
11682        brace so the enum will be recorded as being on the line of its
11683        tag (or the 'enum' keyword, if there is no tag).  */
11684     type = start_enum (identifier);
11685   
11686   /* Consume the opening brace.  */
11687   cp_lexer_consume_token (parser->lexer);
11688
11689   if (type == error_mark_node)
11690     {
11691       cp_parser_skip_to_end_of_block_or_statement (parser);
11692       return error_mark_node;
11693     }
11694
11695   /* If the next token is not '}', then there are some enumerators.  */
11696   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11697     cp_parser_enumerator_list (parser, type);
11698
11699   /* Consume the final '}'.  */
11700   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11701
11702   /* Look for trailing attributes to apply to this enumeration, and
11703      apply them if appropriate.  */
11704   if (cp_parser_allow_gnu_extensions_p (parser))
11705     {
11706       tree trailing_attr = cp_parser_attributes_opt (parser);
11707       cplus_decl_attributes (&type,
11708                              trailing_attr,
11709                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11710     }
11711
11712   /* Finish up the enumeration.  */
11713   finish_enum (type);
11714
11715   return type;
11716 }
11717
11718 /* Parse an enumerator-list.  The enumerators all have the indicated
11719    TYPE.
11720
11721    enumerator-list:
11722      enumerator-definition
11723      enumerator-list , enumerator-definition  */
11724
11725 static void
11726 cp_parser_enumerator_list (cp_parser* parser, tree type)
11727 {
11728   while (true)
11729     {
11730       /* Parse an enumerator-definition.  */
11731       cp_parser_enumerator_definition (parser, type);
11732
11733       /* If the next token is not a ',', we've reached the end of
11734          the list.  */
11735       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11736         break;
11737       /* Otherwise, consume the `,' and keep going.  */
11738       cp_lexer_consume_token (parser->lexer);
11739       /* If the next token is a `}', there is a trailing comma.  */
11740       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11741         {
11742           if (pedantic && !in_system_header)
11743             pedwarn ("comma at end of enumerator list");
11744           break;
11745         }
11746     }
11747 }
11748
11749 /* Parse an enumerator-definition.  The enumerator has the indicated
11750    TYPE.
11751
11752    enumerator-definition:
11753      enumerator
11754      enumerator = constant-expression
11755
11756    enumerator:
11757      identifier  */
11758
11759 static void
11760 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11761 {
11762   tree identifier;
11763   tree value;
11764
11765   /* Look for the identifier.  */
11766   identifier = cp_parser_identifier (parser);
11767   if (identifier == error_mark_node)
11768     return;
11769
11770   /* If the next token is an '=', then there is an explicit value.  */
11771   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11772     {
11773       /* Consume the `=' token.  */
11774       cp_lexer_consume_token (parser->lexer);
11775       /* Parse the value.  */
11776       value = cp_parser_constant_expression (parser,
11777                                              /*allow_non_constant_p=*/false,
11778                                              NULL);
11779     }
11780   else
11781     value = NULL_TREE;
11782
11783   /* Create the enumerator.  */
11784   build_enumerator (identifier, value, type);
11785 }
11786
11787 /* Parse a namespace-name.
11788
11789    namespace-name:
11790      original-namespace-name
11791      namespace-alias
11792
11793    Returns the NAMESPACE_DECL for the namespace.  */
11794
11795 static tree
11796 cp_parser_namespace_name (cp_parser* parser)
11797 {
11798   tree identifier;
11799   tree namespace_decl;
11800
11801   cp_token *token = cp_lexer_peek_token (parser->lexer);
11802
11803   /* Get the name of the namespace.  */
11804   identifier = cp_parser_identifier (parser);
11805   if (identifier == error_mark_node)
11806     return error_mark_node;
11807
11808   /* Look up the identifier in the currently active scope.  Look only
11809      for namespaces, due to:
11810
11811        [basic.lookup.udir]
11812
11813        When looking up a namespace-name in a using-directive or alias
11814        definition, only namespace names are considered.
11815
11816      And:
11817
11818        [basic.lookup.qual]
11819
11820        During the lookup of a name preceding the :: scope resolution
11821        operator, object, function, and enumerator names are ignored.
11822
11823      (Note that cp_parser_class_or_namespace_name only calls this
11824      function if the token after the name is the scope resolution
11825      operator.)  */
11826   namespace_decl = cp_parser_lookup_name (parser, identifier,
11827                                           none_type,
11828                                           /*is_template=*/false,
11829                                           /*is_namespace=*/true,
11830                                           /*check_dependency=*/true,
11831                                           /*ambiguous_decls=*/NULL,
11832                                           token->location);
11833   /* If it's not a namespace, issue an error.  */
11834   if (namespace_decl == error_mark_node
11835       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11836     {
11837       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11838         error ("%H%qD is not a namespace-name", &token->location, identifier);
11839       cp_parser_error (parser, "expected namespace-name");
11840       namespace_decl = error_mark_node;
11841     }
11842
11843   return namespace_decl;
11844 }
11845
11846 /* Parse a namespace-definition.
11847
11848    namespace-definition:
11849      named-namespace-definition
11850      unnamed-namespace-definition
11851
11852    named-namespace-definition:
11853      original-namespace-definition
11854      extension-namespace-definition
11855
11856    original-namespace-definition:
11857      namespace identifier { namespace-body }
11858
11859    extension-namespace-definition:
11860      namespace original-namespace-name { namespace-body }
11861
11862    unnamed-namespace-definition:
11863      namespace { namespace-body } */
11864
11865 static void
11866 cp_parser_namespace_definition (cp_parser* parser)
11867 {
11868   tree identifier, attribs;
11869   bool has_visibility;
11870   bool is_inline;
11871
11872   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11873     {
11874       is_inline = true;
11875       cp_lexer_consume_token (parser->lexer);
11876     }
11877   else
11878     is_inline = false;
11879
11880   /* Look for the `namespace' keyword.  */
11881   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11882
11883   /* Get the name of the namespace.  We do not attempt to distinguish
11884      between an original-namespace-definition and an
11885      extension-namespace-definition at this point.  The semantic
11886      analysis routines are responsible for that.  */
11887   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11888     identifier = cp_parser_identifier (parser);
11889   else
11890     identifier = NULL_TREE;
11891
11892   /* Parse any specified attributes.  */
11893   attribs = cp_parser_attributes_opt (parser);
11894
11895   /* Look for the `{' to start the namespace.  */
11896   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11897   /* Start the namespace.  */
11898   push_namespace (identifier);
11899
11900   /* "inline namespace" is equivalent to a stub namespace definition
11901      followed by a strong using directive.  */
11902   if (is_inline)
11903     {
11904       tree name_space = current_namespace;
11905       /* Set up namespace association.  */
11906       DECL_NAMESPACE_ASSOCIATIONS (name_space)
11907         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
11908                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
11909       /* Import the contents of the inline namespace.  */
11910       pop_namespace ();
11911       do_using_directive (name_space);
11912       push_namespace (identifier);
11913     }
11914
11915   has_visibility = handle_namespace_attrs (current_namespace, attribs);
11916
11917   /* Parse the body of the namespace.  */
11918   cp_parser_namespace_body (parser);
11919
11920 #ifdef HANDLE_PRAGMA_VISIBILITY
11921   if (has_visibility)
11922     pop_visibility ();
11923 #endif
11924
11925   /* Finish the namespace.  */
11926   pop_namespace ();
11927   /* Look for the final `}'.  */
11928   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11929 }
11930
11931 /* Parse a namespace-body.
11932
11933    namespace-body:
11934      declaration-seq [opt]  */
11935
11936 static void
11937 cp_parser_namespace_body (cp_parser* parser)
11938 {
11939   cp_parser_declaration_seq_opt (parser);
11940 }
11941
11942 /* Parse a namespace-alias-definition.
11943
11944    namespace-alias-definition:
11945      namespace identifier = qualified-namespace-specifier ;  */
11946
11947 static void
11948 cp_parser_namespace_alias_definition (cp_parser* parser)
11949 {
11950   tree identifier;
11951   tree namespace_specifier;
11952
11953   cp_token *token = cp_lexer_peek_token (parser->lexer);
11954
11955   /* Look for the `namespace' keyword.  */
11956   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11957   /* Look for the identifier.  */
11958   identifier = cp_parser_identifier (parser);
11959   if (identifier == error_mark_node)
11960     return;
11961   /* Look for the `=' token.  */
11962   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11963       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11964     {
11965       error ("%H%<namespace%> definition is not allowed here", &token->location);
11966       /* Skip the definition.  */
11967       cp_lexer_consume_token (parser->lexer);
11968       if (cp_parser_skip_to_closing_brace (parser))
11969         cp_lexer_consume_token (parser->lexer);
11970       return;
11971     }
11972   cp_parser_require (parser, CPP_EQ, "%<=%>");
11973   /* Look for the qualified-namespace-specifier.  */
11974   namespace_specifier
11975     = cp_parser_qualified_namespace_specifier (parser);
11976   /* Look for the `;' token.  */
11977   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11978
11979   /* Register the alias in the symbol table.  */
11980   do_namespace_alias (identifier, namespace_specifier);
11981 }
11982
11983 /* Parse a qualified-namespace-specifier.
11984
11985    qualified-namespace-specifier:
11986      :: [opt] nested-name-specifier [opt] namespace-name
11987
11988    Returns a NAMESPACE_DECL corresponding to the specified
11989    namespace.  */
11990
11991 static tree
11992 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11993 {
11994   /* Look for the optional `::'.  */
11995   cp_parser_global_scope_opt (parser,
11996                               /*current_scope_valid_p=*/false);
11997
11998   /* Look for the optional nested-name-specifier.  */
11999   cp_parser_nested_name_specifier_opt (parser,
12000                                        /*typename_keyword_p=*/false,
12001                                        /*check_dependency_p=*/true,
12002                                        /*type_p=*/false,
12003                                        /*is_declaration=*/true);
12004
12005   return cp_parser_namespace_name (parser);
12006 }
12007
12008 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12009    access declaration.
12010
12011    using-declaration:
12012      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12013      using :: unqualified-id ;  
12014
12015    access-declaration:
12016      qualified-id ;  
12017
12018    */
12019
12020 static bool
12021 cp_parser_using_declaration (cp_parser* parser, 
12022                              bool access_declaration_p)
12023 {
12024   cp_token *token;
12025   bool typename_p = false;
12026   bool global_scope_p;
12027   tree decl;
12028   tree identifier;
12029   tree qscope;
12030
12031   if (access_declaration_p)
12032     cp_parser_parse_tentatively (parser);
12033   else
12034     {
12035       /* Look for the `using' keyword.  */
12036       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12037       
12038       /* Peek at the next token.  */
12039       token = cp_lexer_peek_token (parser->lexer);
12040       /* See if it's `typename'.  */
12041       if (token->keyword == RID_TYPENAME)
12042         {
12043           /* Remember that we've seen it.  */
12044           typename_p = true;
12045           /* Consume the `typename' token.  */
12046           cp_lexer_consume_token (parser->lexer);
12047         }
12048     }
12049
12050   /* Look for the optional global scope qualification.  */
12051   global_scope_p
12052     = (cp_parser_global_scope_opt (parser,
12053                                    /*current_scope_valid_p=*/false)
12054        != NULL_TREE);
12055
12056   /* If we saw `typename', or didn't see `::', then there must be a
12057      nested-name-specifier present.  */
12058   if (typename_p || !global_scope_p)
12059     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12060                                               /*check_dependency_p=*/true,
12061                                               /*type_p=*/false,
12062                                               /*is_declaration=*/true);
12063   /* Otherwise, we could be in either of the two productions.  In that
12064      case, treat the nested-name-specifier as optional.  */
12065   else
12066     qscope = cp_parser_nested_name_specifier_opt (parser,
12067                                                   /*typename_keyword_p=*/false,
12068                                                   /*check_dependency_p=*/true,
12069                                                   /*type_p=*/false,
12070                                                   /*is_declaration=*/true);
12071   if (!qscope)
12072     qscope = global_namespace;
12073
12074   if (access_declaration_p && cp_parser_error_occurred (parser))
12075     /* Something has already gone wrong; there's no need to parse
12076        further.  Since an error has occurred, the return value of
12077        cp_parser_parse_definitely will be false, as required.  */
12078     return cp_parser_parse_definitely (parser);
12079
12080   token = cp_lexer_peek_token (parser->lexer);
12081   /* Parse the unqualified-id.  */
12082   identifier = cp_parser_unqualified_id (parser,
12083                                          /*template_keyword_p=*/false,
12084                                          /*check_dependency_p=*/true,
12085                                          /*declarator_p=*/true,
12086                                          /*optional_p=*/false);
12087
12088   if (access_declaration_p)
12089     {
12090       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12091         cp_parser_simulate_error (parser);
12092       if (!cp_parser_parse_definitely (parser))
12093         return false;
12094     }
12095
12096   /* The function we call to handle a using-declaration is different
12097      depending on what scope we are in.  */
12098   if (qscope == error_mark_node || identifier == error_mark_node)
12099     ;
12100   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12101            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12102     /* [namespace.udecl]
12103
12104        A using declaration shall not name a template-id.  */
12105     error ("%Ha template-id may not appear in a using-declaration",
12106             &token->location);
12107   else
12108     {
12109       if (at_class_scope_p ())
12110         {
12111           /* Create the USING_DECL.  */
12112           decl = do_class_using_decl (parser->scope, identifier);
12113
12114           if (check_for_bare_parameter_packs (decl))
12115             return false;
12116           else
12117             /* Add it to the list of members in this class.  */
12118             finish_member_declaration (decl);
12119         }
12120       else
12121         {
12122           decl = cp_parser_lookup_name_simple (parser,
12123                                                identifier,
12124                                                token->location);
12125           if (decl == error_mark_node)
12126             cp_parser_name_lookup_error (parser, identifier,
12127                                          decl, NULL,
12128                                          token->location);
12129           else if (check_for_bare_parameter_packs (decl))
12130             return false;
12131           else if (!at_namespace_scope_p ())
12132             do_local_using_decl (decl, qscope, identifier);
12133           else
12134             do_toplevel_using_decl (decl, qscope, identifier);
12135         }
12136     }
12137
12138   /* Look for the final `;'.  */
12139   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12140   
12141   return true;
12142 }
12143
12144 /* Parse a using-directive.
12145
12146    using-directive:
12147      using namespace :: [opt] nested-name-specifier [opt]
12148        namespace-name ;  */
12149
12150 static void
12151 cp_parser_using_directive (cp_parser* parser)
12152 {
12153   tree namespace_decl;
12154   tree attribs;
12155
12156   /* Look for the `using' keyword.  */
12157   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12158   /* And the `namespace' keyword.  */
12159   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12160   /* Look for the optional `::' operator.  */
12161   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12162   /* And the optional nested-name-specifier.  */
12163   cp_parser_nested_name_specifier_opt (parser,
12164                                        /*typename_keyword_p=*/false,
12165                                        /*check_dependency_p=*/true,
12166                                        /*type_p=*/false,
12167                                        /*is_declaration=*/true);
12168   /* Get the namespace being used.  */
12169   namespace_decl = cp_parser_namespace_name (parser);
12170   /* And any specified attributes.  */
12171   attribs = cp_parser_attributes_opt (parser);
12172   /* Update the symbol table.  */
12173   parse_using_directive (namespace_decl, attribs);
12174   /* Look for the final `;'.  */
12175   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12176 }
12177
12178 /* Parse an asm-definition.
12179
12180    asm-definition:
12181      asm ( string-literal ) ;
12182
12183    GNU Extension:
12184
12185    asm-definition:
12186      asm volatile [opt] ( string-literal ) ;
12187      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12188      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12189                           : asm-operand-list [opt] ) ;
12190      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12191                           : asm-operand-list [opt]
12192                           : asm-operand-list [opt] ) ;  */
12193
12194 static void
12195 cp_parser_asm_definition (cp_parser* parser)
12196 {
12197   tree string;
12198   tree outputs = NULL_TREE;
12199   tree inputs = NULL_TREE;
12200   tree clobbers = NULL_TREE;
12201   tree asm_stmt;
12202   bool volatile_p = false;
12203   bool extended_p = false;
12204   bool invalid_inputs_p = false;
12205   bool invalid_outputs_p = false;
12206
12207   /* Look for the `asm' keyword.  */
12208   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12209   /* See if the next token is `volatile'.  */
12210   if (cp_parser_allow_gnu_extensions_p (parser)
12211       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12212     {
12213       /* Remember that we saw the `volatile' keyword.  */
12214       volatile_p = true;
12215       /* Consume the token.  */
12216       cp_lexer_consume_token (parser->lexer);
12217     }
12218   /* Look for the opening `('.  */
12219   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12220     return;
12221   /* Look for the string.  */
12222   string = cp_parser_string_literal (parser, false, false);
12223   if (string == error_mark_node)
12224     {
12225       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12226                                              /*consume_paren=*/true);
12227       return;
12228     }
12229
12230   /* If we're allowing GNU extensions, check for the extended assembly
12231      syntax.  Unfortunately, the `:' tokens need not be separated by
12232      a space in C, and so, for compatibility, we tolerate that here
12233      too.  Doing that means that we have to treat the `::' operator as
12234      two `:' tokens.  */
12235   if (cp_parser_allow_gnu_extensions_p (parser)
12236       && parser->in_function_body
12237       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12238           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12239     {
12240       bool inputs_p = false;
12241       bool clobbers_p = false;
12242
12243       /* The extended syntax was used.  */
12244       extended_p = true;
12245
12246       /* Look for outputs.  */
12247       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12248         {
12249           /* Consume the `:'.  */
12250           cp_lexer_consume_token (parser->lexer);
12251           /* Parse the output-operands.  */
12252           if (cp_lexer_next_token_is_not (parser->lexer,
12253                                           CPP_COLON)
12254               && cp_lexer_next_token_is_not (parser->lexer,
12255                                              CPP_SCOPE)
12256               && cp_lexer_next_token_is_not (parser->lexer,
12257                                              CPP_CLOSE_PAREN))
12258             outputs = cp_parser_asm_operand_list (parser);
12259
12260             if (outputs == error_mark_node)
12261               invalid_outputs_p = true;
12262         }
12263       /* If the next token is `::', there are no outputs, and the
12264          next token is the beginning of the inputs.  */
12265       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12266         /* The inputs are coming next.  */
12267         inputs_p = true;
12268
12269       /* Look for inputs.  */
12270       if (inputs_p
12271           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12272         {
12273           /* Consume the `:' or `::'.  */
12274           cp_lexer_consume_token (parser->lexer);
12275           /* Parse the output-operands.  */
12276           if (cp_lexer_next_token_is_not (parser->lexer,
12277                                           CPP_COLON)
12278               && cp_lexer_next_token_is_not (parser->lexer,
12279                                              CPP_CLOSE_PAREN))
12280             inputs = cp_parser_asm_operand_list (parser);
12281
12282             if (inputs == error_mark_node)
12283               invalid_inputs_p = true;
12284         }
12285       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12286         /* The clobbers are coming next.  */
12287         clobbers_p = true;
12288
12289       /* Look for clobbers.  */
12290       if (clobbers_p
12291           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12292         {
12293           /* Consume the `:' or `::'.  */
12294           cp_lexer_consume_token (parser->lexer);
12295           /* Parse the clobbers.  */
12296           if (cp_lexer_next_token_is_not (parser->lexer,
12297                                           CPP_CLOSE_PAREN))
12298             clobbers = cp_parser_asm_clobber_list (parser);
12299         }
12300     }
12301   /* Look for the closing `)'.  */
12302   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12303     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12304                                            /*consume_paren=*/true);
12305   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12306
12307   if (!invalid_inputs_p && !invalid_outputs_p)
12308     {
12309       /* Create the ASM_EXPR.  */
12310       if (parser->in_function_body)
12311         {
12312           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12313                                       inputs, clobbers);
12314           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12315           if (!extended_p)
12316             {
12317               tree temp = asm_stmt;
12318               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12319                 temp = TREE_OPERAND (temp, 0);
12320
12321               ASM_INPUT_P (temp) = 1;
12322             }
12323         }
12324       else
12325         cgraph_add_asm_node (string);
12326     }
12327 }
12328
12329 /* Declarators [gram.dcl.decl] */
12330
12331 /* Parse an init-declarator.
12332
12333    init-declarator:
12334      declarator initializer [opt]
12335
12336    GNU Extension:
12337
12338    init-declarator:
12339      declarator asm-specification [opt] attributes [opt] initializer [opt]
12340
12341    function-definition:
12342      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12343        function-body
12344      decl-specifier-seq [opt] declarator function-try-block
12345
12346    GNU Extension:
12347
12348    function-definition:
12349      __extension__ function-definition
12350
12351    The DECL_SPECIFIERS apply to this declarator.  Returns a
12352    representation of the entity declared.  If MEMBER_P is TRUE, then
12353    this declarator appears in a class scope.  The new DECL created by
12354    this declarator is returned.
12355
12356    The CHECKS are access checks that should be performed once we know
12357    what entity is being declared (and, therefore, what classes have
12358    befriended it).
12359
12360    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12361    for a function-definition here as well.  If the declarator is a
12362    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12363    be TRUE upon return.  By that point, the function-definition will
12364    have been completely parsed.
12365
12366    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12367    is FALSE.  */
12368
12369 static tree
12370 cp_parser_init_declarator (cp_parser* parser,
12371                            cp_decl_specifier_seq *decl_specifiers,
12372                            VEC (deferred_access_check,gc)* checks,
12373                            bool function_definition_allowed_p,
12374                            bool member_p,
12375                            int declares_class_or_enum,
12376                            bool* function_definition_p)
12377 {
12378   cp_token *token = NULL, *asm_spec_start_token = NULL,
12379            *attributes_start_token = NULL;
12380   cp_declarator *declarator;
12381   tree prefix_attributes;
12382   tree attributes;
12383   tree asm_specification;
12384   tree initializer;
12385   tree decl = NULL_TREE;
12386   tree scope;
12387   bool is_initialized;
12388   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12389      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12390      "(...)".  */
12391   enum cpp_ttype initialization_kind;
12392   bool is_direct_init = false;
12393   bool is_non_constant_init;
12394   int ctor_dtor_or_conv_p;
12395   bool friend_p;
12396   tree pushed_scope = NULL;
12397
12398   /* Gather the attributes that were provided with the
12399      decl-specifiers.  */
12400   prefix_attributes = decl_specifiers->attributes;
12401
12402   /* Assume that this is not the declarator for a function
12403      definition.  */
12404   if (function_definition_p)
12405     *function_definition_p = false;
12406
12407   /* Defer access checks while parsing the declarator; we cannot know
12408      what names are accessible until we know what is being
12409      declared.  */
12410   resume_deferring_access_checks ();
12411
12412   /* Parse the declarator.  */
12413   token = cp_lexer_peek_token (parser->lexer);
12414   declarator
12415     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12416                             &ctor_dtor_or_conv_p,
12417                             /*parenthesized_p=*/NULL,
12418                             /*member_p=*/false);
12419   /* Gather up the deferred checks.  */
12420   stop_deferring_access_checks ();
12421
12422   /* If the DECLARATOR was erroneous, there's no need to go
12423      further.  */
12424   if (declarator == cp_error_declarator)
12425     return error_mark_node;
12426
12427   /* Check that the number of template-parameter-lists is OK.  */
12428   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12429                                                        token->location))
12430     return error_mark_node;
12431
12432   if (declares_class_or_enum & 2)
12433     cp_parser_check_for_definition_in_return_type (declarator,
12434                                                    decl_specifiers->type,
12435                                                    decl_specifiers->type_location);
12436
12437   /* Figure out what scope the entity declared by the DECLARATOR is
12438      located in.  `grokdeclarator' sometimes changes the scope, so
12439      we compute it now.  */
12440   scope = get_scope_of_declarator (declarator);
12441
12442   /* If we're allowing GNU extensions, look for an asm-specification
12443      and attributes.  */
12444   if (cp_parser_allow_gnu_extensions_p (parser))
12445     {
12446       /* Look for an asm-specification.  */
12447       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12448       asm_specification = cp_parser_asm_specification_opt (parser);
12449       /* And attributes.  */
12450       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12451       attributes = cp_parser_attributes_opt (parser);
12452     }
12453   else
12454     {
12455       asm_specification = NULL_TREE;
12456       attributes = NULL_TREE;
12457     }
12458
12459   /* Peek at the next token.  */
12460   token = cp_lexer_peek_token (parser->lexer);
12461   /* Check to see if the token indicates the start of a
12462      function-definition.  */
12463   if (function_declarator_p (declarator)
12464       && cp_parser_token_starts_function_definition_p (token))
12465     {
12466       if (!function_definition_allowed_p)
12467         {
12468           /* If a function-definition should not appear here, issue an
12469              error message.  */
12470           cp_parser_error (parser,
12471                            "a function-definition is not allowed here");
12472           return error_mark_node;
12473         }
12474       else
12475         {
12476           /* Neither attributes nor an asm-specification are allowed
12477              on a function-definition.  */
12478           if (asm_specification)
12479             error ("%Han asm-specification is not allowed "
12480                    "on a function-definition",
12481                    &asm_spec_start_token->location);
12482           if (attributes)
12483             error ("%Hattributes are not allowed on a function-definition",
12484                    &attributes_start_token->location);
12485           /* This is a function-definition.  */
12486           *function_definition_p = true;
12487
12488           /* Parse the function definition.  */
12489           if (member_p)
12490             decl = cp_parser_save_member_function_body (parser,
12491                                                         decl_specifiers,
12492                                                         declarator,
12493                                                         prefix_attributes);
12494           else
12495             decl
12496               = (cp_parser_function_definition_from_specifiers_and_declarator
12497                  (parser, decl_specifiers, prefix_attributes, declarator));
12498
12499           return decl;
12500         }
12501     }
12502
12503   /* [dcl.dcl]
12504
12505      Only in function declarations for constructors, destructors, and
12506      type conversions can the decl-specifier-seq be omitted.
12507
12508      We explicitly postpone this check past the point where we handle
12509      function-definitions because we tolerate function-definitions
12510      that are missing their return types in some modes.  */
12511   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12512     {
12513       cp_parser_error (parser,
12514                        "expected constructor, destructor, or type conversion");
12515       return error_mark_node;
12516     }
12517
12518   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12519   if (token->type == CPP_EQ
12520       || token->type == CPP_OPEN_PAREN
12521       || token->type == CPP_OPEN_BRACE)
12522     {
12523       is_initialized = true;
12524       initialization_kind = token->type;
12525     }
12526   else
12527     {
12528       /* If the init-declarator isn't initialized and isn't followed by a
12529          `,' or `;', it's not a valid init-declarator.  */
12530       if (token->type != CPP_COMMA
12531           && token->type != CPP_SEMICOLON)
12532         {
12533           cp_parser_error (parser, "expected initializer");
12534           return error_mark_node;
12535         }
12536       is_initialized = false;
12537       initialization_kind = CPP_EOF;
12538     }
12539
12540   /* Because start_decl has side-effects, we should only call it if we
12541      know we're going ahead.  By this point, we know that we cannot
12542      possibly be looking at any other construct.  */
12543   cp_parser_commit_to_tentative_parse (parser);
12544
12545   /* If the decl specifiers were bad, issue an error now that we're
12546      sure this was intended to be a declarator.  Then continue
12547      declaring the variable(s), as int, to try to cut down on further
12548      errors.  */
12549   if (decl_specifiers->any_specifiers_p
12550       && decl_specifiers->type == error_mark_node)
12551     {
12552       cp_parser_error (parser, "invalid type in declaration");
12553       decl_specifiers->type = integer_type_node;
12554     }
12555
12556   /* Check to see whether or not this declaration is a friend.  */
12557   friend_p = cp_parser_friend_p (decl_specifiers);
12558
12559   /* Enter the newly declared entry in the symbol table.  If we're
12560      processing a declaration in a class-specifier, we wait until
12561      after processing the initializer.  */
12562   if (!member_p)
12563     {
12564       if (parser->in_unbraced_linkage_specification_p)
12565         decl_specifiers->storage_class = sc_extern;
12566       decl = start_decl (declarator, decl_specifiers,
12567                          is_initialized, attributes, prefix_attributes,
12568                          &pushed_scope);
12569     }
12570   else if (scope)
12571     /* Enter the SCOPE.  That way unqualified names appearing in the
12572        initializer will be looked up in SCOPE.  */
12573     pushed_scope = push_scope (scope);
12574
12575   /* Perform deferred access control checks, now that we know in which
12576      SCOPE the declared entity resides.  */
12577   if (!member_p && decl)
12578     {
12579       tree saved_current_function_decl = NULL_TREE;
12580
12581       /* If the entity being declared is a function, pretend that we
12582          are in its scope.  If it is a `friend', it may have access to
12583          things that would not otherwise be accessible.  */
12584       if (TREE_CODE (decl) == FUNCTION_DECL)
12585         {
12586           saved_current_function_decl = current_function_decl;
12587           current_function_decl = decl;
12588         }
12589
12590       /* Perform access checks for template parameters.  */
12591       cp_parser_perform_template_parameter_access_checks (checks);
12592
12593       /* Perform the access control checks for the declarator and the
12594          the decl-specifiers.  */
12595       perform_deferred_access_checks ();
12596
12597       /* Restore the saved value.  */
12598       if (TREE_CODE (decl) == FUNCTION_DECL)
12599         current_function_decl = saved_current_function_decl;
12600     }
12601
12602   /* Parse the initializer.  */
12603   initializer = NULL_TREE;
12604   is_direct_init = false;
12605   is_non_constant_init = true;
12606   if (is_initialized)
12607     {
12608       if (function_declarator_p (declarator))
12609         {
12610           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12611            if (initialization_kind == CPP_EQ)
12612              initializer = cp_parser_pure_specifier (parser);
12613            else
12614              {
12615                /* If the declaration was erroneous, we don't really
12616                   know what the user intended, so just silently
12617                   consume the initializer.  */
12618                if (decl != error_mark_node)
12619                  error ("%Hinitializer provided for function",
12620                         &initializer_start_token->location);
12621                cp_parser_skip_to_closing_parenthesis (parser,
12622                                                       /*recovering=*/true,
12623                                                       /*or_comma=*/false,
12624                                                       /*consume_paren=*/true);
12625              }
12626         }
12627       else
12628         initializer = cp_parser_initializer (parser,
12629                                              &is_direct_init,
12630                                              &is_non_constant_init);
12631     }
12632
12633   /* The old parser allows attributes to appear after a parenthesized
12634      initializer.  Mark Mitchell proposed removing this functionality
12635      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12636      attributes -- but ignores them.  */
12637   if (cp_parser_allow_gnu_extensions_p (parser)
12638       && initialization_kind == CPP_OPEN_PAREN)
12639     if (cp_parser_attributes_opt (parser))
12640       warning (OPT_Wattributes,
12641                "attributes after parenthesized initializer ignored");
12642
12643   /* For an in-class declaration, use `grokfield' to create the
12644      declaration.  */
12645   if (member_p)
12646     {
12647       if (pushed_scope)
12648         {
12649           pop_scope (pushed_scope);
12650           pushed_scope = false;
12651         }
12652       decl = grokfield (declarator, decl_specifiers,
12653                         initializer, !is_non_constant_init,
12654                         /*asmspec=*/NULL_TREE,
12655                         prefix_attributes);
12656       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12657         cp_parser_save_default_args (parser, decl);
12658     }
12659
12660   /* Finish processing the declaration.  But, skip friend
12661      declarations.  */
12662   if (!friend_p && decl && decl != error_mark_node)
12663     {
12664       cp_finish_decl (decl,
12665                       initializer, !is_non_constant_init,
12666                       asm_specification,
12667                       /* If the initializer is in parentheses, then this is
12668                          a direct-initialization, which means that an
12669                          `explicit' constructor is OK.  Otherwise, an
12670                          `explicit' constructor cannot be used.  */
12671                       ((is_direct_init || !is_initialized)
12672                        ? 0 : LOOKUP_ONLYCONVERTING));
12673     }
12674   else if ((cxx_dialect != cxx98) && friend_p
12675            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12676     /* Core issue #226 (C++0x only): A default template-argument
12677        shall not be specified in a friend class template
12678        declaration. */
12679     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12680                              /*is_partial=*/0, /*is_friend_decl=*/1);
12681
12682   if (!friend_p && pushed_scope)
12683     pop_scope (pushed_scope);
12684
12685   return decl;
12686 }
12687
12688 /* Parse a declarator.
12689
12690    declarator:
12691      direct-declarator
12692      ptr-operator declarator
12693
12694    abstract-declarator:
12695      ptr-operator abstract-declarator [opt]
12696      direct-abstract-declarator
12697
12698    GNU Extensions:
12699
12700    declarator:
12701      attributes [opt] direct-declarator
12702      attributes [opt] ptr-operator declarator
12703
12704    abstract-declarator:
12705      attributes [opt] ptr-operator abstract-declarator [opt]
12706      attributes [opt] direct-abstract-declarator
12707
12708    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12709    detect constructor, destructor or conversion operators. It is set
12710    to -1 if the declarator is a name, and +1 if it is a
12711    function. Otherwise it is set to zero. Usually you just want to
12712    test for >0, but internally the negative value is used.
12713
12714    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12715    a decl-specifier-seq unless it declares a constructor, destructor,
12716    or conversion.  It might seem that we could check this condition in
12717    semantic analysis, rather than parsing, but that makes it difficult
12718    to handle something like `f()'.  We want to notice that there are
12719    no decl-specifiers, and therefore realize that this is an
12720    expression, not a declaration.)
12721
12722    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12723    the declarator is a direct-declarator of the form "(...)".
12724
12725    MEMBER_P is true iff this declarator is a member-declarator.  */
12726
12727 static cp_declarator *
12728 cp_parser_declarator (cp_parser* parser,
12729                       cp_parser_declarator_kind dcl_kind,
12730                       int* ctor_dtor_or_conv_p,
12731                       bool* parenthesized_p,
12732                       bool member_p)
12733 {
12734   cp_token *token;
12735   cp_declarator *declarator;
12736   enum tree_code code;
12737   cp_cv_quals cv_quals;
12738   tree class_type;
12739   tree attributes = NULL_TREE;
12740
12741   /* Assume this is not a constructor, destructor, or type-conversion
12742      operator.  */
12743   if (ctor_dtor_or_conv_p)
12744     *ctor_dtor_or_conv_p = 0;
12745
12746   if (cp_parser_allow_gnu_extensions_p (parser))
12747     attributes = cp_parser_attributes_opt (parser);
12748
12749   /* Peek at the next token.  */
12750   token = cp_lexer_peek_token (parser->lexer);
12751
12752   /* Check for the ptr-operator production.  */
12753   cp_parser_parse_tentatively (parser);
12754   /* Parse the ptr-operator.  */
12755   code = cp_parser_ptr_operator (parser,
12756                                  &class_type,
12757                                  &cv_quals);
12758   /* If that worked, then we have a ptr-operator.  */
12759   if (cp_parser_parse_definitely (parser))
12760     {
12761       /* If a ptr-operator was found, then this declarator was not
12762          parenthesized.  */
12763       if (parenthesized_p)
12764         *parenthesized_p = true;
12765       /* The dependent declarator is optional if we are parsing an
12766          abstract-declarator.  */
12767       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12768         cp_parser_parse_tentatively (parser);
12769
12770       /* Parse the dependent declarator.  */
12771       declarator = cp_parser_declarator (parser, dcl_kind,
12772                                          /*ctor_dtor_or_conv_p=*/NULL,
12773                                          /*parenthesized_p=*/NULL,
12774                                          /*member_p=*/false);
12775
12776       /* If we are parsing an abstract-declarator, we must handle the
12777          case where the dependent declarator is absent.  */
12778       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12779           && !cp_parser_parse_definitely (parser))
12780         declarator = NULL;
12781
12782       declarator = cp_parser_make_indirect_declarator
12783         (code, class_type, cv_quals, declarator);
12784     }
12785   /* Everything else is a direct-declarator.  */
12786   else
12787     {
12788       if (parenthesized_p)
12789         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12790                                                    CPP_OPEN_PAREN);
12791       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12792                                                 ctor_dtor_or_conv_p,
12793                                                 member_p);
12794     }
12795
12796   if (attributes && declarator && declarator != cp_error_declarator)
12797     declarator->attributes = attributes;
12798
12799   return declarator;
12800 }
12801
12802 /* Parse a direct-declarator or direct-abstract-declarator.
12803
12804    direct-declarator:
12805      declarator-id
12806      direct-declarator ( parameter-declaration-clause )
12807        cv-qualifier-seq [opt]
12808        exception-specification [opt]
12809      direct-declarator [ constant-expression [opt] ]
12810      ( declarator )
12811
12812    direct-abstract-declarator:
12813      direct-abstract-declarator [opt]
12814        ( parameter-declaration-clause )
12815        cv-qualifier-seq [opt]
12816        exception-specification [opt]
12817      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12818      ( abstract-declarator )
12819
12820    Returns a representation of the declarator.  DCL_KIND is
12821    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12822    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12823    we are parsing a direct-declarator.  It is
12824    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12825    of ambiguity we prefer an abstract declarator, as per
12826    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12827    cp_parser_declarator.  */
12828
12829 static cp_declarator *
12830 cp_parser_direct_declarator (cp_parser* parser,
12831                              cp_parser_declarator_kind dcl_kind,
12832                              int* ctor_dtor_or_conv_p,
12833                              bool member_p)
12834 {
12835   cp_token *token;
12836   cp_declarator *declarator = NULL;
12837   tree scope = NULL_TREE;
12838   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12839   bool saved_in_declarator_p = parser->in_declarator_p;
12840   bool first = true;
12841   tree pushed_scope = NULL_TREE;
12842
12843   while (true)
12844     {
12845       /* Peek at the next token.  */
12846       token = cp_lexer_peek_token (parser->lexer);
12847       if (token->type == CPP_OPEN_PAREN)
12848         {
12849           /* This is either a parameter-declaration-clause, or a
12850              parenthesized declarator. When we know we are parsing a
12851              named declarator, it must be a parenthesized declarator
12852              if FIRST is true. For instance, `(int)' is a
12853              parameter-declaration-clause, with an omitted
12854              direct-abstract-declarator. But `((*))', is a
12855              parenthesized abstract declarator. Finally, when T is a
12856              template parameter `(T)' is a
12857              parameter-declaration-clause, and not a parenthesized
12858              named declarator.
12859
12860              We first try and parse a parameter-declaration-clause,
12861              and then try a nested declarator (if FIRST is true).
12862
12863              It is not an error for it not to be a
12864              parameter-declaration-clause, even when FIRST is
12865              false. Consider,
12866
12867                int i (int);
12868                int i (3);
12869
12870              The first is the declaration of a function while the
12871              second is a the definition of a variable, including its
12872              initializer.
12873
12874              Having seen only the parenthesis, we cannot know which of
12875              these two alternatives should be selected.  Even more
12876              complex are examples like:
12877
12878                int i (int (a));
12879                int i (int (3));
12880
12881              The former is a function-declaration; the latter is a
12882              variable initialization.
12883
12884              Thus again, we try a parameter-declaration-clause, and if
12885              that fails, we back out and return.  */
12886
12887           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12888             {
12889               cp_parameter_declarator *params;
12890               unsigned saved_num_template_parameter_lists;
12891
12892               /* In a member-declarator, the only valid interpretation
12893                  of a parenthesis is the start of a
12894                  parameter-declaration-clause.  (It is invalid to
12895                  initialize a static data member with a parenthesized
12896                  initializer; only the "=" form of initialization is
12897                  permitted.)  */
12898               if (!member_p)
12899                 cp_parser_parse_tentatively (parser);
12900
12901               /* Consume the `('.  */
12902               cp_lexer_consume_token (parser->lexer);
12903               if (first)
12904                 {
12905                   /* If this is going to be an abstract declarator, we're
12906                      in a declarator and we can't have default args.  */
12907                   parser->default_arg_ok_p = false;
12908                   parser->in_declarator_p = true;
12909                 }
12910
12911               /* Inside the function parameter list, surrounding
12912                  template-parameter-lists do not apply.  */
12913               saved_num_template_parameter_lists
12914                 = parser->num_template_parameter_lists;
12915               parser->num_template_parameter_lists = 0;
12916
12917               /* Parse the parameter-declaration-clause.  */
12918               params = cp_parser_parameter_declaration_clause (parser);
12919
12920               parser->num_template_parameter_lists
12921                 = saved_num_template_parameter_lists;
12922
12923               /* If all went well, parse the cv-qualifier-seq and the
12924                  exception-specification.  */
12925               if (member_p || cp_parser_parse_definitely (parser))
12926                 {
12927                   cp_cv_quals cv_quals;
12928                   tree exception_specification;
12929
12930                   if (ctor_dtor_or_conv_p)
12931                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12932                   first = false;
12933                   /* Consume the `)'.  */
12934                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
12935
12936                   /* Parse the cv-qualifier-seq.  */
12937                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12938                   /* And the exception-specification.  */
12939                   exception_specification
12940                     = cp_parser_exception_specification_opt (parser);
12941
12942                   /* Create the function-declarator.  */
12943                   declarator = make_call_declarator (declarator,
12944                                                      params,
12945                                                      cv_quals,
12946                                                      exception_specification);
12947                   /* Any subsequent parameter lists are to do with
12948                      return type, so are not those of the declared
12949                      function.  */
12950                   parser->default_arg_ok_p = false;
12951
12952                   /* Repeat the main loop.  */
12953                   continue;
12954                 }
12955             }
12956
12957           /* If this is the first, we can try a parenthesized
12958              declarator.  */
12959           if (first)
12960             {
12961               bool saved_in_type_id_in_expr_p;
12962
12963               parser->default_arg_ok_p = saved_default_arg_ok_p;
12964               parser->in_declarator_p = saved_in_declarator_p;
12965
12966               /* Consume the `('.  */
12967               cp_lexer_consume_token (parser->lexer);
12968               /* Parse the nested declarator.  */
12969               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12970               parser->in_type_id_in_expr_p = true;
12971               declarator
12972                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12973                                         /*parenthesized_p=*/NULL,
12974                                         member_p);
12975               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12976               first = false;
12977               /* Expect a `)'.  */
12978               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12979                 declarator = cp_error_declarator;
12980               if (declarator == cp_error_declarator)
12981                 break;
12982
12983               goto handle_declarator;
12984             }
12985           /* Otherwise, we must be done.  */
12986           else
12987             break;
12988         }
12989       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12990                && token->type == CPP_OPEN_SQUARE)
12991         {
12992           /* Parse an array-declarator.  */
12993           tree bounds;
12994
12995           if (ctor_dtor_or_conv_p)
12996             *ctor_dtor_or_conv_p = 0;
12997
12998           first = false;
12999           parser->default_arg_ok_p = false;
13000           parser->in_declarator_p = true;
13001           /* Consume the `['.  */
13002           cp_lexer_consume_token (parser->lexer);
13003           /* Peek at the next token.  */
13004           token = cp_lexer_peek_token (parser->lexer);
13005           /* If the next token is `]', then there is no
13006              constant-expression.  */
13007           if (token->type != CPP_CLOSE_SQUARE)
13008             {
13009               bool non_constant_p;
13010
13011               bounds
13012                 = cp_parser_constant_expression (parser,
13013                                                  /*allow_non_constant=*/true,
13014                                                  &non_constant_p);
13015               if (!non_constant_p)
13016                 bounds = fold_non_dependent_expr (bounds);
13017               /* Normally, the array bound must be an integral constant
13018                  expression.  However, as an extension, we allow VLAs
13019                  in function scopes.  */
13020               else if (!parser->in_function_body)
13021                 {
13022                   error ("%Harray bound is not an integer constant",
13023                          &token->location);
13024                   bounds = error_mark_node;
13025                 }
13026             }
13027           else
13028             bounds = NULL_TREE;
13029           /* Look for the closing `]'.  */
13030           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13031             {
13032               declarator = cp_error_declarator;
13033               break;
13034             }
13035
13036           declarator = make_array_declarator (declarator, bounds);
13037         }
13038       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13039         {
13040           tree qualifying_scope;
13041           tree unqualified_name;
13042           special_function_kind sfk;
13043           bool abstract_ok;
13044           bool pack_expansion_p = false;
13045           cp_token *declarator_id_start_token;
13046
13047           /* Parse a declarator-id */
13048           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13049           if (abstract_ok)
13050             {
13051               cp_parser_parse_tentatively (parser);
13052
13053               /* If we see an ellipsis, we should be looking at a
13054                  parameter pack. */
13055               if (token->type == CPP_ELLIPSIS)
13056                 {
13057                   /* Consume the `...' */
13058                   cp_lexer_consume_token (parser->lexer);
13059
13060                   pack_expansion_p = true;
13061                 }
13062             }
13063
13064           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13065           unqualified_name
13066             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13067           qualifying_scope = parser->scope;
13068           if (abstract_ok)
13069             {
13070               bool okay = false;
13071
13072               if (!unqualified_name && pack_expansion_p)
13073                 {
13074                   /* Check whether an error occurred. */
13075                   okay = !cp_parser_error_occurred (parser);
13076
13077                   /* We already consumed the ellipsis to mark a
13078                      parameter pack, but we have no way to report it,
13079                      so abort the tentative parse. We will be exiting
13080                      immediately anyway. */
13081                   cp_parser_abort_tentative_parse (parser);
13082                 }
13083               else
13084                 okay = cp_parser_parse_definitely (parser);
13085
13086               if (!okay)
13087                 unqualified_name = error_mark_node;
13088               else if (unqualified_name
13089                        && (qualifying_scope
13090                            || (TREE_CODE (unqualified_name)
13091                                != IDENTIFIER_NODE)))
13092                 {
13093                   cp_parser_error (parser, "expected unqualified-id");
13094                   unqualified_name = error_mark_node;
13095                 }
13096             }
13097
13098           if (!unqualified_name)
13099             return NULL;
13100           if (unqualified_name == error_mark_node)
13101             {
13102               declarator = cp_error_declarator;
13103               pack_expansion_p = false;
13104               declarator->parameter_pack_p = false;
13105               break;
13106             }
13107
13108           if (qualifying_scope && at_namespace_scope_p ()
13109               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13110             {
13111               /* In the declaration of a member of a template class
13112                  outside of the class itself, the SCOPE will sometimes
13113                  be a TYPENAME_TYPE.  For example, given:
13114
13115                  template <typename T>
13116                  int S<T>::R::i = 3;
13117
13118                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13119                  this context, we must resolve S<T>::R to an ordinary
13120                  type, rather than a typename type.
13121
13122                  The reason we normally avoid resolving TYPENAME_TYPEs
13123                  is that a specialization of `S' might render
13124                  `S<T>::R' not a type.  However, if `S' is
13125                  specialized, then this `i' will not be used, so there
13126                  is no harm in resolving the types here.  */
13127               tree type;
13128
13129               /* Resolve the TYPENAME_TYPE.  */
13130               type = resolve_typename_type (qualifying_scope,
13131                                             /*only_current_p=*/false);
13132               /* If that failed, the declarator is invalid.  */
13133               if (TREE_CODE (type) == TYPENAME_TYPE)
13134                 error ("%H%<%T::%E%> is not a type",
13135                        &declarator_id_start_token->location,
13136                        TYPE_CONTEXT (qualifying_scope),
13137                        TYPE_IDENTIFIER (qualifying_scope));
13138               qualifying_scope = type;
13139             }
13140
13141           sfk = sfk_none;
13142
13143           if (unqualified_name)
13144             {
13145               tree class_type;
13146
13147               if (qualifying_scope
13148                   && CLASS_TYPE_P (qualifying_scope))
13149                 class_type = qualifying_scope;
13150               else
13151                 class_type = current_class_type;
13152
13153               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13154                 {
13155                   tree name_type = TREE_TYPE (unqualified_name);
13156                   if (class_type && same_type_p (name_type, class_type))
13157                     {
13158                       if (qualifying_scope
13159                           && CLASSTYPE_USE_TEMPLATE (name_type))
13160                         {
13161                           error ("%Hinvalid use of constructor as a template",
13162                                  &declarator_id_start_token->location);
13163                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
13164                                   "name the constructor in a qualified name",
13165                                   class_type,
13166                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13167                                   class_type, name_type);
13168                           declarator = cp_error_declarator;
13169                           break;
13170                         }
13171                       else
13172                         unqualified_name = constructor_name (class_type);
13173                     }
13174                   else
13175                     {
13176                       /* We do not attempt to print the declarator
13177                          here because we do not have enough
13178                          information about its original syntactic
13179                          form.  */
13180                       cp_parser_error (parser, "invalid declarator");
13181                       declarator = cp_error_declarator;
13182                       break;
13183                     }
13184                 }
13185
13186               if (class_type)
13187                 {
13188                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13189                     sfk = sfk_destructor;
13190                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13191                     sfk = sfk_conversion;
13192                   else if (/* There's no way to declare a constructor
13193                               for an anonymous type, even if the type
13194                               got a name for linkage purposes.  */
13195                            !TYPE_WAS_ANONYMOUS (class_type)
13196                            && constructor_name_p (unqualified_name,
13197                                                   class_type))
13198                     {
13199                       unqualified_name = constructor_name (class_type);
13200                       sfk = sfk_constructor;
13201                     }
13202
13203                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13204                     *ctor_dtor_or_conv_p = -1;
13205                 }
13206             }
13207           declarator = make_id_declarator (qualifying_scope,
13208                                            unqualified_name,
13209                                            sfk);
13210           declarator->id_loc = token->location;
13211           declarator->parameter_pack_p = pack_expansion_p;
13212
13213           if (pack_expansion_p)
13214             maybe_warn_variadic_templates ();
13215
13216         handle_declarator:;
13217           scope = get_scope_of_declarator (declarator);
13218           if (scope)
13219             /* Any names that appear after the declarator-id for a
13220                member are looked up in the containing scope.  */
13221             pushed_scope = push_scope (scope);
13222           parser->in_declarator_p = true;
13223           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13224               || (declarator && declarator->kind == cdk_id))
13225             /* Default args are only allowed on function
13226                declarations.  */
13227             parser->default_arg_ok_p = saved_default_arg_ok_p;
13228           else
13229             parser->default_arg_ok_p = false;
13230
13231           first = false;
13232         }
13233       /* We're done.  */
13234       else
13235         break;
13236     }
13237
13238   /* For an abstract declarator, we might wind up with nothing at this
13239      point.  That's an error; the declarator is not optional.  */
13240   if (!declarator)
13241     cp_parser_error (parser, "expected declarator");
13242
13243   /* If we entered a scope, we must exit it now.  */
13244   if (pushed_scope)
13245     pop_scope (pushed_scope);
13246
13247   parser->default_arg_ok_p = saved_default_arg_ok_p;
13248   parser->in_declarator_p = saved_in_declarator_p;
13249
13250   return declarator;
13251 }
13252
13253 /* Parse a ptr-operator.
13254
13255    ptr-operator:
13256      * cv-qualifier-seq [opt]
13257      &
13258      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13259
13260    GNU Extension:
13261
13262    ptr-operator:
13263      & cv-qualifier-seq [opt]
13264
13265    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13266    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13267    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13268    filled in with the TYPE containing the member.  *CV_QUALS is
13269    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13270    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13271    Note that the tree codes returned by this function have nothing
13272    to do with the types of trees that will be eventually be created
13273    to represent the pointer or reference type being parsed. They are
13274    just constants with suggestive names. */
13275 static enum tree_code
13276 cp_parser_ptr_operator (cp_parser* parser,
13277                         tree* type,
13278                         cp_cv_quals *cv_quals)
13279 {
13280   enum tree_code code = ERROR_MARK;
13281   cp_token *token;
13282
13283   /* Assume that it's not a pointer-to-member.  */
13284   *type = NULL_TREE;
13285   /* And that there are no cv-qualifiers.  */
13286   *cv_quals = TYPE_UNQUALIFIED;
13287
13288   /* Peek at the next token.  */
13289   token = cp_lexer_peek_token (parser->lexer);
13290
13291   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13292   if (token->type == CPP_MULT)
13293     code = INDIRECT_REF;
13294   else if (token->type == CPP_AND)
13295     code = ADDR_EXPR;
13296   else if ((cxx_dialect != cxx98) &&
13297            token->type == CPP_AND_AND) /* C++0x only */
13298     code = NON_LVALUE_EXPR;
13299
13300   if (code != ERROR_MARK)
13301     {
13302       /* Consume the `*', `&' or `&&'.  */
13303       cp_lexer_consume_token (parser->lexer);
13304
13305       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13306          `&', if we are allowing GNU extensions.  (The only qualifier
13307          that can legally appear after `&' is `restrict', but that is
13308          enforced during semantic analysis.  */
13309       if (code == INDIRECT_REF
13310           || cp_parser_allow_gnu_extensions_p (parser))
13311         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13312     }
13313   else
13314     {
13315       /* Try the pointer-to-member case.  */
13316       cp_parser_parse_tentatively (parser);
13317       /* Look for the optional `::' operator.  */
13318       cp_parser_global_scope_opt (parser,
13319                                   /*current_scope_valid_p=*/false);
13320       /* Look for the nested-name specifier.  */
13321       token = cp_lexer_peek_token (parser->lexer);
13322       cp_parser_nested_name_specifier (parser,
13323                                        /*typename_keyword_p=*/false,
13324                                        /*check_dependency_p=*/true,
13325                                        /*type_p=*/false,
13326                                        /*is_declaration=*/false);
13327       /* If we found it, and the next token is a `*', then we are
13328          indeed looking at a pointer-to-member operator.  */
13329       if (!cp_parser_error_occurred (parser)
13330           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13331         {
13332           /* Indicate that the `*' operator was used.  */
13333           code = INDIRECT_REF;
13334
13335           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13336             error ("%H%qD is a namespace", &token->location, parser->scope);
13337           else
13338             {
13339               /* The type of which the member is a member is given by the
13340                  current SCOPE.  */
13341               *type = parser->scope;
13342               /* The next name will not be qualified.  */
13343               parser->scope = NULL_TREE;
13344               parser->qualifying_scope = NULL_TREE;
13345               parser->object_scope = NULL_TREE;
13346               /* Look for the optional cv-qualifier-seq.  */
13347               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13348             }
13349         }
13350       /* If that didn't work we don't have a ptr-operator.  */
13351       if (!cp_parser_parse_definitely (parser))
13352         cp_parser_error (parser, "expected ptr-operator");
13353     }
13354
13355   return code;
13356 }
13357
13358 /* Parse an (optional) cv-qualifier-seq.
13359
13360    cv-qualifier-seq:
13361      cv-qualifier cv-qualifier-seq [opt]
13362
13363    cv-qualifier:
13364      const
13365      volatile
13366
13367    GNU Extension:
13368
13369    cv-qualifier:
13370      __restrict__
13371
13372    Returns a bitmask representing the cv-qualifiers.  */
13373
13374 static cp_cv_quals
13375 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13376 {
13377   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13378
13379   while (true)
13380     {
13381       cp_token *token;
13382       cp_cv_quals cv_qualifier;
13383
13384       /* Peek at the next token.  */
13385       token = cp_lexer_peek_token (parser->lexer);
13386       /* See if it's a cv-qualifier.  */
13387       switch (token->keyword)
13388         {
13389         case RID_CONST:
13390           cv_qualifier = TYPE_QUAL_CONST;
13391           break;
13392
13393         case RID_VOLATILE:
13394           cv_qualifier = TYPE_QUAL_VOLATILE;
13395           break;
13396
13397         case RID_RESTRICT:
13398           cv_qualifier = TYPE_QUAL_RESTRICT;
13399           break;
13400
13401         default:
13402           cv_qualifier = TYPE_UNQUALIFIED;
13403           break;
13404         }
13405
13406       if (!cv_qualifier)
13407         break;
13408
13409       if (cv_quals & cv_qualifier)
13410         {
13411           error ("%Hduplicate cv-qualifier", &token->location);
13412           cp_lexer_purge_token (parser->lexer);
13413         }
13414       else
13415         {
13416           cp_lexer_consume_token (parser->lexer);
13417           cv_quals |= cv_qualifier;
13418         }
13419     }
13420
13421   return cv_quals;
13422 }
13423
13424 /* Parse a declarator-id.
13425
13426    declarator-id:
13427      id-expression
13428      :: [opt] nested-name-specifier [opt] type-name
13429
13430    In the `id-expression' case, the value returned is as for
13431    cp_parser_id_expression if the id-expression was an unqualified-id.
13432    If the id-expression was a qualified-id, then a SCOPE_REF is
13433    returned.  The first operand is the scope (either a NAMESPACE_DECL
13434    or TREE_TYPE), but the second is still just a representation of an
13435    unqualified-id.  */
13436
13437 static tree
13438 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13439 {
13440   tree id;
13441   /* The expression must be an id-expression.  Assume that qualified
13442      names are the names of types so that:
13443
13444        template <class T>
13445        int S<T>::R::i = 3;
13446
13447      will work; we must treat `S<T>::R' as the name of a type.
13448      Similarly, assume that qualified names are templates, where
13449      required, so that:
13450
13451        template <class T>
13452        int S<T>::R<T>::i = 3;
13453
13454      will work, too.  */
13455   id = cp_parser_id_expression (parser,
13456                                 /*template_keyword_p=*/false,
13457                                 /*check_dependency_p=*/false,
13458                                 /*template_p=*/NULL,
13459                                 /*declarator_p=*/true,
13460                                 optional_p);
13461   if (id && BASELINK_P (id))
13462     id = BASELINK_FUNCTIONS (id);
13463   return id;
13464 }
13465
13466 /* Parse a type-id.
13467
13468    type-id:
13469      type-specifier-seq abstract-declarator [opt]
13470
13471    Returns the TYPE specified.  */
13472
13473 static tree
13474 cp_parser_type_id (cp_parser* parser)
13475 {
13476   cp_decl_specifier_seq type_specifier_seq;
13477   cp_declarator *abstract_declarator;
13478
13479   /* Parse the type-specifier-seq.  */
13480   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13481                                 &type_specifier_seq);
13482   if (type_specifier_seq.type == error_mark_node)
13483     return error_mark_node;
13484
13485   /* There might or might not be an abstract declarator.  */
13486   cp_parser_parse_tentatively (parser);
13487   /* Look for the declarator.  */
13488   abstract_declarator
13489     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13490                             /*parenthesized_p=*/NULL,
13491                             /*member_p=*/false);
13492   /* Check to see if there really was a declarator.  */
13493   if (!cp_parser_parse_definitely (parser))
13494     abstract_declarator = NULL;
13495
13496   return groktypename (&type_specifier_seq, abstract_declarator);
13497 }
13498
13499 /* Parse a type-specifier-seq.
13500
13501    type-specifier-seq:
13502      type-specifier type-specifier-seq [opt]
13503
13504    GNU extension:
13505
13506    type-specifier-seq:
13507      attributes type-specifier-seq [opt]
13508
13509    If IS_CONDITION is true, we are at the start of a "condition",
13510    e.g., we've just seen "if (".
13511
13512    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13513
13514 static void
13515 cp_parser_type_specifier_seq (cp_parser* parser,
13516                               bool is_condition,
13517                               cp_decl_specifier_seq *type_specifier_seq)
13518 {
13519   bool seen_type_specifier = false;
13520   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13521   cp_token *start_token = NULL;
13522
13523   /* Clear the TYPE_SPECIFIER_SEQ.  */
13524   clear_decl_specs (type_specifier_seq);
13525
13526   /* Parse the type-specifiers and attributes.  */
13527   while (true)
13528     {
13529       tree type_specifier;
13530       bool is_cv_qualifier;
13531
13532       /* Check for attributes first.  */
13533       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13534         {
13535           type_specifier_seq->attributes =
13536             chainon (type_specifier_seq->attributes,
13537                      cp_parser_attributes_opt (parser));
13538           continue;
13539         }
13540
13541       /* record the token of the beginning of the type specifier seq,
13542          for error reporting purposes*/
13543      if (!start_token)
13544        start_token = cp_lexer_peek_token (parser->lexer);
13545
13546       /* Look for the type-specifier.  */
13547       type_specifier = cp_parser_type_specifier (parser,
13548                                                  flags,
13549                                                  type_specifier_seq,
13550                                                  /*is_declaration=*/false,
13551                                                  NULL,
13552                                                  &is_cv_qualifier);
13553       if (!type_specifier)
13554         {
13555           /* If the first type-specifier could not be found, this is not a
13556              type-specifier-seq at all.  */
13557           if (!seen_type_specifier)
13558             {
13559               cp_parser_error (parser, "expected type-specifier");
13560               type_specifier_seq->type = error_mark_node;
13561               return;
13562             }
13563           /* If subsequent type-specifiers could not be found, the
13564              type-specifier-seq is complete.  */
13565           break;
13566         }
13567
13568       seen_type_specifier = true;
13569       /* The standard says that a condition can be:
13570
13571             type-specifier-seq declarator = assignment-expression
13572
13573          However, given:
13574
13575            struct S {};
13576            if (int S = ...)
13577
13578          we should treat the "S" as a declarator, not as a
13579          type-specifier.  The standard doesn't say that explicitly for
13580          type-specifier-seq, but it does say that for
13581          decl-specifier-seq in an ordinary declaration.  Perhaps it
13582          would be clearer just to allow a decl-specifier-seq here, and
13583          then add a semantic restriction that if any decl-specifiers
13584          that are not type-specifiers appear, the program is invalid.  */
13585       if (is_condition && !is_cv_qualifier)
13586         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13587     }
13588
13589   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13590 }
13591
13592 /* Parse a parameter-declaration-clause.
13593
13594    parameter-declaration-clause:
13595      parameter-declaration-list [opt] ... [opt]
13596      parameter-declaration-list , ...
13597
13598    Returns a representation for the parameter declarations.  A return
13599    value of NULL indicates a parameter-declaration-clause consisting
13600    only of an ellipsis.  */
13601
13602 static cp_parameter_declarator *
13603 cp_parser_parameter_declaration_clause (cp_parser* parser)
13604 {
13605   cp_parameter_declarator *parameters;
13606   cp_token *token;
13607   bool ellipsis_p;
13608   bool is_error;
13609
13610   /* Peek at the next token.  */
13611   token = cp_lexer_peek_token (parser->lexer);
13612   /* Check for trivial parameter-declaration-clauses.  */
13613   if (token->type == CPP_ELLIPSIS)
13614     {
13615       /* Consume the `...' token.  */
13616       cp_lexer_consume_token (parser->lexer);
13617       return NULL;
13618     }
13619   else if (token->type == CPP_CLOSE_PAREN)
13620     /* There are no parameters.  */
13621     {
13622 #ifndef NO_IMPLICIT_EXTERN_C
13623       if (in_system_header && current_class_type == NULL
13624           && current_lang_name == lang_name_c)
13625         return NULL;
13626       else
13627 #endif
13628         return no_parameters;
13629     }
13630   /* Check for `(void)', too, which is a special case.  */
13631   else if (token->keyword == RID_VOID
13632            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13633                == CPP_CLOSE_PAREN))
13634     {
13635       /* Consume the `void' token.  */
13636       cp_lexer_consume_token (parser->lexer);
13637       /* There are no parameters.  */
13638       return no_parameters;
13639     }
13640
13641   /* Parse the parameter-declaration-list.  */
13642   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13643   /* If a parse error occurred while parsing the
13644      parameter-declaration-list, then the entire
13645      parameter-declaration-clause is erroneous.  */
13646   if (is_error)
13647     return NULL;
13648
13649   /* Peek at the next token.  */
13650   token = cp_lexer_peek_token (parser->lexer);
13651   /* If it's a `,', the clause should terminate with an ellipsis.  */
13652   if (token->type == CPP_COMMA)
13653     {
13654       /* Consume the `,'.  */
13655       cp_lexer_consume_token (parser->lexer);
13656       /* Expect an ellipsis.  */
13657       ellipsis_p
13658         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13659     }
13660   /* It might also be `...' if the optional trailing `,' was
13661      omitted.  */
13662   else if (token->type == CPP_ELLIPSIS)
13663     {
13664       /* Consume the `...' token.  */
13665       cp_lexer_consume_token (parser->lexer);
13666       /* And remember that we saw it.  */
13667       ellipsis_p = true;
13668     }
13669   else
13670     ellipsis_p = false;
13671
13672   /* Finish the parameter list.  */
13673   if (parameters && ellipsis_p)
13674     parameters->ellipsis_p = true;
13675
13676   return parameters;
13677 }
13678
13679 /* Parse a parameter-declaration-list.
13680
13681    parameter-declaration-list:
13682      parameter-declaration
13683      parameter-declaration-list , parameter-declaration
13684
13685    Returns a representation of the parameter-declaration-list, as for
13686    cp_parser_parameter_declaration_clause.  However, the
13687    `void_list_node' is never appended to the list.  Upon return,
13688    *IS_ERROR will be true iff an error occurred.  */
13689
13690 static cp_parameter_declarator *
13691 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13692 {
13693   cp_parameter_declarator *parameters = NULL;
13694   cp_parameter_declarator **tail = &parameters;
13695   bool saved_in_unbraced_linkage_specification_p;
13696
13697   /* Assume all will go well.  */
13698   *is_error = false;
13699   /* The special considerations that apply to a function within an
13700      unbraced linkage specifications do not apply to the parameters
13701      to the function.  */
13702   saved_in_unbraced_linkage_specification_p 
13703     = parser->in_unbraced_linkage_specification_p;
13704   parser->in_unbraced_linkage_specification_p = false;
13705
13706   /* Look for more parameters.  */
13707   while (true)
13708     {
13709       cp_parameter_declarator *parameter;
13710       bool parenthesized_p;
13711       /* Parse the parameter.  */
13712       parameter
13713         = cp_parser_parameter_declaration (parser,
13714                                            /*template_parm_p=*/false,
13715                                            &parenthesized_p);
13716
13717       /* If a parse error occurred parsing the parameter declaration,
13718          then the entire parameter-declaration-list is erroneous.  */
13719       if (!parameter)
13720         {
13721           *is_error = true;
13722           parameters = NULL;
13723           break;
13724         }
13725       /* Add the new parameter to the list.  */
13726       *tail = parameter;
13727       tail = &parameter->next;
13728
13729       /* Peek at the next token.  */
13730       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13731           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13732           /* These are for Objective-C++ */
13733           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13734           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13735         /* The parameter-declaration-list is complete.  */
13736         break;
13737       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13738         {
13739           cp_token *token;
13740
13741           /* Peek at the next token.  */
13742           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13743           /* If it's an ellipsis, then the list is complete.  */
13744           if (token->type == CPP_ELLIPSIS)
13745             break;
13746           /* Otherwise, there must be more parameters.  Consume the
13747              `,'.  */
13748           cp_lexer_consume_token (parser->lexer);
13749           /* When parsing something like:
13750
13751                 int i(float f, double d)
13752
13753              we can tell after seeing the declaration for "f" that we
13754              are not looking at an initialization of a variable "i",
13755              but rather at the declaration of a function "i".
13756
13757              Due to the fact that the parsing of template arguments
13758              (as specified to a template-id) requires backtracking we
13759              cannot use this technique when inside a template argument
13760              list.  */
13761           if (!parser->in_template_argument_list_p
13762               && !parser->in_type_id_in_expr_p
13763               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13764               /* However, a parameter-declaration of the form
13765                  "foat(f)" (which is a valid declaration of a
13766                  parameter "f") can also be interpreted as an
13767                  expression (the conversion of "f" to "float").  */
13768               && !parenthesized_p)
13769             cp_parser_commit_to_tentative_parse (parser);
13770         }
13771       else
13772         {
13773           cp_parser_error (parser, "expected %<,%> or %<...%>");
13774           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13775             cp_parser_skip_to_closing_parenthesis (parser,
13776                                                    /*recovering=*/true,
13777                                                    /*or_comma=*/false,
13778                                                    /*consume_paren=*/false);
13779           break;
13780         }
13781     }
13782
13783   parser->in_unbraced_linkage_specification_p
13784     = saved_in_unbraced_linkage_specification_p;
13785
13786   return parameters;
13787 }
13788
13789 /* Parse a parameter declaration.
13790
13791    parameter-declaration:
13792      decl-specifier-seq ... [opt] declarator
13793      decl-specifier-seq declarator = assignment-expression
13794      decl-specifier-seq ... [opt] abstract-declarator [opt]
13795      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13796
13797    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13798    declares a template parameter.  (In that case, a non-nested `>'
13799    token encountered during the parsing of the assignment-expression
13800    is not interpreted as a greater-than operator.)
13801
13802    Returns a representation of the parameter, or NULL if an error
13803    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13804    true iff the declarator is of the form "(p)".  */
13805
13806 static cp_parameter_declarator *
13807 cp_parser_parameter_declaration (cp_parser *parser,
13808                                  bool template_parm_p,
13809                                  bool *parenthesized_p)
13810 {
13811   int declares_class_or_enum;
13812   bool greater_than_is_operator_p;
13813   cp_decl_specifier_seq decl_specifiers;
13814   cp_declarator *declarator;
13815   tree default_argument;
13816   cp_token *token = NULL, *declarator_token_start = NULL;
13817   const char *saved_message;
13818
13819   /* In a template parameter, `>' is not an operator.
13820
13821      [temp.param]
13822
13823      When parsing a default template-argument for a non-type
13824      template-parameter, the first non-nested `>' is taken as the end
13825      of the template parameter-list rather than a greater-than
13826      operator.  */
13827   greater_than_is_operator_p = !template_parm_p;
13828
13829   /* Type definitions may not appear in parameter types.  */
13830   saved_message = parser->type_definition_forbidden_message;
13831   parser->type_definition_forbidden_message
13832     = "types may not be defined in parameter types";
13833
13834   /* Parse the declaration-specifiers.  */
13835   cp_parser_decl_specifier_seq (parser,
13836                                 CP_PARSER_FLAGS_NONE,
13837                                 &decl_specifiers,
13838                                 &declares_class_or_enum);
13839   /* If an error occurred, there's no reason to attempt to parse the
13840      rest of the declaration.  */
13841   if (cp_parser_error_occurred (parser))
13842     {
13843       parser->type_definition_forbidden_message = saved_message;
13844       return NULL;
13845     }
13846
13847   /* Peek at the next token.  */
13848   token = cp_lexer_peek_token (parser->lexer);
13849
13850   /* If the next token is a `)', `,', `=', `>', or `...', then there
13851      is no declarator. However, when variadic templates are enabled,
13852      there may be a declarator following `...'.  */
13853   if (token->type == CPP_CLOSE_PAREN
13854       || token->type == CPP_COMMA
13855       || token->type == CPP_EQ
13856       || token->type == CPP_GREATER)
13857     {
13858       declarator = NULL;
13859       if (parenthesized_p)
13860         *parenthesized_p = false;
13861     }
13862   /* Otherwise, there should be a declarator.  */
13863   else
13864     {
13865       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13866       parser->default_arg_ok_p = false;
13867
13868       /* After seeing a decl-specifier-seq, if the next token is not a
13869          "(", there is no possibility that the code is a valid
13870          expression.  Therefore, if parsing tentatively, we commit at
13871          this point.  */
13872       if (!parser->in_template_argument_list_p
13873           /* In an expression context, having seen:
13874
13875                (int((char ...
13876
13877              we cannot be sure whether we are looking at a
13878              function-type (taking a "char" as a parameter) or a cast
13879              of some object of type "char" to "int".  */
13880           && !parser->in_type_id_in_expr_p
13881           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13882           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13883         cp_parser_commit_to_tentative_parse (parser);
13884       /* Parse the declarator.  */
13885       declarator_token_start = token;
13886       declarator = cp_parser_declarator (parser,
13887                                          CP_PARSER_DECLARATOR_EITHER,
13888                                          /*ctor_dtor_or_conv_p=*/NULL,
13889                                          parenthesized_p,
13890                                          /*member_p=*/false);
13891       parser->default_arg_ok_p = saved_default_arg_ok_p;
13892       /* After the declarator, allow more attributes.  */
13893       decl_specifiers.attributes
13894         = chainon (decl_specifiers.attributes,
13895                    cp_parser_attributes_opt (parser));
13896     }
13897
13898   /* If the next token is an ellipsis, and we have not seen a
13899      declarator name, and the type of the declarator contains parameter
13900      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13901      a parameter pack expansion expression. Otherwise, leave the
13902      ellipsis for a C-style variadic function. */
13903   token = cp_lexer_peek_token (parser->lexer);
13904   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13905     {
13906       tree type = decl_specifiers.type;
13907
13908       if (type && DECL_P (type))
13909         type = TREE_TYPE (type);
13910
13911       if (type
13912           && TREE_CODE (type) != TYPE_PACK_EXPANSION
13913           && declarator_can_be_parameter_pack (declarator)
13914           && (!declarator || !declarator->parameter_pack_p)
13915           && uses_parameter_packs (type))
13916         {
13917           /* Consume the `...'. */
13918           cp_lexer_consume_token (parser->lexer);
13919           maybe_warn_variadic_templates ();
13920           
13921           /* Build a pack expansion type */
13922           if (declarator)
13923             declarator->parameter_pack_p = true;
13924           else
13925             decl_specifiers.type = make_pack_expansion (type);
13926         }
13927     }
13928
13929   /* The restriction on defining new types applies only to the type
13930      of the parameter, not to the default argument.  */
13931   parser->type_definition_forbidden_message = saved_message;
13932
13933   /* If the next token is `=', then process a default argument.  */
13934   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13935     {
13936       /* Consume the `='.  */
13937       cp_lexer_consume_token (parser->lexer);
13938
13939       /* If we are defining a class, then the tokens that make up the
13940          default argument must be saved and processed later.  */
13941       if (!template_parm_p && at_class_scope_p ()
13942           && TYPE_BEING_DEFINED (current_class_type))
13943         {
13944           unsigned depth = 0;
13945           int maybe_template_id = 0;
13946           cp_token *first_token;
13947           cp_token *token;
13948
13949           /* Add tokens until we have processed the entire default
13950              argument.  We add the range [first_token, token).  */
13951           first_token = cp_lexer_peek_token (parser->lexer);
13952           while (true)
13953             {
13954               bool done = false;
13955
13956               /* Peek at the next token.  */
13957               token = cp_lexer_peek_token (parser->lexer);
13958               /* What we do depends on what token we have.  */
13959               switch (token->type)
13960                 {
13961                   /* In valid code, a default argument must be
13962                      immediately followed by a `,' `)', or `...'.  */
13963                 case CPP_COMMA:
13964                   if (depth == 0 && maybe_template_id)
13965                     {
13966                       /* If we've seen a '<', we might be in a
13967                          template-argument-list.  Until Core issue 325 is
13968                          resolved, we don't know how this situation ought
13969                          to be handled, so try to DTRT.  We check whether
13970                          what comes after the comma is a valid parameter
13971                          declaration list.  If it is, then the comma ends
13972                          the default argument; otherwise the default
13973                          argument continues.  */
13974                       bool error = false;
13975
13976                       /* Set ITALP so cp_parser_parameter_declaration_list
13977                          doesn't decide to commit to this parse.  */
13978                       bool saved_italp = parser->in_template_argument_list_p;
13979                       parser->in_template_argument_list_p = true;
13980
13981                       cp_parser_parse_tentatively (parser);
13982                       cp_lexer_consume_token (parser->lexer);
13983                       cp_parser_parameter_declaration_list (parser, &error);
13984                       if (!cp_parser_error_occurred (parser) && !error)
13985                         done = true;
13986                       cp_parser_abort_tentative_parse (parser);
13987
13988                       parser->in_template_argument_list_p = saved_italp;
13989                       break;
13990                     }
13991                 case CPP_CLOSE_PAREN:
13992                 case CPP_ELLIPSIS:
13993                   /* If we run into a non-nested `;', `}', or `]',
13994                      then the code is invalid -- but the default
13995                      argument is certainly over.  */
13996                 case CPP_SEMICOLON:
13997                 case CPP_CLOSE_BRACE:
13998                 case CPP_CLOSE_SQUARE:
13999                   if (depth == 0)
14000                     done = true;
14001                   /* Update DEPTH, if necessary.  */
14002                   else if (token->type == CPP_CLOSE_PAREN
14003                            || token->type == CPP_CLOSE_BRACE
14004                            || token->type == CPP_CLOSE_SQUARE)
14005                     --depth;
14006                   break;
14007
14008                 case CPP_OPEN_PAREN:
14009                 case CPP_OPEN_SQUARE:
14010                 case CPP_OPEN_BRACE:
14011                   ++depth;
14012                   break;
14013
14014                 case CPP_LESS:
14015                   if (depth == 0)
14016                     /* This might be the comparison operator, or it might
14017                        start a template argument list.  */
14018                     ++maybe_template_id;
14019                   break;
14020
14021                 case CPP_RSHIFT:
14022                   if (cxx_dialect == cxx98)
14023                     break;
14024                   /* Fall through for C++0x, which treats the `>>'
14025                      operator like two `>' tokens in certain
14026                      cases.  */
14027
14028                 case CPP_GREATER:
14029                   if (depth == 0)
14030                     {
14031                       /* This might be an operator, or it might close a
14032                          template argument list.  But if a previous '<'
14033                          started a template argument list, this will have
14034                          closed it, so we can't be in one anymore.  */
14035                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14036                       if (maybe_template_id < 0)
14037                         maybe_template_id = 0;
14038                     }
14039                   break;
14040
14041                   /* If we run out of tokens, issue an error message.  */
14042                 case CPP_EOF:
14043                 case CPP_PRAGMA_EOL:
14044                   error ("%Hfile ends in default argument", &token->location);
14045                   done = true;
14046                   break;
14047
14048                 case CPP_NAME:
14049                 case CPP_SCOPE:
14050                   /* In these cases, we should look for template-ids.
14051                      For example, if the default argument is
14052                      `X<int, double>()', we need to do name lookup to
14053                      figure out whether or not `X' is a template; if
14054                      so, the `,' does not end the default argument.
14055
14056                      That is not yet done.  */
14057                   break;
14058
14059                 default:
14060                   break;
14061                 }
14062
14063               /* If we've reached the end, stop.  */
14064               if (done)
14065                 break;
14066
14067               /* Add the token to the token block.  */
14068               token = cp_lexer_consume_token (parser->lexer);
14069             }
14070
14071           /* Create a DEFAULT_ARG to represent the unparsed default
14072              argument.  */
14073           default_argument = make_node (DEFAULT_ARG);
14074           DEFARG_TOKENS (default_argument)
14075             = cp_token_cache_new (first_token, token);
14076           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14077         }
14078       /* Outside of a class definition, we can just parse the
14079          assignment-expression.  */
14080       else
14081         {
14082           token = cp_lexer_peek_token (parser->lexer);
14083           default_argument 
14084             = cp_parser_default_argument (parser, template_parm_p);
14085         }
14086
14087       if (!parser->default_arg_ok_p)
14088         {
14089           if (flag_permissive)
14090             warning (0, "deprecated use of default argument for parameter of non-function");
14091           else
14092             {
14093               error ("%Hdefault arguments are only "
14094                      "permitted for function parameters",
14095                      &token->location);
14096               default_argument = NULL_TREE;
14097             }
14098         }
14099       else if ((declarator && declarator->parameter_pack_p)
14100                || (decl_specifiers.type
14101                    && PACK_EXPANSION_P (decl_specifiers.type)))
14102         {
14103           const char* kind = template_parm_p? "template " : "";
14104           
14105           /* Find the name of the parameter pack.  */     
14106           cp_declarator *id_declarator = declarator;
14107           while (id_declarator && id_declarator->kind != cdk_id)
14108             id_declarator = id_declarator->declarator;
14109           
14110           if (id_declarator && id_declarator->kind == cdk_id)
14111             error ("%H%sparameter pack %qD cannot have a default argument",
14112                    &declarator_token_start->location,
14113                    kind, id_declarator->u.id.unqualified_name);
14114           else
14115             error ("%H%sparameter pack cannot have a default argument",
14116                    &declarator_token_start->location, kind);
14117           
14118           default_argument = NULL_TREE;
14119         }
14120     }
14121   else
14122     default_argument = NULL_TREE;
14123
14124   return make_parameter_declarator (&decl_specifiers,
14125                                     declarator,
14126                                     default_argument);
14127 }
14128
14129 /* Parse a default argument and return it.
14130
14131    TEMPLATE_PARM_P is true if this is a default argument for a
14132    non-type template parameter.  */
14133 static tree
14134 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14135 {
14136   tree default_argument = NULL_TREE;
14137   bool saved_greater_than_is_operator_p;
14138   bool saved_local_variables_forbidden_p;
14139
14140   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14141      set correctly.  */
14142   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14143   parser->greater_than_is_operator_p = !template_parm_p;
14144   /* Local variable names (and the `this' keyword) may not
14145      appear in a default argument.  */
14146   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14147   parser->local_variables_forbidden_p = true;
14148   /* The default argument expression may cause implicitly
14149      defined member functions to be synthesized, which will
14150      result in garbage collection.  We must treat this
14151      situation as if we were within the body of function so as
14152      to avoid collecting live data on the stack.  */
14153   ++function_depth;
14154   /* Parse the assignment-expression.  */
14155   if (template_parm_p)
14156     push_deferring_access_checks (dk_no_deferred);
14157   default_argument
14158     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14159   if (template_parm_p)
14160     pop_deferring_access_checks ();
14161   /* Restore saved state.  */
14162   --function_depth;
14163   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14164   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14165
14166   return default_argument;
14167 }
14168
14169 /* Parse a function-body.
14170
14171    function-body:
14172      compound_statement  */
14173
14174 static void
14175 cp_parser_function_body (cp_parser *parser)
14176 {
14177   cp_parser_compound_statement (parser, NULL, false);
14178 }
14179
14180 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14181    true if a ctor-initializer was present.  */
14182
14183 static bool
14184 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14185 {
14186   tree body;
14187   bool ctor_initializer_p;
14188
14189   /* Begin the function body.  */
14190   body = begin_function_body ();
14191   /* Parse the optional ctor-initializer.  */
14192   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14193   /* Parse the function-body.  */
14194   cp_parser_function_body (parser);
14195   /* Finish the function body.  */
14196   finish_function_body (body);
14197
14198   return ctor_initializer_p;
14199 }
14200
14201 /* Parse an initializer.
14202
14203    initializer:
14204      = initializer-clause
14205      ( expression-list )
14206
14207    Returns an expression representing the initializer.  If no
14208    initializer is present, NULL_TREE is returned.
14209
14210    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14211    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14212    set to TRUE if there is no initializer present.  If there is an
14213    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14214    is set to true; otherwise it is set to false.  */
14215
14216 static tree
14217 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14218                        bool* non_constant_p)
14219 {
14220   cp_token *token;
14221   tree init;
14222
14223   /* Peek at the next token.  */
14224   token = cp_lexer_peek_token (parser->lexer);
14225
14226   /* Let our caller know whether or not this initializer was
14227      parenthesized.  */
14228   *is_direct_init = (token->type != CPP_EQ);
14229   /* Assume that the initializer is constant.  */
14230   *non_constant_p = false;
14231
14232   if (token->type == CPP_EQ)
14233     {
14234       /* Consume the `='.  */
14235       cp_lexer_consume_token (parser->lexer);
14236       /* Parse the initializer-clause.  */
14237       init = cp_parser_initializer_clause (parser, non_constant_p);
14238     }
14239   else if (token->type == CPP_OPEN_PAREN)
14240     init = cp_parser_parenthesized_expression_list (parser, false,
14241                                                     /*cast_p=*/false,
14242                                                     /*allow_expansion_p=*/true,
14243                                                     non_constant_p);
14244   else if (token->type == CPP_OPEN_BRACE)
14245     {
14246       maybe_warn_cpp0x ("extended initializer lists");
14247       init = cp_parser_braced_list (parser, non_constant_p);
14248       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14249     }
14250   else
14251     {
14252       /* Anything else is an error.  */
14253       cp_parser_error (parser, "expected initializer");
14254       init = error_mark_node;
14255     }
14256
14257   return init;
14258 }
14259
14260 /* Parse an initializer-clause.
14261
14262    initializer-clause:
14263      assignment-expression
14264      braced-init-list
14265
14266    Returns an expression representing the initializer.
14267
14268    If the `assignment-expression' production is used the value
14269    returned is simply a representation for the expression.
14270
14271    Otherwise, calls cp_parser_braced_list.  */
14272
14273 static tree
14274 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14275 {
14276   tree initializer;
14277
14278   /* Assume the expression is constant.  */
14279   *non_constant_p = false;
14280
14281   /* If it is not a `{', then we are looking at an
14282      assignment-expression.  */
14283   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14284     {
14285       initializer
14286         = cp_parser_constant_expression (parser,
14287                                         /*allow_non_constant_p=*/true,
14288                                         non_constant_p);
14289       if (!*non_constant_p)
14290         initializer = fold_non_dependent_expr (initializer);
14291     }
14292   else
14293     initializer = cp_parser_braced_list (parser, non_constant_p);
14294
14295   return initializer;
14296 }
14297
14298 /* Parse a brace-enclosed initializer list.
14299
14300    braced-init-list:
14301      { initializer-list , [opt] }
14302      { }
14303
14304    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14305    the elements of the initializer-list (or NULL, if the last
14306    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14307    NULL_TREE.  There is no way to detect whether or not the optional
14308    trailing `,' was provided.  NON_CONSTANT_P is as for
14309    cp_parser_initializer.  */     
14310
14311 static tree
14312 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14313 {
14314   tree initializer;
14315
14316   /* Consume the `{' token.  */
14317   cp_lexer_consume_token (parser->lexer);
14318   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14319   initializer = make_node (CONSTRUCTOR);
14320   /* If it's not a `}', then there is a non-trivial initializer.  */
14321   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14322     {
14323       /* Parse the initializer list.  */
14324       CONSTRUCTOR_ELTS (initializer)
14325         = cp_parser_initializer_list (parser, non_constant_p);
14326       /* A trailing `,' token is allowed.  */
14327       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14328         cp_lexer_consume_token (parser->lexer);
14329     }
14330   /* Now, there should be a trailing `}'.  */
14331   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14332   TREE_TYPE (initializer) = init_list_type_node;
14333   return initializer;
14334 }
14335
14336 /* Parse an initializer-list.
14337
14338    initializer-list:
14339      initializer-clause ... [opt]
14340      initializer-list , initializer-clause ... [opt]
14341
14342    GNU Extension:
14343
14344    initializer-list:
14345      identifier : initializer-clause
14346      initializer-list, identifier : initializer-clause
14347
14348    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14349    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14350    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14351    as for cp_parser_initializer.  */
14352
14353 static VEC(constructor_elt,gc) *
14354 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14355 {
14356   VEC(constructor_elt,gc) *v = NULL;
14357
14358   /* Assume all of the expressions are constant.  */
14359   *non_constant_p = false;
14360
14361   /* Parse the rest of the list.  */
14362   while (true)
14363     {
14364       cp_token *token;
14365       tree identifier;
14366       tree initializer;
14367       bool clause_non_constant_p;
14368
14369       /* If the next token is an identifier and the following one is a
14370          colon, we are looking at the GNU designated-initializer
14371          syntax.  */
14372       if (cp_parser_allow_gnu_extensions_p (parser)
14373           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14374           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14375         {
14376           /* Warn the user that they are using an extension.  */
14377           if (pedantic)
14378             pedwarn ("ISO C++ does not allow designated initializers");
14379           /* Consume the identifier.  */
14380           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14381           /* Consume the `:'.  */
14382           cp_lexer_consume_token (parser->lexer);
14383         }
14384       else
14385         identifier = NULL_TREE;
14386
14387       /* Parse the initializer.  */
14388       initializer = cp_parser_initializer_clause (parser,
14389                                                   &clause_non_constant_p);
14390       /* If any clause is non-constant, so is the entire initializer.  */
14391       if (clause_non_constant_p)
14392         *non_constant_p = true;
14393
14394       /* If we have an ellipsis, this is an initializer pack
14395          expansion.  */
14396       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14397         {
14398           /* Consume the `...'.  */
14399           cp_lexer_consume_token (parser->lexer);
14400
14401           /* Turn the initializer into an initializer expansion.  */
14402           initializer = make_pack_expansion (initializer);
14403         }
14404
14405       /* Add it to the vector.  */
14406       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14407
14408       /* If the next token is not a comma, we have reached the end of
14409          the list.  */
14410       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14411         break;
14412
14413       /* Peek at the next token.  */
14414       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14415       /* If the next token is a `}', then we're still done.  An
14416          initializer-clause can have a trailing `,' after the
14417          initializer-list and before the closing `}'.  */
14418       if (token->type == CPP_CLOSE_BRACE)
14419         break;
14420
14421       /* Consume the `,' token.  */
14422       cp_lexer_consume_token (parser->lexer);
14423     }
14424
14425   return v;
14426 }
14427
14428 /* Classes [gram.class] */
14429
14430 /* Parse a class-name.
14431
14432    class-name:
14433      identifier
14434      template-id
14435
14436    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14437    to indicate that names looked up in dependent types should be
14438    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14439    keyword has been used to indicate that the name that appears next
14440    is a template.  TAG_TYPE indicates the explicit tag given before
14441    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14442    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14443    is the class being defined in a class-head.
14444
14445    Returns the TYPE_DECL representing the class.  */
14446
14447 static tree
14448 cp_parser_class_name (cp_parser *parser,
14449                       bool typename_keyword_p,
14450                       bool template_keyword_p,
14451                       enum tag_types tag_type,
14452                       bool check_dependency_p,
14453                       bool class_head_p,
14454                       bool is_declaration)
14455 {
14456   tree decl;
14457   tree scope;
14458   bool typename_p;
14459   cp_token *token;
14460
14461   /* All class-names start with an identifier.  */
14462   token = cp_lexer_peek_token (parser->lexer);
14463   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14464     {
14465       cp_parser_error (parser, "expected class-name");
14466       return error_mark_node;
14467     }
14468
14469   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14470      to a template-id, so we save it here.  */
14471   scope = parser->scope;
14472   if (scope == error_mark_node)
14473     return error_mark_node;
14474
14475   /* Any name names a type if we're following the `typename' keyword
14476      in a qualified name where the enclosing scope is type-dependent.  */
14477   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14478                 && dependent_type_p (scope));
14479   /* Handle the common case (an identifier, but not a template-id)
14480      efficiently.  */
14481   if (token->type == CPP_NAME
14482       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14483     {
14484       cp_token *identifier_token;
14485       tree identifier;
14486       bool ambiguous_p;
14487
14488       /* Look for the identifier.  */
14489       identifier_token = cp_lexer_peek_token (parser->lexer);
14490       ambiguous_p = identifier_token->ambiguous_p;
14491       identifier = cp_parser_identifier (parser);
14492       /* If the next token isn't an identifier, we are certainly not
14493          looking at a class-name.  */
14494       if (identifier == error_mark_node)
14495         decl = error_mark_node;
14496       /* If we know this is a type-name, there's no need to look it
14497          up.  */
14498       else if (typename_p)
14499         decl = identifier;
14500       else
14501         {
14502           tree ambiguous_decls;
14503           /* If we already know that this lookup is ambiguous, then
14504              we've already issued an error message; there's no reason
14505              to check again.  */
14506           if (ambiguous_p)
14507             {
14508               cp_parser_simulate_error (parser);
14509               return error_mark_node;
14510             }
14511           /* If the next token is a `::', then the name must be a type
14512              name.
14513
14514              [basic.lookup.qual]
14515
14516              During the lookup for a name preceding the :: scope
14517              resolution operator, object, function, and enumerator
14518              names are ignored.  */
14519           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14520             tag_type = typename_type;
14521           /* Look up the name.  */
14522           decl = cp_parser_lookup_name (parser, identifier,
14523                                         tag_type,
14524                                         /*is_template=*/false,
14525                                         /*is_namespace=*/false,
14526                                         check_dependency_p,
14527                                         &ambiguous_decls,
14528                                         identifier_token->location);
14529           if (ambiguous_decls)
14530             {
14531               error ("%Hreference to %qD is ambiguous",
14532                      &identifier_token->location, identifier);
14533               print_candidates (ambiguous_decls);
14534               if (cp_parser_parsing_tentatively (parser))
14535                 {
14536                   identifier_token->ambiguous_p = true;
14537                   cp_parser_simulate_error (parser);
14538                 }
14539               return error_mark_node;
14540             }
14541         }
14542     }
14543   else
14544     {
14545       /* Try a template-id.  */
14546       decl = cp_parser_template_id (parser, template_keyword_p,
14547                                     check_dependency_p,
14548                                     is_declaration);
14549       if (decl == error_mark_node)
14550         return error_mark_node;
14551     }
14552
14553   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14554
14555   /* If this is a typename, create a TYPENAME_TYPE.  */
14556   if (typename_p && decl != error_mark_node)
14557     {
14558       decl = make_typename_type (scope, decl, typename_type,
14559                                  /*complain=*/tf_error);
14560       if (decl != error_mark_node)
14561         decl = TYPE_NAME (decl);
14562     }
14563
14564   /* Check to see that it is really the name of a class.  */
14565   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14566       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14567       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14568     /* Situations like this:
14569
14570          template <typename T> struct A {
14571            typename T::template X<int>::I i;
14572          };
14573
14574        are problematic.  Is `T::template X<int>' a class-name?  The
14575        standard does not seem to be definitive, but there is no other
14576        valid interpretation of the following `::'.  Therefore, those
14577        names are considered class-names.  */
14578     {
14579       decl = make_typename_type (scope, decl, tag_type, tf_error);
14580       if (decl != error_mark_node)
14581         decl = TYPE_NAME (decl);
14582     }
14583   else if (TREE_CODE (decl) != TYPE_DECL
14584            || TREE_TYPE (decl) == error_mark_node
14585            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14586     decl = error_mark_node;
14587
14588   if (decl == error_mark_node)
14589     cp_parser_error (parser, "expected class-name");
14590
14591   return decl;
14592 }
14593
14594 /* Parse a class-specifier.
14595
14596    class-specifier:
14597      class-head { member-specification [opt] }
14598
14599    Returns the TREE_TYPE representing the class.  */
14600
14601 static tree
14602 cp_parser_class_specifier (cp_parser* parser)
14603 {
14604   cp_token *token;
14605   tree type;
14606   tree attributes = NULL_TREE;
14607   int has_trailing_semicolon;
14608   bool nested_name_specifier_p;
14609   unsigned saved_num_template_parameter_lists;
14610   bool saved_in_function_body;
14611   tree old_scope = NULL_TREE;
14612   tree scope = NULL_TREE;
14613   tree bases;
14614
14615   push_deferring_access_checks (dk_no_deferred);
14616
14617   /* Parse the class-head.  */
14618   type = cp_parser_class_head (parser,
14619                                &nested_name_specifier_p,
14620                                &attributes,
14621                                &bases);
14622   /* If the class-head was a semantic disaster, skip the entire body
14623      of the class.  */
14624   if (!type)
14625     {
14626       cp_parser_skip_to_end_of_block_or_statement (parser);
14627       pop_deferring_access_checks ();
14628       return error_mark_node;
14629     }
14630
14631   /* Look for the `{'.  */
14632   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14633     {
14634       pop_deferring_access_checks ();
14635       return error_mark_node;
14636     }
14637
14638   /* Process the base classes. If they're invalid, skip the 
14639      entire class body.  */
14640   if (!xref_basetypes (type, bases))
14641     {
14642       /* Consuming the closing brace yields better error messages
14643          later on.  */
14644       if (cp_parser_skip_to_closing_brace (parser))
14645         cp_lexer_consume_token (parser->lexer);
14646       pop_deferring_access_checks ();
14647       return error_mark_node;
14648     }
14649
14650   /* Issue an error message if type-definitions are forbidden here.  */
14651   cp_parser_check_type_definition (parser);
14652   /* Remember that we are defining one more class.  */
14653   ++parser->num_classes_being_defined;
14654   /* Inside the class, surrounding template-parameter-lists do not
14655      apply.  */
14656   saved_num_template_parameter_lists
14657     = parser->num_template_parameter_lists;
14658   parser->num_template_parameter_lists = 0;
14659   /* We are not in a function body.  */
14660   saved_in_function_body = parser->in_function_body;
14661   parser->in_function_body = false;
14662
14663   /* Start the class.  */
14664   if (nested_name_specifier_p)
14665     {
14666       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14667       old_scope = push_inner_scope (scope);
14668     }
14669   type = begin_class_definition (type, attributes);
14670
14671   if (type == error_mark_node)
14672     /* If the type is erroneous, skip the entire body of the class.  */
14673     cp_parser_skip_to_closing_brace (parser);
14674   else
14675     /* Parse the member-specification.  */
14676     cp_parser_member_specification_opt (parser);
14677
14678   /* Look for the trailing `}'.  */
14679   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14680   /* We get better error messages by noticing a common problem: a
14681      missing trailing `;'.  */
14682   token = cp_lexer_peek_token (parser->lexer);
14683   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14684   /* Look for trailing attributes to apply to this class.  */
14685   if (cp_parser_allow_gnu_extensions_p (parser))
14686     attributes = cp_parser_attributes_opt (parser);
14687   if (type != error_mark_node)
14688     type = finish_struct (type, attributes);
14689   if (nested_name_specifier_p)
14690     pop_inner_scope (old_scope, scope);
14691   /* If this class is not itself within the scope of another class,
14692      then we need to parse the bodies of all of the queued function
14693      definitions.  Note that the queued functions defined in a class
14694      are not always processed immediately following the
14695      class-specifier for that class.  Consider:
14696
14697        struct A {
14698          struct B { void f() { sizeof (A); } };
14699        };
14700
14701      If `f' were processed before the processing of `A' were
14702      completed, there would be no way to compute the size of `A'.
14703      Note that the nesting we are interested in here is lexical --
14704      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14705      for:
14706
14707        struct A { struct B; };
14708        struct A::B { void f() { } };
14709
14710      there is no need to delay the parsing of `A::B::f'.  */
14711   if (--parser->num_classes_being_defined == 0)
14712     {
14713       tree queue_entry;
14714       tree fn;
14715       tree class_type = NULL_TREE;
14716       tree pushed_scope = NULL_TREE;
14717
14718       /* In a first pass, parse default arguments to the functions.
14719          Then, in a second pass, parse the bodies of the functions.
14720          This two-phased approach handles cases like:
14721
14722             struct S {
14723               void f() { g(); }
14724               void g(int i = 3);
14725             };
14726
14727          */
14728       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14729              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14730            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14731            TREE_PURPOSE (parser->unparsed_functions_queues)
14732              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14733         {
14734           fn = TREE_VALUE (queue_entry);
14735           /* If there are default arguments that have not yet been processed,
14736              take care of them now.  */
14737           if (class_type != TREE_PURPOSE (queue_entry))
14738             {
14739               if (pushed_scope)
14740                 pop_scope (pushed_scope);
14741               class_type = TREE_PURPOSE (queue_entry);
14742               pushed_scope = push_scope (class_type);
14743             }
14744           /* Make sure that any template parameters are in scope.  */
14745           maybe_begin_member_template_processing (fn);
14746           /* Parse the default argument expressions.  */
14747           cp_parser_late_parsing_default_args (parser, fn);
14748           /* Remove any template parameters from the symbol table.  */
14749           maybe_end_member_template_processing ();
14750         }
14751       if (pushed_scope)
14752         pop_scope (pushed_scope);
14753       /* Now parse the body of the functions.  */
14754       for (TREE_VALUE (parser->unparsed_functions_queues)
14755              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14756            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14757            TREE_VALUE (parser->unparsed_functions_queues)
14758              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14759         {
14760           /* Figure out which function we need to process.  */
14761           fn = TREE_VALUE (queue_entry);
14762           /* Parse the function.  */
14763           cp_parser_late_parsing_for_member (parser, fn);
14764         }
14765     }
14766
14767   /* Put back any saved access checks.  */
14768   pop_deferring_access_checks ();
14769
14770   /* Restore saved state.  */
14771   parser->in_function_body = saved_in_function_body;
14772   parser->num_template_parameter_lists
14773     = saved_num_template_parameter_lists;
14774
14775   return type;
14776 }
14777
14778 /* Parse a class-head.
14779
14780    class-head:
14781      class-key identifier [opt] base-clause [opt]
14782      class-key nested-name-specifier identifier base-clause [opt]
14783      class-key nested-name-specifier [opt] template-id
14784        base-clause [opt]
14785
14786    GNU Extensions:
14787      class-key attributes identifier [opt] base-clause [opt]
14788      class-key attributes nested-name-specifier identifier base-clause [opt]
14789      class-key attributes nested-name-specifier [opt] template-id
14790        base-clause [opt]
14791
14792    Upon return BASES is initialized to the list of base classes (or
14793    NULL, if there are none) in the same form returned by
14794    cp_parser_base_clause.
14795
14796    Returns the TYPE of the indicated class.  Sets
14797    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14798    involving a nested-name-specifier was used, and FALSE otherwise.
14799
14800    Returns error_mark_node if this is not a class-head.
14801
14802    Returns NULL_TREE if the class-head is syntactically valid, but
14803    semantically invalid in a way that means we should skip the entire
14804    body of the class.  */
14805
14806 static tree
14807 cp_parser_class_head (cp_parser* parser,
14808                       bool* nested_name_specifier_p,
14809                       tree *attributes_p,
14810                       tree *bases)
14811 {
14812   tree nested_name_specifier;
14813   enum tag_types class_key;
14814   tree id = NULL_TREE;
14815   tree type = NULL_TREE;
14816   tree attributes;
14817   bool template_id_p = false;
14818   bool qualified_p = false;
14819   bool invalid_nested_name_p = false;
14820   bool invalid_explicit_specialization_p = false;
14821   tree pushed_scope = NULL_TREE;
14822   unsigned num_templates;
14823   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
14824   /* Assume no nested-name-specifier will be present.  */
14825   *nested_name_specifier_p = false;
14826   /* Assume no template parameter lists will be used in defining the
14827      type.  */
14828   num_templates = 0;
14829
14830   *bases = NULL_TREE;
14831
14832   /* Look for the class-key.  */
14833   class_key = cp_parser_class_key (parser);
14834   if (class_key == none_type)
14835     return error_mark_node;
14836
14837   /* Parse the attributes.  */
14838   attributes = cp_parser_attributes_opt (parser);
14839
14840   /* If the next token is `::', that is invalid -- but sometimes
14841      people do try to write:
14842
14843        struct ::S {};
14844
14845      Handle this gracefully by accepting the extra qualifier, and then
14846      issuing an error about it later if this really is a
14847      class-head.  If it turns out just to be an elaborated type
14848      specifier, remain silent.  */
14849   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14850     qualified_p = true;
14851
14852   push_deferring_access_checks (dk_no_check);
14853
14854   /* Determine the name of the class.  Begin by looking for an
14855      optional nested-name-specifier.  */
14856   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
14857   nested_name_specifier
14858     = cp_parser_nested_name_specifier_opt (parser,
14859                                            /*typename_keyword_p=*/false,
14860                                            /*check_dependency_p=*/false,
14861                                            /*type_p=*/false,
14862                                            /*is_declaration=*/false);
14863   /* If there was a nested-name-specifier, then there *must* be an
14864      identifier.  */
14865   if (nested_name_specifier)
14866     {
14867       type_start_token = cp_lexer_peek_token (parser->lexer);
14868       /* Although the grammar says `identifier', it really means
14869          `class-name' or `template-name'.  You are only allowed to
14870          define a class that has already been declared with this
14871          syntax.
14872
14873          The proposed resolution for Core Issue 180 says that wherever
14874          you see `class T::X' you should treat `X' as a type-name.
14875
14876          It is OK to define an inaccessible class; for example:
14877
14878            class A { class B; };
14879            class A::B {};
14880
14881          We do not know if we will see a class-name, or a
14882          template-name.  We look for a class-name first, in case the
14883          class-name is a template-id; if we looked for the
14884          template-name first we would stop after the template-name.  */
14885       cp_parser_parse_tentatively (parser);
14886       type = cp_parser_class_name (parser,
14887                                    /*typename_keyword_p=*/false,
14888                                    /*template_keyword_p=*/false,
14889                                    class_type,
14890                                    /*check_dependency_p=*/false,
14891                                    /*class_head_p=*/true,
14892                                    /*is_declaration=*/false);
14893       /* If that didn't work, ignore the nested-name-specifier.  */
14894       if (!cp_parser_parse_definitely (parser))
14895         {
14896           invalid_nested_name_p = true;
14897           type_start_token = cp_lexer_peek_token (parser->lexer);
14898           id = cp_parser_identifier (parser);
14899           if (id == error_mark_node)
14900             id = NULL_TREE;
14901         }
14902       /* If we could not find a corresponding TYPE, treat this
14903          declaration like an unqualified declaration.  */
14904       if (type == error_mark_node)
14905         nested_name_specifier = NULL_TREE;
14906       /* Otherwise, count the number of templates used in TYPE and its
14907          containing scopes.  */
14908       else
14909         {
14910           tree scope;
14911
14912           for (scope = TREE_TYPE (type);
14913                scope && TREE_CODE (scope) != NAMESPACE_DECL;
14914                scope = (TYPE_P (scope)
14915                         ? TYPE_CONTEXT (scope)
14916                         : DECL_CONTEXT (scope)))
14917             if (TYPE_P (scope)
14918                 && CLASS_TYPE_P (scope)
14919                 && CLASSTYPE_TEMPLATE_INFO (scope)
14920                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14921                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14922               ++num_templates;
14923         }
14924     }
14925   /* Otherwise, the identifier is optional.  */
14926   else
14927     {
14928       /* We don't know whether what comes next is a template-id,
14929          an identifier, or nothing at all.  */
14930       cp_parser_parse_tentatively (parser);
14931       /* Check for a template-id.  */
14932       type_start_token = cp_lexer_peek_token (parser->lexer);
14933       id = cp_parser_template_id (parser,
14934                                   /*template_keyword_p=*/false,
14935                                   /*check_dependency_p=*/true,
14936                                   /*is_declaration=*/true);
14937       /* If that didn't work, it could still be an identifier.  */
14938       if (!cp_parser_parse_definitely (parser))
14939         {
14940           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14941             {
14942               type_start_token = cp_lexer_peek_token (parser->lexer);
14943               id = cp_parser_identifier (parser);
14944             }
14945           else
14946             id = NULL_TREE;
14947         }
14948       else
14949         {
14950           template_id_p = true;
14951           ++num_templates;
14952         }
14953     }
14954
14955   pop_deferring_access_checks ();
14956
14957   if (id)
14958     cp_parser_check_for_invalid_template_id (parser, id,
14959                                              type_start_token->location);
14960
14961   /* If it's not a `:' or a `{' then we can't really be looking at a
14962      class-head, since a class-head only appears as part of a
14963      class-specifier.  We have to detect this situation before calling
14964      xref_tag, since that has irreversible side-effects.  */
14965   if (!cp_parser_next_token_starts_class_definition_p (parser))
14966     {
14967       cp_parser_error (parser, "expected %<{%> or %<:%>");
14968       return error_mark_node;
14969     }
14970
14971   /* At this point, we're going ahead with the class-specifier, even
14972      if some other problem occurs.  */
14973   cp_parser_commit_to_tentative_parse (parser);
14974   /* Issue the error about the overly-qualified name now.  */
14975   if (qualified_p)
14976     cp_parser_error (parser,
14977                      "global qualification of class name is invalid");
14978   else if (invalid_nested_name_p)
14979     cp_parser_error (parser,
14980                      "qualified name does not name a class");
14981   else if (nested_name_specifier)
14982     {
14983       tree scope;
14984
14985       /* Reject typedef-names in class heads.  */
14986       if (!DECL_IMPLICIT_TYPEDEF_P (type))
14987         {
14988           error ("%Hinvalid class name in declaration of %qD",
14989                  &type_start_token->location, type);
14990           type = NULL_TREE;
14991           goto done;
14992         }
14993
14994       /* Figure out in what scope the declaration is being placed.  */
14995       scope = current_scope ();
14996       /* If that scope does not contain the scope in which the
14997          class was originally declared, the program is invalid.  */
14998       if (scope && !is_ancestor (scope, nested_name_specifier))
14999         {
15000           if (at_namespace_scope_p ())
15001             error ("%Hdeclaration of %qD in namespace %qD which does not "
15002                    "enclose %qD",
15003                    &type_start_token->location,
15004                    type, scope, nested_name_specifier);
15005           else
15006             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15007                    &type_start_token->location,
15008                    type, scope, nested_name_specifier);
15009           type = NULL_TREE;
15010           goto done;
15011         }
15012       /* [dcl.meaning]
15013
15014          A declarator-id shall not be qualified exception of the
15015          definition of a ... nested class outside of its class
15016          ... [or] a the definition or explicit instantiation of a
15017          class member of a namespace outside of its namespace.  */
15018       if (scope == nested_name_specifier)
15019         {
15020           permerror ("%Hextra qualification not allowed",
15021                      &nested_name_specifier_token_start->location);
15022           nested_name_specifier = NULL_TREE;
15023           num_templates = 0;
15024         }
15025     }
15026   /* An explicit-specialization must be preceded by "template <>".  If
15027      it is not, try to recover gracefully.  */
15028   if (at_namespace_scope_p ()
15029       && parser->num_template_parameter_lists == 0
15030       && template_id_p)
15031     {
15032       error ("%Han explicit specialization must be preceded by %<template <>%>",
15033              &type_start_token->location);
15034       invalid_explicit_specialization_p = true;
15035       /* Take the same action that would have been taken by
15036          cp_parser_explicit_specialization.  */
15037       ++parser->num_template_parameter_lists;
15038       begin_specialization ();
15039     }
15040   /* There must be no "return" statements between this point and the
15041      end of this function; set "type "to the correct return value and
15042      use "goto done;" to return.  */
15043   /* Make sure that the right number of template parameters were
15044      present.  */
15045   if (!cp_parser_check_template_parameters (parser, num_templates,
15046                                             type_start_token->location))
15047     {
15048       /* If something went wrong, there is no point in even trying to
15049          process the class-definition.  */
15050       type = NULL_TREE;
15051       goto done;
15052     }
15053
15054   /* Look up the type.  */
15055   if (template_id_p)
15056     {
15057       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15058           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15059               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15060         {
15061           error ("%Hfunction template %qD redeclared as a class template",
15062                  &type_start_token->location, id);
15063           type = error_mark_node;
15064         }
15065       else
15066         {
15067           type = TREE_TYPE (id);
15068           type = maybe_process_partial_specialization (type);
15069         }
15070       if (nested_name_specifier)
15071         pushed_scope = push_scope (nested_name_specifier);
15072     }
15073   else if (nested_name_specifier)
15074     {
15075       tree class_type;
15076
15077       /* Given:
15078
15079             template <typename T> struct S { struct T };
15080             template <typename T> struct S<T>::T { };
15081
15082          we will get a TYPENAME_TYPE when processing the definition of
15083          `S::T'.  We need to resolve it to the actual type before we
15084          try to define it.  */
15085       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15086         {
15087           class_type = resolve_typename_type (TREE_TYPE (type),
15088                                               /*only_current_p=*/false);
15089           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15090             type = TYPE_NAME (class_type);
15091           else
15092             {
15093               cp_parser_error (parser, "could not resolve typename type");
15094               type = error_mark_node;
15095             }
15096         }
15097
15098       if (maybe_process_partial_specialization (TREE_TYPE (type))
15099           == error_mark_node)
15100         {
15101           type = NULL_TREE;
15102           goto done;
15103         }
15104
15105       class_type = current_class_type;
15106       /* Enter the scope indicated by the nested-name-specifier.  */
15107       pushed_scope = push_scope (nested_name_specifier);
15108       /* Get the canonical version of this type.  */
15109       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15110       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15111           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15112         {
15113           type = push_template_decl (type);
15114           if (type == error_mark_node)
15115             {
15116               type = NULL_TREE;
15117               goto done;
15118             }
15119         }
15120
15121       type = TREE_TYPE (type);
15122       *nested_name_specifier_p = true;
15123     }
15124   else      /* The name is not a nested name.  */
15125     {
15126       /* If the class was unnamed, create a dummy name.  */
15127       if (!id)
15128         id = make_anon_name ();
15129       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15130                        parser->num_template_parameter_lists);
15131     }
15132
15133   /* Indicate whether this class was declared as a `class' or as a
15134      `struct'.  */
15135   if (TREE_CODE (type) == RECORD_TYPE)
15136     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15137   cp_parser_check_class_key (class_key, type);
15138
15139   /* If this type was already complete, and we see another definition,
15140      that's an error.  */
15141   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15142     {
15143       error ("%Hredefinition of %q#T",
15144              &type_start_token->location, type);
15145       error ("%Hprevious definition of %q+#T",
15146              &type_start_token->location, type);
15147       type = NULL_TREE;
15148       goto done;
15149     }
15150   else if (type == error_mark_node)
15151     type = NULL_TREE;
15152
15153   /* We will have entered the scope containing the class; the names of
15154      base classes should be looked up in that context.  For example:
15155
15156        struct A { struct B {}; struct C; };
15157        struct A::C : B {};
15158
15159      is valid.  */
15160
15161   /* Get the list of base-classes, if there is one.  */
15162   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15163     *bases = cp_parser_base_clause (parser);
15164
15165  done:
15166   /* Leave the scope given by the nested-name-specifier.  We will
15167      enter the class scope itself while processing the members.  */
15168   if (pushed_scope)
15169     pop_scope (pushed_scope);
15170
15171   if (invalid_explicit_specialization_p)
15172     {
15173       end_specialization ();
15174       --parser->num_template_parameter_lists;
15175     }
15176   *attributes_p = attributes;
15177   return type;
15178 }
15179
15180 /* Parse a class-key.
15181
15182    class-key:
15183      class
15184      struct
15185      union
15186
15187    Returns the kind of class-key specified, or none_type to indicate
15188    error.  */
15189
15190 static enum tag_types
15191 cp_parser_class_key (cp_parser* parser)
15192 {
15193   cp_token *token;
15194   enum tag_types tag_type;
15195
15196   /* Look for the class-key.  */
15197   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15198   if (!token)
15199     return none_type;
15200
15201   /* Check to see if the TOKEN is a class-key.  */
15202   tag_type = cp_parser_token_is_class_key (token);
15203   if (!tag_type)
15204     cp_parser_error (parser, "expected class-key");
15205   return tag_type;
15206 }
15207
15208 /* Parse an (optional) member-specification.
15209
15210    member-specification:
15211      member-declaration member-specification [opt]
15212      access-specifier : member-specification [opt]  */
15213
15214 static void
15215 cp_parser_member_specification_opt (cp_parser* parser)
15216 {
15217   while (true)
15218     {
15219       cp_token *token;
15220       enum rid keyword;
15221
15222       /* Peek at the next token.  */
15223       token = cp_lexer_peek_token (parser->lexer);
15224       /* If it's a `}', or EOF then we've seen all the members.  */
15225       if (token->type == CPP_CLOSE_BRACE
15226           || token->type == CPP_EOF
15227           || token->type == CPP_PRAGMA_EOL)
15228         break;
15229
15230       /* See if this token is a keyword.  */
15231       keyword = token->keyword;
15232       switch (keyword)
15233         {
15234         case RID_PUBLIC:
15235         case RID_PROTECTED:
15236         case RID_PRIVATE:
15237           /* Consume the access-specifier.  */
15238           cp_lexer_consume_token (parser->lexer);
15239           /* Remember which access-specifier is active.  */
15240           current_access_specifier = token->u.value;
15241           /* Look for the `:'.  */
15242           cp_parser_require (parser, CPP_COLON, "%<:%>");
15243           break;
15244
15245         default:
15246           /* Accept #pragmas at class scope.  */
15247           if (token->type == CPP_PRAGMA)
15248             {
15249               cp_parser_pragma (parser, pragma_external);
15250               break;
15251             }
15252
15253           /* Otherwise, the next construction must be a
15254              member-declaration.  */
15255           cp_parser_member_declaration (parser);
15256         }
15257     }
15258 }
15259
15260 /* Parse a member-declaration.
15261
15262    member-declaration:
15263      decl-specifier-seq [opt] member-declarator-list [opt] ;
15264      function-definition ; [opt]
15265      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15266      using-declaration
15267      template-declaration
15268
15269    member-declarator-list:
15270      member-declarator
15271      member-declarator-list , member-declarator
15272
15273    member-declarator:
15274      declarator pure-specifier [opt]
15275      declarator constant-initializer [opt]
15276      identifier [opt] : constant-expression
15277
15278    GNU Extensions:
15279
15280    member-declaration:
15281      __extension__ member-declaration
15282
15283    member-declarator:
15284      declarator attributes [opt] pure-specifier [opt]
15285      declarator attributes [opt] constant-initializer [opt]
15286      identifier [opt] attributes [opt] : constant-expression  
15287
15288    C++0x Extensions:
15289
15290    member-declaration:
15291      static_assert-declaration  */
15292
15293 static void
15294 cp_parser_member_declaration (cp_parser* parser)
15295 {
15296   cp_decl_specifier_seq decl_specifiers;
15297   tree prefix_attributes;
15298   tree decl;
15299   int declares_class_or_enum;
15300   bool friend_p;
15301   cp_token *token = NULL;
15302   cp_token *decl_spec_token_start = NULL;
15303   cp_token *initializer_token_start = NULL;
15304   int saved_pedantic;
15305
15306   /* Check for the `__extension__' keyword.  */
15307   if (cp_parser_extension_opt (parser, &saved_pedantic))
15308     {
15309       /* Recurse.  */
15310       cp_parser_member_declaration (parser);
15311       /* Restore the old value of the PEDANTIC flag.  */
15312       pedantic = saved_pedantic;
15313
15314       return;
15315     }
15316
15317   /* Check for a template-declaration.  */
15318   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15319     {
15320       /* An explicit specialization here is an error condition, and we
15321          expect the specialization handler to detect and report this.  */
15322       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15323           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15324         cp_parser_explicit_specialization (parser);
15325       else
15326         cp_parser_template_declaration (parser, /*member_p=*/true);
15327
15328       return;
15329     }
15330
15331   /* Check for a using-declaration.  */
15332   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15333     {
15334       /* Parse the using-declaration.  */
15335       cp_parser_using_declaration (parser,
15336                                    /*access_declaration_p=*/false);
15337       return;
15338     }
15339
15340   /* Check for @defs.  */
15341   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15342     {
15343       tree ivar, member;
15344       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15345       ivar = ivar_chains;
15346       while (ivar)
15347         {
15348           member = ivar;
15349           ivar = TREE_CHAIN (member);
15350           TREE_CHAIN (member) = NULL_TREE;
15351           finish_member_declaration (member);
15352         }
15353       return;
15354     }
15355
15356   /* If the next token is `static_assert' we have a static assertion.  */
15357   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15358     {
15359       cp_parser_static_assert (parser, /*member_p=*/true);
15360       return;
15361     }
15362
15363   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15364     return;
15365
15366   /* Parse the decl-specifier-seq.  */
15367   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15368   cp_parser_decl_specifier_seq (parser,
15369                                 CP_PARSER_FLAGS_OPTIONAL,
15370                                 &decl_specifiers,
15371                                 &declares_class_or_enum);
15372   prefix_attributes = decl_specifiers.attributes;
15373   decl_specifiers.attributes = NULL_TREE;
15374   /* Check for an invalid type-name.  */
15375   if (!decl_specifiers.type
15376       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15377     return;
15378   /* If there is no declarator, then the decl-specifier-seq should
15379      specify a type.  */
15380   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15381     {
15382       /* If there was no decl-specifier-seq, and the next token is a
15383          `;', then we have something like:
15384
15385            struct S { ; };
15386
15387          [class.mem]
15388
15389          Each member-declaration shall declare at least one member
15390          name of the class.  */
15391       if (!decl_specifiers.any_specifiers_p)
15392         {
15393           cp_token *token = cp_lexer_peek_token (parser->lexer);
15394           if (pedantic && !token->in_system_header)
15395             pedwarn ("%Hextra %<;%>", &token->location);
15396         }
15397       else
15398         {
15399           tree type;
15400
15401           /* See if this declaration is a friend.  */
15402           friend_p = cp_parser_friend_p (&decl_specifiers);
15403           /* If there were decl-specifiers, check to see if there was
15404              a class-declaration.  */
15405           type = check_tag_decl (&decl_specifiers);
15406           /* Nested classes have already been added to the class, but
15407              a `friend' needs to be explicitly registered.  */
15408           if (friend_p)
15409             {
15410               /* If the `friend' keyword was present, the friend must
15411                  be introduced with a class-key.  */
15412                if (!declares_class_or_enum)
15413                  error ("%Ha class-key must be used when declaring a friend",
15414                         &decl_spec_token_start->location);
15415                /* In this case:
15416
15417                     template <typename T> struct A {
15418                       friend struct A<T>::B;
15419                     };
15420
15421                   A<T>::B will be represented by a TYPENAME_TYPE, and
15422                   therefore not recognized by check_tag_decl.  */
15423                if (!type
15424                    && decl_specifiers.type
15425                    && TYPE_P (decl_specifiers.type))
15426                  type = decl_specifiers.type;
15427                if (!type || !TYPE_P (type))
15428                  error ("%Hfriend declaration does not name a class or "
15429                         "function", &decl_spec_token_start->location);
15430                else
15431                  make_friend_class (current_class_type, type,
15432                                     /*complain=*/true);
15433             }
15434           /* If there is no TYPE, an error message will already have
15435              been issued.  */
15436           else if (!type || type == error_mark_node)
15437             ;
15438           /* An anonymous aggregate has to be handled specially; such
15439              a declaration really declares a data member (with a
15440              particular type), as opposed to a nested class.  */
15441           else if (ANON_AGGR_TYPE_P (type))
15442             {
15443               /* Remove constructors and such from TYPE, now that we
15444                  know it is an anonymous aggregate.  */
15445               fixup_anonymous_aggr (type);
15446               /* And make the corresponding data member.  */
15447               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15448               /* Add it to the class.  */
15449               finish_member_declaration (decl);
15450             }
15451           else
15452             cp_parser_check_access_in_redeclaration
15453                                               (TYPE_NAME (type),
15454                                                decl_spec_token_start->location);
15455         }
15456     }
15457   else
15458     {
15459       /* See if these declarations will be friends.  */
15460       friend_p = cp_parser_friend_p (&decl_specifiers);
15461
15462       /* Keep going until we hit the `;' at the end of the
15463          declaration.  */
15464       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15465         {
15466           tree attributes = NULL_TREE;
15467           tree first_attribute;
15468
15469           /* Peek at the next token.  */
15470           token = cp_lexer_peek_token (parser->lexer);
15471
15472           /* Check for a bitfield declaration.  */
15473           if (token->type == CPP_COLON
15474               || (token->type == CPP_NAME
15475                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15476                   == CPP_COLON))
15477             {
15478               tree identifier;
15479               tree width;
15480
15481               /* Get the name of the bitfield.  Note that we cannot just
15482                  check TOKEN here because it may have been invalidated by
15483                  the call to cp_lexer_peek_nth_token above.  */
15484               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15485                 identifier = cp_parser_identifier (parser);
15486               else
15487                 identifier = NULL_TREE;
15488
15489               /* Consume the `:' token.  */
15490               cp_lexer_consume_token (parser->lexer);
15491               /* Get the width of the bitfield.  */
15492               width
15493                 = cp_parser_constant_expression (parser,
15494                                                  /*allow_non_constant=*/false,
15495                                                  NULL);
15496
15497               /* Look for attributes that apply to the bitfield.  */
15498               attributes = cp_parser_attributes_opt (parser);
15499               /* Remember which attributes are prefix attributes and
15500                  which are not.  */
15501               first_attribute = attributes;
15502               /* Combine the attributes.  */
15503               attributes = chainon (prefix_attributes, attributes);
15504
15505               /* Create the bitfield declaration.  */
15506               decl = grokbitfield (identifier
15507                                    ? make_id_declarator (NULL_TREE,
15508                                                          identifier,
15509                                                          sfk_none)
15510                                    : NULL,
15511                                    &decl_specifiers,
15512                                    width,
15513                                    attributes);
15514             }
15515           else
15516             {
15517               cp_declarator *declarator;
15518               tree initializer;
15519               tree asm_specification;
15520               int ctor_dtor_or_conv_p;
15521
15522               /* Parse the declarator.  */
15523               declarator
15524                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15525                                         &ctor_dtor_or_conv_p,
15526                                         /*parenthesized_p=*/NULL,
15527                                         /*member_p=*/true);
15528
15529               /* If something went wrong parsing the declarator, make sure
15530                  that we at least consume some tokens.  */
15531               if (declarator == cp_error_declarator)
15532                 {
15533                   /* Skip to the end of the statement.  */
15534                   cp_parser_skip_to_end_of_statement (parser);
15535                   /* If the next token is not a semicolon, that is
15536                      probably because we just skipped over the body of
15537                      a function.  So, we consume a semicolon if
15538                      present, but do not issue an error message if it
15539                      is not present.  */
15540                   if (cp_lexer_next_token_is (parser->lexer,
15541                                               CPP_SEMICOLON))
15542                     cp_lexer_consume_token (parser->lexer);
15543                   return;
15544                 }
15545
15546               if (declares_class_or_enum & 2)
15547                 cp_parser_check_for_definition_in_return_type
15548                                             (declarator, decl_specifiers.type,
15549                                              decl_specifiers.type_location);
15550
15551               /* Look for an asm-specification.  */
15552               asm_specification = cp_parser_asm_specification_opt (parser);
15553               /* Look for attributes that apply to the declaration.  */
15554               attributes = cp_parser_attributes_opt (parser);
15555               /* Remember which attributes are prefix attributes and
15556                  which are not.  */
15557               first_attribute = attributes;
15558               /* Combine the attributes.  */
15559               attributes = chainon (prefix_attributes, attributes);
15560
15561               /* If it's an `=', then we have a constant-initializer or a
15562                  pure-specifier.  It is not correct to parse the
15563                  initializer before registering the member declaration
15564                  since the member declaration should be in scope while
15565                  its initializer is processed.  However, the rest of the
15566                  front end does not yet provide an interface that allows
15567                  us to handle this correctly.  */
15568               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15569                 {
15570                   /* In [class.mem]:
15571
15572                      A pure-specifier shall be used only in the declaration of
15573                      a virtual function.
15574
15575                      A member-declarator can contain a constant-initializer
15576                      only if it declares a static member of integral or
15577                      enumeration type.
15578
15579                      Therefore, if the DECLARATOR is for a function, we look
15580                      for a pure-specifier; otherwise, we look for a
15581                      constant-initializer.  When we call `grokfield', it will
15582                      perform more stringent semantics checks.  */
15583                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15584                   if (function_declarator_p (declarator))
15585                     initializer = cp_parser_pure_specifier (parser);
15586                   else
15587                     /* Parse the initializer.  */
15588                     initializer = cp_parser_constant_initializer (parser);
15589                 }
15590               /* Otherwise, there is no initializer.  */
15591               else
15592                 initializer = NULL_TREE;
15593
15594               /* See if we are probably looking at a function
15595                  definition.  We are certainly not looking at a
15596                  member-declarator.  Calling `grokfield' has
15597                  side-effects, so we must not do it unless we are sure
15598                  that we are looking at a member-declarator.  */
15599               if (cp_parser_token_starts_function_definition_p
15600                   (cp_lexer_peek_token (parser->lexer)))
15601                 {
15602                   /* The grammar does not allow a pure-specifier to be
15603                      used when a member function is defined.  (It is
15604                      possible that this fact is an oversight in the
15605                      standard, since a pure function may be defined
15606                      outside of the class-specifier.  */
15607                   if (initializer)
15608                     error ("%Hpure-specifier on function-definition",
15609                            &initializer_token_start->location);
15610                   decl = cp_parser_save_member_function_body (parser,
15611                                                               &decl_specifiers,
15612                                                               declarator,
15613                                                               attributes);
15614                   /* If the member was not a friend, declare it here.  */
15615                   if (!friend_p)
15616                     finish_member_declaration (decl);
15617                   /* Peek at the next token.  */
15618                   token = cp_lexer_peek_token (parser->lexer);
15619                   /* If the next token is a semicolon, consume it.  */
15620                   if (token->type == CPP_SEMICOLON)
15621                     cp_lexer_consume_token (parser->lexer);
15622                   return;
15623                 }
15624               else
15625                 /* Create the declaration.  */
15626                 decl = grokfield (declarator, &decl_specifiers,
15627                                   initializer, /*init_const_expr_p=*/true,
15628                                   asm_specification,
15629                                   attributes);
15630             }
15631
15632           /* Reset PREFIX_ATTRIBUTES.  */
15633           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15634             attributes = TREE_CHAIN (attributes);
15635           if (attributes)
15636             TREE_CHAIN (attributes) = NULL_TREE;
15637
15638           /* If there is any qualification still in effect, clear it
15639              now; we will be starting fresh with the next declarator.  */
15640           parser->scope = NULL_TREE;
15641           parser->qualifying_scope = NULL_TREE;
15642           parser->object_scope = NULL_TREE;
15643           /* If it's a `,', then there are more declarators.  */
15644           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15645             cp_lexer_consume_token (parser->lexer);
15646           /* If the next token isn't a `;', then we have a parse error.  */
15647           else if (cp_lexer_next_token_is_not (parser->lexer,
15648                                                CPP_SEMICOLON))
15649             {
15650               cp_parser_error (parser, "expected %<;%>");
15651               /* Skip tokens until we find a `;'.  */
15652               cp_parser_skip_to_end_of_statement (parser);
15653
15654               break;
15655             }
15656
15657           if (decl)
15658             {
15659               /* Add DECL to the list of members.  */
15660               if (!friend_p)
15661                 finish_member_declaration (decl);
15662
15663               if (TREE_CODE (decl) == FUNCTION_DECL)
15664                 cp_parser_save_default_args (parser, decl);
15665             }
15666         }
15667     }
15668
15669   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15670 }
15671
15672 /* Parse a pure-specifier.
15673
15674    pure-specifier:
15675      = 0
15676
15677    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15678    Otherwise, ERROR_MARK_NODE is returned.  */
15679
15680 static tree
15681 cp_parser_pure_specifier (cp_parser* parser)
15682 {
15683   cp_token *token;
15684
15685   /* Look for the `=' token.  */
15686   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15687     return error_mark_node;
15688   /* Look for the `0' token.  */
15689   token = cp_lexer_consume_token (parser->lexer);
15690   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15691   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15692     {
15693       cp_parser_error (parser,
15694                        "invalid pure specifier (only %<= 0%> is allowed)");
15695       cp_parser_skip_to_end_of_statement (parser);
15696       return error_mark_node;
15697     }
15698   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15699     {
15700       error ("%Htemplates may not be %<virtual%>", &token->location);
15701       return error_mark_node;
15702     }
15703
15704   return integer_zero_node;
15705 }
15706
15707 /* Parse a constant-initializer.
15708
15709    constant-initializer:
15710      = constant-expression
15711
15712    Returns a representation of the constant-expression.  */
15713
15714 static tree
15715 cp_parser_constant_initializer (cp_parser* parser)
15716 {
15717   /* Look for the `=' token.  */
15718   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15719     return error_mark_node;
15720
15721   /* It is invalid to write:
15722
15723        struct S { static const int i = { 7 }; };
15724
15725      */
15726   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15727     {
15728       cp_parser_error (parser,
15729                        "a brace-enclosed initializer is not allowed here");
15730       /* Consume the opening brace.  */
15731       cp_lexer_consume_token (parser->lexer);
15732       /* Skip the initializer.  */
15733       cp_parser_skip_to_closing_brace (parser);
15734       /* Look for the trailing `}'.  */
15735       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15736
15737       return error_mark_node;
15738     }
15739
15740   return cp_parser_constant_expression (parser,
15741                                         /*allow_non_constant=*/false,
15742                                         NULL);
15743 }
15744
15745 /* Derived classes [gram.class.derived] */
15746
15747 /* Parse a base-clause.
15748
15749    base-clause:
15750      : base-specifier-list
15751
15752    base-specifier-list:
15753      base-specifier ... [opt]
15754      base-specifier-list , base-specifier ... [opt]
15755
15756    Returns a TREE_LIST representing the base-classes, in the order in
15757    which they were declared.  The representation of each node is as
15758    described by cp_parser_base_specifier.
15759
15760    In the case that no bases are specified, this function will return
15761    NULL_TREE, not ERROR_MARK_NODE.  */
15762
15763 static tree
15764 cp_parser_base_clause (cp_parser* parser)
15765 {
15766   tree bases = NULL_TREE;
15767
15768   /* Look for the `:' that begins the list.  */
15769   cp_parser_require (parser, CPP_COLON, "%<:%>");
15770
15771   /* Scan the base-specifier-list.  */
15772   while (true)
15773     {
15774       cp_token *token;
15775       tree base;
15776       bool pack_expansion_p = false;
15777
15778       /* Look for the base-specifier.  */
15779       base = cp_parser_base_specifier (parser);
15780       /* Look for the (optional) ellipsis. */
15781       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15782         {
15783           /* Consume the `...'. */
15784           cp_lexer_consume_token (parser->lexer);
15785
15786           pack_expansion_p = true;
15787         }
15788
15789       /* Add BASE to the front of the list.  */
15790       if (base != error_mark_node)
15791         {
15792           if (pack_expansion_p)
15793             /* Make this a pack expansion type. */
15794             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15795           
15796
15797           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15798             {
15799               TREE_CHAIN (base) = bases;
15800               bases = base;
15801             }
15802         }
15803       /* Peek at the next token.  */
15804       token = cp_lexer_peek_token (parser->lexer);
15805       /* If it's not a comma, then the list is complete.  */
15806       if (token->type != CPP_COMMA)
15807         break;
15808       /* Consume the `,'.  */
15809       cp_lexer_consume_token (parser->lexer);
15810     }
15811
15812   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15813      base class had a qualified name.  However, the next name that
15814      appears is certainly not qualified.  */
15815   parser->scope = NULL_TREE;
15816   parser->qualifying_scope = NULL_TREE;
15817   parser->object_scope = NULL_TREE;
15818
15819   return nreverse (bases);
15820 }
15821
15822 /* Parse a base-specifier.
15823
15824    base-specifier:
15825      :: [opt] nested-name-specifier [opt] class-name
15826      virtual access-specifier [opt] :: [opt] nested-name-specifier
15827        [opt] class-name
15828      access-specifier virtual [opt] :: [opt] nested-name-specifier
15829        [opt] class-name
15830
15831    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15832    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15833    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15834    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15835
15836 static tree
15837 cp_parser_base_specifier (cp_parser* parser)
15838 {
15839   cp_token *token;
15840   bool done = false;
15841   bool virtual_p = false;
15842   bool duplicate_virtual_error_issued_p = false;
15843   bool duplicate_access_error_issued_p = false;
15844   bool class_scope_p, template_p;
15845   tree access = access_default_node;
15846   tree type;
15847
15848   /* Process the optional `virtual' and `access-specifier'.  */
15849   while (!done)
15850     {
15851       /* Peek at the next token.  */
15852       token = cp_lexer_peek_token (parser->lexer);
15853       /* Process `virtual'.  */
15854       switch (token->keyword)
15855         {
15856         case RID_VIRTUAL:
15857           /* If `virtual' appears more than once, issue an error.  */
15858           if (virtual_p && !duplicate_virtual_error_issued_p)
15859             {
15860               cp_parser_error (parser,
15861                                "%<virtual%> specified more than once in base-specified");
15862               duplicate_virtual_error_issued_p = true;
15863             }
15864
15865           virtual_p = true;
15866
15867           /* Consume the `virtual' token.  */
15868           cp_lexer_consume_token (parser->lexer);
15869
15870           break;
15871
15872         case RID_PUBLIC:
15873         case RID_PROTECTED:
15874         case RID_PRIVATE:
15875           /* If more than one access specifier appears, issue an
15876              error.  */
15877           if (access != access_default_node
15878               && !duplicate_access_error_issued_p)
15879             {
15880               cp_parser_error (parser,
15881                                "more than one access specifier in base-specified");
15882               duplicate_access_error_issued_p = true;
15883             }
15884
15885           access = ridpointers[(int) token->keyword];
15886
15887           /* Consume the access-specifier.  */
15888           cp_lexer_consume_token (parser->lexer);
15889
15890           break;
15891
15892         default:
15893           done = true;
15894           break;
15895         }
15896     }
15897   /* It is not uncommon to see programs mechanically, erroneously, use
15898      the 'typename' keyword to denote (dependent) qualified types
15899      as base classes.  */
15900   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15901     {
15902       token = cp_lexer_peek_token (parser->lexer);
15903       if (!processing_template_decl)
15904         error ("%Hkeyword %<typename%> not allowed outside of templates",
15905                &token->location);
15906       else
15907         error ("%Hkeyword %<typename%> not allowed in this context "
15908                "(the base class is implicitly a type)",
15909                &token->location);
15910       cp_lexer_consume_token (parser->lexer);
15911     }
15912
15913   /* Look for the optional `::' operator.  */
15914   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15915   /* Look for the nested-name-specifier.  The simplest way to
15916      implement:
15917
15918        [temp.res]
15919
15920        The keyword `typename' is not permitted in a base-specifier or
15921        mem-initializer; in these contexts a qualified name that
15922        depends on a template-parameter is implicitly assumed to be a
15923        type name.
15924
15925      is to pretend that we have seen the `typename' keyword at this
15926      point.  */
15927   cp_parser_nested_name_specifier_opt (parser,
15928                                        /*typename_keyword_p=*/true,
15929                                        /*check_dependency_p=*/true,
15930                                        typename_type,
15931                                        /*is_declaration=*/true);
15932   /* If the base class is given by a qualified name, assume that names
15933      we see are type names or templates, as appropriate.  */
15934   class_scope_p = (parser->scope && TYPE_P (parser->scope));
15935   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15936
15937   /* Finally, look for the class-name.  */
15938   type = cp_parser_class_name (parser,
15939                                class_scope_p,
15940                                template_p,
15941                                typename_type,
15942                                /*check_dependency_p=*/true,
15943                                /*class_head_p=*/false,
15944                                /*is_declaration=*/true);
15945
15946   if (type == error_mark_node)
15947     return error_mark_node;
15948
15949   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15950 }
15951
15952 /* Exception handling [gram.exception] */
15953
15954 /* Parse an (optional) exception-specification.
15955
15956    exception-specification:
15957      throw ( type-id-list [opt] )
15958
15959    Returns a TREE_LIST representing the exception-specification.  The
15960    TREE_VALUE of each node is a type.  */
15961
15962 static tree
15963 cp_parser_exception_specification_opt (cp_parser* parser)
15964 {
15965   cp_token *token;
15966   tree type_id_list;
15967
15968   /* Peek at the next token.  */
15969   token = cp_lexer_peek_token (parser->lexer);
15970   /* If it's not `throw', then there's no exception-specification.  */
15971   if (!cp_parser_is_keyword (token, RID_THROW))
15972     return NULL_TREE;
15973
15974   /* Consume the `throw'.  */
15975   cp_lexer_consume_token (parser->lexer);
15976
15977   /* Look for the `('.  */
15978   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15979
15980   /* Peek at the next token.  */
15981   token = cp_lexer_peek_token (parser->lexer);
15982   /* If it's not a `)', then there is a type-id-list.  */
15983   if (token->type != CPP_CLOSE_PAREN)
15984     {
15985       const char *saved_message;
15986
15987       /* Types may not be defined in an exception-specification.  */
15988       saved_message = parser->type_definition_forbidden_message;
15989       parser->type_definition_forbidden_message
15990         = "types may not be defined in an exception-specification";
15991       /* Parse the type-id-list.  */
15992       type_id_list = cp_parser_type_id_list (parser);
15993       /* Restore the saved message.  */
15994       parser->type_definition_forbidden_message = saved_message;
15995     }
15996   else
15997     type_id_list = empty_except_spec;
15998
15999   /* Look for the `)'.  */
16000   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16001
16002   return type_id_list;
16003 }
16004
16005 /* Parse an (optional) type-id-list.
16006
16007    type-id-list:
16008      type-id ... [opt]
16009      type-id-list , type-id ... [opt]
16010
16011    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16012    in the order that the types were presented.  */
16013
16014 static tree
16015 cp_parser_type_id_list (cp_parser* parser)
16016 {
16017   tree types = NULL_TREE;
16018
16019   while (true)
16020     {
16021       cp_token *token;
16022       tree type;
16023
16024       /* Get the next type-id.  */
16025       type = cp_parser_type_id (parser);
16026       /* Parse the optional ellipsis. */
16027       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16028         {
16029           /* Consume the `...'. */
16030           cp_lexer_consume_token (parser->lexer);
16031
16032           /* Turn the type into a pack expansion expression. */
16033           type = make_pack_expansion (type);
16034         }
16035       /* Add it to the list.  */
16036       types = add_exception_specifier (types, type, /*complain=*/1);
16037       /* Peek at the next token.  */
16038       token = cp_lexer_peek_token (parser->lexer);
16039       /* If it is not a `,', we are done.  */
16040       if (token->type != CPP_COMMA)
16041         break;
16042       /* Consume the `,'.  */
16043       cp_lexer_consume_token (parser->lexer);
16044     }
16045
16046   return nreverse (types);
16047 }
16048
16049 /* Parse a try-block.
16050
16051    try-block:
16052      try compound-statement handler-seq  */
16053
16054 static tree
16055 cp_parser_try_block (cp_parser* parser)
16056 {
16057   tree try_block;
16058
16059   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16060   try_block = begin_try_block ();
16061   cp_parser_compound_statement (parser, NULL, true);
16062   finish_try_block (try_block);
16063   cp_parser_handler_seq (parser);
16064   finish_handler_sequence (try_block);
16065
16066   return try_block;
16067 }
16068
16069 /* Parse a function-try-block.
16070
16071    function-try-block:
16072      try ctor-initializer [opt] function-body handler-seq  */
16073
16074 static bool
16075 cp_parser_function_try_block (cp_parser* parser)
16076 {
16077   tree compound_stmt;
16078   tree try_block;
16079   bool ctor_initializer_p;
16080
16081   /* Look for the `try' keyword.  */
16082   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16083     return false;
16084   /* Let the rest of the front end know where we are.  */
16085   try_block = begin_function_try_block (&compound_stmt);
16086   /* Parse the function-body.  */
16087   ctor_initializer_p
16088     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16089   /* We're done with the `try' part.  */
16090   finish_function_try_block (try_block);
16091   /* Parse the handlers.  */
16092   cp_parser_handler_seq (parser);
16093   /* We're done with the handlers.  */
16094   finish_function_handler_sequence (try_block, compound_stmt);
16095
16096   return ctor_initializer_p;
16097 }
16098
16099 /* Parse a handler-seq.
16100
16101    handler-seq:
16102      handler handler-seq [opt]  */
16103
16104 static void
16105 cp_parser_handler_seq (cp_parser* parser)
16106 {
16107   while (true)
16108     {
16109       cp_token *token;
16110
16111       /* Parse the handler.  */
16112       cp_parser_handler (parser);
16113       /* Peek at the next token.  */
16114       token = cp_lexer_peek_token (parser->lexer);
16115       /* If it's not `catch' then there are no more handlers.  */
16116       if (!cp_parser_is_keyword (token, RID_CATCH))
16117         break;
16118     }
16119 }
16120
16121 /* Parse a handler.
16122
16123    handler:
16124      catch ( exception-declaration ) compound-statement  */
16125
16126 static void
16127 cp_parser_handler (cp_parser* parser)
16128 {
16129   tree handler;
16130   tree declaration;
16131
16132   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16133   handler = begin_handler ();
16134   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16135   declaration = cp_parser_exception_declaration (parser);
16136   finish_handler_parms (declaration, handler);
16137   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16138   cp_parser_compound_statement (parser, NULL, false);
16139   finish_handler (handler);
16140 }
16141
16142 /* Parse an exception-declaration.
16143
16144    exception-declaration:
16145      type-specifier-seq declarator
16146      type-specifier-seq abstract-declarator
16147      type-specifier-seq
16148      ...
16149
16150    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16151    ellipsis variant is used.  */
16152
16153 static tree
16154 cp_parser_exception_declaration (cp_parser* parser)
16155 {
16156   cp_decl_specifier_seq type_specifiers;
16157   cp_declarator *declarator;
16158   const char *saved_message;
16159
16160   /* If it's an ellipsis, it's easy to handle.  */
16161   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16162     {
16163       /* Consume the `...' token.  */
16164       cp_lexer_consume_token (parser->lexer);
16165       return NULL_TREE;
16166     }
16167
16168   /* Types may not be defined in exception-declarations.  */
16169   saved_message = parser->type_definition_forbidden_message;
16170   parser->type_definition_forbidden_message
16171     = "types may not be defined in exception-declarations";
16172
16173   /* Parse the type-specifier-seq.  */
16174   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16175                                 &type_specifiers);
16176   /* If it's a `)', then there is no declarator.  */
16177   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16178     declarator = NULL;
16179   else
16180     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16181                                        /*ctor_dtor_or_conv_p=*/NULL,
16182                                        /*parenthesized_p=*/NULL,
16183                                        /*member_p=*/false);
16184
16185   /* Restore the saved message.  */
16186   parser->type_definition_forbidden_message = saved_message;
16187
16188   if (!type_specifiers.any_specifiers_p)
16189     return error_mark_node;
16190
16191   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16192 }
16193
16194 /* Parse a throw-expression.
16195
16196    throw-expression:
16197      throw assignment-expression [opt]
16198
16199    Returns a THROW_EXPR representing the throw-expression.  */
16200
16201 static tree
16202 cp_parser_throw_expression (cp_parser* parser)
16203 {
16204   tree expression;
16205   cp_token* token;
16206
16207   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16208   token = cp_lexer_peek_token (parser->lexer);
16209   /* Figure out whether or not there is an assignment-expression
16210      following the "throw" keyword.  */
16211   if (token->type == CPP_COMMA
16212       || token->type == CPP_SEMICOLON
16213       || token->type == CPP_CLOSE_PAREN
16214       || token->type == CPP_CLOSE_SQUARE
16215       || token->type == CPP_CLOSE_BRACE
16216       || token->type == CPP_COLON)
16217     expression = NULL_TREE;
16218   else
16219     expression = cp_parser_assignment_expression (parser,
16220                                                   /*cast_p=*/false);
16221
16222   return build_throw (expression);
16223 }
16224
16225 /* GNU Extensions */
16226
16227 /* Parse an (optional) asm-specification.
16228
16229    asm-specification:
16230      asm ( string-literal )
16231
16232    If the asm-specification is present, returns a STRING_CST
16233    corresponding to the string-literal.  Otherwise, returns
16234    NULL_TREE.  */
16235
16236 static tree
16237 cp_parser_asm_specification_opt (cp_parser* parser)
16238 {
16239   cp_token *token;
16240   tree asm_specification;
16241
16242   /* Peek at the next token.  */
16243   token = cp_lexer_peek_token (parser->lexer);
16244   /* If the next token isn't the `asm' keyword, then there's no
16245      asm-specification.  */
16246   if (!cp_parser_is_keyword (token, RID_ASM))
16247     return NULL_TREE;
16248
16249   /* Consume the `asm' token.  */
16250   cp_lexer_consume_token (parser->lexer);
16251   /* Look for the `('.  */
16252   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16253
16254   /* Look for the string-literal.  */
16255   asm_specification = cp_parser_string_literal (parser, false, false);
16256
16257   /* Look for the `)'.  */
16258   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16259
16260   return asm_specification;
16261 }
16262
16263 /* Parse an asm-operand-list.
16264
16265    asm-operand-list:
16266      asm-operand
16267      asm-operand-list , asm-operand
16268
16269    asm-operand:
16270      string-literal ( expression )
16271      [ string-literal ] string-literal ( expression )
16272
16273    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16274    each node is the expression.  The TREE_PURPOSE is itself a
16275    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16276    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16277    is a STRING_CST for the string literal before the parenthesis. Returns
16278    ERROR_MARK_NODE if any of the operands are invalid.  */
16279
16280 static tree
16281 cp_parser_asm_operand_list (cp_parser* parser)
16282 {
16283   tree asm_operands = NULL_TREE;
16284   bool invalid_operands = false;
16285
16286   while (true)
16287     {
16288       tree string_literal;
16289       tree expression;
16290       tree name;
16291
16292       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16293         {
16294           /* Consume the `[' token.  */
16295           cp_lexer_consume_token (parser->lexer);
16296           /* Read the operand name.  */
16297           name = cp_parser_identifier (parser);
16298           if (name != error_mark_node)
16299             name = build_string (IDENTIFIER_LENGTH (name),
16300                                  IDENTIFIER_POINTER (name));
16301           /* Look for the closing `]'.  */
16302           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16303         }
16304       else
16305         name = NULL_TREE;
16306       /* Look for the string-literal.  */
16307       string_literal = cp_parser_string_literal (parser, false, false);
16308
16309       /* Look for the `('.  */
16310       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16311       /* Parse the expression.  */
16312       expression = cp_parser_expression (parser, /*cast_p=*/false);
16313       /* Look for the `)'.  */
16314       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16315
16316       if (name == error_mark_node 
16317           || string_literal == error_mark_node 
16318           || expression == error_mark_node)
16319         invalid_operands = true;
16320
16321       /* Add this operand to the list.  */
16322       asm_operands = tree_cons (build_tree_list (name, string_literal),
16323                                 expression,
16324                                 asm_operands);
16325       /* If the next token is not a `,', there are no more
16326          operands.  */
16327       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16328         break;
16329       /* Consume the `,'.  */
16330       cp_lexer_consume_token (parser->lexer);
16331     }
16332
16333   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16334 }
16335
16336 /* Parse an asm-clobber-list.
16337
16338    asm-clobber-list:
16339      string-literal
16340      asm-clobber-list , string-literal
16341
16342    Returns a TREE_LIST, indicating the clobbers in the order that they
16343    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16344
16345 static tree
16346 cp_parser_asm_clobber_list (cp_parser* parser)
16347 {
16348   tree clobbers = NULL_TREE;
16349
16350   while (true)
16351     {
16352       tree string_literal;
16353
16354       /* Look for the string literal.  */
16355       string_literal = cp_parser_string_literal (parser, false, false);
16356       /* Add it to the list.  */
16357       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16358       /* If the next token is not a `,', then the list is
16359          complete.  */
16360       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16361         break;
16362       /* Consume the `,' token.  */
16363       cp_lexer_consume_token (parser->lexer);
16364     }
16365
16366   return clobbers;
16367 }
16368
16369 /* Parse an (optional) series of attributes.
16370
16371    attributes:
16372      attributes attribute
16373
16374    attribute:
16375      __attribute__ (( attribute-list [opt] ))
16376
16377    The return value is as for cp_parser_attribute_list.  */
16378
16379 static tree
16380 cp_parser_attributes_opt (cp_parser* parser)
16381 {
16382   tree attributes = NULL_TREE;
16383
16384   while (true)
16385     {
16386       cp_token *token;
16387       tree attribute_list;
16388
16389       /* Peek at the next token.  */
16390       token = cp_lexer_peek_token (parser->lexer);
16391       /* If it's not `__attribute__', then we're done.  */
16392       if (token->keyword != RID_ATTRIBUTE)
16393         break;
16394
16395       /* Consume the `__attribute__' keyword.  */
16396       cp_lexer_consume_token (parser->lexer);
16397       /* Look for the two `(' tokens.  */
16398       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16399       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16400
16401       /* Peek at the next token.  */
16402       token = cp_lexer_peek_token (parser->lexer);
16403       if (token->type != CPP_CLOSE_PAREN)
16404         /* Parse the attribute-list.  */
16405         attribute_list = cp_parser_attribute_list (parser);
16406       else
16407         /* If the next token is a `)', then there is no attribute
16408            list.  */
16409         attribute_list = NULL;
16410
16411       /* Look for the two `)' tokens.  */
16412       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16413       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16414
16415       /* Add these new attributes to the list.  */
16416       attributes = chainon (attributes, attribute_list);
16417     }
16418
16419   return attributes;
16420 }
16421
16422 /* Parse an attribute-list.
16423
16424    attribute-list:
16425      attribute
16426      attribute-list , attribute
16427
16428    attribute:
16429      identifier
16430      identifier ( identifier )
16431      identifier ( identifier , expression-list )
16432      identifier ( expression-list )
16433
16434    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16435    to an attribute.  The TREE_PURPOSE of each node is the identifier
16436    indicating which attribute is in use.  The TREE_VALUE represents
16437    the arguments, if any.  */
16438
16439 static tree
16440 cp_parser_attribute_list (cp_parser* parser)
16441 {
16442   tree attribute_list = NULL_TREE;
16443   bool save_translate_strings_p = parser->translate_strings_p;
16444
16445   parser->translate_strings_p = false;
16446   while (true)
16447     {
16448       cp_token *token;
16449       tree identifier;
16450       tree attribute;
16451
16452       /* Look for the identifier.  We also allow keywords here; for
16453          example `__attribute__ ((const))' is legal.  */
16454       token = cp_lexer_peek_token (parser->lexer);
16455       if (token->type == CPP_NAME
16456           || token->type == CPP_KEYWORD)
16457         {
16458           tree arguments = NULL_TREE;
16459
16460           /* Consume the token.  */
16461           token = cp_lexer_consume_token (parser->lexer);
16462
16463           /* Save away the identifier that indicates which attribute
16464              this is.  */
16465           identifier = token->u.value;
16466           attribute = build_tree_list (identifier, NULL_TREE);
16467
16468           /* Peek at the next token.  */
16469           token = cp_lexer_peek_token (parser->lexer);
16470           /* If it's an `(', then parse the attribute arguments.  */
16471           if (token->type == CPP_OPEN_PAREN)
16472             {
16473               arguments = cp_parser_parenthesized_expression_list
16474                           (parser, true, /*cast_p=*/false,
16475                            /*allow_expansion_p=*/false,
16476                            /*non_constant_p=*/NULL);
16477               /* Save the arguments away.  */
16478               TREE_VALUE (attribute) = arguments;
16479             }
16480
16481           if (arguments != error_mark_node)
16482             {
16483               /* Add this attribute to the list.  */
16484               TREE_CHAIN (attribute) = attribute_list;
16485               attribute_list = attribute;
16486             }
16487
16488           token = cp_lexer_peek_token (parser->lexer);
16489         }
16490       /* Now, look for more attributes.  If the next token isn't a
16491          `,', we're done.  */
16492       if (token->type != CPP_COMMA)
16493         break;
16494
16495       /* Consume the comma and keep going.  */
16496       cp_lexer_consume_token (parser->lexer);
16497     }
16498   parser->translate_strings_p = save_translate_strings_p;
16499
16500   /* We built up the list in reverse order.  */
16501   return nreverse (attribute_list);
16502 }
16503
16504 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16505    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16506    current value of the PEDANTIC flag, regardless of whether or not
16507    the `__extension__' keyword is present.  The caller is responsible
16508    for restoring the value of the PEDANTIC flag.  */
16509
16510 static bool
16511 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16512 {
16513   /* Save the old value of the PEDANTIC flag.  */
16514   *saved_pedantic = pedantic;
16515
16516   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16517     {
16518       /* Consume the `__extension__' token.  */
16519       cp_lexer_consume_token (parser->lexer);
16520       /* We're not being pedantic while the `__extension__' keyword is
16521          in effect.  */
16522       pedantic = 0;
16523
16524       return true;
16525     }
16526
16527   return false;
16528 }
16529
16530 /* Parse a label declaration.
16531
16532    label-declaration:
16533      __label__ label-declarator-seq ;
16534
16535    label-declarator-seq:
16536      identifier , label-declarator-seq
16537      identifier  */
16538
16539 static void
16540 cp_parser_label_declaration (cp_parser* parser)
16541 {
16542   /* Look for the `__label__' keyword.  */
16543   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16544
16545   while (true)
16546     {
16547       tree identifier;
16548
16549       /* Look for an identifier.  */
16550       identifier = cp_parser_identifier (parser);
16551       /* If we failed, stop.  */
16552       if (identifier == error_mark_node)
16553         break;
16554       /* Declare it as a label.  */
16555       finish_label_decl (identifier);
16556       /* If the next token is a `;', stop.  */
16557       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16558         break;
16559       /* Look for the `,' separating the label declarations.  */
16560       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16561     }
16562
16563   /* Look for the final `;'.  */
16564   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16565 }
16566
16567 /* Support Functions */
16568
16569 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16570    NAME should have one of the representations used for an
16571    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16572    is returned.  If PARSER->SCOPE is a dependent type, then a
16573    SCOPE_REF is returned.
16574
16575    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16576    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16577    was formed.  Abstractly, such entities should not be passed to this
16578    function, because they do not need to be looked up, but it is
16579    simpler to check for this special case here, rather than at the
16580    call-sites.
16581
16582    In cases not explicitly covered above, this function returns a
16583    DECL, OVERLOAD, or baselink representing the result of the lookup.
16584    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16585    is returned.
16586
16587    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16588    (e.g., "struct") that was used.  In that case bindings that do not
16589    refer to types are ignored.
16590
16591    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16592    ignored.
16593
16594    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16595    are ignored.
16596
16597    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16598    types.
16599
16600    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16601    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16602    NULL_TREE otherwise.  */
16603
16604 static tree
16605 cp_parser_lookup_name (cp_parser *parser, tree name,
16606                        enum tag_types tag_type,
16607                        bool is_template,
16608                        bool is_namespace,
16609                        bool check_dependency,
16610                        tree *ambiguous_decls,
16611                        location_t name_location)
16612 {
16613   int flags = 0;
16614   tree decl;
16615   tree object_type = parser->context->object_type;
16616
16617   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16618     flags |= LOOKUP_COMPLAIN;
16619
16620   /* Assume that the lookup will be unambiguous.  */
16621   if (ambiguous_decls)
16622     *ambiguous_decls = NULL_TREE;
16623
16624   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16625      no longer valid.  Note that if we are parsing tentatively, and
16626      the parse fails, OBJECT_TYPE will be automatically restored.  */
16627   parser->context->object_type = NULL_TREE;
16628
16629   if (name == error_mark_node)
16630     return error_mark_node;
16631
16632   /* A template-id has already been resolved; there is no lookup to
16633      do.  */
16634   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16635     return name;
16636   if (BASELINK_P (name))
16637     {
16638       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16639                   == TEMPLATE_ID_EXPR);
16640       return name;
16641     }
16642
16643   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16644      it should already have been checked to make sure that the name
16645      used matches the type being destroyed.  */
16646   if (TREE_CODE (name) == BIT_NOT_EXPR)
16647     {
16648       tree type;
16649
16650       /* Figure out to which type this destructor applies.  */
16651       if (parser->scope)
16652         type = parser->scope;
16653       else if (object_type)
16654         type = object_type;
16655       else
16656         type = current_class_type;
16657       /* If that's not a class type, there is no destructor.  */
16658       if (!type || !CLASS_TYPE_P (type))
16659         return error_mark_node;
16660       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16661         lazily_declare_fn (sfk_destructor, type);
16662       if (!CLASSTYPE_DESTRUCTORS (type))
16663           return error_mark_node;
16664       /* If it was a class type, return the destructor.  */
16665       return CLASSTYPE_DESTRUCTORS (type);
16666     }
16667
16668   /* By this point, the NAME should be an ordinary identifier.  If
16669      the id-expression was a qualified name, the qualifying scope is
16670      stored in PARSER->SCOPE at this point.  */
16671   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16672
16673   /* Perform the lookup.  */
16674   if (parser->scope)
16675     {
16676       bool dependent_p;
16677
16678       if (parser->scope == error_mark_node)
16679         return error_mark_node;
16680
16681       /* If the SCOPE is dependent, the lookup must be deferred until
16682          the template is instantiated -- unless we are explicitly
16683          looking up names in uninstantiated templates.  Even then, we
16684          cannot look up the name if the scope is not a class type; it
16685          might, for example, be a template type parameter.  */
16686       dependent_p = (TYPE_P (parser->scope)
16687                      && !(parser->in_declarator_p
16688                           && currently_open_class (parser->scope))
16689                      && dependent_type_p (parser->scope));
16690       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16691            && dependent_p)
16692         {
16693           if (tag_type)
16694             {
16695               tree type;
16696
16697               /* The resolution to Core Issue 180 says that `struct
16698                  A::B' should be considered a type-name, even if `A'
16699                  is dependent.  */
16700               type = make_typename_type (parser->scope, name, tag_type,
16701                                          /*complain=*/tf_error);
16702               decl = TYPE_NAME (type);
16703             }
16704           else if (is_template
16705                    && (cp_parser_next_token_ends_template_argument_p (parser)
16706                        || cp_lexer_next_token_is (parser->lexer,
16707                                                   CPP_CLOSE_PAREN)))
16708             decl = make_unbound_class_template (parser->scope,
16709                                                 name, NULL_TREE,
16710                                                 /*complain=*/tf_error);
16711           else
16712             decl = build_qualified_name (/*type=*/NULL_TREE,
16713                                          parser->scope, name,
16714                                          is_template);
16715         }
16716       else
16717         {
16718           tree pushed_scope = NULL_TREE;
16719
16720           /* If PARSER->SCOPE is a dependent type, then it must be a
16721              class type, and we must not be checking dependencies;
16722              otherwise, we would have processed this lookup above.  So
16723              that PARSER->SCOPE is not considered a dependent base by
16724              lookup_member, we must enter the scope here.  */
16725           if (dependent_p)
16726             pushed_scope = push_scope (parser->scope);
16727           /* If the PARSER->SCOPE is a template specialization, it
16728              may be instantiated during name lookup.  In that case,
16729              errors may be issued.  Even if we rollback the current
16730              tentative parse, those errors are valid.  */
16731           decl = lookup_qualified_name (parser->scope, name,
16732                                         tag_type != none_type,
16733                                         /*complain=*/true);
16734
16735           /* If we have a single function from a using decl, pull it out.  */
16736           if (decl
16737               && TREE_CODE (decl) == OVERLOAD
16738               && !really_overloaded_fn (decl))
16739             decl = OVL_FUNCTION (decl);
16740
16741           if (pushed_scope)
16742             pop_scope (pushed_scope);
16743         }
16744       parser->qualifying_scope = parser->scope;
16745       parser->object_scope = NULL_TREE;
16746     }
16747   else if (object_type)
16748     {
16749       tree object_decl = NULL_TREE;
16750       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16751          OBJECT_TYPE is not a class.  */
16752       if (CLASS_TYPE_P (object_type))
16753         /* If the OBJECT_TYPE is a template specialization, it may
16754            be instantiated during name lookup.  In that case, errors
16755            may be issued.  Even if we rollback the current tentative
16756            parse, those errors are valid.  */
16757         object_decl = lookup_member (object_type,
16758                                      name,
16759                                      /*protect=*/0,
16760                                      tag_type != none_type);
16761       /* Look it up in the enclosing context, too.  */
16762       decl = lookup_name_real (name, tag_type != none_type,
16763                                /*nonclass=*/0,
16764                                /*block_p=*/true, is_namespace, flags);
16765       parser->object_scope = object_type;
16766       parser->qualifying_scope = NULL_TREE;
16767       if (object_decl)
16768         decl = object_decl;
16769     }
16770   else
16771     {
16772       decl = lookup_name_real (name, tag_type != none_type,
16773                                /*nonclass=*/0,
16774                                /*block_p=*/true, is_namespace, flags);
16775       parser->qualifying_scope = NULL_TREE;
16776       parser->object_scope = NULL_TREE;
16777     }
16778
16779   /* If the lookup failed, let our caller know.  */
16780   if (!decl || decl == error_mark_node)
16781     return error_mark_node;
16782
16783   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16784   if (TREE_CODE (decl) == TREE_LIST)
16785     {
16786       if (ambiguous_decls)
16787         *ambiguous_decls = decl;
16788       /* The error message we have to print is too complicated for
16789          cp_parser_error, so we incorporate its actions directly.  */
16790       if (!cp_parser_simulate_error (parser))
16791         {
16792           error ("%Hreference to %qD is ambiguous",
16793                  &name_location, name);
16794           print_candidates (decl);
16795         }
16796       return error_mark_node;
16797     }
16798
16799   gcc_assert (DECL_P (decl)
16800               || TREE_CODE (decl) == OVERLOAD
16801               || TREE_CODE (decl) == SCOPE_REF
16802               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16803               || BASELINK_P (decl));
16804
16805   /* If we have resolved the name of a member declaration, check to
16806      see if the declaration is accessible.  When the name resolves to
16807      set of overloaded functions, accessibility is checked when
16808      overload resolution is done.
16809
16810      During an explicit instantiation, access is not checked at all,
16811      as per [temp.explicit].  */
16812   if (DECL_P (decl))
16813     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16814
16815   return decl;
16816 }
16817
16818 /* Like cp_parser_lookup_name, but for use in the typical case where
16819    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16820    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16821
16822 static tree
16823 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
16824 {
16825   return cp_parser_lookup_name (parser, name,
16826                                 none_type,
16827                                 /*is_template=*/false,
16828                                 /*is_namespace=*/false,
16829                                 /*check_dependency=*/true,
16830                                 /*ambiguous_decls=*/NULL,
16831                                 location);
16832 }
16833
16834 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16835    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16836    true, the DECL indicates the class being defined in a class-head,
16837    or declared in an elaborated-type-specifier.
16838
16839    Otherwise, return DECL.  */
16840
16841 static tree
16842 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16843 {
16844   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16845      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16846
16847        struct A {
16848          template <typename T> struct B;
16849        };
16850
16851        template <typename T> struct A::B {};
16852
16853      Similarly, in an elaborated-type-specifier:
16854
16855        namespace N { struct X{}; }
16856
16857        struct A {
16858          template <typename T> friend struct N::X;
16859        };
16860
16861      However, if the DECL refers to a class type, and we are in
16862      the scope of the class, then the name lookup automatically
16863      finds the TYPE_DECL created by build_self_reference rather
16864      than a TEMPLATE_DECL.  For example, in:
16865
16866        template <class T> struct S {
16867          S s;
16868        };
16869
16870      there is no need to handle such case.  */
16871
16872   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16873     return DECL_TEMPLATE_RESULT (decl);
16874
16875   return decl;
16876 }
16877
16878 /* If too many, or too few, template-parameter lists apply to the
16879    declarator, issue an error message.  Returns TRUE if all went well,
16880    and FALSE otherwise.  */
16881
16882 static bool
16883 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16884                                                 cp_declarator *declarator,
16885                                                 location_t declarator_location)
16886 {
16887   unsigned num_templates;
16888
16889   /* We haven't seen any classes that involve template parameters yet.  */
16890   num_templates = 0;
16891
16892   switch (declarator->kind)
16893     {
16894     case cdk_id:
16895       if (declarator->u.id.qualifying_scope)
16896         {
16897           tree scope;
16898           tree member;
16899
16900           scope = declarator->u.id.qualifying_scope;
16901           member = declarator->u.id.unqualified_name;
16902
16903           while (scope && CLASS_TYPE_P (scope))
16904             {
16905               /* You're supposed to have one `template <...>'
16906                  for every template class, but you don't need one
16907                  for a full specialization.  For example:
16908
16909                  template <class T> struct S{};
16910                  template <> struct S<int> { void f(); };
16911                  void S<int>::f () {}
16912
16913                  is correct; there shouldn't be a `template <>' for
16914                  the definition of `S<int>::f'.  */
16915               if (!CLASSTYPE_TEMPLATE_INFO (scope))
16916                 /* If SCOPE does not have template information of any
16917                    kind, then it is not a template, nor is it nested
16918                    within a template.  */
16919                 break;
16920               if (explicit_class_specialization_p (scope))
16921                 break;
16922               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16923                 ++num_templates;
16924
16925               scope = TYPE_CONTEXT (scope);
16926             }
16927         }
16928       else if (TREE_CODE (declarator->u.id.unqualified_name)
16929                == TEMPLATE_ID_EXPR)
16930         /* If the DECLARATOR has the form `X<y>' then it uses one
16931            additional level of template parameters.  */
16932         ++num_templates;
16933
16934       return cp_parser_check_template_parameters (parser,
16935                                                   num_templates,
16936                                                   declarator_location);
16937
16938     case cdk_function:
16939     case cdk_array:
16940     case cdk_pointer:
16941     case cdk_reference:
16942     case cdk_ptrmem:
16943       return (cp_parser_check_declarator_template_parameters
16944               (parser, declarator->declarator, declarator_location));
16945
16946     case cdk_error:
16947       return true;
16948
16949     default:
16950       gcc_unreachable ();
16951     }
16952   return false;
16953 }
16954
16955 /* NUM_TEMPLATES were used in the current declaration.  If that is
16956    invalid, return FALSE and issue an error messages.  Otherwise,
16957    return TRUE.  */
16958
16959 static bool
16960 cp_parser_check_template_parameters (cp_parser* parser,
16961                                      unsigned num_templates,
16962                                      location_t location)
16963 {
16964   /* If there are more template classes than parameter lists, we have
16965      something like:
16966
16967        template <class T> void S<T>::R<T>::f ();  */
16968   if (parser->num_template_parameter_lists < num_templates)
16969     {
16970       error ("%Htoo few template-parameter-lists", &location);
16971       return false;
16972     }
16973   /* If there are the same number of template classes and parameter
16974      lists, that's OK.  */
16975   if (parser->num_template_parameter_lists == num_templates)
16976     return true;
16977   /* If there are more, but only one more, then we are referring to a
16978      member template.  That's OK too.  */
16979   if (parser->num_template_parameter_lists == num_templates + 1)
16980       return true;
16981   /* Otherwise, there are too many template parameter lists.  We have
16982      something like:
16983
16984      template <class T> template <class U> void S::f();  */
16985   error ("%Htoo many template-parameter-lists", &location);
16986   return false;
16987 }
16988
16989 /* Parse an optional `::' token indicating that the following name is
16990    from the global namespace.  If so, PARSER->SCOPE is set to the
16991    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16992    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16993    Returns the new value of PARSER->SCOPE, if the `::' token is
16994    present, and NULL_TREE otherwise.  */
16995
16996 static tree
16997 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16998 {
16999   cp_token *token;
17000
17001   /* Peek at the next token.  */
17002   token = cp_lexer_peek_token (parser->lexer);
17003   /* If we're looking at a `::' token then we're starting from the
17004      global namespace, not our current location.  */
17005   if (token->type == CPP_SCOPE)
17006     {
17007       /* Consume the `::' token.  */
17008       cp_lexer_consume_token (parser->lexer);
17009       /* Set the SCOPE so that we know where to start the lookup.  */
17010       parser->scope = global_namespace;
17011       parser->qualifying_scope = global_namespace;
17012       parser->object_scope = NULL_TREE;
17013
17014       return parser->scope;
17015     }
17016   else if (!current_scope_valid_p)
17017     {
17018       parser->scope = NULL_TREE;
17019       parser->qualifying_scope = NULL_TREE;
17020       parser->object_scope = NULL_TREE;
17021     }
17022
17023   return NULL_TREE;
17024 }
17025
17026 /* Returns TRUE if the upcoming token sequence is the start of a
17027    constructor declarator.  If FRIEND_P is true, the declarator is
17028    preceded by the `friend' specifier.  */
17029
17030 static bool
17031 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17032 {
17033   bool constructor_p;
17034   tree type_decl = NULL_TREE;
17035   bool nested_name_p;
17036   cp_token *next_token;
17037
17038   /* The common case is that this is not a constructor declarator, so
17039      try to avoid doing lots of work if at all possible.  It's not
17040      valid declare a constructor at function scope.  */
17041   if (parser->in_function_body)
17042     return false;
17043   /* And only certain tokens can begin a constructor declarator.  */
17044   next_token = cp_lexer_peek_token (parser->lexer);
17045   if (next_token->type != CPP_NAME
17046       && next_token->type != CPP_SCOPE
17047       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17048       && next_token->type != CPP_TEMPLATE_ID)
17049     return false;
17050
17051   /* Parse tentatively; we are going to roll back all of the tokens
17052      consumed here.  */
17053   cp_parser_parse_tentatively (parser);
17054   /* Assume that we are looking at a constructor declarator.  */
17055   constructor_p = true;
17056
17057   /* Look for the optional `::' operator.  */
17058   cp_parser_global_scope_opt (parser,
17059                               /*current_scope_valid_p=*/false);
17060   /* Look for the nested-name-specifier.  */
17061   nested_name_p
17062     = (cp_parser_nested_name_specifier_opt (parser,
17063                                             /*typename_keyword_p=*/false,
17064                                             /*check_dependency_p=*/false,
17065                                             /*type_p=*/false,
17066                                             /*is_declaration=*/false)
17067        != NULL_TREE);
17068   /* Outside of a class-specifier, there must be a
17069      nested-name-specifier.  */
17070   if (!nested_name_p &&
17071       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17072        || friend_p))
17073     constructor_p = false;
17074   /* If we still think that this might be a constructor-declarator,
17075      look for a class-name.  */
17076   if (constructor_p)
17077     {
17078       /* If we have:
17079
17080            template <typename T> struct S { S(); };
17081            template <typename T> S<T>::S ();
17082
17083          we must recognize that the nested `S' names a class.
17084          Similarly, for:
17085
17086            template <typename T> S<T>::S<T> ();
17087
17088          we must recognize that the nested `S' names a template.  */
17089       type_decl = cp_parser_class_name (parser,
17090                                         /*typename_keyword_p=*/false,
17091                                         /*template_keyword_p=*/false,
17092                                         none_type,
17093                                         /*check_dependency_p=*/false,
17094                                         /*class_head_p=*/false,
17095                                         /*is_declaration=*/false);
17096       /* If there was no class-name, then this is not a constructor.  */
17097       constructor_p = !cp_parser_error_occurred (parser);
17098     }
17099
17100   /* If we're still considering a constructor, we have to see a `(',
17101      to begin the parameter-declaration-clause, followed by either a
17102      `)', an `...', or a decl-specifier.  We need to check for a
17103      type-specifier to avoid being fooled into thinking that:
17104
17105        S::S (f) (int);
17106
17107      is a constructor.  (It is actually a function named `f' that
17108      takes one parameter (of type `int') and returns a value of type
17109      `S::S'.  */
17110   if (constructor_p
17111       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17112     {
17113       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17114           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17115           /* A parameter declaration begins with a decl-specifier,
17116              which is either the "attribute" keyword, a storage class
17117              specifier, or (usually) a type-specifier.  */
17118           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17119         {
17120           tree type;
17121           tree pushed_scope = NULL_TREE;
17122           unsigned saved_num_template_parameter_lists;
17123
17124           /* Names appearing in the type-specifier should be looked up
17125              in the scope of the class.  */
17126           if (current_class_type)
17127             type = NULL_TREE;
17128           else
17129             {
17130               type = TREE_TYPE (type_decl);
17131               if (TREE_CODE (type) == TYPENAME_TYPE)
17132                 {
17133                   type = resolve_typename_type (type,
17134                                                 /*only_current_p=*/false);
17135                   if (TREE_CODE (type) == TYPENAME_TYPE)
17136                     {
17137                       cp_parser_abort_tentative_parse (parser);
17138                       return false;
17139                     }
17140                 }
17141               pushed_scope = push_scope (type);
17142             }
17143
17144           /* Inside the constructor parameter list, surrounding
17145              template-parameter-lists do not apply.  */
17146           saved_num_template_parameter_lists
17147             = parser->num_template_parameter_lists;
17148           parser->num_template_parameter_lists = 0;
17149
17150           /* Look for the type-specifier.  */
17151           cp_parser_type_specifier (parser,
17152                                     CP_PARSER_FLAGS_NONE,
17153                                     /*decl_specs=*/NULL,
17154                                     /*is_declarator=*/true,
17155                                     /*declares_class_or_enum=*/NULL,
17156                                     /*is_cv_qualifier=*/NULL);
17157
17158           parser->num_template_parameter_lists
17159             = saved_num_template_parameter_lists;
17160
17161           /* Leave the scope of the class.  */
17162           if (pushed_scope)
17163             pop_scope (pushed_scope);
17164
17165           constructor_p = !cp_parser_error_occurred (parser);
17166         }
17167     }
17168   else
17169     constructor_p = false;
17170   /* We did not really want to consume any tokens.  */
17171   cp_parser_abort_tentative_parse (parser);
17172
17173   return constructor_p;
17174 }
17175
17176 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17177    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17178    they must be performed once we are in the scope of the function.
17179
17180    Returns the function defined.  */
17181
17182 static tree
17183 cp_parser_function_definition_from_specifiers_and_declarator
17184   (cp_parser* parser,
17185    cp_decl_specifier_seq *decl_specifiers,
17186    tree attributes,
17187    const cp_declarator *declarator)
17188 {
17189   tree fn;
17190   bool success_p;
17191
17192   /* Begin the function-definition.  */
17193   success_p = start_function (decl_specifiers, declarator, attributes);
17194
17195   /* The things we're about to see are not directly qualified by any
17196      template headers we've seen thus far.  */
17197   reset_specialization ();
17198
17199   /* If there were names looked up in the decl-specifier-seq that we
17200      did not check, check them now.  We must wait until we are in the
17201      scope of the function to perform the checks, since the function
17202      might be a friend.  */
17203   perform_deferred_access_checks ();
17204
17205   if (!success_p)
17206     {
17207       /* Skip the entire function.  */
17208       cp_parser_skip_to_end_of_block_or_statement (parser);
17209       fn = error_mark_node;
17210     }
17211   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17212     {
17213       /* Seen already, skip it.  An error message has already been output.  */
17214       cp_parser_skip_to_end_of_block_or_statement (parser);
17215       fn = current_function_decl;
17216       current_function_decl = NULL_TREE;
17217       /* If this is a function from a class, pop the nested class.  */
17218       if (current_class_name)
17219         pop_nested_class ();
17220     }
17221   else
17222     fn = cp_parser_function_definition_after_declarator (parser,
17223                                                          /*inline_p=*/false);
17224
17225   return fn;
17226 }
17227
17228 /* Parse the part of a function-definition that follows the
17229    declarator.  INLINE_P is TRUE iff this function is an inline
17230    function defined with a class-specifier.
17231
17232    Returns the function defined.  */
17233
17234 static tree
17235 cp_parser_function_definition_after_declarator (cp_parser* parser,
17236                                                 bool inline_p)
17237 {
17238   tree fn;
17239   bool ctor_initializer_p = false;
17240   bool saved_in_unbraced_linkage_specification_p;
17241   bool saved_in_function_body;
17242   unsigned saved_num_template_parameter_lists;
17243   cp_token *token;
17244
17245   saved_in_function_body = parser->in_function_body;
17246   parser->in_function_body = true;
17247   /* If the next token is `return', then the code may be trying to
17248      make use of the "named return value" extension that G++ used to
17249      support.  */
17250   token = cp_lexer_peek_token (parser->lexer);
17251   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17252     {
17253       /* Consume the `return' keyword.  */
17254       cp_lexer_consume_token (parser->lexer);
17255       /* Look for the identifier that indicates what value is to be
17256          returned.  */
17257       cp_parser_identifier (parser);
17258       /* Issue an error message.  */
17259       error ("%Hnamed return values are no longer supported",
17260              &token->location);
17261       /* Skip tokens until we reach the start of the function body.  */
17262       while (true)
17263         {
17264           cp_token *token = cp_lexer_peek_token (parser->lexer);
17265           if (token->type == CPP_OPEN_BRACE
17266               || token->type == CPP_EOF
17267               || token->type == CPP_PRAGMA_EOL)
17268             break;
17269           cp_lexer_consume_token (parser->lexer);
17270         }
17271     }
17272   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17273      anything declared inside `f'.  */
17274   saved_in_unbraced_linkage_specification_p
17275     = parser->in_unbraced_linkage_specification_p;
17276   parser->in_unbraced_linkage_specification_p = false;
17277   /* Inside the function, surrounding template-parameter-lists do not
17278      apply.  */
17279   saved_num_template_parameter_lists
17280     = parser->num_template_parameter_lists;
17281   parser->num_template_parameter_lists = 0;
17282   /* If the next token is `try', then we are looking at a
17283      function-try-block.  */
17284   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17285     ctor_initializer_p = cp_parser_function_try_block (parser);
17286   /* A function-try-block includes the function-body, so we only do
17287      this next part if we're not processing a function-try-block.  */
17288   else
17289     ctor_initializer_p
17290       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17291
17292   /* Finish the function.  */
17293   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17294                         (inline_p ? 2 : 0));
17295   /* Generate code for it, if necessary.  */
17296   expand_or_defer_fn (fn);
17297   /* Restore the saved values.  */
17298   parser->in_unbraced_linkage_specification_p
17299     = saved_in_unbraced_linkage_specification_p;
17300   parser->num_template_parameter_lists
17301     = saved_num_template_parameter_lists;
17302   parser->in_function_body = saved_in_function_body;
17303
17304   return fn;
17305 }
17306
17307 /* Parse a template-declaration, assuming that the `export' (and
17308    `extern') keywords, if present, has already been scanned.  MEMBER_P
17309    is as for cp_parser_template_declaration.  */
17310
17311 static void
17312 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17313 {
17314   tree decl = NULL_TREE;
17315   VEC (deferred_access_check,gc) *checks;
17316   tree parameter_list;
17317   bool friend_p = false;
17318   bool need_lang_pop;
17319   cp_token *token;
17320
17321   /* Look for the `template' keyword.  */
17322   token = cp_lexer_peek_token (parser->lexer);
17323   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17324     return;
17325
17326   /* And the `<'.  */
17327   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17328     return;
17329   if (at_class_scope_p () && current_function_decl)
17330     {
17331       /* 14.5.2.2 [temp.mem]
17332
17333          A local class shall not have member templates.  */
17334       error ("%Hinvalid declaration of member template in local class",
17335              &token->location);
17336       cp_parser_skip_to_end_of_block_or_statement (parser);
17337       return;
17338     }
17339   /* [temp]
17340
17341      A template ... shall not have C linkage.  */
17342   if (current_lang_name == lang_name_c)
17343     {
17344       error ("%Htemplate with C linkage", &token->location);
17345       /* Give it C++ linkage to avoid confusing other parts of the
17346          front end.  */
17347       push_lang_context (lang_name_cplusplus);
17348       need_lang_pop = true;
17349     }
17350   else
17351     need_lang_pop = false;
17352
17353   /* We cannot perform access checks on the template parameter
17354      declarations until we know what is being declared, just as we
17355      cannot check the decl-specifier list.  */
17356   push_deferring_access_checks (dk_deferred);
17357
17358   /* If the next token is `>', then we have an invalid
17359      specialization.  Rather than complain about an invalid template
17360      parameter, issue an error message here.  */
17361   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17362     {
17363       cp_parser_error (parser, "invalid explicit specialization");
17364       begin_specialization ();
17365       parameter_list = NULL_TREE;
17366     }
17367   else
17368     /* Parse the template parameters.  */
17369     parameter_list = cp_parser_template_parameter_list (parser);
17370
17371   /* Get the deferred access checks from the parameter list.  These
17372      will be checked once we know what is being declared, as for a
17373      member template the checks must be performed in the scope of the
17374      class containing the member.  */
17375   checks = get_deferred_access_checks ();
17376
17377   /* Look for the `>'.  */
17378   cp_parser_skip_to_end_of_template_parameter_list (parser);
17379   /* We just processed one more parameter list.  */
17380   ++parser->num_template_parameter_lists;
17381   /* If the next token is `template', there are more template
17382      parameters.  */
17383   if (cp_lexer_next_token_is_keyword (parser->lexer,
17384                                       RID_TEMPLATE))
17385     cp_parser_template_declaration_after_export (parser, member_p);
17386   else
17387     {
17388       /* There are no access checks when parsing a template, as we do not
17389          know if a specialization will be a friend.  */
17390       push_deferring_access_checks (dk_no_check);
17391       token = cp_lexer_peek_token (parser->lexer);
17392       decl = cp_parser_single_declaration (parser,
17393                                            checks,
17394                                            member_p,
17395                                            /*explicit_specialization_p=*/false,
17396                                            &friend_p);
17397       pop_deferring_access_checks ();
17398
17399       /* If this is a member template declaration, let the front
17400          end know.  */
17401       if (member_p && !friend_p && decl)
17402         {
17403           if (TREE_CODE (decl) == TYPE_DECL)
17404             cp_parser_check_access_in_redeclaration (decl, token->location);
17405
17406           decl = finish_member_template_decl (decl);
17407         }
17408       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17409         make_friend_class (current_class_type, TREE_TYPE (decl),
17410                            /*complain=*/true);
17411     }
17412   /* We are done with the current parameter list.  */
17413   --parser->num_template_parameter_lists;
17414
17415   pop_deferring_access_checks ();
17416
17417   /* Finish up.  */
17418   finish_template_decl (parameter_list);
17419
17420   /* Register member declarations.  */
17421   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17422     finish_member_declaration (decl);
17423   /* For the erroneous case of a template with C linkage, we pushed an
17424      implicit C++ linkage scope; exit that scope now.  */
17425   if (need_lang_pop)
17426     pop_lang_context ();
17427   /* If DECL is a function template, we must return to parse it later.
17428      (Even though there is no definition, there might be default
17429      arguments that need handling.)  */
17430   if (member_p && decl
17431       && (TREE_CODE (decl) == FUNCTION_DECL
17432           || DECL_FUNCTION_TEMPLATE_P (decl)))
17433     TREE_VALUE (parser->unparsed_functions_queues)
17434       = tree_cons (NULL_TREE, decl,
17435                    TREE_VALUE (parser->unparsed_functions_queues));
17436 }
17437
17438 /* Perform the deferred access checks from a template-parameter-list.
17439    CHECKS is a TREE_LIST of access checks, as returned by
17440    get_deferred_access_checks.  */
17441
17442 static void
17443 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17444 {
17445   ++processing_template_parmlist;
17446   perform_access_checks (checks);
17447   --processing_template_parmlist;
17448 }
17449
17450 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17451    `function-definition' sequence.  MEMBER_P is true, this declaration
17452    appears in a class scope.
17453
17454    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17455    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17456
17457 static tree
17458 cp_parser_single_declaration (cp_parser* parser,
17459                               VEC (deferred_access_check,gc)* checks,
17460                               bool member_p,
17461                               bool explicit_specialization_p,
17462                               bool* friend_p)
17463 {
17464   int declares_class_or_enum;
17465   tree decl = NULL_TREE;
17466   cp_decl_specifier_seq decl_specifiers;
17467   bool function_definition_p = false;
17468   cp_token *decl_spec_token_start;
17469
17470   /* This function is only used when processing a template
17471      declaration.  */
17472   gcc_assert (innermost_scope_kind () == sk_template_parms
17473               || innermost_scope_kind () == sk_template_spec);
17474
17475   /* Defer access checks until we know what is being declared.  */
17476   push_deferring_access_checks (dk_deferred);
17477
17478   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17479      alternative.  */
17480   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17481   cp_parser_decl_specifier_seq (parser,
17482                                 CP_PARSER_FLAGS_OPTIONAL,
17483                                 &decl_specifiers,
17484                                 &declares_class_or_enum);
17485   if (friend_p)
17486     *friend_p = cp_parser_friend_p (&decl_specifiers);
17487
17488   /* There are no template typedefs.  */
17489   if (decl_specifiers.specs[(int) ds_typedef])
17490     {
17491       error ("%Htemplate declaration of %qs",
17492              &decl_spec_token_start->location, "typedef");
17493       decl = error_mark_node;
17494     }
17495
17496   /* Gather up the access checks that occurred the
17497      decl-specifier-seq.  */
17498   stop_deferring_access_checks ();
17499
17500   /* Check for the declaration of a template class.  */
17501   if (declares_class_or_enum)
17502     {
17503       if (cp_parser_declares_only_class_p (parser))
17504         {
17505           decl = shadow_tag (&decl_specifiers);
17506
17507           /* In this case:
17508
17509                struct C {
17510                  friend template <typename T> struct A<T>::B;
17511                };
17512
17513              A<T>::B will be represented by a TYPENAME_TYPE, and
17514              therefore not recognized by shadow_tag.  */
17515           if (friend_p && *friend_p
17516               && !decl
17517               && decl_specifiers.type
17518               && TYPE_P (decl_specifiers.type))
17519             decl = decl_specifiers.type;
17520
17521           if (decl && decl != error_mark_node)
17522             decl = TYPE_NAME (decl);
17523           else
17524             decl = error_mark_node;
17525
17526           /* Perform access checks for template parameters.  */
17527           cp_parser_perform_template_parameter_access_checks (checks);
17528         }
17529     }
17530   /* If it's not a template class, try for a template function.  If
17531      the next token is a `;', then this declaration does not declare
17532      anything.  But, if there were errors in the decl-specifiers, then
17533      the error might well have come from an attempted class-specifier.
17534      In that case, there's no need to warn about a missing declarator.  */
17535   if (!decl
17536       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17537           || decl_specifiers.type != error_mark_node))
17538     {
17539       decl = cp_parser_init_declarator (parser,
17540                                         &decl_specifiers,
17541                                         checks,
17542                                         /*function_definition_allowed_p=*/true,
17543                                         member_p,
17544                                         declares_class_or_enum,
17545                                         &function_definition_p);
17546
17547     /* 7.1.1-1 [dcl.stc]
17548
17549        A storage-class-specifier shall not be specified in an explicit
17550        specialization...  */
17551     if (decl
17552         && explicit_specialization_p
17553         && decl_specifiers.storage_class != sc_none)
17554       {
17555         error ("%Hexplicit template specialization cannot have a storage class",
17556                &decl_spec_token_start->location);
17557         decl = error_mark_node;
17558       }
17559     }
17560
17561   pop_deferring_access_checks ();
17562
17563   /* Clear any current qualification; whatever comes next is the start
17564      of something new.  */
17565   parser->scope = NULL_TREE;
17566   parser->qualifying_scope = NULL_TREE;
17567   parser->object_scope = NULL_TREE;
17568   /* Look for a trailing `;' after the declaration.  */
17569   if (!function_definition_p
17570       && (decl == error_mark_node
17571           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17572     cp_parser_skip_to_end_of_block_or_statement (parser);
17573
17574   return decl;
17575 }
17576
17577 /* Parse a cast-expression that is not the operand of a unary "&".  */
17578
17579 static tree
17580 cp_parser_simple_cast_expression (cp_parser *parser)
17581 {
17582   return cp_parser_cast_expression (parser, /*address_p=*/false,
17583                                     /*cast_p=*/false);
17584 }
17585
17586 /* Parse a functional cast to TYPE.  Returns an expression
17587    representing the cast.  */
17588
17589 static tree
17590 cp_parser_functional_cast (cp_parser* parser, tree type)
17591 {
17592   tree expression_list;
17593   tree cast;
17594   bool nonconst_p;
17595
17596   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17597     {
17598       maybe_warn_cpp0x ("extended initializer lists");
17599       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17600       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17601       if (TREE_CODE (type) == TYPE_DECL)
17602         type = TREE_TYPE (type);
17603       return finish_compound_literal (type, expression_list);
17604     }
17605
17606   expression_list
17607     = cp_parser_parenthesized_expression_list (parser, false,
17608                                                /*cast_p=*/true,
17609                                                /*allow_expansion_p=*/true,
17610                                                /*non_constant_p=*/NULL);
17611
17612   cast = build_functional_cast (type, expression_list,
17613                                 tf_warning_or_error);
17614   /* [expr.const]/1: In an integral constant expression "only type
17615      conversions to integral or enumeration type can be used".  */
17616   if (TREE_CODE (type) == TYPE_DECL)
17617     type = TREE_TYPE (type);
17618   if (cast != error_mark_node
17619       && !cast_valid_in_integral_constant_expression_p (type)
17620       && (cp_parser_non_integral_constant_expression
17621           (parser, "a call to a constructor")))
17622     return error_mark_node;
17623   return cast;
17624 }
17625
17626 /* Save the tokens that make up the body of a member function defined
17627    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17628    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17629    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17630    for the member function.  */
17631
17632 static tree
17633 cp_parser_save_member_function_body (cp_parser* parser,
17634                                      cp_decl_specifier_seq *decl_specifiers,
17635                                      cp_declarator *declarator,
17636                                      tree attributes)
17637 {
17638   cp_token *first;
17639   cp_token *last;
17640   tree fn;
17641
17642   /* Create the function-declaration.  */
17643   fn = start_method (decl_specifiers, declarator, attributes);
17644   /* If something went badly wrong, bail out now.  */
17645   if (fn == error_mark_node)
17646     {
17647       /* If there's a function-body, skip it.  */
17648       if (cp_parser_token_starts_function_definition_p
17649           (cp_lexer_peek_token (parser->lexer)))
17650         cp_parser_skip_to_end_of_block_or_statement (parser);
17651       return error_mark_node;
17652     }
17653
17654   /* Remember it, if there default args to post process.  */
17655   cp_parser_save_default_args (parser, fn);
17656
17657   /* Save away the tokens that make up the body of the
17658      function.  */
17659   first = parser->lexer->next_token;
17660   /* We can have braced-init-list mem-initializers before the fn body.  */
17661   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17662     {
17663       cp_lexer_consume_token (parser->lexer);
17664       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17665              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17666         {
17667           /* cache_group will stop after an un-nested { } pair, too.  */
17668           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17669             break;
17670
17671           /* variadic mem-inits have ... after the ')'.  */
17672           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17673             cp_lexer_consume_token (parser->lexer);
17674         }
17675     }
17676   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17677   /* Handle function try blocks.  */
17678   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17679     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17680   last = parser->lexer->next_token;
17681
17682   /* Save away the inline definition; we will process it when the
17683      class is complete.  */
17684   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17685   DECL_PENDING_INLINE_P (fn) = 1;
17686
17687   /* We need to know that this was defined in the class, so that
17688      friend templates are handled correctly.  */
17689   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17690
17691   /* We're done with the inline definition.  */
17692   finish_method (fn);
17693
17694   /* Add FN to the queue of functions to be parsed later.  */
17695   TREE_VALUE (parser->unparsed_functions_queues)
17696     = tree_cons (NULL_TREE, fn,
17697                  TREE_VALUE (parser->unparsed_functions_queues));
17698
17699   return fn;
17700 }
17701
17702 /* Parse a template-argument-list, as well as the trailing ">" (but
17703    not the opening ">").  See cp_parser_template_argument_list for the
17704    return value.  */
17705
17706 static tree
17707 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17708 {
17709   tree arguments;
17710   tree saved_scope;
17711   tree saved_qualifying_scope;
17712   tree saved_object_scope;
17713   bool saved_greater_than_is_operator_p;
17714   bool saved_skip_evaluation;
17715
17716   /* [temp.names]
17717
17718      When parsing a template-id, the first non-nested `>' is taken as
17719      the end of the template-argument-list rather than a greater-than
17720      operator.  */
17721   saved_greater_than_is_operator_p
17722     = parser->greater_than_is_operator_p;
17723   parser->greater_than_is_operator_p = false;
17724   /* Parsing the argument list may modify SCOPE, so we save it
17725      here.  */
17726   saved_scope = parser->scope;
17727   saved_qualifying_scope = parser->qualifying_scope;
17728   saved_object_scope = parser->object_scope;
17729   /* We need to evaluate the template arguments, even though this
17730      template-id may be nested within a "sizeof".  */
17731   saved_skip_evaluation = skip_evaluation;
17732   skip_evaluation = false;
17733   /* Parse the template-argument-list itself.  */
17734   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17735       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17736     arguments = NULL_TREE;
17737   else
17738     arguments = cp_parser_template_argument_list (parser);
17739   /* Look for the `>' that ends the template-argument-list. If we find
17740      a '>>' instead, it's probably just a typo.  */
17741   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17742     {
17743       if (cxx_dialect != cxx98)
17744         {
17745           /* In C++0x, a `>>' in a template argument list or cast
17746              expression is considered to be two separate `>'
17747              tokens. So, change the current token to a `>', but don't
17748              consume it: it will be consumed later when the outer
17749              template argument list (or cast expression) is parsed.
17750              Note that this replacement of `>' for `>>' is necessary
17751              even if we are parsing tentatively: in the tentative
17752              case, after calling
17753              cp_parser_enclosed_template_argument_list we will always
17754              throw away all of the template arguments and the first
17755              closing `>', either because the template argument list
17756              was erroneous or because we are replacing those tokens
17757              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17758              not have been thrown away) is needed either to close an
17759              outer template argument list or to complete a new-style
17760              cast.  */
17761           cp_token *token = cp_lexer_peek_token (parser->lexer);
17762           token->type = CPP_GREATER;
17763         }
17764       else if (!saved_greater_than_is_operator_p)
17765         {
17766           /* If we're in a nested template argument list, the '>>' has
17767             to be a typo for '> >'. We emit the error message, but we
17768             continue parsing and we push a '>' as next token, so that
17769             the argument list will be parsed correctly.  Note that the
17770             global source location is still on the token before the
17771             '>>', so we need to say explicitly where we want it.  */
17772           cp_token *token = cp_lexer_peek_token (parser->lexer);
17773           error ("%H%<>>%> should be %<> >%> "
17774                  "within a nested template argument list",
17775                  &token->location);
17776
17777           token->type = CPP_GREATER;
17778         }
17779       else
17780         {
17781           /* If this is not a nested template argument list, the '>>'
17782             is a typo for '>'. Emit an error message and continue.
17783             Same deal about the token location, but here we can get it
17784             right by consuming the '>>' before issuing the diagnostic.  */
17785           cp_token *token = cp_lexer_consume_token (parser->lexer);
17786           error ("%Hspurious %<>>%>, use %<>%> to terminate "
17787                  "a template argument list", &token->location);
17788         }
17789     }
17790   else
17791     cp_parser_skip_to_end_of_template_parameter_list (parser);
17792   /* The `>' token might be a greater-than operator again now.  */
17793   parser->greater_than_is_operator_p
17794     = saved_greater_than_is_operator_p;
17795   /* Restore the SAVED_SCOPE.  */
17796   parser->scope = saved_scope;
17797   parser->qualifying_scope = saved_qualifying_scope;
17798   parser->object_scope = saved_object_scope;
17799   skip_evaluation = saved_skip_evaluation;
17800
17801   return arguments;
17802 }
17803
17804 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17805    arguments, or the body of the function have not yet been parsed,
17806    parse them now.  */
17807
17808 static void
17809 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17810 {
17811   /* If this member is a template, get the underlying
17812      FUNCTION_DECL.  */
17813   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17814     member_function = DECL_TEMPLATE_RESULT (member_function);
17815
17816   /* There should not be any class definitions in progress at this
17817      point; the bodies of members are only parsed outside of all class
17818      definitions.  */
17819   gcc_assert (parser->num_classes_being_defined == 0);
17820   /* While we're parsing the member functions we might encounter more
17821      classes.  We want to handle them right away, but we don't want
17822      them getting mixed up with functions that are currently in the
17823      queue.  */
17824   parser->unparsed_functions_queues
17825     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17826
17827   /* Make sure that any template parameters are in scope.  */
17828   maybe_begin_member_template_processing (member_function);
17829
17830   /* If the body of the function has not yet been parsed, parse it
17831      now.  */
17832   if (DECL_PENDING_INLINE_P (member_function))
17833     {
17834       tree function_scope;
17835       cp_token_cache *tokens;
17836
17837       /* The function is no longer pending; we are processing it.  */
17838       tokens = DECL_PENDING_INLINE_INFO (member_function);
17839       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17840       DECL_PENDING_INLINE_P (member_function) = 0;
17841
17842       /* If this is a local class, enter the scope of the containing
17843          function.  */
17844       function_scope = current_function_decl;
17845       if (function_scope)
17846         push_function_context ();
17847
17848       /* Push the body of the function onto the lexer stack.  */
17849       cp_parser_push_lexer_for_tokens (parser, tokens);
17850
17851       /* Let the front end know that we going to be defining this
17852          function.  */
17853       start_preparsed_function (member_function, NULL_TREE,
17854                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17855
17856       /* Don't do access checking if it is a templated function.  */
17857       if (processing_template_decl)
17858         push_deferring_access_checks (dk_no_check);
17859
17860       /* Now, parse the body of the function.  */
17861       cp_parser_function_definition_after_declarator (parser,
17862                                                       /*inline_p=*/true);
17863
17864       if (processing_template_decl)
17865         pop_deferring_access_checks ();
17866
17867       /* Leave the scope of the containing function.  */
17868       if (function_scope)
17869         pop_function_context ();
17870       cp_parser_pop_lexer (parser);
17871     }
17872
17873   /* Remove any template parameters from the symbol table.  */
17874   maybe_end_member_template_processing ();
17875
17876   /* Restore the queue.  */
17877   parser->unparsed_functions_queues
17878     = TREE_CHAIN (parser->unparsed_functions_queues);
17879 }
17880
17881 /* If DECL contains any default args, remember it on the unparsed
17882    functions queue.  */
17883
17884 static void
17885 cp_parser_save_default_args (cp_parser* parser, tree decl)
17886 {
17887   tree probe;
17888
17889   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17890        probe;
17891        probe = TREE_CHAIN (probe))
17892     if (TREE_PURPOSE (probe))
17893       {
17894         TREE_PURPOSE (parser->unparsed_functions_queues)
17895           = tree_cons (current_class_type, decl,
17896                        TREE_PURPOSE (parser->unparsed_functions_queues));
17897         break;
17898       }
17899 }
17900
17901 /* FN is a FUNCTION_DECL which may contains a parameter with an
17902    unparsed DEFAULT_ARG.  Parse the default args now.  This function
17903    assumes that the current scope is the scope in which the default
17904    argument should be processed.  */
17905
17906 static void
17907 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17908 {
17909   bool saved_local_variables_forbidden_p;
17910   tree parm;
17911
17912   /* While we're parsing the default args, we might (due to the
17913      statement expression extension) encounter more classes.  We want
17914      to handle them right away, but we don't want them getting mixed
17915      up with default args that are currently in the queue.  */
17916   parser->unparsed_functions_queues
17917     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17918
17919   /* Local variable names (and the `this' keyword) may not appear
17920      in a default argument.  */
17921   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17922   parser->local_variables_forbidden_p = true;
17923
17924   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17925        parm;
17926        parm = TREE_CHAIN (parm))
17927     {
17928       cp_token_cache *tokens;
17929       tree default_arg = TREE_PURPOSE (parm);
17930       tree parsed_arg;
17931       VEC(tree,gc) *insts;
17932       tree copy;
17933       unsigned ix;
17934
17935       if (!default_arg)
17936         continue;
17937
17938       if (TREE_CODE (default_arg) != DEFAULT_ARG)
17939         /* This can happen for a friend declaration for a function
17940            already declared with default arguments.  */
17941         continue;
17942
17943        /* Push the saved tokens for the default argument onto the parser's
17944           lexer stack.  */
17945       tokens = DEFARG_TOKENS (default_arg);
17946       cp_parser_push_lexer_for_tokens (parser, tokens);
17947
17948       /* Parse the assignment-expression.  */
17949       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17950
17951       if (!processing_template_decl)
17952         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17953
17954       TREE_PURPOSE (parm) = parsed_arg;
17955
17956       /* Update any instantiations we've already created.  */
17957       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17958            VEC_iterate (tree, insts, ix, copy); ix++)
17959         TREE_PURPOSE (copy) = parsed_arg;
17960
17961       /* If the token stream has not been completely used up, then
17962          there was extra junk after the end of the default
17963          argument.  */
17964       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17965         cp_parser_error (parser, "expected %<,%>");
17966
17967       /* Revert to the main lexer.  */
17968       cp_parser_pop_lexer (parser);
17969     }
17970
17971   /* Make sure no default arg is missing.  */
17972   check_default_args (fn);
17973
17974   /* Restore the state of local_variables_forbidden_p.  */
17975   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17976
17977   /* Restore the queue.  */
17978   parser->unparsed_functions_queues
17979     = TREE_CHAIN (parser->unparsed_functions_queues);
17980 }
17981
17982 /* Parse the operand of `sizeof' (or a similar operator).  Returns
17983    either a TYPE or an expression, depending on the form of the
17984    input.  The KEYWORD indicates which kind of expression we have
17985    encountered.  */
17986
17987 static tree
17988 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17989 {
17990   tree expr = NULL_TREE;
17991   const char *saved_message;
17992   char *tmp;
17993   bool saved_integral_constant_expression_p;
17994   bool saved_non_integral_constant_expression_p;
17995   bool pack_expansion_p = false;
17996
17997   /* Types cannot be defined in a `sizeof' expression.  Save away the
17998      old message.  */
17999   saved_message = parser->type_definition_forbidden_message;
18000   /* And create the new one.  */
18001   tmp = concat ("types may not be defined in %<",
18002                 IDENTIFIER_POINTER (ridpointers[keyword]),
18003                 "%> expressions", NULL);
18004   parser->type_definition_forbidden_message = tmp;
18005
18006   /* The restrictions on constant-expressions do not apply inside
18007      sizeof expressions.  */
18008   saved_integral_constant_expression_p
18009     = parser->integral_constant_expression_p;
18010   saved_non_integral_constant_expression_p
18011     = parser->non_integral_constant_expression_p;
18012   parser->integral_constant_expression_p = false;
18013
18014   /* If it's a `...', then we are computing the length of a parameter
18015      pack.  */
18016   if (keyword == RID_SIZEOF
18017       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18018     {
18019       /* Consume the `...'.  */
18020       cp_lexer_consume_token (parser->lexer);
18021       maybe_warn_variadic_templates ();
18022
18023       /* Note that this is an expansion.  */
18024       pack_expansion_p = true;
18025     }
18026
18027   /* Do not actually evaluate the expression.  */
18028   ++skip_evaluation;
18029   /* If it's a `(', then we might be looking at the type-id
18030      construction.  */
18031   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18032     {
18033       tree type;
18034       bool saved_in_type_id_in_expr_p;
18035
18036       /* We can't be sure yet whether we're looking at a type-id or an
18037          expression.  */
18038       cp_parser_parse_tentatively (parser);
18039       /* Consume the `('.  */
18040       cp_lexer_consume_token (parser->lexer);
18041       /* Parse the type-id.  */
18042       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18043       parser->in_type_id_in_expr_p = true;
18044       type = cp_parser_type_id (parser);
18045       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18046       /* Now, look for the trailing `)'.  */
18047       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18048       /* If all went well, then we're done.  */
18049       if (cp_parser_parse_definitely (parser))
18050         {
18051           cp_decl_specifier_seq decl_specs;
18052
18053           /* Build a trivial decl-specifier-seq.  */
18054           clear_decl_specs (&decl_specs);
18055           decl_specs.type = type;
18056
18057           /* Call grokdeclarator to figure out what type this is.  */
18058           expr = grokdeclarator (NULL,
18059                                  &decl_specs,
18060                                  TYPENAME,
18061                                  /*initialized=*/0,
18062                                  /*attrlist=*/NULL);
18063         }
18064     }
18065
18066   /* If the type-id production did not work out, then we must be
18067      looking at the unary-expression production.  */
18068   if (!expr)
18069     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18070                                        /*cast_p=*/false);
18071
18072   if (pack_expansion_p)
18073     /* Build a pack expansion. */
18074     expr = make_pack_expansion (expr);
18075
18076   /* Go back to evaluating expressions.  */
18077   --skip_evaluation;
18078
18079   /* Free the message we created.  */
18080   free (tmp);
18081   /* And restore the old one.  */
18082   parser->type_definition_forbidden_message = saved_message;
18083   parser->integral_constant_expression_p
18084     = saved_integral_constant_expression_p;
18085   parser->non_integral_constant_expression_p
18086     = saved_non_integral_constant_expression_p;
18087
18088   return expr;
18089 }
18090
18091 /* If the current declaration has no declarator, return true.  */
18092
18093 static bool
18094 cp_parser_declares_only_class_p (cp_parser *parser)
18095 {
18096   /* If the next token is a `;' or a `,' then there is no
18097      declarator.  */
18098   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18099           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18100 }
18101
18102 /* Update the DECL_SPECS to reflect the storage class indicated by
18103    KEYWORD.  */
18104
18105 static void
18106 cp_parser_set_storage_class (cp_parser *parser,
18107                              cp_decl_specifier_seq *decl_specs,
18108                              enum rid keyword,
18109                              location_t location)
18110 {
18111   cp_storage_class storage_class;
18112
18113   if (parser->in_unbraced_linkage_specification_p)
18114     {
18115       error ("%Hinvalid use of %qD in linkage specification",
18116              &location, ridpointers[keyword]);
18117       return;
18118     }
18119   else if (decl_specs->storage_class != sc_none)
18120     {
18121       decl_specs->conflicting_specifiers_p = true;
18122       return;
18123     }
18124
18125   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18126       && decl_specs->specs[(int) ds_thread])
18127     {
18128       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18129       decl_specs->specs[(int) ds_thread] = 0;
18130     }
18131
18132   switch (keyword)
18133     {
18134     case RID_AUTO:
18135       storage_class = sc_auto;
18136       break;
18137     case RID_REGISTER:
18138       storage_class = sc_register;
18139       break;
18140     case RID_STATIC:
18141       storage_class = sc_static;
18142       break;
18143     case RID_EXTERN:
18144       storage_class = sc_extern;
18145       break;
18146     case RID_MUTABLE:
18147       storage_class = sc_mutable;
18148       break;
18149     default:
18150       gcc_unreachable ();
18151     }
18152   decl_specs->storage_class = storage_class;
18153
18154   /* A storage class specifier cannot be applied alongside a typedef 
18155      specifier. If there is a typedef specifier present then set 
18156      conflicting_specifiers_p which will trigger an error later
18157      on in grokdeclarator. */
18158   if (decl_specs->specs[(int)ds_typedef])
18159     decl_specs->conflicting_specifiers_p = true;
18160 }
18161
18162 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18163    is true, the type is a user-defined type; otherwise it is a
18164    built-in type specified by a keyword.  */
18165
18166 static void
18167 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18168                               tree type_spec,
18169                               location_t location,
18170                               bool user_defined_p)
18171 {
18172   decl_specs->any_specifiers_p = true;
18173
18174   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18175      (with, for example, in "typedef int wchar_t;") we remember that
18176      this is what happened.  In system headers, we ignore these
18177      declarations so that G++ can work with system headers that are not
18178      C++-safe.  */
18179   if (decl_specs->specs[(int) ds_typedef]
18180       && !user_defined_p
18181       && (type_spec == boolean_type_node
18182           || type_spec == char16_type_node
18183           || type_spec == char32_type_node
18184           || type_spec == wchar_type_node)
18185       && (decl_specs->type
18186           || decl_specs->specs[(int) ds_long]
18187           || decl_specs->specs[(int) ds_short]
18188           || decl_specs->specs[(int) ds_unsigned]
18189           || decl_specs->specs[(int) ds_signed]))
18190     {
18191       decl_specs->redefined_builtin_type = type_spec;
18192       if (!decl_specs->type)
18193         {
18194           decl_specs->type = type_spec;
18195           decl_specs->user_defined_type_p = false;
18196           decl_specs->type_location = location;
18197         }
18198     }
18199   else if (decl_specs->type)
18200     decl_specs->multiple_types_p = true;
18201   else
18202     {
18203       decl_specs->type = type_spec;
18204       decl_specs->user_defined_type_p = user_defined_p;
18205       decl_specs->redefined_builtin_type = NULL_TREE;
18206       decl_specs->type_location = location;
18207     }
18208 }
18209
18210 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18211    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18212
18213 static bool
18214 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18215 {
18216   return decl_specifiers->specs[(int) ds_friend] != 0;
18217 }
18218
18219 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18220    issue an error message indicating that TOKEN_DESC was expected.
18221
18222    Returns the token consumed, if the token had the appropriate type.
18223    Otherwise, returns NULL.  */
18224
18225 static cp_token *
18226 cp_parser_require (cp_parser* parser,
18227                    enum cpp_ttype type,
18228                    const char* token_desc)
18229 {
18230   if (cp_lexer_next_token_is (parser->lexer, type))
18231     return cp_lexer_consume_token (parser->lexer);
18232   else
18233     {
18234       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18235       if (!cp_parser_simulate_error (parser))
18236         {
18237           char *message = concat ("expected ", token_desc, NULL);
18238           cp_parser_error (parser, message);
18239           free (message);
18240         }
18241       return NULL;
18242     }
18243 }
18244
18245 /* An error message is produced if the next token is not '>'.
18246    All further tokens are skipped until the desired token is
18247    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18248
18249 static void
18250 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18251 {
18252   /* Current level of '< ... >'.  */
18253   unsigned level = 0;
18254   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18255   unsigned nesting_depth = 0;
18256
18257   /* Are we ready, yet?  If not, issue error message.  */
18258   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18259     return;
18260
18261   /* Skip tokens until the desired token is found.  */
18262   while (true)
18263     {
18264       /* Peek at the next token.  */
18265       switch (cp_lexer_peek_token (parser->lexer)->type)
18266         {
18267         case CPP_LESS:
18268           if (!nesting_depth)
18269             ++level;
18270           break;
18271
18272         case CPP_RSHIFT:
18273           if (cxx_dialect == cxx98)
18274             /* C++0x views the `>>' operator as two `>' tokens, but
18275                C++98 does not. */
18276             break;
18277           else if (!nesting_depth && level-- == 0)
18278             {
18279               /* We've hit a `>>' where the first `>' closes the
18280                  template argument list, and the second `>' is
18281                  spurious.  Just consume the `>>' and stop; we've
18282                  already produced at least one error.  */
18283               cp_lexer_consume_token (parser->lexer);
18284               return;
18285             }
18286           /* Fall through for C++0x, so we handle the second `>' in
18287              the `>>'.  */
18288
18289         case CPP_GREATER:
18290           if (!nesting_depth && level-- == 0)
18291             {
18292               /* We've reached the token we want, consume it and stop.  */
18293               cp_lexer_consume_token (parser->lexer);
18294               return;
18295             }
18296           break;
18297
18298         case CPP_OPEN_PAREN:
18299         case CPP_OPEN_SQUARE:
18300           ++nesting_depth;
18301           break;
18302
18303         case CPP_CLOSE_PAREN:
18304         case CPP_CLOSE_SQUARE:
18305           if (nesting_depth-- == 0)
18306             return;
18307           break;
18308
18309         case CPP_EOF:
18310         case CPP_PRAGMA_EOL:
18311         case CPP_SEMICOLON:
18312         case CPP_OPEN_BRACE:
18313         case CPP_CLOSE_BRACE:
18314           /* The '>' was probably forgotten, don't look further.  */
18315           return;
18316
18317         default:
18318           break;
18319         }
18320
18321       /* Consume this token.  */
18322       cp_lexer_consume_token (parser->lexer);
18323     }
18324 }
18325
18326 /* If the next token is the indicated keyword, consume it.  Otherwise,
18327    issue an error message indicating that TOKEN_DESC was expected.
18328
18329    Returns the token consumed, if the token had the appropriate type.
18330    Otherwise, returns NULL.  */
18331
18332 static cp_token *
18333 cp_parser_require_keyword (cp_parser* parser,
18334                            enum rid keyword,
18335                            const char* token_desc)
18336 {
18337   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18338
18339   if (token && token->keyword != keyword)
18340     {
18341       dyn_string_t error_msg;
18342
18343       /* Format the error message.  */
18344       error_msg = dyn_string_new (0);
18345       dyn_string_append_cstr (error_msg, "expected ");
18346       dyn_string_append_cstr (error_msg, token_desc);
18347       cp_parser_error (parser, error_msg->s);
18348       dyn_string_delete (error_msg);
18349       return NULL;
18350     }
18351
18352   return token;
18353 }
18354
18355 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18356    function-definition.  */
18357
18358 static bool
18359 cp_parser_token_starts_function_definition_p (cp_token* token)
18360 {
18361   return (/* An ordinary function-body begins with an `{'.  */
18362           token->type == CPP_OPEN_BRACE
18363           /* A ctor-initializer begins with a `:'.  */
18364           || token->type == CPP_COLON
18365           /* A function-try-block begins with `try'.  */
18366           || token->keyword == RID_TRY
18367           /* The named return value extension begins with `return'.  */
18368           || token->keyword == RID_RETURN);
18369 }
18370
18371 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18372    definition.  */
18373
18374 static bool
18375 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18376 {
18377   cp_token *token;
18378
18379   token = cp_lexer_peek_token (parser->lexer);
18380   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18381 }
18382
18383 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18384    C++0x) ending a template-argument.  */
18385
18386 static bool
18387 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18388 {
18389   cp_token *token;
18390
18391   token = cp_lexer_peek_token (parser->lexer);
18392   return (token->type == CPP_COMMA 
18393           || token->type == CPP_GREATER
18394           || token->type == CPP_ELLIPSIS
18395           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18396 }
18397
18398 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18399    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18400
18401 static bool
18402 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18403                                                      size_t n)
18404 {
18405   cp_token *token;
18406
18407   token = cp_lexer_peek_nth_token (parser->lexer, n);
18408   if (token->type == CPP_LESS)
18409     return true;
18410   /* Check for the sequence `<::' in the original code. It would be lexed as
18411      `[:', where `[' is a digraph, and there is no whitespace before
18412      `:'.  */
18413   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18414     {
18415       cp_token *token2;
18416       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18417       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18418         return true;
18419     }
18420   return false;
18421 }
18422
18423 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18424    or none_type otherwise.  */
18425
18426 static enum tag_types
18427 cp_parser_token_is_class_key (cp_token* token)
18428 {
18429   switch (token->keyword)
18430     {
18431     case RID_CLASS:
18432       return class_type;
18433     case RID_STRUCT:
18434       return record_type;
18435     case RID_UNION:
18436       return union_type;
18437
18438     default:
18439       return none_type;
18440     }
18441 }
18442
18443 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18444
18445 static void
18446 cp_parser_check_class_key (enum tag_types class_key, tree type)
18447 {
18448   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18449     permerror ("%qs tag used in naming %q#T",
18450             class_key == union_type ? "union"
18451              : class_key == record_type ? "struct" : "class",
18452              type);
18453 }
18454
18455 /* Issue an error message if DECL is redeclared with different
18456    access than its original declaration [class.access.spec/3].
18457    This applies to nested classes and nested class templates.
18458    [class.mem/1].  */
18459
18460 static void
18461 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18462 {
18463   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18464     return;
18465
18466   if ((TREE_PRIVATE (decl)
18467        != (current_access_specifier == access_private_node))
18468       || (TREE_PROTECTED (decl)
18469           != (current_access_specifier == access_protected_node)))
18470     error ("%H%qD redeclared with different access", &location, decl);
18471 }
18472
18473 /* Look for the `template' keyword, as a syntactic disambiguator.
18474    Return TRUE iff it is present, in which case it will be
18475    consumed.  */
18476
18477 static bool
18478 cp_parser_optional_template_keyword (cp_parser *parser)
18479 {
18480   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18481     {
18482       /* The `template' keyword can only be used within templates;
18483          outside templates the parser can always figure out what is a
18484          template and what is not.  */
18485       if (!processing_template_decl)
18486         {
18487           cp_token *token = cp_lexer_peek_token (parser->lexer);
18488           error ("%H%<template%> (as a disambiguator) is only allowed "
18489                  "within templates", &token->location);
18490           /* If this part of the token stream is rescanned, the same
18491              error message would be generated.  So, we purge the token
18492              from the stream.  */
18493           cp_lexer_purge_token (parser->lexer);
18494           return false;
18495         }
18496       else
18497         {
18498           /* Consume the `template' keyword.  */
18499           cp_lexer_consume_token (parser->lexer);
18500           return true;
18501         }
18502     }
18503
18504   return false;
18505 }
18506
18507 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18508    set PARSER->SCOPE, and perform other related actions.  */
18509
18510 static void
18511 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18512 {
18513   int i;
18514   struct tree_check *check_value;
18515   deferred_access_check *chk;
18516   VEC (deferred_access_check,gc) *checks;
18517
18518   /* Get the stored value.  */
18519   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18520   /* Perform any access checks that were deferred.  */
18521   checks = check_value->checks;
18522   if (checks)
18523     {
18524       for (i = 0 ;
18525            VEC_iterate (deferred_access_check, checks, i, chk) ;
18526            ++i)
18527         {
18528           perform_or_defer_access_check (chk->binfo,
18529                                          chk->decl,
18530                                          chk->diag_decl);
18531         }
18532     }
18533   /* Set the scope from the stored value.  */
18534   parser->scope = check_value->value;
18535   parser->qualifying_scope = check_value->qualifying_scope;
18536   parser->object_scope = NULL_TREE;
18537 }
18538
18539 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18540    encounter the end of a block before what we were looking for.  */
18541
18542 static bool
18543 cp_parser_cache_group (cp_parser *parser,
18544                        enum cpp_ttype end,
18545                        unsigned depth)
18546 {
18547   while (true)
18548     {
18549       cp_token *token = cp_lexer_peek_token (parser->lexer);
18550
18551       /* Abort a parenthesized expression if we encounter a semicolon.  */
18552       if ((end == CPP_CLOSE_PAREN || depth == 0)
18553           && token->type == CPP_SEMICOLON)
18554         return true;
18555       /* If we've reached the end of the file, stop.  */
18556       if (token->type == CPP_EOF
18557           || (end != CPP_PRAGMA_EOL
18558               && token->type == CPP_PRAGMA_EOL))
18559         return true;
18560       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18561         /* We've hit the end of an enclosing block, so there's been some
18562            kind of syntax error.  */
18563         return true;
18564
18565       /* Consume the token.  */
18566       cp_lexer_consume_token (parser->lexer);
18567       /* See if it starts a new group.  */
18568       if (token->type == CPP_OPEN_BRACE)
18569         {
18570           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18571           /* In theory this should probably check end == '}', but
18572              cp_parser_save_member_function_body needs it to exit
18573              after either '}' or ')' when called with ')'.  */
18574           if (depth == 0)
18575             return false;
18576         }
18577       else if (token->type == CPP_OPEN_PAREN)
18578         {
18579           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18580           if (depth == 0 && end == CPP_CLOSE_PAREN)
18581             return false;
18582         }
18583       else if (token->type == CPP_PRAGMA)
18584         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18585       else if (token->type == end)
18586         return false;
18587     }
18588 }
18589
18590 /* Begin parsing tentatively.  We always save tokens while parsing
18591    tentatively so that if the tentative parsing fails we can restore the
18592    tokens.  */
18593
18594 static void
18595 cp_parser_parse_tentatively (cp_parser* parser)
18596 {
18597   /* Enter a new parsing context.  */
18598   parser->context = cp_parser_context_new (parser->context);
18599   /* Begin saving tokens.  */
18600   cp_lexer_save_tokens (parser->lexer);
18601   /* In order to avoid repetitive access control error messages,
18602      access checks are queued up until we are no longer parsing
18603      tentatively.  */
18604   push_deferring_access_checks (dk_deferred);
18605 }
18606
18607 /* Commit to the currently active tentative parse.  */
18608
18609 static void
18610 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18611 {
18612   cp_parser_context *context;
18613   cp_lexer *lexer;
18614
18615   /* Mark all of the levels as committed.  */
18616   lexer = parser->lexer;
18617   for (context = parser->context; context->next; context = context->next)
18618     {
18619       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18620         break;
18621       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18622       while (!cp_lexer_saving_tokens (lexer))
18623         lexer = lexer->next;
18624       cp_lexer_commit_tokens (lexer);
18625     }
18626 }
18627
18628 /* Abort the currently active tentative parse.  All consumed tokens
18629    will be rolled back, and no diagnostics will be issued.  */
18630
18631 static void
18632 cp_parser_abort_tentative_parse (cp_parser* parser)
18633 {
18634   cp_parser_simulate_error (parser);
18635   /* Now, pretend that we want to see if the construct was
18636      successfully parsed.  */
18637   cp_parser_parse_definitely (parser);
18638 }
18639
18640 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18641    token stream.  Otherwise, commit to the tokens we have consumed.
18642    Returns true if no error occurred; false otherwise.  */
18643
18644 static bool
18645 cp_parser_parse_definitely (cp_parser* parser)
18646 {
18647   bool error_occurred;
18648   cp_parser_context *context;
18649
18650   /* Remember whether or not an error occurred, since we are about to
18651      destroy that information.  */
18652   error_occurred = cp_parser_error_occurred (parser);
18653   /* Remove the topmost context from the stack.  */
18654   context = parser->context;
18655   parser->context = context->next;
18656   /* If no parse errors occurred, commit to the tentative parse.  */
18657   if (!error_occurred)
18658     {
18659       /* Commit to the tokens read tentatively, unless that was
18660          already done.  */
18661       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18662         cp_lexer_commit_tokens (parser->lexer);
18663
18664       pop_to_parent_deferring_access_checks ();
18665     }
18666   /* Otherwise, if errors occurred, roll back our state so that things
18667      are just as they were before we began the tentative parse.  */
18668   else
18669     {
18670       cp_lexer_rollback_tokens (parser->lexer);
18671       pop_deferring_access_checks ();
18672     }
18673   /* Add the context to the front of the free list.  */
18674   context->next = cp_parser_context_free_list;
18675   cp_parser_context_free_list = context;
18676
18677   return !error_occurred;
18678 }
18679
18680 /* Returns true if we are parsing tentatively and are not committed to
18681    this tentative parse.  */
18682
18683 static bool
18684 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18685 {
18686   return (cp_parser_parsing_tentatively (parser)
18687           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18688 }
18689
18690 /* Returns nonzero iff an error has occurred during the most recent
18691    tentative parse.  */
18692
18693 static bool
18694 cp_parser_error_occurred (cp_parser* parser)
18695 {
18696   return (cp_parser_parsing_tentatively (parser)
18697           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18698 }
18699
18700 /* Returns nonzero if GNU extensions are allowed.  */
18701
18702 static bool
18703 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18704 {
18705   return parser->allow_gnu_extensions_p;
18706 }
18707 \f
18708 /* Objective-C++ Productions */
18709
18710
18711 /* Parse an Objective-C expression, which feeds into a primary-expression
18712    above.
18713
18714    objc-expression:
18715      objc-message-expression
18716      objc-string-literal
18717      objc-encode-expression
18718      objc-protocol-expression
18719      objc-selector-expression
18720
18721   Returns a tree representation of the expression.  */
18722
18723 static tree
18724 cp_parser_objc_expression (cp_parser* parser)
18725 {
18726   /* Try to figure out what kind of declaration is present.  */
18727   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18728
18729   switch (kwd->type)
18730     {
18731     case CPP_OPEN_SQUARE:
18732       return cp_parser_objc_message_expression (parser);
18733
18734     case CPP_OBJC_STRING:
18735       kwd = cp_lexer_consume_token (parser->lexer);
18736       return objc_build_string_object (kwd->u.value);
18737
18738     case CPP_KEYWORD:
18739       switch (kwd->keyword)
18740         {
18741         case RID_AT_ENCODE:
18742           return cp_parser_objc_encode_expression (parser);
18743
18744         case RID_AT_PROTOCOL:
18745           return cp_parser_objc_protocol_expression (parser);
18746
18747         case RID_AT_SELECTOR:
18748           return cp_parser_objc_selector_expression (parser);
18749
18750         default:
18751           break;
18752         }
18753     default:
18754       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18755              &kwd->location, kwd->u.value);
18756       cp_parser_skip_to_end_of_block_or_statement (parser);
18757     }
18758
18759   return error_mark_node;
18760 }
18761
18762 /* Parse an Objective-C message expression.
18763
18764    objc-message-expression:
18765      [ objc-message-receiver objc-message-args ]
18766
18767    Returns a representation of an Objective-C message.  */
18768
18769 static tree
18770 cp_parser_objc_message_expression (cp_parser* parser)
18771 {
18772   tree receiver, messageargs;
18773
18774   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18775   receiver = cp_parser_objc_message_receiver (parser);
18776   messageargs = cp_parser_objc_message_args (parser);
18777   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18778
18779   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18780 }
18781
18782 /* Parse an objc-message-receiver.
18783
18784    objc-message-receiver:
18785      expression
18786      simple-type-specifier
18787
18788   Returns a representation of the type or expression.  */
18789
18790 static tree
18791 cp_parser_objc_message_receiver (cp_parser* parser)
18792 {
18793   tree rcv;
18794
18795   /* An Objective-C message receiver may be either (1) a type
18796      or (2) an expression.  */
18797   cp_parser_parse_tentatively (parser);
18798   rcv = cp_parser_expression (parser, false);
18799
18800   if (cp_parser_parse_definitely (parser))
18801     return rcv;
18802
18803   rcv = cp_parser_simple_type_specifier (parser,
18804                                          /*decl_specs=*/NULL,
18805                                          CP_PARSER_FLAGS_NONE);
18806
18807   return objc_get_class_reference (rcv);
18808 }
18809
18810 /* Parse the arguments and selectors comprising an Objective-C message.
18811
18812    objc-message-args:
18813      objc-selector
18814      objc-selector-args
18815      objc-selector-args , objc-comma-args
18816
18817    objc-selector-args:
18818      objc-selector [opt] : assignment-expression
18819      objc-selector-args objc-selector [opt] : assignment-expression
18820
18821    objc-comma-args:
18822      assignment-expression
18823      objc-comma-args , assignment-expression
18824
18825    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18826    selector arguments and TREE_VALUE containing a list of comma
18827    arguments.  */
18828
18829 static tree
18830 cp_parser_objc_message_args (cp_parser* parser)
18831 {
18832   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18833   bool maybe_unary_selector_p = true;
18834   cp_token *token = cp_lexer_peek_token (parser->lexer);
18835
18836   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18837     {
18838       tree selector = NULL_TREE, arg;
18839
18840       if (token->type != CPP_COLON)
18841         selector = cp_parser_objc_selector (parser);
18842
18843       /* Detect if we have a unary selector.  */
18844       if (maybe_unary_selector_p
18845           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18846         return build_tree_list (selector, NULL_TREE);
18847
18848       maybe_unary_selector_p = false;
18849       cp_parser_require (parser, CPP_COLON, "%<:%>");
18850       arg = cp_parser_assignment_expression (parser, false);
18851
18852       sel_args
18853         = chainon (sel_args,
18854                    build_tree_list (selector, arg));
18855
18856       token = cp_lexer_peek_token (parser->lexer);
18857     }
18858
18859   /* Handle non-selector arguments, if any. */
18860   while (token->type == CPP_COMMA)
18861     {
18862       tree arg;
18863
18864       cp_lexer_consume_token (parser->lexer);
18865       arg = cp_parser_assignment_expression (parser, false);
18866
18867       addl_args
18868         = chainon (addl_args,
18869                    build_tree_list (NULL_TREE, arg));
18870
18871       token = cp_lexer_peek_token (parser->lexer);
18872     }
18873
18874   return build_tree_list (sel_args, addl_args);
18875 }
18876
18877 /* Parse an Objective-C encode expression.
18878
18879    objc-encode-expression:
18880      @encode objc-typename
18881
18882    Returns an encoded representation of the type argument.  */
18883
18884 static tree
18885 cp_parser_objc_encode_expression (cp_parser* parser)
18886 {
18887   tree type;
18888   cp_token *token;
18889
18890   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
18891   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18892   token = cp_lexer_peek_token (parser->lexer);
18893   type = complete_type (cp_parser_type_id (parser));
18894   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18895
18896   if (!type)
18897     {
18898       error ("%H%<@encode%> must specify a type as an argument",
18899              &token->location);
18900       return error_mark_node;
18901     }
18902
18903   return objc_build_encode_expr (type);
18904 }
18905
18906 /* Parse an Objective-C @defs expression.  */
18907
18908 static tree
18909 cp_parser_objc_defs_expression (cp_parser *parser)
18910 {
18911   tree name;
18912
18913   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
18914   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18915   name = cp_parser_identifier (parser);
18916   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18917
18918   return objc_get_class_ivars (name);
18919 }
18920
18921 /* Parse an Objective-C protocol expression.
18922
18923   objc-protocol-expression:
18924     @protocol ( identifier )
18925
18926   Returns a representation of the protocol expression.  */
18927
18928 static tree
18929 cp_parser_objc_protocol_expression (cp_parser* parser)
18930 {
18931   tree proto;
18932
18933   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18934   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18935   proto = cp_parser_identifier (parser);
18936   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18937
18938   return objc_build_protocol_expr (proto);
18939 }
18940
18941 /* Parse an Objective-C selector expression.
18942
18943    objc-selector-expression:
18944      @selector ( objc-method-signature )
18945
18946    objc-method-signature:
18947      objc-selector
18948      objc-selector-seq
18949
18950    objc-selector-seq:
18951      objc-selector :
18952      objc-selector-seq objc-selector :
18953
18954   Returns a representation of the method selector.  */
18955
18956 static tree
18957 cp_parser_objc_selector_expression (cp_parser* parser)
18958 {
18959   tree sel_seq = NULL_TREE;
18960   bool maybe_unary_selector_p = true;
18961   cp_token *token;
18962
18963   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
18964   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18965   token = cp_lexer_peek_token (parser->lexer);
18966
18967   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18968          || token->type == CPP_SCOPE)
18969     {
18970       tree selector = NULL_TREE;
18971
18972       if (token->type != CPP_COLON
18973           || token->type == CPP_SCOPE)
18974         selector = cp_parser_objc_selector (parser);
18975
18976       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18977           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18978         {
18979           /* Detect if we have a unary selector.  */
18980           if (maybe_unary_selector_p)
18981             {
18982               sel_seq = selector;
18983               goto finish_selector;
18984             }
18985           else
18986             {
18987               cp_parser_error (parser, "expected %<:%>");
18988             }
18989         }
18990       maybe_unary_selector_p = false;
18991       token = cp_lexer_consume_token (parser->lexer);
18992
18993       if (token->type == CPP_SCOPE)
18994         {
18995           sel_seq
18996             = chainon (sel_seq,
18997                        build_tree_list (selector, NULL_TREE));
18998           sel_seq
18999             = chainon (sel_seq,
19000                        build_tree_list (NULL_TREE, NULL_TREE));
19001         }
19002       else
19003         sel_seq
19004           = chainon (sel_seq,
19005                      build_tree_list (selector, NULL_TREE));
19006
19007       token = cp_lexer_peek_token (parser->lexer);
19008     }
19009
19010  finish_selector:
19011   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19012
19013   return objc_build_selector_expr (sel_seq);
19014 }
19015
19016 /* Parse a list of identifiers.
19017
19018    objc-identifier-list:
19019      identifier
19020      objc-identifier-list , identifier
19021
19022    Returns a TREE_LIST of identifier nodes.  */
19023
19024 static tree
19025 cp_parser_objc_identifier_list (cp_parser* parser)
19026 {
19027   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19028   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19029
19030   while (sep->type == CPP_COMMA)
19031     {
19032       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19033       list = chainon (list,
19034                       build_tree_list (NULL_TREE,
19035                                        cp_parser_identifier (parser)));
19036       sep = cp_lexer_peek_token (parser->lexer);
19037     }
19038
19039   return list;
19040 }
19041
19042 /* Parse an Objective-C alias declaration.
19043
19044    objc-alias-declaration:
19045      @compatibility_alias identifier identifier ;
19046
19047    This function registers the alias mapping with the Objective-C front end.
19048    It returns nothing.  */
19049
19050 static void
19051 cp_parser_objc_alias_declaration (cp_parser* parser)
19052 {
19053   tree alias, orig;
19054
19055   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19056   alias = cp_parser_identifier (parser);
19057   orig = cp_parser_identifier (parser);
19058   objc_declare_alias (alias, orig);
19059   cp_parser_consume_semicolon_at_end_of_statement (parser);
19060 }
19061
19062 /* Parse an Objective-C class forward-declaration.
19063
19064    objc-class-declaration:
19065      @class objc-identifier-list ;
19066
19067    The function registers the forward declarations with the Objective-C
19068    front end.  It returns nothing.  */
19069
19070 static void
19071 cp_parser_objc_class_declaration (cp_parser* parser)
19072 {
19073   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19074   objc_declare_class (cp_parser_objc_identifier_list (parser));
19075   cp_parser_consume_semicolon_at_end_of_statement (parser);
19076 }
19077
19078 /* Parse a list of Objective-C protocol references.
19079
19080    objc-protocol-refs-opt:
19081      objc-protocol-refs [opt]
19082
19083    objc-protocol-refs:
19084      < objc-identifier-list >
19085
19086    Returns a TREE_LIST of identifiers, if any.  */
19087
19088 static tree
19089 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19090 {
19091   tree protorefs = NULL_TREE;
19092
19093   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19094     {
19095       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19096       protorefs = cp_parser_objc_identifier_list (parser);
19097       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19098     }
19099
19100   return protorefs;
19101 }
19102
19103 /* Parse a Objective-C visibility specification.  */
19104
19105 static void
19106 cp_parser_objc_visibility_spec (cp_parser* parser)
19107 {
19108   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19109
19110   switch (vis->keyword)
19111     {
19112     case RID_AT_PRIVATE:
19113       objc_set_visibility (2);
19114       break;
19115     case RID_AT_PROTECTED:
19116       objc_set_visibility (0);
19117       break;
19118     case RID_AT_PUBLIC:
19119       objc_set_visibility (1);
19120       break;
19121     default:
19122       return;
19123     }
19124
19125   /* Eat '@private'/'@protected'/'@public'.  */
19126   cp_lexer_consume_token (parser->lexer);
19127 }
19128
19129 /* Parse an Objective-C method type.  */
19130
19131 static void
19132 cp_parser_objc_method_type (cp_parser* parser)
19133 {
19134   objc_set_method_type
19135    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19136     ? PLUS_EXPR
19137     : MINUS_EXPR);
19138 }
19139
19140 /* Parse an Objective-C protocol qualifier.  */
19141
19142 static tree
19143 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19144 {
19145   tree quals = NULL_TREE, node;
19146   cp_token *token = cp_lexer_peek_token (parser->lexer);
19147
19148   node = token->u.value;
19149
19150   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19151          && (node == ridpointers [(int) RID_IN]
19152              || node == ridpointers [(int) RID_OUT]
19153              || node == ridpointers [(int) RID_INOUT]
19154              || node == ridpointers [(int) RID_BYCOPY]
19155              || node == ridpointers [(int) RID_BYREF]
19156              || node == ridpointers [(int) RID_ONEWAY]))
19157     {
19158       quals = tree_cons (NULL_TREE, node, quals);
19159       cp_lexer_consume_token (parser->lexer);
19160       token = cp_lexer_peek_token (parser->lexer);
19161       node = token->u.value;
19162     }
19163
19164   return quals;
19165 }
19166
19167 /* Parse an Objective-C typename.  */
19168
19169 static tree
19170 cp_parser_objc_typename (cp_parser* parser)
19171 {
19172   tree type_name = NULL_TREE;
19173
19174   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19175     {
19176       tree proto_quals, cp_type = NULL_TREE;
19177
19178       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19179       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19180
19181       /* An ObjC type name may consist of just protocol qualifiers, in which
19182          case the type shall default to 'id'.  */
19183       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19184         cp_type = cp_parser_type_id (parser);
19185
19186       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19187       type_name = build_tree_list (proto_quals, cp_type);
19188     }
19189
19190   return type_name;
19191 }
19192
19193 /* Check to see if TYPE refers to an Objective-C selector name.  */
19194
19195 static bool
19196 cp_parser_objc_selector_p (enum cpp_ttype type)
19197 {
19198   return (type == CPP_NAME || type == CPP_KEYWORD
19199           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19200           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19201           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19202           || type == CPP_XOR || type == CPP_XOR_EQ);
19203 }
19204
19205 /* Parse an Objective-C selector.  */
19206
19207 static tree
19208 cp_parser_objc_selector (cp_parser* parser)
19209 {
19210   cp_token *token = cp_lexer_consume_token (parser->lexer);
19211
19212   if (!cp_parser_objc_selector_p (token->type))
19213     {
19214       error ("%Hinvalid Objective-C++ selector name", &token->location);
19215       return error_mark_node;
19216     }
19217
19218   /* C++ operator names are allowed to appear in ObjC selectors.  */
19219   switch (token->type)
19220     {
19221     case CPP_AND_AND: return get_identifier ("and");
19222     case CPP_AND_EQ: return get_identifier ("and_eq");
19223     case CPP_AND: return get_identifier ("bitand");
19224     case CPP_OR: return get_identifier ("bitor");
19225     case CPP_COMPL: return get_identifier ("compl");
19226     case CPP_NOT: return get_identifier ("not");
19227     case CPP_NOT_EQ: return get_identifier ("not_eq");
19228     case CPP_OR_OR: return get_identifier ("or");
19229     case CPP_OR_EQ: return get_identifier ("or_eq");
19230     case CPP_XOR: return get_identifier ("xor");
19231     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19232     default: return token->u.value;
19233     }
19234 }
19235
19236 /* Parse an Objective-C params list.  */
19237
19238 static tree
19239 cp_parser_objc_method_keyword_params (cp_parser* parser)
19240 {
19241   tree params = NULL_TREE;
19242   bool maybe_unary_selector_p = true;
19243   cp_token *token = cp_lexer_peek_token (parser->lexer);
19244
19245   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19246     {
19247       tree selector = NULL_TREE, type_name, identifier;
19248
19249       if (token->type != CPP_COLON)
19250         selector = cp_parser_objc_selector (parser);
19251
19252       /* Detect if we have a unary selector.  */
19253       if (maybe_unary_selector_p
19254           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19255         return selector;
19256
19257       maybe_unary_selector_p = false;
19258       cp_parser_require (parser, CPP_COLON, "%<:%>");
19259       type_name = cp_parser_objc_typename (parser);
19260       identifier = cp_parser_identifier (parser);
19261
19262       params
19263         = chainon (params,
19264                    objc_build_keyword_decl (selector,
19265                                             type_name,
19266                                             identifier));
19267
19268       token = cp_lexer_peek_token (parser->lexer);
19269     }
19270
19271   return params;
19272 }
19273
19274 /* Parse the non-keyword Objective-C params.  */
19275
19276 static tree
19277 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19278 {
19279   tree params = make_node (TREE_LIST);
19280   cp_token *token = cp_lexer_peek_token (parser->lexer);
19281   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19282
19283   while (token->type == CPP_COMMA)
19284     {
19285       cp_parameter_declarator *parmdecl;
19286       tree parm;
19287
19288       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19289       token = cp_lexer_peek_token (parser->lexer);
19290
19291       if (token->type == CPP_ELLIPSIS)
19292         {
19293           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19294           *ellipsisp = true;
19295           break;
19296         }
19297
19298       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19299       parm = grokdeclarator (parmdecl->declarator,
19300                              &parmdecl->decl_specifiers,
19301                              PARM, /*initialized=*/0,
19302                              /*attrlist=*/NULL);
19303
19304       chainon (params, build_tree_list (NULL_TREE, parm));
19305       token = cp_lexer_peek_token (parser->lexer);
19306     }
19307
19308   return params;
19309 }
19310
19311 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19312
19313 static void
19314 cp_parser_objc_interstitial_code (cp_parser* parser)
19315 {
19316   cp_token *token = cp_lexer_peek_token (parser->lexer);
19317
19318   /* If the next token is `extern' and the following token is a string
19319      literal, then we have a linkage specification.  */
19320   if (token->keyword == RID_EXTERN
19321       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19322     cp_parser_linkage_specification (parser);
19323   /* Handle #pragma, if any.  */
19324   else if (token->type == CPP_PRAGMA)
19325     cp_parser_pragma (parser, pragma_external);
19326   /* Allow stray semicolons.  */
19327   else if (token->type == CPP_SEMICOLON)
19328     cp_lexer_consume_token (parser->lexer);
19329   /* Finally, try to parse a block-declaration, or a function-definition.  */
19330   else
19331     cp_parser_block_declaration (parser, /*statement_p=*/false);
19332 }
19333
19334 /* Parse a method signature.  */
19335
19336 static tree
19337 cp_parser_objc_method_signature (cp_parser* parser)
19338 {
19339   tree rettype, kwdparms, optparms;
19340   bool ellipsis = false;
19341
19342   cp_parser_objc_method_type (parser);
19343   rettype = cp_parser_objc_typename (parser);
19344   kwdparms = cp_parser_objc_method_keyword_params (parser);
19345   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19346
19347   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19348 }
19349
19350 /* Pars an Objective-C method prototype list.  */
19351
19352 static void
19353 cp_parser_objc_method_prototype_list (cp_parser* parser)
19354 {
19355   cp_token *token = cp_lexer_peek_token (parser->lexer);
19356
19357   while (token->keyword != RID_AT_END)
19358     {
19359       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19360         {
19361           objc_add_method_declaration
19362            (cp_parser_objc_method_signature (parser));
19363           cp_parser_consume_semicolon_at_end_of_statement (parser);
19364         }
19365       else
19366         /* Allow for interspersed non-ObjC++ code.  */
19367         cp_parser_objc_interstitial_code (parser);
19368
19369       token = cp_lexer_peek_token (parser->lexer);
19370     }
19371
19372   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19373   objc_finish_interface ();
19374 }
19375
19376 /* Parse an Objective-C method definition list.  */
19377
19378 static void
19379 cp_parser_objc_method_definition_list (cp_parser* parser)
19380 {
19381   cp_token *token = cp_lexer_peek_token (parser->lexer);
19382
19383   while (token->keyword != RID_AT_END)
19384     {
19385       tree meth;
19386
19387       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19388         {
19389           push_deferring_access_checks (dk_deferred);
19390           objc_start_method_definition
19391            (cp_parser_objc_method_signature (parser));
19392
19393           /* For historical reasons, we accept an optional semicolon.  */
19394           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19395             cp_lexer_consume_token (parser->lexer);
19396
19397           perform_deferred_access_checks ();
19398           stop_deferring_access_checks ();
19399           meth = cp_parser_function_definition_after_declarator (parser,
19400                                                                  false);
19401           pop_deferring_access_checks ();
19402           objc_finish_method_definition (meth);
19403         }
19404       else
19405         /* Allow for interspersed non-ObjC++ code.  */
19406         cp_parser_objc_interstitial_code (parser);
19407
19408       token = cp_lexer_peek_token (parser->lexer);
19409     }
19410
19411   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19412   objc_finish_implementation ();
19413 }
19414
19415 /* Parse Objective-C ivars.  */
19416
19417 static void
19418 cp_parser_objc_class_ivars (cp_parser* parser)
19419 {
19420   cp_token *token = cp_lexer_peek_token (parser->lexer);
19421
19422   if (token->type != CPP_OPEN_BRACE)
19423     return;     /* No ivars specified.  */
19424
19425   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19426   token = cp_lexer_peek_token (parser->lexer);
19427
19428   while (token->type != CPP_CLOSE_BRACE)
19429     {
19430       cp_decl_specifier_seq declspecs;
19431       int decl_class_or_enum_p;
19432       tree prefix_attributes;
19433
19434       cp_parser_objc_visibility_spec (parser);
19435
19436       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19437         break;
19438
19439       cp_parser_decl_specifier_seq (parser,
19440                                     CP_PARSER_FLAGS_OPTIONAL,
19441                                     &declspecs,
19442                                     &decl_class_or_enum_p);
19443       prefix_attributes = declspecs.attributes;
19444       declspecs.attributes = NULL_TREE;
19445
19446       /* Keep going until we hit the `;' at the end of the
19447          declaration.  */
19448       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19449         {
19450           tree width = NULL_TREE, attributes, first_attribute, decl;
19451           cp_declarator *declarator = NULL;
19452           int ctor_dtor_or_conv_p;
19453
19454           /* Check for a (possibly unnamed) bitfield declaration.  */
19455           token = cp_lexer_peek_token (parser->lexer);
19456           if (token->type == CPP_COLON)
19457             goto eat_colon;
19458
19459           if (token->type == CPP_NAME
19460               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19461                   == CPP_COLON))
19462             {
19463               /* Get the name of the bitfield.  */
19464               declarator = make_id_declarator (NULL_TREE,
19465                                                cp_parser_identifier (parser),
19466                                                sfk_none);
19467
19468              eat_colon:
19469               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19470               /* Get the width of the bitfield.  */
19471               width
19472                 = cp_parser_constant_expression (parser,
19473                                                  /*allow_non_constant=*/false,
19474                                                  NULL);
19475             }
19476           else
19477             {
19478               /* Parse the declarator.  */
19479               declarator
19480                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19481                                         &ctor_dtor_or_conv_p,
19482                                         /*parenthesized_p=*/NULL,
19483                                         /*member_p=*/false);
19484             }
19485
19486           /* Look for attributes that apply to the ivar.  */
19487           attributes = cp_parser_attributes_opt (parser);
19488           /* Remember which attributes are prefix attributes and
19489              which are not.  */
19490           first_attribute = attributes;
19491           /* Combine the attributes.  */
19492           attributes = chainon (prefix_attributes, attributes);
19493
19494           if (width)
19495               /* Create the bitfield declaration.  */
19496               decl = grokbitfield (declarator, &declspecs,
19497                                    width,
19498                                    attributes);
19499           else
19500             decl = grokfield (declarator, &declspecs,
19501                               NULL_TREE, /*init_const_expr_p=*/false,
19502                               NULL_TREE, attributes);
19503
19504           /* Add the instance variable.  */
19505           objc_add_instance_variable (decl);
19506
19507           /* Reset PREFIX_ATTRIBUTES.  */
19508           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19509             attributes = TREE_CHAIN (attributes);
19510           if (attributes)
19511             TREE_CHAIN (attributes) = NULL_TREE;
19512
19513           token = cp_lexer_peek_token (parser->lexer);
19514
19515           if (token->type == CPP_COMMA)
19516             {
19517               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19518               continue;
19519             }
19520           break;
19521         }
19522
19523       cp_parser_consume_semicolon_at_end_of_statement (parser);
19524       token = cp_lexer_peek_token (parser->lexer);
19525     }
19526
19527   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19528   /* For historical reasons, we accept an optional semicolon.  */
19529   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19530     cp_lexer_consume_token (parser->lexer);
19531 }
19532
19533 /* Parse an Objective-C protocol declaration.  */
19534
19535 static void
19536 cp_parser_objc_protocol_declaration (cp_parser* parser)
19537 {
19538   tree proto, protorefs;
19539   cp_token *tok;
19540
19541   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19542   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19543     {
19544       tok = cp_lexer_peek_token (parser->lexer);
19545       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19546       goto finish;
19547     }
19548
19549   /* See if we have a forward declaration or a definition.  */
19550   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19551
19552   /* Try a forward declaration first.  */
19553   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19554     {
19555       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19556      finish:
19557       cp_parser_consume_semicolon_at_end_of_statement (parser);
19558     }
19559
19560   /* Ok, we got a full-fledged definition (or at least should).  */
19561   else
19562     {
19563       proto = cp_parser_identifier (parser);
19564       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19565       objc_start_protocol (proto, protorefs);
19566       cp_parser_objc_method_prototype_list (parser);
19567     }
19568 }
19569
19570 /* Parse an Objective-C superclass or category.  */
19571
19572 static void
19573 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19574                                                           tree *categ)
19575 {
19576   cp_token *next = cp_lexer_peek_token (parser->lexer);
19577
19578   *super = *categ = NULL_TREE;
19579   if (next->type == CPP_COLON)
19580     {
19581       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19582       *super = cp_parser_identifier (parser);
19583     }
19584   else if (next->type == CPP_OPEN_PAREN)
19585     {
19586       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19587       *categ = cp_parser_identifier (parser);
19588       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19589     }
19590 }
19591
19592 /* Parse an Objective-C class interface.  */
19593
19594 static void
19595 cp_parser_objc_class_interface (cp_parser* parser)
19596 {
19597   tree name, super, categ, protos;
19598
19599   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19600   name = cp_parser_identifier (parser);
19601   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19602   protos = cp_parser_objc_protocol_refs_opt (parser);
19603
19604   /* We have either a class or a category on our hands.  */
19605   if (categ)
19606     objc_start_category_interface (name, categ, protos);
19607   else
19608     {
19609       objc_start_class_interface (name, super, protos);
19610       /* Handle instance variable declarations, if any.  */
19611       cp_parser_objc_class_ivars (parser);
19612       objc_continue_interface ();
19613     }
19614
19615   cp_parser_objc_method_prototype_list (parser);
19616 }
19617
19618 /* Parse an Objective-C class implementation.  */
19619
19620 static void
19621 cp_parser_objc_class_implementation (cp_parser* parser)
19622 {
19623   tree name, super, categ;
19624
19625   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19626   name = cp_parser_identifier (parser);
19627   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19628
19629   /* We have either a class or a category on our hands.  */
19630   if (categ)
19631     objc_start_category_implementation (name, categ);
19632   else
19633     {
19634       objc_start_class_implementation (name, super);
19635       /* Handle instance variable declarations, if any.  */
19636       cp_parser_objc_class_ivars (parser);
19637       objc_continue_implementation ();
19638     }
19639
19640   cp_parser_objc_method_definition_list (parser);
19641 }
19642
19643 /* Consume the @end token and finish off the implementation.  */
19644
19645 static void
19646 cp_parser_objc_end_implementation (cp_parser* parser)
19647 {
19648   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19649   objc_finish_implementation ();
19650 }
19651
19652 /* Parse an Objective-C declaration.  */
19653
19654 static void
19655 cp_parser_objc_declaration (cp_parser* parser)
19656 {
19657   /* Try to figure out what kind of declaration is present.  */
19658   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19659
19660   switch (kwd->keyword)
19661     {
19662     case RID_AT_ALIAS:
19663       cp_parser_objc_alias_declaration (parser);
19664       break;
19665     case RID_AT_CLASS:
19666       cp_parser_objc_class_declaration (parser);
19667       break;
19668     case RID_AT_PROTOCOL:
19669       cp_parser_objc_protocol_declaration (parser);
19670       break;
19671     case RID_AT_INTERFACE:
19672       cp_parser_objc_class_interface (parser);
19673       break;
19674     case RID_AT_IMPLEMENTATION:
19675       cp_parser_objc_class_implementation (parser);
19676       break;
19677     case RID_AT_END:
19678       cp_parser_objc_end_implementation (parser);
19679       break;
19680     default:
19681       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19682              &kwd->location, kwd->u.value);
19683       cp_parser_skip_to_end_of_block_or_statement (parser);
19684     }
19685 }
19686
19687 /* Parse an Objective-C try-catch-finally statement.
19688
19689    objc-try-catch-finally-stmt:
19690      @try compound-statement objc-catch-clause-seq [opt]
19691        objc-finally-clause [opt]
19692
19693    objc-catch-clause-seq:
19694      objc-catch-clause objc-catch-clause-seq [opt]
19695
19696    objc-catch-clause:
19697      @catch ( exception-declaration ) compound-statement
19698
19699    objc-finally-clause
19700      @finally compound-statement
19701
19702    Returns NULL_TREE.  */
19703
19704 static tree
19705 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19706   location_t location;
19707   tree stmt;
19708
19709   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19710   location = cp_lexer_peek_token (parser->lexer)->location;
19711   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19712      node, lest it get absorbed into the surrounding block.  */
19713   stmt = push_stmt_list ();
19714   cp_parser_compound_statement (parser, NULL, false);
19715   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19716
19717   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19718     {
19719       cp_parameter_declarator *parmdecl;
19720       tree parm;
19721
19722       cp_lexer_consume_token (parser->lexer);
19723       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19724       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19725       parm = grokdeclarator (parmdecl->declarator,
19726                              &parmdecl->decl_specifiers,
19727                              PARM, /*initialized=*/0,
19728                              /*attrlist=*/NULL);
19729       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19730       objc_begin_catch_clause (parm);
19731       cp_parser_compound_statement (parser, NULL, false);
19732       objc_finish_catch_clause ();
19733     }
19734
19735   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19736     {
19737       cp_lexer_consume_token (parser->lexer);
19738       location = cp_lexer_peek_token (parser->lexer)->location;
19739       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19740          node, lest it get absorbed into the surrounding block.  */
19741       stmt = push_stmt_list ();
19742       cp_parser_compound_statement (parser, NULL, false);
19743       objc_build_finally_clause (location, pop_stmt_list (stmt));
19744     }
19745
19746   return objc_finish_try_stmt ();
19747 }
19748
19749 /* Parse an Objective-C synchronized statement.
19750
19751    objc-synchronized-stmt:
19752      @synchronized ( expression ) compound-statement
19753
19754    Returns NULL_TREE.  */
19755
19756 static tree
19757 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19758   location_t location;
19759   tree lock, stmt;
19760
19761   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19762
19763   location = cp_lexer_peek_token (parser->lexer)->location;
19764   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19765   lock = cp_parser_expression (parser, false);
19766   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19767
19768   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19769      node, lest it get absorbed into the surrounding block.  */
19770   stmt = push_stmt_list ();
19771   cp_parser_compound_statement (parser, NULL, false);
19772
19773   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19774 }
19775
19776 /* Parse an Objective-C throw statement.
19777
19778    objc-throw-stmt:
19779      @throw assignment-expression [opt] ;
19780
19781    Returns a constructed '@throw' statement.  */
19782
19783 static tree
19784 cp_parser_objc_throw_statement (cp_parser *parser) {
19785   tree expr = NULL_TREE;
19786
19787   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19788
19789   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19790     expr = cp_parser_assignment_expression (parser, false);
19791
19792   cp_parser_consume_semicolon_at_end_of_statement (parser);
19793
19794   return objc_build_throw_stmt (expr);
19795 }
19796
19797 /* Parse an Objective-C statement.  */
19798
19799 static tree
19800 cp_parser_objc_statement (cp_parser * parser) {
19801   /* Try to figure out what kind of declaration is present.  */
19802   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19803
19804   switch (kwd->keyword)
19805     {
19806     case RID_AT_TRY:
19807       return cp_parser_objc_try_catch_finally_statement (parser);
19808     case RID_AT_SYNCHRONIZED:
19809       return cp_parser_objc_synchronized_statement (parser);
19810     case RID_AT_THROW:
19811       return cp_parser_objc_throw_statement (parser);
19812     default:
19813       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19814              &kwd->location, kwd->u.value);
19815       cp_parser_skip_to_end_of_block_or_statement (parser);
19816     }
19817
19818   return error_mark_node;
19819 }
19820 \f
19821 /* OpenMP 2.5 parsing routines.  */
19822
19823 /* Returns name of the next clause.
19824    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19825    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19826    returned and the token is consumed.  */
19827
19828 static pragma_omp_clause
19829 cp_parser_omp_clause_name (cp_parser *parser)
19830 {
19831   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19832
19833   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19834     result = PRAGMA_OMP_CLAUSE_IF;
19835   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19836     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19837   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19838     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19839   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19840     {
19841       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19842       const char *p = IDENTIFIER_POINTER (id);
19843
19844       switch (p[0])
19845         {
19846         case 'c':
19847           if (!strcmp ("collapse", p))
19848             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
19849           else if (!strcmp ("copyin", p))
19850             result = PRAGMA_OMP_CLAUSE_COPYIN;
19851           else if (!strcmp ("copyprivate", p))
19852             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19853           break;
19854         case 'f':
19855           if (!strcmp ("firstprivate", p))
19856             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19857           break;
19858         case 'l':
19859           if (!strcmp ("lastprivate", p))
19860             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19861           break;
19862         case 'n':
19863           if (!strcmp ("nowait", p))
19864             result = PRAGMA_OMP_CLAUSE_NOWAIT;
19865           else if (!strcmp ("num_threads", p))
19866             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19867           break;
19868         case 'o':
19869           if (!strcmp ("ordered", p))
19870             result = PRAGMA_OMP_CLAUSE_ORDERED;
19871           break;
19872         case 'r':
19873           if (!strcmp ("reduction", p))
19874             result = PRAGMA_OMP_CLAUSE_REDUCTION;
19875           break;
19876         case 's':
19877           if (!strcmp ("schedule", p))
19878             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19879           else if (!strcmp ("shared", p))
19880             result = PRAGMA_OMP_CLAUSE_SHARED;
19881           break;
19882         case 'u':
19883           if (!strcmp ("untied", p))
19884             result = PRAGMA_OMP_CLAUSE_UNTIED;
19885           break;
19886         }
19887     }
19888
19889   if (result != PRAGMA_OMP_CLAUSE_NONE)
19890     cp_lexer_consume_token (parser->lexer);
19891
19892   return result;
19893 }
19894
19895 /* Validate that a clause of the given type does not already exist.  */
19896
19897 static void
19898 check_no_duplicate_clause (tree clauses, enum tree_code code,
19899                            const char *name, location_t location)
19900 {
19901   tree c;
19902
19903   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19904     if (OMP_CLAUSE_CODE (c) == code)
19905       {
19906         error ("%Htoo many %qs clauses", &location, name);
19907         break;
19908       }
19909 }
19910
19911 /* OpenMP 2.5:
19912    variable-list:
19913      identifier
19914      variable-list , identifier
19915
19916    In addition, we match a closing parenthesis.  An opening parenthesis
19917    will have been consumed by the caller.
19918
19919    If KIND is nonzero, create the appropriate node and install the decl
19920    in OMP_CLAUSE_DECL and add the node to the head of the list.
19921
19922    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19923    return the list created.  */
19924
19925 static tree
19926 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19927                                 tree list)
19928 {
19929   cp_token *token;
19930   while (1)
19931     {
19932       tree name, decl;
19933
19934       token = cp_lexer_peek_token (parser->lexer);
19935       name = cp_parser_id_expression (parser, /*template_p=*/false,
19936                                       /*check_dependency_p=*/true,
19937                                       /*template_p=*/NULL,
19938                                       /*declarator_p=*/false,
19939                                       /*optional_p=*/false);
19940       if (name == error_mark_node)
19941         goto skip_comma;
19942
19943       decl = cp_parser_lookup_name_simple (parser, name, token->location);
19944       if (decl == error_mark_node)
19945         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
19946       else if (kind != 0)
19947         {
19948           tree u = build_omp_clause (kind);
19949           OMP_CLAUSE_DECL (u) = decl;
19950           OMP_CLAUSE_CHAIN (u) = list;
19951           list = u;
19952         }
19953       else
19954         list = tree_cons (decl, NULL_TREE, list);
19955
19956     get_comma:
19957       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19958         break;
19959       cp_lexer_consume_token (parser->lexer);
19960     }
19961
19962   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19963     {
19964       int ending;
19965
19966       /* Try to resync to an unnested comma.  Copied from
19967          cp_parser_parenthesized_expression_list.  */
19968     skip_comma:
19969       ending = cp_parser_skip_to_closing_parenthesis (parser,
19970                                                       /*recovering=*/true,
19971                                                       /*or_comma=*/true,
19972                                                       /*consume_paren=*/true);
19973       if (ending < 0)
19974         goto get_comma;
19975     }
19976
19977   return list;
19978 }
19979
19980 /* Similarly, but expect leading and trailing parenthesis.  This is a very
19981    common case for omp clauses.  */
19982
19983 static tree
19984 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19985 {
19986   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19987     return cp_parser_omp_var_list_no_open (parser, kind, list);
19988   return list;
19989 }
19990
19991 /* OpenMP 3.0:
19992    collapse ( constant-expression ) */
19993
19994 static tree
19995 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
19996 {
19997   tree c, num;
19998   location_t loc;
19999   HOST_WIDE_INT n;
20000
20001   loc = cp_lexer_peek_token (parser->lexer)->location;
20002   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20003     return list;
20004
20005   num = cp_parser_constant_expression (parser, false, NULL);
20006
20007   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20008     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20009                                            /*or_comma=*/false,
20010                                            /*consume_paren=*/true);
20011
20012   if (num == error_mark_node)
20013     return list;
20014   num = fold_non_dependent_expr (num);
20015   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20016       || !host_integerp (num, 0)
20017       || (n = tree_low_cst (num, 0)) <= 0
20018       || (int) n != n)
20019     {
20020       error ("%Hcollapse argument needs positive constant integer expression",
20021              &loc);
20022       return list;
20023     }
20024
20025   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20026   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20027   OMP_CLAUSE_CHAIN (c) = list;
20028   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20029
20030   return c;
20031 }
20032
20033 /* OpenMP 2.5:
20034    default ( shared | none ) */
20035
20036 static tree
20037 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20038 {
20039   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20040   tree c;
20041
20042   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20043     return list;
20044   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20045     {
20046       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20047       const char *p = IDENTIFIER_POINTER (id);
20048
20049       switch (p[0])
20050         {
20051         case 'n':
20052           if (strcmp ("none", p) != 0)
20053             goto invalid_kind;
20054           kind = OMP_CLAUSE_DEFAULT_NONE;
20055           break;
20056
20057         case 's':
20058           if (strcmp ("shared", p) != 0)
20059             goto invalid_kind;
20060           kind = OMP_CLAUSE_DEFAULT_SHARED;
20061           break;
20062
20063         default:
20064           goto invalid_kind;
20065         }
20066
20067       cp_lexer_consume_token (parser->lexer);
20068     }
20069   else
20070     {
20071     invalid_kind:
20072       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20073     }
20074
20075   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20076     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20077                                            /*or_comma=*/false,
20078                                            /*consume_paren=*/true);
20079
20080   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20081     return list;
20082
20083   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20084   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20085   OMP_CLAUSE_CHAIN (c) = list;
20086   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20087
20088   return c;
20089 }
20090
20091 /* OpenMP 2.5:
20092    if ( expression ) */
20093
20094 static tree
20095 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20096 {
20097   tree t, c;
20098
20099   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20100     return list;
20101
20102   t = cp_parser_condition (parser);
20103
20104   if (t == error_mark_node
20105       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20106     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20107                                            /*or_comma=*/false,
20108                                            /*consume_paren=*/true);
20109
20110   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20111
20112   c = build_omp_clause (OMP_CLAUSE_IF);
20113   OMP_CLAUSE_IF_EXPR (c) = t;
20114   OMP_CLAUSE_CHAIN (c) = list;
20115
20116   return c;
20117 }
20118
20119 /* OpenMP 2.5:
20120    nowait */
20121
20122 static tree
20123 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20124                              tree list, location_t location)
20125 {
20126   tree c;
20127
20128   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20129
20130   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20131   OMP_CLAUSE_CHAIN (c) = list;
20132   return c;
20133 }
20134
20135 /* OpenMP 2.5:
20136    num_threads ( expression ) */
20137
20138 static tree
20139 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20140                                   location_t location)
20141 {
20142   tree t, c;
20143
20144   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20145     return list;
20146
20147   t = cp_parser_expression (parser, false);
20148
20149   if (t == error_mark_node
20150       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20151     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20152                                            /*or_comma=*/false,
20153                                            /*consume_paren=*/true);
20154
20155   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20156                              "num_threads", location);
20157
20158   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20159   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20160   OMP_CLAUSE_CHAIN (c) = list;
20161
20162   return c;
20163 }
20164
20165 /* OpenMP 2.5:
20166    ordered */
20167
20168 static tree
20169 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20170                               tree list, location_t location)
20171 {
20172   tree c;
20173
20174   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20175                              "ordered", location);
20176
20177   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20178   OMP_CLAUSE_CHAIN (c) = list;
20179   return c;
20180 }
20181
20182 /* OpenMP 2.5:
20183    reduction ( reduction-operator : variable-list )
20184
20185    reduction-operator:
20186      One of: + * - & ^ | && || */
20187
20188 static tree
20189 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20190 {
20191   enum tree_code code;
20192   tree nlist, c;
20193
20194   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20195     return list;
20196
20197   switch (cp_lexer_peek_token (parser->lexer)->type)
20198     {
20199     case CPP_PLUS:
20200       code = PLUS_EXPR;
20201       break;
20202     case CPP_MULT:
20203       code = MULT_EXPR;
20204       break;
20205     case CPP_MINUS:
20206       code = MINUS_EXPR;
20207       break;
20208     case CPP_AND:
20209       code = BIT_AND_EXPR;
20210       break;
20211     case CPP_XOR:
20212       code = BIT_XOR_EXPR;
20213       break;
20214     case CPP_OR:
20215       code = BIT_IOR_EXPR;
20216       break;
20217     case CPP_AND_AND:
20218       code = TRUTH_ANDIF_EXPR;
20219       break;
20220     case CPP_OR_OR:
20221       code = TRUTH_ORIF_EXPR;
20222       break;
20223     default:
20224       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20225                                "%<|%>, %<&&%>, or %<||%>");
20226     resync_fail:
20227       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20228                                              /*or_comma=*/false,
20229                                              /*consume_paren=*/true);
20230       return list;
20231     }
20232   cp_lexer_consume_token (parser->lexer);
20233
20234   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20235     goto resync_fail;
20236
20237   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20238   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20239     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20240
20241   return nlist;
20242 }
20243
20244 /* OpenMP 2.5:
20245    schedule ( schedule-kind )
20246    schedule ( schedule-kind , expression )
20247
20248    schedule-kind:
20249      static | dynamic | guided | runtime | auto  */
20250
20251 static tree
20252 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20253 {
20254   tree c, t;
20255
20256   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20257     return list;
20258
20259   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20260
20261   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20262     {
20263       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20264       const char *p = IDENTIFIER_POINTER (id);
20265
20266       switch (p[0])
20267         {
20268         case 'd':
20269           if (strcmp ("dynamic", p) != 0)
20270             goto invalid_kind;
20271           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20272           break;
20273
20274         case 'g':
20275           if (strcmp ("guided", p) != 0)
20276             goto invalid_kind;
20277           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20278           break;
20279
20280         case 'r':
20281           if (strcmp ("runtime", p) != 0)
20282             goto invalid_kind;
20283           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20284           break;
20285
20286         default:
20287           goto invalid_kind;
20288         }
20289     }
20290   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20291     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20292   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20293     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20294   else
20295     goto invalid_kind;
20296   cp_lexer_consume_token (parser->lexer);
20297
20298   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20299     {
20300       cp_token *token;
20301       cp_lexer_consume_token (parser->lexer);
20302
20303       token = cp_lexer_peek_token (parser->lexer);
20304       t = cp_parser_assignment_expression (parser, false);
20305
20306       if (t == error_mark_node)
20307         goto resync_fail;
20308       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20309         error ("%Hschedule %<runtime%> does not take "
20310                "a %<chunk_size%> parameter", &token->location);
20311       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20312         error ("%Hschedule %<auto%> does not take "
20313                "a %<chunk_size%> parameter", &token->location);
20314       else
20315         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20316
20317       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20318         goto resync_fail;
20319     }
20320   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20321     goto resync_fail;
20322
20323   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20324   OMP_CLAUSE_CHAIN (c) = list;
20325   return c;
20326
20327  invalid_kind:
20328   cp_parser_error (parser, "invalid schedule kind");
20329  resync_fail:
20330   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20331                                          /*or_comma=*/false,
20332                                          /*consume_paren=*/true);
20333   return list;
20334 }
20335
20336 /* OpenMP 3.0:
20337    untied */
20338
20339 static tree
20340 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20341                              tree list, location_t location)
20342 {
20343   tree c;
20344
20345   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20346
20347   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20348   OMP_CLAUSE_CHAIN (c) = list;
20349   return c;
20350 }
20351
20352 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20353    is a bitmask in MASK.  Return the list of clauses found; the result
20354    of clause default goes in *pdefault.  */
20355
20356 static tree
20357 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20358                            const char *where, cp_token *pragma_tok)
20359 {
20360   tree clauses = NULL;
20361   bool first = true;
20362   cp_token *token = NULL;
20363
20364   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20365     {
20366       pragma_omp_clause c_kind;
20367       const char *c_name;
20368       tree prev = clauses;
20369
20370       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20371         cp_lexer_consume_token (parser->lexer);
20372
20373       token = cp_lexer_peek_token (parser->lexer);
20374       c_kind = cp_parser_omp_clause_name (parser);
20375       first = false;
20376
20377       switch (c_kind)
20378         {
20379         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20380           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20381                                                    token->location);
20382           c_name = "collapse";
20383           break;
20384         case PRAGMA_OMP_CLAUSE_COPYIN:
20385           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20386           c_name = "copyin";
20387           break;
20388         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20389           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20390                                             clauses);
20391           c_name = "copyprivate";
20392           break;
20393         case PRAGMA_OMP_CLAUSE_DEFAULT:
20394           clauses = cp_parser_omp_clause_default (parser, clauses,
20395                                                   token->location);
20396           c_name = "default";
20397           break;
20398         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20399           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20400                                             clauses);
20401           c_name = "firstprivate";
20402           break;
20403         case PRAGMA_OMP_CLAUSE_IF:
20404           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20405           c_name = "if";
20406           break;
20407         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20408           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20409                                             clauses);
20410           c_name = "lastprivate";
20411           break;
20412         case PRAGMA_OMP_CLAUSE_NOWAIT:
20413           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20414           c_name = "nowait";
20415           break;
20416         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20417           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20418                                                       token->location);
20419           c_name = "num_threads";
20420           break;
20421         case PRAGMA_OMP_CLAUSE_ORDERED:
20422           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20423                                                   token->location);
20424           c_name = "ordered";
20425           break;
20426         case PRAGMA_OMP_CLAUSE_PRIVATE:
20427           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20428                                             clauses);
20429           c_name = "private";
20430           break;
20431         case PRAGMA_OMP_CLAUSE_REDUCTION:
20432           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20433           c_name = "reduction";
20434           break;
20435         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20436           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20437                                                    token->location);
20438           c_name = "schedule";
20439           break;
20440         case PRAGMA_OMP_CLAUSE_SHARED:
20441           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20442                                             clauses);
20443           c_name = "shared";
20444           break;
20445         case PRAGMA_OMP_CLAUSE_UNTIED:
20446           clauses = cp_parser_omp_clause_untied (parser, clauses,
20447                                                  token->location);
20448           c_name = "nowait";
20449           break;
20450         default:
20451           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20452           goto saw_error;
20453         }
20454
20455       if (((mask >> c_kind) & 1) == 0)
20456         {
20457           /* Remove the invalid clause(s) from the list to avoid
20458              confusing the rest of the compiler.  */
20459           clauses = prev;
20460           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20461         }
20462     }
20463  saw_error:
20464   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20465   return finish_omp_clauses (clauses);
20466 }
20467
20468 /* OpenMP 2.5:
20469    structured-block:
20470      statement
20471
20472    In practice, we're also interested in adding the statement to an
20473    outer node.  So it is convenient if we work around the fact that
20474    cp_parser_statement calls add_stmt.  */
20475
20476 static unsigned
20477 cp_parser_begin_omp_structured_block (cp_parser *parser)
20478 {
20479   unsigned save = parser->in_statement;
20480
20481   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20482      This preserves the "not within loop or switch" style error messages
20483      for nonsense cases like
20484         void foo() {
20485         #pragma omp single
20486           break;
20487         }
20488   */
20489   if (parser->in_statement)
20490     parser->in_statement = IN_OMP_BLOCK;
20491
20492   return save;
20493 }
20494
20495 static void
20496 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20497 {
20498   parser->in_statement = save;
20499 }
20500
20501 static tree
20502 cp_parser_omp_structured_block (cp_parser *parser)
20503 {
20504   tree stmt = begin_omp_structured_block ();
20505   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20506
20507   cp_parser_statement (parser, NULL_TREE, false, NULL);
20508
20509   cp_parser_end_omp_structured_block (parser, save);
20510   return finish_omp_structured_block (stmt);
20511 }
20512
20513 /* OpenMP 2.5:
20514    # pragma omp atomic new-line
20515      expression-stmt
20516
20517    expression-stmt:
20518      x binop= expr | x++ | ++x | x-- | --x
20519    binop:
20520      +, *, -, /, &, ^, |, <<, >>
20521
20522   where x is an lvalue expression with scalar type.  */
20523
20524 static void
20525 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20526 {
20527   tree lhs, rhs;
20528   enum tree_code code;
20529
20530   cp_parser_require_pragma_eol (parser, pragma_tok);
20531
20532   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20533                                     /*cast_p=*/false);
20534   switch (TREE_CODE (lhs))
20535     {
20536     case ERROR_MARK:
20537       goto saw_error;
20538
20539     case PREINCREMENT_EXPR:
20540     case POSTINCREMENT_EXPR:
20541       lhs = TREE_OPERAND (lhs, 0);
20542       code = PLUS_EXPR;
20543       rhs = integer_one_node;
20544       break;
20545
20546     case PREDECREMENT_EXPR:
20547     case POSTDECREMENT_EXPR:
20548       lhs = TREE_OPERAND (lhs, 0);
20549       code = MINUS_EXPR;
20550       rhs = integer_one_node;
20551       break;
20552
20553     default:
20554       switch (cp_lexer_peek_token (parser->lexer)->type)
20555         {
20556         case CPP_MULT_EQ:
20557           code = MULT_EXPR;
20558           break;
20559         case CPP_DIV_EQ:
20560           code = TRUNC_DIV_EXPR;
20561           break;
20562         case CPP_PLUS_EQ:
20563           code = PLUS_EXPR;
20564           break;
20565         case CPP_MINUS_EQ:
20566           code = MINUS_EXPR;
20567           break;
20568         case CPP_LSHIFT_EQ:
20569           code = LSHIFT_EXPR;
20570           break;
20571         case CPP_RSHIFT_EQ:
20572           code = RSHIFT_EXPR;
20573           break;
20574         case CPP_AND_EQ:
20575           code = BIT_AND_EXPR;
20576           break;
20577         case CPP_OR_EQ:
20578           code = BIT_IOR_EXPR;
20579           break;
20580         case CPP_XOR_EQ:
20581           code = BIT_XOR_EXPR;
20582           break;
20583         default:
20584           cp_parser_error (parser,
20585                            "invalid operator for %<#pragma omp atomic%>");
20586           goto saw_error;
20587         }
20588       cp_lexer_consume_token (parser->lexer);
20589
20590       rhs = cp_parser_expression (parser, false);
20591       if (rhs == error_mark_node)
20592         goto saw_error;
20593       break;
20594     }
20595   finish_omp_atomic (code, lhs, rhs);
20596   cp_parser_consume_semicolon_at_end_of_statement (parser);
20597   return;
20598
20599  saw_error:
20600   cp_parser_skip_to_end_of_block_or_statement (parser);
20601 }
20602
20603
20604 /* OpenMP 2.5:
20605    # pragma omp barrier new-line  */
20606
20607 static void
20608 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20609 {
20610   cp_parser_require_pragma_eol (parser, pragma_tok);
20611   finish_omp_barrier ();
20612 }
20613
20614 /* OpenMP 2.5:
20615    # pragma omp critical [(name)] new-line
20616      structured-block  */
20617
20618 static tree
20619 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20620 {
20621   tree stmt, name = NULL;
20622
20623   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20624     {
20625       cp_lexer_consume_token (parser->lexer);
20626
20627       name = cp_parser_identifier (parser);
20628
20629       if (name == error_mark_node
20630           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20631         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20632                                                /*or_comma=*/false,
20633                                                /*consume_paren=*/true);
20634       if (name == error_mark_node)
20635         name = NULL;
20636     }
20637   cp_parser_require_pragma_eol (parser, pragma_tok);
20638
20639   stmt = cp_parser_omp_structured_block (parser);
20640   return c_finish_omp_critical (stmt, name);
20641 }
20642
20643 /* OpenMP 2.5:
20644    # pragma omp flush flush-vars[opt] new-line
20645
20646    flush-vars:
20647      ( variable-list ) */
20648
20649 static void
20650 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20651 {
20652   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20653     (void) cp_parser_omp_var_list (parser, 0, NULL);
20654   cp_parser_require_pragma_eol (parser, pragma_tok);
20655
20656   finish_omp_flush ();
20657 }
20658
20659 /* Helper function, to parse omp for increment expression.  */
20660
20661 static tree
20662 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20663 {
20664   tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20665   enum tree_code op;
20666   cp_token *token;
20667
20668   if (lhs != decl)
20669     {
20670       cp_parser_skip_to_end_of_statement (parser);
20671       return error_mark_node;
20672     }
20673
20674   token = cp_lexer_peek_token (parser->lexer);
20675   op = binops_by_token [token->type].tree_type;
20676   switch (op)
20677     {
20678     case LT_EXPR:
20679     case LE_EXPR:
20680     case GT_EXPR:
20681     case GE_EXPR:
20682       break;
20683     default:
20684       cp_parser_skip_to_end_of_statement (parser);
20685       return error_mark_node;
20686     }
20687
20688   cp_lexer_consume_token (parser->lexer);
20689   rhs = cp_parser_binary_expression (parser, false,
20690                                      PREC_RELATIONAL_EXPRESSION);
20691   if (rhs == error_mark_node
20692       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20693     {
20694       cp_parser_skip_to_end_of_statement (parser);
20695       return error_mark_node;
20696     }
20697
20698   return build2 (op, boolean_type_node, lhs, rhs);
20699 }
20700
20701 /* Helper function, to parse omp for increment expression.  */
20702
20703 static tree
20704 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20705 {
20706   cp_token *token = cp_lexer_peek_token (parser->lexer);
20707   enum tree_code op;
20708   tree lhs, rhs;
20709   cp_id_kind idk;
20710   bool decl_first;
20711
20712   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20713     {
20714       op = (token->type == CPP_PLUS_PLUS
20715             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20716       cp_lexer_consume_token (parser->lexer);
20717       lhs = cp_parser_cast_expression (parser, false, false);
20718       if (lhs != decl)
20719         return error_mark_node;
20720       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20721     }
20722
20723   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20724   if (lhs != decl)
20725     return error_mark_node;
20726
20727   token = cp_lexer_peek_token (parser->lexer);
20728   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20729     {
20730       op = (token->type == CPP_PLUS_PLUS
20731             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20732       cp_lexer_consume_token (parser->lexer);
20733       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20734     }
20735
20736   op = cp_parser_assignment_operator_opt (parser);
20737   if (op == ERROR_MARK)
20738     return error_mark_node;
20739
20740   if (op != NOP_EXPR)
20741     {
20742       rhs = cp_parser_assignment_expression (parser, false);
20743       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20744       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20745     }
20746
20747   lhs = cp_parser_binary_expression (parser, false,
20748                                      PREC_ADDITIVE_EXPRESSION);
20749   token = cp_lexer_peek_token (parser->lexer);
20750   decl_first = lhs == decl;
20751   if (decl_first)
20752     lhs = NULL_TREE;
20753   if (token->type != CPP_PLUS
20754       && token->type != CPP_MINUS)
20755     return error_mark_node;
20756
20757   do
20758     {
20759       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20760       cp_lexer_consume_token (parser->lexer);
20761       rhs = cp_parser_binary_expression (parser, false,
20762                                          PREC_ADDITIVE_EXPRESSION);
20763       token = cp_lexer_peek_token (parser->lexer);
20764       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20765         {
20766           if (lhs == NULL_TREE)
20767             {
20768               if (op == PLUS_EXPR)
20769                 lhs = rhs;
20770               else
20771                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20772             }
20773           else
20774             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20775                                      NULL, tf_warning_or_error);
20776         }
20777     }
20778   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20779
20780   if (!decl_first)
20781     {
20782       if (rhs != decl || op == MINUS_EXPR)
20783         return error_mark_node;
20784       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20785     }
20786   else
20787     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20788
20789   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20790 }
20791
20792 /* Parse the restricted form of the for statement allowed by OpenMP.  */
20793
20794 static tree
20795 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20796 {
20797   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20798   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20799   tree this_pre_body, cl;
20800   location_t loc_first;
20801   bool collapse_err = false;
20802   int i, collapse = 1, nbraces = 0;
20803
20804   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20805     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20806       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20807
20808   gcc_assert (collapse >= 1);
20809
20810   declv = make_tree_vec (collapse);
20811   initv = make_tree_vec (collapse);
20812   condv = make_tree_vec (collapse);
20813   incrv = make_tree_vec (collapse);
20814
20815   loc_first = cp_lexer_peek_token (parser->lexer)->location;
20816
20817   for (i = 0; i < collapse; i++)
20818     {
20819       int bracecount = 0;
20820       bool add_private_clause = false;
20821       location_t loc;
20822
20823       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20824         {
20825           cp_parser_error (parser, "for statement expected");
20826           return NULL;
20827         }
20828       loc = cp_lexer_consume_token (parser->lexer)->location;
20829
20830       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20831         return NULL;
20832
20833       init = decl = real_decl = NULL;
20834       this_pre_body = push_stmt_list ();
20835       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20836         {
20837           cp_decl_specifier_seq type_specifiers;
20838
20839           /* First, try to parse as an initialized declaration.  See
20840              cp_parser_condition, from whence the bulk of this is copied.  */
20841
20842           cp_parser_parse_tentatively (parser);
20843           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20844                                         &type_specifiers);
20845           if (!cp_parser_error_occurred (parser))
20846             {
20847               tree asm_specification, attributes;
20848               cp_declarator *declarator;
20849
20850               declarator = cp_parser_declarator (parser,
20851                                                  CP_PARSER_DECLARATOR_NAMED,
20852                                                  /*ctor_dtor_or_conv_p=*/NULL,
20853                                                  /*parenthesized_p=*/NULL,
20854                                                  /*member_p=*/false);
20855               attributes = cp_parser_attributes_opt (parser);
20856               asm_specification = cp_parser_asm_specification_opt (parser);
20857
20858               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
20859                 cp_parser_require (parser, CPP_EQ, "%<=%>");
20860               if (cp_parser_parse_definitely (parser))
20861                 {
20862                   tree pushed_scope;
20863
20864                   decl = start_decl (declarator, &type_specifiers,
20865                                      /*initialized_p=*/false, attributes,
20866                                      /*prefix_attributes=*/NULL_TREE,
20867                                      &pushed_scope);
20868
20869                   if (CLASS_TYPE_P (TREE_TYPE (decl))
20870                       || type_dependent_expression_p (decl))
20871                     {
20872                       bool is_direct_init, is_non_constant_init;
20873
20874                       init = cp_parser_initializer (parser,
20875                                                     &is_direct_init,
20876                                                     &is_non_constant_init);
20877
20878                       cp_finish_decl (decl, init, !is_non_constant_init,
20879                                       asm_specification,
20880                                       LOOKUP_ONLYCONVERTING);
20881                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
20882                         {
20883                           for_block
20884                             = tree_cons (NULL, this_pre_body, for_block);
20885                           init = NULL_TREE;
20886                         }
20887                       else
20888                         init = pop_stmt_list (this_pre_body);
20889                       this_pre_body = NULL_TREE;
20890                     }
20891                   else
20892                     {
20893                       cp_parser_require (parser, CPP_EQ, "%<=%>");
20894                       init = cp_parser_assignment_expression (parser, false);
20895
20896                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20897                         init = error_mark_node;
20898                       else
20899                         cp_finish_decl (decl, NULL_TREE,
20900                                         /*init_const_expr_p=*/false,
20901                                         asm_specification,
20902                                         LOOKUP_ONLYCONVERTING);
20903                     }
20904
20905                   if (pushed_scope)
20906                     pop_scope (pushed_scope);
20907                 }
20908             }
20909           else
20910             cp_parser_abort_tentative_parse (parser);
20911
20912           /* If parsing as an initialized declaration failed, try again as
20913              a simple expression.  */
20914           if (decl == NULL)
20915             {
20916               cp_id_kind idk;
20917               cp_parser_parse_tentatively (parser);
20918               decl = cp_parser_primary_expression (parser, false, false,
20919                                                    false, &idk);
20920               if (!cp_parser_error_occurred (parser)
20921                   && decl
20922                   && DECL_P (decl)
20923                   && CLASS_TYPE_P (TREE_TYPE (decl)))
20924                 {
20925                   tree rhs;
20926
20927                   cp_parser_parse_definitely (parser);
20928                   cp_parser_require (parser, CPP_EQ, "%<=%>");
20929                   rhs = cp_parser_assignment_expression (parser, false);
20930                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
20931                                                          rhs,
20932                                                          tf_warning_or_error));
20933                   add_private_clause = true;
20934                 }
20935               else
20936                 {
20937                   decl = NULL;
20938                   cp_parser_abort_tentative_parse (parser);
20939                   init = cp_parser_expression (parser, false);
20940                   if (init)
20941                     {
20942                       if (TREE_CODE (init) == MODIFY_EXPR
20943                           || TREE_CODE (init) == MODOP_EXPR)
20944                         real_decl = TREE_OPERAND (init, 0);
20945                     }
20946                 }
20947             }
20948         }
20949       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
20950       if (this_pre_body)
20951         {
20952           this_pre_body = pop_stmt_list (this_pre_body);
20953           if (pre_body)
20954             {
20955               tree t = pre_body;
20956               pre_body = push_stmt_list ();
20957               add_stmt (t);
20958               add_stmt (this_pre_body);
20959               pre_body = pop_stmt_list (pre_body);
20960             }
20961           else
20962             pre_body = this_pre_body;
20963         }
20964
20965       if (decl)
20966         real_decl = decl;
20967       if (par_clauses != NULL && real_decl != NULL_TREE)
20968         {
20969           tree *c;
20970           for (c = par_clauses; *c ; )
20971             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
20972                 && OMP_CLAUSE_DECL (*c) == real_decl)
20973               {
20974                 error ("%Hiteration variable %qD should not be firstprivate",
20975                        &loc, real_decl);
20976                 *c = OMP_CLAUSE_CHAIN (*c);
20977               }
20978             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
20979                      && OMP_CLAUSE_DECL (*c) == real_decl)
20980               {
20981                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
20982                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
20983                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
20984                 OMP_CLAUSE_DECL (l) = real_decl;
20985                 OMP_CLAUSE_CHAIN (l) = clauses;
20986                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
20987                 clauses = l;
20988                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
20989                 CP_OMP_CLAUSE_INFO (*c) = NULL;
20990                 add_private_clause = false;
20991               }
20992             else
20993               {
20994                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
20995                     && OMP_CLAUSE_DECL (*c) == real_decl)
20996                   add_private_clause = false;
20997                 c = &OMP_CLAUSE_CHAIN (*c);
20998               }
20999         }
21000
21001       if (add_private_clause)
21002         {
21003           tree c;
21004           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21005             {
21006               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21007                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21008                   && OMP_CLAUSE_DECL (c) == decl)
21009                 break;
21010               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21011                        && OMP_CLAUSE_DECL (c) == decl)
21012                 error ("%Hiteration variable %qD should not be firstprivate",
21013                        &loc, decl);
21014               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21015                        && OMP_CLAUSE_DECL (c) == decl)
21016                 error ("%Hiteration variable %qD should not be reduction",
21017                        &loc, decl);
21018             }
21019           if (c == NULL)
21020             {
21021               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21022               OMP_CLAUSE_DECL (c) = decl;
21023               c = finish_omp_clauses (c);
21024               if (c)
21025                 {
21026                   OMP_CLAUSE_CHAIN (c) = clauses;
21027                   clauses = c;
21028                 }
21029             }
21030         }
21031
21032       cond = NULL;
21033       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21034         {
21035           /* If decl is an iterator, preserve LHS and RHS of the relational
21036              expr until finish_omp_for.  */
21037           if (decl
21038               && (type_dependent_expression_p (decl)
21039                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21040             cond = cp_parser_omp_for_cond (parser, decl);
21041           else
21042             cond = cp_parser_condition (parser);
21043         }
21044       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21045
21046       incr = NULL;
21047       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21048         {
21049           /* If decl is an iterator, preserve the operator on decl
21050              until finish_omp_for.  */
21051           if (decl
21052               && (type_dependent_expression_p (decl)
21053                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21054             incr = cp_parser_omp_for_incr (parser, decl);
21055           else
21056             incr = cp_parser_expression (parser, false);
21057         }
21058
21059       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21060         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21061                                                /*or_comma=*/false,
21062                                                /*consume_paren=*/true);
21063
21064       TREE_VEC_ELT (declv, i) = decl;
21065       TREE_VEC_ELT (initv, i) = init;
21066       TREE_VEC_ELT (condv, i) = cond;
21067       TREE_VEC_ELT (incrv, i) = incr;
21068
21069       if (i == collapse - 1)
21070         break;
21071
21072       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21073          in between the collapsed for loops to be still considered perfectly
21074          nested.  Hopefully the final version clarifies this.
21075          For now handle (multiple) {'s and empty statements.  */
21076       cp_parser_parse_tentatively (parser);
21077       do
21078         {
21079           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21080             break;
21081           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21082             {
21083               cp_lexer_consume_token (parser->lexer);
21084               bracecount++;
21085             }
21086           else if (bracecount
21087                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21088             cp_lexer_consume_token (parser->lexer);
21089           else
21090             {
21091               loc = cp_lexer_peek_token (parser->lexer)->location;
21092               error ("%Hnot enough collapsed for loops", &loc);
21093               collapse_err = true;
21094               cp_parser_abort_tentative_parse (parser);
21095               declv = NULL_TREE;
21096               break;
21097             }
21098         }
21099       while (1);
21100
21101       if (declv)
21102         {
21103           cp_parser_parse_definitely (parser);
21104           nbraces += bracecount;
21105         }
21106     }
21107
21108   /* Note that we saved the original contents of this flag when we entered
21109      the structured block, and so we don't need to re-save it here.  */
21110   parser->in_statement = IN_OMP_FOR;
21111
21112   /* Note that the grammar doesn't call for a structured block here,
21113      though the loop as a whole is a structured block.  */
21114   body = push_stmt_list ();
21115   cp_parser_statement (parser, NULL_TREE, false, NULL);
21116   body = pop_stmt_list (body);
21117
21118   if (declv == NULL_TREE)
21119     ret = NULL_TREE;
21120   else
21121     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21122                           pre_body, clauses);
21123
21124   while (nbraces)
21125     {
21126       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21127         {
21128           cp_lexer_consume_token (parser->lexer);
21129           nbraces--;
21130         }
21131       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21132         cp_lexer_consume_token (parser->lexer);
21133       else
21134         {
21135           if (!collapse_err)
21136             {
21137               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21138               error ("%Hcollapsed loops not perfectly nested", &loc);
21139             }
21140           collapse_err = true;
21141           cp_parser_statement_seq_opt (parser, NULL);
21142           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21143         }
21144     }
21145
21146   while (for_block)
21147     {
21148       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21149       for_block = TREE_CHAIN (for_block);
21150     }
21151
21152   return ret;
21153 }
21154
21155 /* OpenMP 2.5:
21156    #pragma omp for for-clause[optseq] new-line
21157      for-loop  */
21158
21159 #define OMP_FOR_CLAUSE_MASK                             \
21160         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21161         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21162         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21163         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21164         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21165         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21166         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21167         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21168
21169 static tree
21170 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21171 {
21172   tree clauses, sb, ret;
21173   unsigned int save;
21174
21175   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21176                                        "#pragma omp for", pragma_tok);
21177
21178   sb = begin_omp_structured_block ();
21179   save = cp_parser_begin_omp_structured_block (parser);
21180
21181   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21182
21183   cp_parser_end_omp_structured_block (parser, save);
21184   add_stmt (finish_omp_structured_block (sb));
21185
21186   return ret;
21187 }
21188
21189 /* OpenMP 2.5:
21190    # pragma omp master new-line
21191      structured-block  */
21192
21193 static tree
21194 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21195 {
21196   cp_parser_require_pragma_eol (parser, pragma_tok);
21197   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21198 }
21199
21200 /* OpenMP 2.5:
21201    # pragma omp ordered new-line
21202      structured-block  */
21203
21204 static tree
21205 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21206 {
21207   cp_parser_require_pragma_eol (parser, pragma_tok);
21208   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21209 }
21210
21211 /* OpenMP 2.5:
21212
21213    section-scope:
21214      { section-sequence }
21215
21216    section-sequence:
21217      section-directive[opt] structured-block
21218      section-sequence section-directive structured-block  */
21219
21220 static tree
21221 cp_parser_omp_sections_scope (cp_parser *parser)
21222 {
21223   tree stmt, substmt;
21224   bool error_suppress = false;
21225   cp_token *tok;
21226
21227   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21228     return NULL_TREE;
21229
21230   stmt = push_stmt_list ();
21231
21232   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21233     {
21234       unsigned save;
21235
21236       substmt = begin_omp_structured_block ();
21237       save = cp_parser_begin_omp_structured_block (parser);
21238
21239       while (1)
21240         {
21241           cp_parser_statement (parser, NULL_TREE, false, NULL);
21242
21243           tok = cp_lexer_peek_token (parser->lexer);
21244           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21245             break;
21246           if (tok->type == CPP_CLOSE_BRACE)
21247             break;
21248           if (tok->type == CPP_EOF)
21249             break;
21250         }
21251
21252       cp_parser_end_omp_structured_block (parser, save);
21253       substmt = finish_omp_structured_block (substmt);
21254       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21255       add_stmt (substmt);
21256     }
21257
21258   while (1)
21259     {
21260       tok = cp_lexer_peek_token (parser->lexer);
21261       if (tok->type == CPP_CLOSE_BRACE)
21262         break;
21263       if (tok->type == CPP_EOF)
21264         break;
21265
21266       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21267         {
21268           cp_lexer_consume_token (parser->lexer);
21269           cp_parser_require_pragma_eol (parser, tok);
21270           error_suppress = false;
21271         }
21272       else if (!error_suppress)
21273         {
21274           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21275           error_suppress = true;
21276         }
21277
21278       substmt = cp_parser_omp_structured_block (parser);
21279       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21280       add_stmt (substmt);
21281     }
21282   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21283
21284   substmt = pop_stmt_list (stmt);
21285
21286   stmt = make_node (OMP_SECTIONS);
21287   TREE_TYPE (stmt) = void_type_node;
21288   OMP_SECTIONS_BODY (stmt) = substmt;
21289
21290   add_stmt (stmt);
21291   return stmt;
21292 }
21293
21294 /* OpenMP 2.5:
21295    # pragma omp sections sections-clause[optseq] newline
21296      sections-scope  */
21297
21298 #define OMP_SECTIONS_CLAUSE_MASK                        \
21299         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21300         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21301         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21302         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21303         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21304
21305 static tree
21306 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21307 {
21308   tree clauses, ret;
21309
21310   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21311                                        "#pragma omp sections", pragma_tok);
21312
21313   ret = cp_parser_omp_sections_scope (parser);
21314   if (ret)
21315     OMP_SECTIONS_CLAUSES (ret) = clauses;
21316
21317   return ret;
21318 }
21319
21320 /* OpenMP 2.5:
21321    # pragma parallel parallel-clause new-line
21322    # pragma parallel for parallel-for-clause new-line
21323    # pragma parallel sections parallel-sections-clause new-line  */
21324
21325 #define OMP_PARALLEL_CLAUSE_MASK                        \
21326         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21327         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21328         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21329         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21330         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21331         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21332         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21333         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21334
21335 static tree
21336 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21337 {
21338   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21339   const char *p_name = "#pragma omp parallel";
21340   tree stmt, clauses, par_clause, ws_clause, block;
21341   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21342   unsigned int save;
21343
21344   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21345     {
21346       cp_lexer_consume_token (parser->lexer);
21347       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21348       p_name = "#pragma omp parallel for";
21349       mask |= OMP_FOR_CLAUSE_MASK;
21350       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21351     }
21352   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21353     {
21354       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21355       const char *p = IDENTIFIER_POINTER (id);
21356       if (strcmp (p, "sections") == 0)
21357         {
21358           cp_lexer_consume_token (parser->lexer);
21359           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21360           p_name = "#pragma omp parallel sections";
21361           mask |= OMP_SECTIONS_CLAUSE_MASK;
21362           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21363         }
21364     }
21365
21366   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21367   block = begin_omp_parallel ();
21368   save = cp_parser_begin_omp_structured_block (parser);
21369
21370   switch (p_kind)
21371     {
21372     case PRAGMA_OMP_PARALLEL:
21373       cp_parser_statement (parser, NULL_TREE, false, NULL);
21374       par_clause = clauses;
21375       break;
21376
21377     case PRAGMA_OMP_PARALLEL_FOR:
21378       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21379       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21380       break;
21381
21382     case PRAGMA_OMP_PARALLEL_SECTIONS:
21383       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21384       stmt = cp_parser_omp_sections_scope (parser);
21385       if (stmt)
21386         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21387       break;
21388
21389     default:
21390       gcc_unreachable ();
21391     }
21392
21393   cp_parser_end_omp_structured_block (parser, save);
21394   stmt = finish_omp_parallel (par_clause, block);
21395   if (p_kind != PRAGMA_OMP_PARALLEL)
21396     OMP_PARALLEL_COMBINED (stmt) = 1;
21397   return stmt;
21398 }
21399
21400 /* OpenMP 2.5:
21401    # pragma omp single single-clause[optseq] new-line
21402      structured-block  */
21403
21404 #define OMP_SINGLE_CLAUSE_MASK                          \
21405         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21406         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21407         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21408         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21409
21410 static tree
21411 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21412 {
21413   tree stmt = make_node (OMP_SINGLE);
21414   TREE_TYPE (stmt) = void_type_node;
21415
21416   OMP_SINGLE_CLAUSES (stmt)
21417     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21418                                  "#pragma omp single", pragma_tok);
21419   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21420
21421   return add_stmt (stmt);
21422 }
21423
21424 /* OpenMP 3.0:
21425    # pragma omp task task-clause[optseq] new-line
21426      structured-block  */
21427
21428 #define OMP_TASK_CLAUSE_MASK                            \
21429         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21430         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21431         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21432         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21433         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21434         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21435
21436 static tree
21437 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21438 {
21439   tree clauses, block;
21440   unsigned int save;
21441
21442   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21443                                        "#pragma omp task", pragma_tok);
21444   block = begin_omp_task ();
21445   save = cp_parser_begin_omp_structured_block (parser);
21446   cp_parser_statement (parser, NULL_TREE, false, NULL);
21447   cp_parser_end_omp_structured_block (parser, save);
21448   return finish_omp_task (clauses, block);
21449 }
21450
21451 /* OpenMP 3.0:
21452    # pragma omp taskwait new-line  */
21453
21454 static void
21455 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21456 {
21457   cp_parser_require_pragma_eol (parser, pragma_tok);
21458   finish_omp_taskwait ();
21459 }
21460
21461 /* OpenMP 2.5:
21462    # pragma omp threadprivate (variable-list) */
21463
21464 static void
21465 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21466 {
21467   tree vars;
21468
21469   vars = cp_parser_omp_var_list (parser, 0, NULL);
21470   cp_parser_require_pragma_eol (parser, pragma_tok);
21471
21472   finish_omp_threadprivate (vars);
21473 }
21474
21475 /* Main entry point to OpenMP statement pragmas.  */
21476
21477 static void
21478 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21479 {
21480   tree stmt;
21481
21482   switch (pragma_tok->pragma_kind)
21483     {
21484     case PRAGMA_OMP_ATOMIC:
21485       cp_parser_omp_atomic (parser, pragma_tok);
21486       return;
21487     case PRAGMA_OMP_CRITICAL:
21488       stmt = cp_parser_omp_critical (parser, pragma_tok);
21489       break;
21490     case PRAGMA_OMP_FOR:
21491       stmt = cp_parser_omp_for (parser, pragma_tok);
21492       break;
21493     case PRAGMA_OMP_MASTER:
21494       stmt = cp_parser_omp_master (parser, pragma_tok);
21495       break;
21496     case PRAGMA_OMP_ORDERED:
21497       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21498       break;
21499     case PRAGMA_OMP_PARALLEL:
21500       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21501       break;
21502     case PRAGMA_OMP_SECTIONS:
21503       stmt = cp_parser_omp_sections (parser, pragma_tok);
21504       break;
21505     case PRAGMA_OMP_SINGLE:
21506       stmt = cp_parser_omp_single (parser, pragma_tok);
21507       break;
21508     case PRAGMA_OMP_TASK:
21509       stmt = cp_parser_omp_task (parser, pragma_tok);
21510       break;
21511     default:
21512       gcc_unreachable ();
21513     }
21514
21515   if (stmt)
21516     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21517 }
21518 \f
21519 /* The parser.  */
21520
21521 static GTY (()) cp_parser *the_parser;
21522
21523 \f
21524 /* Special handling for the first token or line in the file.  The first
21525    thing in the file might be #pragma GCC pch_preprocess, which loads a
21526    PCH file, which is a GC collection point.  So we need to handle this
21527    first pragma without benefit of an existing lexer structure.
21528
21529    Always returns one token to the caller in *FIRST_TOKEN.  This is
21530    either the true first token of the file, or the first token after
21531    the initial pragma.  */
21532
21533 static void
21534 cp_parser_initial_pragma (cp_token *first_token)
21535 {
21536   tree name = NULL;
21537
21538   cp_lexer_get_preprocessor_token (NULL, first_token);
21539   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21540     return;
21541
21542   cp_lexer_get_preprocessor_token (NULL, first_token);
21543   if (first_token->type == CPP_STRING)
21544     {
21545       name = first_token->u.value;
21546
21547       cp_lexer_get_preprocessor_token (NULL, first_token);
21548       if (first_token->type != CPP_PRAGMA_EOL)
21549         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21550                &first_token->location);
21551     }
21552   else
21553     error ("%Hexpected string literal", &first_token->location);
21554
21555   /* Skip to the end of the pragma.  */
21556   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21557     cp_lexer_get_preprocessor_token (NULL, first_token);
21558
21559   /* Now actually load the PCH file.  */
21560   if (name)
21561     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21562
21563   /* Read one more token to return to our caller.  We have to do this
21564      after reading the PCH file in, since its pointers have to be
21565      live.  */
21566   cp_lexer_get_preprocessor_token (NULL, first_token);
21567 }
21568
21569 /* Normal parsing of a pragma token.  Here we can (and must) use the
21570    regular lexer.  */
21571
21572 static bool
21573 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21574 {
21575   cp_token *pragma_tok;
21576   unsigned int id;
21577
21578   pragma_tok = cp_lexer_consume_token (parser->lexer);
21579   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21580   parser->lexer->in_pragma = true;
21581
21582   id = pragma_tok->pragma_kind;
21583   switch (id)
21584     {
21585     case PRAGMA_GCC_PCH_PREPROCESS:
21586       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21587              &pragma_tok->location);
21588       break;
21589
21590     case PRAGMA_OMP_BARRIER:
21591       switch (context)
21592         {
21593         case pragma_compound:
21594           cp_parser_omp_barrier (parser, pragma_tok);
21595           return false;
21596         case pragma_stmt:
21597           error ("%H%<#pragma omp barrier%> may only be "
21598                  "used in compound statements", &pragma_tok->location);
21599           break;
21600         default:
21601           goto bad_stmt;
21602         }
21603       break;
21604
21605     case PRAGMA_OMP_FLUSH:
21606       switch (context)
21607         {
21608         case pragma_compound:
21609           cp_parser_omp_flush (parser, pragma_tok);
21610           return false;
21611         case pragma_stmt:
21612           error ("%H%<#pragma omp flush%> may only be "
21613                  "used in compound statements", &pragma_tok->location);
21614           break;
21615         default:
21616           goto bad_stmt;
21617         }
21618       break;
21619
21620     case PRAGMA_OMP_TASKWAIT:
21621       switch (context)
21622         {
21623         case pragma_compound:
21624           cp_parser_omp_taskwait (parser, pragma_tok);
21625           return false;
21626         case pragma_stmt:
21627           error ("%H%<#pragma omp taskwait%> may only be "
21628                  "used in compound statements",
21629                  &pragma_tok->location);
21630           break;
21631         default:
21632           goto bad_stmt;
21633         }
21634       break;
21635
21636     case PRAGMA_OMP_THREADPRIVATE:
21637       cp_parser_omp_threadprivate (parser, pragma_tok);
21638       return false;
21639
21640     case PRAGMA_OMP_ATOMIC:
21641     case PRAGMA_OMP_CRITICAL:
21642     case PRAGMA_OMP_FOR:
21643     case PRAGMA_OMP_MASTER:
21644     case PRAGMA_OMP_ORDERED:
21645     case PRAGMA_OMP_PARALLEL:
21646     case PRAGMA_OMP_SECTIONS:
21647     case PRAGMA_OMP_SINGLE:
21648     case PRAGMA_OMP_TASK:
21649       if (context == pragma_external)
21650         goto bad_stmt;
21651       cp_parser_omp_construct (parser, pragma_tok);
21652       return true;
21653
21654     case PRAGMA_OMP_SECTION:
21655       error ("%H%<#pragma omp section%> may only be used in "
21656              "%<#pragma omp sections%> construct", &pragma_tok->location);
21657       break;
21658
21659     default:
21660       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21661       c_invoke_pragma_handler (id);
21662       break;
21663
21664     bad_stmt:
21665       cp_parser_error (parser, "expected declaration specifiers");
21666       break;
21667     }
21668
21669   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21670   return false;
21671 }
21672
21673 /* The interface the pragma parsers have to the lexer.  */
21674
21675 enum cpp_ttype
21676 pragma_lex (tree *value)
21677 {
21678   cp_token *tok;
21679   enum cpp_ttype ret;
21680
21681   tok = cp_lexer_peek_token (the_parser->lexer);
21682
21683   ret = tok->type;
21684   *value = tok->u.value;
21685
21686   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21687     ret = CPP_EOF;
21688   else if (ret == CPP_STRING)
21689     *value = cp_parser_string_literal (the_parser, false, false);
21690   else
21691     {
21692       cp_lexer_consume_token (the_parser->lexer);
21693       if (ret == CPP_KEYWORD)
21694         ret = CPP_NAME;
21695     }
21696
21697   return ret;
21698 }
21699
21700 \f
21701 /* External interface.  */
21702
21703 /* Parse one entire translation unit.  */
21704
21705 void
21706 c_parse_file (void)
21707 {
21708   bool error_occurred;
21709   static bool already_called = false;
21710
21711   if (already_called)
21712     {
21713       sorry ("inter-module optimizations not implemented for C++");
21714       return;
21715     }
21716   already_called = true;
21717
21718   the_parser = cp_parser_new ();
21719   push_deferring_access_checks (flag_access_control
21720                                 ? dk_no_deferred : dk_no_check);
21721   error_occurred = cp_parser_translation_unit (the_parser);
21722   the_parser = NULL;
21723 }
21724
21725 #include "gt-cp-parser.h"